Chris Mason | 6cbd557 | 2007-06-12 09:07:21 -0400 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2007 Oracle. All rights reserved. |
| 3 | * |
| 4 | * This program is free software; you can redistribute it and/or |
| 5 | * modify it under the terms of the GNU General Public |
| 6 | * License v2 as published by the Free Software Foundation. |
| 7 | * |
| 8 | * This program is distributed in the hope that it will be useful, |
| 9 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 10 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 11 | * General Public License for more details. |
| 12 | * |
| 13 | * You should have received a copy of the GNU General Public |
| 14 | * License along with this program; if not, write to the |
| 15 | * Free Software Foundation, Inc., 59 Temple Place - Suite 330, |
| 16 | * Boston, MA 021110-1307, USA. |
| 17 | */ |
| 18 | |
Chris Mason | 8f18cf1 | 2008-04-25 16:53:30 -0400 | [diff] [blame] | 19 | #include <linux/kernel.h> |
Chris Mason | 065631f | 2008-02-20 12:07:25 -0500 | [diff] [blame] | 20 | #include <linux/bio.h> |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 21 | #include <linux/buffer_head.h> |
Sage Weil | f2eb0a2 | 2008-05-02 14:43:14 -0400 | [diff] [blame] | 22 | #include <linux/file.h> |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 23 | #include <linux/fs.h> |
| 24 | #include <linux/pagemap.h> |
| 25 | #include <linux/highmem.h> |
| 26 | #include <linux/time.h> |
| 27 | #include <linux/init.h> |
| 28 | #include <linux/string.h> |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 29 | #include <linux/backing-dev.h> |
| 30 | #include <linux/mpage.h> |
| 31 | #include <linux/swap.h> |
| 32 | #include <linux/writeback.h> |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 33 | #include <linux/compat.h> |
Chris Mason | 9ebefb18 | 2007-06-15 13:50:00 -0400 | [diff] [blame] | 34 | #include <linux/bit_spinlock.h> |
Josef Bacik | 5103e94 | 2007-11-16 11:45:54 -0500 | [diff] [blame] | 35 | #include <linux/xattr.h> |
Josef Bacik | 33268ea | 2008-07-24 12:16:36 -0400 | [diff] [blame] | 36 | #include <linux/posix_acl.h> |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 37 | #include <linux/falloc.h> |
Tejun Heo | 5a0e3ad | 2010-03-24 17:04:11 +0900 | [diff] [blame] | 38 | #include <linux/slab.h> |
David Sterba | 7a36dde | 2011-05-06 15:33:15 +0200 | [diff] [blame] | 39 | #include <linux/ratelimit.h> |
Josef Bacik | 22c44fe | 2011-11-30 10:45:38 -0500 | [diff] [blame] | 40 | #include <linux/mount.h> |
Filipe Brandenburger | 55e301f | 2013-01-29 06:04:50 +0000 | [diff] [blame] | 41 | #include <linux/btrfs.h> |
David Woodhouse | 53b381b | 2013-01-29 18:40:14 -0500 | [diff] [blame] | 42 | #include <linux/blkdev.h> |
Josef Bacik | f23b5a5 | 2013-06-19 10:16:26 -0400 | [diff] [blame] | 43 | #include <linux/posix_acl_xattr.h> |
Christoph Hellwig | e2e40f2 | 2015-02-22 08:58:50 -0800 | [diff] [blame] | 44 | #include <linux/uio.h> |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 45 | #include "ctree.h" |
| 46 | #include "disk-io.h" |
| 47 | #include "transaction.h" |
| 48 | #include "btrfs_inode.h" |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 49 | #include "print-tree.h" |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 50 | #include "ordered-data.h" |
Christoph Hellwig | 95819c0 | 2008-08-28 06:21:17 -0400 | [diff] [blame] | 51 | #include "xattr.h" |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 52 | #include "tree-log.h" |
Jan Schmidt | 4a54c8c | 2011-07-22 15:41:52 +0200 | [diff] [blame] | 53 | #include "volumes.h" |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 54 | #include "compression.h" |
Chris Mason | b4ce94d | 2009-02-04 09:25:08 -0500 | [diff] [blame] | 55 | #include "locking.h" |
Josef Bacik | dc89e98 | 2011-01-28 17:05:48 -0500 | [diff] [blame] | 56 | #include "free-space-cache.h" |
Li Zefan | 581bb05 | 2011-04-20 10:06:11 +0800 | [diff] [blame] | 57 | #include "inode-map.h" |
Liu Bo | 38c227d | 2013-01-29 03:18:40 +0000 | [diff] [blame] | 58 | #include "backref.h" |
Josef Bacik | f23b5a5 | 2013-06-19 10:16:26 -0400 | [diff] [blame] | 59 | #include "hash.h" |
Filipe David Borba Manana | 6354192 | 2014-01-07 11:47:46 +0000 | [diff] [blame] | 60 | #include "props.h" |
Dongsheng Yang | 3119321 | 2014-12-12 16:44:35 +0800 | [diff] [blame] | 61 | #include "qgroup.h" |
Wang Xiaoguang | dda3245 | 2016-07-11 11:05:29 +0800 | [diff] [blame] | 62 | #include "dedupe.h" |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 63 | |
| 64 | struct btrfs_iget_args { |
Chris Mason | 90d3e59 | 2014-01-09 17:28:00 -0800 | [diff] [blame] | 65 | struct btrfs_key *location; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 66 | struct btrfs_root *root; |
| 67 | }; |
| 68 | |
Filipe Manana | f28a492 | 2015-12-08 19:23:20 +0000 | [diff] [blame] | 69 | struct btrfs_dio_data { |
| 70 | u64 outstanding_extents; |
| 71 | u64 reserve; |
| 72 | u64 unsubmitted_oe_range_start; |
| 73 | u64 unsubmitted_oe_range_end; |
| 74 | }; |
| 75 | |
Alexey Dobriyan | 6e1d5dc | 2009-09-21 17:01:11 -0700 | [diff] [blame] | 76 | static const struct inode_operations btrfs_dir_inode_operations; |
| 77 | static const struct inode_operations btrfs_symlink_inode_operations; |
| 78 | static const struct inode_operations btrfs_dir_ro_inode_operations; |
| 79 | static const struct inode_operations btrfs_special_inode_operations; |
| 80 | static const struct inode_operations btrfs_file_inode_operations; |
Alexey Dobriyan | 7f09410 | 2009-09-21 17:01:10 -0700 | [diff] [blame] | 81 | static const struct address_space_operations btrfs_aops; |
| 82 | static const struct address_space_operations btrfs_symlink_aops; |
Alexey Dobriyan | 828c095 | 2009-10-01 15:43:56 -0700 | [diff] [blame] | 83 | static const struct file_operations btrfs_dir_file_operations; |
David Sterba | 20e5506 | 2015-11-19 11:42:28 +0100 | [diff] [blame] | 84 | static const struct extent_io_ops btrfs_extent_io_ops; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 85 | |
| 86 | static struct kmem_cache *btrfs_inode_cachep; |
| 87 | struct kmem_cache *btrfs_trans_handle_cachep; |
| 88 | struct kmem_cache *btrfs_transaction_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); |
Josef Bacik | 70c8a91 | 2012-10-11 16:54:30 -0400 | [diff] [blame] | 111 | static struct extent_map *create_pinned_em(struct inode *inode, u64 start, |
| 112 | u64 len, u64 orig_start, |
| 113 | u64 block_start, u64 block_len, |
Josef Bacik | cc95bef | 2013-04-04 14:31:27 -0400 | [diff] [blame] | 114 | u64 orig_block_len, u64 ram_bytes, |
| 115 | int type); |
Josef Bacik | 7b12876 | 2008-07-24 12:17:14 -0400 | [diff] [blame] | 116 | |
Eric Sandeen | 48a3b63 | 2013-04-25 20:41:01 +0000 | [diff] [blame] | 117 | static int btrfs_dirty_inode(struct inode *inode); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 118 | |
Josef Bacik | 6a3891c | 2015-03-16 17:38:52 -0400 | [diff] [blame] | 119 | #ifdef CONFIG_BTRFS_FS_RUN_SANITY_TESTS |
| 120 | void btrfs_test_inode_set_ops(struct inode *inode) |
| 121 | { |
| 122 | BTRFS_I(inode)->io_tree.ops = &btrfs_extent_io_ops; |
| 123 | } |
| 124 | #endif |
| 125 | |
Yan, Zheng | f34f57a | 2009-11-12 09:35:27 +0000 | [diff] [blame] | 126 | static int btrfs_init_inode_security(struct btrfs_trans_handle *trans, |
Eric Paris | 2a7dba3 | 2011-02-01 11:05:39 -0500 | [diff] [blame] | 127 | struct inode *inode, struct inode *dir, |
| 128 | const struct qstr *qstr) |
Jim Owens | 0279b4c | 2009-02-04 09:29:13 -0500 | [diff] [blame] | 129 | { |
| 130 | int err; |
| 131 | |
Yan, Zheng | f34f57a | 2009-11-12 09:35:27 +0000 | [diff] [blame] | 132 | err = btrfs_init_acl(trans, inode, dir); |
Jim Owens | 0279b4c | 2009-02-04 09:29:13 -0500 | [diff] [blame] | 133 | if (!err) |
Eric Paris | 2a7dba3 | 2011-02-01 11:05:39 -0500 | [diff] [blame] | 134 | err = btrfs_xattr_security_init(trans, inode, dir, qstr); |
Jim Owens | 0279b4c | 2009-02-04 09:29:13 -0500 | [diff] [blame] | 135 | return err; |
| 136 | } |
| 137 | |
Chris Mason | d352ac6 | 2008-09-29 15:18:18 -0400 | [diff] [blame] | 138 | /* |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 139 | * this does all the hard work for inserting an inline extent into |
| 140 | * the btree. The caller should have done a btrfs_drop_extents so that |
| 141 | * no overlapping inline items exist in the btree |
| 142 | */ |
Chris Mason | 40f7658 | 2014-05-21 13:35:51 -0700 | [diff] [blame] | 143 | static int insert_inline_extent(struct btrfs_trans_handle *trans, |
Filipe David Borba Manana | 1acae57 | 2014-01-07 11:42:27 +0000 | [diff] [blame] | 144 | struct btrfs_path *path, int extent_inserted, |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 145 | struct btrfs_root *root, struct inode *inode, |
| 146 | u64 start, size_t size, size_t compressed_size, |
Li Zefan | fe3f566 | 2011-03-28 08:30:38 +0000 | [diff] [blame] | 147 | int compress_type, |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 148 | struct page **compressed_pages) |
| 149 | { |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 150 | struct extent_buffer *leaf; |
| 151 | struct page *page = NULL; |
| 152 | char *kaddr; |
| 153 | unsigned long ptr; |
| 154 | struct btrfs_file_extent_item *ei; |
| 155 | int err = 0; |
| 156 | int ret; |
| 157 | size_t cur_size = size; |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 158 | unsigned long offset; |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 159 | |
Li Zefan | fe3f566 | 2011-03-28 08:30:38 +0000 | [diff] [blame] | 160 | if (compressed_size && compressed_pages) |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 161 | cur_size = compressed_size; |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 162 | |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 163 | inode_add_bytes(inode, size); |
Filipe David Borba Manana | 1acae57 | 2014-01-07 11:42:27 +0000 | [diff] [blame] | 164 | |
| 165 | if (!extent_inserted) { |
| 166 | struct btrfs_key key; |
| 167 | size_t datasize; |
| 168 | |
Nikolay Borisov | 4a0cc7c | 2017-01-10 20:35:31 +0200 | [diff] [blame] | 169 | key.objectid = btrfs_ino(BTRFS_I(inode)); |
Filipe David Borba Manana | 1acae57 | 2014-01-07 11:42:27 +0000 | [diff] [blame] | 170 | key.offset = start; |
David Sterba | 962a298 | 2014-06-04 18:41:45 +0200 | [diff] [blame] | 171 | key.type = BTRFS_EXTENT_DATA_KEY; |
Filipe David Borba Manana | 1acae57 | 2014-01-07 11:42:27 +0000 | [diff] [blame] | 172 | |
| 173 | datasize = btrfs_file_extent_calc_inline_size(cur_size); |
| 174 | path->leave_spinning = 1; |
| 175 | ret = btrfs_insert_empty_item(trans, root, path, &key, |
| 176 | datasize); |
| 177 | if (ret) { |
| 178 | err = ret; |
| 179 | goto fail; |
| 180 | } |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 181 | } |
| 182 | leaf = path->nodes[0]; |
| 183 | ei = btrfs_item_ptr(leaf, path->slots[0], |
| 184 | struct btrfs_file_extent_item); |
| 185 | btrfs_set_file_extent_generation(leaf, ei, trans->transid); |
| 186 | btrfs_set_file_extent_type(leaf, ei, BTRFS_FILE_EXTENT_INLINE); |
| 187 | btrfs_set_file_extent_encryption(leaf, ei, 0); |
| 188 | btrfs_set_file_extent_other_encoding(leaf, ei, 0); |
| 189 | btrfs_set_file_extent_ram_bytes(leaf, ei, size); |
| 190 | ptr = btrfs_file_extent_inline_start(ei); |
| 191 | |
Li Zefan | 261507a0 | 2010-12-17 14:21:50 +0800 | [diff] [blame] | 192 | if (compress_type != BTRFS_COMPRESS_NONE) { |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 193 | struct page *cpage; |
| 194 | int i = 0; |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 195 | while (compressed_size > 0) { |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 196 | cpage = compressed_pages[i]; |
Chris Mason | 5b050f0 | 2008-11-11 09:34:41 -0500 | [diff] [blame] | 197 | cur_size = min_t(unsigned long, compressed_size, |
Kirill A. Shutemov | 09cbfea | 2016-04-01 15:29:47 +0300 | [diff] [blame] | 198 | PAGE_SIZE); |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 199 | |
Cong Wang | 7ac687d | 2011-11-25 23:14:28 +0800 | [diff] [blame] | 200 | kaddr = kmap_atomic(cpage); |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 201 | write_extent_buffer(leaf, kaddr, ptr, cur_size); |
Cong Wang | 7ac687d | 2011-11-25 23:14:28 +0800 | [diff] [blame] | 202 | kunmap_atomic(kaddr); |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 203 | |
| 204 | i++; |
| 205 | ptr += cur_size; |
| 206 | compressed_size -= cur_size; |
| 207 | } |
| 208 | btrfs_set_file_extent_compression(leaf, ei, |
Li Zefan | 261507a0 | 2010-12-17 14:21:50 +0800 | [diff] [blame] | 209 | compress_type); |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 210 | } else { |
| 211 | page = find_get_page(inode->i_mapping, |
Kirill A. Shutemov | 09cbfea | 2016-04-01 15:29:47 +0300 | [diff] [blame] | 212 | start >> PAGE_SHIFT); |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 213 | btrfs_set_file_extent_compression(leaf, ei, 0); |
Cong Wang | 7ac687d | 2011-11-25 23:14:28 +0800 | [diff] [blame] | 214 | kaddr = kmap_atomic(page); |
Kirill A. Shutemov | 09cbfea | 2016-04-01 15:29:47 +0300 | [diff] [blame] | 215 | offset = start & (PAGE_SIZE - 1); |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 216 | write_extent_buffer(leaf, kaddr + offset, ptr, size); |
Cong Wang | 7ac687d | 2011-11-25 23:14:28 +0800 | [diff] [blame] | 217 | kunmap_atomic(kaddr); |
Kirill A. Shutemov | 09cbfea | 2016-04-01 15:29:47 +0300 | [diff] [blame] | 218 | put_page(page); |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 219 | } |
| 220 | btrfs_mark_buffer_dirty(leaf); |
Filipe David Borba Manana | 1acae57 | 2014-01-07 11:42:27 +0000 | [diff] [blame] | 221 | btrfs_release_path(path); |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 222 | |
Yan, Zheng | c216775 | 2009-11-12 09:34:21 +0000 | [diff] [blame] | 223 | /* |
| 224 | * we're an inline extent, so nobody can |
| 225 | * extend the file past i_size without locking |
| 226 | * a page we already have locked. |
| 227 | * |
| 228 | * We must do any isize and inode updates |
| 229 | * before we unlock the pages. Otherwise we |
| 230 | * could end up racing with unlink. |
| 231 | */ |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 232 | BTRFS_I(inode)->disk_i_size = inode->i_size; |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 233 | ret = btrfs_update_inode(trans, root, inode); |
Yan, Zheng | c216775 | 2009-11-12 09:34:21 +0000 | [diff] [blame] | 234 | |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 235 | return ret; |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 236 | fail: |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 237 | return err; |
| 238 | } |
| 239 | |
| 240 | |
| 241 | /* |
| 242 | * conditionally insert an inline extent into the file. This |
| 243 | * does the checks required to make sure the data is small enough |
| 244 | * to fit as an inline extent. |
| 245 | */ |
Josef Bacik | 0036158 | 2013-08-14 14:02:47 -0400 | [diff] [blame] | 246 | static noinline int cow_file_range_inline(struct btrfs_root *root, |
| 247 | struct inode *inode, u64 start, |
| 248 | u64 end, size_t compressed_size, |
| 249 | int compress_type, |
| 250 | struct page **compressed_pages) |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 251 | { |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 252 | struct btrfs_fs_info *fs_info = root->fs_info; |
Josef Bacik | 0036158 | 2013-08-14 14:02:47 -0400 | [diff] [blame] | 253 | struct btrfs_trans_handle *trans; |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 254 | u64 isize = i_size_read(inode); |
| 255 | u64 actual_end = min(end + 1, isize); |
| 256 | u64 inline_len = actual_end - start; |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 257 | u64 aligned_end = ALIGN(end, fs_info->sectorsize); |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 258 | u64 data_len = inline_len; |
| 259 | int ret; |
Filipe David Borba Manana | 1acae57 | 2014-01-07 11:42:27 +0000 | [diff] [blame] | 260 | struct btrfs_path *path; |
| 261 | int extent_inserted = 0; |
| 262 | u32 extent_item_size; |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 263 | |
| 264 | if (compressed_size) |
| 265 | data_len = compressed_size; |
| 266 | |
| 267 | if (start > 0 || |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 268 | actual_end > fs_info->sectorsize || |
| 269 | data_len > BTRFS_MAX_INLINE_DATA_SIZE(fs_info) || |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 270 | (!compressed_size && |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 271 | (actual_end & (fs_info->sectorsize - 1)) == 0) || |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 272 | end + 1 < isize || |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 273 | data_len > fs_info->max_inline) { |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 274 | return 1; |
| 275 | } |
| 276 | |
Filipe David Borba Manana | 1acae57 | 2014-01-07 11:42:27 +0000 | [diff] [blame] | 277 | path = btrfs_alloc_path(); |
| 278 | if (!path) |
| 279 | return -ENOMEM; |
| 280 | |
Josef Bacik | 0036158 | 2013-08-14 14:02:47 -0400 | [diff] [blame] | 281 | trans = btrfs_join_transaction(root); |
Filipe David Borba Manana | 1acae57 | 2014-01-07 11:42:27 +0000 | [diff] [blame] | 282 | if (IS_ERR(trans)) { |
| 283 | btrfs_free_path(path); |
Josef Bacik | 0036158 | 2013-08-14 14:02:47 -0400 | [diff] [blame] | 284 | return PTR_ERR(trans); |
Filipe David Borba Manana | 1acae57 | 2014-01-07 11:42:27 +0000 | [diff] [blame] | 285 | } |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 286 | trans->block_rsv = &fs_info->delalloc_block_rsv; |
Josef Bacik | 0036158 | 2013-08-14 14:02:47 -0400 | [diff] [blame] | 287 | |
Filipe David Borba Manana | 1acae57 | 2014-01-07 11:42:27 +0000 | [diff] [blame] | 288 | if (compressed_size && compressed_pages) |
| 289 | extent_item_size = btrfs_file_extent_calc_inline_size( |
| 290 | compressed_size); |
| 291 | else |
| 292 | extent_item_size = btrfs_file_extent_calc_inline_size( |
| 293 | inline_len); |
| 294 | |
| 295 | ret = __btrfs_drop_extents(trans, root, inode, path, |
| 296 | start, aligned_end, NULL, |
| 297 | 1, 1, extent_item_size, &extent_inserted); |
Josef Bacik | 0036158 | 2013-08-14 14:02:47 -0400 | [diff] [blame] | 298 | if (ret) { |
Jeff Mahoney | 6664283 | 2016-06-10 18:19:25 -0400 | [diff] [blame] | 299 | btrfs_abort_transaction(trans, ret); |
Josef Bacik | 0036158 | 2013-08-14 14:02:47 -0400 | [diff] [blame] | 300 | goto out; |
| 301 | } |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 302 | |
| 303 | if (isize > actual_end) |
| 304 | inline_len = min_t(u64, isize, actual_end); |
Filipe David Borba Manana | 1acae57 | 2014-01-07 11:42:27 +0000 | [diff] [blame] | 305 | ret = insert_inline_extent(trans, path, extent_inserted, |
| 306 | root, inode, start, |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 307 | inline_len, compressed_size, |
Li Zefan | fe3f566 | 2011-03-28 08:30:38 +0000 | [diff] [blame] | 308 | compress_type, compressed_pages); |
Josef Bacik | 2adcac1 | 2012-05-23 16:10:14 -0400 | [diff] [blame] | 309 | if (ret && ret != -ENOSPC) { |
Jeff Mahoney | 6664283 | 2016-06-10 18:19:25 -0400 | [diff] [blame] | 310 | btrfs_abort_transaction(trans, ret); |
Josef Bacik | 0036158 | 2013-08-14 14:02:47 -0400 | [diff] [blame] | 311 | goto out; |
Josef Bacik | 2adcac1 | 2012-05-23 16:10:14 -0400 | [diff] [blame] | 312 | } else if (ret == -ENOSPC) { |
Josef Bacik | 0036158 | 2013-08-14 14:02:47 -0400 | [diff] [blame] | 313 | ret = 1; |
| 314 | goto out; |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 315 | } |
Josef Bacik | 2adcac1 | 2012-05-23 16:10:14 -0400 | [diff] [blame] | 316 | |
Josef Bacik | bdc20e6 | 2013-02-28 13:23:38 -0500 | [diff] [blame] | 317 | set_bit(BTRFS_INODE_NEEDS_FULL_SYNC, &BTRFS_I(inode)->runtime_flags); |
Yan, Zheng | 0ca1f7c | 2010-05-16 10:48:47 -0400 | [diff] [blame] | 318 | btrfs_delalloc_release_metadata(inode, end + 1 - start); |
Chris Mason | a1ed835 | 2009-09-11 12:27:37 -0400 | [diff] [blame] | 319 | btrfs_drop_extent_cache(inode, start, aligned_end - 1, 0); |
Josef Bacik | 0036158 | 2013-08-14 14:02:47 -0400 | [diff] [blame] | 320 | out: |
Qu Wenruo | 94ed938 | 2015-09-08 17:25:56 +0800 | [diff] [blame] | 321 | /* |
| 322 | * Don't forget to free the reserved space, as for inlined extent |
| 323 | * it won't count as data extent, free them directly here. |
| 324 | * And at reserve time, it's always aligned to page size, so |
| 325 | * just free one page here. |
| 326 | */ |
Kirill A. Shutemov | 09cbfea | 2016-04-01 15:29:47 +0300 | [diff] [blame] | 327 | btrfs_qgroup_free_data(inode, 0, PAGE_SIZE); |
Filipe David Borba Manana | 1acae57 | 2014-01-07 11:42:27 +0000 | [diff] [blame] | 328 | btrfs_free_path(path); |
Jeff Mahoney | 3a45bb2 | 2016-09-09 21:39:03 -0400 | [diff] [blame] | 329 | btrfs_end_transaction(trans); |
Josef Bacik | 0036158 | 2013-08-14 14:02:47 -0400 | [diff] [blame] | 330 | return ret; |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 331 | } |
| 332 | |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 333 | struct async_extent { |
| 334 | u64 start; |
| 335 | u64 ram_size; |
| 336 | u64 compressed_size; |
| 337 | struct page **pages; |
| 338 | unsigned long nr_pages; |
Li Zefan | 261507a0 | 2010-12-17 14:21:50 +0800 | [diff] [blame] | 339 | int compress_type; |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 340 | struct list_head list; |
| 341 | }; |
| 342 | |
| 343 | struct async_cow { |
| 344 | struct inode *inode; |
| 345 | struct btrfs_root *root; |
| 346 | struct page *locked_page; |
| 347 | u64 start; |
| 348 | u64 end; |
| 349 | struct list_head extents; |
| 350 | struct btrfs_work work; |
| 351 | }; |
| 352 | |
| 353 | static noinline int add_async_extent(struct async_cow *cow, |
| 354 | u64 start, u64 ram_size, |
| 355 | u64 compressed_size, |
| 356 | struct page **pages, |
Li Zefan | 261507a0 | 2010-12-17 14:21:50 +0800 | [diff] [blame] | 357 | unsigned long nr_pages, |
| 358 | int compress_type) |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 359 | { |
| 360 | struct async_extent *async_extent; |
| 361 | |
| 362 | async_extent = kmalloc(sizeof(*async_extent), GFP_NOFS); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 363 | BUG_ON(!async_extent); /* -ENOMEM */ |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 364 | async_extent->start = start; |
| 365 | async_extent->ram_size = ram_size; |
| 366 | async_extent->compressed_size = compressed_size; |
| 367 | async_extent->pages = pages; |
| 368 | async_extent->nr_pages = nr_pages; |
Li Zefan | 261507a0 | 2010-12-17 14:21:50 +0800 | [diff] [blame] | 369 | async_extent->compress_type = compress_type; |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 370 | list_add_tail(&async_extent->list, &cow->extents); |
| 371 | return 0; |
| 372 | } |
| 373 | |
Wang Shilong | f79707b | 2014-07-17 11:44:09 +0800 | [diff] [blame] | 374 | static inline int inode_need_compress(struct inode *inode) |
| 375 | { |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 376 | struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb); |
Wang Shilong | f79707b | 2014-07-17 11:44:09 +0800 | [diff] [blame] | 377 | |
| 378 | /* force compress */ |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 379 | if (btrfs_test_opt(fs_info, FORCE_COMPRESS)) |
Wang Shilong | f79707b | 2014-07-17 11:44:09 +0800 | [diff] [blame] | 380 | return 1; |
| 381 | /* bad compression ratios */ |
| 382 | if (BTRFS_I(inode)->flags & BTRFS_INODE_NOCOMPRESS) |
| 383 | return 0; |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 384 | if (btrfs_test_opt(fs_info, COMPRESS) || |
Wang Shilong | f79707b | 2014-07-17 11:44:09 +0800 | [diff] [blame] | 385 | BTRFS_I(inode)->flags & BTRFS_INODE_COMPRESS || |
| 386 | BTRFS_I(inode)->force_compress) |
| 387 | return 1; |
| 388 | return 0; |
| 389 | } |
| 390 | |
Anand Jain | 26d30f8 | 2016-12-19 19:09:06 +0800 | [diff] [blame] | 391 | static inline void inode_should_defrag(struct inode *inode, |
| 392 | u64 start, u64 end, u64 num_bytes, u64 small_write) |
| 393 | { |
| 394 | /* If this is a small write inside eof, kick off a defrag */ |
| 395 | if (num_bytes < small_write && |
| 396 | (start > 0 || end + 1 < BTRFS_I(inode)->disk_i_size)) |
| 397 | btrfs_add_inode_defrag(NULL, inode); |
| 398 | } |
| 399 | |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 400 | /* |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 401 | * we create compressed extents in two phases. The first |
| 402 | * phase compresses a range of pages that have already been |
| 403 | * locked (both pages and state bits are locked). |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 404 | * |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 405 | * This is done inside an ordered work queue, and the compression |
| 406 | * is spread across many cpus. The actual IO submission is step |
| 407 | * two, and the ordered work queue takes care of making sure that |
| 408 | * happens in the same order things were put onto the queue by |
| 409 | * writepages and friends. |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 410 | * |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 411 | * If this code finds it can't get good compression, it puts an |
| 412 | * entry onto the work queue to write the uncompressed bytes. This |
| 413 | * makes sure that both compressed inodes and uncompressed inodes |
Artem Bityutskiy | b257031 | 2012-07-25 18:12:06 +0300 | [diff] [blame] | 414 | * are written in the same order that the flusher thread sent them |
| 415 | * down. |
Chris Mason | d352ac6 | 2008-09-29 15:18:18 -0400 | [diff] [blame] | 416 | */ |
Filipe Manana | c44f649 | 2014-10-09 21:15:44 +0100 | [diff] [blame] | 417 | static noinline void compress_file_range(struct inode *inode, |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 418 | struct page *locked_page, |
| 419 | u64 start, u64 end, |
| 420 | struct async_cow *async_cow, |
| 421 | int *num_added) |
Chris Mason | b888db2 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 422 | { |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 423 | struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb); |
Chris Mason | b888db2 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 424 | struct btrfs_root *root = BTRFS_I(inode)->root; |
Chris Mason | db94535 | 2007-10-15 16:15:53 -0400 | [diff] [blame] | 425 | u64 num_bytes; |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 426 | u64 blocksize = fs_info->sectorsize; |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 427 | u64 actual_end; |
Chris Mason | 42dc7ba | 2008-12-15 11:44:56 -0500 | [diff] [blame] | 428 | u64 isize = i_size_read(inode); |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 429 | int ret = 0; |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 430 | struct page **pages = NULL; |
| 431 | unsigned long nr_pages; |
| 432 | unsigned long nr_pages_ret = 0; |
| 433 | unsigned long total_compressed = 0; |
| 434 | unsigned long total_in = 0; |
Byongho Lee | ee22184 | 2015-12-15 01:42:10 +0900 | [diff] [blame] | 435 | unsigned long max_compressed = SZ_128K; |
| 436 | unsigned long max_uncompressed = SZ_128K; |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 437 | int i; |
| 438 | int will_compress; |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 439 | int compress_type = fs_info->compress_type; |
Chris Mason | 4adaa61 | 2013-03-26 13:07:00 -0400 | [diff] [blame] | 440 | int redirty = 0; |
Chris Mason | b888db2 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 441 | |
Anand Jain | 26d30f8 | 2016-12-19 19:09:06 +0800 | [diff] [blame] | 442 | inode_should_defrag(inode, start, end, end - start + 1, SZ_16K); |
Chris Mason | 4cb5300 | 2011-05-24 15:35:30 -0400 | [diff] [blame] | 443 | |
Chris Mason | 42dc7ba | 2008-12-15 11:44:56 -0500 | [diff] [blame] | 444 | actual_end = min_t(u64, isize, end + 1); |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 445 | again: |
| 446 | will_compress = 0; |
Kirill A. Shutemov | 09cbfea | 2016-04-01 15:29:47 +0300 | [diff] [blame] | 447 | nr_pages = (end >> PAGE_SHIFT) - (start >> PAGE_SHIFT) + 1; |
| 448 | nr_pages = min_t(unsigned long, nr_pages, SZ_128K / PAGE_SIZE); |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 449 | |
Chris Mason | f03d9301f | 2009-02-04 09:31:06 -0500 | [diff] [blame] | 450 | /* |
| 451 | * we don't want to send crud past the end of i_size through |
| 452 | * compression, that's just a waste of CPU time. So, if the |
| 453 | * end of the file is before the start of our current |
| 454 | * requested range of bytes, we bail out to the uncompressed |
| 455 | * cleanup code that can deal with all of this. |
| 456 | * |
| 457 | * It isn't really the fastest way to fix things, but this is a |
| 458 | * very uncommon corner. |
| 459 | */ |
| 460 | if (actual_end <= start) |
| 461 | goto cleanup_and_bail_uncompressed; |
| 462 | |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 463 | total_compressed = actual_end - start; |
| 464 | |
Shilong Wang | 4bcbb33 | 2014-10-07 18:44:35 -0400 | [diff] [blame] | 465 | /* |
| 466 | * skip compression for a small file range(<=blocksize) that |
Nicholas D Steeves | 0132761 | 2016-05-19 21:18:45 -0400 | [diff] [blame] | 467 | * 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] | 468 | */ |
| 469 | if (total_compressed <= blocksize && |
| 470 | (start > 0 || end + 1 < BTRFS_I(inode)->disk_i_size)) |
| 471 | goto cleanup_and_bail_uncompressed; |
| 472 | |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 473 | /* we want to make sure that amount of ram required to uncompress |
| 474 | * an extent is reasonable, so we limit the total size in ram |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 475 | * of a compressed extent to 128k. This is a crucial number |
| 476 | * because it also controls how easily we can spread reads across |
| 477 | * cpus for decompression. |
| 478 | * |
| 479 | * We also want to make sure the amount of IO required to do |
| 480 | * a random read is reasonably small, so we limit the size of |
| 481 | * a compressed extent to 128k. |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 482 | */ |
| 483 | total_compressed = min(total_compressed, max_uncompressed); |
Qu Wenruo | fda2832 | 2013-02-26 08:10:22 +0000 | [diff] [blame] | 484 | num_bytes = ALIGN(end - start + 1, blocksize); |
Chris Mason | be20aa9 | 2007-12-17 20:14:01 -0500 | [diff] [blame] | 485 | num_bytes = max(blocksize, num_bytes); |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 486 | total_in = 0; |
| 487 | ret = 0; |
Chris Mason | db94535 | 2007-10-15 16:15:53 -0400 | [diff] [blame] | 488 | |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 489 | /* |
| 490 | * we do compression for mount -o compress and when the |
| 491 | * inode has not been flagged as nocompress. This flag can |
| 492 | * change at any time if we discover bad compression ratios. |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 493 | */ |
Wang Shilong | f79707b | 2014-07-17 11:44:09 +0800 | [diff] [blame] | 494 | if (inode_need_compress(inode)) { |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 495 | WARN_ON(pages); |
David Sterba | 31e818f | 2015-02-20 18:00:26 +0100 | [diff] [blame] | 496 | pages = kcalloc(nr_pages, sizeof(struct page *), GFP_NOFS); |
Li Zefan | 560f7d7 | 2011-09-08 10:22:01 +0800 | [diff] [blame] | 497 | if (!pages) { |
| 498 | /* just bail out to the uncompressed code */ |
| 499 | goto cont; |
| 500 | } |
Chris Mason | 179e29e | 2007-11-01 11:28:41 -0400 | [diff] [blame] | 501 | |
Li Zefan | 261507a0 | 2010-12-17 14:21:50 +0800 | [diff] [blame] | 502 | if (BTRFS_I(inode)->force_compress) |
| 503 | compress_type = BTRFS_I(inode)->force_compress; |
| 504 | |
Chris Mason | 4adaa61 | 2013-03-26 13:07:00 -0400 | [diff] [blame] | 505 | /* |
| 506 | * we need to call clear_page_dirty_for_io on each |
| 507 | * page in the range. Otherwise applications with the file |
| 508 | * mmap'd can wander in and change the page contents while |
| 509 | * we are compressing them. |
| 510 | * |
| 511 | * If the compression fails for any reason, we set the pages |
| 512 | * dirty again later on. |
| 513 | */ |
| 514 | extent_range_clear_dirty_for_io(inode, start, end); |
| 515 | redirty = 1; |
Li Zefan | 261507a0 | 2010-12-17 14:21:50 +0800 | [diff] [blame] | 516 | ret = btrfs_compress_pages(compress_type, |
| 517 | inode->i_mapping, start, |
| 518 | total_compressed, pages, |
| 519 | nr_pages, &nr_pages_ret, |
| 520 | &total_in, |
| 521 | &total_compressed, |
| 522 | max_compressed); |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 523 | |
| 524 | if (!ret) { |
| 525 | unsigned long offset = total_compressed & |
Kirill A. Shutemov | 09cbfea | 2016-04-01 15:29:47 +0300 | [diff] [blame] | 526 | (PAGE_SIZE - 1); |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 527 | struct page *page = pages[nr_pages_ret - 1]; |
| 528 | char *kaddr; |
| 529 | |
| 530 | /* zero the tail end of the last page, we might be |
| 531 | * sending it down to disk |
| 532 | */ |
| 533 | if (offset) { |
Cong Wang | 7ac687d | 2011-11-25 23:14:28 +0800 | [diff] [blame] | 534 | kaddr = kmap_atomic(page); |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 535 | memset(kaddr + offset, 0, |
Kirill A. Shutemov | 09cbfea | 2016-04-01 15:29:47 +0300 | [diff] [blame] | 536 | PAGE_SIZE - offset); |
Cong Wang | 7ac687d | 2011-11-25 23:14:28 +0800 | [diff] [blame] | 537 | kunmap_atomic(kaddr); |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 538 | } |
| 539 | will_compress = 1; |
| 540 | } |
| 541 | } |
Li Zefan | 560f7d7 | 2011-09-08 10:22:01 +0800 | [diff] [blame] | 542 | cont: |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 543 | if (start == 0) { |
| 544 | /* lets try to make an inline extent */ |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 545 | if (ret || total_in < (actual_end - start)) { |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 546 | /* we didn't compress the entire range, try |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 547 | * to make an uncompressed inline extent. |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 548 | */ |
Josef Bacik | 0036158 | 2013-08-14 14:02:47 -0400 | [diff] [blame] | 549 | ret = cow_file_range_inline(root, inode, start, end, |
Anand Jain | f74670f | 2016-12-06 12:43:07 +0800 | [diff] [blame] | 550 | 0, BTRFS_COMPRESS_NONE, NULL); |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 551 | } else { |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 552 | /* try making a compressed inline extent */ |
Josef Bacik | 0036158 | 2013-08-14 14:02:47 -0400 | [diff] [blame] | 553 | ret = cow_file_range_inline(root, inode, start, end, |
Li Zefan | fe3f566 | 2011-03-28 08:30:38 +0000 | [diff] [blame] | 554 | total_compressed, |
| 555 | compress_type, pages); |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 556 | } |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 557 | if (ret <= 0) { |
Josef Bacik | 151a41b | 2013-07-29 13:22:24 -0400 | [diff] [blame] | 558 | unsigned long clear_flags = EXTENT_DELALLOC | |
| 559 | EXTENT_DEFRAG; |
Filipe Manana | e6eb431 | 2014-10-10 10:45:12 +0100 | [diff] [blame] | 560 | unsigned long page_error_op; |
| 561 | |
Josef Bacik | 151a41b | 2013-07-29 13:22:24 -0400 | [diff] [blame] | 562 | clear_flags |= (ret < 0) ? EXTENT_DO_ACCOUNTING : 0; |
Filipe Manana | e6eb431 | 2014-10-10 10:45:12 +0100 | [diff] [blame] | 563 | page_error_op = ret < 0 ? PAGE_SET_ERROR : 0; |
Josef Bacik | 151a41b | 2013-07-29 13:22:24 -0400 | [diff] [blame] | 564 | |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 565 | /* |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 566 | * inline extent creation worked or returned error, |
| 567 | * we don't need to create any more async work items. |
| 568 | * Unlock and free up our temp pages. |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 569 | */ |
Qu Wenruo | ba8b04c | 2016-07-19 16:50:36 +0800 | [diff] [blame] | 570 | extent_clear_unlock_delalloc(inode, start, end, end, |
| 571 | NULL, clear_flags, |
| 572 | PAGE_UNLOCK | |
Josef Bacik | c2790a2 | 2013-07-29 11:20:47 -0400 | [diff] [blame] | 573 | PAGE_CLEAR_DIRTY | |
| 574 | PAGE_SET_WRITEBACK | |
Filipe Manana | e6eb431 | 2014-10-10 10:45:12 +0100 | [diff] [blame] | 575 | page_error_op | |
Josef Bacik | c2790a2 | 2013-07-29 11:20:47 -0400 | [diff] [blame] | 576 | PAGE_END_WRITEBACK); |
Wang Xiaoguang | 1851309 | 2016-07-25 15:51:40 +0800 | [diff] [blame] | 577 | btrfs_free_reserved_data_space_noquota(inode, start, |
| 578 | end - start + 1); |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 579 | goto free_pages_out; |
| 580 | } |
| 581 | } |
| 582 | |
| 583 | if (will_compress) { |
| 584 | /* |
| 585 | * we aren't doing an inline extent round the compressed size |
| 586 | * up to a block size boundary so the allocator does sane |
| 587 | * things |
| 588 | */ |
Qu Wenruo | fda2832 | 2013-02-26 08:10:22 +0000 | [diff] [blame] | 589 | total_compressed = ALIGN(total_compressed, blocksize); |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 590 | |
| 591 | /* |
| 592 | * one last check to make sure the compression is really a |
| 593 | * win, compare the page count read with the blocks on disk |
| 594 | */ |
Kirill A. Shutemov | 09cbfea | 2016-04-01 15:29:47 +0300 | [diff] [blame] | 595 | total_in = ALIGN(total_in, PAGE_SIZE); |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 596 | if (total_compressed >= total_in) { |
| 597 | will_compress = 0; |
| 598 | } else { |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 599 | num_bytes = total_in; |
Ashish Samant | c8bb0c8 | 2016-03-25 19:01:33 -0700 | [diff] [blame] | 600 | *num_added += 1; |
| 601 | |
| 602 | /* |
| 603 | * The async work queues will take care of doing actual |
| 604 | * allocation on disk for these compressed pages, and |
| 605 | * will submit them to the elevator. |
| 606 | */ |
| 607 | add_async_extent(async_cow, start, num_bytes, |
| 608 | total_compressed, pages, nr_pages_ret, |
| 609 | compress_type); |
| 610 | |
| 611 | if (start + num_bytes < end) { |
| 612 | start += num_bytes; |
| 613 | pages = NULL; |
| 614 | cond_resched(); |
| 615 | goto again; |
| 616 | } |
| 617 | return; |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 618 | } |
| 619 | } |
Ashish Samant | c8bb0c8 | 2016-03-25 19:01:33 -0700 | [diff] [blame] | 620 | if (pages) { |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 621 | /* |
| 622 | * the compression code ran but failed to make things smaller, |
| 623 | * free any pages it allocated and our page pointer array |
| 624 | */ |
| 625 | for (i = 0; i < nr_pages_ret; i++) { |
Chris Mason | 70b99e6 | 2008-10-31 12:46:39 -0400 | [diff] [blame] | 626 | WARN_ON(pages[i]->mapping); |
Kirill A. Shutemov | 09cbfea | 2016-04-01 15:29:47 +0300 | [diff] [blame] | 627 | put_page(pages[i]); |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 628 | } |
| 629 | kfree(pages); |
| 630 | pages = NULL; |
| 631 | total_compressed = 0; |
| 632 | nr_pages_ret = 0; |
| 633 | |
| 634 | /* flag the file so we don't compress in the future */ |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 635 | if (!btrfs_test_opt(fs_info, FORCE_COMPRESS) && |
Chris Mason | 1e701a3 | 2010-03-11 09:42:04 -0500 | [diff] [blame] | 636 | !(BTRFS_I(inode)->force_compress)) { |
Chris Mason | a555f81 | 2010-01-28 16:18:15 -0500 | [diff] [blame] | 637 | BTRFS_I(inode)->flags |= BTRFS_INODE_NOCOMPRESS; |
Chris Mason | 1e701a3 | 2010-03-11 09:42:04 -0500 | [diff] [blame] | 638 | } |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 639 | } |
Chris Mason | f03d9301f | 2009-02-04 09:31:06 -0500 | [diff] [blame] | 640 | cleanup_and_bail_uncompressed: |
Ashish Samant | c8bb0c8 | 2016-03-25 19:01:33 -0700 | [diff] [blame] | 641 | /* |
| 642 | * No compression, but we still need to write the pages in the file |
| 643 | * we've been given so far. redirty the locked page if it corresponds |
| 644 | * to our extent and set things up for the async work queue to run |
| 645 | * cow_file_range to do the normal delalloc dance. |
| 646 | */ |
| 647 | if (page_offset(locked_page) >= start && |
| 648 | page_offset(locked_page) <= end) |
| 649 | __set_page_dirty_nobuffers(locked_page); |
| 650 | /* unlocked later on in the async handlers */ |
| 651 | |
| 652 | if (redirty) |
| 653 | extent_range_redirty_for_io(inode, start, end); |
| 654 | add_async_extent(async_cow, start, end - start + 1, 0, NULL, 0, |
| 655 | BTRFS_COMPRESS_NONE); |
| 656 | *num_added += 1; |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 657 | |
Filipe Manana | c44f649 | 2014-10-09 21:15:44 +0100 | [diff] [blame] | 658 | return; |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 659 | |
| 660 | free_pages_out: |
| 661 | for (i = 0; i < nr_pages_ret; i++) { |
| 662 | WARN_ON(pages[i]->mapping); |
Kirill A. Shutemov | 09cbfea | 2016-04-01 15:29:47 +0300 | [diff] [blame] | 663 | put_page(pages[i]); |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 664 | } |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 665 | kfree(pages); |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 666 | } |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 667 | |
Filipe Manana | 40ae837 | 2014-10-06 22:14:24 +0100 | [diff] [blame] | 668 | static void free_async_extent_pages(struct async_extent *async_extent) |
| 669 | { |
| 670 | int i; |
| 671 | |
| 672 | if (!async_extent->pages) |
| 673 | return; |
| 674 | |
| 675 | for (i = 0; i < async_extent->nr_pages; i++) { |
| 676 | WARN_ON(async_extent->pages[i]->mapping); |
Kirill A. Shutemov | 09cbfea | 2016-04-01 15:29:47 +0300 | [diff] [blame] | 677 | put_page(async_extent->pages[i]); |
Filipe Manana | 40ae837 | 2014-10-06 22:14:24 +0100 | [diff] [blame] | 678 | } |
| 679 | kfree(async_extent->pages); |
| 680 | async_extent->nr_pages = 0; |
| 681 | async_extent->pages = NULL; |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 682 | } |
| 683 | |
| 684 | /* |
| 685 | * phase two of compressed writeback. This is the ordered portion |
| 686 | * of the code, which only gets called in the order the work was |
| 687 | * queued. We walk all the async extents created by compress_file_range |
| 688 | * and send them down to the disk. |
| 689 | */ |
Filipe Manana | dec8f17 | 2014-10-06 22:14:26 +0100 | [diff] [blame] | 690 | static noinline void submit_compressed_extents(struct inode *inode, |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 691 | struct async_cow *async_cow) |
| 692 | { |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 693 | struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb); |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 694 | struct async_extent *async_extent; |
| 695 | u64 alloc_hint = 0; |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 696 | struct btrfs_key ins; |
| 697 | struct extent_map *em; |
| 698 | struct btrfs_root *root = BTRFS_I(inode)->root; |
| 699 | struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree; |
| 700 | struct extent_io_tree *io_tree; |
Josef Bacik | f5a84ee3 | 2009-11-10 21:23:48 -0500 | [diff] [blame] | 701 | int ret = 0; |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 702 | |
Josef Bacik | 3e04e7f | 2013-02-06 16:49:15 -0500 | [diff] [blame] | 703 | again: |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 704 | while (!list_empty(&async_cow->extents)) { |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 705 | async_extent = list_entry(async_cow->extents.next, |
| 706 | struct async_extent, list); |
| 707 | list_del(&async_extent->list); |
| 708 | |
| 709 | io_tree = &BTRFS_I(inode)->io_tree; |
| 710 | |
Josef Bacik | f5a84ee3 | 2009-11-10 21:23:48 -0500 | [diff] [blame] | 711 | retry: |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 712 | /* did the compression code fall back to uncompressed IO? */ |
| 713 | if (!async_extent->pages) { |
| 714 | int page_started = 0; |
| 715 | unsigned long nr_written = 0; |
| 716 | |
| 717 | lock_extent(io_tree, async_extent->start, |
Josef Bacik | 2ac55d4 | 2010-02-03 19:33:23 +0000 | [diff] [blame] | 718 | async_extent->start + |
Jeff Mahoney | d008237 | 2012-03-01 14:57:19 +0100 | [diff] [blame] | 719 | async_extent->ram_size - 1); |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 720 | |
| 721 | /* allocate blocks */ |
Josef Bacik | f5a84ee3 | 2009-11-10 21:23:48 -0500 | [diff] [blame] | 722 | ret = cow_file_range(inode, async_cow->locked_page, |
| 723 | async_extent->start, |
| 724 | async_extent->start + |
| 725 | async_extent->ram_size - 1, |
Wang Xiaoguang | dda3245 | 2016-07-11 11:05:29 +0800 | [diff] [blame] | 726 | async_extent->start + |
| 727 | async_extent->ram_size - 1, |
| 728 | &page_started, &nr_written, 0, |
| 729 | NULL); |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 730 | |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 731 | /* JDM XXX */ |
| 732 | |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 733 | /* |
| 734 | * if page_started, cow_file_range inserted an |
| 735 | * inline extent and took care of all the unlocking |
| 736 | * and IO for us. Otherwise, we need to submit |
| 737 | * all those pages down to the drive. |
| 738 | */ |
Josef Bacik | f5a84ee3 | 2009-11-10 21:23:48 -0500 | [diff] [blame] | 739 | if (!page_started && !ret) |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 740 | extent_write_locked_range(io_tree, |
| 741 | inode, async_extent->start, |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 742 | async_extent->start + |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 743 | async_extent->ram_size - 1, |
| 744 | btrfs_get_extent, |
| 745 | WB_SYNC_ALL); |
Josef Bacik | 3e04e7f | 2013-02-06 16:49:15 -0500 | [diff] [blame] | 746 | else if (ret) |
| 747 | unlock_page(async_cow->locked_page); |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 748 | kfree(async_extent); |
| 749 | cond_resched(); |
| 750 | continue; |
| 751 | } |
| 752 | |
| 753 | lock_extent(io_tree, async_extent->start, |
Jeff Mahoney | d008237 | 2012-03-01 14:57:19 +0100 | [diff] [blame] | 754 | async_extent->start + async_extent->ram_size - 1); |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 755 | |
Wang Xiaoguang | 1851309 | 2016-07-25 15:51:40 +0800 | [diff] [blame] | 756 | ret = btrfs_reserve_extent(root, async_extent->ram_size, |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 757 | async_extent->compressed_size, |
| 758 | async_extent->compressed_size, |
Miao Xie | e570fd2 | 2014-06-19 10:42:50 +0800 | [diff] [blame] | 759 | 0, alloc_hint, &ins, 1, 1); |
Josef Bacik | f5a84ee3 | 2009-11-10 21:23:48 -0500 | [diff] [blame] | 760 | if (ret) { |
Filipe Manana | 40ae837 | 2014-10-06 22:14:24 +0100 | [diff] [blame] | 761 | free_async_extent_pages(async_extent); |
Josef Bacik | 3e04e7f | 2013-02-06 16:49:15 -0500 | [diff] [blame] | 762 | |
Josef Bacik | fdf8e2e | 2013-06-14 16:58:23 -0400 | [diff] [blame] | 763 | if (ret == -ENOSPC) { |
| 764 | unlock_extent(io_tree, async_extent->start, |
| 765 | async_extent->start + |
| 766 | async_extent->ram_size - 1); |
Liu Bo | ce62003 | 2014-07-24 22:48:05 +0800 | [diff] [blame] | 767 | |
| 768 | /* |
| 769 | * we need to redirty the pages if we decide to |
| 770 | * fallback to uncompressed IO, otherwise we |
| 771 | * will not submit these pages down to lower |
| 772 | * layers. |
| 773 | */ |
| 774 | extent_range_redirty_for_io(inode, |
| 775 | async_extent->start, |
| 776 | async_extent->start + |
| 777 | async_extent->ram_size - 1); |
| 778 | |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 779 | goto retry; |
Josef Bacik | fdf8e2e | 2013-06-14 16:58:23 -0400 | [diff] [blame] | 780 | } |
Josef Bacik | 3e04e7f | 2013-02-06 16:49:15 -0500 | [diff] [blame] | 781 | goto out_free; |
Josef Bacik | f5a84ee3 | 2009-11-10 21:23:48 -0500 | [diff] [blame] | 782 | } |
Yan, Zheng | c216775 | 2009-11-12 09:34:21 +0000 | [diff] [blame] | 783 | /* |
| 784 | * here we're doing allocation and writeback of the |
| 785 | * compressed pages |
| 786 | */ |
| 787 | btrfs_drop_extent_cache(inode, async_extent->start, |
| 788 | async_extent->start + |
| 789 | async_extent->ram_size - 1, 0); |
| 790 | |
David Sterba | 172ddd6 | 2011-04-21 00:48:27 +0200 | [diff] [blame] | 791 | em = alloc_extent_map(); |
Liu Bo | b9aa55b | 2013-05-14 02:12:15 +0000 | [diff] [blame] | 792 | if (!em) { |
| 793 | ret = -ENOMEM; |
Josef Bacik | 3e04e7f | 2013-02-06 16:49:15 -0500 | [diff] [blame] | 794 | goto out_free_reserve; |
Liu Bo | b9aa55b | 2013-05-14 02:12:15 +0000 | [diff] [blame] | 795 | } |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 796 | em->start = async_extent->start; |
| 797 | em->len = async_extent->ram_size; |
Chris Mason | 445a694 | 2008-11-10 11:53:33 -0500 | [diff] [blame] | 798 | em->orig_start = em->start; |
Josef Bacik | 2ab28f3 | 2012-10-12 15:27:49 -0400 | [diff] [blame] | 799 | em->mod_start = em->start; |
| 800 | em->mod_len = em->len; |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 801 | |
| 802 | em->block_start = ins.objectid; |
| 803 | em->block_len = ins.offset; |
Josef Bacik | b493968 | 2012-12-03 10:31:19 -0500 | [diff] [blame] | 804 | em->orig_block_len = ins.offset; |
Josef Bacik | cc95bef | 2013-04-04 14:31:27 -0400 | [diff] [blame] | 805 | em->ram_bytes = async_extent->ram_size; |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 806 | em->bdev = fs_info->fs_devices->latest_bdev; |
Li Zefan | 261507a0 | 2010-12-17 14:21:50 +0800 | [diff] [blame] | 807 | em->compress_type = async_extent->compress_type; |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 808 | set_bit(EXTENT_FLAG_PINNED, &em->flags); |
| 809 | set_bit(EXTENT_FLAG_COMPRESSED, &em->flags); |
Josef Bacik | 70c8a91 | 2012-10-11 16:54:30 -0400 | [diff] [blame] | 810 | em->generation = -1; |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 811 | |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 812 | while (1) { |
Chris Mason | 890871b | 2009-09-02 16:24:52 -0400 | [diff] [blame] | 813 | write_lock(&em_tree->lock); |
Josef Bacik | 09a2a8f9 | 2013-04-05 16:51:15 -0400 | [diff] [blame] | 814 | ret = add_extent_mapping(em_tree, em, 1); |
Chris Mason | 890871b | 2009-09-02 16:24:52 -0400 | [diff] [blame] | 815 | write_unlock(&em_tree->lock); |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 816 | if (ret != -EEXIST) { |
| 817 | free_extent_map(em); |
| 818 | break; |
| 819 | } |
| 820 | btrfs_drop_extent_cache(inode, async_extent->start, |
| 821 | async_extent->start + |
| 822 | async_extent->ram_size - 1, 0); |
| 823 | } |
| 824 | |
Josef Bacik | 3e04e7f | 2013-02-06 16:49:15 -0500 | [diff] [blame] | 825 | if (ret) |
| 826 | goto out_free_reserve; |
| 827 | |
Li Zefan | 261507a0 | 2010-12-17 14:21:50 +0800 | [diff] [blame] | 828 | ret = btrfs_add_ordered_extent_compress(inode, |
| 829 | async_extent->start, |
| 830 | ins.objectid, |
| 831 | async_extent->ram_size, |
| 832 | ins.offset, |
| 833 | BTRFS_ORDERED_COMPRESSED, |
| 834 | async_extent->compress_type); |
Filipe Manana | d9f8596 | 2014-08-25 10:43:00 +0100 | [diff] [blame] | 835 | if (ret) { |
| 836 | btrfs_drop_extent_cache(inode, async_extent->start, |
| 837 | async_extent->start + |
| 838 | async_extent->ram_size - 1, 0); |
Josef Bacik | 3e04e7f | 2013-02-06 16:49:15 -0500 | [diff] [blame] | 839 | goto out_free_reserve; |
Filipe Manana | d9f8596 | 2014-08-25 10:43:00 +0100 | [diff] [blame] | 840 | } |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 841 | btrfs_dec_block_group_reservations(fs_info, ins.objectid); |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 842 | |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 843 | /* |
| 844 | * clear dirty, set writeback and unlock the pages. |
| 845 | */ |
Josef Bacik | c2790a2 | 2013-07-29 11:20:47 -0400 | [diff] [blame] | 846 | extent_clear_unlock_delalloc(inode, async_extent->start, |
Chris Mason | a791e35 | 2009-10-08 11:27:10 -0400 | [diff] [blame] | 847 | async_extent->start + |
| 848 | async_extent->ram_size - 1, |
Qu Wenruo | ba8b04c | 2016-07-19 16:50:36 +0800 | [diff] [blame] | 849 | async_extent->start + |
| 850 | async_extent->ram_size - 1, |
Josef Bacik | 151a41b | 2013-07-29 13:22:24 -0400 | [diff] [blame] | 851 | NULL, EXTENT_LOCKED | EXTENT_DELALLOC, |
| 852 | PAGE_UNLOCK | PAGE_CLEAR_DIRTY | |
Josef Bacik | c2790a2 | 2013-07-29 11:20:47 -0400 | [diff] [blame] | 853 | PAGE_SET_WRITEBACK); |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 854 | ret = btrfs_submit_compressed_write(inode, |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 855 | async_extent->start, |
| 856 | async_extent->ram_size, |
| 857 | ins.objectid, |
| 858 | ins.offset, async_extent->pages, |
| 859 | async_extent->nr_pages); |
Filipe Manana | fce2a4e | 2014-10-06 22:14:23 +0100 | [diff] [blame] | 860 | if (ret) { |
| 861 | struct extent_io_tree *tree = &BTRFS_I(inode)->io_tree; |
| 862 | struct page *p = async_extent->pages[0]; |
| 863 | const u64 start = async_extent->start; |
| 864 | const u64 end = start + async_extent->ram_size - 1; |
| 865 | |
| 866 | p->mapping = inode->i_mapping; |
| 867 | tree->ops->writepage_end_io_hook(p, start, end, |
| 868 | NULL, 0); |
| 869 | p->mapping = NULL; |
Qu Wenruo | ba8b04c | 2016-07-19 16:50:36 +0800 | [diff] [blame] | 870 | extent_clear_unlock_delalloc(inode, start, end, end, |
| 871 | NULL, 0, |
Filipe Manana | fce2a4e | 2014-10-06 22:14:23 +0100 | [diff] [blame] | 872 | PAGE_END_WRITEBACK | |
| 873 | PAGE_SET_ERROR); |
Filipe Manana | 40ae837 | 2014-10-06 22:14:24 +0100 | [diff] [blame] | 874 | free_async_extent_pages(async_extent); |
Filipe Manana | fce2a4e | 2014-10-06 22:14:23 +0100 | [diff] [blame] | 875 | } |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 876 | alloc_hint = ins.objectid + ins.offset; |
| 877 | kfree(async_extent); |
| 878 | cond_resched(); |
| 879 | } |
Filipe Manana | dec8f17 | 2014-10-06 22:14:26 +0100 | [diff] [blame] | 880 | return; |
Josef Bacik | 3e04e7f | 2013-02-06 16:49:15 -0500 | [diff] [blame] | 881 | out_free_reserve: |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 882 | btrfs_dec_block_group_reservations(fs_info, ins.objectid); |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 883 | btrfs_free_reserved_extent(fs_info, ins.objectid, ins.offset, 1); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 884 | out_free: |
Josef Bacik | c2790a2 | 2013-07-29 11:20:47 -0400 | [diff] [blame] | 885 | extent_clear_unlock_delalloc(inode, async_extent->start, |
Josef Bacik | 3e04e7f | 2013-02-06 16:49:15 -0500 | [diff] [blame] | 886 | async_extent->start + |
| 887 | async_extent->ram_size - 1, |
Qu Wenruo | ba8b04c | 2016-07-19 16:50:36 +0800 | [diff] [blame] | 888 | async_extent->start + |
| 889 | async_extent->ram_size - 1, |
Josef Bacik | c2790a2 | 2013-07-29 11:20:47 -0400 | [diff] [blame] | 890 | NULL, EXTENT_LOCKED | EXTENT_DELALLOC | |
Josef Bacik | 151a41b | 2013-07-29 13:22:24 -0400 | [diff] [blame] | 891 | EXTENT_DEFRAG | EXTENT_DO_ACCOUNTING, |
| 892 | PAGE_UNLOCK | PAGE_CLEAR_DIRTY | |
Filipe Manana | 704de49 | 2014-10-06 22:14:22 +0100 | [diff] [blame] | 893 | PAGE_SET_WRITEBACK | PAGE_END_WRITEBACK | |
| 894 | PAGE_SET_ERROR); |
Filipe Manana | 40ae837 | 2014-10-06 22:14:24 +0100 | [diff] [blame] | 895 | free_async_extent_pages(async_extent); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 896 | kfree(async_extent); |
Josef Bacik | 3e04e7f | 2013-02-06 16:49:15 -0500 | [diff] [blame] | 897 | goto again; |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 898 | } |
| 899 | |
Josef Bacik | 4b46fce | 2010-05-23 11:00:55 -0400 | [diff] [blame] | 900 | static u64 get_extent_allocation_hint(struct inode *inode, u64 start, |
| 901 | u64 num_bytes) |
| 902 | { |
| 903 | struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree; |
| 904 | struct extent_map *em; |
| 905 | u64 alloc_hint = 0; |
| 906 | |
| 907 | read_lock(&em_tree->lock); |
| 908 | em = search_extent_mapping(em_tree, start, num_bytes); |
| 909 | if (em) { |
| 910 | /* |
| 911 | * if block start isn't an actual block number then find the |
| 912 | * first block in this inode and use that as a hint. If that |
| 913 | * block is also bogus then just don't worry about it. |
| 914 | */ |
| 915 | if (em->block_start >= EXTENT_MAP_LAST_BYTE) { |
| 916 | free_extent_map(em); |
| 917 | em = search_extent_mapping(em_tree, 0, 0); |
| 918 | if (em && em->block_start < EXTENT_MAP_LAST_BYTE) |
| 919 | alloc_hint = em->block_start; |
| 920 | if (em) |
| 921 | free_extent_map(em); |
| 922 | } else { |
| 923 | alloc_hint = em->block_start; |
| 924 | free_extent_map(em); |
| 925 | } |
| 926 | } |
| 927 | read_unlock(&em_tree->lock); |
| 928 | |
| 929 | return alloc_hint; |
| 930 | } |
| 931 | |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 932 | /* |
| 933 | * when extent_io.c finds a delayed allocation range in the file, |
| 934 | * the call backs end up in this code. The basic idea is to |
| 935 | * allocate extents on disk for the range, and create ordered data structs |
| 936 | * in ram to track those extents. |
| 937 | * |
| 938 | * locked_page is the page that writepage had locked already. We use |
| 939 | * it to make sure we don't do extra locks or unlocks. |
| 940 | * |
| 941 | * *page_started is set to one if we unlock locked_page and do everything |
| 942 | * required to start IO on it. It may be clean and already done with |
| 943 | * IO when we return. |
| 944 | */ |
Josef Bacik | 0036158 | 2013-08-14 14:02:47 -0400 | [diff] [blame] | 945 | static noinline int cow_file_range(struct inode *inode, |
| 946 | struct page *locked_page, |
Wang Xiaoguang | dda3245 | 2016-07-11 11:05:29 +0800 | [diff] [blame] | 947 | u64 start, u64 end, u64 delalloc_end, |
| 948 | int *page_started, unsigned long *nr_written, |
| 949 | int unlock, struct btrfs_dedupe_hash *hash) |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 950 | { |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 951 | struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb); |
Josef Bacik | 0036158 | 2013-08-14 14:02:47 -0400 | [diff] [blame] | 952 | struct btrfs_root *root = BTRFS_I(inode)->root; |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 953 | u64 alloc_hint = 0; |
| 954 | u64 num_bytes; |
| 955 | unsigned long ram_size; |
| 956 | u64 disk_num_bytes; |
| 957 | u64 cur_alloc_size; |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 958 | u64 blocksize = fs_info->sectorsize; |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 959 | struct btrfs_key ins; |
| 960 | struct extent_map *em; |
| 961 | struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree; |
| 962 | int ret = 0; |
| 963 | |
Josef Bacik | 02ecd2c | 2013-10-25 16:19:08 -0400 | [diff] [blame] | 964 | if (btrfs_is_free_space_inode(inode)) { |
| 965 | WARN_ON_ONCE(1); |
Josef Bacik | 29bce2f | 2014-02-07 12:21:23 -0500 | [diff] [blame] | 966 | ret = -EINVAL; |
| 967 | goto out_unlock; |
Josef Bacik | 02ecd2c | 2013-10-25 16:19:08 -0400 | [diff] [blame] | 968 | } |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 969 | |
Qu Wenruo | fda2832 | 2013-02-26 08:10:22 +0000 | [diff] [blame] | 970 | num_bytes = ALIGN(end - start + 1, blocksize); |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 971 | num_bytes = max(blocksize, num_bytes); |
| 972 | disk_num_bytes = num_bytes; |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 973 | |
Anand Jain | 26d30f8 | 2016-12-19 19:09:06 +0800 | [diff] [blame] | 974 | inode_should_defrag(inode, start, end, num_bytes, SZ_64K); |
Chris Mason | 4cb5300 | 2011-05-24 15:35:30 -0400 | [diff] [blame] | 975 | |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 976 | if (start == 0) { |
| 977 | /* lets try to make an inline extent */ |
Anand Jain | f74670f | 2016-12-06 12:43:07 +0800 | [diff] [blame] | 978 | ret = cow_file_range_inline(root, inode, start, end, 0, |
| 979 | BTRFS_COMPRESS_NONE, NULL); |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 980 | if (ret == 0) { |
Qu Wenruo | ba8b04c | 2016-07-19 16:50:36 +0800 | [diff] [blame] | 981 | extent_clear_unlock_delalloc(inode, start, end, |
| 982 | delalloc_end, NULL, |
Josef Bacik | c2790a2 | 2013-07-29 11:20:47 -0400 | [diff] [blame] | 983 | EXTENT_LOCKED | EXTENT_DELALLOC | |
Josef Bacik | 151a41b | 2013-07-29 13:22:24 -0400 | [diff] [blame] | 984 | EXTENT_DEFRAG, PAGE_UNLOCK | |
Josef Bacik | c2790a2 | 2013-07-29 11:20:47 -0400 | [diff] [blame] | 985 | PAGE_CLEAR_DIRTY | PAGE_SET_WRITEBACK | |
| 986 | PAGE_END_WRITEBACK); |
Wang Xiaoguang | 1851309 | 2016-07-25 15:51:40 +0800 | [diff] [blame] | 987 | btrfs_free_reserved_data_space_noquota(inode, start, |
| 988 | end - start + 1); |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 989 | *nr_written = *nr_written + |
Kirill A. Shutemov | 09cbfea | 2016-04-01 15:29:47 +0300 | [diff] [blame] | 990 | (end - start + PAGE_SIZE) / PAGE_SIZE; |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 991 | *page_started = 1; |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 992 | goto out; |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 993 | } else if (ret < 0) { |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 994 | goto out_unlock; |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 995 | } |
| 996 | } |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 997 | |
| 998 | BUG_ON(disk_num_bytes > |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 999 | btrfs_super_total_bytes(fs_info->super_copy)); |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 1000 | |
Josef Bacik | 4b46fce | 2010-05-23 11:00:55 -0400 | [diff] [blame] | 1001 | alloc_hint = get_extent_allocation_hint(inode, start, num_bytes); |
Zheng Yan | 5b21f2e | 2008-09-26 10:05:38 -0400 | [diff] [blame] | 1002 | btrfs_drop_extent_cache(inode, start, start + num_bytes - 1, 0); |
Chris Mason | 3b95151 | 2008-04-17 11:29:12 -0400 | [diff] [blame] | 1003 | |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 1004 | while (disk_num_bytes > 0) { |
Chris Mason | a791e35 | 2009-10-08 11:27:10 -0400 | [diff] [blame] | 1005 | unsigned long op; |
| 1006 | |
Josef Bacik | 287a0ab | 2010-03-19 18:07:23 +0000 | [diff] [blame] | 1007 | cur_alloc_size = disk_num_bytes; |
Wang Xiaoguang | 1851309 | 2016-07-25 15:51:40 +0800 | [diff] [blame] | 1008 | ret = btrfs_reserve_extent(root, cur_alloc_size, cur_alloc_size, |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 1009 | fs_info->sectorsize, 0, alloc_hint, |
Miao Xie | e570fd2 | 2014-06-19 10:42:50 +0800 | [diff] [blame] | 1010 | &ins, 1, 1); |
Josef Bacik | 0036158 | 2013-08-14 14:02:47 -0400 | [diff] [blame] | 1011 | if (ret < 0) |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 1012 | goto out_unlock; |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 1013 | |
David Sterba | 172ddd6 | 2011-04-21 00:48:27 +0200 | [diff] [blame] | 1014 | em = alloc_extent_map(); |
Liu Bo | b9aa55b | 2013-05-14 02:12:15 +0000 | [diff] [blame] | 1015 | if (!em) { |
| 1016 | ret = -ENOMEM; |
Liu Bo | ace68ba | 2013-04-22 10:53:47 +0000 | [diff] [blame] | 1017 | goto out_reserve; |
Liu Bo | b9aa55b | 2013-05-14 02:12:15 +0000 | [diff] [blame] | 1018 | } |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 1019 | em->start = start; |
Chris Mason | 445a694 | 2008-11-10 11:53:33 -0500 | [diff] [blame] | 1020 | em->orig_start = em->start; |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 1021 | ram_size = ins.offset; |
| 1022 | em->len = ins.offset; |
Josef Bacik | 2ab28f3 | 2012-10-12 15:27:49 -0400 | [diff] [blame] | 1023 | em->mod_start = em->start; |
| 1024 | em->mod_len = em->len; |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 1025 | |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 1026 | em->block_start = ins.objectid; |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 1027 | em->block_len = ins.offset; |
Josef Bacik | b493968 | 2012-12-03 10:31:19 -0500 | [diff] [blame] | 1028 | em->orig_block_len = ins.offset; |
Josef Bacik | cc95bef | 2013-04-04 14:31:27 -0400 | [diff] [blame] | 1029 | em->ram_bytes = ram_size; |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 1030 | em->bdev = fs_info->fs_devices->latest_bdev; |
Chris Mason | 7f3c74f | 2008-07-18 12:01:11 -0400 | [diff] [blame] | 1031 | set_bit(EXTENT_FLAG_PINNED, &em->flags); |
Josef Bacik | 70c8a91 | 2012-10-11 16:54:30 -0400 | [diff] [blame] | 1032 | em->generation = -1; |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 1033 | |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 1034 | while (1) { |
Chris Mason | 890871b | 2009-09-02 16:24:52 -0400 | [diff] [blame] | 1035 | write_lock(&em_tree->lock); |
Josef Bacik | 09a2a8f9 | 2013-04-05 16:51:15 -0400 | [diff] [blame] | 1036 | ret = add_extent_mapping(em_tree, em, 1); |
Chris Mason | 890871b | 2009-09-02 16:24:52 -0400 | [diff] [blame] | 1037 | write_unlock(&em_tree->lock); |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 1038 | if (ret != -EEXIST) { |
| 1039 | free_extent_map(em); |
| 1040 | break; |
| 1041 | } |
| 1042 | btrfs_drop_extent_cache(inode, start, |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 1043 | start + ram_size - 1, 0); |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 1044 | } |
Liu Bo | ace68ba | 2013-04-22 10:53:47 +0000 | [diff] [blame] | 1045 | if (ret) |
| 1046 | goto out_reserve; |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 1047 | |
Chris Mason | 98d20f6 | 2008-04-14 09:46:10 -0400 | [diff] [blame] | 1048 | cur_alloc_size = ins.offset; |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 1049 | ret = btrfs_add_ordered_extent(inode, start, ins.objectid, |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 1050 | ram_size, cur_alloc_size, 0); |
Liu Bo | ace68ba | 2013-04-22 10:53:47 +0000 | [diff] [blame] | 1051 | if (ret) |
Filipe Manana | d9f8596 | 2014-08-25 10:43:00 +0100 | [diff] [blame] | 1052 | goto out_drop_extent_cache; |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 1053 | |
Yan Zheng | 17d217f | 2008-12-12 10:03:38 -0500 | [diff] [blame] | 1054 | if (root->root_key.objectid == |
| 1055 | BTRFS_DATA_RELOC_TREE_OBJECTID) { |
| 1056 | ret = btrfs_reloc_clone_csums(inode, start, |
| 1057 | cur_alloc_size); |
Josef Bacik | 0036158 | 2013-08-14 14:02:47 -0400 | [diff] [blame] | 1058 | if (ret) |
Filipe Manana | d9f8596 | 2014-08-25 10:43:00 +0100 | [diff] [blame] | 1059 | goto out_drop_extent_cache; |
Yan Zheng | 17d217f | 2008-12-12 10:03:38 -0500 | [diff] [blame] | 1060 | } |
| 1061 | |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 1062 | btrfs_dec_block_group_reservations(fs_info, ins.objectid); |
Filipe Manana | 9cfa3e3 | 2016-04-26 15:39:32 +0100 | [diff] [blame] | 1063 | |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 1064 | if (disk_num_bytes < cur_alloc_size) |
Chris Mason | 3b95151 | 2008-04-17 11:29:12 -0400 | [diff] [blame] | 1065 | break; |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 1066 | |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 1067 | /* we're not doing compressed IO, don't unlock the first |
| 1068 | * page (which the caller expects to stay locked), don't |
| 1069 | * clear any dirty bits and don't set any writeback bits |
Chris Mason | 8b62b72 | 2009-09-02 16:53:46 -0400 | [diff] [blame] | 1070 | * |
| 1071 | * Do set the Private2 bit so we know this page was properly |
| 1072 | * setup for writepage |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 1073 | */ |
Josef Bacik | c2790a2 | 2013-07-29 11:20:47 -0400 | [diff] [blame] | 1074 | op = unlock ? PAGE_UNLOCK : 0; |
| 1075 | op |= PAGE_SET_PRIVATE2; |
Chris Mason | a791e35 | 2009-10-08 11:27:10 -0400 | [diff] [blame] | 1076 | |
Josef Bacik | c2790a2 | 2013-07-29 11:20:47 -0400 | [diff] [blame] | 1077 | extent_clear_unlock_delalloc(inode, start, |
Qu Wenruo | ba8b04c | 2016-07-19 16:50:36 +0800 | [diff] [blame] | 1078 | start + ram_size - 1, |
| 1079 | delalloc_end, locked_page, |
Josef Bacik | c2790a2 | 2013-07-29 11:20:47 -0400 | [diff] [blame] | 1080 | EXTENT_LOCKED | EXTENT_DELALLOC, |
| 1081 | op); |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 1082 | disk_num_bytes -= cur_alloc_size; |
Chris Mason | c59f895 | 2007-12-17 20:14:04 -0500 | [diff] [blame] | 1083 | num_bytes -= cur_alloc_size; |
| 1084 | alloc_hint = ins.objectid + ins.offset; |
| 1085 | start += cur_alloc_size; |
Chris Mason | b888db2 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 1086 | } |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 1087 | out: |
Chris Mason | be20aa9 | 2007-12-17 20:14:01 -0500 | [diff] [blame] | 1088 | return ret; |
Miao Xie | b7d5b0a | 2012-11-01 07:32:18 +0000 | [diff] [blame] | 1089 | |
Filipe Manana | d9f8596 | 2014-08-25 10:43:00 +0100 | [diff] [blame] | 1090 | out_drop_extent_cache: |
| 1091 | btrfs_drop_extent_cache(inode, start, start + ram_size - 1, 0); |
Liu Bo | ace68ba | 2013-04-22 10:53:47 +0000 | [diff] [blame] | 1092 | out_reserve: |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 1093 | btrfs_dec_block_group_reservations(fs_info, ins.objectid); |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 1094 | btrfs_free_reserved_extent(fs_info, ins.objectid, ins.offset, 1); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 1095 | out_unlock: |
Qu Wenruo | ba8b04c | 2016-07-19 16:50:36 +0800 | [diff] [blame] | 1096 | extent_clear_unlock_delalloc(inode, start, end, delalloc_end, |
| 1097 | locked_page, |
Josef Bacik | 151a41b | 2013-07-29 13:22:24 -0400 | [diff] [blame] | 1098 | EXTENT_LOCKED | EXTENT_DO_ACCOUNTING | |
| 1099 | EXTENT_DELALLOC | EXTENT_DEFRAG, |
| 1100 | PAGE_UNLOCK | PAGE_CLEAR_DIRTY | |
| 1101 | PAGE_SET_WRITEBACK | PAGE_END_WRITEBACK); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 1102 | goto out; |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 1103 | } |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 1104 | |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 1105 | /* |
| 1106 | * work queue call back to started compression on a file and pages |
| 1107 | */ |
| 1108 | static noinline void async_cow_start(struct btrfs_work *work) |
| 1109 | { |
| 1110 | struct async_cow *async_cow; |
| 1111 | int num_added = 0; |
| 1112 | async_cow = container_of(work, struct async_cow, work); |
| 1113 | |
| 1114 | compress_file_range(async_cow->inode, async_cow->locked_page, |
| 1115 | async_cow->start, async_cow->end, async_cow, |
| 1116 | &num_added); |
Josef Bacik | 8180ef8 | 2012-06-08 15:16:12 -0400 | [diff] [blame] | 1117 | if (num_added == 0) { |
Josef Bacik | cb77fcd | 2012-06-15 12:19:48 -0600 | [diff] [blame] | 1118 | btrfs_add_delayed_iput(async_cow->inode); |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 1119 | async_cow->inode = NULL; |
Josef Bacik | 8180ef8 | 2012-06-08 15:16:12 -0400 | [diff] [blame] | 1120 | } |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 1121 | } |
| 1122 | |
| 1123 | /* |
| 1124 | * work queue call back to submit previously compressed pages |
| 1125 | */ |
| 1126 | static noinline void async_cow_submit(struct btrfs_work *work) |
| 1127 | { |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 1128 | struct btrfs_fs_info *fs_info; |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 1129 | struct async_cow *async_cow; |
| 1130 | struct btrfs_root *root; |
| 1131 | unsigned long nr_pages; |
| 1132 | |
| 1133 | async_cow = container_of(work, struct async_cow, work); |
| 1134 | |
| 1135 | root = async_cow->root; |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 1136 | fs_info = root->fs_info; |
Kirill A. Shutemov | 09cbfea | 2016-04-01 15:29:47 +0300 | [diff] [blame] | 1137 | nr_pages = (async_cow->end - async_cow->start + PAGE_SIZE) >> |
| 1138 | PAGE_SHIFT; |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 1139 | |
David Sterba | ee86395 | 2015-02-16 19:41:40 +0100 | [diff] [blame] | 1140 | /* |
| 1141 | * atomic_sub_return implies a barrier for waitqueue_active |
| 1142 | */ |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 1143 | if (atomic_sub_return(nr_pages, &fs_info->async_delalloc_pages) < |
Byongho Lee | ee22184 | 2015-12-15 01:42:10 +0900 | [diff] [blame] | 1144 | 5 * SZ_1M && |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 1145 | waitqueue_active(&fs_info->async_submit_wait)) |
| 1146 | wake_up(&fs_info->async_submit_wait); |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 1147 | |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 1148 | if (async_cow->inode) |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 1149 | submit_compressed_extents(async_cow->inode, async_cow); |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 1150 | } |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 1151 | |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 1152 | static noinline void async_cow_free(struct btrfs_work *work) |
| 1153 | { |
| 1154 | struct async_cow *async_cow; |
| 1155 | async_cow = container_of(work, struct async_cow, work); |
Josef Bacik | 8180ef8 | 2012-06-08 15:16:12 -0400 | [diff] [blame] | 1156 | if (async_cow->inode) |
Josef Bacik | cb77fcd | 2012-06-15 12:19:48 -0600 | [diff] [blame] | 1157 | btrfs_add_delayed_iput(async_cow->inode); |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 1158 | kfree(async_cow); |
| 1159 | } |
| 1160 | |
| 1161 | static int cow_file_range_async(struct inode *inode, struct page *locked_page, |
| 1162 | u64 start, u64 end, int *page_started, |
| 1163 | unsigned long *nr_written) |
| 1164 | { |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 1165 | struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb); |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 1166 | struct async_cow *async_cow; |
| 1167 | struct btrfs_root *root = BTRFS_I(inode)->root; |
| 1168 | unsigned long nr_pages; |
| 1169 | u64 cur_end; |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 1170 | |
Chris Mason | a3429ab | 2009-10-08 12:30:20 -0400 | [diff] [blame] | 1171 | clear_extent_bit(&BTRFS_I(inode)->io_tree, start, end, EXTENT_LOCKED, |
| 1172 | 1, 0, NULL, GFP_NOFS); |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 1173 | while (start < end) { |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 1174 | async_cow = kmalloc(sizeof(*async_cow), GFP_NOFS); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 1175 | BUG_ON(!async_cow); /* -ENOMEM */ |
Josef Bacik | 8180ef8 | 2012-06-08 15:16:12 -0400 | [diff] [blame] | 1176 | async_cow->inode = igrab(inode); |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 1177 | async_cow->root = root; |
| 1178 | async_cow->locked_page = locked_page; |
| 1179 | async_cow->start = start; |
| 1180 | |
Wang Shilong | f79707b | 2014-07-17 11:44:09 +0800 | [diff] [blame] | 1181 | if (BTRFS_I(inode)->flags & BTRFS_INODE_NOCOMPRESS && |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 1182 | !btrfs_test_opt(fs_info, FORCE_COMPRESS)) |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 1183 | cur_end = end; |
| 1184 | else |
Byongho Lee | ee22184 | 2015-12-15 01:42:10 +0900 | [diff] [blame] | 1185 | cur_end = min(end, start + SZ_512K - 1); |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 1186 | |
| 1187 | async_cow->end = cur_end; |
| 1188 | INIT_LIST_HEAD(&async_cow->extents); |
| 1189 | |
Liu Bo | 9e0af23 | 2014-08-15 23:36:53 +0800 | [diff] [blame] | 1190 | btrfs_init_work(&async_cow->work, |
| 1191 | btrfs_delalloc_helper, |
| 1192 | async_cow_start, async_cow_submit, |
| 1193 | async_cow_free); |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 1194 | |
Kirill A. Shutemov | 09cbfea | 2016-04-01 15:29:47 +0300 | [diff] [blame] | 1195 | nr_pages = (cur_end - start + PAGE_SIZE) >> |
| 1196 | PAGE_SHIFT; |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 1197 | atomic_add(nr_pages, &fs_info->async_delalloc_pages); |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 1198 | |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 1199 | btrfs_queue_work(fs_info->delalloc_workers, &async_cow->work); |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 1200 | |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 1201 | while (atomic_read(&fs_info->async_submit_draining) && |
| 1202 | atomic_read(&fs_info->async_delalloc_pages)) { |
| 1203 | wait_event(fs_info->async_submit_wait, |
| 1204 | (atomic_read(&fs_info->async_delalloc_pages) == |
| 1205 | 0)); |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 1206 | } |
| 1207 | |
| 1208 | *nr_written += nr_pages; |
| 1209 | start = cur_end + 1; |
| 1210 | } |
| 1211 | *page_started = 1; |
| 1212 | return 0; |
Chris Mason | be20aa9 | 2007-12-17 20:14:01 -0500 | [diff] [blame] | 1213 | } |
| 1214 | |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 1215 | 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] | 1216 | u64 bytenr, u64 num_bytes) |
| 1217 | { |
| 1218 | int ret; |
| 1219 | struct btrfs_ordered_sum *sums; |
| 1220 | LIST_HEAD(list); |
| 1221 | |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 1222 | ret = btrfs_lookup_csums_range(fs_info->csum_root, bytenr, |
Arne Jansen | a2de733 | 2011-03-08 14:14:00 +0100 | [diff] [blame] | 1223 | bytenr + num_bytes - 1, &list, 0); |
Yan Zheng | 17d217f | 2008-12-12 10:03:38 -0500 | [diff] [blame] | 1224 | if (ret == 0 && list_empty(&list)) |
| 1225 | return 0; |
| 1226 | |
| 1227 | while (!list_empty(&list)) { |
| 1228 | sums = list_entry(list.next, struct btrfs_ordered_sum, list); |
| 1229 | list_del(&sums->list); |
| 1230 | kfree(sums); |
| 1231 | } |
| 1232 | return 1; |
| 1233 | } |
| 1234 | |
Chris Mason | d352ac6 | 2008-09-29 15:18:18 -0400 | [diff] [blame] | 1235 | /* |
| 1236 | * when nowcow writeback call back. This checks for snapshots or COW copies |
| 1237 | * of the extents that exist in the file, and COWs the file as required. |
| 1238 | * |
| 1239 | * If no cow copies or snapshots exist, we write directly to the existing |
| 1240 | * blocks on disk |
| 1241 | */ |
Chris Mason | 7f366cf | 2009-03-12 20:12:45 -0400 | [diff] [blame] | 1242 | static noinline int run_delalloc_nocow(struct inode *inode, |
| 1243 | struct page *locked_page, |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 1244 | u64 start, u64 end, int *page_started, int force, |
| 1245 | unsigned long *nr_written) |
Chris Mason | be20aa9 | 2007-12-17 20:14:01 -0500 | [diff] [blame] | 1246 | { |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 1247 | struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb); |
Chris Mason | be20aa9 | 2007-12-17 20:14:01 -0500 | [diff] [blame] | 1248 | struct btrfs_root *root = BTRFS_I(inode)->root; |
Yan Zheng | 7ea394f | 2008-08-05 13:05:02 -0400 | [diff] [blame] | 1249 | struct btrfs_trans_handle *trans; |
Chris Mason | be20aa9 | 2007-12-17 20:14:01 -0500 | [diff] [blame] | 1250 | struct extent_buffer *leaf; |
Chris Mason | be20aa9 | 2007-12-17 20:14:01 -0500 | [diff] [blame] | 1251 | struct btrfs_path *path; |
Yan Zheng | 80ff385 | 2008-10-30 14:20:02 -0400 | [diff] [blame] | 1252 | struct btrfs_file_extent_item *fi; |
Chris Mason | be20aa9 | 2007-12-17 20:14:01 -0500 | [diff] [blame] | 1253 | struct btrfs_key found_key; |
Yan Zheng | 80ff385 | 2008-10-30 14:20:02 -0400 | [diff] [blame] | 1254 | u64 cow_start; |
| 1255 | u64 cur_offset; |
| 1256 | u64 extent_end; |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 1257 | u64 extent_offset; |
Yan Zheng | 80ff385 | 2008-10-30 14:20:02 -0400 | [diff] [blame] | 1258 | u64 disk_bytenr; |
| 1259 | u64 num_bytes; |
Josef Bacik | b493968 | 2012-12-03 10:31:19 -0500 | [diff] [blame] | 1260 | u64 disk_num_bytes; |
Josef Bacik | cc95bef | 2013-04-04 14:31:27 -0400 | [diff] [blame] | 1261 | u64 ram_bytes; |
Yan Zheng | 80ff385 | 2008-10-30 14:20:02 -0400 | [diff] [blame] | 1262 | int extent_type; |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 1263 | int ret, err; |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 1264 | int type; |
Yan Zheng | 80ff385 | 2008-10-30 14:20:02 -0400 | [diff] [blame] | 1265 | int nocow; |
| 1266 | int check_prev = 1; |
Li Zefan | 82d5902 | 2011-04-20 10:33:24 +0800 | [diff] [blame] | 1267 | bool nolock; |
Nikolay Borisov | 4a0cc7c | 2017-01-10 20:35:31 +0200 | [diff] [blame] | 1268 | u64 ino = btrfs_ino(BTRFS_I(inode)); |
Chris Mason | be20aa9 | 2007-12-17 20:14:01 -0500 | [diff] [blame] | 1269 | |
| 1270 | path = btrfs_alloc_path(); |
Josef Bacik | 17ca04a | 2012-05-31 15:58:55 -0400 | [diff] [blame] | 1271 | if (!path) { |
Qu Wenruo | ba8b04c | 2016-07-19 16:50:36 +0800 | [diff] [blame] | 1272 | extent_clear_unlock_delalloc(inode, start, end, end, |
| 1273 | locked_page, |
Josef Bacik | c2790a2 | 2013-07-29 11:20:47 -0400 | [diff] [blame] | 1274 | EXTENT_LOCKED | EXTENT_DELALLOC | |
Josef Bacik | 151a41b | 2013-07-29 13:22:24 -0400 | [diff] [blame] | 1275 | EXTENT_DO_ACCOUNTING | |
| 1276 | EXTENT_DEFRAG, PAGE_UNLOCK | |
Josef Bacik | c2790a2 | 2013-07-29 11:20:47 -0400 | [diff] [blame] | 1277 | PAGE_CLEAR_DIRTY | |
| 1278 | PAGE_SET_WRITEBACK | |
| 1279 | PAGE_END_WRITEBACK); |
Mark Fasheh | d8926bb | 2011-07-13 10:38:47 -0700 | [diff] [blame] | 1280 | return -ENOMEM; |
Josef Bacik | 17ca04a | 2012-05-31 15:58:55 -0400 | [diff] [blame] | 1281 | } |
Li Zefan | 82d5902 | 2011-04-20 10:33:24 +0800 | [diff] [blame] | 1282 | |
Liu Bo | 83eea1f | 2012-07-10 05:28:39 -0600 | [diff] [blame] | 1283 | nolock = btrfs_is_free_space_inode(inode); |
Li Zefan | 82d5902 | 2011-04-20 10:33:24 +0800 | [diff] [blame] | 1284 | |
| 1285 | if (nolock) |
Josef Bacik | 7a7eaa4 | 2011-04-13 12:54:33 -0400 | [diff] [blame] | 1286 | trans = btrfs_join_transaction_nolock(root); |
Li Zefan | 82d5902 | 2011-04-20 10:33:24 +0800 | [diff] [blame] | 1287 | else |
Josef Bacik | 7a7eaa4 | 2011-04-13 12:54:33 -0400 | [diff] [blame] | 1288 | trans = btrfs_join_transaction(root); |
Chris Mason | ff5714c | 2011-05-28 07:00:39 -0400 | [diff] [blame] | 1289 | |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 1290 | if (IS_ERR(trans)) { |
Qu Wenruo | ba8b04c | 2016-07-19 16:50:36 +0800 | [diff] [blame] | 1291 | extent_clear_unlock_delalloc(inode, start, end, end, |
| 1292 | locked_page, |
Josef Bacik | c2790a2 | 2013-07-29 11:20:47 -0400 | [diff] [blame] | 1293 | EXTENT_LOCKED | EXTENT_DELALLOC | |
Josef Bacik | 151a41b | 2013-07-29 13:22:24 -0400 | [diff] [blame] | 1294 | EXTENT_DO_ACCOUNTING | |
| 1295 | EXTENT_DEFRAG, PAGE_UNLOCK | |
Josef Bacik | c2790a2 | 2013-07-29 11:20:47 -0400 | [diff] [blame] | 1296 | PAGE_CLEAR_DIRTY | |
| 1297 | PAGE_SET_WRITEBACK | |
| 1298 | PAGE_END_WRITEBACK); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 1299 | btrfs_free_path(path); |
| 1300 | return PTR_ERR(trans); |
| 1301 | } |
| 1302 | |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 1303 | trans->block_rsv = &fs_info->delalloc_block_rsv; |
Chris Mason | be20aa9 | 2007-12-17 20:14:01 -0500 | [diff] [blame] | 1304 | |
Yan Zheng | 80ff385 | 2008-10-30 14:20:02 -0400 | [diff] [blame] | 1305 | cow_start = (u64)-1; |
| 1306 | cur_offset = start; |
| 1307 | while (1) { |
Li Zefan | 33345d01 | 2011-04-20 10:31:50 +0800 | [diff] [blame] | 1308 | ret = btrfs_lookup_file_extent(trans, root, path, ino, |
Yan Zheng | 80ff385 | 2008-10-30 14:20:02 -0400 | [diff] [blame] | 1309 | cur_offset, 0); |
Josef Bacik | d788a34 | 2013-10-25 16:55:08 -0400 | [diff] [blame] | 1310 | if (ret < 0) |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 1311 | goto error; |
Yan Zheng | 80ff385 | 2008-10-30 14:20:02 -0400 | [diff] [blame] | 1312 | if (ret > 0 && path->slots[0] > 0 && check_prev) { |
| 1313 | leaf = path->nodes[0]; |
| 1314 | btrfs_item_key_to_cpu(leaf, &found_key, |
| 1315 | path->slots[0] - 1); |
Li Zefan | 33345d01 | 2011-04-20 10:31:50 +0800 | [diff] [blame] | 1316 | if (found_key.objectid == ino && |
Yan Zheng | 80ff385 | 2008-10-30 14:20:02 -0400 | [diff] [blame] | 1317 | found_key.type == BTRFS_EXTENT_DATA_KEY) |
| 1318 | path->slots[0]--; |
| 1319 | } |
| 1320 | check_prev = 0; |
| 1321 | next_slot: |
| 1322 | leaf = path->nodes[0]; |
| 1323 | if (path->slots[0] >= btrfs_header_nritems(leaf)) { |
| 1324 | ret = btrfs_next_leaf(root, path); |
Josef Bacik | d788a34 | 2013-10-25 16:55:08 -0400 | [diff] [blame] | 1325 | if (ret < 0) |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 1326 | goto error; |
Yan Zheng | 80ff385 | 2008-10-30 14:20:02 -0400 | [diff] [blame] | 1327 | if (ret > 0) |
| 1328 | break; |
| 1329 | leaf = path->nodes[0]; |
| 1330 | } |
Chris Mason | be20aa9 | 2007-12-17 20:14:01 -0500 | [diff] [blame] | 1331 | |
Yan Zheng | 80ff385 | 2008-10-30 14:20:02 -0400 | [diff] [blame] | 1332 | nocow = 0; |
| 1333 | disk_bytenr = 0; |
Yan Zheng | 17d217f | 2008-12-12 10:03:38 -0500 | [diff] [blame] | 1334 | num_bytes = 0; |
Yan Zheng | 80ff385 | 2008-10-30 14:20:02 -0400 | [diff] [blame] | 1335 | btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]); |
Chris Mason | be20aa9 | 2007-12-17 20:14:01 -0500 | [diff] [blame] | 1336 | |
Filipe Manana | 1d512cb | 2015-11-09 00:33:58 +0000 | [diff] [blame] | 1337 | if (found_key.objectid > ino) |
| 1338 | break; |
| 1339 | if (WARN_ON_ONCE(found_key.objectid < ino) || |
| 1340 | found_key.type < BTRFS_EXTENT_DATA_KEY) { |
| 1341 | path->slots[0]++; |
| 1342 | goto next_slot; |
| 1343 | } |
| 1344 | if (found_key.type > BTRFS_EXTENT_DATA_KEY || |
Yan Zheng | 80ff385 | 2008-10-30 14:20:02 -0400 | [diff] [blame] | 1345 | found_key.offset > end) |
| 1346 | break; |
Chris Mason | be20aa9 | 2007-12-17 20:14:01 -0500 | [diff] [blame] | 1347 | |
Yan Zheng | 80ff385 | 2008-10-30 14:20:02 -0400 | [diff] [blame] | 1348 | if (found_key.offset > cur_offset) { |
| 1349 | extent_end = found_key.offset; |
Chris Mason | e9061e2 | 2009-10-09 09:57:45 -0400 | [diff] [blame] | 1350 | extent_type = 0; |
Yan Zheng | 80ff385 | 2008-10-30 14:20:02 -0400 | [diff] [blame] | 1351 | goto out_check; |
| 1352 | } |
Chris Mason | c31f883 | 2008-01-08 15:46:31 -0500 | [diff] [blame] | 1353 | |
Yan Zheng | 80ff385 | 2008-10-30 14:20:02 -0400 | [diff] [blame] | 1354 | fi = btrfs_item_ptr(leaf, path->slots[0], |
| 1355 | struct btrfs_file_extent_item); |
| 1356 | extent_type = btrfs_file_extent_type(leaf, fi); |
Chris Mason | be20aa9 | 2007-12-17 20:14:01 -0500 | [diff] [blame] | 1357 | |
Josef Bacik | cc95bef | 2013-04-04 14:31:27 -0400 | [diff] [blame] | 1358 | ram_bytes = btrfs_file_extent_ram_bytes(leaf, fi); |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 1359 | if (extent_type == BTRFS_FILE_EXTENT_REG || |
| 1360 | extent_type == BTRFS_FILE_EXTENT_PREALLOC) { |
Yan Zheng | 80ff385 | 2008-10-30 14:20:02 -0400 | [diff] [blame] | 1361 | disk_bytenr = btrfs_file_extent_disk_bytenr(leaf, fi); |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 1362 | extent_offset = btrfs_file_extent_offset(leaf, fi); |
Yan Zheng | 80ff385 | 2008-10-30 14:20:02 -0400 | [diff] [blame] | 1363 | extent_end = found_key.offset + |
| 1364 | btrfs_file_extent_num_bytes(leaf, fi); |
Josef Bacik | b493968 | 2012-12-03 10:31:19 -0500 | [diff] [blame] | 1365 | disk_num_bytes = |
| 1366 | btrfs_file_extent_disk_num_bytes(leaf, fi); |
Yan Zheng | 80ff385 | 2008-10-30 14:20:02 -0400 | [diff] [blame] | 1367 | if (extent_end <= start) { |
| 1368 | path->slots[0]++; |
| 1369 | goto next_slot; |
| 1370 | } |
Yan Zheng | 17d217f | 2008-12-12 10:03:38 -0500 | [diff] [blame] | 1371 | if (disk_bytenr == 0) |
| 1372 | goto out_check; |
Yan Zheng | 80ff385 | 2008-10-30 14:20:02 -0400 | [diff] [blame] | 1373 | if (btrfs_file_extent_compression(leaf, fi) || |
| 1374 | btrfs_file_extent_encryption(leaf, fi) || |
| 1375 | btrfs_file_extent_other_encoding(leaf, fi)) |
| 1376 | goto out_check; |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 1377 | if (extent_type == BTRFS_FILE_EXTENT_REG && !force) |
| 1378 | goto out_check; |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 1379 | if (btrfs_extent_readonly(fs_info, disk_bytenr)) |
Yan Zheng | 80ff385 | 2008-10-30 14:20:02 -0400 | [diff] [blame] | 1380 | goto out_check; |
Li Zefan | 33345d01 | 2011-04-20 10:31:50 +0800 | [diff] [blame] | 1381 | if (btrfs_cross_ref_exist(trans, root, ino, |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 1382 | found_key.offset - |
| 1383 | extent_offset, disk_bytenr)) |
Yan Zheng | 17d217f | 2008-12-12 10:03:38 -0500 | [diff] [blame] | 1384 | goto out_check; |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 1385 | disk_bytenr += extent_offset; |
Yan Zheng | 17d217f | 2008-12-12 10:03:38 -0500 | [diff] [blame] | 1386 | disk_bytenr += cur_offset - found_key.offset; |
| 1387 | num_bytes = min(end + 1, extent_end) - cur_offset; |
| 1388 | /* |
Wang Shilong | e9894fd | 2014-03-27 11:12:25 +0800 | [diff] [blame] | 1389 | * if there are pending snapshots for this root, |
| 1390 | * we fall into common COW way. |
| 1391 | */ |
| 1392 | if (!nolock) { |
Filipe Manana | 9ea24bb | 2014-10-29 11:57:59 +0000 | [diff] [blame] | 1393 | err = btrfs_start_write_no_snapshoting(root); |
Wang Shilong | e9894fd | 2014-03-27 11:12:25 +0800 | [diff] [blame] | 1394 | if (!err) |
| 1395 | goto out_check; |
| 1396 | } |
| 1397 | /* |
Yan Zheng | 17d217f | 2008-12-12 10:03:38 -0500 | [diff] [blame] | 1398 | * force cow if csum exists in the range. |
| 1399 | * this ensure that csum for a given extent are |
| 1400 | * either valid or do not exist. |
| 1401 | */ |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 1402 | if (csum_exist_in_range(fs_info, disk_bytenr, |
| 1403 | num_bytes)) |
Yan Zheng | 17d217f | 2008-12-12 10:03:38 -0500 | [diff] [blame] | 1404 | goto out_check; |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 1405 | if (!btrfs_inc_nocow_writers(fs_info, disk_bytenr)) |
Filipe Manana | f78c436 | 2016-05-09 13:15:41 +0100 | [diff] [blame] | 1406 | goto out_check; |
Yan Zheng | 80ff385 | 2008-10-30 14:20:02 -0400 | [diff] [blame] | 1407 | nocow = 1; |
| 1408 | } else if (extent_type == BTRFS_FILE_EXTENT_INLINE) { |
| 1409 | extent_end = found_key.offset + |
Chris Mason | 514ac8a | 2014-01-03 21:07:00 -0800 | [diff] [blame] | 1410 | btrfs_file_extent_inline_len(leaf, |
| 1411 | path->slots[0], fi); |
Jeff Mahoney | da17066 | 2016-06-15 09:22:56 -0400 | [diff] [blame] | 1412 | extent_end = ALIGN(extent_end, |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 1413 | fs_info->sectorsize); |
Yan Zheng | 80ff385 | 2008-10-30 14:20:02 -0400 | [diff] [blame] | 1414 | } else { |
| 1415 | BUG_ON(1); |
| 1416 | } |
| 1417 | out_check: |
| 1418 | if (extent_end <= start) { |
| 1419 | path->slots[0]++; |
Wang Shilong | e9894fd | 2014-03-27 11:12:25 +0800 | [diff] [blame] | 1420 | if (!nolock && nocow) |
Filipe Manana | 9ea24bb | 2014-10-29 11:57:59 +0000 | [diff] [blame] | 1421 | btrfs_end_write_no_snapshoting(root); |
Filipe Manana | f78c436 | 2016-05-09 13:15:41 +0100 | [diff] [blame] | 1422 | if (nocow) |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 1423 | btrfs_dec_nocow_writers(fs_info, disk_bytenr); |
Yan Zheng | 80ff385 | 2008-10-30 14:20:02 -0400 | [diff] [blame] | 1424 | goto next_slot; |
| 1425 | } |
| 1426 | if (!nocow) { |
| 1427 | if (cow_start == (u64)-1) |
| 1428 | cow_start = cur_offset; |
| 1429 | cur_offset = extent_end; |
| 1430 | if (cur_offset > end) |
| 1431 | break; |
| 1432 | path->slots[0]++; |
| 1433 | goto next_slot; |
Yan Zheng | 7ea394f | 2008-08-05 13:05:02 -0400 | [diff] [blame] | 1434 | } |
Chris Mason | be20aa9 | 2007-12-17 20:14:01 -0500 | [diff] [blame] | 1435 | |
David Sterba | b3b4aa7 | 2011-04-21 01:20:15 +0200 | [diff] [blame] | 1436 | btrfs_release_path(path); |
Yan Zheng | 80ff385 | 2008-10-30 14:20:02 -0400 | [diff] [blame] | 1437 | if (cow_start != (u64)-1) { |
Josef Bacik | 0036158 | 2013-08-14 14:02:47 -0400 | [diff] [blame] | 1438 | ret = cow_file_range(inode, locked_page, |
| 1439 | cow_start, found_key.offset - 1, |
Wang Xiaoguang | dda3245 | 2016-07-11 11:05:29 +0800 | [diff] [blame] | 1440 | end, page_started, nr_written, 1, |
| 1441 | NULL); |
Wang Shilong | e9894fd | 2014-03-27 11:12:25 +0800 | [diff] [blame] | 1442 | if (ret) { |
| 1443 | if (!nolock && nocow) |
Filipe Manana | 9ea24bb | 2014-10-29 11:57:59 +0000 | [diff] [blame] | 1444 | btrfs_end_write_no_snapshoting(root); |
Filipe Manana | f78c436 | 2016-05-09 13:15:41 +0100 | [diff] [blame] | 1445 | if (nocow) |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 1446 | btrfs_dec_nocow_writers(fs_info, |
Filipe Manana | f78c436 | 2016-05-09 13:15:41 +0100 | [diff] [blame] | 1447 | disk_bytenr); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 1448 | goto error; |
Wang Shilong | e9894fd | 2014-03-27 11:12:25 +0800 | [diff] [blame] | 1449 | } |
Yan Zheng | 80ff385 | 2008-10-30 14:20:02 -0400 | [diff] [blame] | 1450 | cow_start = (u64)-1; |
Yan Zheng | 7ea394f | 2008-08-05 13:05:02 -0400 | [diff] [blame] | 1451 | } |
Yan Zheng | 80ff385 | 2008-10-30 14:20:02 -0400 | [diff] [blame] | 1452 | |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 1453 | if (extent_type == BTRFS_FILE_EXTENT_PREALLOC) { |
| 1454 | struct extent_map *em; |
| 1455 | struct extent_map_tree *em_tree; |
| 1456 | em_tree = &BTRFS_I(inode)->extent_tree; |
David Sterba | 172ddd6 | 2011-04-21 00:48:27 +0200 | [diff] [blame] | 1457 | em = alloc_extent_map(); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 1458 | BUG_ON(!em); /* -ENOMEM */ |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 1459 | em->start = cur_offset; |
Josef Bacik | 70c8a91 | 2012-10-11 16:54:30 -0400 | [diff] [blame] | 1460 | em->orig_start = found_key.offset - extent_offset; |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 1461 | em->len = num_bytes; |
| 1462 | em->block_len = num_bytes; |
| 1463 | em->block_start = disk_bytenr; |
Josef Bacik | b493968 | 2012-12-03 10:31:19 -0500 | [diff] [blame] | 1464 | em->orig_block_len = disk_num_bytes; |
Josef Bacik | cc95bef | 2013-04-04 14:31:27 -0400 | [diff] [blame] | 1465 | em->ram_bytes = ram_bytes; |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 1466 | em->bdev = fs_info->fs_devices->latest_bdev; |
Josef Bacik | 2ab28f3 | 2012-10-12 15:27:49 -0400 | [diff] [blame] | 1467 | em->mod_start = em->start; |
| 1468 | em->mod_len = em->len; |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 1469 | set_bit(EXTENT_FLAG_PINNED, &em->flags); |
Josef Bacik | b11e234 | 2012-12-03 10:58:15 -0500 | [diff] [blame] | 1470 | set_bit(EXTENT_FLAG_FILLING, &em->flags); |
Josef Bacik | 70c8a91 | 2012-10-11 16:54:30 -0400 | [diff] [blame] | 1471 | em->generation = -1; |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 1472 | while (1) { |
Chris Mason | 890871b | 2009-09-02 16:24:52 -0400 | [diff] [blame] | 1473 | write_lock(&em_tree->lock); |
Josef Bacik | 09a2a8f9 | 2013-04-05 16:51:15 -0400 | [diff] [blame] | 1474 | ret = add_extent_mapping(em_tree, em, 1); |
Chris Mason | 890871b | 2009-09-02 16:24:52 -0400 | [diff] [blame] | 1475 | write_unlock(&em_tree->lock); |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 1476 | if (ret != -EEXIST) { |
| 1477 | free_extent_map(em); |
| 1478 | break; |
| 1479 | } |
| 1480 | btrfs_drop_extent_cache(inode, em->start, |
| 1481 | em->start + em->len - 1, 0); |
| 1482 | } |
| 1483 | type = BTRFS_ORDERED_PREALLOC; |
| 1484 | } else { |
| 1485 | type = BTRFS_ORDERED_NOCOW; |
| 1486 | } |
Yan Zheng | 80ff385 | 2008-10-30 14:20:02 -0400 | [diff] [blame] | 1487 | |
| 1488 | ret = btrfs_add_ordered_extent(inode, cur_offset, disk_bytenr, |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 1489 | num_bytes, num_bytes, type); |
Filipe Manana | f78c436 | 2016-05-09 13:15:41 +0100 | [diff] [blame] | 1490 | if (nocow) |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 1491 | btrfs_dec_nocow_writers(fs_info, disk_bytenr); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 1492 | BUG_ON(ret); /* -ENOMEM */ |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 1493 | |
Yan, Zheng | efa5646 | 2010-05-16 10:49:59 -0400 | [diff] [blame] | 1494 | if (root->root_key.objectid == |
| 1495 | BTRFS_DATA_RELOC_TREE_OBJECTID) { |
| 1496 | ret = btrfs_reloc_clone_csums(inode, cur_offset, |
| 1497 | num_bytes); |
Wang Shilong | e9894fd | 2014-03-27 11:12:25 +0800 | [diff] [blame] | 1498 | if (ret) { |
| 1499 | if (!nolock && nocow) |
Filipe Manana | 9ea24bb | 2014-10-29 11:57:59 +0000 | [diff] [blame] | 1500 | btrfs_end_write_no_snapshoting(root); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 1501 | goto error; |
Wang Shilong | e9894fd | 2014-03-27 11:12:25 +0800 | [diff] [blame] | 1502 | } |
Yan, Zheng | efa5646 | 2010-05-16 10:49:59 -0400 | [diff] [blame] | 1503 | } |
| 1504 | |
Josef Bacik | c2790a2 | 2013-07-29 11:20:47 -0400 | [diff] [blame] | 1505 | extent_clear_unlock_delalloc(inode, cur_offset, |
Qu Wenruo | ba8b04c | 2016-07-19 16:50:36 +0800 | [diff] [blame] | 1506 | cur_offset + num_bytes - 1, end, |
Josef Bacik | c2790a2 | 2013-07-29 11:20:47 -0400 | [diff] [blame] | 1507 | locked_page, EXTENT_LOCKED | |
Wang Xiaoguang | 1851309 | 2016-07-25 15:51:40 +0800 | [diff] [blame] | 1508 | EXTENT_DELALLOC | |
| 1509 | EXTENT_CLEAR_DATA_RESV, |
| 1510 | PAGE_UNLOCK | PAGE_SET_PRIVATE2); |
| 1511 | |
Wang Shilong | e9894fd | 2014-03-27 11:12:25 +0800 | [diff] [blame] | 1512 | if (!nolock && nocow) |
Filipe Manana | 9ea24bb | 2014-10-29 11:57:59 +0000 | [diff] [blame] | 1513 | btrfs_end_write_no_snapshoting(root); |
Yan Zheng | 80ff385 | 2008-10-30 14:20:02 -0400 | [diff] [blame] | 1514 | cur_offset = extent_end; |
| 1515 | if (cur_offset > end) |
| 1516 | break; |
Yan Zheng | 7ea394f | 2008-08-05 13:05:02 -0400 | [diff] [blame] | 1517 | } |
David Sterba | b3b4aa7 | 2011-04-21 01:20:15 +0200 | [diff] [blame] | 1518 | btrfs_release_path(path); |
Yan Zheng | 80ff385 | 2008-10-30 14:20:02 -0400 | [diff] [blame] | 1519 | |
Josef Bacik | 17ca04a | 2012-05-31 15:58:55 -0400 | [diff] [blame] | 1520 | if (cur_offset <= end && cow_start == (u64)-1) { |
Yan Zheng | 80ff385 | 2008-10-30 14:20:02 -0400 | [diff] [blame] | 1521 | cow_start = cur_offset; |
Josef Bacik | 17ca04a | 2012-05-31 15:58:55 -0400 | [diff] [blame] | 1522 | cur_offset = end; |
| 1523 | } |
| 1524 | |
Yan Zheng | 80ff385 | 2008-10-30 14:20:02 -0400 | [diff] [blame] | 1525 | if (cow_start != (u64)-1) { |
Wang Xiaoguang | dda3245 | 2016-07-11 11:05:29 +0800 | [diff] [blame] | 1526 | ret = cow_file_range(inode, locked_page, cow_start, end, end, |
| 1527 | page_started, nr_written, 1, NULL); |
Josef Bacik | d788a34 | 2013-10-25 16:55:08 -0400 | [diff] [blame] | 1528 | if (ret) |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 1529 | goto error; |
Yan Zheng | 80ff385 | 2008-10-30 14:20:02 -0400 | [diff] [blame] | 1530 | } |
| 1531 | |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 1532 | error: |
Jeff Mahoney | 3a45bb2 | 2016-09-09 21:39:03 -0400 | [diff] [blame] | 1533 | err = btrfs_end_transaction(trans); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 1534 | if (!ret) |
| 1535 | ret = err; |
| 1536 | |
Josef Bacik | 17ca04a | 2012-05-31 15:58:55 -0400 | [diff] [blame] | 1537 | if (ret && cur_offset < end) |
Qu Wenruo | ba8b04c | 2016-07-19 16:50:36 +0800 | [diff] [blame] | 1538 | extent_clear_unlock_delalloc(inode, cur_offset, end, end, |
Josef Bacik | c2790a2 | 2013-07-29 11:20:47 -0400 | [diff] [blame] | 1539 | locked_page, EXTENT_LOCKED | |
Josef Bacik | 151a41b | 2013-07-29 13:22:24 -0400 | [diff] [blame] | 1540 | EXTENT_DELALLOC | EXTENT_DEFRAG | |
| 1541 | EXTENT_DO_ACCOUNTING, PAGE_UNLOCK | |
| 1542 | PAGE_CLEAR_DIRTY | |
Josef Bacik | c2790a2 | 2013-07-29 11:20:47 -0400 | [diff] [blame] | 1543 | PAGE_SET_WRITEBACK | |
| 1544 | PAGE_END_WRITEBACK); |
Yan Zheng | 7ea394f | 2008-08-05 13:05:02 -0400 | [diff] [blame] | 1545 | btrfs_free_path(path); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 1546 | return ret; |
Chris Mason | be20aa9 | 2007-12-17 20:14:01 -0500 | [diff] [blame] | 1547 | } |
| 1548 | |
Wang Shilong | 47059d9 | 2014-07-03 18:22:07 +0800 | [diff] [blame] | 1549 | static inline int need_force_cow(struct inode *inode, u64 start, u64 end) |
| 1550 | { |
| 1551 | |
| 1552 | if (!(BTRFS_I(inode)->flags & BTRFS_INODE_NODATACOW) && |
| 1553 | !(BTRFS_I(inode)->flags & BTRFS_INODE_PREALLOC)) |
| 1554 | return 0; |
| 1555 | |
| 1556 | /* |
| 1557 | * @defrag_bytes is a hint value, no spinlock held here, |
| 1558 | * if is not zero, it means the file is defragging. |
| 1559 | * Force cow if given extent needs to be defragged. |
| 1560 | */ |
| 1561 | if (BTRFS_I(inode)->defrag_bytes && |
| 1562 | test_range_bit(&BTRFS_I(inode)->io_tree, start, end, |
| 1563 | EXTENT_DEFRAG, 0, NULL)) |
| 1564 | return 1; |
| 1565 | |
| 1566 | return 0; |
| 1567 | } |
| 1568 | |
Chris Mason | d352ac6 | 2008-09-29 15:18:18 -0400 | [diff] [blame] | 1569 | /* |
| 1570 | * extent_io.c call back to do delayed allocation processing |
| 1571 | */ |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 1572 | static int run_delalloc_range(struct inode *inode, struct page *locked_page, |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 1573 | u64 start, u64 end, int *page_started, |
| 1574 | unsigned long *nr_written) |
Chris Mason | be20aa9 | 2007-12-17 20:14:01 -0500 | [diff] [blame] | 1575 | { |
Chris Mason | be20aa9 | 2007-12-17 20:14:01 -0500 | [diff] [blame] | 1576 | int ret; |
Wang Shilong | 47059d9 | 2014-07-03 18:22:07 +0800 | [diff] [blame] | 1577 | int force_cow = need_force_cow(inode, start, end); |
Chris Mason | a213501 | 2008-06-25 16:01:30 -0400 | [diff] [blame] | 1578 | |
Wang Shilong | 47059d9 | 2014-07-03 18:22:07 +0800 | [diff] [blame] | 1579 | if (BTRFS_I(inode)->flags & BTRFS_INODE_NODATACOW && !force_cow) { |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 1580 | ret = run_delalloc_nocow(inode, locked_page, start, end, |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 1581 | page_started, 1, nr_written); |
Wang Shilong | 47059d9 | 2014-07-03 18:22:07 +0800 | [diff] [blame] | 1582 | } else if (BTRFS_I(inode)->flags & BTRFS_INODE_PREALLOC && !force_cow) { |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 1583 | ret = run_delalloc_nocow(inode, locked_page, start, end, |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 1584 | page_started, 0, nr_written); |
Wang Shilong | 7816030 | 2014-07-17 11:44:10 +0800 | [diff] [blame] | 1585 | } else if (!inode_need_compress(inode)) { |
Wang Xiaoguang | dda3245 | 2016-07-11 11:05:29 +0800 | [diff] [blame] | 1586 | ret = cow_file_range(inode, locked_page, start, end, end, |
| 1587 | page_started, nr_written, 1, NULL); |
Josef Bacik | 7ddf5a4 | 2012-06-08 15:26:47 -0400 | [diff] [blame] | 1588 | } else { |
| 1589 | set_bit(BTRFS_INODE_HAS_ASYNC_EXTENT, |
| 1590 | &BTRFS_I(inode)->runtime_flags); |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 1591 | ret = cow_file_range_async(inode, locked_page, start, end, |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 1592 | page_started, nr_written); |
Josef Bacik | 7ddf5a4 | 2012-06-08 15:26:47 -0400 | [diff] [blame] | 1593 | } |
Chris Mason | b888db2 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 1594 | return ret; |
| 1595 | } |
| 1596 | |
Jeff Mahoney | 1bf8504 | 2011-07-21 16:56:09 +0000 | [diff] [blame] | 1597 | static void btrfs_split_extent_hook(struct inode *inode, |
| 1598 | struct extent_state *orig, u64 split) |
Josef Bacik | 9ed74f2 | 2009-09-11 16:12:44 -0400 | [diff] [blame] | 1599 | { |
Josef Bacik | dcab6a3 | 2015-02-11 15:08:59 -0500 | [diff] [blame] | 1600 | u64 size; |
| 1601 | |
Yan, Zheng | 0ca1f7c | 2010-05-16 10:48:47 -0400 | [diff] [blame] | 1602 | /* not delalloc, ignore it */ |
Josef Bacik | 9ed74f2 | 2009-09-11 16:12:44 -0400 | [diff] [blame] | 1603 | if (!(orig->state & EXTENT_DELALLOC)) |
Jeff Mahoney | 1bf8504 | 2011-07-21 16:56:09 +0000 | [diff] [blame] | 1604 | return; |
Josef Bacik | 9ed74f2 | 2009-09-11 16:12:44 -0400 | [diff] [blame] | 1605 | |
Josef Bacik | dcab6a3 | 2015-02-11 15:08:59 -0500 | [diff] [blame] | 1606 | size = orig->end - orig->start + 1; |
| 1607 | if (size > BTRFS_MAX_EXTENT_SIZE) { |
David Sterba | 823bb20 | 2017-01-04 11:09:51 +0100 | [diff] [blame] | 1608 | u32 num_extents; |
Josef Bacik | dcab6a3 | 2015-02-11 15:08:59 -0500 | [diff] [blame] | 1609 | u64 new_size; |
| 1610 | |
| 1611 | /* |
Josef Bacik | ba11721 | 2015-03-13 15:01:24 -0400 | [diff] [blame] | 1612 | * See the explanation in btrfs_merge_extent_hook, the same |
| 1613 | * applies here, just in reverse. |
Josef Bacik | dcab6a3 | 2015-02-11 15:08:59 -0500 | [diff] [blame] | 1614 | */ |
| 1615 | new_size = orig->end - split + 1; |
David Sterba | 823bb20 | 2017-01-04 11:09:51 +0100 | [diff] [blame] | 1616 | num_extents = count_max_extents(new_size); |
Josef Bacik | ba11721 | 2015-03-13 15:01:24 -0400 | [diff] [blame] | 1617 | new_size = split - orig->start; |
David Sterba | 823bb20 | 2017-01-04 11:09:51 +0100 | [diff] [blame] | 1618 | num_extents += count_max_extents(new_size); |
| 1619 | if (count_max_extents(size) >= num_extents) |
Josef Bacik | dcab6a3 | 2015-02-11 15:08:59 -0500 | [diff] [blame] | 1620 | return; |
| 1621 | } |
| 1622 | |
Josef Bacik | 9e0baf6 | 2011-07-15 15:16:44 +0000 | [diff] [blame] | 1623 | spin_lock(&BTRFS_I(inode)->lock); |
| 1624 | BTRFS_I(inode)->outstanding_extents++; |
| 1625 | spin_unlock(&BTRFS_I(inode)->lock); |
Josef Bacik | 9ed74f2 | 2009-09-11 16:12:44 -0400 | [diff] [blame] | 1626 | } |
| 1627 | |
| 1628 | /* |
| 1629 | * extent_io.c merge_extent_hook, used to track merged delayed allocation |
| 1630 | * extents so we can keep track of new extents that are just merged onto old |
| 1631 | * extents, such as when we are doing sequential writes, so we can properly |
| 1632 | * account for the metadata space we'll need. |
| 1633 | */ |
Jeff Mahoney | 1bf8504 | 2011-07-21 16:56:09 +0000 | [diff] [blame] | 1634 | static void btrfs_merge_extent_hook(struct inode *inode, |
| 1635 | struct extent_state *new, |
| 1636 | struct extent_state *other) |
Josef Bacik | 9ed74f2 | 2009-09-11 16:12:44 -0400 | [diff] [blame] | 1637 | { |
Josef Bacik | dcab6a3 | 2015-02-11 15:08:59 -0500 | [diff] [blame] | 1638 | u64 new_size, old_size; |
David Sterba | 823bb20 | 2017-01-04 11:09:51 +0100 | [diff] [blame] | 1639 | u32 num_extents; |
Josef Bacik | dcab6a3 | 2015-02-11 15:08:59 -0500 | [diff] [blame] | 1640 | |
Josef Bacik | 9ed74f2 | 2009-09-11 16:12:44 -0400 | [diff] [blame] | 1641 | /* not delalloc, ignore it */ |
| 1642 | if (!(other->state & EXTENT_DELALLOC)) |
Jeff Mahoney | 1bf8504 | 2011-07-21 16:56:09 +0000 | [diff] [blame] | 1643 | return; |
Josef Bacik | 9ed74f2 | 2009-09-11 16:12:44 -0400 | [diff] [blame] | 1644 | |
Josef Bacik | 8461a3d | 2015-03-13 15:12:08 -0400 | [diff] [blame] | 1645 | if (new->start > other->start) |
| 1646 | new_size = new->end - other->start + 1; |
| 1647 | else |
| 1648 | new_size = other->end - new->start + 1; |
Josef Bacik | dcab6a3 | 2015-02-11 15:08:59 -0500 | [diff] [blame] | 1649 | |
| 1650 | /* we're not bigger than the max, unreserve the space and go */ |
| 1651 | if (new_size <= BTRFS_MAX_EXTENT_SIZE) { |
| 1652 | spin_lock(&BTRFS_I(inode)->lock); |
| 1653 | BTRFS_I(inode)->outstanding_extents--; |
| 1654 | spin_unlock(&BTRFS_I(inode)->lock); |
| 1655 | return; |
| 1656 | } |
| 1657 | |
| 1658 | /* |
Josef Bacik | ba11721 | 2015-03-13 15:01:24 -0400 | [diff] [blame] | 1659 | * We have to add up either side to figure out how many extents were |
| 1660 | * accounted for before we merged into one big extent. If the number of |
| 1661 | * extents we accounted for is <= the amount we need for the new range |
| 1662 | * then we can return, otherwise drop. Think of it like this |
| 1663 | * |
| 1664 | * [ 4k][MAX_SIZE] |
| 1665 | * |
| 1666 | * So we've grown the extent by a MAX_SIZE extent, this would mean we |
| 1667 | * need 2 outstanding extents, on one side we have 1 and the other side |
| 1668 | * we have 1 so they are == and we can return. But in this case |
| 1669 | * |
| 1670 | * [MAX_SIZE+4k][MAX_SIZE+4k] |
| 1671 | * |
| 1672 | * Each range on their own accounts for 2 extents, but merged together |
| 1673 | * they are only 3 extents worth of accounting, so we need to drop in |
| 1674 | * this case. |
Josef Bacik | dcab6a3 | 2015-02-11 15:08:59 -0500 | [diff] [blame] | 1675 | */ |
Josef Bacik | ba11721 | 2015-03-13 15:01:24 -0400 | [diff] [blame] | 1676 | old_size = other->end - other->start + 1; |
David Sterba | 823bb20 | 2017-01-04 11:09:51 +0100 | [diff] [blame] | 1677 | num_extents = count_max_extents(old_size); |
Josef Bacik | ba11721 | 2015-03-13 15:01:24 -0400 | [diff] [blame] | 1678 | old_size = new->end - new->start + 1; |
David Sterba | 823bb20 | 2017-01-04 11:09:51 +0100 | [diff] [blame] | 1679 | num_extents += count_max_extents(old_size); |
| 1680 | if (count_max_extents(new_size) >= num_extents) |
Josef Bacik | dcab6a3 | 2015-02-11 15:08:59 -0500 | [diff] [blame] | 1681 | return; |
| 1682 | |
Josef Bacik | 9e0baf6 | 2011-07-15 15:16:44 +0000 | [diff] [blame] | 1683 | spin_lock(&BTRFS_I(inode)->lock); |
| 1684 | BTRFS_I(inode)->outstanding_extents--; |
| 1685 | spin_unlock(&BTRFS_I(inode)->lock); |
Josef Bacik | 9ed74f2 | 2009-09-11 16:12:44 -0400 | [diff] [blame] | 1686 | } |
| 1687 | |
Miao Xie | eb73c1b | 2013-05-15 07:48:22 +0000 | [diff] [blame] | 1688 | static void btrfs_add_delalloc_inodes(struct btrfs_root *root, |
| 1689 | struct inode *inode) |
| 1690 | { |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 1691 | struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb); |
| 1692 | |
Miao Xie | eb73c1b | 2013-05-15 07:48:22 +0000 | [diff] [blame] | 1693 | spin_lock(&root->delalloc_lock); |
| 1694 | if (list_empty(&BTRFS_I(inode)->delalloc_inodes)) { |
| 1695 | list_add_tail(&BTRFS_I(inode)->delalloc_inodes, |
| 1696 | &root->delalloc_inodes); |
| 1697 | set_bit(BTRFS_INODE_IN_DELALLOC_LIST, |
| 1698 | &BTRFS_I(inode)->runtime_flags); |
| 1699 | root->nr_delalloc_inodes++; |
| 1700 | if (root->nr_delalloc_inodes == 1) { |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 1701 | spin_lock(&fs_info->delalloc_root_lock); |
Miao Xie | eb73c1b | 2013-05-15 07:48:22 +0000 | [diff] [blame] | 1702 | BUG_ON(!list_empty(&root->delalloc_root)); |
| 1703 | list_add_tail(&root->delalloc_root, |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 1704 | &fs_info->delalloc_roots); |
| 1705 | spin_unlock(&fs_info->delalloc_root_lock); |
Miao Xie | eb73c1b | 2013-05-15 07:48:22 +0000 | [diff] [blame] | 1706 | } |
| 1707 | } |
| 1708 | spin_unlock(&root->delalloc_lock); |
| 1709 | } |
| 1710 | |
| 1711 | static void btrfs_del_delalloc_inode(struct btrfs_root *root, |
| 1712 | struct inode *inode) |
| 1713 | { |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 1714 | struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb); |
| 1715 | |
Miao Xie | eb73c1b | 2013-05-15 07:48:22 +0000 | [diff] [blame] | 1716 | spin_lock(&root->delalloc_lock); |
| 1717 | if (!list_empty(&BTRFS_I(inode)->delalloc_inodes)) { |
| 1718 | list_del_init(&BTRFS_I(inode)->delalloc_inodes); |
| 1719 | clear_bit(BTRFS_INODE_IN_DELALLOC_LIST, |
| 1720 | &BTRFS_I(inode)->runtime_flags); |
| 1721 | root->nr_delalloc_inodes--; |
| 1722 | if (!root->nr_delalloc_inodes) { |
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_del_init(&root->delalloc_root); |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 1726 | spin_unlock(&fs_info->delalloc_root_lock); |
Miao Xie | eb73c1b | 2013-05-15 07:48:22 +0000 | [diff] [blame] | 1727 | } |
| 1728 | } |
| 1729 | spin_unlock(&root->delalloc_lock); |
| 1730 | } |
| 1731 | |
Chris Mason | d352ac6 | 2008-09-29 15:18:18 -0400 | [diff] [blame] | 1732 | /* |
| 1733 | * extent_io.c set_bit_hook, used to track delayed allocation |
| 1734 | * bytes in this file, and to maintain the list of inodes that |
| 1735 | * have pending delalloc work to be done. |
| 1736 | */ |
Jeff Mahoney | 1bf8504 | 2011-07-21 16:56:09 +0000 | [diff] [blame] | 1737 | static void btrfs_set_bit_hook(struct inode *inode, |
David Sterba | 9ee49a04 | 2015-01-14 19:52:13 +0100 | [diff] [blame] | 1738 | struct extent_state *state, unsigned *bits) |
Chris Mason | 291d673 | 2008-01-29 15:55:23 -0500 | [diff] [blame] | 1739 | { |
Josef Bacik | 9ed74f2 | 2009-09-11 16:12:44 -0400 | [diff] [blame] | 1740 | |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 1741 | struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb); |
| 1742 | |
Wang Shilong | 47059d9 | 2014-07-03 18:22:07 +0800 | [diff] [blame] | 1743 | if ((*bits & EXTENT_DEFRAG) && !(*bits & EXTENT_DELALLOC)) |
| 1744 | WARN_ON(1); |
Chris Mason | 75eff68 | 2008-12-15 15:54:40 -0500 | [diff] [blame] | 1745 | /* |
| 1746 | * set_bit and clear bit hooks normally require _irqsave/restore |
Sergei Trofimovich | 27160b6 | 2011-05-20 20:20:32 +0000 | [diff] [blame] | 1747 | * but in this case, we are only testing for the DELALLOC |
Chris Mason | 75eff68 | 2008-12-15 15:54:40 -0500 | [diff] [blame] | 1748 | * bit, which is only set or cleared with irqs on |
| 1749 | */ |
Yan, Zheng | 0ca1f7c | 2010-05-16 10:48:47 -0400 | [diff] [blame] | 1750 | if (!(state->state & EXTENT_DELALLOC) && (*bits & EXTENT_DELALLOC)) { |
Chris Mason | 291d673 | 2008-01-29 15:55:23 -0500 | [diff] [blame] | 1751 | struct btrfs_root *root = BTRFS_I(inode)->root; |
Yan, Zheng | 0ca1f7c | 2010-05-16 10:48:47 -0400 | [diff] [blame] | 1752 | u64 len = state->end + 1 - state->start; |
Liu Bo | 83eea1f | 2012-07-10 05:28:39 -0600 | [diff] [blame] | 1753 | bool do_list = !btrfs_is_free_space_inode(inode); |
Josef Bacik | 9ed74f2 | 2009-09-11 16:12:44 -0400 | [diff] [blame] | 1754 | |
Josef Bacik | 9e0baf6 | 2011-07-15 15:16:44 +0000 | [diff] [blame] | 1755 | if (*bits & EXTENT_FIRST_DELALLOC) { |
Yan, Zheng | 0ca1f7c | 2010-05-16 10:48:47 -0400 | [diff] [blame] | 1756 | *bits &= ~EXTENT_FIRST_DELALLOC; |
Josef Bacik | 9e0baf6 | 2011-07-15 15:16:44 +0000 | [diff] [blame] | 1757 | } else { |
| 1758 | spin_lock(&BTRFS_I(inode)->lock); |
| 1759 | BTRFS_I(inode)->outstanding_extents++; |
| 1760 | spin_unlock(&BTRFS_I(inode)->lock); |
| 1761 | } |
Josef Bacik | 287a0ab | 2010-03-19 18:07:23 +0000 | [diff] [blame] | 1762 | |
Josef Bacik | 6a3891c | 2015-03-16 17:38:52 -0400 | [diff] [blame] | 1763 | /* For sanity tests */ |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 1764 | if (btrfs_is_testing(fs_info)) |
Josef Bacik | 6a3891c | 2015-03-16 17:38:52 -0400 | [diff] [blame] | 1765 | return; |
| 1766 | |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 1767 | __percpu_counter_add(&fs_info->delalloc_bytes, len, |
| 1768 | fs_info->delalloc_batch); |
Miao Xie | df0af1a | 2013-01-29 10:11:59 +0000 | [diff] [blame] | 1769 | spin_lock(&BTRFS_I(inode)->lock); |
Yan, Zheng | 0ca1f7c | 2010-05-16 10:48:47 -0400 | [diff] [blame] | 1770 | BTRFS_I(inode)->delalloc_bytes += len; |
Wang Shilong | 47059d9 | 2014-07-03 18:22:07 +0800 | [diff] [blame] | 1771 | if (*bits & EXTENT_DEFRAG) |
| 1772 | BTRFS_I(inode)->defrag_bytes += len; |
Miao Xie | df0af1a | 2013-01-29 10:11:59 +0000 | [diff] [blame] | 1773 | if (do_list && !test_bit(BTRFS_INODE_IN_DELALLOC_LIST, |
Miao Xie | eb73c1b | 2013-05-15 07:48:22 +0000 | [diff] [blame] | 1774 | &BTRFS_I(inode)->runtime_flags)) |
| 1775 | btrfs_add_delalloc_inodes(root, inode); |
Miao Xie | df0af1a | 2013-01-29 10:11:59 +0000 | [diff] [blame] | 1776 | spin_unlock(&BTRFS_I(inode)->lock); |
Chris Mason | 291d673 | 2008-01-29 15:55:23 -0500 | [diff] [blame] | 1777 | } |
Chris Mason | 291d673 | 2008-01-29 15:55:23 -0500 | [diff] [blame] | 1778 | } |
| 1779 | |
Chris Mason | d352ac6 | 2008-09-29 15:18:18 -0400 | [diff] [blame] | 1780 | /* |
| 1781 | * extent_io.c clear_bit_hook, see set_bit_hook for why |
| 1782 | */ |
Jeff Mahoney | 1bf8504 | 2011-07-21 16:56:09 +0000 | [diff] [blame] | 1783 | static void btrfs_clear_bit_hook(struct inode *inode, |
David Sterba | 4107488 | 2013-04-29 13:38:46 +0000 | [diff] [blame] | 1784 | struct extent_state *state, |
David Sterba | 9ee49a04 | 2015-01-14 19:52:13 +0100 | [diff] [blame] | 1785 | unsigned *bits) |
Chris Mason | 291d673 | 2008-01-29 15:55:23 -0500 | [diff] [blame] | 1786 | { |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 1787 | struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb); |
Wang Shilong | 47059d9 | 2014-07-03 18:22:07 +0800 | [diff] [blame] | 1788 | u64 len = state->end + 1 - state->start; |
David Sterba | 823bb20 | 2017-01-04 11:09:51 +0100 | [diff] [blame] | 1789 | u32 num_extents = count_max_extents(len); |
Wang Shilong | 47059d9 | 2014-07-03 18:22:07 +0800 | [diff] [blame] | 1790 | |
| 1791 | spin_lock(&BTRFS_I(inode)->lock); |
| 1792 | if ((state->state & EXTENT_DEFRAG) && (*bits & EXTENT_DEFRAG)) |
| 1793 | BTRFS_I(inode)->defrag_bytes -= len; |
| 1794 | spin_unlock(&BTRFS_I(inode)->lock); |
| 1795 | |
Chris Mason | 75eff68 | 2008-12-15 15:54:40 -0500 | [diff] [blame] | 1796 | /* |
| 1797 | * set_bit and clear bit hooks normally require _irqsave/restore |
Sergei Trofimovich | 27160b6 | 2011-05-20 20:20:32 +0000 | [diff] [blame] | 1798 | * but in this case, we are only testing for the DELALLOC |
Chris Mason | 75eff68 | 2008-12-15 15:54:40 -0500 | [diff] [blame] | 1799 | * bit, which is only set or cleared with irqs on |
| 1800 | */ |
Yan, Zheng | 0ca1f7c | 2010-05-16 10:48:47 -0400 | [diff] [blame] | 1801 | if ((state->state & EXTENT_DELALLOC) && (*bits & EXTENT_DELALLOC)) { |
Chris Mason | 291d673 | 2008-01-29 15:55:23 -0500 | [diff] [blame] | 1802 | struct btrfs_root *root = BTRFS_I(inode)->root; |
Liu Bo | 83eea1f | 2012-07-10 05:28:39 -0600 | [diff] [blame] | 1803 | bool do_list = !btrfs_is_free_space_inode(inode); |
Chris Mason | bcbfce8 | 2008-04-22 13:26:47 -0400 | [diff] [blame] | 1804 | |
Josef Bacik | 9e0baf6 | 2011-07-15 15:16:44 +0000 | [diff] [blame] | 1805 | if (*bits & EXTENT_FIRST_DELALLOC) { |
Yan, Zheng | 0ca1f7c | 2010-05-16 10:48:47 -0400 | [diff] [blame] | 1806 | *bits &= ~EXTENT_FIRST_DELALLOC; |
Josef Bacik | 9e0baf6 | 2011-07-15 15:16:44 +0000 | [diff] [blame] | 1807 | } else if (!(*bits & EXTENT_DO_ACCOUNTING)) { |
| 1808 | spin_lock(&BTRFS_I(inode)->lock); |
Josef Bacik | dcab6a3 | 2015-02-11 15:08:59 -0500 | [diff] [blame] | 1809 | BTRFS_I(inode)->outstanding_extents -= num_extents; |
Josef Bacik | 9e0baf6 | 2011-07-15 15:16:44 +0000 | [diff] [blame] | 1810 | spin_unlock(&BTRFS_I(inode)->lock); |
| 1811 | } |
Yan, Zheng | 0ca1f7c | 2010-05-16 10:48:47 -0400 | [diff] [blame] | 1812 | |
Josef Bacik | b6d08f0 | 2013-09-27 14:57:43 -0400 | [diff] [blame] | 1813 | /* |
| 1814 | * We don't reserve metadata space for space cache inodes so we |
| 1815 | * don't need to call dellalloc_release_metadata if there is an |
| 1816 | * error. |
| 1817 | */ |
| 1818 | if (*bits & EXTENT_DO_ACCOUNTING && |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 1819 | root != fs_info->tree_root) |
Yan, Zheng | 0ca1f7c | 2010-05-16 10:48:47 -0400 | [diff] [blame] | 1820 | btrfs_delalloc_release_metadata(inode, len); |
| 1821 | |
Josef Bacik | 6a3891c | 2015-03-16 17:38:52 -0400 | [diff] [blame] | 1822 | /* For sanity tests. */ |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 1823 | if (btrfs_is_testing(fs_info)) |
Josef Bacik | 6a3891c | 2015-03-16 17:38:52 -0400 | [diff] [blame] | 1824 | return; |
| 1825 | |
Josef Bacik | 0cb59c9 | 2010-07-02 12:14:14 -0400 | [diff] [blame] | 1826 | if (root->root_key.objectid != BTRFS_DATA_RELOC_TREE_OBJECTID |
Wang Xiaoguang | 1851309 | 2016-07-25 15:51:40 +0800 | [diff] [blame] | 1827 | && do_list && !(state->state & EXTENT_NORESERVE) |
| 1828 | && (*bits & (EXTENT_DO_ACCOUNTING | |
| 1829 | EXTENT_CLEAR_DATA_RESV))) |
Qu Wenruo | 51773be | 2015-10-08 18:19:37 +0800 | [diff] [blame] | 1830 | btrfs_free_reserved_data_space_noquota(inode, |
| 1831 | state->start, len); |
Josef Bacik | 9ed74f2 | 2009-09-11 16:12:44 -0400 | [diff] [blame] | 1832 | |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 1833 | __percpu_counter_add(&fs_info->delalloc_bytes, -len, |
| 1834 | fs_info->delalloc_batch); |
Miao Xie | df0af1a | 2013-01-29 10:11:59 +0000 | [diff] [blame] | 1835 | spin_lock(&BTRFS_I(inode)->lock); |
Yan, Zheng | 0ca1f7c | 2010-05-16 10:48:47 -0400 | [diff] [blame] | 1836 | BTRFS_I(inode)->delalloc_bytes -= len; |
Josef Bacik | 0cb59c9 | 2010-07-02 12:14:14 -0400 | [diff] [blame] | 1837 | if (do_list && BTRFS_I(inode)->delalloc_bytes == 0 && |
Miao Xie | df0af1a | 2013-01-29 10:11:59 +0000 | [diff] [blame] | 1838 | test_bit(BTRFS_INODE_IN_DELALLOC_LIST, |
Miao Xie | eb73c1b | 2013-05-15 07:48:22 +0000 | [diff] [blame] | 1839 | &BTRFS_I(inode)->runtime_flags)) |
| 1840 | btrfs_del_delalloc_inode(root, inode); |
Miao Xie | df0af1a | 2013-01-29 10:11:59 +0000 | [diff] [blame] | 1841 | spin_unlock(&BTRFS_I(inode)->lock); |
Chris Mason | 291d673 | 2008-01-29 15:55:23 -0500 | [diff] [blame] | 1842 | } |
Chris Mason | 291d673 | 2008-01-29 15:55:23 -0500 | [diff] [blame] | 1843 | } |
| 1844 | |
Chris Mason | d352ac6 | 2008-09-29 15:18:18 -0400 | [diff] [blame] | 1845 | /* |
| 1846 | * extent_io.c merge_bio_hook, this must check the chunk tree to make sure |
| 1847 | * we don't create bios that span stripes or chunks |
Liu Bo | 6f034ec | 2016-06-22 18:31:49 -0700 | [diff] [blame] | 1848 | * |
| 1849 | * return 1 if page cannot be merged to bio |
| 1850 | * return 0 if page can be merged to bio |
| 1851 | * return error otherwise |
Chris Mason | d352ac6 | 2008-09-29 15:18:18 -0400 | [diff] [blame] | 1852 | */ |
Mike Christie | 81a75f67 | 2016-06-05 14:31:54 -0500 | [diff] [blame] | 1853 | int btrfs_merge_bio_hook(struct page *page, unsigned long offset, |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 1854 | size_t size, struct bio *bio, |
| 1855 | unsigned long bio_flags) |
Chris Mason | 239b14b | 2008-03-24 15:02:07 -0400 | [diff] [blame] | 1856 | { |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 1857 | struct inode *inode = page->mapping->host; |
| 1858 | struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb); |
Kent Overstreet | 4f024f3 | 2013-10-11 15:44:27 -0700 | [diff] [blame] | 1859 | u64 logical = (u64)bio->bi_iter.bi_sector << 9; |
Chris Mason | 239b14b | 2008-03-24 15:02:07 -0400 | [diff] [blame] | 1860 | u64 length = 0; |
| 1861 | u64 map_length; |
Chris Mason | 239b14b | 2008-03-24 15:02:07 -0400 | [diff] [blame] | 1862 | int ret; |
| 1863 | |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 1864 | if (bio_flags & EXTENT_BIO_COMPRESSED) |
| 1865 | return 0; |
| 1866 | |
Kent Overstreet | 4f024f3 | 2013-10-11 15:44:27 -0700 | [diff] [blame] | 1867 | length = bio->bi_iter.bi_size; |
Chris Mason | 239b14b | 2008-03-24 15:02:07 -0400 | [diff] [blame] | 1868 | map_length = length; |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 1869 | ret = btrfs_map_block(fs_info, btrfs_op(bio), logical, &map_length, |
| 1870 | NULL, 0); |
Liu Bo | 6f034ec | 2016-06-22 18:31:49 -0700 | [diff] [blame] | 1871 | if (ret < 0) |
| 1872 | return ret; |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 1873 | if (map_length < length + size) |
Chris Mason | 239b14b | 2008-03-24 15:02:07 -0400 | [diff] [blame] | 1874 | return 1; |
Jeff Mahoney | 3444a97 | 2011-10-03 23:23:13 -0400 | [diff] [blame] | 1875 | return 0; |
Chris Mason | 239b14b | 2008-03-24 15:02:07 -0400 | [diff] [blame] | 1876 | } |
| 1877 | |
Chris Mason | d352ac6 | 2008-09-29 15:18:18 -0400 | [diff] [blame] | 1878 | /* |
| 1879 | * in order to insert checksums into the metadata in large chunks, |
| 1880 | * we wait until bio submission time. All the pages in the bio are |
| 1881 | * checksummed and sums are attached onto the ordered extent record. |
| 1882 | * |
| 1883 | * At IO completion time the cums attached on the ordered extent record |
| 1884 | * are inserted into the btree |
| 1885 | */ |
Mike Christie | 81a75f67 | 2016-06-05 14:31:54 -0500 | [diff] [blame] | 1886 | static int __btrfs_submit_bio_start(struct inode *inode, struct bio *bio, |
| 1887 | int mirror_num, unsigned long bio_flags, |
Chris Mason | eaf25d9 | 2010-05-25 09:48:28 -0400 | [diff] [blame] | 1888 | u64 bio_offset) |
Chris Mason | 065631f | 2008-02-20 12:07:25 -0500 | [diff] [blame] | 1889 | { |
Chris Mason | 065631f | 2008-02-20 12:07:25 -0500 | [diff] [blame] | 1890 | int ret = 0; |
Chris Mason | e015640 | 2008-04-16 11:15:20 -0400 | [diff] [blame] | 1891 | |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 1892 | ret = btrfs_csum_one_bio(inode, bio, 0, 0); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 1893 | BUG_ON(ret); /* -ENOMEM */ |
Chris Mason | 4a69a41 | 2008-11-06 22:03:00 -0500 | [diff] [blame] | 1894 | return 0; |
| 1895 | } |
Chris Mason | e015640 | 2008-04-16 11:15:20 -0400 | [diff] [blame] | 1896 | |
Chris Mason | 4a69a41 | 2008-11-06 22:03:00 -0500 | [diff] [blame] | 1897 | /* |
| 1898 | * in order to insert checksums into the metadata in large chunks, |
| 1899 | * we wait until bio submission time. All the pages in the bio are |
| 1900 | * checksummed and sums are attached onto the ordered extent record. |
| 1901 | * |
| 1902 | * At IO completion time the cums attached on the ordered extent record |
| 1903 | * are inserted into the btree |
| 1904 | */ |
Mike Christie | 81a75f67 | 2016-06-05 14:31:54 -0500 | [diff] [blame] | 1905 | static int __btrfs_submit_bio_done(struct inode *inode, struct bio *bio, |
Chris Mason | eaf25d9 | 2010-05-25 09:48:28 -0400 | [diff] [blame] | 1906 | int mirror_num, unsigned long bio_flags, |
| 1907 | u64 bio_offset) |
Chris Mason | 4a69a41 | 2008-11-06 22:03:00 -0500 | [diff] [blame] | 1908 | { |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 1909 | struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb); |
Stefan Behrens | 6189192 | 2012-11-05 18:51:52 +0100 | [diff] [blame] | 1910 | int ret; |
| 1911 | |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 1912 | ret = btrfs_map_bio(fs_info, bio, mirror_num, 1); |
Christoph Hellwig | 4246a0b | 2015-07-20 15:29:37 +0200 | [diff] [blame] | 1913 | if (ret) { |
| 1914 | bio->bi_error = ret; |
| 1915 | bio_endio(bio); |
| 1916 | } |
Stefan Behrens | 6189192 | 2012-11-05 18:51:52 +0100 | [diff] [blame] | 1917 | return ret; |
Chris Mason | 44b8bd7 | 2008-04-16 11:14:51 -0400 | [diff] [blame] | 1918 | } |
| 1919 | |
Chris Mason | d352ac6 | 2008-09-29 15:18:18 -0400 | [diff] [blame] | 1920 | /* |
Chris Mason | cad321a | 2008-12-17 14:51:42 -0500 | [diff] [blame] | 1921 | * extent_io.c submission hook. This does the right thing for csum calculation |
| 1922 | * on write, or reading the csums from the tree before a read |
Chris Mason | d352ac6 | 2008-09-29 15:18:18 -0400 | [diff] [blame] | 1923 | */ |
Mike Christie | 81a75f67 | 2016-06-05 14:31:54 -0500 | [diff] [blame] | 1924 | static int btrfs_submit_bio_hook(struct inode *inode, struct bio *bio, |
Chris Mason | eaf25d9 | 2010-05-25 09:48:28 -0400 | [diff] [blame] | 1925 | int mirror_num, unsigned long bio_flags, |
| 1926 | u64 bio_offset) |
Chris Mason | 44b8bd7 | 2008-04-16 11:14:51 -0400 | [diff] [blame] | 1927 | { |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 1928 | struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb); |
Chris Mason | 44b8bd7 | 2008-04-16 11:14:51 -0400 | [diff] [blame] | 1929 | struct btrfs_root *root = BTRFS_I(inode)->root; |
Chandan Rajendra | 0d51e28 | 2015-07-27 15:26:43 +0530 | [diff] [blame] | 1930 | enum btrfs_wq_endio_type metadata = BTRFS_WQ_ENDIO_DATA; |
Chris Mason | 44b8bd7 | 2008-04-16 11:14:51 -0400 | [diff] [blame] | 1931 | int ret = 0; |
Chris Mason | 19b9bdb | 2008-10-30 14:23:13 -0400 | [diff] [blame] | 1932 | int skip_sum; |
Josef Bacik | b812ce2 | 2012-11-16 13:56:32 -0500 | [diff] [blame] | 1933 | int async = !atomic_read(&BTRFS_I(inode)->sync_writers); |
Chris Mason | 44b8bd7 | 2008-04-16 11:14:51 -0400 | [diff] [blame] | 1934 | |
Christoph Hellwig | 6cbff00 | 2009-04-17 10:37:41 +0200 | [diff] [blame] | 1935 | skip_sum = BTRFS_I(inode)->flags & BTRFS_INODE_NODATASUM; |
Chris Mason | cad321a | 2008-12-17 14:51:42 -0500 | [diff] [blame] | 1936 | |
Liu Bo | 83eea1f | 2012-07-10 05:28:39 -0600 | [diff] [blame] | 1937 | if (btrfs_is_free_space_inode(inode)) |
Chandan Rajendra | 0d51e28 | 2015-07-27 15:26:43 +0530 | [diff] [blame] | 1938 | metadata = BTRFS_WQ_ENDIO_FREE_SPACE; |
Jeff Mahoney | 0417341 | 2011-10-03 23:23:12 -0400 | [diff] [blame] | 1939 | |
Mike Christie | 37226b2 | 2016-06-05 14:31:52 -0500 | [diff] [blame] | 1940 | if (bio_op(bio) != REQ_OP_WRITE) { |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 1941 | ret = btrfs_bio_wq_end_io(fs_info, bio, metadata); |
Josef Bacik | 5fd0204 | 2012-05-02 14:00:54 -0400 | [diff] [blame] | 1942 | if (ret) |
Stefan Behrens | 6189192 | 2012-11-05 18:51:52 +0100 | [diff] [blame] | 1943 | goto out; |
Josef Bacik | 5fd0204 | 2012-05-02 14:00:54 -0400 | [diff] [blame] | 1944 | |
Chris Mason | d20f704 | 2008-12-08 16:58:54 -0500 | [diff] [blame] | 1945 | if (bio_flags & EXTENT_BIO_COMPRESSED) { |
Stefan Behrens | 6189192 | 2012-11-05 18:51:52 +0100 | [diff] [blame] | 1946 | ret = btrfs_submit_compressed_read(inode, bio, |
| 1947 | mirror_num, |
| 1948 | bio_flags); |
| 1949 | goto out; |
Tsutomu Itoh | c2db107 | 2011-03-01 06:48:31 +0000 | [diff] [blame] | 1950 | } else if (!skip_sum) { |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 1951 | ret = btrfs_lookup_bio_sums(inode, bio, NULL); |
Tsutomu Itoh | c2db107 | 2011-03-01 06:48:31 +0000 | [diff] [blame] | 1952 | if (ret) |
Stefan Behrens | 6189192 | 2012-11-05 18:51:52 +0100 | [diff] [blame] | 1953 | goto out; |
Tsutomu Itoh | c2db107 | 2011-03-01 06:48:31 +0000 | [diff] [blame] | 1954 | } |
Chris Mason | 4d1b5fb | 2008-08-20 09:44:52 -0400 | [diff] [blame] | 1955 | goto mapit; |
Josef Bacik | b812ce2 | 2012-11-16 13:56:32 -0500 | [diff] [blame] | 1956 | } else if (async && !skip_sum) { |
Yan Zheng | 17d217f | 2008-12-12 10:03:38 -0500 | [diff] [blame] | 1957 | /* csum items have already been cloned */ |
| 1958 | if (root->root_key.objectid == BTRFS_DATA_RELOC_TREE_OBJECTID) |
| 1959 | goto mapit; |
Chris Mason | 19b9bdb | 2008-10-30 14:23:13 -0400 | [diff] [blame] | 1960 | /* we're doing a write, do the async checksumming */ |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 1961 | ret = btrfs_wq_submit_bio(fs_info, inode, bio, mirror_num, |
| 1962 | bio_flags, bio_offset, |
| 1963 | __btrfs_submit_bio_start, |
| 1964 | __btrfs_submit_bio_done); |
Stefan Behrens | 6189192 | 2012-11-05 18:51:52 +0100 | [diff] [blame] | 1965 | goto out; |
Josef Bacik | b812ce2 | 2012-11-16 13:56:32 -0500 | [diff] [blame] | 1966 | } else if (!skip_sum) { |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 1967 | ret = btrfs_csum_one_bio(inode, bio, 0, 0); |
Josef Bacik | b812ce2 | 2012-11-16 13:56:32 -0500 | [diff] [blame] | 1968 | if (ret) |
| 1969 | goto out; |
Chris Mason | 19b9bdb | 2008-10-30 14:23:13 -0400 | [diff] [blame] | 1970 | } |
| 1971 | |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 1972 | mapit: |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 1973 | ret = btrfs_map_bio(fs_info, bio, mirror_num, 0); |
Stefan Behrens | 6189192 | 2012-11-05 18:51:52 +0100 | [diff] [blame] | 1974 | |
| 1975 | out: |
Christoph Hellwig | 4246a0b | 2015-07-20 15:29:37 +0200 | [diff] [blame] | 1976 | if (ret < 0) { |
| 1977 | bio->bi_error = ret; |
| 1978 | bio_endio(bio); |
| 1979 | } |
Stefan Behrens | 6189192 | 2012-11-05 18:51:52 +0100 | [diff] [blame] | 1980 | return ret; |
Chris Mason | 065631f | 2008-02-20 12:07:25 -0500 | [diff] [blame] | 1981 | } |
Chris Mason | 6885f30 | 2008-02-20 16:11:05 -0500 | [diff] [blame] | 1982 | |
Chris Mason | d352ac6 | 2008-09-29 15:18:18 -0400 | [diff] [blame] | 1983 | /* |
| 1984 | * given a list of ordered sums record them in the inode. This happens |
| 1985 | * at IO completion time based on sums calculated at bio submission time. |
| 1986 | */ |
Chris Mason | ba1da2f | 2008-07-17 12:54:15 -0400 | [diff] [blame] | 1987 | static noinline int add_pending_csums(struct btrfs_trans_handle *trans, |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 1988 | struct inode *inode, u64 file_offset, |
| 1989 | struct list_head *list) |
| 1990 | { |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 1991 | struct btrfs_ordered_sum *sum; |
| 1992 | |
Qinghuang Feng | c6e3087 | 2009-01-21 10:59:08 -0500 | [diff] [blame] | 1993 | list_for_each_entry(sum, list, list) { |
Miao Xie | 39847c4 | 2013-03-28 08:08:20 +0000 | [diff] [blame] | 1994 | trans->adding_csums = 1; |
Chris Mason | d20f704 | 2008-12-08 16:58:54 -0500 | [diff] [blame] | 1995 | btrfs_csum_file_blocks(trans, |
| 1996 | BTRFS_I(inode)->root->fs_info->csum_root, sum); |
Miao Xie | 39847c4 | 2013-03-28 08:08:20 +0000 | [diff] [blame] | 1997 | trans->adding_csums = 0; |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 1998 | } |
| 1999 | return 0; |
| 2000 | } |
| 2001 | |
Josef Bacik | 2ac55d4 | 2010-02-03 19:33:23 +0000 | [diff] [blame] | 2002 | int btrfs_set_extent_delalloc(struct inode *inode, u64 start, u64 end, |
Qu Wenruo | ba8b04c | 2016-07-19 16:50:36 +0800 | [diff] [blame] | 2003 | struct extent_state **cached_state, int dedupe) |
Chris Mason | ea8c281 | 2008-08-04 23:17:27 -0400 | [diff] [blame] | 2004 | { |
Kirill A. Shutemov | 09cbfea | 2016-04-01 15:29:47 +0300 | [diff] [blame] | 2005 | WARN_ON((end & (PAGE_SIZE - 1)) == 0); |
Chris Mason | ea8c281 | 2008-08-04 23:17:27 -0400 | [diff] [blame] | 2006 | return set_extent_delalloc(&BTRFS_I(inode)->io_tree, start, end, |
David Sterba | 7cd8c75 | 2016-04-26 23:54:39 +0200 | [diff] [blame] | 2007 | cached_state); |
Chris Mason | ea8c281 | 2008-08-04 23:17:27 -0400 | [diff] [blame] | 2008 | } |
| 2009 | |
Chris Mason | d352ac6 | 2008-09-29 15:18:18 -0400 | [diff] [blame] | 2010 | /* see btrfs_writepage_start_hook for details on why this is required */ |
Chris Mason | 247e743 | 2008-07-17 12:53:51 -0400 | [diff] [blame] | 2011 | struct btrfs_writepage_fixup { |
| 2012 | struct page *page; |
| 2013 | struct btrfs_work work; |
| 2014 | }; |
| 2015 | |
Christoph Hellwig | b295086 | 2008-12-02 09:54:17 -0500 | [diff] [blame] | 2016 | static void btrfs_writepage_fixup_worker(struct btrfs_work *work) |
Chris Mason | 247e743 | 2008-07-17 12:53:51 -0400 | [diff] [blame] | 2017 | { |
| 2018 | struct btrfs_writepage_fixup *fixup; |
| 2019 | struct btrfs_ordered_extent *ordered; |
Josef Bacik | 2ac55d4 | 2010-02-03 19:33:23 +0000 | [diff] [blame] | 2020 | struct extent_state *cached_state = NULL; |
Chris Mason | 247e743 | 2008-07-17 12:53:51 -0400 | [diff] [blame] | 2021 | struct page *page; |
| 2022 | struct inode *inode; |
| 2023 | u64 page_start; |
| 2024 | u64 page_end; |
Jeff Mahoney | 87826df | 2012-02-15 16:23:57 +0100 | [diff] [blame] | 2025 | int ret; |
Chris Mason | 247e743 | 2008-07-17 12:53:51 -0400 | [diff] [blame] | 2026 | |
| 2027 | fixup = container_of(work, struct btrfs_writepage_fixup, work); |
| 2028 | page = fixup->page; |
Chris Mason | 4a09675 | 2008-07-21 10:29:44 -0400 | [diff] [blame] | 2029 | again: |
Chris Mason | 247e743 | 2008-07-17 12:53:51 -0400 | [diff] [blame] | 2030 | lock_page(page); |
| 2031 | if (!page->mapping || !PageDirty(page) || !PageChecked(page)) { |
| 2032 | ClearPageChecked(page); |
| 2033 | goto out_page; |
| 2034 | } |
| 2035 | |
| 2036 | inode = page->mapping->host; |
| 2037 | page_start = page_offset(page); |
Kirill A. Shutemov | 09cbfea | 2016-04-01 15:29:47 +0300 | [diff] [blame] | 2038 | page_end = page_offset(page) + PAGE_SIZE - 1; |
Chris Mason | 247e743 | 2008-07-17 12:53:51 -0400 | [diff] [blame] | 2039 | |
David Sterba | ff13db4 | 2015-12-03 14:30:40 +0100 | [diff] [blame] | 2040 | lock_extent_bits(&BTRFS_I(inode)->io_tree, page_start, page_end, |
Jeff Mahoney | d008237 | 2012-03-01 14:57:19 +0100 | [diff] [blame] | 2041 | &cached_state); |
Chris Mason | 4a09675 | 2008-07-21 10:29:44 -0400 | [diff] [blame] | 2042 | |
| 2043 | /* already ordered? We're done */ |
Chris Mason | 8b62b72 | 2009-09-02 16:53:46 -0400 | [diff] [blame] | 2044 | if (PagePrivate2(page)) |
Chris Mason | 247e743 | 2008-07-17 12:53:51 -0400 | [diff] [blame] | 2045 | goto out; |
Chris Mason | 4a09675 | 2008-07-21 10:29:44 -0400 | [diff] [blame] | 2046 | |
Chandan Rajendra | dbfdb6d | 2016-01-21 15:55:58 +0530 | [diff] [blame] | 2047 | ordered = btrfs_lookup_ordered_range(inode, page_start, |
Kirill A. Shutemov | 09cbfea | 2016-04-01 15:29:47 +0300 | [diff] [blame] | 2048 | PAGE_SIZE); |
Chris Mason | 4a09675 | 2008-07-21 10:29:44 -0400 | [diff] [blame] | 2049 | if (ordered) { |
Josef Bacik | 2ac55d4 | 2010-02-03 19:33:23 +0000 | [diff] [blame] | 2050 | unlock_extent_cached(&BTRFS_I(inode)->io_tree, page_start, |
| 2051 | page_end, &cached_state, GFP_NOFS); |
Chris Mason | 4a09675 | 2008-07-21 10:29:44 -0400 | [diff] [blame] | 2052 | unlock_page(page); |
| 2053 | btrfs_start_ordered_extent(inode, ordered, 1); |
Jeff Mahoney | 87826df | 2012-02-15 16:23:57 +0100 | [diff] [blame] | 2054 | btrfs_put_ordered_extent(ordered); |
Chris Mason | 4a09675 | 2008-07-21 10:29:44 -0400 | [diff] [blame] | 2055 | goto again; |
| 2056 | } |
Chris Mason | 247e743 | 2008-07-17 12:53:51 -0400 | [diff] [blame] | 2057 | |
Qu Wenruo | 7cf5b97 | 2015-09-08 17:25:55 +0800 | [diff] [blame] | 2058 | ret = btrfs_delalloc_reserve_space(inode, page_start, |
Kirill A. Shutemov | 09cbfea | 2016-04-01 15:29:47 +0300 | [diff] [blame] | 2059 | PAGE_SIZE); |
Jeff Mahoney | 87826df | 2012-02-15 16:23:57 +0100 | [diff] [blame] | 2060 | if (ret) { |
| 2061 | mapping_set_error(page->mapping, ret); |
| 2062 | end_extent_writepage(page, ret, page_start, page_end); |
| 2063 | ClearPageChecked(page); |
| 2064 | goto out; |
| 2065 | } |
| 2066 | |
Qu Wenruo | ba8b04c | 2016-07-19 16:50:36 +0800 | [diff] [blame] | 2067 | btrfs_set_extent_delalloc(inode, page_start, page_end, &cached_state, |
| 2068 | 0); |
Chris Mason | 247e743 | 2008-07-17 12:53:51 -0400 | [diff] [blame] | 2069 | ClearPageChecked(page); |
Jeff Mahoney | 87826df | 2012-02-15 16:23:57 +0100 | [diff] [blame] | 2070 | set_page_dirty(page); |
Chris Mason | 247e743 | 2008-07-17 12:53:51 -0400 | [diff] [blame] | 2071 | out: |
Josef Bacik | 2ac55d4 | 2010-02-03 19:33:23 +0000 | [diff] [blame] | 2072 | unlock_extent_cached(&BTRFS_I(inode)->io_tree, page_start, page_end, |
| 2073 | &cached_state, GFP_NOFS); |
Chris Mason | 247e743 | 2008-07-17 12:53:51 -0400 | [diff] [blame] | 2074 | out_page: |
| 2075 | unlock_page(page); |
Kirill A. Shutemov | 09cbfea | 2016-04-01 15:29:47 +0300 | [diff] [blame] | 2076 | put_page(page); |
Miao Xie | b897abe | 2011-01-26 16:19:22 +0800 | [diff] [blame] | 2077 | kfree(fixup); |
Chris Mason | 247e743 | 2008-07-17 12:53:51 -0400 | [diff] [blame] | 2078 | } |
| 2079 | |
| 2080 | /* |
| 2081 | * There are a few paths in the higher layers of the kernel that directly |
| 2082 | * set the page dirty bit without asking the filesystem if it is a |
| 2083 | * good idea. This causes problems because we want to make sure COW |
| 2084 | * properly happens and the data=ordered rules are followed. |
| 2085 | * |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 2086 | * 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] | 2087 | * hasn't been properly setup for IO. We kick off an async process |
| 2088 | * to fix it up. The async helper will wait for ordered extents, set |
| 2089 | * the delalloc bit and make it safe to write the page. |
| 2090 | */ |
Christoph Hellwig | b295086 | 2008-12-02 09:54:17 -0500 | [diff] [blame] | 2091 | 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] | 2092 | { |
| 2093 | struct inode *inode = page->mapping->host; |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 2094 | struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb); |
Chris Mason | 247e743 | 2008-07-17 12:53:51 -0400 | [diff] [blame] | 2095 | struct btrfs_writepage_fixup *fixup; |
Chris Mason | 247e743 | 2008-07-17 12:53:51 -0400 | [diff] [blame] | 2096 | |
Chris Mason | 8b62b72 | 2009-09-02 16:53:46 -0400 | [diff] [blame] | 2097 | /* this page is properly in the ordered list */ |
| 2098 | if (TestClearPagePrivate2(page)) |
Chris Mason | 247e743 | 2008-07-17 12:53:51 -0400 | [diff] [blame] | 2099 | return 0; |
| 2100 | |
| 2101 | if (PageChecked(page)) |
| 2102 | return -EAGAIN; |
| 2103 | |
| 2104 | fixup = kzalloc(sizeof(*fixup), GFP_NOFS); |
| 2105 | if (!fixup) |
| 2106 | return -EAGAIN; |
Chris Mason | f421950 | 2008-07-22 11:18:09 -0400 | [diff] [blame] | 2107 | |
Chris Mason | 247e743 | 2008-07-17 12:53:51 -0400 | [diff] [blame] | 2108 | SetPageChecked(page); |
Kirill A. Shutemov | 09cbfea | 2016-04-01 15:29:47 +0300 | [diff] [blame] | 2109 | get_page(page); |
Liu Bo | 9e0af23 | 2014-08-15 23:36:53 +0800 | [diff] [blame] | 2110 | btrfs_init_work(&fixup->work, btrfs_fixup_helper, |
| 2111 | btrfs_writepage_fixup_worker, NULL, NULL); |
Chris Mason | 247e743 | 2008-07-17 12:53:51 -0400 | [diff] [blame] | 2112 | fixup->page = page; |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 2113 | btrfs_queue_work(fs_info->fixup_workers, &fixup->work); |
Jeff Mahoney | 87826df | 2012-02-15 16:23:57 +0100 | [diff] [blame] | 2114 | return -EBUSY; |
Chris Mason | 247e743 | 2008-07-17 12:53:51 -0400 | [diff] [blame] | 2115 | } |
| 2116 | |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 2117 | static int insert_reserved_file_extent(struct btrfs_trans_handle *trans, |
| 2118 | struct inode *inode, u64 file_pos, |
| 2119 | u64 disk_bytenr, u64 disk_num_bytes, |
| 2120 | u64 num_bytes, u64 ram_bytes, |
| 2121 | u8 compression, u8 encryption, |
| 2122 | u16 other_encoding, int extent_type) |
| 2123 | { |
| 2124 | struct btrfs_root *root = BTRFS_I(inode)->root; |
| 2125 | struct btrfs_file_extent_item *fi; |
| 2126 | struct btrfs_path *path; |
| 2127 | struct extent_buffer *leaf; |
| 2128 | struct btrfs_key ins; |
Filipe David Borba Manana | 1acae57 | 2014-01-07 11:42:27 +0000 | [diff] [blame] | 2129 | int extent_inserted = 0; |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 2130 | int ret; |
| 2131 | |
| 2132 | path = btrfs_alloc_path(); |
Mark Fasheh | d8926bb | 2011-07-13 10:38:47 -0700 | [diff] [blame] | 2133 | if (!path) |
| 2134 | return -ENOMEM; |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 2135 | |
Chris Mason | a1ed835 | 2009-09-11 12:27:37 -0400 | [diff] [blame] | 2136 | /* |
| 2137 | * we may be replacing one extent in the tree with another. |
| 2138 | * The new extent is pinned in the extent map, and we don't want |
| 2139 | * to drop it from the cache until it is completely in the btree. |
| 2140 | * |
| 2141 | * So, tell btrfs_drop_extents to leave this extent in the cache. |
| 2142 | * the caller is expected to unpin it and allow it to be merged |
| 2143 | * with the others. |
| 2144 | */ |
Filipe David Borba Manana | 1acae57 | 2014-01-07 11:42:27 +0000 | [diff] [blame] | 2145 | ret = __btrfs_drop_extents(trans, root, inode, path, file_pos, |
| 2146 | file_pos + num_bytes, NULL, 0, |
| 2147 | 1, sizeof(*fi), &extent_inserted); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 2148 | if (ret) |
| 2149 | goto out; |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 2150 | |
Filipe David Borba Manana | 1acae57 | 2014-01-07 11:42:27 +0000 | [diff] [blame] | 2151 | if (!extent_inserted) { |
Nikolay Borisov | 4a0cc7c | 2017-01-10 20:35:31 +0200 | [diff] [blame] | 2152 | ins.objectid = btrfs_ino(BTRFS_I(inode)); |
Filipe David Borba Manana | 1acae57 | 2014-01-07 11:42:27 +0000 | [diff] [blame] | 2153 | ins.offset = file_pos; |
| 2154 | ins.type = BTRFS_EXTENT_DATA_KEY; |
| 2155 | |
| 2156 | path->leave_spinning = 1; |
| 2157 | ret = btrfs_insert_empty_item(trans, root, path, &ins, |
| 2158 | sizeof(*fi)); |
| 2159 | if (ret) |
| 2160 | goto out; |
| 2161 | } |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 2162 | leaf = path->nodes[0]; |
| 2163 | fi = btrfs_item_ptr(leaf, path->slots[0], |
| 2164 | struct btrfs_file_extent_item); |
| 2165 | btrfs_set_file_extent_generation(leaf, fi, trans->transid); |
| 2166 | btrfs_set_file_extent_type(leaf, fi, extent_type); |
| 2167 | btrfs_set_file_extent_disk_bytenr(leaf, fi, disk_bytenr); |
| 2168 | btrfs_set_file_extent_disk_num_bytes(leaf, fi, disk_num_bytes); |
| 2169 | btrfs_set_file_extent_offset(leaf, fi, 0); |
| 2170 | btrfs_set_file_extent_num_bytes(leaf, fi, num_bytes); |
| 2171 | btrfs_set_file_extent_ram_bytes(leaf, fi, ram_bytes); |
| 2172 | btrfs_set_file_extent_compression(leaf, fi, compression); |
| 2173 | btrfs_set_file_extent_encryption(leaf, fi, encryption); |
| 2174 | btrfs_set_file_extent_other_encoding(leaf, fi, other_encoding); |
Chris Mason | b947343 | 2009-03-13 11:00:37 -0400 | [diff] [blame] | 2175 | |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 2176 | btrfs_mark_buffer_dirty(leaf); |
Josef Bacik | ce19533 | 2012-09-25 15:26:16 -0400 | [diff] [blame] | 2177 | btrfs_release_path(path); |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 2178 | |
| 2179 | inode_add_bytes(inode, num_bytes); |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 2180 | |
| 2181 | ins.objectid = disk_bytenr; |
| 2182 | ins.offset = disk_num_bytes; |
| 2183 | ins.type = BTRFS_EXTENT_ITEM_KEY; |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 2184 | ret = btrfs_alloc_reserved_file_extent(trans, root->root_key.objectid, |
David Sterba | f85b737 | 2017-01-20 14:54:07 +0100 | [diff] [blame] | 2185 | btrfs_ino(BTRFS_I(inode)), file_pos, ram_bytes, &ins); |
Qu Wenruo | 297d750 | 2015-09-08 17:08:37 +0800 | [diff] [blame] | 2186 | /* |
Qu Wenruo | 5846a3c | 2015-10-26 14:11:18 +0800 | [diff] [blame] | 2187 | * Release the reserved range from inode dirty range map, as it is |
| 2188 | * already moved into delayed_ref_head |
Qu Wenruo | 297d750 | 2015-09-08 17:08:37 +0800 | [diff] [blame] | 2189 | */ |
| 2190 | btrfs_qgroup_release_data(inode, file_pos, ram_bytes); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 2191 | out: |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 2192 | btrfs_free_path(path); |
Chris Mason | b947343 | 2009-03-13 11:00:37 -0400 | [diff] [blame] | 2193 | |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 2194 | return ret; |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 2195 | } |
| 2196 | |
Liu Bo | 38c227d | 2013-01-29 03:18:40 +0000 | [diff] [blame] | 2197 | /* snapshot-aware defrag */ |
| 2198 | struct sa_defrag_extent_backref { |
| 2199 | struct rb_node node; |
| 2200 | struct old_sa_defrag_extent *old; |
| 2201 | u64 root_id; |
| 2202 | u64 inum; |
| 2203 | u64 file_pos; |
| 2204 | u64 extent_offset; |
| 2205 | u64 num_bytes; |
| 2206 | u64 generation; |
| 2207 | }; |
| 2208 | |
| 2209 | struct old_sa_defrag_extent { |
| 2210 | struct list_head list; |
| 2211 | struct new_sa_defrag_extent *new; |
| 2212 | |
| 2213 | u64 extent_offset; |
| 2214 | u64 bytenr; |
| 2215 | u64 offset; |
| 2216 | u64 len; |
| 2217 | int count; |
| 2218 | }; |
| 2219 | |
| 2220 | struct new_sa_defrag_extent { |
| 2221 | struct rb_root root; |
| 2222 | struct list_head head; |
| 2223 | struct btrfs_path *path; |
| 2224 | struct inode *inode; |
| 2225 | u64 file_pos; |
| 2226 | u64 len; |
| 2227 | u64 bytenr; |
| 2228 | u64 disk_len; |
| 2229 | u8 compress_type; |
| 2230 | }; |
| 2231 | |
| 2232 | static int backref_comp(struct sa_defrag_extent_backref *b1, |
| 2233 | struct sa_defrag_extent_backref *b2) |
| 2234 | { |
| 2235 | if (b1->root_id < b2->root_id) |
| 2236 | return -1; |
| 2237 | else if (b1->root_id > b2->root_id) |
| 2238 | return 1; |
| 2239 | |
| 2240 | if (b1->inum < b2->inum) |
| 2241 | return -1; |
| 2242 | else if (b1->inum > b2->inum) |
| 2243 | return 1; |
| 2244 | |
| 2245 | if (b1->file_pos < b2->file_pos) |
| 2246 | return -1; |
| 2247 | else if (b1->file_pos > b2->file_pos) |
| 2248 | return 1; |
| 2249 | |
| 2250 | /* |
| 2251 | * [------------------------------] ===> (a range of space) |
| 2252 | * |<--->| |<---->| =============> (fs/file tree A) |
| 2253 | * |<---------------------------->| ===> (fs/file tree B) |
| 2254 | * |
| 2255 | * A range of space can refer to two file extents in one tree while |
| 2256 | * refer to only one file extent in another tree. |
| 2257 | * |
| 2258 | * So we may process a disk offset more than one time(two extents in A) |
| 2259 | * and locate at the same extent(one extent in B), then insert two same |
| 2260 | * backrefs(both refer to the extent in B). |
| 2261 | */ |
| 2262 | return 0; |
| 2263 | } |
| 2264 | |
| 2265 | static void backref_insert(struct rb_root *root, |
| 2266 | struct sa_defrag_extent_backref *backref) |
| 2267 | { |
| 2268 | struct rb_node **p = &root->rb_node; |
| 2269 | struct rb_node *parent = NULL; |
| 2270 | struct sa_defrag_extent_backref *entry; |
| 2271 | int ret; |
| 2272 | |
| 2273 | while (*p) { |
| 2274 | parent = *p; |
| 2275 | entry = rb_entry(parent, struct sa_defrag_extent_backref, node); |
| 2276 | |
| 2277 | ret = backref_comp(backref, entry); |
| 2278 | if (ret < 0) |
| 2279 | p = &(*p)->rb_left; |
| 2280 | else |
| 2281 | p = &(*p)->rb_right; |
| 2282 | } |
| 2283 | |
| 2284 | rb_link_node(&backref->node, parent, p); |
| 2285 | rb_insert_color(&backref->node, root); |
| 2286 | } |
| 2287 | |
| 2288 | /* |
| 2289 | * Note the backref might has changed, and in this case we just return 0. |
| 2290 | */ |
| 2291 | static noinline int record_one_backref(u64 inum, u64 offset, u64 root_id, |
| 2292 | void *ctx) |
| 2293 | { |
| 2294 | struct btrfs_file_extent_item *extent; |
Liu Bo | 38c227d | 2013-01-29 03:18:40 +0000 | [diff] [blame] | 2295 | struct old_sa_defrag_extent *old = ctx; |
| 2296 | struct new_sa_defrag_extent *new = old->new; |
| 2297 | struct btrfs_path *path = new->path; |
| 2298 | struct btrfs_key key; |
| 2299 | struct btrfs_root *root; |
| 2300 | struct sa_defrag_extent_backref *backref; |
| 2301 | struct extent_buffer *leaf; |
| 2302 | struct inode *inode = new->inode; |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 2303 | struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb); |
Liu Bo | 38c227d | 2013-01-29 03:18:40 +0000 | [diff] [blame] | 2304 | int slot; |
| 2305 | int ret; |
| 2306 | u64 extent_offset; |
| 2307 | u64 num_bytes; |
| 2308 | |
| 2309 | if (BTRFS_I(inode)->root->root_key.objectid == root_id && |
Nikolay Borisov | 4a0cc7c | 2017-01-10 20:35:31 +0200 | [diff] [blame] | 2310 | inum == btrfs_ino(BTRFS_I(inode))) |
Liu Bo | 38c227d | 2013-01-29 03:18:40 +0000 | [diff] [blame] | 2311 | return 0; |
| 2312 | |
| 2313 | key.objectid = root_id; |
| 2314 | key.type = BTRFS_ROOT_ITEM_KEY; |
| 2315 | key.offset = (u64)-1; |
| 2316 | |
Liu Bo | 38c227d | 2013-01-29 03:18:40 +0000 | [diff] [blame] | 2317 | root = btrfs_read_fs_root_no_name(fs_info, &key); |
| 2318 | if (IS_ERR(root)) { |
| 2319 | if (PTR_ERR(root) == -ENOENT) |
| 2320 | return 0; |
| 2321 | WARN_ON(1); |
Jeff Mahoney | ab8d0fc | 2016-09-20 10:05:02 -0400 | [diff] [blame] | 2322 | btrfs_debug(fs_info, "inum=%llu, offset=%llu, root_id=%llu", |
Liu Bo | 38c227d | 2013-01-29 03:18:40 +0000 | [diff] [blame] | 2323 | inum, offset, root_id); |
| 2324 | return PTR_ERR(root); |
| 2325 | } |
| 2326 | |
| 2327 | key.objectid = inum; |
| 2328 | key.type = BTRFS_EXTENT_DATA_KEY; |
| 2329 | if (offset > (u64)-1 << 32) |
| 2330 | key.offset = 0; |
| 2331 | else |
| 2332 | key.offset = offset; |
| 2333 | |
| 2334 | ret = btrfs_search_slot(NULL, root, &key, path, 0, 0); |
Dulshani Gunawardhana | fae7f21 | 2013-10-31 10:30:08 +0530 | [diff] [blame] | 2335 | if (WARN_ON(ret < 0)) |
Liu Bo | 38c227d | 2013-01-29 03:18:40 +0000 | [diff] [blame] | 2336 | return ret; |
Josef Bacik | 50f1319 | 2013-07-22 12:50:37 -0400 | [diff] [blame] | 2337 | ret = 0; |
Liu Bo | 38c227d | 2013-01-29 03:18:40 +0000 | [diff] [blame] | 2338 | |
| 2339 | while (1) { |
| 2340 | cond_resched(); |
| 2341 | |
| 2342 | leaf = path->nodes[0]; |
| 2343 | slot = path->slots[0]; |
| 2344 | |
| 2345 | if (slot >= btrfs_header_nritems(leaf)) { |
| 2346 | ret = btrfs_next_leaf(root, path); |
| 2347 | if (ret < 0) { |
| 2348 | goto out; |
| 2349 | } else if (ret > 0) { |
| 2350 | ret = 0; |
| 2351 | goto out; |
| 2352 | } |
| 2353 | continue; |
| 2354 | } |
| 2355 | |
| 2356 | path->slots[0]++; |
| 2357 | |
| 2358 | btrfs_item_key_to_cpu(leaf, &key, slot); |
| 2359 | |
| 2360 | if (key.objectid > inum) |
| 2361 | goto out; |
| 2362 | |
| 2363 | if (key.objectid < inum || key.type != BTRFS_EXTENT_DATA_KEY) |
| 2364 | continue; |
| 2365 | |
| 2366 | extent = btrfs_item_ptr(leaf, slot, |
| 2367 | struct btrfs_file_extent_item); |
| 2368 | |
| 2369 | if (btrfs_file_extent_disk_bytenr(leaf, extent) != old->bytenr) |
| 2370 | continue; |
| 2371 | |
Liu Bo | e68afa4 | 2013-07-01 22:13:26 +0800 | [diff] [blame] | 2372 | /* |
| 2373 | * 'offset' refers to the exact key.offset, |
| 2374 | * NOT the 'offset' field in btrfs_extent_data_ref, ie. |
| 2375 | * (key.offset - extent_offset). |
| 2376 | */ |
| 2377 | if (key.offset != offset) |
Liu Bo | 38c227d | 2013-01-29 03:18:40 +0000 | [diff] [blame] | 2378 | continue; |
| 2379 | |
Liu Bo | e68afa4 | 2013-07-01 22:13:26 +0800 | [diff] [blame] | 2380 | extent_offset = btrfs_file_extent_offset(leaf, extent); |
Liu Bo | 38c227d | 2013-01-29 03:18:40 +0000 | [diff] [blame] | 2381 | num_bytes = btrfs_file_extent_num_bytes(leaf, extent); |
Liu Bo | e68afa4 | 2013-07-01 22:13:26 +0800 | [diff] [blame] | 2382 | |
Liu Bo | 38c227d | 2013-01-29 03:18:40 +0000 | [diff] [blame] | 2383 | if (extent_offset >= old->extent_offset + old->offset + |
| 2384 | old->len || extent_offset + num_bytes <= |
| 2385 | old->extent_offset + old->offset) |
| 2386 | continue; |
Liu Bo | 38c227d | 2013-01-29 03:18:40 +0000 | [diff] [blame] | 2387 | break; |
| 2388 | } |
| 2389 | |
| 2390 | backref = kmalloc(sizeof(*backref), GFP_NOFS); |
| 2391 | if (!backref) { |
| 2392 | ret = -ENOENT; |
| 2393 | goto out; |
| 2394 | } |
| 2395 | |
| 2396 | backref->root_id = root_id; |
| 2397 | backref->inum = inum; |
Liu Bo | e68afa4 | 2013-07-01 22:13:26 +0800 | [diff] [blame] | 2398 | backref->file_pos = offset; |
Liu Bo | 38c227d | 2013-01-29 03:18:40 +0000 | [diff] [blame] | 2399 | backref->num_bytes = num_bytes; |
| 2400 | backref->extent_offset = extent_offset; |
| 2401 | backref->generation = btrfs_file_extent_generation(leaf, extent); |
| 2402 | backref->old = old; |
| 2403 | backref_insert(&new->root, backref); |
| 2404 | old->count++; |
| 2405 | out: |
| 2406 | btrfs_release_path(path); |
| 2407 | WARN_ON(ret); |
| 2408 | return ret; |
| 2409 | } |
| 2410 | |
| 2411 | static noinline bool record_extent_backrefs(struct btrfs_path *path, |
| 2412 | struct new_sa_defrag_extent *new) |
| 2413 | { |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 2414 | struct btrfs_fs_info *fs_info = btrfs_sb(new->inode->i_sb); |
Liu Bo | 38c227d | 2013-01-29 03:18:40 +0000 | [diff] [blame] | 2415 | struct old_sa_defrag_extent *old, *tmp; |
| 2416 | int ret; |
| 2417 | |
| 2418 | new->path = path; |
| 2419 | |
| 2420 | list_for_each_entry_safe(old, tmp, &new->head, list) { |
Liu Bo | e68afa4 | 2013-07-01 22:13:26 +0800 | [diff] [blame] | 2421 | ret = iterate_inodes_from_logical(old->bytenr + |
| 2422 | old->extent_offset, fs_info, |
Liu Bo | 38c227d | 2013-01-29 03:18:40 +0000 | [diff] [blame] | 2423 | path, record_one_backref, |
| 2424 | old); |
Josef Bacik | 4724b10 | 2013-11-05 11:11:40 -0500 | [diff] [blame] | 2425 | if (ret < 0 && ret != -ENOENT) |
| 2426 | return false; |
Liu Bo | 38c227d | 2013-01-29 03:18:40 +0000 | [diff] [blame] | 2427 | |
| 2428 | /* no backref to be processed for this extent */ |
| 2429 | if (!old->count) { |
| 2430 | list_del(&old->list); |
| 2431 | kfree(old); |
| 2432 | } |
| 2433 | } |
| 2434 | |
| 2435 | if (list_empty(&new->head)) |
| 2436 | return false; |
| 2437 | |
| 2438 | return true; |
| 2439 | } |
| 2440 | |
| 2441 | static int relink_is_mergable(struct extent_buffer *leaf, |
| 2442 | struct btrfs_file_extent_item *fi, |
Liu Bo | 116e002 | 2013-08-02 16:30:40 +0800 | [diff] [blame] | 2443 | struct new_sa_defrag_extent *new) |
Liu Bo | 38c227d | 2013-01-29 03:18:40 +0000 | [diff] [blame] | 2444 | { |
Liu Bo | 116e002 | 2013-08-02 16:30:40 +0800 | [diff] [blame] | 2445 | if (btrfs_file_extent_disk_bytenr(leaf, fi) != new->bytenr) |
Liu Bo | 38c227d | 2013-01-29 03:18:40 +0000 | [diff] [blame] | 2446 | return 0; |
| 2447 | |
| 2448 | if (btrfs_file_extent_type(leaf, fi) != BTRFS_FILE_EXTENT_REG) |
| 2449 | return 0; |
| 2450 | |
Liu Bo | 116e002 | 2013-08-02 16:30:40 +0800 | [diff] [blame] | 2451 | if (btrfs_file_extent_compression(leaf, fi) != new->compress_type) |
| 2452 | return 0; |
| 2453 | |
| 2454 | if (btrfs_file_extent_encryption(leaf, fi) || |
Liu Bo | 38c227d | 2013-01-29 03:18:40 +0000 | [diff] [blame] | 2455 | btrfs_file_extent_other_encoding(leaf, fi)) |
| 2456 | return 0; |
| 2457 | |
| 2458 | return 1; |
| 2459 | } |
| 2460 | |
| 2461 | /* |
| 2462 | * Note the backref might has changed, and in this case we just return 0. |
| 2463 | */ |
| 2464 | static noinline int relink_extent_backref(struct btrfs_path *path, |
| 2465 | struct sa_defrag_extent_backref *prev, |
| 2466 | struct sa_defrag_extent_backref *backref) |
| 2467 | { |
| 2468 | struct btrfs_file_extent_item *extent; |
| 2469 | struct btrfs_file_extent_item *item; |
| 2470 | struct btrfs_ordered_extent *ordered; |
| 2471 | struct btrfs_trans_handle *trans; |
Liu Bo | 38c227d | 2013-01-29 03:18:40 +0000 | [diff] [blame] | 2472 | struct btrfs_root *root; |
| 2473 | struct btrfs_key key; |
| 2474 | struct extent_buffer *leaf; |
| 2475 | struct old_sa_defrag_extent *old = backref->old; |
| 2476 | struct new_sa_defrag_extent *new = old->new; |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 2477 | struct btrfs_fs_info *fs_info = btrfs_sb(new->inode->i_sb); |
Liu Bo | 38c227d | 2013-01-29 03:18:40 +0000 | [diff] [blame] | 2478 | struct inode *inode; |
| 2479 | struct extent_state *cached = NULL; |
| 2480 | int ret = 0; |
| 2481 | u64 start; |
| 2482 | u64 len; |
| 2483 | u64 lock_start; |
| 2484 | u64 lock_end; |
| 2485 | bool merge = false; |
| 2486 | int index; |
| 2487 | |
| 2488 | if (prev && prev->root_id == backref->root_id && |
| 2489 | prev->inum == backref->inum && |
| 2490 | prev->file_pos + prev->num_bytes == backref->file_pos) |
| 2491 | merge = true; |
| 2492 | |
| 2493 | /* step 1: get root */ |
| 2494 | key.objectid = backref->root_id; |
| 2495 | key.type = BTRFS_ROOT_ITEM_KEY; |
| 2496 | key.offset = (u64)-1; |
| 2497 | |
Liu Bo | 38c227d | 2013-01-29 03:18:40 +0000 | [diff] [blame] | 2498 | index = srcu_read_lock(&fs_info->subvol_srcu); |
| 2499 | |
| 2500 | root = btrfs_read_fs_root_no_name(fs_info, &key); |
| 2501 | if (IS_ERR(root)) { |
| 2502 | srcu_read_unlock(&fs_info->subvol_srcu, index); |
| 2503 | if (PTR_ERR(root) == -ENOENT) |
| 2504 | return 0; |
| 2505 | return PTR_ERR(root); |
| 2506 | } |
Liu Bo | 38c227d | 2013-01-29 03:18:40 +0000 | [diff] [blame] | 2507 | |
Wang Shilong | bcbba5e | 2014-02-08 23:46:35 +0800 | [diff] [blame] | 2508 | if (btrfs_root_readonly(root)) { |
| 2509 | srcu_read_unlock(&fs_info->subvol_srcu, index); |
| 2510 | return 0; |
| 2511 | } |
| 2512 | |
Liu Bo | 38c227d | 2013-01-29 03:18:40 +0000 | [diff] [blame] | 2513 | /* step 2: get inode */ |
| 2514 | key.objectid = backref->inum; |
| 2515 | key.type = BTRFS_INODE_ITEM_KEY; |
| 2516 | key.offset = 0; |
| 2517 | |
| 2518 | inode = btrfs_iget(fs_info->sb, &key, root, NULL); |
| 2519 | if (IS_ERR(inode)) { |
| 2520 | srcu_read_unlock(&fs_info->subvol_srcu, index); |
| 2521 | return 0; |
| 2522 | } |
| 2523 | |
| 2524 | srcu_read_unlock(&fs_info->subvol_srcu, index); |
| 2525 | |
| 2526 | /* step 3: relink backref */ |
| 2527 | lock_start = backref->file_pos; |
| 2528 | lock_end = backref->file_pos + backref->num_bytes - 1; |
| 2529 | lock_extent_bits(&BTRFS_I(inode)->io_tree, lock_start, lock_end, |
David Sterba | ff13db4 | 2015-12-03 14:30:40 +0100 | [diff] [blame] | 2530 | &cached); |
Liu Bo | 38c227d | 2013-01-29 03:18:40 +0000 | [diff] [blame] | 2531 | |
| 2532 | ordered = btrfs_lookup_first_ordered_extent(inode, lock_end); |
| 2533 | if (ordered) { |
| 2534 | btrfs_put_ordered_extent(ordered); |
| 2535 | goto out_unlock; |
| 2536 | } |
| 2537 | |
| 2538 | trans = btrfs_join_transaction(root); |
| 2539 | if (IS_ERR(trans)) { |
| 2540 | ret = PTR_ERR(trans); |
| 2541 | goto out_unlock; |
| 2542 | } |
| 2543 | |
| 2544 | key.objectid = backref->inum; |
| 2545 | key.type = BTRFS_EXTENT_DATA_KEY; |
| 2546 | key.offset = backref->file_pos; |
| 2547 | |
| 2548 | ret = btrfs_search_slot(NULL, root, &key, path, 0, 0); |
| 2549 | if (ret < 0) { |
| 2550 | goto out_free_path; |
| 2551 | } else if (ret > 0) { |
| 2552 | ret = 0; |
| 2553 | goto out_free_path; |
| 2554 | } |
| 2555 | |
| 2556 | extent = btrfs_item_ptr(path->nodes[0], path->slots[0], |
| 2557 | struct btrfs_file_extent_item); |
| 2558 | |
| 2559 | if (btrfs_file_extent_generation(path->nodes[0], extent) != |
| 2560 | backref->generation) |
| 2561 | goto out_free_path; |
| 2562 | |
| 2563 | btrfs_release_path(path); |
| 2564 | |
| 2565 | start = backref->file_pos; |
| 2566 | if (backref->extent_offset < old->extent_offset + old->offset) |
| 2567 | start += old->extent_offset + old->offset - |
| 2568 | backref->extent_offset; |
| 2569 | |
| 2570 | len = min(backref->extent_offset + backref->num_bytes, |
| 2571 | old->extent_offset + old->offset + old->len); |
| 2572 | len -= max(backref->extent_offset, old->extent_offset + old->offset); |
| 2573 | |
| 2574 | ret = btrfs_drop_extents(trans, root, inode, start, |
| 2575 | start + len, 1); |
| 2576 | if (ret) |
| 2577 | goto out_free_path; |
| 2578 | again: |
Nikolay Borisov | 4a0cc7c | 2017-01-10 20:35:31 +0200 | [diff] [blame] | 2579 | key.objectid = btrfs_ino(BTRFS_I(inode)); |
Liu Bo | 38c227d | 2013-01-29 03:18:40 +0000 | [diff] [blame] | 2580 | key.type = BTRFS_EXTENT_DATA_KEY; |
| 2581 | key.offset = start; |
| 2582 | |
Liu Bo | a09a0a7 | 2013-03-11 09:20:58 +0000 | [diff] [blame] | 2583 | path->leave_spinning = 1; |
Liu Bo | 38c227d | 2013-01-29 03:18:40 +0000 | [diff] [blame] | 2584 | if (merge) { |
| 2585 | struct btrfs_file_extent_item *fi; |
| 2586 | u64 extent_len; |
| 2587 | struct btrfs_key found_key; |
| 2588 | |
Gui Hecheng | 3c9665d | 2014-01-23 13:41:09 +0800 | [diff] [blame] | 2589 | ret = btrfs_search_slot(trans, root, &key, path, 0, 1); |
Liu Bo | 38c227d | 2013-01-29 03:18:40 +0000 | [diff] [blame] | 2590 | if (ret < 0) |
| 2591 | goto out_free_path; |
| 2592 | |
| 2593 | path->slots[0]--; |
| 2594 | leaf = path->nodes[0]; |
| 2595 | btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]); |
| 2596 | |
| 2597 | fi = btrfs_item_ptr(leaf, path->slots[0], |
| 2598 | struct btrfs_file_extent_item); |
| 2599 | extent_len = btrfs_file_extent_num_bytes(leaf, fi); |
| 2600 | |
Liu Bo | 116e002 | 2013-08-02 16:30:40 +0800 | [diff] [blame] | 2601 | if (extent_len + found_key.offset == start && |
| 2602 | relink_is_mergable(leaf, fi, new)) { |
Liu Bo | 38c227d | 2013-01-29 03:18:40 +0000 | [diff] [blame] | 2603 | btrfs_set_file_extent_num_bytes(leaf, fi, |
| 2604 | extent_len + len); |
| 2605 | btrfs_mark_buffer_dirty(leaf); |
| 2606 | inode_add_bytes(inode, len); |
| 2607 | |
| 2608 | ret = 1; |
| 2609 | goto out_free_path; |
| 2610 | } else { |
| 2611 | merge = false; |
| 2612 | btrfs_release_path(path); |
| 2613 | goto again; |
| 2614 | } |
| 2615 | } |
| 2616 | |
| 2617 | ret = btrfs_insert_empty_item(trans, root, path, &key, |
| 2618 | sizeof(*extent)); |
| 2619 | if (ret) { |
Jeff Mahoney | 6664283 | 2016-06-10 18:19:25 -0400 | [diff] [blame] | 2620 | btrfs_abort_transaction(trans, ret); |
Liu Bo | 38c227d | 2013-01-29 03:18:40 +0000 | [diff] [blame] | 2621 | goto out_free_path; |
| 2622 | } |
| 2623 | |
| 2624 | leaf = path->nodes[0]; |
| 2625 | item = btrfs_item_ptr(leaf, path->slots[0], |
| 2626 | struct btrfs_file_extent_item); |
| 2627 | btrfs_set_file_extent_disk_bytenr(leaf, item, new->bytenr); |
| 2628 | btrfs_set_file_extent_disk_num_bytes(leaf, item, new->disk_len); |
| 2629 | btrfs_set_file_extent_offset(leaf, item, start - new->file_pos); |
| 2630 | btrfs_set_file_extent_num_bytes(leaf, item, len); |
| 2631 | btrfs_set_file_extent_ram_bytes(leaf, item, new->len); |
| 2632 | btrfs_set_file_extent_generation(leaf, item, trans->transid); |
| 2633 | btrfs_set_file_extent_type(leaf, item, BTRFS_FILE_EXTENT_REG); |
| 2634 | btrfs_set_file_extent_compression(leaf, item, new->compress_type); |
| 2635 | btrfs_set_file_extent_encryption(leaf, item, 0); |
| 2636 | btrfs_set_file_extent_other_encoding(leaf, item, 0); |
| 2637 | |
| 2638 | btrfs_mark_buffer_dirty(leaf); |
| 2639 | inode_add_bytes(inode, len); |
Liu Bo | a09a0a7 | 2013-03-11 09:20:58 +0000 | [diff] [blame] | 2640 | btrfs_release_path(path); |
Liu Bo | 38c227d | 2013-01-29 03:18:40 +0000 | [diff] [blame] | 2641 | |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 2642 | ret = btrfs_inc_extent_ref(trans, fs_info, new->bytenr, |
Liu Bo | 38c227d | 2013-01-29 03:18:40 +0000 | [diff] [blame] | 2643 | new->disk_len, 0, |
| 2644 | backref->root_id, backref->inum, |
Filipe Manana | b06c4bf | 2015-10-23 07:52:54 +0100 | [diff] [blame] | 2645 | new->file_pos); /* start - extent_offset */ |
Liu Bo | 38c227d | 2013-01-29 03:18:40 +0000 | [diff] [blame] | 2646 | if (ret) { |
Jeff Mahoney | 6664283 | 2016-06-10 18:19:25 -0400 | [diff] [blame] | 2647 | btrfs_abort_transaction(trans, ret); |
Liu Bo | 38c227d | 2013-01-29 03:18:40 +0000 | [diff] [blame] | 2648 | goto out_free_path; |
| 2649 | } |
| 2650 | |
| 2651 | ret = 1; |
| 2652 | out_free_path: |
| 2653 | btrfs_release_path(path); |
Liu Bo | a09a0a7 | 2013-03-11 09:20:58 +0000 | [diff] [blame] | 2654 | path->leave_spinning = 0; |
Jeff Mahoney | 3a45bb2 | 2016-09-09 21:39:03 -0400 | [diff] [blame] | 2655 | btrfs_end_transaction(trans); |
Liu Bo | 38c227d | 2013-01-29 03:18:40 +0000 | [diff] [blame] | 2656 | out_unlock: |
| 2657 | unlock_extent_cached(&BTRFS_I(inode)->io_tree, lock_start, lock_end, |
| 2658 | &cached, GFP_NOFS); |
| 2659 | iput(inode); |
| 2660 | return ret; |
| 2661 | } |
| 2662 | |
Liu Bo | 6f51956 | 2013-10-29 10:45:05 +0800 | [diff] [blame] | 2663 | static void free_sa_defrag_extent(struct new_sa_defrag_extent *new) |
| 2664 | { |
| 2665 | struct old_sa_defrag_extent *old, *tmp; |
| 2666 | |
| 2667 | if (!new) |
| 2668 | return; |
| 2669 | |
| 2670 | list_for_each_entry_safe(old, tmp, &new->head, list) { |
Liu Bo | 6f51956 | 2013-10-29 10:45:05 +0800 | [diff] [blame] | 2671 | kfree(old); |
| 2672 | } |
| 2673 | kfree(new); |
| 2674 | } |
| 2675 | |
Liu Bo | 38c227d | 2013-01-29 03:18:40 +0000 | [diff] [blame] | 2676 | static void relink_file_extents(struct new_sa_defrag_extent *new) |
| 2677 | { |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 2678 | struct btrfs_fs_info *fs_info = btrfs_sb(new->inode->i_sb); |
Liu Bo | 38c227d | 2013-01-29 03:18:40 +0000 | [diff] [blame] | 2679 | struct btrfs_path *path; |
Liu Bo | 38c227d | 2013-01-29 03:18:40 +0000 | [diff] [blame] | 2680 | struct sa_defrag_extent_backref *backref; |
| 2681 | struct sa_defrag_extent_backref *prev = NULL; |
| 2682 | struct inode *inode; |
| 2683 | struct btrfs_root *root; |
| 2684 | struct rb_node *node; |
| 2685 | int ret; |
| 2686 | |
| 2687 | inode = new->inode; |
| 2688 | root = BTRFS_I(inode)->root; |
| 2689 | |
| 2690 | path = btrfs_alloc_path(); |
| 2691 | if (!path) |
| 2692 | return; |
| 2693 | |
| 2694 | if (!record_extent_backrefs(path, new)) { |
| 2695 | btrfs_free_path(path); |
| 2696 | goto out; |
| 2697 | } |
| 2698 | btrfs_release_path(path); |
| 2699 | |
| 2700 | while (1) { |
| 2701 | node = rb_first(&new->root); |
| 2702 | if (!node) |
| 2703 | break; |
| 2704 | rb_erase(node, &new->root); |
| 2705 | |
| 2706 | backref = rb_entry(node, struct sa_defrag_extent_backref, node); |
| 2707 | |
| 2708 | ret = relink_extent_backref(path, prev, backref); |
| 2709 | WARN_ON(ret < 0); |
| 2710 | |
| 2711 | kfree(prev); |
| 2712 | |
| 2713 | if (ret == 1) |
| 2714 | prev = backref; |
| 2715 | else |
| 2716 | prev = NULL; |
| 2717 | cond_resched(); |
| 2718 | } |
| 2719 | kfree(prev); |
| 2720 | |
| 2721 | btrfs_free_path(path); |
Liu Bo | 38c227d | 2013-01-29 03:18:40 +0000 | [diff] [blame] | 2722 | out: |
Liu Bo | 6f51956 | 2013-10-29 10:45:05 +0800 | [diff] [blame] | 2723 | free_sa_defrag_extent(new); |
| 2724 | |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 2725 | atomic_dec(&fs_info->defrag_running); |
| 2726 | wake_up(&fs_info->transaction_wait); |
Liu Bo | 38c227d | 2013-01-29 03:18:40 +0000 | [diff] [blame] | 2727 | } |
| 2728 | |
| 2729 | static struct new_sa_defrag_extent * |
| 2730 | record_old_file_extents(struct inode *inode, |
| 2731 | struct btrfs_ordered_extent *ordered) |
| 2732 | { |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 2733 | struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb); |
Liu Bo | 38c227d | 2013-01-29 03:18:40 +0000 | [diff] [blame] | 2734 | struct btrfs_root *root = BTRFS_I(inode)->root; |
| 2735 | struct btrfs_path *path; |
| 2736 | struct btrfs_key key; |
Liu Bo | 6f51956 | 2013-10-29 10:45:05 +0800 | [diff] [blame] | 2737 | struct old_sa_defrag_extent *old; |
Liu Bo | 38c227d | 2013-01-29 03:18:40 +0000 | [diff] [blame] | 2738 | struct new_sa_defrag_extent *new; |
| 2739 | int ret; |
| 2740 | |
| 2741 | new = kmalloc(sizeof(*new), GFP_NOFS); |
| 2742 | if (!new) |
| 2743 | return NULL; |
| 2744 | |
| 2745 | new->inode = inode; |
| 2746 | new->file_pos = ordered->file_offset; |
| 2747 | new->len = ordered->len; |
| 2748 | new->bytenr = ordered->start; |
| 2749 | new->disk_len = ordered->disk_len; |
| 2750 | new->compress_type = ordered->compress_type; |
| 2751 | new->root = RB_ROOT; |
| 2752 | INIT_LIST_HEAD(&new->head); |
| 2753 | |
| 2754 | path = btrfs_alloc_path(); |
| 2755 | if (!path) |
| 2756 | goto out_kfree; |
| 2757 | |
Nikolay Borisov | 4a0cc7c | 2017-01-10 20:35:31 +0200 | [diff] [blame] | 2758 | key.objectid = btrfs_ino(BTRFS_I(inode)); |
Liu Bo | 38c227d | 2013-01-29 03:18:40 +0000 | [diff] [blame] | 2759 | key.type = BTRFS_EXTENT_DATA_KEY; |
| 2760 | key.offset = new->file_pos; |
| 2761 | |
| 2762 | ret = btrfs_search_slot(NULL, root, &key, path, 0, 0); |
| 2763 | if (ret < 0) |
| 2764 | goto out_free_path; |
| 2765 | if (ret > 0 && path->slots[0] > 0) |
| 2766 | path->slots[0]--; |
| 2767 | |
| 2768 | /* find out all the old extents for the file range */ |
| 2769 | while (1) { |
| 2770 | struct btrfs_file_extent_item *extent; |
| 2771 | struct extent_buffer *l; |
| 2772 | int slot; |
| 2773 | u64 num_bytes; |
| 2774 | u64 offset; |
| 2775 | u64 end; |
| 2776 | u64 disk_bytenr; |
| 2777 | u64 extent_offset; |
| 2778 | |
| 2779 | l = path->nodes[0]; |
| 2780 | slot = path->slots[0]; |
| 2781 | |
| 2782 | if (slot >= btrfs_header_nritems(l)) { |
| 2783 | ret = btrfs_next_leaf(root, path); |
| 2784 | if (ret < 0) |
Liu Bo | 6f51956 | 2013-10-29 10:45:05 +0800 | [diff] [blame] | 2785 | goto out_free_path; |
Liu Bo | 38c227d | 2013-01-29 03:18:40 +0000 | [diff] [blame] | 2786 | else if (ret > 0) |
| 2787 | break; |
| 2788 | continue; |
| 2789 | } |
| 2790 | |
| 2791 | btrfs_item_key_to_cpu(l, &key, slot); |
| 2792 | |
Nikolay Borisov | 4a0cc7c | 2017-01-10 20:35:31 +0200 | [diff] [blame] | 2793 | if (key.objectid != btrfs_ino(BTRFS_I(inode))) |
Liu Bo | 38c227d | 2013-01-29 03:18:40 +0000 | [diff] [blame] | 2794 | break; |
| 2795 | if (key.type != BTRFS_EXTENT_DATA_KEY) |
| 2796 | break; |
| 2797 | if (key.offset >= new->file_pos + new->len) |
| 2798 | break; |
| 2799 | |
| 2800 | extent = btrfs_item_ptr(l, slot, struct btrfs_file_extent_item); |
| 2801 | |
| 2802 | num_bytes = btrfs_file_extent_num_bytes(l, extent); |
| 2803 | if (key.offset + num_bytes < new->file_pos) |
| 2804 | goto next; |
| 2805 | |
| 2806 | disk_bytenr = btrfs_file_extent_disk_bytenr(l, extent); |
| 2807 | if (!disk_bytenr) |
| 2808 | goto next; |
| 2809 | |
| 2810 | extent_offset = btrfs_file_extent_offset(l, extent); |
| 2811 | |
| 2812 | old = kmalloc(sizeof(*old), GFP_NOFS); |
| 2813 | if (!old) |
Liu Bo | 6f51956 | 2013-10-29 10:45:05 +0800 | [diff] [blame] | 2814 | goto out_free_path; |
Liu Bo | 38c227d | 2013-01-29 03:18:40 +0000 | [diff] [blame] | 2815 | |
| 2816 | offset = max(new->file_pos, key.offset); |
| 2817 | end = min(new->file_pos + new->len, key.offset + num_bytes); |
| 2818 | |
| 2819 | old->bytenr = disk_bytenr; |
| 2820 | old->extent_offset = extent_offset; |
| 2821 | old->offset = offset - key.offset; |
| 2822 | old->len = end - offset; |
| 2823 | old->new = new; |
| 2824 | old->count = 0; |
| 2825 | list_add_tail(&old->list, &new->head); |
| 2826 | next: |
| 2827 | path->slots[0]++; |
| 2828 | cond_resched(); |
| 2829 | } |
| 2830 | |
| 2831 | btrfs_free_path(path); |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 2832 | atomic_inc(&fs_info->defrag_running); |
Liu Bo | 38c227d | 2013-01-29 03:18:40 +0000 | [diff] [blame] | 2833 | |
| 2834 | return new; |
| 2835 | |
Liu Bo | 38c227d | 2013-01-29 03:18:40 +0000 | [diff] [blame] | 2836 | out_free_path: |
| 2837 | btrfs_free_path(path); |
| 2838 | out_kfree: |
Liu Bo | 6f51956 | 2013-10-29 10:45:05 +0800 | [diff] [blame] | 2839 | free_sa_defrag_extent(new); |
Liu Bo | 38c227d | 2013-01-29 03:18:40 +0000 | [diff] [blame] | 2840 | return NULL; |
| 2841 | } |
| 2842 | |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 2843 | static void btrfs_release_delalloc_bytes(struct btrfs_fs_info *fs_info, |
Miao Xie | e570fd2 | 2014-06-19 10:42:50 +0800 | [diff] [blame] | 2844 | u64 start, u64 len) |
| 2845 | { |
| 2846 | struct btrfs_block_group_cache *cache; |
| 2847 | |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 2848 | cache = btrfs_lookup_block_group(fs_info, start); |
Miao Xie | e570fd2 | 2014-06-19 10:42:50 +0800 | [diff] [blame] | 2849 | ASSERT(cache); |
| 2850 | |
| 2851 | spin_lock(&cache->lock); |
| 2852 | cache->delalloc_bytes -= len; |
| 2853 | spin_unlock(&cache->lock); |
| 2854 | |
| 2855 | btrfs_put_block_group(cache); |
| 2856 | } |
| 2857 | |
Chris Mason | d352ac6 | 2008-09-29 15:18:18 -0400 | [diff] [blame] | 2858 | /* as ordered data IO finishes, this gets called so we can finish |
| 2859 | * an ordered extent if the range of bytes in the file it covers are |
| 2860 | * fully written. |
| 2861 | */ |
Josef Bacik | 5fd0204 | 2012-05-02 14:00:54 -0400 | [diff] [blame] | 2862 | static int btrfs_finish_ordered_io(struct btrfs_ordered_extent *ordered_extent) |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 2863 | { |
Josef Bacik | 5fd0204 | 2012-05-02 14:00:54 -0400 | [diff] [blame] | 2864 | struct inode *inode = ordered_extent->inode; |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 2865 | struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb); |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 2866 | struct btrfs_root *root = BTRFS_I(inode)->root; |
Yan, Zheng | 0ca1f7c | 2010-05-16 10:48:47 -0400 | [diff] [blame] | 2867 | struct btrfs_trans_handle *trans = NULL; |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 2868 | struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree; |
Josef Bacik | 2ac55d4 | 2010-02-03 19:33:23 +0000 | [diff] [blame] | 2869 | struct extent_state *cached_state = NULL; |
Liu Bo | 38c227d | 2013-01-29 03:18:40 +0000 | [diff] [blame] | 2870 | struct new_sa_defrag_extent *new = NULL; |
Li Zefan | 261507a0 | 2010-12-17 14:21:50 +0800 | [diff] [blame] | 2871 | int compress_type = 0; |
Josef Bacik | 77cef2e | 2013-08-29 13:57:21 -0400 | [diff] [blame] | 2872 | int ret = 0; |
| 2873 | u64 logical_len = ordered_extent->len; |
Li Zefan | 82d5902 | 2011-04-20 10:33:24 +0800 | [diff] [blame] | 2874 | bool nolock; |
Josef Bacik | 77cef2e | 2013-08-29 13:57:21 -0400 | [diff] [blame] | 2875 | bool truncated = false; |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 2876 | |
Liu Bo | 83eea1f | 2012-07-10 05:28:39 -0600 | [diff] [blame] | 2877 | nolock = btrfs_is_free_space_inode(inode); |
Josef Bacik | 0cb59c9 | 2010-07-02 12:14:14 -0400 | [diff] [blame] | 2878 | |
Josef Bacik | 5fd0204 | 2012-05-02 14:00:54 -0400 | [diff] [blame] | 2879 | if (test_bit(BTRFS_ORDERED_IOERR, &ordered_extent->flags)) { |
| 2880 | ret = -EIO; |
| 2881 | goto out; |
| 2882 | } |
| 2883 | |
Miao Xie | f612496 | 2014-09-12 18:44:04 +0800 | [diff] [blame] | 2884 | btrfs_free_io_failure_record(inode, ordered_extent->file_offset, |
| 2885 | ordered_extent->file_offset + |
| 2886 | ordered_extent->len - 1); |
| 2887 | |
Josef Bacik | 77cef2e | 2013-08-29 13:57:21 -0400 | [diff] [blame] | 2888 | if (test_bit(BTRFS_ORDERED_TRUNCATED, &ordered_extent->flags)) { |
| 2889 | truncated = true; |
| 2890 | logical_len = ordered_extent->truncated_len; |
| 2891 | /* Truncated the entire extent, don't bother adding */ |
| 2892 | if (!logical_len) |
| 2893 | goto out; |
| 2894 | } |
| 2895 | |
Yan, Zheng | c216775 | 2009-11-12 09:34:21 +0000 | [diff] [blame] | 2896 | if (test_bit(BTRFS_ORDERED_NOCOW, &ordered_extent->flags)) { |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 2897 | BUG_ON(!list_empty(&ordered_extent->list)); /* Logic error */ |
Qu Wenruo | 94ed938 | 2015-09-08 17:25:56 +0800 | [diff] [blame] | 2898 | |
| 2899 | /* |
| 2900 | * For mwrite(mmap + memset to write) case, we still reserve |
| 2901 | * space for NOCOW range. |
| 2902 | * As NOCOW won't cause a new delayed ref, just free the space |
| 2903 | */ |
| 2904 | btrfs_qgroup_free_data(inode, ordered_extent->file_offset, |
| 2905 | ordered_extent->len); |
Josef Bacik | 6c760c0 | 2012-11-09 10:53:21 -0500 | [diff] [blame] | 2906 | btrfs_ordered_update_i_size(inode, 0, ordered_extent); |
| 2907 | if (nolock) |
| 2908 | trans = btrfs_join_transaction_nolock(root); |
| 2909 | else |
| 2910 | trans = btrfs_join_transaction(root); |
| 2911 | if (IS_ERR(trans)) { |
| 2912 | ret = PTR_ERR(trans); |
| 2913 | trans = NULL; |
| 2914 | goto out; |
Yan, Zheng | c216775 | 2009-11-12 09:34:21 +0000 | [diff] [blame] | 2915 | } |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 2916 | trans->block_rsv = &fs_info->delalloc_block_rsv; |
Josef Bacik | 6c760c0 | 2012-11-09 10:53:21 -0500 | [diff] [blame] | 2917 | ret = btrfs_update_inode_fallback(trans, root, inode); |
| 2918 | if (ret) /* -ENOMEM or corruption */ |
Jeff Mahoney | 6664283 | 2016-06-10 18:19:25 -0400 | [diff] [blame] | 2919 | btrfs_abort_transaction(trans, ret); |
Yan, Zheng | c216775 | 2009-11-12 09:34:21 +0000 | [diff] [blame] | 2920 | goto out; |
| 2921 | } |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 2922 | |
Josef Bacik | 2ac55d4 | 2010-02-03 19:33:23 +0000 | [diff] [blame] | 2923 | lock_extent_bits(io_tree, ordered_extent->file_offset, |
| 2924 | ordered_extent->file_offset + ordered_extent->len - 1, |
David Sterba | ff13db4 | 2015-12-03 14:30:40 +0100 | [diff] [blame] | 2925 | &cached_state); |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 2926 | |
Liu Bo | 38c227d | 2013-01-29 03:18:40 +0000 | [diff] [blame] | 2927 | ret = test_range_bit(io_tree, ordered_extent->file_offset, |
| 2928 | ordered_extent->file_offset + ordered_extent->len - 1, |
| 2929 | EXTENT_DEFRAG, 1, cached_state); |
| 2930 | if (ret) { |
| 2931 | u64 last_snapshot = btrfs_root_last_snapshot(&root->root_item); |
Josef Bacik | 8101c8d | 2014-01-29 16:05:30 -0500 | [diff] [blame] | 2932 | if (0 && last_snapshot >= BTRFS_I(inode)->generation) |
Liu Bo | 38c227d | 2013-01-29 03:18:40 +0000 | [diff] [blame] | 2933 | /* the inode is shared */ |
| 2934 | new = record_old_file_extents(inode, ordered_extent); |
| 2935 | |
| 2936 | clear_extent_bit(io_tree, ordered_extent->file_offset, |
| 2937 | ordered_extent->file_offset + ordered_extent->len - 1, |
| 2938 | EXTENT_DEFRAG, 0, 0, &cached_state, GFP_NOFS); |
| 2939 | } |
| 2940 | |
Josef Bacik | 0cb59c9 | 2010-07-02 12:14:14 -0400 | [diff] [blame] | 2941 | if (nolock) |
Josef Bacik | 7a7eaa4 | 2011-04-13 12:54:33 -0400 | [diff] [blame] | 2942 | trans = btrfs_join_transaction_nolock(root); |
Josef Bacik | 0cb59c9 | 2010-07-02 12:14:14 -0400 | [diff] [blame] | 2943 | else |
Josef Bacik | 7a7eaa4 | 2011-04-13 12:54:33 -0400 | [diff] [blame] | 2944 | trans = btrfs_join_transaction(root); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 2945 | if (IS_ERR(trans)) { |
| 2946 | ret = PTR_ERR(trans); |
| 2947 | trans = NULL; |
| 2948 | goto out_unlock; |
| 2949 | } |
Chris Mason | a79b7d4 | 2014-05-22 16:18:52 -0700 | [diff] [blame] | 2950 | |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 2951 | trans->block_rsv = &fs_info->delalloc_block_rsv; |
Yan, Zheng | c216775 | 2009-11-12 09:34:21 +0000 | [diff] [blame] | 2952 | |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 2953 | if (test_bit(BTRFS_ORDERED_COMPRESSED, &ordered_extent->flags)) |
Li Zefan | 261507a0 | 2010-12-17 14:21:50 +0800 | [diff] [blame] | 2954 | compress_type = ordered_extent->compress_type; |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 2955 | if (test_bit(BTRFS_ORDERED_PREALLOC, &ordered_extent->flags)) { |
Li Zefan | 261507a0 | 2010-12-17 14:21:50 +0800 | [diff] [blame] | 2956 | BUG_ON(compress_type); |
Yan, Zheng | 920bbbf | 2009-11-12 09:34:08 +0000 | [diff] [blame] | 2957 | ret = btrfs_mark_extent_written(trans, inode, |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 2958 | ordered_extent->file_offset, |
| 2959 | ordered_extent->file_offset + |
Josef Bacik | 77cef2e | 2013-08-29 13:57:21 -0400 | [diff] [blame] | 2960 | logical_len); |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 2961 | } else { |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 2962 | BUG_ON(root == fs_info->tree_root); |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 2963 | ret = insert_reserved_file_extent(trans, inode, |
| 2964 | ordered_extent->file_offset, |
| 2965 | ordered_extent->start, |
| 2966 | ordered_extent->disk_len, |
Josef Bacik | 77cef2e | 2013-08-29 13:57:21 -0400 | [diff] [blame] | 2967 | logical_len, logical_len, |
Li Zefan | 261507a0 | 2010-12-17 14:21:50 +0800 | [diff] [blame] | 2968 | compress_type, 0, 0, |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 2969 | BTRFS_FILE_EXTENT_REG); |
Miao Xie | e570fd2 | 2014-06-19 10:42:50 +0800 | [diff] [blame] | 2970 | if (!ret) |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 2971 | btrfs_release_delalloc_bytes(fs_info, |
Miao Xie | e570fd2 | 2014-06-19 10:42:50 +0800 | [diff] [blame] | 2972 | ordered_extent->start, |
| 2973 | ordered_extent->disk_len); |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 2974 | } |
Josef Bacik | 5dc562c | 2012-08-17 13:14:17 -0400 | [diff] [blame] | 2975 | unpin_extent_cache(&BTRFS_I(inode)->extent_tree, |
| 2976 | ordered_extent->file_offset, ordered_extent->len, |
| 2977 | trans->transid); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 2978 | if (ret < 0) { |
Jeff Mahoney | 6664283 | 2016-06-10 18:19:25 -0400 | [diff] [blame] | 2979 | btrfs_abort_transaction(trans, ret); |
Josef Bacik | 5fd0204 | 2012-05-02 14:00:54 -0400 | [diff] [blame] | 2980 | goto out_unlock; |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 2981 | } |
Josef Bacik | 2ac55d4 | 2010-02-03 19:33:23 +0000 | [diff] [blame] | 2982 | |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 2983 | add_pending_csums(trans, inode, ordered_extent->file_offset, |
| 2984 | &ordered_extent->list); |
| 2985 | |
Josef Bacik | 6c760c0 | 2012-11-09 10:53:21 -0500 | [diff] [blame] | 2986 | btrfs_ordered_update_i_size(inode, 0, ordered_extent); |
| 2987 | ret = btrfs_update_inode_fallback(trans, root, inode); |
| 2988 | if (ret) { /* -ENOMEM or corruption */ |
Jeff Mahoney | 6664283 | 2016-06-10 18:19:25 -0400 | [diff] [blame] | 2989 | btrfs_abort_transaction(trans, ret); |
Josef Bacik | 6c760c0 | 2012-11-09 10:53:21 -0500 | [diff] [blame] | 2990 | goto out_unlock; |
Josef Bacik | 1ef30be | 2011-04-05 19:25:36 -0400 | [diff] [blame] | 2991 | } |
| 2992 | ret = 0; |
Josef Bacik | 5fd0204 | 2012-05-02 14:00:54 -0400 | [diff] [blame] | 2993 | out_unlock: |
| 2994 | unlock_extent_cached(io_tree, ordered_extent->file_offset, |
| 2995 | ordered_extent->file_offset + |
| 2996 | ordered_extent->len - 1, &cached_state, GFP_NOFS); |
Yan, Zheng | c216775 | 2009-11-12 09:34:21 +0000 | [diff] [blame] | 2997 | out: |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 2998 | if (root != fs_info->tree_root) |
Josef Bacik | 0cb59c9 | 2010-07-02 12:14:14 -0400 | [diff] [blame] | 2999 | btrfs_delalloc_release_metadata(inode, ordered_extent->len); |
Miao Xie | a698d075 | 2012-09-20 01:51:59 -0600 | [diff] [blame] | 3000 | if (trans) |
Jeff Mahoney | 3a45bb2 | 2016-09-09 21:39:03 -0400 | [diff] [blame] | 3001 | btrfs_end_transaction(trans); |
Josef Bacik | 0cb59c9 | 2010-07-02 12:14:14 -0400 | [diff] [blame] | 3002 | |
Josef Bacik | 77cef2e | 2013-08-29 13:57:21 -0400 | [diff] [blame] | 3003 | if (ret || truncated) { |
| 3004 | u64 start, end; |
| 3005 | |
| 3006 | if (truncated) |
| 3007 | start = ordered_extent->file_offset + logical_len; |
| 3008 | else |
| 3009 | start = ordered_extent->file_offset; |
| 3010 | end = ordered_extent->file_offset + ordered_extent->len - 1; |
| 3011 | clear_extent_uptodate(io_tree, start, end, NULL, GFP_NOFS); |
| 3012 | |
| 3013 | /* Drop the cache for the part of the extent we didn't write. */ |
| 3014 | btrfs_drop_extent_cache(inode, start, end, 0); |
Josef Bacik | 5fd0204 | 2012-05-02 14:00:54 -0400 | [diff] [blame] | 3015 | |
Josef Bacik | 0bec9ef | 2013-01-31 14:58:00 -0500 | [diff] [blame] | 3016 | /* |
| 3017 | * If the ordered extent had an IOERR or something else went |
| 3018 | * wrong we need to return the space for this ordered extent |
Josef Bacik | 77cef2e | 2013-08-29 13:57:21 -0400 | [diff] [blame] | 3019 | * back to the allocator. We only free the extent in the |
| 3020 | * truncated case if we didn't write out the extent at all. |
Josef Bacik | 0bec9ef | 2013-01-31 14:58:00 -0500 | [diff] [blame] | 3021 | */ |
Josef Bacik | 77cef2e | 2013-08-29 13:57:21 -0400 | [diff] [blame] | 3022 | if ((ret || !logical_len) && |
| 3023 | !test_bit(BTRFS_ORDERED_NOCOW, &ordered_extent->flags) && |
Josef Bacik | 0bec9ef | 2013-01-31 14:58:00 -0500 | [diff] [blame] | 3024 | !test_bit(BTRFS_ORDERED_PREALLOC, &ordered_extent->flags)) |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 3025 | btrfs_free_reserved_extent(fs_info, |
| 3026 | ordered_extent->start, |
Miao Xie | e570fd2 | 2014-06-19 10:42:50 +0800 | [diff] [blame] | 3027 | ordered_extent->disk_len, 1); |
Josef Bacik | 0bec9ef | 2013-01-31 14:58:00 -0500 | [diff] [blame] | 3028 | } |
| 3029 | |
| 3030 | |
Josef Bacik | 5fd0204 | 2012-05-02 14:00:54 -0400 | [diff] [blame] | 3031 | /* |
Liu Bo | 8bad3c0 | 2012-06-18 12:14:23 +0800 | [diff] [blame] | 3032 | * This needs to be done to make sure anybody waiting knows we are done |
| 3033 | * updating everything for this ordered extent. |
Josef Bacik | 5fd0204 | 2012-05-02 14:00:54 -0400 | [diff] [blame] | 3034 | */ |
| 3035 | btrfs_remove_ordered_extent(inode, ordered_extent); |
| 3036 | |
Liu Bo | 38c227d | 2013-01-29 03:18:40 +0000 | [diff] [blame] | 3037 | /* for snapshot-aware defrag */ |
Liu Bo | 6f51956 | 2013-10-29 10:45:05 +0800 | [diff] [blame] | 3038 | if (new) { |
| 3039 | if (ret) { |
| 3040 | free_sa_defrag_extent(new); |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 3041 | atomic_dec(&fs_info->defrag_running); |
Liu Bo | 6f51956 | 2013-10-29 10:45:05 +0800 | [diff] [blame] | 3042 | } else { |
| 3043 | relink_file_extents(new); |
| 3044 | } |
| 3045 | } |
Liu Bo | 38c227d | 2013-01-29 03:18:40 +0000 | [diff] [blame] | 3046 | |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 3047 | /* once for us */ |
| 3048 | btrfs_put_ordered_extent(ordered_extent); |
| 3049 | /* once for the tree */ |
| 3050 | btrfs_put_ordered_extent(ordered_extent); |
| 3051 | |
Josef Bacik | 5fd0204 | 2012-05-02 14:00:54 -0400 | [diff] [blame] | 3052 | return ret; |
| 3053 | } |
| 3054 | |
| 3055 | static void finish_ordered_fn(struct btrfs_work *work) |
| 3056 | { |
| 3057 | struct btrfs_ordered_extent *ordered_extent; |
| 3058 | ordered_extent = container_of(work, struct btrfs_ordered_extent, work); |
| 3059 | btrfs_finish_ordered_io(ordered_extent); |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 3060 | } |
| 3061 | |
Christoph Hellwig | b295086 | 2008-12-02 09:54:17 -0500 | [diff] [blame] | 3062 | static int btrfs_writepage_end_io_hook(struct page *page, u64 start, u64 end, |
Chris Mason | 211f90e | 2008-07-18 11:56:15 -0400 | [diff] [blame] | 3063 | struct extent_state *state, int uptodate) |
| 3064 | { |
Josef Bacik | 5fd0204 | 2012-05-02 14:00:54 -0400 | [diff] [blame] | 3065 | struct inode *inode = page->mapping->host; |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 3066 | struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb); |
Josef Bacik | 5fd0204 | 2012-05-02 14:00:54 -0400 | [diff] [blame] | 3067 | struct btrfs_ordered_extent *ordered_extent = NULL; |
Liu Bo | 9e0af23 | 2014-08-15 23:36:53 +0800 | [diff] [blame] | 3068 | struct btrfs_workqueue *wq; |
| 3069 | btrfs_work_func_t func; |
Josef Bacik | 5fd0204 | 2012-05-02 14:00:54 -0400 | [diff] [blame] | 3070 | |
liubo | 1abe9b8 | 2011-03-24 11:18:59 +0000 | [diff] [blame] | 3071 | trace_btrfs_writepage_end_io_hook(page, start, end, uptodate); |
| 3072 | |
Chris Mason | 8b62b72 | 2009-09-02 16:53:46 -0400 | [diff] [blame] | 3073 | ClearPagePrivate2(page); |
Josef Bacik | 5fd0204 | 2012-05-02 14:00:54 -0400 | [diff] [blame] | 3074 | if (!btrfs_dec_test_ordered_pending(inode, &ordered_extent, start, |
| 3075 | end - start + 1, uptodate)) |
| 3076 | return 0; |
| 3077 | |
Liu Bo | 9e0af23 | 2014-08-15 23:36:53 +0800 | [diff] [blame] | 3078 | if (btrfs_is_free_space_inode(inode)) { |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 3079 | wq = fs_info->endio_freespace_worker; |
Liu Bo | 9e0af23 | 2014-08-15 23:36:53 +0800 | [diff] [blame] | 3080 | func = btrfs_freespace_write_helper; |
| 3081 | } else { |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 3082 | wq = fs_info->endio_write_workers; |
Liu Bo | 9e0af23 | 2014-08-15 23:36:53 +0800 | [diff] [blame] | 3083 | func = btrfs_endio_write_helper; |
| 3084 | } |
Josef Bacik | 5fd0204 | 2012-05-02 14:00:54 -0400 | [diff] [blame] | 3085 | |
Liu Bo | 9e0af23 | 2014-08-15 23:36:53 +0800 | [diff] [blame] | 3086 | btrfs_init_work(&ordered_extent->work, func, finish_ordered_fn, NULL, |
| 3087 | NULL); |
| 3088 | btrfs_queue_work(wq, &ordered_extent->work); |
Josef Bacik | 5fd0204 | 2012-05-02 14:00:54 -0400 | [diff] [blame] | 3089 | |
| 3090 | return 0; |
Chris Mason | 211f90e | 2008-07-18 11:56:15 -0400 | [diff] [blame] | 3091 | } |
| 3092 | |
Miao Xie | dc380ae | 2014-09-12 18:43:55 +0800 | [diff] [blame] | 3093 | static int __readpage_endio_check(struct inode *inode, |
| 3094 | struct btrfs_io_bio *io_bio, |
| 3095 | int icsum, struct page *page, |
| 3096 | int pgoff, u64 start, size_t len) |
| 3097 | { |
| 3098 | char *kaddr; |
| 3099 | u32 csum_expected; |
| 3100 | u32 csum = ~(u32)0; |
Miao Xie | dc380ae | 2014-09-12 18:43:55 +0800 | [diff] [blame] | 3101 | |
| 3102 | csum_expected = *(((u32 *)io_bio->csum) + icsum); |
| 3103 | |
| 3104 | kaddr = kmap_atomic(page); |
| 3105 | csum = btrfs_csum_data(kaddr + pgoff, csum, len); |
Domagoj Tršan | 0b5e3da | 2016-10-27 08:52:33 +0100 | [diff] [blame] | 3106 | btrfs_csum_final(csum, (u8 *)&csum); |
Miao Xie | dc380ae | 2014-09-12 18:43:55 +0800 | [diff] [blame] | 3107 | if (csum != csum_expected) |
| 3108 | goto zeroit; |
| 3109 | |
| 3110 | kunmap_atomic(kaddr); |
| 3111 | return 0; |
| 3112 | zeroit: |
David Sterba | 9464732 | 2015-10-08 11:01:36 +0200 | [diff] [blame] | 3113 | btrfs_warn_rl(BTRFS_I(inode)->root->fs_info, |
| 3114 | "csum failed ino %llu off %llu csum %u expected csum %u", |
David Sterba | f85b737 | 2017-01-20 14:54:07 +0100 | [diff] [blame] | 3115 | btrfs_ino(BTRFS_I(inode)), start, csum, csum_expected); |
Miao Xie | dc380ae | 2014-09-12 18:43:55 +0800 | [diff] [blame] | 3116 | memset(kaddr + pgoff, 1, len); |
| 3117 | flush_dcache_page(page); |
| 3118 | kunmap_atomic(kaddr); |
| 3119 | if (csum_expected == 0) |
| 3120 | return 0; |
| 3121 | return -EIO; |
| 3122 | } |
| 3123 | |
Chris Mason | d352ac6 | 2008-09-29 15:18:18 -0400 | [diff] [blame] | 3124 | /* |
Chris Mason | d352ac6 | 2008-09-29 15:18:18 -0400 | [diff] [blame] | 3125 | * 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] | 3126 | * if there's a match, we allow the bio to finish. If not, the code in |
| 3127 | * extent_io.c will try to find good copies for us. |
Chris Mason | d352ac6 | 2008-09-29 15:18:18 -0400 | [diff] [blame] | 3128 | */ |
Miao Xie | facc8a22 | 2013-07-25 19:22:34 +0800 | [diff] [blame] | 3129 | static int btrfs_readpage_end_io_hook(struct btrfs_io_bio *io_bio, |
| 3130 | u64 phy_offset, struct page *page, |
| 3131 | u64 start, u64 end, int mirror) |
Chris Mason | 07157aa | 2007-08-30 08:50:51 -0400 | [diff] [blame] | 3132 | { |
Miao Xie | 4eee4fa | 2012-12-21 09:17:45 +0000 | [diff] [blame] | 3133 | size_t offset = start - page_offset(page); |
Chris Mason | 07157aa | 2007-08-30 08:50:51 -0400 | [diff] [blame] | 3134 | struct inode *inode = page->mapping->host; |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 3135 | struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree; |
Chris Mason | ff79f81 | 2007-10-15 16:22:25 -0400 | [diff] [blame] | 3136 | struct btrfs_root *root = BTRFS_I(inode)->root; |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 3137 | |
Chris Mason | d20f704 | 2008-12-08 16:58:54 -0500 | [diff] [blame] | 3138 | if (PageChecked(page)) { |
| 3139 | ClearPageChecked(page); |
Miao Xie | dc380ae | 2014-09-12 18:43:55 +0800 | [diff] [blame] | 3140 | return 0; |
Chris Mason | d20f704 | 2008-12-08 16:58:54 -0500 | [diff] [blame] | 3141 | } |
Christoph Hellwig | 6cbff00 | 2009-04-17 10:37:41 +0200 | [diff] [blame] | 3142 | |
| 3143 | if (BTRFS_I(inode)->flags & BTRFS_INODE_NODATASUM) |
Miao Xie | dc380ae | 2014-09-12 18:43:55 +0800 | [diff] [blame] | 3144 | return 0; |
Chris Mason | d20f704 | 2008-12-08 16:58:54 -0500 | [diff] [blame] | 3145 | |
Yan Zheng | 17d217f | 2008-12-12 10:03:38 -0500 | [diff] [blame] | 3146 | if (root->root_key.objectid == BTRFS_DATA_RELOC_TREE_OBJECTID && |
Chris Mason | 9655d29 | 2009-09-02 15:22:30 -0400 | [diff] [blame] | 3147 | test_range_bit(io_tree, start, end, EXTENT_NODATASUM, 1, NULL)) { |
David Sterba | 9116621 | 2016-04-26 23:54:39 +0200 | [diff] [blame] | 3148 | clear_extent_bits(io_tree, start, end, EXTENT_NODATASUM); |
Yan Zheng | 17d217f | 2008-12-12 10:03:38 -0500 | [diff] [blame] | 3149 | return 0; |
| 3150 | } |
| 3151 | |
Miao Xie | facc8a22 | 2013-07-25 19:22:34 +0800 | [diff] [blame] | 3152 | phy_offset >>= inode->i_sb->s_blocksize_bits; |
Miao Xie | dc380ae | 2014-09-12 18:43:55 +0800 | [diff] [blame] | 3153 | return __readpage_endio_check(inode, io_bio, phy_offset, page, offset, |
| 3154 | start, (size_t)(end - start + 1)); |
Chris Mason | 07157aa | 2007-08-30 08:50:51 -0400 | [diff] [blame] | 3155 | } |
Chris Mason | b888db2 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 3156 | |
Yan, Zheng | 24bbcf0 | 2009-11-12 09:36:34 +0000 | [diff] [blame] | 3157 | void btrfs_add_delayed_iput(struct inode *inode) |
| 3158 | { |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 3159 | struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb); |
David Sterba | 8089fe6 | 2015-11-19 14:15:51 +0100 | [diff] [blame] | 3160 | struct btrfs_inode *binode = BTRFS_I(inode); |
Yan, Zheng | 24bbcf0 | 2009-11-12 09:36:34 +0000 | [diff] [blame] | 3161 | |
| 3162 | if (atomic_add_unless(&inode->i_count, -1, 1)) |
| 3163 | return; |
| 3164 | |
Yan, Zheng | 24bbcf0 | 2009-11-12 09:36:34 +0000 | [diff] [blame] | 3165 | spin_lock(&fs_info->delayed_iput_lock); |
David Sterba | 8089fe6 | 2015-11-19 14:15:51 +0100 | [diff] [blame] | 3166 | if (binode->delayed_iput_count == 0) { |
| 3167 | ASSERT(list_empty(&binode->delayed_iput)); |
| 3168 | list_add_tail(&binode->delayed_iput, &fs_info->delayed_iputs); |
| 3169 | } else { |
| 3170 | binode->delayed_iput_count++; |
| 3171 | } |
Yan, Zheng | 24bbcf0 | 2009-11-12 09:36:34 +0000 | [diff] [blame] | 3172 | spin_unlock(&fs_info->delayed_iput_lock); |
| 3173 | } |
| 3174 | |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 3175 | void btrfs_run_delayed_iputs(struct btrfs_fs_info *fs_info) |
Yan, Zheng | 24bbcf0 | 2009-11-12 09:36:34 +0000 | [diff] [blame] | 3176 | { |
Yan, Zheng | 24bbcf0 | 2009-11-12 09:36:34 +0000 | [diff] [blame] | 3177 | |
Yan, Zheng | 24bbcf0 | 2009-11-12 09:36:34 +0000 | [diff] [blame] | 3178 | spin_lock(&fs_info->delayed_iput_lock); |
David Sterba | 8089fe6 | 2015-11-19 14:15:51 +0100 | [diff] [blame] | 3179 | while (!list_empty(&fs_info->delayed_iputs)) { |
| 3180 | struct btrfs_inode *inode; |
Yan, Zheng | 24bbcf0 | 2009-11-12 09:36:34 +0000 | [diff] [blame] | 3181 | |
David Sterba | 8089fe6 | 2015-11-19 14:15:51 +0100 | [diff] [blame] | 3182 | inode = list_first_entry(&fs_info->delayed_iputs, |
| 3183 | struct btrfs_inode, delayed_iput); |
| 3184 | if (inode->delayed_iput_count) { |
| 3185 | inode->delayed_iput_count--; |
| 3186 | list_move_tail(&inode->delayed_iput, |
| 3187 | &fs_info->delayed_iputs); |
| 3188 | } else { |
| 3189 | list_del_init(&inode->delayed_iput); |
| 3190 | } |
| 3191 | spin_unlock(&fs_info->delayed_iput_lock); |
| 3192 | iput(&inode->vfs_inode); |
| 3193 | spin_lock(&fs_info->delayed_iput_lock); |
Yan, Zheng | 24bbcf0 | 2009-11-12 09:36:34 +0000 | [diff] [blame] | 3194 | } |
David Sterba | 8089fe6 | 2015-11-19 14:15:51 +0100 | [diff] [blame] | 3195 | spin_unlock(&fs_info->delayed_iput_lock); |
Yan, Zheng | 24bbcf0 | 2009-11-12 09:36:34 +0000 | [diff] [blame] | 3196 | } |
| 3197 | |
Yan, Zheng | d68fc57 | 2010-05-16 10:49:58 -0400 | [diff] [blame] | 3198 | /* |
Justin P. Mattock | 42b2aa8 | 2011-11-28 20:31:00 -0800 | [diff] [blame] | 3199 | * This is called in transaction commit time. If there are no orphan |
Yan, Zheng | d68fc57 | 2010-05-16 10:49:58 -0400 | [diff] [blame] | 3200 | * files in the subvolume, it removes orphan item and frees block_rsv |
| 3201 | * structure. |
| 3202 | */ |
| 3203 | void btrfs_orphan_commit_root(struct btrfs_trans_handle *trans, |
| 3204 | struct btrfs_root *root) |
| 3205 | { |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 3206 | struct btrfs_fs_info *fs_info = root->fs_info; |
Josef Bacik | 90290e1 | 2011-12-02 15:44:12 -0500 | [diff] [blame] | 3207 | struct btrfs_block_rsv *block_rsv; |
Yan, Zheng | d68fc57 | 2010-05-16 10:49:58 -0400 | [diff] [blame] | 3208 | int ret; |
| 3209 | |
Josef Bacik | 8a35d95 | 2012-05-23 14:26:42 -0400 | [diff] [blame] | 3210 | if (atomic_read(&root->orphan_inodes) || |
Yan, Zheng | d68fc57 | 2010-05-16 10:49:58 -0400 | [diff] [blame] | 3211 | root->orphan_cleanup_state != ORPHAN_CLEANUP_DONE) |
| 3212 | return; |
| 3213 | |
Josef Bacik | 90290e1 | 2011-12-02 15:44:12 -0500 | [diff] [blame] | 3214 | spin_lock(&root->orphan_lock); |
Josef Bacik | 8a35d95 | 2012-05-23 14:26:42 -0400 | [diff] [blame] | 3215 | if (atomic_read(&root->orphan_inodes)) { |
Josef Bacik | 90290e1 | 2011-12-02 15:44:12 -0500 | [diff] [blame] | 3216 | spin_unlock(&root->orphan_lock); |
| 3217 | return; |
| 3218 | } |
| 3219 | |
| 3220 | if (root->orphan_cleanup_state != ORPHAN_CLEANUP_DONE) { |
| 3221 | spin_unlock(&root->orphan_lock); |
| 3222 | return; |
| 3223 | } |
| 3224 | |
| 3225 | block_rsv = root->orphan_block_rsv; |
| 3226 | root->orphan_block_rsv = NULL; |
| 3227 | spin_unlock(&root->orphan_lock); |
| 3228 | |
Miao Xie | 27cdeb7 | 2014-04-02 19:51:05 +0800 | [diff] [blame] | 3229 | if (test_bit(BTRFS_ROOT_ORPHAN_ITEM_INSERTED, &root->state) && |
Yan, Zheng | d68fc57 | 2010-05-16 10:49:58 -0400 | [diff] [blame] | 3230 | btrfs_root_refs(&root->root_item) > 0) { |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 3231 | ret = btrfs_del_orphan_item(trans, fs_info->tree_root, |
Yan, Zheng | d68fc57 | 2010-05-16 10:49:58 -0400 | [diff] [blame] | 3232 | root->root_key.objectid); |
Josef Bacik | 4ef31a4 | 2013-08-13 14:10:08 -0400 | [diff] [blame] | 3233 | if (ret) |
Jeff Mahoney | 6664283 | 2016-06-10 18:19:25 -0400 | [diff] [blame] | 3234 | btrfs_abort_transaction(trans, ret); |
Josef Bacik | 4ef31a4 | 2013-08-13 14:10:08 -0400 | [diff] [blame] | 3235 | else |
Miao Xie | 27cdeb7 | 2014-04-02 19:51:05 +0800 | [diff] [blame] | 3236 | clear_bit(BTRFS_ROOT_ORPHAN_ITEM_INSERTED, |
| 3237 | &root->state); |
Yan, Zheng | d68fc57 | 2010-05-16 10:49:58 -0400 | [diff] [blame] | 3238 | } |
| 3239 | |
Josef Bacik | 90290e1 | 2011-12-02 15:44:12 -0500 | [diff] [blame] | 3240 | if (block_rsv) { |
| 3241 | WARN_ON(block_rsv->size > 0); |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 3242 | btrfs_free_block_rsv(fs_info, block_rsv); |
Yan, Zheng | d68fc57 | 2010-05-16 10:49:58 -0400 | [diff] [blame] | 3243 | } |
| 3244 | } |
| 3245 | |
| 3246 | /* |
Josef Bacik | 7b12876 | 2008-07-24 12:17:14 -0400 | [diff] [blame] | 3247 | * This creates an orphan entry for the given inode in case something goes |
| 3248 | * wrong in the middle of an unlink/truncate. |
Yan, Zheng | d68fc57 | 2010-05-16 10:49:58 -0400 | [diff] [blame] | 3249 | * |
| 3250 | * NOTE: caller of this function should reserve 5 units of metadata for |
| 3251 | * this function. |
Josef Bacik | 7b12876 | 2008-07-24 12:17:14 -0400 | [diff] [blame] | 3252 | */ |
| 3253 | int btrfs_orphan_add(struct btrfs_trans_handle *trans, struct inode *inode) |
| 3254 | { |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 3255 | struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb); |
Josef Bacik | 7b12876 | 2008-07-24 12:17:14 -0400 | [diff] [blame] | 3256 | struct btrfs_root *root = BTRFS_I(inode)->root; |
Yan, Zheng | d68fc57 | 2010-05-16 10:49:58 -0400 | [diff] [blame] | 3257 | struct btrfs_block_rsv *block_rsv = NULL; |
| 3258 | int reserve = 0; |
| 3259 | int insert = 0; |
| 3260 | int ret; |
Josef Bacik | 7b12876 | 2008-07-24 12:17:14 -0400 | [diff] [blame] | 3261 | |
Yan, Zheng | d68fc57 | 2010-05-16 10:49:58 -0400 | [diff] [blame] | 3262 | if (!root->orphan_block_rsv) { |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 3263 | block_rsv = btrfs_alloc_block_rsv(fs_info, |
| 3264 | BTRFS_BLOCK_RSV_TEMP); |
Tsutomu Itoh | b532402 | 2011-07-19 07:27:20 +0000 | [diff] [blame] | 3265 | if (!block_rsv) |
| 3266 | return -ENOMEM; |
Josef Bacik | 7b12876 | 2008-07-24 12:17:14 -0400 | [diff] [blame] | 3267 | } |
| 3268 | |
Yan, Zheng | d68fc57 | 2010-05-16 10:49:58 -0400 | [diff] [blame] | 3269 | spin_lock(&root->orphan_lock); |
| 3270 | if (!root->orphan_block_rsv) { |
| 3271 | root->orphan_block_rsv = block_rsv; |
| 3272 | } else if (block_rsv) { |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 3273 | btrfs_free_block_rsv(fs_info, block_rsv); |
Yan, Zheng | d68fc57 | 2010-05-16 10:49:58 -0400 | [diff] [blame] | 3274 | block_rsv = NULL; |
| 3275 | } |
Josef Bacik | 7b12876 | 2008-07-24 12:17:14 -0400 | [diff] [blame] | 3276 | |
Josef Bacik | 8a35d95 | 2012-05-23 14:26:42 -0400 | [diff] [blame] | 3277 | if (!test_and_set_bit(BTRFS_INODE_HAS_ORPHAN_ITEM, |
| 3278 | &BTRFS_I(inode)->runtime_flags)) { |
Yan, Zheng | d68fc57 | 2010-05-16 10:49:58 -0400 | [diff] [blame] | 3279 | #if 0 |
| 3280 | /* |
| 3281 | * For proper ENOSPC handling, we should do orphan |
| 3282 | * cleanup when mounting. But this introduces backward |
| 3283 | * compatibility issue. |
| 3284 | */ |
| 3285 | if (!xchg(&root->orphan_item_inserted, 1)) |
| 3286 | insert = 2; |
| 3287 | else |
| 3288 | insert = 1; |
| 3289 | #endif |
| 3290 | insert = 1; |
Miao Xie | 321f0e7 | 2012-08-28 22:13:02 -0600 | [diff] [blame] | 3291 | atomic_inc(&root->orphan_inodes); |
Yan, Zheng | d68fc57 | 2010-05-16 10:49:58 -0400 | [diff] [blame] | 3292 | } |
Josef Bacik | 7b12876 | 2008-07-24 12:17:14 -0400 | [diff] [blame] | 3293 | |
Josef Bacik | 72ac3c0 | 2012-05-23 14:13:11 -0400 | [diff] [blame] | 3294 | if (!test_and_set_bit(BTRFS_INODE_ORPHAN_META_RESERVED, |
| 3295 | &BTRFS_I(inode)->runtime_flags)) |
Yan, Zheng | d68fc57 | 2010-05-16 10:49:58 -0400 | [diff] [blame] | 3296 | reserve = 1; |
Yan, Zheng | d68fc57 | 2010-05-16 10:49:58 -0400 | [diff] [blame] | 3297 | spin_unlock(&root->orphan_lock); |
Josef Bacik | 7b12876 | 2008-07-24 12:17:14 -0400 | [diff] [blame] | 3298 | |
Yan, Zheng | d68fc57 | 2010-05-16 10:49:58 -0400 | [diff] [blame] | 3299 | /* grab metadata reservation from transaction handle */ |
| 3300 | if (reserve) { |
| 3301 | ret = btrfs_orphan_reserve_metadata(trans, inode); |
Josef Bacik | 3b6571c | 2016-05-27 13:03:04 -0400 | [diff] [blame] | 3302 | ASSERT(!ret); |
| 3303 | if (ret) { |
| 3304 | atomic_dec(&root->orphan_inodes); |
| 3305 | clear_bit(BTRFS_INODE_ORPHAN_META_RESERVED, |
| 3306 | &BTRFS_I(inode)->runtime_flags); |
| 3307 | if (insert) |
| 3308 | clear_bit(BTRFS_INODE_HAS_ORPHAN_ITEM, |
| 3309 | &BTRFS_I(inode)->runtime_flags); |
| 3310 | return ret; |
| 3311 | } |
Yan, Zheng | d68fc57 | 2010-05-16 10:49:58 -0400 | [diff] [blame] | 3312 | } |
| 3313 | |
| 3314 | /* insert an orphan item to track this unlinked/truncated file */ |
| 3315 | if (insert >= 1) { |
David Sterba | f85b737 | 2017-01-20 14:54:07 +0100 | [diff] [blame] | 3316 | ret = btrfs_insert_orphan_item(trans, root, |
| 3317 | btrfs_ino(BTRFS_I(inode))); |
Josef Bacik | 4ef31a4 | 2013-08-13 14:10:08 -0400 | [diff] [blame] | 3318 | if (ret) { |
Filipe David Borba Manana | 703c88e | 2013-09-22 21:54:55 +0100 | [diff] [blame] | 3319 | atomic_dec(&root->orphan_inodes); |
Josef Bacik | 4ef31a4 | 2013-08-13 14:10:08 -0400 | [diff] [blame] | 3320 | if (reserve) { |
| 3321 | clear_bit(BTRFS_INODE_ORPHAN_META_RESERVED, |
| 3322 | &BTRFS_I(inode)->runtime_flags); |
| 3323 | btrfs_orphan_release_metadata(inode); |
| 3324 | } |
| 3325 | if (ret != -EEXIST) { |
Josef Bacik | e8e7cff | 2013-08-21 15:54:00 -0400 | [diff] [blame] | 3326 | clear_bit(BTRFS_INODE_HAS_ORPHAN_ITEM, |
| 3327 | &BTRFS_I(inode)->runtime_flags); |
Jeff Mahoney | 6664283 | 2016-06-10 18:19:25 -0400 | [diff] [blame] | 3328 | btrfs_abort_transaction(trans, ret); |
Josef Bacik | 4ef31a4 | 2013-08-13 14:10:08 -0400 | [diff] [blame] | 3329 | return ret; |
| 3330 | } |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 3331 | } |
| 3332 | ret = 0; |
Yan, Zheng | d68fc57 | 2010-05-16 10:49:58 -0400 | [diff] [blame] | 3333 | } |
| 3334 | |
| 3335 | /* insert an orphan item to track subvolume contains orphan files */ |
| 3336 | if (insert >= 2) { |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 3337 | ret = btrfs_insert_orphan_item(trans, fs_info->tree_root, |
Yan, Zheng | d68fc57 | 2010-05-16 10:49:58 -0400 | [diff] [blame] | 3338 | root->root_key.objectid); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 3339 | if (ret && ret != -EEXIST) { |
Jeff Mahoney | 6664283 | 2016-06-10 18:19:25 -0400 | [diff] [blame] | 3340 | btrfs_abort_transaction(trans, ret); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 3341 | return ret; |
| 3342 | } |
Yan, Zheng | d68fc57 | 2010-05-16 10:49:58 -0400 | [diff] [blame] | 3343 | } |
| 3344 | return 0; |
Josef Bacik | 7b12876 | 2008-07-24 12:17:14 -0400 | [diff] [blame] | 3345 | } |
| 3346 | |
| 3347 | /* |
| 3348 | * We have done the truncate/delete so we can go ahead and remove the orphan |
| 3349 | * item for this particular inode. |
| 3350 | */ |
Eric Sandeen | 48a3b63 | 2013-04-25 20:41:01 +0000 | [diff] [blame] | 3351 | static int btrfs_orphan_del(struct btrfs_trans_handle *trans, |
| 3352 | struct inode *inode) |
Josef Bacik | 7b12876 | 2008-07-24 12:17:14 -0400 | [diff] [blame] | 3353 | { |
| 3354 | struct btrfs_root *root = BTRFS_I(inode)->root; |
Yan, Zheng | d68fc57 | 2010-05-16 10:49:58 -0400 | [diff] [blame] | 3355 | int delete_item = 0; |
| 3356 | int release_rsv = 0; |
Josef Bacik | 7b12876 | 2008-07-24 12:17:14 -0400 | [diff] [blame] | 3357 | int ret = 0; |
| 3358 | |
Yan, Zheng | d68fc57 | 2010-05-16 10:49:58 -0400 | [diff] [blame] | 3359 | spin_lock(&root->orphan_lock); |
Josef Bacik | 8a35d95 | 2012-05-23 14:26:42 -0400 | [diff] [blame] | 3360 | if (test_and_clear_bit(BTRFS_INODE_HAS_ORPHAN_ITEM, |
| 3361 | &BTRFS_I(inode)->runtime_flags)) |
Yan, Zheng | d68fc57 | 2010-05-16 10:49:58 -0400 | [diff] [blame] | 3362 | delete_item = 1; |
Josef Bacik | 7b12876 | 2008-07-24 12:17:14 -0400 | [diff] [blame] | 3363 | |
Josef Bacik | 72ac3c0 | 2012-05-23 14:13:11 -0400 | [diff] [blame] | 3364 | if (test_and_clear_bit(BTRFS_INODE_ORPHAN_META_RESERVED, |
| 3365 | &BTRFS_I(inode)->runtime_flags)) |
Yan, Zheng | d68fc57 | 2010-05-16 10:49:58 -0400 | [diff] [blame] | 3366 | release_rsv = 1; |
Yan, Zheng | d68fc57 | 2010-05-16 10:49:58 -0400 | [diff] [blame] | 3367 | spin_unlock(&root->orphan_lock); |
| 3368 | |
Filipe David Borba Manana | 703c88e | 2013-09-22 21:54:55 +0100 | [diff] [blame] | 3369 | if (delete_item) { |
Josef Bacik | 8a35d95 | 2012-05-23 14:26:42 -0400 | [diff] [blame] | 3370 | atomic_dec(&root->orphan_inodes); |
Filipe David Borba Manana | 703c88e | 2013-09-22 21:54:55 +0100 | [diff] [blame] | 3371 | if (trans) |
| 3372 | ret = btrfs_del_orphan_item(trans, root, |
Nikolay Borisov | 4a0cc7c | 2017-01-10 20:35:31 +0200 | [diff] [blame] | 3373 | btrfs_ino(BTRFS_I(inode))); |
Josef Bacik | 8a35d95 | 2012-05-23 14:26:42 -0400 | [diff] [blame] | 3374 | } |
Josef Bacik | 7b12876 | 2008-07-24 12:17:14 -0400 | [diff] [blame] | 3375 | |
Filipe David Borba Manana | 703c88e | 2013-09-22 21:54:55 +0100 | [diff] [blame] | 3376 | if (release_rsv) |
| 3377 | btrfs_orphan_release_metadata(inode); |
| 3378 | |
Josef Bacik | 4ef31a4 | 2013-08-13 14:10:08 -0400 | [diff] [blame] | 3379 | return ret; |
Josef Bacik | 7b12876 | 2008-07-24 12:17:14 -0400 | [diff] [blame] | 3380 | } |
| 3381 | |
| 3382 | /* |
| 3383 | * this cleans up any orphans that may be left on the list from the last use |
| 3384 | * of this root. |
| 3385 | */ |
Josef Bacik | 66b4ffd | 2011-01-31 16:22:42 -0500 | [diff] [blame] | 3386 | int btrfs_orphan_cleanup(struct btrfs_root *root) |
Josef Bacik | 7b12876 | 2008-07-24 12:17:14 -0400 | [diff] [blame] | 3387 | { |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 3388 | struct btrfs_fs_info *fs_info = root->fs_info; |
Josef Bacik | 7b12876 | 2008-07-24 12:17:14 -0400 | [diff] [blame] | 3389 | struct btrfs_path *path; |
| 3390 | struct extent_buffer *leaf; |
Josef Bacik | 7b12876 | 2008-07-24 12:17:14 -0400 | [diff] [blame] | 3391 | struct btrfs_key key, found_key; |
| 3392 | struct btrfs_trans_handle *trans; |
| 3393 | struct inode *inode; |
Josef Bacik | 8f6d7f4 | 2011-09-26 15:55:20 -0400 | [diff] [blame] | 3394 | u64 last_objectid = 0; |
Josef Bacik | 7b12876 | 2008-07-24 12:17:14 -0400 | [diff] [blame] | 3395 | int ret = 0, nr_unlink = 0, nr_truncate = 0; |
| 3396 | |
Yan, Zheng | d68fc57 | 2010-05-16 10:49:58 -0400 | [diff] [blame] | 3397 | if (cmpxchg(&root->orphan_cleanup_state, 0, ORPHAN_CLEANUP_STARTED)) |
Josef Bacik | 66b4ffd | 2011-01-31 16:22:42 -0500 | [diff] [blame] | 3398 | return 0; |
Yan, Zheng | c71bf09 | 2009-11-12 09:34:40 +0000 | [diff] [blame] | 3399 | |
| 3400 | path = btrfs_alloc_path(); |
Josef Bacik | 66b4ffd | 2011-01-31 16:22:42 -0500 | [diff] [blame] | 3401 | if (!path) { |
| 3402 | ret = -ENOMEM; |
| 3403 | goto out; |
| 3404 | } |
David Sterba | e4058b5 | 2015-11-27 16:31:35 +0100 | [diff] [blame] | 3405 | path->reada = READA_BACK; |
Josef Bacik | 7b12876 | 2008-07-24 12:17:14 -0400 | [diff] [blame] | 3406 | |
| 3407 | key.objectid = BTRFS_ORPHAN_OBJECTID; |
David Sterba | 962a298 | 2014-06-04 18:41:45 +0200 | [diff] [blame] | 3408 | key.type = BTRFS_ORPHAN_ITEM_KEY; |
Josef Bacik | 7b12876 | 2008-07-24 12:17:14 -0400 | [diff] [blame] | 3409 | key.offset = (u64)-1; |
| 3410 | |
Josef Bacik | 7b12876 | 2008-07-24 12:17:14 -0400 | [diff] [blame] | 3411 | while (1) { |
| 3412 | ret = btrfs_search_slot(NULL, root, &key, path, 0, 0); |
Josef Bacik | 66b4ffd | 2011-01-31 16:22:42 -0500 | [diff] [blame] | 3413 | if (ret < 0) |
| 3414 | goto out; |
Josef Bacik | 7b12876 | 2008-07-24 12:17:14 -0400 | [diff] [blame] | 3415 | |
| 3416 | /* |
| 3417 | * 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] | 3418 | * 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] | 3419 | * find the key and see if we have stuff that matches |
| 3420 | */ |
| 3421 | if (ret > 0) { |
Josef Bacik | 66b4ffd | 2011-01-31 16:22:42 -0500 | [diff] [blame] | 3422 | ret = 0; |
Josef Bacik | 7b12876 | 2008-07-24 12:17:14 -0400 | [diff] [blame] | 3423 | if (path->slots[0] == 0) |
| 3424 | break; |
| 3425 | path->slots[0]--; |
| 3426 | } |
| 3427 | |
| 3428 | /* pull out the item */ |
| 3429 | leaf = path->nodes[0]; |
Josef Bacik | 7b12876 | 2008-07-24 12:17:14 -0400 | [diff] [blame] | 3430 | btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]); |
| 3431 | |
| 3432 | /* make sure the item matches what we want */ |
| 3433 | if (found_key.objectid != BTRFS_ORPHAN_OBJECTID) |
| 3434 | break; |
David Sterba | 962a298 | 2014-06-04 18:41:45 +0200 | [diff] [blame] | 3435 | if (found_key.type != BTRFS_ORPHAN_ITEM_KEY) |
Josef Bacik | 7b12876 | 2008-07-24 12:17:14 -0400 | [diff] [blame] | 3436 | break; |
| 3437 | |
| 3438 | /* release the path since we're done with it */ |
David Sterba | b3b4aa7 | 2011-04-21 01:20:15 +0200 | [diff] [blame] | 3439 | btrfs_release_path(path); |
Josef Bacik | 7b12876 | 2008-07-24 12:17:14 -0400 | [diff] [blame] | 3440 | |
| 3441 | /* |
| 3442 | * this is where we are basically btrfs_lookup, without the |
| 3443 | * crossing root thing. we store the inode number in the |
| 3444 | * offset of the orphan item. |
| 3445 | */ |
Josef Bacik | 8f6d7f4 | 2011-09-26 15:55:20 -0400 | [diff] [blame] | 3446 | |
| 3447 | if (found_key.offset == last_objectid) { |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 3448 | btrfs_err(fs_info, |
| 3449 | "Error removing orphan entry, stopping orphan cleanup"); |
Josef Bacik | 8f6d7f4 | 2011-09-26 15:55:20 -0400 | [diff] [blame] | 3450 | ret = -EINVAL; |
| 3451 | goto out; |
| 3452 | } |
| 3453 | |
| 3454 | last_objectid = found_key.offset; |
| 3455 | |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 3456 | found_key.objectid = found_key.offset; |
| 3457 | found_key.type = BTRFS_INODE_ITEM_KEY; |
| 3458 | found_key.offset = 0; |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 3459 | inode = btrfs_iget(fs_info->sb, &found_key, root, NULL); |
Rusty Russell | 8c6ffba | 2013-07-15 11:20:32 +0930 | [diff] [blame] | 3460 | ret = PTR_ERR_OR_ZERO(inode); |
Filipe Manana | 6771089 | 2016-06-06 11:51:25 +0100 | [diff] [blame] | 3461 | if (ret && ret != -ENOENT) |
Josef Bacik | 66b4ffd | 2011-01-31 16:22:42 -0500 | [diff] [blame] | 3462 | goto out; |
Josef Bacik | a8c9e57 | 2011-09-21 16:55:59 -0400 | [diff] [blame] | 3463 | |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 3464 | if (ret == -ENOENT && root == fs_info->tree_root) { |
Arne Jansen | f8e9e0b | 2011-12-14 20:12:02 -0500 | [diff] [blame] | 3465 | struct btrfs_root *dead_root; |
| 3466 | struct btrfs_fs_info *fs_info = root->fs_info; |
| 3467 | int is_dead_root = 0; |
| 3468 | |
| 3469 | /* |
| 3470 | * this is an orphan in the tree root. Currently these |
| 3471 | * could come from 2 sources: |
| 3472 | * a) a snapshot deletion in progress |
| 3473 | * b) a free space cache inode |
| 3474 | * We need to distinguish those two, as the snapshot |
| 3475 | * orphan must not get deleted. |
| 3476 | * find_dead_roots already ran before us, so if this |
| 3477 | * is a snapshot deletion, we should find the root |
| 3478 | * in the dead_roots list |
| 3479 | */ |
| 3480 | spin_lock(&fs_info->trans_lock); |
| 3481 | list_for_each_entry(dead_root, &fs_info->dead_roots, |
| 3482 | root_list) { |
| 3483 | if (dead_root->root_key.objectid == |
| 3484 | found_key.objectid) { |
| 3485 | is_dead_root = 1; |
| 3486 | break; |
| 3487 | } |
| 3488 | } |
| 3489 | spin_unlock(&fs_info->trans_lock); |
| 3490 | if (is_dead_root) { |
| 3491 | /* prevent this orphan from being found again */ |
| 3492 | key.offset = found_key.objectid - 1; |
| 3493 | continue; |
| 3494 | } |
| 3495 | } |
Josef Bacik | a8c9e57 | 2011-09-21 16:55:59 -0400 | [diff] [blame] | 3496 | /* |
| 3497 | * Inode is already gone but the orphan item is still there, |
| 3498 | * kill the orphan item. |
| 3499 | */ |
Filipe Manana | 6771089 | 2016-06-06 11:51:25 +0100 | [diff] [blame] | 3500 | if (ret == -ENOENT) { |
Josef Bacik | a8c9e57 | 2011-09-21 16:55:59 -0400 | [diff] [blame] | 3501 | trans = btrfs_start_transaction(root, 1); |
| 3502 | if (IS_ERR(trans)) { |
| 3503 | ret = PTR_ERR(trans); |
| 3504 | goto out; |
| 3505 | } |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 3506 | btrfs_debug(fs_info, "auto deleting %Lu", |
| 3507 | found_key.objectid); |
Josef Bacik | a8c9e57 | 2011-09-21 16:55:59 -0400 | [diff] [blame] | 3508 | ret = btrfs_del_orphan_item(trans, root, |
| 3509 | found_key.objectid); |
Jeff Mahoney | 3a45bb2 | 2016-09-09 21:39:03 -0400 | [diff] [blame] | 3510 | btrfs_end_transaction(trans); |
Josef Bacik | 4ef31a4 | 2013-08-13 14:10:08 -0400 | [diff] [blame] | 3511 | if (ret) |
| 3512 | goto out; |
Josef Bacik | a8c9e57 | 2011-09-21 16:55:59 -0400 | [diff] [blame] | 3513 | continue; |
Josef Bacik | 66b4ffd | 2011-01-31 16:22:42 -0500 | [diff] [blame] | 3514 | } |
Josef Bacik | 7b12876 | 2008-07-24 12:17:14 -0400 | [diff] [blame] | 3515 | |
Josef Bacik | 7b12876 | 2008-07-24 12:17:14 -0400 | [diff] [blame] | 3516 | /* |
| 3517 | * add this inode to the orphan list so btrfs_orphan_del does |
| 3518 | * the proper thing when we hit it |
| 3519 | */ |
Josef Bacik | 8a35d95 | 2012-05-23 14:26:42 -0400 | [diff] [blame] | 3520 | set_bit(BTRFS_INODE_HAS_ORPHAN_ITEM, |
| 3521 | &BTRFS_I(inode)->runtime_flags); |
Josef Bacik | 925396e | 2013-02-01 15:57:47 -0500 | [diff] [blame] | 3522 | atomic_inc(&root->orphan_inodes); |
Josef Bacik | 7b12876 | 2008-07-24 12:17:14 -0400 | [diff] [blame] | 3523 | |
Josef Bacik | 7b12876 | 2008-07-24 12:17:14 -0400 | [diff] [blame] | 3524 | /* if we have links, this was a truncate, lets do that */ |
| 3525 | if (inode->i_nlink) { |
Dulshani Gunawardhana | fae7f21 | 2013-10-31 10:30:08 +0530 | [diff] [blame] | 3526 | if (WARN_ON(!S_ISREG(inode->i_mode))) { |
Josef Bacik | a41ad39 | 2011-01-31 15:30:16 -0500 | [diff] [blame] | 3527 | iput(inode); |
| 3528 | continue; |
| 3529 | } |
Josef Bacik | 7b12876 | 2008-07-24 12:17:14 -0400 | [diff] [blame] | 3530 | nr_truncate++; |
Josef Bacik | f3fe820 | 2013-01-07 17:03:21 -0500 | [diff] [blame] | 3531 | |
| 3532 | /* 1 for the orphan item deletion. */ |
| 3533 | trans = btrfs_start_transaction(root, 1); |
| 3534 | if (IS_ERR(trans)) { |
Josef Bacik | c69b26b | 2013-06-03 16:51:23 -0400 | [diff] [blame] | 3535 | iput(inode); |
Josef Bacik | f3fe820 | 2013-01-07 17:03:21 -0500 | [diff] [blame] | 3536 | ret = PTR_ERR(trans); |
| 3537 | goto out; |
| 3538 | } |
| 3539 | ret = btrfs_orphan_add(trans, inode); |
Jeff Mahoney | 3a45bb2 | 2016-09-09 21:39:03 -0400 | [diff] [blame] | 3540 | btrfs_end_transaction(trans); |
Josef Bacik | c69b26b | 2013-06-03 16:51:23 -0400 | [diff] [blame] | 3541 | if (ret) { |
| 3542 | iput(inode); |
Josef Bacik | f3fe820 | 2013-01-07 17:03:21 -0500 | [diff] [blame] | 3543 | goto out; |
Josef Bacik | c69b26b | 2013-06-03 16:51:23 -0400 | [diff] [blame] | 3544 | } |
Josef Bacik | f3fe820 | 2013-01-07 17:03:21 -0500 | [diff] [blame] | 3545 | |
Josef Bacik | 66b4ffd | 2011-01-31 16:22:42 -0500 | [diff] [blame] | 3546 | ret = btrfs_truncate(inode); |
Josef Bacik | 4a7d0f6 | 2013-02-07 16:27:28 -0500 | [diff] [blame] | 3547 | if (ret) |
| 3548 | btrfs_orphan_del(NULL, inode); |
Josef Bacik | 7b12876 | 2008-07-24 12:17:14 -0400 | [diff] [blame] | 3549 | } else { |
| 3550 | nr_unlink++; |
| 3551 | } |
| 3552 | |
| 3553 | /* this will do delete_inode and everything for us */ |
| 3554 | iput(inode); |
Josef Bacik | 66b4ffd | 2011-01-31 16:22:42 -0500 | [diff] [blame] | 3555 | if (ret) |
| 3556 | goto out; |
Josef Bacik | 7b12876 | 2008-07-24 12:17:14 -0400 | [diff] [blame] | 3557 | } |
Miao Xie | 3254c87 | 2011-11-10 20:45:05 -0500 | [diff] [blame] | 3558 | /* release the path since we're done with it */ |
| 3559 | btrfs_release_path(path); |
| 3560 | |
Yan, Zheng | d68fc57 | 2010-05-16 10:49:58 -0400 | [diff] [blame] | 3561 | root->orphan_cleanup_state = ORPHAN_CLEANUP_DONE; |
| 3562 | |
| 3563 | if (root->orphan_block_rsv) |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 3564 | btrfs_block_rsv_release(fs_info, root->orphan_block_rsv, |
Yan, Zheng | d68fc57 | 2010-05-16 10:49:58 -0400 | [diff] [blame] | 3565 | (u64)-1); |
| 3566 | |
Miao Xie | 27cdeb7 | 2014-04-02 19:51:05 +0800 | [diff] [blame] | 3567 | if (root->orphan_block_rsv || |
| 3568 | test_bit(BTRFS_ROOT_ORPHAN_ITEM_INSERTED, &root->state)) { |
Josef Bacik | 7a7eaa4 | 2011-04-13 12:54:33 -0400 | [diff] [blame] | 3569 | trans = btrfs_join_transaction(root); |
Josef Bacik | 66b4ffd | 2011-01-31 16:22:42 -0500 | [diff] [blame] | 3570 | if (!IS_ERR(trans)) |
Jeff Mahoney | 3a45bb2 | 2016-09-09 21:39:03 -0400 | [diff] [blame] | 3571 | btrfs_end_transaction(trans); |
Yan, Zheng | d68fc57 | 2010-05-16 10:49:58 -0400 | [diff] [blame] | 3572 | } |
Josef Bacik | 7b12876 | 2008-07-24 12:17:14 -0400 | [diff] [blame] | 3573 | |
| 3574 | if (nr_unlink) |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 3575 | btrfs_debug(fs_info, "unlinked %d orphans", nr_unlink); |
Josef Bacik | 7b12876 | 2008-07-24 12:17:14 -0400 | [diff] [blame] | 3576 | if (nr_truncate) |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 3577 | btrfs_debug(fs_info, "truncated %d orphans", nr_truncate); |
Josef Bacik | 66b4ffd | 2011-01-31 16:22:42 -0500 | [diff] [blame] | 3578 | |
| 3579 | out: |
| 3580 | if (ret) |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 3581 | btrfs_err(fs_info, "could not do orphan cleanup %d", ret); |
Josef Bacik | 66b4ffd | 2011-01-31 16:22:42 -0500 | [diff] [blame] | 3582 | btrfs_free_path(path); |
| 3583 | return ret; |
Josef Bacik | 7b12876 | 2008-07-24 12:17:14 -0400 | [diff] [blame] | 3584 | } |
| 3585 | |
Chris Mason | d352ac6 | 2008-09-29 15:18:18 -0400 | [diff] [blame] | 3586 | /* |
Chris Mason | 46a53cc | 2009-04-27 11:47:50 -0400 | [diff] [blame] | 3587 | * very simple check to peek ahead in the leaf looking for xattrs. If we |
| 3588 | * don't find any xattrs, we know there can't be any acls. |
| 3589 | * |
| 3590 | * slot is the slot the inode is in, objectid is the objectid of the inode |
| 3591 | */ |
| 3592 | 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] | 3593 | int slot, u64 objectid, |
| 3594 | int *first_xattr_slot) |
Chris Mason | 46a53cc | 2009-04-27 11:47:50 -0400 | [diff] [blame] | 3595 | { |
| 3596 | u32 nritems = btrfs_header_nritems(leaf); |
| 3597 | struct btrfs_key found_key; |
Josef Bacik | f23b5a5 | 2013-06-19 10:16:26 -0400 | [diff] [blame] | 3598 | static u64 xattr_access = 0; |
| 3599 | static u64 xattr_default = 0; |
Chris Mason | 46a53cc | 2009-04-27 11:47:50 -0400 | [diff] [blame] | 3600 | int scanned = 0; |
| 3601 | |
Josef Bacik | f23b5a5 | 2013-06-19 10:16:26 -0400 | [diff] [blame] | 3602 | if (!xattr_access) { |
Andreas Gruenbacher | 97d7929 | 2015-12-02 14:44:35 +0100 | [diff] [blame] | 3603 | xattr_access = btrfs_name_hash(XATTR_NAME_POSIX_ACL_ACCESS, |
| 3604 | strlen(XATTR_NAME_POSIX_ACL_ACCESS)); |
| 3605 | xattr_default = btrfs_name_hash(XATTR_NAME_POSIX_ACL_DEFAULT, |
| 3606 | strlen(XATTR_NAME_POSIX_ACL_DEFAULT)); |
Josef Bacik | f23b5a5 | 2013-06-19 10:16:26 -0400 | [diff] [blame] | 3607 | } |
| 3608 | |
Chris Mason | 46a53cc | 2009-04-27 11:47:50 -0400 | [diff] [blame] | 3609 | slot++; |
Filipe David Borba Manana | 6354192 | 2014-01-07 11:47:46 +0000 | [diff] [blame] | 3610 | *first_xattr_slot = -1; |
Chris Mason | 46a53cc | 2009-04-27 11:47:50 -0400 | [diff] [blame] | 3611 | while (slot < nritems) { |
| 3612 | btrfs_item_key_to_cpu(leaf, &found_key, slot); |
| 3613 | |
| 3614 | /* we found a different objectid, there must not be acls */ |
| 3615 | if (found_key.objectid != objectid) |
| 3616 | return 0; |
| 3617 | |
| 3618 | /* we found an xattr, assume we've got an acl */ |
Josef Bacik | f23b5a5 | 2013-06-19 10:16:26 -0400 | [diff] [blame] | 3619 | if (found_key.type == BTRFS_XATTR_ITEM_KEY) { |
Filipe David Borba Manana | 6354192 | 2014-01-07 11:47:46 +0000 | [diff] [blame] | 3620 | if (*first_xattr_slot == -1) |
| 3621 | *first_xattr_slot = slot; |
Josef Bacik | f23b5a5 | 2013-06-19 10:16:26 -0400 | [diff] [blame] | 3622 | if (found_key.offset == xattr_access || |
| 3623 | found_key.offset == xattr_default) |
| 3624 | return 1; |
| 3625 | } |
Chris Mason | 46a53cc | 2009-04-27 11:47:50 -0400 | [diff] [blame] | 3626 | |
| 3627 | /* |
| 3628 | * we found a key greater than an xattr key, there can't |
| 3629 | * be any acls later on |
| 3630 | */ |
| 3631 | if (found_key.type > BTRFS_XATTR_ITEM_KEY) |
| 3632 | return 0; |
| 3633 | |
| 3634 | slot++; |
| 3635 | scanned++; |
| 3636 | |
| 3637 | /* |
| 3638 | * it goes inode, inode backrefs, xattrs, extents, |
| 3639 | * so if there are a ton of hard links to an inode there can |
| 3640 | * be a lot of backrefs. Don't waste time searching too hard, |
| 3641 | * this is just an optimization |
| 3642 | */ |
| 3643 | if (scanned >= 8) |
| 3644 | break; |
| 3645 | } |
| 3646 | /* we hit the end of the leaf before we found an xattr or |
| 3647 | * something larger than an xattr. We have to assume the inode |
| 3648 | * has acls |
| 3649 | */ |
Filipe David Borba Manana | 6354192 | 2014-01-07 11:47:46 +0000 | [diff] [blame] | 3650 | if (*first_xattr_slot == -1) |
| 3651 | *first_xattr_slot = slot; |
Chris Mason | 46a53cc | 2009-04-27 11:47:50 -0400 | [diff] [blame] | 3652 | return 1; |
| 3653 | } |
| 3654 | |
| 3655 | /* |
Chris Mason | d352ac6 | 2008-09-29 15:18:18 -0400 | [diff] [blame] | 3656 | * read an inode from the btree into the in-memory inode |
| 3657 | */ |
Filipe Manana | 6771089 | 2016-06-06 11:51:25 +0100 | [diff] [blame] | 3658 | static int btrfs_read_locked_inode(struct inode *inode) |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3659 | { |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 3660 | struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3661 | struct btrfs_path *path; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 3662 | struct extent_buffer *leaf; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3663 | struct btrfs_inode_item *inode_item; |
| 3664 | struct btrfs_root *root = BTRFS_I(inode)->root; |
| 3665 | struct btrfs_key location; |
Miao Xie | 67de117 | 2013-12-26 13:07:06 +0800 | [diff] [blame] | 3666 | unsigned long ptr; |
Chris Mason | 46a53cc | 2009-04-27 11:47:50 -0400 | [diff] [blame] | 3667 | int maybe_acls; |
Josef Bacik | 618e21d | 2007-07-11 10:18:17 -0400 | [diff] [blame] | 3668 | u32 rdev; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3669 | int ret; |
Miao Xie | 2f7e33d | 2011-06-23 07:27:13 +0000 | [diff] [blame] | 3670 | bool filled = false; |
Filipe David Borba Manana | 6354192 | 2014-01-07 11:47:46 +0000 | [diff] [blame] | 3671 | int first_xattr_slot; |
Miao Xie | 2f7e33d | 2011-06-23 07:27:13 +0000 | [diff] [blame] | 3672 | |
| 3673 | ret = btrfs_fill_inode(inode, &rdev); |
| 3674 | if (!ret) |
| 3675 | filled = true; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3676 | |
| 3677 | path = btrfs_alloc_path(); |
Filipe Manana | 6771089 | 2016-06-06 11:51:25 +0100 | [diff] [blame] | 3678 | if (!path) { |
| 3679 | ret = -ENOMEM; |
Mark Fasheh | 1748f84 | 2011-07-12 11:25:31 -0700 | [diff] [blame] | 3680 | goto make_bad; |
Filipe Manana | 6771089 | 2016-06-06 11:51:25 +0100 | [diff] [blame] | 3681 | } |
Mark Fasheh | 1748f84 | 2011-07-12 11:25:31 -0700 | [diff] [blame] | 3682 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3683 | memcpy(&location, &BTRFS_I(inode)->location, sizeof(location)); |
Chris Mason | dc17ff8 | 2008-01-08 15:46:30 -0500 | [diff] [blame] | 3684 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3685 | ret = btrfs_lookup_inode(NULL, root, path, &location, 0); |
Filipe Manana | 6771089 | 2016-06-06 11:51:25 +0100 | [diff] [blame] | 3686 | if (ret) { |
| 3687 | if (ret > 0) |
| 3688 | ret = -ENOENT; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3689 | goto make_bad; |
Filipe Manana | 6771089 | 2016-06-06 11:51:25 +0100 | [diff] [blame] | 3690 | } |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3691 | |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 3692 | leaf = path->nodes[0]; |
Miao Xie | 2f7e33d | 2011-06-23 07:27:13 +0000 | [diff] [blame] | 3693 | |
| 3694 | if (filled) |
Miao Xie | 67de117 | 2013-12-26 13:07:06 +0800 | [diff] [blame] | 3695 | goto cache_index; |
Miao Xie | 2f7e33d | 2011-06-23 07:27:13 +0000 | [diff] [blame] | 3696 | |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 3697 | inode_item = btrfs_item_ptr(leaf, path->slots[0], |
| 3698 | struct btrfs_inode_item); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 3699 | inode->i_mode = btrfs_inode_mode(leaf, inode_item); |
Miklos Szeredi | bfe8684 | 2011-10-28 14:13:29 +0200 | [diff] [blame] | 3700 | set_nlink(inode, btrfs_inode_nlink(leaf, inode_item)); |
Eric W. Biederman | 2f2f43d | 2012-02-10 11:05:07 -0800 | [diff] [blame] | 3701 | i_uid_write(inode, btrfs_inode_uid(leaf, inode_item)); |
| 3702 | i_gid_write(inode, btrfs_inode_gid(leaf, inode_item)); |
Chris Mason | dbe674a | 2008-07-17 12:54:05 -0400 | [diff] [blame] | 3703 | btrfs_i_size_write(inode, btrfs_inode_size(leaf, inode_item)); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 3704 | |
David Sterba | a937b97 | 2014-12-12 17:39:12 +0100 | [diff] [blame] | 3705 | inode->i_atime.tv_sec = btrfs_timespec_sec(leaf, &inode_item->atime); |
| 3706 | inode->i_atime.tv_nsec = btrfs_timespec_nsec(leaf, &inode_item->atime); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 3707 | |
David Sterba | a937b97 | 2014-12-12 17:39:12 +0100 | [diff] [blame] | 3708 | inode->i_mtime.tv_sec = btrfs_timespec_sec(leaf, &inode_item->mtime); |
| 3709 | inode->i_mtime.tv_nsec = btrfs_timespec_nsec(leaf, &inode_item->mtime); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 3710 | |
David Sterba | a937b97 | 2014-12-12 17:39:12 +0100 | [diff] [blame] | 3711 | inode->i_ctime.tv_sec = btrfs_timespec_sec(leaf, &inode_item->ctime); |
| 3712 | inode->i_ctime.tv_nsec = btrfs_timespec_nsec(leaf, &inode_item->ctime); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 3713 | |
chandan r | 9cc97d6 | 2012-07-04 12:48:07 +0530 | [diff] [blame] | 3714 | BTRFS_I(inode)->i_otime.tv_sec = |
| 3715 | btrfs_timespec_sec(leaf, &inode_item->otime); |
| 3716 | BTRFS_I(inode)->i_otime.tv_nsec = |
| 3717 | btrfs_timespec_nsec(leaf, &inode_item->otime); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 3718 | |
Yan Zheng | a76a3cd | 2008-10-09 11:46:29 -0400 | [diff] [blame] | 3719 | inode_set_bytes(inode, btrfs_inode_nbytes(leaf, inode_item)); |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 3720 | BTRFS_I(inode)->generation = btrfs_inode_generation(leaf, inode_item); |
Josef Bacik | 5dc562c | 2012-08-17 13:14:17 -0400 | [diff] [blame] | 3721 | BTRFS_I(inode)->last_trans = btrfs_inode_transid(leaf, inode_item); |
| 3722 | |
Josef Bacik | 0c4d2d9 | 2012-04-05 15:03:02 -0400 | [diff] [blame] | 3723 | inode->i_version = btrfs_inode_sequence(leaf, inode_item); |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 3724 | inode->i_generation = BTRFS_I(inode)->generation; |
Josef Bacik | 618e21d | 2007-07-11 10:18:17 -0400 | [diff] [blame] | 3725 | inode->i_rdev = 0; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 3726 | rdev = btrfs_inode_rdev(leaf, inode_item); |
| 3727 | |
Josef Bacik | aec7477 | 2008-07-24 12:12:38 -0400 | [diff] [blame] | 3728 | BTRFS_I(inode)->index_cnt = (u64)-1; |
Yan Zheng | d2fb343 | 2008-12-11 16:30:39 -0500 | [diff] [blame] | 3729 | BTRFS_I(inode)->flags = btrfs_inode_flags(leaf, inode_item); |
Miao Xie | 67de117 | 2013-12-26 13:07:06 +0800 | [diff] [blame] | 3730 | |
| 3731 | cache_index: |
Yang Dongsheng | 6e17d30 | 2015-04-09 12:08:43 +0800 | [diff] [blame] | 3732 | /* |
| 3733 | * If we were modified in the current generation and evicted from memory |
| 3734 | * and then re-read we need to do a full sync since we don't have any |
| 3735 | * idea about which extents were modified before we were evicted from |
| 3736 | * cache. |
| 3737 | * |
| 3738 | * This is required for both inode re-read from disk and delayed inode |
| 3739 | * in delayed_nodes_tree. |
| 3740 | */ |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 3741 | if (BTRFS_I(inode)->last_trans == fs_info->generation) |
Yang Dongsheng | 6e17d30 | 2015-04-09 12:08:43 +0800 | [diff] [blame] | 3742 | set_bit(BTRFS_INODE_NEEDS_FULL_SYNC, |
| 3743 | &BTRFS_I(inode)->runtime_flags); |
| 3744 | |
Filipe Manana | bde6c24 | 2015-07-24 00:00:19 +0100 | [diff] [blame] | 3745 | /* |
| 3746 | * We don't persist the id of the transaction where an unlink operation |
| 3747 | * against the inode was last made. So here we assume the inode might |
| 3748 | * have been evicted, and therefore the exact value of last_unlink_trans |
| 3749 | * lost, and set it to last_trans to avoid metadata inconsistencies |
| 3750 | * between the inode and its parent if the inode is fsync'ed and the log |
| 3751 | * replayed. For example, in the scenario: |
| 3752 | * |
| 3753 | * touch mydir/foo |
| 3754 | * ln mydir/foo mydir/bar |
| 3755 | * sync |
| 3756 | * unlink mydir/bar |
| 3757 | * echo 2 > /proc/sys/vm/drop_caches # evicts inode |
| 3758 | * xfs_io -c fsync mydir/foo |
| 3759 | * <power failure> |
| 3760 | * mount fs, triggers fsync log replay |
| 3761 | * |
| 3762 | * We must make sure that when we fsync our inode foo we also log its |
| 3763 | * parent inode, otherwise after log replay the parent still has the |
| 3764 | * dentry with the "bar" name but our inode foo has a link count of 1 |
| 3765 | * and doesn't have an inode ref with the name "bar" anymore. |
| 3766 | * |
| 3767 | * Setting last_unlink_trans to last_trans is a pessimistic approach, |
Nicholas D Steeves | 0132761 | 2016-05-19 21:18:45 -0400 | [diff] [blame] | 3768 | * but it guarantees correctness at the expense of occasional full |
Filipe Manana | bde6c24 | 2015-07-24 00:00:19 +0100 | [diff] [blame] | 3769 | * transaction commits on fsync if our inode is a directory, or if our |
| 3770 | * inode is not a directory, logging its parent unnecessarily. |
| 3771 | */ |
| 3772 | BTRFS_I(inode)->last_unlink_trans = BTRFS_I(inode)->last_trans; |
| 3773 | |
Miao Xie | 67de117 | 2013-12-26 13:07:06 +0800 | [diff] [blame] | 3774 | path->slots[0]++; |
| 3775 | if (inode->i_nlink != 1 || |
| 3776 | path->slots[0] >= btrfs_header_nritems(leaf)) |
| 3777 | goto cache_acl; |
| 3778 | |
| 3779 | btrfs_item_key_to_cpu(leaf, &location, path->slots[0]); |
Nikolay Borisov | 4a0cc7c | 2017-01-10 20:35:31 +0200 | [diff] [blame] | 3780 | if (location.objectid != btrfs_ino(BTRFS_I(inode))) |
Miao Xie | 67de117 | 2013-12-26 13:07:06 +0800 | [diff] [blame] | 3781 | goto cache_acl; |
| 3782 | |
| 3783 | ptr = btrfs_item_ptr_offset(leaf, path->slots[0]); |
| 3784 | if (location.type == BTRFS_INODE_REF_KEY) { |
| 3785 | struct btrfs_inode_ref *ref; |
| 3786 | |
| 3787 | ref = (struct btrfs_inode_ref *)ptr; |
| 3788 | BTRFS_I(inode)->dir_index = btrfs_inode_ref_index(leaf, ref); |
| 3789 | } else if (location.type == BTRFS_INODE_EXTREF_KEY) { |
| 3790 | struct btrfs_inode_extref *extref; |
| 3791 | |
| 3792 | extref = (struct btrfs_inode_extref *)ptr; |
| 3793 | BTRFS_I(inode)->dir_index = btrfs_inode_extref_index(leaf, |
| 3794 | extref); |
| 3795 | } |
Miao Xie | 2f7e33d | 2011-06-23 07:27:13 +0000 | [diff] [blame] | 3796 | cache_acl: |
Chris Mason | 46a53cc | 2009-04-27 11:47:50 -0400 | [diff] [blame] | 3797 | /* |
| 3798 | * try to precache a NULL acl entry for files that don't have |
| 3799 | * any xattrs or acls |
| 3800 | */ |
Li Zefan | 33345d01 | 2011-04-20 10:31:50 +0800 | [diff] [blame] | 3801 | maybe_acls = acls_after_inode_item(leaf, path->slots[0], |
David Sterba | f85b737 | 2017-01-20 14:54:07 +0100 | [diff] [blame] | 3802 | btrfs_ino(BTRFS_I(inode)), &first_xattr_slot); |
Filipe David Borba Manana | 6354192 | 2014-01-07 11:47:46 +0000 | [diff] [blame] | 3803 | if (first_xattr_slot != -1) { |
| 3804 | path->slots[0] = first_xattr_slot; |
| 3805 | ret = btrfs_load_inode_props(inode, path); |
| 3806 | if (ret) |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 3807 | btrfs_err(fs_info, |
David Sterba | 351fd35 | 2014-05-15 16:48:20 +0200 | [diff] [blame] | 3808 | "error loading props for ino %llu (root %llu): %d", |
Nikolay Borisov | 4a0cc7c | 2017-01-10 20:35:31 +0200 | [diff] [blame] | 3809 | btrfs_ino(BTRFS_I(inode)), |
Filipe David Borba Manana | 6354192 | 2014-01-07 11:47:46 +0000 | [diff] [blame] | 3810 | root->root_key.objectid, ret); |
| 3811 | } |
| 3812 | btrfs_free_path(path); |
| 3813 | |
Al Viro | 72c0490 | 2009-06-24 16:58:48 -0400 | [diff] [blame] | 3814 | if (!maybe_acls) |
| 3815 | cache_no_acl(inode); |
Chris Mason | 46a53cc | 2009-04-27 11:47:50 -0400 | [diff] [blame] | 3816 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3817 | switch (inode->i_mode & S_IFMT) { |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3818 | case S_IFREG: |
| 3819 | inode->i_mapping->a_ops = &btrfs_aops; |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 3820 | BTRFS_I(inode)->io_tree.ops = &btrfs_extent_io_ops; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3821 | inode->i_fop = &btrfs_file_operations; |
| 3822 | inode->i_op = &btrfs_file_inode_operations; |
| 3823 | break; |
| 3824 | case S_IFDIR: |
| 3825 | inode->i_fop = &btrfs_dir_file_operations; |
Omar Sandoval | 67ade05 | 2017-01-25 17:06:38 -0800 | [diff] [blame] | 3826 | inode->i_op = &btrfs_dir_inode_operations; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3827 | break; |
| 3828 | case S_IFLNK: |
| 3829 | inode->i_op = &btrfs_symlink_inode_operations; |
Al Viro | 21fc61c | 2015-11-17 01:07:57 -0500 | [diff] [blame] | 3830 | inode_nohighmem(inode); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3831 | inode->i_mapping->a_ops = &btrfs_symlink_aops; |
| 3832 | break; |
Josef Bacik | 618e21d | 2007-07-11 10:18:17 -0400 | [diff] [blame] | 3833 | default: |
Jim Owens | 0279b4c | 2009-02-04 09:29:13 -0500 | [diff] [blame] | 3834 | inode->i_op = &btrfs_special_inode_operations; |
Josef Bacik | 618e21d | 2007-07-11 10:18:17 -0400 | [diff] [blame] | 3835 | init_special_inode(inode, inode->i_mode, rdev); |
| 3836 | break; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3837 | } |
Christoph Hellwig | 6cbff00 | 2009-04-17 10:37:41 +0200 | [diff] [blame] | 3838 | |
| 3839 | btrfs_update_iflags(inode); |
Filipe Manana | 6771089 | 2016-06-06 11:51:25 +0100 | [diff] [blame] | 3840 | return 0; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3841 | |
| 3842 | make_bad: |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3843 | btrfs_free_path(path); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3844 | make_bad_inode(inode); |
Filipe Manana | 6771089 | 2016-06-06 11:51:25 +0100 | [diff] [blame] | 3845 | return ret; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3846 | } |
| 3847 | |
Chris Mason | d352ac6 | 2008-09-29 15:18:18 -0400 | [diff] [blame] | 3848 | /* |
| 3849 | * given a leaf and an inode, copy the inode fields into the leaf |
| 3850 | */ |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 3851 | static void fill_inode_item(struct btrfs_trans_handle *trans, |
| 3852 | struct extent_buffer *leaf, |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 3853 | struct btrfs_inode_item *item, |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3854 | struct inode *inode) |
| 3855 | { |
Liu Bo | 51fab69 | 2012-12-27 09:01:21 +0000 | [diff] [blame] | 3856 | struct btrfs_map_token token; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 3857 | |
Liu Bo | 51fab69 | 2012-12-27 09:01:21 +0000 | [diff] [blame] | 3858 | btrfs_init_map_token(&token); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 3859 | |
Liu Bo | 51fab69 | 2012-12-27 09:01:21 +0000 | [diff] [blame] | 3860 | btrfs_set_token_inode_uid(leaf, item, i_uid_read(inode), &token); |
| 3861 | btrfs_set_token_inode_gid(leaf, item, i_gid_read(inode), &token); |
| 3862 | btrfs_set_token_inode_size(leaf, item, BTRFS_I(inode)->disk_i_size, |
| 3863 | &token); |
| 3864 | btrfs_set_token_inode_mode(leaf, item, inode->i_mode, &token); |
| 3865 | btrfs_set_token_inode_nlink(leaf, item, inode->i_nlink, &token); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 3866 | |
David Sterba | a937b97 | 2014-12-12 17:39:12 +0100 | [diff] [blame] | 3867 | btrfs_set_token_timespec_sec(leaf, &item->atime, |
Liu Bo | 51fab69 | 2012-12-27 09:01:21 +0000 | [diff] [blame] | 3868 | inode->i_atime.tv_sec, &token); |
David Sterba | a937b97 | 2014-12-12 17:39:12 +0100 | [diff] [blame] | 3869 | btrfs_set_token_timespec_nsec(leaf, &item->atime, |
Liu Bo | 51fab69 | 2012-12-27 09:01:21 +0000 | [diff] [blame] | 3870 | inode->i_atime.tv_nsec, &token); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 3871 | |
David Sterba | a937b97 | 2014-12-12 17:39:12 +0100 | [diff] [blame] | 3872 | btrfs_set_token_timespec_sec(leaf, &item->mtime, |
Liu Bo | 51fab69 | 2012-12-27 09:01:21 +0000 | [diff] [blame] | 3873 | inode->i_mtime.tv_sec, &token); |
David Sterba | a937b97 | 2014-12-12 17:39:12 +0100 | [diff] [blame] | 3874 | btrfs_set_token_timespec_nsec(leaf, &item->mtime, |
Liu Bo | 51fab69 | 2012-12-27 09:01:21 +0000 | [diff] [blame] | 3875 | inode->i_mtime.tv_nsec, &token); |
| 3876 | |
David Sterba | a937b97 | 2014-12-12 17:39:12 +0100 | [diff] [blame] | 3877 | btrfs_set_token_timespec_sec(leaf, &item->ctime, |
Liu Bo | 51fab69 | 2012-12-27 09:01:21 +0000 | [diff] [blame] | 3878 | inode->i_ctime.tv_sec, &token); |
David Sterba | a937b97 | 2014-12-12 17:39:12 +0100 | [diff] [blame] | 3879 | btrfs_set_token_timespec_nsec(leaf, &item->ctime, |
Liu Bo | 51fab69 | 2012-12-27 09:01:21 +0000 | [diff] [blame] | 3880 | inode->i_ctime.tv_nsec, &token); |
| 3881 | |
chandan r | 9cc97d6 | 2012-07-04 12:48:07 +0530 | [diff] [blame] | 3882 | btrfs_set_token_timespec_sec(leaf, &item->otime, |
| 3883 | BTRFS_I(inode)->i_otime.tv_sec, &token); |
| 3884 | btrfs_set_token_timespec_nsec(leaf, &item->otime, |
| 3885 | BTRFS_I(inode)->i_otime.tv_nsec, &token); |
| 3886 | |
Liu Bo | 51fab69 | 2012-12-27 09:01:21 +0000 | [diff] [blame] | 3887 | btrfs_set_token_inode_nbytes(leaf, item, inode_get_bytes(inode), |
| 3888 | &token); |
| 3889 | btrfs_set_token_inode_generation(leaf, item, BTRFS_I(inode)->generation, |
| 3890 | &token); |
| 3891 | btrfs_set_token_inode_sequence(leaf, item, inode->i_version, &token); |
| 3892 | btrfs_set_token_inode_transid(leaf, item, trans->transid, &token); |
| 3893 | btrfs_set_token_inode_rdev(leaf, item, inode->i_rdev, &token); |
| 3894 | btrfs_set_token_inode_flags(leaf, item, BTRFS_I(inode)->flags, &token); |
| 3895 | btrfs_set_token_inode_block_group(leaf, item, 0, &token); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3896 | } |
| 3897 | |
Chris Mason | d352ac6 | 2008-09-29 15:18:18 -0400 | [diff] [blame] | 3898 | /* |
| 3899 | * copy everything in the in-memory inode into the btree. |
| 3900 | */ |
Chris Mason | 2115133 | 2011-11-10 20:39:08 -0500 | [diff] [blame] | 3901 | static noinline int btrfs_update_inode_item(struct btrfs_trans_handle *trans, |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 3902 | struct btrfs_root *root, struct inode *inode) |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3903 | { |
| 3904 | struct btrfs_inode_item *inode_item; |
| 3905 | struct btrfs_path *path; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 3906 | struct extent_buffer *leaf; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3907 | int ret; |
| 3908 | |
| 3909 | path = btrfs_alloc_path(); |
Miao Xie | 16cdcec | 2011-04-22 18:12:22 +0800 | [diff] [blame] | 3910 | if (!path) |
| 3911 | return -ENOMEM; |
| 3912 | |
Chris Mason | b947343 | 2009-03-13 11:00:37 -0400 | [diff] [blame] | 3913 | path->leave_spinning = 1; |
Miao Xie | 16cdcec | 2011-04-22 18:12:22 +0800 | [diff] [blame] | 3914 | ret = btrfs_lookup_inode(trans, root, path, &BTRFS_I(inode)->location, |
| 3915 | 1); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3916 | if (ret) { |
| 3917 | if (ret > 0) |
| 3918 | ret = -ENOENT; |
| 3919 | goto failed; |
| 3920 | } |
| 3921 | |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 3922 | leaf = path->nodes[0]; |
| 3923 | inode_item = btrfs_item_ptr(leaf, path->slots[0], |
Miao Xie | 16cdcec | 2011-04-22 18:12:22 +0800 | [diff] [blame] | 3924 | struct btrfs_inode_item); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3925 | |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 3926 | fill_inode_item(trans, leaf, inode_item, inode); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 3927 | btrfs_mark_buffer_dirty(leaf); |
Josef Bacik | 15ee9bc | 2007-08-10 16:22:09 -0400 | [diff] [blame] | 3928 | btrfs_set_inode_last_trans(trans, inode); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3929 | ret = 0; |
| 3930 | failed: |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3931 | btrfs_free_path(path); |
| 3932 | return ret; |
| 3933 | } |
| 3934 | |
Chris Mason | d352ac6 | 2008-09-29 15:18:18 -0400 | [diff] [blame] | 3935 | /* |
Chris Mason | 2115133 | 2011-11-10 20:39:08 -0500 | [diff] [blame] | 3936 | * copy everything in the in-memory inode into the btree. |
| 3937 | */ |
| 3938 | noinline int btrfs_update_inode(struct btrfs_trans_handle *trans, |
| 3939 | struct btrfs_root *root, struct inode *inode) |
| 3940 | { |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 3941 | struct btrfs_fs_info *fs_info = root->fs_info; |
Chris Mason | 2115133 | 2011-11-10 20:39:08 -0500 | [diff] [blame] | 3942 | int ret; |
| 3943 | |
| 3944 | /* |
| 3945 | * If the inode is a free space inode, we can deadlock during commit |
| 3946 | * if we put it into the delayed code. |
| 3947 | * |
| 3948 | * The data relocation inode should also be directly updated |
| 3949 | * without delay |
| 3950 | */ |
Liu Bo | 83eea1f | 2012-07-10 05:28:39 -0600 | [diff] [blame] | 3951 | if (!btrfs_is_free_space_inode(inode) |
Josef Bacik | 1d52c78 | 2014-09-18 11:30:44 -0400 | [diff] [blame] | 3952 | && root->root_key.objectid != BTRFS_DATA_RELOC_TREE_OBJECTID |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 3953 | && !test_bit(BTRFS_FS_LOG_RECOVERING, &fs_info->flags)) { |
Alexander Block | 8ea05e3 | 2012-07-25 17:35:53 +0200 | [diff] [blame] | 3954 | btrfs_update_root_times(trans, root); |
| 3955 | |
Chris Mason | 2115133 | 2011-11-10 20:39:08 -0500 | [diff] [blame] | 3956 | ret = btrfs_delayed_update_inode(trans, root, inode); |
| 3957 | if (!ret) |
| 3958 | btrfs_set_inode_last_trans(trans, inode); |
| 3959 | return ret; |
| 3960 | } |
| 3961 | |
| 3962 | return btrfs_update_inode_item(trans, root, inode); |
| 3963 | } |
| 3964 | |
Josef Bacik | be6aef6 | 2012-10-22 15:43:12 -0400 | [diff] [blame] | 3965 | noinline int btrfs_update_inode_fallback(struct btrfs_trans_handle *trans, |
| 3966 | struct btrfs_root *root, |
| 3967 | struct inode *inode) |
Chris Mason | 2115133 | 2011-11-10 20:39:08 -0500 | [diff] [blame] | 3968 | { |
| 3969 | int ret; |
| 3970 | |
| 3971 | ret = btrfs_update_inode(trans, root, inode); |
| 3972 | if (ret == -ENOSPC) |
| 3973 | return btrfs_update_inode_item(trans, root, inode); |
| 3974 | return ret; |
| 3975 | } |
| 3976 | |
| 3977 | /* |
Chris Mason | d352ac6 | 2008-09-29 15:18:18 -0400 | [diff] [blame] | 3978 | * unlink helper that gets used here in inode.c and in the tree logging |
| 3979 | * recovery code. It remove a link in a directory with a given name, and |
| 3980 | * also drops the back refs in the inode to the directory |
| 3981 | */ |
Al Viro | 9298679 | 2011-03-04 17:14:37 +0000 | [diff] [blame] | 3982 | static int __btrfs_unlink_inode(struct btrfs_trans_handle *trans, |
| 3983 | struct btrfs_root *root, |
Nikolay Borisov | 4ec5934 | 2017-01-18 00:31:44 +0200 | [diff] [blame] | 3984 | struct btrfs_inode *dir, |
| 3985 | struct btrfs_inode *inode, |
Al Viro | 9298679 | 2011-03-04 17:14:37 +0000 | [diff] [blame] | 3986 | const char *name, int name_len) |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3987 | { |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 3988 | struct btrfs_fs_info *fs_info = root->fs_info; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3989 | struct btrfs_path *path; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3990 | int ret = 0; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 3991 | struct extent_buffer *leaf; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3992 | struct btrfs_dir_item *di; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 3993 | struct btrfs_key key; |
Josef Bacik | aec7477 | 2008-07-24 12:12:38 -0400 | [diff] [blame] | 3994 | u64 index; |
Nikolay Borisov | 4ec5934 | 2017-01-18 00:31:44 +0200 | [diff] [blame] | 3995 | u64 ino = btrfs_ino(inode); |
| 3996 | u64 dir_ino = btrfs_ino(dir); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3997 | |
| 3998 | path = btrfs_alloc_path(); |
Chris Mason | 54aa1f4 | 2007-06-22 14:16:25 -0400 | [diff] [blame] | 3999 | if (!path) { |
| 4000 | ret = -ENOMEM; |
Tsutomu Itoh | 554233a | 2011-02-03 03:16:25 +0000 | [diff] [blame] | 4001 | goto out; |
Chris Mason | 54aa1f4 | 2007-06-22 14:16:25 -0400 | [diff] [blame] | 4002 | } |
| 4003 | |
Chris Mason | b947343 | 2009-03-13 11:00:37 -0400 | [diff] [blame] | 4004 | path->leave_spinning = 1; |
Li Zefan | 33345d01 | 2011-04-20 10:31:50 +0800 | [diff] [blame] | 4005 | di = btrfs_lookup_dir_item(trans, root, path, dir_ino, |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4006 | name, name_len, -1); |
| 4007 | if (IS_ERR(di)) { |
| 4008 | ret = PTR_ERR(di); |
| 4009 | goto err; |
| 4010 | } |
| 4011 | if (!di) { |
| 4012 | ret = -ENOENT; |
| 4013 | goto err; |
| 4014 | } |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 4015 | leaf = path->nodes[0]; |
| 4016 | btrfs_dir_item_key_to_cpu(leaf, di, &key); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4017 | ret = btrfs_delete_one_dir_name(trans, root, path, di); |
Chris Mason | 54aa1f4 | 2007-06-22 14:16:25 -0400 | [diff] [blame] | 4018 | if (ret) |
| 4019 | goto err; |
David Sterba | b3b4aa7 | 2011-04-21 01:20:15 +0200 | [diff] [blame] | 4020 | btrfs_release_path(path); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4021 | |
Miao Xie | 67de117 | 2013-12-26 13:07:06 +0800 | [diff] [blame] | 4022 | /* |
| 4023 | * If we don't have dir index, we have to get it by looking up |
| 4024 | * the inode ref, since we get the inode ref, remove it directly, |
| 4025 | * it is unnecessary to do delayed deletion. |
| 4026 | * |
| 4027 | * But if we have dir index, needn't search inode ref to get it. |
| 4028 | * Since the inode ref is close to the inode item, it is better |
| 4029 | * that we delay to delete it, and just do this deletion when |
| 4030 | * we update the inode item. |
| 4031 | */ |
Nikolay Borisov | 4ec5934 | 2017-01-18 00:31:44 +0200 | [diff] [blame] | 4032 | if (inode->dir_index) { |
| 4033 | ret = btrfs_delayed_delete_inode_ref(inode); |
Miao Xie | 67de117 | 2013-12-26 13:07:06 +0800 | [diff] [blame] | 4034 | if (!ret) { |
Nikolay Borisov | 4ec5934 | 2017-01-18 00:31:44 +0200 | [diff] [blame] | 4035 | index = inode->dir_index; |
Miao Xie | 67de117 | 2013-12-26 13:07:06 +0800 | [diff] [blame] | 4036 | goto skip_backref; |
| 4037 | } |
| 4038 | } |
| 4039 | |
Li Zefan | 33345d01 | 2011-04-20 10:31:50 +0800 | [diff] [blame] | 4040 | ret = btrfs_del_inode_ref(trans, root, name, name_len, ino, |
| 4041 | dir_ino, &index); |
Josef Bacik | aec7477 | 2008-07-24 12:12:38 -0400 | [diff] [blame] | 4042 | if (ret) { |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 4043 | btrfs_info(fs_info, |
Simon Kirby | c2cf52e | 2013-03-19 22:41:23 +0000 | [diff] [blame] | 4044 | "failed to delete reference to %.*s, inode %llu parent %llu", |
Geert Uytterhoeven | c1c9ff7 | 2013-08-20 13:20:07 +0200 | [diff] [blame] | 4045 | name_len, name, ino, dir_ino); |
Jeff Mahoney | 6664283 | 2016-06-10 18:19:25 -0400 | [diff] [blame] | 4046 | btrfs_abort_transaction(trans, ret); |
Josef Bacik | aec7477 | 2008-07-24 12:12:38 -0400 | [diff] [blame] | 4047 | goto err; |
| 4048 | } |
Miao Xie | 67de117 | 2013-12-26 13:07:06 +0800 | [diff] [blame] | 4049 | skip_backref: |
Nikolay Borisov | 4ec5934 | 2017-01-18 00:31:44 +0200 | [diff] [blame] | 4050 | ret = btrfs_delete_delayed_dir_index(trans, fs_info, dir, index); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 4051 | if (ret) { |
Jeff Mahoney | 6664283 | 2016-06-10 18:19:25 -0400 | [diff] [blame] | 4052 | btrfs_abort_transaction(trans, ret); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4053 | goto err; |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 4054 | } |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4055 | |
Nikolay Borisov | 4ec5934 | 2017-01-18 00:31:44 +0200 | [diff] [blame] | 4056 | ret = btrfs_del_inode_ref_in_log(trans, root, name, name_len, inode, |
| 4057 | dir_ino); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 4058 | if (ret != 0 && ret != -ENOENT) { |
Jeff Mahoney | 6664283 | 2016-06-10 18:19:25 -0400 | [diff] [blame] | 4059 | btrfs_abort_transaction(trans, ret); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 4060 | goto err; |
| 4061 | } |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 4062 | |
Nikolay Borisov | 4ec5934 | 2017-01-18 00:31:44 +0200 | [diff] [blame] | 4063 | ret = btrfs_del_dir_entries_in_log(trans, root, name, name_len, dir, |
| 4064 | index); |
Chris Mason | 6418c96 | 2010-10-30 07:34:24 -0400 | [diff] [blame] | 4065 | if (ret == -ENOENT) |
| 4066 | ret = 0; |
Zach Brown | d4e3991 | 2013-04-02 21:02:16 +0000 | [diff] [blame] | 4067 | else if (ret) |
Jeff Mahoney | 6664283 | 2016-06-10 18:19:25 -0400 | [diff] [blame] | 4068 | btrfs_abort_transaction(trans, ret); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4069 | err: |
| 4070 | btrfs_free_path(path); |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 4071 | if (ret) |
| 4072 | goto out; |
| 4073 | |
Nikolay Borisov | 4ec5934 | 2017-01-18 00:31:44 +0200 | [diff] [blame] | 4074 | btrfs_i_size_write(&dir->vfs_inode, |
| 4075 | dir->vfs_inode.i_size - name_len * 2); |
| 4076 | inode_inc_iversion(&inode->vfs_inode); |
| 4077 | inode_inc_iversion(&dir->vfs_inode); |
| 4078 | inode->vfs_inode.i_ctime = dir->vfs_inode.i_mtime = |
| 4079 | dir->vfs_inode.i_ctime = current_time(&inode->vfs_inode); |
| 4080 | ret = btrfs_update_inode(trans, root, &dir->vfs_inode); |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 4081 | out: |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4082 | return ret; |
| 4083 | } |
| 4084 | |
Al Viro | 9298679 | 2011-03-04 17:14:37 +0000 | [diff] [blame] | 4085 | int btrfs_unlink_inode(struct btrfs_trans_handle *trans, |
| 4086 | struct btrfs_root *root, |
Nikolay Borisov | 4ec5934 | 2017-01-18 00:31:44 +0200 | [diff] [blame] | 4087 | struct btrfs_inode *dir, struct btrfs_inode *inode, |
Al Viro | 9298679 | 2011-03-04 17:14:37 +0000 | [diff] [blame] | 4088 | const char *name, int name_len) |
| 4089 | { |
| 4090 | int ret; |
| 4091 | ret = __btrfs_unlink_inode(trans, root, dir, inode, name, name_len); |
| 4092 | if (!ret) { |
Nikolay Borisov | 4ec5934 | 2017-01-18 00:31:44 +0200 | [diff] [blame] | 4093 | drop_nlink(&inode->vfs_inode); |
| 4094 | ret = btrfs_update_inode(trans, root, &inode->vfs_inode); |
Al Viro | 9298679 | 2011-03-04 17:14:37 +0000 | [diff] [blame] | 4095 | } |
| 4096 | return ret; |
| 4097 | } |
Yan, Zheng | a22285a | 2010-05-16 10:48:46 -0400 | [diff] [blame] | 4098 | |
| 4099 | /* |
| 4100 | * helper to start transaction for unlink and rmdir. |
| 4101 | * |
Josef Bacik | d52be81 | 2013-05-29 14:54:47 -0400 | [diff] [blame] | 4102 | * unlink and rmdir are special in btrfs, they do not always free space, so |
| 4103 | * if we cannot make our reservations the normal way try and see if there is |
| 4104 | * plenty of slack room in the global reserve to migrate, otherwise we cannot |
| 4105 | * allow the unlink to occur. |
Yan, Zheng | a22285a | 2010-05-16 10:48:46 -0400 | [diff] [blame] | 4106 | */ |
Josef Bacik | d52be81 | 2013-05-29 14:54:47 -0400 | [diff] [blame] | 4107 | static struct btrfs_trans_handle *__unlink_start_trans(struct inode *dir) |
Yan, Zheng | a22285a | 2010-05-16 10:48:46 -0400 | [diff] [blame] | 4108 | { |
Yan, Zheng | a22285a | 2010-05-16 10:48:46 -0400 | [diff] [blame] | 4109 | struct btrfs_root *root = BTRFS_I(dir)->root; |
Yan, Zheng | a22285a | 2010-05-16 10:48:46 -0400 | [diff] [blame] | 4110 | |
Josef Bacik | e70bea5 | 2011-10-11 14:18:24 -0400 | [diff] [blame] | 4111 | /* |
| 4112 | * 1 for the possible orphan item |
| 4113 | * 1 for the dir item |
| 4114 | * 1 for the dir index |
| 4115 | * 1 for the inode ref |
Josef Bacik | e70bea5 | 2011-10-11 14:18:24 -0400 | [diff] [blame] | 4116 | * 1 for the inode |
| 4117 | */ |
Filipe Manana | 8eab77f | 2015-11-13 23:57:16 +0000 | [diff] [blame] | 4118 | return btrfs_start_transaction_fallback_global_rsv(root, 5, 5); |
Yan, Zheng | a22285a | 2010-05-16 10:48:46 -0400 | [diff] [blame] | 4119 | } |
| 4120 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4121 | static int btrfs_unlink(struct inode *dir, struct dentry *dentry) |
| 4122 | { |
Yan, Zheng | a22285a | 2010-05-16 10:48:46 -0400 | [diff] [blame] | 4123 | struct btrfs_root *root = BTRFS_I(dir)->root; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4124 | struct btrfs_trans_handle *trans; |
David Howells | 2b0143b | 2015-03-17 22:25:59 +0000 | [diff] [blame] | 4125 | struct inode *inode = d_inode(dentry); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4126 | int ret; |
| 4127 | |
Josef Bacik | d52be81 | 2013-05-29 14:54:47 -0400 | [diff] [blame] | 4128 | trans = __unlink_start_trans(dir); |
Yan, Zheng | a22285a | 2010-05-16 10:48:46 -0400 | [diff] [blame] | 4129 | if (IS_ERR(trans)) |
Josef Bacik | 5df6a9f | 2009-11-10 21:23:48 -0500 | [diff] [blame] | 4130 | return PTR_ERR(trans); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 4131 | |
Nikolay Borisov | 4ec5934 | 2017-01-18 00:31:44 +0200 | [diff] [blame] | 4132 | btrfs_record_unlink_dir(trans, BTRFS_I(dir), BTRFS_I(d_inode(dentry)), |
| 4133 | 0); |
Chris Mason | 12fcfd2 | 2009-03-24 10:24:20 -0400 | [diff] [blame] | 4134 | |
Nikolay Borisov | 4ec5934 | 2017-01-18 00:31:44 +0200 | [diff] [blame] | 4135 | ret = btrfs_unlink_inode(trans, root, BTRFS_I(dir), |
| 4136 | BTRFS_I(d_inode(dentry)), dentry->d_name.name, |
| 4137 | dentry->d_name.len); |
Tsutomu Itoh | b532402 | 2011-07-19 07:27:20 +0000 | [diff] [blame] | 4138 | if (ret) |
| 4139 | goto out; |
Josef Bacik | 7b12876 | 2008-07-24 12:17:14 -0400 | [diff] [blame] | 4140 | |
Yan, Zheng | a22285a | 2010-05-16 10:48:46 -0400 | [diff] [blame] | 4141 | if (inode->i_nlink == 0) { |
Josef Bacik | 7b12876 | 2008-07-24 12:17:14 -0400 | [diff] [blame] | 4142 | ret = btrfs_orphan_add(trans, inode); |
Tsutomu Itoh | b532402 | 2011-07-19 07:27:20 +0000 | [diff] [blame] | 4143 | if (ret) |
| 4144 | goto out; |
Yan, Zheng | a22285a | 2010-05-16 10:48:46 -0400 | [diff] [blame] | 4145 | } |
Josef Bacik | 7b12876 | 2008-07-24 12:17:14 -0400 | [diff] [blame] | 4146 | |
Tsutomu Itoh | b532402 | 2011-07-19 07:27:20 +0000 | [diff] [blame] | 4147 | out: |
Jeff Mahoney | 3a45bb2 | 2016-09-09 21:39:03 -0400 | [diff] [blame] | 4148 | btrfs_end_transaction(trans); |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 4149 | btrfs_btree_balance_dirty(root->fs_info); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4150 | return ret; |
| 4151 | } |
| 4152 | |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 4153 | int btrfs_unlink_subvol(struct btrfs_trans_handle *trans, |
| 4154 | struct btrfs_root *root, |
| 4155 | struct inode *dir, u64 objectid, |
| 4156 | const char *name, int name_len) |
| 4157 | { |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 4158 | struct btrfs_fs_info *fs_info = root->fs_info; |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 4159 | struct btrfs_path *path; |
| 4160 | struct extent_buffer *leaf; |
| 4161 | struct btrfs_dir_item *di; |
| 4162 | struct btrfs_key key; |
| 4163 | u64 index; |
| 4164 | int ret; |
Nikolay Borisov | 4a0cc7c | 2017-01-10 20:35:31 +0200 | [diff] [blame] | 4165 | u64 dir_ino = btrfs_ino(BTRFS_I(dir)); |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 4166 | |
| 4167 | path = btrfs_alloc_path(); |
| 4168 | if (!path) |
| 4169 | return -ENOMEM; |
| 4170 | |
Li Zefan | 33345d01 | 2011-04-20 10:31:50 +0800 | [diff] [blame] | 4171 | di = btrfs_lookup_dir_item(trans, root, path, dir_ino, |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 4172 | name, name_len, -1); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 4173 | if (IS_ERR_OR_NULL(di)) { |
| 4174 | if (!di) |
| 4175 | ret = -ENOENT; |
| 4176 | else |
| 4177 | ret = PTR_ERR(di); |
| 4178 | goto out; |
| 4179 | } |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 4180 | |
| 4181 | leaf = path->nodes[0]; |
| 4182 | btrfs_dir_item_key_to_cpu(leaf, di, &key); |
| 4183 | WARN_ON(key.type != BTRFS_ROOT_ITEM_KEY || key.objectid != objectid); |
| 4184 | ret = btrfs_delete_one_dir_name(trans, root, path, di); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 4185 | if (ret) { |
Jeff Mahoney | 6664283 | 2016-06-10 18:19:25 -0400 | [diff] [blame] | 4186 | btrfs_abort_transaction(trans, ret); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 4187 | goto out; |
| 4188 | } |
David Sterba | b3b4aa7 | 2011-04-21 01:20:15 +0200 | [diff] [blame] | 4189 | btrfs_release_path(path); |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 4190 | |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 4191 | ret = btrfs_del_root_ref(trans, fs_info, objectid, |
| 4192 | root->root_key.objectid, dir_ino, |
| 4193 | &index, name, name_len); |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 4194 | if (ret < 0) { |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 4195 | if (ret != -ENOENT) { |
Jeff Mahoney | 6664283 | 2016-06-10 18:19:25 -0400 | [diff] [blame] | 4196 | btrfs_abort_transaction(trans, ret); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 4197 | goto out; |
| 4198 | } |
Li Zefan | 33345d01 | 2011-04-20 10:31:50 +0800 | [diff] [blame] | 4199 | di = btrfs_search_dir_index_item(root, path, dir_ino, |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 4200 | name, name_len); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 4201 | if (IS_ERR_OR_NULL(di)) { |
| 4202 | if (!di) |
| 4203 | ret = -ENOENT; |
| 4204 | else |
| 4205 | ret = PTR_ERR(di); |
Jeff Mahoney | 6664283 | 2016-06-10 18:19:25 -0400 | [diff] [blame] | 4206 | btrfs_abort_transaction(trans, ret); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 4207 | goto out; |
| 4208 | } |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 4209 | |
| 4210 | leaf = path->nodes[0]; |
| 4211 | btrfs_item_key_to_cpu(leaf, &key, path->slots[0]); |
David Sterba | b3b4aa7 | 2011-04-21 01:20:15 +0200 | [diff] [blame] | 4212 | btrfs_release_path(path); |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 4213 | index = key.offset; |
| 4214 | } |
David Sterba | b3b4aa7 | 2011-04-21 01:20:15 +0200 | [diff] [blame] | 4215 | btrfs_release_path(path); |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 4216 | |
Nikolay Borisov | e67bbbb | 2017-01-10 20:35:36 +0200 | [diff] [blame] | 4217 | 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] | 4218 | if (ret) { |
Jeff Mahoney | 6664283 | 2016-06-10 18:19:25 -0400 | [diff] [blame] | 4219 | btrfs_abort_transaction(trans, ret); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 4220 | goto out; |
| 4221 | } |
Miao Xie | 16cdcec | 2011-04-22 18:12:22 +0800 | [diff] [blame] | 4222 | |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 4223 | btrfs_i_size_write(dir, dir->i_size - name_len * 2); |
Josef Bacik | 0c4d2d9 | 2012-04-05 15:03:02 -0400 | [diff] [blame] | 4224 | inode_inc_iversion(dir); |
Deepa Dinamani | c2050a4 | 2016-09-14 07:48:06 -0700 | [diff] [blame] | 4225 | dir->i_mtime = dir->i_ctime = current_time(dir); |
Josef Bacik | 5a24e84 | 2012-08-08 10:12:59 -0600 | [diff] [blame] | 4226 | ret = btrfs_update_inode_fallback(trans, root, dir); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 4227 | if (ret) |
Jeff Mahoney | 6664283 | 2016-06-10 18:19:25 -0400 | [diff] [blame] | 4228 | btrfs_abort_transaction(trans, ret); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 4229 | out: |
Josef Bacik | 71d7aed | 2011-06-14 14:24:32 -0400 | [diff] [blame] | 4230 | btrfs_free_path(path); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 4231 | return ret; |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 4232 | } |
| 4233 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4234 | static int btrfs_rmdir(struct inode *dir, struct dentry *dentry) |
| 4235 | { |
David Howells | 2b0143b | 2015-03-17 22:25:59 +0000 | [diff] [blame] | 4236 | struct inode *inode = d_inode(dentry); |
Chris Mason | 1832a6d | 2007-12-21 16:27:21 -0500 | [diff] [blame] | 4237 | int err = 0; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4238 | struct btrfs_root *root = BTRFS_I(dir)->root; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4239 | struct btrfs_trans_handle *trans; |
Filipe Manana | 44f714d | 2016-06-06 16:11:13 +0100 | [diff] [blame] | 4240 | u64 last_unlink_trans; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4241 | |
David Sterba | b3ae244 | 2012-09-13 16:04:34 -0600 | [diff] [blame] | 4242 | if (inode->i_size > BTRFS_EMPTY_DIR_SIZE) |
Yan | 134d451 | 2007-10-25 15:49:25 -0400 | [diff] [blame] | 4243 | return -ENOTEMPTY; |
Nikolay Borisov | 4a0cc7c | 2017-01-10 20:35:31 +0200 | [diff] [blame] | 4244 | if (btrfs_ino(BTRFS_I(inode)) == BTRFS_FIRST_FREE_OBJECTID) |
David Sterba | b3ae244 | 2012-09-13 16:04:34 -0600 | [diff] [blame] | 4245 | return -EPERM; |
Yan | 134d451 | 2007-10-25 15:49:25 -0400 | [diff] [blame] | 4246 | |
Josef Bacik | d52be81 | 2013-05-29 14:54:47 -0400 | [diff] [blame] | 4247 | trans = __unlink_start_trans(dir); |
Yan, Zheng | a22285a | 2010-05-16 10:48:46 -0400 | [diff] [blame] | 4248 | if (IS_ERR(trans)) |
Josef Bacik | 5df6a9f | 2009-11-10 21:23:48 -0500 | [diff] [blame] | 4249 | return PTR_ERR(trans); |
Josef Bacik | 5df6a9f | 2009-11-10 21:23:48 -0500 | [diff] [blame] | 4250 | |
Nikolay Borisov | 4a0cc7c | 2017-01-10 20:35:31 +0200 | [diff] [blame] | 4251 | if (unlikely(btrfs_ino(BTRFS_I(inode)) == BTRFS_EMPTY_SUBVOL_DIR_OBJECTID)) { |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 4252 | err = btrfs_unlink_subvol(trans, root, dir, |
| 4253 | BTRFS_I(inode)->location.objectid, |
| 4254 | dentry->d_name.name, |
| 4255 | dentry->d_name.len); |
| 4256 | goto out; |
| 4257 | } |
| 4258 | |
Josef Bacik | 7b12876 | 2008-07-24 12:17:14 -0400 | [diff] [blame] | 4259 | err = btrfs_orphan_add(trans, inode); |
| 4260 | if (err) |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 4261 | goto out; |
Josef Bacik | 7b12876 | 2008-07-24 12:17:14 -0400 | [diff] [blame] | 4262 | |
Filipe Manana | 44f714d | 2016-06-06 16:11:13 +0100 | [diff] [blame] | 4263 | last_unlink_trans = BTRFS_I(inode)->last_unlink_trans; |
| 4264 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4265 | /* now the directory is empty */ |
Nikolay Borisov | 4ec5934 | 2017-01-18 00:31:44 +0200 | [diff] [blame] | 4266 | err = btrfs_unlink_inode(trans, root, BTRFS_I(dir), |
| 4267 | BTRFS_I(d_inode(dentry)), dentry->d_name.name, |
| 4268 | dentry->d_name.len); |
Filipe Manana | 44f714d | 2016-06-06 16:11:13 +0100 | [diff] [blame] | 4269 | if (!err) { |
Chris Mason | dbe674a | 2008-07-17 12:54:05 -0400 | [diff] [blame] | 4270 | btrfs_i_size_write(inode, 0); |
Filipe Manana | 44f714d | 2016-06-06 16:11:13 +0100 | [diff] [blame] | 4271 | /* |
| 4272 | * Propagate the last_unlink_trans value of the deleted dir to |
| 4273 | * its parent directory. This is to prevent an unrecoverable |
| 4274 | * log tree in the case we do something like this: |
| 4275 | * 1) create dir foo |
| 4276 | * 2) create snapshot under dir foo |
| 4277 | * 3) delete the snapshot |
| 4278 | * 4) rmdir foo |
| 4279 | * 5) mkdir foo |
| 4280 | * 6) fsync foo or some file inside foo |
| 4281 | */ |
| 4282 | if (last_unlink_trans >= trans->transid) |
| 4283 | BTRFS_I(dir)->last_unlink_trans = last_unlink_trans; |
| 4284 | } |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 4285 | out: |
Jeff Mahoney | 3a45bb2 | 2016-09-09 21:39:03 -0400 | [diff] [blame] | 4286 | btrfs_end_transaction(trans); |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 4287 | btrfs_btree_balance_dirty(root->fs_info); |
Chris Mason | 3954401 | 2007-12-12 14:38:19 -0500 | [diff] [blame] | 4288 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4289 | return err; |
| 4290 | } |
| 4291 | |
Chris Mason | 28f75a0 | 2015-02-04 06:59:29 -0800 | [diff] [blame] | 4292 | static int truncate_space_check(struct btrfs_trans_handle *trans, |
| 4293 | struct btrfs_root *root, |
| 4294 | u64 bytes_deleted) |
| 4295 | { |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 4296 | struct btrfs_fs_info *fs_info = root->fs_info; |
Chris Mason | 28f75a0 | 2015-02-04 06:59:29 -0800 | [diff] [blame] | 4297 | int ret; |
| 4298 | |
Josef Bacik | dc95f7b | 2016-01-13 11:48:06 -0500 | [diff] [blame] | 4299 | /* |
| 4300 | * This is only used to apply pressure to the enospc system, we don't |
| 4301 | * intend to use this reservation at all. |
| 4302 | */ |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 4303 | bytes_deleted = btrfs_csum_bytes_to_leaves(fs_info, bytes_deleted); |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 4304 | bytes_deleted *= fs_info->nodesize; |
| 4305 | ret = btrfs_block_rsv_add(root, &fs_info->trans_block_rsv, |
Chris Mason | 28f75a0 | 2015-02-04 06:59:29 -0800 | [diff] [blame] | 4306 | bytes_deleted, BTRFS_RESERVE_NO_FLUSH); |
Josef Bacik | dc95f7b | 2016-01-13 11:48:06 -0500 | [diff] [blame] | 4307 | if (!ret) { |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 4308 | trace_btrfs_space_reservation(fs_info, "transaction", |
Josef Bacik | dc95f7b | 2016-01-13 11:48:06 -0500 | [diff] [blame] | 4309 | trans->transid, |
| 4310 | bytes_deleted, 1); |
Chris Mason | 28f75a0 | 2015-02-04 06:59:29 -0800 | [diff] [blame] | 4311 | trans->bytes_reserved += bytes_deleted; |
Josef Bacik | dc95f7b | 2016-01-13 11:48:06 -0500 | [diff] [blame] | 4312 | } |
Chris Mason | 28f75a0 | 2015-02-04 06:59:29 -0800 | [diff] [blame] | 4313 | return ret; |
| 4314 | |
| 4315 | } |
| 4316 | |
Filipe Manana | 0305cd5 | 2015-10-16 12:34:25 +0100 | [diff] [blame] | 4317 | static int truncate_inline_extent(struct inode *inode, |
| 4318 | struct btrfs_path *path, |
| 4319 | struct btrfs_key *found_key, |
| 4320 | const u64 item_end, |
| 4321 | const u64 new_size) |
| 4322 | { |
| 4323 | struct extent_buffer *leaf = path->nodes[0]; |
| 4324 | int slot = path->slots[0]; |
| 4325 | struct btrfs_file_extent_item *fi; |
| 4326 | u32 size = (u32)(new_size - found_key->offset); |
| 4327 | struct btrfs_root *root = BTRFS_I(inode)->root; |
| 4328 | |
| 4329 | fi = btrfs_item_ptr(leaf, slot, struct btrfs_file_extent_item); |
| 4330 | |
| 4331 | if (btrfs_file_extent_compression(leaf, fi) != BTRFS_COMPRESS_NONE) { |
| 4332 | loff_t offset = new_size; |
Kirill A. Shutemov | 09cbfea | 2016-04-01 15:29:47 +0300 | [diff] [blame] | 4333 | loff_t page_end = ALIGN(offset, PAGE_SIZE); |
Filipe Manana | 0305cd5 | 2015-10-16 12:34:25 +0100 | [diff] [blame] | 4334 | |
| 4335 | /* |
| 4336 | * Zero out the remaining of the last page of our inline extent, |
| 4337 | * instead of directly truncating our inline extent here - that |
| 4338 | * would be much more complex (decompressing all the data, then |
| 4339 | * compressing the truncated data, which might be bigger than |
| 4340 | * the size of the inline extent, resize the extent, etc). |
| 4341 | * We release the path because to get the page we might need to |
| 4342 | * read the extent item from disk (data not in the page cache). |
| 4343 | */ |
| 4344 | btrfs_release_path(path); |
Chandan Rajendra | 9703fef | 2016-01-21 15:55:56 +0530 | [diff] [blame] | 4345 | return btrfs_truncate_block(inode, offset, page_end - offset, |
| 4346 | 0); |
Filipe Manana | 0305cd5 | 2015-10-16 12:34:25 +0100 | [diff] [blame] | 4347 | } |
| 4348 | |
| 4349 | btrfs_set_file_extent_ram_bytes(leaf, fi, size); |
| 4350 | size = btrfs_file_extent_calc_inline_size(size); |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 4351 | btrfs_truncate_item(root->fs_info, path, size, 1); |
Filipe Manana | 0305cd5 | 2015-10-16 12:34:25 +0100 | [diff] [blame] | 4352 | |
| 4353 | if (test_bit(BTRFS_ROOT_REF_COWS, &root->state)) |
| 4354 | inode_sub_bytes(inode, item_end + 1 - new_size); |
| 4355 | |
| 4356 | return 0; |
| 4357 | } |
| 4358 | |
Chris Mason | 323ac95 | 2008-10-01 19:05:46 -0400 | [diff] [blame] | 4359 | /* |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4360 | * this can truncate away extent items, csum items and directory items. |
| 4361 | * 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] | 4362 | * any higher than new_size |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4363 | * |
| 4364 | * csum items that cross the new i_size are truncated to the new size |
| 4365 | * as well. |
Josef Bacik | 7b12876 | 2008-07-24 12:17:14 -0400 | [diff] [blame] | 4366 | * |
| 4367 | * min_type is the minimum key type to truncate down to. If set to 0, this |
| 4368 | * 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] | 4369 | */ |
Yan, Zheng | 8082510 | 2009-11-12 09:35:36 +0000 | [diff] [blame] | 4370 | int btrfs_truncate_inode_items(struct btrfs_trans_handle *trans, |
| 4371 | struct btrfs_root *root, |
| 4372 | struct inode *inode, |
| 4373 | u64 new_size, u32 min_type) |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4374 | { |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 4375 | struct btrfs_fs_info *fs_info = root->fs_info; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4376 | struct btrfs_path *path; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 4377 | struct extent_buffer *leaf; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4378 | struct btrfs_file_extent_item *fi; |
Yan, Zheng | 8082510 | 2009-11-12 09:35:36 +0000 | [diff] [blame] | 4379 | struct btrfs_key key; |
| 4380 | struct btrfs_key found_key; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4381 | u64 extent_start = 0; |
Chris Mason | db94535 | 2007-10-15 16:15:53 -0400 | [diff] [blame] | 4382 | u64 extent_num_bytes = 0; |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 4383 | u64 extent_offset = 0; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4384 | u64 item_end = 0; |
Filipe Manana | c1aa457 | 2015-06-20 18:20:09 +0100 | [diff] [blame] | 4385 | u64 last_size = new_size; |
Yan, Zheng | 8082510 | 2009-11-12 09:35:36 +0000 | [diff] [blame] | 4386 | u32 found_type = (u8)-1; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4387 | int found_extent; |
| 4388 | int del_item; |
Chris Mason | 85e21ba | 2008-01-29 15:11:36 -0500 | [diff] [blame] | 4389 | int pending_del_nr = 0; |
| 4390 | int pending_del_slot = 0; |
Chris Mason | 179e29e | 2007-11-01 11:28:41 -0400 | [diff] [blame] | 4391 | int extent_type = -1; |
Yan, Zheng | 8082510 | 2009-11-12 09:35:36 +0000 | [diff] [blame] | 4392 | int ret; |
| 4393 | int err = 0; |
Nikolay Borisov | 4a0cc7c | 2017-01-10 20:35:31 +0200 | [diff] [blame] | 4394 | u64 ino = btrfs_ino(BTRFS_I(inode)); |
Chris Mason | 28ed134 | 2014-12-17 09:41:04 -0800 | [diff] [blame] | 4395 | u64 bytes_deleted = 0; |
Josef Bacik | 1262133 | 2015-02-03 07:50:16 -0800 | [diff] [blame] | 4396 | bool be_nice = 0; |
| 4397 | bool should_throttle = 0; |
Chris Mason | 28f75a0 | 2015-02-04 06:59:29 -0800 | [diff] [blame] | 4398 | bool should_end = 0; |
Yan, Zheng | 8082510 | 2009-11-12 09:35:36 +0000 | [diff] [blame] | 4399 | |
| 4400 | BUG_ON(new_size > 0 && min_type != BTRFS_EXTENT_DATA_KEY); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4401 | |
Chris Mason | 28ed134 | 2014-12-17 09:41:04 -0800 | [diff] [blame] | 4402 | /* |
| 4403 | * for non-free space inodes and ref cows, we want to back off from |
| 4404 | * time to time |
| 4405 | */ |
| 4406 | if (!btrfs_is_free_space_inode(inode) && |
| 4407 | test_bit(BTRFS_ROOT_REF_COWS, &root->state)) |
| 4408 | be_nice = 1; |
| 4409 | |
Mark Fasheh | 0eb0e19 | 2011-07-12 16:44:10 -0700 | [diff] [blame] | 4410 | path = btrfs_alloc_path(); |
| 4411 | if (!path) |
| 4412 | return -ENOMEM; |
David Sterba | e4058b5 | 2015-11-27 16:31:35 +0100 | [diff] [blame] | 4413 | path->reada = READA_BACK; |
Mark Fasheh | 0eb0e19 | 2011-07-12 16:44:10 -0700 | [diff] [blame] | 4414 | |
Josef Bacik | 5dc562c | 2012-08-17 13:14:17 -0400 | [diff] [blame] | 4415 | /* |
| 4416 | * We want to drop from the next block forward in case this new size is |
| 4417 | * not block aligned since we will be keeping the last block of the |
| 4418 | * extent just the way it is. |
| 4419 | */ |
Miao Xie | 27cdeb7 | 2014-04-02 19:51:05 +0800 | [diff] [blame] | 4420 | if (test_bit(BTRFS_ROOT_REF_COWS, &root->state) || |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 4421 | root == fs_info->tree_root) |
Qu Wenruo | fda2832 | 2013-02-26 08:10:22 +0000 | [diff] [blame] | 4422 | btrfs_drop_extent_cache(inode, ALIGN(new_size, |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 4423 | fs_info->sectorsize), |
Jeff Mahoney | da17066 | 2016-06-15 09:22:56 -0400 | [diff] [blame] | 4424 | (u64)-1, 0); |
Yan, Zheng | 8082510 | 2009-11-12 09:35:36 +0000 | [diff] [blame] | 4425 | |
Miao Xie | 16cdcec | 2011-04-22 18:12:22 +0800 | [diff] [blame] | 4426 | /* |
| 4427 | * This function is also used to drop the items in the log tree before |
| 4428 | * we relog the inode, so if root != BTRFS_I(inode)->root, it means |
| 4429 | * it is used to drop the loged items. So we shouldn't kill the delayed |
| 4430 | * items. |
| 4431 | */ |
| 4432 | if (min_type == 0 && root == BTRFS_I(inode)->root) |
Nikolay Borisov | 4ccb5c7 | 2017-01-10 20:35:38 +0200 | [diff] [blame] | 4433 | btrfs_kill_delayed_inode_items(BTRFS_I(inode)); |
Miao Xie | 16cdcec | 2011-04-22 18:12:22 +0800 | [diff] [blame] | 4434 | |
Li Zefan | 33345d01 | 2011-04-20 10:31:50 +0800 | [diff] [blame] | 4435 | key.objectid = ino; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4436 | key.offset = (u64)-1; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 4437 | key.type = (u8)-1; |
| 4438 | |
Chris Mason | 85e21ba | 2008-01-29 15:11:36 -0500 | [diff] [blame] | 4439 | search_again: |
Chris Mason | 28ed134 | 2014-12-17 09:41:04 -0800 | [diff] [blame] | 4440 | /* |
| 4441 | * with a 16K leaf size and 128MB extents, you can actually queue |
| 4442 | * up a huge file in a single leaf. Most of the time that |
| 4443 | * bytes_deleted is > 0, it will be huge by the time we get here |
| 4444 | */ |
Byongho Lee | ee22184 | 2015-12-15 01:42:10 +0900 | [diff] [blame] | 4445 | if (be_nice && bytes_deleted > SZ_32M) { |
Jeff Mahoney | 3a45bb2 | 2016-09-09 21:39:03 -0400 | [diff] [blame] | 4446 | if (btrfs_should_end_transaction(trans)) { |
Chris Mason | 28ed134 | 2014-12-17 09:41:04 -0800 | [diff] [blame] | 4447 | err = -EAGAIN; |
| 4448 | goto error; |
| 4449 | } |
| 4450 | } |
| 4451 | |
| 4452 | |
Chris Mason | b947343 | 2009-03-13 11:00:37 -0400 | [diff] [blame] | 4453 | path->leave_spinning = 1; |
Chris Mason | 85e21ba | 2008-01-29 15:11:36 -0500 | [diff] [blame] | 4454 | ret = btrfs_search_slot(trans, root, &key, path, -1, 1); |
Yan, Zheng | 8082510 | 2009-11-12 09:35:36 +0000 | [diff] [blame] | 4455 | if (ret < 0) { |
| 4456 | err = ret; |
| 4457 | goto out; |
| 4458 | } |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 4459 | |
Chris Mason | 85e21ba | 2008-01-29 15:11:36 -0500 | [diff] [blame] | 4460 | if (ret > 0) { |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 4461 | /* there are no items in the tree for us to truncate, we're |
| 4462 | * done |
| 4463 | */ |
Yan, Zheng | 8082510 | 2009-11-12 09:35:36 +0000 | [diff] [blame] | 4464 | if (path->slots[0] == 0) |
| 4465 | goto out; |
Chris Mason | 85e21ba | 2008-01-29 15:11:36 -0500 | [diff] [blame] | 4466 | path->slots[0]--; |
| 4467 | } |
| 4468 | |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 4469 | while (1) { |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4470 | fi = NULL; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 4471 | leaf = path->nodes[0]; |
| 4472 | btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]); |
David Sterba | 962a298 | 2014-06-04 18:41:45 +0200 | [diff] [blame] | 4473 | found_type = found_key.type; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4474 | |
Li Zefan | 33345d01 | 2011-04-20 10:31:50 +0800 | [diff] [blame] | 4475 | if (found_key.objectid != ino) |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4476 | break; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 4477 | |
Chris Mason | 85e21ba | 2008-01-29 15:11:36 -0500 | [diff] [blame] | 4478 | if (found_type < min_type) |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4479 | break; |
| 4480 | |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 4481 | item_end = found_key.offset; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4482 | if (found_type == BTRFS_EXTENT_DATA_KEY) { |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 4483 | fi = btrfs_item_ptr(leaf, path->slots[0], |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4484 | struct btrfs_file_extent_item); |
Chris Mason | 179e29e | 2007-11-01 11:28:41 -0400 | [diff] [blame] | 4485 | extent_type = btrfs_file_extent_type(leaf, fi); |
| 4486 | if (extent_type != BTRFS_FILE_EXTENT_INLINE) { |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 4487 | item_end += |
Chris Mason | db94535 | 2007-10-15 16:15:53 -0400 | [diff] [blame] | 4488 | btrfs_file_extent_num_bytes(leaf, fi); |
Chris Mason | 179e29e | 2007-11-01 11:28:41 -0400 | [diff] [blame] | 4489 | } else if (extent_type == BTRFS_FILE_EXTENT_INLINE) { |
Chris Mason | 179e29e | 2007-11-01 11:28:41 -0400 | [diff] [blame] | 4490 | item_end += btrfs_file_extent_inline_len(leaf, |
Chris Mason | 514ac8a | 2014-01-03 21:07:00 -0800 | [diff] [blame] | 4491 | path->slots[0], fi); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4492 | } |
Yan | 008630c | 2007-11-07 13:31:09 -0500 | [diff] [blame] | 4493 | item_end--; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4494 | } |
Yan, Zheng | 8082510 | 2009-11-12 09:35:36 +0000 | [diff] [blame] | 4495 | if (found_type > min_type) { |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4496 | del_item = 1; |
Yan, Zheng | 8082510 | 2009-11-12 09:35:36 +0000 | [diff] [blame] | 4497 | } else { |
Liu Bo | 91298ee | 2016-12-01 13:43:31 -0800 | [diff] [blame] | 4498 | if (item_end < new_size) { |
| 4499 | /* |
| 4500 | * With NO_HOLES mode, for the following mapping |
| 4501 | * |
| 4502 | * [0-4k][hole][8k-12k] |
| 4503 | * |
| 4504 | * if truncating isize down to 6k, it ends up |
| 4505 | * isize being 8k. |
| 4506 | */ |
| 4507 | if (btrfs_fs_incompat(root->fs_info, NO_HOLES)) |
| 4508 | last_size = new_size; |
Yan, Zheng | 8082510 | 2009-11-12 09:35:36 +0000 | [diff] [blame] | 4509 | break; |
Liu Bo | 91298ee | 2016-12-01 13:43:31 -0800 | [diff] [blame] | 4510 | } |
Yan, Zheng | 8082510 | 2009-11-12 09:35:36 +0000 | [diff] [blame] | 4511 | if (found_key.offset >= new_size) |
| 4512 | del_item = 1; |
| 4513 | else |
| 4514 | del_item = 0; |
| 4515 | } |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4516 | found_extent = 0; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4517 | /* FIXME, shrink the extent if the ref count is only 1 */ |
Chris Mason | 179e29e | 2007-11-01 11:28:41 -0400 | [diff] [blame] | 4518 | if (found_type != BTRFS_EXTENT_DATA_KEY) |
| 4519 | goto delete; |
| 4520 | |
Josef Bacik | 7f4f6e0 | 2013-08-29 16:43:28 -0400 | [diff] [blame] | 4521 | if (del_item) |
| 4522 | last_size = found_key.offset; |
| 4523 | else |
| 4524 | last_size = new_size; |
| 4525 | |
Chris Mason | 179e29e | 2007-11-01 11:28:41 -0400 | [diff] [blame] | 4526 | if (extent_type != BTRFS_FILE_EXTENT_INLINE) { |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4527 | u64 num_dec; |
Chris Mason | db94535 | 2007-10-15 16:15:53 -0400 | [diff] [blame] | 4528 | extent_start = btrfs_file_extent_disk_bytenr(leaf, fi); |
Miao Xie | f70a9a6b | 2012-01-12 19:10:12 -0500 | [diff] [blame] | 4529 | if (!del_item) { |
Chris Mason | db94535 | 2007-10-15 16:15:53 -0400 | [diff] [blame] | 4530 | u64 orig_num_bytes = |
| 4531 | btrfs_file_extent_num_bytes(leaf, fi); |
Qu Wenruo | fda2832 | 2013-02-26 08:10:22 +0000 | [diff] [blame] | 4532 | extent_num_bytes = ALIGN(new_size - |
| 4533 | found_key.offset, |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 4534 | fs_info->sectorsize); |
Chris Mason | db94535 | 2007-10-15 16:15:53 -0400 | [diff] [blame] | 4535 | btrfs_set_file_extent_num_bytes(leaf, fi, |
| 4536 | extent_num_bytes); |
| 4537 | num_dec = (orig_num_bytes - |
Chris Mason | 9069218 | 2008-02-08 13:49:28 -0500 | [diff] [blame] | 4538 | extent_num_bytes); |
Miao Xie | 27cdeb7 | 2014-04-02 19:51:05 +0800 | [diff] [blame] | 4539 | if (test_bit(BTRFS_ROOT_REF_COWS, |
| 4540 | &root->state) && |
| 4541 | extent_start != 0) |
Yan Zheng | a76a3cd | 2008-10-09 11:46:29 -0400 | [diff] [blame] | 4542 | inode_sub_bytes(inode, num_dec); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 4543 | btrfs_mark_buffer_dirty(leaf); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4544 | } else { |
Chris Mason | db94535 | 2007-10-15 16:15:53 -0400 | [diff] [blame] | 4545 | extent_num_bytes = |
| 4546 | btrfs_file_extent_disk_num_bytes(leaf, |
| 4547 | fi); |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 4548 | extent_offset = found_key.offset - |
| 4549 | btrfs_file_extent_offset(leaf, fi); |
| 4550 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4551 | /* FIXME blocksize != 4096 */ |
Chris Mason | 9069218 | 2008-02-08 13:49:28 -0500 | [diff] [blame] | 4552 | num_dec = btrfs_file_extent_num_bytes(leaf, fi); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4553 | if (extent_start != 0) { |
| 4554 | found_extent = 1; |
Miao Xie | 27cdeb7 | 2014-04-02 19:51:05 +0800 | [diff] [blame] | 4555 | if (test_bit(BTRFS_ROOT_REF_COWS, |
| 4556 | &root->state)) |
Yan Zheng | a76a3cd | 2008-10-09 11:46:29 -0400 | [diff] [blame] | 4557 | inode_sub_bytes(inode, num_dec); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4558 | } |
| 4559 | } |
Chris Mason | 9069218 | 2008-02-08 13:49:28 -0500 | [diff] [blame] | 4560 | } else if (extent_type == BTRFS_FILE_EXTENT_INLINE) { |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 4561 | /* |
| 4562 | * we can't truncate inline items that have had |
| 4563 | * special encodings |
| 4564 | */ |
| 4565 | if (!del_item && |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 4566 | btrfs_file_extent_encryption(leaf, fi) == 0 && |
| 4567 | btrfs_file_extent_other_encoding(leaf, fi) == 0) { |
Chris Mason | 514ac8a | 2014-01-03 21:07:00 -0800 | [diff] [blame] | 4568 | |
| 4569 | /* |
Filipe Manana | 0305cd5 | 2015-10-16 12:34:25 +0100 | [diff] [blame] | 4570 | * Need to release path in order to truncate a |
| 4571 | * compressed extent. So delete any accumulated |
| 4572 | * extent items so far. |
Chris Mason | 514ac8a | 2014-01-03 21:07:00 -0800 | [diff] [blame] | 4573 | */ |
Filipe Manana | 0305cd5 | 2015-10-16 12:34:25 +0100 | [diff] [blame] | 4574 | if (btrfs_file_extent_compression(leaf, fi) != |
| 4575 | BTRFS_COMPRESS_NONE && pending_del_nr) { |
| 4576 | err = btrfs_del_items(trans, root, path, |
| 4577 | pending_del_slot, |
| 4578 | pending_del_nr); |
| 4579 | if (err) { |
| 4580 | btrfs_abort_transaction(trans, |
Filipe Manana | 0305cd5 | 2015-10-16 12:34:25 +0100 | [diff] [blame] | 4581 | err); |
| 4582 | goto error; |
| 4583 | } |
| 4584 | pending_del_nr = 0; |
| 4585 | } |
| 4586 | |
| 4587 | err = truncate_inline_extent(inode, path, |
| 4588 | &found_key, |
| 4589 | item_end, |
| 4590 | new_size); |
| 4591 | if (err) { |
Jeff Mahoney | 6664283 | 2016-06-10 18:19:25 -0400 | [diff] [blame] | 4592 | btrfs_abort_transaction(trans, err); |
Filipe Manana | 0305cd5 | 2015-10-16 12:34:25 +0100 | [diff] [blame] | 4593 | goto error; |
| 4594 | } |
Miao Xie | 27cdeb7 | 2014-04-02 19:51:05 +0800 | [diff] [blame] | 4595 | } else if (test_bit(BTRFS_ROOT_REF_COWS, |
| 4596 | &root->state)) { |
Filipe Manana | 0305cd5 | 2015-10-16 12:34:25 +0100 | [diff] [blame] | 4597 | inode_sub_bytes(inode, item_end + 1 - new_size); |
Chris Mason | 9069218 | 2008-02-08 13:49:28 -0500 | [diff] [blame] | 4598 | } |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4599 | } |
Chris Mason | 179e29e | 2007-11-01 11:28:41 -0400 | [diff] [blame] | 4600 | delete: |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4601 | if (del_item) { |
Chris Mason | 85e21ba | 2008-01-29 15:11:36 -0500 | [diff] [blame] | 4602 | if (!pending_del_nr) { |
| 4603 | /* no pending yet, add ourselves */ |
| 4604 | pending_del_slot = path->slots[0]; |
| 4605 | pending_del_nr = 1; |
| 4606 | } else if (pending_del_nr && |
| 4607 | path->slots[0] + 1 == pending_del_slot) { |
| 4608 | /* hop on the pending chunk */ |
| 4609 | pending_del_nr++; |
| 4610 | pending_del_slot = path->slots[0]; |
| 4611 | } else { |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 4612 | BUG(); |
Chris Mason | 85e21ba | 2008-01-29 15:11:36 -0500 | [diff] [blame] | 4613 | } |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4614 | } else { |
| 4615 | break; |
| 4616 | } |
Chris Mason | 28f75a0 | 2015-02-04 06:59:29 -0800 | [diff] [blame] | 4617 | should_throttle = 0; |
| 4618 | |
Miao Xie | 27cdeb7 | 2014-04-02 19:51:05 +0800 | [diff] [blame] | 4619 | if (found_extent && |
| 4620 | (test_bit(BTRFS_ROOT_REF_COWS, &root->state) || |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 4621 | root == fs_info->tree_root)) { |
Chris Mason | b947343 | 2009-03-13 11:00:37 -0400 | [diff] [blame] | 4622 | btrfs_set_path_blocking(path); |
Chris Mason | 28ed134 | 2014-12-17 09:41:04 -0800 | [diff] [blame] | 4623 | bytes_deleted += extent_num_bytes; |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 4624 | ret = btrfs_free_extent(trans, fs_info, extent_start, |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 4625 | extent_num_bytes, 0, |
| 4626 | btrfs_header_owner(leaf), |
Filipe Manana | b06c4bf | 2015-10-23 07:52:54 +0100 | [diff] [blame] | 4627 | ino, extent_offset); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4628 | BUG_ON(ret); |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 4629 | if (btrfs_should_throttle_delayed_refs(trans, fs_info)) |
| 4630 | btrfs_async_run_delayed_refs(fs_info, |
Wang Xiaoguang | dd4b857 | 2016-10-18 15:56:13 +0800 | [diff] [blame] | 4631 | trans->delayed_ref_updates * 2, |
| 4632 | trans->transid, 0); |
Chris Mason | 28f75a0 | 2015-02-04 06:59:29 -0800 | [diff] [blame] | 4633 | if (be_nice) { |
| 4634 | if (truncate_space_check(trans, root, |
| 4635 | extent_num_bytes)) { |
| 4636 | should_end = 1; |
| 4637 | } |
| 4638 | if (btrfs_should_throttle_delayed_refs(trans, |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 4639 | fs_info)) |
Chris Mason | 28f75a0 | 2015-02-04 06:59:29 -0800 | [diff] [blame] | 4640 | should_throttle = 1; |
Chris Mason | 28f75a0 | 2015-02-04 06:59:29 -0800 | [diff] [blame] | 4641 | } |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4642 | } |
Chris Mason | 85e21ba | 2008-01-29 15:11:36 -0500 | [diff] [blame] | 4643 | |
Yan, Zheng | 8082510 | 2009-11-12 09:35:36 +0000 | [diff] [blame] | 4644 | if (found_type == BTRFS_INODE_ITEM_KEY) |
| 4645 | break; |
| 4646 | |
| 4647 | if (path->slots[0] == 0 || |
Josef Bacik | 1262133 | 2015-02-03 07:50:16 -0800 | [diff] [blame] | 4648 | path->slots[0] != pending_del_slot || |
Chris Mason | 28f75a0 | 2015-02-04 06:59:29 -0800 | [diff] [blame] | 4649 | should_throttle || should_end) { |
Yan, Zheng | 8082510 | 2009-11-12 09:35:36 +0000 | [diff] [blame] | 4650 | if (pending_del_nr) { |
| 4651 | ret = btrfs_del_items(trans, root, path, |
| 4652 | pending_del_slot, |
| 4653 | pending_del_nr); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 4654 | if (ret) { |
Jeff Mahoney | 6664283 | 2016-06-10 18:19:25 -0400 | [diff] [blame] | 4655 | btrfs_abort_transaction(trans, ret); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 4656 | goto error; |
| 4657 | } |
Yan, Zheng | 8082510 | 2009-11-12 09:35:36 +0000 | [diff] [blame] | 4658 | pending_del_nr = 0; |
| 4659 | } |
David Sterba | b3b4aa7 | 2011-04-21 01:20:15 +0200 | [diff] [blame] | 4660 | btrfs_release_path(path); |
Chris Mason | 28f75a0 | 2015-02-04 06:59:29 -0800 | [diff] [blame] | 4661 | if (should_throttle) { |
Josef Bacik | 1262133 | 2015-02-03 07:50:16 -0800 | [diff] [blame] | 4662 | unsigned long updates = trans->delayed_ref_updates; |
| 4663 | if (updates) { |
| 4664 | trans->delayed_ref_updates = 0; |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 4665 | ret = btrfs_run_delayed_refs(trans, |
| 4666 | fs_info, |
| 4667 | updates * 2); |
Josef Bacik | 1262133 | 2015-02-03 07:50:16 -0800 | [diff] [blame] | 4668 | if (ret && !err) |
| 4669 | err = ret; |
| 4670 | } |
| 4671 | } |
Chris Mason | 28f75a0 | 2015-02-04 06:59:29 -0800 | [diff] [blame] | 4672 | /* |
| 4673 | * if we failed to refill our space rsv, bail out |
| 4674 | * and let the transaction restart |
| 4675 | */ |
| 4676 | if (should_end) { |
| 4677 | err = -EAGAIN; |
| 4678 | goto error; |
| 4679 | } |
Chris Mason | 85e21ba | 2008-01-29 15:11:36 -0500 | [diff] [blame] | 4680 | goto search_again; |
Yan, Zheng | 8082510 | 2009-11-12 09:35:36 +0000 | [diff] [blame] | 4681 | } else { |
| 4682 | path->slots[0]--; |
Chris Mason | 85e21ba | 2008-01-29 15:11:36 -0500 | [diff] [blame] | 4683 | } |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4684 | } |
Yan, Zheng | 8082510 | 2009-11-12 09:35:36 +0000 | [diff] [blame] | 4685 | out: |
Chris Mason | 85e21ba | 2008-01-29 15:11:36 -0500 | [diff] [blame] | 4686 | if (pending_del_nr) { |
| 4687 | ret = btrfs_del_items(trans, root, path, pending_del_slot, |
| 4688 | pending_del_nr); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 4689 | if (ret) |
Jeff Mahoney | 6664283 | 2016-06-10 18:19:25 -0400 | [diff] [blame] | 4690 | btrfs_abort_transaction(trans, ret); |
Chris Mason | 85e21ba | 2008-01-29 15:11:36 -0500 | [diff] [blame] | 4691 | } |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 4692 | error: |
Filipe Manana | c1aa457 | 2015-06-20 18:20:09 +0100 | [diff] [blame] | 4693 | if (root->root_key.objectid != BTRFS_TREE_LOG_OBJECTID) |
Josef Bacik | 7f4f6e0 | 2013-08-29 16:43:28 -0400 | [diff] [blame] | 4694 | btrfs_ordered_update_i_size(inode, last_size, NULL); |
Chris Mason | 28ed134 | 2014-12-17 09:41:04 -0800 | [diff] [blame] | 4695 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4696 | btrfs_free_path(path); |
Chris Mason | 28ed134 | 2014-12-17 09:41:04 -0800 | [diff] [blame] | 4697 | |
Liu Bo | 19fd2df | 2016-12-01 13:01:02 -0800 | [diff] [blame] | 4698 | if (err == 0) { |
| 4699 | /* only inline file may have last_size != new_size */ |
| 4700 | if (new_size >= fs_info->sectorsize || |
| 4701 | new_size > fs_info->max_inline) |
| 4702 | ASSERT(last_size == new_size); |
| 4703 | } |
| 4704 | |
Byongho Lee | ee22184 | 2015-12-15 01:42:10 +0900 | [diff] [blame] | 4705 | if (be_nice && bytes_deleted > SZ_32M) { |
Chris Mason | 28ed134 | 2014-12-17 09:41:04 -0800 | [diff] [blame] | 4706 | unsigned long updates = trans->delayed_ref_updates; |
| 4707 | if (updates) { |
| 4708 | trans->delayed_ref_updates = 0; |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 4709 | ret = btrfs_run_delayed_refs(trans, fs_info, |
| 4710 | updates * 2); |
Chris Mason | 28ed134 | 2014-12-17 09:41:04 -0800 | [diff] [blame] | 4711 | if (ret && !err) |
| 4712 | err = ret; |
| 4713 | } |
| 4714 | } |
Yan, Zheng | 8082510 | 2009-11-12 09:35:36 +0000 | [diff] [blame] | 4715 | return err; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4716 | } |
| 4717 | |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 4718 | /* |
Chandan Rajendra | 9703fef | 2016-01-21 15:55:56 +0530 | [diff] [blame] | 4719 | * btrfs_truncate_block - read, zero a chunk and write a block |
Josef Bacik | 2aaa665 | 2012-08-29 14:27:18 -0400 | [diff] [blame] | 4720 | * @inode - inode that we're zeroing |
| 4721 | * @from - the offset to start zeroing |
| 4722 | * @len - the length to zero, 0 to zero the entire range respective to the |
| 4723 | * offset |
| 4724 | * @front - zero up to the offset instead of from the offset on |
| 4725 | * |
Chandan Rajendra | 9703fef | 2016-01-21 15:55:56 +0530 | [diff] [blame] | 4726 | * 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] | 4727 | * 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] | 4728 | */ |
Chandan Rajendra | 9703fef | 2016-01-21 15:55:56 +0530 | [diff] [blame] | 4729 | 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] | 4730 | int front) |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 4731 | { |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 4732 | struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb); |
Josef Bacik | 2aaa665 | 2012-08-29 14:27:18 -0400 | [diff] [blame] | 4733 | struct address_space *mapping = inode->i_mapping; |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 4734 | struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree; |
| 4735 | struct btrfs_ordered_extent *ordered; |
Josef Bacik | 2ac55d4 | 2010-02-03 19:33:23 +0000 | [diff] [blame] | 4736 | struct extent_state *cached_state = NULL; |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 4737 | char *kaddr; |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 4738 | u32 blocksize = fs_info->sectorsize; |
Kirill A. Shutemov | 09cbfea | 2016-04-01 15:29:47 +0300 | [diff] [blame] | 4739 | pgoff_t index = from >> PAGE_SHIFT; |
Chandan Rajendra | 9703fef | 2016-01-21 15:55:56 +0530 | [diff] [blame] | 4740 | unsigned offset = from & (blocksize - 1); |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 4741 | struct page *page; |
Josef Bacik | 3b16a4e | 2011-09-21 15:05:58 -0400 | [diff] [blame] | 4742 | gfp_t mask = btrfs_alloc_write_mask(mapping); |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 4743 | int ret = 0; |
Chandan Rajendra | 9703fef | 2016-01-21 15:55:56 +0530 | [diff] [blame] | 4744 | u64 block_start; |
| 4745 | u64 block_end; |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 4746 | |
Josef Bacik | 2aaa665 | 2012-08-29 14:27:18 -0400 | [diff] [blame] | 4747 | if ((offset & (blocksize - 1)) == 0 && |
| 4748 | (!len || ((len & (blocksize - 1)) == 0))) |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 4749 | goto out; |
Chandan Rajendra | 9703fef | 2016-01-21 15:55:56 +0530 | [diff] [blame] | 4750 | |
Qu Wenruo | 7cf5b97 | 2015-09-08 17:25:55 +0800 | [diff] [blame] | 4751 | ret = btrfs_delalloc_reserve_space(inode, |
Chandan Rajendra | 9703fef | 2016-01-21 15:55:56 +0530 | [diff] [blame] | 4752 | round_down(from, blocksize), blocksize); |
Josef Bacik | 5d5e103 | 2009-10-13 16:46:49 -0400 | [diff] [blame] | 4753 | if (ret) |
| 4754 | goto out; |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 4755 | |
Chris Mason | 211c17f | 2008-05-15 09:13:45 -0400 | [diff] [blame] | 4756 | again: |
Josef Bacik | 3b16a4e | 2011-09-21 15:05:58 -0400 | [diff] [blame] | 4757 | page = find_or_create_page(mapping, index, mask); |
Josef Bacik | 5d5e103 | 2009-10-13 16:46:49 -0400 | [diff] [blame] | 4758 | if (!page) { |
Qu Wenruo | 7cf5b97 | 2015-09-08 17:25:55 +0800 | [diff] [blame] | 4759 | btrfs_delalloc_release_space(inode, |
Chandan Rajendra | 9703fef | 2016-01-21 15:55:56 +0530 | [diff] [blame] | 4760 | round_down(from, blocksize), |
| 4761 | blocksize); |
Miao Xie | ac6a2b3 | 2012-12-05 10:56:13 +0000 | [diff] [blame] | 4762 | ret = -ENOMEM; |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 4763 | goto out; |
Josef Bacik | 5d5e103 | 2009-10-13 16:46:49 -0400 | [diff] [blame] | 4764 | } |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 4765 | |
Chandan Rajendra | 9703fef | 2016-01-21 15:55:56 +0530 | [diff] [blame] | 4766 | block_start = round_down(from, blocksize); |
| 4767 | block_end = block_start + blocksize - 1; |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 4768 | |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 4769 | if (!PageUptodate(page)) { |
| 4770 | ret = btrfs_readpage(NULL, page); |
| 4771 | lock_page(page); |
Chris Mason | 211c17f | 2008-05-15 09:13:45 -0400 | [diff] [blame] | 4772 | if (page->mapping != mapping) { |
| 4773 | unlock_page(page); |
Kirill A. Shutemov | 09cbfea | 2016-04-01 15:29:47 +0300 | [diff] [blame] | 4774 | put_page(page); |
Chris Mason | 211c17f | 2008-05-15 09:13:45 -0400 | [diff] [blame] | 4775 | goto again; |
| 4776 | } |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 4777 | if (!PageUptodate(page)) { |
| 4778 | ret = -EIO; |
Chris Mason | 8964222 | 2008-07-24 09:41:53 -0400 | [diff] [blame] | 4779 | goto out_unlock; |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 4780 | } |
| 4781 | } |
Chris Mason | 211c17f | 2008-05-15 09:13:45 -0400 | [diff] [blame] | 4782 | wait_on_page_writeback(page); |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 4783 | |
Chandan Rajendra | 9703fef | 2016-01-21 15:55:56 +0530 | [diff] [blame] | 4784 | lock_extent_bits(io_tree, block_start, block_end, &cached_state); |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 4785 | set_page_extent_mapped(page); |
| 4786 | |
Chandan Rajendra | 9703fef | 2016-01-21 15:55:56 +0530 | [diff] [blame] | 4787 | ordered = btrfs_lookup_ordered_extent(inode, block_start); |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 4788 | if (ordered) { |
Chandan Rajendra | 9703fef | 2016-01-21 15:55:56 +0530 | [diff] [blame] | 4789 | unlock_extent_cached(io_tree, block_start, block_end, |
Josef Bacik | 2ac55d4 | 2010-02-03 19:33:23 +0000 | [diff] [blame] | 4790 | &cached_state, GFP_NOFS); |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 4791 | unlock_page(page); |
Kirill A. Shutemov | 09cbfea | 2016-04-01 15:29:47 +0300 | [diff] [blame] | 4792 | put_page(page); |
Chris Mason | eb84ae0 | 2008-07-17 13:53:27 -0400 | [diff] [blame] | 4793 | btrfs_start_ordered_extent(inode, ordered, 1); |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 4794 | btrfs_put_ordered_extent(ordered); |
| 4795 | goto again; |
| 4796 | } |
| 4797 | |
Chandan Rajendra | 9703fef | 2016-01-21 15:55:56 +0530 | [diff] [blame] | 4798 | clear_extent_bit(&BTRFS_I(inode)->io_tree, block_start, block_end, |
Liu Bo | 9e8a4a8 | 2012-09-05 19:10:51 -0600 | [diff] [blame] | 4799 | EXTENT_DIRTY | EXTENT_DELALLOC | |
| 4800 | EXTENT_DO_ACCOUNTING | EXTENT_DEFRAG, |
Josef Bacik | 2ac55d4 | 2010-02-03 19:33:23 +0000 | [diff] [blame] | 4801 | 0, 0, &cached_state, GFP_NOFS); |
Josef Bacik | 5d5e103 | 2009-10-13 16:46:49 -0400 | [diff] [blame] | 4802 | |
Chandan Rajendra | 9703fef | 2016-01-21 15:55:56 +0530 | [diff] [blame] | 4803 | ret = btrfs_set_extent_delalloc(inode, block_start, block_end, |
Qu Wenruo | ba8b04c | 2016-07-19 16:50:36 +0800 | [diff] [blame] | 4804 | &cached_state, 0); |
Josef Bacik | 9ed74f2 | 2009-09-11 16:12:44 -0400 | [diff] [blame] | 4805 | if (ret) { |
Chandan Rajendra | 9703fef | 2016-01-21 15:55:56 +0530 | [diff] [blame] | 4806 | unlock_extent_cached(io_tree, block_start, block_end, |
Josef Bacik | 2ac55d4 | 2010-02-03 19:33:23 +0000 | [diff] [blame] | 4807 | &cached_state, GFP_NOFS); |
Josef Bacik | 9ed74f2 | 2009-09-11 16:12:44 -0400 | [diff] [blame] | 4808 | goto out_unlock; |
| 4809 | } |
| 4810 | |
Chandan Rajendra | 9703fef | 2016-01-21 15:55:56 +0530 | [diff] [blame] | 4811 | if (offset != blocksize) { |
Josef Bacik | 2aaa665 | 2012-08-29 14:27:18 -0400 | [diff] [blame] | 4812 | if (!len) |
Chandan Rajendra | 9703fef | 2016-01-21 15:55:56 +0530 | [diff] [blame] | 4813 | len = blocksize - offset; |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 4814 | kaddr = kmap(page); |
Josef Bacik | 2aaa665 | 2012-08-29 14:27:18 -0400 | [diff] [blame] | 4815 | if (front) |
Chandan Rajendra | 9703fef | 2016-01-21 15:55:56 +0530 | [diff] [blame] | 4816 | memset(kaddr + (block_start - page_offset(page)), |
| 4817 | 0, offset); |
Josef Bacik | 2aaa665 | 2012-08-29 14:27:18 -0400 | [diff] [blame] | 4818 | else |
Chandan Rajendra | 9703fef | 2016-01-21 15:55:56 +0530 | [diff] [blame] | 4819 | memset(kaddr + (block_start - page_offset(page)) + offset, |
| 4820 | 0, len); |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 4821 | flush_dcache_page(page); |
| 4822 | kunmap(page); |
| 4823 | } |
Chris Mason | 247e743 | 2008-07-17 12:53:51 -0400 | [diff] [blame] | 4824 | ClearPageChecked(page); |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 4825 | set_page_dirty(page); |
Chandan Rajendra | 9703fef | 2016-01-21 15:55:56 +0530 | [diff] [blame] | 4826 | unlock_extent_cached(io_tree, block_start, block_end, &cached_state, |
Josef Bacik | 2ac55d4 | 2010-02-03 19:33:23 +0000 | [diff] [blame] | 4827 | GFP_NOFS); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4828 | |
Chris Mason | 8964222 | 2008-07-24 09:41:53 -0400 | [diff] [blame] | 4829 | out_unlock: |
Josef Bacik | 5d5e103 | 2009-10-13 16:46:49 -0400 | [diff] [blame] | 4830 | if (ret) |
Chandan Rajendra | 9703fef | 2016-01-21 15:55:56 +0530 | [diff] [blame] | 4831 | btrfs_delalloc_release_space(inode, block_start, |
| 4832 | blocksize); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4833 | unlock_page(page); |
Kirill A. Shutemov | 09cbfea | 2016-04-01 15:29:47 +0300 | [diff] [blame] | 4834 | put_page(page); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4835 | out: |
| 4836 | return ret; |
| 4837 | } |
| 4838 | |
Josef Bacik | 16e7549 | 2013-10-22 12:18:51 -0400 | [diff] [blame] | 4839 | static int maybe_insert_hole(struct btrfs_root *root, struct inode *inode, |
| 4840 | u64 offset, u64 len) |
| 4841 | { |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 4842 | struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb); |
Josef Bacik | 16e7549 | 2013-10-22 12:18:51 -0400 | [diff] [blame] | 4843 | struct btrfs_trans_handle *trans; |
| 4844 | int ret; |
| 4845 | |
| 4846 | /* |
| 4847 | * Still need to make sure the inode looks like it's been updated so |
| 4848 | * that any holes get logged if we fsync. |
| 4849 | */ |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 4850 | if (btrfs_fs_incompat(fs_info, NO_HOLES)) { |
| 4851 | BTRFS_I(inode)->last_trans = fs_info->generation; |
Josef Bacik | 16e7549 | 2013-10-22 12:18:51 -0400 | [diff] [blame] | 4852 | BTRFS_I(inode)->last_sub_trans = root->log_transid; |
| 4853 | BTRFS_I(inode)->last_log_commit = root->last_log_commit; |
| 4854 | return 0; |
| 4855 | } |
| 4856 | |
| 4857 | /* |
| 4858 | * 1 - for the one we're dropping |
| 4859 | * 1 - for the one we're adding |
| 4860 | * 1 - for updating the inode. |
| 4861 | */ |
| 4862 | trans = btrfs_start_transaction(root, 3); |
| 4863 | if (IS_ERR(trans)) |
| 4864 | return PTR_ERR(trans); |
| 4865 | |
| 4866 | ret = btrfs_drop_extents(trans, root, inode, offset, offset + len, 1); |
| 4867 | if (ret) { |
Jeff Mahoney | 6664283 | 2016-06-10 18:19:25 -0400 | [diff] [blame] | 4868 | btrfs_abort_transaction(trans, ret); |
Jeff Mahoney | 3a45bb2 | 2016-09-09 21:39:03 -0400 | [diff] [blame] | 4869 | btrfs_end_transaction(trans); |
Josef Bacik | 16e7549 | 2013-10-22 12:18:51 -0400 | [diff] [blame] | 4870 | return ret; |
| 4871 | } |
| 4872 | |
David Sterba | f85b737 | 2017-01-20 14:54:07 +0100 | [diff] [blame] | 4873 | ret = btrfs_insert_file_extent(trans, root, btrfs_ino(BTRFS_I(inode)), |
| 4874 | offset, 0, 0, len, 0, len, 0, 0, 0); |
Josef Bacik | 16e7549 | 2013-10-22 12:18:51 -0400 | [diff] [blame] | 4875 | if (ret) |
Jeff Mahoney | 6664283 | 2016-06-10 18:19:25 -0400 | [diff] [blame] | 4876 | btrfs_abort_transaction(trans, ret); |
Josef Bacik | 16e7549 | 2013-10-22 12:18:51 -0400 | [diff] [blame] | 4877 | else |
| 4878 | btrfs_update_inode(trans, root, inode); |
Jeff Mahoney | 3a45bb2 | 2016-09-09 21:39:03 -0400 | [diff] [blame] | 4879 | btrfs_end_transaction(trans); |
Josef Bacik | 16e7549 | 2013-10-22 12:18:51 -0400 | [diff] [blame] | 4880 | return ret; |
| 4881 | } |
| 4882 | |
Josef Bacik | 695a0d0 | 2011-03-04 15:46:53 -0500 | [diff] [blame] | 4883 | /* |
| 4884 | * This function puts in dummy file extents for the area we're creating a hole |
| 4885 | * for. So if we are truncating this file to a larger size we need to insert |
| 4886 | * these file extents so that btrfs_get_extent will return a EXTENT_MAP_HOLE for |
| 4887 | * the range between oldsize and size |
| 4888 | */ |
Josef Bacik | a41ad39 | 2011-01-31 15:30:16 -0500 | [diff] [blame] | 4889 | 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] | 4890 | { |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 4891 | struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb); |
Yan Zheng | 9036c10 | 2008-10-30 14:19:41 -0400 | [diff] [blame] | 4892 | struct btrfs_root *root = BTRFS_I(inode)->root; |
| 4893 | struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree; |
Yan, Zheng | a22285a | 2010-05-16 10:48:46 -0400 | [diff] [blame] | 4894 | struct extent_map *em = NULL; |
Josef Bacik | 2ac55d4 | 2010-02-03 19:33:23 +0000 | [diff] [blame] | 4895 | struct extent_state *cached_state = NULL; |
Josef Bacik | 5dc562c | 2012-08-17 13:14:17 -0400 | [diff] [blame] | 4896 | struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree; |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 4897 | u64 hole_start = ALIGN(oldsize, fs_info->sectorsize); |
| 4898 | u64 block_end = ALIGN(size, fs_info->sectorsize); |
Yan Zheng | 9036c10 | 2008-10-30 14:19:41 -0400 | [diff] [blame] | 4899 | u64 last_byte; |
| 4900 | u64 cur_offset; |
| 4901 | u64 hole_size; |
Josef Bacik | 9ed74f2 | 2009-09-11 16:12:44 -0400 | [diff] [blame] | 4902 | int err = 0; |
Yan Zheng | 9036c10 | 2008-10-30 14:19:41 -0400 | [diff] [blame] | 4903 | |
Josef Bacik | a71754f | 2013-06-17 17:14:39 -0400 | [diff] [blame] | 4904 | /* |
Chandan Rajendra | 9703fef | 2016-01-21 15:55:56 +0530 | [diff] [blame] | 4905 | * If our size started in the middle of a block we need to zero out the |
| 4906 | * 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] | 4907 | * expose stale data. |
| 4908 | */ |
Chandan Rajendra | 9703fef | 2016-01-21 15:55:56 +0530 | [diff] [blame] | 4909 | err = btrfs_truncate_block(inode, oldsize, 0, 0); |
Josef Bacik | a71754f | 2013-06-17 17:14:39 -0400 | [diff] [blame] | 4910 | if (err) |
| 4911 | return err; |
| 4912 | |
Yan Zheng | 9036c10 | 2008-10-30 14:19:41 -0400 | [diff] [blame] | 4913 | if (size <= hole_start) |
| 4914 | return 0; |
| 4915 | |
Yan Zheng | 9036c10 | 2008-10-30 14:19:41 -0400 | [diff] [blame] | 4916 | while (1) { |
| 4917 | struct btrfs_ordered_extent *ordered; |
Miao Xie | fa7c149 | 2013-09-26 13:15:27 +0800 | [diff] [blame] | 4918 | |
David Sterba | ff13db4 | 2015-12-03 14:30:40 +0100 | [diff] [blame] | 4919 | lock_extent_bits(io_tree, hole_start, block_end - 1, |
Jeff Mahoney | d008237 | 2012-03-01 14:57:19 +0100 | [diff] [blame] | 4920 | &cached_state); |
Miao Xie | fa7c149 | 2013-09-26 13:15:27 +0800 | [diff] [blame] | 4921 | ordered = btrfs_lookup_ordered_range(inode, hole_start, |
| 4922 | block_end - hole_start); |
Yan Zheng | 9036c10 | 2008-10-30 14:19:41 -0400 | [diff] [blame] | 4923 | if (!ordered) |
| 4924 | break; |
Josef Bacik | 2ac55d4 | 2010-02-03 19:33:23 +0000 | [diff] [blame] | 4925 | unlock_extent_cached(io_tree, hole_start, block_end - 1, |
| 4926 | &cached_state, GFP_NOFS); |
Miao Xie | fa7c149 | 2013-09-26 13:15:27 +0800 | [diff] [blame] | 4927 | btrfs_start_ordered_extent(inode, ordered, 1); |
Yan Zheng | 9036c10 | 2008-10-30 14:19:41 -0400 | [diff] [blame] | 4928 | btrfs_put_ordered_extent(ordered); |
| 4929 | } |
| 4930 | |
Yan Zheng | 9036c10 | 2008-10-30 14:19:41 -0400 | [diff] [blame] | 4931 | cur_offset = hole_start; |
| 4932 | while (1) { |
| 4933 | em = btrfs_get_extent(inode, NULL, 0, cur_offset, |
| 4934 | block_end - cur_offset, 0); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 4935 | if (IS_ERR(em)) { |
| 4936 | err = PTR_ERR(em); |
Zach Brown | f276795 | 2013-01-08 19:37:58 +0000 | [diff] [blame] | 4937 | em = NULL; |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 4938 | break; |
| 4939 | } |
Yan Zheng | 9036c10 | 2008-10-30 14:19:41 -0400 | [diff] [blame] | 4940 | last_byte = min(extent_map_end(em), block_end); |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 4941 | last_byte = ALIGN(last_byte, fs_info->sectorsize); |
Yan, Zheng | 8082510 | 2009-11-12 09:35:36 +0000 | [diff] [blame] | 4942 | if (!test_bit(EXTENT_FLAG_PREALLOC, &em->flags)) { |
Josef Bacik | 5dc562c | 2012-08-17 13:14:17 -0400 | [diff] [blame] | 4943 | struct extent_map *hole_em; |
Yan Zheng | 9036c10 | 2008-10-30 14:19:41 -0400 | [diff] [blame] | 4944 | hole_size = last_byte - cur_offset; |
Yan, Zheng | 8082510 | 2009-11-12 09:35:36 +0000 | [diff] [blame] | 4945 | |
Josef Bacik | 16e7549 | 2013-10-22 12:18:51 -0400 | [diff] [blame] | 4946 | err = maybe_insert_hole(root, inode, cur_offset, |
| 4947 | hole_size); |
| 4948 | if (err) |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 4949 | break; |
Josef Bacik | 5dc562c | 2012-08-17 13:14:17 -0400 | [diff] [blame] | 4950 | btrfs_drop_extent_cache(inode, cur_offset, |
| 4951 | cur_offset + hole_size - 1, 0); |
| 4952 | hole_em = alloc_extent_map(); |
| 4953 | if (!hole_em) { |
| 4954 | set_bit(BTRFS_INODE_NEEDS_FULL_SYNC, |
| 4955 | &BTRFS_I(inode)->runtime_flags); |
| 4956 | goto next; |
| 4957 | } |
| 4958 | hole_em->start = cur_offset; |
| 4959 | hole_em->len = hole_size; |
| 4960 | hole_em->orig_start = cur_offset; |
Yan, Zheng | 8082510 | 2009-11-12 09:35:36 +0000 | [diff] [blame] | 4961 | |
Josef Bacik | 5dc562c | 2012-08-17 13:14:17 -0400 | [diff] [blame] | 4962 | hole_em->block_start = EXTENT_MAP_HOLE; |
| 4963 | hole_em->block_len = 0; |
Josef Bacik | b493968 | 2012-12-03 10:31:19 -0500 | [diff] [blame] | 4964 | hole_em->orig_block_len = 0; |
Josef Bacik | cc95bef | 2013-04-04 14:31:27 -0400 | [diff] [blame] | 4965 | hole_em->ram_bytes = hole_size; |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 4966 | hole_em->bdev = fs_info->fs_devices->latest_bdev; |
Josef Bacik | 5dc562c | 2012-08-17 13:14:17 -0400 | [diff] [blame] | 4967 | hole_em->compress_type = BTRFS_COMPRESS_NONE; |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 4968 | hole_em->generation = fs_info->generation; |
Josef Bacik | 5dc562c | 2012-08-17 13:14:17 -0400 | [diff] [blame] | 4969 | |
| 4970 | while (1) { |
| 4971 | write_lock(&em_tree->lock); |
Josef Bacik | 09a2a8f9 | 2013-04-05 16:51:15 -0400 | [diff] [blame] | 4972 | err = add_extent_mapping(em_tree, hole_em, 1); |
Josef Bacik | 5dc562c | 2012-08-17 13:14:17 -0400 | [diff] [blame] | 4973 | write_unlock(&em_tree->lock); |
| 4974 | if (err != -EEXIST) |
| 4975 | break; |
| 4976 | btrfs_drop_extent_cache(inode, cur_offset, |
| 4977 | cur_offset + |
| 4978 | hole_size - 1, 0); |
| 4979 | } |
| 4980 | free_extent_map(hole_em); |
Yan Zheng | 9036c10 | 2008-10-30 14:19:41 -0400 | [diff] [blame] | 4981 | } |
Josef Bacik | 16e7549 | 2013-10-22 12:18:51 -0400 | [diff] [blame] | 4982 | next: |
Yan Zheng | 9036c10 | 2008-10-30 14:19:41 -0400 | [diff] [blame] | 4983 | free_extent_map(em); |
Yan, Zheng | a22285a | 2010-05-16 10:48:46 -0400 | [diff] [blame] | 4984 | em = NULL; |
Yan Zheng | 9036c10 | 2008-10-30 14:19:41 -0400 | [diff] [blame] | 4985 | cur_offset = last_byte; |
Yan, Zheng | 8082510 | 2009-11-12 09:35:36 +0000 | [diff] [blame] | 4986 | if (cur_offset >= block_end) |
Yan Zheng | 9036c10 | 2008-10-30 14:19:41 -0400 | [diff] [blame] | 4987 | break; |
| 4988 | } |
Yan, Zheng | a22285a | 2010-05-16 10:48:46 -0400 | [diff] [blame] | 4989 | free_extent_map(em); |
Josef Bacik | 2ac55d4 | 2010-02-03 19:33:23 +0000 | [diff] [blame] | 4990 | unlock_extent_cached(io_tree, hole_start, block_end - 1, &cached_state, |
| 4991 | GFP_NOFS); |
Yan Zheng | 9036c10 | 2008-10-30 14:19:41 -0400 | [diff] [blame] | 4992 | return err; |
| 4993 | } |
| 4994 | |
Eric Sandeen | 3972f26 | 2013-01-12 02:57:22 +0000 | [diff] [blame] | 4995 | static int btrfs_setsize(struct inode *inode, struct iattr *attr) |
Yan, Zheng | 8082510 | 2009-11-12 09:35:36 +0000 | [diff] [blame] | 4996 | { |
Miao Xie | f4a2f4c | 2011-12-14 20:12:01 -0500 | [diff] [blame] | 4997 | struct btrfs_root *root = BTRFS_I(inode)->root; |
| 4998 | struct btrfs_trans_handle *trans; |
Josef Bacik | a41ad39 | 2011-01-31 15:30:16 -0500 | [diff] [blame] | 4999 | loff_t oldsize = i_size_read(inode); |
Eric Sandeen | 3972f26 | 2013-01-12 02:57:22 +0000 | [diff] [blame] | 5000 | loff_t newsize = attr->ia_size; |
| 5001 | int mask = attr->ia_valid; |
Yan, Zheng | 8082510 | 2009-11-12 09:35:36 +0000 | [diff] [blame] | 5002 | int ret; |
| 5003 | |
Eric Sandeen | 3972f26 | 2013-01-12 02:57:22 +0000 | [diff] [blame] | 5004 | /* |
| 5005 | * The regular truncate() case without ATTR_CTIME and ATTR_MTIME is a |
| 5006 | * special case where we need to update the times despite not having |
| 5007 | * these flags set. For all other operations the VFS set these flags |
| 5008 | * explicitly if it wants a timestamp update. |
| 5009 | */ |
Christoph Hellwig | dff6efc | 2013-11-19 07:17:07 -0800 | [diff] [blame] | 5010 | if (newsize != oldsize) { |
| 5011 | inode_inc_iversion(inode); |
| 5012 | if (!(mask & (ATTR_CTIME | ATTR_MTIME))) |
| 5013 | inode->i_ctime = inode->i_mtime = |
Deepa Dinamani | c2050a4 | 2016-09-14 07:48:06 -0700 | [diff] [blame] | 5014 | current_time(inode); |
Christoph Hellwig | dff6efc | 2013-11-19 07:17:07 -0800 | [diff] [blame] | 5015 | } |
Eric Sandeen | 3972f26 | 2013-01-12 02:57:22 +0000 | [diff] [blame] | 5016 | |
Josef Bacik | a41ad39 | 2011-01-31 15:30:16 -0500 | [diff] [blame] | 5017 | if (newsize > oldsize) { |
Filipe Manana | 9ea24bb | 2014-10-29 11:57:59 +0000 | [diff] [blame] | 5018 | /* |
| 5019 | * Don't do an expanding truncate while snapshoting is ongoing. |
| 5020 | * This is to ensure the snapshot captures a fully consistent |
| 5021 | * state of this file - if the snapshot captures this expanding |
| 5022 | * truncation, it must capture all writes that happened before |
| 5023 | * this truncation. |
| 5024 | */ |
Zhao Lei | 0bc19f90 | 2016-01-06 18:56:36 +0800 | [diff] [blame] | 5025 | btrfs_wait_for_snapshot_creation(root); |
Josef Bacik | a41ad39 | 2011-01-31 15:30:16 -0500 | [diff] [blame] | 5026 | ret = btrfs_cont_expand(inode, oldsize, newsize); |
Filipe Manana | 9ea24bb | 2014-10-29 11:57:59 +0000 | [diff] [blame] | 5027 | if (ret) { |
| 5028 | btrfs_end_write_no_snapshoting(root); |
Yan, Zheng | 8082510 | 2009-11-12 09:35:36 +0000 | [diff] [blame] | 5029 | return ret; |
Filipe Manana | 9ea24bb | 2014-10-29 11:57:59 +0000 | [diff] [blame] | 5030 | } |
Yan, Zheng | 8082510 | 2009-11-12 09:35:36 +0000 | [diff] [blame] | 5031 | |
Miao Xie | f4a2f4c | 2011-12-14 20:12:01 -0500 | [diff] [blame] | 5032 | trans = btrfs_start_transaction(root, 1); |
Filipe Manana | 9ea24bb | 2014-10-29 11:57:59 +0000 | [diff] [blame] | 5033 | if (IS_ERR(trans)) { |
| 5034 | btrfs_end_write_no_snapshoting(root); |
Miao Xie | f4a2f4c | 2011-12-14 20:12:01 -0500 | [diff] [blame] | 5035 | return PTR_ERR(trans); |
Filipe Manana | 9ea24bb | 2014-10-29 11:57:59 +0000 | [diff] [blame] | 5036 | } |
Miao Xie | f4a2f4c | 2011-12-14 20:12:01 -0500 | [diff] [blame] | 5037 | |
| 5038 | i_size_write(inode, newsize); |
| 5039 | btrfs_ordered_update_i_size(inode, i_size_read(inode), NULL); |
Chandan Rajendra | 27772b6 | 2016-01-21 15:56:03 +0530 | [diff] [blame] | 5040 | pagecache_isize_extended(inode, oldsize, newsize); |
Miao Xie | f4a2f4c | 2011-12-14 20:12:01 -0500 | [diff] [blame] | 5041 | ret = btrfs_update_inode(trans, root, inode); |
Filipe Manana | 9ea24bb | 2014-10-29 11:57:59 +0000 | [diff] [blame] | 5042 | btrfs_end_write_no_snapshoting(root); |
Jeff Mahoney | 3a45bb2 | 2016-09-09 21:39:03 -0400 | [diff] [blame] | 5043 | btrfs_end_transaction(trans); |
Josef Bacik | a41ad39 | 2011-01-31 15:30:16 -0500 | [diff] [blame] | 5044 | } else { |
Yan, Zheng | 8082510 | 2009-11-12 09:35:36 +0000 | [diff] [blame] | 5045 | |
Josef Bacik | a41ad39 | 2011-01-31 15:30:16 -0500 | [diff] [blame] | 5046 | /* |
| 5047 | * We're truncating a file that used to have good data down to |
| 5048 | * zero. Make sure it gets into the ordered flush list so that |
| 5049 | * any new writes get down to disk quickly. |
| 5050 | */ |
| 5051 | if (newsize == 0) |
Josef Bacik | 72ac3c0 | 2012-05-23 14:13:11 -0400 | [diff] [blame] | 5052 | set_bit(BTRFS_INODE_ORDERED_DATA_CLOSE, |
| 5053 | &BTRFS_I(inode)->runtime_flags); |
Yan, Zheng | 8082510 | 2009-11-12 09:35:36 +0000 | [diff] [blame] | 5054 | |
Josef Bacik | f3fe820 | 2013-01-07 17:03:21 -0500 | [diff] [blame] | 5055 | /* |
| 5056 | * 1 for the orphan item we're going to add |
| 5057 | * 1 for the orphan item deletion. |
| 5058 | */ |
| 5059 | trans = btrfs_start_transaction(root, 2); |
| 5060 | if (IS_ERR(trans)) |
| 5061 | return PTR_ERR(trans); |
| 5062 | |
| 5063 | /* |
| 5064 | * We need to do this in case we fail at _any_ point during the |
| 5065 | * actual truncate. Once we do the truncate_setsize we could |
| 5066 | * invalidate pages which forces any outstanding ordered io to |
| 5067 | * be instantly completed which will give us extents that need |
| 5068 | * to be truncated. If we fail to get an orphan inode down we |
| 5069 | * could have left over extents that were never meant to live, |
Nicholas D Steeves | 0132761 | 2016-05-19 21:18:45 -0400 | [diff] [blame] | 5070 | * so we need to guarantee from this point on that everything |
Josef Bacik | f3fe820 | 2013-01-07 17:03:21 -0500 | [diff] [blame] | 5071 | * will be consistent. |
| 5072 | */ |
| 5073 | ret = btrfs_orphan_add(trans, inode); |
Jeff Mahoney | 3a45bb2 | 2016-09-09 21:39:03 -0400 | [diff] [blame] | 5074 | btrfs_end_transaction(trans); |
Josef Bacik | f3fe820 | 2013-01-07 17:03:21 -0500 | [diff] [blame] | 5075 | if (ret) |
| 5076 | return ret; |
| 5077 | |
Josef Bacik | a41ad39 | 2011-01-31 15:30:16 -0500 | [diff] [blame] | 5078 | /* we don't support swapfiles, so vmtruncate shouldn't fail */ |
| 5079 | truncate_setsize(inode, newsize); |
Miao Xie | 2e60a51 | 2013-02-08 07:01:08 +0000 | [diff] [blame] | 5080 | |
| 5081 | /* Disable nonlocked read DIO to avoid the end less truncate */ |
| 5082 | btrfs_inode_block_unlocked_dio(inode); |
| 5083 | inode_dio_wait(inode); |
| 5084 | btrfs_inode_resume_unlocked_dio(inode); |
| 5085 | |
Josef Bacik | a41ad39 | 2011-01-31 15:30:16 -0500 | [diff] [blame] | 5086 | ret = btrfs_truncate(inode); |
Josef Bacik | 7f4f6e0 | 2013-08-29 16:43:28 -0400 | [diff] [blame] | 5087 | if (ret && inode->i_nlink) { |
| 5088 | int err; |
| 5089 | |
Liu Bo | 19fd2df | 2016-12-01 13:01:02 -0800 | [diff] [blame] | 5090 | /* To get a stable disk_i_size */ |
| 5091 | err = btrfs_wait_ordered_range(inode, 0, (u64)-1); |
| 5092 | if (err) { |
| 5093 | btrfs_orphan_del(NULL, inode); |
| 5094 | return err; |
| 5095 | } |
| 5096 | |
Josef Bacik | 7f4f6e0 | 2013-08-29 16:43:28 -0400 | [diff] [blame] | 5097 | /* |
| 5098 | * failed to truncate, disk_i_size is only adjusted down |
| 5099 | * as we remove extents, so it should represent the true |
| 5100 | * size of the inode, so reset the in memory size and |
| 5101 | * delete our orphan entry. |
| 5102 | */ |
| 5103 | trans = btrfs_join_transaction(root); |
| 5104 | if (IS_ERR(trans)) { |
| 5105 | btrfs_orphan_del(NULL, inode); |
| 5106 | return ret; |
| 5107 | } |
| 5108 | i_size_write(inode, BTRFS_I(inode)->disk_i_size); |
| 5109 | err = btrfs_orphan_del(trans, inode); |
| 5110 | if (err) |
Jeff Mahoney | 6664283 | 2016-06-10 18:19:25 -0400 | [diff] [blame] | 5111 | btrfs_abort_transaction(trans, err); |
Jeff Mahoney | 3a45bb2 | 2016-09-09 21:39:03 -0400 | [diff] [blame] | 5112 | btrfs_end_transaction(trans); |
Josef Bacik | 7f4f6e0 | 2013-08-29 16:43:28 -0400 | [diff] [blame] | 5113 | } |
Yan, Zheng | 8082510 | 2009-11-12 09:35:36 +0000 | [diff] [blame] | 5114 | } |
| 5115 | |
Josef Bacik | a41ad39 | 2011-01-31 15:30:16 -0500 | [diff] [blame] | 5116 | return ret; |
Yan, Zheng | 8082510 | 2009-11-12 09:35:36 +0000 | [diff] [blame] | 5117 | } |
| 5118 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5119 | static int btrfs_setattr(struct dentry *dentry, struct iattr *attr) |
| 5120 | { |
David Howells | 2b0143b | 2015-03-17 22:25:59 +0000 | [diff] [blame] | 5121 | struct inode *inode = d_inode(dentry); |
Li Zefan | b83cc96 | 2010-12-20 16:04:08 +0800 | [diff] [blame] | 5122 | struct btrfs_root *root = BTRFS_I(inode)->root; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5123 | int err; |
| 5124 | |
Li Zefan | b83cc96 | 2010-12-20 16:04:08 +0800 | [diff] [blame] | 5125 | if (btrfs_root_readonly(root)) |
| 5126 | return -EROFS; |
| 5127 | |
Jan Kara | 31051c8 | 2016-05-26 16:55:18 +0200 | [diff] [blame] | 5128 | err = setattr_prepare(dentry, attr); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5129 | if (err) |
| 5130 | return err; |
| 5131 | |
Chris Mason | 5a3f23d | 2009-03-31 13:27:11 -0400 | [diff] [blame] | 5132 | if (S_ISREG(inode->i_mode) && (attr->ia_valid & ATTR_SIZE)) { |
Eric Sandeen | 3972f26 | 2013-01-12 02:57:22 +0000 | [diff] [blame] | 5133 | err = btrfs_setsize(inode, attr); |
Yan, Zheng | 8082510 | 2009-11-12 09:35:36 +0000 | [diff] [blame] | 5134 | if (err) |
| 5135 | return err; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5136 | } |
Yan Zheng | 9036c10 | 2008-10-30 14:19:41 -0400 | [diff] [blame] | 5137 | |
Christoph Hellwig | 1025774 | 2010-06-04 11:30:02 +0200 | [diff] [blame] | 5138 | if (attr->ia_valid) { |
| 5139 | setattr_copy(inode, attr); |
Josef Bacik | 0c4d2d9 | 2012-04-05 15:03:02 -0400 | [diff] [blame] | 5140 | inode_inc_iversion(inode); |
Josef Bacik | 22c44fe | 2011-11-30 10:45:38 -0500 | [diff] [blame] | 5141 | err = btrfs_dirty_inode(inode); |
Josef Bacik | 33268ea | 2008-07-24 12:16:36 -0400 | [diff] [blame] | 5142 | |
Josef Bacik | 22c44fe | 2011-11-30 10:45:38 -0500 | [diff] [blame] | 5143 | if (!err && attr->ia_valid & ATTR_MODE) |
Christoph Hellwig | 996a710 | 2013-12-20 05:16:43 -0800 | [diff] [blame] | 5144 | err = posix_acl_chmod(inode, inode->i_mode); |
Christoph Hellwig | 1025774 | 2010-06-04 11:30:02 +0200 | [diff] [blame] | 5145 | } |
| 5146 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5147 | return err; |
| 5148 | } |
Chris Mason | 61295eb | 2008-01-14 16:24:38 -0500 | [diff] [blame] | 5149 | |
Filipe David Borba Manana | 131e404 | 2013-11-19 22:29:35 +0000 | [diff] [blame] | 5150 | /* |
| 5151 | * While truncating the inode pages during eviction, we get the VFS calling |
| 5152 | * btrfs_invalidatepage() against each page of the inode. This is slow because |
| 5153 | * the calls to btrfs_invalidatepage() result in a huge amount of calls to |
| 5154 | * lock_extent_bits() and clear_extent_bit(), which keep merging and splitting |
| 5155 | * extent_state structures over and over, wasting lots of time. |
| 5156 | * |
| 5157 | * Therefore if the inode is being evicted, let btrfs_invalidatepage() skip all |
| 5158 | * those expensive operations on a per page basis and do only the ordered io |
| 5159 | * finishing, while we release here the extent_map and extent_state structures, |
| 5160 | * without the excessive merging and splitting. |
| 5161 | */ |
| 5162 | static void evict_inode_truncate_pages(struct inode *inode) |
| 5163 | { |
| 5164 | struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree; |
| 5165 | struct extent_map_tree *map_tree = &BTRFS_I(inode)->extent_tree; |
| 5166 | struct rb_node *node; |
| 5167 | |
| 5168 | ASSERT(inode->i_state & I_FREEING); |
Johannes Weiner | 91b0abe | 2014-04-03 14:47:49 -0700 | [diff] [blame] | 5169 | truncate_inode_pages_final(&inode->i_data); |
Filipe David Borba Manana | 131e404 | 2013-11-19 22:29:35 +0000 | [diff] [blame] | 5170 | |
| 5171 | write_lock(&map_tree->lock); |
| 5172 | while (!RB_EMPTY_ROOT(&map_tree->map)) { |
| 5173 | struct extent_map *em; |
| 5174 | |
| 5175 | node = rb_first(&map_tree->map); |
| 5176 | em = rb_entry(node, struct extent_map, rb_node); |
Wang Shilong | 180589e | 2013-12-14 15:27:31 +0800 | [diff] [blame] | 5177 | clear_bit(EXTENT_FLAG_PINNED, &em->flags); |
| 5178 | clear_bit(EXTENT_FLAG_LOGGING, &em->flags); |
Filipe David Borba Manana | 131e404 | 2013-11-19 22:29:35 +0000 | [diff] [blame] | 5179 | remove_extent_mapping(map_tree, em); |
| 5180 | free_extent_map(em); |
Filipe Manana | 7064dd5 | 2014-08-08 02:47:05 +0100 | [diff] [blame] | 5181 | if (need_resched()) { |
| 5182 | write_unlock(&map_tree->lock); |
| 5183 | cond_resched(); |
| 5184 | write_lock(&map_tree->lock); |
| 5185 | } |
Filipe David Borba Manana | 131e404 | 2013-11-19 22:29:35 +0000 | [diff] [blame] | 5186 | } |
| 5187 | write_unlock(&map_tree->lock); |
| 5188 | |
Filipe Manana | 6ca0709 | 2015-05-26 00:55:42 +0100 | [diff] [blame] | 5189 | /* |
| 5190 | * Keep looping until we have no more ranges in the io tree. |
| 5191 | * We can have ongoing bios started by readpages (called from readahead) |
Filipe Manana | 9c6429d | 2015-06-10 12:55:41 +0100 | [diff] [blame] | 5192 | * that have their endio callback (extent_io.c:end_bio_extent_readpage) |
| 5193 | * still in progress (unlocked the pages in the bio but did not yet |
| 5194 | * unlocked the ranges in the io tree). Therefore this means some |
Filipe Manana | 6ca0709 | 2015-05-26 00:55:42 +0100 | [diff] [blame] | 5195 | * ranges can still be locked and eviction started because before |
| 5196 | * submitting those bios, which are executed by a separate task (work |
| 5197 | * queue kthread), inode references (inode->i_count) were not taken |
| 5198 | * (which would be dropped in the end io callback of each bio). |
| 5199 | * Therefore here we effectively end up waiting for those bios and |
| 5200 | * anyone else holding locked ranges without having bumped the inode's |
| 5201 | * reference count - if we don't do it, when they access the inode's |
| 5202 | * io_tree to unlock a range it may be too late, leading to an |
| 5203 | * use-after-free issue. |
| 5204 | */ |
Filipe David Borba Manana | 131e404 | 2013-11-19 22:29:35 +0000 | [diff] [blame] | 5205 | spin_lock(&io_tree->lock); |
| 5206 | while (!RB_EMPTY_ROOT(&io_tree->state)) { |
| 5207 | struct extent_state *state; |
| 5208 | struct extent_state *cached_state = NULL; |
Filipe Manana | 6ca0709 | 2015-05-26 00:55:42 +0100 | [diff] [blame] | 5209 | u64 start; |
| 5210 | u64 end; |
Filipe David Borba Manana | 131e404 | 2013-11-19 22:29:35 +0000 | [diff] [blame] | 5211 | |
| 5212 | node = rb_first(&io_tree->state); |
| 5213 | state = rb_entry(node, struct extent_state, rb_node); |
Filipe Manana | 6ca0709 | 2015-05-26 00:55:42 +0100 | [diff] [blame] | 5214 | start = state->start; |
| 5215 | end = state->end; |
Filipe David Borba Manana | 131e404 | 2013-11-19 22:29:35 +0000 | [diff] [blame] | 5216 | spin_unlock(&io_tree->lock); |
| 5217 | |
David Sterba | ff13db4 | 2015-12-03 14:30:40 +0100 | [diff] [blame] | 5218 | lock_extent_bits(io_tree, start, end, &cached_state); |
Qu Wenruo | b9d0b38 | 2015-09-29 10:35:16 +0800 | [diff] [blame] | 5219 | |
| 5220 | /* |
| 5221 | * If still has DELALLOC flag, the extent didn't reach disk, |
| 5222 | * and its reserved space won't be freed by delayed_ref. |
| 5223 | * So we need to free its reserved space here. |
| 5224 | * (Refer to comment in btrfs_invalidatepage, case 2) |
| 5225 | * |
| 5226 | * Note, end is the bytenr of last byte, so we need + 1 here. |
| 5227 | */ |
| 5228 | if (state->state & EXTENT_DELALLOC) |
| 5229 | btrfs_qgroup_free_data(inode, start, end - start + 1); |
| 5230 | |
Filipe Manana | 6ca0709 | 2015-05-26 00:55:42 +0100 | [diff] [blame] | 5231 | clear_extent_bit(io_tree, start, end, |
Filipe David Borba Manana | 131e404 | 2013-11-19 22:29:35 +0000 | [diff] [blame] | 5232 | EXTENT_LOCKED | EXTENT_DIRTY | |
| 5233 | EXTENT_DELALLOC | EXTENT_DO_ACCOUNTING | |
| 5234 | EXTENT_DEFRAG, 1, 1, |
| 5235 | &cached_state, GFP_NOFS); |
Filipe David Borba Manana | 131e404 | 2013-11-19 22:29:35 +0000 | [diff] [blame] | 5236 | |
Filipe Manana | 7064dd5 | 2014-08-08 02:47:05 +0100 | [diff] [blame] | 5237 | cond_resched(); |
Filipe David Borba Manana | 131e404 | 2013-11-19 22:29:35 +0000 | [diff] [blame] | 5238 | spin_lock(&io_tree->lock); |
| 5239 | } |
| 5240 | spin_unlock(&io_tree->lock); |
| 5241 | } |
| 5242 | |
Al Viro | bd55597 | 2010-06-07 11:35:40 -0400 | [diff] [blame] | 5243 | void btrfs_evict_inode(struct inode *inode) |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5244 | { |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 5245 | struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5246 | struct btrfs_trans_handle *trans; |
| 5247 | struct btrfs_root *root = BTRFS_I(inode)->root; |
Josef Bacik | 726c35f | 2011-09-26 15:46:06 -0400 | [diff] [blame] | 5248 | struct btrfs_block_rsv *rsv, *global_rsv; |
Josef Bacik | 3bce876 | 2015-02-24 12:35:51 -0800 | [diff] [blame] | 5249 | int steal_from_global = 0; |
Nikolay Borisov | 3d48d98 | 2016-06-29 09:46:41 +0300 | [diff] [blame] | 5250 | u64 min_size; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5251 | int ret; |
| 5252 | |
liubo | 1abe9b8 | 2011-03-24 11:18:59 +0000 | [diff] [blame] | 5253 | trace_btrfs_inode_evict(inode); |
| 5254 | |
Nikolay Borisov | 3d48d98 | 2016-06-29 09:46:41 +0300 | [diff] [blame] | 5255 | if (!root) { |
| 5256 | kmem_cache_free(btrfs_inode_cachep, BTRFS_I(inode)); |
| 5257 | return; |
| 5258 | } |
| 5259 | |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 5260 | min_size = btrfs_calc_trunc_metadata_size(fs_info, 1); |
Nikolay Borisov | 3d48d98 | 2016-06-29 09:46:41 +0300 | [diff] [blame] | 5261 | |
Filipe David Borba Manana | 131e404 | 2013-11-19 22:29:35 +0000 | [diff] [blame] | 5262 | evict_inode_truncate_pages(inode); |
| 5263 | |
Stefan Behrens | 69e9c6c | 2013-09-05 16:58:43 +0200 | [diff] [blame] | 5264 | if (inode->i_nlink && |
| 5265 | ((btrfs_root_refs(&root->root_item) != 0 && |
| 5266 | root->root_key.objectid != BTRFS_ROOT_TREE_OBJECTID) || |
| 5267 | btrfs_is_free_space_inode(inode))) |
Al Viro | bd55597 | 2010-06-07 11:35:40 -0400 | [diff] [blame] | 5268 | goto no_delete; |
| 5269 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5270 | if (is_bad_inode(inode)) { |
Josef Bacik | 7b12876 | 2008-07-24 12:17:14 -0400 | [diff] [blame] | 5271 | btrfs_orphan_del(NULL, inode); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5272 | goto no_delete; |
| 5273 | } |
Al Viro | bd55597 | 2010-06-07 11:35:40 -0400 | [diff] [blame] | 5274 | /* 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] | 5275 | if (!special_file(inode->i_mode)) |
| 5276 | btrfs_wait_ordered_range(inode, 0, (u64)-1); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 5277 | |
Miao Xie | f612496 | 2014-09-12 18:44:04 +0800 | [diff] [blame] | 5278 | btrfs_free_io_failure_record(inode, 0, (u64)-1); |
| 5279 | |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 5280 | if (test_bit(BTRFS_FS_LOG_RECOVERING, &fs_info->flags)) { |
Liu Bo | 6bf0231 | 2012-06-25 21:59:09 -0600 | [diff] [blame] | 5281 | BUG_ON(test_bit(BTRFS_INODE_HAS_ORPHAN_ITEM, |
Josef Bacik | 8a35d95 | 2012-05-23 14:26:42 -0400 | [diff] [blame] | 5282 | &BTRFS_I(inode)->runtime_flags)); |
Yan, Zheng | c71bf09 | 2009-11-12 09:34:40 +0000 | [diff] [blame] | 5283 | goto no_delete; |
| 5284 | } |
| 5285 | |
Yan, Zheng | 76dda93 | 2009-09-21 16:00:26 -0400 | [diff] [blame] | 5286 | if (inode->i_nlink > 0) { |
Stefan Behrens | 69e9c6c | 2013-09-05 16:58:43 +0200 | [diff] [blame] | 5287 | BUG_ON(btrfs_root_refs(&root->root_item) != 0 && |
| 5288 | root->root_key.objectid != BTRFS_ROOT_TREE_OBJECTID); |
Yan, Zheng | 76dda93 | 2009-09-21 16:00:26 -0400 | [diff] [blame] | 5289 | goto no_delete; |
| 5290 | } |
| 5291 | |
Nikolay Borisov | aa79021 | 2017-01-10 20:35:40 +0200 | [diff] [blame] | 5292 | ret = btrfs_commit_inode_delayed_inode(BTRFS_I(inode)); |
Miao Xie | 0e8c36a | 2012-12-19 06:59:51 +0000 | [diff] [blame] | 5293 | if (ret) { |
| 5294 | btrfs_orphan_del(NULL, inode); |
| 5295 | goto no_delete; |
| 5296 | } |
| 5297 | |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 5298 | rsv = btrfs_alloc_block_rsv(fs_info, BTRFS_BLOCK_RSV_TEMP); |
Josef Bacik | 4289a66 | 2011-08-05 13:22:24 -0400 | [diff] [blame] | 5299 | if (!rsv) { |
| 5300 | btrfs_orphan_del(NULL, inode); |
| 5301 | goto no_delete; |
| 5302 | } |
Josef Bacik | 4a33854 | 2011-08-29 11:01:31 -0400 | [diff] [blame] | 5303 | rsv->size = min_size; |
Josef Bacik | ca7e70f | 2012-08-27 17:48:15 -0400 | [diff] [blame] | 5304 | rsv->failfast = 1; |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 5305 | global_rsv = &fs_info->global_block_rsv; |
Josef Bacik | 4289a66 | 2011-08-05 13:22:24 -0400 | [diff] [blame] | 5306 | |
Chris Mason | dbe674a | 2008-07-17 12:54:05 -0400 | [diff] [blame] | 5307 | btrfs_i_size_write(inode, 0); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 5308 | |
Josef Bacik | 4289a66 | 2011-08-05 13:22:24 -0400 | [diff] [blame] | 5309 | /* |
Miao Xie | 8407aa4 | 2012-09-07 01:43:32 -0600 | [diff] [blame] | 5310 | * This is a bit simpler than btrfs_truncate since we've already |
| 5311 | * reserved our space for our orphan item in the unlink, so we just |
| 5312 | * need to reserve some slack space in case we add bytes and update |
| 5313 | * inode item when doing the truncate. |
Josef Bacik | 4289a66 | 2011-08-05 13:22:24 -0400 | [diff] [blame] | 5314 | */ |
Yan, Zheng | 8082510 | 2009-11-12 09:35:36 +0000 | [diff] [blame] | 5315 | while (1) { |
Miao Xie | 08e007d | 2012-10-16 11:33:38 +0000 | [diff] [blame] | 5316 | ret = btrfs_block_rsv_refill(root, rsv, min_size, |
| 5317 | BTRFS_RESERVE_FLUSH_LIMIT); |
Yan, Zheng | 8082510 | 2009-11-12 09:35:36 +0000 | [diff] [blame] | 5318 | |
Josef Bacik | 726c35f | 2011-09-26 15:46:06 -0400 | [diff] [blame] | 5319 | /* |
| 5320 | * Try and steal from the global reserve since we will |
| 5321 | * likely not use this space anyway, we want to try as |
| 5322 | * hard as possible to get this to work. |
| 5323 | */ |
| 5324 | if (ret) |
Josef Bacik | 3bce876 | 2015-02-24 12:35:51 -0800 | [diff] [blame] | 5325 | steal_from_global++; |
| 5326 | else |
| 5327 | steal_from_global = 0; |
| 5328 | ret = 0; |
Josef Bacik | 726c35f | 2011-09-26 15:46:06 -0400 | [diff] [blame] | 5329 | |
Josef Bacik | 3bce876 | 2015-02-24 12:35:51 -0800 | [diff] [blame] | 5330 | /* |
| 5331 | * steal_from_global == 0: we reserved stuff, hooray! |
| 5332 | * steal_from_global == 1: we didn't reserve stuff, boo! |
| 5333 | * steal_from_global == 2: we've committed, still not a lot of |
| 5334 | * room but maybe we'll have room in the global reserve this |
| 5335 | * time. |
| 5336 | * steal_from_global == 3: abandon all hope! |
| 5337 | */ |
| 5338 | if (steal_from_global > 2) { |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 5339 | btrfs_warn(fs_info, |
| 5340 | "Could not get space for a delete, will truncate on mount %d", |
| 5341 | ret); |
Josef Bacik | 4289a66 | 2011-08-05 13:22:24 -0400 | [diff] [blame] | 5342 | btrfs_orphan_del(NULL, inode); |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 5343 | btrfs_free_block_rsv(fs_info, rsv); |
Josef Bacik | 4289a66 | 2011-08-05 13:22:24 -0400 | [diff] [blame] | 5344 | goto no_delete; |
Yan, Zheng | d68fc57 | 2010-05-16 10:49:58 -0400 | [diff] [blame] | 5345 | } |
| 5346 | |
Miao Xie | 0e8c36a | 2012-12-19 06:59:51 +0000 | [diff] [blame] | 5347 | trans = btrfs_join_transaction(root); |
Josef Bacik | 4289a66 | 2011-08-05 13:22:24 -0400 | [diff] [blame] | 5348 | if (IS_ERR(trans)) { |
| 5349 | btrfs_orphan_del(NULL, inode); |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 5350 | btrfs_free_block_rsv(fs_info, rsv); |
Josef Bacik | 4289a66 | 2011-08-05 13:22:24 -0400 | [diff] [blame] | 5351 | goto no_delete; |
| 5352 | } |
| 5353 | |
Josef Bacik | 3bce876 | 2015-02-24 12:35:51 -0800 | [diff] [blame] | 5354 | /* |
Nicholas D Steeves | 0132761 | 2016-05-19 21:18:45 -0400 | [diff] [blame] | 5355 | * 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] | 5356 | * sure there is room to do it, if not we need to commit and try |
| 5357 | * again. |
| 5358 | */ |
| 5359 | if (steal_from_global) { |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 5360 | if (!btrfs_check_space_for_delayed_refs(trans, fs_info)) |
Josef Bacik | 3bce876 | 2015-02-24 12:35:51 -0800 | [diff] [blame] | 5361 | ret = btrfs_block_rsv_migrate(global_rsv, rsv, |
Josef Bacik | 25d609f | 2016-03-25 13:25:48 -0400 | [diff] [blame] | 5362 | min_size, 0); |
Josef Bacik | 3bce876 | 2015-02-24 12:35:51 -0800 | [diff] [blame] | 5363 | else |
| 5364 | ret = -ENOSPC; |
| 5365 | } |
| 5366 | |
| 5367 | /* |
| 5368 | * Couldn't steal from the global reserve, we have too much |
| 5369 | * pending stuff built up, commit the transaction and try it |
| 5370 | * again. |
| 5371 | */ |
| 5372 | if (ret) { |
Jeff Mahoney | 3a45bb2 | 2016-09-09 21:39:03 -0400 | [diff] [blame] | 5373 | ret = btrfs_commit_transaction(trans); |
Josef Bacik | 3bce876 | 2015-02-24 12:35:51 -0800 | [diff] [blame] | 5374 | if (ret) { |
| 5375 | btrfs_orphan_del(NULL, inode); |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 5376 | btrfs_free_block_rsv(fs_info, rsv); |
Josef Bacik | 3bce876 | 2015-02-24 12:35:51 -0800 | [diff] [blame] | 5377 | goto no_delete; |
| 5378 | } |
| 5379 | continue; |
| 5380 | } else { |
| 5381 | steal_from_global = 0; |
| 5382 | } |
| 5383 | |
Josef Bacik | 4289a66 | 2011-08-05 13:22:24 -0400 | [diff] [blame] | 5384 | trans->block_rsv = rsv; |
| 5385 | |
Yan, Zheng | d68fc57 | 2010-05-16 10:49:58 -0400 | [diff] [blame] | 5386 | ret = btrfs_truncate_inode_items(trans, root, inode, 0, 0); |
Chris Mason | 28ed134 | 2014-12-17 09:41:04 -0800 | [diff] [blame] | 5387 | if (ret != -ENOSPC && ret != -EAGAIN) |
Yan, Zheng | 8082510 | 2009-11-12 09:35:36 +0000 | [diff] [blame] | 5388 | break; |
| 5389 | |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 5390 | trans->block_rsv = &fs_info->trans_block_rsv; |
Jeff Mahoney | 3a45bb2 | 2016-09-09 21:39:03 -0400 | [diff] [blame] | 5391 | btrfs_end_transaction(trans); |
Yan, Zheng | 8082510 | 2009-11-12 09:35:36 +0000 | [diff] [blame] | 5392 | trans = NULL; |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 5393 | btrfs_btree_balance_dirty(fs_info); |
Josef Bacik | 7b12876 | 2008-07-24 12:17:14 -0400 | [diff] [blame] | 5394 | } |
| 5395 | |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 5396 | btrfs_free_block_rsv(fs_info, rsv); |
Josef Bacik | 4289a66 | 2011-08-05 13:22:24 -0400 | [diff] [blame] | 5397 | |
Josef Bacik | 4ef31a4 | 2013-08-13 14:10:08 -0400 | [diff] [blame] | 5398 | /* |
| 5399 | * Errors here aren't a big deal, it just means we leave orphan items |
| 5400 | * in the tree. They will be cleaned up on the next mount. |
| 5401 | */ |
Yan, Zheng | 8082510 | 2009-11-12 09:35:36 +0000 | [diff] [blame] | 5402 | if (ret == 0) { |
Josef Bacik | 4289a66 | 2011-08-05 13:22:24 -0400 | [diff] [blame] | 5403 | trans->block_rsv = root->orphan_block_rsv; |
Josef Bacik | 4ef31a4 | 2013-08-13 14:10:08 -0400 | [diff] [blame] | 5404 | btrfs_orphan_del(trans, inode); |
| 5405 | } else { |
| 5406 | btrfs_orphan_del(NULL, inode); |
Yan, Zheng | 8082510 | 2009-11-12 09:35:36 +0000 | [diff] [blame] | 5407 | } |
Chris Mason | 85e21ba | 2008-01-29 15:11:36 -0500 | [diff] [blame] | 5408 | |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 5409 | trans->block_rsv = &fs_info->trans_block_rsv; |
| 5410 | if (!(root == fs_info->tree_root || |
Li Zefan | 581bb05 | 2011-04-20 10:06:11 +0800 | [diff] [blame] | 5411 | root->root_key.objectid == BTRFS_TREE_RELOC_OBJECTID)) |
Nikolay Borisov | 4a0cc7c | 2017-01-10 20:35:31 +0200 | [diff] [blame] | 5412 | btrfs_return_ino(root, btrfs_ino(BTRFS_I(inode))); |
Li Zefan | 581bb05 | 2011-04-20 10:06:11 +0800 | [diff] [blame] | 5413 | |
Jeff Mahoney | 3a45bb2 | 2016-09-09 21:39:03 -0400 | [diff] [blame] | 5414 | btrfs_end_transaction(trans); |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 5415 | btrfs_btree_balance_dirty(fs_info); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5416 | no_delete: |
Nikolay Borisov | f48d1cf | 2017-01-10 20:35:39 +0200 | [diff] [blame] | 5417 | btrfs_remove_delayed_node(BTRFS_I(inode)); |
Jan Kara | dbd5768 | 2012-05-03 14:48:02 +0200 | [diff] [blame] | 5418 | clear_inode(inode); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5419 | } |
| 5420 | |
| 5421 | /* |
| 5422 | * this returns the key found in the dir entry in the location pointer. |
| 5423 | * If no dir entries were found, location->objectid is 0. |
| 5424 | */ |
| 5425 | static int btrfs_inode_by_name(struct inode *dir, struct dentry *dentry, |
| 5426 | struct btrfs_key *location) |
| 5427 | { |
| 5428 | const char *name = dentry->d_name.name; |
| 5429 | int namelen = dentry->d_name.len; |
| 5430 | struct btrfs_dir_item *di; |
| 5431 | struct btrfs_path *path; |
| 5432 | struct btrfs_root *root = BTRFS_I(dir)->root; |
Yan | 0d9f7f3 | 2007-10-25 15:48:28 -0400 | [diff] [blame] | 5433 | int ret = 0; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5434 | |
| 5435 | path = btrfs_alloc_path(); |
Mark Fasheh | d8926bb | 2011-07-13 10:38:47 -0700 | [diff] [blame] | 5436 | if (!path) |
| 5437 | return -ENOMEM; |
Chris Mason | 3954401 | 2007-12-12 14:38:19 -0500 | [diff] [blame] | 5438 | |
David Sterba | f85b737 | 2017-01-20 14:54:07 +0100 | [diff] [blame] | 5439 | di = btrfs_lookup_dir_item(NULL, root, path, btrfs_ino(BTRFS_I(dir)), |
| 5440 | name, namelen, 0); |
Yan | 0d9f7f3 | 2007-10-25 15:48:28 -0400 | [diff] [blame] | 5441 | if (IS_ERR(di)) |
| 5442 | ret = PTR_ERR(di); |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 5443 | |
David Sterba | c704005 | 2011-04-19 18:00:01 +0200 | [diff] [blame] | 5444 | if (IS_ERR_OR_NULL(di)) |
Chris Mason | 3954401 | 2007-12-12 14:38:19 -0500 | [diff] [blame] | 5445 | goto out_err; |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 5446 | |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 5447 | btrfs_dir_item_key_to_cpu(path->nodes[0], di, location); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5448 | out: |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5449 | btrfs_free_path(path); |
| 5450 | return ret; |
Chris Mason | 3954401 | 2007-12-12 14:38:19 -0500 | [diff] [blame] | 5451 | out_err: |
| 5452 | location->objectid = 0; |
| 5453 | goto out; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5454 | } |
| 5455 | |
| 5456 | /* |
| 5457 | * when we hit a tree root in a directory, the btrfs part of the inode |
| 5458 | * needs to be changed to reflect the root directory of the tree root. This |
| 5459 | * is kind of like crossing a mount point. |
| 5460 | */ |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 5461 | static int fixup_tree_root_location(struct btrfs_fs_info *fs_info, |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 5462 | struct inode *dir, |
| 5463 | struct dentry *dentry, |
| 5464 | struct btrfs_key *location, |
| 5465 | struct btrfs_root **sub_root) |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5466 | { |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 5467 | struct btrfs_path *path; |
| 5468 | struct btrfs_root *new_root; |
| 5469 | struct btrfs_root_ref *ref; |
| 5470 | struct extent_buffer *leaf; |
David Sterba | 1d4c08e | 2015-01-02 19:36:14 +0100 | [diff] [blame] | 5471 | struct btrfs_key key; |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 5472 | int ret; |
| 5473 | int err = 0; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5474 | |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 5475 | path = btrfs_alloc_path(); |
| 5476 | if (!path) { |
| 5477 | err = -ENOMEM; |
| 5478 | goto out; |
| 5479 | } |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5480 | |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 5481 | err = -ENOENT; |
David Sterba | 1d4c08e | 2015-01-02 19:36:14 +0100 | [diff] [blame] | 5482 | key.objectid = BTRFS_I(dir)->root->root_key.objectid; |
| 5483 | key.type = BTRFS_ROOT_REF_KEY; |
| 5484 | key.offset = location->objectid; |
| 5485 | |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 5486 | 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] | 5487 | if (ret) { |
| 5488 | if (ret < 0) |
| 5489 | err = ret; |
| 5490 | goto out; |
| 5491 | } |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5492 | |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 5493 | leaf = path->nodes[0]; |
| 5494 | ref = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_root_ref); |
Nikolay Borisov | 4a0cc7c | 2017-01-10 20:35:31 +0200 | [diff] [blame] | 5495 | if (btrfs_root_ref_dirid(leaf, ref) != btrfs_ino(BTRFS_I(dir)) || |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 5496 | btrfs_root_ref_name_len(leaf, ref) != dentry->d_name.len) |
| 5497 | goto out; |
| 5498 | |
| 5499 | ret = memcmp_extent_buffer(leaf, dentry->d_name.name, |
| 5500 | (unsigned long)(ref + 1), |
| 5501 | dentry->d_name.len); |
| 5502 | if (ret) |
| 5503 | goto out; |
| 5504 | |
David Sterba | b3b4aa7 | 2011-04-21 01:20:15 +0200 | [diff] [blame] | 5505 | btrfs_release_path(path); |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 5506 | |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 5507 | new_root = btrfs_read_fs_root_no_name(fs_info, location); |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 5508 | if (IS_ERR(new_root)) { |
| 5509 | err = PTR_ERR(new_root); |
| 5510 | goto out; |
| 5511 | } |
| 5512 | |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 5513 | *sub_root = new_root; |
| 5514 | location->objectid = btrfs_root_dirid(&new_root->root_item); |
| 5515 | location->type = BTRFS_INODE_ITEM_KEY; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5516 | location->offset = 0; |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 5517 | err = 0; |
| 5518 | out: |
| 5519 | btrfs_free_path(path); |
| 5520 | return err; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5521 | } |
| 5522 | |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 5523 | static void inode_tree_add(struct inode *inode) |
| 5524 | { |
| 5525 | struct btrfs_root *root = BTRFS_I(inode)->root; |
| 5526 | struct btrfs_inode *entry; |
From: Nick Piggin | 03e860b | 2009-08-21 10:09:44 +0200 | [diff] [blame] | 5527 | struct rb_node **p; |
| 5528 | struct rb_node *parent; |
Filipe David Borba Manana | cef2193 | 2013-09-02 12:19:13 +0100 | [diff] [blame] | 5529 | struct rb_node *new = &BTRFS_I(inode)->rb_node; |
Nikolay Borisov | 4a0cc7c | 2017-01-10 20:35:31 +0200 | [diff] [blame] | 5530 | u64 ino = btrfs_ino(BTRFS_I(inode)); |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 5531 | |
Al Viro | 1d3382cb | 2010-10-23 15:19:20 -0400 | [diff] [blame] | 5532 | if (inode_unhashed(inode)) |
Yan, Zheng | 76dda93 | 2009-09-21 16:00:26 -0400 | [diff] [blame] | 5533 | return; |
Miao Xie | e1409ce | 2013-05-15 07:48:16 +0000 | [diff] [blame] | 5534 | parent = NULL; |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 5535 | spin_lock(&root->inode_lock); |
Miao Xie | e1409ce | 2013-05-15 07:48:16 +0000 | [diff] [blame] | 5536 | p = &root->inode_tree.rb_node; |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 5537 | while (*p) { |
| 5538 | parent = *p; |
| 5539 | entry = rb_entry(parent, struct btrfs_inode, rb_node); |
| 5540 | |
Nikolay Borisov | 4a0cc7c | 2017-01-10 20:35:31 +0200 | [diff] [blame] | 5541 | if (ino < btrfs_ino(BTRFS_I(&entry->vfs_inode))) |
From: Nick Piggin | 03e860b | 2009-08-21 10:09:44 +0200 | [diff] [blame] | 5542 | p = &parent->rb_left; |
Nikolay Borisov | 4a0cc7c | 2017-01-10 20:35:31 +0200 | [diff] [blame] | 5543 | else if (ino > btrfs_ino(BTRFS_I(&entry->vfs_inode))) |
From: Nick Piggin | 03e860b | 2009-08-21 10:09:44 +0200 | [diff] [blame] | 5544 | p = &parent->rb_right; |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 5545 | else { |
| 5546 | WARN_ON(!(entry->vfs_inode.i_state & |
Al Viro | a4ffdde | 2010-06-02 17:38:30 -0400 | [diff] [blame] | 5547 | (I_WILL_FREE | I_FREEING))); |
Filipe David Borba Manana | cef2193 | 2013-09-02 12:19:13 +0100 | [diff] [blame] | 5548 | rb_replace_node(parent, new, &root->inode_tree); |
From: Nick Piggin | 03e860b | 2009-08-21 10:09:44 +0200 | [diff] [blame] | 5549 | RB_CLEAR_NODE(parent); |
| 5550 | spin_unlock(&root->inode_lock); |
Filipe David Borba Manana | cef2193 | 2013-09-02 12:19:13 +0100 | [diff] [blame] | 5551 | return; |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 5552 | } |
| 5553 | } |
Filipe David Borba Manana | cef2193 | 2013-09-02 12:19:13 +0100 | [diff] [blame] | 5554 | rb_link_node(new, parent, p); |
| 5555 | rb_insert_color(new, &root->inode_tree); |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 5556 | spin_unlock(&root->inode_lock); |
| 5557 | } |
| 5558 | |
| 5559 | static void inode_tree_del(struct inode *inode) |
| 5560 | { |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 5561 | struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb); |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 5562 | struct btrfs_root *root = BTRFS_I(inode)->root; |
Yan, Zheng | 76dda93 | 2009-09-21 16:00:26 -0400 | [diff] [blame] | 5563 | int empty = 0; |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 5564 | |
From: Nick Piggin | 03e860b | 2009-08-21 10:09:44 +0200 | [diff] [blame] | 5565 | spin_lock(&root->inode_lock); |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 5566 | if (!RB_EMPTY_NODE(&BTRFS_I(inode)->rb_node)) { |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 5567 | rb_erase(&BTRFS_I(inode)->rb_node, &root->inode_tree); |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 5568 | RB_CLEAR_NODE(&BTRFS_I(inode)->rb_node); |
Yan, Zheng | 76dda93 | 2009-09-21 16:00:26 -0400 | [diff] [blame] | 5569 | empty = RB_EMPTY_ROOT(&root->inode_tree); |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 5570 | } |
From: Nick Piggin | 03e860b | 2009-08-21 10:09:44 +0200 | [diff] [blame] | 5571 | spin_unlock(&root->inode_lock); |
Yan, Zheng | 76dda93 | 2009-09-21 16:00:26 -0400 | [diff] [blame] | 5572 | |
Stefan Behrens | 69e9c6c | 2013-09-05 16:58:43 +0200 | [diff] [blame] | 5573 | if (empty && btrfs_root_refs(&root->root_item) == 0) { |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 5574 | synchronize_srcu(&fs_info->subvol_srcu); |
Yan, Zheng | 76dda93 | 2009-09-21 16:00:26 -0400 | [diff] [blame] | 5575 | spin_lock(&root->inode_lock); |
| 5576 | empty = RB_EMPTY_ROOT(&root->inode_tree); |
| 5577 | spin_unlock(&root->inode_lock); |
| 5578 | if (empty) |
| 5579 | btrfs_add_dead_root(root); |
| 5580 | } |
| 5581 | } |
| 5582 | |
Jeff Mahoney | 143bede | 2012-03-01 14:56:26 +0100 | [diff] [blame] | 5583 | void btrfs_invalidate_inodes(struct btrfs_root *root) |
Yan, Zheng | 76dda93 | 2009-09-21 16:00:26 -0400 | [diff] [blame] | 5584 | { |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 5585 | struct btrfs_fs_info *fs_info = root->fs_info; |
Yan, Zheng | 76dda93 | 2009-09-21 16:00:26 -0400 | [diff] [blame] | 5586 | struct rb_node *node; |
| 5587 | struct rb_node *prev; |
| 5588 | struct btrfs_inode *entry; |
| 5589 | struct inode *inode; |
| 5590 | u64 objectid = 0; |
| 5591 | |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 5592 | if (!test_bit(BTRFS_FS_STATE_ERROR, &fs_info->fs_state)) |
Liu Bo | 7813b3d | 2014-02-10 17:37:25 +0800 | [diff] [blame] | 5593 | WARN_ON(btrfs_root_refs(&root->root_item) != 0); |
Yan, Zheng | 76dda93 | 2009-09-21 16:00:26 -0400 | [diff] [blame] | 5594 | |
| 5595 | spin_lock(&root->inode_lock); |
| 5596 | again: |
| 5597 | node = root->inode_tree.rb_node; |
| 5598 | prev = NULL; |
| 5599 | while (node) { |
| 5600 | prev = node; |
| 5601 | entry = rb_entry(node, struct btrfs_inode, rb_node); |
| 5602 | |
Nikolay Borisov | 4a0cc7c | 2017-01-10 20:35:31 +0200 | [diff] [blame] | 5603 | if (objectid < btrfs_ino(BTRFS_I(&entry->vfs_inode))) |
Yan, Zheng | 76dda93 | 2009-09-21 16:00:26 -0400 | [diff] [blame] | 5604 | node = node->rb_left; |
Nikolay Borisov | 4a0cc7c | 2017-01-10 20:35:31 +0200 | [diff] [blame] | 5605 | else if (objectid > btrfs_ino(BTRFS_I(&entry->vfs_inode))) |
Yan, Zheng | 76dda93 | 2009-09-21 16:00:26 -0400 | [diff] [blame] | 5606 | node = node->rb_right; |
| 5607 | else |
| 5608 | break; |
| 5609 | } |
| 5610 | if (!node) { |
| 5611 | while (prev) { |
| 5612 | entry = rb_entry(prev, struct btrfs_inode, rb_node); |
Nikolay Borisov | 4a0cc7c | 2017-01-10 20:35:31 +0200 | [diff] [blame] | 5613 | if (objectid <= btrfs_ino(BTRFS_I(&entry->vfs_inode))) { |
Yan, Zheng | 76dda93 | 2009-09-21 16:00:26 -0400 | [diff] [blame] | 5614 | node = prev; |
| 5615 | break; |
| 5616 | } |
| 5617 | prev = rb_next(prev); |
| 5618 | } |
| 5619 | } |
| 5620 | while (node) { |
| 5621 | entry = rb_entry(node, struct btrfs_inode, rb_node); |
Nikolay Borisov | 4a0cc7c | 2017-01-10 20:35:31 +0200 | [diff] [blame] | 5622 | objectid = btrfs_ino(BTRFS_I(&entry->vfs_inode)) + 1; |
Yan, Zheng | 76dda93 | 2009-09-21 16:00:26 -0400 | [diff] [blame] | 5623 | inode = igrab(&entry->vfs_inode); |
| 5624 | if (inode) { |
| 5625 | spin_unlock(&root->inode_lock); |
| 5626 | if (atomic_read(&inode->i_count) > 1) |
| 5627 | d_prune_aliases(inode); |
| 5628 | /* |
Al Viro | 45321ac | 2010-06-07 13:43:19 -0400 | [diff] [blame] | 5629 | * btrfs_drop_inode will have it removed from |
Yan, Zheng | 76dda93 | 2009-09-21 16:00:26 -0400 | [diff] [blame] | 5630 | * the inode cache when its usage count |
| 5631 | * hits zero. |
| 5632 | */ |
| 5633 | iput(inode); |
| 5634 | cond_resched(); |
| 5635 | spin_lock(&root->inode_lock); |
| 5636 | goto again; |
| 5637 | } |
| 5638 | |
| 5639 | if (cond_resched_lock(&root->inode_lock)) |
| 5640 | goto again; |
| 5641 | |
| 5642 | node = rb_next(node); |
| 5643 | } |
| 5644 | spin_unlock(&root->inode_lock); |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 5645 | } |
| 5646 | |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 5647 | static int btrfs_init_locked_inode(struct inode *inode, void *p) |
| 5648 | { |
| 5649 | struct btrfs_iget_args *args = p; |
Chris Mason | 90d3e59 | 2014-01-09 17:28:00 -0800 | [diff] [blame] | 5650 | inode->i_ino = args->location->objectid; |
| 5651 | memcpy(&BTRFS_I(inode)->location, args->location, |
| 5652 | sizeof(*args->location)); |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 5653 | BTRFS_I(inode)->root = args->root; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5654 | return 0; |
| 5655 | } |
| 5656 | |
| 5657 | static int btrfs_find_actor(struct inode *inode, void *opaque) |
| 5658 | { |
| 5659 | struct btrfs_iget_args *args = opaque; |
Chris Mason | 90d3e59 | 2014-01-09 17:28:00 -0800 | [diff] [blame] | 5660 | return args->location->objectid == BTRFS_I(inode)->location.objectid && |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 5661 | args->root == BTRFS_I(inode)->root; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5662 | } |
| 5663 | |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 5664 | static struct inode *btrfs_iget_locked(struct super_block *s, |
Chris Mason | 90d3e59 | 2014-01-09 17:28:00 -0800 | [diff] [blame] | 5665 | struct btrfs_key *location, |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 5666 | struct btrfs_root *root) |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5667 | { |
| 5668 | struct inode *inode; |
| 5669 | struct btrfs_iget_args args; |
Chris Mason | 90d3e59 | 2014-01-09 17:28:00 -0800 | [diff] [blame] | 5670 | unsigned long hashval = btrfs_inode_hash(location->objectid, root); |
Filipe David Borba Manana | 778ba82 | 2013-10-06 22:22:33 +0100 | [diff] [blame] | 5671 | |
Chris Mason | 90d3e59 | 2014-01-09 17:28:00 -0800 | [diff] [blame] | 5672 | args.location = location; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5673 | args.root = root; |
| 5674 | |
Filipe David Borba Manana | 778ba82 | 2013-10-06 22:22:33 +0100 | [diff] [blame] | 5675 | inode = iget5_locked(s, hashval, btrfs_find_actor, |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5676 | btrfs_init_locked_inode, |
| 5677 | (void *)&args); |
| 5678 | return inode; |
| 5679 | } |
| 5680 | |
Balaji Rao | 1a54ef8 | 2008-07-21 02:01:04 +0530 | [diff] [blame] | 5681 | /* Get an inode object given its location and corresponding root. |
| 5682 | * Returns in *is_new if the inode was read from disk |
| 5683 | */ |
| 5684 | struct inode *btrfs_iget(struct super_block *s, struct btrfs_key *location, |
Josef Bacik | 73f7341 | 2009-12-04 17:38:27 +0000 | [diff] [blame] | 5685 | struct btrfs_root *root, int *new) |
Balaji Rao | 1a54ef8 | 2008-07-21 02:01:04 +0530 | [diff] [blame] | 5686 | { |
| 5687 | struct inode *inode; |
| 5688 | |
Chris Mason | 90d3e59 | 2014-01-09 17:28:00 -0800 | [diff] [blame] | 5689 | inode = btrfs_iget_locked(s, location, root); |
Balaji Rao | 1a54ef8 | 2008-07-21 02:01:04 +0530 | [diff] [blame] | 5690 | if (!inode) |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 5691 | return ERR_PTR(-ENOMEM); |
Balaji Rao | 1a54ef8 | 2008-07-21 02:01:04 +0530 | [diff] [blame] | 5692 | |
| 5693 | if (inode->i_state & I_NEW) { |
Filipe Manana | 6771089 | 2016-06-06 11:51:25 +0100 | [diff] [blame] | 5694 | int ret; |
| 5695 | |
| 5696 | ret = btrfs_read_locked_inode(inode); |
Mark Fasheh | 1748f84 | 2011-07-12 11:25:31 -0700 | [diff] [blame] | 5697 | if (!is_bad_inode(inode)) { |
| 5698 | inode_tree_add(inode); |
| 5699 | unlock_new_inode(inode); |
| 5700 | if (new) |
| 5701 | *new = 1; |
| 5702 | } else { |
Sergei Trofimovich | e0b6d65 | 2011-09-11 10:52:24 -0400 | [diff] [blame] | 5703 | unlock_new_inode(inode); |
| 5704 | iput(inode); |
Filipe Manana | 6771089 | 2016-06-06 11:51:25 +0100 | [diff] [blame] | 5705 | ASSERT(ret < 0); |
| 5706 | inode = ERR_PTR(ret < 0 ? ret : -ESTALE); |
Mark Fasheh | 1748f84 | 2011-07-12 11:25:31 -0700 | [diff] [blame] | 5707 | } |
| 5708 | } |
| 5709 | |
Balaji Rao | 1a54ef8 | 2008-07-21 02:01:04 +0530 | [diff] [blame] | 5710 | return inode; |
| 5711 | } |
| 5712 | |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 5713 | static struct inode *new_simple_dir(struct super_block *s, |
| 5714 | struct btrfs_key *key, |
| 5715 | struct btrfs_root *root) |
| 5716 | { |
| 5717 | struct inode *inode = new_inode(s); |
| 5718 | |
| 5719 | if (!inode) |
| 5720 | return ERR_PTR(-ENOMEM); |
| 5721 | |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 5722 | BTRFS_I(inode)->root = root; |
| 5723 | memcpy(&BTRFS_I(inode)->location, key, sizeof(*key)); |
Josef Bacik | 72ac3c0 | 2012-05-23 14:13:11 -0400 | [diff] [blame] | 5724 | set_bit(BTRFS_INODE_DUMMY, &BTRFS_I(inode)->runtime_flags); |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 5725 | |
| 5726 | inode->i_ino = BTRFS_EMPTY_SUBVOL_DIR_OBJECTID; |
Li Zefan | 848cce0 | 2012-02-21 17:04:28 +0800 | [diff] [blame] | 5727 | inode->i_op = &btrfs_dir_ro_inode_operations; |
Omar Sandoval | 1fdf419 | 2017-01-25 17:06:39 -0800 | [diff] [blame] | 5728 | inode->i_opflags &= ~IOP_XATTR; |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 5729 | inode->i_fop = &simple_dir_operations; |
| 5730 | inode->i_mode = S_IFDIR | S_IRUGO | S_IWUSR | S_IXUGO; |
Deepa Dinamani | c2050a4 | 2016-09-14 07:48:06 -0700 | [diff] [blame] | 5731 | inode->i_mtime = current_time(inode); |
chandan r | 9cc97d6 | 2012-07-04 12:48:07 +0530 | [diff] [blame] | 5732 | inode->i_atime = inode->i_mtime; |
| 5733 | inode->i_ctime = inode->i_mtime; |
| 5734 | BTRFS_I(inode)->i_otime = inode->i_mtime; |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 5735 | |
| 5736 | return inode; |
| 5737 | } |
| 5738 | |
Chris Mason | 3de4586 | 2008-11-17 21:02:50 -0500 | [diff] [blame] | 5739 | struct inode *btrfs_lookup_dentry(struct inode *dir, struct dentry *dentry) |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5740 | { |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 5741 | struct btrfs_fs_info *fs_info = btrfs_sb(dir->i_sb); |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 5742 | struct inode *inode; |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 5743 | struct btrfs_root *root = BTRFS_I(dir)->root; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5744 | struct btrfs_root *sub_root = root; |
| 5745 | struct btrfs_key location; |
Yan, Zheng | 76dda93 | 2009-09-21 16:00:26 -0400 | [diff] [blame] | 5746 | int index; |
Josef Bacik | b4aff1f | 2011-06-28 16:18:59 -0400 | [diff] [blame] | 5747 | int ret = 0; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5748 | |
| 5749 | if (dentry->d_name.len > BTRFS_NAME_LEN) |
| 5750 | return ERR_PTR(-ENAMETOOLONG); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 5751 | |
Jeff Layton | 39e3c95 | 2012-11-28 11:30:53 -0500 | [diff] [blame] | 5752 | ret = btrfs_inode_by_name(dir, dentry, &location); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5753 | if (ret < 0) |
| 5754 | return ERR_PTR(ret); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 5755 | |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 5756 | if (location.objectid == 0) |
Tsutomu Itoh | 5662344b3 | 2013-12-13 09:51:42 +0900 | [diff] [blame] | 5757 | return ERR_PTR(-ENOENT); |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 5758 | |
| 5759 | if (location.type == BTRFS_INODE_ITEM_KEY) { |
Josef Bacik | 73f7341 | 2009-12-04 17:38:27 +0000 | [diff] [blame] | 5760 | inode = btrfs_iget(dir->i_sb, &location, root, NULL); |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 5761 | return inode; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5762 | } |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 5763 | |
| 5764 | BUG_ON(location.type != BTRFS_ROOT_ITEM_KEY); |
| 5765 | |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 5766 | index = srcu_read_lock(&fs_info->subvol_srcu); |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 5767 | ret = fixup_tree_root_location(fs_info, dir, dentry, |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 5768 | &location, &sub_root); |
| 5769 | if (ret < 0) { |
| 5770 | if (ret != -ENOENT) |
| 5771 | inode = ERR_PTR(ret); |
| 5772 | else |
| 5773 | inode = new_simple_dir(dir->i_sb, &location, sub_root); |
| 5774 | } else { |
Josef Bacik | 73f7341 | 2009-12-04 17:38:27 +0000 | [diff] [blame] | 5775 | inode = btrfs_iget(dir->i_sb, &location, sub_root, NULL); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5776 | } |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 5777 | srcu_read_unlock(&fs_info->subvol_srcu, index); |
Yan, Zheng | 76dda93 | 2009-09-21 16:00:26 -0400 | [diff] [blame] | 5778 | |
Julia Lawall | 34d19ba | 2011-01-24 19:55:19 +0000 | [diff] [blame] | 5779 | if (!IS_ERR(inode) && root != sub_root) { |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 5780 | down_read(&fs_info->cleanup_work_sem); |
Yan, Zheng | c71bf09 | 2009-11-12 09:34:40 +0000 | [diff] [blame] | 5781 | if (!(inode->i_sb->s_flags & MS_RDONLY)) |
Josef Bacik | 66b4ffd | 2011-01-31 16:22:42 -0500 | [diff] [blame] | 5782 | ret = btrfs_orphan_cleanup(sub_root); |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 5783 | up_read(&fs_info->cleanup_work_sem); |
Josef Bacik | 01cd336 | 2013-06-03 21:39:49 -0400 | [diff] [blame] | 5784 | if (ret) { |
| 5785 | iput(inode); |
Josef Bacik | 66b4ffd | 2011-01-31 16:22:42 -0500 | [diff] [blame] | 5786 | inode = ERR_PTR(ret); |
Josef Bacik | 01cd336 | 2013-06-03 21:39:49 -0400 | [diff] [blame] | 5787 | } |
Yan, Zheng | c71bf09 | 2009-11-12 09:34:40 +0000 | [diff] [blame] | 5788 | } |
| 5789 | |
Chris Mason | 3de4586 | 2008-11-17 21:02:50 -0500 | [diff] [blame] | 5790 | return inode; |
| 5791 | } |
| 5792 | |
Nick Piggin | fe15ce4 | 2011-01-07 17:49:23 +1100 | [diff] [blame] | 5793 | static int btrfs_dentry_delete(const struct dentry *dentry) |
Yan, Zheng | 76dda93 | 2009-09-21 16:00:26 -0400 | [diff] [blame] | 5794 | { |
| 5795 | struct btrfs_root *root; |
David Howells | 2b0143b | 2015-03-17 22:25:59 +0000 | [diff] [blame] | 5796 | struct inode *inode = d_inode(dentry); |
Yan, Zheng | 76dda93 | 2009-09-21 16:00:26 -0400 | [diff] [blame] | 5797 | |
Li Zefan | 848cce0 | 2012-02-21 17:04:28 +0800 | [diff] [blame] | 5798 | if (!inode && !IS_ROOT(dentry)) |
David Howells | 2b0143b | 2015-03-17 22:25:59 +0000 | [diff] [blame] | 5799 | inode = d_inode(dentry->d_parent); |
Yan, Zheng | 76dda93 | 2009-09-21 16:00:26 -0400 | [diff] [blame] | 5800 | |
Li Zefan | 848cce0 | 2012-02-21 17:04:28 +0800 | [diff] [blame] | 5801 | if (inode) { |
| 5802 | root = BTRFS_I(inode)->root; |
Yan, Zheng | efefb14 | 2009-10-09 09:25:16 -0400 | [diff] [blame] | 5803 | if (btrfs_root_refs(&root->root_item) == 0) |
| 5804 | return 1; |
Li Zefan | 848cce0 | 2012-02-21 17:04:28 +0800 | [diff] [blame] | 5805 | |
Nikolay Borisov | 4a0cc7c | 2017-01-10 20:35:31 +0200 | [diff] [blame] | 5806 | if (btrfs_ino(BTRFS_I(inode)) == BTRFS_EMPTY_SUBVOL_DIR_OBJECTID) |
Li Zefan | 848cce0 | 2012-02-21 17:04:28 +0800 | [diff] [blame] | 5807 | return 1; |
Yan, Zheng | efefb14 | 2009-10-09 09:25:16 -0400 | [diff] [blame] | 5808 | } |
Yan, Zheng | 76dda93 | 2009-09-21 16:00:26 -0400 | [diff] [blame] | 5809 | return 0; |
| 5810 | } |
| 5811 | |
Josef Bacik | b4aff1f | 2011-06-28 16:18:59 -0400 | [diff] [blame] | 5812 | static void btrfs_dentry_release(struct dentry *dentry) |
| 5813 | { |
Daeseok Youn | 944a451 | 2014-04-14 15:37:02 +0900 | [diff] [blame] | 5814 | kfree(dentry->d_fsdata); |
Josef Bacik | b4aff1f | 2011-06-28 16:18:59 -0400 | [diff] [blame] | 5815 | } |
| 5816 | |
Chris Mason | 3de4586 | 2008-11-17 21:02:50 -0500 | [diff] [blame] | 5817 | static struct dentry *btrfs_lookup(struct inode *dir, struct dentry *dentry, |
Al Viro | 00cd8dd | 2012-06-10 17:13:09 -0400 | [diff] [blame] | 5818 | unsigned int flags) |
Chris Mason | 3de4586 | 2008-11-17 21:02:50 -0500 | [diff] [blame] | 5819 | { |
Tsutomu Itoh | 5662344b3 | 2013-12-13 09:51:42 +0900 | [diff] [blame] | 5820 | struct inode *inode; |
Josef Bacik | a66e7cc | 2011-09-18 10:34:03 -0400 | [diff] [blame] | 5821 | |
Tsutomu Itoh | 5662344b3 | 2013-12-13 09:51:42 +0900 | [diff] [blame] | 5822 | inode = btrfs_lookup_dentry(dir, dentry); |
| 5823 | if (IS_ERR(inode)) { |
| 5824 | if (PTR_ERR(inode) == -ENOENT) |
| 5825 | inode = NULL; |
| 5826 | else |
| 5827 | return ERR_CAST(inode); |
| 5828 | } |
| 5829 | |
Al Viro | 41d28bc | 2014-10-12 22:24:21 -0400 | [diff] [blame] | 5830 | return d_splice_alias(inode, dentry); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5831 | } |
| 5832 | |
Miao Xie | 16cdcec | 2011-04-22 18:12:22 +0800 | [diff] [blame] | 5833 | unsigned char btrfs_filetype_table[] = { |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5834 | DT_UNKNOWN, DT_REG, DT_DIR, DT_CHR, DT_BLK, DT_FIFO, DT_SOCK, DT_LNK |
| 5835 | }; |
| 5836 | |
Al Viro | 9cdda8d | 2013-05-22 16:48:09 -0400 | [diff] [blame] | 5837 | static int btrfs_real_readdir(struct file *file, struct dir_context *ctx) |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5838 | { |
Al Viro | 9cdda8d | 2013-05-22 16:48:09 -0400 | [diff] [blame] | 5839 | struct inode *inode = file_inode(file); |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 5840 | struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5841 | struct btrfs_root *root = BTRFS_I(inode)->root; |
| 5842 | struct btrfs_item *item; |
| 5843 | struct btrfs_dir_item *di; |
| 5844 | struct btrfs_key key; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 5845 | struct btrfs_key found_key; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5846 | struct btrfs_path *path; |
Miao Xie | 16cdcec | 2011-04-22 18:12:22 +0800 | [diff] [blame] | 5847 | struct list_head ins_list; |
| 5848 | struct list_head del_list; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5849 | int ret; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 5850 | struct extent_buffer *leaf; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5851 | int slot; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5852 | unsigned char d_type; |
| 5853 | int over = 0; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 5854 | char tmp_name[32]; |
| 5855 | char *name_ptr; |
| 5856 | int name_len; |
Omar Sandoval | 02dbfc9 | 2016-05-20 13:50:33 -0700 | [diff] [blame] | 5857 | bool put = false; |
Jeff Mahoney | c2951f3 | 2016-11-21 15:59:04 +0100 | [diff] [blame] | 5858 | struct btrfs_key location; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 5859 | |
Al Viro | 9cdda8d | 2013-05-22 16:48:09 -0400 | [diff] [blame] | 5860 | if (!dir_emit_dots(file, ctx)) |
| 5861 | return 0; |
| 5862 | |
David Woodhouse | 49593bf | 2008-08-17 17:08:36 +0100 | [diff] [blame] | 5863 | path = btrfs_alloc_path(); |
Miao Xie | 16cdcec | 2011-04-22 18:12:22 +0800 | [diff] [blame] | 5864 | if (!path) |
| 5865 | return -ENOMEM; |
Chris Mason | ff5714c | 2011-05-28 07:00:39 -0400 | [diff] [blame] | 5866 | |
David Sterba | e4058b5 | 2015-11-27 16:31:35 +0100 | [diff] [blame] | 5867 | path->reada = READA_FORWARD; |
David Woodhouse | 49593bf | 2008-08-17 17:08:36 +0100 | [diff] [blame] | 5868 | |
Jeff Mahoney | c2951f3 | 2016-11-21 15:59:04 +0100 | [diff] [blame] | 5869 | INIT_LIST_HEAD(&ins_list); |
| 5870 | INIT_LIST_HEAD(&del_list); |
| 5871 | put = btrfs_readdir_get_delayed_items(inode, &ins_list, &del_list); |
Miao Xie | 16cdcec | 2011-04-22 18:12:22 +0800 | [diff] [blame] | 5872 | |
Jeff Mahoney | c2951f3 | 2016-11-21 15:59:04 +0100 | [diff] [blame] | 5873 | key.type = BTRFS_DIR_INDEX_KEY; |
Al Viro | 9cdda8d | 2013-05-22 16:48:09 -0400 | [diff] [blame] | 5874 | key.offset = ctx->pos; |
Nikolay Borisov | 4a0cc7c | 2017-01-10 20:35:31 +0200 | [diff] [blame] | 5875 | key.objectid = btrfs_ino(BTRFS_I(inode)); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 5876 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5877 | ret = btrfs_search_slot(NULL, root, &key, path, 0, 0); |
| 5878 | if (ret < 0) |
| 5879 | goto err; |
David Woodhouse | 49593bf | 2008-08-17 17:08:36 +0100 | [diff] [blame] | 5880 | |
| 5881 | while (1) { |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 5882 | leaf = path->nodes[0]; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5883 | slot = path->slots[0]; |
Li Zefan | b9e03af | 2011-03-23 10:43:58 +0800 | [diff] [blame] | 5884 | if (slot >= btrfs_header_nritems(leaf)) { |
| 5885 | ret = btrfs_next_leaf(root, path); |
| 5886 | if (ret < 0) |
| 5887 | goto err; |
| 5888 | else if (ret > 0) |
| 5889 | break; |
| 5890 | continue; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5891 | } |
Chris Mason | 3de4586 | 2008-11-17 21:02:50 -0500 | [diff] [blame] | 5892 | |
Ross Kirk | dd3cc16 | 2013-09-16 15:58:09 +0100 | [diff] [blame] | 5893 | item = btrfs_item_nr(slot); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 5894 | btrfs_item_key_to_cpu(leaf, &found_key, slot); |
| 5895 | |
| 5896 | if (found_key.objectid != key.objectid) |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5897 | break; |
Jeff Mahoney | c2951f3 | 2016-11-21 15:59:04 +0100 | [diff] [blame] | 5898 | if (found_key.type != BTRFS_DIR_INDEX_KEY) |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5899 | break; |
Al Viro | 9cdda8d | 2013-05-22 16:48:09 -0400 | [diff] [blame] | 5900 | if (found_key.offset < ctx->pos) |
Li Zefan | b9e03af | 2011-03-23 10:43:58 +0800 | [diff] [blame] | 5901 | goto next; |
Jeff Mahoney | c2951f3 | 2016-11-21 15:59:04 +0100 | [diff] [blame] | 5902 | if (btrfs_should_delete_dir_index(&del_list, found_key.offset)) |
Miao Xie | 16cdcec | 2011-04-22 18:12:22 +0800 | [diff] [blame] | 5903 | goto next; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 5904 | |
Al Viro | 9cdda8d | 2013-05-22 16:48:09 -0400 | [diff] [blame] | 5905 | ctx->pos = found_key.offset; |
David Woodhouse | 49593bf | 2008-08-17 17:08:36 +0100 | [diff] [blame] | 5906 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5907 | di = btrfs_item_ptr(leaf, slot, struct btrfs_dir_item); |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 5908 | if (verify_dir_item(fs_info, leaf, di)) |
Jeff Mahoney | c2951f3 | 2016-11-21 15:59:04 +0100 | [diff] [blame] | 5909 | goto next; |
David Woodhouse | 49593bf | 2008-08-17 17:08:36 +0100 | [diff] [blame] | 5910 | |
Jeff Mahoney | c2951f3 | 2016-11-21 15:59:04 +0100 | [diff] [blame] | 5911 | name_len = btrfs_dir_name_len(leaf, di); |
| 5912 | if (name_len <= sizeof(tmp_name)) { |
| 5913 | name_ptr = tmp_name; |
| 5914 | } else { |
| 5915 | name_ptr = kmalloc(name_len, GFP_KERNEL); |
| 5916 | if (!name_ptr) { |
| 5917 | ret = -ENOMEM; |
| 5918 | goto err; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 5919 | } |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5920 | } |
Jeff Mahoney | c2951f3 | 2016-11-21 15:59:04 +0100 | [diff] [blame] | 5921 | read_extent_buffer(leaf, name_ptr, (unsigned long)(di + 1), |
| 5922 | name_len); |
| 5923 | |
| 5924 | d_type = btrfs_filetype_table[btrfs_dir_type(leaf, di)]; |
| 5925 | btrfs_dir_item_key_to_cpu(leaf, di, &location); |
| 5926 | |
| 5927 | over = !dir_emit(ctx, name_ptr, name_len, location.objectid, |
| 5928 | d_type); |
| 5929 | |
| 5930 | if (name_ptr != tmp_name) |
| 5931 | kfree(name_ptr); |
| 5932 | |
Jeff Mahoney | c2951f3 | 2016-11-21 15:59:04 +0100 | [diff] [blame] | 5933 | if (over) |
| 5934 | goto nopos; |
Jeff Mahoney | d2fbb2b | 2016-11-05 13:26:35 -0400 | [diff] [blame] | 5935 | ctx->pos++; |
Li Zefan | b9e03af | 2011-03-23 10:43:58 +0800 | [diff] [blame] | 5936 | next: |
| 5937 | path->slots[0]++; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5938 | } |
David Woodhouse | 49593bf | 2008-08-17 17:08:36 +0100 | [diff] [blame] | 5939 | |
Jeff Mahoney | d2fbb2b | 2016-11-05 13:26:35 -0400 | [diff] [blame] | 5940 | ret = btrfs_readdir_delayed_dir_index(ctx, &ins_list); |
Jeff Mahoney | c2951f3 | 2016-11-21 15:59:04 +0100 | [diff] [blame] | 5941 | if (ret) |
David Sterba | bc4ef75 | 2015-11-13 13:44:28 +0100 | [diff] [blame] | 5942 | goto nopos; |
| 5943 | |
Zach Brown | db62efb | 2013-07-11 16:19:42 -0700 | [diff] [blame] | 5944 | /* |
| 5945 | * Stop new entries from being returned after we return the last |
| 5946 | * entry. |
| 5947 | * |
| 5948 | * New directory entries are assigned a strictly increasing |
| 5949 | * offset. This means that new entries created during readdir |
| 5950 | * are *guaranteed* to be seen in the future by that readdir. |
| 5951 | * This has broken buggy programs which operate on names as |
| 5952 | * they're returned by readdir. Until we re-use freed offsets |
| 5953 | * we have this hack to stop new entries from being returned |
| 5954 | * under the assumption that they'll never reach this huge |
| 5955 | * offset. |
| 5956 | * |
| 5957 | * This is being careful not to overflow 32bit loff_t unless the |
| 5958 | * last entry requires it because doing so has broken 32bit apps |
| 5959 | * in the past. |
| 5960 | */ |
Jeff Mahoney | c2951f3 | 2016-11-21 15:59:04 +0100 | [diff] [blame] | 5961 | if (ctx->pos >= INT_MAX) |
| 5962 | ctx->pos = LLONG_MAX; |
| 5963 | else |
| 5964 | ctx->pos = INT_MAX; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5965 | nopos: |
| 5966 | ret = 0; |
| 5967 | err: |
Omar Sandoval | 02dbfc9 | 2016-05-20 13:50:33 -0700 | [diff] [blame] | 5968 | if (put) |
| 5969 | btrfs_readdir_put_delayed_items(inode, &ins_list, &del_list); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5970 | btrfs_free_path(path); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5971 | return ret; |
| 5972 | } |
| 5973 | |
Christoph Hellwig | a9185b4 | 2010-03-05 09:21:37 +0100 | [diff] [blame] | 5974 | int btrfs_write_inode(struct inode *inode, struct writeback_control *wbc) |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5975 | { |
| 5976 | struct btrfs_root *root = BTRFS_I(inode)->root; |
| 5977 | struct btrfs_trans_handle *trans; |
| 5978 | int ret = 0; |
Josef Bacik | 0af3d00 | 2010-06-21 14:48:16 -0400 | [diff] [blame] | 5979 | bool nolock = false; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5980 | |
Josef Bacik | 72ac3c0 | 2012-05-23 14:13:11 -0400 | [diff] [blame] | 5981 | if (test_bit(BTRFS_INODE_DUMMY, &BTRFS_I(inode)->runtime_flags)) |
Chris Mason | 4ca8b41 | 2008-08-05 13:30:48 -0400 | [diff] [blame] | 5982 | return 0; |
| 5983 | |
Liu Bo | 83eea1f | 2012-07-10 05:28:39 -0600 | [diff] [blame] | 5984 | if (btrfs_fs_closing(root->fs_info) && btrfs_is_free_space_inode(inode)) |
Li Zefan | 82d5902 | 2011-04-20 10:33:24 +0800 | [diff] [blame] | 5985 | nolock = true; |
Josef Bacik | 0af3d00 | 2010-06-21 14:48:16 -0400 | [diff] [blame] | 5986 | |
Christoph Hellwig | a9185b4 | 2010-03-05 09:21:37 +0100 | [diff] [blame] | 5987 | if (wbc->sync_mode == WB_SYNC_ALL) { |
Josef Bacik | 0af3d00 | 2010-06-21 14:48:16 -0400 | [diff] [blame] | 5988 | if (nolock) |
Josef Bacik | 7a7eaa4 | 2011-04-13 12:54:33 -0400 | [diff] [blame] | 5989 | trans = btrfs_join_transaction_nolock(root); |
Josef Bacik | 0af3d00 | 2010-06-21 14:48:16 -0400 | [diff] [blame] | 5990 | else |
Josef Bacik | 7a7eaa4 | 2011-04-13 12:54:33 -0400 | [diff] [blame] | 5991 | trans = btrfs_join_transaction(root); |
Tsutomu Itoh | 3612b49 | 2011-01-25 02:51:38 +0000 | [diff] [blame] | 5992 | if (IS_ERR(trans)) |
| 5993 | return PTR_ERR(trans); |
Jeff Mahoney | 3a45bb2 | 2016-09-09 21:39:03 -0400 | [diff] [blame] | 5994 | ret = btrfs_commit_transaction(trans); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5995 | } |
| 5996 | return ret; |
| 5997 | } |
| 5998 | |
| 5999 | /* |
Chris Mason | 54aa1f4 | 2007-06-22 14:16:25 -0400 | [diff] [blame] | 6000 | * This is somewhat expensive, updating the tree every time the |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 6001 | * inode changes. But, it is most likely to find the inode in cache. |
| 6002 | * FIXME, needs more benchmarking...there are no reasons other than performance |
| 6003 | * to keep or drop this code. |
| 6004 | */ |
Eric Sandeen | 48a3b63 | 2013-04-25 20:41:01 +0000 | [diff] [blame] | 6005 | static int btrfs_dirty_inode(struct inode *inode) |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 6006 | { |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 6007 | struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 6008 | struct btrfs_root *root = BTRFS_I(inode)->root; |
| 6009 | struct btrfs_trans_handle *trans; |
Yan, Zheng | 8929ecfa | 2010-05-16 10:49:58 -0400 | [diff] [blame] | 6010 | int ret; |
| 6011 | |
Josef Bacik | 72ac3c0 | 2012-05-23 14:13:11 -0400 | [diff] [blame] | 6012 | if (test_bit(BTRFS_INODE_DUMMY, &BTRFS_I(inode)->runtime_flags)) |
Josef Bacik | 22c44fe | 2011-11-30 10:45:38 -0500 | [diff] [blame] | 6013 | return 0; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 6014 | |
Josef Bacik | 7a7eaa4 | 2011-04-13 12:54:33 -0400 | [diff] [blame] | 6015 | trans = btrfs_join_transaction(root); |
Josef Bacik | 22c44fe | 2011-11-30 10:45:38 -0500 | [diff] [blame] | 6016 | if (IS_ERR(trans)) |
| 6017 | return PTR_ERR(trans); |
Yan, Zheng | 8929ecfa | 2010-05-16 10:49:58 -0400 | [diff] [blame] | 6018 | |
| 6019 | ret = btrfs_update_inode(trans, root, inode); |
Chris Mason | 94b6044 | 2010-05-26 11:02:00 -0400 | [diff] [blame] | 6020 | if (ret && ret == -ENOSPC) { |
| 6021 | /* whoops, lets try again with the full transaction */ |
Jeff Mahoney | 3a45bb2 | 2016-09-09 21:39:03 -0400 | [diff] [blame] | 6022 | btrfs_end_transaction(trans); |
Chris Mason | 94b6044 | 2010-05-26 11:02:00 -0400 | [diff] [blame] | 6023 | trans = btrfs_start_transaction(root, 1); |
Josef Bacik | 22c44fe | 2011-11-30 10:45:38 -0500 | [diff] [blame] | 6024 | if (IS_ERR(trans)) |
| 6025 | return PTR_ERR(trans); |
Yan, Zheng | 8929ecfa | 2010-05-16 10:49:58 -0400 | [diff] [blame] | 6026 | |
Chris Mason | 94b6044 | 2010-05-26 11:02:00 -0400 | [diff] [blame] | 6027 | ret = btrfs_update_inode(trans, root, inode); |
Chris Mason | 94b6044 | 2010-05-26 11:02:00 -0400 | [diff] [blame] | 6028 | } |
Jeff Mahoney | 3a45bb2 | 2016-09-09 21:39:03 -0400 | [diff] [blame] | 6029 | btrfs_end_transaction(trans); |
Miao Xie | 16cdcec | 2011-04-22 18:12:22 +0800 | [diff] [blame] | 6030 | if (BTRFS_I(inode)->delayed_node) |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 6031 | btrfs_balance_delayed_items(fs_info); |
Josef Bacik | 22c44fe | 2011-11-30 10:45:38 -0500 | [diff] [blame] | 6032 | |
| 6033 | return ret; |
| 6034 | } |
| 6035 | |
| 6036 | /* |
| 6037 | * This is a copy of file_update_time. We need this so we can return error on |
| 6038 | * ENOSPC for updating the inode in the case of file write and mmap writes. |
| 6039 | */ |
Josef Bacik | e41f941 | 2012-03-26 09:46:47 -0400 | [diff] [blame] | 6040 | static int btrfs_update_time(struct inode *inode, struct timespec *now, |
| 6041 | int flags) |
Josef Bacik | 22c44fe | 2011-11-30 10:45:38 -0500 | [diff] [blame] | 6042 | { |
Alexander Block | 2bc556528 | 2012-06-15 09:49:33 +0200 | [diff] [blame] | 6043 | struct btrfs_root *root = BTRFS_I(inode)->root; |
| 6044 | |
| 6045 | if (btrfs_root_readonly(root)) |
| 6046 | return -EROFS; |
| 6047 | |
Josef Bacik | e41f941 | 2012-03-26 09:46:47 -0400 | [diff] [blame] | 6048 | if (flags & S_VERSION) |
Josef Bacik | 22c44fe | 2011-11-30 10:45:38 -0500 | [diff] [blame] | 6049 | inode_inc_iversion(inode); |
Josef Bacik | e41f941 | 2012-03-26 09:46:47 -0400 | [diff] [blame] | 6050 | if (flags & S_CTIME) |
| 6051 | inode->i_ctime = *now; |
| 6052 | if (flags & S_MTIME) |
| 6053 | inode->i_mtime = *now; |
| 6054 | if (flags & S_ATIME) |
| 6055 | inode->i_atime = *now; |
| 6056 | return btrfs_dirty_inode(inode); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 6057 | } |
| 6058 | |
Chris Mason | d352ac6 | 2008-09-29 15:18:18 -0400 | [diff] [blame] | 6059 | /* |
| 6060 | * find the highest existing sequence number in a directory |
| 6061 | * and then set the in-memory index_cnt variable to reflect |
| 6062 | * free sequence numbers |
| 6063 | */ |
Josef Bacik | aec7477 | 2008-07-24 12:12:38 -0400 | [diff] [blame] | 6064 | static int btrfs_set_inode_index_count(struct inode *inode) |
| 6065 | { |
| 6066 | struct btrfs_root *root = BTRFS_I(inode)->root; |
| 6067 | struct btrfs_key key, found_key; |
| 6068 | struct btrfs_path *path; |
| 6069 | struct extent_buffer *leaf; |
| 6070 | int ret; |
| 6071 | |
Nikolay Borisov | 4a0cc7c | 2017-01-10 20:35:31 +0200 | [diff] [blame] | 6072 | key.objectid = btrfs_ino(BTRFS_I(inode)); |
David Sterba | 962a298 | 2014-06-04 18:41:45 +0200 | [diff] [blame] | 6073 | key.type = BTRFS_DIR_INDEX_KEY; |
Josef Bacik | aec7477 | 2008-07-24 12:12:38 -0400 | [diff] [blame] | 6074 | key.offset = (u64)-1; |
| 6075 | |
| 6076 | path = btrfs_alloc_path(); |
| 6077 | if (!path) |
| 6078 | return -ENOMEM; |
| 6079 | |
| 6080 | ret = btrfs_search_slot(NULL, root, &key, path, 0, 0); |
| 6081 | if (ret < 0) |
| 6082 | goto out; |
| 6083 | /* FIXME: we should be able to handle this */ |
| 6084 | if (ret == 0) |
| 6085 | goto out; |
| 6086 | ret = 0; |
| 6087 | |
| 6088 | /* |
| 6089 | * MAGIC NUMBER EXPLANATION: |
| 6090 | * since we search a directory based on f_pos we have to start at 2 |
| 6091 | * since '.' and '..' have f_pos of 0 and 1 respectively, so everybody |
| 6092 | * else has to start at 2 |
| 6093 | */ |
| 6094 | if (path->slots[0] == 0) { |
| 6095 | BTRFS_I(inode)->index_cnt = 2; |
| 6096 | goto out; |
| 6097 | } |
| 6098 | |
| 6099 | path->slots[0]--; |
| 6100 | |
| 6101 | leaf = path->nodes[0]; |
| 6102 | btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]); |
| 6103 | |
Nikolay Borisov | 4a0cc7c | 2017-01-10 20:35:31 +0200 | [diff] [blame] | 6104 | if (found_key.objectid != btrfs_ino(BTRFS_I(inode)) || |
David Sterba | 962a298 | 2014-06-04 18:41:45 +0200 | [diff] [blame] | 6105 | found_key.type != BTRFS_DIR_INDEX_KEY) { |
Josef Bacik | aec7477 | 2008-07-24 12:12:38 -0400 | [diff] [blame] | 6106 | BTRFS_I(inode)->index_cnt = 2; |
| 6107 | goto out; |
| 6108 | } |
| 6109 | |
| 6110 | BTRFS_I(inode)->index_cnt = found_key.offset + 1; |
| 6111 | out: |
| 6112 | btrfs_free_path(path); |
| 6113 | return ret; |
| 6114 | } |
| 6115 | |
Chris Mason | d352ac6 | 2008-09-29 15:18:18 -0400 | [diff] [blame] | 6116 | /* |
| 6117 | * helper to find a free sequence number in a given directory. This current |
| 6118 | * code is very simple, later versions will do smarter things in the btree |
| 6119 | */ |
Chris Mason | 3de4586 | 2008-11-17 21:02:50 -0500 | [diff] [blame] | 6120 | int btrfs_set_inode_index(struct inode *dir, u64 *index) |
Josef Bacik | aec7477 | 2008-07-24 12:12:38 -0400 | [diff] [blame] | 6121 | { |
| 6122 | int ret = 0; |
| 6123 | |
| 6124 | if (BTRFS_I(dir)->index_cnt == (u64)-1) { |
Nikolay Borisov | f5cc7b8 | 2017-01-10 20:35:42 +0200 | [diff] [blame] | 6125 | ret = btrfs_inode_delayed_dir_index_count(BTRFS_I(dir)); |
Miao Xie | 16cdcec | 2011-04-22 18:12:22 +0800 | [diff] [blame] | 6126 | if (ret) { |
| 6127 | ret = btrfs_set_inode_index_count(dir); |
| 6128 | if (ret) |
| 6129 | return ret; |
| 6130 | } |
Josef Bacik | aec7477 | 2008-07-24 12:12:38 -0400 | [diff] [blame] | 6131 | } |
| 6132 | |
Chris Mason | 00e4e6b | 2008-08-05 11:18:09 -0400 | [diff] [blame] | 6133 | *index = BTRFS_I(dir)->index_cnt; |
Josef Bacik | aec7477 | 2008-07-24 12:12:38 -0400 | [diff] [blame] | 6134 | BTRFS_I(dir)->index_cnt++; |
| 6135 | |
| 6136 | return ret; |
| 6137 | } |
| 6138 | |
Chris Mason | b0d5d10 | 2014-09-08 13:08:51 -0700 | [diff] [blame] | 6139 | static int btrfs_insert_inode_locked(struct inode *inode) |
| 6140 | { |
| 6141 | struct btrfs_iget_args args; |
| 6142 | args.location = &BTRFS_I(inode)->location; |
| 6143 | args.root = BTRFS_I(inode)->root; |
| 6144 | |
| 6145 | return insert_inode_locked4(inode, |
| 6146 | btrfs_inode_hash(inode->i_ino, BTRFS_I(inode)->root), |
| 6147 | btrfs_find_actor, &args); |
| 6148 | } |
| 6149 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 6150 | static struct inode *btrfs_new_inode(struct btrfs_trans_handle *trans, |
| 6151 | struct btrfs_root *root, |
Josef Bacik | aec7477 | 2008-07-24 12:12:38 -0400 | [diff] [blame] | 6152 | struct inode *dir, |
Chris Mason | 9c58309 | 2008-01-29 15:15:18 -0500 | [diff] [blame] | 6153 | const char *name, int name_len, |
Al Viro | 175a4eb | 2011-07-26 03:30:54 -0400 | [diff] [blame] | 6154 | u64 ref_objectid, u64 objectid, |
| 6155 | umode_t mode, u64 *index) |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 6156 | { |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 6157 | struct btrfs_fs_info *fs_info = root->fs_info; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 6158 | struct inode *inode; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 6159 | struct btrfs_inode_item *inode_item; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 6160 | struct btrfs_key *location; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 6161 | struct btrfs_path *path; |
Chris Mason | 9c58309 | 2008-01-29 15:15:18 -0500 | [diff] [blame] | 6162 | struct btrfs_inode_ref *ref; |
| 6163 | struct btrfs_key key[2]; |
| 6164 | u32 sizes[2]; |
Filipe Manana | ef3b9af | 2014-04-27 20:40:45 +0100 | [diff] [blame] | 6165 | int nitems = name ? 2 : 1; |
Chris Mason | 9c58309 | 2008-01-29 15:15:18 -0500 | [diff] [blame] | 6166 | unsigned long ptr; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 6167 | int ret; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 6168 | |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 6169 | path = btrfs_alloc_path(); |
Mark Fasheh | d8926bb | 2011-07-13 10:38:47 -0700 | [diff] [blame] | 6170 | if (!path) |
| 6171 | return ERR_PTR(-ENOMEM); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 6172 | |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 6173 | inode = new_inode(fs_info->sb); |
Yoshinori Sano | 8fb2764 | 2011-04-09 02:30:07 +0000 | [diff] [blame] | 6174 | if (!inode) { |
| 6175 | btrfs_free_path(path); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 6176 | return ERR_PTR(-ENOMEM); |
Yoshinori Sano | 8fb2764 | 2011-04-09 02:30:07 +0000 | [diff] [blame] | 6177 | } |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 6178 | |
Li Zefan | 581bb05 | 2011-04-20 10:06:11 +0800 | [diff] [blame] | 6179 | /* |
Filipe Manana | 5762b5c | 2014-08-01 00:10:32 +0100 | [diff] [blame] | 6180 | * O_TMPFILE, set link count to 0, so that after this point, |
| 6181 | * we fill in an inode item with the correct link count. |
| 6182 | */ |
| 6183 | if (!name) |
| 6184 | set_nlink(inode, 0); |
| 6185 | |
| 6186 | /* |
Li Zefan | 581bb05 | 2011-04-20 10:06:11 +0800 | [diff] [blame] | 6187 | * we have to initialize this early, so we can reclaim the inode |
| 6188 | * number if we fail afterwards in this function. |
| 6189 | */ |
| 6190 | inode->i_ino = objectid; |
| 6191 | |
Filipe Manana | ef3b9af | 2014-04-27 20:40:45 +0100 | [diff] [blame] | 6192 | if (dir && name) { |
liubo | 1abe9b8 | 2011-03-24 11:18:59 +0000 | [diff] [blame] | 6193 | trace_btrfs_inode_request(dir); |
| 6194 | |
Chris Mason | 3de4586 | 2008-11-17 21:02:50 -0500 | [diff] [blame] | 6195 | ret = btrfs_set_inode_index(dir, index); |
Shen Feng | 0977143 | 2009-04-02 16:46:06 -0400 | [diff] [blame] | 6196 | if (ret) { |
Yoshinori Sano | 8fb2764 | 2011-04-09 02:30:07 +0000 | [diff] [blame] | 6197 | btrfs_free_path(path); |
Shen Feng | 0977143 | 2009-04-02 16:46:06 -0400 | [diff] [blame] | 6198 | iput(inode); |
Josef Bacik | aec7477 | 2008-07-24 12:12:38 -0400 | [diff] [blame] | 6199 | return ERR_PTR(ret); |
Shen Feng | 0977143 | 2009-04-02 16:46:06 -0400 | [diff] [blame] | 6200 | } |
Filipe Manana | ef3b9af | 2014-04-27 20:40:45 +0100 | [diff] [blame] | 6201 | } else if (dir) { |
| 6202 | *index = 0; |
Josef Bacik | aec7477 | 2008-07-24 12:12:38 -0400 | [diff] [blame] | 6203 | } |
| 6204 | /* |
| 6205 | * index_cnt is ignored for everything but a dir, |
| 6206 | * btrfs_get_inode_index_count has an explanation for the magic |
| 6207 | * number |
| 6208 | */ |
| 6209 | BTRFS_I(inode)->index_cnt = 2; |
Miao Xie | 67de117 | 2013-12-26 13:07:06 +0800 | [diff] [blame] | 6210 | BTRFS_I(inode)->dir_index = *index; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 6211 | BTRFS_I(inode)->root = root; |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 6212 | BTRFS_I(inode)->generation = trans->transid; |
Josef Bacik | 7619585 | 2010-11-19 02:18:02 +0000 | [diff] [blame] | 6213 | inode->i_generation = BTRFS_I(inode)->generation; |
Chris Mason | b888db2 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 6214 | |
Josef Bacik | 5dc562c | 2012-08-17 13:14:17 -0400 | [diff] [blame] | 6215 | /* |
| 6216 | * We could have gotten an inode number from somebody who was fsynced |
| 6217 | * and then removed in this same transaction, so let's just set full |
| 6218 | * sync since it will be a full sync anyway and this will blow away the |
| 6219 | * old info in the log. |
| 6220 | */ |
| 6221 | set_bit(BTRFS_INODE_NEEDS_FULL_SYNC, &BTRFS_I(inode)->runtime_flags); |
| 6222 | |
Chris Mason | 9c58309 | 2008-01-29 15:15:18 -0500 | [diff] [blame] | 6223 | key[0].objectid = objectid; |
David Sterba | 962a298 | 2014-06-04 18:41:45 +0200 | [diff] [blame] | 6224 | key[0].type = BTRFS_INODE_ITEM_KEY; |
Chris Mason | 9c58309 | 2008-01-29 15:15:18 -0500 | [diff] [blame] | 6225 | key[0].offset = 0; |
| 6226 | |
Chris Mason | 9c58309 | 2008-01-29 15:15:18 -0500 | [diff] [blame] | 6227 | sizes[0] = sizeof(struct btrfs_inode_item); |
Filipe Manana | ef3b9af | 2014-04-27 20:40:45 +0100 | [diff] [blame] | 6228 | |
| 6229 | if (name) { |
| 6230 | /* |
| 6231 | * Start new inodes with an inode_ref. This is slightly more |
| 6232 | * efficient for small numbers of hard links since they will |
| 6233 | * be packed into one item. Extended refs will kick in if we |
| 6234 | * add more hard links than can fit in the ref item. |
| 6235 | */ |
| 6236 | key[1].objectid = objectid; |
David Sterba | 962a298 | 2014-06-04 18:41:45 +0200 | [diff] [blame] | 6237 | key[1].type = BTRFS_INODE_REF_KEY; |
Filipe Manana | ef3b9af | 2014-04-27 20:40:45 +0100 | [diff] [blame] | 6238 | key[1].offset = ref_objectid; |
| 6239 | |
| 6240 | sizes[1] = name_len + sizeof(*ref); |
| 6241 | } |
Chris Mason | 9c58309 | 2008-01-29 15:15:18 -0500 | [diff] [blame] | 6242 | |
Chris Mason | b0d5d10 | 2014-09-08 13:08:51 -0700 | [diff] [blame] | 6243 | location = &BTRFS_I(inode)->location; |
| 6244 | location->objectid = objectid; |
| 6245 | location->offset = 0; |
David Sterba | 962a298 | 2014-06-04 18:41:45 +0200 | [diff] [blame] | 6246 | location->type = BTRFS_INODE_ITEM_KEY; |
Chris Mason | b0d5d10 | 2014-09-08 13:08:51 -0700 | [diff] [blame] | 6247 | |
| 6248 | ret = btrfs_insert_inode_locked(inode); |
| 6249 | if (ret < 0) |
| 6250 | goto fail; |
| 6251 | |
Chris Mason | b947343 | 2009-03-13 11:00:37 -0400 | [diff] [blame] | 6252 | path->leave_spinning = 1; |
Filipe Manana | ef3b9af | 2014-04-27 20:40:45 +0100 | [diff] [blame] | 6253 | ret = btrfs_insert_empty_items(trans, root, path, key, sizes, nitems); |
Chris Mason | 9c58309 | 2008-01-29 15:15:18 -0500 | [diff] [blame] | 6254 | if (ret != 0) |
Chris Mason | b0d5d10 | 2014-09-08 13:08:51 -0700 | [diff] [blame] | 6255 | goto fail_unlock; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 6256 | |
Dmitry Monakhov | ecc11fab | 2010-03-04 17:31:47 +0300 | [diff] [blame] | 6257 | inode_init_owner(inode, dir, mode); |
Yan Zheng | a76a3cd | 2008-10-09 11:46:29 -0400 | [diff] [blame] | 6258 | inode_set_bytes(inode, 0); |
chandan r | 9cc97d6 | 2012-07-04 12:48:07 +0530 | [diff] [blame] | 6259 | |
Deepa Dinamani | c2050a4 | 2016-09-14 07:48:06 -0700 | [diff] [blame] | 6260 | inode->i_mtime = current_time(inode); |
chandan r | 9cc97d6 | 2012-07-04 12:48:07 +0530 | [diff] [blame] | 6261 | inode->i_atime = inode->i_mtime; |
| 6262 | inode->i_ctime = inode->i_mtime; |
| 6263 | BTRFS_I(inode)->i_otime = inode->i_mtime; |
| 6264 | |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 6265 | inode_item = btrfs_item_ptr(path->nodes[0], path->slots[0], |
| 6266 | struct btrfs_inode_item); |
David Sterba | b159fa2 | 2016-11-08 18:09:03 +0100 | [diff] [blame] | 6267 | memzero_extent_buffer(path->nodes[0], (unsigned long)inode_item, |
Li Zefan | 293f7e0 | 2012-07-10 00:58:58 -0600 | [diff] [blame] | 6268 | sizeof(*inode_item)); |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 6269 | fill_inode_item(trans, path->nodes[0], inode_item, inode); |
Chris Mason | 9c58309 | 2008-01-29 15:15:18 -0500 | [diff] [blame] | 6270 | |
Filipe Manana | ef3b9af | 2014-04-27 20:40:45 +0100 | [diff] [blame] | 6271 | if (name) { |
| 6272 | ref = btrfs_item_ptr(path->nodes[0], path->slots[0] + 1, |
| 6273 | struct btrfs_inode_ref); |
| 6274 | btrfs_set_inode_ref_name_len(path->nodes[0], ref, name_len); |
| 6275 | btrfs_set_inode_ref_index(path->nodes[0], ref, *index); |
| 6276 | ptr = (unsigned long)(ref + 1); |
| 6277 | write_extent_buffer(path->nodes[0], name, ptr, name_len); |
| 6278 | } |
Chris Mason | 9c58309 | 2008-01-29 15:15:18 -0500 | [diff] [blame] | 6279 | |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 6280 | btrfs_mark_buffer_dirty(path->nodes[0]); |
| 6281 | btrfs_free_path(path); |
| 6282 | |
Christoph Hellwig | 6cbff00 | 2009-04-17 10:37:41 +0200 | [diff] [blame] | 6283 | btrfs_inherit_iflags(inode, dir); |
| 6284 | |
Al Viro | 569254b | 2011-07-24 17:08:40 -0400 | [diff] [blame] | 6285 | if (S_ISREG(mode)) { |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 6286 | if (btrfs_test_opt(fs_info, NODATASUM)) |
Chris Mason | 9427216 | 2009-07-02 12:26:06 -0400 | [diff] [blame] | 6287 | BTRFS_I(inode)->flags |= BTRFS_INODE_NODATASUM; |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 6288 | if (btrfs_test_opt(fs_info, NODATACOW)) |
Josef Bacik | f2bdf9a | 2013-02-21 15:28:28 -0500 | [diff] [blame] | 6289 | BTRFS_I(inode)->flags |= BTRFS_INODE_NODATACOW | |
| 6290 | BTRFS_INODE_NODATASUM; |
Chris Mason | 9427216 | 2009-07-02 12:26:06 -0400 | [diff] [blame] | 6291 | } |
| 6292 | |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 6293 | inode_tree_add(inode); |
liubo | 1abe9b8 | 2011-03-24 11:18:59 +0000 | [diff] [blame] | 6294 | |
| 6295 | trace_btrfs_inode_new(inode); |
Chris Mason | 1973f0f | 2011-06-24 13:13:29 -0400 | [diff] [blame] | 6296 | btrfs_set_inode_last_trans(trans, inode); |
liubo | 1abe9b8 | 2011-03-24 11:18:59 +0000 | [diff] [blame] | 6297 | |
Alexander Block | 8ea05e3 | 2012-07-25 17:35:53 +0200 | [diff] [blame] | 6298 | btrfs_update_root_times(trans, root); |
| 6299 | |
Filipe David Borba Manana | 6354192 | 2014-01-07 11:47:46 +0000 | [diff] [blame] | 6300 | ret = btrfs_inode_inherit_props(trans, inode, dir); |
| 6301 | if (ret) |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 6302 | btrfs_err(fs_info, |
Filipe David Borba Manana | 6354192 | 2014-01-07 11:47:46 +0000 | [diff] [blame] | 6303 | "error inheriting props for ino %llu (root %llu): %d", |
David Sterba | f85b737 | 2017-01-20 14:54:07 +0100 | [diff] [blame] | 6304 | btrfs_ino(BTRFS_I(inode)), root->root_key.objectid, ret); |
Filipe David Borba Manana | 6354192 | 2014-01-07 11:47:46 +0000 | [diff] [blame] | 6305 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 6306 | return inode; |
Chris Mason | b0d5d10 | 2014-09-08 13:08:51 -0700 | [diff] [blame] | 6307 | |
| 6308 | fail_unlock: |
| 6309 | unlock_new_inode(inode); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 6310 | fail: |
Filipe Manana | ef3b9af | 2014-04-27 20:40:45 +0100 | [diff] [blame] | 6311 | if (dir && name) |
Josef Bacik | aec7477 | 2008-07-24 12:12:38 -0400 | [diff] [blame] | 6312 | BTRFS_I(dir)->index_cnt--; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 6313 | btrfs_free_path(path); |
Shen Feng | 0977143 | 2009-04-02 16:46:06 -0400 | [diff] [blame] | 6314 | iput(inode); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 6315 | return ERR_PTR(ret); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 6316 | } |
| 6317 | |
| 6318 | static inline u8 btrfs_inode_type(struct inode *inode) |
| 6319 | { |
| 6320 | return btrfs_type_by_mode[(inode->i_mode & S_IFMT) >> S_SHIFT]; |
| 6321 | } |
| 6322 | |
Chris Mason | d352ac6 | 2008-09-29 15:18:18 -0400 | [diff] [blame] | 6323 | /* |
| 6324 | * utility function to add 'inode' into 'parent_inode' with |
| 6325 | * a give name and a given sequence number. |
| 6326 | * if 'add_backref' is true, also insert a backref from the |
| 6327 | * inode to the parent directory. |
| 6328 | */ |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 6329 | int btrfs_add_link(struct btrfs_trans_handle *trans, |
| 6330 | struct inode *parent_inode, struct inode *inode, |
| 6331 | const char *name, int name_len, int add_backref, u64 index) |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 6332 | { |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 6333 | struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb); |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 6334 | int ret = 0; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 6335 | struct btrfs_key key; |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 6336 | struct btrfs_root *root = BTRFS_I(parent_inode)->root; |
Nikolay Borisov | 4a0cc7c | 2017-01-10 20:35:31 +0200 | [diff] [blame] | 6337 | u64 ino = btrfs_ino(BTRFS_I(inode)); |
| 6338 | u64 parent_ino = btrfs_ino(BTRFS_I(parent_inode)); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 6339 | |
Li Zefan | 33345d01 | 2011-04-20 10:31:50 +0800 | [diff] [blame] | 6340 | if (unlikely(ino == BTRFS_FIRST_FREE_OBJECTID)) { |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 6341 | memcpy(&key, &BTRFS_I(inode)->root->root_key, sizeof(key)); |
| 6342 | } else { |
Li Zefan | 33345d01 | 2011-04-20 10:31:50 +0800 | [diff] [blame] | 6343 | key.objectid = ino; |
David Sterba | 962a298 | 2014-06-04 18:41:45 +0200 | [diff] [blame] | 6344 | key.type = BTRFS_INODE_ITEM_KEY; |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 6345 | key.offset = 0; |
| 6346 | } |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 6347 | |
Li Zefan | 33345d01 | 2011-04-20 10:31:50 +0800 | [diff] [blame] | 6348 | if (unlikely(ino == BTRFS_FIRST_FREE_OBJECTID)) { |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 6349 | ret = btrfs_add_root_ref(trans, fs_info, key.objectid, |
| 6350 | root->root_key.objectid, parent_ino, |
| 6351 | index, name, name_len); |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 6352 | } else if (add_backref) { |
Li Zefan | 33345d01 | 2011-04-20 10:31:50 +0800 | [diff] [blame] | 6353 | ret = btrfs_insert_inode_ref(trans, root, name, name_len, ino, |
| 6354 | parent_ino, index); |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 6355 | } |
| 6356 | |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 6357 | /* Nothing to clean up yet */ |
| 6358 | if (ret) |
| 6359 | return ret; |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 6360 | |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 6361 | ret = btrfs_insert_dir_item(trans, root, name, name_len, |
| 6362 | parent_inode, &key, |
| 6363 | btrfs_inode_type(inode), index); |
Chris Mason | 9c52057 | 2012-12-17 14:26:57 -0500 | [diff] [blame] | 6364 | if (ret == -EEXIST || ret == -EOVERFLOW) |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 6365 | goto fail_dir_item; |
| 6366 | else if (ret) { |
Jeff Mahoney | 6664283 | 2016-06-10 18:19:25 -0400 | [diff] [blame] | 6367 | btrfs_abort_transaction(trans, ret); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 6368 | return ret; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 6369 | } |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 6370 | |
| 6371 | btrfs_i_size_write(parent_inode, parent_inode->i_size + |
| 6372 | name_len * 2); |
Josef Bacik | 0c4d2d9 | 2012-04-05 15:03:02 -0400 | [diff] [blame] | 6373 | inode_inc_iversion(parent_inode); |
Deepa Dinamani | 04b285f | 2016-02-06 23:57:21 -0800 | [diff] [blame] | 6374 | parent_inode->i_mtime = parent_inode->i_ctime = |
Deepa Dinamani | c2050a4 | 2016-09-14 07:48:06 -0700 | [diff] [blame] | 6375 | current_time(parent_inode); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 6376 | ret = btrfs_update_inode(trans, root, parent_inode); |
| 6377 | if (ret) |
Jeff Mahoney | 6664283 | 2016-06-10 18:19:25 -0400 | [diff] [blame] | 6378 | btrfs_abort_transaction(trans, ret); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 6379 | return ret; |
Chris Mason | fe66a05 | 2012-02-20 08:40:56 -0500 | [diff] [blame] | 6380 | |
| 6381 | fail_dir_item: |
| 6382 | if (unlikely(ino == BTRFS_FIRST_FREE_OBJECTID)) { |
| 6383 | u64 local_index; |
| 6384 | int err; |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 6385 | err = btrfs_del_root_ref(trans, fs_info, key.objectid, |
| 6386 | root->root_key.objectid, parent_ino, |
| 6387 | &local_index, name, name_len); |
Chris Mason | fe66a05 | 2012-02-20 08:40:56 -0500 | [diff] [blame] | 6388 | |
| 6389 | } else if (add_backref) { |
| 6390 | u64 local_index; |
| 6391 | int err; |
| 6392 | |
| 6393 | err = btrfs_del_inode_ref(trans, root, name, name_len, |
| 6394 | ino, parent_ino, &local_index); |
| 6395 | } |
| 6396 | return ret; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 6397 | } |
| 6398 | |
| 6399 | static int btrfs_add_nondir(struct btrfs_trans_handle *trans, |
Josef Bacik | a1b075d | 2010-11-19 20:36:11 +0000 | [diff] [blame] | 6400 | struct inode *dir, struct dentry *dentry, |
| 6401 | struct inode *inode, int backref, u64 index) |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 6402 | { |
Josef Bacik | a1b075d | 2010-11-19 20:36:11 +0000 | [diff] [blame] | 6403 | int err = btrfs_add_link(trans, dir, inode, |
| 6404 | dentry->d_name.name, dentry->d_name.len, |
| 6405 | backref, index); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 6406 | if (err > 0) |
| 6407 | err = -EEXIST; |
| 6408 | return err; |
| 6409 | } |
| 6410 | |
Josef Bacik | 618e21d | 2007-07-11 10:18:17 -0400 | [diff] [blame] | 6411 | static int btrfs_mknod(struct inode *dir, struct dentry *dentry, |
Al Viro | 1a67aaf | 2011-07-26 01:52:52 -0400 | [diff] [blame] | 6412 | umode_t mode, dev_t rdev) |
Josef Bacik | 618e21d | 2007-07-11 10:18:17 -0400 | [diff] [blame] | 6413 | { |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 6414 | struct btrfs_fs_info *fs_info = btrfs_sb(dir->i_sb); |
Josef Bacik | 618e21d | 2007-07-11 10:18:17 -0400 | [diff] [blame] | 6415 | struct btrfs_trans_handle *trans; |
| 6416 | struct btrfs_root *root = BTRFS_I(dir)->root; |
Chris Mason | 1832a6d | 2007-12-21 16:27:21 -0500 | [diff] [blame] | 6417 | struct inode *inode = NULL; |
Josef Bacik | 618e21d | 2007-07-11 10:18:17 -0400 | [diff] [blame] | 6418 | int err; |
| 6419 | int drop_inode = 0; |
| 6420 | u64 objectid; |
Chris Mason | 00e4e6b | 2008-08-05 11:18:09 -0400 | [diff] [blame] | 6421 | u64 index = 0; |
Josef Bacik | 618e21d | 2007-07-11 10:18:17 -0400 | [diff] [blame] | 6422 | |
Josef Bacik | 9ed74f2 | 2009-09-11 16:12:44 -0400 | [diff] [blame] | 6423 | /* |
| 6424 | * 2 for inode item and ref |
| 6425 | * 2 for dir items |
| 6426 | * 1 for xattr if selinux is on |
| 6427 | */ |
Yan, Zheng | a22285a | 2010-05-16 10:48:46 -0400 | [diff] [blame] | 6428 | trans = btrfs_start_transaction(root, 5); |
| 6429 | if (IS_ERR(trans)) |
| 6430 | return PTR_ERR(trans); |
Chris Mason | 1832a6d | 2007-12-21 16:27:21 -0500 | [diff] [blame] | 6431 | |
Li Zefan | 581bb05 | 2011-04-20 10:06:11 +0800 | [diff] [blame] | 6432 | err = btrfs_find_free_ino(root, &objectid); |
| 6433 | if (err) |
| 6434 | goto out_unlock; |
| 6435 | |
Josef Bacik | aec7477 | 2008-07-24 12:12:38 -0400 | [diff] [blame] | 6436 | inode = btrfs_new_inode(trans, root, dir, dentry->d_name.name, |
David Sterba | f85b737 | 2017-01-20 14:54:07 +0100 | [diff] [blame] | 6437 | dentry->d_name.len, btrfs_ino(BTRFS_I(dir)), objectid, |
| 6438 | mode, &index); |
Tsutomu Itoh | 7cf96da | 2011-04-25 19:43:53 -0400 | [diff] [blame] | 6439 | if (IS_ERR(inode)) { |
| 6440 | err = PTR_ERR(inode); |
Josef Bacik | 618e21d | 2007-07-11 10:18:17 -0400 | [diff] [blame] | 6441 | goto out_unlock; |
Tsutomu Itoh | 7cf96da | 2011-04-25 19:43:53 -0400 | [diff] [blame] | 6442 | } |
Josef Bacik | 618e21d | 2007-07-11 10:18:17 -0400 | [diff] [blame] | 6443 | |
Casey Schaufler | ad19db7 | 2011-12-15 10:09:07 -0500 | [diff] [blame] | 6444 | /* |
| 6445 | * If the active LSM wants to access the inode during |
| 6446 | * d_instantiate it needs these. Smack checks to see |
| 6447 | * if the filesystem supports xattrs by looking at the |
| 6448 | * ops vector. |
| 6449 | */ |
Casey Schaufler | ad19db7 | 2011-12-15 10:09:07 -0500 | [diff] [blame] | 6450 | inode->i_op = &btrfs_special_inode_operations; |
Chris Mason | b0d5d10 | 2014-09-08 13:08:51 -0700 | [diff] [blame] | 6451 | init_special_inode(inode, inode->i_mode, rdev); |
| 6452 | |
| 6453 | err = btrfs_init_inode_security(trans, inode, dir, &dentry->d_name); |
Josef Bacik | 618e21d | 2007-07-11 10:18:17 -0400 | [diff] [blame] | 6454 | if (err) |
Chris Mason | b0d5d10 | 2014-09-08 13:08:51 -0700 | [diff] [blame] | 6455 | goto out_unlock_inode; |
| 6456 | |
| 6457 | err = btrfs_add_nondir(trans, dir, dentry, inode, 0, index); |
| 6458 | if (err) { |
| 6459 | goto out_unlock_inode; |
| 6460 | } else { |
Yan | 1b4ab1b | 2007-08-29 09:11:44 -0400 | [diff] [blame] | 6461 | btrfs_update_inode(trans, root, inode); |
Chris Mason | b0d5d10 | 2014-09-08 13:08:51 -0700 | [diff] [blame] | 6462 | unlock_new_inode(inode); |
Al Viro | 08c422c | 2011-12-23 07:58:13 -0500 | [diff] [blame] | 6463 | d_instantiate(dentry, inode); |
Josef Bacik | 618e21d | 2007-07-11 10:18:17 -0400 | [diff] [blame] | 6464 | } |
Chris Mason | b0d5d10 | 2014-09-08 13:08:51 -0700 | [diff] [blame] | 6465 | |
Josef Bacik | 618e21d | 2007-07-11 10:18:17 -0400 | [diff] [blame] | 6466 | out_unlock: |
Jeff Mahoney | 3a45bb2 | 2016-09-09 21:39:03 -0400 | [diff] [blame] | 6467 | btrfs_end_transaction(trans); |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 6468 | btrfs_balance_delayed_items(fs_info); |
| 6469 | btrfs_btree_balance_dirty(fs_info); |
Josef Bacik | 618e21d | 2007-07-11 10:18:17 -0400 | [diff] [blame] | 6470 | if (drop_inode) { |
| 6471 | inode_dec_link_count(inode); |
| 6472 | iput(inode); |
| 6473 | } |
Josef Bacik | 618e21d | 2007-07-11 10:18:17 -0400 | [diff] [blame] | 6474 | return err; |
Chris Mason | b0d5d10 | 2014-09-08 13:08:51 -0700 | [diff] [blame] | 6475 | |
| 6476 | out_unlock_inode: |
| 6477 | drop_inode = 1; |
| 6478 | unlock_new_inode(inode); |
| 6479 | goto out_unlock; |
| 6480 | |
Josef Bacik | 618e21d | 2007-07-11 10:18:17 -0400 | [diff] [blame] | 6481 | } |
| 6482 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 6483 | static int btrfs_create(struct inode *dir, struct dentry *dentry, |
Al Viro | ebfc3b4 | 2012-06-10 18:05:36 -0400 | [diff] [blame] | 6484 | umode_t mode, bool excl) |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 6485 | { |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 6486 | struct btrfs_fs_info *fs_info = btrfs_sb(dir->i_sb); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 6487 | struct btrfs_trans_handle *trans; |
| 6488 | struct btrfs_root *root = BTRFS_I(dir)->root; |
Chris Mason | 1832a6d | 2007-12-21 16:27:21 -0500 | [diff] [blame] | 6489 | struct inode *inode = NULL; |
Filipe Brandenburger | 43baa57 | 2012-11-30 03:40:09 +0000 | [diff] [blame] | 6490 | int drop_inode_on_err = 0; |
Yan, Zheng | a22285a | 2010-05-16 10:48:46 -0400 | [diff] [blame] | 6491 | int err; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 6492 | u64 objectid; |
Chris Mason | 00e4e6b | 2008-08-05 11:18:09 -0400 | [diff] [blame] | 6493 | u64 index = 0; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 6494 | |
Josef Bacik | 9ed74f2 | 2009-09-11 16:12:44 -0400 | [diff] [blame] | 6495 | /* |
| 6496 | * 2 for inode item and ref |
| 6497 | * 2 for dir items |
| 6498 | * 1 for xattr if selinux is on |
| 6499 | */ |
Yan, Zheng | a22285a | 2010-05-16 10:48:46 -0400 | [diff] [blame] | 6500 | trans = btrfs_start_transaction(root, 5); |
| 6501 | if (IS_ERR(trans)) |
| 6502 | return PTR_ERR(trans); |
Josef Bacik | 9ed74f2 | 2009-09-11 16:12:44 -0400 | [diff] [blame] | 6503 | |
Li Zefan | 581bb05 | 2011-04-20 10:06:11 +0800 | [diff] [blame] | 6504 | err = btrfs_find_free_ino(root, &objectid); |
| 6505 | if (err) |
| 6506 | goto out_unlock; |
| 6507 | |
Josef Bacik | aec7477 | 2008-07-24 12:12:38 -0400 | [diff] [blame] | 6508 | inode = btrfs_new_inode(trans, root, dir, dentry->d_name.name, |
David Sterba | f85b737 | 2017-01-20 14:54:07 +0100 | [diff] [blame] | 6509 | dentry->d_name.len, btrfs_ino(BTRFS_I(dir)), objectid, |
| 6510 | mode, &index); |
Tsutomu Itoh | 7cf96da | 2011-04-25 19:43:53 -0400 | [diff] [blame] | 6511 | if (IS_ERR(inode)) { |
| 6512 | err = PTR_ERR(inode); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 6513 | goto out_unlock; |
Tsutomu Itoh | 7cf96da | 2011-04-25 19:43:53 -0400 | [diff] [blame] | 6514 | } |
Filipe Brandenburger | 43baa57 | 2012-11-30 03:40:09 +0000 | [diff] [blame] | 6515 | drop_inode_on_err = 1; |
Casey Schaufler | ad19db7 | 2011-12-15 10:09:07 -0500 | [diff] [blame] | 6516 | /* |
| 6517 | * If the active LSM wants to access the inode during |
| 6518 | * d_instantiate it needs these. Smack checks to see |
| 6519 | * if the filesystem supports xattrs by looking at the |
| 6520 | * ops vector. |
| 6521 | */ |
| 6522 | inode->i_fop = &btrfs_file_operations; |
| 6523 | inode->i_op = &btrfs_file_inode_operations; |
Chris Mason | b0d5d10 | 2014-09-08 13:08:51 -0700 | [diff] [blame] | 6524 | inode->i_mapping->a_ops = &btrfs_aops; |
Chris Mason | b0d5d10 | 2014-09-08 13:08:51 -0700 | [diff] [blame] | 6525 | |
| 6526 | err = btrfs_init_inode_security(trans, inode, dir, &dentry->d_name); |
| 6527 | if (err) |
| 6528 | goto out_unlock_inode; |
| 6529 | |
| 6530 | err = btrfs_update_inode(trans, root, inode); |
| 6531 | if (err) |
| 6532 | goto out_unlock_inode; |
Casey Schaufler | ad19db7 | 2011-12-15 10:09:07 -0500 | [diff] [blame] | 6533 | |
Josef Bacik | a1b075d | 2010-11-19 20:36:11 +0000 | [diff] [blame] | 6534 | err = btrfs_add_nondir(trans, dir, dentry, inode, 0, index); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 6535 | if (err) |
Chris Mason | b0d5d10 | 2014-09-08 13:08:51 -0700 | [diff] [blame] | 6536 | goto out_unlock_inode; |
Filipe Brandenburger | 43baa57 | 2012-11-30 03:40:09 +0000 | [diff] [blame] | 6537 | |
Filipe Brandenburger | 43baa57 | 2012-11-30 03:40:09 +0000 | [diff] [blame] | 6538 | BTRFS_I(inode)->io_tree.ops = &btrfs_extent_io_ops; |
Chris Mason | b0d5d10 | 2014-09-08 13:08:51 -0700 | [diff] [blame] | 6539 | unlock_new_inode(inode); |
Filipe Brandenburger | 43baa57 | 2012-11-30 03:40:09 +0000 | [diff] [blame] | 6540 | d_instantiate(dentry, inode); |
| 6541 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 6542 | out_unlock: |
Jeff Mahoney | 3a45bb2 | 2016-09-09 21:39:03 -0400 | [diff] [blame] | 6543 | btrfs_end_transaction(trans); |
Filipe Brandenburger | 43baa57 | 2012-11-30 03:40:09 +0000 | [diff] [blame] | 6544 | if (err && drop_inode_on_err) { |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 6545 | inode_dec_link_count(inode); |
| 6546 | iput(inode); |
| 6547 | } |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 6548 | btrfs_balance_delayed_items(fs_info); |
| 6549 | btrfs_btree_balance_dirty(fs_info); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 6550 | return err; |
Chris Mason | b0d5d10 | 2014-09-08 13:08:51 -0700 | [diff] [blame] | 6551 | |
| 6552 | out_unlock_inode: |
| 6553 | unlock_new_inode(inode); |
| 6554 | goto out_unlock; |
| 6555 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 6556 | } |
| 6557 | |
| 6558 | static int btrfs_link(struct dentry *old_dentry, struct inode *dir, |
| 6559 | struct dentry *dentry) |
| 6560 | { |
Filipe Manana | 271dba45 | 2016-01-05 16:24:05 +0000 | [diff] [blame] | 6561 | struct btrfs_trans_handle *trans = NULL; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 6562 | struct btrfs_root *root = BTRFS_I(dir)->root; |
David Howells | 2b0143b | 2015-03-17 22:25:59 +0000 | [diff] [blame] | 6563 | struct inode *inode = d_inode(old_dentry); |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 6564 | struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb); |
Chris Mason | 00e4e6b | 2008-08-05 11:18:09 -0400 | [diff] [blame] | 6565 | u64 index; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 6566 | int err; |
| 6567 | int drop_inode = 0; |
| 6568 | |
TARUISI Hiroaki | 4a8be42 | 2009-11-12 07:14:26 +0000 | [diff] [blame] | 6569 | /* do not allow sys_link's with other subvols of the same device */ |
| 6570 | if (root->objectid != BTRFS_I(inode)->root->objectid) |
Mark Fasheh | 3ab3564 | 2011-03-22 17:20:26 +0000 | [diff] [blame] | 6571 | return -EXDEV; |
TARUISI Hiroaki | 4a8be42 | 2009-11-12 07:14:26 +0000 | [diff] [blame] | 6572 | |
Mark Fasheh | f186373 | 2012-08-08 11:32:27 -0700 | [diff] [blame] | 6573 | if (inode->i_nlink >= BTRFS_LINK_MAX) |
Al Viro | c055e99 | 2011-03-04 17:15:18 +0000 | [diff] [blame] | 6574 | return -EMLINK; |
Josef Bacik | 9ed74f2 | 2009-09-11 16:12:44 -0400 | [diff] [blame] | 6575 | |
Chris Mason | 3de4586 | 2008-11-17 21:02:50 -0500 | [diff] [blame] | 6576 | err = btrfs_set_inode_index(dir, &index); |
Josef Bacik | aec7477 | 2008-07-24 12:12:38 -0400 | [diff] [blame] | 6577 | if (err) |
| 6578 | goto fail; |
| 6579 | |
Yan, Zheng | a22285a | 2010-05-16 10:48:46 -0400 | [diff] [blame] | 6580 | /* |
Miao Xie | 7e6b646 | 2011-02-18 09:21:17 +0000 | [diff] [blame] | 6581 | * 2 items for inode and inode ref |
Yan, Zheng | a22285a | 2010-05-16 10:48:46 -0400 | [diff] [blame] | 6582 | * 2 items for dir items |
Miao Xie | 7e6b646 | 2011-02-18 09:21:17 +0000 | [diff] [blame] | 6583 | * 1 item for parent inode |
Yan, Zheng | a22285a | 2010-05-16 10:48:46 -0400 | [diff] [blame] | 6584 | */ |
Miao Xie | 7e6b646 | 2011-02-18 09:21:17 +0000 | [diff] [blame] | 6585 | trans = btrfs_start_transaction(root, 5); |
Yan, Zheng | a22285a | 2010-05-16 10:48:46 -0400 | [diff] [blame] | 6586 | if (IS_ERR(trans)) { |
| 6587 | err = PTR_ERR(trans); |
Filipe Manana | 271dba45 | 2016-01-05 16:24:05 +0000 | [diff] [blame] | 6588 | trans = NULL; |
Yan, Zheng | a22285a | 2010-05-16 10:48:46 -0400 | [diff] [blame] | 6589 | goto fail; |
| 6590 | } |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 6591 | |
Miao Xie | 67de117 | 2013-12-26 13:07:06 +0800 | [diff] [blame] | 6592 | /* There are several dir indexes for this inode, clear the cache. */ |
| 6593 | BTRFS_I(inode)->dir_index = 0ULL; |
Zach Brown | 8b558c5 | 2013-10-16 12:10:34 -0700 | [diff] [blame] | 6594 | inc_nlink(inode); |
Josef Bacik | 0c4d2d9 | 2012-04-05 15:03:02 -0400 | [diff] [blame] | 6595 | inode_inc_iversion(inode); |
Deepa Dinamani | c2050a4 | 2016-09-14 07:48:06 -0700 | [diff] [blame] | 6596 | inode->i_ctime = current_time(inode); |
Al Viro | 7de9c6ee | 2010-10-23 11:11:40 -0400 | [diff] [blame] | 6597 | ihold(inode); |
Josef Bacik | e997615 | 2012-10-11 15:53:56 -0400 | [diff] [blame] | 6598 | set_bit(BTRFS_INODE_COPY_EVERYTHING, &BTRFS_I(inode)->runtime_flags); |
Josef Bacik | aec7477 | 2008-07-24 12:12:38 -0400 | [diff] [blame] | 6599 | |
Josef Bacik | a1b075d | 2010-11-19 20:36:11 +0000 | [diff] [blame] | 6600 | err = btrfs_add_nondir(trans, dir, dentry, inode, 1, index); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 6601 | |
Yan, Zheng | a571952 | 2009-09-24 09:17:31 -0400 | [diff] [blame] | 6602 | if (err) { |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 6603 | drop_inode = 1; |
Yan, Zheng | a571952 | 2009-09-24 09:17:31 -0400 | [diff] [blame] | 6604 | } else { |
Al Viro | 10d9f30 | 2011-07-16 23:09:10 -0400 | [diff] [blame] | 6605 | struct dentry *parent = dentry->d_parent; |
Yan, Zheng | a571952 | 2009-09-24 09:17:31 -0400 | [diff] [blame] | 6606 | err = btrfs_update_inode(trans, root, inode); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 6607 | if (err) |
| 6608 | goto fail; |
Filipe Manana | ef3b9af | 2014-04-27 20:40:45 +0100 | [diff] [blame] | 6609 | if (inode->i_nlink == 1) { |
| 6610 | /* |
| 6611 | * If new hard link count is 1, it's a file created |
| 6612 | * with open(2) O_TMPFILE flag. |
| 6613 | */ |
| 6614 | err = btrfs_orphan_del(trans, inode); |
| 6615 | if (err) |
| 6616 | goto fail; |
| 6617 | } |
Al Viro | 08c422c | 2011-12-23 07:58:13 -0500 | [diff] [blame] | 6618 | d_instantiate(dentry, inode); |
Nikolay Borisov | 9ca5fbfb | 2017-01-18 00:31:31 +0200 | [diff] [blame] | 6619 | btrfs_log_new_name(trans, BTRFS_I(inode), NULL, parent); |
Yan, Zheng | a571952 | 2009-09-24 09:17:31 -0400 | [diff] [blame] | 6620 | } |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 6621 | |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 6622 | btrfs_balance_delayed_items(fs_info); |
Chris Mason | 1832a6d | 2007-12-21 16:27:21 -0500 | [diff] [blame] | 6623 | fail: |
Filipe Manana | 271dba45 | 2016-01-05 16:24:05 +0000 | [diff] [blame] | 6624 | if (trans) |
Jeff Mahoney | 3a45bb2 | 2016-09-09 21:39:03 -0400 | [diff] [blame] | 6625 | btrfs_end_transaction(trans); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 6626 | if (drop_inode) { |
| 6627 | inode_dec_link_count(inode); |
| 6628 | iput(inode); |
| 6629 | } |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 6630 | btrfs_btree_balance_dirty(fs_info); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 6631 | return err; |
| 6632 | } |
| 6633 | |
Al Viro | 18bb1db | 2011-07-26 01:41:39 -0400 | [diff] [blame] | 6634 | 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] | 6635 | { |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 6636 | struct btrfs_fs_info *fs_info = btrfs_sb(dir->i_sb); |
Chris Mason | b9d8666 | 2008-05-02 16:13:49 -0400 | [diff] [blame] | 6637 | struct inode *inode = NULL; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 6638 | struct btrfs_trans_handle *trans; |
| 6639 | struct btrfs_root *root = BTRFS_I(dir)->root; |
| 6640 | int err = 0; |
| 6641 | int drop_on_err = 0; |
Chris Mason | b9d8666 | 2008-05-02 16:13:49 -0400 | [diff] [blame] | 6642 | u64 objectid = 0; |
Chris Mason | 00e4e6b | 2008-08-05 11:18:09 -0400 | [diff] [blame] | 6643 | u64 index = 0; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 6644 | |
Josef Bacik | 9ed74f2 | 2009-09-11 16:12:44 -0400 | [diff] [blame] | 6645 | /* |
| 6646 | * 2 items for inode and ref |
| 6647 | * 2 items for dir items |
| 6648 | * 1 for xattr if selinux is on |
| 6649 | */ |
Yan, Zheng | a22285a | 2010-05-16 10:48:46 -0400 | [diff] [blame] | 6650 | trans = btrfs_start_transaction(root, 5); |
| 6651 | if (IS_ERR(trans)) |
| 6652 | return PTR_ERR(trans); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 6653 | |
Li Zefan | 581bb05 | 2011-04-20 10:06:11 +0800 | [diff] [blame] | 6654 | err = btrfs_find_free_ino(root, &objectid); |
| 6655 | if (err) |
| 6656 | goto out_fail; |
| 6657 | |
Josef Bacik | aec7477 | 2008-07-24 12:12:38 -0400 | [diff] [blame] | 6658 | inode = btrfs_new_inode(trans, root, dir, dentry->d_name.name, |
David Sterba | f85b737 | 2017-01-20 14:54:07 +0100 | [diff] [blame] | 6659 | dentry->d_name.len, btrfs_ino(BTRFS_I(dir)), objectid, |
| 6660 | S_IFDIR | mode, &index); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 6661 | if (IS_ERR(inode)) { |
| 6662 | err = PTR_ERR(inode); |
| 6663 | goto out_fail; |
| 6664 | } |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 6665 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 6666 | drop_on_err = 1; |
Chris Mason | b0d5d10 | 2014-09-08 13:08:51 -0700 | [diff] [blame] | 6667 | /* these must be set before we unlock the inode */ |
| 6668 | inode->i_op = &btrfs_dir_inode_operations; |
| 6669 | inode->i_fop = &btrfs_dir_file_operations; |
Josef Bacik | 33268ea | 2008-07-24 12:16:36 -0400 | [diff] [blame] | 6670 | |
Eric Paris | 2a7dba3 | 2011-02-01 11:05:39 -0500 | [diff] [blame] | 6671 | err = btrfs_init_inode_security(trans, inode, dir, &dentry->d_name); |
Josef Bacik | 33268ea | 2008-07-24 12:16:36 -0400 | [diff] [blame] | 6672 | if (err) |
Chris Mason | b0d5d10 | 2014-09-08 13:08:51 -0700 | [diff] [blame] | 6673 | goto out_fail_inode; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 6674 | |
Chris Mason | dbe674a | 2008-07-17 12:54:05 -0400 | [diff] [blame] | 6675 | btrfs_i_size_write(inode, 0); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 6676 | err = btrfs_update_inode(trans, root, inode); |
| 6677 | if (err) |
Chris Mason | b0d5d10 | 2014-09-08 13:08:51 -0700 | [diff] [blame] | 6678 | goto out_fail_inode; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 6679 | |
Josef Bacik | a1b075d | 2010-11-19 20:36:11 +0000 | [diff] [blame] | 6680 | err = btrfs_add_link(trans, dir, inode, dentry->d_name.name, |
| 6681 | dentry->d_name.len, 0, index); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 6682 | if (err) |
Chris Mason | b0d5d10 | 2014-09-08 13:08:51 -0700 | [diff] [blame] | 6683 | goto out_fail_inode; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 6684 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 6685 | d_instantiate(dentry, inode); |
Chris Mason | b0d5d10 | 2014-09-08 13:08:51 -0700 | [diff] [blame] | 6686 | /* |
| 6687 | * mkdir is special. We're unlocking after we call d_instantiate |
| 6688 | * to avoid a race with nfsd calling d_instantiate. |
| 6689 | */ |
| 6690 | unlock_new_inode(inode); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 6691 | drop_on_err = 0; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 6692 | |
| 6693 | out_fail: |
Jeff Mahoney | 3a45bb2 | 2016-09-09 21:39:03 -0400 | [diff] [blame] | 6694 | btrfs_end_transaction(trans); |
Wang Shilong | c7cfb8a | 2014-12-24 14:45:30 +0800 | [diff] [blame] | 6695 | if (drop_on_err) { |
| 6696 | inode_dec_link_count(inode); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 6697 | iput(inode); |
Wang Shilong | c7cfb8a | 2014-12-24 14:45:30 +0800 | [diff] [blame] | 6698 | } |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 6699 | btrfs_balance_delayed_items(fs_info); |
| 6700 | btrfs_btree_balance_dirty(fs_info); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 6701 | return err; |
Chris Mason | b0d5d10 | 2014-09-08 13:08:51 -0700 | [diff] [blame] | 6702 | |
| 6703 | out_fail_inode: |
| 6704 | unlock_new_inode(inode); |
| 6705 | goto out_fail; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 6706 | } |
| 6707 | |
Qu Wenruo | e6c4efd | 2014-09-17 11:53:35 +0800 | [diff] [blame] | 6708 | /* Find next extent map of a given extent map, caller needs to ensure locks */ |
| 6709 | static struct extent_map *next_extent_map(struct extent_map *em) |
| 6710 | { |
| 6711 | struct rb_node *next; |
| 6712 | |
| 6713 | next = rb_next(&em->rb_node); |
| 6714 | if (!next) |
| 6715 | return NULL; |
| 6716 | return container_of(next, struct extent_map, rb_node); |
| 6717 | } |
| 6718 | |
| 6719 | static struct extent_map *prev_extent_map(struct extent_map *em) |
| 6720 | { |
| 6721 | struct rb_node *prev; |
| 6722 | |
| 6723 | prev = rb_prev(&em->rb_node); |
| 6724 | if (!prev) |
| 6725 | return NULL; |
| 6726 | return container_of(prev, struct extent_map, rb_node); |
| 6727 | } |
| 6728 | |
Chris Mason | d352ac6 | 2008-09-29 15:18:18 -0400 | [diff] [blame] | 6729 | /* helper for btfs_get_extent. Given an existing extent in the tree, |
Qu Wenruo | e6c4efd | 2014-09-17 11:53:35 +0800 | [diff] [blame] | 6730 | * the existing extent is the nearest extent to map_start, |
Chris Mason | d352ac6 | 2008-09-29 15:18:18 -0400 | [diff] [blame] | 6731 | * 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] | 6732 | * the best fitted new extent into the tree. |
Chris Mason | d352ac6 | 2008-09-29 15:18:18 -0400 | [diff] [blame] | 6733 | */ |
Chris Mason | 3b95151 | 2008-04-17 11:29:12 -0400 | [diff] [blame] | 6734 | static int merge_extent_mapping(struct extent_map_tree *em_tree, |
| 6735 | struct extent_map *existing, |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 6736 | struct extent_map *em, |
Qu Wenruo | 51f395a | 2014-08-08 13:06:20 +0800 | [diff] [blame] | 6737 | u64 map_start) |
Chris Mason | 3b95151 | 2008-04-17 11:29:12 -0400 | [diff] [blame] | 6738 | { |
Qu Wenruo | e6c4efd | 2014-09-17 11:53:35 +0800 | [diff] [blame] | 6739 | struct extent_map *prev; |
| 6740 | struct extent_map *next; |
| 6741 | u64 start; |
| 6742 | u64 end; |
Chris Mason | 3b95151 | 2008-04-17 11:29:12 -0400 | [diff] [blame] | 6743 | u64 start_diff; |
Chris Mason | 3b95151 | 2008-04-17 11:29:12 -0400 | [diff] [blame] | 6744 | |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 6745 | BUG_ON(map_start < em->start || map_start >= extent_map_end(em)); |
Qu Wenruo | e6c4efd | 2014-09-17 11:53:35 +0800 | [diff] [blame] | 6746 | |
| 6747 | if (existing->start > map_start) { |
| 6748 | next = existing; |
| 6749 | prev = prev_extent_map(next); |
| 6750 | } else { |
| 6751 | prev = existing; |
| 6752 | next = next_extent_map(prev); |
| 6753 | } |
| 6754 | |
| 6755 | start = prev ? extent_map_end(prev) : em->start; |
| 6756 | start = max_t(u64, start, em->start); |
| 6757 | end = next ? next->start : extent_map_end(em); |
| 6758 | end = min_t(u64, end, extent_map_end(em)); |
| 6759 | start_diff = start - em->start; |
| 6760 | em->start = start; |
| 6761 | em->len = end - start; |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 6762 | if (em->block_start < EXTENT_MAP_LAST_BYTE && |
| 6763 | !test_bit(EXTENT_FLAG_COMPRESSED, &em->flags)) { |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 6764 | em->block_start += start_diff; |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 6765 | em->block_len -= start_diff; |
| 6766 | } |
Josef Bacik | 09a2a8f9 | 2013-04-05 16:51:15 -0400 | [diff] [blame] | 6767 | return add_extent_mapping(em_tree, em, 0); |
Chris Mason | 3b95151 | 2008-04-17 11:29:12 -0400 | [diff] [blame] | 6768 | } |
| 6769 | |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 6770 | static noinline int uncompress_inline(struct btrfs_path *path, |
Byongho Lee | e40da0e | 2015-05-19 23:46:45 +0900 | [diff] [blame] | 6771 | struct page *page, |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 6772 | size_t pg_offset, u64 extent_offset, |
| 6773 | struct btrfs_file_extent_item *item) |
| 6774 | { |
| 6775 | int ret; |
| 6776 | struct extent_buffer *leaf = path->nodes[0]; |
| 6777 | char *tmp; |
| 6778 | size_t max_size; |
| 6779 | unsigned long inline_size; |
| 6780 | unsigned long ptr; |
Li Zefan | 261507a0 | 2010-12-17 14:21:50 +0800 | [diff] [blame] | 6781 | int compress_type; |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 6782 | |
| 6783 | WARN_ON(pg_offset != 0); |
Li Zefan | 261507a0 | 2010-12-17 14:21:50 +0800 | [diff] [blame] | 6784 | compress_type = btrfs_file_extent_compression(leaf, item); |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 6785 | max_size = btrfs_file_extent_ram_bytes(leaf, item); |
| 6786 | inline_size = btrfs_file_extent_inline_item_len(leaf, |
Ross Kirk | dd3cc16 | 2013-09-16 15:58:09 +0100 | [diff] [blame] | 6787 | btrfs_item_nr(path->slots[0])); |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 6788 | tmp = kmalloc(inline_size, GFP_NOFS); |
Tsutomu Itoh | 8d41371 | 2011-04-25 19:43:52 -0400 | [diff] [blame] | 6789 | if (!tmp) |
| 6790 | return -ENOMEM; |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 6791 | ptr = btrfs_file_extent_inline_start(item); |
| 6792 | |
| 6793 | read_extent_buffer(leaf, tmp, ptr, inline_size); |
| 6794 | |
Kirill A. Shutemov | 09cbfea | 2016-04-01 15:29:47 +0300 | [diff] [blame] | 6795 | max_size = min_t(unsigned long, PAGE_SIZE, max_size); |
Li Zefan | 261507a0 | 2010-12-17 14:21:50 +0800 | [diff] [blame] | 6796 | ret = btrfs_decompress(compress_type, tmp, page, |
| 6797 | extent_offset, inline_size, max_size); |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 6798 | kfree(tmp); |
Zach Brown | 166ae5a | 2014-05-09 17:15:10 -0400 | [diff] [blame] | 6799 | return ret; |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 6800 | } |
| 6801 | |
Chris Mason | d352ac6 | 2008-09-29 15:18:18 -0400 | [diff] [blame] | 6802 | /* |
| 6803 | * 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] | 6804 | * the ugly parts come from merging extents from the disk with the in-ram |
| 6805 | * representation. This gets more complex because of the data=ordered code, |
Chris Mason | d352ac6 | 2008-09-29 15:18:18 -0400 | [diff] [blame] | 6806 | * where the in-ram extents might be locked pending data=ordered completion. |
| 6807 | * |
| 6808 | * This also copies inline extents directly into the page. |
| 6809 | */ |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 6810 | |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 6811 | struct extent_map *btrfs_get_extent(struct inode *inode, struct page *page, |
Chris Mason | 70dec80 | 2008-01-29 09:59:12 -0500 | [diff] [blame] | 6812 | size_t pg_offset, u64 start, u64 len, |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 6813 | int create) |
| 6814 | { |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 6815 | struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb); |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 6816 | int ret; |
| 6817 | int err = 0; |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 6818 | u64 extent_start = 0; |
| 6819 | u64 extent_end = 0; |
Nikolay Borisov | 4a0cc7c | 2017-01-10 20:35:31 +0200 | [diff] [blame] | 6820 | u64 objectid = btrfs_ino(BTRFS_I(inode)); |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 6821 | u32 found_type; |
Chris Mason | f421950 | 2008-07-22 11:18:09 -0400 | [diff] [blame] | 6822 | struct btrfs_path *path = NULL; |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 6823 | struct btrfs_root *root = BTRFS_I(inode)->root; |
| 6824 | struct btrfs_file_extent_item *item; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 6825 | struct extent_buffer *leaf; |
| 6826 | struct btrfs_key found_key; |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 6827 | struct extent_map *em = NULL; |
| 6828 | struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree; |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 6829 | struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree; |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 6830 | struct btrfs_trans_handle *trans = NULL; |
Filipe Manana | 7ffbb59 | 2014-06-09 03:48:05 +0100 | [diff] [blame] | 6831 | const bool new_inline = !page || create; |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 6832 | |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 6833 | again: |
Chris Mason | 890871b | 2009-09-02 16:24:52 -0400 | [diff] [blame] | 6834 | read_lock(&em_tree->lock); |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 6835 | em = lookup_extent_mapping(em_tree, start, len); |
Chris Mason | a061fc8 | 2008-05-07 11:43:44 -0400 | [diff] [blame] | 6836 | if (em) |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 6837 | em->bdev = fs_info->fs_devices->latest_bdev; |
Chris Mason | 890871b | 2009-09-02 16:24:52 -0400 | [diff] [blame] | 6838 | read_unlock(&em_tree->lock); |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 6839 | |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 6840 | if (em) { |
Chris Mason | e1c4b74 | 2008-04-22 13:26:46 -0400 | [diff] [blame] | 6841 | if (em->start > start || em->start + em->len <= start) |
| 6842 | free_extent_map(em); |
| 6843 | else if (em->block_start == EXTENT_MAP_INLINE && page) |
Chris Mason | 70dec80 | 2008-01-29 09:59:12 -0500 | [diff] [blame] | 6844 | free_extent_map(em); |
| 6845 | else |
| 6846 | goto out; |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 6847 | } |
David Sterba | 172ddd6 | 2011-04-21 00:48:27 +0200 | [diff] [blame] | 6848 | em = alloc_extent_map(); |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 6849 | if (!em) { |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 6850 | err = -ENOMEM; |
| 6851 | goto out; |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 6852 | } |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 6853 | em->bdev = fs_info->fs_devices->latest_bdev; |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 6854 | em->start = EXTENT_MAP_HOLE; |
Chris Mason | 445a694 | 2008-11-10 11:53:33 -0500 | [diff] [blame] | 6855 | em->orig_start = EXTENT_MAP_HOLE; |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 6856 | em->len = (u64)-1; |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 6857 | em->block_len = (u64)-1; |
Chris Mason | f421950 | 2008-07-22 11:18:09 -0400 | [diff] [blame] | 6858 | |
| 6859 | if (!path) { |
| 6860 | path = btrfs_alloc_path(); |
Josef Bacik | 026fd31 | 2011-05-13 10:32:11 -0400 | [diff] [blame] | 6861 | if (!path) { |
| 6862 | err = -ENOMEM; |
| 6863 | goto out; |
| 6864 | } |
| 6865 | /* |
| 6866 | * Chances are we'll be called again, so go ahead and do |
| 6867 | * readahead |
| 6868 | */ |
David Sterba | e4058b5 | 2015-11-27 16:31:35 +0100 | [diff] [blame] | 6869 | path->reada = READA_FORWARD; |
Chris Mason | f421950 | 2008-07-22 11:18:09 -0400 | [diff] [blame] | 6870 | } |
| 6871 | |
Chris Mason | 179e29e | 2007-11-01 11:28:41 -0400 | [diff] [blame] | 6872 | ret = btrfs_lookup_file_extent(trans, root, path, |
| 6873 | objectid, start, trans != NULL); |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 6874 | if (ret < 0) { |
| 6875 | err = ret; |
| 6876 | goto out; |
| 6877 | } |
| 6878 | |
| 6879 | if (ret != 0) { |
| 6880 | if (path->slots[0] == 0) |
| 6881 | goto not_found; |
| 6882 | path->slots[0]--; |
| 6883 | } |
| 6884 | |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 6885 | leaf = path->nodes[0]; |
| 6886 | item = btrfs_item_ptr(leaf, path->slots[0], |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 6887 | struct btrfs_file_extent_item); |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 6888 | /* are we inside the extent that was found? */ |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 6889 | btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]); |
David Sterba | 962a298 | 2014-06-04 18:41:45 +0200 | [diff] [blame] | 6890 | found_type = found_key.type; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 6891 | if (found_key.objectid != objectid || |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 6892 | found_type != BTRFS_EXTENT_DATA_KEY) { |
Josef Bacik | 25a5034 | 2013-10-14 12:08:38 -0400 | [diff] [blame] | 6893 | /* |
| 6894 | * If we backup past the first extent we want to move forward |
| 6895 | * and see if there is an extent in front of us, otherwise we'll |
| 6896 | * say there is a hole for our whole search range which can |
| 6897 | * cause problems. |
| 6898 | */ |
| 6899 | extent_end = start; |
| 6900 | goto next; |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 6901 | } |
| 6902 | |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 6903 | found_type = btrfs_file_extent_type(leaf, item); |
| 6904 | extent_start = found_key.offset; |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 6905 | if (found_type == BTRFS_FILE_EXTENT_REG || |
| 6906 | found_type == BTRFS_FILE_EXTENT_PREALLOC) { |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 6907 | extent_end = extent_start + |
Chris Mason | db94535 | 2007-10-15 16:15:53 -0400 | [diff] [blame] | 6908 | btrfs_file_extent_num_bytes(leaf, item); |
Yan Zheng | 9036c10 | 2008-10-30 14:19:41 -0400 | [diff] [blame] | 6909 | } else if (found_type == BTRFS_FILE_EXTENT_INLINE) { |
| 6910 | size_t size; |
Chris Mason | 514ac8a | 2014-01-03 21:07:00 -0800 | [diff] [blame] | 6911 | size = btrfs_file_extent_inline_len(leaf, path->slots[0], item); |
Jeff Mahoney | da17066 | 2016-06-15 09:22:56 -0400 | [diff] [blame] | 6912 | extent_end = ALIGN(extent_start + size, |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 6913 | fs_info->sectorsize); |
Yan Zheng | 9036c10 | 2008-10-30 14:19:41 -0400 | [diff] [blame] | 6914 | } |
Josef Bacik | 25a5034 | 2013-10-14 12:08:38 -0400 | [diff] [blame] | 6915 | next: |
Yan Zheng | 9036c10 | 2008-10-30 14:19:41 -0400 | [diff] [blame] | 6916 | if (start >= extent_end) { |
| 6917 | path->slots[0]++; |
| 6918 | if (path->slots[0] >= btrfs_header_nritems(leaf)) { |
| 6919 | ret = btrfs_next_leaf(root, path); |
| 6920 | if (ret < 0) { |
| 6921 | err = ret; |
| 6922 | goto out; |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 6923 | } |
Yan Zheng | 9036c10 | 2008-10-30 14:19:41 -0400 | [diff] [blame] | 6924 | if (ret > 0) |
| 6925 | goto not_found; |
| 6926 | leaf = path->nodes[0]; |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 6927 | } |
Yan Zheng | 9036c10 | 2008-10-30 14:19:41 -0400 | [diff] [blame] | 6928 | btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]); |
| 6929 | if (found_key.objectid != objectid || |
| 6930 | found_key.type != BTRFS_EXTENT_DATA_KEY) |
| 6931 | goto not_found; |
| 6932 | if (start + len <= found_key.offset) |
| 6933 | goto not_found; |
Wang Shilong | e2eca69 | 2014-07-17 11:44:14 +0800 | [diff] [blame] | 6934 | if (start > found_key.offset) |
| 6935 | goto next; |
Yan Zheng | 9036c10 | 2008-10-30 14:19:41 -0400 | [diff] [blame] | 6936 | em->start = start; |
Josef Bacik | 70c8a91 | 2012-10-11 16:54:30 -0400 | [diff] [blame] | 6937 | em->orig_start = start; |
Yan Zheng | 9036c10 | 2008-10-30 14:19:41 -0400 | [diff] [blame] | 6938 | em->len = found_key.offset - start; |
| 6939 | goto not_found_em; |
| 6940 | } |
| 6941 | |
Filipe Manana | 7ffbb59 | 2014-06-09 03:48:05 +0100 | [diff] [blame] | 6942 | btrfs_extent_item_to_extent_map(inode, path, item, new_inline, em); |
| 6943 | |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 6944 | if (found_type == BTRFS_FILE_EXTENT_REG || |
| 6945 | found_type == BTRFS_FILE_EXTENT_PREALLOC) { |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 6946 | goto insert; |
| 6947 | } else if (found_type == BTRFS_FILE_EXTENT_INLINE) { |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 6948 | unsigned long ptr; |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 6949 | char *map; |
Chris Mason | 3326d1b | 2007-10-15 16:18:25 -0400 | [diff] [blame] | 6950 | size_t size; |
| 6951 | size_t extent_offset; |
| 6952 | size_t copy_size; |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 6953 | |
Filipe Manana | 7ffbb59 | 2014-06-09 03:48:05 +0100 | [diff] [blame] | 6954 | if (new_inline) |
Yan | 689f934 | 2007-10-29 11:41:07 -0400 | [diff] [blame] | 6955 | goto out; |
Yan | 689f934 | 2007-10-29 11:41:07 -0400 | [diff] [blame] | 6956 | |
Chris Mason | 514ac8a | 2014-01-03 21:07:00 -0800 | [diff] [blame] | 6957 | size = btrfs_file_extent_inline_len(leaf, path->slots[0], item); |
Yan Zheng | 9036c10 | 2008-10-30 14:19:41 -0400 | [diff] [blame] | 6958 | extent_offset = page_offset(page) + pg_offset - extent_start; |
Kirill A. Shutemov | 09cbfea | 2016-04-01 15:29:47 +0300 | [diff] [blame] | 6959 | copy_size = min_t(u64, PAGE_SIZE - pg_offset, |
| 6960 | size - extent_offset); |
Chris Mason | 3326d1b | 2007-10-15 16:18:25 -0400 | [diff] [blame] | 6961 | em->start = extent_start + extent_offset; |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 6962 | em->len = ALIGN(copy_size, fs_info->sectorsize); |
Josef Bacik | b493968 | 2012-12-03 10:31:19 -0500 | [diff] [blame] | 6963 | em->orig_block_len = em->len; |
Josef Bacik | 70c8a91 | 2012-10-11 16:54:30 -0400 | [diff] [blame] | 6964 | em->orig_start = em->start; |
Yan | 689f934 | 2007-10-29 11:41:07 -0400 | [diff] [blame] | 6965 | ptr = btrfs_file_extent_inline_start(item) + extent_offset; |
Chris Mason | 179e29e | 2007-11-01 11:28:41 -0400 | [diff] [blame] | 6966 | if (create == 0 && !PageUptodate(page)) { |
Li Zefan | 261507a0 | 2010-12-17 14:21:50 +0800 | [diff] [blame] | 6967 | if (btrfs_file_extent_compression(leaf, item) != |
| 6968 | BTRFS_COMPRESS_NONE) { |
Byongho Lee | e40da0e | 2015-05-19 23:46:45 +0900 | [diff] [blame] | 6969 | ret = uncompress_inline(path, page, pg_offset, |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 6970 | extent_offset, item); |
Zach Brown | 166ae5a | 2014-05-09 17:15:10 -0400 | [diff] [blame] | 6971 | if (ret) { |
| 6972 | err = ret; |
| 6973 | goto out; |
| 6974 | } |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 6975 | } else { |
| 6976 | map = kmap(page); |
| 6977 | read_extent_buffer(leaf, map + pg_offset, ptr, |
| 6978 | copy_size); |
Kirill A. Shutemov | 09cbfea | 2016-04-01 15:29:47 +0300 | [diff] [blame] | 6979 | if (pg_offset + copy_size < PAGE_SIZE) { |
Chris Mason | 93c82d5 | 2009-09-11 12:36:29 -0400 | [diff] [blame] | 6980 | memset(map + pg_offset + copy_size, 0, |
Kirill A. Shutemov | 09cbfea | 2016-04-01 15:29:47 +0300 | [diff] [blame] | 6981 | PAGE_SIZE - pg_offset - |
Chris Mason | 93c82d5 | 2009-09-11 12:36:29 -0400 | [diff] [blame] | 6982 | copy_size); |
| 6983 | } |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 6984 | kunmap(page); |
| 6985 | } |
Chris Mason | 179e29e | 2007-11-01 11:28:41 -0400 | [diff] [blame] | 6986 | flush_dcache_page(page); |
| 6987 | } else if (create && PageUptodate(page)) { |
Jan Schmidt | 6bf7e08 | 2011-12-01 14:35:19 +0100 | [diff] [blame] | 6988 | BUG(); |
Chris Mason | 179e29e | 2007-11-01 11:28:41 -0400 | [diff] [blame] | 6989 | if (!trans) { |
| 6990 | kunmap(page); |
| 6991 | free_extent_map(em); |
| 6992 | em = NULL; |
Chris Mason | ff5714c | 2011-05-28 07:00:39 -0400 | [diff] [blame] | 6993 | |
David Sterba | b3b4aa7 | 2011-04-21 01:20:15 +0200 | [diff] [blame] | 6994 | btrfs_release_path(path); |
Josef Bacik | 7a7eaa4 | 2011-04-13 12:54:33 -0400 | [diff] [blame] | 6995 | trans = btrfs_join_transaction(root); |
Chris Mason | ff5714c | 2011-05-28 07:00:39 -0400 | [diff] [blame] | 6996 | |
Tsutomu Itoh | 3612b49 | 2011-01-25 02:51:38 +0000 | [diff] [blame] | 6997 | if (IS_ERR(trans)) |
| 6998 | return ERR_CAST(trans); |
Chris Mason | 179e29e | 2007-11-01 11:28:41 -0400 | [diff] [blame] | 6999 | goto again; |
| 7000 | } |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 7001 | map = kmap(page); |
Chris Mason | 70dec80 | 2008-01-29 09:59:12 -0500 | [diff] [blame] | 7002 | write_extent_buffer(leaf, map + pg_offset, ptr, |
Chris Mason | 179e29e | 2007-11-01 11:28:41 -0400 | [diff] [blame] | 7003 | copy_size); |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 7004 | kunmap(page); |
Chris Mason | 179e29e | 2007-11-01 11:28:41 -0400 | [diff] [blame] | 7005 | btrfs_mark_buffer_dirty(leaf); |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 7006 | } |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 7007 | set_extent_uptodate(io_tree, em->start, |
Arne Jansen | 507903b | 2011-04-06 10:02:20 +0000 | [diff] [blame] | 7008 | extent_map_end(em) - 1, NULL, GFP_NOFS); |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 7009 | goto insert; |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 7010 | } |
| 7011 | not_found: |
| 7012 | em->start = start; |
Josef Bacik | 70c8a91 | 2012-10-11 16:54:30 -0400 | [diff] [blame] | 7013 | em->orig_start = start; |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 7014 | em->len = len; |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 7015 | not_found_em: |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 7016 | em->block_start = EXTENT_MAP_HOLE; |
Yan Zheng | 9036c10 | 2008-10-30 14:19:41 -0400 | [diff] [blame] | 7017 | set_bit(EXTENT_FLAG_VACANCY, &em->flags); |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 7018 | insert: |
David Sterba | b3b4aa7 | 2011-04-21 01:20:15 +0200 | [diff] [blame] | 7019 | btrfs_release_path(path); |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 7020 | if (em->start > start || extent_map_end(em) <= start) { |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 7021 | btrfs_err(fs_info, |
Jeff Mahoney | 5d163e0 | 2016-09-20 10:05:00 -0400 | [diff] [blame] | 7022 | "bad extent! em: [%llu %llu] passed [%llu %llu]", |
| 7023 | em->start, em->len, start, len); |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 7024 | err = -EIO; |
| 7025 | goto out; |
| 7026 | } |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 7027 | |
| 7028 | err = 0; |
Chris Mason | 890871b | 2009-09-02 16:24:52 -0400 | [diff] [blame] | 7029 | write_lock(&em_tree->lock); |
Josef Bacik | 09a2a8f9 | 2013-04-05 16:51:15 -0400 | [diff] [blame] | 7030 | ret = add_extent_mapping(em_tree, em, 0); |
Chris Mason | 3b95151 | 2008-04-17 11:29:12 -0400 | [diff] [blame] | 7031 | /* it is possible that someone inserted the extent into the tree |
| 7032 | * while we had the lock dropped. It is also possible that |
| 7033 | * an overlapping map exists in the tree |
| 7034 | */ |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 7035 | if (ret == -EEXIST) { |
Chris Mason | 3b95151 | 2008-04-17 11:29:12 -0400 | [diff] [blame] | 7036 | struct extent_map *existing; |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 7037 | |
| 7038 | ret = 0; |
| 7039 | |
Qu Wenruo | e6c4efd | 2014-09-17 11:53:35 +0800 | [diff] [blame] | 7040 | existing = search_extent_mapping(em_tree, start, len); |
| 7041 | /* |
| 7042 | * existing will always be non-NULL, since there must be |
| 7043 | * extent causing the -EEXIST. |
| 7044 | */ |
Chris Mason | 8dff9c8 | 2015-09-19 11:28:25 -0700 | [diff] [blame] | 7045 | if (existing->start == em->start && |
Omar Sandoval | 8e2bd3b | 2016-11-09 15:26:50 -0800 | [diff] [blame] | 7046 | extent_map_end(existing) >= extent_map_end(em) && |
Chris Mason | 8dff9c8 | 2015-09-19 11:28:25 -0700 | [diff] [blame] | 7047 | em->block_start == existing->block_start) { |
| 7048 | /* |
Omar Sandoval | 8e2bd3b | 2016-11-09 15:26:50 -0800 | [diff] [blame] | 7049 | * The existing extent map already encompasses the |
| 7050 | * entire extent map we tried to add. |
Chris Mason | 8dff9c8 | 2015-09-19 11:28:25 -0700 | [diff] [blame] | 7051 | */ |
| 7052 | free_extent_map(em); |
| 7053 | em = existing; |
| 7054 | err = 0; |
| 7055 | |
| 7056 | } else if (start >= extent_map_end(existing) || |
Qu Wenruo | 32be3a1 | 2014-09-22 09:13:03 +0800 | [diff] [blame] | 7057 | start <= existing->start) { |
Qu Wenruo | e6c4efd | 2014-09-17 11:53:35 +0800 | [diff] [blame] | 7058 | /* |
| 7059 | * The existing extent map is the one nearest to |
| 7060 | * the [start, start + len) range which overlaps |
| 7061 | */ |
| 7062 | err = merge_extent_mapping(em_tree, existing, |
| 7063 | em, start); |
Chris Mason | e1c4b74 | 2008-04-22 13:26:46 -0400 | [diff] [blame] | 7064 | free_extent_map(existing); |
Qu Wenruo | e6c4efd | 2014-09-17 11:53:35 +0800 | [diff] [blame] | 7065 | if (err) { |
Chris Mason | 3b95151 | 2008-04-17 11:29:12 -0400 | [diff] [blame] | 7066 | free_extent_map(em); |
| 7067 | em = NULL; |
| 7068 | } |
| 7069 | } else { |
| 7070 | free_extent_map(em); |
| 7071 | em = existing; |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 7072 | err = 0; |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 7073 | } |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 7074 | } |
Chris Mason | 890871b | 2009-09-02 16:24:52 -0400 | [diff] [blame] | 7075 | write_unlock(&em_tree->lock); |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 7076 | out: |
liubo | 1abe9b8 | 2011-03-24 11:18:59 +0000 | [diff] [blame] | 7077 | |
Nikolay Borisov | 4a0cc7c | 2017-01-10 20:35:31 +0200 | [diff] [blame] | 7078 | trace_btrfs_get_extent(root, BTRFS_I(inode), em); |
liubo | 1abe9b8 | 2011-03-24 11:18:59 +0000 | [diff] [blame] | 7079 | |
Tsutomu Itoh | 527afb4 | 2015-08-19 14:55:00 +0900 | [diff] [blame] | 7080 | btrfs_free_path(path); |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 7081 | if (trans) { |
Jeff Mahoney | 3a45bb2 | 2016-09-09 21:39:03 -0400 | [diff] [blame] | 7082 | ret = btrfs_end_transaction(trans); |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 7083 | if (!err) |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 7084 | err = ret; |
| 7085 | } |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 7086 | if (err) { |
| 7087 | free_extent_map(em); |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 7088 | return ERR_PTR(err); |
| 7089 | } |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 7090 | BUG_ON(!em); /* Error is always set */ |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 7091 | return em; |
| 7092 | } |
| 7093 | |
Chris Mason | ec29ed5 | 2011-02-23 16:23:20 -0500 | [diff] [blame] | 7094 | struct extent_map *btrfs_get_extent_fiemap(struct inode *inode, struct page *page, |
| 7095 | size_t pg_offset, u64 start, u64 len, |
| 7096 | int create) |
| 7097 | { |
| 7098 | struct extent_map *em; |
| 7099 | struct extent_map *hole_em = NULL; |
| 7100 | u64 range_start = start; |
| 7101 | u64 end; |
| 7102 | u64 found; |
| 7103 | u64 found_end; |
| 7104 | int err = 0; |
| 7105 | |
| 7106 | em = btrfs_get_extent(inode, page, pg_offset, start, len, create); |
| 7107 | if (IS_ERR(em)) |
| 7108 | return em; |
| 7109 | if (em) { |
| 7110 | /* |
Liu Bo | f9e4fb5 | 2013-01-07 10:10:12 +0000 | [diff] [blame] | 7111 | * if our em maps to |
| 7112 | * - a hole or |
| 7113 | * - a pre-alloc extent, |
| 7114 | * there might actually be delalloc bytes behind it. |
Chris Mason | ec29ed5 | 2011-02-23 16:23:20 -0500 | [diff] [blame] | 7115 | */ |
Liu Bo | f9e4fb5 | 2013-01-07 10:10:12 +0000 | [diff] [blame] | 7116 | if (em->block_start != EXTENT_MAP_HOLE && |
| 7117 | !test_bit(EXTENT_FLAG_PREALLOC, &em->flags)) |
Chris Mason | ec29ed5 | 2011-02-23 16:23:20 -0500 | [diff] [blame] | 7118 | return em; |
| 7119 | else |
| 7120 | hole_em = em; |
| 7121 | } |
| 7122 | |
| 7123 | /* check to see if we've wrapped (len == -1 or similar) */ |
| 7124 | end = start + len; |
| 7125 | if (end < start) |
| 7126 | end = (u64)-1; |
| 7127 | else |
| 7128 | end -= 1; |
| 7129 | |
| 7130 | em = NULL; |
| 7131 | |
| 7132 | /* ok, we didn't find anything, lets look for delalloc */ |
| 7133 | found = count_range_bits(&BTRFS_I(inode)->io_tree, &range_start, |
| 7134 | end, len, EXTENT_DELALLOC, 1); |
| 7135 | found_end = range_start + found; |
| 7136 | if (found_end < range_start) |
| 7137 | found_end = (u64)-1; |
| 7138 | |
| 7139 | /* |
| 7140 | * we didn't find anything useful, return |
| 7141 | * the original results from get_extent() |
| 7142 | */ |
| 7143 | if (range_start > end || found_end <= start) { |
| 7144 | em = hole_em; |
| 7145 | hole_em = NULL; |
| 7146 | goto out; |
| 7147 | } |
| 7148 | |
| 7149 | /* adjust the range_start to make sure it doesn't |
| 7150 | * go backwards from the start they passed in |
| 7151 | */ |
Dulshani Gunawardhana | 6787125 | 2013-10-31 10:33:04 +0530 | [diff] [blame] | 7152 | range_start = max(start, range_start); |
Chris Mason | ec29ed5 | 2011-02-23 16:23:20 -0500 | [diff] [blame] | 7153 | found = found_end - range_start; |
| 7154 | |
| 7155 | if (found > 0) { |
| 7156 | u64 hole_start = start; |
| 7157 | u64 hole_len = len; |
| 7158 | |
David Sterba | 172ddd6 | 2011-04-21 00:48:27 +0200 | [diff] [blame] | 7159 | em = alloc_extent_map(); |
Chris Mason | ec29ed5 | 2011-02-23 16:23:20 -0500 | [diff] [blame] | 7160 | if (!em) { |
| 7161 | err = -ENOMEM; |
| 7162 | goto out; |
| 7163 | } |
| 7164 | /* |
| 7165 | * when btrfs_get_extent can't find anything it |
| 7166 | * returns one huge hole |
| 7167 | * |
| 7168 | * make sure what it found really fits our range, and |
| 7169 | * adjust to make sure it is based on the start from |
| 7170 | * the caller |
| 7171 | */ |
| 7172 | if (hole_em) { |
| 7173 | u64 calc_end = extent_map_end(hole_em); |
| 7174 | |
| 7175 | if (calc_end <= start || (hole_em->start > end)) { |
| 7176 | free_extent_map(hole_em); |
| 7177 | hole_em = NULL; |
| 7178 | } else { |
| 7179 | hole_start = max(hole_em->start, start); |
| 7180 | hole_len = calc_end - hole_start; |
| 7181 | } |
| 7182 | } |
| 7183 | em->bdev = NULL; |
| 7184 | if (hole_em && range_start > hole_start) { |
| 7185 | /* our hole starts before our delalloc, so we |
| 7186 | * have to return just the parts of the hole |
| 7187 | * that go until the delalloc starts |
| 7188 | */ |
| 7189 | em->len = min(hole_len, |
| 7190 | range_start - hole_start); |
| 7191 | em->start = hole_start; |
| 7192 | em->orig_start = hole_start; |
| 7193 | /* |
| 7194 | * don't adjust block start at all, |
| 7195 | * it is fixed at EXTENT_MAP_HOLE |
| 7196 | */ |
| 7197 | em->block_start = hole_em->block_start; |
| 7198 | em->block_len = hole_len; |
Liu Bo | f9e4fb5 | 2013-01-07 10:10:12 +0000 | [diff] [blame] | 7199 | if (test_bit(EXTENT_FLAG_PREALLOC, &hole_em->flags)) |
| 7200 | set_bit(EXTENT_FLAG_PREALLOC, &em->flags); |
Chris Mason | ec29ed5 | 2011-02-23 16:23:20 -0500 | [diff] [blame] | 7201 | } else { |
| 7202 | em->start = range_start; |
| 7203 | em->len = found; |
| 7204 | em->orig_start = range_start; |
| 7205 | em->block_start = EXTENT_MAP_DELALLOC; |
| 7206 | em->block_len = found; |
| 7207 | } |
| 7208 | } else if (hole_em) { |
| 7209 | return hole_em; |
| 7210 | } |
| 7211 | out: |
| 7212 | |
| 7213 | free_extent_map(hole_em); |
| 7214 | if (err) { |
| 7215 | free_extent_map(em); |
| 7216 | return ERR_PTR(err); |
| 7217 | } |
| 7218 | return em; |
| 7219 | } |
| 7220 | |
Filipe Manana | 5f9a8a5 | 2016-05-12 13:53:36 +0100 | [diff] [blame] | 7221 | static struct extent_map *btrfs_create_dio_extent(struct inode *inode, |
| 7222 | const u64 start, |
| 7223 | const u64 len, |
| 7224 | const u64 orig_start, |
| 7225 | const u64 block_start, |
| 7226 | const u64 block_len, |
| 7227 | const u64 orig_block_len, |
| 7228 | const u64 ram_bytes, |
| 7229 | const int type) |
| 7230 | { |
| 7231 | struct extent_map *em = NULL; |
| 7232 | int ret; |
| 7233 | |
Filipe Manana | 5f9a8a5 | 2016-05-12 13:53:36 +0100 | [diff] [blame] | 7234 | if (type != BTRFS_ORDERED_NOCOW) { |
| 7235 | em = create_pinned_em(inode, start, len, orig_start, |
| 7236 | block_start, block_len, orig_block_len, |
| 7237 | ram_bytes, type); |
| 7238 | if (IS_ERR(em)) |
| 7239 | goto out; |
| 7240 | } |
| 7241 | ret = btrfs_add_ordered_extent_dio(inode, start, block_start, |
| 7242 | len, block_len, type); |
| 7243 | if (ret) { |
| 7244 | if (em) { |
| 7245 | free_extent_map(em); |
| 7246 | btrfs_drop_extent_cache(inode, start, |
| 7247 | start + len - 1, 0); |
| 7248 | } |
| 7249 | em = ERR_PTR(ret); |
| 7250 | } |
| 7251 | out: |
Filipe Manana | 5f9a8a5 | 2016-05-12 13:53:36 +0100 | [diff] [blame] | 7252 | |
| 7253 | return em; |
| 7254 | } |
| 7255 | |
Josef Bacik | 4b46fce | 2010-05-23 11:00:55 -0400 | [diff] [blame] | 7256 | static struct extent_map *btrfs_new_extent_direct(struct inode *inode, |
| 7257 | u64 start, u64 len) |
| 7258 | { |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 7259 | struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb); |
Josef Bacik | 4b46fce | 2010-05-23 11:00:55 -0400 | [diff] [blame] | 7260 | struct btrfs_root *root = BTRFS_I(inode)->root; |
Josef Bacik | 70c8a91 | 2012-10-11 16:54:30 -0400 | [diff] [blame] | 7261 | struct extent_map *em; |
Josef Bacik | 4b46fce | 2010-05-23 11:00:55 -0400 | [diff] [blame] | 7262 | struct btrfs_key ins; |
| 7263 | u64 alloc_hint; |
| 7264 | int ret; |
Josef Bacik | 4b46fce | 2010-05-23 11:00:55 -0400 | [diff] [blame] | 7265 | |
Josef Bacik | 4b46fce | 2010-05-23 11:00:55 -0400 | [diff] [blame] | 7266 | alloc_hint = get_extent_allocation_hint(inode, start, len); |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 7267 | ret = btrfs_reserve_extent(root, len, len, fs_info->sectorsize, |
Jeff Mahoney | da17066 | 2016-06-15 09:22:56 -0400 | [diff] [blame] | 7268 | 0, alloc_hint, &ins, 1, 1); |
Josef Bacik | 0036158 | 2013-08-14 14:02:47 -0400 | [diff] [blame] | 7269 | if (ret) |
| 7270 | return ERR_PTR(ret); |
Josef Bacik | 4b46fce | 2010-05-23 11:00:55 -0400 | [diff] [blame] | 7271 | |
Filipe Manana | 5f9a8a5 | 2016-05-12 13:53:36 +0100 | [diff] [blame] | 7272 | em = btrfs_create_dio_extent(inode, start, ins.offset, start, |
| 7273 | ins.objectid, ins.offset, ins.offset, |
| 7274 | ins.offset, 0); |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 7275 | btrfs_dec_block_group_reservations(fs_info, ins.objectid); |
Filipe Manana | 5f9a8a5 | 2016-05-12 13:53:36 +0100 | [diff] [blame] | 7276 | if (IS_ERR(em)) |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 7277 | btrfs_free_reserved_extent(fs_info, ins.objectid, |
| 7278 | ins.offset, 1); |
Josef Bacik | 0036158 | 2013-08-14 14:02:47 -0400 | [diff] [blame] | 7279 | |
Josef Bacik | 4b46fce | 2010-05-23 11:00:55 -0400 | [diff] [blame] | 7280 | return em; |
| 7281 | } |
| 7282 | |
Chris Mason | 46bfbb5 | 2010-05-26 11:04:10 -0400 | [diff] [blame] | 7283 | /* |
| 7284 | * returns 1 when the nocow is safe, < 1 on error, 0 if the |
| 7285 | * block must be cow'd |
| 7286 | */ |
Josef Bacik | 0036158 | 2013-08-14 14:02:47 -0400 | [diff] [blame] | 7287 | noinline int can_nocow_extent(struct inode *inode, u64 offset, u64 *len, |
Josef Bacik | 7ee9e44 | 2013-06-21 16:37:03 -0400 | [diff] [blame] | 7288 | u64 *orig_start, u64 *orig_block_len, |
| 7289 | u64 *ram_bytes) |
Chris Mason | 46bfbb5 | 2010-05-26 11:04:10 -0400 | [diff] [blame] | 7290 | { |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 7291 | struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb); |
Josef Bacik | 0036158 | 2013-08-14 14:02:47 -0400 | [diff] [blame] | 7292 | struct btrfs_trans_handle *trans; |
Chris Mason | 46bfbb5 | 2010-05-26 11:04:10 -0400 | [diff] [blame] | 7293 | struct btrfs_path *path; |
| 7294 | int ret; |
| 7295 | struct extent_buffer *leaf; |
| 7296 | struct btrfs_root *root = BTRFS_I(inode)->root; |
Miao Xie | 7b2b708 | 2014-02-27 13:58:05 +0800 | [diff] [blame] | 7297 | struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree; |
Chris Mason | 46bfbb5 | 2010-05-26 11:04:10 -0400 | [diff] [blame] | 7298 | struct btrfs_file_extent_item *fi; |
| 7299 | struct btrfs_key key; |
| 7300 | u64 disk_bytenr; |
| 7301 | u64 backref_offset; |
| 7302 | u64 extent_end; |
| 7303 | u64 num_bytes; |
| 7304 | int slot; |
| 7305 | int found_type; |
Josef Bacik | 7ee9e44 | 2013-06-21 16:37:03 -0400 | [diff] [blame] | 7306 | bool nocow = (BTRFS_I(inode)->flags & BTRFS_INODE_NODATACOW); |
Miao Xie | e77751a | 2013-12-27 21:11:50 +0800 | [diff] [blame] | 7307 | |
Chris Mason | 46bfbb5 | 2010-05-26 11:04:10 -0400 | [diff] [blame] | 7308 | path = btrfs_alloc_path(); |
| 7309 | if (!path) |
| 7310 | return -ENOMEM; |
| 7311 | |
David Sterba | f85b737 | 2017-01-20 14:54:07 +0100 | [diff] [blame] | 7312 | ret = btrfs_lookup_file_extent(NULL, root, path, |
| 7313 | btrfs_ino(BTRFS_I(inode)), offset, 0); |
Chris Mason | 46bfbb5 | 2010-05-26 11:04:10 -0400 | [diff] [blame] | 7314 | if (ret < 0) |
| 7315 | goto out; |
| 7316 | |
| 7317 | slot = path->slots[0]; |
| 7318 | if (ret == 1) { |
| 7319 | if (slot == 0) { |
| 7320 | /* can't find the item, must cow */ |
| 7321 | ret = 0; |
| 7322 | goto out; |
| 7323 | } |
| 7324 | slot--; |
| 7325 | } |
| 7326 | ret = 0; |
| 7327 | leaf = path->nodes[0]; |
| 7328 | btrfs_item_key_to_cpu(leaf, &key, slot); |
Nikolay Borisov | 4a0cc7c | 2017-01-10 20:35:31 +0200 | [diff] [blame] | 7329 | if (key.objectid != btrfs_ino(BTRFS_I(inode)) || |
Chris Mason | 46bfbb5 | 2010-05-26 11:04:10 -0400 | [diff] [blame] | 7330 | key.type != BTRFS_EXTENT_DATA_KEY) { |
| 7331 | /* not our file or wrong item type, must cow */ |
| 7332 | goto out; |
| 7333 | } |
| 7334 | |
| 7335 | if (key.offset > offset) { |
| 7336 | /* Wrong offset, must cow */ |
| 7337 | goto out; |
| 7338 | } |
| 7339 | |
| 7340 | fi = btrfs_item_ptr(leaf, slot, struct btrfs_file_extent_item); |
| 7341 | found_type = btrfs_file_extent_type(leaf, fi); |
| 7342 | if (found_type != BTRFS_FILE_EXTENT_REG && |
| 7343 | found_type != BTRFS_FILE_EXTENT_PREALLOC) { |
| 7344 | /* not a regular extent, must cow */ |
| 7345 | goto out; |
| 7346 | } |
Josef Bacik | 7ee9e44 | 2013-06-21 16:37:03 -0400 | [diff] [blame] | 7347 | |
| 7348 | if (!nocow && found_type == BTRFS_FILE_EXTENT_REG) |
| 7349 | goto out; |
| 7350 | |
Miao Xie | e77751a | 2013-12-27 21:11:50 +0800 | [diff] [blame] | 7351 | extent_end = key.offset + btrfs_file_extent_num_bytes(leaf, fi); |
| 7352 | if (extent_end <= offset) |
| 7353 | goto out; |
| 7354 | |
Chris Mason | 46bfbb5 | 2010-05-26 11:04:10 -0400 | [diff] [blame] | 7355 | disk_bytenr = btrfs_file_extent_disk_bytenr(leaf, fi); |
Josef Bacik | 7ee9e44 | 2013-06-21 16:37:03 -0400 | [diff] [blame] | 7356 | if (disk_bytenr == 0) |
| 7357 | goto out; |
| 7358 | |
| 7359 | if (btrfs_file_extent_compression(leaf, fi) || |
| 7360 | btrfs_file_extent_encryption(leaf, fi) || |
| 7361 | btrfs_file_extent_other_encoding(leaf, fi)) |
| 7362 | goto out; |
| 7363 | |
Chris Mason | 46bfbb5 | 2010-05-26 11:04:10 -0400 | [diff] [blame] | 7364 | backref_offset = btrfs_file_extent_offset(leaf, fi); |
| 7365 | |
Josef Bacik | 7ee9e44 | 2013-06-21 16:37:03 -0400 | [diff] [blame] | 7366 | if (orig_start) { |
| 7367 | *orig_start = key.offset - backref_offset; |
| 7368 | *orig_block_len = btrfs_file_extent_disk_num_bytes(leaf, fi); |
| 7369 | *ram_bytes = btrfs_file_extent_ram_bytes(leaf, fi); |
| 7370 | } |
Josef Bacik | eb384b5 | 2013-04-24 16:32:55 -0400 | [diff] [blame] | 7371 | |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 7372 | if (btrfs_extent_readonly(fs_info, disk_bytenr)) |
Chris Mason | 46bfbb5 | 2010-05-26 11:04:10 -0400 | [diff] [blame] | 7373 | goto out; |
Miao Xie | 7b2b708 | 2014-02-27 13:58:05 +0800 | [diff] [blame] | 7374 | |
| 7375 | num_bytes = min(offset + *len, extent_end) - offset; |
| 7376 | if (!nocow && found_type == BTRFS_FILE_EXTENT_PREALLOC) { |
| 7377 | u64 range_end; |
| 7378 | |
Jeff Mahoney | da17066 | 2016-06-15 09:22:56 -0400 | [diff] [blame] | 7379 | range_end = round_up(offset + num_bytes, |
| 7380 | root->fs_info->sectorsize) - 1; |
Miao Xie | 7b2b708 | 2014-02-27 13:58:05 +0800 | [diff] [blame] | 7381 | ret = test_range_bit(io_tree, offset, range_end, |
| 7382 | EXTENT_DELALLOC, 0, NULL); |
| 7383 | if (ret) { |
| 7384 | ret = -EAGAIN; |
| 7385 | goto out; |
| 7386 | } |
| 7387 | } |
| 7388 | |
Josef Bacik | 1bda19e | 2013-10-18 12:10:36 -0400 | [diff] [blame] | 7389 | btrfs_release_path(path); |
Chris Mason | 46bfbb5 | 2010-05-26 11:04:10 -0400 | [diff] [blame] | 7390 | |
| 7391 | /* |
| 7392 | * look for other files referencing this extent, if we |
| 7393 | * find any we must cow |
| 7394 | */ |
Josef Bacik | 0036158 | 2013-08-14 14:02:47 -0400 | [diff] [blame] | 7395 | trans = btrfs_join_transaction(root); |
| 7396 | if (IS_ERR(trans)) { |
| 7397 | ret = 0; |
Chris Mason | 46bfbb5 | 2010-05-26 11:04:10 -0400 | [diff] [blame] | 7398 | goto out; |
Josef Bacik | 0036158 | 2013-08-14 14:02:47 -0400 | [diff] [blame] | 7399 | } |
| 7400 | |
Nikolay Borisov | 4a0cc7c | 2017-01-10 20:35:31 +0200 | [diff] [blame] | 7401 | ret = btrfs_cross_ref_exist(trans, root, btrfs_ino(BTRFS_I(inode)), |
Josef Bacik | 0036158 | 2013-08-14 14:02:47 -0400 | [diff] [blame] | 7402 | key.offset - backref_offset, disk_bytenr); |
Jeff Mahoney | 3a45bb2 | 2016-09-09 21:39:03 -0400 | [diff] [blame] | 7403 | btrfs_end_transaction(trans); |
Josef Bacik | 0036158 | 2013-08-14 14:02:47 -0400 | [diff] [blame] | 7404 | if (ret) { |
| 7405 | ret = 0; |
| 7406 | goto out; |
| 7407 | } |
Chris Mason | 46bfbb5 | 2010-05-26 11:04:10 -0400 | [diff] [blame] | 7408 | |
| 7409 | /* |
| 7410 | * adjust disk_bytenr and num_bytes to cover just the bytes |
| 7411 | * in this extent we are about to write. If there |
| 7412 | * are any csums in that range we have to cow in order |
| 7413 | * to keep the csums correct |
| 7414 | */ |
| 7415 | disk_bytenr += backref_offset; |
| 7416 | disk_bytenr += offset - key.offset; |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 7417 | if (csum_exist_in_range(fs_info, disk_bytenr, num_bytes)) |
| 7418 | goto out; |
Chris Mason | 46bfbb5 | 2010-05-26 11:04:10 -0400 | [diff] [blame] | 7419 | /* |
| 7420 | * all of the above have passed, it is safe to overwrite this extent |
| 7421 | * without cow |
| 7422 | */ |
Josef Bacik | eb384b5 | 2013-04-24 16:32:55 -0400 | [diff] [blame] | 7423 | *len = num_bytes; |
Chris Mason | 46bfbb5 | 2010-05-26 11:04:10 -0400 | [diff] [blame] | 7424 | ret = 1; |
| 7425 | out: |
| 7426 | btrfs_free_path(path); |
| 7427 | return ret; |
| 7428 | } |
| 7429 | |
Alex Gartrell | fc4adbff | 2014-05-20 13:07:56 -0700 | [diff] [blame] | 7430 | bool btrfs_page_exists_in_range(struct inode *inode, loff_t start, loff_t end) |
| 7431 | { |
| 7432 | struct radix_tree_root *root = &inode->i_mapping->page_tree; |
| 7433 | int found = false; |
| 7434 | void **pagep = NULL; |
| 7435 | struct page *page = NULL; |
| 7436 | int start_idx; |
| 7437 | int end_idx; |
| 7438 | |
Kirill A. Shutemov | 09cbfea | 2016-04-01 15:29:47 +0300 | [diff] [blame] | 7439 | start_idx = start >> PAGE_SHIFT; |
Alex Gartrell | fc4adbff | 2014-05-20 13:07:56 -0700 | [diff] [blame] | 7440 | |
| 7441 | /* |
| 7442 | * end is the last byte in the last page. end == start is legal |
| 7443 | */ |
Kirill A. Shutemov | 09cbfea | 2016-04-01 15:29:47 +0300 | [diff] [blame] | 7444 | end_idx = end >> PAGE_SHIFT; |
Alex Gartrell | fc4adbff | 2014-05-20 13:07:56 -0700 | [diff] [blame] | 7445 | |
| 7446 | rcu_read_lock(); |
| 7447 | |
| 7448 | /* Most of the code in this while loop is lifted from |
| 7449 | * find_get_page. It's been modified to begin searching from a |
| 7450 | * page and return just the first page found in that range. If the |
| 7451 | * found idx is less than or equal to the end idx then we know that |
| 7452 | * a page exists. If no pages are found or if those pages are |
| 7453 | * outside of the range then we're fine (yay!) */ |
| 7454 | while (page == NULL && |
| 7455 | radix_tree_gang_lookup_slot(root, &pagep, NULL, start_idx, 1)) { |
| 7456 | page = radix_tree_deref_slot(pagep); |
| 7457 | if (unlikely(!page)) |
| 7458 | break; |
| 7459 | |
| 7460 | if (radix_tree_exception(page)) { |
Filipe Manana | 809f901 | 2014-06-05 13:22:25 +0100 | [diff] [blame] | 7461 | if (radix_tree_deref_retry(page)) { |
| 7462 | page = NULL; |
Alex Gartrell | fc4adbff | 2014-05-20 13:07:56 -0700 | [diff] [blame] | 7463 | continue; |
Filipe Manana | 809f901 | 2014-06-05 13:22:25 +0100 | [diff] [blame] | 7464 | } |
Alex Gartrell | fc4adbff | 2014-05-20 13:07:56 -0700 | [diff] [blame] | 7465 | /* |
| 7466 | * Otherwise, shmem/tmpfs must be storing a swap entry |
| 7467 | * here as an exceptional entry: so return it without |
| 7468 | * attempting to raise page count. |
| 7469 | */ |
Filipe Manana | 6fdef6d | 2014-06-05 13:22:26 +0100 | [diff] [blame] | 7470 | page = NULL; |
Alex Gartrell | fc4adbff | 2014-05-20 13:07:56 -0700 | [diff] [blame] | 7471 | break; /* TODO: Is this relevant for this use case? */ |
| 7472 | } |
| 7473 | |
Filipe Manana | 9140515 | 2014-06-05 13:22:24 +0100 | [diff] [blame] | 7474 | if (!page_cache_get_speculative(page)) { |
| 7475 | page = NULL; |
Alex Gartrell | fc4adbff | 2014-05-20 13:07:56 -0700 | [diff] [blame] | 7476 | continue; |
Filipe Manana | 9140515 | 2014-06-05 13:22:24 +0100 | [diff] [blame] | 7477 | } |
Alex Gartrell | fc4adbff | 2014-05-20 13:07:56 -0700 | [diff] [blame] | 7478 | |
| 7479 | /* |
| 7480 | * Has the page moved? |
| 7481 | * This is part of the lockless pagecache protocol. See |
| 7482 | * include/linux/pagemap.h for details. |
| 7483 | */ |
| 7484 | if (unlikely(page != *pagep)) { |
Kirill A. Shutemov | 09cbfea | 2016-04-01 15:29:47 +0300 | [diff] [blame] | 7485 | put_page(page); |
Alex Gartrell | fc4adbff | 2014-05-20 13:07:56 -0700 | [diff] [blame] | 7486 | page = NULL; |
| 7487 | } |
| 7488 | } |
| 7489 | |
| 7490 | if (page) { |
| 7491 | if (page->index <= end_idx) |
| 7492 | found = true; |
Kirill A. Shutemov | 09cbfea | 2016-04-01 15:29:47 +0300 | [diff] [blame] | 7493 | put_page(page); |
Alex Gartrell | fc4adbff | 2014-05-20 13:07:56 -0700 | [diff] [blame] | 7494 | } |
| 7495 | |
| 7496 | rcu_read_unlock(); |
| 7497 | return found; |
| 7498 | } |
| 7499 | |
Josef Bacik | eb838e7 | 2012-07-31 16:28:48 -0400 | [diff] [blame] | 7500 | static int lock_extent_direct(struct inode *inode, u64 lockstart, u64 lockend, |
| 7501 | struct extent_state **cached_state, int writing) |
| 7502 | { |
| 7503 | struct btrfs_ordered_extent *ordered; |
| 7504 | int ret = 0; |
| 7505 | |
| 7506 | while (1) { |
| 7507 | lock_extent_bits(&BTRFS_I(inode)->io_tree, lockstart, lockend, |
David Sterba | ff13db4 | 2015-12-03 14:30:40 +0100 | [diff] [blame] | 7508 | cached_state); |
Josef Bacik | eb838e7 | 2012-07-31 16:28:48 -0400 | [diff] [blame] | 7509 | /* |
| 7510 | * 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] | 7511 | * 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] | 7512 | * extents in this range. |
| 7513 | */ |
| 7514 | ordered = btrfs_lookup_ordered_range(inode, lockstart, |
| 7515 | lockend - lockstart + 1); |
| 7516 | |
| 7517 | /* |
| 7518 | * We need to make sure there are no buffered pages in this |
| 7519 | * range either, we could have raced between the invalidate in |
| 7520 | * generic_file_direct_write and locking the extent. The |
| 7521 | * invalidate needs to happen so that reads after a write do not |
| 7522 | * get stale data. |
| 7523 | */ |
Alex Gartrell | fc4adbff | 2014-05-20 13:07:56 -0700 | [diff] [blame] | 7524 | if (!ordered && |
| 7525 | (!writing || |
| 7526 | !btrfs_page_exists_in_range(inode, lockstart, lockend))) |
Josef Bacik | eb838e7 | 2012-07-31 16:28:48 -0400 | [diff] [blame] | 7527 | break; |
| 7528 | |
| 7529 | unlock_extent_cached(&BTRFS_I(inode)->io_tree, lockstart, lockend, |
| 7530 | cached_state, GFP_NOFS); |
| 7531 | |
| 7532 | if (ordered) { |
Filipe Manana | ade7702 | 2016-02-18 14:28:55 +0000 | [diff] [blame] | 7533 | /* |
| 7534 | * If we are doing a DIO read and the ordered extent we |
| 7535 | * found is for a buffered write, we can not wait for it |
| 7536 | * to complete and retry, because if we do so we can |
| 7537 | * deadlock with concurrent buffered writes on page |
| 7538 | * locks. This happens only if our DIO read covers more |
| 7539 | * than one extent map, if at this point has already |
| 7540 | * created an ordered extent for a previous extent map |
| 7541 | * and locked its range in the inode's io tree, and a |
| 7542 | * concurrent write against that previous extent map's |
| 7543 | * range and this range started (we unlock the ranges |
| 7544 | * in the io tree only when the bios complete and |
| 7545 | * buffered writes always lock pages before attempting |
| 7546 | * to lock range in the io tree). |
| 7547 | */ |
| 7548 | if (writing || |
| 7549 | test_bit(BTRFS_ORDERED_DIRECT, &ordered->flags)) |
| 7550 | btrfs_start_ordered_extent(inode, ordered, 1); |
| 7551 | else |
| 7552 | ret = -ENOTBLK; |
Josef Bacik | eb838e7 | 2012-07-31 16:28:48 -0400 | [diff] [blame] | 7553 | btrfs_put_ordered_extent(ordered); |
| 7554 | } else { |
Josef Bacik | eb838e7 | 2012-07-31 16:28:48 -0400 | [diff] [blame] | 7555 | /* |
Filipe Manana | b850ae1 | 2015-12-08 16:23:16 +0000 | [diff] [blame] | 7556 | * We could trigger writeback for this range (and wait |
| 7557 | * for it to complete) and then invalidate the pages for |
| 7558 | * this range (through invalidate_inode_pages2_range()), |
| 7559 | * but that can lead us to a deadlock with a concurrent |
| 7560 | * call to readpages() (a buffered read or a defrag call |
| 7561 | * triggered a readahead) on a page lock due to an |
| 7562 | * ordered dio extent we created before but did not have |
| 7563 | * yet a corresponding bio submitted (whence it can not |
| 7564 | * complete), which makes readpages() wait for that |
| 7565 | * ordered extent to complete while holding a lock on |
| 7566 | * that page. |
Josef Bacik | eb838e7 | 2012-07-31 16:28:48 -0400 | [diff] [blame] | 7567 | */ |
Filipe Manana | b850ae1 | 2015-12-08 16:23:16 +0000 | [diff] [blame] | 7568 | ret = -ENOTBLK; |
Josef Bacik | eb838e7 | 2012-07-31 16:28:48 -0400 | [diff] [blame] | 7569 | } |
| 7570 | |
Filipe Manana | ade7702 | 2016-02-18 14:28:55 +0000 | [diff] [blame] | 7571 | if (ret) |
| 7572 | break; |
| 7573 | |
Josef Bacik | eb838e7 | 2012-07-31 16:28:48 -0400 | [diff] [blame] | 7574 | cond_resched(); |
| 7575 | } |
| 7576 | |
| 7577 | return ret; |
| 7578 | } |
| 7579 | |
Josef Bacik | 69ffb54 | 2012-09-11 15:40:07 -0400 | [diff] [blame] | 7580 | static struct extent_map *create_pinned_em(struct inode *inode, u64 start, |
| 7581 | u64 len, u64 orig_start, |
| 7582 | u64 block_start, u64 block_len, |
Josef Bacik | cc95bef | 2013-04-04 14:31:27 -0400 | [diff] [blame] | 7583 | u64 orig_block_len, u64 ram_bytes, |
| 7584 | int type) |
Josef Bacik | 69ffb54 | 2012-09-11 15:40:07 -0400 | [diff] [blame] | 7585 | { |
| 7586 | struct extent_map_tree *em_tree; |
| 7587 | struct extent_map *em; |
| 7588 | struct btrfs_root *root = BTRFS_I(inode)->root; |
| 7589 | int ret; |
| 7590 | |
| 7591 | em_tree = &BTRFS_I(inode)->extent_tree; |
| 7592 | em = alloc_extent_map(); |
| 7593 | if (!em) |
| 7594 | return ERR_PTR(-ENOMEM); |
| 7595 | |
| 7596 | em->start = start; |
| 7597 | em->orig_start = orig_start; |
Josef Bacik | 2ab28f3 | 2012-10-12 15:27:49 -0400 | [diff] [blame] | 7598 | em->mod_start = start; |
| 7599 | em->mod_len = len; |
Josef Bacik | 69ffb54 | 2012-09-11 15:40:07 -0400 | [diff] [blame] | 7600 | em->len = len; |
| 7601 | em->block_len = block_len; |
| 7602 | em->block_start = block_start; |
| 7603 | em->bdev = root->fs_info->fs_devices->latest_bdev; |
Josef Bacik | b493968 | 2012-12-03 10:31:19 -0500 | [diff] [blame] | 7604 | em->orig_block_len = orig_block_len; |
Josef Bacik | cc95bef | 2013-04-04 14:31:27 -0400 | [diff] [blame] | 7605 | em->ram_bytes = ram_bytes; |
Josef Bacik | 70c8a91 | 2012-10-11 16:54:30 -0400 | [diff] [blame] | 7606 | em->generation = -1; |
Josef Bacik | 69ffb54 | 2012-09-11 15:40:07 -0400 | [diff] [blame] | 7607 | set_bit(EXTENT_FLAG_PINNED, &em->flags); |
| 7608 | if (type == BTRFS_ORDERED_PREALLOC) |
Josef Bacik | b11e234 | 2012-12-03 10:58:15 -0500 | [diff] [blame] | 7609 | set_bit(EXTENT_FLAG_FILLING, &em->flags); |
Josef Bacik | 69ffb54 | 2012-09-11 15:40:07 -0400 | [diff] [blame] | 7610 | |
| 7611 | do { |
| 7612 | btrfs_drop_extent_cache(inode, em->start, |
| 7613 | em->start + em->len - 1, 0); |
| 7614 | write_lock(&em_tree->lock); |
Josef Bacik | 09a2a8f9 | 2013-04-05 16:51:15 -0400 | [diff] [blame] | 7615 | ret = add_extent_mapping(em_tree, em, 1); |
Josef Bacik | 69ffb54 | 2012-09-11 15:40:07 -0400 | [diff] [blame] | 7616 | write_unlock(&em_tree->lock); |
| 7617 | } while (ret == -EEXIST); |
| 7618 | |
| 7619 | if (ret) { |
| 7620 | free_extent_map(em); |
| 7621 | return ERR_PTR(ret); |
| 7622 | } |
| 7623 | |
| 7624 | return em; |
| 7625 | } |
| 7626 | |
Filipe Manana | 9c9464c | 2015-11-04 09:52:04 +0000 | [diff] [blame] | 7627 | static void adjust_dio_outstanding_extents(struct inode *inode, |
| 7628 | struct btrfs_dio_data *dio_data, |
| 7629 | const u64 len) |
| 7630 | { |
David Sterba | 823bb20 | 2017-01-04 11:09:51 +0100 | [diff] [blame] | 7631 | unsigned num_extents = count_max_extents(len); |
Filipe Manana | 9c9464c | 2015-11-04 09:52:04 +0000 | [diff] [blame] | 7632 | |
Filipe Manana | 9c9464c | 2015-11-04 09:52:04 +0000 | [diff] [blame] | 7633 | /* |
| 7634 | * If we have an outstanding_extents count still set then we're |
| 7635 | * within our reservation, otherwise we need to adjust our inode |
| 7636 | * counter appropriately. |
| 7637 | */ |
Liu Bo | c293166 | 2016-12-22 17:13:54 -0800 | [diff] [blame] | 7638 | if (dio_data->outstanding_extents >= num_extents) { |
Filipe Manana | 9c9464c | 2015-11-04 09:52:04 +0000 | [diff] [blame] | 7639 | dio_data->outstanding_extents -= num_extents; |
| 7640 | } else { |
Liu Bo | c293166 | 2016-12-22 17:13:54 -0800 | [diff] [blame] | 7641 | /* |
| 7642 | * If dio write length has been split due to no large enough |
| 7643 | * contiguous space, we need to compensate our inode counter |
| 7644 | * appropriately. |
| 7645 | */ |
| 7646 | u64 num_needed = num_extents - dio_data->outstanding_extents; |
| 7647 | |
Filipe Manana | 9c9464c | 2015-11-04 09:52:04 +0000 | [diff] [blame] | 7648 | spin_lock(&BTRFS_I(inode)->lock); |
Liu Bo | c293166 | 2016-12-22 17:13:54 -0800 | [diff] [blame] | 7649 | BTRFS_I(inode)->outstanding_extents += num_needed; |
Filipe Manana | 9c9464c | 2015-11-04 09:52:04 +0000 | [diff] [blame] | 7650 | spin_unlock(&BTRFS_I(inode)->lock); |
| 7651 | } |
| 7652 | } |
| 7653 | |
Josef Bacik | 4b46fce | 2010-05-23 11:00:55 -0400 | [diff] [blame] | 7654 | static int btrfs_get_blocks_direct(struct inode *inode, sector_t iblock, |
| 7655 | struct buffer_head *bh_result, int create) |
| 7656 | { |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 7657 | struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb); |
Josef Bacik | 4b46fce | 2010-05-23 11:00:55 -0400 | [diff] [blame] | 7658 | struct extent_map *em; |
Josef Bacik | eb838e7 | 2012-07-31 16:28:48 -0400 | [diff] [blame] | 7659 | struct extent_state *cached_state = NULL; |
chandan | 50745b0 | 2015-08-28 21:10:13 +0530 | [diff] [blame] | 7660 | struct btrfs_dio_data *dio_data = NULL; |
Josef Bacik | 4b46fce | 2010-05-23 11:00:55 -0400 | [diff] [blame] | 7661 | u64 start = iblock << inode->i_blkbits; |
Josef Bacik | eb838e7 | 2012-07-31 16:28:48 -0400 | [diff] [blame] | 7662 | u64 lockstart, lockend; |
Josef Bacik | 4b46fce | 2010-05-23 11:00:55 -0400 | [diff] [blame] | 7663 | u64 len = bh_result->b_size; |
Josef Bacik | eb838e7 | 2012-07-31 16:28:48 -0400 | [diff] [blame] | 7664 | int unlock_bits = EXTENT_LOCKED; |
Miao Xie | 0934856 | 2013-02-07 10:12:07 +0000 | [diff] [blame] | 7665 | int ret = 0; |
Josef Bacik | eb838e7 | 2012-07-31 16:28:48 -0400 | [diff] [blame] | 7666 | |
Miao Xie | 172a504 | 2013-02-21 02:48:22 -0700 | [diff] [blame] | 7667 | if (create) |
Josef Bacik | 3266789 | 2015-02-11 15:08:58 -0500 | [diff] [blame] | 7668 | unlock_bits |= EXTENT_DIRTY; |
Miao Xie | 172a504 | 2013-02-21 02:48:22 -0700 | [diff] [blame] | 7669 | else |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 7670 | len = min_t(u64, len, fs_info->sectorsize); |
Josef Bacik | eb838e7 | 2012-07-31 16:28:48 -0400 | [diff] [blame] | 7671 | |
Josef Bacik | c329861 | 2012-08-03 16:49:19 -0400 | [diff] [blame] | 7672 | lockstart = start; |
| 7673 | lockend = start + len - 1; |
| 7674 | |
Josef Bacik | e1cbbfa | 2015-03-17 10:52:28 -0400 | [diff] [blame] | 7675 | if (current->journal_info) { |
| 7676 | /* |
| 7677 | * 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] | 7678 | * 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] | 7679 | * confused. |
| 7680 | */ |
chandan | 50745b0 | 2015-08-28 21:10:13 +0530 | [diff] [blame] | 7681 | dio_data = current->journal_info; |
Josef Bacik | e1cbbfa | 2015-03-17 10:52:28 -0400 | [diff] [blame] | 7682 | current->journal_info = NULL; |
| 7683 | } |
| 7684 | |
Josef Bacik | eb838e7 | 2012-07-31 16:28:48 -0400 | [diff] [blame] | 7685 | /* |
| 7686 | * If this errors out it's because we couldn't invalidate pagecache for |
| 7687 | * this range and we need to fallback to buffered. |
| 7688 | */ |
Filipe Manana | 9c9464c | 2015-11-04 09:52:04 +0000 | [diff] [blame] | 7689 | if (lock_extent_direct(inode, lockstart, lockend, &cached_state, |
| 7690 | create)) { |
| 7691 | ret = -ENOTBLK; |
| 7692 | goto err; |
| 7693 | } |
Josef Bacik | eb838e7 | 2012-07-31 16:28:48 -0400 | [diff] [blame] | 7694 | |
Josef Bacik | 4b46fce | 2010-05-23 11:00:55 -0400 | [diff] [blame] | 7695 | em = btrfs_get_extent(inode, NULL, 0, start, len, 0); |
Josef Bacik | eb838e7 | 2012-07-31 16:28:48 -0400 | [diff] [blame] | 7696 | if (IS_ERR(em)) { |
| 7697 | ret = PTR_ERR(em); |
| 7698 | goto unlock_err; |
| 7699 | } |
Josef Bacik | 4b46fce | 2010-05-23 11:00:55 -0400 | [diff] [blame] | 7700 | |
| 7701 | /* |
| 7702 | * Ok for INLINE and COMPRESSED extents we need to fallback on buffered |
| 7703 | * io. INLINE is special, and we could probably kludge it in here, but |
| 7704 | * it's still buffered so for safety lets just fall back to the generic |
| 7705 | * buffered path. |
| 7706 | * |
| 7707 | * For COMPRESSED we _have_ to read the entire extent in so we can |
| 7708 | * decompress it, so there will be buffering required no matter what we |
| 7709 | * do, so go ahead and fallback to buffered. |
| 7710 | * |
Nicholas D Steeves | 0132761 | 2016-05-19 21:18:45 -0400 | [diff] [blame] | 7711 | * 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] | 7712 | * to buffered IO. Don't blame me, this is the price we pay for using |
| 7713 | * the generic code. |
| 7714 | */ |
| 7715 | if (test_bit(EXTENT_FLAG_COMPRESSED, &em->flags) || |
| 7716 | em->block_start == EXTENT_MAP_INLINE) { |
| 7717 | free_extent_map(em); |
Josef Bacik | eb838e7 | 2012-07-31 16:28:48 -0400 | [diff] [blame] | 7718 | ret = -ENOTBLK; |
| 7719 | goto unlock_err; |
Josef Bacik | 4b46fce | 2010-05-23 11:00:55 -0400 | [diff] [blame] | 7720 | } |
| 7721 | |
| 7722 | /* Just a good old fashioned hole, return */ |
| 7723 | if (!create && (em->block_start == EXTENT_MAP_HOLE || |
| 7724 | test_bit(EXTENT_FLAG_PREALLOC, &em->flags))) { |
| 7725 | free_extent_map(em); |
Josef Bacik | eb838e7 | 2012-07-31 16:28:48 -0400 | [diff] [blame] | 7726 | goto unlock_err; |
Josef Bacik | 4b46fce | 2010-05-23 11:00:55 -0400 | [diff] [blame] | 7727 | } |
| 7728 | |
| 7729 | /* |
| 7730 | * We don't allocate a new extent in the following cases |
| 7731 | * |
| 7732 | * 1) The inode is marked as NODATACOW. In this case we'll just use the |
| 7733 | * existing extent. |
| 7734 | * 2) The extent is marked as PREALLOC. We're good to go here and can |
| 7735 | * just use the extent. |
| 7736 | * |
| 7737 | */ |
Chris Mason | 46bfbb5 | 2010-05-26 11:04:10 -0400 | [diff] [blame] | 7738 | if (!create) { |
Josef Bacik | eb838e7 | 2012-07-31 16:28:48 -0400 | [diff] [blame] | 7739 | len = min(len, em->len - (start - em->start)); |
| 7740 | lockstart = start + len; |
| 7741 | goto unlock; |
Chris Mason | 46bfbb5 | 2010-05-26 11:04:10 -0400 | [diff] [blame] | 7742 | } |
Josef Bacik | 4b46fce | 2010-05-23 11:00:55 -0400 | [diff] [blame] | 7743 | |
| 7744 | if (test_bit(EXTENT_FLAG_PREALLOC, &em->flags) || |
| 7745 | ((BTRFS_I(inode)->flags & BTRFS_INODE_NODATACOW) && |
| 7746 | em->block_start != EXTENT_MAP_HOLE)) { |
Josef Bacik | 4b46fce | 2010-05-23 11:00:55 -0400 | [diff] [blame] | 7747 | int type; |
Josef Bacik | eb384b5 | 2013-04-24 16:32:55 -0400 | [diff] [blame] | 7748 | u64 block_start, orig_start, orig_block_len, ram_bytes; |
Josef Bacik | 4b46fce | 2010-05-23 11:00:55 -0400 | [diff] [blame] | 7749 | |
| 7750 | if (test_bit(EXTENT_FLAG_PREALLOC, &em->flags)) |
| 7751 | type = BTRFS_ORDERED_PREALLOC; |
| 7752 | else |
| 7753 | type = BTRFS_ORDERED_NOCOW; |
Chris Mason | 46bfbb5 | 2010-05-26 11:04:10 -0400 | [diff] [blame] | 7754 | len = min(len, em->len - (start - em->start)); |
Josef Bacik | 4b46fce | 2010-05-23 11:00:55 -0400 | [diff] [blame] | 7755 | block_start = em->block_start + (start - em->start); |
Chris Mason | 46bfbb5 | 2010-05-26 11:04:10 -0400 | [diff] [blame] | 7756 | |
Josef Bacik | 0036158 | 2013-08-14 14:02:47 -0400 | [diff] [blame] | 7757 | if (can_nocow_extent(inode, start, &len, &orig_start, |
Filipe Manana | f78c436 | 2016-05-09 13:15:41 +0100 | [diff] [blame] | 7758 | &orig_block_len, &ram_bytes) == 1 && |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 7759 | btrfs_inc_nocow_writers(fs_info, block_start)) { |
Filipe Manana | 5f9a8a5 | 2016-05-12 13:53:36 +0100 | [diff] [blame] | 7760 | struct extent_map *em2; |
Filipe Manana | 0b90191 | 2016-05-09 13:15:27 +0100 | [diff] [blame] | 7761 | |
Filipe Manana | 5f9a8a5 | 2016-05-12 13:53:36 +0100 | [diff] [blame] | 7762 | em2 = btrfs_create_dio_extent(inode, start, len, |
| 7763 | orig_start, block_start, |
| 7764 | len, orig_block_len, |
| 7765 | ram_bytes, type); |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 7766 | btrfs_dec_nocow_writers(fs_info, block_start); |
Josef Bacik | 69ffb54 | 2012-09-11 15:40:07 -0400 | [diff] [blame] | 7767 | if (type == BTRFS_ORDERED_PREALLOC) { |
| 7768 | free_extent_map(em); |
Filipe Manana | 5f9a8a5 | 2016-05-12 13:53:36 +0100 | [diff] [blame] | 7769 | em = em2; |
Josef Bacik | 69ffb54 | 2012-09-11 15:40:07 -0400 | [diff] [blame] | 7770 | } |
Filipe Manana | 5f9a8a5 | 2016-05-12 13:53:36 +0100 | [diff] [blame] | 7771 | if (em2 && IS_ERR(em2)) { |
| 7772 | ret = PTR_ERR(em2); |
Josef Bacik | eb838e7 | 2012-07-31 16:28:48 -0400 | [diff] [blame] | 7773 | goto unlock_err; |
Chris Mason | 46bfbb5 | 2010-05-26 11:04:10 -0400 | [diff] [blame] | 7774 | } |
Wang Xiaoguang | 1851309 | 2016-07-25 15:51:40 +0800 | [diff] [blame] | 7775 | /* |
| 7776 | * For inode marked NODATACOW or extent marked PREALLOC, |
| 7777 | * use the existing or preallocated extent, so does not |
| 7778 | * need to adjust btrfs_space_info's bytes_may_use. |
| 7779 | */ |
| 7780 | btrfs_free_reserved_data_space_noquota(inode, |
| 7781 | start, len); |
Chris Mason | 46bfbb5 | 2010-05-26 11:04:10 -0400 | [diff] [blame] | 7782 | goto unlock; |
Josef Bacik | 4b46fce | 2010-05-23 11:00:55 -0400 | [diff] [blame] | 7783 | } |
Josef Bacik | 4b46fce | 2010-05-23 11:00:55 -0400 | [diff] [blame] | 7784 | } |
Josef Bacik | 0036158 | 2013-08-14 14:02:47 -0400 | [diff] [blame] | 7785 | |
Chris Mason | 46bfbb5 | 2010-05-26 11:04:10 -0400 | [diff] [blame] | 7786 | /* |
| 7787 | * this will cow the extent, reset the len in case we changed |
| 7788 | * it above |
| 7789 | */ |
| 7790 | len = bh_result->b_size; |
Josef Bacik | 70c8a91 | 2012-10-11 16:54:30 -0400 | [diff] [blame] | 7791 | free_extent_map(em); |
| 7792 | em = btrfs_new_extent_direct(inode, start, len); |
Josef Bacik | eb838e7 | 2012-07-31 16:28:48 -0400 | [diff] [blame] | 7793 | if (IS_ERR(em)) { |
| 7794 | ret = PTR_ERR(em); |
| 7795 | goto unlock_err; |
| 7796 | } |
Chris Mason | 46bfbb5 | 2010-05-26 11:04:10 -0400 | [diff] [blame] | 7797 | len = min(len, em->len - (start - em->start)); |
| 7798 | unlock: |
Josef Bacik | 4b46fce | 2010-05-23 11:00:55 -0400 | [diff] [blame] | 7799 | bh_result->b_blocknr = (em->block_start + (start - em->start)) >> |
| 7800 | inode->i_blkbits; |
Chris Mason | 46bfbb5 | 2010-05-26 11:04:10 -0400 | [diff] [blame] | 7801 | bh_result->b_size = len; |
Josef Bacik | 4b46fce | 2010-05-23 11:00:55 -0400 | [diff] [blame] | 7802 | bh_result->b_bdev = em->bdev; |
| 7803 | set_buffer_mapped(bh_result); |
Josef Bacik | c3473e8 | 2012-06-19 10:59:00 -0400 | [diff] [blame] | 7804 | if (create) { |
| 7805 | if (!test_bit(EXTENT_FLAG_PREALLOC, &em->flags)) |
| 7806 | set_buffer_new(bh_result); |
| 7807 | |
| 7808 | /* |
| 7809 | * Need to update the i_size under the extent lock so buffered |
| 7810 | * readers will get the updated i_size when we unlock. |
| 7811 | */ |
| 7812 | if (start + len > i_size_read(inode)) |
| 7813 | i_size_write(inode, start + len); |
Miao Xie | 0934856 | 2013-02-07 10:12:07 +0000 | [diff] [blame] | 7814 | |
Filipe Manana | 9c9464c | 2015-11-04 09:52:04 +0000 | [diff] [blame] | 7815 | adjust_dio_outstanding_extents(inode, dio_data, len); |
chandan | 50745b0 | 2015-08-28 21:10:13 +0530 | [diff] [blame] | 7816 | WARN_ON(dio_data->reserve < len); |
| 7817 | dio_data->reserve -= len; |
Filipe Manana | f28a492 | 2015-12-08 19:23:20 +0000 | [diff] [blame] | 7818 | dio_data->unsubmitted_oe_range_end = start + len; |
chandan | 50745b0 | 2015-08-28 21:10:13 +0530 | [diff] [blame] | 7819 | current->journal_info = dio_data; |
Josef Bacik | c3473e8 | 2012-06-19 10:59:00 -0400 | [diff] [blame] | 7820 | } |
Josef Bacik | 4b46fce | 2010-05-23 11:00:55 -0400 | [diff] [blame] | 7821 | |
Josef Bacik | eb838e7 | 2012-07-31 16:28:48 -0400 | [diff] [blame] | 7822 | /* |
| 7823 | * In the case of write we need to clear and unlock the entire range, |
| 7824 | * in the case of read we need to unlock only the end area that we |
| 7825 | * aren't using if there is any left over space. |
| 7826 | */ |
Liu Bo | 24c03fa | 2012-08-22 20:10:38 -0600 | [diff] [blame] | 7827 | if (lockstart < lockend) { |
Miao Xie | 0934856 | 2013-02-07 10:12:07 +0000 | [diff] [blame] | 7828 | clear_extent_bit(&BTRFS_I(inode)->io_tree, lockstart, |
| 7829 | lockend, unlock_bits, 1, 0, |
| 7830 | &cached_state, GFP_NOFS); |
Liu Bo | 24c03fa | 2012-08-22 20:10:38 -0600 | [diff] [blame] | 7831 | } else { |
Josef Bacik | eb838e7 | 2012-07-31 16:28:48 -0400 | [diff] [blame] | 7832 | free_extent_state(cached_state); |
Liu Bo | 24c03fa | 2012-08-22 20:10:38 -0600 | [diff] [blame] | 7833 | } |
Josef Bacik | eb838e7 | 2012-07-31 16:28:48 -0400 | [diff] [blame] | 7834 | |
Josef Bacik | 4b46fce | 2010-05-23 11:00:55 -0400 | [diff] [blame] | 7835 | free_extent_map(em); |
| 7836 | |
| 7837 | return 0; |
Josef Bacik | eb838e7 | 2012-07-31 16:28:48 -0400 | [diff] [blame] | 7838 | |
| 7839 | unlock_err: |
Josef Bacik | eb838e7 | 2012-07-31 16:28:48 -0400 | [diff] [blame] | 7840 | clear_extent_bit(&BTRFS_I(inode)->io_tree, lockstart, lockend, |
| 7841 | unlock_bits, 1, 0, &cached_state, GFP_NOFS); |
Filipe Manana | 9c9464c | 2015-11-04 09:52:04 +0000 | [diff] [blame] | 7842 | err: |
chandan | 50745b0 | 2015-08-28 21:10:13 +0530 | [diff] [blame] | 7843 | if (dio_data) |
| 7844 | current->journal_info = dio_data; |
Filipe Manana | 9c9464c | 2015-11-04 09:52:04 +0000 | [diff] [blame] | 7845 | /* |
| 7846 | * Compensate the delalloc release we do in btrfs_direct_IO() when we |
| 7847 | * write less data then expected, so that we don't underflow our inode's |
| 7848 | * outstanding extents counter. |
| 7849 | */ |
| 7850 | if (create && dio_data) |
| 7851 | adjust_dio_outstanding_extents(inode, dio_data, len); |
| 7852 | |
Josef Bacik | eb838e7 | 2012-07-31 16:28:48 -0400 | [diff] [blame] | 7853 | return ret; |
Josef Bacik | 4b46fce | 2010-05-23 11:00:55 -0400 | [diff] [blame] | 7854 | } |
| 7855 | |
Miao Xie | 8b110e3 | 2014-09-12 18:44:03 +0800 | [diff] [blame] | 7856 | static inline int submit_dio_repair_bio(struct inode *inode, struct bio *bio, |
Mike Christie | 81a75f67 | 2016-06-05 14:31:54 -0500 | [diff] [blame] | 7857 | int mirror_num) |
Miao Xie | 8b110e3 | 2014-09-12 18:44:03 +0800 | [diff] [blame] | 7858 | { |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 7859 | struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb); |
Miao Xie | 8b110e3 | 2014-09-12 18:44:03 +0800 | [diff] [blame] | 7860 | int ret; |
| 7861 | |
Mike Christie | 37226b2 | 2016-06-05 14:31:52 -0500 | [diff] [blame] | 7862 | BUG_ON(bio_op(bio) == REQ_OP_WRITE); |
Miao Xie | 8b110e3 | 2014-09-12 18:44:03 +0800 | [diff] [blame] | 7863 | |
| 7864 | bio_get(bio); |
| 7865 | |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 7866 | 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] | 7867 | if (ret) |
| 7868 | goto err; |
| 7869 | |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 7870 | ret = btrfs_map_bio(fs_info, bio, mirror_num, 0); |
Miao Xie | 8b110e3 | 2014-09-12 18:44:03 +0800 | [diff] [blame] | 7871 | err: |
| 7872 | bio_put(bio); |
| 7873 | return ret; |
| 7874 | } |
| 7875 | |
| 7876 | static int btrfs_check_dio_repairable(struct inode *inode, |
| 7877 | struct bio *failed_bio, |
| 7878 | struct io_failure_record *failrec, |
| 7879 | int failed_mirror) |
| 7880 | { |
Jeff Mahoney | ab8d0fc | 2016-09-20 10:05:02 -0400 | [diff] [blame] | 7881 | struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb); |
Miao Xie | 8b110e3 | 2014-09-12 18:44:03 +0800 | [diff] [blame] | 7882 | int num_copies; |
| 7883 | |
Jeff Mahoney | ab8d0fc | 2016-09-20 10:05:02 -0400 | [diff] [blame] | 7884 | num_copies = btrfs_num_copies(fs_info, failrec->logical, failrec->len); |
Miao Xie | 8b110e3 | 2014-09-12 18:44:03 +0800 | [diff] [blame] | 7885 | if (num_copies == 1) { |
| 7886 | /* |
| 7887 | * we only have a single copy of the data, so don't bother with |
| 7888 | * all the retry and error correction code that follows. no |
| 7889 | * matter what the error is, it is very likely to persist. |
| 7890 | */ |
Jeff Mahoney | ab8d0fc | 2016-09-20 10:05:02 -0400 | [diff] [blame] | 7891 | btrfs_debug(fs_info, |
| 7892 | "Check DIO Repairable: cannot repair, num_copies=%d, next_mirror %d, failed_mirror %d", |
| 7893 | num_copies, failrec->this_mirror, failed_mirror); |
Miao Xie | 8b110e3 | 2014-09-12 18:44:03 +0800 | [diff] [blame] | 7894 | return 0; |
| 7895 | } |
| 7896 | |
| 7897 | failrec->failed_mirror = failed_mirror; |
| 7898 | failrec->this_mirror++; |
| 7899 | if (failrec->this_mirror == failed_mirror) |
| 7900 | failrec->this_mirror++; |
| 7901 | |
| 7902 | if (failrec->this_mirror > num_copies) { |
Jeff Mahoney | ab8d0fc | 2016-09-20 10:05:02 -0400 | [diff] [blame] | 7903 | btrfs_debug(fs_info, |
| 7904 | "Check DIO Repairable: (fail) num_copies=%d, next_mirror %d, failed_mirror %d", |
| 7905 | num_copies, failrec->this_mirror, failed_mirror); |
Miao Xie | 8b110e3 | 2014-09-12 18:44:03 +0800 | [diff] [blame] | 7906 | return 0; |
| 7907 | } |
| 7908 | |
| 7909 | return 1; |
| 7910 | } |
| 7911 | |
| 7912 | static int dio_read_error(struct inode *inode, struct bio *failed_bio, |
Chandan Rajendra | 2dabb32 | 2016-01-21 15:55:55 +0530 | [diff] [blame] | 7913 | struct page *page, unsigned int pgoff, |
| 7914 | u64 start, u64 end, int failed_mirror, |
| 7915 | bio_end_io_t *repair_endio, void *repair_arg) |
Miao Xie | 8b110e3 | 2014-09-12 18:44:03 +0800 | [diff] [blame] | 7916 | { |
| 7917 | struct io_failure_record *failrec; |
| 7918 | struct bio *bio; |
| 7919 | int isector; |
Christoph Hellwig | 70fd761 | 2016-11-01 07:40:10 -0600 | [diff] [blame] | 7920 | int read_mode = 0; |
Miao Xie | 8b110e3 | 2014-09-12 18:44:03 +0800 | [diff] [blame] | 7921 | int ret; |
| 7922 | |
Mike Christie | 37226b2 | 2016-06-05 14:31:52 -0500 | [diff] [blame] | 7923 | BUG_ON(bio_op(failed_bio) == REQ_OP_WRITE); |
Miao Xie | 8b110e3 | 2014-09-12 18:44:03 +0800 | [diff] [blame] | 7924 | |
| 7925 | ret = btrfs_get_io_failure_record(inode, start, end, &failrec); |
| 7926 | if (ret) |
| 7927 | return ret; |
| 7928 | |
| 7929 | ret = btrfs_check_dio_repairable(inode, failed_bio, failrec, |
| 7930 | failed_mirror); |
| 7931 | if (!ret) { |
| 7932 | free_io_failure(inode, failrec); |
| 7933 | return -EIO; |
| 7934 | } |
| 7935 | |
Chandan Rajendra | 2dabb32 | 2016-01-21 15:55:55 +0530 | [diff] [blame] | 7936 | if ((failed_bio->bi_vcnt > 1) |
| 7937 | || (failed_bio->bi_io_vec->bv_len |
Jeff Mahoney | da17066 | 2016-06-15 09:22:56 -0400 | [diff] [blame] | 7938 | > btrfs_inode_sectorsize(inode))) |
Christoph Hellwig | 70fd761 | 2016-11-01 07:40:10 -0600 | [diff] [blame] | 7939 | read_mode |= REQ_FAILFAST_DEV; |
Miao Xie | 8b110e3 | 2014-09-12 18:44:03 +0800 | [diff] [blame] | 7940 | |
| 7941 | isector = start - btrfs_io_bio(failed_bio)->logical; |
| 7942 | isector >>= inode->i_sb->s_blocksize_bits; |
| 7943 | bio = btrfs_create_repair_bio(inode, failed_bio, failrec, page, |
Chandan Rajendra | 2dabb32 | 2016-01-21 15:55:55 +0530 | [diff] [blame] | 7944 | pgoff, isector, repair_endio, repair_arg); |
Miao Xie | 8b110e3 | 2014-09-12 18:44:03 +0800 | [diff] [blame] | 7945 | if (!bio) { |
| 7946 | free_io_failure(inode, failrec); |
| 7947 | return -EIO; |
| 7948 | } |
Mike Christie | 37226b2 | 2016-06-05 14:31:52 -0500 | [diff] [blame] | 7949 | bio_set_op_attrs(bio, REQ_OP_READ, read_mode); |
Miao Xie | 8b110e3 | 2014-09-12 18:44:03 +0800 | [diff] [blame] | 7950 | |
| 7951 | btrfs_debug(BTRFS_I(inode)->root->fs_info, |
| 7952 | "Repair DIO Read Error: submitting new dio read[%#x] to this_mirror=%d, in_validation=%d\n", |
| 7953 | read_mode, failrec->this_mirror, failrec->in_validation); |
| 7954 | |
Mike Christie | 81a75f67 | 2016-06-05 14:31:54 -0500 | [diff] [blame] | 7955 | ret = submit_dio_repair_bio(inode, bio, failrec->this_mirror); |
Miao Xie | 8b110e3 | 2014-09-12 18:44:03 +0800 | [diff] [blame] | 7956 | if (ret) { |
| 7957 | free_io_failure(inode, failrec); |
| 7958 | bio_put(bio); |
| 7959 | } |
| 7960 | |
| 7961 | return ret; |
| 7962 | } |
| 7963 | |
| 7964 | struct btrfs_retry_complete { |
| 7965 | struct completion done; |
| 7966 | struct inode *inode; |
| 7967 | u64 start; |
| 7968 | int uptodate; |
| 7969 | }; |
| 7970 | |
Christoph Hellwig | 4246a0b | 2015-07-20 15:29:37 +0200 | [diff] [blame] | 7971 | static void btrfs_retry_endio_nocsum(struct bio *bio) |
Miao Xie | 8b110e3 | 2014-09-12 18:44:03 +0800 | [diff] [blame] | 7972 | { |
| 7973 | struct btrfs_retry_complete *done = bio->bi_private; |
Chandan Rajendra | 2dabb32 | 2016-01-21 15:55:55 +0530 | [diff] [blame] | 7974 | struct inode *inode; |
Miao Xie | 8b110e3 | 2014-09-12 18:44:03 +0800 | [diff] [blame] | 7975 | struct bio_vec *bvec; |
| 7976 | int i; |
| 7977 | |
Christoph Hellwig | 4246a0b | 2015-07-20 15:29:37 +0200 | [diff] [blame] | 7978 | if (bio->bi_error) |
Miao Xie | 8b110e3 | 2014-09-12 18:44:03 +0800 | [diff] [blame] | 7979 | goto end; |
| 7980 | |
Chandan Rajendra | 2dabb32 | 2016-01-21 15:55:55 +0530 | [diff] [blame] | 7981 | ASSERT(bio->bi_vcnt == 1); |
| 7982 | inode = bio->bi_io_vec->bv_page->mapping->host; |
Jeff Mahoney | da17066 | 2016-06-15 09:22:56 -0400 | [diff] [blame] | 7983 | ASSERT(bio->bi_io_vec->bv_len == btrfs_inode_sectorsize(inode)); |
Chandan Rajendra | 2dabb32 | 2016-01-21 15:55:55 +0530 | [diff] [blame] | 7984 | |
Miao Xie | 8b110e3 | 2014-09-12 18:44:03 +0800 | [diff] [blame] | 7985 | done->uptodate = 1; |
| 7986 | bio_for_each_segment_all(bvec, bio, i) |
| 7987 | clean_io_failure(done->inode, done->start, bvec->bv_page, 0); |
| 7988 | end: |
| 7989 | complete(&done->done); |
| 7990 | bio_put(bio); |
| 7991 | } |
| 7992 | |
| 7993 | static int __btrfs_correct_data_nocsum(struct inode *inode, |
| 7994 | struct btrfs_io_bio *io_bio) |
Josef Bacik | 4b46fce | 2010-05-23 11:00:55 -0400 | [diff] [blame] | 7995 | { |
Chandan Rajendra | 2dabb32 | 2016-01-21 15:55:55 +0530 | [diff] [blame] | 7996 | struct btrfs_fs_info *fs_info; |
Kent Overstreet | 2c30c71 | 2013-11-07 12:20:26 -0800 | [diff] [blame] | 7997 | struct bio_vec *bvec; |
Miao Xie | 8b110e3 | 2014-09-12 18:44:03 +0800 | [diff] [blame] | 7998 | struct btrfs_retry_complete done; |
Josef Bacik | 4b46fce | 2010-05-23 11:00:55 -0400 | [diff] [blame] | 7999 | u64 start; |
Chandan Rajendra | 2dabb32 | 2016-01-21 15:55:55 +0530 | [diff] [blame] | 8000 | unsigned int pgoff; |
| 8001 | u32 sectorsize; |
| 8002 | int nr_sectors; |
Kent Overstreet | 2c30c71 | 2013-11-07 12:20:26 -0800 | [diff] [blame] | 8003 | int i; |
Miao Xie | c1dc089 | 2014-09-12 18:43:56 +0800 | [diff] [blame] | 8004 | int ret; |
Miao Xie | dc380ae | 2014-09-12 18:43:55 +0800 | [diff] [blame] | 8005 | |
Chandan Rajendra | 2dabb32 | 2016-01-21 15:55:55 +0530 | [diff] [blame] | 8006 | fs_info = BTRFS_I(inode)->root->fs_info; |
Jeff Mahoney | da17066 | 2016-06-15 09:22:56 -0400 | [diff] [blame] | 8007 | sectorsize = fs_info->sectorsize; |
Chandan Rajendra | 2dabb32 | 2016-01-21 15:55:55 +0530 | [diff] [blame] | 8008 | |
Miao Xie | c1dc089 | 2014-09-12 18:43:56 +0800 | [diff] [blame] | 8009 | start = io_bio->logical; |
Miao Xie | 8b110e3 | 2014-09-12 18:44:03 +0800 | [diff] [blame] | 8010 | done.inode = inode; |
| 8011 | |
| 8012 | bio_for_each_segment_all(bvec, &io_bio->bio, i) { |
Chandan Rajendra | 2dabb32 | 2016-01-21 15:55:55 +0530 | [diff] [blame] | 8013 | nr_sectors = BTRFS_BYTES_TO_BLKS(fs_info, bvec->bv_len); |
| 8014 | pgoff = bvec->bv_offset; |
| 8015 | |
| 8016 | next_block_or_try_again: |
Miao Xie | 8b110e3 | 2014-09-12 18:44:03 +0800 | [diff] [blame] | 8017 | done.uptodate = 0; |
| 8018 | done.start = start; |
| 8019 | init_completion(&done.done); |
| 8020 | |
Chandan Rajendra | 2dabb32 | 2016-01-21 15:55:55 +0530 | [diff] [blame] | 8021 | ret = dio_read_error(inode, &io_bio->bio, bvec->bv_page, |
| 8022 | pgoff, start, start + sectorsize - 1, |
| 8023 | io_bio->mirror_num, |
| 8024 | btrfs_retry_endio_nocsum, &done); |
Miao Xie | 8b110e3 | 2014-09-12 18:44:03 +0800 | [diff] [blame] | 8025 | if (ret) |
| 8026 | return ret; |
| 8027 | |
| 8028 | wait_for_completion(&done.done); |
| 8029 | |
| 8030 | if (!done.uptodate) { |
| 8031 | /* We might have another mirror, so try again */ |
Chandan Rajendra | 2dabb32 | 2016-01-21 15:55:55 +0530 | [diff] [blame] | 8032 | goto next_block_or_try_again; |
Miao Xie | 8b110e3 | 2014-09-12 18:44:03 +0800 | [diff] [blame] | 8033 | } |
| 8034 | |
Chandan Rajendra | 2dabb32 | 2016-01-21 15:55:55 +0530 | [diff] [blame] | 8035 | start += sectorsize; |
| 8036 | |
| 8037 | if (nr_sectors--) { |
| 8038 | pgoff += sectorsize; |
| 8039 | goto next_block_or_try_again; |
| 8040 | } |
Miao Xie | 8b110e3 | 2014-09-12 18:44:03 +0800 | [diff] [blame] | 8041 | } |
| 8042 | |
| 8043 | return 0; |
| 8044 | } |
| 8045 | |
Christoph Hellwig | 4246a0b | 2015-07-20 15:29:37 +0200 | [diff] [blame] | 8046 | static void btrfs_retry_endio(struct bio *bio) |
Miao Xie | 8b110e3 | 2014-09-12 18:44:03 +0800 | [diff] [blame] | 8047 | { |
| 8048 | struct btrfs_retry_complete *done = bio->bi_private; |
| 8049 | struct btrfs_io_bio *io_bio = btrfs_io_bio(bio); |
Chandan Rajendra | 2dabb32 | 2016-01-21 15:55:55 +0530 | [diff] [blame] | 8050 | struct inode *inode; |
Miao Xie | 8b110e3 | 2014-09-12 18:44:03 +0800 | [diff] [blame] | 8051 | struct bio_vec *bvec; |
Chandan Rajendra | 2dabb32 | 2016-01-21 15:55:55 +0530 | [diff] [blame] | 8052 | u64 start; |
Miao Xie | 8b110e3 | 2014-09-12 18:44:03 +0800 | [diff] [blame] | 8053 | int uptodate; |
| 8054 | int ret; |
| 8055 | int i; |
| 8056 | |
Christoph Hellwig | 4246a0b | 2015-07-20 15:29:37 +0200 | [diff] [blame] | 8057 | if (bio->bi_error) |
Miao Xie | 8b110e3 | 2014-09-12 18:44:03 +0800 | [diff] [blame] | 8058 | goto end; |
| 8059 | |
| 8060 | uptodate = 1; |
Chandan Rajendra | 2dabb32 | 2016-01-21 15:55:55 +0530 | [diff] [blame] | 8061 | |
| 8062 | start = done->start; |
| 8063 | |
| 8064 | ASSERT(bio->bi_vcnt == 1); |
| 8065 | inode = bio->bi_io_vec->bv_page->mapping->host; |
Jeff Mahoney | da17066 | 2016-06-15 09:22:56 -0400 | [diff] [blame] | 8066 | ASSERT(bio->bi_io_vec->bv_len == btrfs_inode_sectorsize(inode)); |
Chandan Rajendra | 2dabb32 | 2016-01-21 15:55:55 +0530 | [diff] [blame] | 8067 | |
Miao Xie | 8b110e3 | 2014-09-12 18:44:03 +0800 | [diff] [blame] | 8068 | bio_for_each_segment_all(bvec, bio, i) { |
| 8069 | ret = __readpage_endio_check(done->inode, io_bio, i, |
Chandan Rajendra | 2dabb32 | 2016-01-21 15:55:55 +0530 | [diff] [blame] | 8070 | bvec->bv_page, bvec->bv_offset, |
| 8071 | done->start, bvec->bv_len); |
Miao Xie | 8b110e3 | 2014-09-12 18:44:03 +0800 | [diff] [blame] | 8072 | if (!ret) |
| 8073 | clean_io_failure(done->inode, done->start, |
Chandan Rajendra | 2dabb32 | 2016-01-21 15:55:55 +0530 | [diff] [blame] | 8074 | bvec->bv_page, bvec->bv_offset); |
Miao Xie | 8b110e3 | 2014-09-12 18:44:03 +0800 | [diff] [blame] | 8075 | else |
| 8076 | uptodate = 0; |
| 8077 | } |
| 8078 | |
| 8079 | done->uptodate = uptodate; |
| 8080 | end: |
| 8081 | complete(&done->done); |
| 8082 | bio_put(bio); |
| 8083 | } |
| 8084 | |
| 8085 | static int __btrfs_subio_endio_read(struct inode *inode, |
| 8086 | struct btrfs_io_bio *io_bio, int err) |
| 8087 | { |
Chandan Rajendra | 2dabb32 | 2016-01-21 15:55:55 +0530 | [diff] [blame] | 8088 | struct btrfs_fs_info *fs_info; |
Miao Xie | 8b110e3 | 2014-09-12 18:44:03 +0800 | [diff] [blame] | 8089 | struct bio_vec *bvec; |
| 8090 | struct btrfs_retry_complete done; |
| 8091 | u64 start; |
| 8092 | u64 offset = 0; |
Chandan Rajendra | 2dabb32 | 2016-01-21 15:55:55 +0530 | [diff] [blame] | 8093 | u32 sectorsize; |
| 8094 | int nr_sectors; |
| 8095 | unsigned int pgoff; |
| 8096 | int csum_pos; |
Miao Xie | 8b110e3 | 2014-09-12 18:44:03 +0800 | [diff] [blame] | 8097 | int i; |
| 8098 | int ret; |
| 8099 | |
Chandan Rajendra | 2dabb32 | 2016-01-21 15:55:55 +0530 | [diff] [blame] | 8100 | fs_info = BTRFS_I(inode)->root->fs_info; |
Jeff Mahoney | da17066 | 2016-06-15 09:22:56 -0400 | [diff] [blame] | 8101 | sectorsize = fs_info->sectorsize; |
Chandan Rajendra | 2dabb32 | 2016-01-21 15:55:55 +0530 | [diff] [blame] | 8102 | |
Miao Xie | 8b110e3 | 2014-09-12 18:44:03 +0800 | [diff] [blame] | 8103 | err = 0; |
| 8104 | start = io_bio->logical; |
| 8105 | done.inode = inode; |
| 8106 | |
Miao Xie | c1dc089 | 2014-09-12 18:43:56 +0800 | [diff] [blame] | 8107 | bio_for_each_segment_all(bvec, &io_bio->bio, i) { |
Chandan Rajendra | 2dabb32 | 2016-01-21 15:55:55 +0530 | [diff] [blame] | 8108 | nr_sectors = BTRFS_BYTES_TO_BLKS(fs_info, bvec->bv_len); |
| 8109 | |
| 8110 | pgoff = bvec->bv_offset; |
| 8111 | next_block: |
| 8112 | csum_pos = BTRFS_BYTES_TO_BLKS(fs_info, offset); |
| 8113 | ret = __readpage_endio_check(inode, io_bio, csum_pos, |
| 8114 | bvec->bv_page, pgoff, start, |
| 8115 | sectorsize); |
Miao Xie | 8b110e3 | 2014-09-12 18:44:03 +0800 | [diff] [blame] | 8116 | if (likely(!ret)) |
| 8117 | goto next; |
| 8118 | try_again: |
| 8119 | done.uptodate = 0; |
| 8120 | done.start = start; |
| 8121 | init_completion(&done.done); |
| 8122 | |
Chandan Rajendra | 2dabb32 | 2016-01-21 15:55:55 +0530 | [diff] [blame] | 8123 | ret = dio_read_error(inode, &io_bio->bio, bvec->bv_page, |
| 8124 | pgoff, start, start + sectorsize - 1, |
| 8125 | io_bio->mirror_num, |
| 8126 | btrfs_retry_endio, &done); |
Miao Xie | 8b110e3 | 2014-09-12 18:44:03 +0800 | [diff] [blame] | 8127 | if (ret) { |
| 8128 | err = ret; |
| 8129 | goto next; |
| 8130 | } |
| 8131 | |
| 8132 | wait_for_completion(&done.done); |
| 8133 | |
| 8134 | if (!done.uptodate) { |
| 8135 | /* We might have another mirror, so try again */ |
| 8136 | goto try_again; |
| 8137 | } |
| 8138 | next: |
Chandan Rajendra | 2dabb32 | 2016-01-21 15:55:55 +0530 | [diff] [blame] | 8139 | offset += sectorsize; |
| 8140 | start += sectorsize; |
| 8141 | |
| 8142 | ASSERT(nr_sectors); |
| 8143 | |
| 8144 | if (--nr_sectors) { |
| 8145 | pgoff += sectorsize; |
| 8146 | goto next_block; |
| 8147 | } |
Kent Overstreet | 2c30c71 | 2013-11-07 12:20:26 -0800 | [diff] [blame] | 8148 | } |
Miao Xie | c1dc089 | 2014-09-12 18:43:56 +0800 | [diff] [blame] | 8149 | |
| 8150 | return err; |
| 8151 | } |
| 8152 | |
Miao Xie | 8b110e3 | 2014-09-12 18:44:03 +0800 | [diff] [blame] | 8153 | static int btrfs_subio_endio_read(struct inode *inode, |
| 8154 | struct btrfs_io_bio *io_bio, int err) |
| 8155 | { |
| 8156 | bool skip_csum = BTRFS_I(inode)->flags & BTRFS_INODE_NODATASUM; |
| 8157 | |
| 8158 | if (skip_csum) { |
| 8159 | if (unlikely(err)) |
| 8160 | return __btrfs_correct_data_nocsum(inode, io_bio); |
| 8161 | else |
| 8162 | return 0; |
| 8163 | } else { |
| 8164 | return __btrfs_subio_endio_read(inode, io_bio, err); |
| 8165 | } |
| 8166 | } |
| 8167 | |
Christoph Hellwig | 4246a0b | 2015-07-20 15:29:37 +0200 | [diff] [blame] | 8168 | static void btrfs_endio_direct_read(struct bio *bio) |
Miao Xie | c1dc089 | 2014-09-12 18:43:56 +0800 | [diff] [blame] | 8169 | { |
| 8170 | struct btrfs_dio_private *dip = bio->bi_private; |
| 8171 | struct inode *inode = dip->inode; |
| 8172 | struct bio *dio_bio; |
| 8173 | struct btrfs_io_bio *io_bio = btrfs_io_bio(bio); |
Christoph Hellwig | 4246a0b | 2015-07-20 15:29:37 +0200 | [diff] [blame] | 8174 | int err = bio->bi_error; |
Miao Xie | c1dc089 | 2014-09-12 18:43:56 +0800 | [diff] [blame] | 8175 | |
Miao Xie | 8b110e3 | 2014-09-12 18:44:03 +0800 | [diff] [blame] | 8176 | if (dip->flags & BTRFS_DIO_ORIG_BIO_SUBMITTED) |
| 8177 | err = btrfs_subio_endio_read(inode, io_bio, err); |
Miao Xie | c1dc089 | 2014-09-12 18:43:56 +0800 | [diff] [blame] | 8178 | |
Josef Bacik | 4b46fce | 2010-05-23 11:00:55 -0400 | [diff] [blame] | 8179 | unlock_extent(&BTRFS_I(inode)->io_tree, dip->logical_offset, |
Jeff Mahoney | d008237 | 2012-03-01 14:57:19 +0100 | [diff] [blame] | 8180 | dip->logical_offset + dip->bytes - 1); |
Chris Mason | 9be3395 | 2013-05-17 18:30:14 -0400 | [diff] [blame] | 8181 | dio_bio = dip->dio_bio; |
Josef Bacik | 4b46fce | 2010-05-23 11:00:55 -0400 | [diff] [blame] | 8182 | |
Josef Bacik | 4b46fce | 2010-05-23 11:00:55 -0400 | [diff] [blame] | 8183 | kfree(dip); |
Josef Bacik | c0da7aa | 2011-03-22 11:05:07 -0400 | [diff] [blame] | 8184 | |
Filipe Manana | 1636d1d | 2016-02-15 16:20:26 +0000 | [diff] [blame] | 8185 | dio_bio->bi_error = bio->bi_error; |
Christoph Hellwig | 4246a0b | 2015-07-20 15:29:37 +0200 | [diff] [blame] | 8186 | dio_end_io(dio_bio, bio->bi_error); |
Miao Xie | 23ea8e5 | 2014-09-12 18:43:54 +0800 | [diff] [blame] | 8187 | |
| 8188 | if (io_bio->end_io) |
| 8189 | io_bio->end_io(io_bio, err); |
Chris Mason | 9be3395 | 2013-05-17 18:30:14 -0400 | [diff] [blame] | 8190 | bio_put(bio); |
Josef Bacik | 4b46fce | 2010-05-23 11:00:55 -0400 | [diff] [blame] | 8191 | } |
| 8192 | |
Filipe Manana | 1454377 | 2015-11-24 16:23:54 +0000 | [diff] [blame] | 8193 | static void btrfs_endio_direct_write_update_ordered(struct inode *inode, |
| 8194 | const u64 offset, |
| 8195 | const u64 bytes, |
| 8196 | const int uptodate) |
Josef Bacik | 4b46fce | 2010-05-23 11:00:55 -0400 | [diff] [blame] | 8197 | { |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 8198 | struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb); |
Josef Bacik | 4b46fce | 2010-05-23 11:00:55 -0400 | [diff] [blame] | 8199 | struct btrfs_ordered_extent *ordered = NULL; |
Filipe Manana | 1454377 | 2015-11-24 16:23:54 +0000 | [diff] [blame] | 8200 | u64 ordered_offset = offset; |
| 8201 | u64 ordered_bytes = bytes; |
Josef Bacik | 4b46fce | 2010-05-23 11:00:55 -0400 | [diff] [blame] | 8202 | int ret; |
| 8203 | |
Chris Mason | 163cf09 | 2010-11-28 19:56:33 -0500 | [diff] [blame] | 8204 | again: |
| 8205 | ret = btrfs_dec_test_first_ordered_pending(inode, &ordered, |
| 8206 | &ordered_offset, |
Christoph Hellwig | 4246a0b | 2015-07-20 15:29:37 +0200 | [diff] [blame] | 8207 | ordered_bytes, |
Filipe Manana | 1454377 | 2015-11-24 16:23:54 +0000 | [diff] [blame] | 8208 | uptodate); |
Josef Bacik | 4b46fce | 2010-05-23 11:00:55 -0400 | [diff] [blame] | 8209 | if (!ret) |
Chris Mason | 163cf09 | 2010-11-28 19:56:33 -0500 | [diff] [blame] | 8210 | goto out_test; |
Josef Bacik | 4b46fce | 2010-05-23 11:00:55 -0400 | [diff] [blame] | 8211 | |
Liu Bo | 9e0af23 | 2014-08-15 23:36:53 +0800 | [diff] [blame] | 8212 | btrfs_init_work(&ordered->work, btrfs_endio_write_helper, |
| 8213 | finish_ordered_fn, NULL, NULL); |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 8214 | btrfs_queue_work(fs_info->endio_write_workers, &ordered->work); |
Chris Mason | 163cf09 | 2010-11-28 19:56:33 -0500 | [diff] [blame] | 8215 | out_test: |
| 8216 | /* |
| 8217 | * our bio might span multiple ordered extents. If we haven't |
| 8218 | * completed the accounting for the whole dio, go back and try again |
| 8219 | */ |
Filipe Manana | 1454377 | 2015-11-24 16:23:54 +0000 | [diff] [blame] | 8220 | if (ordered_offset < offset + bytes) { |
| 8221 | ordered_bytes = offset + bytes - ordered_offset; |
Josef Bacik | 5fd0204 | 2012-05-02 14:00:54 -0400 | [diff] [blame] | 8222 | ordered = NULL; |
Chris Mason | 163cf09 | 2010-11-28 19:56:33 -0500 | [diff] [blame] | 8223 | goto again; |
| 8224 | } |
Filipe Manana | 1454377 | 2015-11-24 16:23:54 +0000 | [diff] [blame] | 8225 | } |
| 8226 | |
| 8227 | static void btrfs_endio_direct_write(struct bio *bio) |
| 8228 | { |
| 8229 | struct btrfs_dio_private *dip = bio->bi_private; |
| 8230 | struct bio *dio_bio = dip->dio_bio; |
| 8231 | |
| 8232 | btrfs_endio_direct_write_update_ordered(dip->inode, |
| 8233 | dip->logical_offset, |
| 8234 | dip->bytes, |
| 8235 | !bio->bi_error); |
Josef Bacik | 4b46fce | 2010-05-23 11:00:55 -0400 | [diff] [blame] | 8236 | |
Josef Bacik | 4b46fce | 2010-05-23 11:00:55 -0400 | [diff] [blame] | 8237 | kfree(dip); |
Josef Bacik | c0da7aa | 2011-03-22 11:05:07 -0400 | [diff] [blame] | 8238 | |
Filipe Manana | 1636d1d | 2016-02-15 16:20:26 +0000 | [diff] [blame] | 8239 | dio_bio->bi_error = bio->bi_error; |
Christoph Hellwig | 4246a0b | 2015-07-20 15:29:37 +0200 | [diff] [blame] | 8240 | dio_end_io(dio_bio, bio->bi_error); |
Chris Mason | 9be3395 | 2013-05-17 18:30:14 -0400 | [diff] [blame] | 8241 | bio_put(bio); |
Josef Bacik | 4b46fce | 2010-05-23 11:00:55 -0400 | [diff] [blame] | 8242 | } |
| 8243 | |
Mike Christie | 81a75f67 | 2016-06-05 14:31:54 -0500 | [diff] [blame] | 8244 | static int __btrfs_submit_bio_start_direct_io(struct inode *inode, |
Chris Mason | eaf25d9 | 2010-05-25 09:48:28 -0400 | [diff] [blame] | 8245 | struct bio *bio, int mirror_num, |
| 8246 | unsigned long bio_flags, u64 offset) |
| 8247 | { |
| 8248 | int ret; |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 8249 | ret = btrfs_csum_one_bio(inode, bio, offset, 1); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 8250 | BUG_ON(ret); /* -ENOMEM */ |
Chris Mason | eaf25d9 | 2010-05-25 09:48:28 -0400 | [diff] [blame] | 8251 | return 0; |
| 8252 | } |
| 8253 | |
Christoph Hellwig | 4246a0b | 2015-07-20 15:29:37 +0200 | [diff] [blame] | 8254 | static void btrfs_end_dio_bio(struct bio *bio) |
Miao Xie | e65e153 | 2010-11-22 03:04:43 +0000 | [diff] [blame] | 8255 | { |
| 8256 | struct btrfs_dio_private *dip = bio->bi_private; |
Christoph Hellwig | 4246a0b | 2015-07-20 15:29:37 +0200 | [diff] [blame] | 8257 | int err = bio->bi_error; |
Miao Xie | e65e153 | 2010-11-22 03:04:43 +0000 | [diff] [blame] | 8258 | |
Miao Xie | 8b110e3 | 2014-09-12 18:44:03 +0800 | [diff] [blame] | 8259 | if (err) |
| 8260 | btrfs_warn(BTRFS_I(dip->inode)->root->fs_info, |
Mike Christie | 6296b96 | 2016-06-05 14:32:21 -0500 | [diff] [blame] | 8261 | "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] | 8262 | btrfs_ino(BTRFS_I(dip->inode)), bio_op(bio), |
| 8263 | bio->bi_opf, |
Miao Xie | 8b110e3 | 2014-09-12 18:44:03 +0800 | [diff] [blame] | 8264 | (unsigned long long)bio->bi_iter.bi_sector, |
| 8265 | bio->bi_iter.bi_size, err); |
| 8266 | |
| 8267 | if (dip->subio_endio) |
| 8268 | err = dip->subio_endio(dip->inode, btrfs_io_bio(bio), err); |
Miao Xie | c1dc089 | 2014-09-12 18:43:56 +0800 | [diff] [blame] | 8269 | |
| 8270 | if (err) { |
Miao Xie | e65e153 | 2010-11-22 03:04:43 +0000 | [diff] [blame] | 8271 | dip->errors = 1; |
| 8272 | |
| 8273 | /* |
| 8274 | * before atomic variable goto zero, we must make sure |
| 8275 | * dip->errors is perceived to be set. |
| 8276 | */ |
Peter Zijlstra | 4e857c5 | 2014-03-17 18:06:10 +0100 | [diff] [blame] | 8277 | smp_mb__before_atomic(); |
Miao Xie | e65e153 | 2010-11-22 03:04:43 +0000 | [diff] [blame] | 8278 | } |
| 8279 | |
| 8280 | /* if there are more bios still pending for this dio, just exit */ |
| 8281 | if (!atomic_dec_and_test(&dip->pending_bios)) |
| 8282 | goto out; |
| 8283 | |
Chris Mason | 9be3395 | 2013-05-17 18:30:14 -0400 | [diff] [blame] | 8284 | if (dip->errors) { |
Miao Xie | e65e153 | 2010-11-22 03:04:43 +0000 | [diff] [blame] | 8285 | bio_io_error(dip->orig_bio); |
Chris Mason | 9be3395 | 2013-05-17 18:30:14 -0400 | [diff] [blame] | 8286 | } else { |
Christoph Hellwig | 4246a0b | 2015-07-20 15:29:37 +0200 | [diff] [blame] | 8287 | dip->dio_bio->bi_error = 0; |
| 8288 | bio_endio(dip->orig_bio); |
Miao Xie | e65e153 | 2010-11-22 03:04:43 +0000 | [diff] [blame] | 8289 | } |
| 8290 | out: |
| 8291 | bio_put(bio); |
| 8292 | } |
| 8293 | |
| 8294 | static struct bio *btrfs_dio_bio_alloc(struct block_device *bdev, |
| 8295 | u64 first_sector, gfp_t gfp_flags) |
| 8296 | { |
Chris Mason | da2f0f7 | 2015-07-02 13:57:22 -0700 | [diff] [blame] | 8297 | struct bio *bio; |
Linus Torvalds | 2236597 | 2015-09-05 15:14:43 -0700 | [diff] [blame] | 8298 | bio = btrfs_bio_alloc(bdev, first_sector, BIO_MAX_PAGES, gfp_flags); |
Chris Mason | da2f0f7 | 2015-07-02 13:57:22 -0700 | [diff] [blame] | 8299 | if (bio) |
| 8300 | bio_associate_current(bio); |
| 8301 | return bio; |
Miao Xie | e65e153 | 2010-11-22 03:04:43 +0000 | [diff] [blame] | 8302 | } |
| 8303 | |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 8304 | static inline int btrfs_lookup_and_bind_dio_csum(struct inode *inode, |
Miao Xie | c1dc089 | 2014-09-12 18:43:56 +0800 | [diff] [blame] | 8305 | struct btrfs_dio_private *dip, |
| 8306 | struct bio *bio, |
| 8307 | u64 file_offset) |
| 8308 | { |
| 8309 | struct btrfs_io_bio *io_bio = btrfs_io_bio(bio); |
| 8310 | struct btrfs_io_bio *orig_io_bio = btrfs_io_bio(dip->orig_bio); |
| 8311 | int ret; |
| 8312 | |
| 8313 | /* |
| 8314 | * We load all the csum data we need when we submit |
| 8315 | * the first bio to reduce the csum tree search and |
| 8316 | * contention. |
| 8317 | */ |
| 8318 | if (dip->logical_offset == file_offset) { |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 8319 | ret = btrfs_lookup_bio_sums_dio(inode, dip->orig_bio, |
Miao Xie | c1dc089 | 2014-09-12 18:43:56 +0800 | [diff] [blame] | 8320 | file_offset); |
| 8321 | if (ret) |
| 8322 | return ret; |
| 8323 | } |
| 8324 | |
| 8325 | if (bio == dip->orig_bio) |
| 8326 | return 0; |
| 8327 | |
| 8328 | file_offset -= dip->logical_offset; |
| 8329 | file_offset >>= inode->i_sb->s_blocksize_bits; |
| 8330 | io_bio->csum = (u8 *)(((u32 *)orig_io_bio->csum) + file_offset); |
| 8331 | |
| 8332 | return 0; |
| 8333 | } |
| 8334 | |
Miao Xie | e65e153 | 2010-11-22 03:04:43 +0000 | [diff] [blame] | 8335 | static inline int __btrfs_submit_dio_bio(struct bio *bio, struct inode *inode, |
Mike Christie | 81a75f67 | 2016-06-05 14:31:54 -0500 | [diff] [blame] | 8336 | u64 file_offset, int skip_sum, |
Josef Bacik | c329861 | 2012-08-03 16:49:19 -0400 | [diff] [blame] | 8337 | int async_submit) |
Miao Xie | e65e153 | 2010-11-22 03:04:43 +0000 | [diff] [blame] | 8338 | { |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 8339 | struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb); |
Miao Xie | facc8a22 | 2013-07-25 19:22:34 +0800 | [diff] [blame] | 8340 | struct btrfs_dio_private *dip = bio->bi_private; |
Mike Christie | 37226b2 | 2016-06-05 14:31:52 -0500 | [diff] [blame] | 8341 | bool write = bio_op(bio) == REQ_OP_WRITE; |
Miao Xie | e65e153 | 2010-11-22 03:04:43 +0000 | [diff] [blame] | 8342 | int ret; |
| 8343 | |
Josef Bacik | b812ce2 | 2012-11-16 13:56:32 -0500 | [diff] [blame] | 8344 | if (async_submit) |
| 8345 | async_submit = !atomic_read(&BTRFS_I(inode)->sync_writers); |
| 8346 | |
Miao Xie | e65e153 | 2010-11-22 03:04:43 +0000 | [diff] [blame] | 8347 | bio_get(bio); |
Josef Bacik | 5fd0204 | 2012-05-02 14:00:54 -0400 | [diff] [blame] | 8348 | |
| 8349 | if (!write) { |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 8350 | 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] | 8351 | if (ret) |
| 8352 | goto err; |
| 8353 | } |
Miao Xie | e65e153 | 2010-11-22 03:04:43 +0000 | [diff] [blame] | 8354 | |
Josef Bacik | 1ae3993 | 2011-04-06 14:41:34 -0400 | [diff] [blame] | 8355 | if (skip_sum) |
| 8356 | goto map; |
| 8357 | |
| 8358 | if (write && async_submit) { |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 8359 | ret = btrfs_wq_submit_bio(fs_info, inode, bio, 0, 0, |
| 8360 | file_offset, |
| 8361 | __btrfs_submit_bio_start_direct_io, |
| 8362 | __btrfs_submit_bio_done); |
Miao Xie | e65e153 | 2010-11-22 03:04:43 +0000 | [diff] [blame] | 8363 | goto err; |
Josef Bacik | 1ae3993 | 2011-04-06 14:41:34 -0400 | [diff] [blame] | 8364 | } else if (write) { |
| 8365 | /* |
| 8366 | * If we aren't doing async submit, calculate the csum of the |
| 8367 | * bio now. |
| 8368 | */ |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 8369 | ret = btrfs_csum_one_bio(inode, bio, file_offset, 1); |
Josef Bacik | 1ae3993 | 2011-04-06 14:41:34 -0400 | [diff] [blame] | 8370 | if (ret) |
| 8371 | goto err; |
Miao Xie | 23ea8e5 | 2014-09-12 18:43:54 +0800 | [diff] [blame] | 8372 | } else { |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 8373 | ret = btrfs_lookup_and_bind_dio_csum(inode, dip, bio, |
Miao Xie | c1dc089 | 2014-09-12 18:43:56 +0800 | [diff] [blame] | 8374 | file_offset); |
Tsutomu Itoh | c2db107 | 2011-03-01 06:48:31 +0000 | [diff] [blame] | 8375 | if (ret) |
| 8376 | goto err; |
| 8377 | } |
Josef Bacik | 1ae3993 | 2011-04-06 14:41:34 -0400 | [diff] [blame] | 8378 | map: |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 8379 | ret = btrfs_map_bio(fs_info, bio, 0, async_submit); |
Miao Xie | e65e153 | 2010-11-22 03:04:43 +0000 | [diff] [blame] | 8380 | err: |
| 8381 | bio_put(bio); |
| 8382 | return ret; |
| 8383 | } |
| 8384 | |
Mike Christie | 81a75f67 | 2016-06-05 14:31:54 -0500 | [diff] [blame] | 8385 | static int btrfs_submit_direct_hook(struct btrfs_dio_private *dip, |
Miao Xie | e65e153 | 2010-11-22 03:04:43 +0000 | [diff] [blame] | 8386 | int skip_sum) |
| 8387 | { |
| 8388 | struct inode *inode = dip->inode; |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 8389 | struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb); |
Miao Xie | e65e153 | 2010-11-22 03:04:43 +0000 | [diff] [blame] | 8390 | struct btrfs_root *root = BTRFS_I(inode)->root; |
Miao Xie | e65e153 | 2010-11-22 03:04:43 +0000 | [diff] [blame] | 8391 | struct bio *bio; |
| 8392 | struct bio *orig_bio = dip->orig_bio; |
Christoph Hellwig | 6a2de22 | 2016-11-25 09:07:48 +0100 | [diff] [blame] | 8393 | struct bio_vec *bvec; |
Kent Overstreet | 4f024f3 | 2013-10-11 15:44:27 -0700 | [diff] [blame] | 8394 | u64 start_sector = orig_bio->bi_iter.bi_sector; |
Miao Xie | e65e153 | 2010-11-22 03:04:43 +0000 | [diff] [blame] | 8395 | u64 file_offset = dip->logical_offset; |
| 8396 | u64 submit_len = 0; |
| 8397 | u64 map_length; |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 8398 | u32 blocksize = fs_info->sectorsize; |
Josef Bacik | 1ae3993 | 2011-04-06 14:41:34 -0400 | [diff] [blame] | 8399 | int async_submit = 0; |
Chandan Rajendra | 5f4dc8f | 2016-01-21 15:56:00 +0530 | [diff] [blame] | 8400 | int nr_sectors; |
| 8401 | int ret; |
Christoph Hellwig | 6a2de22 | 2016-11-25 09:07:48 +0100 | [diff] [blame] | 8402 | int i, j; |
Miao Xie | e65e153 | 2010-11-22 03:04:43 +0000 | [diff] [blame] | 8403 | |
Kent Overstreet | 4f024f3 | 2013-10-11 15:44:27 -0700 | [diff] [blame] | 8404 | map_length = orig_bio->bi_iter.bi_size; |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 8405 | ret = btrfs_map_block(fs_info, btrfs_op(orig_bio), start_sector << 9, |
| 8406 | &map_length, NULL, 0); |
Miao Xie | 7a5c3c9 | 2014-06-17 18:58:59 +0800 | [diff] [blame] | 8407 | if (ret) |
Miao Xie | e65e153 | 2010-11-22 03:04:43 +0000 | [diff] [blame] | 8408 | return -EIO; |
Miao Xie | facc8a22 | 2013-07-25 19:22:34 +0800 | [diff] [blame] | 8409 | |
Kent Overstreet | 4f024f3 | 2013-10-11 15:44:27 -0700 | [diff] [blame] | 8410 | if (map_length >= orig_bio->bi_iter.bi_size) { |
Josef Bacik | 02f57c7 | 2011-04-06 14:25:44 -0400 | [diff] [blame] | 8411 | bio = orig_bio; |
Miao Xie | c1dc089 | 2014-09-12 18:43:56 +0800 | [diff] [blame] | 8412 | dip->flags |= BTRFS_DIO_ORIG_BIO_SUBMITTED; |
Josef Bacik | 02f57c7 | 2011-04-06 14:25:44 -0400 | [diff] [blame] | 8413 | goto submit; |
| 8414 | } |
| 8415 | |
David Woodhouse | 53b381b | 2013-01-29 18:40:14 -0500 | [diff] [blame] | 8416 | /* async crcs make it difficult to collect full stripe writes. */ |
Zhao Lei | ffe2d20 | 2015-01-20 15:11:44 +0800 | [diff] [blame] | 8417 | if (btrfs_get_alloc_profile(root, 1) & BTRFS_BLOCK_GROUP_RAID56_MASK) |
David Woodhouse | 53b381b | 2013-01-29 18:40:14 -0500 | [diff] [blame] | 8418 | async_submit = 0; |
| 8419 | else |
| 8420 | async_submit = 1; |
| 8421 | |
Josef Bacik | 02f57c7 | 2011-04-06 14:25:44 -0400 | [diff] [blame] | 8422 | bio = btrfs_dio_bio_alloc(orig_bio->bi_bdev, start_sector, GFP_NOFS); |
| 8423 | if (!bio) |
| 8424 | return -ENOMEM; |
Miao Xie | 7a5c3c9 | 2014-06-17 18:58:59 +0800 | [diff] [blame] | 8425 | |
Christoph Hellwig | ef295ec | 2016-10-28 08:48:16 -0600 | [diff] [blame] | 8426 | bio->bi_opf = orig_bio->bi_opf; |
Josef Bacik | 02f57c7 | 2011-04-06 14:25:44 -0400 | [diff] [blame] | 8427 | bio->bi_private = dip; |
| 8428 | bio->bi_end_io = btrfs_end_dio_bio; |
Miao Xie | c1dc089 | 2014-09-12 18:43:56 +0800 | [diff] [blame] | 8429 | btrfs_io_bio(bio)->logical = file_offset; |
Josef Bacik | 02f57c7 | 2011-04-06 14:25:44 -0400 | [diff] [blame] | 8430 | atomic_inc(&dip->pending_bios); |
| 8431 | |
Christoph Hellwig | 6a2de22 | 2016-11-25 09:07:48 +0100 | [diff] [blame] | 8432 | bio_for_each_segment_all(bvec, orig_bio, j) { |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 8433 | nr_sectors = BTRFS_BYTES_TO_BLKS(fs_info, bvec->bv_len); |
Chandan Rajendra | 5f4dc8f | 2016-01-21 15:56:00 +0530 | [diff] [blame] | 8434 | i = 0; |
| 8435 | next_block: |
| 8436 | if (unlikely(map_length < submit_len + blocksize || |
| 8437 | bio_add_page(bio, bvec->bv_page, blocksize, |
| 8438 | bvec->bv_offset + (i * blocksize)) < blocksize)) { |
Miao Xie | e65e153 | 2010-11-22 03:04:43 +0000 | [diff] [blame] | 8439 | /* |
| 8440 | * inc the count before we submit the bio so |
| 8441 | * we know the end IO handler won't happen before |
| 8442 | * we inc the count. Otherwise, the dip might get freed |
| 8443 | * before we're done setting it up |
| 8444 | */ |
| 8445 | atomic_inc(&dip->pending_bios); |
Mike Christie | 81a75f67 | 2016-06-05 14:31:54 -0500 | [diff] [blame] | 8446 | ret = __btrfs_submit_dio_bio(bio, inode, |
Miao Xie | e65e153 | 2010-11-22 03:04:43 +0000 | [diff] [blame] | 8447 | file_offset, skip_sum, |
Josef Bacik | c329861 | 2012-08-03 16:49:19 -0400 | [diff] [blame] | 8448 | async_submit); |
Miao Xie | e65e153 | 2010-11-22 03:04:43 +0000 | [diff] [blame] | 8449 | if (ret) { |
| 8450 | bio_put(bio); |
| 8451 | atomic_dec(&dip->pending_bios); |
| 8452 | goto out_err; |
| 8453 | } |
| 8454 | |
Miao Xie | e65e153 | 2010-11-22 03:04:43 +0000 | [diff] [blame] | 8455 | start_sector += submit_len >> 9; |
| 8456 | file_offset += submit_len; |
| 8457 | |
| 8458 | submit_len = 0; |
Miao Xie | e65e153 | 2010-11-22 03:04:43 +0000 | [diff] [blame] | 8459 | |
| 8460 | bio = btrfs_dio_bio_alloc(orig_bio->bi_bdev, |
| 8461 | start_sector, GFP_NOFS); |
| 8462 | if (!bio) |
| 8463 | goto out_err; |
Christoph Hellwig | ef295ec | 2016-10-28 08:48:16 -0600 | [diff] [blame] | 8464 | bio->bi_opf = orig_bio->bi_opf; |
Miao Xie | e65e153 | 2010-11-22 03:04:43 +0000 | [diff] [blame] | 8465 | bio->bi_private = dip; |
| 8466 | bio->bi_end_io = btrfs_end_dio_bio; |
Miao Xie | c1dc089 | 2014-09-12 18:43:56 +0800 | [diff] [blame] | 8467 | btrfs_io_bio(bio)->logical = file_offset; |
Miao Xie | e65e153 | 2010-11-22 03:04:43 +0000 | [diff] [blame] | 8468 | |
Kent Overstreet | 4f024f3 | 2013-10-11 15:44:27 -0700 | [diff] [blame] | 8469 | map_length = orig_bio->bi_iter.bi_size; |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 8470 | ret = btrfs_map_block(fs_info, btrfs_op(orig_bio), |
Stefan Behrens | 3ec706c | 2012-11-05 15:46:42 +0100 | [diff] [blame] | 8471 | start_sector << 9, |
Miao Xie | e65e153 | 2010-11-22 03:04:43 +0000 | [diff] [blame] | 8472 | &map_length, NULL, 0); |
| 8473 | if (ret) { |
| 8474 | bio_put(bio); |
| 8475 | goto out_err; |
| 8476 | } |
Chandan Rajendra | 5f4dc8f | 2016-01-21 15:56:00 +0530 | [diff] [blame] | 8477 | |
| 8478 | goto next_block; |
Miao Xie | e65e153 | 2010-11-22 03:04:43 +0000 | [diff] [blame] | 8479 | } else { |
Chandan Rajendra | 5f4dc8f | 2016-01-21 15:56:00 +0530 | [diff] [blame] | 8480 | submit_len += blocksize; |
| 8481 | if (--nr_sectors) { |
| 8482 | i++; |
| 8483 | goto next_block; |
| 8484 | } |
Miao Xie | e65e153 | 2010-11-22 03:04:43 +0000 | [diff] [blame] | 8485 | } |
| 8486 | } |
| 8487 | |
Josef Bacik | 02f57c7 | 2011-04-06 14:25:44 -0400 | [diff] [blame] | 8488 | submit: |
Mike Christie | 81a75f67 | 2016-06-05 14:31:54 -0500 | [diff] [blame] | 8489 | ret = __btrfs_submit_dio_bio(bio, inode, file_offset, skip_sum, |
Josef Bacik | c329861 | 2012-08-03 16:49:19 -0400 | [diff] [blame] | 8490 | async_submit); |
Miao Xie | e65e153 | 2010-11-22 03:04:43 +0000 | [diff] [blame] | 8491 | if (!ret) |
| 8492 | return 0; |
| 8493 | |
| 8494 | bio_put(bio); |
| 8495 | out_err: |
| 8496 | dip->errors = 1; |
| 8497 | /* |
| 8498 | * before atomic variable goto zero, we must |
| 8499 | * make sure dip->errors is perceived to be set. |
| 8500 | */ |
Peter Zijlstra | 4e857c5 | 2014-03-17 18:06:10 +0100 | [diff] [blame] | 8501 | smp_mb__before_atomic(); |
Miao Xie | e65e153 | 2010-11-22 03:04:43 +0000 | [diff] [blame] | 8502 | if (atomic_dec_and_test(&dip->pending_bios)) |
| 8503 | bio_io_error(dip->orig_bio); |
| 8504 | |
| 8505 | /* bio_end_io() will handle error, so we needn't return it */ |
| 8506 | return 0; |
| 8507 | } |
| 8508 | |
Mike Christie | 8a4c1e4 | 2016-06-05 14:31:50 -0500 | [diff] [blame] | 8509 | static void btrfs_submit_direct(struct bio *dio_bio, struct inode *inode, |
| 8510 | loff_t file_offset) |
Josef Bacik | 4b46fce | 2010-05-23 11:00:55 -0400 | [diff] [blame] | 8511 | { |
Filipe Manana | 61de718 | 2015-07-01 12:13:10 +0100 | [diff] [blame] | 8512 | struct btrfs_dio_private *dip = NULL; |
| 8513 | struct bio *io_bio = NULL; |
Miao Xie | 23ea8e5 | 2014-09-12 18:43:54 +0800 | [diff] [blame] | 8514 | struct btrfs_io_bio *btrfs_bio; |
Josef Bacik | 4b46fce | 2010-05-23 11:00:55 -0400 | [diff] [blame] | 8515 | int skip_sum; |
Mike Christie | 8a4c1e4 | 2016-06-05 14:31:50 -0500 | [diff] [blame] | 8516 | bool write = (bio_op(dio_bio) == REQ_OP_WRITE); |
Josef Bacik | 4b46fce | 2010-05-23 11:00:55 -0400 | [diff] [blame] | 8517 | int ret = 0; |
| 8518 | |
| 8519 | skip_sum = BTRFS_I(inode)->flags & BTRFS_INODE_NODATASUM; |
| 8520 | |
Chris Mason | 9be3395 | 2013-05-17 18:30:14 -0400 | [diff] [blame] | 8521 | io_bio = btrfs_bio_clone(dio_bio, GFP_NOFS); |
Chris Mason | 9be3395 | 2013-05-17 18:30:14 -0400 | [diff] [blame] | 8522 | if (!io_bio) { |
Josef Bacik | 4b46fce | 2010-05-23 11:00:55 -0400 | [diff] [blame] | 8523 | ret = -ENOMEM; |
| 8524 | goto free_ordered; |
| 8525 | } |
Josef Bacik | 4b46fce | 2010-05-23 11:00:55 -0400 | [diff] [blame] | 8526 | |
Miao Xie | c1dc089 | 2014-09-12 18:43:56 +0800 | [diff] [blame] | 8527 | dip = kzalloc(sizeof(*dip), GFP_NOFS); |
Chris Mason | 9be3395 | 2013-05-17 18:30:14 -0400 | [diff] [blame] | 8528 | if (!dip) { |
| 8529 | ret = -ENOMEM; |
Filipe Manana | 61de718 | 2015-07-01 12:13:10 +0100 | [diff] [blame] | 8530 | goto free_ordered; |
Chris Mason | 9be3395 | 2013-05-17 18:30:14 -0400 | [diff] [blame] | 8531 | } |
| 8532 | |
| 8533 | dip->private = dio_bio->bi_private; |
Josef Bacik | 4b46fce | 2010-05-23 11:00:55 -0400 | [diff] [blame] | 8534 | dip->inode = inode; |
| 8535 | dip->logical_offset = file_offset; |
Kent Overstreet | 4f024f3 | 2013-10-11 15:44:27 -0700 | [diff] [blame] | 8536 | dip->bytes = dio_bio->bi_iter.bi_size; |
| 8537 | dip->disk_bytenr = (u64)dio_bio->bi_iter.bi_sector << 9; |
Chris Mason | 9be3395 | 2013-05-17 18:30:14 -0400 | [diff] [blame] | 8538 | io_bio->bi_private = dip; |
Chris Mason | 9be3395 | 2013-05-17 18:30:14 -0400 | [diff] [blame] | 8539 | dip->orig_bio = io_bio; |
| 8540 | dip->dio_bio = dio_bio; |
Miao Xie | e65e153 | 2010-11-22 03:04:43 +0000 | [diff] [blame] | 8541 | atomic_set(&dip->pending_bios, 0); |
Miao Xie | c1dc089 | 2014-09-12 18:43:56 +0800 | [diff] [blame] | 8542 | btrfs_bio = btrfs_io_bio(io_bio); |
| 8543 | btrfs_bio->logical = file_offset; |
Josef Bacik | 4b46fce | 2010-05-23 11:00:55 -0400 | [diff] [blame] | 8544 | |
Miao Xie | c1dc089 | 2014-09-12 18:43:56 +0800 | [diff] [blame] | 8545 | if (write) { |
Chris Mason | 9be3395 | 2013-05-17 18:30:14 -0400 | [diff] [blame] | 8546 | io_bio->bi_end_io = btrfs_endio_direct_write; |
Miao Xie | c1dc089 | 2014-09-12 18:43:56 +0800 | [diff] [blame] | 8547 | } else { |
Chris Mason | 9be3395 | 2013-05-17 18:30:14 -0400 | [diff] [blame] | 8548 | io_bio->bi_end_io = btrfs_endio_direct_read; |
Miao Xie | c1dc089 | 2014-09-12 18:43:56 +0800 | [diff] [blame] | 8549 | dip->subio_endio = btrfs_subio_endio_read; |
| 8550 | } |
Josef Bacik | 4b46fce | 2010-05-23 11:00:55 -0400 | [diff] [blame] | 8551 | |
Filipe Manana | f28a492 | 2015-12-08 19:23:20 +0000 | [diff] [blame] | 8552 | /* |
| 8553 | * Reset the range for unsubmitted ordered extents (to a 0 length range) |
| 8554 | * even if we fail to submit a bio, because in such case we do the |
| 8555 | * corresponding error handling below and it must not be done a second |
| 8556 | * time by btrfs_direct_IO(). |
| 8557 | */ |
| 8558 | if (write) { |
| 8559 | struct btrfs_dio_data *dio_data = current->journal_info; |
| 8560 | |
| 8561 | dio_data->unsubmitted_oe_range_end = dip->logical_offset + |
| 8562 | dip->bytes; |
| 8563 | dio_data->unsubmitted_oe_range_start = |
| 8564 | dio_data->unsubmitted_oe_range_end; |
| 8565 | } |
| 8566 | |
Mike Christie | 81a75f67 | 2016-06-05 14:31:54 -0500 | [diff] [blame] | 8567 | ret = btrfs_submit_direct_hook(dip, skip_sum); |
Miao Xie | e65e153 | 2010-11-22 03:04:43 +0000 | [diff] [blame] | 8568 | if (!ret) |
Chris Mason | eaf25d9 | 2010-05-25 09:48:28 -0400 | [diff] [blame] | 8569 | return; |
Chris Mason | 9be3395 | 2013-05-17 18:30:14 -0400 | [diff] [blame] | 8570 | |
Miao Xie | 23ea8e5 | 2014-09-12 18:43:54 +0800 | [diff] [blame] | 8571 | if (btrfs_bio->end_io) |
| 8572 | btrfs_bio->end_io(btrfs_bio, ret); |
Chris Mason | 9be3395 | 2013-05-17 18:30:14 -0400 | [diff] [blame] | 8573 | |
Josef Bacik | 4b46fce | 2010-05-23 11:00:55 -0400 | [diff] [blame] | 8574 | free_ordered: |
| 8575 | /* |
Filipe Manana | 61de718 | 2015-07-01 12:13:10 +0100 | [diff] [blame] | 8576 | * If we arrived here it means either we failed to submit the dip |
| 8577 | * or we either failed to clone the dio_bio or failed to allocate the |
| 8578 | * dip. If we cloned the dio_bio and allocated the dip, we can just |
| 8579 | * call bio_endio against our io_bio so that we get proper resource |
| 8580 | * cleanup if we fail to submit the dip, otherwise, we must do the |
| 8581 | * same as btrfs_endio_direct_[write|read] because we can't call these |
| 8582 | * callbacks - they require an allocated dip and a clone of dio_bio. |
Josef Bacik | 4b46fce | 2010-05-23 11:00:55 -0400 | [diff] [blame] | 8583 | */ |
Filipe Manana | 61de718 | 2015-07-01 12:13:10 +0100 | [diff] [blame] | 8584 | if (io_bio && dip) { |
Christoph Hellwig | 4246a0b | 2015-07-20 15:29:37 +0200 | [diff] [blame] | 8585 | io_bio->bi_error = -EIO; |
| 8586 | bio_endio(io_bio); |
Filipe Manana | 61de718 | 2015-07-01 12:13:10 +0100 | [diff] [blame] | 8587 | /* |
| 8588 | * The end io callbacks free our dip, do the final put on io_bio |
| 8589 | * and all the cleanup and final put for dio_bio (through |
| 8590 | * dio_end_io()). |
| 8591 | */ |
| 8592 | dip = NULL; |
| 8593 | io_bio = NULL; |
| 8594 | } else { |
Filipe Manana | 1454377 | 2015-11-24 16:23:54 +0000 | [diff] [blame] | 8595 | if (write) |
| 8596 | btrfs_endio_direct_write_update_ordered(inode, |
| 8597 | file_offset, |
| 8598 | dio_bio->bi_iter.bi_size, |
| 8599 | 0); |
| 8600 | else |
Filipe Manana | 61de718 | 2015-07-01 12:13:10 +0100 | [diff] [blame] | 8601 | unlock_extent(&BTRFS_I(inode)->io_tree, file_offset, |
| 8602 | file_offset + dio_bio->bi_iter.bi_size - 1); |
Filipe Manana | 1454377 | 2015-11-24 16:23:54 +0000 | [diff] [blame] | 8603 | |
Christoph Hellwig | 4246a0b | 2015-07-20 15:29:37 +0200 | [diff] [blame] | 8604 | dio_bio->bi_error = -EIO; |
Filipe Manana | 61de718 | 2015-07-01 12:13:10 +0100 | [diff] [blame] | 8605 | /* |
| 8606 | * Releases and cleans up our dio_bio, no need to bio_put() |
| 8607 | * nor bio_endio()/bio_io_error() against dio_bio. |
| 8608 | */ |
| 8609 | dio_end_io(dio_bio, ret); |
Josef Bacik | 4b46fce | 2010-05-23 11:00:55 -0400 | [diff] [blame] | 8610 | } |
Filipe Manana | 61de718 | 2015-07-01 12:13:10 +0100 | [diff] [blame] | 8611 | if (io_bio) |
| 8612 | bio_put(io_bio); |
| 8613 | kfree(dip); |
Josef Bacik | 4b46fce | 2010-05-23 11:00:55 -0400 | [diff] [blame] | 8614 | } |
| 8615 | |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 8616 | static ssize_t check_direct_IO(struct btrfs_fs_info *fs_info, |
| 8617 | struct kiocb *iocb, |
| 8618 | const struct iov_iter *iter, loff_t offset) |
Chris Mason | 5a5f79b | 2010-05-26 21:33:37 -0400 | [diff] [blame] | 8619 | { |
| 8620 | int seg; |
Josef Bacik | a1b75f7 | 2011-04-08 15:51:18 +0000 | [diff] [blame] | 8621 | int i; |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 8622 | unsigned int blocksize_mask = fs_info->sectorsize - 1; |
Chris Mason | 5a5f79b | 2010-05-26 21:33:37 -0400 | [diff] [blame] | 8623 | ssize_t retval = -EINVAL; |
Chris Mason | 5a5f79b | 2010-05-26 21:33:37 -0400 | [diff] [blame] | 8624 | |
| 8625 | if (offset & blocksize_mask) |
| 8626 | goto out; |
| 8627 | |
Al Viro | 28060d5 | 2014-03-22 05:15:17 -0400 | [diff] [blame] | 8628 | if (iov_iter_alignment(iter) & blocksize_mask) |
| 8629 | goto out; |
Josef Bacik | a1b75f7 | 2011-04-08 15:51:18 +0000 | [diff] [blame] | 8630 | |
Al Viro | 28060d5 | 2014-03-22 05:15:17 -0400 | [diff] [blame] | 8631 | /* If this is a write we don't need to check anymore */ |
Al Viro | cd27e45 | 2016-10-10 13:39:05 -0400 | [diff] [blame] | 8632 | if (iov_iter_rw(iter) != READ || !iter_is_iovec(iter)) |
Al Viro | 28060d5 | 2014-03-22 05:15:17 -0400 | [diff] [blame] | 8633 | return 0; |
| 8634 | /* |
| 8635 | * Check to make sure we don't have duplicate iov_base's in this |
| 8636 | * iovec, if so return EINVAL, otherwise we'll get csum errors |
| 8637 | * when reading back. |
| 8638 | */ |
| 8639 | for (seg = 0; seg < iter->nr_segs; seg++) { |
| 8640 | for (i = seg + 1; i < iter->nr_segs; i++) { |
| 8641 | if (iter->iov[seg].iov_base == iter->iov[i].iov_base) |
Josef Bacik | a1b75f7 | 2011-04-08 15:51:18 +0000 | [diff] [blame] | 8642 | goto out; |
| 8643 | } |
Chris Mason | 5a5f79b | 2010-05-26 21:33:37 -0400 | [diff] [blame] | 8644 | } |
| 8645 | retval = 0; |
| 8646 | out: |
| 8647 | return retval; |
| 8648 | } |
Josef Bacik | eb838e7 | 2012-07-31 16:28:48 -0400 | [diff] [blame] | 8649 | |
Christoph Hellwig | c8b8e32 | 2016-04-07 08:51:58 -0700 | [diff] [blame] | 8650 | 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] | 8651 | { |
Josef Bacik | 4b46fce | 2010-05-23 11:00:55 -0400 | [diff] [blame] | 8652 | struct file *file = iocb->ki_filp; |
| 8653 | struct inode *inode = file->f_mapping->host; |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 8654 | struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb); |
chandan | 50745b0 | 2015-08-28 21:10:13 +0530 | [diff] [blame] | 8655 | struct btrfs_dio_data dio_data = { 0 }; |
Christoph Hellwig | c8b8e32 | 2016-04-07 08:51:58 -0700 | [diff] [blame] | 8656 | loff_t offset = iocb->ki_pos; |
Miao Xie | 0934856 | 2013-02-07 10:12:07 +0000 | [diff] [blame] | 8657 | size_t count = 0; |
Miao Xie | 2e60a51 | 2013-02-08 07:01:08 +0000 | [diff] [blame] | 8658 | int flags = 0; |
Miao Xie | 38851cc | 2013-02-08 07:04:11 +0000 | [diff] [blame] | 8659 | bool wakeup = true; |
| 8660 | bool relock = false; |
Miao Xie | 0934856 | 2013-02-07 10:12:07 +0000 | [diff] [blame] | 8661 | ssize_t ret; |
Josef Bacik | 4b46fce | 2010-05-23 11:00:55 -0400 | [diff] [blame] | 8662 | |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 8663 | if (check_direct_IO(fs_info, iocb, iter, offset)) |
Chris Mason | 5a5f79b | 2010-05-26 21:33:37 -0400 | [diff] [blame] | 8664 | return 0; |
Chris Mason | 5a5f79b | 2010-05-26 21:33:37 -0400 | [diff] [blame] | 8665 | |
Jens Axboe | fe0f07d | 2015-04-15 17:05:48 -0600 | [diff] [blame] | 8666 | inode_dio_begin(inode); |
Peter Zijlstra | 4e857c5 | 2014-03-17 18:06:10 +0100 | [diff] [blame] | 8667 | smp_mb__after_atomic(); |
Miao Xie | 38851cc | 2013-02-08 07:04:11 +0000 | [diff] [blame] | 8668 | |
Josef Bacik | 0e267c4 | 2013-07-02 10:38:02 -0400 | [diff] [blame] | 8669 | /* |
Miao Xie | 41bd9ca | 2014-03-06 13:54:57 +0800 | [diff] [blame] | 8670 | * The generic stuff only does filemap_write_and_wait_range, which |
| 8671 | * isn't enough if we've written compressed pages to this area, so |
| 8672 | * we need to flush the dirty pages again to make absolutely sure |
| 8673 | * that any outstanding dirty pages are on disk. |
Josef Bacik | 0e267c4 | 2013-07-02 10:38:02 -0400 | [diff] [blame] | 8674 | */ |
Al Viro | a6cbcd4 | 2014-03-04 22:38:00 -0500 | [diff] [blame] | 8675 | count = iov_iter_count(iter); |
Miao Xie | 41bd9ca | 2014-03-06 13:54:57 +0800 | [diff] [blame] | 8676 | if (test_bit(BTRFS_INODE_HAS_ASYNC_EXTENT, |
| 8677 | &BTRFS_I(inode)->runtime_flags)) |
Wang Shilong | 9a025a0 | 2014-07-17 11:44:13 +0800 | [diff] [blame] | 8678 | filemap_fdatawrite_range(inode->i_mapping, offset, |
| 8679 | offset + count - 1); |
Josef Bacik | 0e267c4 | 2013-07-02 10:38:02 -0400 | [diff] [blame] | 8680 | |
Omar Sandoval | 6f67376 | 2015-03-16 04:33:52 -0700 | [diff] [blame] | 8681 | if (iov_iter_rw(iter) == WRITE) { |
Miao Xie | 38851cc | 2013-02-08 07:04:11 +0000 | [diff] [blame] | 8682 | /* |
| 8683 | * If the write DIO is beyond the EOF, we need update |
| 8684 | * the isize, but it is protected by i_mutex. So we can |
| 8685 | * not unlock the i_mutex at this case. |
| 8686 | */ |
| 8687 | if (offset + count <= inode->i_size) { |
Al Viro | 5955102 | 2016-01-22 15:40:57 -0500 | [diff] [blame] | 8688 | inode_unlock(inode); |
Miao Xie | 38851cc | 2013-02-08 07:04:11 +0000 | [diff] [blame] | 8689 | relock = true; |
| 8690 | } |
Qu Wenruo | 7cf5b97 | 2015-09-08 17:25:55 +0800 | [diff] [blame] | 8691 | ret = btrfs_delalloc_reserve_space(inode, offset, count); |
Miao Xie | 0934856 | 2013-02-07 10:12:07 +0000 | [diff] [blame] | 8692 | if (ret) |
Miao Xie | 38851cc | 2013-02-08 07:04:11 +0000 | [diff] [blame] | 8693 | goto out; |
David Sterba | 823bb20 | 2017-01-04 11:09:51 +0100 | [diff] [blame] | 8694 | dio_data.outstanding_extents = count_max_extents(count); |
Josef Bacik | e1cbbfa | 2015-03-17 10:52:28 -0400 | [diff] [blame] | 8695 | |
| 8696 | /* |
| 8697 | * We need to know how many extents we reserved so that we can |
| 8698 | * do the accounting properly if we go over the number we |
| 8699 | * originally calculated. Abuse current->journal_info for this. |
| 8700 | */ |
Jeff Mahoney | da17066 | 2016-06-15 09:22:56 -0400 | [diff] [blame] | 8701 | dio_data.reserve = round_up(count, |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 8702 | fs_info->sectorsize); |
Filipe Manana | f28a492 | 2015-12-08 19:23:20 +0000 | [diff] [blame] | 8703 | dio_data.unsubmitted_oe_range_start = (u64)offset; |
| 8704 | dio_data.unsubmitted_oe_range_end = (u64)offset; |
chandan | 50745b0 | 2015-08-28 21:10:13 +0530 | [diff] [blame] | 8705 | current->journal_info = &dio_data; |
Chandan Rajendra | 97dcdea | 2016-12-23 15:00:18 +0530 | [diff] [blame] | 8706 | down_read(&BTRFS_I(inode)->dio_sem); |
David Sterba | ee39b43 | 2014-09-30 01:33:33 +0200 | [diff] [blame] | 8707 | } else if (test_bit(BTRFS_INODE_READDIO_NEED_LOCK, |
| 8708 | &BTRFS_I(inode)->runtime_flags)) { |
Jens Axboe | fe0f07d | 2015-04-15 17:05:48 -0600 | [diff] [blame] | 8709 | inode_dio_end(inode); |
Miao Xie | 38851cc | 2013-02-08 07:04:11 +0000 | [diff] [blame] | 8710 | flags = DIO_LOCKING | DIO_SKIP_HOLES; |
| 8711 | wakeup = false; |
Miao Xie | 0934856 | 2013-02-07 10:12:07 +0000 | [diff] [blame] | 8712 | } |
| 8713 | |
Omar Sandoval | 17f8c84 | 2015-03-16 04:33:50 -0700 | [diff] [blame] | 8714 | ret = __blockdev_direct_IO(iocb, inode, |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 8715 | fs_info->fs_devices->latest_bdev, |
Christoph Hellwig | c8b8e32 | 2016-04-07 08:51:58 -0700 | [diff] [blame] | 8716 | iter, btrfs_get_blocks_direct, NULL, |
Omar Sandoval | 17f8c84 | 2015-03-16 04:33:50 -0700 | [diff] [blame] | 8717 | btrfs_submit_direct, flags); |
Omar Sandoval | 6f67376 | 2015-03-16 04:33:52 -0700 | [diff] [blame] | 8718 | if (iov_iter_rw(iter) == WRITE) { |
Chandan Rajendra | 97dcdea | 2016-12-23 15:00:18 +0530 | [diff] [blame] | 8719 | up_read(&BTRFS_I(inode)->dio_sem); |
Josef Bacik | e1cbbfa | 2015-03-17 10:52:28 -0400 | [diff] [blame] | 8720 | current->journal_info = NULL; |
Liu Bo | ddba1bf | 2015-06-17 16:59:58 +0800 | [diff] [blame] | 8721 | if (ret < 0 && ret != -EIOCBQUEUED) { |
chandan | 50745b0 | 2015-08-28 21:10:13 +0530 | [diff] [blame] | 8722 | if (dio_data.reserve) |
Qu Wenruo | 7cf5b97 | 2015-09-08 17:25:55 +0800 | [diff] [blame] | 8723 | btrfs_delalloc_release_space(inode, offset, |
| 8724 | dio_data.reserve); |
Filipe Manana | f28a492 | 2015-12-08 19:23:20 +0000 | [diff] [blame] | 8725 | /* |
| 8726 | * On error we might have left some ordered extents |
| 8727 | * without submitting corresponding bios for them, so |
| 8728 | * cleanup them up to avoid other tasks getting them |
| 8729 | * and waiting for them to complete forever. |
| 8730 | */ |
| 8731 | if (dio_data.unsubmitted_oe_range_start < |
| 8732 | dio_data.unsubmitted_oe_range_end) |
| 8733 | btrfs_endio_direct_write_update_ordered(inode, |
| 8734 | dio_data.unsubmitted_oe_range_start, |
| 8735 | dio_data.unsubmitted_oe_range_end - |
| 8736 | dio_data.unsubmitted_oe_range_start, |
| 8737 | 0); |
Liu Bo | ddba1bf | 2015-06-17 16:59:58 +0800 | [diff] [blame] | 8738 | } else if (ret >= 0 && (size_t)ret < count) |
Qu Wenruo | 7cf5b97 | 2015-09-08 17:25:55 +0800 | [diff] [blame] | 8739 | btrfs_delalloc_release_space(inode, offset, |
| 8740 | count - (size_t)ret); |
Miao Xie | 0934856 | 2013-02-07 10:12:07 +0000 | [diff] [blame] | 8741 | } |
Miao Xie | 38851cc | 2013-02-08 07:04:11 +0000 | [diff] [blame] | 8742 | out: |
Miao Xie | 2e60a51 | 2013-02-08 07:01:08 +0000 | [diff] [blame] | 8743 | if (wakeup) |
Jens Axboe | fe0f07d | 2015-04-15 17:05:48 -0600 | [diff] [blame] | 8744 | inode_dio_end(inode); |
Miao Xie | 38851cc | 2013-02-08 07:04:11 +0000 | [diff] [blame] | 8745 | if (relock) |
Al Viro | 5955102 | 2016-01-22 15:40:57 -0500 | [diff] [blame] | 8746 | inode_lock(inode); |
Miao Xie | 0934856 | 2013-02-07 10:12:07 +0000 | [diff] [blame] | 8747 | |
| 8748 | return ret; |
Chris Mason | 1643298 | 2008-04-10 10:23:21 -0400 | [diff] [blame] | 8749 | } |
| 8750 | |
Tsutomu Itoh | 05dadc0 | 2012-11-29 05:08:26 +0000 | [diff] [blame] | 8751 | #define BTRFS_FIEMAP_FLAGS (FIEMAP_FLAG_SYNC) |
| 8752 | |
Yehuda Sadeh | 1506fcc | 2009-01-21 14:39:14 -0500 | [diff] [blame] | 8753 | static int btrfs_fiemap(struct inode *inode, struct fiemap_extent_info *fieinfo, |
| 8754 | __u64 start, __u64 len) |
| 8755 | { |
Tsutomu Itoh | 05dadc0 | 2012-11-29 05:08:26 +0000 | [diff] [blame] | 8756 | int ret; |
| 8757 | |
| 8758 | ret = fiemap_check_flags(fieinfo, BTRFS_FIEMAP_FLAGS); |
| 8759 | if (ret) |
| 8760 | return ret; |
| 8761 | |
Chris Mason | ec29ed5 | 2011-02-23 16:23:20 -0500 | [diff] [blame] | 8762 | return extent_fiemap(inode, fieinfo, start, len, btrfs_get_extent_fiemap); |
Yehuda Sadeh | 1506fcc | 2009-01-21 14:39:14 -0500 | [diff] [blame] | 8763 | } |
| 8764 | |
Chris Mason | 9ebefb18 | 2007-06-15 13:50:00 -0400 | [diff] [blame] | 8765 | int btrfs_readpage(struct file *file, struct page *page) |
| 8766 | { |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 8767 | struct extent_io_tree *tree; |
| 8768 | tree = &BTRFS_I(page->mapping->host)->io_tree; |
Jan Schmidt | 8ddc7d9 | 2011-06-13 20:02:58 +0200 | [diff] [blame] | 8769 | return extent_read_full_page(tree, page, btrfs_get_extent, 0); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 8770 | } |
Chris Mason | 1832a6d | 2007-12-21 16:27:21 -0500 | [diff] [blame] | 8771 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 8772 | static int btrfs_writepage(struct page *page, struct writeback_control *wbc) |
| 8773 | { |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 8774 | struct extent_io_tree *tree; |
Josef Bacik | be7bd73 | 2015-10-22 15:05:09 -0400 | [diff] [blame] | 8775 | struct inode *inode = page->mapping->host; |
| 8776 | int ret; |
Chris Mason | b888db2 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 8777 | |
| 8778 | if (current->flags & PF_MEMALLOC) { |
| 8779 | redirty_page_for_writepage(wbc, page); |
| 8780 | unlock_page(page); |
| 8781 | return 0; |
| 8782 | } |
Josef Bacik | be7bd73 | 2015-10-22 15:05:09 -0400 | [diff] [blame] | 8783 | |
| 8784 | /* |
| 8785 | * If we are under memory pressure we will call this directly from the |
| 8786 | * VM, we need to make sure we have the inode referenced for the ordered |
| 8787 | * extent. If not just return like we didn't do anything. |
| 8788 | */ |
| 8789 | if (!igrab(inode)) { |
| 8790 | redirty_page_for_writepage(wbc, page); |
| 8791 | return AOP_WRITEPAGE_ACTIVATE; |
| 8792 | } |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 8793 | tree = &BTRFS_I(page->mapping->host)->io_tree; |
Josef Bacik | be7bd73 | 2015-10-22 15:05:09 -0400 | [diff] [blame] | 8794 | ret = extent_write_full_page(tree, page, btrfs_get_extent, wbc); |
| 8795 | btrfs_add_delayed_iput(inode); |
| 8796 | return ret; |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 8797 | } |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 8798 | |
Eric Sandeen | 48a3b63 | 2013-04-25 20:41:01 +0000 | [diff] [blame] | 8799 | static int btrfs_writepages(struct address_space *mapping, |
| 8800 | struct writeback_control *wbc) |
Chris Mason | b293f02e | 2007-11-01 19:45:34 -0400 | [diff] [blame] | 8801 | { |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 8802 | struct extent_io_tree *tree; |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 8803 | |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 8804 | tree = &BTRFS_I(mapping->host)->io_tree; |
Chris Mason | b293f02e | 2007-11-01 19:45:34 -0400 | [diff] [blame] | 8805 | return extent_writepages(tree, mapping, btrfs_get_extent, wbc); |
| 8806 | } |
| 8807 | |
Chris Mason | 3ab2fb5 | 2007-11-08 10:59:22 -0500 | [diff] [blame] | 8808 | static int |
| 8809 | btrfs_readpages(struct file *file, struct address_space *mapping, |
| 8810 | struct list_head *pages, unsigned nr_pages) |
| 8811 | { |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 8812 | struct extent_io_tree *tree; |
| 8813 | tree = &BTRFS_I(mapping->host)->io_tree; |
Chris Mason | 3ab2fb5 | 2007-11-08 10:59:22 -0500 | [diff] [blame] | 8814 | return extent_readpages(tree, mapping, pages, nr_pages, |
| 8815 | btrfs_get_extent); |
| 8816 | } |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 8817 | static int __btrfs_releasepage(struct page *page, gfp_t gfp_flags) |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 8818 | { |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 8819 | struct extent_io_tree *tree; |
| 8820 | struct extent_map_tree *map; |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 8821 | int ret; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 8822 | |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 8823 | tree = &BTRFS_I(page->mapping->host)->io_tree; |
| 8824 | map = &BTRFS_I(page->mapping->host)->extent_tree; |
Chris Mason | 70dec80 | 2008-01-29 09:59:12 -0500 | [diff] [blame] | 8825 | ret = try_release_extent_mapping(map, tree, page, gfp_flags); |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 8826 | if (ret == 1) { |
| 8827 | ClearPagePrivate(page); |
| 8828 | set_page_private(page, 0); |
Kirill A. Shutemov | 09cbfea | 2016-04-01 15:29:47 +0300 | [diff] [blame] | 8829 | put_page(page); |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 8830 | } |
| 8831 | return ret; |
| 8832 | } |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 8833 | |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 8834 | static int btrfs_releasepage(struct page *page, gfp_t gfp_flags) |
| 8835 | { |
Chris Mason | 98509cf | 2008-09-11 15:51:43 -0400 | [diff] [blame] | 8836 | if (PageWriteback(page) || PageDirty(page)) |
| 8837 | return 0; |
Michal Hocko | 3ba7ab2 | 2017-01-09 15:39:02 +0100 | [diff] [blame] | 8838 | return __btrfs_releasepage(page, gfp_flags); |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 8839 | } |
| 8840 | |
Lukas Czerner | d47992f | 2013-05-21 23:17:23 -0400 | [diff] [blame] | 8841 | static void btrfs_invalidatepage(struct page *page, unsigned int offset, |
| 8842 | unsigned int length) |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 8843 | { |
Josef Bacik | 5fd0204 | 2012-05-02 14:00:54 -0400 | [diff] [blame] | 8844 | struct inode *inode = page->mapping->host; |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 8845 | struct extent_io_tree *tree; |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 8846 | struct btrfs_ordered_extent *ordered; |
Josef Bacik | 2ac55d4 | 2010-02-03 19:33:23 +0000 | [diff] [blame] | 8847 | struct extent_state *cached_state = NULL; |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 8848 | u64 page_start = page_offset(page); |
Kirill A. Shutemov | 09cbfea | 2016-04-01 15:29:47 +0300 | [diff] [blame] | 8849 | u64 page_end = page_start + PAGE_SIZE - 1; |
Chandan Rajendra | dbfdb6d | 2016-01-21 15:55:58 +0530 | [diff] [blame] | 8850 | u64 start; |
| 8851 | u64 end; |
Filipe David Borba Manana | 131e404 | 2013-11-19 22:29:35 +0000 | [diff] [blame] | 8852 | int inode_evicting = inode->i_state & I_FREEING; |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 8853 | |
Chris Mason | 8b62b72 | 2009-09-02 16:53:46 -0400 | [diff] [blame] | 8854 | /* |
| 8855 | * we have the page locked, so new writeback can't start, |
| 8856 | * and the dirty bit won't be cleared while we are here. |
| 8857 | * |
| 8858 | * Wait for IO on this page so that we can safely clear |
| 8859 | * the PagePrivate2 bit and do ordered accounting |
| 8860 | */ |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 8861 | wait_on_page_writeback(page); |
Chris Mason | 8b62b72 | 2009-09-02 16:53:46 -0400 | [diff] [blame] | 8862 | |
Josef Bacik | 5fd0204 | 2012-05-02 14:00:54 -0400 | [diff] [blame] | 8863 | tree = &BTRFS_I(inode)->io_tree; |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 8864 | if (offset) { |
| 8865 | btrfs_releasepage(page, GFP_NOFS); |
| 8866 | return; |
| 8867 | } |
Filipe David Borba Manana | 131e404 | 2013-11-19 22:29:35 +0000 | [diff] [blame] | 8868 | |
| 8869 | if (!inode_evicting) |
David Sterba | ff13db4 | 2015-12-03 14:30:40 +0100 | [diff] [blame] | 8870 | lock_extent_bits(tree, page_start, page_end, &cached_state); |
Chandan Rajendra | dbfdb6d | 2016-01-21 15:55:58 +0530 | [diff] [blame] | 8871 | again: |
| 8872 | start = page_start; |
| 8873 | ordered = btrfs_lookup_ordered_range(inode, start, |
| 8874 | page_end - start + 1); |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 8875 | if (ordered) { |
Chandan Rajendra | dbfdb6d | 2016-01-21 15:55:58 +0530 | [diff] [blame] | 8876 | end = min(page_end, ordered->file_offset + ordered->len - 1); |
Chris Mason | eb84ae0 | 2008-07-17 13:53:27 -0400 | [diff] [blame] | 8877 | /* |
| 8878 | * IO on this page will never be started, so we need |
| 8879 | * to account for any ordered extents now |
| 8880 | */ |
Filipe David Borba Manana | 131e404 | 2013-11-19 22:29:35 +0000 | [diff] [blame] | 8881 | if (!inode_evicting) |
Chandan Rajendra | dbfdb6d | 2016-01-21 15:55:58 +0530 | [diff] [blame] | 8882 | clear_extent_bit(tree, start, end, |
Filipe David Borba Manana | 131e404 | 2013-11-19 22:29:35 +0000 | [diff] [blame] | 8883 | EXTENT_DIRTY | EXTENT_DELALLOC | |
| 8884 | EXTENT_LOCKED | EXTENT_DO_ACCOUNTING | |
| 8885 | EXTENT_DEFRAG, 1, 0, &cached_state, |
| 8886 | GFP_NOFS); |
Chris Mason | 8b62b72 | 2009-09-02 16:53:46 -0400 | [diff] [blame] | 8887 | /* |
| 8888 | * whoever cleared the private bit is responsible |
| 8889 | * for the finish_ordered_io |
| 8890 | */ |
Josef Bacik | 77cef2e | 2013-08-29 13:57:21 -0400 | [diff] [blame] | 8891 | if (TestClearPagePrivate2(page)) { |
| 8892 | struct btrfs_ordered_inode_tree *tree; |
| 8893 | u64 new_len; |
| 8894 | |
| 8895 | tree = &BTRFS_I(inode)->ordered_tree; |
| 8896 | |
| 8897 | spin_lock_irq(&tree->lock); |
| 8898 | set_bit(BTRFS_ORDERED_TRUNCATED, &ordered->flags); |
Chandan Rajendra | dbfdb6d | 2016-01-21 15:55:58 +0530 | [diff] [blame] | 8899 | new_len = start - ordered->file_offset; |
Josef Bacik | 77cef2e | 2013-08-29 13:57:21 -0400 | [diff] [blame] | 8900 | if (new_len < ordered->truncated_len) |
| 8901 | ordered->truncated_len = new_len; |
| 8902 | spin_unlock_irq(&tree->lock); |
| 8903 | |
| 8904 | if (btrfs_dec_test_ordered_pending(inode, &ordered, |
Chandan Rajendra | dbfdb6d | 2016-01-21 15:55:58 +0530 | [diff] [blame] | 8905 | start, |
| 8906 | end - start + 1, 1)) |
Josef Bacik | 77cef2e | 2013-08-29 13:57:21 -0400 | [diff] [blame] | 8907 | btrfs_finish_ordered_io(ordered); |
Chris Mason | 8b62b72 | 2009-09-02 16:53:46 -0400 | [diff] [blame] | 8908 | } |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 8909 | btrfs_put_ordered_extent(ordered); |
Filipe David Borba Manana | 131e404 | 2013-11-19 22:29:35 +0000 | [diff] [blame] | 8910 | if (!inode_evicting) { |
| 8911 | cached_state = NULL; |
Chandan Rajendra | dbfdb6d | 2016-01-21 15:55:58 +0530 | [diff] [blame] | 8912 | lock_extent_bits(tree, start, end, |
Filipe David Borba Manana | 131e404 | 2013-11-19 22:29:35 +0000 | [diff] [blame] | 8913 | &cached_state); |
| 8914 | } |
Chandan Rajendra | dbfdb6d | 2016-01-21 15:55:58 +0530 | [diff] [blame] | 8915 | |
| 8916 | start = end + 1; |
| 8917 | if (start < page_end) |
| 8918 | goto again; |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 8919 | } |
Filipe David Borba Manana | 131e404 | 2013-11-19 22:29:35 +0000 | [diff] [blame] | 8920 | |
Qu Wenruo | b9d0b38 | 2015-09-29 10:35:16 +0800 | [diff] [blame] | 8921 | /* |
| 8922 | * Qgroup reserved space handler |
| 8923 | * Page here will be either |
| 8924 | * 1) Already written to disk |
| 8925 | * In this case, its reserved space is released from data rsv map |
| 8926 | * and will be freed by delayed_ref handler finally. |
| 8927 | * So even we call qgroup_free_data(), it won't decrease reserved |
| 8928 | * space. |
| 8929 | * 2) Not written to disk |
Goldwyn Rodrigues | 0b34c26 | 2016-09-30 10:40:52 -0500 | [diff] [blame] | 8930 | * This means the reserved space should be freed here. However, |
| 8931 | * if a truncate invalidates the page (by clearing PageDirty) |
| 8932 | * and the page is accounted for while allocating extent |
| 8933 | * in btrfs_check_data_free_space() we let delayed_ref to |
| 8934 | * free the entire extent. |
Qu Wenruo | b9d0b38 | 2015-09-29 10:35:16 +0800 | [diff] [blame] | 8935 | */ |
Goldwyn Rodrigues | 0b34c26 | 2016-09-30 10:40:52 -0500 | [diff] [blame] | 8936 | if (PageDirty(page)) |
| 8937 | btrfs_qgroup_free_data(inode, page_start, PAGE_SIZE); |
Filipe David Borba Manana | 131e404 | 2013-11-19 22:29:35 +0000 | [diff] [blame] | 8938 | if (!inode_evicting) { |
| 8939 | clear_extent_bit(tree, page_start, page_end, |
| 8940 | EXTENT_LOCKED | EXTENT_DIRTY | |
| 8941 | EXTENT_DELALLOC | EXTENT_DO_ACCOUNTING | |
| 8942 | EXTENT_DEFRAG, 1, 1, |
| 8943 | &cached_state, GFP_NOFS); |
| 8944 | |
| 8945 | __btrfs_releasepage(page, GFP_NOFS); |
| 8946 | } |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 8947 | |
Chris Mason | 4a09675 | 2008-07-21 10:29:44 -0400 | [diff] [blame] | 8948 | ClearPageChecked(page); |
Chris Mason | 9ad6b7bc | 2008-04-18 16:11:30 -0400 | [diff] [blame] | 8949 | if (PagePrivate(page)) { |
Chris Mason | 9ad6b7bc | 2008-04-18 16:11:30 -0400 | [diff] [blame] | 8950 | ClearPagePrivate(page); |
| 8951 | set_page_private(page, 0); |
Kirill A. Shutemov | 09cbfea | 2016-04-01 15:29:47 +0300 | [diff] [blame] | 8952 | put_page(page); |
Chris Mason | 9ad6b7bc | 2008-04-18 16:11:30 -0400 | [diff] [blame] | 8953 | } |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 8954 | } |
| 8955 | |
Chris Mason | 9ebefb18 | 2007-06-15 13:50:00 -0400 | [diff] [blame] | 8956 | /* |
| 8957 | * btrfs_page_mkwrite() is not allowed to change the file size as it gets |
| 8958 | * called from a page fault handler when a page is first dirtied. Hence we must |
| 8959 | * be careful to check for EOF conditions here. We set the page up correctly |
| 8960 | * for a written page which means we get ENOSPC checking when writing into |
| 8961 | * holes and correct delalloc and unwritten extent mapping on filesystems that |
| 8962 | * support these features. |
| 8963 | * |
| 8964 | * We are not allowed to take the i_mutex here so we have to play games to |
| 8965 | * protect against truncate races as the page could now be beyond EOF. Because |
| 8966 | * vmtruncate() writes the inode size before removing pages, once we have the |
| 8967 | * page lock we can determine safely if the page is beyond EOF. If it is not |
| 8968 | * beyond EOF, then the page is guaranteed safe against truncation until we |
| 8969 | * unlock the page. |
| 8970 | */ |
Nick Piggin | c2ec175 | 2009-03-31 15:23:21 -0700 | [diff] [blame] | 8971 | int btrfs_page_mkwrite(struct vm_area_struct *vma, struct vm_fault *vmf) |
Chris Mason | 9ebefb18 | 2007-06-15 13:50:00 -0400 | [diff] [blame] | 8972 | { |
Nick Piggin | c2ec175 | 2009-03-31 15:23:21 -0700 | [diff] [blame] | 8973 | struct page *page = vmf->page; |
Al Viro | 496ad9a | 2013-01-23 17:07:38 -0500 | [diff] [blame] | 8974 | struct inode *inode = file_inode(vma->vm_file); |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 8975 | struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb); |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 8976 | struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree; |
| 8977 | struct btrfs_ordered_extent *ordered; |
Josef Bacik | 2ac55d4 | 2010-02-03 19:33:23 +0000 | [diff] [blame] | 8978 | struct extent_state *cached_state = NULL; |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 8979 | char *kaddr; |
| 8980 | unsigned long zero_start; |
Chris Mason | 9ebefb18 | 2007-06-15 13:50:00 -0400 | [diff] [blame] | 8981 | loff_t size; |
Chris Mason | 1832a6d | 2007-12-21 16:27:21 -0500 | [diff] [blame] | 8982 | int ret; |
Chris Mason | 9998eb7 | 2012-01-25 13:47:40 -0500 | [diff] [blame] | 8983 | int reserved = 0; |
Chandan Rajendra | d0b7da88 | 2016-01-21 15:55:57 +0530 | [diff] [blame] | 8984 | u64 reserved_space; |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 8985 | u64 page_start; |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 8986 | u64 page_end; |
Chandan Rajendra | d0b7da88 | 2016-01-21 15:55:57 +0530 | [diff] [blame] | 8987 | u64 end; |
| 8988 | |
Kirill A. Shutemov | 09cbfea | 2016-04-01 15:29:47 +0300 | [diff] [blame] | 8989 | reserved_space = PAGE_SIZE; |
Chris Mason | 9ebefb18 | 2007-06-15 13:50:00 -0400 | [diff] [blame] | 8990 | |
Jan Kara | b2b5ef5 | 2012-06-12 16:20:45 +0200 | [diff] [blame] | 8991 | sb_start_pagefault(inode->i_sb); |
Qu Wenruo | df48063 | 2015-09-08 17:25:54 +0800 | [diff] [blame] | 8992 | page_start = page_offset(page); |
Kirill A. Shutemov | 09cbfea | 2016-04-01 15:29:47 +0300 | [diff] [blame] | 8993 | page_end = page_start + PAGE_SIZE - 1; |
Chandan Rajendra | d0b7da88 | 2016-01-21 15:55:57 +0530 | [diff] [blame] | 8994 | end = page_end; |
Qu Wenruo | df48063 | 2015-09-08 17:25:54 +0800 | [diff] [blame] | 8995 | |
Chandan Rajendra | d0b7da88 | 2016-01-21 15:55:57 +0530 | [diff] [blame] | 8996 | /* |
| 8997 | * Reserving delalloc space after obtaining the page lock can lead to |
| 8998 | * deadlock. For example, if a dirty page is locked by this function |
| 8999 | * and the call to btrfs_delalloc_reserve_space() ends up triggering |
| 9000 | * dirty page write out, then the btrfs_writepage() function could |
| 9001 | * end up waiting indefinitely to get a lock on the page currently |
| 9002 | * being processed by btrfs_page_mkwrite() function. |
| 9003 | */ |
Qu Wenruo | 7cf5b97 | 2015-09-08 17:25:55 +0800 | [diff] [blame] | 9004 | ret = btrfs_delalloc_reserve_space(inode, page_start, |
Chandan Rajendra | d0b7da88 | 2016-01-21 15:55:57 +0530 | [diff] [blame] | 9005 | reserved_space); |
Chris Mason | 9998eb7 | 2012-01-25 13:47:40 -0500 | [diff] [blame] | 9006 | if (!ret) { |
Josef Bacik | e41f941 | 2012-03-26 09:46:47 -0400 | [diff] [blame] | 9007 | ret = file_update_time(vma->vm_file); |
Chris Mason | 9998eb7 | 2012-01-25 13:47:40 -0500 | [diff] [blame] | 9008 | reserved = 1; |
| 9009 | } |
Nick Piggin | 56a76f8 | 2009-03-31 15:23:23 -0700 | [diff] [blame] | 9010 | if (ret) { |
| 9011 | if (ret == -ENOMEM) |
| 9012 | ret = VM_FAULT_OOM; |
| 9013 | else /* -ENOSPC, -EIO, etc */ |
| 9014 | ret = VM_FAULT_SIGBUS; |
Chris Mason | 9998eb7 | 2012-01-25 13:47:40 -0500 | [diff] [blame] | 9015 | if (reserved) |
| 9016 | goto out; |
| 9017 | goto out_noreserve; |
Nick Piggin | 56a76f8 | 2009-03-31 15:23:23 -0700 | [diff] [blame] | 9018 | } |
Chris Mason | 1832a6d | 2007-12-21 16:27:21 -0500 | [diff] [blame] | 9019 | |
Nick Piggin | 56a76f8 | 2009-03-31 15:23:23 -0700 | [diff] [blame] | 9020 | ret = VM_FAULT_NOPAGE; /* make the VM retry the fault */ |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 9021 | again: |
Chris Mason | 9ebefb18 | 2007-06-15 13:50:00 -0400 | [diff] [blame] | 9022 | lock_page(page); |
Chris Mason | 9ebefb18 | 2007-06-15 13:50:00 -0400 | [diff] [blame] | 9023 | size = i_size_read(inode); |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 9024 | |
Chris Mason | 9ebefb18 | 2007-06-15 13:50:00 -0400 | [diff] [blame] | 9025 | if ((page->mapping != inode->i_mapping) || |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 9026 | (page_start >= size)) { |
Chris Mason | 9ebefb18 | 2007-06-15 13:50:00 -0400 | [diff] [blame] | 9027 | /* page got truncated out from underneath us */ |
| 9028 | goto out_unlock; |
| 9029 | } |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 9030 | wait_on_page_writeback(page); |
| 9031 | |
David Sterba | ff13db4 | 2015-12-03 14:30:40 +0100 | [diff] [blame] | 9032 | lock_extent_bits(io_tree, page_start, page_end, &cached_state); |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 9033 | set_page_extent_mapped(page); |
| 9034 | |
Chris Mason | eb84ae0 | 2008-07-17 13:53:27 -0400 | [diff] [blame] | 9035 | /* |
| 9036 | * we can't set the delalloc bits if there are pending ordered |
| 9037 | * extents. Drop our locks and wait for them to finish |
| 9038 | */ |
Chandan Rajendra | d0b7da88 | 2016-01-21 15:55:57 +0530 | [diff] [blame] | 9039 | ordered = btrfs_lookup_ordered_range(inode, page_start, page_end); |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 9040 | if (ordered) { |
Josef Bacik | 2ac55d4 | 2010-02-03 19:33:23 +0000 | [diff] [blame] | 9041 | unlock_extent_cached(io_tree, page_start, page_end, |
| 9042 | &cached_state, GFP_NOFS); |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 9043 | unlock_page(page); |
Chris Mason | eb84ae0 | 2008-07-17 13:53:27 -0400 | [diff] [blame] | 9044 | btrfs_start_ordered_extent(inode, ordered, 1); |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 9045 | btrfs_put_ordered_extent(ordered); |
| 9046 | goto again; |
| 9047 | } |
| 9048 | |
Kirill A. Shutemov | 09cbfea | 2016-04-01 15:29:47 +0300 | [diff] [blame] | 9049 | if (page->index == ((size - 1) >> PAGE_SHIFT)) { |
Jeff Mahoney | da17066 | 2016-06-15 09:22:56 -0400 | [diff] [blame] | 9050 | reserved_space = round_up(size - page_start, |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 9051 | fs_info->sectorsize); |
Kirill A. Shutemov | 09cbfea | 2016-04-01 15:29:47 +0300 | [diff] [blame] | 9052 | if (reserved_space < PAGE_SIZE) { |
Chandan Rajendra | d0b7da88 | 2016-01-21 15:55:57 +0530 | [diff] [blame] | 9053 | end = page_start + reserved_space - 1; |
| 9054 | spin_lock(&BTRFS_I(inode)->lock); |
| 9055 | BTRFS_I(inode)->outstanding_extents++; |
| 9056 | spin_unlock(&BTRFS_I(inode)->lock); |
| 9057 | btrfs_delalloc_release_space(inode, page_start, |
Kirill A. Shutemov | 09cbfea | 2016-04-01 15:29:47 +0300 | [diff] [blame] | 9058 | PAGE_SIZE - reserved_space); |
Chandan Rajendra | d0b7da88 | 2016-01-21 15:55:57 +0530 | [diff] [blame] | 9059 | } |
| 9060 | } |
| 9061 | |
Josef Bacik | fbf1908 | 2009-10-01 17:10:23 -0400 | [diff] [blame] | 9062 | /* |
Liu Bo | 5416034 | 2016-12-13 12:15:19 -0800 | [diff] [blame^] | 9063 | * page_mkwrite gets called when the page is firstly dirtied after it's |
| 9064 | * faulted in, but write(2) could also dirty a page and set delalloc |
| 9065 | * bits, thus in this case for space account reason, we still need to |
| 9066 | * clear any delalloc bits within this page range since we have to |
| 9067 | * reserve data&meta space before lock_page() (see above comments). |
Josef Bacik | fbf1908 | 2009-10-01 17:10:23 -0400 | [diff] [blame] | 9068 | */ |
Chandan Rajendra | d0b7da88 | 2016-01-21 15:55:57 +0530 | [diff] [blame] | 9069 | clear_extent_bit(&BTRFS_I(inode)->io_tree, page_start, end, |
Liu Bo | 9e8a4a8 | 2012-09-05 19:10:51 -0600 | [diff] [blame] | 9070 | EXTENT_DIRTY | EXTENT_DELALLOC | |
| 9071 | EXTENT_DO_ACCOUNTING | EXTENT_DEFRAG, |
Josef Bacik | 2ac55d4 | 2010-02-03 19:33:23 +0000 | [diff] [blame] | 9072 | 0, 0, &cached_state, GFP_NOFS); |
Josef Bacik | fbf1908 | 2009-10-01 17:10:23 -0400 | [diff] [blame] | 9073 | |
Chandan Rajendra | d0b7da88 | 2016-01-21 15:55:57 +0530 | [diff] [blame] | 9074 | ret = btrfs_set_extent_delalloc(inode, page_start, end, |
Qu Wenruo | ba8b04c | 2016-07-19 16:50:36 +0800 | [diff] [blame] | 9075 | &cached_state, 0); |
Josef Bacik | 9ed74f2 | 2009-09-11 16:12:44 -0400 | [diff] [blame] | 9076 | if (ret) { |
Josef Bacik | 2ac55d4 | 2010-02-03 19:33:23 +0000 | [diff] [blame] | 9077 | unlock_extent_cached(io_tree, page_start, page_end, |
| 9078 | &cached_state, GFP_NOFS); |
Josef Bacik | 9ed74f2 | 2009-09-11 16:12:44 -0400 | [diff] [blame] | 9079 | ret = VM_FAULT_SIGBUS; |
| 9080 | goto out_unlock; |
| 9081 | } |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 9082 | ret = 0; |
Chris Mason | 9ebefb18 | 2007-06-15 13:50:00 -0400 | [diff] [blame] | 9083 | |
| 9084 | /* page is wholly or partially inside EOF */ |
Kirill A. Shutemov | 09cbfea | 2016-04-01 15:29:47 +0300 | [diff] [blame] | 9085 | if (page_start + PAGE_SIZE > size) |
| 9086 | zero_start = size & ~PAGE_MASK; |
Chris Mason | 9ebefb18 | 2007-06-15 13:50:00 -0400 | [diff] [blame] | 9087 | else |
Kirill A. Shutemov | 09cbfea | 2016-04-01 15:29:47 +0300 | [diff] [blame] | 9088 | zero_start = PAGE_SIZE; |
Chris Mason | 9ebefb18 | 2007-06-15 13:50:00 -0400 | [diff] [blame] | 9089 | |
Kirill A. Shutemov | 09cbfea | 2016-04-01 15:29:47 +0300 | [diff] [blame] | 9090 | if (zero_start != PAGE_SIZE) { |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 9091 | kaddr = kmap(page); |
Kirill A. Shutemov | 09cbfea | 2016-04-01 15:29:47 +0300 | [diff] [blame] | 9092 | memset(kaddr + zero_start, 0, PAGE_SIZE - zero_start); |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 9093 | flush_dcache_page(page); |
| 9094 | kunmap(page); |
| 9095 | } |
Chris Mason | 247e743 | 2008-07-17 12:53:51 -0400 | [diff] [blame] | 9096 | ClearPageChecked(page); |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 9097 | set_page_dirty(page); |
Chris Mason | 50a9b21 | 2009-09-11 12:33:12 -0400 | [diff] [blame] | 9098 | SetPageUptodate(page); |
Chris Mason | 5a3f23d | 2009-03-31 13:27:11 -0400 | [diff] [blame] | 9099 | |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 9100 | BTRFS_I(inode)->last_trans = fs_info->generation; |
Chris Mason | 257c62e | 2009-10-13 13:21:08 -0400 | [diff] [blame] | 9101 | BTRFS_I(inode)->last_sub_trans = BTRFS_I(inode)->root->log_transid; |
Liu Bo | 46d8bc3 | 2012-08-29 01:07:55 -0600 | [diff] [blame] | 9102 | 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] | 9103 | |
Josef Bacik | 2ac55d4 | 2010-02-03 19:33:23 +0000 | [diff] [blame] | 9104 | 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] | 9105 | |
| 9106 | out_unlock: |
Jan Kara | b2b5ef5 | 2012-06-12 16:20:45 +0200 | [diff] [blame] | 9107 | if (!ret) { |
| 9108 | sb_end_pagefault(inode->i_sb); |
Chris Mason | 50a9b21 | 2009-09-11 12:33:12 -0400 | [diff] [blame] | 9109 | return VM_FAULT_LOCKED; |
Jan Kara | b2b5ef5 | 2012-06-12 16:20:45 +0200 | [diff] [blame] | 9110 | } |
Chris Mason | 9ebefb18 | 2007-06-15 13:50:00 -0400 | [diff] [blame] | 9111 | unlock_page(page); |
Chris Mason | 1832a6d | 2007-12-21 16:27:21 -0500 | [diff] [blame] | 9112 | out: |
Chandan Rajendra | d0b7da88 | 2016-01-21 15:55:57 +0530 | [diff] [blame] | 9113 | btrfs_delalloc_release_space(inode, page_start, reserved_space); |
Chris Mason | 9998eb7 | 2012-01-25 13:47:40 -0500 | [diff] [blame] | 9114 | out_noreserve: |
Jan Kara | b2b5ef5 | 2012-06-12 16:20:45 +0200 | [diff] [blame] | 9115 | sb_end_pagefault(inode->i_sb); |
Chris Mason | 9ebefb18 | 2007-06-15 13:50:00 -0400 | [diff] [blame] | 9116 | return ret; |
| 9117 | } |
| 9118 | |
Josef Bacik | a41ad39 | 2011-01-31 15:30:16 -0500 | [diff] [blame] | 9119 | static int btrfs_truncate(struct inode *inode) |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 9120 | { |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 9121 | struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 9122 | struct btrfs_root *root = BTRFS_I(inode)->root; |
Josef Bacik | fcb80c2 | 2011-05-03 10:40:22 -0400 | [diff] [blame] | 9123 | struct btrfs_block_rsv *rsv; |
Josef Bacik | a71754f | 2013-06-17 17:14:39 -0400 | [diff] [blame] | 9124 | int ret = 0; |
Josef Bacik | 3893e33 | 2011-01-31 16:03:11 -0500 | [diff] [blame] | 9125 | int err = 0; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 9126 | struct btrfs_trans_handle *trans; |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 9127 | u64 mask = fs_info->sectorsize - 1; |
| 9128 | u64 min_size = btrfs_calc_trunc_metadata_size(fs_info, 1); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 9129 | |
Josef Bacik | 0ef8b72 | 2013-10-25 16:13:35 -0400 | [diff] [blame] | 9130 | ret = btrfs_wait_ordered_range(inode, inode->i_size & (~mask), |
| 9131 | (u64)-1); |
| 9132 | if (ret) |
| 9133 | return ret; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 9134 | |
Josef Bacik | fcb80c2 | 2011-05-03 10:40:22 -0400 | [diff] [blame] | 9135 | /* |
Nicholas D Steeves | 0132761 | 2016-05-19 21:18:45 -0400 | [diff] [blame] | 9136 | * 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] | 9137 | * 3 things going on here |
| 9138 | * |
| 9139 | * 1) We need to reserve space for our orphan item and the space to |
| 9140 | * delete our orphan item. Lord knows we don't want to have a dangling |
| 9141 | * orphan item because we didn't reserve space to remove it. |
| 9142 | * |
| 9143 | * 2) We need to reserve space to update our inode. |
| 9144 | * |
| 9145 | * 3) We need to have something to cache all the space that is going to |
| 9146 | * be free'd up by the truncate operation, but also have some slack |
| 9147 | * space reserved in case it uses space during the truncate (thank you |
| 9148 | * very much snapshotting). |
| 9149 | * |
Nicholas D Steeves | 0132761 | 2016-05-19 21:18:45 -0400 | [diff] [blame] | 9150 | * 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] | 9151 | * 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] | 9152 | * 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] | 9153 | * doesn't end up using space reserved for updating the inode or |
| 9154 | * removing the orphan item. We also need to be able to stop the |
| 9155 | * transaction and start a new one, which means we need to be able to |
| 9156 | * update the inode several times, and we have no idea of knowing how |
| 9157 | * 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] | 9158 | * 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] | 9159 | * Then there is the orphan item, which does indeed need to be held on |
| 9160 | * to for the whole operation, and we need nobody to touch this reserved |
| 9161 | * space except the orphan code. |
| 9162 | * |
| 9163 | * So that leaves us with |
| 9164 | * |
| 9165 | * 1) root->orphan_block_rsv - for the orphan deletion. |
| 9166 | * 2) rsv - for the truncate reservation, which we will steal from the |
| 9167 | * transaction reservation. |
| 9168 | * 3) fs_info->trans_block_rsv - this will have 1 items worth left for |
| 9169 | * updating the inode. |
| 9170 | */ |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 9171 | rsv = btrfs_alloc_block_rsv(fs_info, BTRFS_BLOCK_RSV_TEMP); |
Josef Bacik | fcb80c2 | 2011-05-03 10:40:22 -0400 | [diff] [blame] | 9172 | if (!rsv) |
| 9173 | return -ENOMEM; |
Josef Bacik | 4a33854 | 2011-08-29 11:01:31 -0400 | [diff] [blame] | 9174 | rsv->size = min_size; |
Josef Bacik | ca7e70f | 2012-08-27 17:48:15 -0400 | [diff] [blame] | 9175 | rsv->failfast = 1; |
Josef Bacik | f0cd846 | 2011-03-04 14:37:08 -0500 | [diff] [blame] | 9176 | |
Josef Bacik | 907cbce | 2011-08-08 13:46:15 -0400 | [diff] [blame] | 9177 | /* |
Josef Bacik | 0712718 | 2011-08-19 10:29:59 -0400 | [diff] [blame] | 9178 | * 1 for the truncate slack space |
Josef Bacik | 907cbce | 2011-08-08 13:46:15 -0400 | [diff] [blame] | 9179 | * 1 for updating the inode. |
| 9180 | */ |
Josef Bacik | f3fe820 | 2013-01-07 17:03:21 -0500 | [diff] [blame] | 9181 | trans = btrfs_start_transaction(root, 2); |
Josef Bacik | fcb80c2 | 2011-05-03 10:40:22 -0400 | [diff] [blame] | 9182 | if (IS_ERR(trans)) { |
| 9183 | err = PTR_ERR(trans); |
| 9184 | goto out; |
| 9185 | } |
Josef Bacik | f0cd846 | 2011-03-04 14:37:08 -0500 | [diff] [blame] | 9186 | |
Josef Bacik | 907cbce | 2011-08-08 13:46:15 -0400 | [diff] [blame] | 9187 | /* Migrate the slack space for the truncate to our reserve */ |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 9188 | ret = btrfs_block_rsv_migrate(&fs_info->trans_block_rsv, rsv, |
Josef Bacik | 25d609f | 2016-03-25 13:25:48 -0400 | [diff] [blame] | 9189 | min_size, 0); |
Josef Bacik | fcb80c2 | 2011-05-03 10:40:22 -0400 | [diff] [blame] | 9190 | BUG_ON(ret); |
Josef Bacik | f0cd846 | 2011-03-04 14:37:08 -0500 | [diff] [blame] | 9191 | |
Chris Mason | 5a3f23d | 2009-03-31 13:27:11 -0400 | [diff] [blame] | 9192 | /* |
Josef Bacik | 5dc562c | 2012-08-17 13:14:17 -0400 | [diff] [blame] | 9193 | * So if we truncate and then write and fsync we normally would just |
| 9194 | * write the extents that changed, which is a problem if we need to |
| 9195 | * first truncate that entire inode. So set this flag so we write out |
| 9196 | * all of the extents in the inode to the sync log so we're completely |
| 9197 | * safe. |
| 9198 | */ |
| 9199 | set_bit(BTRFS_INODE_NEEDS_FULL_SYNC, &BTRFS_I(inode)->runtime_flags); |
Josef Bacik | ca7e70f | 2012-08-27 17:48:15 -0400 | [diff] [blame] | 9200 | trans->block_rsv = rsv; |
Josef Bacik | 5dc562c | 2012-08-17 13:14:17 -0400 | [diff] [blame] | 9201 | |
Yan, Zheng | 8082510 | 2009-11-12 09:35:36 +0000 | [diff] [blame] | 9202 | while (1) { |
| 9203 | ret = btrfs_truncate_inode_items(trans, root, inode, |
| 9204 | inode->i_size, |
| 9205 | BTRFS_EXTENT_DATA_KEY); |
Chris Mason | 28ed134 | 2014-12-17 09:41:04 -0800 | [diff] [blame] | 9206 | if (ret != -ENOSPC && ret != -EAGAIN) { |
Josef Bacik | 3893e33 | 2011-01-31 16:03:11 -0500 | [diff] [blame] | 9207 | err = ret; |
Yan, Zheng | 8082510 | 2009-11-12 09:35:36 +0000 | [diff] [blame] | 9208 | break; |
Josef Bacik | 3893e33 | 2011-01-31 16:03:11 -0500 | [diff] [blame] | 9209 | } |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 9210 | |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 9211 | trans->block_rsv = &fs_info->trans_block_rsv; |
Yan, Zheng | 8082510 | 2009-11-12 09:35:36 +0000 | [diff] [blame] | 9212 | ret = btrfs_update_inode(trans, root, inode); |
Josef Bacik | 3893e33 | 2011-01-31 16:03:11 -0500 | [diff] [blame] | 9213 | if (ret) { |
| 9214 | err = ret; |
| 9215 | break; |
| 9216 | } |
Josef Bacik | ca7e70f | 2012-08-27 17:48:15 -0400 | [diff] [blame] | 9217 | |
Jeff Mahoney | 3a45bb2 | 2016-09-09 21:39:03 -0400 | [diff] [blame] | 9218 | btrfs_end_transaction(trans); |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 9219 | btrfs_btree_balance_dirty(fs_info); |
Josef Bacik | ca7e70f | 2012-08-27 17:48:15 -0400 | [diff] [blame] | 9220 | |
| 9221 | trans = btrfs_start_transaction(root, 2); |
| 9222 | if (IS_ERR(trans)) { |
| 9223 | ret = err = PTR_ERR(trans); |
| 9224 | trans = NULL; |
| 9225 | break; |
| 9226 | } |
| 9227 | |
Wang Xiaoguang | 47b5d64 | 2016-09-07 20:17:38 +0800 | [diff] [blame] | 9228 | btrfs_block_rsv_release(fs_info, rsv, -1); |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 9229 | ret = btrfs_block_rsv_migrate(&fs_info->trans_block_rsv, |
Josef Bacik | 25d609f | 2016-03-25 13:25:48 -0400 | [diff] [blame] | 9230 | rsv, min_size, 0); |
Josef Bacik | ca7e70f | 2012-08-27 17:48:15 -0400 | [diff] [blame] | 9231 | BUG_ON(ret); /* shouldn't happen */ |
| 9232 | trans->block_rsv = rsv; |
Yan, Zheng | 8082510 | 2009-11-12 09:35:36 +0000 | [diff] [blame] | 9233 | } |
| 9234 | |
| 9235 | if (ret == 0 && inode->i_nlink > 0) { |
Josef Bacik | fcb80c2 | 2011-05-03 10:40:22 -0400 | [diff] [blame] | 9236 | trans->block_rsv = root->orphan_block_rsv; |
Yan, Zheng | 8082510 | 2009-11-12 09:35:36 +0000 | [diff] [blame] | 9237 | ret = btrfs_orphan_del(trans, inode); |
Josef Bacik | 3893e33 | 2011-01-31 16:03:11 -0500 | [diff] [blame] | 9238 | if (ret) |
| 9239 | err = ret; |
Yan, Zheng | 8082510 | 2009-11-12 09:35:36 +0000 | [diff] [blame] | 9240 | } |
| 9241 | |
Chris Mason | 917c16b | 2011-11-08 14:49:59 -0500 | [diff] [blame] | 9242 | if (trans) { |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 9243 | trans->block_rsv = &fs_info->trans_block_rsv; |
Chris Mason | 917c16b | 2011-11-08 14:49:59 -0500 | [diff] [blame] | 9244 | ret = btrfs_update_inode(trans, root, inode); |
| 9245 | if (ret && !err) |
| 9246 | err = ret; |
Josef Bacik | 7b12876 | 2008-07-24 12:17:14 -0400 | [diff] [blame] | 9247 | |
Jeff Mahoney | 3a45bb2 | 2016-09-09 21:39:03 -0400 | [diff] [blame] | 9248 | ret = btrfs_end_transaction(trans); |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 9249 | btrfs_btree_balance_dirty(fs_info); |
Chris Mason | 917c16b | 2011-11-08 14:49:59 -0500 | [diff] [blame] | 9250 | } |
Josef Bacik | fcb80c2 | 2011-05-03 10:40:22 -0400 | [diff] [blame] | 9251 | out: |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 9252 | btrfs_free_block_rsv(fs_info, rsv); |
Josef Bacik | fcb80c2 | 2011-05-03 10:40:22 -0400 | [diff] [blame] | 9253 | |
Josef Bacik | 3893e33 | 2011-01-31 16:03:11 -0500 | [diff] [blame] | 9254 | if (ret && !err) |
| 9255 | err = ret; |
Josef Bacik | a41ad39 | 2011-01-31 15:30:16 -0500 | [diff] [blame] | 9256 | |
Josef Bacik | 3893e33 | 2011-01-31 16:03:11 -0500 | [diff] [blame] | 9257 | return err; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 9258 | } |
| 9259 | |
Sven Wegener | 3b96362 | 2008-06-09 21:57:42 -0400 | [diff] [blame] | 9260 | /* |
Chris Mason | d352ac6 | 2008-09-29 15:18:18 -0400 | [diff] [blame] | 9261 | * create a new subvolume directory/inode (helper for the ioctl). |
| 9262 | */ |
Yan Zheng | d2fb343 | 2008-12-11 16:30:39 -0500 | [diff] [blame] | 9263 | int btrfs_create_subvol_root(struct btrfs_trans_handle *trans, |
Filipe David Borba Manana | 6354192 | 2014-01-07 11:47:46 +0000 | [diff] [blame] | 9264 | struct btrfs_root *new_root, |
| 9265 | struct btrfs_root *parent_root, |
| 9266 | u64 new_dirid) |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 9267 | { |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 9268 | struct inode *inode; |
Yan, Zheng | 76dda93 | 2009-09-21 16:00:26 -0400 | [diff] [blame] | 9269 | int err; |
Chris Mason | 00e4e6b | 2008-08-05 11:18:09 -0400 | [diff] [blame] | 9270 | u64 index = 0; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 9271 | |
Florian Albrechtskirchinger | 12fc9d0 | 2012-02-10 22:15:54 +0100 | [diff] [blame] | 9272 | inode = btrfs_new_inode(trans, new_root, NULL, "..", 2, |
| 9273 | new_dirid, new_dirid, |
| 9274 | S_IFDIR | (~current_umask() & S_IRWXUGO), |
| 9275 | &index); |
Chris Mason | 54aa1f4 | 2007-06-22 14:16:25 -0400 | [diff] [blame] | 9276 | if (IS_ERR(inode)) |
Christoph Hellwig | f46b5a6 | 2008-06-11 21:53:53 -0400 | [diff] [blame] | 9277 | return PTR_ERR(inode); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 9278 | inode->i_op = &btrfs_dir_inode_operations; |
| 9279 | inode->i_fop = &btrfs_dir_file_operations; |
| 9280 | |
Miklos Szeredi | bfe8684 | 2011-10-28 14:13:29 +0200 | [diff] [blame] | 9281 | set_nlink(inode, 1); |
Chris Mason | dbe674a | 2008-07-17 12:54:05 -0400 | [diff] [blame] | 9282 | btrfs_i_size_write(inode, 0); |
Chris Mason | b0d5d10 | 2014-09-08 13:08:51 -0700 | [diff] [blame] | 9283 | unlock_new_inode(inode); |
Sven Wegener | 3b96362 | 2008-06-09 21:57:42 -0400 | [diff] [blame] | 9284 | |
Filipe David Borba Manana | 6354192 | 2014-01-07 11:47:46 +0000 | [diff] [blame] | 9285 | err = btrfs_subvol_inherit_props(trans, new_root, parent_root); |
| 9286 | if (err) |
| 9287 | btrfs_err(new_root->fs_info, |
David Sterba | 351fd35 | 2014-05-15 16:48:20 +0200 | [diff] [blame] | 9288 | "error inheriting subvolume %llu properties: %d", |
Filipe David Borba Manana | 6354192 | 2014-01-07 11:47:46 +0000 | [diff] [blame] | 9289 | new_root->root_key.objectid, err); |
| 9290 | |
Yan, Zheng | 76dda93 | 2009-09-21 16:00:26 -0400 | [diff] [blame] | 9291 | err = btrfs_update_inode(trans, new_root, inode); |
Christoph Hellwig | cb8e709 | 2008-10-09 13:39:39 -0400 | [diff] [blame] | 9292 | |
Yan, Zheng | 76dda93 | 2009-09-21 16:00:26 -0400 | [diff] [blame] | 9293 | iput(inode); |
Mark Fasheh | ce59897 | 2011-07-26 11:32:23 -0700 | [diff] [blame] | 9294 | return err; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 9295 | } |
| 9296 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 9297 | struct inode *btrfs_alloc_inode(struct super_block *sb) |
| 9298 | { |
| 9299 | struct btrfs_inode *ei; |
Yan, Zheng | 2ead6ae | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 9300 | struct inode *inode; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 9301 | |
| 9302 | ei = kmem_cache_alloc(btrfs_inode_cachep, GFP_NOFS); |
| 9303 | if (!ei) |
| 9304 | return NULL; |
Yan, Zheng | 2ead6ae | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 9305 | |
| 9306 | ei->root = NULL; |
Yan, Zheng | 2ead6ae | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 9307 | ei->generation = 0; |
Josef Bacik | 15ee9bc | 2007-08-10 16:22:09 -0400 | [diff] [blame] | 9308 | ei->last_trans = 0; |
Chris Mason | 257c62e | 2009-10-13 13:21:08 -0400 | [diff] [blame] | 9309 | ei->last_sub_trans = 0; |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 9310 | ei->logged_trans = 0; |
Yan, Zheng | 2ead6ae | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 9311 | ei->delalloc_bytes = 0; |
Wang Shilong | 47059d9 | 2014-07-03 18:22:07 +0800 | [diff] [blame] | 9312 | ei->defrag_bytes = 0; |
Yan, Zheng | 2ead6ae | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 9313 | ei->disk_i_size = 0; |
| 9314 | ei->flags = 0; |
Josef Bacik | 7709cde | 2011-08-04 10:25:02 -0400 | [diff] [blame] | 9315 | ei->csum_bytes = 0; |
Yan, Zheng | 2ead6ae | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 9316 | ei->index_cnt = (u64)-1; |
Miao Xie | 67de117 | 2013-12-26 13:07:06 +0800 | [diff] [blame] | 9317 | ei->dir_index = 0; |
Yan, Zheng | 2ead6ae | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 9318 | ei->last_unlink_trans = 0; |
Liu Bo | 46d8bc3 | 2012-08-29 01:07:55 -0600 | [diff] [blame] | 9319 | ei->last_log_commit = 0; |
David Sterba | 8089fe6 | 2015-11-19 14:15:51 +0100 | [diff] [blame] | 9320 | ei->delayed_iput_count = 0; |
Yan, Zheng | 2ead6ae | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 9321 | |
Josef Bacik | 9e0baf6 | 2011-07-15 15:16:44 +0000 | [diff] [blame] | 9322 | spin_lock_init(&ei->lock); |
| 9323 | ei->outstanding_extents = 0; |
| 9324 | ei->reserved_extents = 0; |
Yan, Zheng | 2ead6ae | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 9325 | |
Josef Bacik | 72ac3c0 | 2012-05-23 14:13:11 -0400 | [diff] [blame] | 9326 | ei->runtime_flags = 0; |
Li Zefan | 261507a0 | 2010-12-17 14:21:50 +0800 | [diff] [blame] | 9327 | ei->force_compress = BTRFS_COMPRESS_NONE; |
Yan, Zheng | 2ead6ae | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 9328 | |
Miao Xie | 16cdcec | 2011-04-22 18:12:22 +0800 | [diff] [blame] | 9329 | ei->delayed_node = NULL; |
| 9330 | |
chandan r | 9cc97d6 | 2012-07-04 12:48:07 +0530 | [diff] [blame] | 9331 | ei->i_otime.tv_sec = 0; |
| 9332 | ei->i_otime.tv_nsec = 0; |
| 9333 | |
Yan, Zheng | 2ead6ae | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 9334 | inode = &ei->vfs_inode; |
David Sterba | a8067e0 | 2011-04-21 00:34:43 +0200 | [diff] [blame] | 9335 | extent_map_tree_init(&ei->extent_tree); |
David Sterba | f993c88 | 2011-04-20 23:35:57 +0200 | [diff] [blame] | 9336 | extent_io_tree_init(&ei->io_tree, &inode->i_data); |
| 9337 | extent_io_tree_init(&ei->io_failure_tree, &inode->i_data); |
Josef Bacik | 0b32f4b | 2012-03-13 09:38:00 -0400 | [diff] [blame] | 9338 | ei->io_tree.track_uptodate = 1; |
| 9339 | ei->io_failure_tree.track_uptodate = 1; |
Josef Bacik | b812ce2 | 2012-11-16 13:56:32 -0500 | [diff] [blame] | 9340 | atomic_set(&ei->sync_writers, 0); |
Yan, Zheng | 2ead6ae | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 9341 | mutex_init(&ei->log_mutex); |
Josef Bacik | f248679 | 2012-01-13 12:09:22 -0500 | [diff] [blame] | 9342 | mutex_init(&ei->delalloc_mutex); |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 9343 | btrfs_ordered_inode_tree_init(&ei->ordered_tree); |
Yan, Zheng | 2ead6ae | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 9344 | INIT_LIST_HEAD(&ei->delalloc_inodes); |
David Sterba | 8089fe6 | 2015-11-19 14:15:51 +0100 | [diff] [blame] | 9345 | INIT_LIST_HEAD(&ei->delayed_iput); |
Yan, Zheng | 2ead6ae | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 9346 | RB_CLEAR_NODE(&ei->rb_node); |
Filipe Manana | 5f9a8a5 | 2016-05-12 13:53:36 +0100 | [diff] [blame] | 9347 | init_rwsem(&ei->dio_sem); |
Yan, Zheng | 2ead6ae | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 9348 | |
| 9349 | return inode; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 9350 | } |
| 9351 | |
Josef Bacik | aaedb55 | 2013-10-11 14:44:09 -0400 | [diff] [blame] | 9352 | #ifdef CONFIG_BTRFS_FS_RUN_SANITY_TESTS |
| 9353 | void btrfs_test_destroy_inode(struct inode *inode) |
| 9354 | { |
| 9355 | btrfs_drop_extent_cache(inode, 0, (u64)-1, 0); |
| 9356 | kmem_cache_free(btrfs_inode_cachep, BTRFS_I(inode)); |
| 9357 | } |
| 9358 | #endif |
| 9359 | |
Nick Piggin | fa0d7e3d | 2011-01-07 17:49:49 +1100 | [diff] [blame] | 9360 | static void btrfs_i_callback(struct rcu_head *head) |
| 9361 | { |
| 9362 | struct inode *inode = container_of(head, struct inode, i_rcu); |
Nick Piggin | fa0d7e3d | 2011-01-07 17:49:49 +1100 | [diff] [blame] | 9363 | kmem_cache_free(btrfs_inode_cachep, BTRFS_I(inode)); |
| 9364 | } |
| 9365 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 9366 | void btrfs_destroy_inode(struct inode *inode) |
| 9367 | { |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 9368 | struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb); |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 9369 | struct btrfs_ordered_extent *ordered; |
Chris Mason | 5a3f23d | 2009-03-31 13:27:11 -0400 | [diff] [blame] | 9370 | struct btrfs_root *root = BTRFS_I(inode)->root; |
| 9371 | |
Al Viro | b3d9b7a | 2012-06-09 13:51:19 -0400 | [diff] [blame] | 9372 | WARN_ON(!hlist_empty(&inode->i_dentry)); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 9373 | WARN_ON(inode->i_data.nrpages); |
Josef Bacik | 9e0baf6 | 2011-07-15 15:16:44 +0000 | [diff] [blame] | 9374 | WARN_ON(BTRFS_I(inode)->outstanding_extents); |
| 9375 | WARN_ON(BTRFS_I(inode)->reserved_extents); |
Josef Bacik | 7709cde | 2011-08-04 10:25:02 -0400 | [diff] [blame] | 9376 | WARN_ON(BTRFS_I(inode)->delalloc_bytes); |
| 9377 | WARN_ON(BTRFS_I(inode)->csum_bytes); |
Wang Shilong | 47059d9 | 2014-07-03 18:22:07 +0800 | [diff] [blame] | 9378 | WARN_ON(BTRFS_I(inode)->defrag_bytes); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 9379 | |
Chris Mason | 5a3f23d | 2009-03-31 13:27:11 -0400 | [diff] [blame] | 9380 | /* |
Josef Bacik | a6dbd42 | 2009-11-11 15:53:34 -0500 | [diff] [blame] | 9381 | * This can happen where we create an inode, but somebody else also |
| 9382 | * created the same inode and we need to destroy the one we already |
| 9383 | * created. |
| 9384 | */ |
| 9385 | if (!root) |
| 9386 | goto free; |
| 9387 | |
Josef Bacik | 8a35d95 | 2012-05-23 14:26:42 -0400 | [diff] [blame] | 9388 | if (test_bit(BTRFS_INODE_HAS_ORPHAN_ITEM, |
| 9389 | &BTRFS_I(inode)->runtime_flags)) { |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 9390 | btrfs_info(fs_info, "inode %llu still on the orphan list", |
Nikolay Borisov | 4a0cc7c | 2017-01-10 20:35:31 +0200 | [diff] [blame] | 9391 | btrfs_ino(BTRFS_I(inode))); |
Josef Bacik | 8a35d95 | 2012-05-23 14:26:42 -0400 | [diff] [blame] | 9392 | atomic_dec(&root->orphan_inodes); |
Josef Bacik | 7b12876 | 2008-07-24 12:17:14 -0400 | [diff] [blame] | 9393 | } |
Josef Bacik | 7b12876 | 2008-07-24 12:17:14 -0400 | [diff] [blame] | 9394 | |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 9395 | while (1) { |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 9396 | ordered = btrfs_lookup_first_ordered_extent(inode, (u64)-1); |
| 9397 | if (!ordered) |
| 9398 | break; |
| 9399 | else { |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 9400 | btrfs_err(fs_info, |
Jeff Mahoney | 5d163e0 | 2016-09-20 10:05:00 -0400 | [diff] [blame] | 9401 | "found ordered extent %llu %llu on inode cleanup", |
| 9402 | ordered->file_offset, ordered->len); |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 9403 | btrfs_remove_ordered_extent(inode, ordered); |
| 9404 | btrfs_put_ordered_extent(ordered); |
| 9405 | btrfs_put_ordered_extent(ordered); |
| 9406 | } |
| 9407 | } |
Qu Wenruo | 56fa9d0 | 2015-10-13 09:53:10 +0800 | [diff] [blame] | 9408 | btrfs_qgroup_check_reserved_leak(inode); |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 9409 | inode_tree_del(inode); |
Zheng Yan | 5b21f2e | 2008-09-26 10:05:38 -0400 | [diff] [blame] | 9410 | btrfs_drop_extent_cache(inode, 0, (u64)-1, 0); |
Josef Bacik | a6dbd42 | 2009-11-11 15:53:34 -0500 | [diff] [blame] | 9411 | free: |
Nick Piggin | fa0d7e3d | 2011-01-07 17:49:49 +1100 | [diff] [blame] | 9412 | call_rcu(&inode->i_rcu, btrfs_i_callback); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 9413 | } |
| 9414 | |
Al Viro | 45321ac | 2010-06-07 13:43:19 -0400 | [diff] [blame] | 9415 | int btrfs_drop_inode(struct inode *inode) |
Yan, Zheng | 76dda93 | 2009-09-21 16:00:26 -0400 | [diff] [blame] | 9416 | { |
| 9417 | struct btrfs_root *root = BTRFS_I(inode)->root; |
Al Viro | 45321ac | 2010-06-07 13:43:19 -0400 | [diff] [blame] | 9418 | |
Naohiro Aota | 6379ef9 | 2013-06-06 09:56:34 +0000 | [diff] [blame] | 9419 | if (root == NULL) |
| 9420 | return 1; |
| 9421 | |
Liu Bo | fa6ac87 | 2013-02-20 14:10:23 +0000 | [diff] [blame] | 9422 | /* the snap/subvol tree is on deleting */ |
Stefan Behrens | 69e9c6c | 2013-09-05 16:58:43 +0200 | [diff] [blame] | 9423 | if (btrfs_root_refs(&root->root_item) == 0) |
Al Viro | 45321ac | 2010-06-07 13:43:19 -0400 | [diff] [blame] | 9424 | return 1; |
Yan, Zheng | 76dda93 | 2009-09-21 16:00:26 -0400 | [diff] [blame] | 9425 | else |
Al Viro | 45321ac | 2010-06-07 13:43:19 -0400 | [diff] [blame] | 9426 | return generic_drop_inode(inode); |
Yan, Zheng | 76dda93 | 2009-09-21 16:00:26 -0400 | [diff] [blame] | 9427 | } |
| 9428 | |
Sven Wegener | 0ee0fda | 2008-07-30 16:54:26 -0400 | [diff] [blame] | 9429 | static void init_once(void *foo) |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 9430 | { |
| 9431 | struct btrfs_inode *ei = (struct btrfs_inode *) foo; |
| 9432 | |
| 9433 | inode_init_once(&ei->vfs_inode); |
| 9434 | } |
| 9435 | |
| 9436 | void btrfs_destroy_cachep(void) |
| 9437 | { |
Kirill A. Shutemov | 8c0a853 | 2012-09-26 11:33:07 +1000 | [diff] [blame] | 9438 | /* |
| 9439 | * Make sure all delayed rcu free inodes are flushed before we |
| 9440 | * destroy cache. |
| 9441 | */ |
| 9442 | rcu_barrier(); |
Kinglong Mee | 5598e90 | 2016-01-29 21:36:35 +0800 | [diff] [blame] | 9443 | kmem_cache_destroy(btrfs_inode_cachep); |
| 9444 | kmem_cache_destroy(btrfs_trans_handle_cachep); |
| 9445 | kmem_cache_destroy(btrfs_transaction_cachep); |
| 9446 | kmem_cache_destroy(btrfs_path_cachep); |
| 9447 | kmem_cache_destroy(btrfs_free_space_cachep); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 9448 | } |
| 9449 | |
| 9450 | int btrfs_init_cachep(void) |
| 9451 | { |
David Sterba | 837e197 | 2012-09-07 03:00:48 -0600 | [diff] [blame] | 9452 | btrfs_inode_cachep = kmem_cache_create("btrfs_inode", |
Christoph Hellwig | 9601e3f | 2009-04-13 15:33:09 +0200 | [diff] [blame] | 9453 | sizeof(struct btrfs_inode), 0, |
Vladimir Davydov | 5d09705 | 2016-01-14 15:18:21 -0800 | [diff] [blame] | 9454 | SLAB_RECLAIM_ACCOUNT | SLAB_MEM_SPREAD | SLAB_ACCOUNT, |
| 9455 | init_once); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 9456 | if (!btrfs_inode_cachep) |
| 9457 | goto fail; |
Christoph Hellwig | 9601e3f | 2009-04-13 15:33:09 +0200 | [diff] [blame] | 9458 | |
David Sterba | 837e197 | 2012-09-07 03:00:48 -0600 | [diff] [blame] | 9459 | btrfs_trans_handle_cachep = kmem_cache_create("btrfs_trans_handle", |
Christoph Hellwig | 9601e3f | 2009-04-13 15:33:09 +0200 | [diff] [blame] | 9460 | sizeof(struct btrfs_trans_handle), 0, |
Nikolay Borisov | fba4b69 | 2016-06-23 21:17:08 +0300 | [diff] [blame] | 9461 | SLAB_TEMPORARY | SLAB_MEM_SPREAD, NULL); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 9462 | if (!btrfs_trans_handle_cachep) |
| 9463 | goto fail; |
Christoph Hellwig | 9601e3f | 2009-04-13 15:33:09 +0200 | [diff] [blame] | 9464 | |
David Sterba | 837e197 | 2012-09-07 03:00:48 -0600 | [diff] [blame] | 9465 | btrfs_transaction_cachep = kmem_cache_create("btrfs_transaction", |
Christoph Hellwig | 9601e3f | 2009-04-13 15:33:09 +0200 | [diff] [blame] | 9466 | sizeof(struct btrfs_transaction), 0, |
Nikolay Borisov | fba4b69 | 2016-06-23 21:17:08 +0300 | [diff] [blame] | 9467 | SLAB_TEMPORARY | SLAB_MEM_SPREAD, NULL); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 9468 | if (!btrfs_transaction_cachep) |
| 9469 | goto fail; |
Christoph Hellwig | 9601e3f | 2009-04-13 15:33:09 +0200 | [diff] [blame] | 9470 | |
David Sterba | 837e197 | 2012-09-07 03:00:48 -0600 | [diff] [blame] | 9471 | btrfs_path_cachep = kmem_cache_create("btrfs_path", |
Christoph Hellwig | 9601e3f | 2009-04-13 15:33:09 +0200 | [diff] [blame] | 9472 | sizeof(struct btrfs_path), 0, |
Nikolay Borisov | fba4b69 | 2016-06-23 21:17:08 +0300 | [diff] [blame] | 9473 | SLAB_MEM_SPREAD, NULL); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 9474 | if (!btrfs_path_cachep) |
| 9475 | goto fail; |
Christoph Hellwig | 9601e3f | 2009-04-13 15:33:09 +0200 | [diff] [blame] | 9476 | |
David Sterba | 837e197 | 2012-09-07 03:00:48 -0600 | [diff] [blame] | 9477 | btrfs_free_space_cachep = kmem_cache_create("btrfs_free_space", |
Josef Bacik | dc89e98 | 2011-01-28 17:05:48 -0500 | [diff] [blame] | 9478 | sizeof(struct btrfs_free_space), 0, |
Nikolay Borisov | fba4b69 | 2016-06-23 21:17:08 +0300 | [diff] [blame] | 9479 | SLAB_MEM_SPREAD, NULL); |
Josef Bacik | dc89e98 | 2011-01-28 17:05:48 -0500 | [diff] [blame] | 9480 | if (!btrfs_free_space_cachep) |
| 9481 | goto fail; |
| 9482 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 9483 | return 0; |
| 9484 | fail: |
| 9485 | btrfs_destroy_cachep(); |
| 9486 | return -ENOMEM; |
| 9487 | } |
| 9488 | |
| 9489 | static int btrfs_getattr(struct vfsmount *mnt, |
| 9490 | struct dentry *dentry, struct kstat *stat) |
| 9491 | { |
Miao Xie | df0af1a | 2013-01-29 10:11:59 +0000 | [diff] [blame] | 9492 | u64 delalloc_bytes; |
David Howells | 2b0143b | 2015-03-17 22:25:59 +0000 | [diff] [blame] | 9493 | struct inode *inode = d_inode(dentry); |
David Sterba | fadc0d8 | 2011-11-20 07:33:38 -0500 | [diff] [blame] | 9494 | u32 blocksize = inode->i_sb->s_blocksize; |
| 9495 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 9496 | generic_fillattr(inode, stat); |
Al Viro | 0ee5dc6 | 2011-07-07 15:44:25 -0400 | [diff] [blame] | 9497 | stat->dev = BTRFS_I(inode)->root->anon_dev; |
Miao Xie | df0af1a | 2013-01-29 10:11:59 +0000 | [diff] [blame] | 9498 | |
| 9499 | spin_lock(&BTRFS_I(inode)->lock); |
| 9500 | delalloc_bytes = BTRFS_I(inode)->delalloc_bytes; |
| 9501 | spin_unlock(&BTRFS_I(inode)->lock); |
David Sterba | fadc0d8 | 2011-11-20 07:33:38 -0500 | [diff] [blame] | 9502 | stat->blocks = (ALIGN(inode_get_bytes(inode), blocksize) + |
Miao Xie | df0af1a | 2013-01-29 10:11:59 +0000 | [diff] [blame] | 9503 | ALIGN(delalloc_bytes, blocksize)) >> 9; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 9504 | return 0; |
| 9505 | } |
| 9506 | |
Dan Fuhry | cdd1fed | 2016-03-17 15:23:38 +0100 | [diff] [blame] | 9507 | static int btrfs_rename_exchange(struct inode *old_dir, |
| 9508 | struct dentry *old_dentry, |
| 9509 | struct inode *new_dir, |
| 9510 | struct dentry *new_dentry) |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 9511 | { |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 9512 | struct btrfs_fs_info *fs_info = btrfs_sb(old_dir->i_sb); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 9513 | struct btrfs_trans_handle *trans; |
| 9514 | struct btrfs_root *root = BTRFS_I(old_dir)->root; |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 9515 | struct btrfs_root *dest = BTRFS_I(new_dir)->root; |
Dan Fuhry | cdd1fed | 2016-03-17 15:23:38 +0100 | [diff] [blame] | 9516 | struct inode *new_inode = new_dentry->d_inode; |
| 9517 | struct inode *old_inode = old_dentry->d_inode; |
Deepa Dinamani | c2050a4 | 2016-09-14 07:48:06 -0700 | [diff] [blame] | 9518 | struct timespec ctime = current_time(old_inode); |
Dan Fuhry | cdd1fed | 2016-03-17 15:23:38 +0100 | [diff] [blame] | 9519 | struct dentry *parent; |
Nikolay Borisov | 4a0cc7c | 2017-01-10 20:35:31 +0200 | [diff] [blame] | 9520 | u64 old_ino = btrfs_ino(BTRFS_I(old_inode)); |
| 9521 | u64 new_ino = btrfs_ino(BTRFS_I(new_inode)); |
Dan Fuhry | cdd1fed | 2016-03-17 15:23:38 +0100 | [diff] [blame] | 9522 | u64 old_idx = 0; |
| 9523 | u64 new_idx = 0; |
| 9524 | u64 root_objectid; |
| 9525 | int ret; |
Filipe Manana | 86e8aa0 | 2016-05-05 02:02:27 +0100 | [diff] [blame] | 9526 | bool root_log_pinned = false; |
| 9527 | bool dest_log_pinned = false; |
Dan Fuhry | cdd1fed | 2016-03-17 15:23:38 +0100 | [diff] [blame] | 9528 | |
| 9529 | /* we only allow rename subvolume link between subvolumes */ |
| 9530 | if (old_ino != BTRFS_FIRST_FREE_OBJECTID && root != dest) |
| 9531 | return -EXDEV; |
| 9532 | |
| 9533 | /* close the race window with snapshot create/destroy ioctl */ |
| 9534 | if (old_ino == BTRFS_FIRST_FREE_OBJECTID) |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 9535 | down_read(&fs_info->subvol_sem); |
Dan Fuhry | cdd1fed | 2016-03-17 15:23:38 +0100 | [diff] [blame] | 9536 | if (new_ino == BTRFS_FIRST_FREE_OBJECTID) |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 9537 | down_read(&fs_info->subvol_sem); |
Dan Fuhry | cdd1fed | 2016-03-17 15:23:38 +0100 | [diff] [blame] | 9538 | |
| 9539 | /* |
| 9540 | * We want to reserve the absolute worst case amount of items. So if |
| 9541 | * both inodes are subvols and we need to unlink them then that would |
| 9542 | * require 4 item modifications, but if they are both normal inodes it |
| 9543 | * would require 5 item modifications, so we'll assume their normal |
| 9544 | * inodes. So 5 * 2 is 10, plus 2 for the new links, so 12 total items |
| 9545 | * should cover the worst case number of items we'll modify. |
| 9546 | */ |
| 9547 | trans = btrfs_start_transaction(root, 12); |
| 9548 | if (IS_ERR(trans)) { |
| 9549 | ret = PTR_ERR(trans); |
| 9550 | goto out_notrans; |
| 9551 | } |
| 9552 | |
| 9553 | /* |
| 9554 | * We need to find a free sequence number both in the source and |
| 9555 | * in the destination directory for the exchange. |
| 9556 | */ |
| 9557 | ret = btrfs_set_inode_index(new_dir, &old_idx); |
| 9558 | if (ret) |
| 9559 | goto out_fail; |
| 9560 | ret = btrfs_set_inode_index(old_dir, &new_idx); |
| 9561 | if (ret) |
| 9562 | goto out_fail; |
| 9563 | |
| 9564 | BTRFS_I(old_inode)->dir_index = 0ULL; |
| 9565 | BTRFS_I(new_inode)->dir_index = 0ULL; |
| 9566 | |
| 9567 | /* Reference for the source. */ |
| 9568 | if (old_ino == BTRFS_FIRST_FREE_OBJECTID) { |
| 9569 | /* force full log commit if subvolume involved. */ |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 9570 | btrfs_set_log_full_commit(fs_info, trans); |
Dan Fuhry | cdd1fed | 2016-03-17 15:23:38 +0100 | [diff] [blame] | 9571 | } else { |
Filipe Manana | 376e5a5 | 2016-05-05 02:08:56 +0100 | [diff] [blame] | 9572 | btrfs_pin_log_trans(root); |
| 9573 | root_log_pinned = true; |
Dan Fuhry | cdd1fed | 2016-03-17 15:23:38 +0100 | [diff] [blame] | 9574 | ret = btrfs_insert_inode_ref(trans, dest, |
| 9575 | new_dentry->d_name.name, |
| 9576 | new_dentry->d_name.len, |
| 9577 | old_ino, |
David Sterba | f85b737 | 2017-01-20 14:54:07 +0100 | [diff] [blame] | 9578 | btrfs_ino(BTRFS_I(new_dir)), |
| 9579 | old_idx); |
Dan Fuhry | cdd1fed | 2016-03-17 15:23:38 +0100 | [diff] [blame] | 9580 | if (ret) |
| 9581 | goto out_fail; |
Dan Fuhry | cdd1fed | 2016-03-17 15:23:38 +0100 | [diff] [blame] | 9582 | } |
| 9583 | |
| 9584 | /* And now for the dest. */ |
| 9585 | if (new_ino == BTRFS_FIRST_FREE_OBJECTID) { |
| 9586 | /* force full log commit if subvolume involved. */ |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 9587 | btrfs_set_log_full_commit(fs_info, trans); |
Dan Fuhry | cdd1fed | 2016-03-17 15:23:38 +0100 | [diff] [blame] | 9588 | } else { |
Filipe Manana | 376e5a5 | 2016-05-05 02:08:56 +0100 | [diff] [blame] | 9589 | btrfs_pin_log_trans(dest); |
| 9590 | dest_log_pinned = true; |
Dan Fuhry | cdd1fed | 2016-03-17 15:23:38 +0100 | [diff] [blame] | 9591 | ret = btrfs_insert_inode_ref(trans, root, |
| 9592 | old_dentry->d_name.name, |
| 9593 | old_dentry->d_name.len, |
| 9594 | new_ino, |
David Sterba | f85b737 | 2017-01-20 14:54:07 +0100 | [diff] [blame] | 9595 | btrfs_ino(BTRFS_I(old_dir)), |
| 9596 | new_idx); |
Dan Fuhry | cdd1fed | 2016-03-17 15:23:38 +0100 | [diff] [blame] | 9597 | if (ret) |
| 9598 | goto out_fail; |
Dan Fuhry | cdd1fed | 2016-03-17 15:23:38 +0100 | [diff] [blame] | 9599 | } |
| 9600 | |
| 9601 | /* Update inode version and ctime/mtime. */ |
| 9602 | inode_inc_iversion(old_dir); |
| 9603 | inode_inc_iversion(new_dir); |
| 9604 | inode_inc_iversion(old_inode); |
| 9605 | inode_inc_iversion(new_inode); |
| 9606 | old_dir->i_ctime = old_dir->i_mtime = ctime; |
| 9607 | new_dir->i_ctime = new_dir->i_mtime = ctime; |
| 9608 | old_inode->i_ctime = ctime; |
| 9609 | new_inode->i_ctime = ctime; |
| 9610 | |
| 9611 | if (old_dentry->d_parent != new_dentry->d_parent) { |
David Sterba | f85b737 | 2017-01-20 14:54:07 +0100 | [diff] [blame] | 9612 | btrfs_record_unlink_dir(trans, BTRFS_I(old_dir), |
| 9613 | BTRFS_I(old_inode), 1); |
| 9614 | btrfs_record_unlink_dir(trans, BTRFS_I(new_dir), |
| 9615 | BTRFS_I(new_inode), 1); |
Dan Fuhry | cdd1fed | 2016-03-17 15:23:38 +0100 | [diff] [blame] | 9616 | } |
| 9617 | |
| 9618 | /* src is a subvolume */ |
| 9619 | if (old_ino == BTRFS_FIRST_FREE_OBJECTID) { |
| 9620 | root_objectid = BTRFS_I(old_inode)->root->root_key.objectid; |
| 9621 | ret = btrfs_unlink_subvol(trans, root, old_dir, |
| 9622 | root_objectid, |
| 9623 | old_dentry->d_name.name, |
| 9624 | old_dentry->d_name.len); |
| 9625 | } else { /* src is an inode */ |
Nikolay Borisov | 4ec5934 | 2017-01-18 00:31:44 +0200 | [diff] [blame] | 9626 | ret = __btrfs_unlink_inode(trans, root, BTRFS_I(old_dir), |
| 9627 | BTRFS_I(old_dentry->d_inode), |
Dan Fuhry | cdd1fed | 2016-03-17 15:23:38 +0100 | [diff] [blame] | 9628 | old_dentry->d_name.name, |
| 9629 | old_dentry->d_name.len); |
| 9630 | if (!ret) |
| 9631 | ret = btrfs_update_inode(trans, root, old_inode); |
| 9632 | } |
| 9633 | if (ret) { |
Jeff Mahoney | 6664283 | 2016-06-10 18:19:25 -0400 | [diff] [blame] | 9634 | btrfs_abort_transaction(trans, ret); |
Dan Fuhry | cdd1fed | 2016-03-17 15:23:38 +0100 | [diff] [blame] | 9635 | goto out_fail; |
| 9636 | } |
| 9637 | |
| 9638 | /* dest is a subvolume */ |
| 9639 | if (new_ino == BTRFS_FIRST_FREE_OBJECTID) { |
| 9640 | root_objectid = BTRFS_I(new_inode)->root->root_key.objectid; |
| 9641 | ret = btrfs_unlink_subvol(trans, dest, new_dir, |
| 9642 | root_objectid, |
| 9643 | new_dentry->d_name.name, |
| 9644 | new_dentry->d_name.len); |
| 9645 | } else { /* dest is an inode */ |
Nikolay Borisov | 4ec5934 | 2017-01-18 00:31:44 +0200 | [diff] [blame] | 9646 | ret = __btrfs_unlink_inode(trans, dest, BTRFS_I(new_dir), |
| 9647 | BTRFS_I(new_dentry->d_inode), |
Dan Fuhry | cdd1fed | 2016-03-17 15:23:38 +0100 | [diff] [blame] | 9648 | new_dentry->d_name.name, |
| 9649 | new_dentry->d_name.len); |
| 9650 | if (!ret) |
| 9651 | ret = btrfs_update_inode(trans, dest, new_inode); |
| 9652 | } |
| 9653 | if (ret) { |
Jeff Mahoney | 6664283 | 2016-06-10 18:19:25 -0400 | [diff] [blame] | 9654 | btrfs_abort_transaction(trans, ret); |
Dan Fuhry | cdd1fed | 2016-03-17 15:23:38 +0100 | [diff] [blame] | 9655 | goto out_fail; |
| 9656 | } |
| 9657 | |
| 9658 | ret = btrfs_add_link(trans, new_dir, old_inode, |
| 9659 | new_dentry->d_name.name, |
| 9660 | new_dentry->d_name.len, 0, old_idx); |
| 9661 | if (ret) { |
Jeff Mahoney | 6664283 | 2016-06-10 18:19:25 -0400 | [diff] [blame] | 9662 | btrfs_abort_transaction(trans, ret); |
Dan Fuhry | cdd1fed | 2016-03-17 15:23:38 +0100 | [diff] [blame] | 9663 | goto out_fail; |
| 9664 | } |
| 9665 | |
| 9666 | ret = btrfs_add_link(trans, old_dir, new_inode, |
| 9667 | old_dentry->d_name.name, |
| 9668 | old_dentry->d_name.len, 0, new_idx); |
| 9669 | if (ret) { |
Jeff Mahoney | 6664283 | 2016-06-10 18:19:25 -0400 | [diff] [blame] | 9670 | btrfs_abort_transaction(trans, ret); |
Dan Fuhry | cdd1fed | 2016-03-17 15:23:38 +0100 | [diff] [blame] | 9671 | goto out_fail; |
| 9672 | } |
| 9673 | |
| 9674 | if (old_inode->i_nlink == 1) |
| 9675 | BTRFS_I(old_inode)->dir_index = old_idx; |
| 9676 | if (new_inode->i_nlink == 1) |
| 9677 | BTRFS_I(new_inode)->dir_index = new_idx; |
| 9678 | |
Filipe Manana | 86e8aa0 | 2016-05-05 02:02:27 +0100 | [diff] [blame] | 9679 | if (root_log_pinned) { |
Dan Fuhry | cdd1fed | 2016-03-17 15:23:38 +0100 | [diff] [blame] | 9680 | parent = new_dentry->d_parent; |
David Sterba | f85b737 | 2017-01-20 14:54:07 +0100 | [diff] [blame] | 9681 | btrfs_log_new_name(trans, BTRFS_I(old_inode), BTRFS_I(old_dir), |
| 9682 | parent); |
Dan Fuhry | cdd1fed | 2016-03-17 15:23:38 +0100 | [diff] [blame] | 9683 | btrfs_end_log_trans(root); |
Filipe Manana | 86e8aa0 | 2016-05-05 02:02:27 +0100 | [diff] [blame] | 9684 | root_log_pinned = false; |
Dan Fuhry | cdd1fed | 2016-03-17 15:23:38 +0100 | [diff] [blame] | 9685 | } |
Filipe Manana | 86e8aa0 | 2016-05-05 02:02:27 +0100 | [diff] [blame] | 9686 | if (dest_log_pinned) { |
Dan Fuhry | cdd1fed | 2016-03-17 15:23:38 +0100 | [diff] [blame] | 9687 | parent = old_dentry->d_parent; |
David Sterba | f85b737 | 2017-01-20 14:54:07 +0100 | [diff] [blame] | 9688 | btrfs_log_new_name(trans, BTRFS_I(new_inode), BTRFS_I(new_dir), |
| 9689 | parent); |
Dan Fuhry | cdd1fed | 2016-03-17 15:23:38 +0100 | [diff] [blame] | 9690 | btrfs_end_log_trans(dest); |
Filipe Manana | 86e8aa0 | 2016-05-05 02:02:27 +0100 | [diff] [blame] | 9691 | dest_log_pinned = false; |
Dan Fuhry | cdd1fed | 2016-03-17 15:23:38 +0100 | [diff] [blame] | 9692 | } |
| 9693 | out_fail: |
Filipe Manana | 86e8aa0 | 2016-05-05 02:02:27 +0100 | [diff] [blame] | 9694 | /* |
| 9695 | * If we have pinned a log and an error happened, we unpin tasks |
| 9696 | * trying to sync the log and force them to fallback to a transaction |
| 9697 | * commit if the log currently contains any of the inodes involved in |
| 9698 | * this rename operation (to ensure we do not persist a log with an |
| 9699 | * inconsistent state for any of these inodes or leading to any |
| 9700 | * inconsistencies when replayed). If the transaction was aborted, the |
| 9701 | * abortion reason is propagated to userspace when attempting to commit |
| 9702 | * the transaction. If the log does not contain any of these inodes, we |
| 9703 | * allow the tasks to sync it. |
| 9704 | */ |
| 9705 | if (ret && (root_log_pinned || dest_log_pinned)) { |
Nikolay Borisov | 0f8939b | 2017-01-18 00:31:30 +0200 | [diff] [blame] | 9706 | if (btrfs_inode_in_log(BTRFS_I(old_dir), fs_info->generation) || |
| 9707 | btrfs_inode_in_log(BTRFS_I(new_dir), fs_info->generation) || |
| 9708 | btrfs_inode_in_log(BTRFS_I(old_inode), fs_info->generation) || |
Filipe Manana | 86e8aa0 | 2016-05-05 02:02:27 +0100 | [diff] [blame] | 9709 | (new_inode && |
Nikolay Borisov | 0f8939b | 2017-01-18 00:31:30 +0200 | [diff] [blame] | 9710 | btrfs_inode_in_log(BTRFS_I(new_inode), fs_info->generation))) |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 9711 | btrfs_set_log_full_commit(fs_info, trans); |
Filipe Manana | 86e8aa0 | 2016-05-05 02:02:27 +0100 | [diff] [blame] | 9712 | |
| 9713 | if (root_log_pinned) { |
| 9714 | btrfs_end_log_trans(root); |
| 9715 | root_log_pinned = false; |
| 9716 | } |
| 9717 | if (dest_log_pinned) { |
| 9718 | btrfs_end_log_trans(dest); |
| 9719 | dest_log_pinned = false; |
| 9720 | } |
| 9721 | } |
Jeff Mahoney | 3a45bb2 | 2016-09-09 21:39:03 -0400 | [diff] [blame] | 9722 | ret = btrfs_end_transaction(trans); |
Dan Fuhry | cdd1fed | 2016-03-17 15:23:38 +0100 | [diff] [blame] | 9723 | out_notrans: |
| 9724 | if (new_ino == BTRFS_FIRST_FREE_OBJECTID) |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 9725 | up_read(&fs_info->subvol_sem); |
Dan Fuhry | cdd1fed | 2016-03-17 15:23:38 +0100 | [diff] [blame] | 9726 | if (old_ino == BTRFS_FIRST_FREE_OBJECTID) |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 9727 | up_read(&fs_info->subvol_sem); |
Dan Fuhry | cdd1fed | 2016-03-17 15:23:38 +0100 | [diff] [blame] | 9728 | |
| 9729 | return ret; |
| 9730 | } |
| 9731 | |
| 9732 | static int btrfs_whiteout_for_rename(struct btrfs_trans_handle *trans, |
| 9733 | struct btrfs_root *root, |
| 9734 | struct inode *dir, |
| 9735 | struct dentry *dentry) |
| 9736 | { |
| 9737 | int ret; |
| 9738 | struct inode *inode; |
| 9739 | u64 objectid; |
| 9740 | u64 index; |
| 9741 | |
| 9742 | ret = btrfs_find_free_ino(root, &objectid); |
| 9743 | if (ret) |
| 9744 | return ret; |
| 9745 | |
| 9746 | inode = btrfs_new_inode(trans, root, dir, |
| 9747 | dentry->d_name.name, |
| 9748 | dentry->d_name.len, |
Nikolay Borisov | 4a0cc7c | 2017-01-10 20:35:31 +0200 | [diff] [blame] | 9749 | btrfs_ino(BTRFS_I(dir)), |
Dan Fuhry | cdd1fed | 2016-03-17 15:23:38 +0100 | [diff] [blame] | 9750 | objectid, |
| 9751 | S_IFCHR | WHITEOUT_MODE, |
| 9752 | &index); |
| 9753 | |
| 9754 | if (IS_ERR(inode)) { |
| 9755 | ret = PTR_ERR(inode); |
| 9756 | return ret; |
| 9757 | } |
| 9758 | |
| 9759 | inode->i_op = &btrfs_special_inode_operations; |
| 9760 | init_special_inode(inode, inode->i_mode, |
| 9761 | WHITEOUT_DEV); |
| 9762 | |
| 9763 | ret = btrfs_init_inode_security(trans, inode, dir, |
| 9764 | &dentry->d_name); |
| 9765 | if (ret) |
Filipe Manana | c990161 | 2016-05-05 01:41:57 +0100 | [diff] [blame] | 9766 | goto out; |
Dan Fuhry | cdd1fed | 2016-03-17 15:23:38 +0100 | [diff] [blame] | 9767 | |
| 9768 | ret = btrfs_add_nondir(trans, dir, dentry, |
| 9769 | inode, 0, index); |
| 9770 | if (ret) |
Filipe Manana | c990161 | 2016-05-05 01:41:57 +0100 | [diff] [blame] | 9771 | goto out; |
Dan Fuhry | cdd1fed | 2016-03-17 15:23:38 +0100 | [diff] [blame] | 9772 | |
| 9773 | ret = btrfs_update_inode(trans, root, inode); |
Filipe Manana | c990161 | 2016-05-05 01:41:57 +0100 | [diff] [blame] | 9774 | out: |
Dan Fuhry | cdd1fed | 2016-03-17 15:23:38 +0100 | [diff] [blame] | 9775 | unlock_new_inode(inode); |
Filipe Manana | c990161 | 2016-05-05 01:41:57 +0100 | [diff] [blame] | 9776 | if (ret) |
| 9777 | inode_dec_link_count(inode); |
Dan Fuhry | cdd1fed | 2016-03-17 15:23:38 +0100 | [diff] [blame] | 9778 | iput(inode); |
| 9779 | |
Filipe Manana | c990161 | 2016-05-05 01:41:57 +0100 | [diff] [blame] | 9780 | return ret; |
Dan Fuhry | cdd1fed | 2016-03-17 15:23:38 +0100 | [diff] [blame] | 9781 | } |
| 9782 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 9783 | static int btrfs_rename(struct inode *old_dir, struct dentry *old_dentry, |
Dan Fuhry | cdd1fed | 2016-03-17 15:23:38 +0100 | [diff] [blame] | 9784 | struct inode *new_dir, struct dentry *new_dentry, |
| 9785 | unsigned int flags) |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 9786 | { |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 9787 | struct btrfs_fs_info *fs_info = btrfs_sb(old_dir->i_sb); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 9788 | struct btrfs_trans_handle *trans; |
Filipe Manana | 5062af3 | 2016-05-05 10:26:26 +0100 | [diff] [blame] | 9789 | unsigned int trans_num_items; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 9790 | struct btrfs_root *root = BTRFS_I(old_dir)->root; |
| 9791 | struct btrfs_root *dest = BTRFS_I(new_dir)->root; |
David Howells | 2b0143b | 2015-03-17 22:25:59 +0000 | [diff] [blame] | 9792 | struct inode *new_inode = d_inode(new_dentry); |
| 9793 | struct inode *old_inode = d_inode(old_dentry); |
Chris Mason | 00e4e6b | 2008-08-05 11:18:09 -0400 | [diff] [blame] | 9794 | u64 index = 0; |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 9795 | u64 root_objectid; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 9796 | int ret; |
Nikolay Borisov | 4a0cc7c | 2017-01-10 20:35:31 +0200 | [diff] [blame] | 9797 | u64 old_ino = btrfs_ino(BTRFS_I(old_inode)); |
Filipe Manana | 3dc9e8f7 | 2016-04-29 11:34:22 +0100 | [diff] [blame] | 9798 | bool log_pinned = false; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 9799 | |
Nikolay Borisov | 4a0cc7c | 2017-01-10 20:35:31 +0200 | [diff] [blame] | 9800 | if (btrfs_ino(BTRFS_I(new_dir)) == BTRFS_EMPTY_SUBVOL_DIR_OBJECTID) |
Yan, Zheng | f679a84 | 2009-09-24 09:17:31 -0400 | [diff] [blame] | 9801 | return -EPERM; |
| 9802 | |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 9803 | /* we only allow rename subvolume link between subvolumes */ |
Li Zefan | 33345d01 | 2011-04-20 10:31:50 +0800 | [diff] [blame] | 9804 | if (old_ino != BTRFS_FIRST_FREE_OBJECTID && root != dest) |
Chris Mason | 3394e16 | 2008-11-17 20:42:26 -0500 | [diff] [blame] | 9805 | return -EXDEV; |
| 9806 | |
Li Zefan | 33345d01 | 2011-04-20 10:31:50 +0800 | [diff] [blame] | 9807 | if (old_ino == BTRFS_EMPTY_SUBVOL_DIR_OBJECTID || |
Nikolay Borisov | 4a0cc7c | 2017-01-10 20:35:31 +0200 | [diff] [blame] | 9808 | (new_inode && btrfs_ino(BTRFS_I(new_inode)) == BTRFS_FIRST_FREE_OBJECTID)) |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 9809 | return -ENOTEMPTY; |
| 9810 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 9811 | if (S_ISDIR(old_inode->i_mode) && new_inode && |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 9812 | new_inode->i_size > BTRFS_EMPTY_DIR_SIZE) |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 9813 | return -ENOTEMPTY; |
Chris Mason | 9c52057 | 2012-12-17 14:26:57 -0500 | [diff] [blame] | 9814 | |
| 9815 | |
| 9816 | /* check for collisions, even if the name isn't there */ |
Josef Bacik | 4871c15 | 2013-10-09 12:24:04 -0400 | [diff] [blame] | 9817 | ret = btrfs_check_dir_item_collision(dest, new_dir->i_ino, |
Chris Mason | 9c52057 | 2012-12-17 14:26:57 -0500 | [diff] [blame] | 9818 | new_dentry->d_name.name, |
| 9819 | new_dentry->d_name.len); |
| 9820 | |
| 9821 | if (ret) { |
| 9822 | if (ret == -EEXIST) { |
| 9823 | /* we shouldn't get |
| 9824 | * eexist without a new_inode */ |
Dulshani Gunawardhana | fae7f21 | 2013-10-31 10:30:08 +0530 | [diff] [blame] | 9825 | if (WARN_ON(!new_inode)) { |
Chris Mason | 9c52057 | 2012-12-17 14:26:57 -0500 | [diff] [blame] | 9826 | return ret; |
| 9827 | } |
| 9828 | } else { |
| 9829 | /* maybe -EOVERFLOW */ |
| 9830 | return ret; |
| 9831 | } |
| 9832 | } |
| 9833 | ret = 0; |
| 9834 | |
Chris Mason | 5a3f23d | 2009-03-31 13:27:11 -0400 | [diff] [blame] | 9835 | /* |
Chris Mason | 8d875f9 | 2014-08-12 10:47:42 -0700 | [diff] [blame] | 9836 | * we're using rename to replace one file with another. Start IO on it |
| 9837 | * 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] | 9838 | */ |
Chris Mason | 8d875f9 | 2014-08-12 10:47:42 -0700 | [diff] [blame] | 9839 | 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] | 9840 | filemap_flush(old_inode->i_mapping); |
| 9841 | |
Yan, Zheng | 76dda93 | 2009-09-21 16:00:26 -0400 | [diff] [blame] | 9842 | /* close the racy window with snapshot create/destroy ioctl */ |
Li Zefan | 33345d01 | 2011-04-20 10:31:50 +0800 | [diff] [blame] | 9843 | if (old_ino == BTRFS_FIRST_FREE_OBJECTID) |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 9844 | down_read(&fs_info->subvol_sem); |
Yan, Zheng | a22285a | 2010-05-16 10:48:46 -0400 | [diff] [blame] | 9845 | /* |
| 9846 | * We want to reserve the absolute worst case amount of items. So if |
| 9847 | * both inodes are subvols and we need to unlink them then that would |
| 9848 | * require 4 item modifications, but if they are both normal inodes it |
Dan Fuhry | cdd1fed | 2016-03-17 15:23:38 +0100 | [diff] [blame] | 9849 | * would require 5 item modifications, so we'll assume they are normal |
Yan, Zheng | a22285a | 2010-05-16 10:48:46 -0400 | [diff] [blame] | 9850 | * inodes. So 5 * 2 is 10, plus 1 for the new link, so 11 total items |
| 9851 | * should cover the worst case number of items we'll modify. |
Filipe Manana | 5062af3 | 2016-05-05 10:26:26 +0100 | [diff] [blame] | 9852 | * If our rename has the whiteout flag, we need more 5 units for the |
| 9853 | * new inode (1 inode item, 1 inode ref, 2 dir items and 1 xattr item |
| 9854 | * when selinux is enabled). |
Yan, Zheng | a22285a | 2010-05-16 10:48:46 -0400 | [diff] [blame] | 9855 | */ |
Filipe Manana | 5062af3 | 2016-05-05 10:26:26 +0100 | [diff] [blame] | 9856 | trans_num_items = 11; |
| 9857 | if (flags & RENAME_WHITEOUT) |
| 9858 | trans_num_items += 5; |
| 9859 | trans = btrfs_start_transaction(root, trans_num_items); |
Johann Lombardi | b44c59a | 2011-03-31 13:23:47 +0000 | [diff] [blame] | 9860 | if (IS_ERR(trans)) { |
Dan Fuhry | cdd1fed | 2016-03-17 15:23:38 +0100 | [diff] [blame] | 9861 | ret = PTR_ERR(trans); |
| 9862 | goto out_notrans; |
| 9863 | } |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 9864 | |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 9865 | if (dest != root) |
| 9866 | btrfs_record_root_in_trans(trans, dest); |
| 9867 | |
Yan, Zheng | a571952 | 2009-09-24 09:17:31 -0400 | [diff] [blame] | 9868 | ret = btrfs_set_inode_index(new_dir, &index); |
| 9869 | if (ret) |
| 9870 | goto out_fail; |
Chris Mason | 5a3f23d | 2009-03-31 13:27:11 -0400 | [diff] [blame] | 9871 | |
Miao Xie | 67de117 | 2013-12-26 13:07:06 +0800 | [diff] [blame] | 9872 | BTRFS_I(old_inode)->dir_index = 0ULL; |
Li Zefan | 33345d01 | 2011-04-20 10:31:50 +0800 | [diff] [blame] | 9873 | if (unlikely(old_ino == BTRFS_FIRST_FREE_OBJECTID)) { |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 9874 | /* force full log commit if subvolume involved. */ |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 9875 | btrfs_set_log_full_commit(fs_info, trans); |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 9876 | } else { |
Filipe Manana | c4aba95 | 2016-04-29 13:14:42 +0100 | [diff] [blame] | 9877 | btrfs_pin_log_trans(root); |
| 9878 | log_pinned = true; |
Yan, Zheng | a571952 | 2009-09-24 09:17:31 -0400 | [diff] [blame] | 9879 | ret = btrfs_insert_inode_ref(trans, dest, |
| 9880 | new_dentry->d_name.name, |
| 9881 | new_dentry->d_name.len, |
Li Zefan | 33345d01 | 2011-04-20 10:31:50 +0800 | [diff] [blame] | 9882 | old_ino, |
Nikolay Borisov | 4a0cc7c | 2017-01-10 20:35:31 +0200 | [diff] [blame] | 9883 | btrfs_ino(BTRFS_I(new_dir)), index); |
Yan, Zheng | a571952 | 2009-09-24 09:17:31 -0400 | [diff] [blame] | 9884 | if (ret) |
| 9885 | goto out_fail; |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 9886 | } |
Chris Mason | 5a3f23d | 2009-03-31 13:27:11 -0400 | [diff] [blame] | 9887 | |
Josef Bacik | 0c4d2d9 | 2012-04-05 15:03:02 -0400 | [diff] [blame] | 9888 | inode_inc_iversion(old_dir); |
| 9889 | inode_inc_iversion(new_dir); |
| 9890 | inode_inc_iversion(old_inode); |
Deepa Dinamani | 04b285f | 2016-02-06 23:57:21 -0800 | [diff] [blame] | 9891 | old_dir->i_ctime = old_dir->i_mtime = |
| 9892 | new_dir->i_ctime = new_dir->i_mtime = |
Deepa Dinamani | c2050a4 | 2016-09-14 07:48:06 -0700 | [diff] [blame] | 9893 | old_inode->i_ctime = current_time(old_dir); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 9894 | |
Chris Mason | 12fcfd2 | 2009-03-24 10:24:20 -0400 | [diff] [blame] | 9895 | if (old_dentry->d_parent != new_dentry->d_parent) |
David Sterba | f85b737 | 2017-01-20 14:54:07 +0100 | [diff] [blame] | 9896 | btrfs_record_unlink_dir(trans, BTRFS_I(old_dir), |
| 9897 | BTRFS_I(old_inode), 1); |
Chris Mason | 12fcfd2 | 2009-03-24 10:24:20 -0400 | [diff] [blame] | 9898 | |
Li Zefan | 33345d01 | 2011-04-20 10:31:50 +0800 | [diff] [blame] | 9899 | if (unlikely(old_ino == BTRFS_FIRST_FREE_OBJECTID)) { |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 9900 | root_objectid = BTRFS_I(old_inode)->root->root_key.objectid; |
| 9901 | ret = btrfs_unlink_subvol(trans, root, old_dir, root_objectid, |
| 9902 | old_dentry->d_name.name, |
| 9903 | old_dentry->d_name.len); |
| 9904 | } else { |
Nikolay Borisov | 4ec5934 | 2017-01-18 00:31:44 +0200 | [diff] [blame] | 9905 | ret = __btrfs_unlink_inode(trans, root, BTRFS_I(old_dir), |
| 9906 | BTRFS_I(d_inode(old_dentry)), |
Al Viro | 9298679 | 2011-03-04 17:14:37 +0000 | [diff] [blame] | 9907 | old_dentry->d_name.name, |
| 9908 | old_dentry->d_name.len); |
| 9909 | if (!ret) |
| 9910 | ret = btrfs_update_inode(trans, root, old_inode); |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 9911 | } |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 9912 | if (ret) { |
Jeff Mahoney | 6664283 | 2016-06-10 18:19:25 -0400 | [diff] [blame] | 9913 | btrfs_abort_transaction(trans, ret); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 9914 | goto out_fail; |
| 9915 | } |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 9916 | |
| 9917 | if (new_inode) { |
Josef Bacik | 0c4d2d9 | 2012-04-05 15:03:02 -0400 | [diff] [blame] | 9918 | inode_inc_iversion(new_inode); |
Deepa Dinamani | c2050a4 | 2016-09-14 07:48:06 -0700 | [diff] [blame] | 9919 | new_inode->i_ctime = current_time(new_inode); |
Nikolay Borisov | 4a0cc7c | 2017-01-10 20:35:31 +0200 | [diff] [blame] | 9920 | if (unlikely(btrfs_ino(BTRFS_I(new_inode)) == |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 9921 | BTRFS_EMPTY_SUBVOL_DIR_OBJECTID)) { |
| 9922 | root_objectid = BTRFS_I(new_inode)->location.objectid; |
| 9923 | ret = btrfs_unlink_subvol(trans, dest, new_dir, |
| 9924 | root_objectid, |
| 9925 | new_dentry->d_name.name, |
| 9926 | new_dentry->d_name.len); |
| 9927 | BUG_ON(new_inode->i_nlink == 0); |
| 9928 | } else { |
Nikolay Borisov | 4ec5934 | 2017-01-18 00:31:44 +0200 | [diff] [blame] | 9929 | ret = btrfs_unlink_inode(trans, dest, BTRFS_I(new_dir), |
| 9930 | BTRFS_I(d_inode(new_dentry)), |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 9931 | new_dentry->d_name.name, |
| 9932 | new_dentry->d_name.len); |
| 9933 | } |
Josef Bacik | 4ef31a4 | 2013-08-13 14:10:08 -0400 | [diff] [blame] | 9934 | if (!ret && new_inode->i_nlink == 0) |
David Howells | 2b0143b | 2015-03-17 22:25:59 +0000 | [diff] [blame] | 9935 | ret = btrfs_orphan_add(trans, d_inode(new_dentry)); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 9936 | if (ret) { |
Jeff Mahoney | 6664283 | 2016-06-10 18:19:25 -0400 | [diff] [blame] | 9937 | btrfs_abort_transaction(trans, ret); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 9938 | goto out_fail; |
| 9939 | } |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 9940 | } |
Josef Bacik | aec7477 | 2008-07-24 12:12:38 -0400 | [diff] [blame] | 9941 | |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 9942 | ret = btrfs_add_link(trans, new_dir, old_inode, |
| 9943 | new_dentry->d_name.name, |
Yan, Zheng | a571952 | 2009-09-24 09:17:31 -0400 | [diff] [blame] | 9944 | new_dentry->d_name.len, 0, index); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 9945 | if (ret) { |
Jeff Mahoney | 6664283 | 2016-06-10 18:19:25 -0400 | [diff] [blame] | 9946 | btrfs_abort_transaction(trans, ret); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 9947 | goto out_fail; |
| 9948 | } |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 9949 | |
Miao Xie | 67de117 | 2013-12-26 13:07:06 +0800 | [diff] [blame] | 9950 | if (old_inode->i_nlink == 1) |
| 9951 | BTRFS_I(old_inode)->dir_index = index; |
| 9952 | |
Filipe Manana | 3dc9e8f7 | 2016-04-29 11:34:22 +0100 | [diff] [blame] | 9953 | if (log_pinned) { |
Al Viro | 10d9f30 | 2011-07-16 23:09:10 -0400 | [diff] [blame] | 9954 | struct dentry *parent = new_dentry->d_parent; |
Filipe Manana | 3dc9e8f7 | 2016-04-29 11:34:22 +0100 | [diff] [blame] | 9955 | |
David Sterba | f85b737 | 2017-01-20 14:54:07 +0100 | [diff] [blame] | 9956 | btrfs_log_new_name(trans, BTRFS_I(old_inode), BTRFS_I(old_dir), |
| 9957 | parent); |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 9958 | btrfs_end_log_trans(root); |
Filipe Manana | 3dc9e8f7 | 2016-04-29 11:34:22 +0100 | [diff] [blame] | 9959 | log_pinned = false; |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 9960 | } |
Dan Fuhry | cdd1fed | 2016-03-17 15:23:38 +0100 | [diff] [blame] | 9961 | |
| 9962 | if (flags & RENAME_WHITEOUT) { |
| 9963 | ret = btrfs_whiteout_for_rename(trans, root, old_dir, |
| 9964 | old_dentry); |
| 9965 | |
| 9966 | if (ret) { |
Jeff Mahoney | 6664283 | 2016-06-10 18:19:25 -0400 | [diff] [blame] | 9967 | btrfs_abort_transaction(trans, ret); |
Dan Fuhry | cdd1fed | 2016-03-17 15:23:38 +0100 | [diff] [blame] | 9968 | goto out_fail; |
| 9969 | } |
Chris Mason | 12fcfd2 | 2009-03-24 10:24:20 -0400 | [diff] [blame] | 9970 | } |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 9971 | out_fail: |
Filipe Manana | 3dc9e8f7 | 2016-04-29 11:34:22 +0100 | [diff] [blame] | 9972 | /* |
| 9973 | * If we have pinned the log and an error happened, we unpin tasks |
| 9974 | * trying to sync the log and force them to fallback to a transaction |
| 9975 | * commit if the log currently contains any of the inodes involved in |
| 9976 | * this rename operation (to ensure we do not persist a log with an |
| 9977 | * inconsistent state for any of these inodes or leading to any |
| 9978 | * inconsistencies when replayed). If the transaction was aborted, the |
| 9979 | * abortion reason is propagated to userspace when attempting to commit |
| 9980 | * the transaction. If the log does not contain any of these inodes, we |
| 9981 | * allow the tasks to sync it. |
| 9982 | */ |
| 9983 | if (ret && log_pinned) { |
Nikolay Borisov | 0f8939b | 2017-01-18 00:31:30 +0200 | [diff] [blame] | 9984 | if (btrfs_inode_in_log(BTRFS_I(old_dir), fs_info->generation) || |
| 9985 | btrfs_inode_in_log(BTRFS_I(new_dir), fs_info->generation) || |
| 9986 | btrfs_inode_in_log(BTRFS_I(old_inode), fs_info->generation) || |
Filipe Manana | 3dc9e8f7 | 2016-04-29 11:34:22 +0100 | [diff] [blame] | 9987 | (new_inode && |
Nikolay Borisov | 0f8939b | 2017-01-18 00:31:30 +0200 | [diff] [blame] | 9988 | btrfs_inode_in_log(BTRFS_I(new_inode), fs_info->generation))) |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 9989 | btrfs_set_log_full_commit(fs_info, trans); |
Filipe Manana | 3dc9e8f7 | 2016-04-29 11:34:22 +0100 | [diff] [blame] | 9990 | |
| 9991 | btrfs_end_log_trans(root); |
| 9992 | log_pinned = false; |
| 9993 | } |
Jeff Mahoney | 3a45bb2 | 2016-09-09 21:39:03 -0400 | [diff] [blame] | 9994 | btrfs_end_transaction(trans); |
Johann Lombardi | b44c59a | 2011-03-31 13:23:47 +0000 | [diff] [blame] | 9995 | out_notrans: |
Li Zefan | 33345d01 | 2011-04-20 10:31:50 +0800 | [diff] [blame] | 9996 | if (old_ino == BTRFS_FIRST_FREE_OBJECTID) |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 9997 | up_read(&fs_info->subvol_sem); |
Josef Bacik | 9ed74f2 | 2009-09-11 16:12:44 -0400 | [diff] [blame] | 9998 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 9999 | return ret; |
| 10000 | } |
| 10001 | |
Miklos Szeredi | 80ace85 | 2014-07-23 15:15:32 +0200 | [diff] [blame] | 10002 | static int btrfs_rename2(struct inode *old_dir, struct dentry *old_dentry, |
| 10003 | struct inode *new_dir, struct dentry *new_dentry, |
| 10004 | unsigned int flags) |
| 10005 | { |
Dan Fuhry | cdd1fed | 2016-03-17 15:23:38 +0100 | [diff] [blame] | 10006 | if (flags & ~(RENAME_NOREPLACE | RENAME_EXCHANGE | RENAME_WHITEOUT)) |
Miklos Szeredi | 80ace85 | 2014-07-23 15:15:32 +0200 | [diff] [blame] | 10007 | return -EINVAL; |
| 10008 | |
Dan Fuhry | cdd1fed | 2016-03-17 15:23:38 +0100 | [diff] [blame] | 10009 | if (flags & RENAME_EXCHANGE) |
| 10010 | return btrfs_rename_exchange(old_dir, old_dentry, new_dir, |
| 10011 | new_dentry); |
| 10012 | |
| 10013 | return btrfs_rename(old_dir, old_dentry, new_dir, new_dentry, flags); |
Miklos Szeredi | 80ace85 | 2014-07-23 15:15:32 +0200 | [diff] [blame] | 10014 | } |
| 10015 | |
Miao Xie | 8ccf6f19 | 2012-10-25 09:28:04 +0000 | [diff] [blame] | 10016 | static void btrfs_run_delalloc_work(struct btrfs_work *work) |
| 10017 | { |
| 10018 | struct btrfs_delalloc_work *delalloc_work; |
Josef Bacik | 9f23e28 | 2013-10-28 15:03:41 -0400 | [diff] [blame] | 10019 | struct inode *inode; |
Miao Xie | 8ccf6f19 | 2012-10-25 09:28:04 +0000 | [diff] [blame] | 10020 | |
| 10021 | delalloc_work = container_of(work, struct btrfs_delalloc_work, |
| 10022 | work); |
Josef Bacik | 9f23e28 | 2013-10-28 15:03:41 -0400 | [diff] [blame] | 10023 | inode = delalloc_work->inode; |
David Sterba | 3042460 | 2015-11-27 19:27:11 +0100 | [diff] [blame] | 10024 | filemap_flush(inode->i_mapping); |
| 10025 | if (test_bit(BTRFS_INODE_HAS_ASYNC_EXTENT, |
| 10026 | &BTRFS_I(inode)->runtime_flags)) |
Josef Bacik | 9f23e28 | 2013-10-28 15:03:41 -0400 | [diff] [blame] | 10027 | filemap_flush(inode->i_mapping); |
Miao Xie | 8ccf6f19 | 2012-10-25 09:28:04 +0000 | [diff] [blame] | 10028 | |
| 10029 | if (delalloc_work->delay_iput) |
Josef Bacik | 9f23e28 | 2013-10-28 15:03:41 -0400 | [diff] [blame] | 10030 | btrfs_add_delayed_iput(inode); |
Miao Xie | 8ccf6f19 | 2012-10-25 09:28:04 +0000 | [diff] [blame] | 10031 | else |
Josef Bacik | 9f23e28 | 2013-10-28 15:03:41 -0400 | [diff] [blame] | 10032 | iput(inode); |
Miao Xie | 8ccf6f19 | 2012-10-25 09:28:04 +0000 | [diff] [blame] | 10033 | complete(&delalloc_work->completion); |
| 10034 | } |
| 10035 | |
| 10036 | struct btrfs_delalloc_work *btrfs_alloc_delalloc_work(struct inode *inode, |
David Sterba | 651d494 | 2015-11-27 19:24:16 +0100 | [diff] [blame] | 10037 | int delay_iput) |
Miao Xie | 8ccf6f19 | 2012-10-25 09:28:04 +0000 | [diff] [blame] | 10038 | { |
| 10039 | struct btrfs_delalloc_work *work; |
| 10040 | |
David Sterba | 100d570 | 2015-12-08 14:39:32 +0100 | [diff] [blame] | 10041 | work = kmalloc(sizeof(*work), GFP_NOFS); |
Miao Xie | 8ccf6f19 | 2012-10-25 09:28:04 +0000 | [diff] [blame] | 10042 | if (!work) |
| 10043 | return NULL; |
| 10044 | |
| 10045 | init_completion(&work->completion); |
| 10046 | INIT_LIST_HEAD(&work->list); |
| 10047 | work->inode = inode; |
Miao Xie | 8ccf6f19 | 2012-10-25 09:28:04 +0000 | [diff] [blame] | 10048 | work->delay_iput = delay_iput; |
Liu Bo | 9e0af23 | 2014-08-15 23:36:53 +0800 | [diff] [blame] | 10049 | WARN_ON_ONCE(!inode); |
| 10050 | btrfs_init_work(&work->work, btrfs_flush_delalloc_helper, |
| 10051 | btrfs_run_delalloc_work, NULL, NULL); |
Miao Xie | 8ccf6f19 | 2012-10-25 09:28:04 +0000 | [diff] [blame] | 10052 | |
| 10053 | return work; |
| 10054 | } |
| 10055 | |
| 10056 | void btrfs_wait_and_free_delalloc_work(struct btrfs_delalloc_work *work) |
| 10057 | { |
| 10058 | wait_for_completion(&work->completion); |
David Sterba | 100d570 | 2015-12-08 14:39:32 +0100 | [diff] [blame] | 10059 | kfree(work); |
Miao Xie | 8ccf6f19 | 2012-10-25 09:28:04 +0000 | [diff] [blame] | 10060 | } |
| 10061 | |
Chris Mason | d352ac6 | 2008-09-29 15:18:18 -0400 | [diff] [blame] | 10062 | /* |
| 10063 | * some fairly slow code that needs optimization. This walks the list |
| 10064 | * of all the inodes with pending delalloc and forces them to disk. |
| 10065 | */ |
Miao Xie | 6c255e6 | 2014-03-06 13:55:01 +0800 | [diff] [blame] | 10066 | static int __start_delalloc_inodes(struct btrfs_root *root, int delay_iput, |
| 10067 | int nr) |
Chris Mason | ea8c281 | 2008-08-04 23:17:27 -0400 | [diff] [blame] | 10068 | { |
Chris Mason | ea8c281 | 2008-08-04 23:17:27 -0400 | [diff] [blame] | 10069 | struct btrfs_inode *binode; |
Zheng Yan | 5b21f2e | 2008-09-26 10:05:38 -0400 | [diff] [blame] | 10070 | struct inode *inode; |
Miao Xie | 8ccf6f19 | 2012-10-25 09:28:04 +0000 | [diff] [blame] | 10071 | struct btrfs_delalloc_work *work, *next; |
| 10072 | struct list_head works; |
Miao Xie | 1eafa6c | 2013-01-22 10:49:00 +0000 | [diff] [blame] | 10073 | struct list_head splice; |
Miao Xie | 8ccf6f19 | 2012-10-25 09:28:04 +0000 | [diff] [blame] | 10074 | int ret = 0; |
Chris Mason | ea8c281 | 2008-08-04 23:17:27 -0400 | [diff] [blame] | 10075 | |
Miao Xie | 8ccf6f19 | 2012-10-25 09:28:04 +0000 | [diff] [blame] | 10076 | INIT_LIST_HEAD(&works); |
Miao Xie | 1eafa6c | 2013-01-22 10:49:00 +0000 | [diff] [blame] | 10077 | INIT_LIST_HEAD(&splice); |
Miao Xie | 63607cc | 2013-01-22 10:50:35 +0000 | [diff] [blame] | 10078 | |
Miao Xie | 573bfb7 | 2014-03-06 13:55:03 +0800 | [diff] [blame] | 10079 | mutex_lock(&root->delalloc_mutex); |
Miao Xie | eb73c1b | 2013-05-15 07:48:22 +0000 | [diff] [blame] | 10080 | spin_lock(&root->delalloc_lock); |
| 10081 | list_splice_init(&root->delalloc_inodes, &splice); |
Miao Xie | 1eafa6c | 2013-01-22 10:49:00 +0000 | [diff] [blame] | 10082 | while (!list_empty(&splice)) { |
| 10083 | binode = list_entry(splice.next, struct btrfs_inode, |
Chris Mason | ea8c281 | 2008-08-04 23:17:27 -0400 | [diff] [blame] | 10084 | delalloc_inodes); |
Miao Xie | 1eafa6c | 2013-01-22 10:49:00 +0000 | [diff] [blame] | 10085 | |
Miao Xie | eb73c1b | 2013-05-15 07:48:22 +0000 | [diff] [blame] | 10086 | list_move_tail(&binode->delalloc_inodes, |
| 10087 | &root->delalloc_inodes); |
Zheng Yan | 5b21f2e | 2008-09-26 10:05:38 -0400 | [diff] [blame] | 10088 | inode = igrab(&binode->vfs_inode); |
Miao Xie | df0af1a | 2013-01-29 10:11:59 +0000 | [diff] [blame] | 10089 | if (!inode) { |
Miao Xie | eb73c1b | 2013-05-15 07:48:22 +0000 | [diff] [blame] | 10090 | cond_resched_lock(&root->delalloc_lock); |
Miao Xie | 1eafa6c | 2013-01-22 10:49:00 +0000 | [diff] [blame] | 10091 | continue; |
Miao Xie | df0af1a | 2013-01-29 10:11:59 +0000 | [diff] [blame] | 10092 | } |
Miao Xie | eb73c1b | 2013-05-15 07:48:22 +0000 | [diff] [blame] | 10093 | spin_unlock(&root->delalloc_lock); |
Miao Xie | 1eafa6c | 2013-01-22 10:49:00 +0000 | [diff] [blame] | 10094 | |
David Sterba | 651d494 | 2015-11-27 19:24:16 +0100 | [diff] [blame] | 10095 | work = btrfs_alloc_delalloc_work(inode, delay_iput); |
David Sterba | 5d99a998 | 2014-09-29 19:20:37 +0200 | [diff] [blame] | 10096 | if (!work) { |
Josef Bacik | f4ab9ea | 2013-09-17 11:25:44 -0400 | [diff] [blame] | 10097 | if (delay_iput) |
| 10098 | btrfs_add_delayed_iput(inode); |
| 10099 | else |
| 10100 | iput(inode); |
Miao Xie | 1eafa6c | 2013-01-22 10:49:00 +0000 | [diff] [blame] | 10101 | ret = -ENOMEM; |
Wang Shilong | a1ecaab | 2014-04-02 19:53:32 +0800 | [diff] [blame] | 10102 | goto out; |
Zheng Yan | 5b21f2e | 2008-09-26 10:05:38 -0400 | [diff] [blame] | 10103 | } |
Miao Xie | 1eafa6c | 2013-01-22 10:49:00 +0000 | [diff] [blame] | 10104 | list_add_tail(&work->list, &works); |
Qu Wenruo | a44903a | 2014-02-28 10:46:09 +0800 | [diff] [blame] | 10105 | btrfs_queue_work(root->fs_info->flush_workers, |
| 10106 | &work->work); |
Miao Xie | 6c255e6 | 2014-03-06 13:55:01 +0800 | [diff] [blame] | 10107 | ret++; |
| 10108 | if (nr != -1 && ret >= nr) |
Wang Shilong | a1ecaab | 2014-04-02 19:53:32 +0800 | [diff] [blame] | 10109 | goto out; |
Zheng Yan | 5b21f2e | 2008-09-26 10:05:38 -0400 | [diff] [blame] | 10110 | cond_resched(); |
Miao Xie | eb73c1b | 2013-05-15 07:48:22 +0000 | [diff] [blame] | 10111 | spin_lock(&root->delalloc_lock); |
Chris Mason | ea8c281 | 2008-08-04 23:17:27 -0400 | [diff] [blame] | 10112 | } |
Miao Xie | eb73c1b | 2013-05-15 07:48:22 +0000 | [diff] [blame] | 10113 | spin_unlock(&root->delalloc_lock); |
Chris Mason | 8c8bee1 | 2008-09-29 11:19:10 -0400 | [diff] [blame] | 10114 | |
Wang Shilong | a1ecaab | 2014-04-02 19:53:32 +0800 | [diff] [blame] | 10115 | out: |
Miao Xie | 1eafa6c | 2013-01-22 10:49:00 +0000 | [diff] [blame] | 10116 | list_for_each_entry_safe(work, next, &works, list) { |
| 10117 | list_del_init(&work->list); |
| 10118 | btrfs_wait_and_free_delalloc_work(work); |
| 10119 | } |
| 10120 | |
Miao Xie | eb73c1b | 2013-05-15 07:48:22 +0000 | [diff] [blame] | 10121 | if (!list_empty_careful(&splice)) { |
| 10122 | spin_lock(&root->delalloc_lock); |
| 10123 | list_splice_tail(&splice, &root->delalloc_inodes); |
| 10124 | spin_unlock(&root->delalloc_lock); |
| 10125 | } |
Miao Xie | 573bfb7 | 2014-03-06 13:55:03 +0800 | [diff] [blame] | 10126 | mutex_unlock(&root->delalloc_mutex); |
Miao Xie | eb73c1b | 2013-05-15 07:48:22 +0000 | [diff] [blame] | 10127 | return ret; |
| 10128 | } |
| 10129 | |
| 10130 | int btrfs_start_delalloc_inodes(struct btrfs_root *root, int delay_iput) |
| 10131 | { |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 10132 | struct btrfs_fs_info *fs_info = root->fs_info; |
Miao Xie | eb73c1b | 2013-05-15 07:48:22 +0000 | [diff] [blame] | 10133 | int ret; |
| 10134 | |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 10135 | if (test_bit(BTRFS_FS_STATE_ERROR, &fs_info->fs_state)) |
Miao Xie | eb73c1b | 2013-05-15 07:48:22 +0000 | [diff] [blame] | 10136 | return -EROFS; |
| 10137 | |
Miao Xie | 6c255e6 | 2014-03-06 13:55:01 +0800 | [diff] [blame] | 10138 | ret = __start_delalloc_inodes(root, delay_iput, -1); |
| 10139 | if (ret > 0) |
| 10140 | ret = 0; |
Miao Xie | eb73c1b | 2013-05-15 07:48:22 +0000 | [diff] [blame] | 10141 | /* |
| 10142 | * the filemap_flush will queue IO into the worker threads, but |
Chris Mason | 8c8bee1 | 2008-09-29 11:19:10 -0400 | [diff] [blame] | 10143 | * we have to make sure the IO is actually started and that |
| 10144 | * ordered extents get created before we return |
| 10145 | */ |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 10146 | atomic_inc(&fs_info->async_submit_draining); |
| 10147 | while (atomic_read(&fs_info->nr_async_submits) || |
| 10148 | atomic_read(&fs_info->async_delalloc_pages)) { |
| 10149 | wait_event(fs_info->async_submit_wait, |
| 10150 | (atomic_read(&fs_info->nr_async_submits) == 0 && |
| 10151 | atomic_read(&fs_info->async_delalloc_pages) == 0)); |
Chris Mason | 8c8bee1 | 2008-09-29 11:19:10 -0400 | [diff] [blame] | 10152 | } |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 10153 | atomic_dec(&fs_info->async_submit_draining); |
Miao Xie | eb73c1b | 2013-05-15 07:48:22 +0000 | [diff] [blame] | 10154 | return ret; |
| 10155 | } |
| 10156 | |
Miao Xie | 6c255e6 | 2014-03-06 13:55:01 +0800 | [diff] [blame] | 10157 | int btrfs_start_delalloc_roots(struct btrfs_fs_info *fs_info, int delay_iput, |
| 10158 | int nr) |
Miao Xie | eb73c1b | 2013-05-15 07:48:22 +0000 | [diff] [blame] | 10159 | { |
| 10160 | struct btrfs_root *root; |
| 10161 | struct list_head splice; |
| 10162 | int ret; |
| 10163 | |
Wang Shilong | 2c21b4d | 2014-01-14 19:42:20 +0800 | [diff] [blame] | 10164 | if (test_bit(BTRFS_FS_STATE_ERROR, &fs_info->fs_state)) |
Miao Xie | eb73c1b | 2013-05-15 07:48:22 +0000 | [diff] [blame] | 10165 | return -EROFS; |
| 10166 | |
| 10167 | INIT_LIST_HEAD(&splice); |
| 10168 | |
Miao Xie | 573bfb7 | 2014-03-06 13:55:03 +0800 | [diff] [blame] | 10169 | mutex_lock(&fs_info->delalloc_root_mutex); |
Miao Xie | eb73c1b | 2013-05-15 07:48:22 +0000 | [diff] [blame] | 10170 | spin_lock(&fs_info->delalloc_root_lock); |
| 10171 | list_splice_init(&fs_info->delalloc_roots, &splice); |
Miao Xie | 6c255e6 | 2014-03-06 13:55:01 +0800 | [diff] [blame] | 10172 | while (!list_empty(&splice) && nr) { |
Miao Xie | eb73c1b | 2013-05-15 07:48:22 +0000 | [diff] [blame] | 10173 | root = list_first_entry(&splice, struct btrfs_root, |
| 10174 | delalloc_root); |
| 10175 | root = btrfs_grab_fs_root(root); |
| 10176 | BUG_ON(!root); |
| 10177 | list_move_tail(&root->delalloc_root, |
| 10178 | &fs_info->delalloc_roots); |
| 10179 | spin_unlock(&fs_info->delalloc_root_lock); |
| 10180 | |
Miao Xie | 6c255e6 | 2014-03-06 13:55:01 +0800 | [diff] [blame] | 10181 | ret = __start_delalloc_inodes(root, delay_iput, nr); |
Miao Xie | eb73c1b | 2013-05-15 07:48:22 +0000 | [diff] [blame] | 10182 | btrfs_put_fs_root(root); |
Miao Xie | 6c255e6 | 2014-03-06 13:55:01 +0800 | [diff] [blame] | 10183 | if (ret < 0) |
Miao Xie | eb73c1b | 2013-05-15 07:48:22 +0000 | [diff] [blame] | 10184 | goto out; |
| 10185 | |
Miao Xie | 6c255e6 | 2014-03-06 13:55:01 +0800 | [diff] [blame] | 10186 | if (nr != -1) { |
| 10187 | nr -= ret; |
| 10188 | WARN_ON(nr < 0); |
| 10189 | } |
Miao Xie | eb73c1b | 2013-05-15 07:48:22 +0000 | [diff] [blame] | 10190 | spin_lock(&fs_info->delalloc_root_lock); |
| 10191 | } |
| 10192 | spin_unlock(&fs_info->delalloc_root_lock); |
| 10193 | |
Miao Xie | 6c255e6 | 2014-03-06 13:55:01 +0800 | [diff] [blame] | 10194 | ret = 0; |
Miao Xie | eb73c1b | 2013-05-15 07:48:22 +0000 | [diff] [blame] | 10195 | atomic_inc(&fs_info->async_submit_draining); |
| 10196 | while (atomic_read(&fs_info->nr_async_submits) || |
| 10197 | atomic_read(&fs_info->async_delalloc_pages)) { |
| 10198 | wait_event(fs_info->async_submit_wait, |
| 10199 | (atomic_read(&fs_info->nr_async_submits) == 0 && |
| 10200 | atomic_read(&fs_info->async_delalloc_pages) == 0)); |
| 10201 | } |
| 10202 | atomic_dec(&fs_info->async_submit_draining); |
Miao Xie | 8ccf6f19 | 2012-10-25 09:28:04 +0000 | [diff] [blame] | 10203 | out: |
Miao Xie | 1eafa6c | 2013-01-22 10:49:00 +0000 | [diff] [blame] | 10204 | if (!list_empty_careful(&splice)) { |
Miao Xie | eb73c1b | 2013-05-15 07:48:22 +0000 | [diff] [blame] | 10205 | spin_lock(&fs_info->delalloc_root_lock); |
| 10206 | list_splice_tail(&splice, &fs_info->delalloc_roots); |
| 10207 | spin_unlock(&fs_info->delalloc_root_lock); |
Miao Xie | 1eafa6c | 2013-01-22 10:49:00 +0000 | [diff] [blame] | 10208 | } |
Miao Xie | 573bfb7 | 2014-03-06 13:55:03 +0800 | [diff] [blame] | 10209 | mutex_unlock(&fs_info->delalloc_root_mutex); |
Miao Xie | 8ccf6f19 | 2012-10-25 09:28:04 +0000 | [diff] [blame] | 10210 | return ret; |
Chris Mason | ea8c281 | 2008-08-04 23:17:27 -0400 | [diff] [blame] | 10211 | } |
| 10212 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 10213 | static int btrfs_symlink(struct inode *dir, struct dentry *dentry, |
| 10214 | const char *symname) |
| 10215 | { |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 10216 | struct btrfs_fs_info *fs_info = btrfs_sb(dir->i_sb); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 10217 | struct btrfs_trans_handle *trans; |
| 10218 | struct btrfs_root *root = BTRFS_I(dir)->root; |
| 10219 | struct btrfs_path *path; |
| 10220 | struct btrfs_key key; |
Chris Mason | 1832a6d | 2007-12-21 16:27:21 -0500 | [diff] [blame] | 10221 | struct inode *inode = NULL; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 10222 | int err; |
| 10223 | int drop_inode = 0; |
| 10224 | u64 objectid; |
Dulshani Gunawardhana | 6787125 | 2013-10-31 10:33:04 +0530 | [diff] [blame] | 10225 | u64 index = 0; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 10226 | int name_len; |
| 10227 | int datasize; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 10228 | unsigned long ptr; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 10229 | struct btrfs_file_extent_item *ei; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 10230 | struct extent_buffer *leaf; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 10231 | |
Filipe David Borba Manana | f06becc | 2013-09-16 09:53:28 +0100 | [diff] [blame] | 10232 | name_len = strlen(symname); |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 10233 | if (name_len > BTRFS_MAX_INLINE_DATA_SIZE(fs_info)) |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 10234 | return -ENAMETOOLONG; |
Chris Mason | 1832a6d | 2007-12-21 16:27:21 -0500 | [diff] [blame] | 10235 | |
Josef Bacik | 9ed74f2 | 2009-09-11 16:12:44 -0400 | [diff] [blame] | 10236 | /* |
| 10237 | * 2 items for inode item and ref |
| 10238 | * 2 items for dir items |
Filipe Manana | 9269d12 | 2015-12-31 18:16:29 +0000 | [diff] [blame] | 10239 | * 1 item for updating parent inode item |
| 10240 | * 1 item for the inline extent item |
Josef Bacik | 9ed74f2 | 2009-09-11 16:12:44 -0400 | [diff] [blame] | 10241 | * 1 item for xattr if selinux is on |
| 10242 | */ |
Filipe Manana | 9269d12 | 2015-12-31 18:16:29 +0000 | [diff] [blame] | 10243 | trans = btrfs_start_transaction(root, 7); |
Yan, Zheng | a22285a | 2010-05-16 10:48:46 -0400 | [diff] [blame] | 10244 | if (IS_ERR(trans)) |
| 10245 | return PTR_ERR(trans); |
Chris Mason | 1832a6d | 2007-12-21 16:27:21 -0500 | [diff] [blame] | 10246 | |
Li Zefan | 581bb05 | 2011-04-20 10:06:11 +0800 | [diff] [blame] | 10247 | err = btrfs_find_free_ino(root, &objectid); |
| 10248 | if (err) |
| 10249 | goto out_unlock; |
| 10250 | |
Josef Bacik | aec7477 | 2008-07-24 12:12:38 -0400 | [diff] [blame] | 10251 | inode = btrfs_new_inode(trans, root, dir, dentry->d_name.name, |
David Sterba | f85b737 | 2017-01-20 14:54:07 +0100 | [diff] [blame] | 10252 | dentry->d_name.len, btrfs_ino(BTRFS_I(dir)), |
| 10253 | objectid, S_IFLNK|S_IRWXUGO, &index); |
Tsutomu Itoh | 7cf96da | 2011-04-25 19:43:53 -0400 | [diff] [blame] | 10254 | if (IS_ERR(inode)) { |
| 10255 | err = PTR_ERR(inode); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 10256 | goto out_unlock; |
Tsutomu Itoh | 7cf96da | 2011-04-25 19:43:53 -0400 | [diff] [blame] | 10257 | } |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 10258 | |
Casey Schaufler | ad19db7 | 2011-12-15 10:09:07 -0500 | [diff] [blame] | 10259 | /* |
| 10260 | * If the active LSM wants to access the inode during |
| 10261 | * d_instantiate it needs these. Smack checks to see |
| 10262 | * if the filesystem supports xattrs by looking at the |
| 10263 | * ops vector. |
| 10264 | */ |
| 10265 | inode->i_fop = &btrfs_file_operations; |
| 10266 | inode->i_op = &btrfs_file_inode_operations; |
Chris Mason | b0d5d10 | 2014-09-08 13:08:51 -0700 | [diff] [blame] | 10267 | inode->i_mapping->a_ops = &btrfs_aops; |
Chris Mason | b0d5d10 | 2014-09-08 13:08:51 -0700 | [diff] [blame] | 10268 | BTRFS_I(inode)->io_tree.ops = &btrfs_extent_io_ops; |
| 10269 | |
| 10270 | err = btrfs_init_inode_security(trans, inode, dir, &dentry->d_name); |
| 10271 | if (err) |
| 10272 | goto out_unlock_inode; |
Casey Schaufler | ad19db7 | 2011-12-15 10:09:07 -0500 | [diff] [blame] | 10273 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 10274 | path = btrfs_alloc_path(); |
Mark Fasheh | d8926bb | 2011-07-13 10:38:47 -0700 | [diff] [blame] | 10275 | if (!path) { |
| 10276 | err = -ENOMEM; |
Chris Mason | b0d5d10 | 2014-09-08 13:08:51 -0700 | [diff] [blame] | 10277 | goto out_unlock_inode; |
Mark Fasheh | d8926bb | 2011-07-13 10:38:47 -0700 | [diff] [blame] | 10278 | } |
Nikolay Borisov | 4a0cc7c | 2017-01-10 20:35:31 +0200 | [diff] [blame] | 10279 | key.objectid = btrfs_ino(BTRFS_I(inode)); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 10280 | key.offset = 0; |
David Sterba | 962a298 | 2014-06-04 18:41:45 +0200 | [diff] [blame] | 10281 | key.type = BTRFS_EXTENT_DATA_KEY; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 10282 | datasize = btrfs_file_extent_calc_inline_size(name_len); |
| 10283 | err = btrfs_insert_empty_item(trans, root, path, &key, |
| 10284 | datasize); |
Chris Mason | 54aa1f4 | 2007-06-22 14:16:25 -0400 | [diff] [blame] | 10285 | if (err) { |
Julia Lawall | b083916 | 2011-05-14 07:10:51 +0000 | [diff] [blame] | 10286 | btrfs_free_path(path); |
Chris Mason | b0d5d10 | 2014-09-08 13:08:51 -0700 | [diff] [blame] | 10287 | goto out_unlock_inode; |
Chris Mason | 54aa1f4 | 2007-06-22 14:16:25 -0400 | [diff] [blame] | 10288 | } |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 10289 | leaf = path->nodes[0]; |
| 10290 | ei = btrfs_item_ptr(leaf, path->slots[0], |
| 10291 | struct btrfs_file_extent_item); |
| 10292 | btrfs_set_file_extent_generation(leaf, ei, trans->transid); |
| 10293 | btrfs_set_file_extent_type(leaf, ei, |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 10294 | BTRFS_FILE_EXTENT_INLINE); |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 10295 | btrfs_set_file_extent_encryption(leaf, ei, 0); |
| 10296 | btrfs_set_file_extent_compression(leaf, ei, 0); |
| 10297 | btrfs_set_file_extent_other_encoding(leaf, ei, 0); |
| 10298 | btrfs_set_file_extent_ram_bytes(leaf, ei, name_len); |
| 10299 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 10300 | ptr = btrfs_file_extent_inline_start(ei); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 10301 | write_extent_buffer(leaf, symname, ptr, name_len); |
| 10302 | btrfs_mark_buffer_dirty(leaf); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 10303 | btrfs_free_path(path); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 10304 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 10305 | inode->i_op = &btrfs_symlink_inode_operations; |
Al Viro | 21fc61c | 2015-11-17 01:07:57 -0500 | [diff] [blame] | 10306 | inode_nohighmem(inode); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 10307 | inode->i_mapping->a_ops = &btrfs_symlink_aops; |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 10308 | inode_set_bytes(inode, name_len); |
Filipe David Borba Manana | f06becc | 2013-09-16 09:53:28 +0100 | [diff] [blame] | 10309 | btrfs_i_size_write(inode, name_len); |
Chris Mason | 54aa1f4 | 2007-06-22 14:16:25 -0400 | [diff] [blame] | 10310 | err = btrfs_update_inode(trans, root, inode); |
Filipe Manana | d50866d | 2015-12-31 18:08:24 +0000 | [diff] [blame] | 10311 | /* |
| 10312 | * Last step, add directory indexes for our symlink inode. This is the |
| 10313 | * last step to avoid extra cleanup of these indexes if an error happens |
| 10314 | * elsewhere above. |
| 10315 | */ |
| 10316 | if (!err) |
| 10317 | err = btrfs_add_nondir(trans, dir, dentry, inode, 0, index); |
Chris Mason | b0d5d10 | 2014-09-08 13:08:51 -0700 | [diff] [blame] | 10318 | if (err) { |
Chris Mason | 54aa1f4 | 2007-06-22 14:16:25 -0400 | [diff] [blame] | 10319 | drop_inode = 1; |
Chris Mason | b0d5d10 | 2014-09-08 13:08:51 -0700 | [diff] [blame] | 10320 | goto out_unlock_inode; |
| 10321 | } |
| 10322 | |
| 10323 | unlock_new_inode(inode); |
| 10324 | d_instantiate(dentry, inode); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 10325 | |
| 10326 | out_unlock: |
Jeff Mahoney | 3a45bb2 | 2016-09-09 21:39:03 -0400 | [diff] [blame] | 10327 | btrfs_end_transaction(trans); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 10328 | if (drop_inode) { |
| 10329 | inode_dec_link_count(inode); |
| 10330 | iput(inode); |
| 10331 | } |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 10332 | btrfs_btree_balance_dirty(fs_info); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 10333 | return err; |
Chris Mason | b0d5d10 | 2014-09-08 13:08:51 -0700 | [diff] [blame] | 10334 | |
| 10335 | out_unlock_inode: |
| 10336 | drop_inode = 1; |
| 10337 | unlock_new_inode(inode); |
| 10338 | goto out_unlock; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 10339 | } |
Chris Mason | 1643298 | 2008-04-10 10:23:21 -0400 | [diff] [blame] | 10340 | |
Josef Bacik | 0af3d00 | 2010-06-21 14:48:16 -0400 | [diff] [blame] | 10341 | static int __btrfs_prealloc_file_range(struct inode *inode, int mode, |
| 10342 | u64 start, u64 num_bytes, u64 min_size, |
| 10343 | loff_t actual_len, u64 *alloc_hint, |
| 10344 | struct btrfs_trans_handle *trans) |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 10345 | { |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 10346 | struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb); |
Josef Bacik | 5dc562c | 2012-08-17 13:14:17 -0400 | [diff] [blame] | 10347 | struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree; |
| 10348 | struct extent_map *em; |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 10349 | struct btrfs_root *root = BTRFS_I(inode)->root; |
| 10350 | struct btrfs_key ins; |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 10351 | u64 cur_offset = start; |
Josef Bacik | 55a61d1 | 2010-11-22 18:50:32 +0000 | [diff] [blame] | 10352 | u64 i_size; |
Chris Mason | 154ea28 | 2013-03-05 11:11:26 -0500 | [diff] [blame] | 10353 | u64 cur_bytes; |
Josef Bacik | 0b670dc | 2015-09-23 17:11:16 -0400 | [diff] [blame] | 10354 | u64 last_alloc = (u64)-1; |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 10355 | int ret = 0; |
Josef Bacik | 0af3d00 | 2010-06-21 14:48:16 -0400 | [diff] [blame] | 10356 | bool own_trans = true; |
Wang Xiaoguang | 1851309 | 2016-07-25 15:51:40 +0800 | [diff] [blame] | 10357 | u64 end = start + num_bytes - 1; |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 10358 | |
Josef Bacik | 0af3d00 | 2010-06-21 14:48:16 -0400 | [diff] [blame] | 10359 | if (trans) |
| 10360 | own_trans = false; |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 10361 | while (num_bytes > 0) { |
Josef Bacik | 0af3d00 | 2010-06-21 14:48:16 -0400 | [diff] [blame] | 10362 | if (own_trans) { |
| 10363 | trans = btrfs_start_transaction(root, 3); |
| 10364 | if (IS_ERR(trans)) { |
| 10365 | ret = PTR_ERR(trans); |
| 10366 | break; |
| 10367 | } |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 10368 | } |
Yan, Zheng | 5a303d5 | 2009-11-12 09:34:52 +0000 | [diff] [blame] | 10369 | |
Byongho Lee | ee22184 | 2015-12-15 01:42:10 +0900 | [diff] [blame] | 10370 | cur_bytes = min_t(u64, num_bytes, SZ_256M); |
Chris Mason | 154ea28 | 2013-03-05 11:11:26 -0500 | [diff] [blame] | 10371 | cur_bytes = max(cur_bytes, min_size); |
Josef Bacik | 0b670dc | 2015-09-23 17:11:16 -0400 | [diff] [blame] | 10372 | /* |
| 10373 | * If we are severely fragmented we could end up with really |
| 10374 | * small allocations, so if the allocator is returning small |
| 10375 | * chunks lets make its job easier by only searching for those |
| 10376 | * sized chunks. |
| 10377 | */ |
| 10378 | cur_bytes = min(cur_bytes, last_alloc); |
Wang Xiaoguang | 1851309 | 2016-07-25 15:51:40 +0800 | [diff] [blame] | 10379 | ret = btrfs_reserve_extent(root, cur_bytes, cur_bytes, |
| 10380 | min_size, 0, *alloc_hint, &ins, 1, 0); |
Yan, Zheng | 5a303d5 | 2009-11-12 09:34:52 +0000 | [diff] [blame] | 10381 | if (ret) { |
Josef Bacik | 0af3d00 | 2010-06-21 14:48:16 -0400 | [diff] [blame] | 10382 | if (own_trans) |
Jeff Mahoney | 3a45bb2 | 2016-09-09 21:39:03 -0400 | [diff] [blame] | 10383 | btrfs_end_transaction(trans); |
Yan, Zheng | a22285a | 2010-05-16 10:48:46 -0400 | [diff] [blame] | 10384 | break; |
Yan, Zheng | 5a303d5 | 2009-11-12 09:34:52 +0000 | [diff] [blame] | 10385 | } |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 10386 | btrfs_dec_block_group_reservations(fs_info, ins.objectid); |
Yan, Zheng | 5a303d5 | 2009-11-12 09:34:52 +0000 | [diff] [blame] | 10387 | |
Josef Bacik | 0b670dc | 2015-09-23 17:11:16 -0400 | [diff] [blame] | 10388 | last_alloc = ins.offset; |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 10389 | ret = insert_reserved_file_extent(trans, inode, |
| 10390 | cur_offset, ins.objectid, |
| 10391 | ins.offset, ins.offset, |
Yan, Zheng | 920bbbf | 2009-11-12 09:34:08 +0000 | [diff] [blame] | 10392 | ins.offset, 0, 0, 0, |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 10393 | BTRFS_FILE_EXTENT_PREALLOC); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 10394 | if (ret) { |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 10395 | btrfs_free_reserved_extent(fs_info, ins.objectid, |
Miao Xie | e570fd2 | 2014-06-19 10:42:50 +0800 | [diff] [blame] | 10396 | ins.offset, 0); |
Jeff Mahoney | 6664283 | 2016-06-10 18:19:25 -0400 | [diff] [blame] | 10397 | btrfs_abort_transaction(trans, ret); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 10398 | if (own_trans) |
Jeff Mahoney | 3a45bb2 | 2016-09-09 21:39:03 -0400 | [diff] [blame] | 10399 | btrfs_end_transaction(trans); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 10400 | break; |
| 10401 | } |
Dongsheng Yang | 3119321 | 2014-12-12 16:44:35 +0800 | [diff] [blame] | 10402 | |
Chris Mason | a1ed835 | 2009-09-11 12:27:37 -0400 | [diff] [blame] | 10403 | btrfs_drop_extent_cache(inode, cur_offset, |
| 10404 | cur_offset + ins.offset -1, 0); |
Yan, Zheng | 5a303d5 | 2009-11-12 09:34:52 +0000 | [diff] [blame] | 10405 | |
Josef Bacik | 5dc562c | 2012-08-17 13:14:17 -0400 | [diff] [blame] | 10406 | em = alloc_extent_map(); |
| 10407 | if (!em) { |
| 10408 | set_bit(BTRFS_INODE_NEEDS_FULL_SYNC, |
| 10409 | &BTRFS_I(inode)->runtime_flags); |
| 10410 | goto next; |
| 10411 | } |
| 10412 | |
| 10413 | em->start = cur_offset; |
| 10414 | em->orig_start = cur_offset; |
| 10415 | em->len = ins.offset; |
| 10416 | em->block_start = ins.objectid; |
| 10417 | em->block_len = ins.offset; |
Josef Bacik | b493968 | 2012-12-03 10:31:19 -0500 | [diff] [blame] | 10418 | em->orig_block_len = ins.offset; |
Josef Bacik | cc95bef | 2013-04-04 14:31:27 -0400 | [diff] [blame] | 10419 | em->ram_bytes = ins.offset; |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 10420 | em->bdev = fs_info->fs_devices->latest_bdev; |
Josef Bacik | 5dc562c | 2012-08-17 13:14:17 -0400 | [diff] [blame] | 10421 | set_bit(EXTENT_FLAG_PREALLOC, &em->flags); |
| 10422 | em->generation = trans->transid; |
| 10423 | |
| 10424 | while (1) { |
| 10425 | write_lock(&em_tree->lock); |
Josef Bacik | 09a2a8f9 | 2013-04-05 16:51:15 -0400 | [diff] [blame] | 10426 | ret = add_extent_mapping(em_tree, em, 1); |
Josef Bacik | 5dc562c | 2012-08-17 13:14:17 -0400 | [diff] [blame] | 10427 | write_unlock(&em_tree->lock); |
| 10428 | if (ret != -EEXIST) |
| 10429 | break; |
| 10430 | btrfs_drop_extent_cache(inode, cur_offset, |
| 10431 | cur_offset + ins.offset - 1, |
| 10432 | 0); |
| 10433 | } |
| 10434 | free_extent_map(em); |
| 10435 | next: |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 10436 | num_bytes -= ins.offset; |
| 10437 | cur_offset += ins.offset; |
Yan, Zheng | efa5646 | 2010-05-16 10:49:59 -0400 | [diff] [blame] | 10438 | *alloc_hint = ins.objectid + ins.offset; |
Yan, Zheng | 5a303d5 | 2009-11-12 09:34:52 +0000 | [diff] [blame] | 10439 | |
Josef Bacik | 0c4d2d9 | 2012-04-05 15:03:02 -0400 | [diff] [blame] | 10440 | inode_inc_iversion(inode); |
Deepa Dinamani | c2050a4 | 2016-09-14 07:48:06 -0700 | [diff] [blame] | 10441 | inode->i_ctime = current_time(inode); |
Christoph Hellwig | 6cbff00 | 2009-04-17 10:37:41 +0200 | [diff] [blame] | 10442 | BTRFS_I(inode)->flags |= BTRFS_INODE_PREALLOC; |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 10443 | if (!(mode & FALLOC_FL_KEEP_SIZE) && |
Yan, Zheng | efa5646 | 2010-05-16 10:49:59 -0400 | [diff] [blame] | 10444 | (actual_len > inode->i_size) && |
| 10445 | (cur_offset > inode->i_size)) { |
Aneesh Kumar K.V | d1ea6a6 | 2010-01-20 07:28:54 +0000 | [diff] [blame] | 10446 | if (cur_offset > actual_len) |
Josef Bacik | 55a61d1 | 2010-11-22 18:50:32 +0000 | [diff] [blame] | 10447 | i_size = actual_len; |
Aneesh Kumar K.V | d1ea6a6 | 2010-01-20 07:28:54 +0000 | [diff] [blame] | 10448 | else |
Josef Bacik | 55a61d1 | 2010-11-22 18:50:32 +0000 | [diff] [blame] | 10449 | i_size = cur_offset; |
| 10450 | i_size_write(inode, i_size); |
| 10451 | btrfs_ordered_update_i_size(inode, i_size, NULL); |
Yan, Zheng | 5a303d5 | 2009-11-12 09:34:52 +0000 | [diff] [blame] | 10452 | } |
| 10453 | |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 10454 | ret = btrfs_update_inode(trans, root, inode); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 10455 | |
| 10456 | if (ret) { |
Jeff Mahoney | 6664283 | 2016-06-10 18:19:25 -0400 | [diff] [blame] | 10457 | btrfs_abort_transaction(trans, ret); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 10458 | if (own_trans) |
Jeff Mahoney | 3a45bb2 | 2016-09-09 21:39:03 -0400 | [diff] [blame] | 10459 | btrfs_end_transaction(trans); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 10460 | break; |
| 10461 | } |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 10462 | |
Josef Bacik | 0af3d00 | 2010-06-21 14:48:16 -0400 | [diff] [blame] | 10463 | if (own_trans) |
Jeff Mahoney | 3a45bb2 | 2016-09-09 21:39:03 -0400 | [diff] [blame] | 10464 | btrfs_end_transaction(trans); |
Yan, Zheng | 5a303d5 | 2009-11-12 09:34:52 +0000 | [diff] [blame] | 10465 | } |
Wang Xiaoguang | 1851309 | 2016-07-25 15:51:40 +0800 | [diff] [blame] | 10466 | if (cur_offset < end) |
| 10467 | btrfs_free_reserved_data_space(inode, cur_offset, |
| 10468 | end - cur_offset + 1); |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 10469 | return ret; |
| 10470 | } |
| 10471 | |
Josef Bacik | 0af3d00 | 2010-06-21 14:48:16 -0400 | [diff] [blame] | 10472 | int btrfs_prealloc_file_range(struct inode *inode, int mode, |
| 10473 | u64 start, u64 num_bytes, u64 min_size, |
| 10474 | loff_t actual_len, u64 *alloc_hint) |
| 10475 | { |
| 10476 | return __btrfs_prealloc_file_range(inode, mode, start, num_bytes, |
| 10477 | min_size, actual_len, alloc_hint, |
| 10478 | NULL); |
| 10479 | } |
| 10480 | |
| 10481 | int btrfs_prealloc_file_range_trans(struct inode *inode, |
| 10482 | struct btrfs_trans_handle *trans, int mode, |
| 10483 | u64 start, u64 num_bytes, u64 min_size, |
| 10484 | loff_t actual_len, u64 *alloc_hint) |
| 10485 | { |
| 10486 | return __btrfs_prealloc_file_range(inode, mode, start, num_bytes, |
| 10487 | min_size, actual_len, alloc_hint, trans); |
| 10488 | } |
| 10489 | |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 10490 | static int btrfs_set_page_dirty(struct page *page) |
| 10491 | { |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 10492 | return __set_page_dirty_nobuffers(page); |
| 10493 | } |
| 10494 | |
Al Viro | 10556cb2 | 2011-06-20 19:28:19 -0400 | [diff] [blame] | 10495 | static int btrfs_permission(struct inode *inode, int mask) |
Yan | fdebe2b | 2008-01-14 13:26:08 -0500 | [diff] [blame] | 10496 | { |
Li Zefan | b83cc96 | 2010-12-20 16:04:08 +0800 | [diff] [blame] | 10497 | struct btrfs_root *root = BTRFS_I(inode)->root; |
Jeff Mahoney | cb6db4e | 2011-08-15 17:27:21 +0000 | [diff] [blame] | 10498 | umode_t mode = inode->i_mode; |
Li Zefan | b83cc96 | 2010-12-20 16:04:08 +0800 | [diff] [blame] | 10499 | |
Jeff Mahoney | cb6db4e | 2011-08-15 17:27:21 +0000 | [diff] [blame] | 10500 | if (mask & MAY_WRITE && |
| 10501 | (S_ISREG(mode) || S_ISDIR(mode) || S_ISLNK(mode))) { |
| 10502 | if (btrfs_root_readonly(root)) |
| 10503 | return -EROFS; |
| 10504 | if (BTRFS_I(inode)->flags & BTRFS_INODE_READONLY) |
| 10505 | return -EACCES; |
| 10506 | } |
Al Viro | 2830ba7 | 2011-06-20 19:16:29 -0400 | [diff] [blame] | 10507 | return generic_permission(inode, mask); |
Yan | fdebe2b | 2008-01-14 13:26:08 -0500 | [diff] [blame] | 10508 | } |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 10509 | |
Filipe Manana | ef3b9af | 2014-04-27 20:40:45 +0100 | [diff] [blame] | 10510 | static int btrfs_tmpfile(struct inode *dir, struct dentry *dentry, umode_t mode) |
| 10511 | { |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 10512 | struct btrfs_fs_info *fs_info = btrfs_sb(dir->i_sb); |
Filipe Manana | ef3b9af | 2014-04-27 20:40:45 +0100 | [diff] [blame] | 10513 | struct btrfs_trans_handle *trans; |
| 10514 | struct btrfs_root *root = BTRFS_I(dir)->root; |
| 10515 | struct inode *inode = NULL; |
| 10516 | u64 objectid; |
| 10517 | u64 index; |
| 10518 | int ret = 0; |
| 10519 | |
| 10520 | /* |
| 10521 | * 5 units required for adding orphan entry |
| 10522 | */ |
| 10523 | trans = btrfs_start_transaction(root, 5); |
| 10524 | if (IS_ERR(trans)) |
| 10525 | return PTR_ERR(trans); |
| 10526 | |
| 10527 | ret = btrfs_find_free_ino(root, &objectid); |
| 10528 | if (ret) |
| 10529 | goto out; |
| 10530 | |
| 10531 | inode = btrfs_new_inode(trans, root, dir, NULL, 0, |
David Sterba | f85b737 | 2017-01-20 14:54:07 +0100 | [diff] [blame] | 10532 | btrfs_ino(BTRFS_I(dir)), objectid, mode, &index); |
Filipe Manana | ef3b9af | 2014-04-27 20:40:45 +0100 | [diff] [blame] | 10533 | if (IS_ERR(inode)) { |
| 10534 | ret = PTR_ERR(inode); |
| 10535 | inode = NULL; |
| 10536 | goto out; |
| 10537 | } |
| 10538 | |
Filipe Manana | ef3b9af | 2014-04-27 20:40:45 +0100 | [diff] [blame] | 10539 | inode->i_fop = &btrfs_file_operations; |
| 10540 | inode->i_op = &btrfs_file_inode_operations; |
| 10541 | |
| 10542 | inode->i_mapping->a_ops = &btrfs_aops; |
Filipe Manana | ef3b9af | 2014-04-27 20:40:45 +0100 | [diff] [blame] | 10543 | BTRFS_I(inode)->io_tree.ops = &btrfs_extent_io_ops; |
| 10544 | |
Chris Mason | b0d5d10 | 2014-09-08 13:08:51 -0700 | [diff] [blame] | 10545 | ret = btrfs_init_inode_security(trans, inode, dir, NULL); |
| 10546 | if (ret) |
| 10547 | goto out_inode; |
| 10548 | |
| 10549 | ret = btrfs_update_inode(trans, root, inode); |
| 10550 | if (ret) |
| 10551 | goto out_inode; |
Filipe Manana | ef3b9af | 2014-04-27 20:40:45 +0100 | [diff] [blame] | 10552 | ret = btrfs_orphan_add(trans, inode); |
| 10553 | if (ret) |
Chris Mason | b0d5d10 | 2014-09-08 13:08:51 -0700 | [diff] [blame] | 10554 | goto out_inode; |
Filipe Manana | ef3b9af | 2014-04-27 20:40:45 +0100 | [diff] [blame] | 10555 | |
Filipe Manana | 5762b5c | 2014-08-01 00:10:32 +0100 | [diff] [blame] | 10556 | /* |
| 10557 | * We set number of links to 0 in btrfs_new_inode(), and here we set |
| 10558 | * it to 1 because d_tmpfile() will issue a warning if the count is 0, |
| 10559 | * through: |
| 10560 | * |
| 10561 | * d_tmpfile() -> inode_dec_link_count() -> drop_nlink() |
| 10562 | */ |
| 10563 | set_nlink(inode, 1); |
Chris Mason | b0d5d10 | 2014-09-08 13:08:51 -0700 | [diff] [blame] | 10564 | unlock_new_inode(inode); |
Filipe Manana | ef3b9af | 2014-04-27 20:40:45 +0100 | [diff] [blame] | 10565 | d_tmpfile(dentry, inode); |
| 10566 | mark_inode_dirty(inode); |
| 10567 | |
| 10568 | out: |
Jeff Mahoney | 3a45bb2 | 2016-09-09 21:39:03 -0400 | [diff] [blame] | 10569 | btrfs_end_transaction(trans); |
Filipe Manana | ef3b9af | 2014-04-27 20:40:45 +0100 | [diff] [blame] | 10570 | if (ret) |
| 10571 | iput(inode); |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 10572 | btrfs_balance_delayed_items(fs_info); |
| 10573 | btrfs_btree_balance_dirty(fs_info); |
Filipe Manana | ef3b9af | 2014-04-27 20:40:45 +0100 | [diff] [blame] | 10574 | return ret; |
Chris Mason | b0d5d10 | 2014-09-08 13:08:51 -0700 | [diff] [blame] | 10575 | |
| 10576 | out_inode: |
| 10577 | unlock_new_inode(inode); |
| 10578 | goto out; |
| 10579 | |
Filipe Manana | ef3b9af | 2014-04-27 20:40:45 +0100 | [diff] [blame] | 10580 | } |
| 10581 | |
Alexey Dobriyan | 6e1d5dc | 2009-09-21 17:01:11 -0700 | [diff] [blame] | 10582 | static const struct inode_operations btrfs_dir_inode_operations = { |
Chris Mason | 3394e16 | 2008-11-17 20:42:26 -0500 | [diff] [blame] | 10583 | .getattr = btrfs_getattr, |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 10584 | .lookup = btrfs_lookup, |
| 10585 | .create = btrfs_create, |
| 10586 | .unlink = btrfs_unlink, |
| 10587 | .link = btrfs_link, |
| 10588 | .mkdir = btrfs_mkdir, |
| 10589 | .rmdir = btrfs_rmdir, |
Miklos Szeredi | 2773bf0 | 2016-09-27 11:03:58 +0200 | [diff] [blame] | 10590 | .rename = btrfs_rename2, |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 10591 | .symlink = btrfs_symlink, |
| 10592 | .setattr = btrfs_setattr, |
Josef Bacik | 618e21d | 2007-07-11 10:18:17 -0400 | [diff] [blame] | 10593 | .mknod = btrfs_mknod, |
Josef Bacik | 5103e94 | 2007-11-16 11:45:54 -0500 | [diff] [blame] | 10594 | .listxattr = btrfs_listxattr, |
Yan | fdebe2b | 2008-01-14 13:26:08 -0500 | [diff] [blame] | 10595 | .permission = btrfs_permission, |
Christoph Hellwig | 4e34e71 | 2011-07-23 17:37:31 +0200 | [diff] [blame] | 10596 | .get_acl = btrfs_get_acl, |
Christoph Hellwig | 996a710 | 2013-12-20 05:16:43 -0800 | [diff] [blame] | 10597 | .set_acl = btrfs_set_acl, |
Guangyu Sun | 93fd63c | 2013-09-16 10:42:03 -0700 | [diff] [blame] | 10598 | .update_time = btrfs_update_time, |
Filipe Manana | ef3b9af | 2014-04-27 20:40:45 +0100 | [diff] [blame] | 10599 | .tmpfile = btrfs_tmpfile, |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 10600 | }; |
Alexey Dobriyan | 6e1d5dc | 2009-09-21 17:01:11 -0700 | [diff] [blame] | 10601 | static const struct inode_operations btrfs_dir_ro_inode_operations = { |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 10602 | .lookup = btrfs_lookup, |
Yan | fdebe2b | 2008-01-14 13:26:08 -0500 | [diff] [blame] | 10603 | .permission = btrfs_permission, |
Guangyu Sun | 93fd63c | 2013-09-16 10:42:03 -0700 | [diff] [blame] | 10604 | .update_time = btrfs_update_time, |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 10605 | }; |
Yan, Zheng | 76dda93 | 2009-09-21 16:00:26 -0400 | [diff] [blame] | 10606 | |
Alexey Dobriyan | 828c095 | 2009-10-01 15:43:56 -0700 | [diff] [blame] | 10607 | static const struct file_operations btrfs_dir_file_operations = { |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 10608 | .llseek = generic_file_llseek, |
| 10609 | .read = generic_read_dir, |
Omar Sandoval | 02dbfc9 | 2016-05-20 13:50:33 -0700 | [diff] [blame] | 10610 | .iterate_shared = btrfs_real_readdir, |
Christoph Hellwig | 34287aa | 2007-09-14 10:22:47 -0400 | [diff] [blame] | 10611 | .unlocked_ioctl = btrfs_ioctl, |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 10612 | #ifdef CONFIG_COMPAT |
Luke Dashjr | 4c63c24 | 2015-10-29 08:22:21 +0000 | [diff] [blame] | 10613 | .compat_ioctl = btrfs_compat_ioctl, |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 10614 | #endif |
Sage Weil | 6bf13c0 | 2008-06-10 10:07:39 -0400 | [diff] [blame] | 10615 | .release = btrfs_release_file, |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 10616 | .fsync = btrfs_sync_file, |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 10617 | }; |
| 10618 | |
David Sterba | 20e5506 | 2015-11-19 11:42:28 +0100 | [diff] [blame] | 10619 | static const struct extent_io_ops btrfs_extent_io_ops = { |
Chris Mason | 07157aa | 2007-08-30 08:50:51 -0400 | [diff] [blame] | 10620 | .fill_delalloc = run_delalloc_range, |
Chris Mason | 065631f | 2008-02-20 12:07:25 -0500 | [diff] [blame] | 10621 | .submit_bio_hook = btrfs_submit_bio_hook, |
Chris Mason | 239b14b | 2008-03-24 15:02:07 -0400 | [diff] [blame] | 10622 | .merge_bio_hook = btrfs_merge_bio_hook, |
Chris Mason | 07157aa | 2007-08-30 08:50:51 -0400 | [diff] [blame] | 10623 | .readpage_end_io_hook = btrfs_readpage_end_io_hook, |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 10624 | .writepage_end_io_hook = btrfs_writepage_end_io_hook, |
Chris Mason | 247e743 | 2008-07-17 12:53:51 -0400 | [diff] [blame] | 10625 | .writepage_start_hook = btrfs_writepage_start_hook, |
Chris Mason | b0c68f8 | 2008-01-31 11:05:37 -0500 | [diff] [blame] | 10626 | .set_bit_hook = btrfs_set_bit_hook, |
| 10627 | .clear_bit_hook = btrfs_clear_bit_hook, |
Josef Bacik | 9ed74f2 | 2009-09-11 16:12:44 -0400 | [diff] [blame] | 10628 | .merge_extent_hook = btrfs_merge_extent_hook, |
| 10629 | .split_extent_hook = btrfs_split_extent_hook, |
Chris Mason | 07157aa | 2007-08-30 08:50:51 -0400 | [diff] [blame] | 10630 | }; |
| 10631 | |
Chris Mason | 3505439 | 2009-01-21 13:11:13 -0500 | [diff] [blame] | 10632 | /* |
| 10633 | * btrfs doesn't support the bmap operation because swapfiles |
| 10634 | * use bmap to make a mapping of extents in the file. They assume |
| 10635 | * these extents won't change over the life of the file and they |
| 10636 | * use the bmap result to do IO directly to the drive. |
| 10637 | * |
| 10638 | * the btrfs bmap call would return logical addresses that aren't |
| 10639 | * suitable for IO and they also will change frequently as COW |
| 10640 | * operations happen. So, swapfile + btrfs == corruption. |
| 10641 | * |
| 10642 | * For now we're avoiding this by dropping bmap. |
| 10643 | */ |
Alexey Dobriyan | 7f09410 | 2009-09-21 17:01:10 -0700 | [diff] [blame] | 10644 | static const struct address_space_operations btrfs_aops = { |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 10645 | .readpage = btrfs_readpage, |
| 10646 | .writepage = btrfs_writepage, |
Chris Mason | b293f02e | 2007-11-01 19:45:34 -0400 | [diff] [blame] | 10647 | .writepages = btrfs_writepages, |
Chris Mason | 3ab2fb5 | 2007-11-08 10:59:22 -0500 | [diff] [blame] | 10648 | .readpages = btrfs_readpages, |
Chris Mason | 1643298 | 2008-04-10 10:23:21 -0400 | [diff] [blame] | 10649 | .direct_IO = btrfs_direct_IO, |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 10650 | .invalidatepage = btrfs_invalidatepage, |
| 10651 | .releasepage = btrfs_releasepage, |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 10652 | .set_page_dirty = btrfs_set_page_dirty, |
Andi Kleen | 465fdd9 | 2009-09-16 11:50:18 +0200 | [diff] [blame] | 10653 | .error_remove_page = generic_error_remove_page, |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 10654 | }; |
| 10655 | |
Alexey Dobriyan | 7f09410 | 2009-09-21 17:01:10 -0700 | [diff] [blame] | 10656 | static const struct address_space_operations btrfs_symlink_aops = { |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 10657 | .readpage = btrfs_readpage, |
| 10658 | .writepage = btrfs_writepage, |
Chris Mason | 2bf5a72 | 2007-08-30 11:54:02 -0400 | [diff] [blame] | 10659 | .invalidatepage = btrfs_invalidatepage, |
| 10660 | .releasepage = btrfs_releasepage, |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 10661 | }; |
| 10662 | |
Alexey Dobriyan | 6e1d5dc | 2009-09-21 17:01:11 -0700 | [diff] [blame] | 10663 | static const struct inode_operations btrfs_file_inode_operations = { |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 10664 | .getattr = btrfs_getattr, |
| 10665 | .setattr = btrfs_setattr, |
Josef Bacik | 5103e94 | 2007-11-16 11:45:54 -0500 | [diff] [blame] | 10666 | .listxattr = btrfs_listxattr, |
Yan | fdebe2b | 2008-01-14 13:26:08 -0500 | [diff] [blame] | 10667 | .permission = btrfs_permission, |
Yehuda Sadeh | 1506fcc | 2009-01-21 14:39:14 -0500 | [diff] [blame] | 10668 | .fiemap = btrfs_fiemap, |
Christoph Hellwig | 4e34e71 | 2011-07-23 17:37:31 +0200 | [diff] [blame] | 10669 | .get_acl = btrfs_get_acl, |
Christoph Hellwig | 996a710 | 2013-12-20 05:16:43 -0800 | [diff] [blame] | 10670 | .set_acl = btrfs_set_acl, |
Josef Bacik | e41f941 | 2012-03-26 09:46:47 -0400 | [diff] [blame] | 10671 | .update_time = btrfs_update_time, |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 10672 | }; |
Alexey Dobriyan | 6e1d5dc | 2009-09-21 17:01:11 -0700 | [diff] [blame] | 10673 | static const struct inode_operations btrfs_special_inode_operations = { |
Josef Bacik | 618e21d | 2007-07-11 10:18:17 -0400 | [diff] [blame] | 10674 | .getattr = btrfs_getattr, |
| 10675 | .setattr = btrfs_setattr, |
Yan | fdebe2b | 2008-01-14 13:26:08 -0500 | [diff] [blame] | 10676 | .permission = btrfs_permission, |
Josef Bacik | 33268ea | 2008-07-24 12:16:36 -0400 | [diff] [blame] | 10677 | .listxattr = btrfs_listxattr, |
Christoph Hellwig | 4e34e71 | 2011-07-23 17:37:31 +0200 | [diff] [blame] | 10678 | .get_acl = btrfs_get_acl, |
Christoph Hellwig | 996a710 | 2013-12-20 05:16:43 -0800 | [diff] [blame] | 10679 | .set_acl = btrfs_set_acl, |
Josef Bacik | e41f941 | 2012-03-26 09:46:47 -0400 | [diff] [blame] | 10680 | .update_time = btrfs_update_time, |
Josef Bacik | 618e21d | 2007-07-11 10:18:17 -0400 | [diff] [blame] | 10681 | }; |
Alexey Dobriyan | 6e1d5dc | 2009-09-21 17:01:11 -0700 | [diff] [blame] | 10682 | static const struct inode_operations btrfs_symlink_inode_operations = { |
Al Viro | 6b25539 | 2015-11-17 10:20:54 -0500 | [diff] [blame] | 10683 | .get_link = page_get_link, |
Li Zefan | f209561 | 2010-11-19 02:05:24 +0000 | [diff] [blame] | 10684 | .getattr = btrfs_getattr, |
Josef Bacik | 22c44fe | 2011-11-30 10:45:38 -0500 | [diff] [blame] | 10685 | .setattr = btrfs_setattr, |
Yan | fdebe2b | 2008-01-14 13:26:08 -0500 | [diff] [blame] | 10686 | .permission = btrfs_permission, |
Jim Owens | 0279b4c | 2009-02-04 09:29:13 -0500 | [diff] [blame] | 10687 | .listxattr = btrfs_listxattr, |
Josef Bacik | e41f941 | 2012-03-26 09:46:47 -0400 | [diff] [blame] | 10688 | .update_time = btrfs_update_time, |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 10689 | }; |
Yan, Zheng | 76dda93 | 2009-09-21 16:00:26 -0400 | [diff] [blame] | 10690 | |
Alexey Dobriyan | 82d339d | 2009-10-09 09:54:36 -0400 | [diff] [blame] | 10691 | const struct dentry_operations btrfs_dentry_operations = { |
Yan, Zheng | 76dda93 | 2009-09-21 16:00:26 -0400 | [diff] [blame] | 10692 | .d_delete = btrfs_dentry_delete, |
Josef Bacik | b4aff1f | 2011-06-28 16:18:59 -0400 | [diff] [blame] | 10693 | .d_release = btrfs_dentry_release, |
Yan, Zheng | 76dda93 | 2009-09-21 16:00:26 -0400 | [diff] [blame] | 10694 | }; |