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> |
| 33 | #include <linux/statfs.h> |
| 34 | #include <linux/compat.h> |
Chris Mason | 9ebefb18 | 2007-06-15 13:50:00 -0400 | [diff] [blame] | 35 | #include <linux/bit_spinlock.h> |
Josef Bacik | 5103e94 | 2007-11-16 11:45:54 -0500 | [diff] [blame] | 36 | #include <linux/xattr.h> |
Josef Bacik | 33268ea | 2008-07-24 12:16:36 -0400 | [diff] [blame] | 37 | #include <linux/posix_acl.h> |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 38 | #include <linux/falloc.h> |
Tejun Heo | 5a0e3ad | 2010-03-24 17:04:11 +0900 | [diff] [blame] | 39 | #include <linux/slab.h> |
David Sterba | 7a36dde | 2011-05-06 15:33:15 +0200 | [diff] [blame] | 40 | #include <linux/ratelimit.h> |
Josef Bacik | 22c44fe | 2011-11-30 10:45:38 -0500 | [diff] [blame] | 41 | #include <linux/mount.h> |
Filipe Brandenburger | 55e301f | 2013-01-29 06:04:50 +0000 | [diff] [blame] | 42 | #include <linux/btrfs.h> |
David Woodhouse | 53b381b | 2013-01-29 18:40:14 -0500 | [diff] [blame] | 43 | #include <linux/blkdev.h> |
Josef Bacik | f23b5a5 | 2013-06-19 10:16:26 -0400 | [diff] [blame] | 44 | #include <linux/posix_acl_xattr.h> |
Christoph Hellwig | e2e40f2 | 2015-02-22 08:58:50 -0800 | [diff] [blame] | 45 | #include <linux/uio.h> |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 46 | #include "ctree.h" |
| 47 | #include "disk-io.h" |
| 48 | #include "transaction.h" |
| 49 | #include "btrfs_inode.h" |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 50 | #include "print-tree.h" |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 51 | #include "ordered-data.h" |
Christoph Hellwig | 95819c0 | 2008-08-28 06:21:17 -0400 | [diff] [blame] | 52 | #include "xattr.h" |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 53 | #include "tree-log.h" |
Jan Schmidt | 4a54c8c | 2011-07-22 15:41:52 +0200 | [diff] [blame] | 54 | #include "volumes.h" |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 55 | #include "compression.h" |
Chris Mason | b4ce94d | 2009-02-04 09:25:08 -0500 | [diff] [blame] | 56 | #include "locking.h" |
Josef Bacik | dc89e98 | 2011-01-28 17:05:48 -0500 | [diff] [blame] | 57 | #include "free-space-cache.h" |
Li Zefan | 581bb05 | 2011-04-20 10:06:11 +0800 | [diff] [blame] | 58 | #include "inode-map.h" |
Liu Bo | 38c227d | 2013-01-29 03:18:40 +0000 | [diff] [blame] | 59 | #include "backref.h" |
Josef Bacik | f23b5a5 | 2013-06-19 10:16:26 -0400 | [diff] [blame] | 60 | #include "hash.h" |
Filipe David Borba Manana | 6354192 | 2014-01-07 11:47:46 +0000 | [diff] [blame] | 61 | #include "props.h" |
Dongsheng Yang | 3119321 | 2014-12-12 16:44:35 +0800 | [diff] [blame] | 62 | #include "qgroup.h" |
Wang Xiaoguang | dda3245 | 2016-07-11 11:05:29 +0800 | [diff] [blame] | 63 | #include "dedupe.h" |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 64 | |
| 65 | struct btrfs_iget_args { |
Chris Mason | 90d3e59 | 2014-01-09 17:28:00 -0800 | [diff] [blame] | 66 | struct btrfs_key *location; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 67 | struct btrfs_root *root; |
| 68 | }; |
| 69 | |
Filipe Manana | f28a492 | 2015-12-08 19:23:20 +0000 | [diff] [blame] | 70 | struct btrfs_dio_data { |
| 71 | u64 outstanding_extents; |
| 72 | u64 reserve; |
| 73 | u64 unsubmitted_oe_range_start; |
| 74 | u64 unsubmitted_oe_range_end; |
| 75 | }; |
| 76 | |
Alexey Dobriyan | 6e1d5dc | 2009-09-21 17:01:11 -0700 | [diff] [blame] | 77 | static const struct inode_operations btrfs_dir_inode_operations; |
| 78 | static const struct inode_operations btrfs_symlink_inode_operations; |
| 79 | static const struct inode_operations btrfs_dir_ro_inode_operations; |
| 80 | static const struct inode_operations btrfs_special_inode_operations; |
| 81 | static const struct inode_operations btrfs_file_inode_operations; |
Alexey Dobriyan | 7f09410 | 2009-09-21 17:01:10 -0700 | [diff] [blame] | 82 | static const struct address_space_operations btrfs_aops; |
| 83 | static const struct address_space_operations btrfs_symlink_aops; |
Alexey Dobriyan | 828c095 | 2009-10-01 15:43:56 -0700 | [diff] [blame] | 84 | static const struct file_operations btrfs_dir_file_operations; |
David Sterba | 20e5506 | 2015-11-19 11:42:28 +0100 | [diff] [blame] | 85 | static const struct extent_io_ops btrfs_extent_io_ops; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 86 | |
| 87 | static struct kmem_cache *btrfs_inode_cachep; |
| 88 | struct kmem_cache *btrfs_trans_handle_cachep; |
| 89 | struct kmem_cache *btrfs_transaction_cachep; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 90 | struct kmem_cache *btrfs_path_cachep; |
Josef Bacik | dc89e98 | 2011-01-28 17:05:48 -0500 | [diff] [blame] | 91 | struct kmem_cache *btrfs_free_space_cachep; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 92 | |
| 93 | #define S_SHIFT 12 |
David Sterba | 4d4ab6d | 2015-11-19 11:42:31 +0100 | [diff] [blame] | 94 | static const unsigned char btrfs_type_by_mode[S_IFMT >> S_SHIFT] = { |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 95 | [S_IFREG >> S_SHIFT] = BTRFS_FT_REG_FILE, |
| 96 | [S_IFDIR >> S_SHIFT] = BTRFS_FT_DIR, |
| 97 | [S_IFCHR >> S_SHIFT] = BTRFS_FT_CHRDEV, |
| 98 | [S_IFBLK >> S_SHIFT] = BTRFS_FT_BLKDEV, |
| 99 | [S_IFIFO >> S_SHIFT] = BTRFS_FT_FIFO, |
| 100 | [S_IFSOCK >> S_SHIFT] = BTRFS_FT_SOCK, |
| 101 | [S_IFLNK >> S_SHIFT] = BTRFS_FT_SYMLINK, |
| 102 | }; |
| 103 | |
Eric Sandeen | 3972f26 | 2013-01-12 02:57:22 +0000 | [diff] [blame] | 104 | static int btrfs_setsize(struct inode *inode, struct iattr *attr); |
Josef Bacik | a41ad39 | 2011-01-31 15:30:16 -0500 | [diff] [blame] | 105 | static int btrfs_truncate(struct inode *inode); |
Josef Bacik | 5fd0204 | 2012-05-02 14:00:54 -0400 | [diff] [blame] | 106 | static int btrfs_finish_ordered_io(struct btrfs_ordered_extent *ordered_extent); |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 107 | static noinline int cow_file_range(struct inode *inode, |
| 108 | struct page *locked_page, |
Wang Xiaoguang | dda3245 | 2016-07-11 11:05:29 +0800 | [diff] [blame] | 109 | u64 start, u64 end, u64 delalloc_end, |
| 110 | int *page_started, unsigned long *nr_written, |
| 111 | int unlock, struct btrfs_dedupe_hash *hash); |
Josef Bacik | 70c8a91 | 2012-10-11 16:54:30 -0400 | [diff] [blame] | 112 | static struct extent_map *create_pinned_em(struct inode *inode, u64 start, |
| 113 | u64 len, u64 orig_start, |
| 114 | u64 block_start, u64 block_len, |
Josef Bacik | cc95bef | 2013-04-04 14:31:27 -0400 | [diff] [blame] | 115 | u64 orig_block_len, u64 ram_bytes, |
| 116 | int type); |
Josef Bacik | 7b12876 | 2008-07-24 12:17:14 -0400 | [diff] [blame] | 117 | |
Eric Sandeen | 48a3b63 | 2013-04-25 20:41:01 +0000 | [diff] [blame] | 118 | static int btrfs_dirty_inode(struct inode *inode); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 119 | |
Josef Bacik | 6a3891c | 2015-03-16 17:38:52 -0400 | [diff] [blame] | 120 | #ifdef CONFIG_BTRFS_FS_RUN_SANITY_TESTS |
| 121 | void btrfs_test_inode_set_ops(struct inode *inode) |
| 122 | { |
| 123 | BTRFS_I(inode)->io_tree.ops = &btrfs_extent_io_ops; |
| 124 | } |
| 125 | #endif |
| 126 | |
Yan, Zheng | f34f57a | 2009-11-12 09:35:27 +0000 | [diff] [blame] | 127 | static int btrfs_init_inode_security(struct btrfs_trans_handle *trans, |
Eric Paris | 2a7dba3 | 2011-02-01 11:05:39 -0500 | [diff] [blame] | 128 | struct inode *inode, struct inode *dir, |
| 129 | const struct qstr *qstr) |
Jim Owens | 0279b4c | 2009-02-04 09:29:13 -0500 | [diff] [blame] | 130 | { |
| 131 | int err; |
| 132 | |
Yan, Zheng | f34f57a | 2009-11-12 09:35:27 +0000 | [diff] [blame] | 133 | err = btrfs_init_acl(trans, inode, dir); |
Jim Owens | 0279b4c | 2009-02-04 09:29:13 -0500 | [diff] [blame] | 134 | if (!err) |
Eric Paris | 2a7dba3 | 2011-02-01 11:05:39 -0500 | [diff] [blame] | 135 | err = btrfs_xattr_security_init(trans, inode, dir, qstr); |
Jim Owens | 0279b4c | 2009-02-04 09:29:13 -0500 | [diff] [blame] | 136 | return err; |
| 137 | } |
| 138 | |
Chris Mason | d352ac6 | 2008-09-29 15:18:18 -0400 | [diff] [blame] | 139 | /* |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 140 | * this does all the hard work for inserting an inline extent into |
| 141 | * the btree. The caller should have done a btrfs_drop_extents so that |
| 142 | * no overlapping inline items exist in the btree |
| 143 | */ |
Chris Mason | 40f7658 | 2014-05-21 13:35:51 -0700 | [diff] [blame] | 144 | static int insert_inline_extent(struct btrfs_trans_handle *trans, |
Filipe David Borba Manana | 1acae57 | 2014-01-07 11:42:27 +0000 | [diff] [blame] | 145 | struct btrfs_path *path, int extent_inserted, |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 146 | struct btrfs_root *root, struct inode *inode, |
| 147 | u64 start, size_t size, size_t compressed_size, |
Li Zefan | fe3f566 | 2011-03-28 08:30:38 +0000 | [diff] [blame] | 148 | int compress_type, |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 149 | struct page **compressed_pages) |
| 150 | { |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 151 | struct extent_buffer *leaf; |
| 152 | struct page *page = NULL; |
| 153 | char *kaddr; |
| 154 | unsigned long ptr; |
| 155 | struct btrfs_file_extent_item *ei; |
| 156 | int err = 0; |
| 157 | int ret; |
| 158 | size_t cur_size = size; |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 159 | unsigned long offset; |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 160 | |
Li Zefan | fe3f566 | 2011-03-28 08:30:38 +0000 | [diff] [blame] | 161 | if (compressed_size && compressed_pages) |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 162 | cur_size = compressed_size; |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 163 | |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 164 | inode_add_bytes(inode, size); |
Filipe David Borba Manana | 1acae57 | 2014-01-07 11:42:27 +0000 | [diff] [blame] | 165 | |
| 166 | if (!extent_inserted) { |
| 167 | struct btrfs_key key; |
| 168 | size_t datasize; |
| 169 | |
| 170 | key.objectid = btrfs_ino(inode); |
| 171 | key.offset = start; |
David Sterba | 962a298 | 2014-06-04 18:41:45 +0200 | [diff] [blame] | 172 | key.type = BTRFS_EXTENT_DATA_KEY; |
Filipe David Borba Manana | 1acae57 | 2014-01-07 11:42:27 +0000 | [diff] [blame] | 173 | |
| 174 | datasize = btrfs_file_extent_calc_inline_size(cur_size); |
| 175 | path->leave_spinning = 1; |
| 176 | ret = btrfs_insert_empty_item(trans, root, path, &key, |
| 177 | datasize); |
| 178 | if (ret) { |
| 179 | err = ret; |
| 180 | goto fail; |
| 181 | } |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 182 | } |
| 183 | leaf = path->nodes[0]; |
| 184 | ei = btrfs_item_ptr(leaf, path->slots[0], |
| 185 | struct btrfs_file_extent_item); |
| 186 | btrfs_set_file_extent_generation(leaf, ei, trans->transid); |
| 187 | btrfs_set_file_extent_type(leaf, ei, BTRFS_FILE_EXTENT_INLINE); |
| 188 | btrfs_set_file_extent_encryption(leaf, ei, 0); |
| 189 | btrfs_set_file_extent_other_encoding(leaf, ei, 0); |
| 190 | btrfs_set_file_extent_ram_bytes(leaf, ei, size); |
| 191 | ptr = btrfs_file_extent_inline_start(ei); |
| 192 | |
Li Zefan | 261507a0 | 2010-12-17 14:21:50 +0800 | [diff] [blame] | 193 | if (compress_type != BTRFS_COMPRESS_NONE) { |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 194 | struct page *cpage; |
| 195 | int i = 0; |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 196 | while (compressed_size > 0) { |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 197 | cpage = compressed_pages[i]; |
Chris Mason | 5b050f0 | 2008-11-11 09:34:41 -0500 | [diff] [blame] | 198 | cur_size = min_t(unsigned long, compressed_size, |
Kirill A. Shutemov | 09cbfea | 2016-04-01 15:29:47 +0300 | [diff] [blame] | 199 | PAGE_SIZE); |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 200 | |
Cong Wang | 7ac687d | 2011-11-25 23:14:28 +0800 | [diff] [blame] | 201 | kaddr = kmap_atomic(cpage); |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 202 | write_extent_buffer(leaf, kaddr, ptr, cur_size); |
Cong Wang | 7ac687d | 2011-11-25 23:14:28 +0800 | [diff] [blame] | 203 | kunmap_atomic(kaddr); |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 204 | |
| 205 | i++; |
| 206 | ptr += cur_size; |
| 207 | compressed_size -= cur_size; |
| 208 | } |
| 209 | btrfs_set_file_extent_compression(leaf, ei, |
Li Zefan | 261507a0 | 2010-12-17 14:21:50 +0800 | [diff] [blame] | 210 | compress_type); |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 211 | } else { |
| 212 | page = find_get_page(inode->i_mapping, |
Kirill A. Shutemov | 09cbfea | 2016-04-01 15:29:47 +0300 | [diff] [blame] | 213 | start >> PAGE_SHIFT); |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 214 | btrfs_set_file_extent_compression(leaf, ei, 0); |
Cong Wang | 7ac687d | 2011-11-25 23:14:28 +0800 | [diff] [blame] | 215 | kaddr = kmap_atomic(page); |
Kirill A. Shutemov | 09cbfea | 2016-04-01 15:29:47 +0300 | [diff] [blame] | 216 | offset = start & (PAGE_SIZE - 1); |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 217 | write_extent_buffer(leaf, kaddr + offset, ptr, size); |
Cong Wang | 7ac687d | 2011-11-25 23:14:28 +0800 | [diff] [blame] | 218 | kunmap_atomic(kaddr); |
Kirill A. Shutemov | 09cbfea | 2016-04-01 15:29:47 +0300 | [diff] [blame] | 219 | put_page(page); |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 220 | } |
| 221 | btrfs_mark_buffer_dirty(leaf); |
Filipe David Borba Manana | 1acae57 | 2014-01-07 11:42:27 +0000 | [diff] [blame] | 222 | btrfs_release_path(path); |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 223 | |
Yan, Zheng | c216775 | 2009-11-12 09:34:21 +0000 | [diff] [blame] | 224 | /* |
| 225 | * we're an inline extent, so nobody can |
| 226 | * extend the file past i_size without locking |
| 227 | * a page we already have locked. |
| 228 | * |
| 229 | * We must do any isize and inode updates |
| 230 | * before we unlock the pages. Otherwise we |
| 231 | * could end up racing with unlink. |
| 232 | */ |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 233 | BTRFS_I(inode)->disk_i_size = inode->i_size; |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 234 | ret = btrfs_update_inode(trans, root, inode); |
Yan, Zheng | c216775 | 2009-11-12 09:34:21 +0000 | [diff] [blame] | 235 | |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 236 | return ret; |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 237 | fail: |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 238 | return err; |
| 239 | } |
| 240 | |
| 241 | |
| 242 | /* |
| 243 | * conditionally insert an inline extent into the file. This |
| 244 | * does the checks required to make sure the data is small enough |
| 245 | * to fit as an inline extent. |
| 246 | */ |
Josef Bacik | 0036158 | 2013-08-14 14:02:47 -0400 | [diff] [blame] | 247 | static noinline int cow_file_range_inline(struct btrfs_root *root, |
| 248 | struct inode *inode, u64 start, |
| 249 | u64 end, size_t compressed_size, |
| 250 | int compress_type, |
| 251 | struct page **compressed_pages) |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 252 | { |
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; |
Qu Wenruo | fda2832 | 2013-02-26 08:10:22 +0000 | [diff] [blame] | 257 | u64 aligned_end = ALIGN(end, root->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 || |
Chandan Rajendra | 0c29ba9 | 2016-01-21 15:56:01 +0530 | [diff] [blame] | 268 | actual_end > root->sectorsize || |
Wang Shilong | 354877b | 2014-07-17 11:44:11 +0800 | [diff] [blame] | 269 | data_len > BTRFS_MAX_INLINE_DATA_SIZE(root) || |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 270 | (!compressed_size && |
| 271 | (actual_end & (root->sectorsize - 1)) == 0) || |
| 272 | end + 1 < isize || |
| 273 | data_len > root->fs_info->max_inline) { |
| 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 | } |
Josef Bacik | 0036158 | 2013-08-14 14:02:47 -0400 | [diff] [blame] | 286 | trans->block_rsv = &root->fs_info->delalloc_block_rsv; |
| 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); |
Josef Bacik | 0036158 | 2013-08-14 14:02:47 -0400 | [diff] [blame] | 329 | btrfs_end_transaction(trans, root); |
| 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 | { |
| 376 | struct btrfs_root *root = BTRFS_I(inode)->root; |
| 377 | |
| 378 | /* force compress */ |
Jeff Mahoney | 3cdde22 | 2016-06-09 21:38:35 -0400 | [diff] [blame] | 379 | if (btrfs_test_opt(root->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 | 3cdde22 | 2016-06-09 21:38:35 -0400 | [diff] [blame] | 384 | if (btrfs_test_opt(root->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 | |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 391 | /* |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 392 | * we create compressed extents in two phases. The first |
| 393 | * phase compresses a range of pages that have already been |
| 394 | * locked (both pages and state bits are locked). |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 395 | * |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 396 | * This is done inside an ordered work queue, and the compression |
| 397 | * is spread across many cpus. The actual IO submission is step |
| 398 | * two, and the ordered work queue takes care of making sure that |
| 399 | * happens in the same order things were put onto the queue by |
| 400 | * writepages and friends. |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 401 | * |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 402 | * If this code finds it can't get good compression, it puts an |
| 403 | * entry onto the work queue to write the uncompressed bytes. This |
| 404 | * makes sure that both compressed inodes and uncompressed inodes |
Artem Bityutskiy | b257031 | 2012-07-25 18:12:06 +0300 | [diff] [blame] | 405 | * are written in the same order that the flusher thread sent them |
| 406 | * down. |
Chris Mason | d352ac6 | 2008-09-29 15:18:18 -0400 | [diff] [blame] | 407 | */ |
Filipe Manana | c44f649 | 2014-10-09 21:15:44 +0100 | [diff] [blame] | 408 | static noinline void compress_file_range(struct inode *inode, |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 409 | struct page *locked_page, |
| 410 | u64 start, u64 end, |
| 411 | struct async_cow *async_cow, |
| 412 | int *num_added) |
Chris Mason | b888db2 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 413 | { |
| 414 | struct btrfs_root *root = BTRFS_I(inode)->root; |
Chris Mason | db94535 | 2007-10-15 16:15:53 -0400 | [diff] [blame] | 415 | u64 num_bytes; |
Chris Mason | db94535 | 2007-10-15 16:15:53 -0400 | [diff] [blame] | 416 | u64 blocksize = root->sectorsize; |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 417 | u64 actual_end; |
Chris Mason | 42dc7ba | 2008-12-15 11:44:56 -0500 | [diff] [blame] | 418 | u64 isize = i_size_read(inode); |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 419 | int ret = 0; |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 420 | struct page **pages = NULL; |
| 421 | unsigned long nr_pages; |
| 422 | unsigned long nr_pages_ret = 0; |
| 423 | unsigned long total_compressed = 0; |
| 424 | unsigned long total_in = 0; |
Byongho Lee | ee22184 | 2015-12-15 01:42:10 +0900 | [diff] [blame] | 425 | unsigned long max_compressed = SZ_128K; |
| 426 | unsigned long max_uncompressed = SZ_128K; |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 427 | int i; |
| 428 | int will_compress; |
Li Zefan | 261507a0 | 2010-12-17 14:21:50 +0800 | [diff] [blame] | 429 | int compress_type = root->fs_info->compress_type; |
Chris Mason | 4adaa61 | 2013-03-26 13:07:00 -0400 | [diff] [blame] | 430 | int redirty = 0; |
Chris Mason | b888db2 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 431 | |
Liu Bo | 4cb13e5 | 2012-03-29 09:57:45 -0400 | [diff] [blame] | 432 | /* if this is a small write inside eof, kick off a defrag */ |
Byongho Lee | ee22184 | 2015-12-15 01:42:10 +0900 | [diff] [blame] | 433 | if ((end - start + 1) < SZ_16K && |
Liu Bo | 4cb13e5 | 2012-03-29 09:57:45 -0400 | [diff] [blame] | 434 | (start > 0 || end + 1 < BTRFS_I(inode)->disk_i_size)) |
Chris Mason | 4cb5300 | 2011-05-24 15:35:30 -0400 | [diff] [blame] | 435 | btrfs_add_inode_defrag(NULL, inode); |
| 436 | |
Chris Mason | 42dc7ba | 2008-12-15 11:44:56 -0500 | [diff] [blame] | 437 | actual_end = min_t(u64, isize, end + 1); |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 438 | again: |
| 439 | will_compress = 0; |
Kirill A. Shutemov | 09cbfea | 2016-04-01 15:29:47 +0300 | [diff] [blame] | 440 | nr_pages = (end >> PAGE_SHIFT) - (start >> PAGE_SHIFT) + 1; |
| 441 | nr_pages = min_t(unsigned long, nr_pages, SZ_128K / PAGE_SIZE); |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 442 | |
Chris Mason | f03d9301f | 2009-02-04 09:31:06 -0500 | [diff] [blame] | 443 | /* |
| 444 | * we don't want to send crud past the end of i_size through |
| 445 | * compression, that's just a waste of CPU time. So, if the |
| 446 | * end of the file is before the start of our current |
| 447 | * requested range of bytes, we bail out to the uncompressed |
| 448 | * cleanup code that can deal with all of this. |
| 449 | * |
| 450 | * It isn't really the fastest way to fix things, but this is a |
| 451 | * very uncommon corner. |
| 452 | */ |
| 453 | if (actual_end <= start) |
| 454 | goto cleanup_and_bail_uncompressed; |
| 455 | |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 456 | total_compressed = actual_end - start; |
| 457 | |
Shilong Wang | 4bcbb33 | 2014-10-07 18:44:35 -0400 | [diff] [blame] | 458 | /* |
| 459 | * skip compression for a small file range(<=blocksize) that |
Nicholas D Steeves | 0132761 | 2016-05-19 21:18:45 -0400 | [diff] [blame] | 460 | * 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] | 461 | */ |
| 462 | if (total_compressed <= blocksize && |
| 463 | (start > 0 || end + 1 < BTRFS_I(inode)->disk_i_size)) |
| 464 | goto cleanup_and_bail_uncompressed; |
| 465 | |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 466 | /* we want to make sure that amount of ram required to uncompress |
| 467 | * an extent is reasonable, so we limit the total size in ram |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 468 | * of a compressed extent to 128k. This is a crucial number |
| 469 | * because it also controls how easily we can spread reads across |
| 470 | * cpus for decompression. |
| 471 | * |
| 472 | * We also want to make sure the amount of IO required to do |
| 473 | * a random read is reasonably small, so we limit the size of |
| 474 | * a compressed extent to 128k. |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 475 | */ |
| 476 | total_compressed = min(total_compressed, max_uncompressed); |
Qu Wenruo | fda2832 | 2013-02-26 08:10:22 +0000 | [diff] [blame] | 477 | num_bytes = ALIGN(end - start + 1, blocksize); |
Chris Mason | be20aa9 | 2007-12-17 20:14:01 -0500 | [diff] [blame] | 478 | num_bytes = max(blocksize, num_bytes); |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 479 | total_in = 0; |
| 480 | ret = 0; |
Chris Mason | db94535 | 2007-10-15 16:15:53 -0400 | [diff] [blame] | 481 | |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 482 | /* |
| 483 | * we do compression for mount -o compress and when the |
| 484 | * inode has not been flagged as nocompress. This flag can |
| 485 | * change at any time if we discover bad compression ratios. |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 486 | */ |
Wang Shilong | f79707b | 2014-07-17 11:44:09 +0800 | [diff] [blame] | 487 | if (inode_need_compress(inode)) { |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 488 | WARN_ON(pages); |
David Sterba | 31e818f | 2015-02-20 18:00:26 +0100 | [diff] [blame] | 489 | pages = kcalloc(nr_pages, sizeof(struct page *), GFP_NOFS); |
Li Zefan | 560f7d7 | 2011-09-08 10:22:01 +0800 | [diff] [blame] | 490 | if (!pages) { |
| 491 | /* just bail out to the uncompressed code */ |
| 492 | goto cont; |
| 493 | } |
Chris Mason | 179e29e | 2007-11-01 11:28:41 -0400 | [diff] [blame] | 494 | |
Li Zefan | 261507a0 | 2010-12-17 14:21:50 +0800 | [diff] [blame] | 495 | if (BTRFS_I(inode)->force_compress) |
| 496 | compress_type = BTRFS_I(inode)->force_compress; |
| 497 | |
Chris Mason | 4adaa61 | 2013-03-26 13:07:00 -0400 | [diff] [blame] | 498 | /* |
| 499 | * we need to call clear_page_dirty_for_io on each |
| 500 | * page in the range. Otherwise applications with the file |
| 501 | * mmap'd can wander in and change the page contents while |
| 502 | * we are compressing them. |
| 503 | * |
| 504 | * If the compression fails for any reason, we set the pages |
| 505 | * dirty again later on. |
| 506 | */ |
| 507 | extent_range_clear_dirty_for_io(inode, start, end); |
| 508 | redirty = 1; |
Li Zefan | 261507a0 | 2010-12-17 14:21:50 +0800 | [diff] [blame] | 509 | ret = btrfs_compress_pages(compress_type, |
| 510 | inode->i_mapping, start, |
| 511 | total_compressed, pages, |
| 512 | nr_pages, &nr_pages_ret, |
| 513 | &total_in, |
| 514 | &total_compressed, |
| 515 | max_compressed); |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 516 | |
| 517 | if (!ret) { |
| 518 | unsigned long offset = total_compressed & |
Kirill A. Shutemov | 09cbfea | 2016-04-01 15:29:47 +0300 | [diff] [blame] | 519 | (PAGE_SIZE - 1); |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 520 | struct page *page = pages[nr_pages_ret - 1]; |
| 521 | char *kaddr; |
| 522 | |
| 523 | /* zero the tail end of the last page, we might be |
| 524 | * sending it down to disk |
| 525 | */ |
| 526 | if (offset) { |
Cong Wang | 7ac687d | 2011-11-25 23:14:28 +0800 | [diff] [blame] | 527 | kaddr = kmap_atomic(page); |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 528 | memset(kaddr + offset, 0, |
Kirill A. Shutemov | 09cbfea | 2016-04-01 15:29:47 +0300 | [diff] [blame] | 529 | PAGE_SIZE - offset); |
Cong Wang | 7ac687d | 2011-11-25 23:14:28 +0800 | [diff] [blame] | 530 | kunmap_atomic(kaddr); |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 531 | } |
| 532 | will_compress = 1; |
| 533 | } |
| 534 | } |
Li Zefan | 560f7d7 | 2011-09-08 10:22:01 +0800 | [diff] [blame] | 535 | cont: |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 536 | if (start == 0) { |
| 537 | /* lets try to make an inline extent */ |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 538 | if (ret || total_in < (actual_end - start)) { |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 539 | /* we didn't compress the entire range, try |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 540 | * to make an uncompressed inline extent. |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 541 | */ |
Josef Bacik | 0036158 | 2013-08-14 14:02:47 -0400 | [diff] [blame] | 542 | ret = cow_file_range_inline(root, inode, start, end, |
| 543 | 0, 0, NULL); |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 544 | } else { |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 545 | /* try making a compressed inline extent */ |
Josef Bacik | 0036158 | 2013-08-14 14:02:47 -0400 | [diff] [blame] | 546 | ret = cow_file_range_inline(root, inode, start, end, |
Li Zefan | fe3f566 | 2011-03-28 08:30:38 +0000 | [diff] [blame] | 547 | total_compressed, |
| 548 | compress_type, pages); |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 549 | } |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 550 | if (ret <= 0) { |
Josef Bacik | 151a41b | 2013-07-29 13:22:24 -0400 | [diff] [blame] | 551 | unsigned long clear_flags = EXTENT_DELALLOC | |
| 552 | EXTENT_DEFRAG; |
Filipe Manana | e6eb431 | 2014-10-10 10:45:12 +0100 | [diff] [blame] | 553 | unsigned long page_error_op; |
| 554 | |
Josef Bacik | 151a41b | 2013-07-29 13:22:24 -0400 | [diff] [blame] | 555 | clear_flags |= (ret < 0) ? EXTENT_DO_ACCOUNTING : 0; |
Filipe Manana | e6eb431 | 2014-10-10 10:45:12 +0100 | [diff] [blame] | 556 | page_error_op = ret < 0 ? PAGE_SET_ERROR : 0; |
Josef Bacik | 151a41b | 2013-07-29 13:22:24 -0400 | [diff] [blame] | 557 | |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 558 | /* |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 559 | * inline extent creation worked or returned error, |
| 560 | * we don't need to create any more async work items. |
| 561 | * Unlock and free up our temp pages. |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 562 | */ |
Josef Bacik | c2790a2 | 2013-07-29 11:20:47 -0400 | [diff] [blame] | 563 | extent_clear_unlock_delalloc(inode, start, end, NULL, |
Josef Bacik | 151a41b | 2013-07-29 13:22:24 -0400 | [diff] [blame] | 564 | clear_flags, PAGE_UNLOCK | |
Josef Bacik | c2790a2 | 2013-07-29 11:20:47 -0400 | [diff] [blame] | 565 | PAGE_CLEAR_DIRTY | |
| 566 | PAGE_SET_WRITEBACK | |
Filipe Manana | e6eb431 | 2014-10-10 10:45:12 +0100 | [diff] [blame] | 567 | page_error_op | |
Josef Bacik | c2790a2 | 2013-07-29 11:20:47 -0400 | [diff] [blame] | 568 | PAGE_END_WRITEBACK); |
Wang Xiaoguang | 1851309 | 2016-07-25 15:51:40 +0800 | [diff] [blame] | 569 | btrfs_free_reserved_data_space_noquota(inode, start, |
| 570 | end - start + 1); |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 571 | goto free_pages_out; |
| 572 | } |
| 573 | } |
| 574 | |
| 575 | if (will_compress) { |
| 576 | /* |
| 577 | * we aren't doing an inline extent round the compressed size |
| 578 | * up to a block size boundary so the allocator does sane |
| 579 | * things |
| 580 | */ |
Qu Wenruo | fda2832 | 2013-02-26 08:10:22 +0000 | [diff] [blame] | 581 | total_compressed = ALIGN(total_compressed, blocksize); |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 582 | |
| 583 | /* |
| 584 | * one last check to make sure the compression is really a |
| 585 | * win, compare the page count read with the blocks on disk |
| 586 | */ |
Kirill A. Shutemov | 09cbfea | 2016-04-01 15:29:47 +0300 | [diff] [blame] | 587 | total_in = ALIGN(total_in, PAGE_SIZE); |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 588 | if (total_compressed >= total_in) { |
| 589 | will_compress = 0; |
| 590 | } else { |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 591 | num_bytes = total_in; |
Ashish Samant | c8bb0c8 | 2016-03-25 19:01:33 -0700 | [diff] [blame] | 592 | *num_added += 1; |
| 593 | |
| 594 | /* |
| 595 | * The async work queues will take care of doing actual |
| 596 | * allocation on disk for these compressed pages, and |
| 597 | * will submit them to the elevator. |
| 598 | */ |
| 599 | add_async_extent(async_cow, start, num_bytes, |
| 600 | total_compressed, pages, nr_pages_ret, |
| 601 | compress_type); |
| 602 | |
| 603 | if (start + num_bytes < end) { |
| 604 | start += num_bytes; |
| 605 | pages = NULL; |
| 606 | cond_resched(); |
| 607 | goto again; |
| 608 | } |
| 609 | return; |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 610 | } |
| 611 | } |
Ashish Samant | c8bb0c8 | 2016-03-25 19:01:33 -0700 | [diff] [blame] | 612 | if (pages) { |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 613 | /* |
| 614 | * the compression code ran but failed to make things smaller, |
| 615 | * free any pages it allocated and our page pointer array |
| 616 | */ |
| 617 | for (i = 0; i < nr_pages_ret; i++) { |
Chris Mason | 70b99e6 | 2008-10-31 12:46:39 -0400 | [diff] [blame] | 618 | WARN_ON(pages[i]->mapping); |
Kirill A. Shutemov | 09cbfea | 2016-04-01 15:29:47 +0300 | [diff] [blame] | 619 | put_page(pages[i]); |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 620 | } |
| 621 | kfree(pages); |
| 622 | pages = NULL; |
| 623 | total_compressed = 0; |
| 624 | nr_pages_ret = 0; |
| 625 | |
| 626 | /* flag the file so we don't compress in the future */ |
Jeff Mahoney | 3cdde22 | 2016-06-09 21:38:35 -0400 | [diff] [blame] | 627 | if (!btrfs_test_opt(root->fs_info, FORCE_COMPRESS) && |
Chris Mason | 1e701a3 | 2010-03-11 09:42:04 -0500 | [diff] [blame] | 628 | !(BTRFS_I(inode)->force_compress)) { |
Chris Mason | a555f81 | 2010-01-28 16:18:15 -0500 | [diff] [blame] | 629 | BTRFS_I(inode)->flags |= BTRFS_INODE_NOCOMPRESS; |
Chris Mason | 1e701a3 | 2010-03-11 09:42:04 -0500 | [diff] [blame] | 630 | } |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 631 | } |
Chris Mason | f03d9301f | 2009-02-04 09:31:06 -0500 | [diff] [blame] | 632 | cleanup_and_bail_uncompressed: |
Ashish Samant | c8bb0c8 | 2016-03-25 19:01:33 -0700 | [diff] [blame] | 633 | /* |
| 634 | * No compression, but we still need to write the pages in the file |
| 635 | * we've been given so far. redirty the locked page if it corresponds |
| 636 | * to our extent and set things up for the async work queue to run |
| 637 | * cow_file_range to do the normal delalloc dance. |
| 638 | */ |
| 639 | if (page_offset(locked_page) >= start && |
| 640 | page_offset(locked_page) <= end) |
| 641 | __set_page_dirty_nobuffers(locked_page); |
| 642 | /* unlocked later on in the async handlers */ |
| 643 | |
| 644 | if (redirty) |
| 645 | extent_range_redirty_for_io(inode, start, end); |
| 646 | add_async_extent(async_cow, start, end - start + 1, 0, NULL, 0, |
| 647 | BTRFS_COMPRESS_NONE); |
| 648 | *num_added += 1; |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 649 | |
Filipe Manana | c44f649 | 2014-10-09 21:15:44 +0100 | [diff] [blame] | 650 | return; |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 651 | |
| 652 | free_pages_out: |
| 653 | for (i = 0; i < nr_pages_ret; i++) { |
| 654 | WARN_ON(pages[i]->mapping); |
Kirill A. Shutemov | 09cbfea | 2016-04-01 15:29:47 +0300 | [diff] [blame] | 655 | put_page(pages[i]); |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 656 | } |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 657 | kfree(pages); |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 658 | } |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 659 | |
Filipe Manana | 40ae837 | 2014-10-06 22:14:24 +0100 | [diff] [blame] | 660 | static void free_async_extent_pages(struct async_extent *async_extent) |
| 661 | { |
| 662 | int i; |
| 663 | |
| 664 | if (!async_extent->pages) |
| 665 | return; |
| 666 | |
| 667 | for (i = 0; i < async_extent->nr_pages; i++) { |
| 668 | WARN_ON(async_extent->pages[i]->mapping); |
Kirill A. Shutemov | 09cbfea | 2016-04-01 15:29:47 +0300 | [diff] [blame] | 669 | put_page(async_extent->pages[i]); |
Filipe Manana | 40ae837 | 2014-10-06 22:14:24 +0100 | [diff] [blame] | 670 | } |
| 671 | kfree(async_extent->pages); |
| 672 | async_extent->nr_pages = 0; |
| 673 | async_extent->pages = NULL; |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 674 | } |
| 675 | |
| 676 | /* |
| 677 | * phase two of compressed writeback. This is the ordered portion |
| 678 | * of the code, which only gets called in the order the work was |
| 679 | * queued. We walk all the async extents created by compress_file_range |
| 680 | * and send them down to the disk. |
| 681 | */ |
Filipe Manana | dec8f17 | 2014-10-06 22:14:26 +0100 | [diff] [blame] | 682 | static noinline void submit_compressed_extents(struct inode *inode, |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 683 | struct async_cow *async_cow) |
| 684 | { |
| 685 | struct async_extent *async_extent; |
| 686 | u64 alloc_hint = 0; |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 687 | struct btrfs_key ins; |
| 688 | struct extent_map *em; |
| 689 | struct btrfs_root *root = BTRFS_I(inode)->root; |
| 690 | struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree; |
| 691 | struct extent_io_tree *io_tree; |
Josef Bacik | f5a84ee3 | 2009-11-10 21:23:48 -0500 | [diff] [blame] | 692 | int ret = 0; |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 693 | |
Josef Bacik | 3e04e7f | 2013-02-06 16:49:15 -0500 | [diff] [blame] | 694 | again: |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 695 | while (!list_empty(&async_cow->extents)) { |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 696 | async_extent = list_entry(async_cow->extents.next, |
| 697 | struct async_extent, list); |
| 698 | list_del(&async_extent->list); |
| 699 | |
| 700 | io_tree = &BTRFS_I(inode)->io_tree; |
| 701 | |
Josef Bacik | f5a84ee3 | 2009-11-10 21:23:48 -0500 | [diff] [blame] | 702 | retry: |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 703 | /* did the compression code fall back to uncompressed IO? */ |
| 704 | if (!async_extent->pages) { |
| 705 | int page_started = 0; |
| 706 | unsigned long nr_written = 0; |
| 707 | |
| 708 | lock_extent(io_tree, async_extent->start, |
Josef Bacik | 2ac55d4 | 2010-02-03 19:33:23 +0000 | [diff] [blame] | 709 | async_extent->start + |
Jeff Mahoney | d008237 | 2012-03-01 14:57:19 +0100 | [diff] [blame] | 710 | async_extent->ram_size - 1); |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 711 | |
| 712 | /* allocate blocks */ |
Josef Bacik | f5a84ee3 | 2009-11-10 21:23:48 -0500 | [diff] [blame] | 713 | ret = cow_file_range(inode, async_cow->locked_page, |
| 714 | async_extent->start, |
| 715 | async_extent->start + |
| 716 | async_extent->ram_size - 1, |
Wang Xiaoguang | dda3245 | 2016-07-11 11:05:29 +0800 | [diff] [blame] | 717 | async_extent->start + |
| 718 | async_extent->ram_size - 1, |
| 719 | &page_started, &nr_written, 0, |
| 720 | NULL); |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 721 | |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 722 | /* JDM XXX */ |
| 723 | |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 724 | /* |
| 725 | * if page_started, cow_file_range inserted an |
| 726 | * inline extent and took care of all the unlocking |
| 727 | * and IO for us. Otherwise, we need to submit |
| 728 | * all those pages down to the drive. |
| 729 | */ |
Josef Bacik | f5a84ee3 | 2009-11-10 21:23:48 -0500 | [diff] [blame] | 730 | if (!page_started && !ret) |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 731 | extent_write_locked_range(io_tree, |
| 732 | inode, async_extent->start, |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 733 | async_extent->start + |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 734 | async_extent->ram_size - 1, |
| 735 | btrfs_get_extent, |
| 736 | WB_SYNC_ALL); |
Josef Bacik | 3e04e7f | 2013-02-06 16:49:15 -0500 | [diff] [blame] | 737 | else if (ret) |
| 738 | unlock_page(async_cow->locked_page); |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 739 | kfree(async_extent); |
| 740 | cond_resched(); |
| 741 | continue; |
| 742 | } |
| 743 | |
| 744 | lock_extent(io_tree, async_extent->start, |
Jeff Mahoney | d008237 | 2012-03-01 14:57:19 +0100 | [diff] [blame] | 745 | async_extent->start + async_extent->ram_size - 1); |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 746 | |
Wang Xiaoguang | 1851309 | 2016-07-25 15:51:40 +0800 | [diff] [blame] | 747 | ret = btrfs_reserve_extent(root, async_extent->ram_size, |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 748 | async_extent->compressed_size, |
| 749 | async_extent->compressed_size, |
Miao Xie | e570fd2 | 2014-06-19 10:42:50 +0800 | [diff] [blame] | 750 | 0, alloc_hint, &ins, 1, 1); |
Josef Bacik | f5a84ee3 | 2009-11-10 21:23:48 -0500 | [diff] [blame] | 751 | if (ret) { |
Filipe Manana | 40ae837 | 2014-10-06 22:14:24 +0100 | [diff] [blame] | 752 | free_async_extent_pages(async_extent); |
Josef Bacik | 3e04e7f | 2013-02-06 16:49:15 -0500 | [diff] [blame] | 753 | |
Josef Bacik | fdf8e2e | 2013-06-14 16:58:23 -0400 | [diff] [blame] | 754 | if (ret == -ENOSPC) { |
| 755 | unlock_extent(io_tree, async_extent->start, |
| 756 | async_extent->start + |
| 757 | async_extent->ram_size - 1); |
Liu Bo | ce62003 | 2014-07-24 22:48:05 +0800 | [diff] [blame] | 758 | |
| 759 | /* |
| 760 | * we need to redirty the pages if we decide to |
| 761 | * fallback to uncompressed IO, otherwise we |
| 762 | * will not submit these pages down to lower |
| 763 | * layers. |
| 764 | */ |
| 765 | extent_range_redirty_for_io(inode, |
| 766 | async_extent->start, |
| 767 | async_extent->start + |
| 768 | async_extent->ram_size - 1); |
| 769 | |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 770 | goto retry; |
Josef Bacik | fdf8e2e | 2013-06-14 16:58:23 -0400 | [diff] [blame] | 771 | } |
Josef Bacik | 3e04e7f | 2013-02-06 16:49:15 -0500 | [diff] [blame] | 772 | goto out_free; |
Josef Bacik | f5a84ee3 | 2009-11-10 21:23:48 -0500 | [diff] [blame] | 773 | } |
Yan, Zheng | c216775 | 2009-11-12 09:34:21 +0000 | [diff] [blame] | 774 | /* |
| 775 | * here we're doing allocation and writeback of the |
| 776 | * compressed pages |
| 777 | */ |
| 778 | btrfs_drop_extent_cache(inode, async_extent->start, |
| 779 | async_extent->start + |
| 780 | async_extent->ram_size - 1, 0); |
| 781 | |
David Sterba | 172ddd6 | 2011-04-21 00:48:27 +0200 | [diff] [blame] | 782 | em = alloc_extent_map(); |
Liu Bo | b9aa55b | 2013-05-14 02:12:15 +0000 | [diff] [blame] | 783 | if (!em) { |
| 784 | ret = -ENOMEM; |
Josef Bacik | 3e04e7f | 2013-02-06 16:49:15 -0500 | [diff] [blame] | 785 | goto out_free_reserve; |
Liu Bo | b9aa55b | 2013-05-14 02:12:15 +0000 | [diff] [blame] | 786 | } |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 787 | em->start = async_extent->start; |
| 788 | em->len = async_extent->ram_size; |
Chris Mason | 445a694 | 2008-11-10 11:53:33 -0500 | [diff] [blame] | 789 | em->orig_start = em->start; |
Josef Bacik | 2ab28f3 | 2012-10-12 15:27:49 -0400 | [diff] [blame] | 790 | em->mod_start = em->start; |
| 791 | em->mod_len = em->len; |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 792 | |
| 793 | em->block_start = ins.objectid; |
| 794 | em->block_len = ins.offset; |
Josef Bacik | b493968 | 2012-12-03 10:31:19 -0500 | [diff] [blame] | 795 | em->orig_block_len = ins.offset; |
Josef Bacik | cc95bef | 2013-04-04 14:31:27 -0400 | [diff] [blame] | 796 | em->ram_bytes = async_extent->ram_size; |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 797 | em->bdev = root->fs_info->fs_devices->latest_bdev; |
Li Zefan | 261507a0 | 2010-12-17 14:21:50 +0800 | [diff] [blame] | 798 | em->compress_type = async_extent->compress_type; |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 799 | set_bit(EXTENT_FLAG_PINNED, &em->flags); |
| 800 | set_bit(EXTENT_FLAG_COMPRESSED, &em->flags); |
Josef Bacik | 70c8a91 | 2012-10-11 16:54:30 -0400 | [diff] [blame] | 801 | em->generation = -1; |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 802 | |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 803 | while (1) { |
Chris Mason | 890871b | 2009-09-02 16:24:52 -0400 | [diff] [blame] | 804 | write_lock(&em_tree->lock); |
Josef Bacik | 09a2a8f9 | 2013-04-05 16:51:15 -0400 | [diff] [blame] | 805 | ret = add_extent_mapping(em_tree, em, 1); |
Chris Mason | 890871b | 2009-09-02 16:24:52 -0400 | [diff] [blame] | 806 | write_unlock(&em_tree->lock); |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 807 | if (ret != -EEXIST) { |
| 808 | free_extent_map(em); |
| 809 | break; |
| 810 | } |
| 811 | btrfs_drop_extent_cache(inode, async_extent->start, |
| 812 | async_extent->start + |
| 813 | async_extent->ram_size - 1, 0); |
| 814 | } |
| 815 | |
Josef Bacik | 3e04e7f | 2013-02-06 16:49:15 -0500 | [diff] [blame] | 816 | if (ret) |
| 817 | goto out_free_reserve; |
| 818 | |
Li Zefan | 261507a0 | 2010-12-17 14:21:50 +0800 | [diff] [blame] | 819 | ret = btrfs_add_ordered_extent_compress(inode, |
| 820 | async_extent->start, |
| 821 | ins.objectid, |
| 822 | async_extent->ram_size, |
| 823 | ins.offset, |
| 824 | BTRFS_ORDERED_COMPRESSED, |
| 825 | async_extent->compress_type); |
Filipe Manana | d9f8596 | 2014-08-25 10:43:00 +0100 | [diff] [blame] | 826 | if (ret) { |
| 827 | btrfs_drop_extent_cache(inode, async_extent->start, |
| 828 | async_extent->start + |
| 829 | async_extent->ram_size - 1, 0); |
Josef Bacik | 3e04e7f | 2013-02-06 16:49:15 -0500 | [diff] [blame] | 830 | goto out_free_reserve; |
Filipe Manana | d9f8596 | 2014-08-25 10:43:00 +0100 | [diff] [blame] | 831 | } |
Filipe Manana | 9cfa3e3 | 2016-04-26 15:39:32 +0100 | [diff] [blame] | 832 | btrfs_dec_block_group_reservations(root->fs_info, ins.objectid); |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 833 | |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 834 | /* |
| 835 | * clear dirty, set writeback and unlock the pages. |
| 836 | */ |
Josef Bacik | c2790a2 | 2013-07-29 11:20:47 -0400 | [diff] [blame] | 837 | extent_clear_unlock_delalloc(inode, async_extent->start, |
Chris Mason | a791e35 | 2009-10-08 11:27:10 -0400 | [diff] [blame] | 838 | async_extent->start + |
| 839 | async_extent->ram_size - 1, |
Josef Bacik | 151a41b | 2013-07-29 13:22:24 -0400 | [diff] [blame] | 840 | NULL, EXTENT_LOCKED | EXTENT_DELALLOC, |
| 841 | PAGE_UNLOCK | PAGE_CLEAR_DIRTY | |
Josef Bacik | c2790a2 | 2013-07-29 11:20:47 -0400 | [diff] [blame] | 842 | PAGE_SET_WRITEBACK); |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 843 | ret = btrfs_submit_compressed_write(inode, |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 844 | async_extent->start, |
| 845 | async_extent->ram_size, |
| 846 | ins.objectid, |
| 847 | ins.offset, async_extent->pages, |
| 848 | async_extent->nr_pages); |
Filipe Manana | fce2a4e | 2014-10-06 22:14:23 +0100 | [diff] [blame] | 849 | if (ret) { |
| 850 | struct extent_io_tree *tree = &BTRFS_I(inode)->io_tree; |
| 851 | struct page *p = async_extent->pages[0]; |
| 852 | const u64 start = async_extent->start; |
| 853 | const u64 end = start + async_extent->ram_size - 1; |
| 854 | |
| 855 | p->mapping = inode->i_mapping; |
| 856 | tree->ops->writepage_end_io_hook(p, start, end, |
| 857 | NULL, 0); |
| 858 | p->mapping = NULL; |
| 859 | extent_clear_unlock_delalloc(inode, start, end, NULL, 0, |
| 860 | PAGE_END_WRITEBACK | |
| 861 | PAGE_SET_ERROR); |
Filipe Manana | 40ae837 | 2014-10-06 22:14:24 +0100 | [diff] [blame] | 862 | free_async_extent_pages(async_extent); |
Filipe Manana | fce2a4e | 2014-10-06 22:14:23 +0100 | [diff] [blame] | 863 | } |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 864 | alloc_hint = ins.objectid + ins.offset; |
| 865 | kfree(async_extent); |
| 866 | cond_resched(); |
| 867 | } |
Filipe Manana | dec8f17 | 2014-10-06 22:14:26 +0100 | [diff] [blame] | 868 | return; |
Josef Bacik | 3e04e7f | 2013-02-06 16:49:15 -0500 | [diff] [blame] | 869 | out_free_reserve: |
Filipe Manana | 9cfa3e3 | 2016-04-26 15:39:32 +0100 | [diff] [blame] | 870 | btrfs_dec_block_group_reservations(root->fs_info, ins.objectid); |
Miao Xie | e570fd2 | 2014-06-19 10:42:50 +0800 | [diff] [blame] | 871 | btrfs_free_reserved_extent(root, ins.objectid, ins.offset, 1); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 872 | out_free: |
Josef Bacik | c2790a2 | 2013-07-29 11:20:47 -0400 | [diff] [blame] | 873 | extent_clear_unlock_delalloc(inode, async_extent->start, |
Josef Bacik | 3e04e7f | 2013-02-06 16:49:15 -0500 | [diff] [blame] | 874 | async_extent->start + |
| 875 | async_extent->ram_size - 1, |
Josef Bacik | c2790a2 | 2013-07-29 11:20:47 -0400 | [diff] [blame] | 876 | NULL, EXTENT_LOCKED | EXTENT_DELALLOC | |
Josef Bacik | 151a41b | 2013-07-29 13:22:24 -0400 | [diff] [blame] | 877 | EXTENT_DEFRAG | EXTENT_DO_ACCOUNTING, |
| 878 | PAGE_UNLOCK | PAGE_CLEAR_DIRTY | |
Filipe Manana | 704de49 | 2014-10-06 22:14:22 +0100 | [diff] [blame] | 879 | PAGE_SET_WRITEBACK | PAGE_END_WRITEBACK | |
| 880 | PAGE_SET_ERROR); |
Filipe Manana | 40ae837 | 2014-10-06 22:14:24 +0100 | [diff] [blame] | 881 | free_async_extent_pages(async_extent); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 882 | kfree(async_extent); |
Josef Bacik | 3e04e7f | 2013-02-06 16:49:15 -0500 | [diff] [blame] | 883 | goto again; |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 884 | } |
| 885 | |
Josef Bacik | 4b46fce | 2010-05-23 11:00:55 -0400 | [diff] [blame] | 886 | static u64 get_extent_allocation_hint(struct inode *inode, u64 start, |
| 887 | u64 num_bytes) |
| 888 | { |
| 889 | struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree; |
| 890 | struct extent_map *em; |
| 891 | u64 alloc_hint = 0; |
| 892 | |
| 893 | read_lock(&em_tree->lock); |
| 894 | em = search_extent_mapping(em_tree, start, num_bytes); |
| 895 | if (em) { |
| 896 | /* |
| 897 | * if block start isn't an actual block number then find the |
| 898 | * first block in this inode and use that as a hint. If that |
| 899 | * block is also bogus then just don't worry about it. |
| 900 | */ |
| 901 | if (em->block_start >= EXTENT_MAP_LAST_BYTE) { |
| 902 | free_extent_map(em); |
| 903 | em = search_extent_mapping(em_tree, 0, 0); |
| 904 | if (em && em->block_start < EXTENT_MAP_LAST_BYTE) |
| 905 | alloc_hint = em->block_start; |
| 906 | if (em) |
| 907 | free_extent_map(em); |
| 908 | } else { |
| 909 | alloc_hint = em->block_start; |
| 910 | free_extent_map(em); |
| 911 | } |
| 912 | } |
| 913 | read_unlock(&em_tree->lock); |
| 914 | |
| 915 | return alloc_hint; |
| 916 | } |
| 917 | |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 918 | /* |
| 919 | * when extent_io.c finds a delayed allocation range in the file, |
| 920 | * the call backs end up in this code. The basic idea is to |
| 921 | * allocate extents on disk for the range, and create ordered data structs |
| 922 | * in ram to track those extents. |
| 923 | * |
| 924 | * locked_page is the page that writepage had locked already. We use |
| 925 | * it to make sure we don't do extra locks or unlocks. |
| 926 | * |
| 927 | * *page_started is set to one if we unlock locked_page and do everything |
| 928 | * required to start IO on it. It may be clean and already done with |
| 929 | * IO when we return. |
| 930 | */ |
Josef Bacik | 0036158 | 2013-08-14 14:02:47 -0400 | [diff] [blame] | 931 | static noinline int cow_file_range(struct inode *inode, |
| 932 | struct page *locked_page, |
Wang Xiaoguang | dda3245 | 2016-07-11 11:05:29 +0800 | [diff] [blame] | 933 | u64 start, u64 end, u64 delalloc_end, |
| 934 | int *page_started, unsigned long *nr_written, |
| 935 | int unlock, struct btrfs_dedupe_hash *hash) |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 936 | { |
Josef Bacik | 0036158 | 2013-08-14 14:02:47 -0400 | [diff] [blame] | 937 | struct btrfs_root *root = BTRFS_I(inode)->root; |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 938 | u64 alloc_hint = 0; |
| 939 | u64 num_bytes; |
| 940 | unsigned long ram_size; |
| 941 | u64 disk_num_bytes; |
| 942 | u64 cur_alloc_size; |
| 943 | u64 blocksize = root->sectorsize; |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 944 | struct btrfs_key ins; |
| 945 | struct extent_map *em; |
| 946 | struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree; |
| 947 | int ret = 0; |
| 948 | |
Josef Bacik | 02ecd2c | 2013-10-25 16:19:08 -0400 | [diff] [blame] | 949 | if (btrfs_is_free_space_inode(inode)) { |
| 950 | WARN_ON_ONCE(1); |
Josef Bacik | 29bce2f | 2014-02-07 12:21:23 -0500 | [diff] [blame] | 951 | ret = -EINVAL; |
| 952 | goto out_unlock; |
Josef Bacik | 02ecd2c | 2013-10-25 16:19:08 -0400 | [diff] [blame] | 953 | } |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 954 | |
Qu Wenruo | fda2832 | 2013-02-26 08:10:22 +0000 | [diff] [blame] | 955 | num_bytes = ALIGN(end - start + 1, blocksize); |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 956 | num_bytes = max(blocksize, num_bytes); |
| 957 | disk_num_bytes = num_bytes; |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 958 | |
Chris Mason | 4cb5300 | 2011-05-24 15:35:30 -0400 | [diff] [blame] | 959 | /* if this is a small write inside eof, kick off defrag */ |
Byongho Lee | ee22184 | 2015-12-15 01:42:10 +0900 | [diff] [blame] | 960 | if (num_bytes < SZ_64K && |
Liu Bo | 4cb13e5 | 2012-03-29 09:57:45 -0400 | [diff] [blame] | 961 | (start > 0 || end + 1 < BTRFS_I(inode)->disk_i_size)) |
Josef Bacik | 0036158 | 2013-08-14 14:02:47 -0400 | [diff] [blame] | 962 | btrfs_add_inode_defrag(NULL, inode); |
Chris Mason | 4cb5300 | 2011-05-24 15:35:30 -0400 | [diff] [blame] | 963 | |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 964 | if (start == 0) { |
| 965 | /* lets try to make an inline extent */ |
Josef Bacik | 0036158 | 2013-08-14 14:02:47 -0400 | [diff] [blame] | 966 | ret = cow_file_range_inline(root, inode, start, end, 0, 0, |
| 967 | NULL); |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 968 | if (ret == 0) { |
Josef Bacik | c2790a2 | 2013-07-29 11:20:47 -0400 | [diff] [blame] | 969 | extent_clear_unlock_delalloc(inode, start, end, NULL, |
| 970 | EXTENT_LOCKED | EXTENT_DELALLOC | |
Josef Bacik | 151a41b | 2013-07-29 13:22:24 -0400 | [diff] [blame] | 971 | EXTENT_DEFRAG, PAGE_UNLOCK | |
Josef Bacik | c2790a2 | 2013-07-29 11:20:47 -0400 | [diff] [blame] | 972 | PAGE_CLEAR_DIRTY | PAGE_SET_WRITEBACK | |
| 973 | PAGE_END_WRITEBACK); |
Wang Xiaoguang | 1851309 | 2016-07-25 15:51:40 +0800 | [diff] [blame] | 974 | btrfs_free_reserved_data_space_noquota(inode, start, |
| 975 | end - start + 1); |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 976 | *nr_written = *nr_written + |
Kirill A. Shutemov | 09cbfea | 2016-04-01 15:29:47 +0300 | [diff] [blame] | 977 | (end - start + PAGE_SIZE) / PAGE_SIZE; |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 978 | *page_started = 1; |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 979 | goto out; |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 980 | } else if (ret < 0) { |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 981 | goto out_unlock; |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 982 | } |
| 983 | } |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 984 | |
| 985 | BUG_ON(disk_num_bytes > |
David Sterba | 6c41761 | 2011-04-13 15:41:04 +0200 | [diff] [blame] | 986 | btrfs_super_total_bytes(root->fs_info->super_copy)); |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 987 | |
Josef Bacik | 4b46fce | 2010-05-23 11:00:55 -0400 | [diff] [blame] | 988 | alloc_hint = get_extent_allocation_hint(inode, start, num_bytes); |
Zheng Yan | 5b21f2e | 2008-09-26 10:05:38 -0400 | [diff] [blame] | 989 | btrfs_drop_extent_cache(inode, start, start + num_bytes - 1, 0); |
Chris Mason | 3b95151 | 2008-04-17 11:29:12 -0400 | [diff] [blame] | 990 | |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 991 | while (disk_num_bytes > 0) { |
Chris Mason | a791e35 | 2009-10-08 11:27:10 -0400 | [diff] [blame] | 992 | unsigned long op; |
| 993 | |
Josef Bacik | 287a0ab | 2010-03-19 18:07:23 +0000 | [diff] [blame] | 994 | cur_alloc_size = disk_num_bytes; |
Wang Xiaoguang | 1851309 | 2016-07-25 15:51:40 +0800 | [diff] [blame] | 995 | ret = btrfs_reserve_extent(root, cur_alloc_size, cur_alloc_size, |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 996 | root->sectorsize, 0, alloc_hint, |
Miao Xie | e570fd2 | 2014-06-19 10:42:50 +0800 | [diff] [blame] | 997 | &ins, 1, 1); |
Josef Bacik | 0036158 | 2013-08-14 14:02:47 -0400 | [diff] [blame] | 998 | if (ret < 0) |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 999 | goto out_unlock; |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 1000 | |
David Sterba | 172ddd6 | 2011-04-21 00:48:27 +0200 | [diff] [blame] | 1001 | em = alloc_extent_map(); |
Liu Bo | b9aa55b | 2013-05-14 02:12:15 +0000 | [diff] [blame] | 1002 | if (!em) { |
| 1003 | ret = -ENOMEM; |
Liu Bo | ace68ba | 2013-04-22 10:53:47 +0000 | [diff] [blame] | 1004 | goto out_reserve; |
Liu Bo | b9aa55b | 2013-05-14 02:12:15 +0000 | [diff] [blame] | 1005 | } |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 1006 | em->start = start; |
Chris Mason | 445a694 | 2008-11-10 11:53:33 -0500 | [diff] [blame] | 1007 | em->orig_start = em->start; |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 1008 | ram_size = ins.offset; |
| 1009 | em->len = ins.offset; |
Josef Bacik | 2ab28f3 | 2012-10-12 15:27:49 -0400 | [diff] [blame] | 1010 | em->mod_start = em->start; |
| 1011 | em->mod_len = em->len; |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 1012 | |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 1013 | em->block_start = ins.objectid; |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 1014 | em->block_len = ins.offset; |
Josef Bacik | b493968 | 2012-12-03 10:31:19 -0500 | [diff] [blame] | 1015 | em->orig_block_len = ins.offset; |
Josef Bacik | cc95bef | 2013-04-04 14:31:27 -0400 | [diff] [blame] | 1016 | em->ram_bytes = ram_size; |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 1017 | em->bdev = root->fs_info->fs_devices->latest_bdev; |
Chris Mason | 7f3c74f | 2008-07-18 12:01:11 -0400 | [diff] [blame] | 1018 | set_bit(EXTENT_FLAG_PINNED, &em->flags); |
Josef Bacik | 70c8a91 | 2012-10-11 16:54:30 -0400 | [diff] [blame] | 1019 | em->generation = -1; |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 1020 | |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 1021 | while (1) { |
Chris Mason | 890871b | 2009-09-02 16:24:52 -0400 | [diff] [blame] | 1022 | write_lock(&em_tree->lock); |
Josef Bacik | 09a2a8f9 | 2013-04-05 16:51:15 -0400 | [diff] [blame] | 1023 | ret = add_extent_mapping(em_tree, em, 1); |
Chris Mason | 890871b | 2009-09-02 16:24:52 -0400 | [diff] [blame] | 1024 | write_unlock(&em_tree->lock); |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 1025 | if (ret != -EEXIST) { |
| 1026 | free_extent_map(em); |
| 1027 | break; |
| 1028 | } |
| 1029 | btrfs_drop_extent_cache(inode, start, |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 1030 | start + ram_size - 1, 0); |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 1031 | } |
Liu Bo | ace68ba | 2013-04-22 10:53:47 +0000 | [diff] [blame] | 1032 | if (ret) |
| 1033 | goto out_reserve; |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 1034 | |
Chris Mason | 98d20f6 | 2008-04-14 09:46:10 -0400 | [diff] [blame] | 1035 | cur_alloc_size = ins.offset; |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 1036 | ret = btrfs_add_ordered_extent(inode, start, ins.objectid, |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 1037 | ram_size, cur_alloc_size, 0); |
Liu Bo | ace68ba | 2013-04-22 10:53:47 +0000 | [diff] [blame] | 1038 | if (ret) |
Filipe Manana | d9f8596 | 2014-08-25 10:43:00 +0100 | [diff] [blame] | 1039 | goto out_drop_extent_cache; |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 1040 | |
Yan Zheng | 17d217f | 2008-12-12 10:03:38 -0500 | [diff] [blame] | 1041 | if (root->root_key.objectid == |
| 1042 | BTRFS_DATA_RELOC_TREE_OBJECTID) { |
| 1043 | ret = btrfs_reloc_clone_csums(inode, start, |
| 1044 | cur_alloc_size); |
Josef Bacik | 0036158 | 2013-08-14 14:02:47 -0400 | [diff] [blame] | 1045 | if (ret) |
Filipe Manana | d9f8596 | 2014-08-25 10:43:00 +0100 | [diff] [blame] | 1046 | goto out_drop_extent_cache; |
Yan Zheng | 17d217f | 2008-12-12 10:03:38 -0500 | [diff] [blame] | 1047 | } |
| 1048 | |
Filipe Manana | 9cfa3e3 | 2016-04-26 15:39:32 +0100 | [diff] [blame] | 1049 | btrfs_dec_block_group_reservations(root->fs_info, ins.objectid); |
| 1050 | |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 1051 | if (disk_num_bytes < cur_alloc_size) |
Chris Mason | 3b95151 | 2008-04-17 11:29:12 -0400 | [diff] [blame] | 1052 | break; |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 1053 | |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 1054 | /* we're not doing compressed IO, don't unlock the first |
| 1055 | * page (which the caller expects to stay locked), don't |
| 1056 | * clear any dirty bits and don't set any writeback bits |
Chris Mason | 8b62b72 | 2009-09-02 16:53:46 -0400 | [diff] [blame] | 1057 | * |
| 1058 | * Do set the Private2 bit so we know this page was properly |
| 1059 | * setup for writepage |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 1060 | */ |
Josef Bacik | c2790a2 | 2013-07-29 11:20:47 -0400 | [diff] [blame] | 1061 | op = unlock ? PAGE_UNLOCK : 0; |
| 1062 | op |= PAGE_SET_PRIVATE2; |
Chris Mason | a791e35 | 2009-10-08 11:27:10 -0400 | [diff] [blame] | 1063 | |
Josef Bacik | c2790a2 | 2013-07-29 11:20:47 -0400 | [diff] [blame] | 1064 | extent_clear_unlock_delalloc(inode, start, |
| 1065 | start + ram_size - 1, locked_page, |
| 1066 | EXTENT_LOCKED | EXTENT_DELALLOC, |
| 1067 | op); |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 1068 | disk_num_bytes -= cur_alloc_size; |
Chris Mason | c59f895 | 2007-12-17 20:14:04 -0500 | [diff] [blame] | 1069 | num_bytes -= cur_alloc_size; |
| 1070 | alloc_hint = ins.objectid + ins.offset; |
| 1071 | start += cur_alloc_size; |
Chris Mason | b888db2 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 1072 | } |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 1073 | out: |
Chris Mason | be20aa9 | 2007-12-17 20:14:01 -0500 | [diff] [blame] | 1074 | return ret; |
Miao Xie | b7d5b0a | 2012-11-01 07:32:18 +0000 | [diff] [blame] | 1075 | |
Filipe Manana | d9f8596 | 2014-08-25 10:43:00 +0100 | [diff] [blame] | 1076 | out_drop_extent_cache: |
| 1077 | btrfs_drop_extent_cache(inode, start, start + ram_size - 1, 0); |
Liu Bo | ace68ba | 2013-04-22 10:53:47 +0000 | [diff] [blame] | 1078 | out_reserve: |
Filipe Manana | 9cfa3e3 | 2016-04-26 15:39:32 +0100 | [diff] [blame] | 1079 | btrfs_dec_block_group_reservations(root->fs_info, ins.objectid); |
Miao Xie | e570fd2 | 2014-06-19 10:42:50 +0800 | [diff] [blame] | 1080 | btrfs_free_reserved_extent(root, ins.objectid, ins.offset, 1); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 1081 | out_unlock: |
Josef Bacik | c2790a2 | 2013-07-29 11:20:47 -0400 | [diff] [blame] | 1082 | extent_clear_unlock_delalloc(inode, start, end, locked_page, |
Josef Bacik | 151a41b | 2013-07-29 13:22:24 -0400 | [diff] [blame] | 1083 | EXTENT_LOCKED | EXTENT_DO_ACCOUNTING | |
| 1084 | EXTENT_DELALLOC | EXTENT_DEFRAG, |
| 1085 | PAGE_UNLOCK | PAGE_CLEAR_DIRTY | |
| 1086 | PAGE_SET_WRITEBACK | PAGE_END_WRITEBACK); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 1087 | goto out; |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 1088 | } |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 1089 | |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 1090 | /* |
| 1091 | * work queue call back to started compression on a file and pages |
| 1092 | */ |
| 1093 | static noinline void async_cow_start(struct btrfs_work *work) |
| 1094 | { |
| 1095 | struct async_cow *async_cow; |
| 1096 | int num_added = 0; |
| 1097 | async_cow = container_of(work, struct async_cow, work); |
| 1098 | |
| 1099 | compress_file_range(async_cow->inode, async_cow->locked_page, |
| 1100 | async_cow->start, async_cow->end, async_cow, |
| 1101 | &num_added); |
Josef Bacik | 8180ef8 | 2012-06-08 15:16:12 -0400 | [diff] [blame] | 1102 | if (num_added == 0) { |
Josef Bacik | cb77fcd | 2012-06-15 12:19:48 -0600 | [diff] [blame] | 1103 | btrfs_add_delayed_iput(async_cow->inode); |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 1104 | async_cow->inode = NULL; |
Josef Bacik | 8180ef8 | 2012-06-08 15:16:12 -0400 | [diff] [blame] | 1105 | } |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 1106 | } |
| 1107 | |
| 1108 | /* |
| 1109 | * work queue call back to submit previously compressed pages |
| 1110 | */ |
| 1111 | static noinline void async_cow_submit(struct btrfs_work *work) |
| 1112 | { |
| 1113 | struct async_cow *async_cow; |
| 1114 | struct btrfs_root *root; |
| 1115 | unsigned long nr_pages; |
| 1116 | |
| 1117 | async_cow = container_of(work, struct async_cow, work); |
| 1118 | |
| 1119 | root = async_cow->root; |
Kirill A. Shutemov | 09cbfea | 2016-04-01 15:29:47 +0300 | [diff] [blame] | 1120 | nr_pages = (async_cow->end - async_cow->start + PAGE_SIZE) >> |
| 1121 | PAGE_SHIFT; |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 1122 | |
David Sterba | ee86395 | 2015-02-16 19:41:40 +0100 | [diff] [blame] | 1123 | /* |
| 1124 | * atomic_sub_return implies a barrier for waitqueue_active |
| 1125 | */ |
Josef Bacik | 66657b3 | 2012-08-01 15:36:24 -0400 | [diff] [blame] | 1126 | if (atomic_sub_return(nr_pages, &root->fs_info->async_delalloc_pages) < |
Byongho Lee | ee22184 | 2015-12-15 01:42:10 +0900 | [diff] [blame] | 1127 | 5 * SZ_1M && |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 1128 | waitqueue_active(&root->fs_info->async_submit_wait)) |
| 1129 | wake_up(&root->fs_info->async_submit_wait); |
| 1130 | |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 1131 | if (async_cow->inode) |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 1132 | submit_compressed_extents(async_cow->inode, async_cow); |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 1133 | } |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 1134 | |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 1135 | static noinline void async_cow_free(struct btrfs_work *work) |
| 1136 | { |
| 1137 | struct async_cow *async_cow; |
| 1138 | async_cow = container_of(work, struct async_cow, work); |
Josef Bacik | 8180ef8 | 2012-06-08 15:16:12 -0400 | [diff] [blame] | 1139 | if (async_cow->inode) |
Josef Bacik | cb77fcd | 2012-06-15 12:19:48 -0600 | [diff] [blame] | 1140 | btrfs_add_delayed_iput(async_cow->inode); |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 1141 | kfree(async_cow); |
| 1142 | } |
| 1143 | |
| 1144 | static int cow_file_range_async(struct inode *inode, struct page *locked_page, |
| 1145 | u64 start, u64 end, int *page_started, |
| 1146 | unsigned long *nr_written) |
| 1147 | { |
| 1148 | struct async_cow *async_cow; |
| 1149 | struct btrfs_root *root = BTRFS_I(inode)->root; |
| 1150 | unsigned long nr_pages; |
| 1151 | u64 cur_end; |
Byongho Lee | ee22184 | 2015-12-15 01:42:10 +0900 | [diff] [blame] | 1152 | int limit = 10 * SZ_1M; |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 1153 | |
Chris Mason | a3429ab | 2009-10-08 12:30:20 -0400 | [diff] [blame] | 1154 | clear_extent_bit(&BTRFS_I(inode)->io_tree, start, end, EXTENT_LOCKED, |
| 1155 | 1, 0, NULL, GFP_NOFS); |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 1156 | while (start < end) { |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 1157 | async_cow = kmalloc(sizeof(*async_cow), GFP_NOFS); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 1158 | BUG_ON(!async_cow); /* -ENOMEM */ |
Josef Bacik | 8180ef8 | 2012-06-08 15:16:12 -0400 | [diff] [blame] | 1159 | async_cow->inode = igrab(inode); |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 1160 | async_cow->root = root; |
| 1161 | async_cow->locked_page = locked_page; |
| 1162 | async_cow->start = start; |
| 1163 | |
Wang Shilong | f79707b | 2014-07-17 11:44:09 +0800 | [diff] [blame] | 1164 | if (BTRFS_I(inode)->flags & BTRFS_INODE_NOCOMPRESS && |
Jeff Mahoney | 3cdde22 | 2016-06-09 21:38:35 -0400 | [diff] [blame] | 1165 | !btrfs_test_opt(root->fs_info, FORCE_COMPRESS)) |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 1166 | cur_end = end; |
| 1167 | else |
Byongho Lee | ee22184 | 2015-12-15 01:42:10 +0900 | [diff] [blame] | 1168 | cur_end = min(end, start + SZ_512K - 1); |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 1169 | |
| 1170 | async_cow->end = cur_end; |
| 1171 | INIT_LIST_HEAD(&async_cow->extents); |
| 1172 | |
Liu Bo | 9e0af23 | 2014-08-15 23:36:53 +0800 | [diff] [blame] | 1173 | btrfs_init_work(&async_cow->work, |
| 1174 | btrfs_delalloc_helper, |
| 1175 | async_cow_start, async_cow_submit, |
| 1176 | async_cow_free); |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 1177 | |
Kirill A. Shutemov | 09cbfea | 2016-04-01 15:29:47 +0300 | [diff] [blame] | 1178 | nr_pages = (cur_end - start + PAGE_SIZE) >> |
| 1179 | PAGE_SHIFT; |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 1180 | atomic_add(nr_pages, &root->fs_info->async_delalloc_pages); |
| 1181 | |
Qu Wenruo | afe3d24 | 2014-02-28 10:46:07 +0800 | [diff] [blame] | 1182 | btrfs_queue_work(root->fs_info->delalloc_workers, |
| 1183 | &async_cow->work); |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 1184 | |
| 1185 | if (atomic_read(&root->fs_info->async_delalloc_pages) > limit) { |
| 1186 | wait_event(root->fs_info->async_submit_wait, |
| 1187 | (atomic_read(&root->fs_info->async_delalloc_pages) < |
| 1188 | limit)); |
| 1189 | } |
| 1190 | |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 1191 | while (atomic_read(&root->fs_info->async_submit_draining) && |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 1192 | atomic_read(&root->fs_info->async_delalloc_pages)) { |
| 1193 | wait_event(root->fs_info->async_submit_wait, |
| 1194 | (atomic_read(&root->fs_info->async_delalloc_pages) == |
| 1195 | 0)); |
| 1196 | } |
| 1197 | |
| 1198 | *nr_written += nr_pages; |
| 1199 | start = cur_end + 1; |
| 1200 | } |
| 1201 | *page_started = 1; |
| 1202 | return 0; |
Chris Mason | be20aa9 | 2007-12-17 20:14:01 -0500 | [diff] [blame] | 1203 | } |
| 1204 | |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 1205 | static noinline int csum_exist_in_range(struct btrfs_root *root, |
Yan Zheng | 17d217f | 2008-12-12 10:03:38 -0500 | [diff] [blame] | 1206 | u64 bytenr, u64 num_bytes) |
| 1207 | { |
| 1208 | int ret; |
| 1209 | struct btrfs_ordered_sum *sums; |
| 1210 | LIST_HEAD(list); |
| 1211 | |
Yan Zheng | 07d400a | 2009-01-06 11:42:00 -0500 | [diff] [blame] | 1212 | ret = btrfs_lookup_csums_range(root->fs_info->csum_root, bytenr, |
Arne Jansen | a2de733 | 2011-03-08 14:14:00 +0100 | [diff] [blame] | 1213 | bytenr + num_bytes - 1, &list, 0); |
Yan Zheng | 17d217f | 2008-12-12 10:03:38 -0500 | [diff] [blame] | 1214 | if (ret == 0 && list_empty(&list)) |
| 1215 | return 0; |
| 1216 | |
| 1217 | while (!list_empty(&list)) { |
| 1218 | sums = list_entry(list.next, struct btrfs_ordered_sum, list); |
| 1219 | list_del(&sums->list); |
| 1220 | kfree(sums); |
| 1221 | } |
| 1222 | return 1; |
| 1223 | } |
| 1224 | |
Chris Mason | d352ac6 | 2008-09-29 15:18:18 -0400 | [diff] [blame] | 1225 | /* |
| 1226 | * when nowcow writeback call back. This checks for snapshots or COW copies |
| 1227 | * of the extents that exist in the file, and COWs the file as required. |
| 1228 | * |
| 1229 | * If no cow copies or snapshots exist, we write directly to the existing |
| 1230 | * blocks on disk |
| 1231 | */ |
Chris Mason | 7f366cf | 2009-03-12 20:12:45 -0400 | [diff] [blame] | 1232 | static noinline int run_delalloc_nocow(struct inode *inode, |
| 1233 | struct page *locked_page, |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 1234 | u64 start, u64 end, int *page_started, int force, |
| 1235 | unsigned long *nr_written) |
Chris Mason | be20aa9 | 2007-12-17 20:14:01 -0500 | [diff] [blame] | 1236 | { |
Chris Mason | be20aa9 | 2007-12-17 20:14:01 -0500 | [diff] [blame] | 1237 | struct btrfs_root *root = BTRFS_I(inode)->root; |
Yan Zheng | 7ea394f | 2008-08-05 13:05:02 -0400 | [diff] [blame] | 1238 | struct btrfs_trans_handle *trans; |
Chris Mason | be20aa9 | 2007-12-17 20:14:01 -0500 | [diff] [blame] | 1239 | struct extent_buffer *leaf; |
Chris Mason | be20aa9 | 2007-12-17 20:14:01 -0500 | [diff] [blame] | 1240 | struct btrfs_path *path; |
Yan Zheng | 80ff385 | 2008-10-30 14:20:02 -0400 | [diff] [blame] | 1241 | struct btrfs_file_extent_item *fi; |
Chris Mason | be20aa9 | 2007-12-17 20:14:01 -0500 | [diff] [blame] | 1242 | struct btrfs_key found_key; |
Yan Zheng | 80ff385 | 2008-10-30 14:20:02 -0400 | [diff] [blame] | 1243 | u64 cow_start; |
| 1244 | u64 cur_offset; |
| 1245 | u64 extent_end; |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 1246 | u64 extent_offset; |
Yan Zheng | 80ff385 | 2008-10-30 14:20:02 -0400 | [diff] [blame] | 1247 | u64 disk_bytenr; |
| 1248 | u64 num_bytes; |
Josef Bacik | b493968 | 2012-12-03 10:31:19 -0500 | [diff] [blame] | 1249 | u64 disk_num_bytes; |
Josef Bacik | cc95bef | 2013-04-04 14:31:27 -0400 | [diff] [blame] | 1250 | u64 ram_bytes; |
Yan Zheng | 80ff385 | 2008-10-30 14:20:02 -0400 | [diff] [blame] | 1251 | int extent_type; |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 1252 | int ret, err; |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 1253 | int type; |
Yan Zheng | 80ff385 | 2008-10-30 14:20:02 -0400 | [diff] [blame] | 1254 | int nocow; |
| 1255 | int check_prev = 1; |
Li Zefan | 82d5902 | 2011-04-20 10:33:24 +0800 | [diff] [blame] | 1256 | bool nolock; |
Li Zefan | 33345d01 | 2011-04-20 10:31:50 +0800 | [diff] [blame] | 1257 | u64 ino = btrfs_ino(inode); |
Chris Mason | be20aa9 | 2007-12-17 20:14:01 -0500 | [diff] [blame] | 1258 | |
| 1259 | path = btrfs_alloc_path(); |
Josef Bacik | 17ca04a | 2012-05-31 15:58:55 -0400 | [diff] [blame] | 1260 | if (!path) { |
Josef Bacik | c2790a2 | 2013-07-29 11:20:47 -0400 | [diff] [blame] | 1261 | extent_clear_unlock_delalloc(inode, start, end, locked_page, |
| 1262 | EXTENT_LOCKED | EXTENT_DELALLOC | |
Josef Bacik | 151a41b | 2013-07-29 13:22:24 -0400 | [diff] [blame] | 1263 | EXTENT_DO_ACCOUNTING | |
| 1264 | EXTENT_DEFRAG, PAGE_UNLOCK | |
Josef Bacik | c2790a2 | 2013-07-29 11:20:47 -0400 | [diff] [blame] | 1265 | PAGE_CLEAR_DIRTY | |
| 1266 | PAGE_SET_WRITEBACK | |
| 1267 | PAGE_END_WRITEBACK); |
Mark Fasheh | d8926bb | 2011-07-13 10:38:47 -0700 | [diff] [blame] | 1268 | return -ENOMEM; |
Josef Bacik | 17ca04a | 2012-05-31 15:58:55 -0400 | [diff] [blame] | 1269 | } |
Li Zefan | 82d5902 | 2011-04-20 10:33:24 +0800 | [diff] [blame] | 1270 | |
Liu Bo | 83eea1f | 2012-07-10 05:28:39 -0600 | [diff] [blame] | 1271 | nolock = btrfs_is_free_space_inode(inode); |
Li Zefan | 82d5902 | 2011-04-20 10:33:24 +0800 | [diff] [blame] | 1272 | |
| 1273 | if (nolock) |
Josef Bacik | 7a7eaa4 | 2011-04-13 12:54:33 -0400 | [diff] [blame] | 1274 | trans = btrfs_join_transaction_nolock(root); |
Li Zefan | 82d5902 | 2011-04-20 10:33:24 +0800 | [diff] [blame] | 1275 | else |
Josef Bacik | 7a7eaa4 | 2011-04-13 12:54:33 -0400 | [diff] [blame] | 1276 | trans = btrfs_join_transaction(root); |
Chris Mason | ff5714c | 2011-05-28 07:00:39 -0400 | [diff] [blame] | 1277 | |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 1278 | if (IS_ERR(trans)) { |
Josef Bacik | c2790a2 | 2013-07-29 11:20:47 -0400 | [diff] [blame] | 1279 | extent_clear_unlock_delalloc(inode, start, end, locked_page, |
| 1280 | EXTENT_LOCKED | EXTENT_DELALLOC | |
Josef Bacik | 151a41b | 2013-07-29 13:22:24 -0400 | [diff] [blame] | 1281 | EXTENT_DO_ACCOUNTING | |
| 1282 | EXTENT_DEFRAG, PAGE_UNLOCK | |
Josef Bacik | c2790a2 | 2013-07-29 11:20:47 -0400 | [diff] [blame] | 1283 | PAGE_CLEAR_DIRTY | |
| 1284 | PAGE_SET_WRITEBACK | |
| 1285 | PAGE_END_WRITEBACK); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 1286 | btrfs_free_path(path); |
| 1287 | return PTR_ERR(trans); |
| 1288 | } |
| 1289 | |
Josef Bacik | 74b2107 | 2011-04-13 12:02:53 -0400 | [diff] [blame] | 1290 | trans->block_rsv = &root->fs_info->delalloc_block_rsv; |
Chris Mason | be20aa9 | 2007-12-17 20:14:01 -0500 | [diff] [blame] | 1291 | |
Yan Zheng | 80ff385 | 2008-10-30 14:20:02 -0400 | [diff] [blame] | 1292 | cow_start = (u64)-1; |
| 1293 | cur_offset = start; |
| 1294 | while (1) { |
Li Zefan | 33345d01 | 2011-04-20 10:31:50 +0800 | [diff] [blame] | 1295 | ret = btrfs_lookup_file_extent(trans, root, path, ino, |
Yan Zheng | 80ff385 | 2008-10-30 14:20:02 -0400 | [diff] [blame] | 1296 | cur_offset, 0); |
Josef Bacik | d788a34 | 2013-10-25 16:55:08 -0400 | [diff] [blame] | 1297 | if (ret < 0) |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 1298 | goto error; |
Yan Zheng | 80ff385 | 2008-10-30 14:20:02 -0400 | [diff] [blame] | 1299 | if (ret > 0 && path->slots[0] > 0 && check_prev) { |
| 1300 | leaf = path->nodes[0]; |
| 1301 | btrfs_item_key_to_cpu(leaf, &found_key, |
| 1302 | path->slots[0] - 1); |
Li Zefan | 33345d01 | 2011-04-20 10:31:50 +0800 | [diff] [blame] | 1303 | if (found_key.objectid == ino && |
Yan Zheng | 80ff385 | 2008-10-30 14:20:02 -0400 | [diff] [blame] | 1304 | found_key.type == BTRFS_EXTENT_DATA_KEY) |
| 1305 | path->slots[0]--; |
| 1306 | } |
| 1307 | check_prev = 0; |
| 1308 | next_slot: |
| 1309 | leaf = path->nodes[0]; |
| 1310 | if (path->slots[0] >= btrfs_header_nritems(leaf)) { |
| 1311 | ret = btrfs_next_leaf(root, path); |
Josef Bacik | d788a34 | 2013-10-25 16:55:08 -0400 | [diff] [blame] | 1312 | if (ret < 0) |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 1313 | goto error; |
Yan Zheng | 80ff385 | 2008-10-30 14:20:02 -0400 | [diff] [blame] | 1314 | if (ret > 0) |
| 1315 | break; |
| 1316 | leaf = path->nodes[0]; |
| 1317 | } |
Chris Mason | be20aa9 | 2007-12-17 20:14:01 -0500 | [diff] [blame] | 1318 | |
Yan Zheng | 80ff385 | 2008-10-30 14:20:02 -0400 | [diff] [blame] | 1319 | nocow = 0; |
| 1320 | disk_bytenr = 0; |
Yan Zheng | 17d217f | 2008-12-12 10:03:38 -0500 | [diff] [blame] | 1321 | num_bytes = 0; |
Yan Zheng | 80ff385 | 2008-10-30 14:20:02 -0400 | [diff] [blame] | 1322 | btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]); |
Chris Mason | be20aa9 | 2007-12-17 20:14:01 -0500 | [diff] [blame] | 1323 | |
Filipe Manana | 1d512cb | 2015-11-09 00:33:58 +0000 | [diff] [blame] | 1324 | if (found_key.objectid > ino) |
| 1325 | break; |
| 1326 | if (WARN_ON_ONCE(found_key.objectid < ino) || |
| 1327 | found_key.type < BTRFS_EXTENT_DATA_KEY) { |
| 1328 | path->slots[0]++; |
| 1329 | goto next_slot; |
| 1330 | } |
| 1331 | if (found_key.type > BTRFS_EXTENT_DATA_KEY || |
Yan Zheng | 80ff385 | 2008-10-30 14:20:02 -0400 | [diff] [blame] | 1332 | found_key.offset > end) |
| 1333 | break; |
Chris Mason | be20aa9 | 2007-12-17 20:14:01 -0500 | [diff] [blame] | 1334 | |
Yan Zheng | 80ff385 | 2008-10-30 14:20:02 -0400 | [diff] [blame] | 1335 | if (found_key.offset > cur_offset) { |
| 1336 | extent_end = found_key.offset; |
Chris Mason | e9061e2 | 2009-10-09 09:57:45 -0400 | [diff] [blame] | 1337 | extent_type = 0; |
Yan Zheng | 80ff385 | 2008-10-30 14:20:02 -0400 | [diff] [blame] | 1338 | goto out_check; |
| 1339 | } |
Chris Mason | c31f883 | 2008-01-08 15:46:31 -0500 | [diff] [blame] | 1340 | |
Yan Zheng | 80ff385 | 2008-10-30 14:20:02 -0400 | [diff] [blame] | 1341 | fi = btrfs_item_ptr(leaf, path->slots[0], |
| 1342 | struct btrfs_file_extent_item); |
| 1343 | extent_type = btrfs_file_extent_type(leaf, fi); |
Chris Mason | be20aa9 | 2007-12-17 20:14:01 -0500 | [diff] [blame] | 1344 | |
Josef Bacik | cc95bef | 2013-04-04 14:31:27 -0400 | [diff] [blame] | 1345 | ram_bytes = btrfs_file_extent_ram_bytes(leaf, fi); |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 1346 | if (extent_type == BTRFS_FILE_EXTENT_REG || |
| 1347 | extent_type == BTRFS_FILE_EXTENT_PREALLOC) { |
Yan Zheng | 80ff385 | 2008-10-30 14:20:02 -0400 | [diff] [blame] | 1348 | disk_bytenr = btrfs_file_extent_disk_bytenr(leaf, fi); |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 1349 | extent_offset = btrfs_file_extent_offset(leaf, fi); |
Yan Zheng | 80ff385 | 2008-10-30 14:20:02 -0400 | [diff] [blame] | 1350 | extent_end = found_key.offset + |
| 1351 | btrfs_file_extent_num_bytes(leaf, fi); |
Josef Bacik | b493968 | 2012-12-03 10:31:19 -0500 | [diff] [blame] | 1352 | disk_num_bytes = |
| 1353 | btrfs_file_extent_disk_num_bytes(leaf, fi); |
Yan Zheng | 80ff385 | 2008-10-30 14:20:02 -0400 | [diff] [blame] | 1354 | if (extent_end <= start) { |
| 1355 | path->slots[0]++; |
| 1356 | goto next_slot; |
| 1357 | } |
Yan Zheng | 17d217f | 2008-12-12 10:03:38 -0500 | [diff] [blame] | 1358 | if (disk_bytenr == 0) |
| 1359 | goto out_check; |
Yan Zheng | 80ff385 | 2008-10-30 14:20:02 -0400 | [diff] [blame] | 1360 | if (btrfs_file_extent_compression(leaf, fi) || |
| 1361 | btrfs_file_extent_encryption(leaf, fi) || |
| 1362 | btrfs_file_extent_other_encoding(leaf, fi)) |
| 1363 | goto out_check; |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 1364 | if (extent_type == BTRFS_FILE_EXTENT_REG && !force) |
| 1365 | goto out_check; |
Yan Zheng | d2fb343 | 2008-12-11 16:30:39 -0500 | [diff] [blame] | 1366 | if (btrfs_extent_readonly(root, disk_bytenr)) |
Yan Zheng | 80ff385 | 2008-10-30 14:20:02 -0400 | [diff] [blame] | 1367 | goto out_check; |
Li Zefan | 33345d01 | 2011-04-20 10:31:50 +0800 | [diff] [blame] | 1368 | if (btrfs_cross_ref_exist(trans, root, ino, |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 1369 | found_key.offset - |
| 1370 | extent_offset, disk_bytenr)) |
Yan Zheng | 17d217f | 2008-12-12 10:03:38 -0500 | [diff] [blame] | 1371 | goto out_check; |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 1372 | disk_bytenr += extent_offset; |
Yan Zheng | 17d217f | 2008-12-12 10:03:38 -0500 | [diff] [blame] | 1373 | disk_bytenr += cur_offset - found_key.offset; |
| 1374 | num_bytes = min(end + 1, extent_end) - cur_offset; |
| 1375 | /* |
Wang Shilong | e9894fd | 2014-03-27 11:12:25 +0800 | [diff] [blame] | 1376 | * if there are pending snapshots for this root, |
| 1377 | * we fall into common COW way. |
| 1378 | */ |
| 1379 | if (!nolock) { |
Filipe Manana | 9ea24bb | 2014-10-29 11:57:59 +0000 | [diff] [blame] | 1380 | err = btrfs_start_write_no_snapshoting(root); |
Wang Shilong | e9894fd | 2014-03-27 11:12:25 +0800 | [diff] [blame] | 1381 | if (!err) |
| 1382 | goto out_check; |
| 1383 | } |
| 1384 | /* |
Yan Zheng | 17d217f | 2008-12-12 10:03:38 -0500 | [diff] [blame] | 1385 | * force cow if csum exists in the range. |
| 1386 | * this ensure that csum for a given extent are |
| 1387 | * either valid or do not exist. |
| 1388 | */ |
| 1389 | if (csum_exist_in_range(root, disk_bytenr, num_bytes)) |
| 1390 | goto out_check; |
Filipe Manana | f78c436 | 2016-05-09 13:15:41 +0100 | [diff] [blame] | 1391 | if (!btrfs_inc_nocow_writers(root->fs_info, |
| 1392 | disk_bytenr)) |
| 1393 | goto out_check; |
Yan Zheng | 80ff385 | 2008-10-30 14:20:02 -0400 | [diff] [blame] | 1394 | nocow = 1; |
| 1395 | } else if (extent_type == BTRFS_FILE_EXTENT_INLINE) { |
| 1396 | extent_end = found_key.offset + |
Chris Mason | 514ac8a | 2014-01-03 21:07:00 -0800 | [diff] [blame] | 1397 | btrfs_file_extent_inline_len(leaf, |
| 1398 | path->slots[0], fi); |
Yan Zheng | 80ff385 | 2008-10-30 14:20:02 -0400 | [diff] [blame] | 1399 | extent_end = ALIGN(extent_end, root->sectorsize); |
| 1400 | } else { |
| 1401 | BUG_ON(1); |
| 1402 | } |
| 1403 | out_check: |
| 1404 | if (extent_end <= start) { |
| 1405 | path->slots[0]++; |
Wang Shilong | e9894fd | 2014-03-27 11:12:25 +0800 | [diff] [blame] | 1406 | if (!nolock && nocow) |
Filipe Manana | 9ea24bb | 2014-10-29 11:57:59 +0000 | [diff] [blame] | 1407 | btrfs_end_write_no_snapshoting(root); |
Filipe Manana | f78c436 | 2016-05-09 13:15:41 +0100 | [diff] [blame] | 1408 | if (nocow) |
| 1409 | btrfs_dec_nocow_writers(root->fs_info, |
| 1410 | disk_bytenr); |
Yan Zheng | 80ff385 | 2008-10-30 14:20:02 -0400 | [diff] [blame] | 1411 | goto next_slot; |
| 1412 | } |
| 1413 | if (!nocow) { |
| 1414 | if (cow_start == (u64)-1) |
| 1415 | cow_start = cur_offset; |
| 1416 | cur_offset = extent_end; |
| 1417 | if (cur_offset > end) |
| 1418 | break; |
| 1419 | path->slots[0]++; |
| 1420 | goto next_slot; |
Yan Zheng | 7ea394f | 2008-08-05 13:05:02 -0400 | [diff] [blame] | 1421 | } |
Chris Mason | be20aa9 | 2007-12-17 20:14:01 -0500 | [diff] [blame] | 1422 | |
David Sterba | b3b4aa7 | 2011-04-21 01:20:15 +0200 | [diff] [blame] | 1423 | btrfs_release_path(path); |
Yan Zheng | 80ff385 | 2008-10-30 14:20:02 -0400 | [diff] [blame] | 1424 | if (cow_start != (u64)-1) { |
Josef Bacik | 0036158 | 2013-08-14 14:02:47 -0400 | [diff] [blame] | 1425 | ret = cow_file_range(inode, locked_page, |
| 1426 | cow_start, found_key.offset - 1, |
Wang Xiaoguang | dda3245 | 2016-07-11 11:05:29 +0800 | [diff] [blame] | 1427 | end, page_started, nr_written, 1, |
| 1428 | NULL); |
Wang Shilong | e9894fd | 2014-03-27 11:12:25 +0800 | [diff] [blame] | 1429 | if (ret) { |
| 1430 | if (!nolock && nocow) |
Filipe Manana | 9ea24bb | 2014-10-29 11:57:59 +0000 | [diff] [blame] | 1431 | btrfs_end_write_no_snapshoting(root); |
Filipe Manana | f78c436 | 2016-05-09 13:15:41 +0100 | [diff] [blame] | 1432 | if (nocow) |
| 1433 | btrfs_dec_nocow_writers(root->fs_info, |
| 1434 | disk_bytenr); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 1435 | goto error; |
Wang Shilong | e9894fd | 2014-03-27 11:12:25 +0800 | [diff] [blame] | 1436 | } |
Yan Zheng | 80ff385 | 2008-10-30 14:20:02 -0400 | [diff] [blame] | 1437 | cow_start = (u64)-1; |
Yan Zheng | 7ea394f | 2008-08-05 13:05:02 -0400 | [diff] [blame] | 1438 | } |
Yan Zheng | 80ff385 | 2008-10-30 14:20:02 -0400 | [diff] [blame] | 1439 | |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 1440 | if (extent_type == BTRFS_FILE_EXTENT_PREALLOC) { |
| 1441 | struct extent_map *em; |
| 1442 | struct extent_map_tree *em_tree; |
| 1443 | em_tree = &BTRFS_I(inode)->extent_tree; |
David Sterba | 172ddd6 | 2011-04-21 00:48:27 +0200 | [diff] [blame] | 1444 | em = alloc_extent_map(); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 1445 | BUG_ON(!em); /* -ENOMEM */ |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 1446 | em->start = cur_offset; |
Josef Bacik | 70c8a91 | 2012-10-11 16:54:30 -0400 | [diff] [blame] | 1447 | em->orig_start = found_key.offset - extent_offset; |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 1448 | em->len = num_bytes; |
| 1449 | em->block_len = num_bytes; |
| 1450 | em->block_start = disk_bytenr; |
Josef Bacik | b493968 | 2012-12-03 10:31:19 -0500 | [diff] [blame] | 1451 | em->orig_block_len = disk_num_bytes; |
Josef Bacik | cc95bef | 2013-04-04 14:31:27 -0400 | [diff] [blame] | 1452 | em->ram_bytes = ram_bytes; |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 1453 | em->bdev = root->fs_info->fs_devices->latest_bdev; |
Josef Bacik | 2ab28f3 | 2012-10-12 15:27:49 -0400 | [diff] [blame] | 1454 | em->mod_start = em->start; |
| 1455 | em->mod_len = em->len; |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 1456 | set_bit(EXTENT_FLAG_PINNED, &em->flags); |
Josef Bacik | b11e234 | 2012-12-03 10:58:15 -0500 | [diff] [blame] | 1457 | set_bit(EXTENT_FLAG_FILLING, &em->flags); |
Josef Bacik | 70c8a91 | 2012-10-11 16:54:30 -0400 | [diff] [blame] | 1458 | em->generation = -1; |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 1459 | while (1) { |
Chris Mason | 890871b | 2009-09-02 16:24:52 -0400 | [diff] [blame] | 1460 | write_lock(&em_tree->lock); |
Josef Bacik | 09a2a8f9 | 2013-04-05 16:51:15 -0400 | [diff] [blame] | 1461 | ret = add_extent_mapping(em_tree, em, 1); |
Chris Mason | 890871b | 2009-09-02 16:24:52 -0400 | [diff] [blame] | 1462 | write_unlock(&em_tree->lock); |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 1463 | if (ret != -EEXIST) { |
| 1464 | free_extent_map(em); |
| 1465 | break; |
| 1466 | } |
| 1467 | btrfs_drop_extent_cache(inode, em->start, |
| 1468 | em->start + em->len - 1, 0); |
| 1469 | } |
| 1470 | type = BTRFS_ORDERED_PREALLOC; |
| 1471 | } else { |
| 1472 | type = BTRFS_ORDERED_NOCOW; |
| 1473 | } |
Yan Zheng | 80ff385 | 2008-10-30 14:20:02 -0400 | [diff] [blame] | 1474 | |
| 1475 | ret = btrfs_add_ordered_extent(inode, cur_offset, disk_bytenr, |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 1476 | num_bytes, num_bytes, type); |
Filipe Manana | f78c436 | 2016-05-09 13:15:41 +0100 | [diff] [blame] | 1477 | if (nocow) |
| 1478 | btrfs_dec_nocow_writers(root->fs_info, disk_bytenr); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 1479 | BUG_ON(ret); /* -ENOMEM */ |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 1480 | |
Yan, Zheng | efa5646 | 2010-05-16 10:49:59 -0400 | [diff] [blame] | 1481 | if (root->root_key.objectid == |
| 1482 | BTRFS_DATA_RELOC_TREE_OBJECTID) { |
| 1483 | ret = btrfs_reloc_clone_csums(inode, cur_offset, |
| 1484 | num_bytes); |
Wang Shilong | e9894fd | 2014-03-27 11:12:25 +0800 | [diff] [blame] | 1485 | if (ret) { |
| 1486 | if (!nolock && nocow) |
Filipe Manana | 9ea24bb | 2014-10-29 11:57:59 +0000 | [diff] [blame] | 1487 | btrfs_end_write_no_snapshoting(root); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 1488 | goto error; |
Wang Shilong | e9894fd | 2014-03-27 11:12:25 +0800 | [diff] [blame] | 1489 | } |
Yan, Zheng | efa5646 | 2010-05-16 10:49:59 -0400 | [diff] [blame] | 1490 | } |
| 1491 | |
Josef Bacik | c2790a2 | 2013-07-29 11:20:47 -0400 | [diff] [blame] | 1492 | extent_clear_unlock_delalloc(inode, cur_offset, |
| 1493 | cur_offset + num_bytes - 1, |
| 1494 | locked_page, EXTENT_LOCKED | |
Wang Xiaoguang | 1851309 | 2016-07-25 15:51:40 +0800 | [diff] [blame] | 1495 | EXTENT_DELALLOC | |
| 1496 | EXTENT_CLEAR_DATA_RESV, |
| 1497 | PAGE_UNLOCK | PAGE_SET_PRIVATE2); |
| 1498 | |
Wang Shilong | e9894fd | 2014-03-27 11:12:25 +0800 | [diff] [blame] | 1499 | if (!nolock && nocow) |
Filipe Manana | 9ea24bb | 2014-10-29 11:57:59 +0000 | [diff] [blame] | 1500 | btrfs_end_write_no_snapshoting(root); |
Yan Zheng | 80ff385 | 2008-10-30 14:20:02 -0400 | [diff] [blame] | 1501 | cur_offset = extent_end; |
| 1502 | if (cur_offset > end) |
| 1503 | break; |
Yan Zheng | 7ea394f | 2008-08-05 13:05:02 -0400 | [diff] [blame] | 1504 | } |
David Sterba | b3b4aa7 | 2011-04-21 01:20:15 +0200 | [diff] [blame] | 1505 | btrfs_release_path(path); |
Yan Zheng | 80ff385 | 2008-10-30 14:20:02 -0400 | [diff] [blame] | 1506 | |
Josef Bacik | 17ca04a | 2012-05-31 15:58:55 -0400 | [diff] [blame] | 1507 | if (cur_offset <= end && cow_start == (u64)-1) { |
Yan Zheng | 80ff385 | 2008-10-30 14:20:02 -0400 | [diff] [blame] | 1508 | cow_start = cur_offset; |
Josef Bacik | 17ca04a | 2012-05-31 15:58:55 -0400 | [diff] [blame] | 1509 | cur_offset = end; |
| 1510 | } |
| 1511 | |
Yan Zheng | 80ff385 | 2008-10-30 14:20:02 -0400 | [diff] [blame] | 1512 | if (cow_start != (u64)-1) { |
Wang Xiaoguang | dda3245 | 2016-07-11 11:05:29 +0800 | [diff] [blame] | 1513 | ret = cow_file_range(inode, locked_page, cow_start, end, end, |
| 1514 | page_started, nr_written, 1, NULL); |
Josef Bacik | d788a34 | 2013-10-25 16:55:08 -0400 | [diff] [blame] | 1515 | if (ret) |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 1516 | goto error; |
Yan Zheng | 80ff385 | 2008-10-30 14:20:02 -0400 | [diff] [blame] | 1517 | } |
| 1518 | |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 1519 | error: |
Miao Xie | a698d075 | 2012-09-20 01:51:59 -0600 | [diff] [blame] | 1520 | err = btrfs_end_transaction(trans, root); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 1521 | if (!ret) |
| 1522 | ret = err; |
| 1523 | |
Josef Bacik | 17ca04a | 2012-05-31 15:58:55 -0400 | [diff] [blame] | 1524 | if (ret && cur_offset < end) |
Josef Bacik | c2790a2 | 2013-07-29 11:20:47 -0400 | [diff] [blame] | 1525 | extent_clear_unlock_delalloc(inode, cur_offset, end, |
| 1526 | locked_page, EXTENT_LOCKED | |
Josef Bacik | 151a41b | 2013-07-29 13:22:24 -0400 | [diff] [blame] | 1527 | EXTENT_DELALLOC | EXTENT_DEFRAG | |
| 1528 | EXTENT_DO_ACCOUNTING, PAGE_UNLOCK | |
| 1529 | PAGE_CLEAR_DIRTY | |
Josef Bacik | c2790a2 | 2013-07-29 11:20:47 -0400 | [diff] [blame] | 1530 | PAGE_SET_WRITEBACK | |
| 1531 | PAGE_END_WRITEBACK); |
Yan Zheng | 7ea394f | 2008-08-05 13:05:02 -0400 | [diff] [blame] | 1532 | btrfs_free_path(path); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 1533 | return ret; |
Chris Mason | be20aa9 | 2007-12-17 20:14:01 -0500 | [diff] [blame] | 1534 | } |
| 1535 | |
Wang Shilong | 47059d9 | 2014-07-03 18:22:07 +0800 | [diff] [blame] | 1536 | static inline int need_force_cow(struct inode *inode, u64 start, u64 end) |
| 1537 | { |
| 1538 | |
| 1539 | if (!(BTRFS_I(inode)->flags & BTRFS_INODE_NODATACOW) && |
| 1540 | !(BTRFS_I(inode)->flags & BTRFS_INODE_PREALLOC)) |
| 1541 | return 0; |
| 1542 | |
| 1543 | /* |
| 1544 | * @defrag_bytes is a hint value, no spinlock held here, |
| 1545 | * if is not zero, it means the file is defragging. |
| 1546 | * Force cow if given extent needs to be defragged. |
| 1547 | */ |
| 1548 | if (BTRFS_I(inode)->defrag_bytes && |
| 1549 | test_range_bit(&BTRFS_I(inode)->io_tree, start, end, |
| 1550 | EXTENT_DEFRAG, 0, NULL)) |
| 1551 | return 1; |
| 1552 | |
| 1553 | return 0; |
| 1554 | } |
| 1555 | |
Chris Mason | d352ac6 | 2008-09-29 15:18:18 -0400 | [diff] [blame] | 1556 | /* |
| 1557 | * extent_io.c call back to do delayed allocation processing |
| 1558 | */ |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 1559 | static int run_delalloc_range(struct inode *inode, struct page *locked_page, |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 1560 | u64 start, u64 end, int *page_started, |
| 1561 | unsigned long *nr_written) |
Chris Mason | be20aa9 | 2007-12-17 20:14:01 -0500 | [diff] [blame] | 1562 | { |
Chris Mason | be20aa9 | 2007-12-17 20:14:01 -0500 | [diff] [blame] | 1563 | int ret; |
Wang Shilong | 47059d9 | 2014-07-03 18:22:07 +0800 | [diff] [blame] | 1564 | int force_cow = need_force_cow(inode, start, end); |
Chris Mason | a213501 | 2008-06-25 16:01:30 -0400 | [diff] [blame] | 1565 | |
Wang Shilong | 47059d9 | 2014-07-03 18:22:07 +0800 | [diff] [blame] | 1566 | if (BTRFS_I(inode)->flags & BTRFS_INODE_NODATACOW && !force_cow) { |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 1567 | ret = run_delalloc_nocow(inode, locked_page, start, end, |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 1568 | page_started, 1, nr_written); |
Wang Shilong | 47059d9 | 2014-07-03 18:22:07 +0800 | [diff] [blame] | 1569 | } else if (BTRFS_I(inode)->flags & BTRFS_INODE_PREALLOC && !force_cow) { |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 1570 | ret = run_delalloc_nocow(inode, locked_page, start, end, |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 1571 | page_started, 0, nr_written); |
Wang Shilong | 7816030 | 2014-07-17 11:44:10 +0800 | [diff] [blame] | 1572 | } else if (!inode_need_compress(inode)) { |
Wang Xiaoguang | dda3245 | 2016-07-11 11:05:29 +0800 | [diff] [blame] | 1573 | ret = cow_file_range(inode, locked_page, start, end, end, |
| 1574 | page_started, nr_written, 1, NULL); |
Josef Bacik | 7ddf5a4 | 2012-06-08 15:26:47 -0400 | [diff] [blame] | 1575 | } else { |
| 1576 | set_bit(BTRFS_INODE_HAS_ASYNC_EXTENT, |
| 1577 | &BTRFS_I(inode)->runtime_flags); |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 1578 | ret = cow_file_range_async(inode, locked_page, start, end, |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 1579 | page_started, nr_written); |
Josef Bacik | 7ddf5a4 | 2012-06-08 15:26:47 -0400 | [diff] [blame] | 1580 | } |
Chris Mason | b888db2 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 1581 | return ret; |
| 1582 | } |
| 1583 | |
Jeff Mahoney | 1bf8504 | 2011-07-21 16:56:09 +0000 | [diff] [blame] | 1584 | static void btrfs_split_extent_hook(struct inode *inode, |
| 1585 | struct extent_state *orig, u64 split) |
Josef Bacik | 9ed74f2 | 2009-09-11 16:12:44 -0400 | [diff] [blame] | 1586 | { |
Josef Bacik | dcab6a3 | 2015-02-11 15:08:59 -0500 | [diff] [blame] | 1587 | u64 size; |
| 1588 | |
Yan, Zheng | 0ca1f7c | 2010-05-16 10:48:47 -0400 | [diff] [blame] | 1589 | /* not delalloc, ignore it */ |
Josef Bacik | 9ed74f2 | 2009-09-11 16:12:44 -0400 | [diff] [blame] | 1590 | if (!(orig->state & EXTENT_DELALLOC)) |
Jeff Mahoney | 1bf8504 | 2011-07-21 16:56:09 +0000 | [diff] [blame] | 1591 | return; |
Josef Bacik | 9ed74f2 | 2009-09-11 16:12:44 -0400 | [diff] [blame] | 1592 | |
Josef Bacik | dcab6a3 | 2015-02-11 15:08:59 -0500 | [diff] [blame] | 1593 | size = orig->end - orig->start + 1; |
| 1594 | if (size > BTRFS_MAX_EXTENT_SIZE) { |
| 1595 | u64 num_extents; |
| 1596 | u64 new_size; |
| 1597 | |
| 1598 | /* |
Josef Bacik | ba11721 | 2015-03-13 15:01:24 -0400 | [diff] [blame] | 1599 | * See the explanation in btrfs_merge_extent_hook, the same |
| 1600 | * applies here, just in reverse. |
Josef Bacik | dcab6a3 | 2015-02-11 15:08:59 -0500 | [diff] [blame] | 1601 | */ |
| 1602 | new_size = orig->end - split + 1; |
Josef Bacik | ba11721 | 2015-03-13 15:01:24 -0400 | [diff] [blame] | 1603 | num_extents = div64_u64(new_size + BTRFS_MAX_EXTENT_SIZE - 1, |
Josef Bacik | dcab6a3 | 2015-02-11 15:08:59 -0500 | [diff] [blame] | 1604 | BTRFS_MAX_EXTENT_SIZE); |
Josef Bacik | ba11721 | 2015-03-13 15:01:24 -0400 | [diff] [blame] | 1605 | new_size = split - orig->start; |
| 1606 | num_extents += div64_u64(new_size + BTRFS_MAX_EXTENT_SIZE - 1, |
| 1607 | BTRFS_MAX_EXTENT_SIZE); |
| 1608 | if (div64_u64(size + BTRFS_MAX_EXTENT_SIZE - 1, |
| 1609 | BTRFS_MAX_EXTENT_SIZE) >= num_extents) |
Josef Bacik | dcab6a3 | 2015-02-11 15:08:59 -0500 | [diff] [blame] | 1610 | return; |
| 1611 | } |
| 1612 | |
Josef Bacik | 9e0baf6 | 2011-07-15 15:16:44 +0000 | [diff] [blame] | 1613 | spin_lock(&BTRFS_I(inode)->lock); |
| 1614 | BTRFS_I(inode)->outstanding_extents++; |
| 1615 | spin_unlock(&BTRFS_I(inode)->lock); |
Josef Bacik | 9ed74f2 | 2009-09-11 16:12:44 -0400 | [diff] [blame] | 1616 | } |
| 1617 | |
| 1618 | /* |
| 1619 | * extent_io.c merge_extent_hook, used to track merged delayed allocation |
| 1620 | * extents so we can keep track of new extents that are just merged onto old |
| 1621 | * extents, such as when we are doing sequential writes, so we can properly |
| 1622 | * account for the metadata space we'll need. |
| 1623 | */ |
Jeff Mahoney | 1bf8504 | 2011-07-21 16:56:09 +0000 | [diff] [blame] | 1624 | static void btrfs_merge_extent_hook(struct inode *inode, |
| 1625 | struct extent_state *new, |
| 1626 | struct extent_state *other) |
Josef Bacik | 9ed74f2 | 2009-09-11 16:12:44 -0400 | [diff] [blame] | 1627 | { |
Josef Bacik | dcab6a3 | 2015-02-11 15:08:59 -0500 | [diff] [blame] | 1628 | u64 new_size, old_size; |
| 1629 | u64 num_extents; |
| 1630 | |
Josef Bacik | 9ed74f2 | 2009-09-11 16:12:44 -0400 | [diff] [blame] | 1631 | /* not delalloc, ignore it */ |
| 1632 | if (!(other->state & EXTENT_DELALLOC)) |
Jeff Mahoney | 1bf8504 | 2011-07-21 16:56:09 +0000 | [diff] [blame] | 1633 | return; |
Josef Bacik | 9ed74f2 | 2009-09-11 16:12:44 -0400 | [diff] [blame] | 1634 | |
Josef Bacik | 8461a3d | 2015-03-13 15:12:08 -0400 | [diff] [blame] | 1635 | if (new->start > other->start) |
| 1636 | new_size = new->end - other->start + 1; |
| 1637 | else |
| 1638 | new_size = other->end - new->start + 1; |
Josef Bacik | dcab6a3 | 2015-02-11 15:08:59 -0500 | [diff] [blame] | 1639 | |
| 1640 | /* we're not bigger than the max, unreserve the space and go */ |
| 1641 | if (new_size <= BTRFS_MAX_EXTENT_SIZE) { |
| 1642 | spin_lock(&BTRFS_I(inode)->lock); |
| 1643 | BTRFS_I(inode)->outstanding_extents--; |
| 1644 | spin_unlock(&BTRFS_I(inode)->lock); |
| 1645 | return; |
| 1646 | } |
| 1647 | |
| 1648 | /* |
Josef Bacik | ba11721 | 2015-03-13 15:01:24 -0400 | [diff] [blame] | 1649 | * We have to add up either side to figure out how many extents were |
| 1650 | * accounted for before we merged into one big extent. If the number of |
| 1651 | * extents we accounted for is <= the amount we need for the new range |
| 1652 | * then we can return, otherwise drop. Think of it like this |
| 1653 | * |
| 1654 | * [ 4k][MAX_SIZE] |
| 1655 | * |
| 1656 | * So we've grown the extent by a MAX_SIZE extent, this would mean we |
| 1657 | * need 2 outstanding extents, on one side we have 1 and the other side |
| 1658 | * we have 1 so they are == and we can return. But in this case |
| 1659 | * |
| 1660 | * [MAX_SIZE+4k][MAX_SIZE+4k] |
| 1661 | * |
| 1662 | * Each range on their own accounts for 2 extents, but merged together |
| 1663 | * they are only 3 extents worth of accounting, so we need to drop in |
| 1664 | * this case. |
Josef Bacik | dcab6a3 | 2015-02-11 15:08:59 -0500 | [diff] [blame] | 1665 | */ |
Josef Bacik | ba11721 | 2015-03-13 15:01:24 -0400 | [diff] [blame] | 1666 | old_size = other->end - other->start + 1; |
Josef Bacik | dcab6a3 | 2015-02-11 15:08:59 -0500 | [diff] [blame] | 1667 | num_extents = div64_u64(old_size + BTRFS_MAX_EXTENT_SIZE - 1, |
| 1668 | BTRFS_MAX_EXTENT_SIZE); |
Josef Bacik | ba11721 | 2015-03-13 15:01:24 -0400 | [diff] [blame] | 1669 | old_size = new->end - new->start + 1; |
| 1670 | num_extents += div64_u64(old_size + BTRFS_MAX_EXTENT_SIZE - 1, |
| 1671 | BTRFS_MAX_EXTENT_SIZE); |
| 1672 | |
Josef Bacik | dcab6a3 | 2015-02-11 15:08:59 -0500 | [diff] [blame] | 1673 | if (div64_u64(new_size + BTRFS_MAX_EXTENT_SIZE - 1, |
Josef Bacik | ba11721 | 2015-03-13 15:01:24 -0400 | [diff] [blame] | 1674 | BTRFS_MAX_EXTENT_SIZE) >= num_extents) |
Josef Bacik | dcab6a3 | 2015-02-11 15:08:59 -0500 | [diff] [blame] | 1675 | return; |
| 1676 | |
Josef Bacik | 9e0baf6 | 2011-07-15 15:16:44 +0000 | [diff] [blame] | 1677 | spin_lock(&BTRFS_I(inode)->lock); |
| 1678 | BTRFS_I(inode)->outstanding_extents--; |
| 1679 | spin_unlock(&BTRFS_I(inode)->lock); |
Josef Bacik | 9ed74f2 | 2009-09-11 16:12:44 -0400 | [diff] [blame] | 1680 | } |
| 1681 | |
Miao Xie | eb73c1b | 2013-05-15 07:48:22 +0000 | [diff] [blame] | 1682 | static void btrfs_add_delalloc_inodes(struct btrfs_root *root, |
| 1683 | struct inode *inode) |
| 1684 | { |
| 1685 | spin_lock(&root->delalloc_lock); |
| 1686 | if (list_empty(&BTRFS_I(inode)->delalloc_inodes)) { |
| 1687 | list_add_tail(&BTRFS_I(inode)->delalloc_inodes, |
| 1688 | &root->delalloc_inodes); |
| 1689 | set_bit(BTRFS_INODE_IN_DELALLOC_LIST, |
| 1690 | &BTRFS_I(inode)->runtime_flags); |
| 1691 | root->nr_delalloc_inodes++; |
| 1692 | if (root->nr_delalloc_inodes == 1) { |
| 1693 | spin_lock(&root->fs_info->delalloc_root_lock); |
| 1694 | BUG_ON(!list_empty(&root->delalloc_root)); |
| 1695 | list_add_tail(&root->delalloc_root, |
| 1696 | &root->fs_info->delalloc_roots); |
| 1697 | spin_unlock(&root->fs_info->delalloc_root_lock); |
| 1698 | } |
| 1699 | } |
| 1700 | spin_unlock(&root->delalloc_lock); |
| 1701 | } |
| 1702 | |
| 1703 | static void btrfs_del_delalloc_inode(struct btrfs_root *root, |
| 1704 | struct inode *inode) |
| 1705 | { |
| 1706 | spin_lock(&root->delalloc_lock); |
| 1707 | if (!list_empty(&BTRFS_I(inode)->delalloc_inodes)) { |
| 1708 | list_del_init(&BTRFS_I(inode)->delalloc_inodes); |
| 1709 | clear_bit(BTRFS_INODE_IN_DELALLOC_LIST, |
| 1710 | &BTRFS_I(inode)->runtime_flags); |
| 1711 | root->nr_delalloc_inodes--; |
| 1712 | if (!root->nr_delalloc_inodes) { |
| 1713 | spin_lock(&root->fs_info->delalloc_root_lock); |
| 1714 | BUG_ON(list_empty(&root->delalloc_root)); |
| 1715 | list_del_init(&root->delalloc_root); |
| 1716 | spin_unlock(&root->fs_info->delalloc_root_lock); |
| 1717 | } |
| 1718 | } |
| 1719 | spin_unlock(&root->delalloc_lock); |
| 1720 | } |
| 1721 | |
Chris Mason | d352ac6 | 2008-09-29 15:18:18 -0400 | [diff] [blame] | 1722 | /* |
| 1723 | * extent_io.c set_bit_hook, used to track delayed allocation |
| 1724 | * bytes in this file, and to maintain the list of inodes that |
| 1725 | * have pending delalloc work to be done. |
| 1726 | */ |
Jeff Mahoney | 1bf8504 | 2011-07-21 16:56:09 +0000 | [diff] [blame] | 1727 | static void btrfs_set_bit_hook(struct inode *inode, |
David Sterba | 9ee49a04 | 2015-01-14 19:52:13 +0100 | [diff] [blame] | 1728 | struct extent_state *state, unsigned *bits) |
Chris Mason | 291d673 | 2008-01-29 15:55:23 -0500 | [diff] [blame] | 1729 | { |
Josef Bacik | 9ed74f2 | 2009-09-11 16:12:44 -0400 | [diff] [blame] | 1730 | |
Wang Shilong | 47059d9 | 2014-07-03 18:22:07 +0800 | [diff] [blame] | 1731 | if ((*bits & EXTENT_DEFRAG) && !(*bits & EXTENT_DELALLOC)) |
| 1732 | WARN_ON(1); |
Chris Mason | 75eff68 | 2008-12-15 15:54:40 -0500 | [diff] [blame] | 1733 | /* |
| 1734 | * set_bit and clear bit hooks normally require _irqsave/restore |
Sergei Trofimovich | 27160b6 | 2011-05-20 20:20:32 +0000 | [diff] [blame] | 1735 | * but in this case, we are only testing for the DELALLOC |
Chris Mason | 75eff68 | 2008-12-15 15:54:40 -0500 | [diff] [blame] | 1736 | * bit, which is only set or cleared with irqs on |
| 1737 | */ |
Yan, Zheng | 0ca1f7c | 2010-05-16 10:48:47 -0400 | [diff] [blame] | 1738 | if (!(state->state & EXTENT_DELALLOC) && (*bits & EXTENT_DELALLOC)) { |
Chris Mason | 291d673 | 2008-01-29 15:55:23 -0500 | [diff] [blame] | 1739 | struct btrfs_root *root = BTRFS_I(inode)->root; |
Yan, Zheng | 0ca1f7c | 2010-05-16 10:48:47 -0400 | [diff] [blame] | 1740 | u64 len = state->end + 1 - state->start; |
Liu Bo | 83eea1f | 2012-07-10 05:28:39 -0600 | [diff] [blame] | 1741 | bool do_list = !btrfs_is_free_space_inode(inode); |
Josef Bacik | 9ed74f2 | 2009-09-11 16:12:44 -0400 | [diff] [blame] | 1742 | |
Josef Bacik | 9e0baf6 | 2011-07-15 15:16:44 +0000 | [diff] [blame] | 1743 | if (*bits & EXTENT_FIRST_DELALLOC) { |
Yan, Zheng | 0ca1f7c | 2010-05-16 10:48:47 -0400 | [diff] [blame] | 1744 | *bits &= ~EXTENT_FIRST_DELALLOC; |
Josef Bacik | 9e0baf6 | 2011-07-15 15:16:44 +0000 | [diff] [blame] | 1745 | } else { |
| 1746 | spin_lock(&BTRFS_I(inode)->lock); |
| 1747 | BTRFS_I(inode)->outstanding_extents++; |
| 1748 | spin_unlock(&BTRFS_I(inode)->lock); |
| 1749 | } |
Josef Bacik | 287a0ab | 2010-03-19 18:07:23 +0000 | [diff] [blame] | 1750 | |
Josef Bacik | 6a3891c | 2015-03-16 17:38:52 -0400 | [diff] [blame] | 1751 | /* For sanity tests */ |
Jeff Mahoney | f5ee5c9 | 2016-06-21 09:52:41 -0400 | [diff] [blame] | 1752 | if (btrfs_is_testing(root->fs_info)) |
Josef Bacik | 6a3891c | 2015-03-16 17:38:52 -0400 | [diff] [blame] | 1753 | return; |
| 1754 | |
Miao Xie | 963d678 | 2013-01-29 10:10:51 +0000 | [diff] [blame] | 1755 | __percpu_counter_add(&root->fs_info->delalloc_bytes, len, |
| 1756 | root->fs_info->delalloc_batch); |
Miao Xie | df0af1a | 2013-01-29 10:11:59 +0000 | [diff] [blame] | 1757 | spin_lock(&BTRFS_I(inode)->lock); |
Yan, Zheng | 0ca1f7c | 2010-05-16 10:48:47 -0400 | [diff] [blame] | 1758 | BTRFS_I(inode)->delalloc_bytes += len; |
Wang Shilong | 47059d9 | 2014-07-03 18:22:07 +0800 | [diff] [blame] | 1759 | if (*bits & EXTENT_DEFRAG) |
| 1760 | BTRFS_I(inode)->defrag_bytes += len; |
Miao Xie | df0af1a | 2013-01-29 10:11:59 +0000 | [diff] [blame] | 1761 | if (do_list && !test_bit(BTRFS_INODE_IN_DELALLOC_LIST, |
Miao Xie | eb73c1b | 2013-05-15 07:48:22 +0000 | [diff] [blame] | 1762 | &BTRFS_I(inode)->runtime_flags)) |
| 1763 | btrfs_add_delalloc_inodes(root, inode); |
Miao Xie | df0af1a | 2013-01-29 10:11:59 +0000 | [diff] [blame] | 1764 | spin_unlock(&BTRFS_I(inode)->lock); |
Chris Mason | 291d673 | 2008-01-29 15:55:23 -0500 | [diff] [blame] | 1765 | } |
Chris Mason | 291d673 | 2008-01-29 15:55:23 -0500 | [diff] [blame] | 1766 | } |
| 1767 | |
Chris Mason | d352ac6 | 2008-09-29 15:18:18 -0400 | [diff] [blame] | 1768 | /* |
| 1769 | * extent_io.c clear_bit_hook, see set_bit_hook for why |
| 1770 | */ |
Jeff Mahoney | 1bf8504 | 2011-07-21 16:56:09 +0000 | [diff] [blame] | 1771 | static void btrfs_clear_bit_hook(struct inode *inode, |
David Sterba | 4107488 | 2013-04-29 13:38:46 +0000 | [diff] [blame] | 1772 | struct extent_state *state, |
David Sterba | 9ee49a04 | 2015-01-14 19:52:13 +0100 | [diff] [blame] | 1773 | unsigned *bits) |
Chris Mason | 291d673 | 2008-01-29 15:55:23 -0500 | [diff] [blame] | 1774 | { |
Wang Shilong | 47059d9 | 2014-07-03 18:22:07 +0800 | [diff] [blame] | 1775 | u64 len = state->end + 1 - state->start; |
Josef Bacik | dcab6a3 | 2015-02-11 15:08:59 -0500 | [diff] [blame] | 1776 | u64 num_extents = div64_u64(len + BTRFS_MAX_EXTENT_SIZE -1, |
| 1777 | BTRFS_MAX_EXTENT_SIZE); |
Wang Shilong | 47059d9 | 2014-07-03 18:22:07 +0800 | [diff] [blame] | 1778 | |
| 1779 | spin_lock(&BTRFS_I(inode)->lock); |
| 1780 | if ((state->state & EXTENT_DEFRAG) && (*bits & EXTENT_DEFRAG)) |
| 1781 | BTRFS_I(inode)->defrag_bytes -= len; |
| 1782 | spin_unlock(&BTRFS_I(inode)->lock); |
| 1783 | |
Chris Mason | 75eff68 | 2008-12-15 15:54:40 -0500 | [diff] [blame] | 1784 | /* |
| 1785 | * set_bit and clear bit hooks normally require _irqsave/restore |
Sergei Trofimovich | 27160b6 | 2011-05-20 20:20:32 +0000 | [diff] [blame] | 1786 | * but in this case, we are only testing for the DELALLOC |
Chris Mason | 75eff68 | 2008-12-15 15:54:40 -0500 | [diff] [blame] | 1787 | * bit, which is only set or cleared with irqs on |
| 1788 | */ |
Yan, Zheng | 0ca1f7c | 2010-05-16 10:48:47 -0400 | [diff] [blame] | 1789 | if ((state->state & EXTENT_DELALLOC) && (*bits & EXTENT_DELALLOC)) { |
Chris Mason | 291d673 | 2008-01-29 15:55:23 -0500 | [diff] [blame] | 1790 | struct btrfs_root *root = BTRFS_I(inode)->root; |
Liu Bo | 83eea1f | 2012-07-10 05:28:39 -0600 | [diff] [blame] | 1791 | bool do_list = !btrfs_is_free_space_inode(inode); |
Chris Mason | bcbfce8 | 2008-04-22 13:26:47 -0400 | [diff] [blame] | 1792 | |
Josef Bacik | 9e0baf6 | 2011-07-15 15:16:44 +0000 | [diff] [blame] | 1793 | if (*bits & EXTENT_FIRST_DELALLOC) { |
Yan, Zheng | 0ca1f7c | 2010-05-16 10:48:47 -0400 | [diff] [blame] | 1794 | *bits &= ~EXTENT_FIRST_DELALLOC; |
Josef Bacik | 9e0baf6 | 2011-07-15 15:16:44 +0000 | [diff] [blame] | 1795 | } else if (!(*bits & EXTENT_DO_ACCOUNTING)) { |
| 1796 | spin_lock(&BTRFS_I(inode)->lock); |
Josef Bacik | dcab6a3 | 2015-02-11 15:08:59 -0500 | [diff] [blame] | 1797 | BTRFS_I(inode)->outstanding_extents -= num_extents; |
Josef Bacik | 9e0baf6 | 2011-07-15 15:16:44 +0000 | [diff] [blame] | 1798 | spin_unlock(&BTRFS_I(inode)->lock); |
| 1799 | } |
Yan, Zheng | 0ca1f7c | 2010-05-16 10:48:47 -0400 | [diff] [blame] | 1800 | |
Josef Bacik | b6d08f0 | 2013-09-27 14:57:43 -0400 | [diff] [blame] | 1801 | /* |
| 1802 | * We don't reserve metadata space for space cache inodes so we |
| 1803 | * don't need to call dellalloc_release_metadata if there is an |
| 1804 | * error. |
| 1805 | */ |
| 1806 | if (*bits & EXTENT_DO_ACCOUNTING && |
| 1807 | root != root->fs_info->tree_root) |
Yan, Zheng | 0ca1f7c | 2010-05-16 10:48:47 -0400 | [diff] [blame] | 1808 | btrfs_delalloc_release_metadata(inode, len); |
| 1809 | |
Josef Bacik | 6a3891c | 2015-03-16 17:38:52 -0400 | [diff] [blame] | 1810 | /* For sanity tests. */ |
Jeff Mahoney | f5ee5c9 | 2016-06-21 09:52:41 -0400 | [diff] [blame] | 1811 | if (btrfs_is_testing(root->fs_info)) |
Josef Bacik | 6a3891c | 2015-03-16 17:38:52 -0400 | [diff] [blame] | 1812 | return; |
| 1813 | |
Josef Bacik | 0cb59c9 | 2010-07-02 12:14:14 -0400 | [diff] [blame] | 1814 | if (root->root_key.objectid != BTRFS_DATA_RELOC_TREE_OBJECTID |
Wang Xiaoguang | 1851309 | 2016-07-25 15:51:40 +0800 | [diff] [blame] | 1815 | && do_list && !(state->state & EXTENT_NORESERVE) |
| 1816 | && (*bits & (EXTENT_DO_ACCOUNTING | |
| 1817 | EXTENT_CLEAR_DATA_RESV))) |
Qu Wenruo | 51773be | 2015-10-08 18:19:37 +0800 | [diff] [blame] | 1818 | btrfs_free_reserved_data_space_noquota(inode, |
| 1819 | state->start, len); |
Josef Bacik | 9ed74f2 | 2009-09-11 16:12:44 -0400 | [diff] [blame] | 1820 | |
Miao Xie | 963d678 | 2013-01-29 10:10:51 +0000 | [diff] [blame] | 1821 | __percpu_counter_add(&root->fs_info->delalloc_bytes, -len, |
| 1822 | root->fs_info->delalloc_batch); |
Miao Xie | df0af1a | 2013-01-29 10:11:59 +0000 | [diff] [blame] | 1823 | spin_lock(&BTRFS_I(inode)->lock); |
Yan, Zheng | 0ca1f7c | 2010-05-16 10:48:47 -0400 | [diff] [blame] | 1824 | BTRFS_I(inode)->delalloc_bytes -= len; |
Josef Bacik | 0cb59c9 | 2010-07-02 12:14:14 -0400 | [diff] [blame] | 1825 | if (do_list && BTRFS_I(inode)->delalloc_bytes == 0 && |
Miao Xie | df0af1a | 2013-01-29 10:11:59 +0000 | [diff] [blame] | 1826 | test_bit(BTRFS_INODE_IN_DELALLOC_LIST, |
Miao Xie | eb73c1b | 2013-05-15 07:48:22 +0000 | [diff] [blame] | 1827 | &BTRFS_I(inode)->runtime_flags)) |
| 1828 | btrfs_del_delalloc_inode(root, inode); |
Miao Xie | df0af1a | 2013-01-29 10:11:59 +0000 | [diff] [blame] | 1829 | spin_unlock(&BTRFS_I(inode)->lock); |
Chris Mason | 291d673 | 2008-01-29 15:55:23 -0500 | [diff] [blame] | 1830 | } |
Chris Mason | 291d673 | 2008-01-29 15:55:23 -0500 | [diff] [blame] | 1831 | } |
| 1832 | |
Chris Mason | d352ac6 | 2008-09-29 15:18:18 -0400 | [diff] [blame] | 1833 | /* |
| 1834 | * extent_io.c merge_bio_hook, this must check the chunk tree to make sure |
| 1835 | * we don't create bios that span stripes or chunks |
Liu Bo | 6f034ec | 2016-06-22 18:31:49 -0700 | [diff] [blame] | 1836 | * |
| 1837 | * return 1 if page cannot be merged to bio |
| 1838 | * return 0 if page can be merged to bio |
| 1839 | * return error otherwise |
Chris Mason | d352ac6 | 2008-09-29 15:18:18 -0400 | [diff] [blame] | 1840 | */ |
Mike Christie | 81a75f6 | 2016-06-05 14:31:54 -0500 | [diff] [blame] | 1841 | int btrfs_merge_bio_hook(struct page *page, unsigned long offset, |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 1842 | size_t size, struct bio *bio, |
| 1843 | unsigned long bio_flags) |
Chris Mason | 239b14b | 2008-03-24 15:02:07 -0400 | [diff] [blame] | 1844 | { |
| 1845 | struct btrfs_root *root = BTRFS_I(page->mapping->host)->root; |
Kent Overstreet | 4f024f3 | 2013-10-11 15:44:27 -0700 | [diff] [blame] | 1846 | u64 logical = (u64)bio->bi_iter.bi_sector << 9; |
Chris Mason | 239b14b | 2008-03-24 15:02:07 -0400 | [diff] [blame] | 1847 | u64 length = 0; |
| 1848 | u64 map_length; |
Chris Mason | 239b14b | 2008-03-24 15:02:07 -0400 | [diff] [blame] | 1849 | int ret; |
| 1850 | |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 1851 | if (bio_flags & EXTENT_BIO_COMPRESSED) |
| 1852 | return 0; |
| 1853 | |
Kent Overstreet | 4f024f3 | 2013-10-11 15:44:27 -0700 | [diff] [blame] | 1854 | length = bio->bi_iter.bi_size; |
Chris Mason | 239b14b | 2008-03-24 15:02:07 -0400 | [diff] [blame] | 1855 | map_length = length; |
Mike Christie | b3d3fa5 | 2016-06-05 14:31:53 -0500 | [diff] [blame] | 1856 | ret = btrfs_map_block(root->fs_info, bio_op(bio), logical, |
Chris Mason | f188591 | 2008-04-09 16:28:12 -0400 | [diff] [blame] | 1857 | &map_length, NULL, 0); |
Liu Bo | 6f034ec | 2016-06-22 18:31:49 -0700 | [diff] [blame] | 1858 | if (ret < 0) |
| 1859 | return ret; |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 1860 | if (map_length < length + size) |
Chris Mason | 239b14b | 2008-03-24 15:02:07 -0400 | [diff] [blame] | 1861 | return 1; |
Jeff Mahoney | 3444a97 | 2011-10-03 23:23:13 -0400 | [diff] [blame] | 1862 | return 0; |
Chris Mason | 239b14b | 2008-03-24 15:02:07 -0400 | [diff] [blame] | 1863 | } |
| 1864 | |
Chris Mason | d352ac6 | 2008-09-29 15:18:18 -0400 | [diff] [blame] | 1865 | /* |
| 1866 | * in order to insert checksums into the metadata in large chunks, |
| 1867 | * we wait until bio submission time. All the pages in the bio are |
| 1868 | * checksummed and sums are attached onto the ordered extent record. |
| 1869 | * |
| 1870 | * At IO completion time the cums attached on the ordered extent record |
| 1871 | * are inserted into the btree |
| 1872 | */ |
Mike Christie | 81a75f6 | 2016-06-05 14:31:54 -0500 | [diff] [blame] | 1873 | static int __btrfs_submit_bio_start(struct inode *inode, struct bio *bio, |
| 1874 | int mirror_num, unsigned long bio_flags, |
Chris Mason | eaf25d9 | 2010-05-25 09:48:28 -0400 | [diff] [blame] | 1875 | u64 bio_offset) |
Chris Mason | 065631f | 2008-02-20 12:07:25 -0500 | [diff] [blame] | 1876 | { |
Chris Mason | 065631f | 2008-02-20 12:07:25 -0500 | [diff] [blame] | 1877 | struct btrfs_root *root = BTRFS_I(inode)->root; |
Chris Mason | 065631f | 2008-02-20 12:07:25 -0500 | [diff] [blame] | 1878 | int ret = 0; |
Chris Mason | e015640 | 2008-04-16 11:15:20 -0400 | [diff] [blame] | 1879 | |
Chris Mason | d20f704 | 2008-12-08 16:58:54 -0500 | [diff] [blame] | 1880 | ret = btrfs_csum_one_bio(root, inode, bio, 0, 0); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 1881 | BUG_ON(ret); /* -ENOMEM */ |
Chris Mason | 4a69a41 | 2008-11-06 22:03:00 -0500 | [diff] [blame] | 1882 | return 0; |
| 1883 | } |
Chris Mason | e015640 | 2008-04-16 11:15:20 -0400 | [diff] [blame] | 1884 | |
Chris Mason | 4a69a41 | 2008-11-06 22:03:00 -0500 | [diff] [blame] | 1885 | /* |
| 1886 | * in order to insert checksums into the metadata in large chunks, |
| 1887 | * we wait until bio submission time. All the pages in the bio are |
| 1888 | * checksummed and sums are attached onto the ordered extent record. |
| 1889 | * |
| 1890 | * At IO completion time the cums attached on the ordered extent record |
| 1891 | * are inserted into the btree |
| 1892 | */ |
Mike Christie | 81a75f6 | 2016-06-05 14:31:54 -0500 | [diff] [blame] | 1893 | static int __btrfs_submit_bio_done(struct inode *inode, struct bio *bio, |
Chris Mason | eaf25d9 | 2010-05-25 09:48:28 -0400 | [diff] [blame] | 1894 | int mirror_num, unsigned long bio_flags, |
| 1895 | u64 bio_offset) |
Chris Mason | 4a69a41 | 2008-11-06 22:03:00 -0500 | [diff] [blame] | 1896 | { |
| 1897 | struct btrfs_root *root = BTRFS_I(inode)->root; |
Stefan Behrens | 6189192 | 2012-11-05 18:51:52 +0100 | [diff] [blame] | 1898 | int ret; |
| 1899 | |
Mike Christie | 81a75f6 | 2016-06-05 14:31:54 -0500 | [diff] [blame] | 1900 | ret = btrfs_map_bio(root, bio, mirror_num, 1); |
Christoph Hellwig | 4246a0b | 2015-07-20 15:29:37 +0200 | [diff] [blame] | 1901 | if (ret) { |
| 1902 | bio->bi_error = ret; |
| 1903 | bio_endio(bio); |
| 1904 | } |
Stefan Behrens | 6189192 | 2012-11-05 18:51:52 +0100 | [diff] [blame] | 1905 | return ret; |
Chris Mason | 44b8bd7 | 2008-04-16 11:14:51 -0400 | [diff] [blame] | 1906 | } |
| 1907 | |
Chris Mason | d352ac6 | 2008-09-29 15:18:18 -0400 | [diff] [blame] | 1908 | /* |
Chris Mason | cad321a | 2008-12-17 14:51:42 -0500 | [diff] [blame] | 1909 | * extent_io.c submission hook. This does the right thing for csum calculation |
| 1910 | * on write, or reading the csums from the tree before a read |
Chris Mason | d352ac6 | 2008-09-29 15:18:18 -0400 | [diff] [blame] | 1911 | */ |
Mike Christie | 81a75f6 | 2016-06-05 14:31:54 -0500 | [diff] [blame] | 1912 | static int btrfs_submit_bio_hook(struct inode *inode, struct bio *bio, |
Chris Mason | eaf25d9 | 2010-05-25 09:48:28 -0400 | [diff] [blame] | 1913 | int mirror_num, unsigned long bio_flags, |
| 1914 | u64 bio_offset) |
Chris Mason | 44b8bd7 | 2008-04-16 11:14:51 -0400 | [diff] [blame] | 1915 | { |
| 1916 | struct btrfs_root *root = BTRFS_I(inode)->root; |
Chandan Rajendra | 0d51e28 | 2015-07-27 15:26:43 +0530 | [diff] [blame] | 1917 | enum btrfs_wq_endio_type metadata = BTRFS_WQ_ENDIO_DATA; |
Chris Mason | 44b8bd7 | 2008-04-16 11:14:51 -0400 | [diff] [blame] | 1918 | int ret = 0; |
Chris Mason | 19b9bdb | 2008-10-30 14:23:13 -0400 | [diff] [blame] | 1919 | int skip_sum; |
Josef Bacik | b812ce2 | 2012-11-16 13:56:32 -0500 | [diff] [blame] | 1920 | int async = !atomic_read(&BTRFS_I(inode)->sync_writers); |
Chris Mason | 44b8bd7 | 2008-04-16 11:14:51 -0400 | [diff] [blame] | 1921 | |
Christoph Hellwig | 6cbff00 | 2009-04-17 10:37:41 +0200 | [diff] [blame] | 1922 | skip_sum = BTRFS_I(inode)->flags & BTRFS_INODE_NODATASUM; |
Chris Mason | cad321a | 2008-12-17 14:51:42 -0500 | [diff] [blame] | 1923 | |
Liu Bo | 83eea1f | 2012-07-10 05:28:39 -0600 | [diff] [blame] | 1924 | if (btrfs_is_free_space_inode(inode)) |
Chandan Rajendra | 0d51e28 | 2015-07-27 15:26:43 +0530 | [diff] [blame] | 1925 | metadata = BTRFS_WQ_ENDIO_FREE_SPACE; |
Jeff Mahoney | 0417341 | 2011-10-03 23:23:12 -0400 | [diff] [blame] | 1926 | |
Mike Christie | 37226b2 | 2016-06-05 14:31:52 -0500 | [diff] [blame] | 1927 | if (bio_op(bio) != REQ_OP_WRITE) { |
Josef Bacik | 5fd0204 | 2012-05-02 14:00:54 -0400 | [diff] [blame] | 1928 | ret = btrfs_bio_wq_end_io(root->fs_info, bio, metadata); |
| 1929 | if (ret) |
Stefan Behrens | 6189192 | 2012-11-05 18:51:52 +0100 | [diff] [blame] | 1930 | goto out; |
Josef Bacik | 5fd0204 | 2012-05-02 14:00:54 -0400 | [diff] [blame] | 1931 | |
Chris Mason | d20f704 | 2008-12-08 16:58:54 -0500 | [diff] [blame] | 1932 | if (bio_flags & EXTENT_BIO_COMPRESSED) { |
Stefan Behrens | 6189192 | 2012-11-05 18:51:52 +0100 | [diff] [blame] | 1933 | ret = btrfs_submit_compressed_read(inode, bio, |
| 1934 | mirror_num, |
| 1935 | bio_flags); |
| 1936 | goto out; |
Tsutomu Itoh | c2db107 | 2011-03-01 06:48:31 +0000 | [diff] [blame] | 1937 | } else if (!skip_sum) { |
| 1938 | ret = btrfs_lookup_bio_sums(root, inode, bio, NULL); |
| 1939 | if (ret) |
Stefan Behrens | 6189192 | 2012-11-05 18:51:52 +0100 | [diff] [blame] | 1940 | goto out; |
Tsutomu Itoh | c2db107 | 2011-03-01 06:48:31 +0000 | [diff] [blame] | 1941 | } |
Chris Mason | 4d1b5fb | 2008-08-20 09:44:52 -0400 | [diff] [blame] | 1942 | goto mapit; |
Josef Bacik | b812ce2 | 2012-11-16 13:56:32 -0500 | [diff] [blame] | 1943 | } else if (async && !skip_sum) { |
Yan Zheng | 17d217f | 2008-12-12 10:03:38 -0500 | [diff] [blame] | 1944 | /* csum items have already been cloned */ |
| 1945 | if (root->root_key.objectid == BTRFS_DATA_RELOC_TREE_OBJECTID) |
| 1946 | goto mapit; |
Chris Mason | 19b9bdb | 2008-10-30 14:23:13 -0400 | [diff] [blame] | 1947 | /* we're doing a write, do the async checksumming */ |
Stefan Behrens | 6189192 | 2012-11-05 18:51:52 +0100 | [diff] [blame] | 1948 | ret = btrfs_wq_submit_bio(BTRFS_I(inode)->root->fs_info, |
Mike Christie | 81a75f6 | 2016-06-05 14:31:54 -0500 | [diff] [blame] | 1949 | inode, bio, mirror_num, |
Chris Mason | eaf25d9 | 2010-05-25 09:48:28 -0400 | [diff] [blame] | 1950 | bio_flags, bio_offset, |
| 1951 | __btrfs_submit_bio_start, |
Chris Mason | 4a69a41 | 2008-11-06 22:03:00 -0500 | [diff] [blame] | 1952 | __btrfs_submit_bio_done); |
Stefan Behrens | 6189192 | 2012-11-05 18:51:52 +0100 | [diff] [blame] | 1953 | goto out; |
Josef Bacik | b812ce2 | 2012-11-16 13:56:32 -0500 | [diff] [blame] | 1954 | } else if (!skip_sum) { |
| 1955 | ret = btrfs_csum_one_bio(root, inode, bio, 0, 0); |
| 1956 | if (ret) |
| 1957 | goto out; |
Chris Mason | 19b9bdb | 2008-10-30 14:23:13 -0400 | [diff] [blame] | 1958 | } |
| 1959 | |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 1960 | mapit: |
Mike Christie | 81a75f6 | 2016-06-05 14:31:54 -0500 | [diff] [blame] | 1961 | ret = btrfs_map_bio(root, bio, mirror_num, 0); |
Stefan Behrens | 6189192 | 2012-11-05 18:51:52 +0100 | [diff] [blame] | 1962 | |
| 1963 | out: |
Christoph Hellwig | 4246a0b | 2015-07-20 15:29:37 +0200 | [diff] [blame] | 1964 | if (ret < 0) { |
| 1965 | bio->bi_error = ret; |
| 1966 | bio_endio(bio); |
| 1967 | } |
Stefan Behrens | 6189192 | 2012-11-05 18:51:52 +0100 | [diff] [blame] | 1968 | return ret; |
Chris Mason | 065631f | 2008-02-20 12:07:25 -0500 | [diff] [blame] | 1969 | } |
Chris Mason | 6885f30 | 2008-02-20 16:11:05 -0500 | [diff] [blame] | 1970 | |
Chris Mason | d352ac6 | 2008-09-29 15:18:18 -0400 | [diff] [blame] | 1971 | /* |
| 1972 | * given a list of ordered sums record them in the inode. This happens |
| 1973 | * at IO completion time based on sums calculated at bio submission time. |
| 1974 | */ |
Chris Mason | ba1da2f | 2008-07-17 12:54:15 -0400 | [diff] [blame] | 1975 | static noinline int add_pending_csums(struct btrfs_trans_handle *trans, |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 1976 | struct inode *inode, u64 file_offset, |
| 1977 | struct list_head *list) |
| 1978 | { |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 1979 | struct btrfs_ordered_sum *sum; |
| 1980 | |
Qinghuang Feng | c6e3087 | 2009-01-21 10:59:08 -0500 | [diff] [blame] | 1981 | list_for_each_entry(sum, list, list) { |
Miao Xie | 39847c4 | 2013-03-28 08:08:20 +0000 | [diff] [blame] | 1982 | trans->adding_csums = 1; |
Chris Mason | d20f704 | 2008-12-08 16:58:54 -0500 | [diff] [blame] | 1983 | btrfs_csum_file_blocks(trans, |
| 1984 | BTRFS_I(inode)->root->fs_info->csum_root, sum); |
Miao Xie | 39847c4 | 2013-03-28 08:08:20 +0000 | [diff] [blame] | 1985 | trans->adding_csums = 0; |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 1986 | } |
| 1987 | return 0; |
| 1988 | } |
| 1989 | |
Josef Bacik | 2ac55d4 | 2010-02-03 19:33:23 +0000 | [diff] [blame] | 1990 | int btrfs_set_extent_delalloc(struct inode *inode, u64 start, u64 end, |
| 1991 | struct extent_state **cached_state) |
Chris Mason | ea8c281 | 2008-08-04 23:17:27 -0400 | [diff] [blame] | 1992 | { |
Kirill A. Shutemov | 09cbfea | 2016-04-01 15:29:47 +0300 | [diff] [blame] | 1993 | WARN_ON((end & (PAGE_SIZE - 1)) == 0); |
Chris Mason | ea8c281 | 2008-08-04 23:17:27 -0400 | [diff] [blame] | 1994 | return set_extent_delalloc(&BTRFS_I(inode)->io_tree, start, end, |
David Sterba | 7cd8c75 | 2016-04-26 23:54:39 +0200 | [diff] [blame] | 1995 | cached_state); |
Chris Mason | ea8c281 | 2008-08-04 23:17:27 -0400 | [diff] [blame] | 1996 | } |
| 1997 | |
Chris Mason | d352ac6 | 2008-09-29 15:18:18 -0400 | [diff] [blame] | 1998 | /* see btrfs_writepage_start_hook for details on why this is required */ |
Chris Mason | 247e743 | 2008-07-17 12:53:51 -0400 | [diff] [blame] | 1999 | struct btrfs_writepage_fixup { |
| 2000 | struct page *page; |
| 2001 | struct btrfs_work work; |
| 2002 | }; |
| 2003 | |
Christoph Hellwig | b295086 | 2008-12-02 09:54:17 -0500 | [diff] [blame] | 2004 | static void btrfs_writepage_fixup_worker(struct btrfs_work *work) |
Chris Mason | 247e743 | 2008-07-17 12:53:51 -0400 | [diff] [blame] | 2005 | { |
| 2006 | struct btrfs_writepage_fixup *fixup; |
| 2007 | struct btrfs_ordered_extent *ordered; |
Josef Bacik | 2ac55d4 | 2010-02-03 19:33:23 +0000 | [diff] [blame] | 2008 | struct extent_state *cached_state = NULL; |
Chris Mason | 247e743 | 2008-07-17 12:53:51 -0400 | [diff] [blame] | 2009 | struct page *page; |
| 2010 | struct inode *inode; |
| 2011 | u64 page_start; |
| 2012 | u64 page_end; |
Jeff Mahoney | 87826df | 2012-02-15 16:23:57 +0100 | [diff] [blame] | 2013 | int ret; |
Chris Mason | 247e743 | 2008-07-17 12:53:51 -0400 | [diff] [blame] | 2014 | |
| 2015 | fixup = container_of(work, struct btrfs_writepage_fixup, work); |
| 2016 | page = fixup->page; |
Chris Mason | 4a09675 | 2008-07-21 10:29:44 -0400 | [diff] [blame] | 2017 | again: |
Chris Mason | 247e743 | 2008-07-17 12:53:51 -0400 | [diff] [blame] | 2018 | lock_page(page); |
| 2019 | if (!page->mapping || !PageDirty(page) || !PageChecked(page)) { |
| 2020 | ClearPageChecked(page); |
| 2021 | goto out_page; |
| 2022 | } |
| 2023 | |
| 2024 | inode = page->mapping->host; |
| 2025 | page_start = page_offset(page); |
Kirill A. Shutemov | 09cbfea | 2016-04-01 15:29:47 +0300 | [diff] [blame] | 2026 | page_end = page_offset(page) + PAGE_SIZE - 1; |
Chris Mason | 247e743 | 2008-07-17 12:53:51 -0400 | [diff] [blame] | 2027 | |
David Sterba | ff13db4 | 2015-12-03 14:30:40 +0100 | [diff] [blame] | 2028 | lock_extent_bits(&BTRFS_I(inode)->io_tree, page_start, page_end, |
Jeff Mahoney | d008237 | 2012-03-01 14:57:19 +0100 | [diff] [blame] | 2029 | &cached_state); |
Chris Mason | 4a09675 | 2008-07-21 10:29:44 -0400 | [diff] [blame] | 2030 | |
| 2031 | /* already ordered? We're done */ |
Chris Mason | 8b62b72 | 2009-09-02 16:53:46 -0400 | [diff] [blame] | 2032 | if (PagePrivate2(page)) |
Chris Mason | 247e743 | 2008-07-17 12:53:51 -0400 | [diff] [blame] | 2033 | goto out; |
Chris Mason | 4a09675 | 2008-07-21 10:29:44 -0400 | [diff] [blame] | 2034 | |
Chandan Rajendra | dbfdb6d | 2016-01-21 15:55:58 +0530 | [diff] [blame] | 2035 | ordered = btrfs_lookup_ordered_range(inode, page_start, |
Kirill A. Shutemov | 09cbfea | 2016-04-01 15:29:47 +0300 | [diff] [blame] | 2036 | PAGE_SIZE); |
Chris Mason | 4a09675 | 2008-07-21 10:29:44 -0400 | [diff] [blame] | 2037 | if (ordered) { |
Josef Bacik | 2ac55d4 | 2010-02-03 19:33:23 +0000 | [diff] [blame] | 2038 | unlock_extent_cached(&BTRFS_I(inode)->io_tree, page_start, |
| 2039 | page_end, &cached_state, GFP_NOFS); |
Chris Mason | 4a09675 | 2008-07-21 10:29:44 -0400 | [diff] [blame] | 2040 | unlock_page(page); |
| 2041 | btrfs_start_ordered_extent(inode, ordered, 1); |
Jeff Mahoney | 87826df | 2012-02-15 16:23:57 +0100 | [diff] [blame] | 2042 | btrfs_put_ordered_extent(ordered); |
Chris Mason | 4a09675 | 2008-07-21 10:29:44 -0400 | [diff] [blame] | 2043 | goto again; |
| 2044 | } |
Chris Mason | 247e743 | 2008-07-17 12:53:51 -0400 | [diff] [blame] | 2045 | |
Qu Wenruo | 7cf5b97 | 2015-09-08 17:25:55 +0800 | [diff] [blame] | 2046 | ret = btrfs_delalloc_reserve_space(inode, page_start, |
Kirill A. Shutemov | 09cbfea | 2016-04-01 15:29:47 +0300 | [diff] [blame] | 2047 | PAGE_SIZE); |
Jeff Mahoney | 87826df | 2012-02-15 16:23:57 +0100 | [diff] [blame] | 2048 | if (ret) { |
| 2049 | mapping_set_error(page->mapping, ret); |
| 2050 | end_extent_writepage(page, ret, page_start, page_end); |
| 2051 | ClearPageChecked(page); |
| 2052 | goto out; |
| 2053 | } |
| 2054 | |
Josef Bacik | 2ac55d4 | 2010-02-03 19:33:23 +0000 | [diff] [blame] | 2055 | btrfs_set_extent_delalloc(inode, page_start, page_end, &cached_state); |
Chris Mason | 247e743 | 2008-07-17 12:53:51 -0400 | [diff] [blame] | 2056 | ClearPageChecked(page); |
Jeff Mahoney | 87826df | 2012-02-15 16:23:57 +0100 | [diff] [blame] | 2057 | set_page_dirty(page); |
Chris Mason | 247e743 | 2008-07-17 12:53:51 -0400 | [diff] [blame] | 2058 | out: |
Josef Bacik | 2ac55d4 | 2010-02-03 19:33:23 +0000 | [diff] [blame] | 2059 | unlock_extent_cached(&BTRFS_I(inode)->io_tree, page_start, page_end, |
| 2060 | &cached_state, GFP_NOFS); |
Chris Mason | 247e743 | 2008-07-17 12:53:51 -0400 | [diff] [blame] | 2061 | out_page: |
| 2062 | unlock_page(page); |
Kirill A. Shutemov | 09cbfea | 2016-04-01 15:29:47 +0300 | [diff] [blame] | 2063 | put_page(page); |
Miao Xie | b897abe | 2011-01-26 16:19:22 +0800 | [diff] [blame] | 2064 | kfree(fixup); |
Chris Mason | 247e743 | 2008-07-17 12:53:51 -0400 | [diff] [blame] | 2065 | } |
| 2066 | |
| 2067 | /* |
| 2068 | * There are a few paths in the higher layers of the kernel that directly |
| 2069 | * set the page dirty bit without asking the filesystem if it is a |
| 2070 | * good idea. This causes problems because we want to make sure COW |
| 2071 | * properly happens and the data=ordered rules are followed. |
| 2072 | * |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 2073 | * 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] | 2074 | * hasn't been properly setup for IO. We kick off an async process |
| 2075 | * to fix it up. The async helper will wait for ordered extents, set |
| 2076 | * the delalloc bit and make it safe to write the page. |
| 2077 | */ |
Christoph Hellwig | b295086 | 2008-12-02 09:54:17 -0500 | [diff] [blame] | 2078 | 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] | 2079 | { |
| 2080 | struct inode *inode = page->mapping->host; |
| 2081 | struct btrfs_writepage_fixup *fixup; |
| 2082 | struct btrfs_root *root = BTRFS_I(inode)->root; |
Chris Mason | 247e743 | 2008-07-17 12:53:51 -0400 | [diff] [blame] | 2083 | |
Chris Mason | 8b62b72 | 2009-09-02 16:53:46 -0400 | [diff] [blame] | 2084 | /* this page is properly in the ordered list */ |
| 2085 | if (TestClearPagePrivate2(page)) |
Chris Mason | 247e743 | 2008-07-17 12:53:51 -0400 | [diff] [blame] | 2086 | return 0; |
| 2087 | |
| 2088 | if (PageChecked(page)) |
| 2089 | return -EAGAIN; |
| 2090 | |
| 2091 | fixup = kzalloc(sizeof(*fixup), GFP_NOFS); |
| 2092 | if (!fixup) |
| 2093 | return -EAGAIN; |
Chris Mason | f421950 | 2008-07-22 11:18:09 -0400 | [diff] [blame] | 2094 | |
Chris Mason | 247e743 | 2008-07-17 12:53:51 -0400 | [diff] [blame] | 2095 | SetPageChecked(page); |
Kirill A. Shutemov | 09cbfea | 2016-04-01 15:29:47 +0300 | [diff] [blame] | 2096 | get_page(page); |
Liu Bo | 9e0af23 | 2014-08-15 23:36:53 +0800 | [diff] [blame] | 2097 | btrfs_init_work(&fixup->work, btrfs_fixup_helper, |
| 2098 | btrfs_writepage_fixup_worker, NULL, NULL); |
Chris Mason | 247e743 | 2008-07-17 12:53:51 -0400 | [diff] [blame] | 2099 | fixup->page = page; |
Qu Wenruo | dc6e320 | 2014-02-28 10:46:14 +0800 | [diff] [blame] | 2100 | btrfs_queue_work(root->fs_info->fixup_workers, &fixup->work); |
Jeff Mahoney | 87826df | 2012-02-15 16:23:57 +0100 | [diff] [blame] | 2101 | return -EBUSY; |
Chris Mason | 247e743 | 2008-07-17 12:53:51 -0400 | [diff] [blame] | 2102 | } |
| 2103 | |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 2104 | static int insert_reserved_file_extent(struct btrfs_trans_handle *trans, |
| 2105 | struct inode *inode, u64 file_pos, |
| 2106 | u64 disk_bytenr, u64 disk_num_bytes, |
| 2107 | u64 num_bytes, u64 ram_bytes, |
| 2108 | u8 compression, u8 encryption, |
| 2109 | u16 other_encoding, int extent_type) |
| 2110 | { |
| 2111 | struct btrfs_root *root = BTRFS_I(inode)->root; |
| 2112 | struct btrfs_file_extent_item *fi; |
| 2113 | struct btrfs_path *path; |
| 2114 | struct extent_buffer *leaf; |
| 2115 | struct btrfs_key ins; |
Filipe David Borba Manana | 1acae57 | 2014-01-07 11:42:27 +0000 | [diff] [blame] | 2116 | int extent_inserted = 0; |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 2117 | int ret; |
| 2118 | |
| 2119 | path = btrfs_alloc_path(); |
Mark Fasheh | d8926bb | 2011-07-13 10:38:47 -0700 | [diff] [blame] | 2120 | if (!path) |
| 2121 | return -ENOMEM; |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 2122 | |
Chris Mason | a1ed835 | 2009-09-11 12:27:37 -0400 | [diff] [blame] | 2123 | /* |
| 2124 | * we may be replacing one extent in the tree with another. |
| 2125 | * The new extent is pinned in the extent map, and we don't want |
| 2126 | * to drop it from the cache until it is completely in the btree. |
| 2127 | * |
| 2128 | * So, tell btrfs_drop_extents to leave this extent in the cache. |
| 2129 | * the caller is expected to unpin it and allow it to be merged |
| 2130 | * with the others. |
| 2131 | */ |
Filipe David Borba Manana | 1acae57 | 2014-01-07 11:42:27 +0000 | [diff] [blame] | 2132 | ret = __btrfs_drop_extents(trans, root, inode, path, file_pos, |
| 2133 | file_pos + num_bytes, NULL, 0, |
| 2134 | 1, sizeof(*fi), &extent_inserted); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 2135 | if (ret) |
| 2136 | goto out; |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 2137 | |
Filipe David Borba Manana | 1acae57 | 2014-01-07 11:42:27 +0000 | [diff] [blame] | 2138 | if (!extent_inserted) { |
| 2139 | ins.objectid = btrfs_ino(inode); |
| 2140 | ins.offset = file_pos; |
| 2141 | ins.type = BTRFS_EXTENT_DATA_KEY; |
| 2142 | |
| 2143 | path->leave_spinning = 1; |
| 2144 | ret = btrfs_insert_empty_item(trans, root, path, &ins, |
| 2145 | sizeof(*fi)); |
| 2146 | if (ret) |
| 2147 | goto out; |
| 2148 | } |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 2149 | leaf = path->nodes[0]; |
| 2150 | fi = btrfs_item_ptr(leaf, path->slots[0], |
| 2151 | struct btrfs_file_extent_item); |
| 2152 | btrfs_set_file_extent_generation(leaf, fi, trans->transid); |
| 2153 | btrfs_set_file_extent_type(leaf, fi, extent_type); |
| 2154 | btrfs_set_file_extent_disk_bytenr(leaf, fi, disk_bytenr); |
| 2155 | btrfs_set_file_extent_disk_num_bytes(leaf, fi, disk_num_bytes); |
| 2156 | btrfs_set_file_extent_offset(leaf, fi, 0); |
| 2157 | btrfs_set_file_extent_num_bytes(leaf, fi, num_bytes); |
| 2158 | btrfs_set_file_extent_ram_bytes(leaf, fi, ram_bytes); |
| 2159 | btrfs_set_file_extent_compression(leaf, fi, compression); |
| 2160 | btrfs_set_file_extent_encryption(leaf, fi, encryption); |
| 2161 | btrfs_set_file_extent_other_encoding(leaf, fi, other_encoding); |
Chris Mason | b947343 | 2009-03-13 11:00:37 -0400 | [diff] [blame] | 2162 | |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 2163 | btrfs_mark_buffer_dirty(leaf); |
Josef Bacik | ce19533 | 2012-09-25 15:26:16 -0400 | [diff] [blame] | 2164 | btrfs_release_path(path); |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 2165 | |
| 2166 | inode_add_bytes(inode, num_bytes); |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 2167 | |
| 2168 | ins.objectid = disk_bytenr; |
| 2169 | ins.offset = disk_num_bytes; |
| 2170 | ins.type = BTRFS_EXTENT_ITEM_KEY; |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 2171 | ret = btrfs_alloc_reserved_file_extent(trans, root, |
| 2172 | root->root_key.objectid, |
Qu Wenruo | 5846a3c | 2015-10-26 14:11:18 +0800 | [diff] [blame] | 2173 | btrfs_ino(inode), file_pos, |
| 2174 | ram_bytes, &ins); |
Qu Wenruo | 297d750 | 2015-09-08 17:08:37 +0800 | [diff] [blame] | 2175 | /* |
Qu Wenruo | 5846a3c | 2015-10-26 14:11:18 +0800 | [diff] [blame] | 2176 | * Release the reserved range from inode dirty range map, as it is |
| 2177 | * already moved into delayed_ref_head |
Qu Wenruo | 297d750 | 2015-09-08 17:08:37 +0800 | [diff] [blame] | 2178 | */ |
| 2179 | btrfs_qgroup_release_data(inode, file_pos, ram_bytes); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 2180 | out: |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 2181 | btrfs_free_path(path); |
Chris Mason | b947343 | 2009-03-13 11:00:37 -0400 | [diff] [blame] | 2182 | |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 2183 | return ret; |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 2184 | } |
| 2185 | |
Liu Bo | 38c227d | 2013-01-29 03:18:40 +0000 | [diff] [blame] | 2186 | /* snapshot-aware defrag */ |
| 2187 | struct sa_defrag_extent_backref { |
| 2188 | struct rb_node node; |
| 2189 | struct old_sa_defrag_extent *old; |
| 2190 | u64 root_id; |
| 2191 | u64 inum; |
| 2192 | u64 file_pos; |
| 2193 | u64 extent_offset; |
| 2194 | u64 num_bytes; |
| 2195 | u64 generation; |
| 2196 | }; |
| 2197 | |
| 2198 | struct old_sa_defrag_extent { |
| 2199 | struct list_head list; |
| 2200 | struct new_sa_defrag_extent *new; |
| 2201 | |
| 2202 | u64 extent_offset; |
| 2203 | u64 bytenr; |
| 2204 | u64 offset; |
| 2205 | u64 len; |
| 2206 | int count; |
| 2207 | }; |
| 2208 | |
| 2209 | struct new_sa_defrag_extent { |
| 2210 | struct rb_root root; |
| 2211 | struct list_head head; |
| 2212 | struct btrfs_path *path; |
| 2213 | struct inode *inode; |
| 2214 | u64 file_pos; |
| 2215 | u64 len; |
| 2216 | u64 bytenr; |
| 2217 | u64 disk_len; |
| 2218 | u8 compress_type; |
| 2219 | }; |
| 2220 | |
| 2221 | static int backref_comp(struct sa_defrag_extent_backref *b1, |
| 2222 | struct sa_defrag_extent_backref *b2) |
| 2223 | { |
| 2224 | if (b1->root_id < b2->root_id) |
| 2225 | return -1; |
| 2226 | else if (b1->root_id > b2->root_id) |
| 2227 | return 1; |
| 2228 | |
| 2229 | if (b1->inum < b2->inum) |
| 2230 | return -1; |
| 2231 | else if (b1->inum > b2->inum) |
| 2232 | return 1; |
| 2233 | |
| 2234 | if (b1->file_pos < b2->file_pos) |
| 2235 | return -1; |
| 2236 | else if (b1->file_pos > b2->file_pos) |
| 2237 | return 1; |
| 2238 | |
| 2239 | /* |
| 2240 | * [------------------------------] ===> (a range of space) |
| 2241 | * |<--->| |<---->| =============> (fs/file tree A) |
| 2242 | * |<---------------------------->| ===> (fs/file tree B) |
| 2243 | * |
| 2244 | * A range of space can refer to two file extents in one tree while |
| 2245 | * refer to only one file extent in another tree. |
| 2246 | * |
| 2247 | * So we may process a disk offset more than one time(two extents in A) |
| 2248 | * and locate at the same extent(one extent in B), then insert two same |
| 2249 | * backrefs(both refer to the extent in B). |
| 2250 | */ |
| 2251 | return 0; |
| 2252 | } |
| 2253 | |
| 2254 | static void backref_insert(struct rb_root *root, |
| 2255 | struct sa_defrag_extent_backref *backref) |
| 2256 | { |
| 2257 | struct rb_node **p = &root->rb_node; |
| 2258 | struct rb_node *parent = NULL; |
| 2259 | struct sa_defrag_extent_backref *entry; |
| 2260 | int ret; |
| 2261 | |
| 2262 | while (*p) { |
| 2263 | parent = *p; |
| 2264 | entry = rb_entry(parent, struct sa_defrag_extent_backref, node); |
| 2265 | |
| 2266 | ret = backref_comp(backref, entry); |
| 2267 | if (ret < 0) |
| 2268 | p = &(*p)->rb_left; |
| 2269 | else |
| 2270 | p = &(*p)->rb_right; |
| 2271 | } |
| 2272 | |
| 2273 | rb_link_node(&backref->node, parent, p); |
| 2274 | rb_insert_color(&backref->node, root); |
| 2275 | } |
| 2276 | |
| 2277 | /* |
| 2278 | * Note the backref might has changed, and in this case we just return 0. |
| 2279 | */ |
| 2280 | static noinline int record_one_backref(u64 inum, u64 offset, u64 root_id, |
| 2281 | void *ctx) |
| 2282 | { |
| 2283 | struct btrfs_file_extent_item *extent; |
| 2284 | struct btrfs_fs_info *fs_info; |
| 2285 | struct old_sa_defrag_extent *old = ctx; |
| 2286 | struct new_sa_defrag_extent *new = old->new; |
| 2287 | struct btrfs_path *path = new->path; |
| 2288 | struct btrfs_key key; |
| 2289 | struct btrfs_root *root; |
| 2290 | struct sa_defrag_extent_backref *backref; |
| 2291 | struct extent_buffer *leaf; |
| 2292 | struct inode *inode = new->inode; |
| 2293 | int slot; |
| 2294 | int ret; |
| 2295 | u64 extent_offset; |
| 2296 | u64 num_bytes; |
| 2297 | |
| 2298 | if (BTRFS_I(inode)->root->root_key.objectid == root_id && |
| 2299 | inum == btrfs_ino(inode)) |
| 2300 | return 0; |
| 2301 | |
| 2302 | key.objectid = root_id; |
| 2303 | key.type = BTRFS_ROOT_ITEM_KEY; |
| 2304 | key.offset = (u64)-1; |
| 2305 | |
| 2306 | fs_info = BTRFS_I(inode)->root->fs_info; |
| 2307 | root = btrfs_read_fs_root_no_name(fs_info, &key); |
| 2308 | if (IS_ERR(root)) { |
| 2309 | if (PTR_ERR(root) == -ENOENT) |
| 2310 | return 0; |
| 2311 | WARN_ON(1); |
| 2312 | pr_debug("inum=%llu, offset=%llu, root_id=%llu\n", |
| 2313 | inum, offset, root_id); |
| 2314 | return PTR_ERR(root); |
| 2315 | } |
| 2316 | |
| 2317 | key.objectid = inum; |
| 2318 | key.type = BTRFS_EXTENT_DATA_KEY; |
| 2319 | if (offset > (u64)-1 << 32) |
| 2320 | key.offset = 0; |
| 2321 | else |
| 2322 | key.offset = offset; |
| 2323 | |
| 2324 | ret = btrfs_search_slot(NULL, root, &key, path, 0, 0); |
Dulshani Gunawardhana | fae7f21 | 2013-10-31 10:30:08 +0530 | [diff] [blame] | 2325 | if (WARN_ON(ret < 0)) |
Liu Bo | 38c227d | 2013-01-29 03:18:40 +0000 | [diff] [blame] | 2326 | return ret; |
Josef Bacik | 50f1319 | 2013-07-22 12:50:37 -0400 | [diff] [blame] | 2327 | ret = 0; |
Liu Bo | 38c227d | 2013-01-29 03:18:40 +0000 | [diff] [blame] | 2328 | |
| 2329 | while (1) { |
| 2330 | cond_resched(); |
| 2331 | |
| 2332 | leaf = path->nodes[0]; |
| 2333 | slot = path->slots[0]; |
| 2334 | |
| 2335 | if (slot >= btrfs_header_nritems(leaf)) { |
| 2336 | ret = btrfs_next_leaf(root, path); |
| 2337 | if (ret < 0) { |
| 2338 | goto out; |
| 2339 | } else if (ret > 0) { |
| 2340 | ret = 0; |
| 2341 | goto out; |
| 2342 | } |
| 2343 | continue; |
| 2344 | } |
| 2345 | |
| 2346 | path->slots[0]++; |
| 2347 | |
| 2348 | btrfs_item_key_to_cpu(leaf, &key, slot); |
| 2349 | |
| 2350 | if (key.objectid > inum) |
| 2351 | goto out; |
| 2352 | |
| 2353 | if (key.objectid < inum || key.type != BTRFS_EXTENT_DATA_KEY) |
| 2354 | continue; |
| 2355 | |
| 2356 | extent = btrfs_item_ptr(leaf, slot, |
| 2357 | struct btrfs_file_extent_item); |
| 2358 | |
| 2359 | if (btrfs_file_extent_disk_bytenr(leaf, extent) != old->bytenr) |
| 2360 | continue; |
| 2361 | |
Liu Bo | e68afa4 | 2013-07-01 22:13:26 +0800 | [diff] [blame] | 2362 | /* |
| 2363 | * 'offset' refers to the exact key.offset, |
| 2364 | * NOT the 'offset' field in btrfs_extent_data_ref, ie. |
| 2365 | * (key.offset - extent_offset). |
| 2366 | */ |
| 2367 | if (key.offset != offset) |
Liu Bo | 38c227d | 2013-01-29 03:18:40 +0000 | [diff] [blame] | 2368 | continue; |
| 2369 | |
Liu Bo | e68afa4 | 2013-07-01 22:13:26 +0800 | [diff] [blame] | 2370 | extent_offset = btrfs_file_extent_offset(leaf, extent); |
Liu Bo | 38c227d | 2013-01-29 03:18:40 +0000 | [diff] [blame] | 2371 | num_bytes = btrfs_file_extent_num_bytes(leaf, extent); |
Liu Bo | e68afa4 | 2013-07-01 22:13:26 +0800 | [diff] [blame] | 2372 | |
Liu Bo | 38c227d | 2013-01-29 03:18:40 +0000 | [diff] [blame] | 2373 | if (extent_offset >= old->extent_offset + old->offset + |
| 2374 | old->len || extent_offset + num_bytes <= |
| 2375 | old->extent_offset + old->offset) |
| 2376 | continue; |
Liu Bo | 38c227d | 2013-01-29 03:18:40 +0000 | [diff] [blame] | 2377 | break; |
| 2378 | } |
| 2379 | |
| 2380 | backref = kmalloc(sizeof(*backref), GFP_NOFS); |
| 2381 | if (!backref) { |
| 2382 | ret = -ENOENT; |
| 2383 | goto out; |
| 2384 | } |
| 2385 | |
| 2386 | backref->root_id = root_id; |
| 2387 | backref->inum = inum; |
Liu Bo | e68afa4 | 2013-07-01 22:13:26 +0800 | [diff] [blame] | 2388 | backref->file_pos = offset; |
Liu Bo | 38c227d | 2013-01-29 03:18:40 +0000 | [diff] [blame] | 2389 | backref->num_bytes = num_bytes; |
| 2390 | backref->extent_offset = extent_offset; |
| 2391 | backref->generation = btrfs_file_extent_generation(leaf, extent); |
| 2392 | backref->old = old; |
| 2393 | backref_insert(&new->root, backref); |
| 2394 | old->count++; |
| 2395 | out: |
| 2396 | btrfs_release_path(path); |
| 2397 | WARN_ON(ret); |
| 2398 | return ret; |
| 2399 | } |
| 2400 | |
| 2401 | static noinline bool record_extent_backrefs(struct btrfs_path *path, |
| 2402 | struct new_sa_defrag_extent *new) |
| 2403 | { |
| 2404 | struct btrfs_fs_info *fs_info = BTRFS_I(new->inode)->root->fs_info; |
| 2405 | struct old_sa_defrag_extent *old, *tmp; |
| 2406 | int ret; |
| 2407 | |
| 2408 | new->path = path; |
| 2409 | |
| 2410 | list_for_each_entry_safe(old, tmp, &new->head, list) { |
Liu Bo | e68afa4 | 2013-07-01 22:13:26 +0800 | [diff] [blame] | 2411 | ret = iterate_inodes_from_logical(old->bytenr + |
| 2412 | old->extent_offset, fs_info, |
Liu Bo | 38c227d | 2013-01-29 03:18:40 +0000 | [diff] [blame] | 2413 | path, record_one_backref, |
| 2414 | old); |
Josef Bacik | 4724b10 | 2013-11-05 11:11:40 -0500 | [diff] [blame] | 2415 | if (ret < 0 && ret != -ENOENT) |
| 2416 | return false; |
Liu Bo | 38c227d | 2013-01-29 03:18:40 +0000 | [diff] [blame] | 2417 | |
| 2418 | /* no backref to be processed for this extent */ |
| 2419 | if (!old->count) { |
| 2420 | list_del(&old->list); |
| 2421 | kfree(old); |
| 2422 | } |
| 2423 | } |
| 2424 | |
| 2425 | if (list_empty(&new->head)) |
| 2426 | return false; |
| 2427 | |
| 2428 | return true; |
| 2429 | } |
| 2430 | |
| 2431 | static int relink_is_mergable(struct extent_buffer *leaf, |
| 2432 | struct btrfs_file_extent_item *fi, |
Liu Bo | 116e002 | 2013-08-02 16:30:40 +0800 | [diff] [blame] | 2433 | struct new_sa_defrag_extent *new) |
Liu Bo | 38c227d | 2013-01-29 03:18:40 +0000 | [diff] [blame] | 2434 | { |
Liu Bo | 116e002 | 2013-08-02 16:30:40 +0800 | [diff] [blame] | 2435 | if (btrfs_file_extent_disk_bytenr(leaf, fi) != new->bytenr) |
Liu Bo | 38c227d | 2013-01-29 03:18:40 +0000 | [diff] [blame] | 2436 | return 0; |
| 2437 | |
| 2438 | if (btrfs_file_extent_type(leaf, fi) != BTRFS_FILE_EXTENT_REG) |
| 2439 | return 0; |
| 2440 | |
Liu Bo | 116e002 | 2013-08-02 16:30:40 +0800 | [diff] [blame] | 2441 | if (btrfs_file_extent_compression(leaf, fi) != new->compress_type) |
| 2442 | return 0; |
| 2443 | |
| 2444 | if (btrfs_file_extent_encryption(leaf, fi) || |
Liu Bo | 38c227d | 2013-01-29 03:18:40 +0000 | [diff] [blame] | 2445 | btrfs_file_extent_other_encoding(leaf, fi)) |
| 2446 | return 0; |
| 2447 | |
| 2448 | return 1; |
| 2449 | } |
| 2450 | |
| 2451 | /* |
| 2452 | * Note the backref might has changed, and in this case we just return 0. |
| 2453 | */ |
| 2454 | static noinline int relink_extent_backref(struct btrfs_path *path, |
| 2455 | struct sa_defrag_extent_backref *prev, |
| 2456 | struct sa_defrag_extent_backref *backref) |
| 2457 | { |
| 2458 | struct btrfs_file_extent_item *extent; |
| 2459 | struct btrfs_file_extent_item *item; |
| 2460 | struct btrfs_ordered_extent *ordered; |
| 2461 | struct btrfs_trans_handle *trans; |
| 2462 | struct btrfs_fs_info *fs_info; |
| 2463 | struct btrfs_root *root; |
| 2464 | struct btrfs_key key; |
| 2465 | struct extent_buffer *leaf; |
| 2466 | struct old_sa_defrag_extent *old = backref->old; |
| 2467 | struct new_sa_defrag_extent *new = old->new; |
| 2468 | struct inode *src_inode = new->inode; |
| 2469 | struct inode *inode; |
| 2470 | struct extent_state *cached = NULL; |
| 2471 | int ret = 0; |
| 2472 | u64 start; |
| 2473 | u64 len; |
| 2474 | u64 lock_start; |
| 2475 | u64 lock_end; |
| 2476 | bool merge = false; |
| 2477 | int index; |
| 2478 | |
| 2479 | if (prev && prev->root_id == backref->root_id && |
| 2480 | prev->inum == backref->inum && |
| 2481 | prev->file_pos + prev->num_bytes == backref->file_pos) |
| 2482 | merge = true; |
| 2483 | |
| 2484 | /* step 1: get root */ |
| 2485 | key.objectid = backref->root_id; |
| 2486 | key.type = BTRFS_ROOT_ITEM_KEY; |
| 2487 | key.offset = (u64)-1; |
| 2488 | |
| 2489 | fs_info = BTRFS_I(src_inode)->root->fs_info; |
| 2490 | index = srcu_read_lock(&fs_info->subvol_srcu); |
| 2491 | |
| 2492 | root = btrfs_read_fs_root_no_name(fs_info, &key); |
| 2493 | if (IS_ERR(root)) { |
| 2494 | srcu_read_unlock(&fs_info->subvol_srcu, index); |
| 2495 | if (PTR_ERR(root) == -ENOENT) |
| 2496 | return 0; |
| 2497 | return PTR_ERR(root); |
| 2498 | } |
Liu Bo | 38c227d | 2013-01-29 03:18:40 +0000 | [diff] [blame] | 2499 | |
Wang Shilong | bcbba5e | 2014-02-08 23:46:35 +0800 | [diff] [blame] | 2500 | if (btrfs_root_readonly(root)) { |
| 2501 | srcu_read_unlock(&fs_info->subvol_srcu, index); |
| 2502 | return 0; |
| 2503 | } |
| 2504 | |
Liu Bo | 38c227d | 2013-01-29 03:18:40 +0000 | [diff] [blame] | 2505 | /* step 2: get inode */ |
| 2506 | key.objectid = backref->inum; |
| 2507 | key.type = BTRFS_INODE_ITEM_KEY; |
| 2508 | key.offset = 0; |
| 2509 | |
| 2510 | inode = btrfs_iget(fs_info->sb, &key, root, NULL); |
| 2511 | if (IS_ERR(inode)) { |
| 2512 | srcu_read_unlock(&fs_info->subvol_srcu, index); |
| 2513 | return 0; |
| 2514 | } |
| 2515 | |
| 2516 | srcu_read_unlock(&fs_info->subvol_srcu, index); |
| 2517 | |
| 2518 | /* step 3: relink backref */ |
| 2519 | lock_start = backref->file_pos; |
| 2520 | lock_end = backref->file_pos + backref->num_bytes - 1; |
| 2521 | lock_extent_bits(&BTRFS_I(inode)->io_tree, lock_start, lock_end, |
David Sterba | ff13db4 | 2015-12-03 14:30:40 +0100 | [diff] [blame] | 2522 | &cached); |
Liu Bo | 38c227d | 2013-01-29 03:18:40 +0000 | [diff] [blame] | 2523 | |
| 2524 | ordered = btrfs_lookup_first_ordered_extent(inode, lock_end); |
| 2525 | if (ordered) { |
| 2526 | btrfs_put_ordered_extent(ordered); |
| 2527 | goto out_unlock; |
| 2528 | } |
| 2529 | |
| 2530 | trans = btrfs_join_transaction(root); |
| 2531 | if (IS_ERR(trans)) { |
| 2532 | ret = PTR_ERR(trans); |
| 2533 | goto out_unlock; |
| 2534 | } |
| 2535 | |
| 2536 | key.objectid = backref->inum; |
| 2537 | key.type = BTRFS_EXTENT_DATA_KEY; |
| 2538 | key.offset = backref->file_pos; |
| 2539 | |
| 2540 | ret = btrfs_search_slot(NULL, root, &key, path, 0, 0); |
| 2541 | if (ret < 0) { |
| 2542 | goto out_free_path; |
| 2543 | } else if (ret > 0) { |
| 2544 | ret = 0; |
| 2545 | goto out_free_path; |
| 2546 | } |
| 2547 | |
| 2548 | extent = btrfs_item_ptr(path->nodes[0], path->slots[0], |
| 2549 | struct btrfs_file_extent_item); |
| 2550 | |
| 2551 | if (btrfs_file_extent_generation(path->nodes[0], extent) != |
| 2552 | backref->generation) |
| 2553 | goto out_free_path; |
| 2554 | |
| 2555 | btrfs_release_path(path); |
| 2556 | |
| 2557 | start = backref->file_pos; |
| 2558 | if (backref->extent_offset < old->extent_offset + old->offset) |
| 2559 | start += old->extent_offset + old->offset - |
| 2560 | backref->extent_offset; |
| 2561 | |
| 2562 | len = min(backref->extent_offset + backref->num_bytes, |
| 2563 | old->extent_offset + old->offset + old->len); |
| 2564 | len -= max(backref->extent_offset, old->extent_offset + old->offset); |
| 2565 | |
| 2566 | ret = btrfs_drop_extents(trans, root, inode, start, |
| 2567 | start + len, 1); |
| 2568 | if (ret) |
| 2569 | goto out_free_path; |
| 2570 | again: |
| 2571 | key.objectid = btrfs_ino(inode); |
| 2572 | key.type = BTRFS_EXTENT_DATA_KEY; |
| 2573 | key.offset = start; |
| 2574 | |
Liu Bo | a09a0a7 | 2013-03-11 09:20:58 +0000 | [diff] [blame] | 2575 | path->leave_spinning = 1; |
Liu Bo | 38c227d | 2013-01-29 03:18:40 +0000 | [diff] [blame] | 2576 | if (merge) { |
| 2577 | struct btrfs_file_extent_item *fi; |
| 2578 | u64 extent_len; |
| 2579 | struct btrfs_key found_key; |
| 2580 | |
Gui Hecheng | 3c9665d | 2014-01-23 13:41:09 +0800 | [diff] [blame] | 2581 | ret = btrfs_search_slot(trans, root, &key, path, 0, 1); |
Liu Bo | 38c227d | 2013-01-29 03:18:40 +0000 | [diff] [blame] | 2582 | if (ret < 0) |
| 2583 | goto out_free_path; |
| 2584 | |
| 2585 | path->slots[0]--; |
| 2586 | leaf = path->nodes[0]; |
| 2587 | btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]); |
| 2588 | |
| 2589 | fi = btrfs_item_ptr(leaf, path->slots[0], |
| 2590 | struct btrfs_file_extent_item); |
| 2591 | extent_len = btrfs_file_extent_num_bytes(leaf, fi); |
| 2592 | |
Liu Bo | 116e002 | 2013-08-02 16:30:40 +0800 | [diff] [blame] | 2593 | if (extent_len + found_key.offset == start && |
| 2594 | relink_is_mergable(leaf, fi, new)) { |
Liu Bo | 38c227d | 2013-01-29 03:18:40 +0000 | [diff] [blame] | 2595 | btrfs_set_file_extent_num_bytes(leaf, fi, |
| 2596 | extent_len + len); |
| 2597 | btrfs_mark_buffer_dirty(leaf); |
| 2598 | inode_add_bytes(inode, len); |
| 2599 | |
| 2600 | ret = 1; |
| 2601 | goto out_free_path; |
| 2602 | } else { |
| 2603 | merge = false; |
| 2604 | btrfs_release_path(path); |
| 2605 | goto again; |
| 2606 | } |
| 2607 | } |
| 2608 | |
| 2609 | ret = btrfs_insert_empty_item(trans, root, path, &key, |
| 2610 | sizeof(*extent)); |
| 2611 | if (ret) { |
Jeff Mahoney | 6664283 | 2016-06-10 18:19:25 -0400 | [diff] [blame] | 2612 | btrfs_abort_transaction(trans, ret); |
Liu Bo | 38c227d | 2013-01-29 03:18:40 +0000 | [diff] [blame] | 2613 | goto out_free_path; |
| 2614 | } |
| 2615 | |
| 2616 | leaf = path->nodes[0]; |
| 2617 | item = btrfs_item_ptr(leaf, path->slots[0], |
| 2618 | struct btrfs_file_extent_item); |
| 2619 | btrfs_set_file_extent_disk_bytenr(leaf, item, new->bytenr); |
| 2620 | btrfs_set_file_extent_disk_num_bytes(leaf, item, new->disk_len); |
| 2621 | btrfs_set_file_extent_offset(leaf, item, start - new->file_pos); |
| 2622 | btrfs_set_file_extent_num_bytes(leaf, item, len); |
| 2623 | btrfs_set_file_extent_ram_bytes(leaf, item, new->len); |
| 2624 | btrfs_set_file_extent_generation(leaf, item, trans->transid); |
| 2625 | btrfs_set_file_extent_type(leaf, item, BTRFS_FILE_EXTENT_REG); |
| 2626 | btrfs_set_file_extent_compression(leaf, item, new->compress_type); |
| 2627 | btrfs_set_file_extent_encryption(leaf, item, 0); |
| 2628 | btrfs_set_file_extent_other_encoding(leaf, item, 0); |
| 2629 | |
| 2630 | btrfs_mark_buffer_dirty(leaf); |
| 2631 | inode_add_bytes(inode, len); |
Liu Bo | a09a0a7 | 2013-03-11 09:20:58 +0000 | [diff] [blame] | 2632 | btrfs_release_path(path); |
Liu Bo | 38c227d | 2013-01-29 03:18:40 +0000 | [diff] [blame] | 2633 | |
| 2634 | ret = btrfs_inc_extent_ref(trans, root, new->bytenr, |
| 2635 | new->disk_len, 0, |
| 2636 | backref->root_id, backref->inum, |
Filipe Manana | b06c4bf | 2015-10-23 07:52:54 +0100 | [diff] [blame] | 2637 | new->file_pos); /* start - extent_offset */ |
Liu Bo | 38c227d | 2013-01-29 03:18:40 +0000 | [diff] [blame] | 2638 | if (ret) { |
Jeff Mahoney | 6664283 | 2016-06-10 18:19:25 -0400 | [diff] [blame] | 2639 | btrfs_abort_transaction(trans, ret); |
Liu Bo | 38c227d | 2013-01-29 03:18:40 +0000 | [diff] [blame] | 2640 | goto out_free_path; |
| 2641 | } |
| 2642 | |
| 2643 | ret = 1; |
| 2644 | out_free_path: |
| 2645 | btrfs_release_path(path); |
Liu Bo | a09a0a7 | 2013-03-11 09:20:58 +0000 | [diff] [blame] | 2646 | path->leave_spinning = 0; |
Liu Bo | 38c227d | 2013-01-29 03:18:40 +0000 | [diff] [blame] | 2647 | btrfs_end_transaction(trans, root); |
| 2648 | out_unlock: |
| 2649 | unlock_extent_cached(&BTRFS_I(inode)->io_tree, lock_start, lock_end, |
| 2650 | &cached, GFP_NOFS); |
| 2651 | iput(inode); |
| 2652 | return ret; |
| 2653 | } |
| 2654 | |
Liu Bo | 6f51956 | 2013-10-29 10:45:05 +0800 | [diff] [blame] | 2655 | static void free_sa_defrag_extent(struct new_sa_defrag_extent *new) |
| 2656 | { |
| 2657 | struct old_sa_defrag_extent *old, *tmp; |
| 2658 | |
| 2659 | if (!new) |
| 2660 | return; |
| 2661 | |
| 2662 | list_for_each_entry_safe(old, tmp, &new->head, list) { |
Liu Bo | 6f51956 | 2013-10-29 10:45:05 +0800 | [diff] [blame] | 2663 | kfree(old); |
| 2664 | } |
| 2665 | kfree(new); |
| 2666 | } |
| 2667 | |
Liu Bo | 38c227d | 2013-01-29 03:18:40 +0000 | [diff] [blame] | 2668 | static void relink_file_extents(struct new_sa_defrag_extent *new) |
| 2669 | { |
| 2670 | struct btrfs_path *path; |
Liu Bo | 38c227d | 2013-01-29 03:18:40 +0000 | [diff] [blame] | 2671 | struct sa_defrag_extent_backref *backref; |
| 2672 | struct sa_defrag_extent_backref *prev = NULL; |
| 2673 | struct inode *inode; |
| 2674 | struct btrfs_root *root; |
| 2675 | struct rb_node *node; |
| 2676 | int ret; |
| 2677 | |
| 2678 | inode = new->inode; |
| 2679 | root = BTRFS_I(inode)->root; |
| 2680 | |
| 2681 | path = btrfs_alloc_path(); |
| 2682 | if (!path) |
| 2683 | return; |
| 2684 | |
| 2685 | if (!record_extent_backrefs(path, new)) { |
| 2686 | btrfs_free_path(path); |
| 2687 | goto out; |
| 2688 | } |
| 2689 | btrfs_release_path(path); |
| 2690 | |
| 2691 | while (1) { |
| 2692 | node = rb_first(&new->root); |
| 2693 | if (!node) |
| 2694 | break; |
| 2695 | rb_erase(node, &new->root); |
| 2696 | |
| 2697 | backref = rb_entry(node, struct sa_defrag_extent_backref, node); |
| 2698 | |
| 2699 | ret = relink_extent_backref(path, prev, backref); |
| 2700 | WARN_ON(ret < 0); |
| 2701 | |
| 2702 | kfree(prev); |
| 2703 | |
| 2704 | if (ret == 1) |
| 2705 | prev = backref; |
| 2706 | else |
| 2707 | prev = NULL; |
| 2708 | cond_resched(); |
| 2709 | } |
| 2710 | kfree(prev); |
| 2711 | |
| 2712 | btrfs_free_path(path); |
Liu Bo | 38c227d | 2013-01-29 03:18:40 +0000 | [diff] [blame] | 2713 | out: |
Liu Bo | 6f51956 | 2013-10-29 10:45:05 +0800 | [diff] [blame] | 2714 | free_sa_defrag_extent(new); |
| 2715 | |
Liu Bo | 38c227d | 2013-01-29 03:18:40 +0000 | [diff] [blame] | 2716 | atomic_dec(&root->fs_info->defrag_running); |
| 2717 | wake_up(&root->fs_info->transaction_wait); |
Liu Bo | 38c227d | 2013-01-29 03:18:40 +0000 | [diff] [blame] | 2718 | } |
| 2719 | |
| 2720 | static struct new_sa_defrag_extent * |
| 2721 | record_old_file_extents(struct inode *inode, |
| 2722 | struct btrfs_ordered_extent *ordered) |
| 2723 | { |
| 2724 | struct btrfs_root *root = BTRFS_I(inode)->root; |
| 2725 | struct btrfs_path *path; |
| 2726 | struct btrfs_key key; |
Liu Bo | 6f51956 | 2013-10-29 10:45:05 +0800 | [diff] [blame] | 2727 | struct old_sa_defrag_extent *old; |
Liu Bo | 38c227d | 2013-01-29 03:18:40 +0000 | [diff] [blame] | 2728 | struct new_sa_defrag_extent *new; |
| 2729 | int ret; |
| 2730 | |
| 2731 | new = kmalloc(sizeof(*new), GFP_NOFS); |
| 2732 | if (!new) |
| 2733 | return NULL; |
| 2734 | |
| 2735 | new->inode = inode; |
| 2736 | new->file_pos = ordered->file_offset; |
| 2737 | new->len = ordered->len; |
| 2738 | new->bytenr = ordered->start; |
| 2739 | new->disk_len = ordered->disk_len; |
| 2740 | new->compress_type = ordered->compress_type; |
| 2741 | new->root = RB_ROOT; |
| 2742 | INIT_LIST_HEAD(&new->head); |
| 2743 | |
| 2744 | path = btrfs_alloc_path(); |
| 2745 | if (!path) |
| 2746 | goto out_kfree; |
| 2747 | |
| 2748 | key.objectid = btrfs_ino(inode); |
| 2749 | key.type = BTRFS_EXTENT_DATA_KEY; |
| 2750 | key.offset = new->file_pos; |
| 2751 | |
| 2752 | ret = btrfs_search_slot(NULL, root, &key, path, 0, 0); |
| 2753 | if (ret < 0) |
| 2754 | goto out_free_path; |
| 2755 | if (ret > 0 && path->slots[0] > 0) |
| 2756 | path->slots[0]--; |
| 2757 | |
| 2758 | /* find out all the old extents for the file range */ |
| 2759 | while (1) { |
| 2760 | struct btrfs_file_extent_item *extent; |
| 2761 | struct extent_buffer *l; |
| 2762 | int slot; |
| 2763 | u64 num_bytes; |
| 2764 | u64 offset; |
| 2765 | u64 end; |
| 2766 | u64 disk_bytenr; |
| 2767 | u64 extent_offset; |
| 2768 | |
| 2769 | l = path->nodes[0]; |
| 2770 | slot = path->slots[0]; |
| 2771 | |
| 2772 | if (slot >= btrfs_header_nritems(l)) { |
| 2773 | ret = btrfs_next_leaf(root, path); |
| 2774 | if (ret < 0) |
Liu Bo | 6f51956 | 2013-10-29 10:45:05 +0800 | [diff] [blame] | 2775 | goto out_free_path; |
Liu Bo | 38c227d | 2013-01-29 03:18:40 +0000 | [diff] [blame] | 2776 | else if (ret > 0) |
| 2777 | break; |
| 2778 | continue; |
| 2779 | } |
| 2780 | |
| 2781 | btrfs_item_key_to_cpu(l, &key, slot); |
| 2782 | |
| 2783 | if (key.objectid != btrfs_ino(inode)) |
| 2784 | break; |
| 2785 | if (key.type != BTRFS_EXTENT_DATA_KEY) |
| 2786 | break; |
| 2787 | if (key.offset >= new->file_pos + new->len) |
| 2788 | break; |
| 2789 | |
| 2790 | extent = btrfs_item_ptr(l, slot, struct btrfs_file_extent_item); |
| 2791 | |
| 2792 | num_bytes = btrfs_file_extent_num_bytes(l, extent); |
| 2793 | if (key.offset + num_bytes < new->file_pos) |
| 2794 | goto next; |
| 2795 | |
| 2796 | disk_bytenr = btrfs_file_extent_disk_bytenr(l, extent); |
| 2797 | if (!disk_bytenr) |
| 2798 | goto next; |
| 2799 | |
| 2800 | extent_offset = btrfs_file_extent_offset(l, extent); |
| 2801 | |
| 2802 | old = kmalloc(sizeof(*old), GFP_NOFS); |
| 2803 | if (!old) |
Liu Bo | 6f51956 | 2013-10-29 10:45:05 +0800 | [diff] [blame] | 2804 | goto out_free_path; |
Liu Bo | 38c227d | 2013-01-29 03:18:40 +0000 | [diff] [blame] | 2805 | |
| 2806 | offset = max(new->file_pos, key.offset); |
| 2807 | end = min(new->file_pos + new->len, key.offset + num_bytes); |
| 2808 | |
| 2809 | old->bytenr = disk_bytenr; |
| 2810 | old->extent_offset = extent_offset; |
| 2811 | old->offset = offset - key.offset; |
| 2812 | old->len = end - offset; |
| 2813 | old->new = new; |
| 2814 | old->count = 0; |
| 2815 | list_add_tail(&old->list, &new->head); |
| 2816 | next: |
| 2817 | path->slots[0]++; |
| 2818 | cond_resched(); |
| 2819 | } |
| 2820 | |
| 2821 | btrfs_free_path(path); |
| 2822 | atomic_inc(&root->fs_info->defrag_running); |
| 2823 | |
| 2824 | return new; |
| 2825 | |
Liu Bo | 38c227d | 2013-01-29 03:18:40 +0000 | [diff] [blame] | 2826 | out_free_path: |
| 2827 | btrfs_free_path(path); |
| 2828 | out_kfree: |
Liu Bo | 6f51956 | 2013-10-29 10:45:05 +0800 | [diff] [blame] | 2829 | free_sa_defrag_extent(new); |
Liu Bo | 38c227d | 2013-01-29 03:18:40 +0000 | [diff] [blame] | 2830 | return NULL; |
| 2831 | } |
| 2832 | |
Miao Xie | e570fd2 | 2014-06-19 10:42:50 +0800 | [diff] [blame] | 2833 | static void btrfs_release_delalloc_bytes(struct btrfs_root *root, |
| 2834 | u64 start, u64 len) |
| 2835 | { |
| 2836 | struct btrfs_block_group_cache *cache; |
| 2837 | |
| 2838 | cache = btrfs_lookup_block_group(root->fs_info, start); |
| 2839 | ASSERT(cache); |
| 2840 | |
| 2841 | spin_lock(&cache->lock); |
| 2842 | cache->delalloc_bytes -= len; |
| 2843 | spin_unlock(&cache->lock); |
| 2844 | |
| 2845 | btrfs_put_block_group(cache); |
| 2846 | } |
| 2847 | |
Chris Mason | d352ac6 | 2008-09-29 15:18:18 -0400 | [diff] [blame] | 2848 | /* as ordered data IO finishes, this gets called so we can finish |
| 2849 | * an ordered extent if the range of bytes in the file it covers are |
| 2850 | * fully written. |
| 2851 | */ |
Josef Bacik | 5fd0204 | 2012-05-02 14:00:54 -0400 | [diff] [blame] | 2852 | static int btrfs_finish_ordered_io(struct btrfs_ordered_extent *ordered_extent) |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 2853 | { |
Josef Bacik | 5fd0204 | 2012-05-02 14:00:54 -0400 | [diff] [blame] | 2854 | struct inode *inode = ordered_extent->inode; |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 2855 | struct btrfs_root *root = BTRFS_I(inode)->root; |
Yan, Zheng | 0ca1f7c | 2010-05-16 10:48:47 -0400 | [diff] [blame] | 2856 | struct btrfs_trans_handle *trans = NULL; |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 2857 | struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree; |
Josef Bacik | 2ac55d4 | 2010-02-03 19:33:23 +0000 | [diff] [blame] | 2858 | struct extent_state *cached_state = NULL; |
Liu Bo | 38c227d | 2013-01-29 03:18:40 +0000 | [diff] [blame] | 2859 | struct new_sa_defrag_extent *new = NULL; |
Li Zefan | 261507a0 | 2010-12-17 14:21:50 +0800 | [diff] [blame] | 2860 | int compress_type = 0; |
Josef Bacik | 77cef2e | 2013-08-29 13:57:21 -0400 | [diff] [blame] | 2861 | int ret = 0; |
| 2862 | u64 logical_len = ordered_extent->len; |
Li Zefan | 82d5902 | 2011-04-20 10:33:24 +0800 | [diff] [blame] | 2863 | bool nolock; |
Josef Bacik | 77cef2e | 2013-08-29 13:57:21 -0400 | [diff] [blame] | 2864 | bool truncated = false; |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 2865 | |
Liu Bo | 83eea1f | 2012-07-10 05:28:39 -0600 | [diff] [blame] | 2866 | nolock = btrfs_is_free_space_inode(inode); |
Josef Bacik | 0cb59c9 | 2010-07-02 12:14:14 -0400 | [diff] [blame] | 2867 | |
Josef Bacik | 5fd0204 | 2012-05-02 14:00:54 -0400 | [diff] [blame] | 2868 | if (test_bit(BTRFS_ORDERED_IOERR, &ordered_extent->flags)) { |
| 2869 | ret = -EIO; |
| 2870 | goto out; |
| 2871 | } |
| 2872 | |
Miao Xie | f612496 | 2014-09-12 18:44:04 +0800 | [diff] [blame] | 2873 | btrfs_free_io_failure_record(inode, ordered_extent->file_offset, |
| 2874 | ordered_extent->file_offset + |
| 2875 | ordered_extent->len - 1); |
| 2876 | |
Josef Bacik | 77cef2e | 2013-08-29 13:57:21 -0400 | [diff] [blame] | 2877 | if (test_bit(BTRFS_ORDERED_TRUNCATED, &ordered_extent->flags)) { |
| 2878 | truncated = true; |
| 2879 | logical_len = ordered_extent->truncated_len; |
| 2880 | /* Truncated the entire extent, don't bother adding */ |
| 2881 | if (!logical_len) |
| 2882 | goto out; |
| 2883 | } |
| 2884 | |
Yan, Zheng | c216775 | 2009-11-12 09:34:21 +0000 | [diff] [blame] | 2885 | if (test_bit(BTRFS_ORDERED_NOCOW, &ordered_extent->flags)) { |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 2886 | BUG_ON(!list_empty(&ordered_extent->list)); /* Logic error */ |
Qu Wenruo | 94ed938 | 2015-09-08 17:25:56 +0800 | [diff] [blame] | 2887 | |
| 2888 | /* |
| 2889 | * For mwrite(mmap + memset to write) case, we still reserve |
| 2890 | * space for NOCOW range. |
| 2891 | * As NOCOW won't cause a new delayed ref, just free the space |
| 2892 | */ |
| 2893 | btrfs_qgroup_free_data(inode, ordered_extent->file_offset, |
| 2894 | ordered_extent->len); |
Josef Bacik | 6c760c0 | 2012-11-09 10:53:21 -0500 | [diff] [blame] | 2895 | btrfs_ordered_update_i_size(inode, 0, ordered_extent); |
| 2896 | if (nolock) |
| 2897 | trans = btrfs_join_transaction_nolock(root); |
| 2898 | else |
| 2899 | trans = btrfs_join_transaction(root); |
| 2900 | if (IS_ERR(trans)) { |
| 2901 | ret = PTR_ERR(trans); |
| 2902 | trans = NULL; |
| 2903 | goto out; |
Yan, Zheng | c216775 | 2009-11-12 09:34:21 +0000 | [diff] [blame] | 2904 | } |
Josef Bacik | 6c760c0 | 2012-11-09 10:53:21 -0500 | [diff] [blame] | 2905 | trans->block_rsv = &root->fs_info->delalloc_block_rsv; |
| 2906 | ret = btrfs_update_inode_fallback(trans, root, inode); |
| 2907 | if (ret) /* -ENOMEM or corruption */ |
Jeff Mahoney | 6664283 | 2016-06-10 18:19:25 -0400 | [diff] [blame] | 2908 | btrfs_abort_transaction(trans, ret); |
Yan, Zheng | c216775 | 2009-11-12 09:34:21 +0000 | [diff] [blame] | 2909 | goto out; |
| 2910 | } |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 2911 | |
Josef Bacik | 2ac55d4 | 2010-02-03 19:33:23 +0000 | [diff] [blame] | 2912 | lock_extent_bits(io_tree, ordered_extent->file_offset, |
| 2913 | ordered_extent->file_offset + ordered_extent->len - 1, |
David Sterba | ff13db4 | 2015-12-03 14:30:40 +0100 | [diff] [blame] | 2914 | &cached_state); |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 2915 | |
Liu Bo | 38c227d | 2013-01-29 03:18:40 +0000 | [diff] [blame] | 2916 | ret = test_range_bit(io_tree, ordered_extent->file_offset, |
| 2917 | ordered_extent->file_offset + ordered_extent->len - 1, |
| 2918 | EXTENT_DEFRAG, 1, cached_state); |
| 2919 | if (ret) { |
| 2920 | u64 last_snapshot = btrfs_root_last_snapshot(&root->root_item); |
Josef Bacik | 8101c8d | 2014-01-29 16:05:30 -0500 | [diff] [blame] | 2921 | if (0 && last_snapshot >= BTRFS_I(inode)->generation) |
Liu Bo | 38c227d | 2013-01-29 03:18:40 +0000 | [diff] [blame] | 2922 | /* the inode is shared */ |
| 2923 | new = record_old_file_extents(inode, ordered_extent); |
| 2924 | |
| 2925 | clear_extent_bit(io_tree, ordered_extent->file_offset, |
| 2926 | ordered_extent->file_offset + ordered_extent->len - 1, |
| 2927 | EXTENT_DEFRAG, 0, 0, &cached_state, GFP_NOFS); |
| 2928 | } |
| 2929 | |
Josef Bacik | 0cb59c9 | 2010-07-02 12:14:14 -0400 | [diff] [blame] | 2930 | if (nolock) |
Josef Bacik | 7a7eaa4 | 2011-04-13 12:54:33 -0400 | [diff] [blame] | 2931 | trans = btrfs_join_transaction_nolock(root); |
Josef Bacik | 0cb59c9 | 2010-07-02 12:14:14 -0400 | [diff] [blame] | 2932 | else |
Josef Bacik | 7a7eaa4 | 2011-04-13 12:54:33 -0400 | [diff] [blame] | 2933 | trans = btrfs_join_transaction(root); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 2934 | if (IS_ERR(trans)) { |
| 2935 | ret = PTR_ERR(trans); |
| 2936 | trans = NULL; |
| 2937 | goto out_unlock; |
| 2938 | } |
Chris Mason | a79b7d4 | 2014-05-22 16:18:52 -0700 | [diff] [blame] | 2939 | |
Yan, Zheng | 0ca1f7c | 2010-05-16 10:48:47 -0400 | [diff] [blame] | 2940 | trans->block_rsv = &root->fs_info->delalloc_block_rsv; |
Yan, Zheng | c216775 | 2009-11-12 09:34:21 +0000 | [diff] [blame] | 2941 | |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 2942 | if (test_bit(BTRFS_ORDERED_COMPRESSED, &ordered_extent->flags)) |
Li Zefan | 261507a0 | 2010-12-17 14:21:50 +0800 | [diff] [blame] | 2943 | compress_type = ordered_extent->compress_type; |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 2944 | if (test_bit(BTRFS_ORDERED_PREALLOC, &ordered_extent->flags)) { |
Li Zefan | 261507a0 | 2010-12-17 14:21:50 +0800 | [diff] [blame] | 2945 | BUG_ON(compress_type); |
Yan, Zheng | 920bbbf | 2009-11-12 09:34:08 +0000 | [diff] [blame] | 2946 | ret = btrfs_mark_extent_written(trans, inode, |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 2947 | ordered_extent->file_offset, |
| 2948 | ordered_extent->file_offset + |
Josef Bacik | 77cef2e | 2013-08-29 13:57:21 -0400 | [diff] [blame] | 2949 | logical_len); |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 2950 | } else { |
Josef Bacik | 0af3d00 | 2010-06-21 14:48:16 -0400 | [diff] [blame] | 2951 | BUG_ON(root == root->fs_info->tree_root); |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 2952 | ret = insert_reserved_file_extent(trans, inode, |
| 2953 | ordered_extent->file_offset, |
| 2954 | ordered_extent->start, |
| 2955 | ordered_extent->disk_len, |
Josef Bacik | 77cef2e | 2013-08-29 13:57:21 -0400 | [diff] [blame] | 2956 | logical_len, logical_len, |
Li Zefan | 261507a0 | 2010-12-17 14:21:50 +0800 | [diff] [blame] | 2957 | compress_type, 0, 0, |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 2958 | BTRFS_FILE_EXTENT_REG); |
Miao Xie | e570fd2 | 2014-06-19 10:42:50 +0800 | [diff] [blame] | 2959 | if (!ret) |
| 2960 | btrfs_release_delalloc_bytes(root, |
| 2961 | ordered_extent->start, |
| 2962 | ordered_extent->disk_len); |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 2963 | } |
Josef Bacik | 5dc562c | 2012-08-17 13:14:17 -0400 | [diff] [blame] | 2964 | unpin_extent_cache(&BTRFS_I(inode)->extent_tree, |
| 2965 | ordered_extent->file_offset, ordered_extent->len, |
| 2966 | trans->transid); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 2967 | if (ret < 0) { |
Jeff Mahoney | 6664283 | 2016-06-10 18:19:25 -0400 | [diff] [blame] | 2968 | btrfs_abort_transaction(trans, ret); |
Josef Bacik | 5fd0204 | 2012-05-02 14:00:54 -0400 | [diff] [blame] | 2969 | goto out_unlock; |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 2970 | } |
Josef Bacik | 2ac55d4 | 2010-02-03 19:33:23 +0000 | [diff] [blame] | 2971 | |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 2972 | add_pending_csums(trans, inode, ordered_extent->file_offset, |
| 2973 | &ordered_extent->list); |
| 2974 | |
Josef Bacik | 6c760c0 | 2012-11-09 10:53:21 -0500 | [diff] [blame] | 2975 | btrfs_ordered_update_i_size(inode, 0, ordered_extent); |
| 2976 | ret = btrfs_update_inode_fallback(trans, root, inode); |
| 2977 | if (ret) { /* -ENOMEM or corruption */ |
Jeff Mahoney | 6664283 | 2016-06-10 18:19:25 -0400 | [diff] [blame] | 2978 | btrfs_abort_transaction(trans, ret); |
Josef Bacik | 6c760c0 | 2012-11-09 10:53:21 -0500 | [diff] [blame] | 2979 | goto out_unlock; |
Josef Bacik | 1ef30be | 2011-04-05 19:25:36 -0400 | [diff] [blame] | 2980 | } |
| 2981 | ret = 0; |
Josef Bacik | 5fd0204 | 2012-05-02 14:00:54 -0400 | [diff] [blame] | 2982 | out_unlock: |
| 2983 | unlock_extent_cached(io_tree, ordered_extent->file_offset, |
| 2984 | ordered_extent->file_offset + |
| 2985 | ordered_extent->len - 1, &cached_state, GFP_NOFS); |
Yan, Zheng | c216775 | 2009-11-12 09:34:21 +0000 | [diff] [blame] | 2986 | out: |
Josef Bacik | 5b0e95b | 2011-10-06 08:58:24 -0400 | [diff] [blame] | 2987 | if (root != root->fs_info->tree_root) |
Josef Bacik | 0cb59c9 | 2010-07-02 12:14:14 -0400 | [diff] [blame] | 2988 | btrfs_delalloc_release_metadata(inode, ordered_extent->len); |
Miao Xie | a698d075 | 2012-09-20 01:51:59 -0600 | [diff] [blame] | 2989 | if (trans) |
| 2990 | btrfs_end_transaction(trans, root); |
Josef Bacik | 0cb59c9 | 2010-07-02 12:14:14 -0400 | [diff] [blame] | 2991 | |
Josef Bacik | 77cef2e | 2013-08-29 13:57:21 -0400 | [diff] [blame] | 2992 | if (ret || truncated) { |
| 2993 | u64 start, end; |
| 2994 | |
| 2995 | if (truncated) |
| 2996 | start = ordered_extent->file_offset + logical_len; |
| 2997 | else |
| 2998 | start = ordered_extent->file_offset; |
| 2999 | end = ordered_extent->file_offset + ordered_extent->len - 1; |
| 3000 | clear_extent_uptodate(io_tree, start, end, NULL, GFP_NOFS); |
| 3001 | |
| 3002 | /* Drop the cache for the part of the extent we didn't write. */ |
| 3003 | btrfs_drop_extent_cache(inode, start, end, 0); |
Josef Bacik | 5fd0204 | 2012-05-02 14:00:54 -0400 | [diff] [blame] | 3004 | |
Josef Bacik | 0bec9ef | 2013-01-31 14:58:00 -0500 | [diff] [blame] | 3005 | /* |
| 3006 | * If the ordered extent had an IOERR or something else went |
| 3007 | * wrong we need to return the space for this ordered extent |
Josef Bacik | 77cef2e | 2013-08-29 13:57:21 -0400 | [diff] [blame] | 3008 | * back to the allocator. We only free the extent in the |
| 3009 | * truncated case if we didn't write out the extent at all. |
Josef Bacik | 0bec9ef | 2013-01-31 14:58:00 -0500 | [diff] [blame] | 3010 | */ |
Josef Bacik | 77cef2e | 2013-08-29 13:57:21 -0400 | [diff] [blame] | 3011 | if ((ret || !logical_len) && |
| 3012 | !test_bit(BTRFS_ORDERED_NOCOW, &ordered_extent->flags) && |
Josef Bacik | 0bec9ef | 2013-01-31 14:58:00 -0500 | [diff] [blame] | 3013 | !test_bit(BTRFS_ORDERED_PREALLOC, &ordered_extent->flags)) |
| 3014 | btrfs_free_reserved_extent(root, ordered_extent->start, |
Miao Xie | e570fd2 | 2014-06-19 10:42:50 +0800 | [diff] [blame] | 3015 | ordered_extent->disk_len, 1); |
Josef Bacik | 0bec9ef | 2013-01-31 14:58:00 -0500 | [diff] [blame] | 3016 | } |
| 3017 | |
| 3018 | |
Josef Bacik | 5fd0204 | 2012-05-02 14:00:54 -0400 | [diff] [blame] | 3019 | /* |
Liu Bo | 8bad3c0 | 2012-06-18 12:14:23 +0800 | [diff] [blame] | 3020 | * This needs to be done to make sure anybody waiting knows we are done |
| 3021 | * updating everything for this ordered extent. |
Josef Bacik | 5fd0204 | 2012-05-02 14:00:54 -0400 | [diff] [blame] | 3022 | */ |
| 3023 | btrfs_remove_ordered_extent(inode, ordered_extent); |
| 3024 | |
Liu Bo | 38c227d | 2013-01-29 03:18:40 +0000 | [diff] [blame] | 3025 | /* for snapshot-aware defrag */ |
Liu Bo | 6f51956 | 2013-10-29 10:45:05 +0800 | [diff] [blame] | 3026 | if (new) { |
| 3027 | if (ret) { |
| 3028 | free_sa_defrag_extent(new); |
| 3029 | atomic_dec(&root->fs_info->defrag_running); |
| 3030 | } else { |
| 3031 | relink_file_extents(new); |
| 3032 | } |
| 3033 | } |
Liu Bo | 38c227d | 2013-01-29 03:18:40 +0000 | [diff] [blame] | 3034 | |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 3035 | /* once for us */ |
| 3036 | btrfs_put_ordered_extent(ordered_extent); |
| 3037 | /* once for the tree */ |
| 3038 | btrfs_put_ordered_extent(ordered_extent); |
| 3039 | |
Josef Bacik | 5fd0204 | 2012-05-02 14:00:54 -0400 | [diff] [blame] | 3040 | return ret; |
| 3041 | } |
| 3042 | |
| 3043 | static void finish_ordered_fn(struct btrfs_work *work) |
| 3044 | { |
| 3045 | struct btrfs_ordered_extent *ordered_extent; |
| 3046 | ordered_extent = container_of(work, struct btrfs_ordered_extent, work); |
| 3047 | btrfs_finish_ordered_io(ordered_extent); |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 3048 | } |
| 3049 | |
Christoph Hellwig | b295086 | 2008-12-02 09:54:17 -0500 | [diff] [blame] | 3050 | 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] | 3051 | struct extent_state *state, int uptodate) |
| 3052 | { |
Josef Bacik | 5fd0204 | 2012-05-02 14:00:54 -0400 | [diff] [blame] | 3053 | struct inode *inode = page->mapping->host; |
| 3054 | struct btrfs_root *root = BTRFS_I(inode)->root; |
| 3055 | struct btrfs_ordered_extent *ordered_extent = NULL; |
Liu Bo | 9e0af23 | 2014-08-15 23:36:53 +0800 | [diff] [blame] | 3056 | struct btrfs_workqueue *wq; |
| 3057 | btrfs_work_func_t func; |
Josef Bacik | 5fd0204 | 2012-05-02 14:00:54 -0400 | [diff] [blame] | 3058 | |
liubo | 1abe9b8 | 2011-03-24 11:18:59 +0000 | [diff] [blame] | 3059 | trace_btrfs_writepage_end_io_hook(page, start, end, uptodate); |
| 3060 | |
Chris Mason | 8b62b72 | 2009-09-02 16:53:46 -0400 | [diff] [blame] | 3061 | ClearPagePrivate2(page); |
Josef Bacik | 5fd0204 | 2012-05-02 14:00:54 -0400 | [diff] [blame] | 3062 | if (!btrfs_dec_test_ordered_pending(inode, &ordered_extent, start, |
| 3063 | end - start + 1, uptodate)) |
| 3064 | return 0; |
| 3065 | |
Liu Bo | 9e0af23 | 2014-08-15 23:36:53 +0800 | [diff] [blame] | 3066 | if (btrfs_is_free_space_inode(inode)) { |
| 3067 | wq = root->fs_info->endio_freespace_worker; |
| 3068 | func = btrfs_freespace_write_helper; |
| 3069 | } else { |
| 3070 | wq = root->fs_info->endio_write_workers; |
| 3071 | func = btrfs_endio_write_helper; |
| 3072 | } |
Josef Bacik | 5fd0204 | 2012-05-02 14:00:54 -0400 | [diff] [blame] | 3073 | |
Liu Bo | 9e0af23 | 2014-08-15 23:36:53 +0800 | [diff] [blame] | 3074 | btrfs_init_work(&ordered_extent->work, func, finish_ordered_fn, NULL, |
| 3075 | NULL); |
| 3076 | btrfs_queue_work(wq, &ordered_extent->work); |
Josef Bacik | 5fd0204 | 2012-05-02 14:00:54 -0400 | [diff] [blame] | 3077 | |
| 3078 | return 0; |
Chris Mason | 211f90e | 2008-07-18 11:56:15 -0400 | [diff] [blame] | 3079 | } |
| 3080 | |
Miao Xie | dc380ae | 2014-09-12 18:43:55 +0800 | [diff] [blame] | 3081 | static int __readpage_endio_check(struct inode *inode, |
| 3082 | struct btrfs_io_bio *io_bio, |
| 3083 | int icsum, struct page *page, |
| 3084 | int pgoff, u64 start, size_t len) |
| 3085 | { |
| 3086 | char *kaddr; |
| 3087 | u32 csum_expected; |
| 3088 | u32 csum = ~(u32)0; |
Miao Xie | dc380ae | 2014-09-12 18:43:55 +0800 | [diff] [blame] | 3089 | |
| 3090 | csum_expected = *(((u32 *)io_bio->csum) + icsum); |
| 3091 | |
| 3092 | kaddr = kmap_atomic(page); |
| 3093 | csum = btrfs_csum_data(kaddr + pgoff, csum, len); |
| 3094 | btrfs_csum_final(csum, (char *)&csum); |
| 3095 | if (csum != csum_expected) |
| 3096 | goto zeroit; |
| 3097 | |
| 3098 | kunmap_atomic(kaddr); |
| 3099 | return 0; |
| 3100 | zeroit: |
David Sterba | 9464732 | 2015-10-08 11:01:36 +0200 | [diff] [blame] | 3101 | btrfs_warn_rl(BTRFS_I(inode)->root->fs_info, |
| 3102 | "csum failed ino %llu off %llu csum %u expected csum %u", |
Miao Xie | dc380ae | 2014-09-12 18:43:55 +0800 | [diff] [blame] | 3103 | btrfs_ino(inode), start, csum, csum_expected); |
| 3104 | memset(kaddr + pgoff, 1, len); |
| 3105 | flush_dcache_page(page); |
| 3106 | kunmap_atomic(kaddr); |
| 3107 | if (csum_expected == 0) |
| 3108 | return 0; |
| 3109 | return -EIO; |
| 3110 | } |
| 3111 | |
Chris Mason | d352ac6 | 2008-09-29 15:18:18 -0400 | [diff] [blame] | 3112 | /* |
Chris Mason | d352ac6 | 2008-09-29 15:18:18 -0400 | [diff] [blame] | 3113 | * 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] | 3114 | * if there's a match, we allow the bio to finish. If not, the code in |
| 3115 | * extent_io.c will try to find good copies for us. |
Chris Mason | d352ac6 | 2008-09-29 15:18:18 -0400 | [diff] [blame] | 3116 | */ |
Miao Xie | facc8a22 | 2013-07-25 19:22:34 +0800 | [diff] [blame] | 3117 | static int btrfs_readpage_end_io_hook(struct btrfs_io_bio *io_bio, |
| 3118 | u64 phy_offset, struct page *page, |
| 3119 | u64 start, u64 end, int mirror) |
Chris Mason | 07157aa | 2007-08-30 08:50:51 -0400 | [diff] [blame] | 3120 | { |
Miao Xie | 4eee4fa | 2012-12-21 09:17:45 +0000 | [diff] [blame] | 3121 | size_t offset = start - page_offset(page); |
Chris Mason | 07157aa | 2007-08-30 08:50:51 -0400 | [diff] [blame] | 3122 | struct inode *inode = page->mapping->host; |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 3123 | struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree; |
Chris Mason | ff79f81 | 2007-10-15 16:22:25 -0400 | [diff] [blame] | 3124 | struct btrfs_root *root = BTRFS_I(inode)->root; |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 3125 | |
Chris Mason | d20f704 | 2008-12-08 16:58:54 -0500 | [diff] [blame] | 3126 | if (PageChecked(page)) { |
| 3127 | ClearPageChecked(page); |
Miao Xie | dc380ae | 2014-09-12 18:43:55 +0800 | [diff] [blame] | 3128 | return 0; |
Chris Mason | d20f704 | 2008-12-08 16:58:54 -0500 | [diff] [blame] | 3129 | } |
Christoph Hellwig | 6cbff00 | 2009-04-17 10:37:41 +0200 | [diff] [blame] | 3130 | |
| 3131 | if (BTRFS_I(inode)->flags & BTRFS_INODE_NODATASUM) |
Miao Xie | dc380ae | 2014-09-12 18:43:55 +0800 | [diff] [blame] | 3132 | return 0; |
Chris Mason | d20f704 | 2008-12-08 16:58:54 -0500 | [diff] [blame] | 3133 | |
Yan Zheng | 17d217f | 2008-12-12 10:03:38 -0500 | [diff] [blame] | 3134 | if (root->root_key.objectid == BTRFS_DATA_RELOC_TREE_OBJECTID && |
Chris Mason | 9655d29 | 2009-09-02 15:22:30 -0400 | [diff] [blame] | 3135 | test_range_bit(io_tree, start, end, EXTENT_NODATASUM, 1, NULL)) { |
David Sterba | 9116621 | 2016-04-26 23:54:39 +0200 | [diff] [blame] | 3136 | clear_extent_bits(io_tree, start, end, EXTENT_NODATASUM); |
Yan Zheng | 17d217f | 2008-12-12 10:03:38 -0500 | [diff] [blame] | 3137 | return 0; |
| 3138 | } |
| 3139 | |
Miao Xie | facc8a22 | 2013-07-25 19:22:34 +0800 | [diff] [blame] | 3140 | phy_offset >>= inode->i_sb->s_blocksize_bits; |
Miao Xie | dc380ae | 2014-09-12 18:43:55 +0800 | [diff] [blame] | 3141 | return __readpage_endio_check(inode, io_bio, phy_offset, page, offset, |
| 3142 | start, (size_t)(end - start + 1)); |
Chris Mason | 07157aa | 2007-08-30 08:50:51 -0400 | [diff] [blame] | 3143 | } |
Chris Mason | b888db2 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 3144 | |
Yan, Zheng | 24bbcf0 | 2009-11-12 09:36:34 +0000 | [diff] [blame] | 3145 | void btrfs_add_delayed_iput(struct inode *inode) |
| 3146 | { |
| 3147 | struct btrfs_fs_info *fs_info = BTRFS_I(inode)->root->fs_info; |
David Sterba | 8089fe6 | 2015-11-19 14:15:51 +0100 | [diff] [blame] | 3148 | struct btrfs_inode *binode = BTRFS_I(inode); |
Yan, Zheng | 24bbcf0 | 2009-11-12 09:36:34 +0000 | [diff] [blame] | 3149 | |
| 3150 | if (atomic_add_unless(&inode->i_count, -1, 1)) |
| 3151 | return; |
| 3152 | |
Yan, Zheng | 24bbcf0 | 2009-11-12 09:36:34 +0000 | [diff] [blame] | 3153 | spin_lock(&fs_info->delayed_iput_lock); |
David Sterba | 8089fe6 | 2015-11-19 14:15:51 +0100 | [diff] [blame] | 3154 | if (binode->delayed_iput_count == 0) { |
| 3155 | ASSERT(list_empty(&binode->delayed_iput)); |
| 3156 | list_add_tail(&binode->delayed_iput, &fs_info->delayed_iputs); |
| 3157 | } else { |
| 3158 | binode->delayed_iput_count++; |
| 3159 | } |
Yan, Zheng | 24bbcf0 | 2009-11-12 09:36:34 +0000 | [diff] [blame] | 3160 | spin_unlock(&fs_info->delayed_iput_lock); |
| 3161 | } |
| 3162 | |
| 3163 | void btrfs_run_delayed_iputs(struct btrfs_root *root) |
| 3164 | { |
Yan, Zheng | 24bbcf0 | 2009-11-12 09:36:34 +0000 | [diff] [blame] | 3165 | struct btrfs_fs_info *fs_info = root->fs_info; |
Yan, Zheng | 24bbcf0 | 2009-11-12 09:36:34 +0000 | [diff] [blame] | 3166 | |
Yan, Zheng | 24bbcf0 | 2009-11-12 09:36:34 +0000 | [diff] [blame] | 3167 | spin_lock(&fs_info->delayed_iput_lock); |
David Sterba | 8089fe6 | 2015-11-19 14:15:51 +0100 | [diff] [blame] | 3168 | while (!list_empty(&fs_info->delayed_iputs)) { |
| 3169 | struct btrfs_inode *inode; |
Yan, Zheng | 24bbcf0 | 2009-11-12 09:36:34 +0000 | [diff] [blame] | 3170 | |
David Sterba | 8089fe6 | 2015-11-19 14:15:51 +0100 | [diff] [blame] | 3171 | inode = list_first_entry(&fs_info->delayed_iputs, |
| 3172 | struct btrfs_inode, delayed_iput); |
| 3173 | if (inode->delayed_iput_count) { |
| 3174 | inode->delayed_iput_count--; |
| 3175 | list_move_tail(&inode->delayed_iput, |
| 3176 | &fs_info->delayed_iputs); |
| 3177 | } else { |
| 3178 | list_del_init(&inode->delayed_iput); |
| 3179 | } |
| 3180 | spin_unlock(&fs_info->delayed_iput_lock); |
| 3181 | iput(&inode->vfs_inode); |
| 3182 | spin_lock(&fs_info->delayed_iput_lock); |
Yan, Zheng | 24bbcf0 | 2009-11-12 09:36:34 +0000 | [diff] [blame] | 3183 | } |
David Sterba | 8089fe6 | 2015-11-19 14:15:51 +0100 | [diff] [blame] | 3184 | spin_unlock(&fs_info->delayed_iput_lock); |
Yan, Zheng | 24bbcf0 | 2009-11-12 09:36:34 +0000 | [diff] [blame] | 3185 | } |
| 3186 | |
Yan, Zheng | d68fc57 | 2010-05-16 10:49:58 -0400 | [diff] [blame] | 3187 | /* |
Justin P. Mattock | 42b2aa8 | 2011-11-28 20:31:00 -0800 | [diff] [blame] | 3188 | * This is called in transaction commit time. If there are no orphan |
Yan, Zheng | d68fc57 | 2010-05-16 10:49:58 -0400 | [diff] [blame] | 3189 | * files in the subvolume, it removes orphan item and frees block_rsv |
| 3190 | * structure. |
| 3191 | */ |
| 3192 | void btrfs_orphan_commit_root(struct btrfs_trans_handle *trans, |
| 3193 | struct btrfs_root *root) |
| 3194 | { |
Josef Bacik | 90290e1 | 2011-12-02 15:44:12 -0500 | [diff] [blame] | 3195 | struct btrfs_block_rsv *block_rsv; |
Yan, Zheng | d68fc57 | 2010-05-16 10:49:58 -0400 | [diff] [blame] | 3196 | int ret; |
| 3197 | |
Josef Bacik | 8a35d95 | 2012-05-23 14:26:42 -0400 | [diff] [blame] | 3198 | if (atomic_read(&root->orphan_inodes) || |
Yan, Zheng | d68fc57 | 2010-05-16 10:49:58 -0400 | [diff] [blame] | 3199 | root->orphan_cleanup_state != ORPHAN_CLEANUP_DONE) |
| 3200 | return; |
| 3201 | |
Josef Bacik | 90290e1 | 2011-12-02 15:44:12 -0500 | [diff] [blame] | 3202 | spin_lock(&root->orphan_lock); |
Josef Bacik | 8a35d95 | 2012-05-23 14:26:42 -0400 | [diff] [blame] | 3203 | if (atomic_read(&root->orphan_inodes)) { |
Josef Bacik | 90290e1 | 2011-12-02 15:44:12 -0500 | [diff] [blame] | 3204 | spin_unlock(&root->orphan_lock); |
| 3205 | return; |
| 3206 | } |
| 3207 | |
| 3208 | if (root->orphan_cleanup_state != ORPHAN_CLEANUP_DONE) { |
| 3209 | spin_unlock(&root->orphan_lock); |
| 3210 | return; |
| 3211 | } |
| 3212 | |
| 3213 | block_rsv = root->orphan_block_rsv; |
| 3214 | root->orphan_block_rsv = NULL; |
| 3215 | spin_unlock(&root->orphan_lock); |
| 3216 | |
Miao Xie | 27cdeb7 | 2014-04-02 19:51:05 +0800 | [diff] [blame] | 3217 | if (test_bit(BTRFS_ROOT_ORPHAN_ITEM_INSERTED, &root->state) && |
Yan, Zheng | d68fc57 | 2010-05-16 10:49:58 -0400 | [diff] [blame] | 3218 | btrfs_root_refs(&root->root_item) > 0) { |
| 3219 | ret = btrfs_del_orphan_item(trans, root->fs_info->tree_root, |
| 3220 | root->root_key.objectid); |
Josef Bacik | 4ef31a4 | 2013-08-13 14:10:08 -0400 | [diff] [blame] | 3221 | if (ret) |
Jeff Mahoney | 6664283 | 2016-06-10 18:19:25 -0400 | [diff] [blame] | 3222 | btrfs_abort_transaction(trans, ret); |
Josef Bacik | 4ef31a4 | 2013-08-13 14:10:08 -0400 | [diff] [blame] | 3223 | else |
Miao Xie | 27cdeb7 | 2014-04-02 19:51:05 +0800 | [diff] [blame] | 3224 | clear_bit(BTRFS_ROOT_ORPHAN_ITEM_INSERTED, |
| 3225 | &root->state); |
Yan, Zheng | d68fc57 | 2010-05-16 10:49:58 -0400 | [diff] [blame] | 3226 | } |
| 3227 | |
Josef Bacik | 90290e1 | 2011-12-02 15:44:12 -0500 | [diff] [blame] | 3228 | if (block_rsv) { |
| 3229 | WARN_ON(block_rsv->size > 0); |
| 3230 | btrfs_free_block_rsv(root, block_rsv); |
Yan, Zheng | d68fc57 | 2010-05-16 10:49:58 -0400 | [diff] [blame] | 3231 | } |
| 3232 | } |
| 3233 | |
| 3234 | /* |
Josef Bacik | 7b12876 | 2008-07-24 12:17:14 -0400 | [diff] [blame] | 3235 | * This creates an orphan entry for the given inode in case something goes |
| 3236 | * wrong in the middle of an unlink/truncate. |
Yan, Zheng | d68fc57 | 2010-05-16 10:49:58 -0400 | [diff] [blame] | 3237 | * |
| 3238 | * NOTE: caller of this function should reserve 5 units of metadata for |
| 3239 | * this function. |
Josef Bacik | 7b12876 | 2008-07-24 12:17:14 -0400 | [diff] [blame] | 3240 | */ |
| 3241 | int btrfs_orphan_add(struct btrfs_trans_handle *trans, struct inode *inode) |
| 3242 | { |
| 3243 | struct btrfs_root *root = BTRFS_I(inode)->root; |
Yan, Zheng | d68fc57 | 2010-05-16 10:49:58 -0400 | [diff] [blame] | 3244 | struct btrfs_block_rsv *block_rsv = NULL; |
| 3245 | int reserve = 0; |
| 3246 | int insert = 0; |
| 3247 | int ret; |
Josef Bacik | 7b12876 | 2008-07-24 12:17:14 -0400 | [diff] [blame] | 3248 | |
Yan, Zheng | d68fc57 | 2010-05-16 10:49:58 -0400 | [diff] [blame] | 3249 | if (!root->orphan_block_rsv) { |
Miao Xie | 66d8f3d | 2012-09-06 04:02:28 -0600 | [diff] [blame] | 3250 | block_rsv = btrfs_alloc_block_rsv(root, BTRFS_BLOCK_RSV_TEMP); |
Tsutomu Itoh | b532402 | 2011-07-19 07:27:20 +0000 | [diff] [blame] | 3251 | if (!block_rsv) |
| 3252 | return -ENOMEM; |
Josef Bacik | 7b12876 | 2008-07-24 12:17:14 -0400 | [diff] [blame] | 3253 | } |
| 3254 | |
Yan, Zheng | d68fc57 | 2010-05-16 10:49:58 -0400 | [diff] [blame] | 3255 | spin_lock(&root->orphan_lock); |
| 3256 | if (!root->orphan_block_rsv) { |
| 3257 | root->orphan_block_rsv = block_rsv; |
| 3258 | } else if (block_rsv) { |
| 3259 | btrfs_free_block_rsv(root, block_rsv); |
| 3260 | block_rsv = NULL; |
| 3261 | } |
Josef Bacik | 7b12876 | 2008-07-24 12:17:14 -0400 | [diff] [blame] | 3262 | |
Josef Bacik | 8a35d95 | 2012-05-23 14:26:42 -0400 | [diff] [blame] | 3263 | if (!test_and_set_bit(BTRFS_INODE_HAS_ORPHAN_ITEM, |
| 3264 | &BTRFS_I(inode)->runtime_flags)) { |
Yan, Zheng | d68fc57 | 2010-05-16 10:49:58 -0400 | [diff] [blame] | 3265 | #if 0 |
| 3266 | /* |
| 3267 | * For proper ENOSPC handling, we should do orphan |
| 3268 | * cleanup when mounting. But this introduces backward |
| 3269 | * compatibility issue. |
| 3270 | */ |
| 3271 | if (!xchg(&root->orphan_item_inserted, 1)) |
| 3272 | insert = 2; |
| 3273 | else |
| 3274 | insert = 1; |
| 3275 | #endif |
| 3276 | insert = 1; |
Miao Xie | 321f0e7 | 2012-08-28 22:13:02 -0600 | [diff] [blame] | 3277 | atomic_inc(&root->orphan_inodes); |
Yan, Zheng | d68fc57 | 2010-05-16 10:49:58 -0400 | [diff] [blame] | 3278 | } |
Josef Bacik | 7b12876 | 2008-07-24 12:17:14 -0400 | [diff] [blame] | 3279 | |
Josef Bacik | 72ac3c0 | 2012-05-23 14:13:11 -0400 | [diff] [blame] | 3280 | if (!test_and_set_bit(BTRFS_INODE_ORPHAN_META_RESERVED, |
| 3281 | &BTRFS_I(inode)->runtime_flags)) |
Yan, Zheng | d68fc57 | 2010-05-16 10:49:58 -0400 | [diff] [blame] | 3282 | reserve = 1; |
Yan, Zheng | d68fc57 | 2010-05-16 10:49:58 -0400 | [diff] [blame] | 3283 | spin_unlock(&root->orphan_lock); |
Josef Bacik | 7b12876 | 2008-07-24 12:17:14 -0400 | [diff] [blame] | 3284 | |
Yan, Zheng | d68fc57 | 2010-05-16 10:49:58 -0400 | [diff] [blame] | 3285 | /* grab metadata reservation from transaction handle */ |
| 3286 | if (reserve) { |
| 3287 | ret = btrfs_orphan_reserve_metadata(trans, inode); |
Josef Bacik | 3b6571c | 2016-05-27 13:03:04 -0400 | [diff] [blame] | 3288 | ASSERT(!ret); |
| 3289 | if (ret) { |
| 3290 | atomic_dec(&root->orphan_inodes); |
| 3291 | clear_bit(BTRFS_INODE_ORPHAN_META_RESERVED, |
| 3292 | &BTRFS_I(inode)->runtime_flags); |
| 3293 | if (insert) |
| 3294 | clear_bit(BTRFS_INODE_HAS_ORPHAN_ITEM, |
| 3295 | &BTRFS_I(inode)->runtime_flags); |
| 3296 | return ret; |
| 3297 | } |
Yan, Zheng | d68fc57 | 2010-05-16 10:49:58 -0400 | [diff] [blame] | 3298 | } |
| 3299 | |
| 3300 | /* insert an orphan item to track this unlinked/truncated file */ |
| 3301 | if (insert >= 1) { |
Li Zefan | 33345d01 | 2011-04-20 10:31:50 +0800 | [diff] [blame] | 3302 | ret = btrfs_insert_orphan_item(trans, root, btrfs_ino(inode)); |
Josef Bacik | 4ef31a4 | 2013-08-13 14:10:08 -0400 | [diff] [blame] | 3303 | if (ret) { |
Filipe David Borba Manana | 703c88e | 2013-09-22 21:54:55 +0100 | [diff] [blame] | 3304 | atomic_dec(&root->orphan_inodes); |
Josef Bacik | 4ef31a4 | 2013-08-13 14:10:08 -0400 | [diff] [blame] | 3305 | if (reserve) { |
| 3306 | clear_bit(BTRFS_INODE_ORPHAN_META_RESERVED, |
| 3307 | &BTRFS_I(inode)->runtime_flags); |
| 3308 | btrfs_orphan_release_metadata(inode); |
| 3309 | } |
| 3310 | if (ret != -EEXIST) { |
Josef Bacik | e8e7cff | 2013-08-21 15:54:00 -0400 | [diff] [blame] | 3311 | clear_bit(BTRFS_INODE_HAS_ORPHAN_ITEM, |
| 3312 | &BTRFS_I(inode)->runtime_flags); |
Jeff Mahoney | 6664283 | 2016-06-10 18:19:25 -0400 | [diff] [blame] | 3313 | btrfs_abort_transaction(trans, ret); |
Josef Bacik | 4ef31a4 | 2013-08-13 14:10:08 -0400 | [diff] [blame] | 3314 | return ret; |
| 3315 | } |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 3316 | } |
| 3317 | ret = 0; |
Yan, Zheng | d68fc57 | 2010-05-16 10:49:58 -0400 | [diff] [blame] | 3318 | } |
| 3319 | |
| 3320 | /* insert an orphan item to track subvolume contains orphan files */ |
| 3321 | if (insert >= 2) { |
| 3322 | ret = btrfs_insert_orphan_item(trans, root->fs_info->tree_root, |
| 3323 | root->root_key.objectid); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 3324 | if (ret && ret != -EEXIST) { |
Jeff Mahoney | 6664283 | 2016-06-10 18:19:25 -0400 | [diff] [blame] | 3325 | btrfs_abort_transaction(trans, ret); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 3326 | return ret; |
| 3327 | } |
Yan, Zheng | d68fc57 | 2010-05-16 10:49:58 -0400 | [diff] [blame] | 3328 | } |
| 3329 | return 0; |
Josef Bacik | 7b12876 | 2008-07-24 12:17:14 -0400 | [diff] [blame] | 3330 | } |
| 3331 | |
| 3332 | /* |
| 3333 | * We have done the truncate/delete so we can go ahead and remove the orphan |
| 3334 | * item for this particular inode. |
| 3335 | */ |
Eric Sandeen | 48a3b63 | 2013-04-25 20:41:01 +0000 | [diff] [blame] | 3336 | static int btrfs_orphan_del(struct btrfs_trans_handle *trans, |
| 3337 | struct inode *inode) |
Josef Bacik | 7b12876 | 2008-07-24 12:17:14 -0400 | [diff] [blame] | 3338 | { |
| 3339 | struct btrfs_root *root = BTRFS_I(inode)->root; |
Yan, Zheng | d68fc57 | 2010-05-16 10:49:58 -0400 | [diff] [blame] | 3340 | int delete_item = 0; |
| 3341 | int release_rsv = 0; |
Josef Bacik | 7b12876 | 2008-07-24 12:17:14 -0400 | [diff] [blame] | 3342 | int ret = 0; |
| 3343 | |
Yan, Zheng | d68fc57 | 2010-05-16 10:49:58 -0400 | [diff] [blame] | 3344 | spin_lock(&root->orphan_lock); |
Josef Bacik | 8a35d95 | 2012-05-23 14:26:42 -0400 | [diff] [blame] | 3345 | if (test_and_clear_bit(BTRFS_INODE_HAS_ORPHAN_ITEM, |
| 3346 | &BTRFS_I(inode)->runtime_flags)) |
Yan, Zheng | d68fc57 | 2010-05-16 10:49:58 -0400 | [diff] [blame] | 3347 | delete_item = 1; |
Josef Bacik | 7b12876 | 2008-07-24 12:17:14 -0400 | [diff] [blame] | 3348 | |
Josef Bacik | 72ac3c0 | 2012-05-23 14:13:11 -0400 | [diff] [blame] | 3349 | if (test_and_clear_bit(BTRFS_INODE_ORPHAN_META_RESERVED, |
| 3350 | &BTRFS_I(inode)->runtime_flags)) |
Yan, Zheng | d68fc57 | 2010-05-16 10:49:58 -0400 | [diff] [blame] | 3351 | release_rsv = 1; |
Yan, Zheng | d68fc57 | 2010-05-16 10:49:58 -0400 | [diff] [blame] | 3352 | spin_unlock(&root->orphan_lock); |
| 3353 | |
Filipe David Borba Manana | 703c88e | 2013-09-22 21:54:55 +0100 | [diff] [blame] | 3354 | if (delete_item) { |
Josef Bacik | 8a35d95 | 2012-05-23 14:26:42 -0400 | [diff] [blame] | 3355 | atomic_dec(&root->orphan_inodes); |
Filipe David Borba Manana | 703c88e | 2013-09-22 21:54:55 +0100 | [diff] [blame] | 3356 | if (trans) |
| 3357 | ret = btrfs_del_orphan_item(trans, root, |
| 3358 | btrfs_ino(inode)); |
Josef Bacik | 8a35d95 | 2012-05-23 14:26:42 -0400 | [diff] [blame] | 3359 | } |
Josef Bacik | 7b12876 | 2008-07-24 12:17:14 -0400 | [diff] [blame] | 3360 | |
Filipe David Borba Manana | 703c88e | 2013-09-22 21:54:55 +0100 | [diff] [blame] | 3361 | if (release_rsv) |
| 3362 | btrfs_orphan_release_metadata(inode); |
| 3363 | |
Josef Bacik | 4ef31a4 | 2013-08-13 14:10:08 -0400 | [diff] [blame] | 3364 | return ret; |
Josef Bacik | 7b12876 | 2008-07-24 12:17:14 -0400 | [diff] [blame] | 3365 | } |
| 3366 | |
| 3367 | /* |
| 3368 | * this cleans up any orphans that may be left on the list from the last use |
| 3369 | * of this root. |
| 3370 | */ |
Josef Bacik | 66b4ffd | 2011-01-31 16:22:42 -0500 | [diff] [blame] | 3371 | int btrfs_orphan_cleanup(struct btrfs_root *root) |
Josef Bacik | 7b12876 | 2008-07-24 12:17:14 -0400 | [diff] [blame] | 3372 | { |
| 3373 | struct btrfs_path *path; |
| 3374 | struct extent_buffer *leaf; |
Josef Bacik | 7b12876 | 2008-07-24 12:17:14 -0400 | [diff] [blame] | 3375 | struct btrfs_key key, found_key; |
| 3376 | struct btrfs_trans_handle *trans; |
| 3377 | struct inode *inode; |
Josef Bacik | 8f6d7f4 | 2011-09-26 15:55:20 -0400 | [diff] [blame] | 3378 | u64 last_objectid = 0; |
Josef Bacik | 7b12876 | 2008-07-24 12:17:14 -0400 | [diff] [blame] | 3379 | int ret = 0, nr_unlink = 0, nr_truncate = 0; |
| 3380 | |
Yan, Zheng | d68fc57 | 2010-05-16 10:49:58 -0400 | [diff] [blame] | 3381 | if (cmpxchg(&root->orphan_cleanup_state, 0, ORPHAN_CLEANUP_STARTED)) |
Josef Bacik | 66b4ffd | 2011-01-31 16:22:42 -0500 | [diff] [blame] | 3382 | return 0; |
Yan, Zheng | c71bf09 | 2009-11-12 09:34:40 +0000 | [diff] [blame] | 3383 | |
| 3384 | path = btrfs_alloc_path(); |
Josef Bacik | 66b4ffd | 2011-01-31 16:22:42 -0500 | [diff] [blame] | 3385 | if (!path) { |
| 3386 | ret = -ENOMEM; |
| 3387 | goto out; |
| 3388 | } |
David Sterba | e4058b5 | 2015-11-27 16:31:35 +0100 | [diff] [blame] | 3389 | path->reada = READA_BACK; |
Josef Bacik | 7b12876 | 2008-07-24 12:17:14 -0400 | [diff] [blame] | 3390 | |
| 3391 | key.objectid = BTRFS_ORPHAN_OBJECTID; |
David Sterba | 962a298 | 2014-06-04 18:41:45 +0200 | [diff] [blame] | 3392 | key.type = BTRFS_ORPHAN_ITEM_KEY; |
Josef Bacik | 7b12876 | 2008-07-24 12:17:14 -0400 | [diff] [blame] | 3393 | key.offset = (u64)-1; |
| 3394 | |
Josef Bacik | 7b12876 | 2008-07-24 12:17:14 -0400 | [diff] [blame] | 3395 | while (1) { |
| 3396 | ret = btrfs_search_slot(NULL, root, &key, path, 0, 0); |
Josef Bacik | 66b4ffd | 2011-01-31 16:22:42 -0500 | [diff] [blame] | 3397 | if (ret < 0) |
| 3398 | goto out; |
Josef Bacik | 7b12876 | 2008-07-24 12:17:14 -0400 | [diff] [blame] | 3399 | |
| 3400 | /* |
| 3401 | * 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] | 3402 | * 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] | 3403 | * find the key and see if we have stuff that matches |
| 3404 | */ |
| 3405 | if (ret > 0) { |
Josef Bacik | 66b4ffd | 2011-01-31 16:22:42 -0500 | [diff] [blame] | 3406 | ret = 0; |
Josef Bacik | 7b12876 | 2008-07-24 12:17:14 -0400 | [diff] [blame] | 3407 | if (path->slots[0] == 0) |
| 3408 | break; |
| 3409 | path->slots[0]--; |
| 3410 | } |
| 3411 | |
| 3412 | /* pull out the item */ |
| 3413 | leaf = path->nodes[0]; |
Josef Bacik | 7b12876 | 2008-07-24 12:17:14 -0400 | [diff] [blame] | 3414 | btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]); |
| 3415 | |
| 3416 | /* make sure the item matches what we want */ |
| 3417 | if (found_key.objectid != BTRFS_ORPHAN_OBJECTID) |
| 3418 | break; |
David Sterba | 962a298 | 2014-06-04 18:41:45 +0200 | [diff] [blame] | 3419 | if (found_key.type != BTRFS_ORPHAN_ITEM_KEY) |
Josef Bacik | 7b12876 | 2008-07-24 12:17:14 -0400 | [diff] [blame] | 3420 | break; |
| 3421 | |
| 3422 | /* release the path since we're done with it */ |
David Sterba | b3b4aa7 | 2011-04-21 01:20:15 +0200 | [diff] [blame] | 3423 | btrfs_release_path(path); |
Josef Bacik | 7b12876 | 2008-07-24 12:17:14 -0400 | [diff] [blame] | 3424 | |
| 3425 | /* |
| 3426 | * this is where we are basically btrfs_lookup, without the |
| 3427 | * crossing root thing. we store the inode number in the |
| 3428 | * offset of the orphan item. |
| 3429 | */ |
Josef Bacik | 8f6d7f4 | 2011-09-26 15:55:20 -0400 | [diff] [blame] | 3430 | |
| 3431 | if (found_key.offset == last_objectid) { |
Simon Kirby | c2cf52e | 2013-03-19 22:41:23 +0000 | [diff] [blame] | 3432 | btrfs_err(root->fs_info, |
| 3433 | "Error removing orphan entry, stopping orphan cleanup"); |
Josef Bacik | 8f6d7f4 | 2011-09-26 15:55:20 -0400 | [diff] [blame] | 3434 | ret = -EINVAL; |
| 3435 | goto out; |
| 3436 | } |
| 3437 | |
| 3438 | last_objectid = found_key.offset; |
| 3439 | |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 3440 | found_key.objectid = found_key.offset; |
| 3441 | found_key.type = BTRFS_INODE_ITEM_KEY; |
| 3442 | found_key.offset = 0; |
Josef Bacik | 73f7341 | 2009-12-04 17:38:27 +0000 | [diff] [blame] | 3443 | inode = btrfs_iget(root->fs_info->sb, &found_key, root, NULL); |
Rusty Russell | 8c6ffba | 2013-07-15 11:20:32 +0930 | [diff] [blame] | 3444 | ret = PTR_ERR_OR_ZERO(inode); |
Filipe Manana | 6771089 | 2016-06-06 11:51:25 +0100 | [diff] [blame] | 3445 | if (ret && ret != -ENOENT) |
Josef Bacik | 66b4ffd | 2011-01-31 16:22:42 -0500 | [diff] [blame] | 3446 | goto out; |
Josef Bacik | a8c9e57 | 2011-09-21 16:55:59 -0400 | [diff] [blame] | 3447 | |
Filipe Manana | 6771089 | 2016-06-06 11:51:25 +0100 | [diff] [blame] | 3448 | if (ret == -ENOENT && root == root->fs_info->tree_root) { |
Arne Jansen | f8e9e0b | 2011-12-14 20:12:02 -0500 | [diff] [blame] | 3449 | struct btrfs_root *dead_root; |
| 3450 | struct btrfs_fs_info *fs_info = root->fs_info; |
| 3451 | int is_dead_root = 0; |
| 3452 | |
| 3453 | /* |
| 3454 | * this is an orphan in the tree root. Currently these |
| 3455 | * could come from 2 sources: |
| 3456 | * a) a snapshot deletion in progress |
| 3457 | * b) a free space cache inode |
| 3458 | * We need to distinguish those two, as the snapshot |
| 3459 | * orphan must not get deleted. |
| 3460 | * find_dead_roots already ran before us, so if this |
| 3461 | * is a snapshot deletion, we should find the root |
| 3462 | * in the dead_roots list |
| 3463 | */ |
| 3464 | spin_lock(&fs_info->trans_lock); |
| 3465 | list_for_each_entry(dead_root, &fs_info->dead_roots, |
| 3466 | root_list) { |
| 3467 | if (dead_root->root_key.objectid == |
| 3468 | found_key.objectid) { |
| 3469 | is_dead_root = 1; |
| 3470 | break; |
| 3471 | } |
| 3472 | } |
| 3473 | spin_unlock(&fs_info->trans_lock); |
| 3474 | if (is_dead_root) { |
| 3475 | /* prevent this orphan from being found again */ |
| 3476 | key.offset = found_key.objectid - 1; |
| 3477 | continue; |
| 3478 | } |
| 3479 | } |
Josef Bacik | a8c9e57 | 2011-09-21 16:55:59 -0400 | [diff] [blame] | 3480 | /* |
| 3481 | * Inode is already gone but the orphan item is still there, |
| 3482 | * kill the orphan item. |
| 3483 | */ |
Filipe Manana | 6771089 | 2016-06-06 11:51:25 +0100 | [diff] [blame] | 3484 | if (ret == -ENOENT) { |
Josef Bacik | a8c9e57 | 2011-09-21 16:55:59 -0400 | [diff] [blame] | 3485 | trans = btrfs_start_transaction(root, 1); |
| 3486 | if (IS_ERR(trans)) { |
| 3487 | ret = PTR_ERR(trans); |
| 3488 | goto out; |
| 3489 | } |
Simon Kirby | c2cf52e | 2013-03-19 22:41:23 +0000 | [diff] [blame] | 3490 | btrfs_debug(root->fs_info, "auto deleting %Lu", |
| 3491 | found_key.objectid); |
Josef Bacik | a8c9e57 | 2011-09-21 16:55:59 -0400 | [diff] [blame] | 3492 | ret = btrfs_del_orphan_item(trans, root, |
| 3493 | found_key.objectid); |
Josef Bacik | a8c9e57 | 2011-09-21 16:55:59 -0400 | [diff] [blame] | 3494 | btrfs_end_transaction(trans, root); |
Josef Bacik | 4ef31a4 | 2013-08-13 14:10:08 -0400 | [diff] [blame] | 3495 | if (ret) |
| 3496 | goto out; |
Josef Bacik | a8c9e57 | 2011-09-21 16:55:59 -0400 | [diff] [blame] | 3497 | continue; |
Josef Bacik | 66b4ffd | 2011-01-31 16:22:42 -0500 | [diff] [blame] | 3498 | } |
Josef Bacik | 7b12876 | 2008-07-24 12:17:14 -0400 | [diff] [blame] | 3499 | |
Josef Bacik | 7b12876 | 2008-07-24 12:17:14 -0400 | [diff] [blame] | 3500 | /* |
| 3501 | * add this inode to the orphan list so btrfs_orphan_del does |
| 3502 | * the proper thing when we hit it |
| 3503 | */ |
Josef Bacik | 8a35d95 | 2012-05-23 14:26:42 -0400 | [diff] [blame] | 3504 | set_bit(BTRFS_INODE_HAS_ORPHAN_ITEM, |
| 3505 | &BTRFS_I(inode)->runtime_flags); |
Josef Bacik | 925396e | 2013-02-01 15:57:47 -0500 | [diff] [blame] | 3506 | atomic_inc(&root->orphan_inodes); |
Josef Bacik | 7b12876 | 2008-07-24 12:17:14 -0400 | [diff] [blame] | 3507 | |
Josef Bacik | 7b12876 | 2008-07-24 12:17:14 -0400 | [diff] [blame] | 3508 | /* if we have links, this was a truncate, lets do that */ |
| 3509 | if (inode->i_nlink) { |
Dulshani Gunawardhana | fae7f21 | 2013-10-31 10:30:08 +0530 | [diff] [blame] | 3510 | if (WARN_ON(!S_ISREG(inode->i_mode))) { |
Josef Bacik | a41ad39 | 2011-01-31 15:30:16 -0500 | [diff] [blame] | 3511 | iput(inode); |
| 3512 | continue; |
| 3513 | } |
Josef Bacik | 7b12876 | 2008-07-24 12:17:14 -0400 | [diff] [blame] | 3514 | nr_truncate++; |
Josef Bacik | f3fe820 | 2013-01-07 17:03:21 -0500 | [diff] [blame] | 3515 | |
| 3516 | /* 1 for the orphan item deletion. */ |
| 3517 | trans = btrfs_start_transaction(root, 1); |
| 3518 | if (IS_ERR(trans)) { |
Josef Bacik | c69b26b | 2013-06-03 16:51:23 -0400 | [diff] [blame] | 3519 | iput(inode); |
Josef Bacik | f3fe820 | 2013-01-07 17:03:21 -0500 | [diff] [blame] | 3520 | ret = PTR_ERR(trans); |
| 3521 | goto out; |
| 3522 | } |
| 3523 | ret = btrfs_orphan_add(trans, inode); |
| 3524 | btrfs_end_transaction(trans, root); |
Josef Bacik | c69b26b | 2013-06-03 16:51:23 -0400 | [diff] [blame] | 3525 | if (ret) { |
| 3526 | iput(inode); |
Josef Bacik | f3fe820 | 2013-01-07 17:03:21 -0500 | [diff] [blame] | 3527 | goto out; |
Josef Bacik | c69b26b | 2013-06-03 16:51:23 -0400 | [diff] [blame] | 3528 | } |
Josef Bacik | f3fe820 | 2013-01-07 17:03:21 -0500 | [diff] [blame] | 3529 | |
Josef Bacik | 66b4ffd | 2011-01-31 16:22:42 -0500 | [diff] [blame] | 3530 | ret = btrfs_truncate(inode); |
Josef Bacik | 4a7d0f6 | 2013-02-07 16:27:28 -0500 | [diff] [blame] | 3531 | if (ret) |
| 3532 | btrfs_orphan_del(NULL, inode); |
Josef Bacik | 7b12876 | 2008-07-24 12:17:14 -0400 | [diff] [blame] | 3533 | } else { |
| 3534 | nr_unlink++; |
| 3535 | } |
| 3536 | |
| 3537 | /* this will do delete_inode and everything for us */ |
| 3538 | iput(inode); |
Josef Bacik | 66b4ffd | 2011-01-31 16:22:42 -0500 | [diff] [blame] | 3539 | if (ret) |
| 3540 | goto out; |
Josef Bacik | 7b12876 | 2008-07-24 12:17:14 -0400 | [diff] [blame] | 3541 | } |
Miao Xie | 3254c87 | 2011-11-10 20:45:05 -0500 | [diff] [blame] | 3542 | /* release the path since we're done with it */ |
| 3543 | btrfs_release_path(path); |
| 3544 | |
Yan, Zheng | d68fc57 | 2010-05-16 10:49:58 -0400 | [diff] [blame] | 3545 | root->orphan_cleanup_state = ORPHAN_CLEANUP_DONE; |
| 3546 | |
| 3547 | if (root->orphan_block_rsv) |
| 3548 | btrfs_block_rsv_release(root, root->orphan_block_rsv, |
| 3549 | (u64)-1); |
| 3550 | |
Miao Xie | 27cdeb7 | 2014-04-02 19:51:05 +0800 | [diff] [blame] | 3551 | if (root->orphan_block_rsv || |
| 3552 | test_bit(BTRFS_ROOT_ORPHAN_ITEM_INSERTED, &root->state)) { |
Josef Bacik | 7a7eaa4 | 2011-04-13 12:54:33 -0400 | [diff] [blame] | 3553 | trans = btrfs_join_transaction(root); |
Josef Bacik | 66b4ffd | 2011-01-31 16:22:42 -0500 | [diff] [blame] | 3554 | if (!IS_ERR(trans)) |
| 3555 | btrfs_end_transaction(trans, root); |
Yan, Zheng | d68fc57 | 2010-05-16 10:49:58 -0400 | [diff] [blame] | 3556 | } |
Josef Bacik | 7b12876 | 2008-07-24 12:17:14 -0400 | [diff] [blame] | 3557 | |
| 3558 | if (nr_unlink) |
David Sterba | 4884b47 | 2013-03-20 13:31:27 +0000 | [diff] [blame] | 3559 | btrfs_debug(root->fs_info, "unlinked %d orphans", nr_unlink); |
Josef Bacik | 7b12876 | 2008-07-24 12:17:14 -0400 | [diff] [blame] | 3560 | if (nr_truncate) |
David Sterba | 4884b47 | 2013-03-20 13:31:27 +0000 | [diff] [blame] | 3561 | btrfs_debug(root->fs_info, "truncated %d orphans", nr_truncate); |
Josef Bacik | 66b4ffd | 2011-01-31 16:22:42 -0500 | [diff] [blame] | 3562 | |
| 3563 | out: |
| 3564 | if (ret) |
David Sterba | 68b663d | 2014-12-19 18:38:37 +0100 | [diff] [blame] | 3565 | btrfs_err(root->fs_info, |
Simon Kirby | c2cf52e | 2013-03-19 22:41:23 +0000 | [diff] [blame] | 3566 | "could not do orphan cleanup %d", ret); |
Josef Bacik | 66b4ffd | 2011-01-31 16:22:42 -0500 | [diff] [blame] | 3567 | btrfs_free_path(path); |
| 3568 | return ret; |
Josef Bacik | 7b12876 | 2008-07-24 12:17:14 -0400 | [diff] [blame] | 3569 | } |
| 3570 | |
Chris Mason | d352ac6 | 2008-09-29 15:18:18 -0400 | [diff] [blame] | 3571 | /* |
Chris Mason | 46a53cc | 2009-04-27 11:47:50 -0400 | [diff] [blame] | 3572 | * very simple check to peek ahead in the leaf looking for xattrs. If we |
| 3573 | * don't find any xattrs, we know there can't be any acls. |
| 3574 | * |
| 3575 | * slot is the slot the inode is in, objectid is the objectid of the inode |
| 3576 | */ |
| 3577 | 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] | 3578 | int slot, u64 objectid, |
| 3579 | int *first_xattr_slot) |
Chris Mason | 46a53cc | 2009-04-27 11:47:50 -0400 | [diff] [blame] | 3580 | { |
| 3581 | u32 nritems = btrfs_header_nritems(leaf); |
| 3582 | struct btrfs_key found_key; |
Josef Bacik | f23b5a5 | 2013-06-19 10:16:26 -0400 | [diff] [blame] | 3583 | static u64 xattr_access = 0; |
| 3584 | static u64 xattr_default = 0; |
Chris Mason | 46a53cc | 2009-04-27 11:47:50 -0400 | [diff] [blame] | 3585 | int scanned = 0; |
| 3586 | |
Josef Bacik | f23b5a5 | 2013-06-19 10:16:26 -0400 | [diff] [blame] | 3587 | if (!xattr_access) { |
Andreas Gruenbacher | 97d7929 | 2015-12-02 14:44:35 +0100 | [diff] [blame] | 3588 | xattr_access = btrfs_name_hash(XATTR_NAME_POSIX_ACL_ACCESS, |
| 3589 | strlen(XATTR_NAME_POSIX_ACL_ACCESS)); |
| 3590 | xattr_default = btrfs_name_hash(XATTR_NAME_POSIX_ACL_DEFAULT, |
| 3591 | strlen(XATTR_NAME_POSIX_ACL_DEFAULT)); |
Josef Bacik | f23b5a5 | 2013-06-19 10:16:26 -0400 | [diff] [blame] | 3592 | } |
| 3593 | |
Chris Mason | 46a53cc | 2009-04-27 11:47:50 -0400 | [diff] [blame] | 3594 | slot++; |
Filipe David Borba Manana | 6354192 | 2014-01-07 11:47:46 +0000 | [diff] [blame] | 3595 | *first_xattr_slot = -1; |
Chris Mason | 46a53cc | 2009-04-27 11:47:50 -0400 | [diff] [blame] | 3596 | while (slot < nritems) { |
| 3597 | btrfs_item_key_to_cpu(leaf, &found_key, slot); |
| 3598 | |
| 3599 | /* we found a different objectid, there must not be acls */ |
| 3600 | if (found_key.objectid != objectid) |
| 3601 | return 0; |
| 3602 | |
| 3603 | /* we found an xattr, assume we've got an acl */ |
Josef Bacik | f23b5a5 | 2013-06-19 10:16:26 -0400 | [diff] [blame] | 3604 | if (found_key.type == BTRFS_XATTR_ITEM_KEY) { |
Filipe David Borba Manana | 6354192 | 2014-01-07 11:47:46 +0000 | [diff] [blame] | 3605 | if (*first_xattr_slot == -1) |
| 3606 | *first_xattr_slot = slot; |
Josef Bacik | f23b5a5 | 2013-06-19 10:16:26 -0400 | [diff] [blame] | 3607 | if (found_key.offset == xattr_access || |
| 3608 | found_key.offset == xattr_default) |
| 3609 | return 1; |
| 3610 | } |
Chris Mason | 46a53cc | 2009-04-27 11:47:50 -0400 | [diff] [blame] | 3611 | |
| 3612 | /* |
| 3613 | * we found a key greater than an xattr key, there can't |
| 3614 | * be any acls later on |
| 3615 | */ |
| 3616 | if (found_key.type > BTRFS_XATTR_ITEM_KEY) |
| 3617 | return 0; |
| 3618 | |
| 3619 | slot++; |
| 3620 | scanned++; |
| 3621 | |
| 3622 | /* |
| 3623 | * it goes inode, inode backrefs, xattrs, extents, |
| 3624 | * so if there are a ton of hard links to an inode there can |
| 3625 | * be a lot of backrefs. Don't waste time searching too hard, |
| 3626 | * this is just an optimization |
| 3627 | */ |
| 3628 | if (scanned >= 8) |
| 3629 | break; |
| 3630 | } |
| 3631 | /* we hit the end of the leaf before we found an xattr or |
| 3632 | * something larger than an xattr. We have to assume the inode |
| 3633 | * has acls |
| 3634 | */ |
Filipe David Borba Manana | 6354192 | 2014-01-07 11:47:46 +0000 | [diff] [blame] | 3635 | if (*first_xattr_slot == -1) |
| 3636 | *first_xattr_slot = slot; |
Chris Mason | 46a53cc | 2009-04-27 11:47:50 -0400 | [diff] [blame] | 3637 | return 1; |
| 3638 | } |
| 3639 | |
| 3640 | /* |
Chris Mason | d352ac6 | 2008-09-29 15:18:18 -0400 | [diff] [blame] | 3641 | * read an inode from the btree into the in-memory inode |
| 3642 | */ |
Filipe Manana | 6771089 | 2016-06-06 11:51:25 +0100 | [diff] [blame] | 3643 | static int btrfs_read_locked_inode(struct inode *inode) |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3644 | { |
| 3645 | struct btrfs_path *path; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 3646 | struct extent_buffer *leaf; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3647 | struct btrfs_inode_item *inode_item; |
| 3648 | struct btrfs_root *root = BTRFS_I(inode)->root; |
| 3649 | struct btrfs_key location; |
Miao Xie | 67de117 | 2013-12-26 13:07:06 +0800 | [diff] [blame] | 3650 | unsigned long ptr; |
Chris Mason | 46a53cc | 2009-04-27 11:47:50 -0400 | [diff] [blame] | 3651 | int maybe_acls; |
Josef Bacik | 618e21d | 2007-07-11 10:18:17 -0400 | [diff] [blame] | 3652 | u32 rdev; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3653 | int ret; |
Miao Xie | 2f7e33d | 2011-06-23 07:27:13 +0000 | [diff] [blame] | 3654 | bool filled = false; |
Filipe David Borba Manana | 6354192 | 2014-01-07 11:47:46 +0000 | [diff] [blame] | 3655 | int first_xattr_slot; |
Miao Xie | 2f7e33d | 2011-06-23 07:27:13 +0000 | [diff] [blame] | 3656 | |
| 3657 | ret = btrfs_fill_inode(inode, &rdev); |
| 3658 | if (!ret) |
| 3659 | filled = true; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3660 | |
| 3661 | path = btrfs_alloc_path(); |
Filipe Manana | 6771089 | 2016-06-06 11:51:25 +0100 | [diff] [blame] | 3662 | if (!path) { |
| 3663 | ret = -ENOMEM; |
Mark Fasheh | 1748f84 | 2011-07-12 11:25:31 -0700 | [diff] [blame] | 3664 | goto make_bad; |
Filipe Manana | 6771089 | 2016-06-06 11:51:25 +0100 | [diff] [blame] | 3665 | } |
Mark Fasheh | 1748f84 | 2011-07-12 11:25:31 -0700 | [diff] [blame] | 3666 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3667 | memcpy(&location, &BTRFS_I(inode)->location, sizeof(location)); |
Chris Mason | dc17ff8 | 2008-01-08 15:46:30 -0500 | [diff] [blame] | 3668 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3669 | ret = btrfs_lookup_inode(NULL, root, path, &location, 0); |
Filipe Manana | 6771089 | 2016-06-06 11:51:25 +0100 | [diff] [blame] | 3670 | if (ret) { |
| 3671 | if (ret > 0) |
| 3672 | ret = -ENOENT; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3673 | goto make_bad; |
Filipe Manana | 6771089 | 2016-06-06 11:51:25 +0100 | [diff] [blame] | 3674 | } |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3675 | |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 3676 | leaf = path->nodes[0]; |
Miao Xie | 2f7e33d | 2011-06-23 07:27:13 +0000 | [diff] [blame] | 3677 | |
| 3678 | if (filled) |
Miao Xie | 67de117 | 2013-12-26 13:07:06 +0800 | [diff] [blame] | 3679 | goto cache_index; |
Miao Xie | 2f7e33d | 2011-06-23 07:27:13 +0000 | [diff] [blame] | 3680 | |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 3681 | inode_item = btrfs_item_ptr(leaf, path->slots[0], |
| 3682 | struct btrfs_inode_item); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 3683 | inode->i_mode = btrfs_inode_mode(leaf, inode_item); |
Miklos Szeredi | bfe8684 | 2011-10-28 14:13:29 +0200 | [diff] [blame] | 3684 | set_nlink(inode, btrfs_inode_nlink(leaf, inode_item)); |
Eric W. Biederman | 2f2f43d | 2012-02-10 11:05:07 -0800 | [diff] [blame] | 3685 | i_uid_write(inode, btrfs_inode_uid(leaf, inode_item)); |
| 3686 | i_gid_write(inode, btrfs_inode_gid(leaf, inode_item)); |
Chris Mason | dbe674a | 2008-07-17 12:54:05 -0400 | [diff] [blame] | 3687 | btrfs_i_size_write(inode, btrfs_inode_size(leaf, inode_item)); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 3688 | |
David Sterba | a937b97 | 2014-12-12 17:39:12 +0100 | [diff] [blame] | 3689 | inode->i_atime.tv_sec = btrfs_timespec_sec(leaf, &inode_item->atime); |
| 3690 | inode->i_atime.tv_nsec = btrfs_timespec_nsec(leaf, &inode_item->atime); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 3691 | |
David Sterba | a937b97 | 2014-12-12 17:39:12 +0100 | [diff] [blame] | 3692 | inode->i_mtime.tv_sec = btrfs_timespec_sec(leaf, &inode_item->mtime); |
| 3693 | inode->i_mtime.tv_nsec = btrfs_timespec_nsec(leaf, &inode_item->mtime); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 3694 | |
David Sterba | a937b97 | 2014-12-12 17:39:12 +0100 | [diff] [blame] | 3695 | inode->i_ctime.tv_sec = btrfs_timespec_sec(leaf, &inode_item->ctime); |
| 3696 | inode->i_ctime.tv_nsec = btrfs_timespec_nsec(leaf, &inode_item->ctime); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 3697 | |
chandan r | 9cc97d6 | 2012-07-04 12:48:07 +0530 | [diff] [blame] | 3698 | BTRFS_I(inode)->i_otime.tv_sec = |
| 3699 | btrfs_timespec_sec(leaf, &inode_item->otime); |
| 3700 | BTRFS_I(inode)->i_otime.tv_nsec = |
| 3701 | btrfs_timespec_nsec(leaf, &inode_item->otime); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 3702 | |
Yan Zheng | a76a3cd | 2008-10-09 11:46:29 -0400 | [diff] [blame] | 3703 | inode_set_bytes(inode, btrfs_inode_nbytes(leaf, inode_item)); |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 3704 | BTRFS_I(inode)->generation = btrfs_inode_generation(leaf, inode_item); |
Josef Bacik | 5dc562c | 2012-08-17 13:14:17 -0400 | [diff] [blame] | 3705 | BTRFS_I(inode)->last_trans = btrfs_inode_transid(leaf, inode_item); |
| 3706 | |
Josef Bacik | 0c4d2d9 | 2012-04-05 15:03:02 -0400 | [diff] [blame] | 3707 | inode->i_version = btrfs_inode_sequence(leaf, inode_item); |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 3708 | inode->i_generation = BTRFS_I(inode)->generation; |
Josef Bacik | 618e21d | 2007-07-11 10:18:17 -0400 | [diff] [blame] | 3709 | inode->i_rdev = 0; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 3710 | rdev = btrfs_inode_rdev(leaf, inode_item); |
| 3711 | |
Josef Bacik | aec7477 | 2008-07-24 12:12:38 -0400 | [diff] [blame] | 3712 | BTRFS_I(inode)->index_cnt = (u64)-1; |
Yan Zheng | d2fb343 | 2008-12-11 16:30:39 -0500 | [diff] [blame] | 3713 | BTRFS_I(inode)->flags = btrfs_inode_flags(leaf, inode_item); |
Miao Xie | 67de117 | 2013-12-26 13:07:06 +0800 | [diff] [blame] | 3714 | |
| 3715 | cache_index: |
Yang Dongsheng | 6e17d30 | 2015-04-09 12:08:43 +0800 | [diff] [blame] | 3716 | /* |
| 3717 | * If we were modified in the current generation and evicted from memory |
| 3718 | * and then re-read we need to do a full sync since we don't have any |
| 3719 | * idea about which extents were modified before we were evicted from |
| 3720 | * cache. |
| 3721 | * |
| 3722 | * This is required for both inode re-read from disk and delayed inode |
| 3723 | * in delayed_nodes_tree. |
| 3724 | */ |
| 3725 | if (BTRFS_I(inode)->last_trans == root->fs_info->generation) |
| 3726 | set_bit(BTRFS_INODE_NEEDS_FULL_SYNC, |
| 3727 | &BTRFS_I(inode)->runtime_flags); |
| 3728 | |
Filipe Manana | bde6c24 | 2015-07-24 00:00:19 +0100 | [diff] [blame] | 3729 | /* |
| 3730 | * We don't persist the id of the transaction where an unlink operation |
| 3731 | * against the inode was last made. So here we assume the inode might |
| 3732 | * have been evicted, and therefore the exact value of last_unlink_trans |
| 3733 | * lost, and set it to last_trans to avoid metadata inconsistencies |
| 3734 | * between the inode and its parent if the inode is fsync'ed and the log |
| 3735 | * replayed. For example, in the scenario: |
| 3736 | * |
| 3737 | * touch mydir/foo |
| 3738 | * ln mydir/foo mydir/bar |
| 3739 | * sync |
| 3740 | * unlink mydir/bar |
| 3741 | * echo 2 > /proc/sys/vm/drop_caches # evicts inode |
| 3742 | * xfs_io -c fsync mydir/foo |
| 3743 | * <power failure> |
| 3744 | * mount fs, triggers fsync log replay |
| 3745 | * |
| 3746 | * We must make sure that when we fsync our inode foo we also log its |
| 3747 | * parent inode, otherwise after log replay the parent still has the |
| 3748 | * dentry with the "bar" name but our inode foo has a link count of 1 |
| 3749 | * and doesn't have an inode ref with the name "bar" anymore. |
| 3750 | * |
| 3751 | * Setting last_unlink_trans to last_trans is a pessimistic approach, |
Nicholas D Steeves | 0132761 | 2016-05-19 21:18:45 -0400 | [diff] [blame] | 3752 | * but it guarantees correctness at the expense of occasional full |
Filipe Manana | bde6c24 | 2015-07-24 00:00:19 +0100 | [diff] [blame] | 3753 | * transaction commits on fsync if our inode is a directory, or if our |
| 3754 | * inode is not a directory, logging its parent unnecessarily. |
| 3755 | */ |
| 3756 | BTRFS_I(inode)->last_unlink_trans = BTRFS_I(inode)->last_trans; |
| 3757 | |
Miao Xie | 67de117 | 2013-12-26 13:07:06 +0800 | [diff] [blame] | 3758 | path->slots[0]++; |
| 3759 | if (inode->i_nlink != 1 || |
| 3760 | path->slots[0] >= btrfs_header_nritems(leaf)) |
| 3761 | goto cache_acl; |
| 3762 | |
| 3763 | btrfs_item_key_to_cpu(leaf, &location, path->slots[0]); |
| 3764 | if (location.objectid != btrfs_ino(inode)) |
| 3765 | goto cache_acl; |
| 3766 | |
| 3767 | ptr = btrfs_item_ptr_offset(leaf, path->slots[0]); |
| 3768 | if (location.type == BTRFS_INODE_REF_KEY) { |
| 3769 | struct btrfs_inode_ref *ref; |
| 3770 | |
| 3771 | ref = (struct btrfs_inode_ref *)ptr; |
| 3772 | BTRFS_I(inode)->dir_index = btrfs_inode_ref_index(leaf, ref); |
| 3773 | } else if (location.type == BTRFS_INODE_EXTREF_KEY) { |
| 3774 | struct btrfs_inode_extref *extref; |
| 3775 | |
| 3776 | extref = (struct btrfs_inode_extref *)ptr; |
| 3777 | BTRFS_I(inode)->dir_index = btrfs_inode_extref_index(leaf, |
| 3778 | extref); |
| 3779 | } |
Miao Xie | 2f7e33d | 2011-06-23 07:27:13 +0000 | [diff] [blame] | 3780 | cache_acl: |
Chris Mason | 46a53cc | 2009-04-27 11:47:50 -0400 | [diff] [blame] | 3781 | /* |
| 3782 | * try to precache a NULL acl entry for files that don't have |
| 3783 | * any xattrs or acls |
| 3784 | */ |
Li Zefan | 33345d01 | 2011-04-20 10:31:50 +0800 | [diff] [blame] | 3785 | maybe_acls = acls_after_inode_item(leaf, path->slots[0], |
Filipe David Borba Manana | 6354192 | 2014-01-07 11:47:46 +0000 | [diff] [blame] | 3786 | btrfs_ino(inode), &first_xattr_slot); |
| 3787 | if (first_xattr_slot != -1) { |
| 3788 | path->slots[0] = first_xattr_slot; |
| 3789 | ret = btrfs_load_inode_props(inode, path); |
| 3790 | if (ret) |
| 3791 | btrfs_err(root->fs_info, |
David Sterba | 351fd35 | 2014-05-15 16:48:20 +0200 | [diff] [blame] | 3792 | "error loading props for ino %llu (root %llu): %d", |
Filipe David Borba Manana | 6354192 | 2014-01-07 11:47:46 +0000 | [diff] [blame] | 3793 | btrfs_ino(inode), |
| 3794 | root->root_key.objectid, ret); |
| 3795 | } |
| 3796 | btrfs_free_path(path); |
| 3797 | |
Al Viro | 72c0490 | 2009-06-24 16:58:48 -0400 | [diff] [blame] | 3798 | if (!maybe_acls) |
| 3799 | cache_no_acl(inode); |
Chris Mason | 46a53cc | 2009-04-27 11:47:50 -0400 | [diff] [blame] | 3800 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3801 | switch (inode->i_mode & S_IFMT) { |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3802 | case S_IFREG: |
| 3803 | inode->i_mapping->a_ops = &btrfs_aops; |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 3804 | BTRFS_I(inode)->io_tree.ops = &btrfs_extent_io_ops; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3805 | inode->i_fop = &btrfs_file_operations; |
| 3806 | inode->i_op = &btrfs_file_inode_operations; |
| 3807 | break; |
| 3808 | case S_IFDIR: |
| 3809 | inode->i_fop = &btrfs_dir_file_operations; |
| 3810 | if (root == root->fs_info->tree_root) |
| 3811 | inode->i_op = &btrfs_dir_ro_inode_operations; |
| 3812 | else |
| 3813 | inode->i_op = &btrfs_dir_inode_operations; |
| 3814 | break; |
| 3815 | case S_IFLNK: |
| 3816 | inode->i_op = &btrfs_symlink_inode_operations; |
Al Viro | 21fc61c | 2015-11-17 01:07:57 -0500 | [diff] [blame] | 3817 | inode_nohighmem(inode); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3818 | inode->i_mapping->a_ops = &btrfs_symlink_aops; |
| 3819 | break; |
Josef Bacik | 618e21d | 2007-07-11 10:18:17 -0400 | [diff] [blame] | 3820 | default: |
Jim Owens | 0279b4c | 2009-02-04 09:29:13 -0500 | [diff] [blame] | 3821 | inode->i_op = &btrfs_special_inode_operations; |
Josef Bacik | 618e21d | 2007-07-11 10:18:17 -0400 | [diff] [blame] | 3822 | init_special_inode(inode, inode->i_mode, rdev); |
| 3823 | break; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3824 | } |
Christoph Hellwig | 6cbff00 | 2009-04-17 10:37:41 +0200 | [diff] [blame] | 3825 | |
| 3826 | btrfs_update_iflags(inode); |
Filipe Manana | 6771089 | 2016-06-06 11:51:25 +0100 | [diff] [blame] | 3827 | return 0; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3828 | |
| 3829 | make_bad: |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3830 | btrfs_free_path(path); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3831 | make_bad_inode(inode); |
Filipe Manana | 6771089 | 2016-06-06 11:51:25 +0100 | [diff] [blame] | 3832 | return ret; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3833 | } |
| 3834 | |
Chris Mason | d352ac6 | 2008-09-29 15:18:18 -0400 | [diff] [blame] | 3835 | /* |
| 3836 | * given a leaf and an inode, copy the inode fields into the leaf |
| 3837 | */ |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 3838 | static void fill_inode_item(struct btrfs_trans_handle *trans, |
| 3839 | struct extent_buffer *leaf, |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 3840 | struct btrfs_inode_item *item, |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3841 | struct inode *inode) |
| 3842 | { |
Liu Bo | 51fab69 | 2012-12-27 09:01:21 +0000 | [diff] [blame] | 3843 | struct btrfs_map_token token; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 3844 | |
Liu Bo | 51fab69 | 2012-12-27 09:01:21 +0000 | [diff] [blame] | 3845 | btrfs_init_map_token(&token); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 3846 | |
Liu Bo | 51fab69 | 2012-12-27 09:01:21 +0000 | [diff] [blame] | 3847 | btrfs_set_token_inode_uid(leaf, item, i_uid_read(inode), &token); |
| 3848 | btrfs_set_token_inode_gid(leaf, item, i_gid_read(inode), &token); |
| 3849 | btrfs_set_token_inode_size(leaf, item, BTRFS_I(inode)->disk_i_size, |
| 3850 | &token); |
| 3851 | btrfs_set_token_inode_mode(leaf, item, inode->i_mode, &token); |
| 3852 | btrfs_set_token_inode_nlink(leaf, item, inode->i_nlink, &token); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 3853 | |
David Sterba | a937b97 | 2014-12-12 17:39:12 +0100 | [diff] [blame] | 3854 | btrfs_set_token_timespec_sec(leaf, &item->atime, |
Liu Bo | 51fab69 | 2012-12-27 09:01:21 +0000 | [diff] [blame] | 3855 | inode->i_atime.tv_sec, &token); |
David Sterba | a937b97 | 2014-12-12 17:39:12 +0100 | [diff] [blame] | 3856 | btrfs_set_token_timespec_nsec(leaf, &item->atime, |
Liu Bo | 51fab69 | 2012-12-27 09:01:21 +0000 | [diff] [blame] | 3857 | inode->i_atime.tv_nsec, &token); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 3858 | |
David Sterba | a937b97 | 2014-12-12 17:39:12 +0100 | [diff] [blame] | 3859 | btrfs_set_token_timespec_sec(leaf, &item->mtime, |
Liu Bo | 51fab69 | 2012-12-27 09:01:21 +0000 | [diff] [blame] | 3860 | inode->i_mtime.tv_sec, &token); |
David Sterba | a937b97 | 2014-12-12 17:39:12 +0100 | [diff] [blame] | 3861 | btrfs_set_token_timespec_nsec(leaf, &item->mtime, |
Liu Bo | 51fab69 | 2012-12-27 09:01:21 +0000 | [diff] [blame] | 3862 | inode->i_mtime.tv_nsec, &token); |
| 3863 | |
David Sterba | a937b97 | 2014-12-12 17:39:12 +0100 | [diff] [blame] | 3864 | btrfs_set_token_timespec_sec(leaf, &item->ctime, |
Liu Bo | 51fab69 | 2012-12-27 09:01:21 +0000 | [diff] [blame] | 3865 | inode->i_ctime.tv_sec, &token); |
David Sterba | a937b97 | 2014-12-12 17:39:12 +0100 | [diff] [blame] | 3866 | btrfs_set_token_timespec_nsec(leaf, &item->ctime, |
Liu Bo | 51fab69 | 2012-12-27 09:01:21 +0000 | [diff] [blame] | 3867 | inode->i_ctime.tv_nsec, &token); |
| 3868 | |
chandan r | 9cc97d6 | 2012-07-04 12:48:07 +0530 | [diff] [blame] | 3869 | btrfs_set_token_timespec_sec(leaf, &item->otime, |
| 3870 | BTRFS_I(inode)->i_otime.tv_sec, &token); |
| 3871 | btrfs_set_token_timespec_nsec(leaf, &item->otime, |
| 3872 | BTRFS_I(inode)->i_otime.tv_nsec, &token); |
| 3873 | |
Liu Bo | 51fab69 | 2012-12-27 09:01:21 +0000 | [diff] [blame] | 3874 | btrfs_set_token_inode_nbytes(leaf, item, inode_get_bytes(inode), |
| 3875 | &token); |
| 3876 | btrfs_set_token_inode_generation(leaf, item, BTRFS_I(inode)->generation, |
| 3877 | &token); |
| 3878 | btrfs_set_token_inode_sequence(leaf, item, inode->i_version, &token); |
| 3879 | btrfs_set_token_inode_transid(leaf, item, trans->transid, &token); |
| 3880 | btrfs_set_token_inode_rdev(leaf, item, inode->i_rdev, &token); |
| 3881 | btrfs_set_token_inode_flags(leaf, item, BTRFS_I(inode)->flags, &token); |
| 3882 | btrfs_set_token_inode_block_group(leaf, item, 0, &token); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3883 | } |
| 3884 | |
Chris Mason | d352ac6 | 2008-09-29 15:18:18 -0400 | [diff] [blame] | 3885 | /* |
| 3886 | * copy everything in the in-memory inode into the btree. |
| 3887 | */ |
Chris Mason | 2115133 | 2011-11-10 20:39:08 -0500 | [diff] [blame] | 3888 | static noinline int btrfs_update_inode_item(struct btrfs_trans_handle *trans, |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 3889 | struct btrfs_root *root, struct inode *inode) |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3890 | { |
| 3891 | struct btrfs_inode_item *inode_item; |
| 3892 | struct btrfs_path *path; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 3893 | struct extent_buffer *leaf; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3894 | int ret; |
| 3895 | |
| 3896 | path = btrfs_alloc_path(); |
Miao Xie | 16cdcec | 2011-04-22 18:12:22 +0800 | [diff] [blame] | 3897 | if (!path) |
| 3898 | return -ENOMEM; |
| 3899 | |
Chris Mason | b947343 | 2009-03-13 11:00:37 -0400 | [diff] [blame] | 3900 | path->leave_spinning = 1; |
Miao Xie | 16cdcec | 2011-04-22 18:12:22 +0800 | [diff] [blame] | 3901 | ret = btrfs_lookup_inode(trans, root, path, &BTRFS_I(inode)->location, |
| 3902 | 1); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3903 | if (ret) { |
| 3904 | if (ret > 0) |
| 3905 | ret = -ENOENT; |
| 3906 | goto failed; |
| 3907 | } |
| 3908 | |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 3909 | leaf = path->nodes[0]; |
| 3910 | inode_item = btrfs_item_ptr(leaf, path->slots[0], |
Miao Xie | 16cdcec | 2011-04-22 18:12:22 +0800 | [diff] [blame] | 3911 | struct btrfs_inode_item); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3912 | |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 3913 | fill_inode_item(trans, leaf, inode_item, inode); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 3914 | btrfs_mark_buffer_dirty(leaf); |
Josef Bacik | 15ee9bc | 2007-08-10 16:22:09 -0400 | [diff] [blame] | 3915 | btrfs_set_inode_last_trans(trans, inode); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3916 | ret = 0; |
| 3917 | failed: |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3918 | btrfs_free_path(path); |
| 3919 | return ret; |
| 3920 | } |
| 3921 | |
Chris Mason | d352ac6 | 2008-09-29 15:18:18 -0400 | [diff] [blame] | 3922 | /* |
Chris Mason | 2115133 | 2011-11-10 20:39:08 -0500 | [diff] [blame] | 3923 | * copy everything in the in-memory inode into the btree. |
| 3924 | */ |
| 3925 | noinline int btrfs_update_inode(struct btrfs_trans_handle *trans, |
| 3926 | struct btrfs_root *root, struct inode *inode) |
| 3927 | { |
| 3928 | int ret; |
| 3929 | |
| 3930 | /* |
| 3931 | * If the inode is a free space inode, we can deadlock during commit |
| 3932 | * if we put it into the delayed code. |
| 3933 | * |
| 3934 | * The data relocation inode should also be directly updated |
| 3935 | * without delay |
| 3936 | */ |
Liu Bo | 83eea1f | 2012-07-10 05:28:39 -0600 | [diff] [blame] | 3937 | if (!btrfs_is_free_space_inode(inode) |
Josef Bacik | 1d52c78 | 2014-09-18 11:30:44 -0400 | [diff] [blame] | 3938 | && root->root_key.objectid != BTRFS_DATA_RELOC_TREE_OBJECTID |
| 3939 | && !root->fs_info->log_root_recovering) { |
Alexander Block | 8ea05e3 | 2012-07-25 17:35:53 +0200 | [diff] [blame] | 3940 | btrfs_update_root_times(trans, root); |
| 3941 | |
Chris Mason | 2115133 | 2011-11-10 20:39:08 -0500 | [diff] [blame] | 3942 | ret = btrfs_delayed_update_inode(trans, root, inode); |
| 3943 | if (!ret) |
| 3944 | btrfs_set_inode_last_trans(trans, inode); |
| 3945 | return ret; |
| 3946 | } |
| 3947 | |
| 3948 | return btrfs_update_inode_item(trans, root, inode); |
| 3949 | } |
| 3950 | |
Josef Bacik | be6aef6 | 2012-10-22 15:43:12 -0400 | [diff] [blame] | 3951 | noinline int btrfs_update_inode_fallback(struct btrfs_trans_handle *trans, |
| 3952 | struct btrfs_root *root, |
| 3953 | struct inode *inode) |
Chris Mason | 2115133 | 2011-11-10 20:39:08 -0500 | [diff] [blame] | 3954 | { |
| 3955 | int ret; |
| 3956 | |
| 3957 | ret = btrfs_update_inode(trans, root, inode); |
| 3958 | if (ret == -ENOSPC) |
| 3959 | return btrfs_update_inode_item(trans, root, inode); |
| 3960 | return ret; |
| 3961 | } |
| 3962 | |
| 3963 | /* |
Chris Mason | d352ac6 | 2008-09-29 15:18:18 -0400 | [diff] [blame] | 3964 | * unlink helper that gets used here in inode.c and in the tree logging |
| 3965 | * recovery code. It remove a link in a directory with a given name, and |
| 3966 | * also drops the back refs in the inode to the directory |
| 3967 | */ |
Al Viro | 9298679 | 2011-03-04 17:14:37 +0000 | [diff] [blame] | 3968 | static int __btrfs_unlink_inode(struct btrfs_trans_handle *trans, |
| 3969 | struct btrfs_root *root, |
| 3970 | struct inode *dir, struct inode *inode, |
| 3971 | const char *name, int name_len) |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3972 | { |
| 3973 | struct btrfs_path *path; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3974 | int ret = 0; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 3975 | struct extent_buffer *leaf; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3976 | struct btrfs_dir_item *di; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 3977 | struct btrfs_key key; |
Josef Bacik | aec7477 | 2008-07-24 12:12:38 -0400 | [diff] [blame] | 3978 | u64 index; |
Li Zefan | 33345d01 | 2011-04-20 10:31:50 +0800 | [diff] [blame] | 3979 | u64 ino = btrfs_ino(inode); |
| 3980 | u64 dir_ino = btrfs_ino(dir); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3981 | |
| 3982 | path = btrfs_alloc_path(); |
Chris Mason | 54aa1f4 | 2007-06-22 14:16:25 -0400 | [diff] [blame] | 3983 | if (!path) { |
| 3984 | ret = -ENOMEM; |
Tsutomu Itoh | 554233a | 2011-02-03 03:16:25 +0000 | [diff] [blame] | 3985 | goto out; |
Chris Mason | 54aa1f4 | 2007-06-22 14:16:25 -0400 | [diff] [blame] | 3986 | } |
| 3987 | |
Chris Mason | b947343 | 2009-03-13 11:00:37 -0400 | [diff] [blame] | 3988 | path->leave_spinning = 1; |
Li Zefan | 33345d01 | 2011-04-20 10:31:50 +0800 | [diff] [blame] | 3989 | di = btrfs_lookup_dir_item(trans, root, path, dir_ino, |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3990 | name, name_len, -1); |
| 3991 | if (IS_ERR(di)) { |
| 3992 | ret = PTR_ERR(di); |
| 3993 | goto err; |
| 3994 | } |
| 3995 | if (!di) { |
| 3996 | ret = -ENOENT; |
| 3997 | goto err; |
| 3998 | } |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 3999 | leaf = path->nodes[0]; |
| 4000 | btrfs_dir_item_key_to_cpu(leaf, di, &key); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4001 | ret = btrfs_delete_one_dir_name(trans, root, path, di); |
Chris Mason | 54aa1f4 | 2007-06-22 14:16:25 -0400 | [diff] [blame] | 4002 | if (ret) |
| 4003 | goto err; |
David Sterba | b3b4aa7 | 2011-04-21 01:20:15 +0200 | [diff] [blame] | 4004 | btrfs_release_path(path); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4005 | |
Miao Xie | 67de117 | 2013-12-26 13:07:06 +0800 | [diff] [blame] | 4006 | /* |
| 4007 | * If we don't have dir index, we have to get it by looking up |
| 4008 | * the inode ref, since we get the inode ref, remove it directly, |
| 4009 | * it is unnecessary to do delayed deletion. |
| 4010 | * |
| 4011 | * But if we have dir index, needn't search inode ref to get it. |
| 4012 | * Since the inode ref is close to the inode item, it is better |
| 4013 | * that we delay to delete it, and just do this deletion when |
| 4014 | * we update the inode item. |
| 4015 | */ |
| 4016 | if (BTRFS_I(inode)->dir_index) { |
| 4017 | ret = btrfs_delayed_delete_inode_ref(inode); |
| 4018 | if (!ret) { |
| 4019 | index = BTRFS_I(inode)->dir_index; |
| 4020 | goto skip_backref; |
| 4021 | } |
| 4022 | } |
| 4023 | |
Li Zefan | 33345d01 | 2011-04-20 10:31:50 +0800 | [diff] [blame] | 4024 | ret = btrfs_del_inode_ref(trans, root, name, name_len, ino, |
| 4025 | dir_ino, &index); |
Josef Bacik | aec7477 | 2008-07-24 12:12:38 -0400 | [diff] [blame] | 4026 | if (ret) { |
Simon Kirby | c2cf52e | 2013-03-19 22:41:23 +0000 | [diff] [blame] | 4027 | btrfs_info(root->fs_info, |
| 4028 | "failed to delete reference to %.*s, inode %llu parent %llu", |
Geert Uytterhoeven | c1c9ff7 | 2013-08-20 13:20:07 +0200 | [diff] [blame] | 4029 | name_len, name, ino, dir_ino); |
Jeff Mahoney | 6664283 | 2016-06-10 18:19:25 -0400 | [diff] [blame] | 4030 | btrfs_abort_transaction(trans, ret); |
Josef Bacik | aec7477 | 2008-07-24 12:12:38 -0400 | [diff] [blame] | 4031 | goto err; |
| 4032 | } |
Miao Xie | 67de117 | 2013-12-26 13:07:06 +0800 | [diff] [blame] | 4033 | skip_backref: |
Miao Xie | 16cdcec | 2011-04-22 18:12:22 +0800 | [diff] [blame] | 4034 | ret = btrfs_delete_delayed_dir_index(trans, root, dir, index); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 4035 | if (ret) { |
Jeff Mahoney | 6664283 | 2016-06-10 18:19:25 -0400 | [diff] [blame] | 4036 | btrfs_abort_transaction(trans, ret); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4037 | goto err; |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 4038 | } |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4039 | |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 4040 | ret = btrfs_del_inode_ref_in_log(trans, root, name, name_len, |
Li Zefan | 33345d01 | 2011-04-20 10:31:50 +0800 | [diff] [blame] | 4041 | inode, dir_ino); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 4042 | if (ret != 0 && ret != -ENOENT) { |
Jeff Mahoney | 6664283 | 2016-06-10 18:19:25 -0400 | [diff] [blame] | 4043 | btrfs_abort_transaction(trans, ret); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 4044 | goto err; |
| 4045 | } |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 4046 | |
| 4047 | ret = btrfs_del_dir_entries_in_log(trans, root, name, name_len, |
| 4048 | dir, index); |
Chris Mason | 6418c96 | 2010-10-30 07:34:24 -0400 | [diff] [blame] | 4049 | if (ret == -ENOENT) |
| 4050 | ret = 0; |
Zach Brown | d4e3991 | 2013-04-02 21:02:16 +0000 | [diff] [blame] | 4051 | else 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 | err: |
| 4054 | btrfs_free_path(path); |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 4055 | if (ret) |
| 4056 | goto out; |
| 4057 | |
| 4058 | btrfs_i_size_write(dir, dir->i_size - name_len * 2); |
Josef Bacik | 0c4d2d9 | 2012-04-05 15:03:02 -0400 | [diff] [blame] | 4059 | inode_inc_iversion(inode); |
| 4060 | inode_inc_iversion(dir); |
Deepa Dinamani | 04b285f | 2016-02-06 23:57:21 -0800 | [diff] [blame] | 4061 | inode->i_ctime = dir->i_mtime = |
| 4062 | dir->i_ctime = current_fs_time(inode->i_sb); |
Tsutomu Itoh | b995929 | 2012-06-25 21:25:22 -0600 | [diff] [blame] | 4063 | ret = btrfs_update_inode(trans, root, dir); |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 4064 | out: |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4065 | return ret; |
| 4066 | } |
| 4067 | |
Al Viro | 9298679 | 2011-03-04 17:14:37 +0000 | [diff] [blame] | 4068 | int btrfs_unlink_inode(struct btrfs_trans_handle *trans, |
| 4069 | struct btrfs_root *root, |
| 4070 | struct inode *dir, struct inode *inode, |
| 4071 | const char *name, int name_len) |
| 4072 | { |
| 4073 | int ret; |
| 4074 | ret = __btrfs_unlink_inode(trans, root, dir, inode, name, name_len); |
| 4075 | if (!ret) { |
Zach Brown | 8b558c5 | 2013-10-16 12:10:34 -0700 | [diff] [blame] | 4076 | drop_nlink(inode); |
Al Viro | 9298679 | 2011-03-04 17:14:37 +0000 | [diff] [blame] | 4077 | ret = btrfs_update_inode(trans, root, inode); |
| 4078 | } |
| 4079 | return ret; |
| 4080 | } |
Yan, Zheng | a22285a | 2010-05-16 10:48:46 -0400 | [diff] [blame] | 4081 | |
| 4082 | /* |
| 4083 | * helper to start transaction for unlink and rmdir. |
| 4084 | * |
Josef Bacik | d52be81 | 2013-05-29 14:54:47 -0400 | [diff] [blame] | 4085 | * unlink and rmdir are special in btrfs, they do not always free space, so |
| 4086 | * if we cannot make our reservations the normal way try and see if there is |
| 4087 | * plenty of slack room in the global reserve to migrate, otherwise we cannot |
| 4088 | * allow the unlink to occur. |
Yan, Zheng | a22285a | 2010-05-16 10:48:46 -0400 | [diff] [blame] | 4089 | */ |
Josef Bacik | d52be81 | 2013-05-29 14:54:47 -0400 | [diff] [blame] | 4090 | static struct btrfs_trans_handle *__unlink_start_trans(struct inode *dir) |
Yan, Zheng | a22285a | 2010-05-16 10:48:46 -0400 | [diff] [blame] | 4091 | { |
Yan, Zheng | a22285a | 2010-05-16 10:48:46 -0400 | [diff] [blame] | 4092 | struct btrfs_root *root = BTRFS_I(dir)->root; |
Yan, Zheng | a22285a | 2010-05-16 10:48:46 -0400 | [diff] [blame] | 4093 | |
Josef Bacik | e70bea5 | 2011-10-11 14:18:24 -0400 | [diff] [blame] | 4094 | /* |
| 4095 | * 1 for the possible orphan item |
| 4096 | * 1 for the dir item |
| 4097 | * 1 for the dir index |
| 4098 | * 1 for the inode ref |
Josef Bacik | e70bea5 | 2011-10-11 14:18:24 -0400 | [diff] [blame] | 4099 | * 1 for the inode |
| 4100 | */ |
Filipe Manana | 8eab77f | 2015-11-13 23:57:16 +0000 | [diff] [blame] | 4101 | return btrfs_start_transaction_fallback_global_rsv(root, 5, 5); |
Yan, Zheng | a22285a | 2010-05-16 10:48:46 -0400 | [diff] [blame] | 4102 | } |
| 4103 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4104 | static int btrfs_unlink(struct inode *dir, struct dentry *dentry) |
| 4105 | { |
Yan, Zheng | a22285a | 2010-05-16 10:48:46 -0400 | [diff] [blame] | 4106 | struct btrfs_root *root = BTRFS_I(dir)->root; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4107 | struct btrfs_trans_handle *trans; |
David Howells | 2b0143b | 2015-03-17 22:25:59 +0000 | [diff] [blame] | 4108 | struct inode *inode = d_inode(dentry); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4109 | int ret; |
| 4110 | |
Josef Bacik | d52be81 | 2013-05-29 14:54:47 -0400 | [diff] [blame] | 4111 | trans = __unlink_start_trans(dir); |
Yan, Zheng | a22285a | 2010-05-16 10:48:46 -0400 | [diff] [blame] | 4112 | if (IS_ERR(trans)) |
Josef Bacik | 5df6a9f | 2009-11-10 21:23:48 -0500 | [diff] [blame] | 4113 | return PTR_ERR(trans); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 4114 | |
David Howells | 2b0143b | 2015-03-17 22:25:59 +0000 | [diff] [blame] | 4115 | btrfs_record_unlink_dir(trans, dir, d_inode(dentry), 0); |
Chris Mason | 12fcfd2 | 2009-03-24 10:24:20 -0400 | [diff] [blame] | 4116 | |
David Howells | 2b0143b | 2015-03-17 22:25:59 +0000 | [diff] [blame] | 4117 | ret = btrfs_unlink_inode(trans, root, dir, d_inode(dentry), |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 4118 | dentry->d_name.name, dentry->d_name.len); |
Tsutomu Itoh | b532402 | 2011-07-19 07:27:20 +0000 | [diff] [blame] | 4119 | if (ret) |
| 4120 | goto out; |
Josef Bacik | 7b12876 | 2008-07-24 12:17:14 -0400 | [diff] [blame] | 4121 | |
Yan, Zheng | a22285a | 2010-05-16 10:48:46 -0400 | [diff] [blame] | 4122 | if (inode->i_nlink == 0) { |
Josef Bacik | 7b12876 | 2008-07-24 12:17:14 -0400 | [diff] [blame] | 4123 | ret = btrfs_orphan_add(trans, inode); |
Tsutomu Itoh | b532402 | 2011-07-19 07:27:20 +0000 | [diff] [blame] | 4124 | if (ret) |
| 4125 | goto out; |
Yan, Zheng | a22285a | 2010-05-16 10:48:46 -0400 | [diff] [blame] | 4126 | } |
Josef Bacik | 7b12876 | 2008-07-24 12:17:14 -0400 | [diff] [blame] | 4127 | |
Tsutomu Itoh | b532402 | 2011-07-19 07:27:20 +0000 | [diff] [blame] | 4128 | out: |
Josef Bacik | d52be81 | 2013-05-29 14:54:47 -0400 | [diff] [blame] | 4129 | btrfs_end_transaction(trans, root); |
Liu Bo | b53d3f5 | 2012-11-14 14:34:34 +0000 | [diff] [blame] | 4130 | btrfs_btree_balance_dirty(root); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4131 | return ret; |
| 4132 | } |
| 4133 | |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 4134 | int btrfs_unlink_subvol(struct btrfs_trans_handle *trans, |
| 4135 | struct btrfs_root *root, |
| 4136 | struct inode *dir, u64 objectid, |
| 4137 | const char *name, int name_len) |
| 4138 | { |
| 4139 | struct btrfs_path *path; |
| 4140 | struct extent_buffer *leaf; |
| 4141 | struct btrfs_dir_item *di; |
| 4142 | struct btrfs_key key; |
| 4143 | u64 index; |
| 4144 | int ret; |
Li Zefan | 33345d01 | 2011-04-20 10:31:50 +0800 | [diff] [blame] | 4145 | u64 dir_ino = btrfs_ino(dir); |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 4146 | |
| 4147 | path = btrfs_alloc_path(); |
| 4148 | if (!path) |
| 4149 | return -ENOMEM; |
| 4150 | |
Li Zefan | 33345d01 | 2011-04-20 10:31:50 +0800 | [diff] [blame] | 4151 | di = btrfs_lookup_dir_item(trans, root, path, dir_ino, |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 4152 | name, name_len, -1); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 4153 | if (IS_ERR_OR_NULL(di)) { |
| 4154 | if (!di) |
| 4155 | ret = -ENOENT; |
| 4156 | else |
| 4157 | ret = PTR_ERR(di); |
| 4158 | goto out; |
| 4159 | } |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 4160 | |
| 4161 | leaf = path->nodes[0]; |
| 4162 | btrfs_dir_item_key_to_cpu(leaf, di, &key); |
| 4163 | WARN_ON(key.type != BTRFS_ROOT_ITEM_KEY || key.objectid != objectid); |
| 4164 | ret = btrfs_delete_one_dir_name(trans, root, path, di); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 4165 | if (ret) { |
Jeff Mahoney | 6664283 | 2016-06-10 18:19:25 -0400 | [diff] [blame] | 4166 | btrfs_abort_transaction(trans, ret); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 4167 | goto out; |
| 4168 | } |
David Sterba | b3b4aa7 | 2011-04-21 01:20:15 +0200 | [diff] [blame] | 4169 | btrfs_release_path(path); |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 4170 | |
| 4171 | ret = btrfs_del_root_ref(trans, root->fs_info->tree_root, |
| 4172 | objectid, root->root_key.objectid, |
Li Zefan | 33345d01 | 2011-04-20 10:31:50 +0800 | [diff] [blame] | 4173 | dir_ino, &index, name, name_len); |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 4174 | if (ret < 0) { |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 4175 | if (ret != -ENOENT) { |
Jeff Mahoney | 6664283 | 2016-06-10 18:19:25 -0400 | [diff] [blame] | 4176 | btrfs_abort_transaction(trans, ret); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 4177 | goto out; |
| 4178 | } |
Li Zefan | 33345d01 | 2011-04-20 10:31:50 +0800 | [diff] [blame] | 4179 | di = btrfs_search_dir_index_item(root, path, dir_ino, |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 4180 | name, name_len); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 4181 | if (IS_ERR_OR_NULL(di)) { |
| 4182 | if (!di) |
| 4183 | ret = -ENOENT; |
| 4184 | else |
| 4185 | ret = PTR_ERR(di); |
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 | } |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 4189 | |
| 4190 | leaf = path->nodes[0]; |
| 4191 | btrfs_item_key_to_cpu(leaf, &key, path->slots[0]); |
David Sterba | b3b4aa7 | 2011-04-21 01:20:15 +0200 | [diff] [blame] | 4192 | btrfs_release_path(path); |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 4193 | index = key.offset; |
| 4194 | } |
David Sterba | b3b4aa7 | 2011-04-21 01:20:15 +0200 | [diff] [blame] | 4195 | btrfs_release_path(path); |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 4196 | |
Miao Xie | 16cdcec | 2011-04-22 18:12:22 +0800 | [diff] [blame] | 4197 | ret = btrfs_delete_delayed_dir_index(trans, root, dir, index); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 4198 | if (ret) { |
Jeff Mahoney | 6664283 | 2016-06-10 18:19:25 -0400 | [diff] [blame] | 4199 | btrfs_abort_transaction(trans, ret); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 4200 | goto out; |
| 4201 | } |
Miao Xie | 16cdcec | 2011-04-22 18:12:22 +0800 | [diff] [blame] | 4202 | |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 4203 | btrfs_i_size_write(dir, dir->i_size - name_len * 2); |
Josef Bacik | 0c4d2d9 | 2012-04-05 15:03:02 -0400 | [diff] [blame] | 4204 | inode_inc_iversion(dir); |
Deepa Dinamani | 04b285f | 2016-02-06 23:57:21 -0800 | [diff] [blame] | 4205 | dir->i_mtime = dir->i_ctime = current_fs_time(dir->i_sb); |
Josef Bacik | 5a24e84 | 2012-08-08 10:12:59 -0600 | [diff] [blame] | 4206 | ret = btrfs_update_inode_fallback(trans, root, dir); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 4207 | if (ret) |
Jeff Mahoney | 6664283 | 2016-06-10 18:19:25 -0400 | [diff] [blame] | 4208 | btrfs_abort_transaction(trans, ret); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 4209 | out: |
Josef Bacik | 71d7aed | 2011-06-14 14:24:32 -0400 | [diff] [blame] | 4210 | btrfs_free_path(path); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 4211 | return ret; |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 4212 | } |
| 4213 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4214 | static int btrfs_rmdir(struct inode *dir, struct dentry *dentry) |
| 4215 | { |
David Howells | 2b0143b | 2015-03-17 22:25:59 +0000 | [diff] [blame] | 4216 | struct inode *inode = d_inode(dentry); |
Chris Mason | 1832a6d | 2007-12-21 16:27:21 -0500 | [diff] [blame] | 4217 | int err = 0; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4218 | struct btrfs_root *root = BTRFS_I(dir)->root; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4219 | struct btrfs_trans_handle *trans; |
Filipe Manana | 44f714d | 2016-06-06 16:11:13 +0100 | [diff] [blame] | 4220 | u64 last_unlink_trans; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4221 | |
David Sterba | b3ae244 | 2012-09-13 16:04:34 -0600 | [diff] [blame] | 4222 | if (inode->i_size > BTRFS_EMPTY_DIR_SIZE) |
Yan | 134d451 | 2007-10-25 15:49:25 -0400 | [diff] [blame] | 4223 | return -ENOTEMPTY; |
David Sterba | b3ae244 | 2012-09-13 16:04:34 -0600 | [diff] [blame] | 4224 | if (btrfs_ino(inode) == BTRFS_FIRST_FREE_OBJECTID) |
| 4225 | return -EPERM; |
Yan | 134d451 | 2007-10-25 15:49:25 -0400 | [diff] [blame] | 4226 | |
Josef Bacik | d52be81 | 2013-05-29 14:54:47 -0400 | [diff] [blame] | 4227 | trans = __unlink_start_trans(dir); |
Yan, Zheng | a22285a | 2010-05-16 10:48:46 -0400 | [diff] [blame] | 4228 | if (IS_ERR(trans)) |
Josef Bacik | 5df6a9f | 2009-11-10 21:23:48 -0500 | [diff] [blame] | 4229 | return PTR_ERR(trans); |
Josef Bacik | 5df6a9f | 2009-11-10 21:23:48 -0500 | [diff] [blame] | 4230 | |
Li Zefan | 33345d01 | 2011-04-20 10:31:50 +0800 | [diff] [blame] | 4231 | if (unlikely(btrfs_ino(inode) == BTRFS_EMPTY_SUBVOL_DIR_OBJECTID)) { |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 4232 | err = btrfs_unlink_subvol(trans, root, dir, |
| 4233 | BTRFS_I(inode)->location.objectid, |
| 4234 | dentry->d_name.name, |
| 4235 | dentry->d_name.len); |
| 4236 | goto out; |
| 4237 | } |
| 4238 | |
Josef Bacik | 7b12876 | 2008-07-24 12:17:14 -0400 | [diff] [blame] | 4239 | err = btrfs_orphan_add(trans, inode); |
| 4240 | if (err) |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 4241 | goto out; |
Josef Bacik | 7b12876 | 2008-07-24 12:17:14 -0400 | [diff] [blame] | 4242 | |
Filipe Manana | 44f714d | 2016-06-06 16:11:13 +0100 | [diff] [blame] | 4243 | last_unlink_trans = BTRFS_I(inode)->last_unlink_trans; |
| 4244 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4245 | /* now the directory is empty */ |
David Howells | 2b0143b | 2015-03-17 22:25:59 +0000 | [diff] [blame] | 4246 | err = btrfs_unlink_inode(trans, root, dir, d_inode(dentry), |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 4247 | dentry->d_name.name, dentry->d_name.len); |
Filipe Manana | 44f714d | 2016-06-06 16:11:13 +0100 | [diff] [blame] | 4248 | if (!err) { |
Chris Mason | dbe674a | 2008-07-17 12:54:05 -0400 | [diff] [blame] | 4249 | btrfs_i_size_write(inode, 0); |
Filipe Manana | 44f714d | 2016-06-06 16:11:13 +0100 | [diff] [blame] | 4250 | /* |
| 4251 | * Propagate the last_unlink_trans value of the deleted dir to |
| 4252 | * its parent directory. This is to prevent an unrecoverable |
| 4253 | * log tree in the case we do something like this: |
| 4254 | * 1) create dir foo |
| 4255 | * 2) create snapshot under dir foo |
| 4256 | * 3) delete the snapshot |
| 4257 | * 4) rmdir foo |
| 4258 | * 5) mkdir foo |
| 4259 | * 6) fsync foo or some file inside foo |
| 4260 | */ |
| 4261 | if (last_unlink_trans >= trans->transid) |
| 4262 | BTRFS_I(dir)->last_unlink_trans = last_unlink_trans; |
| 4263 | } |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 4264 | out: |
Josef Bacik | d52be81 | 2013-05-29 14:54:47 -0400 | [diff] [blame] | 4265 | btrfs_end_transaction(trans, root); |
Liu Bo | b53d3f5 | 2012-11-14 14:34:34 +0000 | [diff] [blame] | 4266 | btrfs_btree_balance_dirty(root); |
Chris Mason | 3954401 | 2007-12-12 14:38:19 -0500 | [diff] [blame] | 4267 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4268 | return err; |
| 4269 | } |
| 4270 | |
Chris Mason | 28f75a0 | 2015-02-04 06:59:29 -0800 | [diff] [blame] | 4271 | static int truncate_space_check(struct btrfs_trans_handle *trans, |
| 4272 | struct btrfs_root *root, |
| 4273 | u64 bytes_deleted) |
| 4274 | { |
| 4275 | int ret; |
| 4276 | |
Josef Bacik | dc95f7b | 2016-01-13 11:48:06 -0500 | [diff] [blame] | 4277 | /* |
| 4278 | * This is only used to apply pressure to the enospc system, we don't |
| 4279 | * intend to use this reservation at all. |
| 4280 | */ |
Chris Mason | 28f75a0 | 2015-02-04 06:59:29 -0800 | [diff] [blame] | 4281 | bytes_deleted = btrfs_csum_bytes_to_leaves(root, bytes_deleted); |
Josef Bacik | dc95f7b | 2016-01-13 11:48:06 -0500 | [diff] [blame] | 4282 | bytes_deleted *= root->nodesize; |
Chris Mason | 28f75a0 | 2015-02-04 06:59:29 -0800 | [diff] [blame] | 4283 | ret = btrfs_block_rsv_add(root, &root->fs_info->trans_block_rsv, |
| 4284 | bytes_deleted, BTRFS_RESERVE_NO_FLUSH); |
Josef Bacik | dc95f7b | 2016-01-13 11:48:06 -0500 | [diff] [blame] | 4285 | if (!ret) { |
| 4286 | trace_btrfs_space_reservation(root->fs_info, "transaction", |
| 4287 | trans->transid, |
| 4288 | bytes_deleted, 1); |
Chris Mason | 28f75a0 | 2015-02-04 06:59:29 -0800 | [diff] [blame] | 4289 | trans->bytes_reserved += bytes_deleted; |
Josef Bacik | dc95f7b | 2016-01-13 11:48:06 -0500 | [diff] [blame] | 4290 | } |
Chris Mason | 28f75a0 | 2015-02-04 06:59:29 -0800 | [diff] [blame] | 4291 | return ret; |
| 4292 | |
| 4293 | } |
| 4294 | |
Filipe Manana | 0305cd5 | 2015-10-16 12:34:25 +0100 | [diff] [blame] | 4295 | static int truncate_inline_extent(struct inode *inode, |
| 4296 | struct btrfs_path *path, |
| 4297 | struct btrfs_key *found_key, |
| 4298 | const u64 item_end, |
| 4299 | const u64 new_size) |
| 4300 | { |
| 4301 | struct extent_buffer *leaf = path->nodes[0]; |
| 4302 | int slot = path->slots[0]; |
| 4303 | struct btrfs_file_extent_item *fi; |
| 4304 | u32 size = (u32)(new_size - found_key->offset); |
| 4305 | struct btrfs_root *root = BTRFS_I(inode)->root; |
| 4306 | |
| 4307 | fi = btrfs_item_ptr(leaf, slot, struct btrfs_file_extent_item); |
| 4308 | |
| 4309 | if (btrfs_file_extent_compression(leaf, fi) != BTRFS_COMPRESS_NONE) { |
| 4310 | loff_t offset = new_size; |
Kirill A. Shutemov | 09cbfea | 2016-04-01 15:29:47 +0300 | [diff] [blame] | 4311 | loff_t page_end = ALIGN(offset, PAGE_SIZE); |
Filipe Manana | 0305cd5 | 2015-10-16 12:34:25 +0100 | [diff] [blame] | 4312 | |
| 4313 | /* |
| 4314 | * Zero out the remaining of the last page of our inline extent, |
| 4315 | * instead of directly truncating our inline extent here - that |
| 4316 | * would be much more complex (decompressing all the data, then |
| 4317 | * compressing the truncated data, which might be bigger than |
| 4318 | * the size of the inline extent, resize the extent, etc). |
| 4319 | * We release the path because to get the page we might need to |
| 4320 | * read the extent item from disk (data not in the page cache). |
| 4321 | */ |
| 4322 | btrfs_release_path(path); |
Chandan Rajendra | 9703fef | 2016-01-21 15:55:56 +0530 | [diff] [blame] | 4323 | return btrfs_truncate_block(inode, offset, page_end - offset, |
| 4324 | 0); |
Filipe Manana | 0305cd5 | 2015-10-16 12:34:25 +0100 | [diff] [blame] | 4325 | } |
| 4326 | |
| 4327 | btrfs_set_file_extent_ram_bytes(leaf, fi, size); |
| 4328 | size = btrfs_file_extent_calc_inline_size(size); |
| 4329 | btrfs_truncate_item(root, path, size, 1); |
| 4330 | |
| 4331 | if (test_bit(BTRFS_ROOT_REF_COWS, &root->state)) |
| 4332 | inode_sub_bytes(inode, item_end + 1 - new_size); |
| 4333 | |
| 4334 | return 0; |
| 4335 | } |
| 4336 | |
Chris Mason | 323ac95 | 2008-10-01 19:05:46 -0400 | [diff] [blame] | 4337 | /* |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4338 | * this can truncate away extent items, csum items and directory items. |
| 4339 | * 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] | 4340 | * any higher than new_size |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4341 | * |
| 4342 | * csum items that cross the new i_size are truncated to the new size |
| 4343 | * as well. |
Josef Bacik | 7b12876 | 2008-07-24 12:17:14 -0400 | [diff] [blame] | 4344 | * |
| 4345 | * min_type is the minimum key type to truncate down to. If set to 0, this |
| 4346 | * 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] | 4347 | */ |
Yan, Zheng | 8082510 | 2009-11-12 09:35:36 +0000 | [diff] [blame] | 4348 | int btrfs_truncate_inode_items(struct btrfs_trans_handle *trans, |
| 4349 | struct btrfs_root *root, |
| 4350 | struct inode *inode, |
| 4351 | u64 new_size, u32 min_type) |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4352 | { |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4353 | struct btrfs_path *path; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 4354 | struct extent_buffer *leaf; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4355 | struct btrfs_file_extent_item *fi; |
Yan, Zheng | 8082510 | 2009-11-12 09:35:36 +0000 | [diff] [blame] | 4356 | struct btrfs_key key; |
| 4357 | struct btrfs_key found_key; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4358 | u64 extent_start = 0; |
Chris Mason | db94535 | 2007-10-15 16:15:53 -0400 | [diff] [blame] | 4359 | u64 extent_num_bytes = 0; |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 4360 | u64 extent_offset = 0; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4361 | u64 item_end = 0; |
Filipe Manana | c1aa457 | 2015-06-20 18:20:09 +0100 | [diff] [blame] | 4362 | u64 last_size = new_size; |
Yan, Zheng | 8082510 | 2009-11-12 09:35:36 +0000 | [diff] [blame] | 4363 | u32 found_type = (u8)-1; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4364 | int found_extent; |
| 4365 | int del_item; |
Chris Mason | 85e21ba | 2008-01-29 15:11:36 -0500 | [diff] [blame] | 4366 | int pending_del_nr = 0; |
| 4367 | int pending_del_slot = 0; |
Chris Mason | 179e29e | 2007-11-01 11:28:41 -0400 | [diff] [blame] | 4368 | int extent_type = -1; |
Yan, Zheng | 8082510 | 2009-11-12 09:35:36 +0000 | [diff] [blame] | 4369 | int ret; |
| 4370 | int err = 0; |
Li Zefan | 33345d01 | 2011-04-20 10:31:50 +0800 | [diff] [blame] | 4371 | u64 ino = btrfs_ino(inode); |
Chris Mason | 28ed134 | 2014-12-17 09:41:04 -0800 | [diff] [blame] | 4372 | u64 bytes_deleted = 0; |
Josef Bacik | 1262133 | 2015-02-03 07:50:16 -0800 | [diff] [blame] | 4373 | bool be_nice = 0; |
| 4374 | bool should_throttle = 0; |
Chris Mason | 28f75a0 | 2015-02-04 06:59:29 -0800 | [diff] [blame] | 4375 | bool should_end = 0; |
Yan, Zheng | 8082510 | 2009-11-12 09:35:36 +0000 | [diff] [blame] | 4376 | |
| 4377 | BUG_ON(new_size > 0 && min_type != BTRFS_EXTENT_DATA_KEY); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4378 | |
Chris Mason | 28ed134 | 2014-12-17 09:41:04 -0800 | [diff] [blame] | 4379 | /* |
| 4380 | * for non-free space inodes and ref cows, we want to back off from |
| 4381 | * time to time |
| 4382 | */ |
| 4383 | if (!btrfs_is_free_space_inode(inode) && |
| 4384 | test_bit(BTRFS_ROOT_REF_COWS, &root->state)) |
| 4385 | be_nice = 1; |
| 4386 | |
Mark Fasheh | 0eb0e19 | 2011-07-12 16:44:10 -0700 | [diff] [blame] | 4387 | path = btrfs_alloc_path(); |
| 4388 | if (!path) |
| 4389 | return -ENOMEM; |
David Sterba | e4058b5 | 2015-11-27 16:31:35 +0100 | [diff] [blame] | 4390 | path->reada = READA_BACK; |
Mark Fasheh | 0eb0e19 | 2011-07-12 16:44:10 -0700 | [diff] [blame] | 4391 | |
Josef Bacik | 5dc562c | 2012-08-17 13:14:17 -0400 | [diff] [blame] | 4392 | /* |
| 4393 | * We want to drop from the next block forward in case this new size is |
| 4394 | * not block aligned since we will be keeping the last block of the |
| 4395 | * extent just the way it is. |
| 4396 | */ |
Miao Xie | 27cdeb7 | 2014-04-02 19:51:05 +0800 | [diff] [blame] | 4397 | if (test_bit(BTRFS_ROOT_REF_COWS, &root->state) || |
| 4398 | root == root->fs_info->tree_root) |
Qu Wenruo | fda2832 | 2013-02-26 08:10:22 +0000 | [diff] [blame] | 4399 | btrfs_drop_extent_cache(inode, ALIGN(new_size, |
| 4400 | root->sectorsize), (u64)-1, 0); |
Yan, Zheng | 8082510 | 2009-11-12 09:35:36 +0000 | [diff] [blame] | 4401 | |
Miao Xie | 16cdcec | 2011-04-22 18:12:22 +0800 | [diff] [blame] | 4402 | /* |
| 4403 | * This function is also used to drop the items in the log tree before |
| 4404 | * we relog the inode, so if root != BTRFS_I(inode)->root, it means |
| 4405 | * it is used to drop the loged items. So we shouldn't kill the delayed |
| 4406 | * items. |
| 4407 | */ |
| 4408 | if (min_type == 0 && root == BTRFS_I(inode)->root) |
| 4409 | btrfs_kill_delayed_inode_items(inode); |
| 4410 | |
Li Zefan | 33345d01 | 2011-04-20 10:31:50 +0800 | [diff] [blame] | 4411 | key.objectid = ino; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4412 | key.offset = (u64)-1; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 4413 | key.type = (u8)-1; |
| 4414 | |
Chris Mason | 85e21ba | 2008-01-29 15:11:36 -0500 | [diff] [blame] | 4415 | search_again: |
Chris Mason | 28ed134 | 2014-12-17 09:41:04 -0800 | [diff] [blame] | 4416 | /* |
| 4417 | * with a 16K leaf size and 128MB extents, you can actually queue |
| 4418 | * up a huge file in a single leaf. Most of the time that |
| 4419 | * bytes_deleted is > 0, it will be huge by the time we get here |
| 4420 | */ |
Byongho Lee | ee22184 | 2015-12-15 01:42:10 +0900 | [diff] [blame] | 4421 | if (be_nice && bytes_deleted > SZ_32M) { |
Chris Mason | 28ed134 | 2014-12-17 09:41:04 -0800 | [diff] [blame] | 4422 | if (btrfs_should_end_transaction(trans, root)) { |
| 4423 | err = -EAGAIN; |
| 4424 | goto error; |
| 4425 | } |
| 4426 | } |
| 4427 | |
| 4428 | |
Chris Mason | b947343 | 2009-03-13 11:00:37 -0400 | [diff] [blame] | 4429 | path->leave_spinning = 1; |
Chris Mason | 85e21ba | 2008-01-29 15:11:36 -0500 | [diff] [blame] | 4430 | ret = btrfs_search_slot(trans, root, &key, path, -1, 1); |
Yan, Zheng | 8082510 | 2009-11-12 09:35:36 +0000 | [diff] [blame] | 4431 | if (ret < 0) { |
| 4432 | err = ret; |
| 4433 | goto out; |
| 4434 | } |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 4435 | |
Chris Mason | 85e21ba | 2008-01-29 15:11:36 -0500 | [diff] [blame] | 4436 | if (ret > 0) { |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 4437 | /* there are no items in the tree for us to truncate, we're |
| 4438 | * done |
| 4439 | */ |
Yan, Zheng | 8082510 | 2009-11-12 09:35:36 +0000 | [diff] [blame] | 4440 | if (path->slots[0] == 0) |
| 4441 | goto out; |
Chris Mason | 85e21ba | 2008-01-29 15:11:36 -0500 | [diff] [blame] | 4442 | path->slots[0]--; |
| 4443 | } |
| 4444 | |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 4445 | while (1) { |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4446 | fi = NULL; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 4447 | leaf = path->nodes[0]; |
| 4448 | btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]); |
David Sterba | 962a298 | 2014-06-04 18:41:45 +0200 | [diff] [blame] | 4449 | found_type = found_key.type; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4450 | |
Li Zefan | 33345d01 | 2011-04-20 10:31:50 +0800 | [diff] [blame] | 4451 | if (found_key.objectid != ino) |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4452 | break; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 4453 | |
Chris Mason | 85e21ba | 2008-01-29 15:11:36 -0500 | [diff] [blame] | 4454 | if (found_type < min_type) |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4455 | break; |
| 4456 | |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 4457 | item_end = found_key.offset; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4458 | if (found_type == BTRFS_EXTENT_DATA_KEY) { |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 4459 | fi = btrfs_item_ptr(leaf, path->slots[0], |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4460 | struct btrfs_file_extent_item); |
Chris Mason | 179e29e | 2007-11-01 11:28:41 -0400 | [diff] [blame] | 4461 | extent_type = btrfs_file_extent_type(leaf, fi); |
| 4462 | if (extent_type != BTRFS_FILE_EXTENT_INLINE) { |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 4463 | item_end += |
Chris Mason | db94535 | 2007-10-15 16:15:53 -0400 | [diff] [blame] | 4464 | btrfs_file_extent_num_bytes(leaf, fi); |
Chris Mason | 179e29e | 2007-11-01 11:28:41 -0400 | [diff] [blame] | 4465 | } else if (extent_type == BTRFS_FILE_EXTENT_INLINE) { |
Chris Mason | 179e29e | 2007-11-01 11:28:41 -0400 | [diff] [blame] | 4466 | item_end += btrfs_file_extent_inline_len(leaf, |
Chris Mason | 514ac8a | 2014-01-03 21:07:00 -0800 | [diff] [blame] | 4467 | path->slots[0], fi); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4468 | } |
Yan | 008630c | 2007-11-07 13:31:09 -0500 | [diff] [blame] | 4469 | item_end--; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4470 | } |
Yan, Zheng | 8082510 | 2009-11-12 09:35:36 +0000 | [diff] [blame] | 4471 | if (found_type > min_type) { |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4472 | del_item = 1; |
Yan, Zheng | 8082510 | 2009-11-12 09:35:36 +0000 | [diff] [blame] | 4473 | } else { |
| 4474 | if (item_end < new_size) |
| 4475 | break; |
| 4476 | if (found_key.offset >= new_size) |
| 4477 | del_item = 1; |
| 4478 | else |
| 4479 | del_item = 0; |
| 4480 | } |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4481 | found_extent = 0; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4482 | /* FIXME, shrink the extent if the ref count is only 1 */ |
Chris Mason | 179e29e | 2007-11-01 11:28:41 -0400 | [diff] [blame] | 4483 | if (found_type != BTRFS_EXTENT_DATA_KEY) |
| 4484 | goto delete; |
| 4485 | |
Josef Bacik | 7f4f6e0 | 2013-08-29 16:43:28 -0400 | [diff] [blame] | 4486 | if (del_item) |
| 4487 | last_size = found_key.offset; |
| 4488 | else |
| 4489 | last_size = new_size; |
| 4490 | |
Chris Mason | 179e29e | 2007-11-01 11:28:41 -0400 | [diff] [blame] | 4491 | if (extent_type != BTRFS_FILE_EXTENT_INLINE) { |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4492 | u64 num_dec; |
Chris Mason | db94535 | 2007-10-15 16:15:53 -0400 | [diff] [blame] | 4493 | extent_start = btrfs_file_extent_disk_bytenr(leaf, fi); |
Miao Xie | f70a9a6b | 2012-01-12 19:10:12 -0500 | [diff] [blame] | 4494 | if (!del_item) { |
Chris Mason | db94535 | 2007-10-15 16:15:53 -0400 | [diff] [blame] | 4495 | u64 orig_num_bytes = |
| 4496 | btrfs_file_extent_num_bytes(leaf, fi); |
Qu Wenruo | fda2832 | 2013-02-26 08:10:22 +0000 | [diff] [blame] | 4497 | extent_num_bytes = ALIGN(new_size - |
| 4498 | found_key.offset, |
| 4499 | root->sectorsize); |
Chris Mason | db94535 | 2007-10-15 16:15:53 -0400 | [diff] [blame] | 4500 | btrfs_set_file_extent_num_bytes(leaf, fi, |
| 4501 | extent_num_bytes); |
| 4502 | num_dec = (orig_num_bytes - |
Chris Mason | 9069218 | 2008-02-08 13:49:28 -0500 | [diff] [blame] | 4503 | extent_num_bytes); |
Miao Xie | 27cdeb7 | 2014-04-02 19:51:05 +0800 | [diff] [blame] | 4504 | if (test_bit(BTRFS_ROOT_REF_COWS, |
| 4505 | &root->state) && |
| 4506 | extent_start != 0) |
Yan Zheng | a76a3cd | 2008-10-09 11:46:29 -0400 | [diff] [blame] | 4507 | inode_sub_bytes(inode, num_dec); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 4508 | btrfs_mark_buffer_dirty(leaf); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4509 | } else { |
Chris Mason | db94535 | 2007-10-15 16:15:53 -0400 | [diff] [blame] | 4510 | extent_num_bytes = |
| 4511 | btrfs_file_extent_disk_num_bytes(leaf, |
| 4512 | fi); |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 4513 | extent_offset = found_key.offset - |
| 4514 | btrfs_file_extent_offset(leaf, fi); |
| 4515 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4516 | /* FIXME blocksize != 4096 */ |
Chris Mason | 9069218 | 2008-02-08 13:49:28 -0500 | [diff] [blame] | 4517 | num_dec = btrfs_file_extent_num_bytes(leaf, fi); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4518 | if (extent_start != 0) { |
| 4519 | found_extent = 1; |
Miao Xie | 27cdeb7 | 2014-04-02 19:51:05 +0800 | [diff] [blame] | 4520 | if (test_bit(BTRFS_ROOT_REF_COWS, |
| 4521 | &root->state)) |
Yan Zheng | a76a3cd | 2008-10-09 11:46:29 -0400 | [diff] [blame] | 4522 | inode_sub_bytes(inode, num_dec); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4523 | } |
| 4524 | } |
Chris Mason | 9069218 | 2008-02-08 13:49:28 -0500 | [diff] [blame] | 4525 | } else if (extent_type == BTRFS_FILE_EXTENT_INLINE) { |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 4526 | /* |
| 4527 | * we can't truncate inline items that have had |
| 4528 | * special encodings |
| 4529 | */ |
| 4530 | if (!del_item && |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 4531 | btrfs_file_extent_encryption(leaf, fi) == 0 && |
| 4532 | btrfs_file_extent_other_encoding(leaf, fi) == 0) { |
Chris Mason | 514ac8a | 2014-01-03 21:07:00 -0800 | [diff] [blame] | 4533 | |
| 4534 | /* |
Filipe Manana | 0305cd5 | 2015-10-16 12:34:25 +0100 | [diff] [blame] | 4535 | * Need to release path in order to truncate a |
| 4536 | * compressed extent. So delete any accumulated |
| 4537 | * extent items so far. |
Chris Mason | 514ac8a | 2014-01-03 21:07:00 -0800 | [diff] [blame] | 4538 | */ |
Filipe Manana | 0305cd5 | 2015-10-16 12:34:25 +0100 | [diff] [blame] | 4539 | if (btrfs_file_extent_compression(leaf, fi) != |
| 4540 | BTRFS_COMPRESS_NONE && pending_del_nr) { |
| 4541 | err = btrfs_del_items(trans, root, path, |
| 4542 | pending_del_slot, |
| 4543 | pending_del_nr); |
| 4544 | if (err) { |
| 4545 | btrfs_abort_transaction(trans, |
Filipe Manana | 0305cd5 | 2015-10-16 12:34:25 +0100 | [diff] [blame] | 4546 | err); |
| 4547 | goto error; |
| 4548 | } |
| 4549 | pending_del_nr = 0; |
| 4550 | } |
| 4551 | |
| 4552 | err = truncate_inline_extent(inode, path, |
| 4553 | &found_key, |
| 4554 | item_end, |
| 4555 | new_size); |
| 4556 | if (err) { |
Jeff Mahoney | 6664283 | 2016-06-10 18:19:25 -0400 | [diff] [blame] | 4557 | btrfs_abort_transaction(trans, err); |
Filipe Manana | 0305cd5 | 2015-10-16 12:34:25 +0100 | [diff] [blame] | 4558 | goto error; |
| 4559 | } |
Miao Xie | 27cdeb7 | 2014-04-02 19:51:05 +0800 | [diff] [blame] | 4560 | } else if (test_bit(BTRFS_ROOT_REF_COWS, |
| 4561 | &root->state)) { |
Filipe Manana | 0305cd5 | 2015-10-16 12:34:25 +0100 | [diff] [blame] | 4562 | inode_sub_bytes(inode, item_end + 1 - new_size); |
Chris Mason | 9069218 | 2008-02-08 13:49:28 -0500 | [diff] [blame] | 4563 | } |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4564 | } |
Chris Mason | 179e29e | 2007-11-01 11:28:41 -0400 | [diff] [blame] | 4565 | delete: |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4566 | if (del_item) { |
Chris Mason | 85e21ba | 2008-01-29 15:11:36 -0500 | [diff] [blame] | 4567 | if (!pending_del_nr) { |
| 4568 | /* no pending yet, add ourselves */ |
| 4569 | pending_del_slot = path->slots[0]; |
| 4570 | pending_del_nr = 1; |
| 4571 | } else if (pending_del_nr && |
| 4572 | path->slots[0] + 1 == pending_del_slot) { |
| 4573 | /* hop on the pending chunk */ |
| 4574 | pending_del_nr++; |
| 4575 | pending_del_slot = path->slots[0]; |
| 4576 | } else { |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 4577 | BUG(); |
Chris Mason | 85e21ba | 2008-01-29 15:11:36 -0500 | [diff] [blame] | 4578 | } |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4579 | } else { |
| 4580 | break; |
| 4581 | } |
Chris Mason | 28f75a0 | 2015-02-04 06:59:29 -0800 | [diff] [blame] | 4582 | should_throttle = 0; |
| 4583 | |
Miao Xie | 27cdeb7 | 2014-04-02 19:51:05 +0800 | [diff] [blame] | 4584 | if (found_extent && |
| 4585 | (test_bit(BTRFS_ROOT_REF_COWS, &root->state) || |
| 4586 | root == root->fs_info->tree_root)) { |
Chris Mason | b947343 | 2009-03-13 11:00:37 -0400 | [diff] [blame] | 4587 | btrfs_set_path_blocking(path); |
Chris Mason | 28ed134 | 2014-12-17 09:41:04 -0800 | [diff] [blame] | 4588 | bytes_deleted += extent_num_bytes; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4589 | ret = btrfs_free_extent(trans, root, extent_start, |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 4590 | extent_num_bytes, 0, |
| 4591 | btrfs_header_owner(leaf), |
Filipe Manana | b06c4bf | 2015-10-23 07:52:54 +0100 | [diff] [blame] | 4592 | ino, extent_offset); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4593 | BUG_ON(ret); |
Josef Bacik | 1262133 | 2015-02-03 07:50:16 -0800 | [diff] [blame] | 4594 | if (btrfs_should_throttle_delayed_refs(trans, root)) |
Chris Mason | 28ed134 | 2014-12-17 09:41:04 -0800 | [diff] [blame] | 4595 | btrfs_async_run_delayed_refs(root, |
Josef Bacik | 31b9655 | 2016-04-11 17:37:40 -0400 | [diff] [blame] | 4596 | trans->transid, |
Chris Mason | 28ed134 | 2014-12-17 09:41:04 -0800 | [diff] [blame] | 4597 | trans->delayed_ref_updates * 2, 0); |
Chris Mason | 28f75a0 | 2015-02-04 06:59:29 -0800 | [diff] [blame] | 4598 | if (be_nice) { |
| 4599 | if (truncate_space_check(trans, root, |
| 4600 | extent_num_bytes)) { |
| 4601 | should_end = 1; |
| 4602 | } |
| 4603 | if (btrfs_should_throttle_delayed_refs(trans, |
| 4604 | root)) { |
| 4605 | should_throttle = 1; |
| 4606 | } |
| 4607 | } |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4608 | } |
Chris Mason | 85e21ba | 2008-01-29 15:11:36 -0500 | [diff] [blame] | 4609 | |
Yan, Zheng | 8082510 | 2009-11-12 09:35:36 +0000 | [diff] [blame] | 4610 | if (found_type == BTRFS_INODE_ITEM_KEY) |
| 4611 | break; |
| 4612 | |
| 4613 | if (path->slots[0] == 0 || |
Josef Bacik | 1262133 | 2015-02-03 07:50:16 -0800 | [diff] [blame] | 4614 | path->slots[0] != pending_del_slot || |
Chris Mason | 28f75a0 | 2015-02-04 06:59:29 -0800 | [diff] [blame] | 4615 | should_throttle || should_end) { |
Yan, Zheng | 8082510 | 2009-11-12 09:35:36 +0000 | [diff] [blame] | 4616 | if (pending_del_nr) { |
| 4617 | ret = btrfs_del_items(trans, root, path, |
| 4618 | pending_del_slot, |
| 4619 | pending_del_nr); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 4620 | if (ret) { |
Jeff Mahoney | 6664283 | 2016-06-10 18:19:25 -0400 | [diff] [blame] | 4621 | btrfs_abort_transaction(trans, ret); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 4622 | goto error; |
| 4623 | } |
Yan, Zheng | 8082510 | 2009-11-12 09:35:36 +0000 | [diff] [blame] | 4624 | pending_del_nr = 0; |
| 4625 | } |
David Sterba | b3b4aa7 | 2011-04-21 01:20:15 +0200 | [diff] [blame] | 4626 | btrfs_release_path(path); |
Chris Mason | 28f75a0 | 2015-02-04 06:59:29 -0800 | [diff] [blame] | 4627 | if (should_throttle) { |
Josef Bacik | 1262133 | 2015-02-03 07:50:16 -0800 | [diff] [blame] | 4628 | unsigned long updates = trans->delayed_ref_updates; |
| 4629 | if (updates) { |
| 4630 | trans->delayed_ref_updates = 0; |
| 4631 | ret = btrfs_run_delayed_refs(trans, root, updates * 2); |
| 4632 | if (ret && !err) |
| 4633 | err = ret; |
| 4634 | } |
| 4635 | } |
Chris Mason | 28f75a0 | 2015-02-04 06:59:29 -0800 | [diff] [blame] | 4636 | /* |
| 4637 | * if we failed to refill our space rsv, bail out |
| 4638 | * and let the transaction restart |
| 4639 | */ |
| 4640 | if (should_end) { |
| 4641 | err = -EAGAIN; |
| 4642 | goto error; |
| 4643 | } |
Chris Mason | 85e21ba | 2008-01-29 15:11:36 -0500 | [diff] [blame] | 4644 | goto search_again; |
Yan, Zheng | 8082510 | 2009-11-12 09:35:36 +0000 | [diff] [blame] | 4645 | } else { |
| 4646 | path->slots[0]--; |
Chris Mason | 85e21ba | 2008-01-29 15:11:36 -0500 | [diff] [blame] | 4647 | } |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4648 | } |
Yan, Zheng | 8082510 | 2009-11-12 09:35:36 +0000 | [diff] [blame] | 4649 | out: |
Chris Mason | 85e21ba | 2008-01-29 15:11:36 -0500 | [diff] [blame] | 4650 | if (pending_del_nr) { |
| 4651 | ret = btrfs_del_items(trans, root, path, pending_del_slot, |
| 4652 | pending_del_nr); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 4653 | if (ret) |
Jeff Mahoney | 6664283 | 2016-06-10 18:19:25 -0400 | [diff] [blame] | 4654 | btrfs_abort_transaction(trans, ret); |
Chris Mason | 85e21ba | 2008-01-29 15:11:36 -0500 | [diff] [blame] | 4655 | } |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 4656 | error: |
Filipe Manana | c1aa457 | 2015-06-20 18:20:09 +0100 | [diff] [blame] | 4657 | if (root->root_key.objectid != BTRFS_TREE_LOG_OBJECTID) |
Josef Bacik | 7f4f6e0 | 2013-08-29 16:43:28 -0400 | [diff] [blame] | 4658 | btrfs_ordered_update_i_size(inode, last_size, NULL); |
Chris Mason | 28ed134 | 2014-12-17 09:41:04 -0800 | [diff] [blame] | 4659 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4660 | btrfs_free_path(path); |
Chris Mason | 28ed134 | 2014-12-17 09:41:04 -0800 | [diff] [blame] | 4661 | |
Byongho Lee | ee22184 | 2015-12-15 01:42:10 +0900 | [diff] [blame] | 4662 | if (be_nice && bytes_deleted > SZ_32M) { |
Chris Mason | 28ed134 | 2014-12-17 09:41:04 -0800 | [diff] [blame] | 4663 | unsigned long updates = trans->delayed_ref_updates; |
| 4664 | if (updates) { |
| 4665 | trans->delayed_ref_updates = 0; |
| 4666 | ret = btrfs_run_delayed_refs(trans, root, updates * 2); |
| 4667 | if (ret && !err) |
| 4668 | err = ret; |
| 4669 | } |
| 4670 | } |
Yan, Zheng | 8082510 | 2009-11-12 09:35:36 +0000 | [diff] [blame] | 4671 | return err; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4672 | } |
| 4673 | |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 4674 | /* |
Chandan Rajendra | 9703fef | 2016-01-21 15:55:56 +0530 | [diff] [blame] | 4675 | * btrfs_truncate_block - read, zero a chunk and write a block |
Josef Bacik | 2aaa665 | 2012-08-29 14:27:18 -0400 | [diff] [blame] | 4676 | * @inode - inode that we're zeroing |
| 4677 | * @from - the offset to start zeroing |
| 4678 | * @len - the length to zero, 0 to zero the entire range respective to the |
| 4679 | * offset |
| 4680 | * @front - zero up to the offset instead of from the offset on |
| 4681 | * |
Chandan Rajendra | 9703fef | 2016-01-21 15:55:56 +0530 | [diff] [blame] | 4682 | * 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] | 4683 | * 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] | 4684 | */ |
Chandan Rajendra | 9703fef | 2016-01-21 15:55:56 +0530 | [diff] [blame] | 4685 | 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] | 4686 | int front) |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 4687 | { |
Josef Bacik | 2aaa665 | 2012-08-29 14:27:18 -0400 | [diff] [blame] | 4688 | struct address_space *mapping = inode->i_mapping; |
Chris Mason | db94535 | 2007-10-15 16:15:53 -0400 | [diff] [blame] | 4689 | struct btrfs_root *root = BTRFS_I(inode)->root; |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 4690 | struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree; |
| 4691 | struct btrfs_ordered_extent *ordered; |
Josef Bacik | 2ac55d4 | 2010-02-03 19:33:23 +0000 | [diff] [blame] | 4692 | struct extent_state *cached_state = NULL; |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 4693 | char *kaddr; |
Chris Mason | db94535 | 2007-10-15 16:15:53 -0400 | [diff] [blame] | 4694 | u32 blocksize = root->sectorsize; |
Kirill A. Shutemov | 09cbfea | 2016-04-01 15:29:47 +0300 | [diff] [blame] | 4695 | pgoff_t index = from >> PAGE_SHIFT; |
Chandan Rajendra | 9703fef | 2016-01-21 15:55:56 +0530 | [diff] [blame] | 4696 | unsigned offset = from & (blocksize - 1); |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 4697 | struct page *page; |
Josef Bacik | 3b16a4e | 2011-09-21 15:05:58 -0400 | [diff] [blame] | 4698 | gfp_t mask = btrfs_alloc_write_mask(mapping); |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 4699 | int ret = 0; |
Chandan Rajendra | 9703fef | 2016-01-21 15:55:56 +0530 | [diff] [blame] | 4700 | u64 block_start; |
| 4701 | u64 block_end; |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 4702 | |
Josef Bacik | 2aaa665 | 2012-08-29 14:27:18 -0400 | [diff] [blame] | 4703 | if ((offset & (blocksize - 1)) == 0 && |
| 4704 | (!len || ((len & (blocksize - 1)) == 0))) |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 4705 | goto out; |
Chandan Rajendra | 9703fef | 2016-01-21 15:55:56 +0530 | [diff] [blame] | 4706 | |
Qu Wenruo | 7cf5b97 | 2015-09-08 17:25:55 +0800 | [diff] [blame] | 4707 | ret = btrfs_delalloc_reserve_space(inode, |
Chandan Rajendra | 9703fef | 2016-01-21 15:55:56 +0530 | [diff] [blame] | 4708 | round_down(from, blocksize), blocksize); |
Josef Bacik | 5d5e103 | 2009-10-13 16:46:49 -0400 | [diff] [blame] | 4709 | if (ret) |
| 4710 | goto out; |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 4711 | |
Chris Mason | 211c17f | 2008-05-15 09:13:45 -0400 | [diff] [blame] | 4712 | again: |
Josef Bacik | 3b16a4e | 2011-09-21 15:05:58 -0400 | [diff] [blame] | 4713 | page = find_or_create_page(mapping, index, mask); |
Josef Bacik | 5d5e103 | 2009-10-13 16:46:49 -0400 | [diff] [blame] | 4714 | if (!page) { |
Qu Wenruo | 7cf5b97 | 2015-09-08 17:25:55 +0800 | [diff] [blame] | 4715 | btrfs_delalloc_release_space(inode, |
Chandan Rajendra | 9703fef | 2016-01-21 15:55:56 +0530 | [diff] [blame] | 4716 | round_down(from, blocksize), |
| 4717 | blocksize); |
Miao Xie | ac6a2b3 | 2012-12-05 10:56:13 +0000 | [diff] [blame] | 4718 | ret = -ENOMEM; |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 4719 | goto out; |
Josef Bacik | 5d5e103 | 2009-10-13 16:46:49 -0400 | [diff] [blame] | 4720 | } |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 4721 | |
Chandan Rajendra | 9703fef | 2016-01-21 15:55:56 +0530 | [diff] [blame] | 4722 | block_start = round_down(from, blocksize); |
| 4723 | block_end = block_start + blocksize - 1; |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 4724 | |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 4725 | if (!PageUptodate(page)) { |
| 4726 | ret = btrfs_readpage(NULL, page); |
| 4727 | lock_page(page); |
Chris Mason | 211c17f | 2008-05-15 09:13:45 -0400 | [diff] [blame] | 4728 | if (page->mapping != mapping) { |
| 4729 | unlock_page(page); |
Kirill A. Shutemov | 09cbfea | 2016-04-01 15:29:47 +0300 | [diff] [blame] | 4730 | put_page(page); |
Chris Mason | 211c17f | 2008-05-15 09:13:45 -0400 | [diff] [blame] | 4731 | goto again; |
| 4732 | } |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 4733 | if (!PageUptodate(page)) { |
| 4734 | ret = -EIO; |
Chris Mason | 8964222 | 2008-07-24 09:41:53 -0400 | [diff] [blame] | 4735 | goto out_unlock; |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 4736 | } |
| 4737 | } |
Chris Mason | 211c17f | 2008-05-15 09:13:45 -0400 | [diff] [blame] | 4738 | wait_on_page_writeback(page); |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 4739 | |
Chandan Rajendra | 9703fef | 2016-01-21 15:55:56 +0530 | [diff] [blame] | 4740 | lock_extent_bits(io_tree, block_start, block_end, &cached_state); |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 4741 | set_page_extent_mapped(page); |
| 4742 | |
Chandan Rajendra | 9703fef | 2016-01-21 15:55:56 +0530 | [diff] [blame] | 4743 | ordered = btrfs_lookup_ordered_extent(inode, block_start); |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 4744 | if (ordered) { |
Chandan Rajendra | 9703fef | 2016-01-21 15:55:56 +0530 | [diff] [blame] | 4745 | unlock_extent_cached(io_tree, block_start, block_end, |
Josef Bacik | 2ac55d4 | 2010-02-03 19:33:23 +0000 | [diff] [blame] | 4746 | &cached_state, GFP_NOFS); |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 4747 | unlock_page(page); |
Kirill A. Shutemov | 09cbfea | 2016-04-01 15:29:47 +0300 | [diff] [blame] | 4748 | put_page(page); |
Chris Mason | eb84ae0 | 2008-07-17 13:53:27 -0400 | [diff] [blame] | 4749 | btrfs_start_ordered_extent(inode, ordered, 1); |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 4750 | btrfs_put_ordered_extent(ordered); |
| 4751 | goto again; |
| 4752 | } |
| 4753 | |
Chandan Rajendra | 9703fef | 2016-01-21 15:55:56 +0530 | [diff] [blame] | 4754 | clear_extent_bit(&BTRFS_I(inode)->io_tree, block_start, block_end, |
Liu Bo | 9e8a4a8 | 2012-09-05 19:10:51 -0600 | [diff] [blame] | 4755 | EXTENT_DIRTY | EXTENT_DELALLOC | |
| 4756 | EXTENT_DO_ACCOUNTING | EXTENT_DEFRAG, |
Josef Bacik | 2ac55d4 | 2010-02-03 19:33:23 +0000 | [diff] [blame] | 4757 | 0, 0, &cached_state, GFP_NOFS); |
Josef Bacik | 5d5e103 | 2009-10-13 16:46:49 -0400 | [diff] [blame] | 4758 | |
Chandan Rajendra | 9703fef | 2016-01-21 15:55:56 +0530 | [diff] [blame] | 4759 | ret = btrfs_set_extent_delalloc(inode, block_start, block_end, |
Josef Bacik | 2ac55d4 | 2010-02-03 19:33:23 +0000 | [diff] [blame] | 4760 | &cached_state); |
Josef Bacik | 9ed74f2 | 2009-09-11 16:12:44 -0400 | [diff] [blame] | 4761 | if (ret) { |
Chandan Rajendra | 9703fef | 2016-01-21 15:55:56 +0530 | [diff] [blame] | 4762 | unlock_extent_cached(io_tree, block_start, block_end, |
Josef Bacik | 2ac55d4 | 2010-02-03 19:33:23 +0000 | [diff] [blame] | 4763 | &cached_state, GFP_NOFS); |
Josef Bacik | 9ed74f2 | 2009-09-11 16:12:44 -0400 | [diff] [blame] | 4764 | goto out_unlock; |
| 4765 | } |
| 4766 | |
Chandan Rajendra | 9703fef | 2016-01-21 15:55:56 +0530 | [diff] [blame] | 4767 | if (offset != blocksize) { |
Josef Bacik | 2aaa665 | 2012-08-29 14:27:18 -0400 | [diff] [blame] | 4768 | if (!len) |
Chandan Rajendra | 9703fef | 2016-01-21 15:55:56 +0530 | [diff] [blame] | 4769 | len = blocksize - offset; |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 4770 | kaddr = kmap(page); |
Josef Bacik | 2aaa665 | 2012-08-29 14:27:18 -0400 | [diff] [blame] | 4771 | if (front) |
Chandan Rajendra | 9703fef | 2016-01-21 15:55:56 +0530 | [diff] [blame] | 4772 | memset(kaddr + (block_start - page_offset(page)), |
| 4773 | 0, offset); |
Josef Bacik | 2aaa665 | 2012-08-29 14:27:18 -0400 | [diff] [blame] | 4774 | else |
Chandan Rajendra | 9703fef | 2016-01-21 15:55:56 +0530 | [diff] [blame] | 4775 | memset(kaddr + (block_start - page_offset(page)) + offset, |
| 4776 | 0, len); |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 4777 | flush_dcache_page(page); |
| 4778 | kunmap(page); |
| 4779 | } |
Chris Mason | 247e743 | 2008-07-17 12:53:51 -0400 | [diff] [blame] | 4780 | ClearPageChecked(page); |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 4781 | set_page_dirty(page); |
Chandan Rajendra | 9703fef | 2016-01-21 15:55:56 +0530 | [diff] [blame] | 4782 | unlock_extent_cached(io_tree, block_start, block_end, &cached_state, |
Josef Bacik | 2ac55d4 | 2010-02-03 19:33:23 +0000 | [diff] [blame] | 4783 | GFP_NOFS); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4784 | |
Chris Mason | 8964222 | 2008-07-24 09:41:53 -0400 | [diff] [blame] | 4785 | out_unlock: |
Josef Bacik | 5d5e103 | 2009-10-13 16:46:49 -0400 | [diff] [blame] | 4786 | if (ret) |
Chandan Rajendra | 9703fef | 2016-01-21 15:55:56 +0530 | [diff] [blame] | 4787 | btrfs_delalloc_release_space(inode, block_start, |
| 4788 | blocksize); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4789 | unlock_page(page); |
Kirill A. Shutemov | 09cbfea | 2016-04-01 15:29:47 +0300 | [diff] [blame] | 4790 | put_page(page); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4791 | out: |
| 4792 | return ret; |
| 4793 | } |
| 4794 | |
Josef Bacik | 16e7549 | 2013-10-22 12:18:51 -0400 | [diff] [blame] | 4795 | static int maybe_insert_hole(struct btrfs_root *root, struct inode *inode, |
| 4796 | u64 offset, u64 len) |
| 4797 | { |
| 4798 | struct btrfs_trans_handle *trans; |
| 4799 | int ret; |
| 4800 | |
| 4801 | /* |
| 4802 | * Still need to make sure the inode looks like it's been updated so |
| 4803 | * that any holes get logged if we fsync. |
| 4804 | */ |
| 4805 | if (btrfs_fs_incompat(root->fs_info, NO_HOLES)) { |
| 4806 | BTRFS_I(inode)->last_trans = root->fs_info->generation; |
| 4807 | BTRFS_I(inode)->last_sub_trans = root->log_transid; |
| 4808 | BTRFS_I(inode)->last_log_commit = root->last_log_commit; |
| 4809 | return 0; |
| 4810 | } |
| 4811 | |
| 4812 | /* |
| 4813 | * 1 - for the one we're dropping |
| 4814 | * 1 - for the one we're adding |
| 4815 | * 1 - for updating the inode. |
| 4816 | */ |
| 4817 | trans = btrfs_start_transaction(root, 3); |
| 4818 | if (IS_ERR(trans)) |
| 4819 | return PTR_ERR(trans); |
| 4820 | |
| 4821 | ret = btrfs_drop_extents(trans, root, inode, offset, offset + len, 1); |
| 4822 | if (ret) { |
Jeff Mahoney | 6664283 | 2016-06-10 18:19:25 -0400 | [diff] [blame] | 4823 | btrfs_abort_transaction(trans, ret); |
Josef Bacik | 16e7549 | 2013-10-22 12:18:51 -0400 | [diff] [blame] | 4824 | btrfs_end_transaction(trans, root); |
| 4825 | return ret; |
| 4826 | } |
| 4827 | |
| 4828 | ret = btrfs_insert_file_extent(trans, root, btrfs_ino(inode), offset, |
| 4829 | 0, 0, len, 0, len, 0, 0, 0); |
| 4830 | if (ret) |
Jeff Mahoney | 6664283 | 2016-06-10 18:19:25 -0400 | [diff] [blame] | 4831 | btrfs_abort_transaction(trans, ret); |
Josef Bacik | 16e7549 | 2013-10-22 12:18:51 -0400 | [diff] [blame] | 4832 | else |
| 4833 | btrfs_update_inode(trans, root, inode); |
| 4834 | btrfs_end_transaction(trans, root); |
| 4835 | return ret; |
| 4836 | } |
| 4837 | |
Josef Bacik | 695a0d0 | 2011-03-04 15:46:53 -0500 | [diff] [blame] | 4838 | /* |
| 4839 | * This function puts in dummy file extents for the area we're creating a hole |
| 4840 | * for. So if we are truncating this file to a larger size we need to insert |
| 4841 | * these file extents so that btrfs_get_extent will return a EXTENT_MAP_HOLE for |
| 4842 | * the range between oldsize and size |
| 4843 | */ |
Josef Bacik | a41ad39 | 2011-01-31 15:30:16 -0500 | [diff] [blame] | 4844 | 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] | 4845 | { |
Yan Zheng | 9036c10 | 2008-10-30 14:19:41 -0400 | [diff] [blame] | 4846 | struct btrfs_root *root = BTRFS_I(inode)->root; |
| 4847 | struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree; |
Yan, Zheng | a22285a | 2010-05-16 10:48:46 -0400 | [diff] [blame] | 4848 | struct extent_map *em = NULL; |
Josef Bacik | 2ac55d4 | 2010-02-03 19:33:23 +0000 | [diff] [blame] | 4849 | struct extent_state *cached_state = NULL; |
Josef Bacik | 5dc562c | 2012-08-17 13:14:17 -0400 | [diff] [blame] | 4850 | struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree; |
Qu Wenruo | fda2832 | 2013-02-26 08:10:22 +0000 | [diff] [blame] | 4851 | u64 hole_start = ALIGN(oldsize, root->sectorsize); |
| 4852 | u64 block_end = ALIGN(size, root->sectorsize); |
Yan Zheng | 9036c10 | 2008-10-30 14:19:41 -0400 | [diff] [blame] | 4853 | u64 last_byte; |
| 4854 | u64 cur_offset; |
| 4855 | u64 hole_size; |
Josef Bacik | 9ed74f2 | 2009-09-11 16:12:44 -0400 | [diff] [blame] | 4856 | int err = 0; |
Yan Zheng | 9036c10 | 2008-10-30 14:19:41 -0400 | [diff] [blame] | 4857 | |
Josef Bacik | a71754f | 2013-06-17 17:14:39 -0400 | [diff] [blame] | 4858 | /* |
Chandan Rajendra | 9703fef | 2016-01-21 15:55:56 +0530 | [diff] [blame] | 4859 | * If our size started in the middle of a block we need to zero out the |
| 4860 | * 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] | 4861 | * expose stale data. |
| 4862 | */ |
Chandan Rajendra | 9703fef | 2016-01-21 15:55:56 +0530 | [diff] [blame] | 4863 | err = btrfs_truncate_block(inode, oldsize, 0, 0); |
Josef Bacik | a71754f | 2013-06-17 17:14:39 -0400 | [diff] [blame] | 4864 | if (err) |
| 4865 | return err; |
| 4866 | |
Yan Zheng | 9036c10 | 2008-10-30 14:19:41 -0400 | [diff] [blame] | 4867 | if (size <= hole_start) |
| 4868 | return 0; |
| 4869 | |
Yan Zheng | 9036c10 | 2008-10-30 14:19:41 -0400 | [diff] [blame] | 4870 | while (1) { |
| 4871 | struct btrfs_ordered_extent *ordered; |
Miao Xie | fa7c149 | 2013-09-26 13:15:27 +0800 | [diff] [blame] | 4872 | |
David Sterba | ff13db4 | 2015-12-03 14:30:40 +0100 | [diff] [blame] | 4873 | lock_extent_bits(io_tree, hole_start, block_end - 1, |
Jeff Mahoney | d008237 | 2012-03-01 14:57:19 +0100 | [diff] [blame] | 4874 | &cached_state); |
Miao Xie | fa7c149 | 2013-09-26 13:15:27 +0800 | [diff] [blame] | 4875 | ordered = btrfs_lookup_ordered_range(inode, hole_start, |
| 4876 | block_end - hole_start); |
Yan Zheng | 9036c10 | 2008-10-30 14:19:41 -0400 | [diff] [blame] | 4877 | if (!ordered) |
| 4878 | break; |
Josef Bacik | 2ac55d4 | 2010-02-03 19:33:23 +0000 | [diff] [blame] | 4879 | unlock_extent_cached(io_tree, hole_start, block_end - 1, |
| 4880 | &cached_state, GFP_NOFS); |
Miao Xie | fa7c149 | 2013-09-26 13:15:27 +0800 | [diff] [blame] | 4881 | btrfs_start_ordered_extent(inode, ordered, 1); |
Yan Zheng | 9036c10 | 2008-10-30 14:19:41 -0400 | [diff] [blame] | 4882 | btrfs_put_ordered_extent(ordered); |
| 4883 | } |
| 4884 | |
Yan Zheng | 9036c10 | 2008-10-30 14:19:41 -0400 | [diff] [blame] | 4885 | cur_offset = hole_start; |
| 4886 | while (1) { |
| 4887 | em = btrfs_get_extent(inode, NULL, 0, cur_offset, |
| 4888 | block_end - cur_offset, 0); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 4889 | if (IS_ERR(em)) { |
| 4890 | err = PTR_ERR(em); |
Zach Brown | f276795 | 2013-01-08 19:37:58 +0000 | [diff] [blame] | 4891 | em = NULL; |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 4892 | break; |
| 4893 | } |
Yan Zheng | 9036c10 | 2008-10-30 14:19:41 -0400 | [diff] [blame] | 4894 | last_byte = min(extent_map_end(em), block_end); |
Qu Wenruo | fda2832 | 2013-02-26 08:10:22 +0000 | [diff] [blame] | 4895 | last_byte = ALIGN(last_byte , root->sectorsize); |
Yan, Zheng | 8082510 | 2009-11-12 09:35:36 +0000 | [diff] [blame] | 4896 | if (!test_bit(EXTENT_FLAG_PREALLOC, &em->flags)) { |
Josef Bacik | 5dc562c | 2012-08-17 13:14:17 -0400 | [diff] [blame] | 4897 | struct extent_map *hole_em; |
Yan Zheng | 9036c10 | 2008-10-30 14:19:41 -0400 | [diff] [blame] | 4898 | hole_size = last_byte - cur_offset; |
Yan, Zheng | 8082510 | 2009-11-12 09:35:36 +0000 | [diff] [blame] | 4899 | |
Josef Bacik | 16e7549 | 2013-10-22 12:18:51 -0400 | [diff] [blame] | 4900 | err = maybe_insert_hole(root, inode, cur_offset, |
| 4901 | hole_size); |
| 4902 | if (err) |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 4903 | break; |
Josef Bacik | 5dc562c | 2012-08-17 13:14:17 -0400 | [diff] [blame] | 4904 | btrfs_drop_extent_cache(inode, cur_offset, |
| 4905 | cur_offset + hole_size - 1, 0); |
| 4906 | hole_em = alloc_extent_map(); |
| 4907 | if (!hole_em) { |
| 4908 | set_bit(BTRFS_INODE_NEEDS_FULL_SYNC, |
| 4909 | &BTRFS_I(inode)->runtime_flags); |
| 4910 | goto next; |
| 4911 | } |
| 4912 | hole_em->start = cur_offset; |
| 4913 | hole_em->len = hole_size; |
| 4914 | hole_em->orig_start = cur_offset; |
Yan, Zheng | 8082510 | 2009-11-12 09:35:36 +0000 | [diff] [blame] | 4915 | |
Josef Bacik | 5dc562c | 2012-08-17 13:14:17 -0400 | [diff] [blame] | 4916 | hole_em->block_start = EXTENT_MAP_HOLE; |
| 4917 | hole_em->block_len = 0; |
Josef Bacik | b493968 | 2012-12-03 10:31:19 -0500 | [diff] [blame] | 4918 | hole_em->orig_block_len = 0; |
Josef Bacik | cc95bef | 2013-04-04 14:31:27 -0400 | [diff] [blame] | 4919 | hole_em->ram_bytes = hole_size; |
Josef Bacik | 5dc562c | 2012-08-17 13:14:17 -0400 | [diff] [blame] | 4920 | hole_em->bdev = root->fs_info->fs_devices->latest_bdev; |
| 4921 | hole_em->compress_type = BTRFS_COMPRESS_NONE; |
Josef Bacik | 16e7549 | 2013-10-22 12:18:51 -0400 | [diff] [blame] | 4922 | hole_em->generation = root->fs_info->generation; |
Josef Bacik | 5dc562c | 2012-08-17 13:14:17 -0400 | [diff] [blame] | 4923 | |
| 4924 | while (1) { |
| 4925 | write_lock(&em_tree->lock); |
Josef Bacik | 09a2a8f9 | 2013-04-05 16:51:15 -0400 | [diff] [blame] | 4926 | err = add_extent_mapping(em_tree, hole_em, 1); |
Josef Bacik | 5dc562c | 2012-08-17 13:14:17 -0400 | [diff] [blame] | 4927 | write_unlock(&em_tree->lock); |
| 4928 | if (err != -EEXIST) |
| 4929 | break; |
| 4930 | btrfs_drop_extent_cache(inode, cur_offset, |
| 4931 | cur_offset + |
| 4932 | hole_size - 1, 0); |
| 4933 | } |
| 4934 | free_extent_map(hole_em); |
Yan Zheng | 9036c10 | 2008-10-30 14:19:41 -0400 | [diff] [blame] | 4935 | } |
Josef Bacik | 16e7549 | 2013-10-22 12:18:51 -0400 | [diff] [blame] | 4936 | next: |
Yan Zheng | 9036c10 | 2008-10-30 14:19:41 -0400 | [diff] [blame] | 4937 | free_extent_map(em); |
Yan, Zheng | a22285a | 2010-05-16 10:48:46 -0400 | [diff] [blame] | 4938 | em = NULL; |
Yan Zheng | 9036c10 | 2008-10-30 14:19:41 -0400 | [diff] [blame] | 4939 | cur_offset = last_byte; |
Yan, Zheng | 8082510 | 2009-11-12 09:35:36 +0000 | [diff] [blame] | 4940 | if (cur_offset >= block_end) |
Yan Zheng | 9036c10 | 2008-10-30 14:19:41 -0400 | [diff] [blame] | 4941 | break; |
| 4942 | } |
Yan, Zheng | a22285a | 2010-05-16 10:48:46 -0400 | [diff] [blame] | 4943 | free_extent_map(em); |
Josef Bacik | 2ac55d4 | 2010-02-03 19:33:23 +0000 | [diff] [blame] | 4944 | unlock_extent_cached(io_tree, hole_start, block_end - 1, &cached_state, |
| 4945 | GFP_NOFS); |
Yan Zheng | 9036c10 | 2008-10-30 14:19:41 -0400 | [diff] [blame] | 4946 | return err; |
| 4947 | } |
| 4948 | |
Eric Sandeen | 3972f26 | 2013-01-12 02:57:22 +0000 | [diff] [blame] | 4949 | static int btrfs_setsize(struct inode *inode, struct iattr *attr) |
Yan, Zheng | 8082510 | 2009-11-12 09:35:36 +0000 | [diff] [blame] | 4950 | { |
Miao Xie | f4a2f4c | 2011-12-14 20:12:01 -0500 | [diff] [blame] | 4951 | struct btrfs_root *root = BTRFS_I(inode)->root; |
| 4952 | struct btrfs_trans_handle *trans; |
Josef Bacik | a41ad39 | 2011-01-31 15:30:16 -0500 | [diff] [blame] | 4953 | loff_t oldsize = i_size_read(inode); |
Eric Sandeen | 3972f26 | 2013-01-12 02:57:22 +0000 | [diff] [blame] | 4954 | loff_t newsize = attr->ia_size; |
| 4955 | int mask = attr->ia_valid; |
Yan, Zheng | 8082510 | 2009-11-12 09:35:36 +0000 | [diff] [blame] | 4956 | int ret; |
| 4957 | |
Eric Sandeen | 3972f26 | 2013-01-12 02:57:22 +0000 | [diff] [blame] | 4958 | /* |
| 4959 | * The regular truncate() case without ATTR_CTIME and ATTR_MTIME is a |
| 4960 | * special case where we need to update the times despite not having |
| 4961 | * these flags set. For all other operations the VFS set these flags |
| 4962 | * explicitly if it wants a timestamp update. |
| 4963 | */ |
Christoph Hellwig | dff6efc | 2013-11-19 07:17:07 -0800 | [diff] [blame] | 4964 | if (newsize != oldsize) { |
| 4965 | inode_inc_iversion(inode); |
| 4966 | if (!(mask & (ATTR_CTIME | ATTR_MTIME))) |
| 4967 | inode->i_ctime = inode->i_mtime = |
| 4968 | current_fs_time(inode->i_sb); |
| 4969 | } |
Eric Sandeen | 3972f26 | 2013-01-12 02:57:22 +0000 | [diff] [blame] | 4970 | |
Josef Bacik | a41ad39 | 2011-01-31 15:30:16 -0500 | [diff] [blame] | 4971 | if (newsize > oldsize) { |
Filipe Manana | 9ea24bb | 2014-10-29 11:57:59 +0000 | [diff] [blame] | 4972 | /* |
| 4973 | * Don't do an expanding truncate while snapshoting is ongoing. |
| 4974 | * This is to ensure the snapshot captures a fully consistent |
| 4975 | * state of this file - if the snapshot captures this expanding |
| 4976 | * truncation, it must capture all writes that happened before |
| 4977 | * this truncation. |
| 4978 | */ |
Zhao Lei | 0bc19f90 | 2016-01-06 18:56:36 +0800 | [diff] [blame] | 4979 | btrfs_wait_for_snapshot_creation(root); |
Josef Bacik | a41ad39 | 2011-01-31 15:30:16 -0500 | [diff] [blame] | 4980 | ret = btrfs_cont_expand(inode, oldsize, newsize); |
Filipe Manana | 9ea24bb | 2014-10-29 11:57:59 +0000 | [diff] [blame] | 4981 | if (ret) { |
| 4982 | btrfs_end_write_no_snapshoting(root); |
Yan, Zheng | 8082510 | 2009-11-12 09:35:36 +0000 | [diff] [blame] | 4983 | return ret; |
Filipe Manana | 9ea24bb | 2014-10-29 11:57:59 +0000 | [diff] [blame] | 4984 | } |
Yan, Zheng | 8082510 | 2009-11-12 09:35:36 +0000 | [diff] [blame] | 4985 | |
Miao Xie | f4a2f4c | 2011-12-14 20:12:01 -0500 | [diff] [blame] | 4986 | trans = btrfs_start_transaction(root, 1); |
Filipe Manana | 9ea24bb | 2014-10-29 11:57:59 +0000 | [diff] [blame] | 4987 | if (IS_ERR(trans)) { |
| 4988 | btrfs_end_write_no_snapshoting(root); |
Miao Xie | f4a2f4c | 2011-12-14 20:12:01 -0500 | [diff] [blame] | 4989 | return PTR_ERR(trans); |
Filipe Manana | 9ea24bb | 2014-10-29 11:57:59 +0000 | [diff] [blame] | 4990 | } |
Miao Xie | f4a2f4c | 2011-12-14 20:12:01 -0500 | [diff] [blame] | 4991 | |
| 4992 | i_size_write(inode, newsize); |
| 4993 | btrfs_ordered_update_i_size(inode, i_size_read(inode), NULL); |
Chandan Rajendra | 27772b6 | 2016-01-21 15:56:03 +0530 | [diff] [blame] | 4994 | pagecache_isize_extended(inode, oldsize, newsize); |
Miao Xie | f4a2f4c | 2011-12-14 20:12:01 -0500 | [diff] [blame] | 4995 | ret = btrfs_update_inode(trans, root, inode); |
Filipe Manana | 9ea24bb | 2014-10-29 11:57:59 +0000 | [diff] [blame] | 4996 | btrfs_end_write_no_snapshoting(root); |
Josef Bacik | 7ad85bb | 2012-01-12 19:10:12 -0500 | [diff] [blame] | 4997 | btrfs_end_transaction(trans, root); |
Josef Bacik | a41ad39 | 2011-01-31 15:30:16 -0500 | [diff] [blame] | 4998 | } else { |
Yan, Zheng | 8082510 | 2009-11-12 09:35:36 +0000 | [diff] [blame] | 4999 | |
Josef Bacik | a41ad39 | 2011-01-31 15:30:16 -0500 | [diff] [blame] | 5000 | /* |
| 5001 | * We're truncating a file that used to have good data down to |
| 5002 | * zero. Make sure it gets into the ordered flush list so that |
| 5003 | * any new writes get down to disk quickly. |
| 5004 | */ |
| 5005 | if (newsize == 0) |
Josef Bacik | 72ac3c0 | 2012-05-23 14:13:11 -0400 | [diff] [blame] | 5006 | set_bit(BTRFS_INODE_ORDERED_DATA_CLOSE, |
| 5007 | &BTRFS_I(inode)->runtime_flags); |
Yan, Zheng | 8082510 | 2009-11-12 09:35:36 +0000 | [diff] [blame] | 5008 | |
Josef Bacik | f3fe820 | 2013-01-07 17:03:21 -0500 | [diff] [blame] | 5009 | /* |
| 5010 | * 1 for the orphan item we're going to add |
| 5011 | * 1 for the orphan item deletion. |
| 5012 | */ |
| 5013 | trans = btrfs_start_transaction(root, 2); |
| 5014 | if (IS_ERR(trans)) |
| 5015 | return PTR_ERR(trans); |
| 5016 | |
| 5017 | /* |
| 5018 | * We need to do this in case we fail at _any_ point during the |
| 5019 | * actual truncate. Once we do the truncate_setsize we could |
| 5020 | * invalidate pages which forces any outstanding ordered io to |
| 5021 | * be instantly completed which will give us extents that need |
| 5022 | * to be truncated. If we fail to get an orphan inode down we |
| 5023 | * could have left over extents that were never meant to live, |
Nicholas D Steeves | 0132761 | 2016-05-19 21:18:45 -0400 | [diff] [blame] | 5024 | * so we need to guarantee from this point on that everything |
Josef Bacik | f3fe820 | 2013-01-07 17:03:21 -0500 | [diff] [blame] | 5025 | * will be consistent. |
| 5026 | */ |
| 5027 | ret = btrfs_orphan_add(trans, inode); |
| 5028 | btrfs_end_transaction(trans, root); |
| 5029 | if (ret) |
| 5030 | return ret; |
| 5031 | |
Josef Bacik | a41ad39 | 2011-01-31 15:30:16 -0500 | [diff] [blame] | 5032 | /* we don't support swapfiles, so vmtruncate shouldn't fail */ |
| 5033 | truncate_setsize(inode, newsize); |
Miao Xie | 2e60a51 | 2013-02-08 07:01:08 +0000 | [diff] [blame] | 5034 | |
| 5035 | /* Disable nonlocked read DIO to avoid the end less truncate */ |
| 5036 | btrfs_inode_block_unlocked_dio(inode); |
| 5037 | inode_dio_wait(inode); |
| 5038 | btrfs_inode_resume_unlocked_dio(inode); |
| 5039 | |
Josef Bacik | a41ad39 | 2011-01-31 15:30:16 -0500 | [diff] [blame] | 5040 | ret = btrfs_truncate(inode); |
Josef Bacik | 7f4f6e0 | 2013-08-29 16:43:28 -0400 | [diff] [blame] | 5041 | if (ret && inode->i_nlink) { |
| 5042 | int err; |
| 5043 | |
| 5044 | /* |
| 5045 | * failed to truncate, disk_i_size is only adjusted down |
| 5046 | * as we remove extents, so it should represent the true |
| 5047 | * size of the inode, so reset the in memory size and |
| 5048 | * delete our orphan entry. |
| 5049 | */ |
| 5050 | trans = btrfs_join_transaction(root); |
| 5051 | if (IS_ERR(trans)) { |
| 5052 | btrfs_orphan_del(NULL, inode); |
| 5053 | return ret; |
| 5054 | } |
| 5055 | i_size_write(inode, BTRFS_I(inode)->disk_i_size); |
| 5056 | err = btrfs_orphan_del(trans, inode); |
| 5057 | if (err) |
Jeff Mahoney | 6664283 | 2016-06-10 18:19:25 -0400 | [diff] [blame] | 5058 | btrfs_abort_transaction(trans, err); |
Josef Bacik | 7f4f6e0 | 2013-08-29 16:43:28 -0400 | [diff] [blame] | 5059 | btrfs_end_transaction(trans, root); |
| 5060 | } |
Yan, Zheng | 8082510 | 2009-11-12 09:35:36 +0000 | [diff] [blame] | 5061 | } |
| 5062 | |
Josef Bacik | a41ad39 | 2011-01-31 15:30:16 -0500 | [diff] [blame] | 5063 | return ret; |
Yan, Zheng | 8082510 | 2009-11-12 09:35:36 +0000 | [diff] [blame] | 5064 | } |
| 5065 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5066 | static int btrfs_setattr(struct dentry *dentry, struct iattr *attr) |
| 5067 | { |
David Howells | 2b0143b | 2015-03-17 22:25:59 +0000 | [diff] [blame] | 5068 | struct inode *inode = d_inode(dentry); |
Li Zefan | b83cc96 | 2010-12-20 16:04:08 +0800 | [diff] [blame] | 5069 | struct btrfs_root *root = BTRFS_I(inode)->root; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5070 | int err; |
| 5071 | |
Li Zefan | b83cc96 | 2010-12-20 16:04:08 +0800 | [diff] [blame] | 5072 | if (btrfs_root_readonly(root)) |
| 5073 | return -EROFS; |
| 5074 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5075 | err = inode_change_ok(inode, attr); |
| 5076 | if (err) |
| 5077 | return err; |
| 5078 | |
Chris Mason | 5a3f23d | 2009-03-31 13:27:11 -0400 | [diff] [blame] | 5079 | if (S_ISREG(inode->i_mode) && (attr->ia_valid & ATTR_SIZE)) { |
Eric Sandeen | 3972f26 | 2013-01-12 02:57:22 +0000 | [diff] [blame] | 5080 | err = btrfs_setsize(inode, attr); |
Yan, Zheng | 8082510 | 2009-11-12 09:35:36 +0000 | [diff] [blame] | 5081 | if (err) |
| 5082 | return err; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5083 | } |
Yan Zheng | 9036c10 | 2008-10-30 14:19:41 -0400 | [diff] [blame] | 5084 | |
Christoph Hellwig | 1025774 | 2010-06-04 11:30:02 +0200 | [diff] [blame] | 5085 | if (attr->ia_valid) { |
| 5086 | setattr_copy(inode, attr); |
Josef Bacik | 0c4d2d9 | 2012-04-05 15:03:02 -0400 | [diff] [blame] | 5087 | inode_inc_iversion(inode); |
Josef Bacik | 22c44fe | 2011-11-30 10:45:38 -0500 | [diff] [blame] | 5088 | err = btrfs_dirty_inode(inode); |
Josef Bacik | 33268ea | 2008-07-24 12:16:36 -0400 | [diff] [blame] | 5089 | |
Josef Bacik | 22c44fe | 2011-11-30 10:45:38 -0500 | [diff] [blame] | 5090 | if (!err && attr->ia_valid & ATTR_MODE) |
Christoph Hellwig | 996a710 | 2013-12-20 05:16:43 -0800 | [diff] [blame] | 5091 | err = posix_acl_chmod(inode, inode->i_mode); |
Christoph Hellwig | 1025774 | 2010-06-04 11:30:02 +0200 | [diff] [blame] | 5092 | } |
| 5093 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5094 | return err; |
| 5095 | } |
Chris Mason | 61295eb | 2008-01-14 16:24:38 -0500 | [diff] [blame] | 5096 | |
Filipe David Borba Manana | 131e404 | 2013-11-19 22:29:35 +0000 | [diff] [blame] | 5097 | /* |
| 5098 | * While truncating the inode pages during eviction, we get the VFS calling |
| 5099 | * btrfs_invalidatepage() against each page of the inode. This is slow because |
| 5100 | * the calls to btrfs_invalidatepage() result in a huge amount of calls to |
| 5101 | * lock_extent_bits() and clear_extent_bit(), which keep merging and splitting |
| 5102 | * extent_state structures over and over, wasting lots of time. |
| 5103 | * |
| 5104 | * Therefore if the inode is being evicted, let btrfs_invalidatepage() skip all |
| 5105 | * those expensive operations on a per page basis and do only the ordered io |
| 5106 | * finishing, while we release here the extent_map and extent_state structures, |
| 5107 | * without the excessive merging and splitting. |
| 5108 | */ |
| 5109 | static void evict_inode_truncate_pages(struct inode *inode) |
| 5110 | { |
| 5111 | struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree; |
| 5112 | struct extent_map_tree *map_tree = &BTRFS_I(inode)->extent_tree; |
| 5113 | struct rb_node *node; |
| 5114 | |
| 5115 | ASSERT(inode->i_state & I_FREEING); |
Johannes Weiner | 91b0abe | 2014-04-03 14:47:49 -0700 | [diff] [blame] | 5116 | truncate_inode_pages_final(&inode->i_data); |
Filipe David Borba Manana | 131e404 | 2013-11-19 22:29:35 +0000 | [diff] [blame] | 5117 | |
| 5118 | write_lock(&map_tree->lock); |
| 5119 | while (!RB_EMPTY_ROOT(&map_tree->map)) { |
| 5120 | struct extent_map *em; |
| 5121 | |
| 5122 | node = rb_first(&map_tree->map); |
| 5123 | em = rb_entry(node, struct extent_map, rb_node); |
Wang Shilong | 180589e | 2013-12-14 15:27:31 +0800 | [diff] [blame] | 5124 | clear_bit(EXTENT_FLAG_PINNED, &em->flags); |
| 5125 | clear_bit(EXTENT_FLAG_LOGGING, &em->flags); |
Filipe David Borba Manana | 131e404 | 2013-11-19 22:29:35 +0000 | [diff] [blame] | 5126 | remove_extent_mapping(map_tree, em); |
| 5127 | free_extent_map(em); |
Filipe Manana | 7064dd5 | 2014-08-08 02:47:05 +0100 | [diff] [blame] | 5128 | if (need_resched()) { |
| 5129 | write_unlock(&map_tree->lock); |
| 5130 | cond_resched(); |
| 5131 | write_lock(&map_tree->lock); |
| 5132 | } |
Filipe David Borba Manana | 131e404 | 2013-11-19 22:29:35 +0000 | [diff] [blame] | 5133 | } |
| 5134 | write_unlock(&map_tree->lock); |
| 5135 | |
Filipe Manana | 6ca0709 | 2015-05-26 00:55:42 +0100 | [diff] [blame] | 5136 | /* |
| 5137 | * Keep looping until we have no more ranges in the io tree. |
| 5138 | * We can have ongoing bios started by readpages (called from readahead) |
Filipe Manana | 9c6429d | 2015-06-10 12:55:41 +0100 | [diff] [blame] | 5139 | * that have their endio callback (extent_io.c:end_bio_extent_readpage) |
| 5140 | * still in progress (unlocked the pages in the bio but did not yet |
| 5141 | * unlocked the ranges in the io tree). Therefore this means some |
Filipe Manana | 6ca0709 | 2015-05-26 00:55:42 +0100 | [diff] [blame] | 5142 | * ranges can still be locked and eviction started because before |
| 5143 | * submitting those bios, which are executed by a separate task (work |
| 5144 | * queue kthread), inode references (inode->i_count) were not taken |
| 5145 | * (which would be dropped in the end io callback of each bio). |
| 5146 | * Therefore here we effectively end up waiting for those bios and |
| 5147 | * anyone else holding locked ranges without having bumped the inode's |
| 5148 | * reference count - if we don't do it, when they access the inode's |
| 5149 | * io_tree to unlock a range it may be too late, leading to an |
| 5150 | * use-after-free issue. |
| 5151 | */ |
Filipe David Borba Manana | 131e404 | 2013-11-19 22:29:35 +0000 | [diff] [blame] | 5152 | spin_lock(&io_tree->lock); |
| 5153 | while (!RB_EMPTY_ROOT(&io_tree->state)) { |
| 5154 | struct extent_state *state; |
| 5155 | struct extent_state *cached_state = NULL; |
Filipe Manana | 6ca0709 | 2015-05-26 00:55:42 +0100 | [diff] [blame] | 5156 | u64 start; |
| 5157 | u64 end; |
Filipe David Borba Manana | 131e404 | 2013-11-19 22:29:35 +0000 | [diff] [blame] | 5158 | |
| 5159 | node = rb_first(&io_tree->state); |
| 5160 | state = rb_entry(node, struct extent_state, rb_node); |
Filipe Manana | 6ca0709 | 2015-05-26 00:55:42 +0100 | [diff] [blame] | 5161 | start = state->start; |
| 5162 | end = state->end; |
Filipe David Borba Manana | 131e404 | 2013-11-19 22:29:35 +0000 | [diff] [blame] | 5163 | spin_unlock(&io_tree->lock); |
| 5164 | |
David Sterba | ff13db4 | 2015-12-03 14:30:40 +0100 | [diff] [blame] | 5165 | lock_extent_bits(io_tree, start, end, &cached_state); |
Qu Wenruo | b9d0b38 | 2015-09-29 10:35:16 +0800 | [diff] [blame] | 5166 | |
| 5167 | /* |
| 5168 | * If still has DELALLOC flag, the extent didn't reach disk, |
| 5169 | * and its reserved space won't be freed by delayed_ref. |
| 5170 | * So we need to free its reserved space here. |
| 5171 | * (Refer to comment in btrfs_invalidatepage, case 2) |
| 5172 | * |
| 5173 | * Note, end is the bytenr of last byte, so we need + 1 here. |
| 5174 | */ |
| 5175 | if (state->state & EXTENT_DELALLOC) |
| 5176 | btrfs_qgroup_free_data(inode, start, end - start + 1); |
| 5177 | |
Filipe Manana | 6ca0709 | 2015-05-26 00:55:42 +0100 | [diff] [blame] | 5178 | clear_extent_bit(io_tree, start, end, |
Filipe David Borba Manana | 131e404 | 2013-11-19 22:29:35 +0000 | [diff] [blame] | 5179 | EXTENT_LOCKED | EXTENT_DIRTY | |
| 5180 | EXTENT_DELALLOC | EXTENT_DO_ACCOUNTING | |
| 5181 | EXTENT_DEFRAG, 1, 1, |
| 5182 | &cached_state, GFP_NOFS); |
Filipe David Borba Manana | 131e404 | 2013-11-19 22:29:35 +0000 | [diff] [blame] | 5183 | |
Filipe Manana | 7064dd5 | 2014-08-08 02:47:05 +0100 | [diff] [blame] | 5184 | cond_resched(); |
Filipe David Borba Manana | 131e404 | 2013-11-19 22:29:35 +0000 | [diff] [blame] | 5185 | spin_lock(&io_tree->lock); |
| 5186 | } |
| 5187 | spin_unlock(&io_tree->lock); |
| 5188 | } |
| 5189 | |
Al Viro | bd55597 | 2010-06-07 11:35:40 -0400 | [diff] [blame] | 5190 | void btrfs_evict_inode(struct inode *inode) |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5191 | { |
| 5192 | struct btrfs_trans_handle *trans; |
| 5193 | struct btrfs_root *root = BTRFS_I(inode)->root; |
Josef Bacik | 726c35f | 2011-09-26 15:46:06 -0400 | [diff] [blame] | 5194 | struct btrfs_block_rsv *rsv, *global_rsv; |
Josef Bacik | 3bce876 | 2015-02-24 12:35:51 -0800 | [diff] [blame] | 5195 | int steal_from_global = 0; |
Nikolay Borisov | 3d48d98 | 2016-06-29 09:46:41 +0300 | [diff] [blame] | 5196 | u64 min_size; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5197 | int ret; |
| 5198 | |
liubo | 1abe9b8 | 2011-03-24 11:18:59 +0000 | [diff] [blame] | 5199 | trace_btrfs_inode_evict(inode); |
| 5200 | |
Nikolay Borisov | 3d48d98 | 2016-06-29 09:46:41 +0300 | [diff] [blame] | 5201 | if (!root) { |
| 5202 | kmem_cache_free(btrfs_inode_cachep, BTRFS_I(inode)); |
| 5203 | return; |
| 5204 | } |
| 5205 | |
| 5206 | min_size = btrfs_calc_trunc_metadata_size(root, 1); |
| 5207 | |
Filipe David Borba Manana | 131e404 | 2013-11-19 22:29:35 +0000 | [diff] [blame] | 5208 | evict_inode_truncate_pages(inode); |
| 5209 | |
Stefan Behrens | 69e9c6c | 2013-09-05 16:58:43 +0200 | [diff] [blame] | 5210 | if (inode->i_nlink && |
| 5211 | ((btrfs_root_refs(&root->root_item) != 0 && |
| 5212 | root->root_key.objectid != BTRFS_ROOT_TREE_OBJECTID) || |
| 5213 | btrfs_is_free_space_inode(inode))) |
Al Viro | bd55597 | 2010-06-07 11:35:40 -0400 | [diff] [blame] | 5214 | goto no_delete; |
| 5215 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5216 | if (is_bad_inode(inode)) { |
Josef Bacik | 7b12876 | 2008-07-24 12:17:14 -0400 | [diff] [blame] | 5217 | btrfs_orphan_del(NULL, inode); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5218 | goto no_delete; |
| 5219 | } |
Al Viro | bd55597 | 2010-06-07 11:35:40 -0400 | [diff] [blame] | 5220 | /* 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] | 5221 | if (!special_file(inode->i_mode)) |
| 5222 | btrfs_wait_ordered_range(inode, 0, (u64)-1); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 5223 | |
Miao Xie | f612496 | 2014-09-12 18:44:04 +0800 | [diff] [blame] | 5224 | btrfs_free_io_failure_record(inode, 0, (u64)-1); |
| 5225 | |
Yan, Zheng | c71bf09 | 2009-11-12 09:34:40 +0000 | [diff] [blame] | 5226 | if (root->fs_info->log_root_recovering) { |
Liu Bo | 6bf0231 | 2012-06-25 21:59:09 -0600 | [diff] [blame] | 5227 | BUG_ON(test_bit(BTRFS_INODE_HAS_ORPHAN_ITEM, |
Josef Bacik | 8a35d95 | 2012-05-23 14:26:42 -0400 | [diff] [blame] | 5228 | &BTRFS_I(inode)->runtime_flags)); |
Yan, Zheng | c71bf09 | 2009-11-12 09:34:40 +0000 | [diff] [blame] | 5229 | goto no_delete; |
| 5230 | } |
| 5231 | |
Yan, Zheng | 76dda93 | 2009-09-21 16:00:26 -0400 | [diff] [blame] | 5232 | if (inode->i_nlink > 0) { |
Stefan Behrens | 69e9c6c | 2013-09-05 16:58:43 +0200 | [diff] [blame] | 5233 | BUG_ON(btrfs_root_refs(&root->root_item) != 0 && |
| 5234 | root->root_key.objectid != BTRFS_ROOT_TREE_OBJECTID); |
Yan, Zheng | 76dda93 | 2009-09-21 16:00:26 -0400 | [diff] [blame] | 5235 | goto no_delete; |
| 5236 | } |
| 5237 | |
Miao Xie | 0e8c36a | 2012-12-19 06:59:51 +0000 | [diff] [blame] | 5238 | ret = btrfs_commit_inode_delayed_inode(inode); |
| 5239 | if (ret) { |
| 5240 | btrfs_orphan_del(NULL, inode); |
| 5241 | goto no_delete; |
| 5242 | } |
| 5243 | |
Miao Xie | 66d8f3d | 2012-09-06 04:02:28 -0600 | [diff] [blame] | 5244 | rsv = btrfs_alloc_block_rsv(root, BTRFS_BLOCK_RSV_TEMP); |
Josef Bacik | 4289a66 | 2011-08-05 13:22:24 -0400 | [diff] [blame] | 5245 | if (!rsv) { |
| 5246 | btrfs_orphan_del(NULL, inode); |
| 5247 | goto no_delete; |
| 5248 | } |
Josef Bacik | 4a33854 | 2011-08-29 11:01:31 -0400 | [diff] [blame] | 5249 | rsv->size = min_size; |
Josef Bacik | ca7e70f | 2012-08-27 17:48:15 -0400 | [diff] [blame] | 5250 | rsv->failfast = 1; |
Josef Bacik | 726c35f | 2011-09-26 15:46:06 -0400 | [diff] [blame] | 5251 | global_rsv = &root->fs_info->global_block_rsv; |
Josef Bacik | 4289a66 | 2011-08-05 13:22:24 -0400 | [diff] [blame] | 5252 | |
Chris Mason | dbe674a | 2008-07-17 12:54:05 -0400 | [diff] [blame] | 5253 | btrfs_i_size_write(inode, 0); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 5254 | |
Josef Bacik | 4289a66 | 2011-08-05 13:22:24 -0400 | [diff] [blame] | 5255 | /* |
Miao Xie | 8407aa4 | 2012-09-07 01:43:32 -0600 | [diff] [blame] | 5256 | * This is a bit simpler than btrfs_truncate since we've already |
| 5257 | * reserved our space for our orphan item in the unlink, so we just |
| 5258 | * need to reserve some slack space in case we add bytes and update |
| 5259 | * inode item when doing the truncate. |
Josef Bacik | 4289a66 | 2011-08-05 13:22:24 -0400 | [diff] [blame] | 5260 | */ |
Yan, Zheng | 8082510 | 2009-11-12 09:35:36 +0000 | [diff] [blame] | 5261 | while (1) { |
Miao Xie | 08e007d | 2012-10-16 11:33:38 +0000 | [diff] [blame] | 5262 | ret = btrfs_block_rsv_refill(root, rsv, min_size, |
| 5263 | BTRFS_RESERVE_FLUSH_LIMIT); |
Yan, Zheng | 8082510 | 2009-11-12 09:35:36 +0000 | [diff] [blame] | 5264 | |
Josef Bacik | 726c35f | 2011-09-26 15:46:06 -0400 | [diff] [blame] | 5265 | /* |
| 5266 | * Try and steal from the global reserve since we will |
| 5267 | * likely not use this space anyway, we want to try as |
| 5268 | * hard as possible to get this to work. |
| 5269 | */ |
| 5270 | if (ret) |
Josef Bacik | 3bce876 | 2015-02-24 12:35:51 -0800 | [diff] [blame] | 5271 | steal_from_global++; |
| 5272 | else |
| 5273 | steal_from_global = 0; |
| 5274 | ret = 0; |
Josef Bacik | 726c35f | 2011-09-26 15:46:06 -0400 | [diff] [blame] | 5275 | |
Josef Bacik | 3bce876 | 2015-02-24 12:35:51 -0800 | [diff] [blame] | 5276 | /* |
| 5277 | * steal_from_global == 0: we reserved stuff, hooray! |
| 5278 | * steal_from_global == 1: we didn't reserve stuff, boo! |
| 5279 | * steal_from_global == 2: we've committed, still not a lot of |
| 5280 | * room but maybe we'll have room in the global reserve this |
| 5281 | * time. |
| 5282 | * steal_from_global == 3: abandon all hope! |
| 5283 | */ |
| 5284 | if (steal_from_global > 2) { |
Simon Kirby | c2cf52e | 2013-03-19 22:41:23 +0000 | [diff] [blame] | 5285 | btrfs_warn(root->fs_info, |
| 5286 | "Could not get space for a delete, will truncate on mount %d", |
| 5287 | ret); |
Josef Bacik | 4289a66 | 2011-08-05 13:22:24 -0400 | [diff] [blame] | 5288 | btrfs_orphan_del(NULL, inode); |
| 5289 | btrfs_free_block_rsv(root, rsv); |
| 5290 | goto no_delete; |
Yan, Zheng | d68fc57 | 2010-05-16 10:49:58 -0400 | [diff] [blame] | 5291 | } |
| 5292 | |
Miao Xie | 0e8c36a | 2012-12-19 06:59:51 +0000 | [diff] [blame] | 5293 | trans = btrfs_join_transaction(root); |
Josef Bacik | 4289a66 | 2011-08-05 13:22:24 -0400 | [diff] [blame] | 5294 | if (IS_ERR(trans)) { |
| 5295 | btrfs_orphan_del(NULL, inode); |
| 5296 | btrfs_free_block_rsv(root, rsv); |
| 5297 | goto no_delete; |
| 5298 | } |
| 5299 | |
Josef Bacik | 3bce876 | 2015-02-24 12:35:51 -0800 | [diff] [blame] | 5300 | /* |
Nicholas D Steeves | 0132761 | 2016-05-19 21:18:45 -0400 | [diff] [blame] | 5301 | * 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] | 5302 | * sure there is room to do it, if not we need to commit and try |
| 5303 | * again. |
| 5304 | */ |
| 5305 | if (steal_from_global) { |
| 5306 | if (!btrfs_check_space_for_delayed_refs(trans, root)) |
| 5307 | ret = btrfs_block_rsv_migrate(global_rsv, rsv, |
Josef Bacik | 25d609f | 2016-03-25 13:25:48 -0400 | [diff] [blame] | 5308 | min_size, 0); |
Josef Bacik | 3bce876 | 2015-02-24 12:35:51 -0800 | [diff] [blame] | 5309 | else |
| 5310 | ret = -ENOSPC; |
| 5311 | } |
| 5312 | |
| 5313 | /* |
| 5314 | * Couldn't steal from the global reserve, we have too much |
| 5315 | * pending stuff built up, commit the transaction and try it |
| 5316 | * again. |
| 5317 | */ |
| 5318 | if (ret) { |
| 5319 | ret = btrfs_commit_transaction(trans, root); |
| 5320 | if (ret) { |
| 5321 | btrfs_orphan_del(NULL, inode); |
| 5322 | btrfs_free_block_rsv(root, rsv); |
| 5323 | goto no_delete; |
| 5324 | } |
| 5325 | continue; |
| 5326 | } else { |
| 5327 | steal_from_global = 0; |
| 5328 | } |
| 5329 | |
Josef Bacik | 4289a66 | 2011-08-05 13:22:24 -0400 | [diff] [blame] | 5330 | trans->block_rsv = rsv; |
| 5331 | |
Yan, Zheng | d68fc57 | 2010-05-16 10:49:58 -0400 | [diff] [blame] | 5332 | ret = btrfs_truncate_inode_items(trans, root, inode, 0, 0); |
Chris Mason | 28ed134 | 2014-12-17 09:41:04 -0800 | [diff] [blame] | 5333 | if (ret != -ENOSPC && ret != -EAGAIN) |
Yan, Zheng | 8082510 | 2009-11-12 09:35:36 +0000 | [diff] [blame] | 5334 | break; |
| 5335 | |
Miao Xie | 8407aa4 | 2012-09-07 01:43:32 -0600 | [diff] [blame] | 5336 | trans->block_rsv = &root->fs_info->trans_block_rsv; |
Yan, Zheng | 8082510 | 2009-11-12 09:35:36 +0000 | [diff] [blame] | 5337 | btrfs_end_transaction(trans, root); |
| 5338 | trans = NULL; |
Liu Bo | b53d3f5 | 2012-11-14 14:34:34 +0000 | [diff] [blame] | 5339 | btrfs_btree_balance_dirty(root); |
Josef Bacik | 7b12876 | 2008-07-24 12:17:14 -0400 | [diff] [blame] | 5340 | } |
| 5341 | |
Josef Bacik | 4289a66 | 2011-08-05 13:22:24 -0400 | [diff] [blame] | 5342 | btrfs_free_block_rsv(root, rsv); |
| 5343 | |
Josef Bacik | 4ef31a4 | 2013-08-13 14:10:08 -0400 | [diff] [blame] | 5344 | /* |
| 5345 | * Errors here aren't a big deal, it just means we leave orphan items |
| 5346 | * in the tree. They will be cleaned up on the next mount. |
| 5347 | */ |
Yan, Zheng | 8082510 | 2009-11-12 09:35:36 +0000 | [diff] [blame] | 5348 | if (ret == 0) { |
Josef Bacik | 4289a66 | 2011-08-05 13:22:24 -0400 | [diff] [blame] | 5349 | trans->block_rsv = root->orphan_block_rsv; |
Josef Bacik | 4ef31a4 | 2013-08-13 14:10:08 -0400 | [diff] [blame] | 5350 | btrfs_orphan_del(trans, inode); |
| 5351 | } else { |
| 5352 | btrfs_orphan_del(NULL, inode); |
Yan, Zheng | 8082510 | 2009-11-12 09:35:36 +0000 | [diff] [blame] | 5353 | } |
Chris Mason | 85e21ba | 2008-01-29 15:11:36 -0500 | [diff] [blame] | 5354 | |
Josef Bacik | 4289a66 | 2011-08-05 13:22:24 -0400 | [diff] [blame] | 5355 | trans->block_rsv = &root->fs_info->trans_block_rsv; |
Li Zefan | 581bb05 | 2011-04-20 10:06:11 +0800 | [diff] [blame] | 5356 | if (!(root == root->fs_info->tree_root || |
| 5357 | root->root_key.objectid == BTRFS_TREE_RELOC_OBJECTID)) |
Li Zefan | 33345d01 | 2011-04-20 10:31:50 +0800 | [diff] [blame] | 5358 | btrfs_return_ino(root, btrfs_ino(inode)); |
Li Zefan | 581bb05 | 2011-04-20 10:06:11 +0800 | [diff] [blame] | 5359 | |
Chris Mason | 54aa1f4 | 2007-06-22 14:16:25 -0400 | [diff] [blame] | 5360 | btrfs_end_transaction(trans, root); |
Liu Bo | b53d3f5 | 2012-11-14 14:34:34 +0000 | [diff] [blame] | 5361 | btrfs_btree_balance_dirty(root); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5362 | no_delete: |
Miao Xie | 89042e5 | 2013-05-15 07:48:15 +0000 | [diff] [blame] | 5363 | btrfs_remove_delayed_node(inode); |
Jan Kara | dbd5768 | 2012-05-03 14:48:02 +0200 | [diff] [blame] | 5364 | clear_inode(inode); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5365 | } |
| 5366 | |
| 5367 | /* |
| 5368 | * this returns the key found in the dir entry in the location pointer. |
| 5369 | * If no dir entries were found, location->objectid is 0. |
| 5370 | */ |
| 5371 | static int btrfs_inode_by_name(struct inode *dir, struct dentry *dentry, |
| 5372 | struct btrfs_key *location) |
| 5373 | { |
| 5374 | const char *name = dentry->d_name.name; |
| 5375 | int namelen = dentry->d_name.len; |
| 5376 | struct btrfs_dir_item *di; |
| 5377 | struct btrfs_path *path; |
| 5378 | struct btrfs_root *root = BTRFS_I(dir)->root; |
Yan | 0d9f7f3 | 2007-10-25 15:48:28 -0400 | [diff] [blame] | 5379 | int ret = 0; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5380 | |
| 5381 | path = btrfs_alloc_path(); |
Mark Fasheh | d8926bb | 2011-07-13 10:38:47 -0700 | [diff] [blame] | 5382 | if (!path) |
| 5383 | return -ENOMEM; |
Chris Mason | 3954401 | 2007-12-12 14:38:19 -0500 | [diff] [blame] | 5384 | |
Li Zefan | 33345d01 | 2011-04-20 10:31:50 +0800 | [diff] [blame] | 5385 | di = btrfs_lookup_dir_item(NULL, root, path, btrfs_ino(dir), name, |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5386 | namelen, 0); |
Yan | 0d9f7f3 | 2007-10-25 15:48:28 -0400 | [diff] [blame] | 5387 | if (IS_ERR(di)) |
| 5388 | ret = PTR_ERR(di); |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 5389 | |
David Sterba | c704005 | 2011-04-19 18:00:01 +0200 | [diff] [blame] | 5390 | if (IS_ERR_OR_NULL(di)) |
Chris Mason | 3954401 | 2007-12-12 14:38:19 -0500 | [diff] [blame] | 5391 | goto out_err; |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 5392 | |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 5393 | btrfs_dir_item_key_to_cpu(path->nodes[0], di, location); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5394 | out: |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5395 | btrfs_free_path(path); |
| 5396 | return ret; |
Chris Mason | 3954401 | 2007-12-12 14:38:19 -0500 | [diff] [blame] | 5397 | out_err: |
| 5398 | location->objectid = 0; |
| 5399 | goto out; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5400 | } |
| 5401 | |
| 5402 | /* |
| 5403 | * when we hit a tree root in a directory, the btrfs part of the inode |
| 5404 | * needs to be changed to reflect the root directory of the tree root. This |
| 5405 | * is kind of like crossing a mount point. |
| 5406 | */ |
| 5407 | static int fixup_tree_root_location(struct btrfs_root *root, |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 5408 | struct inode *dir, |
| 5409 | struct dentry *dentry, |
| 5410 | struct btrfs_key *location, |
| 5411 | struct btrfs_root **sub_root) |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5412 | { |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 5413 | struct btrfs_path *path; |
| 5414 | struct btrfs_root *new_root; |
| 5415 | struct btrfs_root_ref *ref; |
| 5416 | struct extent_buffer *leaf; |
David Sterba | 1d4c08e | 2015-01-02 19:36:14 +0100 | [diff] [blame] | 5417 | struct btrfs_key key; |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 5418 | int ret; |
| 5419 | int err = 0; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5420 | |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 5421 | path = btrfs_alloc_path(); |
| 5422 | if (!path) { |
| 5423 | err = -ENOMEM; |
| 5424 | goto out; |
| 5425 | } |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5426 | |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 5427 | err = -ENOENT; |
David Sterba | 1d4c08e | 2015-01-02 19:36:14 +0100 | [diff] [blame] | 5428 | key.objectid = BTRFS_I(dir)->root->root_key.objectid; |
| 5429 | key.type = BTRFS_ROOT_REF_KEY; |
| 5430 | key.offset = location->objectid; |
| 5431 | |
| 5432 | ret = btrfs_search_slot(NULL, root->fs_info->tree_root, &key, path, |
| 5433 | 0, 0); |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 5434 | if (ret) { |
| 5435 | if (ret < 0) |
| 5436 | err = ret; |
| 5437 | goto out; |
| 5438 | } |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5439 | |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 5440 | leaf = path->nodes[0]; |
| 5441 | ref = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_root_ref); |
Li Zefan | 33345d01 | 2011-04-20 10:31:50 +0800 | [diff] [blame] | 5442 | if (btrfs_root_ref_dirid(leaf, ref) != btrfs_ino(dir) || |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 5443 | btrfs_root_ref_name_len(leaf, ref) != dentry->d_name.len) |
| 5444 | goto out; |
| 5445 | |
| 5446 | ret = memcmp_extent_buffer(leaf, dentry->d_name.name, |
| 5447 | (unsigned long)(ref + 1), |
| 5448 | dentry->d_name.len); |
| 5449 | if (ret) |
| 5450 | goto out; |
| 5451 | |
David Sterba | b3b4aa7 | 2011-04-21 01:20:15 +0200 | [diff] [blame] | 5452 | btrfs_release_path(path); |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 5453 | |
| 5454 | new_root = btrfs_read_fs_root_no_name(root->fs_info, location); |
| 5455 | if (IS_ERR(new_root)) { |
| 5456 | err = PTR_ERR(new_root); |
| 5457 | goto out; |
| 5458 | } |
| 5459 | |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 5460 | *sub_root = new_root; |
| 5461 | location->objectid = btrfs_root_dirid(&new_root->root_item); |
| 5462 | location->type = BTRFS_INODE_ITEM_KEY; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5463 | location->offset = 0; |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 5464 | err = 0; |
| 5465 | out: |
| 5466 | btrfs_free_path(path); |
| 5467 | return err; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5468 | } |
| 5469 | |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 5470 | static void inode_tree_add(struct inode *inode) |
| 5471 | { |
| 5472 | struct btrfs_root *root = BTRFS_I(inode)->root; |
| 5473 | struct btrfs_inode *entry; |
From: Nick Piggin | 03e860b | 2009-08-21 10:09:44 +0200 | [diff] [blame] | 5474 | struct rb_node **p; |
| 5475 | struct rb_node *parent; |
Filipe David Borba Manana | cef2193 | 2013-09-02 12:19:13 +0100 | [diff] [blame] | 5476 | struct rb_node *new = &BTRFS_I(inode)->rb_node; |
Li Zefan | 33345d01 | 2011-04-20 10:31:50 +0800 | [diff] [blame] | 5477 | u64 ino = btrfs_ino(inode); |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 5478 | |
Al Viro | 1d3382cb | 2010-10-23 15:19:20 -0400 | [diff] [blame] | 5479 | if (inode_unhashed(inode)) |
Yan, Zheng | 76dda93 | 2009-09-21 16:00:26 -0400 | [diff] [blame] | 5480 | return; |
Miao Xie | e1409ce | 2013-05-15 07:48:16 +0000 | [diff] [blame] | 5481 | parent = NULL; |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 5482 | spin_lock(&root->inode_lock); |
Miao Xie | e1409ce | 2013-05-15 07:48:16 +0000 | [diff] [blame] | 5483 | p = &root->inode_tree.rb_node; |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 5484 | while (*p) { |
| 5485 | parent = *p; |
| 5486 | entry = rb_entry(parent, struct btrfs_inode, rb_node); |
| 5487 | |
Li Zefan | 33345d01 | 2011-04-20 10:31:50 +0800 | [diff] [blame] | 5488 | if (ino < btrfs_ino(&entry->vfs_inode)) |
From: Nick Piggin | 03e860b | 2009-08-21 10:09:44 +0200 | [diff] [blame] | 5489 | p = &parent->rb_left; |
Li Zefan | 33345d01 | 2011-04-20 10:31:50 +0800 | [diff] [blame] | 5490 | else if (ino > btrfs_ino(&entry->vfs_inode)) |
From: Nick Piggin | 03e860b | 2009-08-21 10:09:44 +0200 | [diff] [blame] | 5491 | p = &parent->rb_right; |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 5492 | else { |
| 5493 | WARN_ON(!(entry->vfs_inode.i_state & |
Al Viro | a4ffdde | 2010-06-02 17:38:30 -0400 | [diff] [blame] | 5494 | (I_WILL_FREE | I_FREEING))); |
Filipe David Borba Manana | cef2193 | 2013-09-02 12:19:13 +0100 | [diff] [blame] | 5495 | rb_replace_node(parent, new, &root->inode_tree); |
From: Nick Piggin | 03e860b | 2009-08-21 10:09:44 +0200 | [diff] [blame] | 5496 | RB_CLEAR_NODE(parent); |
| 5497 | spin_unlock(&root->inode_lock); |
Filipe David Borba Manana | cef2193 | 2013-09-02 12:19:13 +0100 | [diff] [blame] | 5498 | return; |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 5499 | } |
| 5500 | } |
Filipe David Borba Manana | cef2193 | 2013-09-02 12:19:13 +0100 | [diff] [blame] | 5501 | rb_link_node(new, parent, p); |
| 5502 | rb_insert_color(new, &root->inode_tree); |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 5503 | spin_unlock(&root->inode_lock); |
| 5504 | } |
| 5505 | |
| 5506 | static void inode_tree_del(struct inode *inode) |
| 5507 | { |
| 5508 | struct btrfs_root *root = BTRFS_I(inode)->root; |
Yan, Zheng | 76dda93 | 2009-09-21 16:00:26 -0400 | [diff] [blame] | 5509 | int empty = 0; |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 5510 | |
From: Nick Piggin | 03e860b | 2009-08-21 10:09:44 +0200 | [diff] [blame] | 5511 | spin_lock(&root->inode_lock); |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 5512 | if (!RB_EMPTY_NODE(&BTRFS_I(inode)->rb_node)) { |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 5513 | rb_erase(&BTRFS_I(inode)->rb_node, &root->inode_tree); |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 5514 | RB_CLEAR_NODE(&BTRFS_I(inode)->rb_node); |
Yan, Zheng | 76dda93 | 2009-09-21 16:00:26 -0400 | [diff] [blame] | 5515 | empty = RB_EMPTY_ROOT(&root->inode_tree); |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 5516 | } |
From: Nick Piggin | 03e860b | 2009-08-21 10:09:44 +0200 | [diff] [blame] | 5517 | spin_unlock(&root->inode_lock); |
Yan, Zheng | 76dda93 | 2009-09-21 16:00:26 -0400 | [diff] [blame] | 5518 | |
Stefan Behrens | 69e9c6c | 2013-09-05 16:58:43 +0200 | [diff] [blame] | 5519 | if (empty && btrfs_root_refs(&root->root_item) == 0) { |
Yan, Zheng | 76dda93 | 2009-09-21 16:00:26 -0400 | [diff] [blame] | 5520 | synchronize_srcu(&root->fs_info->subvol_srcu); |
| 5521 | spin_lock(&root->inode_lock); |
| 5522 | empty = RB_EMPTY_ROOT(&root->inode_tree); |
| 5523 | spin_unlock(&root->inode_lock); |
| 5524 | if (empty) |
| 5525 | btrfs_add_dead_root(root); |
| 5526 | } |
| 5527 | } |
| 5528 | |
Jeff Mahoney | 143bede | 2012-03-01 14:56:26 +0100 | [diff] [blame] | 5529 | void btrfs_invalidate_inodes(struct btrfs_root *root) |
Yan, Zheng | 76dda93 | 2009-09-21 16:00:26 -0400 | [diff] [blame] | 5530 | { |
| 5531 | struct rb_node *node; |
| 5532 | struct rb_node *prev; |
| 5533 | struct btrfs_inode *entry; |
| 5534 | struct inode *inode; |
| 5535 | u64 objectid = 0; |
| 5536 | |
Liu Bo | 7813b3d | 2014-02-10 17:37:25 +0800 | [diff] [blame] | 5537 | if (!test_bit(BTRFS_FS_STATE_ERROR, &root->fs_info->fs_state)) |
| 5538 | WARN_ON(btrfs_root_refs(&root->root_item) != 0); |
Yan, Zheng | 76dda93 | 2009-09-21 16:00:26 -0400 | [diff] [blame] | 5539 | |
| 5540 | spin_lock(&root->inode_lock); |
| 5541 | again: |
| 5542 | node = root->inode_tree.rb_node; |
| 5543 | prev = NULL; |
| 5544 | while (node) { |
| 5545 | prev = node; |
| 5546 | entry = rb_entry(node, struct btrfs_inode, rb_node); |
| 5547 | |
Li Zefan | 33345d01 | 2011-04-20 10:31:50 +0800 | [diff] [blame] | 5548 | if (objectid < btrfs_ino(&entry->vfs_inode)) |
Yan, Zheng | 76dda93 | 2009-09-21 16:00:26 -0400 | [diff] [blame] | 5549 | node = node->rb_left; |
Li Zefan | 33345d01 | 2011-04-20 10:31:50 +0800 | [diff] [blame] | 5550 | else if (objectid > btrfs_ino(&entry->vfs_inode)) |
Yan, Zheng | 76dda93 | 2009-09-21 16:00:26 -0400 | [diff] [blame] | 5551 | node = node->rb_right; |
| 5552 | else |
| 5553 | break; |
| 5554 | } |
| 5555 | if (!node) { |
| 5556 | while (prev) { |
| 5557 | entry = rb_entry(prev, struct btrfs_inode, rb_node); |
Li Zefan | 33345d01 | 2011-04-20 10:31:50 +0800 | [diff] [blame] | 5558 | if (objectid <= btrfs_ino(&entry->vfs_inode)) { |
Yan, Zheng | 76dda93 | 2009-09-21 16:00:26 -0400 | [diff] [blame] | 5559 | node = prev; |
| 5560 | break; |
| 5561 | } |
| 5562 | prev = rb_next(prev); |
| 5563 | } |
| 5564 | } |
| 5565 | while (node) { |
| 5566 | entry = rb_entry(node, struct btrfs_inode, rb_node); |
Li Zefan | 33345d01 | 2011-04-20 10:31:50 +0800 | [diff] [blame] | 5567 | objectid = btrfs_ino(&entry->vfs_inode) + 1; |
Yan, Zheng | 76dda93 | 2009-09-21 16:00:26 -0400 | [diff] [blame] | 5568 | inode = igrab(&entry->vfs_inode); |
| 5569 | if (inode) { |
| 5570 | spin_unlock(&root->inode_lock); |
| 5571 | if (atomic_read(&inode->i_count) > 1) |
| 5572 | d_prune_aliases(inode); |
| 5573 | /* |
Al Viro | 45321ac | 2010-06-07 13:43:19 -0400 | [diff] [blame] | 5574 | * btrfs_drop_inode will have it removed from |
Yan, Zheng | 76dda93 | 2009-09-21 16:00:26 -0400 | [diff] [blame] | 5575 | * the inode cache when its usage count |
| 5576 | * hits zero. |
| 5577 | */ |
| 5578 | iput(inode); |
| 5579 | cond_resched(); |
| 5580 | spin_lock(&root->inode_lock); |
| 5581 | goto again; |
| 5582 | } |
| 5583 | |
| 5584 | if (cond_resched_lock(&root->inode_lock)) |
| 5585 | goto again; |
| 5586 | |
| 5587 | node = rb_next(node); |
| 5588 | } |
| 5589 | spin_unlock(&root->inode_lock); |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 5590 | } |
| 5591 | |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 5592 | static int btrfs_init_locked_inode(struct inode *inode, void *p) |
| 5593 | { |
| 5594 | struct btrfs_iget_args *args = p; |
Chris Mason | 90d3e59 | 2014-01-09 17:28:00 -0800 | [diff] [blame] | 5595 | inode->i_ino = args->location->objectid; |
| 5596 | memcpy(&BTRFS_I(inode)->location, args->location, |
| 5597 | sizeof(*args->location)); |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 5598 | BTRFS_I(inode)->root = args->root; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5599 | return 0; |
| 5600 | } |
| 5601 | |
| 5602 | static int btrfs_find_actor(struct inode *inode, void *opaque) |
| 5603 | { |
| 5604 | struct btrfs_iget_args *args = opaque; |
Chris Mason | 90d3e59 | 2014-01-09 17:28:00 -0800 | [diff] [blame] | 5605 | return args->location->objectid == BTRFS_I(inode)->location.objectid && |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 5606 | args->root == BTRFS_I(inode)->root; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5607 | } |
| 5608 | |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 5609 | static struct inode *btrfs_iget_locked(struct super_block *s, |
Chris Mason | 90d3e59 | 2014-01-09 17:28:00 -0800 | [diff] [blame] | 5610 | struct btrfs_key *location, |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 5611 | struct btrfs_root *root) |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5612 | { |
| 5613 | struct inode *inode; |
| 5614 | struct btrfs_iget_args args; |
Chris Mason | 90d3e59 | 2014-01-09 17:28:00 -0800 | [diff] [blame] | 5615 | unsigned long hashval = btrfs_inode_hash(location->objectid, root); |
Filipe David Borba Manana | 778ba82 | 2013-10-06 22:22:33 +0100 | [diff] [blame] | 5616 | |
Chris Mason | 90d3e59 | 2014-01-09 17:28:00 -0800 | [diff] [blame] | 5617 | args.location = location; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5618 | args.root = root; |
| 5619 | |
Filipe David Borba Manana | 778ba82 | 2013-10-06 22:22:33 +0100 | [diff] [blame] | 5620 | inode = iget5_locked(s, hashval, btrfs_find_actor, |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5621 | btrfs_init_locked_inode, |
| 5622 | (void *)&args); |
| 5623 | return inode; |
| 5624 | } |
| 5625 | |
Balaji Rao | 1a54ef8 | 2008-07-21 02:01:04 +0530 | [diff] [blame] | 5626 | /* Get an inode object given its location and corresponding root. |
| 5627 | * Returns in *is_new if the inode was read from disk |
| 5628 | */ |
| 5629 | struct inode *btrfs_iget(struct super_block *s, struct btrfs_key *location, |
Josef Bacik | 73f7341 | 2009-12-04 17:38:27 +0000 | [diff] [blame] | 5630 | struct btrfs_root *root, int *new) |
Balaji Rao | 1a54ef8 | 2008-07-21 02:01:04 +0530 | [diff] [blame] | 5631 | { |
| 5632 | struct inode *inode; |
| 5633 | |
Chris Mason | 90d3e59 | 2014-01-09 17:28:00 -0800 | [diff] [blame] | 5634 | inode = btrfs_iget_locked(s, location, root); |
Balaji Rao | 1a54ef8 | 2008-07-21 02:01:04 +0530 | [diff] [blame] | 5635 | if (!inode) |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 5636 | return ERR_PTR(-ENOMEM); |
Balaji Rao | 1a54ef8 | 2008-07-21 02:01:04 +0530 | [diff] [blame] | 5637 | |
| 5638 | if (inode->i_state & I_NEW) { |
Filipe Manana | 6771089 | 2016-06-06 11:51:25 +0100 | [diff] [blame] | 5639 | int ret; |
| 5640 | |
| 5641 | ret = btrfs_read_locked_inode(inode); |
Mark Fasheh | 1748f84 | 2011-07-12 11:25:31 -0700 | [diff] [blame] | 5642 | if (!is_bad_inode(inode)) { |
| 5643 | inode_tree_add(inode); |
| 5644 | unlock_new_inode(inode); |
| 5645 | if (new) |
| 5646 | *new = 1; |
| 5647 | } else { |
Sergei Trofimovich | e0b6d65 | 2011-09-11 10:52:24 -0400 | [diff] [blame] | 5648 | unlock_new_inode(inode); |
| 5649 | iput(inode); |
Filipe Manana | 6771089 | 2016-06-06 11:51:25 +0100 | [diff] [blame] | 5650 | ASSERT(ret < 0); |
| 5651 | inode = ERR_PTR(ret < 0 ? ret : -ESTALE); |
Mark Fasheh | 1748f84 | 2011-07-12 11:25:31 -0700 | [diff] [blame] | 5652 | } |
| 5653 | } |
| 5654 | |
Balaji Rao | 1a54ef8 | 2008-07-21 02:01:04 +0530 | [diff] [blame] | 5655 | return inode; |
| 5656 | } |
| 5657 | |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 5658 | static struct inode *new_simple_dir(struct super_block *s, |
| 5659 | struct btrfs_key *key, |
| 5660 | struct btrfs_root *root) |
| 5661 | { |
| 5662 | struct inode *inode = new_inode(s); |
| 5663 | |
| 5664 | if (!inode) |
| 5665 | return ERR_PTR(-ENOMEM); |
| 5666 | |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 5667 | BTRFS_I(inode)->root = root; |
| 5668 | memcpy(&BTRFS_I(inode)->location, key, sizeof(*key)); |
Josef Bacik | 72ac3c0 | 2012-05-23 14:13:11 -0400 | [diff] [blame] | 5669 | set_bit(BTRFS_INODE_DUMMY, &BTRFS_I(inode)->runtime_flags); |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 5670 | |
| 5671 | inode->i_ino = BTRFS_EMPTY_SUBVOL_DIR_OBJECTID; |
Li Zefan | 848cce0 | 2012-02-21 17:04:28 +0800 | [diff] [blame] | 5672 | inode->i_op = &btrfs_dir_ro_inode_operations; |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 5673 | inode->i_fop = &simple_dir_operations; |
| 5674 | inode->i_mode = S_IFDIR | S_IRUGO | S_IWUSR | S_IXUGO; |
Deepa Dinamani | 04b285f | 2016-02-06 23:57:21 -0800 | [diff] [blame] | 5675 | inode->i_mtime = current_fs_time(inode->i_sb); |
chandan r | 9cc97d6 | 2012-07-04 12:48:07 +0530 | [diff] [blame] | 5676 | inode->i_atime = inode->i_mtime; |
| 5677 | inode->i_ctime = inode->i_mtime; |
| 5678 | BTRFS_I(inode)->i_otime = inode->i_mtime; |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 5679 | |
| 5680 | return inode; |
| 5681 | } |
| 5682 | |
Chris Mason | 3de4586 | 2008-11-17 21:02:50 -0500 | [diff] [blame] | 5683 | struct inode *btrfs_lookup_dentry(struct inode *dir, struct dentry *dentry) |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5684 | { |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 5685 | struct inode *inode; |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 5686 | struct btrfs_root *root = BTRFS_I(dir)->root; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5687 | struct btrfs_root *sub_root = root; |
| 5688 | struct btrfs_key location; |
Yan, Zheng | 76dda93 | 2009-09-21 16:00:26 -0400 | [diff] [blame] | 5689 | int index; |
Josef Bacik | b4aff1f | 2011-06-28 16:18:59 -0400 | [diff] [blame] | 5690 | int ret = 0; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5691 | |
| 5692 | if (dentry->d_name.len > BTRFS_NAME_LEN) |
| 5693 | return ERR_PTR(-ENAMETOOLONG); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 5694 | |
Jeff Layton | 39e3c95 | 2012-11-28 11:30:53 -0500 | [diff] [blame] | 5695 | ret = btrfs_inode_by_name(dir, dentry, &location); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5696 | if (ret < 0) |
| 5697 | return ERR_PTR(ret); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 5698 | |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 5699 | if (location.objectid == 0) |
Tsutomu Itoh | 5662344b3 | 2013-12-13 09:51:42 +0900 | [diff] [blame] | 5700 | return ERR_PTR(-ENOENT); |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 5701 | |
| 5702 | if (location.type == BTRFS_INODE_ITEM_KEY) { |
Josef Bacik | 73f7341 | 2009-12-04 17:38:27 +0000 | [diff] [blame] | 5703 | inode = btrfs_iget(dir->i_sb, &location, root, NULL); |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 5704 | return inode; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5705 | } |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 5706 | |
| 5707 | BUG_ON(location.type != BTRFS_ROOT_ITEM_KEY); |
| 5708 | |
Yan, Zheng | 76dda93 | 2009-09-21 16:00:26 -0400 | [diff] [blame] | 5709 | index = srcu_read_lock(&root->fs_info->subvol_srcu); |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 5710 | ret = fixup_tree_root_location(root, dir, dentry, |
| 5711 | &location, &sub_root); |
| 5712 | if (ret < 0) { |
| 5713 | if (ret != -ENOENT) |
| 5714 | inode = ERR_PTR(ret); |
| 5715 | else |
| 5716 | inode = new_simple_dir(dir->i_sb, &location, sub_root); |
| 5717 | } else { |
Josef Bacik | 73f7341 | 2009-12-04 17:38:27 +0000 | [diff] [blame] | 5718 | inode = btrfs_iget(dir->i_sb, &location, sub_root, NULL); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5719 | } |
Yan, Zheng | 76dda93 | 2009-09-21 16:00:26 -0400 | [diff] [blame] | 5720 | srcu_read_unlock(&root->fs_info->subvol_srcu, index); |
| 5721 | |
Julia Lawall | 34d19ba | 2011-01-24 19:55:19 +0000 | [diff] [blame] | 5722 | if (!IS_ERR(inode) && root != sub_root) { |
Yan, Zheng | c71bf09 | 2009-11-12 09:34:40 +0000 | [diff] [blame] | 5723 | down_read(&root->fs_info->cleanup_work_sem); |
| 5724 | if (!(inode->i_sb->s_flags & MS_RDONLY)) |
Josef Bacik | 66b4ffd | 2011-01-31 16:22:42 -0500 | [diff] [blame] | 5725 | ret = btrfs_orphan_cleanup(sub_root); |
Yan, Zheng | c71bf09 | 2009-11-12 09:34:40 +0000 | [diff] [blame] | 5726 | up_read(&root->fs_info->cleanup_work_sem); |
Josef Bacik | 01cd336 | 2013-06-03 21:39:49 -0400 | [diff] [blame] | 5727 | if (ret) { |
| 5728 | iput(inode); |
Josef Bacik | 66b4ffd | 2011-01-31 16:22:42 -0500 | [diff] [blame] | 5729 | inode = ERR_PTR(ret); |
Josef Bacik | 01cd336 | 2013-06-03 21:39:49 -0400 | [diff] [blame] | 5730 | } |
Yan, Zheng | c71bf09 | 2009-11-12 09:34:40 +0000 | [diff] [blame] | 5731 | } |
| 5732 | |
Chris Mason | 3de4586 | 2008-11-17 21:02:50 -0500 | [diff] [blame] | 5733 | return inode; |
| 5734 | } |
| 5735 | |
Nick Piggin | fe15ce4 | 2011-01-07 17:49:23 +1100 | [diff] [blame] | 5736 | static int btrfs_dentry_delete(const struct dentry *dentry) |
Yan, Zheng | 76dda93 | 2009-09-21 16:00:26 -0400 | [diff] [blame] | 5737 | { |
| 5738 | struct btrfs_root *root; |
David Howells | 2b0143b | 2015-03-17 22:25:59 +0000 | [diff] [blame] | 5739 | struct inode *inode = d_inode(dentry); |
Yan, Zheng | 76dda93 | 2009-09-21 16:00:26 -0400 | [diff] [blame] | 5740 | |
Li Zefan | 848cce0 | 2012-02-21 17:04:28 +0800 | [diff] [blame] | 5741 | if (!inode && !IS_ROOT(dentry)) |
David Howells | 2b0143b | 2015-03-17 22:25:59 +0000 | [diff] [blame] | 5742 | inode = d_inode(dentry->d_parent); |
Yan, Zheng | 76dda93 | 2009-09-21 16:00:26 -0400 | [diff] [blame] | 5743 | |
Li Zefan | 848cce0 | 2012-02-21 17:04:28 +0800 | [diff] [blame] | 5744 | if (inode) { |
| 5745 | root = BTRFS_I(inode)->root; |
Yan, Zheng | efefb14 | 2009-10-09 09:25:16 -0400 | [diff] [blame] | 5746 | if (btrfs_root_refs(&root->root_item) == 0) |
| 5747 | return 1; |
Li Zefan | 848cce0 | 2012-02-21 17:04:28 +0800 | [diff] [blame] | 5748 | |
| 5749 | if (btrfs_ino(inode) == BTRFS_EMPTY_SUBVOL_DIR_OBJECTID) |
| 5750 | return 1; |
Yan, Zheng | efefb14 | 2009-10-09 09:25:16 -0400 | [diff] [blame] | 5751 | } |
Yan, Zheng | 76dda93 | 2009-09-21 16:00:26 -0400 | [diff] [blame] | 5752 | return 0; |
| 5753 | } |
| 5754 | |
Josef Bacik | b4aff1f | 2011-06-28 16:18:59 -0400 | [diff] [blame] | 5755 | static void btrfs_dentry_release(struct dentry *dentry) |
| 5756 | { |
Daeseok Youn | 944a451 | 2014-04-14 15:37:02 +0900 | [diff] [blame] | 5757 | kfree(dentry->d_fsdata); |
Josef Bacik | b4aff1f | 2011-06-28 16:18:59 -0400 | [diff] [blame] | 5758 | } |
| 5759 | |
Chris Mason | 3de4586 | 2008-11-17 21:02:50 -0500 | [diff] [blame] | 5760 | static struct dentry *btrfs_lookup(struct inode *dir, struct dentry *dentry, |
Al Viro | 00cd8dd | 2012-06-10 17:13:09 -0400 | [diff] [blame] | 5761 | unsigned int flags) |
Chris Mason | 3de4586 | 2008-11-17 21:02:50 -0500 | [diff] [blame] | 5762 | { |
Tsutomu Itoh | 5662344b3 | 2013-12-13 09:51:42 +0900 | [diff] [blame] | 5763 | struct inode *inode; |
Josef Bacik | a66e7cc | 2011-09-18 10:34:03 -0400 | [diff] [blame] | 5764 | |
Tsutomu Itoh | 5662344b3 | 2013-12-13 09:51:42 +0900 | [diff] [blame] | 5765 | inode = btrfs_lookup_dentry(dir, dentry); |
| 5766 | if (IS_ERR(inode)) { |
| 5767 | if (PTR_ERR(inode) == -ENOENT) |
| 5768 | inode = NULL; |
| 5769 | else |
| 5770 | return ERR_CAST(inode); |
| 5771 | } |
| 5772 | |
Al Viro | 41d28bc | 2014-10-12 22:24:21 -0400 | [diff] [blame] | 5773 | return d_splice_alias(inode, dentry); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5774 | } |
| 5775 | |
Miao Xie | 16cdcec | 2011-04-22 18:12:22 +0800 | [diff] [blame] | 5776 | unsigned char btrfs_filetype_table[] = { |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5777 | DT_UNKNOWN, DT_REG, DT_DIR, DT_CHR, DT_BLK, DT_FIFO, DT_SOCK, DT_LNK |
| 5778 | }; |
| 5779 | |
Al Viro | 9cdda8d | 2013-05-22 16:48:09 -0400 | [diff] [blame] | 5780 | static int btrfs_real_readdir(struct file *file, struct dir_context *ctx) |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5781 | { |
Al Viro | 9cdda8d | 2013-05-22 16:48:09 -0400 | [diff] [blame] | 5782 | struct inode *inode = file_inode(file); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5783 | struct btrfs_root *root = BTRFS_I(inode)->root; |
| 5784 | struct btrfs_item *item; |
| 5785 | struct btrfs_dir_item *di; |
| 5786 | struct btrfs_key key; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 5787 | struct btrfs_key found_key; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5788 | struct btrfs_path *path; |
Miao Xie | 16cdcec | 2011-04-22 18:12:22 +0800 | [diff] [blame] | 5789 | struct list_head ins_list; |
| 5790 | struct list_head del_list; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5791 | int ret; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 5792 | struct extent_buffer *leaf; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5793 | int slot; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5794 | unsigned char d_type; |
| 5795 | int over = 0; |
| 5796 | u32 di_cur; |
| 5797 | u32 di_total; |
| 5798 | u32 di_len; |
| 5799 | int key_type = BTRFS_DIR_INDEX_KEY; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 5800 | char tmp_name[32]; |
| 5801 | char *name_ptr; |
| 5802 | int name_len; |
Al Viro | 9cdda8d | 2013-05-22 16:48:09 -0400 | [diff] [blame] | 5803 | int is_curr = 0; /* ctx->pos points to the current index? */ |
David Sterba | bc4ef75 | 2015-11-13 13:44:28 +0100 | [diff] [blame] | 5804 | bool emitted; |
Omar Sandoval | 02dbfc9 | 2016-05-20 13:50:33 -0700 | [diff] [blame] | 5805 | bool put = false; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5806 | |
| 5807 | /* FIXME, use a real flag for deciding about the key type */ |
| 5808 | if (root->fs_info->tree_root == root) |
| 5809 | key_type = BTRFS_DIR_ITEM_KEY; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 5810 | |
Al Viro | 9cdda8d | 2013-05-22 16:48:09 -0400 | [diff] [blame] | 5811 | if (!dir_emit_dots(file, ctx)) |
| 5812 | return 0; |
| 5813 | |
David Woodhouse | 49593bf | 2008-08-17 17:08:36 +0100 | [diff] [blame] | 5814 | path = btrfs_alloc_path(); |
Miao Xie | 16cdcec | 2011-04-22 18:12:22 +0800 | [diff] [blame] | 5815 | if (!path) |
| 5816 | return -ENOMEM; |
Chris Mason | ff5714c | 2011-05-28 07:00:39 -0400 | [diff] [blame] | 5817 | |
David Sterba | e4058b5 | 2015-11-27 16:31:35 +0100 | [diff] [blame] | 5818 | path->reada = READA_FORWARD; |
David Woodhouse | 49593bf | 2008-08-17 17:08:36 +0100 | [diff] [blame] | 5819 | |
Miao Xie | 16cdcec | 2011-04-22 18:12:22 +0800 | [diff] [blame] | 5820 | if (key_type == BTRFS_DIR_INDEX_KEY) { |
| 5821 | INIT_LIST_HEAD(&ins_list); |
| 5822 | INIT_LIST_HEAD(&del_list); |
Omar Sandoval | 02dbfc9 | 2016-05-20 13:50:33 -0700 | [diff] [blame] | 5823 | put = btrfs_readdir_get_delayed_items(inode, &ins_list, |
| 5824 | &del_list); |
Miao Xie | 16cdcec | 2011-04-22 18:12:22 +0800 | [diff] [blame] | 5825 | } |
| 5826 | |
David Sterba | 962a298 | 2014-06-04 18:41:45 +0200 | [diff] [blame] | 5827 | key.type = key_type; |
Al Viro | 9cdda8d | 2013-05-22 16:48:09 -0400 | [diff] [blame] | 5828 | key.offset = ctx->pos; |
Li Zefan | 33345d01 | 2011-04-20 10:31:50 +0800 | [diff] [blame] | 5829 | key.objectid = btrfs_ino(inode); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 5830 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5831 | ret = btrfs_search_slot(NULL, root, &key, path, 0, 0); |
| 5832 | if (ret < 0) |
| 5833 | goto err; |
David Woodhouse | 49593bf | 2008-08-17 17:08:36 +0100 | [diff] [blame] | 5834 | |
David Sterba | bc4ef75 | 2015-11-13 13:44:28 +0100 | [diff] [blame] | 5835 | emitted = false; |
David Woodhouse | 49593bf | 2008-08-17 17:08:36 +0100 | [diff] [blame] | 5836 | while (1) { |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 5837 | leaf = path->nodes[0]; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5838 | slot = path->slots[0]; |
Li Zefan | b9e03af | 2011-03-23 10:43:58 +0800 | [diff] [blame] | 5839 | if (slot >= btrfs_header_nritems(leaf)) { |
| 5840 | ret = btrfs_next_leaf(root, path); |
| 5841 | if (ret < 0) |
| 5842 | goto err; |
| 5843 | else if (ret > 0) |
| 5844 | break; |
| 5845 | continue; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5846 | } |
Chris Mason | 3de4586 | 2008-11-17 21:02:50 -0500 | [diff] [blame] | 5847 | |
Ross Kirk | dd3cc16 | 2013-09-16 15:58:09 +0100 | [diff] [blame] | 5848 | item = btrfs_item_nr(slot); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 5849 | btrfs_item_key_to_cpu(leaf, &found_key, slot); |
| 5850 | |
| 5851 | if (found_key.objectid != key.objectid) |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5852 | break; |
David Sterba | 962a298 | 2014-06-04 18:41:45 +0200 | [diff] [blame] | 5853 | if (found_key.type != key_type) |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5854 | break; |
Al Viro | 9cdda8d | 2013-05-22 16:48:09 -0400 | [diff] [blame] | 5855 | if (found_key.offset < ctx->pos) |
Li Zefan | b9e03af | 2011-03-23 10:43:58 +0800 | [diff] [blame] | 5856 | goto next; |
Miao Xie | 16cdcec | 2011-04-22 18:12:22 +0800 | [diff] [blame] | 5857 | if (key_type == BTRFS_DIR_INDEX_KEY && |
| 5858 | btrfs_should_delete_dir_index(&del_list, |
| 5859 | found_key.offset)) |
| 5860 | goto next; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 5861 | |
Al Viro | 9cdda8d | 2013-05-22 16:48:09 -0400 | [diff] [blame] | 5862 | ctx->pos = found_key.offset; |
Miao Xie | 16cdcec | 2011-04-22 18:12:22 +0800 | [diff] [blame] | 5863 | is_curr = 1; |
David Woodhouse | 49593bf | 2008-08-17 17:08:36 +0100 | [diff] [blame] | 5864 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5865 | di = btrfs_item_ptr(leaf, slot, struct btrfs_dir_item); |
| 5866 | di_cur = 0; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 5867 | di_total = btrfs_item_size(leaf, item); |
David Woodhouse | 49593bf | 2008-08-17 17:08:36 +0100 | [diff] [blame] | 5868 | |
| 5869 | while (di_cur < di_total) { |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 5870 | struct btrfs_key location; |
| 5871 | |
Josef Bacik | 22a94d4 | 2011-03-16 16:47:17 -0400 | [diff] [blame] | 5872 | if (verify_dir_item(root, leaf, di)) |
| 5873 | break; |
| 5874 | |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 5875 | name_len = btrfs_dir_name_len(leaf, di); |
David Woodhouse | 49593bf | 2008-08-17 17:08:36 +0100 | [diff] [blame] | 5876 | if (name_len <= sizeof(tmp_name)) { |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 5877 | name_ptr = tmp_name; |
| 5878 | } else { |
David Sterba | 49e350a | 2016-02-11 14:25:38 +0100 | [diff] [blame] | 5879 | name_ptr = kmalloc(name_len, GFP_KERNEL); |
David Woodhouse | 49593bf | 2008-08-17 17:08:36 +0100 | [diff] [blame] | 5880 | if (!name_ptr) { |
| 5881 | ret = -ENOMEM; |
| 5882 | goto err; |
| 5883 | } |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 5884 | } |
| 5885 | read_extent_buffer(leaf, name_ptr, |
| 5886 | (unsigned long)(di + 1), name_len); |
| 5887 | |
| 5888 | d_type = btrfs_filetype_table[btrfs_dir_type(leaf, di)]; |
| 5889 | btrfs_dir_item_key_to_cpu(leaf, di, &location); |
Chris Mason | 3de4586 | 2008-11-17 21:02:50 -0500 | [diff] [blame] | 5890 | |
Josef Bacik | b4aff1f | 2011-06-28 16:18:59 -0400 | [diff] [blame] | 5891 | |
Chris Mason | 3de4586 | 2008-11-17 21:02:50 -0500 | [diff] [blame] | 5892 | /* is this a reference to our own snapshot? If so |
Arne Jansen | 8c9c2bf | 2012-02-25 09:09:30 +0100 | [diff] [blame] | 5893 | * skip it. |
| 5894 | * |
| 5895 | * In contrast to old kernels, we insert the snapshot's |
| 5896 | * dir item and dir index after it has been created, so |
| 5897 | * we won't find a reference to our own snapshot. We |
| 5898 | * still keep the following code for backward |
| 5899 | * compatibility. |
Chris Mason | 3de4586 | 2008-11-17 21:02:50 -0500 | [diff] [blame] | 5900 | */ |
| 5901 | if (location.type == BTRFS_ROOT_ITEM_KEY && |
| 5902 | location.objectid == root->root_key.objectid) { |
| 5903 | over = 0; |
| 5904 | goto skip; |
| 5905 | } |
Al Viro | 9cdda8d | 2013-05-22 16:48:09 -0400 | [diff] [blame] | 5906 | over = !dir_emit(ctx, name_ptr, name_len, |
| 5907 | location.objectid, d_type); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 5908 | |
Chris Mason | 3de4586 | 2008-11-17 21:02:50 -0500 | [diff] [blame] | 5909 | skip: |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 5910 | if (name_ptr != tmp_name) |
| 5911 | kfree(name_ptr); |
| 5912 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5913 | if (over) |
| 5914 | goto nopos; |
David Sterba | bc4ef75 | 2015-11-13 13:44:28 +0100 | [diff] [blame] | 5915 | emitted = true; |
Josef Bacik | 5103e94 | 2007-11-16 11:45:54 -0500 | [diff] [blame] | 5916 | di_len = btrfs_dir_name_len(leaf, di) + |
David Woodhouse | 49593bf | 2008-08-17 17:08:36 +0100 | [diff] [blame] | 5917 | btrfs_dir_data_len(leaf, di) + sizeof(*di); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5918 | di_cur += di_len; |
| 5919 | di = (struct btrfs_dir_item *)((char *)di + di_len); |
| 5920 | } |
Li Zefan | b9e03af | 2011-03-23 10:43:58 +0800 | [diff] [blame] | 5921 | next: |
| 5922 | path->slots[0]++; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5923 | } |
David Woodhouse | 49593bf | 2008-08-17 17:08:36 +0100 | [diff] [blame] | 5924 | |
Miao Xie | 16cdcec | 2011-04-22 18:12:22 +0800 | [diff] [blame] | 5925 | if (key_type == BTRFS_DIR_INDEX_KEY) { |
| 5926 | if (is_curr) |
Al Viro | 9cdda8d | 2013-05-22 16:48:09 -0400 | [diff] [blame] | 5927 | ctx->pos++; |
David Sterba | bc4ef75 | 2015-11-13 13:44:28 +0100 | [diff] [blame] | 5928 | ret = btrfs_readdir_delayed_dir_index(ctx, &ins_list, &emitted); |
Miao Xie | 16cdcec | 2011-04-22 18:12:22 +0800 | [diff] [blame] | 5929 | if (ret) |
| 5930 | goto nopos; |
| 5931 | } |
| 5932 | |
David Sterba | bc4ef75 | 2015-11-13 13:44:28 +0100 | [diff] [blame] | 5933 | /* |
| 5934 | * If we haven't emitted any dir entry, we must not touch ctx->pos as |
| 5935 | * it was was set to the termination value in previous call. We assume |
| 5936 | * that "." and ".." were emitted if we reach this point and set the |
| 5937 | * termination value as well for an empty directory. |
| 5938 | */ |
| 5939 | if (ctx->pos > 2 && !emitted) |
| 5940 | goto nopos; |
| 5941 | |
David Woodhouse | 49593bf | 2008-08-17 17:08:36 +0100 | [diff] [blame] | 5942 | /* Reached end of directory/root. Bump pos past the last item. */ |
Zach Brown | db62efb | 2013-07-11 16:19:42 -0700 | [diff] [blame] | 5943 | ctx->pos++; |
| 5944 | |
| 5945 | /* |
| 5946 | * Stop new entries from being returned after we return the last |
| 5947 | * entry. |
| 5948 | * |
| 5949 | * New directory entries are assigned a strictly increasing |
| 5950 | * offset. This means that new entries created during readdir |
| 5951 | * are *guaranteed* to be seen in the future by that readdir. |
| 5952 | * This has broken buggy programs which operate on names as |
| 5953 | * they're returned by readdir. Until we re-use freed offsets |
| 5954 | * we have this hack to stop new entries from being returned |
| 5955 | * under the assumption that they'll never reach this huge |
| 5956 | * offset. |
| 5957 | * |
| 5958 | * This is being careful not to overflow 32bit loff_t unless the |
| 5959 | * last entry requires it because doing so has broken 32bit apps |
| 5960 | * in the past. |
| 5961 | */ |
| 5962 | if (key_type == BTRFS_DIR_INDEX_KEY) { |
| 5963 | if (ctx->pos >= INT_MAX) |
| 5964 | ctx->pos = LLONG_MAX; |
| 5965 | else |
| 5966 | ctx->pos = INT_MAX; |
| 5967 | } |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5968 | nopos: |
| 5969 | ret = 0; |
| 5970 | err: |
Omar Sandoval | 02dbfc9 | 2016-05-20 13:50:33 -0700 | [diff] [blame] | 5971 | if (put) |
| 5972 | btrfs_readdir_put_delayed_items(inode, &ins_list, &del_list); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5973 | btrfs_free_path(path); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5974 | return ret; |
| 5975 | } |
| 5976 | |
Christoph Hellwig | a9185b4 | 2010-03-05 09:21:37 +0100 | [diff] [blame] | 5977 | int btrfs_write_inode(struct inode *inode, struct writeback_control *wbc) |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5978 | { |
| 5979 | struct btrfs_root *root = BTRFS_I(inode)->root; |
| 5980 | struct btrfs_trans_handle *trans; |
| 5981 | int ret = 0; |
Josef Bacik | 0af3d00 | 2010-06-21 14:48:16 -0400 | [diff] [blame] | 5982 | bool nolock = false; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5983 | |
Josef Bacik | 72ac3c0 | 2012-05-23 14:13:11 -0400 | [diff] [blame] | 5984 | if (test_bit(BTRFS_INODE_DUMMY, &BTRFS_I(inode)->runtime_flags)) |
Chris Mason | 4ca8b41 | 2008-08-05 13:30:48 -0400 | [diff] [blame] | 5985 | return 0; |
| 5986 | |
Liu Bo | 83eea1f | 2012-07-10 05:28:39 -0600 | [diff] [blame] | 5987 | 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] | 5988 | nolock = true; |
Josef Bacik | 0af3d00 | 2010-06-21 14:48:16 -0400 | [diff] [blame] | 5989 | |
Christoph Hellwig | a9185b4 | 2010-03-05 09:21:37 +0100 | [diff] [blame] | 5990 | if (wbc->sync_mode == WB_SYNC_ALL) { |
Josef Bacik | 0af3d00 | 2010-06-21 14:48:16 -0400 | [diff] [blame] | 5991 | if (nolock) |
Josef Bacik | 7a7eaa4 | 2011-04-13 12:54:33 -0400 | [diff] [blame] | 5992 | trans = btrfs_join_transaction_nolock(root); |
Josef Bacik | 0af3d00 | 2010-06-21 14:48:16 -0400 | [diff] [blame] | 5993 | else |
Josef Bacik | 7a7eaa4 | 2011-04-13 12:54:33 -0400 | [diff] [blame] | 5994 | trans = btrfs_join_transaction(root); |
Tsutomu Itoh | 3612b49 | 2011-01-25 02:51:38 +0000 | [diff] [blame] | 5995 | if (IS_ERR(trans)) |
| 5996 | return PTR_ERR(trans); |
Miao Xie | a698d075 | 2012-09-20 01:51:59 -0600 | [diff] [blame] | 5997 | ret = btrfs_commit_transaction(trans, root); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5998 | } |
| 5999 | return ret; |
| 6000 | } |
| 6001 | |
| 6002 | /* |
Chris Mason | 54aa1f4 | 2007-06-22 14:16:25 -0400 | [diff] [blame] | 6003 | * This is somewhat expensive, updating the tree every time the |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 6004 | * inode changes. But, it is most likely to find the inode in cache. |
| 6005 | * FIXME, needs more benchmarking...there are no reasons other than performance |
| 6006 | * to keep or drop this code. |
| 6007 | */ |
Eric Sandeen | 48a3b63 | 2013-04-25 20:41:01 +0000 | [diff] [blame] | 6008 | static int btrfs_dirty_inode(struct inode *inode) |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 6009 | { |
| 6010 | struct btrfs_root *root = BTRFS_I(inode)->root; |
| 6011 | struct btrfs_trans_handle *trans; |
Yan, Zheng | 8929ecfa | 2010-05-16 10:49:58 -0400 | [diff] [blame] | 6012 | int ret; |
| 6013 | |
Josef Bacik | 72ac3c0 | 2012-05-23 14:13:11 -0400 | [diff] [blame] | 6014 | if (test_bit(BTRFS_INODE_DUMMY, &BTRFS_I(inode)->runtime_flags)) |
Josef Bacik | 22c44fe | 2011-11-30 10:45:38 -0500 | [diff] [blame] | 6015 | return 0; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 6016 | |
Josef Bacik | 7a7eaa4 | 2011-04-13 12:54:33 -0400 | [diff] [blame] | 6017 | trans = btrfs_join_transaction(root); |
Josef Bacik | 22c44fe | 2011-11-30 10:45:38 -0500 | [diff] [blame] | 6018 | if (IS_ERR(trans)) |
| 6019 | return PTR_ERR(trans); |
Yan, Zheng | 8929ecfa | 2010-05-16 10:49:58 -0400 | [diff] [blame] | 6020 | |
| 6021 | ret = btrfs_update_inode(trans, root, inode); |
Chris Mason | 94b6044 | 2010-05-26 11:02:00 -0400 | [diff] [blame] | 6022 | if (ret && ret == -ENOSPC) { |
| 6023 | /* whoops, lets try again with the full transaction */ |
| 6024 | btrfs_end_transaction(trans, root); |
| 6025 | trans = btrfs_start_transaction(root, 1); |
Josef Bacik | 22c44fe | 2011-11-30 10:45:38 -0500 | [diff] [blame] | 6026 | if (IS_ERR(trans)) |
| 6027 | return PTR_ERR(trans); |
Yan, Zheng | 8929ecfa | 2010-05-16 10:49:58 -0400 | [diff] [blame] | 6028 | |
Chris Mason | 94b6044 | 2010-05-26 11:02:00 -0400 | [diff] [blame] | 6029 | ret = btrfs_update_inode(trans, root, inode); |
Chris Mason | 94b6044 | 2010-05-26 11:02:00 -0400 | [diff] [blame] | 6030 | } |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 6031 | btrfs_end_transaction(trans, root); |
Miao Xie | 16cdcec | 2011-04-22 18:12:22 +0800 | [diff] [blame] | 6032 | if (BTRFS_I(inode)->delayed_node) |
| 6033 | btrfs_balance_delayed_items(root); |
Josef Bacik | 22c44fe | 2011-11-30 10:45:38 -0500 | [diff] [blame] | 6034 | |
| 6035 | return ret; |
| 6036 | } |
| 6037 | |
| 6038 | /* |
| 6039 | * This is a copy of file_update_time. We need this so we can return error on |
| 6040 | * ENOSPC for updating the inode in the case of file write and mmap writes. |
| 6041 | */ |
Josef Bacik | e41f941 | 2012-03-26 09:46:47 -0400 | [diff] [blame] | 6042 | static int btrfs_update_time(struct inode *inode, struct timespec *now, |
| 6043 | int flags) |
Josef Bacik | 22c44fe | 2011-11-30 10:45:38 -0500 | [diff] [blame] | 6044 | { |
Alexander Block | 2bc556528 | 2012-06-15 09:49:33 +0200 | [diff] [blame] | 6045 | struct btrfs_root *root = BTRFS_I(inode)->root; |
| 6046 | |
| 6047 | if (btrfs_root_readonly(root)) |
| 6048 | return -EROFS; |
| 6049 | |
Josef Bacik | e41f941 | 2012-03-26 09:46:47 -0400 | [diff] [blame] | 6050 | if (flags & S_VERSION) |
Josef Bacik | 22c44fe | 2011-11-30 10:45:38 -0500 | [diff] [blame] | 6051 | inode_inc_iversion(inode); |
Josef Bacik | e41f941 | 2012-03-26 09:46:47 -0400 | [diff] [blame] | 6052 | if (flags & S_CTIME) |
| 6053 | inode->i_ctime = *now; |
| 6054 | if (flags & S_MTIME) |
| 6055 | inode->i_mtime = *now; |
| 6056 | if (flags & S_ATIME) |
| 6057 | inode->i_atime = *now; |
| 6058 | return btrfs_dirty_inode(inode); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 6059 | } |
| 6060 | |
Chris Mason | d352ac6 | 2008-09-29 15:18:18 -0400 | [diff] [blame] | 6061 | /* |
| 6062 | * find the highest existing sequence number in a directory |
| 6063 | * and then set the in-memory index_cnt variable to reflect |
| 6064 | * free sequence numbers |
| 6065 | */ |
Josef Bacik | aec7477 | 2008-07-24 12:12:38 -0400 | [diff] [blame] | 6066 | static int btrfs_set_inode_index_count(struct inode *inode) |
| 6067 | { |
| 6068 | struct btrfs_root *root = BTRFS_I(inode)->root; |
| 6069 | struct btrfs_key key, found_key; |
| 6070 | struct btrfs_path *path; |
| 6071 | struct extent_buffer *leaf; |
| 6072 | int ret; |
| 6073 | |
Li Zefan | 33345d01 | 2011-04-20 10:31:50 +0800 | [diff] [blame] | 6074 | key.objectid = btrfs_ino(inode); |
David Sterba | 962a298 | 2014-06-04 18:41:45 +0200 | [diff] [blame] | 6075 | key.type = BTRFS_DIR_INDEX_KEY; |
Josef Bacik | aec7477 | 2008-07-24 12:12:38 -0400 | [diff] [blame] | 6076 | key.offset = (u64)-1; |
| 6077 | |
| 6078 | path = btrfs_alloc_path(); |
| 6079 | if (!path) |
| 6080 | return -ENOMEM; |
| 6081 | |
| 6082 | ret = btrfs_search_slot(NULL, root, &key, path, 0, 0); |
| 6083 | if (ret < 0) |
| 6084 | goto out; |
| 6085 | /* FIXME: we should be able to handle this */ |
| 6086 | if (ret == 0) |
| 6087 | goto out; |
| 6088 | ret = 0; |
| 6089 | |
| 6090 | /* |
| 6091 | * MAGIC NUMBER EXPLANATION: |
| 6092 | * since we search a directory based on f_pos we have to start at 2 |
| 6093 | * since '.' and '..' have f_pos of 0 and 1 respectively, so everybody |
| 6094 | * else has to start at 2 |
| 6095 | */ |
| 6096 | if (path->slots[0] == 0) { |
| 6097 | BTRFS_I(inode)->index_cnt = 2; |
| 6098 | goto out; |
| 6099 | } |
| 6100 | |
| 6101 | path->slots[0]--; |
| 6102 | |
| 6103 | leaf = path->nodes[0]; |
| 6104 | btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]); |
| 6105 | |
Li Zefan | 33345d01 | 2011-04-20 10:31:50 +0800 | [diff] [blame] | 6106 | if (found_key.objectid != btrfs_ino(inode) || |
David Sterba | 962a298 | 2014-06-04 18:41:45 +0200 | [diff] [blame] | 6107 | found_key.type != BTRFS_DIR_INDEX_KEY) { |
Josef Bacik | aec7477 | 2008-07-24 12:12:38 -0400 | [diff] [blame] | 6108 | BTRFS_I(inode)->index_cnt = 2; |
| 6109 | goto out; |
| 6110 | } |
| 6111 | |
| 6112 | BTRFS_I(inode)->index_cnt = found_key.offset + 1; |
| 6113 | out: |
| 6114 | btrfs_free_path(path); |
| 6115 | return ret; |
| 6116 | } |
| 6117 | |
Chris Mason | d352ac6 | 2008-09-29 15:18:18 -0400 | [diff] [blame] | 6118 | /* |
| 6119 | * helper to find a free sequence number in a given directory. This current |
| 6120 | * code is very simple, later versions will do smarter things in the btree |
| 6121 | */ |
Chris Mason | 3de4586 | 2008-11-17 21:02:50 -0500 | [diff] [blame] | 6122 | int btrfs_set_inode_index(struct inode *dir, u64 *index) |
Josef Bacik | aec7477 | 2008-07-24 12:12:38 -0400 | [diff] [blame] | 6123 | { |
| 6124 | int ret = 0; |
| 6125 | |
| 6126 | if (BTRFS_I(dir)->index_cnt == (u64)-1) { |
Miao Xie | 16cdcec | 2011-04-22 18:12:22 +0800 | [diff] [blame] | 6127 | ret = btrfs_inode_delayed_dir_index_count(dir); |
| 6128 | if (ret) { |
| 6129 | ret = btrfs_set_inode_index_count(dir); |
| 6130 | if (ret) |
| 6131 | return ret; |
| 6132 | } |
Josef Bacik | aec7477 | 2008-07-24 12:12:38 -0400 | [diff] [blame] | 6133 | } |
| 6134 | |
Chris Mason | 00e4e6b | 2008-08-05 11:18:09 -0400 | [diff] [blame] | 6135 | *index = BTRFS_I(dir)->index_cnt; |
Josef Bacik | aec7477 | 2008-07-24 12:12:38 -0400 | [diff] [blame] | 6136 | BTRFS_I(dir)->index_cnt++; |
| 6137 | |
| 6138 | return ret; |
| 6139 | } |
| 6140 | |
Chris Mason | b0d5d10 | 2014-09-08 13:08:51 -0700 | [diff] [blame] | 6141 | static int btrfs_insert_inode_locked(struct inode *inode) |
| 6142 | { |
| 6143 | struct btrfs_iget_args args; |
| 6144 | args.location = &BTRFS_I(inode)->location; |
| 6145 | args.root = BTRFS_I(inode)->root; |
| 6146 | |
| 6147 | return insert_inode_locked4(inode, |
| 6148 | btrfs_inode_hash(inode->i_ino, BTRFS_I(inode)->root), |
| 6149 | btrfs_find_actor, &args); |
| 6150 | } |
| 6151 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 6152 | static struct inode *btrfs_new_inode(struct btrfs_trans_handle *trans, |
| 6153 | struct btrfs_root *root, |
Josef Bacik | aec7477 | 2008-07-24 12:12:38 -0400 | [diff] [blame] | 6154 | struct inode *dir, |
Chris Mason | 9c58309 | 2008-01-29 15:15:18 -0500 | [diff] [blame] | 6155 | const char *name, int name_len, |
Al Viro | 175a4eb | 2011-07-26 03:30:54 -0400 | [diff] [blame] | 6156 | u64 ref_objectid, u64 objectid, |
| 6157 | umode_t mode, u64 *index) |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 6158 | { |
| 6159 | struct inode *inode; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 6160 | struct btrfs_inode_item *inode_item; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 6161 | struct btrfs_key *location; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 6162 | struct btrfs_path *path; |
Chris Mason | 9c58309 | 2008-01-29 15:15:18 -0500 | [diff] [blame] | 6163 | struct btrfs_inode_ref *ref; |
| 6164 | struct btrfs_key key[2]; |
| 6165 | u32 sizes[2]; |
Filipe Manana | ef3b9af | 2014-04-27 20:40:45 +0100 | [diff] [blame] | 6166 | int nitems = name ? 2 : 1; |
Chris Mason | 9c58309 | 2008-01-29 15:15:18 -0500 | [diff] [blame] | 6167 | unsigned long ptr; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 6168 | int ret; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 6169 | |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 6170 | path = btrfs_alloc_path(); |
Mark Fasheh | d8926bb | 2011-07-13 10:38:47 -0700 | [diff] [blame] | 6171 | if (!path) |
| 6172 | return ERR_PTR(-ENOMEM); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 6173 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 6174 | inode = new_inode(root->fs_info->sb); |
Yoshinori Sano | 8fb2764 | 2011-04-09 02:30:07 +0000 | [diff] [blame] | 6175 | if (!inode) { |
| 6176 | btrfs_free_path(path); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 6177 | return ERR_PTR(-ENOMEM); |
Yoshinori Sano | 8fb2764 | 2011-04-09 02:30:07 +0000 | [diff] [blame] | 6178 | } |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 6179 | |
Li Zefan | 581bb05 | 2011-04-20 10:06:11 +0800 | [diff] [blame] | 6180 | /* |
Filipe Manana | 5762b5c | 2014-08-01 00:10:32 +0100 | [diff] [blame] | 6181 | * O_TMPFILE, set link count to 0, so that after this point, |
| 6182 | * we fill in an inode item with the correct link count. |
| 6183 | */ |
| 6184 | if (!name) |
| 6185 | set_nlink(inode, 0); |
| 6186 | |
| 6187 | /* |
Li Zefan | 581bb05 | 2011-04-20 10:06:11 +0800 | [diff] [blame] | 6188 | * we have to initialize this early, so we can reclaim the inode |
| 6189 | * number if we fail afterwards in this function. |
| 6190 | */ |
| 6191 | inode->i_ino = objectid; |
| 6192 | |
Filipe Manana | ef3b9af | 2014-04-27 20:40:45 +0100 | [diff] [blame] | 6193 | if (dir && name) { |
liubo | 1abe9b8 | 2011-03-24 11:18:59 +0000 | [diff] [blame] | 6194 | trace_btrfs_inode_request(dir); |
| 6195 | |
Chris Mason | 3de4586 | 2008-11-17 21:02:50 -0500 | [diff] [blame] | 6196 | ret = btrfs_set_inode_index(dir, index); |
Shen Feng | 0977143 | 2009-04-02 16:46:06 -0400 | [diff] [blame] | 6197 | if (ret) { |
Yoshinori Sano | 8fb2764 | 2011-04-09 02:30:07 +0000 | [diff] [blame] | 6198 | btrfs_free_path(path); |
Shen Feng | 0977143 | 2009-04-02 16:46:06 -0400 | [diff] [blame] | 6199 | iput(inode); |
Josef Bacik | aec7477 | 2008-07-24 12:12:38 -0400 | [diff] [blame] | 6200 | return ERR_PTR(ret); |
Shen Feng | 0977143 | 2009-04-02 16:46:06 -0400 | [diff] [blame] | 6201 | } |
Filipe Manana | ef3b9af | 2014-04-27 20:40:45 +0100 | [diff] [blame] | 6202 | } else if (dir) { |
| 6203 | *index = 0; |
Josef Bacik | aec7477 | 2008-07-24 12:12:38 -0400 | [diff] [blame] | 6204 | } |
| 6205 | /* |
| 6206 | * index_cnt is ignored for everything but a dir, |
| 6207 | * btrfs_get_inode_index_count has an explanation for the magic |
| 6208 | * number |
| 6209 | */ |
| 6210 | BTRFS_I(inode)->index_cnt = 2; |
Miao Xie | 67de117 | 2013-12-26 13:07:06 +0800 | [diff] [blame] | 6211 | BTRFS_I(inode)->dir_index = *index; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 6212 | BTRFS_I(inode)->root = root; |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 6213 | BTRFS_I(inode)->generation = trans->transid; |
Josef Bacik | 7619585 | 2010-11-19 02:18:02 +0000 | [diff] [blame] | 6214 | inode->i_generation = BTRFS_I(inode)->generation; |
Chris Mason | b888db2 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 6215 | |
Josef Bacik | 5dc562c | 2012-08-17 13:14:17 -0400 | [diff] [blame] | 6216 | /* |
| 6217 | * We could have gotten an inode number from somebody who was fsynced |
| 6218 | * and then removed in this same transaction, so let's just set full |
| 6219 | * sync since it will be a full sync anyway and this will blow away the |
| 6220 | * old info in the log. |
| 6221 | */ |
| 6222 | set_bit(BTRFS_INODE_NEEDS_FULL_SYNC, &BTRFS_I(inode)->runtime_flags); |
| 6223 | |
Chris Mason | 9c58309 | 2008-01-29 15:15:18 -0500 | [diff] [blame] | 6224 | key[0].objectid = objectid; |
David Sterba | 962a298 | 2014-06-04 18:41:45 +0200 | [diff] [blame] | 6225 | key[0].type = BTRFS_INODE_ITEM_KEY; |
Chris Mason | 9c58309 | 2008-01-29 15:15:18 -0500 | [diff] [blame] | 6226 | key[0].offset = 0; |
| 6227 | |
Chris Mason | 9c58309 | 2008-01-29 15:15:18 -0500 | [diff] [blame] | 6228 | sizes[0] = sizeof(struct btrfs_inode_item); |
Filipe Manana | ef3b9af | 2014-04-27 20:40:45 +0100 | [diff] [blame] | 6229 | |
| 6230 | if (name) { |
| 6231 | /* |
| 6232 | * Start new inodes with an inode_ref. This is slightly more |
| 6233 | * efficient for small numbers of hard links since they will |
| 6234 | * be packed into one item. Extended refs will kick in if we |
| 6235 | * add more hard links than can fit in the ref item. |
| 6236 | */ |
| 6237 | key[1].objectid = objectid; |
David Sterba | 962a298 | 2014-06-04 18:41:45 +0200 | [diff] [blame] | 6238 | key[1].type = BTRFS_INODE_REF_KEY; |
Filipe Manana | ef3b9af | 2014-04-27 20:40:45 +0100 | [diff] [blame] | 6239 | key[1].offset = ref_objectid; |
| 6240 | |
| 6241 | sizes[1] = name_len + sizeof(*ref); |
| 6242 | } |
Chris Mason | 9c58309 | 2008-01-29 15:15:18 -0500 | [diff] [blame] | 6243 | |
Chris Mason | b0d5d10 | 2014-09-08 13:08:51 -0700 | [diff] [blame] | 6244 | location = &BTRFS_I(inode)->location; |
| 6245 | location->objectid = objectid; |
| 6246 | location->offset = 0; |
David Sterba | 962a298 | 2014-06-04 18:41:45 +0200 | [diff] [blame] | 6247 | location->type = BTRFS_INODE_ITEM_KEY; |
Chris Mason | b0d5d10 | 2014-09-08 13:08:51 -0700 | [diff] [blame] | 6248 | |
| 6249 | ret = btrfs_insert_inode_locked(inode); |
| 6250 | if (ret < 0) |
| 6251 | goto fail; |
| 6252 | |
Chris Mason | b947343 | 2009-03-13 11:00:37 -0400 | [diff] [blame] | 6253 | path->leave_spinning = 1; |
Filipe Manana | ef3b9af | 2014-04-27 20:40:45 +0100 | [diff] [blame] | 6254 | ret = btrfs_insert_empty_items(trans, root, path, key, sizes, nitems); |
Chris Mason | 9c58309 | 2008-01-29 15:15:18 -0500 | [diff] [blame] | 6255 | if (ret != 0) |
Chris Mason | b0d5d10 | 2014-09-08 13:08:51 -0700 | [diff] [blame] | 6256 | goto fail_unlock; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 6257 | |
Dmitry Monakhov | ecc11fab | 2010-03-04 17:31:47 +0300 | [diff] [blame] | 6258 | inode_init_owner(inode, dir, mode); |
Yan Zheng | a76a3cd | 2008-10-09 11:46:29 -0400 | [diff] [blame] | 6259 | inode_set_bytes(inode, 0); |
chandan r | 9cc97d6 | 2012-07-04 12:48:07 +0530 | [diff] [blame] | 6260 | |
Deepa Dinamani | 04b285f | 2016-02-06 23:57:21 -0800 | [diff] [blame] | 6261 | inode->i_mtime = current_fs_time(inode->i_sb); |
chandan r | 9cc97d6 | 2012-07-04 12:48:07 +0530 | [diff] [blame] | 6262 | inode->i_atime = inode->i_mtime; |
| 6263 | inode->i_ctime = inode->i_mtime; |
| 6264 | BTRFS_I(inode)->i_otime = inode->i_mtime; |
| 6265 | |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 6266 | inode_item = btrfs_item_ptr(path->nodes[0], path->slots[0], |
| 6267 | struct btrfs_inode_item); |
Li Zefan | 293f7e0 | 2012-07-10 00:58:58 -0600 | [diff] [blame] | 6268 | memset_extent_buffer(path->nodes[0], 0, (unsigned long)inode_item, |
| 6269 | sizeof(*inode_item)); |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 6270 | fill_inode_item(trans, path->nodes[0], inode_item, inode); |
Chris Mason | 9c58309 | 2008-01-29 15:15:18 -0500 | [diff] [blame] | 6271 | |
Filipe Manana | ef3b9af | 2014-04-27 20:40:45 +0100 | [diff] [blame] | 6272 | if (name) { |
| 6273 | ref = btrfs_item_ptr(path->nodes[0], path->slots[0] + 1, |
| 6274 | struct btrfs_inode_ref); |
| 6275 | btrfs_set_inode_ref_name_len(path->nodes[0], ref, name_len); |
| 6276 | btrfs_set_inode_ref_index(path->nodes[0], ref, *index); |
| 6277 | ptr = (unsigned long)(ref + 1); |
| 6278 | write_extent_buffer(path->nodes[0], name, ptr, name_len); |
| 6279 | } |
Chris Mason | 9c58309 | 2008-01-29 15:15:18 -0500 | [diff] [blame] | 6280 | |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 6281 | btrfs_mark_buffer_dirty(path->nodes[0]); |
| 6282 | btrfs_free_path(path); |
| 6283 | |
Christoph Hellwig | 6cbff00 | 2009-04-17 10:37:41 +0200 | [diff] [blame] | 6284 | btrfs_inherit_iflags(inode, dir); |
| 6285 | |
Al Viro | 569254b | 2011-07-24 17:08:40 -0400 | [diff] [blame] | 6286 | if (S_ISREG(mode)) { |
Jeff Mahoney | 3cdde22 | 2016-06-09 21:38:35 -0400 | [diff] [blame] | 6287 | if (btrfs_test_opt(root->fs_info, NODATASUM)) |
Chris Mason | 9427216 | 2009-07-02 12:26:06 -0400 | [diff] [blame] | 6288 | BTRFS_I(inode)->flags |= BTRFS_INODE_NODATASUM; |
Jeff Mahoney | 3cdde22 | 2016-06-09 21:38:35 -0400 | [diff] [blame] | 6289 | if (btrfs_test_opt(root->fs_info, NODATACOW)) |
Josef Bacik | f2bdf9a | 2013-02-21 15:28:28 -0500 | [diff] [blame] | 6290 | BTRFS_I(inode)->flags |= BTRFS_INODE_NODATACOW | |
| 6291 | BTRFS_INODE_NODATASUM; |
Chris Mason | 9427216 | 2009-07-02 12:26:06 -0400 | [diff] [blame] | 6292 | } |
| 6293 | |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 6294 | inode_tree_add(inode); |
liubo | 1abe9b8 | 2011-03-24 11:18:59 +0000 | [diff] [blame] | 6295 | |
| 6296 | trace_btrfs_inode_new(inode); |
Chris Mason | 1973f0f | 2011-06-24 13:13:29 -0400 | [diff] [blame] | 6297 | btrfs_set_inode_last_trans(trans, inode); |
liubo | 1abe9b8 | 2011-03-24 11:18:59 +0000 | [diff] [blame] | 6298 | |
Alexander Block | 8ea05e3 | 2012-07-25 17:35:53 +0200 | [diff] [blame] | 6299 | btrfs_update_root_times(trans, root); |
| 6300 | |
Filipe David Borba Manana | 6354192 | 2014-01-07 11:47:46 +0000 | [diff] [blame] | 6301 | ret = btrfs_inode_inherit_props(trans, inode, dir); |
| 6302 | if (ret) |
| 6303 | btrfs_err(root->fs_info, |
| 6304 | "error inheriting props for ino %llu (root %llu): %d", |
| 6305 | btrfs_ino(inode), root->root_key.objectid, ret); |
| 6306 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 6307 | return inode; |
Chris Mason | b0d5d10 | 2014-09-08 13:08:51 -0700 | [diff] [blame] | 6308 | |
| 6309 | fail_unlock: |
| 6310 | unlock_new_inode(inode); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 6311 | fail: |
Filipe Manana | ef3b9af | 2014-04-27 20:40:45 +0100 | [diff] [blame] | 6312 | if (dir && name) |
Josef Bacik | aec7477 | 2008-07-24 12:12:38 -0400 | [diff] [blame] | 6313 | BTRFS_I(dir)->index_cnt--; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 6314 | btrfs_free_path(path); |
Shen Feng | 0977143 | 2009-04-02 16:46:06 -0400 | [diff] [blame] | 6315 | iput(inode); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 6316 | return ERR_PTR(ret); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 6317 | } |
| 6318 | |
| 6319 | static inline u8 btrfs_inode_type(struct inode *inode) |
| 6320 | { |
| 6321 | return btrfs_type_by_mode[(inode->i_mode & S_IFMT) >> S_SHIFT]; |
| 6322 | } |
| 6323 | |
Chris Mason | d352ac6 | 2008-09-29 15:18:18 -0400 | [diff] [blame] | 6324 | /* |
| 6325 | * utility function to add 'inode' into 'parent_inode' with |
| 6326 | * a give name and a given sequence number. |
| 6327 | * if 'add_backref' is true, also insert a backref from the |
| 6328 | * inode to the parent directory. |
| 6329 | */ |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 6330 | int btrfs_add_link(struct btrfs_trans_handle *trans, |
| 6331 | struct inode *parent_inode, struct inode *inode, |
| 6332 | const char *name, int name_len, int add_backref, u64 index) |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 6333 | { |
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; |
Li Zefan | 33345d01 | 2011-04-20 10:31:50 +0800 | [diff] [blame] | 6337 | u64 ino = btrfs_ino(inode); |
| 6338 | u64 parent_ino = btrfs_ino(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)) { |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 6349 | ret = btrfs_add_root_ref(trans, root->fs_info->tree_root, |
| 6350 | key.objectid, root->root_key.objectid, |
Li Zefan | 33345d01 | 2011-04-20 10:31:50 +0800 | [diff] [blame] | 6351 | parent_ino, 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 = |
| 6375 | current_fs_time(parent_inode->i_sb); |
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; |
| 6385 | err = btrfs_del_root_ref(trans, root->fs_info->tree_root, |
| 6386 | key.objectid, root->root_key.objectid, |
| 6387 | parent_ino, &local_index, name, name_len); |
| 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 | { |
| 6414 | struct btrfs_trans_handle *trans; |
| 6415 | struct btrfs_root *root = BTRFS_I(dir)->root; |
Chris Mason | 1832a6d | 2007-12-21 16:27:21 -0500 | [diff] [blame] | 6416 | struct inode *inode = NULL; |
Josef Bacik | 618e21d | 2007-07-11 10:18:17 -0400 | [diff] [blame] | 6417 | int err; |
| 6418 | int drop_inode = 0; |
| 6419 | u64 objectid; |
Chris Mason | 00e4e6b | 2008-08-05 11:18:09 -0400 | [diff] [blame] | 6420 | u64 index = 0; |
Josef Bacik | 618e21d | 2007-07-11 10:18:17 -0400 | [diff] [blame] | 6421 | |
Josef Bacik | 9ed74f2 | 2009-09-11 16:12:44 -0400 | [diff] [blame] | 6422 | /* |
| 6423 | * 2 for inode item and ref |
| 6424 | * 2 for dir items |
| 6425 | * 1 for xattr if selinux is on |
| 6426 | */ |
Yan, Zheng | a22285a | 2010-05-16 10:48:46 -0400 | [diff] [blame] | 6427 | trans = btrfs_start_transaction(root, 5); |
| 6428 | if (IS_ERR(trans)) |
| 6429 | return PTR_ERR(trans); |
Chris Mason | 1832a6d | 2007-12-21 16:27:21 -0500 | [diff] [blame] | 6430 | |
Li Zefan | 581bb05 | 2011-04-20 10:06:11 +0800 | [diff] [blame] | 6431 | err = btrfs_find_free_ino(root, &objectid); |
| 6432 | if (err) |
| 6433 | goto out_unlock; |
| 6434 | |
Josef Bacik | aec7477 | 2008-07-24 12:12:38 -0400 | [diff] [blame] | 6435 | inode = btrfs_new_inode(trans, root, dir, dentry->d_name.name, |
Li Zefan | 33345d01 | 2011-04-20 10:31:50 +0800 | [diff] [blame] | 6436 | dentry->d_name.len, btrfs_ino(dir), objectid, |
Josef Bacik | d82a6f1 | 2011-05-11 15:26:06 -0400 | [diff] [blame] | 6437 | mode, &index); |
Tsutomu Itoh | 7cf96da | 2011-04-25 19:43:53 -0400 | [diff] [blame] | 6438 | if (IS_ERR(inode)) { |
| 6439 | err = PTR_ERR(inode); |
Josef Bacik | 618e21d | 2007-07-11 10:18:17 -0400 | [diff] [blame] | 6440 | goto out_unlock; |
Tsutomu Itoh | 7cf96da | 2011-04-25 19:43:53 -0400 | [diff] [blame] | 6441 | } |
Josef Bacik | 618e21d | 2007-07-11 10:18:17 -0400 | [diff] [blame] | 6442 | |
Casey Schaufler | ad19db7 | 2011-12-15 10:09:07 -0500 | [diff] [blame] | 6443 | /* |
| 6444 | * If the active LSM wants to access the inode during |
| 6445 | * d_instantiate it needs these. Smack checks to see |
| 6446 | * if the filesystem supports xattrs by looking at the |
| 6447 | * ops vector. |
| 6448 | */ |
Casey Schaufler | ad19db7 | 2011-12-15 10:09:07 -0500 | [diff] [blame] | 6449 | inode->i_op = &btrfs_special_inode_operations; |
Chris Mason | b0d5d10 | 2014-09-08 13:08:51 -0700 | [diff] [blame] | 6450 | init_special_inode(inode, inode->i_mode, rdev); |
| 6451 | |
| 6452 | err = btrfs_init_inode_security(trans, inode, dir, &dentry->d_name); |
Josef Bacik | 618e21d | 2007-07-11 10:18:17 -0400 | [diff] [blame] | 6453 | if (err) |
Chris Mason | b0d5d10 | 2014-09-08 13:08:51 -0700 | [diff] [blame] | 6454 | goto out_unlock_inode; |
| 6455 | |
| 6456 | err = btrfs_add_nondir(trans, dir, dentry, inode, 0, index); |
| 6457 | if (err) { |
| 6458 | goto out_unlock_inode; |
| 6459 | } else { |
Yan | 1b4ab1b | 2007-08-29 09:11:44 -0400 | [diff] [blame] | 6460 | btrfs_update_inode(trans, root, inode); |
Chris Mason | b0d5d10 | 2014-09-08 13:08:51 -0700 | [diff] [blame] | 6461 | unlock_new_inode(inode); |
Al Viro | 08c422c | 2011-12-23 07:58:13 -0500 | [diff] [blame] | 6462 | d_instantiate(dentry, inode); |
Josef Bacik | 618e21d | 2007-07-11 10:18:17 -0400 | [diff] [blame] | 6463 | } |
Chris Mason | b0d5d10 | 2014-09-08 13:08:51 -0700 | [diff] [blame] | 6464 | |
Josef Bacik | 618e21d | 2007-07-11 10:18:17 -0400 | [diff] [blame] | 6465 | out_unlock: |
Josef Bacik | 7ad85bb | 2012-01-12 19:10:12 -0500 | [diff] [blame] | 6466 | btrfs_end_transaction(trans, root); |
Josef Bacik | c581afc | 2014-02-06 16:06:06 -0500 | [diff] [blame] | 6467 | btrfs_balance_delayed_items(root); |
Liu Bo | b53d3f5 | 2012-11-14 14:34:34 +0000 | [diff] [blame] | 6468 | btrfs_btree_balance_dirty(root); |
Josef Bacik | 618e21d | 2007-07-11 10:18:17 -0400 | [diff] [blame] | 6469 | if (drop_inode) { |
| 6470 | inode_dec_link_count(inode); |
| 6471 | iput(inode); |
| 6472 | } |
Josef Bacik | 618e21d | 2007-07-11 10:18:17 -0400 | [diff] [blame] | 6473 | return err; |
Chris Mason | b0d5d10 | 2014-09-08 13:08:51 -0700 | [diff] [blame] | 6474 | |
| 6475 | out_unlock_inode: |
| 6476 | drop_inode = 1; |
| 6477 | unlock_new_inode(inode); |
| 6478 | goto out_unlock; |
| 6479 | |
Josef Bacik | 618e21d | 2007-07-11 10:18:17 -0400 | [diff] [blame] | 6480 | } |
| 6481 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 6482 | static int btrfs_create(struct inode *dir, struct dentry *dentry, |
Al Viro | ebfc3b4 | 2012-06-10 18:05:36 -0400 | [diff] [blame] | 6483 | umode_t mode, bool excl) |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 6484 | { |
| 6485 | struct btrfs_trans_handle *trans; |
| 6486 | struct btrfs_root *root = BTRFS_I(dir)->root; |
Chris Mason | 1832a6d | 2007-12-21 16:27:21 -0500 | [diff] [blame] | 6487 | struct inode *inode = NULL; |
Filipe Brandenburger | 43baa57 | 2012-11-30 03:40:09 +0000 | [diff] [blame] | 6488 | int drop_inode_on_err = 0; |
Yan, Zheng | a22285a | 2010-05-16 10:48:46 -0400 | [diff] [blame] | 6489 | int err; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 6490 | u64 objectid; |
Chris Mason | 00e4e6b | 2008-08-05 11:18:09 -0400 | [diff] [blame] | 6491 | u64 index = 0; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 6492 | |
Josef Bacik | 9ed74f2 | 2009-09-11 16:12:44 -0400 | [diff] [blame] | 6493 | /* |
| 6494 | * 2 for inode item and ref |
| 6495 | * 2 for dir items |
| 6496 | * 1 for xattr if selinux is on |
| 6497 | */ |
Yan, Zheng | a22285a | 2010-05-16 10:48:46 -0400 | [diff] [blame] | 6498 | trans = btrfs_start_transaction(root, 5); |
| 6499 | if (IS_ERR(trans)) |
| 6500 | return PTR_ERR(trans); |
Josef Bacik | 9ed74f2 | 2009-09-11 16:12:44 -0400 | [diff] [blame] | 6501 | |
Li Zefan | 581bb05 | 2011-04-20 10:06:11 +0800 | [diff] [blame] | 6502 | err = btrfs_find_free_ino(root, &objectid); |
| 6503 | if (err) |
| 6504 | goto out_unlock; |
| 6505 | |
Josef Bacik | aec7477 | 2008-07-24 12:12:38 -0400 | [diff] [blame] | 6506 | inode = btrfs_new_inode(trans, root, dir, dentry->d_name.name, |
Li Zefan | 33345d01 | 2011-04-20 10:31:50 +0800 | [diff] [blame] | 6507 | dentry->d_name.len, btrfs_ino(dir), objectid, |
Josef Bacik | d82a6f1 | 2011-05-11 15:26:06 -0400 | [diff] [blame] | 6508 | mode, &index); |
Tsutomu Itoh | 7cf96da | 2011-04-25 19:43:53 -0400 | [diff] [blame] | 6509 | if (IS_ERR(inode)) { |
| 6510 | err = PTR_ERR(inode); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 6511 | goto out_unlock; |
Tsutomu Itoh | 7cf96da | 2011-04-25 19:43:53 -0400 | [diff] [blame] | 6512 | } |
Filipe Brandenburger | 43baa57 | 2012-11-30 03:40:09 +0000 | [diff] [blame] | 6513 | drop_inode_on_err = 1; |
Casey Schaufler | ad19db7 | 2011-12-15 10:09:07 -0500 | [diff] [blame] | 6514 | /* |
| 6515 | * If the active LSM wants to access the inode during |
| 6516 | * d_instantiate it needs these. Smack checks to see |
| 6517 | * if the filesystem supports xattrs by looking at the |
| 6518 | * ops vector. |
| 6519 | */ |
| 6520 | inode->i_fop = &btrfs_file_operations; |
| 6521 | inode->i_op = &btrfs_file_inode_operations; |
Chris Mason | b0d5d10 | 2014-09-08 13:08:51 -0700 | [diff] [blame] | 6522 | inode->i_mapping->a_ops = &btrfs_aops; |
Chris Mason | b0d5d10 | 2014-09-08 13:08:51 -0700 | [diff] [blame] | 6523 | |
| 6524 | err = btrfs_init_inode_security(trans, inode, dir, &dentry->d_name); |
| 6525 | if (err) |
| 6526 | goto out_unlock_inode; |
| 6527 | |
| 6528 | err = btrfs_update_inode(trans, root, inode); |
| 6529 | if (err) |
| 6530 | goto out_unlock_inode; |
Casey Schaufler | ad19db7 | 2011-12-15 10:09:07 -0500 | [diff] [blame] | 6531 | |
Josef Bacik | a1b075d | 2010-11-19 20:36:11 +0000 | [diff] [blame] | 6532 | err = btrfs_add_nondir(trans, dir, dentry, inode, 0, index); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 6533 | if (err) |
Chris Mason | b0d5d10 | 2014-09-08 13:08:51 -0700 | [diff] [blame] | 6534 | goto out_unlock_inode; |
Filipe Brandenburger | 43baa57 | 2012-11-30 03:40:09 +0000 | [diff] [blame] | 6535 | |
Filipe Brandenburger | 43baa57 | 2012-11-30 03:40:09 +0000 | [diff] [blame] | 6536 | BTRFS_I(inode)->io_tree.ops = &btrfs_extent_io_ops; |
Chris Mason | b0d5d10 | 2014-09-08 13:08:51 -0700 | [diff] [blame] | 6537 | unlock_new_inode(inode); |
Filipe Brandenburger | 43baa57 | 2012-11-30 03:40:09 +0000 | [diff] [blame] | 6538 | d_instantiate(dentry, inode); |
| 6539 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 6540 | out_unlock: |
Josef Bacik | 7ad85bb | 2012-01-12 19:10:12 -0500 | [diff] [blame] | 6541 | btrfs_end_transaction(trans, root); |
Filipe Brandenburger | 43baa57 | 2012-11-30 03:40:09 +0000 | [diff] [blame] | 6542 | if (err && drop_inode_on_err) { |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 6543 | inode_dec_link_count(inode); |
| 6544 | iput(inode); |
| 6545 | } |
Josef Bacik | c581afc | 2014-02-06 16:06:06 -0500 | [diff] [blame] | 6546 | btrfs_balance_delayed_items(root); |
Liu Bo | b53d3f5 | 2012-11-14 14:34:34 +0000 | [diff] [blame] | 6547 | btrfs_btree_balance_dirty(root); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 6548 | return err; |
Chris Mason | b0d5d10 | 2014-09-08 13:08:51 -0700 | [diff] [blame] | 6549 | |
| 6550 | out_unlock_inode: |
| 6551 | unlock_new_inode(inode); |
| 6552 | goto out_unlock; |
| 6553 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 6554 | } |
| 6555 | |
| 6556 | static int btrfs_link(struct dentry *old_dentry, struct inode *dir, |
| 6557 | struct dentry *dentry) |
| 6558 | { |
Filipe Manana | 271dba4 | 2016-01-05 16:24:05 +0000 | [diff] [blame] | 6559 | struct btrfs_trans_handle *trans = NULL; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 6560 | struct btrfs_root *root = BTRFS_I(dir)->root; |
David Howells | 2b0143b | 2015-03-17 22:25:59 +0000 | [diff] [blame] | 6561 | struct inode *inode = d_inode(old_dentry); |
Chris Mason | 00e4e6b | 2008-08-05 11:18:09 -0400 | [diff] [blame] | 6562 | u64 index; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 6563 | int err; |
| 6564 | int drop_inode = 0; |
| 6565 | |
TARUISI Hiroaki | 4a8be42 | 2009-11-12 07:14:26 +0000 | [diff] [blame] | 6566 | /* do not allow sys_link's with other subvols of the same device */ |
| 6567 | if (root->objectid != BTRFS_I(inode)->root->objectid) |
Mark Fasheh | 3ab3564 | 2011-03-22 17:20:26 +0000 | [diff] [blame] | 6568 | return -EXDEV; |
TARUISI Hiroaki | 4a8be42 | 2009-11-12 07:14:26 +0000 | [diff] [blame] | 6569 | |
Mark Fasheh | f186373 | 2012-08-08 11:32:27 -0700 | [diff] [blame] | 6570 | if (inode->i_nlink >= BTRFS_LINK_MAX) |
Al Viro | c055e99 | 2011-03-04 17:15:18 +0000 | [diff] [blame] | 6571 | return -EMLINK; |
Josef Bacik | 9ed74f2 | 2009-09-11 16:12:44 -0400 | [diff] [blame] | 6572 | |
Chris Mason | 3de4586 | 2008-11-17 21:02:50 -0500 | [diff] [blame] | 6573 | err = btrfs_set_inode_index(dir, &index); |
Josef Bacik | aec7477 | 2008-07-24 12:12:38 -0400 | [diff] [blame] | 6574 | if (err) |
| 6575 | goto fail; |
| 6576 | |
Yan, Zheng | a22285a | 2010-05-16 10:48:46 -0400 | [diff] [blame] | 6577 | /* |
Miao Xie | 7e6b646 | 2011-02-18 09:21:17 +0000 | [diff] [blame] | 6578 | * 2 items for inode and inode ref |
Yan, Zheng | a22285a | 2010-05-16 10:48:46 -0400 | [diff] [blame] | 6579 | * 2 items for dir items |
Miao Xie | 7e6b646 | 2011-02-18 09:21:17 +0000 | [diff] [blame] | 6580 | * 1 item for parent inode |
Yan, Zheng | a22285a | 2010-05-16 10:48:46 -0400 | [diff] [blame] | 6581 | */ |
Miao Xie | 7e6b646 | 2011-02-18 09:21:17 +0000 | [diff] [blame] | 6582 | trans = btrfs_start_transaction(root, 5); |
Yan, Zheng | a22285a | 2010-05-16 10:48:46 -0400 | [diff] [blame] | 6583 | if (IS_ERR(trans)) { |
| 6584 | err = PTR_ERR(trans); |
Filipe Manana | 271dba4 | 2016-01-05 16:24:05 +0000 | [diff] [blame] | 6585 | trans = NULL; |
Yan, Zheng | a22285a | 2010-05-16 10:48:46 -0400 | [diff] [blame] | 6586 | goto fail; |
| 6587 | } |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 6588 | |
Miao Xie | 67de117 | 2013-12-26 13:07:06 +0800 | [diff] [blame] | 6589 | /* There are several dir indexes for this inode, clear the cache. */ |
| 6590 | BTRFS_I(inode)->dir_index = 0ULL; |
Zach Brown | 8b558c5 | 2013-10-16 12:10:34 -0700 | [diff] [blame] | 6591 | inc_nlink(inode); |
Josef Bacik | 0c4d2d9 | 2012-04-05 15:03:02 -0400 | [diff] [blame] | 6592 | inode_inc_iversion(inode); |
Deepa Dinamani | 04b285f | 2016-02-06 23:57:21 -0800 | [diff] [blame] | 6593 | inode->i_ctime = current_fs_time(inode->i_sb); |
Al Viro | 7de9c6ee | 2010-10-23 11:11:40 -0400 | [diff] [blame] | 6594 | ihold(inode); |
Josef Bacik | e997615 | 2012-10-11 15:53:56 -0400 | [diff] [blame] | 6595 | set_bit(BTRFS_INODE_COPY_EVERYTHING, &BTRFS_I(inode)->runtime_flags); |
Josef Bacik | aec7477 | 2008-07-24 12:12:38 -0400 | [diff] [blame] | 6596 | |
Josef Bacik | a1b075d | 2010-11-19 20:36:11 +0000 | [diff] [blame] | 6597 | err = btrfs_add_nondir(trans, dir, dentry, inode, 1, index); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 6598 | |
Yan, Zheng | a571952 | 2009-09-24 09:17:31 -0400 | [diff] [blame] | 6599 | if (err) { |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 6600 | drop_inode = 1; |
Yan, Zheng | a571952 | 2009-09-24 09:17:31 -0400 | [diff] [blame] | 6601 | } else { |
Al Viro | 10d9f30 | 2011-07-16 23:09:10 -0400 | [diff] [blame] | 6602 | struct dentry *parent = dentry->d_parent; |
Yan, Zheng | a571952 | 2009-09-24 09:17:31 -0400 | [diff] [blame] | 6603 | err = btrfs_update_inode(trans, root, inode); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 6604 | if (err) |
| 6605 | goto fail; |
Filipe Manana | ef3b9af | 2014-04-27 20:40:45 +0100 | [diff] [blame] | 6606 | if (inode->i_nlink == 1) { |
| 6607 | /* |
| 6608 | * If new hard link count is 1, it's a file created |
| 6609 | * with open(2) O_TMPFILE flag. |
| 6610 | */ |
| 6611 | err = btrfs_orphan_del(trans, inode); |
| 6612 | if (err) |
| 6613 | goto fail; |
| 6614 | } |
Al Viro | 08c422c | 2011-12-23 07:58:13 -0500 | [diff] [blame] | 6615 | d_instantiate(dentry, inode); |
Josef Bacik | 6a91221 | 2010-11-20 09:48:00 +0000 | [diff] [blame] | 6616 | btrfs_log_new_name(trans, inode, NULL, parent); |
Yan, Zheng | a571952 | 2009-09-24 09:17:31 -0400 | [diff] [blame] | 6617 | } |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 6618 | |
Josef Bacik | c581afc | 2014-02-06 16:06:06 -0500 | [diff] [blame] | 6619 | btrfs_balance_delayed_items(root); |
Chris Mason | 1832a6d | 2007-12-21 16:27:21 -0500 | [diff] [blame] | 6620 | fail: |
Filipe Manana | 271dba4 | 2016-01-05 16:24:05 +0000 | [diff] [blame] | 6621 | if (trans) |
| 6622 | btrfs_end_transaction(trans, root); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 6623 | if (drop_inode) { |
| 6624 | inode_dec_link_count(inode); |
| 6625 | iput(inode); |
| 6626 | } |
Liu Bo | b53d3f5 | 2012-11-14 14:34:34 +0000 | [diff] [blame] | 6627 | btrfs_btree_balance_dirty(root); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 6628 | return err; |
| 6629 | } |
| 6630 | |
Al Viro | 18bb1db | 2011-07-26 01:41:39 -0400 | [diff] [blame] | 6631 | 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] | 6632 | { |
Chris Mason | b9d8666 | 2008-05-02 16:13:49 -0400 | [diff] [blame] | 6633 | struct inode *inode = NULL; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 6634 | struct btrfs_trans_handle *trans; |
| 6635 | struct btrfs_root *root = BTRFS_I(dir)->root; |
| 6636 | int err = 0; |
| 6637 | int drop_on_err = 0; |
Chris Mason | b9d8666 | 2008-05-02 16:13:49 -0400 | [diff] [blame] | 6638 | u64 objectid = 0; |
Chris Mason | 00e4e6b | 2008-08-05 11:18:09 -0400 | [diff] [blame] | 6639 | u64 index = 0; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 6640 | |
Josef Bacik | 9ed74f2 | 2009-09-11 16:12:44 -0400 | [diff] [blame] | 6641 | /* |
| 6642 | * 2 items for inode and ref |
| 6643 | * 2 items for dir items |
| 6644 | * 1 for xattr if selinux is on |
| 6645 | */ |
Yan, Zheng | a22285a | 2010-05-16 10:48:46 -0400 | [diff] [blame] | 6646 | trans = btrfs_start_transaction(root, 5); |
| 6647 | if (IS_ERR(trans)) |
| 6648 | return PTR_ERR(trans); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 6649 | |
Li Zefan | 581bb05 | 2011-04-20 10:06:11 +0800 | [diff] [blame] | 6650 | err = btrfs_find_free_ino(root, &objectid); |
| 6651 | if (err) |
| 6652 | goto out_fail; |
| 6653 | |
Josef Bacik | aec7477 | 2008-07-24 12:12:38 -0400 | [diff] [blame] | 6654 | inode = btrfs_new_inode(trans, root, dir, dentry->d_name.name, |
Li Zefan | 33345d01 | 2011-04-20 10:31:50 +0800 | [diff] [blame] | 6655 | dentry->d_name.len, btrfs_ino(dir), objectid, |
Josef Bacik | d82a6f1 | 2011-05-11 15:26:06 -0400 | [diff] [blame] | 6656 | S_IFDIR | mode, &index); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 6657 | if (IS_ERR(inode)) { |
| 6658 | err = PTR_ERR(inode); |
| 6659 | goto out_fail; |
| 6660 | } |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 6661 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 6662 | drop_on_err = 1; |
Chris Mason | b0d5d10 | 2014-09-08 13:08:51 -0700 | [diff] [blame] | 6663 | /* these must be set before we unlock the inode */ |
| 6664 | inode->i_op = &btrfs_dir_inode_operations; |
| 6665 | inode->i_fop = &btrfs_dir_file_operations; |
Josef Bacik | 33268ea | 2008-07-24 12:16:36 -0400 | [diff] [blame] | 6666 | |
Eric Paris | 2a7dba3 | 2011-02-01 11:05:39 -0500 | [diff] [blame] | 6667 | err = btrfs_init_inode_security(trans, inode, dir, &dentry->d_name); |
Josef Bacik | 33268ea | 2008-07-24 12:16:36 -0400 | [diff] [blame] | 6668 | if (err) |
Chris Mason | b0d5d10 | 2014-09-08 13:08:51 -0700 | [diff] [blame] | 6669 | goto out_fail_inode; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 6670 | |
Chris Mason | dbe674a | 2008-07-17 12:54:05 -0400 | [diff] [blame] | 6671 | btrfs_i_size_write(inode, 0); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 6672 | err = btrfs_update_inode(trans, root, inode); |
| 6673 | if (err) |
Chris Mason | b0d5d10 | 2014-09-08 13:08:51 -0700 | [diff] [blame] | 6674 | goto out_fail_inode; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 6675 | |
Josef Bacik | a1b075d | 2010-11-19 20:36:11 +0000 | [diff] [blame] | 6676 | err = btrfs_add_link(trans, dir, inode, dentry->d_name.name, |
| 6677 | dentry->d_name.len, 0, index); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 6678 | if (err) |
Chris Mason | b0d5d10 | 2014-09-08 13:08:51 -0700 | [diff] [blame] | 6679 | goto out_fail_inode; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 6680 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 6681 | d_instantiate(dentry, inode); |
Chris Mason | b0d5d10 | 2014-09-08 13:08:51 -0700 | [diff] [blame] | 6682 | /* |
| 6683 | * mkdir is special. We're unlocking after we call d_instantiate |
| 6684 | * to avoid a race with nfsd calling d_instantiate. |
| 6685 | */ |
| 6686 | unlock_new_inode(inode); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 6687 | drop_on_err = 0; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 6688 | |
| 6689 | out_fail: |
Josef Bacik | 7ad85bb | 2012-01-12 19:10:12 -0500 | [diff] [blame] | 6690 | btrfs_end_transaction(trans, root); |
Wang Shilong | c7cfb8a | 2014-12-24 14:45:30 +0800 | [diff] [blame] | 6691 | if (drop_on_err) { |
| 6692 | inode_dec_link_count(inode); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 6693 | iput(inode); |
Wang Shilong | c7cfb8a | 2014-12-24 14:45:30 +0800 | [diff] [blame] | 6694 | } |
Josef Bacik | c581afc | 2014-02-06 16:06:06 -0500 | [diff] [blame] | 6695 | btrfs_balance_delayed_items(root); |
Liu Bo | b53d3f5 | 2012-11-14 14:34:34 +0000 | [diff] [blame] | 6696 | btrfs_btree_balance_dirty(root); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 6697 | return err; |
Chris Mason | b0d5d10 | 2014-09-08 13:08:51 -0700 | [diff] [blame] | 6698 | |
| 6699 | out_fail_inode: |
| 6700 | unlock_new_inode(inode); |
| 6701 | goto out_fail; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 6702 | } |
| 6703 | |
Qu Wenruo | e6c4efd | 2014-09-17 11:53:35 +0800 | [diff] [blame] | 6704 | /* Find next extent map of a given extent map, caller needs to ensure locks */ |
| 6705 | static struct extent_map *next_extent_map(struct extent_map *em) |
| 6706 | { |
| 6707 | struct rb_node *next; |
| 6708 | |
| 6709 | next = rb_next(&em->rb_node); |
| 6710 | if (!next) |
| 6711 | return NULL; |
| 6712 | return container_of(next, struct extent_map, rb_node); |
| 6713 | } |
| 6714 | |
| 6715 | static struct extent_map *prev_extent_map(struct extent_map *em) |
| 6716 | { |
| 6717 | struct rb_node *prev; |
| 6718 | |
| 6719 | prev = rb_prev(&em->rb_node); |
| 6720 | if (!prev) |
| 6721 | return NULL; |
| 6722 | return container_of(prev, struct extent_map, rb_node); |
| 6723 | } |
| 6724 | |
Chris Mason | d352ac6 | 2008-09-29 15:18:18 -0400 | [diff] [blame] | 6725 | /* helper for btfs_get_extent. Given an existing extent in the tree, |
Qu Wenruo | e6c4efd | 2014-09-17 11:53:35 +0800 | [diff] [blame] | 6726 | * the existing extent is the nearest extent to map_start, |
Chris Mason | d352ac6 | 2008-09-29 15:18:18 -0400 | [diff] [blame] | 6727 | * 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] | 6728 | * the best fitted new extent into the tree. |
Chris Mason | d352ac6 | 2008-09-29 15:18:18 -0400 | [diff] [blame] | 6729 | */ |
Chris Mason | 3b95151 | 2008-04-17 11:29:12 -0400 | [diff] [blame] | 6730 | static int merge_extent_mapping(struct extent_map_tree *em_tree, |
| 6731 | struct extent_map *existing, |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 6732 | struct extent_map *em, |
Qu Wenruo | 51f395a | 2014-08-08 13:06:20 +0800 | [diff] [blame] | 6733 | u64 map_start) |
Chris Mason | 3b95151 | 2008-04-17 11:29:12 -0400 | [diff] [blame] | 6734 | { |
Qu Wenruo | e6c4efd | 2014-09-17 11:53:35 +0800 | [diff] [blame] | 6735 | struct extent_map *prev; |
| 6736 | struct extent_map *next; |
| 6737 | u64 start; |
| 6738 | u64 end; |
Chris Mason | 3b95151 | 2008-04-17 11:29:12 -0400 | [diff] [blame] | 6739 | u64 start_diff; |
Chris Mason | 3b95151 | 2008-04-17 11:29:12 -0400 | [diff] [blame] | 6740 | |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 6741 | BUG_ON(map_start < em->start || map_start >= extent_map_end(em)); |
Qu Wenruo | e6c4efd | 2014-09-17 11:53:35 +0800 | [diff] [blame] | 6742 | |
| 6743 | if (existing->start > map_start) { |
| 6744 | next = existing; |
| 6745 | prev = prev_extent_map(next); |
| 6746 | } else { |
| 6747 | prev = existing; |
| 6748 | next = next_extent_map(prev); |
| 6749 | } |
| 6750 | |
| 6751 | start = prev ? extent_map_end(prev) : em->start; |
| 6752 | start = max_t(u64, start, em->start); |
| 6753 | end = next ? next->start : extent_map_end(em); |
| 6754 | end = min_t(u64, end, extent_map_end(em)); |
| 6755 | start_diff = start - em->start; |
| 6756 | em->start = start; |
| 6757 | em->len = end - start; |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 6758 | if (em->block_start < EXTENT_MAP_LAST_BYTE && |
| 6759 | !test_bit(EXTENT_FLAG_COMPRESSED, &em->flags)) { |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 6760 | em->block_start += start_diff; |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 6761 | em->block_len -= start_diff; |
| 6762 | } |
Josef Bacik | 09a2a8f9 | 2013-04-05 16:51:15 -0400 | [diff] [blame] | 6763 | return add_extent_mapping(em_tree, em, 0); |
Chris Mason | 3b95151 | 2008-04-17 11:29:12 -0400 | [diff] [blame] | 6764 | } |
| 6765 | |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 6766 | static noinline int uncompress_inline(struct btrfs_path *path, |
Byongho Lee | e40da0e | 2015-05-19 23:46:45 +0900 | [diff] [blame] | 6767 | struct page *page, |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 6768 | size_t pg_offset, u64 extent_offset, |
| 6769 | struct btrfs_file_extent_item *item) |
| 6770 | { |
| 6771 | int ret; |
| 6772 | struct extent_buffer *leaf = path->nodes[0]; |
| 6773 | char *tmp; |
| 6774 | size_t max_size; |
| 6775 | unsigned long inline_size; |
| 6776 | unsigned long ptr; |
Li Zefan | 261507a0 | 2010-12-17 14:21:50 +0800 | [diff] [blame] | 6777 | int compress_type; |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 6778 | |
| 6779 | WARN_ON(pg_offset != 0); |
Li Zefan | 261507a0 | 2010-12-17 14:21:50 +0800 | [diff] [blame] | 6780 | compress_type = btrfs_file_extent_compression(leaf, item); |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 6781 | max_size = btrfs_file_extent_ram_bytes(leaf, item); |
| 6782 | inline_size = btrfs_file_extent_inline_item_len(leaf, |
Ross Kirk | dd3cc16 | 2013-09-16 15:58:09 +0100 | [diff] [blame] | 6783 | btrfs_item_nr(path->slots[0])); |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 6784 | tmp = kmalloc(inline_size, GFP_NOFS); |
Tsutomu Itoh | 8d41371 | 2011-04-25 19:43:52 -0400 | [diff] [blame] | 6785 | if (!tmp) |
| 6786 | return -ENOMEM; |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 6787 | ptr = btrfs_file_extent_inline_start(item); |
| 6788 | |
| 6789 | read_extent_buffer(leaf, tmp, ptr, inline_size); |
| 6790 | |
Kirill A. Shutemov | 09cbfea | 2016-04-01 15:29:47 +0300 | [diff] [blame] | 6791 | max_size = min_t(unsigned long, PAGE_SIZE, max_size); |
Li Zefan | 261507a0 | 2010-12-17 14:21:50 +0800 | [diff] [blame] | 6792 | ret = btrfs_decompress(compress_type, tmp, page, |
| 6793 | extent_offset, inline_size, max_size); |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 6794 | kfree(tmp); |
Zach Brown | 166ae5a | 2014-05-09 17:15:10 -0400 | [diff] [blame] | 6795 | return ret; |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 6796 | } |
| 6797 | |
Chris Mason | d352ac6 | 2008-09-29 15:18:18 -0400 | [diff] [blame] | 6798 | /* |
| 6799 | * 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] | 6800 | * the ugly parts come from merging extents from the disk with the in-ram |
| 6801 | * representation. This gets more complex because of the data=ordered code, |
Chris Mason | d352ac6 | 2008-09-29 15:18:18 -0400 | [diff] [blame] | 6802 | * where the in-ram extents might be locked pending data=ordered completion. |
| 6803 | * |
| 6804 | * This also copies inline extents directly into the page. |
| 6805 | */ |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 6806 | |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 6807 | struct extent_map *btrfs_get_extent(struct inode *inode, struct page *page, |
Chris Mason | 70dec80 | 2008-01-29 09:59:12 -0500 | [diff] [blame] | 6808 | size_t pg_offset, u64 start, u64 len, |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 6809 | int create) |
| 6810 | { |
| 6811 | int ret; |
| 6812 | int err = 0; |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 6813 | u64 extent_start = 0; |
| 6814 | u64 extent_end = 0; |
Li Zefan | 33345d01 | 2011-04-20 10:31:50 +0800 | [diff] [blame] | 6815 | u64 objectid = btrfs_ino(inode); |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 6816 | u32 found_type; |
Chris Mason | f421950 | 2008-07-22 11:18:09 -0400 | [diff] [blame] | 6817 | struct btrfs_path *path = NULL; |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 6818 | struct btrfs_root *root = BTRFS_I(inode)->root; |
| 6819 | struct btrfs_file_extent_item *item; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 6820 | struct extent_buffer *leaf; |
| 6821 | struct btrfs_key found_key; |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 6822 | struct extent_map *em = NULL; |
| 6823 | struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree; |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 6824 | struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree; |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 6825 | struct btrfs_trans_handle *trans = NULL; |
Filipe Manana | 7ffbb59 | 2014-06-09 03:48:05 +0100 | [diff] [blame] | 6826 | const bool new_inline = !page || create; |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 6827 | |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 6828 | again: |
Chris Mason | 890871b | 2009-09-02 16:24:52 -0400 | [diff] [blame] | 6829 | read_lock(&em_tree->lock); |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 6830 | em = lookup_extent_mapping(em_tree, start, len); |
Chris Mason | a061fc8 | 2008-05-07 11:43:44 -0400 | [diff] [blame] | 6831 | if (em) |
| 6832 | em->bdev = root->fs_info->fs_devices->latest_bdev; |
Chris Mason | 890871b | 2009-09-02 16:24:52 -0400 | [diff] [blame] | 6833 | read_unlock(&em_tree->lock); |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 6834 | |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 6835 | if (em) { |
Chris Mason | e1c4b74 | 2008-04-22 13:26:46 -0400 | [diff] [blame] | 6836 | if (em->start > start || em->start + em->len <= start) |
| 6837 | free_extent_map(em); |
| 6838 | else if (em->block_start == EXTENT_MAP_INLINE && page) |
Chris Mason | 70dec80 | 2008-01-29 09:59:12 -0500 | [diff] [blame] | 6839 | free_extent_map(em); |
| 6840 | else |
| 6841 | goto out; |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 6842 | } |
David Sterba | 172ddd6 | 2011-04-21 00:48:27 +0200 | [diff] [blame] | 6843 | em = alloc_extent_map(); |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 6844 | if (!em) { |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 6845 | err = -ENOMEM; |
| 6846 | goto out; |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 6847 | } |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 6848 | em->bdev = root->fs_info->fs_devices->latest_bdev; |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 6849 | em->start = EXTENT_MAP_HOLE; |
Chris Mason | 445a694 | 2008-11-10 11:53:33 -0500 | [diff] [blame] | 6850 | em->orig_start = EXTENT_MAP_HOLE; |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 6851 | em->len = (u64)-1; |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 6852 | em->block_len = (u64)-1; |
Chris Mason | f421950 | 2008-07-22 11:18:09 -0400 | [diff] [blame] | 6853 | |
| 6854 | if (!path) { |
| 6855 | path = btrfs_alloc_path(); |
Josef Bacik | 026fd31 | 2011-05-13 10:32:11 -0400 | [diff] [blame] | 6856 | if (!path) { |
| 6857 | err = -ENOMEM; |
| 6858 | goto out; |
| 6859 | } |
| 6860 | /* |
| 6861 | * Chances are we'll be called again, so go ahead and do |
| 6862 | * readahead |
| 6863 | */ |
David Sterba | e4058b5 | 2015-11-27 16:31:35 +0100 | [diff] [blame] | 6864 | path->reada = READA_FORWARD; |
Chris Mason | f421950 | 2008-07-22 11:18:09 -0400 | [diff] [blame] | 6865 | } |
| 6866 | |
Chris Mason | 179e29e | 2007-11-01 11:28:41 -0400 | [diff] [blame] | 6867 | ret = btrfs_lookup_file_extent(trans, root, path, |
| 6868 | objectid, start, trans != NULL); |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 6869 | if (ret < 0) { |
| 6870 | err = ret; |
| 6871 | goto out; |
| 6872 | } |
| 6873 | |
| 6874 | if (ret != 0) { |
| 6875 | if (path->slots[0] == 0) |
| 6876 | goto not_found; |
| 6877 | path->slots[0]--; |
| 6878 | } |
| 6879 | |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 6880 | leaf = path->nodes[0]; |
| 6881 | item = btrfs_item_ptr(leaf, path->slots[0], |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 6882 | struct btrfs_file_extent_item); |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 6883 | /* are we inside the extent that was found? */ |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 6884 | btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]); |
David Sterba | 962a298 | 2014-06-04 18:41:45 +0200 | [diff] [blame] | 6885 | found_type = found_key.type; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 6886 | if (found_key.objectid != objectid || |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 6887 | found_type != BTRFS_EXTENT_DATA_KEY) { |
Josef Bacik | 25a5034 | 2013-10-14 12:08:38 -0400 | [diff] [blame] | 6888 | /* |
| 6889 | * If we backup past the first extent we want to move forward |
| 6890 | * and see if there is an extent in front of us, otherwise we'll |
| 6891 | * say there is a hole for our whole search range which can |
| 6892 | * cause problems. |
| 6893 | */ |
| 6894 | extent_end = start; |
| 6895 | goto next; |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 6896 | } |
| 6897 | |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 6898 | found_type = btrfs_file_extent_type(leaf, item); |
| 6899 | extent_start = found_key.offset; |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 6900 | if (found_type == BTRFS_FILE_EXTENT_REG || |
| 6901 | found_type == BTRFS_FILE_EXTENT_PREALLOC) { |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 6902 | extent_end = extent_start + |
Chris Mason | db94535 | 2007-10-15 16:15:53 -0400 | [diff] [blame] | 6903 | btrfs_file_extent_num_bytes(leaf, item); |
Yan Zheng | 9036c10 | 2008-10-30 14:19:41 -0400 | [diff] [blame] | 6904 | } else if (found_type == BTRFS_FILE_EXTENT_INLINE) { |
| 6905 | size_t size; |
Chris Mason | 514ac8a | 2014-01-03 21:07:00 -0800 | [diff] [blame] | 6906 | size = btrfs_file_extent_inline_len(leaf, path->slots[0], item); |
Qu Wenruo | fda2832 | 2013-02-26 08:10:22 +0000 | [diff] [blame] | 6907 | extent_end = ALIGN(extent_start + size, root->sectorsize); |
Yan Zheng | 9036c10 | 2008-10-30 14:19:41 -0400 | [diff] [blame] | 6908 | } |
Josef Bacik | 25a5034 | 2013-10-14 12:08:38 -0400 | [diff] [blame] | 6909 | next: |
Yan Zheng | 9036c10 | 2008-10-30 14:19:41 -0400 | [diff] [blame] | 6910 | if (start >= extent_end) { |
| 6911 | path->slots[0]++; |
| 6912 | if (path->slots[0] >= btrfs_header_nritems(leaf)) { |
| 6913 | ret = btrfs_next_leaf(root, path); |
| 6914 | if (ret < 0) { |
| 6915 | err = ret; |
| 6916 | goto out; |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 6917 | } |
Yan Zheng | 9036c10 | 2008-10-30 14:19:41 -0400 | [diff] [blame] | 6918 | if (ret > 0) |
| 6919 | goto not_found; |
| 6920 | leaf = path->nodes[0]; |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 6921 | } |
Yan Zheng | 9036c10 | 2008-10-30 14:19:41 -0400 | [diff] [blame] | 6922 | btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]); |
| 6923 | if (found_key.objectid != objectid || |
| 6924 | found_key.type != BTRFS_EXTENT_DATA_KEY) |
| 6925 | goto not_found; |
| 6926 | if (start + len <= found_key.offset) |
| 6927 | goto not_found; |
Wang Shilong | e2eca69 | 2014-07-17 11:44:14 +0800 | [diff] [blame] | 6928 | if (start > found_key.offset) |
| 6929 | goto next; |
Yan Zheng | 9036c10 | 2008-10-30 14:19:41 -0400 | [diff] [blame] | 6930 | em->start = start; |
Josef Bacik | 70c8a91 | 2012-10-11 16:54:30 -0400 | [diff] [blame] | 6931 | em->orig_start = start; |
Yan Zheng | 9036c10 | 2008-10-30 14:19:41 -0400 | [diff] [blame] | 6932 | em->len = found_key.offset - start; |
| 6933 | goto not_found_em; |
| 6934 | } |
| 6935 | |
Filipe Manana | 7ffbb59 | 2014-06-09 03:48:05 +0100 | [diff] [blame] | 6936 | btrfs_extent_item_to_extent_map(inode, path, item, new_inline, em); |
| 6937 | |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 6938 | if (found_type == BTRFS_FILE_EXTENT_REG || |
| 6939 | found_type == BTRFS_FILE_EXTENT_PREALLOC) { |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 6940 | goto insert; |
| 6941 | } else if (found_type == BTRFS_FILE_EXTENT_INLINE) { |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 6942 | unsigned long ptr; |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 6943 | char *map; |
Chris Mason | 3326d1b | 2007-10-15 16:18:25 -0400 | [diff] [blame] | 6944 | size_t size; |
| 6945 | size_t extent_offset; |
| 6946 | size_t copy_size; |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 6947 | |
Filipe Manana | 7ffbb59 | 2014-06-09 03:48:05 +0100 | [diff] [blame] | 6948 | if (new_inline) |
Yan | 689f934 | 2007-10-29 11:41:07 -0400 | [diff] [blame] | 6949 | goto out; |
Yan | 689f934 | 2007-10-29 11:41:07 -0400 | [diff] [blame] | 6950 | |
Chris Mason | 514ac8a | 2014-01-03 21:07:00 -0800 | [diff] [blame] | 6951 | size = btrfs_file_extent_inline_len(leaf, path->slots[0], item); |
Yan Zheng | 9036c10 | 2008-10-30 14:19:41 -0400 | [diff] [blame] | 6952 | extent_offset = page_offset(page) + pg_offset - extent_start; |
Kirill A. Shutemov | 09cbfea | 2016-04-01 15:29:47 +0300 | [diff] [blame] | 6953 | copy_size = min_t(u64, PAGE_SIZE - pg_offset, |
| 6954 | size - extent_offset); |
Chris Mason | 3326d1b | 2007-10-15 16:18:25 -0400 | [diff] [blame] | 6955 | em->start = extent_start + extent_offset; |
Qu Wenruo | fda2832 | 2013-02-26 08:10:22 +0000 | [diff] [blame] | 6956 | em->len = ALIGN(copy_size, root->sectorsize); |
Josef Bacik | b493968 | 2012-12-03 10:31:19 -0500 | [diff] [blame] | 6957 | em->orig_block_len = em->len; |
Josef Bacik | 70c8a91 | 2012-10-11 16:54:30 -0400 | [diff] [blame] | 6958 | em->orig_start = em->start; |
Yan | 689f934 | 2007-10-29 11:41:07 -0400 | [diff] [blame] | 6959 | ptr = btrfs_file_extent_inline_start(item) + extent_offset; |
Chris Mason | 179e29e | 2007-11-01 11:28:41 -0400 | [diff] [blame] | 6960 | if (create == 0 && !PageUptodate(page)) { |
Li Zefan | 261507a0 | 2010-12-17 14:21:50 +0800 | [diff] [blame] | 6961 | if (btrfs_file_extent_compression(leaf, item) != |
| 6962 | BTRFS_COMPRESS_NONE) { |
Byongho Lee | e40da0e | 2015-05-19 23:46:45 +0900 | [diff] [blame] | 6963 | ret = uncompress_inline(path, page, pg_offset, |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 6964 | extent_offset, item); |
Zach Brown | 166ae5a | 2014-05-09 17:15:10 -0400 | [diff] [blame] | 6965 | if (ret) { |
| 6966 | err = ret; |
| 6967 | goto out; |
| 6968 | } |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 6969 | } else { |
| 6970 | map = kmap(page); |
| 6971 | read_extent_buffer(leaf, map + pg_offset, ptr, |
| 6972 | copy_size); |
Kirill A. Shutemov | 09cbfea | 2016-04-01 15:29:47 +0300 | [diff] [blame] | 6973 | if (pg_offset + copy_size < PAGE_SIZE) { |
Chris Mason | 93c82d5 | 2009-09-11 12:36:29 -0400 | [diff] [blame] | 6974 | memset(map + pg_offset + copy_size, 0, |
Kirill A. Shutemov | 09cbfea | 2016-04-01 15:29:47 +0300 | [diff] [blame] | 6975 | PAGE_SIZE - pg_offset - |
Chris Mason | 93c82d5 | 2009-09-11 12:36:29 -0400 | [diff] [blame] | 6976 | copy_size); |
| 6977 | } |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 6978 | kunmap(page); |
| 6979 | } |
Chris Mason | 179e29e | 2007-11-01 11:28:41 -0400 | [diff] [blame] | 6980 | flush_dcache_page(page); |
| 6981 | } else if (create && PageUptodate(page)) { |
Jan Schmidt | 6bf7e08 | 2011-12-01 14:35:19 +0100 | [diff] [blame] | 6982 | BUG(); |
Chris Mason | 179e29e | 2007-11-01 11:28:41 -0400 | [diff] [blame] | 6983 | if (!trans) { |
| 6984 | kunmap(page); |
| 6985 | free_extent_map(em); |
| 6986 | em = NULL; |
Chris Mason | ff5714c | 2011-05-28 07:00:39 -0400 | [diff] [blame] | 6987 | |
David Sterba | b3b4aa7 | 2011-04-21 01:20:15 +0200 | [diff] [blame] | 6988 | btrfs_release_path(path); |
Josef Bacik | 7a7eaa4 | 2011-04-13 12:54:33 -0400 | [diff] [blame] | 6989 | trans = btrfs_join_transaction(root); |
Chris Mason | ff5714c | 2011-05-28 07:00:39 -0400 | [diff] [blame] | 6990 | |
Tsutomu Itoh | 3612b49 | 2011-01-25 02:51:38 +0000 | [diff] [blame] | 6991 | if (IS_ERR(trans)) |
| 6992 | return ERR_CAST(trans); |
Chris Mason | 179e29e | 2007-11-01 11:28:41 -0400 | [diff] [blame] | 6993 | goto again; |
| 6994 | } |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 6995 | map = kmap(page); |
Chris Mason | 70dec80 | 2008-01-29 09:59:12 -0500 | [diff] [blame] | 6996 | write_extent_buffer(leaf, map + pg_offset, ptr, |
Chris Mason | 179e29e | 2007-11-01 11:28:41 -0400 | [diff] [blame] | 6997 | copy_size); |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 6998 | kunmap(page); |
Chris Mason | 179e29e | 2007-11-01 11:28:41 -0400 | [diff] [blame] | 6999 | btrfs_mark_buffer_dirty(leaf); |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 7000 | } |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 7001 | set_extent_uptodate(io_tree, em->start, |
Arne Jansen | 507903b | 2011-04-06 10:02:20 +0000 | [diff] [blame] | 7002 | extent_map_end(em) - 1, NULL, GFP_NOFS); |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 7003 | goto insert; |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 7004 | } |
| 7005 | not_found: |
| 7006 | em->start = start; |
Josef Bacik | 70c8a91 | 2012-10-11 16:54:30 -0400 | [diff] [blame] | 7007 | em->orig_start = start; |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 7008 | em->len = len; |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 7009 | not_found_em: |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 7010 | em->block_start = EXTENT_MAP_HOLE; |
Yan Zheng | 9036c10 | 2008-10-30 14:19:41 -0400 | [diff] [blame] | 7011 | set_bit(EXTENT_FLAG_VACANCY, &em->flags); |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 7012 | insert: |
David Sterba | b3b4aa7 | 2011-04-21 01:20:15 +0200 | [diff] [blame] | 7013 | btrfs_release_path(path); |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 7014 | if (em->start > start || extent_map_end(em) <= start) { |
Simon Kirby | c2cf52e | 2013-03-19 22:41:23 +0000 | [diff] [blame] | 7015 | btrfs_err(root->fs_info, "bad extent! em: [%llu %llu] passed [%llu %llu]", |
Geert Uytterhoeven | c1c9ff7 | 2013-08-20 13:20:07 +0200 | [diff] [blame] | 7016 | em->start, em->len, start, len); |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 7017 | err = -EIO; |
| 7018 | goto out; |
| 7019 | } |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 7020 | |
| 7021 | err = 0; |
Chris Mason | 890871b | 2009-09-02 16:24:52 -0400 | [diff] [blame] | 7022 | write_lock(&em_tree->lock); |
Josef Bacik | 09a2a8f9 | 2013-04-05 16:51:15 -0400 | [diff] [blame] | 7023 | ret = add_extent_mapping(em_tree, em, 0); |
Chris Mason | 3b95151 | 2008-04-17 11:29:12 -0400 | [diff] [blame] | 7024 | /* it is possible that someone inserted the extent into the tree |
| 7025 | * while we had the lock dropped. It is also possible that |
| 7026 | * an overlapping map exists in the tree |
| 7027 | */ |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 7028 | if (ret == -EEXIST) { |
Chris Mason | 3b95151 | 2008-04-17 11:29:12 -0400 | [diff] [blame] | 7029 | struct extent_map *existing; |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 7030 | |
| 7031 | ret = 0; |
| 7032 | |
Qu Wenruo | e6c4efd | 2014-09-17 11:53:35 +0800 | [diff] [blame] | 7033 | existing = search_extent_mapping(em_tree, start, len); |
| 7034 | /* |
| 7035 | * existing will always be non-NULL, since there must be |
| 7036 | * extent causing the -EEXIST. |
| 7037 | */ |
Chris Mason | 8dff9c8 | 2015-09-19 11:28:25 -0700 | [diff] [blame] | 7038 | if (existing->start == em->start && |
| 7039 | extent_map_end(existing) == extent_map_end(em) && |
| 7040 | em->block_start == existing->block_start) { |
| 7041 | /* |
| 7042 | * these two extents are the same, it happens |
| 7043 | * with inlines especially |
| 7044 | */ |
| 7045 | free_extent_map(em); |
| 7046 | em = existing; |
| 7047 | err = 0; |
| 7048 | |
| 7049 | } else if (start >= extent_map_end(existing) || |
Qu Wenruo | 32be3a1 | 2014-09-22 09:13:03 +0800 | [diff] [blame] | 7050 | start <= existing->start) { |
Qu Wenruo | e6c4efd | 2014-09-17 11:53:35 +0800 | [diff] [blame] | 7051 | /* |
| 7052 | * The existing extent map is the one nearest to |
| 7053 | * the [start, start + len) range which overlaps |
| 7054 | */ |
| 7055 | err = merge_extent_mapping(em_tree, existing, |
| 7056 | em, start); |
Chris Mason | e1c4b74 | 2008-04-22 13:26:46 -0400 | [diff] [blame] | 7057 | free_extent_map(existing); |
Qu Wenruo | e6c4efd | 2014-09-17 11:53:35 +0800 | [diff] [blame] | 7058 | if (err) { |
Chris Mason | 3b95151 | 2008-04-17 11:29:12 -0400 | [diff] [blame] | 7059 | free_extent_map(em); |
| 7060 | em = NULL; |
| 7061 | } |
| 7062 | } else { |
| 7063 | free_extent_map(em); |
| 7064 | em = existing; |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 7065 | err = 0; |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 7066 | } |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 7067 | } |
Chris Mason | 890871b | 2009-09-02 16:24:52 -0400 | [diff] [blame] | 7068 | write_unlock(&em_tree->lock); |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 7069 | out: |
liubo | 1abe9b8 | 2011-03-24 11:18:59 +0000 | [diff] [blame] | 7070 | |
Steven Rostedt | 4cd8587 | 2013-11-14 22:57:29 -0500 | [diff] [blame] | 7071 | trace_btrfs_get_extent(root, em); |
liubo | 1abe9b8 | 2011-03-24 11:18:59 +0000 | [diff] [blame] | 7072 | |
Tsutomu Itoh | 527afb4 | 2015-08-19 14:55:00 +0900 | [diff] [blame] | 7073 | btrfs_free_path(path); |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 7074 | if (trans) { |
| 7075 | ret = btrfs_end_transaction(trans, root); |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 7076 | if (!err) |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 7077 | err = ret; |
| 7078 | } |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 7079 | if (err) { |
| 7080 | free_extent_map(em); |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 7081 | return ERR_PTR(err); |
| 7082 | } |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 7083 | BUG_ON(!em); /* Error is always set */ |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 7084 | return em; |
| 7085 | } |
| 7086 | |
Chris Mason | ec29ed5 | 2011-02-23 16:23:20 -0500 | [diff] [blame] | 7087 | struct extent_map *btrfs_get_extent_fiemap(struct inode *inode, struct page *page, |
| 7088 | size_t pg_offset, u64 start, u64 len, |
| 7089 | int create) |
| 7090 | { |
| 7091 | struct extent_map *em; |
| 7092 | struct extent_map *hole_em = NULL; |
| 7093 | u64 range_start = start; |
| 7094 | u64 end; |
| 7095 | u64 found; |
| 7096 | u64 found_end; |
| 7097 | int err = 0; |
| 7098 | |
| 7099 | em = btrfs_get_extent(inode, page, pg_offset, start, len, create); |
| 7100 | if (IS_ERR(em)) |
| 7101 | return em; |
| 7102 | if (em) { |
| 7103 | /* |
Liu Bo | f9e4fb5 | 2013-01-07 10:10:12 +0000 | [diff] [blame] | 7104 | * if our em maps to |
| 7105 | * - a hole or |
| 7106 | * - a pre-alloc extent, |
| 7107 | * there might actually be delalloc bytes behind it. |
Chris Mason | ec29ed5 | 2011-02-23 16:23:20 -0500 | [diff] [blame] | 7108 | */ |
Liu Bo | f9e4fb5 | 2013-01-07 10:10:12 +0000 | [diff] [blame] | 7109 | if (em->block_start != EXTENT_MAP_HOLE && |
| 7110 | !test_bit(EXTENT_FLAG_PREALLOC, &em->flags)) |
Chris Mason | ec29ed5 | 2011-02-23 16:23:20 -0500 | [diff] [blame] | 7111 | return em; |
| 7112 | else |
| 7113 | hole_em = em; |
| 7114 | } |
| 7115 | |
| 7116 | /* check to see if we've wrapped (len == -1 or similar) */ |
| 7117 | end = start + len; |
| 7118 | if (end < start) |
| 7119 | end = (u64)-1; |
| 7120 | else |
| 7121 | end -= 1; |
| 7122 | |
| 7123 | em = NULL; |
| 7124 | |
| 7125 | /* ok, we didn't find anything, lets look for delalloc */ |
| 7126 | found = count_range_bits(&BTRFS_I(inode)->io_tree, &range_start, |
| 7127 | end, len, EXTENT_DELALLOC, 1); |
| 7128 | found_end = range_start + found; |
| 7129 | if (found_end < range_start) |
| 7130 | found_end = (u64)-1; |
| 7131 | |
| 7132 | /* |
| 7133 | * we didn't find anything useful, return |
| 7134 | * the original results from get_extent() |
| 7135 | */ |
| 7136 | if (range_start > end || found_end <= start) { |
| 7137 | em = hole_em; |
| 7138 | hole_em = NULL; |
| 7139 | goto out; |
| 7140 | } |
| 7141 | |
| 7142 | /* adjust the range_start to make sure it doesn't |
| 7143 | * go backwards from the start they passed in |
| 7144 | */ |
Dulshani Gunawardhana | 6787125 | 2013-10-31 10:33:04 +0530 | [diff] [blame] | 7145 | range_start = max(start, range_start); |
Chris Mason | ec29ed5 | 2011-02-23 16:23:20 -0500 | [diff] [blame] | 7146 | found = found_end - range_start; |
| 7147 | |
| 7148 | if (found > 0) { |
| 7149 | u64 hole_start = start; |
| 7150 | u64 hole_len = len; |
| 7151 | |
David Sterba | 172ddd6 | 2011-04-21 00:48:27 +0200 | [diff] [blame] | 7152 | em = alloc_extent_map(); |
Chris Mason | ec29ed5 | 2011-02-23 16:23:20 -0500 | [diff] [blame] | 7153 | if (!em) { |
| 7154 | err = -ENOMEM; |
| 7155 | goto out; |
| 7156 | } |
| 7157 | /* |
| 7158 | * when btrfs_get_extent can't find anything it |
| 7159 | * returns one huge hole |
| 7160 | * |
| 7161 | * make sure what it found really fits our range, and |
| 7162 | * adjust to make sure it is based on the start from |
| 7163 | * the caller |
| 7164 | */ |
| 7165 | if (hole_em) { |
| 7166 | u64 calc_end = extent_map_end(hole_em); |
| 7167 | |
| 7168 | if (calc_end <= start || (hole_em->start > end)) { |
| 7169 | free_extent_map(hole_em); |
| 7170 | hole_em = NULL; |
| 7171 | } else { |
| 7172 | hole_start = max(hole_em->start, start); |
| 7173 | hole_len = calc_end - hole_start; |
| 7174 | } |
| 7175 | } |
| 7176 | em->bdev = NULL; |
| 7177 | if (hole_em && range_start > hole_start) { |
| 7178 | /* our hole starts before our delalloc, so we |
| 7179 | * have to return just the parts of the hole |
| 7180 | * that go until the delalloc starts |
| 7181 | */ |
| 7182 | em->len = min(hole_len, |
| 7183 | range_start - hole_start); |
| 7184 | em->start = hole_start; |
| 7185 | em->orig_start = hole_start; |
| 7186 | /* |
| 7187 | * don't adjust block start at all, |
| 7188 | * it is fixed at EXTENT_MAP_HOLE |
| 7189 | */ |
| 7190 | em->block_start = hole_em->block_start; |
| 7191 | em->block_len = hole_len; |
Liu Bo | f9e4fb5 | 2013-01-07 10:10:12 +0000 | [diff] [blame] | 7192 | if (test_bit(EXTENT_FLAG_PREALLOC, &hole_em->flags)) |
| 7193 | set_bit(EXTENT_FLAG_PREALLOC, &em->flags); |
Chris Mason | ec29ed5 | 2011-02-23 16:23:20 -0500 | [diff] [blame] | 7194 | } else { |
| 7195 | em->start = range_start; |
| 7196 | em->len = found; |
| 7197 | em->orig_start = range_start; |
| 7198 | em->block_start = EXTENT_MAP_DELALLOC; |
| 7199 | em->block_len = found; |
| 7200 | } |
| 7201 | } else if (hole_em) { |
| 7202 | return hole_em; |
| 7203 | } |
| 7204 | out: |
| 7205 | |
| 7206 | free_extent_map(hole_em); |
| 7207 | if (err) { |
| 7208 | free_extent_map(em); |
| 7209 | return ERR_PTR(err); |
| 7210 | } |
| 7211 | return em; |
| 7212 | } |
| 7213 | |
Filipe Manana | 5f9a8a5 | 2016-05-12 13:53:36 +0100 | [diff] [blame] | 7214 | static struct extent_map *btrfs_create_dio_extent(struct inode *inode, |
| 7215 | const u64 start, |
| 7216 | const u64 len, |
| 7217 | const u64 orig_start, |
| 7218 | const u64 block_start, |
| 7219 | const u64 block_len, |
| 7220 | const u64 orig_block_len, |
| 7221 | const u64 ram_bytes, |
| 7222 | const int type) |
| 7223 | { |
| 7224 | struct extent_map *em = NULL; |
| 7225 | int ret; |
| 7226 | |
| 7227 | down_read(&BTRFS_I(inode)->dio_sem); |
| 7228 | if (type != BTRFS_ORDERED_NOCOW) { |
| 7229 | em = create_pinned_em(inode, start, len, orig_start, |
| 7230 | block_start, block_len, orig_block_len, |
| 7231 | ram_bytes, type); |
| 7232 | if (IS_ERR(em)) |
| 7233 | goto out; |
| 7234 | } |
| 7235 | ret = btrfs_add_ordered_extent_dio(inode, start, block_start, |
| 7236 | len, block_len, type); |
| 7237 | if (ret) { |
| 7238 | if (em) { |
| 7239 | free_extent_map(em); |
| 7240 | btrfs_drop_extent_cache(inode, start, |
| 7241 | start + len - 1, 0); |
| 7242 | } |
| 7243 | em = ERR_PTR(ret); |
| 7244 | } |
| 7245 | out: |
| 7246 | up_read(&BTRFS_I(inode)->dio_sem); |
| 7247 | |
| 7248 | return em; |
| 7249 | } |
| 7250 | |
Josef Bacik | 4b46fce | 2010-05-23 11:00:55 -0400 | [diff] [blame] | 7251 | static struct extent_map *btrfs_new_extent_direct(struct inode *inode, |
| 7252 | u64 start, u64 len) |
| 7253 | { |
| 7254 | struct btrfs_root *root = BTRFS_I(inode)->root; |
Josef Bacik | 70c8a91 | 2012-10-11 16:54:30 -0400 | [diff] [blame] | 7255 | struct extent_map *em; |
Josef Bacik | 4b46fce | 2010-05-23 11:00:55 -0400 | [diff] [blame] | 7256 | struct btrfs_key ins; |
| 7257 | u64 alloc_hint; |
| 7258 | int ret; |
Josef Bacik | 4b46fce | 2010-05-23 11:00:55 -0400 | [diff] [blame] | 7259 | |
Josef Bacik | 4b46fce | 2010-05-23 11:00:55 -0400 | [diff] [blame] | 7260 | alloc_hint = get_extent_allocation_hint(inode, start, len); |
Wang Xiaoguang | 1851309 | 2016-07-25 15:51:40 +0800 | [diff] [blame] | 7261 | ret = btrfs_reserve_extent(root, len, len, root->sectorsize, 0, |
Miao Xie | e570fd2 | 2014-06-19 10:42:50 +0800 | [diff] [blame] | 7262 | alloc_hint, &ins, 1, 1); |
Josef Bacik | 0036158 | 2013-08-14 14:02:47 -0400 | [diff] [blame] | 7263 | if (ret) |
| 7264 | return ERR_PTR(ret); |
Josef Bacik | 4b46fce | 2010-05-23 11:00:55 -0400 | [diff] [blame] | 7265 | |
Filipe Manana | 5f9a8a5 | 2016-05-12 13:53:36 +0100 | [diff] [blame] | 7266 | em = btrfs_create_dio_extent(inode, start, ins.offset, start, |
| 7267 | ins.objectid, ins.offset, ins.offset, |
| 7268 | ins.offset, 0); |
Filipe Manana | 9cfa3e3 | 2016-04-26 15:39:32 +0100 | [diff] [blame] | 7269 | btrfs_dec_block_group_reservations(root->fs_info, ins.objectid); |
Filipe Manana | 5f9a8a5 | 2016-05-12 13:53:36 +0100 | [diff] [blame] | 7270 | if (IS_ERR(em)) |
Miao Xie | e570fd2 | 2014-06-19 10:42:50 +0800 | [diff] [blame] | 7271 | btrfs_free_reserved_extent(root, ins.objectid, ins.offset, 1); |
Josef Bacik | 0036158 | 2013-08-14 14:02:47 -0400 | [diff] [blame] | 7272 | |
Josef Bacik | 4b46fce | 2010-05-23 11:00:55 -0400 | [diff] [blame] | 7273 | return em; |
| 7274 | } |
| 7275 | |
Chris Mason | 46bfbb5 | 2010-05-26 11:04:10 -0400 | [diff] [blame] | 7276 | /* |
| 7277 | * returns 1 when the nocow is safe, < 1 on error, 0 if the |
| 7278 | * block must be cow'd |
| 7279 | */ |
Josef Bacik | 0036158 | 2013-08-14 14:02:47 -0400 | [diff] [blame] | 7280 | noinline int can_nocow_extent(struct inode *inode, u64 offset, u64 *len, |
Josef Bacik | 7ee9e44 | 2013-06-21 16:37:03 -0400 | [diff] [blame] | 7281 | u64 *orig_start, u64 *orig_block_len, |
| 7282 | u64 *ram_bytes) |
Chris Mason | 46bfbb5 | 2010-05-26 11:04:10 -0400 | [diff] [blame] | 7283 | { |
Josef Bacik | 0036158 | 2013-08-14 14:02:47 -0400 | [diff] [blame] | 7284 | struct btrfs_trans_handle *trans; |
Chris Mason | 46bfbb5 | 2010-05-26 11:04:10 -0400 | [diff] [blame] | 7285 | struct btrfs_path *path; |
| 7286 | int ret; |
| 7287 | struct extent_buffer *leaf; |
| 7288 | struct btrfs_root *root = BTRFS_I(inode)->root; |
Miao Xie | 7b2b708 | 2014-02-27 13:58:05 +0800 | [diff] [blame] | 7289 | struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree; |
Chris Mason | 46bfbb5 | 2010-05-26 11:04:10 -0400 | [diff] [blame] | 7290 | struct btrfs_file_extent_item *fi; |
| 7291 | struct btrfs_key key; |
| 7292 | u64 disk_bytenr; |
| 7293 | u64 backref_offset; |
| 7294 | u64 extent_end; |
| 7295 | u64 num_bytes; |
| 7296 | int slot; |
| 7297 | int found_type; |
Josef Bacik | 7ee9e44 | 2013-06-21 16:37:03 -0400 | [diff] [blame] | 7298 | bool nocow = (BTRFS_I(inode)->flags & BTRFS_INODE_NODATACOW); |
Miao Xie | e77751a | 2013-12-27 21:11:50 +0800 | [diff] [blame] | 7299 | |
Chris Mason | 46bfbb5 | 2010-05-26 11:04:10 -0400 | [diff] [blame] | 7300 | path = btrfs_alloc_path(); |
| 7301 | if (!path) |
| 7302 | return -ENOMEM; |
| 7303 | |
Josef Bacik | 0036158 | 2013-08-14 14:02:47 -0400 | [diff] [blame] | 7304 | ret = btrfs_lookup_file_extent(NULL, root, path, btrfs_ino(inode), |
Chris Mason | 46bfbb5 | 2010-05-26 11:04:10 -0400 | [diff] [blame] | 7305 | offset, 0); |
| 7306 | if (ret < 0) |
| 7307 | goto out; |
| 7308 | |
| 7309 | slot = path->slots[0]; |
| 7310 | if (ret == 1) { |
| 7311 | if (slot == 0) { |
| 7312 | /* can't find the item, must cow */ |
| 7313 | ret = 0; |
| 7314 | goto out; |
| 7315 | } |
| 7316 | slot--; |
| 7317 | } |
| 7318 | ret = 0; |
| 7319 | leaf = path->nodes[0]; |
| 7320 | btrfs_item_key_to_cpu(leaf, &key, slot); |
Li Zefan | 33345d01 | 2011-04-20 10:31:50 +0800 | [diff] [blame] | 7321 | if (key.objectid != btrfs_ino(inode) || |
Chris Mason | 46bfbb5 | 2010-05-26 11:04:10 -0400 | [diff] [blame] | 7322 | key.type != BTRFS_EXTENT_DATA_KEY) { |
| 7323 | /* not our file or wrong item type, must cow */ |
| 7324 | goto out; |
| 7325 | } |
| 7326 | |
| 7327 | if (key.offset > offset) { |
| 7328 | /* Wrong offset, must cow */ |
| 7329 | goto out; |
| 7330 | } |
| 7331 | |
| 7332 | fi = btrfs_item_ptr(leaf, slot, struct btrfs_file_extent_item); |
| 7333 | found_type = btrfs_file_extent_type(leaf, fi); |
| 7334 | if (found_type != BTRFS_FILE_EXTENT_REG && |
| 7335 | found_type != BTRFS_FILE_EXTENT_PREALLOC) { |
| 7336 | /* not a regular extent, must cow */ |
| 7337 | goto out; |
| 7338 | } |
Josef Bacik | 7ee9e44 | 2013-06-21 16:37:03 -0400 | [diff] [blame] | 7339 | |
| 7340 | if (!nocow && found_type == BTRFS_FILE_EXTENT_REG) |
| 7341 | goto out; |
| 7342 | |
Miao Xie | e77751a | 2013-12-27 21:11:50 +0800 | [diff] [blame] | 7343 | extent_end = key.offset + btrfs_file_extent_num_bytes(leaf, fi); |
| 7344 | if (extent_end <= offset) |
| 7345 | goto out; |
| 7346 | |
Chris Mason | 46bfbb5 | 2010-05-26 11:04:10 -0400 | [diff] [blame] | 7347 | disk_bytenr = btrfs_file_extent_disk_bytenr(leaf, fi); |
Josef Bacik | 7ee9e44 | 2013-06-21 16:37:03 -0400 | [diff] [blame] | 7348 | if (disk_bytenr == 0) |
| 7349 | goto out; |
| 7350 | |
| 7351 | if (btrfs_file_extent_compression(leaf, fi) || |
| 7352 | btrfs_file_extent_encryption(leaf, fi) || |
| 7353 | btrfs_file_extent_other_encoding(leaf, fi)) |
| 7354 | goto out; |
| 7355 | |
Chris Mason | 46bfbb5 | 2010-05-26 11:04:10 -0400 | [diff] [blame] | 7356 | backref_offset = btrfs_file_extent_offset(leaf, fi); |
| 7357 | |
Josef Bacik | 7ee9e44 | 2013-06-21 16:37:03 -0400 | [diff] [blame] | 7358 | if (orig_start) { |
| 7359 | *orig_start = key.offset - backref_offset; |
| 7360 | *orig_block_len = btrfs_file_extent_disk_num_bytes(leaf, fi); |
| 7361 | *ram_bytes = btrfs_file_extent_ram_bytes(leaf, fi); |
| 7362 | } |
Josef Bacik | eb384b5 | 2013-04-24 16:32:55 -0400 | [diff] [blame] | 7363 | |
Chris Mason | 46bfbb5 | 2010-05-26 11:04:10 -0400 | [diff] [blame] | 7364 | if (btrfs_extent_readonly(root, disk_bytenr)) |
| 7365 | goto out; |
Miao Xie | 7b2b708 | 2014-02-27 13:58:05 +0800 | [diff] [blame] | 7366 | |
| 7367 | num_bytes = min(offset + *len, extent_end) - offset; |
| 7368 | if (!nocow && found_type == BTRFS_FILE_EXTENT_PREALLOC) { |
| 7369 | u64 range_end; |
| 7370 | |
| 7371 | range_end = round_up(offset + num_bytes, root->sectorsize) - 1; |
| 7372 | ret = test_range_bit(io_tree, offset, range_end, |
| 7373 | EXTENT_DELALLOC, 0, NULL); |
| 7374 | if (ret) { |
| 7375 | ret = -EAGAIN; |
| 7376 | goto out; |
| 7377 | } |
| 7378 | } |
| 7379 | |
Josef Bacik | 1bda19e | 2013-10-18 12:10:36 -0400 | [diff] [blame] | 7380 | btrfs_release_path(path); |
Chris Mason | 46bfbb5 | 2010-05-26 11:04:10 -0400 | [diff] [blame] | 7381 | |
| 7382 | /* |
| 7383 | * look for other files referencing this extent, if we |
| 7384 | * find any we must cow |
| 7385 | */ |
Josef Bacik | 0036158 | 2013-08-14 14:02:47 -0400 | [diff] [blame] | 7386 | trans = btrfs_join_transaction(root); |
| 7387 | if (IS_ERR(trans)) { |
| 7388 | ret = 0; |
Chris Mason | 46bfbb5 | 2010-05-26 11:04:10 -0400 | [diff] [blame] | 7389 | goto out; |
Josef Bacik | 0036158 | 2013-08-14 14:02:47 -0400 | [diff] [blame] | 7390 | } |
| 7391 | |
| 7392 | ret = btrfs_cross_ref_exist(trans, root, btrfs_ino(inode), |
| 7393 | key.offset - backref_offset, disk_bytenr); |
| 7394 | btrfs_end_transaction(trans, root); |
| 7395 | if (ret) { |
| 7396 | ret = 0; |
| 7397 | goto out; |
| 7398 | } |
Chris Mason | 46bfbb5 | 2010-05-26 11:04:10 -0400 | [diff] [blame] | 7399 | |
| 7400 | /* |
| 7401 | * adjust disk_bytenr and num_bytes to cover just the bytes |
| 7402 | * in this extent we are about to write. If there |
| 7403 | * are any csums in that range we have to cow in order |
| 7404 | * to keep the csums correct |
| 7405 | */ |
| 7406 | disk_bytenr += backref_offset; |
| 7407 | disk_bytenr += offset - key.offset; |
Chris Mason | 46bfbb5 | 2010-05-26 11:04:10 -0400 | [diff] [blame] | 7408 | if (csum_exist_in_range(root, disk_bytenr, num_bytes)) |
| 7409 | goto out; |
| 7410 | /* |
| 7411 | * all of the above have passed, it is safe to overwrite this extent |
| 7412 | * without cow |
| 7413 | */ |
Josef Bacik | eb384b5 | 2013-04-24 16:32:55 -0400 | [diff] [blame] | 7414 | *len = num_bytes; |
Chris Mason | 46bfbb5 | 2010-05-26 11:04:10 -0400 | [diff] [blame] | 7415 | ret = 1; |
| 7416 | out: |
| 7417 | btrfs_free_path(path); |
| 7418 | return ret; |
| 7419 | } |
| 7420 | |
Alex Gartrell | fc4adbf | 2014-05-20 13:07:56 -0700 | [diff] [blame] | 7421 | bool btrfs_page_exists_in_range(struct inode *inode, loff_t start, loff_t end) |
| 7422 | { |
| 7423 | struct radix_tree_root *root = &inode->i_mapping->page_tree; |
| 7424 | int found = false; |
| 7425 | void **pagep = NULL; |
| 7426 | struct page *page = NULL; |
| 7427 | int start_idx; |
| 7428 | int end_idx; |
| 7429 | |
Kirill A. Shutemov | 09cbfea | 2016-04-01 15:29:47 +0300 | [diff] [blame] | 7430 | start_idx = start >> PAGE_SHIFT; |
Alex Gartrell | fc4adbf | 2014-05-20 13:07:56 -0700 | [diff] [blame] | 7431 | |
| 7432 | /* |
| 7433 | * end is the last byte in the last page. end == start is legal |
| 7434 | */ |
Kirill A. Shutemov | 09cbfea | 2016-04-01 15:29:47 +0300 | [diff] [blame] | 7435 | end_idx = end >> PAGE_SHIFT; |
Alex Gartrell | fc4adbf | 2014-05-20 13:07:56 -0700 | [diff] [blame] | 7436 | |
| 7437 | rcu_read_lock(); |
| 7438 | |
| 7439 | /* Most of the code in this while loop is lifted from |
| 7440 | * find_get_page. It's been modified to begin searching from a |
| 7441 | * page and return just the first page found in that range. If the |
| 7442 | * found idx is less than or equal to the end idx then we know that |
| 7443 | * a page exists. If no pages are found or if those pages are |
| 7444 | * outside of the range then we're fine (yay!) */ |
| 7445 | while (page == NULL && |
| 7446 | radix_tree_gang_lookup_slot(root, &pagep, NULL, start_idx, 1)) { |
| 7447 | page = radix_tree_deref_slot(pagep); |
| 7448 | if (unlikely(!page)) |
| 7449 | break; |
| 7450 | |
| 7451 | if (radix_tree_exception(page)) { |
Filipe Manana | 809f901 | 2014-06-05 13:22:25 +0100 | [diff] [blame] | 7452 | if (radix_tree_deref_retry(page)) { |
| 7453 | page = NULL; |
Alex Gartrell | fc4adbf | 2014-05-20 13:07:56 -0700 | [diff] [blame] | 7454 | continue; |
Filipe Manana | 809f901 | 2014-06-05 13:22:25 +0100 | [diff] [blame] | 7455 | } |
Alex Gartrell | fc4adbf | 2014-05-20 13:07:56 -0700 | [diff] [blame] | 7456 | /* |
| 7457 | * Otherwise, shmem/tmpfs must be storing a swap entry |
| 7458 | * here as an exceptional entry: so return it without |
| 7459 | * attempting to raise page count. |
| 7460 | */ |
Filipe Manana | 6fdef6d | 2014-06-05 13:22:26 +0100 | [diff] [blame] | 7461 | page = NULL; |
Alex Gartrell | fc4adbf | 2014-05-20 13:07:56 -0700 | [diff] [blame] | 7462 | break; /* TODO: Is this relevant for this use case? */ |
| 7463 | } |
| 7464 | |
Filipe Manana | 9140515 | 2014-06-05 13:22:24 +0100 | [diff] [blame] | 7465 | if (!page_cache_get_speculative(page)) { |
| 7466 | page = NULL; |
Alex Gartrell | fc4adbf | 2014-05-20 13:07:56 -0700 | [diff] [blame] | 7467 | continue; |
Filipe Manana | 9140515 | 2014-06-05 13:22:24 +0100 | [diff] [blame] | 7468 | } |
Alex Gartrell | fc4adbf | 2014-05-20 13:07:56 -0700 | [diff] [blame] | 7469 | |
| 7470 | /* |
| 7471 | * Has the page moved? |
| 7472 | * This is part of the lockless pagecache protocol. See |
| 7473 | * include/linux/pagemap.h for details. |
| 7474 | */ |
| 7475 | if (unlikely(page != *pagep)) { |
Kirill A. Shutemov | 09cbfea | 2016-04-01 15:29:47 +0300 | [diff] [blame] | 7476 | put_page(page); |
Alex Gartrell | fc4adbf | 2014-05-20 13:07:56 -0700 | [diff] [blame] | 7477 | page = NULL; |
| 7478 | } |
| 7479 | } |
| 7480 | |
| 7481 | if (page) { |
| 7482 | if (page->index <= end_idx) |
| 7483 | found = true; |
Kirill A. Shutemov | 09cbfea | 2016-04-01 15:29:47 +0300 | [diff] [blame] | 7484 | put_page(page); |
Alex Gartrell | fc4adbf | 2014-05-20 13:07:56 -0700 | [diff] [blame] | 7485 | } |
| 7486 | |
| 7487 | rcu_read_unlock(); |
| 7488 | return found; |
| 7489 | } |
| 7490 | |
Josef Bacik | eb838e7 | 2012-07-31 16:28:48 -0400 | [diff] [blame] | 7491 | static int lock_extent_direct(struct inode *inode, u64 lockstart, u64 lockend, |
| 7492 | struct extent_state **cached_state, int writing) |
| 7493 | { |
| 7494 | struct btrfs_ordered_extent *ordered; |
| 7495 | int ret = 0; |
| 7496 | |
| 7497 | while (1) { |
| 7498 | lock_extent_bits(&BTRFS_I(inode)->io_tree, lockstart, lockend, |
David Sterba | ff13db4 | 2015-12-03 14:30:40 +0100 | [diff] [blame] | 7499 | cached_state); |
Josef Bacik | eb838e7 | 2012-07-31 16:28:48 -0400 | [diff] [blame] | 7500 | /* |
| 7501 | * 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] | 7502 | * 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] | 7503 | * extents in this range. |
| 7504 | */ |
| 7505 | ordered = btrfs_lookup_ordered_range(inode, lockstart, |
| 7506 | lockend - lockstart + 1); |
| 7507 | |
| 7508 | /* |
| 7509 | * We need to make sure there are no buffered pages in this |
| 7510 | * range either, we could have raced between the invalidate in |
| 7511 | * generic_file_direct_write and locking the extent. The |
| 7512 | * invalidate needs to happen so that reads after a write do not |
| 7513 | * get stale data. |
| 7514 | */ |
Alex Gartrell | fc4adbf | 2014-05-20 13:07:56 -0700 | [diff] [blame] | 7515 | if (!ordered && |
| 7516 | (!writing || |
| 7517 | !btrfs_page_exists_in_range(inode, lockstart, lockend))) |
Josef Bacik | eb838e7 | 2012-07-31 16:28:48 -0400 | [diff] [blame] | 7518 | break; |
| 7519 | |
| 7520 | unlock_extent_cached(&BTRFS_I(inode)->io_tree, lockstart, lockend, |
| 7521 | cached_state, GFP_NOFS); |
| 7522 | |
| 7523 | if (ordered) { |
Filipe Manana | ade7702 | 2016-02-18 14:28:55 +0000 | [diff] [blame] | 7524 | /* |
| 7525 | * If we are doing a DIO read and the ordered extent we |
| 7526 | * found is for a buffered write, we can not wait for it |
| 7527 | * to complete and retry, because if we do so we can |
| 7528 | * deadlock with concurrent buffered writes on page |
| 7529 | * locks. This happens only if our DIO read covers more |
| 7530 | * than one extent map, if at this point has already |
| 7531 | * created an ordered extent for a previous extent map |
| 7532 | * and locked its range in the inode's io tree, and a |
| 7533 | * concurrent write against that previous extent map's |
| 7534 | * range and this range started (we unlock the ranges |
| 7535 | * in the io tree only when the bios complete and |
| 7536 | * buffered writes always lock pages before attempting |
| 7537 | * to lock range in the io tree). |
| 7538 | */ |
| 7539 | if (writing || |
| 7540 | test_bit(BTRFS_ORDERED_DIRECT, &ordered->flags)) |
| 7541 | btrfs_start_ordered_extent(inode, ordered, 1); |
| 7542 | else |
| 7543 | ret = -ENOTBLK; |
Josef Bacik | eb838e7 | 2012-07-31 16:28:48 -0400 | [diff] [blame] | 7544 | btrfs_put_ordered_extent(ordered); |
| 7545 | } else { |
Josef Bacik | eb838e7 | 2012-07-31 16:28:48 -0400 | [diff] [blame] | 7546 | /* |
Filipe Manana | b850ae1 | 2015-12-08 16:23:16 +0000 | [diff] [blame] | 7547 | * We could trigger writeback for this range (and wait |
| 7548 | * for it to complete) and then invalidate the pages for |
| 7549 | * this range (through invalidate_inode_pages2_range()), |
| 7550 | * but that can lead us to a deadlock with a concurrent |
| 7551 | * call to readpages() (a buffered read or a defrag call |
| 7552 | * triggered a readahead) on a page lock due to an |
| 7553 | * ordered dio extent we created before but did not have |
| 7554 | * yet a corresponding bio submitted (whence it can not |
| 7555 | * complete), which makes readpages() wait for that |
| 7556 | * ordered extent to complete while holding a lock on |
| 7557 | * that page. |
Josef Bacik | eb838e7 | 2012-07-31 16:28:48 -0400 | [diff] [blame] | 7558 | */ |
Filipe Manana | b850ae1 | 2015-12-08 16:23:16 +0000 | [diff] [blame] | 7559 | ret = -ENOTBLK; |
Josef Bacik | eb838e7 | 2012-07-31 16:28:48 -0400 | [diff] [blame] | 7560 | } |
| 7561 | |
Filipe Manana | ade7702 | 2016-02-18 14:28:55 +0000 | [diff] [blame] | 7562 | if (ret) |
| 7563 | break; |
| 7564 | |
Josef Bacik | eb838e7 | 2012-07-31 16:28:48 -0400 | [diff] [blame] | 7565 | cond_resched(); |
| 7566 | } |
| 7567 | |
| 7568 | return ret; |
| 7569 | } |
| 7570 | |
Josef Bacik | 69ffb54 | 2012-09-11 15:40:07 -0400 | [diff] [blame] | 7571 | static struct extent_map *create_pinned_em(struct inode *inode, u64 start, |
| 7572 | u64 len, u64 orig_start, |
| 7573 | u64 block_start, u64 block_len, |
Josef Bacik | cc95bef | 2013-04-04 14:31:27 -0400 | [diff] [blame] | 7574 | u64 orig_block_len, u64 ram_bytes, |
| 7575 | int type) |
Josef Bacik | 69ffb54 | 2012-09-11 15:40:07 -0400 | [diff] [blame] | 7576 | { |
| 7577 | struct extent_map_tree *em_tree; |
| 7578 | struct extent_map *em; |
| 7579 | struct btrfs_root *root = BTRFS_I(inode)->root; |
| 7580 | int ret; |
| 7581 | |
| 7582 | em_tree = &BTRFS_I(inode)->extent_tree; |
| 7583 | em = alloc_extent_map(); |
| 7584 | if (!em) |
| 7585 | return ERR_PTR(-ENOMEM); |
| 7586 | |
| 7587 | em->start = start; |
| 7588 | em->orig_start = orig_start; |
Josef Bacik | 2ab28f3 | 2012-10-12 15:27:49 -0400 | [diff] [blame] | 7589 | em->mod_start = start; |
| 7590 | em->mod_len = len; |
Josef Bacik | 69ffb54 | 2012-09-11 15:40:07 -0400 | [diff] [blame] | 7591 | em->len = len; |
| 7592 | em->block_len = block_len; |
| 7593 | em->block_start = block_start; |
| 7594 | em->bdev = root->fs_info->fs_devices->latest_bdev; |
Josef Bacik | b493968 | 2012-12-03 10:31:19 -0500 | [diff] [blame] | 7595 | em->orig_block_len = orig_block_len; |
Josef Bacik | cc95bef | 2013-04-04 14:31:27 -0400 | [diff] [blame] | 7596 | em->ram_bytes = ram_bytes; |
Josef Bacik | 70c8a91 | 2012-10-11 16:54:30 -0400 | [diff] [blame] | 7597 | em->generation = -1; |
Josef Bacik | 69ffb54 | 2012-09-11 15:40:07 -0400 | [diff] [blame] | 7598 | set_bit(EXTENT_FLAG_PINNED, &em->flags); |
| 7599 | if (type == BTRFS_ORDERED_PREALLOC) |
Josef Bacik | b11e234 | 2012-12-03 10:58:15 -0500 | [diff] [blame] | 7600 | set_bit(EXTENT_FLAG_FILLING, &em->flags); |
Josef Bacik | 69ffb54 | 2012-09-11 15:40:07 -0400 | [diff] [blame] | 7601 | |
| 7602 | do { |
| 7603 | btrfs_drop_extent_cache(inode, em->start, |
| 7604 | em->start + em->len - 1, 0); |
| 7605 | write_lock(&em_tree->lock); |
Josef Bacik | 09a2a8f9 | 2013-04-05 16:51:15 -0400 | [diff] [blame] | 7606 | ret = add_extent_mapping(em_tree, em, 1); |
Josef Bacik | 69ffb54 | 2012-09-11 15:40:07 -0400 | [diff] [blame] | 7607 | write_unlock(&em_tree->lock); |
| 7608 | } while (ret == -EEXIST); |
| 7609 | |
| 7610 | if (ret) { |
| 7611 | free_extent_map(em); |
| 7612 | return ERR_PTR(ret); |
| 7613 | } |
| 7614 | |
| 7615 | return em; |
| 7616 | } |
| 7617 | |
Filipe Manana | 9c9464c | 2015-11-04 09:52:04 +0000 | [diff] [blame] | 7618 | static void adjust_dio_outstanding_extents(struct inode *inode, |
| 7619 | struct btrfs_dio_data *dio_data, |
| 7620 | const u64 len) |
| 7621 | { |
| 7622 | unsigned num_extents; |
| 7623 | |
| 7624 | num_extents = (unsigned) div64_u64(len + BTRFS_MAX_EXTENT_SIZE - 1, |
| 7625 | BTRFS_MAX_EXTENT_SIZE); |
| 7626 | /* |
| 7627 | * If we have an outstanding_extents count still set then we're |
| 7628 | * within our reservation, otherwise we need to adjust our inode |
| 7629 | * counter appropriately. |
| 7630 | */ |
| 7631 | if (dio_data->outstanding_extents) { |
| 7632 | dio_data->outstanding_extents -= num_extents; |
| 7633 | } else { |
| 7634 | spin_lock(&BTRFS_I(inode)->lock); |
| 7635 | BTRFS_I(inode)->outstanding_extents += num_extents; |
| 7636 | spin_unlock(&BTRFS_I(inode)->lock); |
| 7637 | } |
| 7638 | } |
| 7639 | |
Josef Bacik | 4b46fce | 2010-05-23 11:00:55 -0400 | [diff] [blame] | 7640 | static int btrfs_get_blocks_direct(struct inode *inode, sector_t iblock, |
| 7641 | struct buffer_head *bh_result, int create) |
| 7642 | { |
| 7643 | struct extent_map *em; |
| 7644 | struct btrfs_root *root = BTRFS_I(inode)->root; |
Josef Bacik | eb838e7 | 2012-07-31 16:28:48 -0400 | [diff] [blame] | 7645 | struct extent_state *cached_state = NULL; |
chandan | 50745b0 | 2015-08-28 21:10:13 +0530 | [diff] [blame] | 7646 | struct btrfs_dio_data *dio_data = NULL; |
Josef Bacik | 4b46fce | 2010-05-23 11:00:55 -0400 | [diff] [blame] | 7647 | u64 start = iblock << inode->i_blkbits; |
Josef Bacik | eb838e7 | 2012-07-31 16:28:48 -0400 | [diff] [blame] | 7648 | u64 lockstart, lockend; |
Josef Bacik | 4b46fce | 2010-05-23 11:00:55 -0400 | [diff] [blame] | 7649 | u64 len = bh_result->b_size; |
Josef Bacik | eb838e7 | 2012-07-31 16:28:48 -0400 | [diff] [blame] | 7650 | int unlock_bits = EXTENT_LOCKED; |
Miao Xie | 0934856 | 2013-02-07 10:12:07 +0000 | [diff] [blame] | 7651 | int ret = 0; |
Josef Bacik | eb838e7 | 2012-07-31 16:28:48 -0400 | [diff] [blame] | 7652 | |
Miao Xie | 172a504 | 2013-02-21 02:48:22 -0700 | [diff] [blame] | 7653 | if (create) |
Josef Bacik | 3266789 | 2015-02-11 15:08:58 -0500 | [diff] [blame] | 7654 | unlock_bits |= EXTENT_DIRTY; |
Miao Xie | 172a504 | 2013-02-21 02:48:22 -0700 | [diff] [blame] | 7655 | else |
Josef Bacik | c329861 | 2012-08-03 16:49:19 -0400 | [diff] [blame] | 7656 | len = min_t(u64, len, root->sectorsize); |
Josef Bacik | eb838e7 | 2012-07-31 16:28:48 -0400 | [diff] [blame] | 7657 | |
Josef Bacik | c329861 | 2012-08-03 16:49:19 -0400 | [diff] [blame] | 7658 | lockstart = start; |
| 7659 | lockend = start + len - 1; |
| 7660 | |
Josef Bacik | e1cbbfa | 2015-03-17 10:52:28 -0400 | [diff] [blame] | 7661 | if (current->journal_info) { |
| 7662 | /* |
| 7663 | * 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] | 7664 | * 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] | 7665 | * confused. |
| 7666 | */ |
chandan | 50745b0 | 2015-08-28 21:10:13 +0530 | [diff] [blame] | 7667 | dio_data = current->journal_info; |
Josef Bacik | e1cbbfa | 2015-03-17 10:52:28 -0400 | [diff] [blame] | 7668 | current->journal_info = NULL; |
| 7669 | } |
| 7670 | |
Josef Bacik | eb838e7 | 2012-07-31 16:28:48 -0400 | [diff] [blame] | 7671 | /* |
| 7672 | * If this errors out it's because we couldn't invalidate pagecache for |
| 7673 | * this range and we need to fallback to buffered. |
| 7674 | */ |
Filipe Manana | 9c9464c | 2015-11-04 09:52:04 +0000 | [diff] [blame] | 7675 | if (lock_extent_direct(inode, lockstart, lockend, &cached_state, |
| 7676 | create)) { |
| 7677 | ret = -ENOTBLK; |
| 7678 | goto err; |
| 7679 | } |
Josef Bacik | eb838e7 | 2012-07-31 16:28:48 -0400 | [diff] [blame] | 7680 | |
Josef Bacik | 4b46fce | 2010-05-23 11:00:55 -0400 | [diff] [blame] | 7681 | em = btrfs_get_extent(inode, NULL, 0, start, len, 0); |
Josef Bacik | eb838e7 | 2012-07-31 16:28:48 -0400 | [diff] [blame] | 7682 | if (IS_ERR(em)) { |
| 7683 | ret = PTR_ERR(em); |
| 7684 | goto unlock_err; |
| 7685 | } |
Josef Bacik | 4b46fce | 2010-05-23 11:00:55 -0400 | [diff] [blame] | 7686 | |
| 7687 | /* |
| 7688 | * Ok for INLINE and COMPRESSED extents we need to fallback on buffered |
| 7689 | * io. INLINE is special, and we could probably kludge it in here, but |
| 7690 | * it's still buffered so for safety lets just fall back to the generic |
| 7691 | * buffered path. |
| 7692 | * |
| 7693 | * For COMPRESSED we _have_ to read the entire extent in so we can |
| 7694 | * decompress it, so there will be buffering required no matter what we |
| 7695 | * do, so go ahead and fallback to buffered. |
| 7696 | * |
Nicholas D Steeves | 0132761 | 2016-05-19 21:18:45 -0400 | [diff] [blame] | 7697 | * 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] | 7698 | * to buffered IO. Don't blame me, this is the price we pay for using |
| 7699 | * the generic code. |
| 7700 | */ |
| 7701 | if (test_bit(EXTENT_FLAG_COMPRESSED, &em->flags) || |
| 7702 | em->block_start == EXTENT_MAP_INLINE) { |
| 7703 | free_extent_map(em); |
Josef Bacik | eb838e7 | 2012-07-31 16:28:48 -0400 | [diff] [blame] | 7704 | ret = -ENOTBLK; |
| 7705 | goto unlock_err; |
Josef Bacik | 4b46fce | 2010-05-23 11:00:55 -0400 | [diff] [blame] | 7706 | } |
| 7707 | |
| 7708 | /* Just a good old fashioned hole, return */ |
| 7709 | if (!create && (em->block_start == EXTENT_MAP_HOLE || |
| 7710 | test_bit(EXTENT_FLAG_PREALLOC, &em->flags))) { |
| 7711 | free_extent_map(em); |
Josef Bacik | eb838e7 | 2012-07-31 16:28:48 -0400 | [diff] [blame] | 7712 | goto unlock_err; |
Josef Bacik | 4b46fce | 2010-05-23 11:00:55 -0400 | [diff] [blame] | 7713 | } |
| 7714 | |
| 7715 | /* |
| 7716 | * We don't allocate a new extent in the following cases |
| 7717 | * |
| 7718 | * 1) The inode is marked as NODATACOW. In this case we'll just use the |
| 7719 | * existing extent. |
| 7720 | * 2) The extent is marked as PREALLOC. We're good to go here and can |
| 7721 | * just use the extent. |
| 7722 | * |
| 7723 | */ |
Chris Mason | 46bfbb5 | 2010-05-26 11:04:10 -0400 | [diff] [blame] | 7724 | if (!create) { |
Josef Bacik | eb838e7 | 2012-07-31 16:28:48 -0400 | [diff] [blame] | 7725 | len = min(len, em->len - (start - em->start)); |
| 7726 | lockstart = start + len; |
| 7727 | goto unlock; |
Chris Mason | 46bfbb5 | 2010-05-26 11:04:10 -0400 | [diff] [blame] | 7728 | } |
Josef Bacik | 4b46fce | 2010-05-23 11:00:55 -0400 | [diff] [blame] | 7729 | |
| 7730 | if (test_bit(EXTENT_FLAG_PREALLOC, &em->flags) || |
| 7731 | ((BTRFS_I(inode)->flags & BTRFS_INODE_NODATACOW) && |
| 7732 | em->block_start != EXTENT_MAP_HOLE)) { |
Josef Bacik | 4b46fce | 2010-05-23 11:00:55 -0400 | [diff] [blame] | 7733 | int type; |
Josef Bacik | eb384b5 | 2013-04-24 16:32:55 -0400 | [diff] [blame] | 7734 | u64 block_start, orig_start, orig_block_len, ram_bytes; |
Josef Bacik | 4b46fce | 2010-05-23 11:00:55 -0400 | [diff] [blame] | 7735 | |
| 7736 | if (test_bit(EXTENT_FLAG_PREALLOC, &em->flags)) |
| 7737 | type = BTRFS_ORDERED_PREALLOC; |
| 7738 | else |
| 7739 | type = BTRFS_ORDERED_NOCOW; |
Chris Mason | 46bfbb5 | 2010-05-26 11:04:10 -0400 | [diff] [blame] | 7740 | len = min(len, em->len - (start - em->start)); |
Josef Bacik | 4b46fce | 2010-05-23 11:00:55 -0400 | [diff] [blame] | 7741 | block_start = em->block_start + (start - em->start); |
Chris Mason | 46bfbb5 | 2010-05-26 11:04:10 -0400 | [diff] [blame] | 7742 | |
Josef Bacik | 0036158 | 2013-08-14 14:02:47 -0400 | [diff] [blame] | 7743 | if (can_nocow_extent(inode, start, &len, &orig_start, |
Filipe Manana | f78c436 | 2016-05-09 13:15:41 +0100 | [diff] [blame] | 7744 | &orig_block_len, &ram_bytes) == 1 && |
| 7745 | btrfs_inc_nocow_writers(root->fs_info, block_start)) { |
Filipe Manana | 5f9a8a5 | 2016-05-12 13:53:36 +0100 | [diff] [blame] | 7746 | struct extent_map *em2; |
Filipe Manana | 0b90191 | 2016-05-09 13:15:27 +0100 | [diff] [blame] | 7747 | |
Filipe Manana | 5f9a8a5 | 2016-05-12 13:53:36 +0100 | [diff] [blame] | 7748 | em2 = btrfs_create_dio_extent(inode, start, len, |
| 7749 | orig_start, block_start, |
| 7750 | len, orig_block_len, |
| 7751 | ram_bytes, type); |
Filipe Manana | f78c436 | 2016-05-09 13:15:41 +0100 | [diff] [blame] | 7752 | btrfs_dec_nocow_writers(root->fs_info, block_start); |
Josef Bacik | 69ffb54 | 2012-09-11 15:40:07 -0400 | [diff] [blame] | 7753 | if (type == BTRFS_ORDERED_PREALLOC) { |
| 7754 | free_extent_map(em); |
Filipe Manana | 5f9a8a5 | 2016-05-12 13:53:36 +0100 | [diff] [blame] | 7755 | em = em2; |
Josef Bacik | 69ffb54 | 2012-09-11 15:40:07 -0400 | [diff] [blame] | 7756 | } |
Filipe Manana | 5f9a8a5 | 2016-05-12 13:53:36 +0100 | [diff] [blame] | 7757 | if (em2 && IS_ERR(em2)) { |
| 7758 | ret = PTR_ERR(em2); |
Josef Bacik | eb838e7 | 2012-07-31 16:28:48 -0400 | [diff] [blame] | 7759 | goto unlock_err; |
Chris Mason | 46bfbb5 | 2010-05-26 11:04:10 -0400 | [diff] [blame] | 7760 | } |
Wang Xiaoguang | 1851309 | 2016-07-25 15:51:40 +0800 | [diff] [blame] | 7761 | /* |
| 7762 | * For inode marked NODATACOW or extent marked PREALLOC, |
| 7763 | * use the existing or preallocated extent, so does not |
| 7764 | * need to adjust btrfs_space_info's bytes_may_use. |
| 7765 | */ |
| 7766 | btrfs_free_reserved_data_space_noquota(inode, |
| 7767 | start, len); |
Chris Mason | 46bfbb5 | 2010-05-26 11:04:10 -0400 | [diff] [blame] | 7768 | goto unlock; |
Josef Bacik | 4b46fce | 2010-05-23 11:00:55 -0400 | [diff] [blame] | 7769 | } |
Josef Bacik | 4b46fce | 2010-05-23 11:00:55 -0400 | [diff] [blame] | 7770 | } |
Josef Bacik | 0036158 | 2013-08-14 14:02:47 -0400 | [diff] [blame] | 7771 | |
Chris Mason | 46bfbb5 | 2010-05-26 11:04:10 -0400 | [diff] [blame] | 7772 | /* |
| 7773 | * this will cow the extent, reset the len in case we changed |
| 7774 | * it above |
| 7775 | */ |
| 7776 | len = bh_result->b_size; |
Josef Bacik | 70c8a91 | 2012-10-11 16:54:30 -0400 | [diff] [blame] | 7777 | free_extent_map(em); |
| 7778 | em = btrfs_new_extent_direct(inode, start, len); |
Josef Bacik | eb838e7 | 2012-07-31 16:28:48 -0400 | [diff] [blame] | 7779 | if (IS_ERR(em)) { |
| 7780 | ret = PTR_ERR(em); |
| 7781 | goto unlock_err; |
| 7782 | } |
Chris Mason | 46bfbb5 | 2010-05-26 11:04:10 -0400 | [diff] [blame] | 7783 | len = min(len, em->len - (start - em->start)); |
| 7784 | unlock: |
Josef Bacik | 4b46fce | 2010-05-23 11:00:55 -0400 | [diff] [blame] | 7785 | bh_result->b_blocknr = (em->block_start + (start - em->start)) >> |
| 7786 | inode->i_blkbits; |
Chris Mason | 46bfbb5 | 2010-05-26 11:04:10 -0400 | [diff] [blame] | 7787 | bh_result->b_size = len; |
Josef Bacik | 4b46fce | 2010-05-23 11:00:55 -0400 | [diff] [blame] | 7788 | bh_result->b_bdev = em->bdev; |
| 7789 | set_buffer_mapped(bh_result); |
Josef Bacik | c3473e8 | 2012-06-19 10:59:00 -0400 | [diff] [blame] | 7790 | if (create) { |
| 7791 | if (!test_bit(EXTENT_FLAG_PREALLOC, &em->flags)) |
| 7792 | set_buffer_new(bh_result); |
| 7793 | |
| 7794 | /* |
| 7795 | * Need to update the i_size under the extent lock so buffered |
| 7796 | * readers will get the updated i_size when we unlock. |
| 7797 | */ |
| 7798 | if (start + len > i_size_read(inode)) |
| 7799 | i_size_write(inode, start + len); |
Miao Xie | 0934856 | 2013-02-07 10:12:07 +0000 | [diff] [blame] | 7800 | |
Filipe Manana | 9c9464c | 2015-11-04 09:52:04 +0000 | [diff] [blame] | 7801 | adjust_dio_outstanding_extents(inode, dio_data, len); |
chandan | 50745b0 | 2015-08-28 21:10:13 +0530 | [diff] [blame] | 7802 | WARN_ON(dio_data->reserve < len); |
| 7803 | dio_data->reserve -= len; |
Filipe Manana | f28a492 | 2015-12-08 19:23:20 +0000 | [diff] [blame] | 7804 | dio_data->unsubmitted_oe_range_end = start + len; |
chandan | 50745b0 | 2015-08-28 21:10:13 +0530 | [diff] [blame] | 7805 | current->journal_info = dio_data; |
Josef Bacik | c3473e8 | 2012-06-19 10:59:00 -0400 | [diff] [blame] | 7806 | } |
Josef Bacik | 4b46fce | 2010-05-23 11:00:55 -0400 | [diff] [blame] | 7807 | |
Josef Bacik | eb838e7 | 2012-07-31 16:28:48 -0400 | [diff] [blame] | 7808 | /* |
| 7809 | * In the case of write we need to clear and unlock the entire range, |
| 7810 | * in the case of read we need to unlock only the end area that we |
| 7811 | * aren't using if there is any left over space. |
| 7812 | */ |
Liu Bo | 24c03fa | 2012-08-22 20:10:38 -0600 | [diff] [blame] | 7813 | if (lockstart < lockend) { |
Miao Xie | 0934856 | 2013-02-07 10:12:07 +0000 | [diff] [blame] | 7814 | clear_extent_bit(&BTRFS_I(inode)->io_tree, lockstart, |
| 7815 | lockend, unlock_bits, 1, 0, |
| 7816 | &cached_state, GFP_NOFS); |
Liu Bo | 24c03fa | 2012-08-22 20:10:38 -0600 | [diff] [blame] | 7817 | } else { |
Josef Bacik | eb838e7 | 2012-07-31 16:28:48 -0400 | [diff] [blame] | 7818 | free_extent_state(cached_state); |
Liu Bo | 24c03fa | 2012-08-22 20:10:38 -0600 | [diff] [blame] | 7819 | } |
Josef Bacik | eb838e7 | 2012-07-31 16:28:48 -0400 | [diff] [blame] | 7820 | |
Josef Bacik | 4b46fce | 2010-05-23 11:00:55 -0400 | [diff] [blame] | 7821 | free_extent_map(em); |
| 7822 | |
| 7823 | return 0; |
Josef Bacik | eb838e7 | 2012-07-31 16:28:48 -0400 | [diff] [blame] | 7824 | |
| 7825 | unlock_err: |
Josef Bacik | eb838e7 | 2012-07-31 16:28:48 -0400 | [diff] [blame] | 7826 | clear_extent_bit(&BTRFS_I(inode)->io_tree, lockstart, lockend, |
| 7827 | unlock_bits, 1, 0, &cached_state, GFP_NOFS); |
Filipe Manana | 9c9464c | 2015-11-04 09:52:04 +0000 | [diff] [blame] | 7828 | err: |
chandan | 50745b0 | 2015-08-28 21:10:13 +0530 | [diff] [blame] | 7829 | if (dio_data) |
| 7830 | current->journal_info = dio_data; |
Filipe Manana | 9c9464c | 2015-11-04 09:52:04 +0000 | [diff] [blame] | 7831 | /* |
| 7832 | * Compensate the delalloc release we do in btrfs_direct_IO() when we |
| 7833 | * write less data then expected, so that we don't underflow our inode's |
| 7834 | * outstanding extents counter. |
| 7835 | */ |
| 7836 | if (create && dio_data) |
| 7837 | adjust_dio_outstanding_extents(inode, dio_data, len); |
| 7838 | |
Josef Bacik | eb838e7 | 2012-07-31 16:28:48 -0400 | [diff] [blame] | 7839 | return ret; |
Josef Bacik | 4b46fce | 2010-05-23 11:00:55 -0400 | [diff] [blame] | 7840 | } |
| 7841 | |
Miao Xie | 8b110e3 | 2014-09-12 18:44:03 +0800 | [diff] [blame] | 7842 | static inline int submit_dio_repair_bio(struct inode *inode, struct bio *bio, |
Mike Christie | 81a75f6 | 2016-06-05 14:31:54 -0500 | [diff] [blame] | 7843 | int mirror_num) |
Miao Xie | 8b110e3 | 2014-09-12 18:44:03 +0800 | [diff] [blame] | 7844 | { |
| 7845 | struct btrfs_root *root = BTRFS_I(inode)->root; |
| 7846 | int ret; |
| 7847 | |
Mike Christie | 37226b2 | 2016-06-05 14:31:52 -0500 | [diff] [blame] | 7848 | BUG_ON(bio_op(bio) == REQ_OP_WRITE); |
Miao Xie | 8b110e3 | 2014-09-12 18:44:03 +0800 | [diff] [blame] | 7849 | |
| 7850 | bio_get(bio); |
| 7851 | |
| 7852 | ret = btrfs_bio_wq_end_io(root->fs_info, bio, |
| 7853 | BTRFS_WQ_ENDIO_DIO_REPAIR); |
| 7854 | if (ret) |
| 7855 | goto err; |
| 7856 | |
Mike Christie | 81a75f6 | 2016-06-05 14:31:54 -0500 | [diff] [blame] | 7857 | ret = btrfs_map_bio(root, bio, mirror_num, 0); |
Miao Xie | 8b110e3 | 2014-09-12 18:44:03 +0800 | [diff] [blame] | 7858 | err: |
| 7859 | bio_put(bio); |
| 7860 | return ret; |
| 7861 | } |
| 7862 | |
| 7863 | static int btrfs_check_dio_repairable(struct inode *inode, |
| 7864 | struct bio *failed_bio, |
| 7865 | struct io_failure_record *failrec, |
| 7866 | int failed_mirror) |
| 7867 | { |
| 7868 | int num_copies; |
| 7869 | |
| 7870 | num_copies = btrfs_num_copies(BTRFS_I(inode)->root->fs_info, |
| 7871 | failrec->logical, failrec->len); |
| 7872 | if (num_copies == 1) { |
| 7873 | /* |
| 7874 | * we only have a single copy of the data, so don't bother with |
| 7875 | * all the retry and error correction code that follows. no |
| 7876 | * matter what the error is, it is very likely to persist. |
| 7877 | */ |
| 7878 | pr_debug("Check DIO Repairable: cannot repair, num_copies=%d, next_mirror %d, failed_mirror %d\n", |
| 7879 | num_copies, failrec->this_mirror, failed_mirror); |
| 7880 | return 0; |
| 7881 | } |
| 7882 | |
| 7883 | failrec->failed_mirror = failed_mirror; |
| 7884 | failrec->this_mirror++; |
| 7885 | if (failrec->this_mirror == failed_mirror) |
| 7886 | failrec->this_mirror++; |
| 7887 | |
| 7888 | if (failrec->this_mirror > num_copies) { |
| 7889 | pr_debug("Check DIO Repairable: (fail) num_copies=%d, next_mirror %d, failed_mirror %d\n", |
| 7890 | num_copies, failrec->this_mirror, failed_mirror); |
| 7891 | return 0; |
| 7892 | } |
| 7893 | |
| 7894 | return 1; |
| 7895 | } |
| 7896 | |
| 7897 | static int dio_read_error(struct inode *inode, struct bio *failed_bio, |
Chandan Rajendra | 2dabb32 | 2016-01-21 15:55:55 +0530 | [diff] [blame] | 7898 | struct page *page, unsigned int pgoff, |
| 7899 | u64 start, u64 end, int failed_mirror, |
| 7900 | bio_end_io_t *repair_endio, void *repair_arg) |
Miao Xie | 8b110e3 | 2014-09-12 18:44:03 +0800 | [diff] [blame] | 7901 | { |
| 7902 | struct io_failure_record *failrec; |
| 7903 | struct bio *bio; |
| 7904 | int isector; |
| 7905 | int read_mode; |
| 7906 | int ret; |
| 7907 | |
Mike Christie | 37226b2 | 2016-06-05 14:31:52 -0500 | [diff] [blame] | 7908 | BUG_ON(bio_op(failed_bio) == REQ_OP_WRITE); |
Miao Xie | 8b110e3 | 2014-09-12 18:44:03 +0800 | [diff] [blame] | 7909 | |
| 7910 | ret = btrfs_get_io_failure_record(inode, start, end, &failrec); |
| 7911 | if (ret) |
| 7912 | return ret; |
| 7913 | |
| 7914 | ret = btrfs_check_dio_repairable(inode, failed_bio, failrec, |
| 7915 | failed_mirror); |
| 7916 | if (!ret) { |
| 7917 | free_io_failure(inode, failrec); |
| 7918 | return -EIO; |
| 7919 | } |
| 7920 | |
Chandan Rajendra | 2dabb32 | 2016-01-21 15:55:55 +0530 | [diff] [blame] | 7921 | if ((failed_bio->bi_vcnt > 1) |
| 7922 | || (failed_bio->bi_io_vec->bv_len |
| 7923 | > BTRFS_I(inode)->root->sectorsize)) |
Miao Xie | 8b110e3 | 2014-09-12 18:44:03 +0800 | [diff] [blame] | 7924 | read_mode = READ_SYNC | REQ_FAILFAST_DEV; |
| 7925 | else |
| 7926 | read_mode = READ_SYNC; |
| 7927 | |
| 7928 | isector = start - btrfs_io_bio(failed_bio)->logical; |
| 7929 | isector >>= inode->i_sb->s_blocksize_bits; |
| 7930 | bio = btrfs_create_repair_bio(inode, failed_bio, failrec, page, |
Chandan Rajendra | 2dabb32 | 2016-01-21 15:55:55 +0530 | [diff] [blame] | 7931 | pgoff, isector, repair_endio, repair_arg); |
Miao Xie | 8b110e3 | 2014-09-12 18:44:03 +0800 | [diff] [blame] | 7932 | if (!bio) { |
| 7933 | free_io_failure(inode, failrec); |
| 7934 | return -EIO; |
| 7935 | } |
Mike Christie | 37226b2 | 2016-06-05 14:31:52 -0500 | [diff] [blame] | 7936 | bio_set_op_attrs(bio, REQ_OP_READ, read_mode); |
Miao Xie | 8b110e3 | 2014-09-12 18:44:03 +0800 | [diff] [blame] | 7937 | |
| 7938 | btrfs_debug(BTRFS_I(inode)->root->fs_info, |
| 7939 | "Repair DIO Read Error: submitting new dio read[%#x] to this_mirror=%d, in_validation=%d\n", |
| 7940 | read_mode, failrec->this_mirror, failrec->in_validation); |
| 7941 | |
Mike Christie | 81a75f6 | 2016-06-05 14:31:54 -0500 | [diff] [blame] | 7942 | ret = submit_dio_repair_bio(inode, bio, failrec->this_mirror); |
Miao Xie | 8b110e3 | 2014-09-12 18:44:03 +0800 | [diff] [blame] | 7943 | if (ret) { |
| 7944 | free_io_failure(inode, failrec); |
| 7945 | bio_put(bio); |
| 7946 | } |
| 7947 | |
| 7948 | return ret; |
| 7949 | } |
| 7950 | |
| 7951 | struct btrfs_retry_complete { |
| 7952 | struct completion done; |
| 7953 | struct inode *inode; |
| 7954 | u64 start; |
| 7955 | int uptodate; |
| 7956 | }; |
| 7957 | |
Christoph Hellwig | 4246a0b | 2015-07-20 15:29:37 +0200 | [diff] [blame] | 7958 | static void btrfs_retry_endio_nocsum(struct bio *bio) |
Miao Xie | 8b110e3 | 2014-09-12 18:44:03 +0800 | [diff] [blame] | 7959 | { |
| 7960 | struct btrfs_retry_complete *done = bio->bi_private; |
Chandan Rajendra | 2dabb32 | 2016-01-21 15:55:55 +0530 | [diff] [blame] | 7961 | struct inode *inode; |
Miao Xie | 8b110e3 | 2014-09-12 18:44:03 +0800 | [diff] [blame] | 7962 | struct bio_vec *bvec; |
| 7963 | int i; |
| 7964 | |
Christoph Hellwig | 4246a0b | 2015-07-20 15:29:37 +0200 | [diff] [blame] | 7965 | if (bio->bi_error) |
Miao Xie | 8b110e3 | 2014-09-12 18:44:03 +0800 | [diff] [blame] | 7966 | goto end; |
| 7967 | |
Chandan Rajendra | 2dabb32 | 2016-01-21 15:55:55 +0530 | [diff] [blame] | 7968 | ASSERT(bio->bi_vcnt == 1); |
| 7969 | inode = bio->bi_io_vec->bv_page->mapping->host; |
| 7970 | ASSERT(bio->bi_io_vec->bv_len == BTRFS_I(inode)->root->sectorsize); |
| 7971 | |
Miao Xie | 8b110e3 | 2014-09-12 18:44:03 +0800 | [diff] [blame] | 7972 | done->uptodate = 1; |
| 7973 | bio_for_each_segment_all(bvec, bio, i) |
| 7974 | clean_io_failure(done->inode, done->start, bvec->bv_page, 0); |
| 7975 | end: |
| 7976 | complete(&done->done); |
| 7977 | bio_put(bio); |
| 7978 | } |
| 7979 | |
| 7980 | static int __btrfs_correct_data_nocsum(struct inode *inode, |
| 7981 | struct btrfs_io_bio *io_bio) |
Josef Bacik | 4b46fce | 2010-05-23 11:00:55 -0400 | [diff] [blame] | 7982 | { |
Chandan Rajendra | 2dabb32 | 2016-01-21 15:55:55 +0530 | [diff] [blame] | 7983 | struct btrfs_fs_info *fs_info; |
Kent Overstreet | 2c30c71 | 2013-11-07 12:20:26 -0800 | [diff] [blame] | 7984 | struct bio_vec *bvec; |
Miao Xie | 8b110e3 | 2014-09-12 18:44:03 +0800 | [diff] [blame] | 7985 | struct btrfs_retry_complete done; |
Josef Bacik | 4b46fce | 2010-05-23 11:00:55 -0400 | [diff] [blame] | 7986 | u64 start; |
Chandan Rajendra | 2dabb32 | 2016-01-21 15:55:55 +0530 | [diff] [blame] | 7987 | unsigned int pgoff; |
| 7988 | u32 sectorsize; |
| 7989 | int nr_sectors; |
Kent Overstreet | 2c30c71 | 2013-11-07 12:20:26 -0800 | [diff] [blame] | 7990 | int i; |
Miao Xie | c1dc089 | 2014-09-12 18:43:56 +0800 | [diff] [blame] | 7991 | int ret; |
Miao Xie | dc380ae | 2014-09-12 18:43:55 +0800 | [diff] [blame] | 7992 | |
Chandan Rajendra | 2dabb32 | 2016-01-21 15:55:55 +0530 | [diff] [blame] | 7993 | fs_info = BTRFS_I(inode)->root->fs_info; |
| 7994 | sectorsize = BTRFS_I(inode)->root->sectorsize; |
| 7995 | |
Miao Xie | c1dc089 | 2014-09-12 18:43:56 +0800 | [diff] [blame] | 7996 | start = io_bio->logical; |
Miao Xie | 8b110e3 | 2014-09-12 18:44:03 +0800 | [diff] [blame] | 7997 | done.inode = inode; |
| 7998 | |
| 7999 | bio_for_each_segment_all(bvec, &io_bio->bio, i) { |
Chandan Rajendra | 2dabb32 | 2016-01-21 15:55:55 +0530 | [diff] [blame] | 8000 | nr_sectors = BTRFS_BYTES_TO_BLKS(fs_info, bvec->bv_len); |
| 8001 | pgoff = bvec->bv_offset; |
| 8002 | |
| 8003 | next_block_or_try_again: |
Miao Xie | 8b110e3 | 2014-09-12 18:44:03 +0800 | [diff] [blame] | 8004 | done.uptodate = 0; |
| 8005 | done.start = start; |
| 8006 | init_completion(&done.done); |
| 8007 | |
Chandan Rajendra | 2dabb32 | 2016-01-21 15:55:55 +0530 | [diff] [blame] | 8008 | ret = dio_read_error(inode, &io_bio->bio, bvec->bv_page, |
| 8009 | pgoff, start, start + sectorsize - 1, |
| 8010 | io_bio->mirror_num, |
| 8011 | btrfs_retry_endio_nocsum, &done); |
Miao Xie | 8b110e3 | 2014-09-12 18:44:03 +0800 | [diff] [blame] | 8012 | if (ret) |
| 8013 | return ret; |
| 8014 | |
| 8015 | wait_for_completion(&done.done); |
| 8016 | |
| 8017 | if (!done.uptodate) { |
| 8018 | /* We might have another mirror, so try again */ |
Chandan Rajendra | 2dabb32 | 2016-01-21 15:55:55 +0530 | [diff] [blame] | 8019 | goto next_block_or_try_again; |
Miao Xie | 8b110e3 | 2014-09-12 18:44:03 +0800 | [diff] [blame] | 8020 | } |
| 8021 | |
Chandan Rajendra | 2dabb32 | 2016-01-21 15:55:55 +0530 | [diff] [blame] | 8022 | start += sectorsize; |
| 8023 | |
| 8024 | if (nr_sectors--) { |
| 8025 | pgoff += sectorsize; |
| 8026 | goto next_block_or_try_again; |
| 8027 | } |
Miao Xie | 8b110e3 | 2014-09-12 18:44:03 +0800 | [diff] [blame] | 8028 | } |
| 8029 | |
| 8030 | return 0; |
| 8031 | } |
| 8032 | |
Christoph Hellwig | 4246a0b | 2015-07-20 15:29:37 +0200 | [diff] [blame] | 8033 | static void btrfs_retry_endio(struct bio *bio) |
Miao Xie | 8b110e3 | 2014-09-12 18:44:03 +0800 | [diff] [blame] | 8034 | { |
| 8035 | struct btrfs_retry_complete *done = bio->bi_private; |
| 8036 | struct btrfs_io_bio *io_bio = btrfs_io_bio(bio); |
Chandan Rajendra | 2dabb32 | 2016-01-21 15:55:55 +0530 | [diff] [blame] | 8037 | struct inode *inode; |
Miao Xie | 8b110e3 | 2014-09-12 18:44:03 +0800 | [diff] [blame] | 8038 | struct bio_vec *bvec; |
Chandan Rajendra | 2dabb32 | 2016-01-21 15:55:55 +0530 | [diff] [blame] | 8039 | u64 start; |
Miao Xie | 8b110e3 | 2014-09-12 18:44:03 +0800 | [diff] [blame] | 8040 | int uptodate; |
| 8041 | int ret; |
| 8042 | int i; |
| 8043 | |
Christoph Hellwig | 4246a0b | 2015-07-20 15:29:37 +0200 | [diff] [blame] | 8044 | if (bio->bi_error) |
Miao Xie | 8b110e3 | 2014-09-12 18:44:03 +0800 | [diff] [blame] | 8045 | goto end; |
| 8046 | |
| 8047 | uptodate = 1; |
Chandan Rajendra | 2dabb32 | 2016-01-21 15:55:55 +0530 | [diff] [blame] | 8048 | |
| 8049 | start = done->start; |
| 8050 | |
| 8051 | ASSERT(bio->bi_vcnt == 1); |
| 8052 | inode = bio->bi_io_vec->bv_page->mapping->host; |
| 8053 | ASSERT(bio->bi_io_vec->bv_len == BTRFS_I(inode)->root->sectorsize); |
| 8054 | |
Miao Xie | 8b110e3 | 2014-09-12 18:44:03 +0800 | [diff] [blame] | 8055 | bio_for_each_segment_all(bvec, bio, i) { |
| 8056 | ret = __readpage_endio_check(done->inode, io_bio, i, |
Chandan Rajendra | 2dabb32 | 2016-01-21 15:55:55 +0530 | [diff] [blame] | 8057 | bvec->bv_page, bvec->bv_offset, |
| 8058 | done->start, bvec->bv_len); |
Miao Xie | 8b110e3 | 2014-09-12 18:44:03 +0800 | [diff] [blame] | 8059 | if (!ret) |
| 8060 | clean_io_failure(done->inode, done->start, |
Chandan Rajendra | 2dabb32 | 2016-01-21 15:55:55 +0530 | [diff] [blame] | 8061 | bvec->bv_page, bvec->bv_offset); |
Miao Xie | 8b110e3 | 2014-09-12 18:44:03 +0800 | [diff] [blame] | 8062 | else |
| 8063 | uptodate = 0; |
| 8064 | } |
| 8065 | |
| 8066 | done->uptodate = uptodate; |
| 8067 | end: |
| 8068 | complete(&done->done); |
| 8069 | bio_put(bio); |
| 8070 | } |
| 8071 | |
| 8072 | static int __btrfs_subio_endio_read(struct inode *inode, |
| 8073 | struct btrfs_io_bio *io_bio, int err) |
| 8074 | { |
Chandan Rajendra | 2dabb32 | 2016-01-21 15:55:55 +0530 | [diff] [blame] | 8075 | struct btrfs_fs_info *fs_info; |
Miao Xie | 8b110e3 | 2014-09-12 18:44:03 +0800 | [diff] [blame] | 8076 | struct bio_vec *bvec; |
| 8077 | struct btrfs_retry_complete done; |
| 8078 | u64 start; |
| 8079 | u64 offset = 0; |
Chandan Rajendra | 2dabb32 | 2016-01-21 15:55:55 +0530 | [diff] [blame] | 8080 | u32 sectorsize; |
| 8081 | int nr_sectors; |
| 8082 | unsigned int pgoff; |
| 8083 | int csum_pos; |
Miao Xie | 8b110e3 | 2014-09-12 18:44:03 +0800 | [diff] [blame] | 8084 | int i; |
| 8085 | int ret; |
| 8086 | |
Chandan Rajendra | 2dabb32 | 2016-01-21 15:55:55 +0530 | [diff] [blame] | 8087 | fs_info = BTRFS_I(inode)->root->fs_info; |
| 8088 | sectorsize = BTRFS_I(inode)->root->sectorsize; |
| 8089 | |
Miao Xie | 8b110e3 | 2014-09-12 18:44:03 +0800 | [diff] [blame] | 8090 | err = 0; |
| 8091 | start = io_bio->logical; |
| 8092 | done.inode = inode; |
| 8093 | |
Miao Xie | c1dc089 | 2014-09-12 18:43:56 +0800 | [diff] [blame] | 8094 | bio_for_each_segment_all(bvec, &io_bio->bio, i) { |
Chandan Rajendra | 2dabb32 | 2016-01-21 15:55:55 +0530 | [diff] [blame] | 8095 | nr_sectors = BTRFS_BYTES_TO_BLKS(fs_info, bvec->bv_len); |
| 8096 | |
| 8097 | pgoff = bvec->bv_offset; |
| 8098 | next_block: |
| 8099 | csum_pos = BTRFS_BYTES_TO_BLKS(fs_info, offset); |
| 8100 | ret = __readpage_endio_check(inode, io_bio, csum_pos, |
| 8101 | bvec->bv_page, pgoff, start, |
| 8102 | sectorsize); |
Miao Xie | 8b110e3 | 2014-09-12 18:44:03 +0800 | [diff] [blame] | 8103 | if (likely(!ret)) |
| 8104 | goto next; |
| 8105 | try_again: |
| 8106 | done.uptodate = 0; |
| 8107 | done.start = start; |
| 8108 | init_completion(&done.done); |
| 8109 | |
Chandan Rajendra | 2dabb32 | 2016-01-21 15:55:55 +0530 | [diff] [blame] | 8110 | ret = dio_read_error(inode, &io_bio->bio, bvec->bv_page, |
| 8111 | pgoff, start, start + sectorsize - 1, |
| 8112 | io_bio->mirror_num, |
| 8113 | btrfs_retry_endio, &done); |
Miao Xie | 8b110e3 | 2014-09-12 18:44:03 +0800 | [diff] [blame] | 8114 | if (ret) { |
| 8115 | err = ret; |
| 8116 | goto next; |
| 8117 | } |
| 8118 | |
| 8119 | wait_for_completion(&done.done); |
| 8120 | |
| 8121 | if (!done.uptodate) { |
| 8122 | /* We might have another mirror, so try again */ |
| 8123 | goto try_again; |
| 8124 | } |
| 8125 | next: |
Chandan Rajendra | 2dabb32 | 2016-01-21 15:55:55 +0530 | [diff] [blame] | 8126 | offset += sectorsize; |
| 8127 | start += sectorsize; |
| 8128 | |
| 8129 | ASSERT(nr_sectors); |
| 8130 | |
| 8131 | if (--nr_sectors) { |
| 8132 | pgoff += sectorsize; |
| 8133 | goto next_block; |
| 8134 | } |
Kent Overstreet | 2c30c71 | 2013-11-07 12:20:26 -0800 | [diff] [blame] | 8135 | } |
Miao Xie | c1dc089 | 2014-09-12 18:43:56 +0800 | [diff] [blame] | 8136 | |
| 8137 | return err; |
| 8138 | } |
| 8139 | |
Miao Xie | 8b110e3 | 2014-09-12 18:44:03 +0800 | [diff] [blame] | 8140 | static int btrfs_subio_endio_read(struct inode *inode, |
| 8141 | struct btrfs_io_bio *io_bio, int err) |
| 8142 | { |
| 8143 | bool skip_csum = BTRFS_I(inode)->flags & BTRFS_INODE_NODATASUM; |
| 8144 | |
| 8145 | if (skip_csum) { |
| 8146 | if (unlikely(err)) |
| 8147 | return __btrfs_correct_data_nocsum(inode, io_bio); |
| 8148 | else |
| 8149 | return 0; |
| 8150 | } else { |
| 8151 | return __btrfs_subio_endio_read(inode, io_bio, err); |
| 8152 | } |
| 8153 | } |
| 8154 | |
Christoph Hellwig | 4246a0b | 2015-07-20 15:29:37 +0200 | [diff] [blame] | 8155 | static void btrfs_endio_direct_read(struct bio *bio) |
Miao Xie | c1dc089 | 2014-09-12 18:43:56 +0800 | [diff] [blame] | 8156 | { |
| 8157 | struct btrfs_dio_private *dip = bio->bi_private; |
| 8158 | struct inode *inode = dip->inode; |
| 8159 | struct bio *dio_bio; |
| 8160 | struct btrfs_io_bio *io_bio = btrfs_io_bio(bio); |
Christoph Hellwig | 4246a0b | 2015-07-20 15:29:37 +0200 | [diff] [blame] | 8161 | int err = bio->bi_error; |
Miao Xie | c1dc089 | 2014-09-12 18:43:56 +0800 | [diff] [blame] | 8162 | |
Miao Xie | 8b110e3 | 2014-09-12 18:44:03 +0800 | [diff] [blame] | 8163 | if (dip->flags & BTRFS_DIO_ORIG_BIO_SUBMITTED) |
| 8164 | err = btrfs_subio_endio_read(inode, io_bio, err); |
Miao Xie | c1dc089 | 2014-09-12 18:43:56 +0800 | [diff] [blame] | 8165 | |
Josef Bacik | 4b46fce | 2010-05-23 11:00:55 -0400 | [diff] [blame] | 8166 | unlock_extent(&BTRFS_I(inode)->io_tree, dip->logical_offset, |
Jeff Mahoney | d008237 | 2012-03-01 14:57:19 +0100 | [diff] [blame] | 8167 | dip->logical_offset + dip->bytes - 1); |
Chris Mason | 9be3395 | 2013-05-17 18:30:14 -0400 | [diff] [blame] | 8168 | dio_bio = dip->dio_bio; |
Josef Bacik | 4b46fce | 2010-05-23 11:00:55 -0400 | [diff] [blame] | 8169 | |
Josef Bacik | 4b46fce | 2010-05-23 11:00:55 -0400 | [diff] [blame] | 8170 | kfree(dip); |
Josef Bacik | c0da7aa | 2011-03-22 11:05:07 -0400 | [diff] [blame] | 8171 | |
Filipe Manana | 1636d1d | 2016-02-15 16:20:26 +0000 | [diff] [blame] | 8172 | dio_bio->bi_error = bio->bi_error; |
Christoph Hellwig | 4246a0b | 2015-07-20 15:29:37 +0200 | [diff] [blame] | 8173 | dio_end_io(dio_bio, bio->bi_error); |
Miao Xie | 23ea8e5 | 2014-09-12 18:43:54 +0800 | [diff] [blame] | 8174 | |
| 8175 | if (io_bio->end_io) |
| 8176 | io_bio->end_io(io_bio, err); |
Chris Mason | 9be3395 | 2013-05-17 18:30:14 -0400 | [diff] [blame] | 8177 | bio_put(bio); |
Josef Bacik | 4b46fce | 2010-05-23 11:00:55 -0400 | [diff] [blame] | 8178 | } |
| 8179 | |
Filipe Manana | 1454377 | 2015-11-24 16:23:54 +0000 | [diff] [blame] | 8180 | static void btrfs_endio_direct_write_update_ordered(struct inode *inode, |
| 8181 | const u64 offset, |
| 8182 | const u64 bytes, |
| 8183 | const int uptodate) |
Josef Bacik | 4b46fce | 2010-05-23 11:00:55 -0400 | [diff] [blame] | 8184 | { |
Josef Bacik | 4b46fce | 2010-05-23 11:00:55 -0400 | [diff] [blame] | 8185 | struct btrfs_root *root = BTRFS_I(inode)->root; |
Josef Bacik | 4b46fce | 2010-05-23 11:00:55 -0400 | [diff] [blame] | 8186 | struct btrfs_ordered_extent *ordered = NULL; |
Filipe Manana | 1454377 | 2015-11-24 16:23:54 +0000 | [diff] [blame] | 8187 | u64 ordered_offset = offset; |
| 8188 | u64 ordered_bytes = bytes; |
Josef Bacik | 4b46fce | 2010-05-23 11:00:55 -0400 | [diff] [blame] | 8189 | int ret; |
| 8190 | |
Chris Mason | 163cf09 | 2010-11-28 19:56:33 -0500 | [diff] [blame] | 8191 | again: |
| 8192 | ret = btrfs_dec_test_first_ordered_pending(inode, &ordered, |
| 8193 | &ordered_offset, |
Christoph Hellwig | 4246a0b | 2015-07-20 15:29:37 +0200 | [diff] [blame] | 8194 | ordered_bytes, |
Filipe Manana | 1454377 | 2015-11-24 16:23:54 +0000 | [diff] [blame] | 8195 | uptodate); |
Josef Bacik | 4b46fce | 2010-05-23 11:00:55 -0400 | [diff] [blame] | 8196 | if (!ret) |
Chris Mason | 163cf09 | 2010-11-28 19:56:33 -0500 | [diff] [blame] | 8197 | goto out_test; |
Josef Bacik | 4b46fce | 2010-05-23 11:00:55 -0400 | [diff] [blame] | 8198 | |
Liu Bo | 9e0af23 | 2014-08-15 23:36:53 +0800 | [diff] [blame] | 8199 | btrfs_init_work(&ordered->work, btrfs_endio_write_helper, |
| 8200 | finish_ordered_fn, NULL, NULL); |
Qu Wenruo | fccb5d8 | 2014-02-28 10:46:10 +0800 | [diff] [blame] | 8201 | btrfs_queue_work(root->fs_info->endio_write_workers, |
| 8202 | &ordered->work); |
Chris Mason | 163cf09 | 2010-11-28 19:56:33 -0500 | [diff] [blame] | 8203 | out_test: |
| 8204 | /* |
| 8205 | * our bio might span multiple ordered extents. If we haven't |
| 8206 | * completed the accounting for the whole dio, go back and try again |
| 8207 | */ |
Filipe Manana | 1454377 | 2015-11-24 16:23:54 +0000 | [diff] [blame] | 8208 | if (ordered_offset < offset + bytes) { |
| 8209 | ordered_bytes = offset + bytes - ordered_offset; |
Josef Bacik | 5fd0204 | 2012-05-02 14:00:54 -0400 | [diff] [blame] | 8210 | ordered = NULL; |
Chris Mason | 163cf09 | 2010-11-28 19:56:33 -0500 | [diff] [blame] | 8211 | goto again; |
| 8212 | } |
Filipe Manana | 1454377 | 2015-11-24 16:23:54 +0000 | [diff] [blame] | 8213 | } |
| 8214 | |
| 8215 | static void btrfs_endio_direct_write(struct bio *bio) |
| 8216 | { |
| 8217 | struct btrfs_dio_private *dip = bio->bi_private; |
| 8218 | struct bio *dio_bio = dip->dio_bio; |
| 8219 | |
| 8220 | btrfs_endio_direct_write_update_ordered(dip->inode, |
| 8221 | dip->logical_offset, |
| 8222 | dip->bytes, |
| 8223 | !bio->bi_error); |
Josef Bacik | 4b46fce | 2010-05-23 11:00:55 -0400 | [diff] [blame] | 8224 | |
Josef Bacik | 4b46fce | 2010-05-23 11:00:55 -0400 | [diff] [blame] | 8225 | kfree(dip); |
Josef Bacik | c0da7aa | 2011-03-22 11:05:07 -0400 | [diff] [blame] | 8226 | |
Filipe Manana | 1636d1d | 2016-02-15 16:20:26 +0000 | [diff] [blame] | 8227 | dio_bio->bi_error = bio->bi_error; |
Christoph Hellwig | 4246a0b | 2015-07-20 15:29:37 +0200 | [diff] [blame] | 8228 | dio_end_io(dio_bio, bio->bi_error); |
Chris Mason | 9be3395 | 2013-05-17 18:30:14 -0400 | [diff] [blame] | 8229 | bio_put(bio); |
Josef Bacik | 4b46fce | 2010-05-23 11:00:55 -0400 | [diff] [blame] | 8230 | } |
| 8231 | |
Mike Christie | 81a75f6 | 2016-06-05 14:31:54 -0500 | [diff] [blame] | 8232 | static int __btrfs_submit_bio_start_direct_io(struct inode *inode, |
Chris Mason | eaf25d9 | 2010-05-25 09:48:28 -0400 | [diff] [blame] | 8233 | struct bio *bio, int mirror_num, |
| 8234 | unsigned long bio_flags, u64 offset) |
| 8235 | { |
| 8236 | int ret; |
| 8237 | struct btrfs_root *root = BTRFS_I(inode)->root; |
| 8238 | ret = btrfs_csum_one_bio(root, inode, bio, offset, 1); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 8239 | BUG_ON(ret); /* -ENOMEM */ |
Chris Mason | eaf25d9 | 2010-05-25 09:48:28 -0400 | [diff] [blame] | 8240 | return 0; |
| 8241 | } |
| 8242 | |
Christoph Hellwig | 4246a0b | 2015-07-20 15:29:37 +0200 | [diff] [blame] | 8243 | static void btrfs_end_dio_bio(struct bio *bio) |
Miao Xie | e65e153 | 2010-11-22 03:04:43 +0000 | [diff] [blame] | 8244 | { |
| 8245 | struct btrfs_dio_private *dip = bio->bi_private; |
Christoph Hellwig | 4246a0b | 2015-07-20 15:29:37 +0200 | [diff] [blame] | 8246 | int err = bio->bi_error; |
Miao Xie | e65e153 | 2010-11-22 03:04:43 +0000 | [diff] [blame] | 8247 | |
Miao Xie | 8b110e3 | 2014-09-12 18:44:03 +0800 | [diff] [blame] | 8248 | if (err) |
| 8249 | btrfs_warn(BTRFS_I(dip->inode)->root->fs_info, |
Mike Christie | 6296b96 | 2016-06-05 14:32:21 -0500 | [diff] [blame] | 8250 | "direct IO failed ino %llu rw %d,%u sector %#Lx len %u err no %d", |
Jens Axboe | 1eff9d3 | 2016-08-05 15:35:16 -0600 | [diff] [blame] | 8251 | btrfs_ino(dip->inode), bio_op(bio), bio->bi_opf, |
Miao Xie | 8b110e3 | 2014-09-12 18:44:03 +0800 | [diff] [blame] | 8252 | (unsigned long long)bio->bi_iter.bi_sector, |
| 8253 | bio->bi_iter.bi_size, err); |
| 8254 | |
| 8255 | if (dip->subio_endio) |
| 8256 | err = dip->subio_endio(dip->inode, btrfs_io_bio(bio), err); |
Miao Xie | c1dc089 | 2014-09-12 18:43:56 +0800 | [diff] [blame] | 8257 | |
| 8258 | if (err) { |
Miao Xie | e65e153 | 2010-11-22 03:04:43 +0000 | [diff] [blame] | 8259 | dip->errors = 1; |
| 8260 | |
| 8261 | /* |
| 8262 | * before atomic variable goto zero, we must make sure |
| 8263 | * dip->errors is perceived to be set. |
| 8264 | */ |
Peter Zijlstra | 4e857c5 | 2014-03-17 18:06:10 +0100 | [diff] [blame] | 8265 | smp_mb__before_atomic(); |
Miao Xie | e65e153 | 2010-11-22 03:04:43 +0000 | [diff] [blame] | 8266 | } |
| 8267 | |
| 8268 | /* if there are more bios still pending for this dio, just exit */ |
| 8269 | if (!atomic_dec_and_test(&dip->pending_bios)) |
| 8270 | goto out; |
| 8271 | |
Chris Mason | 9be3395 | 2013-05-17 18:30:14 -0400 | [diff] [blame] | 8272 | if (dip->errors) { |
Miao Xie | e65e153 | 2010-11-22 03:04:43 +0000 | [diff] [blame] | 8273 | bio_io_error(dip->orig_bio); |
Chris Mason | 9be3395 | 2013-05-17 18:30:14 -0400 | [diff] [blame] | 8274 | } else { |
Christoph Hellwig | 4246a0b | 2015-07-20 15:29:37 +0200 | [diff] [blame] | 8275 | dip->dio_bio->bi_error = 0; |
| 8276 | bio_endio(dip->orig_bio); |
Miao Xie | e65e153 | 2010-11-22 03:04:43 +0000 | [diff] [blame] | 8277 | } |
| 8278 | out: |
| 8279 | bio_put(bio); |
| 8280 | } |
| 8281 | |
| 8282 | static struct bio *btrfs_dio_bio_alloc(struct block_device *bdev, |
| 8283 | u64 first_sector, gfp_t gfp_flags) |
| 8284 | { |
Chris Mason | da2f0f7 | 2015-07-02 13:57:22 -0700 | [diff] [blame] | 8285 | struct bio *bio; |
Linus Torvalds | 2236597 | 2015-09-05 15:14:43 -0700 | [diff] [blame] | 8286 | bio = btrfs_bio_alloc(bdev, first_sector, BIO_MAX_PAGES, gfp_flags); |
Chris Mason | da2f0f7 | 2015-07-02 13:57:22 -0700 | [diff] [blame] | 8287 | if (bio) |
| 8288 | bio_associate_current(bio); |
| 8289 | return bio; |
Miao Xie | e65e153 | 2010-11-22 03:04:43 +0000 | [diff] [blame] | 8290 | } |
| 8291 | |
Miao Xie | c1dc089 | 2014-09-12 18:43:56 +0800 | [diff] [blame] | 8292 | static inline int btrfs_lookup_and_bind_dio_csum(struct btrfs_root *root, |
| 8293 | struct inode *inode, |
| 8294 | struct btrfs_dio_private *dip, |
| 8295 | struct bio *bio, |
| 8296 | u64 file_offset) |
| 8297 | { |
| 8298 | struct btrfs_io_bio *io_bio = btrfs_io_bio(bio); |
| 8299 | struct btrfs_io_bio *orig_io_bio = btrfs_io_bio(dip->orig_bio); |
| 8300 | int ret; |
| 8301 | |
| 8302 | /* |
| 8303 | * We load all the csum data we need when we submit |
| 8304 | * the first bio to reduce the csum tree search and |
| 8305 | * contention. |
| 8306 | */ |
| 8307 | if (dip->logical_offset == file_offset) { |
| 8308 | ret = btrfs_lookup_bio_sums_dio(root, inode, dip->orig_bio, |
| 8309 | file_offset); |
| 8310 | if (ret) |
| 8311 | return ret; |
| 8312 | } |
| 8313 | |
| 8314 | if (bio == dip->orig_bio) |
| 8315 | return 0; |
| 8316 | |
| 8317 | file_offset -= dip->logical_offset; |
| 8318 | file_offset >>= inode->i_sb->s_blocksize_bits; |
| 8319 | io_bio->csum = (u8 *)(((u32 *)orig_io_bio->csum) + file_offset); |
| 8320 | |
| 8321 | return 0; |
| 8322 | } |
| 8323 | |
Miao Xie | e65e153 | 2010-11-22 03:04:43 +0000 | [diff] [blame] | 8324 | static inline int __btrfs_submit_dio_bio(struct bio *bio, struct inode *inode, |
Mike Christie | 81a75f6 | 2016-06-05 14:31:54 -0500 | [diff] [blame] | 8325 | u64 file_offset, int skip_sum, |
Josef Bacik | c329861 | 2012-08-03 16:49:19 -0400 | [diff] [blame] | 8326 | int async_submit) |
Miao Xie | e65e153 | 2010-11-22 03:04:43 +0000 | [diff] [blame] | 8327 | { |
Miao Xie | facc8a22 | 2013-07-25 19:22:34 +0800 | [diff] [blame] | 8328 | struct btrfs_dio_private *dip = bio->bi_private; |
Mike Christie | 37226b2 | 2016-06-05 14:31:52 -0500 | [diff] [blame] | 8329 | bool write = bio_op(bio) == REQ_OP_WRITE; |
Miao Xie | e65e153 | 2010-11-22 03:04:43 +0000 | [diff] [blame] | 8330 | struct btrfs_root *root = BTRFS_I(inode)->root; |
| 8331 | int ret; |
| 8332 | |
Josef Bacik | b812ce2 | 2012-11-16 13:56:32 -0500 | [diff] [blame] | 8333 | if (async_submit) |
| 8334 | async_submit = !atomic_read(&BTRFS_I(inode)->sync_writers); |
| 8335 | |
Miao Xie | e65e153 | 2010-11-22 03:04:43 +0000 | [diff] [blame] | 8336 | bio_get(bio); |
Josef Bacik | 5fd0204 | 2012-05-02 14:00:54 -0400 | [diff] [blame] | 8337 | |
| 8338 | if (!write) { |
David Sterba | bfebd8b | 2014-07-30 00:25:45 +0200 | [diff] [blame] | 8339 | ret = btrfs_bio_wq_end_io(root->fs_info, bio, |
| 8340 | BTRFS_WQ_ENDIO_DATA); |
Josef Bacik | 5fd0204 | 2012-05-02 14:00:54 -0400 | [diff] [blame] | 8341 | if (ret) |
| 8342 | goto err; |
| 8343 | } |
Miao Xie | e65e153 | 2010-11-22 03:04:43 +0000 | [diff] [blame] | 8344 | |
Josef Bacik | 1ae3993 | 2011-04-06 14:41:34 -0400 | [diff] [blame] | 8345 | if (skip_sum) |
| 8346 | goto map; |
| 8347 | |
| 8348 | if (write && async_submit) { |
Miao Xie | e65e153 | 2010-11-22 03:04:43 +0000 | [diff] [blame] | 8349 | ret = btrfs_wq_submit_bio(root->fs_info, |
Mike Christie | 81a75f6 | 2016-06-05 14:31:54 -0500 | [diff] [blame] | 8350 | inode, bio, 0, 0, file_offset, |
Miao Xie | e65e153 | 2010-11-22 03:04:43 +0000 | [diff] [blame] | 8351 | __btrfs_submit_bio_start_direct_io, |
| 8352 | __btrfs_submit_bio_done); |
| 8353 | goto err; |
Josef Bacik | 1ae3993 | 2011-04-06 14:41:34 -0400 | [diff] [blame] | 8354 | } else if (write) { |
| 8355 | /* |
| 8356 | * If we aren't doing async submit, calculate the csum of the |
| 8357 | * bio now. |
| 8358 | */ |
| 8359 | ret = btrfs_csum_one_bio(root, inode, bio, file_offset, 1); |
| 8360 | if (ret) |
| 8361 | goto err; |
Miao Xie | 23ea8e5 | 2014-09-12 18:43:54 +0800 | [diff] [blame] | 8362 | } else { |
Miao Xie | c1dc089 | 2014-09-12 18:43:56 +0800 | [diff] [blame] | 8363 | ret = btrfs_lookup_and_bind_dio_csum(root, inode, dip, bio, |
| 8364 | file_offset); |
Tsutomu Itoh | c2db107 | 2011-03-01 06:48:31 +0000 | [diff] [blame] | 8365 | if (ret) |
| 8366 | goto err; |
| 8367 | } |
Josef Bacik | 1ae3993 | 2011-04-06 14:41:34 -0400 | [diff] [blame] | 8368 | map: |
Mike Christie | 81a75f6 | 2016-06-05 14:31:54 -0500 | [diff] [blame] | 8369 | ret = btrfs_map_bio(root, bio, 0, async_submit); |
Miao Xie | e65e153 | 2010-11-22 03:04:43 +0000 | [diff] [blame] | 8370 | err: |
| 8371 | bio_put(bio); |
| 8372 | return ret; |
| 8373 | } |
| 8374 | |
Mike Christie | 81a75f6 | 2016-06-05 14:31:54 -0500 | [diff] [blame] | 8375 | static int btrfs_submit_direct_hook(struct btrfs_dio_private *dip, |
Miao Xie | e65e153 | 2010-11-22 03:04:43 +0000 | [diff] [blame] | 8376 | int skip_sum) |
| 8377 | { |
| 8378 | struct inode *inode = dip->inode; |
| 8379 | struct btrfs_root *root = BTRFS_I(inode)->root; |
Miao Xie | e65e153 | 2010-11-22 03:04:43 +0000 | [diff] [blame] | 8380 | struct bio *bio; |
| 8381 | struct bio *orig_bio = dip->orig_bio; |
| 8382 | struct bio_vec *bvec = orig_bio->bi_io_vec; |
Kent Overstreet | 4f024f3 | 2013-10-11 15:44:27 -0700 | [diff] [blame] | 8383 | u64 start_sector = orig_bio->bi_iter.bi_sector; |
Miao Xie | e65e153 | 2010-11-22 03:04:43 +0000 | [diff] [blame] | 8384 | u64 file_offset = dip->logical_offset; |
| 8385 | u64 submit_len = 0; |
| 8386 | u64 map_length; |
Chandan Rajendra | 5f4dc8f | 2016-01-21 15:56:00 +0530 | [diff] [blame] | 8387 | u32 blocksize = root->sectorsize; |
Josef Bacik | 1ae3993 | 2011-04-06 14:41:34 -0400 | [diff] [blame] | 8388 | int async_submit = 0; |
Chandan Rajendra | 5f4dc8f | 2016-01-21 15:56:00 +0530 | [diff] [blame] | 8389 | int nr_sectors; |
| 8390 | int ret; |
| 8391 | int i; |
Miao Xie | e65e153 | 2010-11-22 03:04:43 +0000 | [diff] [blame] | 8392 | |
Kent Overstreet | 4f024f3 | 2013-10-11 15:44:27 -0700 | [diff] [blame] | 8393 | map_length = orig_bio->bi_iter.bi_size; |
Mike Christie | 37226b2 | 2016-06-05 14:31:52 -0500 | [diff] [blame] | 8394 | ret = btrfs_map_block(root->fs_info, bio_op(orig_bio), |
| 8395 | start_sector << 9, &map_length, NULL, 0); |
Miao Xie | 7a5c3c9 | 2014-06-17 18:58:59 +0800 | [diff] [blame] | 8396 | if (ret) |
Miao Xie | e65e153 | 2010-11-22 03:04:43 +0000 | [diff] [blame] | 8397 | return -EIO; |
Miao Xie | facc8a22 | 2013-07-25 19:22:34 +0800 | [diff] [blame] | 8398 | |
Kent Overstreet | 4f024f3 | 2013-10-11 15:44:27 -0700 | [diff] [blame] | 8399 | if (map_length >= orig_bio->bi_iter.bi_size) { |
Josef Bacik | 02f57c7 | 2011-04-06 14:25:44 -0400 | [diff] [blame] | 8400 | bio = orig_bio; |
Miao Xie | c1dc089 | 2014-09-12 18:43:56 +0800 | [diff] [blame] | 8401 | dip->flags |= BTRFS_DIO_ORIG_BIO_SUBMITTED; |
Josef Bacik | 02f57c7 | 2011-04-06 14:25:44 -0400 | [diff] [blame] | 8402 | goto submit; |
| 8403 | } |
| 8404 | |
David Woodhouse | 53b381b | 2013-01-29 18:40:14 -0500 | [diff] [blame] | 8405 | /* async crcs make it difficult to collect full stripe writes. */ |
Zhao Lei | ffe2d20 | 2015-01-20 15:11:44 +0800 | [diff] [blame] | 8406 | if (btrfs_get_alloc_profile(root, 1) & BTRFS_BLOCK_GROUP_RAID56_MASK) |
David Woodhouse | 53b381b | 2013-01-29 18:40:14 -0500 | [diff] [blame] | 8407 | async_submit = 0; |
| 8408 | else |
| 8409 | async_submit = 1; |
| 8410 | |
Josef Bacik | 02f57c7 | 2011-04-06 14:25:44 -0400 | [diff] [blame] | 8411 | bio = btrfs_dio_bio_alloc(orig_bio->bi_bdev, start_sector, GFP_NOFS); |
| 8412 | if (!bio) |
| 8413 | return -ENOMEM; |
Miao Xie | 7a5c3c9 | 2014-06-17 18:58:59 +0800 | [diff] [blame] | 8414 | |
Jens Axboe | 1eff9d3 | 2016-08-05 15:35:16 -0600 | [diff] [blame] | 8415 | bio_set_op_attrs(bio, bio_op(orig_bio), orig_bio->bi_opf); |
Josef Bacik | 02f57c7 | 2011-04-06 14:25:44 -0400 | [diff] [blame] | 8416 | bio->bi_private = dip; |
| 8417 | bio->bi_end_io = btrfs_end_dio_bio; |
Miao Xie | c1dc089 | 2014-09-12 18:43:56 +0800 | [diff] [blame] | 8418 | btrfs_io_bio(bio)->logical = file_offset; |
Josef Bacik | 02f57c7 | 2011-04-06 14:25:44 -0400 | [diff] [blame] | 8419 | atomic_inc(&dip->pending_bios); |
| 8420 | |
Miao Xie | e65e153 | 2010-11-22 03:04:43 +0000 | [diff] [blame] | 8421 | while (bvec <= (orig_bio->bi_io_vec + orig_bio->bi_vcnt - 1)) { |
Chandan Rajendra | 5f4dc8f | 2016-01-21 15:56:00 +0530 | [diff] [blame] | 8422 | nr_sectors = BTRFS_BYTES_TO_BLKS(root->fs_info, bvec->bv_len); |
| 8423 | i = 0; |
| 8424 | next_block: |
| 8425 | if (unlikely(map_length < submit_len + blocksize || |
| 8426 | bio_add_page(bio, bvec->bv_page, blocksize, |
| 8427 | bvec->bv_offset + (i * blocksize)) < blocksize)) { |
Miao Xie | e65e153 | 2010-11-22 03:04:43 +0000 | [diff] [blame] | 8428 | /* |
| 8429 | * inc the count before we submit the bio so |
| 8430 | * we know the end IO handler won't happen before |
| 8431 | * we inc the count. Otherwise, the dip might get freed |
| 8432 | * before we're done setting it up |
| 8433 | */ |
| 8434 | atomic_inc(&dip->pending_bios); |
Mike Christie | 81a75f6 | 2016-06-05 14:31:54 -0500 | [diff] [blame] | 8435 | ret = __btrfs_submit_dio_bio(bio, inode, |
Miao Xie | e65e153 | 2010-11-22 03:04:43 +0000 | [diff] [blame] | 8436 | file_offset, skip_sum, |
Josef Bacik | c329861 | 2012-08-03 16:49:19 -0400 | [diff] [blame] | 8437 | async_submit); |
Miao Xie | e65e153 | 2010-11-22 03:04:43 +0000 | [diff] [blame] | 8438 | if (ret) { |
| 8439 | bio_put(bio); |
| 8440 | atomic_dec(&dip->pending_bios); |
| 8441 | goto out_err; |
| 8442 | } |
| 8443 | |
Miao Xie | e65e153 | 2010-11-22 03:04:43 +0000 | [diff] [blame] | 8444 | start_sector += submit_len >> 9; |
| 8445 | file_offset += submit_len; |
| 8446 | |
| 8447 | submit_len = 0; |
Miao Xie | e65e153 | 2010-11-22 03:04:43 +0000 | [diff] [blame] | 8448 | |
| 8449 | bio = btrfs_dio_bio_alloc(orig_bio->bi_bdev, |
| 8450 | start_sector, GFP_NOFS); |
| 8451 | if (!bio) |
| 8452 | goto out_err; |
Jens Axboe | 1eff9d3 | 2016-08-05 15:35:16 -0600 | [diff] [blame] | 8453 | bio_set_op_attrs(bio, bio_op(orig_bio), orig_bio->bi_opf); |
Miao Xie | e65e153 | 2010-11-22 03:04:43 +0000 | [diff] [blame] | 8454 | bio->bi_private = dip; |
| 8455 | bio->bi_end_io = btrfs_end_dio_bio; |
Miao Xie | c1dc089 | 2014-09-12 18:43:56 +0800 | [diff] [blame] | 8456 | btrfs_io_bio(bio)->logical = file_offset; |
Miao Xie | e65e153 | 2010-11-22 03:04:43 +0000 | [diff] [blame] | 8457 | |
Kent Overstreet | 4f024f3 | 2013-10-11 15:44:27 -0700 | [diff] [blame] | 8458 | map_length = orig_bio->bi_iter.bi_size; |
Mike Christie | 37226b2 | 2016-06-05 14:31:52 -0500 | [diff] [blame] | 8459 | ret = btrfs_map_block(root->fs_info, bio_op(orig_bio), |
Stefan Behrens | 3ec706c | 2012-11-05 15:46:42 +0100 | [diff] [blame] | 8460 | start_sector << 9, |
Miao Xie | e65e153 | 2010-11-22 03:04:43 +0000 | [diff] [blame] | 8461 | &map_length, NULL, 0); |
| 8462 | if (ret) { |
| 8463 | bio_put(bio); |
| 8464 | goto out_err; |
| 8465 | } |
Chandan Rajendra | 5f4dc8f | 2016-01-21 15:56:00 +0530 | [diff] [blame] | 8466 | |
| 8467 | goto next_block; |
Miao Xie | e65e153 | 2010-11-22 03:04:43 +0000 | [diff] [blame] | 8468 | } else { |
Chandan Rajendra | 5f4dc8f | 2016-01-21 15:56:00 +0530 | [diff] [blame] | 8469 | submit_len += blocksize; |
| 8470 | if (--nr_sectors) { |
| 8471 | i++; |
| 8472 | goto next_block; |
| 8473 | } |
Miao Xie | e65e153 | 2010-11-22 03:04:43 +0000 | [diff] [blame] | 8474 | bvec++; |
| 8475 | } |
| 8476 | } |
| 8477 | |
Josef Bacik | 02f57c7 | 2011-04-06 14:25:44 -0400 | [diff] [blame] | 8478 | submit: |
Mike Christie | 81a75f6 | 2016-06-05 14:31:54 -0500 | [diff] [blame] | 8479 | ret = __btrfs_submit_dio_bio(bio, inode, file_offset, skip_sum, |
Josef Bacik | c329861 | 2012-08-03 16:49:19 -0400 | [diff] [blame] | 8480 | async_submit); |
Miao Xie | e65e153 | 2010-11-22 03:04:43 +0000 | [diff] [blame] | 8481 | if (!ret) |
| 8482 | return 0; |
| 8483 | |
| 8484 | bio_put(bio); |
| 8485 | out_err: |
| 8486 | dip->errors = 1; |
| 8487 | /* |
| 8488 | * before atomic variable goto zero, we must |
| 8489 | * make sure dip->errors is perceived to be set. |
| 8490 | */ |
Peter Zijlstra | 4e857c5 | 2014-03-17 18:06:10 +0100 | [diff] [blame] | 8491 | smp_mb__before_atomic(); |
Miao Xie | e65e153 | 2010-11-22 03:04:43 +0000 | [diff] [blame] | 8492 | if (atomic_dec_and_test(&dip->pending_bios)) |
| 8493 | bio_io_error(dip->orig_bio); |
| 8494 | |
| 8495 | /* bio_end_io() will handle error, so we needn't return it */ |
| 8496 | return 0; |
| 8497 | } |
| 8498 | |
Mike Christie | 8a4c1e4 | 2016-06-05 14:31:50 -0500 | [diff] [blame] | 8499 | static void btrfs_submit_direct(struct bio *dio_bio, struct inode *inode, |
| 8500 | loff_t file_offset) |
Josef Bacik | 4b46fce | 2010-05-23 11:00:55 -0400 | [diff] [blame] | 8501 | { |
Filipe Manana | 61de718 | 2015-07-01 12:13:10 +0100 | [diff] [blame] | 8502 | struct btrfs_dio_private *dip = NULL; |
| 8503 | struct bio *io_bio = NULL; |
Miao Xie | 23ea8e5 | 2014-09-12 18:43:54 +0800 | [diff] [blame] | 8504 | struct btrfs_io_bio *btrfs_bio; |
Josef Bacik | 4b46fce | 2010-05-23 11:00:55 -0400 | [diff] [blame] | 8505 | int skip_sum; |
Mike Christie | 8a4c1e4 | 2016-06-05 14:31:50 -0500 | [diff] [blame] | 8506 | bool write = (bio_op(dio_bio) == REQ_OP_WRITE); |
Josef Bacik | 4b46fce | 2010-05-23 11:00:55 -0400 | [diff] [blame] | 8507 | int ret = 0; |
| 8508 | |
| 8509 | skip_sum = BTRFS_I(inode)->flags & BTRFS_INODE_NODATASUM; |
| 8510 | |
Chris Mason | 9be3395 | 2013-05-17 18:30:14 -0400 | [diff] [blame] | 8511 | io_bio = btrfs_bio_clone(dio_bio, GFP_NOFS); |
Chris Mason | 9be3395 | 2013-05-17 18:30:14 -0400 | [diff] [blame] | 8512 | if (!io_bio) { |
Josef Bacik | 4b46fce | 2010-05-23 11:00:55 -0400 | [diff] [blame] | 8513 | ret = -ENOMEM; |
| 8514 | goto free_ordered; |
| 8515 | } |
Josef Bacik | 4b46fce | 2010-05-23 11:00:55 -0400 | [diff] [blame] | 8516 | |
Miao Xie | c1dc089 | 2014-09-12 18:43:56 +0800 | [diff] [blame] | 8517 | dip = kzalloc(sizeof(*dip), GFP_NOFS); |
Chris Mason | 9be3395 | 2013-05-17 18:30:14 -0400 | [diff] [blame] | 8518 | if (!dip) { |
| 8519 | ret = -ENOMEM; |
Filipe Manana | 61de718 | 2015-07-01 12:13:10 +0100 | [diff] [blame] | 8520 | goto free_ordered; |
Chris Mason | 9be3395 | 2013-05-17 18:30:14 -0400 | [diff] [blame] | 8521 | } |
| 8522 | |
| 8523 | dip->private = dio_bio->bi_private; |
Josef Bacik | 4b46fce | 2010-05-23 11:00:55 -0400 | [diff] [blame] | 8524 | dip->inode = inode; |
| 8525 | dip->logical_offset = file_offset; |
Kent Overstreet | 4f024f3 | 2013-10-11 15:44:27 -0700 | [diff] [blame] | 8526 | dip->bytes = dio_bio->bi_iter.bi_size; |
| 8527 | dip->disk_bytenr = (u64)dio_bio->bi_iter.bi_sector << 9; |
Chris Mason | 9be3395 | 2013-05-17 18:30:14 -0400 | [diff] [blame] | 8528 | io_bio->bi_private = dip; |
Chris Mason | 9be3395 | 2013-05-17 18:30:14 -0400 | [diff] [blame] | 8529 | dip->orig_bio = io_bio; |
| 8530 | dip->dio_bio = dio_bio; |
Miao Xie | e65e153 | 2010-11-22 03:04:43 +0000 | [diff] [blame] | 8531 | atomic_set(&dip->pending_bios, 0); |
Miao Xie | c1dc089 | 2014-09-12 18:43:56 +0800 | [diff] [blame] | 8532 | btrfs_bio = btrfs_io_bio(io_bio); |
| 8533 | btrfs_bio->logical = file_offset; |
Josef Bacik | 4b46fce | 2010-05-23 11:00:55 -0400 | [diff] [blame] | 8534 | |
Miao Xie | c1dc089 | 2014-09-12 18:43:56 +0800 | [diff] [blame] | 8535 | if (write) { |
Chris Mason | 9be3395 | 2013-05-17 18:30:14 -0400 | [diff] [blame] | 8536 | io_bio->bi_end_io = btrfs_endio_direct_write; |
Miao Xie | c1dc089 | 2014-09-12 18:43:56 +0800 | [diff] [blame] | 8537 | } else { |
Chris Mason | 9be3395 | 2013-05-17 18:30:14 -0400 | [diff] [blame] | 8538 | io_bio->bi_end_io = btrfs_endio_direct_read; |
Miao Xie | c1dc089 | 2014-09-12 18:43:56 +0800 | [diff] [blame] | 8539 | dip->subio_endio = btrfs_subio_endio_read; |
| 8540 | } |
Josef Bacik | 4b46fce | 2010-05-23 11:00:55 -0400 | [diff] [blame] | 8541 | |
Filipe Manana | f28a492 | 2015-12-08 19:23:20 +0000 | [diff] [blame] | 8542 | /* |
| 8543 | * Reset the range for unsubmitted ordered extents (to a 0 length range) |
| 8544 | * even if we fail to submit a bio, because in such case we do the |
| 8545 | * corresponding error handling below and it must not be done a second |
| 8546 | * time by btrfs_direct_IO(). |
| 8547 | */ |
| 8548 | if (write) { |
| 8549 | struct btrfs_dio_data *dio_data = current->journal_info; |
| 8550 | |
| 8551 | dio_data->unsubmitted_oe_range_end = dip->logical_offset + |
| 8552 | dip->bytes; |
| 8553 | dio_data->unsubmitted_oe_range_start = |
| 8554 | dio_data->unsubmitted_oe_range_end; |
| 8555 | } |
| 8556 | |
Mike Christie | 81a75f6 | 2016-06-05 14:31:54 -0500 | [diff] [blame] | 8557 | ret = btrfs_submit_direct_hook(dip, skip_sum); |
Miao Xie | e65e153 | 2010-11-22 03:04:43 +0000 | [diff] [blame] | 8558 | if (!ret) |
Chris Mason | eaf25d9 | 2010-05-25 09:48:28 -0400 | [diff] [blame] | 8559 | return; |
Chris Mason | 9be3395 | 2013-05-17 18:30:14 -0400 | [diff] [blame] | 8560 | |
Miao Xie | 23ea8e5 | 2014-09-12 18:43:54 +0800 | [diff] [blame] | 8561 | if (btrfs_bio->end_io) |
| 8562 | btrfs_bio->end_io(btrfs_bio, ret); |
Chris Mason | 9be3395 | 2013-05-17 18:30:14 -0400 | [diff] [blame] | 8563 | |
Josef Bacik | 4b46fce | 2010-05-23 11:00:55 -0400 | [diff] [blame] | 8564 | free_ordered: |
| 8565 | /* |
Filipe Manana | 61de718 | 2015-07-01 12:13:10 +0100 | [diff] [blame] | 8566 | * If we arrived here it means either we failed to submit the dip |
| 8567 | * or we either failed to clone the dio_bio or failed to allocate the |
| 8568 | * dip. If we cloned the dio_bio and allocated the dip, we can just |
| 8569 | * call bio_endio against our io_bio so that we get proper resource |
| 8570 | * cleanup if we fail to submit the dip, otherwise, we must do the |
| 8571 | * same as btrfs_endio_direct_[write|read] because we can't call these |
| 8572 | * callbacks - they require an allocated dip and a clone of dio_bio. |
Josef Bacik | 4b46fce | 2010-05-23 11:00:55 -0400 | [diff] [blame] | 8573 | */ |
Filipe Manana | 61de718 | 2015-07-01 12:13:10 +0100 | [diff] [blame] | 8574 | if (io_bio && dip) { |
Christoph Hellwig | 4246a0b | 2015-07-20 15:29:37 +0200 | [diff] [blame] | 8575 | io_bio->bi_error = -EIO; |
| 8576 | bio_endio(io_bio); |
Filipe Manana | 61de718 | 2015-07-01 12:13:10 +0100 | [diff] [blame] | 8577 | /* |
| 8578 | * The end io callbacks free our dip, do the final put on io_bio |
| 8579 | * and all the cleanup and final put for dio_bio (through |
| 8580 | * dio_end_io()). |
| 8581 | */ |
| 8582 | dip = NULL; |
| 8583 | io_bio = NULL; |
| 8584 | } else { |
Filipe Manana | 1454377 | 2015-11-24 16:23:54 +0000 | [diff] [blame] | 8585 | if (write) |
| 8586 | btrfs_endio_direct_write_update_ordered(inode, |
| 8587 | file_offset, |
| 8588 | dio_bio->bi_iter.bi_size, |
| 8589 | 0); |
| 8590 | else |
Filipe Manana | 61de718 | 2015-07-01 12:13:10 +0100 | [diff] [blame] | 8591 | unlock_extent(&BTRFS_I(inode)->io_tree, file_offset, |
| 8592 | file_offset + dio_bio->bi_iter.bi_size - 1); |
Filipe Manana | 1454377 | 2015-11-24 16:23:54 +0000 | [diff] [blame] | 8593 | |
Christoph Hellwig | 4246a0b | 2015-07-20 15:29:37 +0200 | [diff] [blame] | 8594 | dio_bio->bi_error = -EIO; |
Filipe Manana | 61de718 | 2015-07-01 12:13:10 +0100 | [diff] [blame] | 8595 | /* |
| 8596 | * Releases and cleans up our dio_bio, no need to bio_put() |
| 8597 | * nor bio_endio()/bio_io_error() against dio_bio. |
| 8598 | */ |
| 8599 | dio_end_io(dio_bio, ret); |
Josef Bacik | 4b46fce | 2010-05-23 11:00:55 -0400 | [diff] [blame] | 8600 | } |
Filipe Manana | 61de718 | 2015-07-01 12:13:10 +0100 | [diff] [blame] | 8601 | if (io_bio) |
| 8602 | bio_put(io_bio); |
| 8603 | kfree(dip); |
Josef Bacik | 4b46fce | 2010-05-23 11:00:55 -0400 | [diff] [blame] | 8604 | } |
| 8605 | |
Omar Sandoval | 6f67376 | 2015-03-16 04:33:52 -0700 | [diff] [blame] | 8606 | static ssize_t check_direct_IO(struct btrfs_root *root, struct kiocb *iocb, |
Al Viro | 28060d5 | 2014-03-22 05:15:17 -0400 | [diff] [blame] | 8607 | const struct iov_iter *iter, loff_t offset) |
Chris Mason | 5a5f79b | 2010-05-26 21:33:37 -0400 | [diff] [blame] | 8608 | { |
| 8609 | int seg; |
Josef Bacik | a1b75f7 | 2011-04-08 15:51:18 +0000 | [diff] [blame] | 8610 | int i; |
Chris Mason | 5a5f79b | 2010-05-26 21:33:37 -0400 | [diff] [blame] | 8611 | unsigned blocksize_mask = root->sectorsize - 1; |
| 8612 | ssize_t retval = -EINVAL; |
Chris Mason | 5a5f79b | 2010-05-26 21:33:37 -0400 | [diff] [blame] | 8613 | |
| 8614 | if (offset & blocksize_mask) |
| 8615 | goto out; |
| 8616 | |
Al Viro | 28060d5 | 2014-03-22 05:15:17 -0400 | [diff] [blame] | 8617 | if (iov_iter_alignment(iter) & blocksize_mask) |
| 8618 | goto out; |
Josef Bacik | a1b75f7 | 2011-04-08 15:51:18 +0000 | [diff] [blame] | 8619 | |
Al Viro | 28060d5 | 2014-03-22 05:15:17 -0400 | [diff] [blame] | 8620 | /* If this is a write we don't need to check anymore */ |
Omar Sandoval | 6f67376 | 2015-03-16 04:33:52 -0700 | [diff] [blame] | 8621 | if (iov_iter_rw(iter) == WRITE) |
Al Viro | 28060d5 | 2014-03-22 05:15:17 -0400 | [diff] [blame] | 8622 | return 0; |
| 8623 | /* |
| 8624 | * Check to make sure we don't have duplicate iov_base's in this |
| 8625 | * iovec, if so return EINVAL, otherwise we'll get csum errors |
| 8626 | * when reading back. |
| 8627 | */ |
| 8628 | for (seg = 0; seg < iter->nr_segs; seg++) { |
| 8629 | for (i = seg + 1; i < iter->nr_segs; i++) { |
| 8630 | if (iter->iov[seg].iov_base == iter->iov[i].iov_base) |
Josef Bacik | a1b75f7 | 2011-04-08 15:51:18 +0000 | [diff] [blame] | 8631 | goto out; |
| 8632 | } |
Chris Mason | 5a5f79b | 2010-05-26 21:33:37 -0400 | [diff] [blame] | 8633 | } |
| 8634 | retval = 0; |
| 8635 | out: |
| 8636 | return retval; |
| 8637 | } |
Josef Bacik | eb838e7 | 2012-07-31 16:28:48 -0400 | [diff] [blame] | 8638 | |
Christoph Hellwig | c8b8e32 | 2016-04-07 08:51:58 -0700 | [diff] [blame] | 8639 | 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] | 8640 | { |
Josef Bacik | 4b46fce | 2010-05-23 11:00:55 -0400 | [diff] [blame] | 8641 | struct file *file = iocb->ki_filp; |
| 8642 | struct inode *inode = file->f_mapping->host; |
chandan | 50745b0 | 2015-08-28 21:10:13 +0530 | [diff] [blame] | 8643 | struct btrfs_root *root = BTRFS_I(inode)->root; |
| 8644 | struct btrfs_dio_data dio_data = { 0 }; |
Christoph Hellwig | c8b8e32 | 2016-04-07 08:51:58 -0700 | [diff] [blame] | 8645 | loff_t offset = iocb->ki_pos; |
Miao Xie | 0934856 | 2013-02-07 10:12:07 +0000 | [diff] [blame] | 8646 | size_t count = 0; |
Miao Xie | 2e60a51 | 2013-02-08 07:01:08 +0000 | [diff] [blame] | 8647 | int flags = 0; |
Miao Xie | 38851cc | 2013-02-08 07:04:11 +0000 | [diff] [blame] | 8648 | bool wakeup = true; |
| 8649 | bool relock = false; |
Miao Xie | 0934856 | 2013-02-07 10:12:07 +0000 | [diff] [blame] | 8650 | ssize_t ret; |
Josef Bacik | 4b46fce | 2010-05-23 11:00:55 -0400 | [diff] [blame] | 8651 | |
Omar Sandoval | 6f67376 | 2015-03-16 04:33:52 -0700 | [diff] [blame] | 8652 | if (check_direct_IO(BTRFS_I(inode)->root, iocb, iter, offset)) |
Chris Mason | 5a5f79b | 2010-05-26 21:33:37 -0400 | [diff] [blame] | 8653 | return 0; |
Chris Mason | 5a5f79b | 2010-05-26 21:33:37 -0400 | [diff] [blame] | 8654 | |
Jens Axboe | fe0f07d | 2015-04-15 17:05:48 -0600 | [diff] [blame] | 8655 | inode_dio_begin(inode); |
Peter Zijlstra | 4e857c5 | 2014-03-17 18:06:10 +0100 | [diff] [blame] | 8656 | smp_mb__after_atomic(); |
Miao Xie | 38851cc | 2013-02-08 07:04:11 +0000 | [diff] [blame] | 8657 | |
Josef Bacik | 0e267c4 | 2013-07-02 10:38:02 -0400 | [diff] [blame] | 8658 | /* |
Miao Xie | 41bd9ca | 2014-03-06 13:54:57 +0800 | [diff] [blame] | 8659 | * The generic stuff only does filemap_write_and_wait_range, which |
| 8660 | * isn't enough if we've written compressed pages to this area, so |
| 8661 | * we need to flush the dirty pages again to make absolutely sure |
| 8662 | * that any outstanding dirty pages are on disk. |
Josef Bacik | 0e267c4 | 2013-07-02 10:38:02 -0400 | [diff] [blame] | 8663 | */ |
Al Viro | a6cbcd4 | 2014-03-04 22:38:00 -0500 | [diff] [blame] | 8664 | count = iov_iter_count(iter); |
Miao Xie | 41bd9ca | 2014-03-06 13:54:57 +0800 | [diff] [blame] | 8665 | if (test_bit(BTRFS_INODE_HAS_ASYNC_EXTENT, |
| 8666 | &BTRFS_I(inode)->runtime_flags)) |
Wang Shilong | 9a025a0 | 2014-07-17 11:44:13 +0800 | [diff] [blame] | 8667 | filemap_fdatawrite_range(inode->i_mapping, offset, |
| 8668 | offset + count - 1); |
Josef Bacik | 0e267c4 | 2013-07-02 10:38:02 -0400 | [diff] [blame] | 8669 | |
Omar Sandoval | 6f67376 | 2015-03-16 04:33:52 -0700 | [diff] [blame] | 8670 | if (iov_iter_rw(iter) == WRITE) { |
Miao Xie | 38851cc | 2013-02-08 07:04:11 +0000 | [diff] [blame] | 8671 | /* |
| 8672 | * If the write DIO is beyond the EOF, we need update |
| 8673 | * the isize, but it is protected by i_mutex. So we can |
| 8674 | * not unlock the i_mutex at this case. |
| 8675 | */ |
| 8676 | if (offset + count <= inode->i_size) { |
Al Viro | 5955102 | 2016-01-22 15:40:57 -0500 | [diff] [blame] | 8677 | inode_unlock(inode); |
Miao Xie | 38851cc | 2013-02-08 07:04:11 +0000 | [diff] [blame] | 8678 | relock = true; |
| 8679 | } |
Qu Wenruo | 7cf5b97 | 2015-09-08 17:25:55 +0800 | [diff] [blame] | 8680 | ret = btrfs_delalloc_reserve_space(inode, offset, count); |
Miao Xie | 0934856 | 2013-02-07 10:12:07 +0000 | [diff] [blame] | 8681 | if (ret) |
Miao Xie | 38851cc | 2013-02-08 07:04:11 +0000 | [diff] [blame] | 8682 | goto out; |
chandan | 50745b0 | 2015-08-28 21:10:13 +0530 | [diff] [blame] | 8683 | dio_data.outstanding_extents = div64_u64(count + |
Josef Bacik | e1cbbfa | 2015-03-17 10:52:28 -0400 | [diff] [blame] | 8684 | BTRFS_MAX_EXTENT_SIZE - 1, |
| 8685 | BTRFS_MAX_EXTENT_SIZE); |
| 8686 | |
| 8687 | /* |
| 8688 | * We need to know how many extents we reserved so that we can |
| 8689 | * do the accounting properly if we go over the number we |
| 8690 | * originally calculated. Abuse current->journal_info for this. |
| 8691 | */ |
chandan | 50745b0 | 2015-08-28 21:10:13 +0530 | [diff] [blame] | 8692 | dio_data.reserve = round_up(count, root->sectorsize); |
Filipe Manana | f28a492 | 2015-12-08 19:23:20 +0000 | [diff] [blame] | 8693 | dio_data.unsubmitted_oe_range_start = (u64)offset; |
| 8694 | dio_data.unsubmitted_oe_range_end = (u64)offset; |
chandan | 50745b0 | 2015-08-28 21:10:13 +0530 | [diff] [blame] | 8695 | current->journal_info = &dio_data; |
David Sterba | ee39b43 | 2014-09-30 01:33:33 +0200 | [diff] [blame] | 8696 | } else if (test_bit(BTRFS_INODE_READDIO_NEED_LOCK, |
| 8697 | &BTRFS_I(inode)->runtime_flags)) { |
Jens Axboe | fe0f07d | 2015-04-15 17:05:48 -0600 | [diff] [blame] | 8698 | inode_dio_end(inode); |
Miao Xie | 38851cc | 2013-02-08 07:04:11 +0000 | [diff] [blame] | 8699 | flags = DIO_LOCKING | DIO_SKIP_HOLES; |
| 8700 | wakeup = false; |
Miao Xie | 0934856 | 2013-02-07 10:12:07 +0000 | [diff] [blame] | 8701 | } |
| 8702 | |
Omar Sandoval | 17f8c84 | 2015-03-16 04:33:50 -0700 | [diff] [blame] | 8703 | ret = __blockdev_direct_IO(iocb, inode, |
| 8704 | BTRFS_I(inode)->root->fs_info->fs_devices->latest_bdev, |
Christoph Hellwig | c8b8e32 | 2016-04-07 08:51:58 -0700 | [diff] [blame] | 8705 | iter, btrfs_get_blocks_direct, NULL, |
Omar Sandoval | 17f8c84 | 2015-03-16 04:33:50 -0700 | [diff] [blame] | 8706 | btrfs_submit_direct, flags); |
Omar Sandoval | 6f67376 | 2015-03-16 04:33:52 -0700 | [diff] [blame] | 8707 | if (iov_iter_rw(iter) == WRITE) { |
Josef Bacik | e1cbbfa | 2015-03-17 10:52:28 -0400 | [diff] [blame] | 8708 | current->journal_info = NULL; |
Liu Bo | ddba1bf | 2015-06-17 16:59:58 +0800 | [diff] [blame] | 8709 | if (ret < 0 && ret != -EIOCBQUEUED) { |
chandan | 50745b0 | 2015-08-28 21:10:13 +0530 | [diff] [blame] | 8710 | if (dio_data.reserve) |
Qu Wenruo | 7cf5b97 | 2015-09-08 17:25:55 +0800 | [diff] [blame] | 8711 | btrfs_delalloc_release_space(inode, offset, |
| 8712 | dio_data.reserve); |
Filipe Manana | f28a492 | 2015-12-08 19:23:20 +0000 | [diff] [blame] | 8713 | /* |
| 8714 | * On error we might have left some ordered extents |
| 8715 | * without submitting corresponding bios for them, so |
| 8716 | * cleanup them up to avoid other tasks getting them |
| 8717 | * and waiting for them to complete forever. |
| 8718 | */ |
| 8719 | if (dio_data.unsubmitted_oe_range_start < |
| 8720 | dio_data.unsubmitted_oe_range_end) |
| 8721 | btrfs_endio_direct_write_update_ordered(inode, |
| 8722 | dio_data.unsubmitted_oe_range_start, |
| 8723 | dio_data.unsubmitted_oe_range_end - |
| 8724 | dio_data.unsubmitted_oe_range_start, |
| 8725 | 0); |
Liu Bo | ddba1bf | 2015-06-17 16:59:58 +0800 | [diff] [blame] | 8726 | } else if (ret >= 0 && (size_t)ret < count) |
Qu Wenruo | 7cf5b97 | 2015-09-08 17:25:55 +0800 | [diff] [blame] | 8727 | btrfs_delalloc_release_space(inode, offset, |
| 8728 | count - (size_t)ret); |
Miao Xie | 0934856 | 2013-02-07 10:12:07 +0000 | [diff] [blame] | 8729 | } |
Miao Xie | 38851cc | 2013-02-08 07:04:11 +0000 | [diff] [blame] | 8730 | out: |
Miao Xie | 2e60a51 | 2013-02-08 07:01:08 +0000 | [diff] [blame] | 8731 | if (wakeup) |
Jens Axboe | fe0f07d | 2015-04-15 17:05:48 -0600 | [diff] [blame] | 8732 | inode_dio_end(inode); |
Miao Xie | 38851cc | 2013-02-08 07:04:11 +0000 | [diff] [blame] | 8733 | if (relock) |
Al Viro | 5955102 | 2016-01-22 15:40:57 -0500 | [diff] [blame] | 8734 | inode_lock(inode); |
Miao Xie | 0934856 | 2013-02-07 10:12:07 +0000 | [diff] [blame] | 8735 | |
| 8736 | return ret; |
Chris Mason | 1643298 | 2008-04-10 10:23:21 -0400 | [diff] [blame] | 8737 | } |
| 8738 | |
Tsutomu Itoh | 05dadc0 | 2012-11-29 05:08:26 +0000 | [diff] [blame] | 8739 | #define BTRFS_FIEMAP_FLAGS (FIEMAP_FLAG_SYNC) |
| 8740 | |
Yehuda Sadeh | 1506fcc | 2009-01-21 14:39:14 -0500 | [diff] [blame] | 8741 | static int btrfs_fiemap(struct inode *inode, struct fiemap_extent_info *fieinfo, |
| 8742 | __u64 start, __u64 len) |
| 8743 | { |
Tsutomu Itoh | 05dadc0 | 2012-11-29 05:08:26 +0000 | [diff] [blame] | 8744 | int ret; |
| 8745 | |
| 8746 | ret = fiemap_check_flags(fieinfo, BTRFS_FIEMAP_FLAGS); |
| 8747 | if (ret) |
| 8748 | return ret; |
| 8749 | |
Chris Mason | ec29ed5 | 2011-02-23 16:23:20 -0500 | [diff] [blame] | 8750 | return extent_fiemap(inode, fieinfo, start, len, btrfs_get_extent_fiemap); |
Yehuda Sadeh | 1506fcc | 2009-01-21 14:39:14 -0500 | [diff] [blame] | 8751 | } |
| 8752 | |
Chris Mason | 9ebefb18 | 2007-06-15 13:50:00 -0400 | [diff] [blame] | 8753 | int btrfs_readpage(struct file *file, struct page *page) |
| 8754 | { |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 8755 | struct extent_io_tree *tree; |
| 8756 | tree = &BTRFS_I(page->mapping->host)->io_tree; |
Jan Schmidt | 8ddc7d9 | 2011-06-13 20:02:58 +0200 | [diff] [blame] | 8757 | return extent_read_full_page(tree, page, btrfs_get_extent, 0); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 8758 | } |
Chris Mason | 1832a6d | 2007-12-21 16:27:21 -0500 | [diff] [blame] | 8759 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 8760 | static int btrfs_writepage(struct page *page, struct writeback_control *wbc) |
| 8761 | { |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 8762 | struct extent_io_tree *tree; |
Josef Bacik | be7bd73 | 2015-10-22 15:05:09 -0400 | [diff] [blame] | 8763 | struct inode *inode = page->mapping->host; |
| 8764 | int ret; |
Chris Mason | b888db2 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 8765 | |
| 8766 | if (current->flags & PF_MEMALLOC) { |
| 8767 | redirty_page_for_writepage(wbc, page); |
| 8768 | unlock_page(page); |
| 8769 | return 0; |
| 8770 | } |
Josef Bacik | be7bd73 | 2015-10-22 15:05:09 -0400 | [diff] [blame] | 8771 | |
| 8772 | /* |
| 8773 | * If we are under memory pressure we will call this directly from the |
| 8774 | * VM, we need to make sure we have the inode referenced for the ordered |
| 8775 | * extent. If not just return like we didn't do anything. |
| 8776 | */ |
| 8777 | if (!igrab(inode)) { |
| 8778 | redirty_page_for_writepage(wbc, page); |
| 8779 | return AOP_WRITEPAGE_ACTIVATE; |
| 8780 | } |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 8781 | tree = &BTRFS_I(page->mapping->host)->io_tree; |
Josef Bacik | be7bd73 | 2015-10-22 15:05:09 -0400 | [diff] [blame] | 8782 | ret = extent_write_full_page(tree, page, btrfs_get_extent, wbc); |
| 8783 | btrfs_add_delayed_iput(inode); |
| 8784 | return ret; |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 8785 | } |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 8786 | |
Eric Sandeen | 48a3b63 | 2013-04-25 20:41:01 +0000 | [diff] [blame] | 8787 | static int btrfs_writepages(struct address_space *mapping, |
| 8788 | struct writeback_control *wbc) |
Chris Mason | b293f02 | 2007-11-01 19:45:34 -0400 | [diff] [blame] | 8789 | { |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 8790 | struct extent_io_tree *tree; |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 8791 | |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 8792 | tree = &BTRFS_I(mapping->host)->io_tree; |
Chris Mason | b293f02 | 2007-11-01 19:45:34 -0400 | [diff] [blame] | 8793 | return extent_writepages(tree, mapping, btrfs_get_extent, wbc); |
| 8794 | } |
| 8795 | |
Chris Mason | 3ab2fb5 | 2007-11-08 10:59:22 -0500 | [diff] [blame] | 8796 | static int |
| 8797 | btrfs_readpages(struct file *file, struct address_space *mapping, |
| 8798 | struct list_head *pages, unsigned nr_pages) |
| 8799 | { |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 8800 | struct extent_io_tree *tree; |
| 8801 | tree = &BTRFS_I(mapping->host)->io_tree; |
Chris Mason | 3ab2fb5 | 2007-11-08 10:59:22 -0500 | [diff] [blame] | 8802 | return extent_readpages(tree, mapping, pages, nr_pages, |
| 8803 | btrfs_get_extent); |
| 8804 | } |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 8805 | static int __btrfs_releasepage(struct page *page, gfp_t gfp_flags) |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 8806 | { |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 8807 | struct extent_io_tree *tree; |
| 8808 | struct extent_map_tree *map; |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 8809 | int ret; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 8810 | |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 8811 | tree = &BTRFS_I(page->mapping->host)->io_tree; |
| 8812 | map = &BTRFS_I(page->mapping->host)->extent_tree; |
Chris Mason | 70dec80 | 2008-01-29 09:59:12 -0500 | [diff] [blame] | 8813 | ret = try_release_extent_mapping(map, tree, page, gfp_flags); |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 8814 | if (ret == 1) { |
| 8815 | ClearPagePrivate(page); |
| 8816 | set_page_private(page, 0); |
Kirill A. Shutemov | 09cbfea | 2016-04-01 15:29:47 +0300 | [diff] [blame] | 8817 | put_page(page); |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 8818 | } |
| 8819 | return ret; |
| 8820 | } |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 8821 | |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 8822 | static int btrfs_releasepage(struct page *page, gfp_t gfp_flags) |
| 8823 | { |
Chris Mason | 98509cf | 2008-09-11 15:51:43 -0400 | [diff] [blame] | 8824 | if (PageWriteback(page) || PageDirty(page)) |
| 8825 | return 0; |
Yan Zheng | b335b00 | 2009-02-12 10:06:04 -0500 | [diff] [blame] | 8826 | return __btrfs_releasepage(page, gfp_flags & GFP_NOFS); |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 8827 | } |
| 8828 | |
Lukas Czerner | d47992f | 2013-05-21 23:17:23 -0400 | [diff] [blame] | 8829 | static void btrfs_invalidatepage(struct page *page, unsigned int offset, |
| 8830 | unsigned int length) |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 8831 | { |
Josef Bacik | 5fd0204 | 2012-05-02 14:00:54 -0400 | [diff] [blame] | 8832 | struct inode *inode = page->mapping->host; |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 8833 | struct extent_io_tree *tree; |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 8834 | struct btrfs_ordered_extent *ordered; |
Josef Bacik | 2ac55d4 | 2010-02-03 19:33:23 +0000 | [diff] [blame] | 8835 | struct extent_state *cached_state = NULL; |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 8836 | u64 page_start = page_offset(page); |
Kirill A. Shutemov | 09cbfea | 2016-04-01 15:29:47 +0300 | [diff] [blame] | 8837 | u64 page_end = page_start + PAGE_SIZE - 1; |
Chandan Rajendra | dbfdb6d | 2016-01-21 15:55:58 +0530 | [diff] [blame] | 8838 | u64 start; |
| 8839 | u64 end; |
Filipe David Borba Manana | 131e404 | 2013-11-19 22:29:35 +0000 | [diff] [blame] | 8840 | int inode_evicting = inode->i_state & I_FREEING; |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 8841 | |
Chris Mason | 8b62b72 | 2009-09-02 16:53:46 -0400 | [diff] [blame] | 8842 | /* |
| 8843 | * we have the page locked, so new writeback can't start, |
| 8844 | * and the dirty bit won't be cleared while we are here. |
| 8845 | * |
| 8846 | * Wait for IO on this page so that we can safely clear |
| 8847 | * the PagePrivate2 bit and do ordered accounting |
| 8848 | */ |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 8849 | wait_on_page_writeback(page); |
Chris Mason | 8b62b72 | 2009-09-02 16:53:46 -0400 | [diff] [blame] | 8850 | |
Josef Bacik | 5fd0204 | 2012-05-02 14:00:54 -0400 | [diff] [blame] | 8851 | tree = &BTRFS_I(inode)->io_tree; |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 8852 | if (offset) { |
| 8853 | btrfs_releasepage(page, GFP_NOFS); |
| 8854 | return; |
| 8855 | } |
Filipe David Borba Manana | 131e404 | 2013-11-19 22:29:35 +0000 | [diff] [blame] | 8856 | |
| 8857 | if (!inode_evicting) |
David Sterba | ff13db4 | 2015-12-03 14:30:40 +0100 | [diff] [blame] | 8858 | lock_extent_bits(tree, page_start, page_end, &cached_state); |
Chandan Rajendra | dbfdb6d | 2016-01-21 15:55:58 +0530 | [diff] [blame] | 8859 | again: |
| 8860 | start = page_start; |
| 8861 | ordered = btrfs_lookup_ordered_range(inode, start, |
| 8862 | page_end - start + 1); |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 8863 | if (ordered) { |
Chandan Rajendra | dbfdb6d | 2016-01-21 15:55:58 +0530 | [diff] [blame] | 8864 | end = min(page_end, ordered->file_offset + ordered->len - 1); |
Chris Mason | eb84ae0 | 2008-07-17 13:53:27 -0400 | [diff] [blame] | 8865 | /* |
| 8866 | * IO on this page will never be started, so we need |
| 8867 | * to account for any ordered extents now |
| 8868 | */ |
Filipe David Borba Manana | 131e404 | 2013-11-19 22:29:35 +0000 | [diff] [blame] | 8869 | if (!inode_evicting) |
Chandan Rajendra | dbfdb6d | 2016-01-21 15:55:58 +0530 | [diff] [blame] | 8870 | clear_extent_bit(tree, start, end, |
Filipe David Borba Manana | 131e404 | 2013-11-19 22:29:35 +0000 | [diff] [blame] | 8871 | EXTENT_DIRTY | EXTENT_DELALLOC | |
| 8872 | EXTENT_LOCKED | EXTENT_DO_ACCOUNTING | |
| 8873 | EXTENT_DEFRAG, 1, 0, &cached_state, |
| 8874 | GFP_NOFS); |
Chris Mason | 8b62b72 | 2009-09-02 16:53:46 -0400 | [diff] [blame] | 8875 | /* |
| 8876 | * whoever cleared the private bit is responsible |
| 8877 | * for the finish_ordered_io |
| 8878 | */ |
Josef Bacik | 77cef2e | 2013-08-29 13:57:21 -0400 | [diff] [blame] | 8879 | if (TestClearPagePrivate2(page)) { |
| 8880 | struct btrfs_ordered_inode_tree *tree; |
| 8881 | u64 new_len; |
| 8882 | |
| 8883 | tree = &BTRFS_I(inode)->ordered_tree; |
| 8884 | |
| 8885 | spin_lock_irq(&tree->lock); |
| 8886 | set_bit(BTRFS_ORDERED_TRUNCATED, &ordered->flags); |
Chandan Rajendra | dbfdb6d | 2016-01-21 15:55:58 +0530 | [diff] [blame] | 8887 | new_len = start - ordered->file_offset; |
Josef Bacik | 77cef2e | 2013-08-29 13:57:21 -0400 | [diff] [blame] | 8888 | if (new_len < ordered->truncated_len) |
| 8889 | ordered->truncated_len = new_len; |
| 8890 | spin_unlock_irq(&tree->lock); |
| 8891 | |
| 8892 | if (btrfs_dec_test_ordered_pending(inode, &ordered, |
Chandan Rajendra | dbfdb6d | 2016-01-21 15:55:58 +0530 | [diff] [blame] | 8893 | start, |
| 8894 | end - start + 1, 1)) |
Josef Bacik | 77cef2e | 2013-08-29 13:57:21 -0400 | [diff] [blame] | 8895 | btrfs_finish_ordered_io(ordered); |
Chris Mason | 8b62b72 | 2009-09-02 16:53:46 -0400 | [diff] [blame] | 8896 | } |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 8897 | btrfs_put_ordered_extent(ordered); |
Filipe David Borba Manana | 131e404 | 2013-11-19 22:29:35 +0000 | [diff] [blame] | 8898 | if (!inode_evicting) { |
| 8899 | cached_state = NULL; |
Chandan Rajendra | dbfdb6d | 2016-01-21 15:55:58 +0530 | [diff] [blame] | 8900 | lock_extent_bits(tree, start, end, |
Filipe David Borba Manana | 131e404 | 2013-11-19 22:29:35 +0000 | [diff] [blame] | 8901 | &cached_state); |
| 8902 | } |
Chandan Rajendra | dbfdb6d | 2016-01-21 15:55:58 +0530 | [diff] [blame] | 8903 | |
| 8904 | start = end + 1; |
| 8905 | if (start < page_end) |
| 8906 | goto again; |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 8907 | } |
Filipe David Borba Manana | 131e404 | 2013-11-19 22:29:35 +0000 | [diff] [blame] | 8908 | |
Qu Wenruo | b9d0b38 | 2015-09-29 10:35:16 +0800 | [diff] [blame] | 8909 | /* |
| 8910 | * Qgroup reserved space handler |
| 8911 | * Page here will be either |
| 8912 | * 1) Already written to disk |
| 8913 | * In this case, its reserved space is released from data rsv map |
| 8914 | * and will be freed by delayed_ref handler finally. |
| 8915 | * So even we call qgroup_free_data(), it won't decrease reserved |
| 8916 | * space. |
| 8917 | * 2) Not written to disk |
| 8918 | * This means the reserved space should be freed here. |
| 8919 | */ |
Kirill A. Shutemov | 09cbfea | 2016-04-01 15:29:47 +0300 | [diff] [blame] | 8920 | btrfs_qgroup_free_data(inode, page_start, PAGE_SIZE); |
Filipe David Borba Manana | 131e404 | 2013-11-19 22:29:35 +0000 | [diff] [blame] | 8921 | if (!inode_evicting) { |
| 8922 | clear_extent_bit(tree, page_start, page_end, |
| 8923 | EXTENT_LOCKED | EXTENT_DIRTY | |
| 8924 | EXTENT_DELALLOC | EXTENT_DO_ACCOUNTING | |
| 8925 | EXTENT_DEFRAG, 1, 1, |
| 8926 | &cached_state, GFP_NOFS); |
| 8927 | |
| 8928 | __btrfs_releasepage(page, GFP_NOFS); |
| 8929 | } |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 8930 | |
Chris Mason | 4a09675 | 2008-07-21 10:29:44 -0400 | [diff] [blame] | 8931 | ClearPageChecked(page); |
Chris Mason | 9ad6b7b | 2008-04-18 16:11:30 -0400 | [diff] [blame] | 8932 | if (PagePrivate(page)) { |
Chris Mason | 9ad6b7b | 2008-04-18 16:11:30 -0400 | [diff] [blame] | 8933 | ClearPagePrivate(page); |
| 8934 | set_page_private(page, 0); |
Kirill A. Shutemov | 09cbfea | 2016-04-01 15:29:47 +0300 | [diff] [blame] | 8935 | put_page(page); |
Chris Mason | 9ad6b7b | 2008-04-18 16:11:30 -0400 | [diff] [blame] | 8936 | } |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 8937 | } |
| 8938 | |
Chris Mason | 9ebefb18 | 2007-06-15 13:50:00 -0400 | [diff] [blame] | 8939 | /* |
| 8940 | * btrfs_page_mkwrite() is not allowed to change the file size as it gets |
| 8941 | * called from a page fault handler when a page is first dirtied. Hence we must |
| 8942 | * be careful to check for EOF conditions here. We set the page up correctly |
| 8943 | * for a written page which means we get ENOSPC checking when writing into |
| 8944 | * holes and correct delalloc and unwritten extent mapping on filesystems that |
| 8945 | * support these features. |
| 8946 | * |
| 8947 | * We are not allowed to take the i_mutex here so we have to play games to |
| 8948 | * protect against truncate races as the page could now be beyond EOF. Because |
| 8949 | * vmtruncate() writes the inode size before removing pages, once we have the |
| 8950 | * page lock we can determine safely if the page is beyond EOF. If it is not |
| 8951 | * beyond EOF, then the page is guaranteed safe against truncation until we |
| 8952 | * unlock the page. |
| 8953 | */ |
Nick Piggin | c2ec175 | 2009-03-31 15:23:21 -0700 | [diff] [blame] | 8954 | 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] | 8955 | { |
Nick Piggin | c2ec175 | 2009-03-31 15:23:21 -0700 | [diff] [blame] | 8956 | struct page *page = vmf->page; |
Al Viro | 496ad9a | 2013-01-23 17:07:38 -0500 | [diff] [blame] | 8957 | struct inode *inode = file_inode(vma->vm_file); |
Chris Mason | 1832a6d | 2007-12-21 16:27:21 -0500 | [diff] [blame] | 8958 | struct btrfs_root *root = BTRFS_I(inode)->root; |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 8959 | struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree; |
| 8960 | struct btrfs_ordered_extent *ordered; |
Josef Bacik | 2ac55d4 | 2010-02-03 19:33:23 +0000 | [diff] [blame] | 8961 | struct extent_state *cached_state = NULL; |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 8962 | char *kaddr; |
| 8963 | unsigned long zero_start; |
Chris Mason | 9ebefb18 | 2007-06-15 13:50:00 -0400 | [diff] [blame] | 8964 | loff_t size; |
Chris Mason | 1832a6d | 2007-12-21 16:27:21 -0500 | [diff] [blame] | 8965 | int ret; |
Chris Mason | 9998eb7 | 2012-01-25 13:47:40 -0500 | [diff] [blame] | 8966 | int reserved = 0; |
Chandan Rajendra | d0b7da88 | 2016-01-21 15:55:57 +0530 | [diff] [blame] | 8967 | u64 reserved_space; |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 8968 | u64 page_start; |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 8969 | u64 page_end; |
Chandan Rajendra | d0b7da88 | 2016-01-21 15:55:57 +0530 | [diff] [blame] | 8970 | u64 end; |
| 8971 | |
Kirill A. Shutemov | 09cbfea | 2016-04-01 15:29:47 +0300 | [diff] [blame] | 8972 | reserved_space = PAGE_SIZE; |
Chris Mason | 9ebefb18 | 2007-06-15 13:50:00 -0400 | [diff] [blame] | 8973 | |
Jan Kara | b2b5ef5 | 2012-06-12 16:20:45 +0200 | [diff] [blame] | 8974 | sb_start_pagefault(inode->i_sb); |
Qu Wenruo | df48063 | 2015-09-08 17:25:54 +0800 | [diff] [blame] | 8975 | page_start = page_offset(page); |
Kirill A. Shutemov | 09cbfea | 2016-04-01 15:29:47 +0300 | [diff] [blame] | 8976 | page_end = page_start + PAGE_SIZE - 1; |
Chandan Rajendra | d0b7da88 | 2016-01-21 15:55:57 +0530 | [diff] [blame] | 8977 | end = page_end; |
Qu Wenruo | df48063 | 2015-09-08 17:25:54 +0800 | [diff] [blame] | 8978 | |
Chandan Rajendra | d0b7da88 | 2016-01-21 15:55:57 +0530 | [diff] [blame] | 8979 | /* |
| 8980 | * Reserving delalloc space after obtaining the page lock can lead to |
| 8981 | * deadlock. For example, if a dirty page is locked by this function |
| 8982 | * and the call to btrfs_delalloc_reserve_space() ends up triggering |
| 8983 | * dirty page write out, then the btrfs_writepage() function could |
| 8984 | * end up waiting indefinitely to get a lock on the page currently |
| 8985 | * being processed by btrfs_page_mkwrite() function. |
| 8986 | */ |
Qu Wenruo | 7cf5b97 | 2015-09-08 17:25:55 +0800 | [diff] [blame] | 8987 | ret = btrfs_delalloc_reserve_space(inode, page_start, |
Chandan Rajendra | d0b7da88 | 2016-01-21 15:55:57 +0530 | [diff] [blame] | 8988 | reserved_space); |
Chris Mason | 9998eb7 | 2012-01-25 13:47:40 -0500 | [diff] [blame] | 8989 | if (!ret) { |
Josef Bacik | e41f941 | 2012-03-26 09:46:47 -0400 | [diff] [blame] | 8990 | ret = file_update_time(vma->vm_file); |
Chris Mason | 9998eb7 | 2012-01-25 13:47:40 -0500 | [diff] [blame] | 8991 | reserved = 1; |
| 8992 | } |
Nick Piggin | 56a76f8 | 2009-03-31 15:23:23 -0700 | [diff] [blame] | 8993 | if (ret) { |
| 8994 | if (ret == -ENOMEM) |
| 8995 | ret = VM_FAULT_OOM; |
| 8996 | else /* -ENOSPC, -EIO, etc */ |
| 8997 | ret = VM_FAULT_SIGBUS; |
Chris Mason | 9998eb7 | 2012-01-25 13:47:40 -0500 | [diff] [blame] | 8998 | if (reserved) |
| 8999 | goto out; |
| 9000 | goto out_noreserve; |
Nick Piggin | 56a76f8 | 2009-03-31 15:23:23 -0700 | [diff] [blame] | 9001 | } |
Chris Mason | 1832a6d | 2007-12-21 16:27:21 -0500 | [diff] [blame] | 9002 | |
Nick Piggin | 56a76f8 | 2009-03-31 15:23:23 -0700 | [diff] [blame] | 9003 | ret = VM_FAULT_NOPAGE; /* make the VM retry the fault */ |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 9004 | again: |
Chris Mason | 9ebefb18 | 2007-06-15 13:50:00 -0400 | [diff] [blame] | 9005 | lock_page(page); |
Chris Mason | 9ebefb18 | 2007-06-15 13:50:00 -0400 | [diff] [blame] | 9006 | size = i_size_read(inode); |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 9007 | |
Chris Mason | 9ebefb18 | 2007-06-15 13:50:00 -0400 | [diff] [blame] | 9008 | if ((page->mapping != inode->i_mapping) || |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 9009 | (page_start >= size)) { |
Chris Mason | 9ebefb18 | 2007-06-15 13:50:00 -0400 | [diff] [blame] | 9010 | /* page got truncated out from underneath us */ |
| 9011 | goto out_unlock; |
| 9012 | } |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 9013 | wait_on_page_writeback(page); |
| 9014 | |
David Sterba | ff13db4 | 2015-12-03 14:30:40 +0100 | [diff] [blame] | 9015 | lock_extent_bits(io_tree, page_start, page_end, &cached_state); |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 9016 | set_page_extent_mapped(page); |
| 9017 | |
Chris Mason | eb84ae0 | 2008-07-17 13:53:27 -0400 | [diff] [blame] | 9018 | /* |
| 9019 | * we can't set the delalloc bits if there are pending ordered |
| 9020 | * extents. Drop our locks and wait for them to finish |
| 9021 | */ |
Chandan Rajendra | d0b7da88 | 2016-01-21 15:55:57 +0530 | [diff] [blame] | 9022 | ordered = btrfs_lookup_ordered_range(inode, page_start, page_end); |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 9023 | if (ordered) { |
Josef Bacik | 2ac55d4 | 2010-02-03 19:33:23 +0000 | [diff] [blame] | 9024 | unlock_extent_cached(io_tree, page_start, page_end, |
| 9025 | &cached_state, GFP_NOFS); |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 9026 | unlock_page(page); |
Chris Mason | eb84ae0 | 2008-07-17 13:53:27 -0400 | [diff] [blame] | 9027 | btrfs_start_ordered_extent(inode, ordered, 1); |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 9028 | btrfs_put_ordered_extent(ordered); |
| 9029 | goto again; |
| 9030 | } |
| 9031 | |
Kirill A. Shutemov | 09cbfea | 2016-04-01 15:29:47 +0300 | [diff] [blame] | 9032 | if (page->index == ((size - 1) >> PAGE_SHIFT)) { |
Chandan Rajendra | d0b7da88 | 2016-01-21 15:55:57 +0530 | [diff] [blame] | 9033 | reserved_space = round_up(size - page_start, root->sectorsize); |
Kirill A. Shutemov | 09cbfea | 2016-04-01 15:29:47 +0300 | [diff] [blame] | 9034 | if (reserved_space < PAGE_SIZE) { |
Chandan Rajendra | d0b7da88 | 2016-01-21 15:55:57 +0530 | [diff] [blame] | 9035 | end = page_start + reserved_space - 1; |
| 9036 | spin_lock(&BTRFS_I(inode)->lock); |
| 9037 | BTRFS_I(inode)->outstanding_extents++; |
| 9038 | spin_unlock(&BTRFS_I(inode)->lock); |
| 9039 | btrfs_delalloc_release_space(inode, page_start, |
Kirill A. Shutemov | 09cbfea | 2016-04-01 15:29:47 +0300 | [diff] [blame] | 9040 | PAGE_SIZE - reserved_space); |
Chandan Rajendra | d0b7da88 | 2016-01-21 15:55:57 +0530 | [diff] [blame] | 9041 | } |
| 9042 | } |
| 9043 | |
Josef Bacik | fbf1908 | 2009-10-01 17:10:23 -0400 | [diff] [blame] | 9044 | /* |
| 9045 | * XXX - page_mkwrite gets called every time the page is dirtied, even |
| 9046 | * if it was already dirty, so for space accounting reasons we need to |
| 9047 | * clear any delalloc bits for the range we are fixing to save. There |
| 9048 | * is probably a better way to do this, but for now keep consistent with |
| 9049 | * prepare_pages in the normal write path. |
| 9050 | */ |
Chandan Rajendra | d0b7da88 | 2016-01-21 15:55:57 +0530 | [diff] [blame] | 9051 | clear_extent_bit(&BTRFS_I(inode)->io_tree, page_start, end, |
Liu Bo | 9e8a4a8 | 2012-09-05 19:10:51 -0600 | [diff] [blame] | 9052 | EXTENT_DIRTY | EXTENT_DELALLOC | |
| 9053 | EXTENT_DO_ACCOUNTING | EXTENT_DEFRAG, |
Josef Bacik | 2ac55d4 | 2010-02-03 19:33:23 +0000 | [diff] [blame] | 9054 | 0, 0, &cached_state, GFP_NOFS); |
Josef Bacik | fbf1908 | 2009-10-01 17:10:23 -0400 | [diff] [blame] | 9055 | |
Chandan Rajendra | d0b7da88 | 2016-01-21 15:55:57 +0530 | [diff] [blame] | 9056 | ret = btrfs_set_extent_delalloc(inode, page_start, end, |
Josef Bacik | 2ac55d4 | 2010-02-03 19:33:23 +0000 | [diff] [blame] | 9057 | &cached_state); |
Josef Bacik | 9ed74f2 | 2009-09-11 16:12:44 -0400 | [diff] [blame] | 9058 | if (ret) { |
Josef Bacik | 2ac55d4 | 2010-02-03 19:33:23 +0000 | [diff] [blame] | 9059 | unlock_extent_cached(io_tree, page_start, page_end, |
| 9060 | &cached_state, GFP_NOFS); |
Josef Bacik | 9ed74f2 | 2009-09-11 16:12:44 -0400 | [diff] [blame] | 9061 | ret = VM_FAULT_SIGBUS; |
| 9062 | goto out_unlock; |
| 9063 | } |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 9064 | ret = 0; |
Chris Mason | 9ebefb18 | 2007-06-15 13:50:00 -0400 | [diff] [blame] | 9065 | |
| 9066 | /* page is wholly or partially inside EOF */ |
Kirill A. Shutemov | 09cbfea | 2016-04-01 15:29:47 +0300 | [diff] [blame] | 9067 | if (page_start + PAGE_SIZE > size) |
| 9068 | zero_start = size & ~PAGE_MASK; |
Chris Mason | 9ebefb18 | 2007-06-15 13:50:00 -0400 | [diff] [blame] | 9069 | else |
Kirill A. Shutemov | 09cbfea | 2016-04-01 15:29:47 +0300 | [diff] [blame] | 9070 | zero_start = PAGE_SIZE; |
Chris Mason | 9ebefb18 | 2007-06-15 13:50:00 -0400 | [diff] [blame] | 9071 | |
Kirill A. Shutemov | 09cbfea | 2016-04-01 15:29:47 +0300 | [diff] [blame] | 9072 | if (zero_start != PAGE_SIZE) { |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 9073 | kaddr = kmap(page); |
Kirill A. Shutemov | 09cbfea | 2016-04-01 15:29:47 +0300 | [diff] [blame] | 9074 | memset(kaddr + zero_start, 0, PAGE_SIZE - zero_start); |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 9075 | flush_dcache_page(page); |
| 9076 | kunmap(page); |
| 9077 | } |
Chris Mason | 247e743 | 2008-07-17 12:53:51 -0400 | [diff] [blame] | 9078 | ClearPageChecked(page); |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 9079 | set_page_dirty(page); |
Chris Mason | 50a9b21 | 2009-09-11 12:33:12 -0400 | [diff] [blame] | 9080 | SetPageUptodate(page); |
Chris Mason | 5a3f23d | 2009-03-31 13:27:11 -0400 | [diff] [blame] | 9081 | |
Chris Mason | 257c62e | 2009-10-13 13:21:08 -0400 | [diff] [blame] | 9082 | BTRFS_I(inode)->last_trans = root->fs_info->generation; |
| 9083 | BTRFS_I(inode)->last_sub_trans = BTRFS_I(inode)->root->log_transid; |
Liu Bo | 46d8bc3 | 2012-08-29 01:07:55 -0600 | [diff] [blame] | 9084 | 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] | 9085 | |
Josef Bacik | 2ac55d4 | 2010-02-03 19:33:23 +0000 | [diff] [blame] | 9086 | 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] | 9087 | |
| 9088 | out_unlock: |
Jan Kara | b2b5ef5 | 2012-06-12 16:20:45 +0200 | [diff] [blame] | 9089 | if (!ret) { |
| 9090 | sb_end_pagefault(inode->i_sb); |
Chris Mason | 50a9b21 | 2009-09-11 12:33:12 -0400 | [diff] [blame] | 9091 | return VM_FAULT_LOCKED; |
Jan Kara | b2b5ef5 | 2012-06-12 16:20:45 +0200 | [diff] [blame] | 9092 | } |
Chris Mason | 9ebefb18 | 2007-06-15 13:50:00 -0400 | [diff] [blame] | 9093 | unlock_page(page); |
Chris Mason | 1832a6d | 2007-12-21 16:27:21 -0500 | [diff] [blame] | 9094 | out: |
Chandan Rajendra | d0b7da88 | 2016-01-21 15:55:57 +0530 | [diff] [blame] | 9095 | btrfs_delalloc_release_space(inode, page_start, reserved_space); |
Chris Mason | 9998eb7 | 2012-01-25 13:47:40 -0500 | [diff] [blame] | 9096 | out_noreserve: |
Jan Kara | b2b5ef5 | 2012-06-12 16:20:45 +0200 | [diff] [blame] | 9097 | sb_end_pagefault(inode->i_sb); |
Chris Mason | 9ebefb18 | 2007-06-15 13:50:00 -0400 | [diff] [blame] | 9098 | return ret; |
| 9099 | } |
| 9100 | |
Josef Bacik | a41ad39 | 2011-01-31 15:30:16 -0500 | [diff] [blame] | 9101 | static int btrfs_truncate(struct inode *inode) |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 9102 | { |
| 9103 | struct btrfs_root *root = BTRFS_I(inode)->root; |
Josef Bacik | fcb80c2 | 2011-05-03 10:40:22 -0400 | [diff] [blame] | 9104 | struct btrfs_block_rsv *rsv; |
Josef Bacik | a71754f | 2013-06-17 17:14:39 -0400 | [diff] [blame] | 9105 | int ret = 0; |
Josef Bacik | 3893e33 | 2011-01-31 16:03:11 -0500 | [diff] [blame] | 9106 | int err = 0; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 9107 | struct btrfs_trans_handle *trans; |
Chris Mason | dbe674a | 2008-07-17 12:54:05 -0400 | [diff] [blame] | 9108 | u64 mask = root->sectorsize - 1; |
Josef Bacik | 0712718 | 2011-08-19 10:29:59 -0400 | [diff] [blame] | 9109 | u64 min_size = btrfs_calc_trunc_metadata_size(root, 1); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 9110 | |
Josef Bacik | 0ef8b72 | 2013-10-25 16:13:35 -0400 | [diff] [blame] | 9111 | ret = btrfs_wait_ordered_range(inode, inode->i_size & (~mask), |
| 9112 | (u64)-1); |
| 9113 | if (ret) |
| 9114 | return ret; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 9115 | |
Josef Bacik | fcb80c2 | 2011-05-03 10:40:22 -0400 | [diff] [blame] | 9116 | /* |
Nicholas D Steeves | 0132761 | 2016-05-19 21:18:45 -0400 | [diff] [blame] | 9117 | * 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] | 9118 | * 3 things going on here |
| 9119 | * |
| 9120 | * 1) We need to reserve space for our orphan item and the space to |
| 9121 | * delete our orphan item. Lord knows we don't want to have a dangling |
| 9122 | * orphan item because we didn't reserve space to remove it. |
| 9123 | * |
| 9124 | * 2) We need to reserve space to update our inode. |
| 9125 | * |
| 9126 | * 3) We need to have something to cache all the space that is going to |
| 9127 | * be free'd up by the truncate operation, but also have some slack |
| 9128 | * space reserved in case it uses space during the truncate (thank you |
| 9129 | * very much snapshotting). |
| 9130 | * |
Nicholas D Steeves | 0132761 | 2016-05-19 21:18:45 -0400 | [diff] [blame] | 9131 | * 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] | 9132 | * 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] | 9133 | * 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] | 9134 | * doesn't end up using space reserved for updating the inode or |
| 9135 | * removing the orphan item. We also need to be able to stop the |
| 9136 | * transaction and start a new one, which means we need to be able to |
| 9137 | * update the inode several times, and we have no idea of knowing how |
| 9138 | * 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] | 9139 | * 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] | 9140 | * Then there is the orphan item, which does indeed need to be held on |
| 9141 | * to for the whole operation, and we need nobody to touch this reserved |
| 9142 | * space except the orphan code. |
| 9143 | * |
| 9144 | * So that leaves us with |
| 9145 | * |
| 9146 | * 1) root->orphan_block_rsv - for the orphan deletion. |
| 9147 | * 2) rsv - for the truncate reservation, which we will steal from the |
| 9148 | * transaction reservation. |
| 9149 | * 3) fs_info->trans_block_rsv - this will have 1 items worth left for |
| 9150 | * updating the inode. |
| 9151 | */ |
Miao Xie | 66d8f3d | 2012-09-06 04:02:28 -0600 | [diff] [blame] | 9152 | rsv = btrfs_alloc_block_rsv(root, BTRFS_BLOCK_RSV_TEMP); |
Josef Bacik | fcb80c2 | 2011-05-03 10:40:22 -0400 | [diff] [blame] | 9153 | if (!rsv) |
| 9154 | return -ENOMEM; |
Josef Bacik | 4a33854 | 2011-08-29 11:01:31 -0400 | [diff] [blame] | 9155 | rsv->size = min_size; |
Josef Bacik | ca7e70f | 2012-08-27 17:48:15 -0400 | [diff] [blame] | 9156 | rsv->failfast = 1; |
Josef Bacik | f0cd846 | 2011-03-04 14:37:08 -0500 | [diff] [blame] | 9157 | |
Josef Bacik | 907cbce | 2011-08-08 13:46:15 -0400 | [diff] [blame] | 9158 | /* |
Josef Bacik | 0712718 | 2011-08-19 10:29:59 -0400 | [diff] [blame] | 9159 | * 1 for the truncate slack space |
Josef Bacik | 907cbce | 2011-08-08 13:46:15 -0400 | [diff] [blame] | 9160 | * 1 for updating the inode. |
| 9161 | */ |
Josef Bacik | f3fe820 | 2013-01-07 17:03:21 -0500 | [diff] [blame] | 9162 | trans = btrfs_start_transaction(root, 2); |
Josef Bacik | fcb80c2 | 2011-05-03 10:40:22 -0400 | [diff] [blame] | 9163 | if (IS_ERR(trans)) { |
| 9164 | err = PTR_ERR(trans); |
| 9165 | goto out; |
| 9166 | } |
Josef Bacik | f0cd846 | 2011-03-04 14:37:08 -0500 | [diff] [blame] | 9167 | |
Josef Bacik | 907cbce | 2011-08-08 13:46:15 -0400 | [diff] [blame] | 9168 | /* Migrate the slack space for the truncate to our reserve */ |
| 9169 | ret = btrfs_block_rsv_migrate(&root->fs_info->trans_block_rsv, rsv, |
Josef Bacik | 25d609f | 2016-03-25 13:25:48 -0400 | [diff] [blame] | 9170 | min_size, 0); |
Josef Bacik | fcb80c2 | 2011-05-03 10:40:22 -0400 | [diff] [blame] | 9171 | BUG_ON(ret); |
Josef Bacik | f0cd846 | 2011-03-04 14:37:08 -0500 | [diff] [blame] | 9172 | |
Chris Mason | 5a3f23d | 2009-03-31 13:27:11 -0400 | [diff] [blame] | 9173 | /* |
Josef Bacik | 5dc562c | 2012-08-17 13:14:17 -0400 | [diff] [blame] | 9174 | * So if we truncate and then write and fsync we normally would just |
| 9175 | * write the extents that changed, which is a problem if we need to |
| 9176 | * first truncate that entire inode. So set this flag so we write out |
| 9177 | * all of the extents in the inode to the sync log so we're completely |
| 9178 | * safe. |
| 9179 | */ |
| 9180 | set_bit(BTRFS_INODE_NEEDS_FULL_SYNC, &BTRFS_I(inode)->runtime_flags); |
Josef Bacik | ca7e70f | 2012-08-27 17:48:15 -0400 | [diff] [blame] | 9181 | trans->block_rsv = rsv; |
Josef Bacik | 5dc562c | 2012-08-17 13:14:17 -0400 | [diff] [blame] | 9182 | |
Yan, Zheng | 8082510 | 2009-11-12 09:35:36 +0000 | [diff] [blame] | 9183 | while (1) { |
| 9184 | ret = btrfs_truncate_inode_items(trans, root, inode, |
| 9185 | inode->i_size, |
| 9186 | BTRFS_EXTENT_DATA_KEY); |
Chris Mason | 28ed134 | 2014-12-17 09:41:04 -0800 | [diff] [blame] | 9187 | if (ret != -ENOSPC && ret != -EAGAIN) { |
Josef Bacik | 3893e33 | 2011-01-31 16:03:11 -0500 | [diff] [blame] | 9188 | err = ret; |
Yan, Zheng | 8082510 | 2009-11-12 09:35:36 +0000 | [diff] [blame] | 9189 | break; |
Josef Bacik | 3893e33 | 2011-01-31 16:03:11 -0500 | [diff] [blame] | 9190 | } |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 9191 | |
Josef Bacik | fcb80c2 | 2011-05-03 10:40:22 -0400 | [diff] [blame] | 9192 | trans->block_rsv = &root->fs_info->trans_block_rsv; |
Yan, Zheng | 8082510 | 2009-11-12 09:35:36 +0000 | [diff] [blame] | 9193 | ret = btrfs_update_inode(trans, root, inode); |
Josef Bacik | 3893e33 | 2011-01-31 16:03:11 -0500 | [diff] [blame] | 9194 | if (ret) { |
| 9195 | err = ret; |
| 9196 | break; |
| 9197 | } |
Josef Bacik | ca7e70f | 2012-08-27 17:48:15 -0400 | [diff] [blame] | 9198 | |
Yan, Zheng | 8082510 | 2009-11-12 09:35:36 +0000 | [diff] [blame] | 9199 | btrfs_end_transaction(trans, root); |
Liu Bo | b53d3f5 | 2012-11-14 14:34:34 +0000 | [diff] [blame] | 9200 | btrfs_btree_balance_dirty(root); |
Josef Bacik | ca7e70f | 2012-08-27 17:48:15 -0400 | [diff] [blame] | 9201 | |
| 9202 | trans = btrfs_start_transaction(root, 2); |
| 9203 | if (IS_ERR(trans)) { |
| 9204 | ret = err = PTR_ERR(trans); |
| 9205 | trans = NULL; |
| 9206 | break; |
| 9207 | } |
| 9208 | |
| 9209 | ret = btrfs_block_rsv_migrate(&root->fs_info->trans_block_rsv, |
Josef Bacik | 25d609f | 2016-03-25 13:25:48 -0400 | [diff] [blame] | 9210 | rsv, min_size, 0); |
Josef Bacik | ca7e70f | 2012-08-27 17:48:15 -0400 | [diff] [blame] | 9211 | BUG_ON(ret); /* shouldn't happen */ |
| 9212 | trans->block_rsv = rsv; |
Yan, Zheng | 8082510 | 2009-11-12 09:35:36 +0000 | [diff] [blame] | 9213 | } |
| 9214 | |
| 9215 | if (ret == 0 && inode->i_nlink > 0) { |
Josef Bacik | fcb80c2 | 2011-05-03 10:40:22 -0400 | [diff] [blame] | 9216 | trans->block_rsv = root->orphan_block_rsv; |
Yan, Zheng | 8082510 | 2009-11-12 09:35:36 +0000 | [diff] [blame] | 9217 | ret = btrfs_orphan_del(trans, inode); |
Josef Bacik | 3893e33 | 2011-01-31 16:03:11 -0500 | [diff] [blame] | 9218 | if (ret) |
| 9219 | err = ret; |
Yan, Zheng | 8082510 | 2009-11-12 09:35:36 +0000 | [diff] [blame] | 9220 | } |
| 9221 | |
Chris Mason | 917c16b | 2011-11-08 14:49:59 -0500 | [diff] [blame] | 9222 | if (trans) { |
| 9223 | trans->block_rsv = &root->fs_info->trans_block_rsv; |
| 9224 | ret = btrfs_update_inode(trans, root, inode); |
| 9225 | if (ret && !err) |
| 9226 | err = ret; |
Josef Bacik | 7b12876 | 2008-07-24 12:17:14 -0400 | [diff] [blame] | 9227 | |
Josef Bacik | 7ad85bb | 2012-01-12 19:10:12 -0500 | [diff] [blame] | 9228 | ret = btrfs_end_transaction(trans, root); |
Liu Bo | b53d3f5 | 2012-11-14 14:34:34 +0000 | [diff] [blame] | 9229 | btrfs_btree_balance_dirty(root); |
Chris Mason | 917c16b | 2011-11-08 14:49:59 -0500 | [diff] [blame] | 9230 | } |
Josef Bacik | fcb80c2 | 2011-05-03 10:40:22 -0400 | [diff] [blame] | 9231 | out: |
| 9232 | btrfs_free_block_rsv(root, rsv); |
| 9233 | |
Josef Bacik | 3893e33 | 2011-01-31 16:03:11 -0500 | [diff] [blame] | 9234 | if (ret && !err) |
| 9235 | err = ret; |
Josef Bacik | a41ad39 | 2011-01-31 15:30:16 -0500 | [diff] [blame] | 9236 | |
Josef Bacik | 3893e33 | 2011-01-31 16:03:11 -0500 | [diff] [blame] | 9237 | return err; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 9238 | } |
| 9239 | |
Sven Wegener | 3b96362 | 2008-06-09 21:57:42 -0400 | [diff] [blame] | 9240 | /* |
Chris Mason | d352ac6 | 2008-09-29 15:18:18 -0400 | [diff] [blame] | 9241 | * create a new subvolume directory/inode (helper for the ioctl). |
| 9242 | */ |
Yan Zheng | d2fb343 | 2008-12-11 16:30:39 -0500 | [diff] [blame] | 9243 | int btrfs_create_subvol_root(struct btrfs_trans_handle *trans, |
Filipe David Borba Manana | 6354192 | 2014-01-07 11:47:46 +0000 | [diff] [blame] | 9244 | struct btrfs_root *new_root, |
| 9245 | struct btrfs_root *parent_root, |
| 9246 | u64 new_dirid) |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 9247 | { |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 9248 | struct inode *inode; |
Yan, Zheng | 76dda93 | 2009-09-21 16:00:26 -0400 | [diff] [blame] | 9249 | int err; |
Chris Mason | 00e4e6b | 2008-08-05 11:18:09 -0400 | [diff] [blame] | 9250 | u64 index = 0; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 9251 | |
Florian Albrechtskirchinger | 12fc9d0 | 2012-02-10 22:15:54 +0100 | [diff] [blame] | 9252 | inode = btrfs_new_inode(trans, new_root, NULL, "..", 2, |
| 9253 | new_dirid, new_dirid, |
| 9254 | S_IFDIR | (~current_umask() & S_IRWXUGO), |
| 9255 | &index); |
Chris Mason | 54aa1f4 | 2007-06-22 14:16:25 -0400 | [diff] [blame] | 9256 | if (IS_ERR(inode)) |
Christoph Hellwig | f46b5a6 | 2008-06-11 21:53:53 -0400 | [diff] [blame] | 9257 | return PTR_ERR(inode); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 9258 | inode->i_op = &btrfs_dir_inode_operations; |
| 9259 | inode->i_fop = &btrfs_dir_file_operations; |
| 9260 | |
Miklos Szeredi | bfe8684 | 2011-10-28 14:13:29 +0200 | [diff] [blame] | 9261 | set_nlink(inode, 1); |
Chris Mason | dbe674a | 2008-07-17 12:54:05 -0400 | [diff] [blame] | 9262 | btrfs_i_size_write(inode, 0); |
Chris Mason | b0d5d10 | 2014-09-08 13:08:51 -0700 | [diff] [blame] | 9263 | unlock_new_inode(inode); |
Sven Wegener | 3b96362 | 2008-06-09 21:57:42 -0400 | [diff] [blame] | 9264 | |
Filipe David Borba Manana | 6354192 | 2014-01-07 11:47:46 +0000 | [diff] [blame] | 9265 | err = btrfs_subvol_inherit_props(trans, new_root, parent_root); |
| 9266 | if (err) |
| 9267 | btrfs_err(new_root->fs_info, |
David Sterba | 351fd35 | 2014-05-15 16:48:20 +0200 | [diff] [blame] | 9268 | "error inheriting subvolume %llu properties: %d", |
Filipe David Borba Manana | 6354192 | 2014-01-07 11:47:46 +0000 | [diff] [blame] | 9269 | new_root->root_key.objectid, err); |
| 9270 | |
Yan, Zheng | 76dda93 | 2009-09-21 16:00:26 -0400 | [diff] [blame] | 9271 | err = btrfs_update_inode(trans, new_root, inode); |
Christoph Hellwig | cb8e709 | 2008-10-09 13:39:39 -0400 | [diff] [blame] | 9272 | |
Yan, Zheng | 76dda93 | 2009-09-21 16:00:26 -0400 | [diff] [blame] | 9273 | iput(inode); |
Mark Fasheh | ce59897 | 2011-07-26 11:32:23 -0700 | [diff] [blame] | 9274 | return err; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 9275 | } |
| 9276 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 9277 | struct inode *btrfs_alloc_inode(struct super_block *sb) |
| 9278 | { |
| 9279 | struct btrfs_inode *ei; |
Yan, Zheng | 2ead6ae | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 9280 | struct inode *inode; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 9281 | |
| 9282 | ei = kmem_cache_alloc(btrfs_inode_cachep, GFP_NOFS); |
| 9283 | if (!ei) |
| 9284 | return NULL; |
Yan, Zheng | 2ead6ae | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 9285 | |
| 9286 | ei->root = NULL; |
Yan, Zheng | 2ead6ae | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 9287 | ei->generation = 0; |
Josef Bacik | 15ee9bc | 2007-08-10 16:22:09 -0400 | [diff] [blame] | 9288 | ei->last_trans = 0; |
Chris Mason | 257c62e | 2009-10-13 13:21:08 -0400 | [diff] [blame] | 9289 | ei->last_sub_trans = 0; |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 9290 | ei->logged_trans = 0; |
Yan, Zheng | 2ead6ae | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 9291 | ei->delalloc_bytes = 0; |
Wang Shilong | 47059d9 | 2014-07-03 18:22:07 +0800 | [diff] [blame] | 9292 | ei->defrag_bytes = 0; |
Yan, Zheng | 2ead6ae | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 9293 | ei->disk_i_size = 0; |
| 9294 | ei->flags = 0; |
Josef Bacik | 7709cde | 2011-08-04 10:25:02 -0400 | [diff] [blame] | 9295 | ei->csum_bytes = 0; |
Yan, Zheng | 2ead6ae | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 9296 | ei->index_cnt = (u64)-1; |
Miao Xie | 67de117 | 2013-12-26 13:07:06 +0800 | [diff] [blame] | 9297 | ei->dir_index = 0; |
Yan, Zheng | 2ead6ae | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 9298 | ei->last_unlink_trans = 0; |
Liu Bo | 46d8bc3 | 2012-08-29 01:07:55 -0600 | [diff] [blame] | 9299 | ei->last_log_commit = 0; |
David Sterba | 8089fe6 | 2015-11-19 14:15:51 +0100 | [diff] [blame] | 9300 | ei->delayed_iput_count = 0; |
Yan, Zheng | 2ead6ae | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 9301 | |
Josef Bacik | 9e0baf6 | 2011-07-15 15:16:44 +0000 | [diff] [blame] | 9302 | spin_lock_init(&ei->lock); |
| 9303 | ei->outstanding_extents = 0; |
| 9304 | ei->reserved_extents = 0; |
Yan, Zheng | 2ead6ae | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 9305 | |
Josef Bacik | 72ac3c0 | 2012-05-23 14:13:11 -0400 | [diff] [blame] | 9306 | ei->runtime_flags = 0; |
Li Zefan | 261507a0 | 2010-12-17 14:21:50 +0800 | [diff] [blame] | 9307 | ei->force_compress = BTRFS_COMPRESS_NONE; |
Yan, Zheng | 2ead6ae | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 9308 | |
Miao Xie | 16cdcec | 2011-04-22 18:12:22 +0800 | [diff] [blame] | 9309 | ei->delayed_node = NULL; |
| 9310 | |
chandan r | 9cc97d6 | 2012-07-04 12:48:07 +0530 | [diff] [blame] | 9311 | ei->i_otime.tv_sec = 0; |
| 9312 | ei->i_otime.tv_nsec = 0; |
| 9313 | |
Yan, Zheng | 2ead6ae | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 9314 | inode = &ei->vfs_inode; |
David Sterba | a8067e0 | 2011-04-21 00:34:43 +0200 | [diff] [blame] | 9315 | extent_map_tree_init(&ei->extent_tree); |
David Sterba | f993c88 | 2011-04-20 23:35:57 +0200 | [diff] [blame] | 9316 | extent_io_tree_init(&ei->io_tree, &inode->i_data); |
| 9317 | extent_io_tree_init(&ei->io_failure_tree, &inode->i_data); |
Josef Bacik | 0b32f4b | 2012-03-13 09:38:00 -0400 | [diff] [blame] | 9318 | ei->io_tree.track_uptodate = 1; |
| 9319 | ei->io_failure_tree.track_uptodate = 1; |
Josef Bacik | b812ce2 | 2012-11-16 13:56:32 -0500 | [diff] [blame] | 9320 | atomic_set(&ei->sync_writers, 0); |
Yan, Zheng | 2ead6ae | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 9321 | mutex_init(&ei->log_mutex); |
Josef Bacik | f248679 | 2012-01-13 12:09:22 -0500 | [diff] [blame] | 9322 | mutex_init(&ei->delalloc_mutex); |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 9323 | btrfs_ordered_inode_tree_init(&ei->ordered_tree); |
Yan, Zheng | 2ead6ae | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 9324 | INIT_LIST_HEAD(&ei->delalloc_inodes); |
David Sterba | 8089fe6 | 2015-11-19 14:15:51 +0100 | [diff] [blame] | 9325 | INIT_LIST_HEAD(&ei->delayed_iput); |
Yan, Zheng | 2ead6ae | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 9326 | RB_CLEAR_NODE(&ei->rb_node); |
Filipe Manana | 5f9a8a5 | 2016-05-12 13:53:36 +0100 | [diff] [blame] | 9327 | init_rwsem(&ei->dio_sem); |
Yan, Zheng | 2ead6ae | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 9328 | |
| 9329 | return inode; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 9330 | } |
| 9331 | |
Josef Bacik | aaedb55 | 2013-10-11 14:44:09 -0400 | [diff] [blame] | 9332 | #ifdef CONFIG_BTRFS_FS_RUN_SANITY_TESTS |
| 9333 | void btrfs_test_destroy_inode(struct inode *inode) |
| 9334 | { |
| 9335 | btrfs_drop_extent_cache(inode, 0, (u64)-1, 0); |
| 9336 | kmem_cache_free(btrfs_inode_cachep, BTRFS_I(inode)); |
| 9337 | } |
| 9338 | #endif |
| 9339 | |
Nick Piggin | fa0d7e3d | 2011-01-07 17:49:49 +1100 | [diff] [blame] | 9340 | static void btrfs_i_callback(struct rcu_head *head) |
| 9341 | { |
| 9342 | struct inode *inode = container_of(head, struct inode, i_rcu); |
Nick Piggin | fa0d7e3d | 2011-01-07 17:49:49 +1100 | [diff] [blame] | 9343 | kmem_cache_free(btrfs_inode_cachep, BTRFS_I(inode)); |
| 9344 | } |
| 9345 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 9346 | void btrfs_destroy_inode(struct inode *inode) |
| 9347 | { |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 9348 | struct btrfs_ordered_extent *ordered; |
Chris Mason | 5a3f23d | 2009-03-31 13:27:11 -0400 | [diff] [blame] | 9349 | struct btrfs_root *root = BTRFS_I(inode)->root; |
| 9350 | |
Al Viro | b3d9b7a | 2012-06-09 13:51:19 -0400 | [diff] [blame] | 9351 | WARN_ON(!hlist_empty(&inode->i_dentry)); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 9352 | WARN_ON(inode->i_data.nrpages); |
Josef Bacik | 9e0baf6 | 2011-07-15 15:16:44 +0000 | [diff] [blame] | 9353 | WARN_ON(BTRFS_I(inode)->outstanding_extents); |
| 9354 | WARN_ON(BTRFS_I(inode)->reserved_extents); |
Josef Bacik | 7709cde | 2011-08-04 10:25:02 -0400 | [diff] [blame] | 9355 | WARN_ON(BTRFS_I(inode)->delalloc_bytes); |
| 9356 | WARN_ON(BTRFS_I(inode)->csum_bytes); |
Wang Shilong | 47059d9 | 2014-07-03 18:22:07 +0800 | [diff] [blame] | 9357 | WARN_ON(BTRFS_I(inode)->defrag_bytes); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 9358 | |
Chris Mason | 5a3f23d | 2009-03-31 13:27:11 -0400 | [diff] [blame] | 9359 | /* |
Josef Bacik | a6dbd42 | 2009-11-11 15:53:34 -0500 | [diff] [blame] | 9360 | * This can happen where we create an inode, but somebody else also |
| 9361 | * created the same inode and we need to destroy the one we already |
| 9362 | * created. |
| 9363 | */ |
| 9364 | if (!root) |
| 9365 | goto free; |
| 9366 | |
Josef Bacik | 8a35d95 | 2012-05-23 14:26:42 -0400 | [diff] [blame] | 9367 | if (test_bit(BTRFS_INODE_HAS_ORPHAN_ITEM, |
| 9368 | &BTRFS_I(inode)->runtime_flags)) { |
Simon Kirby | c2cf52e | 2013-03-19 22:41:23 +0000 | [diff] [blame] | 9369 | btrfs_info(root->fs_info, "inode %llu still on the orphan list", |
Geert Uytterhoeven | c1c9ff7 | 2013-08-20 13:20:07 +0200 | [diff] [blame] | 9370 | btrfs_ino(inode)); |
Josef Bacik | 8a35d95 | 2012-05-23 14:26:42 -0400 | [diff] [blame] | 9371 | atomic_dec(&root->orphan_inodes); |
Josef Bacik | 7b12876 | 2008-07-24 12:17:14 -0400 | [diff] [blame] | 9372 | } |
Josef Bacik | 7b12876 | 2008-07-24 12:17:14 -0400 | [diff] [blame] | 9373 | |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 9374 | while (1) { |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 9375 | ordered = btrfs_lookup_first_ordered_extent(inode, (u64)-1); |
| 9376 | if (!ordered) |
| 9377 | break; |
| 9378 | else { |
Simon Kirby | c2cf52e | 2013-03-19 22:41:23 +0000 | [diff] [blame] | 9379 | btrfs_err(root->fs_info, "found ordered extent %llu %llu on inode cleanup", |
Geert Uytterhoeven | c1c9ff7 | 2013-08-20 13:20:07 +0200 | [diff] [blame] | 9380 | ordered->file_offset, ordered->len); |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 9381 | btrfs_remove_ordered_extent(inode, ordered); |
| 9382 | btrfs_put_ordered_extent(ordered); |
| 9383 | btrfs_put_ordered_extent(ordered); |
| 9384 | } |
| 9385 | } |
Qu Wenruo | 56fa9d0 | 2015-10-13 09:53:10 +0800 | [diff] [blame] | 9386 | btrfs_qgroup_check_reserved_leak(inode); |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 9387 | inode_tree_del(inode); |
Zheng Yan | 5b21f2e | 2008-09-26 10:05:38 -0400 | [diff] [blame] | 9388 | btrfs_drop_extent_cache(inode, 0, (u64)-1, 0); |
Josef Bacik | a6dbd42 | 2009-11-11 15:53:34 -0500 | [diff] [blame] | 9389 | free: |
Nick Piggin | fa0d7e3d | 2011-01-07 17:49:49 +1100 | [diff] [blame] | 9390 | call_rcu(&inode->i_rcu, btrfs_i_callback); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 9391 | } |
| 9392 | |
Al Viro | 45321ac | 2010-06-07 13:43:19 -0400 | [diff] [blame] | 9393 | int btrfs_drop_inode(struct inode *inode) |
Yan, Zheng | 76dda93 | 2009-09-21 16:00:26 -0400 | [diff] [blame] | 9394 | { |
| 9395 | struct btrfs_root *root = BTRFS_I(inode)->root; |
Al Viro | 45321ac | 2010-06-07 13:43:19 -0400 | [diff] [blame] | 9396 | |
Naohiro Aota | 6379ef9 | 2013-06-06 09:56:34 +0000 | [diff] [blame] | 9397 | if (root == NULL) |
| 9398 | return 1; |
| 9399 | |
Liu Bo | fa6ac87 | 2013-02-20 14:10:23 +0000 | [diff] [blame] | 9400 | /* the snap/subvol tree is on deleting */ |
Stefan Behrens | 69e9c6c | 2013-09-05 16:58:43 +0200 | [diff] [blame] | 9401 | if (btrfs_root_refs(&root->root_item) == 0) |
Al Viro | 45321ac | 2010-06-07 13:43:19 -0400 | [diff] [blame] | 9402 | return 1; |
Yan, Zheng | 76dda93 | 2009-09-21 16:00:26 -0400 | [diff] [blame] | 9403 | else |
Al Viro | 45321ac | 2010-06-07 13:43:19 -0400 | [diff] [blame] | 9404 | return generic_drop_inode(inode); |
Yan, Zheng | 76dda93 | 2009-09-21 16:00:26 -0400 | [diff] [blame] | 9405 | } |
| 9406 | |
Sven Wegener | 0ee0fda | 2008-07-30 16:54:26 -0400 | [diff] [blame] | 9407 | static void init_once(void *foo) |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 9408 | { |
| 9409 | struct btrfs_inode *ei = (struct btrfs_inode *) foo; |
| 9410 | |
| 9411 | inode_init_once(&ei->vfs_inode); |
| 9412 | } |
| 9413 | |
| 9414 | void btrfs_destroy_cachep(void) |
| 9415 | { |
Kirill A. Shutemov | 8c0a853 | 2012-09-26 11:33:07 +1000 | [diff] [blame] | 9416 | /* |
| 9417 | * Make sure all delayed rcu free inodes are flushed before we |
| 9418 | * destroy cache. |
| 9419 | */ |
| 9420 | rcu_barrier(); |
Kinglong Mee | 5598e90 | 2016-01-29 21:36:35 +0800 | [diff] [blame] | 9421 | kmem_cache_destroy(btrfs_inode_cachep); |
| 9422 | kmem_cache_destroy(btrfs_trans_handle_cachep); |
| 9423 | kmem_cache_destroy(btrfs_transaction_cachep); |
| 9424 | kmem_cache_destroy(btrfs_path_cachep); |
| 9425 | kmem_cache_destroy(btrfs_free_space_cachep); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 9426 | } |
| 9427 | |
| 9428 | int btrfs_init_cachep(void) |
| 9429 | { |
David Sterba | 837e197 | 2012-09-07 03:00:48 -0600 | [diff] [blame] | 9430 | btrfs_inode_cachep = kmem_cache_create("btrfs_inode", |
Christoph Hellwig | 9601e3f | 2009-04-13 15:33:09 +0200 | [diff] [blame] | 9431 | sizeof(struct btrfs_inode), 0, |
Vladimir Davydov | 5d09705 | 2016-01-14 15:18:21 -0800 | [diff] [blame] | 9432 | SLAB_RECLAIM_ACCOUNT | SLAB_MEM_SPREAD | SLAB_ACCOUNT, |
| 9433 | init_once); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 9434 | if (!btrfs_inode_cachep) |
| 9435 | goto fail; |
Christoph Hellwig | 9601e3f | 2009-04-13 15:33:09 +0200 | [diff] [blame] | 9436 | |
David Sterba | 837e197 | 2012-09-07 03:00:48 -0600 | [diff] [blame] | 9437 | btrfs_trans_handle_cachep = kmem_cache_create("btrfs_trans_handle", |
Christoph Hellwig | 9601e3f | 2009-04-13 15:33:09 +0200 | [diff] [blame] | 9438 | sizeof(struct btrfs_trans_handle), 0, |
Nikolay Borisov | fba4b69 | 2016-06-23 21:17:08 +0300 | [diff] [blame] | 9439 | SLAB_TEMPORARY | SLAB_MEM_SPREAD, NULL); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 9440 | if (!btrfs_trans_handle_cachep) |
| 9441 | goto fail; |
Christoph Hellwig | 9601e3f | 2009-04-13 15:33:09 +0200 | [diff] [blame] | 9442 | |
David Sterba | 837e197 | 2012-09-07 03:00:48 -0600 | [diff] [blame] | 9443 | btrfs_transaction_cachep = kmem_cache_create("btrfs_transaction", |
Christoph Hellwig | 9601e3f | 2009-04-13 15:33:09 +0200 | [diff] [blame] | 9444 | sizeof(struct btrfs_transaction), 0, |
Nikolay Borisov | fba4b69 | 2016-06-23 21:17:08 +0300 | [diff] [blame] | 9445 | SLAB_TEMPORARY | SLAB_MEM_SPREAD, NULL); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 9446 | if (!btrfs_transaction_cachep) |
| 9447 | goto fail; |
Christoph Hellwig | 9601e3f | 2009-04-13 15:33:09 +0200 | [diff] [blame] | 9448 | |
David Sterba | 837e197 | 2012-09-07 03:00:48 -0600 | [diff] [blame] | 9449 | btrfs_path_cachep = kmem_cache_create("btrfs_path", |
Christoph Hellwig | 9601e3f | 2009-04-13 15:33:09 +0200 | [diff] [blame] | 9450 | sizeof(struct btrfs_path), 0, |
Nikolay Borisov | fba4b69 | 2016-06-23 21:17:08 +0300 | [diff] [blame] | 9451 | SLAB_MEM_SPREAD, NULL); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 9452 | if (!btrfs_path_cachep) |
| 9453 | goto fail; |
Christoph Hellwig | 9601e3f | 2009-04-13 15:33:09 +0200 | [diff] [blame] | 9454 | |
David Sterba | 837e197 | 2012-09-07 03:00:48 -0600 | [diff] [blame] | 9455 | btrfs_free_space_cachep = kmem_cache_create("btrfs_free_space", |
Josef Bacik | dc89e98 | 2011-01-28 17:05:48 -0500 | [diff] [blame] | 9456 | sizeof(struct btrfs_free_space), 0, |
Nikolay Borisov | fba4b69 | 2016-06-23 21:17:08 +0300 | [diff] [blame] | 9457 | SLAB_MEM_SPREAD, NULL); |
Josef Bacik | dc89e98 | 2011-01-28 17:05:48 -0500 | [diff] [blame] | 9458 | if (!btrfs_free_space_cachep) |
| 9459 | goto fail; |
| 9460 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 9461 | return 0; |
| 9462 | fail: |
| 9463 | btrfs_destroy_cachep(); |
| 9464 | return -ENOMEM; |
| 9465 | } |
| 9466 | |
| 9467 | static int btrfs_getattr(struct vfsmount *mnt, |
| 9468 | struct dentry *dentry, struct kstat *stat) |
| 9469 | { |
Miao Xie | df0af1a | 2013-01-29 10:11:59 +0000 | [diff] [blame] | 9470 | u64 delalloc_bytes; |
David Howells | 2b0143b | 2015-03-17 22:25:59 +0000 | [diff] [blame] | 9471 | struct inode *inode = d_inode(dentry); |
David Sterba | fadc0d8 | 2011-11-20 07:33:38 -0500 | [diff] [blame] | 9472 | u32 blocksize = inode->i_sb->s_blocksize; |
| 9473 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 9474 | generic_fillattr(inode, stat); |
Al Viro | 0ee5dc6 | 2011-07-07 15:44:25 -0400 | [diff] [blame] | 9475 | stat->dev = BTRFS_I(inode)->root->anon_dev; |
Miao Xie | df0af1a | 2013-01-29 10:11:59 +0000 | [diff] [blame] | 9476 | |
| 9477 | spin_lock(&BTRFS_I(inode)->lock); |
| 9478 | delalloc_bytes = BTRFS_I(inode)->delalloc_bytes; |
| 9479 | spin_unlock(&BTRFS_I(inode)->lock); |
David Sterba | fadc0d8 | 2011-11-20 07:33:38 -0500 | [diff] [blame] | 9480 | stat->blocks = (ALIGN(inode_get_bytes(inode), blocksize) + |
Miao Xie | df0af1a | 2013-01-29 10:11:59 +0000 | [diff] [blame] | 9481 | ALIGN(delalloc_bytes, blocksize)) >> 9; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 9482 | return 0; |
| 9483 | } |
| 9484 | |
Dan Fuhry | cdd1fed | 2016-03-17 15:23:38 +0100 | [diff] [blame] | 9485 | static int btrfs_rename_exchange(struct inode *old_dir, |
| 9486 | struct dentry *old_dentry, |
| 9487 | struct inode *new_dir, |
| 9488 | struct dentry *new_dentry) |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 9489 | { |
| 9490 | struct btrfs_trans_handle *trans; |
| 9491 | struct btrfs_root *root = BTRFS_I(old_dir)->root; |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 9492 | struct btrfs_root *dest = BTRFS_I(new_dir)->root; |
Dan Fuhry | cdd1fed | 2016-03-17 15:23:38 +0100 | [diff] [blame] | 9493 | struct inode *new_inode = new_dentry->d_inode; |
| 9494 | struct inode *old_inode = old_dentry->d_inode; |
| 9495 | struct timespec ctime = CURRENT_TIME; |
| 9496 | struct dentry *parent; |
| 9497 | u64 old_ino = btrfs_ino(old_inode); |
| 9498 | u64 new_ino = btrfs_ino(new_inode); |
| 9499 | u64 old_idx = 0; |
| 9500 | u64 new_idx = 0; |
| 9501 | u64 root_objectid; |
| 9502 | int ret; |
Filipe Manana | 86e8aa0 | 2016-05-05 02:02:27 +0100 | [diff] [blame] | 9503 | bool root_log_pinned = false; |
| 9504 | bool dest_log_pinned = false; |
Dan Fuhry | cdd1fed | 2016-03-17 15:23:38 +0100 | [diff] [blame] | 9505 | |
| 9506 | /* we only allow rename subvolume link between subvolumes */ |
| 9507 | if (old_ino != BTRFS_FIRST_FREE_OBJECTID && root != dest) |
| 9508 | return -EXDEV; |
| 9509 | |
| 9510 | /* close the race window with snapshot create/destroy ioctl */ |
| 9511 | if (old_ino == BTRFS_FIRST_FREE_OBJECTID) |
| 9512 | down_read(&root->fs_info->subvol_sem); |
| 9513 | if (new_ino == BTRFS_FIRST_FREE_OBJECTID) |
| 9514 | down_read(&dest->fs_info->subvol_sem); |
| 9515 | |
| 9516 | /* |
| 9517 | * We want to reserve the absolute worst case amount of items. So if |
| 9518 | * both inodes are subvols and we need to unlink them then that would |
| 9519 | * require 4 item modifications, but if they are both normal inodes it |
| 9520 | * would require 5 item modifications, so we'll assume their normal |
| 9521 | * inodes. So 5 * 2 is 10, plus 2 for the new links, so 12 total items |
| 9522 | * should cover the worst case number of items we'll modify. |
| 9523 | */ |
| 9524 | trans = btrfs_start_transaction(root, 12); |
| 9525 | if (IS_ERR(trans)) { |
| 9526 | ret = PTR_ERR(trans); |
| 9527 | goto out_notrans; |
| 9528 | } |
| 9529 | |
| 9530 | /* |
| 9531 | * We need to find a free sequence number both in the source and |
| 9532 | * in the destination directory for the exchange. |
| 9533 | */ |
| 9534 | ret = btrfs_set_inode_index(new_dir, &old_idx); |
| 9535 | if (ret) |
| 9536 | goto out_fail; |
| 9537 | ret = btrfs_set_inode_index(old_dir, &new_idx); |
| 9538 | if (ret) |
| 9539 | goto out_fail; |
| 9540 | |
| 9541 | BTRFS_I(old_inode)->dir_index = 0ULL; |
| 9542 | BTRFS_I(new_inode)->dir_index = 0ULL; |
| 9543 | |
| 9544 | /* Reference for the source. */ |
| 9545 | if (old_ino == BTRFS_FIRST_FREE_OBJECTID) { |
| 9546 | /* force full log commit if subvolume involved. */ |
| 9547 | btrfs_set_log_full_commit(root->fs_info, trans); |
| 9548 | } else { |
Filipe Manana | 376e5a5 | 2016-05-05 02:08:56 +0100 | [diff] [blame] | 9549 | btrfs_pin_log_trans(root); |
| 9550 | root_log_pinned = true; |
Dan Fuhry | cdd1fed | 2016-03-17 15:23:38 +0100 | [diff] [blame] | 9551 | ret = btrfs_insert_inode_ref(trans, dest, |
| 9552 | new_dentry->d_name.name, |
| 9553 | new_dentry->d_name.len, |
| 9554 | old_ino, |
| 9555 | btrfs_ino(new_dir), old_idx); |
| 9556 | if (ret) |
| 9557 | goto out_fail; |
Dan Fuhry | cdd1fed | 2016-03-17 15:23:38 +0100 | [diff] [blame] | 9558 | } |
| 9559 | |
| 9560 | /* And now for the dest. */ |
| 9561 | if (new_ino == BTRFS_FIRST_FREE_OBJECTID) { |
| 9562 | /* force full log commit if subvolume involved. */ |
| 9563 | btrfs_set_log_full_commit(dest->fs_info, trans); |
| 9564 | } else { |
Filipe Manana | 376e5a5 | 2016-05-05 02:08:56 +0100 | [diff] [blame] | 9565 | btrfs_pin_log_trans(dest); |
| 9566 | dest_log_pinned = true; |
Dan Fuhry | cdd1fed | 2016-03-17 15:23:38 +0100 | [diff] [blame] | 9567 | ret = btrfs_insert_inode_ref(trans, root, |
| 9568 | old_dentry->d_name.name, |
| 9569 | old_dentry->d_name.len, |
| 9570 | new_ino, |
| 9571 | btrfs_ino(old_dir), new_idx); |
| 9572 | if (ret) |
| 9573 | goto out_fail; |
Dan Fuhry | cdd1fed | 2016-03-17 15:23:38 +0100 | [diff] [blame] | 9574 | } |
| 9575 | |
| 9576 | /* Update inode version and ctime/mtime. */ |
| 9577 | inode_inc_iversion(old_dir); |
| 9578 | inode_inc_iversion(new_dir); |
| 9579 | inode_inc_iversion(old_inode); |
| 9580 | inode_inc_iversion(new_inode); |
| 9581 | old_dir->i_ctime = old_dir->i_mtime = ctime; |
| 9582 | new_dir->i_ctime = new_dir->i_mtime = ctime; |
| 9583 | old_inode->i_ctime = ctime; |
| 9584 | new_inode->i_ctime = ctime; |
| 9585 | |
| 9586 | if (old_dentry->d_parent != new_dentry->d_parent) { |
| 9587 | btrfs_record_unlink_dir(trans, old_dir, old_inode, 1); |
| 9588 | btrfs_record_unlink_dir(trans, new_dir, new_inode, 1); |
| 9589 | } |
| 9590 | |
| 9591 | /* src is a subvolume */ |
| 9592 | if (old_ino == BTRFS_FIRST_FREE_OBJECTID) { |
| 9593 | root_objectid = BTRFS_I(old_inode)->root->root_key.objectid; |
| 9594 | ret = btrfs_unlink_subvol(trans, root, old_dir, |
| 9595 | root_objectid, |
| 9596 | old_dentry->d_name.name, |
| 9597 | old_dentry->d_name.len); |
| 9598 | } else { /* src is an inode */ |
| 9599 | ret = __btrfs_unlink_inode(trans, root, old_dir, |
| 9600 | old_dentry->d_inode, |
| 9601 | old_dentry->d_name.name, |
| 9602 | old_dentry->d_name.len); |
| 9603 | if (!ret) |
| 9604 | ret = btrfs_update_inode(trans, root, old_inode); |
| 9605 | } |
| 9606 | if (ret) { |
Jeff Mahoney | 6664283 | 2016-06-10 18:19:25 -0400 | [diff] [blame] | 9607 | btrfs_abort_transaction(trans, ret); |
Dan Fuhry | cdd1fed | 2016-03-17 15:23:38 +0100 | [diff] [blame] | 9608 | goto out_fail; |
| 9609 | } |
| 9610 | |
| 9611 | /* dest is a subvolume */ |
| 9612 | if (new_ino == BTRFS_FIRST_FREE_OBJECTID) { |
| 9613 | root_objectid = BTRFS_I(new_inode)->root->root_key.objectid; |
| 9614 | ret = btrfs_unlink_subvol(trans, dest, new_dir, |
| 9615 | root_objectid, |
| 9616 | new_dentry->d_name.name, |
| 9617 | new_dentry->d_name.len); |
| 9618 | } else { /* dest is an inode */ |
| 9619 | ret = __btrfs_unlink_inode(trans, dest, new_dir, |
| 9620 | new_dentry->d_inode, |
| 9621 | new_dentry->d_name.name, |
| 9622 | new_dentry->d_name.len); |
| 9623 | if (!ret) |
| 9624 | ret = btrfs_update_inode(trans, dest, new_inode); |
| 9625 | } |
| 9626 | if (ret) { |
Jeff Mahoney | 6664283 | 2016-06-10 18:19:25 -0400 | [diff] [blame] | 9627 | btrfs_abort_transaction(trans, ret); |
Dan Fuhry | cdd1fed | 2016-03-17 15:23:38 +0100 | [diff] [blame] | 9628 | goto out_fail; |
| 9629 | } |
| 9630 | |
| 9631 | ret = btrfs_add_link(trans, new_dir, old_inode, |
| 9632 | new_dentry->d_name.name, |
| 9633 | new_dentry->d_name.len, 0, old_idx); |
| 9634 | if (ret) { |
Jeff Mahoney | 6664283 | 2016-06-10 18:19:25 -0400 | [diff] [blame] | 9635 | btrfs_abort_transaction(trans, ret); |
Dan Fuhry | cdd1fed | 2016-03-17 15:23:38 +0100 | [diff] [blame] | 9636 | goto out_fail; |
| 9637 | } |
| 9638 | |
| 9639 | ret = btrfs_add_link(trans, old_dir, new_inode, |
| 9640 | old_dentry->d_name.name, |
| 9641 | old_dentry->d_name.len, 0, new_idx); |
| 9642 | if (ret) { |
Jeff Mahoney | 6664283 | 2016-06-10 18:19:25 -0400 | [diff] [blame] | 9643 | btrfs_abort_transaction(trans, ret); |
Dan Fuhry | cdd1fed | 2016-03-17 15:23:38 +0100 | [diff] [blame] | 9644 | goto out_fail; |
| 9645 | } |
| 9646 | |
| 9647 | if (old_inode->i_nlink == 1) |
| 9648 | BTRFS_I(old_inode)->dir_index = old_idx; |
| 9649 | if (new_inode->i_nlink == 1) |
| 9650 | BTRFS_I(new_inode)->dir_index = new_idx; |
| 9651 | |
Filipe Manana | 86e8aa0 | 2016-05-05 02:02:27 +0100 | [diff] [blame] | 9652 | if (root_log_pinned) { |
Dan Fuhry | cdd1fed | 2016-03-17 15:23:38 +0100 | [diff] [blame] | 9653 | parent = new_dentry->d_parent; |
| 9654 | btrfs_log_new_name(trans, old_inode, old_dir, parent); |
| 9655 | btrfs_end_log_trans(root); |
Filipe Manana | 86e8aa0 | 2016-05-05 02:02:27 +0100 | [diff] [blame] | 9656 | root_log_pinned = false; |
Dan Fuhry | cdd1fed | 2016-03-17 15:23:38 +0100 | [diff] [blame] | 9657 | } |
Filipe Manana | 86e8aa0 | 2016-05-05 02:02:27 +0100 | [diff] [blame] | 9658 | if (dest_log_pinned) { |
Dan Fuhry | cdd1fed | 2016-03-17 15:23:38 +0100 | [diff] [blame] | 9659 | parent = old_dentry->d_parent; |
| 9660 | btrfs_log_new_name(trans, new_inode, new_dir, parent); |
| 9661 | btrfs_end_log_trans(dest); |
Filipe Manana | 86e8aa0 | 2016-05-05 02:02:27 +0100 | [diff] [blame] | 9662 | dest_log_pinned = false; |
Dan Fuhry | cdd1fed | 2016-03-17 15:23:38 +0100 | [diff] [blame] | 9663 | } |
| 9664 | out_fail: |
Filipe Manana | 86e8aa0 | 2016-05-05 02:02:27 +0100 | [diff] [blame] | 9665 | /* |
| 9666 | * If we have pinned a log and an error happened, we unpin tasks |
| 9667 | * trying to sync the log and force them to fallback to a transaction |
| 9668 | * commit if the log currently contains any of the inodes involved in |
| 9669 | * this rename operation (to ensure we do not persist a log with an |
| 9670 | * inconsistent state for any of these inodes or leading to any |
| 9671 | * inconsistencies when replayed). If the transaction was aborted, the |
| 9672 | * abortion reason is propagated to userspace when attempting to commit |
| 9673 | * the transaction. If the log does not contain any of these inodes, we |
| 9674 | * allow the tasks to sync it. |
| 9675 | */ |
| 9676 | if (ret && (root_log_pinned || dest_log_pinned)) { |
| 9677 | if (btrfs_inode_in_log(old_dir, root->fs_info->generation) || |
| 9678 | btrfs_inode_in_log(new_dir, root->fs_info->generation) || |
| 9679 | btrfs_inode_in_log(old_inode, root->fs_info->generation) || |
| 9680 | (new_inode && |
| 9681 | btrfs_inode_in_log(new_inode, root->fs_info->generation))) |
| 9682 | btrfs_set_log_full_commit(root->fs_info, trans); |
| 9683 | |
| 9684 | if (root_log_pinned) { |
| 9685 | btrfs_end_log_trans(root); |
| 9686 | root_log_pinned = false; |
| 9687 | } |
| 9688 | if (dest_log_pinned) { |
| 9689 | btrfs_end_log_trans(dest); |
| 9690 | dest_log_pinned = false; |
| 9691 | } |
| 9692 | } |
Dan Fuhry | cdd1fed | 2016-03-17 15:23:38 +0100 | [diff] [blame] | 9693 | ret = btrfs_end_transaction(trans, root); |
| 9694 | out_notrans: |
| 9695 | if (new_ino == BTRFS_FIRST_FREE_OBJECTID) |
| 9696 | up_read(&dest->fs_info->subvol_sem); |
| 9697 | if (old_ino == BTRFS_FIRST_FREE_OBJECTID) |
| 9698 | up_read(&root->fs_info->subvol_sem); |
| 9699 | |
| 9700 | return ret; |
| 9701 | } |
| 9702 | |
| 9703 | static int btrfs_whiteout_for_rename(struct btrfs_trans_handle *trans, |
| 9704 | struct btrfs_root *root, |
| 9705 | struct inode *dir, |
| 9706 | struct dentry *dentry) |
| 9707 | { |
| 9708 | int ret; |
| 9709 | struct inode *inode; |
| 9710 | u64 objectid; |
| 9711 | u64 index; |
| 9712 | |
| 9713 | ret = btrfs_find_free_ino(root, &objectid); |
| 9714 | if (ret) |
| 9715 | return ret; |
| 9716 | |
| 9717 | inode = btrfs_new_inode(trans, root, dir, |
| 9718 | dentry->d_name.name, |
| 9719 | dentry->d_name.len, |
| 9720 | btrfs_ino(dir), |
| 9721 | objectid, |
| 9722 | S_IFCHR | WHITEOUT_MODE, |
| 9723 | &index); |
| 9724 | |
| 9725 | if (IS_ERR(inode)) { |
| 9726 | ret = PTR_ERR(inode); |
| 9727 | return ret; |
| 9728 | } |
| 9729 | |
| 9730 | inode->i_op = &btrfs_special_inode_operations; |
| 9731 | init_special_inode(inode, inode->i_mode, |
| 9732 | WHITEOUT_DEV); |
| 9733 | |
| 9734 | ret = btrfs_init_inode_security(trans, inode, dir, |
| 9735 | &dentry->d_name); |
| 9736 | if (ret) |
Filipe Manana | c990161 | 2016-05-05 01:41:57 +0100 | [diff] [blame] | 9737 | goto out; |
Dan Fuhry | cdd1fed | 2016-03-17 15:23:38 +0100 | [diff] [blame] | 9738 | |
| 9739 | ret = btrfs_add_nondir(trans, dir, dentry, |
| 9740 | inode, 0, index); |
| 9741 | if (ret) |
Filipe Manana | c990161 | 2016-05-05 01:41:57 +0100 | [diff] [blame] | 9742 | goto out; |
Dan Fuhry | cdd1fed | 2016-03-17 15:23:38 +0100 | [diff] [blame] | 9743 | |
| 9744 | ret = btrfs_update_inode(trans, root, inode); |
Filipe Manana | c990161 | 2016-05-05 01:41:57 +0100 | [diff] [blame] | 9745 | out: |
Dan Fuhry | cdd1fed | 2016-03-17 15:23:38 +0100 | [diff] [blame] | 9746 | unlock_new_inode(inode); |
Filipe Manana | c990161 | 2016-05-05 01:41:57 +0100 | [diff] [blame] | 9747 | if (ret) |
| 9748 | inode_dec_link_count(inode); |
Dan Fuhry | cdd1fed | 2016-03-17 15:23:38 +0100 | [diff] [blame] | 9749 | iput(inode); |
| 9750 | |
Filipe Manana | c990161 | 2016-05-05 01:41:57 +0100 | [diff] [blame] | 9751 | return ret; |
Dan Fuhry | cdd1fed | 2016-03-17 15:23:38 +0100 | [diff] [blame] | 9752 | } |
| 9753 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 9754 | static int btrfs_rename(struct inode *old_dir, struct dentry *old_dentry, |
Dan Fuhry | cdd1fed | 2016-03-17 15:23:38 +0100 | [diff] [blame] | 9755 | struct inode *new_dir, struct dentry *new_dentry, |
| 9756 | unsigned int flags) |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 9757 | { |
| 9758 | struct btrfs_trans_handle *trans; |
Filipe Manana | 5062af3 | 2016-05-05 10:26:26 +0100 | [diff] [blame] | 9759 | unsigned int trans_num_items; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 9760 | struct btrfs_root *root = BTRFS_I(old_dir)->root; |
| 9761 | struct btrfs_root *dest = BTRFS_I(new_dir)->root; |
David Howells | 2b0143b | 2015-03-17 22:25:59 +0000 | [diff] [blame] | 9762 | struct inode *new_inode = d_inode(new_dentry); |
| 9763 | struct inode *old_inode = d_inode(old_dentry); |
Chris Mason | 00e4e6b | 2008-08-05 11:18:09 -0400 | [diff] [blame] | 9764 | u64 index = 0; |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 9765 | u64 root_objectid; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 9766 | int ret; |
Li Zefan | 33345d01 | 2011-04-20 10:31:50 +0800 | [diff] [blame] | 9767 | u64 old_ino = btrfs_ino(old_inode); |
Filipe Manana | 3dc9e8f | 2016-04-29 11:34:22 +0100 | [diff] [blame] | 9768 | bool log_pinned = false; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 9769 | |
Li Zefan | 33345d01 | 2011-04-20 10:31:50 +0800 | [diff] [blame] | 9770 | if (btrfs_ino(new_dir) == BTRFS_EMPTY_SUBVOL_DIR_OBJECTID) |
Yan, Zheng | f679a84 | 2009-09-24 09:17:31 -0400 | [diff] [blame] | 9771 | return -EPERM; |
| 9772 | |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 9773 | /* we only allow rename subvolume link between subvolumes */ |
Li Zefan | 33345d01 | 2011-04-20 10:31:50 +0800 | [diff] [blame] | 9774 | if (old_ino != BTRFS_FIRST_FREE_OBJECTID && root != dest) |
Chris Mason | 3394e16 | 2008-11-17 20:42:26 -0500 | [diff] [blame] | 9775 | return -EXDEV; |
| 9776 | |
Li Zefan | 33345d01 | 2011-04-20 10:31:50 +0800 | [diff] [blame] | 9777 | if (old_ino == BTRFS_EMPTY_SUBVOL_DIR_OBJECTID || |
| 9778 | (new_inode && btrfs_ino(new_inode) == BTRFS_FIRST_FREE_OBJECTID)) |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 9779 | return -ENOTEMPTY; |
| 9780 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 9781 | if (S_ISDIR(old_inode->i_mode) && new_inode && |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 9782 | new_inode->i_size > BTRFS_EMPTY_DIR_SIZE) |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 9783 | return -ENOTEMPTY; |
Chris Mason | 9c52057 | 2012-12-17 14:26:57 -0500 | [diff] [blame] | 9784 | |
| 9785 | |
| 9786 | /* check for collisions, even if the name isn't there */ |
Josef Bacik | 4871c15 | 2013-10-09 12:24:04 -0400 | [diff] [blame] | 9787 | ret = btrfs_check_dir_item_collision(dest, new_dir->i_ino, |
Chris Mason | 9c52057 | 2012-12-17 14:26:57 -0500 | [diff] [blame] | 9788 | new_dentry->d_name.name, |
| 9789 | new_dentry->d_name.len); |
| 9790 | |
| 9791 | if (ret) { |
| 9792 | if (ret == -EEXIST) { |
| 9793 | /* we shouldn't get |
| 9794 | * eexist without a new_inode */ |
Dulshani Gunawardhana | fae7f21 | 2013-10-31 10:30:08 +0530 | [diff] [blame] | 9795 | if (WARN_ON(!new_inode)) { |
Chris Mason | 9c52057 | 2012-12-17 14:26:57 -0500 | [diff] [blame] | 9796 | return ret; |
| 9797 | } |
| 9798 | } else { |
| 9799 | /* maybe -EOVERFLOW */ |
| 9800 | return ret; |
| 9801 | } |
| 9802 | } |
| 9803 | ret = 0; |
| 9804 | |
Chris Mason | 5a3f23d | 2009-03-31 13:27:11 -0400 | [diff] [blame] | 9805 | /* |
Chris Mason | 8d875f9 | 2014-08-12 10:47:42 -0700 | [diff] [blame] | 9806 | * we're using rename to replace one file with another. Start IO on it |
| 9807 | * 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] | 9808 | */ |
Chris Mason | 8d875f9 | 2014-08-12 10:47:42 -0700 | [diff] [blame] | 9809 | 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] | 9810 | filemap_flush(old_inode->i_mapping); |
| 9811 | |
Yan, Zheng | 76dda93 | 2009-09-21 16:00:26 -0400 | [diff] [blame] | 9812 | /* close the racy window with snapshot create/destroy ioctl */ |
Li Zefan | 33345d01 | 2011-04-20 10:31:50 +0800 | [diff] [blame] | 9813 | if (old_ino == BTRFS_FIRST_FREE_OBJECTID) |
Yan, Zheng | 76dda93 | 2009-09-21 16:00:26 -0400 | [diff] [blame] | 9814 | down_read(&root->fs_info->subvol_sem); |
Yan, Zheng | a22285a | 2010-05-16 10:48:46 -0400 | [diff] [blame] | 9815 | /* |
| 9816 | * We want to reserve the absolute worst case amount of items. So if |
| 9817 | * both inodes are subvols and we need to unlink them then that would |
| 9818 | * require 4 item modifications, but if they are both normal inodes it |
Dan Fuhry | cdd1fed | 2016-03-17 15:23:38 +0100 | [diff] [blame] | 9819 | * would require 5 item modifications, so we'll assume they are normal |
Yan, Zheng | a22285a | 2010-05-16 10:48:46 -0400 | [diff] [blame] | 9820 | * inodes. So 5 * 2 is 10, plus 1 for the new link, so 11 total items |
| 9821 | * should cover the worst case number of items we'll modify. |
Filipe Manana | 5062af3 | 2016-05-05 10:26:26 +0100 | [diff] [blame] | 9822 | * If our rename has the whiteout flag, we need more 5 units for the |
| 9823 | * new inode (1 inode item, 1 inode ref, 2 dir items and 1 xattr item |
| 9824 | * when selinux is enabled). |
Yan, Zheng | a22285a | 2010-05-16 10:48:46 -0400 | [diff] [blame] | 9825 | */ |
Filipe Manana | 5062af3 | 2016-05-05 10:26:26 +0100 | [diff] [blame] | 9826 | trans_num_items = 11; |
| 9827 | if (flags & RENAME_WHITEOUT) |
| 9828 | trans_num_items += 5; |
| 9829 | trans = btrfs_start_transaction(root, trans_num_items); |
Johann Lombardi | b44c59a | 2011-03-31 13:23:47 +0000 | [diff] [blame] | 9830 | if (IS_ERR(trans)) { |
Dan Fuhry | cdd1fed | 2016-03-17 15:23:38 +0100 | [diff] [blame] | 9831 | ret = PTR_ERR(trans); |
| 9832 | goto out_notrans; |
| 9833 | } |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 9834 | |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 9835 | if (dest != root) |
| 9836 | btrfs_record_root_in_trans(trans, dest); |
| 9837 | |
Yan, Zheng | a571952 | 2009-09-24 09:17:31 -0400 | [diff] [blame] | 9838 | ret = btrfs_set_inode_index(new_dir, &index); |
| 9839 | if (ret) |
| 9840 | goto out_fail; |
Chris Mason | 5a3f23d | 2009-03-31 13:27:11 -0400 | [diff] [blame] | 9841 | |
Miao Xie | 67de117 | 2013-12-26 13:07:06 +0800 | [diff] [blame] | 9842 | BTRFS_I(old_inode)->dir_index = 0ULL; |
Li Zefan | 33345d01 | 2011-04-20 10:31:50 +0800 | [diff] [blame] | 9843 | if (unlikely(old_ino == BTRFS_FIRST_FREE_OBJECTID)) { |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 9844 | /* force full log commit if subvolume involved. */ |
Miao Xie | 995946d | 2014-04-02 19:51:06 +0800 | [diff] [blame] | 9845 | btrfs_set_log_full_commit(root->fs_info, trans); |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 9846 | } else { |
Filipe Manana | c4aba95 | 2016-04-29 13:14:42 +0100 | [diff] [blame] | 9847 | btrfs_pin_log_trans(root); |
| 9848 | log_pinned = true; |
Yan, Zheng | a571952 | 2009-09-24 09:17:31 -0400 | [diff] [blame] | 9849 | ret = btrfs_insert_inode_ref(trans, dest, |
| 9850 | new_dentry->d_name.name, |
| 9851 | new_dentry->d_name.len, |
Li Zefan | 33345d01 | 2011-04-20 10:31:50 +0800 | [diff] [blame] | 9852 | old_ino, |
| 9853 | btrfs_ino(new_dir), index); |
Yan, Zheng | a571952 | 2009-09-24 09:17:31 -0400 | [diff] [blame] | 9854 | if (ret) |
| 9855 | goto out_fail; |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 9856 | } |
Chris Mason | 5a3f23d | 2009-03-31 13:27:11 -0400 | [diff] [blame] | 9857 | |
Josef Bacik | 0c4d2d9 | 2012-04-05 15:03:02 -0400 | [diff] [blame] | 9858 | inode_inc_iversion(old_dir); |
| 9859 | inode_inc_iversion(new_dir); |
| 9860 | inode_inc_iversion(old_inode); |
Deepa Dinamani | 04b285f | 2016-02-06 23:57:21 -0800 | [diff] [blame] | 9861 | old_dir->i_ctime = old_dir->i_mtime = |
| 9862 | new_dir->i_ctime = new_dir->i_mtime = |
| 9863 | old_inode->i_ctime = current_fs_time(old_dir->i_sb); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 9864 | |
Chris Mason | 12fcfd2 | 2009-03-24 10:24:20 -0400 | [diff] [blame] | 9865 | if (old_dentry->d_parent != new_dentry->d_parent) |
| 9866 | btrfs_record_unlink_dir(trans, old_dir, old_inode, 1); |
| 9867 | |
Li Zefan | 33345d01 | 2011-04-20 10:31:50 +0800 | [diff] [blame] | 9868 | if (unlikely(old_ino == BTRFS_FIRST_FREE_OBJECTID)) { |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 9869 | root_objectid = BTRFS_I(old_inode)->root->root_key.objectid; |
| 9870 | ret = btrfs_unlink_subvol(trans, root, old_dir, root_objectid, |
| 9871 | old_dentry->d_name.name, |
| 9872 | old_dentry->d_name.len); |
| 9873 | } else { |
Al Viro | 9298679 | 2011-03-04 17:14:37 +0000 | [diff] [blame] | 9874 | ret = __btrfs_unlink_inode(trans, root, old_dir, |
David Howells | 2b0143b | 2015-03-17 22:25:59 +0000 | [diff] [blame] | 9875 | d_inode(old_dentry), |
Al Viro | 9298679 | 2011-03-04 17:14:37 +0000 | [diff] [blame] | 9876 | old_dentry->d_name.name, |
| 9877 | old_dentry->d_name.len); |
| 9878 | if (!ret) |
| 9879 | ret = btrfs_update_inode(trans, root, old_inode); |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 9880 | } |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 9881 | if (ret) { |
Jeff Mahoney | 6664283 | 2016-06-10 18:19:25 -0400 | [diff] [blame] | 9882 | btrfs_abort_transaction(trans, ret); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 9883 | goto out_fail; |
| 9884 | } |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 9885 | |
| 9886 | if (new_inode) { |
Josef Bacik | 0c4d2d9 | 2012-04-05 15:03:02 -0400 | [diff] [blame] | 9887 | inode_inc_iversion(new_inode); |
Deepa Dinamani | 04b285f | 2016-02-06 23:57:21 -0800 | [diff] [blame] | 9888 | new_inode->i_ctime = current_fs_time(new_inode->i_sb); |
Li Zefan | 33345d01 | 2011-04-20 10:31:50 +0800 | [diff] [blame] | 9889 | if (unlikely(btrfs_ino(new_inode) == |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 9890 | BTRFS_EMPTY_SUBVOL_DIR_OBJECTID)) { |
| 9891 | root_objectid = BTRFS_I(new_inode)->location.objectid; |
| 9892 | ret = btrfs_unlink_subvol(trans, dest, new_dir, |
| 9893 | root_objectid, |
| 9894 | new_dentry->d_name.name, |
| 9895 | new_dentry->d_name.len); |
| 9896 | BUG_ON(new_inode->i_nlink == 0); |
| 9897 | } else { |
| 9898 | ret = btrfs_unlink_inode(trans, dest, new_dir, |
David Howells | 2b0143b | 2015-03-17 22:25:59 +0000 | [diff] [blame] | 9899 | d_inode(new_dentry), |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 9900 | new_dentry->d_name.name, |
| 9901 | new_dentry->d_name.len); |
| 9902 | } |
Josef Bacik | 4ef31a4 | 2013-08-13 14:10:08 -0400 | [diff] [blame] | 9903 | if (!ret && new_inode->i_nlink == 0) |
David Howells | 2b0143b | 2015-03-17 22:25:59 +0000 | [diff] [blame] | 9904 | ret = btrfs_orphan_add(trans, d_inode(new_dentry)); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 9905 | if (ret) { |
Jeff Mahoney | 6664283 | 2016-06-10 18:19:25 -0400 | [diff] [blame] | 9906 | btrfs_abort_transaction(trans, ret); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 9907 | goto out_fail; |
| 9908 | } |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 9909 | } |
Josef Bacik | aec7477 | 2008-07-24 12:12:38 -0400 | [diff] [blame] | 9910 | |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 9911 | ret = btrfs_add_link(trans, new_dir, old_inode, |
| 9912 | new_dentry->d_name.name, |
Yan, Zheng | a571952 | 2009-09-24 09:17:31 -0400 | [diff] [blame] | 9913 | new_dentry->d_name.len, 0, index); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 9914 | if (ret) { |
Jeff Mahoney | 6664283 | 2016-06-10 18:19:25 -0400 | [diff] [blame] | 9915 | btrfs_abort_transaction(trans, ret); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 9916 | goto out_fail; |
| 9917 | } |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 9918 | |
Miao Xie | 67de117 | 2013-12-26 13:07:06 +0800 | [diff] [blame] | 9919 | if (old_inode->i_nlink == 1) |
| 9920 | BTRFS_I(old_inode)->dir_index = index; |
| 9921 | |
Filipe Manana | 3dc9e8f | 2016-04-29 11:34:22 +0100 | [diff] [blame] | 9922 | if (log_pinned) { |
Al Viro | 10d9f30 | 2011-07-16 23:09:10 -0400 | [diff] [blame] | 9923 | struct dentry *parent = new_dentry->d_parent; |
Filipe Manana | 3dc9e8f | 2016-04-29 11:34:22 +0100 | [diff] [blame] | 9924 | |
Josef Bacik | 6a91221 | 2010-11-20 09:48:00 +0000 | [diff] [blame] | 9925 | btrfs_log_new_name(trans, old_inode, old_dir, parent); |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 9926 | btrfs_end_log_trans(root); |
Filipe Manana | 3dc9e8f | 2016-04-29 11:34:22 +0100 | [diff] [blame] | 9927 | log_pinned = false; |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 9928 | } |
Dan Fuhry | cdd1fed | 2016-03-17 15:23:38 +0100 | [diff] [blame] | 9929 | |
| 9930 | if (flags & RENAME_WHITEOUT) { |
| 9931 | ret = btrfs_whiteout_for_rename(trans, root, old_dir, |
| 9932 | old_dentry); |
| 9933 | |
| 9934 | if (ret) { |
Jeff Mahoney | 6664283 | 2016-06-10 18:19:25 -0400 | [diff] [blame] | 9935 | btrfs_abort_transaction(trans, ret); |
Dan Fuhry | cdd1fed | 2016-03-17 15:23:38 +0100 | [diff] [blame] | 9936 | goto out_fail; |
| 9937 | } |
Chris Mason | 12fcfd2 | 2009-03-24 10:24:20 -0400 | [diff] [blame] | 9938 | } |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 9939 | out_fail: |
Filipe Manana | 3dc9e8f | 2016-04-29 11:34:22 +0100 | [diff] [blame] | 9940 | /* |
| 9941 | * If we have pinned the log and an error happened, we unpin tasks |
| 9942 | * trying to sync the log and force them to fallback to a transaction |
| 9943 | * commit if the log currently contains any of the inodes involved in |
| 9944 | * this rename operation (to ensure we do not persist a log with an |
| 9945 | * inconsistent state for any of these inodes or leading to any |
| 9946 | * inconsistencies when replayed). If the transaction was aborted, the |
| 9947 | * abortion reason is propagated to userspace when attempting to commit |
| 9948 | * the transaction. If the log does not contain any of these inodes, we |
| 9949 | * allow the tasks to sync it. |
| 9950 | */ |
| 9951 | if (ret && log_pinned) { |
| 9952 | if (btrfs_inode_in_log(old_dir, root->fs_info->generation) || |
| 9953 | btrfs_inode_in_log(new_dir, root->fs_info->generation) || |
| 9954 | btrfs_inode_in_log(old_inode, root->fs_info->generation) || |
| 9955 | (new_inode && |
| 9956 | btrfs_inode_in_log(new_inode, root->fs_info->generation))) |
| 9957 | btrfs_set_log_full_commit(root->fs_info, trans); |
| 9958 | |
| 9959 | btrfs_end_log_trans(root); |
| 9960 | log_pinned = false; |
| 9961 | } |
Josef Bacik | 7ad85bb | 2012-01-12 19:10:12 -0500 | [diff] [blame] | 9962 | btrfs_end_transaction(trans, root); |
Johann Lombardi | b44c59a | 2011-03-31 13:23:47 +0000 | [diff] [blame] | 9963 | out_notrans: |
Li Zefan | 33345d01 | 2011-04-20 10:31:50 +0800 | [diff] [blame] | 9964 | if (old_ino == BTRFS_FIRST_FREE_OBJECTID) |
Yan, Zheng | 76dda93 | 2009-09-21 16:00:26 -0400 | [diff] [blame] | 9965 | up_read(&root->fs_info->subvol_sem); |
Josef Bacik | 9ed74f2 | 2009-09-11 16:12:44 -0400 | [diff] [blame] | 9966 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 9967 | return ret; |
| 9968 | } |
| 9969 | |
Miklos Szeredi | 80ace85 | 2014-07-23 15:15:32 +0200 | [diff] [blame] | 9970 | static int btrfs_rename2(struct inode *old_dir, struct dentry *old_dentry, |
| 9971 | struct inode *new_dir, struct dentry *new_dentry, |
| 9972 | unsigned int flags) |
| 9973 | { |
Dan Fuhry | cdd1fed | 2016-03-17 15:23:38 +0100 | [diff] [blame] | 9974 | if (flags & ~(RENAME_NOREPLACE | RENAME_EXCHANGE | RENAME_WHITEOUT)) |
Miklos Szeredi | 80ace85 | 2014-07-23 15:15:32 +0200 | [diff] [blame] | 9975 | return -EINVAL; |
| 9976 | |
Dan Fuhry | cdd1fed | 2016-03-17 15:23:38 +0100 | [diff] [blame] | 9977 | if (flags & RENAME_EXCHANGE) |
| 9978 | return btrfs_rename_exchange(old_dir, old_dentry, new_dir, |
| 9979 | new_dentry); |
| 9980 | |
| 9981 | return btrfs_rename(old_dir, old_dentry, new_dir, new_dentry, flags); |
Miklos Szeredi | 80ace85 | 2014-07-23 15:15:32 +0200 | [diff] [blame] | 9982 | } |
| 9983 | |
Miao Xie | 8ccf6f19 | 2012-10-25 09:28:04 +0000 | [diff] [blame] | 9984 | static void btrfs_run_delalloc_work(struct btrfs_work *work) |
| 9985 | { |
| 9986 | struct btrfs_delalloc_work *delalloc_work; |
Josef Bacik | 9f23e28 | 2013-10-28 15:03:41 -0400 | [diff] [blame] | 9987 | struct inode *inode; |
Miao Xie | 8ccf6f19 | 2012-10-25 09:28:04 +0000 | [diff] [blame] | 9988 | |
| 9989 | delalloc_work = container_of(work, struct btrfs_delalloc_work, |
| 9990 | work); |
Josef Bacik | 9f23e28 | 2013-10-28 15:03:41 -0400 | [diff] [blame] | 9991 | inode = delalloc_work->inode; |
David Sterba | 3042460 | 2015-11-27 19:27:11 +0100 | [diff] [blame] | 9992 | filemap_flush(inode->i_mapping); |
| 9993 | if (test_bit(BTRFS_INODE_HAS_ASYNC_EXTENT, |
| 9994 | &BTRFS_I(inode)->runtime_flags)) |
Josef Bacik | 9f23e28 | 2013-10-28 15:03:41 -0400 | [diff] [blame] | 9995 | filemap_flush(inode->i_mapping); |
Miao Xie | 8ccf6f19 | 2012-10-25 09:28:04 +0000 | [diff] [blame] | 9996 | |
| 9997 | if (delalloc_work->delay_iput) |
Josef Bacik | 9f23e28 | 2013-10-28 15:03:41 -0400 | [diff] [blame] | 9998 | btrfs_add_delayed_iput(inode); |
Miao Xie | 8ccf6f19 | 2012-10-25 09:28:04 +0000 | [diff] [blame] | 9999 | else |
Josef Bacik | 9f23e28 | 2013-10-28 15:03:41 -0400 | [diff] [blame] | 10000 | iput(inode); |
Miao Xie | 8ccf6f19 | 2012-10-25 09:28:04 +0000 | [diff] [blame] | 10001 | complete(&delalloc_work->completion); |
| 10002 | } |
| 10003 | |
| 10004 | struct btrfs_delalloc_work *btrfs_alloc_delalloc_work(struct inode *inode, |
David Sterba | 651d494 | 2015-11-27 19:24:16 +0100 | [diff] [blame] | 10005 | int delay_iput) |
Miao Xie | 8ccf6f19 | 2012-10-25 09:28:04 +0000 | [diff] [blame] | 10006 | { |
| 10007 | struct btrfs_delalloc_work *work; |
| 10008 | |
David Sterba | 100d570 | 2015-12-08 14:39:32 +0100 | [diff] [blame] | 10009 | work = kmalloc(sizeof(*work), GFP_NOFS); |
Miao Xie | 8ccf6f19 | 2012-10-25 09:28:04 +0000 | [diff] [blame] | 10010 | if (!work) |
| 10011 | return NULL; |
| 10012 | |
| 10013 | init_completion(&work->completion); |
| 10014 | INIT_LIST_HEAD(&work->list); |
| 10015 | work->inode = inode; |
Miao Xie | 8ccf6f19 | 2012-10-25 09:28:04 +0000 | [diff] [blame] | 10016 | work->delay_iput = delay_iput; |
Liu Bo | 9e0af23 | 2014-08-15 23:36:53 +0800 | [diff] [blame] | 10017 | WARN_ON_ONCE(!inode); |
| 10018 | btrfs_init_work(&work->work, btrfs_flush_delalloc_helper, |
| 10019 | btrfs_run_delalloc_work, NULL, NULL); |
Miao Xie | 8ccf6f19 | 2012-10-25 09:28:04 +0000 | [diff] [blame] | 10020 | |
| 10021 | return work; |
| 10022 | } |
| 10023 | |
| 10024 | void btrfs_wait_and_free_delalloc_work(struct btrfs_delalloc_work *work) |
| 10025 | { |
| 10026 | wait_for_completion(&work->completion); |
David Sterba | 100d570 | 2015-12-08 14:39:32 +0100 | [diff] [blame] | 10027 | kfree(work); |
Miao Xie | 8ccf6f19 | 2012-10-25 09:28:04 +0000 | [diff] [blame] | 10028 | } |
| 10029 | |
Chris Mason | d352ac6 | 2008-09-29 15:18:18 -0400 | [diff] [blame] | 10030 | /* |
| 10031 | * some fairly slow code that needs optimization. This walks the list |
| 10032 | * of all the inodes with pending delalloc and forces them to disk. |
| 10033 | */ |
Miao Xie | 6c255e6 | 2014-03-06 13:55:01 +0800 | [diff] [blame] | 10034 | static int __start_delalloc_inodes(struct btrfs_root *root, int delay_iput, |
| 10035 | int nr) |
Chris Mason | ea8c281 | 2008-08-04 23:17:27 -0400 | [diff] [blame] | 10036 | { |
Chris Mason | ea8c281 | 2008-08-04 23:17:27 -0400 | [diff] [blame] | 10037 | struct btrfs_inode *binode; |
Zheng Yan | 5b21f2e | 2008-09-26 10:05:38 -0400 | [diff] [blame] | 10038 | struct inode *inode; |
Miao Xie | 8ccf6f19 | 2012-10-25 09:28:04 +0000 | [diff] [blame] | 10039 | struct btrfs_delalloc_work *work, *next; |
| 10040 | struct list_head works; |
Miao Xie | 1eafa6c | 2013-01-22 10:49:00 +0000 | [diff] [blame] | 10041 | struct list_head splice; |
Miao Xie | 8ccf6f19 | 2012-10-25 09:28:04 +0000 | [diff] [blame] | 10042 | int ret = 0; |
Chris Mason | ea8c281 | 2008-08-04 23:17:27 -0400 | [diff] [blame] | 10043 | |
Miao Xie | 8ccf6f19 | 2012-10-25 09:28:04 +0000 | [diff] [blame] | 10044 | INIT_LIST_HEAD(&works); |
Miao Xie | 1eafa6c | 2013-01-22 10:49:00 +0000 | [diff] [blame] | 10045 | INIT_LIST_HEAD(&splice); |
Miao Xie | 63607cc | 2013-01-22 10:50:35 +0000 | [diff] [blame] | 10046 | |
Miao Xie | 573bfb7 | 2014-03-06 13:55:03 +0800 | [diff] [blame] | 10047 | mutex_lock(&root->delalloc_mutex); |
Miao Xie | eb73c1b | 2013-05-15 07:48:22 +0000 | [diff] [blame] | 10048 | spin_lock(&root->delalloc_lock); |
| 10049 | list_splice_init(&root->delalloc_inodes, &splice); |
Miao Xie | 1eafa6c | 2013-01-22 10:49:00 +0000 | [diff] [blame] | 10050 | while (!list_empty(&splice)) { |
| 10051 | binode = list_entry(splice.next, struct btrfs_inode, |
Chris Mason | ea8c281 | 2008-08-04 23:17:27 -0400 | [diff] [blame] | 10052 | delalloc_inodes); |
Miao Xie | 1eafa6c | 2013-01-22 10:49:00 +0000 | [diff] [blame] | 10053 | |
Miao Xie | eb73c1b | 2013-05-15 07:48:22 +0000 | [diff] [blame] | 10054 | list_move_tail(&binode->delalloc_inodes, |
| 10055 | &root->delalloc_inodes); |
Zheng Yan | 5b21f2e | 2008-09-26 10:05:38 -0400 | [diff] [blame] | 10056 | inode = igrab(&binode->vfs_inode); |
Miao Xie | df0af1a | 2013-01-29 10:11:59 +0000 | [diff] [blame] | 10057 | if (!inode) { |
Miao Xie | eb73c1b | 2013-05-15 07:48:22 +0000 | [diff] [blame] | 10058 | cond_resched_lock(&root->delalloc_lock); |
Miao Xie | 1eafa6c | 2013-01-22 10:49:00 +0000 | [diff] [blame] | 10059 | continue; |
Miao Xie | df0af1a | 2013-01-29 10:11:59 +0000 | [diff] [blame] | 10060 | } |
Miao Xie | eb73c1b | 2013-05-15 07:48:22 +0000 | [diff] [blame] | 10061 | spin_unlock(&root->delalloc_lock); |
Miao Xie | 1eafa6c | 2013-01-22 10:49:00 +0000 | [diff] [blame] | 10062 | |
David Sterba | 651d494 | 2015-11-27 19:24:16 +0100 | [diff] [blame] | 10063 | work = btrfs_alloc_delalloc_work(inode, delay_iput); |
David Sterba | 5d99a998 | 2014-09-29 19:20:37 +0200 | [diff] [blame] | 10064 | if (!work) { |
Josef Bacik | f4ab9ea | 2013-09-17 11:25:44 -0400 | [diff] [blame] | 10065 | if (delay_iput) |
| 10066 | btrfs_add_delayed_iput(inode); |
| 10067 | else |
| 10068 | iput(inode); |
Miao Xie | 1eafa6c | 2013-01-22 10:49:00 +0000 | [diff] [blame] | 10069 | ret = -ENOMEM; |
Wang Shilong | a1ecaab | 2014-04-02 19:53:32 +0800 | [diff] [blame] | 10070 | goto out; |
Zheng Yan | 5b21f2e | 2008-09-26 10:05:38 -0400 | [diff] [blame] | 10071 | } |
Miao Xie | 1eafa6c | 2013-01-22 10:49:00 +0000 | [diff] [blame] | 10072 | list_add_tail(&work->list, &works); |
Qu Wenruo | a44903a | 2014-02-28 10:46:09 +0800 | [diff] [blame] | 10073 | btrfs_queue_work(root->fs_info->flush_workers, |
| 10074 | &work->work); |
Miao Xie | 6c255e6 | 2014-03-06 13:55:01 +0800 | [diff] [blame] | 10075 | ret++; |
| 10076 | if (nr != -1 && ret >= nr) |
Wang Shilong | a1ecaab | 2014-04-02 19:53:32 +0800 | [diff] [blame] | 10077 | goto out; |
Zheng Yan | 5b21f2e | 2008-09-26 10:05:38 -0400 | [diff] [blame] | 10078 | cond_resched(); |
Miao Xie | eb73c1b | 2013-05-15 07:48:22 +0000 | [diff] [blame] | 10079 | spin_lock(&root->delalloc_lock); |
Chris Mason | ea8c281 | 2008-08-04 23:17:27 -0400 | [diff] [blame] | 10080 | } |
Miao Xie | eb73c1b | 2013-05-15 07:48:22 +0000 | [diff] [blame] | 10081 | spin_unlock(&root->delalloc_lock); |
Chris Mason | 8c8bee1 | 2008-09-29 11:19:10 -0400 | [diff] [blame] | 10082 | |
Wang Shilong | a1ecaab | 2014-04-02 19:53:32 +0800 | [diff] [blame] | 10083 | out: |
Miao Xie | 1eafa6c | 2013-01-22 10:49:00 +0000 | [diff] [blame] | 10084 | list_for_each_entry_safe(work, next, &works, list) { |
| 10085 | list_del_init(&work->list); |
| 10086 | btrfs_wait_and_free_delalloc_work(work); |
| 10087 | } |
| 10088 | |
Miao Xie | eb73c1b | 2013-05-15 07:48:22 +0000 | [diff] [blame] | 10089 | if (!list_empty_careful(&splice)) { |
| 10090 | spin_lock(&root->delalloc_lock); |
| 10091 | list_splice_tail(&splice, &root->delalloc_inodes); |
| 10092 | spin_unlock(&root->delalloc_lock); |
| 10093 | } |
Miao Xie | 573bfb7 | 2014-03-06 13:55:03 +0800 | [diff] [blame] | 10094 | mutex_unlock(&root->delalloc_mutex); |
Miao Xie | eb73c1b | 2013-05-15 07:48:22 +0000 | [diff] [blame] | 10095 | return ret; |
| 10096 | } |
| 10097 | |
| 10098 | int btrfs_start_delalloc_inodes(struct btrfs_root *root, int delay_iput) |
| 10099 | { |
| 10100 | int ret; |
| 10101 | |
Wang Shilong | 2c21b4d | 2014-01-14 19:42:20 +0800 | [diff] [blame] | 10102 | if (test_bit(BTRFS_FS_STATE_ERROR, &root->fs_info->fs_state)) |
Miao Xie | eb73c1b | 2013-05-15 07:48:22 +0000 | [diff] [blame] | 10103 | return -EROFS; |
| 10104 | |
Miao Xie | 6c255e6 | 2014-03-06 13:55:01 +0800 | [diff] [blame] | 10105 | ret = __start_delalloc_inodes(root, delay_iput, -1); |
| 10106 | if (ret > 0) |
| 10107 | ret = 0; |
Miao Xie | eb73c1b | 2013-05-15 07:48:22 +0000 | [diff] [blame] | 10108 | /* |
| 10109 | * the filemap_flush will queue IO into the worker threads, but |
Chris Mason | 8c8bee1 | 2008-09-29 11:19:10 -0400 | [diff] [blame] | 10110 | * we have to make sure the IO is actually started and that |
| 10111 | * ordered extents get created before we return |
| 10112 | */ |
| 10113 | atomic_inc(&root->fs_info->async_submit_draining); |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 10114 | while (atomic_read(&root->fs_info->nr_async_submits) || |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 10115 | atomic_read(&root->fs_info->async_delalloc_pages)) { |
Chris Mason | 8c8bee1 | 2008-09-29 11:19:10 -0400 | [diff] [blame] | 10116 | wait_event(root->fs_info->async_submit_wait, |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 10117 | (atomic_read(&root->fs_info->nr_async_submits) == 0 && |
| 10118 | atomic_read(&root->fs_info->async_delalloc_pages) == 0)); |
Chris Mason | 8c8bee1 | 2008-09-29 11:19:10 -0400 | [diff] [blame] | 10119 | } |
| 10120 | atomic_dec(&root->fs_info->async_submit_draining); |
Miao Xie | eb73c1b | 2013-05-15 07:48:22 +0000 | [diff] [blame] | 10121 | return ret; |
| 10122 | } |
| 10123 | |
Miao Xie | 6c255e6 | 2014-03-06 13:55:01 +0800 | [diff] [blame] | 10124 | int btrfs_start_delalloc_roots(struct btrfs_fs_info *fs_info, int delay_iput, |
| 10125 | int nr) |
Miao Xie | eb73c1b | 2013-05-15 07:48:22 +0000 | [diff] [blame] | 10126 | { |
| 10127 | struct btrfs_root *root; |
| 10128 | struct list_head splice; |
| 10129 | int ret; |
| 10130 | |
Wang Shilong | 2c21b4d | 2014-01-14 19:42:20 +0800 | [diff] [blame] | 10131 | if (test_bit(BTRFS_FS_STATE_ERROR, &fs_info->fs_state)) |
Miao Xie | eb73c1b | 2013-05-15 07:48:22 +0000 | [diff] [blame] | 10132 | return -EROFS; |
| 10133 | |
| 10134 | INIT_LIST_HEAD(&splice); |
| 10135 | |
Miao Xie | 573bfb7 | 2014-03-06 13:55:03 +0800 | [diff] [blame] | 10136 | mutex_lock(&fs_info->delalloc_root_mutex); |
Miao Xie | eb73c1b | 2013-05-15 07:48:22 +0000 | [diff] [blame] | 10137 | spin_lock(&fs_info->delalloc_root_lock); |
| 10138 | list_splice_init(&fs_info->delalloc_roots, &splice); |
Miao Xie | 6c255e6 | 2014-03-06 13:55:01 +0800 | [diff] [blame] | 10139 | while (!list_empty(&splice) && nr) { |
Miao Xie | eb73c1b | 2013-05-15 07:48:22 +0000 | [diff] [blame] | 10140 | root = list_first_entry(&splice, struct btrfs_root, |
| 10141 | delalloc_root); |
| 10142 | root = btrfs_grab_fs_root(root); |
| 10143 | BUG_ON(!root); |
| 10144 | list_move_tail(&root->delalloc_root, |
| 10145 | &fs_info->delalloc_roots); |
| 10146 | spin_unlock(&fs_info->delalloc_root_lock); |
| 10147 | |
Miao Xie | 6c255e6 | 2014-03-06 13:55:01 +0800 | [diff] [blame] | 10148 | ret = __start_delalloc_inodes(root, delay_iput, nr); |
Miao Xie | eb73c1b | 2013-05-15 07:48:22 +0000 | [diff] [blame] | 10149 | btrfs_put_fs_root(root); |
Miao Xie | 6c255e6 | 2014-03-06 13:55:01 +0800 | [diff] [blame] | 10150 | if (ret < 0) |
Miao Xie | eb73c1b | 2013-05-15 07:48:22 +0000 | [diff] [blame] | 10151 | goto out; |
| 10152 | |
Miao Xie | 6c255e6 | 2014-03-06 13:55:01 +0800 | [diff] [blame] | 10153 | if (nr != -1) { |
| 10154 | nr -= ret; |
| 10155 | WARN_ON(nr < 0); |
| 10156 | } |
Miao Xie | eb73c1b | 2013-05-15 07:48:22 +0000 | [diff] [blame] | 10157 | spin_lock(&fs_info->delalloc_root_lock); |
| 10158 | } |
| 10159 | spin_unlock(&fs_info->delalloc_root_lock); |
| 10160 | |
Miao Xie | 6c255e6 | 2014-03-06 13:55:01 +0800 | [diff] [blame] | 10161 | ret = 0; |
Miao Xie | eb73c1b | 2013-05-15 07:48:22 +0000 | [diff] [blame] | 10162 | atomic_inc(&fs_info->async_submit_draining); |
| 10163 | while (atomic_read(&fs_info->nr_async_submits) || |
| 10164 | atomic_read(&fs_info->async_delalloc_pages)) { |
| 10165 | wait_event(fs_info->async_submit_wait, |
| 10166 | (atomic_read(&fs_info->nr_async_submits) == 0 && |
| 10167 | atomic_read(&fs_info->async_delalloc_pages) == 0)); |
| 10168 | } |
| 10169 | atomic_dec(&fs_info->async_submit_draining); |
Miao Xie | 8ccf6f19 | 2012-10-25 09:28:04 +0000 | [diff] [blame] | 10170 | out: |
Miao Xie | 1eafa6c | 2013-01-22 10:49:00 +0000 | [diff] [blame] | 10171 | if (!list_empty_careful(&splice)) { |
Miao Xie | eb73c1b | 2013-05-15 07:48:22 +0000 | [diff] [blame] | 10172 | spin_lock(&fs_info->delalloc_root_lock); |
| 10173 | list_splice_tail(&splice, &fs_info->delalloc_roots); |
| 10174 | spin_unlock(&fs_info->delalloc_root_lock); |
Miao Xie | 1eafa6c | 2013-01-22 10:49:00 +0000 | [diff] [blame] | 10175 | } |
Miao Xie | 573bfb7 | 2014-03-06 13:55:03 +0800 | [diff] [blame] | 10176 | mutex_unlock(&fs_info->delalloc_root_mutex); |
Miao Xie | 8ccf6f19 | 2012-10-25 09:28:04 +0000 | [diff] [blame] | 10177 | return ret; |
Chris Mason | ea8c281 | 2008-08-04 23:17:27 -0400 | [diff] [blame] | 10178 | } |
| 10179 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 10180 | static int btrfs_symlink(struct inode *dir, struct dentry *dentry, |
| 10181 | const char *symname) |
| 10182 | { |
| 10183 | struct btrfs_trans_handle *trans; |
| 10184 | struct btrfs_root *root = BTRFS_I(dir)->root; |
| 10185 | struct btrfs_path *path; |
| 10186 | struct btrfs_key key; |
Chris Mason | 1832a6d | 2007-12-21 16:27:21 -0500 | [diff] [blame] | 10187 | struct inode *inode = NULL; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 10188 | int err; |
| 10189 | int drop_inode = 0; |
| 10190 | u64 objectid; |
Dulshani Gunawardhana | 6787125 | 2013-10-31 10:33:04 +0530 | [diff] [blame] | 10191 | u64 index = 0; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 10192 | int name_len; |
| 10193 | int datasize; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 10194 | unsigned long ptr; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 10195 | struct btrfs_file_extent_item *ei; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 10196 | struct extent_buffer *leaf; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 10197 | |
Filipe David Borba Manana | f06becc | 2013-09-16 09:53:28 +0100 | [diff] [blame] | 10198 | name_len = strlen(symname); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 10199 | if (name_len > BTRFS_MAX_INLINE_DATA_SIZE(root)) |
| 10200 | return -ENAMETOOLONG; |
Chris Mason | 1832a6d | 2007-12-21 16:27:21 -0500 | [diff] [blame] | 10201 | |
Josef Bacik | 9ed74f2 | 2009-09-11 16:12:44 -0400 | [diff] [blame] | 10202 | /* |
| 10203 | * 2 items for inode item and ref |
| 10204 | * 2 items for dir items |
Filipe Manana | 9269d12 | 2015-12-31 18:16:29 +0000 | [diff] [blame] | 10205 | * 1 item for updating parent inode item |
| 10206 | * 1 item for the inline extent item |
Josef Bacik | 9ed74f2 | 2009-09-11 16:12:44 -0400 | [diff] [blame] | 10207 | * 1 item for xattr if selinux is on |
| 10208 | */ |
Filipe Manana | 9269d12 | 2015-12-31 18:16:29 +0000 | [diff] [blame] | 10209 | trans = btrfs_start_transaction(root, 7); |
Yan, Zheng | a22285a | 2010-05-16 10:48:46 -0400 | [diff] [blame] | 10210 | if (IS_ERR(trans)) |
| 10211 | return PTR_ERR(trans); |
Chris Mason | 1832a6d | 2007-12-21 16:27:21 -0500 | [diff] [blame] | 10212 | |
Li Zefan | 581bb05 | 2011-04-20 10:06:11 +0800 | [diff] [blame] | 10213 | err = btrfs_find_free_ino(root, &objectid); |
| 10214 | if (err) |
| 10215 | goto out_unlock; |
| 10216 | |
Josef Bacik | aec7477 | 2008-07-24 12:12:38 -0400 | [diff] [blame] | 10217 | inode = btrfs_new_inode(trans, root, dir, dentry->d_name.name, |
Li Zefan | 33345d01 | 2011-04-20 10:31:50 +0800 | [diff] [blame] | 10218 | dentry->d_name.len, btrfs_ino(dir), objectid, |
Josef Bacik | d82a6f1 | 2011-05-11 15:26:06 -0400 | [diff] [blame] | 10219 | S_IFLNK|S_IRWXUGO, &index); |
Tsutomu Itoh | 7cf96da | 2011-04-25 19:43:53 -0400 | [diff] [blame] | 10220 | if (IS_ERR(inode)) { |
| 10221 | err = PTR_ERR(inode); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 10222 | goto out_unlock; |
Tsutomu Itoh | 7cf96da | 2011-04-25 19:43:53 -0400 | [diff] [blame] | 10223 | } |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 10224 | |
Casey Schaufler | ad19db7 | 2011-12-15 10:09:07 -0500 | [diff] [blame] | 10225 | /* |
| 10226 | * If the active LSM wants to access the inode during |
| 10227 | * d_instantiate it needs these. Smack checks to see |
| 10228 | * if the filesystem supports xattrs by looking at the |
| 10229 | * ops vector. |
| 10230 | */ |
| 10231 | inode->i_fop = &btrfs_file_operations; |
| 10232 | inode->i_op = &btrfs_file_inode_operations; |
Chris Mason | b0d5d10 | 2014-09-08 13:08:51 -0700 | [diff] [blame] | 10233 | inode->i_mapping->a_ops = &btrfs_aops; |
Chris Mason | b0d5d10 | 2014-09-08 13:08:51 -0700 | [diff] [blame] | 10234 | BTRFS_I(inode)->io_tree.ops = &btrfs_extent_io_ops; |
| 10235 | |
| 10236 | err = btrfs_init_inode_security(trans, inode, dir, &dentry->d_name); |
| 10237 | if (err) |
| 10238 | goto out_unlock_inode; |
Casey Schaufler | ad19db7 | 2011-12-15 10:09:07 -0500 | [diff] [blame] | 10239 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 10240 | path = btrfs_alloc_path(); |
Mark Fasheh | d8926bb | 2011-07-13 10:38:47 -0700 | [diff] [blame] | 10241 | if (!path) { |
| 10242 | err = -ENOMEM; |
Chris Mason | b0d5d10 | 2014-09-08 13:08:51 -0700 | [diff] [blame] | 10243 | goto out_unlock_inode; |
Mark Fasheh | d8926bb | 2011-07-13 10:38:47 -0700 | [diff] [blame] | 10244 | } |
Li Zefan | 33345d01 | 2011-04-20 10:31:50 +0800 | [diff] [blame] | 10245 | key.objectid = btrfs_ino(inode); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 10246 | key.offset = 0; |
David Sterba | 962a298 | 2014-06-04 18:41:45 +0200 | [diff] [blame] | 10247 | key.type = BTRFS_EXTENT_DATA_KEY; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 10248 | datasize = btrfs_file_extent_calc_inline_size(name_len); |
| 10249 | err = btrfs_insert_empty_item(trans, root, path, &key, |
| 10250 | datasize); |
Chris Mason | 54aa1f4 | 2007-06-22 14:16:25 -0400 | [diff] [blame] | 10251 | if (err) { |
Julia Lawall | b083916 | 2011-05-14 07:10:51 +0000 | [diff] [blame] | 10252 | btrfs_free_path(path); |
Chris Mason | b0d5d10 | 2014-09-08 13:08:51 -0700 | [diff] [blame] | 10253 | goto out_unlock_inode; |
Chris Mason | 54aa1f4 | 2007-06-22 14:16:25 -0400 | [diff] [blame] | 10254 | } |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 10255 | leaf = path->nodes[0]; |
| 10256 | ei = btrfs_item_ptr(leaf, path->slots[0], |
| 10257 | struct btrfs_file_extent_item); |
| 10258 | btrfs_set_file_extent_generation(leaf, ei, trans->transid); |
| 10259 | btrfs_set_file_extent_type(leaf, ei, |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 10260 | BTRFS_FILE_EXTENT_INLINE); |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 10261 | btrfs_set_file_extent_encryption(leaf, ei, 0); |
| 10262 | btrfs_set_file_extent_compression(leaf, ei, 0); |
| 10263 | btrfs_set_file_extent_other_encoding(leaf, ei, 0); |
| 10264 | btrfs_set_file_extent_ram_bytes(leaf, ei, name_len); |
| 10265 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 10266 | ptr = btrfs_file_extent_inline_start(ei); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 10267 | write_extent_buffer(leaf, symname, ptr, name_len); |
| 10268 | btrfs_mark_buffer_dirty(leaf); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 10269 | btrfs_free_path(path); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 10270 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 10271 | inode->i_op = &btrfs_symlink_inode_operations; |
Al Viro | 21fc61c | 2015-11-17 01:07:57 -0500 | [diff] [blame] | 10272 | inode_nohighmem(inode); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 10273 | inode->i_mapping->a_ops = &btrfs_symlink_aops; |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 10274 | inode_set_bytes(inode, name_len); |
Filipe David Borba Manana | f06becc | 2013-09-16 09:53:28 +0100 | [diff] [blame] | 10275 | btrfs_i_size_write(inode, name_len); |
Chris Mason | 54aa1f4 | 2007-06-22 14:16:25 -0400 | [diff] [blame] | 10276 | err = btrfs_update_inode(trans, root, inode); |
Filipe Manana | d50866d | 2015-12-31 18:08:24 +0000 | [diff] [blame] | 10277 | /* |
| 10278 | * Last step, add directory indexes for our symlink inode. This is the |
| 10279 | * last step to avoid extra cleanup of these indexes if an error happens |
| 10280 | * elsewhere above. |
| 10281 | */ |
| 10282 | if (!err) |
| 10283 | err = btrfs_add_nondir(trans, dir, dentry, inode, 0, index); |
Chris Mason | b0d5d10 | 2014-09-08 13:08:51 -0700 | [diff] [blame] | 10284 | if (err) { |
Chris Mason | 54aa1f4 | 2007-06-22 14:16:25 -0400 | [diff] [blame] | 10285 | drop_inode = 1; |
Chris Mason | b0d5d10 | 2014-09-08 13:08:51 -0700 | [diff] [blame] | 10286 | goto out_unlock_inode; |
| 10287 | } |
| 10288 | |
| 10289 | unlock_new_inode(inode); |
| 10290 | d_instantiate(dentry, inode); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 10291 | |
| 10292 | out_unlock: |
Josef Bacik | 7ad85bb | 2012-01-12 19:10:12 -0500 | [diff] [blame] | 10293 | btrfs_end_transaction(trans, root); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 10294 | if (drop_inode) { |
| 10295 | inode_dec_link_count(inode); |
| 10296 | iput(inode); |
| 10297 | } |
Liu Bo | b53d3f5 | 2012-11-14 14:34:34 +0000 | [diff] [blame] | 10298 | btrfs_btree_balance_dirty(root); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 10299 | return err; |
Chris Mason | b0d5d10 | 2014-09-08 13:08:51 -0700 | [diff] [blame] | 10300 | |
| 10301 | out_unlock_inode: |
| 10302 | drop_inode = 1; |
| 10303 | unlock_new_inode(inode); |
| 10304 | goto out_unlock; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 10305 | } |
Chris Mason | 1643298 | 2008-04-10 10:23:21 -0400 | [diff] [blame] | 10306 | |
Josef Bacik | 0af3d00 | 2010-06-21 14:48:16 -0400 | [diff] [blame] | 10307 | static int __btrfs_prealloc_file_range(struct inode *inode, int mode, |
| 10308 | u64 start, u64 num_bytes, u64 min_size, |
| 10309 | loff_t actual_len, u64 *alloc_hint, |
| 10310 | struct btrfs_trans_handle *trans) |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 10311 | { |
Josef Bacik | 5dc562c | 2012-08-17 13:14:17 -0400 | [diff] [blame] | 10312 | struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree; |
| 10313 | struct extent_map *em; |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 10314 | struct btrfs_root *root = BTRFS_I(inode)->root; |
| 10315 | struct btrfs_key ins; |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 10316 | u64 cur_offset = start; |
Josef Bacik | 55a61d1 | 2010-11-22 18:50:32 +0000 | [diff] [blame] | 10317 | u64 i_size; |
Chris Mason | 154ea28 | 2013-03-05 11:11:26 -0500 | [diff] [blame] | 10318 | u64 cur_bytes; |
Josef Bacik | 0b670dc | 2015-09-23 17:11:16 -0400 | [diff] [blame] | 10319 | u64 last_alloc = (u64)-1; |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 10320 | int ret = 0; |
Josef Bacik | 0af3d00 | 2010-06-21 14:48:16 -0400 | [diff] [blame] | 10321 | bool own_trans = true; |
Wang Xiaoguang | 1851309 | 2016-07-25 15:51:40 +0800 | [diff] [blame] | 10322 | u64 end = start + num_bytes - 1; |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 10323 | |
Josef Bacik | 0af3d00 | 2010-06-21 14:48:16 -0400 | [diff] [blame] | 10324 | if (trans) |
| 10325 | own_trans = false; |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 10326 | while (num_bytes > 0) { |
Josef Bacik | 0af3d00 | 2010-06-21 14:48:16 -0400 | [diff] [blame] | 10327 | if (own_trans) { |
| 10328 | trans = btrfs_start_transaction(root, 3); |
| 10329 | if (IS_ERR(trans)) { |
| 10330 | ret = PTR_ERR(trans); |
| 10331 | break; |
| 10332 | } |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 10333 | } |
Yan, Zheng | 5a303d5 | 2009-11-12 09:34:52 +0000 | [diff] [blame] | 10334 | |
Byongho Lee | ee22184 | 2015-12-15 01:42:10 +0900 | [diff] [blame] | 10335 | cur_bytes = min_t(u64, num_bytes, SZ_256M); |
Chris Mason | 154ea28 | 2013-03-05 11:11:26 -0500 | [diff] [blame] | 10336 | cur_bytes = max(cur_bytes, min_size); |
Josef Bacik | 0b670dc | 2015-09-23 17:11:16 -0400 | [diff] [blame] | 10337 | /* |
| 10338 | * If we are severely fragmented we could end up with really |
| 10339 | * small allocations, so if the allocator is returning small |
| 10340 | * chunks lets make its job easier by only searching for those |
| 10341 | * sized chunks. |
| 10342 | */ |
| 10343 | cur_bytes = min(cur_bytes, last_alloc); |
Wang Xiaoguang | 1851309 | 2016-07-25 15:51:40 +0800 | [diff] [blame] | 10344 | ret = btrfs_reserve_extent(root, cur_bytes, cur_bytes, |
| 10345 | min_size, 0, *alloc_hint, &ins, 1, 0); |
Yan, Zheng | 5a303d5 | 2009-11-12 09:34:52 +0000 | [diff] [blame] | 10346 | if (ret) { |
Josef Bacik | 0af3d00 | 2010-06-21 14:48:16 -0400 | [diff] [blame] | 10347 | if (own_trans) |
| 10348 | btrfs_end_transaction(trans, root); |
Yan, Zheng | a22285a | 2010-05-16 10:48:46 -0400 | [diff] [blame] | 10349 | break; |
Yan, Zheng | 5a303d5 | 2009-11-12 09:34:52 +0000 | [diff] [blame] | 10350 | } |
Filipe Manana | 9cfa3e3 | 2016-04-26 15:39:32 +0100 | [diff] [blame] | 10351 | btrfs_dec_block_group_reservations(root->fs_info, ins.objectid); |
Yan, Zheng | 5a303d5 | 2009-11-12 09:34:52 +0000 | [diff] [blame] | 10352 | |
Josef Bacik | 0b670dc | 2015-09-23 17:11:16 -0400 | [diff] [blame] | 10353 | last_alloc = ins.offset; |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 10354 | ret = insert_reserved_file_extent(trans, inode, |
| 10355 | cur_offset, ins.objectid, |
| 10356 | ins.offset, ins.offset, |
Yan, Zheng | 920bbbf | 2009-11-12 09:34:08 +0000 | [diff] [blame] | 10357 | ins.offset, 0, 0, 0, |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 10358 | BTRFS_FILE_EXTENT_PREALLOC); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 10359 | if (ret) { |
Josef Bacik | 857cc2f | 2013-10-07 15:21:08 -0400 | [diff] [blame] | 10360 | btrfs_free_reserved_extent(root, ins.objectid, |
Miao Xie | e570fd2 | 2014-06-19 10:42:50 +0800 | [diff] [blame] | 10361 | ins.offset, 0); |
Jeff Mahoney | 6664283 | 2016-06-10 18:19:25 -0400 | [diff] [blame] | 10362 | btrfs_abort_transaction(trans, ret); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 10363 | if (own_trans) |
| 10364 | btrfs_end_transaction(trans, root); |
| 10365 | break; |
| 10366 | } |
Dongsheng Yang | 3119321 | 2014-12-12 16:44:35 +0800 | [diff] [blame] | 10367 | |
Chris Mason | a1ed835 | 2009-09-11 12:27:37 -0400 | [diff] [blame] | 10368 | btrfs_drop_extent_cache(inode, cur_offset, |
| 10369 | cur_offset + ins.offset -1, 0); |
Yan, Zheng | 5a303d5 | 2009-11-12 09:34:52 +0000 | [diff] [blame] | 10370 | |
Josef Bacik | 5dc562c | 2012-08-17 13:14:17 -0400 | [diff] [blame] | 10371 | em = alloc_extent_map(); |
| 10372 | if (!em) { |
| 10373 | set_bit(BTRFS_INODE_NEEDS_FULL_SYNC, |
| 10374 | &BTRFS_I(inode)->runtime_flags); |
| 10375 | goto next; |
| 10376 | } |
| 10377 | |
| 10378 | em->start = cur_offset; |
| 10379 | em->orig_start = cur_offset; |
| 10380 | em->len = ins.offset; |
| 10381 | em->block_start = ins.objectid; |
| 10382 | em->block_len = ins.offset; |
Josef Bacik | b493968 | 2012-12-03 10:31:19 -0500 | [diff] [blame] | 10383 | em->orig_block_len = ins.offset; |
Josef Bacik | cc95bef | 2013-04-04 14:31:27 -0400 | [diff] [blame] | 10384 | em->ram_bytes = ins.offset; |
Josef Bacik | 5dc562c | 2012-08-17 13:14:17 -0400 | [diff] [blame] | 10385 | em->bdev = root->fs_info->fs_devices->latest_bdev; |
| 10386 | set_bit(EXTENT_FLAG_PREALLOC, &em->flags); |
| 10387 | em->generation = trans->transid; |
| 10388 | |
| 10389 | while (1) { |
| 10390 | write_lock(&em_tree->lock); |
Josef Bacik | 09a2a8f9 | 2013-04-05 16:51:15 -0400 | [diff] [blame] | 10391 | ret = add_extent_mapping(em_tree, em, 1); |
Josef Bacik | 5dc562c | 2012-08-17 13:14:17 -0400 | [diff] [blame] | 10392 | write_unlock(&em_tree->lock); |
| 10393 | if (ret != -EEXIST) |
| 10394 | break; |
| 10395 | btrfs_drop_extent_cache(inode, cur_offset, |
| 10396 | cur_offset + ins.offset - 1, |
| 10397 | 0); |
| 10398 | } |
| 10399 | free_extent_map(em); |
| 10400 | next: |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 10401 | num_bytes -= ins.offset; |
| 10402 | cur_offset += ins.offset; |
Yan, Zheng | efa5646 | 2010-05-16 10:49:59 -0400 | [diff] [blame] | 10403 | *alloc_hint = ins.objectid + ins.offset; |
Yan, Zheng | 5a303d5 | 2009-11-12 09:34:52 +0000 | [diff] [blame] | 10404 | |
Josef Bacik | 0c4d2d9 | 2012-04-05 15:03:02 -0400 | [diff] [blame] | 10405 | inode_inc_iversion(inode); |
Deepa Dinamani | 04b285f | 2016-02-06 23:57:21 -0800 | [diff] [blame] | 10406 | inode->i_ctime = current_fs_time(inode->i_sb); |
Christoph Hellwig | 6cbff00 | 2009-04-17 10:37:41 +0200 | [diff] [blame] | 10407 | BTRFS_I(inode)->flags |= BTRFS_INODE_PREALLOC; |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 10408 | if (!(mode & FALLOC_FL_KEEP_SIZE) && |
Yan, Zheng | efa5646 | 2010-05-16 10:49:59 -0400 | [diff] [blame] | 10409 | (actual_len > inode->i_size) && |
| 10410 | (cur_offset > inode->i_size)) { |
Aneesh Kumar K.V | d1ea6a6 | 2010-01-20 07:28:54 +0000 | [diff] [blame] | 10411 | if (cur_offset > actual_len) |
Josef Bacik | 55a61d1 | 2010-11-22 18:50:32 +0000 | [diff] [blame] | 10412 | i_size = actual_len; |
Aneesh Kumar K.V | d1ea6a6 | 2010-01-20 07:28:54 +0000 | [diff] [blame] | 10413 | else |
Josef Bacik | 55a61d1 | 2010-11-22 18:50:32 +0000 | [diff] [blame] | 10414 | i_size = cur_offset; |
| 10415 | i_size_write(inode, i_size); |
| 10416 | btrfs_ordered_update_i_size(inode, i_size, NULL); |
Yan, Zheng | 5a303d5 | 2009-11-12 09:34:52 +0000 | [diff] [blame] | 10417 | } |
| 10418 | |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 10419 | ret = btrfs_update_inode(trans, root, inode); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 10420 | |
| 10421 | if (ret) { |
Jeff Mahoney | 6664283 | 2016-06-10 18:19:25 -0400 | [diff] [blame] | 10422 | btrfs_abort_transaction(trans, ret); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 10423 | if (own_trans) |
| 10424 | btrfs_end_transaction(trans, root); |
| 10425 | break; |
| 10426 | } |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 10427 | |
Josef Bacik | 0af3d00 | 2010-06-21 14:48:16 -0400 | [diff] [blame] | 10428 | if (own_trans) |
| 10429 | btrfs_end_transaction(trans, root); |
Yan, Zheng | 5a303d5 | 2009-11-12 09:34:52 +0000 | [diff] [blame] | 10430 | } |
Wang Xiaoguang | 1851309 | 2016-07-25 15:51:40 +0800 | [diff] [blame] | 10431 | if (cur_offset < end) |
| 10432 | btrfs_free_reserved_data_space(inode, cur_offset, |
| 10433 | end - cur_offset + 1); |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 10434 | return ret; |
| 10435 | } |
| 10436 | |
Josef Bacik | 0af3d00 | 2010-06-21 14:48:16 -0400 | [diff] [blame] | 10437 | int btrfs_prealloc_file_range(struct inode *inode, int mode, |
| 10438 | u64 start, u64 num_bytes, u64 min_size, |
| 10439 | loff_t actual_len, u64 *alloc_hint) |
| 10440 | { |
| 10441 | return __btrfs_prealloc_file_range(inode, mode, start, num_bytes, |
| 10442 | min_size, actual_len, alloc_hint, |
| 10443 | NULL); |
| 10444 | } |
| 10445 | |
| 10446 | int btrfs_prealloc_file_range_trans(struct inode *inode, |
| 10447 | struct btrfs_trans_handle *trans, int mode, |
| 10448 | u64 start, u64 num_bytes, u64 min_size, |
| 10449 | loff_t actual_len, u64 *alloc_hint) |
| 10450 | { |
| 10451 | return __btrfs_prealloc_file_range(inode, mode, start, num_bytes, |
| 10452 | min_size, actual_len, alloc_hint, trans); |
| 10453 | } |
| 10454 | |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 10455 | static int btrfs_set_page_dirty(struct page *page) |
| 10456 | { |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 10457 | return __set_page_dirty_nobuffers(page); |
| 10458 | } |
| 10459 | |
Al Viro | 10556cb | 2011-06-20 19:28:19 -0400 | [diff] [blame] | 10460 | static int btrfs_permission(struct inode *inode, int mask) |
Yan | fdebe2b | 2008-01-14 13:26:08 -0500 | [diff] [blame] | 10461 | { |
Li Zefan | b83cc96 | 2010-12-20 16:04:08 +0800 | [diff] [blame] | 10462 | struct btrfs_root *root = BTRFS_I(inode)->root; |
Jeff Mahoney | cb6db4e | 2011-08-15 17:27:21 +0000 | [diff] [blame] | 10463 | umode_t mode = inode->i_mode; |
Li Zefan | b83cc96 | 2010-12-20 16:04:08 +0800 | [diff] [blame] | 10464 | |
Jeff Mahoney | cb6db4e | 2011-08-15 17:27:21 +0000 | [diff] [blame] | 10465 | if (mask & MAY_WRITE && |
| 10466 | (S_ISREG(mode) || S_ISDIR(mode) || S_ISLNK(mode))) { |
| 10467 | if (btrfs_root_readonly(root)) |
| 10468 | return -EROFS; |
| 10469 | if (BTRFS_I(inode)->flags & BTRFS_INODE_READONLY) |
| 10470 | return -EACCES; |
| 10471 | } |
Al Viro | 2830ba7 | 2011-06-20 19:16:29 -0400 | [diff] [blame] | 10472 | return generic_permission(inode, mask); |
Yan | fdebe2b | 2008-01-14 13:26:08 -0500 | [diff] [blame] | 10473 | } |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 10474 | |
Filipe Manana | ef3b9af | 2014-04-27 20:40:45 +0100 | [diff] [blame] | 10475 | static int btrfs_tmpfile(struct inode *dir, struct dentry *dentry, umode_t mode) |
| 10476 | { |
| 10477 | struct btrfs_trans_handle *trans; |
| 10478 | struct btrfs_root *root = BTRFS_I(dir)->root; |
| 10479 | struct inode *inode = NULL; |
| 10480 | u64 objectid; |
| 10481 | u64 index; |
| 10482 | int ret = 0; |
| 10483 | |
| 10484 | /* |
| 10485 | * 5 units required for adding orphan entry |
| 10486 | */ |
| 10487 | trans = btrfs_start_transaction(root, 5); |
| 10488 | if (IS_ERR(trans)) |
| 10489 | return PTR_ERR(trans); |
| 10490 | |
| 10491 | ret = btrfs_find_free_ino(root, &objectid); |
| 10492 | if (ret) |
| 10493 | goto out; |
| 10494 | |
| 10495 | inode = btrfs_new_inode(trans, root, dir, NULL, 0, |
| 10496 | btrfs_ino(dir), objectid, mode, &index); |
| 10497 | if (IS_ERR(inode)) { |
| 10498 | ret = PTR_ERR(inode); |
| 10499 | inode = NULL; |
| 10500 | goto out; |
| 10501 | } |
| 10502 | |
Filipe Manana | ef3b9af | 2014-04-27 20:40:45 +0100 | [diff] [blame] | 10503 | inode->i_fop = &btrfs_file_operations; |
| 10504 | inode->i_op = &btrfs_file_inode_operations; |
| 10505 | |
| 10506 | inode->i_mapping->a_ops = &btrfs_aops; |
Filipe Manana | ef3b9af | 2014-04-27 20:40:45 +0100 | [diff] [blame] | 10507 | BTRFS_I(inode)->io_tree.ops = &btrfs_extent_io_ops; |
| 10508 | |
Chris Mason | b0d5d10 | 2014-09-08 13:08:51 -0700 | [diff] [blame] | 10509 | ret = btrfs_init_inode_security(trans, inode, dir, NULL); |
| 10510 | if (ret) |
| 10511 | goto out_inode; |
| 10512 | |
| 10513 | ret = btrfs_update_inode(trans, root, inode); |
| 10514 | if (ret) |
| 10515 | goto out_inode; |
Filipe Manana | ef3b9af | 2014-04-27 20:40:45 +0100 | [diff] [blame] | 10516 | ret = btrfs_orphan_add(trans, inode); |
| 10517 | if (ret) |
Chris Mason | b0d5d10 | 2014-09-08 13:08:51 -0700 | [diff] [blame] | 10518 | goto out_inode; |
Filipe Manana | ef3b9af | 2014-04-27 20:40:45 +0100 | [diff] [blame] | 10519 | |
Filipe Manana | 5762b5c | 2014-08-01 00:10:32 +0100 | [diff] [blame] | 10520 | /* |
| 10521 | * We set number of links to 0 in btrfs_new_inode(), and here we set |
| 10522 | * it to 1 because d_tmpfile() will issue a warning if the count is 0, |
| 10523 | * through: |
| 10524 | * |
| 10525 | * d_tmpfile() -> inode_dec_link_count() -> drop_nlink() |
| 10526 | */ |
| 10527 | set_nlink(inode, 1); |
Chris Mason | b0d5d10 | 2014-09-08 13:08:51 -0700 | [diff] [blame] | 10528 | unlock_new_inode(inode); |
Filipe Manana | ef3b9af | 2014-04-27 20:40:45 +0100 | [diff] [blame] | 10529 | d_tmpfile(dentry, inode); |
| 10530 | mark_inode_dirty(inode); |
| 10531 | |
| 10532 | out: |
| 10533 | btrfs_end_transaction(trans, root); |
| 10534 | if (ret) |
| 10535 | iput(inode); |
| 10536 | btrfs_balance_delayed_items(root); |
| 10537 | btrfs_btree_balance_dirty(root); |
Filipe Manana | ef3b9af | 2014-04-27 20:40:45 +0100 | [diff] [blame] | 10538 | return ret; |
Chris Mason | b0d5d10 | 2014-09-08 13:08:51 -0700 | [diff] [blame] | 10539 | |
| 10540 | out_inode: |
| 10541 | unlock_new_inode(inode); |
| 10542 | goto out; |
| 10543 | |
Filipe Manana | ef3b9af | 2014-04-27 20:40:45 +0100 | [diff] [blame] | 10544 | } |
| 10545 | |
Filipe Manana | b38ef71 | 2014-11-13 17:01:45 +0000 | [diff] [blame] | 10546 | /* Inspired by filemap_check_errors() */ |
| 10547 | int btrfs_inode_check_errors(struct inode *inode) |
| 10548 | { |
| 10549 | int ret = 0; |
| 10550 | |
| 10551 | if (test_bit(AS_ENOSPC, &inode->i_mapping->flags) && |
| 10552 | test_and_clear_bit(AS_ENOSPC, &inode->i_mapping->flags)) |
| 10553 | ret = -ENOSPC; |
| 10554 | if (test_bit(AS_EIO, &inode->i_mapping->flags) && |
| 10555 | test_and_clear_bit(AS_EIO, &inode->i_mapping->flags)) |
| 10556 | ret = -EIO; |
| 10557 | |
| 10558 | return ret; |
| 10559 | } |
| 10560 | |
Alexey Dobriyan | 6e1d5dc | 2009-09-21 17:01:11 -0700 | [diff] [blame] | 10561 | static const struct inode_operations btrfs_dir_inode_operations = { |
Chris Mason | 3394e16 | 2008-11-17 20:42:26 -0500 | [diff] [blame] | 10562 | .getattr = btrfs_getattr, |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 10563 | .lookup = btrfs_lookup, |
| 10564 | .create = btrfs_create, |
| 10565 | .unlink = btrfs_unlink, |
| 10566 | .link = btrfs_link, |
| 10567 | .mkdir = btrfs_mkdir, |
| 10568 | .rmdir = btrfs_rmdir, |
Miklos Szeredi | 80ace85 | 2014-07-23 15:15:32 +0200 | [diff] [blame] | 10569 | .rename2 = btrfs_rename2, |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 10570 | .symlink = btrfs_symlink, |
| 10571 | .setattr = btrfs_setattr, |
Josef Bacik | 618e21d | 2007-07-11 10:18:17 -0400 | [diff] [blame] | 10572 | .mknod = btrfs_mknod, |
Andreas Gruenbacher | e0d46f5 | 2016-04-22 22:36:44 +0200 | [diff] [blame] | 10573 | .setxattr = generic_setxattr, |
Andreas Gruenbacher | 9172abb | 2015-12-02 14:44:37 +0100 | [diff] [blame] | 10574 | .getxattr = generic_getxattr, |
Josef Bacik | 5103e94 | 2007-11-16 11:45:54 -0500 | [diff] [blame] | 10575 | .listxattr = btrfs_listxattr, |
Andreas Gruenbacher | e0d46f5 | 2016-04-22 22:36:44 +0200 | [diff] [blame] | 10576 | .removexattr = generic_removexattr, |
Yan | fdebe2b | 2008-01-14 13:26:08 -0500 | [diff] [blame] | 10577 | .permission = btrfs_permission, |
Christoph Hellwig | 4e34e71 | 2011-07-23 17:37:31 +0200 | [diff] [blame] | 10578 | .get_acl = btrfs_get_acl, |
Christoph Hellwig | 996a710 | 2013-12-20 05:16:43 -0800 | [diff] [blame] | 10579 | .set_acl = btrfs_set_acl, |
Guangyu Sun | 93fd63c | 2013-09-16 10:42:03 -0700 | [diff] [blame] | 10580 | .update_time = btrfs_update_time, |
Filipe Manana | ef3b9af | 2014-04-27 20:40:45 +0100 | [diff] [blame] | 10581 | .tmpfile = btrfs_tmpfile, |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 10582 | }; |
Alexey Dobriyan | 6e1d5dc | 2009-09-21 17:01:11 -0700 | [diff] [blame] | 10583 | static const struct inode_operations btrfs_dir_ro_inode_operations = { |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 10584 | .lookup = btrfs_lookup, |
Yan | fdebe2b | 2008-01-14 13:26:08 -0500 | [diff] [blame] | 10585 | .permission = btrfs_permission, |
Christoph Hellwig | 4e34e71 | 2011-07-23 17:37:31 +0200 | [diff] [blame] | 10586 | .get_acl = btrfs_get_acl, |
Christoph Hellwig | 996a710 | 2013-12-20 05:16:43 -0800 | [diff] [blame] | 10587 | .set_acl = btrfs_set_acl, |
Guangyu Sun | 93fd63c | 2013-09-16 10:42:03 -0700 | [diff] [blame] | 10588 | .update_time = btrfs_update_time, |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 10589 | }; |
Yan, Zheng | 76dda93 | 2009-09-21 16:00:26 -0400 | [diff] [blame] | 10590 | |
Alexey Dobriyan | 828c095 | 2009-10-01 15:43:56 -0700 | [diff] [blame] | 10591 | static const struct file_operations btrfs_dir_file_operations = { |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 10592 | .llseek = generic_file_llseek, |
| 10593 | .read = generic_read_dir, |
Omar Sandoval | 02dbfc9 | 2016-05-20 13:50:33 -0700 | [diff] [blame] | 10594 | .iterate_shared = btrfs_real_readdir, |
Christoph Hellwig | 34287aa | 2007-09-14 10:22:47 -0400 | [diff] [blame] | 10595 | .unlocked_ioctl = btrfs_ioctl, |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 10596 | #ifdef CONFIG_COMPAT |
Luke Dashjr | 4c63c24 | 2015-10-29 08:22:21 +0000 | [diff] [blame] | 10597 | .compat_ioctl = btrfs_compat_ioctl, |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 10598 | #endif |
Sage Weil | 6bf13c0 | 2008-06-10 10:07:39 -0400 | [diff] [blame] | 10599 | .release = btrfs_release_file, |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 10600 | .fsync = btrfs_sync_file, |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 10601 | }; |
| 10602 | |
David Sterba | 20e5506 | 2015-11-19 11:42:28 +0100 | [diff] [blame] | 10603 | static const struct extent_io_ops btrfs_extent_io_ops = { |
Chris Mason | 07157aa | 2007-08-30 08:50:51 -0400 | [diff] [blame] | 10604 | .fill_delalloc = run_delalloc_range, |
Chris Mason | 065631f | 2008-02-20 12:07:25 -0500 | [diff] [blame] | 10605 | .submit_bio_hook = btrfs_submit_bio_hook, |
Chris Mason | 239b14b | 2008-03-24 15:02:07 -0400 | [diff] [blame] | 10606 | .merge_bio_hook = btrfs_merge_bio_hook, |
Chris Mason | 07157aa | 2007-08-30 08:50:51 -0400 | [diff] [blame] | 10607 | .readpage_end_io_hook = btrfs_readpage_end_io_hook, |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 10608 | .writepage_end_io_hook = btrfs_writepage_end_io_hook, |
Chris Mason | 247e743 | 2008-07-17 12:53:51 -0400 | [diff] [blame] | 10609 | .writepage_start_hook = btrfs_writepage_start_hook, |
Chris Mason | b0c68f8 | 2008-01-31 11:05:37 -0500 | [diff] [blame] | 10610 | .set_bit_hook = btrfs_set_bit_hook, |
| 10611 | .clear_bit_hook = btrfs_clear_bit_hook, |
Josef Bacik | 9ed74f2 | 2009-09-11 16:12:44 -0400 | [diff] [blame] | 10612 | .merge_extent_hook = btrfs_merge_extent_hook, |
| 10613 | .split_extent_hook = btrfs_split_extent_hook, |
Chris Mason | 07157aa | 2007-08-30 08:50:51 -0400 | [diff] [blame] | 10614 | }; |
| 10615 | |
Chris Mason | 3505439 | 2009-01-21 13:11:13 -0500 | [diff] [blame] | 10616 | /* |
| 10617 | * btrfs doesn't support the bmap operation because swapfiles |
| 10618 | * use bmap to make a mapping of extents in the file. They assume |
| 10619 | * these extents won't change over the life of the file and they |
| 10620 | * use the bmap result to do IO directly to the drive. |
| 10621 | * |
| 10622 | * the btrfs bmap call would return logical addresses that aren't |
| 10623 | * suitable for IO and they also will change frequently as COW |
| 10624 | * operations happen. So, swapfile + btrfs == corruption. |
| 10625 | * |
| 10626 | * For now we're avoiding this by dropping bmap. |
| 10627 | */ |
Alexey Dobriyan | 7f09410 | 2009-09-21 17:01:10 -0700 | [diff] [blame] | 10628 | static const struct address_space_operations btrfs_aops = { |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 10629 | .readpage = btrfs_readpage, |
| 10630 | .writepage = btrfs_writepage, |
Chris Mason | b293f02 | 2007-11-01 19:45:34 -0400 | [diff] [blame] | 10631 | .writepages = btrfs_writepages, |
Chris Mason | 3ab2fb5 | 2007-11-08 10:59:22 -0500 | [diff] [blame] | 10632 | .readpages = btrfs_readpages, |
Chris Mason | 1643298 | 2008-04-10 10:23:21 -0400 | [diff] [blame] | 10633 | .direct_IO = btrfs_direct_IO, |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 10634 | .invalidatepage = btrfs_invalidatepage, |
| 10635 | .releasepage = btrfs_releasepage, |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 10636 | .set_page_dirty = btrfs_set_page_dirty, |
Andi Kleen | 465fdd9 | 2009-09-16 11:50:18 +0200 | [diff] [blame] | 10637 | .error_remove_page = generic_error_remove_page, |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 10638 | }; |
| 10639 | |
Alexey Dobriyan | 7f09410 | 2009-09-21 17:01:10 -0700 | [diff] [blame] | 10640 | static const struct address_space_operations btrfs_symlink_aops = { |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 10641 | .readpage = btrfs_readpage, |
| 10642 | .writepage = btrfs_writepage, |
Chris Mason | 2bf5a72 | 2007-08-30 11:54:02 -0400 | [diff] [blame] | 10643 | .invalidatepage = btrfs_invalidatepage, |
| 10644 | .releasepage = btrfs_releasepage, |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 10645 | }; |
| 10646 | |
Alexey Dobriyan | 6e1d5dc | 2009-09-21 17:01:11 -0700 | [diff] [blame] | 10647 | static const struct inode_operations btrfs_file_inode_operations = { |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 10648 | .getattr = btrfs_getattr, |
| 10649 | .setattr = btrfs_setattr, |
Andreas Gruenbacher | e0d46f5 | 2016-04-22 22:36:44 +0200 | [diff] [blame] | 10650 | .setxattr = generic_setxattr, |
Andreas Gruenbacher | 9172abb | 2015-12-02 14:44:37 +0100 | [diff] [blame] | 10651 | .getxattr = generic_getxattr, |
Josef Bacik | 5103e94 | 2007-11-16 11:45:54 -0500 | [diff] [blame] | 10652 | .listxattr = btrfs_listxattr, |
Andreas Gruenbacher | e0d46f5 | 2016-04-22 22:36:44 +0200 | [diff] [blame] | 10653 | .removexattr = generic_removexattr, |
Yan | fdebe2b | 2008-01-14 13:26:08 -0500 | [diff] [blame] | 10654 | .permission = btrfs_permission, |
Yehuda Sadeh | 1506fcc | 2009-01-21 14:39:14 -0500 | [diff] [blame] | 10655 | .fiemap = btrfs_fiemap, |
Christoph Hellwig | 4e34e71 | 2011-07-23 17:37:31 +0200 | [diff] [blame] | 10656 | .get_acl = btrfs_get_acl, |
Christoph Hellwig | 996a710 | 2013-12-20 05:16:43 -0800 | [diff] [blame] | 10657 | .set_acl = btrfs_set_acl, |
Josef Bacik | e41f941 | 2012-03-26 09:46:47 -0400 | [diff] [blame] | 10658 | .update_time = btrfs_update_time, |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 10659 | }; |
Alexey Dobriyan | 6e1d5dc | 2009-09-21 17:01:11 -0700 | [diff] [blame] | 10660 | static const struct inode_operations btrfs_special_inode_operations = { |
Josef Bacik | 618e21d | 2007-07-11 10:18:17 -0400 | [diff] [blame] | 10661 | .getattr = btrfs_getattr, |
| 10662 | .setattr = btrfs_setattr, |
Yan | fdebe2b | 2008-01-14 13:26:08 -0500 | [diff] [blame] | 10663 | .permission = btrfs_permission, |
Andreas Gruenbacher | e0d46f5 | 2016-04-22 22:36:44 +0200 | [diff] [blame] | 10664 | .setxattr = generic_setxattr, |
Andreas Gruenbacher | 9172abb | 2015-12-02 14:44:37 +0100 | [diff] [blame] | 10665 | .getxattr = generic_getxattr, |
Josef Bacik | 33268ea | 2008-07-24 12:16:36 -0400 | [diff] [blame] | 10666 | .listxattr = btrfs_listxattr, |
Andreas Gruenbacher | e0d46f5 | 2016-04-22 22:36:44 +0200 | [diff] [blame] | 10667 | .removexattr = generic_removexattr, |
Christoph Hellwig | 4e34e71 | 2011-07-23 17:37:31 +0200 | [diff] [blame] | 10668 | .get_acl = btrfs_get_acl, |
Christoph Hellwig | 996a710 | 2013-12-20 05:16:43 -0800 | [diff] [blame] | 10669 | .set_acl = btrfs_set_acl, |
Josef Bacik | e41f941 | 2012-03-26 09:46:47 -0400 | [diff] [blame] | 10670 | .update_time = btrfs_update_time, |
Josef Bacik | 618e21d | 2007-07-11 10:18:17 -0400 | [diff] [blame] | 10671 | }; |
Alexey Dobriyan | 6e1d5dc | 2009-09-21 17:01:11 -0700 | [diff] [blame] | 10672 | static const struct inode_operations btrfs_symlink_inode_operations = { |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 10673 | .readlink = generic_readlink, |
Al Viro | 6b25539 | 2015-11-17 10:20:54 -0500 | [diff] [blame] | 10674 | .get_link = page_get_link, |
Li Zefan | f209561 | 2010-11-19 02:05:24 +0000 | [diff] [blame] | 10675 | .getattr = btrfs_getattr, |
Josef Bacik | 22c44fe | 2011-11-30 10:45:38 -0500 | [diff] [blame] | 10676 | .setattr = btrfs_setattr, |
Yan | fdebe2b | 2008-01-14 13:26:08 -0500 | [diff] [blame] | 10677 | .permission = btrfs_permission, |
Andreas Gruenbacher | e0d46f5 | 2016-04-22 22:36:44 +0200 | [diff] [blame] | 10678 | .setxattr = generic_setxattr, |
Andreas Gruenbacher | 9172abb | 2015-12-02 14:44:37 +0100 | [diff] [blame] | 10679 | .getxattr = generic_getxattr, |
Jim Owens | 0279b4c | 2009-02-04 09:29:13 -0500 | [diff] [blame] | 10680 | .listxattr = btrfs_listxattr, |
Andreas Gruenbacher | e0d46f5 | 2016-04-22 22:36:44 +0200 | [diff] [blame] | 10681 | .removexattr = generic_removexattr, |
Josef Bacik | e41f941 | 2012-03-26 09:46:47 -0400 | [diff] [blame] | 10682 | .update_time = btrfs_update_time, |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 10683 | }; |
Yan, Zheng | 76dda93 | 2009-09-21 16:00:26 -0400 | [diff] [blame] | 10684 | |
Alexey Dobriyan | 82d339d | 2009-10-09 09:54:36 -0400 | [diff] [blame] | 10685 | const struct dentry_operations btrfs_dentry_operations = { |
Yan, Zheng | 76dda93 | 2009-09-21 16:00:26 -0400 | [diff] [blame] | 10686 | .d_delete = btrfs_dentry_delete, |
Josef Bacik | b4aff1f | 2011-06-28 16:18:59 -0400 | [diff] [blame] | 10687 | .d_release = btrfs_dentry_release, |
Yan, Zheng | 76dda93 | 2009-09-21 16:00:26 -0400 | [diff] [blame] | 10688 | }; |