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 | f03d930 | 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 | */ |
Qu Wenruo | ba8b04c | 2016-07-19 16:50:36 +0800 | [diff] [blame] | 563 | extent_clear_unlock_delalloc(inode, start, end, end, |
| 564 | NULL, clear_flags, |
| 565 | PAGE_UNLOCK | |
Josef Bacik | c2790a2 | 2013-07-29 11:20:47 -0400 | [diff] [blame] | 566 | PAGE_CLEAR_DIRTY | |
| 567 | PAGE_SET_WRITEBACK | |
Filipe Manana | e6eb431 | 2014-10-10 10:45:12 +0100 | [diff] [blame] | 568 | page_error_op | |
Josef Bacik | c2790a2 | 2013-07-29 11:20:47 -0400 | [diff] [blame] | 569 | PAGE_END_WRITEBACK); |
Wang Xiaoguang | 1851309 | 2016-07-25 15:51:40 +0800 | [diff] [blame] | 570 | btrfs_free_reserved_data_space_noquota(inode, start, |
| 571 | end - start + 1); |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 572 | goto free_pages_out; |
| 573 | } |
| 574 | } |
| 575 | |
| 576 | if (will_compress) { |
| 577 | /* |
| 578 | * we aren't doing an inline extent round the compressed size |
| 579 | * up to a block size boundary so the allocator does sane |
| 580 | * things |
| 581 | */ |
Qu Wenruo | fda2832 | 2013-02-26 08:10:22 +0000 | [diff] [blame] | 582 | total_compressed = ALIGN(total_compressed, blocksize); |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 583 | |
| 584 | /* |
| 585 | * one last check to make sure the compression is really a |
| 586 | * win, compare the page count read with the blocks on disk |
| 587 | */ |
Kirill A. Shutemov | 09cbfea | 2016-04-01 15:29:47 +0300 | [diff] [blame] | 588 | total_in = ALIGN(total_in, PAGE_SIZE); |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 589 | if (total_compressed >= total_in) { |
| 590 | will_compress = 0; |
| 591 | } else { |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 592 | num_bytes = total_in; |
Ashish Samant | c8bb0c8 | 2016-03-25 19:01:33 -0700 | [diff] [blame] | 593 | *num_added += 1; |
| 594 | |
| 595 | /* |
| 596 | * The async work queues will take care of doing actual |
| 597 | * allocation on disk for these compressed pages, and |
| 598 | * will submit them to the elevator. |
| 599 | */ |
| 600 | add_async_extent(async_cow, start, num_bytes, |
| 601 | total_compressed, pages, nr_pages_ret, |
| 602 | compress_type); |
| 603 | |
| 604 | if (start + num_bytes < end) { |
| 605 | start += num_bytes; |
| 606 | pages = NULL; |
| 607 | cond_resched(); |
| 608 | goto again; |
| 609 | } |
| 610 | return; |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 611 | } |
| 612 | } |
Ashish Samant | c8bb0c8 | 2016-03-25 19:01:33 -0700 | [diff] [blame] | 613 | if (pages) { |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 614 | /* |
| 615 | * the compression code ran but failed to make things smaller, |
| 616 | * free any pages it allocated and our page pointer array |
| 617 | */ |
| 618 | for (i = 0; i < nr_pages_ret; i++) { |
Chris Mason | 70b99e6 | 2008-10-31 12:46:39 -0400 | [diff] [blame] | 619 | WARN_ON(pages[i]->mapping); |
Kirill A. Shutemov | 09cbfea | 2016-04-01 15:29:47 +0300 | [diff] [blame] | 620 | put_page(pages[i]); |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 621 | } |
| 622 | kfree(pages); |
| 623 | pages = NULL; |
| 624 | total_compressed = 0; |
| 625 | nr_pages_ret = 0; |
| 626 | |
| 627 | /* flag the file so we don't compress in the future */ |
Jeff Mahoney | 3cdde22 | 2016-06-09 21:38:35 -0400 | [diff] [blame] | 628 | if (!btrfs_test_opt(root->fs_info, FORCE_COMPRESS) && |
Chris Mason | 1e701a3 | 2010-03-11 09:42:04 -0500 | [diff] [blame] | 629 | !(BTRFS_I(inode)->force_compress)) { |
Chris Mason | a555f81 | 2010-01-28 16:18:15 -0500 | [diff] [blame] | 630 | BTRFS_I(inode)->flags |= BTRFS_INODE_NOCOMPRESS; |
Chris Mason | 1e701a3 | 2010-03-11 09:42:04 -0500 | [diff] [blame] | 631 | } |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 632 | } |
Chris Mason | f03d930 | 2009-02-04 09:31:06 -0500 | [diff] [blame] | 633 | cleanup_and_bail_uncompressed: |
Ashish Samant | c8bb0c8 | 2016-03-25 19:01:33 -0700 | [diff] [blame] | 634 | /* |
| 635 | * No compression, but we still need to write the pages in the file |
| 636 | * we've been given so far. redirty the locked page if it corresponds |
| 637 | * to our extent and set things up for the async work queue to run |
| 638 | * cow_file_range to do the normal delalloc dance. |
| 639 | */ |
| 640 | if (page_offset(locked_page) >= start && |
| 641 | page_offset(locked_page) <= end) |
| 642 | __set_page_dirty_nobuffers(locked_page); |
| 643 | /* unlocked later on in the async handlers */ |
| 644 | |
| 645 | if (redirty) |
| 646 | extent_range_redirty_for_io(inode, start, end); |
| 647 | add_async_extent(async_cow, start, end - start + 1, 0, NULL, 0, |
| 648 | BTRFS_COMPRESS_NONE); |
| 649 | *num_added += 1; |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 650 | |
Filipe Manana | c44f649 | 2014-10-09 21:15:44 +0100 | [diff] [blame] | 651 | return; |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 652 | |
| 653 | free_pages_out: |
| 654 | for (i = 0; i < nr_pages_ret; i++) { |
| 655 | WARN_ON(pages[i]->mapping); |
Kirill A. Shutemov | 09cbfea | 2016-04-01 15:29:47 +0300 | [diff] [blame] | 656 | put_page(pages[i]); |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 657 | } |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 658 | kfree(pages); |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 659 | } |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 660 | |
Filipe Manana | 40ae837 | 2014-10-06 22:14:24 +0100 | [diff] [blame] | 661 | static void free_async_extent_pages(struct async_extent *async_extent) |
| 662 | { |
| 663 | int i; |
| 664 | |
| 665 | if (!async_extent->pages) |
| 666 | return; |
| 667 | |
| 668 | for (i = 0; i < async_extent->nr_pages; i++) { |
| 669 | WARN_ON(async_extent->pages[i]->mapping); |
Kirill A. Shutemov | 09cbfea | 2016-04-01 15:29:47 +0300 | [diff] [blame] | 670 | put_page(async_extent->pages[i]); |
Filipe Manana | 40ae837 | 2014-10-06 22:14:24 +0100 | [diff] [blame] | 671 | } |
| 672 | kfree(async_extent->pages); |
| 673 | async_extent->nr_pages = 0; |
| 674 | async_extent->pages = NULL; |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 675 | } |
| 676 | |
| 677 | /* |
| 678 | * phase two of compressed writeback. This is the ordered portion |
| 679 | * of the code, which only gets called in the order the work was |
| 680 | * queued. We walk all the async extents created by compress_file_range |
| 681 | * and send them down to the disk. |
| 682 | */ |
Filipe Manana | dec8f17 | 2014-10-06 22:14:26 +0100 | [diff] [blame] | 683 | static noinline void submit_compressed_extents(struct inode *inode, |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 684 | struct async_cow *async_cow) |
| 685 | { |
| 686 | struct async_extent *async_extent; |
| 687 | u64 alloc_hint = 0; |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 688 | struct btrfs_key ins; |
| 689 | struct extent_map *em; |
| 690 | struct btrfs_root *root = BTRFS_I(inode)->root; |
| 691 | struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree; |
| 692 | struct extent_io_tree *io_tree; |
Josef Bacik | f5a84ee3 | 2009-11-10 21:23:48 -0500 | [diff] [blame] | 693 | int ret = 0; |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 694 | |
Josef Bacik | 3e04e7f | 2013-02-06 16:49:15 -0500 | [diff] [blame] | 695 | again: |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 696 | while (!list_empty(&async_cow->extents)) { |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 697 | async_extent = list_entry(async_cow->extents.next, |
| 698 | struct async_extent, list); |
| 699 | list_del(&async_extent->list); |
| 700 | |
| 701 | io_tree = &BTRFS_I(inode)->io_tree; |
| 702 | |
Josef Bacik | f5a84ee3 | 2009-11-10 21:23:48 -0500 | [diff] [blame] | 703 | retry: |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 704 | /* did the compression code fall back to uncompressed IO? */ |
| 705 | if (!async_extent->pages) { |
| 706 | int page_started = 0; |
| 707 | unsigned long nr_written = 0; |
| 708 | |
| 709 | lock_extent(io_tree, async_extent->start, |
Josef Bacik | 2ac55d4 | 2010-02-03 19:33:23 +0000 | [diff] [blame] | 710 | async_extent->start + |
Jeff Mahoney | d008237 | 2012-03-01 14:57:19 +0100 | [diff] [blame] | 711 | async_extent->ram_size - 1); |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 712 | |
| 713 | /* allocate blocks */ |
Josef Bacik | f5a84ee3 | 2009-11-10 21:23:48 -0500 | [diff] [blame] | 714 | ret = cow_file_range(inode, async_cow->locked_page, |
| 715 | async_extent->start, |
| 716 | async_extent->start + |
| 717 | async_extent->ram_size - 1, |
Wang Xiaoguang | dda3245 | 2016-07-11 11:05:29 +0800 | [diff] [blame] | 718 | async_extent->start + |
| 719 | async_extent->ram_size - 1, |
| 720 | &page_started, &nr_written, 0, |
| 721 | NULL); |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 722 | |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 723 | /* JDM XXX */ |
| 724 | |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 725 | /* |
| 726 | * if page_started, cow_file_range inserted an |
| 727 | * inline extent and took care of all the unlocking |
| 728 | * and IO for us. Otherwise, we need to submit |
| 729 | * all those pages down to the drive. |
| 730 | */ |
Josef Bacik | f5a84ee3 | 2009-11-10 21:23:48 -0500 | [diff] [blame] | 731 | if (!page_started && !ret) |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 732 | extent_write_locked_range(io_tree, |
| 733 | inode, async_extent->start, |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 734 | async_extent->start + |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 735 | async_extent->ram_size - 1, |
| 736 | btrfs_get_extent, |
| 737 | WB_SYNC_ALL); |
Josef Bacik | 3e04e7f | 2013-02-06 16:49:15 -0500 | [diff] [blame] | 738 | else if (ret) |
| 739 | unlock_page(async_cow->locked_page); |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 740 | kfree(async_extent); |
| 741 | cond_resched(); |
| 742 | continue; |
| 743 | } |
| 744 | |
| 745 | lock_extent(io_tree, async_extent->start, |
Jeff Mahoney | d008237 | 2012-03-01 14:57:19 +0100 | [diff] [blame] | 746 | async_extent->start + async_extent->ram_size - 1); |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 747 | |
Wang Xiaoguang | 1851309 | 2016-07-25 15:51:40 +0800 | [diff] [blame] | 748 | ret = btrfs_reserve_extent(root, async_extent->ram_size, |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 749 | async_extent->compressed_size, |
| 750 | async_extent->compressed_size, |
Miao Xie | e570fd2 | 2014-06-19 10:42:50 +0800 | [diff] [blame] | 751 | 0, alloc_hint, &ins, 1, 1); |
Josef Bacik | f5a84ee3 | 2009-11-10 21:23:48 -0500 | [diff] [blame] | 752 | if (ret) { |
Filipe Manana | 40ae837 | 2014-10-06 22:14:24 +0100 | [diff] [blame] | 753 | free_async_extent_pages(async_extent); |
Josef Bacik | 3e04e7f | 2013-02-06 16:49:15 -0500 | [diff] [blame] | 754 | |
Josef Bacik | fdf8e2e | 2013-06-14 16:58:23 -0400 | [diff] [blame] | 755 | if (ret == -ENOSPC) { |
| 756 | unlock_extent(io_tree, async_extent->start, |
| 757 | async_extent->start + |
| 758 | async_extent->ram_size - 1); |
Liu Bo | ce62003 | 2014-07-24 22:48:05 +0800 | [diff] [blame] | 759 | |
| 760 | /* |
| 761 | * we need to redirty the pages if we decide to |
| 762 | * fallback to uncompressed IO, otherwise we |
| 763 | * will not submit these pages down to lower |
| 764 | * layers. |
| 765 | */ |
| 766 | extent_range_redirty_for_io(inode, |
| 767 | async_extent->start, |
| 768 | async_extent->start + |
| 769 | async_extent->ram_size - 1); |
| 770 | |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 771 | goto retry; |
Josef Bacik | fdf8e2e | 2013-06-14 16:58:23 -0400 | [diff] [blame] | 772 | } |
Josef Bacik | 3e04e7f | 2013-02-06 16:49:15 -0500 | [diff] [blame] | 773 | goto out_free; |
Josef Bacik | f5a84ee3 | 2009-11-10 21:23:48 -0500 | [diff] [blame] | 774 | } |
Yan, Zheng | c216775 | 2009-11-12 09:34:21 +0000 | [diff] [blame] | 775 | /* |
| 776 | * here we're doing allocation and writeback of the |
| 777 | * compressed pages |
| 778 | */ |
| 779 | btrfs_drop_extent_cache(inode, async_extent->start, |
| 780 | async_extent->start + |
| 781 | async_extent->ram_size - 1, 0); |
| 782 | |
David Sterba | 172ddd6 | 2011-04-21 00:48:27 +0200 | [diff] [blame] | 783 | em = alloc_extent_map(); |
Liu Bo | b9aa55b | 2013-05-14 02:12:15 +0000 | [diff] [blame] | 784 | if (!em) { |
| 785 | ret = -ENOMEM; |
Josef Bacik | 3e04e7f | 2013-02-06 16:49:15 -0500 | [diff] [blame] | 786 | goto out_free_reserve; |
Liu Bo | b9aa55b | 2013-05-14 02:12:15 +0000 | [diff] [blame] | 787 | } |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 788 | em->start = async_extent->start; |
| 789 | em->len = async_extent->ram_size; |
Chris Mason | 445a694 | 2008-11-10 11:53:33 -0500 | [diff] [blame] | 790 | em->orig_start = em->start; |
Josef Bacik | 2ab28f3 | 2012-10-12 15:27:49 -0400 | [diff] [blame] | 791 | em->mod_start = em->start; |
| 792 | em->mod_len = em->len; |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 793 | |
| 794 | em->block_start = ins.objectid; |
| 795 | em->block_len = ins.offset; |
Josef Bacik | b493968 | 2012-12-03 10:31:19 -0500 | [diff] [blame] | 796 | em->orig_block_len = ins.offset; |
Josef Bacik | cc95bef | 2013-04-04 14:31:27 -0400 | [diff] [blame] | 797 | em->ram_bytes = async_extent->ram_size; |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 798 | em->bdev = root->fs_info->fs_devices->latest_bdev; |
Li Zefan | 261507a0 | 2010-12-17 14:21:50 +0800 | [diff] [blame] | 799 | em->compress_type = async_extent->compress_type; |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 800 | set_bit(EXTENT_FLAG_PINNED, &em->flags); |
| 801 | set_bit(EXTENT_FLAG_COMPRESSED, &em->flags); |
Josef Bacik | 70c8a91 | 2012-10-11 16:54:30 -0400 | [diff] [blame] | 802 | em->generation = -1; |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 803 | |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 804 | while (1) { |
Chris Mason | 890871b | 2009-09-02 16:24:52 -0400 | [diff] [blame] | 805 | write_lock(&em_tree->lock); |
Josef Bacik | 09a2a8f9 | 2013-04-05 16:51:15 -0400 | [diff] [blame] | 806 | ret = add_extent_mapping(em_tree, em, 1); |
Chris Mason | 890871b | 2009-09-02 16:24:52 -0400 | [diff] [blame] | 807 | write_unlock(&em_tree->lock); |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 808 | if (ret != -EEXIST) { |
| 809 | free_extent_map(em); |
| 810 | break; |
| 811 | } |
| 812 | btrfs_drop_extent_cache(inode, async_extent->start, |
| 813 | async_extent->start + |
| 814 | async_extent->ram_size - 1, 0); |
| 815 | } |
| 816 | |
Josef Bacik | 3e04e7f | 2013-02-06 16:49:15 -0500 | [diff] [blame] | 817 | if (ret) |
| 818 | goto out_free_reserve; |
| 819 | |
Li Zefan | 261507a0 | 2010-12-17 14:21:50 +0800 | [diff] [blame] | 820 | ret = btrfs_add_ordered_extent_compress(inode, |
| 821 | async_extent->start, |
| 822 | ins.objectid, |
| 823 | async_extent->ram_size, |
| 824 | ins.offset, |
| 825 | BTRFS_ORDERED_COMPRESSED, |
| 826 | async_extent->compress_type); |
Filipe Manana | d9f8596 | 2014-08-25 10:43:00 +0100 | [diff] [blame] | 827 | if (ret) { |
| 828 | btrfs_drop_extent_cache(inode, async_extent->start, |
| 829 | async_extent->start + |
| 830 | async_extent->ram_size - 1, 0); |
Josef Bacik | 3e04e7f | 2013-02-06 16:49:15 -0500 | [diff] [blame] | 831 | goto out_free_reserve; |
Filipe Manana | d9f8596 | 2014-08-25 10:43:00 +0100 | [diff] [blame] | 832 | } |
Filipe Manana | 9cfa3e3 | 2016-04-26 15:39:32 +0100 | [diff] [blame] | 833 | btrfs_dec_block_group_reservations(root->fs_info, ins.objectid); |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 834 | |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 835 | /* |
| 836 | * clear dirty, set writeback and unlock the pages. |
| 837 | */ |
Josef Bacik | c2790a2 | 2013-07-29 11:20:47 -0400 | [diff] [blame] | 838 | extent_clear_unlock_delalloc(inode, async_extent->start, |
Chris Mason | a791e35 | 2009-10-08 11:27:10 -0400 | [diff] [blame] | 839 | async_extent->start + |
| 840 | async_extent->ram_size - 1, |
Qu Wenruo | ba8b04c | 2016-07-19 16:50:36 +0800 | [diff] [blame] | 841 | async_extent->start + |
| 842 | async_extent->ram_size - 1, |
Josef Bacik | 151a41b | 2013-07-29 13:22:24 -0400 | [diff] [blame] | 843 | NULL, EXTENT_LOCKED | EXTENT_DELALLOC, |
| 844 | PAGE_UNLOCK | PAGE_CLEAR_DIRTY | |
Josef Bacik | c2790a2 | 2013-07-29 11:20:47 -0400 | [diff] [blame] | 845 | PAGE_SET_WRITEBACK); |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 846 | ret = btrfs_submit_compressed_write(inode, |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 847 | async_extent->start, |
| 848 | async_extent->ram_size, |
| 849 | ins.objectid, |
| 850 | ins.offset, async_extent->pages, |
| 851 | async_extent->nr_pages); |
Filipe Manana | fce2a4e | 2014-10-06 22:14:23 +0100 | [diff] [blame] | 852 | if (ret) { |
| 853 | struct extent_io_tree *tree = &BTRFS_I(inode)->io_tree; |
| 854 | struct page *p = async_extent->pages[0]; |
| 855 | const u64 start = async_extent->start; |
| 856 | const u64 end = start + async_extent->ram_size - 1; |
| 857 | |
| 858 | p->mapping = inode->i_mapping; |
| 859 | tree->ops->writepage_end_io_hook(p, start, end, |
| 860 | NULL, 0); |
| 861 | p->mapping = NULL; |
Qu Wenruo | ba8b04c | 2016-07-19 16:50:36 +0800 | [diff] [blame] | 862 | extent_clear_unlock_delalloc(inode, start, end, end, |
| 863 | NULL, 0, |
Filipe Manana | fce2a4e | 2014-10-06 22:14:23 +0100 | [diff] [blame] | 864 | PAGE_END_WRITEBACK | |
| 865 | PAGE_SET_ERROR); |
Filipe Manana | 40ae837 | 2014-10-06 22:14:24 +0100 | [diff] [blame] | 866 | free_async_extent_pages(async_extent); |
Filipe Manana | fce2a4e | 2014-10-06 22:14:23 +0100 | [diff] [blame] | 867 | } |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 868 | alloc_hint = ins.objectid + ins.offset; |
| 869 | kfree(async_extent); |
| 870 | cond_resched(); |
| 871 | } |
Filipe Manana | dec8f17 | 2014-10-06 22:14:26 +0100 | [diff] [blame] | 872 | return; |
Josef Bacik | 3e04e7f | 2013-02-06 16:49:15 -0500 | [diff] [blame] | 873 | out_free_reserve: |
Filipe Manana | 9cfa3e3 | 2016-04-26 15:39:32 +0100 | [diff] [blame] | 874 | btrfs_dec_block_group_reservations(root->fs_info, ins.objectid); |
Miao Xie | e570fd2 | 2014-06-19 10:42:50 +0800 | [diff] [blame] | 875 | btrfs_free_reserved_extent(root, ins.objectid, ins.offset, 1); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 876 | out_free: |
Josef Bacik | c2790a2 | 2013-07-29 11:20:47 -0400 | [diff] [blame] | 877 | extent_clear_unlock_delalloc(inode, async_extent->start, |
Josef Bacik | 3e04e7f | 2013-02-06 16:49:15 -0500 | [diff] [blame] | 878 | async_extent->start + |
| 879 | async_extent->ram_size - 1, |
Qu Wenruo | ba8b04c | 2016-07-19 16:50:36 +0800 | [diff] [blame] | 880 | async_extent->start + |
| 881 | async_extent->ram_size - 1, |
Josef Bacik | c2790a2 | 2013-07-29 11:20:47 -0400 | [diff] [blame] | 882 | NULL, EXTENT_LOCKED | EXTENT_DELALLOC | |
Josef Bacik | 151a41b | 2013-07-29 13:22:24 -0400 | [diff] [blame] | 883 | EXTENT_DEFRAG | EXTENT_DO_ACCOUNTING, |
| 884 | PAGE_UNLOCK | PAGE_CLEAR_DIRTY | |
Filipe Manana | 704de49 | 2014-10-06 22:14:22 +0100 | [diff] [blame] | 885 | PAGE_SET_WRITEBACK | PAGE_END_WRITEBACK | |
| 886 | PAGE_SET_ERROR); |
Filipe Manana | 40ae837 | 2014-10-06 22:14:24 +0100 | [diff] [blame] | 887 | free_async_extent_pages(async_extent); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 888 | kfree(async_extent); |
Josef Bacik | 3e04e7f | 2013-02-06 16:49:15 -0500 | [diff] [blame] | 889 | goto again; |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 890 | } |
| 891 | |
Josef Bacik | 4b46fce | 2010-05-23 11:00:55 -0400 | [diff] [blame] | 892 | static u64 get_extent_allocation_hint(struct inode *inode, u64 start, |
| 893 | u64 num_bytes) |
| 894 | { |
| 895 | struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree; |
| 896 | struct extent_map *em; |
| 897 | u64 alloc_hint = 0; |
| 898 | |
| 899 | read_lock(&em_tree->lock); |
| 900 | em = search_extent_mapping(em_tree, start, num_bytes); |
| 901 | if (em) { |
| 902 | /* |
| 903 | * if block start isn't an actual block number then find the |
| 904 | * first block in this inode and use that as a hint. If that |
| 905 | * block is also bogus then just don't worry about it. |
| 906 | */ |
| 907 | if (em->block_start >= EXTENT_MAP_LAST_BYTE) { |
| 908 | free_extent_map(em); |
| 909 | em = search_extent_mapping(em_tree, 0, 0); |
| 910 | if (em && em->block_start < EXTENT_MAP_LAST_BYTE) |
| 911 | alloc_hint = em->block_start; |
| 912 | if (em) |
| 913 | free_extent_map(em); |
| 914 | } else { |
| 915 | alloc_hint = em->block_start; |
| 916 | free_extent_map(em); |
| 917 | } |
| 918 | } |
| 919 | read_unlock(&em_tree->lock); |
| 920 | |
| 921 | return alloc_hint; |
| 922 | } |
| 923 | |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 924 | /* |
| 925 | * when extent_io.c finds a delayed allocation range in the file, |
| 926 | * the call backs end up in this code. The basic idea is to |
| 927 | * allocate extents on disk for the range, and create ordered data structs |
| 928 | * in ram to track those extents. |
| 929 | * |
| 930 | * locked_page is the page that writepage had locked already. We use |
| 931 | * it to make sure we don't do extra locks or unlocks. |
| 932 | * |
| 933 | * *page_started is set to one if we unlock locked_page and do everything |
| 934 | * required to start IO on it. It may be clean and already done with |
| 935 | * IO when we return. |
| 936 | */ |
Josef Bacik | 0036158 | 2013-08-14 14:02:47 -0400 | [diff] [blame] | 937 | static noinline int cow_file_range(struct inode *inode, |
| 938 | struct page *locked_page, |
Wang Xiaoguang | dda3245 | 2016-07-11 11:05:29 +0800 | [diff] [blame] | 939 | u64 start, u64 end, u64 delalloc_end, |
| 940 | int *page_started, unsigned long *nr_written, |
| 941 | int unlock, struct btrfs_dedupe_hash *hash) |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 942 | { |
Josef Bacik | 0036158 | 2013-08-14 14:02:47 -0400 | [diff] [blame] | 943 | struct btrfs_root *root = BTRFS_I(inode)->root; |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 944 | u64 alloc_hint = 0; |
| 945 | u64 num_bytes; |
| 946 | unsigned long ram_size; |
| 947 | u64 disk_num_bytes; |
| 948 | u64 cur_alloc_size; |
| 949 | u64 blocksize = root->sectorsize; |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 950 | struct btrfs_key ins; |
| 951 | struct extent_map *em; |
| 952 | struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree; |
| 953 | int ret = 0; |
| 954 | |
Josef Bacik | 02ecd2c | 2013-10-25 16:19:08 -0400 | [diff] [blame] | 955 | if (btrfs_is_free_space_inode(inode)) { |
| 956 | WARN_ON_ONCE(1); |
Josef Bacik | 29bce2f | 2014-02-07 12:21:23 -0500 | [diff] [blame] | 957 | ret = -EINVAL; |
| 958 | goto out_unlock; |
Josef Bacik | 02ecd2c | 2013-10-25 16:19:08 -0400 | [diff] [blame] | 959 | } |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 960 | |
Qu Wenruo | fda2832 | 2013-02-26 08:10:22 +0000 | [diff] [blame] | 961 | num_bytes = ALIGN(end - start + 1, blocksize); |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 962 | num_bytes = max(blocksize, num_bytes); |
| 963 | disk_num_bytes = num_bytes; |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 964 | |
Chris Mason | 4cb5300 | 2011-05-24 15:35:30 -0400 | [diff] [blame] | 965 | /* if this is a small write inside eof, kick off defrag */ |
Byongho Lee | ee22184 | 2015-12-15 01:42:10 +0900 | [diff] [blame] | 966 | if (num_bytes < SZ_64K && |
Liu Bo | 4cb13e5 | 2012-03-29 09:57:45 -0400 | [diff] [blame] | 967 | (start > 0 || end + 1 < BTRFS_I(inode)->disk_i_size)) |
Josef Bacik | 0036158 | 2013-08-14 14:02:47 -0400 | [diff] [blame] | 968 | btrfs_add_inode_defrag(NULL, inode); |
Chris Mason | 4cb5300 | 2011-05-24 15:35:30 -0400 | [diff] [blame] | 969 | |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 970 | if (start == 0) { |
| 971 | /* lets try to make an inline extent */ |
Josef Bacik | 0036158 | 2013-08-14 14:02:47 -0400 | [diff] [blame] | 972 | ret = cow_file_range_inline(root, inode, start, end, 0, 0, |
| 973 | NULL); |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 974 | if (ret == 0) { |
Qu Wenruo | ba8b04c | 2016-07-19 16:50:36 +0800 | [diff] [blame] | 975 | extent_clear_unlock_delalloc(inode, start, end, |
| 976 | delalloc_end, NULL, |
Josef Bacik | c2790a2 | 2013-07-29 11:20:47 -0400 | [diff] [blame] | 977 | EXTENT_LOCKED | EXTENT_DELALLOC | |
Josef Bacik | 151a41b | 2013-07-29 13:22:24 -0400 | [diff] [blame] | 978 | EXTENT_DEFRAG, PAGE_UNLOCK | |
Josef Bacik | c2790a2 | 2013-07-29 11:20:47 -0400 | [diff] [blame] | 979 | PAGE_CLEAR_DIRTY | PAGE_SET_WRITEBACK | |
| 980 | PAGE_END_WRITEBACK); |
Wang Xiaoguang | 1851309 | 2016-07-25 15:51:40 +0800 | [diff] [blame] | 981 | btrfs_free_reserved_data_space_noquota(inode, start, |
| 982 | end - start + 1); |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 983 | *nr_written = *nr_written + |
Kirill A. Shutemov | 09cbfea | 2016-04-01 15:29:47 +0300 | [diff] [blame] | 984 | (end - start + PAGE_SIZE) / PAGE_SIZE; |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 985 | *page_started = 1; |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 986 | goto out; |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 987 | } else if (ret < 0) { |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 988 | goto out_unlock; |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 989 | } |
| 990 | } |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 991 | |
| 992 | BUG_ON(disk_num_bytes > |
David Sterba | 6c41761 | 2011-04-13 15:41:04 +0200 | [diff] [blame] | 993 | btrfs_super_total_bytes(root->fs_info->super_copy)); |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 994 | |
Josef Bacik | 4b46fce | 2010-05-23 11:00:55 -0400 | [diff] [blame] | 995 | alloc_hint = get_extent_allocation_hint(inode, start, num_bytes); |
Zheng Yan | 5b21f2e | 2008-09-26 10:05:38 -0400 | [diff] [blame] | 996 | btrfs_drop_extent_cache(inode, start, start + num_bytes - 1, 0); |
Chris Mason | 3b95151 | 2008-04-17 11:29:12 -0400 | [diff] [blame] | 997 | |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 998 | while (disk_num_bytes > 0) { |
Chris Mason | a791e35 | 2009-10-08 11:27:10 -0400 | [diff] [blame] | 999 | unsigned long op; |
| 1000 | |
Josef Bacik | 287a0ab | 2010-03-19 18:07:23 +0000 | [diff] [blame] | 1001 | cur_alloc_size = disk_num_bytes; |
Wang Xiaoguang | 1851309 | 2016-07-25 15:51:40 +0800 | [diff] [blame] | 1002 | ret = btrfs_reserve_extent(root, cur_alloc_size, cur_alloc_size, |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 1003 | root->sectorsize, 0, alloc_hint, |
Miao Xie | e570fd2 | 2014-06-19 10:42:50 +0800 | [diff] [blame] | 1004 | &ins, 1, 1); |
Josef Bacik | 0036158 | 2013-08-14 14:02:47 -0400 | [diff] [blame] | 1005 | if (ret < 0) |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 1006 | goto out_unlock; |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 1007 | |
David Sterba | 172ddd6 | 2011-04-21 00:48:27 +0200 | [diff] [blame] | 1008 | em = alloc_extent_map(); |
Liu Bo | b9aa55b | 2013-05-14 02:12:15 +0000 | [diff] [blame] | 1009 | if (!em) { |
| 1010 | ret = -ENOMEM; |
Liu Bo | ace68ba | 2013-04-22 10:53:47 +0000 | [diff] [blame] | 1011 | goto out_reserve; |
Liu Bo | b9aa55b | 2013-05-14 02:12:15 +0000 | [diff] [blame] | 1012 | } |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 1013 | em->start = start; |
Chris Mason | 445a694 | 2008-11-10 11:53:33 -0500 | [diff] [blame] | 1014 | em->orig_start = em->start; |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 1015 | ram_size = ins.offset; |
| 1016 | em->len = ins.offset; |
Josef Bacik | 2ab28f3 | 2012-10-12 15:27:49 -0400 | [diff] [blame] | 1017 | em->mod_start = em->start; |
| 1018 | em->mod_len = em->len; |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 1019 | |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 1020 | em->block_start = ins.objectid; |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 1021 | em->block_len = ins.offset; |
Josef Bacik | b493968 | 2012-12-03 10:31:19 -0500 | [diff] [blame] | 1022 | em->orig_block_len = ins.offset; |
Josef Bacik | cc95bef | 2013-04-04 14:31:27 -0400 | [diff] [blame] | 1023 | em->ram_bytes = ram_size; |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 1024 | em->bdev = root->fs_info->fs_devices->latest_bdev; |
Chris Mason | 7f3c74f | 2008-07-18 12:01:11 -0400 | [diff] [blame] | 1025 | set_bit(EXTENT_FLAG_PINNED, &em->flags); |
Josef Bacik | 70c8a91 | 2012-10-11 16:54:30 -0400 | [diff] [blame] | 1026 | em->generation = -1; |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 1027 | |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 1028 | while (1) { |
Chris Mason | 890871b | 2009-09-02 16:24:52 -0400 | [diff] [blame] | 1029 | write_lock(&em_tree->lock); |
Josef Bacik | 09a2a8f9 | 2013-04-05 16:51:15 -0400 | [diff] [blame] | 1030 | ret = add_extent_mapping(em_tree, em, 1); |
Chris Mason | 890871b | 2009-09-02 16:24:52 -0400 | [diff] [blame] | 1031 | write_unlock(&em_tree->lock); |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 1032 | if (ret != -EEXIST) { |
| 1033 | free_extent_map(em); |
| 1034 | break; |
| 1035 | } |
| 1036 | btrfs_drop_extent_cache(inode, start, |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 1037 | start + ram_size - 1, 0); |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 1038 | } |
Liu Bo | ace68ba | 2013-04-22 10:53:47 +0000 | [diff] [blame] | 1039 | if (ret) |
| 1040 | goto out_reserve; |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 1041 | |
Chris Mason | 98d20f6 | 2008-04-14 09:46:10 -0400 | [diff] [blame] | 1042 | cur_alloc_size = ins.offset; |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 1043 | ret = btrfs_add_ordered_extent(inode, start, ins.objectid, |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 1044 | ram_size, cur_alloc_size, 0); |
Liu Bo | ace68ba | 2013-04-22 10:53:47 +0000 | [diff] [blame] | 1045 | if (ret) |
Filipe Manana | d9f8596 | 2014-08-25 10:43:00 +0100 | [diff] [blame] | 1046 | goto out_drop_extent_cache; |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 1047 | |
Yan Zheng | 17d217f | 2008-12-12 10:03:38 -0500 | [diff] [blame] | 1048 | if (root->root_key.objectid == |
| 1049 | BTRFS_DATA_RELOC_TREE_OBJECTID) { |
| 1050 | ret = btrfs_reloc_clone_csums(inode, start, |
| 1051 | cur_alloc_size); |
Josef Bacik | 0036158 | 2013-08-14 14:02:47 -0400 | [diff] [blame] | 1052 | if (ret) |
Filipe Manana | d9f8596 | 2014-08-25 10:43:00 +0100 | [diff] [blame] | 1053 | goto out_drop_extent_cache; |
Yan Zheng | 17d217f | 2008-12-12 10:03:38 -0500 | [diff] [blame] | 1054 | } |
| 1055 | |
Filipe Manana | 9cfa3e3 | 2016-04-26 15:39:32 +0100 | [diff] [blame] | 1056 | btrfs_dec_block_group_reservations(root->fs_info, ins.objectid); |
| 1057 | |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 1058 | if (disk_num_bytes < cur_alloc_size) |
Chris Mason | 3b95151 | 2008-04-17 11:29:12 -0400 | [diff] [blame] | 1059 | break; |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 1060 | |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 1061 | /* we're not doing compressed IO, don't unlock the first |
| 1062 | * page (which the caller expects to stay locked), don't |
| 1063 | * clear any dirty bits and don't set any writeback bits |
Chris Mason | 8b62b72 | 2009-09-02 16:53:46 -0400 | [diff] [blame] | 1064 | * |
| 1065 | * Do set the Private2 bit so we know this page was properly |
| 1066 | * setup for writepage |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 1067 | */ |
Josef Bacik | c2790a2 | 2013-07-29 11:20:47 -0400 | [diff] [blame] | 1068 | op = unlock ? PAGE_UNLOCK : 0; |
| 1069 | op |= PAGE_SET_PRIVATE2; |
Chris Mason | a791e35 | 2009-10-08 11:27:10 -0400 | [diff] [blame] | 1070 | |
Josef Bacik | c2790a2 | 2013-07-29 11:20:47 -0400 | [diff] [blame] | 1071 | extent_clear_unlock_delalloc(inode, start, |
Qu Wenruo | ba8b04c | 2016-07-19 16:50:36 +0800 | [diff] [blame] | 1072 | start + ram_size - 1, |
| 1073 | delalloc_end, locked_page, |
Josef Bacik | c2790a2 | 2013-07-29 11:20:47 -0400 | [diff] [blame] | 1074 | EXTENT_LOCKED | EXTENT_DELALLOC, |
| 1075 | op); |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 1076 | disk_num_bytes -= cur_alloc_size; |
Chris Mason | c59f895 | 2007-12-17 20:14:04 -0500 | [diff] [blame] | 1077 | num_bytes -= cur_alloc_size; |
| 1078 | alloc_hint = ins.objectid + ins.offset; |
| 1079 | start += cur_alloc_size; |
Chris Mason | b888db2 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 1080 | } |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 1081 | out: |
Chris Mason | be20aa9 | 2007-12-17 20:14:01 -0500 | [diff] [blame] | 1082 | return ret; |
Miao Xie | b7d5b0a | 2012-11-01 07:32:18 +0000 | [diff] [blame] | 1083 | |
Filipe Manana | d9f8596 | 2014-08-25 10:43:00 +0100 | [diff] [blame] | 1084 | out_drop_extent_cache: |
| 1085 | btrfs_drop_extent_cache(inode, start, start + ram_size - 1, 0); |
Liu Bo | ace68ba | 2013-04-22 10:53:47 +0000 | [diff] [blame] | 1086 | out_reserve: |
Filipe Manana | 9cfa3e3 | 2016-04-26 15:39:32 +0100 | [diff] [blame] | 1087 | btrfs_dec_block_group_reservations(root->fs_info, ins.objectid); |
Miao Xie | e570fd2 | 2014-06-19 10:42:50 +0800 | [diff] [blame] | 1088 | btrfs_free_reserved_extent(root, ins.objectid, ins.offset, 1); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 1089 | out_unlock: |
Qu Wenruo | ba8b04c | 2016-07-19 16:50:36 +0800 | [diff] [blame] | 1090 | extent_clear_unlock_delalloc(inode, start, end, delalloc_end, |
| 1091 | locked_page, |
Josef Bacik | 151a41b | 2013-07-29 13:22:24 -0400 | [diff] [blame] | 1092 | EXTENT_LOCKED | EXTENT_DO_ACCOUNTING | |
| 1093 | EXTENT_DELALLOC | EXTENT_DEFRAG, |
| 1094 | PAGE_UNLOCK | PAGE_CLEAR_DIRTY | |
| 1095 | PAGE_SET_WRITEBACK | PAGE_END_WRITEBACK); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 1096 | goto out; |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 1097 | } |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 1098 | |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 1099 | /* |
| 1100 | * work queue call back to started compression on a file and pages |
| 1101 | */ |
| 1102 | static noinline void async_cow_start(struct btrfs_work *work) |
| 1103 | { |
| 1104 | struct async_cow *async_cow; |
| 1105 | int num_added = 0; |
| 1106 | async_cow = container_of(work, struct async_cow, work); |
| 1107 | |
| 1108 | compress_file_range(async_cow->inode, async_cow->locked_page, |
| 1109 | async_cow->start, async_cow->end, async_cow, |
| 1110 | &num_added); |
Josef Bacik | 8180ef8 | 2012-06-08 15:16:12 -0400 | [diff] [blame] | 1111 | if (num_added == 0) { |
Josef Bacik | cb77fcd | 2012-06-15 12:19:48 -0600 | [diff] [blame] | 1112 | btrfs_add_delayed_iput(async_cow->inode); |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 1113 | async_cow->inode = NULL; |
Josef Bacik | 8180ef8 | 2012-06-08 15:16:12 -0400 | [diff] [blame] | 1114 | } |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 1115 | } |
| 1116 | |
| 1117 | /* |
| 1118 | * work queue call back to submit previously compressed pages |
| 1119 | */ |
| 1120 | static noinline void async_cow_submit(struct btrfs_work *work) |
| 1121 | { |
| 1122 | struct async_cow *async_cow; |
| 1123 | struct btrfs_root *root; |
| 1124 | unsigned long nr_pages; |
| 1125 | |
| 1126 | async_cow = container_of(work, struct async_cow, work); |
| 1127 | |
| 1128 | root = async_cow->root; |
Kirill A. Shutemov | 09cbfea | 2016-04-01 15:29:47 +0300 | [diff] [blame] | 1129 | nr_pages = (async_cow->end - async_cow->start + PAGE_SIZE) >> |
| 1130 | PAGE_SHIFT; |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 1131 | |
David Sterba | ee86395 | 2015-02-16 19:41:40 +0100 | [diff] [blame] | 1132 | /* |
| 1133 | * atomic_sub_return implies a barrier for waitqueue_active |
| 1134 | */ |
Josef Bacik | 66657b3 | 2012-08-01 15:36:24 -0400 | [diff] [blame] | 1135 | if (atomic_sub_return(nr_pages, &root->fs_info->async_delalloc_pages) < |
Byongho Lee | ee22184 | 2015-12-15 01:42:10 +0900 | [diff] [blame] | 1136 | 5 * SZ_1M && |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 1137 | waitqueue_active(&root->fs_info->async_submit_wait)) |
| 1138 | wake_up(&root->fs_info->async_submit_wait); |
| 1139 | |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 1140 | if (async_cow->inode) |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 1141 | submit_compressed_extents(async_cow->inode, async_cow); |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 1142 | } |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 1143 | |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 1144 | static noinline void async_cow_free(struct btrfs_work *work) |
| 1145 | { |
| 1146 | struct async_cow *async_cow; |
| 1147 | async_cow = container_of(work, struct async_cow, work); |
Josef Bacik | 8180ef8 | 2012-06-08 15:16:12 -0400 | [diff] [blame] | 1148 | if (async_cow->inode) |
Josef Bacik | cb77fcd | 2012-06-15 12:19:48 -0600 | [diff] [blame] | 1149 | btrfs_add_delayed_iput(async_cow->inode); |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 1150 | kfree(async_cow); |
| 1151 | } |
| 1152 | |
| 1153 | static int cow_file_range_async(struct inode *inode, struct page *locked_page, |
| 1154 | u64 start, u64 end, int *page_started, |
| 1155 | unsigned long *nr_written) |
| 1156 | { |
| 1157 | struct async_cow *async_cow; |
| 1158 | struct btrfs_root *root = BTRFS_I(inode)->root; |
| 1159 | unsigned long nr_pages; |
| 1160 | u64 cur_end; |
Byongho Lee | ee22184 | 2015-12-15 01:42:10 +0900 | [diff] [blame] | 1161 | int limit = 10 * SZ_1M; |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 1162 | |
Chris Mason | a3429ab | 2009-10-08 12:30:20 -0400 | [diff] [blame] | 1163 | clear_extent_bit(&BTRFS_I(inode)->io_tree, start, end, EXTENT_LOCKED, |
| 1164 | 1, 0, NULL, GFP_NOFS); |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 1165 | while (start < end) { |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 1166 | async_cow = kmalloc(sizeof(*async_cow), GFP_NOFS); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 1167 | BUG_ON(!async_cow); /* -ENOMEM */ |
Josef Bacik | 8180ef8 | 2012-06-08 15:16:12 -0400 | [diff] [blame] | 1168 | async_cow->inode = igrab(inode); |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 1169 | async_cow->root = root; |
| 1170 | async_cow->locked_page = locked_page; |
| 1171 | async_cow->start = start; |
| 1172 | |
Wang Shilong | f79707b | 2014-07-17 11:44:09 +0800 | [diff] [blame] | 1173 | if (BTRFS_I(inode)->flags & BTRFS_INODE_NOCOMPRESS && |
Jeff Mahoney | 3cdde22 | 2016-06-09 21:38:35 -0400 | [diff] [blame] | 1174 | !btrfs_test_opt(root->fs_info, FORCE_COMPRESS)) |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 1175 | cur_end = end; |
| 1176 | else |
Byongho Lee | ee22184 | 2015-12-15 01:42:10 +0900 | [diff] [blame] | 1177 | cur_end = min(end, start + SZ_512K - 1); |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 1178 | |
| 1179 | async_cow->end = cur_end; |
| 1180 | INIT_LIST_HEAD(&async_cow->extents); |
| 1181 | |
Liu Bo | 9e0af23 | 2014-08-15 23:36:53 +0800 | [diff] [blame] | 1182 | btrfs_init_work(&async_cow->work, |
| 1183 | btrfs_delalloc_helper, |
| 1184 | async_cow_start, async_cow_submit, |
| 1185 | async_cow_free); |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 1186 | |
Kirill A. Shutemov | 09cbfea | 2016-04-01 15:29:47 +0300 | [diff] [blame] | 1187 | nr_pages = (cur_end - start + PAGE_SIZE) >> |
| 1188 | PAGE_SHIFT; |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 1189 | atomic_add(nr_pages, &root->fs_info->async_delalloc_pages); |
| 1190 | |
Qu Wenruo | afe3d24 | 2014-02-28 10:46:07 +0800 | [diff] [blame] | 1191 | btrfs_queue_work(root->fs_info->delalloc_workers, |
| 1192 | &async_cow->work); |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 1193 | |
| 1194 | if (atomic_read(&root->fs_info->async_delalloc_pages) > limit) { |
| 1195 | wait_event(root->fs_info->async_submit_wait, |
| 1196 | (atomic_read(&root->fs_info->async_delalloc_pages) < |
| 1197 | limit)); |
| 1198 | } |
| 1199 | |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 1200 | while (atomic_read(&root->fs_info->async_submit_draining) && |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 1201 | atomic_read(&root->fs_info->async_delalloc_pages)) { |
| 1202 | wait_event(root->fs_info->async_submit_wait, |
| 1203 | (atomic_read(&root->fs_info->async_delalloc_pages) == |
| 1204 | 0)); |
| 1205 | } |
| 1206 | |
| 1207 | *nr_written += nr_pages; |
| 1208 | start = cur_end + 1; |
| 1209 | } |
| 1210 | *page_started = 1; |
| 1211 | return 0; |
Chris Mason | be20aa9 | 2007-12-17 20:14:01 -0500 | [diff] [blame] | 1212 | } |
| 1213 | |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 1214 | static noinline int csum_exist_in_range(struct btrfs_root *root, |
Yan Zheng | 17d217f | 2008-12-12 10:03:38 -0500 | [diff] [blame] | 1215 | u64 bytenr, u64 num_bytes) |
| 1216 | { |
| 1217 | int ret; |
| 1218 | struct btrfs_ordered_sum *sums; |
| 1219 | LIST_HEAD(list); |
| 1220 | |
Yan Zheng | 07d400a | 2009-01-06 11:42:00 -0500 | [diff] [blame] | 1221 | ret = btrfs_lookup_csums_range(root->fs_info->csum_root, bytenr, |
Arne Jansen | a2de733 | 2011-03-08 14:14:00 +0100 | [diff] [blame] | 1222 | bytenr + num_bytes - 1, &list, 0); |
Yan Zheng | 17d217f | 2008-12-12 10:03:38 -0500 | [diff] [blame] | 1223 | if (ret == 0 && list_empty(&list)) |
| 1224 | return 0; |
| 1225 | |
| 1226 | while (!list_empty(&list)) { |
| 1227 | sums = list_entry(list.next, struct btrfs_ordered_sum, list); |
| 1228 | list_del(&sums->list); |
| 1229 | kfree(sums); |
| 1230 | } |
| 1231 | return 1; |
| 1232 | } |
| 1233 | |
Chris Mason | d352ac6 | 2008-09-29 15:18:18 -0400 | [diff] [blame] | 1234 | /* |
| 1235 | * when nowcow writeback call back. This checks for snapshots or COW copies |
| 1236 | * of the extents that exist in the file, and COWs the file as required. |
| 1237 | * |
| 1238 | * If no cow copies or snapshots exist, we write directly to the existing |
| 1239 | * blocks on disk |
| 1240 | */ |
Chris Mason | 7f366cf | 2009-03-12 20:12:45 -0400 | [diff] [blame] | 1241 | static noinline int run_delalloc_nocow(struct inode *inode, |
| 1242 | struct page *locked_page, |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 1243 | u64 start, u64 end, int *page_started, int force, |
| 1244 | unsigned long *nr_written) |
Chris Mason | be20aa9 | 2007-12-17 20:14:01 -0500 | [diff] [blame] | 1245 | { |
Chris Mason | be20aa9 | 2007-12-17 20:14:01 -0500 | [diff] [blame] | 1246 | struct btrfs_root *root = BTRFS_I(inode)->root; |
Yan Zheng | 7ea394f | 2008-08-05 13:05:02 -0400 | [diff] [blame] | 1247 | struct btrfs_trans_handle *trans; |
Chris Mason | be20aa9 | 2007-12-17 20:14:01 -0500 | [diff] [blame] | 1248 | struct extent_buffer *leaf; |
Chris Mason | be20aa9 | 2007-12-17 20:14:01 -0500 | [diff] [blame] | 1249 | struct btrfs_path *path; |
Yan Zheng | 80ff385 | 2008-10-30 14:20:02 -0400 | [diff] [blame] | 1250 | struct btrfs_file_extent_item *fi; |
Chris Mason | be20aa9 | 2007-12-17 20:14:01 -0500 | [diff] [blame] | 1251 | struct btrfs_key found_key; |
Yan Zheng | 80ff385 | 2008-10-30 14:20:02 -0400 | [diff] [blame] | 1252 | u64 cow_start; |
| 1253 | u64 cur_offset; |
| 1254 | u64 extent_end; |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 1255 | u64 extent_offset; |
Yan Zheng | 80ff385 | 2008-10-30 14:20:02 -0400 | [diff] [blame] | 1256 | u64 disk_bytenr; |
| 1257 | u64 num_bytes; |
Josef Bacik | b493968 | 2012-12-03 10:31:19 -0500 | [diff] [blame] | 1258 | u64 disk_num_bytes; |
Josef Bacik | cc95bef | 2013-04-04 14:31:27 -0400 | [diff] [blame] | 1259 | u64 ram_bytes; |
Yan Zheng | 80ff385 | 2008-10-30 14:20:02 -0400 | [diff] [blame] | 1260 | int extent_type; |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 1261 | int ret, err; |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 1262 | int type; |
Yan Zheng | 80ff385 | 2008-10-30 14:20:02 -0400 | [diff] [blame] | 1263 | int nocow; |
| 1264 | int check_prev = 1; |
Li Zefan | 82d5902 | 2011-04-20 10:33:24 +0800 | [diff] [blame] | 1265 | bool nolock; |
Li Zefan | 33345d01 | 2011-04-20 10:31:50 +0800 | [diff] [blame] | 1266 | u64 ino = btrfs_ino(inode); |
Chris Mason | be20aa9 | 2007-12-17 20:14:01 -0500 | [diff] [blame] | 1267 | |
| 1268 | path = btrfs_alloc_path(); |
Josef Bacik | 17ca04a | 2012-05-31 15:58:55 -0400 | [diff] [blame] | 1269 | if (!path) { |
Qu Wenruo | ba8b04c | 2016-07-19 16:50:36 +0800 | [diff] [blame] | 1270 | extent_clear_unlock_delalloc(inode, start, end, end, |
| 1271 | locked_page, |
Josef Bacik | c2790a2 | 2013-07-29 11:20:47 -0400 | [diff] [blame] | 1272 | EXTENT_LOCKED | EXTENT_DELALLOC | |
Josef Bacik | 151a41b | 2013-07-29 13:22:24 -0400 | [diff] [blame] | 1273 | EXTENT_DO_ACCOUNTING | |
| 1274 | EXTENT_DEFRAG, PAGE_UNLOCK | |
Josef Bacik | c2790a2 | 2013-07-29 11:20:47 -0400 | [diff] [blame] | 1275 | PAGE_CLEAR_DIRTY | |
| 1276 | PAGE_SET_WRITEBACK | |
| 1277 | PAGE_END_WRITEBACK); |
Mark Fasheh | d8926bb | 2011-07-13 10:38:47 -0700 | [diff] [blame] | 1278 | return -ENOMEM; |
Josef Bacik | 17ca04a | 2012-05-31 15:58:55 -0400 | [diff] [blame] | 1279 | } |
Li Zefan | 82d5902 | 2011-04-20 10:33:24 +0800 | [diff] [blame] | 1280 | |
Liu Bo | 83eea1f | 2012-07-10 05:28:39 -0600 | [diff] [blame] | 1281 | nolock = btrfs_is_free_space_inode(inode); |
Li Zefan | 82d5902 | 2011-04-20 10:33:24 +0800 | [diff] [blame] | 1282 | |
| 1283 | if (nolock) |
Josef Bacik | 7a7eaa4 | 2011-04-13 12:54:33 -0400 | [diff] [blame] | 1284 | trans = btrfs_join_transaction_nolock(root); |
Li Zefan | 82d5902 | 2011-04-20 10:33:24 +0800 | [diff] [blame] | 1285 | else |
Josef Bacik | 7a7eaa4 | 2011-04-13 12:54:33 -0400 | [diff] [blame] | 1286 | trans = btrfs_join_transaction(root); |
Chris Mason | ff5714c | 2011-05-28 07:00:39 -0400 | [diff] [blame] | 1287 | |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 1288 | if (IS_ERR(trans)) { |
Qu Wenruo | ba8b04c | 2016-07-19 16:50:36 +0800 | [diff] [blame] | 1289 | extent_clear_unlock_delalloc(inode, start, end, end, |
| 1290 | locked_page, |
Josef Bacik | c2790a2 | 2013-07-29 11:20:47 -0400 | [diff] [blame] | 1291 | EXTENT_LOCKED | EXTENT_DELALLOC | |
Josef Bacik | 151a41b | 2013-07-29 13:22:24 -0400 | [diff] [blame] | 1292 | EXTENT_DO_ACCOUNTING | |
| 1293 | EXTENT_DEFRAG, PAGE_UNLOCK | |
Josef Bacik | c2790a2 | 2013-07-29 11:20:47 -0400 | [diff] [blame] | 1294 | PAGE_CLEAR_DIRTY | |
| 1295 | PAGE_SET_WRITEBACK | |
| 1296 | PAGE_END_WRITEBACK); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 1297 | btrfs_free_path(path); |
| 1298 | return PTR_ERR(trans); |
| 1299 | } |
| 1300 | |
Josef Bacik | 74b2107 | 2011-04-13 12:02:53 -0400 | [diff] [blame] | 1301 | trans->block_rsv = &root->fs_info->delalloc_block_rsv; |
Chris Mason | be20aa9 | 2007-12-17 20:14:01 -0500 | [diff] [blame] | 1302 | |
Yan Zheng | 80ff385 | 2008-10-30 14:20:02 -0400 | [diff] [blame] | 1303 | cow_start = (u64)-1; |
| 1304 | cur_offset = start; |
| 1305 | while (1) { |
Li Zefan | 33345d01 | 2011-04-20 10:31:50 +0800 | [diff] [blame] | 1306 | ret = btrfs_lookup_file_extent(trans, root, path, ino, |
Yan Zheng | 80ff385 | 2008-10-30 14:20:02 -0400 | [diff] [blame] | 1307 | cur_offset, 0); |
Josef Bacik | d788a34 | 2013-10-25 16:55:08 -0400 | [diff] [blame] | 1308 | if (ret < 0) |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 1309 | goto error; |
Yan Zheng | 80ff385 | 2008-10-30 14:20:02 -0400 | [diff] [blame] | 1310 | if (ret > 0 && path->slots[0] > 0 && check_prev) { |
| 1311 | leaf = path->nodes[0]; |
| 1312 | btrfs_item_key_to_cpu(leaf, &found_key, |
| 1313 | path->slots[0] - 1); |
Li Zefan | 33345d01 | 2011-04-20 10:31:50 +0800 | [diff] [blame] | 1314 | if (found_key.objectid == ino && |
Yan Zheng | 80ff385 | 2008-10-30 14:20:02 -0400 | [diff] [blame] | 1315 | found_key.type == BTRFS_EXTENT_DATA_KEY) |
| 1316 | path->slots[0]--; |
| 1317 | } |
| 1318 | check_prev = 0; |
| 1319 | next_slot: |
| 1320 | leaf = path->nodes[0]; |
| 1321 | if (path->slots[0] >= btrfs_header_nritems(leaf)) { |
| 1322 | ret = btrfs_next_leaf(root, path); |
Josef Bacik | d788a34 | 2013-10-25 16:55:08 -0400 | [diff] [blame] | 1323 | if (ret < 0) |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 1324 | goto error; |
Yan Zheng | 80ff385 | 2008-10-30 14:20:02 -0400 | [diff] [blame] | 1325 | if (ret > 0) |
| 1326 | break; |
| 1327 | leaf = path->nodes[0]; |
| 1328 | } |
Chris Mason | be20aa9 | 2007-12-17 20:14:01 -0500 | [diff] [blame] | 1329 | |
Yan Zheng | 80ff385 | 2008-10-30 14:20:02 -0400 | [diff] [blame] | 1330 | nocow = 0; |
| 1331 | disk_bytenr = 0; |
Yan Zheng | 17d217f | 2008-12-12 10:03:38 -0500 | [diff] [blame] | 1332 | num_bytes = 0; |
Yan Zheng | 80ff385 | 2008-10-30 14:20:02 -0400 | [diff] [blame] | 1333 | btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]); |
Chris Mason | be20aa9 | 2007-12-17 20:14:01 -0500 | [diff] [blame] | 1334 | |
Filipe Manana | 1d512cb | 2015-11-09 00:33:58 +0000 | [diff] [blame] | 1335 | if (found_key.objectid > ino) |
| 1336 | break; |
| 1337 | if (WARN_ON_ONCE(found_key.objectid < ino) || |
| 1338 | found_key.type < BTRFS_EXTENT_DATA_KEY) { |
| 1339 | path->slots[0]++; |
| 1340 | goto next_slot; |
| 1341 | } |
| 1342 | if (found_key.type > BTRFS_EXTENT_DATA_KEY || |
Yan Zheng | 80ff385 | 2008-10-30 14:20:02 -0400 | [diff] [blame] | 1343 | found_key.offset > end) |
| 1344 | break; |
Chris Mason | be20aa9 | 2007-12-17 20:14:01 -0500 | [diff] [blame] | 1345 | |
Yan Zheng | 80ff385 | 2008-10-30 14:20:02 -0400 | [diff] [blame] | 1346 | if (found_key.offset > cur_offset) { |
| 1347 | extent_end = found_key.offset; |
Chris Mason | e9061e2 | 2009-10-09 09:57:45 -0400 | [diff] [blame] | 1348 | extent_type = 0; |
Yan Zheng | 80ff385 | 2008-10-30 14:20:02 -0400 | [diff] [blame] | 1349 | goto out_check; |
| 1350 | } |
Chris Mason | c31f883 | 2008-01-08 15:46:31 -0500 | [diff] [blame] | 1351 | |
Yan Zheng | 80ff385 | 2008-10-30 14:20:02 -0400 | [diff] [blame] | 1352 | fi = btrfs_item_ptr(leaf, path->slots[0], |
| 1353 | struct btrfs_file_extent_item); |
| 1354 | extent_type = btrfs_file_extent_type(leaf, fi); |
Chris Mason | be20aa9 | 2007-12-17 20:14:01 -0500 | [diff] [blame] | 1355 | |
Josef Bacik | cc95bef | 2013-04-04 14:31:27 -0400 | [diff] [blame] | 1356 | ram_bytes = btrfs_file_extent_ram_bytes(leaf, fi); |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 1357 | if (extent_type == BTRFS_FILE_EXTENT_REG || |
| 1358 | extent_type == BTRFS_FILE_EXTENT_PREALLOC) { |
Yan Zheng | 80ff385 | 2008-10-30 14:20:02 -0400 | [diff] [blame] | 1359 | disk_bytenr = btrfs_file_extent_disk_bytenr(leaf, fi); |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 1360 | extent_offset = btrfs_file_extent_offset(leaf, fi); |
Yan Zheng | 80ff385 | 2008-10-30 14:20:02 -0400 | [diff] [blame] | 1361 | extent_end = found_key.offset + |
| 1362 | btrfs_file_extent_num_bytes(leaf, fi); |
Josef Bacik | b493968 | 2012-12-03 10:31:19 -0500 | [diff] [blame] | 1363 | disk_num_bytes = |
| 1364 | btrfs_file_extent_disk_num_bytes(leaf, fi); |
Yan Zheng | 80ff385 | 2008-10-30 14:20:02 -0400 | [diff] [blame] | 1365 | if (extent_end <= start) { |
| 1366 | path->slots[0]++; |
| 1367 | goto next_slot; |
| 1368 | } |
Yan Zheng | 17d217f | 2008-12-12 10:03:38 -0500 | [diff] [blame] | 1369 | if (disk_bytenr == 0) |
| 1370 | goto out_check; |
Yan Zheng | 80ff385 | 2008-10-30 14:20:02 -0400 | [diff] [blame] | 1371 | if (btrfs_file_extent_compression(leaf, fi) || |
| 1372 | btrfs_file_extent_encryption(leaf, fi) || |
| 1373 | btrfs_file_extent_other_encoding(leaf, fi)) |
| 1374 | goto out_check; |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 1375 | if (extent_type == BTRFS_FILE_EXTENT_REG && !force) |
| 1376 | goto out_check; |
Yan Zheng | d2fb343 | 2008-12-11 16:30:39 -0500 | [diff] [blame] | 1377 | if (btrfs_extent_readonly(root, disk_bytenr)) |
Yan Zheng | 80ff385 | 2008-10-30 14:20:02 -0400 | [diff] [blame] | 1378 | goto out_check; |
Li Zefan | 33345d01 | 2011-04-20 10:31:50 +0800 | [diff] [blame] | 1379 | if (btrfs_cross_ref_exist(trans, root, ino, |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 1380 | found_key.offset - |
| 1381 | extent_offset, disk_bytenr)) |
Yan Zheng | 17d217f | 2008-12-12 10:03:38 -0500 | [diff] [blame] | 1382 | goto out_check; |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 1383 | disk_bytenr += extent_offset; |
Yan Zheng | 17d217f | 2008-12-12 10:03:38 -0500 | [diff] [blame] | 1384 | disk_bytenr += cur_offset - found_key.offset; |
| 1385 | num_bytes = min(end + 1, extent_end) - cur_offset; |
| 1386 | /* |
Wang Shilong | e9894fd | 2014-03-27 11:12:25 +0800 | [diff] [blame] | 1387 | * if there are pending snapshots for this root, |
| 1388 | * we fall into common COW way. |
| 1389 | */ |
| 1390 | if (!nolock) { |
Filipe Manana | 9ea24bb | 2014-10-29 11:57:59 +0000 | [diff] [blame] | 1391 | err = btrfs_start_write_no_snapshoting(root); |
Wang Shilong | e9894fd | 2014-03-27 11:12:25 +0800 | [diff] [blame] | 1392 | if (!err) |
| 1393 | goto out_check; |
| 1394 | } |
| 1395 | /* |
Yan Zheng | 17d217f | 2008-12-12 10:03:38 -0500 | [diff] [blame] | 1396 | * force cow if csum exists in the range. |
| 1397 | * this ensure that csum for a given extent are |
| 1398 | * either valid or do not exist. |
| 1399 | */ |
| 1400 | if (csum_exist_in_range(root, disk_bytenr, num_bytes)) |
| 1401 | goto out_check; |
Filipe Manana | f78c436 | 2016-05-09 13:15:41 +0100 | [diff] [blame] | 1402 | if (!btrfs_inc_nocow_writers(root->fs_info, |
| 1403 | disk_bytenr)) |
| 1404 | goto out_check; |
Yan Zheng | 80ff385 | 2008-10-30 14:20:02 -0400 | [diff] [blame] | 1405 | nocow = 1; |
| 1406 | } else if (extent_type == BTRFS_FILE_EXTENT_INLINE) { |
| 1407 | extent_end = found_key.offset + |
Chris Mason | 514ac8a | 2014-01-03 21:07:00 -0800 | [diff] [blame] | 1408 | btrfs_file_extent_inline_len(leaf, |
| 1409 | path->slots[0], fi); |
Yan Zheng | 80ff385 | 2008-10-30 14:20:02 -0400 | [diff] [blame] | 1410 | extent_end = ALIGN(extent_end, root->sectorsize); |
| 1411 | } else { |
| 1412 | BUG_ON(1); |
| 1413 | } |
| 1414 | out_check: |
| 1415 | if (extent_end <= start) { |
| 1416 | path->slots[0]++; |
Wang Shilong | e9894fd | 2014-03-27 11:12:25 +0800 | [diff] [blame] | 1417 | if (!nolock && nocow) |
Filipe Manana | 9ea24bb | 2014-10-29 11:57:59 +0000 | [diff] [blame] | 1418 | btrfs_end_write_no_snapshoting(root); |
Filipe Manana | f78c436 | 2016-05-09 13:15:41 +0100 | [diff] [blame] | 1419 | if (nocow) |
| 1420 | btrfs_dec_nocow_writers(root->fs_info, |
| 1421 | disk_bytenr); |
Yan Zheng | 80ff385 | 2008-10-30 14:20:02 -0400 | [diff] [blame] | 1422 | goto next_slot; |
| 1423 | } |
| 1424 | if (!nocow) { |
| 1425 | if (cow_start == (u64)-1) |
| 1426 | cow_start = cur_offset; |
| 1427 | cur_offset = extent_end; |
| 1428 | if (cur_offset > end) |
| 1429 | break; |
| 1430 | path->slots[0]++; |
| 1431 | goto next_slot; |
Yan Zheng | 7ea394f | 2008-08-05 13:05:02 -0400 | [diff] [blame] | 1432 | } |
Chris Mason | be20aa9 | 2007-12-17 20:14:01 -0500 | [diff] [blame] | 1433 | |
David Sterba | b3b4aa7 | 2011-04-21 01:20:15 +0200 | [diff] [blame] | 1434 | btrfs_release_path(path); |
Yan Zheng | 80ff385 | 2008-10-30 14:20:02 -0400 | [diff] [blame] | 1435 | if (cow_start != (u64)-1) { |
Josef Bacik | 0036158 | 2013-08-14 14:02:47 -0400 | [diff] [blame] | 1436 | ret = cow_file_range(inode, locked_page, |
| 1437 | cow_start, found_key.offset - 1, |
Wang Xiaoguang | dda3245 | 2016-07-11 11:05:29 +0800 | [diff] [blame] | 1438 | end, page_started, nr_written, 1, |
| 1439 | NULL); |
Wang Shilong | e9894fd | 2014-03-27 11:12:25 +0800 | [diff] [blame] | 1440 | if (ret) { |
| 1441 | if (!nolock && nocow) |
Filipe Manana | 9ea24bb | 2014-10-29 11:57:59 +0000 | [diff] [blame] | 1442 | btrfs_end_write_no_snapshoting(root); |
Filipe Manana | f78c436 | 2016-05-09 13:15:41 +0100 | [diff] [blame] | 1443 | if (nocow) |
| 1444 | btrfs_dec_nocow_writers(root->fs_info, |
| 1445 | disk_bytenr); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 1446 | goto error; |
Wang Shilong | e9894fd | 2014-03-27 11:12:25 +0800 | [diff] [blame] | 1447 | } |
Yan Zheng | 80ff385 | 2008-10-30 14:20:02 -0400 | [diff] [blame] | 1448 | cow_start = (u64)-1; |
Yan Zheng | 7ea394f | 2008-08-05 13:05:02 -0400 | [diff] [blame] | 1449 | } |
Yan Zheng | 80ff385 | 2008-10-30 14:20:02 -0400 | [diff] [blame] | 1450 | |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 1451 | if (extent_type == BTRFS_FILE_EXTENT_PREALLOC) { |
| 1452 | struct extent_map *em; |
| 1453 | struct extent_map_tree *em_tree; |
| 1454 | em_tree = &BTRFS_I(inode)->extent_tree; |
David Sterba | 172ddd6 | 2011-04-21 00:48:27 +0200 | [diff] [blame] | 1455 | em = alloc_extent_map(); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 1456 | BUG_ON(!em); /* -ENOMEM */ |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 1457 | em->start = cur_offset; |
Josef Bacik | 70c8a91 | 2012-10-11 16:54:30 -0400 | [diff] [blame] | 1458 | em->orig_start = found_key.offset - extent_offset; |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 1459 | em->len = num_bytes; |
| 1460 | em->block_len = num_bytes; |
| 1461 | em->block_start = disk_bytenr; |
Josef Bacik | b493968 | 2012-12-03 10:31:19 -0500 | [diff] [blame] | 1462 | em->orig_block_len = disk_num_bytes; |
Josef Bacik | cc95bef | 2013-04-04 14:31:27 -0400 | [diff] [blame] | 1463 | em->ram_bytes = ram_bytes; |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 1464 | em->bdev = root->fs_info->fs_devices->latest_bdev; |
Josef Bacik | 2ab28f3 | 2012-10-12 15:27:49 -0400 | [diff] [blame] | 1465 | em->mod_start = em->start; |
| 1466 | em->mod_len = em->len; |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 1467 | set_bit(EXTENT_FLAG_PINNED, &em->flags); |
Josef Bacik | b11e234 | 2012-12-03 10:58:15 -0500 | [diff] [blame] | 1468 | set_bit(EXTENT_FLAG_FILLING, &em->flags); |
Josef Bacik | 70c8a91 | 2012-10-11 16:54:30 -0400 | [diff] [blame] | 1469 | em->generation = -1; |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 1470 | while (1) { |
Chris Mason | 890871b | 2009-09-02 16:24:52 -0400 | [diff] [blame] | 1471 | write_lock(&em_tree->lock); |
Josef Bacik | 09a2a8f9 | 2013-04-05 16:51:15 -0400 | [diff] [blame] | 1472 | ret = add_extent_mapping(em_tree, em, 1); |
Chris Mason | 890871b | 2009-09-02 16:24:52 -0400 | [diff] [blame] | 1473 | write_unlock(&em_tree->lock); |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 1474 | if (ret != -EEXIST) { |
| 1475 | free_extent_map(em); |
| 1476 | break; |
| 1477 | } |
| 1478 | btrfs_drop_extent_cache(inode, em->start, |
| 1479 | em->start + em->len - 1, 0); |
| 1480 | } |
| 1481 | type = BTRFS_ORDERED_PREALLOC; |
| 1482 | } else { |
| 1483 | type = BTRFS_ORDERED_NOCOW; |
| 1484 | } |
Yan Zheng | 80ff385 | 2008-10-30 14:20:02 -0400 | [diff] [blame] | 1485 | |
| 1486 | ret = btrfs_add_ordered_extent(inode, cur_offset, disk_bytenr, |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 1487 | num_bytes, num_bytes, type); |
Filipe Manana | f78c436 | 2016-05-09 13:15:41 +0100 | [diff] [blame] | 1488 | if (nocow) |
| 1489 | btrfs_dec_nocow_writers(root->fs_info, disk_bytenr); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 1490 | BUG_ON(ret); /* -ENOMEM */ |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 1491 | |
Yan, Zheng | efa5646 | 2010-05-16 10:49:59 -0400 | [diff] [blame] | 1492 | if (root->root_key.objectid == |
| 1493 | BTRFS_DATA_RELOC_TREE_OBJECTID) { |
| 1494 | ret = btrfs_reloc_clone_csums(inode, cur_offset, |
| 1495 | num_bytes); |
Wang Shilong | e9894fd | 2014-03-27 11:12:25 +0800 | [diff] [blame] | 1496 | if (ret) { |
| 1497 | if (!nolock && nocow) |
Filipe Manana | 9ea24bb | 2014-10-29 11:57:59 +0000 | [diff] [blame] | 1498 | btrfs_end_write_no_snapshoting(root); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 1499 | goto error; |
Wang Shilong | e9894fd | 2014-03-27 11:12:25 +0800 | [diff] [blame] | 1500 | } |
Yan, Zheng | efa5646 | 2010-05-16 10:49:59 -0400 | [diff] [blame] | 1501 | } |
| 1502 | |
Josef Bacik | c2790a2 | 2013-07-29 11:20:47 -0400 | [diff] [blame] | 1503 | extent_clear_unlock_delalloc(inode, cur_offset, |
Qu Wenruo | ba8b04c | 2016-07-19 16:50:36 +0800 | [diff] [blame] | 1504 | cur_offset + num_bytes - 1, end, |
Josef Bacik | c2790a2 | 2013-07-29 11:20:47 -0400 | [diff] [blame] | 1505 | locked_page, EXTENT_LOCKED | |
Wang Xiaoguang | 1851309 | 2016-07-25 15:51:40 +0800 | [diff] [blame] | 1506 | EXTENT_DELALLOC | |
| 1507 | EXTENT_CLEAR_DATA_RESV, |
| 1508 | PAGE_UNLOCK | PAGE_SET_PRIVATE2); |
| 1509 | |
Wang Shilong | e9894fd | 2014-03-27 11:12:25 +0800 | [diff] [blame] | 1510 | if (!nolock && nocow) |
Filipe Manana | 9ea24bb | 2014-10-29 11:57:59 +0000 | [diff] [blame] | 1511 | btrfs_end_write_no_snapshoting(root); |
Yan Zheng | 80ff385 | 2008-10-30 14:20:02 -0400 | [diff] [blame] | 1512 | cur_offset = extent_end; |
| 1513 | if (cur_offset > end) |
| 1514 | break; |
Yan Zheng | 7ea394f | 2008-08-05 13:05:02 -0400 | [diff] [blame] | 1515 | } |
David Sterba | b3b4aa7 | 2011-04-21 01:20:15 +0200 | [diff] [blame] | 1516 | btrfs_release_path(path); |
Yan Zheng | 80ff385 | 2008-10-30 14:20:02 -0400 | [diff] [blame] | 1517 | |
Josef Bacik | 17ca04a | 2012-05-31 15:58:55 -0400 | [diff] [blame] | 1518 | if (cur_offset <= end && cow_start == (u64)-1) { |
Yan Zheng | 80ff385 | 2008-10-30 14:20:02 -0400 | [diff] [blame] | 1519 | cow_start = cur_offset; |
Josef Bacik | 17ca04a | 2012-05-31 15:58:55 -0400 | [diff] [blame] | 1520 | cur_offset = end; |
| 1521 | } |
| 1522 | |
Yan Zheng | 80ff385 | 2008-10-30 14:20:02 -0400 | [diff] [blame] | 1523 | if (cow_start != (u64)-1) { |
Wang Xiaoguang | dda3245 | 2016-07-11 11:05:29 +0800 | [diff] [blame] | 1524 | ret = cow_file_range(inode, locked_page, cow_start, end, end, |
| 1525 | page_started, nr_written, 1, NULL); |
Josef Bacik | d788a34 | 2013-10-25 16:55:08 -0400 | [diff] [blame] | 1526 | if (ret) |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 1527 | goto error; |
Yan Zheng | 80ff385 | 2008-10-30 14:20:02 -0400 | [diff] [blame] | 1528 | } |
| 1529 | |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 1530 | error: |
Miao Xie | a698d075 | 2012-09-20 01:51:59 -0600 | [diff] [blame] | 1531 | err = btrfs_end_transaction(trans, root); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 1532 | if (!ret) |
| 1533 | ret = err; |
| 1534 | |
Josef Bacik | 17ca04a | 2012-05-31 15:58:55 -0400 | [diff] [blame] | 1535 | if (ret && cur_offset < end) |
Qu Wenruo | ba8b04c | 2016-07-19 16:50:36 +0800 | [diff] [blame] | 1536 | extent_clear_unlock_delalloc(inode, cur_offset, end, end, |
Josef Bacik | c2790a2 | 2013-07-29 11:20:47 -0400 | [diff] [blame] | 1537 | locked_page, EXTENT_LOCKED | |
Josef Bacik | 151a41b | 2013-07-29 13:22:24 -0400 | [diff] [blame] | 1538 | EXTENT_DELALLOC | EXTENT_DEFRAG | |
| 1539 | EXTENT_DO_ACCOUNTING, PAGE_UNLOCK | |
| 1540 | PAGE_CLEAR_DIRTY | |
Josef Bacik | c2790a2 | 2013-07-29 11:20:47 -0400 | [diff] [blame] | 1541 | PAGE_SET_WRITEBACK | |
| 1542 | PAGE_END_WRITEBACK); |
Yan Zheng | 7ea394f | 2008-08-05 13:05:02 -0400 | [diff] [blame] | 1543 | btrfs_free_path(path); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 1544 | return ret; |
Chris Mason | be20aa9 | 2007-12-17 20:14:01 -0500 | [diff] [blame] | 1545 | } |
| 1546 | |
Wang Shilong | 47059d9 | 2014-07-03 18:22:07 +0800 | [diff] [blame] | 1547 | static inline int need_force_cow(struct inode *inode, u64 start, u64 end) |
| 1548 | { |
| 1549 | |
| 1550 | if (!(BTRFS_I(inode)->flags & BTRFS_INODE_NODATACOW) && |
| 1551 | !(BTRFS_I(inode)->flags & BTRFS_INODE_PREALLOC)) |
| 1552 | return 0; |
| 1553 | |
| 1554 | /* |
| 1555 | * @defrag_bytes is a hint value, no spinlock held here, |
| 1556 | * if is not zero, it means the file is defragging. |
| 1557 | * Force cow if given extent needs to be defragged. |
| 1558 | */ |
| 1559 | if (BTRFS_I(inode)->defrag_bytes && |
| 1560 | test_range_bit(&BTRFS_I(inode)->io_tree, start, end, |
| 1561 | EXTENT_DEFRAG, 0, NULL)) |
| 1562 | return 1; |
| 1563 | |
| 1564 | return 0; |
| 1565 | } |
| 1566 | |
Chris Mason | d352ac6 | 2008-09-29 15:18:18 -0400 | [diff] [blame] | 1567 | /* |
| 1568 | * extent_io.c call back to do delayed allocation processing |
| 1569 | */ |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 1570 | static int run_delalloc_range(struct inode *inode, struct page *locked_page, |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 1571 | u64 start, u64 end, int *page_started, |
| 1572 | unsigned long *nr_written) |
Chris Mason | be20aa9 | 2007-12-17 20:14:01 -0500 | [diff] [blame] | 1573 | { |
Chris Mason | be20aa9 | 2007-12-17 20:14:01 -0500 | [diff] [blame] | 1574 | int ret; |
Wang Shilong | 47059d9 | 2014-07-03 18:22:07 +0800 | [diff] [blame] | 1575 | int force_cow = need_force_cow(inode, start, end); |
Chris Mason | a213501 | 2008-06-25 16:01:30 -0400 | [diff] [blame] | 1576 | |
Wang Shilong | 47059d9 | 2014-07-03 18:22:07 +0800 | [diff] [blame] | 1577 | if (BTRFS_I(inode)->flags & BTRFS_INODE_NODATACOW && !force_cow) { |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 1578 | ret = run_delalloc_nocow(inode, locked_page, start, end, |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 1579 | page_started, 1, nr_written); |
Wang Shilong | 47059d9 | 2014-07-03 18:22:07 +0800 | [diff] [blame] | 1580 | } else if (BTRFS_I(inode)->flags & BTRFS_INODE_PREALLOC && !force_cow) { |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 1581 | ret = run_delalloc_nocow(inode, locked_page, start, end, |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 1582 | page_started, 0, nr_written); |
Wang Shilong | 7816030 | 2014-07-17 11:44:10 +0800 | [diff] [blame] | 1583 | } else if (!inode_need_compress(inode)) { |
Wang Xiaoguang | dda3245 | 2016-07-11 11:05:29 +0800 | [diff] [blame] | 1584 | ret = cow_file_range(inode, locked_page, start, end, end, |
| 1585 | page_started, nr_written, 1, NULL); |
Josef Bacik | 7ddf5a4 | 2012-06-08 15:26:47 -0400 | [diff] [blame] | 1586 | } else { |
| 1587 | set_bit(BTRFS_INODE_HAS_ASYNC_EXTENT, |
| 1588 | &BTRFS_I(inode)->runtime_flags); |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 1589 | ret = cow_file_range_async(inode, locked_page, start, end, |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 1590 | page_started, nr_written); |
Josef Bacik | 7ddf5a4 | 2012-06-08 15:26:47 -0400 | [diff] [blame] | 1591 | } |
Chris Mason | b888db2 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 1592 | return ret; |
| 1593 | } |
| 1594 | |
Jeff Mahoney | 1bf8504 | 2011-07-21 16:56:09 +0000 | [diff] [blame] | 1595 | static void btrfs_split_extent_hook(struct inode *inode, |
| 1596 | struct extent_state *orig, u64 split) |
Josef Bacik | 9ed74f2 | 2009-09-11 16:12:44 -0400 | [diff] [blame] | 1597 | { |
Josef Bacik | dcab6a3 | 2015-02-11 15:08:59 -0500 | [diff] [blame] | 1598 | u64 size; |
| 1599 | |
Yan, Zheng | 0ca1f7c | 2010-05-16 10:48:47 -0400 | [diff] [blame] | 1600 | /* not delalloc, ignore it */ |
Josef Bacik | 9ed74f2 | 2009-09-11 16:12:44 -0400 | [diff] [blame] | 1601 | if (!(orig->state & EXTENT_DELALLOC)) |
Jeff Mahoney | 1bf8504 | 2011-07-21 16:56:09 +0000 | [diff] [blame] | 1602 | return; |
Josef Bacik | 9ed74f2 | 2009-09-11 16:12:44 -0400 | [diff] [blame] | 1603 | |
Josef Bacik | dcab6a3 | 2015-02-11 15:08:59 -0500 | [diff] [blame] | 1604 | size = orig->end - orig->start + 1; |
| 1605 | if (size > BTRFS_MAX_EXTENT_SIZE) { |
| 1606 | u64 num_extents; |
| 1607 | u64 new_size; |
| 1608 | |
| 1609 | /* |
Josef Bacik | ba11721 | 2015-03-13 15:01:24 -0400 | [diff] [blame] | 1610 | * See the explanation in btrfs_merge_extent_hook, the same |
| 1611 | * applies here, just in reverse. |
Josef Bacik | dcab6a3 | 2015-02-11 15:08:59 -0500 | [diff] [blame] | 1612 | */ |
| 1613 | new_size = orig->end - split + 1; |
Josef Bacik | ba11721 | 2015-03-13 15:01:24 -0400 | [diff] [blame] | 1614 | num_extents = div64_u64(new_size + BTRFS_MAX_EXTENT_SIZE - 1, |
Josef Bacik | dcab6a3 | 2015-02-11 15:08:59 -0500 | [diff] [blame] | 1615 | BTRFS_MAX_EXTENT_SIZE); |
Josef Bacik | ba11721 | 2015-03-13 15:01:24 -0400 | [diff] [blame] | 1616 | new_size = split - orig->start; |
| 1617 | num_extents += div64_u64(new_size + BTRFS_MAX_EXTENT_SIZE - 1, |
| 1618 | BTRFS_MAX_EXTENT_SIZE); |
| 1619 | if (div64_u64(size + BTRFS_MAX_EXTENT_SIZE - 1, |
| 1620 | BTRFS_MAX_EXTENT_SIZE) >= num_extents) |
Josef Bacik | dcab6a3 | 2015-02-11 15:08:59 -0500 | [diff] [blame] | 1621 | return; |
| 1622 | } |
| 1623 | |
Josef Bacik | 9e0baf6 | 2011-07-15 15:16:44 +0000 | [diff] [blame] | 1624 | spin_lock(&BTRFS_I(inode)->lock); |
| 1625 | BTRFS_I(inode)->outstanding_extents++; |
| 1626 | spin_unlock(&BTRFS_I(inode)->lock); |
Josef Bacik | 9ed74f2 | 2009-09-11 16:12:44 -0400 | [diff] [blame] | 1627 | } |
| 1628 | |
| 1629 | /* |
| 1630 | * extent_io.c merge_extent_hook, used to track merged delayed allocation |
| 1631 | * extents so we can keep track of new extents that are just merged onto old |
| 1632 | * extents, such as when we are doing sequential writes, so we can properly |
| 1633 | * account for the metadata space we'll need. |
| 1634 | */ |
Jeff Mahoney | 1bf8504 | 2011-07-21 16:56:09 +0000 | [diff] [blame] | 1635 | static void btrfs_merge_extent_hook(struct inode *inode, |
| 1636 | struct extent_state *new, |
| 1637 | struct extent_state *other) |
Josef Bacik | 9ed74f2 | 2009-09-11 16:12:44 -0400 | [diff] [blame] | 1638 | { |
Josef Bacik | dcab6a3 | 2015-02-11 15:08:59 -0500 | [diff] [blame] | 1639 | u64 new_size, old_size; |
| 1640 | u64 num_extents; |
| 1641 | |
Josef Bacik | 9ed74f2 | 2009-09-11 16:12:44 -0400 | [diff] [blame] | 1642 | /* not delalloc, ignore it */ |
| 1643 | if (!(other->state & EXTENT_DELALLOC)) |
Jeff Mahoney | 1bf8504 | 2011-07-21 16:56:09 +0000 | [diff] [blame] | 1644 | return; |
Josef Bacik | 9ed74f2 | 2009-09-11 16:12:44 -0400 | [diff] [blame] | 1645 | |
Josef Bacik | 8461a3d | 2015-03-13 15:12:08 -0400 | [diff] [blame] | 1646 | if (new->start > other->start) |
| 1647 | new_size = new->end - other->start + 1; |
| 1648 | else |
| 1649 | new_size = other->end - new->start + 1; |
Josef Bacik | dcab6a3 | 2015-02-11 15:08:59 -0500 | [diff] [blame] | 1650 | |
| 1651 | /* we're not bigger than the max, unreserve the space and go */ |
| 1652 | if (new_size <= BTRFS_MAX_EXTENT_SIZE) { |
| 1653 | spin_lock(&BTRFS_I(inode)->lock); |
| 1654 | BTRFS_I(inode)->outstanding_extents--; |
| 1655 | spin_unlock(&BTRFS_I(inode)->lock); |
| 1656 | return; |
| 1657 | } |
| 1658 | |
| 1659 | /* |
Josef Bacik | ba11721 | 2015-03-13 15:01:24 -0400 | [diff] [blame] | 1660 | * We have to add up either side to figure out how many extents were |
| 1661 | * accounted for before we merged into one big extent. If the number of |
| 1662 | * extents we accounted for is <= the amount we need for the new range |
| 1663 | * then we can return, otherwise drop. Think of it like this |
| 1664 | * |
| 1665 | * [ 4k][MAX_SIZE] |
| 1666 | * |
| 1667 | * So we've grown the extent by a MAX_SIZE extent, this would mean we |
| 1668 | * need 2 outstanding extents, on one side we have 1 and the other side |
| 1669 | * we have 1 so they are == and we can return. But in this case |
| 1670 | * |
| 1671 | * [MAX_SIZE+4k][MAX_SIZE+4k] |
| 1672 | * |
| 1673 | * Each range on their own accounts for 2 extents, but merged together |
| 1674 | * they are only 3 extents worth of accounting, so we need to drop in |
| 1675 | * this case. |
Josef Bacik | dcab6a3 | 2015-02-11 15:08:59 -0500 | [diff] [blame] | 1676 | */ |
Josef Bacik | ba11721 | 2015-03-13 15:01:24 -0400 | [diff] [blame] | 1677 | old_size = other->end - other->start + 1; |
Josef Bacik | dcab6a3 | 2015-02-11 15:08:59 -0500 | [diff] [blame] | 1678 | num_extents = div64_u64(old_size + BTRFS_MAX_EXTENT_SIZE - 1, |
| 1679 | BTRFS_MAX_EXTENT_SIZE); |
Josef Bacik | ba11721 | 2015-03-13 15:01:24 -0400 | [diff] [blame] | 1680 | old_size = new->end - new->start + 1; |
| 1681 | num_extents += div64_u64(old_size + BTRFS_MAX_EXTENT_SIZE - 1, |
| 1682 | BTRFS_MAX_EXTENT_SIZE); |
| 1683 | |
Josef Bacik | dcab6a3 | 2015-02-11 15:08:59 -0500 | [diff] [blame] | 1684 | if (div64_u64(new_size + BTRFS_MAX_EXTENT_SIZE - 1, |
Josef Bacik | ba11721 | 2015-03-13 15:01:24 -0400 | [diff] [blame] | 1685 | BTRFS_MAX_EXTENT_SIZE) >= num_extents) |
Josef Bacik | dcab6a3 | 2015-02-11 15:08:59 -0500 | [diff] [blame] | 1686 | return; |
| 1687 | |
Josef Bacik | 9e0baf6 | 2011-07-15 15:16:44 +0000 | [diff] [blame] | 1688 | spin_lock(&BTRFS_I(inode)->lock); |
| 1689 | BTRFS_I(inode)->outstanding_extents--; |
| 1690 | spin_unlock(&BTRFS_I(inode)->lock); |
Josef Bacik | 9ed74f2 | 2009-09-11 16:12:44 -0400 | [diff] [blame] | 1691 | } |
| 1692 | |
Miao Xie | eb73c1b | 2013-05-15 07:48:22 +0000 | [diff] [blame] | 1693 | static void btrfs_add_delalloc_inodes(struct btrfs_root *root, |
| 1694 | struct inode *inode) |
| 1695 | { |
| 1696 | spin_lock(&root->delalloc_lock); |
| 1697 | if (list_empty(&BTRFS_I(inode)->delalloc_inodes)) { |
| 1698 | list_add_tail(&BTRFS_I(inode)->delalloc_inodes, |
| 1699 | &root->delalloc_inodes); |
| 1700 | set_bit(BTRFS_INODE_IN_DELALLOC_LIST, |
| 1701 | &BTRFS_I(inode)->runtime_flags); |
| 1702 | root->nr_delalloc_inodes++; |
| 1703 | if (root->nr_delalloc_inodes == 1) { |
| 1704 | spin_lock(&root->fs_info->delalloc_root_lock); |
| 1705 | BUG_ON(!list_empty(&root->delalloc_root)); |
| 1706 | list_add_tail(&root->delalloc_root, |
| 1707 | &root->fs_info->delalloc_roots); |
| 1708 | spin_unlock(&root->fs_info->delalloc_root_lock); |
| 1709 | } |
| 1710 | } |
| 1711 | spin_unlock(&root->delalloc_lock); |
| 1712 | } |
| 1713 | |
| 1714 | static void btrfs_del_delalloc_inode(struct btrfs_root *root, |
| 1715 | struct inode *inode) |
| 1716 | { |
| 1717 | spin_lock(&root->delalloc_lock); |
| 1718 | if (!list_empty(&BTRFS_I(inode)->delalloc_inodes)) { |
| 1719 | list_del_init(&BTRFS_I(inode)->delalloc_inodes); |
| 1720 | clear_bit(BTRFS_INODE_IN_DELALLOC_LIST, |
| 1721 | &BTRFS_I(inode)->runtime_flags); |
| 1722 | root->nr_delalloc_inodes--; |
| 1723 | if (!root->nr_delalloc_inodes) { |
| 1724 | spin_lock(&root->fs_info->delalloc_root_lock); |
| 1725 | BUG_ON(list_empty(&root->delalloc_root)); |
| 1726 | list_del_init(&root->delalloc_root); |
| 1727 | spin_unlock(&root->fs_info->delalloc_root_lock); |
| 1728 | } |
| 1729 | } |
| 1730 | spin_unlock(&root->delalloc_lock); |
| 1731 | } |
| 1732 | |
Chris Mason | d352ac6 | 2008-09-29 15:18:18 -0400 | [diff] [blame] | 1733 | /* |
| 1734 | * extent_io.c set_bit_hook, used to track delayed allocation |
| 1735 | * bytes in this file, and to maintain the list of inodes that |
| 1736 | * have pending delalloc work to be done. |
| 1737 | */ |
Jeff Mahoney | 1bf8504 | 2011-07-21 16:56:09 +0000 | [diff] [blame] | 1738 | static void btrfs_set_bit_hook(struct inode *inode, |
David Sterba | 9ee49a04 | 2015-01-14 19:52:13 +0100 | [diff] [blame] | 1739 | struct extent_state *state, unsigned *bits) |
Chris Mason | 291d673 | 2008-01-29 15:55:23 -0500 | [diff] [blame] | 1740 | { |
Josef Bacik | 9ed74f2 | 2009-09-11 16:12:44 -0400 | [diff] [blame] | 1741 | |
Wang Shilong | 47059d9 | 2014-07-03 18:22:07 +0800 | [diff] [blame] | 1742 | if ((*bits & EXTENT_DEFRAG) && !(*bits & EXTENT_DELALLOC)) |
| 1743 | WARN_ON(1); |
Chris Mason | 75eff68 | 2008-12-15 15:54:40 -0500 | [diff] [blame] | 1744 | /* |
| 1745 | * set_bit and clear bit hooks normally require _irqsave/restore |
Sergei Trofimovich | 27160b6 | 2011-05-20 20:20:32 +0000 | [diff] [blame] | 1746 | * but in this case, we are only testing for the DELALLOC |
Chris Mason | 75eff68 | 2008-12-15 15:54:40 -0500 | [diff] [blame] | 1747 | * bit, which is only set or cleared with irqs on |
| 1748 | */ |
Yan, Zheng | 0ca1f7c | 2010-05-16 10:48:47 -0400 | [diff] [blame] | 1749 | if (!(state->state & EXTENT_DELALLOC) && (*bits & EXTENT_DELALLOC)) { |
Chris Mason | 291d673 | 2008-01-29 15:55:23 -0500 | [diff] [blame] | 1750 | struct btrfs_root *root = BTRFS_I(inode)->root; |
Yan, Zheng | 0ca1f7c | 2010-05-16 10:48:47 -0400 | [diff] [blame] | 1751 | u64 len = state->end + 1 - state->start; |
Liu Bo | 83eea1f | 2012-07-10 05:28:39 -0600 | [diff] [blame] | 1752 | bool do_list = !btrfs_is_free_space_inode(inode); |
Josef Bacik | 9ed74f2 | 2009-09-11 16:12:44 -0400 | [diff] [blame] | 1753 | |
Josef Bacik | 9e0baf6 | 2011-07-15 15:16:44 +0000 | [diff] [blame] | 1754 | if (*bits & EXTENT_FIRST_DELALLOC) { |
Yan, Zheng | 0ca1f7c | 2010-05-16 10:48:47 -0400 | [diff] [blame] | 1755 | *bits &= ~EXTENT_FIRST_DELALLOC; |
Josef Bacik | 9e0baf6 | 2011-07-15 15:16:44 +0000 | [diff] [blame] | 1756 | } else { |
| 1757 | spin_lock(&BTRFS_I(inode)->lock); |
| 1758 | BTRFS_I(inode)->outstanding_extents++; |
| 1759 | spin_unlock(&BTRFS_I(inode)->lock); |
| 1760 | } |
Josef Bacik | 287a0ab | 2010-03-19 18:07:23 +0000 | [diff] [blame] | 1761 | |
Josef Bacik | 6a3891c | 2015-03-16 17:38:52 -0400 | [diff] [blame] | 1762 | /* For sanity tests */ |
Jeff Mahoney | f5ee5c9 | 2016-06-21 09:52:41 -0400 | [diff] [blame] | 1763 | if (btrfs_is_testing(root->fs_info)) |
Josef Bacik | 6a3891c | 2015-03-16 17:38:52 -0400 | [diff] [blame] | 1764 | return; |
| 1765 | |
Miao Xie | 963d678 | 2013-01-29 10:10:51 +0000 | [diff] [blame] | 1766 | __percpu_counter_add(&root->fs_info->delalloc_bytes, len, |
| 1767 | root->fs_info->delalloc_batch); |
Miao Xie | df0af1a | 2013-01-29 10:11:59 +0000 | [diff] [blame] | 1768 | spin_lock(&BTRFS_I(inode)->lock); |
Yan, Zheng | 0ca1f7c | 2010-05-16 10:48:47 -0400 | [diff] [blame] | 1769 | BTRFS_I(inode)->delalloc_bytes += len; |
Wang Shilong | 47059d9 | 2014-07-03 18:22:07 +0800 | [diff] [blame] | 1770 | if (*bits & EXTENT_DEFRAG) |
| 1771 | BTRFS_I(inode)->defrag_bytes += len; |
Miao Xie | df0af1a | 2013-01-29 10:11:59 +0000 | [diff] [blame] | 1772 | if (do_list && !test_bit(BTRFS_INODE_IN_DELALLOC_LIST, |
Miao Xie | eb73c1b | 2013-05-15 07:48:22 +0000 | [diff] [blame] | 1773 | &BTRFS_I(inode)->runtime_flags)) |
| 1774 | btrfs_add_delalloc_inodes(root, inode); |
Miao Xie | df0af1a | 2013-01-29 10:11:59 +0000 | [diff] [blame] | 1775 | spin_unlock(&BTRFS_I(inode)->lock); |
Chris Mason | 291d673 | 2008-01-29 15:55:23 -0500 | [diff] [blame] | 1776 | } |
Chris Mason | 291d673 | 2008-01-29 15:55:23 -0500 | [diff] [blame] | 1777 | } |
| 1778 | |
Chris Mason | d352ac6 | 2008-09-29 15:18:18 -0400 | [diff] [blame] | 1779 | /* |
| 1780 | * extent_io.c clear_bit_hook, see set_bit_hook for why |
| 1781 | */ |
Jeff Mahoney | 1bf8504 | 2011-07-21 16:56:09 +0000 | [diff] [blame] | 1782 | static void btrfs_clear_bit_hook(struct inode *inode, |
David Sterba | 4107488 | 2013-04-29 13:38:46 +0000 | [diff] [blame] | 1783 | struct extent_state *state, |
David Sterba | 9ee49a04 | 2015-01-14 19:52:13 +0100 | [diff] [blame] | 1784 | unsigned *bits) |
Chris Mason | 291d673 | 2008-01-29 15:55:23 -0500 | [diff] [blame] | 1785 | { |
Wang Shilong | 47059d9 | 2014-07-03 18:22:07 +0800 | [diff] [blame] | 1786 | u64 len = state->end + 1 - state->start; |
Josef Bacik | dcab6a3 | 2015-02-11 15:08:59 -0500 | [diff] [blame] | 1787 | u64 num_extents = div64_u64(len + BTRFS_MAX_EXTENT_SIZE -1, |
| 1788 | BTRFS_MAX_EXTENT_SIZE); |
Wang Shilong | 47059d9 | 2014-07-03 18:22:07 +0800 | [diff] [blame] | 1789 | |
| 1790 | spin_lock(&BTRFS_I(inode)->lock); |
| 1791 | if ((state->state & EXTENT_DEFRAG) && (*bits & EXTENT_DEFRAG)) |
| 1792 | BTRFS_I(inode)->defrag_bytes -= len; |
| 1793 | spin_unlock(&BTRFS_I(inode)->lock); |
| 1794 | |
Chris Mason | 75eff68 | 2008-12-15 15:54:40 -0500 | [diff] [blame] | 1795 | /* |
| 1796 | * set_bit and clear bit hooks normally require _irqsave/restore |
Sergei Trofimovich | 27160b6 | 2011-05-20 20:20:32 +0000 | [diff] [blame] | 1797 | * but in this case, we are only testing for the DELALLOC |
Chris Mason | 75eff68 | 2008-12-15 15:54:40 -0500 | [diff] [blame] | 1798 | * bit, which is only set or cleared with irqs on |
| 1799 | */ |
Yan, Zheng | 0ca1f7c | 2010-05-16 10:48:47 -0400 | [diff] [blame] | 1800 | if ((state->state & EXTENT_DELALLOC) && (*bits & EXTENT_DELALLOC)) { |
Chris Mason | 291d673 | 2008-01-29 15:55:23 -0500 | [diff] [blame] | 1801 | struct btrfs_root *root = BTRFS_I(inode)->root; |
Liu Bo | 83eea1f | 2012-07-10 05:28:39 -0600 | [diff] [blame] | 1802 | bool do_list = !btrfs_is_free_space_inode(inode); |
Chris Mason | bcbfce8 | 2008-04-22 13:26:47 -0400 | [diff] [blame] | 1803 | |
Josef Bacik | 9e0baf6 | 2011-07-15 15:16:44 +0000 | [diff] [blame] | 1804 | if (*bits & EXTENT_FIRST_DELALLOC) { |
Yan, Zheng | 0ca1f7c | 2010-05-16 10:48:47 -0400 | [diff] [blame] | 1805 | *bits &= ~EXTENT_FIRST_DELALLOC; |
Josef Bacik | 9e0baf6 | 2011-07-15 15:16:44 +0000 | [diff] [blame] | 1806 | } else if (!(*bits & EXTENT_DO_ACCOUNTING)) { |
| 1807 | spin_lock(&BTRFS_I(inode)->lock); |
Josef Bacik | dcab6a3 | 2015-02-11 15:08:59 -0500 | [diff] [blame] | 1808 | BTRFS_I(inode)->outstanding_extents -= num_extents; |
Josef Bacik | 9e0baf6 | 2011-07-15 15:16:44 +0000 | [diff] [blame] | 1809 | spin_unlock(&BTRFS_I(inode)->lock); |
| 1810 | } |
Yan, Zheng | 0ca1f7c | 2010-05-16 10:48:47 -0400 | [diff] [blame] | 1811 | |
Josef Bacik | b6d08f0 | 2013-09-27 14:57:43 -0400 | [diff] [blame] | 1812 | /* |
| 1813 | * We don't reserve metadata space for space cache inodes so we |
| 1814 | * don't need to call dellalloc_release_metadata if there is an |
| 1815 | * error. |
| 1816 | */ |
| 1817 | if (*bits & EXTENT_DO_ACCOUNTING && |
| 1818 | root != root->fs_info->tree_root) |
Yan, Zheng | 0ca1f7c | 2010-05-16 10:48:47 -0400 | [diff] [blame] | 1819 | btrfs_delalloc_release_metadata(inode, len); |
| 1820 | |
Josef Bacik | 6a3891c | 2015-03-16 17:38:52 -0400 | [diff] [blame] | 1821 | /* For sanity tests. */ |
Jeff Mahoney | f5ee5c9 | 2016-06-21 09:52:41 -0400 | [diff] [blame] | 1822 | if (btrfs_is_testing(root->fs_info)) |
Josef Bacik | 6a3891c | 2015-03-16 17:38:52 -0400 | [diff] [blame] | 1823 | return; |
| 1824 | |
Josef Bacik | 0cb59c9 | 2010-07-02 12:14:14 -0400 | [diff] [blame] | 1825 | if (root->root_key.objectid != BTRFS_DATA_RELOC_TREE_OBJECTID |
Wang Xiaoguang | 1851309 | 2016-07-25 15:51:40 +0800 | [diff] [blame] | 1826 | && do_list && !(state->state & EXTENT_NORESERVE) |
| 1827 | && (*bits & (EXTENT_DO_ACCOUNTING | |
| 1828 | EXTENT_CLEAR_DATA_RESV))) |
Qu Wenruo | 51773be | 2015-10-08 18:19:37 +0800 | [diff] [blame] | 1829 | btrfs_free_reserved_data_space_noquota(inode, |
| 1830 | state->start, len); |
Josef Bacik | 9ed74f2 | 2009-09-11 16:12:44 -0400 | [diff] [blame] | 1831 | |
Miao Xie | 963d678 | 2013-01-29 10:10:51 +0000 | [diff] [blame] | 1832 | __percpu_counter_add(&root->fs_info->delalloc_bytes, -len, |
| 1833 | root->fs_info->delalloc_batch); |
Miao Xie | df0af1a | 2013-01-29 10:11:59 +0000 | [diff] [blame] | 1834 | spin_lock(&BTRFS_I(inode)->lock); |
Yan, Zheng | 0ca1f7c | 2010-05-16 10:48:47 -0400 | [diff] [blame] | 1835 | BTRFS_I(inode)->delalloc_bytes -= len; |
Josef Bacik | 0cb59c9 | 2010-07-02 12:14:14 -0400 | [diff] [blame] | 1836 | if (do_list && BTRFS_I(inode)->delalloc_bytes == 0 && |
Miao Xie | df0af1a | 2013-01-29 10:11:59 +0000 | [diff] [blame] | 1837 | test_bit(BTRFS_INODE_IN_DELALLOC_LIST, |
Miao Xie | eb73c1b | 2013-05-15 07:48:22 +0000 | [diff] [blame] | 1838 | &BTRFS_I(inode)->runtime_flags)) |
| 1839 | btrfs_del_delalloc_inode(root, inode); |
Miao Xie | df0af1a | 2013-01-29 10:11:59 +0000 | [diff] [blame] | 1840 | spin_unlock(&BTRFS_I(inode)->lock); |
Chris Mason | 291d673 | 2008-01-29 15:55:23 -0500 | [diff] [blame] | 1841 | } |
Chris Mason | 291d673 | 2008-01-29 15:55:23 -0500 | [diff] [blame] | 1842 | } |
| 1843 | |
Chris Mason | d352ac6 | 2008-09-29 15:18:18 -0400 | [diff] [blame] | 1844 | /* |
| 1845 | * extent_io.c merge_bio_hook, this must check the chunk tree to make sure |
| 1846 | * we don't create bios that span stripes or chunks |
Liu Bo | 6f034ec | 2016-06-22 18:31:49 -0700 | [diff] [blame] | 1847 | * |
| 1848 | * return 1 if page cannot be merged to bio |
| 1849 | * return 0 if page can be merged to bio |
| 1850 | * return error otherwise |
Chris Mason | d352ac6 | 2008-09-29 15:18:18 -0400 | [diff] [blame] | 1851 | */ |
Mike Christie | 81a75f6 | 2016-06-05 14:31:54 -0500 | [diff] [blame] | 1852 | int btrfs_merge_bio_hook(struct page *page, unsigned long offset, |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 1853 | size_t size, struct bio *bio, |
| 1854 | unsigned long bio_flags) |
Chris Mason | 239b14b | 2008-03-24 15:02:07 -0400 | [diff] [blame] | 1855 | { |
| 1856 | struct btrfs_root *root = BTRFS_I(page->mapping->host)->root; |
Kent Overstreet | 4f024f3 | 2013-10-11 15:44:27 -0700 | [diff] [blame] | 1857 | u64 logical = (u64)bio->bi_iter.bi_sector << 9; |
Chris Mason | 239b14b | 2008-03-24 15:02:07 -0400 | [diff] [blame] | 1858 | u64 length = 0; |
| 1859 | u64 map_length; |
Chris Mason | 239b14b | 2008-03-24 15:02:07 -0400 | [diff] [blame] | 1860 | int ret; |
| 1861 | |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 1862 | if (bio_flags & EXTENT_BIO_COMPRESSED) |
| 1863 | return 0; |
| 1864 | |
Kent Overstreet | 4f024f3 | 2013-10-11 15:44:27 -0700 | [diff] [blame] | 1865 | length = bio->bi_iter.bi_size; |
Chris Mason | 239b14b | 2008-03-24 15:02:07 -0400 | [diff] [blame] | 1866 | map_length = length; |
Mike Christie | b3d3fa5 | 2016-06-05 14:31:53 -0500 | [diff] [blame] | 1867 | ret = btrfs_map_block(root->fs_info, bio_op(bio), logical, |
Chris Mason | f188591 | 2008-04-09 16:28:12 -0400 | [diff] [blame] | 1868 | &map_length, NULL, 0); |
Liu Bo | 6f034ec | 2016-06-22 18:31:49 -0700 | [diff] [blame] | 1869 | if (ret < 0) |
| 1870 | return ret; |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 1871 | if (map_length < length + size) |
Chris Mason | 239b14b | 2008-03-24 15:02:07 -0400 | [diff] [blame] | 1872 | return 1; |
Jeff Mahoney | 3444a97 | 2011-10-03 23:23:13 -0400 | [diff] [blame] | 1873 | return 0; |
Chris Mason | 239b14b | 2008-03-24 15:02:07 -0400 | [diff] [blame] | 1874 | } |
| 1875 | |
Chris Mason | d352ac6 | 2008-09-29 15:18:18 -0400 | [diff] [blame] | 1876 | /* |
| 1877 | * in order to insert checksums into the metadata in large chunks, |
| 1878 | * we wait until bio submission time. All the pages in the bio are |
| 1879 | * checksummed and sums are attached onto the ordered extent record. |
| 1880 | * |
| 1881 | * At IO completion time the cums attached on the ordered extent record |
| 1882 | * are inserted into the btree |
| 1883 | */ |
Mike Christie | 81a75f6 | 2016-06-05 14:31:54 -0500 | [diff] [blame] | 1884 | static int __btrfs_submit_bio_start(struct inode *inode, struct bio *bio, |
| 1885 | int mirror_num, unsigned long bio_flags, |
Chris Mason | eaf25d9 | 2010-05-25 09:48:28 -0400 | [diff] [blame] | 1886 | u64 bio_offset) |
Chris Mason | 065631f | 2008-02-20 12:07:25 -0500 | [diff] [blame] | 1887 | { |
Chris Mason | 065631f | 2008-02-20 12:07:25 -0500 | [diff] [blame] | 1888 | struct btrfs_root *root = BTRFS_I(inode)->root; |
Chris Mason | 065631f | 2008-02-20 12:07:25 -0500 | [diff] [blame] | 1889 | int ret = 0; |
Chris Mason | e015640 | 2008-04-16 11:15:20 -0400 | [diff] [blame] | 1890 | |
Chris Mason | d20f704 | 2008-12-08 16:58:54 -0500 | [diff] [blame] | 1891 | ret = btrfs_csum_one_bio(root, inode, bio, 0, 0); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 1892 | BUG_ON(ret); /* -ENOMEM */ |
Chris Mason | 4a69a41 | 2008-11-06 22:03:00 -0500 | [diff] [blame] | 1893 | return 0; |
| 1894 | } |
Chris Mason | e015640 | 2008-04-16 11:15:20 -0400 | [diff] [blame] | 1895 | |
Chris Mason | 4a69a41 | 2008-11-06 22:03:00 -0500 | [diff] [blame] | 1896 | /* |
| 1897 | * in order to insert checksums into the metadata in large chunks, |
| 1898 | * we wait until bio submission time. All the pages in the bio are |
| 1899 | * checksummed and sums are attached onto the ordered extent record. |
| 1900 | * |
| 1901 | * At IO completion time the cums attached on the ordered extent record |
| 1902 | * are inserted into the btree |
| 1903 | */ |
Mike Christie | 81a75f6 | 2016-06-05 14:31:54 -0500 | [diff] [blame] | 1904 | static int __btrfs_submit_bio_done(struct inode *inode, struct bio *bio, |
Chris Mason | eaf25d9 | 2010-05-25 09:48:28 -0400 | [diff] [blame] | 1905 | int mirror_num, unsigned long bio_flags, |
| 1906 | u64 bio_offset) |
Chris Mason | 4a69a41 | 2008-11-06 22:03:00 -0500 | [diff] [blame] | 1907 | { |
| 1908 | struct btrfs_root *root = BTRFS_I(inode)->root; |
Stefan Behrens | 6189192 | 2012-11-05 18:51:52 +0100 | [diff] [blame] | 1909 | int ret; |
| 1910 | |
Mike Christie | 81a75f6 | 2016-06-05 14:31:54 -0500 | [diff] [blame] | 1911 | ret = btrfs_map_bio(root, bio, mirror_num, 1); |
Christoph Hellwig | 4246a0b | 2015-07-20 15:29:37 +0200 | [diff] [blame] | 1912 | if (ret) { |
| 1913 | bio->bi_error = ret; |
| 1914 | bio_endio(bio); |
| 1915 | } |
Stefan Behrens | 6189192 | 2012-11-05 18:51:52 +0100 | [diff] [blame] | 1916 | return ret; |
Chris Mason | 44b8bd7 | 2008-04-16 11:14:51 -0400 | [diff] [blame] | 1917 | } |
| 1918 | |
Chris Mason | d352ac6 | 2008-09-29 15:18:18 -0400 | [diff] [blame] | 1919 | /* |
Chris Mason | cad321a | 2008-12-17 14:51:42 -0500 | [diff] [blame] | 1920 | * extent_io.c submission hook. This does the right thing for csum calculation |
| 1921 | * on write, or reading the csums from the tree before a read |
Chris Mason | d352ac6 | 2008-09-29 15:18:18 -0400 | [diff] [blame] | 1922 | */ |
Mike Christie | 81a75f6 | 2016-06-05 14:31:54 -0500 | [diff] [blame] | 1923 | static int btrfs_submit_bio_hook(struct inode *inode, struct bio *bio, |
Chris Mason | eaf25d9 | 2010-05-25 09:48:28 -0400 | [diff] [blame] | 1924 | int mirror_num, unsigned long bio_flags, |
| 1925 | u64 bio_offset) |
Chris Mason | 44b8bd7 | 2008-04-16 11:14:51 -0400 | [diff] [blame] | 1926 | { |
| 1927 | struct btrfs_root *root = BTRFS_I(inode)->root; |
Chandan Rajendra | 0d51e28 | 2015-07-27 15:26:43 +0530 | [diff] [blame] | 1928 | enum btrfs_wq_endio_type metadata = BTRFS_WQ_ENDIO_DATA; |
Chris Mason | 44b8bd7 | 2008-04-16 11:14:51 -0400 | [diff] [blame] | 1929 | int ret = 0; |
Chris Mason | 19b9bdb | 2008-10-30 14:23:13 -0400 | [diff] [blame] | 1930 | int skip_sum; |
Josef Bacik | b812ce2 | 2012-11-16 13:56:32 -0500 | [diff] [blame] | 1931 | int async = !atomic_read(&BTRFS_I(inode)->sync_writers); |
Chris Mason | 44b8bd7 | 2008-04-16 11:14:51 -0400 | [diff] [blame] | 1932 | |
Christoph Hellwig | 6cbff00 | 2009-04-17 10:37:41 +0200 | [diff] [blame] | 1933 | skip_sum = BTRFS_I(inode)->flags & BTRFS_INODE_NODATASUM; |
Chris Mason | cad321a | 2008-12-17 14:51:42 -0500 | [diff] [blame] | 1934 | |
Liu Bo | 83eea1f | 2012-07-10 05:28:39 -0600 | [diff] [blame] | 1935 | if (btrfs_is_free_space_inode(inode)) |
Chandan Rajendra | 0d51e28 | 2015-07-27 15:26:43 +0530 | [diff] [blame] | 1936 | metadata = BTRFS_WQ_ENDIO_FREE_SPACE; |
Jeff Mahoney | 0417341 | 2011-10-03 23:23:12 -0400 | [diff] [blame] | 1937 | |
Mike Christie | 37226b2 | 2016-06-05 14:31:52 -0500 | [diff] [blame] | 1938 | if (bio_op(bio) != REQ_OP_WRITE) { |
Josef Bacik | 5fd0204 | 2012-05-02 14:00:54 -0400 | [diff] [blame] | 1939 | ret = btrfs_bio_wq_end_io(root->fs_info, bio, metadata); |
| 1940 | if (ret) |
Stefan Behrens | 6189192 | 2012-11-05 18:51:52 +0100 | [diff] [blame] | 1941 | goto out; |
Josef Bacik | 5fd0204 | 2012-05-02 14:00:54 -0400 | [diff] [blame] | 1942 | |
Chris Mason | d20f704 | 2008-12-08 16:58:54 -0500 | [diff] [blame] | 1943 | if (bio_flags & EXTENT_BIO_COMPRESSED) { |
Stefan Behrens | 6189192 | 2012-11-05 18:51:52 +0100 | [diff] [blame] | 1944 | ret = btrfs_submit_compressed_read(inode, bio, |
| 1945 | mirror_num, |
| 1946 | bio_flags); |
| 1947 | goto out; |
Tsutomu Itoh | c2db107 | 2011-03-01 06:48:31 +0000 | [diff] [blame] | 1948 | } else if (!skip_sum) { |
| 1949 | ret = btrfs_lookup_bio_sums(root, inode, bio, NULL); |
| 1950 | if (ret) |
Stefan Behrens | 6189192 | 2012-11-05 18:51:52 +0100 | [diff] [blame] | 1951 | goto out; |
Tsutomu Itoh | c2db107 | 2011-03-01 06:48:31 +0000 | [diff] [blame] | 1952 | } |
Chris Mason | 4d1b5fb | 2008-08-20 09:44:52 -0400 | [diff] [blame] | 1953 | goto mapit; |
Josef Bacik | b812ce2 | 2012-11-16 13:56:32 -0500 | [diff] [blame] | 1954 | } else if (async && !skip_sum) { |
Yan Zheng | 17d217f | 2008-12-12 10:03:38 -0500 | [diff] [blame] | 1955 | /* csum items have already been cloned */ |
| 1956 | if (root->root_key.objectid == BTRFS_DATA_RELOC_TREE_OBJECTID) |
| 1957 | goto mapit; |
Chris Mason | 19b9bdb | 2008-10-30 14:23:13 -0400 | [diff] [blame] | 1958 | /* we're doing a write, do the async checksumming */ |
Stefan Behrens | 6189192 | 2012-11-05 18:51:52 +0100 | [diff] [blame] | 1959 | ret = btrfs_wq_submit_bio(BTRFS_I(inode)->root->fs_info, |
Mike Christie | 81a75f6 | 2016-06-05 14:31:54 -0500 | [diff] [blame] | 1960 | inode, bio, mirror_num, |
Chris Mason | eaf25d9 | 2010-05-25 09:48:28 -0400 | [diff] [blame] | 1961 | bio_flags, bio_offset, |
| 1962 | __btrfs_submit_bio_start, |
Chris Mason | 4a69a41 | 2008-11-06 22:03:00 -0500 | [diff] [blame] | 1963 | __btrfs_submit_bio_done); |
Stefan Behrens | 6189192 | 2012-11-05 18:51:52 +0100 | [diff] [blame] | 1964 | goto out; |
Josef Bacik | b812ce2 | 2012-11-16 13:56:32 -0500 | [diff] [blame] | 1965 | } else if (!skip_sum) { |
| 1966 | ret = btrfs_csum_one_bio(root, inode, bio, 0, 0); |
| 1967 | if (ret) |
| 1968 | goto out; |
Chris Mason | 19b9bdb | 2008-10-30 14:23:13 -0400 | [diff] [blame] | 1969 | } |
| 1970 | |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 1971 | mapit: |
Mike Christie | 81a75f6 | 2016-06-05 14:31:54 -0500 | [diff] [blame] | 1972 | ret = btrfs_map_bio(root, bio, mirror_num, 0); |
Stefan Behrens | 6189192 | 2012-11-05 18:51:52 +0100 | [diff] [blame] | 1973 | |
| 1974 | out: |
Christoph Hellwig | 4246a0b | 2015-07-20 15:29:37 +0200 | [diff] [blame] | 1975 | if (ret < 0) { |
| 1976 | bio->bi_error = ret; |
| 1977 | bio_endio(bio); |
| 1978 | } |
Stefan Behrens | 6189192 | 2012-11-05 18:51:52 +0100 | [diff] [blame] | 1979 | return ret; |
Chris Mason | 065631f | 2008-02-20 12:07:25 -0500 | [diff] [blame] | 1980 | } |
Chris Mason | 6885f30 | 2008-02-20 16:11:05 -0500 | [diff] [blame] | 1981 | |
Chris Mason | d352ac6 | 2008-09-29 15:18:18 -0400 | [diff] [blame] | 1982 | /* |
| 1983 | * given a list of ordered sums record them in the inode. This happens |
| 1984 | * at IO completion time based on sums calculated at bio submission time. |
| 1985 | */ |
Chris Mason | ba1da2f | 2008-07-17 12:54:15 -0400 | [diff] [blame] | 1986 | static noinline int add_pending_csums(struct btrfs_trans_handle *trans, |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 1987 | struct inode *inode, u64 file_offset, |
| 1988 | struct list_head *list) |
| 1989 | { |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 1990 | struct btrfs_ordered_sum *sum; |
| 1991 | |
Qinghuang Feng | c6e3087 | 2009-01-21 10:59:08 -0500 | [diff] [blame] | 1992 | list_for_each_entry(sum, list, list) { |
Miao Xie | 39847c4 | 2013-03-28 08:08:20 +0000 | [diff] [blame] | 1993 | trans->adding_csums = 1; |
Chris Mason | d20f704 | 2008-12-08 16:58:54 -0500 | [diff] [blame] | 1994 | btrfs_csum_file_blocks(trans, |
| 1995 | BTRFS_I(inode)->root->fs_info->csum_root, sum); |
Miao Xie | 39847c4 | 2013-03-28 08:08:20 +0000 | [diff] [blame] | 1996 | trans->adding_csums = 0; |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 1997 | } |
| 1998 | return 0; |
| 1999 | } |
| 2000 | |
Josef Bacik | 2ac55d4 | 2010-02-03 19:33:23 +0000 | [diff] [blame] | 2001 | int btrfs_set_extent_delalloc(struct inode *inode, u64 start, u64 end, |
Qu Wenruo | ba8b04c | 2016-07-19 16:50:36 +0800 | [diff] [blame] | 2002 | struct extent_state **cached_state, int dedupe) |
Chris Mason | ea8c281 | 2008-08-04 23:17:27 -0400 | [diff] [blame] | 2003 | { |
Kirill A. Shutemov | 09cbfea | 2016-04-01 15:29:47 +0300 | [diff] [blame] | 2004 | WARN_ON((end & (PAGE_SIZE - 1)) == 0); |
Chris Mason | ea8c281 | 2008-08-04 23:17:27 -0400 | [diff] [blame] | 2005 | return set_extent_delalloc(&BTRFS_I(inode)->io_tree, start, end, |
David Sterba | 7cd8c75 | 2016-04-26 23:54:39 +0200 | [diff] [blame] | 2006 | cached_state); |
Chris Mason | ea8c281 | 2008-08-04 23:17:27 -0400 | [diff] [blame] | 2007 | } |
| 2008 | |
Chris Mason | d352ac6 | 2008-09-29 15:18:18 -0400 | [diff] [blame] | 2009 | /* see btrfs_writepage_start_hook for details on why this is required */ |
Chris Mason | 247e743 | 2008-07-17 12:53:51 -0400 | [diff] [blame] | 2010 | struct btrfs_writepage_fixup { |
| 2011 | struct page *page; |
| 2012 | struct btrfs_work work; |
| 2013 | }; |
| 2014 | |
Christoph Hellwig | b295086 | 2008-12-02 09:54:17 -0500 | [diff] [blame] | 2015 | static void btrfs_writepage_fixup_worker(struct btrfs_work *work) |
Chris Mason | 247e743 | 2008-07-17 12:53:51 -0400 | [diff] [blame] | 2016 | { |
| 2017 | struct btrfs_writepage_fixup *fixup; |
| 2018 | struct btrfs_ordered_extent *ordered; |
Josef Bacik | 2ac55d4 | 2010-02-03 19:33:23 +0000 | [diff] [blame] | 2019 | struct extent_state *cached_state = NULL; |
Chris Mason | 247e743 | 2008-07-17 12:53:51 -0400 | [diff] [blame] | 2020 | struct page *page; |
| 2021 | struct inode *inode; |
| 2022 | u64 page_start; |
| 2023 | u64 page_end; |
Jeff Mahoney | 87826df | 2012-02-15 16:23:57 +0100 | [diff] [blame] | 2024 | int ret; |
Chris Mason | 247e743 | 2008-07-17 12:53:51 -0400 | [diff] [blame] | 2025 | |
| 2026 | fixup = container_of(work, struct btrfs_writepage_fixup, work); |
| 2027 | page = fixup->page; |
Chris Mason | 4a09675 | 2008-07-21 10:29:44 -0400 | [diff] [blame] | 2028 | again: |
Chris Mason | 247e743 | 2008-07-17 12:53:51 -0400 | [diff] [blame] | 2029 | lock_page(page); |
| 2030 | if (!page->mapping || !PageDirty(page) || !PageChecked(page)) { |
| 2031 | ClearPageChecked(page); |
| 2032 | goto out_page; |
| 2033 | } |
| 2034 | |
| 2035 | inode = page->mapping->host; |
| 2036 | page_start = page_offset(page); |
Kirill A. Shutemov | 09cbfea | 2016-04-01 15:29:47 +0300 | [diff] [blame] | 2037 | page_end = page_offset(page) + PAGE_SIZE - 1; |
Chris Mason | 247e743 | 2008-07-17 12:53:51 -0400 | [diff] [blame] | 2038 | |
David Sterba | ff13db4 | 2015-12-03 14:30:40 +0100 | [diff] [blame] | 2039 | lock_extent_bits(&BTRFS_I(inode)->io_tree, page_start, page_end, |
Jeff Mahoney | d008237 | 2012-03-01 14:57:19 +0100 | [diff] [blame] | 2040 | &cached_state); |
Chris Mason | 4a09675 | 2008-07-21 10:29:44 -0400 | [diff] [blame] | 2041 | |
| 2042 | /* already ordered? We're done */ |
Chris Mason | 8b62b72 | 2009-09-02 16:53:46 -0400 | [diff] [blame] | 2043 | if (PagePrivate2(page)) |
Chris Mason | 247e743 | 2008-07-17 12:53:51 -0400 | [diff] [blame] | 2044 | goto out; |
Chris Mason | 4a09675 | 2008-07-21 10:29:44 -0400 | [diff] [blame] | 2045 | |
Chandan Rajendra | dbfdb6d | 2016-01-21 15:55:58 +0530 | [diff] [blame] | 2046 | ordered = btrfs_lookup_ordered_range(inode, page_start, |
Kirill A. Shutemov | 09cbfea | 2016-04-01 15:29:47 +0300 | [diff] [blame] | 2047 | PAGE_SIZE); |
Chris Mason | 4a09675 | 2008-07-21 10:29:44 -0400 | [diff] [blame] | 2048 | if (ordered) { |
Josef Bacik | 2ac55d4 | 2010-02-03 19:33:23 +0000 | [diff] [blame] | 2049 | unlock_extent_cached(&BTRFS_I(inode)->io_tree, page_start, |
| 2050 | page_end, &cached_state, GFP_NOFS); |
Chris Mason | 4a09675 | 2008-07-21 10:29:44 -0400 | [diff] [blame] | 2051 | unlock_page(page); |
| 2052 | btrfs_start_ordered_extent(inode, ordered, 1); |
Jeff Mahoney | 87826df | 2012-02-15 16:23:57 +0100 | [diff] [blame] | 2053 | btrfs_put_ordered_extent(ordered); |
Chris Mason | 4a09675 | 2008-07-21 10:29:44 -0400 | [diff] [blame] | 2054 | goto again; |
| 2055 | } |
Chris Mason | 247e743 | 2008-07-17 12:53:51 -0400 | [diff] [blame] | 2056 | |
Qu Wenruo | 7cf5b97 | 2015-09-08 17:25:55 +0800 | [diff] [blame] | 2057 | ret = btrfs_delalloc_reserve_space(inode, page_start, |
Kirill A. Shutemov | 09cbfea | 2016-04-01 15:29:47 +0300 | [diff] [blame] | 2058 | PAGE_SIZE); |
Jeff Mahoney | 87826df | 2012-02-15 16:23:57 +0100 | [diff] [blame] | 2059 | if (ret) { |
| 2060 | mapping_set_error(page->mapping, ret); |
| 2061 | end_extent_writepage(page, ret, page_start, page_end); |
| 2062 | ClearPageChecked(page); |
| 2063 | goto out; |
| 2064 | } |
| 2065 | |
Qu Wenruo | ba8b04c | 2016-07-19 16:50:36 +0800 | [diff] [blame] | 2066 | btrfs_set_extent_delalloc(inode, page_start, page_end, &cached_state, |
| 2067 | 0); |
Chris Mason | 247e743 | 2008-07-17 12:53:51 -0400 | [diff] [blame] | 2068 | ClearPageChecked(page); |
Jeff Mahoney | 87826df | 2012-02-15 16:23:57 +0100 | [diff] [blame] | 2069 | set_page_dirty(page); |
Chris Mason | 247e743 | 2008-07-17 12:53:51 -0400 | [diff] [blame] | 2070 | out: |
Josef Bacik | 2ac55d4 | 2010-02-03 19:33:23 +0000 | [diff] [blame] | 2071 | unlock_extent_cached(&BTRFS_I(inode)->io_tree, page_start, page_end, |
| 2072 | &cached_state, GFP_NOFS); |
Chris Mason | 247e743 | 2008-07-17 12:53:51 -0400 | [diff] [blame] | 2073 | out_page: |
| 2074 | unlock_page(page); |
Kirill A. Shutemov | 09cbfea | 2016-04-01 15:29:47 +0300 | [diff] [blame] | 2075 | put_page(page); |
Miao Xie | b897abe | 2011-01-26 16:19:22 +0800 | [diff] [blame] | 2076 | kfree(fixup); |
Chris Mason | 247e743 | 2008-07-17 12:53:51 -0400 | [diff] [blame] | 2077 | } |
| 2078 | |
| 2079 | /* |
| 2080 | * There are a few paths in the higher layers of the kernel that directly |
| 2081 | * set the page dirty bit without asking the filesystem if it is a |
| 2082 | * good idea. This causes problems because we want to make sure COW |
| 2083 | * properly happens and the data=ordered rules are followed. |
| 2084 | * |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 2085 | * 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] | 2086 | * hasn't been properly setup for IO. We kick off an async process |
| 2087 | * to fix it up. The async helper will wait for ordered extents, set |
| 2088 | * the delalloc bit and make it safe to write the page. |
| 2089 | */ |
Christoph Hellwig | b295086 | 2008-12-02 09:54:17 -0500 | [diff] [blame] | 2090 | 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] | 2091 | { |
| 2092 | struct inode *inode = page->mapping->host; |
| 2093 | struct btrfs_writepage_fixup *fixup; |
| 2094 | struct btrfs_root *root = BTRFS_I(inode)->root; |
Chris Mason | 247e743 | 2008-07-17 12:53:51 -0400 | [diff] [blame] | 2095 | |
Chris Mason | 8b62b72 | 2009-09-02 16:53:46 -0400 | [diff] [blame] | 2096 | /* this page is properly in the ordered list */ |
| 2097 | if (TestClearPagePrivate2(page)) |
Chris Mason | 247e743 | 2008-07-17 12:53:51 -0400 | [diff] [blame] | 2098 | return 0; |
| 2099 | |
| 2100 | if (PageChecked(page)) |
| 2101 | return -EAGAIN; |
| 2102 | |
| 2103 | fixup = kzalloc(sizeof(*fixup), GFP_NOFS); |
| 2104 | if (!fixup) |
| 2105 | return -EAGAIN; |
Chris Mason | f421950 | 2008-07-22 11:18:09 -0400 | [diff] [blame] | 2106 | |
Chris Mason | 247e743 | 2008-07-17 12:53:51 -0400 | [diff] [blame] | 2107 | SetPageChecked(page); |
Kirill A. Shutemov | 09cbfea | 2016-04-01 15:29:47 +0300 | [diff] [blame] | 2108 | get_page(page); |
Liu Bo | 9e0af23 | 2014-08-15 23:36:53 +0800 | [diff] [blame] | 2109 | btrfs_init_work(&fixup->work, btrfs_fixup_helper, |
| 2110 | btrfs_writepage_fixup_worker, NULL, NULL); |
Chris Mason | 247e743 | 2008-07-17 12:53:51 -0400 | [diff] [blame] | 2111 | fixup->page = page; |
Qu Wenruo | dc6e320 | 2014-02-28 10:46:14 +0800 | [diff] [blame] | 2112 | btrfs_queue_work(root->fs_info->fixup_workers, &fixup->work); |
Jeff Mahoney | 87826df | 2012-02-15 16:23:57 +0100 | [diff] [blame] | 2113 | return -EBUSY; |
Chris Mason | 247e743 | 2008-07-17 12:53:51 -0400 | [diff] [blame] | 2114 | } |
| 2115 | |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 2116 | static int insert_reserved_file_extent(struct btrfs_trans_handle *trans, |
| 2117 | struct inode *inode, u64 file_pos, |
| 2118 | u64 disk_bytenr, u64 disk_num_bytes, |
| 2119 | u64 num_bytes, u64 ram_bytes, |
| 2120 | u8 compression, u8 encryption, |
| 2121 | u16 other_encoding, int extent_type) |
| 2122 | { |
| 2123 | struct btrfs_root *root = BTRFS_I(inode)->root; |
| 2124 | struct btrfs_file_extent_item *fi; |
| 2125 | struct btrfs_path *path; |
| 2126 | struct extent_buffer *leaf; |
| 2127 | struct btrfs_key ins; |
Filipe David Borba Manana | 1acae57 | 2014-01-07 11:42:27 +0000 | [diff] [blame] | 2128 | int extent_inserted = 0; |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 2129 | int ret; |
| 2130 | |
| 2131 | path = btrfs_alloc_path(); |
Mark Fasheh | d8926bb | 2011-07-13 10:38:47 -0700 | [diff] [blame] | 2132 | if (!path) |
| 2133 | return -ENOMEM; |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 2134 | |
Chris Mason | a1ed835 | 2009-09-11 12:27:37 -0400 | [diff] [blame] | 2135 | /* |
| 2136 | * we may be replacing one extent in the tree with another. |
| 2137 | * The new extent is pinned in the extent map, and we don't want |
| 2138 | * to drop it from the cache until it is completely in the btree. |
| 2139 | * |
| 2140 | * So, tell btrfs_drop_extents to leave this extent in the cache. |
| 2141 | * the caller is expected to unpin it and allow it to be merged |
| 2142 | * with the others. |
| 2143 | */ |
Filipe David Borba Manana | 1acae57 | 2014-01-07 11:42:27 +0000 | [diff] [blame] | 2144 | ret = __btrfs_drop_extents(trans, root, inode, path, file_pos, |
| 2145 | file_pos + num_bytes, NULL, 0, |
| 2146 | 1, sizeof(*fi), &extent_inserted); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 2147 | if (ret) |
| 2148 | goto out; |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 2149 | |
Filipe David Borba Manana | 1acae57 | 2014-01-07 11:42:27 +0000 | [diff] [blame] | 2150 | if (!extent_inserted) { |
| 2151 | ins.objectid = btrfs_ino(inode); |
| 2152 | ins.offset = file_pos; |
| 2153 | ins.type = BTRFS_EXTENT_DATA_KEY; |
| 2154 | |
| 2155 | path->leave_spinning = 1; |
| 2156 | ret = btrfs_insert_empty_item(trans, root, path, &ins, |
| 2157 | sizeof(*fi)); |
| 2158 | if (ret) |
| 2159 | goto out; |
| 2160 | } |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 2161 | leaf = path->nodes[0]; |
| 2162 | fi = btrfs_item_ptr(leaf, path->slots[0], |
| 2163 | struct btrfs_file_extent_item); |
| 2164 | btrfs_set_file_extent_generation(leaf, fi, trans->transid); |
| 2165 | btrfs_set_file_extent_type(leaf, fi, extent_type); |
| 2166 | btrfs_set_file_extent_disk_bytenr(leaf, fi, disk_bytenr); |
| 2167 | btrfs_set_file_extent_disk_num_bytes(leaf, fi, disk_num_bytes); |
| 2168 | btrfs_set_file_extent_offset(leaf, fi, 0); |
| 2169 | btrfs_set_file_extent_num_bytes(leaf, fi, num_bytes); |
| 2170 | btrfs_set_file_extent_ram_bytes(leaf, fi, ram_bytes); |
| 2171 | btrfs_set_file_extent_compression(leaf, fi, compression); |
| 2172 | btrfs_set_file_extent_encryption(leaf, fi, encryption); |
| 2173 | btrfs_set_file_extent_other_encoding(leaf, fi, other_encoding); |
Chris Mason | b947343 | 2009-03-13 11:00:37 -0400 | [diff] [blame] | 2174 | |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 2175 | btrfs_mark_buffer_dirty(leaf); |
Josef Bacik | ce19533 | 2012-09-25 15:26:16 -0400 | [diff] [blame] | 2176 | btrfs_release_path(path); |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 2177 | |
| 2178 | inode_add_bytes(inode, num_bytes); |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 2179 | |
| 2180 | ins.objectid = disk_bytenr; |
| 2181 | ins.offset = disk_num_bytes; |
| 2182 | ins.type = BTRFS_EXTENT_ITEM_KEY; |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 2183 | ret = btrfs_alloc_reserved_file_extent(trans, root, |
| 2184 | root->root_key.objectid, |
Qu Wenruo | 5846a3c | 2015-10-26 14:11:18 +0800 | [diff] [blame] | 2185 | btrfs_ino(inode), file_pos, |
| 2186 | ram_bytes, &ins); |
Qu Wenruo | 297d750 | 2015-09-08 17:08:37 +0800 | [diff] [blame] | 2187 | /* |
Qu Wenruo | 5846a3c | 2015-10-26 14:11:18 +0800 | [diff] [blame] | 2188 | * Release the reserved range from inode dirty range map, as it is |
| 2189 | * already moved into delayed_ref_head |
Qu Wenruo | 297d750 | 2015-09-08 17:08:37 +0800 | [diff] [blame] | 2190 | */ |
| 2191 | btrfs_qgroup_release_data(inode, file_pos, ram_bytes); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 2192 | out: |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 2193 | btrfs_free_path(path); |
Chris Mason | b947343 | 2009-03-13 11:00:37 -0400 | [diff] [blame] | 2194 | |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 2195 | return ret; |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 2196 | } |
| 2197 | |
Liu Bo | 38c227d | 2013-01-29 03:18:40 +0000 | [diff] [blame] | 2198 | /* snapshot-aware defrag */ |
| 2199 | struct sa_defrag_extent_backref { |
| 2200 | struct rb_node node; |
| 2201 | struct old_sa_defrag_extent *old; |
| 2202 | u64 root_id; |
| 2203 | u64 inum; |
| 2204 | u64 file_pos; |
| 2205 | u64 extent_offset; |
| 2206 | u64 num_bytes; |
| 2207 | u64 generation; |
| 2208 | }; |
| 2209 | |
| 2210 | struct old_sa_defrag_extent { |
| 2211 | struct list_head list; |
| 2212 | struct new_sa_defrag_extent *new; |
| 2213 | |
| 2214 | u64 extent_offset; |
| 2215 | u64 bytenr; |
| 2216 | u64 offset; |
| 2217 | u64 len; |
| 2218 | int count; |
| 2219 | }; |
| 2220 | |
| 2221 | struct new_sa_defrag_extent { |
| 2222 | struct rb_root root; |
| 2223 | struct list_head head; |
| 2224 | struct btrfs_path *path; |
| 2225 | struct inode *inode; |
| 2226 | u64 file_pos; |
| 2227 | u64 len; |
| 2228 | u64 bytenr; |
| 2229 | u64 disk_len; |
| 2230 | u8 compress_type; |
| 2231 | }; |
| 2232 | |
| 2233 | static int backref_comp(struct sa_defrag_extent_backref *b1, |
| 2234 | struct sa_defrag_extent_backref *b2) |
| 2235 | { |
| 2236 | if (b1->root_id < b2->root_id) |
| 2237 | return -1; |
| 2238 | else if (b1->root_id > b2->root_id) |
| 2239 | return 1; |
| 2240 | |
| 2241 | if (b1->inum < b2->inum) |
| 2242 | return -1; |
| 2243 | else if (b1->inum > b2->inum) |
| 2244 | return 1; |
| 2245 | |
| 2246 | if (b1->file_pos < b2->file_pos) |
| 2247 | return -1; |
| 2248 | else if (b1->file_pos > b2->file_pos) |
| 2249 | return 1; |
| 2250 | |
| 2251 | /* |
| 2252 | * [------------------------------] ===> (a range of space) |
| 2253 | * |<--->| |<---->| =============> (fs/file tree A) |
| 2254 | * |<---------------------------->| ===> (fs/file tree B) |
| 2255 | * |
| 2256 | * A range of space can refer to two file extents in one tree while |
| 2257 | * refer to only one file extent in another tree. |
| 2258 | * |
| 2259 | * So we may process a disk offset more than one time(two extents in A) |
| 2260 | * and locate at the same extent(one extent in B), then insert two same |
| 2261 | * backrefs(both refer to the extent in B). |
| 2262 | */ |
| 2263 | return 0; |
| 2264 | } |
| 2265 | |
| 2266 | static void backref_insert(struct rb_root *root, |
| 2267 | struct sa_defrag_extent_backref *backref) |
| 2268 | { |
| 2269 | struct rb_node **p = &root->rb_node; |
| 2270 | struct rb_node *parent = NULL; |
| 2271 | struct sa_defrag_extent_backref *entry; |
| 2272 | int ret; |
| 2273 | |
| 2274 | while (*p) { |
| 2275 | parent = *p; |
| 2276 | entry = rb_entry(parent, struct sa_defrag_extent_backref, node); |
| 2277 | |
| 2278 | ret = backref_comp(backref, entry); |
| 2279 | if (ret < 0) |
| 2280 | p = &(*p)->rb_left; |
| 2281 | else |
| 2282 | p = &(*p)->rb_right; |
| 2283 | } |
| 2284 | |
| 2285 | rb_link_node(&backref->node, parent, p); |
| 2286 | rb_insert_color(&backref->node, root); |
| 2287 | } |
| 2288 | |
| 2289 | /* |
| 2290 | * Note the backref might has changed, and in this case we just return 0. |
| 2291 | */ |
| 2292 | static noinline int record_one_backref(u64 inum, u64 offset, u64 root_id, |
| 2293 | void *ctx) |
| 2294 | { |
| 2295 | struct btrfs_file_extent_item *extent; |
| 2296 | struct btrfs_fs_info *fs_info; |
| 2297 | struct old_sa_defrag_extent *old = ctx; |
| 2298 | struct new_sa_defrag_extent *new = old->new; |
| 2299 | struct btrfs_path *path = new->path; |
| 2300 | struct btrfs_key key; |
| 2301 | struct btrfs_root *root; |
| 2302 | struct sa_defrag_extent_backref *backref; |
| 2303 | struct extent_buffer *leaf; |
| 2304 | struct inode *inode = new->inode; |
| 2305 | int slot; |
| 2306 | int ret; |
| 2307 | u64 extent_offset; |
| 2308 | u64 num_bytes; |
| 2309 | |
| 2310 | if (BTRFS_I(inode)->root->root_key.objectid == root_id && |
| 2311 | inum == btrfs_ino(inode)) |
| 2312 | return 0; |
| 2313 | |
| 2314 | key.objectid = root_id; |
| 2315 | key.type = BTRFS_ROOT_ITEM_KEY; |
| 2316 | key.offset = (u64)-1; |
| 2317 | |
| 2318 | fs_info = BTRFS_I(inode)->root->fs_info; |
| 2319 | root = btrfs_read_fs_root_no_name(fs_info, &key); |
| 2320 | if (IS_ERR(root)) { |
| 2321 | if (PTR_ERR(root) == -ENOENT) |
| 2322 | return 0; |
| 2323 | WARN_ON(1); |
Jeff Mahoney | ab8d0fc | 2016-09-20 10:05:02 -0400 | [diff] [blame] | 2324 | btrfs_debug(fs_info, "inum=%llu, offset=%llu, root_id=%llu", |
Liu Bo | 38c227d | 2013-01-29 03:18:40 +0000 | [diff] [blame] | 2325 | inum, offset, root_id); |
| 2326 | return PTR_ERR(root); |
| 2327 | } |
| 2328 | |
| 2329 | key.objectid = inum; |
| 2330 | key.type = BTRFS_EXTENT_DATA_KEY; |
| 2331 | if (offset > (u64)-1 << 32) |
| 2332 | key.offset = 0; |
| 2333 | else |
| 2334 | key.offset = offset; |
| 2335 | |
| 2336 | ret = btrfs_search_slot(NULL, root, &key, path, 0, 0); |
Dulshani Gunawardhana | fae7f21 | 2013-10-31 10:30:08 +0530 | [diff] [blame] | 2337 | if (WARN_ON(ret < 0)) |
Liu Bo | 38c227d | 2013-01-29 03:18:40 +0000 | [diff] [blame] | 2338 | return ret; |
Josef Bacik | 50f1319 | 2013-07-22 12:50:37 -0400 | [diff] [blame] | 2339 | ret = 0; |
Liu Bo | 38c227d | 2013-01-29 03:18:40 +0000 | [diff] [blame] | 2340 | |
| 2341 | while (1) { |
| 2342 | cond_resched(); |
| 2343 | |
| 2344 | leaf = path->nodes[0]; |
| 2345 | slot = path->slots[0]; |
| 2346 | |
| 2347 | if (slot >= btrfs_header_nritems(leaf)) { |
| 2348 | ret = btrfs_next_leaf(root, path); |
| 2349 | if (ret < 0) { |
| 2350 | goto out; |
| 2351 | } else if (ret > 0) { |
| 2352 | ret = 0; |
| 2353 | goto out; |
| 2354 | } |
| 2355 | continue; |
| 2356 | } |
| 2357 | |
| 2358 | path->slots[0]++; |
| 2359 | |
| 2360 | btrfs_item_key_to_cpu(leaf, &key, slot); |
| 2361 | |
| 2362 | if (key.objectid > inum) |
| 2363 | goto out; |
| 2364 | |
| 2365 | if (key.objectid < inum || key.type != BTRFS_EXTENT_DATA_KEY) |
| 2366 | continue; |
| 2367 | |
| 2368 | extent = btrfs_item_ptr(leaf, slot, |
| 2369 | struct btrfs_file_extent_item); |
| 2370 | |
| 2371 | if (btrfs_file_extent_disk_bytenr(leaf, extent) != old->bytenr) |
| 2372 | continue; |
| 2373 | |
Liu Bo | e68afa4 | 2013-07-01 22:13:26 +0800 | [diff] [blame] | 2374 | /* |
| 2375 | * 'offset' refers to the exact key.offset, |
| 2376 | * NOT the 'offset' field in btrfs_extent_data_ref, ie. |
| 2377 | * (key.offset - extent_offset). |
| 2378 | */ |
| 2379 | if (key.offset != offset) |
Liu Bo | 38c227d | 2013-01-29 03:18:40 +0000 | [diff] [blame] | 2380 | continue; |
| 2381 | |
Liu Bo | e68afa4 | 2013-07-01 22:13:26 +0800 | [diff] [blame] | 2382 | extent_offset = btrfs_file_extent_offset(leaf, extent); |
Liu Bo | 38c227d | 2013-01-29 03:18:40 +0000 | [diff] [blame] | 2383 | num_bytes = btrfs_file_extent_num_bytes(leaf, extent); |
Liu Bo | e68afa4 | 2013-07-01 22:13:26 +0800 | [diff] [blame] | 2384 | |
Liu Bo | 38c227d | 2013-01-29 03:18:40 +0000 | [diff] [blame] | 2385 | if (extent_offset >= old->extent_offset + old->offset + |
| 2386 | old->len || extent_offset + num_bytes <= |
| 2387 | old->extent_offset + old->offset) |
| 2388 | continue; |
Liu Bo | 38c227d | 2013-01-29 03:18:40 +0000 | [diff] [blame] | 2389 | break; |
| 2390 | } |
| 2391 | |
| 2392 | backref = kmalloc(sizeof(*backref), GFP_NOFS); |
| 2393 | if (!backref) { |
| 2394 | ret = -ENOENT; |
| 2395 | goto out; |
| 2396 | } |
| 2397 | |
| 2398 | backref->root_id = root_id; |
| 2399 | backref->inum = inum; |
Liu Bo | e68afa4 | 2013-07-01 22:13:26 +0800 | [diff] [blame] | 2400 | backref->file_pos = offset; |
Liu Bo | 38c227d | 2013-01-29 03:18:40 +0000 | [diff] [blame] | 2401 | backref->num_bytes = num_bytes; |
| 2402 | backref->extent_offset = extent_offset; |
| 2403 | backref->generation = btrfs_file_extent_generation(leaf, extent); |
| 2404 | backref->old = old; |
| 2405 | backref_insert(&new->root, backref); |
| 2406 | old->count++; |
| 2407 | out: |
| 2408 | btrfs_release_path(path); |
| 2409 | WARN_ON(ret); |
| 2410 | return ret; |
| 2411 | } |
| 2412 | |
| 2413 | static noinline bool record_extent_backrefs(struct btrfs_path *path, |
| 2414 | struct new_sa_defrag_extent *new) |
| 2415 | { |
| 2416 | struct btrfs_fs_info *fs_info = BTRFS_I(new->inode)->root->fs_info; |
| 2417 | struct old_sa_defrag_extent *old, *tmp; |
| 2418 | int ret; |
| 2419 | |
| 2420 | new->path = path; |
| 2421 | |
| 2422 | list_for_each_entry_safe(old, tmp, &new->head, list) { |
Liu Bo | e68afa4 | 2013-07-01 22:13:26 +0800 | [diff] [blame] | 2423 | ret = iterate_inodes_from_logical(old->bytenr + |
| 2424 | old->extent_offset, fs_info, |
Liu Bo | 38c227d | 2013-01-29 03:18:40 +0000 | [diff] [blame] | 2425 | path, record_one_backref, |
| 2426 | old); |
Josef Bacik | 4724b10 | 2013-11-05 11:11:40 -0500 | [diff] [blame] | 2427 | if (ret < 0 && ret != -ENOENT) |
| 2428 | return false; |
Liu Bo | 38c227d | 2013-01-29 03:18:40 +0000 | [diff] [blame] | 2429 | |
| 2430 | /* no backref to be processed for this extent */ |
| 2431 | if (!old->count) { |
| 2432 | list_del(&old->list); |
| 2433 | kfree(old); |
| 2434 | } |
| 2435 | } |
| 2436 | |
| 2437 | if (list_empty(&new->head)) |
| 2438 | return false; |
| 2439 | |
| 2440 | return true; |
| 2441 | } |
| 2442 | |
| 2443 | static int relink_is_mergable(struct extent_buffer *leaf, |
| 2444 | struct btrfs_file_extent_item *fi, |
Liu Bo | 116e002 | 2013-08-02 16:30:40 +0800 | [diff] [blame] | 2445 | struct new_sa_defrag_extent *new) |
Liu Bo | 38c227d | 2013-01-29 03:18:40 +0000 | [diff] [blame] | 2446 | { |
Liu Bo | 116e002 | 2013-08-02 16:30:40 +0800 | [diff] [blame] | 2447 | if (btrfs_file_extent_disk_bytenr(leaf, fi) != new->bytenr) |
Liu Bo | 38c227d | 2013-01-29 03:18:40 +0000 | [diff] [blame] | 2448 | return 0; |
| 2449 | |
| 2450 | if (btrfs_file_extent_type(leaf, fi) != BTRFS_FILE_EXTENT_REG) |
| 2451 | return 0; |
| 2452 | |
Liu Bo | 116e002 | 2013-08-02 16:30:40 +0800 | [diff] [blame] | 2453 | if (btrfs_file_extent_compression(leaf, fi) != new->compress_type) |
| 2454 | return 0; |
| 2455 | |
| 2456 | if (btrfs_file_extent_encryption(leaf, fi) || |
Liu Bo | 38c227d | 2013-01-29 03:18:40 +0000 | [diff] [blame] | 2457 | btrfs_file_extent_other_encoding(leaf, fi)) |
| 2458 | return 0; |
| 2459 | |
| 2460 | return 1; |
| 2461 | } |
| 2462 | |
| 2463 | /* |
| 2464 | * Note the backref might has changed, and in this case we just return 0. |
| 2465 | */ |
| 2466 | static noinline int relink_extent_backref(struct btrfs_path *path, |
| 2467 | struct sa_defrag_extent_backref *prev, |
| 2468 | struct sa_defrag_extent_backref *backref) |
| 2469 | { |
| 2470 | struct btrfs_file_extent_item *extent; |
| 2471 | struct btrfs_file_extent_item *item; |
| 2472 | struct btrfs_ordered_extent *ordered; |
| 2473 | struct btrfs_trans_handle *trans; |
| 2474 | struct btrfs_fs_info *fs_info; |
| 2475 | struct btrfs_root *root; |
| 2476 | struct btrfs_key key; |
| 2477 | struct extent_buffer *leaf; |
| 2478 | struct old_sa_defrag_extent *old = backref->old; |
| 2479 | struct new_sa_defrag_extent *new = old->new; |
| 2480 | struct inode *src_inode = new->inode; |
| 2481 | struct inode *inode; |
| 2482 | struct extent_state *cached = NULL; |
| 2483 | int ret = 0; |
| 2484 | u64 start; |
| 2485 | u64 len; |
| 2486 | u64 lock_start; |
| 2487 | u64 lock_end; |
| 2488 | bool merge = false; |
| 2489 | int index; |
| 2490 | |
| 2491 | if (prev && prev->root_id == backref->root_id && |
| 2492 | prev->inum == backref->inum && |
| 2493 | prev->file_pos + prev->num_bytes == backref->file_pos) |
| 2494 | merge = true; |
| 2495 | |
| 2496 | /* step 1: get root */ |
| 2497 | key.objectid = backref->root_id; |
| 2498 | key.type = BTRFS_ROOT_ITEM_KEY; |
| 2499 | key.offset = (u64)-1; |
| 2500 | |
| 2501 | fs_info = BTRFS_I(src_inode)->root->fs_info; |
| 2502 | index = srcu_read_lock(&fs_info->subvol_srcu); |
| 2503 | |
| 2504 | root = btrfs_read_fs_root_no_name(fs_info, &key); |
| 2505 | if (IS_ERR(root)) { |
| 2506 | srcu_read_unlock(&fs_info->subvol_srcu, index); |
| 2507 | if (PTR_ERR(root) == -ENOENT) |
| 2508 | return 0; |
| 2509 | return PTR_ERR(root); |
| 2510 | } |
Liu Bo | 38c227d | 2013-01-29 03:18:40 +0000 | [diff] [blame] | 2511 | |
Wang Shilong | bcbba5e | 2014-02-08 23:46:35 +0800 | [diff] [blame] | 2512 | if (btrfs_root_readonly(root)) { |
| 2513 | srcu_read_unlock(&fs_info->subvol_srcu, index); |
| 2514 | return 0; |
| 2515 | } |
| 2516 | |
Liu Bo | 38c227d | 2013-01-29 03:18:40 +0000 | [diff] [blame] | 2517 | /* step 2: get inode */ |
| 2518 | key.objectid = backref->inum; |
| 2519 | key.type = BTRFS_INODE_ITEM_KEY; |
| 2520 | key.offset = 0; |
| 2521 | |
| 2522 | inode = btrfs_iget(fs_info->sb, &key, root, NULL); |
| 2523 | if (IS_ERR(inode)) { |
| 2524 | srcu_read_unlock(&fs_info->subvol_srcu, index); |
| 2525 | return 0; |
| 2526 | } |
| 2527 | |
| 2528 | srcu_read_unlock(&fs_info->subvol_srcu, index); |
| 2529 | |
| 2530 | /* step 3: relink backref */ |
| 2531 | lock_start = backref->file_pos; |
| 2532 | lock_end = backref->file_pos + backref->num_bytes - 1; |
| 2533 | lock_extent_bits(&BTRFS_I(inode)->io_tree, lock_start, lock_end, |
David Sterba | ff13db4 | 2015-12-03 14:30:40 +0100 | [diff] [blame] | 2534 | &cached); |
Liu Bo | 38c227d | 2013-01-29 03:18:40 +0000 | [diff] [blame] | 2535 | |
| 2536 | ordered = btrfs_lookup_first_ordered_extent(inode, lock_end); |
| 2537 | if (ordered) { |
| 2538 | btrfs_put_ordered_extent(ordered); |
| 2539 | goto out_unlock; |
| 2540 | } |
| 2541 | |
| 2542 | trans = btrfs_join_transaction(root); |
| 2543 | if (IS_ERR(trans)) { |
| 2544 | ret = PTR_ERR(trans); |
| 2545 | goto out_unlock; |
| 2546 | } |
| 2547 | |
| 2548 | key.objectid = backref->inum; |
| 2549 | key.type = BTRFS_EXTENT_DATA_KEY; |
| 2550 | key.offset = backref->file_pos; |
| 2551 | |
| 2552 | ret = btrfs_search_slot(NULL, root, &key, path, 0, 0); |
| 2553 | if (ret < 0) { |
| 2554 | goto out_free_path; |
| 2555 | } else if (ret > 0) { |
| 2556 | ret = 0; |
| 2557 | goto out_free_path; |
| 2558 | } |
| 2559 | |
| 2560 | extent = btrfs_item_ptr(path->nodes[0], path->slots[0], |
| 2561 | struct btrfs_file_extent_item); |
| 2562 | |
| 2563 | if (btrfs_file_extent_generation(path->nodes[0], extent) != |
| 2564 | backref->generation) |
| 2565 | goto out_free_path; |
| 2566 | |
| 2567 | btrfs_release_path(path); |
| 2568 | |
| 2569 | start = backref->file_pos; |
| 2570 | if (backref->extent_offset < old->extent_offset + old->offset) |
| 2571 | start += old->extent_offset + old->offset - |
| 2572 | backref->extent_offset; |
| 2573 | |
| 2574 | len = min(backref->extent_offset + backref->num_bytes, |
| 2575 | old->extent_offset + old->offset + old->len); |
| 2576 | len -= max(backref->extent_offset, old->extent_offset + old->offset); |
| 2577 | |
| 2578 | ret = btrfs_drop_extents(trans, root, inode, start, |
| 2579 | start + len, 1); |
| 2580 | if (ret) |
| 2581 | goto out_free_path; |
| 2582 | again: |
| 2583 | key.objectid = btrfs_ino(inode); |
| 2584 | key.type = BTRFS_EXTENT_DATA_KEY; |
| 2585 | key.offset = start; |
| 2586 | |
Liu Bo | a09a0a7 | 2013-03-11 09:20:58 +0000 | [diff] [blame] | 2587 | path->leave_spinning = 1; |
Liu Bo | 38c227d | 2013-01-29 03:18:40 +0000 | [diff] [blame] | 2588 | if (merge) { |
| 2589 | struct btrfs_file_extent_item *fi; |
| 2590 | u64 extent_len; |
| 2591 | struct btrfs_key found_key; |
| 2592 | |
Gui Hecheng | 3c9665d | 2014-01-23 13:41:09 +0800 | [diff] [blame] | 2593 | ret = btrfs_search_slot(trans, root, &key, path, 0, 1); |
Liu Bo | 38c227d | 2013-01-29 03:18:40 +0000 | [diff] [blame] | 2594 | if (ret < 0) |
| 2595 | goto out_free_path; |
| 2596 | |
| 2597 | path->slots[0]--; |
| 2598 | leaf = path->nodes[0]; |
| 2599 | btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]); |
| 2600 | |
| 2601 | fi = btrfs_item_ptr(leaf, path->slots[0], |
| 2602 | struct btrfs_file_extent_item); |
| 2603 | extent_len = btrfs_file_extent_num_bytes(leaf, fi); |
| 2604 | |
Liu Bo | 116e002 | 2013-08-02 16:30:40 +0800 | [diff] [blame] | 2605 | if (extent_len + found_key.offset == start && |
| 2606 | relink_is_mergable(leaf, fi, new)) { |
Liu Bo | 38c227d | 2013-01-29 03:18:40 +0000 | [diff] [blame] | 2607 | btrfs_set_file_extent_num_bytes(leaf, fi, |
| 2608 | extent_len + len); |
| 2609 | btrfs_mark_buffer_dirty(leaf); |
| 2610 | inode_add_bytes(inode, len); |
| 2611 | |
| 2612 | ret = 1; |
| 2613 | goto out_free_path; |
| 2614 | } else { |
| 2615 | merge = false; |
| 2616 | btrfs_release_path(path); |
| 2617 | goto again; |
| 2618 | } |
| 2619 | } |
| 2620 | |
| 2621 | ret = btrfs_insert_empty_item(trans, root, path, &key, |
| 2622 | sizeof(*extent)); |
| 2623 | if (ret) { |
Jeff Mahoney | 6664283 | 2016-06-10 18:19:25 -0400 | [diff] [blame] | 2624 | btrfs_abort_transaction(trans, ret); |
Liu Bo | 38c227d | 2013-01-29 03:18:40 +0000 | [diff] [blame] | 2625 | goto out_free_path; |
| 2626 | } |
| 2627 | |
| 2628 | leaf = path->nodes[0]; |
| 2629 | item = btrfs_item_ptr(leaf, path->slots[0], |
| 2630 | struct btrfs_file_extent_item); |
| 2631 | btrfs_set_file_extent_disk_bytenr(leaf, item, new->bytenr); |
| 2632 | btrfs_set_file_extent_disk_num_bytes(leaf, item, new->disk_len); |
| 2633 | btrfs_set_file_extent_offset(leaf, item, start - new->file_pos); |
| 2634 | btrfs_set_file_extent_num_bytes(leaf, item, len); |
| 2635 | btrfs_set_file_extent_ram_bytes(leaf, item, new->len); |
| 2636 | btrfs_set_file_extent_generation(leaf, item, trans->transid); |
| 2637 | btrfs_set_file_extent_type(leaf, item, BTRFS_FILE_EXTENT_REG); |
| 2638 | btrfs_set_file_extent_compression(leaf, item, new->compress_type); |
| 2639 | btrfs_set_file_extent_encryption(leaf, item, 0); |
| 2640 | btrfs_set_file_extent_other_encoding(leaf, item, 0); |
| 2641 | |
| 2642 | btrfs_mark_buffer_dirty(leaf); |
| 2643 | inode_add_bytes(inode, len); |
Liu Bo | a09a0a7 | 2013-03-11 09:20:58 +0000 | [diff] [blame] | 2644 | btrfs_release_path(path); |
Liu Bo | 38c227d | 2013-01-29 03:18:40 +0000 | [diff] [blame] | 2645 | |
| 2646 | ret = btrfs_inc_extent_ref(trans, root, new->bytenr, |
| 2647 | new->disk_len, 0, |
| 2648 | backref->root_id, backref->inum, |
Filipe Manana | b06c4bf | 2015-10-23 07:52:54 +0100 | [diff] [blame] | 2649 | new->file_pos); /* start - extent_offset */ |
Liu Bo | 38c227d | 2013-01-29 03:18:40 +0000 | [diff] [blame] | 2650 | if (ret) { |
Jeff Mahoney | 6664283 | 2016-06-10 18:19:25 -0400 | [diff] [blame] | 2651 | btrfs_abort_transaction(trans, ret); |
Liu Bo | 38c227d | 2013-01-29 03:18:40 +0000 | [diff] [blame] | 2652 | goto out_free_path; |
| 2653 | } |
| 2654 | |
| 2655 | ret = 1; |
| 2656 | out_free_path: |
| 2657 | btrfs_release_path(path); |
Liu Bo | a09a0a7 | 2013-03-11 09:20:58 +0000 | [diff] [blame] | 2658 | path->leave_spinning = 0; |
Liu Bo | 38c227d | 2013-01-29 03:18:40 +0000 | [diff] [blame] | 2659 | btrfs_end_transaction(trans, root); |
| 2660 | out_unlock: |
| 2661 | unlock_extent_cached(&BTRFS_I(inode)->io_tree, lock_start, lock_end, |
| 2662 | &cached, GFP_NOFS); |
| 2663 | iput(inode); |
| 2664 | return ret; |
| 2665 | } |
| 2666 | |
Liu Bo | 6f51956 | 2013-10-29 10:45:05 +0800 | [diff] [blame] | 2667 | static void free_sa_defrag_extent(struct new_sa_defrag_extent *new) |
| 2668 | { |
| 2669 | struct old_sa_defrag_extent *old, *tmp; |
| 2670 | |
| 2671 | if (!new) |
| 2672 | return; |
| 2673 | |
| 2674 | list_for_each_entry_safe(old, tmp, &new->head, list) { |
Liu Bo | 6f51956 | 2013-10-29 10:45:05 +0800 | [diff] [blame] | 2675 | kfree(old); |
| 2676 | } |
| 2677 | kfree(new); |
| 2678 | } |
| 2679 | |
Liu Bo | 38c227d | 2013-01-29 03:18:40 +0000 | [diff] [blame] | 2680 | static void relink_file_extents(struct new_sa_defrag_extent *new) |
| 2681 | { |
| 2682 | struct btrfs_path *path; |
Liu Bo | 38c227d | 2013-01-29 03:18:40 +0000 | [diff] [blame] | 2683 | struct sa_defrag_extent_backref *backref; |
| 2684 | struct sa_defrag_extent_backref *prev = NULL; |
| 2685 | struct inode *inode; |
| 2686 | struct btrfs_root *root; |
| 2687 | struct rb_node *node; |
| 2688 | int ret; |
| 2689 | |
| 2690 | inode = new->inode; |
| 2691 | root = BTRFS_I(inode)->root; |
| 2692 | |
| 2693 | path = btrfs_alloc_path(); |
| 2694 | if (!path) |
| 2695 | return; |
| 2696 | |
| 2697 | if (!record_extent_backrefs(path, new)) { |
| 2698 | btrfs_free_path(path); |
| 2699 | goto out; |
| 2700 | } |
| 2701 | btrfs_release_path(path); |
| 2702 | |
| 2703 | while (1) { |
| 2704 | node = rb_first(&new->root); |
| 2705 | if (!node) |
| 2706 | break; |
| 2707 | rb_erase(node, &new->root); |
| 2708 | |
| 2709 | backref = rb_entry(node, struct sa_defrag_extent_backref, node); |
| 2710 | |
| 2711 | ret = relink_extent_backref(path, prev, backref); |
| 2712 | WARN_ON(ret < 0); |
| 2713 | |
| 2714 | kfree(prev); |
| 2715 | |
| 2716 | if (ret == 1) |
| 2717 | prev = backref; |
| 2718 | else |
| 2719 | prev = NULL; |
| 2720 | cond_resched(); |
| 2721 | } |
| 2722 | kfree(prev); |
| 2723 | |
| 2724 | btrfs_free_path(path); |
Liu Bo | 38c227d | 2013-01-29 03:18:40 +0000 | [diff] [blame] | 2725 | out: |
Liu Bo | 6f51956 | 2013-10-29 10:45:05 +0800 | [diff] [blame] | 2726 | free_sa_defrag_extent(new); |
| 2727 | |
Liu Bo | 38c227d | 2013-01-29 03:18:40 +0000 | [diff] [blame] | 2728 | atomic_dec(&root->fs_info->defrag_running); |
| 2729 | wake_up(&root->fs_info->transaction_wait); |
Liu Bo | 38c227d | 2013-01-29 03:18:40 +0000 | [diff] [blame] | 2730 | } |
| 2731 | |
| 2732 | static struct new_sa_defrag_extent * |
| 2733 | record_old_file_extents(struct inode *inode, |
| 2734 | struct btrfs_ordered_extent *ordered) |
| 2735 | { |
| 2736 | struct btrfs_root *root = BTRFS_I(inode)->root; |
| 2737 | struct btrfs_path *path; |
| 2738 | struct btrfs_key key; |
Liu Bo | 6f51956 | 2013-10-29 10:45:05 +0800 | [diff] [blame] | 2739 | struct old_sa_defrag_extent *old; |
Liu Bo | 38c227d | 2013-01-29 03:18:40 +0000 | [diff] [blame] | 2740 | struct new_sa_defrag_extent *new; |
| 2741 | int ret; |
| 2742 | |
| 2743 | new = kmalloc(sizeof(*new), GFP_NOFS); |
| 2744 | if (!new) |
| 2745 | return NULL; |
| 2746 | |
| 2747 | new->inode = inode; |
| 2748 | new->file_pos = ordered->file_offset; |
| 2749 | new->len = ordered->len; |
| 2750 | new->bytenr = ordered->start; |
| 2751 | new->disk_len = ordered->disk_len; |
| 2752 | new->compress_type = ordered->compress_type; |
| 2753 | new->root = RB_ROOT; |
| 2754 | INIT_LIST_HEAD(&new->head); |
| 2755 | |
| 2756 | path = btrfs_alloc_path(); |
| 2757 | if (!path) |
| 2758 | goto out_kfree; |
| 2759 | |
| 2760 | key.objectid = btrfs_ino(inode); |
| 2761 | key.type = BTRFS_EXTENT_DATA_KEY; |
| 2762 | key.offset = new->file_pos; |
| 2763 | |
| 2764 | ret = btrfs_search_slot(NULL, root, &key, path, 0, 0); |
| 2765 | if (ret < 0) |
| 2766 | goto out_free_path; |
| 2767 | if (ret > 0 && path->slots[0] > 0) |
| 2768 | path->slots[0]--; |
| 2769 | |
| 2770 | /* find out all the old extents for the file range */ |
| 2771 | while (1) { |
| 2772 | struct btrfs_file_extent_item *extent; |
| 2773 | struct extent_buffer *l; |
| 2774 | int slot; |
| 2775 | u64 num_bytes; |
| 2776 | u64 offset; |
| 2777 | u64 end; |
| 2778 | u64 disk_bytenr; |
| 2779 | u64 extent_offset; |
| 2780 | |
| 2781 | l = path->nodes[0]; |
| 2782 | slot = path->slots[0]; |
| 2783 | |
| 2784 | if (slot >= btrfs_header_nritems(l)) { |
| 2785 | ret = btrfs_next_leaf(root, path); |
| 2786 | if (ret < 0) |
Liu Bo | 6f51956 | 2013-10-29 10:45:05 +0800 | [diff] [blame] | 2787 | goto out_free_path; |
Liu Bo | 38c227d | 2013-01-29 03:18:40 +0000 | [diff] [blame] | 2788 | else if (ret > 0) |
| 2789 | break; |
| 2790 | continue; |
| 2791 | } |
| 2792 | |
| 2793 | btrfs_item_key_to_cpu(l, &key, slot); |
| 2794 | |
| 2795 | if (key.objectid != btrfs_ino(inode)) |
| 2796 | break; |
| 2797 | if (key.type != BTRFS_EXTENT_DATA_KEY) |
| 2798 | break; |
| 2799 | if (key.offset >= new->file_pos + new->len) |
| 2800 | break; |
| 2801 | |
| 2802 | extent = btrfs_item_ptr(l, slot, struct btrfs_file_extent_item); |
| 2803 | |
| 2804 | num_bytes = btrfs_file_extent_num_bytes(l, extent); |
| 2805 | if (key.offset + num_bytes < new->file_pos) |
| 2806 | goto next; |
| 2807 | |
| 2808 | disk_bytenr = btrfs_file_extent_disk_bytenr(l, extent); |
| 2809 | if (!disk_bytenr) |
| 2810 | goto next; |
| 2811 | |
| 2812 | extent_offset = btrfs_file_extent_offset(l, extent); |
| 2813 | |
| 2814 | old = kmalloc(sizeof(*old), GFP_NOFS); |
| 2815 | if (!old) |
Liu Bo | 6f51956 | 2013-10-29 10:45:05 +0800 | [diff] [blame] | 2816 | goto out_free_path; |
Liu Bo | 38c227d | 2013-01-29 03:18:40 +0000 | [diff] [blame] | 2817 | |
| 2818 | offset = max(new->file_pos, key.offset); |
| 2819 | end = min(new->file_pos + new->len, key.offset + num_bytes); |
| 2820 | |
| 2821 | old->bytenr = disk_bytenr; |
| 2822 | old->extent_offset = extent_offset; |
| 2823 | old->offset = offset - key.offset; |
| 2824 | old->len = end - offset; |
| 2825 | old->new = new; |
| 2826 | old->count = 0; |
| 2827 | list_add_tail(&old->list, &new->head); |
| 2828 | next: |
| 2829 | path->slots[0]++; |
| 2830 | cond_resched(); |
| 2831 | } |
| 2832 | |
| 2833 | btrfs_free_path(path); |
| 2834 | atomic_inc(&root->fs_info->defrag_running); |
| 2835 | |
| 2836 | return new; |
| 2837 | |
Liu Bo | 38c227d | 2013-01-29 03:18:40 +0000 | [diff] [blame] | 2838 | out_free_path: |
| 2839 | btrfs_free_path(path); |
| 2840 | out_kfree: |
Liu Bo | 6f51956 | 2013-10-29 10:45:05 +0800 | [diff] [blame] | 2841 | free_sa_defrag_extent(new); |
Liu Bo | 38c227d | 2013-01-29 03:18:40 +0000 | [diff] [blame] | 2842 | return NULL; |
| 2843 | } |
| 2844 | |
Miao Xie | e570fd2 | 2014-06-19 10:42:50 +0800 | [diff] [blame] | 2845 | static void btrfs_release_delalloc_bytes(struct btrfs_root *root, |
| 2846 | u64 start, u64 len) |
| 2847 | { |
| 2848 | struct btrfs_block_group_cache *cache; |
| 2849 | |
| 2850 | cache = btrfs_lookup_block_group(root->fs_info, start); |
| 2851 | ASSERT(cache); |
| 2852 | |
| 2853 | spin_lock(&cache->lock); |
| 2854 | cache->delalloc_bytes -= len; |
| 2855 | spin_unlock(&cache->lock); |
| 2856 | |
| 2857 | btrfs_put_block_group(cache); |
| 2858 | } |
| 2859 | |
Chris Mason | d352ac6 | 2008-09-29 15:18:18 -0400 | [diff] [blame] | 2860 | /* as ordered data IO finishes, this gets called so we can finish |
| 2861 | * an ordered extent if the range of bytes in the file it covers are |
| 2862 | * fully written. |
| 2863 | */ |
Josef Bacik | 5fd0204 | 2012-05-02 14:00:54 -0400 | [diff] [blame] | 2864 | static int btrfs_finish_ordered_io(struct btrfs_ordered_extent *ordered_extent) |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 2865 | { |
Josef Bacik | 5fd0204 | 2012-05-02 14:00:54 -0400 | [diff] [blame] | 2866 | struct inode *inode = ordered_extent->inode; |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 2867 | struct btrfs_root *root = BTRFS_I(inode)->root; |
Yan, Zheng | 0ca1f7c | 2010-05-16 10:48:47 -0400 | [diff] [blame] | 2868 | struct btrfs_trans_handle *trans = NULL; |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 2869 | struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree; |
Josef Bacik | 2ac55d4 | 2010-02-03 19:33:23 +0000 | [diff] [blame] | 2870 | struct extent_state *cached_state = NULL; |
Liu Bo | 38c227d | 2013-01-29 03:18:40 +0000 | [diff] [blame] | 2871 | struct new_sa_defrag_extent *new = NULL; |
Li Zefan | 261507a0 | 2010-12-17 14:21:50 +0800 | [diff] [blame] | 2872 | int compress_type = 0; |
Josef Bacik | 77cef2e | 2013-08-29 13:57:21 -0400 | [diff] [blame] | 2873 | int ret = 0; |
| 2874 | u64 logical_len = ordered_extent->len; |
Li Zefan | 82d5902 | 2011-04-20 10:33:24 +0800 | [diff] [blame] | 2875 | bool nolock; |
Josef Bacik | 77cef2e | 2013-08-29 13:57:21 -0400 | [diff] [blame] | 2876 | bool truncated = false; |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 2877 | |
Liu Bo | 83eea1f | 2012-07-10 05:28:39 -0600 | [diff] [blame] | 2878 | nolock = btrfs_is_free_space_inode(inode); |
Josef Bacik | 0cb59c9 | 2010-07-02 12:14:14 -0400 | [diff] [blame] | 2879 | |
Josef Bacik | 5fd0204 | 2012-05-02 14:00:54 -0400 | [diff] [blame] | 2880 | if (test_bit(BTRFS_ORDERED_IOERR, &ordered_extent->flags)) { |
| 2881 | ret = -EIO; |
| 2882 | goto out; |
| 2883 | } |
| 2884 | |
Miao Xie | f612496 | 2014-09-12 18:44:04 +0800 | [diff] [blame] | 2885 | btrfs_free_io_failure_record(inode, ordered_extent->file_offset, |
| 2886 | ordered_extent->file_offset + |
| 2887 | ordered_extent->len - 1); |
| 2888 | |
Josef Bacik | 77cef2e | 2013-08-29 13:57:21 -0400 | [diff] [blame] | 2889 | if (test_bit(BTRFS_ORDERED_TRUNCATED, &ordered_extent->flags)) { |
| 2890 | truncated = true; |
| 2891 | logical_len = ordered_extent->truncated_len; |
| 2892 | /* Truncated the entire extent, don't bother adding */ |
| 2893 | if (!logical_len) |
| 2894 | goto out; |
| 2895 | } |
| 2896 | |
Yan, Zheng | c216775 | 2009-11-12 09:34:21 +0000 | [diff] [blame] | 2897 | if (test_bit(BTRFS_ORDERED_NOCOW, &ordered_extent->flags)) { |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 2898 | BUG_ON(!list_empty(&ordered_extent->list)); /* Logic error */ |
Qu Wenruo | 94ed938 | 2015-09-08 17:25:56 +0800 | [diff] [blame] | 2899 | |
| 2900 | /* |
| 2901 | * For mwrite(mmap + memset to write) case, we still reserve |
| 2902 | * space for NOCOW range. |
| 2903 | * As NOCOW won't cause a new delayed ref, just free the space |
| 2904 | */ |
| 2905 | btrfs_qgroup_free_data(inode, ordered_extent->file_offset, |
| 2906 | ordered_extent->len); |
Josef Bacik | 6c760c0 | 2012-11-09 10:53:21 -0500 | [diff] [blame] | 2907 | btrfs_ordered_update_i_size(inode, 0, ordered_extent); |
| 2908 | if (nolock) |
| 2909 | trans = btrfs_join_transaction_nolock(root); |
| 2910 | else |
| 2911 | trans = btrfs_join_transaction(root); |
| 2912 | if (IS_ERR(trans)) { |
| 2913 | ret = PTR_ERR(trans); |
| 2914 | trans = NULL; |
| 2915 | goto out; |
Yan, Zheng | c216775 | 2009-11-12 09:34:21 +0000 | [diff] [blame] | 2916 | } |
Josef Bacik | 6c760c0 | 2012-11-09 10:53:21 -0500 | [diff] [blame] | 2917 | trans->block_rsv = &root->fs_info->delalloc_block_rsv; |
| 2918 | ret = btrfs_update_inode_fallback(trans, root, inode); |
| 2919 | if (ret) /* -ENOMEM or corruption */ |
Jeff Mahoney | 6664283 | 2016-06-10 18:19:25 -0400 | [diff] [blame] | 2920 | btrfs_abort_transaction(trans, ret); |
Yan, Zheng | c216775 | 2009-11-12 09:34:21 +0000 | [diff] [blame] | 2921 | goto out; |
| 2922 | } |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 2923 | |
Josef Bacik | 2ac55d4 | 2010-02-03 19:33:23 +0000 | [diff] [blame] | 2924 | lock_extent_bits(io_tree, ordered_extent->file_offset, |
| 2925 | ordered_extent->file_offset + ordered_extent->len - 1, |
David Sterba | ff13db4 | 2015-12-03 14:30:40 +0100 | [diff] [blame] | 2926 | &cached_state); |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 2927 | |
Liu Bo | 38c227d | 2013-01-29 03:18:40 +0000 | [diff] [blame] | 2928 | ret = test_range_bit(io_tree, ordered_extent->file_offset, |
| 2929 | ordered_extent->file_offset + ordered_extent->len - 1, |
| 2930 | EXTENT_DEFRAG, 1, cached_state); |
| 2931 | if (ret) { |
| 2932 | u64 last_snapshot = btrfs_root_last_snapshot(&root->root_item); |
Josef Bacik | 8101c8d | 2014-01-29 16:05:30 -0500 | [diff] [blame] | 2933 | if (0 && last_snapshot >= BTRFS_I(inode)->generation) |
Liu Bo | 38c227d | 2013-01-29 03:18:40 +0000 | [diff] [blame] | 2934 | /* the inode is shared */ |
| 2935 | new = record_old_file_extents(inode, ordered_extent); |
| 2936 | |
| 2937 | clear_extent_bit(io_tree, ordered_extent->file_offset, |
| 2938 | ordered_extent->file_offset + ordered_extent->len - 1, |
| 2939 | EXTENT_DEFRAG, 0, 0, &cached_state, GFP_NOFS); |
| 2940 | } |
| 2941 | |
Josef Bacik | 0cb59c9 | 2010-07-02 12:14:14 -0400 | [diff] [blame] | 2942 | if (nolock) |
Josef Bacik | 7a7eaa4 | 2011-04-13 12:54:33 -0400 | [diff] [blame] | 2943 | trans = btrfs_join_transaction_nolock(root); |
Josef Bacik | 0cb59c9 | 2010-07-02 12:14:14 -0400 | [diff] [blame] | 2944 | else |
Josef Bacik | 7a7eaa4 | 2011-04-13 12:54:33 -0400 | [diff] [blame] | 2945 | trans = btrfs_join_transaction(root); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 2946 | if (IS_ERR(trans)) { |
| 2947 | ret = PTR_ERR(trans); |
| 2948 | trans = NULL; |
| 2949 | goto out_unlock; |
| 2950 | } |
Chris Mason | a79b7d4 | 2014-05-22 16:18:52 -0700 | [diff] [blame] | 2951 | |
Yan, Zheng | 0ca1f7c | 2010-05-16 10:48:47 -0400 | [diff] [blame] | 2952 | trans->block_rsv = &root->fs_info->delalloc_block_rsv; |
Yan, Zheng | c216775 | 2009-11-12 09:34:21 +0000 | [diff] [blame] | 2953 | |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 2954 | if (test_bit(BTRFS_ORDERED_COMPRESSED, &ordered_extent->flags)) |
Li Zefan | 261507a0 | 2010-12-17 14:21:50 +0800 | [diff] [blame] | 2955 | compress_type = ordered_extent->compress_type; |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 2956 | if (test_bit(BTRFS_ORDERED_PREALLOC, &ordered_extent->flags)) { |
Li Zefan | 261507a0 | 2010-12-17 14:21:50 +0800 | [diff] [blame] | 2957 | BUG_ON(compress_type); |
Yan, Zheng | 920bbbf | 2009-11-12 09:34:08 +0000 | [diff] [blame] | 2958 | ret = btrfs_mark_extent_written(trans, inode, |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 2959 | ordered_extent->file_offset, |
| 2960 | ordered_extent->file_offset + |
Josef Bacik | 77cef2e | 2013-08-29 13:57:21 -0400 | [diff] [blame] | 2961 | logical_len); |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 2962 | } else { |
Josef Bacik | 0af3d00 | 2010-06-21 14:48:16 -0400 | [diff] [blame] | 2963 | BUG_ON(root == root->fs_info->tree_root); |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 2964 | ret = insert_reserved_file_extent(trans, inode, |
| 2965 | ordered_extent->file_offset, |
| 2966 | ordered_extent->start, |
| 2967 | ordered_extent->disk_len, |
Josef Bacik | 77cef2e | 2013-08-29 13:57:21 -0400 | [diff] [blame] | 2968 | logical_len, logical_len, |
Li Zefan | 261507a0 | 2010-12-17 14:21:50 +0800 | [diff] [blame] | 2969 | compress_type, 0, 0, |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 2970 | BTRFS_FILE_EXTENT_REG); |
Miao Xie | e570fd2 | 2014-06-19 10:42:50 +0800 | [diff] [blame] | 2971 | if (!ret) |
| 2972 | btrfs_release_delalloc_bytes(root, |
| 2973 | ordered_extent->start, |
| 2974 | ordered_extent->disk_len); |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 2975 | } |
Josef Bacik | 5dc562c | 2012-08-17 13:14:17 -0400 | [diff] [blame] | 2976 | unpin_extent_cache(&BTRFS_I(inode)->extent_tree, |
| 2977 | ordered_extent->file_offset, ordered_extent->len, |
| 2978 | trans->transid); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 2979 | if (ret < 0) { |
Jeff Mahoney | 6664283 | 2016-06-10 18:19:25 -0400 | [diff] [blame] | 2980 | btrfs_abort_transaction(trans, ret); |
Josef Bacik | 5fd0204 | 2012-05-02 14:00:54 -0400 | [diff] [blame] | 2981 | goto out_unlock; |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 2982 | } |
Josef Bacik | 2ac55d4 | 2010-02-03 19:33:23 +0000 | [diff] [blame] | 2983 | |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 2984 | add_pending_csums(trans, inode, ordered_extent->file_offset, |
| 2985 | &ordered_extent->list); |
| 2986 | |
Josef Bacik | 6c760c0 | 2012-11-09 10:53:21 -0500 | [diff] [blame] | 2987 | btrfs_ordered_update_i_size(inode, 0, ordered_extent); |
| 2988 | ret = btrfs_update_inode_fallback(trans, root, inode); |
| 2989 | if (ret) { /* -ENOMEM or corruption */ |
Jeff Mahoney | 6664283 | 2016-06-10 18:19:25 -0400 | [diff] [blame] | 2990 | btrfs_abort_transaction(trans, ret); |
Josef Bacik | 6c760c0 | 2012-11-09 10:53:21 -0500 | [diff] [blame] | 2991 | goto out_unlock; |
Josef Bacik | 1ef30be | 2011-04-05 19:25:36 -0400 | [diff] [blame] | 2992 | } |
| 2993 | ret = 0; |
Josef Bacik | 5fd0204 | 2012-05-02 14:00:54 -0400 | [diff] [blame] | 2994 | out_unlock: |
| 2995 | unlock_extent_cached(io_tree, ordered_extent->file_offset, |
| 2996 | ordered_extent->file_offset + |
| 2997 | ordered_extent->len - 1, &cached_state, GFP_NOFS); |
Yan, Zheng | c216775 | 2009-11-12 09:34:21 +0000 | [diff] [blame] | 2998 | out: |
Josef Bacik | 5b0e95b | 2011-10-06 08:58:24 -0400 | [diff] [blame] | 2999 | if (root != root->fs_info->tree_root) |
Josef Bacik | 0cb59c9 | 2010-07-02 12:14:14 -0400 | [diff] [blame] | 3000 | btrfs_delalloc_release_metadata(inode, ordered_extent->len); |
Miao Xie | a698d075 | 2012-09-20 01:51:59 -0600 | [diff] [blame] | 3001 | if (trans) |
| 3002 | btrfs_end_transaction(trans, root); |
Josef Bacik | 0cb59c9 | 2010-07-02 12:14:14 -0400 | [diff] [blame] | 3003 | |
Josef Bacik | 77cef2e | 2013-08-29 13:57:21 -0400 | [diff] [blame] | 3004 | if (ret || truncated) { |
| 3005 | u64 start, end; |
| 3006 | |
| 3007 | if (truncated) |
| 3008 | start = ordered_extent->file_offset + logical_len; |
| 3009 | else |
| 3010 | start = ordered_extent->file_offset; |
| 3011 | end = ordered_extent->file_offset + ordered_extent->len - 1; |
| 3012 | clear_extent_uptodate(io_tree, start, end, NULL, GFP_NOFS); |
| 3013 | |
| 3014 | /* Drop the cache for the part of the extent we didn't write. */ |
| 3015 | btrfs_drop_extent_cache(inode, start, end, 0); |
Josef Bacik | 5fd0204 | 2012-05-02 14:00:54 -0400 | [diff] [blame] | 3016 | |
Josef Bacik | 0bec9ef | 2013-01-31 14:58:00 -0500 | [diff] [blame] | 3017 | /* |
| 3018 | * If the ordered extent had an IOERR or something else went |
| 3019 | * wrong we need to return the space for this ordered extent |
Josef Bacik | 77cef2e | 2013-08-29 13:57:21 -0400 | [diff] [blame] | 3020 | * back to the allocator. We only free the extent in the |
| 3021 | * truncated case if we didn't write out the extent at all. |
Josef Bacik | 0bec9ef | 2013-01-31 14:58:00 -0500 | [diff] [blame] | 3022 | */ |
Josef Bacik | 77cef2e | 2013-08-29 13:57:21 -0400 | [diff] [blame] | 3023 | if ((ret || !logical_len) && |
| 3024 | !test_bit(BTRFS_ORDERED_NOCOW, &ordered_extent->flags) && |
Josef Bacik | 0bec9ef | 2013-01-31 14:58:00 -0500 | [diff] [blame] | 3025 | !test_bit(BTRFS_ORDERED_PREALLOC, &ordered_extent->flags)) |
| 3026 | btrfs_free_reserved_extent(root, ordered_extent->start, |
Miao Xie | e570fd2 | 2014-06-19 10:42:50 +0800 | [diff] [blame] | 3027 | ordered_extent->disk_len, 1); |
Josef Bacik | 0bec9ef | 2013-01-31 14:58:00 -0500 | [diff] [blame] | 3028 | } |
| 3029 | |
| 3030 | |
Josef Bacik | 5fd0204 | 2012-05-02 14:00:54 -0400 | [diff] [blame] | 3031 | /* |
Liu Bo | 8bad3c0 | 2012-06-18 12:14:23 +0800 | [diff] [blame] | 3032 | * This needs to be done to make sure anybody waiting knows we are done |
| 3033 | * updating everything for this ordered extent. |
Josef Bacik | 5fd0204 | 2012-05-02 14:00:54 -0400 | [diff] [blame] | 3034 | */ |
| 3035 | btrfs_remove_ordered_extent(inode, ordered_extent); |
| 3036 | |
Liu Bo | 38c227d | 2013-01-29 03:18:40 +0000 | [diff] [blame] | 3037 | /* for snapshot-aware defrag */ |
Liu Bo | 6f51956 | 2013-10-29 10:45:05 +0800 | [diff] [blame] | 3038 | if (new) { |
| 3039 | if (ret) { |
| 3040 | free_sa_defrag_extent(new); |
| 3041 | atomic_dec(&root->fs_info->defrag_running); |
| 3042 | } else { |
| 3043 | relink_file_extents(new); |
| 3044 | } |
| 3045 | } |
Liu Bo | 38c227d | 2013-01-29 03:18:40 +0000 | [diff] [blame] | 3046 | |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 3047 | /* once for us */ |
| 3048 | btrfs_put_ordered_extent(ordered_extent); |
| 3049 | /* once for the tree */ |
| 3050 | btrfs_put_ordered_extent(ordered_extent); |
| 3051 | |
Josef Bacik | 5fd0204 | 2012-05-02 14:00:54 -0400 | [diff] [blame] | 3052 | return ret; |
| 3053 | } |
| 3054 | |
| 3055 | static void finish_ordered_fn(struct btrfs_work *work) |
| 3056 | { |
| 3057 | struct btrfs_ordered_extent *ordered_extent; |
| 3058 | ordered_extent = container_of(work, struct btrfs_ordered_extent, work); |
| 3059 | btrfs_finish_ordered_io(ordered_extent); |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 3060 | } |
| 3061 | |
Christoph Hellwig | b295086 | 2008-12-02 09:54:17 -0500 | [diff] [blame] | 3062 | static int btrfs_writepage_end_io_hook(struct page *page, u64 start, u64 end, |
Chris Mason | 211f90e | 2008-07-18 11:56:15 -0400 | [diff] [blame] | 3063 | struct extent_state *state, int uptodate) |
| 3064 | { |
Josef Bacik | 5fd0204 | 2012-05-02 14:00:54 -0400 | [diff] [blame] | 3065 | struct inode *inode = page->mapping->host; |
| 3066 | struct btrfs_root *root = BTRFS_I(inode)->root; |
| 3067 | struct btrfs_ordered_extent *ordered_extent = NULL; |
Liu Bo | 9e0af23 | 2014-08-15 23:36:53 +0800 | [diff] [blame] | 3068 | struct btrfs_workqueue *wq; |
| 3069 | btrfs_work_func_t func; |
Josef Bacik | 5fd0204 | 2012-05-02 14:00:54 -0400 | [diff] [blame] | 3070 | |
liubo | 1abe9b8 | 2011-03-24 11:18:59 +0000 | [diff] [blame] | 3071 | trace_btrfs_writepage_end_io_hook(page, start, end, uptodate); |
| 3072 | |
Chris Mason | 8b62b72 | 2009-09-02 16:53:46 -0400 | [diff] [blame] | 3073 | ClearPagePrivate2(page); |
Josef Bacik | 5fd0204 | 2012-05-02 14:00:54 -0400 | [diff] [blame] | 3074 | if (!btrfs_dec_test_ordered_pending(inode, &ordered_extent, start, |
| 3075 | end - start + 1, uptodate)) |
| 3076 | return 0; |
| 3077 | |
Liu Bo | 9e0af23 | 2014-08-15 23:36:53 +0800 | [diff] [blame] | 3078 | if (btrfs_is_free_space_inode(inode)) { |
| 3079 | wq = root->fs_info->endio_freespace_worker; |
| 3080 | func = btrfs_freespace_write_helper; |
| 3081 | } else { |
| 3082 | wq = root->fs_info->endio_write_workers; |
| 3083 | func = btrfs_endio_write_helper; |
| 3084 | } |
Josef Bacik | 5fd0204 | 2012-05-02 14:00:54 -0400 | [diff] [blame] | 3085 | |
Liu Bo | 9e0af23 | 2014-08-15 23:36:53 +0800 | [diff] [blame] | 3086 | btrfs_init_work(&ordered_extent->work, func, finish_ordered_fn, NULL, |
| 3087 | NULL); |
| 3088 | btrfs_queue_work(wq, &ordered_extent->work); |
Josef Bacik | 5fd0204 | 2012-05-02 14:00:54 -0400 | [diff] [blame] | 3089 | |
| 3090 | return 0; |
Chris Mason | 211f90e | 2008-07-18 11:56:15 -0400 | [diff] [blame] | 3091 | } |
| 3092 | |
Miao Xie | dc380ae | 2014-09-12 18:43:55 +0800 | [diff] [blame] | 3093 | static int __readpage_endio_check(struct inode *inode, |
| 3094 | struct btrfs_io_bio *io_bio, |
| 3095 | int icsum, struct page *page, |
| 3096 | int pgoff, u64 start, size_t len) |
| 3097 | { |
| 3098 | char *kaddr; |
| 3099 | u32 csum_expected; |
| 3100 | u32 csum = ~(u32)0; |
Miao Xie | dc380ae | 2014-09-12 18:43:55 +0800 | [diff] [blame] | 3101 | |
| 3102 | csum_expected = *(((u32 *)io_bio->csum) + icsum); |
| 3103 | |
| 3104 | kaddr = kmap_atomic(page); |
| 3105 | csum = btrfs_csum_data(kaddr + pgoff, csum, len); |
| 3106 | btrfs_csum_final(csum, (char *)&csum); |
| 3107 | if (csum != csum_expected) |
| 3108 | goto zeroit; |
| 3109 | |
| 3110 | kunmap_atomic(kaddr); |
| 3111 | return 0; |
| 3112 | zeroit: |
David Sterba | 9464732 | 2015-10-08 11:01:36 +0200 | [diff] [blame] | 3113 | btrfs_warn_rl(BTRFS_I(inode)->root->fs_info, |
| 3114 | "csum failed ino %llu off %llu csum %u expected csum %u", |
Miao Xie | dc380ae | 2014-09-12 18:43:55 +0800 | [diff] [blame] | 3115 | btrfs_ino(inode), start, csum, csum_expected); |
| 3116 | memset(kaddr + pgoff, 1, len); |
| 3117 | flush_dcache_page(page); |
| 3118 | kunmap_atomic(kaddr); |
| 3119 | if (csum_expected == 0) |
| 3120 | return 0; |
| 3121 | return -EIO; |
| 3122 | } |
| 3123 | |
Chris Mason | d352ac6 | 2008-09-29 15:18:18 -0400 | [diff] [blame] | 3124 | /* |
Chris Mason | d352ac6 | 2008-09-29 15:18:18 -0400 | [diff] [blame] | 3125 | * when reads are done, we need to check csums to verify the data is correct |
Jan Schmidt | 4a54c8c | 2011-07-22 15:41:52 +0200 | [diff] [blame] | 3126 | * if there's a match, we allow the bio to finish. If not, the code in |
| 3127 | * extent_io.c will try to find good copies for us. |
Chris Mason | d352ac6 | 2008-09-29 15:18:18 -0400 | [diff] [blame] | 3128 | */ |
Miao Xie | facc8a22 | 2013-07-25 19:22:34 +0800 | [diff] [blame] | 3129 | static int btrfs_readpage_end_io_hook(struct btrfs_io_bio *io_bio, |
| 3130 | u64 phy_offset, struct page *page, |
| 3131 | u64 start, u64 end, int mirror) |
Chris Mason | 07157aa | 2007-08-30 08:50:51 -0400 | [diff] [blame] | 3132 | { |
Miao Xie | 4eee4fa | 2012-12-21 09:17:45 +0000 | [diff] [blame] | 3133 | size_t offset = start - page_offset(page); |
Chris Mason | 07157aa | 2007-08-30 08:50:51 -0400 | [diff] [blame] | 3134 | struct inode *inode = page->mapping->host; |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 3135 | struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree; |
Chris Mason | ff79f81 | 2007-10-15 16:22:25 -0400 | [diff] [blame] | 3136 | struct btrfs_root *root = BTRFS_I(inode)->root; |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 3137 | |
Chris Mason | d20f704 | 2008-12-08 16:58:54 -0500 | [diff] [blame] | 3138 | if (PageChecked(page)) { |
| 3139 | ClearPageChecked(page); |
Miao Xie | dc380ae | 2014-09-12 18:43:55 +0800 | [diff] [blame] | 3140 | return 0; |
Chris Mason | d20f704 | 2008-12-08 16:58:54 -0500 | [diff] [blame] | 3141 | } |
Christoph Hellwig | 6cbff00 | 2009-04-17 10:37:41 +0200 | [diff] [blame] | 3142 | |
| 3143 | if (BTRFS_I(inode)->flags & BTRFS_INODE_NODATASUM) |
Miao Xie | dc380ae | 2014-09-12 18:43:55 +0800 | [diff] [blame] | 3144 | return 0; |
Chris Mason | d20f704 | 2008-12-08 16:58:54 -0500 | [diff] [blame] | 3145 | |
Yan Zheng | 17d217f | 2008-12-12 10:03:38 -0500 | [diff] [blame] | 3146 | if (root->root_key.objectid == BTRFS_DATA_RELOC_TREE_OBJECTID && |
Chris Mason | 9655d29 | 2009-09-02 15:22:30 -0400 | [diff] [blame] | 3147 | test_range_bit(io_tree, start, end, EXTENT_NODATASUM, 1, NULL)) { |
David Sterba | 9116621 | 2016-04-26 23:54:39 +0200 | [diff] [blame] | 3148 | clear_extent_bits(io_tree, start, end, EXTENT_NODATASUM); |
Yan Zheng | 17d217f | 2008-12-12 10:03:38 -0500 | [diff] [blame] | 3149 | return 0; |
| 3150 | } |
| 3151 | |
Miao Xie | facc8a22 | 2013-07-25 19:22:34 +0800 | [diff] [blame] | 3152 | phy_offset >>= inode->i_sb->s_blocksize_bits; |
Miao Xie | dc380ae | 2014-09-12 18:43:55 +0800 | [diff] [blame] | 3153 | return __readpage_endio_check(inode, io_bio, phy_offset, page, offset, |
| 3154 | start, (size_t)(end - start + 1)); |
Chris Mason | 07157aa | 2007-08-30 08:50:51 -0400 | [diff] [blame] | 3155 | } |
Chris Mason | b888db2 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 3156 | |
Yan, Zheng | 24bbcf0 | 2009-11-12 09:36:34 +0000 | [diff] [blame] | 3157 | void btrfs_add_delayed_iput(struct inode *inode) |
| 3158 | { |
| 3159 | struct btrfs_fs_info *fs_info = BTRFS_I(inode)->root->fs_info; |
David Sterba | 8089fe6 | 2015-11-19 14:15:51 +0100 | [diff] [blame] | 3160 | struct btrfs_inode *binode = BTRFS_I(inode); |
Yan, Zheng | 24bbcf0 | 2009-11-12 09:36:34 +0000 | [diff] [blame] | 3161 | |
| 3162 | if (atomic_add_unless(&inode->i_count, -1, 1)) |
| 3163 | return; |
| 3164 | |
Yan, Zheng | 24bbcf0 | 2009-11-12 09:36:34 +0000 | [diff] [blame] | 3165 | spin_lock(&fs_info->delayed_iput_lock); |
David Sterba | 8089fe6 | 2015-11-19 14:15:51 +0100 | [diff] [blame] | 3166 | if (binode->delayed_iput_count == 0) { |
| 3167 | ASSERT(list_empty(&binode->delayed_iput)); |
| 3168 | list_add_tail(&binode->delayed_iput, &fs_info->delayed_iputs); |
| 3169 | } else { |
| 3170 | binode->delayed_iput_count++; |
| 3171 | } |
Yan, Zheng | 24bbcf0 | 2009-11-12 09:36:34 +0000 | [diff] [blame] | 3172 | spin_unlock(&fs_info->delayed_iput_lock); |
| 3173 | } |
| 3174 | |
| 3175 | void btrfs_run_delayed_iputs(struct btrfs_root *root) |
| 3176 | { |
Yan, Zheng | 24bbcf0 | 2009-11-12 09:36:34 +0000 | [diff] [blame] | 3177 | struct btrfs_fs_info *fs_info = root->fs_info; |
Yan, Zheng | 24bbcf0 | 2009-11-12 09:36:34 +0000 | [diff] [blame] | 3178 | |
Yan, Zheng | 24bbcf0 | 2009-11-12 09:36:34 +0000 | [diff] [blame] | 3179 | spin_lock(&fs_info->delayed_iput_lock); |
David Sterba | 8089fe6 | 2015-11-19 14:15:51 +0100 | [diff] [blame] | 3180 | while (!list_empty(&fs_info->delayed_iputs)) { |
| 3181 | struct btrfs_inode *inode; |
Yan, Zheng | 24bbcf0 | 2009-11-12 09:36:34 +0000 | [diff] [blame] | 3182 | |
David Sterba | 8089fe6 | 2015-11-19 14:15:51 +0100 | [diff] [blame] | 3183 | inode = list_first_entry(&fs_info->delayed_iputs, |
| 3184 | struct btrfs_inode, delayed_iput); |
| 3185 | if (inode->delayed_iput_count) { |
| 3186 | inode->delayed_iput_count--; |
| 3187 | list_move_tail(&inode->delayed_iput, |
| 3188 | &fs_info->delayed_iputs); |
| 3189 | } else { |
| 3190 | list_del_init(&inode->delayed_iput); |
| 3191 | } |
| 3192 | spin_unlock(&fs_info->delayed_iput_lock); |
| 3193 | iput(&inode->vfs_inode); |
| 3194 | spin_lock(&fs_info->delayed_iput_lock); |
Yan, Zheng | 24bbcf0 | 2009-11-12 09:36:34 +0000 | [diff] [blame] | 3195 | } |
David Sterba | 8089fe6 | 2015-11-19 14:15:51 +0100 | [diff] [blame] | 3196 | spin_unlock(&fs_info->delayed_iput_lock); |
Yan, Zheng | 24bbcf0 | 2009-11-12 09:36:34 +0000 | [diff] [blame] | 3197 | } |
| 3198 | |
Yan, Zheng | d68fc57 | 2010-05-16 10:49:58 -0400 | [diff] [blame] | 3199 | /* |
Justin P. Mattock | 42b2aa8 | 2011-11-28 20:31:00 -0800 | [diff] [blame] | 3200 | * This is called in transaction commit time. If there are no orphan |
Yan, Zheng | d68fc57 | 2010-05-16 10:49:58 -0400 | [diff] [blame] | 3201 | * files in the subvolume, it removes orphan item and frees block_rsv |
| 3202 | * structure. |
| 3203 | */ |
| 3204 | void btrfs_orphan_commit_root(struct btrfs_trans_handle *trans, |
| 3205 | struct btrfs_root *root) |
| 3206 | { |
Josef Bacik | 90290e1 | 2011-12-02 15:44:12 -0500 | [diff] [blame] | 3207 | struct btrfs_block_rsv *block_rsv; |
Yan, Zheng | d68fc57 | 2010-05-16 10:49:58 -0400 | [diff] [blame] | 3208 | int ret; |
| 3209 | |
Josef Bacik | 8a35d95 | 2012-05-23 14:26:42 -0400 | [diff] [blame] | 3210 | if (atomic_read(&root->orphan_inodes) || |
Yan, Zheng | d68fc57 | 2010-05-16 10:49:58 -0400 | [diff] [blame] | 3211 | root->orphan_cleanup_state != ORPHAN_CLEANUP_DONE) |
| 3212 | return; |
| 3213 | |
Josef Bacik | 90290e1 | 2011-12-02 15:44:12 -0500 | [diff] [blame] | 3214 | spin_lock(&root->orphan_lock); |
Josef Bacik | 8a35d95 | 2012-05-23 14:26:42 -0400 | [diff] [blame] | 3215 | if (atomic_read(&root->orphan_inodes)) { |
Josef Bacik | 90290e1 | 2011-12-02 15:44:12 -0500 | [diff] [blame] | 3216 | spin_unlock(&root->orphan_lock); |
| 3217 | return; |
| 3218 | } |
| 3219 | |
| 3220 | if (root->orphan_cleanup_state != ORPHAN_CLEANUP_DONE) { |
| 3221 | spin_unlock(&root->orphan_lock); |
| 3222 | return; |
| 3223 | } |
| 3224 | |
| 3225 | block_rsv = root->orphan_block_rsv; |
| 3226 | root->orphan_block_rsv = NULL; |
| 3227 | spin_unlock(&root->orphan_lock); |
| 3228 | |
Miao Xie | 27cdeb7 | 2014-04-02 19:51:05 +0800 | [diff] [blame] | 3229 | if (test_bit(BTRFS_ROOT_ORPHAN_ITEM_INSERTED, &root->state) && |
Yan, Zheng | d68fc57 | 2010-05-16 10:49:58 -0400 | [diff] [blame] | 3230 | btrfs_root_refs(&root->root_item) > 0) { |
| 3231 | ret = btrfs_del_orphan_item(trans, root->fs_info->tree_root, |
| 3232 | root->root_key.objectid); |
Josef Bacik | 4ef31a4 | 2013-08-13 14:10:08 -0400 | [diff] [blame] | 3233 | if (ret) |
Jeff Mahoney | 6664283 | 2016-06-10 18:19:25 -0400 | [diff] [blame] | 3234 | btrfs_abort_transaction(trans, ret); |
Josef Bacik | 4ef31a4 | 2013-08-13 14:10:08 -0400 | [diff] [blame] | 3235 | else |
Miao Xie | 27cdeb7 | 2014-04-02 19:51:05 +0800 | [diff] [blame] | 3236 | clear_bit(BTRFS_ROOT_ORPHAN_ITEM_INSERTED, |
| 3237 | &root->state); |
Yan, Zheng | d68fc57 | 2010-05-16 10:49:58 -0400 | [diff] [blame] | 3238 | } |
| 3239 | |
Josef Bacik | 90290e1 | 2011-12-02 15:44:12 -0500 | [diff] [blame] | 3240 | if (block_rsv) { |
| 3241 | WARN_ON(block_rsv->size > 0); |
| 3242 | btrfs_free_block_rsv(root, block_rsv); |
Yan, Zheng | d68fc57 | 2010-05-16 10:49:58 -0400 | [diff] [blame] | 3243 | } |
| 3244 | } |
| 3245 | |
| 3246 | /* |
Josef Bacik | 7b12876 | 2008-07-24 12:17:14 -0400 | [diff] [blame] | 3247 | * This creates an orphan entry for the given inode in case something goes |
| 3248 | * wrong in the middle of an unlink/truncate. |
Yan, Zheng | d68fc57 | 2010-05-16 10:49:58 -0400 | [diff] [blame] | 3249 | * |
| 3250 | * NOTE: caller of this function should reserve 5 units of metadata for |
| 3251 | * this function. |
Josef Bacik | 7b12876 | 2008-07-24 12:17:14 -0400 | [diff] [blame] | 3252 | */ |
| 3253 | int btrfs_orphan_add(struct btrfs_trans_handle *trans, struct inode *inode) |
| 3254 | { |
| 3255 | struct btrfs_root *root = BTRFS_I(inode)->root; |
Yan, Zheng | d68fc57 | 2010-05-16 10:49:58 -0400 | [diff] [blame] | 3256 | struct btrfs_block_rsv *block_rsv = NULL; |
| 3257 | int reserve = 0; |
| 3258 | int insert = 0; |
| 3259 | int ret; |
Josef Bacik | 7b12876 | 2008-07-24 12:17:14 -0400 | [diff] [blame] | 3260 | |
Yan, Zheng | d68fc57 | 2010-05-16 10:49:58 -0400 | [diff] [blame] | 3261 | if (!root->orphan_block_rsv) { |
Miao Xie | 66d8f3d | 2012-09-06 04:02:28 -0600 | [diff] [blame] | 3262 | block_rsv = btrfs_alloc_block_rsv(root, BTRFS_BLOCK_RSV_TEMP); |
Tsutomu Itoh | b532402 | 2011-07-19 07:27:20 +0000 | [diff] [blame] | 3263 | if (!block_rsv) |
| 3264 | return -ENOMEM; |
Josef Bacik | 7b12876 | 2008-07-24 12:17:14 -0400 | [diff] [blame] | 3265 | } |
| 3266 | |
Yan, Zheng | d68fc57 | 2010-05-16 10:49:58 -0400 | [diff] [blame] | 3267 | spin_lock(&root->orphan_lock); |
| 3268 | if (!root->orphan_block_rsv) { |
| 3269 | root->orphan_block_rsv = block_rsv; |
| 3270 | } else if (block_rsv) { |
| 3271 | btrfs_free_block_rsv(root, block_rsv); |
| 3272 | block_rsv = NULL; |
| 3273 | } |
Josef Bacik | 7b12876 | 2008-07-24 12:17:14 -0400 | [diff] [blame] | 3274 | |
Josef Bacik | 8a35d95 | 2012-05-23 14:26:42 -0400 | [diff] [blame] | 3275 | if (!test_and_set_bit(BTRFS_INODE_HAS_ORPHAN_ITEM, |
| 3276 | &BTRFS_I(inode)->runtime_flags)) { |
Yan, Zheng | d68fc57 | 2010-05-16 10:49:58 -0400 | [diff] [blame] | 3277 | #if 0 |
| 3278 | /* |
| 3279 | * For proper ENOSPC handling, we should do orphan |
| 3280 | * cleanup when mounting. But this introduces backward |
| 3281 | * compatibility issue. |
| 3282 | */ |
| 3283 | if (!xchg(&root->orphan_item_inserted, 1)) |
| 3284 | insert = 2; |
| 3285 | else |
| 3286 | insert = 1; |
| 3287 | #endif |
| 3288 | insert = 1; |
Miao Xie | 321f0e7 | 2012-08-28 22:13:02 -0600 | [diff] [blame] | 3289 | atomic_inc(&root->orphan_inodes); |
Yan, Zheng | d68fc57 | 2010-05-16 10:49:58 -0400 | [diff] [blame] | 3290 | } |
Josef Bacik | 7b12876 | 2008-07-24 12:17:14 -0400 | [diff] [blame] | 3291 | |
Josef Bacik | 72ac3c0 | 2012-05-23 14:13:11 -0400 | [diff] [blame] | 3292 | if (!test_and_set_bit(BTRFS_INODE_ORPHAN_META_RESERVED, |
| 3293 | &BTRFS_I(inode)->runtime_flags)) |
Yan, Zheng | d68fc57 | 2010-05-16 10:49:58 -0400 | [diff] [blame] | 3294 | reserve = 1; |
Yan, Zheng | d68fc57 | 2010-05-16 10:49:58 -0400 | [diff] [blame] | 3295 | spin_unlock(&root->orphan_lock); |
Josef Bacik | 7b12876 | 2008-07-24 12:17:14 -0400 | [diff] [blame] | 3296 | |
Yan, Zheng | d68fc57 | 2010-05-16 10:49:58 -0400 | [diff] [blame] | 3297 | /* grab metadata reservation from transaction handle */ |
| 3298 | if (reserve) { |
| 3299 | ret = btrfs_orphan_reserve_metadata(trans, inode); |
Josef Bacik | 3b6571c | 2016-05-27 13:03:04 -0400 | [diff] [blame] | 3300 | ASSERT(!ret); |
| 3301 | if (ret) { |
| 3302 | atomic_dec(&root->orphan_inodes); |
| 3303 | clear_bit(BTRFS_INODE_ORPHAN_META_RESERVED, |
| 3304 | &BTRFS_I(inode)->runtime_flags); |
| 3305 | if (insert) |
| 3306 | clear_bit(BTRFS_INODE_HAS_ORPHAN_ITEM, |
| 3307 | &BTRFS_I(inode)->runtime_flags); |
| 3308 | return ret; |
| 3309 | } |
Yan, Zheng | d68fc57 | 2010-05-16 10:49:58 -0400 | [diff] [blame] | 3310 | } |
| 3311 | |
| 3312 | /* insert an orphan item to track this unlinked/truncated file */ |
| 3313 | if (insert >= 1) { |
Li Zefan | 33345d01 | 2011-04-20 10:31:50 +0800 | [diff] [blame] | 3314 | ret = btrfs_insert_orphan_item(trans, root, btrfs_ino(inode)); |
Josef Bacik | 4ef31a4 | 2013-08-13 14:10:08 -0400 | [diff] [blame] | 3315 | if (ret) { |
Filipe David Borba Manana | 703c88e | 2013-09-22 21:54:55 +0100 | [diff] [blame] | 3316 | atomic_dec(&root->orphan_inodes); |
Josef Bacik | 4ef31a4 | 2013-08-13 14:10:08 -0400 | [diff] [blame] | 3317 | if (reserve) { |
| 3318 | clear_bit(BTRFS_INODE_ORPHAN_META_RESERVED, |
| 3319 | &BTRFS_I(inode)->runtime_flags); |
| 3320 | btrfs_orphan_release_metadata(inode); |
| 3321 | } |
| 3322 | if (ret != -EEXIST) { |
Josef Bacik | e8e7cff | 2013-08-21 15:54:00 -0400 | [diff] [blame] | 3323 | clear_bit(BTRFS_INODE_HAS_ORPHAN_ITEM, |
| 3324 | &BTRFS_I(inode)->runtime_flags); |
Jeff Mahoney | 6664283 | 2016-06-10 18:19:25 -0400 | [diff] [blame] | 3325 | btrfs_abort_transaction(trans, ret); |
Josef Bacik | 4ef31a4 | 2013-08-13 14:10:08 -0400 | [diff] [blame] | 3326 | return ret; |
| 3327 | } |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 3328 | } |
| 3329 | ret = 0; |
Yan, Zheng | d68fc57 | 2010-05-16 10:49:58 -0400 | [diff] [blame] | 3330 | } |
| 3331 | |
| 3332 | /* insert an orphan item to track subvolume contains orphan files */ |
| 3333 | if (insert >= 2) { |
| 3334 | ret = btrfs_insert_orphan_item(trans, root->fs_info->tree_root, |
| 3335 | root->root_key.objectid); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 3336 | if (ret && ret != -EEXIST) { |
Jeff Mahoney | 6664283 | 2016-06-10 18:19:25 -0400 | [diff] [blame] | 3337 | btrfs_abort_transaction(trans, ret); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 3338 | return ret; |
| 3339 | } |
Yan, Zheng | d68fc57 | 2010-05-16 10:49:58 -0400 | [diff] [blame] | 3340 | } |
| 3341 | return 0; |
Josef Bacik | 7b12876 | 2008-07-24 12:17:14 -0400 | [diff] [blame] | 3342 | } |
| 3343 | |
| 3344 | /* |
| 3345 | * We have done the truncate/delete so we can go ahead and remove the orphan |
| 3346 | * item for this particular inode. |
| 3347 | */ |
Eric Sandeen | 48a3b63 | 2013-04-25 20:41:01 +0000 | [diff] [blame] | 3348 | static int btrfs_orphan_del(struct btrfs_trans_handle *trans, |
| 3349 | struct inode *inode) |
Josef Bacik | 7b12876 | 2008-07-24 12:17:14 -0400 | [diff] [blame] | 3350 | { |
| 3351 | struct btrfs_root *root = BTRFS_I(inode)->root; |
Yan, Zheng | d68fc57 | 2010-05-16 10:49:58 -0400 | [diff] [blame] | 3352 | int delete_item = 0; |
| 3353 | int release_rsv = 0; |
Josef Bacik | 7b12876 | 2008-07-24 12:17:14 -0400 | [diff] [blame] | 3354 | int ret = 0; |
| 3355 | |
Yan, Zheng | d68fc57 | 2010-05-16 10:49:58 -0400 | [diff] [blame] | 3356 | spin_lock(&root->orphan_lock); |
Josef Bacik | 8a35d95 | 2012-05-23 14:26:42 -0400 | [diff] [blame] | 3357 | if (test_and_clear_bit(BTRFS_INODE_HAS_ORPHAN_ITEM, |
| 3358 | &BTRFS_I(inode)->runtime_flags)) |
Yan, Zheng | d68fc57 | 2010-05-16 10:49:58 -0400 | [diff] [blame] | 3359 | delete_item = 1; |
Josef Bacik | 7b12876 | 2008-07-24 12:17:14 -0400 | [diff] [blame] | 3360 | |
Josef Bacik | 72ac3c0 | 2012-05-23 14:13:11 -0400 | [diff] [blame] | 3361 | if (test_and_clear_bit(BTRFS_INODE_ORPHAN_META_RESERVED, |
| 3362 | &BTRFS_I(inode)->runtime_flags)) |
Yan, Zheng | d68fc57 | 2010-05-16 10:49:58 -0400 | [diff] [blame] | 3363 | release_rsv = 1; |
Yan, Zheng | d68fc57 | 2010-05-16 10:49:58 -0400 | [diff] [blame] | 3364 | spin_unlock(&root->orphan_lock); |
| 3365 | |
Filipe David Borba Manana | 703c88e | 2013-09-22 21:54:55 +0100 | [diff] [blame] | 3366 | if (delete_item) { |
Josef Bacik | 8a35d95 | 2012-05-23 14:26:42 -0400 | [diff] [blame] | 3367 | atomic_dec(&root->orphan_inodes); |
Filipe David Borba Manana | 703c88e | 2013-09-22 21:54:55 +0100 | [diff] [blame] | 3368 | if (trans) |
| 3369 | ret = btrfs_del_orphan_item(trans, root, |
| 3370 | btrfs_ino(inode)); |
Josef Bacik | 8a35d95 | 2012-05-23 14:26:42 -0400 | [diff] [blame] | 3371 | } |
Josef Bacik | 7b12876 | 2008-07-24 12:17:14 -0400 | [diff] [blame] | 3372 | |
Filipe David Borba Manana | 703c88e | 2013-09-22 21:54:55 +0100 | [diff] [blame] | 3373 | if (release_rsv) |
| 3374 | btrfs_orphan_release_metadata(inode); |
| 3375 | |
Josef Bacik | 4ef31a4 | 2013-08-13 14:10:08 -0400 | [diff] [blame] | 3376 | return ret; |
Josef Bacik | 7b12876 | 2008-07-24 12:17:14 -0400 | [diff] [blame] | 3377 | } |
| 3378 | |
| 3379 | /* |
| 3380 | * this cleans up any orphans that may be left on the list from the last use |
| 3381 | * of this root. |
| 3382 | */ |
Josef Bacik | 66b4ffd | 2011-01-31 16:22:42 -0500 | [diff] [blame] | 3383 | int btrfs_orphan_cleanup(struct btrfs_root *root) |
Josef Bacik | 7b12876 | 2008-07-24 12:17:14 -0400 | [diff] [blame] | 3384 | { |
| 3385 | struct btrfs_path *path; |
| 3386 | struct extent_buffer *leaf; |
Josef Bacik | 7b12876 | 2008-07-24 12:17:14 -0400 | [diff] [blame] | 3387 | struct btrfs_key key, found_key; |
| 3388 | struct btrfs_trans_handle *trans; |
| 3389 | struct inode *inode; |
Josef Bacik | 8f6d7f4 | 2011-09-26 15:55:20 -0400 | [diff] [blame] | 3390 | u64 last_objectid = 0; |
Josef Bacik | 7b12876 | 2008-07-24 12:17:14 -0400 | [diff] [blame] | 3391 | int ret = 0, nr_unlink = 0, nr_truncate = 0; |
| 3392 | |
Yan, Zheng | d68fc57 | 2010-05-16 10:49:58 -0400 | [diff] [blame] | 3393 | if (cmpxchg(&root->orphan_cleanup_state, 0, ORPHAN_CLEANUP_STARTED)) |
Josef Bacik | 66b4ffd | 2011-01-31 16:22:42 -0500 | [diff] [blame] | 3394 | return 0; |
Yan, Zheng | c71bf09 | 2009-11-12 09:34:40 +0000 | [diff] [blame] | 3395 | |
| 3396 | path = btrfs_alloc_path(); |
Josef Bacik | 66b4ffd | 2011-01-31 16:22:42 -0500 | [diff] [blame] | 3397 | if (!path) { |
| 3398 | ret = -ENOMEM; |
| 3399 | goto out; |
| 3400 | } |
David Sterba | e4058b5 | 2015-11-27 16:31:35 +0100 | [diff] [blame] | 3401 | path->reada = READA_BACK; |
Josef Bacik | 7b12876 | 2008-07-24 12:17:14 -0400 | [diff] [blame] | 3402 | |
| 3403 | key.objectid = BTRFS_ORPHAN_OBJECTID; |
David Sterba | 962a298 | 2014-06-04 18:41:45 +0200 | [diff] [blame] | 3404 | key.type = BTRFS_ORPHAN_ITEM_KEY; |
Josef Bacik | 7b12876 | 2008-07-24 12:17:14 -0400 | [diff] [blame] | 3405 | key.offset = (u64)-1; |
| 3406 | |
Josef Bacik | 7b12876 | 2008-07-24 12:17:14 -0400 | [diff] [blame] | 3407 | while (1) { |
| 3408 | ret = btrfs_search_slot(NULL, root, &key, path, 0, 0); |
Josef Bacik | 66b4ffd | 2011-01-31 16:22:42 -0500 | [diff] [blame] | 3409 | if (ret < 0) |
| 3410 | goto out; |
Josef Bacik | 7b12876 | 2008-07-24 12:17:14 -0400 | [diff] [blame] | 3411 | |
| 3412 | /* |
| 3413 | * 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] | 3414 | * 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] | 3415 | * find the key and see if we have stuff that matches |
| 3416 | */ |
| 3417 | if (ret > 0) { |
Josef Bacik | 66b4ffd | 2011-01-31 16:22:42 -0500 | [diff] [blame] | 3418 | ret = 0; |
Josef Bacik | 7b12876 | 2008-07-24 12:17:14 -0400 | [diff] [blame] | 3419 | if (path->slots[0] == 0) |
| 3420 | break; |
| 3421 | path->slots[0]--; |
| 3422 | } |
| 3423 | |
| 3424 | /* pull out the item */ |
| 3425 | leaf = path->nodes[0]; |
Josef Bacik | 7b12876 | 2008-07-24 12:17:14 -0400 | [diff] [blame] | 3426 | btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]); |
| 3427 | |
| 3428 | /* make sure the item matches what we want */ |
| 3429 | if (found_key.objectid != BTRFS_ORPHAN_OBJECTID) |
| 3430 | break; |
David Sterba | 962a298 | 2014-06-04 18:41:45 +0200 | [diff] [blame] | 3431 | if (found_key.type != BTRFS_ORPHAN_ITEM_KEY) |
Josef Bacik | 7b12876 | 2008-07-24 12:17:14 -0400 | [diff] [blame] | 3432 | break; |
| 3433 | |
| 3434 | /* release the path since we're done with it */ |
David Sterba | b3b4aa7 | 2011-04-21 01:20:15 +0200 | [diff] [blame] | 3435 | btrfs_release_path(path); |
Josef Bacik | 7b12876 | 2008-07-24 12:17:14 -0400 | [diff] [blame] | 3436 | |
| 3437 | /* |
| 3438 | * this is where we are basically btrfs_lookup, without the |
| 3439 | * crossing root thing. we store the inode number in the |
| 3440 | * offset of the orphan item. |
| 3441 | */ |
Josef Bacik | 8f6d7f4 | 2011-09-26 15:55:20 -0400 | [diff] [blame] | 3442 | |
| 3443 | if (found_key.offset == last_objectid) { |
Simon Kirby | c2cf52e | 2013-03-19 22:41:23 +0000 | [diff] [blame] | 3444 | btrfs_err(root->fs_info, |
| 3445 | "Error removing orphan entry, stopping orphan cleanup"); |
Josef Bacik | 8f6d7f4 | 2011-09-26 15:55:20 -0400 | [diff] [blame] | 3446 | ret = -EINVAL; |
| 3447 | goto out; |
| 3448 | } |
| 3449 | |
| 3450 | last_objectid = found_key.offset; |
| 3451 | |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 3452 | found_key.objectid = found_key.offset; |
| 3453 | found_key.type = BTRFS_INODE_ITEM_KEY; |
| 3454 | found_key.offset = 0; |
Josef Bacik | 73f7341 | 2009-12-04 17:38:27 +0000 | [diff] [blame] | 3455 | inode = btrfs_iget(root->fs_info->sb, &found_key, root, NULL); |
Rusty Russell | 8c6ffba | 2013-07-15 11:20:32 +0930 | [diff] [blame] | 3456 | ret = PTR_ERR_OR_ZERO(inode); |
Filipe Manana | 6771089 | 2016-06-06 11:51:25 +0100 | [diff] [blame] | 3457 | if (ret && ret != -ENOENT) |
Josef Bacik | 66b4ffd | 2011-01-31 16:22:42 -0500 | [diff] [blame] | 3458 | goto out; |
Josef Bacik | a8c9e57 | 2011-09-21 16:55:59 -0400 | [diff] [blame] | 3459 | |
Filipe Manana | 6771089 | 2016-06-06 11:51:25 +0100 | [diff] [blame] | 3460 | if (ret == -ENOENT && root == root->fs_info->tree_root) { |
Arne Jansen | f8e9e0b | 2011-12-14 20:12:02 -0500 | [diff] [blame] | 3461 | struct btrfs_root *dead_root; |
| 3462 | struct btrfs_fs_info *fs_info = root->fs_info; |
| 3463 | int is_dead_root = 0; |
| 3464 | |
| 3465 | /* |
| 3466 | * this is an orphan in the tree root. Currently these |
| 3467 | * could come from 2 sources: |
| 3468 | * a) a snapshot deletion in progress |
| 3469 | * b) a free space cache inode |
| 3470 | * We need to distinguish those two, as the snapshot |
| 3471 | * orphan must not get deleted. |
| 3472 | * find_dead_roots already ran before us, so if this |
| 3473 | * is a snapshot deletion, we should find the root |
| 3474 | * in the dead_roots list |
| 3475 | */ |
| 3476 | spin_lock(&fs_info->trans_lock); |
| 3477 | list_for_each_entry(dead_root, &fs_info->dead_roots, |
| 3478 | root_list) { |
| 3479 | if (dead_root->root_key.objectid == |
| 3480 | found_key.objectid) { |
| 3481 | is_dead_root = 1; |
| 3482 | break; |
| 3483 | } |
| 3484 | } |
| 3485 | spin_unlock(&fs_info->trans_lock); |
| 3486 | if (is_dead_root) { |
| 3487 | /* prevent this orphan from being found again */ |
| 3488 | key.offset = found_key.objectid - 1; |
| 3489 | continue; |
| 3490 | } |
| 3491 | } |
Josef Bacik | a8c9e57 | 2011-09-21 16:55:59 -0400 | [diff] [blame] | 3492 | /* |
| 3493 | * Inode is already gone but the orphan item is still there, |
| 3494 | * kill the orphan item. |
| 3495 | */ |
Filipe Manana | 6771089 | 2016-06-06 11:51:25 +0100 | [diff] [blame] | 3496 | if (ret == -ENOENT) { |
Josef Bacik | a8c9e57 | 2011-09-21 16:55:59 -0400 | [diff] [blame] | 3497 | trans = btrfs_start_transaction(root, 1); |
| 3498 | if (IS_ERR(trans)) { |
| 3499 | ret = PTR_ERR(trans); |
| 3500 | goto out; |
| 3501 | } |
Simon Kirby | c2cf52e | 2013-03-19 22:41:23 +0000 | [diff] [blame] | 3502 | btrfs_debug(root->fs_info, "auto deleting %Lu", |
| 3503 | found_key.objectid); |
Josef Bacik | a8c9e57 | 2011-09-21 16:55:59 -0400 | [diff] [blame] | 3504 | ret = btrfs_del_orphan_item(trans, root, |
| 3505 | found_key.objectid); |
Josef Bacik | a8c9e57 | 2011-09-21 16:55:59 -0400 | [diff] [blame] | 3506 | btrfs_end_transaction(trans, root); |
Josef Bacik | 4ef31a4 | 2013-08-13 14:10:08 -0400 | [diff] [blame] | 3507 | if (ret) |
| 3508 | goto out; |
Josef Bacik | a8c9e57 | 2011-09-21 16:55:59 -0400 | [diff] [blame] | 3509 | continue; |
Josef Bacik | 66b4ffd | 2011-01-31 16:22:42 -0500 | [diff] [blame] | 3510 | } |
Josef Bacik | 7b12876 | 2008-07-24 12:17:14 -0400 | [diff] [blame] | 3511 | |
Josef Bacik | 7b12876 | 2008-07-24 12:17:14 -0400 | [diff] [blame] | 3512 | /* |
| 3513 | * add this inode to the orphan list so btrfs_orphan_del does |
| 3514 | * the proper thing when we hit it |
| 3515 | */ |
Josef Bacik | 8a35d95 | 2012-05-23 14:26:42 -0400 | [diff] [blame] | 3516 | set_bit(BTRFS_INODE_HAS_ORPHAN_ITEM, |
| 3517 | &BTRFS_I(inode)->runtime_flags); |
Josef Bacik | 925396e | 2013-02-01 15:57:47 -0500 | [diff] [blame] | 3518 | atomic_inc(&root->orphan_inodes); |
Josef Bacik | 7b12876 | 2008-07-24 12:17:14 -0400 | [diff] [blame] | 3519 | |
Josef Bacik | 7b12876 | 2008-07-24 12:17:14 -0400 | [diff] [blame] | 3520 | /* if we have links, this was a truncate, lets do that */ |
| 3521 | if (inode->i_nlink) { |
Dulshani Gunawardhana | fae7f21 | 2013-10-31 10:30:08 +0530 | [diff] [blame] | 3522 | if (WARN_ON(!S_ISREG(inode->i_mode))) { |
Josef Bacik | a41ad39 | 2011-01-31 15:30:16 -0500 | [diff] [blame] | 3523 | iput(inode); |
| 3524 | continue; |
| 3525 | } |
Josef Bacik | 7b12876 | 2008-07-24 12:17:14 -0400 | [diff] [blame] | 3526 | nr_truncate++; |
Josef Bacik | f3fe820 | 2013-01-07 17:03:21 -0500 | [diff] [blame] | 3527 | |
| 3528 | /* 1 for the orphan item deletion. */ |
| 3529 | trans = btrfs_start_transaction(root, 1); |
| 3530 | if (IS_ERR(trans)) { |
Josef Bacik | c69b26b | 2013-06-03 16:51:23 -0400 | [diff] [blame] | 3531 | iput(inode); |
Josef Bacik | f3fe820 | 2013-01-07 17:03:21 -0500 | [diff] [blame] | 3532 | ret = PTR_ERR(trans); |
| 3533 | goto out; |
| 3534 | } |
| 3535 | ret = btrfs_orphan_add(trans, inode); |
| 3536 | btrfs_end_transaction(trans, root); |
Josef Bacik | c69b26b | 2013-06-03 16:51:23 -0400 | [diff] [blame] | 3537 | if (ret) { |
| 3538 | iput(inode); |
Josef Bacik | f3fe820 | 2013-01-07 17:03:21 -0500 | [diff] [blame] | 3539 | goto out; |
Josef Bacik | c69b26b | 2013-06-03 16:51:23 -0400 | [diff] [blame] | 3540 | } |
Josef Bacik | f3fe820 | 2013-01-07 17:03:21 -0500 | [diff] [blame] | 3541 | |
Josef Bacik | 66b4ffd | 2011-01-31 16:22:42 -0500 | [diff] [blame] | 3542 | ret = btrfs_truncate(inode); |
Josef Bacik | 4a7d0f6 | 2013-02-07 16:27:28 -0500 | [diff] [blame] | 3543 | if (ret) |
| 3544 | btrfs_orphan_del(NULL, inode); |
Josef Bacik | 7b12876 | 2008-07-24 12:17:14 -0400 | [diff] [blame] | 3545 | } else { |
| 3546 | nr_unlink++; |
| 3547 | } |
| 3548 | |
| 3549 | /* this will do delete_inode and everything for us */ |
| 3550 | iput(inode); |
Josef Bacik | 66b4ffd | 2011-01-31 16:22:42 -0500 | [diff] [blame] | 3551 | if (ret) |
| 3552 | goto out; |
Josef Bacik | 7b12876 | 2008-07-24 12:17:14 -0400 | [diff] [blame] | 3553 | } |
Miao Xie | 3254c87 | 2011-11-10 20:45:05 -0500 | [diff] [blame] | 3554 | /* release the path since we're done with it */ |
| 3555 | btrfs_release_path(path); |
| 3556 | |
Yan, Zheng | d68fc57 | 2010-05-16 10:49:58 -0400 | [diff] [blame] | 3557 | root->orphan_cleanup_state = ORPHAN_CLEANUP_DONE; |
| 3558 | |
| 3559 | if (root->orphan_block_rsv) |
| 3560 | btrfs_block_rsv_release(root, root->orphan_block_rsv, |
| 3561 | (u64)-1); |
| 3562 | |
Miao Xie | 27cdeb7 | 2014-04-02 19:51:05 +0800 | [diff] [blame] | 3563 | if (root->orphan_block_rsv || |
| 3564 | test_bit(BTRFS_ROOT_ORPHAN_ITEM_INSERTED, &root->state)) { |
Josef Bacik | 7a7eaa4 | 2011-04-13 12:54:33 -0400 | [diff] [blame] | 3565 | trans = btrfs_join_transaction(root); |
Josef Bacik | 66b4ffd | 2011-01-31 16:22:42 -0500 | [diff] [blame] | 3566 | if (!IS_ERR(trans)) |
| 3567 | btrfs_end_transaction(trans, root); |
Yan, Zheng | d68fc57 | 2010-05-16 10:49:58 -0400 | [diff] [blame] | 3568 | } |
Josef Bacik | 7b12876 | 2008-07-24 12:17:14 -0400 | [diff] [blame] | 3569 | |
| 3570 | if (nr_unlink) |
David Sterba | 4884b47 | 2013-03-20 13:31:27 +0000 | [diff] [blame] | 3571 | btrfs_debug(root->fs_info, "unlinked %d orphans", nr_unlink); |
Josef Bacik | 7b12876 | 2008-07-24 12:17:14 -0400 | [diff] [blame] | 3572 | if (nr_truncate) |
David Sterba | 4884b47 | 2013-03-20 13:31:27 +0000 | [diff] [blame] | 3573 | btrfs_debug(root->fs_info, "truncated %d orphans", nr_truncate); |
Josef Bacik | 66b4ffd | 2011-01-31 16:22:42 -0500 | [diff] [blame] | 3574 | |
| 3575 | out: |
| 3576 | if (ret) |
David Sterba | 68b663d | 2014-12-19 18:38:37 +0100 | [diff] [blame] | 3577 | btrfs_err(root->fs_info, |
Simon Kirby | c2cf52e | 2013-03-19 22:41:23 +0000 | [diff] [blame] | 3578 | "could not do orphan cleanup %d", ret); |
Josef Bacik | 66b4ffd | 2011-01-31 16:22:42 -0500 | [diff] [blame] | 3579 | btrfs_free_path(path); |
| 3580 | return ret; |
Josef Bacik | 7b12876 | 2008-07-24 12:17:14 -0400 | [diff] [blame] | 3581 | } |
| 3582 | |
Chris Mason | d352ac6 | 2008-09-29 15:18:18 -0400 | [diff] [blame] | 3583 | /* |
Chris Mason | 46a53cc | 2009-04-27 11:47:50 -0400 | [diff] [blame] | 3584 | * very simple check to peek ahead in the leaf looking for xattrs. If we |
| 3585 | * don't find any xattrs, we know there can't be any acls. |
| 3586 | * |
| 3587 | * slot is the slot the inode is in, objectid is the objectid of the inode |
| 3588 | */ |
| 3589 | 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] | 3590 | int slot, u64 objectid, |
| 3591 | int *first_xattr_slot) |
Chris Mason | 46a53cc | 2009-04-27 11:47:50 -0400 | [diff] [blame] | 3592 | { |
| 3593 | u32 nritems = btrfs_header_nritems(leaf); |
| 3594 | struct btrfs_key found_key; |
Josef Bacik | f23b5a5 | 2013-06-19 10:16:26 -0400 | [diff] [blame] | 3595 | static u64 xattr_access = 0; |
| 3596 | static u64 xattr_default = 0; |
Chris Mason | 46a53cc | 2009-04-27 11:47:50 -0400 | [diff] [blame] | 3597 | int scanned = 0; |
| 3598 | |
Josef Bacik | f23b5a5 | 2013-06-19 10:16:26 -0400 | [diff] [blame] | 3599 | if (!xattr_access) { |
Andreas Gruenbacher | 97d7929 | 2015-12-02 14:44:35 +0100 | [diff] [blame] | 3600 | xattr_access = btrfs_name_hash(XATTR_NAME_POSIX_ACL_ACCESS, |
| 3601 | strlen(XATTR_NAME_POSIX_ACL_ACCESS)); |
| 3602 | xattr_default = btrfs_name_hash(XATTR_NAME_POSIX_ACL_DEFAULT, |
| 3603 | strlen(XATTR_NAME_POSIX_ACL_DEFAULT)); |
Josef Bacik | f23b5a5 | 2013-06-19 10:16:26 -0400 | [diff] [blame] | 3604 | } |
| 3605 | |
Chris Mason | 46a53cc | 2009-04-27 11:47:50 -0400 | [diff] [blame] | 3606 | slot++; |
Filipe David Borba Manana | 6354192 | 2014-01-07 11:47:46 +0000 | [diff] [blame] | 3607 | *first_xattr_slot = -1; |
Chris Mason | 46a53cc | 2009-04-27 11:47:50 -0400 | [diff] [blame] | 3608 | while (slot < nritems) { |
| 3609 | btrfs_item_key_to_cpu(leaf, &found_key, slot); |
| 3610 | |
| 3611 | /* we found a different objectid, there must not be acls */ |
| 3612 | if (found_key.objectid != objectid) |
| 3613 | return 0; |
| 3614 | |
| 3615 | /* we found an xattr, assume we've got an acl */ |
Josef Bacik | f23b5a5 | 2013-06-19 10:16:26 -0400 | [diff] [blame] | 3616 | if (found_key.type == BTRFS_XATTR_ITEM_KEY) { |
Filipe David Borba Manana | 6354192 | 2014-01-07 11:47:46 +0000 | [diff] [blame] | 3617 | if (*first_xattr_slot == -1) |
| 3618 | *first_xattr_slot = slot; |
Josef Bacik | f23b5a5 | 2013-06-19 10:16:26 -0400 | [diff] [blame] | 3619 | if (found_key.offset == xattr_access || |
| 3620 | found_key.offset == xattr_default) |
| 3621 | return 1; |
| 3622 | } |
Chris Mason | 46a53cc | 2009-04-27 11:47:50 -0400 | [diff] [blame] | 3623 | |
| 3624 | /* |
| 3625 | * we found a key greater than an xattr key, there can't |
| 3626 | * be any acls later on |
| 3627 | */ |
| 3628 | if (found_key.type > BTRFS_XATTR_ITEM_KEY) |
| 3629 | return 0; |
| 3630 | |
| 3631 | slot++; |
| 3632 | scanned++; |
| 3633 | |
| 3634 | /* |
| 3635 | * it goes inode, inode backrefs, xattrs, extents, |
| 3636 | * so if there are a ton of hard links to an inode there can |
| 3637 | * be a lot of backrefs. Don't waste time searching too hard, |
| 3638 | * this is just an optimization |
| 3639 | */ |
| 3640 | if (scanned >= 8) |
| 3641 | break; |
| 3642 | } |
| 3643 | /* we hit the end of the leaf before we found an xattr or |
| 3644 | * something larger than an xattr. We have to assume the inode |
| 3645 | * has acls |
| 3646 | */ |
Filipe David Borba Manana | 6354192 | 2014-01-07 11:47:46 +0000 | [diff] [blame] | 3647 | if (*first_xattr_slot == -1) |
| 3648 | *first_xattr_slot = slot; |
Chris Mason | 46a53cc | 2009-04-27 11:47:50 -0400 | [diff] [blame] | 3649 | return 1; |
| 3650 | } |
| 3651 | |
| 3652 | /* |
Chris Mason | d352ac6 | 2008-09-29 15:18:18 -0400 | [diff] [blame] | 3653 | * read an inode from the btree into the in-memory inode |
| 3654 | */ |
Filipe Manana | 6771089 | 2016-06-06 11:51:25 +0100 | [diff] [blame] | 3655 | static int btrfs_read_locked_inode(struct inode *inode) |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3656 | { |
| 3657 | struct btrfs_path *path; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 3658 | struct extent_buffer *leaf; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3659 | struct btrfs_inode_item *inode_item; |
| 3660 | struct btrfs_root *root = BTRFS_I(inode)->root; |
| 3661 | struct btrfs_key location; |
Miao Xie | 67de117 | 2013-12-26 13:07:06 +0800 | [diff] [blame] | 3662 | unsigned long ptr; |
Chris Mason | 46a53cc | 2009-04-27 11:47:50 -0400 | [diff] [blame] | 3663 | int maybe_acls; |
Josef Bacik | 618e21d | 2007-07-11 10:18:17 -0400 | [diff] [blame] | 3664 | u32 rdev; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3665 | int ret; |
Miao Xie | 2f7e33d | 2011-06-23 07:27:13 +0000 | [diff] [blame] | 3666 | bool filled = false; |
Filipe David Borba Manana | 6354192 | 2014-01-07 11:47:46 +0000 | [diff] [blame] | 3667 | int first_xattr_slot; |
Miao Xie | 2f7e33d | 2011-06-23 07:27:13 +0000 | [diff] [blame] | 3668 | |
| 3669 | ret = btrfs_fill_inode(inode, &rdev); |
| 3670 | if (!ret) |
| 3671 | filled = true; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3672 | |
| 3673 | path = btrfs_alloc_path(); |
Filipe Manana | 6771089 | 2016-06-06 11:51:25 +0100 | [diff] [blame] | 3674 | if (!path) { |
| 3675 | ret = -ENOMEM; |
Mark Fasheh | 1748f84 | 2011-07-12 11:25:31 -0700 | [diff] [blame] | 3676 | goto make_bad; |
Filipe Manana | 6771089 | 2016-06-06 11:51:25 +0100 | [diff] [blame] | 3677 | } |
Mark Fasheh | 1748f84 | 2011-07-12 11:25:31 -0700 | [diff] [blame] | 3678 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3679 | memcpy(&location, &BTRFS_I(inode)->location, sizeof(location)); |
Chris Mason | dc17ff8 | 2008-01-08 15:46:30 -0500 | [diff] [blame] | 3680 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3681 | ret = btrfs_lookup_inode(NULL, root, path, &location, 0); |
Filipe Manana | 6771089 | 2016-06-06 11:51:25 +0100 | [diff] [blame] | 3682 | if (ret) { |
| 3683 | if (ret > 0) |
| 3684 | ret = -ENOENT; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3685 | goto make_bad; |
Filipe Manana | 6771089 | 2016-06-06 11:51:25 +0100 | [diff] [blame] | 3686 | } |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3687 | |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 3688 | leaf = path->nodes[0]; |
Miao Xie | 2f7e33d | 2011-06-23 07:27:13 +0000 | [diff] [blame] | 3689 | |
| 3690 | if (filled) |
Miao Xie | 67de117 | 2013-12-26 13:07:06 +0800 | [diff] [blame] | 3691 | goto cache_index; |
Miao Xie | 2f7e33d | 2011-06-23 07:27:13 +0000 | [diff] [blame] | 3692 | |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 3693 | inode_item = btrfs_item_ptr(leaf, path->slots[0], |
| 3694 | struct btrfs_inode_item); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 3695 | inode->i_mode = btrfs_inode_mode(leaf, inode_item); |
Miklos Szeredi | bfe8684 | 2011-10-28 14:13:29 +0200 | [diff] [blame] | 3696 | set_nlink(inode, btrfs_inode_nlink(leaf, inode_item)); |
Eric W. Biederman | 2f2f43d | 2012-02-10 11:05:07 -0800 | [diff] [blame] | 3697 | i_uid_write(inode, btrfs_inode_uid(leaf, inode_item)); |
| 3698 | i_gid_write(inode, btrfs_inode_gid(leaf, inode_item)); |
Chris Mason | dbe674a | 2008-07-17 12:54:05 -0400 | [diff] [blame] | 3699 | btrfs_i_size_write(inode, btrfs_inode_size(leaf, inode_item)); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 3700 | |
David Sterba | a937b97 | 2014-12-12 17:39:12 +0100 | [diff] [blame] | 3701 | inode->i_atime.tv_sec = btrfs_timespec_sec(leaf, &inode_item->atime); |
| 3702 | inode->i_atime.tv_nsec = btrfs_timespec_nsec(leaf, &inode_item->atime); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 3703 | |
David Sterba | a937b97 | 2014-12-12 17:39:12 +0100 | [diff] [blame] | 3704 | inode->i_mtime.tv_sec = btrfs_timespec_sec(leaf, &inode_item->mtime); |
| 3705 | inode->i_mtime.tv_nsec = btrfs_timespec_nsec(leaf, &inode_item->mtime); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 3706 | |
David Sterba | a937b97 | 2014-12-12 17:39:12 +0100 | [diff] [blame] | 3707 | inode->i_ctime.tv_sec = btrfs_timespec_sec(leaf, &inode_item->ctime); |
| 3708 | inode->i_ctime.tv_nsec = btrfs_timespec_nsec(leaf, &inode_item->ctime); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 3709 | |
chandan r | 9cc97d6 | 2012-07-04 12:48:07 +0530 | [diff] [blame] | 3710 | BTRFS_I(inode)->i_otime.tv_sec = |
| 3711 | btrfs_timespec_sec(leaf, &inode_item->otime); |
| 3712 | BTRFS_I(inode)->i_otime.tv_nsec = |
| 3713 | btrfs_timespec_nsec(leaf, &inode_item->otime); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 3714 | |
Yan Zheng | a76a3cd | 2008-10-09 11:46:29 -0400 | [diff] [blame] | 3715 | inode_set_bytes(inode, btrfs_inode_nbytes(leaf, inode_item)); |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 3716 | BTRFS_I(inode)->generation = btrfs_inode_generation(leaf, inode_item); |
Josef Bacik | 5dc562c | 2012-08-17 13:14:17 -0400 | [diff] [blame] | 3717 | BTRFS_I(inode)->last_trans = btrfs_inode_transid(leaf, inode_item); |
| 3718 | |
Josef Bacik | 0c4d2d9 | 2012-04-05 15:03:02 -0400 | [diff] [blame] | 3719 | inode->i_version = btrfs_inode_sequence(leaf, inode_item); |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 3720 | inode->i_generation = BTRFS_I(inode)->generation; |
Josef Bacik | 618e21d | 2007-07-11 10:18:17 -0400 | [diff] [blame] | 3721 | inode->i_rdev = 0; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 3722 | rdev = btrfs_inode_rdev(leaf, inode_item); |
| 3723 | |
Josef Bacik | aec7477 | 2008-07-24 12:12:38 -0400 | [diff] [blame] | 3724 | BTRFS_I(inode)->index_cnt = (u64)-1; |
Yan Zheng | d2fb343 | 2008-12-11 16:30:39 -0500 | [diff] [blame] | 3725 | BTRFS_I(inode)->flags = btrfs_inode_flags(leaf, inode_item); |
Miao Xie | 67de117 | 2013-12-26 13:07:06 +0800 | [diff] [blame] | 3726 | |
| 3727 | cache_index: |
Yang Dongsheng | 6e17d30 | 2015-04-09 12:08:43 +0800 | [diff] [blame] | 3728 | /* |
| 3729 | * If we were modified in the current generation and evicted from memory |
| 3730 | * and then re-read we need to do a full sync since we don't have any |
| 3731 | * idea about which extents were modified before we were evicted from |
| 3732 | * cache. |
| 3733 | * |
| 3734 | * This is required for both inode re-read from disk and delayed inode |
| 3735 | * in delayed_nodes_tree. |
| 3736 | */ |
| 3737 | if (BTRFS_I(inode)->last_trans == root->fs_info->generation) |
| 3738 | set_bit(BTRFS_INODE_NEEDS_FULL_SYNC, |
| 3739 | &BTRFS_I(inode)->runtime_flags); |
| 3740 | |
Filipe Manana | bde6c24 | 2015-07-24 00:00:19 +0100 | [diff] [blame] | 3741 | /* |
| 3742 | * We don't persist the id of the transaction where an unlink operation |
| 3743 | * against the inode was last made. So here we assume the inode might |
| 3744 | * have been evicted, and therefore the exact value of last_unlink_trans |
| 3745 | * lost, and set it to last_trans to avoid metadata inconsistencies |
| 3746 | * between the inode and its parent if the inode is fsync'ed and the log |
| 3747 | * replayed. For example, in the scenario: |
| 3748 | * |
| 3749 | * touch mydir/foo |
| 3750 | * ln mydir/foo mydir/bar |
| 3751 | * sync |
| 3752 | * unlink mydir/bar |
| 3753 | * echo 2 > /proc/sys/vm/drop_caches # evicts inode |
| 3754 | * xfs_io -c fsync mydir/foo |
| 3755 | * <power failure> |
| 3756 | * mount fs, triggers fsync log replay |
| 3757 | * |
| 3758 | * We must make sure that when we fsync our inode foo we also log its |
| 3759 | * parent inode, otherwise after log replay the parent still has the |
| 3760 | * dentry with the "bar" name but our inode foo has a link count of 1 |
| 3761 | * and doesn't have an inode ref with the name "bar" anymore. |
| 3762 | * |
| 3763 | * Setting last_unlink_trans to last_trans is a pessimistic approach, |
Nicholas D Steeves | 0132761 | 2016-05-19 21:18:45 -0400 | [diff] [blame] | 3764 | * but it guarantees correctness at the expense of occasional full |
Filipe Manana | bde6c24 | 2015-07-24 00:00:19 +0100 | [diff] [blame] | 3765 | * transaction commits on fsync if our inode is a directory, or if our |
| 3766 | * inode is not a directory, logging its parent unnecessarily. |
| 3767 | */ |
| 3768 | BTRFS_I(inode)->last_unlink_trans = BTRFS_I(inode)->last_trans; |
| 3769 | |
Miao Xie | 67de117 | 2013-12-26 13:07:06 +0800 | [diff] [blame] | 3770 | path->slots[0]++; |
| 3771 | if (inode->i_nlink != 1 || |
| 3772 | path->slots[0] >= btrfs_header_nritems(leaf)) |
| 3773 | goto cache_acl; |
| 3774 | |
| 3775 | btrfs_item_key_to_cpu(leaf, &location, path->slots[0]); |
| 3776 | if (location.objectid != btrfs_ino(inode)) |
| 3777 | goto cache_acl; |
| 3778 | |
| 3779 | ptr = btrfs_item_ptr_offset(leaf, path->slots[0]); |
| 3780 | if (location.type == BTRFS_INODE_REF_KEY) { |
| 3781 | struct btrfs_inode_ref *ref; |
| 3782 | |
| 3783 | ref = (struct btrfs_inode_ref *)ptr; |
| 3784 | BTRFS_I(inode)->dir_index = btrfs_inode_ref_index(leaf, ref); |
| 3785 | } else if (location.type == BTRFS_INODE_EXTREF_KEY) { |
| 3786 | struct btrfs_inode_extref *extref; |
| 3787 | |
| 3788 | extref = (struct btrfs_inode_extref *)ptr; |
| 3789 | BTRFS_I(inode)->dir_index = btrfs_inode_extref_index(leaf, |
| 3790 | extref); |
| 3791 | } |
Miao Xie | 2f7e33d | 2011-06-23 07:27:13 +0000 | [diff] [blame] | 3792 | cache_acl: |
Chris Mason | 46a53cc | 2009-04-27 11:47:50 -0400 | [diff] [blame] | 3793 | /* |
| 3794 | * try to precache a NULL acl entry for files that don't have |
| 3795 | * any xattrs or acls |
| 3796 | */ |
Li Zefan | 33345d01 | 2011-04-20 10:31:50 +0800 | [diff] [blame] | 3797 | maybe_acls = acls_after_inode_item(leaf, path->slots[0], |
Filipe David Borba Manana | 6354192 | 2014-01-07 11:47:46 +0000 | [diff] [blame] | 3798 | btrfs_ino(inode), &first_xattr_slot); |
| 3799 | if (first_xattr_slot != -1) { |
| 3800 | path->slots[0] = first_xattr_slot; |
| 3801 | ret = btrfs_load_inode_props(inode, path); |
| 3802 | if (ret) |
| 3803 | btrfs_err(root->fs_info, |
David Sterba | 351fd35 | 2014-05-15 16:48:20 +0200 | [diff] [blame] | 3804 | "error loading props for ino %llu (root %llu): %d", |
Filipe David Borba Manana | 6354192 | 2014-01-07 11:47:46 +0000 | [diff] [blame] | 3805 | btrfs_ino(inode), |
| 3806 | root->root_key.objectid, ret); |
| 3807 | } |
| 3808 | btrfs_free_path(path); |
| 3809 | |
Al Viro | 72c0490 | 2009-06-24 16:58:48 -0400 | [diff] [blame] | 3810 | if (!maybe_acls) |
| 3811 | cache_no_acl(inode); |
Chris Mason | 46a53cc | 2009-04-27 11:47:50 -0400 | [diff] [blame] | 3812 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3813 | switch (inode->i_mode & S_IFMT) { |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3814 | case S_IFREG: |
| 3815 | inode->i_mapping->a_ops = &btrfs_aops; |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 3816 | BTRFS_I(inode)->io_tree.ops = &btrfs_extent_io_ops; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3817 | inode->i_fop = &btrfs_file_operations; |
| 3818 | inode->i_op = &btrfs_file_inode_operations; |
| 3819 | break; |
| 3820 | case S_IFDIR: |
| 3821 | inode->i_fop = &btrfs_dir_file_operations; |
| 3822 | if (root == root->fs_info->tree_root) |
| 3823 | inode->i_op = &btrfs_dir_ro_inode_operations; |
| 3824 | else |
| 3825 | inode->i_op = &btrfs_dir_inode_operations; |
| 3826 | break; |
| 3827 | case S_IFLNK: |
| 3828 | inode->i_op = &btrfs_symlink_inode_operations; |
Al Viro | 21fc61c | 2015-11-17 01:07:57 -0500 | [diff] [blame] | 3829 | inode_nohighmem(inode); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3830 | inode->i_mapping->a_ops = &btrfs_symlink_aops; |
| 3831 | break; |
Josef Bacik | 618e21d | 2007-07-11 10:18:17 -0400 | [diff] [blame] | 3832 | default: |
Jim Owens | 0279b4c | 2009-02-04 09:29:13 -0500 | [diff] [blame] | 3833 | inode->i_op = &btrfs_special_inode_operations; |
Josef Bacik | 618e21d | 2007-07-11 10:18:17 -0400 | [diff] [blame] | 3834 | init_special_inode(inode, inode->i_mode, rdev); |
| 3835 | break; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3836 | } |
Christoph Hellwig | 6cbff00 | 2009-04-17 10:37:41 +0200 | [diff] [blame] | 3837 | |
| 3838 | btrfs_update_iflags(inode); |
Filipe Manana | 6771089 | 2016-06-06 11:51:25 +0100 | [diff] [blame] | 3839 | return 0; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3840 | |
| 3841 | make_bad: |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3842 | btrfs_free_path(path); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3843 | make_bad_inode(inode); |
Filipe Manana | 6771089 | 2016-06-06 11:51:25 +0100 | [diff] [blame] | 3844 | return ret; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3845 | } |
| 3846 | |
Chris Mason | d352ac6 | 2008-09-29 15:18:18 -0400 | [diff] [blame] | 3847 | /* |
| 3848 | * given a leaf and an inode, copy the inode fields into the leaf |
| 3849 | */ |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 3850 | static void fill_inode_item(struct btrfs_trans_handle *trans, |
| 3851 | struct extent_buffer *leaf, |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 3852 | struct btrfs_inode_item *item, |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3853 | struct inode *inode) |
| 3854 | { |
Liu Bo | 51fab69 | 2012-12-27 09:01:21 +0000 | [diff] [blame] | 3855 | struct btrfs_map_token token; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 3856 | |
Liu Bo | 51fab69 | 2012-12-27 09:01:21 +0000 | [diff] [blame] | 3857 | btrfs_init_map_token(&token); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 3858 | |
Liu Bo | 51fab69 | 2012-12-27 09:01:21 +0000 | [diff] [blame] | 3859 | btrfs_set_token_inode_uid(leaf, item, i_uid_read(inode), &token); |
| 3860 | btrfs_set_token_inode_gid(leaf, item, i_gid_read(inode), &token); |
| 3861 | btrfs_set_token_inode_size(leaf, item, BTRFS_I(inode)->disk_i_size, |
| 3862 | &token); |
| 3863 | btrfs_set_token_inode_mode(leaf, item, inode->i_mode, &token); |
| 3864 | btrfs_set_token_inode_nlink(leaf, item, inode->i_nlink, &token); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 3865 | |
David Sterba | a937b97 | 2014-12-12 17:39:12 +0100 | [diff] [blame] | 3866 | btrfs_set_token_timespec_sec(leaf, &item->atime, |
Liu Bo | 51fab69 | 2012-12-27 09:01:21 +0000 | [diff] [blame] | 3867 | inode->i_atime.tv_sec, &token); |
David Sterba | a937b97 | 2014-12-12 17:39:12 +0100 | [diff] [blame] | 3868 | btrfs_set_token_timespec_nsec(leaf, &item->atime, |
Liu Bo | 51fab69 | 2012-12-27 09:01:21 +0000 | [diff] [blame] | 3869 | inode->i_atime.tv_nsec, &token); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 3870 | |
David Sterba | a937b97 | 2014-12-12 17:39:12 +0100 | [diff] [blame] | 3871 | btrfs_set_token_timespec_sec(leaf, &item->mtime, |
Liu Bo | 51fab69 | 2012-12-27 09:01:21 +0000 | [diff] [blame] | 3872 | inode->i_mtime.tv_sec, &token); |
David Sterba | a937b97 | 2014-12-12 17:39:12 +0100 | [diff] [blame] | 3873 | btrfs_set_token_timespec_nsec(leaf, &item->mtime, |
Liu Bo | 51fab69 | 2012-12-27 09:01:21 +0000 | [diff] [blame] | 3874 | inode->i_mtime.tv_nsec, &token); |
| 3875 | |
David Sterba | a937b97 | 2014-12-12 17:39:12 +0100 | [diff] [blame] | 3876 | btrfs_set_token_timespec_sec(leaf, &item->ctime, |
Liu Bo | 51fab69 | 2012-12-27 09:01:21 +0000 | [diff] [blame] | 3877 | inode->i_ctime.tv_sec, &token); |
David Sterba | a937b97 | 2014-12-12 17:39:12 +0100 | [diff] [blame] | 3878 | btrfs_set_token_timespec_nsec(leaf, &item->ctime, |
Liu Bo | 51fab69 | 2012-12-27 09:01:21 +0000 | [diff] [blame] | 3879 | inode->i_ctime.tv_nsec, &token); |
| 3880 | |
chandan r | 9cc97d6 | 2012-07-04 12:48:07 +0530 | [diff] [blame] | 3881 | btrfs_set_token_timespec_sec(leaf, &item->otime, |
| 3882 | BTRFS_I(inode)->i_otime.tv_sec, &token); |
| 3883 | btrfs_set_token_timespec_nsec(leaf, &item->otime, |
| 3884 | BTRFS_I(inode)->i_otime.tv_nsec, &token); |
| 3885 | |
Liu Bo | 51fab69 | 2012-12-27 09:01:21 +0000 | [diff] [blame] | 3886 | btrfs_set_token_inode_nbytes(leaf, item, inode_get_bytes(inode), |
| 3887 | &token); |
| 3888 | btrfs_set_token_inode_generation(leaf, item, BTRFS_I(inode)->generation, |
| 3889 | &token); |
| 3890 | btrfs_set_token_inode_sequence(leaf, item, inode->i_version, &token); |
| 3891 | btrfs_set_token_inode_transid(leaf, item, trans->transid, &token); |
| 3892 | btrfs_set_token_inode_rdev(leaf, item, inode->i_rdev, &token); |
| 3893 | btrfs_set_token_inode_flags(leaf, item, BTRFS_I(inode)->flags, &token); |
| 3894 | btrfs_set_token_inode_block_group(leaf, item, 0, &token); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3895 | } |
| 3896 | |
Chris Mason | d352ac6 | 2008-09-29 15:18:18 -0400 | [diff] [blame] | 3897 | /* |
| 3898 | * copy everything in the in-memory inode into the btree. |
| 3899 | */ |
Chris Mason | 2115133 | 2011-11-10 20:39:08 -0500 | [diff] [blame] | 3900 | static noinline int btrfs_update_inode_item(struct btrfs_trans_handle *trans, |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 3901 | struct btrfs_root *root, struct inode *inode) |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3902 | { |
| 3903 | struct btrfs_inode_item *inode_item; |
| 3904 | struct btrfs_path *path; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 3905 | struct extent_buffer *leaf; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3906 | int ret; |
| 3907 | |
| 3908 | path = btrfs_alloc_path(); |
Miao Xie | 16cdcec | 2011-04-22 18:12:22 +0800 | [diff] [blame] | 3909 | if (!path) |
| 3910 | return -ENOMEM; |
| 3911 | |
Chris Mason | b947343 | 2009-03-13 11:00:37 -0400 | [diff] [blame] | 3912 | path->leave_spinning = 1; |
Miao Xie | 16cdcec | 2011-04-22 18:12:22 +0800 | [diff] [blame] | 3913 | ret = btrfs_lookup_inode(trans, root, path, &BTRFS_I(inode)->location, |
| 3914 | 1); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3915 | if (ret) { |
| 3916 | if (ret > 0) |
| 3917 | ret = -ENOENT; |
| 3918 | goto failed; |
| 3919 | } |
| 3920 | |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 3921 | leaf = path->nodes[0]; |
| 3922 | inode_item = btrfs_item_ptr(leaf, path->slots[0], |
Miao Xie | 16cdcec | 2011-04-22 18:12:22 +0800 | [diff] [blame] | 3923 | struct btrfs_inode_item); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3924 | |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 3925 | fill_inode_item(trans, leaf, inode_item, inode); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 3926 | btrfs_mark_buffer_dirty(leaf); |
Josef Bacik | 15ee9bc | 2007-08-10 16:22:09 -0400 | [diff] [blame] | 3927 | btrfs_set_inode_last_trans(trans, inode); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3928 | ret = 0; |
| 3929 | failed: |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3930 | btrfs_free_path(path); |
| 3931 | return ret; |
| 3932 | } |
| 3933 | |
Chris Mason | d352ac6 | 2008-09-29 15:18:18 -0400 | [diff] [blame] | 3934 | /* |
Chris Mason | 2115133 | 2011-11-10 20:39:08 -0500 | [diff] [blame] | 3935 | * copy everything in the in-memory inode into the btree. |
| 3936 | */ |
| 3937 | noinline int btrfs_update_inode(struct btrfs_trans_handle *trans, |
| 3938 | struct btrfs_root *root, struct inode *inode) |
| 3939 | { |
| 3940 | int ret; |
| 3941 | |
| 3942 | /* |
| 3943 | * If the inode is a free space inode, we can deadlock during commit |
| 3944 | * if we put it into the delayed code. |
| 3945 | * |
| 3946 | * The data relocation inode should also be directly updated |
| 3947 | * without delay |
| 3948 | */ |
Liu Bo | 83eea1f | 2012-07-10 05:28:39 -0600 | [diff] [blame] | 3949 | if (!btrfs_is_free_space_inode(inode) |
Josef Bacik | 1d52c78 | 2014-09-18 11:30:44 -0400 | [diff] [blame] | 3950 | && root->root_key.objectid != BTRFS_DATA_RELOC_TREE_OBJECTID |
Josef Bacik | afcdd12 | 2016-09-02 15:40:02 -0400 | [diff] [blame] | 3951 | && !test_bit(BTRFS_FS_LOG_RECOVERING, &root->fs_info->flags)) { |
Alexander Block | 8ea05e3 | 2012-07-25 17:35:53 +0200 | [diff] [blame] | 3952 | btrfs_update_root_times(trans, root); |
| 3953 | |
Chris Mason | 2115133 | 2011-11-10 20:39:08 -0500 | [diff] [blame] | 3954 | ret = btrfs_delayed_update_inode(trans, root, inode); |
| 3955 | if (!ret) |
| 3956 | btrfs_set_inode_last_trans(trans, inode); |
| 3957 | return ret; |
| 3958 | } |
| 3959 | |
| 3960 | return btrfs_update_inode_item(trans, root, inode); |
| 3961 | } |
| 3962 | |
Josef Bacik | be6aef6 | 2012-10-22 15:43:12 -0400 | [diff] [blame] | 3963 | noinline int btrfs_update_inode_fallback(struct btrfs_trans_handle *trans, |
| 3964 | struct btrfs_root *root, |
| 3965 | struct inode *inode) |
Chris Mason | 2115133 | 2011-11-10 20:39:08 -0500 | [diff] [blame] | 3966 | { |
| 3967 | int ret; |
| 3968 | |
| 3969 | ret = btrfs_update_inode(trans, root, inode); |
| 3970 | if (ret == -ENOSPC) |
| 3971 | return btrfs_update_inode_item(trans, root, inode); |
| 3972 | return ret; |
| 3973 | } |
| 3974 | |
| 3975 | /* |
Chris Mason | d352ac6 | 2008-09-29 15:18:18 -0400 | [diff] [blame] | 3976 | * unlink helper that gets used here in inode.c and in the tree logging |
| 3977 | * recovery code. It remove a link in a directory with a given name, and |
| 3978 | * also drops the back refs in the inode to the directory |
| 3979 | */ |
Al Viro | 9298679 | 2011-03-04 17:14:37 +0000 | [diff] [blame] | 3980 | static int __btrfs_unlink_inode(struct btrfs_trans_handle *trans, |
| 3981 | struct btrfs_root *root, |
| 3982 | struct inode *dir, struct inode *inode, |
| 3983 | const char *name, int name_len) |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3984 | { |
| 3985 | struct btrfs_path *path; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3986 | int ret = 0; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 3987 | struct extent_buffer *leaf; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3988 | struct btrfs_dir_item *di; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 3989 | struct btrfs_key key; |
Josef Bacik | aec7477 | 2008-07-24 12:12:38 -0400 | [diff] [blame] | 3990 | u64 index; |
Li Zefan | 33345d01 | 2011-04-20 10:31:50 +0800 | [diff] [blame] | 3991 | u64 ino = btrfs_ino(inode); |
| 3992 | u64 dir_ino = btrfs_ino(dir); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3993 | |
| 3994 | path = btrfs_alloc_path(); |
Chris Mason | 54aa1f4 | 2007-06-22 14:16:25 -0400 | [diff] [blame] | 3995 | if (!path) { |
| 3996 | ret = -ENOMEM; |
Tsutomu Itoh | 554233a | 2011-02-03 03:16:25 +0000 | [diff] [blame] | 3997 | goto out; |
Chris Mason | 54aa1f4 | 2007-06-22 14:16:25 -0400 | [diff] [blame] | 3998 | } |
| 3999 | |
Chris Mason | b947343 | 2009-03-13 11:00:37 -0400 | [diff] [blame] | 4000 | path->leave_spinning = 1; |
Li Zefan | 33345d01 | 2011-04-20 10:31:50 +0800 | [diff] [blame] | 4001 | di = btrfs_lookup_dir_item(trans, root, path, dir_ino, |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4002 | name, name_len, -1); |
| 4003 | if (IS_ERR(di)) { |
| 4004 | ret = PTR_ERR(di); |
| 4005 | goto err; |
| 4006 | } |
| 4007 | if (!di) { |
| 4008 | ret = -ENOENT; |
| 4009 | goto err; |
| 4010 | } |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 4011 | leaf = path->nodes[0]; |
| 4012 | btrfs_dir_item_key_to_cpu(leaf, di, &key); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4013 | ret = btrfs_delete_one_dir_name(trans, root, path, di); |
Chris Mason | 54aa1f4 | 2007-06-22 14:16:25 -0400 | [diff] [blame] | 4014 | if (ret) |
| 4015 | goto err; |
David Sterba | b3b4aa7 | 2011-04-21 01:20:15 +0200 | [diff] [blame] | 4016 | btrfs_release_path(path); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4017 | |
Miao Xie | 67de117 | 2013-12-26 13:07:06 +0800 | [diff] [blame] | 4018 | /* |
| 4019 | * If we don't have dir index, we have to get it by looking up |
| 4020 | * the inode ref, since we get the inode ref, remove it directly, |
| 4021 | * it is unnecessary to do delayed deletion. |
| 4022 | * |
| 4023 | * But if we have dir index, needn't search inode ref to get it. |
| 4024 | * Since the inode ref is close to the inode item, it is better |
| 4025 | * that we delay to delete it, and just do this deletion when |
| 4026 | * we update the inode item. |
| 4027 | */ |
| 4028 | if (BTRFS_I(inode)->dir_index) { |
| 4029 | ret = btrfs_delayed_delete_inode_ref(inode); |
| 4030 | if (!ret) { |
| 4031 | index = BTRFS_I(inode)->dir_index; |
| 4032 | goto skip_backref; |
| 4033 | } |
| 4034 | } |
| 4035 | |
Li Zefan | 33345d01 | 2011-04-20 10:31:50 +0800 | [diff] [blame] | 4036 | ret = btrfs_del_inode_ref(trans, root, name, name_len, ino, |
| 4037 | dir_ino, &index); |
Josef Bacik | aec7477 | 2008-07-24 12:12:38 -0400 | [diff] [blame] | 4038 | if (ret) { |
Simon Kirby | c2cf52e | 2013-03-19 22:41:23 +0000 | [diff] [blame] | 4039 | btrfs_info(root->fs_info, |
| 4040 | "failed to delete reference to %.*s, inode %llu parent %llu", |
Geert Uytterhoeven | c1c9ff7 | 2013-08-20 13:20:07 +0200 | [diff] [blame] | 4041 | name_len, name, ino, dir_ino); |
Jeff Mahoney | 6664283 | 2016-06-10 18:19:25 -0400 | [diff] [blame] | 4042 | btrfs_abort_transaction(trans, ret); |
Josef Bacik | aec7477 | 2008-07-24 12:12:38 -0400 | [diff] [blame] | 4043 | goto err; |
| 4044 | } |
Miao Xie | 67de117 | 2013-12-26 13:07:06 +0800 | [diff] [blame] | 4045 | skip_backref: |
Miao Xie | 16cdcec | 2011-04-22 18:12:22 +0800 | [diff] [blame] | 4046 | ret = btrfs_delete_delayed_dir_index(trans, root, dir, index); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 4047 | if (ret) { |
Jeff Mahoney | 6664283 | 2016-06-10 18:19:25 -0400 | [diff] [blame] | 4048 | btrfs_abort_transaction(trans, ret); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4049 | goto err; |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 4050 | } |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4051 | |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 4052 | ret = btrfs_del_inode_ref_in_log(trans, root, name, name_len, |
Li Zefan | 33345d01 | 2011-04-20 10:31:50 +0800 | [diff] [blame] | 4053 | inode, dir_ino); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 4054 | if (ret != 0 && ret != -ENOENT) { |
Jeff Mahoney | 6664283 | 2016-06-10 18:19:25 -0400 | [diff] [blame] | 4055 | btrfs_abort_transaction(trans, ret); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 4056 | goto err; |
| 4057 | } |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 4058 | |
| 4059 | ret = btrfs_del_dir_entries_in_log(trans, root, name, name_len, |
| 4060 | dir, index); |
Chris Mason | 6418c96 | 2010-10-30 07:34:24 -0400 | [diff] [blame] | 4061 | if (ret == -ENOENT) |
| 4062 | ret = 0; |
Zach Brown | d4e3991 | 2013-04-02 21:02:16 +0000 | [diff] [blame] | 4063 | else if (ret) |
Jeff Mahoney | 6664283 | 2016-06-10 18:19:25 -0400 | [diff] [blame] | 4064 | btrfs_abort_transaction(trans, ret); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4065 | err: |
| 4066 | btrfs_free_path(path); |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 4067 | if (ret) |
| 4068 | goto out; |
| 4069 | |
| 4070 | btrfs_i_size_write(dir, dir->i_size - name_len * 2); |
Josef Bacik | 0c4d2d9 | 2012-04-05 15:03:02 -0400 | [diff] [blame] | 4071 | inode_inc_iversion(inode); |
| 4072 | inode_inc_iversion(dir); |
Deepa Dinamani | 04b285f | 2016-02-06 23:57:21 -0800 | [diff] [blame] | 4073 | inode->i_ctime = dir->i_mtime = |
Deepa Dinamani | c2050a4 | 2016-09-14 07:48:06 -0700 | [diff] [blame] | 4074 | dir->i_ctime = current_time(inode); |
Tsutomu Itoh | b995929 | 2012-06-25 21:25:22 -0600 | [diff] [blame] | 4075 | ret = btrfs_update_inode(trans, root, dir); |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 4076 | out: |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4077 | return ret; |
| 4078 | } |
| 4079 | |
Al Viro | 9298679 | 2011-03-04 17:14:37 +0000 | [diff] [blame] | 4080 | int btrfs_unlink_inode(struct btrfs_trans_handle *trans, |
| 4081 | struct btrfs_root *root, |
| 4082 | struct inode *dir, struct inode *inode, |
| 4083 | const char *name, int name_len) |
| 4084 | { |
| 4085 | int ret; |
| 4086 | ret = __btrfs_unlink_inode(trans, root, dir, inode, name, name_len); |
| 4087 | if (!ret) { |
Zach Brown | 8b558c5 | 2013-10-16 12:10:34 -0700 | [diff] [blame] | 4088 | drop_nlink(inode); |
Al Viro | 9298679 | 2011-03-04 17:14:37 +0000 | [diff] [blame] | 4089 | ret = btrfs_update_inode(trans, root, inode); |
| 4090 | } |
| 4091 | return ret; |
| 4092 | } |
Yan, Zheng | a22285a | 2010-05-16 10:48:46 -0400 | [diff] [blame] | 4093 | |
| 4094 | /* |
| 4095 | * helper to start transaction for unlink and rmdir. |
| 4096 | * |
Josef Bacik | d52be81 | 2013-05-29 14:54:47 -0400 | [diff] [blame] | 4097 | * unlink and rmdir are special in btrfs, they do not always free space, so |
| 4098 | * if we cannot make our reservations the normal way try and see if there is |
| 4099 | * plenty of slack room in the global reserve to migrate, otherwise we cannot |
| 4100 | * allow the unlink to occur. |
Yan, Zheng | a22285a | 2010-05-16 10:48:46 -0400 | [diff] [blame] | 4101 | */ |
Josef Bacik | d52be81 | 2013-05-29 14:54:47 -0400 | [diff] [blame] | 4102 | static struct btrfs_trans_handle *__unlink_start_trans(struct inode *dir) |
Yan, Zheng | a22285a | 2010-05-16 10:48:46 -0400 | [diff] [blame] | 4103 | { |
Yan, Zheng | a22285a | 2010-05-16 10:48:46 -0400 | [diff] [blame] | 4104 | struct btrfs_root *root = BTRFS_I(dir)->root; |
Yan, Zheng | a22285a | 2010-05-16 10:48:46 -0400 | [diff] [blame] | 4105 | |
Josef Bacik | e70bea5 | 2011-10-11 14:18:24 -0400 | [diff] [blame] | 4106 | /* |
| 4107 | * 1 for the possible orphan item |
| 4108 | * 1 for the dir item |
| 4109 | * 1 for the dir index |
| 4110 | * 1 for the inode ref |
Josef Bacik | e70bea5 | 2011-10-11 14:18:24 -0400 | [diff] [blame] | 4111 | * 1 for the inode |
| 4112 | */ |
Filipe Manana | 8eab77f | 2015-11-13 23:57:16 +0000 | [diff] [blame] | 4113 | return btrfs_start_transaction_fallback_global_rsv(root, 5, 5); |
Yan, Zheng | a22285a | 2010-05-16 10:48:46 -0400 | [diff] [blame] | 4114 | } |
| 4115 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4116 | static int btrfs_unlink(struct inode *dir, struct dentry *dentry) |
| 4117 | { |
Yan, Zheng | a22285a | 2010-05-16 10:48:46 -0400 | [diff] [blame] | 4118 | struct btrfs_root *root = BTRFS_I(dir)->root; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4119 | struct btrfs_trans_handle *trans; |
David Howells | 2b0143b | 2015-03-17 22:25:59 +0000 | [diff] [blame] | 4120 | struct inode *inode = d_inode(dentry); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4121 | int ret; |
| 4122 | |
Josef Bacik | d52be81 | 2013-05-29 14:54:47 -0400 | [diff] [blame] | 4123 | trans = __unlink_start_trans(dir); |
Yan, Zheng | a22285a | 2010-05-16 10:48:46 -0400 | [diff] [blame] | 4124 | if (IS_ERR(trans)) |
Josef Bacik | 5df6a9f | 2009-11-10 21:23:48 -0500 | [diff] [blame] | 4125 | return PTR_ERR(trans); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 4126 | |
David Howells | 2b0143b | 2015-03-17 22:25:59 +0000 | [diff] [blame] | 4127 | btrfs_record_unlink_dir(trans, dir, d_inode(dentry), 0); |
Chris Mason | 12fcfd2 | 2009-03-24 10:24:20 -0400 | [diff] [blame] | 4128 | |
David Howells | 2b0143b | 2015-03-17 22:25:59 +0000 | [diff] [blame] | 4129 | ret = btrfs_unlink_inode(trans, root, dir, d_inode(dentry), |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 4130 | dentry->d_name.name, dentry->d_name.len); |
Tsutomu Itoh | b532402 | 2011-07-19 07:27:20 +0000 | [diff] [blame] | 4131 | if (ret) |
| 4132 | goto out; |
Josef Bacik | 7b12876 | 2008-07-24 12:17:14 -0400 | [diff] [blame] | 4133 | |
Yan, Zheng | a22285a | 2010-05-16 10:48:46 -0400 | [diff] [blame] | 4134 | if (inode->i_nlink == 0) { |
Josef Bacik | 7b12876 | 2008-07-24 12:17:14 -0400 | [diff] [blame] | 4135 | ret = btrfs_orphan_add(trans, inode); |
Tsutomu Itoh | b532402 | 2011-07-19 07:27:20 +0000 | [diff] [blame] | 4136 | if (ret) |
| 4137 | goto out; |
Yan, Zheng | a22285a | 2010-05-16 10:48:46 -0400 | [diff] [blame] | 4138 | } |
Josef Bacik | 7b12876 | 2008-07-24 12:17:14 -0400 | [diff] [blame] | 4139 | |
Tsutomu Itoh | b532402 | 2011-07-19 07:27:20 +0000 | [diff] [blame] | 4140 | out: |
Josef Bacik | d52be81 | 2013-05-29 14:54:47 -0400 | [diff] [blame] | 4141 | btrfs_end_transaction(trans, root); |
Liu Bo | b53d3f5 | 2012-11-14 14:34:34 +0000 | [diff] [blame] | 4142 | btrfs_btree_balance_dirty(root); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4143 | return ret; |
| 4144 | } |
| 4145 | |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 4146 | int btrfs_unlink_subvol(struct btrfs_trans_handle *trans, |
| 4147 | struct btrfs_root *root, |
| 4148 | struct inode *dir, u64 objectid, |
| 4149 | const char *name, int name_len) |
| 4150 | { |
| 4151 | struct btrfs_path *path; |
| 4152 | struct extent_buffer *leaf; |
| 4153 | struct btrfs_dir_item *di; |
| 4154 | struct btrfs_key key; |
| 4155 | u64 index; |
| 4156 | int ret; |
Li Zefan | 33345d01 | 2011-04-20 10:31:50 +0800 | [diff] [blame] | 4157 | u64 dir_ino = btrfs_ino(dir); |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 4158 | |
| 4159 | path = btrfs_alloc_path(); |
| 4160 | if (!path) |
| 4161 | return -ENOMEM; |
| 4162 | |
Li Zefan | 33345d01 | 2011-04-20 10:31:50 +0800 | [diff] [blame] | 4163 | di = btrfs_lookup_dir_item(trans, root, path, dir_ino, |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 4164 | name, name_len, -1); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 4165 | if (IS_ERR_OR_NULL(di)) { |
| 4166 | if (!di) |
| 4167 | ret = -ENOENT; |
| 4168 | else |
| 4169 | ret = PTR_ERR(di); |
| 4170 | goto out; |
| 4171 | } |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 4172 | |
| 4173 | leaf = path->nodes[0]; |
| 4174 | btrfs_dir_item_key_to_cpu(leaf, di, &key); |
| 4175 | WARN_ON(key.type != BTRFS_ROOT_ITEM_KEY || key.objectid != objectid); |
| 4176 | ret = btrfs_delete_one_dir_name(trans, root, path, di); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 4177 | if (ret) { |
Jeff Mahoney | 6664283 | 2016-06-10 18:19:25 -0400 | [diff] [blame] | 4178 | btrfs_abort_transaction(trans, ret); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 4179 | goto out; |
| 4180 | } |
David Sterba | b3b4aa7 | 2011-04-21 01:20:15 +0200 | [diff] [blame] | 4181 | btrfs_release_path(path); |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 4182 | |
| 4183 | ret = btrfs_del_root_ref(trans, root->fs_info->tree_root, |
| 4184 | objectid, root->root_key.objectid, |
Li Zefan | 33345d01 | 2011-04-20 10:31:50 +0800 | [diff] [blame] | 4185 | dir_ino, &index, name, name_len); |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 4186 | if (ret < 0) { |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 4187 | if (ret != -ENOENT) { |
Jeff Mahoney | 6664283 | 2016-06-10 18:19:25 -0400 | [diff] [blame] | 4188 | btrfs_abort_transaction(trans, ret); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 4189 | goto out; |
| 4190 | } |
Li Zefan | 33345d01 | 2011-04-20 10:31:50 +0800 | [diff] [blame] | 4191 | di = btrfs_search_dir_index_item(root, path, dir_ino, |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 4192 | name, name_len); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 4193 | if (IS_ERR_OR_NULL(di)) { |
| 4194 | if (!di) |
| 4195 | ret = -ENOENT; |
| 4196 | else |
| 4197 | ret = PTR_ERR(di); |
Jeff Mahoney | 6664283 | 2016-06-10 18:19:25 -0400 | [diff] [blame] | 4198 | btrfs_abort_transaction(trans, ret); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 4199 | goto out; |
| 4200 | } |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 4201 | |
| 4202 | leaf = path->nodes[0]; |
| 4203 | btrfs_item_key_to_cpu(leaf, &key, path->slots[0]); |
David Sterba | b3b4aa7 | 2011-04-21 01:20:15 +0200 | [diff] [blame] | 4204 | btrfs_release_path(path); |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 4205 | index = key.offset; |
| 4206 | } |
David Sterba | b3b4aa7 | 2011-04-21 01:20:15 +0200 | [diff] [blame] | 4207 | btrfs_release_path(path); |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 4208 | |
Miao Xie | 16cdcec | 2011-04-22 18:12:22 +0800 | [diff] [blame] | 4209 | ret = btrfs_delete_delayed_dir_index(trans, root, dir, index); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 4210 | if (ret) { |
Jeff Mahoney | 6664283 | 2016-06-10 18:19:25 -0400 | [diff] [blame] | 4211 | btrfs_abort_transaction(trans, ret); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 4212 | goto out; |
| 4213 | } |
Miao Xie | 16cdcec | 2011-04-22 18:12:22 +0800 | [diff] [blame] | 4214 | |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 4215 | btrfs_i_size_write(dir, dir->i_size - name_len * 2); |
Josef Bacik | 0c4d2d9 | 2012-04-05 15:03:02 -0400 | [diff] [blame] | 4216 | inode_inc_iversion(dir); |
Deepa Dinamani | c2050a4 | 2016-09-14 07:48:06 -0700 | [diff] [blame] | 4217 | dir->i_mtime = dir->i_ctime = current_time(dir); |
Josef Bacik | 5a24e84 | 2012-08-08 10:12:59 -0600 | [diff] [blame] | 4218 | ret = btrfs_update_inode_fallback(trans, root, dir); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 4219 | if (ret) |
Jeff Mahoney | 6664283 | 2016-06-10 18:19:25 -0400 | [diff] [blame] | 4220 | btrfs_abort_transaction(trans, ret); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 4221 | out: |
Josef Bacik | 71d7aed | 2011-06-14 14:24:32 -0400 | [diff] [blame] | 4222 | btrfs_free_path(path); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 4223 | return ret; |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 4224 | } |
| 4225 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4226 | static int btrfs_rmdir(struct inode *dir, struct dentry *dentry) |
| 4227 | { |
David Howells | 2b0143b | 2015-03-17 22:25:59 +0000 | [diff] [blame] | 4228 | struct inode *inode = d_inode(dentry); |
Chris Mason | 1832a6d | 2007-12-21 16:27:21 -0500 | [diff] [blame] | 4229 | int err = 0; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4230 | struct btrfs_root *root = BTRFS_I(dir)->root; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4231 | struct btrfs_trans_handle *trans; |
Filipe Manana | 44f714d | 2016-06-06 16:11:13 +0100 | [diff] [blame] | 4232 | u64 last_unlink_trans; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4233 | |
David Sterba | b3ae244 | 2012-09-13 16:04:34 -0600 | [diff] [blame] | 4234 | if (inode->i_size > BTRFS_EMPTY_DIR_SIZE) |
Yan | 134d451 | 2007-10-25 15:49:25 -0400 | [diff] [blame] | 4235 | return -ENOTEMPTY; |
David Sterba | b3ae244 | 2012-09-13 16:04:34 -0600 | [diff] [blame] | 4236 | if (btrfs_ino(inode) == BTRFS_FIRST_FREE_OBJECTID) |
| 4237 | return -EPERM; |
Yan | 134d451 | 2007-10-25 15:49:25 -0400 | [diff] [blame] | 4238 | |
Josef Bacik | d52be81 | 2013-05-29 14:54:47 -0400 | [diff] [blame] | 4239 | trans = __unlink_start_trans(dir); |
Yan, Zheng | a22285a | 2010-05-16 10:48:46 -0400 | [diff] [blame] | 4240 | if (IS_ERR(trans)) |
Josef Bacik | 5df6a9f | 2009-11-10 21:23:48 -0500 | [diff] [blame] | 4241 | return PTR_ERR(trans); |
Josef Bacik | 5df6a9f | 2009-11-10 21:23:48 -0500 | [diff] [blame] | 4242 | |
Li Zefan | 33345d01 | 2011-04-20 10:31:50 +0800 | [diff] [blame] | 4243 | if (unlikely(btrfs_ino(inode) == BTRFS_EMPTY_SUBVOL_DIR_OBJECTID)) { |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 4244 | err = btrfs_unlink_subvol(trans, root, dir, |
| 4245 | BTRFS_I(inode)->location.objectid, |
| 4246 | dentry->d_name.name, |
| 4247 | dentry->d_name.len); |
| 4248 | goto out; |
| 4249 | } |
| 4250 | |
Josef Bacik | 7b12876 | 2008-07-24 12:17:14 -0400 | [diff] [blame] | 4251 | err = btrfs_orphan_add(trans, inode); |
| 4252 | if (err) |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 4253 | goto out; |
Josef Bacik | 7b12876 | 2008-07-24 12:17:14 -0400 | [diff] [blame] | 4254 | |
Filipe Manana | 44f714d | 2016-06-06 16:11:13 +0100 | [diff] [blame] | 4255 | last_unlink_trans = BTRFS_I(inode)->last_unlink_trans; |
| 4256 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4257 | /* now the directory is empty */ |
David Howells | 2b0143b | 2015-03-17 22:25:59 +0000 | [diff] [blame] | 4258 | err = btrfs_unlink_inode(trans, root, dir, d_inode(dentry), |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 4259 | dentry->d_name.name, dentry->d_name.len); |
Filipe Manana | 44f714d | 2016-06-06 16:11:13 +0100 | [diff] [blame] | 4260 | if (!err) { |
Chris Mason | dbe674a | 2008-07-17 12:54:05 -0400 | [diff] [blame] | 4261 | btrfs_i_size_write(inode, 0); |
Filipe Manana | 44f714d | 2016-06-06 16:11:13 +0100 | [diff] [blame] | 4262 | /* |
| 4263 | * Propagate the last_unlink_trans value of the deleted dir to |
| 4264 | * its parent directory. This is to prevent an unrecoverable |
| 4265 | * log tree in the case we do something like this: |
| 4266 | * 1) create dir foo |
| 4267 | * 2) create snapshot under dir foo |
| 4268 | * 3) delete the snapshot |
| 4269 | * 4) rmdir foo |
| 4270 | * 5) mkdir foo |
| 4271 | * 6) fsync foo or some file inside foo |
| 4272 | */ |
| 4273 | if (last_unlink_trans >= trans->transid) |
| 4274 | BTRFS_I(dir)->last_unlink_trans = last_unlink_trans; |
| 4275 | } |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 4276 | out: |
Josef Bacik | d52be81 | 2013-05-29 14:54:47 -0400 | [diff] [blame] | 4277 | btrfs_end_transaction(trans, root); |
Liu Bo | b53d3f5 | 2012-11-14 14:34:34 +0000 | [diff] [blame] | 4278 | btrfs_btree_balance_dirty(root); |
Chris Mason | 3954401 | 2007-12-12 14:38:19 -0500 | [diff] [blame] | 4279 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4280 | return err; |
| 4281 | } |
| 4282 | |
Chris Mason | 28f75a0 | 2015-02-04 06:59:29 -0800 | [diff] [blame] | 4283 | static int truncate_space_check(struct btrfs_trans_handle *trans, |
| 4284 | struct btrfs_root *root, |
| 4285 | u64 bytes_deleted) |
| 4286 | { |
| 4287 | int ret; |
| 4288 | |
Josef Bacik | dc95f7b | 2016-01-13 11:48:06 -0500 | [diff] [blame] | 4289 | /* |
| 4290 | * This is only used to apply pressure to the enospc system, we don't |
| 4291 | * intend to use this reservation at all. |
| 4292 | */ |
Chris Mason | 28f75a0 | 2015-02-04 06:59:29 -0800 | [diff] [blame] | 4293 | bytes_deleted = btrfs_csum_bytes_to_leaves(root, bytes_deleted); |
Josef Bacik | dc95f7b | 2016-01-13 11:48:06 -0500 | [diff] [blame] | 4294 | bytes_deleted *= root->nodesize; |
Chris Mason | 28f75a0 | 2015-02-04 06:59:29 -0800 | [diff] [blame] | 4295 | ret = btrfs_block_rsv_add(root, &root->fs_info->trans_block_rsv, |
| 4296 | bytes_deleted, BTRFS_RESERVE_NO_FLUSH); |
Josef Bacik | dc95f7b | 2016-01-13 11:48:06 -0500 | [diff] [blame] | 4297 | if (!ret) { |
| 4298 | trace_btrfs_space_reservation(root->fs_info, "transaction", |
| 4299 | trans->transid, |
| 4300 | bytes_deleted, 1); |
Chris Mason | 28f75a0 | 2015-02-04 06:59:29 -0800 | [diff] [blame] | 4301 | trans->bytes_reserved += bytes_deleted; |
Josef Bacik | dc95f7b | 2016-01-13 11:48:06 -0500 | [diff] [blame] | 4302 | } |
Chris Mason | 28f75a0 | 2015-02-04 06:59:29 -0800 | [diff] [blame] | 4303 | return ret; |
| 4304 | |
| 4305 | } |
| 4306 | |
Filipe Manana | 0305cd5 | 2015-10-16 12:34:25 +0100 | [diff] [blame] | 4307 | static int truncate_inline_extent(struct inode *inode, |
| 4308 | struct btrfs_path *path, |
| 4309 | struct btrfs_key *found_key, |
| 4310 | const u64 item_end, |
| 4311 | const u64 new_size) |
| 4312 | { |
| 4313 | struct extent_buffer *leaf = path->nodes[0]; |
| 4314 | int slot = path->slots[0]; |
| 4315 | struct btrfs_file_extent_item *fi; |
| 4316 | u32 size = (u32)(new_size - found_key->offset); |
| 4317 | struct btrfs_root *root = BTRFS_I(inode)->root; |
| 4318 | |
| 4319 | fi = btrfs_item_ptr(leaf, slot, struct btrfs_file_extent_item); |
| 4320 | |
| 4321 | if (btrfs_file_extent_compression(leaf, fi) != BTRFS_COMPRESS_NONE) { |
| 4322 | loff_t offset = new_size; |
Kirill A. Shutemov | 09cbfea | 2016-04-01 15:29:47 +0300 | [diff] [blame] | 4323 | loff_t page_end = ALIGN(offset, PAGE_SIZE); |
Filipe Manana | 0305cd5 | 2015-10-16 12:34:25 +0100 | [diff] [blame] | 4324 | |
| 4325 | /* |
| 4326 | * Zero out the remaining of the last page of our inline extent, |
| 4327 | * instead of directly truncating our inline extent here - that |
| 4328 | * would be much more complex (decompressing all the data, then |
| 4329 | * compressing the truncated data, which might be bigger than |
| 4330 | * the size of the inline extent, resize the extent, etc). |
| 4331 | * We release the path because to get the page we might need to |
| 4332 | * read the extent item from disk (data not in the page cache). |
| 4333 | */ |
| 4334 | btrfs_release_path(path); |
Chandan Rajendra | 9703fef | 2016-01-21 15:55:56 +0530 | [diff] [blame] | 4335 | return btrfs_truncate_block(inode, offset, page_end - offset, |
| 4336 | 0); |
Filipe Manana | 0305cd5 | 2015-10-16 12:34:25 +0100 | [diff] [blame] | 4337 | } |
| 4338 | |
| 4339 | btrfs_set_file_extent_ram_bytes(leaf, fi, size); |
| 4340 | size = btrfs_file_extent_calc_inline_size(size); |
| 4341 | btrfs_truncate_item(root, path, size, 1); |
| 4342 | |
| 4343 | if (test_bit(BTRFS_ROOT_REF_COWS, &root->state)) |
| 4344 | inode_sub_bytes(inode, item_end + 1 - new_size); |
| 4345 | |
| 4346 | return 0; |
| 4347 | } |
| 4348 | |
Chris Mason | 323ac95 | 2008-10-01 19:05:46 -0400 | [diff] [blame] | 4349 | /* |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4350 | * this can truncate away extent items, csum items and directory items. |
| 4351 | * 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] | 4352 | * any higher than new_size |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4353 | * |
| 4354 | * csum items that cross the new i_size are truncated to the new size |
| 4355 | * as well. |
Josef Bacik | 7b12876 | 2008-07-24 12:17:14 -0400 | [diff] [blame] | 4356 | * |
| 4357 | * min_type is the minimum key type to truncate down to. If set to 0, this |
| 4358 | * 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] | 4359 | */ |
Yan, Zheng | 8082510 | 2009-11-12 09:35:36 +0000 | [diff] [blame] | 4360 | int btrfs_truncate_inode_items(struct btrfs_trans_handle *trans, |
| 4361 | struct btrfs_root *root, |
| 4362 | struct inode *inode, |
| 4363 | u64 new_size, u32 min_type) |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4364 | { |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4365 | struct btrfs_path *path; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 4366 | struct extent_buffer *leaf; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4367 | struct btrfs_file_extent_item *fi; |
Yan, Zheng | 8082510 | 2009-11-12 09:35:36 +0000 | [diff] [blame] | 4368 | struct btrfs_key key; |
| 4369 | struct btrfs_key found_key; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4370 | u64 extent_start = 0; |
Chris Mason | db94535 | 2007-10-15 16:15:53 -0400 | [diff] [blame] | 4371 | u64 extent_num_bytes = 0; |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 4372 | u64 extent_offset = 0; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4373 | u64 item_end = 0; |
Filipe Manana | c1aa457 | 2015-06-20 18:20:09 +0100 | [diff] [blame] | 4374 | u64 last_size = new_size; |
Yan, Zheng | 8082510 | 2009-11-12 09:35:36 +0000 | [diff] [blame] | 4375 | u32 found_type = (u8)-1; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4376 | int found_extent; |
| 4377 | int del_item; |
Chris Mason | 85e21ba | 2008-01-29 15:11:36 -0500 | [diff] [blame] | 4378 | int pending_del_nr = 0; |
| 4379 | int pending_del_slot = 0; |
Chris Mason | 179e29e | 2007-11-01 11:28:41 -0400 | [diff] [blame] | 4380 | int extent_type = -1; |
Yan, Zheng | 8082510 | 2009-11-12 09:35:36 +0000 | [diff] [blame] | 4381 | int ret; |
| 4382 | int err = 0; |
Li Zefan | 33345d01 | 2011-04-20 10:31:50 +0800 | [diff] [blame] | 4383 | u64 ino = btrfs_ino(inode); |
Chris Mason | 28ed134 | 2014-12-17 09:41:04 -0800 | [diff] [blame] | 4384 | u64 bytes_deleted = 0; |
Josef Bacik | 1262133 | 2015-02-03 07:50:16 -0800 | [diff] [blame] | 4385 | bool be_nice = 0; |
| 4386 | bool should_throttle = 0; |
Chris Mason | 28f75a0 | 2015-02-04 06:59:29 -0800 | [diff] [blame] | 4387 | bool should_end = 0; |
Yan, Zheng | 8082510 | 2009-11-12 09:35:36 +0000 | [diff] [blame] | 4388 | |
| 4389 | BUG_ON(new_size > 0 && min_type != BTRFS_EXTENT_DATA_KEY); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4390 | |
Chris Mason | 28ed134 | 2014-12-17 09:41:04 -0800 | [diff] [blame] | 4391 | /* |
| 4392 | * for non-free space inodes and ref cows, we want to back off from |
| 4393 | * time to time |
| 4394 | */ |
| 4395 | if (!btrfs_is_free_space_inode(inode) && |
| 4396 | test_bit(BTRFS_ROOT_REF_COWS, &root->state)) |
| 4397 | be_nice = 1; |
| 4398 | |
Mark Fasheh | 0eb0e19 | 2011-07-12 16:44:10 -0700 | [diff] [blame] | 4399 | path = btrfs_alloc_path(); |
| 4400 | if (!path) |
| 4401 | return -ENOMEM; |
David Sterba | e4058b5 | 2015-11-27 16:31:35 +0100 | [diff] [blame] | 4402 | path->reada = READA_BACK; |
Mark Fasheh | 0eb0e19 | 2011-07-12 16:44:10 -0700 | [diff] [blame] | 4403 | |
Josef Bacik | 5dc562c | 2012-08-17 13:14:17 -0400 | [diff] [blame] | 4404 | /* |
| 4405 | * We want to drop from the next block forward in case this new size is |
| 4406 | * not block aligned since we will be keeping the last block of the |
| 4407 | * extent just the way it is. |
| 4408 | */ |
Miao Xie | 27cdeb7 | 2014-04-02 19:51:05 +0800 | [diff] [blame] | 4409 | if (test_bit(BTRFS_ROOT_REF_COWS, &root->state) || |
| 4410 | root == root->fs_info->tree_root) |
Qu Wenruo | fda2832 | 2013-02-26 08:10:22 +0000 | [diff] [blame] | 4411 | btrfs_drop_extent_cache(inode, ALIGN(new_size, |
| 4412 | root->sectorsize), (u64)-1, 0); |
Yan, Zheng | 8082510 | 2009-11-12 09:35:36 +0000 | [diff] [blame] | 4413 | |
Miao Xie | 16cdcec | 2011-04-22 18:12:22 +0800 | [diff] [blame] | 4414 | /* |
| 4415 | * This function is also used to drop the items in the log tree before |
| 4416 | * we relog the inode, so if root != BTRFS_I(inode)->root, it means |
| 4417 | * it is used to drop the loged items. So we shouldn't kill the delayed |
| 4418 | * items. |
| 4419 | */ |
| 4420 | if (min_type == 0 && root == BTRFS_I(inode)->root) |
| 4421 | btrfs_kill_delayed_inode_items(inode); |
| 4422 | |
Li Zefan | 33345d01 | 2011-04-20 10:31:50 +0800 | [diff] [blame] | 4423 | key.objectid = ino; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4424 | key.offset = (u64)-1; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 4425 | key.type = (u8)-1; |
| 4426 | |
Chris Mason | 85e21ba | 2008-01-29 15:11:36 -0500 | [diff] [blame] | 4427 | search_again: |
Chris Mason | 28ed134 | 2014-12-17 09:41:04 -0800 | [diff] [blame] | 4428 | /* |
| 4429 | * with a 16K leaf size and 128MB extents, you can actually queue |
| 4430 | * up a huge file in a single leaf. Most of the time that |
| 4431 | * bytes_deleted is > 0, it will be huge by the time we get here |
| 4432 | */ |
Byongho Lee | ee22184 | 2015-12-15 01:42:10 +0900 | [diff] [blame] | 4433 | if (be_nice && bytes_deleted > SZ_32M) { |
Chris Mason | 28ed134 | 2014-12-17 09:41:04 -0800 | [diff] [blame] | 4434 | if (btrfs_should_end_transaction(trans, root)) { |
| 4435 | err = -EAGAIN; |
| 4436 | goto error; |
| 4437 | } |
| 4438 | } |
| 4439 | |
| 4440 | |
Chris Mason | b947343 | 2009-03-13 11:00:37 -0400 | [diff] [blame] | 4441 | path->leave_spinning = 1; |
Chris Mason | 85e21ba | 2008-01-29 15:11:36 -0500 | [diff] [blame] | 4442 | ret = btrfs_search_slot(trans, root, &key, path, -1, 1); |
Yan, Zheng | 8082510 | 2009-11-12 09:35:36 +0000 | [diff] [blame] | 4443 | if (ret < 0) { |
| 4444 | err = ret; |
| 4445 | goto out; |
| 4446 | } |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 4447 | |
Chris Mason | 85e21ba | 2008-01-29 15:11:36 -0500 | [diff] [blame] | 4448 | if (ret > 0) { |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 4449 | /* there are no items in the tree for us to truncate, we're |
| 4450 | * done |
| 4451 | */ |
Yan, Zheng | 8082510 | 2009-11-12 09:35:36 +0000 | [diff] [blame] | 4452 | if (path->slots[0] == 0) |
| 4453 | goto out; |
Chris Mason | 85e21ba | 2008-01-29 15:11:36 -0500 | [diff] [blame] | 4454 | path->slots[0]--; |
| 4455 | } |
| 4456 | |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 4457 | while (1) { |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4458 | fi = NULL; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 4459 | leaf = path->nodes[0]; |
| 4460 | btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]); |
David Sterba | 962a298 | 2014-06-04 18:41:45 +0200 | [diff] [blame] | 4461 | found_type = found_key.type; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4462 | |
Li Zefan | 33345d01 | 2011-04-20 10:31:50 +0800 | [diff] [blame] | 4463 | if (found_key.objectid != ino) |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4464 | break; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 4465 | |
Chris Mason | 85e21ba | 2008-01-29 15:11:36 -0500 | [diff] [blame] | 4466 | if (found_type < min_type) |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4467 | break; |
| 4468 | |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 4469 | item_end = found_key.offset; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4470 | if (found_type == BTRFS_EXTENT_DATA_KEY) { |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 4471 | fi = btrfs_item_ptr(leaf, path->slots[0], |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4472 | struct btrfs_file_extent_item); |
Chris Mason | 179e29e | 2007-11-01 11:28:41 -0400 | [diff] [blame] | 4473 | extent_type = btrfs_file_extent_type(leaf, fi); |
| 4474 | if (extent_type != BTRFS_FILE_EXTENT_INLINE) { |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 4475 | item_end += |
Chris Mason | db94535 | 2007-10-15 16:15:53 -0400 | [diff] [blame] | 4476 | btrfs_file_extent_num_bytes(leaf, fi); |
Chris Mason | 179e29e | 2007-11-01 11:28:41 -0400 | [diff] [blame] | 4477 | } else if (extent_type == BTRFS_FILE_EXTENT_INLINE) { |
Chris Mason | 179e29e | 2007-11-01 11:28:41 -0400 | [diff] [blame] | 4478 | item_end += btrfs_file_extent_inline_len(leaf, |
Chris Mason | 514ac8a | 2014-01-03 21:07:00 -0800 | [diff] [blame] | 4479 | path->slots[0], fi); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4480 | } |
Yan | 008630c | 2007-11-07 13:31:09 -0500 | [diff] [blame] | 4481 | item_end--; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4482 | } |
Yan, Zheng | 8082510 | 2009-11-12 09:35:36 +0000 | [diff] [blame] | 4483 | if (found_type > min_type) { |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4484 | del_item = 1; |
Yan, Zheng | 8082510 | 2009-11-12 09:35:36 +0000 | [diff] [blame] | 4485 | } else { |
| 4486 | if (item_end < new_size) |
| 4487 | break; |
| 4488 | if (found_key.offset >= new_size) |
| 4489 | del_item = 1; |
| 4490 | else |
| 4491 | del_item = 0; |
| 4492 | } |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4493 | found_extent = 0; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4494 | /* FIXME, shrink the extent if the ref count is only 1 */ |
Chris Mason | 179e29e | 2007-11-01 11:28:41 -0400 | [diff] [blame] | 4495 | if (found_type != BTRFS_EXTENT_DATA_KEY) |
| 4496 | goto delete; |
| 4497 | |
Josef Bacik | 7f4f6e0 | 2013-08-29 16:43:28 -0400 | [diff] [blame] | 4498 | if (del_item) |
| 4499 | last_size = found_key.offset; |
| 4500 | else |
| 4501 | last_size = new_size; |
| 4502 | |
Chris Mason | 179e29e | 2007-11-01 11:28:41 -0400 | [diff] [blame] | 4503 | if (extent_type != BTRFS_FILE_EXTENT_INLINE) { |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4504 | u64 num_dec; |
Chris Mason | db94535 | 2007-10-15 16:15:53 -0400 | [diff] [blame] | 4505 | extent_start = btrfs_file_extent_disk_bytenr(leaf, fi); |
Miao Xie | f70a9a6b | 2012-01-12 19:10:12 -0500 | [diff] [blame] | 4506 | if (!del_item) { |
Chris Mason | db94535 | 2007-10-15 16:15:53 -0400 | [diff] [blame] | 4507 | u64 orig_num_bytes = |
| 4508 | btrfs_file_extent_num_bytes(leaf, fi); |
Qu Wenruo | fda2832 | 2013-02-26 08:10:22 +0000 | [diff] [blame] | 4509 | extent_num_bytes = ALIGN(new_size - |
| 4510 | found_key.offset, |
| 4511 | root->sectorsize); |
Chris Mason | db94535 | 2007-10-15 16:15:53 -0400 | [diff] [blame] | 4512 | btrfs_set_file_extent_num_bytes(leaf, fi, |
| 4513 | extent_num_bytes); |
| 4514 | num_dec = (orig_num_bytes - |
Chris Mason | 9069218 | 2008-02-08 13:49:28 -0500 | [diff] [blame] | 4515 | extent_num_bytes); |
Miao Xie | 27cdeb7 | 2014-04-02 19:51:05 +0800 | [diff] [blame] | 4516 | if (test_bit(BTRFS_ROOT_REF_COWS, |
| 4517 | &root->state) && |
| 4518 | extent_start != 0) |
Yan Zheng | a76a3cd | 2008-10-09 11:46:29 -0400 | [diff] [blame] | 4519 | inode_sub_bytes(inode, num_dec); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 4520 | btrfs_mark_buffer_dirty(leaf); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4521 | } else { |
Chris Mason | db94535 | 2007-10-15 16:15:53 -0400 | [diff] [blame] | 4522 | extent_num_bytes = |
| 4523 | btrfs_file_extent_disk_num_bytes(leaf, |
| 4524 | fi); |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 4525 | extent_offset = found_key.offset - |
| 4526 | btrfs_file_extent_offset(leaf, fi); |
| 4527 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4528 | /* FIXME blocksize != 4096 */ |
Chris Mason | 9069218 | 2008-02-08 13:49:28 -0500 | [diff] [blame] | 4529 | num_dec = btrfs_file_extent_num_bytes(leaf, fi); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4530 | if (extent_start != 0) { |
| 4531 | found_extent = 1; |
Miao Xie | 27cdeb7 | 2014-04-02 19:51:05 +0800 | [diff] [blame] | 4532 | if (test_bit(BTRFS_ROOT_REF_COWS, |
| 4533 | &root->state)) |
Yan Zheng | a76a3cd | 2008-10-09 11:46:29 -0400 | [diff] [blame] | 4534 | inode_sub_bytes(inode, num_dec); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4535 | } |
| 4536 | } |
Chris Mason | 9069218 | 2008-02-08 13:49:28 -0500 | [diff] [blame] | 4537 | } else if (extent_type == BTRFS_FILE_EXTENT_INLINE) { |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 4538 | /* |
| 4539 | * we can't truncate inline items that have had |
| 4540 | * special encodings |
| 4541 | */ |
| 4542 | if (!del_item && |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 4543 | btrfs_file_extent_encryption(leaf, fi) == 0 && |
| 4544 | btrfs_file_extent_other_encoding(leaf, fi) == 0) { |
Chris Mason | 514ac8a | 2014-01-03 21:07:00 -0800 | [diff] [blame] | 4545 | |
| 4546 | /* |
Filipe Manana | 0305cd5 | 2015-10-16 12:34:25 +0100 | [diff] [blame] | 4547 | * Need to release path in order to truncate a |
| 4548 | * compressed extent. So delete any accumulated |
| 4549 | * extent items so far. |
Chris Mason | 514ac8a | 2014-01-03 21:07:00 -0800 | [diff] [blame] | 4550 | */ |
Filipe Manana | 0305cd5 | 2015-10-16 12:34:25 +0100 | [diff] [blame] | 4551 | if (btrfs_file_extent_compression(leaf, fi) != |
| 4552 | BTRFS_COMPRESS_NONE && pending_del_nr) { |
| 4553 | err = btrfs_del_items(trans, root, path, |
| 4554 | pending_del_slot, |
| 4555 | pending_del_nr); |
| 4556 | if (err) { |
| 4557 | btrfs_abort_transaction(trans, |
Filipe Manana | 0305cd5 | 2015-10-16 12:34:25 +0100 | [diff] [blame] | 4558 | err); |
| 4559 | goto error; |
| 4560 | } |
| 4561 | pending_del_nr = 0; |
| 4562 | } |
| 4563 | |
| 4564 | err = truncate_inline_extent(inode, path, |
| 4565 | &found_key, |
| 4566 | item_end, |
| 4567 | new_size); |
| 4568 | if (err) { |
Jeff Mahoney | 6664283 | 2016-06-10 18:19:25 -0400 | [diff] [blame] | 4569 | btrfs_abort_transaction(trans, err); |
Filipe Manana | 0305cd5 | 2015-10-16 12:34:25 +0100 | [diff] [blame] | 4570 | goto error; |
| 4571 | } |
Miao Xie | 27cdeb7 | 2014-04-02 19:51:05 +0800 | [diff] [blame] | 4572 | } else if (test_bit(BTRFS_ROOT_REF_COWS, |
| 4573 | &root->state)) { |
Filipe Manana | 0305cd5 | 2015-10-16 12:34:25 +0100 | [diff] [blame] | 4574 | inode_sub_bytes(inode, item_end + 1 - new_size); |
Chris Mason | 9069218 | 2008-02-08 13:49:28 -0500 | [diff] [blame] | 4575 | } |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4576 | } |
Chris Mason | 179e29e | 2007-11-01 11:28:41 -0400 | [diff] [blame] | 4577 | delete: |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4578 | if (del_item) { |
Chris Mason | 85e21ba | 2008-01-29 15:11:36 -0500 | [diff] [blame] | 4579 | if (!pending_del_nr) { |
| 4580 | /* no pending yet, add ourselves */ |
| 4581 | pending_del_slot = path->slots[0]; |
| 4582 | pending_del_nr = 1; |
| 4583 | } else if (pending_del_nr && |
| 4584 | path->slots[0] + 1 == pending_del_slot) { |
| 4585 | /* hop on the pending chunk */ |
| 4586 | pending_del_nr++; |
| 4587 | pending_del_slot = path->slots[0]; |
| 4588 | } else { |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 4589 | BUG(); |
Chris Mason | 85e21ba | 2008-01-29 15:11:36 -0500 | [diff] [blame] | 4590 | } |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4591 | } else { |
| 4592 | break; |
| 4593 | } |
Chris Mason | 28f75a0 | 2015-02-04 06:59:29 -0800 | [diff] [blame] | 4594 | should_throttle = 0; |
| 4595 | |
Miao Xie | 27cdeb7 | 2014-04-02 19:51:05 +0800 | [diff] [blame] | 4596 | if (found_extent && |
| 4597 | (test_bit(BTRFS_ROOT_REF_COWS, &root->state) || |
| 4598 | root == root->fs_info->tree_root)) { |
Chris Mason | b947343 | 2009-03-13 11:00:37 -0400 | [diff] [blame] | 4599 | btrfs_set_path_blocking(path); |
Chris Mason | 28ed134 | 2014-12-17 09:41:04 -0800 | [diff] [blame] | 4600 | bytes_deleted += extent_num_bytes; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4601 | ret = btrfs_free_extent(trans, root, extent_start, |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 4602 | extent_num_bytes, 0, |
| 4603 | btrfs_header_owner(leaf), |
Filipe Manana | b06c4bf | 2015-10-23 07:52:54 +0100 | [diff] [blame] | 4604 | ino, extent_offset); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4605 | BUG_ON(ret); |
Josef Bacik | 1262133 | 2015-02-03 07:50:16 -0800 | [diff] [blame] | 4606 | if (btrfs_should_throttle_delayed_refs(trans, root)) |
Chris Mason | 28ed134 | 2014-12-17 09:41:04 -0800 | [diff] [blame] | 4607 | btrfs_async_run_delayed_refs(root, |
Wang Xiaoguang | dd4b857 | 2016-10-18 15:56:13 +0800 | [diff] [blame] | 4608 | trans->delayed_ref_updates * 2, |
| 4609 | trans->transid, 0); |
Chris Mason | 28f75a0 | 2015-02-04 06:59:29 -0800 | [diff] [blame] | 4610 | if (be_nice) { |
| 4611 | if (truncate_space_check(trans, root, |
| 4612 | extent_num_bytes)) { |
| 4613 | should_end = 1; |
| 4614 | } |
| 4615 | if (btrfs_should_throttle_delayed_refs(trans, |
| 4616 | root)) { |
| 4617 | should_throttle = 1; |
| 4618 | } |
| 4619 | } |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4620 | } |
Chris Mason | 85e21ba | 2008-01-29 15:11:36 -0500 | [diff] [blame] | 4621 | |
Yan, Zheng | 8082510 | 2009-11-12 09:35:36 +0000 | [diff] [blame] | 4622 | if (found_type == BTRFS_INODE_ITEM_KEY) |
| 4623 | break; |
| 4624 | |
| 4625 | if (path->slots[0] == 0 || |
Josef Bacik | 1262133 | 2015-02-03 07:50:16 -0800 | [diff] [blame] | 4626 | path->slots[0] != pending_del_slot || |
Chris Mason | 28f75a0 | 2015-02-04 06:59:29 -0800 | [diff] [blame] | 4627 | should_throttle || should_end) { |
Yan, Zheng | 8082510 | 2009-11-12 09:35:36 +0000 | [diff] [blame] | 4628 | if (pending_del_nr) { |
| 4629 | ret = btrfs_del_items(trans, root, path, |
| 4630 | pending_del_slot, |
| 4631 | pending_del_nr); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 4632 | if (ret) { |
Jeff Mahoney | 6664283 | 2016-06-10 18:19:25 -0400 | [diff] [blame] | 4633 | btrfs_abort_transaction(trans, ret); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 4634 | goto error; |
| 4635 | } |
Yan, Zheng | 8082510 | 2009-11-12 09:35:36 +0000 | [diff] [blame] | 4636 | pending_del_nr = 0; |
| 4637 | } |
David Sterba | b3b4aa7 | 2011-04-21 01:20:15 +0200 | [diff] [blame] | 4638 | btrfs_release_path(path); |
Chris Mason | 28f75a0 | 2015-02-04 06:59:29 -0800 | [diff] [blame] | 4639 | if (should_throttle) { |
Josef Bacik | 1262133 | 2015-02-03 07:50:16 -0800 | [diff] [blame] | 4640 | unsigned long updates = trans->delayed_ref_updates; |
| 4641 | if (updates) { |
| 4642 | trans->delayed_ref_updates = 0; |
| 4643 | ret = btrfs_run_delayed_refs(trans, root, updates * 2); |
| 4644 | if (ret && !err) |
| 4645 | err = ret; |
| 4646 | } |
| 4647 | } |
Chris Mason | 28f75a0 | 2015-02-04 06:59:29 -0800 | [diff] [blame] | 4648 | /* |
| 4649 | * if we failed to refill our space rsv, bail out |
| 4650 | * and let the transaction restart |
| 4651 | */ |
| 4652 | if (should_end) { |
| 4653 | err = -EAGAIN; |
| 4654 | goto error; |
| 4655 | } |
Chris Mason | 85e21ba | 2008-01-29 15:11:36 -0500 | [diff] [blame] | 4656 | goto search_again; |
Yan, Zheng | 8082510 | 2009-11-12 09:35:36 +0000 | [diff] [blame] | 4657 | } else { |
| 4658 | path->slots[0]--; |
Chris Mason | 85e21ba | 2008-01-29 15:11:36 -0500 | [diff] [blame] | 4659 | } |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4660 | } |
Yan, Zheng | 8082510 | 2009-11-12 09:35:36 +0000 | [diff] [blame] | 4661 | out: |
Chris Mason | 85e21ba | 2008-01-29 15:11:36 -0500 | [diff] [blame] | 4662 | if (pending_del_nr) { |
| 4663 | ret = btrfs_del_items(trans, root, path, pending_del_slot, |
| 4664 | pending_del_nr); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 4665 | if (ret) |
Jeff Mahoney | 6664283 | 2016-06-10 18:19:25 -0400 | [diff] [blame] | 4666 | btrfs_abort_transaction(trans, ret); |
Chris Mason | 85e21ba | 2008-01-29 15:11:36 -0500 | [diff] [blame] | 4667 | } |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 4668 | error: |
Filipe Manana | c1aa457 | 2015-06-20 18:20:09 +0100 | [diff] [blame] | 4669 | if (root->root_key.objectid != BTRFS_TREE_LOG_OBJECTID) |
Josef Bacik | 7f4f6e0 | 2013-08-29 16:43:28 -0400 | [diff] [blame] | 4670 | btrfs_ordered_update_i_size(inode, last_size, NULL); |
Chris Mason | 28ed134 | 2014-12-17 09:41:04 -0800 | [diff] [blame] | 4671 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4672 | btrfs_free_path(path); |
Chris Mason | 28ed134 | 2014-12-17 09:41:04 -0800 | [diff] [blame] | 4673 | |
Byongho Lee | ee22184 | 2015-12-15 01:42:10 +0900 | [diff] [blame] | 4674 | if (be_nice && bytes_deleted > SZ_32M) { |
Chris Mason | 28ed134 | 2014-12-17 09:41:04 -0800 | [diff] [blame] | 4675 | unsigned long updates = trans->delayed_ref_updates; |
| 4676 | if (updates) { |
| 4677 | trans->delayed_ref_updates = 0; |
| 4678 | ret = btrfs_run_delayed_refs(trans, root, updates * 2); |
| 4679 | if (ret && !err) |
| 4680 | err = ret; |
| 4681 | } |
| 4682 | } |
Yan, Zheng | 8082510 | 2009-11-12 09:35:36 +0000 | [diff] [blame] | 4683 | return err; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4684 | } |
| 4685 | |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 4686 | /* |
Chandan Rajendra | 9703fef | 2016-01-21 15:55:56 +0530 | [diff] [blame] | 4687 | * btrfs_truncate_block - read, zero a chunk and write a block |
Josef Bacik | 2aaa665 | 2012-08-29 14:27:18 -0400 | [diff] [blame] | 4688 | * @inode - inode that we're zeroing |
| 4689 | * @from - the offset to start zeroing |
| 4690 | * @len - the length to zero, 0 to zero the entire range respective to the |
| 4691 | * offset |
| 4692 | * @front - zero up to the offset instead of from the offset on |
| 4693 | * |
Chandan Rajendra | 9703fef | 2016-01-21 15:55:56 +0530 | [diff] [blame] | 4694 | * 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] | 4695 | * 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] | 4696 | */ |
Chandan Rajendra | 9703fef | 2016-01-21 15:55:56 +0530 | [diff] [blame] | 4697 | 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] | 4698 | int front) |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 4699 | { |
Josef Bacik | 2aaa665 | 2012-08-29 14:27:18 -0400 | [diff] [blame] | 4700 | struct address_space *mapping = inode->i_mapping; |
Chris Mason | db94535 | 2007-10-15 16:15:53 -0400 | [diff] [blame] | 4701 | struct btrfs_root *root = BTRFS_I(inode)->root; |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 4702 | struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree; |
| 4703 | struct btrfs_ordered_extent *ordered; |
Josef Bacik | 2ac55d4 | 2010-02-03 19:33:23 +0000 | [diff] [blame] | 4704 | struct extent_state *cached_state = NULL; |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 4705 | char *kaddr; |
Chris Mason | db94535 | 2007-10-15 16:15:53 -0400 | [diff] [blame] | 4706 | u32 blocksize = root->sectorsize; |
Kirill A. Shutemov | 09cbfea | 2016-04-01 15:29:47 +0300 | [diff] [blame] | 4707 | pgoff_t index = from >> PAGE_SHIFT; |
Chandan Rajendra | 9703fef | 2016-01-21 15:55:56 +0530 | [diff] [blame] | 4708 | unsigned offset = from & (blocksize - 1); |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 4709 | struct page *page; |
Josef Bacik | 3b16a4e | 2011-09-21 15:05:58 -0400 | [diff] [blame] | 4710 | gfp_t mask = btrfs_alloc_write_mask(mapping); |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 4711 | int ret = 0; |
Chandan Rajendra | 9703fef | 2016-01-21 15:55:56 +0530 | [diff] [blame] | 4712 | u64 block_start; |
| 4713 | u64 block_end; |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 4714 | |
Josef Bacik | 2aaa665 | 2012-08-29 14:27:18 -0400 | [diff] [blame] | 4715 | if ((offset & (blocksize - 1)) == 0 && |
| 4716 | (!len || ((len & (blocksize - 1)) == 0))) |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 4717 | goto out; |
Chandan Rajendra | 9703fef | 2016-01-21 15:55:56 +0530 | [diff] [blame] | 4718 | |
Qu Wenruo | 7cf5b97 | 2015-09-08 17:25:55 +0800 | [diff] [blame] | 4719 | ret = btrfs_delalloc_reserve_space(inode, |
Chandan Rajendra | 9703fef | 2016-01-21 15:55:56 +0530 | [diff] [blame] | 4720 | round_down(from, blocksize), blocksize); |
Josef Bacik | 5d5e103 | 2009-10-13 16:46:49 -0400 | [diff] [blame] | 4721 | if (ret) |
| 4722 | goto out; |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 4723 | |
Chris Mason | 211c17f | 2008-05-15 09:13:45 -0400 | [diff] [blame] | 4724 | again: |
Josef Bacik | 3b16a4e | 2011-09-21 15:05:58 -0400 | [diff] [blame] | 4725 | page = find_or_create_page(mapping, index, mask); |
Josef Bacik | 5d5e103 | 2009-10-13 16:46:49 -0400 | [diff] [blame] | 4726 | if (!page) { |
Qu Wenruo | 7cf5b97 | 2015-09-08 17:25:55 +0800 | [diff] [blame] | 4727 | btrfs_delalloc_release_space(inode, |
Chandan Rajendra | 9703fef | 2016-01-21 15:55:56 +0530 | [diff] [blame] | 4728 | round_down(from, blocksize), |
| 4729 | blocksize); |
Miao Xie | ac6a2b3 | 2012-12-05 10:56:13 +0000 | [diff] [blame] | 4730 | ret = -ENOMEM; |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 4731 | goto out; |
Josef Bacik | 5d5e103 | 2009-10-13 16:46:49 -0400 | [diff] [blame] | 4732 | } |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 4733 | |
Chandan Rajendra | 9703fef | 2016-01-21 15:55:56 +0530 | [diff] [blame] | 4734 | block_start = round_down(from, blocksize); |
| 4735 | block_end = block_start + blocksize - 1; |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 4736 | |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 4737 | if (!PageUptodate(page)) { |
| 4738 | ret = btrfs_readpage(NULL, page); |
| 4739 | lock_page(page); |
Chris Mason | 211c17f | 2008-05-15 09:13:45 -0400 | [diff] [blame] | 4740 | if (page->mapping != mapping) { |
| 4741 | unlock_page(page); |
Kirill A. Shutemov | 09cbfea | 2016-04-01 15:29:47 +0300 | [diff] [blame] | 4742 | put_page(page); |
Chris Mason | 211c17f | 2008-05-15 09:13:45 -0400 | [diff] [blame] | 4743 | goto again; |
| 4744 | } |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 4745 | if (!PageUptodate(page)) { |
| 4746 | ret = -EIO; |
Chris Mason | 8964222 | 2008-07-24 09:41:53 -0400 | [diff] [blame] | 4747 | goto out_unlock; |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 4748 | } |
| 4749 | } |
Chris Mason | 211c17f | 2008-05-15 09:13:45 -0400 | [diff] [blame] | 4750 | wait_on_page_writeback(page); |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 4751 | |
Chandan Rajendra | 9703fef | 2016-01-21 15:55:56 +0530 | [diff] [blame] | 4752 | lock_extent_bits(io_tree, block_start, block_end, &cached_state); |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 4753 | set_page_extent_mapped(page); |
| 4754 | |
Chandan Rajendra | 9703fef | 2016-01-21 15:55:56 +0530 | [diff] [blame] | 4755 | ordered = btrfs_lookup_ordered_extent(inode, block_start); |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 4756 | if (ordered) { |
Chandan Rajendra | 9703fef | 2016-01-21 15:55:56 +0530 | [diff] [blame] | 4757 | unlock_extent_cached(io_tree, block_start, block_end, |
Josef Bacik | 2ac55d4 | 2010-02-03 19:33:23 +0000 | [diff] [blame] | 4758 | &cached_state, GFP_NOFS); |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 4759 | unlock_page(page); |
Kirill A. Shutemov | 09cbfea | 2016-04-01 15:29:47 +0300 | [diff] [blame] | 4760 | put_page(page); |
Chris Mason | eb84ae0 | 2008-07-17 13:53:27 -0400 | [diff] [blame] | 4761 | btrfs_start_ordered_extent(inode, ordered, 1); |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 4762 | btrfs_put_ordered_extent(ordered); |
| 4763 | goto again; |
| 4764 | } |
| 4765 | |
Chandan Rajendra | 9703fef | 2016-01-21 15:55:56 +0530 | [diff] [blame] | 4766 | clear_extent_bit(&BTRFS_I(inode)->io_tree, block_start, block_end, |
Liu Bo | 9e8a4a8 | 2012-09-05 19:10:51 -0600 | [diff] [blame] | 4767 | EXTENT_DIRTY | EXTENT_DELALLOC | |
| 4768 | EXTENT_DO_ACCOUNTING | EXTENT_DEFRAG, |
Josef Bacik | 2ac55d4 | 2010-02-03 19:33:23 +0000 | [diff] [blame] | 4769 | 0, 0, &cached_state, GFP_NOFS); |
Josef Bacik | 5d5e103 | 2009-10-13 16:46:49 -0400 | [diff] [blame] | 4770 | |
Chandan Rajendra | 9703fef | 2016-01-21 15:55:56 +0530 | [diff] [blame] | 4771 | ret = btrfs_set_extent_delalloc(inode, block_start, block_end, |
Qu Wenruo | ba8b04c | 2016-07-19 16:50:36 +0800 | [diff] [blame] | 4772 | &cached_state, 0); |
Josef Bacik | 9ed74f2 | 2009-09-11 16:12:44 -0400 | [diff] [blame] | 4773 | if (ret) { |
Chandan Rajendra | 9703fef | 2016-01-21 15:55:56 +0530 | [diff] [blame] | 4774 | unlock_extent_cached(io_tree, block_start, block_end, |
Josef Bacik | 2ac55d4 | 2010-02-03 19:33:23 +0000 | [diff] [blame] | 4775 | &cached_state, GFP_NOFS); |
Josef Bacik | 9ed74f2 | 2009-09-11 16:12:44 -0400 | [diff] [blame] | 4776 | goto out_unlock; |
| 4777 | } |
| 4778 | |
Chandan Rajendra | 9703fef | 2016-01-21 15:55:56 +0530 | [diff] [blame] | 4779 | if (offset != blocksize) { |
Josef Bacik | 2aaa665 | 2012-08-29 14:27:18 -0400 | [diff] [blame] | 4780 | if (!len) |
Chandan Rajendra | 9703fef | 2016-01-21 15:55:56 +0530 | [diff] [blame] | 4781 | len = blocksize - offset; |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 4782 | kaddr = kmap(page); |
Josef Bacik | 2aaa665 | 2012-08-29 14:27:18 -0400 | [diff] [blame] | 4783 | if (front) |
Chandan Rajendra | 9703fef | 2016-01-21 15:55:56 +0530 | [diff] [blame] | 4784 | memset(kaddr + (block_start - page_offset(page)), |
| 4785 | 0, offset); |
Josef Bacik | 2aaa665 | 2012-08-29 14:27:18 -0400 | [diff] [blame] | 4786 | else |
Chandan Rajendra | 9703fef | 2016-01-21 15:55:56 +0530 | [diff] [blame] | 4787 | memset(kaddr + (block_start - page_offset(page)) + offset, |
| 4788 | 0, len); |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 4789 | flush_dcache_page(page); |
| 4790 | kunmap(page); |
| 4791 | } |
Chris Mason | 247e743 | 2008-07-17 12:53:51 -0400 | [diff] [blame] | 4792 | ClearPageChecked(page); |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 4793 | set_page_dirty(page); |
Chandan Rajendra | 9703fef | 2016-01-21 15:55:56 +0530 | [diff] [blame] | 4794 | unlock_extent_cached(io_tree, block_start, block_end, &cached_state, |
Josef Bacik | 2ac55d4 | 2010-02-03 19:33:23 +0000 | [diff] [blame] | 4795 | GFP_NOFS); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4796 | |
Chris Mason | 8964222 | 2008-07-24 09:41:53 -0400 | [diff] [blame] | 4797 | out_unlock: |
Josef Bacik | 5d5e103 | 2009-10-13 16:46:49 -0400 | [diff] [blame] | 4798 | if (ret) |
Chandan Rajendra | 9703fef | 2016-01-21 15:55:56 +0530 | [diff] [blame] | 4799 | btrfs_delalloc_release_space(inode, block_start, |
| 4800 | blocksize); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4801 | unlock_page(page); |
Kirill A. Shutemov | 09cbfea | 2016-04-01 15:29:47 +0300 | [diff] [blame] | 4802 | put_page(page); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4803 | out: |
| 4804 | return ret; |
| 4805 | } |
| 4806 | |
Josef Bacik | 16e7549 | 2013-10-22 12:18:51 -0400 | [diff] [blame] | 4807 | static int maybe_insert_hole(struct btrfs_root *root, struct inode *inode, |
| 4808 | u64 offset, u64 len) |
| 4809 | { |
| 4810 | struct btrfs_trans_handle *trans; |
| 4811 | int ret; |
| 4812 | |
| 4813 | /* |
| 4814 | * Still need to make sure the inode looks like it's been updated so |
| 4815 | * that any holes get logged if we fsync. |
| 4816 | */ |
| 4817 | if (btrfs_fs_incompat(root->fs_info, NO_HOLES)) { |
| 4818 | BTRFS_I(inode)->last_trans = root->fs_info->generation; |
| 4819 | BTRFS_I(inode)->last_sub_trans = root->log_transid; |
| 4820 | BTRFS_I(inode)->last_log_commit = root->last_log_commit; |
| 4821 | return 0; |
| 4822 | } |
| 4823 | |
| 4824 | /* |
| 4825 | * 1 - for the one we're dropping |
| 4826 | * 1 - for the one we're adding |
| 4827 | * 1 - for updating the inode. |
| 4828 | */ |
| 4829 | trans = btrfs_start_transaction(root, 3); |
| 4830 | if (IS_ERR(trans)) |
| 4831 | return PTR_ERR(trans); |
| 4832 | |
| 4833 | ret = btrfs_drop_extents(trans, root, inode, offset, offset + len, 1); |
| 4834 | if (ret) { |
Jeff Mahoney | 6664283 | 2016-06-10 18:19:25 -0400 | [diff] [blame] | 4835 | btrfs_abort_transaction(trans, ret); |
Josef Bacik | 16e7549 | 2013-10-22 12:18:51 -0400 | [diff] [blame] | 4836 | btrfs_end_transaction(trans, root); |
| 4837 | return ret; |
| 4838 | } |
| 4839 | |
| 4840 | ret = btrfs_insert_file_extent(trans, root, btrfs_ino(inode), offset, |
| 4841 | 0, 0, len, 0, len, 0, 0, 0); |
| 4842 | if (ret) |
Jeff Mahoney | 6664283 | 2016-06-10 18:19:25 -0400 | [diff] [blame] | 4843 | btrfs_abort_transaction(trans, ret); |
Josef Bacik | 16e7549 | 2013-10-22 12:18:51 -0400 | [diff] [blame] | 4844 | else |
| 4845 | btrfs_update_inode(trans, root, inode); |
| 4846 | btrfs_end_transaction(trans, root); |
| 4847 | return ret; |
| 4848 | } |
| 4849 | |
Josef Bacik | 695a0d0 | 2011-03-04 15:46:53 -0500 | [diff] [blame] | 4850 | /* |
| 4851 | * This function puts in dummy file extents for the area we're creating a hole |
| 4852 | * for. So if we are truncating this file to a larger size we need to insert |
| 4853 | * these file extents so that btrfs_get_extent will return a EXTENT_MAP_HOLE for |
| 4854 | * the range between oldsize and size |
| 4855 | */ |
Josef Bacik | a41ad39 | 2011-01-31 15:30:16 -0500 | [diff] [blame] | 4856 | 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] | 4857 | { |
Yan Zheng | 9036c10 | 2008-10-30 14:19:41 -0400 | [diff] [blame] | 4858 | struct btrfs_root *root = BTRFS_I(inode)->root; |
| 4859 | struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree; |
Yan, Zheng | a22285a | 2010-05-16 10:48:46 -0400 | [diff] [blame] | 4860 | struct extent_map *em = NULL; |
Josef Bacik | 2ac55d4 | 2010-02-03 19:33:23 +0000 | [diff] [blame] | 4861 | struct extent_state *cached_state = NULL; |
Josef Bacik | 5dc562c | 2012-08-17 13:14:17 -0400 | [diff] [blame] | 4862 | struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree; |
Qu Wenruo | fda2832 | 2013-02-26 08:10:22 +0000 | [diff] [blame] | 4863 | u64 hole_start = ALIGN(oldsize, root->sectorsize); |
| 4864 | u64 block_end = ALIGN(size, root->sectorsize); |
Yan Zheng | 9036c10 | 2008-10-30 14:19:41 -0400 | [diff] [blame] | 4865 | u64 last_byte; |
| 4866 | u64 cur_offset; |
| 4867 | u64 hole_size; |
Josef Bacik | 9ed74f2 | 2009-09-11 16:12:44 -0400 | [diff] [blame] | 4868 | int err = 0; |
Yan Zheng | 9036c10 | 2008-10-30 14:19:41 -0400 | [diff] [blame] | 4869 | |
Josef Bacik | a71754f | 2013-06-17 17:14:39 -0400 | [diff] [blame] | 4870 | /* |
Chandan Rajendra | 9703fef | 2016-01-21 15:55:56 +0530 | [diff] [blame] | 4871 | * If our size started in the middle of a block we need to zero out the |
| 4872 | * 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] | 4873 | * expose stale data. |
| 4874 | */ |
Chandan Rajendra | 9703fef | 2016-01-21 15:55:56 +0530 | [diff] [blame] | 4875 | err = btrfs_truncate_block(inode, oldsize, 0, 0); |
Josef Bacik | a71754f | 2013-06-17 17:14:39 -0400 | [diff] [blame] | 4876 | if (err) |
| 4877 | return err; |
| 4878 | |
Yan Zheng | 9036c10 | 2008-10-30 14:19:41 -0400 | [diff] [blame] | 4879 | if (size <= hole_start) |
| 4880 | return 0; |
| 4881 | |
Yan Zheng | 9036c10 | 2008-10-30 14:19:41 -0400 | [diff] [blame] | 4882 | while (1) { |
| 4883 | struct btrfs_ordered_extent *ordered; |
Miao Xie | fa7c149 | 2013-09-26 13:15:27 +0800 | [diff] [blame] | 4884 | |
David Sterba | ff13db4 | 2015-12-03 14:30:40 +0100 | [diff] [blame] | 4885 | lock_extent_bits(io_tree, hole_start, block_end - 1, |
Jeff Mahoney | d008237 | 2012-03-01 14:57:19 +0100 | [diff] [blame] | 4886 | &cached_state); |
Miao Xie | fa7c149 | 2013-09-26 13:15:27 +0800 | [diff] [blame] | 4887 | ordered = btrfs_lookup_ordered_range(inode, hole_start, |
| 4888 | block_end - hole_start); |
Yan Zheng | 9036c10 | 2008-10-30 14:19:41 -0400 | [diff] [blame] | 4889 | if (!ordered) |
| 4890 | break; |
Josef Bacik | 2ac55d4 | 2010-02-03 19:33:23 +0000 | [diff] [blame] | 4891 | unlock_extent_cached(io_tree, hole_start, block_end - 1, |
| 4892 | &cached_state, GFP_NOFS); |
Miao Xie | fa7c149 | 2013-09-26 13:15:27 +0800 | [diff] [blame] | 4893 | btrfs_start_ordered_extent(inode, ordered, 1); |
Yan Zheng | 9036c10 | 2008-10-30 14:19:41 -0400 | [diff] [blame] | 4894 | btrfs_put_ordered_extent(ordered); |
| 4895 | } |
| 4896 | |
Yan Zheng | 9036c10 | 2008-10-30 14:19:41 -0400 | [diff] [blame] | 4897 | cur_offset = hole_start; |
| 4898 | while (1) { |
| 4899 | em = btrfs_get_extent(inode, NULL, 0, cur_offset, |
| 4900 | block_end - cur_offset, 0); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 4901 | if (IS_ERR(em)) { |
| 4902 | err = PTR_ERR(em); |
Zach Brown | f276795 | 2013-01-08 19:37:58 +0000 | [diff] [blame] | 4903 | em = NULL; |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 4904 | break; |
| 4905 | } |
Yan Zheng | 9036c10 | 2008-10-30 14:19:41 -0400 | [diff] [blame] | 4906 | last_byte = min(extent_map_end(em), block_end); |
Qu Wenruo | fda2832 | 2013-02-26 08:10:22 +0000 | [diff] [blame] | 4907 | last_byte = ALIGN(last_byte , root->sectorsize); |
Yan, Zheng | 8082510 | 2009-11-12 09:35:36 +0000 | [diff] [blame] | 4908 | if (!test_bit(EXTENT_FLAG_PREALLOC, &em->flags)) { |
Josef Bacik | 5dc562c | 2012-08-17 13:14:17 -0400 | [diff] [blame] | 4909 | struct extent_map *hole_em; |
Yan Zheng | 9036c10 | 2008-10-30 14:19:41 -0400 | [diff] [blame] | 4910 | hole_size = last_byte - cur_offset; |
Yan, Zheng | 8082510 | 2009-11-12 09:35:36 +0000 | [diff] [blame] | 4911 | |
Josef Bacik | 16e7549 | 2013-10-22 12:18:51 -0400 | [diff] [blame] | 4912 | err = maybe_insert_hole(root, inode, cur_offset, |
| 4913 | hole_size); |
| 4914 | if (err) |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 4915 | break; |
Josef Bacik | 5dc562c | 2012-08-17 13:14:17 -0400 | [diff] [blame] | 4916 | btrfs_drop_extent_cache(inode, cur_offset, |
| 4917 | cur_offset + hole_size - 1, 0); |
| 4918 | hole_em = alloc_extent_map(); |
| 4919 | if (!hole_em) { |
| 4920 | set_bit(BTRFS_INODE_NEEDS_FULL_SYNC, |
| 4921 | &BTRFS_I(inode)->runtime_flags); |
| 4922 | goto next; |
| 4923 | } |
| 4924 | hole_em->start = cur_offset; |
| 4925 | hole_em->len = hole_size; |
| 4926 | hole_em->orig_start = cur_offset; |
Yan, Zheng | 8082510 | 2009-11-12 09:35:36 +0000 | [diff] [blame] | 4927 | |
Josef Bacik | 5dc562c | 2012-08-17 13:14:17 -0400 | [diff] [blame] | 4928 | hole_em->block_start = EXTENT_MAP_HOLE; |
| 4929 | hole_em->block_len = 0; |
Josef Bacik | b493968 | 2012-12-03 10:31:19 -0500 | [diff] [blame] | 4930 | hole_em->orig_block_len = 0; |
Josef Bacik | cc95bef | 2013-04-04 14:31:27 -0400 | [diff] [blame] | 4931 | hole_em->ram_bytes = hole_size; |
Josef Bacik | 5dc562c | 2012-08-17 13:14:17 -0400 | [diff] [blame] | 4932 | hole_em->bdev = root->fs_info->fs_devices->latest_bdev; |
| 4933 | hole_em->compress_type = BTRFS_COMPRESS_NONE; |
Josef Bacik | 16e7549 | 2013-10-22 12:18:51 -0400 | [diff] [blame] | 4934 | hole_em->generation = root->fs_info->generation; |
Josef Bacik | 5dc562c | 2012-08-17 13:14:17 -0400 | [diff] [blame] | 4935 | |
| 4936 | while (1) { |
| 4937 | write_lock(&em_tree->lock); |
Josef Bacik | 09a2a8f9 | 2013-04-05 16:51:15 -0400 | [diff] [blame] | 4938 | err = add_extent_mapping(em_tree, hole_em, 1); |
Josef Bacik | 5dc562c | 2012-08-17 13:14:17 -0400 | [diff] [blame] | 4939 | write_unlock(&em_tree->lock); |
| 4940 | if (err != -EEXIST) |
| 4941 | break; |
| 4942 | btrfs_drop_extent_cache(inode, cur_offset, |
| 4943 | cur_offset + |
| 4944 | hole_size - 1, 0); |
| 4945 | } |
| 4946 | free_extent_map(hole_em); |
Yan Zheng | 9036c10 | 2008-10-30 14:19:41 -0400 | [diff] [blame] | 4947 | } |
Josef Bacik | 16e7549 | 2013-10-22 12:18:51 -0400 | [diff] [blame] | 4948 | next: |
Yan Zheng | 9036c10 | 2008-10-30 14:19:41 -0400 | [diff] [blame] | 4949 | free_extent_map(em); |
Yan, Zheng | a22285a | 2010-05-16 10:48:46 -0400 | [diff] [blame] | 4950 | em = NULL; |
Yan Zheng | 9036c10 | 2008-10-30 14:19:41 -0400 | [diff] [blame] | 4951 | cur_offset = last_byte; |
Yan, Zheng | 8082510 | 2009-11-12 09:35:36 +0000 | [diff] [blame] | 4952 | if (cur_offset >= block_end) |
Yan Zheng | 9036c10 | 2008-10-30 14:19:41 -0400 | [diff] [blame] | 4953 | break; |
| 4954 | } |
Yan, Zheng | a22285a | 2010-05-16 10:48:46 -0400 | [diff] [blame] | 4955 | free_extent_map(em); |
Josef Bacik | 2ac55d4 | 2010-02-03 19:33:23 +0000 | [diff] [blame] | 4956 | unlock_extent_cached(io_tree, hole_start, block_end - 1, &cached_state, |
| 4957 | GFP_NOFS); |
Yan Zheng | 9036c10 | 2008-10-30 14:19:41 -0400 | [diff] [blame] | 4958 | return err; |
| 4959 | } |
| 4960 | |
Eric Sandeen | 3972f26 | 2013-01-12 02:57:22 +0000 | [diff] [blame] | 4961 | static int btrfs_setsize(struct inode *inode, struct iattr *attr) |
Yan, Zheng | 8082510 | 2009-11-12 09:35:36 +0000 | [diff] [blame] | 4962 | { |
Miao Xie | f4a2f4c | 2011-12-14 20:12:01 -0500 | [diff] [blame] | 4963 | struct btrfs_root *root = BTRFS_I(inode)->root; |
| 4964 | struct btrfs_trans_handle *trans; |
Josef Bacik | a41ad39 | 2011-01-31 15:30:16 -0500 | [diff] [blame] | 4965 | loff_t oldsize = i_size_read(inode); |
Eric Sandeen | 3972f26 | 2013-01-12 02:57:22 +0000 | [diff] [blame] | 4966 | loff_t newsize = attr->ia_size; |
| 4967 | int mask = attr->ia_valid; |
Yan, Zheng | 8082510 | 2009-11-12 09:35:36 +0000 | [diff] [blame] | 4968 | int ret; |
| 4969 | |
Eric Sandeen | 3972f26 | 2013-01-12 02:57:22 +0000 | [diff] [blame] | 4970 | /* |
| 4971 | * The regular truncate() case without ATTR_CTIME and ATTR_MTIME is a |
| 4972 | * special case where we need to update the times despite not having |
| 4973 | * these flags set. For all other operations the VFS set these flags |
| 4974 | * explicitly if it wants a timestamp update. |
| 4975 | */ |
Christoph Hellwig | dff6efc | 2013-11-19 07:17:07 -0800 | [diff] [blame] | 4976 | if (newsize != oldsize) { |
| 4977 | inode_inc_iversion(inode); |
| 4978 | if (!(mask & (ATTR_CTIME | ATTR_MTIME))) |
| 4979 | inode->i_ctime = inode->i_mtime = |
Deepa Dinamani | c2050a4 | 2016-09-14 07:48:06 -0700 | [diff] [blame] | 4980 | current_time(inode); |
Christoph Hellwig | dff6efc | 2013-11-19 07:17:07 -0800 | [diff] [blame] | 4981 | } |
Eric Sandeen | 3972f26 | 2013-01-12 02:57:22 +0000 | [diff] [blame] | 4982 | |
Josef Bacik | a41ad39 | 2011-01-31 15:30:16 -0500 | [diff] [blame] | 4983 | if (newsize > oldsize) { |
Filipe Manana | 9ea24bb | 2014-10-29 11:57:59 +0000 | [diff] [blame] | 4984 | /* |
| 4985 | * Don't do an expanding truncate while snapshoting is ongoing. |
| 4986 | * This is to ensure the snapshot captures a fully consistent |
| 4987 | * state of this file - if the snapshot captures this expanding |
| 4988 | * truncation, it must capture all writes that happened before |
| 4989 | * this truncation. |
| 4990 | */ |
Zhao Lei | 0bc19f90 | 2016-01-06 18:56:36 +0800 | [diff] [blame] | 4991 | btrfs_wait_for_snapshot_creation(root); |
Josef Bacik | a41ad39 | 2011-01-31 15:30:16 -0500 | [diff] [blame] | 4992 | ret = btrfs_cont_expand(inode, oldsize, newsize); |
Filipe Manana | 9ea24bb | 2014-10-29 11:57:59 +0000 | [diff] [blame] | 4993 | if (ret) { |
| 4994 | btrfs_end_write_no_snapshoting(root); |
Yan, Zheng | 8082510 | 2009-11-12 09:35:36 +0000 | [diff] [blame] | 4995 | return ret; |
Filipe Manana | 9ea24bb | 2014-10-29 11:57:59 +0000 | [diff] [blame] | 4996 | } |
Yan, Zheng | 8082510 | 2009-11-12 09:35:36 +0000 | [diff] [blame] | 4997 | |
Miao Xie | f4a2f4c | 2011-12-14 20:12:01 -0500 | [diff] [blame] | 4998 | trans = btrfs_start_transaction(root, 1); |
Filipe Manana | 9ea24bb | 2014-10-29 11:57:59 +0000 | [diff] [blame] | 4999 | if (IS_ERR(trans)) { |
| 5000 | btrfs_end_write_no_snapshoting(root); |
Miao Xie | f4a2f4c | 2011-12-14 20:12:01 -0500 | [diff] [blame] | 5001 | return PTR_ERR(trans); |
Filipe Manana | 9ea24bb | 2014-10-29 11:57:59 +0000 | [diff] [blame] | 5002 | } |
Miao Xie | f4a2f4c | 2011-12-14 20:12:01 -0500 | [diff] [blame] | 5003 | |
| 5004 | i_size_write(inode, newsize); |
| 5005 | btrfs_ordered_update_i_size(inode, i_size_read(inode), NULL); |
Chandan Rajendra | 27772b6 | 2016-01-21 15:56:03 +0530 | [diff] [blame] | 5006 | pagecache_isize_extended(inode, oldsize, newsize); |
Miao Xie | f4a2f4c | 2011-12-14 20:12:01 -0500 | [diff] [blame] | 5007 | ret = btrfs_update_inode(trans, root, inode); |
Filipe Manana | 9ea24bb | 2014-10-29 11:57:59 +0000 | [diff] [blame] | 5008 | btrfs_end_write_no_snapshoting(root); |
Josef Bacik | 7ad85bb | 2012-01-12 19:10:12 -0500 | [diff] [blame] | 5009 | btrfs_end_transaction(trans, root); |
Josef Bacik | a41ad39 | 2011-01-31 15:30:16 -0500 | [diff] [blame] | 5010 | } else { |
Yan, Zheng | 8082510 | 2009-11-12 09:35:36 +0000 | [diff] [blame] | 5011 | |
Josef Bacik | a41ad39 | 2011-01-31 15:30:16 -0500 | [diff] [blame] | 5012 | /* |
| 5013 | * We're truncating a file that used to have good data down to |
| 5014 | * zero. Make sure it gets into the ordered flush list so that |
| 5015 | * any new writes get down to disk quickly. |
| 5016 | */ |
| 5017 | if (newsize == 0) |
Josef Bacik | 72ac3c0 | 2012-05-23 14:13:11 -0400 | [diff] [blame] | 5018 | set_bit(BTRFS_INODE_ORDERED_DATA_CLOSE, |
| 5019 | &BTRFS_I(inode)->runtime_flags); |
Yan, Zheng | 8082510 | 2009-11-12 09:35:36 +0000 | [diff] [blame] | 5020 | |
Josef Bacik | f3fe820 | 2013-01-07 17:03:21 -0500 | [diff] [blame] | 5021 | /* |
| 5022 | * 1 for the orphan item we're going to add |
| 5023 | * 1 for the orphan item deletion. |
| 5024 | */ |
| 5025 | trans = btrfs_start_transaction(root, 2); |
| 5026 | if (IS_ERR(trans)) |
| 5027 | return PTR_ERR(trans); |
| 5028 | |
| 5029 | /* |
| 5030 | * We need to do this in case we fail at _any_ point during the |
| 5031 | * actual truncate. Once we do the truncate_setsize we could |
| 5032 | * invalidate pages which forces any outstanding ordered io to |
| 5033 | * be instantly completed which will give us extents that need |
| 5034 | * to be truncated. If we fail to get an orphan inode down we |
| 5035 | * could have left over extents that were never meant to live, |
Nicholas D Steeves | 0132761 | 2016-05-19 21:18:45 -0400 | [diff] [blame] | 5036 | * so we need to guarantee from this point on that everything |
Josef Bacik | f3fe820 | 2013-01-07 17:03:21 -0500 | [diff] [blame] | 5037 | * will be consistent. |
| 5038 | */ |
| 5039 | ret = btrfs_orphan_add(trans, inode); |
| 5040 | btrfs_end_transaction(trans, root); |
| 5041 | if (ret) |
| 5042 | return ret; |
| 5043 | |
Josef Bacik | a41ad39 | 2011-01-31 15:30:16 -0500 | [diff] [blame] | 5044 | /* we don't support swapfiles, so vmtruncate shouldn't fail */ |
| 5045 | truncate_setsize(inode, newsize); |
Miao Xie | 2e60a51 | 2013-02-08 07:01:08 +0000 | [diff] [blame] | 5046 | |
| 5047 | /* Disable nonlocked read DIO to avoid the end less truncate */ |
| 5048 | btrfs_inode_block_unlocked_dio(inode); |
| 5049 | inode_dio_wait(inode); |
| 5050 | btrfs_inode_resume_unlocked_dio(inode); |
| 5051 | |
Josef Bacik | a41ad39 | 2011-01-31 15:30:16 -0500 | [diff] [blame] | 5052 | ret = btrfs_truncate(inode); |
Josef Bacik | 7f4f6e0 | 2013-08-29 16:43:28 -0400 | [diff] [blame] | 5053 | if (ret && inode->i_nlink) { |
| 5054 | int err; |
| 5055 | |
| 5056 | /* |
| 5057 | * failed to truncate, disk_i_size is only adjusted down |
| 5058 | * as we remove extents, so it should represent the true |
| 5059 | * size of the inode, so reset the in memory size and |
| 5060 | * delete our orphan entry. |
| 5061 | */ |
| 5062 | trans = btrfs_join_transaction(root); |
| 5063 | if (IS_ERR(trans)) { |
| 5064 | btrfs_orphan_del(NULL, inode); |
| 5065 | return ret; |
| 5066 | } |
| 5067 | i_size_write(inode, BTRFS_I(inode)->disk_i_size); |
| 5068 | err = btrfs_orphan_del(trans, inode); |
| 5069 | if (err) |
Jeff Mahoney | 6664283 | 2016-06-10 18:19:25 -0400 | [diff] [blame] | 5070 | btrfs_abort_transaction(trans, err); |
Josef Bacik | 7f4f6e0 | 2013-08-29 16:43:28 -0400 | [diff] [blame] | 5071 | btrfs_end_transaction(trans, root); |
| 5072 | } |
Yan, Zheng | 8082510 | 2009-11-12 09:35:36 +0000 | [diff] [blame] | 5073 | } |
| 5074 | |
Josef Bacik | a41ad39 | 2011-01-31 15:30:16 -0500 | [diff] [blame] | 5075 | return ret; |
Yan, Zheng | 8082510 | 2009-11-12 09:35:36 +0000 | [diff] [blame] | 5076 | } |
| 5077 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5078 | static int btrfs_setattr(struct dentry *dentry, struct iattr *attr) |
| 5079 | { |
David Howells | 2b0143b | 2015-03-17 22:25:59 +0000 | [diff] [blame] | 5080 | struct inode *inode = d_inode(dentry); |
Li Zefan | b83cc96 | 2010-12-20 16:04:08 +0800 | [diff] [blame] | 5081 | struct btrfs_root *root = BTRFS_I(inode)->root; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5082 | int err; |
| 5083 | |
Li Zefan | b83cc96 | 2010-12-20 16:04:08 +0800 | [diff] [blame] | 5084 | if (btrfs_root_readonly(root)) |
| 5085 | return -EROFS; |
| 5086 | |
Jan Kara | 31051c8 | 2016-05-26 16:55:18 +0200 | [diff] [blame] | 5087 | err = setattr_prepare(dentry, attr); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5088 | if (err) |
| 5089 | return err; |
| 5090 | |
Chris Mason | 5a3f23d | 2009-03-31 13:27:11 -0400 | [diff] [blame] | 5091 | if (S_ISREG(inode->i_mode) && (attr->ia_valid & ATTR_SIZE)) { |
Eric Sandeen | 3972f26 | 2013-01-12 02:57:22 +0000 | [diff] [blame] | 5092 | err = btrfs_setsize(inode, attr); |
Yan, Zheng | 8082510 | 2009-11-12 09:35:36 +0000 | [diff] [blame] | 5093 | if (err) |
| 5094 | return err; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5095 | } |
Yan Zheng | 9036c10 | 2008-10-30 14:19:41 -0400 | [diff] [blame] | 5096 | |
Christoph Hellwig | 1025774 | 2010-06-04 11:30:02 +0200 | [diff] [blame] | 5097 | if (attr->ia_valid) { |
| 5098 | setattr_copy(inode, attr); |
Josef Bacik | 0c4d2d9 | 2012-04-05 15:03:02 -0400 | [diff] [blame] | 5099 | inode_inc_iversion(inode); |
Josef Bacik | 22c44fe | 2011-11-30 10:45:38 -0500 | [diff] [blame] | 5100 | err = btrfs_dirty_inode(inode); |
Josef Bacik | 33268ea | 2008-07-24 12:16:36 -0400 | [diff] [blame] | 5101 | |
Josef Bacik | 22c44fe | 2011-11-30 10:45:38 -0500 | [diff] [blame] | 5102 | if (!err && attr->ia_valid & ATTR_MODE) |
Christoph Hellwig | 996a710 | 2013-12-20 05:16:43 -0800 | [diff] [blame] | 5103 | err = posix_acl_chmod(inode, inode->i_mode); |
Christoph Hellwig | 1025774 | 2010-06-04 11:30:02 +0200 | [diff] [blame] | 5104 | } |
| 5105 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5106 | return err; |
| 5107 | } |
Chris Mason | 61295eb | 2008-01-14 16:24:38 -0500 | [diff] [blame] | 5108 | |
Filipe David Borba Manana | 131e404 | 2013-11-19 22:29:35 +0000 | [diff] [blame] | 5109 | /* |
| 5110 | * While truncating the inode pages during eviction, we get the VFS calling |
| 5111 | * btrfs_invalidatepage() against each page of the inode. This is slow because |
| 5112 | * the calls to btrfs_invalidatepage() result in a huge amount of calls to |
| 5113 | * lock_extent_bits() and clear_extent_bit(), which keep merging and splitting |
| 5114 | * extent_state structures over and over, wasting lots of time. |
| 5115 | * |
| 5116 | * Therefore if the inode is being evicted, let btrfs_invalidatepage() skip all |
| 5117 | * those expensive operations on a per page basis and do only the ordered io |
| 5118 | * finishing, while we release here the extent_map and extent_state structures, |
| 5119 | * without the excessive merging and splitting. |
| 5120 | */ |
| 5121 | static void evict_inode_truncate_pages(struct inode *inode) |
| 5122 | { |
| 5123 | struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree; |
| 5124 | struct extent_map_tree *map_tree = &BTRFS_I(inode)->extent_tree; |
| 5125 | struct rb_node *node; |
| 5126 | |
| 5127 | ASSERT(inode->i_state & I_FREEING); |
Johannes Weiner | 91b0abe | 2014-04-03 14:47:49 -0700 | [diff] [blame] | 5128 | truncate_inode_pages_final(&inode->i_data); |
Filipe David Borba Manana | 131e404 | 2013-11-19 22:29:35 +0000 | [diff] [blame] | 5129 | |
| 5130 | write_lock(&map_tree->lock); |
| 5131 | while (!RB_EMPTY_ROOT(&map_tree->map)) { |
| 5132 | struct extent_map *em; |
| 5133 | |
| 5134 | node = rb_first(&map_tree->map); |
| 5135 | em = rb_entry(node, struct extent_map, rb_node); |
Wang Shilong | 180589e | 2013-12-14 15:27:31 +0800 | [diff] [blame] | 5136 | clear_bit(EXTENT_FLAG_PINNED, &em->flags); |
| 5137 | clear_bit(EXTENT_FLAG_LOGGING, &em->flags); |
Filipe David Borba Manana | 131e404 | 2013-11-19 22:29:35 +0000 | [diff] [blame] | 5138 | remove_extent_mapping(map_tree, em); |
| 5139 | free_extent_map(em); |
Filipe Manana | 7064dd5 | 2014-08-08 02:47:05 +0100 | [diff] [blame] | 5140 | if (need_resched()) { |
| 5141 | write_unlock(&map_tree->lock); |
| 5142 | cond_resched(); |
| 5143 | write_lock(&map_tree->lock); |
| 5144 | } |
Filipe David Borba Manana | 131e404 | 2013-11-19 22:29:35 +0000 | [diff] [blame] | 5145 | } |
| 5146 | write_unlock(&map_tree->lock); |
| 5147 | |
Filipe Manana | 6ca0709 | 2015-05-26 00:55:42 +0100 | [diff] [blame] | 5148 | /* |
| 5149 | * Keep looping until we have no more ranges in the io tree. |
| 5150 | * We can have ongoing bios started by readpages (called from readahead) |
Filipe Manana | 9c6429d | 2015-06-10 12:55:41 +0100 | [diff] [blame] | 5151 | * that have their endio callback (extent_io.c:end_bio_extent_readpage) |
| 5152 | * still in progress (unlocked the pages in the bio but did not yet |
| 5153 | * unlocked the ranges in the io tree). Therefore this means some |
Filipe Manana | 6ca0709 | 2015-05-26 00:55:42 +0100 | [diff] [blame] | 5154 | * ranges can still be locked and eviction started because before |
| 5155 | * submitting those bios, which are executed by a separate task (work |
| 5156 | * queue kthread), inode references (inode->i_count) were not taken |
| 5157 | * (which would be dropped in the end io callback of each bio). |
| 5158 | * Therefore here we effectively end up waiting for those bios and |
| 5159 | * anyone else holding locked ranges without having bumped the inode's |
| 5160 | * reference count - if we don't do it, when they access the inode's |
| 5161 | * io_tree to unlock a range it may be too late, leading to an |
| 5162 | * use-after-free issue. |
| 5163 | */ |
Filipe David Borba Manana | 131e404 | 2013-11-19 22:29:35 +0000 | [diff] [blame] | 5164 | spin_lock(&io_tree->lock); |
| 5165 | while (!RB_EMPTY_ROOT(&io_tree->state)) { |
| 5166 | struct extent_state *state; |
| 5167 | struct extent_state *cached_state = NULL; |
Filipe Manana | 6ca0709 | 2015-05-26 00:55:42 +0100 | [diff] [blame] | 5168 | u64 start; |
| 5169 | u64 end; |
Filipe David Borba Manana | 131e404 | 2013-11-19 22:29:35 +0000 | [diff] [blame] | 5170 | |
| 5171 | node = rb_first(&io_tree->state); |
| 5172 | state = rb_entry(node, struct extent_state, rb_node); |
Filipe Manana | 6ca0709 | 2015-05-26 00:55:42 +0100 | [diff] [blame] | 5173 | start = state->start; |
| 5174 | end = state->end; |
Filipe David Borba Manana | 131e404 | 2013-11-19 22:29:35 +0000 | [diff] [blame] | 5175 | spin_unlock(&io_tree->lock); |
| 5176 | |
David Sterba | ff13db4 | 2015-12-03 14:30:40 +0100 | [diff] [blame] | 5177 | lock_extent_bits(io_tree, start, end, &cached_state); |
Qu Wenruo | b9d0b38 | 2015-09-29 10:35:16 +0800 | [diff] [blame] | 5178 | |
| 5179 | /* |
| 5180 | * If still has DELALLOC flag, the extent didn't reach disk, |
| 5181 | * and its reserved space won't be freed by delayed_ref. |
| 5182 | * So we need to free its reserved space here. |
| 5183 | * (Refer to comment in btrfs_invalidatepage, case 2) |
| 5184 | * |
| 5185 | * Note, end is the bytenr of last byte, so we need + 1 here. |
| 5186 | */ |
| 5187 | if (state->state & EXTENT_DELALLOC) |
| 5188 | btrfs_qgroup_free_data(inode, start, end - start + 1); |
| 5189 | |
Filipe Manana | 6ca0709 | 2015-05-26 00:55:42 +0100 | [diff] [blame] | 5190 | clear_extent_bit(io_tree, start, end, |
Filipe David Borba Manana | 131e404 | 2013-11-19 22:29:35 +0000 | [diff] [blame] | 5191 | EXTENT_LOCKED | EXTENT_DIRTY | |
| 5192 | EXTENT_DELALLOC | EXTENT_DO_ACCOUNTING | |
| 5193 | EXTENT_DEFRAG, 1, 1, |
| 5194 | &cached_state, GFP_NOFS); |
Filipe David Borba Manana | 131e404 | 2013-11-19 22:29:35 +0000 | [diff] [blame] | 5195 | |
Filipe Manana | 7064dd5 | 2014-08-08 02:47:05 +0100 | [diff] [blame] | 5196 | cond_resched(); |
Filipe David Borba Manana | 131e404 | 2013-11-19 22:29:35 +0000 | [diff] [blame] | 5197 | spin_lock(&io_tree->lock); |
| 5198 | } |
| 5199 | spin_unlock(&io_tree->lock); |
| 5200 | } |
| 5201 | |
Al Viro | bd55597 | 2010-06-07 11:35:40 -0400 | [diff] [blame] | 5202 | void btrfs_evict_inode(struct inode *inode) |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5203 | { |
| 5204 | struct btrfs_trans_handle *trans; |
| 5205 | struct btrfs_root *root = BTRFS_I(inode)->root; |
Josef Bacik | 726c35f | 2011-09-26 15:46:06 -0400 | [diff] [blame] | 5206 | struct btrfs_block_rsv *rsv, *global_rsv; |
Josef Bacik | 3bce876 | 2015-02-24 12:35:51 -0800 | [diff] [blame] | 5207 | int steal_from_global = 0; |
Nikolay Borisov | 3d48d98 | 2016-06-29 09:46:41 +0300 | [diff] [blame] | 5208 | u64 min_size; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5209 | int ret; |
| 5210 | |
liubo | 1abe9b8 | 2011-03-24 11:18:59 +0000 | [diff] [blame] | 5211 | trace_btrfs_inode_evict(inode); |
| 5212 | |
Nikolay Borisov | 3d48d98 | 2016-06-29 09:46:41 +0300 | [diff] [blame] | 5213 | if (!root) { |
| 5214 | kmem_cache_free(btrfs_inode_cachep, BTRFS_I(inode)); |
| 5215 | return; |
| 5216 | } |
| 5217 | |
| 5218 | min_size = btrfs_calc_trunc_metadata_size(root, 1); |
| 5219 | |
Filipe David Borba Manana | 131e404 | 2013-11-19 22:29:35 +0000 | [diff] [blame] | 5220 | evict_inode_truncate_pages(inode); |
| 5221 | |
Stefan Behrens | 69e9c6c | 2013-09-05 16:58:43 +0200 | [diff] [blame] | 5222 | if (inode->i_nlink && |
| 5223 | ((btrfs_root_refs(&root->root_item) != 0 && |
| 5224 | root->root_key.objectid != BTRFS_ROOT_TREE_OBJECTID) || |
| 5225 | btrfs_is_free_space_inode(inode))) |
Al Viro | bd55597 | 2010-06-07 11:35:40 -0400 | [diff] [blame] | 5226 | goto no_delete; |
| 5227 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5228 | if (is_bad_inode(inode)) { |
Josef Bacik | 7b12876 | 2008-07-24 12:17:14 -0400 | [diff] [blame] | 5229 | btrfs_orphan_del(NULL, inode); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5230 | goto no_delete; |
| 5231 | } |
Al Viro | bd55597 | 2010-06-07 11:35:40 -0400 | [diff] [blame] | 5232 | /* 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] | 5233 | if (!special_file(inode->i_mode)) |
| 5234 | btrfs_wait_ordered_range(inode, 0, (u64)-1); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 5235 | |
Miao Xie | f612496 | 2014-09-12 18:44:04 +0800 | [diff] [blame] | 5236 | btrfs_free_io_failure_record(inode, 0, (u64)-1); |
| 5237 | |
Josef Bacik | afcdd12 | 2016-09-02 15:40:02 -0400 | [diff] [blame] | 5238 | if (test_bit(BTRFS_FS_LOG_RECOVERING, &root->fs_info->flags)) { |
Liu Bo | 6bf0231 | 2012-06-25 21:59:09 -0600 | [diff] [blame] | 5239 | BUG_ON(test_bit(BTRFS_INODE_HAS_ORPHAN_ITEM, |
Josef Bacik | 8a35d95 | 2012-05-23 14:26:42 -0400 | [diff] [blame] | 5240 | &BTRFS_I(inode)->runtime_flags)); |
Yan, Zheng | c71bf09 | 2009-11-12 09:34:40 +0000 | [diff] [blame] | 5241 | goto no_delete; |
| 5242 | } |
| 5243 | |
Yan, Zheng | 76dda93 | 2009-09-21 16:00:26 -0400 | [diff] [blame] | 5244 | if (inode->i_nlink > 0) { |
Stefan Behrens | 69e9c6c | 2013-09-05 16:58:43 +0200 | [diff] [blame] | 5245 | BUG_ON(btrfs_root_refs(&root->root_item) != 0 && |
| 5246 | root->root_key.objectid != BTRFS_ROOT_TREE_OBJECTID); |
Yan, Zheng | 76dda93 | 2009-09-21 16:00:26 -0400 | [diff] [blame] | 5247 | goto no_delete; |
| 5248 | } |
| 5249 | |
Miao Xie | 0e8c36a | 2012-12-19 06:59:51 +0000 | [diff] [blame] | 5250 | ret = btrfs_commit_inode_delayed_inode(inode); |
| 5251 | if (ret) { |
| 5252 | btrfs_orphan_del(NULL, inode); |
| 5253 | goto no_delete; |
| 5254 | } |
| 5255 | |
Miao Xie | 66d8f3d | 2012-09-06 04:02:28 -0600 | [diff] [blame] | 5256 | rsv = btrfs_alloc_block_rsv(root, BTRFS_BLOCK_RSV_TEMP); |
Josef Bacik | 4289a66 | 2011-08-05 13:22:24 -0400 | [diff] [blame] | 5257 | if (!rsv) { |
| 5258 | btrfs_orphan_del(NULL, inode); |
| 5259 | goto no_delete; |
| 5260 | } |
Josef Bacik | 4a33854 | 2011-08-29 11:01:31 -0400 | [diff] [blame] | 5261 | rsv->size = min_size; |
Josef Bacik | ca7e70f | 2012-08-27 17:48:15 -0400 | [diff] [blame] | 5262 | rsv->failfast = 1; |
Josef Bacik | 726c35f | 2011-09-26 15:46:06 -0400 | [diff] [blame] | 5263 | global_rsv = &root->fs_info->global_block_rsv; |
Josef Bacik | 4289a66 | 2011-08-05 13:22:24 -0400 | [diff] [blame] | 5264 | |
Chris Mason | dbe674a | 2008-07-17 12:54:05 -0400 | [diff] [blame] | 5265 | btrfs_i_size_write(inode, 0); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 5266 | |
Josef Bacik | 4289a66 | 2011-08-05 13:22:24 -0400 | [diff] [blame] | 5267 | /* |
Miao Xie | 8407aa4 | 2012-09-07 01:43:32 -0600 | [diff] [blame] | 5268 | * This is a bit simpler than btrfs_truncate since we've already |
| 5269 | * reserved our space for our orphan item in the unlink, so we just |
| 5270 | * need to reserve some slack space in case we add bytes and update |
| 5271 | * inode item when doing the truncate. |
Josef Bacik | 4289a66 | 2011-08-05 13:22:24 -0400 | [diff] [blame] | 5272 | */ |
Yan, Zheng | 8082510 | 2009-11-12 09:35:36 +0000 | [diff] [blame] | 5273 | while (1) { |
Miao Xie | 08e007d | 2012-10-16 11:33:38 +0000 | [diff] [blame] | 5274 | ret = btrfs_block_rsv_refill(root, rsv, min_size, |
| 5275 | BTRFS_RESERVE_FLUSH_LIMIT); |
Yan, Zheng | 8082510 | 2009-11-12 09:35:36 +0000 | [diff] [blame] | 5276 | |
Josef Bacik | 726c35f | 2011-09-26 15:46:06 -0400 | [diff] [blame] | 5277 | /* |
| 5278 | * Try and steal from the global reserve since we will |
| 5279 | * likely not use this space anyway, we want to try as |
| 5280 | * hard as possible to get this to work. |
| 5281 | */ |
| 5282 | if (ret) |
Josef Bacik | 3bce876 | 2015-02-24 12:35:51 -0800 | [diff] [blame] | 5283 | steal_from_global++; |
| 5284 | else |
| 5285 | steal_from_global = 0; |
| 5286 | ret = 0; |
Josef Bacik | 726c35f | 2011-09-26 15:46:06 -0400 | [diff] [blame] | 5287 | |
Josef Bacik | 3bce876 | 2015-02-24 12:35:51 -0800 | [diff] [blame] | 5288 | /* |
| 5289 | * steal_from_global == 0: we reserved stuff, hooray! |
| 5290 | * steal_from_global == 1: we didn't reserve stuff, boo! |
| 5291 | * steal_from_global == 2: we've committed, still not a lot of |
| 5292 | * room but maybe we'll have room in the global reserve this |
| 5293 | * time. |
| 5294 | * steal_from_global == 3: abandon all hope! |
| 5295 | */ |
| 5296 | if (steal_from_global > 2) { |
Simon Kirby | c2cf52e | 2013-03-19 22:41:23 +0000 | [diff] [blame] | 5297 | btrfs_warn(root->fs_info, |
| 5298 | "Could not get space for a delete, will truncate on mount %d", |
| 5299 | ret); |
Josef Bacik | 4289a66 | 2011-08-05 13:22:24 -0400 | [diff] [blame] | 5300 | btrfs_orphan_del(NULL, inode); |
| 5301 | btrfs_free_block_rsv(root, rsv); |
| 5302 | goto no_delete; |
Yan, Zheng | d68fc57 | 2010-05-16 10:49:58 -0400 | [diff] [blame] | 5303 | } |
| 5304 | |
Miao Xie | 0e8c36a | 2012-12-19 06:59:51 +0000 | [diff] [blame] | 5305 | trans = btrfs_join_transaction(root); |
Josef Bacik | 4289a66 | 2011-08-05 13:22:24 -0400 | [diff] [blame] | 5306 | if (IS_ERR(trans)) { |
| 5307 | btrfs_orphan_del(NULL, inode); |
| 5308 | btrfs_free_block_rsv(root, rsv); |
| 5309 | goto no_delete; |
| 5310 | } |
| 5311 | |
Josef Bacik | 3bce876 | 2015-02-24 12:35:51 -0800 | [diff] [blame] | 5312 | /* |
Nicholas D Steeves | 0132761 | 2016-05-19 21:18:45 -0400 | [diff] [blame] | 5313 | * 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] | 5314 | * sure there is room to do it, if not we need to commit and try |
| 5315 | * again. |
| 5316 | */ |
| 5317 | if (steal_from_global) { |
| 5318 | if (!btrfs_check_space_for_delayed_refs(trans, root)) |
| 5319 | ret = btrfs_block_rsv_migrate(global_rsv, rsv, |
Josef Bacik | 25d609f | 2016-03-25 13:25:48 -0400 | [diff] [blame] | 5320 | min_size, 0); |
Josef Bacik | 3bce876 | 2015-02-24 12:35:51 -0800 | [diff] [blame] | 5321 | else |
| 5322 | ret = -ENOSPC; |
| 5323 | } |
| 5324 | |
| 5325 | /* |
| 5326 | * Couldn't steal from the global reserve, we have too much |
| 5327 | * pending stuff built up, commit the transaction and try it |
| 5328 | * again. |
| 5329 | */ |
| 5330 | if (ret) { |
| 5331 | ret = btrfs_commit_transaction(trans, root); |
| 5332 | if (ret) { |
| 5333 | btrfs_orphan_del(NULL, inode); |
| 5334 | btrfs_free_block_rsv(root, rsv); |
| 5335 | goto no_delete; |
| 5336 | } |
| 5337 | continue; |
| 5338 | } else { |
| 5339 | steal_from_global = 0; |
| 5340 | } |
| 5341 | |
Josef Bacik | 4289a66 | 2011-08-05 13:22:24 -0400 | [diff] [blame] | 5342 | trans->block_rsv = rsv; |
| 5343 | |
Yan, Zheng | d68fc57 | 2010-05-16 10:49:58 -0400 | [diff] [blame] | 5344 | ret = btrfs_truncate_inode_items(trans, root, inode, 0, 0); |
Chris Mason | 28ed134 | 2014-12-17 09:41:04 -0800 | [diff] [blame] | 5345 | if (ret != -ENOSPC && ret != -EAGAIN) |
Yan, Zheng | 8082510 | 2009-11-12 09:35:36 +0000 | [diff] [blame] | 5346 | break; |
| 5347 | |
Miao Xie | 8407aa4 | 2012-09-07 01:43:32 -0600 | [diff] [blame] | 5348 | trans->block_rsv = &root->fs_info->trans_block_rsv; |
Yan, Zheng | 8082510 | 2009-11-12 09:35:36 +0000 | [diff] [blame] | 5349 | btrfs_end_transaction(trans, root); |
| 5350 | trans = NULL; |
Liu Bo | b53d3f5 | 2012-11-14 14:34:34 +0000 | [diff] [blame] | 5351 | btrfs_btree_balance_dirty(root); |
Josef Bacik | 7b12876 | 2008-07-24 12:17:14 -0400 | [diff] [blame] | 5352 | } |
| 5353 | |
Josef Bacik | 4289a66 | 2011-08-05 13:22:24 -0400 | [diff] [blame] | 5354 | btrfs_free_block_rsv(root, rsv); |
| 5355 | |
Josef Bacik | 4ef31a4 | 2013-08-13 14:10:08 -0400 | [diff] [blame] | 5356 | /* |
| 5357 | * Errors here aren't a big deal, it just means we leave orphan items |
| 5358 | * in the tree. They will be cleaned up on the next mount. |
| 5359 | */ |
Yan, Zheng | 8082510 | 2009-11-12 09:35:36 +0000 | [diff] [blame] | 5360 | if (ret == 0) { |
Josef Bacik | 4289a66 | 2011-08-05 13:22:24 -0400 | [diff] [blame] | 5361 | trans->block_rsv = root->orphan_block_rsv; |
Josef Bacik | 4ef31a4 | 2013-08-13 14:10:08 -0400 | [diff] [blame] | 5362 | btrfs_orphan_del(trans, inode); |
| 5363 | } else { |
| 5364 | btrfs_orphan_del(NULL, inode); |
Yan, Zheng | 8082510 | 2009-11-12 09:35:36 +0000 | [diff] [blame] | 5365 | } |
Chris Mason | 85e21ba | 2008-01-29 15:11:36 -0500 | [diff] [blame] | 5366 | |
Josef Bacik | 4289a66 | 2011-08-05 13:22:24 -0400 | [diff] [blame] | 5367 | trans->block_rsv = &root->fs_info->trans_block_rsv; |
Li Zefan | 581bb05 | 2011-04-20 10:06:11 +0800 | [diff] [blame] | 5368 | if (!(root == root->fs_info->tree_root || |
| 5369 | root->root_key.objectid == BTRFS_TREE_RELOC_OBJECTID)) |
Li Zefan | 33345d01 | 2011-04-20 10:31:50 +0800 | [diff] [blame] | 5370 | btrfs_return_ino(root, btrfs_ino(inode)); |
Li Zefan | 581bb05 | 2011-04-20 10:06:11 +0800 | [diff] [blame] | 5371 | |
Chris Mason | 54aa1f4 | 2007-06-22 14:16:25 -0400 | [diff] [blame] | 5372 | btrfs_end_transaction(trans, root); |
Liu Bo | b53d3f5 | 2012-11-14 14:34:34 +0000 | [diff] [blame] | 5373 | btrfs_btree_balance_dirty(root); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5374 | no_delete: |
Miao Xie | 89042e5 | 2013-05-15 07:48:15 +0000 | [diff] [blame] | 5375 | btrfs_remove_delayed_node(inode); |
Jan Kara | dbd5768 | 2012-05-03 14:48:02 +0200 | [diff] [blame] | 5376 | clear_inode(inode); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5377 | } |
| 5378 | |
| 5379 | /* |
| 5380 | * this returns the key found in the dir entry in the location pointer. |
| 5381 | * If no dir entries were found, location->objectid is 0. |
| 5382 | */ |
| 5383 | static int btrfs_inode_by_name(struct inode *dir, struct dentry *dentry, |
| 5384 | struct btrfs_key *location) |
| 5385 | { |
| 5386 | const char *name = dentry->d_name.name; |
| 5387 | int namelen = dentry->d_name.len; |
| 5388 | struct btrfs_dir_item *di; |
| 5389 | struct btrfs_path *path; |
| 5390 | struct btrfs_root *root = BTRFS_I(dir)->root; |
Yan | 0d9f7f3 | 2007-10-25 15:48:28 -0400 | [diff] [blame] | 5391 | int ret = 0; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5392 | |
| 5393 | path = btrfs_alloc_path(); |
Mark Fasheh | d8926bb | 2011-07-13 10:38:47 -0700 | [diff] [blame] | 5394 | if (!path) |
| 5395 | return -ENOMEM; |
Chris Mason | 3954401 | 2007-12-12 14:38:19 -0500 | [diff] [blame] | 5396 | |
Li Zefan | 33345d01 | 2011-04-20 10:31:50 +0800 | [diff] [blame] | 5397 | di = btrfs_lookup_dir_item(NULL, root, path, btrfs_ino(dir), name, |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5398 | namelen, 0); |
Yan | 0d9f7f3 | 2007-10-25 15:48:28 -0400 | [diff] [blame] | 5399 | if (IS_ERR(di)) |
| 5400 | ret = PTR_ERR(di); |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 5401 | |
David Sterba | c704005 | 2011-04-19 18:00:01 +0200 | [diff] [blame] | 5402 | if (IS_ERR_OR_NULL(di)) |
Chris Mason | 3954401 | 2007-12-12 14:38:19 -0500 | [diff] [blame] | 5403 | goto out_err; |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 5404 | |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 5405 | btrfs_dir_item_key_to_cpu(path->nodes[0], di, location); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5406 | out: |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5407 | btrfs_free_path(path); |
| 5408 | return ret; |
Chris Mason | 3954401 | 2007-12-12 14:38:19 -0500 | [diff] [blame] | 5409 | out_err: |
| 5410 | location->objectid = 0; |
| 5411 | goto out; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5412 | } |
| 5413 | |
| 5414 | /* |
| 5415 | * when we hit a tree root in a directory, the btrfs part of the inode |
| 5416 | * needs to be changed to reflect the root directory of the tree root. This |
| 5417 | * is kind of like crossing a mount point. |
| 5418 | */ |
| 5419 | static int fixup_tree_root_location(struct btrfs_root *root, |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 5420 | struct inode *dir, |
| 5421 | struct dentry *dentry, |
| 5422 | struct btrfs_key *location, |
| 5423 | struct btrfs_root **sub_root) |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5424 | { |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 5425 | struct btrfs_path *path; |
| 5426 | struct btrfs_root *new_root; |
| 5427 | struct btrfs_root_ref *ref; |
| 5428 | struct extent_buffer *leaf; |
David Sterba | 1d4c08e | 2015-01-02 19:36:14 +0100 | [diff] [blame] | 5429 | struct btrfs_key key; |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 5430 | int ret; |
| 5431 | int err = 0; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5432 | |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 5433 | path = btrfs_alloc_path(); |
| 5434 | if (!path) { |
| 5435 | err = -ENOMEM; |
| 5436 | goto out; |
| 5437 | } |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5438 | |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 5439 | err = -ENOENT; |
David Sterba | 1d4c08e | 2015-01-02 19:36:14 +0100 | [diff] [blame] | 5440 | key.objectid = BTRFS_I(dir)->root->root_key.objectid; |
| 5441 | key.type = BTRFS_ROOT_REF_KEY; |
| 5442 | key.offset = location->objectid; |
| 5443 | |
| 5444 | ret = btrfs_search_slot(NULL, root->fs_info->tree_root, &key, path, |
| 5445 | 0, 0); |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 5446 | if (ret) { |
| 5447 | if (ret < 0) |
| 5448 | err = ret; |
| 5449 | goto out; |
| 5450 | } |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5451 | |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 5452 | leaf = path->nodes[0]; |
| 5453 | ref = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_root_ref); |
Li Zefan | 33345d01 | 2011-04-20 10:31:50 +0800 | [diff] [blame] | 5454 | if (btrfs_root_ref_dirid(leaf, ref) != btrfs_ino(dir) || |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 5455 | btrfs_root_ref_name_len(leaf, ref) != dentry->d_name.len) |
| 5456 | goto out; |
| 5457 | |
| 5458 | ret = memcmp_extent_buffer(leaf, dentry->d_name.name, |
| 5459 | (unsigned long)(ref + 1), |
| 5460 | dentry->d_name.len); |
| 5461 | if (ret) |
| 5462 | goto out; |
| 5463 | |
David Sterba | b3b4aa7 | 2011-04-21 01:20:15 +0200 | [diff] [blame] | 5464 | btrfs_release_path(path); |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 5465 | |
| 5466 | new_root = btrfs_read_fs_root_no_name(root->fs_info, location); |
| 5467 | if (IS_ERR(new_root)) { |
| 5468 | err = PTR_ERR(new_root); |
| 5469 | goto out; |
| 5470 | } |
| 5471 | |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 5472 | *sub_root = new_root; |
| 5473 | location->objectid = btrfs_root_dirid(&new_root->root_item); |
| 5474 | location->type = BTRFS_INODE_ITEM_KEY; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5475 | location->offset = 0; |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 5476 | err = 0; |
| 5477 | out: |
| 5478 | btrfs_free_path(path); |
| 5479 | return err; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5480 | } |
| 5481 | |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 5482 | static void inode_tree_add(struct inode *inode) |
| 5483 | { |
| 5484 | struct btrfs_root *root = BTRFS_I(inode)->root; |
| 5485 | struct btrfs_inode *entry; |
From: Nick Piggin | 03e860b | 2009-08-21 10:09:44 +0200 | [diff] [blame] | 5486 | struct rb_node **p; |
| 5487 | struct rb_node *parent; |
Filipe David Borba Manana | cef2193 | 2013-09-02 12:19:13 +0100 | [diff] [blame] | 5488 | struct rb_node *new = &BTRFS_I(inode)->rb_node; |
Li Zefan | 33345d01 | 2011-04-20 10:31:50 +0800 | [diff] [blame] | 5489 | u64 ino = btrfs_ino(inode); |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 5490 | |
Al Viro | 1d3382cb | 2010-10-23 15:19:20 -0400 | [diff] [blame] | 5491 | if (inode_unhashed(inode)) |
Yan, Zheng | 76dda93 | 2009-09-21 16:00:26 -0400 | [diff] [blame] | 5492 | return; |
Miao Xie | e1409ce | 2013-05-15 07:48:16 +0000 | [diff] [blame] | 5493 | parent = NULL; |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 5494 | spin_lock(&root->inode_lock); |
Miao Xie | e1409ce | 2013-05-15 07:48:16 +0000 | [diff] [blame] | 5495 | p = &root->inode_tree.rb_node; |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 5496 | while (*p) { |
| 5497 | parent = *p; |
| 5498 | entry = rb_entry(parent, struct btrfs_inode, rb_node); |
| 5499 | |
Li Zefan | 33345d01 | 2011-04-20 10:31:50 +0800 | [diff] [blame] | 5500 | if (ino < btrfs_ino(&entry->vfs_inode)) |
From: Nick Piggin | 03e860b | 2009-08-21 10:09:44 +0200 | [diff] [blame] | 5501 | p = &parent->rb_left; |
Li Zefan | 33345d01 | 2011-04-20 10:31:50 +0800 | [diff] [blame] | 5502 | else if (ino > btrfs_ino(&entry->vfs_inode)) |
From: Nick Piggin | 03e860b | 2009-08-21 10:09:44 +0200 | [diff] [blame] | 5503 | p = &parent->rb_right; |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 5504 | else { |
| 5505 | WARN_ON(!(entry->vfs_inode.i_state & |
Al Viro | a4ffdde | 2010-06-02 17:38:30 -0400 | [diff] [blame] | 5506 | (I_WILL_FREE | I_FREEING))); |
Filipe David Borba Manana | cef2193 | 2013-09-02 12:19:13 +0100 | [diff] [blame] | 5507 | rb_replace_node(parent, new, &root->inode_tree); |
From: Nick Piggin | 03e860b | 2009-08-21 10:09:44 +0200 | [diff] [blame] | 5508 | RB_CLEAR_NODE(parent); |
| 5509 | spin_unlock(&root->inode_lock); |
Filipe David Borba Manana | cef2193 | 2013-09-02 12:19:13 +0100 | [diff] [blame] | 5510 | return; |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 5511 | } |
| 5512 | } |
Filipe David Borba Manana | cef2193 | 2013-09-02 12:19:13 +0100 | [diff] [blame] | 5513 | rb_link_node(new, parent, p); |
| 5514 | rb_insert_color(new, &root->inode_tree); |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 5515 | spin_unlock(&root->inode_lock); |
| 5516 | } |
| 5517 | |
| 5518 | static void inode_tree_del(struct inode *inode) |
| 5519 | { |
| 5520 | struct btrfs_root *root = BTRFS_I(inode)->root; |
Yan, Zheng | 76dda93 | 2009-09-21 16:00:26 -0400 | [diff] [blame] | 5521 | int empty = 0; |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 5522 | |
From: Nick Piggin | 03e860b | 2009-08-21 10:09:44 +0200 | [diff] [blame] | 5523 | spin_lock(&root->inode_lock); |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 5524 | if (!RB_EMPTY_NODE(&BTRFS_I(inode)->rb_node)) { |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 5525 | rb_erase(&BTRFS_I(inode)->rb_node, &root->inode_tree); |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 5526 | RB_CLEAR_NODE(&BTRFS_I(inode)->rb_node); |
Yan, Zheng | 76dda93 | 2009-09-21 16:00:26 -0400 | [diff] [blame] | 5527 | empty = RB_EMPTY_ROOT(&root->inode_tree); |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 5528 | } |
From: Nick Piggin | 03e860b | 2009-08-21 10:09:44 +0200 | [diff] [blame] | 5529 | spin_unlock(&root->inode_lock); |
Yan, Zheng | 76dda93 | 2009-09-21 16:00:26 -0400 | [diff] [blame] | 5530 | |
Stefan Behrens | 69e9c6c | 2013-09-05 16:58:43 +0200 | [diff] [blame] | 5531 | if (empty && btrfs_root_refs(&root->root_item) == 0) { |
Yan, Zheng | 76dda93 | 2009-09-21 16:00:26 -0400 | [diff] [blame] | 5532 | synchronize_srcu(&root->fs_info->subvol_srcu); |
| 5533 | spin_lock(&root->inode_lock); |
| 5534 | empty = RB_EMPTY_ROOT(&root->inode_tree); |
| 5535 | spin_unlock(&root->inode_lock); |
| 5536 | if (empty) |
| 5537 | btrfs_add_dead_root(root); |
| 5538 | } |
| 5539 | } |
| 5540 | |
Jeff Mahoney | 143bede | 2012-03-01 14:56:26 +0100 | [diff] [blame] | 5541 | void btrfs_invalidate_inodes(struct btrfs_root *root) |
Yan, Zheng | 76dda93 | 2009-09-21 16:00:26 -0400 | [diff] [blame] | 5542 | { |
| 5543 | struct rb_node *node; |
| 5544 | struct rb_node *prev; |
| 5545 | struct btrfs_inode *entry; |
| 5546 | struct inode *inode; |
| 5547 | u64 objectid = 0; |
| 5548 | |
Liu Bo | 7813b3d | 2014-02-10 17:37:25 +0800 | [diff] [blame] | 5549 | if (!test_bit(BTRFS_FS_STATE_ERROR, &root->fs_info->fs_state)) |
| 5550 | WARN_ON(btrfs_root_refs(&root->root_item) != 0); |
Yan, Zheng | 76dda93 | 2009-09-21 16:00:26 -0400 | [diff] [blame] | 5551 | |
| 5552 | spin_lock(&root->inode_lock); |
| 5553 | again: |
| 5554 | node = root->inode_tree.rb_node; |
| 5555 | prev = NULL; |
| 5556 | while (node) { |
| 5557 | prev = node; |
| 5558 | entry = rb_entry(node, struct btrfs_inode, rb_node); |
| 5559 | |
Li Zefan | 33345d01 | 2011-04-20 10:31:50 +0800 | [diff] [blame] | 5560 | if (objectid < btrfs_ino(&entry->vfs_inode)) |
Yan, Zheng | 76dda93 | 2009-09-21 16:00:26 -0400 | [diff] [blame] | 5561 | node = node->rb_left; |
Li Zefan | 33345d01 | 2011-04-20 10:31:50 +0800 | [diff] [blame] | 5562 | else if (objectid > btrfs_ino(&entry->vfs_inode)) |
Yan, Zheng | 76dda93 | 2009-09-21 16:00:26 -0400 | [diff] [blame] | 5563 | node = node->rb_right; |
| 5564 | else |
| 5565 | break; |
| 5566 | } |
| 5567 | if (!node) { |
| 5568 | while (prev) { |
| 5569 | entry = rb_entry(prev, struct btrfs_inode, rb_node); |
Li Zefan | 33345d01 | 2011-04-20 10:31:50 +0800 | [diff] [blame] | 5570 | if (objectid <= btrfs_ino(&entry->vfs_inode)) { |
Yan, Zheng | 76dda93 | 2009-09-21 16:00:26 -0400 | [diff] [blame] | 5571 | node = prev; |
| 5572 | break; |
| 5573 | } |
| 5574 | prev = rb_next(prev); |
| 5575 | } |
| 5576 | } |
| 5577 | while (node) { |
| 5578 | entry = rb_entry(node, struct btrfs_inode, rb_node); |
Li Zefan | 33345d01 | 2011-04-20 10:31:50 +0800 | [diff] [blame] | 5579 | objectid = btrfs_ino(&entry->vfs_inode) + 1; |
Yan, Zheng | 76dda93 | 2009-09-21 16:00:26 -0400 | [diff] [blame] | 5580 | inode = igrab(&entry->vfs_inode); |
| 5581 | if (inode) { |
| 5582 | spin_unlock(&root->inode_lock); |
| 5583 | if (atomic_read(&inode->i_count) > 1) |
| 5584 | d_prune_aliases(inode); |
| 5585 | /* |
Al Viro | 45321ac | 2010-06-07 13:43:19 -0400 | [diff] [blame] | 5586 | * btrfs_drop_inode will have it removed from |
Yan, Zheng | 76dda93 | 2009-09-21 16:00:26 -0400 | [diff] [blame] | 5587 | * the inode cache when its usage count |
| 5588 | * hits zero. |
| 5589 | */ |
| 5590 | iput(inode); |
| 5591 | cond_resched(); |
| 5592 | spin_lock(&root->inode_lock); |
| 5593 | goto again; |
| 5594 | } |
| 5595 | |
| 5596 | if (cond_resched_lock(&root->inode_lock)) |
| 5597 | goto again; |
| 5598 | |
| 5599 | node = rb_next(node); |
| 5600 | } |
| 5601 | spin_unlock(&root->inode_lock); |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 5602 | } |
| 5603 | |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 5604 | static int btrfs_init_locked_inode(struct inode *inode, void *p) |
| 5605 | { |
| 5606 | struct btrfs_iget_args *args = p; |
Chris Mason | 90d3e59 | 2014-01-09 17:28:00 -0800 | [diff] [blame] | 5607 | inode->i_ino = args->location->objectid; |
| 5608 | memcpy(&BTRFS_I(inode)->location, args->location, |
| 5609 | sizeof(*args->location)); |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 5610 | BTRFS_I(inode)->root = args->root; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5611 | return 0; |
| 5612 | } |
| 5613 | |
| 5614 | static int btrfs_find_actor(struct inode *inode, void *opaque) |
| 5615 | { |
| 5616 | struct btrfs_iget_args *args = opaque; |
Chris Mason | 90d3e59 | 2014-01-09 17:28:00 -0800 | [diff] [blame] | 5617 | return args->location->objectid == BTRFS_I(inode)->location.objectid && |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 5618 | args->root == BTRFS_I(inode)->root; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5619 | } |
| 5620 | |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 5621 | static struct inode *btrfs_iget_locked(struct super_block *s, |
Chris Mason | 90d3e59 | 2014-01-09 17:28:00 -0800 | [diff] [blame] | 5622 | struct btrfs_key *location, |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 5623 | struct btrfs_root *root) |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5624 | { |
| 5625 | struct inode *inode; |
| 5626 | struct btrfs_iget_args args; |
Chris Mason | 90d3e59 | 2014-01-09 17:28:00 -0800 | [diff] [blame] | 5627 | unsigned long hashval = btrfs_inode_hash(location->objectid, root); |
Filipe David Borba Manana | 778ba82 | 2013-10-06 22:22:33 +0100 | [diff] [blame] | 5628 | |
Chris Mason | 90d3e59 | 2014-01-09 17:28:00 -0800 | [diff] [blame] | 5629 | args.location = location; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5630 | args.root = root; |
| 5631 | |
Filipe David Borba Manana | 778ba82 | 2013-10-06 22:22:33 +0100 | [diff] [blame] | 5632 | inode = iget5_locked(s, hashval, btrfs_find_actor, |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5633 | btrfs_init_locked_inode, |
| 5634 | (void *)&args); |
| 5635 | return inode; |
| 5636 | } |
| 5637 | |
Balaji Rao | 1a54ef8 | 2008-07-21 02:01:04 +0530 | [diff] [blame] | 5638 | /* Get an inode object given its location and corresponding root. |
| 5639 | * Returns in *is_new if the inode was read from disk |
| 5640 | */ |
| 5641 | struct inode *btrfs_iget(struct super_block *s, struct btrfs_key *location, |
Josef Bacik | 73f7341 | 2009-12-04 17:38:27 +0000 | [diff] [blame] | 5642 | struct btrfs_root *root, int *new) |
Balaji Rao | 1a54ef8 | 2008-07-21 02:01:04 +0530 | [diff] [blame] | 5643 | { |
| 5644 | struct inode *inode; |
| 5645 | |
Chris Mason | 90d3e59 | 2014-01-09 17:28:00 -0800 | [diff] [blame] | 5646 | inode = btrfs_iget_locked(s, location, root); |
Balaji Rao | 1a54ef8 | 2008-07-21 02:01:04 +0530 | [diff] [blame] | 5647 | if (!inode) |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 5648 | return ERR_PTR(-ENOMEM); |
Balaji Rao | 1a54ef8 | 2008-07-21 02:01:04 +0530 | [diff] [blame] | 5649 | |
| 5650 | if (inode->i_state & I_NEW) { |
Filipe Manana | 6771089 | 2016-06-06 11:51:25 +0100 | [diff] [blame] | 5651 | int ret; |
| 5652 | |
| 5653 | ret = btrfs_read_locked_inode(inode); |
Mark Fasheh | 1748f84 | 2011-07-12 11:25:31 -0700 | [diff] [blame] | 5654 | if (!is_bad_inode(inode)) { |
| 5655 | inode_tree_add(inode); |
| 5656 | unlock_new_inode(inode); |
| 5657 | if (new) |
| 5658 | *new = 1; |
| 5659 | } else { |
Sergei Trofimovich | e0b6d65 | 2011-09-11 10:52:24 -0400 | [diff] [blame] | 5660 | unlock_new_inode(inode); |
| 5661 | iput(inode); |
Filipe Manana | 6771089 | 2016-06-06 11:51:25 +0100 | [diff] [blame] | 5662 | ASSERT(ret < 0); |
| 5663 | inode = ERR_PTR(ret < 0 ? ret : -ESTALE); |
Mark Fasheh | 1748f84 | 2011-07-12 11:25:31 -0700 | [diff] [blame] | 5664 | } |
| 5665 | } |
| 5666 | |
Balaji Rao | 1a54ef8 | 2008-07-21 02:01:04 +0530 | [diff] [blame] | 5667 | return inode; |
| 5668 | } |
| 5669 | |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 5670 | static struct inode *new_simple_dir(struct super_block *s, |
| 5671 | struct btrfs_key *key, |
| 5672 | struct btrfs_root *root) |
| 5673 | { |
| 5674 | struct inode *inode = new_inode(s); |
| 5675 | |
| 5676 | if (!inode) |
| 5677 | return ERR_PTR(-ENOMEM); |
| 5678 | |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 5679 | BTRFS_I(inode)->root = root; |
| 5680 | memcpy(&BTRFS_I(inode)->location, key, sizeof(*key)); |
Josef Bacik | 72ac3c0 | 2012-05-23 14:13:11 -0400 | [diff] [blame] | 5681 | set_bit(BTRFS_INODE_DUMMY, &BTRFS_I(inode)->runtime_flags); |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 5682 | |
| 5683 | inode->i_ino = BTRFS_EMPTY_SUBVOL_DIR_OBJECTID; |
Li Zefan | 848cce0 | 2012-02-21 17:04:28 +0800 | [diff] [blame] | 5684 | inode->i_op = &btrfs_dir_ro_inode_operations; |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 5685 | inode->i_fop = &simple_dir_operations; |
| 5686 | inode->i_mode = S_IFDIR | S_IRUGO | S_IWUSR | S_IXUGO; |
Deepa Dinamani | c2050a4 | 2016-09-14 07:48:06 -0700 | [diff] [blame] | 5687 | inode->i_mtime = current_time(inode); |
chandan r | 9cc97d6 | 2012-07-04 12:48:07 +0530 | [diff] [blame] | 5688 | inode->i_atime = inode->i_mtime; |
| 5689 | inode->i_ctime = inode->i_mtime; |
| 5690 | BTRFS_I(inode)->i_otime = inode->i_mtime; |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 5691 | |
| 5692 | return inode; |
| 5693 | } |
| 5694 | |
Chris Mason | 3de4586 | 2008-11-17 21:02:50 -0500 | [diff] [blame] | 5695 | struct inode *btrfs_lookup_dentry(struct inode *dir, struct dentry *dentry) |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5696 | { |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 5697 | struct inode *inode; |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 5698 | struct btrfs_root *root = BTRFS_I(dir)->root; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5699 | struct btrfs_root *sub_root = root; |
| 5700 | struct btrfs_key location; |
Yan, Zheng | 76dda93 | 2009-09-21 16:00:26 -0400 | [diff] [blame] | 5701 | int index; |
Josef Bacik | b4aff1f | 2011-06-28 16:18:59 -0400 | [diff] [blame] | 5702 | int ret = 0; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5703 | |
| 5704 | if (dentry->d_name.len > BTRFS_NAME_LEN) |
| 5705 | return ERR_PTR(-ENAMETOOLONG); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 5706 | |
Jeff Layton | 39e3c95 | 2012-11-28 11:30:53 -0500 | [diff] [blame] | 5707 | ret = btrfs_inode_by_name(dir, dentry, &location); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5708 | if (ret < 0) |
| 5709 | return ERR_PTR(ret); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 5710 | |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 5711 | if (location.objectid == 0) |
Tsutomu Itoh | 5662344b3 | 2013-12-13 09:51:42 +0900 | [diff] [blame] | 5712 | return ERR_PTR(-ENOENT); |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 5713 | |
| 5714 | if (location.type == BTRFS_INODE_ITEM_KEY) { |
Josef Bacik | 73f7341 | 2009-12-04 17:38:27 +0000 | [diff] [blame] | 5715 | inode = btrfs_iget(dir->i_sb, &location, root, NULL); |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 5716 | return inode; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5717 | } |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 5718 | |
| 5719 | BUG_ON(location.type != BTRFS_ROOT_ITEM_KEY); |
| 5720 | |
Yan, Zheng | 76dda93 | 2009-09-21 16:00:26 -0400 | [diff] [blame] | 5721 | index = srcu_read_lock(&root->fs_info->subvol_srcu); |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 5722 | ret = fixup_tree_root_location(root, dir, dentry, |
| 5723 | &location, &sub_root); |
| 5724 | if (ret < 0) { |
| 5725 | if (ret != -ENOENT) |
| 5726 | inode = ERR_PTR(ret); |
| 5727 | else |
| 5728 | inode = new_simple_dir(dir->i_sb, &location, sub_root); |
| 5729 | } else { |
Josef Bacik | 73f7341 | 2009-12-04 17:38:27 +0000 | [diff] [blame] | 5730 | inode = btrfs_iget(dir->i_sb, &location, sub_root, NULL); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5731 | } |
Yan, Zheng | 76dda93 | 2009-09-21 16:00:26 -0400 | [diff] [blame] | 5732 | srcu_read_unlock(&root->fs_info->subvol_srcu, index); |
| 5733 | |
Julia Lawall | 34d19ba | 2011-01-24 19:55:19 +0000 | [diff] [blame] | 5734 | if (!IS_ERR(inode) && root != sub_root) { |
Yan, Zheng | c71bf09 | 2009-11-12 09:34:40 +0000 | [diff] [blame] | 5735 | down_read(&root->fs_info->cleanup_work_sem); |
| 5736 | if (!(inode->i_sb->s_flags & MS_RDONLY)) |
Josef Bacik | 66b4ffd | 2011-01-31 16:22:42 -0500 | [diff] [blame] | 5737 | ret = btrfs_orphan_cleanup(sub_root); |
Yan, Zheng | c71bf09 | 2009-11-12 09:34:40 +0000 | [diff] [blame] | 5738 | up_read(&root->fs_info->cleanup_work_sem); |
Josef Bacik | 01cd336 | 2013-06-03 21:39:49 -0400 | [diff] [blame] | 5739 | if (ret) { |
| 5740 | iput(inode); |
Josef Bacik | 66b4ffd | 2011-01-31 16:22:42 -0500 | [diff] [blame] | 5741 | inode = ERR_PTR(ret); |
Josef Bacik | 01cd336 | 2013-06-03 21:39:49 -0400 | [diff] [blame] | 5742 | } |
Yan, Zheng | c71bf09 | 2009-11-12 09:34:40 +0000 | [diff] [blame] | 5743 | } |
| 5744 | |
Chris Mason | 3de4586 | 2008-11-17 21:02:50 -0500 | [diff] [blame] | 5745 | return inode; |
| 5746 | } |
| 5747 | |
Nick Piggin | fe15ce4 | 2011-01-07 17:49:23 +1100 | [diff] [blame] | 5748 | static int btrfs_dentry_delete(const struct dentry *dentry) |
Yan, Zheng | 76dda93 | 2009-09-21 16:00:26 -0400 | [diff] [blame] | 5749 | { |
| 5750 | struct btrfs_root *root; |
David Howells | 2b0143b | 2015-03-17 22:25:59 +0000 | [diff] [blame] | 5751 | struct inode *inode = d_inode(dentry); |
Yan, Zheng | 76dda93 | 2009-09-21 16:00:26 -0400 | [diff] [blame] | 5752 | |
Li Zefan | 848cce0 | 2012-02-21 17:04:28 +0800 | [diff] [blame] | 5753 | if (!inode && !IS_ROOT(dentry)) |
David Howells | 2b0143b | 2015-03-17 22:25:59 +0000 | [diff] [blame] | 5754 | inode = d_inode(dentry->d_parent); |
Yan, Zheng | 76dda93 | 2009-09-21 16:00:26 -0400 | [diff] [blame] | 5755 | |
Li Zefan | 848cce0 | 2012-02-21 17:04:28 +0800 | [diff] [blame] | 5756 | if (inode) { |
| 5757 | root = BTRFS_I(inode)->root; |
Yan, Zheng | efefb14 | 2009-10-09 09:25:16 -0400 | [diff] [blame] | 5758 | if (btrfs_root_refs(&root->root_item) == 0) |
| 5759 | return 1; |
Li Zefan | 848cce0 | 2012-02-21 17:04:28 +0800 | [diff] [blame] | 5760 | |
| 5761 | if (btrfs_ino(inode) == BTRFS_EMPTY_SUBVOL_DIR_OBJECTID) |
| 5762 | return 1; |
Yan, Zheng | efefb14 | 2009-10-09 09:25:16 -0400 | [diff] [blame] | 5763 | } |
Yan, Zheng | 76dda93 | 2009-09-21 16:00:26 -0400 | [diff] [blame] | 5764 | return 0; |
| 5765 | } |
| 5766 | |
Josef Bacik | b4aff1f | 2011-06-28 16:18:59 -0400 | [diff] [blame] | 5767 | static void btrfs_dentry_release(struct dentry *dentry) |
| 5768 | { |
Daeseok Youn | 944a451 | 2014-04-14 15:37:02 +0900 | [diff] [blame] | 5769 | kfree(dentry->d_fsdata); |
Josef Bacik | b4aff1f | 2011-06-28 16:18:59 -0400 | [diff] [blame] | 5770 | } |
| 5771 | |
Chris Mason | 3de4586 | 2008-11-17 21:02:50 -0500 | [diff] [blame] | 5772 | static struct dentry *btrfs_lookup(struct inode *dir, struct dentry *dentry, |
Al Viro | 00cd8dd | 2012-06-10 17:13:09 -0400 | [diff] [blame] | 5773 | unsigned int flags) |
Chris Mason | 3de4586 | 2008-11-17 21:02:50 -0500 | [diff] [blame] | 5774 | { |
Tsutomu Itoh | 5662344b3 | 2013-12-13 09:51:42 +0900 | [diff] [blame] | 5775 | struct inode *inode; |
Josef Bacik | a66e7cc | 2011-09-18 10:34:03 -0400 | [diff] [blame] | 5776 | |
Tsutomu Itoh | 5662344b3 | 2013-12-13 09:51:42 +0900 | [diff] [blame] | 5777 | inode = btrfs_lookup_dentry(dir, dentry); |
| 5778 | if (IS_ERR(inode)) { |
| 5779 | if (PTR_ERR(inode) == -ENOENT) |
| 5780 | inode = NULL; |
| 5781 | else |
| 5782 | return ERR_CAST(inode); |
| 5783 | } |
| 5784 | |
Al Viro | 41d28bc | 2014-10-12 22:24:21 -0400 | [diff] [blame] | 5785 | return d_splice_alias(inode, dentry); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5786 | } |
| 5787 | |
Miao Xie | 16cdcec | 2011-04-22 18:12:22 +0800 | [diff] [blame] | 5788 | unsigned char btrfs_filetype_table[] = { |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5789 | DT_UNKNOWN, DT_REG, DT_DIR, DT_CHR, DT_BLK, DT_FIFO, DT_SOCK, DT_LNK |
| 5790 | }; |
| 5791 | |
Al Viro | 9cdda8d | 2013-05-22 16:48:09 -0400 | [diff] [blame] | 5792 | static int btrfs_real_readdir(struct file *file, struct dir_context *ctx) |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5793 | { |
Al Viro | 9cdda8d | 2013-05-22 16:48:09 -0400 | [diff] [blame] | 5794 | struct inode *inode = file_inode(file); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5795 | struct btrfs_root *root = BTRFS_I(inode)->root; |
| 5796 | struct btrfs_item *item; |
| 5797 | struct btrfs_dir_item *di; |
| 5798 | struct btrfs_key key; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 5799 | struct btrfs_key found_key; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5800 | struct btrfs_path *path; |
Miao Xie | 16cdcec | 2011-04-22 18:12:22 +0800 | [diff] [blame] | 5801 | struct list_head ins_list; |
| 5802 | struct list_head del_list; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5803 | int ret; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 5804 | struct extent_buffer *leaf; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5805 | int slot; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5806 | unsigned char d_type; |
| 5807 | int over = 0; |
| 5808 | u32 di_cur; |
| 5809 | u32 di_total; |
| 5810 | u32 di_len; |
| 5811 | int key_type = BTRFS_DIR_INDEX_KEY; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 5812 | char tmp_name[32]; |
| 5813 | char *name_ptr; |
| 5814 | int name_len; |
Al Viro | 9cdda8d | 2013-05-22 16:48:09 -0400 | [diff] [blame] | 5815 | int is_curr = 0; /* ctx->pos points to the current index? */ |
David Sterba | bc4ef75 | 2015-11-13 13:44:28 +0100 | [diff] [blame] | 5816 | bool emitted; |
Omar Sandoval | 02dbfc9 | 2016-05-20 13:50:33 -0700 | [diff] [blame] | 5817 | bool put = false; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5818 | |
| 5819 | /* FIXME, use a real flag for deciding about the key type */ |
| 5820 | if (root->fs_info->tree_root == root) |
| 5821 | key_type = BTRFS_DIR_ITEM_KEY; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 5822 | |
Al Viro | 9cdda8d | 2013-05-22 16:48:09 -0400 | [diff] [blame] | 5823 | if (!dir_emit_dots(file, ctx)) |
| 5824 | return 0; |
| 5825 | |
David Woodhouse | 49593bf | 2008-08-17 17:08:36 +0100 | [diff] [blame] | 5826 | path = btrfs_alloc_path(); |
Miao Xie | 16cdcec | 2011-04-22 18:12:22 +0800 | [diff] [blame] | 5827 | if (!path) |
| 5828 | return -ENOMEM; |
Chris Mason | ff5714c | 2011-05-28 07:00:39 -0400 | [diff] [blame] | 5829 | |
David Sterba | e4058b5 | 2015-11-27 16:31:35 +0100 | [diff] [blame] | 5830 | path->reada = READA_FORWARD; |
David Woodhouse | 49593bf | 2008-08-17 17:08:36 +0100 | [diff] [blame] | 5831 | |
Miao Xie | 16cdcec | 2011-04-22 18:12:22 +0800 | [diff] [blame] | 5832 | if (key_type == BTRFS_DIR_INDEX_KEY) { |
| 5833 | INIT_LIST_HEAD(&ins_list); |
| 5834 | INIT_LIST_HEAD(&del_list); |
Omar Sandoval | 02dbfc9 | 2016-05-20 13:50:33 -0700 | [diff] [blame] | 5835 | put = btrfs_readdir_get_delayed_items(inode, &ins_list, |
| 5836 | &del_list); |
Miao Xie | 16cdcec | 2011-04-22 18:12:22 +0800 | [diff] [blame] | 5837 | } |
| 5838 | |
David Sterba | 962a298 | 2014-06-04 18:41:45 +0200 | [diff] [blame] | 5839 | key.type = key_type; |
Al Viro | 9cdda8d | 2013-05-22 16:48:09 -0400 | [diff] [blame] | 5840 | key.offset = ctx->pos; |
Li Zefan | 33345d01 | 2011-04-20 10:31:50 +0800 | [diff] [blame] | 5841 | key.objectid = btrfs_ino(inode); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 5842 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5843 | ret = btrfs_search_slot(NULL, root, &key, path, 0, 0); |
| 5844 | if (ret < 0) |
| 5845 | goto err; |
David Woodhouse | 49593bf | 2008-08-17 17:08:36 +0100 | [diff] [blame] | 5846 | |
David Sterba | bc4ef75 | 2015-11-13 13:44:28 +0100 | [diff] [blame] | 5847 | emitted = false; |
David Woodhouse | 49593bf | 2008-08-17 17:08:36 +0100 | [diff] [blame] | 5848 | while (1) { |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 5849 | leaf = path->nodes[0]; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5850 | slot = path->slots[0]; |
Li Zefan | b9e03af | 2011-03-23 10:43:58 +0800 | [diff] [blame] | 5851 | if (slot >= btrfs_header_nritems(leaf)) { |
| 5852 | ret = btrfs_next_leaf(root, path); |
| 5853 | if (ret < 0) |
| 5854 | goto err; |
| 5855 | else if (ret > 0) |
| 5856 | break; |
| 5857 | continue; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5858 | } |
Chris Mason | 3de4586 | 2008-11-17 21:02:50 -0500 | [diff] [blame] | 5859 | |
Ross Kirk | dd3cc16 | 2013-09-16 15:58:09 +0100 | [diff] [blame] | 5860 | item = btrfs_item_nr(slot); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 5861 | btrfs_item_key_to_cpu(leaf, &found_key, slot); |
| 5862 | |
| 5863 | if (found_key.objectid != key.objectid) |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5864 | break; |
David Sterba | 962a298 | 2014-06-04 18:41:45 +0200 | [diff] [blame] | 5865 | if (found_key.type != key_type) |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5866 | break; |
Al Viro | 9cdda8d | 2013-05-22 16:48:09 -0400 | [diff] [blame] | 5867 | if (found_key.offset < ctx->pos) |
Li Zefan | b9e03af | 2011-03-23 10:43:58 +0800 | [diff] [blame] | 5868 | goto next; |
Miao Xie | 16cdcec | 2011-04-22 18:12:22 +0800 | [diff] [blame] | 5869 | if (key_type == BTRFS_DIR_INDEX_KEY && |
| 5870 | btrfs_should_delete_dir_index(&del_list, |
| 5871 | found_key.offset)) |
| 5872 | goto next; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 5873 | |
Al Viro | 9cdda8d | 2013-05-22 16:48:09 -0400 | [diff] [blame] | 5874 | ctx->pos = found_key.offset; |
Miao Xie | 16cdcec | 2011-04-22 18:12:22 +0800 | [diff] [blame] | 5875 | is_curr = 1; |
David Woodhouse | 49593bf | 2008-08-17 17:08:36 +0100 | [diff] [blame] | 5876 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5877 | di = btrfs_item_ptr(leaf, slot, struct btrfs_dir_item); |
| 5878 | di_cur = 0; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 5879 | di_total = btrfs_item_size(leaf, item); |
David Woodhouse | 49593bf | 2008-08-17 17:08:36 +0100 | [diff] [blame] | 5880 | |
| 5881 | while (di_cur < di_total) { |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 5882 | struct btrfs_key location; |
| 5883 | |
Josef Bacik | 22a94d4 | 2011-03-16 16:47:17 -0400 | [diff] [blame] | 5884 | if (verify_dir_item(root, leaf, di)) |
| 5885 | break; |
| 5886 | |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 5887 | name_len = btrfs_dir_name_len(leaf, di); |
David Woodhouse | 49593bf | 2008-08-17 17:08:36 +0100 | [diff] [blame] | 5888 | if (name_len <= sizeof(tmp_name)) { |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 5889 | name_ptr = tmp_name; |
| 5890 | } else { |
David Sterba | 49e350a | 2016-02-11 14:25:38 +0100 | [diff] [blame] | 5891 | name_ptr = kmalloc(name_len, GFP_KERNEL); |
David Woodhouse | 49593bf | 2008-08-17 17:08:36 +0100 | [diff] [blame] | 5892 | if (!name_ptr) { |
| 5893 | ret = -ENOMEM; |
| 5894 | goto err; |
| 5895 | } |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 5896 | } |
| 5897 | read_extent_buffer(leaf, name_ptr, |
| 5898 | (unsigned long)(di + 1), name_len); |
| 5899 | |
| 5900 | d_type = btrfs_filetype_table[btrfs_dir_type(leaf, di)]; |
| 5901 | btrfs_dir_item_key_to_cpu(leaf, di, &location); |
Chris Mason | 3de4586 | 2008-11-17 21:02:50 -0500 | [diff] [blame] | 5902 | |
Josef Bacik | b4aff1f | 2011-06-28 16:18:59 -0400 | [diff] [blame] | 5903 | |
Chris Mason | 3de4586 | 2008-11-17 21:02:50 -0500 | [diff] [blame] | 5904 | /* is this a reference to our own snapshot? If so |
Arne Jansen | 8c9c2bf | 2012-02-25 09:09:30 +0100 | [diff] [blame] | 5905 | * skip it. |
| 5906 | * |
| 5907 | * In contrast to old kernels, we insert the snapshot's |
| 5908 | * dir item and dir index after it has been created, so |
| 5909 | * we won't find a reference to our own snapshot. We |
| 5910 | * still keep the following code for backward |
| 5911 | * compatibility. |
Chris Mason | 3de4586 | 2008-11-17 21:02:50 -0500 | [diff] [blame] | 5912 | */ |
| 5913 | if (location.type == BTRFS_ROOT_ITEM_KEY && |
| 5914 | location.objectid == root->root_key.objectid) { |
| 5915 | over = 0; |
| 5916 | goto skip; |
| 5917 | } |
Al Viro | 9cdda8d | 2013-05-22 16:48:09 -0400 | [diff] [blame] | 5918 | over = !dir_emit(ctx, name_ptr, name_len, |
| 5919 | location.objectid, d_type); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 5920 | |
Chris Mason | 3de4586 | 2008-11-17 21:02:50 -0500 | [diff] [blame] | 5921 | skip: |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 5922 | if (name_ptr != tmp_name) |
| 5923 | kfree(name_ptr); |
| 5924 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5925 | if (over) |
| 5926 | goto nopos; |
David Sterba | bc4ef75 | 2015-11-13 13:44:28 +0100 | [diff] [blame] | 5927 | emitted = true; |
Josef Bacik | 5103e94 | 2007-11-16 11:45:54 -0500 | [diff] [blame] | 5928 | di_len = btrfs_dir_name_len(leaf, di) + |
David Woodhouse | 49593bf | 2008-08-17 17:08:36 +0100 | [diff] [blame] | 5929 | btrfs_dir_data_len(leaf, di) + sizeof(*di); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5930 | di_cur += di_len; |
| 5931 | di = (struct btrfs_dir_item *)((char *)di + di_len); |
| 5932 | } |
Li Zefan | b9e03af | 2011-03-23 10:43:58 +0800 | [diff] [blame] | 5933 | next: |
| 5934 | path->slots[0]++; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5935 | } |
David Woodhouse | 49593bf | 2008-08-17 17:08:36 +0100 | [diff] [blame] | 5936 | |
Miao Xie | 16cdcec | 2011-04-22 18:12:22 +0800 | [diff] [blame] | 5937 | if (key_type == BTRFS_DIR_INDEX_KEY) { |
| 5938 | if (is_curr) |
Al Viro | 9cdda8d | 2013-05-22 16:48:09 -0400 | [diff] [blame] | 5939 | ctx->pos++; |
David Sterba | bc4ef75 | 2015-11-13 13:44:28 +0100 | [diff] [blame] | 5940 | ret = btrfs_readdir_delayed_dir_index(ctx, &ins_list, &emitted); |
Miao Xie | 16cdcec | 2011-04-22 18:12:22 +0800 | [diff] [blame] | 5941 | if (ret) |
| 5942 | goto nopos; |
| 5943 | } |
| 5944 | |
David Sterba | bc4ef75 | 2015-11-13 13:44:28 +0100 | [diff] [blame] | 5945 | /* |
| 5946 | * If we haven't emitted any dir entry, we must not touch ctx->pos as |
| 5947 | * it was was set to the termination value in previous call. We assume |
| 5948 | * that "." and ".." were emitted if we reach this point and set the |
| 5949 | * termination value as well for an empty directory. |
| 5950 | */ |
| 5951 | if (ctx->pos > 2 && !emitted) |
| 5952 | goto nopos; |
| 5953 | |
David Woodhouse | 49593bf | 2008-08-17 17:08:36 +0100 | [diff] [blame] | 5954 | /* Reached end of directory/root. Bump pos past the last item. */ |
Zach Brown | db62efb | 2013-07-11 16:19:42 -0700 | [diff] [blame] | 5955 | ctx->pos++; |
| 5956 | |
| 5957 | /* |
| 5958 | * Stop new entries from being returned after we return the last |
| 5959 | * entry. |
| 5960 | * |
| 5961 | * New directory entries are assigned a strictly increasing |
| 5962 | * offset. This means that new entries created during readdir |
| 5963 | * are *guaranteed* to be seen in the future by that readdir. |
| 5964 | * This has broken buggy programs which operate on names as |
| 5965 | * they're returned by readdir. Until we re-use freed offsets |
| 5966 | * we have this hack to stop new entries from being returned |
| 5967 | * under the assumption that they'll never reach this huge |
| 5968 | * offset. |
| 5969 | * |
| 5970 | * This is being careful not to overflow 32bit loff_t unless the |
| 5971 | * last entry requires it because doing so has broken 32bit apps |
| 5972 | * in the past. |
| 5973 | */ |
| 5974 | if (key_type == BTRFS_DIR_INDEX_KEY) { |
| 5975 | if (ctx->pos >= INT_MAX) |
| 5976 | ctx->pos = LLONG_MAX; |
| 5977 | else |
| 5978 | ctx->pos = INT_MAX; |
| 5979 | } |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5980 | nopos: |
| 5981 | ret = 0; |
| 5982 | err: |
Omar Sandoval | 02dbfc9 | 2016-05-20 13:50:33 -0700 | [diff] [blame] | 5983 | if (put) |
| 5984 | btrfs_readdir_put_delayed_items(inode, &ins_list, &del_list); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5985 | btrfs_free_path(path); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5986 | return ret; |
| 5987 | } |
| 5988 | |
Christoph Hellwig | a9185b4 | 2010-03-05 09:21:37 +0100 | [diff] [blame] | 5989 | int btrfs_write_inode(struct inode *inode, struct writeback_control *wbc) |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5990 | { |
| 5991 | struct btrfs_root *root = BTRFS_I(inode)->root; |
| 5992 | struct btrfs_trans_handle *trans; |
| 5993 | int ret = 0; |
Josef Bacik | 0af3d00 | 2010-06-21 14:48:16 -0400 | [diff] [blame] | 5994 | bool nolock = false; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5995 | |
Josef Bacik | 72ac3c0 | 2012-05-23 14:13:11 -0400 | [diff] [blame] | 5996 | if (test_bit(BTRFS_INODE_DUMMY, &BTRFS_I(inode)->runtime_flags)) |
Chris Mason | 4ca8b41 | 2008-08-05 13:30:48 -0400 | [diff] [blame] | 5997 | return 0; |
| 5998 | |
Liu Bo | 83eea1f | 2012-07-10 05:28:39 -0600 | [diff] [blame] | 5999 | 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] | 6000 | nolock = true; |
Josef Bacik | 0af3d00 | 2010-06-21 14:48:16 -0400 | [diff] [blame] | 6001 | |
Christoph Hellwig | a9185b4 | 2010-03-05 09:21:37 +0100 | [diff] [blame] | 6002 | if (wbc->sync_mode == WB_SYNC_ALL) { |
Josef Bacik | 0af3d00 | 2010-06-21 14:48:16 -0400 | [diff] [blame] | 6003 | if (nolock) |
Josef Bacik | 7a7eaa4 | 2011-04-13 12:54:33 -0400 | [diff] [blame] | 6004 | trans = btrfs_join_transaction_nolock(root); |
Josef Bacik | 0af3d00 | 2010-06-21 14:48:16 -0400 | [diff] [blame] | 6005 | else |
Josef Bacik | 7a7eaa4 | 2011-04-13 12:54:33 -0400 | [diff] [blame] | 6006 | trans = btrfs_join_transaction(root); |
Tsutomu Itoh | 3612b49 | 2011-01-25 02:51:38 +0000 | [diff] [blame] | 6007 | if (IS_ERR(trans)) |
| 6008 | return PTR_ERR(trans); |
Miao Xie | a698d075 | 2012-09-20 01:51:59 -0600 | [diff] [blame] | 6009 | ret = btrfs_commit_transaction(trans, root); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 6010 | } |
| 6011 | return ret; |
| 6012 | } |
| 6013 | |
| 6014 | /* |
Chris Mason | 54aa1f4 | 2007-06-22 14:16:25 -0400 | [diff] [blame] | 6015 | * This is somewhat expensive, updating the tree every time the |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 6016 | * inode changes. But, it is most likely to find the inode in cache. |
| 6017 | * FIXME, needs more benchmarking...there are no reasons other than performance |
| 6018 | * to keep or drop this code. |
| 6019 | */ |
Eric Sandeen | 48a3b63 | 2013-04-25 20:41:01 +0000 | [diff] [blame] | 6020 | static int btrfs_dirty_inode(struct inode *inode) |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 6021 | { |
| 6022 | struct btrfs_root *root = BTRFS_I(inode)->root; |
| 6023 | struct btrfs_trans_handle *trans; |
Yan, Zheng | 8929ecfa | 2010-05-16 10:49:58 -0400 | [diff] [blame] | 6024 | int ret; |
| 6025 | |
Josef Bacik | 72ac3c0 | 2012-05-23 14:13:11 -0400 | [diff] [blame] | 6026 | if (test_bit(BTRFS_INODE_DUMMY, &BTRFS_I(inode)->runtime_flags)) |
Josef Bacik | 22c44fe | 2011-11-30 10:45:38 -0500 | [diff] [blame] | 6027 | return 0; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 6028 | |
Josef Bacik | 7a7eaa4 | 2011-04-13 12:54:33 -0400 | [diff] [blame] | 6029 | trans = btrfs_join_transaction(root); |
Josef Bacik | 22c44fe | 2011-11-30 10:45:38 -0500 | [diff] [blame] | 6030 | if (IS_ERR(trans)) |
| 6031 | return PTR_ERR(trans); |
Yan, Zheng | 8929ecfa | 2010-05-16 10:49:58 -0400 | [diff] [blame] | 6032 | |
| 6033 | ret = btrfs_update_inode(trans, root, inode); |
Chris Mason | 94b6044 | 2010-05-26 11:02:00 -0400 | [diff] [blame] | 6034 | if (ret && ret == -ENOSPC) { |
| 6035 | /* whoops, lets try again with the full transaction */ |
| 6036 | btrfs_end_transaction(trans, root); |
| 6037 | trans = btrfs_start_transaction(root, 1); |
Josef Bacik | 22c44fe | 2011-11-30 10:45:38 -0500 | [diff] [blame] | 6038 | if (IS_ERR(trans)) |
| 6039 | return PTR_ERR(trans); |
Yan, Zheng | 8929ecfa | 2010-05-16 10:49:58 -0400 | [diff] [blame] | 6040 | |
Chris Mason | 94b6044 | 2010-05-26 11:02:00 -0400 | [diff] [blame] | 6041 | ret = btrfs_update_inode(trans, root, inode); |
Chris Mason | 94b6044 | 2010-05-26 11:02:00 -0400 | [diff] [blame] | 6042 | } |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 6043 | btrfs_end_transaction(trans, root); |
Miao Xie | 16cdcec | 2011-04-22 18:12:22 +0800 | [diff] [blame] | 6044 | if (BTRFS_I(inode)->delayed_node) |
| 6045 | btrfs_balance_delayed_items(root); |
Josef Bacik | 22c44fe | 2011-11-30 10:45:38 -0500 | [diff] [blame] | 6046 | |
| 6047 | return ret; |
| 6048 | } |
| 6049 | |
| 6050 | /* |
| 6051 | * This is a copy of file_update_time. We need this so we can return error on |
| 6052 | * ENOSPC for updating the inode in the case of file write and mmap writes. |
| 6053 | */ |
Josef Bacik | e41f941 | 2012-03-26 09:46:47 -0400 | [diff] [blame] | 6054 | static int btrfs_update_time(struct inode *inode, struct timespec *now, |
| 6055 | int flags) |
Josef Bacik | 22c44fe | 2011-11-30 10:45:38 -0500 | [diff] [blame] | 6056 | { |
Alexander Block | 2bc556528 | 2012-06-15 09:49:33 +0200 | [diff] [blame] | 6057 | struct btrfs_root *root = BTRFS_I(inode)->root; |
| 6058 | |
| 6059 | if (btrfs_root_readonly(root)) |
| 6060 | return -EROFS; |
| 6061 | |
Josef Bacik | e41f941 | 2012-03-26 09:46:47 -0400 | [diff] [blame] | 6062 | if (flags & S_VERSION) |
Josef Bacik | 22c44fe | 2011-11-30 10:45:38 -0500 | [diff] [blame] | 6063 | inode_inc_iversion(inode); |
Josef Bacik | e41f941 | 2012-03-26 09:46:47 -0400 | [diff] [blame] | 6064 | if (flags & S_CTIME) |
| 6065 | inode->i_ctime = *now; |
| 6066 | if (flags & S_MTIME) |
| 6067 | inode->i_mtime = *now; |
| 6068 | if (flags & S_ATIME) |
| 6069 | inode->i_atime = *now; |
| 6070 | return btrfs_dirty_inode(inode); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 6071 | } |
| 6072 | |
Chris Mason | d352ac6 | 2008-09-29 15:18:18 -0400 | [diff] [blame] | 6073 | /* |
| 6074 | * find the highest existing sequence number in a directory |
| 6075 | * and then set the in-memory index_cnt variable to reflect |
| 6076 | * free sequence numbers |
| 6077 | */ |
Josef Bacik | aec7477 | 2008-07-24 12:12:38 -0400 | [diff] [blame] | 6078 | static int btrfs_set_inode_index_count(struct inode *inode) |
| 6079 | { |
| 6080 | struct btrfs_root *root = BTRFS_I(inode)->root; |
| 6081 | struct btrfs_key key, found_key; |
| 6082 | struct btrfs_path *path; |
| 6083 | struct extent_buffer *leaf; |
| 6084 | int ret; |
| 6085 | |
Li Zefan | 33345d01 | 2011-04-20 10:31:50 +0800 | [diff] [blame] | 6086 | key.objectid = btrfs_ino(inode); |
David Sterba | 962a298 | 2014-06-04 18:41:45 +0200 | [diff] [blame] | 6087 | key.type = BTRFS_DIR_INDEX_KEY; |
Josef Bacik | aec7477 | 2008-07-24 12:12:38 -0400 | [diff] [blame] | 6088 | key.offset = (u64)-1; |
| 6089 | |
| 6090 | path = btrfs_alloc_path(); |
| 6091 | if (!path) |
| 6092 | return -ENOMEM; |
| 6093 | |
| 6094 | ret = btrfs_search_slot(NULL, root, &key, path, 0, 0); |
| 6095 | if (ret < 0) |
| 6096 | goto out; |
| 6097 | /* FIXME: we should be able to handle this */ |
| 6098 | if (ret == 0) |
| 6099 | goto out; |
| 6100 | ret = 0; |
| 6101 | |
| 6102 | /* |
| 6103 | * MAGIC NUMBER EXPLANATION: |
| 6104 | * since we search a directory based on f_pos we have to start at 2 |
| 6105 | * since '.' and '..' have f_pos of 0 and 1 respectively, so everybody |
| 6106 | * else has to start at 2 |
| 6107 | */ |
| 6108 | if (path->slots[0] == 0) { |
| 6109 | BTRFS_I(inode)->index_cnt = 2; |
| 6110 | goto out; |
| 6111 | } |
| 6112 | |
| 6113 | path->slots[0]--; |
| 6114 | |
| 6115 | leaf = path->nodes[0]; |
| 6116 | btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]); |
| 6117 | |
Li Zefan | 33345d01 | 2011-04-20 10:31:50 +0800 | [diff] [blame] | 6118 | if (found_key.objectid != btrfs_ino(inode) || |
David Sterba | 962a298 | 2014-06-04 18:41:45 +0200 | [diff] [blame] | 6119 | found_key.type != BTRFS_DIR_INDEX_KEY) { |
Josef Bacik | aec7477 | 2008-07-24 12:12:38 -0400 | [diff] [blame] | 6120 | BTRFS_I(inode)->index_cnt = 2; |
| 6121 | goto out; |
| 6122 | } |
| 6123 | |
| 6124 | BTRFS_I(inode)->index_cnt = found_key.offset + 1; |
| 6125 | out: |
| 6126 | btrfs_free_path(path); |
| 6127 | return ret; |
| 6128 | } |
| 6129 | |
Chris Mason | d352ac6 | 2008-09-29 15:18:18 -0400 | [diff] [blame] | 6130 | /* |
| 6131 | * helper to find a free sequence number in a given directory. This current |
| 6132 | * code is very simple, later versions will do smarter things in the btree |
| 6133 | */ |
Chris Mason | 3de4586 | 2008-11-17 21:02:50 -0500 | [diff] [blame] | 6134 | int btrfs_set_inode_index(struct inode *dir, u64 *index) |
Josef Bacik | aec7477 | 2008-07-24 12:12:38 -0400 | [diff] [blame] | 6135 | { |
| 6136 | int ret = 0; |
| 6137 | |
| 6138 | if (BTRFS_I(dir)->index_cnt == (u64)-1) { |
Miao Xie | 16cdcec | 2011-04-22 18:12:22 +0800 | [diff] [blame] | 6139 | ret = btrfs_inode_delayed_dir_index_count(dir); |
| 6140 | if (ret) { |
| 6141 | ret = btrfs_set_inode_index_count(dir); |
| 6142 | if (ret) |
| 6143 | return ret; |
| 6144 | } |
Josef Bacik | aec7477 | 2008-07-24 12:12:38 -0400 | [diff] [blame] | 6145 | } |
| 6146 | |
Chris Mason | 00e4e6b | 2008-08-05 11:18:09 -0400 | [diff] [blame] | 6147 | *index = BTRFS_I(dir)->index_cnt; |
Josef Bacik | aec7477 | 2008-07-24 12:12:38 -0400 | [diff] [blame] | 6148 | BTRFS_I(dir)->index_cnt++; |
| 6149 | |
| 6150 | return ret; |
| 6151 | } |
| 6152 | |
Chris Mason | b0d5d10 | 2014-09-08 13:08:51 -0700 | [diff] [blame] | 6153 | static int btrfs_insert_inode_locked(struct inode *inode) |
| 6154 | { |
| 6155 | struct btrfs_iget_args args; |
| 6156 | args.location = &BTRFS_I(inode)->location; |
| 6157 | args.root = BTRFS_I(inode)->root; |
| 6158 | |
| 6159 | return insert_inode_locked4(inode, |
| 6160 | btrfs_inode_hash(inode->i_ino, BTRFS_I(inode)->root), |
| 6161 | btrfs_find_actor, &args); |
| 6162 | } |
| 6163 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 6164 | static struct inode *btrfs_new_inode(struct btrfs_trans_handle *trans, |
| 6165 | struct btrfs_root *root, |
Josef Bacik | aec7477 | 2008-07-24 12:12:38 -0400 | [diff] [blame] | 6166 | struct inode *dir, |
Chris Mason | 9c58309 | 2008-01-29 15:15:18 -0500 | [diff] [blame] | 6167 | const char *name, int name_len, |
Al Viro | 175a4eb | 2011-07-26 03:30:54 -0400 | [diff] [blame] | 6168 | u64 ref_objectid, u64 objectid, |
| 6169 | umode_t mode, u64 *index) |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 6170 | { |
| 6171 | struct inode *inode; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 6172 | struct btrfs_inode_item *inode_item; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 6173 | struct btrfs_key *location; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 6174 | struct btrfs_path *path; |
Chris Mason | 9c58309 | 2008-01-29 15:15:18 -0500 | [diff] [blame] | 6175 | struct btrfs_inode_ref *ref; |
| 6176 | struct btrfs_key key[2]; |
| 6177 | u32 sizes[2]; |
Filipe Manana | ef3b9af | 2014-04-27 20:40:45 +0100 | [diff] [blame] | 6178 | int nitems = name ? 2 : 1; |
Chris Mason | 9c58309 | 2008-01-29 15:15:18 -0500 | [diff] [blame] | 6179 | unsigned long ptr; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 6180 | int ret; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 6181 | |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 6182 | path = btrfs_alloc_path(); |
Mark Fasheh | d8926bb | 2011-07-13 10:38:47 -0700 | [diff] [blame] | 6183 | if (!path) |
| 6184 | return ERR_PTR(-ENOMEM); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 6185 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 6186 | inode = new_inode(root->fs_info->sb); |
Yoshinori Sano | 8fb2764 | 2011-04-09 02:30:07 +0000 | [diff] [blame] | 6187 | if (!inode) { |
| 6188 | btrfs_free_path(path); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 6189 | return ERR_PTR(-ENOMEM); |
Yoshinori Sano | 8fb2764 | 2011-04-09 02:30:07 +0000 | [diff] [blame] | 6190 | } |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 6191 | |
Li Zefan | 581bb05 | 2011-04-20 10:06:11 +0800 | [diff] [blame] | 6192 | /* |
Filipe Manana | 5762b5c | 2014-08-01 00:10:32 +0100 | [diff] [blame] | 6193 | * O_TMPFILE, set link count to 0, so that after this point, |
| 6194 | * we fill in an inode item with the correct link count. |
| 6195 | */ |
| 6196 | if (!name) |
| 6197 | set_nlink(inode, 0); |
| 6198 | |
| 6199 | /* |
Li Zefan | 581bb05 | 2011-04-20 10:06:11 +0800 | [diff] [blame] | 6200 | * we have to initialize this early, so we can reclaim the inode |
| 6201 | * number if we fail afterwards in this function. |
| 6202 | */ |
| 6203 | inode->i_ino = objectid; |
| 6204 | |
Filipe Manana | ef3b9af | 2014-04-27 20:40:45 +0100 | [diff] [blame] | 6205 | if (dir && name) { |
liubo | 1abe9b8 | 2011-03-24 11:18:59 +0000 | [diff] [blame] | 6206 | trace_btrfs_inode_request(dir); |
| 6207 | |
Chris Mason | 3de4586 | 2008-11-17 21:02:50 -0500 | [diff] [blame] | 6208 | ret = btrfs_set_inode_index(dir, index); |
Shen Feng | 0977143 | 2009-04-02 16:46:06 -0400 | [diff] [blame] | 6209 | if (ret) { |
Yoshinori Sano | 8fb2764 | 2011-04-09 02:30:07 +0000 | [diff] [blame] | 6210 | btrfs_free_path(path); |
Shen Feng | 0977143 | 2009-04-02 16:46:06 -0400 | [diff] [blame] | 6211 | iput(inode); |
Josef Bacik | aec7477 | 2008-07-24 12:12:38 -0400 | [diff] [blame] | 6212 | return ERR_PTR(ret); |
Shen Feng | 0977143 | 2009-04-02 16:46:06 -0400 | [diff] [blame] | 6213 | } |
Filipe Manana | ef3b9af | 2014-04-27 20:40:45 +0100 | [diff] [blame] | 6214 | } else if (dir) { |
| 6215 | *index = 0; |
Josef Bacik | aec7477 | 2008-07-24 12:12:38 -0400 | [diff] [blame] | 6216 | } |
| 6217 | /* |
| 6218 | * index_cnt is ignored for everything but a dir, |
| 6219 | * btrfs_get_inode_index_count has an explanation for the magic |
| 6220 | * number |
| 6221 | */ |
| 6222 | BTRFS_I(inode)->index_cnt = 2; |
Miao Xie | 67de117 | 2013-12-26 13:07:06 +0800 | [diff] [blame] | 6223 | BTRFS_I(inode)->dir_index = *index; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 6224 | BTRFS_I(inode)->root = root; |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 6225 | BTRFS_I(inode)->generation = trans->transid; |
Josef Bacik | 7619585 | 2010-11-19 02:18:02 +0000 | [diff] [blame] | 6226 | inode->i_generation = BTRFS_I(inode)->generation; |
Chris Mason | b888db2 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 6227 | |
Josef Bacik | 5dc562c | 2012-08-17 13:14:17 -0400 | [diff] [blame] | 6228 | /* |
| 6229 | * We could have gotten an inode number from somebody who was fsynced |
| 6230 | * and then removed in this same transaction, so let's just set full |
| 6231 | * sync since it will be a full sync anyway and this will blow away the |
| 6232 | * old info in the log. |
| 6233 | */ |
| 6234 | set_bit(BTRFS_INODE_NEEDS_FULL_SYNC, &BTRFS_I(inode)->runtime_flags); |
| 6235 | |
Chris Mason | 9c58309 | 2008-01-29 15:15:18 -0500 | [diff] [blame] | 6236 | key[0].objectid = objectid; |
David Sterba | 962a298 | 2014-06-04 18:41:45 +0200 | [diff] [blame] | 6237 | key[0].type = BTRFS_INODE_ITEM_KEY; |
Chris Mason | 9c58309 | 2008-01-29 15:15:18 -0500 | [diff] [blame] | 6238 | key[0].offset = 0; |
| 6239 | |
Chris Mason | 9c58309 | 2008-01-29 15:15:18 -0500 | [diff] [blame] | 6240 | sizes[0] = sizeof(struct btrfs_inode_item); |
Filipe Manana | ef3b9af | 2014-04-27 20:40:45 +0100 | [diff] [blame] | 6241 | |
| 6242 | if (name) { |
| 6243 | /* |
| 6244 | * Start new inodes with an inode_ref. This is slightly more |
| 6245 | * efficient for small numbers of hard links since they will |
| 6246 | * be packed into one item. Extended refs will kick in if we |
| 6247 | * add more hard links than can fit in the ref item. |
| 6248 | */ |
| 6249 | key[1].objectid = objectid; |
David Sterba | 962a298 | 2014-06-04 18:41:45 +0200 | [diff] [blame] | 6250 | key[1].type = BTRFS_INODE_REF_KEY; |
Filipe Manana | ef3b9af | 2014-04-27 20:40:45 +0100 | [diff] [blame] | 6251 | key[1].offset = ref_objectid; |
| 6252 | |
| 6253 | sizes[1] = name_len + sizeof(*ref); |
| 6254 | } |
Chris Mason | 9c58309 | 2008-01-29 15:15:18 -0500 | [diff] [blame] | 6255 | |
Chris Mason | b0d5d10 | 2014-09-08 13:08:51 -0700 | [diff] [blame] | 6256 | location = &BTRFS_I(inode)->location; |
| 6257 | location->objectid = objectid; |
| 6258 | location->offset = 0; |
David Sterba | 962a298 | 2014-06-04 18:41:45 +0200 | [diff] [blame] | 6259 | location->type = BTRFS_INODE_ITEM_KEY; |
Chris Mason | b0d5d10 | 2014-09-08 13:08:51 -0700 | [diff] [blame] | 6260 | |
| 6261 | ret = btrfs_insert_inode_locked(inode); |
| 6262 | if (ret < 0) |
| 6263 | goto fail; |
| 6264 | |
Chris Mason | b947343 | 2009-03-13 11:00:37 -0400 | [diff] [blame] | 6265 | path->leave_spinning = 1; |
Filipe Manana | ef3b9af | 2014-04-27 20:40:45 +0100 | [diff] [blame] | 6266 | ret = btrfs_insert_empty_items(trans, root, path, key, sizes, nitems); |
Chris Mason | 9c58309 | 2008-01-29 15:15:18 -0500 | [diff] [blame] | 6267 | if (ret != 0) |
Chris Mason | b0d5d10 | 2014-09-08 13:08:51 -0700 | [diff] [blame] | 6268 | goto fail_unlock; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 6269 | |
Dmitry Monakhov | ecc11fab | 2010-03-04 17:31:47 +0300 | [diff] [blame] | 6270 | inode_init_owner(inode, dir, mode); |
Yan Zheng | a76a3cd | 2008-10-09 11:46:29 -0400 | [diff] [blame] | 6271 | inode_set_bytes(inode, 0); |
chandan r | 9cc97d6 | 2012-07-04 12:48:07 +0530 | [diff] [blame] | 6272 | |
Deepa Dinamani | c2050a4 | 2016-09-14 07:48:06 -0700 | [diff] [blame] | 6273 | inode->i_mtime = current_time(inode); |
chandan r | 9cc97d6 | 2012-07-04 12:48:07 +0530 | [diff] [blame] | 6274 | inode->i_atime = inode->i_mtime; |
| 6275 | inode->i_ctime = inode->i_mtime; |
| 6276 | BTRFS_I(inode)->i_otime = inode->i_mtime; |
| 6277 | |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 6278 | inode_item = btrfs_item_ptr(path->nodes[0], path->slots[0], |
| 6279 | struct btrfs_inode_item); |
Li Zefan | 293f7e0 | 2012-07-10 00:58:58 -0600 | [diff] [blame] | 6280 | memset_extent_buffer(path->nodes[0], 0, (unsigned long)inode_item, |
| 6281 | sizeof(*inode_item)); |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 6282 | fill_inode_item(trans, path->nodes[0], inode_item, inode); |
Chris Mason | 9c58309 | 2008-01-29 15:15:18 -0500 | [diff] [blame] | 6283 | |
Filipe Manana | ef3b9af | 2014-04-27 20:40:45 +0100 | [diff] [blame] | 6284 | if (name) { |
| 6285 | ref = btrfs_item_ptr(path->nodes[0], path->slots[0] + 1, |
| 6286 | struct btrfs_inode_ref); |
| 6287 | btrfs_set_inode_ref_name_len(path->nodes[0], ref, name_len); |
| 6288 | btrfs_set_inode_ref_index(path->nodes[0], ref, *index); |
| 6289 | ptr = (unsigned long)(ref + 1); |
| 6290 | write_extent_buffer(path->nodes[0], name, ptr, name_len); |
| 6291 | } |
Chris Mason | 9c58309 | 2008-01-29 15:15:18 -0500 | [diff] [blame] | 6292 | |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 6293 | btrfs_mark_buffer_dirty(path->nodes[0]); |
| 6294 | btrfs_free_path(path); |
| 6295 | |
Christoph Hellwig | 6cbff00 | 2009-04-17 10:37:41 +0200 | [diff] [blame] | 6296 | btrfs_inherit_iflags(inode, dir); |
| 6297 | |
Al Viro | 569254b | 2011-07-24 17:08:40 -0400 | [diff] [blame] | 6298 | if (S_ISREG(mode)) { |
Jeff Mahoney | 3cdde22 | 2016-06-09 21:38:35 -0400 | [diff] [blame] | 6299 | if (btrfs_test_opt(root->fs_info, NODATASUM)) |
Chris Mason | 9427216 | 2009-07-02 12:26:06 -0400 | [diff] [blame] | 6300 | BTRFS_I(inode)->flags |= BTRFS_INODE_NODATASUM; |
Jeff Mahoney | 3cdde22 | 2016-06-09 21:38:35 -0400 | [diff] [blame] | 6301 | if (btrfs_test_opt(root->fs_info, NODATACOW)) |
Josef Bacik | f2bdf9a | 2013-02-21 15:28:28 -0500 | [diff] [blame] | 6302 | BTRFS_I(inode)->flags |= BTRFS_INODE_NODATACOW | |
| 6303 | BTRFS_INODE_NODATASUM; |
Chris Mason | 9427216 | 2009-07-02 12:26:06 -0400 | [diff] [blame] | 6304 | } |
| 6305 | |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 6306 | inode_tree_add(inode); |
liubo | 1abe9b8 | 2011-03-24 11:18:59 +0000 | [diff] [blame] | 6307 | |
| 6308 | trace_btrfs_inode_new(inode); |
Chris Mason | 1973f0f | 2011-06-24 13:13:29 -0400 | [diff] [blame] | 6309 | btrfs_set_inode_last_trans(trans, inode); |
liubo | 1abe9b8 | 2011-03-24 11:18:59 +0000 | [diff] [blame] | 6310 | |
Alexander Block | 8ea05e3 | 2012-07-25 17:35:53 +0200 | [diff] [blame] | 6311 | btrfs_update_root_times(trans, root); |
| 6312 | |
Filipe David Borba Manana | 6354192 | 2014-01-07 11:47:46 +0000 | [diff] [blame] | 6313 | ret = btrfs_inode_inherit_props(trans, inode, dir); |
| 6314 | if (ret) |
| 6315 | btrfs_err(root->fs_info, |
| 6316 | "error inheriting props for ino %llu (root %llu): %d", |
| 6317 | btrfs_ino(inode), root->root_key.objectid, ret); |
| 6318 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 6319 | return inode; |
Chris Mason | b0d5d10 | 2014-09-08 13:08:51 -0700 | [diff] [blame] | 6320 | |
| 6321 | fail_unlock: |
| 6322 | unlock_new_inode(inode); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 6323 | fail: |
Filipe Manana | ef3b9af | 2014-04-27 20:40:45 +0100 | [diff] [blame] | 6324 | if (dir && name) |
Josef Bacik | aec7477 | 2008-07-24 12:12:38 -0400 | [diff] [blame] | 6325 | BTRFS_I(dir)->index_cnt--; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 6326 | btrfs_free_path(path); |
Shen Feng | 0977143 | 2009-04-02 16:46:06 -0400 | [diff] [blame] | 6327 | iput(inode); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 6328 | return ERR_PTR(ret); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 6329 | } |
| 6330 | |
| 6331 | static inline u8 btrfs_inode_type(struct inode *inode) |
| 6332 | { |
| 6333 | return btrfs_type_by_mode[(inode->i_mode & S_IFMT) >> S_SHIFT]; |
| 6334 | } |
| 6335 | |
Chris Mason | d352ac6 | 2008-09-29 15:18:18 -0400 | [diff] [blame] | 6336 | /* |
| 6337 | * utility function to add 'inode' into 'parent_inode' with |
| 6338 | * a give name and a given sequence number. |
| 6339 | * if 'add_backref' is true, also insert a backref from the |
| 6340 | * inode to the parent directory. |
| 6341 | */ |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 6342 | int btrfs_add_link(struct btrfs_trans_handle *trans, |
| 6343 | struct inode *parent_inode, struct inode *inode, |
| 6344 | const char *name, int name_len, int add_backref, u64 index) |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 6345 | { |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 6346 | int ret = 0; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 6347 | struct btrfs_key key; |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 6348 | struct btrfs_root *root = BTRFS_I(parent_inode)->root; |
Li Zefan | 33345d01 | 2011-04-20 10:31:50 +0800 | [diff] [blame] | 6349 | u64 ino = btrfs_ino(inode); |
| 6350 | u64 parent_ino = btrfs_ino(parent_inode); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 6351 | |
Li Zefan | 33345d01 | 2011-04-20 10:31:50 +0800 | [diff] [blame] | 6352 | if (unlikely(ino == BTRFS_FIRST_FREE_OBJECTID)) { |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 6353 | memcpy(&key, &BTRFS_I(inode)->root->root_key, sizeof(key)); |
| 6354 | } else { |
Li Zefan | 33345d01 | 2011-04-20 10:31:50 +0800 | [diff] [blame] | 6355 | key.objectid = ino; |
David Sterba | 962a298 | 2014-06-04 18:41:45 +0200 | [diff] [blame] | 6356 | key.type = BTRFS_INODE_ITEM_KEY; |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 6357 | key.offset = 0; |
| 6358 | } |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 6359 | |
Li Zefan | 33345d01 | 2011-04-20 10:31:50 +0800 | [diff] [blame] | 6360 | if (unlikely(ino == BTRFS_FIRST_FREE_OBJECTID)) { |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 6361 | ret = btrfs_add_root_ref(trans, root->fs_info->tree_root, |
| 6362 | key.objectid, root->root_key.objectid, |
Li Zefan | 33345d01 | 2011-04-20 10:31:50 +0800 | [diff] [blame] | 6363 | parent_ino, index, name, name_len); |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 6364 | } else if (add_backref) { |
Li Zefan | 33345d01 | 2011-04-20 10:31:50 +0800 | [diff] [blame] | 6365 | ret = btrfs_insert_inode_ref(trans, root, name, name_len, ino, |
| 6366 | parent_ino, index); |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 6367 | } |
| 6368 | |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 6369 | /* Nothing to clean up yet */ |
| 6370 | if (ret) |
| 6371 | return ret; |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 6372 | |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 6373 | ret = btrfs_insert_dir_item(trans, root, name, name_len, |
| 6374 | parent_inode, &key, |
| 6375 | btrfs_inode_type(inode), index); |
Chris Mason | 9c52057 | 2012-12-17 14:26:57 -0500 | [diff] [blame] | 6376 | if (ret == -EEXIST || ret == -EOVERFLOW) |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 6377 | goto fail_dir_item; |
| 6378 | else if (ret) { |
Jeff Mahoney | 6664283 | 2016-06-10 18:19:25 -0400 | [diff] [blame] | 6379 | btrfs_abort_transaction(trans, ret); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 6380 | return ret; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 6381 | } |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 6382 | |
| 6383 | btrfs_i_size_write(parent_inode, parent_inode->i_size + |
| 6384 | name_len * 2); |
Josef Bacik | 0c4d2d9 | 2012-04-05 15:03:02 -0400 | [diff] [blame] | 6385 | inode_inc_iversion(parent_inode); |
Deepa Dinamani | 04b285f | 2016-02-06 23:57:21 -0800 | [diff] [blame] | 6386 | parent_inode->i_mtime = parent_inode->i_ctime = |
Deepa Dinamani | c2050a4 | 2016-09-14 07:48:06 -0700 | [diff] [blame] | 6387 | current_time(parent_inode); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 6388 | ret = btrfs_update_inode(trans, root, parent_inode); |
| 6389 | if (ret) |
Jeff Mahoney | 6664283 | 2016-06-10 18:19:25 -0400 | [diff] [blame] | 6390 | btrfs_abort_transaction(trans, ret); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 6391 | return ret; |
Chris Mason | fe66a05 | 2012-02-20 08:40:56 -0500 | [diff] [blame] | 6392 | |
| 6393 | fail_dir_item: |
| 6394 | if (unlikely(ino == BTRFS_FIRST_FREE_OBJECTID)) { |
| 6395 | u64 local_index; |
| 6396 | int err; |
| 6397 | err = btrfs_del_root_ref(trans, root->fs_info->tree_root, |
| 6398 | key.objectid, root->root_key.objectid, |
| 6399 | parent_ino, &local_index, name, name_len); |
| 6400 | |
| 6401 | } else if (add_backref) { |
| 6402 | u64 local_index; |
| 6403 | int err; |
| 6404 | |
| 6405 | err = btrfs_del_inode_ref(trans, root, name, name_len, |
| 6406 | ino, parent_ino, &local_index); |
| 6407 | } |
| 6408 | return ret; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 6409 | } |
| 6410 | |
| 6411 | static int btrfs_add_nondir(struct btrfs_trans_handle *trans, |
Josef Bacik | a1b075d | 2010-11-19 20:36:11 +0000 | [diff] [blame] | 6412 | struct inode *dir, struct dentry *dentry, |
| 6413 | struct inode *inode, int backref, u64 index) |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 6414 | { |
Josef Bacik | a1b075d | 2010-11-19 20:36:11 +0000 | [diff] [blame] | 6415 | int err = btrfs_add_link(trans, dir, inode, |
| 6416 | dentry->d_name.name, dentry->d_name.len, |
| 6417 | backref, index); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 6418 | if (err > 0) |
| 6419 | err = -EEXIST; |
| 6420 | return err; |
| 6421 | } |
| 6422 | |
Josef Bacik | 618e21d | 2007-07-11 10:18:17 -0400 | [diff] [blame] | 6423 | static int btrfs_mknod(struct inode *dir, struct dentry *dentry, |
Al Viro | 1a67aaf | 2011-07-26 01:52:52 -0400 | [diff] [blame] | 6424 | umode_t mode, dev_t rdev) |
Josef Bacik | 618e21d | 2007-07-11 10:18:17 -0400 | [diff] [blame] | 6425 | { |
| 6426 | struct btrfs_trans_handle *trans; |
| 6427 | struct btrfs_root *root = BTRFS_I(dir)->root; |
Chris Mason | 1832a6d | 2007-12-21 16:27:21 -0500 | [diff] [blame] | 6428 | struct inode *inode = NULL; |
Josef Bacik | 618e21d | 2007-07-11 10:18:17 -0400 | [diff] [blame] | 6429 | int err; |
| 6430 | int drop_inode = 0; |
| 6431 | u64 objectid; |
Chris Mason | 00e4e6b | 2008-08-05 11:18:09 -0400 | [diff] [blame] | 6432 | u64 index = 0; |
Josef Bacik | 618e21d | 2007-07-11 10:18:17 -0400 | [diff] [blame] | 6433 | |
Josef Bacik | 9ed74f2 | 2009-09-11 16:12:44 -0400 | [diff] [blame] | 6434 | /* |
| 6435 | * 2 for inode item and ref |
| 6436 | * 2 for dir items |
| 6437 | * 1 for xattr if selinux is on |
| 6438 | */ |
Yan, Zheng | a22285a | 2010-05-16 10:48:46 -0400 | [diff] [blame] | 6439 | trans = btrfs_start_transaction(root, 5); |
| 6440 | if (IS_ERR(trans)) |
| 6441 | return PTR_ERR(trans); |
Chris Mason | 1832a6d | 2007-12-21 16:27:21 -0500 | [diff] [blame] | 6442 | |
Li Zefan | 581bb05 | 2011-04-20 10:06:11 +0800 | [diff] [blame] | 6443 | err = btrfs_find_free_ino(root, &objectid); |
| 6444 | if (err) |
| 6445 | goto out_unlock; |
| 6446 | |
Josef Bacik | aec7477 | 2008-07-24 12:12:38 -0400 | [diff] [blame] | 6447 | inode = btrfs_new_inode(trans, root, dir, dentry->d_name.name, |
Li Zefan | 33345d01 | 2011-04-20 10:31:50 +0800 | [diff] [blame] | 6448 | dentry->d_name.len, btrfs_ino(dir), objectid, |
Josef Bacik | d82a6f1 | 2011-05-11 15:26:06 -0400 | [diff] [blame] | 6449 | mode, &index); |
Tsutomu Itoh | 7cf96da | 2011-04-25 19:43:53 -0400 | [diff] [blame] | 6450 | if (IS_ERR(inode)) { |
| 6451 | err = PTR_ERR(inode); |
Josef Bacik | 618e21d | 2007-07-11 10:18:17 -0400 | [diff] [blame] | 6452 | goto out_unlock; |
Tsutomu Itoh | 7cf96da | 2011-04-25 19:43:53 -0400 | [diff] [blame] | 6453 | } |
Josef Bacik | 618e21d | 2007-07-11 10:18:17 -0400 | [diff] [blame] | 6454 | |
Casey Schaufler | ad19db7 | 2011-12-15 10:09:07 -0500 | [diff] [blame] | 6455 | /* |
| 6456 | * If the active LSM wants to access the inode during |
| 6457 | * d_instantiate it needs these. Smack checks to see |
| 6458 | * if the filesystem supports xattrs by looking at the |
| 6459 | * ops vector. |
| 6460 | */ |
Casey Schaufler | ad19db7 | 2011-12-15 10:09:07 -0500 | [diff] [blame] | 6461 | inode->i_op = &btrfs_special_inode_operations; |
Chris Mason | b0d5d10 | 2014-09-08 13:08:51 -0700 | [diff] [blame] | 6462 | init_special_inode(inode, inode->i_mode, rdev); |
| 6463 | |
| 6464 | err = btrfs_init_inode_security(trans, inode, dir, &dentry->d_name); |
Josef Bacik | 618e21d | 2007-07-11 10:18:17 -0400 | [diff] [blame] | 6465 | if (err) |
Chris Mason | b0d5d10 | 2014-09-08 13:08:51 -0700 | [diff] [blame] | 6466 | goto out_unlock_inode; |
| 6467 | |
| 6468 | err = btrfs_add_nondir(trans, dir, dentry, inode, 0, index); |
| 6469 | if (err) { |
| 6470 | goto out_unlock_inode; |
| 6471 | } else { |
Yan | 1b4ab1b | 2007-08-29 09:11:44 -0400 | [diff] [blame] | 6472 | btrfs_update_inode(trans, root, inode); |
Chris Mason | b0d5d10 | 2014-09-08 13:08:51 -0700 | [diff] [blame] | 6473 | unlock_new_inode(inode); |
Al Viro | 08c422c | 2011-12-23 07:58:13 -0500 | [diff] [blame] | 6474 | d_instantiate(dentry, inode); |
Josef Bacik | 618e21d | 2007-07-11 10:18:17 -0400 | [diff] [blame] | 6475 | } |
Chris Mason | b0d5d10 | 2014-09-08 13:08:51 -0700 | [diff] [blame] | 6476 | |
Josef Bacik | 618e21d | 2007-07-11 10:18:17 -0400 | [diff] [blame] | 6477 | out_unlock: |
Josef Bacik | 7ad85bb | 2012-01-12 19:10:12 -0500 | [diff] [blame] | 6478 | btrfs_end_transaction(trans, root); |
Josef Bacik | c581afc | 2014-02-06 16:06:06 -0500 | [diff] [blame] | 6479 | btrfs_balance_delayed_items(root); |
Liu Bo | b53d3f5 | 2012-11-14 14:34:34 +0000 | [diff] [blame] | 6480 | btrfs_btree_balance_dirty(root); |
Josef Bacik | 618e21d | 2007-07-11 10:18:17 -0400 | [diff] [blame] | 6481 | if (drop_inode) { |
| 6482 | inode_dec_link_count(inode); |
| 6483 | iput(inode); |
| 6484 | } |
Josef Bacik | 618e21d | 2007-07-11 10:18:17 -0400 | [diff] [blame] | 6485 | return err; |
Chris Mason | b0d5d10 | 2014-09-08 13:08:51 -0700 | [diff] [blame] | 6486 | |
| 6487 | out_unlock_inode: |
| 6488 | drop_inode = 1; |
| 6489 | unlock_new_inode(inode); |
| 6490 | goto out_unlock; |
| 6491 | |
Josef Bacik | 618e21d | 2007-07-11 10:18:17 -0400 | [diff] [blame] | 6492 | } |
| 6493 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 6494 | static int btrfs_create(struct inode *dir, struct dentry *dentry, |
Al Viro | ebfc3b4 | 2012-06-10 18:05:36 -0400 | [diff] [blame] | 6495 | umode_t mode, bool excl) |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 6496 | { |
| 6497 | struct btrfs_trans_handle *trans; |
| 6498 | struct btrfs_root *root = BTRFS_I(dir)->root; |
Chris Mason | 1832a6d | 2007-12-21 16:27:21 -0500 | [diff] [blame] | 6499 | struct inode *inode = NULL; |
Filipe Brandenburger | 43baa57 | 2012-11-30 03:40:09 +0000 | [diff] [blame] | 6500 | int drop_inode_on_err = 0; |
Yan, Zheng | a22285a | 2010-05-16 10:48:46 -0400 | [diff] [blame] | 6501 | int err; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 6502 | u64 objectid; |
Chris Mason | 00e4e6b | 2008-08-05 11:18:09 -0400 | [diff] [blame] | 6503 | u64 index = 0; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 6504 | |
Josef Bacik | 9ed74f2 | 2009-09-11 16:12:44 -0400 | [diff] [blame] | 6505 | /* |
| 6506 | * 2 for inode item and ref |
| 6507 | * 2 for dir items |
| 6508 | * 1 for xattr if selinux is on |
| 6509 | */ |
Yan, Zheng | a22285a | 2010-05-16 10:48:46 -0400 | [diff] [blame] | 6510 | trans = btrfs_start_transaction(root, 5); |
| 6511 | if (IS_ERR(trans)) |
| 6512 | return PTR_ERR(trans); |
Josef Bacik | 9ed74f2 | 2009-09-11 16:12:44 -0400 | [diff] [blame] | 6513 | |
Li Zefan | 581bb05 | 2011-04-20 10:06:11 +0800 | [diff] [blame] | 6514 | err = btrfs_find_free_ino(root, &objectid); |
| 6515 | if (err) |
| 6516 | goto out_unlock; |
| 6517 | |
Josef Bacik | aec7477 | 2008-07-24 12:12:38 -0400 | [diff] [blame] | 6518 | inode = btrfs_new_inode(trans, root, dir, dentry->d_name.name, |
Li Zefan | 33345d01 | 2011-04-20 10:31:50 +0800 | [diff] [blame] | 6519 | dentry->d_name.len, btrfs_ino(dir), objectid, |
Josef Bacik | d82a6f1 | 2011-05-11 15:26:06 -0400 | [diff] [blame] | 6520 | mode, &index); |
Tsutomu Itoh | 7cf96da | 2011-04-25 19:43:53 -0400 | [diff] [blame] | 6521 | if (IS_ERR(inode)) { |
| 6522 | err = PTR_ERR(inode); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 6523 | goto out_unlock; |
Tsutomu Itoh | 7cf96da | 2011-04-25 19:43:53 -0400 | [diff] [blame] | 6524 | } |
Filipe Brandenburger | 43baa57 | 2012-11-30 03:40:09 +0000 | [diff] [blame] | 6525 | drop_inode_on_err = 1; |
Casey Schaufler | ad19db7 | 2011-12-15 10:09:07 -0500 | [diff] [blame] | 6526 | /* |
| 6527 | * If the active LSM wants to access the inode during |
| 6528 | * d_instantiate it needs these. Smack checks to see |
| 6529 | * if the filesystem supports xattrs by looking at the |
| 6530 | * ops vector. |
| 6531 | */ |
| 6532 | inode->i_fop = &btrfs_file_operations; |
| 6533 | inode->i_op = &btrfs_file_inode_operations; |
Chris Mason | b0d5d10 | 2014-09-08 13:08:51 -0700 | [diff] [blame] | 6534 | inode->i_mapping->a_ops = &btrfs_aops; |
Chris Mason | b0d5d10 | 2014-09-08 13:08:51 -0700 | [diff] [blame] | 6535 | |
| 6536 | err = btrfs_init_inode_security(trans, inode, dir, &dentry->d_name); |
| 6537 | if (err) |
| 6538 | goto out_unlock_inode; |
| 6539 | |
| 6540 | err = btrfs_update_inode(trans, root, inode); |
| 6541 | if (err) |
| 6542 | goto out_unlock_inode; |
Casey Schaufler | ad19db7 | 2011-12-15 10:09:07 -0500 | [diff] [blame] | 6543 | |
Josef Bacik | a1b075d | 2010-11-19 20:36:11 +0000 | [diff] [blame] | 6544 | err = btrfs_add_nondir(trans, dir, dentry, inode, 0, index); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 6545 | if (err) |
Chris Mason | b0d5d10 | 2014-09-08 13:08:51 -0700 | [diff] [blame] | 6546 | goto out_unlock_inode; |
Filipe Brandenburger | 43baa57 | 2012-11-30 03:40:09 +0000 | [diff] [blame] | 6547 | |
Filipe Brandenburger | 43baa57 | 2012-11-30 03:40:09 +0000 | [diff] [blame] | 6548 | BTRFS_I(inode)->io_tree.ops = &btrfs_extent_io_ops; |
Chris Mason | b0d5d10 | 2014-09-08 13:08:51 -0700 | [diff] [blame] | 6549 | unlock_new_inode(inode); |
Filipe Brandenburger | 43baa57 | 2012-11-30 03:40:09 +0000 | [diff] [blame] | 6550 | d_instantiate(dentry, inode); |
| 6551 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 6552 | out_unlock: |
Josef Bacik | 7ad85bb | 2012-01-12 19:10:12 -0500 | [diff] [blame] | 6553 | btrfs_end_transaction(trans, root); |
Filipe Brandenburger | 43baa57 | 2012-11-30 03:40:09 +0000 | [diff] [blame] | 6554 | if (err && drop_inode_on_err) { |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 6555 | inode_dec_link_count(inode); |
| 6556 | iput(inode); |
| 6557 | } |
Josef Bacik | c581afc | 2014-02-06 16:06:06 -0500 | [diff] [blame] | 6558 | btrfs_balance_delayed_items(root); |
Liu Bo | b53d3f5 | 2012-11-14 14:34:34 +0000 | [diff] [blame] | 6559 | btrfs_btree_balance_dirty(root); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 6560 | return err; |
Chris Mason | b0d5d10 | 2014-09-08 13:08:51 -0700 | [diff] [blame] | 6561 | |
| 6562 | out_unlock_inode: |
| 6563 | unlock_new_inode(inode); |
| 6564 | goto out_unlock; |
| 6565 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 6566 | } |
| 6567 | |
| 6568 | static int btrfs_link(struct dentry *old_dentry, struct inode *dir, |
| 6569 | struct dentry *dentry) |
| 6570 | { |
Filipe Manana | 271dba4 | 2016-01-05 16:24:05 +0000 | [diff] [blame] | 6571 | struct btrfs_trans_handle *trans = NULL; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 6572 | struct btrfs_root *root = BTRFS_I(dir)->root; |
David Howells | 2b0143b | 2015-03-17 22:25:59 +0000 | [diff] [blame] | 6573 | struct inode *inode = d_inode(old_dentry); |
Chris Mason | 00e4e6b | 2008-08-05 11:18:09 -0400 | [diff] [blame] | 6574 | u64 index; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 6575 | int err; |
| 6576 | int drop_inode = 0; |
| 6577 | |
TARUISI Hiroaki | 4a8be42 | 2009-11-12 07:14:26 +0000 | [diff] [blame] | 6578 | /* do not allow sys_link's with other subvols of the same device */ |
| 6579 | if (root->objectid != BTRFS_I(inode)->root->objectid) |
Mark Fasheh | 3ab3564 | 2011-03-22 17:20:26 +0000 | [diff] [blame] | 6580 | return -EXDEV; |
TARUISI Hiroaki | 4a8be42 | 2009-11-12 07:14:26 +0000 | [diff] [blame] | 6581 | |
Mark Fasheh | f186373 | 2012-08-08 11:32:27 -0700 | [diff] [blame] | 6582 | if (inode->i_nlink >= BTRFS_LINK_MAX) |
Al Viro | c055e99 | 2011-03-04 17:15:18 +0000 | [diff] [blame] | 6583 | return -EMLINK; |
Josef Bacik | 9ed74f2 | 2009-09-11 16:12:44 -0400 | [diff] [blame] | 6584 | |
Chris Mason | 3de4586 | 2008-11-17 21:02:50 -0500 | [diff] [blame] | 6585 | err = btrfs_set_inode_index(dir, &index); |
Josef Bacik | aec7477 | 2008-07-24 12:12:38 -0400 | [diff] [blame] | 6586 | if (err) |
| 6587 | goto fail; |
| 6588 | |
Yan, Zheng | a22285a | 2010-05-16 10:48:46 -0400 | [diff] [blame] | 6589 | /* |
Miao Xie | 7e6b646 | 2011-02-18 09:21:17 +0000 | [diff] [blame] | 6590 | * 2 items for inode and inode ref |
Yan, Zheng | a22285a | 2010-05-16 10:48:46 -0400 | [diff] [blame] | 6591 | * 2 items for dir items |
Miao Xie | 7e6b646 | 2011-02-18 09:21:17 +0000 | [diff] [blame] | 6592 | * 1 item for parent inode |
Yan, Zheng | a22285a | 2010-05-16 10:48:46 -0400 | [diff] [blame] | 6593 | */ |
Miao Xie | 7e6b646 | 2011-02-18 09:21:17 +0000 | [diff] [blame] | 6594 | trans = btrfs_start_transaction(root, 5); |
Yan, Zheng | a22285a | 2010-05-16 10:48:46 -0400 | [diff] [blame] | 6595 | if (IS_ERR(trans)) { |
| 6596 | err = PTR_ERR(trans); |
Filipe Manana | 271dba4 | 2016-01-05 16:24:05 +0000 | [diff] [blame] | 6597 | trans = NULL; |
Yan, Zheng | a22285a | 2010-05-16 10:48:46 -0400 | [diff] [blame] | 6598 | goto fail; |
| 6599 | } |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 6600 | |
Miao Xie | 67de117 | 2013-12-26 13:07:06 +0800 | [diff] [blame] | 6601 | /* There are several dir indexes for this inode, clear the cache. */ |
| 6602 | BTRFS_I(inode)->dir_index = 0ULL; |
Zach Brown | 8b558c5 | 2013-10-16 12:10:34 -0700 | [diff] [blame] | 6603 | inc_nlink(inode); |
Josef Bacik | 0c4d2d9 | 2012-04-05 15:03:02 -0400 | [diff] [blame] | 6604 | inode_inc_iversion(inode); |
Deepa Dinamani | c2050a4 | 2016-09-14 07:48:06 -0700 | [diff] [blame] | 6605 | inode->i_ctime = current_time(inode); |
Al Viro | 7de9c6ee | 2010-10-23 11:11:40 -0400 | [diff] [blame] | 6606 | ihold(inode); |
Josef Bacik | e997615 | 2012-10-11 15:53:56 -0400 | [diff] [blame] | 6607 | set_bit(BTRFS_INODE_COPY_EVERYTHING, &BTRFS_I(inode)->runtime_flags); |
Josef Bacik | aec7477 | 2008-07-24 12:12:38 -0400 | [diff] [blame] | 6608 | |
Josef Bacik | a1b075d | 2010-11-19 20:36:11 +0000 | [diff] [blame] | 6609 | err = btrfs_add_nondir(trans, dir, dentry, inode, 1, index); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 6610 | |
Yan, Zheng | a571952 | 2009-09-24 09:17:31 -0400 | [diff] [blame] | 6611 | if (err) { |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 6612 | drop_inode = 1; |
Yan, Zheng | a571952 | 2009-09-24 09:17:31 -0400 | [diff] [blame] | 6613 | } else { |
Al Viro | 10d9f30 | 2011-07-16 23:09:10 -0400 | [diff] [blame] | 6614 | struct dentry *parent = dentry->d_parent; |
Yan, Zheng | a571952 | 2009-09-24 09:17:31 -0400 | [diff] [blame] | 6615 | err = btrfs_update_inode(trans, root, inode); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 6616 | if (err) |
| 6617 | goto fail; |
Filipe Manana | ef3b9af | 2014-04-27 20:40:45 +0100 | [diff] [blame] | 6618 | if (inode->i_nlink == 1) { |
| 6619 | /* |
| 6620 | * If new hard link count is 1, it's a file created |
| 6621 | * with open(2) O_TMPFILE flag. |
| 6622 | */ |
| 6623 | err = btrfs_orphan_del(trans, inode); |
| 6624 | if (err) |
| 6625 | goto fail; |
| 6626 | } |
Al Viro | 08c422c | 2011-12-23 07:58:13 -0500 | [diff] [blame] | 6627 | d_instantiate(dentry, inode); |
Josef Bacik | 6a91221 | 2010-11-20 09:48:00 +0000 | [diff] [blame] | 6628 | btrfs_log_new_name(trans, inode, NULL, parent); |
Yan, Zheng | a571952 | 2009-09-24 09:17:31 -0400 | [diff] [blame] | 6629 | } |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 6630 | |
Josef Bacik | c581afc | 2014-02-06 16:06:06 -0500 | [diff] [blame] | 6631 | btrfs_balance_delayed_items(root); |
Chris Mason | 1832a6d | 2007-12-21 16:27:21 -0500 | [diff] [blame] | 6632 | fail: |
Filipe Manana | 271dba4 | 2016-01-05 16:24:05 +0000 | [diff] [blame] | 6633 | if (trans) |
| 6634 | btrfs_end_transaction(trans, root); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 6635 | if (drop_inode) { |
| 6636 | inode_dec_link_count(inode); |
| 6637 | iput(inode); |
| 6638 | } |
Liu Bo | b53d3f5 | 2012-11-14 14:34:34 +0000 | [diff] [blame] | 6639 | btrfs_btree_balance_dirty(root); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 6640 | return err; |
| 6641 | } |
| 6642 | |
Al Viro | 18bb1db | 2011-07-26 01:41:39 -0400 | [diff] [blame] | 6643 | 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] | 6644 | { |
Chris Mason | b9d8666 | 2008-05-02 16:13:49 -0400 | [diff] [blame] | 6645 | struct inode *inode = NULL; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 6646 | struct btrfs_trans_handle *trans; |
| 6647 | struct btrfs_root *root = BTRFS_I(dir)->root; |
| 6648 | int err = 0; |
| 6649 | int drop_on_err = 0; |
Chris Mason | b9d8666 | 2008-05-02 16:13:49 -0400 | [diff] [blame] | 6650 | u64 objectid = 0; |
Chris Mason | 00e4e6b | 2008-08-05 11:18:09 -0400 | [diff] [blame] | 6651 | u64 index = 0; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 6652 | |
Josef Bacik | 9ed74f2 | 2009-09-11 16:12:44 -0400 | [diff] [blame] | 6653 | /* |
| 6654 | * 2 items for inode and ref |
| 6655 | * 2 items for dir items |
| 6656 | * 1 for xattr if selinux is on |
| 6657 | */ |
Yan, Zheng | a22285a | 2010-05-16 10:48:46 -0400 | [diff] [blame] | 6658 | trans = btrfs_start_transaction(root, 5); |
| 6659 | if (IS_ERR(trans)) |
| 6660 | return PTR_ERR(trans); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 6661 | |
Li Zefan | 581bb05 | 2011-04-20 10:06:11 +0800 | [diff] [blame] | 6662 | err = btrfs_find_free_ino(root, &objectid); |
| 6663 | if (err) |
| 6664 | goto out_fail; |
| 6665 | |
Josef Bacik | aec7477 | 2008-07-24 12:12:38 -0400 | [diff] [blame] | 6666 | inode = btrfs_new_inode(trans, root, dir, dentry->d_name.name, |
Li Zefan | 33345d01 | 2011-04-20 10:31:50 +0800 | [diff] [blame] | 6667 | dentry->d_name.len, btrfs_ino(dir), objectid, |
Josef Bacik | d82a6f1 | 2011-05-11 15:26:06 -0400 | [diff] [blame] | 6668 | S_IFDIR | mode, &index); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 6669 | if (IS_ERR(inode)) { |
| 6670 | err = PTR_ERR(inode); |
| 6671 | goto out_fail; |
| 6672 | } |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 6673 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 6674 | drop_on_err = 1; |
Chris Mason | b0d5d10 | 2014-09-08 13:08:51 -0700 | [diff] [blame] | 6675 | /* these must be set before we unlock the inode */ |
| 6676 | inode->i_op = &btrfs_dir_inode_operations; |
| 6677 | inode->i_fop = &btrfs_dir_file_operations; |
Josef Bacik | 33268ea | 2008-07-24 12:16:36 -0400 | [diff] [blame] | 6678 | |
Eric Paris | 2a7dba3 | 2011-02-01 11:05:39 -0500 | [diff] [blame] | 6679 | err = btrfs_init_inode_security(trans, inode, dir, &dentry->d_name); |
Josef Bacik | 33268ea | 2008-07-24 12:16:36 -0400 | [diff] [blame] | 6680 | if (err) |
Chris Mason | b0d5d10 | 2014-09-08 13:08:51 -0700 | [diff] [blame] | 6681 | goto out_fail_inode; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 6682 | |
Chris Mason | dbe674a | 2008-07-17 12:54:05 -0400 | [diff] [blame] | 6683 | btrfs_i_size_write(inode, 0); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 6684 | err = btrfs_update_inode(trans, root, inode); |
| 6685 | if (err) |
Chris Mason | b0d5d10 | 2014-09-08 13:08:51 -0700 | [diff] [blame] | 6686 | goto out_fail_inode; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 6687 | |
Josef Bacik | a1b075d | 2010-11-19 20:36:11 +0000 | [diff] [blame] | 6688 | err = btrfs_add_link(trans, dir, inode, dentry->d_name.name, |
| 6689 | dentry->d_name.len, 0, index); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 6690 | if (err) |
Chris Mason | b0d5d10 | 2014-09-08 13:08:51 -0700 | [diff] [blame] | 6691 | goto out_fail_inode; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 6692 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 6693 | d_instantiate(dentry, inode); |
Chris Mason | b0d5d10 | 2014-09-08 13:08:51 -0700 | [diff] [blame] | 6694 | /* |
| 6695 | * mkdir is special. We're unlocking after we call d_instantiate |
| 6696 | * to avoid a race with nfsd calling d_instantiate. |
| 6697 | */ |
| 6698 | unlock_new_inode(inode); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 6699 | drop_on_err = 0; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 6700 | |
| 6701 | out_fail: |
Josef Bacik | 7ad85bb | 2012-01-12 19:10:12 -0500 | [diff] [blame] | 6702 | btrfs_end_transaction(trans, root); |
Wang Shilong | c7cfb8a | 2014-12-24 14:45:30 +0800 | [diff] [blame] | 6703 | if (drop_on_err) { |
| 6704 | inode_dec_link_count(inode); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 6705 | iput(inode); |
Wang Shilong | c7cfb8a | 2014-12-24 14:45:30 +0800 | [diff] [blame] | 6706 | } |
Josef Bacik | c581afc | 2014-02-06 16:06:06 -0500 | [diff] [blame] | 6707 | btrfs_balance_delayed_items(root); |
Liu Bo | b53d3f5 | 2012-11-14 14:34:34 +0000 | [diff] [blame] | 6708 | btrfs_btree_balance_dirty(root); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 6709 | return err; |
Chris Mason | b0d5d10 | 2014-09-08 13:08:51 -0700 | [diff] [blame] | 6710 | |
| 6711 | out_fail_inode: |
| 6712 | unlock_new_inode(inode); |
| 6713 | goto out_fail; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 6714 | } |
| 6715 | |
Qu Wenruo | e6c4efd | 2014-09-17 11:53:35 +0800 | [diff] [blame] | 6716 | /* Find next extent map of a given extent map, caller needs to ensure locks */ |
| 6717 | static struct extent_map *next_extent_map(struct extent_map *em) |
| 6718 | { |
| 6719 | struct rb_node *next; |
| 6720 | |
| 6721 | next = rb_next(&em->rb_node); |
| 6722 | if (!next) |
| 6723 | return NULL; |
| 6724 | return container_of(next, struct extent_map, rb_node); |
| 6725 | } |
| 6726 | |
| 6727 | static struct extent_map *prev_extent_map(struct extent_map *em) |
| 6728 | { |
| 6729 | struct rb_node *prev; |
| 6730 | |
| 6731 | prev = rb_prev(&em->rb_node); |
| 6732 | if (!prev) |
| 6733 | return NULL; |
| 6734 | return container_of(prev, struct extent_map, rb_node); |
| 6735 | } |
| 6736 | |
Chris Mason | d352ac6 | 2008-09-29 15:18:18 -0400 | [diff] [blame] | 6737 | /* helper for btfs_get_extent. Given an existing extent in the tree, |
Qu Wenruo | e6c4efd | 2014-09-17 11:53:35 +0800 | [diff] [blame] | 6738 | * the existing extent is the nearest extent to map_start, |
Chris Mason | d352ac6 | 2008-09-29 15:18:18 -0400 | [diff] [blame] | 6739 | * 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] | 6740 | * the best fitted new extent into the tree. |
Chris Mason | d352ac6 | 2008-09-29 15:18:18 -0400 | [diff] [blame] | 6741 | */ |
Chris Mason | 3b95151 | 2008-04-17 11:29:12 -0400 | [diff] [blame] | 6742 | static int merge_extent_mapping(struct extent_map_tree *em_tree, |
| 6743 | struct extent_map *existing, |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 6744 | struct extent_map *em, |
Qu Wenruo | 51f395a | 2014-08-08 13:06:20 +0800 | [diff] [blame] | 6745 | u64 map_start) |
Chris Mason | 3b95151 | 2008-04-17 11:29:12 -0400 | [diff] [blame] | 6746 | { |
Qu Wenruo | e6c4efd | 2014-09-17 11:53:35 +0800 | [diff] [blame] | 6747 | struct extent_map *prev; |
| 6748 | struct extent_map *next; |
| 6749 | u64 start; |
| 6750 | u64 end; |
Chris Mason | 3b95151 | 2008-04-17 11:29:12 -0400 | [diff] [blame] | 6751 | u64 start_diff; |
Chris Mason | 3b95151 | 2008-04-17 11:29:12 -0400 | [diff] [blame] | 6752 | |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 6753 | BUG_ON(map_start < em->start || map_start >= extent_map_end(em)); |
Qu Wenruo | e6c4efd | 2014-09-17 11:53:35 +0800 | [diff] [blame] | 6754 | |
| 6755 | if (existing->start > map_start) { |
| 6756 | next = existing; |
| 6757 | prev = prev_extent_map(next); |
| 6758 | } else { |
| 6759 | prev = existing; |
| 6760 | next = next_extent_map(prev); |
| 6761 | } |
| 6762 | |
| 6763 | start = prev ? extent_map_end(prev) : em->start; |
| 6764 | start = max_t(u64, start, em->start); |
| 6765 | end = next ? next->start : extent_map_end(em); |
| 6766 | end = min_t(u64, end, extent_map_end(em)); |
| 6767 | start_diff = start - em->start; |
| 6768 | em->start = start; |
| 6769 | em->len = end - start; |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 6770 | if (em->block_start < EXTENT_MAP_LAST_BYTE && |
| 6771 | !test_bit(EXTENT_FLAG_COMPRESSED, &em->flags)) { |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 6772 | em->block_start += start_diff; |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 6773 | em->block_len -= start_diff; |
| 6774 | } |
Josef Bacik | 09a2a8f9 | 2013-04-05 16:51:15 -0400 | [diff] [blame] | 6775 | return add_extent_mapping(em_tree, em, 0); |
Chris Mason | 3b95151 | 2008-04-17 11:29:12 -0400 | [diff] [blame] | 6776 | } |
| 6777 | |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 6778 | static noinline int uncompress_inline(struct btrfs_path *path, |
Byongho Lee | e40da0e | 2015-05-19 23:46:45 +0900 | [diff] [blame] | 6779 | struct page *page, |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 6780 | size_t pg_offset, u64 extent_offset, |
| 6781 | struct btrfs_file_extent_item *item) |
| 6782 | { |
| 6783 | int ret; |
| 6784 | struct extent_buffer *leaf = path->nodes[0]; |
| 6785 | char *tmp; |
| 6786 | size_t max_size; |
| 6787 | unsigned long inline_size; |
| 6788 | unsigned long ptr; |
Li Zefan | 261507a0 | 2010-12-17 14:21:50 +0800 | [diff] [blame] | 6789 | int compress_type; |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 6790 | |
| 6791 | WARN_ON(pg_offset != 0); |
Li Zefan | 261507a0 | 2010-12-17 14:21:50 +0800 | [diff] [blame] | 6792 | compress_type = btrfs_file_extent_compression(leaf, item); |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 6793 | max_size = btrfs_file_extent_ram_bytes(leaf, item); |
| 6794 | inline_size = btrfs_file_extent_inline_item_len(leaf, |
Ross Kirk | dd3cc16 | 2013-09-16 15:58:09 +0100 | [diff] [blame] | 6795 | btrfs_item_nr(path->slots[0])); |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 6796 | tmp = kmalloc(inline_size, GFP_NOFS); |
Tsutomu Itoh | 8d41371 | 2011-04-25 19:43:52 -0400 | [diff] [blame] | 6797 | if (!tmp) |
| 6798 | return -ENOMEM; |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 6799 | ptr = btrfs_file_extent_inline_start(item); |
| 6800 | |
| 6801 | read_extent_buffer(leaf, tmp, ptr, inline_size); |
| 6802 | |
Kirill A. Shutemov | 09cbfea | 2016-04-01 15:29:47 +0300 | [diff] [blame] | 6803 | max_size = min_t(unsigned long, PAGE_SIZE, max_size); |
Li Zefan | 261507a0 | 2010-12-17 14:21:50 +0800 | [diff] [blame] | 6804 | ret = btrfs_decompress(compress_type, tmp, page, |
| 6805 | extent_offset, inline_size, max_size); |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 6806 | kfree(tmp); |
Zach Brown | 166ae5a | 2014-05-09 17:15:10 -0400 | [diff] [blame] | 6807 | return ret; |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 6808 | } |
| 6809 | |
Chris Mason | d352ac6 | 2008-09-29 15:18:18 -0400 | [diff] [blame] | 6810 | /* |
| 6811 | * 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] | 6812 | * the ugly parts come from merging extents from the disk with the in-ram |
| 6813 | * representation. This gets more complex because of the data=ordered code, |
Chris Mason | d352ac6 | 2008-09-29 15:18:18 -0400 | [diff] [blame] | 6814 | * where the in-ram extents might be locked pending data=ordered completion. |
| 6815 | * |
| 6816 | * This also copies inline extents directly into the page. |
| 6817 | */ |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 6818 | |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 6819 | struct extent_map *btrfs_get_extent(struct inode *inode, struct page *page, |
Chris Mason | 70dec80 | 2008-01-29 09:59:12 -0500 | [diff] [blame] | 6820 | size_t pg_offset, u64 start, u64 len, |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 6821 | int create) |
| 6822 | { |
| 6823 | int ret; |
| 6824 | int err = 0; |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 6825 | u64 extent_start = 0; |
| 6826 | u64 extent_end = 0; |
Li Zefan | 33345d01 | 2011-04-20 10:31:50 +0800 | [diff] [blame] | 6827 | u64 objectid = btrfs_ino(inode); |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 6828 | u32 found_type; |
Chris Mason | f421950 | 2008-07-22 11:18:09 -0400 | [diff] [blame] | 6829 | struct btrfs_path *path = NULL; |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 6830 | struct btrfs_root *root = BTRFS_I(inode)->root; |
| 6831 | struct btrfs_file_extent_item *item; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 6832 | struct extent_buffer *leaf; |
| 6833 | struct btrfs_key found_key; |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 6834 | struct extent_map *em = NULL; |
| 6835 | struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree; |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 6836 | struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree; |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 6837 | struct btrfs_trans_handle *trans = NULL; |
Filipe Manana | 7ffbb59 | 2014-06-09 03:48:05 +0100 | [diff] [blame] | 6838 | const bool new_inline = !page || create; |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 6839 | |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 6840 | again: |
Chris Mason | 890871b | 2009-09-02 16:24:52 -0400 | [diff] [blame] | 6841 | read_lock(&em_tree->lock); |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 6842 | em = lookup_extent_mapping(em_tree, start, len); |
Chris Mason | a061fc8 | 2008-05-07 11:43:44 -0400 | [diff] [blame] | 6843 | if (em) |
| 6844 | em->bdev = root->fs_info->fs_devices->latest_bdev; |
Chris Mason | 890871b | 2009-09-02 16:24:52 -0400 | [diff] [blame] | 6845 | read_unlock(&em_tree->lock); |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 6846 | |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 6847 | if (em) { |
Chris Mason | e1c4b74 | 2008-04-22 13:26:46 -0400 | [diff] [blame] | 6848 | if (em->start > start || em->start + em->len <= start) |
| 6849 | free_extent_map(em); |
| 6850 | else if (em->block_start == EXTENT_MAP_INLINE && page) |
Chris Mason | 70dec80 | 2008-01-29 09:59:12 -0500 | [diff] [blame] | 6851 | free_extent_map(em); |
| 6852 | else |
| 6853 | goto out; |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 6854 | } |
David Sterba | 172ddd6 | 2011-04-21 00:48:27 +0200 | [diff] [blame] | 6855 | em = alloc_extent_map(); |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 6856 | if (!em) { |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 6857 | err = -ENOMEM; |
| 6858 | goto out; |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 6859 | } |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 6860 | em->bdev = root->fs_info->fs_devices->latest_bdev; |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 6861 | em->start = EXTENT_MAP_HOLE; |
Chris Mason | 445a694 | 2008-11-10 11:53:33 -0500 | [diff] [blame] | 6862 | em->orig_start = EXTENT_MAP_HOLE; |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 6863 | em->len = (u64)-1; |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 6864 | em->block_len = (u64)-1; |
Chris Mason | f421950 | 2008-07-22 11:18:09 -0400 | [diff] [blame] | 6865 | |
| 6866 | if (!path) { |
| 6867 | path = btrfs_alloc_path(); |
Josef Bacik | 026fd31 | 2011-05-13 10:32:11 -0400 | [diff] [blame] | 6868 | if (!path) { |
| 6869 | err = -ENOMEM; |
| 6870 | goto out; |
| 6871 | } |
| 6872 | /* |
| 6873 | * Chances are we'll be called again, so go ahead and do |
| 6874 | * readahead |
| 6875 | */ |
David Sterba | e4058b5 | 2015-11-27 16:31:35 +0100 | [diff] [blame] | 6876 | path->reada = READA_FORWARD; |
Chris Mason | f421950 | 2008-07-22 11:18:09 -0400 | [diff] [blame] | 6877 | } |
| 6878 | |
Chris Mason | 179e29e | 2007-11-01 11:28:41 -0400 | [diff] [blame] | 6879 | ret = btrfs_lookup_file_extent(trans, root, path, |
| 6880 | objectid, start, trans != NULL); |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 6881 | if (ret < 0) { |
| 6882 | err = ret; |
| 6883 | goto out; |
| 6884 | } |
| 6885 | |
| 6886 | if (ret != 0) { |
| 6887 | if (path->slots[0] == 0) |
| 6888 | goto not_found; |
| 6889 | path->slots[0]--; |
| 6890 | } |
| 6891 | |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 6892 | leaf = path->nodes[0]; |
| 6893 | item = btrfs_item_ptr(leaf, path->slots[0], |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 6894 | struct btrfs_file_extent_item); |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 6895 | /* are we inside the extent that was found? */ |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 6896 | btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]); |
David Sterba | 962a298 | 2014-06-04 18:41:45 +0200 | [diff] [blame] | 6897 | found_type = found_key.type; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 6898 | if (found_key.objectid != objectid || |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 6899 | found_type != BTRFS_EXTENT_DATA_KEY) { |
Josef Bacik | 25a5034 | 2013-10-14 12:08:38 -0400 | [diff] [blame] | 6900 | /* |
| 6901 | * If we backup past the first extent we want to move forward |
| 6902 | * and see if there is an extent in front of us, otherwise we'll |
| 6903 | * say there is a hole for our whole search range which can |
| 6904 | * cause problems. |
| 6905 | */ |
| 6906 | extent_end = start; |
| 6907 | goto next; |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 6908 | } |
| 6909 | |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 6910 | found_type = btrfs_file_extent_type(leaf, item); |
| 6911 | extent_start = found_key.offset; |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 6912 | if (found_type == BTRFS_FILE_EXTENT_REG || |
| 6913 | found_type == BTRFS_FILE_EXTENT_PREALLOC) { |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 6914 | extent_end = extent_start + |
Chris Mason | db94535 | 2007-10-15 16:15:53 -0400 | [diff] [blame] | 6915 | btrfs_file_extent_num_bytes(leaf, item); |
Yan Zheng | 9036c10 | 2008-10-30 14:19:41 -0400 | [diff] [blame] | 6916 | } else if (found_type == BTRFS_FILE_EXTENT_INLINE) { |
| 6917 | size_t size; |
Chris Mason | 514ac8a | 2014-01-03 21:07:00 -0800 | [diff] [blame] | 6918 | size = btrfs_file_extent_inline_len(leaf, path->slots[0], item); |
Qu Wenruo | fda2832 | 2013-02-26 08:10:22 +0000 | [diff] [blame] | 6919 | extent_end = ALIGN(extent_start + size, root->sectorsize); |
Yan Zheng | 9036c10 | 2008-10-30 14:19:41 -0400 | [diff] [blame] | 6920 | } |
Josef Bacik | 25a5034 | 2013-10-14 12:08:38 -0400 | [diff] [blame] | 6921 | next: |
Yan Zheng | 9036c10 | 2008-10-30 14:19:41 -0400 | [diff] [blame] | 6922 | if (start >= extent_end) { |
| 6923 | path->slots[0]++; |
| 6924 | if (path->slots[0] >= btrfs_header_nritems(leaf)) { |
| 6925 | ret = btrfs_next_leaf(root, path); |
| 6926 | if (ret < 0) { |
| 6927 | err = ret; |
| 6928 | goto out; |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 6929 | } |
Yan Zheng | 9036c10 | 2008-10-30 14:19:41 -0400 | [diff] [blame] | 6930 | if (ret > 0) |
| 6931 | goto not_found; |
| 6932 | leaf = path->nodes[0]; |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 6933 | } |
Yan Zheng | 9036c10 | 2008-10-30 14:19:41 -0400 | [diff] [blame] | 6934 | btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]); |
| 6935 | if (found_key.objectid != objectid || |
| 6936 | found_key.type != BTRFS_EXTENT_DATA_KEY) |
| 6937 | goto not_found; |
| 6938 | if (start + len <= found_key.offset) |
| 6939 | goto not_found; |
Wang Shilong | e2eca69 | 2014-07-17 11:44:14 +0800 | [diff] [blame] | 6940 | if (start > found_key.offset) |
| 6941 | goto next; |
Yan Zheng | 9036c10 | 2008-10-30 14:19:41 -0400 | [diff] [blame] | 6942 | em->start = start; |
Josef Bacik | 70c8a91 | 2012-10-11 16:54:30 -0400 | [diff] [blame] | 6943 | em->orig_start = start; |
Yan Zheng | 9036c10 | 2008-10-30 14:19:41 -0400 | [diff] [blame] | 6944 | em->len = found_key.offset - start; |
| 6945 | goto not_found_em; |
| 6946 | } |
| 6947 | |
Filipe Manana | 7ffbb59 | 2014-06-09 03:48:05 +0100 | [diff] [blame] | 6948 | btrfs_extent_item_to_extent_map(inode, path, item, new_inline, em); |
| 6949 | |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 6950 | if (found_type == BTRFS_FILE_EXTENT_REG || |
| 6951 | found_type == BTRFS_FILE_EXTENT_PREALLOC) { |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 6952 | goto insert; |
| 6953 | } else if (found_type == BTRFS_FILE_EXTENT_INLINE) { |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 6954 | unsigned long ptr; |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 6955 | char *map; |
Chris Mason | 3326d1b | 2007-10-15 16:18:25 -0400 | [diff] [blame] | 6956 | size_t size; |
| 6957 | size_t extent_offset; |
| 6958 | size_t copy_size; |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 6959 | |
Filipe Manana | 7ffbb59 | 2014-06-09 03:48:05 +0100 | [diff] [blame] | 6960 | if (new_inline) |
Yan | 689f934 | 2007-10-29 11:41:07 -0400 | [diff] [blame] | 6961 | goto out; |
Yan | 689f934 | 2007-10-29 11:41:07 -0400 | [diff] [blame] | 6962 | |
Chris Mason | 514ac8a | 2014-01-03 21:07:00 -0800 | [diff] [blame] | 6963 | size = btrfs_file_extent_inline_len(leaf, path->slots[0], item); |
Yan Zheng | 9036c10 | 2008-10-30 14:19:41 -0400 | [diff] [blame] | 6964 | extent_offset = page_offset(page) + pg_offset - extent_start; |
Kirill A. Shutemov | 09cbfea | 2016-04-01 15:29:47 +0300 | [diff] [blame] | 6965 | copy_size = min_t(u64, PAGE_SIZE - pg_offset, |
| 6966 | size - extent_offset); |
Chris Mason | 3326d1b | 2007-10-15 16:18:25 -0400 | [diff] [blame] | 6967 | em->start = extent_start + extent_offset; |
Qu Wenruo | fda2832 | 2013-02-26 08:10:22 +0000 | [diff] [blame] | 6968 | em->len = ALIGN(copy_size, root->sectorsize); |
Josef Bacik | b493968 | 2012-12-03 10:31:19 -0500 | [diff] [blame] | 6969 | em->orig_block_len = em->len; |
Josef Bacik | 70c8a91 | 2012-10-11 16:54:30 -0400 | [diff] [blame] | 6970 | em->orig_start = em->start; |
Yan | 689f934 | 2007-10-29 11:41:07 -0400 | [diff] [blame] | 6971 | ptr = btrfs_file_extent_inline_start(item) + extent_offset; |
Chris Mason | 179e29e | 2007-11-01 11:28:41 -0400 | [diff] [blame] | 6972 | if (create == 0 && !PageUptodate(page)) { |
Li Zefan | 261507a0 | 2010-12-17 14:21:50 +0800 | [diff] [blame] | 6973 | if (btrfs_file_extent_compression(leaf, item) != |
| 6974 | BTRFS_COMPRESS_NONE) { |
Byongho Lee | e40da0e | 2015-05-19 23:46:45 +0900 | [diff] [blame] | 6975 | ret = uncompress_inline(path, page, pg_offset, |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 6976 | extent_offset, item); |
Zach Brown | 166ae5a | 2014-05-09 17:15:10 -0400 | [diff] [blame] | 6977 | if (ret) { |
| 6978 | err = ret; |
| 6979 | goto out; |
| 6980 | } |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 6981 | } else { |
| 6982 | map = kmap(page); |
| 6983 | read_extent_buffer(leaf, map + pg_offset, ptr, |
| 6984 | copy_size); |
Kirill A. Shutemov | 09cbfea | 2016-04-01 15:29:47 +0300 | [diff] [blame] | 6985 | if (pg_offset + copy_size < PAGE_SIZE) { |
Chris Mason | 93c82d5 | 2009-09-11 12:36:29 -0400 | [diff] [blame] | 6986 | memset(map + pg_offset + copy_size, 0, |
Kirill A. Shutemov | 09cbfea | 2016-04-01 15:29:47 +0300 | [diff] [blame] | 6987 | PAGE_SIZE - pg_offset - |
Chris Mason | 93c82d5 | 2009-09-11 12:36:29 -0400 | [diff] [blame] | 6988 | copy_size); |
| 6989 | } |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 6990 | kunmap(page); |
| 6991 | } |
Chris Mason | 179e29e | 2007-11-01 11:28:41 -0400 | [diff] [blame] | 6992 | flush_dcache_page(page); |
| 6993 | } else if (create && PageUptodate(page)) { |
Jan Schmidt | 6bf7e08 | 2011-12-01 14:35:19 +0100 | [diff] [blame] | 6994 | BUG(); |
Chris Mason | 179e29e | 2007-11-01 11:28:41 -0400 | [diff] [blame] | 6995 | if (!trans) { |
| 6996 | kunmap(page); |
| 6997 | free_extent_map(em); |
| 6998 | em = NULL; |
Chris Mason | ff5714c | 2011-05-28 07:00:39 -0400 | [diff] [blame] | 6999 | |
David Sterba | b3b4aa7 | 2011-04-21 01:20:15 +0200 | [diff] [blame] | 7000 | btrfs_release_path(path); |
Josef Bacik | 7a7eaa4 | 2011-04-13 12:54:33 -0400 | [diff] [blame] | 7001 | trans = btrfs_join_transaction(root); |
Chris Mason | ff5714c | 2011-05-28 07:00:39 -0400 | [diff] [blame] | 7002 | |
Tsutomu Itoh | 3612b49 | 2011-01-25 02:51:38 +0000 | [diff] [blame] | 7003 | if (IS_ERR(trans)) |
| 7004 | return ERR_CAST(trans); |
Chris Mason | 179e29e | 2007-11-01 11:28:41 -0400 | [diff] [blame] | 7005 | goto again; |
| 7006 | } |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 7007 | map = kmap(page); |
Chris Mason | 70dec80 | 2008-01-29 09:59:12 -0500 | [diff] [blame] | 7008 | write_extent_buffer(leaf, map + pg_offset, ptr, |
Chris Mason | 179e29e | 2007-11-01 11:28:41 -0400 | [diff] [blame] | 7009 | copy_size); |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 7010 | kunmap(page); |
Chris Mason | 179e29e | 2007-11-01 11:28:41 -0400 | [diff] [blame] | 7011 | btrfs_mark_buffer_dirty(leaf); |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 7012 | } |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 7013 | set_extent_uptodate(io_tree, em->start, |
Arne Jansen | 507903b | 2011-04-06 10:02:20 +0000 | [diff] [blame] | 7014 | extent_map_end(em) - 1, NULL, GFP_NOFS); |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 7015 | goto insert; |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 7016 | } |
| 7017 | not_found: |
| 7018 | em->start = start; |
Josef Bacik | 70c8a91 | 2012-10-11 16:54:30 -0400 | [diff] [blame] | 7019 | em->orig_start = start; |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 7020 | em->len = len; |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 7021 | not_found_em: |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 7022 | em->block_start = EXTENT_MAP_HOLE; |
Yan Zheng | 9036c10 | 2008-10-30 14:19:41 -0400 | [diff] [blame] | 7023 | set_bit(EXTENT_FLAG_VACANCY, &em->flags); |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 7024 | insert: |
David Sterba | b3b4aa7 | 2011-04-21 01:20:15 +0200 | [diff] [blame] | 7025 | btrfs_release_path(path); |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 7026 | if (em->start > start || extent_map_end(em) <= start) { |
Jeff Mahoney | 5d163e0 | 2016-09-20 10:05:00 -0400 | [diff] [blame] | 7027 | btrfs_err(root->fs_info, |
| 7028 | "bad extent! em: [%llu %llu] passed [%llu %llu]", |
| 7029 | em->start, em->len, start, len); |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 7030 | err = -EIO; |
| 7031 | goto out; |
| 7032 | } |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 7033 | |
| 7034 | err = 0; |
Chris Mason | 890871b | 2009-09-02 16:24:52 -0400 | [diff] [blame] | 7035 | write_lock(&em_tree->lock); |
Josef Bacik | 09a2a8f9 | 2013-04-05 16:51:15 -0400 | [diff] [blame] | 7036 | ret = add_extent_mapping(em_tree, em, 0); |
Chris Mason | 3b95151 | 2008-04-17 11:29:12 -0400 | [diff] [blame] | 7037 | /* it is possible that someone inserted the extent into the tree |
| 7038 | * while we had the lock dropped. It is also possible that |
| 7039 | * an overlapping map exists in the tree |
| 7040 | */ |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 7041 | if (ret == -EEXIST) { |
Chris Mason | 3b95151 | 2008-04-17 11:29:12 -0400 | [diff] [blame] | 7042 | struct extent_map *existing; |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 7043 | |
| 7044 | ret = 0; |
| 7045 | |
Qu Wenruo | e6c4efd | 2014-09-17 11:53:35 +0800 | [diff] [blame] | 7046 | existing = search_extent_mapping(em_tree, start, len); |
| 7047 | /* |
| 7048 | * existing will always be non-NULL, since there must be |
| 7049 | * extent causing the -EEXIST. |
| 7050 | */ |
Chris Mason | 8dff9c8 | 2015-09-19 11:28:25 -0700 | [diff] [blame] | 7051 | if (existing->start == em->start && |
| 7052 | extent_map_end(existing) == extent_map_end(em) && |
| 7053 | em->block_start == existing->block_start) { |
| 7054 | /* |
| 7055 | * these two extents are the same, it happens |
| 7056 | * with inlines especially |
| 7057 | */ |
| 7058 | free_extent_map(em); |
| 7059 | em = existing; |
| 7060 | err = 0; |
| 7061 | |
| 7062 | } else if (start >= extent_map_end(existing) || |
Qu Wenruo | 32be3a1 | 2014-09-22 09:13:03 +0800 | [diff] [blame] | 7063 | start <= existing->start) { |
Qu Wenruo | e6c4efd | 2014-09-17 11:53:35 +0800 | [diff] [blame] | 7064 | /* |
| 7065 | * The existing extent map is the one nearest to |
| 7066 | * the [start, start + len) range which overlaps |
| 7067 | */ |
| 7068 | err = merge_extent_mapping(em_tree, existing, |
| 7069 | em, start); |
Chris Mason | e1c4b74 | 2008-04-22 13:26:46 -0400 | [diff] [blame] | 7070 | free_extent_map(existing); |
Qu Wenruo | e6c4efd | 2014-09-17 11:53:35 +0800 | [diff] [blame] | 7071 | if (err) { |
Chris Mason | 3b95151 | 2008-04-17 11:29:12 -0400 | [diff] [blame] | 7072 | free_extent_map(em); |
| 7073 | em = NULL; |
| 7074 | } |
| 7075 | } else { |
| 7076 | free_extent_map(em); |
| 7077 | em = existing; |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 7078 | err = 0; |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 7079 | } |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 7080 | } |
Chris Mason | 890871b | 2009-09-02 16:24:52 -0400 | [diff] [blame] | 7081 | write_unlock(&em_tree->lock); |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 7082 | out: |
liubo | 1abe9b8 | 2011-03-24 11:18:59 +0000 | [diff] [blame] | 7083 | |
Steven Rostedt | 4cd8587c | 2013-11-14 22:57:29 -0500 | [diff] [blame] | 7084 | trace_btrfs_get_extent(root, em); |
liubo | 1abe9b8 | 2011-03-24 11:18:59 +0000 | [diff] [blame] | 7085 | |
Tsutomu Itoh | 527afb4 | 2015-08-19 14:55:00 +0900 | [diff] [blame] | 7086 | btrfs_free_path(path); |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 7087 | if (trans) { |
| 7088 | ret = btrfs_end_transaction(trans, root); |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 7089 | if (!err) |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 7090 | err = ret; |
| 7091 | } |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 7092 | if (err) { |
| 7093 | free_extent_map(em); |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 7094 | return ERR_PTR(err); |
| 7095 | } |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 7096 | BUG_ON(!em); /* Error is always set */ |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 7097 | return em; |
| 7098 | } |
| 7099 | |
Chris Mason | ec29ed5 | 2011-02-23 16:23:20 -0500 | [diff] [blame] | 7100 | struct extent_map *btrfs_get_extent_fiemap(struct inode *inode, struct page *page, |
| 7101 | size_t pg_offset, u64 start, u64 len, |
| 7102 | int create) |
| 7103 | { |
| 7104 | struct extent_map *em; |
| 7105 | struct extent_map *hole_em = NULL; |
| 7106 | u64 range_start = start; |
| 7107 | u64 end; |
| 7108 | u64 found; |
| 7109 | u64 found_end; |
| 7110 | int err = 0; |
| 7111 | |
| 7112 | em = btrfs_get_extent(inode, page, pg_offset, start, len, create); |
| 7113 | if (IS_ERR(em)) |
| 7114 | return em; |
| 7115 | if (em) { |
| 7116 | /* |
Liu Bo | f9e4fb5 | 2013-01-07 10:10:12 +0000 | [diff] [blame] | 7117 | * if our em maps to |
| 7118 | * - a hole or |
| 7119 | * - a pre-alloc extent, |
| 7120 | * there might actually be delalloc bytes behind it. |
Chris Mason | ec29ed5 | 2011-02-23 16:23:20 -0500 | [diff] [blame] | 7121 | */ |
Liu Bo | f9e4fb5 | 2013-01-07 10:10:12 +0000 | [diff] [blame] | 7122 | if (em->block_start != EXTENT_MAP_HOLE && |
| 7123 | !test_bit(EXTENT_FLAG_PREALLOC, &em->flags)) |
Chris Mason | ec29ed5 | 2011-02-23 16:23:20 -0500 | [diff] [blame] | 7124 | return em; |
| 7125 | else |
| 7126 | hole_em = em; |
| 7127 | } |
| 7128 | |
| 7129 | /* check to see if we've wrapped (len == -1 or similar) */ |
| 7130 | end = start + len; |
| 7131 | if (end < start) |
| 7132 | end = (u64)-1; |
| 7133 | else |
| 7134 | end -= 1; |
| 7135 | |
| 7136 | em = NULL; |
| 7137 | |
| 7138 | /* ok, we didn't find anything, lets look for delalloc */ |
| 7139 | found = count_range_bits(&BTRFS_I(inode)->io_tree, &range_start, |
| 7140 | end, len, EXTENT_DELALLOC, 1); |
| 7141 | found_end = range_start + found; |
| 7142 | if (found_end < range_start) |
| 7143 | found_end = (u64)-1; |
| 7144 | |
| 7145 | /* |
| 7146 | * we didn't find anything useful, return |
| 7147 | * the original results from get_extent() |
| 7148 | */ |
| 7149 | if (range_start > end || found_end <= start) { |
| 7150 | em = hole_em; |
| 7151 | hole_em = NULL; |
| 7152 | goto out; |
| 7153 | } |
| 7154 | |
| 7155 | /* adjust the range_start to make sure it doesn't |
| 7156 | * go backwards from the start they passed in |
| 7157 | */ |
Dulshani Gunawardhana | 6787125 | 2013-10-31 10:33:04 +0530 | [diff] [blame] | 7158 | range_start = max(start, range_start); |
Chris Mason | ec29ed5 | 2011-02-23 16:23:20 -0500 | [diff] [blame] | 7159 | found = found_end - range_start; |
| 7160 | |
| 7161 | if (found > 0) { |
| 7162 | u64 hole_start = start; |
| 7163 | u64 hole_len = len; |
| 7164 | |
David Sterba | 172ddd6 | 2011-04-21 00:48:27 +0200 | [diff] [blame] | 7165 | em = alloc_extent_map(); |
Chris Mason | ec29ed5 | 2011-02-23 16:23:20 -0500 | [diff] [blame] | 7166 | if (!em) { |
| 7167 | err = -ENOMEM; |
| 7168 | goto out; |
| 7169 | } |
| 7170 | /* |
| 7171 | * when btrfs_get_extent can't find anything it |
| 7172 | * returns one huge hole |
| 7173 | * |
| 7174 | * make sure what it found really fits our range, and |
| 7175 | * adjust to make sure it is based on the start from |
| 7176 | * the caller |
| 7177 | */ |
| 7178 | if (hole_em) { |
| 7179 | u64 calc_end = extent_map_end(hole_em); |
| 7180 | |
| 7181 | if (calc_end <= start || (hole_em->start > end)) { |
| 7182 | free_extent_map(hole_em); |
| 7183 | hole_em = NULL; |
| 7184 | } else { |
| 7185 | hole_start = max(hole_em->start, start); |
| 7186 | hole_len = calc_end - hole_start; |
| 7187 | } |
| 7188 | } |
| 7189 | em->bdev = NULL; |
| 7190 | if (hole_em && range_start > hole_start) { |
| 7191 | /* our hole starts before our delalloc, so we |
| 7192 | * have to return just the parts of the hole |
| 7193 | * that go until the delalloc starts |
| 7194 | */ |
| 7195 | em->len = min(hole_len, |
| 7196 | range_start - hole_start); |
| 7197 | em->start = hole_start; |
| 7198 | em->orig_start = hole_start; |
| 7199 | /* |
| 7200 | * don't adjust block start at all, |
| 7201 | * it is fixed at EXTENT_MAP_HOLE |
| 7202 | */ |
| 7203 | em->block_start = hole_em->block_start; |
| 7204 | em->block_len = hole_len; |
Liu Bo | f9e4fb5 | 2013-01-07 10:10:12 +0000 | [diff] [blame] | 7205 | if (test_bit(EXTENT_FLAG_PREALLOC, &hole_em->flags)) |
| 7206 | set_bit(EXTENT_FLAG_PREALLOC, &em->flags); |
Chris Mason | ec29ed5 | 2011-02-23 16:23:20 -0500 | [diff] [blame] | 7207 | } else { |
| 7208 | em->start = range_start; |
| 7209 | em->len = found; |
| 7210 | em->orig_start = range_start; |
| 7211 | em->block_start = EXTENT_MAP_DELALLOC; |
| 7212 | em->block_len = found; |
| 7213 | } |
| 7214 | } else if (hole_em) { |
| 7215 | return hole_em; |
| 7216 | } |
| 7217 | out: |
| 7218 | |
| 7219 | free_extent_map(hole_em); |
| 7220 | if (err) { |
| 7221 | free_extent_map(em); |
| 7222 | return ERR_PTR(err); |
| 7223 | } |
| 7224 | return em; |
| 7225 | } |
| 7226 | |
Filipe Manana | 5f9a8a5 | 2016-05-12 13:53:36 +0100 | [diff] [blame] | 7227 | static struct extent_map *btrfs_create_dio_extent(struct inode *inode, |
| 7228 | const u64 start, |
| 7229 | const u64 len, |
| 7230 | const u64 orig_start, |
| 7231 | const u64 block_start, |
| 7232 | const u64 block_len, |
| 7233 | const u64 orig_block_len, |
| 7234 | const u64 ram_bytes, |
| 7235 | const int type) |
| 7236 | { |
| 7237 | struct extent_map *em = NULL; |
| 7238 | int ret; |
| 7239 | |
| 7240 | down_read(&BTRFS_I(inode)->dio_sem); |
| 7241 | if (type != BTRFS_ORDERED_NOCOW) { |
| 7242 | em = create_pinned_em(inode, start, len, orig_start, |
| 7243 | block_start, block_len, orig_block_len, |
| 7244 | ram_bytes, type); |
| 7245 | if (IS_ERR(em)) |
| 7246 | goto out; |
| 7247 | } |
| 7248 | ret = btrfs_add_ordered_extent_dio(inode, start, block_start, |
| 7249 | len, block_len, type); |
| 7250 | if (ret) { |
| 7251 | if (em) { |
| 7252 | free_extent_map(em); |
| 7253 | btrfs_drop_extent_cache(inode, start, |
| 7254 | start + len - 1, 0); |
| 7255 | } |
| 7256 | em = ERR_PTR(ret); |
| 7257 | } |
| 7258 | out: |
| 7259 | up_read(&BTRFS_I(inode)->dio_sem); |
| 7260 | |
| 7261 | return em; |
| 7262 | } |
| 7263 | |
Josef Bacik | 4b46fce | 2010-05-23 11:00:55 -0400 | [diff] [blame] | 7264 | static struct extent_map *btrfs_new_extent_direct(struct inode *inode, |
| 7265 | u64 start, u64 len) |
| 7266 | { |
| 7267 | struct btrfs_root *root = BTRFS_I(inode)->root; |
Josef Bacik | 70c8a91 | 2012-10-11 16:54:30 -0400 | [diff] [blame] | 7268 | struct extent_map *em; |
Josef Bacik | 4b46fce | 2010-05-23 11:00:55 -0400 | [diff] [blame] | 7269 | struct btrfs_key ins; |
| 7270 | u64 alloc_hint; |
| 7271 | int ret; |
Josef Bacik | 4b46fce | 2010-05-23 11:00:55 -0400 | [diff] [blame] | 7272 | |
Josef Bacik | 4b46fce | 2010-05-23 11:00:55 -0400 | [diff] [blame] | 7273 | alloc_hint = get_extent_allocation_hint(inode, start, len); |
Wang Xiaoguang | 1851309 | 2016-07-25 15:51:40 +0800 | [diff] [blame] | 7274 | ret = btrfs_reserve_extent(root, len, len, root->sectorsize, 0, |
Miao Xie | e570fd2 | 2014-06-19 10:42:50 +0800 | [diff] [blame] | 7275 | alloc_hint, &ins, 1, 1); |
Josef Bacik | 0036158 | 2013-08-14 14:02:47 -0400 | [diff] [blame] | 7276 | if (ret) |
| 7277 | return ERR_PTR(ret); |
Josef Bacik | 4b46fce | 2010-05-23 11:00:55 -0400 | [diff] [blame] | 7278 | |
Filipe Manana | 5f9a8a5 | 2016-05-12 13:53:36 +0100 | [diff] [blame] | 7279 | em = btrfs_create_dio_extent(inode, start, ins.offset, start, |
| 7280 | ins.objectid, ins.offset, ins.offset, |
| 7281 | ins.offset, 0); |
Filipe Manana | 9cfa3e3 | 2016-04-26 15:39:32 +0100 | [diff] [blame] | 7282 | btrfs_dec_block_group_reservations(root->fs_info, ins.objectid); |
Filipe Manana | 5f9a8a5 | 2016-05-12 13:53:36 +0100 | [diff] [blame] | 7283 | if (IS_ERR(em)) |
Miao Xie | e570fd2 | 2014-06-19 10:42:50 +0800 | [diff] [blame] | 7284 | btrfs_free_reserved_extent(root, ins.objectid, ins.offset, 1); |
Josef Bacik | 0036158 | 2013-08-14 14:02:47 -0400 | [diff] [blame] | 7285 | |
Josef Bacik | 4b46fce | 2010-05-23 11:00:55 -0400 | [diff] [blame] | 7286 | return em; |
| 7287 | } |
| 7288 | |
Chris Mason | 46bfbb5 | 2010-05-26 11:04:10 -0400 | [diff] [blame] | 7289 | /* |
| 7290 | * returns 1 when the nocow is safe, < 1 on error, 0 if the |
| 7291 | * block must be cow'd |
| 7292 | */ |
Josef Bacik | 0036158 | 2013-08-14 14:02:47 -0400 | [diff] [blame] | 7293 | noinline int can_nocow_extent(struct inode *inode, u64 offset, u64 *len, |
Josef Bacik | 7ee9e44 | 2013-06-21 16:37:03 -0400 | [diff] [blame] | 7294 | u64 *orig_start, u64 *orig_block_len, |
| 7295 | u64 *ram_bytes) |
Chris Mason | 46bfbb5 | 2010-05-26 11:04:10 -0400 | [diff] [blame] | 7296 | { |
Josef Bacik | 0036158 | 2013-08-14 14:02:47 -0400 | [diff] [blame] | 7297 | struct btrfs_trans_handle *trans; |
Chris Mason | 46bfbb5 | 2010-05-26 11:04:10 -0400 | [diff] [blame] | 7298 | struct btrfs_path *path; |
| 7299 | int ret; |
| 7300 | struct extent_buffer *leaf; |
| 7301 | struct btrfs_root *root = BTRFS_I(inode)->root; |
Miao Xie | 7b2b708 | 2014-02-27 13:58:05 +0800 | [diff] [blame] | 7302 | struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree; |
Chris Mason | 46bfbb5 | 2010-05-26 11:04:10 -0400 | [diff] [blame] | 7303 | struct btrfs_file_extent_item *fi; |
| 7304 | struct btrfs_key key; |
| 7305 | u64 disk_bytenr; |
| 7306 | u64 backref_offset; |
| 7307 | u64 extent_end; |
| 7308 | u64 num_bytes; |
| 7309 | int slot; |
| 7310 | int found_type; |
Josef Bacik | 7ee9e44 | 2013-06-21 16:37:03 -0400 | [diff] [blame] | 7311 | bool nocow = (BTRFS_I(inode)->flags & BTRFS_INODE_NODATACOW); |
Miao Xie | e77751a | 2013-12-27 21:11:50 +0800 | [diff] [blame] | 7312 | |
Chris Mason | 46bfbb5 | 2010-05-26 11:04:10 -0400 | [diff] [blame] | 7313 | path = btrfs_alloc_path(); |
| 7314 | if (!path) |
| 7315 | return -ENOMEM; |
| 7316 | |
Josef Bacik | 0036158 | 2013-08-14 14:02:47 -0400 | [diff] [blame] | 7317 | ret = btrfs_lookup_file_extent(NULL, root, path, btrfs_ino(inode), |
Chris Mason | 46bfbb5 | 2010-05-26 11:04:10 -0400 | [diff] [blame] | 7318 | offset, 0); |
| 7319 | if (ret < 0) |
| 7320 | goto out; |
| 7321 | |
| 7322 | slot = path->slots[0]; |
| 7323 | if (ret == 1) { |
| 7324 | if (slot == 0) { |
| 7325 | /* can't find the item, must cow */ |
| 7326 | ret = 0; |
| 7327 | goto out; |
| 7328 | } |
| 7329 | slot--; |
| 7330 | } |
| 7331 | ret = 0; |
| 7332 | leaf = path->nodes[0]; |
| 7333 | btrfs_item_key_to_cpu(leaf, &key, slot); |
Li Zefan | 33345d01 | 2011-04-20 10:31:50 +0800 | [diff] [blame] | 7334 | if (key.objectid != btrfs_ino(inode) || |
Chris Mason | 46bfbb5 | 2010-05-26 11:04:10 -0400 | [diff] [blame] | 7335 | key.type != BTRFS_EXTENT_DATA_KEY) { |
| 7336 | /* not our file or wrong item type, must cow */ |
| 7337 | goto out; |
| 7338 | } |
| 7339 | |
| 7340 | if (key.offset > offset) { |
| 7341 | /* Wrong offset, must cow */ |
| 7342 | goto out; |
| 7343 | } |
| 7344 | |
| 7345 | fi = btrfs_item_ptr(leaf, slot, struct btrfs_file_extent_item); |
| 7346 | found_type = btrfs_file_extent_type(leaf, fi); |
| 7347 | if (found_type != BTRFS_FILE_EXTENT_REG && |
| 7348 | found_type != BTRFS_FILE_EXTENT_PREALLOC) { |
| 7349 | /* not a regular extent, must cow */ |
| 7350 | goto out; |
| 7351 | } |
Josef Bacik | 7ee9e44 | 2013-06-21 16:37:03 -0400 | [diff] [blame] | 7352 | |
| 7353 | if (!nocow && found_type == BTRFS_FILE_EXTENT_REG) |
| 7354 | goto out; |
| 7355 | |
Miao Xie | e77751a | 2013-12-27 21:11:50 +0800 | [diff] [blame] | 7356 | extent_end = key.offset + btrfs_file_extent_num_bytes(leaf, fi); |
| 7357 | if (extent_end <= offset) |
| 7358 | goto out; |
| 7359 | |
Chris Mason | 46bfbb5 | 2010-05-26 11:04:10 -0400 | [diff] [blame] | 7360 | disk_bytenr = btrfs_file_extent_disk_bytenr(leaf, fi); |
Josef Bacik | 7ee9e44 | 2013-06-21 16:37:03 -0400 | [diff] [blame] | 7361 | if (disk_bytenr == 0) |
| 7362 | goto out; |
| 7363 | |
| 7364 | if (btrfs_file_extent_compression(leaf, fi) || |
| 7365 | btrfs_file_extent_encryption(leaf, fi) || |
| 7366 | btrfs_file_extent_other_encoding(leaf, fi)) |
| 7367 | goto out; |
| 7368 | |
Chris Mason | 46bfbb5 | 2010-05-26 11:04:10 -0400 | [diff] [blame] | 7369 | backref_offset = btrfs_file_extent_offset(leaf, fi); |
| 7370 | |
Josef Bacik | 7ee9e44 | 2013-06-21 16:37:03 -0400 | [diff] [blame] | 7371 | if (orig_start) { |
| 7372 | *orig_start = key.offset - backref_offset; |
| 7373 | *orig_block_len = btrfs_file_extent_disk_num_bytes(leaf, fi); |
| 7374 | *ram_bytes = btrfs_file_extent_ram_bytes(leaf, fi); |
| 7375 | } |
Josef Bacik | eb384b5 | 2013-04-24 16:32:55 -0400 | [diff] [blame] | 7376 | |
Chris Mason | 46bfbb5 | 2010-05-26 11:04:10 -0400 | [diff] [blame] | 7377 | if (btrfs_extent_readonly(root, disk_bytenr)) |
| 7378 | goto out; |
Miao Xie | 7b2b708 | 2014-02-27 13:58:05 +0800 | [diff] [blame] | 7379 | |
| 7380 | num_bytes = min(offset + *len, extent_end) - offset; |
| 7381 | if (!nocow && found_type == BTRFS_FILE_EXTENT_PREALLOC) { |
| 7382 | u64 range_end; |
| 7383 | |
| 7384 | range_end = round_up(offset + num_bytes, root->sectorsize) - 1; |
| 7385 | ret = test_range_bit(io_tree, offset, range_end, |
| 7386 | EXTENT_DELALLOC, 0, NULL); |
| 7387 | if (ret) { |
| 7388 | ret = -EAGAIN; |
| 7389 | goto out; |
| 7390 | } |
| 7391 | } |
| 7392 | |
Josef Bacik | 1bda19e | 2013-10-18 12:10:36 -0400 | [diff] [blame] | 7393 | btrfs_release_path(path); |
Chris Mason | 46bfbb5 | 2010-05-26 11:04:10 -0400 | [diff] [blame] | 7394 | |
| 7395 | /* |
| 7396 | * look for other files referencing this extent, if we |
| 7397 | * find any we must cow |
| 7398 | */ |
Josef Bacik | 0036158 | 2013-08-14 14:02:47 -0400 | [diff] [blame] | 7399 | trans = btrfs_join_transaction(root); |
| 7400 | if (IS_ERR(trans)) { |
| 7401 | ret = 0; |
Chris Mason | 46bfbb5 | 2010-05-26 11:04:10 -0400 | [diff] [blame] | 7402 | goto out; |
Josef Bacik | 0036158 | 2013-08-14 14:02:47 -0400 | [diff] [blame] | 7403 | } |
| 7404 | |
| 7405 | ret = btrfs_cross_ref_exist(trans, root, btrfs_ino(inode), |
| 7406 | key.offset - backref_offset, disk_bytenr); |
| 7407 | btrfs_end_transaction(trans, root); |
| 7408 | if (ret) { |
| 7409 | ret = 0; |
| 7410 | goto out; |
| 7411 | } |
Chris Mason | 46bfbb5 | 2010-05-26 11:04:10 -0400 | [diff] [blame] | 7412 | |
| 7413 | /* |
| 7414 | * adjust disk_bytenr and num_bytes to cover just the bytes |
| 7415 | * in this extent we are about to write. If there |
| 7416 | * are any csums in that range we have to cow in order |
| 7417 | * to keep the csums correct |
| 7418 | */ |
| 7419 | disk_bytenr += backref_offset; |
| 7420 | disk_bytenr += offset - key.offset; |
Chris Mason | 46bfbb5 | 2010-05-26 11:04:10 -0400 | [diff] [blame] | 7421 | if (csum_exist_in_range(root, disk_bytenr, num_bytes)) |
| 7422 | goto out; |
| 7423 | /* |
| 7424 | * all of the above have passed, it is safe to overwrite this extent |
| 7425 | * without cow |
| 7426 | */ |
Josef Bacik | eb384b5 | 2013-04-24 16:32:55 -0400 | [diff] [blame] | 7427 | *len = num_bytes; |
Chris Mason | 46bfbb5 | 2010-05-26 11:04:10 -0400 | [diff] [blame] | 7428 | ret = 1; |
| 7429 | out: |
| 7430 | btrfs_free_path(path); |
| 7431 | return ret; |
| 7432 | } |
| 7433 | |
Alex Gartrell | fc4adbf | 2014-05-20 13:07:56 -0700 | [diff] [blame] | 7434 | bool btrfs_page_exists_in_range(struct inode *inode, loff_t start, loff_t end) |
| 7435 | { |
| 7436 | struct radix_tree_root *root = &inode->i_mapping->page_tree; |
| 7437 | int found = false; |
| 7438 | void **pagep = NULL; |
| 7439 | struct page *page = NULL; |
| 7440 | int start_idx; |
| 7441 | int end_idx; |
| 7442 | |
Kirill A. Shutemov | 09cbfea | 2016-04-01 15:29:47 +0300 | [diff] [blame] | 7443 | start_idx = start >> PAGE_SHIFT; |
Alex Gartrell | fc4adbf | 2014-05-20 13:07:56 -0700 | [diff] [blame] | 7444 | |
| 7445 | /* |
| 7446 | * end is the last byte in the last page. end == start is legal |
| 7447 | */ |
Kirill A. Shutemov | 09cbfea | 2016-04-01 15:29:47 +0300 | [diff] [blame] | 7448 | end_idx = end >> PAGE_SHIFT; |
Alex Gartrell | fc4adbf | 2014-05-20 13:07:56 -0700 | [diff] [blame] | 7449 | |
| 7450 | rcu_read_lock(); |
| 7451 | |
| 7452 | /* Most of the code in this while loop is lifted from |
| 7453 | * find_get_page. It's been modified to begin searching from a |
| 7454 | * page and return just the first page found in that range. If the |
| 7455 | * found idx is less than or equal to the end idx then we know that |
| 7456 | * a page exists. If no pages are found or if those pages are |
| 7457 | * outside of the range then we're fine (yay!) */ |
| 7458 | while (page == NULL && |
| 7459 | radix_tree_gang_lookup_slot(root, &pagep, NULL, start_idx, 1)) { |
| 7460 | page = radix_tree_deref_slot(pagep); |
| 7461 | if (unlikely(!page)) |
| 7462 | break; |
| 7463 | |
| 7464 | if (radix_tree_exception(page)) { |
Filipe Manana | 809f901 | 2014-06-05 13:22:25 +0100 | [diff] [blame] | 7465 | if (radix_tree_deref_retry(page)) { |
| 7466 | page = NULL; |
Alex Gartrell | fc4adbf | 2014-05-20 13:07:56 -0700 | [diff] [blame] | 7467 | continue; |
Filipe Manana | 809f901 | 2014-06-05 13:22:25 +0100 | [diff] [blame] | 7468 | } |
Alex Gartrell | fc4adbf | 2014-05-20 13:07:56 -0700 | [diff] [blame] | 7469 | /* |
| 7470 | * Otherwise, shmem/tmpfs must be storing a swap entry |
| 7471 | * here as an exceptional entry: so return it without |
| 7472 | * attempting to raise page count. |
| 7473 | */ |
Filipe Manana | 6fdef6d | 2014-06-05 13:22:26 +0100 | [diff] [blame] | 7474 | page = NULL; |
Alex Gartrell | fc4adbf | 2014-05-20 13:07:56 -0700 | [diff] [blame] | 7475 | break; /* TODO: Is this relevant for this use case? */ |
| 7476 | } |
| 7477 | |
Filipe Manana | 9140515 | 2014-06-05 13:22:24 +0100 | [diff] [blame] | 7478 | if (!page_cache_get_speculative(page)) { |
| 7479 | page = NULL; |
Alex Gartrell | fc4adbf | 2014-05-20 13:07:56 -0700 | [diff] [blame] | 7480 | continue; |
Filipe Manana | 9140515 | 2014-06-05 13:22:24 +0100 | [diff] [blame] | 7481 | } |
Alex Gartrell | fc4adbf | 2014-05-20 13:07:56 -0700 | [diff] [blame] | 7482 | |
| 7483 | /* |
| 7484 | * Has the page moved? |
| 7485 | * This is part of the lockless pagecache protocol. See |
| 7486 | * include/linux/pagemap.h for details. |
| 7487 | */ |
| 7488 | if (unlikely(page != *pagep)) { |
Kirill A. Shutemov | 09cbfea | 2016-04-01 15:29:47 +0300 | [diff] [blame] | 7489 | put_page(page); |
Alex Gartrell | fc4adbf | 2014-05-20 13:07:56 -0700 | [diff] [blame] | 7490 | page = NULL; |
| 7491 | } |
| 7492 | } |
| 7493 | |
| 7494 | if (page) { |
| 7495 | if (page->index <= end_idx) |
| 7496 | found = true; |
Kirill A. Shutemov | 09cbfea | 2016-04-01 15:29:47 +0300 | [diff] [blame] | 7497 | put_page(page); |
Alex Gartrell | fc4adbf | 2014-05-20 13:07:56 -0700 | [diff] [blame] | 7498 | } |
| 7499 | |
| 7500 | rcu_read_unlock(); |
| 7501 | return found; |
| 7502 | } |
| 7503 | |
Josef Bacik | eb838e7 | 2012-07-31 16:28:48 -0400 | [diff] [blame] | 7504 | static int lock_extent_direct(struct inode *inode, u64 lockstart, u64 lockend, |
| 7505 | struct extent_state **cached_state, int writing) |
| 7506 | { |
| 7507 | struct btrfs_ordered_extent *ordered; |
| 7508 | int ret = 0; |
| 7509 | |
| 7510 | while (1) { |
| 7511 | lock_extent_bits(&BTRFS_I(inode)->io_tree, lockstart, lockend, |
David Sterba | ff13db4 | 2015-12-03 14:30:40 +0100 | [diff] [blame] | 7512 | cached_state); |
Josef Bacik | eb838e7 | 2012-07-31 16:28:48 -0400 | [diff] [blame] | 7513 | /* |
| 7514 | * 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] | 7515 | * 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] | 7516 | * extents in this range. |
| 7517 | */ |
| 7518 | ordered = btrfs_lookup_ordered_range(inode, lockstart, |
| 7519 | lockend - lockstart + 1); |
| 7520 | |
| 7521 | /* |
| 7522 | * We need to make sure there are no buffered pages in this |
| 7523 | * range either, we could have raced between the invalidate in |
| 7524 | * generic_file_direct_write and locking the extent. The |
| 7525 | * invalidate needs to happen so that reads after a write do not |
| 7526 | * get stale data. |
| 7527 | */ |
Alex Gartrell | fc4adbf | 2014-05-20 13:07:56 -0700 | [diff] [blame] | 7528 | if (!ordered && |
| 7529 | (!writing || |
| 7530 | !btrfs_page_exists_in_range(inode, lockstart, lockend))) |
Josef Bacik | eb838e7 | 2012-07-31 16:28:48 -0400 | [diff] [blame] | 7531 | break; |
| 7532 | |
| 7533 | unlock_extent_cached(&BTRFS_I(inode)->io_tree, lockstart, lockend, |
| 7534 | cached_state, GFP_NOFS); |
| 7535 | |
| 7536 | if (ordered) { |
Filipe Manana | ade7702 | 2016-02-18 14:28:55 +0000 | [diff] [blame] | 7537 | /* |
| 7538 | * If we are doing a DIO read and the ordered extent we |
| 7539 | * found is for a buffered write, we can not wait for it |
| 7540 | * to complete and retry, because if we do so we can |
| 7541 | * deadlock with concurrent buffered writes on page |
| 7542 | * locks. This happens only if our DIO read covers more |
| 7543 | * than one extent map, if at this point has already |
| 7544 | * created an ordered extent for a previous extent map |
| 7545 | * and locked its range in the inode's io tree, and a |
| 7546 | * concurrent write against that previous extent map's |
| 7547 | * range and this range started (we unlock the ranges |
| 7548 | * in the io tree only when the bios complete and |
| 7549 | * buffered writes always lock pages before attempting |
| 7550 | * to lock range in the io tree). |
| 7551 | */ |
| 7552 | if (writing || |
| 7553 | test_bit(BTRFS_ORDERED_DIRECT, &ordered->flags)) |
| 7554 | btrfs_start_ordered_extent(inode, ordered, 1); |
| 7555 | else |
| 7556 | ret = -ENOTBLK; |
Josef Bacik | eb838e7 | 2012-07-31 16:28:48 -0400 | [diff] [blame] | 7557 | btrfs_put_ordered_extent(ordered); |
| 7558 | } else { |
Josef Bacik | eb838e7 | 2012-07-31 16:28:48 -0400 | [diff] [blame] | 7559 | /* |
Filipe Manana | b850ae1 | 2015-12-08 16:23:16 +0000 | [diff] [blame] | 7560 | * We could trigger writeback for this range (and wait |
| 7561 | * for it to complete) and then invalidate the pages for |
| 7562 | * this range (through invalidate_inode_pages2_range()), |
| 7563 | * but that can lead us to a deadlock with a concurrent |
| 7564 | * call to readpages() (a buffered read or a defrag call |
| 7565 | * triggered a readahead) on a page lock due to an |
| 7566 | * ordered dio extent we created before but did not have |
| 7567 | * yet a corresponding bio submitted (whence it can not |
| 7568 | * complete), which makes readpages() wait for that |
| 7569 | * ordered extent to complete while holding a lock on |
| 7570 | * that page. |
Josef Bacik | eb838e7 | 2012-07-31 16:28:48 -0400 | [diff] [blame] | 7571 | */ |
Filipe Manana | b850ae1 | 2015-12-08 16:23:16 +0000 | [diff] [blame] | 7572 | ret = -ENOTBLK; |
Josef Bacik | eb838e7 | 2012-07-31 16:28:48 -0400 | [diff] [blame] | 7573 | } |
| 7574 | |
Filipe Manana | ade7702 | 2016-02-18 14:28:55 +0000 | [diff] [blame] | 7575 | if (ret) |
| 7576 | break; |
| 7577 | |
Josef Bacik | eb838e7 | 2012-07-31 16:28:48 -0400 | [diff] [blame] | 7578 | cond_resched(); |
| 7579 | } |
| 7580 | |
| 7581 | return ret; |
| 7582 | } |
| 7583 | |
Josef Bacik | 69ffb54 | 2012-09-11 15:40:07 -0400 | [diff] [blame] | 7584 | static struct extent_map *create_pinned_em(struct inode *inode, u64 start, |
| 7585 | u64 len, u64 orig_start, |
| 7586 | u64 block_start, u64 block_len, |
Josef Bacik | cc95bef | 2013-04-04 14:31:27 -0400 | [diff] [blame] | 7587 | u64 orig_block_len, u64 ram_bytes, |
| 7588 | int type) |
Josef Bacik | 69ffb54 | 2012-09-11 15:40:07 -0400 | [diff] [blame] | 7589 | { |
| 7590 | struct extent_map_tree *em_tree; |
| 7591 | struct extent_map *em; |
| 7592 | struct btrfs_root *root = BTRFS_I(inode)->root; |
| 7593 | int ret; |
| 7594 | |
| 7595 | em_tree = &BTRFS_I(inode)->extent_tree; |
| 7596 | em = alloc_extent_map(); |
| 7597 | if (!em) |
| 7598 | return ERR_PTR(-ENOMEM); |
| 7599 | |
| 7600 | em->start = start; |
| 7601 | em->orig_start = orig_start; |
Josef Bacik | 2ab28f3 | 2012-10-12 15:27:49 -0400 | [diff] [blame] | 7602 | em->mod_start = start; |
| 7603 | em->mod_len = len; |
Josef Bacik | 69ffb54 | 2012-09-11 15:40:07 -0400 | [diff] [blame] | 7604 | em->len = len; |
| 7605 | em->block_len = block_len; |
| 7606 | em->block_start = block_start; |
| 7607 | em->bdev = root->fs_info->fs_devices->latest_bdev; |
Josef Bacik | b493968 | 2012-12-03 10:31:19 -0500 | [diff] [blame] | 7608 | em->orig_block_len = orig_block_len; |
Josef Bacik | cc95bef | 2013-04-04 14:31:27 -0400 | [diff] [blame] | 7609 | em->ram_bytes = ram_bytes; |
Josef Bacik | 70c8a91 | 2012-10-11 16:54:30 -0400 | [diff] [blame] | 7610 | em->generation = -1; |
Josef Bacik | 69ffb54 | 2012-09-11 15:40:07 -0400 | [diff] [blame] | 7611 | set_bit(EXTENT_FLAG_PINNED, &em->flags); |
| 7612 | if (type == BTRFS_ORDERED_PREALLOC) |
Josef Bacik | b11e234 | 2012-12-03 10:58:15 -0500 | [diff] [blame] | 7613 | set_bit(EXTENT_FLAG_FILLING, &em->flags); |
Josef Bacik | 69ffb54 | 2012-09-11 15:40:07 -0400 | [diff] [blame] | 7614 | |
| 7615 | do { |
| 7616 | btrfs_drop_extent_cache(inode, em->start, |
| 7617 | em->start + em->len - 1, 0); |
| 7618 | write_lock(&em_tree->lock); |
Josef Bacik | 09a2a8f9 | 2013-04-05 16:51:15 -0400 | [diff] [blame] | 7619 | ret = add_extent_mapping(em_tree, em, 1); |
Josef Bacik | 69ffb54 | 2012-09-11 15:40:07 -0400 | [diff] [blame] | 7620 | write_unlock(&em_tree->lock); |
| 7621 | } while (ret == -EEXIST); |
| 7622 | |
| 7623 | if (ret) { |
| 7624 | free_extent_map(em); |
| 7625 | return ERR_PTR(ret); |
| 7626 | } |
| 7627 | |
| 7628 | return em; |
| 7629 | } |
| 7630 | |
Filipe Manana | 9c9464c | 2015-11-04 09:52:04 +0000 | [diff] [blame] | 7631 | static void adjust_dio_outstanding_extents(struct inode *inode, |
| 7632 | struct btrfs_dio_data *dio_data, |
| 7633 | const u64 len) |
| 7634 | { |
| 7635 | unsigned num_extents; |
| 7636 | |
| 7637 | num_extents = (unsigned) div64_u64(len + BTRFS_MAX_EXTENT_SIZE - 1, |
| 7638 | BTRFS_MAX_EXTENT_SIZE); |
| 7639 | /* |
| 7640 | * If we have an outstanding_extents count still set then we're |
| 7641 | * within our reservation, otherwise we need to adjust our inode |
| 7642 | * counter appropriately. |
| 7643 | */ |
| 7644 | if (dio_data->outstanding_extents) { |
| 7645 | dio_data->outstanding_extents -= num_extents; |
| 7646 | } else { |
| 7647 | spin_lock(&BTRFS_I(inode)->lock); |
| 7648 | BTRFS_I(inode)->outstanding_extents += num_extents; |
| 7649 | spin_unlock(&BTRFS_I(inode)->lock); |
| 7650 | } |
| 7651 | } |
| 7652 | |
Josef Bacik | 4b46fce | 2010-05-23 11:00:55 -0400 | [diff] [blame] | 7653 | static int btrfs_get_blocks_direct(struct inode *inode, sector_t iblock, |
| 7654 | struct buffer_head *bh_result, int create) |
| 7655 | { |
| 7656 | struct extent_map *em; |
| 7657 | struct btrfs_root *root = BTRFS_I(inode)->root; |
Josef Bacik | eb838e7 | 2012-07-31 16:28:48 -0400 | [diff] [blame] | 7658 | struct extent_state *cached_state = NULL; |
chandan | 50745b0 | 2015-08-28 21:10:13 +0530 | [diff] [blame] | 7659 | struct btrfs_dio_data *dio_data = NULL; |
Josef Bacik | 4b46fce | 2010-05-23 11:00:55 -0400 | [diff] [blame] | 7660 | u64 start = iblock << inode->i_blkbits; |
Josef Bacik | eb838e7 | 2012-07-31 16:28:48 -0400 | [diff] [blame] | 7661 | u64 lockstart, lockend; |
Josef Bacik | 4b46fce | 2010-05-23 11:00:55 -0400 | [diff] [blame] | 7662 | u64 len = bh_result->b_size; |
Josef Bacik | eb838e7 | 2012-07-31 16:28:48 -0400 | [diff] [blame] | 7663 | int unlock_bits = EXTENT_LOCKED; |
Miao Xie | 0934856 | 2013-02-07 10:12:07 +0000 | [diff] [blame] | 7664 | int ret = 0; |
Josef Bacik | eb838e7 | 2012-07-31 16:28:48 -0400 | [diff] [blame] | 7665 | |
Miao Xie | 172a504 | 2013-02-21 02:48:22 -0700 | [diff] [blame] | 7666 | if (create) |
Josef Bacik | 3266789 | 2015-02-11 15:08:58 -0500 | [diff] [blame] | 7667 | unlock_bits |= EXTENT_DIRTY; |
Miao Xie | 172a504 | 2013-02-21 02:48:22 -0700 | [diff] [blame] | 7668 | else |
Josef Bacik | c329861 | 2012-08-03 16:49:19 -0400 | [diff] [blame] | 7669 | len = min_t(u64, len, root->sectorsize); |
Josef Bacik | eb838e7 | 2012-07-31 16:28:48 -0400 | [diff] [blame] | 7670 | |
Josef Bacik | c329861 | 2012-08-03 16:49:19 -0400 | [diff] [blame] | 7671 | lockstart = start; |
| 7672 | lockend = start + len - 1; |
| 7673 | |
Josef Bacik | e1cbbfa | 2015-03-17 10:52:28 -0400 | [diff] [blame] | 7674 | if (current->journal_info) { |
| 7675 | /* |
| 7676 | * 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] | 7677 | * 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] | 7678 | * confused. |
| 7679 | */ |
chandan | 50745b0 | 2015-08-28 21:10:13 +0530 | [diff] [blame] | 7680 | dio_data = current->journal_info; |
Josef Bacik | e1cbbfa | 2015-03-17 10:52:28 -0400 | [diff] [blame] | 7681 | current->journal_info = NULL; |
| 7682 | } |
| 7683 | |
Josef Bacik | eb838e7 | 2012-07-31 16:28:48 -0400 | [diff] [blame] | 7684 | /* |
| 7685 | * If this errors out it's because we couldn't invalidate pagecache for |
| 7686 | * this range and we need to fallback to buffered. |
| 7687 | */ |
Filipe Manana | 9c9464c | 2015-11-04 09:52:04 +0000 | [diff] [blame] | 7688 | if (lock_extent_direct(inode, lockstart, lockend, &cached_state, |
| 7689 | create)) { |
| 7690 | ret = -ENOTBLK; |
| 7691 | goto err; |
| 7692 | } |
Josef Bacik | eb838e7 | 2012-07-31 16:28:48 -0400 | [diff] [blame] | 7693 | |
Josef Bacik | 4b46fce | 2010-05-23 11:00:55 -0400 | [diff] [blame] | 7694 | em = btrfs_get_extent(inode, NULL, 0, start, len, 0); |
Josef Bacik | eb838e7 | 2012-07-31 16:28:48 -0400 | [diff] [blame] | 7695 | if (IS_ERR(em)) { |
| 7696 | ret = PTR_ERR(em); |
| 7697 | goto unlock_err; |
| 7698 | } |
Josef Bacik | 4b46fce | 2010-05-23 11:00:55 -0400 | [diff] [blame] | 7699 | |
| 7700 | /* |
| 7701 | * Ok for INLINE and COMPRESSED extents we need to fallback on buffered |
| 7702 | * io. INLINE is special, and we could probably kludge it in here, but |
| 7703 | * it's still buffered so for safety lets just fall back to the generic |
| 7704 | * buffered path. |
| 7705 | * |
| 7706 | * For COMPRESSED we _have_ to read the entire extent in so we can |
| 7707 | * decompress it, so there will be buffering required no matter what we |
| 7708 | * do, so go ahead and fallback to buffered. |
| 7709 | * |
Nicholas D Steeves | 0132761 | 2016-05-19 21:18:45 -0400 | [diff] [blame] | 7710 | * 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] | 7711 | * to buffered IO. Don't blame me, this is the price we pay for using |
| 7712 | * the generic code. |
| 7713 | */ |
| 7714 | if (test_bit(EXTENT_FLAG_COMPRESSED, &em->flags) || |
| 7715 | em->block_start == EXTENT_MAP_INLINE) { |
| 7716 | free_extent_map(em); |
Josef Bacik | eb838e7 | 2012-07-31 16:28:48 -0400 | [diff] [blame] | 7717 | ret = -ENOTBLK; |
| 7718 | goto unlock_err; |
Josef Bacik | 4b46fce | 2010-05-23 11:00:55 -0400 | [diff] [blame] | 7719 | } |
| 7720 | |
| 7721 | /* Just a good old fashioned hole, return */ |
| 7722 | if (!create && (em->block_start == EXTENT_MAP_HOLE || |
| 7723 | test_bit(EXTENT_FLAG_PREALLOC, &em->flags))) { |
| 7724 | free_extent_map(em); |
Josef Bacik | eb838e7 | 2012-07-31 16:28:48 -0400 | [diff] [blame] | 7725 | goto unlock_err; |
Josef Bacik | 4b46fce | 2010-05-23 11:00:55 -0400 | [diff] [blame] | 7726 | } |
| 7727 | |
| 7728 | /* |
| 7729 | * We don't allocate a new extent in the following cases |
| 7730 | * |
| 7731 | * 1) The inode is marked as NODATACOW. In this case we'll just use the |
| 7732 | * existing extent. |
| 7733 | * 2) The extent is marked as PREALLOC. We're good to go here and can |
| 7734 | * just use the extent. |
| 7735 | * |
| 7736 | */ |
Chris Mason | 46bfbb5 | 2010-05-26 11:04:10 -0400 | [diff] [blame] | 7737 | if (!create) { |
Josef Bacik | eb838e7 | 2012-07-31 16:28:48 -0400 | [diff] [blame] | 7738 | len = min(len, em->len - (start - em->start)); |
| 7739 | lockstart = start + len; |
| 7740 | goto unlock; |
Chris Mason | 46bfbb5 | 2010-05-26 11:04:10 -0400 | [diff] [blame] | 7741 | } |
Josef Bacik | 4b46fce | 2010-05-23 11:00:55 -0400 | [diff] [blame] | 7742 | |
| 7743 | if (test_bit(EXTENT_FLAG_PREALLOC, &em->flags) || |
| 7744 | ((BTRFS_I(inode)->flags & BTRFS_INODE_NODATACOW) && |
| 7745 | em->block_start != EXTENT_MAP_HOLE)) { |
Josef Bacik | 4b46fce | 2010-05-23 11:00:55 -0400 | [diff] [blame] | 7746 | int type; |
Josef Bacik | eb384b5 | 2013-04-24 16:32:55 -0400 | [diff] [blame] | 7747 | u64 block_start, orig_start, orig_block_len, ram_bytes; |
Josef Bacik | 4b46fce | 2010-05-23 11:00:55 -0400 | [diff] [blame] | 7748 | |
| 7749 | if (test_bit(EXTENT_FLAG_PREALLOC, &em->flags)) |
| 7750 | type = BTRFS_ORDERED_PREALLOC; |
| 7751 | else |
| 7752 | type = BTRFS_ORDERED_NOCOW; |
Chris Mason | 46bfbb5 | 2010-05-26 11:04:10 -0400 | [diff] [blame] | 7753 | len = min(len, em->len - (start - em->start)); |
Josef Bacik | 4b46fce | 2010-05-23 11:00:55 -0400 | [diff] [blame] | 7754 | block_start = em->block_start + (start - em->start); |
Chris Mason | 46bfbb5 | 2010-05-26 11:04:10 -0400 | [diff] [blame] | 7755 | |
Josef Bacik | 0036158 | 2013-08-14 14:02:47 -0400 | [diff] [blame] | 7756 | if (can_nocow_extent(inode, start, &len, &orig_start, |
Filipe Manana | f78c436 | 2016-05-09 13:15:41 +0100 | [diff] [blame] | 7757 | &orig_block_len, &ram_bytes) == 1 && |
| 7758 | btrfs_inc_nocow_writers(root->fs_info, block_start)) { |
Filipe Manana | 5f9a8a5 | 2016-05-12 13:53:36 +0100 | [diff] [blame] | 7759 | struct extent_map *em2; |
Filipe Manana | 0b90191 | 2016-05-09 13:15:27 +0100 | [diff] [blame] | 7760 | |
Filipe Manana | 5f9a8a5 | 2016-05-12 13:53:36 +0100 | [diff] [blame] | 7761 | em2 = btrfs_create_dio_extent(inode, start, len, |
| 7762 | orig_start, block_start, |
| 7763 | len, orig_block_len, |
| 7764 | ram_bytes, type); |
Filipe Manana | f78c436 | 2016-05-09 13:15:41 +0100 | [diff] [blame] | 7765 | btrfs_dec_nocow_writers(root->fs_info, block_start); |
Josef Bacik | 69ffb54 | 2012-09-11 15:40:07 -0400 | [diff] [blame] | 7766 | if (type == BTRFS_ORDERED_PREALLOC) { |
| 7767 | free_extent_map(em); |
Filipe Manana | 5f9a8a5 | 2016-05-12 13:53:36 +0100 | [diff] [blame] | 7768 | em = em2; |
Josef Bacik | 69ffb54 | 2012-09-11 15:40:07 -0400 | [diff] [blame] | 7769 | } |
Filipe Manana | 5f9a8a5 | 2016-05-12 13:53:36 +0100 | [diff] [blame] | 7770 | if (em2 && IS_ERR(em2)) { |
| 7771 | ret = PTR_ERR(em2); |
Josef Bacik | eb838e7 | 2012-07-31 16:28:48 -0400 | [diff] [blame] | 7772 | goto unlock_err; |
Chris Mason | 46bfbb5 | 2010-05-26 11:04:10 -0400 | [diff] [blame] | 7773 | } |
Wang Xiaoguang | 1851309 | 2016-07-25 15:51:40 +0800 | [diff] [blame] | 7774 | /* |
| 7775 | * For inode marked NODATACOW or extent marked PREALLOC, |
| 7776 | * use the existing or preallocated extent, so does not |
| 7777 | * need to adjust btrfs_space_info's bytes_may_use. |
| 7778 | */ |
| 7779 | btrfs_free_reserved_data_space_noquota(inode, |
| 7780 | start, len); |
Chris Mason | 46bfbb5 | 2010-05-26 11:04:10 -0400 | [diff] [blame] | 7781 | goto unlock; |
Josef Bacik | 4b46fce | 2010-05-23 11:00:55 -0400 | [diff] [blame] | 7782 | } |
Josef Bacik | 4b46fce | 2010-05-23 11:00:55 -0400 | [diff] [blame] | 7783 | } |
Josef Bacik | 0036158 | 2013-08-14 14:02:47 -0400 | [diff] [blame] | 7784 | |
Chris Mason | 46bfbb5 | 2010-05-26 11:04:10 -0400 | [diff] [blame] | 7785 | /* |
| 7786 | * this will cow the extent, reset the len in case we changed |
| 7787 | * it above |
| 7788 | */ |
| 7789 | len = bh_result->b_size; |
Josef Bacik | 70c8a91 | 2012-10-11 16:54:30 -0400 | [diff] [blame] | 7790 | free_extent_map(em); |
| 7791 | em = btrfs_new_extent_direct(inode, start, len); |
Josef Bacik | eb838e7 | 2012-07-31 16:28:48 -0400 | [diff] [blame] | 7792 | if (IS_ERR(em)) { |
| 7793 | ret = PTR_ERR(em); |
| 7794 | goto unlock_err; |
| 7795 | } |
Chris Mason | 46bfbb5 | 2010-05-26 11:04:10 -0400 | [diff] [blame] | 7796 | len = min(len, em->len - (start - em->start)); |
| 7797 | unlock: |
Josef Bacik | 4b46fce | 2010-05-23 11:00:55 -0400 | [diff] [blame] | 7798 | bh_result->b_blocknr = (em->block_start + (start - em->start)) >> |
| 7799 | inode->i_blkbits; |
Chris Mason | 46bfbb5 | 2010-05-26 11:04:10 -0400 | [diff] [blame] | 7800 | bh_result->b_size = len; |
Josef Bacik | 4b46fce | 2010-05-23 11:00:55 -0400 | [diff] [blame] | 7801 | bh_result->b_bdev = em->bdev; |
| 7802 | set_buffer_mapped(bh_result); |
Josef Bacik | c3473e8 | 2012-06-19 10:59:00 -0400 | [diff] [blame] | 7803 | if (create) { |
| 7804 | if (!test_bit(EXTENT_FLAG_PREALLOC, &em->flags)) |
| 7805 | set_buffer_new(bh_result); |
| 7806 | |
| 7807 | /* |
| 7808 | * Need to update the i_size under the extent lock so buffered |
| 7809 | * readers will get the updated i_size when we unlock. |
| 7810 | */ |
| 7811 | if (start + len > i_size_read(inode)) |
| 7812 | i_size_write(inode, start + len); |
Miao Xie | 0934856 | 2013-02-07 10:12:07 +0000 | [diff] [blame] | 7813 | |
Filipe Manana | 9c9464c | 2015-11-04 09:52:04 +0000 | [diff] [blame] | 7814 | adjust_dio_outstanding_extents(inode, dio_data, len); |
chandan | 50745b0 | 2015-08-28 21:10:13 +0530 | [diff] [blame] | 7815 | WARN_ON(dio_data->reserve < len); |
| 7816 | dio_data->reserve -= len; |
Filipe Manana | f28a492 | 2015-12-08 19:23:20 +0000 | [diff] [blame] | 7817 | dio_data->unsubmitted_oe_range_end = start + len; |
chandan | 50745b0 | 2015-08-28 21:10:13 +0530 | [diff] [blame] | 7818 | current->journal_info = dio_data; |
Josef Bacik | c3473e8 | 2012-06-19 10:59:00 -0400 | [diff] [blame] | 7819 | } |
Josef Bacik | 4b46fce | 2010-05-23 11:00:55 -0400 | [diff] [blame] | 7820 | |
Josef Bacik | eb838e7 | 2012-07-31 16:28:48 -0400 | [diff] [blame] | 7821 | /* |
| 7822 | * In the case of write we need to clear and unlock the entire range, |
| 7823 | * in the case of read we need to unlock only the end area that we |
| 7824 | * aren't using if there is any left over space. |
| 7825 | */ |
Liu Bo | 24c03fa | 2012-08-22 20:10:38 -0600 | [diff] [blame] | 7826 | if (lockstart < lockend) { |
Miao Xie | 0934856 | 2013-02-07 10:12:07 +0000 | [diff] [blame] | 7827 | clear_extent_bit(&BTRFS_I(inode)->io_tree, lockstart, |
| 7828 | lockend, unlock_bits, 1, 0, |
| 7829 | &cached_state, GFP_NOFS); |
Liu Bo | 24c03fa | 2012-08-22 20:10:38 -0600 | [diff] [blame] | 7830 | } else { |
Josef Bacik | eb838e7 | 2012-07-31 16:28:48 -0400 | [diff] [blame] | 7831 | free_extent_state(cached_state); |
Liu Bo | 24c03fa | 2012-08-22 20:10:38 -0600 | [diff] [blame] | 7832 | } |
Josef Bacik | eb838e7 | 2012-07-31 16:28:48 -0400 | [diff] [blame] | 7833 | |
Josef Bacik | 4b46fce | 2010-05-23 11:00:55 -0400 | [diff] [blame] | 7834 | free_extent_map(em); |
| 7835 | |
| 7836 | return 0; |
Josef Bacik | eb838e7 | 2012-07-31 16:28:48 -0400 | [diff] [blame] | 7837 | |
| 7838 | unlock_err: |
Josef Bacik | eb838e7 | 2012-07-31 16:28:48 -0400 | [diff] [blame] | 7839 | clear_extent_bit(&BTRFS_I(inode)->io_tree, lockstart, lockend, |
| 7840 | unlock_bits, 1, 0, &cached_state, GFP_NOFS); |
Filipe Manana | 9c9464c | 2015-11-04 09:52:04 +0000 | [diff] [blame] | 7841 | err: |
chandan | 50745b0 | 2015-08-28 21:10:13 +0530 | [diff] [blame] | 7842 | if (dio_data) |
| 7843 | current->journal_info = dio_data; |
Filipe Manana | 9c9464c | 2015-11-04 09:52:04 +0000 | [diff] [blame] | 7844 | /* |
| 7845 | * Compensate the delalloc release we do in btrfs_direct_IO() when we |
| 7846 | * write less data then expected, so that we don't underflow our inode's |
| 7847 | * outstanding extents counter. |
| 7848 | */ |
| 7849 | if (create && dio_data) |
| 7850 | adjust_dio_outstanding_extents(inode, dio_data, len); |
| 7851 | |
Josef Bacik | eb838e7 | 2012-07-31 16:28:48 -0400 | [diff] [blame] | 7852 | return ret; |
Josef Bacik | 4b46fce | 2010-05-23 11:00:55 -0400 | [diff] [blame] | 7853 | } |
| 7854 | |
Miao Xie | 8b110e3 | 2014-09-12 18:44:03 +0800 | [diff] [blame] | 7855 | 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] | 7856 | int mirror_num) |
Miao Xie | 8b110e3 | 2014-09-12 18:44:03 +0800 | [diff] [blame] | 7857 | { |
| 7858 | struct btrfs_root *root = BTRFS_I(inode)->root; |
| 7859 | int ret; |
| 7860 | |
Mike Christie | 37226b2 | 2016-06-05 14:31:52 -0500 | [diff] [blame] | 7861 | BUG_ON(bio_op(bio) == REQ_OP_WRITE); |
Miao Xie | 8b110e3 | 2014-09-12 18:44:03 +0800 | [diff] [blame] | 7862 | |
| 7863 | bio_get(bio); |
| 7864 | |
| 7865 | ret = btrfs_bio_wq_end_io(root->fs_info, bio, |
| 7866 | BTRFS_WQ_ENDIO_DIO_REPAIR); |
| 7867 | if (ret) |
| 7868 | goto err; |
| 7869 | |
Mike Christie | 81a75f6 | 2016-06-05 14:31:54 -0500 | [diff] [blame] | 7870 | ret = btrfs_map_bio(root, bio, mirror_num, 0); |
Miao Xie | 8b110e3 | 2014-09-12 18:44:03 +0800 | [diff] [blame] | 7871 | err: |
| 7872 | bio_put(bio); |
| 7873 | return ret; |
| 7874 | } |
| 7875 | |
| 7876 | static int btrfs_check_dio_repairable(struct inode *inode, |
| 7877 | struct bio *failed_bio, |
| 7878 | struct io_failure_record *failrec, |
| 7879 | int failed_mirror) |
| 7880 | { |
Jeff Mahoney | ab8d0fc | 2016-09-20 10:05:02 -0400 | [diff] [blame] | 7881 | struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb); |
Miao Xie | 8b110e3 | 2014-09-12 18:44:03 +0800 | [diff] [blame] | 7882 | int num_copies; |
| 7883 | |
Jeff Mahoney | ab8d0fc | 2016-09-20 10:05:02 -0400 | [diff] [blame] | 7884 | num_copies = btrfs_num_copies(fs_info, failrec->logical, failrec->len); |
Miao Xie | 8b110e3 | 2014-09-12 18:44:03 +0800 | [diff] [blame] | 7885 | if (num_copies == 1) { |
| 7886 | /* |
| 7887 | * we only have a single copy of the data, so don't bother with |
| 7888 | * all the retry and error correction code that follows. no |
| 7889 | * matter what the error is, it is very likely to persist. |
| 7890 | */ |
Jeff Mahoney | ab8d0fc | 2016-09-20 10:05:02 -0400 | [diff] [blame] | 7891 | btrfs_debug(fs_info, |
| 7892 | "Check DIO Repairable: cannot repair, num_copies=%d, next_mirror %d, failed_mirror %d", |
| 7893 | num_copies, failrec->this_mirror, failed_mirror); |
Miao Xie | 8b110e3 | 2014-09-12 18:44:03 +0800 | [diff] [blame] | 7894 | return 0; |
| 7895 | } |
| 7896 | |
| 7897 | failrec->failed_mirror = failed_mirror; |
| 7898 | failrec->this_mirror++; |
| 7899 | if (failrec->this_mirror == failed_mirror) |
| 7900 | failrec->this_mirror++; |
| 7901 | |
| 7902 | if (failrec->this_mirror > num_copies) { |
Jeff Mahoney | ab8d0fc | 2016-09-20 10:05:02 -0400 | [diff] [blame] | 7903 | btrfs_debug(fs_info, |
| 7904 | "Check DIO Repairable: (fail) num_copies=%d, next_mirror %d, failed_mirror %d", |
| 7905 | num_copies, failrec->this_mirror, failed_mirror); |
Miao Xie | 8b110e3 | 2014-09-12 18:44:03 +0800 | [diff] [blame] | 7906 | return 0; |
| 7907 | } |
| 7908 | |
| 7909 | return 1; |
| 7910 | } |
| 7911 | |
| 7912 | static int dio_read_error(struct inode *inode, struct bio *failed_bio, |
Chandan Rajendra | 2dabb32 | 2016-01-21 15:55:55 +0530 | [diff] [blame] | 7913 | struct page *page, unsigned int pgoff, |
| 7914 | u64 start, u64 end, int failed_mirror, |
| 7915 | bio_end_io_t *repair_endio, void *repair_arg) |
Miao Xie | 8b110e3 | 2014-09-12 18:44:03 +0800 | [diff] [blame] | 7916 | { |
| 7917 | struct io_failure_record *failrec; |
| 7918 | struct bio *bio; |
| 7919 | int isector; |
| 7920 | int read_mode; |
| 7921 | int ret; |
| 7922 | |
Mike Christie | 37226b2 | 2016-06-05 14:31:52 -0500 | [diff] [blame] | 7923 | BUG_ON(bio_op(failed_bio) == REQ_OP_WRITE); |
Miao Xie | 8b110e3 | 2014-09-12 18:44:03 +0800 | [diff] [blame] | 7924 | |
| 7925 | ret = btrfs_get_io_failure_record(inode, start, end, &failrec); |
| 7926 | if (ret) |
| 7927 | return ret; |
| 7928 | |
| 7929 | ret = btrfs_check_dio_repairable(inode, failed_bio, failrec, |
| 7930 | failed_mirror); |
| 7931 | if (!ret) { |
| 7932 | free_io_failure(inode, failrec); |
| 7933 | return -EIO; |
| 7934 | } |
| 7935 | |
Chandan Rajendra | 2dabb32 | 2016-01-21 15:55:55 +0530 | [diff] [blame] | 7936 | if ((failed_bio->bi_vcnt > 1) |
| 7937 | || (failed_bio->bi_io_vec->bv_len |
| 7938 | > BTRFS_I(inode)->root->sectorsize)) |
Miao Xie | 8b110e3 | 2014-09-12 18:44:03 +0800 | [diff] [blame] | 7939 | read_mode = READ_SYNC | REQ_FAILFAST_DEV; |
| 7940 | else |
| 7941 | read_mode = READ_SYNC; |
| 7942 | |
| 7943 | isector = start - btrfs_io_bio(failed_bio)->logical; |
| 7944 | isector >>= inode->i_sb->s_blocksize_bits; |
| 7945 | bio = btrfs_create_repair_bio(inode, failed_bio, failrec, page, |
Chandan Rajendra | 2dabb32 | 2016-01-21 15:55:55 +0530 | [diff] [blame] | 7946 | pgoff, isector, repair_endio, repair_arg); |
Miao Xie | 8b110e3 | 2014-09-12 18:44:03 +0800 | [diff] [blame] | 7947 | if (!bio) { |
| 7948 | free_io_failure(inode, failrec); |
| 7949 | return -EIO; |
| 7950 | } |
Mike Christie | 37226b2 | 2016-06-05 14:31:52 -0500 | [diff] [blame] | 7951 | bio_set_op_attrs(bio, REQ_OP_READ, read_mode); |
Miao Xie | 8b110e3 | 2014-09-12 18:44:03 +0800 | [diff] [blame] | 7952 | |
| 7953 | btrfs_debug(BTRFS_I(inode)->root->fs_info, |
| 7954 | "Repair DIO Read Error: submitting new dio read[%#x] to this_mirror=%d, in_validation=%d\n", |
| 7955 | read_mode, failrec->this_mirror, failrec->in_validation); |
| 7956 | |
Mike Christie | 81a75f6 | 2016-06-05 14:31:54 -0500 | [diff] [blame] | 7957 | ret = submit_dio_repair_bio(inode, bio, failrec->this_mirror); |
Miao Xie | 8b110e3 | 2014-09-12 18:44:03 +0800 | [diff] [blame] | 7958 | if (ret) { |
| 7959 | free_io_failure(inode, failrec); |
| 7960 | bio_put(bio); |
| 7961 | } |
| 7962 | |
| 7963 | return ret; |
| 7964 | } |
| 7965 | |
| 7966 | struct btrfs_retry_complete { |
| 7967 | struct completion done; |
| 7968 | struct inode *inode; |
| 7969 | u64 start; |
| 7970 | int uptodate; |
| 7971 | }; |
| 7972 | |
Christoph Hellwig | 4246a0b | 2015-07-20 15:29:37 +0200 | [diff] [blame] | 7973 | static void btrfs_retry_endio_nocsum(struct bio *bio) |
Miao Xie | 8b110e3 | 2014-09-12 18:44:03 +0800 | [diff] [blame] | 7974 | { |
| 7975 | struct btrfs_retry_complete *done = bio->bi_private; |
Chandan Rajendra | 2dabb32 | 2016-01-21 15:55:55 +0530 | [diff] [blame] | 7976 | struct inode *inode; |
Miao Xie | 8b110e3 | 2014-09-12 18:44:03 +0800 | [diff] [blame] | 7977 | struct bio_vec *bvec; |
| 7978 | int i; |
| 7979 | |
Christoph Hellwig | 4246a0b | 2015-07-20 15:29:37 +0200 | [diff] [blame] | 7980 | if (bio->bi_error) |
Miao Xie | 8b110e3 | 2014-09-12 18:44:03 +0800 | [diff] [blame] | 7981 | goto end; |
| 7982 | |
Chandan Rajendra | 2dabb32 | 2016-01-21 15:55:55 +0530 | [diff] [blame] | 7983 | ASSERT(bio->bi_vcnt == 1); |
| 7984 | inode = bio->bi_io_vec->bv_page->mapping->host; |
| 7985 | ASSERT(bio->bi_io_vec->bv_len == BTRFS_I(inode)->root->sectorsize); |
| 7986 | |
Miao Xie | 8b110e3 | 2014-09-12 18:44:03 +0800 | [diff] [blame] | 7987 | done->uptodate = 1; |
| 7988 | bio_for_each_segment_all(bvec, bio, i) |
| 7989 | clean_io_failure(done->inode, done->start, bvec->bv_page, 0); |
| 7990 | end: |
| 7991 | complete(&done->done); |
| 7992 | bio_put(bio); |
| 7993 | } |
| 7994 | |
| 7995 | static int __btrfs_correct_data_nocsum(struct inode *inode, |
| 7996 | struct btrfs_io_bio *io_bio) |
Josef Bacik | 4b46fce | 2010-05-23 11:00:55 -0400 | [diff] [blame] | 7997 | { |
Chandan Rajendra | 2dabb32 | 2016-01-21 15:55:55 +0530 | [diff] [blame] | 7998 | struct btrfs_fs_info *fs_info; |
Kent Overstreet | 2c30c71 | 2013-11-07 12:20:26 -0800 | [diff] [blame] | 7999 | struct bio_vec *bvec; |
Miao Xie | 8b110e3 | 2014-09-12 18:44:03 +0800 | [diff] [blame] | 8000 | struct btrfs_retry_complete done; |
Josef Bacik | 4b46fce | 2010-05-23 11:00:55 -0400 | [diff] [blame] | 8001 | u64 start; |
Chandan Rajendra | 2dabb32 | 2016-01-21 15:55:55 +0530 | [diff] [blame] | 8002 | unsigned int pgoff; |
| 8003 | u32 sectorsize; |
| 8004 | int nr_sectors; |
Kent Overstreet | 2c30c71 | 2013-11-07 12:20:26 -0800 | [diff] [blame] | 8005 | int i; |
Miao Xie | c1dc089 | 2014-09-12 18:43:56 +0800 | [diff] [blame] | 8006 | int ret; |
Miao Xie | dc380ae | 2014-09-12 18:43:55 +0800 | [diff] [blame] | 8007 | |
Chandan Rajendra | 2dabb32 | 2016-01-21 15:55:55 +0530 | [diff] [blame] | 8008 | fs_info = BTRFS_I(inode)->root->fs_info; |
| 8009 | sectorsize = BTRFS_I(inode)->root->sectorsize; |
| 8010 | |
Miao Xie | c1dc089 | 2014-09-12 18:43:56 +0800 | [diff] [blame] | 8011 | start = io_bio->logical; |
Miao Xie | 8b110e3 | 2014-09-12 18:44:03 +0800 | [diff] [blame] | 8012 | done.inode = inode; |
| 8013 | |
| 8014 | bio_for_each_segment_all(bvec, &io_bio->bio, i) { |
Chandan Rajendra | 2dabb32 | 2016-01-21 15:55:55 +0530 | [diff] [blame] | 8015 | nr_sectors = BTRFS_BYTES_TO_BLKS(fs_info, bvec->bv_len); |
| 8016 | pgoff = bvec->bv_offset; |
| 8017 | |
| 8018 | next_block_or_try_again: |
Miao Xie | 8b110e3 | 2014-09-12 18:44:03 +0800 | [diff] [blame] | 8019 | done.uptodate = 0; |
| 8020 | done.start = start; |
| 8021 | init_completion(&done.done); |
| 8022 | |
Chandan Rajendra | 2dabb32 | 2016-01-21 15:55:55 +0530 | [diff] [blame] | 8023 | ret = dio_read_error(inode, &io_bio->bio, bvec->bv_page, |
| 8024 | pgoff, start, start + sectorsize - 1, |
| 8025 | io_bio->mirror_num, |
| 8026 | btrfs_retry_endio_nocsum, &done); |
Miao Xie | 8b110e3 | 2014-09-12 18:44:03 +0800 | [diff] [blame] | 8027 | if (ret) |
| 8028 | return ret; |
| 8029 | |
| 8030 | wait_for_completion(&done.done); |
| 8031 | |
| 8032 | if (!done.uptodate) { |
| 8033 | /* We might have another mirror, so try again */ |
Chandan Rajendra | 2dabb32 | 2016-01-21 15:55:55 +0530 | [diff] [blame] | 8034 | goto next_block_or_try_again; |
Miao Xie | 8b110e3 | 2014-09-12 18:44:03 +0800 | [diff] [blame] | 8035 | } |
| 8036 | |
Chandan Rajendra | 2dabb32 | 2016-01-21 15:55:55 +0530 | [diff] [blame] | 8037 | start += sectorsize; |
| 8038 | |
| 8039 | if (nr_sectors--) { |
| 8040 | pgoff += sectorsize; |
| 8041 | goto next_block_or_try_again; |
| 8042 | } |
Miao Xie | 8b110e3 | 2014-09-12 18:44:03 +0800 | [diff] [blame] | 8043 | } |
| 8044 | |
| 8045 | return 0; |
| 8046 | } |
| 8047 | |
Christoph Hellwig | 4246a0b | 2015-07-20 15:29:37 +0200 | [diff] [blame] | 8048 | static void btrfs_retry_endio(struct bio *bio) |
Miao Xie | 8b110e3 | 2014-09-12 18:44:03 +0800 | [diff] [blame] | 8049 | { |
| 8050 | struct btrfs_retry_complete *done = bio->bi_private; |
| 8051 | struct btrfs_io_bio *io_bio = btrfs_io_bio(bio); |
Chandan Rajendra | 2dabb32 | 2016-01-21 15:55:55 +0530 | [diff] [blame] | 8052 | struct inode *inode; |
Miao Xie | 8b110e3 | 2014-09-12 18:44:03 +0800 | [diff] [blame] | 8053 | struct bio_vec *bvec; |
Chandan Rajendra | 2dabb32 | 2016-01-21 15:55:55 +0530 | [diff] [blame] | 8054 | u64 start; |
Miao Xie | 8b110e3 | 2014-09-12 18:44:03 +0800 | [diff] [blame] | 8055 | int uptodate; |
| 8056 | int ret; |
| 8057 | int i; |
| 8058 | |
Christoph Hellwig | 4246a0b | 2015-07-20 15:29:37 +0200 | [diff] [blame] | 8059 | if (bio->bi_error) |
Miao Xie | 8b110e3 | 2014-09-12 18:44:03 +0800 | [diff] [blame] | 8060 | goto end; |
| 8061 | |
| 8062 | uptodate = 1; |
Chandan Rajendra | 2dabb32 | 2016-01-21 15:55:55 +0530 | [diff] [blame] | 8063 | |
| 8064 | start = done->start; |
| 8065 | |
| 8066 | ASSERT(bio->bi_vcnt == 1); |
| 8067 | inode = bio->bi_io_vec->bv_page->mapping->host; |
| 8068 | ASSERT(bio->bi_io_vec->bv_len == BTRFS_I(inode)->root->sectorsize); |
| 8069 | |
Miao Xie | 8b110e3 | 2014-09-12 18:44:03 +0800 | [diff] [blame] | 8070 | bio_for_each_segment_all(bvec, bio, i) { |
| 8071 | ret = __readpage_endio_check(done->inode, io_bio, i, |
Chandan Rajendra | 2dabb32 | 2016-01-21 15:55:55 +0530 | [diff] [blame] | 8072 | bvec->bv_page, bvec->bv_offset, |
| 8073 | done->start, bvec->bv_len); |
Miao Xie | 8b110e3 | 2014-09-12 18:44:03 +0800 | [diff] [blame] | 8074 | if (!ret) |
| 8075 | clean_io_failure(done->inode, done->start, |
Chandan Rajendra | 2dabb32 | 2016-01-21 15:55:55 +0530 | [diff] [blame] | 8076 | bvec->bv_page, bvec->bv_offset); |
Miao Xie | 8b110e3 | 2014-09-12 18:44:03 +0800 | [diff] [blame] | 8077 | else |
| 8078 | uptodate = 0; |
| 8079 | } |
| 8080 | |
| 8081 | done->uptodate = uptodate; |
| 8082 | end: |
| 8083 | complete(&done->done); |
| 8084 | bio_put(bio); |
| 8085 | } |
| 8086 | |
| 8087 | static int __btrfs_subio_endio_read(struct inode *inode, |
| 8088 | struct btrfs_io_bio *io_bio, int err) |
| 8089 | { |
Chandan Rajendra | 2dabb32 | 2016-01-21 15:55:55 +0530 | [diff] [blame] | 8090 | struct btrfs_fs_info *fs_info; |
Miao Xie | 8b110e3 | 2014-09-12 18:44:03 +0800 | [diff] [blame] | 8091 | struct bio_vec *bvec; |
| 8092 | struct btrfs_retry_complete done; |
| 8093 | u64 start; |
| 8094 | u64 offset = 0; |
Chandan Rajendra | 2dabb32 | 2016-01-21 15:55:55 +0530 | [diff] [blame] | 8095 | u32 sectorsize; |
| 8096 | int nr_sectors; |
| 8097 | unsigned int pgoff; |
| 8098 | int csum_pos; |
Miao Xie | 8b110e3 | 2014-09-12 18:44:03 +0800 | [diff] [blame] | 8099 | int i; |
| 8100 | int ret; |
| 8101 | |
Chandan Rajendra | 2dabb32 | 2016-01-21 15:55:55 +0530 | [diff] [blame] | 8102 | fs_info = BTRFS_I(inode)->root->fs_info; |
| 8103 | sectorsize = BTRFS_I(inode)->root->sectorsize; |
| 8104 | |
Miao Xie | 8b110e3 | 2014-09-12 18:44:03 +0800 | [diff] [blame] | 8105 | err = 0; |
| 8106 | start = io_bio->logical; |
| 8107 | done.inode = inode; |
| 8108 | |
Miao Xie | c1dc089 | 2014-09-12 18:43:56 +0800 | [diff] [blame] | 8109 | bio_for_each_segment_all(bvec, &io_bio->bio, i) { |
Chandan Rajendra | 2dabb32 | 2016-01-21 15:55:55 +0530 | [diff] [blame] | 8110 | nr_sectors = BTRFS_BYTES_TO_BLKS(fs_info, bvec->bv_len); |
| 8111 | |
| 8112 | pgoff = bvec->bv_offset; |
| 8113 | next_block: |
| 8114 | csum_pos = BTRFS_BYTES_TO_BLKS(fs_info, offset); |
| 8115 | ret = __readpage_endio_check(inode, io_bio, csum_pos, |
| 8116 | bvec->bv_page, pgoff, start, |
| 8117 | sectorsize); |
Miao Xie | 8b110e3 | 2014-09-12 18:44:03 +0800 | [diff] [blame] | 8118 | if (likely(!ret)) |
| 8119 | goto next; |
| 8120 | try_again: |
| 8121 | done.uptodate = 0; |
| 8122 | done.start = start; |
| 8123 | init_completion(&done.done); |
| 8124 | |
Chandan Rajendra | 2dabb32 | 2016-01-21 15:55:55 +0530 | [diff] [blame] | 8125 | ret = dio_read_error(inode, &io_bio->bio, bvec->bv_page, |
| 8126 | pgoff, start, start + sectorsize - 1, |
| 8127 | io_bio->mirror_num, |
| 8128 | btrfs_retry_endio, &done); |
Miao Xie | 8b110e3 | 2014-09-12 18:44:03 +0800 | [diff] [blame] | 8129 | if (ret) { |
| 8130 | err = ret; |
| 8131 | goto next; |
| 8132 | } |
| 8133 | |
| 8134 | wait_for_completion(&done.done); |
| 8135 | |
| 8136 | if (!done.uptodate) { |
| 8137 | /* We might have another mirror, so try again */ |
| 8138 | goto try_again; |
| 8139 | } |
| 8140 | next: |
Chandan Rajendra | 2dabb32 | 2016-01-21 15:55:55 +0530 | [diff] [blame] | 8141 | offset += sectorsize; |
| 8142 | start += sectorsize; |
| 8143 | |
| 8144 | ASSERT(nr_sectors); |
| 8145 | |
| 8146 | if (--nr_sectors) { |
| 8147 | pgoff += sectorsize; |
| 8148 | goto next_block; |
| 8149 | } |
Kent Overstreet | 2c30c71 | 2013-11-07 12:20:26 -0800 | [diff] [blame] | 8150 | } |
Miao Xie | c1dc089 | 2014-09-12 18:43:56 +0800 | [diff] [blame] | 8151 | |
| 8152 | return err; |
| 8153 | } |
| 8154 | |
Miao Xie | 8b110e3 | 2014-09-12 18:44:03 +0800 | [diff] [blame] | 8155 | static int btrfs_subio_endio_read(struct inode *inode, |
| 8156 | struct btrfs_io_bio *io_bio, int err) |
| 8157 | { |
| 8158 | bool skip_csum = BTRFS_I(inode)->flags & BTRFS_INODE_NODATASUM; |
| 8159 | |
| 8160 | if (skip_csum) { |
| 8161 | if (unlikely(err)) |
| 8162 | return __btrfs_correct_data_nocsum(inode, io_bio); |
| 8163 | else |
| 8164 | return 0; |
| 8165 | } else { |
| 8166 | return __btrfs_subio_endio_read(inode, io_bio, err); |
| 8167 | } |
| 8168 | } |
| 8169 | |
Christoph Hellwig | 4246a0b | 2015-07-20 15:29:37 +0200 | [diff] [blame] | 8170 | static void btrfs_endio_direct_read(struct bio *bio) |
Miao Xie | c1dc089 | 2014-09-12 18:43:56 +0800 | [diff] [blame] | 8171 | { |
| 8172 | struct btrfs_dio_private *dip = bio->bi_private; |
| 8173 | struct inode *inode = dip->inode; |
| 8174 | struct bio *dio_bio; |
| 8175 | struct btrfs_io_bio *io_bio = btrfs_io_bio(bio); |
Christoph Hellwig | 4246a0b | 2015-07-20 15:29:37 +0200 | [diff] [blame] | 8176 | int err = bio->bi_error; |
Miao Xie | c1dc089 | 2014-09-12 18:43:56 +0800 | [diff] [blame] | 8177 | |
Miao Xie | 8b110e3 | 2014-09-12 18:44:03 +0800 | [diff] [blame] | 8178 | if (dip->flags & BTRFS_DIO_ORIG_BIO_SUBMITTED) |
| 8179 | err = btrfs_subio_endio_read(inode, io_bio, err); |
Miao Xie | c1dc089 | 2014-09-12 18:43:56 +0800 | [diff] [blame] | 8180 | |
Josef Bacik | 4b46fce | 2010-05-23 11:00:55 -0400 | [diff] [blame] | 8181 | unlock_extent(&BTRFS_I(inode)->io_tree, dip->logical_offset, |
Jeff Mahoney | d008237 | 2012-03-01 14:57:19 +0100 | [diff] [blame] | 8182 | dip->logical_offset + dip->bytes - 1); |
Chris Mason | 9be3395 | 2013-05-17 18:30:14 -0400 | [diff] [blame] | 8183 | dio_bio = dip->dio_bio; |
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 | kfree(dip); |
Josef Bacik | c0da7aa | 2011-03-22 11:05:07 -0400 | [diff] [blame] | 8186 | |
Filipe Manana | 1636d1d | 2016-02-15 16:20:26 +0000 | [diff] [blame] | 8187 | dio_bio->bi_error = bio->bi_error; |
Christoph Hellwig | 4246a0b | 2015-07-20 15:29:37 +0200 | [diff] [blame] | 8188 | dio_end_io(dio_bio, bio->bi_error); |
Miao Xie | 23ea8e5 | 2014-09-12 18:43:54 +0800 | [diff] [blame] | 8189 | |
| 8190 | if (io_bio->end_io) |
| 8191 | io_bio->end_io(io_bio, err); |
Chris Mason | 9be3395 | 2013-05-17 18:30:14 -0400 | [diff] [blame] | 8192 | bio_put(bio); |
Josef Bacik | 4b46fce | 2010-05-23 11:00:55 -0400 | [diff] [blame] | 8193 | } |
| 8194 | |
Filipe Manana | 1454377 | 2015-11-24 16:23:54 +0000 | [diff] [blame] | 8195 | static void btrfs_endio_direct_write_update_ordered(struct inode *inode, |
| 8196 | const u64 offset, |
| 8197 | const u64 bytes, |
| 8198 | const int uptodate) |
Josef Bacik | 4b46fce | 2010-05-23 11:00:55 -0400 | [diff] [blame] | 8199 | { |
Josef Bacik | 4b46fce | 2010-05-23 11:00:55 -0400 | [diff] [blame] | 8200 | struct btrfs_root *root = BTRFS_I(inode)->root; |
Josef Bacik | 4b46fce | 2010-05-23 11:00:55 -0400 | [diff] [blame] | 8201 | struct btrfs_ordered_extent *ordered = NULL; |
Filipe Manana | 1454377 | 2015-11-24 16:23:54 +0000 | [diff] [blame] | 8202 | u64 ordered_offset = offset; |
| 8203 | u64 ordered_bytes = bytes; |
Josef Bacik | 4b46fce | 2010-05-23 11:00:55 -0400 | [diff] [blame] | 8204 | int ret; |
| 8205 | |
Chris Mason | 163cf09 | 2010-11-28 19:56:33 -0500 | [diff] [blame] | 8206 | again: |
| 8207 | ret = btrfs_dec_test_first_ordered_pending(inode, &ordered, |
| 8208 | &ordered_offset, |
Christoph Hellwig | 4246a0b | 2015-07-20 15:29:37 +0200 | [diff] [blame] | 8209 | ordered_bytes, |
Filipe Manana | 1454377 | 2015-11-24 16:23:54 +0000 | [diff] [blame] | 8210 | uptodate); |
Josef Bacik | 4b46fce | 2010-05-23 11:00:55 -0400 | [diff] [blame] | 8211 | if (!ret) |
Chris Mason | 163cf09 | 2010-11-28 19:56:33 -0500 | [diff] [blame] | 8212 | goto out_test; |
Josef Bacik | 4b46fce | 2010-05-23 11:00:55 -0400 | [diff] [blame] | 8213 | |
Liu Bo | 9e0af23 | 2014-08-15 23:36:53 +0800 | [diff] [blame] | 8214 | btrfs_init_work(&ordered->work, btrfs_endio_write_helper, |
| 8215 | finish_ordered_fn, NULL, NULL); |
Qu Wenruo | fccb5d8 | 2014-02-28 10:46:10 +0800 | [diff] [blame] | 8216 | btrfs_queue_work(root->fs_info->endio_write_workers, |
| 8217 | &ordered->work); |
Chris Mason | 163cf09 | 2010-11-28 19:56:33 -0500 | [diff] [blame] | 8218 | out_test: |
| 8219 | /* |
| 8220 | * our bio might span multiple ordered extents. If we haven't |
| 8221 | * completed the accounting for the whole dio, go back and try again |
| 8222 | */ |
Filipe Manana | 1454377 | 2015-11-24 16:23:54 +0000 | [diff] [blame] | 8223 | if (ordered_offset < offset + bytes) { |
| 8224 | ordered_bytes = offset + bytes - ordered_offset; |
Josef Bacik | 5fd0204 | 2012-05-02 14:00:54 -0400 | [diff] [blame] | 8225 | ordered = NULL; |
Chris Mason | 163cf09 | 2010-11-28 19:56:33 -0500 | [diff] [blame] | 8226 | goto again; |
| 8227 | } |
Filipe Manana | 1454377 | 2015-11-24 16:23:54 +0000 | [diff] [blame] | 8228 | } |
| 8229 | |
| 8230 | static void btrfs_endio_direct_write(struct bio *bio) |
| 8231 | { |
| 8232 | struct btrfs_dio_private *dip = bio->bi_private; |
| 8233 | struct bio *dio_bio = dip->dio_bio; |
| 8234 | |
| 8235 | btrfs_endio_direct_write_update_ordered(dip->inode, |
| 8236 | dip->logical_offset, |
| 8237 | dip->bytes, |
| 8238 | !bio->bi_error); |
Josef Bacik | 4b46fce | 2010-05-23 11:00:55 -0400 | [diff] [blame] | 8239 | |
Josef Bacik | 4b46fce | 2010-05-23 11:00:55 -0400 | [diff] [blame] | 8240 | kfree(dip); |
Josef Bacik | c0da7aa | 2011-03-22 11:05:07 -0400 | [diff] [blame] | 8241 | |
Filipe Manana | 1636d1d | 2016-02-15 16:20:26 +0000 | [diff] [blame] | 8242 | dio_bio->bi_error = bio->bi_error; |
Christoph Hellwig | 4246a0b | 2015-07-20 15:29:37 +0200 | [diff] [blame] | 8243 | dio_end_io(dio_bio, bio->bi_error); |
Chris Mason | 9be3395 | 2013-05-17 18:30:14 -0400 | [diff] [blame] | 8244 | bio_put(bio); |
Josef Bacik | 4b46fce | 2010-05-23 11:00:55 -0400 | [diff] [blame] | 8245 | } |
| 8246 | |
Mike Christie | 81a75f6 | 2016-06-05 14:31:54 -0500 | [diff] [blame] | 8247 | static int __btrfs_submit_bio_start_direct_io(struct inode *inode, |
Chris Mason | eaf25d9 | 2010-05-25 09:48:28 -0400 | [diff] [blame] | 8248 | struct bio *bio, int mirror_num, |
| 8249 | unsigned long bio_flags, u64 offset) |
| 8250 | { |
| 8251 | int ret; |
| 8252 | struct btrfs_root *root = BTRFS_I(inode)->root; |
| 8253 | ret = btrfs_csum_one_bio(root, inode, bio, offset, 1); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 8254 | BUG_ON(ret); /* -ENOMEM */ |
Chris Mason | eaf25d9 | 2010-05-25 09:48:28 -0400 | [diff] [blame] | 8255 | return 0; |
| 8256 | } |
| 8257 | |
Christoph Hellwig | 4246a0b | 2015-07-20 15:29:37 +0200 | [diff] [blame] | 8258 | static void btrfs_end_dio_bio(struct bio *bio) |
Miao Xie | e65e153 | 2010-11-22 03:04:43 +0000 | [diff] [blame] | 8259 | { |
| 8260 | struct btrfs_dio_private *dip = bio->bi_private; |
Christoph Hellwig | 4246a0b | 2015-07-20 15:29:37 +0200 | [diff] [blame] | 8261 | int err = bio->bi_error; |
Miao Xie | e65e153 | 2010-11-22 03:04:43 +0000 | [diff] [blame] | 8262 | |
Miao Xie | 8b110e3 | 2014-09-12 18:44:03 +0800 | [diff] [blame] | 8263 | if (err) |
| 8264 | btrfs_warn(BTRFS_I(dip->inode)->root->fs_info, |
Mike Christie | 6296b96 | 2016-06-05 14:32:21 -0500 | [diff] [blame] | 8265 | "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] | 8266 | btrfs_ino(dip->inode), bio_op(bio), bio->bi_opf, |
Miao Xie | 8b110e3 | 2014-09-12 18:44:03 +0800 | [diff] [blame] | 8267 | (unsigned long long)bio->bi_iter.bi_sector, |
| 8268 | bio->bi_iter.bi_size, err); |
| 8269 | |
| 8270 | if (dip->subio_endio) |
| 8271 | err = dip->subio_endio(dip->inode, btrfs_io_bio(bio), err); |
Miao Xie | c1dc089 | 2014-09-12 18:43:56 +0800 | [diff] [blame] | 8272 | |
| 8273 | if (err) { |
Miao Xie | e65e153 | 2010-11-22 03:04:43 +0000 | [diff] [blame] | 8274 | dip->errors = 1; |
| 8275 | |
| 8276 | /* |
| 8277 | * before atomic variable goto zero, we must make sure |
| 8278 | * dip->errors is perceived to be set. |
| 8279 | */ |
Peter Zijlstra | 4e857c5 | 2014-03-17 18:06:10 +0100 | [diff] [blame] | 8280 | smp_mb__before_atomic(); |
Miao Xie | e65e153 | 2010-11-22 03:04:43 +0000 | [diff] [blame] | 8281 | } |
| 8282 | |
| 8283 | /* if there are more bios still pending for this dio, just exit */ |
| 8284 | if (!atomic_dec_and_test(&dip->pending_bios)) |
| 8285 | goto out; |
| 8286 | |
Chris Mason | 9be3395 | 2013-05-17 18:30:14 -0400 | [diff] [blame] | 8287 | if (dip->errors) { |
Miao Xie | e65e153 | 2010-11-22 03:04:43 +0000 | [diff] [blame] | 8288 | bio_io_error(dip->orig_bio); |
Chris Mason | 9be3395 | 2013-05-17 18:30:14 -0400 | [diff] [blame] | 8289 | } else { |
Christoph Hellwig | 4246a0b | 2015-07-20 15:29:37 +0200 | [diff] [blame] | 8290 | dip->dio_bio->bi_error = 0; |
| 8291 | bio_endio(dip->orig_bio); |
Miao Xie | e65e153 | 2010-11-22 03:04:43 +0000 | [diff] [blame] | 8292 | } |
| 8293 | out: |
| 8294 | bio_put(bio); |
| 8295 | } |
| 8296 | |
| 8297 | static struct bio *btrfs_dio_bio_alloc(struct block_device *bdev, |
| 8298 | u64 first_sector, gfp_t gfp_flags) |
| 8299 | { |
Chris Mason | da2f0f7 | 2015-07-02 13:57:22 -0700 | [diff] [blame] | 8300 | struct bio *bio; |
Linus Torvalds | 2236597 | 2015-09-05 15:14:43 -0700 | [diff] [blame] | 8301 | bio = btrfs_bio_alloc(bdev, first_sector, BIO_MAX_PAGES, gfp_flags); |
Chris Mason | da2f0f7 | 2015-07-02 13:57:22 -0700 | [diff] [blame] | 8302 | if (bio) |
| 8303 | bio_associate_current(bio); |
| 8304 | return bio; |
Miao Xie | e65e153 | 2010-11-22 03:04:43 +0000 | [diff] [blame] | 8305 | } |
| 8306 | |
Miao Xie | c1dc089 | 2014-09-12 18:43:56 +0800 | [diff] [blame] | 8307 | static inline int btrfs_lookup_and_bind_dio_csum(struct btrfs_root *root, |
| 8308 | struct inode *inode, |
| 8309 | struct btrfs_dio_private *dip, |
| 8310 | struct bio *bio, |
| 8311 | u64 file_offset) |
| 8312 | { |
| 8313 | struct btrfs_io_bio *io_bio = btrfs_io_bio(bio); |
| 8314 | struct btrfs_io_bio *orig_io_bio = btrfs_io_bio(dip->orig_bio); |
| 8315 | int ret; |
| 8316 | |
| 8317 | /* |
| 8318 | * We load all the csum data we need when we submit |
| 8319 | * the first bio to reduce the csum tree search and |
| 8320 | * contention. |
| 8321 | */ |
| 8322 | if (dip->logical_offset == file_offset) { |
| 8323 | ret = btrfs_lookup_bio_sums_dio(root, inode, dip->orig_bio, |
| 8324 | file_offset); |
| 8325 | if (ret) |
| 8326 | return ret; |
| 8327 | } |
| 8328 | |
| 8329 | if (bio == dip->orig_bio) |
| 8330 | return 0; |
| 8331 | |
| 8332 | file_offset -= dip->logical_offset; |
| 8333 | file_offset >>= inode->i_sb->s_blocksize_bits; |
| 8334 | io_bio->csum = (u8 *)(((u32 *)orig_io_bio->csum) + file_offset); |
| 8335 | |
| 8336 | return 0; |
| 8337 | } |
| 8338 | |
Miao Xie | e65e153 | 2010-11-22 03:04:43 +0000 | [diff] [blame] | 8339 | 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] | 8340 | u64 file_offset, int skip_sum, |
Josef Bacik | c329861 | 2012-08-03 16:49:19 -0400 | [diff] [blame] | 8341 | int async_submit) |
Miao Xie | e65e153 | 2010-11-22 03:04:43 +0000 | [diff] [blame] | 8342 | { |
Miao Xie | facc8a22 | 2013-07-25 19:22:34 +0800 | [diff] [blame] | 8343 | struct btrfs_dio_private *dip = bio->bi_private; |
Mike Christie | 37226b2 | 2016-06-05 14:31:52 -0500 | [diff] [blame] | 8344 | bool write = bio_op(bio) == REQ_OP_WRITE; |
Miao Xie | e65e153 | 2010-11-22 03:04:43 +0000 | [diff] [blame] | 8345 | struct btrfs_root *root = BTRFS_I(inode)->root; |
| 8346 | int ret; |
| 8347 | |
Josef Bacik | b812ce2 | 2012-11-16 13:56:32 -0500 | [diff] [blame] | 8348 | if (async_submit) |
| 8349 | async_submit = !atomic_read(&BTRFS_I(inode)->sync_writers); |
| 8350 | |
Miao Xie | e65e153 | 2010-11-22 03:04:43 +0000 | [diff] [blame] | 8351 | bio_get(bio); |
Josef Bacik | 5fd0204 | 2012-05-02 14:00:54 -0400 | [diff] [blame] | 8352 | |
| 8353 | if (!write) { |
David Sterba | bfebd8b | 2014-07-30 00:25:45 +0200 | [diff] [blame] | 8354 | ret = btrfs_bio_wq_end_io(root->fs_info, bio, |
| 8355 | BTRFS_WQ_ENDIO_DATA); |
Josef Bacik | 5fd0204 | 2012-05-02 14:00:54 -0400 | [diff] [blame] | 8356 | if (ret) |
| 8357 | goto err; |
| 8358 | } |
Miao Xie | e65e153 | 2010-11-22 03:04:43 +0000 | [diff] [blame] | 8359 | |
Josef Bacik | 1ae3993 | 2011-04-06 14:41:34 -0400 | [diff] [blame] | 8360 | if (skip_sum) |
| 8361 | goto map; |
| 8362 | |
| 8363 | if (write && async_submit) { |
Miao Xie | e65e153 | 2010-11-22 03:04:43 +0000 | [diff] [blame] | 8364 | ret = btrfs_wq_submit_bio(root->fs_info, |
Mike Christie | 81a75f6 | 2016-06-05 14:31:54 -0500 | [diff] [blame] | 8365 | inode, bio, 0, 0, file_offset, |
Miao Xie | e65e153 | 2010-11-22 03:04:43 +0000 | [diff] [blame] | 8366 | __btrfs_submit_bio_start_direct_io, |
| 8367 | __btrfs_submit_bio_done); |
| 8368 | goto err; |
Josef Bacik | 1ae3993 | 2011-04-06 14:41:34 -0400 | [diff] [blame] | 8369 | } else if (write) { |
| 8370 | /* |
| 8371 | * If we aren't doing async submit, calculate the csum of the |
| 8372 | * bio now. |
| 8373 | */ |
| 8374 | ret = btrfs_csum_one_bio(root, inode, bio, file_offset, 1); |
| 8375 | if (ret) |
| 8376 | goto err; |
Miao Xie | 23ea8e5 | 2014-09-12 18:43:54 +0800 | [diff] [blame] | 8377 | } else { |
Miao Xie | c1dc089 | 2014-09-12 18:43:56 +0800 | [diff] [blame] | 8378 | ret = btrfs_lookup_and_bind_dio_csum(root, inode, dip, bio, |
| 8379 | file_offset); |
Tsutomu Itoh | c2db107 | 2011-03-01 06:48:31 +0000 | [diff] [blame] | 8380 | if (ret) |
| 8381 | goto err; |
| 8382 | } |
Josef Bacik | 1ae3993 | 2011-04-06 14:41:34 -0400 | [diff] [blame] | 8383 | map: |
Mike Christie | 81a75f6 | 2016-06-05 14:31:54 -0500 | [diff] [blame] | 8384 | ret = btrfs_map_bio(root, bio, 0, async_submit); |
Miao Xie | e65e153 | 2010-11-22 03:04:43 +0000 | [diff] [blame] | 8385 | err: |
| 8386 | bio_put(bio); |
| 8387 | return ret; |
| 8388 | } |
| 8389 | |
Mike Christie | 81a75f6 | 2016-06-05 14:31:54 -0500 | [diff] [blame] | 8390 | static int btrfs_submit_direct_hook(struct btrfs_dio_private *dip, |
Miao Xie | e65e153 | 2010-11-22 03:04:43 +0000 | [diff] [blame] | 8391 | int skip_sum) |
| 8392 | { |
| 8393 | struct inode *inode = dip->inode; |
| 8394 | struct btrfs_root *root = BTRFS_I(inode)->root; |
Miao Xie | e65e153 | 2010-11-22 03:04:43 +0000 | [diff] [blame] | 8395 | struct bio *bio; |
| 8396 | struct bio *orig_bio = dip->orig_bio; |
| 8397 | struct bio_vec *bvec = orig_bio->bi_io_vec; |
Kent Overstreet | 4f024f3 | 2013-10-11 15:44:27 -0700 | [diff] [blame] | 8398 | u64 start_sector = orig_bio->bi_iter.bi_sector; |
Miao Xie | e65e153 | 2010-11-22 03:04:43 +0000 | [diff] [blame] | 8399 | u64 file_offset = dip->logical_offset; |
| 8400 | u64 submit_len = 0; |
| 8401 | u64 map_length; |
Chandan Rajendra | 5f4dc8f | 2016-01-21 15:56:00 +0530 | [diff] [blame] | 8402 | u32 blocksize = root->sectorsize; |
Josef Bacik | 1ae3993 | 2011-04-06 14:41:34 -0400 | [diff] [blame] | 8403 | int async_submit = 0; |
Chandan Rajendra | 5f4dc8f | 2016-01-21 15:56:00 +0530 | [diff] [blame] | 8404 | int nr_sectors; |
| 8405 | int ret; |
| 8406 | int i; |
Miao Xie | e65e153 | 2010-11-22 03:04:43 +0000 | [diff] [blame] | 8407 | |
Kent Overstreet | 4f024f3 | 2013-10-11 15:44:27 -0700 | [diff] [blame] | 8408 | map_length = orig_bio->bi_iter.bi_size; |
Mike Christie | 37226b2 | 2016-06-05 14:31:52 -0500 | [diff] [blame] | 8409 | ret = btrfs_map_block(root->fs_info, bio_op(orig_bio), |
| 8410 | start_sector << 9, &map_length, NULL, 0); |
Miao Xie | 7a5c3c9 | 2014-06-17 18:58:59 +0800 | [diff] [blame] | 8411 | if (ret) |
Miao Xie | e65e153 | 2010-11-22 03:04:43 +0000 | [diff] [blame] | 8412 | return -EIO; |
Miao Xie | facc8a22 | 2013-07-25 19:22:34 +0800 | [diff] [blame] | 8413 | |
Kent Overstreet | 4f024f3 | 2013-10-11 15:44:27 -0700 | [diff] [blame] | 8414 | if (map_length >= orig_bio->bi_iter.bi_size) { |
Josef Bacik | 02f57c7 | 2011-04-06 14:25:44 -0400 | [diff] [blame] | 8415 | bio = orig_bio; |
Miao Xie | c1dc089 | 2014-09-12 18:43:56 +0800 | [diff] [blame] | 8416 | dip->flags |= BTRFS_DIO_ORIG_BIO_SUBMITTED; |
Josef Bacik | 02f57c7 | 2011-04-06 14:25:44 -0400 | [diff] [blame] | 8417 | goto submit; |
| 8418 | } |
| 8419 | |
David Woodhouse | 53b381b | 2013-01-29 18:40:14 -0500 | [diff] [blame] | 8420 | /* async crcs make it difficult to collect full stripe writes. */ |
Zhao Lei | ffe2d20 | 2015-01-20 15:11:44 +0800 | [diff] [blame] | 8421 | if (btrfs_get_alloc_profile(root, 1) & BTRFS_BLOCK_GROUP_RAID56_MASK) |
David Woodhouse | 53b381b | 2013-01-29 18:40:14 -0500 | [diff] [blame] | 8422 | async_submit = 0; |
| 8423 | else |
| 8424 | async_submit = 1; |
| 8425 | |
Josef Bacik | 02f57c7 | 2011-04-06 14:25:44 -0400 | [diff] [blame] | 8426 | bio = btrfs_dio_bio_alloc(orig_bio->bi_bdev, start_sector, GFP_NOFS); |
| 8427 | if (!bio) |
| 8428 | return -ENOMEM; |
Miao Xie | 7a5c3c9 | 2014-06-17 18:58:59 +0800 | [diff] [blame] | 8429 | |
Bart Van Assche | 4382e33 | 2016-09-14 10:45:36 +0200 | [diff] [blame] | 8430 | bio_set_op_attrs(bio, bio_op(orig_bio), bio_flags(orig_bio)); |
Josef Bacik | 02f57c7 | 2011-04-06 14:25:44 -0400 | [diff] [blame] | 8431 | bio->bi_private = dip; |
| 8432 | bio->bi_end_io = btrfs_end_dio_bio; |
Miao Xie | c1dc089 | 2014-09-12 18:43:56 +0800 | [diff] [blame] | 8433 | btrfs_io_bio(bio)->logical = file_offset; |
Josef Bacik | 02f57c7 | 2011-04-06 14:25:44 -0400 | [diff] [blame] | 8434 | atomic_inc(&dip->pending_bios); |
| 8435 | |
Miao Xie | e65e153 | 2010-11-22 03:04:43 +0000 | [diff] [blame] | 8436 | while (bvec <= (orig_bio->bi_io_vec + orig_bio->bi_vcnt - 1)) { |
Chandan Rajendra | 5f4dc8f | 2016-01-21 15:56:00 +0530 | [diff] [blame] | 8437 | nr_sectors = BTRFS_BYTES_TO_BLKS(root->fs_info, bvec->bv_len); |
| 8438 | i = 0; |
| 8439 | next_block: |
| 8440 | if (unlikely(map_length < submit_len + blocksize || |
| 8441 | bio_add_page(bio, bvec->bv_page, blocksize, |
| 8442 | bvec->bv_offset + (i * blocksize)) < blocksize)) { |
Miao Xie | e65e153 | 2010-11-22 03:04:43 +0000 | [diff] [blame] | 8443 | /* |
| 8444 | * inc the count before we submit the bio so |
| 8445 | * we know the end IO handler won't happen before |
| 8446 | * we inc the count. Otherwise, the dip might get freed |
| 8447 | * before we're done setting it up |
| 8448 | */ |
| 8449 | atomic_inc(&dip->pending_bios); |
Mike Christie | 81a75f6 | 2016-06-05 14:31:54 -0500 | [diff] [blame] | 8450 | ret = __btrfs_submit_dio_bio(bio, inode, |
Miao Xie | e65e153 | 2010-11-22 03:04:43 +0000 | [diff] [blame] | 8451 | file_offset, skip_sum, |
Josef Bacik | c329861 | 2012-08-03 16:49:19 -0400 | [diff] [blame] | 8452 | async_submit); |
Miao Xie | e65e153 | 2010-11-22 03:04:43 +0000 | [diff] [blame] | 8453 | if (ret) { |
| 8454 | bio_put(bio); |
| 8455 | atomic_dec(&dip->pending_bios); |
| 8456 | goto out_err; |
| 8457 | } |
| 8458 | |
Miao Xie | e65e153 | 2010-11-22 03:04:43 +0000 | [diff] [blame] | 8459 | start_sector += submit_len >> 9; |
| 8460 | file_offset += submit_len; |
| 8461 | |
| 8462 | submit_len = 0; |
Miao Xie | e65e153 | 2010-11-22 03:04:43 +0000 | [diff] [blame] | 8463 | |
| 8464 | bio = btrfs_dio_bio_alloc(orig_bio->bi_bdev, |
| 8465 | start_sector, GFP_NOFS); |
| 8466 | if (!bio) |
| 8467 | goto out_err; |
Bart Van Assche | 4382e33 | 2016-09-14 10:45:36 +0200 | [diff] [blame] | 8468 | bio_set_op_attrs(bio, bio_op(orig_bio), |
| 8469 | bio_flags(orig_bio)); |
Miao Xie | e65e153 | 2010-11-22 03:04:43 +0000 | [diff] [blame] | 8470 | bio->bi_private = dip; |
| 8471 | bio->bi_end_io = btrfs_end_dio_bio; |
Miao Xie | c1dc089 | 2014-09-12 18:43:56 +0800 | [diff] [blame] | 8472 | btrfs_io_bio(bio)->logical = file_offset; |
Miao Xie | e65e153 | 2010-11-22 03:04:43 +0000 | [diff] [blame] | 8473 | |
Kent Overstreet | 4f024f3 | 2013-10-11 15:44:27 -0700 | [diff] [blame] | 8474 | map_length = orig_bio->bi_iter.bi_size; |
Mike Christie | 37226b2 | 2016-06-05 14:31:52 -0500 | [diff] [blame] | 8475 | ret = btrfs_map_block(root->fs_info, bio_op(orig_bio), |
Stefan Behrens | 3ec706c | 2012-11-05 15:46:42 +0100 | [diff] [blame] | 8476 | start_sector << 9, |
Miao Xie | e65e153 | 2010-11-22 03:04:43 +0000 | [diff] [blame] | 8477 | &map_length, NULL, 0); |
| 8478 | if (ret) { |
| 8479 | bio_put(bio); |
| 8480 | goto out_err; |
| 8481 | } |
Chandan Rajendra | 5f4dc8f | 2016-01-21 15:56:00 +0530 | [diff] [blame] | 8482 | |
| 8483 | goto next_block; |
Miao Xie | e65e153 | 2010-11-22 03:04:43 +0000 | [diff] [blame] | 8484 | } else { |
Chandan Rajendra | 5f4dc8f | 2016-01-21 15:56:00 +0530 | [diff] [blame] | 8485 | submit_len += blocksize; |
| 8486 | if (--nr_sectors) { |
| 8487 | i++; |
| 8488 | goto next_block; |
| 8489 | } |
Miao Xie | e65e153 | 2010-11-22 03:04:43 +0000 | [diff] [blame] | 8490 | bvec++; |
| 8491 | } |
| 8492 | } |
| 8493 | |
Josef Bacik | 02f57c7 | 2011-04-06 14:25:44 -0400 | [diff] [blame] | 8494 | submit: |
Mike Christie | 81a75f6 | 2016-06-05 14:31:54 -0500 | [diff] [blame] | 8495 | ret = __btrfs_submit_dio_bio(bio, inode, file_offset, skip_sum, |
Josef Bacik | c329861 | 2012-08-03 16:49:19 -0400 | [diff] [blame] | 8496 | async_submit); |
Miao Xie | e65e153 | 2010-11-22 03:04:43 +0000 | [diff] [blame] | 8497 | if (!ret) |
| 8498 | return 0; |
| 8499 | |
| 8500 | bio_put(bio); |
| 8501 | out_err: |
| 8502 | dip->errors = 1; |
| 8503 | /* |
| 8504 | * before atomic variable goto zero, we must |
| 8505 | * make sure dip->errors is perceived to be set. |
| 8506 | */ |
Peter Zijlstra | 4e857c5 | 2014-03-17 18:06:10 +0100 | [diff] [blame] | 8507 | smp_mb__before_atomic(); |
Miao Xie | e65e153 | 2010-11-22 03:04:43 +0000 | [diff] [blame] | 8508 | if (atomic_dec_and_test(&dip->pending_bios)) |
| 8509 | bio_io_error(dip->orig_bio); |
| 8510 | |
| 8511 | /* bio_end_io() will handle error, so we needn't return it */ |
| 8512 | return 0; |
| 8513 | } |
| 8514 | |
Mike Christie | 8a4c1e4 | 2016-06-05 14:31:50 -0500 | [diff] [blame] | 8515 | static void btrfs_submit_direct(struct bio *dio_bio, struct inode *inode, |
| 8516 | loff_t file_offset) |
Josef Bacik | 4b46fce | 2010-05-23 11:00:55 -0400 | [diff] [blame] | 8517 | { |
Filipe Manana | 61de718 | 2015-07-01 12:13:10 +0100 | [diff] [blame] | 8518 | struct btrfs_dio_private *dip = NULL; |
| 8519 | struct bio *io_bio = NULL; |
Miao Xie | 23ea8e5 | 2014-09-12 18:43:54 +0800 | [diff] [blame] | 8520 | struct btrfs_io_bio *btrfs_bio; |
Josef Bacik | 4b46fce | 2010-05-23 11:00:55 -0400 | [diff] [blame] | 8521 | int skip_sum; |
Mike Christie | 8a4c1e4 | 2016-06-05 14:31:50 -0500 | [diff] [blame] | 8522 | bool write = (bio_op(dio_bio) == REQ_OP_WRITE); |
Josef Bacik | 4b46fce | 2010-05-23 11:00:55 -0400 | [diff] [blame] | 8523 | int ret = 0; |
| 8524 | |
| 8525 | skip_sum = BTRFS_I(inode)->flags & BTRFS_INODE_NODATASUM; |
| 8526 | |
Chris Mason | 9be3395 | 2013-05-17 18:30:14 -0400 | [diff] [blame] | 8527 | io_bio = btrfs_bio_clone(dio_bio, GFP_NOFS); |
Chris Mason | 9be3395 | 2013-05-17 18:30:14 -0400 | [diff] [blame] | 8528 | if (!io_bio) { |
Josef Bacik | 4b46fce | 2010-05-23 11:00:55 -0400 | [diff] [blame] | 8529 | ret = -ENOMEM; |
| 8530 | goto free_ordered; |
| 8531 | } |
Josef Bacik | 4b46fce | 2010-05-23 11:00:55 -0400 | [diff] [blame] | 8532 | |
Miao Xie | c1dc089 | 2014-09-12 18:43:56 +0800 | [diff] [blame] | 8533 | dip = kzalloc(sizeof(*dip), GFP_NOFS); |
Chris Mason | 9be3395 | 2013-05-17 18:30:14 -0400 | [diff] [blame] | 8534 | if (!dip) { |
| 8535 | ret = -ENOMEM; |
Filipe Manana | 61de718 | 2015-07-01 12:13:10 +0100 | [diff] [blame] | 8536 | goto free_ordered; |
Chris Mason | 9be3395 | 2013-05-17 18:30:14 -0400 | [diff] [blame] | 8537 | } |
| 8538 | |
| 8539 | dip->private = dio_bio->bi_private; |
Josef Bacik | 4b46fce | 2010-05-23 11:00:55 -0400 | [diff] [blame] | 8540 | dip->inode = inode; |
| 8541 | dip->logical_offset = file_offset; |
Kent Overstreet | 4f024f3 | 2013-10-11 15:44:27 -0700 | [diff] [blame] | 8542 | dip->bytes = dio_bio->bi_iter.bi_size; |
| 8543 | dip->disk_bytenr = (u64)dio_bio->bi_iter.bi_sector << 9; |
Chris Mason | 9be3395 | 2013-05-17 18:30:14 -0400 | [diff] [blame] | 8544 | io_bio->bi_private = dip; |
Chris Mason | 9be3395 | 2013-05-17 18:30:14 -0400 | [diff] [blame] | 8545 | dip->orig_bio = io_bio; |
| 8546 | dip->dio_bio = dio_bio; |
Miao Xie | e65e153 | 2010-11-22 03:04:43 +0000 | [diff] [blame] | 8547 | atomic_set(&dip->pending_bios, 0); |
Miao Xie | c1dc089 | 2014-09-12 18:43:56 +0800 | [diff] [blame] | 8548 | btrfs_bio = btrfs_io_bio(io_bio); |
| 8549 | btrfs_bio->logical = file_offset; |
Josef Bacik | 4b46fce | 2010-05-23 11:00:55 -0400 | [diff] [blame] | 8550 | |
Miao Xie | c1dc089 | 2014-09-12 18:43:56 +0800 | [diff] [blame] | 8551 | if (write) { |
Chris Mason | 9be3395 | 2013-05-17 18:30:14 -0400 | [diff] [blame] | 8552 | io_bio->bi_end_io = btrfs_endio_direct_write; |
Miao Xie | c1dc089 | 2014-09-12 18:43:56 +0800 | [diff] [blame] | 8553 | } else { |
Chris Mason | 9be3395 | 2013-05-17 18:30:14 -0400 | [diff] [blame] | 8554 | io_bio->bi_end_io = btrfs_endio_direct_read; |
Miao Xie | c1dc089 | 2014-09-12 18:43:56 +0800 | [diff] [blame] | 8555 | dip->subio_endio = btrfs_subio_endio_read; |
| 8556 | } |
Josef Bacik | 4b46fce | 2010-05-23 11:00:55 -0400 | [diff] [blame] | 8557 | |
Filipe Manana | f28a492 | 2015-12-08 19:23:20 +0000 | [diff] [blame] | 8558 | /* |
| 8559 | * Reset the range for unsubmitted ordered extents (to a 0 length range) |
| 8560 | * even if we fail to submit a bio, because in such case we do the |
| 8561 | * corresponding error handling below and it must not be done a second |
| 8562 | * time by btrfs_direct_IO(). |
| 8563 | */ |
| 8564 | if (write) { |
| 8565 | struct btrfs_dio_data *dio_data = current->journal_info; |
| 8566 | |
| 8567 | dio_data->unsubmitted_oe_range_end = dip->logical_offset + |
| 8568 | dip->bytes; |
| 8569 | dio_data->unsubmitted_oe_range_start = |
| 8570 | dio_data->unsubmitted_oe_range_end; |
| 8571 | } |
| 8572 | |
Mike Christie | 81a75f6 | 2016-06-05 14:31:54 -0500 | [diff] [blame] | 8573 | ret = btrfs_submit_direct_hook(dip, skip_sum); |
Miao Xie | e65e153 | 2010-11-22 03:04:43 +0000 | [diff] [blame] | 8574 | if (!ret) |
Chris Mason | eaf25d9 | 2010-05-25 09:48:28 -0400 | [diff] [blame] | 8575 | return; |
Chris Mason | 9be3395 | 2013-05-17 18:30:14 -0400 | [diff] [blame] | 8576 | |
Miao Xie | 23ea8e5 | 2014-09-12 18:43:54 +0800 | [diff] [blame] | 8577 | if (btrfs_bio->end_io) |
| 8578 | btrfs_bio->end_io(btrfs_bio, ret); |
Chris Mason | 9be3395 | 2013-05-17 18:30:14 -0400 | [diff] [blame] | 8579 | |
Josef Bacik | 4b46fce | 2010-05-23 11:00:55 -0400 | [diff] [blame] | 8580 | free_ordered: |
| 8581 | /* |
Filipe Manana | 61de718 | 2015-07-01 12:13:10 +0100 | [diff] [blame] | 8582 | * If we arrived here it means either we failed to submit the dip |
| 8583 | * or we either failed to clone the dio_bio or failed to allocate the |
| 8584 | * dip. If we cloned the dio_bio and allocated the dip, we can just |
| 8585 | * call bio_endio against our io_bio so that we get proper resource |
| 8586 | * cleanup if we fail to submit the dip, otherwise, we must do the |
| 8587 | * same as btrfs_endio_direct_[write|read] because we can't call these |
| 8588 | * callbacks - they require an allocated dip and a clone of dio_bio. |
Josef Bacik | 4b46fce | 2010-05-23 11:00:55 -0400 | [diff] [blame] | 8589 | */ |
Filipe Manana | 61de718 | 2015-07-01 12:13:10 +0100 | [diff] [blame] | 8590 | if (io_bio && dip) { |
Christoph Hellwig | 4246a0b | 2015-07-20 15:29:37 +0200 | [diff] [blame] | 8591 | io_bio->bi_error = -EIO; |
| 8592 | bio_endio(io_bio); |
Filipe Manana | 61de718 | 2015-07-01 12:13:10 +0100 | [diff] [blame] | 8593 | /* |
| 8594 | * The end io callbacks free our dip, do the final put on io_bio |
| 8595 | * and all the cleanup and final put for dio_bio (through |
| 8596 | * dio_end_io()). |
| 8597 | */ |
| 8598 | dip = NULL; |
| 8599 | io_bio = NULL; |
| 8600 | } else { |
Filipe Manana | 1454377 | 2015-11-24 16:23:54 +0000 | [diff] [blame] | 8601 | if (write) |
| 8602 | btrfs_endio_direct_write_update_ordered(inode, |
| 8603 | file_offset, |
| 8604 | dio_bio->bi_iter.bi_size, |
| 8605 | 0); |
| 8606 | else |
Filipe Manana | 61de718 | 2015-07-01 12:13:10 +0100 | [diff] [blame] | 8607 | unlock_extent(&BTRFS_I(inode)->io_tree, file_offset, |
| 8608 | file_offset + dio_bio->bi_iter.bi_size - 1); |
Filipe Manana | 1454377 | 2015-11-24 16:23:54 +0000 | [diff] [blame] | 8609 | |
Christoph Hellwig | 4246a0b | 2015-07-20 15:29:37 +0200 | [diff] [blame] | 8610 | dio_bio->bi_error = -EIO; |
Filipe Manana | 61de718 | 2015-07-01 12:13:10 +0100 | [diff] [blame] | 8611 | /* |
| 8612 | * Releases and cleans up our dio_bio, no need to bio_put() |
| 8613 | * nor bio_endio()/bio_io_error() against dio_bio. |
| 8614 | */ |
| 8615 | dio_end_io(dio_bio, ret); |
Josef Bacik | 4b46fce | 2010-05-23 11:00:55 -0400 | [diff] [blame] | 8616 | } |
Filipe Manana | 61de718 | 2015-07-01 12:13:10 +0100 | [diff] [blame] | 8617 | if (io_bio) |
| 8618 | bio_put(io_bio); |
| 8619 | kfree(dip); |
Josef Bacik | 4b46fce | 2010-05-23 11:00:55 -0400 | [diff] [blame] | 8620 | } |
| 8621 | |
Omar Sandoval | 6f67376 | 2015-03-16 04:33:52 -0700 | [diff] [blame] | 8622 | 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] | 8623 | const struct iov_iter *iter, loff_t offset) |
Chris Mason | 5a5f79b | 2010-05-26 21:33:37 -0400 | [diff] [blame] | 8624 | { |
| 8625 | int seg; |
Josef Bacik | a1b75f7 | 2011-04-08 15:51:18 +0000 | [diff] [blame] | 8626 | int i; |
Chris Mason | 5a5f79b | 2010-05-26 21:33:37 -0400 | [diff] [blame] | 8627 | unsigned blocksize_mask = root->sectorsize - 1; |
| 8628 | ssize_t retval = -EINVAL; |
Chris Mason | 5a5f79b | 2010-05-26 21:33:37 -0400 | [diff] [blame] | 8629 | |
| 8630 | if (offset & blocksize_mask) |
| 8631 | goto out; |
| 8632 | |
Al Viro | 28060d5 | 2014-03-22 05:15:17 -0400 | [diff] [blame] | 8633 | if (iov_iter_alignment(iter) & blocksize_mask) |
| 8634 | goto out; |
Josef Bacik | a1b75f7 | 2011-04-08 15:51:18 +0000 | [diff] [blame] | 8635 | |
Al Viro | 28060d5 | 2014-03-22 05:15:17 -0400 | [diff] [blame] | 8636 | /* If this is a write we don't need to check anymore */ |
Al Viro | cd27e45 | 2016-10-10 13:39:05 -0400 | [diff] [blame] | 8637 | if (iov_iter_rw(iter) != READ || !iter_is_iovec(iter)) |
Al Viro | 28060d5 | 2014-03-22 05:15:17 -0400 | [diff] [blame] | 8638 | return 0; |
| 8639 | /* |
| 8640 | * Check to make sure we don't have duplicate iov_base's in this |
| 8641 | * iovec, if so return EINVAL, otherwise we'll get csum errors |
| 8642 | * when reading back. |
| 8643 | */ |
| 8644 | for (seg = 0; seg < iter->nr_segs; seg++) { |
| 8645 | for (i = seg + 1; i < iter->nr_segs; i++) { |
| 8646 | if (iter->iov[seg].iov_base == iter->iov[i].iov_base) |
Josef Bacik | a1b75f7 | 2011-04-08 15:51:18 +0000 | [diff] [blame] | 8647 | goto out; |
| 8648 | } |
Chris Mason | 5a5f79b | 2010-05-26 21:33:37 -0400 | [diff] [blame] | 8649 | } |
| 8650 | retval = 0; |
| 8651 | out: |
| 8652 | return retval; |
| 8653 | } |
Josef Bacik | eb838e7 | 2012-07-31 16:28:48 -0400 | [diff] [blame] | 8654 | |
Christoph Hellwig | c8b8e32 | 2016-04-07 08:51:58 -0700 | [diff] [blame] | 8655 | 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] | 8656 | { |
Josef Bacik | 4b46fce | 2010-05-23 11:00:55 -0400 | [diff] [blame] | 8657 | struct file *file = iocb->ki_filp; |
| 8658 | struct inode *inode = file->f_mapping->host; |
chandan | 50745b0 | 2015-08-28 21:10:13 +0530 | [diff] [blame] | 8659 | struct btrfs_root *root = BTRFS_I(inode)->root; |
| 8660 | struct btrfs_dio_data dio_data = { 0 }; |
Christoph Hellwig | c8b8e32 | 2016-04-07 08:51:58 -0700 | [diff] [blame] | 8661 | loff_t offset = iocb->ki_pos; |
Miao Xie | 0934856 | 2013-02-07 10:12:07 +0000 | [diff] [blame] | 8662 | size_t count = 0; |
Miao Xie | 2e60a51 | 2013-02-08 07:01:08 +0000 | [diff] [blame] | 8663 | int flags = 0; |
Miao Xie | 38851cc | 2013-02-08 07:04:11 +0000 | [diff] [blame] | 8664 | bool wakeup = true; |
| 8665 | bool relock = false; |
Miao Xie | 0934856 | 2013-02-07 10:12:07 +0000 | [diff] [blame] | 8666 | ssize_t ret; |
Josef Bacik | 4b46fce | 2010-05-23 11:00:55 -0400 | [diff] [blame] | 8667 | |
Omar Sandoval | 6f67376 | 2015-03-16 04:33:52 -0700 | [diff] [blame] | 8668 | if (check_direct_IO(BTRFS_I(inode)->root, iocb, iter, offset)) |
Chris Mason | 5a5f79b | 2010-05-26 21:33:37 -0400 | [diff] [blame] | 8669 | return 0; |
Chris Mason | 5a5f79b | 2010-05-26 21:33:37 -0400 | [diff] [blame] | 8670 | |
Jens Axboe | fe0f07d | 2015-04-15 17:05:48 -0600 | [diff] [blame] | 8671 | inode_dio_begin(inode); |
Peter Zijlstra | 4e857c5 | 2014-03-17 18:06:10 +0100 | [diff] [blame] | 8672 | smp_mb__after_atomic(); |
Miao Xie | 38851cc | 2013-02-08 07:04:11 +0000 | [diff] [blame] | 8673 | |
Josef Bacik | 0e267c4 | 2013-07-02 10:38:02 -0400 | [diff] [blame] | 8674 | /* |
Miao Xie | 41bd9ca | 2014-03-06 13:54:57 +0800 | [diff] [blame] | 8675 | * The generic stuff only does filemap_write_and_wait_range, which |
| 8676 | * isn't enough if we've written compressed pages to this area, so |
| 8677 | * we need to flush the dirty pages again to make absolutely sure |
| 8678 | * that any outstanding dirty pages are on disk. |
Josef Bacik | 0e267c4 | 2013-07-02 10:38:02 -0400 | [diff] [blame] | 8679 | */ |
Al Viro | a6cbcd4 | 2014-03-04 22:38:00 -0500 | [diff] [blame] | 8680 | count = iov_iter_count(iter); |
Miao Xie | 41bd9ca | 2014-03-06 13:54:57 +0800 | [diff] [blame] | 8681 | if (test_bit(BTRFS_INODE_HAS_ASYNC_EXTENT, |
| 8682 | &BTRFS_I(inode)->runtime_flags)) |
Wang Shilong | 9a025a0 | 2014-07-17 11:44:13 +0800 | [diff] [blame] | 8683 | filemap_fdatawrite_range(inode->i_mapping, offset, |
| 8684 | offset + count - 1); |
Josef Bacik | 0e267c4 | 2013-07-02 10:38:02 -0400 | [diff] [blame] | 8685 | |
Omar Sandoval | 6f67376 | 2015-03-16 04:33:52 -0700 | [diff] [blame] | 8686 | if (iov_iter_rw(iter) == WRITE) { |
Miao Xie | 38851cc | 2013-02-08 07:04:11 +0000 | [diff] [blame] | 8687 | /* |
| 8688 | * If the write DIO is beyond the EOF, we need update |
| 8689 | * the isize, but it is protected by i_mutex. So we can |
| 8690 | * not unlock the i_mutex at this case. |
| 8691 | */ |
| 8692 | if (offset + count <= inode->i_size) { |
Al Viro | 5955102 | 2016-01-22 15:40:57 -0500 | [diff] [blame] | 8693 | inode_unlock(inode); |
Miao Xie | 38851cc | 2013-02-08 07:04:11 +0000 | [diff] [blame] | 8694 | relock = true; |
| 8695 | } |
Qu Wenruo | 7cf5b97 | 2015-09-08 17:25:55 +0800 | [diff] [blame] | 8696 | ret = btrfs_delalloc_reserve_space(inode, offset, count); |
Miao Xie | 0934856 | 2013-02-07 10:12:07 +0000 | [diff] [blame] | 8697 | if (ret) |
Miao Xie | 38851cc | 2013-02-08 07:04:11 +0000 | [diff] [blame] | 8698 | goto out; |
chandan | 50745b0 | 2015-08-28 21:10:13 +0530 | [diff] [blame] | 8699 | dio_data.outstanding_extents = div64_u64(count + |
Josef Bacik | e1cbbfa | 2015-03-17 10:52:28 -0400 | [diff] [blame] | 8700 | BTRFS_MAX_EXTENT_SIZE - 1, |
| 8701 | BTRFS_MAX_EXTENT_SIZE); |
| 8702 | |
| 8703 | /* |
| 8704 | * We need to know how many extents we reserved so that we can |
| 8705 | * do the accounting properly if we go over the number we |
| 8706 | * originally calculated. Abuse current->journal_info for this. |
| 8707 | */ |
chandan | 50745b0 | 2015-08-28 21:10:13 +0530 | [diff] [blame] | 8708 | dio_data.reserve = round_up(count, root->sectorsize); |
Filipe Manana | f28a492 | 2015-12-08 19:23:20 +0000 | [diff] [blame] | 8709 | dio_data.unsubmitted_oe_range_start = (u64)offset; |
| 8710 | dio_data.unsubmitted_oe_range_end = (u64)offset; |
chandan | 50745b0 | 2015-08-28 21:10:13 +0530 | [diff] [blame] | 8711 | current->journal_info = &dio_data; |
David Sterba | ee39b43 | 2014-09-30 01:33:33 +0200 | [diff] [blame] | 8712 | } else if (test_bit(BTRFS_INODE_READDIO_NEED_LOCK, |
| 8713 | &BTRFS_I(inode)->runtime_flags)) { |
Jens Axboe | fe0f07d | 2015-04-15 17:05:48 -0600 | [diff] [blame] | 8714 | inode_dio_end(inode); |
Miao Xie | 38851cc | 2013-02-08 07:04:11 +0000 | [diff] [blame] | 8715 | flags = DIO_LOCKING | DIO_SKIP_HOLES; |
| 8716 | wakeup = false; |
Miao Xie | 0934856 | 2013-02-07 10:12:07 +0000 | [diff] [blame] | 8717 | } |
| 8718 | |
Omar Sandoval | 17f8c84 | 2015-03-16 04:33:50 -0700 | [diff] [blame] | 8719 | ret = __blockdev_direct_IO(iocb, inode, |
| 8720 | BTRFS_I(inode)->root->fs_info->fs_devices->latest_bdev, |
Christoph Hellwig | c8b8e32 | 2016-04-07 08:51:58 -0700 | [diff] [blame] | 8721 | iter, btrfs_get_blocks_direct, NULL, |
Omar Sandoval | 17f8c84 | 2015-03-16 04:33:50 -0700 | [diff] [blame] | 8722 | btrfs_submit_direct, flags); |
Omar Sandoval | 6f67376 | 2015-03-16 04:33:52 -0700 | [diff] [blame] | 8723 | if (iov_iter_rw(iter) == WRITE) { |
Josef Bacik | e1cbbfa | 2015-03-17 10:52:28 -0400 | [diff] [blame] | 8724 | current->journal_info = NULL; |
Liu Bo | ddba1bf | 2015-06-17 16:59:58 +0800 | [diff] [blame] | 8725 | if (ret < 0 && ret != -EIOCBQUEUED) { |
chandan | 50745b0 | 2015-08-28 21:10:13 +0530 | [diff] [blame] | 8726 | if (dio_data.reserve) |
Qu Wenruo | 7cf5b97 | 2015-09-08 17:25:55 +0800 | [diff] [blame] | 8727 | btrfs_delalloc_release_space(inode, offset, |
| 8728 | dio_data.reserve); |
Filipe Manana | f28a492 | 2015-12-08 19:23:20 +0000 | [diff] [blame] | 8729 | /* |
| 8730 | * On error we might have left some ordered extents |
| 8731 | * without submitting corresponding bios for them, so |
| 8732 | * cleanup them up to avoid other tasks getting them |
| 8733 | * and waiting for them to complete forever. |
| 8734 | */ |
| 8735 | if (dio_data.unsubmitted_oe_range_start < |
| 8736 | dio_data.unsubmitted_oe_range_end) |
| 8737 | btrfs_endio_direct_write_update_ordered(inode, |
| 8738 | dio_data.unsubmitted_oe_range_start, |
| 8739 | dio_data.unsubmitted_oe_range_end - |
| 8740 | dio_data.unsubmitted_oe_range_start, |
| 8741 | 0); |
Liu Bo | ddba1bf | 2015-06-17 16:59:58 +0800 | [diff] [blame] | 8742 | } else if (ret >= 0 && (size_t)ret < count) |
Qu Wenruo | 7cf5b97 | 2015-09-08 17:25:55 +0800 | [diff] [blame] | 8743 | btrfs_delalloc_release_space(inode, offset, |
| 8744 | count - (size_t)ret); |
Miao Xie | 0934856 | 2013-02-07 10:12:07 +0000 | [diff] [blame] | 8745 | } |
Miao Xie | 38851cc | 2013-02-08 07:04:11 +0000 | [diff] [blame] | 8746 | out: |
Miao Xie | 2e60a51 | 2013-02-08 07:01:08 +0000 | [diff] [blame] | 8747 | if (wakeup) |
Jens Axboe | fe0f07d | 2015-04-15 17:05:48 -0600 | [diff] [blame] | 8748 | inode_dio_end(inode); |
Miao Xie | 38851cc | 2013-02-08 07:04:11 +0000 | [diff] [blame] | 8749 | if (relock) |
Al Viro | 5955102 | 2016-01-22 15:40:57 -0500 | [diff] [blame] | 8750 | inode_lock(inode); |
Miao Xie | 0934856 | 2013-02-07 10:12:07 +0000 | [diff] [blame] | 8751 | |
| 8752 | return ret; |
Chris Mason | 1643298 | 2008-04-10 10:23:21 -0400 | [diff] [blame] | 8753 | } |
| 8754 | |
Tsutomu Itoh | 05dadc0 | 2012-11-29 05:08:26 +0000 | [diff] [blame] | 8755 | #define BTRFS_FIEMAP_FLAGS (FIEMAP_FLAG_SYNC) |
| 8756 | |
Yehuda Sadeh | 1506fcc | 2009-01-21 14:39:14 -0500 | [diff] [blame] | 8757 | static int btrfs_fiemap(struct inode *inode, struct fiemap_extent_info *fieinfo, |
| 8758 | __u64 start, __u64 len) |
| 8759 | { |
Tsutomu Itoh | 05dadc0 | 2012-11-29 05:08:26 +0000 | [diff] [blame] | 8760 | int ret; |
| 8761 | |
| 8762 | ret = fiemap_check_flags(fieinfo, BTRFS_FIEMAP_FLAGS); |
| 8763 | if (ret) |
| 8764 | return ret; |
| 8765 | |
Chris Mason | ec29ed5 | 2011-02-23 16:23:20 -0500 | [diff] [blame] | 8766 | return extent_fiemap(inode, fieinfo, start, len, btrfs_get_extent_fiemap); |
Yehuda Sadeh | 1506fcc | 2009-01-21 14:39:14 -0500 | [diff] [blame] | 8767 | } |
| 8768 | |
Chris Mason | 9ebefb18 | 2007-06-15 13:50:00 -0400 | [diff] [blame] | 8769 | int btrfs_readpage(struct file *file, struct page *page) |
| 8770 | { |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 8771 | struct extent_io_tree *tree; |
| 8772 | tree = &BTRFS_I(page->mapping->host)->io_tree; |
Jan Schmidt | 8ddc7d9 | 2011-06-13 20:02:58 +0200 | [diff] [blame] | 8773 | return extent_read_full_page(tree, page, btrfs_get_extent, 0); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 8774 | } |
Chris Mason | 1832a6d | 2007-12-21 16:27:21 -0500 | [diff] [blame] | 8775 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 8776 | static int btrfs_writepage(struct page *page, struct writeback_control *wbc) |
| 8777 | { |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 8778 | struct extent_io_tree *tree; |
Josef Bacik | be7bd73 | 2015-10-22 15:05:09 -0400 | [diff] [blame] | 8779 | struct inode *inode = page->mapping->host; |
| 8780 | int ret; |
Chris Mason | b888db2 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 8781 | |
| 8782 | if (current->flags & PF_MEMALLOC) { |
| 8783 | redirty_page_for_writepage(wbc, page); |
| 8784 | unlock_page(page); |
| 8785 | return 0; |
| 8786 | } |
Josef Bacik | be7bd73 | 2015-10-22 15:05:09 -0400 | [diff] [blame] | 8787 | |
| 8788 | /* |
| 8789 | * If we are under memory pressure we will call this directly from the |
| 8790 | * VM, we need to make sure we have the inode referenced for the ordered |
| 8791 | * extent. If not just return like we didn't do anything. |
| 8792 | */ |
| 8793 | if (!igrab(inode)) { |
| 8794 | redirty_page_for_writepage(wbc, page); |
| 8795 | return AOP_WRITEPAGE_ACTIVATE; |
| 8796 | } |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 8797 | tree = &BTRFS_I(page->mapping->host)->io_tree; |
Josef Bacik | be7bd73 | 2015-10-22 15:05:09 -0400 | [diff] [blame] | 8798 | ret = extent_write_full_page(tree, page, btrfs_get_extent, wbc); |
| 8799 | btrfs_add_delayed_iput(inode); |
| 8800 | return ret; |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 8801 | } |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 8802 | |
Eric Sandeen | 48a3b63 | 2013-04-25 20:41:01 +0000 | [diff] [blame] | 8803 | static int btrfs_writepages(struct address_space *mapping, |
| 8804 | struct writeback_control *wbc) |
Chris Mason | b293f02 | 2007-11-01 19:45:34 -0400 | [diff] [blame] | 8805 | { |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 8806 | struct extent_io_tree *tree; |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 8807 | |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 8808 | tree = &BTRFS_I(mapping->host)->io_tree; |
Chris Mason | b293f02 | 2007-11-01 19:45:34 -0400 | [diff] [blame] | 8809 | return extent_writepages(tree, mapping, btrfs_get_extent, wbc); |
| 8810 | } |
| 8811 | |
Chris Mason | 3ab2fb5 | 2007-11-08 10:59:22 -0500 | [diff] [blame] | 8812 | static int |
| 8813 | btrfs_readpages(struct file *file, struct address_space *mapping, |
| 8814 | struct list_head *pages, unsigned nr_pages) |
| 8815 | { |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 8816 | struct extent_io_tree *tree; |
| 8817 | tree = &BTRFS_I(mapping->host)->io_tree; |
Chris Mason | 3ab2fb5 | 2007-11-08 10:59:22 -0500 | [diff] [blame] | 8818 | return extent_readpages(tree, mapping, pages, nr_pages, |
| 8819 | btrfs_get_extent); |
| 8820 | } |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 8821 | static int __btrfs_releasepage(struct page *page, gfp_t gfp_flags) |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 8822 | { |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 8823 | struct extent_io_tree *tree; |
| 8824 | struct extent_map_tree *map; |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 8825 | int ret; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 8826 | |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 8827 | tree = &BTRFS_I(page->mapping->host)->io_tree; |
| 8828 | map = &BTRFS_I(page->mapping->host)->extent_tree; |
Chris Mason | 70dec80 | 2008-01-29 09:59:12 -0500 | [diff] [blame] | 8829 | ret = try_release_extent_mapping(map, tree, page, gfp_flags); |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 8830 | if (ret == 1) { |
| 8831 | ClearPagePrivate(page); |
| 8832 | set_page_private(page, 0); |
Kirill A. Shutemov | 09cbfea | 2016-04-01 15:29:47 +0300 | [diff] [blame] | 8833 | put_page(page); |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 8834 | } |
| 8835 | return ret; |
| 8836 | } |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 8837 | |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 8838 | static int btrfs_releasepage(struct page *page, gfp_t gfp_flags) |
| 8839 | { |
Chris Mason | 98509cf | 2008-09-11 15:51:43 -0400 | [diff] [blame] | 8840 | if (PageWriteback(page) || PageDirty(page)) |
| 8841 | return 0; |
Yan Zheng | b335b00 | 2009-02-12 10:06:04 -0500 | [diff] [blame] | 8842 | return __btrfs_releasepage(page, gfp_flags & GFP_NOFS); |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 8843 | } |
| 8844 | |
Lukas Czerner | d47992f | 2013-05-21 23:17:23 -0400 | [diff] [blame] | 8845 | static void btrfs_invalidatepage(struct page *page, unsigned int offset, |
| 8846 | unsigned int length) |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 8847 | { |
Josef Bacik | 5fd0204 | 2012-05-02 14:00:54 -0400 | [diff] [blame] | 8848 | struct inode *inode = page->mapping->host; |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 8849 | struct extent_io_tree *tree; |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 8850 | struct btrfs_ordered_extent *ordered; |
Josef Bacik | 2ac55d4 | 2010-02-03 19:33:23 +0000 | [diff] [blame] | 8851 | struct extent_state *cached_state = NULL; |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 8852 | u64 page_start = page_offset(page); |
Kirill A. Shutemov | 09cbfea | 2016-04-01 15:29:47 +0300 | [diff] [blame] | 8853 | u64 page_end = page_start + PAGE_SIZE - 1; |
Chandan Rajendra | dbfdb6d | 2016-01-21 15:55:58 +0530 | [diff] [blame] | 8854 | u64 start; |
| 8855 | u64 end; |
Filipe David Borba Manana | 131e404 | 2013-11-19 22:29:35 +0000 | [diff] [blame] | 8856 | int inode_evicting = inode->i_state & I_FREEING; |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 8857 | |
Chris Mason | 8b62b72 | 2009-09-02 16:53:46 -0400 | [diff] [blame] | 8858 | /* |
| 8859 | * we have the page locked, so new writeback can't start, |
| 8860 | * and the dirty bit won't be cleared while we are here. |
| 8861 | * |
| 8862 | * Wait for IO on this page so that we can safely clear |
| 8863 | * the PagePrivate2 bit and do ordered accounting |
| 8864 | */ |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 8865 | wait_on_page_writeback(page); |
Chris Mason | 8b62b72 | 2009-09-02 16:53:46 -0400 | [diff] [blame] | 8866 | |
Josef Bacik | 5fd0204 | 2012-05-02 14:00:54 -0400 | [diff] [blame] | 8867 | tree = &BTRFS_I(inode)->io_tree; |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 8868 | if (offset) { |
| 8869 | btrfs_releasepage(page, GFP_NOFS); |
| 8870 | return; |
| 8871 | } |
Filipe David Borba Manana | 131e404 | 2013-11-19 22:29:35 +0000 | [diff] [blame] | 8872 | |
| 8873 | if (!inode_evicting) |
David Sterba | ff13db4 | 2015-12-03 14:30:40 +0100 | [diff] [blame] | 8874 | lock_extent_bits(tree, page_start, page_end, &cached_state); |
Chandan Rajendra | dbfdb6d | 2016-01-21 15:55:58 +0530 | [diff] [blame] | 8875 | again: |
| 8876 | start = page_start; |
| 8877 | ordered = btrfs_lookup_ordered_range(inode, start, |
| 8878 | page_end - start + 1); |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 8879 | if (ordered) { |
Chandan Rajendra | dbfdb6d | 2016-01-21 15:55:58 +0530 | [diff] [blame] | 8880 | end = min(page_end, ordered->file_offset + ordered->len - 1); |
Chris Mason | eb84ae0 | 2008-07-17 13:53:27 -0400 | [diff] [blame] | 8881 | /* |
| 8882 | * IO on this page will never be started, so we need |
| 8883 | * to account for any ordered extents now |
| 8884 | */ |
Filipe David Borba Manana | 131e404 | 2013-11-19 22:29:35 +0000 | [diff] [blame] | 8885 | if (!inode_evicting) |
Chandan Rajendra | dbfdb6d | 2016-01-21 15:55:58 +0530 | [diff] [blame] | 8886 | clear_extent_bit(tree, start, end, |
Filipe David Borba Manana | 131e404 | 2013-11-19 22:29:35 +0000 | [diff] [blame] | 8887 | EXTENT_DIRTY | EXTENT_DELALLOC | |
| 8888 | EXTENT_LOCKED | EXTENT_DO_ACCOUNTING | |
| 8889 | EXTENT_DEFRAG, 1, 0, &cached_state, |
| 8890 | GFP_NOFS); |
Chris Mason | 8b62b72 | 2009-09-02 16:53:46 -0400 | [diff] [blame] | 8891 | /* |
| 8892 | * whoever cleared the private bit is responsible |
| 8893 | * for the finish_ordered_io |
| 8894 | */ |
Josef Bacik | 77cef2e | 2013-08-29 13:57:21 -0400 | [diff] [blame] | 8895 | if (TestClearPagePrivate2(page)) { |
| 8896 | struct btrfs_ordered_inode_tree *tree; |
| 8897 | u64 new_len; |
| 8898 | |
| 8899 | tree = &BTRFS_I(inode)->ordered_tree; |
| 8900 | |
| 8901 | spin_lock_irq(&tree->lock); |
| 8902 | set_bit(BTRFS_ORDERED_TRUNCATED, &ordered->flags); |
Chandan Rajendra | dbfdb6d | 2016-01-21 15:55:58 +0530 | [diff] [blame] | 8903 | new_len = start - ordered->file_offset; |
Josef Bacik | 77cef2e | 2013-08-29 13:57:21 -0400 | [diff] [blame] | 8904 | if (new_len < ordered->truncated_len) |
| 8905 | ordered->truncated_len = new_len; |
| 8906 | spin_unlock_irq(&tree->lock); |
| 8907 | |
| 8908 | if (btrfs_dec_test_ordered_pending(inode, &ordered, |
Chandan Rajendra | dbfdb6d | 2016-01-21 15:55:58 +0530 | [diff] [blame] | 8909 | start, |
| 8910 | end - start + 1, 1)) |
Josef Bacik | 77cef2e | 2013-08-29 13:57:21 -0400 | [diff] [blame] | 8911 | btrfs_finish_ordered_io(ordered); |
Chris Mason | 8b62b72 | 2009-09-02 16:53:46 -0400 | [diff] [blame] | 8912 | } |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 8913 | btrfs_put_ordered_extent(ordered); |
Filipe David Borba Manana | 131e404 | 2013-11-19 22:29:35 +0000 | [diff] [blame] | 8914 | if (!inode_evicting) { |
| 8915 | cached_state = NULL; |
Chandan Rajendra | dbfdb6d | 2016-01-21 15:55:58 +0530 | [diff] [blame] | 8916 | lock_extent_bits(tree, start, end, |
Filipe David Borba Manana | 131e404 | 2013-11-19 22:29:35 +0000 | [diff] [blame] | 8917 | &cached_state); |
| 8918 | } |
Chandan Rajendra | dbfdb6d | 2016-01-21 15:55:58 +0530 | [diff] [blame] | 8919 | |
| 8920 | start = end + 1; |
| 8921 | if (start < page_end) |
| 8922 | goto again; |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 8923 | } |
Filipe David Borba Manana | 131e404 | 2013-11-19 22:29:35 +0000 | [diff] [blame] | 8924 | |
Qu Wenruo | b9d0b38 | 2015-09-29 10:35:16 +0800 | [diff] [blame] | 8925 | /* |
| 8926 | * Qgroup reserved space handler |
| 8927 | * Page here will be either |
| 8928 | * 1) Already written to disk |
| 8929 | * In this case, its reserved space is released from data rsv map |
| 8930 | * and will be freed by delayed_ref handler finally. |
| 8931 | * So even we call qgroup_free_data(), it won't decrease reserved |
| 8932 | * space. |
| 8933 | * 2) Not written to disk |
Goldwyn Rodrigues | 0b34c26 | 2016-09-30 10:40:52 -0500 | [diff] [blame] | 8934 | * This means the reserved space should be freed here. However, |
| 8935 | * if a truncate invalidates the page (by clearing PageDirty) |
| 8936 | * and the page is accounted for while allocating extent |
| 8937 | * in btrfs_check_data_free_space() we let delayed_ref to |
| 8938 | * free the entire extent. |
Qu Wenruo | b9d0b38 | 2015-09-29 10:35:16 +0800 | [diff] [blame] | 8939 | */ |
Goldwyn Rodrigues | 0b34c26 | 2016-09-30 10:40:52 -0500 | [diff] [blame] | 8940 | if (PageDirty(page)) |
| 8941 | btrfs_qgroup_free_data(inode, page_start, PAGE_SIZE); |
Filipe David Borba Manana | 131e404 | 2013-11-19 22:29:35 +0000 | [diff] [blame] | 8942 | if (!inode_evicting) { |
| 8943 | clear_extent_bit(tree, page_start, page_end, |
| 8944 | EXTENT_LOCKED | EXTENT_DIRTY | |
| 8945 | EXTENT_DELALLOC | EXTENT_DO_ACCOUNTING | |
| 8946 | EXTENT_DEFRAG, 1, 1, |
| 8947 | &cached_state, GFP_NOFS); |
| 8948 | |
| 8949 | __btrfs_releasepage(page, GFP_NOFS); |
| 8950 | } |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 8951 | |
Chris Mason | 4a09675 | 2008-07-21 10:29:44 -0400 | [diff] [blame] | 8952 | ClearPageChecked(page); |
Chris Mason | 9ad6b7b | 2008-04-18 16:11:30 -0400 | [diff] [blame] | 8953 | if (PagePrivate(page)) { |
Chris Mason | 9ad6b7b | 2008-04-18 16:11:30 -0400 | [diff] [blame] | 8954 | ClearPagePrivate(page); |
| 8955 | set_page_private(page, 0); |
Kirill A. Shutemov | 09cbfea | 2016-04-01 15:29:47 +0300 | [diff] [blame] | 8956 | put_page(page); |
Chris Mason | 9ad6b7b | 2008-04-18 16:11:30 -0400 | [diff] [blame] | 8957 | } |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 8958 | } |
| 8959 | |
Chris Mason | 9ebefb18 | 2007-06-15 13:50:00 -0400 | [diff] [blame] | 8960 | /* |
| 8961 | * btrfs_page_mkwrite() is not allowed to change the file size as it gets |
| 8962 | * called from a page fault handler when a page is first dirtied. Hence we must |
| 8963 | * be careful to check for EOF conditions here. We set the page up correctly |
| 8964 | * for a written page which means we get ENOSPC checking when writing into |
| 8965 | * holes and correct delalloc and unwritten extent mapping on filesystems that |
| 8966 | * support these features. |
| 8967 | * |
| 8968 | * We are not allowed to take the i_mutex here so we have to play games to |
| 8969 | * protect against truncate races as the page could now be beyond EOF. Because |
| 8970 | * vmtruncate() writes the inode size before removing pages, once we have the |
| 8971 | * page lock we can determine safely if the page is beyond EOF. If it is not |
| 8972 | * beyond EOF, then the page is guaranteed safe against truncation until we |
| 8973 | * unlock the page. |
| 8974 | */ |
Nick Piggin | c2ec175 | 2009-03-31 15:23:21 -0700 | [diff] [blame] | 8975 | 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] | 8976 | { |
Nick Piggin | c2ec175 | 2009-03-31 15:23:21 -0700 | [diff] [blame] | 8977 | struct page *page = vmf->page; |
Al Viro | 496ad9a | 2013-01-23 17:07:38 -0500 | [diff] [blame] | 8978 | struct inode *inode = file_inode(vma->vm_file); |
Chris Mason | 1832a6d | 2007-12-21 16:27:21 -0500 | [diff] [blame] | 8979 | struct btrfs_root *root = BTRFS_I(inode)->root; |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 8980 | struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree; |
| 8981 | struct btrfs_ordered_extent *ordered; |
Josef Bacik | 2ac55d4 | 2010-02-03 19:33:23 +0000 | [diff] [blame] | 8982 | struct extent_state *cached_state = NULL; |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 8983 | char *kaddr; |
| 8984 | unsigned long zero_start; |
Chris Mason | 9ebefb18 | 2007-06-15 13:50:00 -0400 | [diff] [blame] | 8985 | loff_t size; |
Chris Mason | 1832a6d | 2007-12-21 16:27:21 -0500 | [diff] [blame] | 8986 | int ret; |
Chris Mason | 9998eb7 | 2012-01-25 13:47:40 -0500 | [diff] [blame] | 8987 | int reserved = 0; |
Chandan Rajendra | d0b7da88 | 2016-01-21 15:55:57 +0530 | [diff] [blame] | 8988 | u64 reserved_space; |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 8989 | u64 page_start; |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 8990 | u64 page_end; |
Chandan Rajendra | d0b7da88 | 2016-01-21 15:55:57 +0530 | [diff] [blame] | 8991 | u64 end; |
| 8992 | |
Kirill A. Shutemov | 09cbfea | 2016-04-01 15:29:47 +0300 | [diff] [blame] | 8993 | reserved_space = PAGE_SIZE; |
Chris Mason | 9ebefb18 | 2007-06-15 13:50:00 -0400 | [diff] [blame] | 8994 | |
Jan Kara | b2b5ef5 | 2012-06-12 16:20:45 +0200 | [diff] [blame] | 8995 | sb_start_pagefault(inode->i_sb); |
Qu Wenruo | df48063 | 2015-09-08 17:25:54 +0800 | [diff] [blame] | 8996 | page_start = page_offset(page); |
Kirill A. Shutemov | 09cbfea | 2016-04-01 15:29:47 +0300 | [diff] [blame] | 8997 | page_end = page_start + PAGE_SIZE - 1; |
Chandan Rajendra | d0b7da88 | 2016-01-21 15:55:57 +0530 | [diff] [blame] | 8998 | end = page_end; |
Qu Wenruo | df48063 | 2015-09-08 17:25:54 +0800 | [diff] [blame] | 8999 | |
Chandan Rajendra | d0b7da88 | 2016-01-21 15:55:57 +0530 | [diff] [blame] | 9000 | /* |
| 9001 | * Reserving delalloc space after obtaining the page lock can lead to |
| 9002 | * deadlock. For example, if a dirty page is locked by this function |
| 9003 | * and the call to btrfs_delalloc_reserve_space() ends up triggering |
| 9004 | * dirty page write out, then the btrfs_writepage() function could |
| 9005 | * end up waiting indefinitely to get a lock on the page currently |
| 9006 | * being processed by btrfs_page_mkwrite() function. |
| 9007 | */ |
Qu Wenruo | 7cf5b97 | 2015-09-08 17:25:55 +0800 | [diff] [blame] | 9008 | ret = btrfs_delalloc_reserve_space(inode, page_start, |
Chandan Rajendra | d0b7da88 | 2016-01-21 15:55:57 +0530 | [diff] [blame] | 9009 | reserved_space); |
Chris Mason | 9998eb7 | 2012-01-25 13:47:40 -0500 | [diff] [blame] | 9010 | if (!ret) { |
Josef Bacik | e41f941 | 2012-03-26 09:46:47 -0400 | [diff] [blame] | 9011 | ret = file_update_time(vma->vm_file); |
Chris Mason | 9998eb7 | 2012-01-25 13:47:40 -0500 | [diff] [blame] | 9012 | reserved = 1; |
| 9013 | } |
Nick Piggin | 56a76f8 | 2009-03-31 15:23:23 -0700 | [diff] [blame] | 9014 | if (ret) { |
| 9015 | if (ret == -ENOMEM) |
| 9016 | ret = VM_FAULT_OOM; |
| 9017 | else /* -ENOSPC, -EIO, etc */ |
| 9018 | ret = VM_FAULT_SIGBUS; |
Chris Mason | 9998eb7 | 2012-01-25 13:47:40 -0500 | [diff] [blame] | 9019 | if (reserved) |
| 9020 | goto out; |
| 9021 | goto out_noreserve; |
Nick Piggin | 56a76f8 | 2009-03-31 15:23:23 -0700 | [diff] [blame] | 9022 | } |
Chris Mason | 1832a6d | 2007-12-21 16:27:21 -0500 | [diff] [blame] | 9023 | |
Nick Piggin | 56a76f8 | 2009-03-31 15:23:23 -0700 | [diff] [blame] | 9024 | ret = VM_FAULT_NOPAGE; /* make the VM retry the fault */ |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 9025 | again: |
Chris Mason | 9ebefb18 | 2007-06-15 13:50:00 -0400 | [diff] [blame] | 9026 | lock_page(page); |
Chris Mason | 9ebefb18 | 2007-06-15 13:50:00 -0400 | [diff] [blame] | 9027 | size = i_size_read(inode); |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 9028 | |
Chris Mason | 9ebefb18 | 2007-06-15 13:50:00 -0400 | [diff] [blame] | 9029 | if ((page->mapping != inode->i_mapping) || |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 9030 | (page_start >= size)) { |
Chris Mason | 9ebefb18 | 2007-06-15 13:50:00 -0400 | [diff] [blame] | 9031 | /* page got truncated out from underneath us */ |
| 9032 | goto out_unlock; |
| 9033 | } |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 9034 | wait_on_page_writeback(page); |
| 9035 | |
David Sterba | ff13db4 | 2015-12-03 14:30:40 +0100 | [diff] [blame] | 9036 | lock_extent_bits(io_tree, page_start, page_end, &cached_state); |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 9037 | set_page_extent_mapped(page); |
| 9038 | |
Chris Mason | eb84ae0 | 2008-07-17 13:53:27 -0400 | [diff] [blame] | 9039 | /* |
| 9040 | * we can't set the delalloc bits if there are pending ordered |
| 9041 | * extents. Drop our locks and wait for them to finish |
| 9042 | */ |
Chandan Rajendra | d0b7da88 | 2016-01-21 15:55:57 +0530 | [diff] [blame] | 9043 | ordered = btrfs_lookup_ordered_range(inode, page_start, page_end); |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 9044 | if (ordered) { |
Josef Bacik | 2ac55d4 | 2010-02-03 19:33:23 +0000 | [diff] [blame] | 9045 | unlock_extent_cached(io_tree, page_start, page_end, |
| 9046 | &cached_state, GFP_NOFS); |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 9047 | unlock_page(page); |
Chris Mason | eb84ae0 | 2008-07-17 13:53:27 -0400 | [diff] [blame] | 9048 | btrfs_start_ordered_extent(inode, ordered, 1); |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 9049 | btrfs_put_ordered_extent(ordered); |
| 9050 | goto again; |
| 9051 | } |
| 9052 | |
Kirill A. Shutemov | 09cbfea | 2016-04-01 15:29:47 +0300 | [diff] [blame] | 9053 | if (page->index == ((size - 1) >> PAGE_SHIFT)) { |
Chandan Rajendra | d0b7da88 | 2016-01-21 15:55:57 +0530 | [diff] [blame] | 9054 | reserved_space = round_up(size - page_start, root->sectorsize); |
Kirill A. Shutemov | 09cbfea | 2016-04-01 15:29:47 +0300 | [diff] [blame] | 9055 | if (reserved_space < PAGE_SIZE) { |
Chandan Rajendra | d0b7da88 | 2016-01-21 15:55:57 +0530 | [diff] [blame] | 9056 | end = page_start + reserved_space - 1; |
| 9057 | spin_lock(&BTRFS_I(inode)->lock); |
| 9058 | BTRFS_I(inode)->outstanding_extents++; |
| 9059 | spin_unlock(&BTRFS_I(inode)->lock); |
| 9060 | btrfs_delalloc_release_space(inode, page_start, |
Kirill A. Shutemov | 09cbfea | 2016-04-01 15:29:47 +0300 | [diff] [blame] | 9061 | PAGE_SIZE - reserved_space); |
Chandan Rajendra | d0b7da88 | 2016-01-21 15:55:57 +0530 | [diff] [blame] | 9062 | } |
| 9063 | } |
| 9064 | |
Josef Bacik | fbf1908 | 2009-10-01 17:10:23 -0400 | [diff] [blame] | 9065 | /* |
| 9066 | * XXX - page_mkwrite gets called every time the page is dirtied, even |
| 9067 | * if it was already dirty, so for space accounting reasons we need to |
| 9068 | * clear any delalloc bits for the range we are fixing to save. There |
| 9069 | * is probably a better way to do this, but for now keep consistent with |
| 9070 | * prepare_pages in the normal write path. |
| 9071 | */ |
Chandan Rajendra | d0b7da88 | 2016-01-21 15:55:57 +0530 | [diff] [blame] | 9072 | clear_extent_bit(&BTRFS_I(inode)->io_tree, page_start, end, |
Liu Bo | 9e8a4a8 | 2012-09-05 19:10:51 -0600 | [diff] [blame] | 9073 | EXTENT_DIRTY | EXTENT_DELALLOC | |
| 9074 | EXTENT_DO_ACCOUNTING | EXTENT_DEFRAG, |
Josef Bacik | 2ac55d4 | 2010-02-03 19:33:23 +0000 | [diff] [blame] | 9075 | 0, 0, &cached_state, GFP_NOFS); |
Josef Bacik | fbf1908 | 2009-10-01 17:10:23 -0400 | [diff] [blame] | 9076 | |
Chandan Rajendra | d0b7da88 | 2016-01-21 15:55:57 +0530 | [diff] [blame] | 9077 | ret = btrfs_set_extent_delalloc(inode, page_start, end, |
Qu Wenruo | ba8b04c | 2016-07-19 16:50:36 +0800 | [diff] [blame] | 9078 | &cached_state, 0); |
Josef Bacik | 9ed74f2 | 2009-09-11 16:12:44 -0400 | [diff] [blame] | 9079 | if (ret) { |
Josef Bacik | 2ac55d4 | 2010-02-03 19:33:23 +0000 | [diff] [blame] | 9080 | unlock_extent_cached(io_tree, page_start, page_end, |
| 9081 | &cached_state, GFP_NOFS); |
Josef Bacik | 9ed74f2 | 2009-09-11 16:12:44 -0400 | [diff] [blame] | 9082 | ret = VM_FAULT_SIGBUS; |
| 9083 | goto out_unlock; |
| 9084 | } |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 9085 | ret = 0; |
Chris Mason | 9ebefb18 | 2007-06-15 13:50:00 -0400 | [diff] [blame] | 9086 | |
| 9087 | /* page is wholly or partially inside EOF */ |
Kirill A. Shutemov | 09cbfea | 2016-04-01 15:29:47 +0300 | [diff] [blame] | 9088 | if (page_start + PAGE_SIZE > size) |
| 9089 | zero_start = size & ~PAGE_MASK; |
Chris Mason | 9ebefb18 | 2007-06-15 13:50:00 -0400 | [diff] [blame] | 9090 | else |
Kirill A. Shutemov | 09cbfea | 2016-04-01 15:29:47 +0300 | [diff] [blame] | 9091 | zero_start = PAGE_SIZE; |
Chris Mason | 9ebefb18 | 2007-06-15 13:50:00 -0400 | [diff] [blame] | 9092 | |
Kirill A. Shutemov | 09cbfea | 2016-04-01 15:29:47 +0300 | [diff] [blame] | 9093 | if (zero_start != PAGE_SIZE) { |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 9094 | kaddr = kmap(page); |
Kirill A. Shutemov | 09cbfea | 2016-04-01 15:29:47 +0300 | [diff] [blame] | 9095 | memset(kaddr + zero_start, 0, PAGE_SIZE - zero_start); |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 9096 | flush_dcache_page(page); |
| 9097 | kunmap(page); |
| 9098 | } |
Chris Mason | 247e743 | 2008-07-17 12:53:51 -0400 | [diff] [blame] | 9099 | ClearPageChecked(page); |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 9100 | set_page_dirty(page); |
Chris Mason | 50a9b21 | 2009-09-11 12:33:12 -0400 | [diff] [blame] | 9101 | SetPageUptodate(page); |
Chris Mason | 5a3f23d | 2009-03-31 13:27:11 -0400 | [diff] [blame] | 9102 | |
Chris Mason | 257c62e | 2009-10-13 13:21:08 -0400 | [diff] [blame] | 9103 | BTRFS_I(inode)->last_trans = root->fs_info->generation; |
| 9104 | BTRFS_I(inode)->last_sub_trans = BTRFS_I(inode)->root->log_transid; |
Liu Bo | 46d8bc3 | 2012-08-29 01:07:55 -0600 | [diff] [blame] | 9105 | 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] | 9106 | |
Josef Bacik | 2ac55d4 | 2010-02-03 19:33:23 +0000 | [diff] [blame] | 9107 | 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] | 9108 | |
| 9109 | out_unlock: |
Jan Kara | b2b5ef5 | 2012-06-12 16:20:45 +0200 | [diff] [blame] | 9110 | if (!ret) { |
| 9111 | sb_end_pagefault(inode->i_sb); |
Chris Mason | 50a9b21 | 2009-09-11 12:33:12 -0400 | [diff] [blame] | 9112 | return VM_FAULT_LOCKED; |
Jan Kara | b2b5ef5 | 2012-06-12 16:20:45 +0200 | [diff] [blame] | 9113 | } |
Chris Mason | 9ebefb18 | 2007-06-15 13:50:00 -0400 | [diff] [blame] | 9114 | unlock_page(page); |
Chris Mason | 1832a6d | 2007-12-21 16:27:21 -0500 | [diff] [blame] | 9115 | out: |
Chandan Rajendra | d0b7da88 | 2016-01-21 15:55:57 +0530 | [diff] [blame] | 9116 | btrfs_delalloc_release_space(inode, page_start, reserved_space); |
Chris Mason | 9998eb7 | 2012-01-25 13:47:40 -0500 | [diff] [blame] | 9117 | out_noreserve: |
Jan Kara | b2b5ef5 | 2012-06-12 16:20:45 +0200 | [diff] [blame] | 9118 | sb_end_pagefault(inode->i_sb); |
Chris Mason | 9ebefb18 | 2007-06-15 13:50:00 -0400 | [diff] [blame] | 9119 | return ret; |
| 9120 | } |
| 9121 | |
Josef Bacik | a41ad39 | 2011-01-31 15:30:16 -0500 | [diff] [blame] | 9122 | static int btrfs_truncate(struct inode *inode) |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 9123 | { |
| 9124 | struct btrfs_root *root = BTRFS_I(inode)->root; |
Josef Bacik | fcb80c2 | 2011-05-03 10:40:22 -0400 | [diff] [blame] | 9125 | struct btrfs_block_rsv *rsv; |
Josef Bacik | a71754f | 2013-06-17 17:14:39 -0400 | [diff] [blame] | 9126 | int ret = 0; |
Josef Bacik | 3893e33 | 2011-01-31 16:03:11 -0500 | [diff] [blame] | 9127 | int err = 0; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 9128 | struct btrfs_trans_handle *trans; |
Chris Mason | dbe674a | 2008-07-17 12:54:05 -0400 | [diff] [blame] | 9129 | u64 mask = root->sectorsize - 1; |
Josef Bacik | 0712718 | 2011-08-19 10:29:59 -0400 | [diff] [blame] | 9130 | u64 min_size = btrfs_calc_trunc_metadata_size(root, 1); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 9131 | |
Josef Bacik | 0ef8b72 | 2013-10-25 16:13:35 -0400 | [diff] [blame] | 9132 | ret = btrfs_wait_ordered_range(inode, inode->i_size & (~mask), |
| 9133 | (u64)-1); |
| 9134 | if (ret) |
| 9135 | return ret; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 9136 | |
Josef Bacik | fcb80c2 | 2011-05-03 10:40:22 -0400 | [diff] [blame] | 9137 | /* |
Nicholas D Steeves | 0132761 | 2016-05-19 21:18:45 -0400 | [diff] [blame] | 9138 | * 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] | 9139 | * 3 things going on here |
| 9140 | * |
| 9141 | * 1) We need to reserve space for our orphan item and the space to |
| 9142 | * delete our orphan item. Lord knows we don't want to have a dangling |
| 9143 | * orphan item because we didn't reserve space to remove it. |
| 9144 | * |
| 9145 | * 2) We need to reserve space to update our inode. |
| 9146 | * |
| 9147 | * 3) We need to have something to cache all the space that is going to |
| 9148 | * be free'd up by the truncate operation, but also have some slack |
| 9149 | * space reserved in case it uses space during the truncate (thank you |
| 9150 | * very much snapshotting). |
| 9151 | * |
Nicholas D Steeves | 0132761 | 2016-05-19 21:18:45 -0400 | [diff] [blame] | 9152 | * 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] | 9153 | * 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] | 9154 | * 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] | 9155 | * doesn't end up using space reserved for updating the inode or |
| 9156 | * removing the orphan item. We also need to be able to stop the |
| 9157 | * transaction and start a new one, which means we need to be able to |
| 9158 | * update the inode several times, and we have no idea of knowing how |
| 9159 | * 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] | 9160 | * 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] | 9161 | * Then there is the orphan item, which does indeed need to be held on |
| 9162 | * to for the whole operation, and we need nobody to touch this reserved |
| 9163 | * space except the orphan code. |
| 9164 | * |
| 9165 | * So that leaves us with |
| 9166 | * |
| 9167 | * 1) root->orphan_block_rsv - for the orphan deletion. |
| 9168 | * 2) rsv - for the truncate reservation, which we will steal from the |
| 9169 | * transaction reservation. |
| 9170 | * 3) fs_info->trans_block_rsv - this will have 1 items worth left for |
| 9171 | * updating the inode. |
| 9172 | */ |
Miao Xie | 66d8f3d | 2012-09-06 04:02:28 -0600 | [diff] [blame] | 9173 | rsv = btrfs_alloc_block_rsv(root, BTRFS_BLOCK_RSV_TEMP); |
Josef Bacik | fcb80c2 | 2011-05-03 10:40:22 -0400 | [diff] [blame] | 9174 | if (!rsv) |
| 9175 | return -ENOMEM; |
Josef Bacik | 4a33854 | 2011-08-29 11:01:31 -0400 | [diff] [blame] | 9176 | rsv->size = min_size; |
Josef Bacik | ca7e70f | 2012-08-27 17:48:15 -0400 | [diff] [blame] | 9177 | rsv->failfast = 1; |
Josef Bacik | f0cd846 | 2011-03-04 14:37:08 -0500 | [diff] [blame] | 9178 | |
Josef Bacik | 907cbce | 2011-08-08 13:46:15 -0400 | [diff] [blame] | 9179 | /* |
Josef Bacik | 0712718 | 2011-08-19 10:29:59 -0400 | [diff] [blame] | 9180 | * 1 for the truncate slack space |
Josef Bacik | 907cbce | 2011-08-08 13:46:15 -0400 | [diff] [blame] | 9181 | * 1 for updating the inode. |
| 9182 | */ |
Josef Bacik | f3fe820 | 2013-01-07 17:03:21 -0500 | [diff] [blame] | 9183 | trans = btrfs_start_transaction(root, 2); |
Josef Bacik | fcb80c2 | 2011-05-03 10:40:22 -0400 | [diff] [blame] | 9184 | if (IS_ERR(trans)) { |
| 9185 | err = PTR_ERR(trans); |
| 9186 | goto out; |
| 9187 | } |
Josef Bacik | f0cd846 | 2011-03-04 14:37:08 -0500 | [diff] [blame] | 9188 | |
Josef Bacik | 907cbce | 2011-08-08 13:46:15 -0400 | [diff] [blame] | 9189 | /* Migrate the slack space for the truncate to our reserve */ |
| 9190 | ret = btrfs_block_rsv_migrate(&root->fs_info->trans_block_rsv, rsv, |
Josef Bacik | 25d609f | 2016-03-25 13:25:48 -0400 | [diff] [blame] | 9191 | min_size, 0); |
Josef Bacik | fcb80c2 | 2011-05-03 10:40:22 -0400 | [diff] [blame] | 9192 | BUG_ON(ret); |
Josef Bacik | f0cd846 | 2011-03-04 14:37:08 -0500 | [diff] [blame] | 9193 | |
Chris Mason | 5a3f23d | 2009-03-31 13:27:11 -0400 | [diff] [blame] | 9194 | /* |
Josef Bacik | 5dc562c | 2012-08-17 13:14:17 -0400 | [diff] [blame] | 9195 | * So if we truncate and then write and fsync we normally would just |
| 9196 | * write the extents that changed, which is a problem if we need to |
| 9197 | * first truncate that entire inode. So set this flag so we write out |
| 9198 | * all of the extents in the inode to the sync log so we're completely |
| 9199 | * safe. |
| 9200 | */ |
| 9201 | set_bit(BTRFS_INODE_NEEDS_FULL_SYNC, &BTRFS_I(inode)->runtime_flags); |
Josef Bacik | ca7e70f | 2012-08-27 17:48:15 -0400 | [diff] [blame] | 9202 | trans->block_rsv = rsv; |
Josef Bacik | 5dc562c | 2012-08-17 13:14:17 -0400 | [diff] [blame] | 9203 | |
Yan, Zheng | 8082510 | 2009-11-12 09:35:36 +0000 | [diff] [blame] | 9204 | while (1) { |
| 9205 | ret = btrfs_truncate_inode_items(trans, root, inode, |
| 9206 | inode->i_size, |
| 9207 | BTRFS_EXTENT_DATA_KEY); |
Chris Mason | 28ed134 | 2014-12-17 09:41:04 -0800 | [diff] [blame] | 9208 | if (ret != -ENOSPC && ret != -EAGAIN) { |
Josef Bacik | 3893e33 | 2011-01-31 16:03:11 -0500 | [diff] [blame] | 9209 | err = ret; |
Yan, Zheng | 8082510 | 2009-11-12 09:35:36 +0000 | [diff] [blame] | 9210 | break; |
Josef Bacik | 3893e33 | 2011-01-31 16:03:11 -0500 | [diff] [blame] | 9211 | } |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 9212 | |
Josef Bacik | fcb80c2 | 2011-05-03 10:40:22 -0400 | [diff] [blame] | 9213 | trans->block_rsv = &root->fs_info->trans_block_rsv; |
Yan, Zheng | 8082510 | 2009-11-12 09:35:36 +0000 | [diff] [blame] | 9214 | ret = btrfs_update_inode(trans, root, inode); |
Josef Bacik | 3893e33 | 2011-01-31 16:03:11 -0500 | [diff] [blame] | 9215 | if (ret) { |
| 9216 | err = ret; |
| 9217 | break; |
| 9218 | } |
Josef Bacik | ca7e70f | 2012-08-27 17:48:15 -0400 | [diff] [blame] | 9219 | |
Yan, Zheng | 8082510 | 2009-11-12 09:35:36 +0000 | [diff] [blame] | 9220 | btrfs_end_transaction(trans, root); |
Liu Bo | b53d3f5 | 2012-11-14 14:34:34 +0000 | [diff] [blame] | 9221 | btrfs_btree_balance_dirty(root); |
Josef Bacik | ca7e70f | 2012-08-27 17:48:15 -0400 | [diff] [blame] | 9222 | |
| 9223 | trans = btrfs_start_transaction(root, 2); |
| 9224 | if (IS_ERR(trans)) { |
| 9225 | ret = err = PTR_ERR(trans); |
| 9226 | trans = NULL; |
| 9227 | break; |
| 9228 | } |
| 9229 | |
| 9230 | ret = btrfs_block_rsv_migrate(&root->fs_info->trans_block_rsv, |
Josef Bacik | 25d609f | 2016-03-25 13:25:48 -0400 | [diff] [blame] | 9231 | rsv, min_size, 0); |
Josef Bacik | ca7e70f | 2012-08-27 17:48:15 -0400 | [diff] [blame] | 9232 | BUG_ON(ret); /* shouldn't happen */ |
| 9233 | trans->block_rsv = rsv; |
Yan, Zheng | 8082510 | 2009-11-12 09:35:36 +0000 | [diff] [blame] | 9234 | } |
| 9235 | |
| 9236 | if (ret == 0 && inode->i_nlink > 0) { |
Josef Bacik | fcb80c2 | 2011-05-03 10:40:22 -0400 | [diff] [blame] | 9237 | trans->block_rsv = root->orphan_block_rsv; |
Yan, Zheng | 8082510 | 2009-11-12 09:35:36 +0000 | [diff] [blame] | 9238 | ret = btrfs_orphan_del(trans, inode); |
Josef Bacik | 3893e33 | 2011-01-31 16:03:11 -0500 | [diff] [blame] | 9239 | if (ret) |
| 9240 | err = ret; |
Yan, Zheng | 8082510 | 2009-11-12 09:35:36 +0000 | [diff] [blame] | 9241 | } |
| 9242 | |
Chris Mason | 917c16b | 2011-11-08 14:49:59 -0500 | [diff] [blame] | 9243 | if (trans) { |
| 9244 | trans->block_rsv = &root->fs_info->trans_block_rsv; |
| 9245 | ret = btrfs_update_inode(trans, root, inode); |
| 9246 | if (ret && !err) |
| 9247 | err = ret; |
Josef Bacik | 7b12876 | 2008-07-24 12:17:14 -0400 | [diff] [blame] | 9248 | |
Josef Bacik | 7ad85bb | 2012-01-12 19:10:12 -0500 | [diff] [blame] | 9249 | ret = btrfs_end_transaction(trans, root); |
Liu Bo | b53d3f5 | 2012-11-14 14:34:34 +0000 | [diff] [blame] | 9250 | btrfs_btree_balance_dirty(root); |
Chris Mason | 917c16b | 2011-11-08 14:49:59 -0500 | [diff] [blame] | 9251 | } |
Josef Bacik | fcb80c2 | 2011-05-03 10:40:22 -0400 | [diff] [blame] | 9252 | out: |
| 9253 | btrfs_free_block_rsv(root, rsv); |
| 9254 | |
Josef Bacik | 3893e33 | 2011-01-31 16:03:11 -0500 | [diff] [blame] | 9255 | if (ret && !err) |
| 9256 | err = ret; |
Josef Bacik | a41ad39 | 2011-01-31 15:30:16 -0500 | [diff] [blame] | 9257 | |
Josef Bacik | 3893e33 | 2011-01-31 16:03:11 -0500 | [diff] [blame] | 9258 | return err; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 9259 | } |
| 9260 | |
Sven Wegener | 3b96362 | 2008-06-09 21:57:42 -0400 | [diff] [blame] | 9261 | /* |
Chris Mason | d352ac6 | 2008-09-29 15:18:18 -0400 | [diff] [blame] | 9262 | * create a new subvolume directory/inode (helper for the ioctl). |
| 9263 | */ |
Yan Zheng | d2fb343 | 2008-12-11 16:30:39 -0500 | [diff] [blame] | 9264 | int btrfs_create_subvol_root(struct btrfs_trans_handle *trans, |
Filipe David Borba Manana | 6354192 | 2014-01-07 11:47:46 +0000 | [diff] [blame] | 9265 | struct btrfs_root *new_root, |
| 9266 | struct btrfs_root *parent_root, |
| 9267 | u64 new_dirid) |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 9268 | { |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 9269 | struct inode *inode; |
Yan, Zheng | 76dda93 | 2009-09-21 16:00:26 -0400 | [diff] [blame] | 9270 | int err; |
Chris Mason | 00e4e6b | 2008-08-05 11:18:09 -0400 | [diff] [blame] | 9271 | u64 index = 0; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 9272 | |
Florian Albrechtskirchinger | 12fc9d0 | 2012-02-10 22:15:54 +0100 | [diff] [blame] | 9273 | inode = btrfs_new_inode(trans, new_root, NULL, "..", 2, |
| 9274 | new_dirid, new_dirid, |
| 9275 | S_IFDIR | (~current_umask() & S_IRWXUGO), |
| 9276 | &index); |
Chris Mason | 54aa1f4 | 2007-06-22 14:16:25 -0400 | [diff] [blame] | 9277 | if (IS_ERR(inode)) |
Christoph Hellwig | f46b5a6 | 2008-06-11 21:53:53 -0400 | [diff] [blame] | 9278 | return PTR_ERR(inode); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 9279 | inode->i_op = &btrfs_dir_inode_operations; |
| 9280 | inode->i_fop = &btrfs_dir_file_operations; |
| 9281 | |
Miklos Szeredi | bfe8684 | 2011-10-28 14:13:29 +0200 | [diff] [blame] | 9282 | set_nlink(inode, 1); |
Chris Mason | dbe674a | 2008-07-17 12:54:05 -0400 | [diff] [blame] | 9283 | btrfs_i_size_write(inode, 0); |
Chris Mason | b0d5d10 | 2014-09-08 13:08:51 -0700 | [diff] [blame] | 9284 | unlock_new_inode(inode); |
Sven Wegener | 3b96362 | 2008-06-09 21:57:42 -0400 | [diff] [blame] | 9285 | |
Filipe David Borba Manana | 6354192 | 2014-01-07 11:47:46 +0000 | [diff] [blame] | 9286 | err = btrfs_subvol_inherit_props(trans, new_root, parent_root); |
| 9287 | if (err) |
| 9288 | btrfs_err(new_root->fs_info, |
David Sterba | 351fd35 | 2014-05-15 16:48:20 +0200 | [diff] [blame] | 9289 | "error inheriting subvolume %llu properties: %d", |
Filipe David Borba Manana | 6354192 | 2014-01-07 11:47:46 +0000 | [diff] [blame] | 9290 | new_root->root_key.objectid, err); |
| 9291 | |
Yan, Zheng | 76dda93 | 2009-09-21 16:00:26 -0400 | [diff] [blame] | 9292 | err = btrfs_update_inode(trans, new_root, inode); |
Christoph Hellwig | cb8e709 | 2008-10-09 13:39:39 -0400 | [diff] [blame] | 9293 | |
Yan, Zheng | 76dda93 | 2009-09-21 16:00:26 -0400 | [diff] [blame] | 9294 | iput(inode); |
Mark Fasheh | ce59897 | 2011-07-26 11:32:23 -0700 | [diff] [blame] | 9295 | return err; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 9296 | } |
| 9297 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 9298 | struct inode *btrfs_alloc_inode(struct super_block *sb) |
| 9299 | { |
| 9300 | struct btrfs_inode *ei; |
Yan, Zheng | 2ead6ae | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 9301 | struct inode *inode; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 9302 | |
| 9303 | ei = kmem_cache_alloc(btrfs_inode_cachep, GFP_NOFS); |
| 9304 | if (!ei) |
| 9305 | return NULL; |
Yan, Zheng | 2ead6ae | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 9306 | |
| 9307 | ei->root = NULL; |
Yan, Zheng | 2ead6ae | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 9308 | ei->generation = 0; |
Josef Bacik | 15ee9bc | 2007-08-10 16:22:09 -0400 | [diff] [blame] | 9309 | ei->last_trans = 0; |
Chris Mason | 257c62e | 2009-10-13 13:21:08 -0400 | [diff] [blame] | 9310 | ei->last_sub_trans = 0; |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 9311 | ei->logged_trans = 0; |
Yan, Zheng | 2ead6ae | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 9312 | ei->delalloc_bytes = 0; |
Wang Shilong | 47059d9 | 2014-07-03 18:22:07 +0800 | [diff] [blame] | 9313 | ei->defrag_bytes = 0; |
Yan, Zheng | 2ead6ae | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 9314 | ei->disk_i_size = 0; |
| 9315 | ei->flags = 0; |
Josef Bacik | 7709cde | 2011-08-04 10:25:02 -0400 | [diff] [blame] | 9316 | ei->csum_bytes = 0; |
Yan, Zheng | 2ead6ae | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 9317 | ei->index_cnt = (u64)-1; |
Miao Xie | 67de117 | 2013-12-26 13:07:06 +0800 | [diff] [blame] | 9318 | ei->dir_index = 0; |
Yan, Zheng | 2ead6ae | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 9319 | ei->last_unlink_trans = 0; |
Liu Bo | 46d8bc3 | 2012-08-29 01:07:55 -0600 | [diff] [blame] | 9320 | ei->last_log_commit = 0; |
David Sterba | 8089fe6 | 2015-11-19 14:15:51 +0100 | [diff] [blame] | 9321 | ei->delayed_iput_count = 0; |
Yan, Zheng | 2ead6ae | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 9322 | |
Josef Bacik | 9e0baf6 | 2011-07-15 15:16:44 +0000 | [diff] [blame] | 9323 | spin_lock_init(&ei->lock); |
| 9324 | ei->outstanding_extents = 0; |
| 9325 | ei->reserved_extents = 0; |
Yan, Zheng | 2ead6ae | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 9326 | |
Josef Bacik | 72ac3c0 | 2012-05-23 14:13:11 -0400 | [diff] [blame] | 9327 | ei->runtime_flags = 0; |
Li Zefan | 261507a0 | 2010-12-17 14:21:50 +0800 | [diff] [blame] | 9328 | ei->force_compress = BTRFS_COMPRESS_NONE; |
Yan, Zheng | 2ead6ae | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 9329 | |
Miao Xie | 16cdcec | 2011-04-22 18:12:22 +0800 | [diff] [blame] | 9330 | ei->delayed_node = NULL; |
| 9331 | |
chandan r | 9cc97d6 | 2012-07-04 12:48:07 +0530 | [diff] [blame] | 9332 | ei->i_otime.tv_sec = 0; |
| 9333 | ei->i_otime.tv_nsec = 0; |
| 9334 | |
Yan, Zheng | 2ead6ae | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 9335 | inode = &ei->vfs_inode; |
David Sterba | a8067e0 | 2011-04-21 00:34:43 +0200 | [diff] [blame] | 9336 | extent_map_tree_init(&ei->extent_tree); |
David Sterba | f993c88 | 2011-04-20 23:35:57 +0200 | [diff] [blame] | 9337 | extent_io_tree_init(&ei->io_tree, &inode->i_data); |
| 9338 | extent_io_tree_init(&ei->io_failure_tree, &inode->i_data); |
Josef Bacik | 0b32f4b | 2012-03-13 09:38:00 -0400 | [diff] [blame] | 9339 | ei->io_tree.track_uptodate = 1; |
| 9340 | ei->io_failure_tree.track_uptodate = 1; |
Josef Bacik | b812ce2 | 2012-11-16 13:56:32 -0500 | [diff] [blame] | 9341 | atomic_set(&ei->sync_writers, 0); |
Yan, Zheng | 2ead6ae | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 9342 | mutex_init(&ei->log_mutex); |
Josef Bacik | f248679 | 2012-01-13 12:09:22 -0500 | [diff] [blame] | 9343 | mutex_init(&ei->delalloc_mutex); |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 9344 | btrfs_ordered_inode_tree_init(&ei->ordered_tree); |
Yan, Zheng | 2ead6ae | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 9345 | INIT_LIST_HEAD(&ei->delalloc_inodes); |
David Sterba | 8089fe6 | 2015-11-19 14:15:51 +0100 | [diff] [blame] | 9346 | INIT_LIST_HEAD(&ei->delayed_iput); |
Yan, Zheng | 2ead6ae | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 9347 | RB_CLEAR_NODE(&ei->rb_node); |
Filipe Manana | 5f9a8a5 | 2016-05-12 13:53:36 +0100 | [diff] [blame] | 9348 | init_rwsem(&ei->dio_sem); |
Yan, Zheng | 2ead6ae | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 9349 | |
| 9350 | return inode; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 9351 | } |
| 9352 | |
Josef Bacik | aaedb55 | 2013-10-11 14:44:09 -0400 | [diff] [blame] | 9353 | #ifdef CONFIG_BTRFS_FS_RUN_SANITY_TESTS |
| 9354 | void btrfs_test_destroy_inode(struct inode *inode) |
| 9355 | { |
| 9356 | btrfs_drop_extent_cache(inode, 0, (u64)-1, 0); |
| 9357 | kmem_cache_free(btrfs_inode_cachep, BTRFS_I(inode)); |
| 9358 | } |
| 9359 | #endif |
| 9360 | |
Nick Piggin | fa0d7e3d | 2011-01-07 17:49:49 +1100 | [diff] [blame] | 9361 | static void btrfs_i_callback(struct rcu_head *head) |
| 9362 | { |
| 9363 | struct inode *inode = container_of(head, struct inode, i_rcu); |
Nick Piggin | fa0d7e3d | 2011-01-07 17:49:49 +1100 | [diff] [blame] | 9364 | kmem_cache_free(btrfs_inode_cachep, BTRFS_I(inode)); |
| 9365 | } |
| 9366 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 9367 | void btrfs_destroy_inode(struct inode *inode) |
| 9368 | { |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 9369 | struct btrfs_ordered_extent *ordered; |
Chris Mason | 5a3f23d | 2009-03-31 13:27:11 -0400 | [diff] [blame] | 9370 | struct btrfs_root *root = BTRFS_I(inode)->root; |
| 9371 | |
Al Viro | b3d9b7a | 2012-06-09 13:51:19 -0400 | [diff] [blame] | 9372 | WARN_ON(!hlist_empty(&inode->i_dentry)); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 9373 | WARN_ON(inode->i_data.nrpages); |
Josef Bacik | 9e0baf6 | 2011-07-15 15:16:44 +0000 | [diff] [blame] | 9374 | WARN_ON(BTRFS_I(inode)->outstanding_extents); |
| 9375 | WARN_ON(BTRFS_I(inode)->reserved_extents); |
Josef Bacik | 7709cde | 2011-08-04 10:25:02 -0400 | [diff] [blame] | 9376 | WARN_ON(BTRFS_I(inode)->delalloc_bytes); |
| 9377 | WARN_ON(BTRFS_I(inode)->csum_bytes); |
Wang Shilong | 47059d9 | 2014-07-03 18:22:07 +0800 | [diff] [blame] | 9378 | WARN_ON(BTRFS_I(inode)->defrag_bytes); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 9379 | |
Chris Mason | 5a3f23d | 2009-03-31 13:27:11 -0400 | [diff] [blame] | 9380 | /* |
Josef Bacik | a6dbd42 | 2009-11-11 15:53:34 -0500 | [diff] [blame] | 9381 | * This can happen where we create an inode, but somebody else also |
| 9382 | * created the same inode and we need to destroy the one we already |
| 9383 | * created. |
| 9384 | */ |
| 9385 | if (!root) |
| 9386 | goto free; |
| 9387 | |
Josef Bacik | 8a35d95 | 2012-05-23 14:26:42 -0400 | [diff] [blame] | 9388 | if (test_bit(BTRFS_INODE_HAS_ORPHAN_ITEM, |
| 9389 | &BTRFS_I(inode)->runtime_flags)) { |
Simon Kirby | c2cf52e | 2013-03-19 22:41:23 +0000 | [diff] [blame] | 9390 | btrfs_info(root->fs_info, "inode %llu still on the orphan list", |
Geert Uytterhoeven | c1c9ff7 | 2013-08-20 13:20:07 +0200 | [diff] [blame] | 9391 | btrfs_ino(inode)); |
Josef Bacik | 8a35d95 | 2012-05-23 14:26:42 -0400 | [diff] [blame] | 9392 | atomic_dec(&root->orphan_inodes); |
Josef Bacik | 7b12876 | 2008-07-24 12:17:14 -0400 | [diff] [blame] | 9393 | } |
Josef Bacik | 7b12876 | 2008-07-24 12:17:14 -0400 | [diff] [blame] | 9394 | |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 9395 | while (1) { |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 9396 | ordered = btrfs_lookup_first_ordered_extent(inode, (u64)-1); |
| 9397 | if (!ordered) |
| 9398 | break; |
| 9399 | else { |
Jeff Mahoney | 5d163e0 | 2016-09-20 10:05:00 -0400 | [diff] [blame] | 9400 | btrfs_err(root->fs_info, |
| 9401 | "found ordered extent %llu %llu on inode cleanup", |
| 9402 | ordered->file_offset, ordered->len); |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 9403 | btrfs_remove_ordered_extent(inode, ordered); |
| 9404 | btrfs_put_ordered_extent(ordered); |
| 9405 | btrfs_put_ordered_extent(ordered); |
| 9406 | } |
| 9407 | } |
Qu Wenruo | 56fa9d0 | 2015-10-13 09:53:10 +0800 | [diff] [blame] | 9408 | btrfs_qgroup_check_reserved_leak(inode); |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 9409 | inode_tree_del(inode); |
Zheng Yan | 5b21f2e | 2008-09-26 10:05:38 -0400 | [diff] [blame] | 9410 | btrfs_drop_extent_cache(inode, 0, (u64)-1, 0); |
Josef Bacik | a6dbd42 | 2009-11-11 15:53:34 -0500 | [diff] [blame] | 9411 | free: |
Nick Piggin | fa0d7e3d | 2011-01-07 17:49:49 +1100 | [diff] [blame] | 9412 | call_rcu(&inode->i_rcu, btrfs_i_callback); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 9413 | } |
| 9414 | |
Al Viro | 45321ac | 2010-06-07 13:43:19 -0400 | [diff] [blame] | 9415 | int btrfs_drop_inode(struct inode *inode) |
Yan, Zheng | 76dda93 | 2009-09-21 16:00:26 -0400 | [diff] [blame] | 9416 | { |
| 9417 | struct btrfs_root *root = BTRFS_I(inode)->root; |
Al Viro | 45321ac | 2010-06-07 13:43:19 -0400 | [diff] [blame] | 9418 | |
Naohiro Aota | 6379ef9 | 2013-06-06 09:56:34 +0000 | [diff] [blame] | 9419 | if (root == NULL) |
| 9420 | return 1; |
| 9421 | |
Liu Bo | fa6ac87 | 2013-02-20 14:10:23 +0000 | [diff] [blame] | 9422 | /* the snap/subvol tree is on deleting */ |
Stefan Behrens | 69e9c6c | 2013-09-05 16:58:43 +0200 | [diff] [blame] | 9423 | if (btrfs_root_refs(&root->root_item) == 0) |
Al Viro | 45321ac | 2010-06-07 13:43:19 -0400 | [diff] [blame] | 9424 | return 1; |
Yan, Zheng | 76dda93 | 2009-09-21 16:00:26 -0400 | [diff] [blame] | 9425 | else |
Al Viro | 45321ac | 2010-06-07 13:43:19 -0400 | [diff] [blame] | 9426 | return generic_drop_inode(inode); |
Yan, Zheng | 76dda93 | 2009-09-21 16:00:26 -0400 | [diff] [blame] | 9427 | } |
| 9428 | |
Sven Wegener | 0ee0fda | 2008-07-30 16:54:26 -0400 | [diff] [blame] | 9429 | static void init_once(void *foo) |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 9430 | { |
| 9431 | struct btrfs_inode *ei = (struct btrfs_inode *) foo; |
| 9432 | |
| 9433 | inode_init_once(&ei->vfs_inode); |
| 9434 | } |
| 9435 | |
| 9436 | void btrfs_destroy_cachep(void) |
| 9437 | { |
Kirill A. Shutemov | 8c0a853 | 2012-09-26 11:33:07 +1000 | [diff] [blame] | 9438 | /* |
| 9439 | * Make sure all delayed rcu free inodes are flushed before we |
| 9440 | * destroy cache. |
| 9441 | */ |
| 9442 | rcu_barrier(); |
Kinglong Mee | 5598e90 | 2016-01-29 21:36:35 +0800 | [diff] [blame] | 9443 | kmem_cache_destroy(btrfs_inode_cachep); |
| 9444 | kmem_cache_destroy(btrfs_trans_handle_cachep); |
| 9445 | kmem_cache_destroy(btrfs_transaction_cachep); |
| 9446 | kmem_cache_destroy(btrfs_path_cachep); |
| 9447 | kmem_cache_destroy(btrfs_free_space_cachep); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 9448 | } |
| 9449 | |
| 9450 | int btrfs_init_cachep(void) |
| 9451 | { |
David Sterba | 837e197 | 2012-09-07 03:00:48 -0600 | [diff] [blame] | 9452 | btrfs_inode_cachep = kmem_cache_create("btrfs_inode", |
Christoph Hellwig | 9601e3f | 2009-04-13 15:33:09 +0200 | [diff] [blame] | 9453 | sizeof(struct btrfs_inode), 0, |
Vladimir Davydov | 5d09705 | 2016-01-14 15:18:21 -0800 | [diff] [blame] | 9454 | SLAB_RECLAIM_ACCOUNT | SLAB_MEM_SPREAD | SLAB_ACCOUNT, |
| 9455 | init_once); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 9456 | if (!btrfs_inode_cachep) |
| 9457 | goto fail; |
Christoph Hellwig | 9601e3f | 2009-04-13 15:33:09 +0200 | [diff] [blame] | 9458 | |
David Sterba | 837e197 | 2012-09-07 03:00:48 -0600 | [diff] [blame] | 9459 | btrfs_trans_handle_cachep = kmem_cache_create("btrfs_trans_handle", |
Christoph Hellwig | 9601e3f | 2009-04-13 15:33:09 +0200 | [diff] [blame] | 9460 | sizeof(struct btrfs_trans_handle), 0, |
Nikolay Borisov | fba4b69 | 2016-06-23 21:17:08 +0300 | [diff] [blame] | 9461 | SLAB_TEMPORARY | SLAB_MEM_SPREAD, NULL); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 9462 | if (!btrfs_trans_handle_cachep) |
| 9463 | goto fail; |
Christoph Hellwig | 9601e3f | 2009-04-13 15:33:09 +0200 | [diff] [blame] | 9464 | |
David Sterba | 837e197 | 2012-09-07 03:00:48 -0600 | [diff] [blame] | 9465 | btrfs_transaction_cachep = kmem_cache_create("btrfs_transaction", |
Christoph Hellwig | 9601e3f | 2009-04-13 15:33:09 +0200 | [diff] [blame] | 9466 | sizeof(struct btrfs_transaction), 0, |
Nikolay Borisov | fba4b69 | 2016-06-23 21:17:08 +0300 | [diff] [blame] | 9467 | SLAB_TEMPORARY | SLAB_MEM_SPREAD, NULL); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 9468 | if (!btrfs_transaction_cachep) |
| 9469 | goto fail; |
Christoph Hellwig | 9601e3f | 2009-04-13 15:33:09 +0200 | [diff] [blame] | 9470 | |
David Sterba | 837e197 | 2012-09-07 03:00:48 -0600 | [diff] [blame] | 9471 | btrfs_path_cachep = kmem_cache_create("btrfs_path", |
Christoph Hellwig | 9601e3f | 2009-04-13 15:33:09 +0200 | [diff] [blame] | 9472 | sizeof(struct btrfs_path), 0, |
Nikolay Borisov | fba4b69 | 2016-06-23 21:17:08 +0300 | [diff] [blame] | 9473 | SLAB_MEM_SPREAD, NULL); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 9474 | if (!btrfs_path_cachep) |
| 9475 | goto fail; |
Christoph Hellwig | 9601e3f | 2009-04-13 15:33:09 +0200 | [diff] [blame] | 9476 | |
David Sterba | 837e197 | 2012-09-07 03:00:48 -0600 | [diff] [blame] | 9477 | btrfs_free_space_cachep = kmem_cache_create("btrfs_free_space", |
Josef Bacik | dc89e98 | 2011-01-28 17:05:48 -0500 | [diff] [blame] | 9478 | sizeof(struct btrfs_free_space), 0, |
Nikolay Borisov | fba4b69 | 2016-06-23 21:17:08 +0300 | [diff] [blame] | 9479 | SLAB_MEM_SPREAD, NULL); |
Josef Bacik | dc89e98 | 2011-01-28 17:05:48 -0500 | [diff] [blame] | 9480 | if (!btrfs_free_space_cachep) |
| 9481 | goto fail; |
| 9482 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 9483 | return 0; |
| 9484 | fail: |
| 9485 | btrfs_destroy_cachep(); |
| 9486 | return -ENOMEM; |
| 9487 | } |
| 9488 | |
| 9489 | static int btrfs_getattr(struct vfsmount *mnt, |
| 9490 | struct dentry *dentry, struct kstat *stat) |
| 9491 | { |
Miao Xie | df0af1a | 2013-01-29 10:11:59 +0000 | [diff] [blame] | 9492 | u64 delalloc_bytes; |
David Howells | 2b0143b | 2015-03-17 22:25:59 +0000 | [diff] [blame] | 9493 | struct inode *inode = d_inode(dentry); |
David Sterba | fadc0d8 | 2011-11-20 07:33:38 -0500 | [diff] [blame] | 9494 | u32 blocksize = inode->i_sb->s_blocksize; |
| 9495 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 9496 | generic_fillattr(inode, stat); |
Al Viro | 0ee5dc6 | 2011-07-07 15:44:25 -0400 | [diff] [blame] | 9497 | stat->dev = BTRFS_I(inode)->root->anon_dev; |
Miao Xie | df0af1a | 2013-01-29 10:11:59 +0000 | [diff] [blame] | 9498 | |
| 9499 | spin_lock(&BTRFS_I(inode)->lock); |
| 9500 | delalloc_bytes = BTRFS_I(inode)->delalloc_bytes; |
| 9501 | spin_unlock(&BTRFS_I(inode)->lock); |
David Sterba | fadc0d8 | 2011-11-20 07:33:38 -0500 | [diff] [blame] | 9502 | stat->blocks = (ALIGN(inode_get_bytes(inode), blocksize) + |
Miao Xie | df0af1a | 2013-01-29 10:11:59 +0000 | [diff] [blame] | 9503 | ALIGN(delalloc_bytes, blocksize)) >> 9; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 9504 | return 0; |
| 9505 | } |
| 9506 | |
Dan Fuhry | cdd1fed | 2016-03-17 15:23:38 +0100 | [diff] [blame] | 9507 | static int btrfs_rename_exchange(struct inode *old_dir, |
| 9508 | struct dentry *old_dentry, |
| 9509 | struct inode *new_dir, |
| 9510 | struct dentry *new_dentry) |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 9511 | { |
| 9512 | struct btrfs_trans_handle *trans; |
| 9513 | struct btrfs_root *root = BTRFS_I(old_dir)->root; |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 9514 | struct btrfs_root *dest = BTRFS_I(new_dir)->root; |
Dan Fuhry | cdd1fed | 2016-03-17 15:23:38 +0100 | [diff] [blame] | 9515 | struct inode *new_inode = new_dentry->d_inode; |
| 9516 | struct inode *old_inode = old_dentry->d_inode; |
Deepa Dinamani | c2050a4 | 2016-09-14 07:48:06 -0700 | [diff] [blame] | 9517 | struct timespec ctime = current_time(old_inode); |
Dan Fuhry | cdd1fed | 2016-03-17 15:23:38 +0100 | [diff] [blame] | 9518 | struct dentry *parent; |
| 9519 | u64 old_ino = btrfs_ino(old_inode); |
| 9520 | u64 new_ino = btrfs_ino(new_inode); |
| 9521 | u64 old_idx = 0; |
| 9522 | u64 new_idx = 0; |
| 9523 | u64 root_objectid; |
| 9524 | int ret; |
Filipe Manana | 86e8aa0 | 2016-05-05 02:02:27 +0100 | [diff] [blame] | 9525 | bool root_log_pinned = false; |
| 9526 | bool dest_log_pinned = false; |
Dan Fuhry | cdd1fed | 2016-03-17 15:23:38 +0100 | [diff] [blame] | 9527 | |
| 9528 | /* we only allow rename subvolume link between subvolumes */ |
| 9529 | if (old_ino != BTRFS_FIRST_FREE_OBJECTID && root != dest) |
| 9530 | return -EXDEV; |
| 9531 | |
| 9532 | /* close the race window with snapshot create/destroy ioctl */ |
| 9533 | if (old_ino == BTRFS_FIRST_FREE_OBJECTID) |
| 9534 | down_read(&root->fs_info->subvol_sem); |
| 9535 | if (new_ino == BTRFS_FIRST_FREE_OBJECTID) |
| 9536 | down_read(&dest->fs_info->subvol_sem); |
| 9537 | |
| 9538 | /* |
| 9539 | * We want to reserve the absolute worst case amount of items. So if |
| 9540 | * both inodes are subvols and we need to unlink them then that would |
| 9541 | * require 4 item modifications, but if they are both normal inodes it |
| 9542 | * would require 5 item modifications, so we'll assume their normal |
| 9543 | * inodes. So 5 * 2 is 10, plus 2 for the new links, so 12 total items |
| 9544 | * should cover the worst case number of items we'll modify. |
| 9545 | */ |
| 9546 | trans = btrfs_start_transaction(root, 12); |
| 9547 | if (IS_ERR(trans)) { |
| 9548 | ret = PTR_ERR(trans); |
| 9549 | goto out_notrans; |
| 9550 | } |
| 9551 | |
| 9552 | /* |
| 9553 | * We need to find a free sequence number both in the source and |
| 9554 | * in the destination directory for the exchange. |
| 9555 | */ |
| 9556 | ret = btrfs_set_inode_index(new_dir, &old_idx); |
| 9557 | if (ret) |
| 9558 | goto out_fail; |
| 9559 | ret = btrfs_set_inode_index(old_dir, &new_idx); |
| 9560 | if (ret) |
| 9561 | goto out_fail; |
| 9562 | |
| 9563 | BTRFS_I(old_inode)->dir_index = 0ULL; |
| 9564 | BTRFS_I(new_inode)->dir_index = 0ULL; |
| 9565 | |
| 9566 | /* Reference for the source. */ |
| 9567 | if (old_ino == BTRFS_FIRST_FREE_OBJECTID) { |
| 9568 | /* force full log commit if subvolume involved. */ |
| 9569 | btrfs_set_log_full_commit(root->fs_info, trans); |
| 9570 | } else { |
Filipe Manana | 376e5a5 | 2016-05-05 02:08:56 +0100 | [diff] [blame] | 9571 | btrfs_pin_log_trans(root); |
| 9572 | root_log_pinned = true; |
Dan Fuhry | cdd1fed | 2016-03-17 15:23:38 +0100 | [diff] [blame] | 9573 | ret = btrfs_insert_inode_ref(trans, dest, |
| 9574 | new_dentry->d_name.name, |
| 9575 | new_dentry->d_name.len, |
| 9576 | old_ino, |
| 9577 | btrfs_ino(new_dir), old_idx); |
| 9578 | if (ret) |
| 9579 | goto out_fail; |
Dan Fuhry | cdd1fed | 2016-03-17 15:23:38 +0100 | [diff] [blame] | 9580 | } |
| 9581 | |
| 9582 | /* And now for the dest. */ |
| 9583 | if (new_ino == BTRFS_FIRST_FREE_OBJECTID) { |
| 9584 | /* force full log commit if subvolume involved. */ |
| 9585 | btrfs_set_log_full_commit(dest->fs_info, trans); |
| 9586 | } else { |
Filipe Manana | 376e5a5 | 2016-05-05 02:08:56 +0100 | [diff] [blame] | 9587 | btrfs_pin_log_trans(dest); |
| 9588 | dest_log_pinned = true; |
Dan Fuhry | cdd1fed | 2016-03-17 15:23:38 +0100 | [diff] [blame] | 9589 | ret = btrfs_insert_inode_ref(trans, root, |
| 9590 | old_dentry->d_name.name, |
| 9591 | old_dentry->d_name.len, |
| 9592 | new_ino, |
| 9593 | btrfs_ino(old_dir), new_idx); |
| 9594 | if (ret) |
| 9595 | goto out_fail; |
Dan Fuhry | cdd1fed | 2016-03-17 15:23:38 +0100 | [diff] [blame] | 9596 | } |
| 9597 | |
| 9598 | /* Update inode version and ctime/mtime. */ |
| 9599 | inode_inc_iversion(old_dir); |
| 9600 | inode_inc_iversion(new_dir); |
| 9601 | inode_inc_iversion(old_inode); |
| 9602 | inode_inc_iversion(new_inode); |
| 9603 | old_dir->i_ctime = old_dir->i_mtime = ctime; |
| 9604 | new_dir->i_ctime = new_dir->i_mtime = ctime; |
| 9605 | old_inode->i_ctime = ctime; |
| 9606 | new_inode->i_ctime = ctime; |
| 9607 | |
| 9608 | if (old_dentry->d_parent != new_dentry->d_parent) { |
| 9609 | btrfs_record_unlink_dir(trans, old_dir, old_inode, 1); |
| 9610 | btrfs_record_unlink_dir(trans, new_dir, new_inode, 1); |
| 9611 | } |
| 9612 | |
| 9613 | /* src is a subvolume */ |
| 9614 | if (old_ino == BTRFS_FIRST_FREE_OBJECTID) { |
| 9615 | root_objectid = BTRFS_I(old_inode)->root->root_key.objectid; |
| 9616 | ret = btrfs_unlink_subvol(trans, root, old_dir, |
| 9617 | root_objectid, |
| 9618 | old_dentry->d_name.name, |
| 9619 | old_dentry->d_name.len); |
| 9620 | } else { /* src is an inode */ |
| 9621 | ret = __btrfs_unlink_inode(trans, root, old_dir, |
| 9622 | old_dentry->d_inode, |
| 9623 | old_dentry->d_name.name, |
| 9624 | old_dentry->d_name.len); |
| 9625 | if (!ret) |
| 9626 | ret = btrfs_update_inode(trans, root, old_inode); |
| 9627 | } |
| 9628 | if (ret) { |
Jeff Mahoney | 6664283 | 2016-06-10 18:19:25 -0400 | [diff] [blame] | 9629 | btrfs_abort_transaction(trans, ret); |
Dan Fuhry | cdd1fed | 2016-03-17 15:23:38 +0100 | [diff] [blame] | 9630 | goto out_fail; |
| 9631 | } |
| 9632 | |
| 9633 | /* dest is a subvolume */ |
| 9634 | if (new_ino == BTRFS_FIRST_FREE_OBJECTID) { |
| 9635 | root_objectid = BTRFS_I(new_inode)->root->root_key.objectid; |
| 9636 | ret = btrfs_unlink_subvol(trans, dest, new_dir, |
| 9637 | root_objectid, |
| 9638 | new_dentry->d_name.name, |
| 9639 | new_dentry->d_name.len); |
| 9640 | } else { /* dest is an inode */ |
| 9641 | ret = __btrfs_unlink_inode(trans, dest, new_dir, |
| 9642 | new_dentry->d_inode, |
| 9643 | new_dentry->d_name.name, |
| 9644 | new_dentry->d_name.len); |
| 9645 | if (!ret) |
| 9646 | ret = btrfs_update_inode(trans, dest, new_inode); |
| 9647 | } |
| 9648 | if (ret) { |
Jeff Mahoney | 6664283 | 2016-06-10 18:19:25 -0400 | [diff] [blame] | 9649 | btrfs_abort_transaction(trans, ret); |
Dan Fuhry | cdd1fed | 2016-03-17 15:23:38 +0100 | [diff] [blame] | 9650 | goto out_fail; |
| 9651 | } |
| 9652 | |
| 9653 | ret = btrfs_add_link(trans, new_dir, old_inode, |
| 9654 | new_dentry->d_name.name, |
| 9655 | new_dentry->d_name.len, 0, old_idx); |
| 9656 | if (ret) { |
Jeff Mahoney | 6664283 | 2016-06-10 18:19:25 -0400 | [diff] [blame] | 9657 | btrfs_abort_transaction(trans, ret); |
Dan Fuhry | cdd1fed | 2016-03-17 15:23:38 +0100 | [diff] [blame] | 9658 | goto out_fail; |
| 9659 | } |
| 9660 | |
| 9661 | ret = btrfs_add_link(trans, old_dir, new_inode, |
| 9662 | old_dentry->d_name.name, |
| 9663 | old_dentry->d_name.len, 0, new_idx); |
| 9664 | if (ret) { |
Jeff Mahoney | 6664283 | 2016-06-10 18:19:25 -0400 | [diff] [blame] | 9665 | btrfs_abort_transaction(trans, ret); |
Dan Fuhry | cdd1fed | 2016-03-17 15:23:38 +0100 | [diff] [blame] | 9666 | goto out_fail; |
| 9667 | } |
| 9668 | |
| 9669 | if (old_inode->i_nlink == 1) |
| 9670 | BTRFS_I(old_inode)->dir_index = old_idx; |
| 9671 | if (new_inode->i_nlink == 1) |
| 9672 | BTRFS_I(new_inode)->dir_index = new_idx; |
| 9673 | |
Filipe Manana | 86e8aa0 | 2016-05-05 02:02:27 +0100 | [diff] [blame] | 9674 | if (root_log_pinned) { |
Dan Fuhry | cdd1fed | 2016-03-17 15:23:38 +0100 | [diff] [blame] | 9675 | parent = new_dentry->d_parent; |
| 9676 | btrfs_log_new_name(trans, old_inode, old_dir, parent); |
| 9677 | btrfs_end_log_trans(root); |
Filipe Manana | 86e8aa0 | 2016-05-05 02:02:27 +0100 | [diff] [blame] | 9678 | root_log_pinned = false; |
Dan Fuhry | cdd1fed | 2016-03-17 15:23:38 +0100 | [diff] [blame] | 9679 | } |
Filipe Manana | 86e8aa0 | 2016-05-05 02:02:27 +0100 | [diff] [blame] | 9680 | if (dest_log_pinned) { |
Dan Fuhry | cdd1fed | 2016-03-17 15:23:38 +0100 | [diff] [blame] | 9681 | parent = old_dentry->d_parent; |
| 9682 | btrfs_log_new_name(trans, new_inode, new_dir, parent); |
| 9683 | btrfs_end_log_trans(dest); |
Filipe Manana | 86e8aa0 | 2016-05-05 02:02:27 +0100 | [diff] [blame] | 9684 | dest_log_pinned = false; |
Dan Fuhry | cdd1fed | 2016-03-17 15:23:38 +0100 | [diff] [blame] | 9685 | } |
| 9686 | out_fail: |
Filipe Manana | 86e8aa0 | 2016-05-05 02:02:27 +0100 | [diff] [blame] | 9687 | /* |
| 9688 | * If we have pinned a log and an error happened, we unpin tasks |
| 9689 | * trying to sync the log and force them to fallback to a transaction |
| 9690 | * commit if the log currently contains any of the inodes involved in |
| 9691 | * this rename operation (to ensure we do not persist a log with an |
| 9692 | * inconsistent state for any of these inodes or leading to any |
| 9693 | * inconsistencies when replayed). If the transaction was aborted, the |
| 9694 | * abortion reason is propagated to userspace when attempting to commit |
| 9695 | * the transaction. If the log does not contain any of these inodes, we |
| 9696 | * allow the tasks to sync it. |
| 9697 | */ |
| 9698 | if (ret && (root_log_pinned || dest_log_pinned)) { |
| 9699 | if (btrfs_inode_in_log(old_dir, root->fs_info->generation) || |
| 9700 | btrfs_inode_in_log(new_dir, root->fs_info->generation) || |
| 9701 | btrfs_inode_in_log(old_inode, root->fs_info->generation) || |
| 9702 | (new_inode && |
| 9703 | btrfs_inode_in_log(new_inode, root->fs_info->generation))) |
| 9704 | btrfs_set_log_full_commit(root->fs_info, trans); |
| 9705 | |
| 9706 | if (root_log_pinned) { |
| 9707 | btrfs_end_log_trans(root); |
| 9708 | root_log_pinned = false; |
| 9709 | } |
| 9710 | if (dest_log_pinned) { |
| 9711 | btrfs_end_log_trans(dest); |
| 9712 | dest_log_pinned = false; |
| 9713 | } |
| 9714 | } |
Dan Fuhry | cdd1fed | 2016-03-17 15:23:38 +0100 | [diff] [blame] | 9715 | ret = btrfs_end_transaction(trans, root); |
| 9716 | out_notrans: |
| 9717 | if (new_ino == BTRFS_FIRST_FREE_OBJECTID) |
| 9718 | up_read(&dest->fs_info->subvol_sem); |
| 9719 | if (old_ino == BTRFS_FIRST_FREE_OBJECTID) |
| 9720 | up_read(&root->fs_info->subvol_sem); |
| 9721 | |
| 9722 | return ret; |
| 9723 | } |
| 9724 | |
| 9725 | static int btrfs_whiteout_for_rename(struct btrfs_trans_handle *trans, |
| 9726 | struct btrfs_root *root, |
| 9727 | struct inode *dir, |
| 9728 | struct dentry *dentry) |
| 9729 | { |
| 9730 | int ret; |
| 9731 | struct inode *inode; |
| 9732 | u64 objectid; |
| 9733 | u64 index; |
| 9734 | |
| 9735 | ret = btrfs_find_free_ino(root, &objectid); |
| 9736 | if (ret) |
| 9737 | return ret; |
| 9738 | |
| 9739 | inode = btrfs_new_inode(trans, root, dir, |
| 9740 | dentry->d_name.name, |
| 9741 | dentry->d_name.len, |
| 9742 | btrfs_ino(dir), |
| 9743 | objectid, |
| 9744 | S_IFCHR | WHITEOUT_MODE, |
| 9745 | &index); |
| 9746 | |
| 9747 | if (IS_ERR(inode)) { |
| 9748 | ret = PTR_ERR(inode); |
| 9749 | return ret; |
| 9750 | } |
| 9751 | |
| 9752 | inode->i_op = &btrfs_special_inode_operations; |
| 9753 | init_special_inode(inode, inode->i_mode, |
| 9754 | WHITEOUT_DEV); |
| 9755 | |
| 9756 | ret = btrfs_init_inode_security(trans, inode, dir, |
| 9757 | &dentry->d_name); |
| 9758 | if (ret) |
Filipe Manana | c990161 | 2016-05-05 01:41:57 +0100 | [diff] [blame] | 9759 | goto out; |
Dan Fuhry | cdd1fed | 2016-03-17 15:23:38 +0100 | [diff] [blame] | 9760 | |
| 9761 | ret = btrfs_add_nondir(trans, dir, dentry, |
| 9762 | inode, 0, index); |
| 9763 | if (ret) |
Filipe Manana | c990161 | 2016-05-05 01:41:57 +0100 | [diff] [blame] | 9764 | goto out; |
Dan Fuhry | cdd1fed | 2016-03-17 15:23:38 +0100 | [diff] [blame] | 9765 | |
| 9766 | ret = btrfs_update_inode(trans, root, inode); |
Filipe Manana | c990161 | 2016-05-05 01:41:57 +0100 | [diff] [blame] | 9767 | out: |
Dan Fuhry | cdd1fed | 2016-03-17 15:23:38 +0100 | [diff] [blame] | 9768 | unlock_new_inode(inode); |
Filipe Manana | c990161 | 2016-05-05 01:41:57 +0100 | [diff] [blame] | 9769 | if (ret) |
| 9770 | inode_dec_link_count(inode); |
Dan Fuhry | cdd1fed | 2016-03-17 15:23:38 +0100 | [diff] [blame] | 9771 | iput(inode); |
| 9772 | |
Filipe Manana | c990161 | 2016-05-05 01:41:57 +0100 | [diff] [blame] | 9773 | return ret; |
Dan Fuhry | cdd1fed | 2016-03-17 15:23:38 +0100 | [diff] [blame] | 9774 | } |
| 9775 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 9776 | static int btrfs_rename(struct inode *old_dir, struct dentry *old_dentry, |
Dan Fuhry | cdd1fed | 2016-03-17 15:23:38 +0100 | [diff] [blame] | 9777 | struct inode *new_dir, struct dentry *new_dentry, |
| 9778 | unsigned int flags) |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 9779 | { |
| 9780 | struct btrfs_trans_handle *trans; |
Filipe Manana | 5062af3 | 2016-05-05 10:26:26 +0100 | [diff] [blame] | 9781 | unsigned int trans_num_items; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 9782 | struct btrfs_root *root = BTRFS_I(old_dir)->root; |
| 9783 | struct btrfs_root *dest = BTRFS_I(new_dir)->root; |
David Howells | 2b0143b | 2015-03-17 22:25:59 +0000 | [diff] [blame] | 9784 | struct inode *new_inode = d_inode(new_dentry); |
| 9785 | struct inode *old_inode = d_inode(old_dentry); |
Chris Mason | 00e4e6b | 2008-08-05 11:18:09 -0400 | [diff] [blame] | 9786 | u64 index = 0; |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 9787 | u64 root_objectid; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 9788 | int ret; |
Li Zefan | 33345d01 | 2011-04-20 10:31:50 +0800 | [diff] [blame] | 9789 | u64 old_ino = btrfs_ino(old_inode); |
Filipe Manana | 3dc9e8f | 2016-04-29 11:34:22 +0100 | [diff] [blame] | 9790 | bool log_pinned = false; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 9791 | |
Li Zefan | 33345d01 | 2011-04-20 10:31:50 +0800 | [diff] [blame] | 9792 | if (btrfs_ino(new_dir) == BTRFS_EMPTY_SUBVOL_DIR_OBJECTID) |
Yan, Zheng | f679a84 | 2009-09-24 09:17:31 -0400 | [diff] [blame] | 9793 | return -EPERM; |
| 9794 | |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 9795 | /* we only allow rename subvolume link between subvolumes */ |
Li Zefan | 33345d01 | 2011-04-20 10:31:50 +0800 | [diff] [blame] | 9796 | if (old_ino != BTRFS_FIRST_FREE_OBJECTID && root != dest) |
Chris Mason | 3394e16 | 2008-11-17 20:42:26 -0500 | [diff] [blame] | 9797 | return -EXDEV; |
| 9798 | |
Li Zefan | 33345d01 | 2011-04-20 10:31:50 +0800 | [diff] [blame] | 9799 | if (old_ino == BTRFS_EMPTY_SUBVOL_DIR_OBJECTID || |
| 9800 | (new_inode && btrfs_ino(new_inode) == BTRFS_FIRST_FREE_OBJECTID)) |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 9801 | return -ENOTEMPTY; |
| 9802 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 9803 | if (S_ISDIR(old_inode->i_mode) && new_inode && |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 9804 | new_inode->i_size > BTRFS_EMPTY_DIR_SIZE) |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 9805 | return -ENOTEMPTY; |
Chris Mason | 9c52057 | 2012-12-17 14:26:57 -0500 | [diff] [blame] | 9806 | |
| 9807 | |
| 9808 | /* check for collisions, even if the name isn't there */ |
Josef Bacik | 4871c15 | 2013-10-09 12:24:04 -0400 | [diff] [blame] | 9809 | ret = btrfs_check_dir_item_collision(dest, new_dir->i_ino, |
Chris Mason | 9c52057 | 2012-12-17 14:26:57 -0500 | [diff] [blame] | 9810 | new_dentry->d_name.name, |
| 9811 | new_dentry->d_name.len); |
| 9812 | |
| 9813 | if (ret) { |
| 9814 | if (ret == -EEXIST) { |
| 9815 | /* we shouldn't get |
| 9816 | * eexist without a new_inode */ |
Dulshani Gunawardhana | fae7f21 | 2013-10-31 10:30:08 +0530 | [diff] [blame] | 9817 | if (WARN_ON(!new_inode)) { |
Chris Mason | 9c52057 | 2012-12-17 14:26:57 -0500 | [diff] [blame] | 9818 | return ret; |
| 9819 | } |
| 9820 | } else { |
| 9821 | /* maybe -EOVERFLOW */ |
| 9822 | return ret; |
| 9823 | } |
| 9824 | } |
| 9825 | ret = 0; |
| 9826 | |
Chris Mason | 5a3f23d | 2009-03-31 13:27:11 -0400 | [diff] [blame] | 9827 | /* |
Chris Mason | 8d875f9 | 2014-08-12 10:47:42 -0700 | [diff] [blame] | 9828 | * we're using rename to replace one file with another. Start IO on it |
| 9829 | * 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] | 9830 | */ |
Chris Mason | 8d875f9 | 2014-08-12 10:47:42 -0700 | [diff] [blame] | 9831 | 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] | 9832 | filemap_flush(old_inode->i_mapping); |
| 9833 | |
Yan, Zheng | 76dda93 | 2009-09-21 16:00:26 -0400 | [diff] [blame] | 9834 | /* close the racy window with snapshot create/destroy ioctl */ |
Li Zefan | 33345d01 | 2011-04-20 10:31:50 +0800 | [diff] [blame] | 9835 | if (old_ino == BTRFS_FIRST_FREE_OBJECTID) |
Yan, Zheng | 76dda93 | 2009-09-21 16:00:26 -0400 | [diff] [blame] | 9836 | down_read(&root->fs_info->subvol_sem); |
Yan, Zheng | a22285a | 2010-05-16 10:48:46 -0400 | [diff] [blame] | 9837 | /* |
| 9838 | * We want to reserve the absolute worst case amount of items. So if |
| 9839 | * both inodes are subvols and we need to unlink them then that would |
| 9840 | * require 4 item modifications, but if they are both normal inodes it |
Dan Fuhry | cdd1fed | 2016-03-17 15:23:38 +0100 | [diff] [blame] | 9841 | * would require 5 item modifications, so we'll assume they are normal |
Yan, Zheng | a22285a | 2010-05-16 10:48:46 -0400 | [diff] [blame] | 9842 | * inodes. So 5 * 2 is 10, plus 1 for the new link, so 11 total items |
| 9843 | * should cover the worst case number of items we'll modify. |
Filipe Manana | 5062af3 | 2016-05-05 10:26:26 +0100 | [diff] [blame] | 9844 | * If our rename has the whiteout flag, we need more 5 units for the |
| 9845 | * new inode (1 inode item, 1 inode ref, 2 dir items and 1 xattr item |
| 9846 | * when selinux is enabled). |
Yan, Zheng | a22285a | 2010-05-16 10:48:46 -0400 | [diff] [blame] | 9847 | */ |
Filipe Manana | 5062af3 | 2016-05-05 10:26:26 +0100 | [diff] [blame] | 9848 | trans_num_items = 11; |
| 9849 | if (flags & RENAME_WHITEOUT) |
| 9850 | trans_num_items += 5; |
| 9851 | trans = btrfs_start_transaction(root, trans_num_items); |
Johann Lombardi | b44c59a | 2011-03-31 13:23:47 +0000 | [diff] [blame] | 9852 | if (IS_ERR(trans)) { |
Dan Fuhry | cdd1fed | 2016-03-17 15:23:38 +0100 | [diff] [blame] | 9853 | ret = PTR_ERR(trans); |
| 9854 | goto out_notrans; |
| 9855 | } |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 9856 | |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 9857 | if (dest != root) |
| 9858 | btrfs_record_root_in_trans(trans, dest); |
| 9859 | |
Yan, Zheng | a571952 | 2009-09-24 09:17:31 -0400 | [diff] [blame] | 9860 | ret = btrfs_set_inode_index(new_dir, &index); |
| 9861 | if (ret) |
| 9862 | goto out_fail; |
Chris Mason | 5a3f23d | 2009-03-31 13:27:11 -0400 | [diff] [blame] | 9863 | |
Miao Xie | 67de117 | 2013-12-26 13:07:06 +0800 | [diff] [blame] | 9864 | BTRFS_I(old_inode)->dir_index = 0ULL; |
Li Zefan | 33345d01 | 2011-04-20 10:31:50 +0800 | [diff] [blame] | 9865 | if (unlikely(old_ino == BTRFS_FIRST_FREE_OBJECTID)) { |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 9866 | /* force full log commit if subvolume involved. */ |
Miao Xie | 995946d | 2014-04-02 19:51:06 +0800 | [diff] [blame] | 9867 | btrfs_set_log_full_commit(root->fs_info, trans); |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 9868 | } else { |
Filipe Manana | c4aba95 | 2016-04-29 13:14:42 +0100 | [diff] [blame] | 9869 | btrfs_pin_log_trans(root); |
| 9870 | log_pinned = true; |
Yan, Zheng | a571952 | 2009-09-24 09:17:31 -0400 | [diff] [blame] | 9871 | ret = btrfs_insert_inode_ref(trans, dest, |
| 9872 | new_dentry->d_name.name, |
| 9873 | new_dentry->d_name.len, |
Li Zefan | 33345d01 | 2011-04-20 10:31:50 +0800 | [diff] [blame] | 9874 | old_ino, |
| 9875 | btrfs_ino(new_dir), index); |
Yan, Zheng | a571952 | 2009-09-24 09:17:31 -0400 | [diff] [blame] | 9876 | if (ret) |
| 9877 | goto out_fail; |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 9878 | } |
Chris Mason | 5a3f23d | 2009-03-31 13:27:11 -0400 | [diff] [blame] | 9879 | |
Josef Bacik | 0c4d2d9 | 2012-04-05 15:03:02 -0400 | [diff] [blame] | 9880 | inode_inc_iversion(old_dir); |
| 9881 | inode_inc_iversion(new_dir); |
| 9882 | inode_inc_iversion(old_inode); |
Deepa Dinamani | 04b285f | 2016-02-06 23:57:21 -0800 | [diff] [blame] | 9883 | old_dir->i_ctime = old_dir->i_mtime = |
| 9884 | new_dir->i_ctime = new_dir->i_mtime = |
Deepa Dinamani | c2050a4 | 2016-09-14 07:48:06 -0700 | [diff] [blame] | 9885 | old_inode->i_ctime = current_time(old_dir); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 9886 | |
Chris Mason | 12fcfd2 | 2009-03-24 10:24:20 -0400 | [diff] [blame] | 9887 | if (old_dentry->d_parent != new_dentry->d_parent) |
| 9888 | btrfs_record_unlink_dir(trans, old_dir, old_inode, 1); |
| 9889 | |
Li Zefan | 33345d01 | 2011-04-20 10:31:50 +0800 | [diff] [blame] | 9890 | if (unlikely(old_ino == BTRFS_FIRST_FREE_OBJECTID)) { |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 9891 | root_objectid = BTRFS_I(old_inode)->root->root_key.objectid; |
| 9892 | ret = btrfs_unlink_subvol(trans, root, old_dir, root_objectid, |
| 9893 | old_dentry->d_name.name, |
| 9894 | old_dentry->d_name.len); |
| 9895 | } else { |
Al Viro | 9298679 | 2011-03-04 17:14:37 +0000 | [diff] [blame] | 9896 | ret = __btrfs_unlink_inode(trans, root, old_dir, |
David Howells | 2b0143b | 2015-03-17 22:25:59 +0000 | [diff] [blame] | 9897 | d_inode(old_dentry), |
Al Viro | 9298679 | 2011-03-04 17:14:37 +0000 | [diff] [blame] | 9898 | old_dentry->d_name.name, |
| 9899 | old_dentry->d_name.len); |
| 9900 | if (!ret) |
| 9901 | ret = btrfs_update_inode(trans, root, old_inode); |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 9902 | } |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 9903 | if (ret) { |
Jeff Mahoney | 6664283 | 2016-06-10 18:19:25 -0400 | [diff] [blame] | 9904 | btrfs_abort_transaction(trans, ret); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 9905 | goto out_fail; |
| 9906 | } |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 9907 | |
| 9908 | if (new_inode) { |
Josef Bacik | 0c4d2d9 | 2012-04-05 15:03:02 -0400 | [diff] [blame] | 9909 | inode_inc_iversion(new_inode); |
Deepa Dinamani | c2050a4 | 2016-09-14 07:48:06 -0700 | [diff] [blame] | 9910 | new_inode->i_ctime = current_time(new_inode); |
Li Zefan | 33345d01 | 2011-04-20 10:31:50 +0800 | [diff] [blame] | 9911 | if (unlikely(btrfs_ino(new_inode) == |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 9912 | BTRFS_EMPTY_SUBVOL_DIR_OBJECTID)) { |
| 9913 | root_objectid = BTRFS_I(new_inode)->location.objectid; |
| 9914 | ret = btrfs_unlink_subvol(trans, dest, new_dir, |
| 9915 | root_objectid, |
| 9916 | new_dentry->d_name.name, |
| 9917 | new_dentry->d_name.len); |
| 9918 | BUG_ON(new_inode->i_nlink == 0); |
| 9919 | } else { |
| 9920 | ret = btrfs_unlink_inode(trans, dest, new_dir, |
David Howells | 2b0143b | 2015-03-17 22:25:59 +0000 | [diff] [blame] | 9921 | d_inode(new_dentry), |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 9922 | new_dentry->d_name.name, |
| 9923 | new_dentry->d_name.len); |
| 9924 | } |
Josef Bacik | 4ef31a4 | 2013-08-13 14:10:08 -0400 | [diff] [blame] | 9925 | if (!ret && new_inode->i_nlink == 0) |
David Howells | 2b0143b | 2015-03-17 22:25:59 +0000 | [diff] [blame] | 9926 | ret = btrfs_orphan_add(trans, d_inode(new_dentry)); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 9927 | if (ret) { |
Jeff Mahoney | 6664283 | 2016-06-10 18:19:25 -0400 | [diff] [blame] | 9928 | btrfs_abort_transaction(trans, ret); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 9929 | goto out_fail; |
| 9930 | } |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 9931 | } |
Josef Bacik | aec7477 | 2008-07-24 12:12:38 -0400 | [diff] [blame] | 9932 | |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 9933 | ret = btrfs_add_link(trans, new_dir, old_inode, |
| 9934 | new_dentry->d_name.name, |
Yan, Zheng | a571952 | 2009-09-24 09:17:31 -0400 | [diff] [blame] | 9935 | new_dentry->d_name.len, 0, index); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 9936 | if (ret) { |
Jeff Mahoney | 6664283 | 2016-06-10 18:19:25 -0400 | [diff] [blame] | 9937 | btrfs_abort_transaction(trans, ret); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 9938 | goto out_fail; |
| 9939 | } |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 9940 | |
Miao Xie | 67de117 | 2013-12-26 13:07:06 +0800 | [diff] [blame] | 9941 | if (old_inode->i_nlink == 1) |
| 9942 | BTRFS_I(old_inode)->dir_index = index; |
| 9943 | |
Filipe Manana | 3dc9e8f | 2016-04-29 11:34:22 +0100 | [diff] [blame] | 9944 | if (log_pinned) { |
Al Viro | 10d9f30 | 2011-07-16 23:09:10 -0400 | [diff] [blame] | 9945 | struct dentry *parent = new_dentry->d_parent; |
Filipe Manana | 3dc9e8f | 2016-04-29 11:34:22 +0100 | [diff] [blame] | 9946 | |
Josef Bacik | 6a91221 | 2010-11-20 09:48:00 +0000 | [diff] [blame] | 9947 | btrfs_log_new_name(trans, old_inode, old_dir, parent); |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 9948 | btrfs_end_log_trans(root); |
Filipe Manana | 3dc9e8f | 2016-04-29 11:34:22 +0100 | [diff] [blame] | 9949 | log_pinned = false; |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 9950 | } |
Dan Fuhry | cdd1fed | 2016-03-17 15:23:38 +0100 | [diff] [blame] | 9951 | |
| 9952 | if (flags & RENAME_WHITEOUT) { |
| 9953 | ret = btrfs_whiteout_for_rename(trans, root, old_dir, |
| 9954 | old_dentry); |
| 9955 | |
| 9956 | if (ret) { |
Jeff Mahoney | 6664283 | 2016-06-10 18:19:25 -0400 | [diff] [blame] | 9957 | btrfs_abort_transaction(trans, ret); |
Dan Fuhry | cdd1fed | 2016-03-17 15:23:38 +0100 | [diff] [blame] | 9958 | goto out_fail; |
| 9959 | } |
Chris Mason | 12fcfd2 | 2009-03-24 10:24:20 -0400 | [diff] [blame] | 9960 | } |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 9961 | out_fail: |
Filipe Manana | 3dc9e8f | 2016-04-29 11:34:22 +0100 | [diff] [blame] | 9962 | /* |
| 9963 | * If we have pinned the log and an error happened, we unpin tasks |
| 9964 | * trying to sync the log and force them to fallback to a transaction |
| 9965 | * commit if the log currently contains any of the inodes involved in |
| 9966 | * this rename operation (to ensure we do not persist a log with an |
| 9967 | * inconsistent state for any of these inodes or leading to any |
| 9968 | * inconsistencies when replayed). If the transaction was aborted, the |
| 9969 | * abortion reason is propagated to userspace when attempting to commit |
| 9970 | * the transaction. If the log does not contain any of these inodes, we |
| 9971 | * allow the tasks to sync it. |
| 9972 | */ |
| 9973 | if (ret && log_pinned) { |
| 9974 | if (btrfs_inode_in_log(old_dir, root->fs_info->generation) || |
| 9975 | btrfs_inode_in_log(new_dir, root->fs_info->generation) || |
| 9976 | btrfs_inode_in_log(old_inode, root->fs_info->generation) || |
| 9977 | (new_inode && |
| 9978 | btrfs_inode_in_log(new_inode, root->fs_info->generation))) |
| 9979 | btrfs_set_log_full_commit(root->fs_info, trans); |
| 9980 | |
| 9981 | btrfs_end_log_trans(root); |
| 9982 | log_pinned = false; |
| 9983 | } |
Josef Bacik | 7ad85bb | 2012-01-12 19:10:12 -0500 | [diff] [blame] | 9984 | btrfs_end_transaction(trans, root); |
Johann Lombardi | b44c59a | 2011-03-31 13:23:47 +0000 | [diff] [blame] | 9985 | out_notrans: |
Li Zefan | 33345d01 | 2011-04-20 10:31:50 +0800 | [diff] [blame] | 9986 | if (old_ino == BTRFS_FIRST_FREE_OBJECTID) |
Yan, Zheng | 76dda93 | 2009-09-21 16:00:26 -0400 | [diff] [blame] | 9987 | up_read(&root->fs_info->subvol_sem); |
Josef Bacik | 9ed74f2 | 2009-09-11 16:12:44 -0400 | [diff] [blame] | 9988 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 9989 | return ret; |
| 9990 | } |
| 9991 | |
Miklos Szeredi | 80ace85 | 2014-07-23 15:15:32 +0200 | [diff] [blame] | 9992 | static int btrfs_rename2(struct inode *old_dir, struct dentry *old_dentry, |
| 9993 | struct inode *new_dir, struct dentry *new_dentry, |
| 9994 | unsigned int flags) |
| 9995 | { |
Dan Fuhry | cdd1fed | 2016-03-17 15:23:38 +0100 | [diff] [blame] | 9996 | if (flags & ~(RENAME_NOREPLACE | RENAME_EXCHANGE | RENAME_WHITEOUT)) |
Miklos Szeredi | 80ace85 | 2014-07-23 15:15:32 +0200 | [diff] [blame] | 9997 | return -EINVAL; |
| 9998 | |
Dan Fuhry | cdd1fed | 2016-03-17 15:23:38 +0100 | [diff] [blame] | 9999 | if (flags & RENAME_EXCHANGE) |
| 10000 | return btrfs_rename_exchange(old_dir, old_dentry, new_dir, |
| 10001 | new_dentry); |
| 10002 | |
| 10003 | return btrfs_rename(old_dir, old_dentry, new_dir, new_dentry, flags); |
Miklos Szeredi | 80ace85 | 2014-07-23 15:15:32 +0200 | [diff] [blame] | 10004 | } |
| 10005 | |
Miao Xie | 8ccf6f19 | 2012-10-25 09:28:04 +0000 | [diff] [blame] | 10006 | static void btrfs_run_delalloc_work(struct btrfs_work *work) |
| 10007 | { |
| 10008 | struct btrfs_delalloc_work *delalloc_work; |
Josef Bacik | 9f23e28 | 2013-10-28 15:03:41 -0400 | [diff] [blame] | 10009 | struct inode *inode; |
Miao Xie | 8ccf6f19 | 2012-10-25 09:28:04 +0000 | [diff] [blame] | 10010 | |
| 10011 | delalloc_work = container_of(work, struct btrfs_delalloc_work, |
| 10012 | work); |
Josef Bacik | 9f23e28 | 2013-10-28 15:03:41 -0400 | [diff] [blame] | 10013 | inode = delalloc_work->inode; |
David Sterba | 3042460 | 2015-11-27 19:27:11 +0100 | [diff] [blame] | 10014 | filemap_flush(inode->i_mapping); |
| 10015 | if (test_bit(BTRFS_INODE_HAS_ASYNC_EXTENT, |
| 10016 | &BTRFS_I(inode)->runtime_flags)) |
Josef Bacik | 9f23e28 | 2013-10-28 15:03:41 -0400 | [diff] [blame] | 10017 | filemap_flush(inode->i_mapping); |
Miao Xie | 8ccf6f19 | 2012-10-25 09:28:04 +0000 | [diff] [blame] | 10018 | |
| 10019 | if (delalloc_work->delay_iput) |
Josef Bacik | 9f23e28 | 2013-10-28 15:03:41 -0400 | [diff] [blame] | 10020 | btrfs_add_delayed_iput(inode); |
Miao Xie | 8ccf6f19 | 2012-10-25 09:28:04 +0000 | [diff] [blame] | 10021 | else |
Josef Bacik | 9f23e28 | 2013-10-28 15:03:41 -0400 | [diff] [blame] | 10022 | iput(inode); |
Miao Xie | 8ccf6f19 | 2012-10-25 09:28:04 +0000 | [diff] [blame] | 10023 | complete(&delalloc_work->completion); |
| 10024 | } |
| 10025 | |
| 10026 | struct btrfs_delalloc_work *btrfs_alloc_delalloc_work(struct inode *inode, |
David Sterba | 651d494 | 2015-11-27 19:24:16 +0100 | [diff] [blame] | 10027 | int delay_iput) |
Miao Xie | 8ccf6f19 | 2012-10-25 09:28:04 +0000 | [diff] [blame] | 10028 | { |
| 10029 | struct btrfs_delalloc_work *work; |
| 10030 | |
David Sterba | 100d570 | 2015-12-08 14:39:32 +0100 | [diff] [blame] | 10031 | work = kmalloc(sizeof(*work), GFP_NOFS); |
Miao Xie | 8ccf6f19 | 2012-10-25 09:28:04 +0000 | [diff] [blame] | 10032 | if (!work) |
| 10033 | return NULL; |
| 10034 | |
| 10035 | init_completion(&work->completion); |
| 10036 | INIT_LIST_HEAD(&work->list); |
| 10037 | work->inode = inode; |
Miao Xie | 8ccf6f19 | 2012-10-25 09:28:04 +0000 | [diff] [blame] | 10038 | work->delay_iput = delay_iput; |
Liu Bo | 9e0af23 | 2014-08-15 23:36:53 +0800 | [diff] [blame] | 10039 | WARN_ON_ONCE(!inode); |
| 10040 | btrfs_init_work(&work->work, btrfs_flush_delalloc_helper, |
| 10041 | btrfs_run_delalloc_work, NULL, NULL); |
Miao Xie | 8ccf6f19 | 2012-10-25 09:28:04 +0000 | [diff] [blame] | 10042 | |
| 10043 | return work; |
| 10044 | } |
| 10045 | |
| 10046 | void btrfs_wait_and_free_delalloc_work(struct btrfs_delalloc_work *work) |
| 10047 | { |
| 10048 | wait_for_completion(&work->completion); |
David Sterba | 100d570 | 2015-12-08 14:39:32 +0100 | [diff] [blame] | 10049 | kfree(work); |
Miao Xie | 8ccf6f19 | 2012-10-25 09:28:04 +0000 | [diff] [blame] | 10050 | } |
| 10051 | |
Chris Mason | d352ac6 | 2008-09-29 15:18:18 -0400 | [diff] [blame] | 10052 | /* |
| 10053 | * some fairly slow code that needs optimization. This walks the list |
| 10054 | * of all the inodes with pending delalloc and forces them to disk. |
| 10055 | */ |
Miao Xie | 6c255e6 | 2014-03-06 13:55:01 +0800 | [diff] [blame] | 10056 | static int __start_delalloc_inodes(struct btrfs_root *root, int delay_iput, |
| 10057 | int nr) |
Chris Mason | ea8c281 | 2008-08-04 23:17:27 -0400 | [diff] [blame] | 10058 | { |
Chris Mason | ea8c281 | 2008-08-04 23:17:27 -0400 | [diff] [blame] | 10059 | struct btrfs_inode *binode; |
Zheng Yan | 5b21f2e | 2008-09-26 10:05:38 -0400 | [diff] [blame] | 10060 | struct inode *inode; |
Miao Xie | 8ccf6f19 | 2012-10-25 09:28:04 +0000 | [diff] [blame] | 10061 | struct btrfs_delalloc_work *work, *next; |
| 10062 | struct list_head works; |
Miao Xie | 1eafa6c | 2013-01-22 10:49:00 +0000 | [diff] [blame] | 10063 | struct list_head splice; |
Miao Xie | 8ccf6f19 | 2012-10-25 09:28:04 +0000 | [diff] [blame] | 10064 | int ret = 0; |
Chris Mason | ea8c281 | 2008-08-04 23:17:27 -0400 | [diff] [blame] | 10065 | |
Miao Xie | 8ccf6f19 | 2012-10-25 09:28:04 +0000 | [diff] [blame] | 10066 | INIT_LIST_HEAD(&works); |
Miao Xie | 1eafa6c | 2013-01-22 10:49:00 +0000 | [diff] [blame] | 10067 | INIT_LIST_HEAD(&splice); |
Miao Xie | 63607cc | 2013-01-22 10:50:35 +0000 | [diff] [blame] | 10068 | |
Miao Xie | 573bfb7 | 2014-03-06 13:55:03 +0800 | [diff] [blame] | 10069 | mutex_lock(&root->delalloc_mutex); |
Miao Xie | eb73c1b | 2013-05-15 07:48:22 +0000 | [diff] [blame] | 10070 | spin_lock(&root->delalloc_lock); |
| 10071 | list_splice_init(&root->delalloc_inodes, &splice); |
Miao Xie | 1eafa6c | 2013-01-22 10:49:00 +0000 | [diff] [blame] | 10072 | while (!list_empty(&splice)) { |
| 10073 | binode = list_entry(splice.next, struct btrfs_inode, |
Chris Mason | ea8c281 | 2008-08-04 23:17:27 -0400 | [diff] [blame] | 10074 | delalloc_inodes); |
Miao Xie | 1eafa6c | 2013-01-22 10:49:00 +0000 | [diff] [blame] | 10075 | |
Miao Xie | eb73c1b | 2013-05-15 07:48:22 +0000 | [diff] [blame] | 10076 | list_move_tail(&binode->delalloc_inodes, |
| 10077 | &root->delalloc_inodes); |
Zheng Yan | 5b21f2e | 2008-09-26 10:05:38 -0400 | [diff] [blame] | 10078 | inode = igrab(&binode->vfs_inode); |
Miao Xie | df0af1a | 2013-01-29 10:11:59 +0000 | [diff] [blame] | 10079 | if (!inode) { |
Miao Xie | eb73c1b | 2013-05-15 07:48:22 +0000 | [diff] [blame] | 10080 | cond_resched_lock(&root->delalloc_lock); |
Miao Xie | 1eafa6c | 2013-01-22 10:49:00 +0000 | [diff] [blame] | 10081 | continue; |
Miao Xie | df0af1a | 2013-01-29 10:11:59 +0000 | [diff] [blame] | 10082 | } |
Miao Xie | eb73c1b | 2013-05-15 07:48:22 +0000 | [diff] [blame] | 10083 | spin_unlock(&root->delalloc_lock); |
Miao Xie | 1eafa6c | 2013-01-22 10:49:00 +0000 | [diff] [blame] | 10084 | |
David Sterba | 651d494 | 2015-11-27 19:24:16 +0100 | [diff] [blame] | 10085 | work = btrfs_alloc_delalloc_work(inode, delay_iput); |
David Sterba | 5d99a998 | 2014-09-29 19:20:37 +0200 | [diff] [blame] | 10086 | if (!work) { |
Josef Bacik | f4ab9ea | 2013-09-17 11:25:44 -0400 | [diff] [blame] | 10087 | if (delay_iput) |
| 10088 | btrfs_add_delayed_iput(inode); |
| 10089 | else |
| 10090 | iput(inode); |
Miao Xie | 1eafa6c | 2013-01-22 10:49:00 +0000 | [diff] [blame] | 10091 | ret = -ENOMEM; |
Wang Shilong | a1ecaab | 2014-04-02 19:53:32 +0800 | [diff] [blame] | 10092 | goto out; |
Zheng Yan | 5b21f2e | 2008-09-26 10:05:38 -0400 | [diff] [blame] | 10093 | } |
Miao Xie | 1eafa6c | 2013-01-22 10:49:00 +0000 | [diff] [blame] | 10094 | list_add_tail(&work->list, &works); |
Qu Wenruo | a44903a | 2014-02-28 10:46:09 +0800 | [diff] [blame] | 10095 | btrfs_queue_work(root->fs_info->flush_workers, |
| 10096 | &work->work); |
Miao Xie | 6c255e6 | 2014-03-06 13:55:01 +0800 | [diff] [blame] | 10097 | ret++; |
| 10098 | if (nr != -1 && ret >= nr) |
Wang Shilong | a1ecaab | 2014-04-02 19:53:32 +0800 | [diff] [blame] | 10099 | goto out; |
Zheng Yan | 5b21f2e | 2008-09-26 10:05:38 -0400 | [diff] [blame] | 10100 | cond_resched(); |
Miao Xie | eb73c1b | 2013-05-15 07:48:22 +0000 | [diff] [blame] | 10101 | spin_lock(&root->delalloc_lock); |
Chris Mason | ea8c281 | 2008-08-04 23:17:27 -0400 | [diff] [blame] | 10102 | } |
Miao Xie | eb73c1b | 2013-05-15 07:48:22 +0000 | [diff] [blame] | 10103 | spin_unlock(&root->delalloc_lock); |
Chris Mason | 8c8bee1 | 2008-09-29 11:19:10 -0400 | [diff] [blame] | 10104 | |
Wang Shilong | a1ecaab | 2014-04-02 19:53:32 +0800 | [diff] [blame] | 10105 | out: |
Miao Xie | 1eafa6c | 2013-01-22 10:49:00 +0000 | [diff] [blame] | 10106 | list_for_each_entry_safe(work, next, &works, list) { |
| 10107 | list_del_init(&work->list); |
| 10108 | btrfs_wait_and_free_delalloc_work(work); |
| 10109 | } |
| 10110 | |
Miao Xie | eb73c1b | 2013-05-15 07:48:22 +0000 | [diff] [blame] | 10111 | if (!list_empty_careful(&splice)) { |
| 10112 | spin_lock(&root->delalloc_lock); |
| 10113 | list_splice_tail(&splice, &root->delalloc_inodes); |
| 10114 | spin_unlock(&root->delalloc_lock); |
| 10115 | } |
Miao Xie | 573bfb7 | 2014-03-06 13:55:03 +0800 | [diff] [blame] | 10116 | mutex_unlock(&root->delalloc_mutex); |
Miao Xie | eb73c1b | 2013-05-15 07:48:22 +0000 | [diff] [blame] | 10117 | return ret; |
| 10118 | } |
| 10119 | |
| 10120 | int btrfs_start_delalloc_inodes(struct btrfs_root *root, int delay_iput) |
| 10121 | { |
| 10122 | int ret; |
| 10123 | |
Wang Shilong | 2c21b4d | 2014-01-14 19:42:20 +0800 | [diff] [blame] | 10124 | if (test_bit(BTRFS_FS_STATE_ERROR, &root->fs_info->fs_state)) |
Miao Xie | eb73c1b | 2013-05-15 07:48:22 +0000 | [diff] [blame] | 10125 | return -EROFS; |
| 10126 | |
Miao Xie | 6c255e6 | 2014-03-06 13:55:01 +0800 | [diff] [blame] | 10127 | ret = __start_delalloc_inodes(root, delay_iput, -1); |
| 10128 | if (ret > 0) |
| 10129 | ret = 0; |
Miao Xie | eb73c1b | 2013-05-15 07:48:22 +0000 | [diff] [blame] | 10130 | /* |
| 10131 | * the filemap_flush will queue IO into the worker threads, but |
Chris Mason | 8c8bee1 | 2008-09-29 11:19:10 -0400 | [diff] [blame] | 10132 | * we have to make sure the IO is actually started and that |
| 10133 | * ordered extents get created before we return |
| 10134 | */ |
| 10135 | atomic_inc(&root->fs_info->async_submit_draining); |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 10136 | while (atomic_read(&root->fs_info->nr_async_submits) || |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 10137 | atomic_read(&root->fs_info->async_delalloc_pages)) { |
Chris Mason | 8c8bee1 | 2008-09-29 11:19:10 -0400 | [diff] [blame] | 10138 | wait_event(root->fs_info->async_submit_wait, |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 10139 | (atomic_read(&root->fs_info->nr_async_submits) == 0 && |
| 10140 | atomic_read(&root->fs_info->async_delalloc_pages) == 0)); |
Chris Mason | 8c8bee1 | 2008-09-29 11:19:10 -0400 | [diff] [blame] | 10141 | } |
| 10142 | atomic_dec(&root->fs_info->async_submit_draining); |
Miao Xie | eb73c1b | 2013-05-15 07:48:22 +0000 | [diff] [blame] | 10143 | return ret; |
| 10144 | } |
| 10145 | |
Miao Xie | 6c255e6 | 2014-03-06 13:55:01 +0800 | [diff] [blame] | 10146 | int btrfs_start_delalloc_roots(struct btrfs_fs_info *fs_info, int delay_iput, |
| 10147 | int nr) |
Miao Xie | eb73c1b | 2013-05-15 07:48:22 +0000 | [diff] [blame] | 10148 | { |
| 10149 | struct btrfs_root *root; |
| 10150 | struct list_head splice; |
| 10151 | int ret; |
| 10152 | |
Wang Shilong | 2c21b4d | 2014-01-14 19:42:20 +0800 | [diff] [blame] | 10153 | if (test_bit(BTRFS_FS_STATE_ERROR, &fs_info->fs_state)) |
Miao Xie | eb73c1b | 2013-05-15 07:48:22 +0000 | [diff] [blame] | 10154 | return -EROFS; |
| 10155 | |
| 10156 | INIT_LIST_HEAD(&splice); |
| 10157 | |
Miao Xie | 573bfb7 | 2014-03-06 13:55:03 +0800 | [diff] [blame] | 10158 | mutex_lock(&fs_info->delalloc_root_mutex); |
Miao Xie | eb73c1b | 2013-05-15 07:48:22 +0000 | [diff] [blame] | 10159 | spin_lock(&fs_info->delalloc_root_lock); |
| 10160 | list_splice_init(&fs_info->delalloc_roots, &splice); |
Miao Xie | 6c255e6 | 2014-03-06 13:55:01 +0800 | [diff] [blame] | 10161 | while (!list_empty(&splice) && nr) { |
Miao Xie | eb73c1b | 2013-05-15 07:48:22 +0000 | [diff] [blame] | 10162 | root = list_first_entry(&splice, struct btrfs_root, |
| 10163 | delalloc_root); |
| 10164 | root = btrfs_grab_fs_root(root); |
| 10165 | BUG_ON(!root); |
| 10166 | list_move_tail(&root->delalloc_root, |
| 10167 | &fs_info->delalloc_roots); |
| 10168 | spin_unlock(&fs_info->delalloc_root_lock); |
| 10169 | |
Miao Xie | 6c255e6 | 2014-03-06 13:55:01 +0800 | [diff] [blame] | 10170 | ret = __start_delalloc_inodes(root, delay_iput, nr); |
Miao Xie | eb73c1b | 2013-05-15 07:48:22 +0000 | [diff] [blame] | 10171 | btrfs_put_fs_root(root); |
Miao Xie | 6c255e6 | 2014-03-06 13:55:01 +0800 | [diff] [blame] | 10172 | if (ret < 0) |
Miao Xie | eb73c1b | 2013-05-15 07:48:22 +0000 | [diff] [blame] | 10173 | goto out; |
| 10174 | |
Miao Xie | 6c255e6 | 2014-03-06 13:55:01 +0800 | [diff] [blame] | 10175 | if (nr != -1) { |
| 10176 | nr -= ret; |
| 10177 | WARN_ON(nr < 0); |
| 10178 | } |
Miao Xie | eb73c1b | 2013-05-15 07:48:22 +0000 | [diff] [blame] | 10179 | spin_lock(&fs_info->delalloc_root_lock); |
| 10180 | } |
| 10181 | spin_unlock(&fs_info->delalloc_root_lock); |
| 10182 | |
Miao Xie | 6c255e6 | 2014-03-06 13:55:01 +0800 | [diff] [blame] | 10183 | ret = 0; |
Miao Xie | eb73c1b | 2013-05-15 07:48:22 +0000 | [diff] [blame] | 10184 | atomic_inc(&fs_info->async_submit_draining); |
| 10185 | while (atomic_read(&fs_info->nr_async_submits) || |
| 10186 | atomic_read(&fs_info->async_delalloc_pages)) { |
| 10187 | wait_event(fs_info->async_submit_wait, |
| 10188 | (atomic_read(&fs_info->nr_async_submits) == 0 && |
| 10189 | atomic_read(&fs_info->async_delalloc_pages) == 0)); |
| 10190 | } |
| 10191 | atomic_dec(&fs_info->async_submit_draining); |
Miao Xie | 8ccf6f19 | 2012-10-25 09:28:04 +0000 | [diff] [blame] | 10192 | out: |
Miao Xie | 1eafa6c | 2013-01-22 10:49:00 +0000 | [diff] [blame] | 10193 | if (!list_empty_careful(&splice)) { |
Miao Xie | eb73c1b | 2013-05-15 07:48:22 +0000 | [diff] [blame] | 10194 | spin_lock(&fs_info->delalloc_root_lock); |
| 10195 | list_splice_tail(&splice, &fs_info->delalloc_roots); |
| 10196 | spin_unlock(&fs_info->delalloc_root_lock); |
Miao Xie | 1eafa6c | 2013-01-22 10:49:00 +0000 | [diff] [blame] | 10197 | } |
Miao Xie | 573bfb7 | 2014-03-06 13:55:03 +0800 | [diff] [blame] | 10198 | mutex_unlock(&fs_info->delalloc_root_mutex); |
Miao Xie | 8ccf6f19 | 2012-10-25 09:28:04 +0000 | [diff] [blame] | 10199 | return ret; |
Chris Mason | ea8c281 | 2008-08-04 23:17:27 -0400 | [diff] [blame] | 10200 | } |
| 10201 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 10202 | static int btrfs_symlink(struct inode *dir, struct dentry *dentry, |
| 10203 | const char *symname) |
| 10204 | { |
| 10205 | struct btrfs_trans_handle *trans; |
| 10206 | struct btrfs_root *root = BTRFS_I(dir)->root; |
| 10207 | struct btrfs_path *path; |
| 10208 | struct btrfs_key key; |
Chris Mason | 1832a6d | 2007-12-21 16:27:21 -0500 | [diff] [blame] | 10209 | struct inode *inode = NULL; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 10210 | int err; |
| 10211 | int drop_inode = 0; |
| 10212 | u64 objectid; |
Dulshani Gunawardhana | 6787125 | 2013-10-31 10:33:04 +0530 | [diff] [blame] | 10213 | u64 index = 0; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 10214 | int name_len; |
| 10215 | int datasize; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 10216 | unsigned long ptr; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 10217 | struct btrfs_file_extent_item *ei; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 10218 | struct extent_buffer *leaf; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 10219 | |
Filipe David Borba Manana | f06becc | 2013-09-16 09:53:28 +0100 | [diff] [blame] | 10220 | name_len = strlen(symname); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 10221 | if (name_len > BTRFS_MAX_INLINE_DATA_SIZE(root)) |
| 10222 | return -ENAMETOOLONG; |
Chris Mason | 1832a6d | 2007-12-21 16:27:21 -0500 | [diff] [blame] | 10223 | |
Josef Bacik | 9ed74f2 | 2009-09-11 16:12:44 -0400 | [diff] [blame] | 10224 | /* |
| 10225 | * 2 items for inode item and ref |
| 10226 | * 2 items for dir items |
Filipe Manana | 9269d12 | 2015-12-31 18:16:29 +0000 | [diff] [blame] | 10227 | * 1 item for updating parent inode item |
| 10228 | * 1 item for the inline extent item |
Josef Bacik | 9ed74f2 | 2009-09-11 16:12:44 -0400 | [diff] [blame] | 10229 | * 1 item for xattr if selinux is on |
| 10230 | */ |
Filipe Manana | 9269d12 | 2015-12-31 18:16:29 +0000 | [diff] [blame] | 10231 | trans = btrfs_start_transaction(root, 7); |
Yan, Zheng | a22285a | 2010-05-16 10:48:46 -0400 | [diff] [blame] | 10232 | if (IS_ERR(trans)) |
| 10233 | return PTR_ERR(trans); |
Chris Mason | 1832a6d | 2007-12-21 16:27:21 -0500 | [diff] [blame] | 10234 | |
Li Zefan | 581bb05 | 2011-04-20 10:06:11 +0800 | [diff] [blame] | 10235 | err = btrfs_find_free_ino(root, &objectid); |
| 10236 | if (err) |
| 10237 | goto out_unlock; |
| 10238 | |
Josef Bacik | aec7477 | 2008-07-24 12:12:38 -0400 | [diff] [blame] | 10239 | inode = btrfs_new_inode(trans, root, dir, dentry->d_name.name, |
Li Zefan | 33345d01 | 2011-04-20 10:31:50 +0800 | [diff] [blame] | 10240 | dentry->d_name.len, btrfs_ino(dir), objectid, |
Josef Bacik | d82a6f1 | 2011-05-11 15:26:06 -0400 | [diff] [blame] | 10241 | S_IFLNK|S_IRWXUGO, &index); |
Tsutomu Itoh | 7cf96da | 2011-04-25 19:43:53 -0400 | [diff] [blame] | 10242 | if (IS_ERR(inode)) { |
| 10243 | err = PTR_ERR(inode); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 10244 | goto out_unlock; |
Tsutomu Itoh | 7cf96da | 2011-04-25 19:43:53 -0400 | [diff] [blame] | 10245 | } |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 10246 | |
Casey Schaufler | ad19db7 | 2011-12-15 10:09:07 -0500 | [diff] [blame] | 10247 | /* |
| 10248 | * If the active LSM wants to access the inode during |
| 10249 | * d_instantiate it needs these. Smack checks to see |
| 10250 | * if the filesystem supports xattrs by looking at the |
| 10251 | * ops vector. |
| 10252 | */ |
| 10253 | inode->i_fop = &btrfs_file_operations; |
| 10254 | inode->i_op = &btrfs_file_inode_operations; |
Chris Mason | b0d5d10 | 2014-09-08 13:08:51 -0700 | [diff] [blame] | 10255 | inode->i_mapping->a_ops = &btrfs_aops; |
Chris Mason | b0d5d10 | 2014-09-08 13:08:51 -0700 | [diff] [blame] | 10256 | BTRFS_I(inode)->io_tree.ops = &btrfs_extent_io_ops; |
| 10257 | |
| 10258 | err = btrfs_init_inode_security(trans, inode, dir, &dentry->d_name); |
| 10259 | if (err) |
| 10260 | goto out_unlock_inode; |
Casey Schaufler | ad19db7 | 2011-12-15 10:09:07 -0500 | [diff] [blame] | 10261 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 10262 | path = btrfs_alloc_path(); |
Mark Fasheh | d8926bb | 2011-07-13 10:38:47 -0700 | [diff] [blame] | 10263 | if (!path) { |
| 10264 | err = -ENOMEM; |
Chris Mason | b0d5d10 | 2014-09-08 13:08:51 -0700 | [diff] [blame] | 10265 | goto out_unlock_inode; |
Mark Fasheh | d8926bb | 2011-07-13 10:38:47 -0700 | [diff] [blame] | 10266 | } |
Li Zefan | 33345d01 | 2011-04-20 10:31:50 +0800 | [diff] [blame] | 10267 | key.objectid = btrfs_ino(inode); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 10268 | key.offset = 0; |
David Sterba | 962a298 | 2014-06-04 18:41:45 +0200 | [diff] [blame] | 10269 | key.type = BTRFS_EXTENT_DATA_KEY; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 10270 | datasize = btrfs_file_extent_calc_inline_size(name_len); |
| 10271 | err = btrfs_insert_empty_item(trans, root, path, &key, |
| 10272 | datasize); |
Chris Mason | 54aa1f4 | 2007-06-22 14:16:25 -0400 | [diff] [blame] | 10273 | if (err) { |
Julia Lawall | b083916 | 2011-05-14 07:10:51 +0000 | [diff] [blame] | 10274 | btrfs_free_path(path); |
Chris Mason | b0d5d10 | 2014-09-08 13:08:51 -0700 | [diff] [blame] | 10275 | goto out_unlock_inode; |
Chris Mason | 54aa1f4 | 2007-06-22 14:16:25 -0400 | [diff] [blame] | 10276 | } |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 10277 | leaf = path->nodes[0]; |
| 10278 | ei = btrfs_item_ptr(leaf, path->slots[0], |
| 10279 | struct btrfs_file_extent_item); |
| 10280 | btrfs_set_file_extent_generation(leaf, ei, trans->transid); |
| 10281 | btrfs_set_file_extent_type(leaf, ei, |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 10282 | BTRFS_FILE_EXTENT_INLINE); |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 10283 | btrfs_set_file_extent_encryption(leaf, ei, 0); |
| 10284 | btrfs_set_file_extent_compression(leaf, ei, 0); |
| 10285 | btrfs_set_file_extent_other_encoding(leaf, ei, 0); |
| 10286 | btrfs_set_file_extent_ram_bytes(leaf, ei, name_len); |
| 10287 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 10288 | ptr = btrfs_file_extent_inline_start(ei); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 10289 | write_extent_buffer(leaf, symname, ptr, name_len); |
| 10290 | btrfs_mark_buffer_dirty(leaf); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 10291 | btrfs_free_path(path); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 10292 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 10293 | inode->i_op = &btrfs_symlink_inode_operations; |
Al Viro | 21fc61c | 2015-11-17 01:07:57 -0500 | [diff] [blame] | 10294 | inode_nohighmem(inode); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 10295 | inode->i_mapping->a_ops = &btrfs_symlink_aops; |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 10296 | inode_set_bytes(inode, name_len); |
Filipe David Borba Manana | f06becc | 2013-09-16 09:53:28 +0100 | [diff] [blame] | 10297 | btrfs_i_size_write(inode, name_len); |
Chris Mason | 54aa1f4 | 2007-06-22 14:16:25 -0400 | [diff] [blame] | 10298 | err = btrfs_update_inode(trans, root, inode); |
Filipe Manana | d50866d | 2015-12-31 18:08:24 +0000 | [diff] [blame] | 10299 | /* |
| 10300 | * Last step, add directory indexes for our symlink inode. This is the |
| 10301 | * last step to avoid extra cleanup of these indexes if an error happens |
| 10302 | * elsewhere above. |
| 10303 | */ |
| 10304 | if (!err) |
| 10305 | err = btrfs_add_nondir(trans, dir, dentry, inode, 0, index); |
Chris Mason | b0d5d10 | 2014-09-08 13:08:51 -0700 | [diff] [blame] | 10306 | if (err) { |
Chris Mason | 54aa1f4 | 2007-06-22 14:16:25 -0400 | [diff] [blame] | 10307 | drop_inode = 1; |
Chris Mason | b0d5d10 | 2014-09-08 13:08:51 -0700 | [diff] [blame] | 10308 | goto out_unlock_inode; |
| 10309 | } |
| 10310 | |
| 10311 | unlock_new_inode(inode); |
| 10312 | d_instantiate(dentry, inode); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 10313 | |
| 10314 | out_unlock: |
Josef Bacik | 7ad85bb | 2012-01-12 19:10:12 -0500 | [diff] [blame] | 10315 | btrfs_end_transaction(trans, root); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 10316 | if (drop_inode) { |
| 10317 | inode_dec_link_count(inode); |
| 10318 | iput(inode); |
| 10319 | } |
Liu Bo | b53d3f5 | 2012-11-14 14:34:34 +0000 | [diff] [blame] | 10320 | btrfs_btree_balance_dirty(root); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 10321 | return err; |
Chris Mason | b0d5d10 | 2014-09-08 13:08:51 -0700 | [diff] [blame] | 10322 | |
| 10323 | out_unlock_inode: |
| 10324 | drop_inode = 1; |
| 10325 | unlock_new_inode(inode); |
| 10326 | goto out_unlock; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 10327 | } |
Chris Mason | 1643298 | 2008-04-10 10:23:21 -0400 | [diff] [blame] | 10328 | |
Josef Bacik | 0af3d00 | 2010-06-21 14:48:16 -0400 | [diff] [blame] | 10329 | static int __btrfs_prealloc_file_range(struct inode *inode, int mode, |
| 10330 | u64 start, u64 num_bytes, u64 min_size, |
| 10331 | loff_t actual_len, u64 *alloc_hint, |
| 10332 | struct btrfs_trans_handle *trans) |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 10333 | { |
Josef Bacik | 5dc562c | 2012-08-17 13:14:17 -0400 | [diff] [blame] | 10334 | struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree; |
| 10335 | struct extent_map *em; |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 10336 | struct btrfs_root *root = BTRFS_I(inode)->root; |
| 10337 | struct btrfs_key ins; |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 10338 | u64 cur_offset = start; |
Josef Bacik | 55a61d1 | 2010-11-22 18:50:32 +0000 | [diff] [blame] | 10339 | u64 i_size; |
Chris Mason | 154ea28 | 2013-03-05 11:11:26 -0500 | [diff] [blame] | 10340 | u64 cur_bytes; |
Josef Bacik | 0b670dc | 2015-09-23 17:11:16 -0400 | [diff] [blame] | 10341 | u64 last_alloc = (u64)-1; |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 10342 | int ret = 0; |
Josef Bacik | 0af3d00 | 2010-06-21 14:48:16 -0400 | [diff] [blame] | 10343 | bool own_trans = true; |
Wang Xiaoguang | 1851309 | 2016-07-25 15:51:40 +0800 | [diff] [blame] | 10344 | u64 end = start + num_bytes - 1; |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 10345 | |
Josef Bacik | 0af3d00 | 2010-06-21 14:48:16 -0400 | [diff] [blame] | 10346 | if (trans) |
| 10347 | own_trans = false; |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 10348 | while (num_bytes > 0) { |
Josef Bacik | 0af3d00 | 2010-06-21 14:48:16 -0400 | [diff] [blame] | 10349 | if (own_trans) { |
| 10350 | trans = btrfs_start_transaction(root, 3); |
| 10351 | if (IS_ERR(trans)) { |
| 10352 | ret = PTR_ERR(trans); |
| 10353 | break; |
| 10354 | } |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 10355 | } |
Yan, Zheng | 5a303d5 | 2009-11-12 09:34:52 +0000 | [diff] [blame] | 10356 | |
Byongho Lee | ee22184 | 2015-12-15 01:42:10 +0900 | [diff] [blame] | 10357 | cur_bytes = min_t(u64, num_bytes, SZ_256M); |
Chris Mason | 154ea28 | 2013-03-05 11:11:26 -0500 | [diff] [blame] | 10358 | cur_bytes = max(cur_bytes, min_size); |
Josef Bacik | 0b670dc | 2015-09-23 17:11:16 -0400 | [diff] [blame] | 10359 | /* |
| 10360 | * If we are severely fragmented we could end up with really |
| 10361 | * small allocations, so if the allocator is returning small |
| 10362 | * chunks lets make its job easier by only searching for those |
| 10363 | * sized chunks. |
| 10364 | */ |
| 10365 | cur_bytes = min(cur_bytes, last_alloc); |
Wang Xiaoguang | 1851309 | 2016-07-25 15:51:40 +0800 | [diff] [blame] | 10366 | ret = btrfs_reserve_extent(root, cur_bytes, cur_bytes, |
| 10367 | min_size, 0, *alloc_hint, &ins, 1, 0); |
Yan, Zheng | 5a303d5 | 2009-11-12 09:34:52 +0000 | [diff] [blame] | 10368 | if (ret) { |
Josef Bacik | 0af3d00 | 2010-06-21 14:48:16 -0400 | [diff] [blame] | 10369 | if (own_trans) |
| 10370 | btrfs_end_transaction(trans, root); |
Yan, Zheng | a22285a | 2010-05-16 10:48:46 -0400 | [diff] [blame] | 10371 | break; |
Yan, Zheng | 5a303d5 | 2009-11-12 09:34:52 +0000 | [diff] [blame] | 10372 | } |
Filipe Manana | 9cfa3e3 | 2016-04-26 15:39:32 +0100 | [diff] [blame] | 10373 | btrfs_dec_block_group_reservations(root->fs_info, ins.objectid); |
Yan, Zheng | 5a303d5 | 2009-11-12 09:34:52 +0000 | [diff] [blame] | 10374 | |
Josef Bacik | 0b670dc | 2015-09-23 17:11:16 -0400 | [diff] [blame] | 10375 | last_alloc = ins.offset; |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 10376 | ret = insert_reserved_file_extent(trans, inode, |
| 10377 | cur_offset, ins.objectid, |
| 10378 | ins.offset, ins.offset, |
Yan, Zheng | 920bbbf | 2009-11-12 09:34:08 +0000 | [diff] [blame] | 10379 | ins.offset, 0, 0, 0, |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 10380 | BTRFS_FILE_EXTENT_PREALLOC); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 10381 | if (ret) { |
Josef Bacik | 857cc2f | 2013-10-07 15:21:08 -0400 | [diff] [blame] | 10382 | btrfs_free_reserved_extent(root, ins.objectid, |
Miao Xie | e570fd2 | 2014-06-19 10:42:50 +0800 | [diff] [blame] | 10383 | ins.offset, 0); |
Jeff Mahoney | 6664283 | 2016-06-10 18:19:25 -0400 | [diff] [blame] | 10384 | btrfs_abort_transaction(trans, ret); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 10385 | if (own_trans) |
| 10386 | btrfs_end_transaction(trans, root); |
| 10387 | break; |
| 10388 | } |
Dongsheng Yang | 3119321 | 2014-12-12 16:44:35 +0800 | [diff] [blame] | 10389 | |
Chris Mason | a1ed835 | 2009-09-11 12:27:37 -0400 | [diff] [blame] | 10390 | btrfs_drop_extent_cache(inode, cur_offset, |
| 10391 | cur_offset + ins.offset -1, 0); |
Yan, Zheng | 5a303d5 | 2009-11-12 09:34:52 +0000 | [diff] [blame] | 10392 | |
Josef Bacik | 5dc562c | 2012-08-17 13:14:17 -0400 | [diff] [blame] | 10393 | em = alloc_extent_map(); |
| 10394 | if (!em) { |
| 10395 | set_bit(BTRFS_INODE_NEEDS_FULL_SYNC, |
| 10396 | &BTRFS_I(inode)->runtime_flags); |
| 10397 | goto next; |
| 10398 | } |
| 10399 | |
| 10400 | em->start = cur_offset; |
| 10401 | em->orig_start = cur_offset; |
| 10402 | em->len = ins.offset; |
| 10403 | em->block_start = ins.objectid; |
| 10404 | em->block_len = ins.offset; |
Josef Bacik | b493968 | 2012-12-03 10:31:19 -0500 | [diff] [blame] | 10405 | em->orig_block_len = ins.offset; |
Josef Bacik | cc95bef | 2013-04-04 14:31:27 -0400 | [diff] [blame] | 10406 | em->ram_bytes = ins.offset; |
Josef Bacik | 5dc562c | 2012-08-17 13:14:17 -0400 | [diff] [blame] | 10407 | em->bdev = root->fs_info->fs_devices->latest_bdev; |
| 10408 | set_bit(EXTENT_FLAG_PREALLOC, &em->flags); |
| 10409 | em->generation = trans->transid; |
| 10410 | |
| 10411 | while (1) { |
| 10412 | write_lock(&em_tree->lock); |
Josef Bacik | 09a2a8f9 | 2013-04-05 16:51:15 -0400 | [diff] [blame] | 10413 | ret = add_extent_mapping(em_tree, em, 1); |
Josef Bacik | 5dc562c | 2012-08-17 13:14:17 -0400 | [diff] [blame] | 10414 | write_unlock(&em_tree->lock); |
| 10415 | if (ret != -EEXIST) |
| 10416 | break; |
| 10417 | btrfs_drop_extent_cache(inode, cur_offset, |
| 10418 | cur_offset + ins.offset - 1, |
| 10419 | 0); |
| 10420 | } |
| 10421 | free_extent_map(em); |
| 10422 | next: |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 10423 | num_bytes -= ins.offset; |
| 10424 | cur_offset += ins.offset; |
Yan, Zheng | efa5646 | 2010-05-16 10:49:59 -0400 | [diff] [blame] | 10425 | *alloc_hint = ins.objectid + ins.offset; |
Yan, Zheng | 5a303d5 | 2009-11-12 09:34:52 +0000 | [diff] [blame] | 10426 | |
Josef Bacik | 0c4d2d9 | 2012-04-05 15:03:02 -0400 | [diff] [blame] | 10427 | inode_inc_iversion(inode); |
Deepa Dinamani | c2050a4 | 2016-09-14 07:48:06 -0700 | [diff] [blame] | 10428 | inode->i_ctime = current_time(inode); |
Christoph Hellwig | 6cbff00 | 2009-04-17 10:37:41 +0200 | [diff] [blame] | 10429 | BTRFS_I(inode)->flags |= BTRFS_INODE_PREALLOC; |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 10430 | if (!(mode & FALLOC_FL_KEEP_SIZE) && |
Yan, Zheng | efa5646 | 2010-05-16 10:49:59 -0400 | [diff] [blame] | 10431 | (actual_len > inode->i_size) && |
| 10432 | (cur_offset > inode->i_size)) { |
Aneesh Kumar K.V | d1ea6a6 | 2010-01-20 07:28:54 +0000 | [diff] [blame] | 10433 | if (cur_offset > actual_len) |
Josef Bacik | 55a61d1 | 2010-11-22 18:50:32 +0000 | [diff] [blame] | 10434 | i_size = actual_len; |
Aneesh Kumar K.V | d1ea6a6 | 2010-01-20 07:28:54 +0000 | [diff] [blame] | 10435 | else |
Josef Bacik | 55a61d1 | 2010-11-22 18:50:32 +0000 | [diff] [blame] | 10436 | i_size = cur_offset; |
| 10437 | i_size_write(inode, i_size); |
| 10438 | btrfs_ordered_update_i_size(inode, i_size, NULL); |
Yan, Zheng | 5a303d5 | 2009-11-12 09:34:52 +0000 | [diff] [blame] | 10439 | } |
| 10440 | |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 10441 | ret = btrfs_update_inode(trans, root, inode); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 10442 | |
| 10443 | if (ret) { |
Jeff Mahoney | 6664283 | 2016-06-10 18:19:25 -0400 | [diff] [blame] | 10444 | btrfs_abort_transaction(trans, ret); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 10445 | if (own_trans) |
| 10446 | btrfs_end_transaction(trans, root); |
| 10447 | break; |
| 10448 | } |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 10449 | |
Josef Bacik | 0af3d00 | 2010-06-21 14:48:16 -0400 | [diff] [blame] | 10450 | if (own_trans) |
| 10451 | btrfs_end_transaction(trans, root); |
Yan, Zheng | 5a303d5 | 2009-11-12 09:34:52 +0000 | [diff] [blame] | 10452 | } |
Wang Xiaoguang | 1851309 | 2016-07-25 15:51:40 +0800 | [diff] [blame] | 10453 | if (cur_offset < end) |
| 10454 | btrfs_free_reserved_data_space(inode, cur_offset, |
| 10455 | end - cur_offset + 1); |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 10456 | return ret; |
| 10457 | } |
| 10458 | |
Josef Bacik | 0af3d00 | 2010-06-21 14:48:16 -0400 | [diff] [blame] | 10459 | int btrfs_prealloc_file_range(struct inode *inode, int mode, |
| 10460 | u64 start, u64 num_bytes, u64 min_size, |
| 10461 | loff_t actual_len, u64 *alloc_hint) |
| 10462 | { |
| 10463 | return __btrfs_prealloc_file_range(inode, mode, start, num_bytes, |
| 10464 | min_size, actual_len, alloc_hint, |
| 10465 | NULL); |
| 10466 | } |
| 10467 | |
| 10468 | int btrfs_prealloc_file_range_trans(struct inode *inode, |
| 10469 | struct btrfs_trans_handle *trans, int mode, |
| 10470 | u64 start, u64 num_bytes, u64 min_size, |
| 10471 | loff_t actual_len, u64 *alloc_hint) |
| 10472 | { |
| 10473 | return __btrfs_prealloc_file_range(inode, mode, start, num_bytes, |
| 10474 | min_size, actual_len, alloc_hint, trans); |
| 10475 | } |
| 10476 | |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 10477 | static int btrfs_set_page_dirty(struct page *page) |
| 10478 | { |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 10479 | return __set_page_dirty_nobuffers(page); |
| 10480 | } |
| 10481 | |
Al Viro | 10556cb | 2011-06-20 19:28:19 -0400 | [diff] [blame] | 10482 | static int btrfs_permission(struct inode *inode, int mask) |
Yan | fdebe2b | 2008-01-14 13:26:08 -0500 | [diff] [blame] | 10483 | { |
Li Zefan | b83cc96 | 2010-12-20 16:04:08 +0800 | [diff] [blame] | 10484 | struct btrfs_root *root = BTRFS_I(inode)->root; |
Jeff Mahoney | cb6db4e | 2011-08-15 17:27:21 +0000 | [diff] [blame] | 10485 | umode_t mode = inode->i_mode; |
Li Zefan | b83cc96 | 2010-12-20 16:04:08 +0800 | [diff] [blame] | 10486 | |
Jeff Mahoney | cb6db4e | 2011-08-15 17:27:21 +0000 | [diff] [blame] | 10487 | if (mask & MAY_WRITE && |
| 10488 | (S_ISREG(mode) || S_ISDIR(mode) || S_ISLNK(mode))) { |
| 10489 | if (btrfs_root_readonly(root)) |
| 10490 | return -EROFS; |
| 10491 | if (BTRFS_I(inode)->flags & BTRFS_INODE_READONLY) |
| 10492 | return -EACCES; |
| 10493 | } |
Al Viro | 2830ba7 | 2011-06-20 19:16:29 -0400 | [diff] [blame] | 10494 | return generic_permission(inode, mask); |
Yan | fdebe2b | 2008-01-14 13:26:08 -0500 | [diff] [blame] | 10495 | } |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 10496 | |
Filipe Manana | ef3b9af | 2014-04-27 20:40:45 +0100 | [diff] [blame] | 10497 | static int btrfs_tmpfile(struct inode *dir, struct dentry *dentry, umode_t mode) |
| 10498 | { |
| 10499 | struct btrfs_trans_handle *trans; |
| 10500 | struct btrfs_root *root = BTRFS_I(dir)->root; |
| 10501 | struct inode *inode = NULL; |
| 10502 | u64 objectid; |
| 10503 | u64 index; |
| 10504 | int ret = 0; |
| 10505 | |
| 10506 | /* |
| 10507 | * 5 units required for adding orphan entry |
| 10508 | */ |
| 10509 | trans = btrfs_start_transaction(root, 5); |
| 10510 | if (IS_ERR(trans)) |
| 10511 | return PTR_ERR(trans); |
| 10512 | |
| 10513 | ret = btrfs_find_free_ino(root, &objectid); |
| 10514 | if (ret) |
| 10515 | goto out; |
| 10516 | |
| 10517 | inode = btrfs_new_inode(trans, root, dir, NULL, 0, |
| 10518 | btrfs_ino(dir), objectid, mode, &index); |
| 10519 | if (IS_ERR(inode)) { |
| 10520 | ret = PTR_ERR(inode); |
| 10521 | inode = NULL; |
| 10522 | goto out; |
| 10523 | } |
| 10524 | |
Filipe Manana | ef3b9af | 2014-04-27 20:40:45 +0100 | [diff] [blame] | 10525 | inode->i_fop = &btrfs_file_operations; |
| 10526 | inode->i_op = &btrfs_file_inode_operations; |
| 10527 | |
| 10528 | inode->i_mapping->a_ops = &btrfs_aops; |
Filipe Manana | ef3b9af | 2014-04-27 20:40:45 +0100 | [diff] [blame] | 10529 | BTRFS_I(inode)->io_tree.ops = &btrfs_extent_io_ops; |
| 10530 | |
Chris Mason | b0d5d10 | 2014-09-08 13:08:51 -0700 | [diff] [blame] | 10531 | ret = btrfs_init_inode_security(trans, inode, dir, NULL); |
| 10532 | if (ret) |
| 10533 | goto out_inode; |
| 10534 | |
| 10535 | ret = btrfs_update_inode(trans, root, inode); |
| 10536 | if (ret) |
| 10537 | goto out_inode; |
Filipe Manana | ef3b9af | 2014-04-27 20:40:45 +0100 | [diff] [blame] | 10538 | ret = btrfs_orphan_add(trans, inode); |
| 10539 | if (ret) |
Chris Mason | b0d5d10 | 2014-09-08 13:08:51 -0700 | [diff] [blame] | 10540 | goto out_inode; |
Filipe Manana | ef3b9af | 2014-04-27 20:40:45 +0100 | [diff] [blame] | 10541 | |
Filipe Manana | 5762b5c | 2014-08-01 00:10:32 +0100 | [diff] [blame] | 10542 | /* |
| 10543 | * We set number of links to 0 in btrfs_new_inode(), and here we set |
| 10544 | * it to 1 because d_tmpfile() will issue a warning if the count is 0, |
| 10545 | * through: |
| 10546 | * |
| 10547 | * d_tmpfile() -> inode_dec_link_count() -> drop_nlink() |
| 10548 | */ |
| 10549 | set_nlink(inode, 1); |
Chris Mason | b0d5d10 | 2014-09-08 13:08:51 -0700 | [diff] [blame] | 10550 | unlock_new_inode(inode); |
Filipe Manana | ef3b9af | 2014-04-27 20:40:45 +0100 | [diff] [blame] | 10551 | d_tmpfile(dentry, inode); |
| 10552 | mark_inode_dirty(inode); |
| 10553 | |
| 10554 | out: |
| 10555 | btrfs_end_transaction(trans, root); |
| 10556 | if (ret) |
| 10557 | iput(inode); |
| 10558 | btrfs_balance_delayed_items(root); |
| 10559 | btrfs_btree_balance_dirty(root); |
Filipe Manana | ef3b9af | 2014-04-27 20:40:45 +0100 | [diff] [blame] | 10560 | return ret; |
Chris Mason | b0d5d10 | 2014-09-08 13:08:51 -0700 | [diff] [blame] | 10561 | |
| 10562 | out_inode: |
| 10563 | unlock_new_inode(inode); |
| 10564 | goto out; |
| 10565 | |
Filipe Manana | ef3b9af | 2014-04-27 20:40:45 +0100 | [diff] [blame] | 10566 | } |
| 10567 | |
Alexey Dobriyan | 6e1d5dc | 2009-09-21 17:01:11 -0700 | [diff] [blame] | 10568 | static const struct inode_operations btrfs_dir_inode_operations = { |
Chris Mason | 3394e16 | 2008-11-17 20:42:26 -0500 | [diff] [blame] | 10569 | .getattr = btrfs_getattr, |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 10570 | .lookup = btrfs_lookup, |
| 10571 | .create = btrfs_create, |
| 10572 | .unlink = btrfs_unlink, |
| 10573 | .link = btrfs_link, |
| 10574 | .mkdir = btrfs_mkdir, |
| 10575 | .rmdir = btrfs_rmdir, |
Miklos Szeredi | 2773bf0 | 2016-09-27 11:03:58 +0200 | [diff] [blame] | 10576 | .rename = btrfs_rename2, |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 10577 | .symlink = btrfs_symlink, |
| 10578 | .setattr = btrfs_setattr, |
Josef Bacik | 618e21d | 2007-07-11 10:18:17 -0400 | [diff] [blame] | 10579 | .mknod = btrfs_mknod, |
Josef Bacik | 5103e94 | 2007-11-16 11:45:54 -0500 | [diff] [blame] | 10580 | .listxattr = btrfs_listxattr, |
Yan | fdebe2b | 2008-01-14 13:26:08 -0500 | [diff] [blame] | 10581 | .permission = btrfs_permission, |
Christoph Hellwig | 4e34e71 | 2011-07-23 17:37:31 +0200 | [diff] [blame] | 10582 | .get_acl = btrfs_get_acl, |
Christoph Hellwig | 996a710 | 2013-12-20 05:16:43 -0800 | [diff] [blame] | 10583 | .set_acl = btrfs_set_acl, |
Guangyu Sun | 93fd63c | 2013-09-16 10:42:03 -0700 | [diff] [blame] | 10584 | .update_time = btrfs_update_time, |
Filipe Manana | ef3b9af | 2014-04-27 20:40:45 +0100 | [diff] [blame] | 10585 | .tmpfile = btrfs_tmpfile, |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 10586 | }; |
Alexey Dobriyan | 6e1d5dc | 2009-09-21 17:01:11 -0700 | [diff] [blame] | 10587 | static const struct inode_operations btrfs_dir_ro_inode_operations = { |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 10588 | .lookup = btrfs_lookup, |
Yan | fdebe2b | 2008-01-14 13:26:08 -0500 | [diff] [blame] | 10589 | .permission = btrfs_permission, |
Christoph Hellwig | 4e34e71 | 2011-07-23 17:37:31 +0200 | [diff] [blame] | 10590 | .get_acl = btrfs_get_acl, |
Christoph Hellwig | 996a710 | 2013-12-20 05:16:43 -0800 | [diff] [blame] | 10591 | .set_acl = btrfs_set_acl, |
Guangyu Sun | 93fd63c | 2013-09-16 10:42:03 -0700 | [diff] [blame] | 10592 | .update_time = btrfs_update_time, |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 10593 | }; |
Yan, Zheng | 76dda93 | 2009-09-21 16:00:26 -0400 | [diff] [blame] | 10594 | |
Alexey Dobriyan | 828c095 | 2009-10-01 15:43:56 -0700 | [diff] [blame] | 10595 | static const struct file_operations btrfs_dir_file_operations = { |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 10596 | .llseek = generic_file_llseek, |
| 10597 | .read = generic_read_dir, |
Omar Sandoval | 02dbfc9 | 2016-05-20 13:50:33 -0700 | [diff] [blame] | 10598 | .iterate_shared = btrfs_real_readdir, |
Christoph Hellwig | 34287aa | 2007-09-14 10:22:47 -0400 | [diff] [blame] | 10599 | .unlocked_ioctl = btrfs_ioctl, |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 10600 | #ifdef CONFIG_COMPAT |
Luke Dashjr | 4c63c24 | 2015-10-29 08:22:21 +0000 | [diff] [blame] | 10601 | .compat_ioctl = btrfs_compat_ioctl, |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 10602 | #endif |
Sage Weil | 6bf13c0 | 2008-06-10 10:07:39 -0400 | [diff] [blame] | 10603 | .release = btrfs_release_file, |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 10604 | .fsync = btrfs_sync_file, |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 10605 | }; |
| 10606 | |
David Sterba | 20e5506 | 2015-11-19 11:42:28 +0100 | [diff] [blame] | 10607 | static const struct extent_io_ops btrfs_extent_io_ops = { |
Chris Mason | 07157aa | 2007-08-30 08:50:51 -0400 | [diff] [blame] | 10608 | .fill_delalloc = run_delalloc_range, |
Chris Mason | 065631f | 2008-02-20 12:07:25 -0500 | [diff] [blame] | 10609 | .submit_bio_hook = btrfs_submit_bio_hook, |
Chris Mason | 239b14b | 2008-03-24 15:02:07 -0400 | [diff] [blame] | 10610 | .merge_bio_hook = btrfs_merge_bio_hook, |
Chris Mason | 07157aa | 2007-08-30 08:50:51 -0400 | [diff] [blame] | 10611 | .readpage_end_io_hook = btrfs_readpage_end_io_hook, |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 10612 | .writepage_end_io_hook = btrfs_writepage_end_io_hook, |
Chris Mason | 247e743 | 2008-07-17 12:53:51 -0400 | [diff] [blame] | 10613 | .writepage_start_hook = btrfs_writepage_start_hook, |
Chris Mason | b0c68f8 | 2008-01-31 11:05:37 -0500 | [diff] [blame] | 10614 | .set_bit_hook = btrfs_set_bit_hook, |
| 10615 | .clear_bit_hook = btrfs_clear_bit_hook, |
Josef Bacik | 9ed74f2 | 2009-09-11 16:12:44 -0400 | [diff] [blame] | 10616 | .merge_extent_hook = btrfs_merge_extent_hook, |
| 10617 | .split_extent_hook = btrfs_split_extent_hook, |
Chris Mason | 07157aa | 2007-08-30 08:50:51 -0400 | [diff] [blame] | 10618 | }; |
| 10619 | |
Chris Mason | 3505439 | 2009-01-21 13:11:13 -0500 | [diff] [blame] | 10620 | /* |
| 10621 | * btrfs doesn't support the bmap operation because swapfiles |
| 10622 | * use bmap to make a mapping of extents in the file. They assume |
| 10623 | * these extents won't change over the life of the file and they |
| 10624 | * use the bmap result to do IO directly to the drive. |
| 10625 | * |
| 10626 | * the btrfs bmap call would return logical addresses that aren't |
| 10627 | * suitable for IO and they also will change frequently as COW |
| 10628 | * operations happen. So, swapfile + btrfs == corruption. |
| 10629 | * |
| 10630 | * For now we're avoiding this by dropping bmap. |
| 10631 | */ |
Alexey Dobriyan | 7f09410 | 2009-09-21 17:01:10 -0700 | [diff] [blame] | 10632 | static const struct address_space_operations btrfs_aops = { |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 10633 | .readpage = btrfs_readpage, |
| 10634 | .writepage = btrfs_writepage, |
Chris Mason | b293f02 | 2007-11-01 19:45:34 -0400 | [diff] [blame] | 10635 | .writepages = btrfs_writepages, |
Chris Mason | 3ab2fb5 | 2007-11-08 10:59:22 -0500 | [diff] [blame] | 10636 | .readpages = btrfs_readpages, |
Chris Mason | 1643298 | 2008-04-10 10:23:21 -0400 | [diff] [blame] | 10637 | .direct_IO = btrfs_direct_IO, |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 10638 | .invalidatepage = btrfs_invalidatepage, |
| 10639 | .releasepage = btrfs_releasepage, |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 10640 | .set_page_dirty = btrfs_set_page_dirty, |
Andi Kleen | 465fdd9 | 2009-09-16 11:50:18 +0200 | [diff] [blame] | 10641 | .error_remove_page = generic_error_remove_page, |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 10642 | }; |
| 10643 | |
Alexey Dobriyan | 7f09410 | 2009-09-21 17:01:10 -0700 | [diff] [blame] | 10644 | static const struct address_space_operations btrfs_symlink_aops = { |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 10645 | .readpage = btrfs_readpage, |
| 10646 | .writepage = btrfs_writepage, |
Chris Mason | 2bf5a72 | 2007-08-30 11:54:02 -0400 | [diff] [blame] | 10647 | .invalidatepage = btrfs_invalidatepage, |
| 10648 | .releasepage = btrfs_releasepage, |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 10649 | }; |
| 10650 | |
Alexey Dobriyan | 6e1d5dc | 2009-09-21 17:01:11 -0700 | [diff] [blame] | 10651 | static const struct inode_operations btrfs_file_inode_operations = { |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 10652 | .getattr = btrfs_getattr, |
| 10653 | .setattr = btrfs_setattr, |
Josef Bacik | 5103e94 | 2007-11-16 11:45:54 -0500 | [diff] [blame] | 10654 | .listxattr = btrfs_listxattr, |
Yan | fdebe2b | 2008-01-14 13:26:08 -0500 | [diff] [blame] | 10655 | .permission = btrfs_permission, |
Yehuda Sadeh | 1506fcc | 2009-01-21 14:39:14 -0500 | [diff] [blame] | 10656 | .fiemap = btrfs_fiemap, |
Christoph Hellwig | 4e34e71 | 2011-07-23 17:37:31 +0200 | [diff] [blame] | 10657 | .get_acl = btrfs_get_acl, |
Christoph Hellwig | 996a710 | 2013-12-20 05:16:43 -0800 | [diff] [blame] | 10658 | .set_acl = btrfs_set_acl, |
Josef Bacik | e41f941 | 2012-03-26 09:46:47 -0400 | [diff] [blame] | 10659 | .update_time = btrfs_update_time, |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 10660 | }; |
Alexey Dobriyan | 6e1d5dc | 2009-09-21 17:01:11 -0700 | [diff] [blame] | 10661 | static const struct inode_operations btrfs_special_inode_operations = { |
Josef Bacik | 618e21d | 2007-07-11 10:18:17 -0400 | [diff] [blame] | 10662 | .getattr = btrfs_getattr, |
| 10663 | .setattr = btrfs_setattr, |
Yan | fdebe2b | 2008-01-14 13:26:08 -0500 | [diff] [blame] | 10664 | .permission = btrfs_permission, |
Josef Bacik | 33268ea | 2008-07-24 12:16:36 -0400 | [diff] [blame] | 10665 | .listxattr = btrfs_listxattr, |
Christoph Hellwig | 4e34e71 | 2011-07-23 17:37:31 +0200 | [diff] [blame] | 10666 | .get_acl = btrfs_get_acl, |
Christoph Hellwig | 996a710 | 2013-12-20 05:16:43 -0800 | [diff] [blame] | 10667 | .set_acl = btrfs_set_acl, |
Josef Bacik | e41f941 | 2012-03-26 09:46:47 -0400 | [diff] [blame] | 10668 | .update_time = btrfs_update_time, |
Josef Bacik | 618e21d | 2007-07-11 10:18:17 -0400 | [diff] [blame] | 10669 | }; |
Alexey Dobriyan | 6e1d5dc | 2009-09-21 17:01:11 -0700 | [diff] [blame] | 10670 | static const struct inode_operations btrfs_symlink_inode_operations = { |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 10671 | .readlink = generic_readlink, |
Al Viro | 6b25539 | 2015-11-17 10:20:54 -0500 | [diff] [blame] | 10672 | .get_link = page_get_link, |
Li Zefan | f209561 | 2010-11-19 02:05:24 +0000 | [diff] [blame] | 10673 | .getattr = btrfs_getattr, |
Josef Bacik | 22c44fe | 2011-11-30 10:45:38 -0500 | [diff] [blame] | 10674 | .setattr = btrfs_setattr, |
Yan | fdebe2b | 2008-01-14 13:26:08 -0500 | [diff] [blame] | 10675 | .permission = btrfs_permission, |
Jim Owens | 0279b4c | 2009-02-04 09:29:13 -0500 | [diff] [blame] | 10676 | .listxattr = btrfs_listxattr, |
Josef Bacik | e41f941 | 2012-03-26 09:46:47 -0400 | [diff] [blame] | 10677 | .update_time = btrfs_update_time, |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 10678 | }; |
Yan, Zheng | 76dda93 | 2009-09-21 16:00:26 -0400 | [diff] [blame] | 10679 | |
Alexey Dobriyan | 82d339d | 2009-10-09 09:54:36 -0400 | [diff] [blame] | 10680 | const struct dentry_operations btrfs_dentry_operations = { |
Yan, Zheng | 76dda93 | 2009-09-21 16:00:26 -0400 | [diff] [blame] | 10681 | .d_delete = btrfs_dentry_delete, |
Josef Bacik | b4aff1f | 2011-06-28 16:18:59 -0400 | [diff] [blame] | 10682 | .d_release = btrfs_dentry_release, |
Yan, Zheng | 76dda93 | 2009-09-21 16:00:26 -0400 | [diff] [blame] | 10683 | }; |