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" |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 63 | |
| 64 | struct btrfs_iget_args { |
Chris Mason | 90d3e59 | 2014-01-09 17:28:00 -0800 | [diff] [blame] | 65 | struct btrfs_key *location; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 66 | struct btrfs_root *root; |
| 67 | }; |
| 68 | |
Alexey Dobriyan | 6e1d5dc | 2009-09-21 17:01:11 -0700 | [diff] [blame] | 69 | static const struct inode_operations btrfs_dir_inode_operations; |
| 70 | static const struct inode_operations btrfs_symlink_inode_operations; |
| 71 | static const struct inode_operations btrfs_dir_ro_inode_operations; |
| 72 | static const struct inode_operations btrfs_special_inode_operations; |
| 73 | static const struct inode_operations btrfs_file_inode_operations; |
Alexey Dobriyan | 7f09410 | 2009-09-21 17:01:10 -0700 | [diff] [blame] | 74 | static const struct address_space_operations btrfs_aops; |
| 75 | static const struct address_space_operations btrfs_symlink_aops; |
Alexey Dobriyan | 828c095 | 2009-10-01 15:43:56 -0700 | [diff] [blame] | 76 | static const struct file_operations btrfs_dir_file_operations; |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 77 | static struct extent_io_ops btrfs_extent_io_ops; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 78 | |
| 79 | static struct kmem_cache *btrfs_inode_cachep; |
Miao Xie | 8ccf6f19 | 2012-10-25 09:28:04 +0000 | [diff] [blame] | 80 | static struct kmem_cache *btrfs_delalloc_work_cachep; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 81 | struct kmem_cache *btrfs_trans_handle_cachep; |
| 82 | struct kmem_cache *btrfs_transaction_cachep; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 83 | struct kmem_cache *btrfs_path_cachep; |
Josef Bacik | dc89e98 | 2011-01-28 17:05:48 -0500 | [diff] [blame] | 84 | struct kmem_cache *btrfs_free_space_cachep; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 85 | |
| 86 | #define S_SHIFT 12 |
| 87 | static unsigned char btrfs_type_by_mode[S_IFMT >> S_SHIFT] = { |
| 88 | [S_IFREG >> S_SHIFT] = BTRFS_FT_REG_FILE, |
| 89 | [S_IFDIR >> S_SHIFT] = BTRFS_FT_DIR, |
| 90 | [S_IFCHR >> S_SHIFT] = BTRFS_FT_CHRDEV, |
| 91 | [S_IFBLK >> S_SHIFT] = BTRFS_FT_BLKDEV, |
| 92 | [S_IFIFO >> S_SHIFT] = BTRFS_FT_FIFO, |
| 93 | [S_IFSOCK >> S_SHIFT] = BTRFS_FT_SOCK, |
| 94 | [S_IFLNK >> S_SHIFT] = BTRFS_FT_SYMLINK, |
| 95 | }; |
| 96 | |
Eric Sandeen | 3972f26 | 2013-01-12 02:57:22 +0000 | [diff] [blame] | 97 | static int btrfs_setsize(struct inode *inode, struct iattr *attr); |
Josef Bacik | a41ad39 | 2011-01-31 15:30:16 -0500 | [diff] [blame] | 98 | static int btrfs_truncate(struct inode *inode); |
Josef Bacik | 5fd0204 | 2012-05-02 14:00:54 -0400 | [diff] [blame] | 99 | static int btrfs_finish_ordered_io(struct btrfs_ordered_extent *ordered_extent); |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 100 | static noinline int cow_file_range(struct inode *inode, |
| 101 | struct page *locked_page, |
| 102 | u64 start, u64 end, int *page_started, |
| 103 | unsigned long *nr_written, int unlock); |
Josef Bacik | 70c8a91 | 2012-10-11 16:54:30 -0400 | [diff] [blame] | 104 | static struct extent_map *create_pinned_em(struct inode *inode, u64 start, |
| 105 | u64 len, u64 orig_start, |
| 106 | u64 block_start, u64 block_len, |
Josef Bacik | cc95bef | 2013-04-04 14:31:27 -0400 | [diff] [blame] | 107 | u64 orig_block_len, u64 ram_bytes, |
| 108 | int type); |
Josef Bacik | 7b12876 | 2008-07-24 12:17:14 -0400 | [diff] [blame] | 109 | |
Eric Sandeen | 48a3b63 | 2013-04-25 20:41:01 +0000 | [diff] [blame] | 110 | static int btrfs_dirty_inode(struct inode *inode); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 111 | |
Josef Bacik | 6a3891c | 2015-03-16 17:38:52 -0400 | [diff] [blame] | 112 | #ifdef CONFIG_BTRFS_FS_RUN_SANITY_TESTS |
| 113 | void btrfs_test_inode_set_ops(struct inode *inode) |
| 114 | { |
| 115 | BTRFS_I(inode)->io_tree.ops = &btrfs_extent_io_ops; |
| 116 | } |
| 117 | #endif |
| 118 | |
Yan, Zheng | f34f57a | 2009-11-12 09:35:27 +0000 | [diff] [blame] | 119 | static int btrfs_init_inode_security(struct btrfs_trans_handle *trans, |
Eric Paris | 2a7dba3 | 2011-02-01 11:05:39 -0500 | [diff] [blame] | 120 | struct inode *inode, struct inode *dir, |
| 121 | const struct qstr *qstr) |
Jim Owens | 0279b4c | 2009-02-04 09:29:13 -0500 | [diff] [blame] | 122 | { |
| 123 | int err; |
| 124 | |
Yan, Zheng | f34f57a | 2009-11-12 09:35:27 +0000 | [diff] [blame] | 125 | err = btrfs_init_acl(trans, inode, dir); |
Jim Owens | 0279b4c | 2009-02-04 09:29:13 -0500 | [diff] [blame] | 126 | if (!err) |
Eric Paris | 2a7dba3 | 2011-02-01 11:05:39 -0500 | [diff] [blame] | 127 | err = btrfs_xattr_security_init(trans, inode, dir, qstr); |
Jim Owens | 0279b4c | 2009-02-04 09:29:13 -0500 | [diff] [blame] | 128 | return err; |
| 129 | } |
| 130 | |
Chris Mason | d352ac6 | 2008-09-29 15:18:18 -0400 | [diff] [blame] | 131 | /* |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 132 | * this does all the hard work for inserting an inline extent into |
| 133 | * the btree. The caller should have done a btrfs_drop_extents so that |
| 134 | * no overlapping inline items exist in the btree |
| 135 | */ |
Chris Mason | 40f7658 | 2014-05-21 13:35:51 -0700 | [diff] [blame] | 136 | static int insert_inline_extent(struct btrfs_trans_handle *trans, |
Filipe David Borba Manana | 1acae57 | 2014-01-07 11:42:27 +0000 | [diff] [blame] | 137 | struct btrfs_path *path, int extent_inserted, |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 138 | struct btrfs_root *root, struct inode *inode, |
| 139 | u64 start, size_t size, size_t compressed_size, |
Li Zefan | fe3f566 | 2011-03-28 08:30:38 +0000 | [diff] [blame] | 140 | int compress_type, |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 141 | struct page **compressed_pages) |
| 142 | { |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 143 | struct extent_buffer *leaf; |
| 144 | struct page *page = NULL; |
| 145 | char *kaddr; |
| 146 | unsigned long ptr; |
| 147 | struct btrfs_file_extent_item *ei; |
| 148 | int err = 0; |
| 149 | int ret; |
| 150 | size_t cur_size = size; |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 151 | unsigned long offset; |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 152 | |
Li Zefan | fe3f566 | 2011-03-28 08:30:38 +0000 | [diff] [blame] | 153 | if (compressed_size && compressed_pages) |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 154 | cur_size = compressed_size; |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 155 | |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 156 | inode_add_bytes(inode, size); |
Filipe David Borba Manana | 1acae57 | 2014-01-07 11:42:27 +0000 | [diff] [blame] | 157 | |
| 158 | if (!extent_inserted) { |
| 159 | struct btrfs_key key; |
| 160 | size_t datasize; |
| 161 | |
| 162 | key.objectid = btrfs_ino(inode); |
| 163 | key.offset = start; |
David Sterba | 962a298 | 2014-06-04 18:41:45 +0200 | [diff] [blame] | 164 | key.type = BTRFS_EXTENT_DATA_KEY; |
Filipe David Borba Manana | 1acae57 | 2014-01-07 11:42:27 +0000 | [diff] [blame] | 165 | |
| 166 | datasize = btrfs_file_extent_calc_inline_size(cur_size); |
| 167 | path->leave_spinning = 1; |
| 168 | ret = btrfs_insert_empty_item(trans, root, path, &key, |
| 169 | datasize); |
| 170 | if (ret) { |
| 171 | err = ret; |
| 172 | goto fail; |
| 173 | } |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 174 | } |
| 175 | leaf = path->nodes[0]; |
| 176 | ei = btrfs_item_ptr(leaf, path->slots[0], |
| 177 | struct btrfs_file_extent_item); |
| 178 | btrfs_set_file_extent_generation(leaf, ei, trans->transid); |
| 179 | btrfs_set_file_extent_type(leaf, ei, BTRFS_FILE_EXTENT_INLINE); |
| 180 | btrfs_set_file_extent_encryption(leaf, ei, 0); |
| 181 | btrfs_set_file_extent_other_encoding(leaf, ei, 0); |
| 182 | btrfs_set_file_extent_ram_bytes(leaf, ei, size); |
| 183 | ptr = btrfs_file_extent_inline_start(ei); |
| 184 | |
Li Zefan | 261507a0 | 2010-12-17 14:21:50 +0800 | [diff] [blame] | 185 | if (compress_type != BTRFS_COMPRESS_NONE) { |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 186 | struct page *cpage; |
| 187 | int i = 0; |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 188 | while (compressed_size > 0) { |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 189 | cpage = compressed_pages[i]; |
Chris Mason | 5b050f0 | 2008-11-11 09:34:41 -0500 | [diff] [blame] | 190 | cur_size = min_t(unsigned long, compressed_size, |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 191 | PAGE_CACHE_SIZE); |
| 192 | |
Cong Wang | 7ac687d | 2011-11-25 23:14:28 +0800 | [diff] [blame] | 193 | kaddr = kmap_atomic(cpage); |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 194 | write_extent_buffer(leaf, kaddr, ptr, cur_size); |
Cong Wang | 7ac687d | 2011-11-25 23:14:28 +0800 | [diff] [blame] | 195 | kunmap_atomic(kaddr); |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 196 | |
| 197 | i++; |
| 198 | ptr += cur_size; |
| 199 | compressed_size -= cur_size; |
| 200 | } |
| 201 | btrfs_set_file_extent_compression(leaf, ei, |
Li Zefan | 261507a0 | 2010-12-17 14:21:50 +0800 | [diff] [blame] | 202 | compress_type); |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 203 | } else { |
| 204 | page = find_get_page(inode->i_mapping, |
| 205 | start >> PAGE_CACHE_SHIFT); |
| 206 | btrfs_set_file_extent_compression(leaf, ei, 0); |
Cong Wang | 7ac687d | 2011-11-25 23:14:28 +0800 | [diff] [blame] | 207 | kaddr = kmap_atomic(page); |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 208 | offset = start & (PAGE_CACHE_SIZE - 1); |
| 209 | write_extent_buffer(leaf, kaddr + offset, ptr, size); |
Cong Wang | 7ac687d | 2011-11-25 23:14:28 +0800 | [diff] [blame] | 210 | kunmap_atomic(kaddr); |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 211 | page_cache_release(page); |
| 212 | } |
| 213 | btrfs_mark_buffer_dirty(leaf); |
Filipe David Borba Manana | 1acae57 | 2014-01-07 11:42:27 +0000 | [diff] [blame] | 214 | btrfs_release_path(path); |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 215 | |
Yan, Zheng | c216775 | 2009-11-12 09:34:21 +0000 | [diff] [blame] | 216 | /* |
| 217 | * we're an inline extent, so nobody can |
| 218 | * extend the file past i_size without locking |
| 219 | * a page we already have locked. |
| 220 | * |
| 221 | * We must do any isize and inode updates |
| 222 | * before we unlock the pages. Otherwise we |
| 223 | * could end up racing with unlink. |
| 224 | */ |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 225 | BTRFS_I(inode)->disk_i_size = inode->i_size; |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 226 | ret = btrfs_update_inode(trans, root, inode); |
Yan, Zheng | c216775 | 2009-11-12 09:34:21 +0000 | [diff] [blame] | 227 | |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 228 | return ret; |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 229 | fail: |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 230 | return err; |
| 231 | } |
| 232 | |
| 233 | |
| 234 | /* |
| 235 | * conditionally insert an inline extent into the file. This |
| 236 | * does the checks required to make sure the data is small enough |
| 237 | * to fit as an inline extent. |
| 238 | */ |
Josef Bacik | 0036158 | 2013-08-14 14:02:47 -0400 | [diff] [blame] | 239 | static noinline int cow_file_range_inline(struct btrfs_root *root, |
| 240 | struct inode *inode, u64 start, |
| 241 | u64 end, size_t compressed_size, |
| 242 | int compress_type, |
| 243 | struct page **compressed_pages) |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 244 | { |
Josef Bacik | 0036158 | 2013-08-14 14:02:47 -0400 | [diff] [blame] | 245 | struct btrfs_trans_handle *trans; |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 246 | u64 isize = i_size_read(inode); |
| 247 | u64 actual_end = min(end + 1, isize); |
| 248 | u64 inline_len = actual_end - start; |
Qu Wenruo | fda2832 | 2013-02-26 08:10:22 +0000 | [diff] [blame] | 249 | u64 aligned_end = ALIGN(end, root->sectorsize); |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 250 | u64 data_len = inline_len; |
| 251 | int ret; |
Filipe David Borba Manana | 1acae57 | 2014-01-07 11:42:27 +0000 | [diff] [blame] | 252 | struct btrfs_path *path; |
| 253 | int extent_inserted = 0; |
| 254 | u32 extent_item_size; |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 255 | |
| 256 | if (compressed_size) |
| 257 | data_len = compressed_size; |
| 258 | |
| 259 | if (start > 0 || |
Wang Shilong | 354877b | 2014-07-17 11:44:11 +0800 | [diff] [blame] | 260 | actual_end > PAGE_CACHE_SIZE || |
| 261 | data_len > BTRFS_MAX_INLINE_DATA_SIZE(root) || |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 262 | (!compressed_size && |
| 263 | (actual_end & (root->sectorsize - 1)) == 0) || |
| 264 | end + 1 < isize || |
| 265 | data_len > root->fs_info->max_inline) { |
| 266 | return 1; |
| 267 | } |
| 268 | |
Filipe David Borba Manana | 1acae57 | 2014-01-07 11:42:27 +0000 | [diff] [blame] | 269 | path = btrfs_alloc_path(); |
| 270 | if (!path) |
| 271 | return -ENOMEM; |
| 272 | |
Josef Bacik | 0036158 | 2013-08-14 14:02:47 -0400 | [diff] [blame] | 273 | trans = btrfs_join_transaction(root); |
Filipe David Borba Manana | 1acae57 | 2014-01-07 11:42:27 +0000 | [diff] [blame] | 274 | if (IS_ERR(trans)) { |
| 275 | btrfs_free_path(path); |
Josef Bacik | 0036158 | 2013-08-14 14:02:47 -0400 | [diff] [blame] | 276 | return PTR_ERR(trans); |
Filipe David Borba Manana | 1acae57 | 2014-01-07 11:42:27 +0000 | [diff] [blame] | 277 | } |
Josef Bacik | 0036158 | 2013-08-14 14:02:47 -0400 | [diff] [blame] | 278 | trans->block_rsv = &root->fs_info->delalloc_block_rsv; |
| 279 | |
Filipe David Borba Manana | 1acae57 | 2014-01-07 11:42:27 +0000 | [diff] [blame] | 280 | if (compressed_size && compressed_pages) |
| 281 | extent_item_size = btrfs_file_extent_calc_inline_size( |
| 282 | compressed_size); |
| 283 | else |
| 284 | extent_item_size = btrfs_file_extent_calc_inline_size( |
| 285 | inline_len); |
| 286 | |
| 287 | ret = __btrfs_drop_extents(trans, root, inode, path, |
| 288 | start, aligned_end, NULL, |
| 289 | 1, 1, extent_item_size, &extent_inserted); |
Josef Bacik | 0036158 | 2013-08-14 14:02:47 -0400 | [diff] [blame] | 290 | if (ret) { |
| 291 | btrfs_abort_transaction(trans, root, ret); |
| 292 | goto out; |
| 293 | } |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 294 | |
| 295 | if (isize > actual_end) |
| 296 | inline_len = min_t(u64, isize, actual_end); |
Filipe David Borba Manana | 1acae57 | 2014-01-07 11:42:27 +0000 | [diff] [blame] | 297 | ret = insert_inline_extent(trans, path, extent_inserted, |
| 298 | root, inode, start, |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 299 | inline_len, compressed_size, |
Li Zefan | fe3f566 | 2011-03-28 08:30:38 +0000 | [diff] [blame] | 300 | compress_type, compressed_pages); |
Josef Bacik | 2adcac1 | 2012-05-23 16:10:14 -0400 | [diff] [blame] | 301 | if (ret && ret != -ENOSPC) { |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 302 | btrfs_abort_transaction(trans, root, ret); |
Josef Bacik | 0036158 | 2013-08-14 14:02:47 -0400 | [diff] [blame] | 303 | goto out; |
Josef Bacik | 2adcac1 | 2012-05-23 16:10:14 -0400 | [diff] [blame] | 304 | } else if (ret == -ENOSPC) { |
Josef Bacik | 0036158 | 2013-08-14 14:02:47 -0400 | [diff] [blame] | 305 | ret = 1; |
| 306 | goto out; |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 307 | } |
Josef Bacik | 2adcac1 | 2012-05-23 16:10:14 -0400 | [diff] [blame] | 308 | |
Josef Bacik | bdc20e6 | 2013-02-28 13:23:38 -0500 | [diff] [blame] | 309 | set_bit(BTRFS_INODE_NEEDS_FULL_SYNC, &BTRFS_I(inode)->runtime_flags); |
Yan, Zheng | 0ca1f7c | 2010-05-16 10:48:47 -0400 | [diff] [blame] | 310 | btrfs_delalloc_release_metadata(inode, end + 1 - start); |
Chris Mason | a1ed835 | 2009-09-11 12:27:37 -0400 | [diff] [blame] | 311 | btrfs_drop_extent_cache(inode, start, aligned_end - 1, 0); |
Josef Bacik | 0036158 | 2013-08-14 14:02:47 -0400 | [diff] [blame] | 312 | out: |
Filipe David Borba Manana | 1acae57 | 2014-01-07 11:42:27 +0000 | [diff] [blame] | 313 | btrfs_free_path(path); |
Josef Bacik | 0036158 | 2013-08-14 14:02:47 -0400 | [diff] [blame] | 314 | btrfs_end_transaction(trans, root); |
| 315 | return ret; |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 316 | } |
| 317 | |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 318 | struct async_extent { |
| 319 | u64 start; |
| 320 | u64 ram_size; |
| 321 | u64 compressed_size; |
| 322 | struct page **pages; |
| 323 | unsigned long nr_pages; |
Li Zefan | 261507a0 | 2010-12-17 14:21:50 +0800 | [diff] [blame] | 324 | int compress_type; |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 325 | struct list_head list; |
| 326 | }; |
| 327 | |
| 328 | struct async_cow { |
| 329 | struct inode *inode; |
| 330 | struct btrfs_root *root; |
| 331 | struct page *locked_page; |
| 332 | u64 start; |
| 333 | u64 end; |
| 334 | struct list_head extents; |
| 335 | struct btrfs_work work; |
| 336 | }; |
| 337 | |
| 338 | static noinline int add_async_extent(struct async_cow *cow, |
| 339 | u64 start, u64 ram_size, |
| 340 | u64 compressed_size, |
| 341 | struct page **pages, |
Li Zefan | 261507a0 | 2010-12-17 14:21:50 +0800 | [diff] [blame] | 342 | unsigned long nr_pages, |
| 343 | int compress_type) |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 344 | { |
| 345 | struct async_extent *async_extent; |
| 346 | |
| 347 | async_extent = kmalloc(sizeof(*async_extent), GFP_NOFS); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 348 | BUG_ON(!async_extent); /* -ENOMEM */ |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 349 | async_extent->start = start; |
| 350 | async_extent->ram_size = ram_size; |
| 351 | async_extent->compressed_size = compressed_size; |
| 352 | async_extent->pages = pages; |
| 353 | async_extent->nr_pages = nr_pages; |
Li Zefan | 261507a0 | 2010-12-17 14:21:50 +0800 | [diff] [blame] | 354 | async_extent->compress_type = compress_type; |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 355 | list_add_tail(&async_extent->list, &cow->extents); |
| 356 | return 0; |
| 357 | } |
| 358 | |
Wang Shilong | f79707b | 2014-07-17 11:44:09 +0800 | [diff] [blame] | 359 | static inline int inode_need_compress(struct inode *inode) |
| 360 | { |
| 361 | struct btrfs_root *root = BTRFS_I(inode)->root; |
| 362 | |
| 363 | /* force compress */ |
| 364 | if (btrfs_test_opt(root, FORCE_COMPRESS)) |
| 365 | return 1; |
| 366 | /* bad compression ratios */ |
| 367 | if (BTRFS_I(inode)->flags & BTRFS_INODE_NOCOMPRESS) |
| 368 | return 0; |
| 369 | if (btrfs_test_opt(root, COMPRESS) || |
| 370 | BTRFS_I(inode)->flags & BTRFS_INODE_COMPRESS || |
| 371 | BTRFS_I(inode)->force_compress) |
| 372 | return 1; |
| 373 | return 0; |
| 374 | } |
| 375 | |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 376 | /* |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 377 | * we create compressed extents in two phases. The first |
| 378 | * phase compresses a range of pages that have already been |
| 379 | * locked (both pages and state bits are locked). |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 380 | * |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 381 | * This is done inside an ordered work queue, and the compression |
| 382 | * is spread across many cpus. The actual IO submission is step |
| 383 | * two, and the ordered work queue takes care of making sure that |
| 384 | * happens in the same order things were put onto the queue by |
| 385 | * writepages and friends. |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 386 | * |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 387 | * If this code finds it can't get good compression, it puts an |
| 388 | * entry onto the work queue to write the uncompressed bytes. This |
| 389 | * makes sure that both compressed inodes and uncompressed inodes |
Artem Bityutskiy | b257031 | 2012-07-25 18:12:06 +0300 | [diff] [blame] | 390 | * are written in the same order that the flusher thread sent them |
| 391 | * down. |
Chris Mason | d352ac6 | 2008-09-29 15:18:18 -0400 | [diff] [blame] | 392 | */ |
Filipe Manana | c44f649 | 2014-10-09 21:15:44 +0100 | [diff] [blame] | 393 | static noinline void compress_file_range(struct inode *inode, |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 394 | struct page *locked_page, |
| 395 | u64 start, u64 end, |
| 396 | struct async_cow *async_cow, |
| 397 | int *num_added) |
Chris Mason | b888db2 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 398 | { |
| 399 | struct btrfs_root *root = BTRFS_I(inode)->root; |
Chris Mason | db94535 | 2007-10-15 16:15:53 -0400 | [diff] [blame] | 400 | u64 num_bytes; |
Chris Mason | db94535 | 2007-10-15 16:15:53 -0400 | [diff] [blame] | 401 | u64 blocksize = root->sectorsize; |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 402 | u64 actual_end; |
Chris Mason | 42dc7ba | 2008-12-15 11:44:56 -0500 | [diff] [blame] | 403 | u64 isize = i_size_read(inode); |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 404 | int ret = 0; |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 405 | struct page **pages = NULL; |
| 406 | unsigned long nr_pages; |
| 407 | unsigned long nr_pages_ret = 0; |
| 408 | unsigned long total_compressed = 0; |
| 409 | unsigned long total_in = 0; |
| 410 | unsigned long max_compressed = 128 * 1024; |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 411 | unsigned long max_uncompressed = 128 * 1024; |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 412 | int i; |
| 413 | int will_compress; |
Li Zefan | 261507a0 | 2010-12-17 14:21:50 +0800 | [diff] [blame] | 414 | int compress_type = root->fs_info->compress_type; |
Chris Mason | 4adaa61 | 2013-03-26 13:07:00 -0400 | [diff] [blame] | 415 | int redirty = 0; |
Chris Mason | b888db2 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 416 | |
Liu Bo | 4cb13e5 | 2012-03-29 09:57:45 -0400 | [diff] [blame] | 417 | /* if this is a small write inside eof, kick off a defrag */ |
| 418 | if ((end - start + 1) < 16 * 1024 && |
| 419 | (start > 0 || end + 1 < BTRFS_I(inode)->disk_i_size)) |
Chris Mason | 4cb5300 | 2011-05-24 15:35:30 -0400 | [diff] [blame] | 420 | btrfs_add_inode_defrag(NULL, inode); |
| 421 | |
Chris Mason | 42dc7ba | 2008-12-15 11:44:56 -0500 | [diff] [blame] | 422 | actual_end = min_t(u64, isize, end + 1); |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 423 | again: |
| 424 | will_compress = 0; |
| 425 | nr_pages = (end >> PAGE_CACHE_SHIFT) - (start >> PAGE_CACHE_SHIFT) + 1; |
| 426 | nr_pages = min(nr_pages, (128 * 1024UL) / PAGE_CACHE_SIZE); |
| 427 | |
Chris Mason | f03d9301f | 2009-02-04 09:31:06 -0500 | [diff] [blame] | 428 | /* |
| 429 | * we don't want to send crud past the end of i_size through |
| 430 | * compression, that's just a waste of CPU time. So, if the |
| 431 | * end of the file is before the start of our current |
| 432 | * requested range of bytes, we bail out to the uncompressed |
| 433 | * cleanup code that can deal with all of this. |
| 434 | * |
| 435 | * It isn't really the fastest way to fix things, but this is a |
| 436 | * very uncommon corner. |
| 437 | */ |
| 438 | if (actual_end <= start) |
| 439 | goto cleanup_and_bail_uncompressed; |
| 440 | |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 441 | total_compressed = actual_end - start; |
| 442 | |
Shilong Wang | 4bcbb33 | 2014-10-07 18:44:35 -0400 | [diff] [blame] | 443 | /* |
| 444 | * skip compression for a small file range(<=blocksize) that |
| 445 | * isn't an inline extent, since it dosen't save disk space at all. |
| 446 | */ |
| 447 | if (total_compressed <= blocksize && |
| 448 | (start > 0 || end + 1 < BTRFS_I(inode)->disk_i_size)) |
| 449 | goto cleanup_and_bail_uncompressed; |
| 450 | |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 451 | /* we want to make sure that amount of ram required to uncompress |
| 452 | * an extent is reasonable, so we limit the total size in ram |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 453 | * of a compressed extent to 128k. This is a crucial number |
| 454 | * because it also controls how easily we can spread reads across |
| 455 | * cpus for decompression. |
| 456 | * |
| 457 | * We also want to make sure the amount of IO required to do |
| 458 | * a random read is reasonably small, so we limit the size of |
| 459 | * a compressed extent to 128k. |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 460 | */ |
| 461 | total_compressed = min(total_compressed, max_uncompressed); |
Qu Wenruo | fda2832 | 2013-02-26 08:10:22 +0000 | [diff] [blame] | 462 | num_bytes = ALIGN(end - start + 1, blocksize); |
Chris Mason | be20aa9 | 2007-12-17 20:14:01 -0500 | [diff] [blame] | 463 | num_bytes = max(blocksize, num_bytes); |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 464 | total_in = 0; |
| 465 | ret = 0; |
Chris Mason | db94535 | 2007-10-15 16:15:53 -0400 | [diff] [blame] | 466 | |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 467 | /* |
| 468 | * we do compression for mount -o compress and when the |
| 469 | * inode has not been flagged as nocompress. This flag can |
| 470 | * change at any time if we discover bad compression ratios. |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 471 | */ |
Wang Shilong | f79707b | 2014-07-17 11:44:09 +0800 | [diff] [blame] | 472 | if (inode_need_compress(inode)) { |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 473 | WARN_ON(pages); |
David Sterba | 31e818f | 2015-02-20 18:00:26 +0100 | [diff] [blame] | 474 | pages = kcalloc(nr_pages, sizeof(struct page *), GFP_NOFS); |
Li Zefan | 560f7d7 | 2011-09-08 10:22:01 +0800 | [diff] [blame] | 475 | if (!pages) { |
| 476 | /* just bail out to the uncompressed code */ |
| 477 | goto cont; |
| 478 | } |
Chris Mason | 179e29e | 2007-11-01 11:28:41 -0400 | [diff] [blame] | 479 | |
Li Zefan | 261507a0 | 2010-12-17 14:21:50 +0800 | [diff] [blame] | 480 | if (BTRFS_I(inode)->force_compress) |
| 481 | compress_type = BTRFS_I(inode)->force_compress; |
| 482 | |
Chris Mason | 4adaa61 | 2013-03-26 13:07:00 -0400 | [diff] [blame] | 483 | /* |
| 484 | * we need to call clear_page_dirty_for_io on each |
| 485 | * page in the range. Otherwise applications with the file |
| 486 | * mmap'd can wander in and change the page contents while |
| 487 | * we are compressing them. |
| 488 | * |
| 489 | * If the compression fails for any reason, we set the pages |
| 490 | * dirty again later on. |
| 491 | */ |
| 492 | extent_range_clear_dirty_for_io(inode, start, end); |
| 493 | redirty = 1; |
Li Zefan | 261507a0 | 2010-12-17 14:21:50 +0800 | [diff] [blame] | 494 | ret = btrfs_compress_pages(compress_type, |
| 495 | inode->i_mapping, start, |
| 496 | total_compressed, pages, |
| 497 | nr_pages, &nr_pages_ret, |
| 498 | &total_in, |
| 499 | &total_compressed, |
| 500 | max_compressed); |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 501 | |
| 502 | if (!ret) { |
| 503 | unsigned long offset = total_compressed & |
| 504 | (PAGE_CACHE_SIZE - 1); |
| 505 | struct page *page = pages[nr_pages_ret - 1]; |
| 506 | char *kaddr; |
| 507 | |
| 508 | /* zero the tail end of the last page, we might be |
| 509 | * sending it down to disk |
| 510 | */ |
| 511 | if (offset) { |
Cong Wang | 7ac687d | 2011-11-25 23:14:28 +0800 | [diff] [blame] | 512 | kaddr = kmap_atomic(page); |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 513 | memset(kaddr + offset, 0, |
| 514 | PAGE_CACHE_SIZE - offset); |
Cong Wang | 7ac687d | 2011-11-25 23:14:28 +0800 | [diff] [blame] | 515 | kunmap_atomic(kaddr); |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 516 | } |
| 517 | will_compress = 1; |
| 518 | } |
| 519 | } |
Li Zefan | 560f7d7 | 2011-09-08 10:22:01 +0800 | [diff] [blame] | 520 | cont: |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 521 | if (start == 0) { |
| 522 | /* lets try to make an inline extent */ |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 523 | if (ret || total_in < (actual_end - start)) { |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 524 | /* we didn't compress the entire range, try |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 525 | * to make an uncompressed inline extent. |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 526 | */ |
Josef Bacik | 0036158 | 2013-08-14 14:02:47 -0400 | [diff] [blame] | 527 | ret = cow_file_range_inline(root, inode, start, end, |
| 528 | 0, 0, NULL); |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 529 | } else { |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 530 | /* try making a compressed inline extent */ |
Josef Bacik | 0036158 | 2013-08-14 14:02:47 -0400 | [diff] [blame] | 531 | ret = cow_file_range_inline(root, inode, start, end, |
Li Zefan | fe3f566 | 2011-03-28 08:30:38 +0000 | [diff] [blame] | 532 | total_compressed, |
| 533 | compress_type, pages); |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 534 | } |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 535 | if (ret <= 0) { |
Josef Bacik | 151a41b | 2013-07-29 13:22:24 -0400 | [diff] [blame] | 536 | unsigned long clear_flags = EXTENT_DELALLOC | |
| 537 | EXTENT_DEFRAG; |
Filipe Manana | e6eb431 | 2014-10-10 10:45:12 +0100 | [diff] [blame] | 538 | unsigned long page_error_op; |
| 539 | |
Josef Bacik | 151a41b | 2013-07-29 13:22:24 -0400 | [diff] [blame] | 540 | clear_flags |= (ret < 0) ? EXTENT_DO_ACCOUNTING : 0; |
Filipe Manana | e6eb431 | 2014-10-10 10:45:12 +0100 | [diff] [blame] | 541 | page_error_op = ret < 0 ? PAGE_SET_ERROR : 0; |
Josef Bacik | 151a41b | 2013-07-29 13:22:24 -0400 | [diff] [blame] | 542 | |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 543 | /* |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 544 | * inline extent creation worked or returned error, |
| 545 | * we don't need to create any more async work items. |
| 546 | * Unlock and free up our temp pages. |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 547 | */ |
Josef Bacik | c2790a2 | 2013-07-29 11:20:47 -0400 | [diff] [blame] | 548 | extent_clear_unlock_delalloc(inode, start, end, NULL, |
Josef Bacik | 151a41b | 2013-07-29 13:22:24 -0400 | [diff] [blame] | 549 | clear_flags, PAGE_UNLOCK | |
Josef Bacik | c2790a2 | 2013-07-29 11:20:47 -0400 | [diff] [blame] | 550 | PAGE_CLEAR_DIRTY | |
| 551 | PAGE_SET_WRITEBACK | |
Filipe Manana | e6eb431 | 2014-10-10 10:45:12 +0100 | [diff] [blame] | 552 | page_error_op | |
Josef Bacik | c2790a2 | 2013-07-29 11:20:47 -0400 | [diff] [blame] | 553 | PAGE_END_WRITEBACK); |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 554 | goto free_pages_out; |
| 555 | } |
| 556 | } |
| 557 | |
| 558 | if (will_compress) { |
| 559 | /* |
| 560 | * we aren't doing an inline extent round the compressed size |
| 561 | * up to a block size boundary so the allocator does sane |
| 562 | * things |
| 563 | */ |
Qu Wenruo | fda2832 | 2013-02-26 08:10:22 +0000 | [diff] [blame] | 564 | total_compressed = ALIGN(total_compressed, blocksize); |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 565 | |
| 566 | /* |
| 567 | * one last check to make sure the compression is really a |
| 568 | * win, compare the page count read with the blocks on disk |
| 569 | */ |
Qu Wenruo | fda2832 | 2013-02-26 08:10:22 +0000 | [diff] [blame] | 570 | total_in = ALIGN(total_in, PAGE_CACHE_SIZE); |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 571 | if (total_compressed >= total_in) { |
| 572 | will_compress = 0; |
| 573 | } else { |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 574 | num_bytes = total_in; |
| 575 | } |
| 576 | } |
| 577 | if (!will_compress && pages) { |
| 578 | /* |
| 579 | * the compression code ran but failed to make things smaller, |
| 580 | * free any pages it allocated and our page pointer array |
| 581 | */ |
| 582 | for (i = 0; i < nr_pages_ret; i++) { |
Chris Mason | 70b99e6 | 2008-10-31 12:46:39 -0400 | [diff] [blame] | 583 | WARN_ON(pages[i]->mapping); |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 584 | page_cache_release(pages[i]); |
| 585 | } |
| 586 | kfree(pages); |
| 587 | pages = NULL; |
| 588 | total_compressed = 0; |
| 589 | nr_pages_ret = 0; |
| 590 | |
| 591 | /* flag the file so we don't compress in the future */ |
Chris Mason | 1e701a3 | 2010-03-11 09:42:04 -0500 | [diff] [blame] | 592 | if (!btrfs_test_opt(root, FORCE_COMPRESS) && |
| 593 | !(BTRFS_I(inode)->force_compress)) { |
Chris Mason | a555f81 | 2010-01-28 16:18:15 -0500 | [diff] [blame] | 594 | BTRFS_I(inode)->flags |= BTRFS_INODE_NOCOMPRESS; |
Chris Mason | 1e701a3 | 2010-03-11 09:42:04 -0500 | [diff] [blame] | 595 | } |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 596 | } |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 597 | if (will_compress) { |
| 598 | *num_added += 1; |
| 599 | |
| 600 | /* the async work queues will take care of doing actual |
| 601 | * allocation on disk for these compressed pages, |
| 602 | * and will submit them to the elevator. |
| 603 | */ |
| 604 | add_async_extent(async_cow, start, num_bytes, |
Li Zefan | 261507a0 | 2010-12-17 14:21:50 +0800 | [diff] [blame] | 605 | total_compressed, pages, nr_pages_ret, |
| 606 | compress_type); |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 607 | |
Yan, Zheng | 24ae636 | 2010-12-06 07:02:36 +0000 | [diff] [blame] | 608 | if (start + num_bytes < end) { |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 609 | start += num_bytes; |
| 610 | pages = NULL; |
| 611 | cond_resched(); |
| 612 | goto again; |
| 613 | } |
| 614 | } else { |
Chris Mason | f03d9301f | 2009-02-04 09:31:06 -0500 | [diff] [blame] | 615 | cleanup_and_bail_uncompressed: |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 616 | /* |
| 617 | * No compression, but we still need to write the pages in |
| 618 | * the file we've been given so far. redirty the locked |
| 619 | * page if it corresponds to our extent and set things up |
| 620 | * for the async work queue to run cow_file_range to do |
| 621 | * the normal delalloc dance |
| 622 | */ |
| 623 | if (page_offset(locked_page) >= start && |
| 624 | page_offset(locked_page) <= end) { |
| 625 | __set_page_dirty_nobuffers(locked_page); |
| 626 | /* unlocked later on in the async handlers */ |
| 627 | } |
Chris Mason | 4adaa61 | 2013-03-26 13:07:00 -0400 | [diff] [blame] | 628 | if (redirty) |
| 629 | extent_range_redirty_for_io(inode, start, end); |
Li Zefan | 261507a0 | 2010-12-17 14:21:50 +0800 | [diff] [blame] | 630 | add_async_extent(async_cow, start, end - start + 1, |
| 631 | 0, NULL, 0, BTRFS_COMPRESS_NONE); |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 632 | *num_added += 1; |
| 633 | } |
| 634 | |
Filipe Manana | c44f649 | 2014-10-09 21:15:44 +0100 | [diff] [blame] | 635 | return; |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 636 | |
| 637 | free_pages_out: |
| 638 | for (i = 0; i < nr_pages_ret; i++) { |
| 639 | WARN_ON(pages[i]->mapping); |
| 640 | page_cache_release(pages[i]); |
| 641 | } |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 642 | kfree(pages); |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 643 | } |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 644 | |
Filipe Manana | 40ae837 | 2014-10-06 22:14:24 +0100 | [diff] [blame] | 645 | static void free_async_extent_pages(struct async_extent *async_extent) |
| 646 | { |
| 647 | int i; |
| 648 | |
| 649 | if (!async_extent->pages) |
| 650 | return; |
| 651 | |
| 652 | for (i = 0; i < async_extent->nr_pages; i++) { |
| 653 | WARN_ON(async_extent->pages[i]->mapping); |
| 654 | page_cache_release(async_extent->pages[i]); |
| 655 | } |
| 656 | kfree(async_extent->pages); |
| 657 | async_extent->nr_pages = 0; |
| 658 | async_extent->pages = NULL; |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 659 | } |
| 660 | |
| 661 | /* |
| 662 | * phase two of compressed writeback. This is the ordered portion |
| 663 | * of the code, which only gets called in the order the work was |
| 664 | * queued. We walk all the async extents created by compress_file_range |
| 665 | * and send them down to the disk. |
| 666 | */ |
Filipe Manana | dec8f17 | 2014-10-06 22:14:26 +0100 | [diff] [blame] | 667 | static noinline void submit_compressed_extents(struct inode *inode, |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 668 | struct async_cow *async_cow) |
| 669 | { |
| 670 | struct async_extent *async_extent; |
| 671 | u64 alloc_hint = 0; |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 672 | struct btrfs_key ins; |
| 673 | struct extent_map *em; |
| 674 | struct btrfs_root *root = BTRFS_I(inode)->root; |
| 675 | struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree; |
| 676 | struct extent_io_tree *io_tree; |
Josef Bacik | f5a84ee3 | 2009-11-10 21:23:48 -0500 | [diff] [blame] | 677 | int ret = 0; |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 678 | |
Josef Bacik | 3e04e7f | 2013-02-06 16:49:15 -0500 | [diff] [blame] | 679 | again: |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 680 | while (!list_empty(&async_cow->extents)) { |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 681 | async_extent = list_entry(async_cow->extents.next, |
| 682 | struct async_extent, list); |
| 683 | list_del(&async_extent->list); |
| 684 | |
| 685 | io_tree = &BTRFS_I(inode)->io_tree; |
| 686 | |
Josef Bacik | f5a84ee3 | 2009-11-10 21:23:48 -0500 | [diff] [blame] | 687 | retry: |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 688 | /* did the compression code fall back to uncompressed IO? */ |
| 689 | if (!async_extent->pages) { |
| 690 | int page_started = 0; |
| 691 | unsigned long nr_written = 0; |
| 692 | |
| 693 | lock_extent(io_tree, async_extent->start, |
Josef Bacik | 2ac55d4 | 2010-02-03 19:33:23 +0000 | [diff] [blame] | 694 | async_extent->start + |
Jeff Mahoney | d008237 | 2012-03-01 14:57:19 +0100 | [diff] [blame] | 695 | async_extent->ram_size - 1); |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 696 | |
| 697 | /* allocate blocks */ |
Josef Bacik | f5a84ee3 | 2009-11-10 21:23:48 -0500 | [diff] [blame] | 698 | ret = cow_file_range(inode, async_cow->locked_page, |
| 699 | async_extent->start, |
| 700 | async_extent->start + |
| 701 | async_extent->ram_size - 1, |
| 702 | &page_started, &nr_written, 0); |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 703 | |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 704 | /* JDM XXX */ |
| 705 | |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 706 | /* |
| 707 | * if page_started, cow_file_range inserted an |
| 708 | * inline extent and took care of all the unlocking |
| 709 | * and IO for us. Otherwise, we need to submit |
| 710 | * all those pages down to the drive. |
| 711 | */ |
Josef Bacik | f5a84ee3 | 2009-11-10 21:23:48 -0500 | [diff] [blame] | 712 | if (!page_started && !ret) |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 713 | extent_write_locked_range(io_tree, |
| 714 | inode, async_extent->start, |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 715 | async_extent->start + |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 716 | async_extent->ram_size - 1, |
| 717 | btrfs_get_extent, |
| 718 | WB_SYNC_ALL); |
Josef Bacik | 3e04e7f | 2013-02-06 16:49:15 -0500 | [diff] [blame] | 719 | else if (ret) |
| 720 | unlock_page(async_cow->locked_page); |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 721 | kfree(async_extent); |
| 722 | cond_resched(); |
| 723 | continue; |
| 724 | } |
| 725 | |
| 726 | lock_extent(io_tree, async_extent->start, |
Jeff Mahoney | d008237 | 2012-03-01 14:57:19 +0100 | [diff] [blame] | 727 | async_extent->start + async_extent->ram_size - 1); |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 728 | |
Josef Bacik | 0036158 | 2013-08-14 14:02:47 -0400 | [diff] [blame] | 729 | ret = btrfs_reserve_extent(root, |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 730 | async_extent->compressed_size, |
| 731 | async_extent->compressed_size, |
Miao Xie | e570fd2 | 2014-06-19 10:42:50 +0800 | [diff] [blame] | 732 | 0, alloc_hint, &ins, 1, 1); |
Josef Bacik | f5a84ee3 | 2009-11-10 21:23:48 -0500 | [diff] [blame] | 733 | if (ret) { |
Filipe Manana | 40ae837 | 2014-10-06 22:14:24 +0100 | [diff] [blame] | 734 | free_async_extent_pages(async_extent); |
Josef Bacik | 3e04e7f | 2013-02-06 16:49:15 -0500 | [diff] [blame] | 735 | |
Josef Bacik | fdf8e2e | 2013-06-14 16:58:23 -0400 | [diff] [blame] | 736 | if (ret == -ENOSPC) { |
| 737 | unlock_extent(io_tree, async_extent->start, |
| 738 | async_extent->start + |
| 739 | async_extent->ram_size - 1); |
Liu Bo | ce62003 | 2014-07-24 22:48:05 +0800 | [diff] [blame] | 740 | |
| 741 | /* |
| 742 | * we need to redirty the pages if we decide to |
| 743 | * fallback to uncompressed IO, otherwise we |
| 744 | * will not submit these pages down to lower |
| 745 | * layers. |
| 746 | */ |
| 747 | extent_range_redirty_for_io(inode, |
| 748 | async_extent->start, |
| 749 | async_extent->start + |
| 750 | async_extent->ram_size - 1); |
| 751 | |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 752 | goto retry; |
Josef Bacik | fdf8e2e | 2013-06-14 16:58:23 -0400 | [diff] [blame] | 753 | } |
Josef Bacik | 3e04e7f | 2013-02-06 16:49:15 -0500 | [diff] [blame] | 754 | goto out_free; |
Josef Bacik | f5a84ee3 | 2009-11-10 21:23:48 -0500 | [diff] [blame] | 755 | } |
Yan, Zheng | c216775 | 2009-11-12 09:34:21 +0000 | [diff] [blame] | 756 | /* |
| 757 | * here we're doing allocation and writeback of the |
| 758 | * compressed pages |
| 759 | */ |
| 760 | btrfs_drop_extent_cache(inode, async_extent->start, |
| 761 | async_extent->start + |
| 762 | async_extent->ram_size - 1, 0); |
| 763 | |
David Sterba | 172ddd6 | 2011-04-21 00:48:27 +0200 | [diff] [blame] | 764 | em = alloc_extent_map(); |
Liu Bo | b9aa55b | 2013-05-14 02:12:15 +0000 | [diff] [blame] | 765 | if (!em) { |
| 766 | ret = -ENOMEM; |
Josef Bacik | 3e04e7f | 2013-02-06 16:49:15 -0500 | [diff] [blame] | 767 | goto out_free_reserve; |
Liu Bo | b9aa55b | 2013-05-14 02:12:15 +0000 | [diff] [blame] | 768 | } |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 769 | em->start = async_extent->start; |
| 770 | em->len = async_extent->ram_size; |
Chris Mason | 445a694 | 2008-11-10 11:53:33 -0500 | [diff] [blame] | 771 | em->orig_start = em->start; |
Josef Bacik | 2ab28f3 | 2012-10-12 15:27:49 -0400 | [diff] [blame] | 772 | em->mod_start = em->start; |
| 773 | em->mod_len = em->len; |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 774 | |
| 775 | em->block_start = ins.objectid; |
| 776 | em->block_len = ins.offset; |
Josef Bacik | b493968 | 2012-12-03 10:31:19 -0500 | [diff] [blame] | 777 | em->orig_block_len = ins.offset; |
Josef Bacik | cc95bef | 2013-04-04 14:31:27 -0400 | [diff] [blame] | 778 | em->ram_bytes = async_extent->ram_size; |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 779 | em->bdev = root->fs_info->fs_devices->latest_bdev; |
Li Zefan | 261507a0 | 2010-12-17 14:21:50 +0800 | [diff] [blame] | 780 | em->compress_type = async_extent->compress_type; |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 781 | set_bit(EXTENT_FLAG_PINNED, &em->flags); |
| 782 | set_bit(EXTENT_FLAG_COMPRESSED, &em->flags); |
Josef Bacik | 70c8a91 | 2012-10-11 16:54:30 -0400 | [diff] [blame] | 783 | em->generation = -1; |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 784 | |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 785 | while (1) { |
Chris Mason | 890871b | 2009-09-02 16:24:52 -0400 | [diff] [blame] | 786 | write_lock(&em_tree->lock); |
Josef Bacik | 09a2a8f9 | 2013-04-05 16:51:15 -0400 | [diff] [blame] | 787 | ret = add_extent_mapping(em_tree, em, 1); |
Chris Mason | 890871b | 2009-09-02 16:24:52 -0400 | [diff] [blame] | 788 | write_unlock(&em_tree->lock); |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 789 | if (ret != -EEXIST) { |
| 790 | free_extent_map(em); |
| 791 | break; |
| 792 | } |
| 793 | btrfs_drop_extent_cache(inode, async_extent->start, |
| 794 | async_extent->start + |
| 795 | async_extent->ram_size - 1, 0); |
| 796 | } |
| 797 | |
Josef Bacik | 3e04e7f | 2013-02-06 16:49:15 -0500 | [diff] [blame] | 798 | if (ret) |
| 799 | goto out_free_reserve; |
| 800 | |
Li Zefan | 261507a0 | 2010-12-17 14:21:50 +0800 | [diff] [blame] | 801 | ret = btrfs_add_ordered_extent_compress(inode, |
| 802 | async_extent->start, |
| 803 | ins.objectid, |
| 804 | async_extent->ram_size, |
| 805 | ins.offset, |
| 806 | BTRFS_ORDERED_COMPRESSED, |
| 807 | async_extent->compress_type); |
Filipe Manana | d9f8596 | 2014-08-25 10:43:00 +0100 | [diff] [blame] | 808 | if (ret) { |
| 809 | btrfs_drop_extent_cache(inode, async_extent->start, |
| 810 | async_extent->start + |
| 811 | async_extent->ram_size - 1, 0); |
Josef Bacik | 3e04e7f | 2013-02-06 16:49:15 -0500 | [diff] [blame] | 812 | goto out_free_reserve; |
Filipe Manana | d9f8596 | 2014-08-25 10:43:00 +0100 | [diff] [blame] | 813 | } |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 814 | |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 815 | /* |
| 816 | * clear dirty, set writeback and unlock the pages. |
| 817 | */ |
Josef Bacik | c2790a2 | 2013-07-29 11:20:47 -0400 | [diff] [blame] | 818 | extent_clear_unlock_delalloc(inode, async_extent->start, |
Chris Mason | a791e35 | 2009-10-08 11:27:10 -0400 | [diff] [blame] | 819 | async_extent->start + |
| 820 | async_extent->ram_size - 1, |
Josef Bacik | 151a41b | 2013-07-29 13:22:24 -0400 | [diff] [blame] | 821 | NULL, EXTENT_LOCKED | EXTENT_DELALLOC, |
| 822 | PAGE_UNLOCK | PAGE_CLEAR_DIRTY | |
Josef Bacik | c2790a2 | 2013-07-29 11:20:47 -0400 | [diff] [blame] | 823 | PAGE_SET_WRITEBACK); |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 824 | ret = btrfs_submit_compressed_write(inode, |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 825 | async_extent->start, |
| 826 | async_extent->ram_size, |
| 827 | ins.objectid, |
| 828 | ins.offset, async_extent->pages, |
| 829 | async_extent->nr_pages); |
Filipe Manana | fce2a4e | 2014-10-06 22:14:23 +0100 | [diff] [blame] | 830 | if (ret) { |
| 831 | struct extent_io_tree *tree = &BTRFS_I(inode)->io_tree; |
| 832 | struct page *p = async_extent->pages[0]; |
| 833 | const u64 start = async_extent->start; |
| 834 | const u64 end = start + async_extent->ram_size - 1; |
| 835 | |
| 836 | p->mapping = inode->i_mapping; |
| 837 | tree->ops->writepage_end_io_hook(p, start, end, |
| 838 | NULL, 0); |
| 839 | p->mapping = NULL; |
| 840 | extent_clear_unlock_delalloc(inode, start, end, NULL, 0, |
| 841 | PAGE_END_WRITEBACK | |
| 842 | PAGE_SET_ERROR); |
Filipe Manana | 40ae837 | 2014-10-06 22:14:24 +0100 | [diff] [blame] | 843 | free_async_extent_pages(async_extent); |
Filipe Manana | fce2a4e | 2014-10-06 22:14:23 +0100 | [diff] [blame] | 844 | } |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 845 | alloc_hint = ins.objectid + ins.offset; |
| 846 | kfree(async_extent); |
| 847 | cond_resched(); |
| 848 | } |
Filipe Manana | dec8f17 | 2014-10-06 22:14:26 +0100 | [diff] [blame] | 849 | return; |
Josef Bacik | 3e04e7f | 2013-02-06 16:49:15 -0500 | [diff] [blame] | 850 | out_free_reserve: |
Miao Xie | e570fd2 | 2014-06-19 10:42:50 +0800 | [diff] [blame] | 851 | btrfs_free_reserved_extent(root, ins.objectid, ins.offset, 1); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 852 | out_free: |
Josef Bacik | c2790a2 | 2013-07-29 11:20:47 -0400 | [diff] [blame] | 853 | extent_clear_unlock_delalloc(inode, async_extent->start, |
Josef Bacik | 3e04e7f | 2013-02-06 16:49:15 -0500 | [diff] [blame] | 854 | async_extent->start + |
| 855 | async_extent->ram_size - 1, |
Josef Bacik | c2790a2 | 2013-07-29 11:20:47 -0400 | [diff] [blame] | 856 | NULL, EXTENT_LOCKED | EXTENT_DELALLOC | |
Josef Bacik | 151a41b | 2013-07-29 13:22:24 -0400 | [diff] [blame] | 857 | EXTENT_DEFRAG | EXTENT_DO_ACCOUNTING, |
| 858 | PAGE_UNLOCK | PAGE_CLEAR_DIRTY | |
Filipe Manana | 704de49 | 2014-10-06 22:14:22 +0100 | [diff] [blame] | 859 | PAGE_SET_WRITEBACK | PAGE_END_WRITEBACK | |
| 860 | PAGE_SET_ERROR); |
Filipe Manana | 40ae837 | 2014-10-06 22:14:24 +0100 | [diff] [blame] | 861 | free_async_extent_pages(async_extent); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 862 | kfree(async_extent); |
Josef Bacik | 3e04e7f | 2013-02-06 16:49:15 -0500 | [diff] [blame] | 863 | goto again; |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 864 | } |
| 865 | |
Josef Bacik | 4b46fce | 2010-05-23 11:00:55 -0400 | [diff] [blame] | 866 | static u64 get_extent_allocation_hint(struct inode *inode, u64 start, |
| 867 | u64 num_bytes) |
| 868 | { |
| 869 | struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree; |
| 870 | struct extent_map *em; |
| 871 | u64 alloc_hint = 0; |
| 872 | |
| 873 | read_lock(&em_tree->lock); |
| 874 | em = search_extent_mapping(em_tree, start, num_bytes); |
| 875 | if (em) { |
| 876 | /* |
| 877 | * if block start isn't an actual block number then find the |
| 878 | * first block in this inode and use that as a hint. If that |
| 879 | * block is also bogus then just don't worry about it. |
| 880 | */ |
| 881 | if (em->block_start >= EXTENT_MAP_LAST_BYTE) { |
| 882 | free_extent_map(em); |
| 883 | em = search_extent_mapping(em_tree, 0, 0); |
| 884 | if (em && em->block_start < EXTENT_MAP_LAST_BYTE) |
| 885 | alloc_hint = em->block_start; |
| 886 | if (em) |
| 887 | free_extent_map(em); |
| 888 | } else { |
| 889 | alloc_hint = em->block_start; |
| 890 | free_extent_map(em); |
| 891 | } |
| 892 | } |
| 893 | read_unlock(&em_tree->lock); |
| 894 | |
| 895 | return alloc_hint; |
| 896 | } |
| 897 | |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 898 | /* |
| 899 | * when extent_io.c finds a delayed allocation range in the file, |
| 900 | * the call backs end up in this code. The basic idea is to |
| 901 | * allocate extents on disk for the range, and create ordered data structs |
| 902 | * in ram to track those extents. |
| 903 | * |
| 904 | * locked_page is the page that writepage had locked already. We use |
| 905 | * it to make sure we don't do extra locks or unlocks. |
| 906 | * |
| 907 | * *page_started is set to one if we unlock locked_page and do everything |
| 908 | * required to start IO on it. It may be clean and already done with |
| 909 | * IO when we return. |
| 910 | */ |
Josef Bacik | 0036158 | 2013-08-14 14:02:47 -0400 | [diff] [blame] | 911 | static noinline int cow_file_range(struct inode *inode, |
| 912 | struct page *locked_page, |
| 913 | u64 start, u64 end, int *page_started, |
| 914 | unsigned long *nr_written, |
| 915 | int unlock) |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 916 | { |
Josef Bacik | 0036158 | 2013-08-14 14:02:47 -0400 | [diff] [blame] | 917 | struct btrfs_root *root = BTRFS_I(inode)->root; |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 918 | u64 alloc_hint = 0; |
| 919 | u64 num_bytes; |
| 920 | unsigned long ram_size; |
| 921 | u64 disk_num_bytes; |
| 922 | u64 cur_alloc_size; |
| 923 | u64 blocksize = root->sectorsize; |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 924 | struct btrfs_key ins; |
| 925 | struct extent_map *em; |
| 926 | struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree; |
| 927 | int ret = 0; |
| 928 | |
Josef Bacik | 02ecd2c | 2013-10-25 16:19:08 -0400 | [diff] [blame] | 929 | if (btrfs_is_free_space_inode(inode)) { |
| 930 | WARN_ON_ONCE(1); |
Josef Bacik | 29bce2f | 2014-02-07 12:21:23 -0500 | [diff] [blame] | 931 | ret = -EINVAL; |
| 932 | goto out_unlock; |
Josef Bacik | 02ecd2c | 2013-10-25 16:19:08 -0400 | [diff] [blame] | 933 | } |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 934 | |
Qu Wenruo | fda2832 | 2013-02-26 08:10:22 +0000 | [diff] [blame] | 935 | num_bytes = ALIGN(end - start + 1, blocksize); |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 936 | num_bytes = max(blocksize, num_bytes); |
| 937 | disk_num_bytes = num_bytes; |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 938 | |
Chris Mason | 4cb5300 | 2011-05-24 15:35:30 -0400 | [diff] [blame] | 939 | /* if this is a small write inside eof, kick off defrag */ |
Liu Bo | 4cb13e5 | 2012-03-29 09:57:45 -0400 | [diff] [blame] | 940 | if (num_bytes < 64 * 1024 && |
| 941 | (start > 0 || end + 1 < BTRFS_I(inode)->disk_i_size)) |
Josef Bacik | 0036158 | 2013-08-14 14:02:47 -0400 | [diff] [blame] | 942 | btrfs_add_inode_defrag(NULL, inode); |
Chris Mason | 4cb5300 | 2011-05-24 15:35:30 -0400 | [diff] [blame] | 943 | |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 944 | if (start == 0) { |
| 945 | /* lets try to make an inline extent */ |
Josef Bacik | 0036158 | 2013-08-14 14:02:47 -0400 | [diff] [blame] | 946 | ret = cow_file_range_inline(root, inode, start, end, 0, 0, |
| 947 | NULL); |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 948 | if (ret == 0) { |
Josef Bacik | c2790a2 | 2013-07-29 11:20:47 -0400 | [diff] [blame] | 949 | extent_clear_unlock_delalloc(inode, start, end, NULL, |
| 950 | EXTENT_LOCKED | EXTENT_DELALLOC | |
Josef Bacik | 151a41b | 2013-07-29 13:22:24 -0400 | [diff] [blame] | 951 | EXTENT_DEFRAG, PAGE_UNLOCK | |
Josef Bacik | c2790a2 | 2013-07-29 11:20:47 -0400 | [diff] [blame] | 952 | PAGE_CLEAR_DIRTY | PAGE_SET_WRITEBACK | |
| 953 | PAGE_END_WRITEBACK); |
Yan, Zheng | c216775 | 2009-11-12 09:34:21 +0000 | [diff] [blame] | 954 | |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 955 | *nr_written = *nr_written + |
| 956 | (end - start + PAGE_CACHE_SIZE) / PAGE_CACHE_SIZE; |
| 957 | *page_started = 1; |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 958 | goto out; |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 959 | } else if (ret < 0) { |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 960 | goto out_unlock; |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 961 | } |
| 962 | } |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 963 | |
| 964 | BUG_ON(disk_num_bytes > |
David Sterba | 6c41761 | 2011-04-13 15:41:04 +0200 | [diff] [blame] | 965 | btrfs_super_total_bytes(root->fs_info->super_copy)); |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 966 | |
Josef Bacik | 4b46fce | 2010-05-23 11:00:55 -0400 | [diff] [blame] | 967 | alloc_hint = get_extent_allocation_hint(inode, start, num_bytes); |
Zheng Yan | 5b21f2e | 2008-09-26 10:05:38 -0400 | [diff] [blame] | 968 | btrfs_drop_extent_cache(inode, start, start + num_bytes - 1, 0); |
Chris Mason | 3b95151 | 2008-04-17 11:29:12 -0400 | [diff] [blame] | 969 | |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 970 | while (disk_num_bytes > 0) { |
Chris Mason | a791e35 | 2009-10-08 11:27:10 -0400 | [diff] [blame] | 971 | unsigned long op; |
| 972 | |
Josef Bacik | 287a0ab | 2010-03-19 18:07:23 +0000 | [diff] [blame] | 973 | cur_alloc_size = disk_num_bytes; |
Josef Bacik | 0036158 | 2013-08-14 14:02:47 -0400 | [diff] [blame] | 974 | ret = btrfs_reserve_extent(root, cur_alloc_size, |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 975 | root->sectorsize, 0, alloc_hint, |
Miao Xie | e570fd2 | 2014-06-19 10:42:50 +0800 | [diff] [blame] | 976 | &ins, 1, 1); |
Josef Bacik | 0036158 | 2013-08-14 14:02:47 -0400 | [diff] [blame] | 977 | if (ret < 0) |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 978 | goto out_unlock; |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 979 | |
David Sterba | 172ddd6 | 2011-04-21 00:48:27 +0200 | [diff] [blame] | 980 | em = alloc_extent_map(); |
Liu Bo | b9aa55b | 2013-05-14 02:12:15 +0000 | [diff] [blame] | 981 | if (!em) { |
| 982 | ret = -ENOMEM; |
Liu Bo | ace68ba | 2013-04-22 10:53:47 +0000 | [diff] [blame] | 983 | goto out_reserve; |
Liu Bo | b9aa55b | 2013-05-14 02:12:15 +0000 | [diff] [blame] | 984 | } |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 985 | em->start = start; |
Chris Mason | 445a694 | 2008-11-10 11:53:33 -0500 | [diff] [blame] | 986 | em->orig_start = em->start; |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 987 | ram_size = ins.offset; |
| 988 | em->len = ins.offset; |
Josef Bacik | 2ab28f3 | 2012-10-12 15:27:49 -0400 | [diff] [blame] | 989 | em->mod_start = em->start; |
| 990 | em->mod_len = em->len; |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 991 | |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 992 | em->block_start = ins.objectid; |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 993 | em->block_len = ins.offset; |
Josef Bacik | b493968 | 2012-12-03 10:31:19 -0500 | [diff] [blame] | 994 | em->orig_block_len = ins.offset; |
Josef Bacik | cc95bef | 2013-04-04 14:31:27 -0400 | [diff] [blame] | 995 | em->ram_bytes = ram_size; |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 996 | em->bdev = root->fs_info->fs_devices->latest_bdev; |
Chris Mason | 7f3c74f | 2008-07-18 12:01:11 -0400 | [diff] [blame] | 997 | set_bit(EXTENT_FLAG_PINNED, &em->flags); |
Josef Bacik | 70c8a91 | 2012-10-11 16:54:30 -0400 | [diff] [blame] | 998 | em->generation = -1; |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 999 | |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 1000 | while (1) { |
Chris Mason | 890871b | 2009-09-02 16:24:52 -0400 | [diff] [blame] | 1001 | write_lock(&em_tree->lock); |
Josef Bacik | 09a2a8f9 | 2013-04-05 16:51:15 -0400 | [diff] [blame] | 1002 | ret = add_extent_mapping(em_tree, em, 1); |
Chris Mason | 890871b | 2009-09-02 16:24:52 -0400 | [diff] [blame] | 1003 | write_unlock(&em_tree->lock); |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 1004 | if (ret != -EEXIST) { |
| 1005 | free_extent_map(em); |
| 1006 | break; |
| 1007 | } |
| 1008 | btrfs_drop_extent_cache(inode, start, |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 1009 | start + ram_size - 1, 0); |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 1010 | } |
Liu Bo | ace68ba | 2013-04-22 10:53:47 +0000 | [diff] [blame] | 1011 | if (ret) |
| 1012 | goto out_reserve; |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 1013 | |
Chris Mason | 98d20f6 | 2008-04-14 09:46:10 -0400 | [diff] [blame] | 1014 | cur_alloc_size = ins.offset; |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 1015 | ret = btrfs_add_ordered_extent(inode, start, ins.objectid, |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 1016 | ram_size, cur_alloc_size, 0); |
Liu Bo | ace68ba | 2013-04-22 10:53:47 +0000 | [diff] [blame] | 1017 | if (ret) |
Filipe Manana | d9f8596 | 2014-08-25 10:43:00 +0100 | [diff] [blame] | 1018 | goto out_drop_extent_cache; |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 1019 | |
Yan Zheng | 17d217f | 2008-12-12 10:03:38 -0500 | [diff] [blame] | 1020 | if (root->root_key.objectid == |
| 1021 | BTRFS_DATA_RELOC_TREE_OBJECTID) { |
| 1022 | ret = btrfs_reloc_clone_csums(inode, start, |
| 1023 | cur_alloc_size); |
Josef Bacik | 0036158 | 2013-08-14 14:02:47 -0400 | [diff] [blame] | 1024 | if (ret) |
Filipe Manana | d9f8596 | 2014-08-25 10:43:00 +0100 | [diff] [blame] | 1025 | goto out_drop_extent_cache; |
Yan Zheng | 17d217f | 2008-12-12 10:03:38 -0500 | [diff] [blame] | 1026 | } |
| 1027 | |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 1028 | if (disk_num_bytes < cur_alloc_size) |
Chris Mason | 3b95151 | 2008-04-17 11:29:12 -0400 | [diff] [blame] | 1029 | break; |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 1030 | |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 1031 | /* we're not doing compressed IO, don't unlock the first |
| 1032 | * page (which the caller expects to stay locked), don't |
| 1033 | * clear any dirty bits and don't set any writeback bits |
Chris Mason | 8b62b72 | 2009-09-02 16:53:46 -0400 | [diff] [blame] | 1034 | * |
| 1035 | * Do set the Private2 bit so we know this page was properly |
| 1036 | * setup for writepage |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 1037 | */ |
Josef Bacik | c2790a2 | 2013-07-29 11:20:47 -0400 | [diff] [blame] | 1038 | op = unlock ? PAGE_UNLOCK : 0; |
| 1039 | op |= PAGE_SET_PRIVATE2; |
Chris Mason | a791e35 | 2009-10-08 11:27:10 -0400 | [diff] [blame] | 1040 | |
Josef Bacik | c2790a2 | 2013-07-29 11:20:47 -0400 | [diff] [blame] | 1041 | extent_clear_unlock_delalloc(inode, start, |
| 1042 | start + ram_size - 1, locked_page, |
| 1043 | EXTENT_LOCKED | EXTENT_DELALLOC, |
| 1044 | op); |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 1045 | disk_num_bytes -= cur_alloc_size; |
Chris Mason | c59f895 | 2007-12-17 20:14:04 -0500 | [diff] [blame] | 1046 | num_bytes -= cur_alloc_size; |
| 1047 | alloc_hint = ins.objectid + ins.offset; |
| 1048 | start += cur_alloc_size; |
Chris Mason | b888db2 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 1049 | } |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 1050 | out: |
Chris Mason | be20aa9 | 2007-12-17 20:14:01 -0500 | [diff] [blame] | 1051 | return ret; |
Miao Xie | b7d5b0a | 2012-11-01 07:32:18 +0000 | [diff] [blame] | 1052 | |
Filipe Manana | d9f8596 | 2014-08-25 10:43:00 +0100 | [diff] [blame] | 1053 | out_drop_extent_cache: |
| 1054 | btrfs_drop_extent_cache(inode, start, start + ram_size - 1, 0); |
Liu Bo | ace68ba | 2013-04-22 10:53:47 +0000 | [diff] [blame] | 1055 | out_reserve: |
Miao Xie | e570fd2 | 2014-06-19 10:42:50 +0800 | [diff] [blame] | 1056 | btrfs_free_reserved_extent(root, ins.objectid, ins.offset, 1); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 1057 | out_unlock: |
Josef Bacik | c2790a2 | 2013-07-29 11:20:47 -0400 | [diff] [blame] | 1058 | extent_clear_unlock_delalloc(inode, start, end, locked_page, |
Josef Bacik | 151a41b | 2013-07-29 13:22:24 -0400 | [diff] [blame] | 1059 | EXTENT_LOCKED | EXTENT_DO_ACCOUNTING | |
| 1060 | EXTENT_DELALLOC | EXTENT_DEFRAG, |
| 1061 | PAGE_UNLOCK | PAGE_CLEAR_DIRTY | |
| 1062 | PAGE_SET_WRITEBACK | PAGE_END_WRITEBACK); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 1063 | goto out; |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 1064 | } |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 1065 | |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 1066 | /* |
| 1067 | * work queue call back to started compression on a file and pages |
| 1068 | */ |
| 1069 | static noinline void async_cow_start(struct btrfs_work *work) |
| 1070 | { |
| 1071 | struct async_cow *async_cow; |
| 1072 | int num_added = 0; |
| 1073 | async_cow = container_of(work, struct async_cow, work); |
| 1074 | |
| 1075 | compress_file_range(async_cow->inode, async_cow->locked_page, |
| 1076 | async_cow->start, async_cow->end, async_cow, |
| 1077 | &num_added); |
Josef Bacik | 8180ef8 | 2012-06-08 15:16:12 -0400 | [diff] [blame] | 1078 | if (num_added == 0) { |
Josef Bacik | cb77fcd | 2012-06-15 12:19:48 -0600 | [diff] [blame] | 1079 | btrfs_add_delayed_iput(async_cow->inode); |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 1080 | async_cow->inode = NULL; |
Josef Bacik | 8180ef8 | 2012-06-08 15:16:12 -0400 | [diff] [blame] | 1081 | } |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 1082 | } |
| 1083 | |
| 1084 | /* |
| 1085 | * work queue call back to submit previously compressed pages |
| 1086 | */ |
| 1087 | static noinline void async_cow_submit(struct btrfs_work *work) |
| 1088 | { |
| 1089 | struct async_cow *async_cow; |
| 1090 | struct btrfs_root *root; |
| 1091 | unsigned long nr_pages; |
| 1092 | |
| 1093 | async_cow = container_of(work, struct async_cow, work); |
| 1094 | |
| 1095 | root = async_cow->root; |
| 1096 | nr_pages = (async_cow->end - async_cow->start + PAGE_CACHE_SIZE) >> |
| 1097 | PAGE_CACHE_SHIFT; |
| 1098 | |
David Sterba | ee86395 | 2015-02-16 19:41:40 +0100 | [diff] [blame] | 1099 | /* |
| 1100 | * atomic_sub_return implies a barrier for waitqueue_active |
| 1101 | */ |
Josef Bacik | 66657b3 | 2012-08-01 15:36:24 -0400 | [diff] [blame] | 1102 | if (atomic_sub_return(nr_pages, &root->fs_info->async_delalloc_pages) < |
Liu Bo | 287082b | 2012-06-28 04:02:24 -0600 | [diff] [blame] | 1103 | 5 * 1024 * 1024 && |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 1104 | waitqueue_active(&root->fs_info->async_submit_wait)) |
| 1105 | wake_up(&root->fs_info->async_submit_wait); |
| 1106 | |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 1107 | if (async_cow->inode) |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 1108 | submit_compressed_extents(async_cow->inode, async_cow); |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 1109 | } |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 1110 | |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 1111 | static noinline void async_cow_free(struct btrfs_work *work) |
| 1112 | { |
| 1113 | struct async_cow *async_cow; |
| 1114 | async_cow = container_of(work, struct async_cow, work); |
Josef Bacik | 8180ef8 | 2012-06-08 15:16:12 -0400 | [diff] [blame] | 1115 | if (async_cow->inode) |
Josef Bacik | cb77fcd | 2012-06-15 12:19:48 -0600 | [diff] [blame] | 1116 | btrfs_add_delayed_iput(async_cow->inode); |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 1117 | kfree(async_cow); |
| 1118 | } |
| 1119 | |
| 1120 | static int cow_file_range_async(struct inode *inode, struct page *locked_page, |
| 1121 | u64 start, u64 end, int *page_started, |
| 1122 | unsigned long *nr_written) |
| 1123 | { |
| 1124 | struct async_cow *async_cow; |
| 1125 | struct btrfs_root *root = BTRFS_I(inode)->root; |
| 1126 | unsigned long nr_pages; |
| 1127 | u64 cur_end; |
Liu Bo | 287082b | 2012-06-28 04:02:24 -0600 | [diff] [blame] | 1128 | int limit = 10 * 1024 * 1024; |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 1129 | |
Chris Mason | a3429ab | 2009-10-08 12:30:20 -0400 | [diff] [blame] | 1130 | clear_extent_bit(&BTRFS_I(inode)->io_tree, start, end, EXTENT_LOCKED, |
| 1131 | 1, 0, NULL, GFP_NOFS); |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 1132 | while (start < end) { |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 1133 | async_cow = kmalloc(sizeof(*async_cow), GFP_NOFS); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 1134 | BUG_ON(!async_cow); /* -ENOMEM */ |
Josef Bacik | 8180ef8 | 2012-06-08 15:16:12 -0400 | [diff] [blame] | 1135 | async_cow->inode = igrab(inode); |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 1136 | async_cow->root = root; |
| 1137 | async_cow->locked_page = locked_page; |
| 1138 | async_cow->start = start; |
| 1139 | |
Wang Shilong | f79707b | 2014-07-17 11:44:09 +0800 | [diff] [blame] | 1140 | if (BTRFS_I(inode)->flags & BTRFS_INODE_NOCOMPRESS && |
| 1141 | !btrfs_test_opt(root, FORCE_COMPRESS)) |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 1142 | cur_end = end; |
| 1143 | else |
| 1144 | cur_end = min(end, start + 512 * 1024 - 1); |
| 1145 | |
| 1146 | async_cow->end = cur_end; |
| 1147 | INIT_LIST_HEAD(&async_cow->extents); |
| 1148 | |
Liu Bo | 9e0af23 | 2014-08-15 23:36:53 +0800 | [diff] [blame] | 1149 | btrfs_init_work(&async_cow->work, |
| 1150 | btrfs_delalloc_helper, |
| 1151 | async_cow_start, async_cow_submit, |
| 1152 | async_cow_free); |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 1153 | |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 1154 | nr_pages = (cur_end - start + PAGE_CACHE_SIZE) >> |
| 1155 | PAGE_CACHE_SHIFT; |
| 1156 | atomic_add(nr_pages, &root->fs_info->async_delalloc_pages); |
| 1157 | |
Qu Wenruo | afe3d24 | 2014-02-28 10:46:07 +0800 | [diff] [blame] | 1158 | btrfs_queue_work(root->fs_info->delalloc_workers, |
| 1159 | &async_cow->work); |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 1160 | |
| 1161 | if (atomic_read(&root->fs_info->async_delalloc_pages) > limit) { |
| 1162 | wait_event(root->fs_info->async_submit_wait, |
| 1163 | (atomic_read(&root->fs_info->async_delalloc_pages) < |
| 1164 | limit)); |
| 1165 | } |
| 1166 | |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 1167 | while (atomic_read(&root->fs_info->async_submit_draining) && |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 1168 | atomic_read(&root->fs_info->async_delalloc_pages)) { |
| 1169 | wait_event(root->fs_info->async_submit_wait, |
| 1170 | (atomic_read(&root->fs_info->async_delalloc_pages) == |
| 1171 | 0)); |
| 1172 | } |
| 1173 | |
| 1174 | *nr_written += nr_pages; |
| 1175 | start = cur_end + 1; |
| 1176 | } |
| 1177 | *page_started = 1; |
| 1178 | return 0; |
Chris Mason | be20aa9 | 2007-12-17 20:14:01 -0500 | [diff] [blame] | 1179 | } |
| 1180 | |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 1181 | static noinline int csum_exist_in_range(struct btrfs_root *root, |
Yan Zheng | 17d217f | 2008-12-12 10:03:38 -0500 | [diff] [blame] | 1182 | u64 bytenr, u64 num_bytes) |
| 1183 | { |
| 1184 | int ret; |
| 1185 | struct btrfs_ordered_sum *sums; |
| 1186 | LIST_HEAD(list); |
| 1187 | |
Yan Zheng | 07d400a | 2009-01-06 11:42:00 -0500 | [diff] [blame] | 1188 | ret = btrfs_lookup_csums_range(root->fs_info->csum_root, bytenr, |
Arne Jansen | a2de733 | 2011-03-08 14:14:00 +0100 | [diff] [blame] | 1189 | bytenr + num_bytes - 1, &list, 0); |
Yan Zheng | 17d217f | 2008-12-12 10:03:38 -0500 | [diff] [blame] | 1190 | if (ret == 0 && list_empty(&list)) |
| 1191 | return 0; |
| 1192 | |
| 1193 | while (!list_empty(&list)) { |
| 1194 | sums = list_entry(list.next, struct btrfs_ordered_sum, list); |
| 1195 | list_del(&sums->list); |
| 1196 | kfree(sums); |
| 1197 | } |
| 1198 | return 1; |
| 1199 | } |
| 1200 | |
Chris Mason | d352ac6 | 2008-09-29 15:18:18 -0400 | [diff] [blame] | 1201 | /* |
| 1202 | * when nowcow writeback call back. This checks for snapshots or COW copies |
| 1203 | * of the extents that exist in the file, and COWs the file as required. |
| 1204 | * |
| 1205 | * If no cow copies or snapshots exist, we write directly to the existing |
| 1206 | * blocks on disk |
| 1207 | */ |
Chris Mason | 7f366cf | 2009-03-12 20:12:45 -0400 | [diff] [blame] | 1208 | static noinline int run_delalloc_nocow(struct inode *inode, |
| 1209 | struct page *locked_page, |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 1210 | u64 start, u64 end, int *page_started, int force, |
| 1211 | unsigned long *nr_written) |
Chris Mason | be20aa9 | 2007-12-17 20:14:01 -0500 | [diff] [blame] | 1212 | { |
Chris Mason | be20aa9 | 2007-12-17 20:14:01 -0500 | [diff] [blame] | 1213 | struct btrfs_root *root = BTRFS_I(inode)->root; |
Yan Zheng | 7ea394f | 2008-08-05 13:05:02 -0400 | [diff] [blame] | 1214 | struct btrfs_trans_handle *trans; |
Chris Mason | be20aa9 | 2007-12-17 20:14:01 -0500 | [diff] [blame] | 1215 | struct extent_buffer *leaf; |
Chris Mason | be20aa9 | 2007-12-17 20:14:01 -0500 | [diff] [blame] | 1216 | struct btrfs_path *path; |
Yan Zheng | 80ff385 | 2008-10-30 14:20:02 -0400 | [diff] [blame] | 1217 | struct btrfs_file_extent_item *fi; |
Chris Mason | be20aa9 | 2007-12-17 20:14:01 -0500 | [diff] [blame] | 1218 | struct btrfs_key found_key; |
Yan Zheng | 80ff385 | 2008-10-30 14:20:02 -0400 | [diff] [blame] | 1219 | u64 cow_start; |
| 1220 | u64 cur_offset; |
| 1221 | u64 extent_end; |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 1222 | u64 extent_offset; |
Yan Zheng | 80ff385 | 2008-10-30 14:20:02 -0400 | [diff] [blame] | 1223 | u64 disk_bytenr; |
| 1224 | u64 num_bytes; |
Josef Bacik | b493968 | 2012-12-03 10:31:19 -0500 | [diff] [blame] | 1225 | u64 disk_num_bytes; |
Josef Bacik | cc95bef | 2013-04-04 14:31:27 -0400 | [diff] [blame] | 1226 | u64 ram_bytes; |
Yan Zheng | 80ff385 | 2008-10-30 14:20:02 -0400 | [diff] [blame] | 1227 | int extent_type; |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 1228 | int ret, err; |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 1229 | int type; |
Yan Zheng | 80ff385 | 2008-10-30 14:20:02 -0400 | [diff] [blame] | 1230 | int nocow; |
| 1231 | int check_prev = 1; |
Li Zefan | 82d5902 | 2011-04-20 10:33:24 +0800 | [diff] [blame] | 1232 | bool nolock; |
Li Zefan | 33345d01 | 2011-04-20 10:31:50 +0800 | [diff] [blame] | 1233 | u64 ino = btrfs_ino(inode); |
Chris Mason | be20aa9 | 2007-12-17 20:14:01 -0500 | [diff] [blame] | 1234 | |
| 1235 | path = btrfs_alloc_path(); |
Josef Bacik | 17ca04a | 2012-05-31 15:58:55 -0400 | [diff] [blame] | 1236 | if (!path) { |
Josef Bacik | c2790a2 | 2013-07-29 11:20:47 -0400 | [diff] [blame] | 1237 | extent_clear_unlock_delalloc(inode, start, end, locked_page, |
| 1238 | EXTENT_LOCKED | EXTENT_DELALLOC | |
Josef Bacik | 151a41b | 2013-07-29 13:22:24 -0400 | [diff] [blame] | 1239 | EXTENT_DO_ACCOUNTING | |
| 1240 | EXTENT_DEFRAG, PAGE_UNLOCK | |
Josef Bacik | c2790a2 | 2013-07-29 11:20:47 -0400 | [diff] [blame] | 1241 | PAGE_CLEAR_DIRTY | |
| 1242 | PAGE_SET_WRITEBACK | |
| 1243 | PAGE_END_WRITEBACK); |
Mark Fasheh | d8926bb | 2011-07-13 10:38:47 -0700 | [diff] [blame] | 1244 | return -ENOMEM; |
Josef Bacik | 17ca04a | 2012-05-31 15:58:55 -0400 | [diff] [blame] | 1245 | } |
Li Zefan | 82d5902 | 2011-04-20 10:33:24 +0800 | [diff] [blame] | 1246 | |
Liu Bo | 83eea1f | 2012-07-10 05:28:39 -0600 | [diff] [blame] | 1247 | nolock = btrfs_is_free_space_inode(inode); |
Li Zefan | 82d5902 | 2011-04-20 10:33:24 +0800 | [diff] [blame] | 1248 | |
| 1249 | if (nolock) |
Josef Bacik | 7a7eaa4 | 2011-04-13 12:54:33 -0400 | [diff] [blame] | 1250 | trans = btrfs_join_transaction_nolock(root); |
Li Zefan | 82d5902 | 2011-04-20 10:33:24 +0800 | [diff] [blame] | 1251 | else |
Josef Bacik | 7a7eaa4 | 2011-04-13 12:54:33 -0400 | [diff] [blame] | 1252 | trans = btrfs_join_transaction(root); |
Chris Mason | ff5714c | 2011-05-28 07:00:39 -0400 | [diff] [blame] | 1253 | |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 1254 | if (IS_ERR(trans)) { |
Josef Bacik | c2790a2 | 2013-07-29 11:20:47 -0400 | [diff] [blame] | 1255 | extent_clear_unlock_delalloc(inode, start, end, locked_page, |
| 1256 | EXTENT_LOCKED | EXTENT_DELALLOC | |
Josef Bacik | 151a41b | 2013-07-29 13:22:24 -0400 | [diff] [blame] | 1257 | EXTENT_DO_ACCOUNTING | |
| 1258 | EXTENT_DEFRAG, PAGE_UNLOCK | |
Josef Bacik | c2790a2 | 2013-07-29 11:20:47 -0400 | [diff] [blame] | 1259 | PAGE_CLEAR_DIRTY | |
| 1260 | PAGE_SET_WRITEBACK | |
| 1261 | PAGE_END_WRITEBACK); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 1262 | btrfs_free_path(path); |
| 1263 | return PTR_ERR(trans); |
| 1264 | } |
| 1265 | |
Josef Bacik | 74b2107 | 2011-04-13 12:02:53 -0400 | [diff] [blame] | 1266 | trans->block_rsv = &root->fs_info->delalloc_block_rsv; |
Chris Mason | be20aa9 | 2007-12-17 20:14:01 -0500 | [diff] [blame] | 1267 | |
Yan Zheng | 80ff385 | 2008-10-30 14:20:02 -0400 | [diff] [blame] | 1268 | cow_start = (u64)-1; |
| 1269 | cur_offset = start; |
| 1270 | while (1) { |
Li Zefan | 33345d01 | 2011-04-20 10:31:50 +0800 | [diff] [blame] | 1271 | ret = btrfs_lookup_file_extent(trans, root, path, ino, |
Yan Zheng | 80ff385 | 2008-10-30 14:20:02 -0400 | [diff] [blame] | 1272 | cur_offset, 0); |
Josef Bacik | d788a34 | 2013-10-25 16:55:08 -0400 | [diff] [blame] | 1273 | if (ret < 0) |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 1274 | goto error; |
Yan Zheng | 80ff385 | 2008-10-30 14:20:02 -0400 | [diff] [blame] | 1275 | if (ret > 0 && path->slots[0] > 0 && check_prev) { |
| 1276 | leaf = path->nodes[0]; |
| 1277 | btrfs_item_key_to_cpu(leaf, &found_key, |
| 1278 | path->slots[0] - 1); |
Li Zefan | 33345d01 | 2011-04-20 10:31:50 +0800 | [diff] [blame] | 1279 | if (found_key.objectid == ino && |
Yan Zheng | 80ff385 | 2008-10-30 14:20:02 -0400 | [diff] [blame] | 1280 | found_key.type == BTRFS_EXTENT_DATA_KEY) |
| 1281 | path->slots[0]--; |
| 1282 | } |
| 1283 | check_prev = 0; |
| 1284 | next_slot: |
| 1285 | leaf = path->nodes[0]; |
| 1286 | if (path->slots[0] >= btrfs_header_nritems(leaf)) { |
| 1287 | ret = btrfs_next_leaf(root, path); |
Josef Bacik | d788a34 | 2013-10-25 16:55:08 -0400 | [diff] [blame] | 1288 | if (ret < 0) |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 1289 | goto error; |
Yan Zheng | 80ff385 | 2008-10-30 14:20:02 -0400 | [diff] [blame] | 1290 | if (ret > 0) |
| 1291 | break; |
| 1292 | leaf = path->nodes[0]; |
| 1293 | } |
Chris Mason | be20aa9 | 2007-12-17 20:14:01 -0500 | [diff] [blame] | 1294 | |
Yan Zheng | 80ff385 | 2008-10-30 14:20:02 -0400 | [diff] [blame] | 1295 | nocow = 0; |
| 1296 | disk_bytenr = 0; |
Yan Zheng | 17d217f | 2008-12-12 10:03:38 -0500 | [diff] [blame] | 1297 | num_bytes = 0; |
Yan Zheng | 80ff385 | 2008-10-30 14:20:02 -0400 | [diff] [blame] | 1298 | btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]); |
Chris Mason | be20aa9 | 2007-12-17 20:14:01 -0500 | [diff] [blame] | 1299 | |
Li Zefan | 33345d01 | 2011-04-20 10:31:50 +0800 | [diff] [blame] | 1300 | if (found_key.objectid > ino || |
Yan Zheng | 80ff385 | 2008-10-30 14:20:02 -0400 | [diff] [blame] | 1301 | found_key.type > BTRFS_EXTENT_DATA_KEY || |
| 1302 | found_key.offset > end) |
| 1303 | break; |
Chris Mason | be20aa9 | 2007-12-17 20:14:01 -0500 | [diff] [blame] | 1304 | |
Yan Zheng | 80ff385 | 2008-10-30 14:20:02 -0400 | [diff] [blame] | 1305 | if (found_key.offset > cur_offset) { |
| 1306 | extent_end = found_key.offset; |
Chris Mason | e9061e2 | 2009-10-09 09:57:45 -0400 | [diff] [blame] | 1307 | extent_type = 0; |
Yan Zheng | 80ff385 | 2008-10-30 14:20:02 -0400 | [diff] [blame] | 1308 | goto out_check; |
| 1309 | } |
Chris Mason | c31f883 | 2008-01-08 15:46:31 -0500 | [diff] [blame] | 1310 | |
Yan Zheng | 80ff385 | 2008-10-30 14:20:02 -0400 | [diff] [blame] | 1311 | fi = btrfs_item_ptr(leaf, path->slots[0], |
| 1312 | struct btrfs_file_extent_item); |
| 1313 | extent_type = btrfs_file_extent_type(leaf, fi); |
Chris Mason | be20aa9 | 2007-12-17 20:14:01 -0500 | [diff] [blame] | 1314 | |
Josef Bacik | cc95bef | 2013-04-04 14:31:27 -0400 | [diff] [blame] | 1315 | ram_bytes = btrfs_file_extent_ram_bytes(leaf, fi); |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 1316 | if (extent_type == BTRFS_FILE_EXTENT_REG || |
| 1317 | extent_type == BTRFS_FILE_EXTENT_PREALLOC) { |
Yan Zheng | 80ff385 | 2008-10-30 14:20:02 -0400 | [diff] [blame] | 1318 | disk_bytenr = btrfs_file_extent_disk_bytenr(leaf, fi); |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 1319 | extent_offset = btrfs_file_extent_offset(leaf, fi); |
Yan Zheng | 80ff385 | 2008-10-30 14:20:02 -0400 | [diff] [blame] | 1320 | extent_end = found_key.offset + |
| 1321 | btrfs_file_extent_num_bytes(leaf, fi); |
Josef Bacik | b493968 | 2012-12-03 10:31:19 -0500 | [diff] [blame] | 1322 | disk_num_bytes = |
| 1323 | btrfs_file_extent_disk_num_bytes(leaf, fi); |
Yan Zheng | 80ff385 | 2008-10-30 14:20:02 -0400 | [diff] [blame] | 1324 | if (extent_end <= start) { |
| 1325 | path->slots[0]++; |
| 1326 | goto next_slot; |
| 1327 | } |
Yan Zheng | 17d217f | 2008-12-12 10:03:38 -0500 | [diff] [blame] | 1328 | if (disk_bytenr == 0) |
| 1329 | goto out_check; |
Yan Zheng | 80ff385 | 2008-10-30 14:20:02 -0400 | [diff] [blame] | 1330 | if (btrfs_file_extent_compression(leaf, fi) || |
| 1331 | btrfs_file_extent_encryption(leaf, fi) || |
| 1332 | btrfs_file_extent_other_encoding(leaf, fi)) |
| 1333 | goto out_check; |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 1334 | if (extent_type == BTRFS_FILE_EXTENT_REG && !force) |
| 1335 | goto out_check; |
Yan Zheng | d2fb343 | 2008-12-11 16:30:39 -0500 | [diff] [blame] | 1336 | if (btrfs_extent_readonly(root, disk_bytenr)) |
Yan Zheng | 80ff385 | 2008-10-30 14:20:02 -0400 | [diff] [blame] | 1337 | goto out_check; |
Li Zefan | 33345d01 | 2011-04-20 10:31:50 +0800 | [diff] [blame] | 1338 | if (btrfs_cross_ref_exist(trans, root, ino, |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 1339 | found_key.offset - |
| 1340 | extent_offset, disk_bytenr)) |
Yan Zheng | 17d217f | 2008-12-12 10:03:38 -0500 | [diff] [blame] | 1341 | goto out_check; |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 1342 | disk_bytenr += extent_offset; |
Yan Zheng | 17d217f | 2008-12-12 10:03:38 -0500 | [diff] [blame] | 1343 | disk_bytenr += cur_offset - found_key.offset; |
| 1344 | num_bytes = min(end + 1, extent_end) - cur_offset; |
| 1345 | /* |
Wang Shilong | e9894fd | 2014-03-27 11:12:25 +0800 | [diff] [blame] | 1346 | * if there are pending snapshots for this root, |
| 1347 | * we fall into common COW way. |
| 1348 | */ |
| 1349 | if (!nolock) { |
Filipe Manana | 9ea24bb | 2014-10-29 11:57:59 +0000 | [diff] [blame] | 1350 | err = btrfs_start_write_no_snapshoting(root); |
Wang Shilong | e9894fd | 2014-03-27 11:12:25 +0800 | [diff] [blame] | 1351 | if (!err) |
| 1352 | goto out_check; |
| 1353 | } |
| 1354 | /* |
Yan Zheng | 17d217f | 2008-12-12 10:03:38 -0500 | [diff] [blame] | 1355 | * force cow if csum exists in the range. |
| 1356 | * this ensure that csum for a given extent are |
| 1357 | * either valid or do not exist. |
| 1358 | */ |
| 1359 | if (csum_exist_in_range(root, disk_bytenr, num_bytes)) |
| 1360 | goto out_check; |
Yan Zheng | 80ff385 | 2008-10-30 14:20:02 -0400 | [diff] [blame] | 1361 | nocow = 1; |
| 1362 | } else if (extent_type == BTRFS_FILE_EXTENT_INLINE) { |
| 1363 | extent_end = found_key.offset + |
Chris Mason | 514ac8a | 2014-01-03 21:07:00 -0800 | [diff] [blame] | 1364 | btrfs_file_extent_inline_len(leaf, |
| 1365 | path->slots[0], fi); |
Yan Zheng | 80ff385 | 2008-10-30 14:20:02 -0400 | [diff] [blame] | 1366 | extent_end = ALIGN(extent_end, root->sectorsize); |
| 1367 | } else { |
| 1368 | BUG_ON(1); |
| 1369 | } |
| 1370 | out_check: |
| 1371 | if (extent_end <= start) { |
| 1372 | path->slots[0]++; |
Wang Shilong | e9894fd | 2014-03-27 11:12:25 +0800 | [diff] [blame] | 1373 | if (!nolock && nocow) |
Filipe Manana | 9ea24bb | 2014-10-29 11:57:59 +0000 | [diff] [blame] | 1374 | btrfs_end_write_no_snapshoting(root); |
Yan Zheng | 80ff385 | 2008-10-30 14:20:02 -0400 | [diff] [blame] | 1375 | goto next_slot; |
| 1376 | } |
| 1377 | if (!nocow) { |
| 1378 | if (cow_start == (u64)-1) |
| 1379 | cow_start = cur_offset; |
| 1380 | cur_offset = extent_end; |
| 1381 | if (cur_offset > end) |
| 1382 | break; |
| 1383 | path->slots[0]++; |
| 1384 | goto next_slot; |
Yan Zheng | 7ea394f | 2008-08-05 13:05:02 -0400 | [diff] [blame] | 1385 | } |
Chris Mason | be20aa9 | 2007-12-17 20:14:01 -0500 | [diff] [blame] | 1386 | |
David Sterba | b3b4aa7 | 2011-04-21 01:20:15 +0200 | [diff] [blame] | 1387 | btrfs_release_path(path); |
Yan Zheng | 80ff385 | 2008-10-30 14:20:02 -0400 | [diff] [blame] | 1388 | if (cow_start != (u64)-1) { |
Josef Bacik | 0036158 | 2013-08-14 14:02:47 -0400 | [diff] [blame] | 1389 | ret = cow_file_range(inode, locked_page, |
| 1390 | cow_start, found_key.offset - 1, |
| 1391 | page_started, nr_written, 1); |
Wang Shilong | e9894fd | 2014-03-27 11:12:25 +0800 | [diff] [blame] | 1392 | if (ret) { |
| 1393 | if (!nolock && nocow) |
Filipe Manana | 9ea24bb | 2014-10-29 11:57:59 +0000 | [diff] [blame] | 1394 | btrfs_end_write_no_snapshoting(root); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 1395 | goto error; |
Wang Shilong | e9894fd | 2014-03-27 11:12:25 +0800 | [diff] [blame] | 1396 | } |
Yan Zheng | 80ff385 | 2008-10-30 14:20:02 -0400 | [diff] [blame] | 1397 | cow_start = (u64)-1; |
Yan Zheng | 7ea394f | 2008-08-05 13:05:02 -0400 | [diff] [blame] | 1398 | } |
Yan Zheng | 80ff385 | 2008-10-30 14:20:02 -0400 | [diff] [blame] | 1399 | |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 1400 | if (extent_type == BTRFS_FILE_EXTENT_PREALLOC) { |
| 1401 | struct extent_map *em; |
| 1402 | struct extent_map_tree *em_tree; |
| 1403 | em_tree = &BTRFS_I(inode)->extent_tree; |
David Sterba | 172ddd6 | 2011-04-21 00:48:27 +0200 | [diff] [blame] | 1404 | em = alloc_extent_map(); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 1405 | BUG_ON(!em); /* -ENOMEM */ |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 1406 | em->start = cur_offset; |
Josef Bacik | 70c8a91 | 2012-10-11 16:54:30 -0400 | [diff] [blame] | 1407 | em->orig_start = found_key.offset - extent_offset; |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 1408 | em->len = num_bytes; |
| 1409 | em->block_len = num_bytes; |
| 1410 | em->block_start = disk_bytenr; |
Josef Bacik | b493968 | 2012-12-03 10:31:19 -0500 | [diff] [blame] | 1411 | em->orig_block_len = disk_num_bytes; |
Josef Bacik | cc95bef | 2013-04-04 14:31:27 -0400 | [diff] [blame] | 1412 | em->ram_bytes = ram_bytes; |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 1413 | em->bdev = root->fs_info->fs_devices->latest_bdev; |
Josef Bacik | 2ab28f3 | 2012-10-12 15:27:49 -0400 | [diff] [blame] | 1414 | em->mod_start = em->start; |
| 1415 | em->mod_len = em->len; |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 1416 | set_bit(EXTENT_FLAG_PINNED, &em->flags); |
Josef Bacik | b11e234 | 2012-12-03 10:58:15 -0500 | [diff] [blame] | 1417 | set_bit(EXTENT_FLAG_FILLING, &em->flags); |
Josef Bacik | 70c8a91 | 2012-10-11 16:54:30 -0400 | [diff] [blame] | 1418 | em->generation = -1; |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 1419 | while (1) { |
Chris Mason | 890871b | 2009-09-02 16:24:52 -0400 | [diff] [blame] | 1420 | write_lock(&em_tree->lock); |
Josef Bacik | 09a2a8f9 | 2013-04-05 16:51:15 -0400 | [diff] [blame] | 1421 | ret = add_extent_mapping(em_tree, em, 1); |
Chris Mason | 890871b | 2009-09-02 16:24:52 -0400 | [diff] [blame] | 1422 | write_unlock(&em_tree->lock); |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 1423 | if (ret != -EEXIST) { |
| 1424 | free_extent_map(em); |
| 1425 | break; |
| 1426 | } |
| 1427 | btrfs_drop_extent_cache(inode, em->start, |
| 1428 | em->start + em->len - 1, 0); |
| 1429 | } |
| 1430 | type = BTRFS_ORDERED_PREALLOC; |
| 1431 | } else { |
| 1432 | type = BTRFS_ORDERED_NOCOW; |
| 1433 | } |
Yan Zheng | 80ff385 | 2008-10-30 14:20:02 -0400 | [diff] [blame] | 1434 | |
| 1435 | ret = btrfs_add_ordered_extent(inode, cur_offset, disk_bytenr, |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 1436 | num_bytes, num_bytes, type); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 1437 | BUG_ON(ret); /* -ENOMEM */ |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 1438 | |
Yan, Zheng | efa5646 | 2010-05-16 10:49:59 -0400 | [diff] [blame] | 1439 | if (root->root_key.objectid == |
| 1440 | BTRFS_DATA_RELOC_TREE_OBJECTID) { |
| 1441 | ret = btrfs_reloc_clone_csums(inode, cur_offset, |
| 1442 | num_bytes); |
Wang Shilong | e9894fd | 2014-03-27 11:12:25 +0800 | [diff] [blame] | 1443 | if (ret) { |
| 1444 | if (!nolock && nocow) |
Filipe Manana | 9ea24bb | 2014-10-29 11:57:59 +0000 | [diff] [blame] | 1445 | btrfs_end_write_no_snapshoting(root); |
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 | efa5646 | 2010-05-16 10:49:59 -0400 | [diff] [blame] | 1448 | } |
| 1449 | |
Josef Bacik | c2790a2 | 2013-07-29 11:20:47 -0400 | [diff] [blame] | 1450 | extent_clear_unlock_delalloc(inode, cur_offset, |
| 1451 | cur_offset + num_bytes - 1, |
| 1452 | locked_page, EXTENT_LOCKED | |
| 1453 | EXTENT_DELALLOC, PAGE_UNLOCK | |
| 1454 | PAGE_SET_PRIVATE2); |
Wang Shilong | e9894fd | 2014-03-27 11:12:25 +0800 | [diff] [blame] | 1455 | if (!nolock && nocow) |
Filipe Manana | 9ea24bb | 2014-10-29 11:57:59 +0000 | [diff] [blame] | 1456 | btrfs_end_write_no_snapshoting(root); |
Yan Zheng | 80ff385 | 2008-10-30 14:20:02 -0400 | [diff] [blame] | 1457 | cur_offset = extent_end; |
| 1458 | if (cur_offset > end) |
| 1459 | break; |
Yan Zheng | 7ea394f | 2008-08-05 13:05:02 -0400 | [diff] [blame] | 1460 | } |
David Sterba | b3b4aa7 | 2011-04-21 01:20:15 +0200 | [diff] [blame] | 1461 | btrfs_release_path(path); |
Yan Zheng | 80ff385 | 2008-10-30 14:20:02 -0400 | [diff] [blame] | 1462 | |
Josef Bacik | 17ca04a | 2012-05-31 15:58:55 -0400 | [diff] [blame] | 1463 | if (cur_offset <= end && cow_start == (u64)-1) { |
Yan Zheng | 80ff385 | 2008-10-30 14:20:02 -0400 | [diff] [blame] | 1464 | cow_start = cur_offset; |
Josef Bacik | 17ca04a | 2012-05-31 15:58:55 -0400 | [diff] [blame] | 1465 | cur_offset = end; |
| 1466 | } |
| 1467 | |
Yan Zheng | 80ff385 | 2008-10-30 14:20:02 -0400 | [diff] [blame] | 1468 | if (cow_start != (u64)-1) { |
Josef Bacik | 0036158 | 2013-08-14 14:02:47 -0400 | [diff] [blame] | 1469 | ret = cow_file_range(inode, locked_page, cow_start, end, |
| 1470 | page_started, nr_written, 1); |
Josef Bacik | d788a34 | 2013-10-25 16:55:08 -0400 | [diff] [blame] | 1471 | if (ret) |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 1472 | goto error; |
Yan Zheng | 80ff385 | 2008-10-30 14:20:02 -0400 | [diff] [blame] | 1473 | } |
| 1474 | |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 1475 | error: |
Miao Xie | a698d075 | 2012-09-20 01:51:59 -0600 | [diff] [blame] | 1476 | err = btrfs_end_transaction(trans, root); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 1477 | if (!ret) |
| 1478 | ret = err; |
| 1479 | |
Josef Bacik | 17ca04a | 2012-05-31 15:58:55 -0400 | [diff] [blame] | 1480 | if (ret && cur_offset < end) |
Josef Bacik | c2790a2 | 2013-07-29 11:20:47 -0400 | [diff] [blame] | 1481 | extent_clear_unlock_delalloc(inode, cur_offset, end, |
| 1482 | locked_page, EXTENT_LOCKED | |
Josef Bacik | 151a41b | 2013-07-29 13:22:24 -0400 | [diff] [blame] | 1483 | EXTENT_DELALLOC | EXTENT_DEFRAG | |
| 1484 | EXTENT_DO_ACCOUNTING, PAGE_UNLOCK | |
| 1485 | PAGE_CLEAR_DIRTY | |
Josef Bacik | c2790a2 | 2013-07-29 11:20:47 -0400 | [diff] [blame] | 1486 | PAGE_SET_WRITEBACK | |
| 1487 | PAGE_END_WRITEBACK); |
Yan Zheng | 7ea394f | 2008-08-05 13:05:02 -0400 | [diff] [blame] | 1488 | btrfs_free_path(path); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 1489 | return ret; |
Chris Mason | be20aa9 | 2007-12-17 20:14:01 -0500 | [diff] [blame] | 1490 | } |
| 1491 | |
Wang Shilong | 47059d9 | 2014-07-03 18:22:07 +0800 | [diff] [blame] | 1492 | static inline int need_force_cow(struct inode *inode, u64 start, u64 end) |
| 1493 | { |
| 1494 | |
| 1495 | if (!(BTRFS_I(inode)->flags & BTRFS_INODE_NODATACOW) && |
| 1496 | !(BTRFS_I(inode)->flags & BTRFS_INODE_PREALLOC)) |
| 1497 | return 0; |
| 1498 | |
| 1499 | /* |
| 1500 | * @defrag_bytes is a hint value, no spinlock held here, |
| 1501 | * if is not zero, it means the file is defragging. |
| 1502 | * Force cow if given extent needs to be defragged. |
| 1503 | */ |
| 1504 | if (BTRFS_I(inode)->defrag_bytes && |
| 1505 | test_range_bit(&BTRFS_I(inode)->io_tree, start, end, |
| 1506 | EXTENT_DEFRAG, 0, NULL)) |
| 1507 | return 1; |
| 1508 | |
| 1509 | return 0; |
| 1510 | } |
| 1511 | |
Chris Mason | d352ac6 | 2008-09-29 15:18:18 -0400 | [diff] [blame] | 1512 | /* |
| 1513 | * extent_io.c call back to do delayed allocation processing |
| 1514 | */ |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 1515 | static int run_delalloc_range(struct inode *inode, struct page *locked_page, |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 1516 | u64 start, u64 end, int *page_started, |
| 1517 | unsigned long *nr_written) |
Chris Mason | be20aa9 | 2007-12-17 20:14:01 -0500 | [diff] [blame] | 1518 | { |
Chris Mason | be20aa9 | 2007-12-17 20:14:01 -0500 | [diff] [blame] | 1519 | int ret; |
Wang Shilong | 47059d9 | 2014-07-03 18:22:07 +0800 | [diff] [blame] | 1520 | int force_cow = need_force_cow(inode, start, end); |
Chris Mason | a213501 | 2008-06-25 16:01:30 -0400 | [diff] [blame] | 1521 | |
Wang Shilong | 47059d9 | 2014-07-03 18:22:07 +0800 | [diff] [blame] | 1522 | if (BTRFS_I(inode)->flags & BTRFS_INODE_NODATACOW && !force_cow) { |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 1523 | ret = run_delalloc_nocow(inode, locked_page, start, end, |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 1524 | page_started, 1, nr_written); |
Wang Shilong | 47059d9 | 2014-07-03 18:22:07 +0800 | [diff] [blame] | 1525 | } else if (BTRFS_I(inode)->flags & BTRFS_INODE_PREALLOC && !force_cow) { |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 1526 | ret = run_delalloc_nocow(inode, locked_page, start, end, |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 1527 | page_started, 0, nr_written); |
Wang Shilong | 7816030 | 2014-07-17 11:44:10 +0800 | [diff] [blame] | 1528 | } else if (!inode_need_compress(inode)) { |
Chris Mason | 7f366cf | 2009-03-12 20:12:45 -0400 | [diff] [blame] | 1529 | ret = cow_file_range(inode, locked_page, start, end, |
| 1530 | page_started, nr_written, 1); |
Josef Bacik | 7ddf5a4 | 2012-06-08 15:26:47 -0400 | [diff] [blame] | 1531 | } else { |
| 1532 | set_bit(BTRFS_INODE_HAS_ASYNC_EXTENT, |
| 1533 | &BTRFS_I(inode)->runtime_flags); |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 1534 | ret = cow_file_range_async(inode, locked_page, start, end, |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 1535 | page_started, nr_written); |
Josef Bacik | 7ddf5a4 | 2012-06-08 15:26:47 -0400 | [diff] [blame] | 1536 | } |
Chris Mason | b888db2 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 1537 | return ret; |
| 1538 | } |
| 1539 | |
Jeff Mahoney | 1bf8504 | 2011-07-21 16:56:09 +0000 | [diff] [blame] | 1540 | static void btrfs_split_extent_hook(struct inode *inode, |
| 1541 | struct extent_state *orig, u64 split) |
Josef Bacik | 9ed74f2 | 2009-09-11 16:12:44 -0400 | [diff] [blame] | 1542 | { |
Josef Bacik | dcab6a3 | 2015-02-11 15:08:59 -0500 | [diff] [blame] | 1543 | u64 size; |
| 1544 | |
Yan, Zheng | 0ca1f7c | 2010-05-16 10:48:47 -0400 | [diff] [blame] | 1545 | /* not delalloc, ignore it */ |
Josef Bacik | 9ed74f2 | 2009-09-11 16:12:44 -0400 | [diff] [blame] | 1546 | if (!(orig->state & EXTENT_DELALLOC)) |
Jeff Mahoney | 1bf8504 | 2011-07-21 16:56:09 +0000 | [diff] [blame] | 1547 | return; |
Josef Bacik | 9ed74f2 | 2009-09-11 16:12:44 -0400 | [diff] [blame] | 1548 | |
Josef Bacik | dcab6a3 | 2015-02-11 15:08:59 -0500 | [diff] [blame] | 1549 | size = orig->end - orig->start + 1; |
| 1550 | if (size > BTRFS_MAX_EXTENT_SIZE) { |
| 1551 | u64 num_extents; |
| 1552 | u64 new_size; |
| 1553 | |
| 1554 | /* |
Josef Bacik | ba11721 | 2015-03-13 15:01:24 -0400 | [diff] [blame] | 1555 | * See the explanation in btrfs_merge_extent_hook, the same |
| 1556 | * applies here, just in reverse. |
Josef Bacik | dcab6a3 | 2015-02-11 15:08:59 -0500 | [diff] [blame] | 1557 | */ |
| 1558 | new_size = orig->end - split + 1; |
Josef Bacik | ba11721 | 2015-03-13 15:01:24 -0400 | [diff] [blame] | 1559 | num_extents = div64_u64(new_size + BTRFS_MAX_EXTENT_SIZE - 1, |
Josef Bacik | dcab6a3 | 2015-02-11 15:08:59 -0500 | [diff] [blame] | 1560 | BTRFS_MAX_EXTENT_SIZE); |
Josef Bacik | ba11721 | 2015-03-13 15:01:24 -0400 | [diff] [blame] | 1561 | new_size = split - orig->start; |
| 1562 | num_extents += div64_u64(new_size + BTRFS_MAX_EXTENT_SIZE - 1, |
| 1563 | BTRFS_MAX_EXTENT_SIZE); |
| 1564 | if (div64_u64(size + BTRFS_MAX_EXTENT_SIZE - 1, |
| 1565 | BTRFS_MAX_EXTENT_SIZE) >= num_extents) |
Josef Bacik | dcab6a3 | 2015-02-11 15:08:59 -0500 | [diff] [blame] | 1566 | return; |
| 1567 | } |
| 1568 | |
Josef Bacik | 9e0baf6 | 2011-07-15 15:16:44 +0000 | [diff] [blame] | 1569 | spin_lock(&BTRFS_I(inode)->lock); |
| 1570 | BTRFS_I(inode)->outstanding_extents++; |
| 1571 | spin_unlock(&BTRFS_I(inode)->lock); |
Josef Bacik | 9ed74f2 | 2009-09-11 16:12:44 -0400 | [diff] [blame] | 1572 | } |
| 1573 | |
| 1574 | /* |
| 1575 | * extent_io.c merge_extent_hook, used to track merged delayed allocation |
| 1576 | * extents so we can keep track of new extents that are just merged onto old |
| 1577 | * extents, such as when we are doing sequential writes, so we can properly |
| 1578 | * account for the metadata space we'll need. |
| 1579 | */ |
Jeff Mahoney | 1bf8504 | 2011-07-21 16:56:09 +0000 | [diff] [blame] | 1580 | static void btrfs_merge_extent_hook(struct inode *inode, |
| 1581 | struct extent_state *new, |
| 1582 | struct extent_state *other) |
Josef Bacik | 9ed74f2 | 2009-09-11 16:12:44 -0400 | [diff] [blame] | 1583 | { |
Josef Bacik | dcab6a3 | 2015-02-11 15:08:59 -0500 | [diff] [blame] | 1584 | u64 new_size, old_size; |
| 1585 | u64 num_extents; |
| 1586 | |
Josef Bacik | 9ed74f2 | 2009-09-11 16:12:44 -0400 | [diff] [blame] | 1587 | /* not delalloc, ignore it */ |
| 1588 | if (!(other->state & EXTENT_DELALLOC)) |
Jeff Mahoney | 1bf8504 | 2011-07-21 16:56:09 +0000 | [diff] [blame] | 1589 | return; |
Josef Bacik | 9ed74f2 | 2009-09-11 16:12:44 -0400 | [diff] [blame] | 1590 | |
Josef Bacik | 8461a3d | 2015-03-13 15:12:08 -0400 | [diff] [blame] | 1591 | if (new->start > other->start) |
| 1592 | new_size = new->end - other->start + 1; |
| 1593 | else |
| 1594 | new_size = other->end - new->start + 1; |
Josef Bacik | dcab6a3 | 2015-02-11 15:08:59 -0500 | [diff] [blame] | 1595 | |
| 1596 | /* we're not bigger than the max, unreserve the space and go */ |
| 1597 | if (new_size <= BTRFS_MAX_EXTENT_SIZE) { |
| 1598 | spin_lock(&BTRFS_I(inode)->lock); |
| 1599 | BTRFS_I(inode)->outstanding_extents--; |
| 1600 | spin_unlock(&BTRFS_I(inode)->lock); |
| 1601 | return; |
| 1602 | } |
| 1603 | |
| 1604 | /* |
Josef Bacik | ba11721 | 2015-03-13 15:01:24 -0400 | [diff] [blame] | 1605 | * We have to add up either side to figure out how many extents were |
| 1606 | * accounted for before we merged into one big extent. If the number of |
| 1607 | * extents we accounted for is <= the amount we need for the new range |
| 1608 | * then we can return, otherwise drop. Think of it like this |
| 1609 | * |
| 1610 | * [ 4k][MAX_SIZE] |
| 1611 | * |
| 1612 | * So we've grown the extent by a MAX_SIZE extent, this would mean we |
| 1613 | * need 2 outstanding extents, on one side we have 1 and the other side |
| 1614 | * we have 1 so they are == and we can return. But in this case |
| 1615 | * |
| 1616 | * [MAX_SIZE+4k][MAX_SIZE+4k] |
| 1617 | * |
| 1618 | * Each range on their own accounts for 2 extents, but merged together |
| 1619 | * they are only 3 extents worth of accounting, so we need to drop in |
| 1620 | * this case. |
Josef Bacik | dcab6a3 | 2015-02-11 15:08:59 -0500 | [diff] [blame] | 1621 | */ |
Josef Bacik | ba11721 | 2015-03-13 15:01:24 -0400 | [diff] [blame] | 1622 | old_size = other->end - other->start + 1; |
Josef Bacik | dcab6a3 | 2015-02-11 15:08:59 -0500 | [diff] [blame] | 1623 | num_extents = div64_u64(old_size + BTRFS_MAX_EXTENT_SIZE - 1, |
| 1624 | BTRFS_MAX_EXTENT_SIZE); |
Josef Bacik | ba11721 | 2015-03-13 15:01:24 -0400 | [diff] [blame] | 1625 | old_size = new->end - new->start + 1; |
| 1626 | num_extents += div64_u64(old_size + BTRFS_MAX_EXTENT_SIZE - 1, |
| 1627 | BTRFS_MAX_EXTENT_SIZE); |
| 1628 | |
Josef Bacik | dcab6a3 | 2015-02-11 15:08:59 -0500 | [diff] [blame] | 1629 | if (div64_u64(new_size + BTRFS_MAX_EXTENT_SIZE - 1, |
Josef Bacik | ba11721 | 2015-03-13 15:01:24 -0400 | [diff] [blame] | 1630 | BTRFS_MAX_EXTENT_SIZE) >= num_extents) |
Josef Bacik | dcab6a3 | 2015-02-11 15:08:59 -0500 | [diff] [blame] | 1631 | return; |
| 1632 | |
Josef Bacik | 9e0baf6 | 2011-07-15 15:16:44 +0000 | [diff] [blame] | 1633 | spin_lock(&BTRFS_I(inode)->lock); |
| 1634 | BTRFS_I(inode)->outstanding_extents--; |
| 1635 | spin_unlock(&BTRFS_I(inode)->lock); |
Josef Bacik | 9ed74f2 | 2009-09-11 16:12:44 -0400 | [diff] [blame] | 1636 | } |
| 1637 | |
Miao Xie | eb73c1b | 2013-05-15 07:48:22 +0000 | [diff] [blame] | 1638 | static void btrfs_add_delalloc_inodes(struct btrfs_root *root, |
| 1639 | struct inode *inode) |
| 1640 | { |
| 1641 | spin_lock(&root->delalloc_lock); |
| 1642 | if (list_empty(&BTRFS_I(inode)->delalloc_inodes)) { |
| 1643 | list_add_tail(&BTRFS_I(inode)->delalloc_inodes, |
| 1644 | &root->delalloc_inodes); |
| 1645 | set_bit(BTRFS_INODE_IN_DELALLOC_LIST, |
| 1646 | &BTRFS_I(inode)->runtime_flags); |
| 1647 | root->nr_delalloc_inodes++; |
| 1648 | if (root->nr_delalloc_inodes == 1) { |
| 1649 | spin_lock(&root->fs_info->delalloc_root_lock); |
| 1650 | BUG_ON(!list_empty(&root->delalloc_root)); |
| 1651 | list_add_tail(&root->delalloc_root, |
| 1652 | &root->fs_info->delalloc_roots); |
| 1653 | spin_unlock(&root->fs_info->delalloc_root_lock); |
| 1654 | } |
| 1655 | } |
| 1656 | spin_unlock(&root->delalloc_lock); |
| 1657 | } |
| 1658 | |
| 1659 | static void btrfs_del_delalloc_inode(struct btrfs_root *root, |
| 1660 | struct inode *inode) |
| 1661 | { |
| 1662 | spin_lock(&root->delalloc_lock); |
| 1663 | if (!list_empty(&BTRFS_I(inode)->delalloc_inodes)) { |
| 1664 | list_del_init(&BTRFS_I(inode)->delalloc_inodes); |
| 1665 | clear_bit(BTRFS_INODE_IN_DELALLOC_LIST, |
| 1666 | &BTRFS_I(inode)->runtime_flags); |
| 1667 | root->nr_delalloc_inodes--; |
| 1668 | if (!root->nr_delalloc_inodes) { |
| 1669 | spin_lock(&root->fs_info->delalloc_root_lock); |
| 1670 | BUG_ON(list_empty(&root->delalloc_root)); |
| 1671 | list_del_init(&root->delalloc_root); |
| 1672 | spin_unlock(&root->fs_info->delalloc_root_lock); |
| 1673 | } |
| 1674 | } |
| 1675 | spin_unlock(&root->delalloc_lock); |
| 1676 | } |
| 1677 | |
Chris Mason | d352ac6 | 2008-09-29 15:18:18 -0400 | [diff] [blame] | 1678 | /* |
| 1679 | * extent_io.c set_bit_hook, used to track delayed allocation |
| 1680 | * bytes in this file, and to maintain the list of inodes that |
| 1681 | * have pending delalloc work to be done. |
| 1682 | */ |
Jeff Mahoney | 1bf8504 | 2011-07-21 16:56:09 +0000 | [diff] [blame] | 1683 | static void btrfs_set_bit_hook(struct inode *inode, |
David Sterba | 9ee49a04 | 2015-01-14 19:52:13 +0100 | [diff] [blame] | 1684 | struct extent_state *state, unsigned *bits) |
Chris Mason | 291d673 | 2008-01-29 15:55:23 -0500 | [diff] [blame] | 1685 | { |
Josef Bacik | 9ed74f2 | 2009-09-11 16:12:44 -0400 | [diff] [blame] | 1686 | |
Wang Shilong | 47059d9 | 2014-07-03 18:22:07 +0800 | [diff] [blame] | 1687 | if ((*bits & EXTENT_DEFRAG) && !(*bits & EXTENT_DELALLOC)) |
| 1688 | WARN_ON(1); |
Chris Mason | 75eff68 | 2008-12-15 15:54:40 -0500 | [diff] [blame] | 1689 | /* |
| 1690 | * set_bit and clear bit hooks normally require _irqsave/restore |
Sergei Trofimovich | 27160b6 | 2011-05-20 20:20:32 +0000 | [diff] [blame] | 1691 | * but in this case, we are only testing for the DELALLOC |
Chris Mason | 75eff68 | 2008-12-15 15:54:40 -0500 | [diff] [blame] | 1692 | * bit, which is only set or cleared with irqs on |
| 1693 | */ |
Yan, Zheng | 0ca1f7c | 2010-05-16 10:48:47 -0400 | [diff] [blame] | 1694 | if (!(state->state & EXTENT_DELALLOC) && (*bits & EXTENT_DELALLOC)) { |
Chris Mason | 291d673 | 2008-01-29 15:55:23 -0500 | [diff] [blame] | 1695 | struct btrfs_root *root = BTRFS_I(inode)->root; |
Yan, Zheng | 0ca1f7c | 2010-05-16 10:48:47 -0400 | [diff] [blame] | 1696 | u64 len = state->end + 1 - state->start; |
Liu Bo | 83eea1f | 2012-07-10 05:28:39 -0600 | [diff] [blame] | 1697 | bool do_list = !btrfs_is_free_space_inode(inode); |
Josef Bacik | 9ed74f2 | 2009-09-11 16:12:44 -0400 | [diff] [blame] | 1698 | |
Josef Bacik | 9e0baf6 | 2011-07-15 15:16:44 +0000 | [diff] [blame] | 1699 | if (*bits & EXTENT_FIRST_DELALLOC) { |
Yan, Zheng | 0ca1f7c | 2010-05-16 10:48:47 -0400 | [diff] [blame] | 1700 | *bits &= ~EXTENT_FIRST_DELALLOC; |
Josef Bacik | 9e0baf6 | 2011-07-15 15:16:44 +0000 | [diff] [blame] | 1701 | } else { |
| 1702 | spin_lock(&BTRFS_I(inode)->lock); |
| 1703 | BTRFS_I(inode)->outstanding_extents++; |
| 1704 | spin_unlock(&BTRFS_I(inode)->lock); |
| 1705 | } |
Josef Bacik | 287a0ab | 2010-03-19 18:07:23 +0000 | [diff] [blame] | 1706 | |
Josef Bacik | 6a3891c | 2015-03-16 17:38:52 -0400 | [diff] [blame] | 1707 | /* For sanity tests */ |
| 1708 | if (btrfs_test_is_dummy_root(root)) |
| 1709 | return; |
| 1710 | |
Miao Xie | 963d678 | 2013-01-29 10:10:51 +0000 | [diff] [blame] | 1711 | __percpu_counter_add(&root->fs_info->delalloc_bytes, len, |
| 1712 | root->fs_info->delalloc_batch); |
Miao Xie | df0af1a | 2013-01-29 10:11:59 +0000 | [diff] [blame] | 1713 | spin_lock(&BTRFS_I(inode)->lock); |
Yan, Zheng | 0ca1f7c | 2010-05-16 10:48:47 -0400 | [diff] [blame] | 1714 | BTRFS_I(inode)->delalloc_bytes += len; |
Wang Shilong | 47059d9 | 2014-07-03 18:22:07 +0800 | [diff] [blame] | 1715 | if (*bits & EXTENT_DEFRAG) |
| 1716 | BTRFS_I(inode)->defrag_bytes += len; |
Miao Xie | df0af1a | 2013-01-29 10:11:59 +0000 | [diff] [blame] | 1717 | if (do_list && !test_bit(BTRFS_INODE_IN_DELALLOC_LIST, |
Miao Xie | eb73c1b | 2013-05-15 07:48:22 +0000 | [diff] [blame] | 1718 | &BTRFS_I(inode)->runtime_flags)) |
| 1719 | btrfs_add_delalloc_inodes(root, inode); |
Miao Xie | df0af1a | 2013-01-29 10:11:59 +0000 | [diff] [blame] | 1720 | spin_unlock(&BTRFS_I(inode)->lock); |
Chris Mason | 291d673 | 2008-01-29 15:55:23 -0500 | [diff] [blame] | 1721 | } |
Chris Mason | 291d673 | 2008-01-29 15:55:23 -0500 | [diff] [blame] | 1722 | } |
| 1723 | |
Chris Mason | d352ac6 | 2008-09-29 15:18:18 -0400 | [diff] [blame] | 1724 | /* |
| 1725 | * extent_io.c clear_bit_hook, see set_bit_hook for why |
| 1726 | */ |
Jeff Mahoney | 1bf8504 | 2011-07-21 16:56:09 +0000 | [diff] [blame] | 1727 | static void btrfs_clear_bit_hook(struct inode *inode, |
David Sterba | 4107488 | 2013-04-29 13:38:46 +0000 | [diff] [blame] | 1728 | struct extent_state *state, |
David Sterba | 9ee49a04 | 2015-01-14 19:52:13 +0100 | [diff] [blame] | 1729 | unsigned *bits) |
Chris Mason | 291d673 | 2008-01-29 15:55:23 -0500 | [diff] [blame] | 1730 | { |
Wang Shilong | 47059d9 | 2014-07-03 18:22:07 +0800 | [diff] [blame] | 1731 | u64 len = state->end + 1 - state->start; |
Josef Bacik | dcab6a3 | 2015-02-11 15:08:59 -0500 | [diff] [blame] | 1732 | u64 num_extents = div64_u64(len + BTRFS_MAX_EXTENT_SIZE -1, |
| 1733 | BTRFS_MAX_EXTENT_SIZE); |
Wang Shilong | 47059d9 | 2014-07-03 18:22:07 +0800 | [diff] [blame] | 1734 | |
| 1735 | spin_lock(&BTRFS_I(inode)->lock); |
| 1736 | if ((state->state & EXTENT_DEFRAG) && (*bits & EXTENT_DEFRAG)) |
| 1737 | BTRFS_I(inode)->defrag_bytes -= len; |
| 1738 | spin_unlock(&BTRFS_I(inode)->lock); |
| 1739 | |
Chris Mason | 75eff68 | 2008-12-15 15:54:40 -0500 | [diff] [blame] | 1740 | /* |
| 1741 | * set_bit and clear bit hooks normally require _irqsave/restore |
Sergei Trofimovich | 27160b6 | 2011-05-20 20:20:32 +0000 | [diff] [blame] | 1742 | * but in this case, we are only testing for the DELALLOC |
Chris Mason | 75eff68 | 2008-12-15 15:54:40 -0500 | [diff] [blame] | 1743 | * bit, which is only set or cleared with irqs on |
| 1744 | */ |
Yan, Zheng | 0ca1f7c | 2010-05-16 10:48:47 -0400 | [diff] [blame] | 1745 | if ((state->state & EXTENT_DELALLOC) && (*bits & EXTENT_DELALLOC)) { |
Chris Mason | 291d673 | 2008-01-29 15:55:23 -0500 | [diff] [blame] | 1746 | struct btrfs_root *root = BTRFS_I(inode)->root; |
Liu Bo | 83eea1f | 2012-07-10 05:28:39 -0600 | [diff] [blame] | 1747 | bool do_list = !btrfs_is_free_space_inode(inode); |
Chris Mason | bcbfce8 | 2008-04-22 13:26:47 -0400 | [diff] [blame] | 1748 | |
Josef Bacik | 9e0baf6 | 2011-07-15 15:16:44 +0000 | [diff] [blame] | 1749 | if (*bits & EXTENT_FIRST_DELALLOC) { |
Yan, Zheng | 0ca1f7c | 2010-05-16 10:48:47 -0400 | [diff] [blame] | 1750 | *bits &= ~EXTENT_FIRST_DELALLOC; |
Josef Bacik | 9e0baf6 | 2011-07-15 15:16:44 +0000 | [diff] [blame] | 1751 | } else if (!(*bits & EXTENT_DO_ACCOUNTING)) { |
| 1752 | spin_lock(&BTRFS_I(inode)->lock); |
Josef Bacik | dcab6a3 | 2015-02-11 15:08:59 -0500 | [diff] [blame] | 1753 | BTRFS_I(inode)->outstanding_extents -= num_extents; |
Josef Bacik | 9e0baf6 | 2011-07-15 15:16:44 +0000 | [diff] [blame] | 1754 | spin_unlock(&BTRFS_I(inode)->lock); |
| 1755 | } |
Yan, Zheng | 0ca1f7c | 2010-05-16 10:48:47 -0400 | [diff] [blame] | 1756 | |
Josef Bacik | b6d08f0 | 2013-09-27 14:57:43 -0400 | [diff] [blame] | 1757 | /* |
| 1758 | * We don't reserve metadata space for space cache inodes so we |
| 1759 | * don't need to call dellalloc_release_metadata if there is an |
| 1760 | * error. |
| 1761 | */ |
| 1762 | if (*bits & EXTENT_DO_ACCOUNTING && |
| 1763 | root != root->fs_info->tree_root) |
Yan, Zheng | 0ca1f7c | 2010-05-16 10:48:47 -0400 | [diff] [blame] | 1764 | btrfs_delalloc_release_metadata(inode, len); |
| 1765 | |
Josef Bacik | 6a3891c | 2015-03-16 17:38:52 -0400 | [diff] [blame] | 1766 | /* For sanity tests. */ |
| 1767 | if (btrfs_test_is_dummy_root(root)) |
| 1768 | return; |
| 1769 | |
Josef Bacik | 0cb59c9 | 2010-07-02 12:14:14 -0400 | [diff] [blame] | 1770 | if (root->root_key.objectid != BTRFS_DATA_RELOC_TREE_OBJECTID |
Josef Bacik | 7ee9e44 | 2013-06-21 16:37:03 -0400 | [diff] [blame] | 1771 | && do_list && !(state->state & EXTENT_NORESERVE)) |
Qu Wenruo | df48063 | 2015-09-08 17:25:54 +0800 | [diff] [blame^] | 1772 | __btrfs_free_reserved_data_space(inode, state->start, |
| 1773 | len); |
Josef Bacik | 9ed74f2 | 2009-09-11 16:12:44 -0400 | [diff] [blame] | 1774 | |
Miao Xie | 963d678 | 2013-01-29 10:10:51 +0000 | [diff] [blame] | 1775 | __percpu_counter_add(&root->fs_info->delalloc_bytes, -len, |
| 1776 | root->fs_info->delalloc_batch); |
Miao Xie | df0af1a | 2013-01-29 10:11:59 +0000 | [diff] [blame] | 1777 | spin_lock(&BTRFS_I(inode)->lock); |
Yan, Zheng | 0ca1f7c | 2010-05-16 10:48:47 -0400 | [diff] [blame] | 1778 | BTRFS_I(inode)->delalloc_bytes -= len; |
Josef Bacik | 0cb59c9 | 2010-07-02 12:14:14 -0400 | [diff] [blame] | 1779 | if (do_list && BTRFS_I(inode)->delalloc_bytes == 0 && |
Miao Xie | df0af1a | 2013-01-29 10:11:59 +0000 | [diff] [blame] | 1780 | test_bit(BTRFS_INODE_IN_DELALLOC_LIST, |
Miao Xie | eb73c1b | 2013-05-15 07:48:22 +0000 | [diff] [blame] | 1781 | &BTRFS_I(inode)->runtime_flags)) |
| 1782 | btrfs_del_delalloc_inode(root, inode); |
Miao Xie | df0af1a | 2013-01-29 10:11:59 +0000 | [diff] [blame] | 1783 | spin_unlock(&BTRFS_I(inode)->lock); |
Chris Mason | 291d673 | 2008-01-29 15:55:23 -0500 | [diff] [blame] | 1784 | } |
Chris Mason | 291d673 | 2008-01-29 15:55:23 -0500 | [diff] [blame] | 1785 | } |
| 1786 | |
Chris Mason | d352ac6 | 2008-09-29 15:18:18 -0400 | [diff] [blame] | 1787 | /* |
| 1788 | * extent_io.c merge_bio_hook, this must check the chunk tree to make sure |
| 1789 | * we don't create bios that span stripes or chunks |
| 1790 | */ |
David Woodhouse | 64a1670 | 2009-07-15 23:29:37 +0100 | [diff] [blame] | 1791 | int btrfs_merge_bio_hook(int rw, struct page *page, unsigned long offset, |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 1792 | size_t size, struct bio *bio, |
| 1793 | unsigned long bio_flags) |
Chris Mason | 239b14b | 2008-03-24 15:02:07 -0400 | [diff] [blame] | 1794 | { |
| 1795 | struct btrfs_root *root = BTRFS_I(page->mapping->host)->root; |
Kent Overstreet | 4f024f3 | 2013-10-11 15:44:27 -0700 | [diff] [blame] | 1796 | u64 logical = (u64)bio->bi_iter.bi_sector << 9; |
Chris Mason | 239b14b | 2008-03-24 15:02:07 -0400 | [diff] [blame] | 1797 | u64 length = 0; |
| 1798 | u64 map_length; |
Chris Mason | 239b14b | 2008-03-24 15:02:07 -0400 | [diff] [blame] | 1799 | int ret; |
| 1800 | |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 1801 | if (bio_flags & EXTENT_BIO_COMPRESSED) |
| 1802 | return 0; |
| 1803 | |
Kent Overstreet | 4f024f3 | 2013-10-11 15:44:27 -0700 | [diff] [blame] | 1804 | length = bio->bi_iter.bi_size; |
Chris Mason | 239b14b | 2008-03-24 15:02:07 -0400 | [diff] [blame] | 1805 | map_length = length; |
David Woodhouse | 64a1670 | 2009-07-15 23:29:37 +0100 | [diff] [blame] | 1806 | ret = btrfs_map_block(root->fs_info, rw, logical, |
Chris Mason | f188591 | 2008-04-09 16:28:12 -0400 | [diff] [blame] | 1807 | &map_length, NULL, 0); |
Stefan Behrens | 3ec706c | 2012-11-05 15:46:42 +0100 | [diff] [blame] | 1808 | /* Will always return 0 with map_multi == NULL */ |
Jeff Mahoney | 3444a97 | 2011-10-03 23:23:13 -0400 | [diff] [blame] | 1809 | BUG_ON(ret < 0); |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 1810 | if (map_length < length + size) |
Chris Mason | 239b14b | 2008-03-24 15:02:07 -0400 | [diff] [blame] | 1811 | return 1; |
Jeff Mahoney | 3444a97 | 2011-10-03 23:23:13 -0400 | [diff] [blame] | 1812 | return 0; |
Chris Mason | 239b14b | 2008-03-24 15:02:07 -0400 | [diff] [blame] | 1813 | } |
| 1814 | |
Chris Mason | d352ac6 | 2008-09-29 15:18:18 -0400 | [diff] [blame] | 1815 | /* |
| 1816 | * in order to insert checksums into the metadata in large chunks, |
| 1817 | * we wait until bio submission time. All the pages in the bio are |
| 1818 | * checksummed and sums are attached onto the ordered extent record. |
| 1819 | * |
| 1820 | * At IO completion time the cums attached on the ordered extent record |
| 1821 | * are inserted into the btree |
| 1822 | */ |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 1823 | static int __btrfs_submit_bio_start(struct inode *inode, int rw, |
| 1824 | struct bio *bio, int mirror_num, |
Chris Mason | eaf25d9 | 2010-05-25 09:48:28 -0400 | [diff] [blame] | 1825 | unsigned long bio_flags, |
| 1826 | u64 bio_offset) |
Chris Mason | 065631f | 2008-02-20 12:07:25 -0500 | [diff] [blame] | 1827 | { |
Chris Mason | 065631f | 2008-02-20 12:07:25 -0500 | [diff] [blame] | 1828 | struct btrfs_root *root = BTRFS_I(inode)->root; |
Chris Mason | 065631f | 2008-02-20 12:07:25 -0500 | [diff] [blame] | 1829 | int ret = 0; |
Chris Mason | e015640 | 2008-04-16 11:15:20 -0400 | [diff] [blame] | 1830 | |
Chris Mason | d20f704 | 2008-12-08 16:58:54 -0500 | [diff] [blame] | 1831 | ret = btrfs_csum_one_bio(root, inode, bio, 0, 0); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 1832 | BUG_ON(ret); /* -ENOMEM */ |
Chris Mason | 4a69a41 | 2008-11-06 22:03:00 -0500 | [diff] [blame] | 1833 | return 0; |
| 1834 | } |
Chris Mason | e015640 | 2008-04-16 11:15:20 -0400 | [diff] [blame] | 1835 | |
Chris Mason | 4a69a41 | 2008-11-06 22:03:00 -0500 | [diff] [blame] | 1836 | /* |
| 1837 | * in order to insert checksums into the metadata in large chunks, |
| 1838 | * we wait until bio submission time. All the pages in the bio are |
| 1839 | * checksummed and sums are attached onto the ordered extent record. |
| 1840 | * |
| 1841 | * At IO completion time the cums attached on the ordered extent record |
| 1842 | * are inserted into the btree |
| 1843 | */ |
Christoph Hellwig | b295086 | 2008-12-02 09:54:17 -0500 | [diff] [blame] | 1844 | static int __btrfs_submit_bio_done(struct inode *inode, int rw, struct bio *bio, |
Chris Mason | eaf25d9 | 2010-05-25 09:48:28 -0400 | [diff] [blame] | 1845 | int mirror_num, unsigned long bio_flags, |
| 1846 | u64 bio_offset) |
Chris Mason | 4a69a41 | 2008-11-06 22:03:00 -0500 | [diff] [blame] | 1847 | { |
| 1848 | struct btrfs_root *root = BTRFS_I(inode)->root; |
Stefan Behrens | 6189192 | 2012-11-05 18:51:52 +0100 | [diff] [blame] | 1849 | int ret; |
| 1850 | |
| 1851 | ret = btrfs_map_bio(root, rw, bio, mirror_num, 1); |
Christoph Hellwig | 4246a0b | 2015-07-20 15:29:37 +0200 | [diff] [blame] | 1852 | if (ret) { |
| 1853 | bio->bi_error = ret; |
| 1854 | bio_endio(bio); |
| 1855 | } |
Stefan Behrens | 6189192 | 2012-11-05 18:51:52 +0100 | [diff] [blame] | 1856 | return ret; |
Chris Mason | 44b8bd7 | 2008-04-16 11:14:51 -0400 | [diff] [blame] | 1857 | } |
| 1858 | |
Chris Mason | d352ac6 | 2008-09-29 15:18:18 -0400 | [diff] [blame] | 1859 | /* |
Chris Mason | cad321a | 2008-12-17 14:51:42 -0500 | [diff] [blame] | 1860 | * extent_io.c submission hook. This does the right thing for csum calculation |
| 1861 | * on write, or reading the csums from the tree before a read |
Chris Mason | d352ac6 | 2008-09-29 15:18:18 -0400 | [diff] [blame] | 1862 | */ |
Christoph Hellwig | b295086 | 2008-12-02 09:54:17 -0500 | [diff] [blame] | 1863 | static int btrfs_submit_bio_hook(struct inode *inode, int rw, struct bio *bio, |
Chris Mason | eaf25d9 | 2010-05-25 09:48:28 -0400 | [diff] [blame] | 1864 | int mirror_num, unsigned long bio_flags, |
| 1865 | u64 bio_offset) |
Chris Mason | 44b8bd7 | 2008-04-16 11:14:51 -0400 | [diff] [blame] | 1866 | { |
| 1867 | struct btrfs_root *root = BTRFS_I(inode)->root; |
Chandan Rajendra | 0d51e28 | 2015-07-27 15:26:43 +0530 | [diff] [blame] | 1868 | enum btrfs_wq_endio_type metadata = BTRFS_WQ_ENDIO_DATA; |
Chris Mason | 44b8bd7 | 2008-04-16 11:14:51 -0400 | [diff] [blame] | 1869 | int ret = 0; |
Chris Mason | 19b9bdb | 2008-10-30 14:23:13 -0400 | [diff] [blame] | 1870 | int skip_sum; |
Josef Bacik | b812ce2 | 2012-11-16 13:56:32 -0500 | [diff] [blame] | 1871 | int async = !atomic_read(&BTRFS_I(inode)->sync_writers); |
Chris Mason | 44b8bd7 | 2008-04-16 11:14:51 -0400 | [diff] [blame] | 1872 | |
Christoph Hellwig | 6cbff00 | 2009-04-17 10:37:41 +0200 | [diff] [blame] | 1873 | skip_sum = BTRFS_I(inode)->flags & BTRFS_INODE_NODATASUM; |
Chris Mason | cad321a | 2008-12-17 14:51:42 -0500 | [diff] [blame] | 1874 | |
Liu Bo | 83eea1f | 2012-07-10 05:28:39 -0600 | [diff] [blame] | 1875 | if (btrfs_is_free_space_inode(inode)) |
Chandan Rajendra | 0d51e28 | 2015-07-27 15:26:43 +0530 | [diff] [blame] | 1876 | metadata = BTRFS_WQ_ENDIO_FREE_SPACE; |
Jeff Mahoney | 0417341 | 2011-10-03 23:23:12 -0400 | [diff] [blame] | 1877 | |
Christoph Hellwig | 7b6d91d | 2010-08-07 18:20:39 +0200 | [diff] [blame] | 1878 | if (!(rw & REQ_WRITE)) { |
Josef Bacik | 5fd0204 | 2012-05-02 14:00:54 -0400 | [diff] [blame] | 1879 | ret = btrfs_bio_wq_end_io(root->fs_info, bio, metadata); |
| 1880 | if (ret) |
Stefan Behrens | 6189192 | 2012-11-05 18:51:52 +0100 | [diff] [blame] | 1881 | goto out; |
Josef Bacik | 5fd0204 | 2012-05-02 14:00:54 -0400 | [diff] [blame] | 1882 | |
Chris Mason | d20f704 | 2008-12-08 16:58:54 -0500 | [diff] [blame] | 1883 | if (bio_flags & EXTENT_BIO_COMPRESSED) { |
Stefan Behrens | 6189192 | 2012-11-05 18:51:52 +0100 | [diff] [blame] | 1884 | ret = btrfs_submit_compressed_read(inode, bio, |
| 1885 | mirror_num, |
| 1886 | bio_flags); |
| 1887 | goto out; |
Tsutomu Itoh | c2db107 | 2011-03-01 06:48:31 +0000 | [diff] [blame] | 1888 | } else if (!skip_sum) { |
| 1889 | ret = btrfs_lookup_bio_sums(root, inode, bio, NULL); |
| 1890 | if (ret) |
Stefan Behrens | 6189192 | 2012-11-05 18:51:52 +0100 | [diff] [blame] | 1891 | goto out; |
Tsutomu Itoh | c2db107 | 2011-03-01 06:48:31 +0000 | [diff] [blame] | 1892 | } |
Chris Mason | 4d1b5fb | 2008-08-20 09:44:52 -0400 | [diff] [blame] | 1893 | goto mapit; |
Josef Bacik | b812ce2 | 2012-11-16 13:56:32 -0500 | [diff] [blame] | 1894 | } else if (async && !skip_sum) { |
Yan Zheng | 17d217f | 2008-12-12 10:03:38 -0500 | [diff] [blame] | 1895 | /* csum items have already been cloned */ |
| 1896 | if (root->root_key.objectid == BTRFS_DATA_RELOC_TREE_OBJECTID) |
| 1897 | goto mapit; |
Chris Mason | 19b9bdb | 2008-10-30 14:23:13 -0400 | [diff] [blame] | 1898 | /* we're doing a write, do the async checksumming */ |
Stefan Behrens | 6189192 | 2012-11-05 18:51:52 +0100 | [diff] [blame] | 1899 | ret = btrfs_wq_submit_bio(BTRFS_I(inode)->root->fs_info, |
Chris Mason | 44b8bd7 | 2008-04-16 11:14:51 -0400 | [diff] [blame] | 1900 | inode, rw, bio, mirror_num, |
Chris Mason | eaf25d9 | 2010-05-25 09:48:28 -0400 | [diff] [blame] | 1901 | bio_flags, bio_offset, |
| 1902 | __btrfs_submit_bio_start, |
Chris Mason | 4a69a41 | 2008-11-06 22:03:00 -0500 | [diff] [blame] | 1903 | __btrfs_submit_bio_done); |
Stefan Behrens | 6189192 | 2012-11-05 18:51:52 +0100 | [diff] [blame] | 1904 | goto out; |
Josef Bacik | b812ce2 | 2012-11-16 13:56:32 -0500 | [diff] [blame] | 1905 | } else if (!skip_sum) { |
| 1906 | ret = btrfs_csum_one_bio(root, inode, bio, 0, 0); |
| 1907 | if (ret) |
| 1908 | goto out; |
Chris Mason | 19b9bdb | 2008-10-30 14:23:13 -0400 | [diff] [blame] | 1909 | } |
| 1910 | |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 1911 | mapit: |
Stefan Behrens | 6189192 | 2012-11-05 18:51:52 +0100 | [diff] [blame] | 1912 | ret = btrfs_map_bio(root, rw, bio, mirror_num, 0); |
| 1913 | |
| 1914 | out: |
Christoph Hellwig | 4246a0b | 2015-07-20 15:29:37 +0200 | [diff] [blame] | 1915 | if (ret < 0) { |
| 1916 | bio->bi_error = ret; |
| 1917 | bio_endio(bio); |
| 1918 | } |
Stefan Behrens | 6189192 | 2012-11-05 18:51:52 +0100 | [diff] [blame] | 1919 | return ret; |
Chris Mason | 065631f | 2008-02-20 12:07:25 -0500 | [diff] [blame] | 1920 | } |
Chris Mason | 6885f30 | 2008-02-20 16:11:05 -0500 | [diff] [blame] | 1921 | |
Chris Mason | d352ac6 | 2008-09-29 15:18:18 -0400 | [diff] [blame] | 1922 | /* |
| 1923 | * given a list of ordered sums record them in the inode. This happens |
| 1924 | * at IO completion time based on sums calculated at bio submission time. |
| 1925 | */ |
Chris Mason | ba1da2f | 2008-07-17 12:54:15 -0400 | [diff] [blame] | 1926 | static noinline int add_pending_csums(struct btrfs_trans_handle *trans, |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 1927 | struct inode *inode, u64 file_offset, |
| 1928 | struct list_head *list) |
| 1929 | { |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 1930 | struct btrfs_ordered_sum *sum; |
| 1931 | |
Qinghuang Feng | c6e3087 | 2009-01-21 10:59:08 -0500 | [diff] [blame] | 1932 | list_for_each_entry(sum, list, list) { |
Miao Xie | 39847c4 | 2013-03-28 08:08:20 +0000 | [diff] [blame] | 1933 | trans->adding_csums = 1; |
Chris Mason | d20f704 | 2008-12-08 16:58:54 -0500 | [diff] [blame] | 1934 | btrfs_csum_file_blocks(trans, |
| 1935 | BTRFS_I(inode)->root->fs_info->csum_root, sum); |
Miao Xie | 39847c4 | 2013-03-28 08:08:20 +0000 | [diff] [blame] | 1936 | trans->adding_csums = 0; |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 1937 | } |
| 1938 | return 0; |
| 1939 | } |
| 1940 | |
Josef Bacik | 2ac55d4 | 2010-02-03 19:33:23 +0000 | [diff] [blame] | 1941 | int btrfs_set_extent_delalloc(struct inode *inode, u64 start, u64 end, |
| 1942 | struct extent_state **cached_state) |
Chris Mason | ea8c281 | 2008-08-04 23:17:27 -0400 | [diff] [blame] | 1943 | { |
Julia Lawall | 6c1500f | 2012-11-03 20:30:18 +0000 | [diff] [blame] | 1944 | WARN_ON((end & (PAGE_CACHE_SIZE - 1)) == 0); |
Chris Mason | ea8c281 | 2008-08-04 23:17:27 -0400 | [diff] [blame] | 1945 | return set_extent_delalloc(&BTRFS_I(inode)->io_tree, start, end, |
Josef Bacik | 2ac55d4 | 2010-02-03 19:33:23 +0000 | [diff] [blame] | 1946 | cached_state, GFP_NOFS); |
Chris Mason | ea8c281 | 2008-08-04 23:17:27 -0400 | [diff] [blame] | 1947 | } |
| 1948 | |
Chris Mason | d352ac6 | 2008-09-29 15:18:18 -0400 | [diff] [blame] | 1949 | /* see btrfs_writepage_start_hook for details on why this is required */ |
Chris Mason | 247e743 | 2008-07-17 12:53:51 -0400 | [diff] [blame] | 1950 | struct btrfs_writepage_fixup { |
| 1951 | struct page *page; |
| 1952 | struct btrfs_work work; |
| 1953 | }; |
| 1954 | |
Christoph Hellwig | b295086 | 2008-12-02 09:54:17 -0500 | [diff] [blame] | 1955 | static void btrfs_writepage_fixup_worker(struct btrfs_work *work) |
Chris Mason | 247e743 | 2008-07-17 12:53:51 -0400 | [diff] [blame] | 1956 | { |
| 1957 | struct btrfs_writepage_fixup *fixup; |
| 1958 | struct btrfs_ordered_extent *ordered; |
Josef Bacik | 2ac55d4 | 2010-02-03 19:33:23 +0000 | [diff] [blame] | 1959 | struct extent_state *cached_state = NULL; |
Chris Mason | 247e743 | 2008-07-17 12:53:51 -0400 | [diff] [blame] | 1960 | struct page *page; |
| 1961 | struct inode *inode; |
| 1962 | u64 page_start; |
| 1963 | u64 page_end; |
Jeff Mahoney | 87826df | 2012-02-15 16:23:57 +0100 | [diff] [blame] | 1964 | int ret; |
Chris Mason | 247e743 | 2008-07-17 12:53:51 -0400 | [diff] [blame] | 1965 | |
| 1966 | fixup = container_of(work, struct btrfs_writepage_fixup, work); |
| 1967 | page = fixup->page; |
Chris Mason | 4a09675 | 2008-07-21 10:29:44 -0400 | [diff] [blame] | 1968 | again: |
Chris Mason | 247e743 | 2008-07-17 12:53:51 -0400 | [diff] [blame] | 1969 | lock_page(page); |
| 1970 | if (!page->mapping || !PageDirty(page) || !PageChecked(page)) { |
| 1971 | ClearPageChecked(page); |
| 1972 | goto out_page; |
| 1973 | } |
| 1974 | |
| 1975 | inode = page->mapping->host; |
| 1976 | page_start = page_offset(page); |
| 1977 | page_end = page_offset(page) + PAGE_CACHE_SIZE - 1; |
| 1978 | |
Josef Bacik | 2ac55d4 | 2010-02-03 19:33:23 +0000 | [diff] [blame] | 1979 | lock_extent_bits(&BTRFS_I(inode)->io_tree, page_start, page_end, 0, |
Jeff Mahoney | d008237 | 2012-03-01 14:57:19 +0100 | [diff] [blame] | 1980 | &cached_state); |
Chris Mason | 4a09675 | 2008-07-21 10:29:44 -0400 | [diff] [blame] | 1981 | |
| 1982 | /* already ordered? We're done */ |
Chris Mason | 8b62b72 | 2009-09-02 16:53:46 -0400 | [diff] [blame] | 1983 | if (PagePrivate2(page)) |
Chris Mason | 247e743 | 2008-07-17 12:53:51 -0400 | [diff] [blame] | 1984 | goto out; |
Chris Mason | 4a09675 | 2008-07-21 10:29:44 -0400 | [diff] [blame] | 1985 | |
| 1986 | ordered = btrfs_lookup_ordered_extent(inode, page_start); |
| 1987 | if (ordered) { |
Josef Bacik | 2ac55d4 | 2010-02-03 19:33:23 +0000 | [diff] [blame] | 1988 | unlock_extent_cached(&BTRFS_I(inode)->io_tree, page_start, |
| 1989 | page_end, &cached_state, GFP_NOFS); |
Chris Mason | 4a09675 | 2008-07-21 10:29:44 -0400 | [diff] [blame] | 1990 | unlock_page(page); |
| 1991 | btrfs_start_ordered_extent(inode, ordered, 1); |
Jeff Mahoney | 87826df | 2012-02-15 16:23:57 +0100 | [diff] [blame] | 1992 | btrfs_put_ordered_extent(ordered); |
Chris Mason | 4a09675 | 2008-07-21 10:29:44 -0400 | [diff] [blame] | 1993 | goto again; |
| 1994 | } |
Chris Mason | 247e743 | 2008-07-17 12:53:51 -0400 | [diff] [blame] | 1995 | |
Qu Wenruo | df48063 | 2015-09-08 17:25:54 +0800 | [diff] [blame^] | 1996 | ret = __btrfs_delalloc_reserve_space(inode, page_start, |
| 1997 | PAGE_CACHE_SIZE); |
Jeff Mahoney | 87826df | 2012-02-15 16:23:57 +0100 | [diff] [blame] | 1998 | if (ret) { |
| 1999 | mapping_set_error(page->mapping, ret); |
| 2000 | end_extent_writepage(page, ret, page_start, page_end); |
| 2001 | ClearPageChecked(page); |
| 2002 | goto out; |
| 2003 | } |
| 2004 | |
Josef Bacik | 2ac55d4 | 2010-02-03 19:33:23 +0000 | [diff] [blame] | 2005 | btrfs_set_extent_delalloc(inode, page_start, page_end, &cached_state); |
Chris Mason | 247e743 | 2008-07-17 12:53:51 -0400 | [diff] [blame] | 2006 | ClearPageChecked(page); |
Jeff Mahoney | 87826df | 2012-02-15 16:23:57 +0100 | [diff] [blame] | 2007 | set_page_dirty(page); |
Chris Mason | 247e743 | 2008-07-17 12:53:51 -0400 | [diff] [blame] | 2008 | out: |
Josef Bacik | 2ac55d4 | 2010-02-03 19:33:23 +0000 | [diff] [blame] | 2009 | unlock_extent_cached(&BTRFS_I(inode)->io_tree, page_start, page_end, |
| 2010 | &cached_state, GFP_NOFS); |
Chris Mason | 247e743 | 2008-07-17 12:53:51 -0400 | [diff] [blame] | 2011 | out_page: |
| 2012 | unlock_page(page); |
| 2013 | page_cache_release(page); |
Miao Xie | b897abe | 2011-01-26 16:19:22 +0800 | [diff] [blame] | 2014 | kfree(fixup); |
Chris Mason | 247e743 | 2008-07-17 12:53:51 -0400 | [diff] [blame] | 2015 | } |
| 2016 | |
| 2017 | /* |
| 2018 | * There are a few paths in the higher layers of the kernel that directly |
| 2019 | * set the page dirty bit without asking the filesystem if it is a |
| 2020 | * good idea. This causes problems because we want to make sure COW |
| 2021 | * properly happens and the data=ordered rules are followed. |
| 2022 | * |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 2023 | * 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] | 2024 | * hasn't been properly setup for IO. We kick off an async process |
| 2025 | * to fix it up. The async helper will wait for ordered extents, set |
| 2026 | * the delalloc bit and make it safe to write the page. |
| 2027 | */ |
Christoph Hellwig | b295086 | 2008-12-02 09:54:17 -0500 | [diff] [blame] | 2028 | 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] | 2029 | { |
| 2030 | struct inode *inode = page->mapping->host; |
| 2031 | struct btrfs_writepage_fixup *fixup; |
| 2032 | struct btrfs_root *root = BTRFS_I(inode)->root; |
Chris Mason | 247e743 | 2008-07-17 12:53:51 -0400 | [diff] [blame] | 2033 | |
Chris Mason | 8b62b72 | 2009-09-02 16:53:46 -0400 | [diff] [blame] | 2034 | /* this page is properly in the ordered list */ |
| 2035 | if (TestClearPagePrivate2(page)) |
Chris Mason | 247e743 | 2008-07-17 12:53:51 -0400 | [diff] [blame] | 2036 | return 0; |
| 2037 | |
| 2038 | if (PageChecked(page)) |
| 2039 | return -EAGAIN; |
| 2040 | |
| 2041 | fixup = kzalloc(sizeof(*fixup), GFP_NOFS); |
| 2042 | if (!fixup) |
| 2043 | return -EAGAIN; |
Chris Mason | f421950 | 2008-07-22 11:18:09 -0400 | [diff] [blame] | 2044 | |
Chris Mason | 247e743 | 2008-07-17 12:53:51 -0400 | [diff] [blame] | 2045 | SetPageChecked(page); |
| 2046 | page_cache_get(page); |
Liu Bo | 9e0af23 | 2014-08-15 23:36:53 +0800 | [diff] [blame] | 2047 | btrfs_init_work(&fixup->work, btrfs_fixup_helper, |
| 2048 | btrfs_writepage_fixup_worker, NULL, NULL); |
Chris Mason | 247e743 | 2008-07-17 12:53:51 -0400 | [diff] [blame] | 2049 | fixup->page = page; |
Qu Wenruo | dc6e320 | 2014-02-28 10:46:14 +0800 | [diff] [blame] | 2050 | btrfs_queue_work(root->fs_info->fixup_workers, &fixup->work); |
Jeff Mahoney | 87826df | 2012-02-15 16:23:57 +0100 | [diff] [blame] | 2051 | return -EBUSY; |
Chris Mason | 247e743 | 2008-07-17 12:53:51 -0400 | [diff] [blame] | 2052 | } |
| 2053 | |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 2054 | static int insert_reserved_file_extent(struct btrfs_trans_handle *trans, |
| 2055 | struct inode *inode, u64 file_pos, |
| 2056 | u64 disk_bytenr, u64 disk_num_bytes, |
| 2057 | u64 num_bytes, u64 ram_bytes, |
| 2058 | u8 compression, u8 encryption, |
| 2059 | u16 other_encoding, int extent_type) |
| 2060 | { |
| 2061 | struct btrfs_root *root = BTRFS_I(inode)->root; |
| 2062 | struct btrfs_file_extent_item *fi; |
| 2063 | struct btrfs_path *path; |
| 2064 | struct extent_buffer *leaf; |
| 2065 | struct btrfs_key ins; |
Filipe David Borba Manana | 1acae57 | 2014-01-07 11:42:27 +0000 | [diff] [blame] | 2066 | int extent_inserted = 0; |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 2067 | int ret; |
| 2068 | |
| 2069 | path = btrfs_alloc_path(); |
Mark Fasheh | d8926bb | 2011-07-13 10:38:47 -0700 | [diff] [blame] | 2070 | if (!path) |
| 2071 | return -ENOMEM; |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 2072 | |
Chris Mason | a1ed835 | 2009-09-11 12:27:37 -0400 | [diff] [blame] | 2073 | /* |
| 2074 | * we may be replacing one extent in the tree with another. |
| 2075 | * The new extent is pinned in the extent map, and we don't want |
| 2076 | * to drop it from the cache until it is completely in the btree. |
| 2077 | * |
| 2078 | * So, tell btrfs_drop_extents to leave this extent in the cache. |
| 2079 | * the caller is expected to unpin it and allow it to be merged |
| 2080 | * with the others. |
| 2081 | */ |
Filipe David Borba Manana | 1acae57 | 2014-01-07 11:42:27 +0000 | [diff] [blame] | 2082 | ret = __btrfs_drop_extents(trans, root, inode, path, file_pos, |
| 2083 | file_pos + num_bytes, NULL, 0, |
| 2084 | 1, sizeof(*fi), &extent_inserted); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 2085 | if (ret) |
| 2086 | goto out; |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 2087 | |
Filipe David Borba Manana | 1acae57 | 2014-01-07 11:42:27 +0000 | [diff] [blame] | 2088 | if (!extent_inserted) { |
| 2089 | ins.objectid = btrfs_ino(inode); |
| 2090 | ins.offset = file_pos; |
| 2091 | ins.type = BTRFS_EXTENT_DATA_KEY; |
| 2092 | |
| 2093 | path->leave_spinning = 1; |
| 2094 | ret = btrfs_insert_empty_item(trans, root, path, &ins, |
| 2095 | sizeof(*fi)); |
| 2096 | if (ret) |
| 2097 | goto out; |
| 2098 | } |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 2099 | leaf = path->nodes[0]; |
| 2100 | fi = btrfs_item_ptr(leaf, path->slots[0], |
| 2101 | struct btrfs_file_extent_item); |
| 2102 | btrfs_set_file_extent_generation(leaf, fi, trans->transid); |
| 2103 | btrfs_set_file_extent_type(leaf, fi, extent_type); |
| 2104 | btrfs_set_file_extent_disk_bytenr(leaf, fi, disk_bytenr); |
| 2105 | btrfs_set_file_extent_disk_num_bytes(leaf, fi, disk_num_bytes); |
| 2106 | btrfs_set_file_extent_offset(leaf, fi, 0); |
| 2107 | btrfs_set_file_extent_num_bytes(leaf, fi, num_bytes); |
| 2108 | btrfs_set_file_extent_ram_bytes(leaf, fi, ram_bytes); |
| 2109 | btrfs_set_file_extent_compression(leaf, fi, compression); |
| 2110 | btrfs_set_file_extent_encryption(leaf, fi, encryption); |
| 2111 | btrfs_set_file_extent_other_encoding(leaf, fi, other_encoding); |
Chris Mason | b947343 | 2009-03-13 11:00:37 -0400 | [diff] [blame] | 2112 | |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 2113 | btrfs_mark_buffer_dirty(leaf); |
Josef Bacik | ce19533 | 2012-09-25 15:26:16 -0400 | [diff] [blame] | 2114 | btrfs_release_path(path); |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 2115 | |
| 2116 | inode_add_bytes(inode, num_bytes); |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 2117 | |
| 2118 | ins.objectid = disk_bytenr; |
| 2119 | ins.offset = disk_num_bytes; |
| 2120 | ins.type = BTRFS_EXTENT_ITEM_KEY; |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 2121 | ret = btrfs_alloc_reserved_file_extent(trans, root, |
| 2122 | root->root_key.objectid, |
Li Zefan | 33345d01 | 2011-04-20 10:31:50 +0800 | [diff] [blame] | 2123 | btrfs_ino(inode), file_pos, &ins); |
Qu Wenruo | 297d750 | 2015-09-08 17:08:37 +0800 | [diff] [blame] | 2124 | if (ret < 0) |
| 2125 | goto out; |
| 2126 | /* |
| 2127 | * Release the reserved range from inode dirty range map, and |
| 2128 | * move it to delayed ref codes, as now accounting only happens at |
| 2129 | * commit_transaction() time. |
| 2130 | */ |
| 2131 | btrfs_qgroup_release_data(inode, file_pos, ram_bytes); |
| 2132 | ret = btrfs_add_delayed_qgroup_reserve(root->fs_info, trans, |
| 2133 | root->objectid, disk_bytenr, ram_bytes); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 2134 | out: |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 2135 | btrfs_free_path(path); |
Chris Mason | b947343 | 2009-03-13 11:00:37 -0400 | [diff] [blame] | 2136 | |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 2137 | return ret; |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 2138 | } |
| 2139 | |
Liu Bo | 38c227d | 2013-01-29 03:18:40 +0000 | [diff] [blame] | 2140 | /* snapshot-aware defrag */ |
| 2141 | struct sa_defrag_extent_backref { |
| 2142 | struct rb_node node; |
| 2143 | struct old_sa_defrag_extent *old; |
| 2144 | u64 root_id; |
| 2145 | u64 inum; |
| 2146 | u64 file_pos; |
| 2147 | u64 extent_offset; |
| 2148 | u64 num_bytes; |
| 2149 | u64 generation; |
| 2150 | }; |
| 2151 | |
| 2152 | struct old_sa_defrag_extent { |
| 2153 | struct list_head list; |
| 2154 | struct new_sa_defrag_extent *new; |
| 2155 | |
| 2156 | u64 extent_offset; |
| 2157 | u64 bytenr; |
| 2158 | u64 offset; |
| 2159 | u64 len; |
| 2160 | int count; |
| 2161 | }; |
| 2162 | |
| 2163 | struct new_sa_defrag_extent { |
| 2164 | struct rb_root root; |
| 2165 | struct list_head head; |
| 2166 | struct btrfs_path *path; |
| 2167 | struct inode *inode; |
| 2168 | u64 file_pos; |
| 2169 | u64 len; |
| 2170 | u64 bytenr; |
| 2171 | u64 disk_len; |
| 2172 | u8 compress_type; |
| 2173 | }; |
| 2174 | |
| 2175 | static int backref_comp(struct sa_defrag_extent_backref *b1, |
| 2176 | struct sa_defrag_extent_backref *b2) |
| 2177 | { |
| 2178 | if (b1->root_id < b2->root_id) |
| 2179 | return -1; |
| 2180 | else if (b1->root_id > b2->root_id) |
| 2181 | return 1; |
| 2182 | |
| 2183 | if (b1->inum < b2->inum) |
| 2184 | return -1; |
| 2185 | else if (b1->inum > b2->inum) |
| 2186 | return 1; |
| 2187 | |
| 2188 | if (b1->file_pos < b2->file_pos) |
| 2189 | return -1; |
| 2190 | else if (b1->file_pos > b2->file_pos) |
| 2191 | return 1; |
| 2192 | |
| 2193 | /* |
| 2194 | * [------------------------------] ===> (a range of space) |
| 2195 | * |<--->| |<---->| =============> (fs/file tree A) |
| 2196 | * |<---------------------------->| ===> (fs/file tree B) |
| 2197 | * |
| 2198 | * A range of space can refer to two file extents in one tree while |
| 2199 | * refer to only one file extent in another tree. |
| 2200 | * |
| 2201 | * So we may process a disk offset more than one time(two extents in A) |
| 2202 | * and locate at the same extent(one extent in B), then insert two same |
| 2203 | * backrefs(both refer to the extent in B). |
| 2204 | */ |
| 2205 | return 0; |
| 2206 | } |
| 2207 | |
| 2208 | static void backref_insert(struct rb_root *root, |
| 2209 | struct sa_defrag_extent_backref *backref) |
| 2210 | { |
| 2211 | struct rb_node **p = &root->rb_node; |
| 2212 | struct rb_node *parent = NULL; |
| 2213 | struct sa_defrag_extent_backref *entry; |
| 2214 | int ret; |
| 2215 | |
| 2216 | while (*p) { |
| 2217 | parent = *p; |
| 2218 | entry = rb_entry(parent, struct sa_defrag_extent_backref, node); |
| 2219 | |
| 2220 | ret = backref_comp(backref, entry); |
| 2221 | if (ret < 0) |
| 2222 | p = &(*p)->rb_left; |
| 2223 | else |
| 2224 | p = &(*p)->rb_right; |
| 2225 | } |
| 2226 | |
| 2227 | rb_link_node(&backref->node, parent, p); |
| 2228 | rb_insert_color(&backref->node, root); |
| 2229 | } |
| 2230 | |
| 2231 | /* |
| 2232 | * Note the backref might has changed, and in this case we just return 0. |
| 2233 | */ |
| 2234 | static noinline int record_one_backref(u64 inum, u64 offset, u64 root_id, |
| 2235 | void *ctx) |
| 2236 | { |
| 2237 | struct btrfs_file_extent_item *extent; |
| 2238 | struct btrfs_fs_info *fs_info; |
| 2239 | struct old_sa_defrag_extent *old = ctx; |
| 2240 | struct new_sa_defrag_extent *new = old->new; |
| 2241 | struct btrfs_path *path = new->path; |
| 2242 | struct btrfs_key key; |
| 2243 | struct btrfs_root *root; |
| 2244 | struct sa_defrag_extent_backref *backref; |
| 2245 | struct extent_buffer *leaf; |
| 2246 | struct inode *inode = new->inode; |
| 2247 | int slot; |
| 2248 | int ret; |
| 2249 | u64 extent_offset; |
| 2250 | u64 num_bytes; |
| 2251 | |
| 2252 | if (BTRFS_I(inode)->root->root_key.objectid == root_id && |
| 2253 | inum == btrfs_ino(inode)) |
| 2254 | return 0; |
| 2255 | |
| 2256 | key.objectid = root_id; |
| 2257 | key.type = BTRFS_ROOT_ITEM_KEY; |
| 2258 | key.offset = (u64)-1; |
| 2259 | |
| 2260 | fs_info = BTRFS_I(inode)->root->fs_info; |
| 2261 | root = btrfs_read_fs_root_no_name(fs_info, &key); |
| 2262 | if (IS_ERR(root)) { |
| 2263 | if (PTR_ERR(root) == -ENOENT) |
| 2264 | return 0; |
| 2265 | WARN_ON(1); |
| 2266 | pr_debug("inum=%llu, offset=%llu, root_id=%llu\n", |
| 2267 | inum, offset, root_id); |
| 2268 | return PTR_ERR(root); |
| 2269 | } |
| 2270 | |
| 2271 | key.objectid = inum; |
| 2272 | key.type = BTRFS_EXTENT_DATA_KEY; |
| 2273 | if (offset > (u64)-1 << 32) |
| 2274 | key.offset = 0; |
| 2275 | else |
| 2276 | key.offset = offset; |
| 2277 | |
| 2278 | ret = btrfs_search_slot(NULL, root, &key, path, 0, 0); |
Dulshani Gunawardhana | fae7f21 | 2013-10-31 10:30:08 +0530 | [diff] [blame] | 2279 | if (WARN_ON(ret < 0)) |
Liu Bo | 38c227d | 2013-01-29 03:18:40 +0000 | [diff] [blame] | 2280 | return ret; |
Josef Bacik | 50f1319 | 2013-07-22 12:50:37 -0400 | [diff] [blame] | 2281 | ret = 0; |
Liu Bo | 38c227d | 2013-01-29 03:18:40 +0000 | [diff] [blame] | 2282 | |
| 2283 | while (1) { |
| 2284 | cond_resched(); |
| 2285 | |
| 2286 | leaf = path->nodes[0]; |
| 2287 | slot = path->slots[0]; |
| 2288 | |
| 2289 | if (slot >= btrfs_header_nritems(leaf)) { |
| 2290 | ret = btrfs_next_leaf(root, path); |
| 2291 | if (ret < 0) { |
| 2292 | goto out; |
| 2293 | } else if (ret > 0) { |
| 2294 | ret = 0; |
| 2295 | goto out; |
| 2296 | } |
| 2297 | continue; |
| 2298 | } |
| 2299 | |
| 2300 | path->slots[0]++; |
| 2301 | |
| 2302 | btrfs_item_key_to_cpu(leaf, &key, slot); |
| 2303 | |
| 2304 | if (key.objectid > inum) |
| 2305 | goto out; |
| 2306 | |
| 2307 | if (key.objectid < inum || key.type != BTRFS_EXTENT_DATA_KEY) |
| 2308 | continue; |
| 2309 | |
| 2310 | extent = btrfs_item_ptr(leaf, slot, |
| 2311 | struct btrfs_file_extent_item); |
| 2312 | |
| 2313 | if (btrfs_file_extent_disk_bytenr(leaf, extent) != old->bytenr) |
| 2314 | continue; |
| 2315 | |
Liu Bo | e68afa4 | 2013-07-01 22:13:26 +0800 | [diff] [blame] | 2316 | /* |
| 2317 | * 'offset' refers to the exact key.offset, |
| 2318 | * NOT the 'offset' field in btrfs_extent_data_ref, ie. |
| 2319 | * (key.offset - extent_offset). |
| 2320 | */ |
| 2321 | if (key.offset != offset) |
Liu Bo | 38c227d | 2013-01-29 03:18:40 +0000 | [diff] [blame] | 2322 | continue; |
| 2323 | |
Liu Bo | e68afa4 | 2013-07-01 22:13:26 +0800 | [diff] [blame] | 2324 | extent_offset = btrfs_file_extent_offset(leaf, extent); |
Liu Bo | 38c227d | 2013-01-29 03:18:40 +0000 | [diff] [blame] | 2325 | num_bytes = btrfs_file_extent_num_bytes(leaf, extent); |
Liu Bo | e68afa4 | 2013-07-01 22:13:26 +0800 | [diff] [blame] | 2326 | |
Liu Bo | 38c227d | 2013-01-29 03:18:40 +0000 | [diff] [blame] | 2327 | if (extent_offset >= old->extent_offset + old->offset + |
| 2328 | old->len || extent_offset + num_bytes <= |
| 2329 | old->extent_offset + old->offset) |
| 2330 | continue; |
Liu Bo | 38c227d | 2013-01-29 03:18:40 +0000 | [diff] [blame] | 2331 | break; |
| 2332 | } |
| 2333 | |
| 2334 | backref = kmalloc(sizeof(*backref), GFP_NOFS); |
| 2335 | if (!backref) { |
| 2336 | ret = -ENOENT; |
| 2337 | goto out; |
| 2338 | } |
| 2339 | |
| 2340 | backref->root_id = root_id; |
| 2341 | backref->inum = inum; |
Liu Bo | e68afa4 | 2013-07-01 22:13:26 +0800 | [diff] [blame] | 2342 | backref->file_pos = offset; |
Liu Bo | 38c227d | 2013-01-29 03:18:40 +0000 | [diff] [blame] | 2343 | backref->num_bytes = num_bytes; |
| 2344 | backref->extent_offset = extent_offset; |
| 2345 | backref->generation = btrfs_file_extent_generation(leaf, extent); |
| 2346 | backref->old = old; |
| 2347 | backref_insert(&new->root, backref); |
| 2348 | old->count++; |
| 2349 | out: |
| 2350 | btrfs_release_path(path); |
| 2351 | WARN_ON(ret); |
| 2352 | return ret; |
| 2353 | } |
| 2354 | |
| 2355 | static noinline bool record_extent_backrefs(struct btrfs_path *path, |
| 2356 | struct new_sa_defrag_extent *new) |
| 2357 | { |
| 2358 | struct btrfs_fs_info *fs_info = BTRFS_I(new->inode)->root->fs_info; |
| 2359 | struct old_sa_defrag_extent *old, *tmp; |
| 2360 | int ret; |
| 2361 | |
| 2362 | new->path = path; |
| 2363 | |
| 2364 | list_for_each_entry_safe(old, tmp, &new->head, list) { |
Liu Bo | e68afa4 | 2013-07-01 22:13:26 +0800 | [diff] [blame] | 2365 | ret = iterate_inodes_from_logical(old->bytenr + |
| 2366 | old->extent_offset, fs_info, |
Liu Bo | 38c227d | 2013-01-29 03:18:40 +0000 | [diff] [blame] | 2367 | path, record_one_backref, |
| 2368 | old); |
Josef Bacik | 4724b10 | 2013-11-05 11:11:40 -0500 | [diff] [blame] | 2369 | if (ret < 0 && ret != -ENOENT) |
| 2370 | return false; |
Liu Bo | 38c227d | 2013-01-29 03:18:40 +0000 | [diff] [blame] | 2371 | |
| 2372 | /* no backref to be processed for this extent */ |
| 2373 | if (!old->count) { |
| 2374 | list_del(&old->list); |
| 2375 | kfree(old); |
| 2376 | } |
| 2377 | } |
| 2378 | |
| 2379 | if (list_empty(&new->head)) |
| 2380 | return false; |
| 2381 | |
| 2382 | return true; |
| 2383 | } |
| 2384 | |
| 2385 | static int relink_is_mergable(struct extent_buffer *leaf, |
| 2386 | struct btrfs_file_extent_item *fi, |
Liu Bo | 116e002 | 2013-08-02 16:30:40 +0800 | [diff] [blame] | 2387 | struct new_sa_defrag_extent *new) |
Liu Bo | 38c227d | 2013-01-29 03:18:40 +0000 | [diff] [blame] | 2388 | { |
Liu Bo | 116e002 | 2013-08-02 16:30:40 +0800 | [diff] [blame] | 2389 | if (btrfs_file_extent_disk_bytenr(leaf, fi) != new->bytenr) |
Liu Bo | 38c227d | 2013-01-29 03:18:40 +0000 | [diff] [blame] | 2390 | return 0; |
| 2391 | |
| 2392 | if (btrfs_file_extent_type(leaf, fi) != BTRFS_FILE_EXTENT_REG) |
| 2393 | return 0; |
| 2394 | |
Liu Bo | 116e002 | 2013-08-02 16:30:40 +0800 | [diff] [blame] | 2395 | if (btrfs_file_extent_compression(leaf, fi) != new->compress_type) |
| 2396 | return 0; |
| 2397 | |
| 2398 | if (btrfs_file_extent_encryption(leaf, fi) || |
Liu Bo | 38c227d | 2013-01-29 03:18:40 +0000 | [diff] [blame] | 2399 | btrfs_file_extent_other_encoding(leaf, fi)) |
| 2400 | return 0; |
| 2401 | |
| 2402 | return 1; |
| 2403 | } |
| 2404 | |
| 2405 | /* |
| 2406 | * Note the backref might has changed, and in this case we just return 0. |
| 2407 | */ |
| 2408 | static noinline int relink_extent_backref(struct btrfs_path *path, |
| 2409 | struct sa_defrag_extent_backref *prev, |
| 2410 | struct sa_defrag_extent_backref *backref) |
| 2411 | { |
| 2412 | struct btrfs_file_extent_item *extent; |
| 2413 | struct btrfs_file_extent_item *item; |
| 2414 | struct btrfs_ordered_extent *ordered; |
| 2415 | struct btrfs_trans_handle *trans; |
| 2416 | struct btrfs_fs_info *fs_info; |
| 2417 | struct btrfs_root *root; |
| 2418 | struct btrfs_key key; |
| 2419 | struct extent_buffer *leaf; |
| 2420 | struct old_sa_defrag_extent *old = backref->old; |
| 2421 | struct new_sa_defrag_extent *new = old->new; |
| 2422 | struct inode *src_inode = new->inode; |
| 2423 | struct inode *inode; |
| 2424 | struct extent_state *cached = NULL; |
| 2425 | int ret = 0; |
| 2426 | u64 start; |
| 2427 | u64 len; |
| 2428 | u64 lock_start; |
| 2429 | u64 lock_end; |
| 2430 | bool merge = false; |
| 2431 | int index; |
| 2432 | |
| 2433 | if (prev && prev->root_id == backref->root_id && |
| 2434 | prev->inum == backref->inum && |
| 2435 | prev->file_pos + prev->num_bytes == backref->file_pos) |
| 2436 | merge = true; |
| 2437 | |
| 2438 | /* step 1: get root */ |
| 2439 | key.objectid = backref->root_id; |
| 2440 | key.type = BTRFS_ROOT_ITEM_KEY; |
| 2441 | key.offset = (u64)-1; |
| 2442 | |
| 2443 | fs_info = BTRFS_I(src_inode)->root->fs_info; |
| 2444 | index = srcu_read_lock(&fs_info->subvol_srcu); |
| 2445 | |
| 2446 | root = btrfs_read_fs_root_no_name(fs_info, &key); |
| 2447 | if (IS_ERR(root)) { |
| 2448 | srcu_read_unlock(&fs_info->subvol_srcu, index); |
| 2449 | if (PTR_ERR(root) == -ENOENT) |
| 2450 | return 0; |
| 2451 | return PTR_ERR(root); |
| 2452 | } |
Liu Bo | 38c227d | 2013-01-29 03:18:40 +0000 | [diff] [blame] | 2453 | |
Wang Shilong | bcbba5e | 2014-02-08 23:46:35 +0800 | [diff] [blame] | 2454 | if (btrfs_root_readonly(root)) { |
| 2455 | srcu_read_unlock(&fs_info->subvol_srcu, index); |
| 2456 | return 0; |
| 2457 | } |
| 2458 | |
Liu Bo | 38c227d | 2013-01-29 03:18:40 +0000 | [diff] [blame] | 2459 | /* step 2: get inode */ |
| 2460 | key.objectid = backref->inum; |
| 2461 | key.type = BTRFS_INODE_ITEM_KEY; |
| 2462 | key.offset = 0; |
| 2463 | |
| 2464 | inode = btrfs_iget(fs_info->sb, &key, root, NULL); |
| 2465 | if (IS_ERR(inode)) { |
| 2466 | srcu_read_unlock(&fs_info->subvol_srcu, index); |
| 2467 | return 0; |
| 2468 | } |
| 2469 | |
| 2470 | srcu_read_unlock(&fs_info->subvol_srcu, index); |
| 2471 | |
| 2472 | /* step 3: relink backref */ |
| 2473 | lock_start = backref->file_pos; |
| 2474 | lock_end = backref->file_pos + backref->num_bytes - 1; |
| 2475 | lock_extent_bits(&BTRFS_I(inode)->io_tree, lock_start, lock_end, |
| 2476 | 0, &cached); |
| 2477 | |
| 2478 | ordered = btrfs_lookup_first_ordered_extent(inode, lock_end); |
| 2479 | if (ordered) { |
| 2480 | btrfs_put_ordered_extent(ordered); |
| 2481 | goto out_unlock; |
| 2482 | } |
| 2483 | |
| 2484 | trans = btrfs_join_transaction(root); |
| 2485 | if (IS_ERR(trans)) { |
| 2486 | ret = PTR_ERR(trans); |
| 2487 | goto out_unlock; |
| 2488 | } |
| 2489 | |
| 2490 | key.objectid = backref->inum; |
| 2491 | key.type = BTRFS_EXTENT_DATA_KEY; |
| 2492 | key.offset = backref->file_pos; |
| 2493 | |
| 2494 | ret = btrfs_search_slot(NULL, root, &key, path, 0, 0); |
| 2495 | if (ret < 0) { |
| 2496 | goto out_free_path; |
| 2497 | } else if (ret > 0) { |
| 2498 | ret = 0; |
| 2499 | goto out_free_path; |
| 2500 | } |
| 2501 | |
| 2502 | extent = btrfs_item_ptr(path->nodes[0], path->slots[0], |
| 2503 | struct btrfs_file_extent_item); |
| 2504 | |
| 2505 | if (btrfs_file_extent_generation(path->nodes[0], extent) != |
| 2506 | backref->generation) |
| 2507 | goto out_free_path; |
| 2508 | |
| 2509 | btrfs_release_path(path); |
| 2510 | |
| 2511 | start = backref->file_pos; |
| 2512 | if (backref->extent_offset < old->extent_offset + old->offset) |
| 2513 | start += old->extent_offset + old->offset - |
| 2514 | backref->extent_offset; |
| 2515 | |
| 2516 | len = min(backref->extent_offset + backref->num_bytes, |
| 2517 | old->extent_offset + old->offset + old->len); |
| 2518 | len -= max(backref->extent_offset, old->extent_offset + old->offset); |
| 2519 | |
| 2520 | ret = btrfs_drop_extents(trans, root, inode, start, |
| 2521 | start + len, 1); |
| 2522 | if (ret) |
| 2523 | goto out_free_path; |
| 2524 | again: |
| 2525 | key.objectid = btrfs_ino(inode); |
| 2526 | key.type = BTRFS_EXTENT_DATA_KEY; |
| 2527 | key.offset = start; |
| 2528 | |
Liu Bo | a09a0a7 | 2013-03-11 09:20:58 +0000 | [diff] [blame] | 2529 | path->leave_spinning = 1; |
Liu Bo | 38c227d | 2013-01-29 03:18:40 +0000 | [diff] [blame] | 2530 | if (merge) { |
| 2531 | struct btrfs_file_extent_item *fi; |
| 2532 | u64 extent_len; |
| 2533 | struct btrfs_key found_key; |
| 2534 | |
Gui Hecheng | 3c9665d | 2014-01-23 13:41:09 +0800 | [diff] [blame] | 2535 | ret = btrfs_search_slot(trans, root, &key, path, 0, 1); |
Liu Bo | 38c227d | 2013-01-29 03:18:40 +0000 | [diff] [blame] | 2536 | if (ret < 0) |
| 2537 | goto out_free_path; |
| 2538 | |
| 2539 | path->slots[0]--; |
| 2540 | leaf = path->nodes[0]; |
| 2541 | btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]); |
| 2542 | |
| 2543 | fi = btrfs_item_ptr(leaf, path->slots[0], |
| 2544 | struct btrfs_file_extent_item); |
| 2545 | extent_len = btrfs_file_extent_num_bytes(leaf, fi); |
| 2546 | |
Liu Bo | 116e002 | 2013-08-02 16:30:40 +0800 | [diff] [blame] | 2547 | if (extent_len + found_key.offset == start && |
| 2548 | relink_is_mergable(leaf, fi, new)) { |
Liu Bo | 38c227d | 2013-01-29 03:18:40 +0000 | [diff] [blame] | 2549 | btrfs_set_file_extent_num_bytes(leaf, fi, |
| 2550 | extent_len + len); |
| 2551 | btrfs_mark_buffer_dirty(leaf); |
| 2552 | inode_add_bytes(inode, len); |
| 2553 | |
| 2554 | ret = 1; |
| 2555 | goto out_free_path; |
| 2556 | } else { |
| 2557 | merge = false; |
| 2558 | btrfs_release_path(path); |
| 2559 | goto again; |
| 2560 | } |
| 2561 | } |
| 2562 | |
| 2563 | ret = btrfs_insert_empty_item(trans, root, path, &key, |
| 2564 | sizeof(*extent)); |
| 2565 | if (ret) { |
| 2566 | btrfs_abort_transaction(trans, root, ret); |
| 2567 | goto out_free_path; |
| 2568 | } |
| 2569 | |
| 2570 | leaf = path->nodes[0]; |
| 2571 | item = btrfs_item_ptr(leaf, path->slots[0], |
| 2572 | struct btrfs_file_extent_item); |
| 2573 | btrfs_set_file_extent_disk_bytenr(leaf, item, new->bytenr); |
| 2574 | btrfs_set_file_extent_disk_num_bytes(leaf, item, new->disk_len); |
| 2575 | btrfs_set_file_extent_offset(leaf, item, start - new->file_pos); |
| 2576 | btrfs_set_file_extent_num_bytes(leaf, item, len); |
| 2577 | btrfs_set_file_extent_ram_bytes(leaf, item, new->len); |
| 2578 | btrfs_set_file_extent_generation(leaf, item, trans->transid); |
| 2579 | btrfs_set_file_extent_type(leaf, item, BTRFS_FILE_EXTENT_REG); |
| 2580 | btrfs_set_file_extent_compression(leaf, item, new->compress_type); |
| 2581 | btrfs_set_file_extent_encryption(leaf, item, 0); |
| 2582 | btrfs_set_file_extent_other_encoding(leaf, item, 0); |
| 2583 | |
| 2584 | btrfs_mark_buffer_dirty(leaf); |
| 2585 | inode_add_bytes(inode, len); |
Liu Bo | a09a0a7 | 2013-03-11 09:20:58 +0000 | [diff] [blame] | 2586 | btrfs_release_path(path); |
Liu Bo | 38c227d | 2013-01-29 03:18:40 +0000 | [diff] [blame] | 2587 | |
| 2588 | ret = btrfs_inc_extent_ref(trans, root, new->bytenr, |
| 2589 | new->disk_len, 0, |
| 2590 | backref->root_id, backref->inum, |
| 2591 | new->file_pos, 0); /* start - extent_offset */ |
| 2592 | if (ret) { |
| 2593 | btrfs_abort_transaction(trans, root, ret); |
| 2594 | goto out_free_path; |
| 2595 | } |
| 2596 | |
| 2597 | ret = 1; |
| 2598 | out_free_path: |
| 2599 | btrfs_release_path(path); |
Liu Bo | a09a0a7 | 2013-03-11 09:20:58 +0000 | [diff] [blame] | 2600 | path->leave_spinning = 0; |
Liu Bo | 38c227d | 2013-01-29 03:18:40 +0000 | [diff] [blame] | 2601 | btrfs_end_transaction(trans, root); |
| 2602 | out_unlock: |
| 2603 | unlock_extent_cached(&BTRFS_I(inode)->io_tree, lock_start, lock_end, |
| 2604 | &cached, GFP_NOFS); |
| 2605 | iput(inode); |
| 2606 | return ret; |
| 2607 | } |
| 2608 | |
Liu Bo | 6f51956 | 2013-10-29 10:45:05 +0800 | [diff] [blame] | 2609 | static void free_sa_defrag_extent(struct new_sa_defrag_extent *new) |
| 2610 | { |
| 2611 | struct old_sa_defrag_extent *old, *tmp; |
| 2612 | |
| 2613 | if (!new) |
| 2614 | return; |
| 2615 | |
| 2616 | list_for_each_entry_safe(old, tmp, &new->head, list) { |
Liu Bo | 6f51956 | 2013-10-29 10:45:05 +0800 | [diff] [blame] | 2617 | kfree(old); |
| 2618 | } |
| 2619 | kfree(new); |
| 2620 | } |
| 2621 | |
Liu Bo | 38c227d | 2013-01-29 03:18:40 +0000 | [diff] [blame] | 2622 | static void relink_file_extents(struct new_sa_defrag_extent *new) |
| 2623 | { |
| 2624 | struct btrfs_path *path; |
Liu Bo | 38c227d | 2013-01-29 03:18:40 +0000 | [diff] [blame] | 2625 | struct sa_defrag_extent_backref *backref; |
| 2626 | struct sa_defrag_extent_backref *prev = NULL; |
| 2627 | struct inode *inode; |
| 2628 | struct btrfs_root *root; |
| 2629 | struct rb_node *node; |
| 2630 | int ret; |
| 2631 | |
| 2632 | inode = new->inode; |
| 2633 | root = BTRFS_I(inode)->root; |
| 2634 | |
| 2635 | path = btrfs_alloc_path(); |
| 2636 | if (!path) |
| 2637 | return; |
| 2638 | |
| 2639 | if (!record_extent_backrefs(path, new)) { |
| 2640 | btrfs_free_path(path); |
| 2641 | goto out; |
| 2642 | } |
| 2643 | btrfs_release_path(path); |
| 2644 | |
| 2645 | while (1) { |
| 2646 | node = rb_first(&new->root); |
| 2647 | if (!node) |
| 2648 | break; |
| 2649 | rb_erase(node, &new->root); |
| 2650 | |
| 2651 | backref = rb_entry(node, struct sa_defrag_extent_backref, node); |
| 2652 | |
| 2653 | ret = relink_extent_backref(path, prev, backref); |
| 2654 | WARN_ON(ret < 0); |
| 2655 | |
| 2656 | kfree(prev); |
| 2657 | |
| 2658 | if (ret == 1) |
| 2659 | prev = backref; |
| 2660 | else |
| 2661 | prev = NULL; |
| 2662 | cond_resched(); |
| 2663 | } |
| 2664 | kfree(prev); |
| 2665 | |
| 2666 | btrfs_free_path(path); |
Liu Bo | 38c227d | 2013-01-29 03:18:40 +0000 | [diff] [blame] | 2667 | out: |
Liu Bo | 6f51956 | 2013-10-29 10:45:05 +0800 | [diff] [blame] | 2668 | free_sa_defrag_extent(new); |
| 2669 | |
Liu Bo | 38c227d | 2013-01-29 03:18:40 +0000 | [diff] [blame] | 2670 | atomic_dec(&root->fs_info->defrag_running); |
| 2671 | wake_up(&root->fs_info->transaction_wait); |
Liu Bo | 38c227d | 2013-01-29 03:18:40 +0000 | [diff] [blame] | 2672 | } |
| 2673 | |
| 2674 | static struct new_sa_defrag_extent * |
| 2675 | record_old_file_extents(struct inode *inode, |
| 2676 | struct btrfs_ordered_extent *ordered) |
| 2677 | { |
| 2678 | struct btrfs_root *root = BTRFS_I(inode)->root; |
| 2679 | struct btrfs_path *path; |
| 2680 | struct btrfs_key key; |
Liu Bo | 6f51956 | 2013-10-29 10:45:05 +0800 | [diff] [blame] | 2681 | struct old_sa_defrag_extent *old; |
Liu Bo | 38c227d | 2013-01-29 03:18:40 +0000 | [diff] [blame] | 2682 | struct new_sa_defrag_extent *new; |
| 2683 | int ret; |
| 2684 | |
| 2685 | new = kmalloc(sizeof(*new), GFP_NOFS); |
| 2686 | if (!new) |
| 2687 | return NULL; |
| 2688 | |
| 2689 | new->inode = inode; |
| 2690 | new->file_pos = ordered->file_offset; |
| 2691 | new->len = ordered->len; |
| 2692 | new->bytenr = ordered->start; |
| 2693 | new->disk_len = ordered->disk_len; |
| 2694 | new->compress_type = ordered->compress_type; |
| 2695 | new->root = RB_ROOT; |
| 2696 | INIT_LIST_HEAD(&new->head); |
| 2697 | |
| 2698 | path = btrfs_alloc_path(); |
| 2699 | if (!path) |
| 2700 | goto out_kfree; |
| 2701 | |
| 2702 | key.objectid = btrfs_ino(inode); |
| 2703 | key.type = BTRFS_EXTENT_DATA_KEY; |
| 2704 | key.offset = new->file_pos; |
| 2705 | |
| 2706 | ret = btrfs_search_slot(NULL, root, &key, path, 0, 0); |
| 2707 | if (ret < 0) |
| 2708 | goto out_free_path; |
| 2709 | if (ret > 0 && path->slots[0] > 0) |
| 2710 | path->slots[0]--; |
| 2711 | |
| 2712 | /* find out all the old extents for the file range */ |
| 2713 | while (1) { |
| 2714 | struct btrfs_file_extent_item *extent; |
| 2715 | struct extent_buffer *l; |
| 2716 | int slot; |
| 2717 | u64 num_bytes; |
| 2718 | u64 offset; |
| 2719 | u64 end; |
| 2720 | u64 disk_bytenr; |
| 2721 | u64 extent_offset; |
| 2722 | |
| 2723 | l = path->nodes[0]; |
| 2724 | slot = path->slots[0]; |
| 2725 | |
| 2726 | if (slot >= btrfs_header_nritems(l)) { |
| 2727 | ret = btrfs_next_leaf(root, path); |
| 2728 | if (ret < 0) |
Liu Bo | 6f51956 | 2013-10-29 10:45:05 +0800 | [diff] [blame] | 2729 | goto out_free_path; |
Liu Bo | 38c227d | 2013-01-29 03:18:40 +0000 | [diff] [blame] | 2730 | else if (ret > 0) |
| 2731 | break; |
| 2732 | continue; |
| 2733 | } |
| 2734 | |
| 2735 | btrfs_item_key_to_cpu(l, &key, slot); |
| 2736 | |
| 2737 | if (key.objectid != btrfs_ino(inode)) |
| 2738 | break; |
| 2739 | if (key.type != BTRFS_EXTENT_DATA_KEY) |
| 2740 | break; |
| 2741 | if (key.offset >= new->file_pos + new->len) |
| 2742 | break; |
| 2743 | |
| 2744 | extent = btrfs_item_ptr(l, slot, struct btrfs_file_extent_item); |
| 2745 | |
| 2746 | num_bytes = btrfs_file_extent_num_bytes(l, extent); |
| 2747 | if (key.offset + num_bytes < new->file_pos) |
| 2748 | goto next; |
| 2749 | |
| 2750 | disk_bytenr = btrfs_file_extent_disk_bytenr(l, extent); |
| 2751 | if (!disk_bytenr) |
| 2752 | goto next; |
| 2753 | |
| 2754 | extent_offset = btrfs_file_extent_offset(l, extent); |
| 2755 | |
| 2756 | old = kmalloc(sizeof(*old), GFP_NOFS); |
| 2757 | if (!old) |
Liu Bo | 6f51956 | 2013-10-29 10:45:05 +0800 | [diff] [blame] | 2758 | goto out_free_path; |
Liu Bo | 38c227d | 2013-01-29 03:18:40 +0000 | [diff] [blame] | 2759 | |
| 2760 | offset = max(new->file_pos, key.offset); |
| 2761 | end = min(new->file_pos + new->len, key.offset + num_bytes); |
| 2762 | |
| 2763 | old->bytenr = disk_bytenr; |
| 2764 | old->extent_offset = extent_offset; |
| 2765 | old->offset = offset - key.offset; |
| 2766 | old->len = end - offset; |
| 2767 | old->new = new; |
| 2768 | old->count = 0; |
| 2769 | list_add_tail(&old->list, &new->head); |
| 2770 | next: |
| 2771 | path->slots[0]++; |
| 2772 | cond_resched(); |
| 2773 | } |
| 2774 | |
| 2775 | btrfs_free_path(path); |
| 2776 | atomic_inc(&root->fs_info->defrag_running); |
| 2777 | |
| 2778 | return new; |
| 2779 | |
Liu Bo | 38c227d | 2013-01-29 03:18:40 +0000 | [diff] [blame] | 2780 | out_free_path: |
| 2781 | btrfs_free_path(path); |
| 2782 | out_kfree: |
Liu Bo | 6f51956 | 2013-10-29 10:45:05 +0800 | [diff] [blame] | 2783 | free_sa_defrag_extent(new); |
Liu Bo | 38c227d | 2013-01-29 03:18:40 +0000 | [diff] [blame] | 2784 | return NULL; |
| 2785 | } |
| 2786 | |
Miao Xie | e570fd2 | 2014-06-19 10:42:50 +0800 | [diff] [blame] | 2787 | static void btrfs_release_delalloc_bytes(struct btrfs_root *root, |
| 2788 | u64 start, u64 len) |
| 2789 | { |
| 2790 | struct btrfs_block_group_cache *cache; |
| 2791 | |
| 2792 | cache = btrfs_lookup_block_group(root->fs_info, start); |
| 2793 | ASSERT(cache); |
| 2794 | |
| 2795 | spin_lock(&cache->lock); |
| 2796 | cache->delalloc_bytes -= len; |
| 2797 | spin_unlock(&cache->lock); |
| 2798 | |
| 2799 | btrfs_put_block_group(cache); |
| 2800 | } |
| 2801 | |
Chris Mason | d352ac6 | 2008-09-29 15:18:18 -0400 | [diff] [blame] | 2802 | /* as ordered data IO finishes, this gets called so we can finish |
| 2803 | * an ordered extent if the range of bytes in the file it covers are |
| 2804 | * fully written. |
| 2805 | */ |
Josef Bacik | 5fd0204 | 2012-05-02 14:00:54 -0400 | [diff] [blame] | 2806 | static int btrfs_finish_ordered_io(struct btrfs_ordered_extent *ordered_extent) |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 2807 | { |
Josef Bacik | 5fd0204 | 2012-05-02 14:00:54 -0400 | [diff] [blame] | 2808 | struct inode *inode = ordered_extent->inode; |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 2809 | struct btrfs_root *root = BTRFS_I(inode)->root; |
Yan, Zheng | 0ca1f7c | 2010-05-16 10:48:47 -0400 | [diff] [blame] | 2810 | struct btrfs_trans_handle *trans = NULL; |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 2811 | struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree; |
Josef Bacik | 2ac55d4 | 2010-02-03 19:33:23 +0000 | [diff] [blame] | 2812 | struct extent_state *cached_state = NULL; |
Liu Bo | 38c227d | 2013-01-29 03:18:40 +0000 | [diff] [blame] | 2813 | struct new_sa_defrag_extent *new = NULL; |
Li Zefan | 261507a0 | 2010-12-17 14:21:50 +0800 | [diff] [blame] | 2814 | int compress_type = 0; |
Josef Bacik | 77cef2e | 2013-08-29 13:57:21 -0400 | [diff] [blame] | 2815 | int ret = 0; |
| 2816 | u64 logical_len = ordered_extent->len; |
Li Zefan | 82d5902 | 2011-04-20 10:33:24 +0800 | [diff] [blame] | 2817 | bool nolock; |
Josef Bacik | 77cef2e | 2013-08-29 13:57:21 -0400 | [diff] [blame] | 2818 | bool truncated = false; |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 2819 | |
Liu Bo | 83eea1f | 2012-07-10 05:28:39 -0600 | [diff] [blame] | 2820 | nolock = btrfs_is_free_space_inode(inode); |
Josef Bacik | 0cb59c9 | 2010-07-02 12:14:14 -0400 | [diff] [blame] | 2821 | |
Josef Bacik | 5fd0204 | 2012-05-02 14:00:54 -0400 | [diff] [blame] | 2822 | if (test_bit(BTRFS_ORDERED_IOERR, &ordered_extent->flags)) { |
| 2823 | ret = -EIO; |
| 2824 | goto out; |
| 2825 | } |
| 2826 | |
Miao Xie | f612496 | 2014-09-12 18:44:04 +0800 | [diff] [blame] | 2827 | btrfs_free_io_failure_record(inode, ordered_extent->file_offset, |
| 2828 | ordered_extent->file_offset + |
| 2829 | ordered_extent->len - 1); |
| 2830 | |
Josef Bacik | 77cef2e | 2013-08-29 13:57:21 -0400 | [diff] [blame] | 2831 | if (test_bit(BTRFS_ORDERED_TRUNCATED, &ordered_extent->flags)) { |
| 2832 | truncated = true; |
| 2833 | logical_len = ordered_extent->truncated_len; |
| 2834 | /* Truncated the entire extent, don't bother adding */ |
| 2835 | if (!logical_len) |
| 2836 | goto out; |
| 2837 | } |
| 2838 | |
Yan, Zheng | c216775 | 2009-11-12 09:34:21 +0000 | [diff] [blame] | 2839 | if (test_bit(BTRFS_ORDERED_NOCOW, &ordered_extent->flags)) { |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 2840 | BUG_ON(!list_empty(&ordered_extent->list)); /* Logic error */ |
Josef Bacik | 6c760c0 | 2012-11-09 10:53:21 -0500 | [diff] [blame] | 2841 | btrfs_ordered_update_i_size(inode, 0, ordered_extent); |
| 2842 | if (nolock) |
| 2843 | trans = btrfs_join_transaction_nolock(root); |
| 2844 | else |
| 2845 | trans = btrfs_join_transaction(root); |
| 2846 | if (IS_ERR(trans)) { |
| 2847 | ret = PTR_ERR(trans); |
| 2848 | trans = NULL; |
| 2849 | goto out; |
Yan, Zheng | c216775 | 2009-11-12 09:34:21 +0000 | [diff] [blame] | 2850 | } |
Josef Bacik | 6c760c0 | 2012-11-09 10:53:21 -0500 | [diff] [blame] | 2851 | trans->block_rsv = &root->fs_info->delalloc_block_rsv; |
| 2852 | ret = btrfs_update_inode_fallback(trans, root, inode); |
| 2853 | if (ret) /* -ENOMEM or corruption */ |
| 2854 | btrfs_abort_transaction(trans, root, ret); |
Yan, Zheng | c216775 | 2009-11-12 09:34:21 +0000 | [diff] [blame] | 2855 | goto out; |
| 2856 | } |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 2857 | |
Josef Bacik | 2ac55d4 | 2010-02-03 19:33:23 +0000 | [diff] [blame] | 2858 | lock_extent_bits(io_tree, ordered_extent->file_offset, |
| 2859 | ordered_extent->file_offset + ordered_extent->len - 1, |
Jeff Mahoney | d008237 | 2012-03-01 14:57:19 +0100 | [diff] [blame] | 2860 | 0, &cached_state); |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 2861 | |
Liu Bo | 38c227d | 2013-01-29 03:18:40 +0000 | [diff] [blame] | 2862 | ret = test_range_bit(io_tree, ordered_extent->file_offset, |
| 2863 | ordered_extent->file_offset + ordered_extent->len - 1, |
| 2864 | EXTENT_DEFRAG, 1, cached_state); |
| 2865 | if (ret) { |
| 2866 | u64 last_snapshot = btrfs_root_last_snapshot(&root->root_item); |
Josef Bacik | 8101c8d | 2014-01-29 16:05:30 -0500 | [diff] [blame] | 2867 | if (0 && last_snapshot >= BTRFS_I(inode)->generation) |
Liu Bo | 38c227d | 2013-01-29 03:18:40 +0000 | [diff] [blame] | 2868 | /* the inode is shared */ |
| 2869 | new = record_old_file_extents(inode, ordered_extent); |
| 2870 | |
| 2871 | clear_extent_bit(io_tree, ordered_extent->file_offset, |
| 2872 | ordered_extent->file_offset + ordered_extent->len - 1, |
| 2873 | EXTENT_DEFRAG, 0, 0, &cached_state, GFP_NOFS); |
| 2874 | } |
| 2875 | |
Josef Bacik | 0cb59c9 | 2010-07-02 12:14:14 -0400 | [diff] [blame] | 2876 | if (nolock) |
Josef Bacik | 7a7eaa4 | 2011-04-13 12:54:33 -0400 | [diff] [blame] | 2877 | trans = btrfs_join_transaction_nolock(root); |
Josef Bacik | 0cb59c9 | 2010-07-02 12:14:14 -0400 | [diff] [blame] | 2878 | else |
Josef Bacik | 7a7eaa4 | 2011-04-13 12:54:33 -0400 | [diff] [blame] | 2879 | trans = btrfs_join_transaction(root); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 2880 | if (IS_ERR(trans)) { |
| 2881 | ret = PTR_ERR(trans); |
| 2882 | trans = NULL; |
| 2883 | goto out_unlock; |
| 2884 | } |
Chris Mason | a79b7d4 | 2014-05-22 16:18:52 -0700 | [diff] [blame] | 2885 | |
Yan, Zheng | 0ca1f7c | 2010-05-16 10:48:47 -0400 | [diff] [blame] | 2886 | trans->block_rsv = &root->fs_info->delalloc_block_rsv; |
Yan, Zheng | c216775 | 2009-11-12 09:34:21 +0000 | [diff] [blame] | 2887 | |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 2888 | if (test_bit(BTRFS_ORDERED_COMPRESSED, &ordered_extent->flags)) |
Li Zefan | 261507a0 | 2010-12-17 14:21:50 +0800 | [diff] [blame] | 2889 | compress_type = ordered_extent->compress_type; |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 2890 | if (test_bit(BTRFS_ORDERED_PREALLOC, &ordered_extent->flags)) { |
Li Zefan | 261507a0 | 2010-12-17 14:21:50 +0800 | [diff] [blame] | 2891 | BUG_ON(compress_type); |
Yan, Zheng | 920bbbf | 2009-11-12 09:34:08 +0000 | [diff] [blame] | 2892 | ret = btrfs_mark_extent_written(trans, inode, |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 2893 | ordered_extent->file_offset, |
| 2894 | ordered_extent->file_offset + |
Josef Bacik | 77cef2e | 2013-08-29 13:57:21 -0400 | [diff] [blame] | 2895 | logical_len); |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 2896 | } else { |
Josef Bacik | 0af3d00 | 2010-06-21 14:48:16 -0400 | [diff] [blame] | 2897 | BUG_ON(root == root->fs_info->tree_root); |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 2898 | ret = insert_reserved_file_extent(trans, inode, |
| 2899 | ordered_extent->file_offset, |
| 2900 | ordered_extent->start, |
| 2901 | ordered_extent->disk_len, |
Josef Bacik | 77cef2e | 2013-08-29 13:57:21 -0400 | [diff] [blame] | 2902 | logical_len, logical_len, |
Li Zefan | 261507a0 | 2010-12-17 14:21:50 +0800 | [diff] [blame] | 2903 | compress_type, 0, 0, |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 2904 | BTRFS_FILE_EXTENT_REG); |
Miao Xie | e570fd2 | 2014-06-19 10:42:50 +0800 | [diff] [blame] | 2905 | if (!ret) |
| 2906 | btrfs_release_delalloc_bytes(root, |
| 2907 | ordered_extent->start, |
| 2908 | ordered_extent->disk_len); |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 2909 | } |
Josef Bacik | 5dc562c | 2012-08-17 13:14:17 -0400 | [diff] [blame] | 2910 | unpin_extent_cache(&BTRFS_I(inode)->extent_tree, |
| 2911 | ordered_extent->file_offset, ordered_extent->len, |
| 2912 | trans->transid); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 2913 | if (ret < 0) { |
| 2914 | btrfs_abort_transaction(trans, root, ret); |
Josef Bacik | 5fd0204 | 2012-05-02 14:00:54 -0400 | [diff] [blame] | 2915 | goto out_unlock; |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 2916 | } |
Josef Bacik | 2ac55d4 | 2010-02-03 19:33:23 +0000 | [diff] [blame] | 2917 | |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 2918 | add_pending_csums(trans, inode, ordered_extent->file_offset, |
| 2919 | &ordered_extent->list); |
| 2920 | |
Josef Bacik | 6c760c0 | 2012-11-09 10:53:21 -0500 | [diff] [blame] | 2921 | btrfs_ordered_update_i_size(inode, 0, ordered_extent); |
| 2922 | ret = btrfs_update_inode_fallback(trans, root, inode); |
| 2923 | if (ret) { /* -ENOMEM or corruption */ |
| 2924 | btrfs_abort_transaction(trans, root, ret); |
| 2925 | goto out_unlock; |
Josef Bacik | 1ef30be | 2011-04-05 19:25:36 -0400 | [diff] [blame] | 2926 | } |
| 2927 | ret = 0; |
Josef Bacik | 5fd0204 | 2012-05-02 14:00:54 -0400 | [diff] [blame] | 2928 | out_unlock: |
| 2929 | unlock_extent_cached(io_tree, ordered_extent->file_offset, |
| 2930 | ordered_extent->file_offset + |
| 2931 | ordered_extent->len - 1, &cached_state, GFP_NOFS); |
Yan, Zheng | c216775 | 2009-11-12 09:34:21 +0000 | [diff] [blame] | 2932 | out: |
Josef Bacik | 5b0e95b | 2011-10-06 08:58:24 -0400 | [diff] [blame] | 2933 | if (root != root->fs_info->tree_root) |
Josef Bacik | 0cb59c9 | 2010-07-02 12:14:14 -0400 | [diff] [blame] | 2934 | btrfs_delalloc_release_metadata(inode, ordered_extent->len); |
Miao Xie | a698d075 | 2012-09-20 01:51:59 -0600 | [diff] [blame] | 2935 | if (trans) |
| 2936 | btrfs_end_transaction(trans, root); |
Josef Bacik | 0cb59c9 | 2010-07-02 12:14:14 -0400 | [diff] [blame] | 2937 | |
Josef Bacik | 77cef2e | 2013-08-29 13:57:21 -0400 | [diff] [blame] | 2938 | if (ret || truncated) { |
| 2939 | u64 start, end; |
| 2940 | |
| 2941 | if (truncated) |
| 2942 | start = ordered_extent->file_offset + logical_len; |
| 2943 | else |
| 2944 | start = ordered_extent->file_offset; |
| 2945 | end = ordered_extent->file_offset + ordered_extent->len - 1; |
| 2946 | clear_extent_uptodate(io_tree, start, end, NULL, GFP_NOFS); |
| 2947 | |
| 2948 | /* Drop the cache for the part of the extent we didn't write. */ |
| 2949 | btrfs_drop_extent_cache(inode, start, end, 0); |
Josef Bacik | 5fd0204 | 2012-05-02 14:00:54 -0400 | [diff] [blame] | 2950 | |
Josef Bacik | 0bec9ef | 2013-01-31 14:58:00 -0500 | [diff] [blame] | 2951 | /* |
| 2952 | * If the ordered extent had an IOERR or something else went |
| 2953 | * wrong we need to return the space for this ordered extent |
Josef Bacik | 77cef2e | 2013-08-29 13:57:21 -0400 | [diff] [blame] | 2954 | * back to the allocator. We only free the extent in the |
| 2955 | * truncated case if we didn't write out the extent at all. |
Josef Bacik | 0bec9ef | 2013-01-31 14:58:00 -0500 | [diff] [blame] | 2956 | */ |
Josef Bacik | 77cef2e | 2013-08-29 13:57:21 -0400 | [diff] [blame] | 2957 | if ((ret || !logical_len) && |
| 2958 | !test_bit(BTRFS_ORDERED_NOCOW, &ordered_extent->flags) && |
Josef Bacik | 0bec9ef | 2013-01-31 14:58:00 -0500 | [diff] [blame] | 2959 | !test_bit(BTRFS_ORDERED_PREALLOC, &ordered_extent->flags)) |
| 2960 | btrfs_free_reserved_extent(root, ordered_extent->start, |
Miao Xie | e570fd2 | 2014-06-19 10:42:50 +0800 | [diff] [blame] | 2961 | ordered_extent->disk_len, 1); |
Josef Bacik | 0bec9ef | 2013-01-31 14:58:00 -0500 | [diff] [blame] | 2962 | } |
| 2963 | |
| 2964 | |
Josef Bacik | 5fd0204 | 2012-05-02 14:00:54 -0400 | [diff] [blame] | 2965 | /* |
Liu Bo | 8bad3c0 | 2012-06-18 12:14:23 +0800 | [diff] [blame] | 2966 | * This needs to be done to make sure anybody waiting knows we are done |
| 2967 | * updating everything for this ordered extent. |
Josef Bacik | 5fd0204 | 2012-05-02 14:00:54 -0400 | [diff] [blame] | 2968 | */ |
| 2969 | btrfs_remove_ordered_extent(inode, ordered_extent); |
| 2970 | |
Liu Bo | 38c227d | 2013-01-29 03:18:40 +0000 | [diff] [blame] | 2971 | /* for snapshot-aware defrag */ |
Liu Bo | 6f51956 | 2013-10-29 10:45:05 +0800 | [diff] [blame] | 2972 | if (new) { |
| 2973 | if (ret) { |
| 2974 | free_sa_defrag_extent(new); |
| 2975 | atomic_dec(&root->fs_info->defrag_running); |
| 2976 | } else { |
| 2977 | relink_file_extents(new); |
| 2978 | } |
| 2979 | } |
Liu Bo | 38c227d | 2013-01-29 03:18:40 +0000 | [diff] [blame] | 2980 | |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 2981 | /* once for us */ |
| 2982 | btrfs_put_ordered_extent(ordered_extent); |
| 2983 | /* once for the tree */ |
| 2984 | btrfs_put_ordered_extent(ordered_extent); |
| 2985 | |
Josef Bacik | 5fd0204 | 2012-05-02 14:00:54 -0400 | [diff] [blame] | 2986 | return ret; |
| 2987 | } |
| 2988 | |
| 2989 | static void finish_ordered_fn(struct btrfs_work *work) |
| 2990 | { |
| 2991 | struct btrfs_ordered_extent *ordered_extent; |
| 2992 | ordered_extent = container_of(work, struct btrfs_ordered_extent, work); |
| 2993 | btrfs_finish_ordered_io(ordered_extent); |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 2994 | } |
| 2995 | |
Christoph Hellwig | b295086 | 2008-12-02 09:54:17 -0500 | [diff] [blame] | 2996 | 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] | 2997 | struct extent_state *state, int uptodate) |
| 2998 | { |
Josef Bacik | 5fd0204 | 2012-05-02 14:00:54 -0400 | [diff] [blame] | 2999 | struct inode *inode = page->mapping->host; |
| 3000 | struct btrfs_root *root = BTRFS_I(inode)->root; |
| 3001 | struct btrfs_ordered_extent *ordered_extent = NULL; |
Liu Bo | 9e0af23 | 2014-08-15 23:36:53 +0800 | [diff] [blame] | 3002 | struct btrfs_workqueue *wq; |
| 3003 | btrfs_work_func_t func; |
Josef Bacik | 5fd0204 | 2012-05-02 14:00:54 -0400 | [diff] [blame] | 3004 | |
liubo | 1abe9b8 | 2011-03-24 11:18:59 +0000 | [diff] [blame] | 3005 | trace_btrfs_writepage_end_io_hook(page, start, end, uptodate); |
| 3006 | |
Chris Mason | 8b62b72 | 2009-09-02 16:53:46 -0400 | [diff] [blame] | 3007 | ClearPagePrivate2(page); |
Josef Bacik | 5fd0204 | 2012-05-02 14:00:54 -0400 | [diff] [blame] | 3008 | if (!btrfs_dec_test_ordered_pending(inode, &ordered_extent, start, |
| 3009 | end - start + 1, uptodate)) |
| 3010 | return 0; |
| 3011 | |
Liu Bo | 9e0af23 | 2014-08-15 23:36:53 +0800 | [diff] [blame] | 3012 | if (btrfs_is_free_space_inode(inode)) { |
| 3013 | wq = root->fs_info->endio_freespace_worker; |
| 3014 | func = btrfs_freespace_write_helper; |
| 3015 | } else { |
| 3016 | wq = root->fs_info->endio_write_workers; |
| 3017 | func = btrfs_endio_write_helper; |
| 3018 | } |
Josef Bacik | 5fd0204 | 2012-05-02 14:00:54 -0400 | [diff] [blame] | 3019 | |
Liu Bo | 9e0af23 | 2014-08-15 23:36:53 +0800 | [diff] [blame] | 3020 | btrfs_init_work(&ordered_extent->work, func, finish_ordered_fn, NULL, |
| 3021 | NULL); |
| 3022 | btrfs_queue_work(wq, &ordered_extent->work); |
Josef Bacik | 5fd0204 | 2012-05-02 14:00:54 -0400 | [diff] [blame] | 3023 | |
| 3024 | return 0; |
Chris Mason | 211f90e | 2008-07-18 11:56:15 -0400 | [diff] [blame] | 3025 | } |
| 3026 | |
Miao Xie | dc380ae | 2014-09-12 18:43:55 +0800 | [diff] [blame] | 3027 | static int __readpage_endio_check(struct inode *inode, |
| 3028 | struct btrfs_io_bio *io_bio, |
| 3029 | int icsum, struct page *page, |
| 3030 | int pgoff, u64 start, size_t len) |
| 3031 | { |
| 3032 | char *kaddr; |
| 3033 | u32 csum_expected; |
| 3034 | u32 csum = ~(u32)0; |
Miao Xie | dc380ae | 2014-09-12 18:43:55 +0800 | [diff] [blame] | 3035 | |
| 3036 | csum_expected = *(((u32 *)io_bio->csum) + icsum); |
| 3037 | |
| 3038 | kaddr = kmap_atomic(page); |
| 3039 | csum = btrfs_csum_data(kaddr + pgoff, csum, len); |
| 3040 | btrfs_csum_final(csum, (char *)&csum); |
| 3041 | if (csum != csum_expected) |
| 3042 | goto zeroit; |
| 3043 | |
| 3044 | kunmap_atomic(kaddr); |
| 3045 | return 0; |
| 3046 | zeroit: |
David Sterba | 9464732 | 2015-10-08 11:01:36 +0200 | [diff] [blame] | 3047 | btrfs_warn_rl(BTRFS_I(inode)->root->fs_info, |
| 3048 | "csum failed ino %llu off %llu csum %u expected csum %u", |
Miao Xie | dc380ae | 2014-09-12 18:43:55 +0800 | [diff] [blame] | 3049 | btrfs_ino(inode), start, csum, csum_expected); |
| 3050 | memset(kaddr + pgoff, 1, len); |
| 3051 | flush_dcache_page(page); |
| 3052 | kunmap_atomic(kaddr); |
| 3053 | if (csum_expected == 0) |
| 3054 | return 0; |
| 3055 | return -EIO; |
| 3056 | } |
| 3057 | |
Chris Mason | d352ac6 | 2008-09-29 15:18:18 -0400 | [diff] [blame] | 3058 | /* |
Chris Mason | d352ac6 | 2008-09-29 15:18:18 -0400 | [diff] [blame] | 3059 | * 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] | 3060 | * if there's a match, we allow the bio to finish. If not, the code in |
| 3061 | * extent_io.c will try to find good copies for us. |
Chris Mason | d352ac6 | 2008-09-29 15:18:18 -0400 | [diff] [blame] | 3062 | */ |
Miao Xie | facc8a22 | 2013-07-25 19:22:34 +0800 | [diff] [blame] | 3063 | static int btrfs_readpage_end_io_hook(struct btrfs_io_bio *io_bio, |
| 3064 | u64 phy_offset, struct page *page, |
| 3065 | u64 start, u64 end, int mirror) |
Chris Mason | 07157aa | 2007-08-30 08:50:51 -0400 | [diff] [blame] | 3066 | { |
Miao Xie | 4eee4fa | 2012-12-21 09:17:45 +0000 | [diff] [blame] | 3067 | size_t offset = start - page_offset(page); |
Chris Mason | 07157aa | 2007-08-30 08:50:51 -0400 | [diff] [blame] | 3068 | struct inode *inode = page->mapping->host; |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 3069 | struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree; |
Chris Mason | ff79f81 | 2007-10-15 16:22:25 -0400 | [diff] [blame] | 3070 | struct btrfs_root *root = BTRFS_I(inode)->root; |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 3071 | |
Chris Mason | d20f704 | 2008-12-08 16:58:54 -0500 | [diff] [blame] | 3072 | if (PageChecked(page)) { |
| 3073 | ClearPageChecked(page); |
Miao Xie | dc380ae | 2014-09-12 18:43:55 +0800 | [diff] [blame] | 3074 | return 0; |
Chris Mason | d20f704 | 2008-12-08 16:58:54 -0500 | [diff] [blame] | 3075 | } |
Christoph Hellwig | 6cbff00 | 2009-04-17 10:37:41 +0200 | [diff] [blame] | 3076 | |
| 3077 | if (BTRFS_I(inode)->flags & BTRFS_INODE_NODATASUM) |
Miao Xie | dc380ae | 2014-09-12 18:43:55 +0800 | [diff] [blame] | 3078 | return 0; |
Chris Mason | d20f704 | 2008-12-08 16:58:54 -0500 | [diff] [blame] | 3079 | |
Yan Zheng | 17d217f | 2008-12-12 10:03:38 -0500 | [diff] [blame] | 3080 | if (root->root_key.objectid == BTRFS_DATA_RELOC_TREE_OBJECTID && |
Chris Mason | 9655d29 | 2009-09-02 15:22:30 -0400 | [diff] [blame] | 3081 | test_range_bit(io_tree, start, end, EXTENT_NODATASUM, 1, NULL)) { |
Yan Zheng | 17d217f | 2008-12-12 10:03:38 -0500 | [diff] [blame] | 3082 | clear_extent_bits(io_tree, start, end, EXTENT_NODATASUM, |
| 3083 | GFP_NOFS); |
| 3084 | return 0; |
| 3085 | } |
| 3086 | |
Miao Xie | facc8a22 | 2013-07-25 19:22:34 +0800 | [diff] [blame] | 3087 | phy_offset >>= inode->i_sb->s_blocksize_bits; |
Miao Xie | dc380ae | 2014-09-12 18:43:55 +0800 | [diff] [blame] | 3088 | return __readpage_endio_check(inode, io_bio, phy_offset, page, offset, |
| 3089 | start, (size_t)(end - start + 1)); |
Chris Mason | 07157aa | 2007-08-30 08:50:51 -0400 | [diff] [blame] | 3090 | } |
Chris Mason | b888db2 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 3091 | |
Yan, Zheng | 24bbcf0 | 2009-11-12 09:36:34 +0000 | [diff] [blame] | 3092 | struct delayed_iput { |
| 3093 | struct list_head list; |
| 3094 | struct inode *inode; |
| 3095 | }; |
| 3096 | |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 3097 | /* JDM: If this is fs-wide, why can't we add a pointer to |
| 3098 | * btrfs_inode instead and avoid the allocation? */ |
Yan, Zheng | 24bbcf0 | 2009-11-12 09:36:34 +0000 | [diff] [blame] | 3099 | void btrfs_add_delayed_iput(struct inode *inode) |
| 3100 | { |
| 3101 | struct btrfs_fs_info *fs_info = BTRFS_I(inode)->root->fs_info; |
| 3102 | struct delayed_iput *delayed; |
| 3103 | |
| 3104 | if (atomic_add_unless(&inode->i_count, -1, 1)) |
| 3105 | return; |
| 3106 | |
| 3107 | delayed = kmalloc(sizeof(*delayed), GFP_NOFS | __GFP_NOFAIL); |
| 3108 | delayed->inode = inode; |
| 3109 | |
| 3110 | spin_lock(&fs_info->delayed_iput_lock); |
| 3111 | list_add_tail(&delayed->list, &fs_info->delayed_iputs); |
| 3112 | spin_unlock(&fs_info->delayed_iput_lock); |
| 3113 | } |
| 3114 | |
| 3115 | void btrfs_run_delayed_iputs(struct btrfs_root *root) |
| 3116 | { |
| 3117 | LIST_HEAD(list); |
| 3118 | struct btrfs_fs_info *fs_info = root->fs_info; |
| 3119 | struct delayed_iput *delayed; |
| 3120 | int empty; |
| 3121 | |
| 3122 | spin_lock(&fs_info->delayed_iput_lock); |
| 3123 | empty = list_empty(&fs_info->delayed_iputs); |
| 3124 | spin_unlock(&fs_info->delayed_iput_lock); |
| 3125 | if (empty) |
| 3126 | return; |
| 3127 | |
Zhao Lei | d7c1517 | 2015-02-26 10:49:20 +0800 | [diff] [blame] | 3128 | down_read(&fs_info->delayed_iput_sem); |
| 3129 | |
Yan, Zheng | 24bbcf0 | 2009-11-12 09:36:34 +0000 | [diff] [blame] | 3130 | spin_lock(&fs_info->delayed_iput_lock); |
| 3131 | list_splice_init(&fs_info->delayed_iputs, &list); |
| 3132 | spin_unlock(&fs_info->delayed_iput_lock); |
| 3133 | |
| 3134 | while (!list_empty(&list)) { |
| 3135 | delayed = list_entry(list.next, struct delayed_iput, list); |
| 3136 | list_del(&delayed->list); |
| 3137 | iput(delayed->inode); |
| 3138 | kfree(delayed); |
| 3139 | } |
Zhao Lei | d7c1517 | 2015-02-26 10:49:20 +0800 | [diff] [blame] | 3140 | |
| 3141 | up_read(&root->fs_info->delayed_iput_sem); |
Yan, Zheng | 24bbcf0 | 2009-11-12 09:36:34 +0000 | [diff] [blame] | 3142 | } |
| 3143 | |
Yan, Zheng | d68fc57 | 2010-05-16 10:49:58 -0400 | [diff] [blame] | 3144 | /* |
Justin P. Mattock | 42b2aa8 | 2011-11-28 20:31:00 -0800 | [diff] [blame] | 3145 | * This is called in transaction commit time. If there are no orphan |
Yan, Zheng | d68fc57 | 2010-05-16 10:49:58 -0400 | [diff] [blame] | 3146 | * files in the subvolume, it removes orphan item and frees block_rsv |
| 3147 | * structure. |
| 3148 | */ |
| 3149 | void btrfs_orphan_commit_root(struct btrfs_trans_handle *trans, |
| 3150 | struct btrfs_root *root) |
| 3151 | { |
Josef Bacik | 90290e1 | 2011-12-02 15:44:12 -0500 | [diff] [blame] | 3152 | struct btrfs_block_rsv *block_rsv; |
Yan, Zheng | d68fc57 | 2010-05-16 10:49:58 -0400 | [diff] [blame] | 3153 | int ret; |
| 3154 | |
Josef Bacik | 8a35d95 | 2012-05-23 14:26:42 -0400 | [diff] [blame] | 3155 | if (atomic_read(&root->orphan_inodes) || |
Yan, Zheng | d68fc57 | 2010-05-16 10:49:58 -0400 | [diff] [blame] | 3156 | root->orphan_cleanup_state != ORPHAN_CLEANUP_DONE) |
| 3157 | return; |
| 3158 | |
Josef Bacik | 90290e1 | 2011-12-02 15:44:12 -0500 | [diff] [blame] | 3159 | spin_lock(&root->orphan_lock); |
Josef Bacik | 8a35d95 | 2012-05-23 14:26:42 -0400 | [diff] [blame] | 3160 | if (atomic_read(&root->orphan_inodes)) { |
Josef Bacik | 90290e1 | 2011-12-02 15:44:12 -0500 | [diff] [blame] | 3161 | spin_unlock(&root->orphan_lock); |
| 3162 | return; |
| 3163 | } |
| 3164 | |
| 3165 | if (root->orphan_cleanup_state != ORPHAN_CLEANUP_DONE) { |
| 3166 | spin_unlock(&root->orphan_lock); |
| 3167 | return; |
| 3168 | } |
| 3169 | |
| 3170 | block_rsv = root->orphan_block_rsv; |
| 3171 | root->orphan_block_rsv = NULL; |
| 3172 | spin_unlock(&root->orphan_lock); |
| 3173 | |
Miao Xie | 27cdeb7 | 2014-04-02 19:51:05 +0800 | [diff] [blame] | 3174 | if (test_bit(BTRFS_ROOT_ORPHAN_ITEM_INSERTED, &root->state) && |
Yan, Zheng | d68fc57 | 2010-05-16 10:49:58 -0400 | [diff] [blame] | 3175 | btrfs_root_refs(&root->root_item) > 0) { |
| 3176 | ret = btrfs_del_orphan_item(trans, root->fs_info->tree_root, |
| 3177 | root->root_key.objectid); |
Josef Bacik | 4ef31a4 | 2013-08-13 14:10:08 -0400 | [diff] [blame] | 3178 | if (ret) |
| 3179 | btrfs_abort_transaction(trans, root, ret); |
| 3180 | else |
Miao Xie | 27cdeb7 | 2014-04-02 19:51:05 +0800 | [diff] [blame] | 3181 | clear_bit(BTRFS_ROOT_ORPHAN_ITEM_INSERTED, |
| 3182 | &root->state); |
Yan, Zheng | d68fc57 | 2010-05-16 10:49:58 -0400 | [diff] [blame] | 3183 | } |
| 3184 | |
Josef Bacik | 90290e1 | 2011-12-02 15:44:12 -0500 | [diff] [blame] | 3185 | if (block_rsv) { |
| 3186 | WARN_ON(block_rsv->size > 0); |
| 3187 | btrfs_free_block_rsv(root, block_rsv); |
Yan, Zheng | d68fc57 | 2010-05-16 10:49:58 -0400 | [diff] [blame] | 3188 | } |
| 3189 | } |
| 3190 | |
| 3191 | /* |
Josef Bacik | 7b12876 | 2008-07-24 12:17:14 -0400 | [diff] [blame] | 3192 | * This creates an orphan entry for the given inode in case something goes |
| 3193 | * wrong in the middle of an unlink/truncate. |
Yan, Zheng | d68fc57 | 2010-05-16 10:49:58 -0400 | [diff] [blame] | 3194 | * |
| 3195 | * NOTE: caller of this function should reserve 5 units of metadata for |
| 3196 | * this function. |
Josef Bacik | 7b12876 | 2008-07-24 12:17:14 -0400 | [diff] [blame] | 3197 | */ |
| 3198 | int btrfs_orphan_add(struct btrfs_trans_handle *trans, struct inode *inode) |
| 3199 | { |
| 3200 | struct btrfs_root *root = BTRFS_I(inode)->root; |
Yan, Zheng | d68fc57 | 2010-05-16 10:49:58 -0400 | [diff] [blame] | 3201 | struct btrfs_block_rsv *block_rsv = NULL; |
| 3202 | int reserve = 0; |
| 3203 | int insert = 0; |
| 3204 | int ret; |
Josef Bacik | 7b12876 | 2008-07-24 12:17:14 -0400 | [diff] [blame] | 3205 | |
Yan, Zheng | d68fc57 | 2010-05-16 10:49:58 -0400 | [diff] [blame] | 3206 | if (!root->orphan_block_rsv) { |
Miao Xie | 66d8f3d | 2012-09-06 04:02:28 -0600 | [diff] [blame] | 3207 | block_rsv = btrfs_alloc_block_rsv(root, BTRFS_BLOCK_RSV_TEMP); |
Tsutomu Itoh | b532402 | 2011-07-19 07:27:20 +0000 | [diff] [blame] | 3208 | if (!block_rsv) |
| 3209 | return -ENOMEM; |
Josef Bacik | 7b12876 | 2008-07-24 12:17:14 -0400 | [diff] [blame] | 3210 | } |
| 3211 | |
Yan, Zheng | d68fc57 | 2010-05-16 10:49:58 -0400 | [diff] [blame] | 3212 | spin_lock(&root->orphan_lock); |
| 3213 | if (!root->orphan_block_rsv) { |
| 3214 | root->orphan_block_rsv = block_rsv; |
| 3215 | } else if (block_rsv) { |
| 3216 | btrfs_free_block_rsv(root, block_rsv); |
| 3217 | block_rsv = NULL; |
| 3218 | } |
Josef Bacik | 7b12876 | 2008-07-24 12:17:14 -0400 | [diff] [blame] | 3219 | |
Josef Bacik | 8a35d95 | 2012-05-23 14:26:42 -0400 | [diff] [blame] | 3220 | if (!test_and_set_bit(BTRFS_INODE_HAS_ORPHAN_ITEM, |
| 3221 | &BTRFS_I(inode)->runtime_flags)) { |
Yan, Zheng | d68fc57 | 2010-05-16 10:49:58 -0400 | [diff] [blame] | 3222 | #if 0 |
| 3223 | /* |
| 3224 | * For proper ENOSPC handling, we should do orphan |
| 3225 | * cleanup when mounting. But this introduces backward |
| 3226 | * compatibility issue. |
| 3227 | */ |
| 3228 | if (!xchg(&root->orphan_item_inserted, 1)) |
| 3229 | insert = 2; |
| 3230 | else |
| 3231 | insert = 1; |
| 3232 | #endif |
| 3233 | insert = 1; |
Miao Xie | 321f0e7 | 2012-08-28 22:13:02 -0600 | [diff] [blame] | 3234 | atomic_inc(&root->orphan_inodes); |
Yan, Zheng | d68fc57 | 2010-05-16 10:49:58 -0400 | [diff] [blame] | 3235 | } |
Josef Bacik | 7b12876 | 2008-07-24 12:17:14 -0400 | [diff] [blame] | 3236 | |
Josef Bacik | 72ac3c0 | 2012-05-23 14:13:11 -0400 | [diff] [blame] | 3237 | if (!test_and_set_bit(BTRFS_INODE_ORPHAN_META_RESERVED, |
| 3238 | &BTRFS_I(inode)->runtime_flags)) |
Yan, Zheng | d68fc57 | 2010-05-16 10:49:58 -0400 | [diff] [blame] | 3239 | reserve = 1; |
Yan, Zheng | d68fc57 | 2010-05-16 10:49:58 -0400 | [diff] [blame] | 3240 | spin_unlock(&root->orphan_lock); |
Josef Bacik | 7b12876 | 2008-07-24 12:17:14 -0400 | [diff] [blame] | 3241 | |
Yan, Zheng | d68fc57 | 2010-05-16 10:49:58 -0400 | [diff] [blame] | 3242 | /* grab metadata reservation from transaction handle */ |
| 3243 | if (reserve) { |
| 3244 | ret = btrfs_orphan_reserve_metadata(trans, inode); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 3245 | BUG_ON(ret); /* -ENOSPC in reservation; Logic error? JDM */ |
Yan, Zheng | d68fc57 | 2010-05-16 10:49:58 -0400 | [diff] [blame] | 3246 | } |
| 3247 | |
| 3248 | /* insert an orphan item to track this unlinked/truncated file */ |
| 3249 | if (insert >= 1) { |
Li Zefan | 33345d01 | 2011-04-20 10:31:50 +0800 | [diff] [blame] | 3250 | ret = btrfs_insert_orphan_item(trans, root, btrfs_ino(inode)); |
Josef Bacik | 4ef31a4 | 2013-08-13 14:10:08 -0400 | [diff] [blame] | 3251 | if (ret) { |
Filipe David Borba Manana | 703c88e | 2013-09-22 21:54:55 +0100 | [diff] [blame] | 3252 | atomic_dec(&root->orphan_inodes); |
Josef Bacik | 4ef31a4 | 2013-08-13 14:10:08 -0400 | [diff] [blame] | 3253 | if (reserve) { |
| 3254 | clear_bit(BTRFS_INODE_ORPHAN_META_RESERVED, |
| 3255 | &BTRFS_I(inode)->runtime_flags); |
| 3256 | btrfs_orphan_release_metadata(inode); |
| 3257 | } |
| 3258 | if (ret != -EEXIST) { |
Josef Bacik | e8e7cff | 2013-08-21 15:54:00 -0400 | [diff] [blame] | 3259 | clear_bit(BTRFS_INODE_HAS_ORPHAN_ITEM, |
| 3260 | &BTRFS_I(inode)->runtime_flags); |
Josef Bacik | 4ef31a4 | 2013-08-13 14:10:08 -0400 | [diff] [blame] | 3261 | btrfs_abort_transaction(trans, root, ret); |
| 3262 | return ret; |
| 3263 | } |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 3264 | } |
| 3265 | ret = 0; |
Yan, Zheng | d68fc57 | 2010-05-16 10:49:58 -0400 | [diff] [blame] | 3266 | } |
| 3267 | |
| 3268 | /* insert an orphan item to track subvolume contains orphan files */ |
| 3269 | if (insert >= 2) { |
| 3270 | ret = btrfs_insert_orphan_item(trans, root->fs_info->tree_root, |
| 3271 | root->root_key.objectid); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 3272 | if (ret && ret != -EEXIST) { |
| 3273 | btrfs_abort_transaction(trans, root, ret); |
| 3274 | return ret; |
| 3275 | } |
Yan, Zheng | d68fc57 | 2010-05-16 10:49:58 -0400 | [diff] [blame] | 3276 | } |
| 3277 | return 0; |
Josef Bacik | 7b12876 | 2008-07-24 12:17:14 -0400 | [diff] [blame] | 3278 | } |
| 3279 | |
| 3280 | /* |
| 3281 | * We have done the truncate/delete so we can go ahead and remove the orphan |
| 3282 | * item for this particular inode. |
| 3283 | */ |
Eric Sandeen | 48a3b63 | 2013-04-25 20:41:01 +0000 | [diff] [blame] | 3284 | static int btrfs_orphan_del(struct btrfs_trans_handle *trans, |
| 3285 | struct inode *inode) |
Josef Bacik | 7b12876 | 2008-07-24 12:17:14 -0400 | [diff] [blame] | 3286 | { |
| 3287 | struct btrfs_root *root = BTRFS_I(inode)->root; |
Yan, Zheng | d68fc57 | 2010-05-16 10:49:58 -0400 | [diff] [blame] | 3288 | int delete_item = 0; |
| 3289 | int release_rsv = 0; |
Josef Bacik | 7b12876 | 2008-07-24 12:17:14 -0400 | [diff] [blame] | 3290 | int ret = 0; |
| 3291 | |
Yan, Zheng | d68fc57 | 2010-05-16 10:49:58 -0400 | [diff] [blame] | 3292 | spin_lock(&root->orphan_lock); |
Josef Bacik | 8a35d95 | 2012-05-23 14:26:42 -0400 | [diff] [blame] | 3293 | if (test_and_clear_bit(BTRFS_INODE_HAS_ORPHAN_ITEM, |
| 3294 | &BTRFS_I(inode)->runtime_flags)) |
Yan, Zheng | d68fc57 | 2010-05-16 10:49:58 -0400 | [diff] [blame] | 3295 | delete_item = 1; |
Josef Bacik | 7b12876 | 2008-07-24 12:17:14 -0400 | [diff] [blame] | 3296 | |
Josef Bacik | 72ac3c0 | 2012-05-23 14:13:11 -0400 | [diff] [blame] | 3297 | if (test_and_clear_bit(BTRFS_INODE_ORPHAN_META_RESERVED, |
| 3298 | &BTRFS_I(inode)->runtime_flags)) |
Yan, Zheng | d68fc57 | 2010-05-16 10:49:58 -0400 | [diff] [blame] | 3299 | release_rsv = 1; |
Yan, Zheng | d68fc57 | 2010-05-16 10:49:58 -0400 | [diff] [blame] | 3300 | spin_unlock(&root->orphan_lock); |
| 3301 | |
Filipe David Borba Manana | 703c88e | 2013-09-22 21:54:55 +0100 | [diff] [blame] | 3302 | if (delete_item) { |
Josef Bacik | 8a35d95 | 2012-05-23 14:26:42 -0400 | [diff] [blame] | 3303 | atomic_dec(&root->orphan_inodes); |
Filipe David Borba Manana | 703c88e | 2013-09-22 21:54:55 +0100 | [diff] [blame] | 3304 | if (trans) |
| 3305 | ret = btrfs_del_orphan_item(trans, root, |
| 3306 | btrfs_ino(inode)); |
Josef Bacik | 8a35d95 | 2012-05-23 14:26:42 -0400 | [diff] [blame] | 3307 | } |
Josef Bacik | 7b12876 | 2008-07-24 12:17:14 -0400 | [diff] [blame] | 3308 | |
Filipe David Borba Manana | 703c88e | 2013-09-22 21:54:55 +0100 | [diff] [blame] | 3309 | if (release_rsv) |
| 3310 | btrfs_orphan_release_metadata(inode); |
| 3311 | |
Josef Bacik | 4ef31a4 | 2013-08-13 14:10:08 -0400 | [diff] [blame] | 3312 | return ret; |
Josef Bacik | 7b12876 | 2008-07-24 12:17:14 -0400 | [diff] [blame] | 3313 | } |
| 3314 | |
| 3315 | /* |
| 3316 | * this cleans up any orphans that may be left on the list from the last use |
| 3317 | * of this root. |
| 3318 | */ |
Josef Bacik | 66b4ffd | 2011-01-31 16:22:42 -0500 | [diff] [blame] | 3319 | int btrfs_orphan_cleanup(struct btrfs_root *root) |
Josef Bacik | 7b12876 | 2008-07-24 12:17:14 -0400 | [diff] [blame] | 3320 | { |
| 3321 | struct btrfs_path *path; |
| 3322 | struct extent_buffer *leaf; |
Josef Bacik | 7b12876 | 2008-07-24 12:17:14 -0400 | [diff] [blame] | 3323 | struct btrfs_key key, found_key; |
| 3324 | struct btrfs_trans_handle *trans; |
| 3325 | struct inode *inode; |
Josef Bacik | 8f6d7f4 | 2011-09-26 15:55:20 -0400 | [diff] [blame] | 3326 | u64 last_objectid = 0; |
Josef Bacik | 7b12876 | 2008-07-24 12:17:14 -0400 | [diff] [blame] | 3327 | int ret = 0, nr_unlink = 0, nr_truncate = 0; |
| 3328 | |
Yan, Zheng | d68fc57 | 2010-05-16 10:49:58 -0400 | [diff] [blame] | 3329 | if (cmpxchg(&root->orphan_cleanup_state, 0, ORPHAN_CLEANUP_STARTED)) |
Josef Bacik | 66b4ffd | 2011-01-31 16:22:42 -0500 | [diff] [blame] | 3330 | return 0; |
Yan, Zheng | c71bf09 | 2009-11-12 09:34:40 +0000 | [diff] [blame] | 3331 | |
| 3332 | path = btrfs_alloc_path(); |
Josef Bacik | 66b4ffd | 2011-01-31 16:22:42 -0500 | [diff] [blame] | 3333 | if (!path) { |
| 3334 | ret = -ENOMEM; |
| 3335 | goto out; |
| 3336 | } |
Josef Bacik | 7b12876 | 2008-07-24 12:17:14 -0400 | [diff] [blame] | 3337 | path->reada = -1; |
| 3338 | |
| 3339 | key.objectid = BTRFS_ORPHAN_OBJECTID; |
David Sterba | 962a298 | 2014-06-04 18:41:45 +0200 | [diff] [blame] | 3340 | key.type = BTRFS_ORPHAN_ITEM_KEY; |
Josef Bacik | 7b12876 | 2008-07-24 12:17:14 -0400 | [diff] [blame] | 3341 | key.offset = (u64)-1; |
| 3342 | |
Josef Bacik | 7b12876 | 2008-07-24 12:17:14 -0400 | [diff] [blame] | 3343 | while (1) { |
| 3344 | ret = btrfs_search_slot(NULL, root, &key, path, 0, 0); |
Josef Bacik | 66b4ffd | 2011-01-31 16:22:42 -0500 | [diff] [blame] | 3345 | if (ret < 0) |
| 3346 | goto out; |
Josef Bacik | 7b12876 | 2008-07-24 12:17:14 -0400 | [diff] [blame] | 3347 | |
| 3348 | /* |
| 3349 | * 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] | 3350 | * 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] | 3351 | * find the key and see if we have stuff that matches |
| 3352 | */ |
| 3353 | if (ret > 0) { |
Josef Bacik | 66b4ffd | 2011-01-31 16:22:42 -0500 | [diff] [blame] | 3354 | ret = 0; |
Josef Bacik | 7b12876 | 2008-07-24 12:17:14 -0400 | [diff] [blame] | 3355 | if (path->slots[0] == 0) |
| 3356 | break; |
| 3357 | path->slots[0]--; |
| 3358 | } |
| 3359 | |
| 3360 | /* pull out the item */ |
| 3361 | leaf = path->nodes[0]; |
Josef Bacik | 7b12876 | 2008-07-24 12:17:14 -0400 | [diff] [blame] | 3362 | btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]); |
| 3363 | |
| 3364 | /* make sure the item matches what we want */ |
| 3365 | if (found_key.objectid != BTRFS_ORPHAN_OBJECTID) |
| 3366 | break; |
David Sterba | 962a298 | 2014-06-04 18:41:45 +0200 | [diff] [blame] | 3367 | if (found_key.type != BTRFS_ORPHAN_ITEM_KEY) |
Josef Bacik | 7b12876 | 2008-07-24 12:17:14 -0400 | [diff] [blame] | 3368 | break; |
| 3369 | |
| 3370 | /* release the path since we're done with it */ |
David Sterba | b3b4aa7 | 2011-04-21 01:20:15 +0200 | [diff] [blame] | 3371 | btrfs_release_path(path); |
Josef Bacik | 7b12876 | 2008-07-24 12:17:14 -0400 | [diff] [blame] | 3372 | |
| 3373 | /* |
| 3374 | * this is where we are basically btrfs_lookup, without the |
| 3375 | * crossing root thing. we store the inode number in the |
| 3376 | * offset of the orphan item. |
| 3377 | */ |
Josef Bacik | 8f6d7f4 | 2011-09-26 15:55:20 -0400 | [diff] [blame] | 3378 | |
| 3379 | if (found_key.offset == last_objectid) { |
Simon Kirby | c2cf52e | 2013-03-19 22:41:23 +0000 | [diff] [blame] | 3380 | btrfs_err(root->fs_info, |
| 3381 | "Error removing orphan entry, stopping orphan cleanup"); |
Josef Bacik | 8f6d7f4 | 2011-09-26 15:55:20 -0400 | [diff] [blame] | 3382 | ret = -EINVAL; |
| 3383 | goto out; |
| 3384 | } |
| 3385 | |
| 3386 | last_objectid = found_key.offset; |
| 3387 | |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 3388 | found_key.objectid = found_key.offset; |
| 3389 | found_key.type = BTRFS_INODE_ITEM_KEY; |
| 3390 | found_key.offset = 0; |
Josef Bacik | 73f7341 | 2009-12-04 17:38:27 +0000 | [diff] [blame] | 3391 | inode = btrfs_iget(root->fs_info->sb, &found_key, root, NULL); |
Rusty Russell | 8c6ffba | 2013-07-15 11:20:32 +0930 | [diff] [blame] | 3392 | ret = PTR_ERR_OR_ZERO(inode); |
Josef Bacik | a8c9e57 | 2011-09-21 16:55:59 -0400 | [diff] [blame] | 3393 | if (ret && ret != -ESTALE) |
Josef Bacik | 66b4ffd | 2011-01-31 16:22:42 -0500 | [diff] [blame] | 3394 | goto out; |
Josef Bacik | a8c9e57 | 2011-09-21 16:55:59 -0400 | [diff] [blame] | 3395 | |
Arne Jansen | f8e9e0b | 2011-12-14 20:12:02 -0500 | [diff] [blame] | 3396 | if (ret == -ESTALE && root == root->fs_info->tree_root) { |
| 3397 | struct btrfs_root *dead_root; |
| 3398 | struct btrfs_fs_info *fs_info = root->fs_info; |
| 3399 | int is_dead_root = 0; |
| 3400 | |
| 3401 | /* |
| 3402 | * this is an orphan in the tree root. Currently these |
| 3403 | * could come from 2 sources: |
| 3404 | * a) a snapshot deletion in progress |
| 3405 | * b) a free space cache inode |
| 3406 | * We need to distinguish those two, as the snapshot |
| 3407 | * orphan must not get deleted. |
| 3408 | * find_dead_roots already ran before us, so if this |
| 3409 | * is a snapshot deletion, we should find the root |
| 3410 | * in the dead_roots list |
| 3411 | */ |
| 3412 | spin_lock(&fs_info->trans_lock); |
| 3413 | list_for_each_entry(dead_root, &fs_info->dead_roots, |
| 3414 | root_list) { |
| 3415 | if (dead_root->root_key.objectid == |
| 3416 | found_key.objectid) { |
| 3417 | is_dead_root = 1; |
| 3418 | break; |
| 3419 | } |
| 3420 | } |
| 3421 | spin_unlock(&fs_info->trans_lock); |
| 3422 | if (is_dead_root) { |
| 3423 | /* prevent this orphan from being found again */ |
| 3424 | key.offset = found_key.objectid - 1; |
| 3425 | continue; |
| 3426 | } |
| 3427 | } |
Josef Bacik | a8c9e57 | 2011-09-21 16:55:59 -0400 | [diff] [blame] | 3428 | /* |
| 3429 | * Inode is already gone but the orphan item is still there, |
| 3430 | * kill the orphan item. |
| 3431 | */ |
| 3432 | if (ret == -ESTALE) { |
| 3433 | trans = btrfs_start_transaction(root, 1); |
| 3434 | if (IS_ERR(trans)) { |
| 3435 | ret = PTR_ERR(trans); |
| 3436 | goto out; |
| 3437 | } |
Simon Kirby | c2cf52e | 2013-03-19 22:41:23 +0000 | [diff] [blame] | 3438 | btrfs_debug(root->fs_info, "auto deleting %Lu", |
| 3439 | found_key.objectid); |
Josef Bacik | a8c9e57 | 2011-09-21 16:55:59 -0400 | [diff] [blame] | 3440 | ret = btrfs_del_orphan_item(trans, root, |
| 3441 | found_key.objectid); |
Josef Bacik | a8c9e57 | 2011-09-21 16:55:59 -0400 | [diff] [blame] | 3442 | btrfs_end_transaction(trans, root); |
Josef Bacik | 4ef31a4 | 2013-08-13 14:10:08 -0400 | [diff] [blame] | 3443 | if (ret) |
| 3444 | goto out; |
Josef Bacik | a8c9e57 | 2011-09-21 16:55:59 -0400 | [diff] [blame] | 3445 | continue; |
Josef Bacik | 66b4ffd | 2011-01-31 16:22:42 -0500 | [diff] [blame] | 3446 | } |
Josef Bacik | 7b12876 | 2008-07-24 12:17:14 -0400 | [diff] [blame] | 3447 | |
Josef Bacik | 7b12876 | 2008-07-24 12:17:14 -0400 | [diff] [blame] | 3448 | /* |
| 3449 | * add this inode to the orphan list so btrfs_orphan_del does |
| 3450 | * the proper thing when we hit it |
| 3451 | */ |
Josef Bacik | 8a35d95 | 2012-05-23 14:26:42 -0400 | [diff] [blame] | 3452 | set_bit(BTRFS_INODE_HAS_ORPHAN_ITEM, |
| 3453 | &BTRFS_I(inode)->runtime_flags); |
Josef Bacik | 925396e | 2013-02-01 15:57:47 -0500 | [diff] [blame] | 3454 | atomic_inc(&root->orphan_inodes); |
Josef Bacik | 7b12876 | 2008-07-24 12:17:14 -0400 | [diff] [blame] | 3455 | |
Josef Bacik | 7b12876 | 2008-07-24 12:17:14 -0400 | [diff] [blame] | 3456 | /* if we have links, this was a truncate, lets do that */ |
| 3457 | if (inode->i_nlink) { |
Dulshani Gunawardhana | fae7f21 | 2013-10-31 10:30:08 +0530 | [diff] [blame] | 3458 | if (WARN_ON(!S_ISREG(inode->i_mode))) { |
Josef Bacik | a41ad39 | 2011-01-31 15:30:16 -0500 | [diff] [blame] | 3459 | iput(inode); |
| 3460 | continue; |
| 3461 | } |
Josef Bacik | 7b12876 | 2008-07-24 12:17:14 -0400 | [diff] [blame] | 3462 | nr_truncate++; |
Josef Bacik | f3fe820 | 2013-01-07 17:03:21 -0500 | [diff] [blame] | 3463 | |
| 3464 | /* 1 for the orphan item deletion. */ |
| 3465 | trans = btrfs_start_transaction(root, 1); |
| 3466 | if (IS_ERR(trans)) { |
Josef Bacik | c69b26b | 2013-06-03 16:51:23 -0400 | [diff] [blame] | 3467 | iput(inode); |
Josef Bacik | f3fe820 | 2013-01-07 17:03:21 -0500 | [diff] [blame] | 3468 | ret = PTR_ERR(trans); |
| 3469 | goto out; |
| 3470 | } |
| 3471 | ret = btrfs_orphan_add(trans, inode); |
| 3472 | btrfs_end_transaction(trans, root); |
Josef Bacik | c69b26b | 2013-06-03 16:51:23 -0400 | [diff] [blame] | 3473 | if (ret) { |
| 3474 | iput(inode); |
Josef Bacik | f3fe820 | 2013-01-07 17:03:21 -0500 | [diff] [blame] | 3475 | goto out; |
Josef Bacik | c69b26b | 2013-06-03 16:51:23 -0400 | [diff] [blame] | 3476 | } |
Josef Bacik | f3fe820 | 2013-01-07 17:03:21 -0500 | [diff] [blame] | 3477 | |
Josef Bacik | 66b4ffd | 2011-01-31 16:22:42 -0500 | [diff] [blame] | 3478 | ret = btrfs_truncate(inode); |
Josef Bacik | 4a7d0f6 | 2013-02-07 16:27:28 -0500 | [diff] [blame] | 3479 | if (ret) |
| 3480 | btrfs_orphan_del(NULL, inode); |
Josef Bacik | 7b12876 | 2008-07-24 12:17:14 -0400 | [diff] [blame] | 3481 | } else { |
| 3482 | nr_unlink++; |
| 3483 | } |
| 3484 | |
| 3485 | /* this will do delete_inode and everything for us */ |
| 3486 | iput(inode); |
Josef Bacik | 66b4ffd | 2011-01-31 16:22:42 -0500 | [diff] [blame] | 3487 | if (ret) |
| 3488 | goto out; |
Josef Bacik | 7b12876 | 2008-07-24 12:17:14 -0400 | [diff] [blame] | 3489 | } |
Miao Xie | 3254c87 | 2011-11-10 20:45:05 -0500 | [diff] [blame] | 3490 | /* release the path since we're done with it */ |
| 3491 | btrfs_release_path(path); |
| 3492 | |
Yan, Zheng | d68fc57 | 2010-05-16 10:49:58 -0400 | [diff] [blame] | 3493 | root->orphan_cleanup_state = ORPHAN_CLEANUP_DONE; |
| 3494 | |
| 3495 | if (root->orphan_block_rsv) |
| 3496 | btrfs_block_rsv_release(root, root->orphan_block_rsv, |
| 3497 | (u64)-1); |
| 3498 | |
Miao Xie | 27cdeb7 | 2014-04-02 19:51:05 +0800 | [diff] [blame] | 3499 | if (root->orphan_block_rsv || |
| 3500 | test_bit(BTRFS_ROOT_ORPHAN_ITEM_INSERTED, &root->state)) { |
Josef Bacik | 7a7eaa4 | 2011-04-13 12:54:33 -0400 | [diff] [blame] | 3501 | trans = btrfs_join_transaction(root); |
Josef Bacik | 66b4ffd | 2011-01-31 16:22:42 -0500 | [diff] [blame] | 3502 | if (!IS_ERR(trans)) |
| 3503 | btrfs_end_transaction(trans, root); |
Yan, Zheng | d68fc57 | 2010-05-16 10:49:58 -0400 | [diff] [blame] | 3504 | } |
Josef Bacik | 7b12876 | 2008-07-24 12:17:14 -0400 | [diff] [blame] | 3505 | |
| 3506 | if (nr_unlink) |
David Sterba | 4884b47 | 2013-03-20 13:31:27 +0000 | [diff] [blame] | 3507 | btrfs_debug(root->fs_info, "unlinked %d orphans", nr_unlink); |
Josef Bacik | 7b12876 | 2008-07-24 12:17:14 -0400 | [diff] [blame] | 3508 | if (nr_truncate) |
David Sterba | 4884b47 | 2013-03-20 13:31:27 +0000 | [diff] [blame] | 3509 | btrfs_debug(root->fs_info, "truncated %d orphans", nr_truncate); |
Josef Bacik | 66b4ffd | 2011-01-31 16:22:42 -0500 | [diff] [blame] | 3510 | |
| 3511 | out: |
| 3512 | if (ret) |
David Sterba | 68b663d | 2014-12-19 18:38:37 +0100 | [diff] [blame] | 3513 | btrfs_err(root->fs_info, |
Simon Kirby | c2cf52e | 2013-03-19 22:41:23 +0000 | [diff] [blame] | 3514 | "could not do orphan cleanup %d", ret); |
Josef Bacik | 66b4ffd | 2011-01-31 16:22:42 -0500 | [diff] [blame] | 3515 | btrfs_free_path(path); |
| 3516 | return ret; |
Josef Bacik | 7b12876 | 2008-07-24 12:17:14 -0400 | [diff] [blame] | 3517 | } |
| 3518 | |
Chris Mason | d352ac6 | 2008-09-29 15:18:18 -0400 | [diff] [blame] | 3519 | /* |
Chris Mason | 46a53cc | 2009-04-27 11:47:50 -0400 | [diff] [blame] | 3520 | * very simple check to peek ahead in the leaf looking for xattrs. If we |
| 3521 | * don't find any xattrs, we know there can't be any acls. |
| 3522 | * |
| 3523 | * slot is the slot the inode is in, objectid is the objectid of the inode |
| 3524 | */ |
| 3525 | 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] | 3526 | int slot, u64 objectid, |
| 3527 | int *first_xattr_slot) |
Chris Mason | 46a53cc | 2009-04-27 11:47:50 -0400 | [diff] [blame] | 3528 | { |
| 3529 | u32 nritems = btrfs_header_nritems(leaf); |
| 3530 | struct btrfs_key found_key; |
Josef Bacik | f23b5a5 | 2013-06-19 10:16:26 -0400 | [diff] [blame] | 3531 | static u64 xattr_access = 0; |
| 3532 | static u64 xattr_default = 0; |
Chris Mason | 46a53cc | 2009-04-27 11:47:50 -0400 | [diff] [blame] | 3533 | int scanned = 0; |
| 3534 | |
Josef Bacik | f23b5a5 | 2013-06-19 10:16:26 -0400 | [diff] [blame] | 3535 | if (!xattr_access) { |
| 3536 | xattr_access = btrfs_name_hash(POSIX_ACL_XATTR_ACCESS, |
| 3537 | strlen(POSIX_ACL_XATTR_ACCESS)); |
| 3538 | xattr_default = btrfs_name_hash(POSIX_ACL_XATTR_DEFAULT, |
| 3539 | strlen(POSIX_ACL_XATTR_DEFAULT)); |
| 3540 | } |
| 3541 | |
Chris Mason | 46a53cc | 2009-04-27 11:47:50 -0400 | [diff] [blame] | 3542 | slot++; |
Filipe David Borba Manana | 6354192 | 2014-01-07 11:47:46 +0000 | [diff] [blame] | 3543 | *first_xattr_slot = -1; |
Chris Mason | 46a53cc | 2009-04-27 11:47:50 -0400 | [diff] [blame] | 3544 | while (slot < nritems) { |
| 3545 | btrfs_item_key_to_cpu(leaf, &found_key, slot); |
| 3546 | |
| 3547 | /* we found a different objectid, there must not be acls */ |
| 3548 | if (found_key.objectid != objectid) |
| 3549 | return 0; |
| 3550 | |
| 3551 | /* we found an xattr, assume we've got an acl */ |
Josef Bacik | f23b5a5 | 2013-06-19 10:16:26 -0400 | [diff] [blame] | 3552 | if (found_key.type == BTRFS_XATTR_ITEM_KEY) { |
Filipe David Borba Manana | 6354192 | 2014-01-07 11:47:46 +0000 | [diff] [blame] | 3553 | if (*first_xattr_slot == -1) |
| 3554 | *first_xattr_slot = slot; |
Josef Bacik | f23b5a5 | 2013-06-19 10:16:26 -0400 | [diff] [blame] | 3555 | if (found_key.offset == xattr_access || |
| 3556 | found_key.offset == xattr_default) |
| 3557 | return 1; |
| 3558 | } |
Chris Mason | 46a53cc | 2009-04-27 11:47:50 -0400 | [diff] [blame] | 3559 | |
| 3560 | /* |
| 3561 | * we found a key greater than an xattr key, there can't |
| 3562 | * be any acls later on |
| 3563 | */ |
| 3564 | if (found_key.type > BTRFS_XATTR_ITEM_KEY) |
| 3565 | return 0; |
| 3566 | |
| 3567 | slot++; |
| 3568 | scanned++; |
| 3569 | |
| 3570 | /* |
| 3571 | * it goes inode, inode backrefs, xattrs, extents, |
| 3572 | * so if there are a ton of hard links to an inode there can |
| 3573 | * be a lot of backrefs. Don't waste time searching too hard, |
| 3574 | * this is just an optimization |
| 3575 | */ |
| 3576 | if (scanned >= 8) |
| 3577 | break; |
| 3578 | } |
| 3579 | /* we hit the end of the leaf before we found an xattr or |
| 3580 | * something larger than an xattr. We have to assume the inode |
| 3581 | * has acls |
| 3582 | */ |
Filipe David Borba Manana | 6354192 | 2014-01-07 11:47:46 +0000 | [diff] [blame] | 3583 | if (*first_xattr_slot == -1) |
| 3584 | *first_xattr_slot = slot; |
Chris Mason | 46a53cc | 2009-04-27 11:47:50 -0400 | [diff] [blame] | 3585 | return 1; |
| 3586 | } |
| 3587 | |
| 3588 | /* |
Chris Mason | d352ac6 | 2008-09-29 15:18:18 -0400 | [diff] [blame] | 3589 | * read an inode from the btree into the in-memory inode |
| 3590 | */ |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 3591 | static void btrfs_read_locked_inode(struct inode *inode) |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3592 | { |
| 3593 | struct btrfs_path *path; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 3594 | struct extent_buffer *leaf; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3595 | struct btrfs_inode_item *inode_item; |
| 3596 | struct btrfs_root *root = BTRFS_I(inode)->root; |
| 3597 | struct btrfs_key location; |
Miao Xie | 67de117 | 2013-12-26 13:07:06 +0800 | [diff] [blame] | 3598 | unsigned long ptr; |
Chris Mason | 46a53cc | 2009-04-27 11:47:50 -0400 | [diff] [blame] | 3599 | int maybe_acls; |
Josef Bacik | 618e21d | 2007-07-11 10:18:17 -0400 | [diff] [blame] | 3600 | u32 rdev; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3601 | int ret; |
Miao Xie | 2f7e33d | 2011-06-23 07:27:13 +0000 | [diff] [blame] | 3602 | bool filled = false; |
Filipe David Borba Manana | 6354192 | 2014-01-07 11:47:46 +0000 | [diff] [blame] | 3603 | int first_xattr_slot; |
Miao Xie | 2f7e33d | 2011-06-23 07:27:13 +0000 | [diff] [blame] | 3604 | |
| 3605 | ret = btrfs_fill_inode(inode, &rdev); |
| 3606 | if (!ret) |
| 3607 | filled = true; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3608 | |
| 3609 | path = btrfs_alloc_path(); |
Mark Fasheh | 1748f84 | 2011-07-12 11:25:31 -0700 | [diff] [blame] | 3610 | if (!path) |
| 3611 | goto make_bad; |
| 3612 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3613 | memcpy(&location, &BTRFS_I(inode)->location, sizeof(location)); |
Chris Mason | dc17ff8 | 2008-01-08 15:46:30 -0500 | [diff] [blame] | 3614 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3615 | ret = btrfs_lookup_inode(NULL, root, path, &location, 0); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 3616 | if (ret) |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3617 | goto make_bad; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3618 | |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 3619 | leaf = path->nodes[0]; |
Miao Xie | 2f7e33d | 2011-06-23 07:27:13 +0000 | [diff] [blame] | 3620 | |
| 3621 | if (filled) |
Miao Xie | 67de117 | 2013-12-26 13:07:06 +0800 | [diff] [blame] | 3622 | goto cache_index; |
Miao Xie | 2f7e33d | 2011-06-23 07:27:13 +0000 | [diff] [blame] | 3623 | |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 3624 | inode_item = btrfs_item_ptr(leaf, path->slots[0], |
| 3625 | struct btrfs_inode_item); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 3626 | inode->i_mode = btrfs_inode_mode(leaf, inode_item); |
Miklos Szeredi | bfe8684 | 2011-10-28 14:13:29 +0200 | [diff] [blame] | 3627 | set_nlink(inode, btrfs_inode_nlink(leaf, inode_item)); |
Eric W. Biederman | 2f2f43d | 2012-02-10 11:05:07 -0800 | [diff] [blame] | 3628 | i_uid_write(inode, btrfs_inode_uid(leaf, inode_item)); |
| 3629 | i_gid_write(inode, btrfs_inode_gid(leaf, inode_item)); |
Chris Mason | dbe674a | 2008-07-17 12:54:05 -0400 | [diff] [blame] | 3630 | btrfs_i_size_write(inode, btrfs_inode_size(leaf, inode_item)); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 3631 | |
David Sterba | a937b97 | 2014-12-12 17:39:12 +0100 | [diff] [blame] | 3632 | inode->i_atime.tv_sec = btrfs_timespec_sec(leaf, &inode_item->atime); |
| 3633 | inode->i_atime.tv_nsec = btrfs_timespec_nsec(leaf, &inode_item->atime); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 3634 | |
David Sterba | a937b97 | 2014-12-12 17:39:12 +0100 | [diff] [blame] | 3635 | inode->i_mtime.tv_sec = btrfs_timespec_sec(leaf, &inode_item->mtime); |
| 3636 | inode->i_mtime.tv_nsec = btrfs_timespec_nsec(leaf, &inode_item->mtime); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 3637 | |
David Sterba | a937b97 | 2014-12-12 17:39:12 +0100 | [diff] [blame] | 3638 | inode->i_ctime.tv_sec = btrfs_timespec_sec(leaf, &inode_item->ctime); |
| 3639 | inode->i_ctime.tv_nsec = btrfs_timespec_nsec(leaf, &inode_item->ctime); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 3640 | |
chandan r | 9cc97d6 | 2012-07-04 12:48:07 +0530 | [diff] [blame] | 3641 | BTRFS_I(inode)->i_otime.tv_sec = |
| 3642 | btrfs_timespec_sec(leaf, &inode_item->otime); |
| 3643 | BTRFS_I(inode)->i_otime.tv_nsec = |
| 3644 | btrfs_timespec_nsec(leaf, &inode_item->otime); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 3645 | |
Yan Zheng | a76a3cd | 2008-10-09 11:46:29 -0400 | [diff] [blame] | 3646 | inode_set_bytes(inode, btrfs_inode_nbytes(leaf, inode_item)); |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 3647 | BTRFS_I(inode)->generation = btrfs_inode_generation(leaf, inode_item); |
Josef Bacik | 5dc562c | 2012-08-17 13:14:17 -0400 | [diff] [blame] | 3648 | BTRFS_I(inode)->last_trans = btrfs_inode_transid(leaf, inode_item); |
| 3649 | |
Josef Bacik | 0c4d2d9 | 2012-04-05 15:03:02 -0400 | [diff] [blame] | 3650 | inode->i_version = btrfs_inode_sequence(leaf, inode_item); |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 3651 | inode->i_generation = BTRFS_I(inode)->generation; |
Josef Bacik | 618e21d | 2007-07-11 10:18:17 -0400 | [diff] [blame] | 3652 | inode->i_rdev = 0; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 3653 | rdev = btrfs_inode_rdev(leaf, inode_item); |
| 3654 | |
Josef Bacik | aec7477 | 2008-07-24 12:12:38 -0400 | [diff] [blame] | 3655 | BTRFS_I(inode)->index_cnt = (u64)-1; |
Yan Zheng | d2fb343 | 2008-12-11 16:30:39 -0500 | [diff] [blame] | 3656 | BTRFS_I(inode)->flags = btrfs_inode_flags(leaf, inode_item); |
Miao Xie | 67de117 | 2013-12-26 13:07:06 +0800 | [diff] [blame] | 3657 | |
| 3658 | cache_index: |
Yang Dongsheng | 6e17d30 | 2015-04-09 12:08:43 +0800 | [diff] [blame] | 3659 | /* |
| 3660 | * If we were modified in the current generation and evicted from memory |
| 3661 | * and then re-read we need to do a full sync since we don't have any |
| 3662 | * idea about which extents were modified before we were evicted from |
| 3663 | * cache. |
| 3664 | * |
| 3665 | * This is required for both inode re-read from disk and delayed inode |
| 3666 | * in delayed_nodes_tree. |
| 3667 | */ |
| 3668 | if (BTRFS_I(inode)->last_trans == root->fs_info->generation) |
| 3669 | set_bit(BTRFS_INODE_NEEDS_FULL_SYNC, |
| 3670 | &BTRFS_I(inode)->runtime_flags); |
| 3671 | |
Filipe Manana | bde6c24 | 2015-07-24 00:00:19 +0100 | [diff] [blame] | 3672 | /* |
| 3673 | * We don't persist the id of the transaction where an unlink operation |
| 3674 | * against the inode was last made. So here we assume the inode might |
| 3675 | * have been evicted, and therefore the exact value of last_unlink_trans |
| 3676 | * lost, and set it to last_trans to avoid metadata inconsistencies |
| 3677 | * between the inode and its parent if the inode is fsync'ed and the log |
| 3678 | * replayed. For example, in the scenario: |
| 3679 | * |
| 3680 | * touch mydir/foo |
| 3681 | * ln mydir/foo mydir/bar |
| 3682 | * sync |
| 3683 | * unlink mydir/bar |
| 3684 | * echo 2 > /proc/sys/vm/drop_caches # evicts inode |
| 3685 | * xfs_io -c fsync mydir/foo |
| 3686 | * <power failure> |
| 3687 | * mount fs, triggers fsync log replay |
| 3688 | * |
| 3689 | * We must make sure that when we fsync our inode foo we also log its |
| 3690 | * parent inode, otherwise after log replay the parent still has the |
| 3691 | * dentry with the "bar" name but our inode foo has a link count of 1 |
| 3692 | * and doesn't have an inode ref with the name "bar" anymore. |
| 3693 | * |
| 3694 | * Setting last_unlink_trans to last_trans is a pessimistic approach, |
| 3695 | * but it guarantees correctness at the expense of ocassional full |
| 3696 | * transaction commits on fsync if our inode is a directory, or if our |
| 3697 | * inode is not a directory, logging its parent unnecessarily. |
| 3698 | */ |
| 3699 | BTRFS_I(inode)->last_unlink_trans = BTRFS_I(inode)->last_trans; |
| 3700 | |
Miao Xie | 67de117 | 2013-12-26 13:07:06 +0800 | [diff] [blame] | 3701 | path->slots[0]++; |
| 3702 | if (inode->i_nlink != 1 || |
| 3703 | path->slots[0] >= btrfs_header_nritems(leaf)) |
| 3704 | goto cache_acl; |
| 3705 | |
| 3706 | btrfs_item_key_to_cpu(leaf, &location, path->slots[0]); |
| 3707 | if (location.objectid != btrfs_ino(inode)) |
| 3708 | goto cache_acl; |
| 3709 | |
| 3710 | ptr = btrfs_item_ptr_offset(leaf, path->slots[0]); |
| 3711 | if (location.type == BTRFS_INODE_REF_KEY) { |
| 3712 | struct btrfs_inode_ref *ref; |
| 3713 | |
| 3714 | ref = (struct btrfs_inode_ref *)ptr; |
| 3715 | BTRFS_I(inode)->dir_index = btrfs_inode_ref_index(leaf, ref); |
| 3716 | } else if (location.type == BTRFS_INODE_EXTREF_KEY) { |
| 3717 | struct btrfs_inode_extref *extref; |
| 3718 | |
| 3719 | extref = (struct btrfs_inode_extref *)ptr; |
| 3720 | BTRFS_I(inode)->dir_index = btrfs_inode_extref_index(leaf, |
| 3721 | extref); |
| 3722 | } |
Miao Xie | 2f7e33d | 2011-06-23 07:27:13 +0000 | [diff] [blame] | 3723 | cache_acl: |
Chris Mason | 46a53cc | 2009-04-27 11:47:50 -0400 | [diff] [blame] | 3724 | /* |
| 3725 | * try to precache a NULL acl entry for files that don't have |
| 3726 | * any xattrs or acls |
| 3727 | */ |
Li Zefan | 33345d01 | 2011-04-20 10:31:50 +0800 | [diff] [blame] | 3728 | maybe_acls = acls_after_inode_item(leaf, path->slots[0], |
Filipe David Borba Manana | 6354192 | 2014-01-07 11:47:46 +0000 | [diff] [blame] | 3729 | btrfs_ino(inode), &first_xattr_slot); |
| 3730 | if (first_xattr_slot != -1) { |
| 3731 | path->slots[0] = first_xattr_slot; |
| 3732 | ret = btrfs_load_inode_props(inode, path); |
| 3733 | if (ret) |
| 3734 | btrfs_err(root->fs_info, |
David Sterba | 351fd35 | 2014-05-15 16:48:20 +0200 | [diff] [blame] | 3735 | "error loading props for ino %llu (root %llu): %d", |
Filipe David Borba Manana | 6354192 | 2014-01-07 11:47:46 +0000 | [diff] [blame] | 3736 | btrfs_ino(inode), |
| 3737 | root->root_key.objectid, ret); |
| 3738 | } |
| 3739 | btrfs_free_path(path); |
| 3740 | |
Al Viro | 72c0490 | 2009-06-24 16:58:48 -0400 | [diff] [blame] | 3741 | if (!maybe_acls) |
| 3742 | cache_no_acl(inode); |
Chris Mason | 46a53cc | 2009-04-27 11:47:50 -0400 | [diff] [blame] | 3743 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3744 | switch (inode->i_mode & S_IFMT) { |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3745 | case S_IFREG: |
| 3746 | inode->i_mapping->a_ops = &btrfs_aops; |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 3747 | BTRFS_I(inode)->io_tree.ops = &btrfs_extent_io_ops; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3748 | inode->i_fop = &btrfs_file_operations; |
| 3749 | inode->i_op = &btrfs_file_inode_operations; |
| 3750 | break; |
| 3751 | case S_IFDIR: |
| 3752 | inode->i_fop = &btrfs_dir_file_operations; |
| 3753 | if (root == root->fs_info->tree_root) |
| 3754 | inode->i_op = &btrfs_dir_ro_inode_operations; |
| 3755 | else |
| 3756 | inode->i_op = &btrfs_dir_inode_operations; |
| 3757 | break; |
| 3758 | case S_IFLNK: |
| 3759 | inode->i_op = &btrfs_symlink_inode_operations; |
| 3760 | inode->i_mapping->a_ops = &btrfs_symlink_aops; |
| 3761 | break; |
Josef Bacik | 618e21d | 2007-07-11 10:18:17 -0400 | [diff] [blame] | 3762 | default: |
Jim Owens | 0279b4c | 2009-02-04 09:29:13 -0500 | [diff] [blame] | 3763 | inode->i_op = &btrfs_special_inode_operations; |
Josef Bacik | 618e21d | 2007-07-11 10:18:17 -0400 | [diff] [blame] | 3764 | init_special_inode(inode, inode->i_mode, rdev); |
| 3765 | break; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3766 | } |
Christoph Hellwig | 6cbff00 | 2009-04-17 10:37:41 +0200 | [diff] [blame] | 3767 | |
| 3768 | btrfs_update_iflags(inode); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3769 | return; |
| 3770 | |
| 3771 | make_bad: |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3772 | btrfs_free_path(path); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3773 | make_bad_inode(inode); |
| 3774 | } |
| 3775 | |
Chris Mason | d352ac6 | 2008-09-29 15:18:18 -0400 | [diff] [blame] | 3776 | /* |
| 3777 | * given a leaf and an inode, copy the inode fields into the leaf |
| 3778 | */ |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 3779 | static void fill_inode_item(struct btrfs_trans_handle *trans, |
| 3780 | struct extent_buffer *leaf, |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 3781 | struct btrfs_inode_item *item, |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3782 | struct inode *inode) |
| 3783 | { |
Liu Bo | 51fab69 | 2012-12-27 09:01:21 +0000 | [diff] [blame] | 3784 | struct btrfs_map_token token; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 3785 | |
Liu Bo | 51fab69 | 2012-12-27 09:01:21 +0000 | [diff] [blame] | 3786 | btrfs_init_map_token(&token); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 3787 | |
Liu Bo | 51fab69 | 2012-12-27 09:01:21 +0000 | [diff] [blame] | 3788 | btrfs_set_token_inode_uid(leaf, item, i_uid_read(inode), &token); |
| 3789 | btrfs_set_token_inode_gid(leaf, item, i_gid_read(inode), &token); |
| 3790 | btrfs_set_token_inode_size(leaf, item, BTRFS_I(inode)->disk_i_size, |
| 3791 | &token); |
| 3792 | btrfs_set_token_inode_mode(leaf, item, inode->i_mode, &token); |
| 3793 | btrfs_set_token_inode_nlink(leaf, item, inode->i_nlink, &token); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 3794 | |
David Sterba | a937b97 | 2014-12-12 17:39:12 +0100 | [diff] [blame] | 3795 | btrfs_set_token_timespec_sec(leaf, &item->atime, |
Liu Bo | 51fab69 | 2012-12-27 09:01:21 +0000 | [diff] [blame] | 3796 | inode->i_atime.tv_sec, &token); |
David Sterba | a937b97 | 2014-12-12 17:39:12 +0100 | [diff] [blame] | 3797 | btrfs_set_token_timespec_nsec(leaf, &item->atime, |
Liu Bo | 51fab69 | 2012-12-27 09:01:21 +0000 | [diff] [blame] | 3798 | inode->i_atime.tv_nsec, &token); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 3799 | |
David Sterba | a937b97 | 2014-12-12 17:39:12 +0100 | [diff] [blame] | 3800 | btrfs_set_token_timespec_sec(leaf, &item->mtime, |
Liu Bo | 51fab69 | 2012-12-27 09:01:21 +0000 | [diff] [blame] | 3801 | inode->i_mtime.tv_sec, &token); |
David Sterba | a937b97 | 2014-12-12 17:39:12 +0100 | [diff] [blame] | 3802 | btrfs_set_token_timespec_nsec(leaf, &item->mtime, |
Liu Bo | 51fab69 | 2012-12-27 09:01:21 +0000 | [diff] [blame] | 3803 | inode->i_mtime.tv_nsec, &token); |
| 3804 | |
David Sterba | a937b97 | 2014-12-12 17:39:12 +0100 | [diff] [blame] | 3805 | btrfs_set_token_timespec_sec(leaf, &item->ctime, |
Liu Bo | 51fab69 | 2012-12-27 09:01:21 +0000 | [diff] [blame] | 3806 | inode->i_ctime.tv_sec, &token); |
David Sterba | a937b97 | 2014-12-12 17:39:12 +0100 | [diff] [blame] | 3807 | btrfs_set_token_timespec_nsec(leaf, &item->ctime, |
Liu Bo | 51fab69 | 2012-12-27 09:01:21 +0000 | [diff] [blame] | 3808 | inode->i_ctime.tv_nsec, &token); |
| 3809 | |
chandan r | 9cc97d6 | 2012-07-04 12:48:07 +0530 | [diff] [blame] | 3810 | btrfs_set_token_timespec_sec(leaf, &item->otime, |
| 3811 | BTRFS_I(inode)->i_otime.tv_sec, &token); |
| 3812 | btrfs_set_token_timespec_nsec(leaf, &item->otime, |
| 3813 | BTRFS_I(inode)->i_otime.tv_nsec, &token); |
| 3814 | |
Liu Bo | 51fab69 | 2012-12-27 09:01:21 +0000 | [diff] [blame] | 3815 | btrfs_set_token_inode_nbytes(leaf, item, inode_get_bytes(inode), |
| 3816 | &token); |
| 3817 | btrfs_set_token_inode_generation(leaf, item, BTRFS_I(inode)->generation, |
| 3818 | &token); |
| 3819 | btrfs_set_token_inode_sequence(leaf, item, inode->i_version, &token); |
| 3820 | btrfs_set_token_inode_transid(leaf, item, trans->transid, &token); |
| 3821 | btrfs_set_token_inode_rdev(leaf, item, inode->i_rdev, &token); |
| 3822 | btrfs_set_token_inode_flags(leaf, item, BTRFS_I(inode)->flags, &token); |
| 3823 | btrfs_set_token_inode_block_group(leaf, item, 0, &token); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3824 | } |
| 3825 | |
Chris Mason | d352ac6 | 2008-09-29 15:18:18 -0400 | [diff] [blame] | 3826 | /* |
| 3827 | * copy everything in the in-memory inode into the btree. |
| 3828 | */ |
Chris Mason | 2115133 | 2011-11-10 20:39:08 -0500 | [diff] [blame] | 3829 | static noinline int btrfs_update_inode_item(struct btrfs_trans_handle *trans, |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 3830 | struct btrfs_root *root, struct inode *inode) |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3831 | { |
| 3832 | struct btrfs_inode_item *inode_item; |
| 3833 | struct btrfs_path *path; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 3834 | struct extent_buffer *leaf; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3835 | int ret; |
| 3836 | |
| 3837 | path = btrfs_alloc_path(); |
Miao Xie | 16cdcec | 2011-04-22 18:12:22 +0800 | [diff] [blame] | 3838 | if (!path) |
| 3839 | return -ENOMEM; |
| 3840 | |
Chris Mason | b947343 | 2009-03-13 11:00:37 -0400 | [diff] [blame] | 3841 | path->leave_spinning = 1; |
Miao Xie | 16cdcec | 2011-04-22 18:12:22 +0800 | [diff] [blame] | 3842 | ret = btrfs_lookup_inode(trans, root, path, &BTRFS_I(inode)->location, |
| 3843 | 1); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3844 | if (ret) { |
| 3845 | if (ret > 0) |
| 3846 | ret = -ENOENT; |
| 3847 | goto failed; |
| 3848 | } |
| 3849 | |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 3850 | leaf = path->nodes[0]; |
| 3851 | inode_item = btrfs_item_ptr(leaf, path->slots[0], |
Miao Xie | 16cdcec | 2011-04-22 18:12:22 +0800 | [diff] [blame] | 3852 | struct btrfs_inode_item); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3853 | |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 3854 | fill_inode_item(trans, leaf, inode_item, inode); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 3855 | btrfs_mark_buffer_dirty(leaf); |
Josef Bacik | 15ee9bc | 2007-08-10 16:22:09 -0400 | [diff] [blame] | 3856 | btrfs_set_inode_last_trans(trans, inode); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3857 | ret = 0; |
| 3858 | failed: |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3859 | btrfs_free_path(path); |
| 3860 | return ret; |
| 3861 | } |
| 3862 | |
Chris Mason | d352ac6 | 2008-09-29 15:18:18 -0400 | [diff] [blame] | 3863 | /* |
Chris Mason | 2115133 | 2011-11-10 20:39:08 -0500 | [diff] [blame] | 3864 | * copy everything in the in-memory inode into the btree. |
| 3865 | */ |
| 3866 | noinline int btrfs_update_inode(struct btrfs_trans_handle *trans, |
| 3867 | struct btrfs_root *root, struct inode *inode) |
| 3868 | { |
| 3869 | int ret; |
| 3870 | |
| 3871 | /* |
| 3872 | * If the inode is a free space inode, we can deadlock during commit |
| 3873 | * if we put it into the delayed code. |
| 3874 | * |
| 3875 | * The data relocation inode should also be directly updated |
| 3876 | * without delay |
| 3877 | */ |
Liu Bo | 83eea1f | 2012-07-10 05:28:39 -0600 | [diff] [blame] | 3878 | if (!btrfs_is_free_space_inode(inode) |
Josef Bacik | 1d52c78 | 2014-09-18 11:30:44 -0400 | [diff] [blame] | 3879 | && root->root_key.objectid != BTRFS_DATA_RELOC_TREE_OBJECTID |
| 3880 | && !root->fs_info->log_root_recovering) { |
Alexander Block | 8ea05e3 | 2012-07-25 17:35:53 +0200 | [diff] [blame] | 3881 | btrfs_update_root_times(trans, root); |
| 3882 | |
Chris Mason | 2115133 | 2011-11-10 20:39:08 -0500 | [diff] [blame] | 3883 | ret = btrfs_delayed_update_inode(trans, root, inode); |
| 3884 | if (!ret) |
| 3885 | btrfs_set_inode_last_trans(trans, inode); |
| 3886 | return ret; |
| 3887 | } |
| 3888 | |
| 3889 | return btrfs_update_inode_item(trans, root, inode); |
| 3890 | } |
| 3891 | |
Josef Bacik | be6aef6 | 2012-10-22 15:43:12 -0400 | [diff] [blame] | 3892 | noinline int btrfs_update_inode_fallback(struct btrfs_trans_handle *trans, |
| 3893 | struct btrfs_root *root, |
| 3894 | struct inode *inode) |
Chris Mason | 2115133 | 2011-11-10 20:39:08 -0500 | [diff] [blame] | 3895 | { |
| 3896 | int ret; |
| 3897 | |
| 3898 | ret = btrfs_update_inode(trans, root, inode); |
| 3899 | if (ret == -ENOSPC) |
| 3900 | return btrfs_update_inode_item(trans, root, inode); |
| 3901 | return ret; |
| 3902 | } |
| 3903 | |
| 3904 | /* |
Chris Mason | d352ac6 | 2008-09-29 15:18:18 -0400 | [diff] [blame] | 3905 | * unlink helper that gets used here in inode.c and in the tree logging |
| 3906 | * recovery code. It remove a link in a directory with a given name, and |
| 3907 | * also drops the back refs in the inode to the directory |
| 3908 | */ |
Al Viro | 9298679 | 2011-03-04 17:14:37 +0000 | [diff] [blame] | 3909 | static int __btrfs_unlink_inode(struct btrfs_trans_handle *trans, |
| 3910 | struct btrfs_root *root, |
| 3911 | struct inode *dir, struct inode *inode, |
| 3912 | const char *name, int name_len) |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3913 | { |
| 3914 | struct btrfs_path *path; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3915 | int ret = 0; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 3916 | struct extent_buffer *leaf; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3917 | struct btrfs_dir_item *di; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 3918 | struct btrfs_key key; |
Josef Bacik | aec7477 | 2008-07-24 12:12:38 -0400 | [diff] [blame] | 3919 | u64 index; |
Li Zefan | 33345d01 | 2011-04-20 10:31:50 +0800 | [diff] [blame] | 3920 | u64 ino = btrfs_ino(inode); |
| 3921 | u64 dir_ino = btrfs_ino(dir); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3922 | |
| 3923 | path = btrfs_alloc_path(); |
Chris Mason | 54aa1f4 | 2007-06-22 14:16:25 -0400 | [diff] [blame] | 3924 | if (!path) { |
| 3925 | ret = -ENOMEM; |
Tsutomu Itoh | 554233a | 2011-02-03 03:16:25 +0000 | [diff] [blame] | 3926 | goto out; |
Chris Mason | 54aa1f4 | 2007-06-22 14:16:25 -0400 | [diff] [blame] | 3927 | } |
| 3928 | |
Chris Mason | b947343 | 2009-03-13 11:00:37 -0400 | [diff] [blame] | 3929 | path->leave_spinning = 1; |
Li Zefan | 33345d01 | 2011-04-20 10:31:50 +0800 | [diff] [blame] | 3930 | di = btrfs_lookup_dir_item(trans, root, path, dir_ino, |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3931 | name, name_len, -1); |
| 3932 | if (IS_ERR(di)) { |
| 3933 | ret = PTR_ERR(di); |
| 3934 | goto err; |
| 3935 | } |
| 3936 | if (!di) { |
| 3937 | ret = -ENOENT; |
| 3938 | goto err; |
| 3939 | } |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 3940 | leaf = path->nodes[0]; |
| 3941 | btrfs_dir_item_key_to_cpu(leaf, di, &key); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3942 | ret = btrfs_delete_one_dir_name(trans, root, path, di); |
Chris Mason | 54aa1f4 | 2007-06-22 14:16:25 -0400 | [diff] [blame] | 3943 | if (ret) |
| 3944 | goto err; |
David Sterba | b3b4aa7 | 2011-04-21 01:20:15 +0200 | [diff] [blame] | 3945 | btrfs_release_path(path); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3946 | |
Miao Xie | 67de117 | 2013-12-26 13:07:06 +0800 | [diff] [blame] | 3947 | /* |
| 3948 | * If we don't have dir index, we have to get it by looking up |
| 3949 | * the inode ref, since we get the inode ref, remove it directly, |
| 3950 | * it is unnecessary to do delayed deletion. |
| 3951 | * |
| 3952 | * But if we have dir index, needn't search inode ref to get it. |
| 3953 | * Since the inode ref is close to the inode item, it is better |
| 3954 | * that we delay to delete it, and just do this deletion when |
| 3955 | * we update the inode item. |
| 3956 | */ |
| 3957 | if (BTRFS_I(inode)->dir_index) { |
| 3958 | ret = btrfs_delayed_delete_inode_ref(inode); |
| 3959 | if (!ret) { |
| 3960 | index = BTRFS_I(inode)->dir_index; |
| 3961 | goto skip_backref; |
| 3962 | } |
| 3963 | } |
| 3964 | |
Li Zefan | 33345d01 | 2011-04-20 10:31:50 +0800 | [diff] [blame] | 3965 | ret = btrfs_del_inode_ref(trans, root, name, name_len, ino, |
| 3966 | dir_ino, &index); |
Josef Bacik | aec7477 | 2008-07-24 12:12:38 -0400 | [diff] [blame] | 3967 | if (ret) { |
Simon Kirby | c2cf52e | 2013-03-19 22:41:23 +0000 | [diff] [blame] | 3968 | btrfs_info(root->fs_info, |
| 3969 | "failed to delete reference to %.*s, inode %llu parent %llu", |
Geert Uytterhoeven | c1c9ff7 | 2013-08-20 13:20:07 +0200 | [diff] [blame] | 3970 | name_len, name, ino, dir_ino); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 3971 | btrfs_abort_transaction(trans, root, ret); |
Josef Bacik | aec7477 | 2008-07-24 12:12:38 -0400 | [diff] [blame] | 3972 | goto err; |
| 3973 | } |
Miao Xie | 67de117 | 2013-12-26 13:07:06 +0800 | [diff] [blame] | 3974 | skip_backref: |
Miao Xie | 16cdcec | 2011-04-22 18:12:22 +0800 | [diff] [blame] | 3975 | ret = btrfs_delete_delayed_dir_index(trans, root, dir, index); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 3976 | if (ret) { |
| 3977 | btrfs_abort_transaction(trans, root, ret); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3978 | goto err; |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 3979 | } |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3980 | |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 3981 | ret = btrfs_del_inode_ref_in_log(trans, root, name, name_len, |
Li Zefan | 33345d01 | 2011-04-20 10:31:50 +0800 | [diff] [blame] | 3982 | inode, dir_ino); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 3983 | if (ret != 0 && ret != -ENOENT) { |
| 3984 | btrfs_abort_transaction(trans, root, ret); |
| 3985 | goto err; |
| 3986 | } |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 3987 | |
| 3988 | ret = btrfs_del_dir_entries_in_log(trans, root, name, name_len, |
| 3989 | dir, index); |
Chris Mason | 6418c96 | 2010-10-30 07:34:24 -0400 | [diff] [blame] | 3990 | if (ret == -ENOENT) |
| 3991 | ret = 0; |
Zach Brown | d4e3991 | 2013-04-02 21:02:16 +0000 | [diff] [blame] | 3992 | else if (ret) |
| 3993 | btrfs_abort_transaction(trans, root, ret); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3994 | err: |
| 3995 | btrfs_free_path(path); |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 3996 | if (ret) |
| 3997 | goto out; |
| 3998 | |
| 3999 | btrfs_i_size_write(dir, dir->i_size - name_len * 2); |
Josef Bacik | 0c4d2d9 | 2012-04-05 15:03:02 -0400 | [diff] [blame] | 4000 | inode_inc_iversion(inode); |
| 4001 | inode_inc_iversion(dir); |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 4002 | inode->i_ctime = dir->i_mtime = dir->i_ctime = CURRENT_TIME; |
Tsutomu Itoh | b995929 | 2012-06-25 21:25:22 -0600 | [diff] [blame] | 4003 | ret = btrfs_update_inode(trans, root, dir); |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 4004 | out: |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4005 | return ret; |
| 4006 | } |
| 4007 | |
Al Viro | 9298679 | 2011-03-04 17:14:37 +0000 | [diff] [blame] | 4008 | int btrfs_unlink_inode(struct btrfs_trans_handle *trans, |
| 4009 | struct btrfs_root *root, |
| 4010 | struct inode *dir, struct inode *inode, |
| 4011 | const char *name, int name_len) |
| 4012 | { |
| 4013 | int ret; |
| 4014 | ret = __btrfs_unlink_inode(trans, root, dir, inode, name, name_len); |
| 4015 | if (!ret) { |
Zach Brown | 8b558c5 | 2013-10-16 12:10:34 -0700 | [diff] [blame] | 4016 | drop_nlink(inode); |
Al Viro | 9298679 | 2011-03-04 17:14:37 +0000 | [diff] [blame] | 4017 | ret = btrfs_update_inode(trans, root, inode); |
| 4018 | } |
| 4019 | return ret; |
| 4020 | } |
Yan, Zheng | a22285a | 2010-05-16 10:48:46 -0400 | [diff] [blame] | 4021 | |
| 4022 | /* |
| 4023 | * helper to start transaction for unlink and rmdir. |
| 4024 | * |
Josef Bacik | d52be81 | 2013-05-29 14:54:47 -0400 | [diff] [blame] | 4025 | * unlink and rmdir are special in btrfs, they do not always free space, so |
| 4026 | * if we cannot make our reservations the normal way try and see if there is |
| 4027 | * plenty of slack room in the global reserve to migrate, otherwise we cannot |
| 4028 | * allow the unlink to occur. |
Yan, Zheng | a22285a | 2010-05-16 10:48:46 -0400 | [diff] [blame] | 4029 | */ |
Josef Bacik | d52be81 | 2013-05-29 14:54:47 -0400 | [diff] [blame] | 4030 | static struct btrfs_trans_handle *__unlink_start_trans(struct inode *dir) |
Yan, Zheng | a22285a | 2010-05-16 10:48:46 -0400 | [diff] [blame] | 4031 | { |
| 4032 | struct btrfs_trans_handle *trans; |
| 4033 | struct btrfs_root *root = BTRFS_I(dir)->root; |
Yan, Zheng | a22285a | 2010-05-16 10:48:46 -0400 | [diff] [blame] | 4034 | int ret; |
| 4035 | |
Josef Bacik | e70bea5 | 2011-10-11 14:18:24 -0400 | [diff] [blame] | 4036 | /* |
| 4037 | * 1 for the possible orphan item |
| 4038 | * 1 for the dir item |
| 4039 | * 1 for the dir index |
| 4040 | * 1 for the inode ref |
Josef Bacik | e70bea5 | 2011-10-11 14:18:24 -0400 | [diff] [blame] | 4041 | * 1 for the inode |
| 4042 | */ |
Josef Bacik | 6e137ed | 2013-03-26 15:26:55 -0400 | [diff] [blame] | 4043 | trans = btrfs_start_transaction(root, 5); |
Yan, Zheng | a22285a | 2010-05-16 10:48:46 -0400 | [diff] [blame] | 4044 | if (!IS_ERR(trans) || PTR_ERR(trans) != -ENOSPC) |
| 4045 | return trans; |
| 4046 | |
Josef Bacik | d52be81 | 2013-05-29 14:54:47 -0400 | [diff] [blame] | 4047 | if (PTR_ERR(trans) == -ENOSPC) { |
| 4048 | u64 num_bytes = btrfs_calc_trans_metadata_size(root, 5); |
Yan, Zheng | a22285a | 2010-05-16 10:48:46 -0400 | [diff] [blame] | 4049 | |
Josef Bacik | d52be81 | 2013-05-29 14:54:47 -0400 | [diff] [blame] | 4050 | trans = btrfs_start_transaction(root, 0); |
| 4051 | if (IS_ERR(trans)) |
| 4052 | return trans; |
| 4053 | ret = btrfs_cond_migrate_bytes(root->fs_info, |
| 4054 | &root->fs_info->trans_block_rsv, |
| 4055 | num_bytes, 5); |
| 4056 | if (ret) { |
| 4057 | btrfs_end_transaction(trans, root); |
| 4058 | return ERR_PTR(ret); |
Yan, Zheng | a22285a | 2010-05-16 10:48:46 -0400 | [diff] [blame] | 4059 | } |
Josef Bacik | 5a77d76 | 2011-11-01 14:32:23 -0400 | [diff] [blame] | 4060 | trans->block_rsv = &root->fs_info->trans_block_rsv; |
Josef Bacik | d52be81 | 2013-05-29 14:54:47 -0400 | [diff] [blame] | 4061 | trans->bytes_reserved = num_bytes; |
Yan, Zheng | a22285a | 2010-05-16 10:48:46 -0400 | [diff] [blame] | 4062 | } |
Josef Bacik | d52be81 | 2013-05-29 14:54:47 -0400 | [diff] [blame] | 4063 | return trans; |
Yan, Zheng | a22285a | 2010-05-16 10:48:46 -0400 | [diff] [blame] | 4064 | } |
| 4065 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4066 | static int btrfs_unlink(struct inode *dir, struct dentry *dentry) |
| 4067 | { |
Yan, Zheng | a22285a | 2010-05-16 10:48:46 -0400 | [diff] [blame] | 4068 | struct btrfs_root *root = BTRFS_I(dir)->root; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4069 | struct btrfs_trans_handle *trans; |
David Howells | 2b0143b | 2015-03-17 22:25:59 +0000 | [diff] [blame] | 4070 | struct inode *inode = d_inode(dentry); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4071 | int ret; |
| 4072 | |
Josef Bacik | d52be81 | 2013-05-29 14:54:47 -0400 | [diff] [blame] | 4073 | trans = __unlink_start_trans(dir); |
Yan, Zheng | a22285a | 2010-05-16 10:48:46 -0400 | [diff] [blame] | 4074 | if (IS_ERR(trans)) |
Josef Bacik | 5df6a9f | 2009-11-10 21:23:48 -0500 | [diff] [blame] | 4075 | return PTR_ERR(trans); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 4076 | |
David Howells | 2b0143b | 2015-03-17 22:25:59 +0000 | [diff] [blame] | 4077 | btrfs_record_unlink_dir(trans, dir, d_inode(dentry), 0); |
Chris Mason | 12fcfd2 | 2009-03-24 10:24:20 -0400 | [diff] [blame] | 4078 | |
David Howells | 2b0143b | 2015-03-17 22:25:59 +0000 | [diff] [blame] | 4079 | ret = btrfs_unlink_inode(trans, root, dir, d_inode(dentry), |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 4080 | dentry->d_name.name, dentry->d_name.len); |
Tsutomu Itoh | b532402 | 2011-07-19 07:27:20 +0000 | [diff] [blame] | 4081 | if (ret) |
| 4082 | goto out; |
Josef Bacik | 7b12876 | 2008-07-24 12:17:14 -0400 | [diff] [blame] | 4083 | |
Yan, Zheng | a22285a | 2010-05-16 10:48:46 -0400 | [diff] [blame] | 4084 | if (inode->i_nlink == 0) { |
Josef Bacik | 7b12876 | 2008-07-24 12:17:14 -0400 | [diff] [blame] | 4085 | ret = btrfs_orphan_add(trans, inode); |
Tsutomu Itoh | b532402 | 2011-07-19 07:27:20 +0000 | [diff] [blame] | 4086 | if (ret) |
| 4087 | goto out; |
Yan, Zheng | a22285a | 2010-05-16 10:48:46 -0400 | [diff] [blame] | 4088 | } |
Josef Bacik | 7b12876 | 2008-07-24 12:17:14 -0400 | [diff] [blame] | 4089 | |
Tsutomu Itoh | b532402 | 2011-07-19 07:27:20 +0000 | [diff] [blame] | 4090 | out: |
Josef Bacik | d52be81 | 2013-05-29 14:54:47 -0400 | [diff] [blame] | 4091 | btrfs_end_transaction(trans, root); |
Liu Bo | b53d3f5 | 2012-11-14 14:34:34 +0000 | [diff] [blame] | 4092 | btrfs_btree_balance_dirty(root); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4093 | return ret; |
| 4094 | } |
| 4095 | |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 4096 | int btrfs_unlink_subvol(struct btrfs_trans_handle *trans, |
| 4097 | struct btrfs_root *root, |
| 4098 | struct inode *dir, u64 objectid, |
| 4099 | const char *name, int name_len) |
| 4100 | { |
| 4101 | struct btrfs_path *path; |
| 4102 | struct extent_buffer *leaf; |
| 4103 | struct btrfs_dir_item *di; |
| 4104 | struct btrfs_key key; |
| 4105 | u64 index; |
| 4106 | int ret; |
Li Zefan | 33345d01 | 2011-04-20 10:31:50 +0800 | [diff] [blame] | 4107 | u64 dir_ino = btrfs_ino(dir); |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 4108 | |
| 4109 | path = btrfs_alloc_path(); |
| 4110 | if (!path) |
| 4111 | return -ENOMEM; |
| 4112 | |
Li Zefan | 33345d01 | 2011-04-20 10:31:50 +0800 | [diff] [blame] | 4113 | di = btrfs_lookup_dir_item(trans, root, path, dir_ino, |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 4114 | name, name_len, -1); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 4115 | if (IS_ERR_OR_NULL(di)) { |
| 4116 | if (!di) |
| 4117 | ret = -ENOENT; |
| 4118 | else |
| 4119 | ret = PTR_ERR(di); |
| 4120 | goto out; |
| 4121 | } |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 4122 | |
| 4123 | leaf = path->nodes[0]; |
| 4124 | btrfs_dir_item_key_to_cpu(leaf, di, &key); |
| 4125 | WARN_ON(key.type != BTRFS_ROOT_ITEM_KEY || key.objectid != objectid); |
| 4126 | ret = btrfs_delete_one_dir_name(trans, root, path, di); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 4127 | if (ret) { |
| 4128 | btrfs_abort_transaction(trans, root, ret); |
| 4129 | goto out; |
| 4130 | } |
David Sterba | b3b4aa7 | 2011-04-21 01:20:15 +0200 | [diff] [blame] | 4131 | btrfs_release_path(path); |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 4132 | |
| 4133 | ret = btrfs_del_root_ref(trans, root->fs_info->tree_root, |
| 4134 | objectid, root->root_key.objectid, |
Li Zefan | 33345d01 | 2011-04-20 10:31:50 +0800 | [diff] [blame] | 4135 | dir_ino, &index, name, name_len); |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 4136 | if (ret < 0) { |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 4137 | if (ret != -ENOENT) { |
| 4138 | btrfs_abort_transaction(trans, root, ret); |
| 4139 | goto out; |
| 4140 | } |
Li Zefan | 33345d01 | 2011-04-20 10:31:50 +0800 | [diff] [blame] | 4141 | di = btrfs_search_dir_index_item(root, path, dir_ino, |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 4142 | name, name_len); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 4143 | if (IS_ERR_OR_NULL(di)) { |
| 4144 | if (!di) |
| 4145 | ret = -ENOENT; |
| 4146 | else |
| 4147 | ret = PTR_ERR(di); |
| 4148 | btrfs_abort_transaction(trans, root, ret); |
| 4149 | goto out; |
| 4150 | } |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 4151 | |
| 4152 | leaf = path->nodes[0]; |
| 4153 | btrfs_item_key_to_cpu(leaf, &key, path->slots[0]); |
David Sterba | b3b4aa7 | 2011-04-21 01:20:15 +0200 | [diff] [blame] | 4154 | btrfs_release_path(path); |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 4155 | index = key.offset; |
| 4156 | } |
David Sterba | b3b4aa7 | 2011-04-21 01:20:15 +0200 | [diff] [blame] | 4157 | btrfs_release_path(path); |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 4158 | |
Miao Xie | 16cdcec | 2011-04-22 18:12:22 +0800 | [diff] [blame] | 4159 | ret = btrfs_delete_delayed_dir_index(trans, root, dir, index); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 4160 | if (ret) { |
| 4161 | btrfs_abort_transaction(trans, root, ret); |
| 4162 | goto out; |
| 4163 | } |
Miao Xie | 16cdcec | 2011-04-22 18:12:22 +0800 | [diff] [blame] | 4164 | |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 4165 | btrfs_i_size_write(dir, dir->i_size - name_len * 2); |
Josef Bacik | 0c4d2d9 | 2012-04-05 15:03:02 -0400 | [diff] [blame] | 4166 | inode_inc_iversion(dir); |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 4167 | dir->i_mtime = dir->i_ctime = CURRENT_TIME; |
Josef Bacik | 5a24e84 | 2012-08-08 10:12:59 -0600 | [diff] [blame] | 4168 | ret = btrfs_update_inode_fallback(trans, root, dir); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 4169 | if (ret) |
| 4170 | btrfs_abort_transaction(trans, root, ret); |
| 4171 | out: |
Josef Bacik | 71d7aed | 2011-06-14 14:24:32 -0400 | [diff] [blame] | 4172 | btrfs_free_path(path); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 4173 | return ret; |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 4174 | } |
| 4175 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4176 | static int btrfs_rmdir(struct inode *dir, struct dentry *dentry) |
| 4177 | { |
David Howells | 2b0143b | 2015-03-17 22:25:59 +0000 | [diff] [blame] | 4178 | struct inode *inode = d_inode(dentry); |
Chris Mason | 1832a6d | 2007-12-21 16:27:21 -0500 | [diff] [blame] | 4179 | int err = 0; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4180 | struct btrfs_root *root = BTRFS_I(dir)->root; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4181 | struct btrfs_trans_handle *trans; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4182 | |
David Sterba | b3ae244 | 2012-09-13 16:04:34 -0600 | [diff] [blame] | 4183 | if (inode->i_size > BTRFS_EMPTY_DIR_SIZE) |
Yan | 134d451 | 2007-10-25 15:49:25 -0400 | [diff] [blame] | 4184 | return -ENOTEMPTY; |
David Sterba | b3ae244 | 2012-09-13 16:04:34 -0600 | [diff] [blame] | 4185 | if (btrfs_ino(inode) == BTRFS_FIRST_FREE_OBJECTID) |
| 4186 | return -EPERM; |
Yan | 134d451 | 2007-10-25 15:49:25 -0400 | [diff] [blame] | 4187 | |
Josef Bacik | d52be81 | 2013-05-29 14:54:47 -0400 | [diff] [blame] | 4188 | trans = __unlink_start_trans(dir); |
Yan, Zheng | a22285a | 2010-05-16 10:48:46 -0400 | [diff] [blame] | 4189 | if (IS_ERR(trans)) |
Josef Bacik | 5df6a9f | 2009-11-10 21:23:48 -0500 | [diff] [blame] | 4190 | return PTR_ERR(trans); |
Josef Bacik | 5df6a9f | 2009-11-10 21:23:48 -0500 | [diff] [blame] | 4191 | |
Li Zefan | 33345d01 | 2011-04-20 10:31:50 +0800 | [diff] [blame] | 4192 | if (unlikely(btrfs_ino(inode) == BTRFS_EMPTY_SUBVOL_DIR_OBJECTID)) { |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 4193 | err = btrfs_unlink_subvol(trans, root, dir, |
| 4194 | BTRFS_I(inode)->location.objectid, |
| 4195 | dentry->d_name.name, |
| 4196 | dentry->d_name.len); |
| 4197 | goto out; |
| 4198 | } |
| 4199 | |
Josef Bacik | 7b12876 | 2008-07-24 12:17:14 -0400 | [diff] [blame] | 4200 | err = btrfs_orphan_add(trans, inode); |
| 4201 | if (err) |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 4202 | goto out; |
Josef Bacik | 7b12876 | 2008-07-24 12:17:14 -0400 | [diff] [blame] | 4203 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4204 | /* now the directory is empty */ |
David Howells | 2b0143b | 2015-03-17 22:25:59 +0000 | [diff] [blame] | 4205 | err = btrfs_unlink_inode(trans, root, dir, d_inode(dentry), |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 4206 | dentry->d_name.name, dentry->d_name.len); |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 4207 | if (!err) |
Chris Mason | dbe674a | 2008-07-17 12:54:05 -0400 | [diff] [blame] | 4208 | btrfs_i_size_write(inode, 0); |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 4209 | out: |
Josef Bacik | d52be81 | 2013-05-29 14:54:47 -0400 | [diff] [blame] | 4210 | btrfs_end_transaction(trans, root); |
Liu Bo | b53d3f5 | 2012-11-14 14:34:34 +0000 | [diff] [blame] | 4211 | btrfs_btree_balance_dirty(root); |
Chris Mason | 3954401 | 2007-12-12 14:38:19 -0500 | [diff] [blame] | 4212 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4213 | return err; |
| 4214 | } |
| 4215 | |
Chris Mason | 28f75a0 | 2015-02-04 06:59:29 -0800 | [diff] [blame] | 4216 | static int truncate_space_check(struct btrfs_trans_handle *trans, |
| 4217 | struct btrfs_root *root, |
| 4218 | u64 bytes_deleted) |
| 4219 | { |
| 4220 | int ret; |
| 4221 | |
| 4222 | bytes_deleted = btrfs_csum_bytes_to_leaves(root, bytes_deleted); |
| 4223 | ret = btrfs_block_rsv_add(root, &root->fs_info->trans_block_rsv, |
| 4224 | bytes_deleted, BTRFS_RESERVE_NO_FLUSH); |
| 4225 | if (!ret) |
| 4226 | trans->bytes_reserved += bytes_deleted; |
| 4227 | return ret; |
| 4228 | |
| 4229 | } |
| 4230 | |
Filipe Manana | 0305cd5 | 2015-10-16 12:34:25 +0100 | [diff] [blame] | 4231 | static int truncate_inline_extent(struct inode *inode, |
| 4232 | struct btrfs_path *path, |
| 4233 | struct btrfs_key *found_key, |
| 4234 | const u64 item_end, |
| 4235 | const u64 new_size) |
| 4236 | { |
| 4237 | struct extent_buffer *leaf = path->nodes[0]; |
| 4238 | int slot = path->slots[0]; |
| 4239 | struct btrfs_file_extent_item *fi; |
| 4240 | u32 size = (u32)(new_size - found_key->offset); |
| 4241 | struct btrfs_root *root = BTRFS_I(inode)->root; |
| 4242 | |
| 4243 | fi = btrfs_item_ptr(leaf, slot, struct btrfs_file_extent_item); |
| 4244 | |
| 4245 | if (btrfs_file_extent_compression(leaf, fi) != BTRFS_COMPRESS_NONE) { |
| 4246 | loff_t offset = new_size; |
| 4247 | loff_t page_end = ALIGN(offset, PAGE_CACHE_SIZE); |
| 4248 | |
| 4249 | /* |
| 4250 | * Zero out the remaining of the last page of our inline extent, |
| 4251 | * instead of directly truncating our inline extent here - that |
| 4252 | * would be much more complex (decompressing all the data, then |
| 4253 | * compressing the truncated data, which might be bigger than |
| 4254 | * the size of the inline extent, resize the extent, etc). |
| 4255 | * We release the path because to get the page we might need to |
| 4256 | * read the extent item from disk (data not in the page cache). |
| 4257 | */ |
| 4258 | btrfs_release_path(path); |
| 4259 | return btrfs_truncate_page(inode, offset, page_end - offset, 0); |
| 4260 | } |
| 4261 | |
| 4262 | btrfs_set_file_extent_ram_bytes(leaf, fi, size); |
| 4263 | size = btrfs_file_extent_calc_inline_size(size); |
| 4264 | btrfs_truncate_item(root, path, size, 1); |
| 4265 | |
| 4266 | if (test_bit(BTRFS_ROOT_REF_COWS, &root->state)) |
| 4267 | inode_sub_bytes(inode, item_end + 1 - new_size); |
| 4268 | |
| 4269 | return 0; |
| 4270 | } |
| 4271 | |
Chris Mason | 323ac95 | 2008-10-01 19:05:46 -0400 | [diff] [blame] | 4272 | /* |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4273 | * this can truncate away extent items, csum items and directory items. |
| 4274 | * 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] | 4275 | * any higher than new_size |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4276 | * |
| 4277 | * csum items that cross the new i_size are truncated to the new size |
| 4278 | * as well. |
Josef Bacik | 7b12876 | 2008-07-24 12:17:14 -0400 | [diff] [blame] | 4279 | * |
| 4280 | * min_type is the minimum key type to truncate down to. If set to 0, this |
| 4281 | * 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] | 4282 | */ |
Yan, Zheng | 8082510 | 2009-11-12 09:35:36 +0000 | [diff] [blame] | 4283 | int btrfs_truncate_inode_items(struct btrfs_trans_handle *trans, |
| 4284 | struct btrfs_root *root, |
| 4285 | struct inode *inode, |
| 4286 | u64 new_size, u32 min_type) |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4287 | { |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4288 | struct btrfs_path *path; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 4289 | struct extent_buffer *leaf; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4290 | struct btrfs_file_extent_item *fi; |
Yan, Zheng | 8082510 | 2009-11-12 09:35:36 +0000 | [diff] [blame] | 4291 | struct btrfs_key key; |
| 4292 | struct btrfs_key found_key; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4293 | u64 extent_start = 0; |
Chris Mason | db94535 | 2007-10-15 16:15:53 -0400 | [diff] [blame] | 4294 | u64 extent_num_bytes = 0; |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 4295 | u64 extent_offset = 0; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4296 | u64 item_end = 0; |
Filipe Manana | c1aa457 | 2015-06-20 18:20:09 +0100 | [diff] [blame] | 4297 | u64 last_size = new_size; |
Yan, Zheng | 8082510 | 2009-11-12 09:35:36 +0000 | [diff] [blame] | 4298 | u32 found_type = (u8)-1; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4299 | int found_extent; |
| 4300 | int del_item; |
Chris Mason | 85e21ba | 2008-01-29 15:11:36 -0500 | [diff] [blame] | 4301 | int pending_del_nr = 0; |
| 4302 | int pending_del_slot = 0; |
Chris Mason | 179e29e | 2007-11-01 11:28:41 -0400 | [diff] [blame] | 4303 | int extent_type = -1; |
Yan, Zheng | 8082510 | 2009-11-12 09:35:36 +0000 | [diff] [blame] | 4304 | int ret; |
| 4305 | int err = 0; |
Li Zefan | 33345d01 | 2011-04-20 10:31:50 +0800 | [diff] [blame] | 4306 | u64 ino = btrfs_ino(inode); |
Chris Mason | 28ed134 | 2014-12-17 09:41:04 -0800 | [diff] [blame] | 4307 | u64 bytes_deleted = 0; |
Josef Bacik | 1262133 | 2015-02-03 07:50:16 -0800 | [diff] [blame] | 4308 | bool be_nice = 0; |
| 4309 | bool should_throttle = 0; |
Chris Mason | 28f75a0 | 2015-02-04 06:59:29 -0800 | [diff] [blame] | 4310 | bool should_end = 0; |
Yan, Zheng | 8082510 | 2009-11-12 09:35:36 +0000 | [diff] [blame] | 4311 | |
| 4312 | BUG_ON(new_size > 0 && min_type != BTRFS_EXTENT_DATA_KEY); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4313 | |
Chris Mason | 28ed134 | 2014-12-17 09:41:04 -0800 | [diff] [blame] | 4314 | /* |
| 4315 | * for non-free space inodes and ref cows, we want to back off from |
| 4316 | * time to time |
| 4317 | */ |
| 4318 | if (!btrfs_is_free_space_inode(inode) && |
| 4319 | test_bit(BTRFS_ROOT_REF_COWS, &root->state)) |
| 4320 | be_nice = 1; |
| 4321 | |
Mark Fasheh | 0eb0e19 | 2011-07-12 16:44:10 -0700 | [diff] [blame] | 4322 | path = btrfs_alloc_path(); |
| 4323 | if (!path) |
| 4324 | return -ENOMEM; |
| 4325 | path->reada = -1; |
| 4326 | |
Josef Bacik | 5dc562c | 2012-08-17 13:14:17 -0400 | [diff] [blame] | 4327 | /* |
| 4328 | * We want to drop from the next block forward in case this new size is |
| 4329 | * not block aligned since we will be keeping the last block of the |
| 4330 | * extent just the way it is. |
| 4331 | */ |
Miao Xie | 27cdeb7 | 2014-04-02 19:51:05 +0800 | [diff] [blame] | 4332 | if (test_bit(BTRFS_ROOT_REF_COWS, &root->state) || |
| 4333 | root == root->fs_info->tree_root) |
Qu Wenruo | fda2832 | 2013-02-26 08:10:22 +0000 | [diff] [blame] | 4334 | btrfs_drop_extent_cache(inode, ALIGN(new_size, |
| 4335 | root->sectorsize), (u64)-1, 0); |
Yan, Zheng | 8082510 | 2009-11-12 09:35:36 +0000 | [diff] [blame] | 4336 | |
Miao Xie | 16cdcec | 2011-04-22 18:12:22 +0800 | [diff] [blame] | 4337 | /* |
| 4338 | * This function is also used to drop the items in the log tree before |
| 4339 | * we relog the inode, so if root != BTRFS_I(inode)->root, it means |
| 4340 | * it is used to drop the loged items. So we shouldn't kill the delayed |
| 4341 | * items. |
| 4342 | */ |
| 4343 | if (min_type == 0 && root == BTRFS_I(inode)->root) |
| 4344 | btrfs_kill_delayed_inode_items(inode); |
| 4345 | |
Li Zefan | 33345d01 | 2011-04-20 10:31:50 +0800 | [diff] [blame] | 4346 | key.objectid = ino; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4347 | key.offset = (u64)-1; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 4348 | key.type = (u8)-1; |
| 4349 | |
Chris Mason | 85e21ba | 2008-01-29 15:11:36 -0500 | [diff] [blame] | 4350 | search_again: |
Chris Mason | 28ed134 | 2014-12-17 09:41:04 -0800 | [diff] [blame] | 4351 | /* |
| 4352 | * with a 16K leaf size and 128MB extents, you can actually queue |
| 4353 | * up a huge file in a single leaf. Most of the time that |
| 4354 | * bytes_deleted is > 0, it will be huge by the time we get here |
| 4355 | */ |
| 4356 | if (be_nice && bytes_deleted > 32 * 1024 * 1024) { |
| 4357 | if (btrfs_should_end_transaction(trans, root)) { |
| 4358 | err = -EAGAIN; |
| 4359 | goto error; |
| 4360 | } |
| 4361 | } |
| 4362 | |
| 4363 | |
Chris Mason | b947343 | 2009-03-13 11:00:37 -0400 | [diff] [blame] | 4364 | path->leave_spinning = 1; |
Chris Mason | 85e21ba | 2008-01-29 15:11:36 -0500 | [diff] [blame] | 4365 | ret = btrfs_search_slot(trans, root, &key, path, -1, 1); |
Yan, Zheng | 8082510 | 2009-11-12 09:35:36 +0000 | [diff] [blame] | 4366 | if (ret < 0) { |
| 4367 | err = ret; |
| 4368 | goto out; |
| 4369 | } |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 4370 | |
Chris Mason | 85e21ba | 2008-01-29 15:11:36 -0500 | [diff] [blame] | 4371 | if (ret > 0) { |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 4372 | /* there are no items in the tree for us to truncate, we're |
| 4373 | * done |
| 4374 | */ |
Yan, Zheng | 8082510 | 2009-11-12 09:35:36 +0000 | [diff] [blame] | 4375 | if (path->slots[0] == 0) |
| 4376 | goto out; |
Chris Mason | 85e21ba | 2008-01-29 15:11:36 -0500 | [diff] [blame] | 4377 | path->slots[0]--; |
| 4378 | } |
| 4379 | |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 4380 | while (1) { |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4381 | fi = NULL; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 4382 | leaf = path->nodes[0]; |
| 4383 | btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]); |
David Sterba | 962a298 | 2014-06-04 18:41:45 +0200 | [diff] [blame] | 4384 | found_type = found_key.type; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4385 | |
Li Zefan | 33345d01 | 2011-04-20 10:31:50 +0800 | [diff] [blame] | 4386 | if (found_key.objectid != ino) |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4387 | break; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 4388 | |
Chris Mason | 85e21ba | 2008-01-29 15:11:36 -0500 | [diff] [blame] | 4389 | if (found_type < min_type) |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4390 | break; |
| 4391 | |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 4392 | item_end = found_key.offset; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4393 | if (found_type == BTRFS_EXTENT_DATA_KEY) { |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 4394 | fi = btrfs_item_ptr(leaf, path->slots[0], |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4395 | struct btrfs_file_extent_item); |
Chris Mason | 179e29e | 2007-11-01 11:28:41 -0400 | [diff] [blame] | 4396 | extent_type = btrfs_file_extent_type(leaf, fi); |
| 4397 | if (extent_type != BTRFS_FILE_EXTENT_INLINE) { |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 4398 | item_end += |
Chris Mason | db94535 | 2007-10-15 16:15:53 -0400 | [diff] [blame] | 4399 | btrfs_file_extent_num_bytes(leaf, fi); |
Chris Mason | 179e29e | 2007-11-01 11:28:41 -0400 | [diff] [blame] | 4400 | } else if (extent_type == BTRFS_FILE_EXTENT_INLINE) { |
Chris Mason | 179e29e | 2007-11-01 11:28:41 -0400 | [diff] [blame] | 4401 | item_end += btrfs_file_extent_inline_len(leaf, |
Chris Mason | 514ac8a | 2014-01-03 21:07:00 -0800 | [diff] [blame] | 4402 | path->slots[0], fi); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4403 | } |
Yan | 008630c | 2007-11-07 13:31:09 -0500 | [diff] [blame] | 4404 | item_end--; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4405 | } |
Yan, Zheng | 8082510 | 2009-11-12 09:35:36 +0000 | [diff] [blame] | 4406 | if (found_type > min_type) { |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4407 | del_item = 1; |
Yan, Zheng | 8082510 | 2009-11-12 09:35:36 +0000 | [diff] [blame] | 4408 | } else { |
| 4409 | if (item_end < new_size) |
| 4410 | break; |
| 4411 | if (found_key.offset >= new_size) |
| 4412 | del_item = 1; |
| 4413 | else |
| 4414 | del_item = 0; |
| 4415 | } |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4416 | found_extent = 0; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4417 | /* FIXME, shrink the extent if the ref count is only 1 */ |
Chris Mason | 179e29e | 2007-11-01 11:28:41 -0400 | [diff] [blame] | 4418 | if (found_type != BTRFS_EXTENT_DATA_KEY) |
| 4419 | goto delete; |
| 4420 | |
Josef Bacik | 7f4f6e0 | 2013-08-29 16:43:28 -0400 | [diff] [blame] | 4421 | if (del_item) |
| 4422 | last_size = found_key.offset; |
| 4423 | else |
| 4424 | last_size = new_size; |
| 4425 | |
Chris Mason | 179e29e | 2007-11-01 11:28:41 -0400 | [diff] [blame] | 4426 | if (extent_type != BTRFS_FILE_EXTENT_INLINE) { |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4427 | u64 num_dec; |
Chris Mason | db94535 | 2007-10-15 16:15:53 -0400 | [diff] [blame] | 4428 | extent_start = btrfs_file_extent_disk_bytenr(leaf, fi); |
Miao Xie | f70a9a6b | 2012-01-12 19:10:12 -0500 | [diff] [blame] | 4429 | if (!del_item) { |
Chris Mason | db94535 | 2007-10-15 16:15:53 -0400 | [diff] [blame] | 4430 | u64 orig_num_bytes = |
| 4431 | btrfs_file_extent_num_bytes(leaf, fi); |
Qu Wenruo | fda2832 | 2013-02-26 08:10:22 +0000 | [diff] [blame] | 4432 | extent_num_bytes = ALIGN(new_size - |
| 4433 | found_key.offset, |
| 4434 | root->sectorsize); |
Chris Mason | db94535 | 2007-10-15 16:15:53 -0400 | [diff] [blame] | 4435 | btrfs_set_file_extent_num_bytes(leaf, fi, |
| 4436 | extent_num_bytes); |
| 4437 | num_dec = (orig_num_bytes - |
Chris Mason | 9069218 | 2008-02-08 13:49:28 -0500 | [diff] [blame] | 4438 | extent_num_bytes); |
Miao Xie | 27cdeb7 | 2014-04-02 19:51:05 +0800 | [diff] [blame] | 4439 | if (test_bit(BTRFS_ROOT_REF_COWS, |
| 4440 | &root->state) && |
| 4441 | extent_start != 0) |
Yan Zheng | a76a3cd | 2008-10-09 11:46:29 -0400 | [diff] [blame] | 4442 | inode_sub_bytes(inode, num_dec); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 4443 | btrfs_mark_buffer_dirty(leaf); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4444 | } else { |
Chris Mason | db94535 | 2007-10-15 16:15:53 -0400 | [diff] [blame] | 4445 | extent_num_bytes = |
| 4446 | btrfs_file_extent_disk_num_bytes(leaf, |
| 4447 | fi); |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 4448 | extent_offset = found_key.offset - |
| 4449 | btrfs_file_extent_offset(leaf, fi); |
| 4450 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4451 | /* FIXME blocksize != 4096 */ |
Chris Mason | 9069218 | 2008-02-08 13:49:28 -0500 | [diff] [blame] | 4452 | num_dec = btrfs_file_extent_num_bytes(leaf, fi); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4453 | if (extent_start != 0) { |
| 4454 | found_extent = 1; |
Miao Xie | 27cdeb7 | 2014-04-02 19:51:05 +0800 | [diff] [blame] | 4455 | if (test_bit(BTRFS_ROOT_REF_COWS, |
| 4456 | &root->state)) |
Yan Zheng | a76a3cd | 2008-10-09 11:46:29 -0400 | [diff] [blame] | 4457 | inode_sub_bytes(inode, num_dec); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4458 | } |
| 4459 | } |
Chris Mason | 9069218 | 2008-02-08 13:49:28 -0500 | [diff] [blame] | 4460 | } else if (extent_type == BTRFS_FILE_EXTENT_INLINE) { |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 4461 | /* |
| 4462 | * we can't truncate inline items that have had |
| 4463 | * special encodings |
| 4464 | */ |
| 4465 | if (!del_item && |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 4466 | btrfs_file_extent_encryption(leaf, fi) == 0 && |
| 4467 | btrfs_file_extent_other_encoding(leaf, fi) == 0) { |
Chris Mason | 514ac8a | 2014-01-03 21:07:00 -0800 | [diff] [blame] | 4468 | |
| 4469 | /* |
Filipe Manana | 0305cd5 | 2015-10-16 12:34:25 +0100 | [diff] [blame] | 4470 | * Need to release path in order to truncate a |
| 4471 | * compressed extent. So delete any accumulated |
| 4472 | * extent items so far. |
Chris Mason | 514ac8a | 2014-01-03 21:07:00 -0800 | [diff] [blame] | 4473 | */ |
Filipe Manana | 0305cd5 | 2015-10-16 12:34:25 +0100 | [diff] [blame] | 4474 | if (btrfs_file_extent_compression(leaf, fi) != |
| 4475 | BTRFS_COMPRESS_NONE && pending_del_nr) { |
| 4476 | err = btrfs_del_items(trans, root, path, |
| 4477 | pending_del_slot, |
| 4478 | pending_del_nr); |
| 4479 | if (err) { |
| 4480 | btrfs_abort_transaction(trans, |
| 4481 | root, |
| 4482 | err); |
| 4483 | goto error; |
| 4484 | } |
| 4485 | pending_del_nr = 0; |
| 4486 | } |
| 4487 | |
| 4488 | err = truncate_inline_extent(inode, path, |
| 4489 | &found_key, |
| 4490 | item_end, |
| 4491 | new_size); |
| 4492 | if (err) { |
| 4493 | btrfs_abort_transaction(trans, |
| 4494 | root, err); |
| 4495 | goto error; |
| 4496 | } |
Miao Xie | 27cdeb7 | 2014-04-02 19:51:05 +0800 | [diff] [blame] | 4497 | } else if (test_bit(BTRFS_ROOT_REF_COWS, |
| 4498 | &root->state)) { |
Filipe Manana | 0305cd5 | 2015-10-16 12:34:25 +0100 | [diff] [blame] | 4499 | inode_sub_bytes(inode, item_end + 1 - new_size); |
Chris Mason | 9069218 | 2008-02-08 13:49:28 -0500 | [diff] [blame] | 4500 | } |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4501 | } |
Chris Mason | 179e29e | 2007-11-01 11:28:41 -0400 | [diff] [blame] | 4502 | delete: |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4503 | if (del_item) { |
Chris Mason | 85e21ba | 2008-01-29 15:11:36 -0500 | [diff] [blame] | 4504 | if (!pending_del_nr) { |
| 4505 | /* no pending yet, add ourselves */ |
| 4506 | pending_del_slot = path->slots[0]; |
| 4507 | pending_del_nr = 1; |
| 4508 | } else if (pending_del_nr && |
| 4509 | path->slots[0] + 1 == pending_del_slot) { |
| 4510 | /* hop on the pending chunk */ |
| 4511 | pending_del_nr++; |
| 4512 | pending_del_slot = path->slots[0]; |
| 4513 | } else { |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 4514 | BUG(); |
Chris Mason | 85e21ba | 2008-01-29 15:11:36 -0500 | [diff] [blame] | 4515 | } |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4516 | } else { |
| 4517 | break; |
| 4518 | } |
Chris Mason | 28f75a0 | 2015-02-04 06:59:29 -0800 | [diff] [blame] | 4519 | should_throttle = 0; |
| 4520 | |
Miao Xie | 27cdeb7 | 2014-04-02 19:51:05 +0800 | [diff] [blame] | 4521 | if (found_extent && |
| 4522 | (test_bit(BTRFS_ROOT_REF_COWS, &root->state) || |
| 4523 | root == root->fs_info->tree_root)) { |
Chris Mason | b947343 | 2009-03-13 11:00:37 -0400 | [diff] [blame] | 4524 | btrfs_set_path_blocking(path); |
Chris Mason | 28ed134 | 2014-12-17 09:41:04 -0800 | [diff] [blame] | 4525 | bytes_deleted += extent_num_bytes; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4526 | ret = btrfs_free_extent(trans, root, extent_start, |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 4527 | extent_num_bytes, 0, |
| 4528 | btrfs_header_owner(leaf), |
Arne Jansen | 66d7e7f | 2011-09-12 15:26:38 +0200 | [diff] [blame] | 4529 | ino, extent_offset, 0); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4530 | BUG_ON(ret); |
Josef Bacik | 1262133 | 2015-02-03 07:50:16 -0800 | [diff] [blame] | 4531 | if (btrfs_should_throttle_delayed_refs(trans, root)) |
Chris Mason | 28ed134 | 2014-12-17 09:41:04 -0800 | [diff] [blame] | 4532 | btrfs_async_run_delayed_refs(root, |
| 4533 | trans->delayed_ref_updates * 2, 0); |
Chris Mason | 28f75a0 | 2015-02-04 06:59:29 -0800 | [diff] [blame] | 4534 | if (be_nice) { |
| 4535 | if (truncate_space_check(trans, root, |
| 4536 | extent_num_bytes)) { |
| 4537 | should_end = 1; |
| 4538 | } |
| 4539 | if (btrfs_should_throttle_delayed_refs(trans, |
| 4540 | root)) { |
| 4541 | should_throttle = 1; |
| 4542 | } |
| 4543 | } |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4544 | } |
Chris Mason | 85e21ba | 2008-01-29 15:11:36 -0500 | [diff] [blame] | 4545 | |
Yan, Zheng | 8082510 | 2009-11-12 09:35:36 +0000 | [diff] [blame] | 4546 | if (found_type == BTRFS_INODE_ITEM_KEY) |
| 4547 | break; |
| 4548 | |
| 4549 | if (path->slots[0] == 0 || |
Josef Bacik | 1262133 | 2015-02-03 07:50:16 -0800 | [diff] [blame] | 4550 | path->slots[0] != pending_del_slot || |
Chris Mason | 28f75a0 | 2015-02-04 06:59:29 -0800 | [diff] [blame] | 4551 | should_throttle || should_end) { |
Yan, Zheng | 8082510 | 2009-11-12 09:35:36 +0000 | [diff] [blame] | 4552 | if (pending_del_nr) { |
| 4553 | ret = btrfs_del_items(trans, root, path, |
| 4554 | pending_del_slot, |
| 4555 | pending_del_nr); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 4556 | if (ret) { |
| 4557 | btrfs_abort_transaction(trans, |
| 4558 | root, ret); |
| 4559 | goto error; |
| 4560 | } |
Yan, Zheng | 8082510 | 2009-11-12 09:35:36 +0000 | [diff] [blame] | 4561 | pending_del_nr = 0; |
| 4562 | } |
David Sterba | b3b4aa7 | 2011-04-21 01:20:15 +0200 | [diff] [blame] | 4563 | btrfs_release_path(path); |
Chris Mason | 28f75a0 | 2015-02-04 06:59:29 -0800 | [diff] [blame] | 4564 | if (should_throttle) { |
Josef Bacik | 1262133 | 2015-02-03 07:50:16 -0800 | [diff] [blame] | 4565 | unsigned long updates = trans->delayed_ref_updates; |
| 4566 | if (updates) { |
| 4567 | trans->delayed_ref_updates = 0; |
| 4568 | ret = btrfs_run_delayed_refs(trans, root, updates * 2); |
| 4569 | if (ret && !err) |
| 4570 | err = ret; |
| 4571 | } |
| 4572 | } |
Chris Mason | 28f75a0 | 2015-02-04 06:59:29 -0800 | [diff] [blame] | 4573 | /* |
| 4574 | * if we failed to refill our space rsv, bail out |
| 4575 | * and let the transaction restart |
| 4576 | */ |
| 4577 | if (should_end) { |
| 4578 | err = -EAGAIN; |
| 4579 | goto error; |
| 4580 | } |
Chris Mason | 85e21ba | 2008-01-29 15:11:36 -0500 | [diff] [blame] | 4581 | goto search_again; |
Yan, Zheng | 8082510 | 2009-11-12 09:35:36 +0000 | [diff] [blame] | 4582 | } else { |
| 4583 | path->slots[0]--; |
Chris Mason | 85e21ba | 2008-01-29 15:11:36 -0500 | [diff] [blame] | 4584 | } |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4585 | } |
Yan, Zheng | 8082510 | 2009-11-12 09:35:36 +0000 | [diff] [blame] | 4586 | out: |
Chris Mason | 85e21ba | 2008-01-29 15:11:36 -0500 | [diff] [blame] | 4587 | if (pending_del_nr) { |
| 4588 | ret = btrfs_del_items(trans, root, path, pending_del_slot, |
| 4589 | pending_del_nr); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 4590 | if (ret) |
| 4591 | btrfs_abort_transaction(trans, root, ret); |
Chris Mason | 85e21ba | 2008-01-29 15:11:36 -0500 | [diff] [blame] | 4592 | } |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 4593 | error: |
Filipe Manana | c1aa457 | 2015-06-20 18:20:09 +0100 | [diff] [blame] | 4594 | if (root->root_key.objectid != BTRFS_TREE_LOG_OBJECTID) |
Josef Bacik | 7f4f6e0 | 2013-08-29 16:43:28 -0400 | [diff] [blame] | 4595 | btrfs_ordered_update_i_size(inode, last_size, NULL); |
Chris Mason | 28ed134 | 2014-12-17 09:41:04 -0800 | [diff] [blame] | 4596 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4597 | btrfs_free_path(path); |
Chris Mason | 28ed134 | 2014-12-17 09:41:04 -0800 | [diff] [blame] | 4598 | |
Chris Mason | 28f75a0 | 2015-02-04 06:59:29 -0800 | [diff] [blame] | 4599 | if (be_nice && bytes_deleted > 32 * 1024 * 1024) { |
Chris Mason | 28ed134 | 2014-12-17 09:41:04 -0800 | [diff] [blame] | 4600 | unsigned long updates = trans->delayed_ref_updates; |
| 4601 | if (updates) { |
| 4602 | trans->delayed_ref_updates = 0; |
| 4603 | ret = btrfs_run_delayed_refs(trans, root, updates * 2); |
| 4604 | if (ret && !err) |
| 4605 | err = ret; |
| 4606 | } |
| 4607 | } |
Yan, Zheng | 8082510 | 2009-11-12 09:35:36 +0000 | [diff] [blame] | 4608 | return err; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4609 | } |
| 4610 | |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 4611 | /* |
Josef Bacik | 2aaa665 | 2012-08-29 14:27:18 -0400 | [diff] [blame] | 4612 | * btrfs_truncate_page - read, zero a chunk and write a page |
| 4613 | * @inode - inode that we're zeroing |
| 4614 | * @from - the offset to start zeroing |
| 4615 | * @len - the length to zero, 0 to zero the entire range respective to the |
| 4616 | * offset |
| 4617 | * @front - zero up to the offset instead of from the offset on |
| 4618 | * |
| 4619 | * This will find the page for the "from" offset and cow the page and zero the |
| 4620 | * 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] | 4621 | */ |
Josef Bacik | 2aaa665 | 2012-08-29 14:27:18 -0400 | [diff] [blame] | 4622 | int btrfs_truncate_page(struct inode *inode, loff_t from, loff_t len, |
| 4623 | int front) |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 4624 | { |
Josef Bacik | 2aaa665 | 2012-08-29 14:27:18 -0400 | [diff] [blame] | 4625 | struct address_space *mapping = inode->i_mapping; |
Chris Mason | db94535 | 2007-10-15 16:15:53 -0400 | [diff] [blame] | 4626 | struct btrfs_root *root = BTRFS_I(inode)->root; |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 4627 | struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree; |
| 4628 | struct btrfs_ordered_extent *ordered; |
Josef Bacik | 2ac55d4 | 2010-02-03 19:33:23 +0000 | [diff] [blame] | 4629 | struct extent_state *cached_state = NULL; |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 4630 | char *kaddr; |
Chris Mason | db94535 | 2007-10-15 16:15:53 -0400 | [diff] [blame] | 4631 | u32 blocksize = root->sectorsize; |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 4632 | pgoff_t index = from >> PAGE_CACHE_SHIFT; |
| 4633 | unsigned offset = from & (PAGE_CACHE_SIZE-1); |
| 4634 | struct page *page; |
Josef Bacik | 3b16a4e | 2011-09-21 15:05:58 -0400 | [diff] [blame] | 4635 | gfp_t mask = btrfs_alloc_write_mask(mapping); |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 4636 | int ret = 0; |
| 4637 | u64 page_start; |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 4638 | u64 page_end; |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 4639 | |
Josef Bacik | 2aaa665 | 2012-08-29 14:27:18 -0400 | [diff] [blame] | 4640 | if ((offset & (blocksize - 1)) == 0 && |
| 4641 | (!len || ((len & (blocksize - 1)) == 0))) |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 4642 | goto out; |
Qu Wenruo | df48063 | 2015-09-08 17:25:54 +0800 | [diff] [blame^] | 4643 | ret = __btrfs_delalloc_reserve_space(inode, |
| 4644 | round_down(from, PAGE_CACHE_SIZE), PAGE_CACHE_SIZE); |
Josef Bacik | 5d5e103 | 2009-10-13 16:46:49 -0400 | [diff] [blame] | 4645 | if (ret) |
| 4646 | goto out; |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 4647 | |
Chris Mason | 211c17f | 2008-05-15 09:13:45 -0400 | [diff] [blame] | 4648 | again: |
Josef Bacik | 3b16a4e | 2011-09-21 15:05:58 -0400 | [diff] [blame] | 4649 | page = find_or_create_page(mapping, index, mask); |
Josef Bacik | 5d5e103 | 2009-10-13 16:46:49 -0400 | [diff] [blame] | 4650 | if (!page) { |
Qu Wenruo | df48063 | 2015-09-08 17:25:54 +0800 | [diff] [blame^] | 4651 | __btrfs_delalloc_release_space(inode, |
| 4652 | round_down(from, PAGE_CACHE_SIZE), |
| 4653 | PAGE_CACHE_SIZE); |
Miao Xie | ac6a2b3 | 2012-12-05 10:56:13 +0000 | [diff] [blame] | 4654 | ret = -ENOMEM; |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 4655 | goto out; |
Josef Bacik | 5d5e103 | 2009-10-13 16:46:49 -0400 | [diff] [blame] | 4656 | } |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 4657 | |
| 4658 | page_start = page_offset(page); |
| 4659 | page_end = page_start + PAGE_CACHE_SIZE - 1; |
| 4660 | |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 4661 | if (!PageUptodate(page)) { |
| 4662 | ret = btrfs_readpage(NULL, page); |
| 4663 | lock_page(page); |
Chris Mason | 211c17f | 2008-05-15 09:13:45 -0400 | [diff] [blame] | 4664 | if (page->mapping != mapping) { |
| 4665 | unlock_page(page); |
| 4666 | page_cache_release(page); |
| 4667 | goto again; |
| 4668 | } |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 4669 | if (!PageUptodate(page)) { |
| 4670 | ret = -EIO; |
Chris Mason | 8964222 | 2008-07-24 09:41:53 -0400 | [diff] [blame] | 4671 | goto out_unlock; |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 4672 | } |
| 4673 | } |
Chris Mason | 211c17f | 2008-05-15 09:13:45 -0400 | [diff] [blame] | 4674 | wait_on_page_writeback(page); |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 4675 | |
Jeff Mahoney | d008237 | 2012-03-01 14:57:19 +0100 | [diff] [blame] | 4676 | lock_extent_bits(io_tree, page_start, page_end, 0, &cached_state); |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 4677 | set_page_extent_mapped(page); |
| 4678 | |
| 4679 | ordered = btrfs_lookup_ordered_extent(inode, page_start); |
| 4680 | if (ordered) { |
Josef Bacik | 2ac55d4 | 2010-02-03 19:33:23 +0000 | [diff] [blame] | 4681 | unlock_extent_cached(io_tree, page_start, page_end, |
| 4682 | &cached_state, GFP_NOFS); |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 4683 | unlock_page(page); |
| 4684 | page_cache_release(page); |
Chris Mason | eb84ae0 | 2008-07-17 13:53:27 -0400 | [diff] [blame] | 4685 | btrfs_start_ordered_extent(inode, ordered, 1); |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 4686 | btrfs_put_ordered_extent(ordered); |
| 4687 | goto again; |
| 4688 | } |
| 4689 | |
Josef Bacik | 2ac55d4 | 2010-02-03 19:33:23 +0000 | [diff] [blame] | 4690 | clear_extent_bit(&BTRFS_I(inode)->io_tree, page_start, page_end, |
Liu Bo | 9e8a4a8 | 2012-09-05 19:10:51 -0600 | [diff] [blame] | 4691 | EXTENT_DIRTY | EXTENT_DELALLOC | |
| 4692 | EXTENT_DO_ACCOUNTING | EXTENT_DEFRAG, |
Josef Bacik | 2ac55d4 | 2010-02-03 19:33:23 +0000 | [diff] [blame] | 4693 | 0, 0, &cached_state, GFP_NOFS); |
Josef Bacik | 5d5e103 | 2009-10-13 16:46:49 -0400 | [diff] [blame] | 4694 | |
Josef Bacik | 2ac55d4 | 2010-02-03 19:33:23 +0000 | [diff] [blame] | 4695 | ret = btrfs_set_extent_delalloc(inode, page_start, page_end, |
| 4696 | &cached_state); |
Josef Bacik | 9ed74f2 | 2009-09-11 16:12:44 -0400 | [diff] [blame] | 4697 | if (ret) { |
Josef Bacik | 2ac55d4 | 2010-02-03 19:33:23 +0000 | [diff] [blame] | 4698 | unlock_extent_cached(io_tree, page_start, page_end, |
| 4699 | &cached_state, GFP_NOFS); |
Josef Bacik | 9ed74f2 | 2009-09-11 16:12:44 -0400 | [diff] [blame] | 4700 | goto out_unlock; |
| 4701 | } |
| 4702 | |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 4703 | if (offset != PAGE_CACHE_SIZE) { |
Josef Bacik | 2aaa665 | 2012-08-29 14:27:18 -0400 | [diff] [blame] | 4704 | if (!len) |
| 4705 | len = PAGE_CACHE_SIZE - offset; |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 4706 | kaddr = kmap(page); |
Josef Bacik | 2aaa665 | 2012-08-29 14:27:18 -0400 | [diff] [blame] | 4707 | if (front) |
| 4708 | memset(kaddr, 0, offset); |
| 4709 | else |
| 4710 | memset(kaddr + offset, 0, len); |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 4711 | flush_dcache_page(page); |
| 4712 | kunmap(page); |
| 4713 | } |
Chris Mason | 247e743 | 2008-07-17 12:53:51 -0400 | [diff] [blame] | 4714 | ClearPageChecked(page); |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 4715 | set_page_dirty(page); |
Josef Bacik | 2ac55d4 | 2010-02-03 19:33:23 +0000 | [diff] [blame] | 4716 | unlock_extent_cached(io_tree, page_start, page_end, &cached_state, |
| 4717 | GFP_NOFS); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4718 | |
Chris Mason | 8964222 | 2008-07-24 09:41:53 -0400 | [diff] [blame] | 4719 | out_unlock: |
Josef Bacik | 5d5e103 | 2009-10-13 16:46:49 -0400 | [diff] [blame] | 4720 | if (ret) |
Qu Wenruo | df48063 | 2015-09-08 17:25:54 +0800 | [diff] [blame^] | 4721 | __btrfs_delalloc_release_space(inode, page_start, |
| 4722 | PAGE_CACHE_SIZE); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4723 | unlock_page(page); |
| 4724 | page_cache_release(page); |
| 4725 | out: |
| 4726 | return ret; |
| 4727 | } |
| 4728 | |
Josef Bacik | 16e7549 | 2013-10-22 12:18:51 -0400 | [diff] [blame] | 4729 | static int maybe_insert_hole(struct btrfs_root *root, struct inode *inode, |
| 4730 | u64 offset, u64 len) |
| 4731 | { |
| 4732 | struct btrfs_trans_handle *trans; |
| 4733 | int ret; |
| 4734 | |
| 4735 | /* |
| 4736 | * Still need to make sure the inode looks like it's been updated so |
| 4737 | * that any holes get logged if we fsync. |
| 4738 | */ |
| 4739 | if (btrfs_fs_incompat(root->fs_info, NO_HOLES)) { |
| 4740 | BTRFS_I(inode)->last_trans = root->fs_info->generation; |
| 4741 | BTRFS_I(inode)->last_sub_trans = root->log_transid; |
| 4742 | BTRFS_I(inode)->last_log_commit = root->last_log_commit; |
| 4743 | return 0; |
| 4744 | } |
| 4745 | |
| 4746 | /* |
| 4747 | * 1 - for the one we're dropping |
| 4748 | * 1 - for the one we're adding |
| 4749 | * 1 - for updating the inode. |
| 4750 | */ |
| 4751 | trans = btrfs_start_transaction(root, 3); |
| 4752 | if (IS_ERR(trans)) |
| 4753 | return PTR_ERR(trans); |
| 4754 | |
| 4755 | ret = btrfs_drop_extents(trans, root, inode, offset, offset + len, 1); |
| 4756 | if (ret) { |
| 4757 | btrfs_abort_transaction(trans, root, ret); |
| 4758 | btrfs_end_transaction(trans, root); |
| 4759 | return ret; |
| 4760 | } |
| 4761 | |
| 4762 | ret = btrfs_insert_file_extent(trans, root, btrfs_ino(inode), offset, |
| 4763 | 0, 0, len, 0, len, 0, 0, 0); |
| 4764 | if (ret) |
| 4765 | btrfs_abort_transaction(trans, root, ret); |
| 4766 | else |
| 4767 | btrfs_update_inode(trans, root, inode); |
| 4768 | btrfs_end_transaction(trans, root); |
| 4769 | return ret; |
| 4770 | } |
| 4771 | |
Josef Bacik | 695a0d0 | 2011-03-04 15:46:53 -0500 | [diff] [blame] | 4772 | /* |
| 4773 | * This function puts in dummy file extents for the area we're creating a hole |
| 4774 | * for. So if we are truncating this file to a larger size we need to insert |
| 4775 | * these file extents so that btrfs_get_extent will return a EXTENT_MAP_HOLE for |
| 4776 | * the range between oldsize and size |
| 4777 | */ |
Josef Bacik | a41ad39 | 2011-01-31 15:30:16 -0500 | [diff] [blame] | 4778 | 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] | 4779 | { |
Yan Zheng | 9036c10 | 2008-10-30 14:19:41 -0400 | [diff] [blame] | 4780 | struct btrfs_root *root = BTRFS_I(inode)->root; |
| 4781 | struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree; |
Yan, Zheng | a22285a | 2010-05-16 10:48:46 -0400 | [diff] [blame] | 4782 | struct extent_map *em = NULL; |
Josef Bacik | 2ac55d4 | 2010-02-03 19:33:23 +0000 | [diff] [blame] | 4783 | struct extent_state *cached_state = NULL; |
Josef Bacik | 5dc562c | 2012-08-17 13:14:17 -0400 | [diff] [blame] | 4784 | struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree; |
Qu Wenruo | fda2832 | 2013-02-26 08:10:22 +0000 | [diff] [blame] | 4785 | u64 hole_start = ALIGN(oldsize, root->sectorsize); |
| 4786 | u64 block_end = ALIGN(size, root->sectorsize); |
Yan Zheng | 9036c10 | 2008-10-30 14:19:41 -0400 | [diff] [blame] | 4787 | u64 last_byte; |
| 4788 | u64 cur_offset; |
| 4789 | u64 hole_size; |
Josef Bacik | 9ed74f2 | 2009-09-11 16:12:44 -0400 | [diff] [blame] | 4790 | int err = 0; |
Yan Zheng | 9036c10 | 2008-10-30 14:19:41 -0400 | [diff] [blame] | 4791 | |
Josef Bacik | a71754f | 2013-06-17 17:14:39 -0400 | [diff] [blame] | 4792 | /* |
| 4793 | * If our size started in the middle of a page we need to zero out the |
| 4794 | * rest of the page before we expand the i_size, otherwise we could |
| 4795 | * expose stale data. |
| 4796 | */ |
| 4797 | err = btrfs_truncate_page(inode, oldsize, 0, 0); |
| 4798 | if (err) |
| 4799 | return err; |
| 4800 | |
Yan Zheng | 9036c10 | 2008-10-30 14:19:41 -0400 | [diff] [blame] | 4801 | if (size <= hole_start) |
| 4802 | return 0; |
| 4803 | |
Yan Zheng | 9036c10 | 2008-10-30 14:19:41 -0400 | [diff] [blame] | 4804 | while (1) { |
| 4805 | struct btrfs_ordered_extent *ordered; |
Miao Xie | fa7c149 | 2013-09-26 13:15:27 +0800 | [diff] [blame] | 4806 | |
Josef Bacik | 2ac55d4 | 2010-02-03 19:33:23 +0000 | [diff] [blame] | 4807 | lock_extent_bits(io_tree, hole_start, block_end - 1, 0, |
Jeff Mahoney | d008237 | 2012-03-01 14:57:19 +0100 | [diff] [blame] | 4808 | &cached_state); |
Miao Xie | fa7c149 | 2013-09-26 13:15:27 +0800 | [diff] [blame] | 4809 | ordered = btrfs_lookup_ordered_range(inode, hole_start, |
| 4810 | block_end - hole_start); |
Yan Zheng | 9036c10 | 2008-10-30 14:19:41 -0400 | [diff] [blame] | 4811 | if (!ordered) |
| 4812 | break; |
Josef Bacik | 2ac55d4 | 2010-02-03 19:33:23 +0000 | [diff] [blame] | 4813 | unlock_extent_cached(io_tree, hole_start, block_end - 1, |
| 4814 | &cached_state, GFP_NOFS); |
Miao Xie | fa7c149 | 2013-09-26 13:15:27 +0800 | [diff] [blame] | 4815 | btrfs_start_ordered_extent(inode, ordered, 1); |
Yan Zheng | 9036c10 | 2008-10-30 14:19:41 -0400 | [diff] [blame] | 4816 | btrfs_put_ordered_extent(ordered); |
| 4817 | } |
| 4818 | |
Yan Zheng | 9036c10 | 2008-10-30 14:19:41 -0400 | [diff] [blame] | 4819 | cur_offset = hole_start; |
| 4820 | while (1) { |
| 4821 | em = btrfs_get_extent(inode, NULL, 0, cur_offset, |
| 4822 | block_end - cur_offset, 0); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 4823 | if (IS_ERR(em)) { |
| 4824 | err = PTR_ERR(em); |
Zach Brown | f276795 | 2013-01-08 19:37:58 +0000 | [diff] [blame] | 4825 | em = NULL; |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 4826 | break; |
| 4827 | } |
Yan Zheng | 9036c10 | 2008-10-30 14:19:41 -0400 | [diff] [blame] | 4828 | last_byte = min(extent_map_end(em), block_end); |
Qu Wenruo | fda2832 | 2013-02-26 08:10:22 +0000 | [diff] [blame] | 4829 | last_byte = ALIGN(last_byte , root->sectorsize); |
Yan, Zheng | 8082510 | 2009-11-12 09:35:36 +0000 | [diff] [blame] | 4830 | if (!test_bit(EXTENT_FLAG_PREALLOC, &em->flags)) { |
Josef Bacik | 5dc562c | 2012-08-17 13:14:17 -0400 | [diff] [blame] | 4831 | struct extent_map *hole_em; |
Yan Zheng | 9036c10 | 2008-10-30 14:19:41 -0400 | [diff] [blame] | 4832 | hole_size = last_byte - cur_offset; |
Yan, Zheng | 8082510 | 2009-11-12 09:35:36 +0000 | [diff] [blame] | 4833 | |
Josef Bacik | 16e7549 | 2013-10-22 12:18:51 -0400 | [diff] [blame] | 4834 | err = maybe_insert_hole(root, inode, cur_offset, |
| 4835 | hole_size); |
| 4836 | if (err) |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 4837 | break; |
Josef Bacik | 5dc562c | 2012-08-17 13:14:17 -0400 | [diff] [blame] | 4838 | btrfs_drop_extent_cache(inode, cur_offset, |
| 4839 | cur_offset + hole_size - 1, 0); |
| 4840 | hole_em = alloc_extent_map(); |
| 4841 | if (!hole_em) { |
| 4842 | set_bit(BTRFS_INODE_NEEDS_FULL_SYNC, |
| 4843 | &BTRFS_I(inode)->runtime_flags); |
| 4844 | goto next; |
| 4845 | } |
| 4846 | hole_em->start = cur_offset; |
| 4847 | hole_em->len = hole_size; |
| 4848 | hole_em->orig_start = cur_offset; |
Yan, Zheng | 8082510 | 2009-11-12 09:35:36 +0000 | [diff] [blame] | 4849 | |
Josef Bacik | 5dc562c | 2012-08-17 13:14:17 -0400 | [diff] [blame] | 4850 | hole_em->block_start = EXTENT_MAP_HOLE; |
| 4851 | hole_em->block_len = 0; |
Josef Bacik | b493968 | 2012-12-03 10:31:19 -0500 | [diff] [blame] | 4852 | hole_em->orig_block_len = 0; |
Josef Bacik | cc95bef | 2013-04-04 14:31:27 -0400 | [diff] [blame] | 4853 | hole_em->ram_bytes = hole_size; |
Josef Bacik | 5dc562c | 2012-08-17 13:14:17 -0400 | [diff] [blame] | 4854 | hole_em->bdev = root->fs_info->fs_devices->latest_bdev; |
| 4855 | hole_em->compress_type = BTRFS_COMPRESS_NONE; |
Josef Bacik | 16e7549 | 2013-10-22 12:18:51 -0400 | [diff] [blame] | 4856 | hole_em->generation = root->fs_info->generation; |
Josef Bacik | 5dc562c | 2012-08-17 13:14:17 -0400 | [diff] [blame] | 4857 | |
| 4858 | while (1) { |
| 4859 | write_lock(&em_tree->lock); |
Josef Bacik | 09a2a8f9 | 2013-04-05 16:51:15 -0400 | [diff] [blame] | 4860 | err = add_extent_mapping(em_tree, hole_em, 1); |
Josef Bacik | 5dc562c | 2012-08-17 13:14:17 -0400 | [diff] [blame] | 4861 | write_unlock(&em_tree->lock); |
| 4862 | if (err != -EEXIST) |
| 4863 | break; |
| 4864 | btrfs_drop_extent_cache(inode, cur_offset, |
| 4865 | cur_offset + |
| 4866 | hole_size - 1, 0); |
| 4867 | } |
| 4868 | free_extent_map(hole_em); |
Yan Zheng | 9036c10 | 2008-10-30 14:19:41 -0400 | [diff] [blame] | 4869 | } |
Josef Bacik | 16e7549 | 2013-10-22 12:18:51 -0400 | [diff] [blame] | 4870 | next: |
Yan Zheng | 9036c10 | 2008-10-30 14:19:41 -0400 | [diff] [blame] | 4871 | free_extent_map(em); |
Yan, Zheng | a22285a | 2010-05-16 10:48:46 -0400 | [diff] [blame] | 4872 | em = NULL; |
Yan Zheng | 9036c10 | 2008-10-30 14:19:41 -0400 | [diff] [blame] | 4873 | cur_offset = last_byte; |
Yan, Zheng | 8082510 | 2009-11-12 09:35:36 +0000 | [diff] [blame] | 4874 | if (cur_offset >= block_end) |
Yan Zheng | 9036c10 | 2008-10-30 14:19:41 -0400 | [diff] [blame] | 4875 | break; |
| 4876 | } |
Yan, Zheng | a22285a | 2010-05-16 10:48:46 -0400 | [diff] [blame] | 4877 | free_extent_map(em); |
Josef Bacik | 2ac55d4 | 2010-02-03 19:33:23 +0000 | [diff] [blame] | 4878 | unlock_extent_cached(io_tree, hole_start, block_end - 1, &cached_state, |
| 4879 | GFP_NOFS); |
Yan Zheng | 9036c10 | 2008-10-30 14:19:41 -0400 | [diff] [blame] | 4880 | return err; |
| 4881 | } |
| 4882 | |
Filipe Manana | 9ea24bb | 2014-10-29 11:57:59 +0000 | [diff] [blame] | 4883 | static int wait_snapshoting_atomic_t(atomic_t *a) |
| 4884 | { |
| 4885 | schedule(); |
| 4886 | return 0; |
| 4887 | } |
| 4888 | |
| 4889 | static void wait_for_snapshot_creation(struct btrfs_root *root) |
| 4890 | { |
| 4891 | while (true) { |
| 4892 | int ret; |
| 4893 | |
| 4894 | ret = btrfs_start_write_no_snapshoting(root); |
| 4895 | if (ret) |
| 4896 | break; |
| 4897 | wait_on_atomic_t(&root->will_be_snapshoted, |
| 4898 | wait_snapshoting_atomic_t, |
| 4899 | TASK_UNINTERRUPTIBLE); |
| 4900 | } |
| 4901 | } |
| 4902 | |
Eric Sandeen | 3972f26 | 2013-01-12 02:57:22 +0000 | [diff] [blame] | 4903 | static int btrfs_setsize(struct inode *inode, struct iattr *attr) |
Yan, Zheng | 8082510 | 2009-11-12 09:35:36 +0000 | [diff] [blame] | 4904 | { |
Miao Xie | f4a2f4c | 2011-12-14 20:12:01 -0500 | [diff] [blame] | 4905 | struct btrfs_root *root = BTRFS_I(inode)->root; |
| 4906 | struct btrfs_trans_handle *trans; |
Josef Bacik | a41ad39 | 2011-01-31 15:30:16 -0500 | [diff] [blame] | 4907 | loff_t oldsize = i_size_read(inode); |
Eric Sandeen | 3972f26 | 2013-01-12 02:57:22 +0000 | [diff] [blame] | 4908 | loff_t newsize = attr->ia_size; |
| 4909 | int mask = attr->ia_valid; |
Yan, Zheng | 8082510 | 2009-11-12 09:35:36 +0000 | [diff] [blame] | 4910 | int ret; |
| 4911 | |
Eric Sandeen | 3972f26 | 2013-01-12 02:57:22 +0000 | [diff] [blame] | 4912 | /* |
| 4913 | * The regular truncate() case without ATTR_CTIME and ATTR_MTIME is a |
| 4914 | * special case where we need to update the times despite not having |
| 4915 | * these flags set. For all other operations the VFS set these flags |
| 4916 | * explicitly if it wants a timestamp update. |
| 4917 | */ |
Christoph Hellwig | dff6efc | 2013-11-19 07:17:07 -0800 | [diff] [blame] | 4918 | if (newsize != oldsize) { |
| 4919 | inode_inc_iversion(inode); |
| 4920 | if (!(mask & (ATTR_CTIME | ATTR_MTIME))) |
| 4921 | inode->i_ctime = inode->i_mtime = |
| 4922 | current_fs_time(inode->i_sb); |
| 4923 | } |
Eric Sandeen | 3972f26 | 2013-01-12 02:57:22 +0000 | [diff] [blame] | 4924 | |
Josef Bacik | a41ad39 | 2011-01-31 15:30:16 -0500 | [diff] [blame] | 4925 | if (newsize > oldsize) { |
Kirill A. Shutemov | 7caef26 | 2013-09-12 15:13:56 -0700 | [diff] [blame] | 4926 | truncate_pagecache(inode, newsize); |
Filipe Manana | 9ea24bb | 2014-10-29 11:57:59 +0000 | [diff] [blame] | 4927 | /* |
| 4928 | * Don't do an expanding truncate while snapshoting is ongoing. |
| 4929 | * This is to ensure the snapshot captures a fully consistent |
| 4930 | * state of this file - if the snapshot captures this expanding |
| 4931 | * truncation, it must capture all writes that happened before |
| 4932 | * this truncation. |
| 4933 | */ |
| 4934 | wait_for_snapshot_creation(root); |
Josef Bacik | a41ad39 | 2011-01-31 15:30:16 -0500 | [diff] [blame] | 4935 | ret = btrfs_cont_expand(inode, oldsize, newsize); |
Filipe Manana | 9ea24bb | 2014-10-29 11:57:59 +0000 | [diff] [blame] | 4936 | if (ret) { |
| 4937 | btrfs_end_write_no_snapshoting(root); |
Yan, Zheng | 8082510 | 2009-11-12 09:35:36 +0000 | [diff] [blame] | 4938 | return ret; |
Filipe Manana | 9ea24bb | 2014-10-29 11:57:59 +0000 | [diff] [blame] | 4939 | } |
Yan, Zheng | 8082510 | 2009-11-12 09:35:36 +0000 | [diff] [blame] | 4940 | |
Miao Xie | f4a2f4c | 2011-12-14 20:12:01 -0500 | [diff] [blame] | 4941 | trans = btrfs_start_transaction(root, 1); |
Filipe Manana | 9ea24bb | 2014-10-29 11:57:59 +0000 | [diff] [blame] | 4942 | if (IS_ERR(trans)) { |
| 4943 | btrfs_end_write_no_snapshoting(root); |
Miao Xie | f4a2f4c | 2011-12-14 20:12:01 -0500 | [diff] [blame] | 4944 | return PTR_ERR(trans); |
Filipe Manana | 9ea24bb | 2014-10-29 11:57:59 +0000 | [diff] [blame] | 4945 | } |
Miao Xie | f4a2f4c | 2011-12-14 20:12:01 -0500 | [diff] [blame] | 4946 | |
| 4947 | i_size_write(inode, newsize); |
| 4948 | btrfs_ordered_update_i_size(inode, i_size_read(inode), NULL); |
| 4949 | ret = btrfs_update_inode(trans, root, inode); |
Filipe Manana | 9ea24bb | 2014-10-29 11:57:59 +0000 | [diff] [blame] | 4950 | btrfs_end_write_no_snapshoting(root); |
Josef Bacik | 7ad85bb | 2012-01-12 19:10:12 -0500 | [diff] [blame] | 4951 | btrfs_end_transaction(trans, root); |
Josef Bacik | a41ad39 | 2011-01-31 15:30:16 -0500 | [diff] [blame] | 4952 | } else { |
Yan, Zheng | 8082510 | 2009-11-12 09:35:36 +0000 | [diff] [blame] | 4953 | |
Josef Bacik | a41ad39 | 2011-01-31 15:30:16 -0500 | [diff] [blame] | 4954 | /* |
| 4955 | * We're truncating a file that used to have good data down to |
| 4956 | * zero. Make sure it gets into the ordered flush list so that |
| 4957 | * any new writes get down to disk quickly. |
| 4958 | */ |
| 4959 | if (newsize == 0) |
Josef Bacik | 72ac3c0 | 2012-05-23 14:13:11 -0400 | [diff] [blame] | 4960 | set_bit(BTRFS_INODE_ORDERED_DATA_CLOSE, |
| 4961 | &BTRFS_I(inode)->runtime_flags); |
Yan, Zheng | 8082510 | 2009-11-12 09:35:36 +0000 | [diff] [blame] | 4962 | |
Josef Bacik | f3fe820 | 2013-01-07 17:03:21 -0500 | [diff] [blame] | 4963 | /* |
| 4964 | * 1 for the orphan item we're going to add |
| 4965 | * 1 for the orphan item deletion. |
| 4966 | */ |
| 4967 | trans = btrfs_start_transaction(root, 2); |
| 4968 | if (IS_ERR(trans)) |
| 4969 | return PTR_ERR(trans); |
| 4970 | |
| 4971 | /* |
| 4972 | * We need to do this in case we fail at _any_ point during the |
| 4973 | * actual truncate. Once we do the truncate_setsize we could |
| 4974 | * invalidate pages which forces any outstanding ordered io to |
| 4975 | * be instantly completed which will give us extents that need |
| 4976 | * to be truncated. If we fail to get an orphan inode down we |
| 4977 | * could have left over extents that were never meant to live, |
| 4978 | * so we need to garuntee from this point on that everything |
| 4979 | * will be consistent. |
| 4980 | */ |
| 4981 | ret = btrfs_orphan_add(trans, inode); |
| 4982 | btrfs_end_transaction(trans, root); |
| 4983 | if (ret) |
| 4984 | return ret; |
| 4985 | |
Josef Bacik | a41ad39 | 2011-01-31 15:30:16 -0500 | [diff] [blame] | 4986 | /* we don't support swapfiles, so vmtruncate shouldn't fail */ |
| 4987 | truncate_setsize(inode, newsize); |
Miao Xie | 2e60a51 | 2013-02-08 07:01:08 +0000 | [diff] [blame] | 4988 | |
| 4989 | /* Disable nonlocked read DIO to avoid the end less truncate */ |
| 4990 | btrfs_inode_block_unlocked_dio(inode); |
| 4991 | inode_dio_wait(inode); |
| 4992 | btrfs_inode_resume_unlocked_dio(inode); |
| 4993 | |
Josef Bacik | a41ad39 | 2011-01-31 15:30:16 -0500 | [diff] [blame] | 4994 | ret = btrfs_truncate(inode); |
Josef Bacik | 7f4f6e0 | 2013-08-29 16:43:28 -0400 | [diff] [blame] | 4995 | if (ret && inode->i_nlink) { |
| 4996 | int err; |
| 4997 | |
| 4998 | /* |
| 4999 | * failed to truncate, disk_i_size is only adjusted down |
| 5000 | * as we remove extents, so it should represent the true |
| 5001 | * size of the inode, so reset the in memory size and |
| 5002 | * delete our orphan entry. |
| 5003 | */ |
| 5004 | trans = btrfs_join_transaction(root); |
| 5005 | if (IS_ERR(trans)) { |
| 5006 | btrfs_orphan_del(NULL, inode); |
| 5007 | return ret; |
| 5008 | } |
| 5009 | i_size_write(inode, BTRFS_I(inode)->disk_i_size); |
| 5010 | err = btrfs_orphan_del(trans, inode); |
| 5011 | if (err) |
| 5012 | btrfs_abort_transaction(trans, root, err); |
| 5013 | btrfs_end_transaction(trans, root); |
| 5014 | } |
Yan, Zheng | 8082510 | 2009-11-12 09:35:36 +0000 | [diff] [blame] | 5015 | } |
| 5016 | |
Josef Bacik | a41ad39 | 2011-01-31 15:30:16 -0500 | [diff] [blame] | 5017 | return ret; |
Yan, Zheng | 8082510 | 2009-11-12 09:35:36 +0000 | [diff] [blame] | 5018 | } |
| 5019 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5020 | static int btrfs_setattr(struct dentry *dentry, struct iattr *attr) |
| 5021 | { |
David Howells | 2b0143b | 2015-03-17 22:25:59 +0000 | [diff] [blame] | 5022 | struct inode *inode = d_inode(dentry); |
Li Zefan | b83cc96 | 2010-12-20 16:04:08 +0800 | [diff] [blame] | 5023 | struct btrfs_root *root = BTRFS_I(inode)->root; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5024 | int err; |
| 5025 | |
Li Zefan | b83cc96 | 2010-12-20 16:04:08 +0800 | [diff] [blame] | 5026 | if (btrfs_root_readonly(root)) |
| 5027 | return -EROFS; |
| 5028 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5029 | err = inode_change_ok(inode, attr); |
| 5030 | if (err) |
| 5031 | return err; |
| 5032 | |
Chris Mason | 5a3f23d | 2009-03-31 13:27:11 -0400 | [diff] [blame] | 5033 | if (S_ISREG(inode->i_mode) && (attr->ia_valid & ATTR_SIZE)) { |
Eric Sandeen | 3972f26 | 2013-01-12 02:57:22 +0000 | [diff] [blame] | 5034 | err = btrfs_setsize(inode, attr); |
Yan, Zheng | 8082510 | 2009-11-12 09:35:36 +0000 | [diff] [blame] | 5035 | if (err) |
| 5036 | return err; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5037 | } |
Yan Zheng | 9036c10 | 2008-10-30 14:19:41 -0400 | [diff] [blame] | 5038 | |
Christoph Hellwig | 1025774 | 2010-06-04 11:30:02 +0200 | [diff] [blame] | 5039 | if (attr->ia_valid) { |
| 5040 | setattr_copy(inode, attr); |
Josef Bacik | 0c4d2d9 | 2012-04-05 15:03:02 -0400 | [diff] [blame] | 5041 | inode_inc_iversion(inode); |
Josef Bacik | 22c44fe | 2011-11-30 10:45:38 -0500 | [diff] [blame] | 5042 | err = btrfs_dirty_inode(inode); |
Josef Bacik | 33268ea | 2008-07-24 12:16:36 -0400 | [diff] [blame] | 5043 | |
Josef Bacik | 22c44fe | 2011-11-30 10:45:38 -0500 | [diff] [blame] | 5044 | if (!err && attr->ia_valid & ATTR_MODE) |
Christoph Hellwig | 996a710 | 2013-12-20 05:16:43 -0800 | [diff] [blame] | 5045 | err = posix_acl_chmod(inode, inode->i_mode); |
Christoph Hellwig | 1025774 | 2010-06-04 11:30:02 +0200 | [diff] [blame] | 5046 | } |
| 5047 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5048 | return err; |
| 5049 | } |
Chris Mason | 61295eb | 2008-01-14 16:24:38 -0500 | [diff] [blame] | 5050 | |
Filipe David Borba Manana | 131e404 | 2013-11-19 22:29:35 +0000 | [diff] [blame] | 5051 | /* |
| 5052 | * While truncating the inode pages during eviction, we get the VFS calling |
| 5053 | * btrfs_invalidatepage() against each page of the inode. This is slow because |
| 5054 | * the calls to btrfs_invalidatepage() result in a huge amount of calls to |
| 5055 | * lock_extent_bits() and clear_extent_bit(), which keep merging and splitting |
| 5056 | * extent_state structures over and over, wasting lots of time. |
| 5057 | * |
| 5058 | * Therefore if the inode is being evicted, let btrfs_invalidatepage() skip all |
| 5059 | * those expensive operations on a per page basis and do only the ordered io |
| 5060 | * finishing, while we release here the extent_map and extent_state structures, |
| 5061 | * without the excessive merging and splitting. |
| 5062 | */ |
| 5063 | static void evict_inode_truncate_pages(struct inode *inode) |
| 5064 | { |
| 5065 | struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree; |
| 5066 | struct extent_map_tree *map_tree = &BTRFS_I(inode)->extent_tree; |
| 5067 | struct rb_node *node; |
| 5068 | |
| 5069 | ASSERT(inode->i_state & I_FREEING); |
Johannes Weiner | 91b0abe | 2014-04-03 14:47:49 -0700 | [diff] [blame] | 5070 | truncate_inode_pages_final(&inode->i_data); |
Filipe David Borba Manana | 131e404 | 2013-11-19 22:29:35 +0000 | [diff] [blame] | 5071 | |
| 5072 | write_lock(&map_tree->lock); |
| 5073 | while (!RB_EMPTY_ROOT(&map_tree->map)) { |
| 5074 | struct extent_map *em; |
| 5075 | |
| 5076 | node = rb_first(&map_tree->map); |
| 5077 | em = rb_entry(node, struct extent_map, rb_node); |
Wang Shilong | 180589e | 2013-12-14 15:27:31 +0800 | [diff] [blame] | 5078 | clear_bit(EXTENT_FLAG_PINNED, &em->flags); |
| 5079 | clear_bit(EXTENT_FLAG_LOGGING, &em->flags); |
Filipe David Borba Manana | 131e404 | 2013-11-19 22:29:35 +0000 | [diff] [blame] | 5080 | remove_extent_mapping(map_tree, em); |
| 5081 | free_extent_map(em); |
Filipe Manana | 7064dd5 | 2014-08-08 02:47:05 +0100 | [diff] [blame] | 5082 | if (need_resched()) { |
| 5083 | write_unlock(&map_tree->lock); |
| 5084 | cond_resched(); |
| 5085 | write_lock(&map_tree->lock); |
| 5086 | } |
Filipe David Borba Manana | 131e404 | 2013-11-19 22:29:35 +0000 | [diff] [blame] | 5087 | } |
| 5088 | write_unlock(&map_tree->lock); |
| 5089 | |
Filipe Manana | 6ca0709 | 2015-05-26 00:55:42 +0100 | [diff] [blame] | 5090 | /* |
| 5091 | * Keep looping until we have no more ranges in the io tree. |
| 5092 | * We can have ongoing bios started by readpages (called from readahead) |
Filipe Manana | 9c6429d | 2015-06-10 12:55:41 +0100 | [diff] [blame] | 5093 | * that have their endio callback (extent_io.c:end_bio_extent_readpage) |
| 5094 | * still in progress (unlocked the pages in the bio but did not yet |
| 5095 | * unlocked the ranges in the io tree). Therefore this means some |
Filipe Manana | 6ca0709 | 2015-05-26 00:55:42 +0100 | [diff] [blame] | 5096 | * ranges can still be locked and eviction started because before |
| 5097 | * submitting those bios, which are executed by a separate task (work |
| 5098 | * queue kthread), inode references (inode->i_count) were not taken |
| 5099 | * (which would be dropped in the end io callback of each bio). |
| 5100 | * Therefore here we effectively end up waiting for those bios and |
| 5101 | * anyone else holding locked ranges without having bumped the inode's |
| 5102 | * reference count - if we don't do it, when they access the inode's |
| 5103 | * io_tree to unlock a range it may be too late, leading to an |
| 5104 | * use-after-free issue. |
| 5105 | */ |
Filipe David Borba Manana | 131e404 | 2013-11-19 22:29:35 +0000 | [diff] [blame] | 5106 | spin_lock(&io_tree->lock); |
| 5107 | while (!RB_EMPTY_ROOT(&io_tree->state)) { |
| 5108 | struct extent_state *state; |
| 5109 | struct extent_state *cached_state = NULL; |
Filipe Manana | 6ca0709 | 2015-05-26 00:55:42 +0100 | [diff] [blame] | 5110 | u64 start; |
| 5111 | u64 end; |
Filipe David Borba Manana | 131e404 | 2013-11-19 22:29:35 +0000 | [diff] [blame] | 5112 | |
| 5113 | node = rb_first(&io_tree->state); |
| 5114 | state = rb_entry(node, struct extent_state, rb_node); |
Filipe Manana | 6ca0709 | 2015-05-26 00:55:42 +0100 | [diff] [blame] | 5115 | start = state->start; |
| 5116 | end = state->end; |
Filipe David Borba Manana | 131e404 | 2013-11-19 22:29:35 +0000 | [diff] [blame] | 5117 | spin_unlock(&io_tree->lock); |
| 5118 | |
Filipe Manana | 6ca0709 | 2015-05-26 00:55:42 +0100 | [diff] [blame] | 5119 | lock_extent_bits(io_tree, start, end, 0, &cached_state); |
| 5120 | clear_extent_bit(io_tree, start, end, |
Filipe David Borba Manana | 131e404 | 2013-11-19 22:29:35 +0000 | [diff] [blame] | 5121 | EXTENT_LOCKED | EXTENT_DIRTY | |
| 5122 | EXTENT_DELALLOC | EXTENT_DO_ACCOUNTING | |
| 5123 | EXTENT_DEFRAG, 1, 1, |
| 5124 | &cached_state, GFP_NOFS); |
Filipe David Borba Manana | 131e404 | 2013-11-19 22:29:35 +0000 | [diff] [blame] | 5125 | |
Filipe Manana | 7064dd5 | 2014-08-08 02:47:05 +0100 | [diff] [blame] | 5126 | cond_resched(); |
Filipe David Borba Manana | 131e404 | 2013-11-19 22:29:35 +0000 | [diff] [blame] | 5127 | spin_lock(&io_tree->lock); |
| 5128 | } |
| 5129 | spin_unlock(&io_tree->lock); |
| 5130 | } |
| 5131 | |
Al Viro | bd55597 | 2010-06-07 11:35:40 -0400 | [diff] [blame] | 5132 | void btrfs_evict_inode(struct inode *inode) |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5133 | { |
| 5134 | struct btrfs_trans_handle *trans; |
| 5135 | struct btrfs_root *root = BTRFS_I(inode)->root; |
Josef Bacik | 726c35f | 2011-09-26 15:46:06 -0400 | [diff] [blame] | 5136 | struct btrfs_block_rsv *rsv, *global_rsv; |
Josef Bacik | 3bce876 | 2015-02-24 12:35:51 -0800 | [diff] [blame] | 5137 | int steal_from_global = 0; |
Josef Bacik | 0712718 | 2011-08-19 10:29:59 -0400 | [diff] [blame] | 5138 | u64 min_size = btrfs_calc_trunc_metadata_size(root, 1); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5139 | int ret; |
| 5140 | |
liubo | 1abe9b8 | 2011-03-24 11:18:59 +0000 | [diff] [blame] | 5141 | trace_btrfs_inode_evict(inode); |
| 5142 | |
Filipe David Borba Manana | 131e404 | 2013-11-19 22:29:35 +0000 | [diff] [blame] | 5143 | evict_inode_truncate_pages(inode); |
| 5144 | |
Stefan Behrens | 69e9c6c | 2013-09-05 16:58:43 +0200 | [diff] [blame] | 5145 | if (inode->i_nlink && |
| 5146 | ((btrfs_root_refs(&root->root_item) != 0 && |
| 5147 | root->root_key.objectid != BTRFS_ROOT_TREE_OBJECTID) || |
| 5148 | btrfs_is_free_space_inode(inode))) |
Al Viro | bd55597 | 2010-06-07 11:35:40 -0400 | [diff] [blame] | 5149 | goto no_delete; |
| 5150 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5151 | if (is_bad_inode(inode)) { |
Josef Bacik | 7b12876 | 2008-07-24 12:17:14 -0400 | [diff] [blame] | 5152 | btrfs_orphan_del(NULL, inode); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5153 | goto no_delete; |
| 5154 | } |
Al Viro | bd55597 | 2010-06-07 11:35:40 -0400 | [diff] [blame] | 5155 | /* 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] | 5156 | if (!special_file(inode->i_mode)) |
| 5157 | btrfs_wait_ordered_range(inode, 0, (u64)-1); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 5158 | |
Miao Xie | f612496 | 2014-09-12 18:44:04 +0800 | [diff] [blame] | 5159 | btrfs_free_io_failure_record(inode, 0, (u64)-1); |
| 5160 | |
Yan, Zheng | c71bf09 | 2009-11-12 09:34:40 +0000 | [diff] [blame] | 5161 | if (root->fs_info->log_root_recovering) { |
Liu Bo | 6bf0231 | 2012-06-25 21:59:09 -0600 | [diff] [blame] | 5162 | BUG_ON(test_bit(BTRFS_INODE_HAS_ORPHAN_ITEM, |
Josef Bacik | 8a35d95 | 2012-05-23 14:26:42 -0400 | [diff] [blame] | 5163 | &BTRFS_I(inode)->runtime_flags)); |
Yan, Zheng | c71bf09 | 2009-11-12 09:34:40 +0000 | [diff] [blame] | 5164 | goto no_delete; |
| 5165 | } |
| 5166 | |
Yan, Zheng | 76dda93 | 2009-09-21 16:00:26 -0400 | [diff] [blame] | 5167 | if (inode->i_nlink > 0) { |
Stefan Behrens | 69e9c6c | 2013-09-05 16:58:43 +0200 | [diff] [blame] | 5168 | BUG_ON(btrfs_root_refs(&root->root_item) != 0 && |
| 5169 | root->root_key.objectid != BTRFS_ROOT_TREE_OBJECTID); |
Yan, Zheng | 76dda93 | 2009-09-21 16:00:26 -0400 | [diff] [blame] | 5170 | goto no_delete; |
| 5171 | } |
| 5172 | |
Miao Xie | 0e8c36a | 2012-12-19 06:59:51 +0000 | [diff] [blame] | 5173 | ret = btrfs_commit_inode_delayed_inode(inode); |
| 5174 | if (ret) { |
| 5175 | btrfs_orphan_del(NULL, inode); |
| 5176 | goto no_delete; |
| 5177 | } |
| 5178 | |
Miao Xie | 66d8f3d | 2012-09-06 04:02:28 -0600 | [diff] [blame] | 5179 | rsv = btrfs_alloc_block_rsv(root, BTRFS_BLOCK_RSV_TEMP); |
Josef Bacik | 4289a66 | 2011-08-05 13:22:24 -0400 | [diff] [blame] | 5180 | if (!rsv) { |
| 5181 | btrfs_orphan_del(NULL, inode); |
| 5182 | goto no_delete; |
| 5183 | } |
Josef Bacik | 4a33854 | 2011-08-29 11:01:31 -0400 | [diff] [blame] | 5184 | rsv->size = min_size; |
Josef Bacik | ca7e70f | 2012-08-27 17:48:15 -0400 | [diff] [blame] | 5185 | rsv->failfast = 1; |
Josef Bacik | 726c35f | 2011-09-26 15:46:06 -0400 | [diff] [blame] | 5186 | global_rsv = &root->fs_info->global_block_rsv; |
Josef Bacik | 4289a66 | 2011-08-05 13:22:24 -0400 | [diff] [blame] | 5187 | |
Chris Mason | dbe674a | 2008-07-17 12:54:05 -0400 | [diff] [blame] | 5188 | btrfs_i_size_write(inode, 0); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 5189 | |
Josef Bacik | 4289a66 | 2011-08-05 13:22:24 -0400 | [diff] [blame] | 5190 | /* |
Miao Xie | 8407aa4 | 2012-09-07 01:43:32 -0600 | [diff] [blame] | 5191 | * This is a bit simpler than btrfs_truncate since we've already |
| 5192 | * reserved our space for our orphan item in the unlink, so we just |
| 5193 | * need to reserve some slack space in case we add bytes and update |
| 5194 | * inode item when doing the truncate. |
Josef Bacik | 4289a66 | 2011-08-05 13:22:24 -0400 | [diff] [blame] | 5195 | */ |
Yan, Zheng | 8082510 | 2009-11-12 09:35:36 +0000 | [diff] [blame] | 5196 | while (1) { |
Miao Xie | 08e007d | 2012-10-16 11:33:38 +0000 | [diff] [blame] | 5197 | ret = btrfs_block_rsv_refill(root, rsv, min_size, |
| 5198 | BTRFS_RESERVE_FLUSH_LIMIT); |
Yan, Zheng | 8082510 | 2009-11-12 09:35:36 +0000 | [diff] [blame] | 5199 | |
Josef Bacik | 726c35f | 2011-09-26 15:46:06 -0400 | [diff] [blame] | 5200 | /* |
| 5201 | * Try and steal from the global reserve since we will |
| 5202 | * likely not use this space anyway, we want to try as |
| 5203 | * hard as possible to get this to work. |
| 5204 | */ |
| 5205 | if (ret) |
Josef Bacik | 3bce876 | 2015-02-24 12:35:51 -0800 | [diff] [blame] | 5206 | steal_from_global++; |
| 5207 | else |
| 5208 | steal_from_global = 0; |
| 5209 | ret = 0; |
Josef Bacik | 726c35f | 2011-09-26 15:46:06 -0400 | [diff] [blame] | 5210 | |
Josef Bacik | 3bce876 | 2015-02-24 12:35:51 -0800 | [diff] [blame] | 5211 | /* |
| 5212 | * steal_from_global == 0: we reserved stuff, hooray! |
| 5213 | * steal_from_global == 1: we didn't reserve stuff, boo! |
| 5214 | * steal_from_global == 2: we've committed, still not a lot of |
| 5215 | * room but maybe we'll have room in the global reserve this |
| 5216 | * time. |
| 5217 | * steal_from_global == 3: abandon all hope! |
| 5218 | */ |
| 5219 | if (steal_from_global > 2) { |
Simon Kirby | c2cf52e | 2013-03-19 22:41:23 +0000 | [diff] [blame] | 5220 | btrfs_warn(root->fs_info, |
| 5221 | "Could not get space for a delete, will truncate on mount %d", |
| 5222 | ret); |
Josef Bacik | 4289a66 | 2011-08-05 13:22:24 -0400 | [diff] [blame] | 5223 | btrfs_orphan_del(NULL, inode); |
| 5224 | btrfs_free_block_rsv(root, rsv); |
| 5225 | goto no_delete; |
Yan, Zheng | d68fc57 | 2010-05-16 10:49:58 -0400 | [diff] [blame] | 5226 | } |
| 5227 | |
Miao Xie | 0e8c36a | 2012-12-19 06:59:51 +0000 | [diff] [blame] | 5228 | trans = btrfs_join_transaction(root); |
Josef Bacik | 4289a66 | 2011-08-05 13:22:24 -0400 | [diff] [blame] | 5229 | if (IS_ERR(trans)) { |
| 5230 | btrfs_orphan_del(NULL, inode); |
| 5231 | btrfs_free_block_rsv(root, rsv); |
| 5232 | goto no_delete; |
| 5233 | } |
| 5234 | |
Josef Bacik | 3bce876 | 2015-02-24 12:35:51 -0800 | [diff] [blame] | 5235 | /* |
| 5236 | * We can't just steal from the global reserve, we need tomake |
| 5237 | * sure there is room to do it, if not we need to commit and try |
| 5238 | * again. |
| 5239 | */ |
| 5240 | if (steal_from_global) { |
| 5241 | if (!btrfs_check_space_for_delayed_refs(trans, root)) |
| 5242 | ret = btrfs_block_rsv_migrate(global_rsv, rsv, |
| 5243 | min_size); |
| 5244 | else |
| 5245 | ret = -ENOSPC; |
| 5246 | } |
| 5247 | |
| 5248 | /* |
| 5249 | * Couldn't steal from the global reserve, we have too much |
| 5250 | * pending stuff built up, commit the transaction and try it |
| 5251 | * again. |
| 5252 | */ |
| 5253 | if (ret) { |
| 5254 | ret = btrfs_commit_transaction(trans, root); |
| 5255 | if (ret) { |
| 5256 | btrfs_orphan_del(NULL, inode); |
| 5257 | btrfs_free_block_rsv(root, rsv); |
| 5258 | goto no_delete; |
| 5259 | } |
| 5260 | continue; |
| 5261 | } else { |
| 5262 | steal_from_global = 0; |
| 5263 | } |
| 5264 | |
Josef Bacik | 4289a66 | 2011-08-05 13:22:24 -0400 | [diff] [blame] | 5265 | trans->block_rsv = rsv; |
| 5266 | |
Yan, Zheng | d68fc57 | 2010-05-16 10:49:58 -0400 | [diff] [blame] | 5267 | ret = btrfs_truncate_inode_items(trans, root, inode, 0, 0); |
Chris Mason | 28ed134 | 2014-12-17 09:41:04 -0800 | [diff] [blame] | 5268 | if (ret != -ENOSPC && ret != -EAGAIN) |
Yan, Zheng | 8082510 | 2009-11-12 09:35:36 +0000 | [diff] [blame] | 5269 | break; |
| 5270 | |
Miao Xie | 8407aa4 | 2012-09-07 01:43:32 -0600 | [diff] [blame] | 5271 | trans->block_rsv = &root->fs_info->trans_block_rsv; |
Yan, Zheng | 8082510 | 2009-11-12 09:35:36 +0000 | [diff] [blame] | 5272 | btrfs_end_transaction(trans, root); |
| 5273 | trans = NULL; |
Liu Bo | b53d3f5 | 2012-11-14 14:34:34 +0000 | [diff] [blame] | 5274 | btrfs_btree_balance_dirty(root); |
Josef Bacik | 7b12876 | 2008-07-24 12:17:14 -0400 | [diff] [blame] | 5275 | } |
| 5276 | |
Josef Bacik | 4289a66 | 2011-08-05 13:22:24 -0400 | [diff] [blame] | 5277 | btrfs_free_block_rsv(root, rsv); |
| 5278 | |
Josef Bacik | 4ef31a4 | 2013-08-13 14:10:08 -0400 | [diff] [blame] | 5279 | /* |
| 5280 | * Errors here aren't a big deal, it just means we leave orphan items |
| 5281 | * in the tree. They will be cleaned up on the next mount. |
| 5282 | */ |
Yan, Zheng | 8082510 | 2009-11-12 09:35:36 +0000 | [diff] [blame] | 5283 | if (ret == 0) { |
Josef Bacik | 4289a66 | 2011-08-05 13:22:24 -0400 | [diff] [blame] | 5284 | trans->block_rsv = root->orphan_block_rsv; |
Josef Bacik | 4ef31a4 | 2013-08-13 14:10:08 -0400 | [diff] [blame] | 5285 | btrfs_orphan_del(trans, inode); |
| 5286 | } else { |
| 5287 | btrfs_orphan_del(NULL, inode); |
Yan, Zheng | 8082510 | 2009-11-12 09:35:36 +0000 | [diff] [blame] | 5288 | } |
Chris Mason | 85e21ba | 2008-01-29 15:11:36 -0500 | [diff] [blame] | 5289 | |
Josef Bacik | 4289a66 | 2011-08-05 13:22:24 -0400 | [diff] [blame] | 5290 | trans->block_rsv = &root->fs_info->trans_block_rsv; |
Li Zefan | 581bb05 | 2011-04-20 10:06:11 +0800 | [diff] [blame] | 5291 | if (!(root == root->fs_info->tree_root || |
| 5292 | root->root_key.objectid == BTRFS_TREE_RELOC_OBJECTID)) |
Li Zefan | 33345d01 | 2011-04-20 10:31:50 +0800 | [diff] [blame] | 5293 | btrfs_return_ino(root, btrfs_ino(inode)); |
Li Zefan | 581bb05 | 2011-04-20 10:06:11 +0800 | [diff] [blame] | 5294 | |
Chris Mason | 54aa1f4 | 2007-06-22 14:16:25 -0400 | [diff] [blame] | 5295 | btrfs_end_transaction(trans, root); |
Liu Bo | b53d3f5 | 2012-11-14 14:34:34 +0000 | [diff] [blame] | 5296 | btrfs_btree_balance_dirty(root); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5297 | no_delete: |
Miao Xie | 89042e5 | 2013-05-15 07:48:15 +0000 | [diff] [blame] | 5298 | btrfs_remove_delayed_node(inode); |
Jan Kara | dbd5768 | 2012-05-03 14:48:02 +0200 | [diff] [blame] | 5299 | clear_inode(inode); |
Yan, Zheng | 8082510 | 2009-11-12 09:35:36 +0000 | [diff] [blame] | 5300 | return; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5301 | } |
| 5302 | |
| 5303 | /* |
| 5304 | * this returns the key found in the dir entry in the location pointer. |
| 5305 | * If no dir entries were found, location->objectid is 0. |
| 5306 | */ |
| 5307 | static int btrfs_inode_by_name(struct inode *dir, struct dentry *dentry, |
| 5308 | struct btrfs_key *location) |
| 5309 | { |
| 5310 | const char *name = dentry->d_name.name; |
| 5311 | int namelen = dentry->d_name.len; |
| 5312 | struct btrfs_dir_item *di; |
| 5313 | struct btrfs_path *path; |
| 5314 | struct btrfs_root *root = BTRFS_I(dir)->root; |
Yan | 0d9f7f3 | 2007-10-25 15:48:28 -0400 | [diff] [blame] | 5315 | int ret = 0; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5316 | |
| 5317 | path = btrfs_alloc_path(); |
Mark Fasheh | d8926bb | 2011-07-13 10:38:47 -0700 | [diff] [blame] | 5318 | if (!path) |
| 5319 | return -ENOMEM; |
Chris Mason | 3954401 | 2007-12-12 14:38:19 -0500 | [diff] [blame] | 5320 | |
Li Zefan | 33345d01 | 2011-04-20 10:31:50 +0800 | [diff] [blame] | 5321 | di = btrfs_lookup_dir_item(NULL, root, path, btrfs_ino(dir), name, |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5322 | namelen, 0); |
Yan | 0d9f7f3 | 2007-10-25 15:48:28 -0400 | [diff] [blame] | 5323 | if (IS_ERR(di)) |
| 5324 | ret = PTR_ERR(di); |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 5325 | |
David Sterba | c704005 | 2011-04-19 18:00:01 +0200 | [diff] [blame] | 5326 | if (IS_ERR_OR_NULL(di)) |
Chris Mason | 3954401 | 2007-12-12 14:38:19 -0500 | [diff] [blame] | 5327 | goto out_err; |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 5328 | |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 5329 | btrfs_dir_item_key_to_cpu(path->nodes[0], di, location); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5330 | out: |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5331 | btrfs_free_path(path); |
| 5332 | return ret; |
Chris Mason | 3954401 | 2007-12-12 14:38:19 -0500 | [diff] [blame] | 5333 | out_err: |
| 5334 | location->objectid = 0; |
| 5335 | goto out; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5336 | } |
| 5337 | |
| 5338 | /* |
| 5339 | * when we hit a tree root in a directory, the btrfs part of the inode |
| 5340 | * needs to be changed to reflect the root directory of the tree root. This |
| 5341 | * is kind of like crossing a mount point. |
| 5342 | */ |
| 5343 | static int fixup_tree_root_location(struct btrfs_root *root, |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 5344 | struct inode *dir, |
| 5345 | struct dentry *dentry, |
| 5346 | struct btrfs_key *location, |
| 5347 | struct btrfs_root **sub_root) |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5348 | { |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 5349 | struct btrfs_path *path; |
| 5350 | struct btrfs_root *new_root; |
| 5351 | struct btrfs_root_ref *ref; |
| 5352 | struct extent_buffer *leaf; |
David Sterba | 1d4c08e | 2015-01-02 19:36:14 +0100 | [diff] [blame] | 5353 | struct btrfs_key key; |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 5354 | int ret; |
| 5355 | int err = 0; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5356 | |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 5357 | path = btrfs_alloc_path(); |
| 5358 | if (!path) { |
| 5359 | err = -ENOMEM; |
| 5360 | goto out; |
| 5361 | } |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5362 | |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 5363 | err = -ENOENT; |
David Sterba | 1d4c08e | 2015-01-02 19:36:14 +0100 | [diff] [blame] | 5364 | key.objectid = BTRFS_I(dir)->root->root_key.objectid; |
| 5365 | key.type = BTRFS_ROOT_REF_KEY; |
| 5366 | key.offset = location->objectid; |
| 5367 | |
| 5368 | ret = btrfs_search_slot(NULL, root->fs_info->tree_root, &key, path, |
| 5369 | 0, 0); |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 5370 | if (ret) { |
| 5371 | if (ret < 0) |
| 5372 | err = ret; |
| 5373 | goto out; |
| 5374 | } |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5375 | |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 5376 | leaf = path->nodes[0]; |
| 5377 | ref = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_root_ref); |
Li Zefan | 33345d01 | 2011-04-20 10:31:50 +0800 | [diff] [blame] | 5378 | if (btrfs_root_ref_dirid(leaf, ref) != btrfs_ino(dir) || |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 5379 | btrfs_root_ref_name_len(leaf, ref) != dentry->d_name.len) |
| 5380 | goto out; |
| 5381 | |
| 5382 | ret = memcmp_extent_buffer(leaf, dentry->d_name.name, |
| 5383 | (unsigned long)(ref + 1), |
| 5384 | dentry->d_name.len); |
| 5385 | if (ret) |
| 5386 | goto out; |
| 5387 | |
David Sterba | b3b4aa7 | 2011-04-21 01:20:15 +0200 | [diff] [blame] | 5388 | btrfs_release_path(path); |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 5389 | |
| 5390 | new_root = btrfs_read_fs_root_no_name(root->fs_info, location); |
| 5391 | if (IS_ERR(new_root)) { |
| 5392 | err = PTR_ERR(new_root); |
| 5393 | goto out; |
| 5394 | } |
| 5395 | |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 5396 | *sub_root = new_root; |
| 5397 | location->objectid = btrfs_root_dirid(&new_root->root_item); |
| 5398 | location->type = BTRFS_INODE_ITEM_KEY; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5399 | location->offset = 0; |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 5400 | err = 0; |
| 5401 | out: |
| 5402 | btrfs_free_path(path); |
| 5403 | return err; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5404 | } |
| 5405 | |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 5406 | static void inode_tree_add(struct inode *inode) |
| 5407 | { |
| 5408 | struct btrfs_root *root = BTRFS_I(inode)->root; |
| 5409 | struct btrfs_inode *entry; |
From: Nick Piggin | 03e860b | 2009-08-21 10:09:44 +0200 | [diff] [blame] | 5410 | struct rb_node **p; |
| 5411 | struct rb_node *parent; |
Filipe David Borba Manana | cef2193 | 2013-09-02 12:19:13 +0100 | [diff] [blame] | 5412 | struct rb_node *new = &BTRFS_I(inode)->rb_node; |
Li Zefan | 33345d01 | 2011-04-20 10:31:50 +0800 | [diff] [blame] | 5413 | u64 ino = btrfs_ino(inode); |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 5414 | |
Al Viro | 1d3382cb | 2010-10-23 15:19:20 -0400 | [diff] [blame] | 5415 | if (inode_unhashed(inode)) |
Yan, Zheng | 76dda93 | 2009-09-21 16:00:26 -0400 | [diff] [blame] | 5416 | return; |
Miao Xie | e1409ce | 2013-05-15 07:48:16 +0000 | [diff] [blame] | 5417 | parent = NULL; |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 5418 | spin_lock(&root->inode_lock); |
Miao Xie | e1409ce | 2013-05-15 07:48:16 +0000 | [diff] [blame] | 5419 | p = &root->inode_tree.rb_node; |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 5420 | while (*p) { |
| 5421 | parent = *p; |
| 5422 | entry = rb_entry(parent, struct btrfs_inode, rb_node); |
| 5423 | |
Li Zefan | 33345d01 | 2011-04-20 10:31:50 +0800 | [diff] [blame] | 5424 | if (ino < btrfs_ino(&entry->vfs_inode)) |
From: Nick Piggin | 03e860b | 2009-08-21 10:09:44 +0200 | [diff] [blame] | 5425 | p = &parent->rb_left; |
Li Zefan | 33345d01 | 2011-04-20 10:31:50 +0800 | [diff] [blame] | 5426 | else if (ino > btrfs_ino(&entry->vfs_inode)) |
From: Nick Piggin | 03e860b | 2009-08-21 10:09:44 +0200 | [diff] [blame] | 5427 | p = &parent->rb_right; |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 5428 | else { |
| 5429 | WARN_ON(!(entry->vfs_inode.i_state & |
Al Viro | a4ffdde | 2010-06-02 17:38:30 -0400 | [diff] [blame] | 5430 | (I_WILL_FREE | I_FREEING))); |
Filipe David Borba Manana | cef2193 | 2013-09-02 12:19:13 +0100 | [diff] [blame] | 5431 | rb_replace_node(parent, new, &root->inode_tree); |
From: Nick Piggin | 03e860b | 2009-08-21 10:09:44 +0200 | [diff] [blame] | 5432 | RB_CLEAR_NODE(parent); |
| 5433 | spin_unlock(&root->inode_lock); |
Filipe David Borba Manana | cef2193 | 2013-09-02 12:19:13 +0100 | [diff] [blame] | 5434 | return; |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 5435 | } |
| 5436 | } |
Filipe David Borba Manana | cef2193 | 2013-09-02 12:19:13 +0100 | [diff] [blame] | 5437 | rb_link_node(new, parent, p); |
| 5438 | rb_insert_color(new, &root->inode_tree); |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 5439 | spin_unlock(&root->inode_lock); |
| 5440 | } |
| 5441 | |
| 5442 | static void inode_tree_del(struct inode *inode) |
| 5443 | { |
| 5444 | struct btrfs_root *root = BTRFS_I(inode)->root; |
Yan, Zheng | 76dda93 | 2009-09-21 16:00:26 -0400 | [diff] [blame] | 5445 | int empty = 0; |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 5446 | |
From: Nick Piggin | 03e860b | 2009-08-21 10:09:44 +0200 | [diff] [blame] | 5447 | spin_lock(&root->inode_lock); |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 5448 | if (!RB_EMPTY_NODE(&BTRFS_I(inode)->rb_node)) { |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 5449 | rb_erase(&BTRFS_I(inode)->rb_node, &root->inode_tree); |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 5450 | RB_CLEAR_NODE(&BTRFS_I(inode)->rb_node); |
Yan, Zheng | 76dda93 | 2009-09-21 16:00:26 -0400 | [diff] [blame] | 5451 | empty = RB_EMPTY_ROOT(&root->inode_tree); |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 5452 | } |
From: Nick Piggin | 03e860b | 2009-08-21 10:09:44 +0200 | [diff] [blame] | 5453 | spin_unlock(&root->inode_lock); |
Yan, Zheng | 76dda93 | 2009-09-21 16:00:26 -0400 | [diff] [blame] | 5454 | |
Stefan Behrens | 69e9c6c | 2013-09-05 16:58:43 +0200 | [diff] [blame] | 5455 | if (empty && btrfs_root_refs(&root->root_item) == 0) { |
Yan, Zheng | 76dda93 | 2009-09-21 16:00:26 -0400 | [diff] [blame] | 5456 | synchronize_srcu(&root->fs_info->subvol_srcu); |
| 5457 | spin_lock(&root->inode_lock); |
| 5458 | empty = RB_EMPTY_ROOT(&root->inode_tree); |
| 5459 | spin_unlock(&root->inode_lock); |
| 5460 | if (empty) |
| 5461 | btrfs_add_dead_root(root); |
| 5462 | } |
| 5463 | } |
| 5464 | |
Jeff Mahoney | 143bede | 2012-03-01 14:56:26 +0100 | [diff] [blame] | 5465 | void btrfs_invalidate_inodes(struct btrfs_root *root) |
Yan, Zheng | 76dda93 | 2009-09-21 16:00:26 -0400 | [diff] [blame] | 5466 | { |
| 5467 | struct rb_node *node; |
| 5468 | struct rb_node *prev; |
| 5469 | struct btrfs_inode *entry; |
| 5470 | struct inode *inode; |
| 5471 | u64 objectid = 0; |
| 5472 | |
Liu Bo | 7813b3d | 2014-02-10 17:37:25 +0800 | [diff] [blame] | 5473 | if (!test_bit(BTRFS_FS_STATE_ERROR, &root->fs_info->fs_state)) |
| 5474 | WARN_ON(btrfs_root_refs(&root->root_item) != 0); |
Yan, Zheng | 76dda93 | 2009-09-21 16:00:26 -0400 | [diff] [blame] | 5475 | |
| 5476 | spin_lock(&root->inode_lock); |
| 5477 | again: |
| 5478 | node = root->inode_tree.rb_node; |
| 5479 | prev = NULL; |
| 5480 | while (node) { |
| 5481 | prev = node; |
| 5482 | entry = rb_entry(node, struct btrfs_inode, rb_node); |
| 5483 | |
Li Zefan | 33345d01 | 2011-04-20 10:31:50 +0800 | [diff] [blame] | 5484 | if (objectid < btrfs_ino(&entry->vfs_inode)) |
Yan, Zheng | 76dda93 | 2009-09-21 16:00:26 -0400 | [diff] [blame] | 5485 | node = node->rb_left; |
Li Zefan | 33345d01 | 2011-04-20 10:31:50 +0800 | [diff] [blame] | 5486 | else if (objectid > btrfs_ino(&entry->vfs_inode)) |
Yan, Zheng | 76dda93 | 2009-09-21 16:00:26 -0400 | [diff] [blame] | 5487 | node = node->rb_right; |
| 5488 | else |
| 5489 | break; |
| 5490 | } |
| 5491 | if (!node) { |
| 5492 | while (prev) { |
| 5493 | entry = rb_entry(prev, struct btrfs_inode, rb_node); |
Li Zefan | 33345d01 | 2011-04-20 10:31:50 +0800 | [diff] [blame] | 5494 | if (objectid <= btrfs_ino(&entry->vfs_inode)) { |
Yan, Zheng | 76dda93 | 2009-09-21 16:00:26 -0400 | [diff] [blame] | 5495 | node = prev; |
| 5496 | break; |
| 5497 | } |
| 5498 | prev = rb_next(prev); |
| 5499 | } |
| 5500 | } |
| 5501 | while (node) { |
| 5502 | entry = rb_entry(node, struct btrfs_inode, rb_node); |
Li Zefan | 33345d01 | 2011-04-20 10:31:50 +0800 | [diff] [blame] | 5503 | objectid = btrfs_ino(&entry->vfs_inode) + 1; |
Yan, Zheng | 76dda93 | 2009-09-21 16:00:26 -0400 | [diff] [blame] | 5504 | inode = igrab(&entry->vfs_inode); |
| 5505 | if (inode) { |
| 5506 | spin_unlock(&root->inode_lock); |
| 5507 | if (atomic_read(&inode->i_count) > 1) |
| 5508 | d_prune_aliases(inode); |
| 5509 | /* |
Al Viro | 45321ac | 2010-06-07 13:43:19 -0400 | [diff] [blame] | 5510 | * btrfs_drop_inode will have it removed from |
Yan, Zheng | 76dda93 | 2009-09-21 16:00:26 -0400 | [diff] [blame] | 5511 | * the inode cache when its usage count |
| 5512 | * hits zero. |
| 5513 | */ |
| 5514 | iput(inode); |
| 5515 | cond_resched(); |
| 5516 | spin_lock(&root->inode_lock); |
| 5517 | goto again; |
| 5518 | } |
| 5519 | |
| 5520 | if (cond_resched_lock(&root->inode_lock)) |
| 5521 | goto again; |
| 5522 | |
| 5523 | node = rb_next(node); |
| 5524 | } |
| 5525 | spin_unlock(&root->inode_lock); |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 5526 | } |
| 5527 | |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 5528 | static int btrfs_init_locked_inode(struct inode *inode, void *p) |
| 5529 | { |
| 5530 | struct btrfs_iget_args *args = p; |
Chris Mason | 90d3e59 | 2014-01-09 17:28:00 -0800 | [diff] [blame] | 5531 | inode->i_ino = args->location->objectid; |
| 5532 | memcpy(&BTRFS_I(inode)->location, args->location, |
| 5533 | sizeof(*args->location)); |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 5534 | BTRFS_I(inode)->root = args->root; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5535 | return 0; |
| 5536 | } |
| 5537 | |
| 5538 | static int btrfs_find_actor(struct inode *inode, void *opaque) |
| 5539 | { |
| 5540 | struct btrfs_iget_args *args = opaque; |
Chris Mason | 90d3e59 | 2014-01-09 17:28:00 -0800 | [diff] [blame] | 5541 | return args->location->objectid == BTRFS_I(inode)->location.objectid && |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 5542 | args->root == BTRFS_I(inode)->root; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5543 | } |
| 5544 | |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 5545 | static struct inode *btrfs_iget_locked(struct super_block *s, |
Chris Mason | 90d3e59 | 2014-01-09 17:28:00 -0800 | [diff] [blame] | 5546 | struct btrfs_key *location, |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 5547 | struct btrfs_root *root) |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5548 | { |
| 5549 | struct inode *inode; |
| 5550 | struct btrfs_iget_args args; |
Chris Mason | 90d3e59 | 2014-01-09 17:28:00 -0800 | [diff] [blame] | 5551 | unsigned long hashval = btrfs_inode_hash(location->objectid, root); |
Filipe David Borba Manana | 778ba82 | 2013-10-06 22:22:33 +0100 | [diff] [blame] | 5552 | |
Chris Mason | 90d3e59 | 2014-01-09 17:28:00 -0800 | [diff] [blame] | 5553 | args.location = location; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5554 | args.root = root; |
| 5555 | |
Filipe David Borba Manana | 778ba82 | 2013-10-06 22:22:33 +0100 | [diff] [blame] | 5556 | inode = iget5_locked(s, hashval, btrfs_find_actor, |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5557 | btrfs_init_locked_inode, |
| 5558 | (void *)&args); |
| 5559 | return inode; |
| 5560 | } |
| 5561 | |
Balaji Rao | 1a54ef8 | 2008-07-21 02:01:04 +0530 | [diff] [blame] | 5562 | /* Get an inode object given its location and corresponding root. |
| 5563 | * Returns in *is_new if the inode was read from disk |
| 5564 | */ |
| 5565 | struct inode *btrfs_iget(struct super_block *s, struct btrfs_key *location, |
Josef Bacik | 73f7341 | 2009-12-04 17:38:27 +0000 | [diff] [blame] | 5566 | struct btrfs_root *root, int *new) |
Balaji Rao | 1a54ef8 | 2008-07-21 02:01:04 +0530 | [diff] [blame] | 5567 | { |
| 5568 | struct inode *inode; |
| 5569 | |
Chris Mason | 90d3e59 | 2014-01-09 17:28:00 -0800 | [diff] [blame] | 5570 | inode = btrfs_iget_locked(s, location, root); |
Balaji Rao | 1a54ef8 | 2008-07-21 02:01:04 +0530 | [diff] [blame] | 5571 | if (!inode) |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 5572 | return ERR_PTR(-ENOMEM); |
Balaji Rao | 1a54ef8 | 2008-07-21 02:01:04 +0530 | [diff] [blame] | 5573 | |
| 5574 | if (inode->i_state & I_NEW) { |
Balaji Rao | 1a54ef8 | 2008-07-21 02:01:04 +0530 | [diff] [blame] | 5575 | btrfs_read_locked_inode(inode); |
Mark Fasheh | 1748f84 | 2011-07-12 11:25:31 -0700 | [diff] [blame] | 5576 | if (!is_bad_inode(inode)) { |
| 5577 | inode_tree_add(inode); |
| 5578 | unlock_new_inode(inode); |
| 5579 | if (new) |
| 5580 | *new = 1; |
| 5581 | } else { |
Sergei Trofimovich | e0b6d65 | 2011-09-11 10:52:24 -0400 | [diff] [blame] | 5582 | unlock_new_inode(inode); |
| 5583 | iput(inode); |
| 5584 | inode = ERR_PTR(-ESTALE); |
Mark Fasheh | 1748f84 | 2011-07-12 11:25:31 -0700 | [diff] [blame] | 5585 | } |
| 5586 | } |
| 5587 | |
Balaji Rao | 1a54ef8 | 2008-07-21 02:01:04 +0530 | [diff] [blame] | 5588 | return inode; |
| 5589 | } |
| 5590 | |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 5591 | static struct inode *new_simple_dir(struct super_block *s, |
| 5592 | struct btrfs_key *key, |
| 5593 | struct btrfs_root *root) |
| 5594 | { |
| 5595 | struct inode *inode = new_inode(s); |
| 5596 | |
| 5597 | if (!inode) |
| 5598 | return ERR_PTR(-ENOMEM); |
| 5599 | |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 5600 | BTRFS_I(inode)->root = root; |
| 5601 | memcpy(&BTRFS_I(inode)->location, key, sizeof(*key)); |
Josef Bacik | 72ac3c0 | 2012-05-23 14:13:11 -0400 | [diff] [blame] | 5602 | set_bit(BTRFS_INODE_DUMMY, &BTRFS_I(inode)->runtime_flags); |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 5603 | |
| 5604 | inode->i_ino = BTRFS_EMPTY_SUBVOL_DIR_OBJECTID; |
Li Zefan | 848cce0 | 2012-02-21 17:04:28 +0800 | [diff] [blame] | 5605 | inode->i_op = &btrfs_dir_ro_inode_operations; |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 5606 | inode->i_fop = &simple_dir_operations; |
| 5607 | inode->i_mode = S_IFDIR | S_IRUGO | S_IWUSR | S_IXUGO; |
chandan r | 9cc97d6 | 2012-07-04 12:48:07 +0530 | [diff] [blame] | 5608 | inode->i_mtime = CURRENT_TIME; |
| 5609 | inode->i_atime = inode->i_mtime; |
| 5610 | inode->i_ctime = inode->i_mtime; |
| 5611 | BTRFS_I(inode)->i_otime = inode->i_mtime; |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 5612 | |
| 5613 | return inode; |
| 5614 | } |
| 5615 | |
Chris Mason | 3de4586 | 2008-11-17 21:02:50 -0500 | [diff] [blame] | 5616 | struct inode *btrfs_lookup_dentry(struct inode *dir, struct dentry *dentry) |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5617 | { |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 5618 | struct inode *inode; |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 5619 | struct btrfs_root *root = BTRFS_I(dir)->root; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5620 | struct btrfs_root *sub_root = root; |
| 5621 | struct btrfs_key location; |
Yan, Zheng | 76dda93 | 2009-09-21 16:00:26 -0400 | [diff] [blame] | 5622 | int index; |
Josef Bacik | b4aff1f | 2011-06-28 16:18:59 -0400 | [diff] [blame] | 5623 | int ret = 0; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5624 | |
| 5625 | if (dentry->d_name.len > BTRFS_NAME_LEN) |
| 5626 | return ERR_PTR(-ENAMETOOLONG); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 5627 | |
Jeff Layton | 39e3c95 | 2012-11-28 11:30:53 -0500 | [diff] [blame] | 5628 | ret = btrfs_inode_by_name(dir, dentry, &location); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5629 | if (ret < 0) |
| 5630 | return ERR_PTR(ret); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 5631 | |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 5632 | if (location.objectid == 0) |
Tsutomu Itoh | 5662344b3 | 2013-12-13 09:51:42 +0900 | [diff] [blame] | 5633 | return ERR_PTR(-ENOENT); |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 5634 | |
| 5635 | if (location.type == BTRFS_INODE_ITEM_KEY) { |
Josef Bacik | 73f7341 | 2009-12-04 17:38:27 +0000 | [diff] [blame] | 5636 | inode = btrfs_iget(dir->i_sb, &location, root, NULL); |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 5637 | return inode; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5638 | } |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 5639 | |
| 5640 | BUG_ON(location.type != BTRFS_ROOT_ITEM_KEY); |
| 5641 | |
Yan, Zheng | 76dda93 | 2009-09-21 16:00:26 -0400 | [diff] [blame] | 5642 | index = srcu_read_lock(&root->fs_info->subvol_srcu); |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 5643 | ret = fixup_tree_root_location(root, dir, dentry, |
| 5644 | &location, &sub_root); |
| 5645 | if (ret < 0) { |
| 5646 | if (ret != -ENOENT) |
| 5647 | inode = ERR_PTR(ret); |
| 5648 | else |
| 5649 | inode = new_simple_dir(dir->i_sb, &location, sub_root); |
| 5650 | } else { |
Josef Bacik | 73f7341 | 2009-12-04 17:38:27 +0000 | [diff] [blame] | 5651 | inode = btrfs_iget(dir->i_sb, &location, sub_root, NULL); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5652 | } |
Yan, Zheng | 76dda93 | 2009-09-21 16:00:26 -0400 | [diff] [blame] | 5653 | srcu_read_unlock(&root->fs_info->subvol_srcu, index); |
| 5654 | |
Julia Lawall | 34d19ba | 2011-01-24 19:55:19 +0000 | [diff] [blame] | 5655 | if (!IS_ERR(inode) && root != sub_root) { |
Yan, Zheng | c71bf09 | 2009-11-12 09:34:40 +0000 | [diff] [blame] | 5656 | down_read(&root->fs_info->cleanup_work_sem); |
| 5657 | if (!(inode->i_sb->s_flags & MS_RDONLY)) |
Josef Bacik | 66b4ffd | 2011-01-31 16:22:42 -0500 | [diff] [blame] | 5658 | ret = btrfs_orphan_cleanup(sub_root); |
Yan, Zheng | c71bf09 | 2009-11-12 09:34:40 +0000 | [diff] [blame] | 5659 | up_read(&root->fs_info->cleanup_work_sem); |
Josef Bacik | 01cd336 | 2013-06-03 21:39:49 -0400 | [diff] [blame] | 5660 | if (ret) { |
| 5661 | iput(inode); |
Josef Bacik | 66b4ffd | 2011-01-31 16:22:42 -0500 | [diff] [blame] | 5662 | inode = ERR_PTR(ret); |
Josef Bacik | 01cd336 | 2013-06-03 21:39:49 -0400 | [diff] [blame] | 5663 | } |
Yan, Zheng | c71bf09 | 2009-11-12 09:34:40 +0000 | [diff] [blame] | 5664 | } |
| 5665 | |
Chris Mason | 3de4586 | 2008-11-17 21:02:50 -0500 | [diff] [blame] | 5666 | return inode; |
| 5667 | } |
| 5668 | |
Nick Piggin | fe15ce4 | 2011-01-07 17:49:23 +1100 | [diff] [blame] | 5669 | static int btrfs_dentry_delete(const struct dentry *dentry) |
Yan, Zheng | 76dda93 | 2009-09-21 16:00:26 -0400 | [diff] [blame] | 5670 | { |
| 5671 | struct btrfs_root *root; |
David Howells | 2b0143b | 2015-03-17 22:25:59 +0000 | [diff] [blame] | 5672 | struct inode *inode = d_inode(dentry); |
Yan, Zheng | 76dda93 | 2009-09-21 16:00:26 -0400 | [diff] [blame] | 5673 | |
Li Zefan | 848cce0 | 2012-02-21 17:04:28 +0800 | [diff] [blame] | 5674 | if (!inode && !IS_ROOT(dentry)) |
David Howells | 2b0143b | 2015-03-17 22:25:59 +0000 | [diff] [blame] | 5675 | inode = d_inode(dentry->d_parent); |
Yan, Zheng | 76dda93 | 2009-09-21 16:00:26 -0400 | [diff] [blame] | 5676 | |
Li Zefan | 848cce0 | 2012-02-21 17:04:28 +0800 | [diff] [blame] | 5677 | if (inode) { |
| 5678 | root = BTRFS_I(inode)->root; |
Yan, Zheng | efefb14 | 2009-10-09 09:25:16 -0400 | [diff] [blame] | 5679 | if (btrfs_root_refs(&root->root_item) == 0) |
| 5680 | return 1; |
Li Zefan | 848cce0 | 2012-02-21 17:04:28 +0800 | [diff] [blame] | 5681 | |
| 5682 | if (btrfs_ino(inode) == BTRFS_EMPTY_SUBVOL_DIR_OBJECTID) |
| 5683 | return 1; |
Yan, Zheng | efefb14 | 2009-10-09 09:25:16 -0400 | [diff] [blame] | 5684 | } |
Yan, Zheng | 76dda93 | 2009-09-21 16:00:26 -0400 | [diff] [blame] | 5685 | return 0; |
| 5686 | } |
| 5687 | |
Josef Bacik | b4aff1f | 2011-06-28 16:18:59 -0400 | [diff] [blame] | 5688 | static void btrfs_dentry_release(struct dentry *dentry) |
| 5689 | { |
Daeseok Youn | 944a451 | 2014-04-14 15:37:02 +0900 | [diff] [blame] | 5690 | kfree(dentry->d_fsdata); |
Josef Bacik | b4aff1f | 2011-06-28 16:18:59 -0400 | [diff] [blame] | 5691 | } |
| 5692 | |
Chris Mason | 3de4586 | 2008-11-17 21:02:50 -0500 | [diff] [blame] | 5693 | static struct dentry *btrfs_lookup(struct inode *dir, struct dentry *dentry, |
Al Viro | 00cd8dd | 2012-06-10 17:13:09 -0400 | [diff] [blame] | 5694 | unsigned int flags) |
Chris Mason | 3de4586 | 2008-11-17 21:02:50 -0500 | [diff] [blame] | 5695 | { |
Tsutomu Itoh | 5662344b3 | 2013-12-13 09:51:42 +0900 | [diff] [blame] | 5696 | struct inode *inode; |
Josef Bacik | a66e7cc | 2011-09-18 10:34:03 -0400 | [diff] [blame] | 5697 | |
Tsutomu Itoh | 5662344b3 | 2013-12-13 09:51:42 +0900 | [diff] [blame] | 5698 | inode = btrfs_lookup_dentry(dir, dentry); |
| 5699 | if (IS_ERR(inode)) { |
| 5700 | if (PTR_ERR(inode) == -ENOENT) |
| 5701 | inode = NULL; |
| 5702 | else |
| 5703 | return ERR_CAST(inode); |
| 5704 | } |
| 5705 | |
Al Viro | 41d28bc | 2014-10-12 22:24:21 -0400 | [diff] [blame] | 5706 | return d_splice_alias(inode, dentry); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5707 | } |
| 5708 | |
Miao Xie | 16cdcec | 2011-04-22 18:12:22 +0800 | [diff] [blame] | 5709 | unsigned char btrfs_filetype_table[] = { |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5710 | DT_UNKNOWN, DT_REG, DT_DIR, DT_CHR, DT_BLK, DT_FIFO, DT_SOCK, DT_LNK |
| 5711 | }; |
| 5712 | |
Al Viro | 9cdda8d | 2013-05-22 16:48:09 -0400 | [diff] [blame] | 5713 | static int btrfs_real_readdir(struct file *file, struct dir_context *ctx) |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5714 | { |
Al Viro | 9cdda8d | 2013-05-22 16:48:09 -0400 | [diff] [blame] | 5715 | struct inode *inode = file_inode(file); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5716 | struct btrfs_root *root = BTRFS_I(inode)->root; |
| 5717 | struct btrfs_item *item; |
| 5718 | struct btrfs_dir_item *di; |
| 5719 | struct btrfs_key key; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 5720 | struct btrfs_key found_key; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5721 | struct btrfs_path *path; |
Miao Xie | 16cdcec | 2011-04-22 18:12:22 +0800 | [diff] [blame] | 5722 | struct list_head ins_list; |
| 5723 | struct list_head del_list; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5724 | int ret; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 5725 | struct extent_buffer *leaf; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5726 | int slot; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5727 | unsigned char d_type; |
| 5728 | int over = 0; |
| 5729 | u32 di_cur; |
| 5730 | u32 di_total; |
| 5731 | u32 di_len; |
| 5732 | int key_type = BTRFS_DIR_INDEX_KEY; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 5733 | char tmp_name[32]; |
| 5734 | char *name_ptr; |
| 5735 | int name_len; |
Al Viro | 9cdda8d | 2013-05-22 16:48:09 -0400 | [diff] [blame] | 5736 | int is_curr = 0; /* ctx->pos points to the current index? */ |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5737 | |
| 5738 | /* FIXME, use a real flag for deciding about the key type */ |
| 5739 | if (root->fs_info->tree_root == root) |
| 5740 | key_type = BTRFS_DIR_ITEM_KEY; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 5741 | |
Al Viro | 9cdda8d | 2013-05-22 16:48:09 -0400 | [diff] [blame] | 5742 | if (!dir_emit_dots(file, ctx)) |
| 5743 | return 0; |
| 5744 | |
David Woodhouse | 49593bf | 2008-08-17 17:08:36 +0100 | [diff] [blame] | 5745 | path = btrfs_alloc_path(); |
Miao Xie | 16cdcec | 2011-04-22 18:12:22 +0800 | [diff] [blame] | 5746 | if (!path) |
| 5747 | return -ENOMEM; |
Chris Mason | ff5714c | 2011-05-28 07:00:39 -0400 | [diff] [blame] | 5748 | |
Josef Bacik | 026fd31 | 2011-05-13 10:32:11 -0400 | [diff] [blame] | 5749 | path->reada = 1; |
David Woodhouse | 49593bf | 2008-08-17 17:08:36 +0100 | [diff] [blame] | 5750 | |
Miao Xie | 16cdcec | 2011-04-22 18:12:22 +0800 | [diff] [blame] | 5751 | if (key_type == BTRFS_DIR_INDEX_KEY) { |
| 5752 | INIT_LIST_HEAD(&ins_list); |
| 5753 | INIT_LIST_HEAD(&del_list); |
| 5754 | btrfs_get_delayed_items(inode, &ins_list, &del_list); |
| 5755 | } |
| 5756 | |
David Sterba | 962a298 | 2014-06-04 18:41:45 +0200 | [diff] [blame] | 5757 | key.type = key_type; |
Al Viro | 9cdda8d | 2013-05-22 16:48:09 -0400 | [diff] [blame] | 5758 | key.offset = ctx->pos; |
Li Zefan | 33345d01 | 2011-04-20 10:31:50 +0800 | [diff] [blame] | 5759 | key.objectid = btrfs_ino(inode); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 5760 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5761 | ret = btrfs_search_slot(NULL, root, &key, path, 0, 0); |
| 5762 | if (ret < 0) |
| 5763 | goto err; |
David Woodhouse | 49593bf | 2008-08-17 17:08:36 +0100 | [diff] [blame] | 5764 | |
| 5765 | while (1) { |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 5766 | leaf = path->nodes[0]; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5767 | slot = path->slots[0]; |
Li Zefan | b9e03af | 2011-03-23 10:43:58 +0800 | [diff] [blame] | 5768 | if (slot >= btrfs_header_nritems(leaf)) { |
| 5769 | ret = btrfs_next_leaf(root, path); |
| 5770 | if (ret < 0) |
| 5771 | goto err; |
| 5772 | else if (ret > 0) |
| 5773 | break; |
| 5774 | continue; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5775 | } |
Chris Mason | 3de4586 | 2008-11-17 21:02:50 -0500 | [diff] [blame] | 5776 | |
Ross Kirk | dd3cc16 | 2013-09-16 15:58:09 +0100 | [diff] [blame] | 5777 | item = btrfs_item_nr(slot); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 5778 | btrfs_item_key_to_cpu(leaf, &found_key, slot); |
| 5779 | |
| 5780 | if (found_key.objectid != key.objectid) |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5781 | break; |
David Sterba | 962a298 | 2014-06-04 18:41:45 +0200 | [diff] [blame] | 5782 | if (found_key.type != key_type) |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5783 | break; |
Al Viro | 9cdda8d | 2013-05-22 16:48:09 -0400 | [diff] [blame] | 5784 | if (found_key.offset < ctx->pos) |
Li Zefan | b9e03af | 2011-03-23 10:43:58 +0800 | [diff] [blame] | 5785 | goto next; |
Miao Xie | 16cdcec | 2011-04-22 18:12:22 +0800 | [diff] [blame] | 5786 | if (key_type == BTRFS_DIR_INDEX_KEY && |
| 5787 | btrfs_should_delete_dir_index(&del_list, |
| 5788 | found_key.offset)) |
| 5789 | goto next; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 5790 | |
Al Viro | 9cdda8d | 2013-05-22 16:48:09 -0400 | [diff] [blame] | 5791 | ctx->pos = found_key.offset; |
Miao Xie | 16cdcec | 2011-04-22 18:12:22 +0800 | [diff] [blame] | 5792 | is_curr = 1; |
David Woodhouse | 49593bf | 2008-08-17 17:08:36 +0100 | [diff] [blame] | 5793 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5794 | di = btrfs_item_ptr(leaf, slot, struct btrfs_dir_item); |
| 5795 | di_cur = 0; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 5796 | di_total = btrfs_item_size(leaf, item); |
David Woodhouse | 49593bf | 2008-08-17 17:08:36 +0100 | [diff] [blame] | 5797 | |
| 5798 | while (di_cur < di_total) { |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 5799 | struct btrfs_key location; |
| 5800 | |
Josef Bacik | 22a94d4 | 2011-03-16 16:47:17 -0400 | [diff] [blame] | 5801 | if (verify_dir_item(root, leaf, di)) |
| 5802 | break; |
| 5803 | |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 5804 | name_len = btrfs_dir_name_len(leaf, di); |
David Woodhouse | 49593bf | 2008-08-17 17:08:36 +0100 | [diff] [blame] | 5805 | if (name_len <= sizeof(tmp_name)) { |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 5806 | name_ptr = tmp_name; |
| 5807 | } else { |
| 5808 | name_ptr = kmalloc(name_len, GFP_NOFS); |
David Woodhouse | 49593bf | 2008-08-17 17:08:36 +0100 | [diff] [blame] | 5809 | if (!name_ptr) { |
| 5810 | ret = -ENOMEM; |
| 5811 | goto err; |
| 5812 | } |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 5813 | } |
| 5814 | read_extent_buffer(leaf, name_ptr, |
| 5815 | (unsigned long)(di + 1), name_len); |
| 5816 | |
| 5817 | d_type = btrfs_filetype_table[btrfs_dir_type(leaf, di)]; |
| 5818 | btrfs_dir_item_key_to_cpu(leaf, di, &location); |
Chris Mason | 3de4586 | 2008-11-17 21:02:50 -0500 | [diff] [blame] | 5819 | |
Josef Bacik | b4aff1f | 2011-06-28 16:18:59 -0400 | [diff] [blame] | 5820 | |
Chris Mason | 3de4586 | 2008-11-17 21:02:50 -0500 | [diff] [blame] | 5821 | /* is this a reference to our own snapshot? If so |
Arne Jansen | 8c9c2bf | 2012-02-25 09:09:30 +0100 | [diff] [blame] | 5822 | * skip it. |
| 5823 | * |
| 5824 | * In contrast to old kernels, we insert the snapshot's |
| 5825 | * dir item and dir index after it has been created, so |
| 5826 | * we won't find a reference to our own snapshot. We |
| 5827 | * still keep the following code for backward |
| 5828 | * compatibility. |
Chris Mason | 3de4586 | 2008-11-17 21:02:50 -0500 | [diff] [blame] | 5829 | */ |
| 5830 | if (location.type == BTRFS_ROOT_ITEM_KEY && |
| 5831 | location.objectid == root->root_key.objectid) { |
| 5832 | over = 0; |
| 5833 | goto skip; |
| 5834 | } |
Al Viro | 9cdda8d | 2013-05-22 16:48:09 -0400 | [diff] [blame] | 5835 | over = !dir_emit(ctx, name_ptr, name_len, |
| 5836 | location.objectid, d_type); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 5837 | |
Chris Mason | 3de4586 | 2008-11-17 21:02:50 -0500 | [diff] [blame] | 5838 | skip: |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 5839 | if (name_ptr != tmp_name) |
| 5840 | kfree(name_ptr); |
| 5841 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5842 | if (over) |
| 5843 | goto nopos; |
Josef Bacik | 5103e94 | 2007-11-16 11:45:54 -0500 | [diff] [blame] | 5844 | di_len = btrfs_dir_name_len(leaf, di) + |
David Woodhouse | 49593bf | 2008-08-17 17:08:36 +0100 | [diff] [blame] | 5845 | btrfs_dir_data_len(leaf, di) + sizeof(*di); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5846 | di_cur += di_len; |
| 5847 | di = (struct btrfs_dir_item *)((char *)di + di_len); |
| 5848 | } |
Li Zefan | b9e03af | 2011-03-23 10:43:58 +0800 | [diff] [blame] | 5849 | next: |
| 5850 | path->slots[0]++; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5851 | } |
David Woodhouse | 49593bf | 2008-08-17 17:08:36 +0100 | [diff] [blame] | 5852 | |
Miao Xie | 16cdcec | 2011-04-22 18:12:22 +0800 | [diff] [blame] | 5853 | if (key_type == BTRFS_DIR_INDEX_KEY) { |
| 5854 | if (is_curr) |
Al Viro | 9cdda8d | 2013-05-22 16:48:09 -0400 | [diff] [blame] | 5855 | ctx->pos++; |
| 5856 | ret = btrfs_readdir_delayed_dir_index(ctx, &ins_list); |
Miao Xie | 16cdcec | 2011-04-22 18:12:22 +0800 | [diff] [blame] | 5857 | if (ret) |
| 5858 | goto nopos; |
| 5859 | } |
| 5860 | |
David Woodhouse | 49593bf | 2008-08-17 17:08:36 +0100 | [diff] [blame] | 5861 | /* Reached end of directory/root. Bump pos past the last item. */ |
Zach Brown | db62efb | 2013-07-11 16:19:42 -0700 | [diff] [blame] | 5862 | ctx->pos++; |
| 5863 | |
| 5864 | /* |
| 5865 | * Stop new entries from being returned after we return the last |
| 5866 | * entry. |
| 5867 | * |
| 5868 | * New directory entries are assigned a strictly increasing |
| 5869 | * offset. This means that new entries created during readdir |
| 5870 | * are *guaranteed* to be seen in the future by that readdir. |
| 5871 | * This has broken buggy programs which operate on names as |
| 5872 | * they're returned by readdir. Until we re-use freed offsets |
| 5873 | * we have this hack to stop new entries from being returned |
| 5874 | * under the assumption that they'll never reach this huge |
| 5875 | * offset. |
| 5876 | * |
| 5877 | * This is being careful not to overflow 32bit loff_t unless the |
| 5878 | * last entry requires it because doing so has broken 32bit apps |
| 5879 | * in the past. |
| 5880 | */ |
| 5881 | if (key_type == BTRFS_DIR_INDEX_KEY) { |
| 5882 | if (ctx->pos >= INT_MAX) |
| 5883 | ctx->pos = LLONG_MAX; |
| 5884 | else |
| 5885 | ctx->pos = INT_MAX; |
| 5886 | } |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5887 | nopos: |
| 5888 | ret = 0; |
| 5889 | err: |
Miao Xie | 16cdcec | 2011-04-22 18:12:22 +0800 | [diff] [blame] | 5890 | if (key_type == BTRFS_DIR_INDEX_KEY) |
| 5891 | btrfs_put_delayed_items(&ins_list, &del_list); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5892 | btrfs_free_path(path); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5893 | return ret; |
| 5894 | } |
| 5895 | |
Christoph Hellwig | a9185b4 | 2010-03-05 09:21:37 +0100 | [diff] [blame] | 5896 | int btrfs_write_inode(struct inode *inode, struct writeback_control *wbc) |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5897 | { |
| 5898 | struct btrfs_root *root = BTRFS_I(inode)->root; |
| 5899 | struct btrfs_trans_handle *trans; |
| 5900 | int ret = 0; |
Josef Bacik | 0af3d00 | 2010-06-21 14:48:16 -0400 | [diff] [blame] | 5901 | bool nolock = false; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5902 | |
Josef Bacik | 72ac3c0 | 2012-05-23 14:13:11 -0400 | [diff] [blame] | 5903 | if (test_bit(BTRFS_INODE_DUMMY, &BTRFS_I(inode)->runtime_flags)) |
Chris Mason | 4ca8b41 | 2008-08-05 13:30:48 -0400 | [diff] [blame] | 5904 | return 0; |
| 5905 | |
Liu Bo | 83eea1f | 2012-07-10 05:28:39 -0600 | [diff] [blame] | 5906 | 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] | 5907 | nolock = true; |
Josef Bacik | 0af3d00 | 2010-06-21 14:48:16 -0400 | [diff] [blame] | 5908 | |
Christoph Hellwig | a9185b4 | 2010-03-05 09:21:37 +0100 | [diff] [blame] | 5909 | if (wbc->sync_mode == WB_SYNC_ALL) { |
Josef Bacik | 0af3d00 | 2010-06-21 14:48:16 -0400 | [diff] [blame] | 5910 | if (nolock) |
Josef Bacik | 7a7eaa4 | 2011-04-13 12:54:33 -0400 | [diff] [blame] | 5911 | trans = btrfs_join_transaction_nolock(root); |
Josef Bacik | 0af3d00 | 2010-06-21 14:48:16 -0400 | [diff] [blame] | 5912 | else |
Josef Bacik | 7a7eaa4 | 2011-04-13 12:54:33 -0400 | [diff] [blame] | 5913 | trans = btrfs_join_transaction(root); |
Tsutomu Itoh | 3612b49 | 2011-01-25 02:51:38 +0000 | [diff] [blame] | 5914 | if (IS_ERR(trans)) |
| 5915 | return PTR_ERR(trans); |
Miao Xie | a698d075 | 2012-09-20 01:51:59 -0600 | [diff] [blame] | 5916 | ret = btrfs_commit_transaction(trans, root); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5917 | } |
| 5918 | return ret; |
| 5919 | } |
| 5920 | |
| 5921 | /* |
Chris Mason | 54aa1f4 | 2007-06-22 14:16:25 -0400 | [diff] [blame] | 5922 | * This is somewhat expensive, updating the tree every time the |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5923 | * inode changes. But, it is most likely to find the inode in cache. |
| 5924 | * FIXME, needs more benchmarking...there are no reasons other than performance |
| 5925 | * to keep or drop this code. |
| 5926 | */ |
Eric Sandeen | 48a3b63 | 2013-04-25 20:41:01 +0000 | [diff] [blame] | 5927 | static int btrfs_dirty_inode(struct inode *inode) |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5928 | { |
| 5929 | struct btrfs_root *root = BTRFS_I(inode)->root; |
| 5930 | struct btrfs_trans_handle *trans; |
Yan, Zheng | 8929ecfa | 2010-05-16 10:49:58 -0400 | [diff] [blame] | 5931 | int ret; |
| 5932 | |
Josef Bacik | 72ac3c0 | 2012-05-23 14:13:11 -0400 | [diff] [blame] | 5933 | if (test_bit(BTRFS_INODE_DUMMY, &BTRFS_I(inode)->runtime_flags)) |
Josef Bacik | 22c44fe | 2011-11-30 10:45:38 -0500 | [diff] [blame] | 5934 | return 0; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5935 | |
Josef Bacik | 7a7eaa4 | 2011-04-13 12:54:33 -0400 | [diff] [blame] | 5936 | trans = btrfs_join_transaction(root); |
Josef Bacik | 22c44fe | 2011-11-30 10:45:38 -0500 | [diff] [blame] | 5937 | if (IS_ERR(trans)) |
| 5938 | return PTR_ERR(trans); |
Yan, Zheng | 8929ecfa | 2010-05-16 10:49:58 -0400 | [diff] [blame] | 5939 | |
| 5940 | ret = btrfs_update_inode(trans, root, inode); |
Chris Mason | 94b6044 | 2010-05-26 11:02:00 -0400 | [diff] [blame] | 5941 | if (ret && ret == -ENOSPC) { |
| 5942 | /* whoops, lets try again with the full transaction */ |
| 5943 | btrfs_end_transaction(trans, root); |
| 5944 | trans = btrfs_start_transaction(root, 1); |
Josef Bacik | 22c44fe | 2011-11-30 10:45:38 -0500 | [diff] [blame] | 5945 | if (IS_ERR(trans)) |
| 5946 | return PTR_ERR(trans); |
Yan, Zheng | 8929ecfa | 2010-05-16 10:49:58 -0400 | [diff] [blame] | 5947 | |
Chris Mason | 94b6044 | 2010-05-26 11:02:00 -0400 | [diff] [blame] | 5948 | ret = btrfs_update_inode(trans, root, inode); |
Chris Mason | 94b6044 | 2010-05-26 11:02:00 -0400 | [diff] [blame] | 5949 | } |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5950 | btrfs_end_transaction(trans, root); |
Miao Xie | 16cdcec | 2011-04-22 18:12:22 +0800 | [diff] [blame] | 5951 | if (BTRFS_I(inode)->delayed_node) |
| 5952 | btrfs_balance_delayed_items(root); |
Josef Bacik | 22c44fe | 2011-11-30 10:45:38 -0500 | [diff] [blame] | 5953 | |
| 5954 | return ret; |
| 5955 | } |
| 5956 | |
| 5957 | /* |
| 5958 | * This is a copy of file_update_time. We need this so we can return error on |
| 5959 | * ENOSPC for updating the inode in the case of file write and mmap writes. |
| 5960 | */ |
Josef Bacik | e41f941 | 2012-03-26 09:46:47 -0400 | [diff] [blame] | 5961 | static int btrfs_update_time(struct inode *inode, struct timespec *now, |
| 5962 | int flags) |
Josef Bacik | 22c44fe | 2011-11-30 10:45:38 -0500 | [diff] [blame] | 5963 | { |
Alexander Block | 2bc556528 | 2012-06-15 09:49:33 +0200 | [diff] [blame] | 5964 | struct btrfs_root *root = BTRFS_I(inode)->root; |
| 5965 | |
| 5966 | if (btrfs_root_readonly(root)) |
| 5967 | return -EROFS; |
| 5968 | |
Josef Bacik | e41f941 | 2012-03-26 09:46:47 -0400 | [diff] [blame] | 5969 | if (flags & S_VERSION) |
Josef Bacik | 22c44fe | 2011-11-30 10:45:38 -0500 | [diff] [blame] | 5970 | inode_inc_iversion(inode); |
Josef Bacik | e41f941 | 2012-03-26 09:46:47 -0400 | [diff] [blame] | 5971 | if (flags & S_CTIME) |
| 5972 | inode->i_ctime = *now; |
| 5973 | if (flags & S_MTIME) |
| 5974 | inode->i_mtime = *now; |
| 5975 | if (flags & S_ATIME) |
| 5976 | inode->i_atime = *now; |
| 5977 | return btrfs_dirty_inode(inode); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5978 | } |
| 5979 | |
Chris Mason | d352ac6 | 2008-09-29 15:18:18 -0400 | [diff] [blame] | 5980 | /* |
| 5981 | * find the highest existing sequence number in a directory |
| 5982 | * and then set the in-memory index_cnt variable to reflect |
| 5983 | * free sequence numbers |
| 5984 | */ |
Josef Bacik | aec7477 | 2008-07-24 12:12:38 -0400 | [diff] [blame] | 5985 | static int btrfs_set_inode_index_count(struct inode *inode) |
| 5986 | { |
| 5987 | struct btrfs_root *root = BTRFS_I(inode)->root; |
| 5988 | struct btrfs_key key, found_key; |
| 5989 | struct btrfs_path *path; |
| 5990 | struct extent_buffer *leaf; |
| 5991 | int ret; |
| 5992 | |
Li Zefan | 33345d01 | 2011-04-20 10:31:50 +0800 | [diff] [blame] | 5993 | key.objectid = btrfs_ino(inode); |
David Sterba | 962a298 | 2014-06-04 18:41:45 +0200 | [diff] [blame] | 5994 | key.type = BTRFS_DIR_INDEX_KEY; |
Josef Bacik | aec7477 | 2008-07-24 12:12:38 -0400 | [diff] [blame] | 5995 | key.offset = (u64)-1; |
| 5996 | |
| 5997 | path = btrfs_alloc_path(); |
| 5998 | if (!path) |
| 5999 | return -ENOMEM; |
| 6000 | |
| 6001 | ret = btrfs_search_slot(NULL, root, &key, path, 0, 0); |
| 6002 | if (ret < 0) |
| 6003 | goto out; |
| 6004 | /* FIXME: we should be able to handle this */ |
| 6005 | if (ret == 0) |
| 6006 | goto out; |
| 6007 | ret = 0; |
| 6008 | |
| 6009 | /* |
| 6010 | * MAGIC NUMBER EXPLANATION: |
| 6011 | * since we search a directory based on f_pos we have to start at 2 |
| 6012 | * since '.' and '..' have f_pos of 0 and 1 respectively, so everybody |
| 6013 | * else has to start at 2 |
| 6014 | */ |
| 6015 | if (path->slots[0] == 0) { |
| 6016 | BTRFS_I(inode)->index_cnt = 2; |
| 6017 | goto out; |
| 6018 | } |
| 6019 | |
| 6020 | path->slots[0]--; |
| 6021 | |
| 6022 | leaf = path->nodes[0]; |
| 6023 | btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]); |
| 6024 | |
Li Zefan | 33345d01 | 2011-04-20 10:31:50 +0800 | [diff] [blame] | 6025 | if (found_key.objectid != btrfs_ino(inode) || |
David Sterba | 962a298 | 2014-06-04 18:41:45 +0200 | [diff] [blame] | 6026 | found_key.type != BTRFS_DIR_INDEX_KEY) { |
Josef Bacik | aec7477 | 2008-07-24 12:12:38 -0400 | [diff] [blame] | 6027 | BTRFS_I(inode)->index_cnt = 2; |
| 6028 | goto out; |
| 6029 | } |
| 6030 | |
| 6031 | BTRFS_I(inode)->index_cnt = found_key.offset + 1; |
| 6032 | out: |
| 6033 | btrfs_free_path(path); |
| 6034 | return ret; |
| 6035 | } |
| 6036 | |
Chris Mason | d352ac6 | 2008-09-29 15:18:18 -0400 | [diff] [blame] | 6037 | /* |
| 6038 | * helper to find a free sequence number in a given directory. This current |
| 6039 | * code is very simple, later versions will do smarter things in the btree |
| 6040 | */ |
Chris Mason | 3de4586 | 2008-11-17 21:02:50 -0500 | [diff] [blame] | 6041 | int btrfs_set_inode_index(struct inode *dir, u64 *index) |
Josef Bacik | aec7477 | 2008-07-24 12:12:38 -0400 | [diff] [blame] | 6042 | { |
| 6043 | int ret = 0; |
| 6044 | |
| 6045 | if (BTRFS_I(dir)->index_cnt == (u64)-1) { |
Miao Xie | 16cdcec | 2011-04-22 18:12:22 +0800 | [diff] [blame] | 6046 | ret = btrfs_inode_delayed_dir_index_count(dir); |
| 6047 | if (ret) { |
| 6048 | ret = btrfs_set_inode_index_count(dir); |
| 6049 | if (ret) |
| 6050 | return ret; |
| 6051 | } |
Josef Bacik | aec7477 | 2008-07-24 12:12:38 -0400 | [diff] [blame] | 6052 | } |
| 6053 | |
Chris Mason | 00e4e6b | 2008-08-05 11:18:09 -0400 | [diff] [blame] | 6054 | *index = BTRFS_I(dir)->index_cnt; |
Josef Bacik | aec7477 | 2008-07-24 12:12:38 -0400 | [diff] [blame] | 6055 | BTRFS_I(dir)->index_cnt++; |
| 6056 | |
| 6057 | return ret; |
| 6058 | } |
| 6059 | |
Chris Mason | b0d5d10 | 2014-09-08 13:08:51 -0700 | [diff] [blame] | 6060 | static int btrfs_insert_inode_locked(struct inode *inode) |
| 6061 | { |
| 6062 | struct btrfs_iget_args args; |
| 6063 | args.location = &BTRFS_I(inode)->location; |
| 6064 | args.root = BTRFS_I(inode)->root; |
| 6065 | |
| 6066 | return insert_inode_locked4(inode, |
| 6067 | btrfs_inode_hash(inode->i_ino, BTRFS_I(inode)->root), |
| 6068 | btrfs_find_actor, &args); |
| 6069 | } |
| 6070 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 6071 | static struct inode *btrfs_new_inode(struct btrfs_trans_handle *trans, |
| 6072 | struct btrfs_root *root, |
Josef Bacik | aec7477 | 2008-07-24 12:12:38 -0400 | [diff] [blame] | 6073 | struct inode *dir, |
Chris Mason | 9c58309 | 2008-01-29 15:15:18 -0500 | [diff] [blame] | 6074 | const char *name, int name_len, |
Al Viro | 175a4eb | 2011-07-26 03:30:54 -0400 | [diff] [blame] | 6075 | u64 ref_objectid, u64 objectid, |
| 6076 | umode_t mode, u64 *index) |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 6077 | { |
| 6078 | struct inode *inode; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 6079 | struct btrfs_inode_item *inode_item; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 6080 | struct btrfs_key *location; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 6081 | struct btrfs_path *path; |
Chris Mason | 9c58309 | 2008-01-29 15:15:18 -0500 | [diff] [blame] | 6082 | struct btrfs_inode_ref *ref; |
| 6083 | struct btrfs_key key[2]; |
| 6084 | u32 sizes[2]; |
Filipe Manana | ef3b9af | 2014-04-27 20:40:45 +0100 | [diff] [blame] | 6085 | int nitems = name ? 2 : 1; |
Chris Mason | 9c58309 | 2008-01-29 15:15:18 -0500 | [diff] [blame] | 6086 | unsigned long ptr; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 6087 | int ret; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 6088 | |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 6089 | path = btrfs_alloc_path(); |
Mark Fasheh | d8926bb | 2011-07-13 10:38:47 -0700 | [diff] [blame] | 6090 | if (!path) |
| 6091 | return ERR_PTR(-ENOMEM); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 6092 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 6093 | inode = new_inode(root->fs_info->sb); |
Yoshinori Sano | 8fb2764 | 2011-04-09 02:30:07 +0000 | [diff] [blame] | 6094 | if (!inode) { |
| 6095 | btrfs_free_path(path); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 6096 | return ERR_PTR(-ENOMEM); |
Yoshinori Sano | 8fb2764 | 2011-04-09 02:30:07 +0000 | [diff] [blame] | 6097 | } |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 6098 | |
Li Zefan | 581bb05 | 2011-04-20 10:06:11 +0800 | [diff] [blame] | 6099 | /* |
Filipe Manana | 5762b5c | 2014-08-01 00:10:32 +0100 | [diff] [blame] | 6100 | * O_TMPFILE, set link count to 0, so that after this point, |
| 6101 | * we fill in an inode item with the correct link count. |
| 6102 | */ |
| 6103 | if (!name) |
| 6104 | set_nlink(inode, 0); |
| 6105 | |
| 6106 | /* |
Li Zefan | 581bb05 | 2011-04-20 10:06:11 +0800 | [diff] [blame] | 6107 | * we have to initialize this early, so we can reclaim the inode |
| 6108 | * number if we fail afterwards in this function. |
| 6109 | */ |
| 6110 | inode->i_ino = objectid; |
| 6111 | |
Filipe Manana | ef3b9af | 2014-04-27 20:40:45 +0100 | [diff] [blame] | 6112 | if (dir && name) { |
liubo | 1abe9b8 | 2011-03-24 11:18:59 +0000 | [diff] [blame] | 6113 | trace_btrfs_inode_request(dir); |
| 6114 | |
Chris Mason | 3de4586 | 2008-11-17 21:02:50 -0500 | [diff] [blame] | 6115 | ret = btrfs_set_inode_index(dir, index); |
Shen Feng | 0977143 | 2009-04-02 16:46:06 -0400 | [diff] [blame] | 6116 | if (ret) { |
Yoshinori Sano | 8fb2764 | 2011-04-09 02:30:07 +0000 | [diff] [blame] | 6117 | btrfs_free_path(path); |
Shen Feng | 0977143 | 2009-04-02 16:46:06 -0400 | [diff] [blame] | 6118 | iput(inode); |
Josef Bacik | aec7477 | 2008-07-24 12:12:38 -0400 | [diff] [blame] | 6119 | return ERR_PTR(ret); |
Shen Feng | 0977143 | 2009-04-02 16:46:06 -0400 | [diff] [blame] | 6120 | } |
Filipe Manana | ef3b9af | 2014-04-27 20:40:45 +0100 | [diff] [blame] | 6121 | } else if (dir) { |
| 6122 | *index = 0; |
Josef Bacik | aec7477 | 2008-07-24 12:12:38 -0400 | [diff] [blame] | 6123 | } |
| 6124 | /* |
| 6125 | * index_cnt is ignored for everything but a dir, |
| 6126 | * btrfs_get_inode_index_count has an explanation for the magic |
| 6127 | * number |
| 6128 | */ |
| 6129 | BTRFS_I(inode)->index_cnt = 2; |
Miao Xie | 67de117 | 2013-12-26 13:07:06 +0800 | [diff] [blame] | 6130 | BTRFS_I(inode)->dir_index = *index; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 6131 | BTRFS_I(inode)->root = root; |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 6132 | BTRFS_I(inode)->generation = trans->transid; |
Josef Bacik | 7619585 | 2010-11-19 02:18:02 +0000 | [diff] [blame] | 6133 | inode->i_generation = BTRFS_I(inode)->generation; |
Chris Mason | b888db2 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 6134 | |
Josef Bacik | 5dc562c | 2012-08-17 13:14:17 -0400 | [diff] [blame] | 6135 | /* |
| 6136 | * We could have gotten an inode number from somebody who was fsynced |
| 6137 | * and then removed in this same transaction, so let's just set full |
| 6138 | * sync since it will be a full sync anyway and this will blow away the |
| 6139 | * old info in the log. |
| 6140 | */ |
| 6141 | set_bit(BTRFS_INODE_NEEDS_FULL_SYNC, &BTRFS_I(inode)->runtime_flags); |
| 6142 | |
Chris Mason | 9c58309 | 2008-01-29 15:15:18 -0500 | [diff] [blame] | 6143 | key[0].objectid = objectid; |
David Sterba | 962a298 | 2014-06-04 18:41:45 +0200 | [diff] [blame] | 6144 | key[0].type = BTRFS_INODE_ITEM_KEY; |
Chris Mason | 9c58309 | 2008-01-29 15:15:18 -0500 | [diff] [blame] | 6145 | key[0].offset = 0; |
| 6146 | |
Chris Mason | 9c58309 | 2008-01-29 15:15:18 -0500 | [diff] [blame] | 6147 | sizes[0] = sizeof(struct btrfs_inode_item); |
Filipe Manana | ef3b9af | 2014-04-27 20:40:45 +0100 | [diff] [blame] | 6148 | |
| 6149 | if (name) { |
| 6150 | /* |
| 6151 | * Start new inodes with an inode_ref. This is slightly more |
| 6152 | * efficient for small numbers of hard links since they will |
| 6153 | * be packed into one item. Extended refs will kick in if we |
| 6154 | * add more hard links than can fit in the ref item. |
| 6155 | */ |
| 6156 | key[1].objectid = objectid; |
David Sterba | 962a298 | 2014-06-04 18:41:45 +0200 | [diff] [blame] | 6157 | key[1].type = BTRFS_INODE_REF_KEY; |
Filipe Manana | ef3b9af | 2014-04-27 20:40:45 +0100 | [diff] [blame] | 6158 | key[1].offset = ref_objectid; |
| 6159 | |
| 6160 | sizes[1] = name_len + sizeof(*ref); |
| 6161 | } |
Chris Mason | 9c58309 | 2008-01-29 15:15:18 -0500 | [diff] [blame] | 6162 | |
Chris Mason | b0d5d10 | 2014-09-08 13:08:51 -0700 | [diff] [blame] | 6163 | location = &BTRFS_I(inode)->location; |
| 6164 | location->objectid = objectid; |
| 6165 | location->offset = 0; |
David Sterba | 962a298 | 2014-06-04 18:41:45 +0200 | [diff] [blame] | 6166 | location->type = BTRFS_INODE_ITEM_KEY; |
Chris Mason | b0d5d10 | 2014-09-08 13:08:51 -0700 | [diff] [blame] | 6167 | |
| 6168 | ret = btrfs_insert_inode_locked(inode); |
| 6169 | if (ret < 0) |
| 6170 | goto fail; |
| 6171 | |
Chris Mason | b947343 | 2009-03-13 11:00:37 -0400 | [diff] [blame] | 6172 | path->leave_spinning = 1; |
Filipe Manana | ef3b9af | 2014-04-27 20:40:45 +0100 | [diff] [blame] | 6173 | ret = btrfs_insert_empty_items(trans, root, path, key, sizes, nitems); |
Chris Mason | 9c58309 | 2008-01-29 15:15:18 -0500 | [diff] [blame] | 6174 | if (ret != 0) |
Chris Mason | b0d5d10 | 2014-09-08 13:08:51 -0700 | [diff] [blame] | 6175 | goto fail_unlock; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 6176 | |
Dmitry Monakhov | ecc11fab | 2010-03-04 17:31:47 +0300 | [diff] [blame] | 6177 | inode_init_owner(inode, dir, mode); |
Yan Zheng | a76a3cd | 2008-10-09 11:46:29 -0400 | [diff] [blame] | 6178 | inode_set_bytes(inode, 0); |
chandan r | 9cc97d6 | 2012-07-04 12:48:07 +0530 | [diff] [blame] | 6179 | |
| 6180 | inode->i_mtime = CURRENT_TIME; |
| 6181 | inode->i_atime = inode->i_mtime; |
| 6182 | inode->i_ctime = inode->i_mtime; |
| 6183 | BTRFS_I(inode)->i_otime = inode->i_mtime; |
| 6184 | |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 6185 | inode_item = btrfs_item_ptr(path->nodes[0], path->slots[0], |
| 6186 | struct btrfs_inode_item); |
Li Zefan | 293f7e0 | 2012-07-10 00:58:58 -0600 | [diff] [blame] | 6187 | memset_extent_buffer(path->nodes[0], 0, (unsigned long)inode_item, |
| 6188 | sizeof(*inode_item)); |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 6189 | fill_inode_item(trans, path->nodes[0], inode_item, inode); |
Chris Mason | 9c58309 | 2008-01-29 15:15:18 -0500 | [diff] [blame] | 6190 | |
Filipe Manana | ef3b9af | 2014-04-27 20:40:45 +0100 | [diff] [blame] | 6191 | if (name) { |
| 6192 | ref = btrfs_item_ptr(path->nodes[0], path->slots[0] + 1, |
| 6193 | struct btrfs_inode_ref); |
| 6194 | btrfs_set_inode_ref_name_len(path->nodes[0], ref, name_len); |
| 6195 | btrfs_set_inode_ref_index(path->nodes[0], ref, *index); |
| 6196 | ptr = (unsigned long)(ref + 1); |
| 6197 | write_extent_buffer(path->nodes[0], name, ptr, name_len); |
| 6198 | } |
Chris Mason | 9c58309 | 2008-01-29 15:15:18 -0500 | [diff] [blame] | 6199 | |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 6200 | btrfs_mark_buffer_dirty(path->nodes[0]); |
| 6201 | btrfs_free_path(path); |
| 6202 | |
Christoph Hellwig | 6cbff00 | 2009-04-17 10:37:41 +0200 | [diff] [blame] | 6203 | btrfs_inherit_iflags(inode, dir); |
| 6204 | |
Al Viro | 569254b | 2011-07-24 17:08:40 -0400 | [diff] [blame] | 6205 | if (S_ISREG(mode)) { |
Chris Mason | 9427216 | 2009-07-02 12:26:06 -0400 | [diff] [blame] | 6206 | if (btrfs_test_opt(root, NODATASUM)) |
| 6207 | BTRFS_I(inode)->flags |= BTRFS_INODE_NODATASUM; |
Liu Bo | 213490b | 2012-09-11 08:33:50 -0600 | [diff] [blame] | 6208 | if (btrfs_test_opt(root, NODATACOW)) |
Josef Bacik | f2bdf9a | 2013-02-21 15:28:28 -0500 | [diff] [blame] | 6209 | BTRFS_I(inode)->flags |= BTRFS_INODE_NODATACOW | |
| 6210 | BTRFS_INODE_NODATASUM; |
Chris Mason | 9427216 | 2009-07-02 12:26:06 -0400 | [diff] [blame] | 6211 | } |
| 6212 | |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 6213 | inode_tree_add(inode); |
liubo | 1abe9b8 | 2011-03-24 11:18:59 +0000 | [diff] [blame] | 6214 | |
| 6215 | trace_btrfs_inode_new(inode); |
Chris Mason | 1973f0f | 2011-06-24 13:13:29 -0400 | [diff] [blame] | 6216 | btrfs_set_inode_last_trans(trans, inode); |
liubo | 1abe9b8 | 2011-03-24 11:18:59 +0000 | [diff] [blame] | 6217 | |
Alexander Block | 8ea05e3 | 2012-07-25 17:35:53 +0200 | [diff] [blame] | 6218 | btrfs_update_root_times(trans, root); |
| 6219 | |
Filipe David Borba Manana | 6354192 | 2014-01-07 11:47:46 +0000 | [diff] [blame] | 6220 | ret = btrfs_inode_inherit_props(trans, inode, dir); |
| 6221 | if (ret) |
| 6222 | btrfs_err(root->fs_info, |
| 6223 | "error inheriting props for ino %llu (root %llu): %d", |
| 6224 | btrfs_ino(inode), root->root_key.objectid, ret); |
| 6225 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 6226 | return inode; |
Chris Mason | b0d5d10 | 2014-09-08 13:08:51 -0700 | [diff] [blame] | 6227 | |
| 6228 | fail_unlock: |
| 6229 | unlock_new_inode(inode); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 6230 | fail: |
Filipe Manana | ef3b9af | 2014-04-27 20:40:45 +0100 | [diff] [blame] | 6231 | if (dir && name) |
Josef Bacik | aec7477 | 2008-07-24 12:12:38 -0400 | [diff] [blame] | 6232 | BTRFS_I(dir)->index_cnt--; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 6233 | btrfs_free_path(path); |
Shen Feng | 0977143 | 2009-04-02 16:46:06 -0400 | [diff] [blame] | 6234 | iput(inode); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 6235 | return ERR_PTR(ret); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 6236 | } |
| 6237 | |
| 6238 | static inline u8 btrfs_inode_type(struct inode *inode) |
| 6239 | { |
| 6240 | return btrfs_type_by_mode[(inode->i_mode & S_IFMT) >> S_SHIFT]; |
| 6241 | } |
| 6242 | |
Chris Mason | d352ac6 | 2008-09-29 15:18:18 -0400 | [diff] [blame] | 6243 | /* |
| 6244 | * utility function to add 'inode' into 'parent_inode' with |
| 6245 | * a give name and a given sequence number. |
| 6246 | * if 'add_backref' is true, also insert a backref from the |
| 6247 | * inode to the parent directory. |
| 6248 | */ |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 6249 | int btrfs_add_link(struct btrfs_trans_handle *trans, |
| 6250 | struct inode *parent_inode, struct inode *inode, |
| 6251 | const char *name, int name_len, int add_backref, u64 index) |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 6252 | { |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 6253 | int ret = 0; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 6254 | struct btrfs_key key; |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 6255 | struct btrfs_root *root = BTRFS_I(parent_inode)->root; |
Li Zefan | 33345d01 | 2011-04-20 10:31:50 +0800 | [diff] [blame] | 6256 | u64 ino = btrfs_ino(inode); |
| 6257 | u64 parent_ino = btrfs_ino(parent_inode); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 6258 | |
Li Zefan | 33345d01 | 2011-04-20 10:31:50 +0800 | [diff] [blame] | 6259 | if (unlikely(ino == BTRFS_FIRST_FREE_OBJECTID)) { |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 6260 | memcpy(&key, &BTRFS_I(inode)->root->root_key, sizeof(key)); |
| 6261 | } else { |
Li Zefan | 33345d01 | 2011-04-20 10:31:50 +0800 | [diff] [blame] | 6262 | key.objectid = ino; |
David Sterba | 962a298 | 2014-06-04 18:41:45 +0200 | [diff] [blame] | 6263 | key.type = BTRFS_INODE_ITEM_KEY; |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 6264 | key.offset = 0; |
| 6265 | } |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 6266 | |
Li Zefan | 33345d01 | 2011-04-20 10:31:50 +0800 | [diff] [blame] | 6267 | if (unlikely(ino == BTRFS_FIRST_FREE_OBJECTID)) { |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 6268 | ret = btrfs_add_root_ref(trans, root->fs_info->tree_root, |
| 6269 | key.objectid, root->root_key.objectid, |
Li Zefan | 33345d01 | 2011-04-20 10:31:50 +0800 | [diff] [blame] | 6270 | parent_ino, index, name, name_len); |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 6271 | } else if (add_backref) { |
Li Zefan | 33345d01 | 2011-04-20 10:31:50 +0800 | [diff] [blame] | 6272 | ret = btrfs_insert_inode_ref(trans, root, name, name_len, ino, |
| 6273 | parent_ino, index); |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 6274 | } |
| 6275 | |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 6276 | /* Nothing to clean up yet */ |
| 6277 | if (ret) |
| 6278 | return ret; |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 6279 | |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 6280 | ret = btrfs_insert_dir_item(trans, root, name, name_len, |
| 6281 | parent_inode, &key, |
| 6282 | btrfs_inode_type(inode), index); |
Chris Mason | 9c52057 | 2012-12-17 14:26:57 -0500 | [diff] [blame] | 6283 | if (ret == -EEXIST || ret == -EOVERFLOW) |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 6284 | goto fail_dir_item; |
| 6285 | else if (ret) { |
| 6286 | btrfs_abort_transaction(trans, root, ret); |
| 6287 | return ret; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 6288 | } |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 6289 | |
| 6290 | btrfs_i_size_write(parent_inode, parent_inode->i_size + |
| 6291 | name_len * 2); |
Josef Bacik | 0c4d2d9 | 2012-04-05 15:03:02 -0400 | [diff] [blame] | 6292 | inode_inc_iversion(parent_inode); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 6293 | parent_inode->i_mtime = parent_inode->i_ctime = CURRENT_TIME; |
| 6294 | ret = btrfs_update_inode(trans, root, parent_inode); |
| 6295 | if (ret) |
| 6296 | btrfs_abort_transaction(trans, root, ret); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 6297 | return ret; |
Chris Mason | fe66a05 | 2012-02-20 08:40:56 -0500 | [diff] [blame] | 6298 | |
| 6299 | fail_dir_item: |
| 6300 | if (unlikely(ino == BTRFS_FIRST_FREE_OBJECTID)) { |
| 6301 | u64 local_index; |
| 6302 | int err; |
| 6303 | err = btrfs_del_root_ref(trans, root->fs_info->tree_root, |
| 6304 | key.objectid, root->root_key.objectid, |
| 6305 | parent_ino, &local_index, name, name_len); |
| 6306 | |
| 6307 | } else if (add_backref) { |
| 6308 | u64 local_index; |
| 6309 | int err; |
| 6310 | |
| 6311 | err = btrfs_del_inode_ref(trans, root, name, name_len, |
| 6312 | ino, parent_ino, &local_index); |
| 6313 | } |
| 6314 | return ret; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 6315 | } |
| 6316 | |
| 6317 | static int btrfs_add_nondir(struct btrfs_trans_handle *trans, |
Josef Bacik | a1b075d | 2010-11-19 20:36:11 +0000 | [diff] [blame] | 6318 | struct inode *dir, struct dentry *dentry, |
| 6319 | struct inode *inode, int backref, u64 index) |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 6320 | { |
Josef Bacik | a1b075d | 2010-11-19 20:36:11 +0000 | [diff] [blame] | 6321 | int err = btrfs_add_link(trans, dir, inode, |
| 6322 | dentry->d_name.name, dentry->d_name.len, |
| 6323 | backref, index); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 6324 | if (err > 0) |
| 6325 | err = -EEXIST; |
| 6326 | return err; |
| 6327 | } |
| 6328 | |
Josef Bacik | 618e21d | 2007-07-11 10:18:17 -0400 | [diff] [blame] | 6329 | static int btrfs_mknod(struct inode *dir, struct dentry *dentry, |
Al Viro | 1a67aaf | 2011-07-26 01:52:52 -0400 | [diff] [blame] | 6330 | umode_t mode, dev_t rdev) |
Josef Bacik | 618e21d | 2007-07-11 10:18:17 -0400 | [diff] [blame] | 6331 | { |
| 6332 | struct btrfs_trans_handle *trans; |
| 6333 | struct btrfs_root *root = BTRFS_I(dir)->root; |
Chris Mason | 1832a6d | 2007-12-21 16:27:21 -0500 | [diff] [blame] | 6334 | struct inode *inode = NULL; |
Josef Bacik | 618e21d | 2007-07-11 10:18:17 -0400 | [diff] [blame] | 6335 | int err; |
| 6336 | int drop_inode = 0; |
| 6337 | u64 objectid; |
Chris Mason | 00e4e6b | 2008-08-05 11:18:09 -0400 | [diff] [blame] | 6338 | u64 index = 0; |
Josef Bacik | 618e21d | 2007-07-11 10:18:17 -0400 | [diff] [blame] | 6339 | |
| 6340 | if (!new_valid_dev(rdev)) |
| 6341 | return -EINVAL; |
| 6342 | |
Josef Bacik | 9ed74f2 | 2009-09-11 16:12:44 -0400 | [diff] [blame] | 6343 | /* |
| 6344 | * 2 for inode item and ref |
| 6345 | * 2 for dir items |
| 6346 | * 1 for xattr if selinux is on |
| 6347 | */ |
Yan, Zheng | a22285a | 2010-05-16 10:48:46 -0400 | [diff] [blame] | 6348 | trans = btrfs_start_transaction(root, 5); |
| 6349 | if (IS_ERR(trans)) |
| 6350 | return PTR_ERR(trans); |
Chris Mason | 1832a6d | 2007-12-21 16:27:21 -0500 | [diff] [blame] | 6351 | |
Li Zefan | 581bb05 | 2011-04-20 10:06:11 +0800 | [diff] [blame] | 6352 | err = btrfs_find_free_ino(root, &objectid); |
| 6353 | if (err) |
| 6354 | goto out_unlock; |
| 6355 | |
Josef Bacik | aec7477 | 2008-07-24 12:12:38 -0400 | [diff] [blame] | 6356 | inode = btrfs_new_inode(trans, root, dir, dentry->d_name.name, |
Li Zefan | 33345d01 | 2011-04-20 10:31:50 +0800 | [diff] [blame] | 6357 | dentry->d_name.len, btrfs_ino(dir), objectid, |
Josef Bacik | d82a6f1d | 2011-05-11 15:26:06 -0400 | [diff] [blame] | 6358 | mode, &index); |
Tsutomu Itoh | 7cf96da | 2011-04-25 19:43:53 -0400 | [diff] [blame] | 6359 | if (IS_ERR(inode)) { |
| 6360 | err = PTR_ERR(inode); |
Josef Bacik | 618e21d | 2007-07-11 10:18:17 -0400 | [diff] [blame] | 6361 | goto out_unlock; |
Tsutomu Itoh | 7cf96da | 2011-04-25 19:43:53 -0400 | [diff] [blame] | 6362 | } |
Josef Bacik | 618e21d | 2007-07-11 10:18:17 -0400 | [diff] [blame] | 6363 | |
Casey Schaufler | ad19db7 | 2011-12-15 10:09:07 -0500 | [diff] [blame] | 6364 | /* |
| 6365 | * If the active LSM wants to access the inode during |
| 6366 | * d_instantiate it needs these. Smack checks to see |
| 6367 | * if the filesystem supports xattrs by looking at the |
| 6368 | * ops vector. |
| 6369 | */ |
Casey Schaufler | ad19db7 | 2011-12-15 10:09:07 -0500 | [diff] [blame] | 6370 | inode->i_op = &btrfs_special_inode_operations; |
Chris Mason | b0d5d10 | 2014-09-08 13:08:51 -0700 | [diff] [blame] | 6371 | init_special_inode(inode, inode->i_mode, rdev); |
| 6372 | |
| 6373 | err = btrfs_init_inode_security(trans, inode, dir, &dentry->d_name); |
Josef Bacik | 618e21d | 2007-07-11 10:18:17 -0400 | [diff] [blame] | 6374 | if (err) |
Chris Mason | b0d5d10 | 2014-09-08 13:08:51 -0700 | [diff] [blame] | 6375 | goto out_unlock_inode; |
| 6376 | |
| 6377 | err = btrfs_add_nondir(trans, dir, dentry, inode, 0, index); |
| 6378 | if (err) { |
| 6379 | goto out_unlock_inode; |
| 6380 | } else { |
Yan | 1b4ab1b | 2007-08-29 09:11:44 -0400 | [diff] [blame] | 6381 | btrfs_update_inode(trans, root, inode); |
Chris Mason | b0d5d10 | 2014-09-08 13:08:51 -0700 | [diff] [blame] | 6382 | unlock_new_inode(inode); |
Al Viro | 08c422c | 2011-12-23 07:58:13 -0500 | [diff] [blame] | 6383 | d_instantiate(dentry, inode); |
Josef Bacik | 618e21d | 2007-07-11 10:18:17 -0400 | [diff] [blame] | 6384 | } |
Chris Mason | b0d5d10 | 2014-09-08 13:08:51 -0700 | [diff] [blame] | 6385 | |
Josef Bacik | 618e21d | 2007-07-11 10:18:17 -0400 | [diff] [blame] | 6386 | out_unlock: |
Josef Bacik | 7ad85bb | 2012-01-12 19:10:12 -0500 | [diff] [blame] | 6387 | btrfs_end_transaction(trans, root); |
Josef Bacik | c581afc | 2014-02-06 16:06:06 -0500 | [diff] [blame] | 6388 | btrfs_balance_delayed_items(root); |
Liu Bo | b53d3f5 | 2012-11-14 14:34:34 +0000 | [diff] [blame] | 6389 | btrfs_btree_balance_dirty(root); |
Josef Bacik | 618e21d | 2007-07-11 10:18:17 -0400 | [diff] [blame] | 6390 | if (drop_inode) { |
| 6391 | inode_dec_link_count(inode); |
| 6392 | iput(inode); |
| 6393 | } |
Josef Bacik | 618e21d | 2007-07-11 10:18:17 -0400 | [diff] [blame] | 6394 | return err; |
Chris Mason | b0d5d10 | 2014-09-08 13:08:51 -0700 | [diff] [blame] | 6395 | |
| 6396 | out_unlock_inode: |
| 6397 | drop_inode = 1; |
| 6398 | unlock_new_inode(inode); |
| 6399 | goto out_unlock; |
| 6400 | |
Josef Bacik | 618e21d | 2007-07-11 10:18:17 -0400 | [diff] [blame] | 6401 | } |
| 6402 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 6403 | static int btrfs_create(struct inode *dir, struct dentry *dentry, |
Al Viro | ebfc3b4 | 2012-06-10 18:05:36 -0400 | [diff] [blame] | 6404 | umode_t mode, bool excl) |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 6405 | { |
| 6406 | struct btrfs_trans_handle *trans; |
| 6407 | struct btrfs_root *root = BTRFS_I(dir)->root; |
Chris Mason | 1832a6d | 2007-12-21 16:27:21 -0500 | [diff] [blame] | 6408 | struct inode *inode = NULL; |
Filipe Brandenburger | 43baa57 | 2012-11-30 03:40:09 +0000 | [diff] [blame] | 6409 | int drop_inode_on_err = 0; |
Yan, Zheng | a22285a | 2010-05-16 10:48:46 -0400 | [diff] [blame] | 6410 | int err; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 6411 | u64 objectid; |
Chris Mason | 00e4e6b | 2008-08-05 11:18:09 -0400 | [diff] [blame] | 6412 | u64 index = 0; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 6413 | |
Josef Bacik | 9ed74f2 | 2009-09-11 16:12:44 -0400 | [diff] [blame] | 6414 | /* |
| 6415 | * 2 for inode item and ref |
| 6416 | * 2 for dir items |
| 6417 | * 1 for xattr if selinux is on |
| 6418 | */ |
Yan, Zheng | a22285a | 2010-05-16 10:48:46 -0400 | [diff] [blame] | 6419 | trans = btrfs_start_transaction(root, 5); |
| 6420 | if (IS_ERR(trans)) |
| 6421 | return PTR_ERR(trans); |
Josef Bacik | 9ed74f2 | 2009-09-11 16:12:44 -0400 | [diff] [blame] | 6422 | |
Li Zefan | 581bb05 | 2011-04-20 10:06:11 +0800 | [diff] [blame] | 6423 | err = btrfs_find_free_ino(root, &objectid); |
| 6424 | if (err) |
| 6425 | goto out_unlock; |
| 6426 | |
Josef Bacik | aec7477 | 2008-07-24 12:12:38 -0400 | [diff] [blame] | 6427 | inode = btrfs_new_inode(trans, root, dir, dentry->d_name.name, |
Li Zefan | 33345d01 | 2011-04-20 10:31:50 +0800 | [diff] [blame] | 6428 | dentry->d_name.len, btrfs_ino(dir), objectid, |
Josef Bacik | d82a6f1d | 2011-05-11 15:26:06 -0400 | [diff] [blame] | 6429 | mode, &index); |
Tsutomu Itoh | 7cf96da | 2011-04-25 19:43:53 -0400 | [diff] [blame] | 6430 | if (IS_ERR(inode)) { |
| 6431 | err = PTR_ERR(inode); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 6432 | goto out_unlock; |
Tsutomu Itoh | 7cf96da | 2011-04-25 19:43:53 -0400 | [diff] [blame] | 6433 | } |
Filipe Brandenburger | 43baa57 | 2012-11-30 03:40:09 +0000 | [diff] [blame] | 6434 | drop_inode_on_err = 1; |
Casey Schaufler | ad19db7 | 2011-12-15 10:09:07 -0500 | [diff] [blame] | 6435 | /* |
| 6436 | * If the active LSM wants to access the inode during |
| 6437 | * d_instantiate it needs these. Smack checks to see |
| 6438 | * if the filesystem supports xattrs by looking at the |
| 6439 | * ops vector. |
| 6440 | */ |
| 6441 | inode->i_fop = &btrfs_file_operations; |
| 6442 | inode->i_op = &btrfs_file_inode_operations; |
Chris Mason | b0d5d10 | 2014-09-08 13:08:51 -0700 | [diff] [blame] | 6443 | inode->i_mapping->a_ops = &btrfs_aops; |
Chris Mason | b0d5d10 | 2014-09-08 13:08:51 -0700 | [diff] [blame] | 6444 | |
| 6445 | err = btrfs_init_inode_security(trans, inode, dir, &dentry->d_name); |
| 6446 | if (err) |
| 6447 | goto out_unlock_inode; |
| 6448 | |
| 6449 | err = btrfs_update_inode(trans, root, inode); |
| 6450 | if (err) |
| 6451 | goto out_unlock_inode; |
Casey Schaufler | ad19db7 | 2011-12-15 10:09:07 -0500 | [diff] [blame] | 6452 | |
Josef Bacik | a1b075d | 2010-11-19 20:36:11 +0000 | [diff] [blame] | 6453 | err = btrfs_add_nondir(trans, dir, dentry, inode, 0, index); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 6454 | if (err) |
Chris Mason | b0d5d10 | 2014-09-08 13:08:51 -0700 | [diff] [blame] | 6455 | goto out_unlock_inode; |
Filipe Brandenburger | 43baa57 | 2012-11-30 03:40:09 +0000 | [diff] [blame] | 6456 | |
Filipe Brandenburger | 43baa57 | 2012-11-30 03:40:09 +0000 | [diff] [blame] | 6457 | BTRFS_I(inode)->io_tree.ops = &btrfs_extent_io_ops; |
Chris Mason | b0d5d10 | 2014-09-08 13:08:51 -0700 | [diff] [blame] | 6458 | unlock_new_inode(inode); |
Filipe Brandenburger | 43baa57 | 2012-11-30 03:40:09 +0000 | [diff] [blame] | 6459 | d_instantiate(dentry, inode); |
| 6460 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 6461 | out_unlock: |
Josef Bacik | 7ad85bb | 2012-01-12 19:10:12 -0500 | [diff] [blame] | 6462 | btrfs_end_transaction(trans, root); |
Filipe Brandenburger | 43baa57 | 2012-11-30 03:40:09 +0000 | [diff] [blame] | 6463 | if (err && drop_inode_on_err) { |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 6464 | inode_dec_link_count(inode); |
| 6465 | iput(inode); |
| 6466 | } |
Josef Bacik | c581afc | 2014-02-06 16:06:06 -0500 | [diff] [blame] | 6467 | btrfs_balance_delayed_items(root); |
Liu Bo | b53d3f5 | 2012-11-14 14:34:34 +0000 | [diff] [blame] | 6468 | btrfs_btree_balance_dirty(root); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 6469 | return err; |
Chris Mason | b0d5d10 | 2014-09-08 13:08:51 -0700 | [diff] [blame] | 6470 | |
| 6471 | out_unlock_inode: |
| 6472 | unlock_new_inode(inode); |
| 6473 | goto out_unlock; |
| 6474 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 6475 | } |
| 6476 | |
| 6477 | static int btrfs_link(struct dentry *old_dentry, struct inode *dir, |
| 6478 | struct dentry *dentry) |
| 6479 | { |
| 6480 | struct btrfs_trans_handle *trans; |
| 6481 | struct btrfs_root *root = BTRFS_I(dir)->root; |
David Howells | 2b0143b | 2015-03-17 22:25:59 +0000 | [diff] [blame] | 6482 | struct inode *inode = d_inode(old_dentry); |
Chris Mason | 00e4e6b | 2008-08-05 11:18:09 -0400 | [diff] [blame] | 6483 | u64 index; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 6484 | int err; |
| 6485 | int drop_inode = 0; |
| 6486 | |
TARUISI Hiroaki | 4a8be42 | 2009-11-12 07:14:26 +0000 | [diff] [blame] | 6487 | /* do not allow sys_link's with other subvols of the same device */ |
| 6488 | if (root->objectid != BTRFS_I(inode)->root->objectid) |
Mark Fasheh | 3ab3564 | 2011-03-22 17:20:26 +0000 | [diff] [blame] | 6489 | return -EXDEV; |
TARUISI Hiroaki | 4a8be42 | 2009-11-12 07:14:26 +0000 | [diff] [blame] | 6490 | |
Mark Fasheh | f186373 | 2012-08-08 11:32:27 -0700 | [diff] [blame] | 6491 | if (inode->i_nlink >= BTRFS_LINK_MAX) |
Al Viro | c055e99 | 2011-03-04 17:15:18 +0000 | [diff] [blame] | 6492 | return -EMLINK; |
Josef Bacik | 9ed74f2 | 2009-09-11 16:12:44 -0400 | [diff] [blame] | 6493 | |
Chris Mason | 3de4586 | 2008-11-17 21:02:50 -0500 | [diff] [blame] | 6494 | err = btrfs_set_inode_index(dir, &index); |
Josef Bacik | aec7477 | 2008-07-24 12:12:38 -0400 | [diff] [blame] | 6495 | if (err) |
| 6496 | goto fail; |
| 6497 | |
Yan, Zheng | a22285a | 2010-05-16 10:48:46 -0400 | [diff] [blame] | 6498 | /* |
Miao Xie | 7e6b646 | 2011-02-18 09:21:17 +0000 | [diff] [blame] | 6499 | * 2 items for inode and inode ref |
Yan, Zheng | a22285a | 2010-05-16 10:48:46 -0400 | [diff] [blame] | 6500 | * 2 items for dir items |
Miao Xie | 7e6b646 | 2011-02-18 09:21:17 +0000 | [diff] [blame] | 6501 | * 1 item for parent inode |
Yan, Zheng | a22285a | 2010-05-16 10:48:46 -0400 | [diff] [blame] | 6502 | */ |
Miao Xie | 7e6b646 | 2011-02-18 09:21:17 +0000 | [diff] [blame] | 6503 | trans = btrfs_start_transaction(root, 5); |
Yan, Zheng | a22285a | 2010-05-16 10:48:46 -0400 | [diff] [blame] | 6504 | if (IS_ERR(trans)) { |
| 6505 | err = PTR_ERR(trans); |
| 6506 | goto fail; |
| 6507 | } |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 6508 | |
Miao Xie | 67de117 | 2013-12-26 13:07:06 +0800 | [diff] [blame] | 6509 | /* There are several dir indexes for this inode, clear the cache. */ |
| 6510 | BTRFS_I(inode)->dir_index = 0ULL; |
Zach Brown | 8b558c5 | 2013-10-16 12:10:34 -0700 | [diff] [blame] | 6511 | inc_nlink(inode); |
Josef Bacik | 0c4d2d9 | 2012-04-05 15:03:02 -0400 | [diff] [blame] | 6512 | inode_inc_iversion(inode); |
Miao Xie | 3153495 | 2011-04-13 13:19:21 +0800 | [diff] [blame] | 6513 | inode->i_ctime = CURRENT_TIME; |
Al Viro | 7de9c6ee | 2010-10-23 11:11:40 -0400 | [diff] [blame] | 6514 | ihold(inode); |
Josef Bacik | e997615 | 2012-10-11 15:53:56 -0400 | [diff] [blame] | 6515 | set_bit(BTRFS_INODE_COPY_EVERYTHING, &BTRFS_I(inode)->runtime_flags); |
Josef Bacik | aec7477 | 2008-07-24 12:12:38 -0400 | [diff] [blame] | 6516 | |
Josef Bacik | a1b075d | 2010-11-19 20:36:11 +0000 | [diff] [blame] | 6517 | err = btrfs_add_nondir(trans, dir, dentry, inode, 1, index); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 6518 | |
Yan, Zheng | a571952 | 2009-09-24 09:17:31 -0400 | [diff] [blame] | 6519 | if (err) { |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 6520 | drop_inode = 1; |
Yan, Zheng | a571952 | 2009-09-24 09:17:31 -0400 | [diff] [blame] | 6521 | } else { |
Al Viro | 10d9f30 | 2011-07-16 23:09:10 -0400 | [diff] [blame] | 6522 | struct dentry *parent = dentry->d_parent; |
Yan, Zheng | a571952 | 2009-09-24 09:17:31 -0400 | [diff] [blame] | 6523 | err = btrfs_update_inode(trans, root, inode); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 6524 | if (err) |
| 6525 | goto fail; |
Filipe Manana | ef3b9af | 2014-04-27 20:40:45 +0100 | [diff] [blame] | 6526 | if (inode->i_nlink == 1) { |
| 6527 | /* |
| 6528 | * If new hard link count is 1, it's a file created |
| 6529 | * with open(2) O_TMPFILE flag. |
| 6530 | */ |
| 6531 | err = btrfs_orphan_del(trans, inode); |
| 6532 | if (err) |
| 6533 | goto fail; |
| 6534 | } |
Al Viro | 08c422c | 2011-12-23 07:58:13 -0500 | [diff] [blame] | 6535 | d_instantiate(dentry, inode); |
Josef Bacik | 6a91221 | 2010-11-20 09:48:00 +0000 | [diff] [blame] | 6536 | btrfs_log_new_name(trans, inode, NULL, parent); |
Yan, Zheng | a571952 | 2009-09-24 09:17:31 -0400 | [diff] [blame] | 6537 | } |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 6538 | |
Josef Bacik | 7ad85bb | 2012-01-12 19:10:12 -0500 | [diff] [blame] | 6539 | btrfs_end_transaction(trans, root); |
Josef Bacik | c581afc | 2014-02-06 16:06:06 -0500 | [diff] [blame] | 6540 | btrfs_balance_delayed_items(root); |
Chris Mason | 1832a6d | 2007-12-21 16:27:21 -0500 | [diff] [blame] | 6541 | fail: |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 6542 | if (drop_inode) { |
| 6543 | inode_dec_link_count(inode); |
| 6544 | iput(inode); |
| 6545 | } |
Liu Bo | b53d3f5 | 2012-11-14 14:34:34 +0000 | [diff] [blame] | 6546 | btrfs_btree_balance_dirty(root); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 6547 | return err; |
| 6548 | } |
| 6549 | |
Al Viro | 18bb1db | 2011-07-26 01:41:39 -0400 | [diff] [blame] | 6550 | 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] | 6551 | { |
Chris Mason | b9d8666 | 2008-05-02 16:13:49 -0400 | [diff] [blame] | 6552 | struct inode *inode = NULL; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 6553 | struct btrfs_trans_handle *trans; |
| 6554 | struct btrfs_root *root = BTRFS_I(dir)->root; |
| 6555 | int err = 0; |
| 6556 | int drop_on_err = 0; |
Chris Mason | b9d8666 | 2008-05-02 16:13:49 -0400 | [diff] [blame] | 6557 | u64 objectid = 0; |
Chris Mason | 00e4e6b | 2008-08-05 11:18:09 -0400 | [diff] [blame] | 6558 | u64 index = 0; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 6559 | |
Josef Bacik | 9ed74f2 | 2009-09-11 16:12:44 -0400 | [diff] [blame] | 6560 | /* |
| 6561 | * 2 items for inode and ref |
| 6562 | * 2 items for dir items |
| 6563 | * 1 for xattr if selinux is on |
| 6564 | */ |
Yan, Zheng | a22285a | 2010-05-16 10:48:46 -0400 | [diff] [blame] | 6565 | trans = btrfs_start_transaction(root, 5); |
| 6566 | if (IS_ERR(trans)) |
| 6567 | return PTR_ERR(trans); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 6568 | |
Li Zefan | 581bb05 | 2011-04-20 10:06:11 +0800 | [diff] [blame] | 6569 | err = btrfs_find_free_ino(root, &objectid); |
| 6570 | if (err) |
| 6571 | goto out_fail; |
| 6572 | |
Josef Bacik | aec7477 | 2008-07-24 12:12:38 -0400 | [diff] [blame] | 6573 | inode = btrfs_new_inode(trans, root, dir, dentry->d_name.name, |
Li Zefan | 33345d01 | 2011-04-20 10:31:50 +0800 | [diff] [blame] | 6574 | dentry->d_name.len, btrfs_ino(dir), objectid, |
Josef Bacik | d82a6f1d | 2011-05-11 15:26:06 -0400 | [diff] [blame] | 6575 | S_IFDIR | mode, &index); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 6576 | if (IS_ERR(inode)) { |
| 6577 | err = PTR_ERR(inode); |
| 6578 | goto out_fail; |
| 6579 | } |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 6580 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 6581 | drop_on_err = 1; |
Chris Mason | b0d5d10 | 2014-09-08 13:08:51 -0700 | [diff] [blame] | 6582 | /* these must be set before we unlock the inode */ |
| 6583 | inode->i_op = &btrfs_dir_inode_operations; |
| 6584 | inode->i_fop = &btrfs_dir_file_operations; |
Josef Bacik | 33268ea | 2008-07-24 12:16:36 -0400 | [diff] [blame] | 6585 | |
Eric Paris | 2a7dba3 | 2011-02-01 11:05:39 -0500 | [diff] [blame] | 6586 | err = btrfs_init_inode_security(trans, inode, dir, &dentry->d_name); |
Josef Bacik | 33268ea | 2008-07-24 12:16:36 -0400 | [diff] [blame] | 6587 | if (err) |
Chris Mason | b0d5d10 | 2014-09-08 13:08:51 -0700 | [diff] [blame] | 6588 | goto out_fail_inode; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 6589 | |
Chris Mason | dbe674a | 2008-07-17 12:54:05 -0400 | [diff] [blame] | 6590 | btrfs_i_size_write(inode, 0); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 6591 | err = btrfs_update_inode(trans, root, inode); |
| 6592 | if (err) |
Chris Mason | b0d5d10 | 2014-09-08 13:08:51 -0700 | [diff] [blame] | 6593 | goto out_fail_inode; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 6594 | |
Josef Bacik | a1b075d | 2010-11-19 20:36:11 +0000 | [diff] [blame] | 6595 | err = btrfs_add_link(trans, dir, inode, dentry->d_name.name, |
| 6596 | dentry->d_name.len, 0, index); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 6597 | if (err) |
Chris Mason | b0d5d10 | 2014-09-08 13:08:51 -0700 | [diff] [blame] | 6598 | goto out_fail_inode; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 6599 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 6600 | d_instantiate(dentry, inode); |
Chris Mason | b0d5d10 | 2014-09-08 13:08:51 -0700 | [diff] [blame] | 6601 | /* |
| 6602 | * mkdir is special. We're unlocking after we call d_instantiate |
| 6603 | * to avoid a race with nfsd calling d_instantiate. |
| 6604 | */ |
| 6605 | unlock_new_inode(inode); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 6606 | drop_on_err = 0; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 6607 | |
| 6608 | out_fail: |
Josef Bacik | 7ad85bb | 2012-01-12 19:10:12 -0500 | [diff] [blame] | 6609 | btrfs_end_transaction(trans, root); |
Wang Shilong | c7cfb8a | 2014-12-24 14:45:30 +0800 | [diff] [blame] | 6610 | if (drop_on_err) { |
| 6611 | inode_dec_link_count(inode); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 6612 | iput(inode); |
Wang Shilong | c7cfb8a | 2014-12-24 14:45:30 +0800 | [diff] [blame] | 6613 | } |
Josef Bacik | c581afc | 2014-02-06 16:06:06 -0500 | [diff] [blame] | 6614 | btrfs_balance_delayed_items(root); |
Liu Bo | b53d3f5 | 2012-11-14 14:34:34 +0000 | [diff] [blame] | 6615 | btrfs_btree_balance_dirty(root); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 6616 | return err; |
Chris Mason | b0d5d10 | 2014-09-08 13:08:51 -0700 | [diff] [blame] | 6617 | |
| 6618 | out_fail_inode: |
| 6619 | unlock_new_inode(inode); |
| 6620 | goto out_fail; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 6621 | } |
| 6622 | |
Qu Wenruo | e6c4efd | 2014-09-17 11:53:35 +0800 | [diff] [blame] | 6623 | /* Find next extent map of a given extent map, caller needs to ensure locks */ |
| 6624 | static struct extent_map *next_extent_map(struct extent_map *em) |
| 6625 | { |
| 6626 | struct rb_node *next; |
| 6627 | |
| 6628 | next = rb_next(&em->rb_node); |
| 6629 | if (!next) |
| 6630 | return NULL; |
| 6631 | return container_of(next, struct extent_map, rb_node); |
| 6632 | } |
| 6633 | |
| 6634 | static struct extent_map *prev_extent_map(struct extent_map *em) |
| 6635 | { |
| 6636 | struct rb_node *prev; |
| 6637 | |
| 6638 | prev = rb_prev(&em->rb_node); |
| 6639 | if (!prev) |
| 6640 | return NULL; |
| 6641 | return container_of(prev, struct extent_map, rb_node); |
| 6642 | } |
| 6643 | |
Chris Mason | d352ac6 | 2008-09-29 15:18:18 -0400 | [diff] [blame] | 6644 | /* helper for btfs_get_extent. Given an existing extent in the tree, |
Qu Wenruo | e6c4efd | 2014-09-17 11:53:35 +0800 | [diff] [blame] | 6645 | * the existing extent is the nearest extent to map_start, |
Chris Mason | d352ac6 | 2008-09-29 15:18:18 -0400 | [diff] [blame] | 6646 | * 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] | 6647 | * the best fitted new extent into the tree. |
Chris Mason | d352ac6 | 2008-09-29 15:18:18 -0400 | [diff] [blame] | 6648 | */ |
Chris Mason | 3b95151 | 2008-04-17 11:29:12 -0400 | [diff] [blame] | 6649 | static int merge_extent_mapping(struct extent_map_tree *em_tree, |
| 6650 | struct extent_map *existing, |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 6651 | struct extent_map *em, |
Qu Wenruo | 51f395a | 2014-08-08 13:06:20 +0800 | [diff] [blame] | 6652 | u64 map_start) |
Chris Mason | 3b95151 | 2008-04-17 11:29:12 -0400 | [diff] [blame] | 6653 | { |
Qu Wenruo | e6c4efd | 2014-09-17 11:53:35 +0800 | [diff] [blame] | 6654 | struct extent_map *prev; |
| 6655 | struct extent_map *next; |
| 6656 | u64 start; |
| 6657 | u64 end; |
Chris Mason | 3b95151 | 2008-04-17 11:29:12 -0400 | [diff] [blame] | 6658 | u64 start_diff; |
Chris Mason | 3b95151 | 2008-04-17 11:29:12 -0400 | [diff] [blame] | 6659 | |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 6660 | BUG_ON(map_start < em->start || map_start >= extent_map_end(em)); |
Qu Wenruo | e6c4efd | 2014-09-17 11:53:35 +0800 | [diff] [blame] | 6661 | |
| 6662 | if (existing->start > map_start) { |
| 6663 | next = existing; |
| 6664 | prev = prev_extent_map(next); |
| 6665 | } else { |
| 6666 | prev = existing; |
| 6667 | next = next_extent_map(prev); |
| 6668 | } |
| 6669 | |
| 6670 | start = prev ? extent_map_end(prev) : em->start; |
| 6671 | start = max_t(u64, start, em->start); |
| 6672 | end = next ? next->start : extent_map_end(em); |
| 6673 | end = min_t(u64, end, extent_map_end(em)); |
| 6674 | start_diff = start - em->start; |
| 6675 | em->start = start; |
| 6676 | em->len = end - start; |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 6677 | if (em->block_start < EXTENT_MAP_LAST_BYTE && |
| 6678 | !test_bit(EXTENT_FLAG_COMPRESSED, &em->flags)) { |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 6679 | em->block_start += start_diff; |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 6680 | em->block_len -= start_diff; |
| 6681 | } |
Josef Bacik | 09a2a8f9 | 2013-04-05 16:51:15 -0400 | [diff] [blame] | 6682 | return add_extent_mapping(em_tree, em, 0); |
Chris Mason | 3b95151 | 2008-04-17 11:29:12 -0400 | [diff] [blame] | 6683 | } |
| 6684 | |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 6685 | static noinline int uncompress_inline(struct btrfs_path *path, |
| 6686 | struct inode *inode, struct page *page, |
| 6687 | size_t pg_offset, u64 extent_offset, |
| 6688 | struct btrfs_file_extent_item *item) |
| 6689 | { |
| 6690 | int ret; |
| 6691 | struct extent_buffer *leaf = path->nodes[0]; |
| 6692 | char *tmp; |
| 6693 | size_t max_size; |
| 6694 | unsigned long inline_size; |
| 6695 | unsigned long ptr; |
Li Zefan | 261507a0 | 2010-12-17 14:21:50 +0800 | [diff] [blame] | 6696 | int compress_type; |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 6697 | |
| 6698 | WARN_ON(pg_offset != 0); |
Li Zefan | 261507a0 | 2010-12-17 14:21:50 +0800 | [diff] [blame] | 6699 | compress_type = btrfs_file_extent_compression(leaf, item); |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 6700 | max_size = btrfs_file_extent_ram_bytes(leaf, item); |
| 6701 | inline_size = btrfs_file_extent_inline_item_len(leaf, |
Ross Kirk | dd3cc16 | 2013-09-16 15:58:09 +0100 | [diff] [blame] | 6702 | btrfs_item_nr(path->slots[0])); |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 6703 | tmp = kmalloc(inline_size, GFP_NOFS); |
Tsutomu Itoh | 8d41371 | 2011-04-25 19:43:52 -0400 | [diff] [blame] | 6704 | if (!tmp) |
| 6705 | return -ENOMEM; |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 6706 | ptr = btrfs_file_extent_inline_start(item); |
| 6707 | |
| 6708 | read_extent_buffer(leaf, tmp, ptr, inline_size); |
| 6709 | |
Chris Mason | 5b050f0 | 2008-11-11 09:34:41 -0500 | [diff] [blame] | 6710 | max_size = min_t(unsigned long, PAGE_CACHE_SIZE, max_size); |
Li Zefan | 261507a0 | 2010-12-17 14:21:50 +0800 | [diff] [blame] | 6711 | ret = btrfs_decompress(compress_type, tmp, page, |
| 6712 | extent_offset, inline_size, max_size); |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 6713 | kfree(tmp); |
Zach Brown | 166ae5a | 2014-05-09 17:15:10 -0400 | [diff] [blame] | 6714 | return ret; |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 6715 | } |
| 6716 | |
Chris Mason | d352ac6 | 2008-09-29 15:18:18 -0400 | [diff] [blame] | 6717 | /* |
| 6718 | * 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] | 6719 | * the ugly parts come from merging extents from the disk with the in-ram |
| 6720 | * representation. This gets more complex because of the data=ordered code, |
Chris Mason | d352ac6 | 2008-09-29 15:18:18 -0400 | [diff] [blame] | 6721 | * where the in-ram extents might be locked pending data=ordered completion. |
| 6722 | * |
| 6723 | * This also copies inline extents directly into the page. |
| 6724 | */ |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 6725 | |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 6726 | struct extent_map *btrfs_get_extent(struct inode *inode, struct page *page, |
Chris Mason | 70dec80 | 2008-01-29 09:59:12 -0500 | [diff] [blame] | 6727 | size_t pg_offset, u64 start, u64 len, |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 6728 | int create) |
| 6729 | { |
| 6730 | int ret; |
| 6731 | int err = 0; |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 6732 | u64 extent_start = 0; |
| 6733 | u64 extent_end = 0; |
Li Zefan | 33345d01 | 2011-04-20 10:31:50 +0800 | [diff] [blame] | 6734 | u64 objectid = btrfs_ino(inode); |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 6735 | u32 found_type; |
Chris Mason | f421950 | 2008-07-22 11:18:09 -0400 | [diff] [blame] | 6736 | struct btrfs_path *path = NULL; |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 6737 | struct btrfs_root *root = BTRFS_I(inode)->root; |
| 6738 | struct btrfs_file_extent_item *item; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 6739 | struct extent_buffer *leaf; |
| 6740 | struct btrfs_key found_key; |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 6741 | struct extent_map *em = NULL; |
| 6742 | struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree; |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 6743 | struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree; |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 6744 | struct btrfs_trans_handle *trans = NULL; |
Filipe Manana | 7ffbb59 | 2014-06-09 03:48:05 +0100 | [diff] [blame] | 6745 | const bool new_inline = !page || create; |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 6746 | |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 6747 | again: |
Chris Mason | 890871b | 2009-09-02 16:24:52 -0400 | [diff] [blame] | 6748 | read_lock(&em_tree->lock); |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 6749 | em = lookup_extent_mapping(em_tree, start, len); |
Chris Mason | a061fc8 | 2008-05-07 11:43:44 -0400 | [diff] [blame] | 6750 | if (em) |
| 6751 | em->bdev = root->fs_info->fs_devices->latest_bdev; |
Chris Mason | 890871b | 2009-09-02 16:24:52 -0400 | [diff] [blame] | 6752 | read_unlock(&em_tree->lock); |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 6753 | |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 6754 | if (em) { |
Chris Mason | e1c4b74 | 2008-04-22 13:26:46 -0400 | [diff] [blame] | 6755 | if (em->start > start || em->start + em->len <= start) |
| 6756 | free_extent_map(em); |
| 6757 | else if (em->block_start == EXTENT_MAP_INLINE && page) |
Chris Mason | 70dec80 | 2008-01-29 09:59:12 -0500 | [diff] [blame] | 6758 | free_extent_map(em); |
| 6759 | else |
| 6760 | goto out; |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 6761 | } |
David Sterba | 172ddd6 | 2011-04-21 00:48:27 +0200 | [diff] [blame] | 6762 | em = alloc_extent_map(); |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 6763 | if (!em) { |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 6764 | err = -ENOMEM; |
| 6765 | goto out; |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 6766 | } |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 6767 | em->bdev = root->fs_info->fs_devices->latest_bdev; |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 6768 | em->start = EXTENT_MAP_HOLE; |
Chris Mason | 445a694 | 2008-11-10 11:53:33 -0500 | [diff] [blame] | 6769 | em->orig_start = EXTENT_MAP_HOLE; |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 6770 | em->len = (u64)-1; |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 6771 | em->block_len = (u64)-1; |
Chris Mason | f421950 | 2008-07-22 11:18:09 -0400 | [diff] [blame] | 6772 | |
| 6773 | if (!path) { |
| 6774 | path = btrfs_alloc_path(); |
Josef Bacik | 026fd31 | 2011-05-13 10:32:11 -0400 | [diff] [blame] | 6775 | if (!path) { |
| 6776 | err = -ENOMEM; |
| 6777 | goto out; |
| 6778 | } |
| 6779 | /* |
| 6780 | * Chances are we'll be called again, so go ahead and do |
| 6781 | * readahead |
| 6782 | */ |
| 6783 | path->reada = 1; |
Chris Mason | f421950 | 2008-07-22 11:18:09 -0400 | [diff] [blame] | 6784 | } |
| 6785 | |
Chris Mason | 179e29e | 2007-11-01 11:28:41 -0400 | [diff] [blame] | 6786 | ret = btrfs_lookup_file_extent(trans, root, path, |
| 6787 | objectid, start, trans != NULL); |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 6788 | if (ret < 0) { |
| 6789 | err = ret; |
| 6790 | goto out; |
| 6791 | } |
| 6792 | |
| 6793 | if (ret != 0) { |
| 6794 | if (path->slots[0] == 0) |
| 6795 | goto not_found; |
| 6796 | path->slots[0]--; |
| 6797 | } |
| 6798 | |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 6799 | leaf = path->nodes[0]; |
| 6800 | item = btrfs_item_ptr(leaf, path->slots[0], |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 6801 | struct btrfs_file_extent_item); |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 6802 | /* are we inside the extent that was found? */ |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 6803 | btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]); |
David Sterba | 962a298 | 2014-06-04 18:41:45 +0200 | [diff] [blame] | 6804 | found_type = found_key.type; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 6805 | if (found_key.objectid != objectid || |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 6806 | found_type != BTRFS_EXTENT_DATA_KEY) { |
Josef Bacik | 25a5034 | 2013-10-14 12:08:38 -0400 | [diff] [blame] | 6807 | /* |
| 6808 | * If we backup past the first extent we want to move forward |
| 6809 | * and see if there is an extent in front of us, otherwise we'll |
| 6810 | * say there is a hole for our whole search range which can |
| 6811 | * cause problems. |
| 6812 | */ |
| 6813 | extent_end = start; |
| 6814 | goto next; |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 6815 | } |
| 6816 | |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 6817 | found_type = btrfs_file_extent_type(leaf, item); |
| 6818 | extent_start = found_key.offset; |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 6819 | if (found_type == BTRFS_FILE_EXTENT_REG || |
| 6820 | found_type == BTRFS_FILE_EXTENT_PREALLOC) { |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 6821 | extent_end = extent_start + |
Chris Mason | db94535 | 2007-10-15 16:15:53 -0400 | [diff] [blame] | 6822 | btrfs_file_extent_num_bytes(leaf, item); |
Yan Zheng | 9036c10 | 2008-10-30 14:19:41 -0400 | [diff] [blame] | 6823 | } else if (found_type == BTRFS_FILE_EXTENT_INLINE) { |
| 6824 | size_t size; |
Chris Mason | 514ac8a | 2014-01-03 21:07:00 -0800 | [diff] [blame] | 6825 | size = btrfs_file_extent_inline_len(leaf, path->slots[0], item); |
Qu Wenruo | fda2832 | 2013-02-26 08:10:22 +0000 | [diff] [blame] | 6826 | extent_end = ALIGN(extent_start + size, root->sectorsize); |
Yan Zheng | 9036c10 | 2008-10-30 14:19:41 -0400 | [diff] [blame] | 6827 | } |
Josef Bacik | 25a5034 | 2013-10-14 12:08:38 -0400 | [diff] [blame] | 6828 | next: |
Yan Zheng | 9036c10 | 2008-10-30 14:19:41 -0400 | [diff] [blame] | 6829 | if (start >= extent_end) { |
| 6830 | path->slots[0]++; |
| 6831 | if (path->slots[0] >= btrfs_header_nritems(leaf)) { |
| 6832 | ret = btrfs_next_leaf(root, path); |
| 6833 | if (ret < 0) { |
| 6834 | err = ret; |
| 6835 | goto out; |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 6836 | } |
Yan Zheng | 9036c10 | 2008-10-30 14:19:41 -0400 | [diff] [blame] | 6837 | if (ret > 0) |
| 6838 | goto not_found; |
| 6839 | leaf = path->nodes[0]; |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 6840 | } |
Yan Zheng | 9036c10 | 2008-10-30 14:19:41 -0400 | [diff] [blame] | 6841 | btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]); |
| 6842 | if (found_key.objectid != objectid || |
| 6843 | found_key.type != BTRFS_EXTENT_DATA_KEY) |
| 6844 | goto not_found; |
| 6845 | if (start + len <= found_key.offset) |
| 6846 | goto not_found; |
Wang Shilong | e2eca69 | 2014-07-17 11:44:14 +0800 | [diff] [blame] | 6847 | if (start > found_key.offset) |
| 6848 | goto next; |
Yan Zheng | 9036c10 | 2008-10-30 14:19:41 -0400 | [diff] [blame] | 6849 | em->start = start; |
Josef Bacik | 70c8a91 | 2012-10-11 16:54:30 -0400 | [diff] [blame] | 6850 | em->orig_start = start; |
Yan Zheng | 9036c10 | 2008-10-30 14:19:41 -0400 | [diff] [blame] | 6851 | em->len = found_key.offset - start; |
| 6852 | goto not_found_em; |
| 6853 | } |
| 6854 | |
Filipe Manana | 7ffbb59 | 2014-06-09 03:48:05 +0100 | [diff] [blame] | 6855 | btrfs_extent_item_to_extent_map(inode, path, item, new_inline, em); |
| 6856 | |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 6857 | if (found_type == BTRFS_FILE_EXTENT_REG || |
| 6858 | found_type == BTRFS_FILE_EXTENT_PREALLOC) { |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 6859 | goto insert; |
| 6860 | } else if (found_type == BTRFS_FILE_EXTENT_INLINE) { |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 6861 | unsigned long ptr; |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 6862 | char *map; |
Chris Mason | 3326d1b | 2007-10-15 16:18:25 -0400 | [diff] [blame] | 6863 | size_t size; |
| 6864 | size_t extent_offset; |
| 6865 | size_t copy_size; |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 6866 | |
Filipe Manana | 7ffbb59 | 2014-06-09 03:48:05 +0100 | [diff] [blame] | 6867 | if (new_inline) |
Yan | 689f934 | 2007-10-29 11:41:07 -0400 | [diff] [blame] | 6868 | goto out; |
Yan | 689f934 | 2007-10-29 11:41:07 -0400 | [diff] [blame] | 6869 | |
Chris Mason | 514ac8a | 2014-01-03 21:07:00 -0800 | [diff] [blame] | 6870 | size = btrfs_file_extent_inline_len(leaf, path->slots[0], item); |
Yan Zheng | 9036c10 | 2008-10-30 14:19:41 -0400 | [diff] [blame] | 6871 | extent_offset = page_offset(page) + pg_offset - extent_start; |
Chris Mason | 70dec80 | 2008-01-29 09:59:12 -0500 | [diff] [blame] | 6872 | copy_size = min_t(u64, PAGE_CACHE_SIZE - pg_offset, |
Yan | 689f934 | 2007-10-29 11:41:07 -0400 | [diff] [blame] | 6873 | size - extent_offset); |
Chris Mason | 3326d1b | 2007-10-15 16:18:25 -0400 | [diff] [blame] | 6874 | em->start = extent_start + extent_offset; |
Qu Wenruo | fda2832 | 2013-02-26 08:10:22 +0000 | [diff] [blame] | 6875 | em->len = ALIGN(copy_size, root->sectorsize); |
Josef Bacik | b493968 | 2012-12-03 10:31:19 -0500 | [diff] [blame] | 6876 | em->orig_block_len = em->len; |
Josef Bacik | 70c8a91 | 2012-10-11 16:54:30 -0400 | [diff] [blame] | 6877 | em->orig_start = em->start; |
Yan | 689f934 | 2007-10-29 11:41:07 -0400 | [diff] [blame] | 6878 | ptr = btrfs_file_extent_inline_start(item) + extent_offset; |
Chris Mason | 179e29e | 2007-11-01 11:28:41 -0400 | [diff] [blame] | 6879 | if (create == 0 && !PageUptodate(page)) { |
Li Zefan | 261507a0 | 2010-12-17 14:21:50 +0800 | [diff] [blame] | 6880 | if (btrfs_file_extent_compression(leaf, item) != |
| 6881 | BTRFS_COMPRESS_NONE) { |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 6882 | ret = uncompress_inline(path, inode, page, |
| 6883 | pg_offset, |
| 6884 | extent_offset, item); |
Zach Brown | 166ae5a | 2014-05-09 17:15:10 -0400 | [diff] [blame] | 6885 | if (ret) { |
| 6886 | err = ret; |
| 6887 | goto out; |
| 6888 | } |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 6889 | } else { |
| 6890 | map = kmap(page); |
| 6891 | read_extent_buffer(leaf, map + pg_offset, ptr, |
| 6892 | copy_size); |
Chris Mason | 93c82d5 | 2009-09-11 12:36:29 -0400 | [diff] [blame] | 6893 | if (pg_offset + copy_size < PAGE_CACHE_SIZE) { |
| 6894 | memset(map + pg_offset + copy_size, 0, |
| 6895 | PAGE_CACHE_SIZE - pg_offset - |
| 6896 | copy_size); |
| 6897 | } |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 6898 | kunmap(page); |
| 6899 | } |
Chris Mason | 179e29e | 2007-11-01 11:28:41 -0400 | [diff] [blame] | 6900 | flush_dcache_page(page); |
| 6901 | } else if (create && PageUptodate(page)) { |
Jan Schmidt | 6bf7e08 | 2011-12-01 14:35:19 +0100 | [diff] [blame] | 6902 | BUG(); |
Chris Mason | 179e29e | 2007-11-01 11:28:41 -0400 | [diff] [blame] | 6903 | if (!trans) { |
| 6904 | kunmap(page); |
| 6905 | free_extent_map(em); |
| 6906 | em = NULL; |
Chris Mason | ff5714c | 2011-05-28 07:00:39 -0400 | [diff] [blame] | 6907 | |
David Sterba | b3b4aa7 | 2011-04-21 01:20:15 +0200 | [diff] [blame] | 6908 | btrfs_release_path(path); |
Josef Bacik | 7a7eaa4 | 2011-04-13 12:54:33 -0400 | [diff] [blame] | 6909 | trans = btrfs_join_transaction(root); |
Chris Mason | ff5714c | 2011-05-28 07:00:39 -0400 | [diff] [blame] | 6910 | |
Tsutomu Itoh | 3612b49 | 2011-01-25 02:51:38 +0000 | [diff] [blame] | 6911 | if (IS_ERR(trans)) |
| 6912 | return ERR_CAST(trans); |
Chris Mason | 179e29e | 2007-11-01 11:28:41 -0400 | [diff] [blame] | 6913 | goto again; |
| 6914 | } |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 6915 | map = kmap(page); |
Chris Mason | 70dec80 | 2008-01-29 09:59:12 -0500 | [diff] [blame] | 6916 | write_extent_buffer(leaf, map + pg_offset, ptr, |
Chris Mason | 179e29e | 2007-11-01 11:28:41 -0400 | [diff] [blame] | 6917 | copy_size); |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 6918 | kunmap(page); |
Chris Mason | 179e29e | 2007-11-01 11:28:41 -0400 | [diff] [blame] | 6919 | btrfs_mark_buffer_dirty(leaf); |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 6920 | } |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 6921 | set_extent_uptodate(io_tree, em->start, |
Arne Jansen | 507903b | 2011-04-06 10:02:20 +0000 | [diff] [blame] | 6922 | extent_map_end(em) - 1, NULL, GFP_NOFS); |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 6923 | goto insert; |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 6924 | } |
| 6925 | not_found: |
| 6926 | em->start = start; |
Josef Bacik | 70c8a91 | 2012-10-11 16:54:30 -0400 | [diff] [blame] | 6927 | em->orig_start = start; |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 6928 | em->len = len; |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 6929 | not_found_em: |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 6930 | em->block_start = EXTENT_MAP_HOLE; |
Yan Zheng | 9036c10 | 2008-10-30 14:19:41 -0400 | [diff] [blame] | 6931 | set_bit(EXTENT_FLAG_VACANCY, &em->flags); |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 6932 | insert: |
David Sterba | b3b4aa7 | 2011-04-21 01:20:15 +0200 | [diff] [blame] | 6933 | btrfs_release_path(path); |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 6934 | if (em->start > start || extent_map_end(em) <= start) { |
Simon Kirby | c2cf52e | 2013-03-19 22:41:23 +0000 | [diff] [blame] | 6935 | btrfs_err(root->fs_info, "bad extent! em: [%llu %llu] passed [%llu %llu]", |
Geert Uytterhoeven | c1c9ff7 | 2013-08-20 13:20:07 +0200 | [diff] [blame] | 6936 | em->start, em->len, start, len); |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 6937 | err = -EIO; |
| 6938 | goto out; |
| 6939 | } |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 6940 | |
| 6941 | err = 0; |
Chris Mason | 890871b | 2009-09-02 16:24:52 -0400 | [diff] [blame] | 6942 | write_lock(&em_tree->lock); |
Josef Bacik | 09a2a8f9 | 2013-04-05 16:51:15 -0400 | [diff] [blame] | 6943 | ret = add_extent_mapping(em_tree, em, 0); |
Chris Mason | 3b95151 | 2008-04-17 11:29:12 -0400 | [diff] [blame] | 6944 | /* it is possible that someone inserted the extent into the tree |
| 6945 | * while we had the lock dropped. It is also possible that |
| 6946 | * an overlapping map exists in the tree |
| 6947 | */ |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 6948 | if (ret == -EEXIST) { |
Chris Mason | 3b95151 | 2008-04-17 11:29:12 -0400 | [diff] [blame] | 6949 | struct extent_map *existing; |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 6950 | |
| 6951 | ret = 0; |
| 6952 | |
Qu Wenruo | e6c4efd | 2014-09-17 11:53:35 +0800 | [diff] [blame] | 6953 | existing = search_extent_mapping(em_tree, start, len); |
| 6954 | /* |
| 6955 | * existing will always be non-NULL, since there must be |
| 6956 | * extent causing the -EEXIST. |
| 6957 | */ |
| 6958 | if (start >= extent_map_end(existing) || |
Qu Wenruo | 32be3a1 | 2014-09-22 09:13:03 +0800 | [diff] [blame] | 6959 | start <= existing->start) { |
Qu Wenruo | e6c4efd | 2014-09-17 11:53:35 +0800 | [diff] [blame] | 6960 | /* |
| 6961 | * The existing extent map is the one nearest to |
| 6962 | * the [start, start + len) range which overlaps |
| 6963 | */ |
| 6964 | err = merge_extent_mapping(em_tree, existing, |
| 6965 | em, start); |
Chris Mason | e1c4b74 | 2008-04-22 13:26:46 -0400 | [diff] [blame] | 6966 | free_extent_map(existing); |
Qu Wenruo | e6c4efd | 2014-09-17 11:53:35 +0800 | [diff] [blame] | 6967 | if (err) { |
Chris Mason | 3b95151 | 2008-04-17 11:29:12 -0400 | [diff] [blame] | 6968 | free_extent_map(em); |
| 6969 | em = NULL; |
| 6970 | } |
| 6971 | } else { |
| 6972 | free_extent_map(em); |
| 6973 | em = existing; |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 6974 | err = 0; |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 6975 | } |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 6976 | } |
Chris Mason | 890871b | 2009-09-02 16:24:52 -0400 | [diff] [blame] | 6977 | write_unlock(&em_tree->lock); |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 6978 | out: |
liubo | 1abe9b8 | 2011-03-24 11:18:59 +0000 | [diff] [blame] | 6979 | |
Steven Rostedt | 4cd8587 | 2013-11-14 22:57:29 -0500 | [diff] [blame] | 6980 | trace_btrfs_get_extent(root, em); |
liubo | 1abe9b8 | 2011-03-24 11:18:59 +0000 | [diff] [blame] | 6981 | |
Tsutomu Itoh | 527afb4 | 2015-08-19 14:55:00 +0900 | [diff] [blame] | 6982 | btrfs_free_path(path); |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 6983 | if (trans) { |
| 6984 | ret = btrfs_end_transaction(trans, root); |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 6985 | if (!err) |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 6986 | err = ret; |
| 6987 | } |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 6988 | if (err) { |
| 6989 | free_extent_map(em); |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 6990 | return ERR_PTR(err); |
| 6991 | } |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 6992 | BUG_ON(!em); /* Error is always set */ |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 6993 | return em; |
| 6994 | } |
| 6995 | |
Chris Mason | ec29ed5 | 2011-02-23 16:23:20 -0500 | [diff] [blame] | 6996 | struct extent_map *btrfs_get_extent_fiemap(struct inode *inode, struct page *page, |
| 6997 | size_t pg_offset, u64 start, u64 len, |
| 6998 | int create) |
| 6999 | { |
| 7000 | struct extent_map *em; |
| 7001 | struct extent_map *hole_em = NULL; |
| 7002 | u64 range_start = start; |
| 7003 | u64 end; |
| 7004 | u64 found; |
| 7005 | u64 found_end; |
| 7006 | int err = 0; |
| 7007 | |
| 7008 | em = btrfs_get_extent(inode, page, pg_offset, start, len, create); |
| 7009 | if (IS_ERR(em)) |
| 7010 | return em; |
| 7011 | if (em) { |
| 7012 | /* |
Liu Bo | f9e4fb5 | 2013-01-07 10:10:12 +0000 | [diff] [blame] | 7013 | * if our em maps to |
| 7014 | * - a hole or |
| 7015 | * - a pre-alloc extent, |
| 7016 | * there might actually be delalloc bytes behind it. |
Chris Mason | ec29ed5 | 2011-02-23 16:23:20 -0500 | [diff] [blame] | 7017 | */ |
Liu Bo | f9e4fb5 | 2013-01-07 10:10:12 +0000 | [diff] [blame] | 7018 | if (em->block_start != EXTENT_MAP_HOLE && |
| 7019 | !test_bit(EXTENT_FLAG_PREALLOC, &em->flags)) |
Chris Mason | ec29ed5 | 2011-02-23 16:23:20 -0500 | [diff] [blame] | 7020 | return em; |
| 7021 | else |
| 7022 | hole_em = em; |
| 7023 | } |
| 7024 | |
| 7025 | /* check to see if we've wrapped (len == -1 or similar) */ |
| 7026 | end = start + len; |
| 7027 | if (end < start) |
| 7028 | end = (u64)-1; |
| 7029 | else |
| 7030 | end -= 1; |
| 7031 | |
| 7032 | em = NULL; |
| 7033 | |
| 7034 | /* ok, we didn't find anything, lets look for delalloc */ |
| 7035 | found = count_range_bits(&BTRFS_I(inode)->io_tree, &range_start, |
| 7036 | end, len, EXTENT_DELALLOC, 1); |
| 7037 | found_end = range_start + found; |
| 7038 | if (found_end < range_start) |
| 7039 | found_end = (u64)-1; |
| 7040 | |
| 7041 | /* |
| 7042 | * we didn't find anything useful, return |
| 7043 | * the original results from get_extent() |
| 7044 | */ |
| 7045 | if (range_start > end || found_end <= start) { |
| 7046 | em = hole_em; |
| 7047 | hole_em = NULL; |
| 7048 | goto out; |
| 7049 | } |
| 7050 | |
| 7051 | /* adjust the range_start to make sure it doesn't |
| 7052 | * go backwards from the start they passed in |
| 7053 | */ |
Dulshani Gunawardhana | 6787125 | 2013-10-31 10:33:04 +0530 | [diff] [blame] | 7054 | range_start = max(start, range_start); |
Chris Mason | ec29ed5 | 2011-02-23 16:23:20 -0500 | [diff] [blame] | 7055 | found = found_end - range_start; |
| 7056 | |
| 7057 | if (found > 0) { |
| 7058 | u64 hole_start = start; |
| 7059 | u64 hole_len = len; |
| 7060 | |
David Sterba | 172ddd6 | 2011-04-21 00:48:27 +0200 | [diff] [blame] | 7061 | em = alloc_extent_map(); |
Chris Mason | ec29ed5 | 2011-02-23 16:23:20 -0500 | [diff] [blame] | 7062 | if (!em) { |
| 7063 | err = -ENOMEM; |
| 7064 | goto out; |
| 7065 | } |
| 7066 | /* |
| 7067 | * when btrfs_get_extent can't find anything it |
| 7068 | * returns one huge hole |
| 7069 | * |
| 7070 | * make sure what it found really fits our range, and |
| 7071 | * adjust to make sure it is based on the start from |
| 7072 | * the caller |
| 7073 | */ |
| 7074 | if (hole_em) { |
| 7075 | u64 calc_end = extent_map_end(hole_em); |
| 7076 | |
| 7077 | if (calc_end <= start || (hole_em->start > end)) { |
| 7078 | free_extent_map(hole_em); |
| 7079 | hole_em = NULL; |
| 7080 | } else { |
| 7081 | hole_start = max(hole_em->start, start); |
| 7082 | hole_len = calc_end - hole_start; |
| 7083 | } |
| 7084 | } |
| 7085 | em->bdev = NULL; |
| 7086 | if (hole_em && range_start > hole_start) { |
| 7087 | /* our hole starts before our delalloc, so we |
| 7088 | * have to return just the parts of the hole |
| 7089 | * that go until the delalloc starts |
| 7090 | */ |
| 7091 | em->len = min(hole_len, |
| 7092 | range_start - hole_start); |
| 7093 | em->start = hole_start; |
| 7094 | em->orig_start = hole_start; |
| 7095 | /* |
| 7096 | * don't adjust block start at all, |
| 7097 | * it is fixed at EXTENT_MAP_HOLE |
| 7098 | */ |
| 7099 | em->block_start = hole_em->block_start; |
| 7100 | em->block_len = hole_len; |
Liu Bo | f9e4fb5 | 2013-01-07 10:10:12 +0000 | [diff] [blame] | 7101 | if (test_bit(EXTENT_FLAG_PREALLOC, &hole_em->flags)) |
| 7102 | set_bit(EXTENT_FLAG_PREALLOC, &em->flags); |
Chris Mason | ec29ed5 | 2011-02-23 16:23:20 -0500 | [diff] [blame] | 7103 | } else { |
| 7104 | em->start = range_start; |
| 7105 | em->len = found; |
| 7106 | em->orig_start = range_start; |
| 7107 | em->block_start = EXTENT_MAP_DELALLOC; |
| 7108 | em->block_len = found; |
| 7109 | } |
| 7110 | } else if (hole_em) { |
| 7111 | return hole_em; |
| 7112 | } |
| 7113 | out: |
| 7114 | |
| 7115 | free_extent_map(hole_em); |
| 7116 | if (err) { |
| 7117 | free_extent_map(em); |
| 7118 | return ERR_PTR(err); |
| 7119 | } |
| 7120 | return em; |
| 7121 | } |
| 7122 | |
Josef Bacik | 4b46fce | 2010-05-23 11:00:55 -0400 | [diff] [blame] | 7123 | static struct extent_map *btrfs_new_extent_direct(struct inode *inode, |
| 7124 | u64 start, u64 len) |
| 7125 | { |
| 7126 | struct btrfs_root *root = BTRFS_I(inode)->root; |
Josef Bacik | 70c8a91 | 2012-10-11 16:54:30 -0400 | [diff] [blame] | 7127 | struct extent_map *em; |
Josef Bacik | 4b46fce | 2010-05-23 11:00:55 -0400 | [diff] [blame] | 7128 | struct btrfs_key ins; |
| 7129 | u64 alloc_hint; |
| 7130 | int ret; |
Josef Bacik | 4b46fce | 2010-05-23 11:00:55 -0400 | [diff] [blame] | 7131 | |
Josef Bacik | 4b46fce | 2010-05-23 11:00:55 -0400 | [diff] [blame] | 7132 | alloc_hint = get_extent_allocation_hint(inode, start, len); |
Josef Bacik | 0036158 | 2013-08-14 14:02:47 -0400 | [diff] [blame] | 7133 | ret = btrfs_reserve_extent(root, len, root->sectorsize, 0, |
Miao Xie | e570fd2 | 2014-06-19 10:42:50 +0800 | [diff] [blame] | 7134 | alloc_hint, &ins, 1, 1); |
Josef Bacik | 0036158 | 2013-08-14 14:02:47 -0400 | [diff] [blame] | 7135 | if (ret) |
| 7136 | return ERR_PTR(ret); |
Josef Bacik | 4b46fce | 2010-05-23 11:00:55 -0400 | [diff] [blame] | 7137 | |
Josef Bacik | 70c8a91 | 2012-10-11 16:54:30 -0400 | [diff] [blame] | 7138 | em = create_pinned_em(inode, start, ins.offset, start, ins.objectid, |
Josef Bacik | cc95bef | 2013-04-04 14:31:27 -0400 | [diff] [blame] | 7139 | ins.offset, ins.offset, ins.offset, 0); |
Josef Bacik | 0036158 | 2013-08-14 14:02:47 -0400 | [diff] [blame] | 7140 | if (IS_ERR(em)) { |
Miao Xie | e570fd2 | 2014-06-19 10:42:50 +0800 | [diff] [blame] | 7141 | btrfs_free_reserved_extent(root, ins.objectid, ins.offset, 1); |
Josef Bacik | 0036158 | 2013-08-14 14:02:47 -0400 | [diff] [blame] | 7142 | return em; |
| 7143 | } |
Josef Bacik | 4b46fce | 2010-05-23 11:00:55 -0400 | [diff] [blame] | 7144 | |
| 7145 | ret = btrfs_add_ordered_extent_dio(inode, start, ins.objectid, |
| 7146 | ins.offset, ins.offset, 0); |
| 7147 | if (ret) { |
Miao Xie | e570fd2 | 2014-06-19 10:42:50 +0800 | [diff] [blame] | 7148 | btrfs_free_reserved_extent(root, ins.objectid, ins.offset, 1); |
Josef Bacik | 0036158 | 2013-08-14 14:02:47 -0400 | [diff] [blame] | 7149 | free_extent_map(em); |
| 7150 | return ERR_PTR(ret); |
Josef Bacik | 4b46fce | 2010-05-23 11:00:55 -0400 | [diff] [blame] | 7151 | } |
Josef Bacik | 0036158 | 2013-08-14 14:02:47 -0400 | [diff] [blame] | 7152 | |
Josef Bacik | 4b46fce | 2010-05-23 11:00:55 -0400 | [diff] [blame] | 7153 | return em; |
| 7154 | } |
| 7155 | |
Chris Mason | 46bfbb5 | 2010-05-26 11:04:10 -0400 | [diff] [blame] | 7156 | /* |
| 7157 | * returns 1 when the nocow is safe, < 1 on error, 0 if the |
| 7158 | * block must be cow'd |
| 7159 | */ |
Josef Bacik | 0036158 | 2013-08-14 14:02:47 -0400 | [diff] [blame] | 7160 | noinline int can_nocow_extent(struct inode *inode, u64 offset, u64 *len, |
Josef Bacik | 7ee9e44 | 2013-06-21 16:37:03 -0400 | [diff] [blame] | 7161 | u64 *orig_start, u64 *orig_block_len, |
| 7162 | u64 *ram_bytes) |
Chris Mason | 46bfbb5 | 2010-05-26 11:04:10 -0400 | [diff] [blame] | 7163 | { |
Josef Bacik | 0036158 | 2013-08-14 14:02:47 -0400 | [diff] [blame] | 7164 | struct btrfs_trans_handle *trans; |
Chris Mason | 46bfbb5 | 2010-05-26 11:04:10 -0400 | [diff] [blame] | 7165 | struct btrfs_path *path; |
| 7166 | int ret; |
| 7167 | struct extent_buffer *leaf; |
| 7168 | struct btrfs_root *root = BTRFS_I(inode)->root; |
Miao Xie | 7b2b708 | 2014-02-27 13:58:05 +0800 | [diff] [blame] | 7169 | struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree; |
Chris Mason | 46bfbb5 | 2010-05-26 11:04:10 -0400 | [diff] [blame] | 7170 | struct btrfs_file_extent_item *fi; |
| 7171 | struct btrfs_key key; |
| 7172 | u64 disk_bytenr; |
| 7173 | u64 backref_offset; |
| 7174 | u64 extent_end; |
| 7175 | u64 num_bytes; |
| 7176 | int slot; |
| 7177 | int found_type; |
Josef Bacik | 7ee9e44 | 2013-06-21 16:37:03 -0400 | [diff] [blame] | 7178 | bool nocow = (BTRFS_I(inode)->flags & BTRFS_INODE_NODATACOW); |
Miao Xie | e77751a | 2013-12-27 21:11:50 +0800 | [diff] [blame] | 7179 | |
Chris Mason | 46bfbb5 | 2010-05-26 11:04:10 -0400 | [diff] [blame] | 7180 | path = btrfs_alloc_path(); |
| 7181 | if (!path) |
| 7182 | return -ENOMEM; |
| 7183 | |
Josef Bacik | 0036158 | 2013-08-14 14:02:47 -0400 | [diff] [blame] | 7184 | ret = btrfs_lookup_file_extent(NULL, root, path, btrfs_ino(inode), |
Chris Mason | 46bfbb5 | 2010-05-26 11:04:10 -0400 | [diff] [blame] | 7185 | offset, 0); |
| 7186 | if (ret < 0) |
| 7187 | goto out; |
| 7188 | |
| 7189 | slot = path->slots[0]; |
| 7190 | if (ret == 1) { |
| 7191 | if (slot == 0) { |
| 7192 | /* can't find the item, must cow */ |
| 7193 | ret = 0; |
| 7194 | goto out; |
| 7195 | } |
| 7196 | slot--; |
| 7197 | } |
| 7198 | ret = 0; |
| 7199 | leaf = path->nodes[0]; |
| 7200 | btrfs_item_key_to_cpu(leaf, &key, slot); |
Li Zefan | 33345d01 | 2011-04-20 10:31:50 +0800 | [diff] [blame] | 7201 | if (key.objectid != btrfs_ino(inode) || |
Chris Mason | 46bfbb5 | 2010-05-26 11:04:10 -0400 | [diff] [blame] | 7202 | key.type != BTRFS_EXTENT_DATA_KEY) { |
| 7203 | /* not our file or wrong item type, must cow */ |
| 7204 | goto out; |
| 7205 | } |
| 7206 | |
| 7207 | if (key.offset > offset) { |
| 7208 | /* Wrong offset, must cow */ |
| 7209 | goto out; |
| 7210 | } |
| 7211 | |
| 7212 | fi = btrfs_item_ptr(leaf, slot, struct btrfs_file_extent_item); |
| 7213 | found_type = btrfs_file_extent_type(leaf, fi); |
| 7214 | if (found_type != BTRFS_FILE_EXTENT_REG && |
| 7215 | found_type != BTRFS_FILE_EXTENT_PREALLOC) { |
| 7216 | /* not a regular extent, must cow */ |
| 7217 | goto out; |
| 7218 | } |
Josef Bacik | 7ee9e44 | 2013-06-21 16:37:03 -0400 | [diff] [blame] | 7219 | |
| 7220 | if (!nocow && found_type == BTRFS_FILE_EXTENT_REG) |
| 7221 | goto out; |
| 7222 | |
Miao Xie | e77751a | 2013-12-27 21:11:50 +0800 | [diff] [blame] | 7223 | extent_end = key.offset + btrfs_file_extent_num_bytes(leaf, fi); |
| 7224 | if (extent_end <= offset) |
| 7225 | goto out; |
| 7226 | |
Chris Mason | 46bfbb5 | 2010-05-26 11:04:10 -0400 | [diff] [blame] | 7227 | disk_bytenr = btrfs_file_extent_disk_bytenr(leaf, fi); |
Josef Bacik | 7ee9e44 | 2013-06-21 16:37:03 -0400 | [diff] [blame] | 7228 | if (disk_bytenr == 0) |
| 7229 | goto out; |
| 7230 | |
| 7231 | if (btrfs_file_extent_compression(leaf, fi) || |
| 7232 | btrfs_file_extent_encryption(leaf, fi) || |
| 7233 | btrfs_file_extent_other_encoding(leaf, fi)) |
| 7234 | goto out; |
| 7235 | |
Chris Mason | 46bfbb5 | 2010-05-26 11:04:10 -0400 | [diff] [blame] | 7236 | backref_offset = btrfs_file_extent_offset(leaf, fi); |
| 7237 | |
Josef Bacik | 7ee9e44 | 2013-06-21 16:37:03 -0400 | [diff] [blame] | 7238 | if (orig_start) { |
| 7239 | *orig_start = key.offset - backref_offset; |
| 7240 | *orig_block_len = btrfs_file_extent_disk_num_bytes(leaf, fi); |
| 7241 | *ram_bytes = btrfs_file_extent_ram_bytes(leaf, fi); |
| 7242 | } |
Josef Bacik | eb384b5 | 2013-04-24 16:32:55 -0400 | [diff] [blame] | 7243 | |
Chris Mason | 46bfbb5 | 2010-05-26 11:04:10 -0400 | [diff] [blame] | 7244 | if (btrfs_extent_readonly(root, disk_bytenr)) |
| 7245 | goto out; |
Miao Xie | 7b2b708 | 2014-02-27 13:58:05 +0800 | [diff] [blame] | 7246 | |
| 7247 | num_bytes = min(offset + *len, extent_end) - offset; |
| 7248 | if (!nocow && found_type == BTRFS_FILE_EXTENT_PREALLOC) { |
| 7249 | u64 range_end; |
| 7250 | |
| 7251 | range_end = round_up(offset + num_bytes, root->sectorsize) - 1; |
| 7252 | ret = test_range_bit(io_tree, offset, range_end, |
| 7253 | EXTENT_DELALLOC, 0, NULL); |
| 7254 | if (ret) { |
| 7255 | ret = -EAGAIN; |
| 7256 | goto out; |
| 7257 | } |
| 7258 | } |
| 7259 | |
Josef Bacik | 1bda19e | 2013-10-18 12:10:36 -0400 | [diff] [blame] | 7260 | btrfs_release_path(path); |
Chris Mason | 46bfbb5 | 2010-05-26 11:04:10 -0400 | [diff] [blame] | 7261 | |
| 7262 | /* |
| 7263 | * look for other files referencing this extent, if we |
| 7264 | * find any we must cow |
| 7265 | */ |
Josef Bacik | 0036158 | 2013-08-14 14:02:47 -0400 | [diff] [blame] | 7266 | trans = btrfs_join_transaction(root); |
| 7267 | if (IS_ERR(trans)) { |
| 7268 | ret = 0; |
Chris Mason | 46bfbb5 | 2010-05-26 11:04:10 -0400 | [diff] [blame] | 7269 | goto out; |
Josef Bacik | 0036158 | 2013-08-14 14:02:47 -0400 | [diff] [blame] | 7270 | } |
| 7271 | |
| 7272 | ret = btrfs_cross_ref_exist(trans, root, btrfs_ino(inode), |
| 7273 | key.offset - backref_offset, disk_bytenr); |
| 7274 | btrfs_end_transaction(trans, root); |
| 7275 | if (ret) { |
| 7276 | ret = 0; |
| 7277 | goto out; |
| 7278 | } |
Chris Mason | 46bfbb5 | 2010-05-26 11:04:10 -0400 | [diff] [blame] | 7279 | |
| 7280 | /* |
| 7281 | * adjust disk_bytenr and num_bytes to cover just the bytes |
| 7282 | * in this extent we are about to write. If there |
| 7283 | * are any csums in that range we have to cow in order |
| 7284 | * to keep the csums correct |
| 7285 | */ |
| 7286 | disk_bytenr += backref_offset; |
| 7287 | disk_bytenr += offset - key.offset; |
Chris Mason | 46bfbb5 | 2010-05-26 11:04:10 -0400 | [diff] [blame] | 7288 | if (csum_exist_in_range(root, disk_bytenr, num_bytes)) |
| 7289 | goto out; |
| 7290 | /* |
| 7291 | * all of the above have passed, it is safe to overwrite this extent |
| 7292 | * without cow |
| 7293 | */ |
Josef Bacik | eb384b5 | 2013-04-24 16:32:55 -0400 | [diff] [blame] | 7294 | *len = num_bytes; |
Chris Mason | 46bfbb5 | 2010-05-26 11:04:10 -0400 | [diff] [blame] | 7295 | ret = 1; |
| 7296 | out: |
| 7297 | btrfs_free_path(path); |
| 7298 | return ret; |
| 7299 | } |
| 7300 | |
Alex Gartrell | fc4adbff | 2014-05-20 13:07:56 -0700 | [diff] [blame] | 7301 | bool btrfs_page_exists_in_range(struct inode *inode, loff_t start, loff_t end) |
| 7302 | { |
| 7303 | struct radix_tree_root *root = &inode->i_mapping->page_tree; |
| 7304 | int found = false; |
| 7305 | void **pagep = NULL; |
| 7306 | struct page *page = NULL; |
| 7307 | int start_idx; |
| 7308 | int end_idx; |
| 7309 | |
| 7310 | start_idx = start >> PAGE_CACHE_SHIFT; |
| 7311 | |
| 7312 | /* |
| 7313 | * end is the last byte in the last page. end == start is legal |
| 7314 | */ |
| 7315 | end_idx = end >> PAGE_CACHE_SHIFT; |
| 7316 | |
| 7317 | rcu_read_lock(); |
| 7318 | |
| 7319 | /* Most of the code in this while loop is lifted from |
| 7320 | * find_get_page. It's been modified to begin searching from a |
| 7321 | * page and return just the first page found in that range. If the |
| 7322 | * found idx is less than or equal to the end idx then we know that |
| 7323 | * a page exists. If no pages are found or if those pages are |
| 7324 | * outside of the range then we're fine (yay!) */ |
| 7325 | while (page == NULL && |
| 7326 | radix_tree_gang_lookup_slot(root, &pagep, NULL, start_idx, 1)) { |
| 7327 | page = radix_tree_deref_slot(pagep); |
| 7328 | if (unlikely(!page)) |
| 7329 | break; |
| 7330 | |
| 7331 | if (radix_tree_exception(page)) { |
Filipe Manana | 809f901 | 2014-06-05 13:22:25 +0100 | [diff] [blame] | 7332 | if (radix_tree_deref_retry(page)) { |
| 7333 | page = NULL; |
Alex Gartrell | fc4adbff | 2014-05-20 13:07:56 -0700 | [diff] [blame] | 7334 | continue; |
Filipe Manana | 809f901 | 2014-06-05 13:22:25 +0100 | [diff] [blame] | 7335 | } |
Alex Gartrell | fc4adbff | 2014-05-20 13:07:56 -0700 | [diff] [blame] | 7336 | /* |
| 7337 | * Otherwise, shmem/tmpfs must be storing a swap entry |
| 7338 | * here as an exceptional entry: so return it without |
| 7339 | * attempting to raise page count. |
| 7340 | */ |
Filipe Manana | 6fdef6d | 2014-06-05 13:22:26 +0100 | [diff] [blame] | 7341 | page = NULL; |
Alex Gartrell | fc4adbff | 2014-05-20 13:07:56 -0700 | [diff] [blame] | 7342 | break; /* TODO: Is this relevant for this use case? */ |
| 7343 | } |
| 7344 | |
Filipe Manana | 9140515 | 2014-06-05 13:22:24 +0100 | [diff] [blame] | 7345 | if (!page_cache_get_speculative(page)) { |
| 7346 | page = NULL; |
Alex Gartrell | fc4adbff | 2014-05-20 13:07:56 -0700 | [diff] [blame] | 7347 | continue; |
Filipe Manana | 9140515 | 2014-06-05 13:22:24 +0100 | [diff] [blame] | 7348 | } |
Alex Gartrell | fc4adbff | 2014-05-20 13:07:56 -0700 | [diff] [blame] | 7349 | |
| 7350 | /* |
| 7351 | * Has the page moved? |
| 7352 | * This is part of the lockless pagecache protocol. See |
| 7353 | * include/linux/pagemap.h for details. |
| 7354 | */ |
| 7355 | if (unlikely(page != *pagep)) { |
| 7356 | page_cache_release(page); |
| 7357 | page = NULL; |
| 7358 | } |
| 7359 | } |
| 7360 | |
| 7361 | if (page) { |
| 7362 | if (page->index <= end_idx) |
| 7363 | found = true; |
| 7364 | page_cache_release(page); |
| 7365 | } |
| 7366 | |
| 7367 | rcu_read_unlock(); |
| 7368 | return found; |
| 7369 | } |
| 7370 | |
Josef Bacik | eb838e7 | 2012-07-31 16:28:48 -0400 | [diff] [blame] | 7371 | static int lock_extent_direct(struct inode *inode, u64 lockstart, u64 lockend, |
| 7372 | struct extent_state **cached_state, int writing) |
| 7373 | { |
| 7374 | struct btrfs_ordered_extent *ordered; |
| 7375 | int ret = 0; |
| 7376 | |
| 7377 | while (1) { |
| 7378 | lock_extent_bits(&BTRFS_I(inode)->io_tree, lockstart, lockend, |
| 7379 | 0, cached_state); |
| 7380 | /* |
| 7381 | * We're concerned with the entire range that we're going to be |
| 7382 | * doing DIO to, so we need to make sure theres no ordered |
| 7383 | * extents in this range. |
| 7384 | */ |
| 7385 | ordered = btrfs_lookup_ordered_range(inode, lockstart, |
| 7386 | lockend - lockstart + 1); |
| 7387 | |
| 7388 | /* |
| 7389 | * We need to make sure there are no buffered pages in this |
| 7390 | * range either, we could have raced between the invalidate in |
| 7391 | * generic_file_direct_write and locking the extent. The |
| 7392 | * invalidate needs to happen so that reads after a write do not |
| 7393 | * get stale data. |
| 7394 | */ |
Alex Gartrell | fc4adbff | 2014-05-20 13:07:56 -0700 | [diff] [blame] | 7395 | if (!ordered && |
| 7396 | (!writing || |
| 7397 | !btrfs_page_exists_in_range(inode, lockstart, lockend))) |
Josef Bacik | eb838e7 | 2012-07-31 16:28:48 -0400 | [diff] [blame] | 7398 | break; |
| 7399 | |
| 7400 | unlock_extent_cached(&BTRFS_I(inode)->io_tree, lockstart, lockend, |
| 7401 | cached_state, GFP_NOFS); |
| 7402 | |
| 7403 | if (ordered) { |
| 7404 | btrfs_start_ordered_extent(inode, ordered, 1); |
| 7405 | btrfs_put_ordered_extent(ordered); |
| 7406 | } else { |
| 7407 | /* Screw you mmap */ |
Filipe Manana | 728404d | 2014-10-10 09:43:11 +0100 | [diff] [blame] | 7408 | ret = btrfs_fdatawrite_range(inode, lockstart, lockend); |
Filipe Manana | 075bdbd | 2014-10-09 21:18:55 +0100 | [diff] [blame] | 7409 | if (ret) |
| 7410 | break; |
| 7411 | ret = filemap_fdatawait_range(inode->i_mapping, |
| 7412 | lockstart, |
| 7413 | lockend); |
Josef Bacik | eb838e7 | 2012-07-31 16:28:48 -0400 | [diff] [blame] | 7414 | if (ret) |
| 7415 | break; |
| 7416 | |
| 7417 | /* |
| 7418 | * If we found a page that couldn't be invalidated just |
| 7419 | * fall back to buffered. |
| 7420 | */ |
| 7421 | ret = invalidate_inode_pages2_range(inode->i_mapping, |
| 7422 | lockstart >> PAGE_CACHE_SHIFT, |
| 7423 | lockend >> PAGE_CACHE_SHIFT); |
| 7424 | if (ret) |
| 7425 | break; |
| 7426 | } |
| 7427 | |
| 7428 | cond_resched(); |
| 7429 | } |
| 7430 | |
| 7431 | return ret; |
| 7432 | } |
| 7433 | |
Josef Bacik | 69ffb54 | 2012-09-11 15:40:07 -0400 | [diff] [blame] | 7434 | static struct extent_map *create_pinned_em(struct inode *inode, u64 start, |
| 7435 | u64 len, u64 orig_start, |
| 7436 | u64 block_start, u64 block_len, |
Josef Bacik | cc95bef | 2013-04-04 14:31:27 -0400 | [diff] [blame] | 7437 | u64 orig_block_len, u64 ram_bytes, |
| 7438 | int type) |
Josef Bacik | 69ffb54 | 2012-09-11 15:40:07 -0400 | [diff] [blame] | 7439 | { |
| 7440 | struct extent_map_tree *em_tree; |
| 7441 | struct extent_map *em; |
| 7442 | struct btrfs_root *root = BTRFS_I(inode)->root; |
| 7443 | int ret; |
| 7444 | |
| 7445 | em_tree = &BTRFS_I(inode)->extent_tree; |
| 7446 | em = alloc_extent_map(); |
| 7447 | if (!em) |
| 7448 | return ERR_PTR(-ENOMEM); |
| 7449 | |
| 7450 | em->start = start; |
| 7451 | em->orig_start = orig_start; |
Josef Bacik | 2ab28f3 | 2012-10-12 15:27:49 -0400 | [diff] [blame] | 7452 | em->mod_start = start; |
| 7453 | em->mod_len = len; |
Josef Bacik | 69ffb54 | 2012-09-11 15:40:07 -0400 | [diff] [blame] | 7454 | em->len = len; |
| 7455 | em->block_len = block_len; |
| 7456 | em->block_start = block_start; |
| 7457 | em->bdev = root->fs_info->fs_devices->latest_bdev; |
Josef Bacik | b493968 | 2012-12-03 10:31:19 -0500 | [diff] [blame] | 7458 | em->orig_block_len = orig_block_len; |
Josef Bacik | cc95bef | 2013-04-04 14:31:27 -0400 | [diff] [blame] | 7459 | em->ram_bytes = ram_bytes; |
Josef Bacik | 70c8a91 | 2012-10-11 16:54:30 -0400 | [diff] [blame] | 7460 | em->generation = -1; |
Josef Bacik | 69ffb54 | 2012-09-11 15:40:07 -0400 | [diff] [blame] | 7461 | set_bit(EXTENT_FLAG_PINNED, &em->flags); |
| 7462 | if (type == BTRFS_ORDERED_PREALLOC) |
Josef Bacik | b11e234 | 2012-12-03 10:58:15 -0500 | [diff] [blame] | 7463 | set_bit(EXTENT_FLAG_FILLING, &em->flags); |
Josef Bacik | 69ffb54 | 2012-09-11 15:40:07 -0400 | [diff] [blame] | 7464 | |
| 7465 | do { |
| 7466 | btrfs_drop_extent_cache(inode, em->start, |
| 7467 | em->start + em->len - 1, 0); |
| 7468 | write_lock(&em_tree->lock); |
Josef Bacik | 09a2a8f9 | 2013-04-05 16:51:15 -0400 | [diff] [blame] | 7469 | ret = add_extent_mapping(em_tree, em, 1); |
Josef Bacik | 69ffb54 | 2012-09-11 15:40:07 -0400 | [diff] [blame] | 7470 | write_unlock(&em_tree->lock); |
| 7471 | } while (ret == -EEXIST); |
| 7472 | |
| 7473 | if (ret) { |
| 7474 | free_extent_map(em); |
| 7475 | return ERR_PTR(ret); |
| 7476 | } |
| 7477 | |
| 7478 | return em; |
| 7479 | } |
| 7480 | |
chandan | 50745b0 | 2015-08-28 21:10:13 +0530 | [diff] [blame] | 7481 | struct btrfs_dio_data { |
| 7482 | u64 outstanding_extents; |
| 7483 | u64 reserve; |
| 7484 | }; |
Josef Bacik | 69ffb54 | 2012-09-11 15:40:07 -0400 | [diff] [blame] | 7485 | |
Josef Bacik | 4b46fce | 2010-05-23 11:00:55 -0400 | [diff] [blame] | 7486 | static int btrfs_get_blocks_direct(struct inode *inode, sector_t iblock, |
| 7487 | struct buffer_head *bh_result, int create) |
| 7488 | { |
| 7489 | struct extent_map *em; |
| 7490 | struct btrfs_root *root = BTRFS_I(inode)->root; |
Josef Bacik | eb838e7 | 2012-07-31 16:28:48 -0400 | [diff] [blame] | 7491 | struct extent_state *cached_state = NULL; |
chandan | 50745b0 | 2015-08-28 21:10:13 +0530 | [diff] [blame] | 7492 | struct btrfs_dio_data *dio_data = NULL; |
Josef Bacik | 4b46fce | 2010-05-23 11:00:55 -0400 | [diff] [blame] | 7493 | u64 start = iblock << inode->i_blkbits; |
Josef Bacik | eb838e7 | 2012-07-31 16:28:48 -0400 | [diff] [blame] | 7494 | u64 lockstart, lockend; |
Josef Bacik | 4b46fce | 2010-05-23 11:00:55 -0400 | [diff] [blame] | 7495 | u64 len = bh_result->b_size; |
Josef Bacik | eb838e7 | 2012-07-31 16:28:48 -0400 | [diff] [blame] | 7496 | int unlock_bits = EXTENT_LOCKED; |
Miao Xie | 0934856 | 2013-02-07 10:12:07 +0000 | [diff] [blame] | 7497 | int ret = 0; |
Josef Bacik | eb838e7 | 2012-07-31 16:28:48 -0400 | [diff] [blame] | 7498 | |
Miao Xie | 172a504 | 2013-02-21 02:48:22 -0700 | [diff] [blame] | 7499 | if (create) |
Josef Bacik | 3266789 | 2015-02-11 15:08:58 -0500 | [diff] [blame] | 7500 | unlock_bits |= EXTENT_DIRTY; |
Miao Xie | 172a504 | 2013-02-21 02:48:22 -0700 | [diff] [blame] | 7501 | else |
Josef Bacik | c329861 | 2012-08-03 16:49:19 -0400 | [diff] [blame] | 7502 | len = min_t(u64, len, root->sectorsize); |
Josef Bacik | eb838e7 | 2012-07-31 16:28:48 -0400 | [diff] [blame] | 7503 | |
Josef Bacik | c329861 | 2012-08-03 16:49:19 -0400 | [diff] [blame] | 7504 | lockstart = start; |
| 7505 | lockend = start + len - 1; |
| 7506 | |
Josef Bacik | e1cbbfa | 2015-03-17 10:52:28 -0400 | [diff] [blame] | 7507 | if (current->journal_info) { |
| 7508 | /* |
| 7509 | * Need to pull our outstanding extents and set journal_info to NULL so |
| 7510 | * that anything that needs to check if there's a transction doesn't get |
| 7511 | * confused. |
| 7512 | */ |
chandan | 50745b0 | 2015-08-28 21:10:13 +0530 | [diff] [blame] | 7513 | dio_data = current->journal_info; |
Josef Bacik | e1cbbfa | 2015-03-17 10:52:28 -0400 | [diff] [blame] | 7514 | current->journal_info = NULL; |
| 7515 | } |
| 7516 | |
Josef Bacik | eb838e7 | 2012-07-31 16:28:48 -0400 | [diff] [blame] | 7517 | /* |
| 7518 | * If this errors out it's because we couldn't invalidate pagecache for |
| 7519 | * this range and we need to fallback to buffered. |
| 7520 | */ |
| 7521 | if (lock_extent_direct(inode, lockstart, lockend, &cached_state, create)) |
| 7522 | return -ENOTBLK; |
| 7523 | |
Josef Bacik | 4b46fce | 2010-05-23 11:00:55 -0400 | [diff] [blame] | 7524 | em = btrfs_get_extent(inode, NULL, 0, start, len, 0); |
Josef Bacik | eb838e7 | 2012-07-31 16:28:48 -0400 | [diff] [blame] | 7525 | if (IS_ERR(em)) { |
| 7526 | ret = PTR_ERR(em); |
| 7527 | goto unlock_err; |
| 7528 | } |
Josef Bacik | 4b46fce | 2010-05-23 11:00:55 -0400 | [diff] [blame] | 7529 | |
| 7530 | /* |
| 7531 | * Ok for INLINE and COMPRESSED extents we need to fallback on buffered |
| 7532 | * io. INLINE is special, and we could probably kludge it in here, but |
| 7533 | * it's still buffered so for safety lets just fall back to the generic |
| 7534 | * buffered path. |
| 7535 | * |
| 7536 | * For COMPRESSED we _have_ to read the entire extent in so we can |
| 7537 | * decompress it, so there will be buffering required no matter what we |
| 7538 | * do, so go ahead and fallback to buffered. |
| 7539 | * |
| 7540 | * We return -ENOTBLK because thats what makes DIO go ahead and go back |
| 7541 | * to buffered IO. Don't blame me, this is the price we pay for using |
| 7542 | * the generic code. |
| 7543 | */ |
| 7544 | if (test_bit(EXTENT_FLAG_COMPRESSED, &em->flags) || |
| 7545 | em->block_start == EXTENT_MAP_INLINE) { |
| 7546 | free_extent_map(em); |
Josef Bacik | eb838e7 | 2012-07-31 16:28:48 -0400 | [diff] [blame] | 7547 | ret = -ENOTBLK; |
| 7548 | goto unlock_err; |
Josef Bacik | 4b46fce | 2010-05-23 11:00:55 -0400 | [diff] [blame] | 7549 | } |
| 7550 | |
| 7551 | /* Just a good old fashioned hole, return */ |
| 7552 | if (!create && (em->block_start == EXTENT_MAP_HOLE || |
| 7553 | test_bit(EXTENT_FLAG_PREALLOC, &em->flags))) { |
| 7554 | free_extent_map(em); |
Josef Bacik | eb838e7 | 2012-07-31 16:28:48 -0400 | [diff] [blame] | 7555 | goto unlock_err; |
Josef Bacik | 4b46fce | 2010-05-23 11:00:55 -0400 | [diff] [blame] | 7556 | } |
| 7557 | |
| 7558 | /* |
| 7559 | * We don't allocate a new extent in the following cases |
| 7560 | * |
| 7561 | * 1) The inode is marked as NODATACOW. In this case we'll just use the |
| 7562 | * existing extent. |
| 7563 | * 2) The extent is marked as PREALLOC. We're good to go here and can |
| 7564 | * just use the extent. |
| 7565 | * |
| 7566 | */ |
Chris Mason | 46bfbb5 | 2010-05-26 11:04:10 -0400 | [diff] [blame] | 7567 | if (!create) { |
Josef Bacik | eb838e7 | 2012-07-31 16:28:48 -0400 | [diff] [blame] | 7568 | len = min(len, em->len - (start - em->start)); |
| 7569 | lockstart = start + len; |
| 7570 | goto unlock; |
Chris Mason | 46bfbb5 | 2010-05-26 11:04:10 -0400 | [diff] [blame] | 7571 | } |
Josef Bacik | 4b46fce | 2010-05-23 11:00:55 -0400 | [diff] [blame] | 7572 | |
| 7573 | if (test_bit(EXTENT_FLAG_PREALLOC, &em->flags) || |
| 7574 | ((BTRFS_I(inode)->flags & BTRFS_INODE_NODATACOW) && |
| 7575 | em->block_start != EXTENT_MAP_HOLE)) { |
Josef Bacik | 4b46fce | 2010-05-23 11:00:55 -0400 | [diff] [blame] | 7576 | int type; |
Josef Bacik | eb384b5 | 2013-04-24 16:32:55 -0400 | [diff] [blame] | 7577 | u64 block_start, orig_start, orig_block_len, ram_bytes; |
Josef Bacik | 4b46fce | 2010-05-23 11:00:55 -0400 | [diff] [blame] | 7578 | |
| 7579 | if (test_bit(EXTENT_FLAG_PREALLOC, &em->flags)) |
| 7580 | type = BTRFS_ORDERED_PREALLOC; |
| 7581 | else |
| 7582 | type = BTRFS_ORDERED_NOCOW; |
Chris Mason | 46bfbb5 | 2010-05-26 11:04:10 -0400 | [diff] [blame] | 7583 | len = min(len, em->len - (start - em->start)); |
Josef Bacik | 4b46fce | 2010-05-23 11:00:55 -0400 | [diff] [blame] | 7584 | block_start = em->block_start + (start - em->start); |
Chris Mason | 46bfbb5 | 2010-05-26 11:04:10 -0400 | [diff] [blame] | 7585 | |
Josef Bacik | 0036158 | 2013-08-14 14:02:47 -0400 | [diff] [blame] | 7586 | if (can_nocow_extent(inode, start, &len, &orig_start, |
Josef Bacik | 7ee9e44 | 2013-06-21 16:37:03 -0400 | [diff] [blame] | 7587 | &orig_block_len, &ram_bytes) == 1) { |
Josef Bacik | 69ffb54 | 2012-09-11 15:40:07 -0400 | [diff] [blame] | 7588 | if (type == BTRFS_ORDERED_PREALLOC) { |
| 7589 | free_extent_map(em); |
| 7590 | em = create_pinned_em(inode, start, len, |
| 7591 | orig_start, |
Josef Bacik | b493968 | 2012-12-03 10:31:19 -0500 | [diff] [blame] | 7592 | block_start, len, |
Josef Bacik | cc95bef | 2013-04-04 14:31:27 -0400 | [diff] [blame] | 7593 | orig_block_len, |
| 7594 | ram_bytes, type); |
Filipe Manana | 555e128 | 2014-07-07 12:35:21 +0100 | [diff] [blame] | 7595 | if (IS_ERR(em)) { |
| 7596 | ret = PTR_ERR(em); |
Josef Bacik | 69ffb54 | 2012-09-11 15:40:07 -0400 | [diff] [blame] | 7597 | goto unlock_err; |
Filipe Manana | 555e128 | 2014-07-07 12:35:21 +0100 | [diff] [blame] | 7598 | } |
Josef Bacik | 69ffb54 | 2012-09-11 15:40:07 -0400 | [diff] [blame] | 7599 | } |
| 7600 | |
Chris Mason | 46bfbb5 | 2010-05-26 11:04:10 -0400 | [diff] [blame] | 7601 | ret = btrfs_add_ordered_extent_dio(inode, start, |
| 7602 | block_start, len, len, type); |
Chris Mason | 46bfbb5 | 2010-05-26 11:04:10 -0400 | [diff] [blame] | 7603 | if (ret) { |
| 7604 | free_extent_map(em); |
Josef Bacik | eb838e7 | 2012-07-31 16:28:48 -0400 | [diff] [blame] | 7605 | goto unlock_err; |
Chris Mason | 46bfbb5 | 2010-05-26 11:04:10 -0400 | [diff] [blame] | 7606 | } |
| 7607 | goto unlock; |
Josef Bacik | 4b46fce | 2010-05-23 11:00:55 -0400 | [diff] [blame] | 7608 | } |
Josef Bacik | 4b46fce | 2010-05-23 11:00:55 -0400 | [diff] [blame] | 7609 | } |
Josef Bacik | 0036158 | 2013-08-14 14:02:47 -0400 | [diff] [blame] | 7610 | |
Chris Mason | 46bfbb5 | 2010-05-26 11:04:10 -0400 | [diff] [blame] | 7611 | /* |
| 7612 | * this will cow the extent, reset the len in case we changed |
| 7613 | * it above |
| 7614 | */ |
| 7615 | len = bh_result->b_size; |
Josef Bacik | 70c8a91 | 2012-10-11 16:54:30 -0400 | [diff] [blame] | 7616 | free_extent_map(em); |
| 7617 | em = btrfs_new_extent_direct(inode, start, len); |
Josef Bacik | eb838e7 | 2012-07-31 16:28:48 -0400 | [diff] [blame] | 7618 | if (IS_ERR(em)) { |
| 7619 | ret = PTR_ERR(em); |
| 7620 | goto unlock_err; |
| 7621 | } |
Chris Mason | 46bfbb5 | 2010-05-26 11:04:10 -0400 | [diff] [blame] | 7622 | len = min(len, em->len - (start - em->start)); |
| 7623 | unlock: |
Josef Bacik | 4b46fce | 2010-05-23 11:00:55 -0400 | [diff] [blame] | 7624 | bh_result->b_blocknr = (em->block_start + (start - em->start)) >> |
| 7625 | inode->i_blkbits; |
Chris Mason | 46bfbb5 | 2010-05-26 11:04:10 -0400 | [diff] [blame] | 7626 | bh_result->b_size = len; |
Josef Bacik | 4b46fce | 2010-05-23 11:00:55 -0400 | [diff] [blame] | 7627 | bh_result->b_bdev = em->bdev; |
| 7628 | set_buffer_mapped(bh_result); |
Josef Bacik | c3473e8 | 2012-06-19 10:59:00 -0400 | [diff] [blame] | 7629 | if (create) { |
| 7630 | if (!test_bit(EXTENT_FLAG_PREALLOC, &em->flags)) |
| 7631 | set_buffer_new(bh_result); |
| 7632 | |
| 7633 | /* |
| 7634 | * Need to update the i_size under the extent lock so buffered |
| 7635 | * readers will get the updated i_size when we unlock. |
| 7636 | */ |
| 7637 | if (start + len > i_size_read(inode)) |
| 7638 | i_size_write(inode, start + len); |
Miao Xie | 0934856 | 2013-02-07 10:12:07 +0000 | [diff] [blame] | 7639 | |
Josef Bacik | e1cbbfa | 2015-03-17 10:52:28 -0400 | [diff] [blame] | 7640 | /* |
| 7641 | * If we have an outstanding_extents count still set then we're |
| 7642 | * within our reservation, otherwise we need to adjust our inode |
| 7643 | * counter appropriately. |
| 7644 | */ |
chandan | 50745b0 | 2015-08-28 21:10:13 +0530 | [diff] [blame] | 7645 | if (dio_data->outstanding_extents) { |
| 7646 | (dio_data->outstanding_extents)--; |
Josef Bacik | e1cbbfa | 2015-03-17 10:52:28 -0400 | [diff] [blame] | 7647 | } else { |
Josef Bacik | 3e05bde | 2015-02-11 15:08:57 -0500 | [diff] [blame] | 7648 | spin_lock(&BTRFS_I(inode)->lock); |
| 7649 | BTRFS_I(inode)->outstanding_extents++; |
| 7650 | spin_unlock(&BTRFS_I(inode)->lock); |
| 7651 | } |
Josef Bacik | e1cbbfa | 2015-03-17 10:52:28 -0400 | [diff] [blame] | 7652 | |
Qu Wenruo | df48063 | 2015-09-08 17:25:54 +0800 | [diff] [blame^] | 7653 | __btrfs_free_reserved_data_space(inode, start, len); |
chandan | 50745b0 | 2015-08-28 21:10:13 +0530 | [diff] [blame] | 7654 | WARN_ON(dio_data->reserve < len); |
| 7655 | dio_data->reserve -= len; |
| 7656 | current->journal_info = dio_data; |
Josef Bacik | c3473e8 | 2012-06-19 10:59:00 -0400 | [diff] [blame] | 7657 | } |
Josef Bacik | 4b46fce | 2010-05-23 11:00:55 -0400 | [diff] [blame] | 7658 | |
Josef Bacik | eb838e7 | 2012-07-31 16:28:48 -0400 | [diff] [blame] | 7659 | /* |
| 7660 | * In the case of write we need to clear and unlock the entire range, |
| 7661 | * in the case of read we need to unlock only the end area that we |
| 7662 | * aren't using if there is any left over space. |
| 7663 | */ |
Liu Bo | 24c03fa | 2012-08-22 20:10:38 -0600 | [diff] [blame] | 7664 | if (lockstart < lockend) { |
Miao Xie | 0934856 | 2013-02-07 10:12:07 +0000 | [diff] [blame] | 7665 | clear_extent_bit(&BTRFS_I(inode)->io_tree, lockstart, |
| 7666 | lockend, unlock_bits, 1, 0, |
| 7667 | &cached_state, GFP_NOFS); |
Liu Bo | 24c03fa | 2012-08-22 20:10:38 -0600 | [diff] [blame] | 7668 | } else { |
Josef Bacik | eb838e7 | 2012-07-31 16:28:48 -0400 | [diff] [blame] | 7669 | free_extent_state(cached_state); |
Liu Bo | 24c03fa | 2012-08-22 20:10:38 -0600 | [diff] [blame] | 7670 | } |
Josef Bacik | eb838e7 | 2012-07-31 16:28:48 -0400 | [diff] [blame] | 7671 | |
Josef Bacik | 4b46fce | 2010-05-23 11:00:55 -0400 | [diff] [blame] | 7672 | free_extent_map(em); |
| 7673 | |
| 7674 | return 0; |
Josef Bacik | eb838e7 | 2012-07-31 16:28:48 -0400 | [diff] [blame] | 7675 | |
| 7676 | unlock_err: |
Josef Bacik | eb838e7 | 2012-07-31 16:28:48 -0400 | [diff] [blame] | 7677 | clear_extent_bit(&BTRFS_I(inode)->io_tree, lockstart, lockend, |
| 7678 | unlock_bits, 1, 0, &cached_state, GFP_NOFS); |
chandan | 50745b0 | 2015-08-28 21:10:13 +0530 | [diff] [blame] | 7679 | if (dio_data) |
| 7680 | current->journal_info = dio_data; |
Josef Bacik | eb838e7 | 2012-07-31 16:28:48 -0400 | [diff] [blame] | 7681 | return ret; |
Josef Bacik | 4b46fce | 2010-05-23 11:00:55 -0400 | [diff] [blame] | 7682 | } |
| 7683 | |
Miao Xie | 8b110e3 | 2014-09-12 18:44:03 +0800 | [diff] [blame] | 7684 | static inline int submit_dio_repair_bio(struct inode *inode, struct bio *bio, |
| 7685 | int rw, int mirror_num) |
| 7686 | { |
| 7687 | struct btrfs_root *root = BTRFS_I(inode)->root; |
| 7688 | int ret; |
| 7689 | |
| 7690 | BUG_ON(rw & REQ_WRITE); |
| 7691 | |
| 7692 | bio_get(bio); |
| 7693 | |
| 7694 | ret = btrfs_bio_wq_end_io(root->fs_info, bio, |
| 7695 | BTRFS_WQ_ENDIO_DIO_REPAIR); |
| 7696 | if (ret) |
| 7697 | goto err; |
| 7698 | |
| 7699 | ret = btrfs_map_bio(root, rw, bio, mirror_num, 0); |
| 7700 | err: |
| 7701 | bio_put(bio); |
| 7702 | return ret; |
| 7703 | } |
| 7704 | |
| 7705 | static int btrfs_check_dio_repairable(struct inode *inode, |
| 7706 | struct bio *failed_bio, |
| 7707 | struct io_failure_record *failrec, |
| 7708 | int failed_mirror) |
| 7709 | { |
| 7710 | int num_copies; |
| 7711 | |
| 7712 | num_copies = btrfs_num_copies(BTRFS_I(inode)->root->fs_info, |
| 7713 | failrec->logical, failrec->len); |
| 7714 | if (num_copies == 1) { |
| 7715 | /* |
| 7716 | * we only have a single copy of the data, so don't bother with |
| 7717 | * all the retry and error correction code that follows. no |
| 7718 | * matter what the error is, it is very likely to persist. |
| 7719 | */ |
| 7720 | pr_debug("Check DIO Repairable: cannot repair, num_copies=%d, next_mirror %d, failed_mirror %d\n", |
| 7721 | num_copies, failrec->this_mirror, failed_mirror); |
| 7722 | return 0; |
| 7723 | } |
| 7724 | |
| 7725 | failrec->failed_mirror = failed_mirror; |
| 7726 | failrec->this_mirror++; |
| 7727 | if (failrec->this_mirror == failed_mirror) |
| 7728 | failrec->this_mirror++; |
| 7729 | |
| 7730 | if (failrec->this_mirror > num_copies) { |
| 7731 | pr_debug("Check DIO Repairable: (fail) num_copies=%d, next_mirror %d, failed_mirror %d\n", |
| 7732 | num_copies, failrec->this_mirror, failed_mirror); |
| 7733 | return 0; |
| 7734 | } |
| 7735 | |
| 7736 | return 1; |
| 7737 | } |
| 7738 | |
| 7739 | static int dio_read_error(struct inode *inode, struct bio *failed_bio, |
| 7740 | struct page *page, u64 start, u64 end, |
| 7741 | int failed_mirror, bio_end_io_t *repair_endio, |
| 7742 | void *repair_arg) |
| 7743 | { |
| 7744 | struct io_failure_record *failrec; |
| 7745 | struct bio *bio; |
| 7746 | int isector; |
| 7747 | int read_mode; |
| 7748 | int ret; |
| 7749 | |
| 7750 | BUG_ON(failed_bio->bi_rw & REQ_WRITE); |
| 7751 | |
| 7752 | ret = btrfs_get_io_failure_record(inode, start, end, &failrec); |
| 7753 | if (ret) |
| 7754 | return ret; |
| 7755 | |
| 7756 | ret = btrfs_check_dio_repairable(inode, failed_bio, failrec, |
| 7757 | failed_mirror); |
| 7758 | if (!ret) { |
| 7759 | free_io_failure(inode, failrec); |
| 7760 | return -EIO; |
| 7761 | } |
| 7762 | |
| 7763 | if (failed_bio->bi_vcnt > 1) |
| 7764 | read_mode = READ_SYNC | REQ_FAILFAST_DEV; |
| 7765 | else |
| 7766 | read_mode = READ_SYNC; |
| 7767 | |
| 7768 | isector = start - btrfs_io_bio(failed_bio)->logical; |
| 7769 | isector >>= inode->i_sb->s_blocksize_bits; |
| 7770 | bio = btrfs_create_repair_bio(inode, failed_bio, failrec, page, |
| 7771 | 0, isector, repair_endio, repair_arg); |
| 7772 | if (!bio) { |
| 7773 | free_io_failure(inode, failrec); |
| 7774 | return -EIO; |
| 7775 | } |
| 7776 | |
| 7777 | btrfs_debug(BTRFS_I(inode)->root->fs_info, |
| 7778 | "Repair DIO Read Error: submitting new dio read[%#x] to this_mirror=%d, in_validation=%d\n", |
| 7779 | read_mode, failrec->this_mirror, failrec->in_validation); |
| 7780 | |
| 7781 | ret = submit_dio_repair_bio(inode, bio, read_mode, |
| 7782 | failrec->this_mirror); |
| 7783 | if (ret) { |
| 7784 | free_io_failure(inode, failrec); |
| 7785 | bio_put(bio); |
| 7786 | } |
| 7787 | |
| 7788 | return ret; |
| 7789 | } |
| 7790 | |
| 7791 | struct btrfs_retry_complete { |
| 7792 | struct completion done; |
| 7793 | struct inode *inode; |
| 7794 | u64 start; |
| 7795 | int uptodate; |
| 7796 | }; |
| 7797 | |
Christoph Hellwig | 4246a0b | 2015-07-20 15:29:37 +0200 | [diff] [blame] | 7798 | static void btrfs_retry_endio_nocsum(struct bio *bio) |
Miao Xie | 8b110e3 | 2014-09-12 18:44:03 +0800 | [diff] [blame] | 7799 | { |
| 7800 | struct btrfs_retry_complete *done = bio->bi_private; |
| 7801 | struct bio_vec *bvec; |
| 7802 | int i; |
| 7803 | |
Christoph Hellwig | 4246a0b | 2015-07-20 15:29:37 +0200 | [diff] [blame] | 7804 | if (bio->bi_error) |
Miao Xie | 8b110e3 | 2014-09-12 18:44:03 +0800 | [diff] [blame] | 7805 | goto end; |
| 7806 | |
| 7807 | done->uptodate = 1; |
| 7808 | bio_for_each_segment_all(bvec, bio, i) |
| 7809 | clean_io_failure(done->inode, done->start, bvec->bv_page, 0); |
| 7810 | end: |
| 7811 | complete(&done->done); |
| 7812 | bio_put(bio); |
| 7813 | } |
| 7814 | |
| 7815 | static int __btrfs_correct_data_nocsum(struct inode *inode, |
| 7816 | struct btrfs_io_bio *io_bio) |
Josef Bacik | 4b46fce | 2010-05-23 11:00:55 -0400 | [diff] [blame] | 7817 | { |
Kent Overstreet | 2c30c71 | 2013-11-07 12:20:26 -0800 | [diff] [blame] | 7818 | struct bio_vec *bvec; |
Miao Xie | 8b110e3 | 2014-09-12 18:44:03 +0800 | [diff] [blame] | 7819 | struct btrfs_retry_complete done; |
Josef Bacik | 4b46fce | 2010-05-23 11:00:55 -0400 | [diff] [blame] | 7820 | u64 start; |
Kent Overstreet | 2c30c71 | 2013-11-07 12:20:26 -0800 | [diff] [blame] | 7821 | int i; |
Miao Xie | c1dc089 | 2014-09-12 18:43:56 +0800 | [diff] [blame] | 7822 | int ret; |
Miao Xie | dc380ae | 2014-09-12 18:43:55 +0800 | [diff] [blame] | 7823 | |
Miao Xie | c1dc089 | 2014-09-12 18:43:56 +0800 | [diff] [blame] | 7824 | start = io_bio->logical; |
Miao Xie | 8b110e3 | 2014-09-12 18:44:03 +0800 | [diff] [blame] | 7825 | done.inode = inode; |
| 7826 | |
| 7827 | bio_for_each_segment_all(bvec, &io_bio->bio, i) { |
| 7828 | try_again: |
| 7829 | done.uptodate = 0; |
| 7830 | done.start = start; |
| 7831 | init_completion(&done.done); |
| 7832 | |
| 7833 | ret = dio_read_error(inode, &io_bio->bio, bvec->bv_page, start, |
| 7834 | start + bvec->bv_len - 1, |
| 7835 | io_bio->mirror_num, |
| 7836 | btrfs_retry_endio_nocsum, &done); |
| 7837 | if (ret) |
| 7838 | return ret; |
| 7839 | |
| 7840 | wait_for_completion(&done.done); |
| 7841 | |
| 7842 | if (!done.uptodate) { |
| 7843 | /* We might have another mirror, so try again */ |
| 7844 | goto try_again; |
| 7845 | } |
| 7846 | |
| 7847 | start += bvec->bv_len; |
| 7848 | } |
| 7849 | |
| 7850 | return 0; |
| 7851 | } |
| 7852 | |
Christoph Hellwig | 4246a0b | 2015-07-20 15:29:37 +0200 | [diff] [blame] | 7853 | static void btrfs_retry_endio(struct bio *bio) |
Miao Xie | 8b110e3 | 2014-09-12 18:44:03 +0800 | [diff] [blame] | 7854 | { |
| 7855 | struct btrfs_retry_complete *done = bio->bi_private; |
| 7856 | struct btrfs_io_bio *io_bio = btrfs_io_bio(bio); |
| 7857 | struct bio_vec *bvec; |
| 7858 | int uptodate; |
| 7859 | int ret; |
| 7860 | int i; |
| 7861 | |
Christoph Hellwig | 4246a0b | 2015-07-20 15:29:37 +0200 | [diff] [blame] | 7862 | if (bio->bi_error) |
Miao Xie | 8b110e3 | 2014-09-12 18:44:03 +0800 | [diff] [blame] | 7863 | goto end; |
| 7864 | |
| 7865 | uptodate = 1; |
| 7866 | bio_for_each_segment_all(bvec, bio, i) { |
| 7867 | ret = __readpage_endio_check(done->inode, io_bio, i, |
| 7868 | bvec->bv_page, 0, |
| 7869 | done->start, bvec->bv_len); |
| 7870 | if (!ret) |
| 7871 | clean_io_failure(done->inode, done->start, |
| 7872 | bvec->bv_page, 0); |
| 7873 | else |
| 7874 | uptodate = 0; |
| 7875 | } |
| 7876 | |
| 7877 | done->uptodate = uptodate; |
| 7878 | end: |
| 7879 | complete(&done->done); |
| 7880 | bio_put(bio); |
| 7881 | } |
| 7882 | |
| 7883 | static int __btrfs_subio_endio_read(struct inode *inode, |
| 7884 | struct btrfs_io_bio *io_bio, int err) |
| 7885 | { |
| 7886 | struct bio_vec *bvec; |
| 7887 | struct btrfs_retry_complete done; |
| 7888 | u64 start; |
| 7889 | u64 offset = 0; |
| 7890 | int i; |
| 7891 | int ret; |
| 7892 | |
| 7893 | err = 0; |
| 7894 | start = io_bio->logical; |
| 7895 | done.inode = inode; |
| 7896 | |
Miao Xie | c1dc089 | 2014-09-12 18:43:56 +0800 | [diff] [blame] | 7897 | bio_for_each_segment_all(bvec, &io_bio->bio, i) { |
Miao Xie | dc380ae | 2014-09-12 18:43:55 +0800 | [diff] [blame] | 7898 | ret = __readpage_endio_check(inode, io_bio, i, bvec->bv_page, |
| 7899 | 0, start, bvec->bv_len); |
Miao Xie | 8b110e3 | 2014-09-12 18:44:03 +0800 | [diff] [blame] | 7900 | if (likely(!ret)) |
| 7901 | goto next; |
| 7902 | try_again: |
| 7903 | done.uptodate = 0; |
| 7904 | done.start = start; |
| 7905 | init_completion(&done.done); |
| 7906 | |
| 7907 | ret = dio_read_error(inode, &io_bio->bio, bvec->bv_page, start, |
| 7908 | start + bvec->bv_len - 1, |
| 7909 | io_bio->mirror_num, |
| 7910 | btrfs_retry_endio, &done); |
| 7911 | if (ret) { |
| 7912 | err = ret; |
| 7913 | goto next; |
| 7914 | } |
| 7915 | |
| 7916 | wait_for_completion(&done.done); |
| 7917 | |
| 7918 | if (!done.uptodate) { |
| 7919 | /* We might have another mirror, so try again */ |
| 7920 | goto try_again; |
| 7921 | } |
| 7922 | next: |
| 7923 | offset += bvec->bv_len; |
Josef Bacik | 4b46fce | 2010-05-23 11:00:55 -0400 | [diff] [blame] | 7924 | start += bvec->bv_len; |
Kent Overstreet | 2c30c71 | 2013-11-07 12:20:26 -0800 | [diff] [blame] | 7925 | } |
Miao Xie | c1dc089 | 2014-09-12 18:43:56 +0800 | [diff] [blame] | 7926 | |
| 7927 | return err; |
| 7928 | } |
| 7929 | |
Miao Xie | 8b110e3 | 2014-09-12 18:44:03 +0800 | [diff] [blame] | 7930 | static int btrfs_subio_endio_read(struct inode *inode, |
| 7931 | struct btrfs_io_bio *io_bio, int err) |
| 7932 | { |
| 7933 | bool skip_csum = BTRFS_I(inode)->flags & BTRFS_INODE_NODATASUM; |
| 7934 | |
| 7935 | if (skip_csum) { |
| 7936 | if (unlikely(err)) |
| 7937 | return __btrfs_correct_data_nocsum(inode, io_bio); |
| 7938 | else |
| 7939 | return 0; |
| 7940 | } else { |
| 7941 | return __btrfs_subio_endio_read(inode, io_bio, err); |
| 7942 | } |
| 7943 | } |
| 7944 | |
Christoph Hellwig | 4246a0b | 2015-07-20 15:29:37 +0200 | [diff] [blame] | 7945 | static void btrfs_endio_direct_read(struct bio *bio) |
Miao Xie | c1dc089 | 2014-09-12 18:43:56 +0800 | [diff] [blame] | 7946 | { |
| 7947 | struct btrfs_dio_private *dip = bio->bi_private; |
| 7948 | struct inode *inode = dip->inode; |
| 7949 | struct bio *dio_bio; |
| 7950 | struct btrfs_io_bio *io_bio = btrfs_io_bio(bio); |
Christoph Hellwig | 4246a0b | 2015-07-20 15:29:37 +0200 | [diff] [blame] | 7951 | int err = bio->bi_error; |
Miao Xie | c1dc089 | 2014-09-12 18:43:56 +0800 | [diff] [blame] | 7952 | |
Miao Xie | 8b110e3 | 2014-09-12 18:44:03 +0800 | [diff] [blame] | 7953 | if (dip->flags & BTRFS_DIO_ORIG_BIO_SUBMITTED) |
| 7954 | err = btrfs_subio_endio_read(inode, io_bio, err); |
Miao Xie | c1dc089 | 2014-09-12 18:43:56 +0800 | [diff] [blame] | 7955 | |
Josef Bacik | 4b46fce | 2010-05-23 11:00:55 -0400 | [diff] [blame] | 7956 | unlock_extent(&BTRFS_I(inode)->io_tree, dip->logical_offset, |
Jeff Mahoney | d008237 | 2012-03-01 14:57:19 +0100 | [diff] [blame] | 7957 | dip->logical_offset + dip->bytes - 1); |
Chris Mason | 9be3395 | 2013-05-17 18:30:14 -0400 | [diff] [blame] | 7958 | dio_bio = dip->dio_bio; |
Josef Bacik | 4b46fce | 2010-05-23 11:00:55 -0400 | [diff] [blame] | 7959 | |
Josef Bacik | 4b46fce | 2010-05-23 11:00:55 -0400 | [diff] [blame] | 7960 | kfree(dip); |
Josef Bacik | c0da7aa | 2011-03-22 11:05:07 -0400 | [diff] [blame] | 7961 | |
Christoph Hellwig | 4246a0b | 2015-07-20 15:29:37 +0200 | [diff] [blame] | 7962 | dio_end_io(dio_bio, bio->bi_error); |
Miao Xie | 23ea8e5 | 2014-09-12 18:43:54 +0800 | [diff] [blame] | 7963 | |
| 7964 | if (io_bio->end_io) |
| 7965 | io_bio->end_io(io_bio, err); |
Chris Mason | 9be3395 | 2013-05-17 18:30:14 -0400 | [diff] [blame] | 7966 | bio_put(bio); |
Josef Bacik | 4b46fce | 2010-05-23 11:00:55 -0400 | [diff] [blame] | 7967 | } |
| 7968 | |
Christoph Hellwig | 4246a0b | 2015-07-20 15:29:37 +0200 | [diff] [blame] | 7969 | static void btrfs_endio_direct_write(struct bio *bio) |
Josef Bacik | 4b46fce | 2010-05-23 11:00:55 -0400 | [diff] [blame] | 7970 | { |
| 7971 | struct btrfs_dio_private *dip = bio->bi_private; |
| 7972 | struct inode *inode = dip->inode; |
| 7973 | struct btrfs_root *root = BTRFS_I(inode)->root; |
Josef Bacik | 4b46fce | 2010-05-23 11:00:55 -0400 | [diff] [blame] | 7974 | struct btrfs_ordered_extent *ordered = NULL; |
Chris Mason | 163cf09 | 2010-11-28 19:56:33 -0500 | [diff] [blame] | 7975 | u64 ordered_offset = dip->logical_offset; |
| 7976 | u64 ordered_bytes = dip->bytes; |
Chris Mason | 9be3395 | 2013-05-17 18:30:14 -0400 | [diff] [blame] | 7977 | struct bio *dio_bio; |
Josef Bacik | 4b46fce | 2010-05-23 11:00:55 -0400 | [diff] [blame] | 7978 | int ret; |
| 7979 | |
Chris Mason | 163cf09 | 2010-11-28 19:56:33 -0500 | [diff] [blame] | 7980 | again: |
| 7981 | ret = btrfs_dec_test_first_ordered_pending(inode, &ordered, |
| 7982 | &ordered_offset, |
Christoph Hellwig | 4246a0b | 2015-07-20 15:29:37 +0200 | [diff] [blame] | 7983 | ordered_bytes, |
| 7984 | !bio->bi_error); |
Josef Bacik | 4b46fce | 2010-05-23 11:00:55 -0400 | [diff] [blame] | 7985 | if (!ret) |
Chris Mason | 163cf09 | 2010-11-28 19:56:33 -0500 | [diff] [blame] | 7986 | goto out_test; |
Josef Bacik | 4b46fce | 2010-05-23 11:00:55 -0400 | [diff] [blame] | 7987 | |
Liu Bo | 9e0af23 | 2014-08-15 23:36:53 +0800 | [diff] [blame] | 7988 | btrfs_init_work(&ordered->work, btrfs_endio_write_helper, |
| 7989 | finish_ordered_fn, NULL, NULL); |
Qu Wenruo | fccb5d8 | 2014-02-28 10:46:10 +0800 | [diff] [blame] | 7990 | btrfs_queue_work(root->fs_info->endio_write_workers, |
| 7991 | &ordered->work); |
Chris Mason | 163cf09 | 2010-11-28 19:56:33 -0500 | [diff] [blame] | 7992 | out_test: |
| 7993 | /* |
| 7994 | * our bio might span multiple ordered extents. If we haven't |
| 7995 | * completed the accounting for the whole dio, go back and try again |
| 7996 | */ |
| 7997 | if (ordered_offset < dip->logical_offset + dip->bytes) { |
| 7998 | ordered_bytes = dip->logical_offset + dip->bytes - |
| 7999 | ordered_offset; |
Josef Bacik | 5fd0204 | 2012-05-02 14:00:54 -0400 | [diff] [blame] | 8000 | ordered = NULL; |
Chris Mason | 163cf09 | 2010-11-28 19:56:33 -0500 | [diff] [blame] | 8001 | goto again; |
| 8002 | } |
Chris Mason | 9be3395 | 2013-05-17 18:30:14 -0400 | [diff] [blame] | 8003 | dio_bio = dip->dio_bio; |
Josef Bacik | 4b46fce | 2010-05-23 11:00:55 -0400 | [diff] [blame] | 8004 | |
Josef Bacik | 4b46fce | 2010-05-23 11:00:55 -0400 | [diff] [blame] | 8005 | kfree(dip); |
Josef Bacik | c0da7aa | 2011-03-22 11:05:07 -0400 | [diff] [blame] | 8006 | |
Christoph Hellwig | 4246a0b | 2015-07-20 15:29:37 +0200 | [diff] [blame] | 8007 | dio_end_io(dio_bio, bio->bi_error); |
Chris Mason | 9be3395 | 2013-05-17 18:30:14 -0400 | [diff] [blame] | 8008 | bio_put(bio); |
Josef Bacik | 4b46fce | 2010-05-23 11:00:55 -0400 | [diff] [blame] | 8009 | } |
| 8010 | |
Chris Mason | eaf25d9 | 2010-05-25 09:48:28 -0400 | [diff] [blame] | 8011 | static int __btrfs_submit_bio_start_direct_io(struct inode *inode, int rw, |
| 8012 | struct bio *bio, int mirror_num, |
| 8013 | unsigned long bio_flags, u64 offset) |
| 8014 | { |
| 8015 | int ret; |
| 8016 | struct btrfs_root *root = BTRFS_I(inode)->root; |
| 8017 | ret = btrfs_csum_one_bio(root, inode, bio, offset, 1); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 8018 | BUG_ON(ret); /* -ENOMEM */ |
Chris Mason | eaf25d9 | 2010-05-25 09:48:28 -0400 | [diff] [blame] | 8019 | return 0; |
| 8020 | } |
| 8021 | |
Christoph Hellwig | 4246a0b | 2015-07-20 15:29:37 +0200 | [diff] [blame] | 8022 | static void btrfs_end_dio_bio(struct bio *bio) |
Miao Xie | e65e153 | 2010-11-22 03:04:43 +0000 | [diff] [blame] | 8023 | { |
| 8024 | struct btrfs_dio_private *dip = bio->bi_private; |
Christoph Hellwig | 4246a0b | 2015-07-20 15:29:37 +0200 | [diff] [blame] | 8025 | int err = bio->bi_error; |
Miao Xie | e65e153 | 2010-11-22 03:04:43 +0000 | [diff] [blame] | 8026 | |
Miao Xie | 8b110e3 | 2014-09-12 18:44:03 +0800 | [diff] [blame] | 8027 | if (err) |
| 8028 | btrfs_warn(BTRFS_I(dip->inode)->root->fs_info, |
| 8029 | "direct IO failed ino %llu rw %lu sector %#Lx len %u err no %d", |
| 8030 | btrfs_ino(dip->inode), bio->bi_rw, |
| 8031 | (unsigned long long)bio->bi_iter.bi_sector, |
| 8032 | bio->bi_iter.bi_size, err); |
| 8033 | |
| 8034 | if (dip->subio_endio) |
| 8035 | err = dip->subio_endio(dip->inode, btrfs_io_bio(bio), err); |
Miao Xie | c1dc089 | 2014-09-12 18:43:56 +0800 | [diff] [blame] | 8036 | |
| 8037 | if (err) { |
Miao Xie | e65e153 | 2010-11-22 03:04:43 +0000 | [diff] [blame] | 8038 | dip->errors = 1; |
| 8039 | |
| 8040 | /* |
| 8041 | * before atomic variable goto zero, we must make sure |
| 8042 | * dip->errors is perceived to be set. |
| 8043 | */ |
Peter Zijlstra | 4e857c5 | 2014-03-17 18:06:10 +0100 | [diff] [blame] | 8044 | smp_mb__before_atomic(); |
Miao Xie | e65e153 | 2010-11-22 03:04:43 +0000 | [diff] [blame] | 8045 | } |
| 8046 | |
| 8047 | /* if there are more bios still pending for this dio, just exit */ |
| 8048 | if (!atomic_dec_and_test(&dip->pending_bios)) |
| 8049 | goto out; |
| 8050 | |
Chris Mason | 9be3395 | 2013-05-17 18:30:14 -0400 | [diff] [blame] | 8051 | if (dip->errors) { |
Miao Xie | e65e153 | 2010-11-22 03:04:43 +0000 | [diff] [blame] | 8052 | bio_io_error(dip->orig_bio); |
Chris Mason | 9be3395 | 2013-05-17 18:30:14 -0400 | [diff] [blame] | 8053 | } else { |
Christoph Hellwig | 4246a0b | 2015-07-20 15:29:37 +0200 | [diff] [blame] | 8054 | dip->dio_bio->bi_error = 0; |
| 8055 | bio_endio(dip->orig_bio); |
Miao Xie | e65e153 | 2010-11-22 03:04:43 +0000 | [diff] [blame] | 8056 | } |
| 8057 | out: |
| 8058 | bio_put(bio); |
| 8059 | } |
| 8060 | |
| 8061 | static struct bio *btrfs_dio_bio_alloc(struct block_device *bdev, |
| 8062 | u64 first_sector, gfp_t gfp_flags) |
| 8063 | { |
Chris Mason | da2f0f7 | 2015-07-02 13:57:22 -0700 | [diff] [blame] | 8064 | struct bio *bio; |
Linus Torvalds | 2236597 | 2015-09-05 15:14:43 -0700 | [diff] [blame] | 8065 | bio = btrfs_bio_alloc(bdev, first_sector, BIO_MAX_PAGES, gfp_flags); |
Chris Mason | da2f0f7 | 2015-07-02 13:57:22 -0700 | [diff] [blame] | 8066 | if (bio) |
| 8067 | bio_associate_current(bio); |
| 8068 | return bio; |
Miao Xie | e65e153 | 2010-11-22 03:04:43 +0000 | [diff] [blame] | 8069 | } |
| 8070 | |
Miao Xie | c1dc089 | 2014-09-12 18:43:56 +0800 | [diff] [blame] | 8071 | static inline int btrfs_lookup_and_bind_dio_csum(struct btrfs_root *root, |
| 8072 | struct inode *inode, |
| 8073 | struct btrfs_dio_private *dip, |
| 8074 | struct bio *bio, |
| 8075 | u64 file_offset) |
| 8076 | { |
| 8077 | struct btrfs_io_bio *io_bio = btrfs_io_bio(bio); |
| 8078 | struct btrfs_io_bio *orig_io_bio = btrfs_io_bio(dip->orig_bio); |
| 8079 | int ret; |
| 8080 | |
| 8081 | /* |
| 8082 | * We load all the csum data we need when we submit |
| 8083 | * the first bio to reduce the csum tree search and |
| 8084 | * contention. |
| 8085 | */ |
| 8086 | if (dip->logical_offset == file_offset) { |
| 8087 | ret = btrfs_lookup_bio_sums_dio(root, inode, dip->orig_bio, |
| 8088 | file_offset); |
| 8089 | if (ret) |
| 8090 | return ret; |
| 8091 | } |
| 8092 | |
| 8093 | if (bio == dip->orig_bio) |
| 8094 | return 0; |
| 8095 | |
| 8096 | file_offset -= dip->logical_offset; |
| 8097 | file_offset >>= inode->i_sb->s_blocksize_bits; |
| 8098 | io_bio->csum = (u8 *)(((u32 *)orig_io_bio->csum) + file_offset); |
| 8099 | |
| 8100 | return 0; |
| 8101 | } |
| 8102 | |
Miao Xie | e65e153 | 2010-11-22 03:04:43 +0000 | [diff] [blame] | 8103 | static inline int __btrfs_submit_dio_bio(struct bio *bio, struct inode *inode, |
| 8104 | int rw, u64 file_offset, int skip_sum, |
Josef Bacik | c329861 | 2012-08-03 16:49:19 -0400 | [diff] [blame] | 8105 | int async_submit) |
Miao Xie | e65e153 | 2010-11-22 03:04:43 +0000 | [diff] [blame] | 8106 | { |
Miao Xie | facc8a22 | 2013-07-25 19:22:34 +0800 | [diff] [blame] | 8107 | struct btrfs_dio_private *dip = bio->bi_private; |
Miao Xie | e65e153 | 2010-11-22 03:04:43 +0000 | [diff] [blame] | 8108 | int write = rw & REQ_WRITE; |
| 8109 | struct btrfs_root *root = BTRFS_I(inode)->root; |
| 8110 | int ret; |
| 8111 | |
Josef Bacik | b812ce2 | 2012-11-16 13:56:32 -0500 | [diff] [blame] | 8112 | if (async_submit) |
| 8113 | async_submit = !atomic_read(&BTRFS_I(inode)->sync_writers); |
| 8114 | |
Miao Xie | e65e153 | 2010-11-22 03:04:43 +0000 | [diff] [blame] | 8115 | bio_get(bio); |
Josef Bacik | 5fd0204 | 2012-05-02 14:00:54 -0400 | [diff] [blame] | 8116 | |
| 8117 | if (!write) { |
David Sterba | bfebd8b | 2014-07-30 00:25:45 +0200 | [diff] [blame] | 8118 | ret = btrfs_bio_wq_end_io(root->fs_info, bio, |
| 8119 | BTRFS_WQ_ENDIO_DATA); |
Josef Bacik | 5fd0204 | 2012-05-02 14:00:54 -0400 | [diff] [blame] | 8120 | if (ret) |
| 8121 | goto err; |
| 8122 | } |
Miao Xie | e65e153 | 2010-11-22 03:04:43 +0000 | [diff] [blame] | 8123 | |
Josef Bacik | 1ae3993 | 2011-04-06 14:41:34 -0400 | [diff] [blame] | 8124 | if (skip_sum) |
| 8125 | goto map; |
| 8126 | |
| 8127 | if (write && async_submit) { |
Miao Xie | e65e153 | 2010-11-22 03:04:43 +0000 | [diff] [blame] | 8128 | ret = btrfs_wq_submit_bio(root->fs_info, |
| 8129 | inode, rw, bio, 0, 0, |
| 8130 | file_offset, |
| 8131 | __btrfs_submit_bio_start_direct_io, |
| 8132 | __btrfs_submit_bio_done); |
| 8133 | goto err; |
Josef Bacik | 1ae3993 | 2011-04-06 14:41:34 -0400 | [diff] [blame] | 8134 | } else if (write) { |
| 8135 | /* |
| 8136 | * If we aren't doing async submit, calculate the csum of the |
| 8137 | * bio now. |
| 8138 | */ |
| 8139 | ret = btrfs_csum_one_bio(root, inode, bio, file_offset, 1); |
| 8140 | if (ret) |
| 8141 | goto err; |
Miao Xie | 23ea8e5 | 2014-09-12 18:43:54 +0800 | [diff] [blame] | 8142 | } else { |
Miao Xie | c1dc089 | 2014-09-12 18:43:56 +0800 | [diff] [blame] | 8143 | ret = btrfs_lookup_and_bind_dio_csum(root, inode, dip, bio, |
| 8144 | file_offset); |
Tsutomu Itoh | c2db107 | 2011-03-01 06:48:31 +0000 | [diff] [blame] | 8145 | if (ret) |
| 8146 | goto err; |
| 8147 | } |
Josef Bacik | 1ae3993 | 2011-04-06 14:41:34 -0400 | [diff] [blame] | 8148 | map: |
| 8149 | ret = btrfs_map_bio(root, rw, bio, 0, async_submit); |
Miao Xie | e65e153 | 2010-11-22 03:04:43 +0000 | [diff] [blame] | 8150 | err: |
| 8151 | bio_put(bio); |
| 8152 | return ret; |
| 8153 | } |
| 8154 | |
| 8155 | static int btrfs_submit_direct_hook(int rw, struct btrfs_dio_private *dip, |
| 8156 | int skip_sum) |
| 8157 | { |
| 8158 | struct inode *inode = dip->inode; |
| 8159 | struct btrfs_root *root = BTRFS_I(inode)->root; |
Miao Xie | e65e153 | 2010-11-22 03:04:43 +0000 | [diff] [blame] | 8160 | struct bio *bio; |
| 8161 | struct bio *orig_bio = dip->orig_bio; |
| 8162 | struct bio_vec *bvec = orig_bio->bi_io_vec; |
Kent Overstreet | 4f024f3 | 2013-10-11 15:44:27 -0700 | [diff] [blame] | 8163 | u64 start_sector = orig_bio->bi_iter.bi_sector; |
Miao Xie | e65e153 | 2010-11-22 03:04:43 +0000 | [diff] [blame] | 8164 | u64 file_offset = dip->logical_offset; |
| 8165 | u64 submit_len = 0; |
| 8166 | u64 map_length; |
| 8167 | int nr_pages = 0; |
Miao Xie | 23ea8e5 | 2014-09-12 18:43:54 +0800 | [diff] [blame] | 8168 | int ret; |
Josef Bacik | 1ae3993 | 2011-04-06 14:41:34 -0400 | [diff] [blame] | 8169 | int async_submit = 0; |
Miao Xie | e65e153 | 2010-11-22 03:04:43 +0000 | [diff] [blame] | 8170 | |
Kent Overstreet | 4f024f3 | 2013-10-11 15:44:27 -0700 | [diff] [blame] | 8171 | map_length = orig_bio->bi_iter.bi_size; |
David Woodhouse | 53b381b | 2013-01-29 18:40:14 -0500 | [diff] [blame] | 8172 | ret = btrfs_map_block(root->fs_info, rw, start_sector << 9, |
Miao Xie | e65e153 | 2010-11-22 03:04:43 +0000 | [diff] [blame] | 8173 | &map_length, NULL, 0); |
Miao Xie | 7a5c3c9 | 2014-06-17 18:58:59 +0800 | [diff] [blame] | 8174 | if (ret) |
Miao Xie | e65e153 | 2010-11-22 03:04:43 +0000 | [diff] [blame] | 8175 | return -EIO; |
Miao Xie | facc8a22 | 2013-07-25 19:22:34 +0800 | [diff] [blame] | 8176 | |
Kent Overstreet | 4f024f3 | 2013-10-11 15:44:27 -0700 | [diff] [blame] | 8177 | if (map_length >= orig_bio->bi_iter.bi_size) { |
Josef Bacik | 02f57c7 | 2011-04-06 14:25:44 -0400 | [diff] [blame] | 8178 | bio = orig_bio; |
Miao Xie | c1dc089 | 2014-09-12 18:43:56 +0800 | [diff] [blame] | 8179 | dip->flags |= BTRFS_DIO_ORIG_BIO_SUBMITTED; |
Josef Bacik | 02f57c7 | 2011-04-06 14:25:44 -0400 | [diff] [blame] | 8180 | goto submit; |
| 8181 | } |
| 8182 | |
David Woodhouse | 53b381b | 2013-01-29 18:40:14 -0500 | [diff] [blame] | 8183 | /* async crcs make it difficult to collect full stripe writes. */ |
Zhao Lei | ffe2d20 | 2015-01-20 15:11:44 +0800 | [diff] [blame] | 8184 | if (btrfs_get_alloc_profile(root, 1) & BTRFS_BLOCK_GROUP_RAID56_MASK) |
David Woodhouse | 53b381b | 2013-01-29 18:40:14 -0500 | [diff] [blame] | 8185 | async_submit = 0; |
| 8186 | else |
| 8187 | async_submit = 1; |
| 8188 | |
Josef Bacik | 02f57c7 | 2011-04-06 14:25:44 -0400 | [diff] [blame] | 8189 | bio = btrfs_dio_bio_alloc(orig_bio->bi_bdev, start_sector, GFP_NOFS); |
| 8190 | if (!bio) |
| 8191 | return -ENOMEM; |
Miao Xie | 7a5c3c9 | 2014-06-17 18:58:59 +0800 | [diff] [blame] | 8192 | |
Josef Bacik | 02f57c7 | 2011-04-06 14:25:44 -0400 | [diff] [blame] | 8193 | bio->bi_private = dip; |
| 8194 | bio->bi_end_io = btrfs_end_dio_bio; |
Miao Xie | c1dc089 | 2014-09-12 18:43:56 +0800 | [diff] [blame] | 8195 | btrfs_io_bio(bio)->logical = file_offset; |
Josef Bacik | 02f57c7 | 2011-04-06 14:25:44 -0400 | [diff] [blame] | 8196 | atomic_inc(&dip->pending_bios); |
| 8197 | |
Miao Xie | e65e153 | 2010-11-22 03:04:43 +0000 | [diff] [blame] | 8198 | while (bvec <= (orig_bio->bi_io_vec + orig_bio->bi_vcnt - 1)) { |
David Sterba | ee39b43 | 2014-09-30 01:33:33 +0200 | [diff] [blame] | 8199 | if (map_length < submit_len + bvec->bv_len || |
Miao Xie | e65e153 | 2010-11-22 03:04:43 +0000 | [diff] [blame] | 8200 | bio_add_page(bio, bvec->bv_page, bvec->bv_len, |
David Sterba | ee39b43 | 2014-09-30 01:33:33 +0200 | [diff] [blame] | 8201 | bvec->bv_offset) < bvec->bv_len) { |
Miao Xie | e65e153 | 2010-11-22 03:04:43 +0000 | [diff] [blame] | 8202 | /* |
| 8203 | * inc the count before we submit the bio so |
| 8204 | * we know the end IO handler won't happen before |
| 8205 | * we inc the count. Otherwise, the dip might get freed |
| 8206 | * before we're done setting it up |
| 8207 | */ |
| 8208 | atomic_inc(&dip->pending_bios); |
| 8209 | ret = __btrfs_submit_dio_bio(bio, inode, rw, |
| 8210 | file_offset, skip_sum, |
Josef Bacik | c329861 | 2012-08-03 16:49:19 -0400 | [diff] [blame] | 8211 | async_submit); |
Miao Xie | e65e153 | 2010-11-22 03:04:43 +0000 | [diff] [blame] | 8212 | if (ret) { |
| 8213 | bio_put(bio); |
| 8214 | atomic_dec(&dip->pending_bios); |
| 8215 | goto out_err; |
| 8216 | } |
| 8217 | |
Miao Xie | e65e153 | 2010-11-22 03:04:43 +0000 | [diff] [blame] | 8218 | start_sector += submit_len >> 9; |
| 8219 | file_offset += submit_len; |
| 8220 | |
| 8221 | submit_len = 0; |
| 8222 | nr_pages = 0; |
| 8223 | |
| 8224 | bio = btrfs_dio_bio_alloc(orig_bio->bi_bdev, |
| 8225 | start_sector, GFP_NOFS); |
| 8226 | if (!bio) |
| 8227 | goto out_err; |
| 8228 | bio->bi_private = dip; |
| 8229 | bio->bi_end_io = btrfs_end_dio_bio; |
Miao Xie | c1dc089 | 2014-09-12 18:43:56 +0800 | [diff] [blame] | 8230 | btrfs_io_bio(bio)->logical = file_offset; |
Miao Xie | e65e153 | 2010-11-22 03:04:43 +0000 | [diff] [blame] | 8231 | |
Kent Overstreet | 4f024f3 | 2013-10-11 15:44:27 -0700 | [diff] [blame] | 8232 | map_length = orig_bio->bi_iter.bi_size; |
David Woodhouse | 53b381b | 2013-01-29 18:40:14 -0500 | [diff] [blame] | 8233 | ret = btrfs_map_block(root->fs_info, rw, |
Stefan Behrens | 3ec706c | 2012-11-05 15:46:42 +0100 | [diff] [blame] | 8234 | start_sector << 9, |
Miao Xie | e65e153 | 2010-11-22 03:04:43 +0000 | [diff] [blame] | 8235 | &map_length, NULL, 0); |
| 8236 | if (ret) { |
| 8237 | bio_put(bio); |
| 8238 | goto out_err; |
| 8239 | } |
| 8240 | } else { |
| 8241 | submit_len += bvec->bv_len; |
Dulshani Gunawardhana | 6787125 | 2013-10-31 10:33:04 +0530 | [diff] [blame] | 8242 | nr_pages++; |
Miao Xie | e65e153 | 2010-11-22 03:04:43 +0000 | [diff] [blame] | 8243 | bvec++; |
| 8244 | } |
| 8245 | } |
| 8246 | |
Josef Bacik | 02f57c7 | 2011-04-06 14:25:44 -0400 | [diff] [blame] | 8247 | submit: |
Miao Xie | e65e153 | 2010-11-22 03:04:43 +0000 | [diff] [blame] | 8248 | ret = __btrfs_submit_dio_bio(bio, inode, rw, file_offset, skip_sum, |
Josef Bacik | c329861 | 2012-08-03 16:49:19 -0400 | [diff] [blame] | 8249 | async_submit); |
Miao Xie | e65e153 | 2010-11-22 03:04:43 +0000 | [diff] [blame] | 8250 | if (!ret) |
| 8251 | return 0; |
| 8252 | |
| 8253 | bio_put(bio); |
| 8254 | out_err: |
| 8255 | dip->errors = 1; |
| 8256 | /* |
| 8257 | * before atomic variable goto zero, we must |
| 8258 | * make sure dip->errors is perceived to be set. |
| 8259 | */ |
Peter Zijlstra | 4e857c5 | 2014-03-17 18:06:10 +0100 | [diff] [blame] | 8260 | smp_mb__before_atomic(); |
Miao Xie | e65e153 | 2010-11-22 03:04:43 +0000 | [diff] [blame] | 8261 | if (atomic_dec_and_test(&dip->pending_bios)) |
| 8262 | bio_io_error(dip->orig_bio); |
| 8263 | |
| 8264 | /* bio_end_io() will handle error, so we needn't return it */ |
| 8265 | return 0; |
| 8266 | } |
| 8267 | |
Chris Mason | 9be3395 | 2013-05-17 18:30:14 -0400 | [diff] [blame] | 8268 | static void btrfs_submit_direct(int rw, struct bio *dio_bio, |
| 8269 | struct inode *inode, loff_t file_offset) |
Josef Bacik | 4b46fce | 2010-05-23 11:00:55 -0400 | [diff] [blame] | 8270 | { |
Filipe Manana | 61de718 | 2015-07-01 12:13:10 +0100 | [diff] [blame] | 8271 | struct btrfs_dio_private *dip = NULL; |
| 8272 | struct bio *io_bio = NULL; |
Miao Xie | 23ea8e5 | 2014-09-12 18:43:54 +0800 | [diff] [blame] | 8273 | struct btrfs_io_bio *btrfs_bio; |
Josef Bacik | 4b46fce | 2010-05-23 11:00:55 -0400 | [diff] [blame] | 8274 | int skip_sum; |
Christoph Hellwig | 7b6d91d | 2010-08-07 18:20:39 +0200 | [diff] [blame] | 8275 | int write = rw & REQ_WRITE; |
Josef Bacik | 4b46fce | 2010-05-23 11:00:55 -0400 | [diff] [blame] | 8276 | int ret = 0; |
| 8277 | |
| 8278 | skip_sum = BTRFS_I(inode)->flags & BTRFS_INODE_NODATASUM; |
| 8279 | |
Chris Mason | 9be3395 | 2013-05-17 18:30:14 -0400 | [diff] [blame] | 8280 | io_bio = btrfs_bio_clone(dio_bio, GFP_NOFS); |
Chris Mason | 9be3395 | 2013-05-17 18:30:14 -0400 | [diff] [blame] | 8281 | if (!io_bio) { |
Josef Bacik | 4b46fce | 2010-05-23 11:00:55 -0400 | [diff] [blame] | 8282 | ret = -ENOMEM; |
| 8283 | goto free_ordered; |
| 8284 | } |
Josef Bacik | 4b46fce | 2010-05-23 11:00:55 -0400 | [diff] [blame] | 8285 | |
Miao Xie | c1dc089 | 2014-09-12 18:43:56 +0800 | [diff] [blame] | 8286 | dip = kzalloc(sizeof(*dip), GFP_NOFS); |
Chris Mason | 9be3395 | 2013-05-17 18:30:14 -0400 | [diff] [blame] | 8287 | if (!dip) { |
| 8288 | ret = -ENOMEM; |
Filipe Manana | 61de718 | 2015-07-01 12:13:10 +0100 | [diff] [blame] | 8289 | goto free_ordered; |
Chris Mason | 9be3395 | 2013-05-17 18:30:14 -0400 | [diff] [blame] | 8290 | } |
| 8291 | |
| 8292 | dip->private = dio_bio->bi_private; |
Josef Bacik | 4b46fce | 2010-05-23 11:00:55 -0400 | [diff] [blame] | 8293 | dip->inode = inode; |
| 8294 | dip->logical_offset = file_offset; |
Kent Overstreet | 4f024f3 | 2013-10-11 15:44:27 -0700 | [diff] [blame] | 8295 | dip->bytes = dio_bio->bi_iter.bi_size; |
| 8296 | dip->disk_bytenr = (u64)dio_bio->bi_iter.bi_sector << 9; |
Chris Mason | 9be3395 | 2013-05-17 18:30:14 -0400 | [diff] [blame] | 8297 | io_bio->bi_private = dip; |
Chris Mason | 9be3395 | 2013-05-17 18:30:14 -0400 | [diff] [blame] | 8298 | dip->orig_bio = io_bio; |
| 8299 | dip->dio_bio = dio_bio; |
Miao Xie | e65e153 | 2010-11-22 03:04:43 +0000 | [diff] [blame] | 8300 | atomic_set(&dip->pending_bios, 0); |
Miao Xie | c1dc089 | 2014-09-12 18:43:56 +0800 | [diff] [blame] | 8301 | btrfs_bio = btrfs_io_bio(io_bio); |
| 8302 | btrfs_bio->logical = file_offset; |
Josef Bacik | 4b46fce | 2010-05-23 11:00:55 -0400 | [diff] [blame] | 8303 | |
Miao Xie | c1dc089 | 2014-09-12 18:43:56 +0800 | [diff] [blame] | 8304 | if (write) { |
Chris Mason | 9be3395 | 2013-05-17 18:30:14 -0400 | [diff] [blame] | 8305 | io_bio->bi_end_io = btrfs_endio_direct_write; |
Miao Xie | c1dc089 | 2014-09-12 18:43:56 +0800 | [diff] [blame] | 8306 | } else { |
Chris Mason | 9be3395 | 2013-05-17 18:30:14 -0400 | [diff] [blame] | 8307 | io_bio->bi_end_io = btrfs_endio_direct_read; |
Miao Xie | c1dc089 | 2014-09-12 18:43:56 +0800 | [diff] [blame] | 8308 | dip->subio_endio = btrfs_subio_endio_read; |
| 8309 | } |
Josef Bacik | 4b46fce | 2010-05-23 11:00:55 -0400 | [diff] [blame] | 8310 | |
Miao Xie | e65e153 | 2010-11-22 03:04:43 +0000 | [diff] [blame] | 8311 | ret = btrfs_submit_direct_hook(rw, dip, skip_sum); |
| 8312 | if (!ret) |
Chris Mason | eaf25d9 | 2010-05-25 09:48:28 -0400 | [diff] [blame] | 8313 | return; |
Chris Mason | 9be3395 | 2013-05-17 18:30:14 -0400 | [diff] [blame] | 8314 | |
Miao Xie | 23ea8e5 | 2014-09-12 18:43:54 +0800 | [diff] [blame] | 8315 | if (btrfs_bio->end_io) |
| 8316 | btrfs_bio->end_io(btrfs_bio, ret); |
Chris Mason | 9be3395 | 2013-05-17 18:30:14 -0400 | [diff] [blame] | 8317 | |
Josef Bacik | 4b46fce | 2010-05-23 11:00:55 -0400 | [diff] [blame] | 8318 | free_ordered: |
| 8319 | /* |
Filipe Manana | 61de718 | 2015-07-01 12:13:10 +0100 | [diff] [blame] | 8320 | * If we arrived here it means either we failed to submit the dip |
| 8321 | * or we either failed to clone the dio_bio or failed to allocate the |
| 8322 | * dip. If we cloned the dio_bio and allocated the dip, we can just |
| 8323 | * call bio_endio against our io_bio so that we get proper resource |
| 8324 | * cleanup if we fail to submit the dip, otherwise, we must do the |
| 8325 | * same as btrfs_endio_direct_[write|read] because we can't call these |
| 8326 | * callbacks - they require an allocated dip and a clone of dio_bio. |
Josef Bacik | 4b46fce | 2010-05-23 11:00:55 -0400 | [diff] [blame] | 8327 | */ |
Filipe Manana | 61de718 | 2015-07-01 12:13:10 +0100 | [diff] [blame] | 8328 | if (io_bio && dip) { |
Christoph Hellwig | 4246a0b | 2015-07-20 15:29:37 +0200 | [diff] [blame] | 8329 | io_bio->bi_error = -EIO; |
| 8330 | bio_endio(io_bio); |
Filipe Manana | 61de718 | 2015-07-01 12:13:10 +0100 | [diff] [blame] | 8331 | /* |
| 8332 | * The end io callbacks free our dip, do the final put on io_bio |
| 8333 | * and all the cleanup and final put for dio_bio (through |
| 8334 | * dio_end_io()). |
| 8335 | */ |
| 8336 | dip = NULL; |
| 8337 | io_bio = NULL; |
| 8338 | } else { |
| 8339 | if (write) { |
| 8340 | struct btrfs_ordered_extent *ordered; |
| 8341 | |
| 8342 | ordered = btrfs_lookup_ordered_extent(inode, |
| 8343 | file_offset); |
| 8344 | set_bit(BTRFS_ORDERED_IOERR, &ordered->flags); |
| 8345 | /* |
| 8346 | * Decrements our ref on the ordered extent and removes |
| 8347 | * the ordered extent from the inode's ordered tree, |
| 8348 | * doing all the proper resource cleanup such as for the |
| 8349 | * reserved space and waking up any waiters for this |
| 8350 | * ordered extent (through btrfs_remove_ordered_extent). |
| 8351 | */ |
| 8352 | btrfs_finish_ordered_io(ordered); |
| 8353 | } else { |
| 8354 | unlock_extent(&BTRFS_I(inode)->io_tree, file_offset, |
| 8355 | file_offset + dio_bio->bi_iter.bi_size - 1); |
| 8356 | } |
Christoph Hellwig | 4246a0b | 2015-07-20 15:29:37 +0200 | [diff] [blame] | 8357 | dio_bio->bi_error = -EIO; |
Filipe Manana | 61de718 | 2015-07-01 12:13:10 +0100 | [diff] [blame] | 8358 | /* |
| 8359 | * Releases and cleans up our dio_bio, no need to bio_put() |
| 8360 | * nor bio_endio()/bio_io_error() against dio_bio. |
| 8361 | */ |
| 8362 | dio_end_io(dio_bio, ret); |
Josef Bacik | 4b46fce | 2010-05-23 11:00:55 -0400 | [diff] [blame] | 8363 | } |
Filipe Manana | 61de718 | 2015-07-01 12:13:10 +0100 | [diff] [blame] | 8364 | if (io_bio) |
| 8365 | bio_put(io_bio); |
| 8366 | kfree(dip); |
Josef Bacik | 4b46fce | 2010-05-23 11:00:55 -0400 | [diff] [blame] | 8367 | } |
| 8368 | |
Omar Sandoval | 6f67376 | 2015-03-16 04:33:52 -0700 | [diff] [blame] | 8369 | 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] | 8370 | const struct iov_iter *iter, loff_t offset) |
Chris Mason | 5a5f79b | 2010-05-26 21:33:37 -0400 | [diff] [blame] | 8371 | { |
| 8372 | int seg; |
Josef Bacik | a1b75f7 | 2011-04-08 15:51:18 +0000 | [diff] [blame] | 8373 | int i; |
Chris Mason | 5a5f79b | 2010-05-26 21:33:37 -0400 | [diff] [blame] | 8374 | unsigned blocksize_mask = root->sectorsize - 1; |
| 8375 | ssize_t retval = -EINVAL; |
Chris Mason | 5a5f79b | 2010-05-26 21:33:37 -0400 | [diff] [blame] | 8376 | |
| 8377 | if (offset & blocksize_mask) |
| 8378 | goto out; |
| 8379 | |
Al Viro | 28060d5 | 2014-03-22 05:15:17 -0400 | [diff] [blame] | 8380 | if (iov_iter_alignment(iter) & blocksize_mask) |
| 8381 | goto out; |
Josef Bacik | a1b75f7 | 2011-04-08 15:51:18 +0000 | [diff] [blame] | 8382 | |
Al Viro | 28060d5 | 2014-03-22 05:15:17 -0400 | [diff] [blame] | 8383 | /* If this is a write we don't need to check anymore */ |
Omar Sandoval | 6f67376 | 2015-03-16 04:33:52 -0700 | [diff] [blame] | 8384 | if (iov_iter_rw(iter) == WRITE) |
Al Viro | 28060d5 | 2014-03-22 05:15:17 -0400 | [diff] [blame] | 8385 | return 0; |
| 8386 | /* |
| 8387 | * Check to make sure we don't have duplicate iov_base's in this |
| 8388 | * iovec, if so return EINVAL, otherwise we'll get csum errors |
| 8389 | * when reading back. |
| 8390 | */ |
| 8391 | for (seg = 0; seg < iter->nr_segs; seg++) { |
| 8392 | for (i = seg + 1; i < iter->nr_segs; i++) { |
| 8393 | if (iter->iov[seg].iov_base == iter->iov[i].iov_base) |
Josef Bacik | a1b75f7 | 2011-04-08 15:51:18 +0000 | [diff] [blame] | 8394 | goto out; |
| 8395 | } |
Chris Mason | 5a5f79b | 2010-05-26 21:33:37 -0400 | [diff] [blame] | 8396 | } |
| 8397 | retval = 0; |
| 8398 | out: |
| 8399 | return retval; |
| 8400 | } |
Josef Bacik | eb838e7 | 2012-07-31 16:28:48 -0400 | [diff] [blame] | 8401 | |
Omar Sandoval | 22c6186 | 2015-03-16 04:33:53 -0700 | [diff] [blame] | 8402 | static ssize_t btrfs_direct_IO(struct kiocb *iocb, struct iov_iter *iter, |
| 8403 | loff_t offset) |
Chris Mason | 1643298 | 2008-04-10 10:23:21 -0400 | [diff] [blame] | 8404 | { |
Josef Bacik | 4b46fce | 2010-05-23 11:00:55 -0400 | [diff] [blame] | 8405 | struct file *file = iocb->ki_filp; |
| 8406 | struct inode *inode = file->f_mapping->host; |
chandan | 50745b0 | 2015-08-28 21:10:13 +0530 | [diff] [blame] | 8407 | struct btrfs_root *root = BTRFS_I(inode)->root; |
| 8408 | struct btrfs_dio_data dio_data = { 0 }; |
Miao Xie | 0934856 | 2013-02-07 10:12:07 +0000 | [diff] [blame] | 8409 | size_t count = 0; |
Miao Xie | 2e60a51 | 2013-02-08 07:01:08 +0000 | [diff] [blame] | 8410 | int flags = 0; |
Miao Xie | 38851cc | 2013-02-08 07:04:11 +0000 | [diff] [blame] | 8411 | bool wakeup = true; |
| 8412 | bool relock = false; |
Miao Xie | 0934856 | 2013-02-07 10:12:07 +0000 | [diff] [blame] | 8413 | ssize_t ret; |
Josef Bacik | 4b46fce | 2010-05-23 11:00:55 -0400 | [diff] [blame] | 8414 | |
Omar Sandoval | 6f67376 | 2015-03-16 04:33:52 -0700 | [diff] [blame] | 8415 | if (check_direct_IO(BTRFS_I(inode)->root, iocb, iter, offset)) |
Chris Mason | 5a5f79b | 2010-05-26 21:33:37 -0400 | [diff] [blame] | 8416 | return 0; |
Chris Mason | 5a5f79b | 2010-05-26 21:33:37 -0400 | [diff] [blame] | 8417 | |
Jens Axboe | fe0f07d | 2015-04-15 17:05:48 -0600 | [diff] [blame] | 8418 | inode_dio_begin(inode); |
Peter Zijlstra | 4e857c5 | 2014-03-17 18:06:10 +0100 | [diff] [blame] | 8419 | smp_mb__after_atomic(); |
Miao Xie | 38851cc | 2013-02-08 07:04:11 +0000 | [diff] [blame] | 8420 | |
Josef Bacik | 0e267c4 | 2013-07-02 10:38:02 -0400 | [diff] [blame] | 8421 | /* |
Miao Xie | 41bd9ca | 2014-03-06 13:54:57 +0800 | [diff] [blame] | 8422 | * The generic stuff only does filemap_write_and_wait_range, which |
| 8423 | * isn't enough if we've written compressed pages to this area, so |
| 8424 | * we need to flush the dirty pages again to make absolutely sure |
| 8425 | * that any outstanding dirty pages are on disk. |
Josef Bacik | 0e267c4 | 2013-07-02 10:38:02 -0400 | [diff] [blame] | 8426 | */ |
Al Viro | a6cbcd4 | 2014-03-04 22:38:00 -0500 | [diff] [blame] | 8427 | count = iov_iter_count(iter); |
Miao Xie | 41bd9ca | 2014-03-06 13:54:57 +0800 | [diff] [blame] | 8428 | if (test_bit(BTRFS_INODE_HAS_ASYNC_EXTENT, |
| 8429 | &BTRFS_I(inode)->runtime_flags)) |
Wang Shilong | 9a025a0 | 2014-07-17 11:44:13 +0800 | [diff] [blame] | 8430 | filemap_fdatawrite_range(inode->i_mapping, offset, |
| 8431 | offset + count - 1); |
Josef Bacik | 0e267c4 | 2013-07-02 10:38:02 -0400 | [diff] [blame] | 8432 | |
Omar Sandoval | 6f67376 | 2015-03-16 04:33:52 -0700 | [diff] [blame] | 8433 | if (iov_iter_rw(iter) == WRITE) { |
Miao Xie | 38851cc | 2013-02-08 07:04:11 +0000 | [diff] [blame] | 8434 | /* |
| 8435 | * If the write DIO is beyond the EOF, we need update |
| 8436 | * the isize, but it is protected by i_mutex. So we can |
| 8437 | * not unlock the i_mutex at this case. |
| 8438 | */ |
| 8439 | if (offset + count <= inode->i_size) { |
| 8440 | mutex_unlock(&inode->i_mutex); |
| 8441 | relock = true; |
| 8442 | } |
Qu Wenruo | df48063 | 2015-09-08 17:25:54 +0800 | [diff] [blame^] | 8443 | ret = __btrfs_delalloc_reserve_space(inode, offset, count); |
Miao Xie | 0934856 | 2013-02-07 10:12:07 +0000 | [diff] [blame] | 8444 | if (ret) |
Miao Xie | 38851cc | 2013-02-08 07:04:11 +0000 | [diff] [blame] | 8445 | goto out; |
chandan | 50745b0 | 2015-08-28 21:10:13 +0530 | [diff] [blame] | 8446 | dio_data.outstanding_extents = div64_u64(count + |
Josef Bacik | e1cbbfa | 2015-03-17 10:52:28 -0400 | [diff] [blame] | 8447 | BTRFS_MAX_EXTENT_SIZE - 1, |
| 8448 | BTRFS_MAX_EXTENT_SIZE); |
| 8449 | |
| 8450 | /* |
| 8451 | * We need to know how many extents we reserved so that we can |
| 8452 | * do the accounting properly if we go over the number we |
| 8453 | * originally calculated. Abuse current->journal_info for this. |
| 8454 | */ |
chandan | 50745b0 | 2015-08-28 21:10:13 +0530 | [diff] [blame] | 8455 | dio_data.reserve = round_up(count, root->sectorsize); |
| 8456 | current->journal_info = &dio_data; |
David Sterba | ee39b43 | 2014-09-30 01:33:33 +0200 | [diff] [blame] | 8457 | } else if (test_bit(BTRFS_INODE_READDIO_NEED_LOCK, |
| 8458 | &BTRFS_I(inode)->runtime_flags)) { |
Jens Axboe | fe0f07d | 2015-04-15 17:05:48 -0600 | [diff] [blame] | 8459 | inode_dio_end(inode); |
Miao Xie | 38851cc | 2013-02-08 07:04:11 +0000 | [diff] [blame] | 8460 | flags = DIO_LOCKING | DIO_SKIP_HOLES; |
| 8461 | wakeup = false; |
Miao Xie | 0934856 | 2013-02-07 10:12:07 +0000 | [diff] [blame] | 8462 | } |
| 8463 | |
Omar Sandoval | 17f8c84 | 2015-03-16 04:33:50 -0700 | [diff] [blame] | 8464 | ret = __blockdev_direct_IO(iocb, inode, |
| 8465 | BTRFS_I(inode)->root->fs_info->fs_devices->latest_bdev, |
| 8466 | iter, offset, btrfs_get_blocks_direct, NULL, |
| 8467 | btrfs_submit_direct, flags); |
Omar Sandoval | 6f67376 | 2015-03-16 04:33:52 -0700 | [diff] [blame] | 8468 | if (iov_iter_rw(iter) == WRITE) { |
Josef Bacik | e1cbbfa | 2015-03-17 10:52:28 -0400 | [diff] [blame] | 8469 | current->journal_info = NULL; |
Liu Bo | ddba1bf | 2015-06-17 16:59:58 +0800 | [diff] [blame] | 8470 | if (ret < 0 && ret != -EIOCBQUEUED) { |
chandan | 50745b0 | 2015-08-28 21:10:13 +0530 | [diff] [blame] | 8471 | if (dio_data.reserve) |
Qu Wenruo | df48063 | 2015-09-08 17:25:54 +0800 | [diff] [blame^] | 8472 | __btrfs_delalloc_release_space(inode, offset, |
| 8473 | dio_data.reserve); |
Liu Bo | ddba1bf | 2015-06-17 16:59:58 +0800 | [diff] [blame] | 8474 | } else if (ret >= 0 && (size_t)ret < count) |
Qu Wenruo | df48063 | 2015-09-08 17:25:54 +0800 | [diff] [blame^] | 8475 | __btrfs_delalloc_release_space(inode, offset, |
| 8476 | count - (size_t)ret); |
Miao Xie | 0934856 | 2013-02-07 10:12:07 +0000 | [diff] [blame] | 8477 | } |
Miao Xie | 38851cc | 2013-02-08 07:04:11 +0000 | [diff] [blame] | 8478 | out: |
Miao Xie | 2e60a51 | 2013-02-08 07:01:08 +0000 | [diff] [blame] | 8479 | if (wakeup) |
Jens Axboe | fe0f07d | 2015-04-15 17:05:48 -0600 | [diff] [blame] | 8480 | inode_dio_end(inode); |
Miao Xie | 38851cc | 2013-02-08 07:04:11 +0000 | [diff] [blame] | 8481 | if (relock) |
| 8482 | mutex_lock(&inode->i_mutex); |
Miao Xie | 0934856 | 2013-02-07 10:12:07 +0000 | [diff] [blame] | 8483 | |
| 8484 | return ret; |
Chris Mason | 1643298 | 2008-04-10 10:23:21 -0400 | [diff] [blame] | 8485 | } |
| 8486 | |
Tsutomu Itoh | 05dadc0 | 2012-11-29 05:08:26 +0000 | [diff] [blame] | 8487 | #define BTRFS_FIEMAP_FLAGS (FIEMAP_FLAG_SYNC) |
| 8488 | |
Yehuda Sadeh | 1506fcc | 2009-01-21 14:39:14 -0500 | [diff] [blame] | 8489 | static int btrfs_fiemap(struct inode *inode, struct fiemap_extent_info *fieinfo, |
| 8490 | __u64 start, __u64 len) |
| 8491 | { |
Tsutomu Itoh | 05dadc0 | 2012-11-29 05:08:26 +0000 | [diff] [blame] | 8492 | int ret; |
| 8493 | |
| 8494 | ret = fiemap_check_flags(fieinfo, BTRFS_FIEMAP_FLAGS); |
| 8495 | if (ret) |
| 8496 | return ret; |
| 8497 | |
Chris Mason | ec29ed5 | 2011-02-23 16:23:20 -0500 | [diff] [blame] | 8498 | return extent_fiemap(inode, fieinfo, start, len, btrfs_get_extent_fiemap); |
Yehuda Sadeh | 1506fcc | 2009-01-21 14:39:14 -0500 | [diff] [blame] | 8499 | } |
| 8500 | |
Chris Mason | 9ebefb18 | 2007-06-15 13:50:00 -0400 | [diff] [blame] | 8501 | int btrfs_readpage(struct file *file, struct page *page) |
| 8502 | { |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 8503 | struct extent_io_tree *tree; |
| 8504 | tree = &BTRFS_I(page->mapping->host)->io_tree; |
Jan Schmidt | 8ddc7d9 | 2011-06-13 20:02:58 +0200 | [diff] [blame] | 8505 | return extent_read_full_page(tree, page, btrfs_get_extent, 0); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 8506 | } |
Chris Mason | 1832a6d | 2007-12-21 16:27:21 -0500 | [diff] [blame] | 8507 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 8508 | static int btrfs_writepage(struct page *page, struct writeback_control *wbc) |
| 8509 | { |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 8510 | struct extent_io_tree *tree; |
Chris Mason | b888db2 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 8511 | |
| 8512 | |
| 8513 | if (current->flags & PF_MEMALLOC) { |
| 8514 | redirty_page_for_writepage(wbc, page); |
| 8515 | unlock_page(page); |
| 8516 | return 0; |
| 8517 | } |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 8518 | tree = &BTRFS_I(page->mapping->host)->io_tree; |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 8519 | return extent_write_full_page(tree, page, btrfs_get_extent, wbc); |
| 8520 | } |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 8521 | |
Eric Sandeen | 48a3b63 | 2013-04-25 20:41:01 +0000 | [diff] [blame] | 8522 | static int btrfs_writepages(struct address_space *mapping, |
| 8523 | struct writeback_control *wbc) |
Chris Mason | b293f02e | 2007-11-01 19:45:34 -0400 | [diff] [blame] | 8524 | { |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 8525 | struct extent_io_tree *tree; |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 8526 | |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 8527 | tree = &BTRFS_I(mapping->host)->io_tree; |
Chris Mason | b293f02e | 2007-11-01 19:45:34 -0400 | [diff] [blame] | 8528 | return extent_writepages(tree, mapping, btrfs_get_extent, wbc); |
| 8529 | } |
| 8530 | |
Chris Mason | 3ab2fb5 | 2007-11-08 10:59:22 -0500 | [diff] [blame] | 8531 | static int |
| 8532 | btrfs_readpages(struct file *file, struct address_space *mapping, |
| 8533 | struct list_head *pages, unsigned nr_pages) |
| 8534 | { |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 8535 | struct extent_io_tree *tree; |
| 8536 | tree = &BTRFS_I(mapping->host)->io_tree; |
Chris Mason | 3ab2fb5 | 2007-11-08 10:59:22 -0500 | [diff] [blame] | 8537 | return extent_readpages(tree, mapping, pages, nr_pages, |
| 8538 | btrfs_get_extent); |
| 8539 | } |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 8540 | static int __btrfs_releasepage(struct page *page, gfp_t gfp_flags) |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 8541 | { |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 8542 | struct extent_io_tree *tree; |
| 8543 | struct extent_map_tree *map; |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 8544 | int ret; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 8545 | |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 8546 | tree = &BTRFS_I(page->mapping->host)->io_tree; |
| 8547 | map = &BTRFS_I(page->mapping->host)->extent_tree; |
Chris Mason | 70dec80 | 2008-01-29 09:59:12 -0500 | [diff] [blame] | 8548 | ret = try_release_extent_mapping(map, tree, page, gfp_flags); |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 8549 | if (ret == 1) { |
| 8550 | ClearPagePrivate(page); |
| 8551 | set_page_private(page, 0); |
| 8552 | page_cache_release(page); |
| 8553 | } |
| 8554 | return ret; |
| 8555 | } |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 8556 | |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 8557 | static int btrfs_releasepage(struct page *page, gfp_t gfp_flags) |
| 8558 | { |
Chris Mason | 98509cf | 2008-09-11 15:51:43 -0400 | [diff] [blame] | 8559 | if (PageWriteback(page) || PageDirty(page)) |
| 8560 | return 0; |
Yan Zheng | b335b00 | 2009-02-12 10:06:04 -0500 | [diff] [blame] | 8561 | return __btrfs_releasepage(page, gfp_flags & GFP_NOFS); |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 8562 | } |
| 8563 | |
Lukas Czerner | d47992f | 2013-05-21 23:17:23 -0400 | [diff] [blame] | 8564 | static void btrfs_invalidatepage(struct page *page, unsigned int offset, |
| 8565 | unsigned int length) |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 8566 | { |
Josef Bacik | 5fd0204 | 2012-05-02 14:00:54 -0400 | [diff] [blame] | 8567 | struct inode *inode = page->mapping->host; |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 8568 | struct extent_io_tree *tree; |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 8569 | struct btrfs_ordered_extent *ordered; |
Josef Bacik | 2ac55d4 | 2010-02-03 19:33:23 +0000 | [diff] [blame] | 8570 | struct extent_state *cached_state = NULL; |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 8571 | u64 page_start = page_offset(page); |
| 8572 | u64 page_end = page_start + PAGE_CACHE_SIZE - 1; |
Filipe David Borba Manana | 131e404 | 2013-11-19 22:29:35 +0000 | [diff] [blame] | 8573 | int inode_evicting = inode->i_state & I_FREEING; |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 8574 | |
Chris Mason | 8b62b72 | 2009-09-02 16:53:46 -0400 | [diff] [blame] | 8575 | /* |
| 8576 | * we have the page locked, so new writeback can't start, |
| 8577 | * and the dirty bit won't be cleared while we are here. |
| 8578 | * |
| 8579 | * Wait for IO on this page so that we can safely clear |
| 8580 | * the PagePrivate2 bit and do ordered accounting |
| 8581 | */ |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 8582 | wait_on_page_writeback(page); |
Chris Mason | 8b62b72 | 2009-09-02 16:53:46 -0400 | [diff] [blame] | 8583 | |
Josef Bacik | 5fd0204 | 2012-05-02 14:00:54 -0400 | [diff] [blame] | 8584 | tree = &BTRFS_I(inode)->io_tree; |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 8585 | if (offset) { |
| 8586 | btrfs_releasepage(page, GFP_NOFS); |
| 8587 | return; |
| 8588 | } |
Filipe David Borba Manana | 131e404 | 2013-11-19 22:29:35 +0000 | [diff] [blame] | 8589 | |
| 8590 | if (!inode_evicting) |
| 8591 | lock_extent_bits(tree, page_start, page_end, 0, &cached_state); |
| 8592 | ordered = btrfs_lookup_ordered_extent(inode, page_start); |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 8593 | if (ordered) { |
Chris Mason | eb84ae0 | 2008-07-17 13:53:27 -0400 | [diff] [blame] | 8594 | /* |
| 8595 | * IO on this page will never be started, so we need |
| 8596 | * to account for any ordered extents now |
| 8597 | */ |
Filipe David Borba Manana | 131e404 | 2013-11-19 22:29:35 +0000 | [diff] [blame] | 8598 | if (!inode_evicting) |
| 8599 | clear_extent_bit(tree, page_start, page_end, |
| 8600 | EXTENT_DIRTY | EXTENT_DELALLOC | |
| 8601 | EXTENT_LOCKED | EXTENT_DO_ACCOUNTING | |
| 8602 | EXTENT_DEFRAG, 1, 0, &cached_state, |
| 8603 | GFP_NOFS); |
Chris Mason | 8b62b72 | 2009-09-02 16:53:46 -0400 | [diff] [blame] | 8604 | /* |
| 8605 | * whoever cleared the private bit is responsible |
| 8606 | * for the finish_ordered_io |
| 8607 | */ |
Josef Bacik | 77cef2e | 2013-08-29 13:57:21 -0400 | [diff] [blame] | 8608 | if (TestClearPagePrivate2(page)) { |
| 8609 | struct btrfs_ordered_inode_tree *tree; |
| 8610 | u64 new_len; |
| 8611 | |
| 8612 | tree = &BTRFS_I(inode)->ordered_tree; |
| 8613 | |
| 8614 | spin_lock_irq(&tree->lock); |
| 8615 | set_bit(BTRFS_ORDERED_TRUNCATED, &ordered->flags); |
| 8616 | new_len = page_start - ordered->file_offset; |
| 8617 | if (new_len < ordered->truncated_len) |
| 8618 | ordered->truncated_len = new_len; |
| 8619 | spin_unlock_irq(&tree->lock); |
| 8620 | |
| 8621 | if (btrfs_dec_test_ordered_pending(inode, &ordered, |
| 8622 | page_start, |
| 8623 | PAGE_CACHE_SIZE, 1)) |
| 8624 | btrfs_finish_ordered_io(ordered); |
Chris Mason | 8b62b72 | 2009-09-02 16:53:46 -0400 | [diff] [blame] | 8625 | } |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 8626 | btrfs_put_ordered_extent(ordered); |
Filipe David Borba Manana | 131e404 | 2013-11-19 22:29:35 +0000 | [diff] [blame] | 8627 | if (!inode_evicting) { |
| 8628 | cached_state = NULL; |
| 8629 | lock_extent_bits(tree, page_start, page_end, 0, |
| 8630 | &cached_state); |
| 8631 | } |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 8632 | } |
Filipe David Borba Manana | 131e404 | 2013-11-19 22:29:35 +0000 | [diff] [blame] | 8633 | |
| 8634 | if (!inode_evicting) { |
| 8635 | clear_extent_bit(tree, page_start, page_end, |
| 8636 | EXTENT_LOCKED | EXTENT_DIRTY | |
| 8637 | EXTENT_DELALLOC | EXTENT_DO_ACCOUNTING | |
| 8638 | EXTENT_DEFRAG, 1, 1, |
| 8639 | &cached_state, GFP_NOFS); |
| 8640 | |
| 8641 | __btrfs_releasepage(page, GFP_NOFS); |
| 8642 | } |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 8643 | |
Chris Mason | 4a09675 | 2008-07-21 10:29:44 -0400 | [diff] [blame] | 8644 | ClearPageChecked(page); |
Chris Mason | 9ad6b7bc | 2008-04-18 16:11:30 -0400 | [diff] [blame] | 8645 | if (PagePrivate(page)) { |
Chris Mason | 9ad6b7bc | 2008-04-18 16:11:30 -0400 | [diff] [blame] | 8646 | ClearPagePrivate(page); |
| 8647 | set_page_private(page, 0); |
| 8648 | page_cache_release(page); |
| 8649 | } |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 8650 | } |
| 8651 | |
Chris Mason | 9ebefb18 | 2007-06-15 13:50:00 -0400 | [diff] [blame] | 8652 | /* |
| 8653 | * btrfs_page_mkwrite() is not allowed to change the file size as it gets |
| 8654 | * called from a page fault handler when a page is first dirtied. Hence we must |
| 8655 | * be careful to check for EOF conditions here. We set the page up correctly |
| 8656 | * for a written page which means we get ENOSPC checking when writing into |
| 8657 | * holes and correct delalloc and unwritten extent mapping on filesystems that |
| 8658 | * support these features. |
| 8659 | * |
| 8660 | * We are not allowed to take the i_mutex here so we have to play games to |
| 8661 | * protect against truncate races as the page could now be beyond EOF. Because |
| 8662 | * vmtruncate() writes the inode size before removing pages, once we have the |
| 8663 | * page lock we can determine safely if the page is beyond EOF. If it is not |
| 8664 | * beyond EOF, then the page is guaranteed safe against truncation until we |
| 8665 | * unlock the page. |
| 8666 | */ |
Nick Piggin | c2ec175 | 2009-03-31 15:23:21 -0700 | [diff] [blame] | 8667 | 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] | 8668 | { |
Nick Piggin | c2ec175 | 2009-03-31 15:23:21 -0700 | [diff] [blame] | 8669 | struct page *page = vmf->page; |
Al Viro | 496ad9a | 2013-01-23 17:07:38 -0500 | [diff] [blame] | 8670 | struct inode *inode = file_inode(vma->vm_file); |
Chris Mason | 1832a6d | 2007-12-21 16:27:21 -0500 | [diff] [blame] | 8671 | struct btrfs_root *root = BTRFS_I(inode)->root; |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 8672 | struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree; |
| 8673 | struct btrfs_ordered_extent *ordered; |
Josef Bacik | 2ac55d4 | 2010-02-03 19:33:23 +0000 | [diff] [blame] | 8674 | struct extent_state *cached_state = NULL; |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 8675 | char *kaddr; |
| 8676 | unsigned long zero_start; |
Chris Mason | 9ebefb18 | 2007-06-15 13:50:00 -0400 | [diff] [blame] | 8677 | loff_t size; |
Chris Mason | 1832a6d | 2007-12-21 16:27:21 -0500 | [diff] [blame] | 8678 | int ret; |
Chris Mason | 9998eb7 | 2012-01-25 13:47:40 -0500 | [diff] [blame] | 8679 | int reserved = 0; |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 8680 | u64 page_start; |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 8681 | u64 page_end; |
Chris Mason | 9ebefb18 | 2007-06-15 13:50:00 -0400 | [diff] [blame] | 8682 | |
Jan Kara | b2b5ef5 | 2012-06-12 16:20:45 +0200 | [diff] [blame] | 8683 | sb_start_pagefault(inode->i_sb); |
Qu Wenruo | df48063 | 2015-09-08 17:25:54 +0800 | [diff] [blame^] | 8684 | page_start = page_offset(page); |
| 8685 | page_end = page_start + PAGE_CACHE_SIZE - 1; |
| 8686 | |
| 8687 | ret = __btrfs_delalloc_reserve_space(inode, page_start, |
| 8688 | PAGE_CACHE_SIZE); |
Chris Mason | 9998eb7 | 2012-01-25 13:47:40 -0500 | [diff] [blame] | 8689 | if (!ret) { |
Josef Bacik | e41f941 | 2012-03-26 09:46:47 -0400 | [diff] [blame] | 8690 | ret = file_update_time(vma->vm_file); |
Chris Mason | 9998eb7 | 2012-01-25 13:47:40 -0500 | [diff] [blame] | 8691 | reserved = 1; |
| 8692 | } |
Nick Piggin | 56a76f8 | 2009-03-31 15:23:23 -0700 | [diff] [blame] | 8693 | if (ret) { |
| 8694 | if (ret == -ENOMEM) |
| 8695 | ret = VM_FAULT_OOM; |
| 8696 | else /* -ENOSPC, -EIO, etc */ |
| 8697 | ret = VM_FAULT_SIGBUS; |
Chris Mason | 9998eb7 | 2012-01-25 13:47:40 -0500 | [diff] [blame] | 8698 | if (reserved) |
| 8699 | goto out; |
| 8700 | goto out_noreserve; |
Nick Piggin | 56a76f8 | 2009-03-31 15:23:23 -0700 | [diff] [blame] | 8701 | } |
Chris Mason | 1832a6d | 2007-12-21 16:27:21 -0500 | [diff] [blame] | 8702 | |
Nick Piggin | 56a76f8 | 2009-03-31 15:23:23 -0700 | [diff] [blame] | 8703 | ret = VM_FAULT_NOPAGE; /* make the VM retry the fault */ |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 8704 | again: |
Chris Mason | 9ebefb18 | 2007-06-15 13:50:00 -0400 | [diff] [blame] | 8705 | lock_page(page); |
Chris Mason | 9ebefb18 | 2007-06-15 13:50:00 -0400 | [diff] [blame] | 8706 | size = i_size_read(inode); |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 8707 | |
Chris Mason | 9ebefb18 | 2007-06-15 13:50:00 -0400 | [diff] [blame] | 8708 | if ((page->mapping != inode->i_mapping) || |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 8709 | (page_start >= size)) { |
Chris Mason | 9ebefb18 | 2007-06-15 13:50:00 -0400 | [diff] [blame] | 8710 | /* page got truncated out from underneath us */ |
| 8711 | goto out_unlock; |
| 8712 | } |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 8713 | wait_on_page_writeback(page); |
| 8714 | |
Jeff Mahoney | d008237 | 2012-03-01 14:57:19 +0100 | [diff] [blame] | 8715 | lock_extent_bits(io_tree, page_start, page_end, 0, &cached_state); |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 8716 | set_page_extent_mapped(page); |
| 8717 | |
Chris Mason | eb84ae0 | 2008-07-17 13:53:27 -0400 | [diff] [blame] | 8718 | /* |
| 8719 | * we can't set the delalloc bits if there are pending ordered |
| 8720 | * extents. Drop our locks and wait for them to finish |
| 8721 | */ |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 8722 | ordered = btrfs_lookup_ordered_extent(inode, page_start); |
| 8723 | if (ordered) { |
Josef Bacik | 2ac55d4 | 2010-02-03 19:33:23 +0000 | [diff] [blame] | 8724 | unlock_extent_cached(io_tree, page_start, page_end, |
| 8725 | &cached_state, GFP_NOFS); |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 8726 | unlock_page(page); |
Chris Mason | eb84ae0 | 2008-07-17 13:53:27 -0400 | [diff] [blame] | 8727 | btrfs_start_ordered_extent(inode, ordered, 1); |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 8728 | btrfs_put_ordered_extent(ordered); |
| 8729 | goto again; |
| 8730 | } |
| 8731 | |
Josef Bacik | fbf1908 | 2009-10-01 17:10:23 -0400 | [diff] [blame] | 8732 | /* |
| 8733 | * XXX - page_mkwrite gets called every time the page is dirtied, even |
| 8734 | * if it was already dirty, so for space accounting reasons we need to |
| 8735 | * clear any delalloc bits for the range we are fixing to save. There |
| 8736 | * is probably a better way to do this, but for now keep consistent with |
| 8737 | * prepare_pages in the normal write path. |
| 8738 | */ |
Josef Bacik | 2ac55d4 | 2010-02-03 19:33:23 +0000 | [diff] [blame] | 8739 | clear_extent_bit(&BTRFS_I(inode)->io_tree, page_start, page_end, |
Liu Bo | 9e8a4a8 | 2012-09-05 19:10:51 -0600 | [diff] [blame] | 8740 | EXTENT_DIRTY | EXTENT_DELALLOC | |
| 8741 | EXTENT_DO_ACCOUNTING | EXTENT_DEFRAG, |
Josef Bacik | 2ac55d4 | 2010-02-03 19:33:23 +0000 | [diff] [blame] | 8742 | 0, 0, &cached_state, GFP_NOFS); |
Josef Bacik | fbf1908 | 2009-10-01 17:10:23 -0400 | [diff] [blame] | 8743 | |
Josef Bacik | 2ac55d4 | 2010-02-03 19:33:23 +0000 | [diff] [blame] | 8744 | ret = btrfs_set_extent_delalloc(inode, page_start, page_end, |
| 8745 | &cached_state); |
Josef Bacik | 9ed74f2 | 2009-09-11 16:12:44 -0400 | [diff] [blame] | 8746 | if (ret) { |
Josef Bacik | 2ac55d4 | 2010-02-03 19:33:23 +0000 | [diff] [blame] | 8747 | unlock_extent_cached(io_tree, page_start, page_end, |
| 8748 | &cached_state, GFP_NOFS); |
Josef Bacik | 9ed74f2 | 2009-09-11 16:12:44 -0400 | [diff] [blame] | 8749 | ret = VM_FAULT_SIGBUS; |
| 8750 | goto out_unlock; |
| 8751 | } |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 8752 | ret = 0; |
Chris Mason | 9ebefb18 | 2007-06-15 13:50:00 -0400 | [diff] [blame] | 8753 | |
| 8754 | /* page is wholly or partially inside EOF */ |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 8755 | if (page_start + PAGE_CACHE_SIZE > size) |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 8756 | zero_start = size & ~PAGE_CACHE_MASK; |
Chris Mason | 9ebefb18 | 2007-06-15 13:50:00 -0400 | [diff] [blame] | 8757 | else |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 8758 | zero_start = PAGE_CACHE_SIZE; |
Chris Mason | 9ebefb18 | 2007-06-15 13:50:00 -0400 | [diff] [blame] | 8759 | |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 8760 | if (zero_start != PAGE_CACHE_SIZE) { |
| 8761 | kaddr = kmap(page); |
| 8762 | memset(kaddr + zero_start, 0, PAGE_CACHE_SIZE - zero_start); |
| 8763 | flush_dcache_page(page); |
| 8764 | kunmap(page); |
| 8765 | } |
Chris Mason | 247e743 | 2008-07-17 12:53:51 -0400 | [diff] [blame] | 8766 | ClearPageChecked(page); |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 8767 | set_page_dirty(page); |
Chris Mason | 50a9b21 | 2009-09-11 12:33:12 -0400 | [diff] [blame] | 8768 | SetPageUptodate(page); |
Chris Mason | 5a3f23d | 2009-03-31 13:27:11 -0400 | [diff] [blame] | 8769 | |
Chris Mason | 257c62e | 2009-10-13 13:21:08 -0400 | [diff] [blame] | 8770 | BTRFS_I(inode)->last_trans = root->fs_info->generation; |
| 8771 | BTRFS_I(inode)->last_sub_trans = BTRFS_I(inode)->root->log_transid; |
Liu Bo | 46d8bc3 | 2012-08-29 01:07:55 -0600 | [diff] [blame] | 8772 | 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] | 8773 | |
Josef Bacik | 2ac55d4 | 2010-02-03 19:33:23 +0000 | [diff] [blame] | 8774 | 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] | 8775 | |
| 8776 | out_unlock: |
Jan Kara | b2b5ef5 | 2012-06-12 16:20:45 +0200 | [diff] [blame] | 8777 | if (!ret) { |
| 8778 | sb_end_pagefault(inode->i_sb); |
Chris Mason | 50a9b21 | 2009-09-11 12:33:12 -0400 | [diff] [blame] | 8779 | return VM_FAULT_LOCKED; |
Jan Kara | b2b5ef5 | 2012-06-12 16:20:45 +0200 | [diff] [blame] | 8780 | } |
Chris Mason | 9ebefb18 | 2007-06-15 13:50:00 -0400 | [diff] [blame] | 8781 | unlock_page(page); |
Chris Mason | 1832a6d | 2007-12-21 16:27:21 -0500 | [diff] [blame] | 8782 | out: |
Qu Wenruo | df48063 | 2015-09-08 17:25:54 +0800 | [diff] [blame^] | 8783 | __btrfs_delalloc_release_space(inode, page_start, PAGE_CACHE_SIZE); |
Chris Mason | 9998eb7 | 2012-01-25 13:47:40 -0500 | [diff] [blame] | 8784 | out_noreserve: |
Jan Kara | b2b5ef5 | 2012-06-12 16:20:45 +0200 | [diff] [blame] | 8785 | sb_end_pagefault(inode->i_sb); |
Chris Mason | 9ebefb18 | 2007-06-15 13:50:00 -0400 | [diff] [blame] | 8786 | return ret; |
| 8787 | } |
| 8788 | |
Josef Bacik | a41ad39 | 2011-01-31 15:30:16 -0500 | [diff] [blame] | 8789 | static int btrfs_truncate(struct inode *inode) |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 8790 | { |
| 8791 | struct btrfs_root *root = BTRFS_I(inode)->root; |
Josef Bacik | fcb80c2 | 2011-05-03 10:40:22 -0400 | [diff] [blame] | 8792 | struct btrfs_block_rsv *rsv; |
Josef Bacik | a71754f | 2013-06-17 17:14:39 -0400 | [diff] [blame] | 8793 | int ret = 0; |
Josef Bacik | 3893e33 | 2011-01-31 16:03:11 -0500 | [diff] [blame] | 8794 | int err = 0; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 8795 | struct btrfs_trans_handle *trans; |
Chris Mason | dbe674a | 2008-07-17 12:54:05 -0400 | [diff] [blame] | 8796 | u64 mask = root->sectorsize - 1; |
Josef Bacik | 0712718 | 2011-08-19 10:29:59 -0400 | [diff] [blame] | 8797 | u64 min_size = btrfs_calc_trunc_metadata_size(root, 1); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 8798 | |
Josef Bacik | 0ef8b72 | 2013-10-25 16:13:35 -0400 | [diff] [blame] | 8799 | ret = btrfs_wait_ordered_range(inode, inode->i_size & (~mask), |
| 8800 | (u64)-1); |
| 8801 | if (ret) |
| 8802 | return ret; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 8803 | |
Josef Bacik | fcb80c2 | 2011-05-03 10:40:22 -0400 | [diff] [blame] | 8804 | /* |
| 8805 | * Yes ladies and gentelment, this is indeed ugly. The fact is we have |
| 8806 | * 3 things going on here |
| 8807 | * |
| 8808 | * 1) We need to reserve space for our orphan item and the space to |
| 8809 | * delete our orphan item. Lord knows we don't want to have a dangling |
| 8810 | * orphan item because we didn't reserve space to remove it. |
| 8811 | * |
| 8812 | * 2) We need to reserve space to update our inode. |
| 8813 | * |
| 8814 | * 3) We need to have something to cache all the space that is going to |
| 8815 | * be free'd up by the truncate operation, but also have some slack |
| 8816 | * space reserved in case it uses space during the truncate (thank you |
| 8817 | * very much snapshotting). |
| 8818 | * |
| 8819 | * And we need these to all be seperate. The fact is we can use alot of |
| 8820 | * space doing the truncate, and we have no earthly idea how much space |
| 8821 | * we will use, so we need the truncate reservation to be seperate so it |
| 8822 | * doesn't end up using space reserved for updating the inode or |
| 8823 | * removing the orphan item. We also need to be able to stop the |
| 8824 | * transaction and start a new one, which means we need to be able to |
| 8825 | * update the inode several times, and we have no idea of knowing how |
| 8826 | * many times that will be, so we can't just reserve 1 item for the |
| 8827 | * entirety of the opration, so that has to be done seperately as well. |
| 8828 | * Then there is the orphan item, which does indeed need to be held on |
| 8829 | * to for the whole operation, and we need nobody to touch this reserved |
| 8830 | * space except the orphan code. |
| 8831 | * |
| 8832 | * So that leaves us with |
| 8833 | * |
| 8834 | * 1) root->orphan_block_rsv - for the orphan deletion. |
| 8835 | * 2) rsv - for the truncate reservation, which we will steal from the |
| 8836 | * transaction reservation. |
| 8837 | * 3) fs_info->trans_block_rsv - this will have 1 items worth left for |
| 8838 | * updating the inode. |
| 8839 | */ |
Miao Xie | 66d8f3d | 2012-09-06 04:02:28 -0600 | [diff] [blame] | 8840 | rsv = btrfs_alloc_block_rsv(root, BTRFS_BLOCK_RSV_TEMP); |
Josef Bacik | fcb80c2 | 2011-05-03 10:40:22 -0400 | [diff] [blame] | 8841 | if (!rsv) |
| 8842 | return -ENOMEM; |
Josef Bacik | 4a33854 | 2011-08-29 11:01:31 -0400 | [diff] [blame] | 8843 | rsv->size = min_size; |
Josef Bacik | ca7e70f | 2012-08-27 17:48:15 -0400 | [diff] [blame] | 8844 | rsv->failfast = 1; |
Josef Bacik | f0cd846 | 2011-03-04 14:37:08 -0500 | [diff] [blame] | 8845 | |
Josef Bacik | 907cbce | 2011-08-08 13:46:15 -0400 | [diff] [blame] | 8846 | /* |
Josef Bacik | 0712718 | 2011-08-19 10:29:59 -0400 | [diff] [blame] | 8847 | * 1 for the truncate slack space |
Josef Bacik | 907cbce | 2011-08-08 13:46:15 -0400 | [diff] [blame] | 8848 | * 1 for updating the inode. |
| 8849 | */ |
Josef Bacik | f3fe820 | 2013-01-07 17:03:21 -0500 | [diff] [blame] | 8850 | trans = btrfs_start_transaction(root, 2); |
Josef Bacik | fcb80c2 | 2011-05-03 10:40:22 -0400 | [diff] [blame] | 8851 | if (IS_ERR(trans)) { |
| 8852 | err = PTR_ERR(trans); |
| 8853 | goto out; |
| 8854 | } |
Josef Bacik | f0cd846 | 2011-03-04 14:37:08 -0500 | [diff] [blame] | 8855 | |
Josef Bacik | 907cbce | 2011-08-08 13:46:15 -0400 | [diff] [blame] | 8856 | /* Migrate the slack space for the truncate to our reserve */ |
| 8857 | ret = btrfs_block_rsv_migrate(&root->fs_info->trans_block_rsv, rsv, |
| 8858 | min_size); |
Josef Bacik | fcb80c2 | 2011-05-03 10:40:22 -0400 | [diff] [blame] | 8859 | BUG_ON(ret); |
Josef Bacik | f0cd846 | 2011-03-04 14:37:08 -0500 | [diff] [blame] | 8860 | |
Chris Mason | 5a3f23d | 2009-03-31 13:27:11 -0400 | [diff] [blame] | 8861 | /* |
Josef Bacik | 5dc562c | 2012-08-17 13:14:17 -0400 | [diff] [blame] | 8862 | * So if we truncate and then write and fsync we normally would just |
| 8863 | * write the extents that changed, which is a problem if we need to |
| 8864 | * first truncate that entire inode. So set this flag so we write out |
| 8865 | * all of the extents in the inode to the sync log so we're completely |
| 8866 | * safe. |
| 8867 | */ |
| 8868 | set_bit(BTRFS_INODE_NEEDS_FULL_SYNC, &BTRFS_I(inode)->runtime_flags); |
Josef Bacik | ca7e70f | 2012-08-27 17:48:15 -0400 | [diff] [blame] | 8869 | trans->block_rsv = rsv; |
Josef Bacik | 5dc562c | 2012-08-17 13:14:17 -0400 | [diff] [blame] | 8870 | |
Yan, Zheng | 8082510 | 2009-11-12 09:35:36 +0000 | [diff] [blame] | 8871 | while (1) { |
| 8872 | ret = btrfs_truncate_inode_items(trans, root, inode, |
| 8873 | inode->i_size, |
| 8874 | BTRFS_EXTENT_DATA_KEY); |
Chris Mason | 28ed134 | 2014-12-17 09:41:04 -0800 | [diff] [blame] | 8875 | if (ret != -ENOSPC && ret != -EAGAIN) { |
Josef Bacik | 3893e33 | 2011-01-31 16:03:11 -0500 | [diff] [blame] | 8876 | err = ret; |
Yan, Zheng | 8082510 | 2009-11-12 09:35:36 +0000 | [diff] [blame] | 8877 | break; |
Josef Bacik | 3893e33 | 2011-01-31 16:03:11 -0500 | [diff] [blame] | 8878 | } |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 8879 | |
Josef Bacik | fcb80c2 | 2011-05-03 10:40:22 -0400 | [diff] [blame] | 8880 | trans->block_rsv = &root->fs_info->trans_block_rsv; |
Yan, Zheng | 8082510 | 2009-11-12 09:35:36 +0000 | [diff] [blame] | 8881 | ret = btrfs_update_inode(trans, root, inode); |
Josef Bacik | 3893e33 | 2011-01-31 16:03:11 -0500 | [diff] [blame] | 8882 | if (ret) { |
| 8883 | err = ret; |
| 8884 | break; |
| 8885 | } |
Josef Bacik | ca7e70f | 2012-08-27 17:48:15 -0400 | [diff] [blame] | 8886 | |
Yan, Zheng | 8082510 | 2009-11-12 09:35:36 +0000 | [diff] [blame] | 8887 | btrfs_end_transaction(trans, root); |
Liu Bo | b53d3f5 | 2012-11-14 14:34:34 +0000 | [diff] [blame] | 8888 | btrfs_btree_balance_dirty(root); |
Josef Bacik | ca7e70f | 2012-08-27 17:48:15 -0400 | [diff] [blame] | 8889 | |
| 8890 | trans = btrfs_start_transaction(root, 2); |
| 8891 | if (IS_ERR(trans)) { |
| 8892 | ret = err = PTR_ERR(trans); |
| 8893 | trans = NULL; |
| 8894 | break; |
| 8895 | } |
| 8896 | |
| 8897 | ret = btrfs_block_rsv_migrate(&root->fs_info->trans_block_rsv, |
| 8898 | rsv, min_size); |
| 8899 | BUG_ON(ret); /* shouldn't happen */ |
| 8900 | trans->block_rsv = rsv; |
Yan, Zheng | 8082510 | 2009-11-12 09:35:36 +0000 | [diff] [blame] | 8901 | } |
| 8902 | |
| 8903 | if (ret == 0 && inode->i_nlink > 0) { |
Josef Bacik | fcb80c2 | 2011-05-03 10:40:22 -0400 | [diff] [blame] | 8904 | trans->block_rsv = root->orphan_block_rsv; |
Yan, Zheng | 8082510 | 2009-11-12 09:35:36 +0000 | [diff] [blame] | 8905 | ret = btrfs_orphan_del(trans, inode); |
Josef Bacik | 3893e33 | 2011-01-31 16:03:11 -0500 | [diff] [blame] | 8906 | if (ret) |
| 8907 | err = ret; |
Yan, Zheng | 8082510 | 2009-11-12 09:35:36 +0000 | [diff] [blame] | 8908 | } |
| 8909 | |
Chris Mason | 917c16b | 2011-11-08 14:49:59 -0500 | [diff] [blame] | 8910 | if (trans) { |
| 8911 | trans->block_rsv = &root->fs_info->trans_block_rsv; |
| 8912 | ret = btrfs_update_inode(trans, root, inode); |
| 8913 | if (ret && !err) |
| 8914 | err = ret; |
Josef Bacik | 7b12876 | 2008-07-24 12:17:14 -0400 | [diff] [blame] | 8915 | |
Josef Bacik | 7ad85bb | 2012-01-12 19:10:12 -0500 | [diff] [blame] | 8916 | ret = btrfs_end_transaction(trans, root); |
Liu Bo | b53d3f5 | 2012-11-14 14:34:34 +0000 | [diff] [blame] | 8917 | btrfs_btree_balance_dirty(root); |
Chris Mason | 917c16b | 2011-11-08 14:49:59 -0500 | [diff] [blame] | 8918 | } |
Josef Bacik | fcb80c2 | 2011-05-03 10:40:22 -0400 | [diff] [blame] | 8919 | |
| 8920 | out: |
| 8921 | btrfs_free_block_rsv(root, rsv); |
| 8922 | |
Josef Bacik | 3893e33 | 2011-01-31 16:03:11 -0500 | [diff] [blame] | 8923 | if (ret && !err) |
| 8924 | err = ret; |
Josef Bacik | a41ad39 | 2011-01-31 15:30:16 -0500 | [diff] [blame] | 8925 | |
Josef Bacik | 3893e33 | 2011-01-31 16:03:11 -0500 | [diff] [blame] | 8926 | return err; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 8927 | } |
| 8928 | |
Sven Wegener | 3b96362 | 2008-06-09 21:57:42 -0400 | [diff] [blame] | 8929 | /* |
Chris Mason | d352ac6 | 2008-09-29 15:18:18 -0400 | [diff] [blame] | 8930 | * create a new subvolume directory/inode (helper for the ioctl). |
| 8931 | */ |
Yan Zheng | d2fb343 | 2008-12-11 16:30:39 -0500 | [diff] [blame] | 8932 | int btrfs_create_subvol_root(struct btrfs_trans_handle *trans, |
Filipe David Borba Manana | 6354192 | 2014-01-07 11:47:46 +0000 | [diff] [blame] | 8933 | struct btrfs_root *new_root, |
| 8934 | struct btrfs_root *parent_root, |
| 8935 | u64 new_dirid) |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 8936 | { |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 8937 | struct inode *inode; |
Yan, Zheng | 76dda93 | 2009-09-21 16:00:26 -0400 | [diff] [blame] | 8938 | int err; |
Chris Mason | 00e4e6b | 2008-08-05 11:18:09 -0400 | [diff] [blame] | 8939 | u64 index = 0; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 8940 | |
Florian Albrechtskirchinger | 12fc9d0 | 2012-02-10 22:15:54 +0100 | [diff] [blame] | 8941 | inode = btrfs_new_inode(trans, new_root, NULL, "..", 2, |
| 8942 | new_dirid, new_dirid, |
| 8943 | S_IFDIR | (~current_umask() & S_IRWXUGO), |
| 8944 | &index); |
Chris Mason | 54aa1f4 | 2007-06-22 14:16:25 -0400 | [diff] [blame] | 8945 | if (IS_ERR(inode)) |
Christoph Hellwig | f46b5a6 | 2008-06-11 21:53:53 -0400 | [diff] [blame] | 8946 | return PTR_ERR(inode); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 8947 | inode->i_op = &btrfs_dir_inode_operations; |
| 8948 | inode->i_fop = &btrfs_dir_file_operations; |
| 8949 | |
Miklos Szeredi | bfe8684 | 2011-10-28 14:13:29 +0200 | [diff] [blame] | 8950 | set_nlink(inode, 1); |
Chris Mason | dbe674a | 2008-07-17 12:54:05 -0400 | [diff] [blame] | 8951 | btrfs_i_size_write(inode, 0); |
Chris Mason | b0d5d10 | 2014-09-08 13:08:51 -0700 | [diff] [blame] | 8952 | unlock_new_inode(inode); |
Sven Wegener | 3b96362 | 2008-06-09 21:57:42 -0400 | [diff] [blame] | 8953 | |
Filipe David Borba Manana | 6354192 | 2014-01-07 11:47:46 +0000 | [diff] [blame] | 8954 | err = btrfs_subvol_inherit_props(trans, new_root, parent_root); |
| 8955 | if (err) |
| 8956 | btrfs_err(new_root->fs_info, |
David Sterba | 351fd35 | 2014-05-15 16:48:20 +0200 | [diff] [blame] | 8957 | "error inheriting subvolume %llu properties: %d", |
Filipe David Borba Manana | 6354192 | 2014-01-07 11:47:46 +0000 | [diff] [blame] | 8958 | new_root->root_key.objectid, err); |
| 8959 | |
Yan, Zheng | 76dda93 | 2009-09-21 16:00:26 -0400 | [diff] [blame] | 8960 | err = btrfs_update_inode(trans, new_root, inode); |
Christoph Hellwig | cb8e709 | 2008-10-09 13:39:39 -0400 | [diff] [blame] | 8961 | |
Yan, Zheng | 76dda93 | 2009-09-21 16:00:26 -0400 | [diff] [blame] | 8962 | iput(inode); |
Mark Fasheh | ce59897 | 2011-07-26 11:32:23 -0700 | [diff] [blame] | 8963 | return err; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 8964 | } |
| 8965 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 8966 | struct inode *btrfs_alloc_inode(struct super_block *sb) |
| 8967 | { |
| 8968 | struct btrfs_inode *ei; |
Yan, Zheng | 2ead6ae | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 8969 | struct inode *inode; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 8970 | |
| 8971 | ei = kmem_cache_alloc(btrfs_inode_cachep, GFP_NOFS); |
| 8972 | if (!ei) |
| 8973 | return NULL; |
Yan, Zheng | 2ead6ae | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 8974 | |
| 8975 | ei->root = NULL; |
Yan, Zheng | 2ead6ae | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 8976 | ei->generation = 0; |
Josef Bacik | 15ee9bc | 2007-08-10 16:22:09 -0400 | [diff] [blame] | 8977 | ei->last_trans = 0; |
Chris Mason | 257c62e | 2009-10-13 13:21:08 -0400 | [diff] [blame] | 8978 | ei->last_sub_trans = 0; |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 8979 | ei->logged_trans = 0; |
Yan, Zheng | 2ead6ae | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 8980 | ei->delalloc_bytes = 0; |
Wang Shilong | 47059d9 | 2014-07-03 18:22:07 +0800 | [diff] [blame] | 8981 | ei->defrag_bytes = 0; |
Yan, Zheng | 2ead6ae | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 8982 | ei->disk_i_size = 0; |
| 8983 | ei->flags = 0; |
Josef Bacik | 7709cde | 2011-08-04 10:25:02 -0400 | [diff] [blame] | 8984 | ei->csum_bytes = 0; |
Yan, Zheng | 2ead6ae | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 8985 | ei->index_cnt = (u64)-1; |
Miao Xie | 67de117 | 2013-12-26 13:07:06 +0800 | [diff] [blame] | 8986 | ei->dir_index = 0; |
Yan, Zheng | 2ead6ae | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 8987 | ei->last_unlink_trans = 0; |
Liu Bo | 46d8bc3 | 2012-08-29 01:07:55 -0600 | [diff] [blame] | 8988 | ei->last_log_commit = 0; |
Yan, Zheng | 2ead6ae | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 8989 | |
Josef Bacik | 9e0baf6 | 2011-07-15 15:16:44 +0000 | [diff] [blame] | 8990 | spin_lock_init(&ei->lock); |
| 8991 | ei->outstanding_extents = 0; |
| 8992 | ei->reserved_extents = 0; |
Yan, Zheng | 2ead6ae | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 8993 | |
Josef Bacik | 72ac3c0 | 2012-05-23 14:13:11 -0400 | [diff] [blame] | 8994 | ei->runtime_flags = 0; |
Li Zefan | 261507a0 | 2010-12-17 14:21:50 +0800 | [diff] [blame] | 8995 | ei->force_compress = BTRFS_COMPRESS_NONE; |
Yan, Zheng | 2ead6ae | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 8996 | |
Miao Xie | 16cdcec | 2011-04-22 18:12:22 +0800 | [diff] [blame] | 8997 | ei->delayed_node = NULL; |
| 8998 | |
chandan r | 9cc97d6 | 2012-07-04 12:48:07 +0530 | [diff] [blame] | 8999 | ei->i_otime.tv_sec = 0; |
| 9000 | ei->i_otime.tv_nsec = 0; |
| 9001 | |
Yan, Zheng | 2ead6ae | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 9002 | inode = &ei->vfs_inode; |
David Sterba | a8067e0 | 2011-04-21 00:34:43 +0200 | [diff] [blame] | 9003 | extent_map_tree_init(&ei->extent_tree); |
David Sterba | f993c88 | 2011-04-20 23:35:57 +0200 | [diff] [blame] | 9004 | extent_io_tree_init(&ei->io_tree, &inode->i_data); |
| 9005 | extent_io_tree_init(&ei->io_failure_tree, &inode->i_data); |
Josef Bacik | 0b32f4b | 2012-03-13 09:38:00 -0400 | [diff] [blame] | 9006 | ei->io_tree.track_uptodate = 1; |
| 9007 | ei->io_failure_tree.track_uptodate = 1; |
Josef Bacik | b812ce2 | 2012-11-16 13:56:32 -0500 | [diff] [blame] | 9008 | atomic_set(&ei->sync_writers, 0); |
Yan, Zheng | 2ead6ae | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 9009 | mutex_init(&ei->log_mutex); |
Josef Bacik | f248679 | 2012-01-13 12:09:22 -0500 | [diff] [blame] | 9010 | mutex_init(&ei->delalloc_mutex); |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 9011 | btrfs_ordered_inode_tree_init(&ei->ordered_tree); |
Yan, Zheng | 2ead6ae | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 9012 | INIT_LIST_HEAD(&ei->delalloc_inodes); |
Yan, Zheng | 2ead6ae | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 9013 | RB_CLEAR_NODE(&ei->rb_node); |
| 9014 | |
| 9015 | return inode; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 9016 | } |
| 9017 | |
Josef Bacik | aaedb55 | 2013-10-11 14:44:09 -0400 | [diff] [blame] | 9018 | #ifdef CONFIG_BTRFS_FS_RUN_SANITY_TESTS |
| 9019 | void btrfs_test_destroy_inode(struct inode *inode) |
| 9020 | { |
| 9021 | btrfs_drop_extent_cache(inode, 0, (u64)-1, 0); |
| 9022 | kmem_cache_free(btrfs_inode_cachep, BTRFS_I(inode)); |
| 9023 | } |
| 9024 | #endif |
| 9025 | |
Nick Piggin | fa0d7e3d | 2011-01-07 17:49:49 +1100 | [diff] [blame] | 9026 | static void btrfs_i_callback(struct rcu_head *head) |
| 9027 | { |
| 9028 | struct inode *inode = container_of(head, struct inode, i_rcu); |
Nick Piggin | fa0d7e3d | 2011-01-07 17:49:49 +1100 | [diff] [blame] | 9029 | kmem_cache_free(btrfs_inode_cachep, BTRFS_I(inode)); |
| 9030 | } |
| 9031 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 9032 | void btrfs_destroy_inode(struct inode *inode) |
| 9033 | { |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 9034 | struct btrfs_ordered_extent *ordered; |
Chris Mason | 5a3f23d | 2009-03-31 13:27:11 -0400 | [diff] [blame] | 9035 | struct btrfs_root *root = BTRFS_I(inode)->root; |
| 9036 | |
Al Viro | b3d9b7a | 2012-06-09 13:51:19 -0400 | [diff] [blame] | 9037 | WARN_ON(!hlist_empty(&inode->i_dentry)); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 9038 | WARN_ON(inode->i_data.nrpages); |
Josef Bacik | 9e0baf6 | 2011-07-15 15:16:44 +0000 | [diff] [blame] | 9039 | WARN_ON(BTRFS_I(inode)->outstanding_extents); |
| 9040 | WARN_ON(BTRFS_I(inode)->reserved_extents); |
Josef Bacik | 7709cde | 2011-08-04 10:25:02 -0400 | [diff] [blame] | 9041 | WARN_ON(BTRFS_I(inode)->delalloc_bytes); |
| 9042 | WARN_ON(BTRFS_I(inode)->csum_bytes); |
Wang Shilong | 47059d9 | 2014-07-03 18:22:07 +0800 | [diff] [blame] | 9043 | WARN_ON(BTRFS_I(inode)->defrag_bytes); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 9044 | |
Chris Mason | 5a3f23d | 2009-03-31 13:27:11 -0400 | [diff] [blame] | 9045 | /* |
Josef Bacik | a6dbd42 | 2009-11-11 15:53:34 -0500 | [diff] [blame] | 9046 | * This can happen where we create an inode, but somebody else also |
| 9047 | * created the same inode and we need to destroy the one we already |
| 9048 | * created. |
| 9049 | */ |
| 9050 | if (!root) |
| 9051 | goto free; |
| 9052 | |
Josef Bacik | 8a35d95 | 2012-05-23 14:26:42 -0400 | [diff] [blame] | 9053 | if (test_bit(BTRFS_INODE_HAS_ORPHAN_ITEM, |
| 9054 | &BTRFS_I(inode)->runtime_flags)) { |
Simon Kirby | c2cf52e | 2013-03-19 22:41:23 +0000 | [diff] [blame] | 9055 | btrfs_info(root->fs_info, "inode %llu still on the orphan list", |
Geert Uytterhoeven | c1c9ff7 | 2013-08-20 13:20:07 +0200 | [diff] [blame] | 9056 | btrfs_ino(inode)); |
Josef Bacik | 8a35d95 | 2012-05-23 14:26:42 -0400 | [diff] [blame] | 9057 | atomic_dec(&root->orphan_inodes); |
Josef Bacik | 7b12876 | 2008-07-24 12:17:14 -0400 | [diff] [blame] | 9058 | } |
Josef Bacik | 7b12876 | 2008-07-24 12:17:14 -0400 | [diff] [blame] | 9059 | |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 9060 | while (1) { |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 9061 | ordered = btrfs_lookup_first_ordered_extent(inode, (u64)-1); |
| 9062 | if (!ordered) |
| 9063 | break; |
| 9064 | else { |
Simon Kirby | c2cf52e | 2013-03-19 22:41:23 +0000 | [diff] [blame] | 9065 | btrfs_err(root->fs_info, "found ordered extent %llu %llu on inode cleanup", |
Geert Uytterhoeven | c1c9ff7 | 2013-08-20 13:20:07 +0200 | [diff] [blame] | 9066 | ordered->file_offset, ordered->len); |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 9067 | btrfs_remove_ordered_extent(inode, ordered); |
| 9068 | btrfs_put_ordered_extent(ordered); |
| 9069 | btrfs_put_ordered_extent(ordered); |
| 9070 | } |
| 9071 | } |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 9072 | inode_tree_del(inode); |
Zheng Yan | 5b21f2e | 2008-09-26 10:05:38 -0400 | [diff] [blame] | 9073 | btrfs_drop_extent_cache(inode, 0, (u64)-1, 0); |
Josef Bacik | a6dbd42 | 2009-11-11 15:53:34 -0500 | [diff] [blame] | 9074 | free: |
Nick Piggin | fa0d7e3d | 2011-01-07 17:49:49 +1100 | [diff] [blame] | 9075 | call_rcu(&inode->i_rcu, btrfs_i_callback); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 9076 | } |
| 9077 | |
Al Viro | 45321ac | 2010-06-07 13:43:19 -0400 | [diff] [blame] | 9078 | int btrfs_drop_inode(struct inode *inode) |
Yan, Zheng | 76dda93 | 2009-09-21 16:00:26 -0400 | [diff] [blame] | 9079 | { |
| 9080 | struct btrfs_root *root = BTRFS_I(inode)->root; |
Al Viro | 45321ac | 2010-06-07 13:43:19 -0400 | [diff] [blame] | 9081 | |
Naohiro Aota | 6379ef9 | 2013-06-06 09:56:34 +0000 | [diff] [blame] | 9082 | if (root == NULL) |
| 9083 | return 1; |
| 9084 | |
Liu Bo | fa6ac87 | 2013-02-20 14:10:23 +0000 | [diff] [blame] | 9085 | /* the snap/subvol tree is on deleting */ |
Stefan Behrens | 69e9c6c | 2013-09-05 16:58:43 +0200 | [diff] [blame] | 9086 | if (btrfs_root_refs(&root->root_item) == 0) |
Al Viro | 45321ac | 2010-06-07 13:43:19 -0400 | [diff] [blame] | 9087 | return 1; |
Yan, Zheng | 76dda93 | 2009-09-21 16:00:26 -0400 | [diff] [blame] | 9088 | else |
Al Viro | 45321ac | 2010-06-07 13:43:19 -0400 | [diff] [blame] | 9089 | return generic_drop_inode(inode); |
Yan, Zheng | 76dda93 | 2009-09-21 16:00:26 -0400 | [diff] [blame] | 9090 | } |
| 9091 | |
Sven Wegener | 0ee0fda | 2008-07-30 16:54:26 -0400 | [diff] [blame] | 9092 | static void init_once(void *foo) |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 9093 | { |
| 9094 | struct btrfs_inode *ei = (struct btrfs_inode *) foo; |
| 9095 | |
| 9096 | inode_init_once(&ei->vfs_inode); |
| 9097 | } |
| 9098 | |
| 9099 | void btrfs_destroy_cachep(void) |
| 9100 | { |
Kirill A. Shutemov | 8c0a853 | 2012-09-26 11:33:07 +1000 | [diff] [blame] | 9101 | /* |
| 9102 | * Make sure all delayed rcu free inodes are flushed before we |
| 9103 | * destroy cache. |
| 9104 | */ |
| 9105 | rcu_barrier(); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 9106 | if (btrfs_inode_cachep) |
| 9107 | kmem_cache_destroy(btrfs_inode_cachep); |
| 9108 | if (btrfs_trans_handle_cachep) |
| 9109 | kmem_cache_destroy(btrfs_trans_handle_cachep); |
| 9110 | if (btrfs_transaction_cachep) |
| 9111 | kmem_cache_destroy(btrfs_transaction_cachep); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 9112 | if (btrfs_path_cachep) |
| 9113 | kmem_cache_destroy(btrfs_path_cachep); |
Josef Bacik | dc89e98 | 2011-01-28 17:05:48 -0500 | [diff] [blame] | 9114 | if (btrfs_free_space_cachep) |
| 9115 | kmem_cache_destroy(btrfs_free_space_cachep); |
Miao Xie | 8ccf6f19 | 2012-10-25 09:28:04 +0000 | [diff] [blame] | 9116 | if (btrfs_delalloc_work_cachep) |
| 9117 | kmem_cache_destroy(btrfs_delalloc_work_cachep); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 9118 | } |
| 9119 | |
| 9120 | int btrfs_init_cachep(void) |
| 9121 | { |
David Sterba | 837e197 | 2012-09-07 03:00:48 -0600 | [diff] [blame] | 9122 | btrfs_inode_cachep = kmem_cache_create("btrfs_inode", |
Christoph Hellwig | 9601e3f | 2009-04-13 15:33:09 +0200 | [diff] [blame] | 9123 | sizeof(struct btrfs_inode), 0, |
| 9124 | SLAB_RECLAIM_ACCOUNT | SLAB_MEM_SPREAD, init_once); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 9125 | if (!btrfs_inode_cachep) |
| 9126 | goto fail; |
Christoph Hellwig | 9601e3f | 2009-04-13 15:33:09 +0200 | [diff] [blame] | 9127 | |
David Sterba | 837e197 | 2012-09-07 03:00:48 -0600 | [diff] [blame] | 9128 | btrfs_trans_handle_cachep = kmem_cache_create("btrfs_trans_handle", |
Christoph Hellwig | 9601e3f | 2009-04-13 15:33:09 +0200 | [diff] [blame] | 9129 | sizeof(struct btrfs_trans_handle), 0, |
| 9130 | SLAB_RECLAIM_ACCOUNT | SLAB_MEM_SPREAD, NULL); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 9131 | if (!btrfs_trans_handle_cachep) |
| 9132 | goto fail; |
Christoph Hellwig | 9601e3f | 2009-04-13 15:33:09 +0200 | [diff] [blame] | 9133 | |
David Sterba | 837e197 | 2012-09-07 03:00:48 -0600 | [diff] [blame] | 9134 | btrfs_transaction_cachep = kmem_cache_create("btrfs_transaction", |
Christoph Hellwig | 9601e3f | 2009-04-13 15:33:09 +0200 | [diff] [blame] | 9135 | sizeof(struct btrfs_transaction), 0, |
| 9136 | SLAB_RECLAIM_ACCOUNT | SLAB_MEM_SPREAD, NULL); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 9137 | if (!btrfs_transaction_cachep) |
| 9138 | goto fail; |
Christoph Hellwig | 9601e3f | 2009-04-13 15:33:09 +0200 | [diff] [blame] | 9139 | |
David Sterba | 837e197 | 2012-09-07 03:00:48 -0600 | [diff] [blame] | 9140 | btrfs_path_cachep = kmem_cache_create("btrfs_path", |
Christoph Hellwig | 9601e3f | 2009-04-13 15:33:09 +0200 | [diff] [blame] | 9141 | sizeof(struct btrfs_path), 0, |
| 9142 | SLAB_RECLAIM_ACCOUNT | SLAB_MEM_SPREAD, NULL); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 9143 | if (!btrfs_path_cachep) |
| 9144 | goto fail; |
Christoph Hellwig | 9601e3f | 2009-04-13 15:33:09 +0200 | [diff] [blame] | 9145 | |
David Sterba | 837e197 | 2012-09-07 03:00:48 -0600 | [diff] [blame] | 9146 | btrfs_free_space_cachep = kmem_cache_create("btrfs_free_space", |
Josef Bacik | dc89e98 | 2011-01-28 17:05:48 -0500 | [diff] [blame] | 9147 | sizeof(struct btrfs_free_space), 0, |
| 9148 | SLAB_RECLAIM_ACCOUNT | SLAB_MEM_SPREAD, NULL); |
| 9149 | if (!btrfs_free_space_cachep) |
| 9150 | goto fail; |
| 9151 | |
Miao Xie | 8ccf6f19 | 2012-10-25 09:28:04 +0000 | [diff] [blame] | 9152 | btrfs_delalloc_work_cachep = kmem_cache_create("btrfs_delalloc_work", |
| 9153 | sizeof(struct btrfs_delalloc_work), 0, |
| 9154 | SLAB_RECLAIM_ACCOUNT | SLAB_MEM_SPREAD, |
| 9155 | NULL); |
| 9156 | if (!btrfs_delalloc_work_cachep) |
| 9157 | goto fail; |
| 9158 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 9159 | return 0; |
| 9160 | fail: |
| 9161 | btrfs_destroy_cachep(); |
| 9162 | return -ENOMEM; |
| 9163 | } |
| 9164 | |
| 9165 | static int btrfs_getattr(struct vfsmount *mnt, |
| 9166 | struct dentry *dentry, struct kstat *stat) |
| 9167 | { |
Miao Xie | df0af1a | 2013-01-29 10:11:59 +0000 | [diff] [blame] | 9168 | u64 delalloc_bytes; |
David Howells | 2b0143b | 2015-03-17 22:25:59 +0000 | [diff] [blame] | 9169 | struct inode *inode = d_inode(dentry); |
David Sterba | fadc0d8 | 2011-11-20 07:33:38 -0500 | [diff] [blame] | 9170 | u32 blocksize = inode->i_sb->s_blocksize; |
| 9171 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 9172 | generic_fillattr(inode, stat); |
Al Viro | 0ee5dc6 | 2011-07-07 15:44:25 -0400 | [diff] [blame] | 9173 | stat->dev = BTRFS_I(inode)->root->anon_dev; |
Chris Mason | d666746 | 2008-01-03 14:51:00 -0500 | [diff] [blame] | 9174 | stat->blksize = PAGE_CACHE_SIZE; |
Miao Xie | df0af1a | 2013-01-29 10:11:59 +0000 | [diff] [blame] | 9175 | |
| 9176 | spin_lock(&BTRFS_I(inode)->lock); |
| 9177 | delalloc_bytes = BTRFS_I(inode)->delalloc_bytes; |
| 9178 | spin_unlock(&BTRFS_I(inode)->lock); |
David Sterba | fadc0d8 | 2011-11-20 07:33:38 -0500 | [diff] [blame] | 9179 | stat->blocks = (ALIGN(inode_get_bytes(inode), blocksize) + |
Miao Xie | df0af1a | 2013-01-29 10:11:59 +0000 | [diff] [blame] | 9180 | ALIGN(delalloc_bytes, blocksize)) >> 9; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 9181 | return 0; |
| 9182 | } |
| 9183 | |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 9184 | static int btrfs_rename(struct inode *old_dir, struct dentry *old_dentry, |
| 9185 | struct inode *new_dir, struct dentry *new_dentry) |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 9186 | { |
| 9187 | struct btrfs_trans_handle *trans; |
| 9188 | struct btrfs_root *root = BTRFS_I(old_dir)->root; |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 9189 | struct btrfs_root *dest = BTRFS_I(new_dir)->root; |
David Howells | 2b0143b | 2015-03-17 22:25:59 +0000 | [diff] [blame] | 9190 | struct inode *new_inode = d_inode(new_dentry); |
| 9191 | struct inode *old_inode = d_inode(old_dentry); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 9192 | struct timespec ctime = CURRENT_TIME; |
Chris Mason | 00e4e6b | 2008-08-05 11:18:09 -0400 | [diff] [blame] | 9193 | u64 index = 0; |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 9194 | u64 root_objectid; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 9195 | int ret; |
Li Zefan | 33345d01 | 2011-04-20 10:31:50 +0800 | [diff] [blame] | 9196 | u64 old_ino = btrfs_ino(old_inode); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 9197 | |
Li Zefan | 33345d01 | 2011-04-20 10:31:50 +0800 | [diff] [blame] | 9198 | if (btrfs_ino(new_dir) == BTRFS_EMPTY_SUBVOL_DIR_OBJECTID) |
Yan, Zheng | f679a84 | 2009-09-24 09:17:31 -0400 | [diff] [blame] | 9199 | return -EPERM; |
| 9200 | |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 9201 | /* we only allow rename subvolume link between subvolumes */ |
Li Zefan | 33345d01 | 2011-04-20 10:31:50 +0800 | [diff] [blame] | 9202 | if (old_ino != BTRFS_FIRST_FREE_OBJECTID && root != dest) |
Chris Mason | 3394e16 | 2008-11-17 20:42:26 -0500 | [diff] [blame] | 9203 | return -EXDEV; |
| 9204 | |
Li Zefan | 33345d01 | 2011-04-20 10:31:50 +0800 | [diff] [blame] | 9205 | if (old_ino == BTRFS_EMPTY_SUBVOL_DIR_OBJECTID || |
| 9206 | (new_inode && btrfs_ino(new_inode) == BTRFS_FIRST_FREE_OBJECTID)) |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 9207 | return -ENOTEMPTY; |
| 9208 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 9209 | if (S_ISDIR(old_inode->i_mode) && new_inode && |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 9210 | new_inode->i_size > BTRFS_EMPTY_DIR_SIZE) |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 9211 | return -ENOTEMPTY; |
Chris Mason | 9c52057 | 2012-12-17 14:26:57 -0500 | [diff] [blame] | 9212 | |
| 9213 | |
| 9214 | /* check for collisions, even if the name isn't there */ |
Josef Bacik | 4871c15 | 2013-10-09 12:24:04 -0400 | [diff] [blame] | 9215 | ret = btrfs_check_dir_item_collision(dest, new_dir->i_ino, |
Chris Mason | 9c52057 | 2012-12-17 14:26:57 -0500 | [diff] [blame] | 9216 | new_dentry->d_name.name, |
| 9217 | new_dentry->d_name.len); |
| 9218 | |
| 9219 | if (ret) { |
| 9220 | if (ret == -EEXIST) { |
| 9221 | /* we shouldn't get |
| 9222 | * eexist without a new_inode */ |
Dulshani Gunawardhana | fae7f21 | 2013-10-31 10:30:08 +0530 | [diff] [blame] | 9223 | if (WARN_ON(!new_inode)) { |
Chris Mason | 9c52057 | 2012-12-17 14:26:57 -0500 | [diff] [blame] | 9224 | return ret; |
| 9225 | } |
| 9226 | } else { |
| 9227 | /* maybe -EOVERFLOW */ |
| 9228 | return ret; |
| 9229 | } |
| 9230 | } |
| 9231 | ret = 0; |
| 9232 | |
Chris Mason | 5a3f23d | 2009-03-31 13:27:11 -0400 | [diff] [blame] | 9233 | /* |
Chris Mason | 8d875f9 | 2014-08-12 10:47:42 -0700 | [diff] [blame] | 9234 | * we're using rename to replace one file with another. Start IO on it |
| 9235 | * 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] | 9236 | */ |
Chris Mason | 8d875f9 | 2014-08-12 10:47:42 -0700 | [diff] [blame] | 9237 | 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] | 9238 | filemap_flush(old_inode->i_mapping); |
| 9239 | |
Yan, Zheng | 76dda93 | 2009-09-21 16:00:26 -0400 | [diff] [blame] | 9240 | /* close the racy window with snapshot create/destroy ioctl */ |
Li Zefan | 33345d01 | 2011-04-20 10:31:50 +0800 | [diff] [blame] | 9241 | if (old_ino == BTRFS_FIRST_FREE_OBJECTID) |
Yan, Zheng | 76dda93 | 2009-09-21 16:00:26 -0400 | [diff] [blame] | 9242 | down_read(&root->fs_info->subvol_sem); |
Yan, Zheng | a22285a | 2010-05-16 10:48:46 -0400 | [diff] [blame] | 9243 | /* |
| 9244 | * We want to reserve the absolute worst case amount of items. So if |
| 9245 | * both inodes are subvols and we need to unlink them then that would |
| 9246 | * require 4 item modifications, but if they are both normal inodes it |
| 9247 | * would require 5 item modifications, so we'll assume their normal |
| 9248 | * inodes. So 5 * 2 is 10, plus 1 for the new link, so 11 total items |
| 9249 | * should cover the worst case number of items we'll modify. |
| 9250 | */ |
Josef Bacik | 6e137ed | 2013-03-26 15:26:55 -0400 | [diff] [blame] | 9251 | trans = btrfs_start_transaction(root, 11); |
Johann Lombardi | b44c59a | 2011-03-31 13:23:47 +0000 | [diff] [blame] | 9252 | if (IS_ERR(trans)) { |
| 9253 | ret = PTR_ERR(trans); |
| 9254 | goto out_notrans; |
| 9255 | } |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 9256 | |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 9257 | if (dest != root) |
| 9258 | btrfs_record_root_in_trans(trans, dest); |
| 9259 | |
Yan, Zheng | a571952 | 2009-09-24 09:17:31 -0400 | [diff] [blame] | 9260 | ret = btrfs_set_inode_index(new_dir, &index); |
| 9261 | if (ret) |
| 9262 | goto out_fail; |
Chris Mason | 5a3f23d | 2009-03-31 13:27:11 -0400 | [diff] [blame] | 9263 | |
Miao Xie | 67de117 | 2013-12-26 13:07:06 +0800 | [diff] [blame] | 9264 | BTRFS_I(old_inode)->dir_index = 0ULL; |
Li Zefan | 33345d01 | 2011-04-20 10:31:50 +0800 | [diff] [blame] | 9265 | if (unlikely(old_ino == BTRFS_FIRST_FREE_OBJECTID)) { |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 9266 | /* force full log commit if subvolume involved. */ |
Miao Xie | 995946d | 2014-04-02 19:51:06 +0800 | [diff] [blame] | 9267 | btrfs_set_log_full_commit(root->fs_info, trans); |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 9268 | } else { |
Yan, Zheng | a571952 | 2009-09-24 09:17:31 -0400 | [diff] [blame] | 9269 | ret = btrfs_insert_inode_ref(trans, dest, |
| 9270 | new_dentry->d_name.name, |
| 9271 | new_dentry->d_name.len, |
Li Zefan | 33345d01 | 2011-04-20 10:31:50 +0800 | [diff] [blame] | 9272 | old_ino, |
| 9273 | btrfs_ino(new_dir), index); |
Yan, Zheng | a571952 | 2009-09-24 09:17:31 -0400 | [diff] [blame] | 9274 | if (ret) |
| 9275 | goto out_fail; |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 9276 | /* |
| 9277 | * this is an ugly little race, but the rename is required |
| 9278 | * to make sure that if we crash, the inode is either at the |
| 9279 | * old name or the new one. pinning the log transaction lets |
| 9280 | * us make sure we don't allow a log commit to come in after |
| 9281 | * we unlink the name but before we add the new name back in. |
| 9282 | */ |
| 9283 | btrfs_pin_log_trans(root); |
| 9284 | } |
Chris Mason | 5a3f23d | 2009-03-31 13:27:11 -0400 | [diff] [blame] | 9285 | |
Josef Bacik | 0c4d2d9 | 2012-04-05 15:03:02 -0400 | [diff] [blame] | 9286 | inode_inc_iversion(old_dir); |
| 9287 | inode_inc_iversion(new_dir); |
| 9288 | inode_inc_iversion(old_inode); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 9289 | old_dir->i_ctime = old_dir->i_mtime = ctime; |
| 9290 | new_dir->i_ctime = new_dir->i_mtime = ctime; |
| 9291 | old_inode->i_ctime = ctime; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 9292 | |
Chris Mason | 12fcfd2 | 2009-03-24 10:24:20 -0400 | [diff] [blame] | 9293 | if (old_dentry->d_parent != new_dentry->d_parent) |
| 9294 | btrfs_record_unlink_dir(trans, old_dir, old_inode, 1); |
| 9295 | |
Li Zefan | 33345d01 | 2011-04-20 10:31:50 +0800 | [diff] [blame] | 9296 | if (unlikely(old_ino == BTRFS_FIRST_FREE_OBJECTID)) { |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 9297 | root_objectid = BTRFS_I(old_inode)->root->root_key.objectid; |
| 9298 | ret = btrfs_unlink_subvol(trans, root, old_dir, root_objectid, |
| 9299 | old_dentry->d_name.name, |
| 9300 | old_dentry->d_name.len); |
| 9301 | } else { |
Al Viro | 9298679 | 2011-03-04 17:14:37 +0000 | [diff] [blame] | 9302 | ret = __btrfs_unlink_inode(trans, root, old_dir, |
David Howells | 2b0143b | 2015-03-17 22:25:59 +0000 | [diff] [blame] | 9303 | d_inode(old_dentry), |
Al Viro | 9298679 | 2011-03-04 17:14:37 +0000 | [diff] [blame] | 9304 | old_dentry->d_name.name, |
| 9305 | old_dentry->d_name.len); |
| 9306 | if (!ret) |
| 9307 | ret = btrfs_update_inode(trans, root, old_inode); |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 9308 | } |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 9309 | if (ret) { |
| 9310 | btrfs_abort_transaction(trans, root, ret); |
| 9311 | goto out_fail; |
| 9312 | } |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 9313 | |
| 9314 | if (new_inode) { |
Josef Bacik | 0c4d2d9 | 2012-04-05 15:03:02 -0400 | [diff] [blame] | 9315 | inode_inc_iversion(new_inode); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 9316 | new_inode->i_ctime = CURRENT_TIME; |
Li Zefan | 33345d01 | 2011-04-20 10:31:50 +0800 | [diff] [blame] | 9317 | if (unlikely(btrfs_ino(new_inode) == |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 9318 | BTRFS_EMPTY_SUBVOL_DIR_OBJECTID)) { |
| 9319 | root_objectid = BTRFS_I(new_inode)->location.objectid; |
| 9320 | ret = btrfs_unlink_subvol(trans, dest, new_dir, |
| 9321 | root_objectid, |
| 9322 | new_dentry->d_name.name, |
| 9323 | new_dentry->d_name.len); |
| 9324 | BUG_ON(new_inode->i_nlink == 0); |
| 9325 | } else { |
| 9326 | ret = btrfs_unlink_inode(trans, dest, new_dir, |
David Howells | 2b0143b | 2015-03-17 22:25:59 +0000 | [diff] [blame] | 9327 | d_inode(new_dentry), |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 9328 | new_dentry->d_name.name, |
| 9329 | new_dentry->d_name.len); |
| 9330 | } |
Josef Bacik | 4ef31a4 | 2013-08-13 14:10:08 -0400 | [diff] [blame] | 9331 | if (!ret && new_inode->i_nlink == 0) |
David Howells | 2b0143b | 2015-03-17 22:25:59 +0000 | [diff] [blame] | 9332 | ret = btrfs_orphan_add(trans, d_inode(new_dentry)); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 9333 | if (ret) { |
| 9334 | btrfs_abort_transaction(trans, root, ret); |
| 9335 | goto out_fail; |
| 9336 | } |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 9337 | } |
Josef Bacik | aec7477 | 2008-07-24 12:12:38 -0400 | [diff] [blame] | 9338 | |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 9339 | ret = btrfs_add_link(trans, new_dir, old_inode, |
| 9340 | new_dentry->d_name.name, |
Yan, Zheng | a571952 | 2009-09-24 09:17:31 -0400 | [diff] [blame] | 9341 | new_dentry->d_name.len, 0, index); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 9342 | if (ret) { |
| 9343 | btrfs_abort_transaction(trans, root, ret); |
| 9344 | goto out_fail; |
| 9345 | } |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 9346 | |
Miao Xie | 67de117 | 2013-12-26 13:07:06 +0800 | [diff] [blame] | 9347 | if (old_inode->i_nlink == 1) |
| 9348 | BTRFS_I(old_inode)->dir_index = index; |
| 9349 | |
Li Zefan | 33345d01 | 2011-04-20 10:31:50 +0800 | [diff] [blame] | 9350 | if (old_ino != BTRFS_FIRST_FREE_OBJECTID) { |
Al Viro | 10d9f30 | 2011-07-16 23:09:10 -0400 | [diff] [blame] | 9351 | struct dentry *parent = new_dentry->d_parent; |
Josef Bacik | 6a91221 | 2010-11-20 09:48:00 +0000 | [diff] [blame] | 9352 | btrfs_log_new_name(trans, old_inode, old_dir, parent); |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 9353 | btrfs_end_log_trans(root); |
| 9354 | } |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 9355 | out_fail: |
Josef Bacik | 7ad85bb | 2012-01-12 19:10:12 -0500 | [diff] [blame] | 9356 | btrfs_end_transaction(trans, root); |
Johann Lombardi | b44c59a | 2011-03-31 13:23:47 +0000 | [diff] [blame] | 9357 | out_notrans: |
Li Zefan | 33345d01 | 2011-04-20 10:31:50 +0800 | [diff] [blame] | 9358 | if (old_ino == BTRFS_FIRST_FREE_OBJECTID) |
Yan, Zheng | 76dda93 | 2009-09-21 16:00:26 -0400 | [diff] [blame] | 9359 | up_read(&root->fs_info->subvol_sem); |
Josef Bacik | 9ed74f2 | 2009-09-11 16:12:44 -0400 | [diff] [blame] | 9360 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 9361 | return ret; |
| 9362 | } |
| 9363 | |
Miklos Szeredi | 80ace85 | 2014-07-23 15:15:32 +0200 | [diff] [blame] | 9364 | static int btrfs_rename2(struct inode *old_dir, struct dentry *old_dentry, |
| 9365 | struct inode *new_dir, struct dentry *new_dentry, |
| 9366 | unsigned int flags) |
| 9367 | { |
| 9368 | if (flags & ~RENAME_NOREPLACE) |
| 9369 | return -EINVAL; |
| 9370 | |
| 9371 | return btrfs_rename(old_dir, old_dentry, new_dir, new_dentry); |
| 9372 | } |
| 9373 | |
Miao Xie | 8ccf6f19 | 2012-10-25 09:28:04 +0000 | [diff] [blame] | 9374 | static void btrfs_run_delalloc_work(struct btrfs_work *work) |
| 9375 | { |
| 9376 | struct btrfs_delalloc_work *delalloc_work; |
Josef Bacik | 9f23e28 | 2013-10-28 15:03:41 -0400 | [diff] [blame] | 9377 | struct inode *inode; |
Miao Xie | 8ccf6f19 | 2012-10-25 09:28:04 +0000 | [diff] [blame] | 9378 | |
| 9379 | delalloc_work = container_of(work, struct btrfs_delalloc_work, |
| 9380 | work); |
Josef Bacik | 9f23e28 | 2013-10-28 15:03:41 -0400 | [diff] [blame] | 9381 | inode = delalloc_work->inode; |
| 9382 | if (delalloc_work->wait) { |
| 9383 | btrfs_wait_ordered_range(inode, 0, (u64)-1); |
| 9384 | } else { |
| 9385 | filemap_flush(inode->i_mapping); |
| 9386 | if (test_bit(BTRFS_INODE_HAS_ASYNC_EXTENT, |
| 9387 | &BTRFS_I(inode)->runtime_flags)) |
| 9388 | filemap_flush(inode->i_mapping); |
| 9389 | } |
Miao Xie | 8ccf6f19 | 2012-10-25 09:28:04 +0000 | [diff] [blame] | 9390 | |
| 9391 | if (delalloc_work->delay_iput) |
Josef Bacik | 9f23e28 | 2013-10-28 15:03:41 -0400 | [diff] [blame] | 9392 | btrfs_add_delayed_iput(inode); |
Miao Xie | 8ccf6f19 | 2012-10-25 09:28:04 +0000 | [diff] [blame] | 9393 | else |
Josef Bacik | 9f23e28 | 2013-10-28 15:03:41 -0400 | [diff] [blame] | 9394 | iput(inode); |
Miao Xie | 8ccf6f19 | 2012-10-25 09:28:04 +0000 | [diff] [blame] | 9395 | complete(&delalloc_work->completion); |
| 9396 | } |
| 9397 | |
| 9398 | struct btrfs_delalloc_work *btrfs_alloc_delalloc_work(struct inode *inode, |
| 9399 | int wait, int delay_iput) |
| 9400 | { |
| 9401 | struct btrfs_delalloc_work *work; |
| 9402 | |
| 9403 | work = kmem_cache_zalloc(btrfs_delalloc_work_cachep, GFP_NOFS); |
| 9404 | if (!work) |
| 9405 | return NULL; |
| 9406 | |
| 9407 | init_completion(&work->completion); |
| 9408 | INIT_LIST_HEAD(&work->list); |
| 9409 | work->inode = inode; |
| 9410 | work->wait = wait; |
| 9411 | work->delay_iput = delay_iput; |
Liu Bo | 9e0af23 | 2014-08-15 23:36:53 +0800 | [diff] [blame] | 9412 | WARN_ON_ONCE(!inode); |
| 9413 | btrfs_init_work(&work->work, btrfs_flush_delalloc_helper, |
| 9414 | btrfs_run_delalloc_work, NULL, NULL); |
Miao Xie | 8ccf6f19 | 2012-10-25 09:28:04 +0000 | [diff] [blame] | 9415 | |
| 9416 | return work; |
| 9417 | } |
| 9418 | |
| 9419 | void btrfs_wait_and_free_delalloc_work(struct btrfs_delalloc_work *work) |
| 9420 | { |
| 9421 | wait_for_completion(&work->completion); |
| 9422 | kmem_cache_free(btrfs_delalloc_work_cachep, work); |
| 9423 | } |
| 9424 | |
Chris Mason | d352ac6 | 2008-09-29 15:18:18 -0400 | [diff] [blame] | 9425 | /* |
| 9426 | * some fairly slow code that needs optimization. This walks the list |
| 9427 | * of all the inodes with pending delalloc and forces them to disk. |
| 9428 | */ |
Miao Xie | 6c255e6 | 2014-03-06 13:55:01 +0800 | [diff] [blame] | 9429 | static int __start_delalloc_inodes(struct btrfs_root *root, int delay_iput, |
| 9430 | int nr) |
Chris Mason | ea8c281 | 2008-08-04 23:17:27 -0400 | [diff] [blame] | 9431 | { |
Chris Mason | ea8c281 | 2008-08-04 23:17:27 -0400 | [diff] [blame] | 9432 | struct btrfs_inode *binode; |
Zheng Yan | 5b21f2e | 2008-09-26 10:05:38 -0400 | [diff] [blame] | 9433 | struct inode *inode; |
Miao Xie | 8ccf6f19 | 2012-10-25 09:28:04 +0000 | [diff] [blame] | 9434 | struct btrfs_delalloc_work *work, *next; |
| 9435 | struct list_head works; |
Miao Xie | 1eafa6c | 2013-01-22 10:49:00 +0000 | [diff] [blame] | 9436 | struct list_head splice; |
Miao Xie | 8ccf6f19 | 2012-10-25 09:28:04 +0000 | [diff] [blame] | 9437 | int ret = 0; |
Chris Mason | ea8c281 | 2008-08-04 23:17:27 -0400 | [diff] [blame] | 9438 | |
Miao Xie | 8ccf6f19 | 2012-10-25 09:28:04 +0000 | [diff] [blame] | 9439 | INIT_LIST_HEAD(&works); |
Miao Xie | 1eafa6c | 2013-01-22 10:49:00 +0000 | [diff] [blame] | 9440 | INIT_LIST_HEAD(&splice); |
Miao Xie | 63607cc | 2013-01-22 10:50:35 +0000 | [diff] [blame] | 9441 | |
Miao Xie | 573bfb7 | 2014-03-06 13:55:03 +0800 | [diff] [blame] | 9442 | mutex_lock(&root->delalloc_mutex); |
Miao Xie | eb73c1b | 2013-05-15 07:48:22 +0000 | [diff] [blame] | 9443 | spin_lock(&root->delalloc_lock); |
| 9444 | list_splice_init(&root->delalloc_inodes, &splice); |
Miao Xie | 1eafa6c | 2013-01-22 10:49:00 +0000 | [diff] [blame] | 9445 | while (!list_empty(&splice)) { |
| 9446 | binode = list_entry(splice.next, struct btrfs_inode, |
Chris Mason | ea8c281 | 2008-08-04 23:17:27 -0400 | [diff] [blame] | 9447 | delalloc_inodes); |
Miao Xie | 1eafa6c | 2013-01-22 10:49:00 +0000 | [diff] [blame] | 9448 | |
Miao Xie | eb73c1b | 2013-05-15 07:48:22 +0000 | [diff] [blame] | 9449 | list_move_tail(&binode->delalloc_inodes, |
| 9450 | &root->delalloc_inodes); |
Zheng Yan | 5b21f2e | 2008-09-26 10:05:38 -0400 | [diff] [blame] | 9451 | inode = igrab(&binode->vfs_inode); |
Miao Xie | df0af1a | 2013-01-29 10:11:59 +0000 | [diff] [blame] | 9452 | if (!inode) { |
Miao Xie | eb73c1b | 2013-05-15 07:48:22 +0000 | [diff] [blame] | 9453 | cond_resched_lock(&root->delalloc_lock); |
Miao Xie | 1eafa6c | 2013-01-22 10:49:00 +0000 | [diff] [blame] | 9454 | continue; |
Miao Xie | df0af1a | 2013-01-29 10:11:59 +0000 | [diff] [blame] | 9455 | } |
Miao Xie | eb73c1b | 2013-05-15 07:48:22 +0000 | [diff] [blame] | 9456 | spin_unlock(&root->delalloc_lock); |
Miao Xie | 1eafa6c | 2013-01-22 10:49:00 +0000 | [diff] [blame] | 9457 | |
| 9458 | work = btrfs_alloc_delalloc_work(inode, 0, delay_iput); |
David Sterba | 5d99a998 | 2014-09-29 19:20:37 +0200 | [diff] [blame] | 9459 | if (!work) { |
Josef Bacik | f4ab9ea | 2013-09-17 11:25:44 -0400 | [diff] [blame] | 9460 | if (delay_iput) |
| 9461 | btrfs_add_delayed_iput(inode); |
| 9462 | else |
| 9463 | iput(inode); |
Miao Xie | 1eafa6c | 2013-01-22 10:49:00 +0000 | [diff] [blame] | 9464 | ret = -ENOMEM; |
Wang Shilong | a1ecaab | 2014-04-02 19:53:32 +0800 | [diff] [blame] | 9465 | goto out; |
Zheng Yan | 5b21f2e | 2008-09-26 10:05:38 -0400 | [diff] [blame] | 9466 | } |
Miao Xie | 1eafa6c | 2013-01-22 10:49:00 +0000 | [diff] [blame] | 9467 | list_add_tail(&work->list, &works); |
Qu Wenruo | a44903a | 2014-02-28 10:46:09 +0800 | [diff] [blame] | 9468 | btrfs_queue_work(root->fs_info->flush_workers, |
| 9469 | &work->work); |
Miao Xie | 6c255e6 | 2014-03-06 13:55:01 +0800 | [diff] [blame] | 9470 | ret++; |
| 9471 | if (nr != -1 && ret >= nr) |
Wang Shilong | a1ecaab | 2014-04-02 19:53:32 +0800 | [diff] [blame] | 9472 | goto out; |
Zheng Yan | 5b21f2e | 2008-09-26 10:05:38 -0400 | [diff] [blame] | 9473 | cond_resched(); |
Miao Xie | eb73c1b | 2013-05-15 07:48:22 +0000 | [diff] [blame] | 9474 | spin_lock(&root->delalloc_lock); |
Chris Mason | ea8c281 | 2008-08-04 23:17:27 -0400 | [diff] [blame] | 9475 | } |
Miao Xie | eb73c1b | 2013-05-15 07:48:22 +0000 | [diff] [blame] | 9476 | spin_unlock(&root->delalloc_lock); |
Chris Mason | 8c8bee1 | 2008-09-29 11:19:10 -0400 | [diff] [blame] | 9477 | |
Wang Shilong | a1ecaab | 2014-04-02 19:53:32 +0800 | [diff] [blame] | 9478 | out: |
Miao Xie | 1eafa6c | 2013-01-22 10:49:00 +0000 | [diff] [blame] | 9479 | list_for_each_entry_safe(work, next, &works, list) { |
| 9480 | list_del_init(&work->list); |
| 9481 | btrfs_wait_and_free_delalloc_work(work); |
| 9482 | } |
| 9483 | |
Miao Xie | eb73c1b | 2013-05-15 07:48:22 +0000 | [diff] [blame] | 9484 | if (!list_empty_careful(&splice)) { |
| 9485 | spin_lock(&root->delalloc_lock); |
| 9486 | list_splice_tail(&splice, &root->delalloc_inodes); |
| 9487 | spin_unlock(&root->delalloc_lock); |
| 9488 | } |
Miao Xie | 573bfb7 | 2014-03-06 13:55:03 +0800 | [diff] [blame] | 9489 | mutex_unlock(&root->delalloc_mutex); |
Miao Xie | eb73c1b | 2013-05-15 07:48:22 +0000 | [diff] [blame] | 9490 | return ret; |
| 9491 | } |
| 9492 | |
| 9493 | int btrfs_start_delalloc_inodes(struct btrfs_root *root, int delay_iput) |
| 9494 | { |
| 9495 | int ret; |
| 9496 | |
Wang Shilong | 2c21b4d | 2014-01-14 19:42:20 +0800 | [diff] [blame] | 9497 | if (test_bit(BTRFS_FS_STATE_ERROR, &root->fs_info->fs_state)) |
Miao Xie | eb73c1b | 2013-05-15 07:48:22 +0000 | [diff] [blame] | 9498 | return -EROFS; |
| 9499 | |
Miao Xie | 6c255e6 | 2014-03-06 13:55:01 +0800 | [diff] [blame] | 9500 | ret = __start_delalloc_inodes(root, delay_iput, -1); |
| 9501 | if (ret > 0) |
| 9502 | ret = 0; |
Miao Xie | eb73c1b | 2013-05-15 07:48:22 +0000 | [diff] [blame] | 9503 | /* |
| 9504 | * the filemap_flush will queue IO into the worker threads, but |
Chris Mason | 8c8bee1 | 2008-09-29 11:19:10 -0400 | [diff] [blame] | 9505 | * we have to make sure the IO is actually started and that |
| 9506 | * ordered extents get created before we return |
| 9507 | */ |
| 9508 | atomic_inc(&root->fs_info->async_submit_draining); |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 9509 | while (atomic_read(&root->fs_info->nr_async_submits) || |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 9510 | atomic_read(&root->fs_info->async_delalloc_pages)) { |
Chris Mason | 8c8bee1 | 2008-09-29 11:19:10 -0400 | [diff] [blame] | 9511 | wait_event(root->fs_info->async_submit_wait, |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 9512 | (atomic_read(&root->fs_info->nr_async_submits) == 0 && |
| 9513 | atomic_read(&root->fs_info->async_delalloc_pages) == 0)); |
Chris Mason | 8c8bee1 | 2008-09-29 11:19:10 -0400 | [diff] [blame] | 9514 | } |
| 9515 | atomic_dec(&root->fs_info->async_submit_draining); |
Miao Xie | eb73c1b | 2013-05-15 07:48:22 +0000 | [diff] [blame] | 9516 | return ret; |
| 9517 | } |
| 9518 | |
Miao Xie | 6c255e6 | 2014-03-06 13:55:01 +0800 | [diff] [blame] | 9519 | int btrfs_start_delalloc_roots(struct btrfs_fs_info *fs_info, int delay_iput, |
| 9520 | int nr) |
Miao Xie | eb73c1b | 2013-05-15 07:48:22 +0000 | [diff] [blame] | 9521 | { |
| 9522 | struct btrfs_root *root; |
| 9523 | struct list_head splice; |
| 9524 | int ret; |
| 9525 | |
Wang Shilong | 2c21b4d | 2014-01-14 19:42:20 +0800 | [diff] [blame] | 9526 | if (test_bit(BTRFS_FS_STATE_ERROR, &fs_info->fs_state)) |
Miao Xie | eb73c1b | 2013-05-15 07:48:22 +0000 | [diff] [blame] | 9527 | return -EROFS; |
| 9528 | |
| 9529 | INIT_LIST_HEAD(&splice); |
| 9530 | |
Miao Xie | 573bfb7 | 2014-03-06 13:55:03 +0800 | [diff] [blame] | 9531 | mutex_lock(&fs_info->delalloc_root_mutex); |
Miao Xie | eb73c1b | 2013-05-15 07:48:22 +0000 | [diff] [blame] | 9532 | spin_lock(&fs_info->delalloc_root_lock); |
| 9533 | list_splice_init(&fs_info->delalloc_roots, &splice); |
Miao Xie | 6c255e6 | 2014-03-06 13:55:01 +0800 | [diff] [blame] | 9534 | while (!list_empty(&splice) && nr) { |
Miao Xie | eb73c1b | 2013-05-15 07:48:22 +0000 | [diff] [blame] | 9535 | root = list_first_entry(&splice, struct btrfs_root, |
| 9536 | delalloc_root); |
| 9537 | root = btrfs_grab_fs_root(root); |
| 9538 | BUG_ON(!root); |
| 9539 | list_move_tail(&root->delalloc_root, |
| 9540 | &fs_info->delalloc_roots); |
| 9541 | spin_unlock(&fs_info->delalloc_root_lock); |
| 9542 | |
Miao Xie | 6c255e6 | 2014-03-06 13:55:01 +0800 | [diff] [blame] | 9543 | ret = __start_delalloc_inodes(root, delay_iput, nr); |
Miao Xie | eb73c1b | 2013-05-15 07:48:22 +0000 | [diff] [blame] | 9544 | btrfs_put_fs_root(root); |
Miao Xie | 6c255e6 | 2014-03-06 13:55:01 +0800 | [diff] [blame] | 9545 | if (ret < 0) |
Miao Xie | eb73c1b | 2013-05-15 07:48:22 +0000 | [diff] [blame] | 9546 | goto out; |
| 9547 | |
Miao Xie | 6c255e6 | 2014-03-06 13:55:01 +0800 | [diff] [blame] | 9548 | if (nr != -1) { |
| 9549 | nr -= ret; |
| 9550 | WARN_ON(nr < 0); |
| 9551 | } |
Miao Xie | eb73c1b | 2013-05-15 07:48:22 +0000 | [diff] [blame] | 9552 | spin_lock(&fs_info->delalloc_root_lock); |
| 9553 | } |
| 9554 | spin_unlock(&fs_info->delalloc_root_lock); |
| 9555 | |
Miao Xie | 6c255e6 | 2014-03-06 13:55:01 +0800 | [diff] [blame] | 9556 | ret = 0; |
Miao Xie | eb73c1b | 2013-05-15 07:48:22 +0000 | [diff] [blame] | 9557 | atomic_inc(&fs_info->async_submit_draining); |
| 9558 | while (atomic_read(&fs_info->nr_async_submits) || |
| 9559 | atomic_read(&fs_info->async_delalloc_pages)) { |
| 9560 | wait_event(fs_info->async_submit_wait, |
| 9561 | (atomic_read(&fs_info->nr_async_submits) == 0 && |
| 9562 | atomic_read(&fs_info->async_delalloc_pages) == 0)); |
| 9563 | } |
| 9564 | atomic_dec(&fs_info->async_submit_draining); |
Miao Xie | 8ccf6f19 | 2012-10-25 09:28:04 +0000 | [diff] [blame] | 9565 | out: |
Miao Xie | 1eafa6c | 2013-01-22 10:49:00 +0000 | [diff] [blame] | 9566 | if (!list_empty_careful(&splice)) { |
Miao Xie | eb73c1b | 2013-05-15 07:48:22 +0000 | [diff] [blame] | 9567 | spin_lock(&fs_info->delalloc_root_lock); |
| 9568 | list_splice_tail(&splice, &fs_info->delalloc_roots); |
| 9569 | spin_unlock(&fs_info->delalloc_root_lock); |
Miao Xie | 1eafa6c | 2013-01-22 10:49:00 +0000 | [diff] [blame] | 9570 | } |
Miao Xie | 573bfb7 | 2014-03-06 13:55:03 +0800 | [diff] [blame] | 9571 | mutex_unlock(&fs_info->delalloc_root_mutex); |
Miao Xie | 8ccf6f19 | 2012-10-25 09:28:04 +0000 | [diff] [blame] | 9572 | return ret; |
Chris Mason | ea8c281 | 2008-08-04 23:17:27 -0400 | [diff] [blame] | 9573 | } |
| 9574 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 9575 | static int btrfs_symlink(struct inode *dir, struct dentry *dentry, |
| 9576 | const char *symname) |
| 9577 | { |
| 9578 | struct btrfs_trans_handle *trans; |
| 9579 | struct btrfs_root *root = BTRFS_I(dir)->root; |
| 9580 | struct btrfs_path *path; |
| 9581 | struct btrfs_key key; |
Chris Mason | 1832a6d | 2007-12-21 16:27:21 -0500 | [diff] [blame] | 9582 | struct inode *inode = NULL; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 9583 | int err; |
| 9584 | int drop_inode = 0; |
| 9585 | u64 objectid; |
Dulshani Gunawardhana | 6787125 | 2013-10-31 10:33:04 +0530 | [diff] [blame] | 9586 | u64 index = 0; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 9587 | int name_len; |
| 9588 | int datasize; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 9589 | unsigned long ptr; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 9590 | struct btrfs_file_extent_item *ei; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 9591 | struct extent_buffer *leaf; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 9592 | |
Filipe David Borba Manana | f06becc | 2013-09-16 09:53:28 +0100 | [diff] [blame] | 9593 | name_len = strlen(symname); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 9594 | if (name_len > BTRFS_MAX_INLINE_DATA_SIZE(root)) |
| 9595 | return -ENAMETOOLONG; |
Chris Mason | 1832a6d | 2007-12-21 16:27:21 -0500 | [diff] [blame] | 9596 | |
Josef Bacik | 9ed74f2 | 2009-09-11 16:12:44 -0400 | [diff] [blame] | 9597 | /* |
| 9598 | * 2 items for inode item and ref |
| 9599 | * 2 items for dir items |
| 9600 | * 1 item for xattr if selinux is on |
| 9601 | */ |
Yan, Zheng | a22285a | 2010-05-16 10:48:46 -0400 | [diff] [blame] | 9602 | trans = btrfs_start_transaction(root, 5); |
| 9603 | if (IS_ERR(trans)) |
| 9604 | return PTR_ERR(trans); |
Chris Mason | 1832a6d | 2007-12-21 16:27:21 -0500 | [diff] [blame] | 9605 | |
Li Zefan | 581bb05 | 2011-04-20 10:06:11 +0800 | [diff] [blame] | 9606 | err = btrfs_find_free_ino(root, &objectid); |
| 9607 | if (err) |
| 9608 | goto out_unlock; |
| 9609 | |
Josef Bacik | aec7477 | 2008-07-24 12:12:38 -0400 | [diff] [blame] | 9610 | inode = btrfs_new_inode(trans, root, dir, dentry->d_name.name, |
Li Zefan | 33345d01 | 2011-04-20 10:31:50 +0800 | [diff] [blame] | 9611 | dentry->d_name.len, btrfs_ino(dir), objectid, |
Josef Bacik | d82a6f1d | 2011-05-11 15:26:06 -0400 | [diff] [blame] | 9612 | S_IFLNK|S_IRWXUGO, &index); |
Tsutomu Itoh | 7cf96da | 2011-04-25 19:43:53 -0400 | [diff] [blame] | 9613 | if (IS_ERR(inode)) { |
| 9614 | err = PTR_ERR(inode); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 9615 | goto out_unlock; |
Tsutomu Itoh | 7cf96da | 2011-04-25 19:43:53 -0400 | [diff] [blame] | 9616 | } |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 9617 | |
Casey Schaufler | ad19db7 | 2011-12-15 10:09:07 -0500 | [diff] [blame] | 9618 | /* |
| 9619 | * If the active LSM wants to access the inode during |
| 9620 | * d_instantiate it needs these. Smack checks to see |
| 9621 | * if the filesystem supports xattrs by looking at the |
| 9622 | * ops vector. |
| 9623 | */ |
| 9624 | inode->i_fop = &btrfs_file_operations; |
| 9625 | inode->i_op = &btrfs_file_inode_operations; |
Chris Mason | b0d5d10 | 2014-09-08 13:08:51 -0700 | [diff] [blame] | 9626 | inode->i_mapping->a_ops = &btrfs_aops; |
Chris Mason | b0d5d10 | 2014-09-08 13:08:51 -0700 | [diff] [blame] | 9627 | BTRFS_I(inode)->io_tree.ops = &btrfs_extent_io_ops; |
| 9628 | |
| 9629 | err = btrfs_init_inode_security(trans, inode, dir, &dentry->d_name); |
| 9630 | if (err) |
| 9631 | goto out_unlock_inode; |
Casey Schaufler | ad19db7 | 2011-12-15 10:09:07 -0500 | [diff] [blame] | 9632 | |
Josef Bacik | a1b075d | 2010-11-19 20:36:11 +0000 | [diff] [blame] | 9633 | err = btrfs_add_nondir(trans, dir, dentry, inode, 0, index); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 9634 | if (err) |
Chris Mason | b0d5d10 | 2014-09-08 13:08:51 -0700 | [diff] [blame] | 9635 | goto out_unlock_inode; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 9636 | |
| 9637 | path = btrfs_alloc_path(); |
Mark Fasheh | d8926bb | 2011-07-13 10:38:47 -0700 | [diff] [blame] | 9638 | if (!path) { |
| 9639 | err = -ENOMEM; |
Chris Mason | b0d5d10 | 2014-09-08 13:08:51 -0700 | [diff] [blame] | 9640 | goto out_unlock_inode; |
Mark Fasheh | d8926bb | 2011-07-13 10:38:47 -0700 | [diff] [blame] | 9641 | } |
Li Zefan | 33345d01 | 2011-04-20 10:31:50 +0800 | [diff] [blame] | 9642 | key.objectid = btrfs_ino(inode); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 9643 | key.offset = 0; |
David Sterba | 962a298 | 2014-06-04 18:41:45 +0200 | [diff] [blame] | 9644 | key.type = BTRFS_EXTENT_DATA_KEY; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 9645 | datasize = btrfs_file_extent_calc_inline_size(name_len); |
| 9646 | err = btrfs_insert_empty_item(trans, root, path, &key, |
| 9647 | datasize); |
Chris Mason | 54aa1f4 | 2007-06-22 14:16:25 -0400 | [diff] [blame] | 9648 | if (err) { |
Julia Lawall | b083916 | 2011-05-14 07:10:51 +0000 | [diff] [blame] | 9649 | btrfs_free_path(path); |
Chris Mason | b0d5d10 | 2014-09-08 13:08:51 -0700 | [diff] [blame] | 9650 | goto out_unlock_inode; |
Chris Mason | 54aa1f4 | 2007-06-22 14:16:25 -0400 | [diff] [blame] | 9651 | } |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 9652 | leaf = path->nodes[0]; |
| 9653 | ei = btrfs_item_ptr(leaf, path->slots[0], |
| 9654 | struct btrfs_file_extent_item); |
| 9655 | btrfs_set_file_extent_generation(leaf, ei, trans->transid); |
| 9656 | btrfs_set_file_extent_type(leaf, ei, |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 9657 | BTRFS_FILE_EXTENT_INLINE); |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 9658 | btrfs_set_file_extent_encryption(leaf, ei, 0); |
| 9659 | btrfs_set_file_extent_compression(leaf, ei, 0); |
| 9660 | btrfs_set_file_extent_other_encoding(leaf, ei, 0); |
| 9661 | btrfs_set_file_extent_ram_bytes(leaf, ei, name_len); |
| 9662 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 9663 | ptr = btrfs_file_extent_inline_start(ei); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 9664 | write_extent_buffer(leaf, symname, ptr, name_len); |
| 9665 | btrfs_mark_buffer_dirty(leaf); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 9666 | btrfs_free_path(path); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 9667 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 9668 | inode->i_op = &btrfs_symlink_inode_operations; |
| 9669 | inode->i_mapping->a_ops = &btrfs_symlink_aops; |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 9670 | inode_set_bytes(inode, name_len); |
Filipe David Borba Manana | f06becc | 2013-09-16 09:53:28 +0100 | [diff] [blame] | 9671 | btrfs_i_size_write(inode, name_len); |
Chris Mason | 54aa1f4 | 2007-06-22 14:16:25 -0400 | [diff] [blame] | 9672 | err = btrfs_update_inode(trans, root, inode); |
Chris Mason | b0d5d10 | 2014-09-08 13:08:51 -0700 | [diff] [blame] | 9673 | if (err) { |
Chris Mason | 54aa1f4 | 2007-06-22 14:16:25 -0400 | [diff] [blame] | 9674 | drop_inode = 1; |
Chris Mason | b0d5d10 | 2014-09-08 13:08:51 -0700 | [diff] [blame] | 9675 | goto out_unlock_inode; |
| 9676 | } |
| 9677 | |
| 9678 | unlock_new_inode(inode); |
| 9679 | d_instantiate(dentry, inode); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 9680 | |
| 9681 | out_unlock: |
Josef Bacik | 7ad85bb | 2012-01-12 19:10:12 -0500 | [diff] [blame] | 9682 | btrfs_end_transaction(trans, root); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 9683 | if (drop_inode) { |
| 9684 | inode_dec_link_count(inode); |
| 9685 | iput(inode); |
| 9686 | } |
Liu Bo | b53d3f5 | 2012-11-14 14:34:34 +0000 | [diff] [blame] | 9687 | btrfs_btree_balance_dirty(root); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 9688 | return err; |
Chris Mason | b0d5d10 | 2014-09-08 13:08:51 -0700 | [diff] [blame] | 9689 | |
| 9690 | out_unlock_inode: |
| 9691 | drop_inode = 1; |
| 9692 | unlock_new_inode(inode); |
| 9693 | goto out_unlock; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 9694 | } |
Chris Mason | 1643298 | 2008-04-10 10:23:21 -0400 | [diff] [blame] | 9695 | |
Josef Bacik | 0af3d00 | 2010-06-21 14:48:16 -0400 | [diff] [blame] | 9696 | static int __btrfs_prealloc_file_range(struct inode *inode, int mode, |
| 9697 | u64 start, u64 num_bytes, u64 min_size, |
| 9698 | loff_t actual_len, u64 *alloc_hint, |
| 9699 | struct btrfs_trans_handle *trans) |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 9700 | { |
Josef Bacik | 5dc562c | 2012-08-17 13:14:17 -0400 | [diff] [blame] | 9701 | struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree; |
| 9702 | struct extent_map *em; |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 9703 | struct btrfs_root *root = BTRFS_I(inode)->root; |
| 9704 | struct btrfs_key ins; |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 9705 | u64 cur_offset = start; |
Josef Bacik | 55a61d1 | 2010-11-22 18:50:32 +0000 | [diff] [blame] | 9706 | u64 i_size; |
Chris Mason | 154ea28 | 2013-03-05 11:11:26 -0500 | [diff] [blame] | 9707 | u64 cur_bytes; |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 9708 | int ret = 0; |
Josef Bacik | 0af3d00 | 2010-06-21 14:48:16 -0400 | [diff] [blame] | 9709 | bool own_trans = true; |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 9710 | |
Josef Bacik | 0af3d00 | 2010-06-21 14:48:16 -0400 | [diff] [blame] | 9711 | if (trans) |
| 9712 | own_trans = false; |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 9713 | while (num_bytes > 0) { |
Josef Bacik | 0af3d00 | 2010-06-21 14:48:16 -0400 | [diff] [blame] | 9714 | if (own_trans) { |
| 9715 | trans = btrfs_start_transaction(root, 3); |
| 9716 | if (IS_ERR(trans)) { |
| 9717 | ret = PTR_ERR(trans); |
| 9718 | break; |
| 9719 | } |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 9720 | } |
Yan, Zheng | 5a303d5 | 2009-11-12 09:34:52 +0000 | [diff] [blame] | 9721 | |
Chris Mason | 154ea28 | 2013-03-05 11:11:26 -0500 | [diff] [blame] | 9722 | cur_bytes = min(num_bytes, 256ULL * 1024 * 1024); |
| 9723 | cur_bytes = max(cur_bytes, min_size); |
Josef Bacik | 0036158 | 2013-08-14 14:02:47 -0400 | [diff] [blame] | 9724 | ret = btrfs_reserve_extent(root, cur_bytes, min_size, 0, |
Miao Xie | e570fd2 | 2014-06-19 10:42:50 +0800 | [diff] [blame] | 9725 | *alloc_hint, &ins, 1, 0); |
Yan, Zheng | 5a303d5 | 2009-11-12 09:34:52 +0000 | [diff] [blame] | 9726 | if (ret) { |
Josef Bacik | 0af3d00 | 2010-06-21 14:48:16 -0400 | [diff] [blame] | 9727 | if (own_trans) |
| 9728 | btrfs_end_transaction(trans, root); |
Yan, Zheng | a22285a | 2010-05-16 10:48:46 -0400 | [diff] [blame] | 9729 | break; |
Yan, Zheng | 5a303d5 | 2009-11-12 09:34:52 +0000 | [diff] [blame] | 9730 | } |
| 9731 | |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 9732 | ret = insert_reserved_file_extent(trans, inode, |
| 9733 | cur_offset, ins.objectid, |
| 9734 | ins.offset, ins.offset, |
Yan, Zheng | 920bbbf | 2009-11-12 09:34:08 +0000 | [diff] [blame] | 9735 | ins.offset, 0, 0, 0, |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 9736 | BTRFS_FILE_EXTENT_PREALLOC); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 9737 | if (ret) { |
Josef Bacik | 857cc2f | 2013-10-07 15:21:08 -0400 | [diff] [blame] | 9738 | btrfs_free_reserved_extent(root, ins.objectid, |
Miao Xie | e570fd2 | 2014-06-19 10:42:50 +0800 | [diff] [blame] | 9739 | ins.offset, 0); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 9740 | btrfs_abort_transaction(trans, root, ret); |
| 9741 | if (own_trans) |
| 9742 | btrfs_end_transaction(trans, root); |
| 9743 | break; |
| 9744 | } |
Dongsheng Yang | 3119321 | 2014-12-12 16:44:35 +0800 | [diff] [blame] | 9745 | |
Chris Mason | a1ed835 | 2009-09-11 12:27:37 -0400 | [diff] [blame] | 9746 | btrfs_drop_extent_cache(inode, cur_offset, |
| 9747 | cur_offset + ins.offset -1, 0); |
Yan, Zheng | 5a303d5 | 2009-11-12 09:34:52 +0000 | [diff] [blame] | 9748 | |
Josef Bacik | 5dc562c | 2012-08-17 13:14:17 -0400 | [diff] [blame] | 9749 | em = alloc_extent_map(); |
| 9750 | if (!em) { |
| 9751 | set_bit(BTRFS_INODE_NEEDS_FULL_SYNC, |
| 9752 | &BTRFS_I(inode)->runtime_flags); |
| 9753 | goto next; |
| 9754 | } |
| 9755 | |
| 9756 | em->start = cur_offset; |
| 9757 | em->orig_start = cur_offset; |
| 9758 | em->len = ins.offset; |
| 9759 | em->block_start = ins.objectid; |
| 9760 | em->block_len = ins.offset; |
Josef Bacik | b493968 | 2012-12-03 10:31:19 -0500 | [diff] [blame] | 9761 | em->orig_block_len = ins.offset; |
Josef Bacik | cc95bef | 2013-04-04 14:31:27 -0400 | [diff] [blame] | 9762 | em->ram_bytes = ins.offset; |
Josef Bacik | 5dc562c | 2012-08-17 13:14:17 -0400 | [diff] [blame] | 9763 | em->bdev = root->fs_info->fs_devices->latest_bdev; |
| 9764 | set_bit(EXTENT_FLAG_PREALLOC, &em->flags); |
| 9765 | em->generation = trans->transid; |
| 9766 | |
| 9767 | while (1) { |
| 9768 | write_lock(&em_tree->lock); |
Josef Bacik | 09a2a8f9 | 2013-04-05 16:51:15 -0400 | [diff] [blame] | 9769 | ret = add_extent_mapping(em_tree, em, 1); |
Josef Bacik | 5dc562c | 2012-08-17 13:14:17 -0400 | [diff] [blame] | 9770 | write_unlock(&em_tree->lock); |
| 9771 | if (ret != -EEXIST) |
| 9772 | break; |
| 9773 | btrfs_drop_extent_cache(inode, cur_offset, |
| 9774 | cur_offset + ins.offset - 1, |
| 9775 | 0); |
| 9776 | } |
| 9777 | free_extent_map(em); |
| 9778 | next: |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 9779 | num_bytes -= ins.offset; |
| 9780 | cur_offset += ins.offset; |
Yan, Zheng | efa5646 | 2010-05-16 10:49:59 -0400 | [diff] [blame] | 9781 | *alloc_hint = ins.objectid + ins.offset; |
Yan, Zheng | 5a303d5 | 2009-11-12 09:34:52 +0000 | [diff] [blame] | 9782 | |
Josef Bacik | 0c4d2d9 | 2012-04-05 15:03:02 -0400 | [diff] [blame] | 9783 | inode_inc_iversion(inode); |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 9784 | inode->i_ctime = CURRENT_TIME; |
Christoph Hellwig | 6cbff00 | 2009-04-17 10:37:41 +0200 | [diff] [blame] | 9785 | BTRFS_I(inode)->flags |= BTRFS_INODE_PREALLOC; |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 9786 | if (!(mode & FALLOC_FL_KEEP_SIZE) && |
Yan, Zheng | efa5646 | 2010-05-16 10:49:59 -0400 | [diff] [blame] | 9787 | (actual_len > inode->i_size) && |
| 9788 | (cur_offset > inode->i_size)) { |
Aneesh Kumar K.V | d1ea6a6 | 2010-01-20 07:28:54 +0000 | [diff] [blame] | 9789 | if (cur_offset > actual_len) |
Josef Bacik | 55a61d1 | 2010-11-22 18:50:32 +0000 | [diff] [blame] | 9790 | i_size = actual_len; |
Aneesh Kumar K.V | d1ea6a6 | 2010-01-20 07:28:54 +0000 | [diff] [blame] | 9791 | else |
Josef Bacik | 55a61d1 | 2010-11-22 18:50:32 +0000 | [diff] [blame] | 9792 | i_size = cur_offset; |
| 9793 | i_size_write(inode, i_size); |
| 9794 | btrfs_ordered_update_i_size(inode, i_size, NULL); |
Yan, Zheng | 5a303d5 | 2009-11-12 09:34:52 +0000 | [diff] [blame] | 9795 | } |
| 9796 | |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 9797 | ret = btrfs_update_inode(trans, root, inode); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 9798 | |
| 9799 | if (ret) { |
| 9800 | btrfs_abort_transaction(trans, root, ret); |
| 9801 | if (own_trans) |
| 9802 | btrfs_end_transaction(trans, root); |
| 9803 | break; |
| 9804 | } |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 9805 | |
Josef Bacik | 0af3d00 | 2010-06-21 14:48:16 -0400 | [diff] [blame] | 9806 | if (own_trans) |
| 9807 | btrfs_end_transaction(trans, root); |
Yan, Zheng | 5a303d5 | 2009-11-12 09:34:52 +0000 | [diff] [blame] | 9808 | } |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 9809 | return ret; |
| 9810 | } |
| 9811 | |
Josef Bacik | 0af3d00 | 2010-06-21 14:48:16 -0400 | [diff] [blame] | 9812 | int btrfs_prealloc_file_range(struct inode *inode, int mode, |
| 9813 | u64 start, u64 num_bytes, u64 min_size, |
| 9814 | loff_t actual_len, u64 *alloc_hint) |
| 9815 | { |
| 9816 | return __btrfs_prealloc_file_range(inode, mode, start, num_bytes, |
| 9817 | min_size, actual_len, alloc_hint, |
| 9818 | NULL); |
| 9819 | } |
| 9820 | |
| 9821 | int btrfs_prealloc_file_range_trans(struct inode *inode, |
| 9822 | struct btrfs_trans_handle *trans, int mode, |
| 9823 | u64 start, u64 num_bytes, u64 min_size, |
| 9824 | loff_t actual_len, u64 *alloc_hint) |
| 9825 | { |
| 9826 | return __btrfs_prealloc_file_range(inode, mode, start, num_bytes, |
| 9827 | min_size, actual_len, alloc_hint, trans); |
| 9828 | } |
| 9829 | |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 9830 | static int btrfs_set_page_dirty(struct page *page) |
| 9831 | { |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 9832 | return __set_page_dirty_nobuffers(page); |
| 9833 | } |
| 9834 | |
Al Viro | 10556cb2 | 2011-06-20 19:28:19 -0400 | [diff] [blame] | 9835 | static int btrfs_permission(struct inode *inode, int mask) |
Yan | fdebe2b | 2008-01-14 13:26:08 -0500 | [diff] [blame] | 9836 | { |
Li Zefan | b83cc96 | 2010-12-20 16:04:08 +0800 | [diff] [blame] | 9837 | struct btrfs_root *root = BTRFS_I(inode)->root; |
Jeff Mahoney | cb6db4e | 2011-08-15 17:27:21 +0000 | [diff] [blame] | 9838 | umode_t mode = inode->i_mode; |
Li Zefan | b83cc96 | 2010-12-20 16:04:08 +0800 | [diff] [blame] | 9839 | |
Jeff Mahoney | cb6db4e | 2011-08-15 17:27:21 +0000 | [diff] [blame] | 9840 | if (mask & MAY_WRITE && |
| 9841 | (S_ISREG(mode) || S_ISDIR(mode) || S_ISLNK(mode))) { |
| 9842 | if (btrfs_root_readonly(root)) |
| 9843 | return -EROFS; |
| 9844 | if (BTRFS_I(inode)->flags & BTRFS_INODE_READONLY) |
| 9845 | return -EACCES; |
| 9846 | } |
Al Viro | 2830ba7 | 2011-06-20 19:16:29 -0400 | [diff] [blame] | 9847 | return generic_permission(inode, mask); |
Yan | fdebe2b | 2008-01-14 13:26:08 -0500 | [diff] [blame] | 9848 | } |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 9849 | |
Filipe Manana | ef3b9af | 2014-04-27 20:40:45 +0100 | [diff] [blame] | 9850 | static int btrfs_tmpfile(struct inode *dir, struct dentry *dentry, umode_t mode) |
| 9851 | { |
| 9852 | struct btrfs_trans_handle *trans; |
| 9853 | struct btrfs_root *root = BTRFS_I(dir)->root; |
| 9854 | struct inode *inode = NULL; |
| 9855 | u64 objectid; |
| 9856 | u64 index; |
| 9857 | int ret = 0; |
| 9858 | |
| 9859 | /* |
| 9860 | * 5 units required for adding orphan entry |
| 9861 | */ |
| 9862 | trans = btrfs_start_transaction(root, 5); |
| 9863 | if (IS_ERR(trans)) |
| 9864 | return PTR_ERR(trans); |
| 9865 | |
| 9866 | ret = btrfs_find_free_ino(root, &objectid); |
| 9867 | if (ret) |
| 9868 | goto out; |
| 9869 | |
| 9870 | inode = btrfs_new_inode(trans, root, dir, NULL, 0, |
| 9871 | btrfs_ino(dir), objectid, mode, &index); |
| 9872 | if (IS_ERR(inode)) { |
| 9873 | ret = PTR_ERR(inode); |
| 9874 | inode = NULL; |
| 9875 | goto out; |
| 9876 | } |
| 9877 | |
Filipe Manana | ef3b9af | 2014-04-27 20:40:45 +0100 | [diff] [blame] | 9878 | inode->i_fop = &btrfs_file_operations; |
| 9879 | inode->i_op = &btrfs_file_inode_operations; |
| 9880 | |
| 9881 | inode->i_mapping->a_ops = &btrfs_aops; |
Filipe Manana | ef3b9af | 2014-04-27 20:40:45 +0100 | [diff] [blame] | 9882 | BTRFS_I(inode)->io_tree.ops = &btrfs_extent_io_ops; |
| 9883 | |
Chris Mason | b0d5d10 | 2014-09-08 13:08:51 -0700 | [diff] [blame] | 9884 | ret = btrfs_init_inode_security(trans, inode, dir, NULL); |
| 9885 | if (ret) |
| 9886 | goto out_inode; |
| 9887 | |
| 9888 | ret = btrfs_update_inode(trans, root, inode); |
| 9889 | if (ret) |
| 9890 | goto out_inode; |
Filipe Manana | ef3b9af | 2014-04-27 20:40:45 +0100 | [diff] [blame] | 9891 | ret = btrfs_orphan_add(trans, inode); |
| 9892 | if (ret) |
Chris Mason | b0d5d10 | 2014-09-08 13:08:51 -0700 | [diff] [blame] | 9893 | goto out_inode; |
Filipe Manana | ef3b9af | 2014-04-27 20:40:45 +0100 | [diff] [blame] | 9894 | |
Filipe Manana | 5762b5c | 2014-08-01 00:10:32 +0100 | [diff] [blame] | 9895 | /* |
| 9896 | * We set number of links to 0 in btrfs_new_inode(), and here we set |
| 9897 | * it to 1 because d_tmpfile() will issue a warning if the count is 0, |
| 9898 | * through: |
| 9899 | * |
| 9900 | * d_tmpfile() -> inode_dec_link_count() -> drop_nlink() |
| 9901 | */ |
| 9902 | set_nlink(inode, 1); |
Chris Mason | b0d5d10 | 2014-09-08 13:08:51 -0700 | [diff] [blame] | 9903 | unlock_new_inode(inode); |
Filipe Manana | ef3b9af | 2014-04-27 20:40:45 +0100 | [diff] [blame] | 9904 | d_tmpfile(dentry, inode); |
| 9905 | mark_inode_dirty(inode); |
| 9906 | |
| 9907 | out: |
| 9908 | btrfs_end_transaction(trans, root); |
| 9909 | if (ret) |
| 9910 | iput(inode); |
| 9911 | btrfs_balance_delayed_items(root); |
| 9912 | btrfs_btree_balance_dirty(root); |
Filipe Manana | ef3b9af | 2014-04-27 20:40:45 +0100 | [diff] [blame] | 9913 | return ret; |
Chris Mason | b0d5d10 | 2014-09-08 13:08:51 -0700 | [diff] [blame] | 9914 | |
| 9915 | out_inode: |
| 9916 | unlock_new_inode(inode); |
| 9917 | goto out; |
| 9918 | |
Filipe Manana | ef3b9af | 2014-04-27 20:40:45 +0100 | [diff] [blame] | 9919 | } |
| 9920 | |
Filipe Manana | b38ef71 | 2014-11-13 17:01:45 +0000 | [diff] [blame] | 9921 | /* Inspired by filemap_check_errors() */ |
| 9922 | int btrfs_inode_check_errors(struct inode *inode) |
| 9923 | { |
| 9924 | int ret = 0; |
| 9925 | |
| 9926 | if (test_bit(AS_ENOSPC, &inode->i_mapping->flags) && |
| 9927 | test_and_clear_bit(AS_ENOSPC, &inode->i_mapping->flags)) |
| 9928 | ret = -ENOSPC; |
| 9929 | if (test_bit(AS_EIO, &inode->i_mapping->flags) && |
| 9930 | test_and_clear_bit(AS_EIO, &inode->i_mapping->flags)) |
| 9931 | ret = -EIO; |
| 9932 | |
| 9933 | return ret; |
| 9934 | } |
| 9935 | |
Alexey Dobriyan | 6e1d5dc | 2009-09-21 17:01:11 -0700 | [diff] [blame] | 9936 | static const struct inode_operations btrfs_dir_inode_operations = { |
Chris Mason | 3394e16 | 2008-11-17 20:42:26 -0500 | [diff] [blame] | 9937 | .getattr = btrfs_getattr, |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 9938 | .lookup = btrfs_lookup, |
| 9939 | .create = btrfs_create, |
| 9940 | .unlink = btrfs_unlink, |
| 9941 | .link = btrfs_link, |
| 9942 | .mkdir = btrfs_mkdir, |
| 9943 | .rmdir = btrfs_rmdir, |
Miklos Szeredi | 80ace85 | 2014-07-23 15:15:32 +0200 | [diff] [blame] | 9944 | .rename2 = btrfs_rename2, |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 9945 | .symlink = btrfs_symlink, |
| 9946 | .setattr = btrfs_setattr, |
Josef Bacik | 618e21d | 2007-07-11 10:18:17 -0400 | [diff] [blame] | 9947 | .mknod = btrfs_mknod, |
Christoph Hellwig | 95819c0 | 2008-08-28 06:21:17 -0400 | [diff] [blame] | 9948 | .setxattr = btrfs_setxattr, |
| 9949 | .getxattr = btrfs_getxattr, |
Josef Bacik | 5103e94 | 2007-11-16 11:45:54 -0500 | [diff] [blame] | 9950 | .listxattr = btrfs_listxattr, |
Christoph Hellwig | 95819c0 | 2008-08-28 06:21:17 -0400 | [diff] [blame] | 9951 | .removexattr = btrfs_removexattr, |
Yan | fdebe2b | 2008-01-14 13:26:08 -0500 | [diff] [blame] | 9952 | .permission = btrfs_permission, |
Christoph Hellwig | 4e34e71 | 2011-07-23 17:37:31 +0200 | [diff] [blame] | 9953 | .get_acl = btrfs_get_acl, |
Christoph Hellwig | 996a710 | 2013-12-20 05:16:43 -0800 | [diff] [blame] | 9954 | .set_acl = btrfs_set_acl, |
Guangyu Sun | 93fd63c | 2013-09-16 10:42:03 -0700 | [diff] [blame] | 9955 | .update_time = btrfs_update_time, |
Filipe Manana | ef3b9af | 2014-04-27 20:40:45 +0100 | [diff] [blame] | 9956 | .tmpfile = btrfs_tmpfile, |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 9957 | }; |
Alexey Dobriyan | 6e1d5dc | 2009-09-21 17:01:11 -0700 | [diff] [blame] | 9958 | static const struct inode_operations btrfs_dir_ro_inode_operations = { |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 9959 | .lookup = btrfs_lookup, |
Yan | fdebe2b | 2008-01-14 13:26:08 -0500 | [diff] [blame] | 9960 | .permission = btrfs_permission, |
Christoph Hellwig | 4e34e71 | 2011-07-23 17:37:31 +0200 | [diff] [blame] | 9961 | .get_acl = btrfs_get_acl, |
Christoph Hellwig | 996a710 | 2013-12-20 05:16:43 -0800 | [diff] [blame] | 9962 | .set_acl = btrfs_set_acl, |
Guangyu Sun | 93fd63c | 2013-09-16 10:42:03 -0700 | [diff] [blame] | 9963 | .update_time = btrfs_update_time, |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 9964 | }; |
Yan, Zheng | 76dda93 | 2009-09-21 16:00:26 -0400 | [diff] [blame] | 9965 | |
Alexey Dobriyan | 828c095 | 2009-10-01 15:43:56 -0700 | [diff] [blame] | 9966 | static const struct file_operations btrfs_dir_file_operations = { |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 9967 | .llseek = generic_file_llseek, |
| 9968 | .read = generic_read_dir, |
Al Viro | 9cdda8d | 2013-05-22 16:48:09 -0400 | [diff] [blame] | 9969 | .iterate = btrfs_real_readdir, |
Christoph Hellwig | 34287aa | 2007-09-14 10:22:47 -0400 | [diff] [blame] | 9970 | .unlocked_ioctl = btrfs_ioctl, |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 9971 | #ifdef CONFIG_COMPAT |
Christoph Hellwig | 34287aa | 2007-09-14 10:22:47 -0400 | [diff] [blame] | 9972 | .compat_ioctl = btrfs_ioctl, |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 9973 | #endif |
Sage Weil | 6bf13c0 | 2008-06-10 10:07:39 -0400 | [diff] [blame] | 9974 | .release = btrfs_release_file, |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 9975 | .fsync = btrfs_sync_file, |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 9976 | }; |
| 9977 | |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 9978 | static struct extent_io_ops btrfs_extent_io_ops = { |
Chris Mason | 07157aa | 2007-08-30 08:50:51 -0400 | [diff] [blame] | 9979 | .fill_delalloc = run_delalloc_range, |
Chris Mason | 065631f | 2008-02-20 12:07:25 -0500 | [diff] [blame] | 9980 | .submit_bio_hook = btrfs_submit_bio_hook, |
Chris Mason | 239b14b | 2008-03-24 15:02:07 -0400 | [diff] [blame] | 9981 | .merge_bio_hook = btrfs_merge_bio_hook, |
Chris Mason | 07157aa | 2007-08-30 08:50:51 -0400 | [diff] [blame] | 9982 | .readpage_end_io_hook = btrfs_readpage_end_io_hook, |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 9983 | .writepage_end_io_hook = btrfs_writepage_end_io_hook, |
Chris Mason | 247e743 | 2008-07-17 12:53:51 -0400 | [diff] [blame] | 9984 | .writepage_start_hook = btrfs_writepage_start_hook, |
Chris Mason | b0c68f8 | 2008-01-31 11:05:37 -0500 | [diff] [blame] | 9985 | .set_bit_hook = btrfs_set_bit_hook, |
| 9986 | .clear_bit_hook = btrfs_clear_bit_hook, |
Josef Bacik | 9ed74f2 | 2009-09-11 16:12:44 -0400 | [diff] [blame] | 9987 | .merge_extent_hook = btrfs_merge_extent_hook, |
| 9988 | .split_extent_hook = btrfs_split_extent_hook, |
Chris Mason | 07157aa | 2007-08-30 08:50:51 -0400 | [diff] [blame] | 9989 | }; |
| 9990 | |
Chris Mason | 3505439 | 2009-01-21 13:11:13 -0500 | [diff] [blame] | 9991 | /* |
| 9992 | * btrfs doesn't support the bmap operation because swapfiles |
| 9993 | * use bmap to make a mapping of extents in the file. They assume |
| 9994 | * these extents won't change over the life of the file and they |
| 9995 | * use the bmap result to do IO directly to the drive. |
| 9996 | * |
| 9997 | * the btrfs bmap call would return logical addresses that aren't |
| 9998 | * suitable for IO and they also will change frequently as COW |
| 9999 | * operations happen. So, swapfile + btrfs == corruption. |
| 10000 | * |
| 10001 | * For now we're avoiding this by dropping bmap. |
| 10002 | */ |
Alexey Dobriyan | 7f09410 | 2009-09-21 17:01:10 -0700 | [diff] [blame] | 10003 | static const struct address_space_operations btrfs_aops = { |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 10004 | .readpage = btrfs_readpage, |
| 10005 | .writepage = btrfs_writepage, |
Chris Mason | b293f02e | 2007-11-01 19:45:34 -0400 | [diff] [blame] | 10006 | .writepages = btrfs_writepages, |
Chris Mason | 3ab2fb5 | 2007-11-08 10:59:22 -0500 | [diff] [blame] | 10007 | .readpages = btrfs_readpages, |
Chris Mason | 1643298 | 2008-04-10 10:23:21 -0400 | [diff] [blame] | 10008 | .direct_IO = btrfs_direct_IO, |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 10009 | .invalidatepage = btrfs_invalidatepage, |
| 10010 | .releasepage = btrfs_releasepage, |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 10011 | .set_page_dirty = btrfs_set_page_dirty, |
Andi Kleen | 465fdd9 | 2009-09-16 11:50:18 +0200 | [diff] [blame] | 10012 | .error_remove_page = generic_error_remove_page, |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 10013 | }; |
| 10014 | |
Alexey Dobriyan | 7f09410 | 2009-09-21 17:01:10 -0700 | [diff] [blame] | 10015 | static const struct address_space_operations btrfs_symlink_aops = { |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 10016 | .readpage = btrfs_readpage, |
| 10017 | .writepage = btrfs_writepage, |
Chris Mason | 2bf5a72 | 2007-08-30 11:54:02 -0400 | [diff] [blame] | 10018 | .invalidatepage = btrfs_invalidatepage, |
| 10019 | .releasepage = btrfs_releasepage, |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 10020 | }; |
| 10021 | |
Alexey Dobriyan | 6e1d5dc | 2009-09-21 17:01:11 -0700 | [diff] [blame] | 10022 | static const struct inode_operations btrfs_file_inode_operations = { |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 10023 | .getattr = btrfs_getattr, |
| 10024 | .setattr = btrfs_setattr, |
Christoph Hellwig | 95819c0 | 2008-08-28 06:21:17 -0400 | [diff] [blame] | 10025 | .setxattr = btrfs_setxattr, |
| 10026 | .getxattr = btrfs_getxattr, |
Josef Bacik | 5103e94 | 2007-11-16 11:45:54 -0500 | [diff] [blame] | 10027 | .listxattr = btrfs_listxattr, |
Christoph Hellwig | 95819c0 | 2008-08-28 06:21:17 -0400 | [diff] [blame] | 10028 | .removexattr = btrfs_removexattr, |
Yan | fdebe2b | 2008-01-14 13:26:08 -0500 | [diff] [blame] | 10029 | .permission = btrfs_permission, |
Yehuda Sadeh | 1506fcc | 2009-01-21 14:39:14 -0500 | [diff] [blame] | 10030 | .fiemap = btrfs_fiemap, |
Christoph Hellwig | 4e34e71 | 2011-07-23 17:37:31 +0200 | [diff] [blame] | 10031 | .get_acl = btrfs_get_acl, |
Christoph Hellwig | 996a710 | 2013-12-20 05:16:43 -0800 | [diff] [blame] | 10032 | .set_acl = btrfs_set_acl, |
Josef Bacik | e41f941 | 2012-03-26 09:46:47 -0400 | [diff] [blame] | 10033 | .update_time = btrfs_update_time, |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 10034 | }; |
Alexey Dobriyan | 6e1d5dc | 2009-09-21 17:01:11 -0700 | [diff] [blame] | 10035 | static const struct inode_operations btrfs_special_inode_operations = { |
Josef Bacik | 618e21d | 2007-07-11 10:18:17 -0400 | [diff] [blame] | 10036 | .getattr = btrfs_getattr, |
| 10037 | .setattr = btrfs_setattr, |
Yan | fdebe2b | 2008-01-14 13:26:08 -0500 | [diff] [blame] | 10038 | .permission = btrfs_permission, |
Christoph Hellwig | 95819c0 | 2008-08-28 06:21:17 -0400 | [diff] [blame] | 10039 | .setxattr = btrfs_setxattr, |
| 10040 | .getxattr = btrfs_getxattr, |
Josef Bacik | 33268ea | 2008-07-24 12:16:36 -0400 | [diff] [blame] | 10041 | .listxattr = btrfs_listxattr, |
Christoph Hellwig | 95819c0 | 2008-08-28 06:21:17 -0400 | [diff] [blame] | 10042 | .removexattr = btrfs_removexattr, |
Christoph Hellwig | 4e34e71 | 2011-07-23 17:37:31 +0200 | [diff] [blame] | 10043 | .get_acl = btrfs_get_acl, |
Christoph Hellwig | 996a710 | 2013-12-20 05:16:43 -0800 | [diff] [blame] | 10044 | .set_acl = btrfs_set_acl, |
Josef Bacik | e41f941 | 2012-03-26 09:46:47 -0400 | [diff] [blame] | 10045 | .update_time = btrfs_update_time, |
Josef Bacik | 618e21d | 2007-07-11 10:18:17 -0400 | [diff] [blame] | 10046 | }; |
Alexey Dobriyan | 6e1d5dc | 2009-09-21 17:01:11 -0700 | [diff] [blame] | 10047 | static const struct inode_operations btrfs_symlink_inode_operations = { |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 10048 | .readlink = generic_readlink, |
| 10049 | .follow_link = page_follow_link_light, |
| 10050 | .put_link = page_put_link, |
Li Zefan | f209561 | 2010-11-19 02:05:24 +0000 | [diff] [blame] | 10051 | .getattr = btrfs_getattr, |
Josef Bacik | 22c44fe | 2011-11-30 10:45:38 -0500 | [diff] [blame] | 10052 | .setattr = btrfs_setattr, |
Yan | fdebe2b | 2008-01-14 13:26:08 -0500 | [diff] [blame] | 10053 | .permission = btrfs_permission, |
Jim Owens | 0279b4c | 2009-02-04 09:29:13 -0500 | [diff] [blame] | 10054 | .setxattr = btrfs_setxattr, |
| 10055 | .getxattr = btrfs_getxattr, |
| 10056 | .listxattr = btrfs_listxattr, |
| 10057 | .removexattr = btrfs_removexattr, |
Josef Bacik | e41f941 | 2012-03-26 09:46:47 -0400 | [diff] [blame] | 10058 | .update_time = btrfs_update_time, |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 10059 | }; |
Yan, Zheng | 76dda93 | 2009-09-21 16:00:26 -0400 | [diff] [blame] | 10060 | |
Alexey Dobriyan | 82d339d | 2009-10-09 09:54:36 -0400 | [diff] [blame] | 10061 | const struct dentry_operations btrfs_dentry_operations = { |
Yan, Zheng | 76dda93 | 2009-09-21 16:00:26 -0400 | [diff] [blame] | 10062 | .d_delete = btrfs_dentry_delete, |
Josef Bacik | b4aff1f | 2011-06-28 16:18:59 -0400 | [diff] [blame] | 10063 | .d_release = btrfs_dentry_release, |
Yan, Zheng | 76dda93 | 2009-09-21 16:00:26 -0400 | [diff] [blame] | 10064 | }; |