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> |
Kent Overstreet | a27bb33 | 2013-05-07 16:19:08 -0700 | [diff] [blame] | 35 | #include <linux/aio.h> |
Chris Mason | 9ebefb18 | 2007-06-15 13:50:00 -0400 | [diff] [blame] | 36 | #include <linux/bit_spinlock.h> |
Josef Bacik | 5103e94 | 2007-11-16 11:45:54 -0500 | [diff] [blame] | 37 | #include <linux/xattr.h> |
Josef Bacik | 33268ea | 2008-07-24 12:16:36 -0400 | [diff] [blame] | 38 | #include <linux/posix_acl.h> |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 39 | #include <linux/falloc.h> |
Tejun Heo | 5a0e3ad | 2010-03-24 17:04:11 +0900 | [diff] [blame] | 40 | #include <linux/slab.h> |
David Sterba | 7a36dde | 2011-05-06 15:33:15 +0200 | [diff] [blame] | 41 | #include <linux/ratelimit.h> |
Josef Bacik | 22c44fe | 2011-11-30 10:45:38 -0500 | [diff] [blame] | 42 | #include <linux/mount.h> |
Filipe Brandenburger | 55e301f | 2013-01-29 06:04:50 +0000 | [diff] [blame] | 43 | #include <linux/btrfs.h> |
David Woodhouse | 53b381b | 2013-01-29 18:40:14 -0500 | [diff] [blame] | 44 | #include <linux/blkdev.h> |
Josef Bacik | f23b5a5 | 2013-06-19 10:16:26 -0400 | [diff] [blame] | 45 | #include <linux/posix_acl_xattr.h> |
Guenter Roeck | 4a3d1ca | 2015-03-13 01:58:46 -0700 | [diff] [blame] | 46 | #include <linux/uio.h> |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 47 | #include "ctree.h" |
| 48 | #include "disk-io.h" |
| 49 | #include "transaction.h" |
| 50 | #include "btrfs_inode.h" |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 51 | #include "print-tree.h" |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 52 | #include "ordered-data.h" |
Christoph Hellwig | 95819c0 | 2008-08-28 06:21:17 -0400 | [diff] [blame] | 53 | #include "xattr.h" |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 54 | #include "tree-log.h" |
Jan Schmidt | 4a54c8c | 2011-07-22 15:41:52 +0200 | [diff] [blame] | 55 | #include "volumes.h" |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 56 | #include "compression.h" |
Chris Mason | b4ce94d | 2009-02-04 09:25:08 -0500 | [diff] [blame] | 57 | #include "locking.h" |
Josef Bacik | dc89e98 | 2011-01-28 17:05:48 -0500 | [diff] [blame] | 58 | #include "free-space-cache.h" |
Li Zefan | 581bb05 | 2011-04-20 10:06:11 +0800 | [diff] [blame] | 59 | #include "inode-map.h" |
Liu Bo | 38c227d | 2013-01-29 03:18:40 +0000 | [diff] [blame] | 60 | #include "backref.h" |
Josef Bacik | f23b5a5 | 2013-06-19 10:16:26 -0400 | [diff] [blame] | 61 | #include "hash.h" |
Filipe David Borba Manana | 6354192 | 2014-01-07 11:47:46 +0000 | [diff] [blame] | 62 | #include "props.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 | b888db2b | 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 | b888db2b | 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 | } |
| 756 | |
Yan, Zheng | c216775 | 2009-11-12 09:34:21 +0000 | [diff] [blame] | 757 | /* |
| 758 | * here we're doing allocation and writeback of the |
| 759 | * compressed pages |
| 760 | */ |
| 761 | btrfs_drop_extent_cache(inode, async_extent->start, |
| 762 | async_extent->start + |
| 763 | async_extent->ram_size - 1, 0); |
| 764 | |
David Sterba | 172ddd6 | 2011-04-21 00:48:27 +0200 | [diff] [blame] | 765 | em = alloc_extent_map(); |
Liu Bo | b9aa55b | 2013-05-14 02:12:15 +0000 | [diff] [blame] | 766 | if (!em) { |
| 767 | ret = -ENOMEM; |
Josef Bacik | 3e04e7f | 2013-02-06 16:49:15 -0500 | [diff] [blame] | 768 | goto out_free_reserve; |
Liu Bo | b9aa55b | 2013-05-14 02:12:15 +0000 | [diff] [blame] | 769 | } |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 770 | em->start = async_extent->start; |
| 771 | em->len = async_extent->ram_size; |
Chris Mason | 445a694 | 2008-11-10 11:53:33 -0500 | [diff] [blame] | 772 | em->orig_start = em->start; |
Josef Bacik | 2ab28f3 | 2012-10-12 15:27:49 -0400 | [diff] [blame] | 773 | em->mod_start = em->start; |
| 774 | em->mod_len = em->len; |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 775 | |
| 776 | em->block_start = ins.objectid; |
| 777 | em->block_len = ins.offset; |
Josef Bacik | b493968 | 2012-12-03 10:31:19 -0500 | [diff] [blame] | 778 | em->orig_block_len = ins.offset; |
Josef Bacik | cc95bef | 2013-04-04 14:31:27 -0400 | [diff] [blame] | 779 | em->ram_bytes = async_extent->ram_size; |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 780 | em->bdev = root->fs_info->fs_devices->latest_bdev; |
Li Zefan | 261507a0 | 2010-12-17 14:21:50 +0800 | [diff] [blame] | 781 | em->compress_type = async_extent->compress_type; |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 782 | set_bit(EXTENT_FLAG_PINNED, &em->flags); |
| 783 | set_bit(EXTENT_FLAG_COMPRESSED, &em->flags); |
Josef Bacik | 70c8a91 | 2012-10-11 16:54:30 -0400 | [diff] [blame] | 784 | em->generation = -1; |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 785 | |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 786 | while (1) { |
Chris Mason | 890871b | 2009-09-02 16:24:52 -0400 | [diff] [blame] | 787 | write_lock(&em_tree->lock); |
Josef Bacik | 09a2a8f9 | 2013-04-05 16:51:15 -0400 | [diff] [blame] | 788 | ret = add_extent_mapping(em_tree, em, 1); |
Chris Mason | 890871b | 2009-09-02 16:24:52 -0400 | [diff] [blame] | 789 | write_unlock(&em_tree->lock); |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 790 | if (ret != -EEXIST) { |
| 791 | free_extent_map(em); |
| 792 | break; |
| 793 | } |
| 794 | btrfs_drop_extent_cache(inode, async_extent->start, |
| 795 | async_extent->start + |
| 796 | async_extent->ram_size - 1, 0); |
| 797 | } |
| 798 | |
Josef Bacik | 3e04e7f | 2013-02-06 16:49:15 -0500 | [diff] [blame] | 799 | if (ret) |
| 800 | goto out_free_reserve; |
| 801 | |
Li Zefan | 261507a0 | 2010-12-17 14:21:50 +0800 | [diff] [blame] | 802 | ret = btrfs_add_ordered_extent_compress(inode, |
| 803 | async_extent->start, |
| 804 | ins.objectid, |
| 805 | async_extent->ram_size, |
| 806 | ins.offset, |
| 807 | BTRFS_ORDERED_COMPRESSED, |
| 808 | async_extent->compress_type); |
Filipe Manana | d9f8596 | 2014-08-25 10:43:00 +0100 | [diff] [blame] | 809 | if (ret) { |
| 810 | btrfs_drop_extent_cache(inode, async_extent->start, |
| 811 | async_extent->start + |
| 812 | async_extent->ram_size - 1, 0); |
Josef Bacik | 3e04e7f | 2013-02-06 16:49:15 -0500 | [diff] [blame] | 813 | goto out_free_reserve; |
Filipe Manana | d9f8596 | 2014-08-25 10:43:00 +0100 | [diff] [blame] | 814 | } |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 815 | |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 816 | /* |
| 817 | * clear dirty, set writeback and unlock the pages. |
| 818 | */ |
Josef Bacik | c2790a2 | 2013-07-29 11:20:47 -0400 | [diff] [blame] | 819 | extent_clear_unlock_delalloc(inode, async_extent->start, |
Chris Mason | a791e35 | 2009-10-08 11:27:10 -0400 | [diff] [blame] | 820 | async_extent->start + |
| 821 | async_extent->ram_size - 1, |
Josef Bacik | 151a41b | 2013-07-29 13:22:24 -0400 | [diff] [blame] | 822 | NULL, EXTENT_LOCKED | EXTENT_DELALLOC, |
| 823 | PAGE_UNLOCK | PAGE_CLEAR_DIRTY | |
Josef Bacik | c2790a2 | 2013-07-29 11:20:47 -0400 | [diff] [blame] | 824 | PAGE_SET_WRITEBACK); |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 825 | ret = btrfs_submit_compressed_write(inode, |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 826 | async_extent->start, |
| 827 | async_extent->ram_size, |
| 828 | ins.objectid, |
| 829 | ins.offset, async_extent->pages, |
| 830 | async_extent->nr_pages); |
Filipe Manana | fce2a4e | 2014-10-06 22:14:23 +0100 | [diff] [blame] | 831 | if (ret) { |
| 832 | struct extent_io_tree *tree = &BTRFS_I(inode)->io_tree; |
| 833 | struct page *p = async_extent->pages[0]; |
| 834 | const u64 start = async_extent->start; |
| 835 | const u64 end = start + async_extent->ram_size - 1; |
| 836 | |
| 837 | p->mapping = inode->i_mapping; |
| 838 | tree->ops->writepage_end_io_hook(p, start, end, |
| 839 | NULL, 0); |
| 840 | p->mapping = NULL; |
| 841 | extent_clear_unlock_delalloc(inode, start, end, NULL, 0, |
| 842 | PAGE_END_WRITEBACK | |
| 843 | PAGE_SET_ERROR); |
Filipe Manana | 40ae837 | 2014-10-06 22:14:24 +0100 | [diff] [blame] | 844 | free_async_extent_pages(async_extent); |
Filipe Manana | fce2a4e | 2014-10-06 22:14:23 +0100 | [diff] [blame] | 845 | } |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 846 | alloc_hint = ins.objectid + ins.offset; |
| 847 | kfree(async_extent); |
| 848 | cond_resched(); |
| 849 | } |
Filipe Manana | dec8f17 | 2014-10-06 22:14:26 +0100 | [diff] [blame] | 850 | return; |
Josef Bacik | 3e04e7f | 2013-02-06 16:49:15 -0500 | [diff] [blame] | 851 | out_free_reserve: |
Miao Xie | e570fd2 | 2014-06-19 10:42:50 +0800 | [diff] [blame] | 852 | btrfs_free_reserved_extent(root, ins.objectid, ins.offset, 1); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 853 | out_free: |
Josef Bacik | c2790a2 | 2013-07-29 11:20:47 -0400 | [diff] [blame] | 854 | extent_clear_unlock_delalloc(inode, async_extent->start, |
Josef Bacik | 3e04e7f | 2013-02-06 16:49:15 -0500 | [diff] [blame] | 855 | async_extent->start + |
| 856 | async_extent->ram_size - 1, |
Josef Bacik | c2790a2 | 2013-07-29 11:20:47 -0400 | [diff] [blame] | 857 | NULL, EXTENT_LOCKED | EXTENT_DELALLOC | |
Josef Bacik | 151a41b | 2013-07-29 13:22:24 -0400 | [diff] [blame] | 858 | EXTENT_DEFRAG | EXTENT_DO_ACCOUNTING, |
| 859 | PAGE_UNLOCK | PAGE_CLEAR_DIRTY | |
Filipe Manana | 704de49 | 2014-10-06 22:14:22 +0100 | [diff] [blame] | 860 | PAGE_SET_WRITEBACK | PAGE_END_WRITEBACK | |
| 861 | PAGE_SET_ERROR); |
Filipe Manana | 40ae837 | 2014-10-06 22:14:24 +0100 | [diff] [blame] | 862 | free_async_extent_pages(async_extent); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 863 | kfree(async_extent); |
Josef Bacik | 3e04e7f | 2013-02-06 16:49:15 -0500 | [diff] [blame] | 864 | goto again; |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 865 | } |
| 866 | |
Josef Bacik | 4b46fce | 2010-05-23 11:00:55 -0400 | [diff] [blame] | 867 | static u64 get_extent_allocation_hint(struct inode *inode, u64 start, |
| 868 | u64 num_bytes) |
| 869 | { |
| 870 | struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree; |
| 871 | struct extent_map *em; |
| 872 | u64 alloc_hint = 0; |
| 873 | |
| 874 | read_lock(&em_tree->lock); |
| 875 | em = search_extent_mapping(em_tree, start, num_bytes); |
| 876 | if (em) { |
| 877 | /* |
| 878 | * if block start isn't an actual block number then find the |
| 879 | * first block in this inode and use that as a hint. If that |
| 880 | * block is also bogus then just don't worry about it. |
| 881 | */ |
| 882 | if (em->block_start >= EXTENT_MAP_LAST_BYTE) { |
| 883 | free_extent_map(em); |
| 884 | em = search_extent_mapping(em_tree, 0, 0); |
| 885 | if (em && em->block_start < EXTENT_MAP_LAST_BYTE) |
| 886 | alloc_hint = em->block_start; |
| 887 | if (em) |
| 888 | free_extent_map(em); |
| 889 | } else { |
| 890 | alloc_hint = em->block_start; |
| 891 | free_extent_map(em); |
| 892 | } |
| 893 | } |
| 894 | read_unlock(&em_tree->lock); |
| 895 | |
| 896 | return alloc_hint; |
| 897 | } |
| 898 | |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 899 | /* |
| 900 | * when extent_io.c finds a delayed allocation range in the file, |
| 901 | * the call backs end up in this code. The basic idea is to |
| 902 | * allocate extents on disk for the range, and create ordered data structs |
| 903 | * in ram to track those extents. |
| 904 | * |
| 905 | * locked_page is the page that writepage had locked already. We use |
| 906 | * it to make sure we don't do extra locks or unlocks. |
| 907 | * |
| 908 | * *page_started is set to one if we unlock locked_page and do everything |
| 909 | * required to start IO on it. It may be clean and already done with |
| 910 | * IO when we return. |
| 911 | */ |
Josef Bacik | 0036158 | 2013-08-14 14:02:47 -0400 | [diff] [blame] | 912 | static noinline int cow_file_range(struct inode *inode, |
| 913 | struct page *locked_page, |
| 914 | u64 start, u64 end, int *page_started, |
| 915 | unsigned long *nr_written, |
| 916 | int unlock) |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 917 | { |
Josef Bacik | 0036158 | 2013-08-14 14:02:47 -0400 | [diff] [blame] | 918 | struct btrfs_root *root = BTRFS_I(inode)->root; |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 919 | u64 alloc_hint = 0; |
| 920 | u64 num_bytes; |
| 921 | unsigned long ram_size; |
| 922 | u64 disk_num_bytes; |
| 923 | u64 cur_alloc_size; |
| 924 | u64 blocksize = root->sectorsize; |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 925 | struct btrfs_key ins; |
| 926 | struct extent_map *em; |
| 927 | struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree; |
| 928 | int ret = 0; |
| 929 | |
Josef Bacik | 02ecd2c | 2013-10-25 16:19:08 -0400 | [diff] [blame] | 930 | if (btrfs_is_free_space_inode(inode)) { |
| 931 | WARN_ON_ONCE(1); |
Josef Bacik | 29bce2f | 2014-02-07 12:21:23 -0500 | [diff] [blame] | 932 | ret = -EINVAL; |
| 933 | goto out_unlock; |
Josef Bacik | 02ecd2c | 2013-10-25 16:19:08 -0400 | [diff] [blame] | 934 | } |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 935 | |
Qu Wenruo | fda2832 | 2013-02-26 08:10:22 +0000 | [diff] [blame] | 936 | num_bytes = ALIGN(end - start + 1, blocksize); |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 937 | num_bytes = max(blocksize, num_bytes); |
| 938 | disk_num_bytes = num_bytes; |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 939 | |
Chris Mason | 4cb5300 | 2011-05-24 15:35:30 -0400 | [diff] [blame] | 940 | /* if this is a small write inside eof, kick off defrag */ |
Liu Bo | 4cb13e5 | 2012-03-29 09:57:45 -0400 | [diff] [blame] | 941 | if (num_bytes < 64 * 1024 && |
| 942 | (start > 0 || end + 1 < BTRFS_I(inode)->disk_i_size)) |
Josef Bacik | 0036158 | 2013-08-14 14:02:47 -0400 | [diff] [blame] | 943 | btrfs_add_inode_defrag(NULL, inode); |
Chris Mason | 4cb5300 | 2011-05-24 15:35:30 -0400 | [diff] [blame] | 944 | |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 945 | if (start == 0) { |
| 946 | /* lets try to make an inline extent */ |
Josef Bacik | 0036158 | 2013-08-14 14:02:47 -0400 | [diff] [blame] | 947 | ret = cow_file_range_inline(root, inode, start, end, 0, 0, |
| 948 | NULL); |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 949 | if (ret == 0) { |
Josef Bacik | c2790a2 | 2013-07-29 11:20:47 -0400 | [diff] [blame] | 950 | extent_clear_unlock_delalloc(inode, start, end, NULL, |
| 951 | EXTENT_LOCKED | EXTENT_DELALLOC | |
Josef Bacik | 151a41b | 2013-07-29 13:22:24 -0400 | [diff] [blame] | 952 | EXTENT_DEFRAG, PAGE_UNLOCK | |
Josef Bacik | c2790a2 | 2013-07-29 11:20:47 -0400 | [diff] [blame] | 953 | PAGE_CLEAR_DIRTY | PAGE_SET_WRITEBACK | |
| 954 | PAGE_END_WRITEBACK); |
Yan, Zheng | c216775 | 2009-11-12 09:34:21 +0000 | [diff] [blame] | 955 | |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 956 | *nr_written = *nr_written + |
| 957 | (end - start + PAGE_CACHE_SIZE) / PAGE_CACHE_SIZE; |
| 958 | *page_started = 1; |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 959 | goto out; |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 960 | } else if (ret < 0) { |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 961 | goto out_unlock; |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 962 | } |
| 963 | } |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 964 | |
| 965 | BUG_ON(disk_num_bytes > |
David Sterba | 6c41761 | 2011-04-13 15:41:04 +0200 | [diff] [blame] | 966 | btrfs_super_total_bytes(root->fs_info->super_copy)); |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 967 | |
Josef Bacik | 4b46fce | 2010-05-23 11:00:55 -0400 | [diff] [blame] | 968 | alloc_hint = get_extent_allocation_hint(inode, start, num_bytes); |
Zheng Yan | 5b21f2e | 2008-09-26 10:05:38 -0400 | [diff] [blame] | 969 | btrfs_drop_extent_cache(inode, start, start + num_bytes - 1, 0); |
Chris Mason | 3b95151 | 2008-04-17 11:29:12 -0400 | [diff] [blame] | 970 | |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 971 | while (disk_num_bytes > 0) { |
Chris Mason | a791e35 | 2009-10-08 11:27:10 -0400 | [diff] [blame] | 972 | unsigned long op; |
| 973 | |
Josef Bacik | 287a0ab | 2010-03-19 18:07:23 +0000 | [diff] [blame] | 974 | cur_alloc_size = disk_num_bytes; |
Josef Bacik | 0036158 | 2013-08-14 14:02:47 -0400 | [diff] [blame] | 975 | ret = btrfs_reserve_extent(root, cur_alloc_size, |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 976 | root->sectorsize, 0, alloc_hint, |
Miao Xie | e570fd2 | 2014-06-19 10:42:50 +0800 | [diff] [blame] | 977 | &ins, 1, 1); |
Josef Bacik | 0036158 | 2013-08-14 14:02:47 -0400 | [diff] [blame] | 978 | if (ret < 0) |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 979 | goto out_unlock; |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 980 | |
David Sterba | 172ddd6 | 2011-04-21 00:48:27 +0200 | [diff] [blame] | 981 | em = alloc_extent_map(); |
Liu Bo | b9aa55b | 2013-05-14 02:12:15 +0000 | [diff] [blame] | 982 | if (!em) { |
| 983 | ret = -ENOMEM; |
Liu Bo | ace68ba | 2013-04-22 10:53:47 +0000 | [diff] [blame] | 984 | goto out_reserve; |
Liu Bo | b9aa55b | 2013-05-14 02:12:15 +0000 | [diff] [blame] | 985 | } |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 986 | em->start = start; |
Chris Mason | 445a694 | 2008-11-10 11:53:33 -0500 | [diff] [blame] | 987 | em->orig_start = em->start; |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 988 | ram_size = ins.offset; |
| 989 | em->len = ins.offset; |
Josef Bacik | 2ab28f3 | 2012-10-12 15:27:49 -0400 | [diff] [blame] | 990 | em->mod_start = em->start; |
| 991 | em->mod_len = em->len; |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 992 | |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 993 | em->block_start = ins.objectid; |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 994 | em->block_len = ins.offset; |
Josef Bacik | b493968 | 2012-12-03 10:31:19 -0500 | [diff] [blame] | 995 | em->orig_block_len = ins.offset; |
Josef Bacik | cc95bef | 2013-04-04 14:31:27 -0400 | [diff] [blame] | 996 | em->ram_bytes = ram_size; |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 997 | em->bdev = root->fs_info->fs_devices->latest_bdev; |
Chris Mason | 7f3c74f | 2008-07-18 12:01:11 -0400 | [diff] [blame] | 998 | set_bit(EXTENT_FLAG_PINNED, &em->flags); |
Josef Bacik | 70c8a91 | 2012-10-11 16:54:30 -0400 | [diff] [blame] | 999 | em->generation = -1; |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 1000 | |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 1001 | while (1) { |
Chris Mason | 890871b | 2009-09-02 16:24:52 -0400 | [diff] [blame] | 1002 | write_lock(&em_tree->lock); |
Josef Bacik | 09a2a8f9 | 2013-04-05 16:51:15 -0400 | [diff] [blame] | 1003 | ret = add_extent_mapping(em_tree, em, 1); |
Chris Mason | 890871b | 2009-09-02 16:24:52 -0400 | [diff] [blame] | 1004 | write_unlock(&em_tree->lock); |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 1005 | if (ret != -EEXIST) { |
| 1006 | free_extent_map(em); |
| 1007 | break; |
| 1008 | } |
| 1009 | btrfs_drop_extent_cache(inode, start, |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 1010 | start + ram_size - 1, 0); |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 1011 | } |
Liu Bo | ace68ba | 2013-04-22 10:53:47 +0000 | [diff] [blame] | 1012 | if (ret) |
| 1013 | goto out_reserve; |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 1014 | |
Chris Mason | 98d20f6 | 2008-04-14 09:46:10 -0400 | [diff] [blame] | 1015 | cur_alloc_size = ins.offset; |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 1016 | ret = btrfs_add_ordered_extent(inode, start, ins.objectid, |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 1017 | ram_size, cur_alloc_size, 0); |
Liu Bo | ace68ba | 2013-04-22 10:53:47 +0000 | [diff] [blame] | 1018 | if (ret) |
Filipe Manana | d9f8596 | 2014-08-25 10:43:00 +0100 | [diff] [blame] | 1019 | goto out_drop_extent_cache; |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 1020 | |
Yan Zheng | 17d217f | 2008-12-12 10:03:38 -0500 | [diff] [blame] | 1021 | if (root->root_key.objectid == |
| 1022 | BTRFS_DATA_RELOC_TREE_OBJECTID) { |
| 1023 | ret = btrfs_reloc_clone_csums(inode, start, |
| 1024 | cur_alloc_size); |
Josef Bacik | 0036158 | 2013-08-14 14:02:47 -0400 | [diff] [blame] | 1025 | if (ret) |
Filipe Manana | d9f8596 | 2014-08-25 10:43:00 +0100 | [diff] [blame] | 1026 | goto out_drop_extent_cache; |
Yan Zheng | 17d217f | 2008-12-12 10:03:38 -0500 | [diff] [blame] | 1027 | } |
| 1028 | |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 1029 | if (disk_num_bytes < cur_alloc_size) |
Chris Mason | 3b95151 | 2008-04-17 11:29:12 -0400 | [diff] [blame] | 1030 | break; |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 1031 | |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 1032 | /* we're not doing compressed IO, don't unlock the first |
| 1033 | * page (which the caller expects to stay locked), don't |
| 1034 | * clear any dirty bits and don't set any writeback bits |
Chris Mason | 8b62b72 | 2009-09-02 16:53:46 -0400 | [diff] [blame] | 1035 | * |
| 1036 | * Do set the Private2 bit so we know this page was properly |
| 1037 | * setup for writepage |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 1038 | */ |
Josef Bacik | c2790a2 | 2013-07-29 11:20:47 -0400 | [diff] [blame] | 1039 | op = unlock ? PAGE_UNLOCK : 0; |
| 1040 | op |= PAGE_SET_PRIVATE2; |
Chris Mason | a791e35 | 2009-10-08 11:27:10 -0400 | [diff] [blame] | 1041 | |
Josef Bacik | c2790a2 | 2013-07-29 11:20:47 -0400 | [diff] [blame] | 1042 | extent_clear_unlock_delalloc(inode, start, |
| 1043 | start + ram_size - 1, locked_page, |
| 1044 | EXTENT_LOCKED | EXTENT_DELALLOC, |
| 1045 | op); |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 1046 | disk_num_bytes -= cur_alloc_size; |
Chris Mason | c59f895 | 2007-12-17 20:14:04 -0500 | [diff] [blame] | 1047 | num_bytes -= cur_alloc_size; |
| 1048 | alloc_hint = ins.objectid + ins.offset; |
| 1049 | start += cur_alloc_size; |
Chris Mason | b888db2b | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 1050 | } |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 1051 | out: |
Chris Mason | be20aa9 | 2007-12-17 20:14:01 -0500 | [diff] [blame] | 1052 | return ret; |
Miao Xie | b7d5b0a | 2012-11-01 07:32:18 +0000 | [diff] [blame] | 1053 | |
Filipe Manana | d9f8596 | 2014-08-25 10:43:00 +0100 | [diff] [blame] | 1054 | out_drop_extent_cache: |
| 1055 | btrfs_drop_extent_cache(inode, start, start + ram_size - 1, 0); |
Liu Bo | ace68ba | 2013-04-22 10:53:47 +0000 | [diff] [blame] | 1056 | out_reserve: |
Miao Xie | e570fd2 | 2014-06-19 10:42:50 +0800 | [diff] [blame] | 1057 | btrfs_free_reserved_extent(root, ins.objectid, ins.offset, 1); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 1058 | out_unlock: |
Josef Bacik | c2790a2 | 2013-07-29 11:20:47 -0400 | [diff] [blame] | 1059 | extent_clear_unlock_delalloc(inode, start, end, locked_page, |
Josef Bacik | 151a41b | 2013-07-29 13:22:24 -0400 | [diff] [blame] | 1060 | EXTENT_LOCKED | EXTENT_DO_ACCOUNTING | |
| 1061 | EXTENT_DELALLOC | EXTENT_DEFRAG, |
| 1062 | PAGE_UNLOCK | PAGE_CLEAR_DIRTY | |
| 1063 | PAGE_SET_WRITEBACK | PAGE_END_WRITEBACK); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 1064 | goto out; |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 1065 | } |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 1066 | |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 1067 | /* |
| 1068 | * work queue call back to started compression on a file and pages |
| 1069 | */ |
| 1070 | static noinline void async_cow_start(struct btrfs_work *work) |
| 1071 | { |
| 1072 | struct async_cow *async_cow; |
| 1073 | int num_added = 0; |
| 1074 | async_cow = container_of(work, struct async_cow, work); |
| 1075 | |
| 1076 | compress_file_range(async_cow->inode, async_cow->locked_page, |
| 1077 | async_cow->start, async_cow->end, async_cow, |
| 1078 | &num_added); |
Josef Bacik | 8180ef8 | 2012-06-08 15:16:12 -0400 | [diff] [blame] | 1079 | if (num_added == 0) { |
Josef Bacik | cb77fcd | 2012-06-15 12:19:48 -0600 | [diff] [blame] | 1080 | btrfs_add_delayed_iput(async_cow->inode); |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 1081 | async_cow->inode = NULL; |
Josef Bacik | 8180ef8 | 2012-06-08 15:16:12 -0400 | [diff] [blame] | 1082 | } |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 1083 | } |
| 1084 | |
| 1085 | /* |
| 1086 | * work queue call back to submit previously compressed pages |
| 1087 | */ |
| 1088 | static noinline void async_cow_submit(struct btrfs_work *work) |
| 1089 | { |
| 1090 | struct async_cow *async_cow; |
| 1091 | struct btrfs_root *root; |
| 1092 | unsigned long nr_pages; |
| 1093 | |
| 1094 | async_cow = container_of(work, struct async_cow, work); |
| 1095 | |
| 1096 | root = async_cow->root; |
| 1097 | nr_pages = (async_cow->end - async_cow->start + PAGE_CACHE_SIZE) >> |
| 1098 | PAGE_CACHE_SHIFT; |
| 1099 | |
Josef Bacik | 66657b3 | 2012-08-01 15:36:24 -0400 | [diff] [blame] | 1100 | if (atomic_sub_return(nr_pages, &root->fs_info->async_delalloc_pages) < |
Liu Bo | 287082b | 2012-06-28 04:02:24 -0600 | [diff] [blame] | 1101 | 5 * 1024 * 1024 && |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 1102 | waitqueue_active(&root->fs_info->async_submit_wait)) |
| 1103 | wake_up(&root->fs_info->async_submit_wait); |
| 1104 | |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 1105 | if (async_cow->inode) |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 1106 | submit_compressed_extents(async_cow->inode, async_cow); |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 1107 | } |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 1108 | |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 1109 | static noinline void async_cow_free(struct btrfs_work *work) |
| 1110 | { |
| 1111 | struct async_cow *async_cow; |
| 1112 | async_cow = container_of(work, struct async_cow, work); |
Josef Bacik | 8180ef8 | 2012-06-08 15:16:12 -0400 | [diff] [blame] | 1113 | if (async_cow->inode) |
Josef Bacik | cb77fcd | 2012-06-15 12:19:48 -0600 | [diff] [blame] | 1114 | btrfs_add_delayed_iput(async_cow->inode); |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 1115 | kfree(async_cow); |
| 1116 | } |
| 1117 | |
| 1118 | static int cow_file_range_async(struct inode *inode, struct page *locked_page, |
| 1119 | u64 start, u64 end, int *page_started, |
| 1120 | unsigned long *nr_written) |
| 1121 | { |
| 1122 | struct async_cow *async_cow; |
| 1123 | struct btrfs_root *root = BTRFS_I(inode)->root; |
| 1124 | unsigned long nr_pages; |
| 1125 | u64 cur_end; |
Liu Bo | 287082b | 2012-06-28 04:02:24 -0600 | [diff] [blame] | 1126 | int limit = 10 * 1024 * 1024; |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 1127 | |
Chris Mason | a3429ab | 2009-10-08 12:30:20 -0400 | [diff] [blame] | 1128 | clear_extent_bit(&BTRFS_I(inode)->io_tree, start, end, EXTENT_LOCKED, |
| 1129 | 1, 0, NULL, GFP_NOFS); |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 1130 | while (start < end) { |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 1131 | async_cow = kmalloc(sizeof(*async_cow), GFP_NOFS); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 1132 | BUG_ON(!async_cow); /* -ENOMEM */ |
Josef Bacik | 8180ef8 | 2012-06-08 15:16:12 -0400 | [diff] [blame] | 1133 | async_cow->inode = igrab(inode); |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 1134 | async_cow->root = root; |
| 1135 | async_cow->locked_page = locked_page; |
| 1136 | async_cow->start = start; |
| 1137 | |
Wang Shilong | f79707b | 2014-07-17 11:44:09 +0800 | [diff] [blame] | 1138 | if (BTRFS_I(inode)->flags & BTRFS_INODE_NOCOMPRESS && |
| 1139 | !btrfs_test_opt(root, FORCE_COMPRESS)) |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 1140 | cur_end = end; |
| 1141 | else |
| 1142 | cur_end = min(end, start + 512 * 1024 - 1); |
| 1143 | |
| 1144 | async_cow->end = cur_end; |
| 1145 | INIT_LIST_HEAD(&async_cow->extents); |
| 1146 | |
Liu Bo | 9e0af23 | 2014-08-15 23:36:53 +0800 | [diff] [blame] | 1147 | btrfs_init_work(&async_cow->work, |
| 1148 | btrfs_delalloc_helper, |
| 1149 | async_cow_start, async_cow_submit, |
| 1150 | async_cow_free); |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 1151 | |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 1152 | nr_pages = (cur_end - start + PAGE_CACHE_SIZE) >> |
| 1153 | PAGE_CACHE_SHIFT; |
| 1154 | atomic_add(nr_pages, &root->fs_info->async_delalloc_pages); |
| 1155 | |
Qu Wenruo | afe3d24 | 2014-02-28 10:46:07 +0800 | [diff] [blame] | 1156 | btrfs_queue_work(root->fs_info->delalloc_workers, |
| 1157 | &async_cow->work); |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 1158 | |
| 1159 | if (atomic_read(&root->fs_info->async_delalloc_pages) > limit) { |
| 1160 | wait_event(root->fs_info->async_submit_wait, |
| 1161 | (atomic_read(&root->fs_info->async_delalloc_pages) < |
| 1162 | limit)); |
| 1163 | } |
| 1164 | |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 1165 | while (atomic_read(&root->fs_info->async_submit_draining) && |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 1166 | atomic_read(&root->fs_info->async_delalloc_pages)) { |
| 1167 | wait_event(root->fs_info->async_submit_wait, |
| 1168 | (atomic_read(&root->fs_info->async_delalloc_pages) == |
| 1169 | 0)); |
| 1170 | } |
| 1171 | |
| 1172 | *nr_written += nr_pages; |
| 1173 | start = cur_end + 1; |
| 1174 | } |
| 1175 | *page_started = 1; |
| 1176 | return 0; |
Chris Mason | be20aa9 | 2007-12-17 20:14:01 -0500 | [diff] [blame] | 1177 | } |
| 1178 | |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 1179 | static noinline int csum_exist_in_range(struct btrfs_root *root, |
Yan Zheng | 17d217f | 2008-12-12 10:03:38 -0500 | [diff] [blame] | 1180 | u64 bytenr, u64 num_bytes) |
| 1181 | { |
| 1182 | int ret; |
| 1183 | struct btrfs_ordered_sum *sums; |
| 1184 | LIST_HEAD(list); |
| 1185 | |
Yan Zheng | 07d400a | 2009-01-06 11:42:00 -0500 | [diff] [blame] | 1186 | ret = btrfs_lookup_csums_range(root->fs_info->csum_root, bytenr, |
Arne Jansen | a2de733 | 2011-03-08 14:14:00 +0100 | [diff] [blame] | 1187 | bytenr + num_bytes - 1, &list, 0); |
Yan Zheng | 17d217f | 2008-12-12 10:03:38 -0500 | [diff] [blame] | 1188 | if (ret == 0 && list_empty(&list)) |
| 1189 | return 0; |
| 1190 | |
| 1191 | while (!list_empty(&list)) { |
| 1192 | sums = list_entry(list.next, struct btrfs_ordered_sum, list); |
| 1193 | list_del(&sums->list); |
| 1194 | kfree(sums); |
| 1195 | } |
| 1196 | return 1; |
| 1197 | } |
| 1198 | |
Chris Mason | d352ac6 | 2008-09-29 15:18:18 -0400 | [diff] [blame] | 1199 | /* |
| 1200 | * when nowcow writeback call back. This checks for snapshots or COW copies |
| 1201 | * of the extents that exist in the file, and COWs the file as required. |
| 1202 | * |
| 1203 | * If no cow copies or snapshots exist, we write directly to the existing |
| 1204 | * blocks on disk |
| 1205 | */ |
Chris Mason | 7f366cf | 2009-03-12 20:12:45 -0400 | [diff] [blame] | 1206 | static noinline int run_delalloc_nocow(struct inode *inode, |
| 1207 | struct page *locked_page, |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 1208 | u64 start, u64 end, int *page_started, int force, |
| 1209 | unsigned long *nr_written) |
Chris Mason | be20aa9 | 2007-12-17 20:14:01 -0500 | [diff] [blame] | 1210 | { |
Chris Mason | be20aa9 | 2007-12-17 20:14:01 -0500 | [diff] [blame] | 1211 | struct btrfs_root *root = BTRFS_I(inode)->root; |
Yan Zheng | 7ea394f | 2008-08-05 13:05:02 -0400 | [diff] [blame] | 1212 | struct btrfs_trans_handle *trans; |
Chris Mason | be20aa9 | 2007-12-17 20:14:01 -0500 | [diff] [blame] | 1213 | struct extent_buffer *leaf; |
Chris Mason | be20aa9 | 2007-12-17 20:14:01 -0500 | [diff] [blame] | 1214 | struct btrfs_path *path; |
Yan Zheng | 80ff385 | 2008-10-30 14:20:02 -0400 | [diff] [blame] | 1215 | struct btrfs_file_extent_item *fi; |
Chris Mason | be20aa9 | 2007-12-17 20:14:01 -0500 | [diff] [blame] | 1216 | struct btrfs_key found_key; |
Yan Zheng | 80ff385 | 2008-10-30 14:20:02 -0400 | [diff] [blame] | 1217 | u64 cow_start; |
| 1218 | u64 cur_offset; |
| 1219 | u64 extent_end; |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 1220 | u64 extent_offset; |
Yan Zheng | 80ff385 | 2008-10-30 14:20:02 -0400 | [diff] [blame] | 1221 | u64 disk_bytenr; |
| 1222 | u64 num_bytes; |
Josef Bacik | b493968 | 2012-12-03 10:31:19 -0500 | [diff] [blame] | 1223 | u64 disk_num_bytes; |
Josef Bacik | cc95bef | 2013-04-04 14:31:27 -0400 | [diff] [blame] | 1224 | u64 ram_bytes; |
Yan Zheng | 80ff385 | 2008-10-30 14:20:02 -0400 | [diff] [blame] | 1225 | int extent_type; |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 1226 | int ret, err; |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 1227 | int type; |
Yan Zheng | 80ff385 | 2008-10-30 14:20:02 -0400 | [diff] [blame] | 1228 | int nocow; |
| 1229 | int check_prev = 1; |
Li Zefan | 82d5902 | 2011-04-20 10:33:24 +0800 | [diff] [blame] | 1230 | bool nolock; |
Li Zefan | 33345d01 | 2011-04-20 10:31:50 +0800 | [diff] [blame] | 1231 | u64 ino = btrfs_ino(inode); |
Chris Mason | be20aa9 | 2007-12-17 20:14:01 -0500 | [diff] [blame] | 1232 | |
| 1233 | path = btrfs_alloc_path(); |
Josef Bacik | 17ca04a | 2012-05-31 15:58:55 -0400 | [diff] [blame] | 1234 | if (!path) { |
Josef Bacik | c2790a2 | 2013-07-29 11:20:47 -0400 | [diff] [blame] | 1235 | extent_clear_unlock_delalloc(inode, start, end, locked_page, |
| 1236 | EXTENT_LOCKED | EXTENT_DELALLOC | |
Josef Bacik | 151a41b | 2013-07-29 13:22:24 -0400 | [diff] [blame] | 1237 | EXTENT_DO_ACCOUNTING | |
| 1238 | EXTENT_DEFRAG, PAGE_UNLOCK | |
Josef Bacik | c2790a2 | 2013-07-29 11:20:47 -0400 | [diff] [blame] | 1239 | PAGE_CLEAR_DIRTY | |
| 1240 | PAGE_SET_WRITEBACK | |
| 1241 | PAGE_END_WRITEBACK); |
Mark Fasheh | d8926bb | 2011-07-13 10:38:47 -0700 | [diff] [blame] | 1242 | return -ENOMEM; |
Josef Bacik | 17ca04a | 2012-05-31 15:58:55 -0400 | [diff] [blame] | 1243 | } |
Li Zefan | 82d5902 | 2011-04-20 10:33:24 +0800 | [diff] [blame] | 1244 | |
Liu Bo | 83eea1f | 2012-07-10 05:28:39 -0600 | [diff] [blame] | 1245 | nolock = btrfs_is_free_space_inode(inode); |
Li Zefan | 82d5902 | 2011-04-20 10:33:24 +0800 | [diff] [blame] | 1246 | |
| 1247 | if (nolock) |
Josef Bacik | 7a7eaa4 | 2011-04-13 12:54:33 -0400 | [diff] [blame] | 1248 | trans = btrfs_join_transaction_nolock(root); |
Li Zefan | 82d5902 | 2011-04-20 10:33:24 +0800 | [diff] [blame] | 1249 | else |
Josef Bacik | 7a7eaa4 | 2011-04-13 12:54:33 -0400 | [diff] [blame] | 1250 | trans = btrfs_join_transaction(root); |
Chris Mason | ff5714c | 2011-05-28 07:00:39 -0400 | [diff] [blame] | 1251 | |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 1252 | if (IS_ERR(trans)) { |
Josef Bacik | c2790a2 | 2013-07-29 11:20:47 -0400 | [diff] [blame] | 1253 | extent_clear_unlock_delalloc(inode, start, end, locked_page, |
| 1254 | EXTENT_LOCKED | EXTENT_DELALLOC | |
Josef Bacik | 151a41b | 2013-07-29 13:22:24 -0400 | [diff] [blame] | 1255 | EXTENT_DO_ACCOUNTING | |
| 1256 | EXTENT_DEFRAG, PAGE_UNLOCK | |
Josef Bacik | c2790a2 | 2013-07-29 11:20:47 -0400 | [diff] [blame] | 1257 | PAGE_CLEAR_DIRTY | |
| 1258 | PAGE_SET_WRITEBACK | |
| 1259 | PAGE_END_WRITEBACK); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 1260 | btrfs_free_path(path); |
| 1261 | return PTR_ERR(trans); |
| 1262 | } |
| 1263 | |
Josef Bacik | 74b2107 | 2011-04-13 12:02:53 -0400 | [diff] [blame] | 1264 | trans->block_rsv = &root->fs_info->delalloc_block_rsv; |
Chris Mason | be20aa9 | 2007-12-17 20:14:01 -0500 | [diff] [blame] | 1265 | |
Yan Zheng | 80ff385 | 2008-10-30 14:20:02 -0400 | [diff] [blame] | 1266 | cow_start = (u64)-1; |
| 1267 | cur_offset = start; |
| 1268 | while (1) { |
Li Zefan | 33345d01 | 2011-04-20 10:31:50 +0800 | [diff] [blame] | 1269 | ret = btrfs_lookup_file_extent(trans, root, path, ino, |
Yan Zheng | 80ff385 | 2008-10-30 14:20:02 -0400 | [diff] [blame] | 1270 | cur_offset, 0); |
Josef Bacik | d788a34 | 2013-10-25 16:55:08 -0400 | [diff] [blame] | 1271 | if (ret < 0) |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 1272 | goto error; |
Yan Zheng | 80ff385 | 2008-10-30 14:20:02 -0400 | [diff] [blame] | 1273 | if (ret > 0 && path->slots[0] > 0 && check_prev) { |
| 1274 | leaf = path->nodes[0]; |
| 1275 | btrfs_item_key_to_cpu(leaf, &found_key, |
| 1276 | path->slots[0] - 1); |
Li Zefan | 33345d01 | 2011-04-20 10:31:50 +0800 | [diff] [blame] | 1277 | if (found_key.objectid == ino && |
Yan Zheng | 80ff385 | 2008-10-30 14:20:02 -0400 | [diff] [blame] | 1278 | found_key.type == BTRFS_EXTENT_DATA_KEY) |
| 1279 | path->slots[0]--; |
| 1280 | } |
| 1281 | check_prev = 0; |
| 1282 | next_slot: |
| 1283 | leaf = path->nodes[0]; |
| 1284 | if (path->slots[0] >= btrfs_header_nritems(leaf)) { |
| 1285 | ret = btrfs_next_leaf(root, path); |
Josef Bacik | d788a34 | 2013-10-25 16:55:08 -0400 | [diff] [blame] | 1286 | if (ret < 0) |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 1287 | goto error; |
Yan Zheng | 80ff385 | 2008-10-30 14:20:02 -0400 | [diff] [blame] | 1288 | if (ret > 0) |
| 1289 | break; |
| 1290 | leaf = path->nodes[0]; |
| 1291 | } |
Chris Mason | be20aa9 | 2007-12-17 20:14:01 -0500 | [diff] [blame] | 1292 | |
Yan Zheng | 80ff385 | 2008-10-30 14:20:02 -0400 | [diff] [blame] | 1293 | nocow = 0; |
| 1294 | disk_bytenr = 0; |
Yan Zheng | 17d217f | 2008-12-12 10:03:38 -0500 | [diff] [blame] | 1295 | num_bytes = 0; |
Yan Zheng | 80ff385 | 2008-10-30 14:20:02 -0400 | [diff] [blame] | 1296 | btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]); |
Chris Mason | be20aa9 | 2007-12-17 20:14:01 -0500 | [diff] [blame] | 1297 | |
Li Zefan | 33345d01 | 2011-04-20 10:31:50 +0800 | [diff] [blame] | 1298 | if (found_key.objectid > ino || |
Yan Zheng | 80ff385 | 2008-10-30 14:20:02 -0400 | [diff] [blame] | 1299 | found_key.type > BTRFS_EXTENT_DATA_KEY || |
| 1300 | found_key.offset > end) |
| 1301 | break; |
Chris Mason | be20aa9 | 2007-12-17 20:14:01 -0500 | [diff] [blame] | 1302 | |
Yan Zheng | 80ff385 | 2008-10-30 14:20:02 -0400 | [diff] [blame] | 1303 | if (found_key.offset > cur_offset) { |
| 1304 | extent_end = found_key.offset; |
Chris Mason | e9061e2 | 2009-10-09 09:57:45 -0400 | [diff] [blame] | 1305 | extent_type = 0; |
Yan Zheng | 80ff385 | 2008-10-30 14:20:02 -0400 | [diff] [blame] | 1306 | goto out_check; |
| 1307 | } |
Chris Mason | c31f883 | 2008-01-08 15:46:31 -0500 | [diff] [blame] | 1308 | |
Yan Zheng | 80ff385 | 2008-10-30 14:20:02 -0400 | [diff] [blame] | 1309 | fi = btrfs_item_ptr(leaf, path->slots[0], |
| 1310 | struct btrfs_file_extent_item); |
| 1311 | extent_type = btrfs_file_extent_type(leaf, fi); |
Chris Mason | be20aa9 | 2007-12-17 20:14:01 -0500 | [diff] [blame] | 1312 | |
Josef Bacik | cc95bef | 2013-04-04 14:31:27 -0400 | [diff] [blame] | 1313 | ram_bytes = btrfs_file_extent_ram_bytes(leaf, fi); |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 1314 | if (extent_type == BTRFS_FILE_EXTENT_REG || |
| 1315 | extent_type == BTRFS_FILE_EXTENT_PREALLOC) { |
Yan Zheng | 80ff385 | 2008-10-30 14:20:02 -0400 | [diff] [blame] | 1316 | disk_bytenr = btrfs_file_extent_disk_bytenr(leaf, fi); |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 1317 | extent_offset = btrfs_file_extent_offset(leaf, fi); |
Yan Zheng | 80ff385 | 2008-10-30 14:20:02 -0400 | [diff] [blame] | 1318 | extent_end = found_key.offset + |
| 1319 | btrfs_file_extent_num_bytes(leaf, fi); |
Josef Bacik | b493968 | 2012-12-03 10:31:19 -0500 | [diff] [blame] | 1320 | disk_num_bytes = |
| 1321 | btrfs_file_extent_disk_num_bytes(leaf, fi); |
Yan Zheng | 80ff385 | 2008-10-30 14:20:02 -0400 | [diff] [blame] | 1322 | if (extent_end <= start) { |
| 1323 | path->slots[0]++; |
| 1324 | goto next_slot; |
| 1325 | } |
Yan Zheng | 17d217f | 2008-12-12 10:03:38 -0500 | [diff] [blame] | 1326 | if (disk_bytenr == 0) |
| 1327 | goto out_check; |
Yan Zheng | 80ff385 | 2008-10-30 14:20:02 -0400 | [diff] [blame] | 1328 | if (btrfs_file_extent_compression(leaf, fi) || |
| 1329 | btrfs_file_extent_encryption(leaf, fi) || |
| 1330 | btrfs_file_extent_other_encoding(leaf, fi)) |
| 1331 | goto out_check; |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 1332 | if (extent_type == BTRFS_FILE_EXTENT_REG && !force) |
| 1333 | goto out_check; |
Yan Zheng | d2fb343 | 2008-12-11 16:30:39 -0500 | [diff] [blame] | 1334 | if (btrfs_extent_readonly(root, disk_bytenr)) |
Yan Zheng | 80ff385 | 2008-10-30 14:20:02 -0400 | [diff] [blame] | 1335 | goto out_check; |
Li Zefan | 33345d01 | 2011-04-20 10:31:50 +0800 | [diff] [blame] | 1336 | if (btrfs_cross_ref_exist(trans, root, ino, |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 1337 | found_key.offset - |
| 1338 | extent_offset, disk_bytenr)) |
Yan Zheng | 17d217f | 2008-12-12 10:03:38 -0500 | [diff] [blame] | 1339 | goto out_check; |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 1340 | disk_bytenr += extent_offset; |
Yan Zheng | 17d217f | 2008-12-12 10:03:38 -0500 | [diff] [blame] | 1341 | disk_bytenr += cur_offset - found_key.offset; |
| 1342 | num_bytes = min(end + 1, extent_end) - cur_offset; |
| 1343 | /* |
Wang Shilong | e9894fd | 2014-03-27 11:12:25 +0800 | [diff] [blame] | 1344 | * if there are pending snapshots for this root, |
| 1345 | * we fall into common COW way. |
| 1346 | */ |
| 1347 | if (!nolock) { |
Filipe Manana | 9ea24bb | 2014-10-29 11:57:59 +0000 | [diff] [blame] | 1348 | err = btrfs_start_write_no_snapshoting(root); |
Wang Shilong | e9894fd | 2014-03-27 11:12:25 +0800 | [diff] [blame] | 1349 | if (!err) |
| 1350 | goto out_check; |
| 1351 | } |
| 1352 | /* |
Yan Zheng | 17d217f | 2008-12-12 10:03:38 -0500 | [diff] [blame] | 1353 | * force cow if csum exists in the range. |
| 1354 | * this ensure that csum for a given extent are |
| 1355 | * either valid or do not exist. |
| 1356 | */ |
| 1357 | if (csum_exist_in_range(root, disk_bytenr, num_bytes)) |
| 1358 | goto out_check; |
Yan Zheng | 80ff385 | 2008-10-30 14:20:02 -0400 | [diff] [blame] | 1359 | nocow = 1; |
| 1360 | } else if (extent_type == BTRFS_FILE_EXTENT_INLINE) { |
| 1361 | extent_end = found_key.offset + |
Chris Mason | 514ac8a | 2014-01-03 21:07:00 -0800 | [diff] [blame] | 1362 | btrfs_file_extent_inline_len(leaf, |
| 1363 | path->slots[0], fi); |
Yan Zheng | 80ff385 | 2008-10-30 14:20:02 -0400 | [diff] [blame] | 1364 | extent_end = ALIGN(extent_end, root->sectorsize); |
| 1365 | } else { |
| 1366 | BUG_ON(1); |
| 1367 | } |
| 1368 | out_check: |
| 1369 | if (extent_end <= start) { |
| 1370 | path->slots[0]++; |
Wang Shilong | e9894fd | 2014-03-27 11:12:25 +0800 | [diff] [blame] | 1371 | if (!nolock && nocow) |
Filipe Manana | 9ea24bb | 2014-10-29 11:57:59 +0000 | [diff] [blame] | 1372 | btrfs_end_write_no_snapshoting(root); |
Yan Zheng | 80ff385 | 2008-10-30 14:20:02 -0400 | [diff] [blame] | 1373 | goto next_slot; |
| 1374 | } |
| 1375 | if (!nocow) { |
| 1376 | if (cow_start == (u64)-1) |
| 1377 | cow_start = cur_offset; |
| 1378 | cur_offset = extent_end; |
| 1379 | if (cur_offset > end) |
| 1380 | break; |
| 1381 | path->slots[0]++; |
| 1382 | goto next_slot; |
Yan Zheng | 7ea394f | 2008-08-05 13:05:02 -0400 | [diff] [blame] | 1383 | } |
Chris Mason | be20aa9 | 2007-12-17 20:14:01 -0500 | [diff] [blame] | 1384 | |
David Sterba | b3b4aa7 | 2011-04-21 01:20:15 +0200 | [diff] [blame] | 1385 | btrfs_release_path(path); |
Yan Zheng | 80ff385 | 2008-10-30 14:20:02 -0400 | [diff] [blame] | 1386 | if (cow_start != (u64)-1) { |
Josef Bacik | 0036158 | 2013-08-14 14:02:47 -0400 | [diff] [blame] | 1387 | ret = cow_file_range(inode, locked_page, |
| 1388 | cow_start, found_key.offset - 1, |
| 1389 | page_started, nr_written, 1); |
Wang Shilong | e9894fd | 2014-03-27 11:12:25 +0800 | [diff] [blame] | 1390 | if (ret) { |
| 1391 | if (!nolock && nocow) |
Filipe Manana | 9ea24bb | 2014-10-29 11:57:59 +0000 | [diff] [blame] | 1392 | btrfs_end_write_no_snapshoting(root); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 1393 | goto error; |
Wang Shilong | e9894fd | 2014-03-27 11:12:25 +0800 | [diff] [blame] | 1394 | } |
Yan Zheng | 80ff385 | 2008-10-30 14:20:02 -0400 | [diff] [blame] | 1395 | cow_start = (u64)-1; |
Yan Zheng | 7ea394f | 2008-08-05 13:05:02 -0400 | [diff] [blame] | 1396 | } |
Yan Zheng | 80ff385 | 2008-10-30 14:20:02 -0400 | [diff] [blame] | 1397 | |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 1398 | if (extent_type == BTRFS_FILE_EXTENT_PREALLOC) { |
| 1399 | struct extent_map *em; |
| 1400 | struct extent_map_tree *em_tree; |
| 1401 | em_tree = &BTRFS_I(inode)->extent_tree; |
David Sterba | 172ddd6 | 2011-04-21 00:48:27 +0200 | [diff] [blame] | 1402 | em = alloc_extent_map(); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 1403 | BUG_ON(!em); /* -ENOMEM */ |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 1404 | em->start = cur_offset; |
Josef Bacik | 70c8a91 | 2012-10-11 16:54:30 -0400 | [diff] [blame] | 1405 | em->orig_start = found_key.offset - extent_offset; |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 1406 | em->len = num_bytes; |
| 1407 | em->block_len = num_bytes; |
| 1408 | em->block_start = disk_bytenr; |
Josef Bacik | b493968 | 2012-12-03 10:31:19 -0500 | [diff] [blame] | 1409 | em->orig_block_len = disk_num_bytes; |
Josef Bacik | cc95bef | 2013-04-04 14:31:27 -0400 | [diff] [blame] | 1410 | em->ram_bytes = ram_bytes; |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 1411 | em->bdev = root->fs_info->fs_devices->latest_bdev; |
Josef Bacik | 2ab28f3 | 2012-10-12 15:27:49 -0400 | [diff] [blame] | 1412 | em->mod_start = em->start; |
| 1413 | em->mod_len = em->len; |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 1414 | set_bit(EXTENT_FLAG_PINNED, &em->flags); |
Josef Bacik | b11e234 | 2012-12-03 10:58:15 -0500 | [diff] [blame] | 1415 | set_bit(EXTENT_FLAG_FILLING, &em->flags); |
Josef Bacik | 70c8a91 | 2012-10-11 16:54:30 -0400 | [diff] [blame] | 1416 | em->generation = -1; |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 1417 | while (1) { |
Chris Mason | 890871b | 2009-09-02 16:24:52 -0400 | [diff] [blame] | 1418 | write_lock(&em_tree->lock); |
Josef Bacik | 09a2a8f9 | 2013-04-05 16:51:15 -0400 | [diff] [blame] | 1419 | ret = add_extent_mapping(em_tree, em, 1); |
Chris Mason | 890871b | 2009-09-02 16:24:52 -0400 | [diff] [blame] | 1420 | write_unlock(&em_tree->lock); |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 1421 | if (ret != -EEXIST) { |
| 1422 | free_extent_map(em); |
| 1423 | break; |
| 1424 | } |
| 1425 | btrfs_drop_extent_cache(inode, em->start, |
| 1426 | em->start + em->len - 1, 0); |
| 1427 | } |
| 1428 | type = BTRFS_ORDERED_PREALLOC; |
| 1429 | } else { |
| 1430 | type = BTRFS_ORDERED_NOCOW; |
| 1431 | } |
Yan Zheng | 80ff385 | 2008-10-30 14:20:02 -0400 | [diff] [blame] | 1432 | |
| 1433 | ret = btrfs_add_ordered_extent(inode, cur_offset, disk_bytenr, |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 1434 | num_bytes, num_bytes, type); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 1435 | BUG_ON(ret); /* -ENOMEM */ |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 1436 | |
Yan, Zheng | efa5646 | 2010-05-16 10:49:59 -0400 | [diff] [blame] | 1437 | if (root->root_key.objectid == |
| 1438 | BTRFS_DATA_RELOC_TREE_OBJECTID) { |
| 1439 | ret = btrfs_reloc_clone_csums(inode, cur_offset, |
| 1440 | num_bytes); |
Wang Shilong | e9894fd | 2014-03-27 11:12:25 +0800 | [diff] [blame] | 1441 | if (ret) { |
| 1442 | if (!nolock && nocow) |
Filipe Manana | 9ea24bb | 2014-10-29 11:57:59 +0000 | [diff] [blame] | 1443 | btrfs_end_write_no_snapshoting(root); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 1444 | goto error; |
Wang Shilong | e9894fd | 2014-03-27 11:12:25 +0800 | [diff] [blame] | 1445 | } |
Yan, Zheng | efa5646 | 2010-05-16 10:49:59 -0400 | [diff] [blame] | 1446 | } |
| 1447 | |
Josef Bacik | c2790a2 | 2013-07-29 11:20:47 -0400 | [diff] [blame] | 1448 | extent_clear_unlock_delalloc(inode, cur_offset, |
| 1449 | cur_offset + num_bytes - 1, |
| 1450 | locked_page, EXTENT_LOCKED | |
| 1451 | EXTENT_DELALLOC, PAGE_UNLOCK | |
| 1452 | PAGE_SET_PRIVATE2); |
Wang Shilong | e9894fd | 2014-03-27 11:12:25 +0800 | [diff] [blame] | 1453 | if (!nolock && nocow) |
Filipe Manana | 9ea24bb | 2014-10-29 11:57:59 +0000 | [diff] [blame] | 1454 | btrfs_end_write_no_snapshoting(root); |
Yan Zheng | 80ff385 | 2008-10-30 14:20:02 -0400 | [diff] [blame] | 1455 | cur_offset = extent_end; |
| 1456 | if (cur_offset > end) |
| 1457 | break; |
Yan Zheng | 7ea394f | 2008-08-05 13:05:02 -0400 | [diff] [blame] | 1458 | } |
David Sterba | b3b4aa7 | 2011-04-21 01:20:15 +0200 | [diff] [blame] | 1459 | btrfs_release_path(path); |
Yan Zheng | 80ff385 | 2008-10-30 14:20:02 -0400 | [diff] [blame] | 1460 | |
Josef Bacik | 17ca04a | 2012-05-31 15:58:55 -0400 | [diff] [blame] | 1461 | if (cur_offset <= end && cow_start == (u64)-1) { |
Yan Zheng | 80ff385 | 2008-10-30 14:20:02 -0400 | [diff] [blame] | 1462 | cow_start = cur_offset; |
Josef Bacik | 17ca04a | 2012-05-31 15:58:55 -0400 | [diff] [blame] | 1463 | cur_offset = end; |
| 1464 | } |
| 1465 | |
Yan Zheng | 80ff385 | 2008-10-30 14:20:02 -0400 | [diff] [blame] | 1466 | if (cow_start != (u64)-1) { |
Josef Bacik | 0036158 | 2013-08-14 14:02:47 -0400 | [diff] [blame] | 1467 | ret = cow_file_range(inode, locked_page, cow_start, end, |
| 1468 | page_started, nr_written, 1); |
Josef Bacik | d788a34 | 2013-10-25 16:55:08 -0400 | [diff] [blame] | 1469 | if (ret) |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 1470 | goto error; |
Yan Zheng | 80ff385 | 2008-10-30 14:20:02 -0400 | [diff] [blame] | 1471 | } |
| 1472 | |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 1473 | error: |
Miao Xie | a698d075 | 2012-09-20 01:51:59 -0600 | [diff] [blame] | 1474 | err = btrfs_end_transaction(trans, root); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 1475 | if (!ret) |
| 1476 | ret = err; |
| 1477 | |
Josef Bacik | 17ca04a | 2012-05-31 15:58:55 -0400 | [diff] [blame] | 1478 | if (ret && cur_offset < end) |
Josef Bacik | c2790a2 | 2013-07-29 11:20:47 -0400 | [diff] [blame] | 1479 | extent_clear_unlock_delalloc(inode, cur_offset, end, |
| 1480 | locked_page, EXTENT_LOCKED | |
Josef Bacik | 151a41b | 2013-07-29 13:22:24 -0400 | [diff] [blame] | 1481 | EXTENT_DELALLOC | EXTENT_DEFRAG | |
| 1482 | EXTENT_DO_ACCOUNTING, PAGE_UNLOCK | |
| 1483 | PAGE_CLEAR_DIRTY | |
Josef Bacik | c2790a2 | 2013-07-29 11:20:47 -0400 | [diff] [blame] | 1484 | PAGE_SET_WRITEBACK | |
| 1485 | PAGE_END_WRITEBACK); |
Yan Zheng | 7ea394f | 2008-08-05 13:05:02 -0400 | [diff] [blame] | 1486 | btrfs_free_path(path); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 1487 | return ret; |
Chris Mason | be20aa9 | 2007-12-17 20:14:01 -0500 | [diff] [blame] | 1488 | } |
| 1489 | |
Wang Shilong | 47059d9 | 2014-07-03 18:22:07 +0800 | [diff] [blame] | 1490 | static inline int need_force_cow(struct inode *inode, u64 start, u64 end) |
| 1491 | { |
| 1492 | |
| 1493 | if (!(BTRFS_I(inode)->flags & BTRFS_INODE_NODATACOW) && |
| 1494 | !(BTRFS_I(inode)->flags & BTRFS_INODE_PREALLOC)) |
| 1495 | return 0; |
| 1496 | |
| 1497 | /* |
| 1498 | * @defrag_bytes is a hint value, no spinlock held here, |
| 1499 | * if is not zero, it means the file is defragging. |
| 1500 | * Force cow if given extent needs to be defragged. |
| 1501 | */ |
| 1502 | if (BTRFS_I(inode)->defrag_bytes && |
| 1503 | test_range_bit(&BTRFS_I(inode)->io_tree, start, end, |
| 1504 | EXTENT_DEFRAG, 0, NULL)) |
| 1505 | return 1; |
| 1506 | |
| 1507 | return 0; |
| 1508 | } |
| 1509 | |
Chris Mason | d352ac6 | 2008-09-29 15:18:18 -0400 | [diff] [blame] | 1510 | /* |
| 1511 | * extent_io.c call back to do delayed allocation processing |
| 1512 | */ |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 1513 | static int run_delalloc_range(struct inode *inode, struct page *locked_page, |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 1514 | u64 start, u64 end, int *page_started, |
| 1515 | unsigned long *nr_written) |
Chris Mason | be20aa9 | 2007-12-17 20:14:01 -0500 | [diff] [blame] | 1516 | { |
Chris Mason | be20aa9 | 2007-12-17 20:14:01 -0500 | [diff] [blame] | 1517 | int ret; |
Wang Shilong | 47059d9 | 2014-07-03 18:22:07 +0800 | [diff] [blame] | 1518 | int force_cow = need_force_cow(inode, start, end); |
Chris Mason | a213501 | 2008-06-25 16:01:30 -0400 | [diff] [blame] | 1519 | |
Wang Shilong | 47059d9 | 2014-07-03 18:22:07 +0800 | [diff] [blame] | 1520 | if (BTRFS_I(inode)->flags & BTRFS_INODE_NODATACOW && !force_cow) { |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 1521 | ret = run_delalloc_nocow(inode, locked_page, start, end, |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 1522 | page_started, 1, nr_written); |
Wang Shilong | 47059d9 | 2014-07-03 18:22:07 +0800 | [diff] [blame] | 1523 | } else if (BTRFS_I(inode)->flags & BTRFS_INODE_PREALLOC && !force_cow) { |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 1524 | ret = run_delalloc_nocow(inode, locked_page, start, end, |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 1525 | page_started, 0, nr_written); |
Wang Shilong | 7816030 | 2014-07-17 11:44:10 +0800 | [diff] [blame] | 1526 | } else if (!inode_need_compress(inode)) { |
Chris Mason | 7f366cf | 2009-03-12 20:12:45 -0400 | [diff] [blame] | 1527 | ret = cow_file_range(inode, locked_page, start, end, |
| 1528 | page_started, nr_written, 1); |
Josef Bacik | 7ddf5a4 | 2012-06-08 15:26:47 -0400 | [diff] [blame] | 1529 | } else { |
| 1530 | set_bit(BTRFS_INODE_HAS_ASYNC_EXTENT, |
| 1531 | &BTRFS_I(inode)->runtime_flags); |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 1532 | ret = cow_file_range_async(inode, locked_page, start, end, |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 1533 | page_started, nr_written); |
Josef Bacik | 7ddf5a4 | 2012-06-08 15:26:47 -0400 | [diff] [blame] | 1534 | } |
Chris Mason | b888db2b | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 1535 | return ret; |
| 1536 | } |
| 1537 | |
Jeff Mahoney | 1bf8504 | 2011-07-21 16:56:09 +0000 | [diff] [blame] | 1538 | static void btrfs_split_extent_hook(struct inode *inode, |
| 1539 | struct extent_state *orig, u64 split) |
Josef Bacik | 9ed74f2 | 2009-09-11 16:12:44 -0400 | [diff] [blame] | 1540 | { |
Josef Bacik | dcab6a3 | 2015-02-11 15:08:59 -0500 | [diff] [blame] | 1541 | u64 size; |
| 1542 | |
Yan, Zheng | 0ca1f7c | 2010-05-16 10:48:47 -0400 | [diff] [blame] | 1543 | /* not delalloc, ignore it */ |
Josef Bacik | 9ed74f2 | 2009-09-11 16:12:44 -0400 | [diff] [blame] | 1544 | if (!(orig->state & EXTENT_DELALLOC)) |
Jeff Mahoney | 1bf8504 | 2011-07-21 16:56:09 +0000 | [diff] [blame] | 1545 | return; |
Josef Bacik | 9ed74f2 | 2009-09-11 16:12:44 -0400 | [diff] [blame] | 1546 | |
Josef Bacik | dcab6a3 | 2015-02-11 15:08:59 -0500 | [diff] [blame] | 1547 | size = orig->end - orig->start + 1; |
| 1548 | if (size > BTRFS_MAX_EXTENT_SIZE) { |
| 1549 | u64 num_extents; |
| 1550 | u64 new_size; |
| 1551 | |
| 1552 | /* |
Josef Bacik | ba11721 | 2015-03-13 15:01:24 -0400 | [diff] [blame] | 1553 | * See the explanation in btrfs_merge_extent_hook, the same |
| 1554 | * applies here, just in reverse. |
Josef Bacik | dcab6a3 | 2015-02-11 15:08:59 -0500 | [diff] [blame] | 1555 | */ |
| 1556 | new_size = orig->end - split + 1; |
Josef Bacik | ba11721 | 2015-03-13 15:01:24 -0400 | [diff] [blame] | 1557 | num_extents = div64_u64(new_size + BTRFS_MAX_EXTENT_SIZE - 1, |
Josef Bacik | dcab6a3 | 2015-02-11 15:08:59 -0500 | [diff] [blame] | 1558 | BTRFS_MAX_EXTENT_SIZE); |
Josef Bacik | ba11721 | 2015-03-13 15:01:24 -0400 | [diff] [blame] | 1559 | new_size = split - orig->start; |
| 1560 | num_extents += div64_u64(new_size + BTRFS_MAX_EXTENT_SIZE - 1, |
| 1561 | BTRFS_MAX_EXTENT_SIZE); |
| 1562 | if (div64_u64(size + BTRFS_MAX_EXTENT_SIZE - 1, |
| 1563 | BTRFS_MAX_EXTENT_SIZE) >= num_extents) |
Josef Bacik | dcab6a3 | 2015-02-11 15:08:59 -0500 | [diff] [blame] | 1564 | return; |
| 1565 | } |
| 1566 | |
Josef Bacik | 9e0baf6 | 2011-07-15 15:16:44 +0000 | [diff] [blame] | 1567 | spin_lock(&BTRFS_I(inode)->lock); |
| 1568 | BTRFS_I(inode)->outstanding_extents++; |
| 1569 | spin_unlock(&BTRFS_I(inode)->lock); |
Josef Bacik | 9ed74f2 | 2009-09-11 16:12:44 -0400 | [diff] [blame] | 1570 | } |
| 1571 | |
| 1572 | /* |
| 1573 | * extent_io.c merge_extent_hook, used to track merged delayed allocation |
| 1574 | * extents so we can keep track of new extents that are just merged onto old |
| 1575 | * extents, such as when we are doing sequential writes, so we can properly |
| 1576 | * account for the metadata space we'll need. |
| 1577 | */ |
Jeff Mahoney | 1bf8504 | 2011-07-21 16:56:09 +0000 | [diff] [blame] | 1578 | static void btrfs_merge_extent_hook(struct inode *inode, |
| 1579 | struct extent_state *new, |
| 1580 | struct extent_state *other) |
Josef Bacik | 9ed74f2 | 2009-09-11 16:12:44 -0400 | [diff] [blame] | 1581 | { |
Josef Bacik | dcab6a3 | 2015-02-11 15:08:59 -0500 | [diff] [blame] | 1582 | u64 new_size, old_size; |
| 1583 | u64 num_extents; |
| 1584 | |
Josef Bacik | 9ed74f2 | 2009-09-11 16:12:44 -0400 | [diff] [blame] | 1585 | /* not delalloc, ignore it */ |
| 1586 | if (!(other->state & EXTENT_DELALLOC)) |
Jeff Mahoney | 1bf8504 | 2011-07-21 16:56:09 +0000 | [diff] [blame] | 1587 | return; |
Josef Bacik | 9ed74f2 | 2009-09-11 16:12:44 -0400 | [diff] [blame] | 1588 | |
Josef Bacik | 8461a3d | 2015-03-13 15:12:08 -0400 | [diff] [blame] | 1589 | if (new->start > other->start) |
| 1590 | new_size = new->end - other->start + 1; |
| 1591 | else |
| 1592 | new_size = other->end - new->start + 1; |
Josef Bacik | dcab6a3 | 2015-02-11 15:08:59 -0500 | [diff] [blame] | 1593 | |
| 1594 | /* we're not bigger than the max, unreserve the space and go */ |
| 1595 | if (new_size <= BTRFS_MAX_EXTENT_SIZE) { |
| 1596 | spin_lock(&BTRFS_I(inode)->lock); |
| 1597 | BTRFS_I(inode)->outstanding_extents--; |
| 1598 | spin_unlock(&BTRFS_I(inode)->lock); |
| 1599 | return; |
| 1600 | } |
| 1601 | |
| 1602 | /* |
Josef Bacik | ba11721 | 2015-03-13 15:01:24 -0400 | [diff] [blame] | 1603 | * We have to add up either side to figure out how many extents were |
| 1604 | * accounted for before we merged into one big extent. If the number of |
| 1605 | * extents we accounted for is <= the amount we need for the new range |
| 1606 | * then we can return, otherwise drop. Think of it like this |
| 1607 | * |
| 1608 | * [ 4k][MAX_SIZE] |
| 1609 | * |
| 1610 | * So we've grown the extent by a MAX_SIZE extent, this would mean we |
| 1611 | * need 2 outstanding extents, on one side we have 1 and the other side |
| 1612 | * we have 1 so they are == and we can return. But in this case |
| 1613 | * |
| 1614 | * [MAX_SIZE+4k][MAX_SIZE+4k] |
| 1615 | * |
| 1616 | * Each range on their own accounts for 2 extents, but merged together |
| 1617 | * they are only 3 extents worth of accounting, so we need to drop in |
| 1618 | * this case. |
Josef Bacik | dcab6a3 | 2015-02-11 15:08:59 -0500 | [diff] [blame] | 1619 | */ |
Josef Bacik | ba11721 | 2015-03-13 15:01:24 -0400 | [diff] [blame] | 1620 | old_size = other->end - other->start + 1; |
Josef Bacik | dcab6a3 | 2015-02-11 15:08:59 -0500 | [diff] [blame] | 1621 | num_extents = div64_u64(old_size + BTRFS_MAX_EXTENT_SIZE - 1, |
| 1622 | BTRFS_MAX_EXTENT_SIZE); |
Josef Bacik | ba11721 | 2015-03-13 15:01:24 -0400 | [diff] [blame] | 1623 | old_size = new->end - new->start + 1; |
| 1624 | num_extents += div64_u64(old_size + BTRFS_MAX_EXTENT_SIZE - 1, |
| 1625 | BTRFS_MAX_EXTENT_SIZE); |
| 1626 | |
Josef Bacik | dcab6a3 | 2015-02-11 15:08:59 -0500 | [diff] [blame] | 1627 | if (div64_u64(new_size + BTRFS_MAX_EXTENT_SIZE - 1, |
Josef Bacik | ba11721 | 2015-03-13 15:01:24 -0400 | [diff] [blame] | 1628 | BTRFS_MAX_EXTENT_SIZE) >= num_extents) |
Josef Bacik | dcab6a3 | 2015-02-11 15:08:59 -0500 | [diff] [blame] | 1629 | return; |
| 1630 | |
Josef Bacik | 9e0baf6 | 2011-07-15 15:16:44 +0000 | [diff] [blame] | 1631 | spin_lock(&BTRFS_I(inode)->lock); |
| 1632 | BTRFS_I(inode)->outstanding_extents--; |
| 1633 | spin_unlock(&BTRFS_I(inode)->lock); |
Josef Bacik | 9ed74f2 | 2009-09-11 16:12:44 -0400 | [diff] [blame] | 1634 | } |
| 1635 | |
Miao Xie | eb73c1b | 2013-05-15 07:48:22 +0000 | [diff] [blame] | 1636 | static void btrfs_add_delalloc_inodes(struct btrfs_root *root, |
| 1637 | struct inode *inode) |
| 1638 | { |
| 1639 | spin_lock(&root->delalloc_lock); |
| 1640 | if (list_empty(&BTRFS_I(inode)->delalloc_inodes)) { |
| 1641 | list_add_tail(&BTRFS_I(inode)->delalloc_inodes, |
| 1642 | &root->delalloc_inodes); |
| 1643 | set_bit(BTRFS_INODE_IN_DELALLOC_LIST, |
| 1644 | &BTRFS_I(inode)->runtime_flags); |
| 1645 | root->nr_delalloc_inodes++; |
| 1646 | if (root->nr_delalloc_inodes == 1) { |
| 1647 | spin_lock(&root->fs_info->delalloc_root_lock); |
| 1648 | BUG_ON(!list_empty(&root->delalloc_root)); |
| 1649 | list_add_tail(&root->delalloc_root, |
| 1650 | &root->fs_info->delalloc_roots); |
| 1651 | spin_unlock(&root->fs_info->delalloc_root_lock); |
| 1652 | } |
| 1653 | } |
| 1654 | spin_unlock(&root->delalloc_lock); |
| 1655 | } |
| 1656 | |
| 1657 | static void btrfs_del_delalloc_inode(struct btrfs_root *root, |
| 1658 | struct inode *inode) |
| 1659 | { |
| 1660 | spin_lock(&root->delalloc_lock); |
| 1661 | if (!list_empty(&BTRFS_I(inode)->delalloc_inodes)) { |
| 1662 | list_del_init(&BTRFS_I(inode)->delalloc_inodes); |
| 1663 | clear_bit(BTRFS_INODE_IN_DELALLOC_LIST, |
| 1664 | &BTRFS_I(inode)->runtime_flags); |
| 1665 | root->nr_delalloc_inodes--; |
| 1666 | if (!root->nr_delalloc_inodes) { |
| 1667 | spin_lock(&root->fs_info->delalloc_root_lock); |
| 1668 | BUG_ON(list_empty(&root->delalloc_root)); |
| 1669 | list_del_init(&root->delalloc_root); |
| 1670 | spin_unlock(&root->fs_info->delalloc_root_lock); |
| 1671 | } |
| 1672 | } |
| 1673 | spin_unlock(&root->delalloc_lock); |
| 1674 | } |
| 1675 | |
Chris Mason | d352ac6 | 2008-09-29 15:18:18 -0400 | [diff] [blame] | 1676 | /* |
| 1677 | * extent_io.c set_bit_hook, used to track delayed allocation |
| 1678 | * bytes in this file, and to maintain the list of inodes that |
| 1679 | * have pending delalloc work to be done. |
| 1680 | */ |
Jeff Mahoney | 1bf8504 | 2011-07-21 16:56:09 +0000 | [diff] [blame] | 1681 | static void btrfs_set_bit_hook(struct inode *inode, |
David Sterba | 9ee49a04 | 2015-01-14 19:52:13 +0100 | [diff] [blame] | 1682 | struct extent_state *state, unsigned *bits) |
Chris Mason | 291d673 | 2008-01-29 15:55:23 -0500 | [diff] [blame] | 1683 | { |
Josef Bacik | 9ed74f2 | 2009-09-11 16:12:44 -0400 | [diff] [blame] | 1684 | |
Wang Shilong | 47059d9 | 2014-07-03 18:22:07 +0800 | [diff] [blame] | 1685 | if ((*bits & EXTENT_DEFRAG) && !(*bits & EXTENT_DELALLOC)) |
| 1686 | WARN_ON(1); |
Chris Mason | 75eff68 | 2008-12-15 15:54:40 -0500 | [diff] [blame] | 1687 | /* |
| 1688 | * set_bit and clear bit hooks normally require _irqsave/restore |
Sergei Trofimovich | 27160b6 | 2011-05-20 20:20:32 +0000 | [diff] [blame] | 1689 | * but in this case, we are only testing for the DELALLOC |
Chris Mason | 75eff68 | 2008-12-15 15:54:40 -0500 | [diff] [blame] | 1690 | * bit, which is only set or cleared with irqs on |
| 1691 | */ |
Yan, Zheng | 0ca1f7c | 2010-05-16 10:48:47 -0400 | [diff] [blame] | 1692 | if (!(state->state & EXTENT_DELALLOC) && (*bits & EXTENT_DELALLOC)) { |
Chris Mason | 291d673 | 2008-01-29 15:55:23 -0500 | [diff] [blame] | 1693 | struct btrfs_root *root = BTRFS_I(inode)->root; |
Yan, Zheng | 0ca1f7c | 2010-05-16 10:48:47 -0400 | [diff] [blame] | 1694 | u64 len = state->end + 1 - state->start; |
Liu Bo | 83eea1f | 2012-07-10 05:28:39 -0600 | [diff] [blame] | 1695 | bool do_list = !btrfs_is_free_space_inode(inode); |
Josef Bacik | 9ed74f2 | 2009-09-11 16:12:44 -0400 | [diff] [blame] | 1696 | |
Josef Bacik | 9e0baf6 | 2011-07-15 15:16:44 +0000 | [diff] [blame] | 1697 | if (*bits & EXTENT_FIRST_DELALLOC) { |
Yan, Zheng | 0ca1f7c | 2010-05-16 10:48:47 -0400 | [diff] [blame] | 1698 | *bits &= ~EXTENT_FIRST_DELALLOC; |
Josef Bacik | 9e0baf6 | 2011-07-15 15:16:44 +0000 | [diff] [blame] | 1699 | } else { |
| 1700 | spin_lock(&BTRFS_I(inode)->lock); |
| 1701 | BTRFS_I(inode)->outstanding_extents++; |
| 1702 | spin_unlock(&BTRFS_I(inode)->lock); |
| 1703 | } |
Josef Bacik | 287a0ab | 2010-03-19 18:07:23 +0000 | [diff] [blame] | 1704 | |
Josef Bacik | 6a3891c | 2015-03-16 17:38:52 -0400 | [diff] [blame] | 1705 | /* For sanity tests */ |
| 1706 | if (btrfs_test_is_dummy_root(root)) |
| 1707 | return; |
| 1708 | |
Miao Xie | 963d678 | 2013-01-29 10:10:51 +0000 | [diff] [blame] | 1709 | __percpu_counter_add(&root->fs_info->delalloc_bytes, len, |
| 1710 | root->fs_info->delalloc_batch); |
Miao Xie | df0af1a | 2013-01-29 10:11:59 +0000 | [diff] [blame] | 1711 | spin_lock(&BTRFS_I(inode)->lock); |
Yan, Zheng | 0ca1f7c | 2010-05-16 10:48:47 -0400 | [diff] [blame] | 1712 | BTRFS_I(inode)->delalloc_bytes += len; |
Wang Shilong | 47059d9 | 2014-07-03 18:22:07 +0800 | [diff] [blame] | 1713 | if (*bits & EXTENT_DEFRAG) |
| 1714 | BTRFS_I(inode)->defrag_bytes += len; |
Miao Xie | df0af1a | 2013-01-29 10:11:59 +0000 | [diff] [blame] | 1715 | if (do_list && !test_bit(BTRFS_INODE_IN_DELALLOC_LIST, |
Miao Xie | eb73c1b | 2013-05-15 07:48:22 +0000 | [diff] [blame] | 1716 | &BTRFS_I(inode)->runtime_flags)) |
| 1717 | btrfs_add_delalloc_inodes(root, inode); |
Miao Xie | df0af1a | 2013-01-29 10:11:59 +0000 | [diff] [blame] | 1718 | spin_unlock(&BTRFS_I(inode)->lock); |
Chris Mason | 291d673 | 2008-01-29 15:55:23 -0500 | [diff] [blame] | 1719 | } |
Chris Mason | 291d673 | 2008-01-29 15:55:23 -0500 | [diff] [blame] | 1720 | } |
| 1721 | |
Chris Mason | d352ac6 | 2008-09-29 15:18:18 -0400 | [diff] [blame] | 1722 | /* |
| 1723 | * extent_io.c clear_bit_hook, see set_bit_hook for why |
| 1724 | */ |
Jeff Mahoney | 1bf8504 | 2011-07-21 16:56:09 +0000 | [diff] [blame] | 1725 | static void btrfs_clear_bit_hook(struct inode *inode, |
David Sterba | 4107488 | 2013-04-29 13:38:46 +0000 | [diff] [blame] | 1726 | struct extent_state *state, |
David Sterba | 9ee49a04 | 2015-01-14 19:52:13 +0100 | [diff] [blame] | 1727 | unsigned *bits) |
Chris Mason | 291d673 | 2008-01-29 15:55:23 -0500 | [diff] [blame] | 1728 | { |
Wang Shilong | 47059d9 | 2014-07-03 18:22:07 +0800 | [diff] [blame] | 1729 | u64 len = state->end + 1 - state->start; |
Josef Bacik | dcab6a3 | 2015-02-11 15:08:59 -0500 | [diff] [blame] | 1730 | u64 num_extents = div64_u64(len + BTRFS_MAX_EXTENT_SIZE -1, |
| 1731 | BTRFS_MAX_EXTENT_SIZE); |
Wang Shilong | 47059d9 | 2014-07-03 18:22:07 +0800 | [diff] [blame] | 1732 | |
| 1733 | spin_lock(&BTRFS_I(inode)->lock); |
| 1734 | if ((state->state & EXTENT_DEFRAG) && (*bits & EXTENT_DEFRAG)) |
| 1735 | BTRFS_I(inode)->defrag_bytes -= len; |
| 1736 | spin_unlock(&BTRFS_I(inode)->lock); |
| 1737 | |
Chris Mason | 75eff68 | 2008-12-15 15:54:40 -0500 | [diff] [blame] | 1738 | /* |
| 1739 | * set_bit and clear bit hooks normally require _irqsave/restore |
Sergei Trofimovich | 27160b6 | 2011-05-20 20:20:32 +0000 | [diff] [blame] | 1740 | * but in this case, we are only testing for the DELALLOC |
Chris Mason | 75eff68 | 2008-12-15 15:54:40 -0500 | [diff] [blame] | 1741 | * bit, which is only set or cleared with irqs on |
| 1742 | */ |
Yan, Zheng | 0ca1f7c | 2010-05-16 10:48:47 -0400 | [diff] [blame] | 1743 | if ((state->state & EXTENT_DELALLOC) && (*bits & EXTENT_DELALLOC)) { |
Chris Mason | 291d673 | 2008-01-29 15:55:23 -0500 | [diff] [blame] | 1744 | struct btrfs_root *root = BTRFS_I(inode)->root; |
Liu Bo | 83eea1f | 2012-07-10 05:28:39 -0600 | [diff] [blame] | 1745 | bool do_list = !btrfs_is_free_space_inode(inode); |
Chris Mason | bcbfce8 | 2008-04-22 13:26:47 -0400 | [diff] [blame] | 1746 | |
Josef Bacik | 9e0baf6 | 2011-07-15 15:16:44 +0000 | [diff] [blame] | 1747 | if (*bits & EXTENT_FIRST_DELALLOC) { |
Yan, Zheng | 0ca1f7c | 2010-05-16 10:48:47 -0400 | [diff] [blame] | 1748 | *bits &= ~EXTENT_FIRST_DELALLOC; |
Josef Bacik | 9e0baf6 | 2011-07-15 15:16:44 +0000 | [diff] [blame] | 1749 | } else if (!(*bits & EXTENT_DO_ACCOUNTING)) { |
| 1750 | spin_lock(&BTRFS_I(inode)->lock); |
Josef Bacik | dcab6a3 | 2015-02-11 15:08:59 -0500 | [diff] [blame] | 1751 | BTRFS_I(inode)->outstanding_extents -= num_extents; |
Josef Bacik | 9e0baf6 | 2011-07-15 15:16:44 +0000 | [diff] [blame] | 1752 | spin_unlock(&BTRFS_I(inode)->lock); |
| 1753 | } |
Yan, Zheng | 0ca1f7c | 2010-05-16 10:48:47 -0400 | [diff] [blame] | 1754 | |
Josef Bacik | b6d08f0 | 2013-09-27 14:57:43 -0400 | [diff] [blame] | 1755 | /* |
| 1756 | * We don't reserve metadata space for space cache inodes so we |
| 1757 | * don't need to call dellalloc_release_metadata if there is an |
| 1758 | * error. |
| 1759 | */ |
| 1760 | if (*bits & EXTENT_DO_ACCOUNTING && |
| 1761 | root != root->fs_info->tree_root) |
Yan, Zheng | 0ca1f7c | 2010-05-16 10:48:47 -0400 | [diff] [blame] | 1762 | btrfs_delalloc_release_metadata(inode, len); |
| 1763 | |
Josef Bacik | 6a3891c | 2015-03-16 17:38:52 -0400 | [diff] [blame] | 1764 | /* For sanity tests. */ |
| 1765 | if (btrfs_test_is_dummy_root(root)) |
| 1766 | return; |
| 1767 | |
Josef Bacik | 0cb59c9 | 2010-07-02 12:14:14 -0400 | [diff] [blame] | 1768 | if (root->root_key.objectid != BTRFS_DATA_RELOC_TREE_OBJECTID |
Josef Bacik | 7ee9e44 | 2013-06-21 16:37:03 -0400 | [diff] [blame] | 1769 | && do_list && !(state->state & EXTENT_NORESERVE)) |
Yan, Zheng | 0ca1f7c | 2010-05-16 10:48:47 -0400 | [diff] [blame] | 1770 | btrfs_free_reserved_data_space(inode, len); |
Josef Bacik | 9ed74f2 | 2009-09-11 16:12:44 -0400 | [diff] [blame] | 1771 | |
Miao Xie | 963d678 | 2013-01-29 10:10:51 +0000 | [diff] [blame] | 1772 | __percpu_counter_add(&root->fs_info->delalloc_bytes, -len, |
| 1773 | root->fs_info->delalloc_batch); |
Miao Xie | df0af1a | 2013-01-29 10:11:59 +0000 | [diff] [blame] | 1774 | spin_lock(&BTRFS_I(inode)->lock); |
Yan, Zheng | 0ca1f7c | 2010-05-16 10:48:47 -0400 | [diff] [blame] | 1775 | BTRFS_I(inode)->delalloc_bytes -= len; |
Josef Bacik | 0cb59c9 | 2010-07-02 12:14:14 -0400 | [diff] [blame] | 1776 | if (do_list && BTRFS_I(inode)->delalloc_bytes == 0 && |
Miao Xie | df0af1a | 2013-01-29 10:11:59 +0000 | [diff] [blame] | 1777 | test_bit(BTRFS_INODE_IN_DELALLOC_LIST, |
Miao Xie | eb73c1b | 2013-05-15 07:48:22 +0000 | [diff] [blame] | 1778 | &BTRFS_I(inode)->runtime_flags)) |
| 1779 | btrfs_del_delalloc_inode(root, inode); |
Miao Xie | df0af1a | 2013-01-29 10:11:59 +0000 | [diff] [blame] | 1780 | spin_unlock(&BTRFS_I(inode)->lock); |
Chris Mason | 291d673 | 2008-01-29 15:55:23 -0500 | [diff] [blame] | 1781 | } |
Chris Mason | 291d673 | 2008-01-29 15:55:23 -0500 | [diff] [blame] | 1782 | } |
| 1783 | |
Chris Mason | d352ac6 | 2008-09-29 15:18:18 -0400 | [diff] [blame] | 1784 | /* |
| 1785 | * extent_io.c merge_bio_hook, this must check the chunk tree to make sure |
| 1786 | * we don't create bios that span stripes or chunks |
| 1787 | */ |
David Woodhouse | 64a1670 | 2009-07-15 23:29:37 +0100 | [diff] [blame] | 1788 | 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] | 1789 | size_t size, struct bio *bio, |
| 1790 | unsigned long bio_flags) |
Chris Mason | 239b14b | 2008-03-24 15:02:07 -0400 | [diff] [blame] | 1791 | { |
| 1792 | struct btrfs_root *root = BTRFS_I(page->mapping->host)->root; |
Kent Overstreet | 4f024f3 | 2013-10-11 15:44:27 -0700 | [diff] [blame] | 1793 | u64 logical = (u64)bio->bi_iter.bi_sector << 9; |
Chris Mason | 239b14b | 2008-03-24 15:02:07 -0400 | [diff] [blame] | 1794 | u64 length = 0; |
| 1795 | u64 map_length; |
Chris Mason | 239b14b | 2008-03-24 15:02:07 -0400 | [diff] [blame] | 1796 | int ret; |
| 1797 | |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 1798 | if (bio_flags & EXTENT_BIO_COMPRESSED) |
| 1799 | return 0; |
| 1800 | |
Kent Overstreet | 4f024f3 | 2013-10-11 15:44:27 -0700 | [diff] [blame] | 1801 | length = bio->bi_iter.bi_size; |
Chris Mason | 239b14b | 2008-03-24 15:02:07 -0400 | [diff] [blame] | 1802 | map_length = length; |
David Woodhouse | 64a1670 | 2009-07-15 23:29:37 +0100 | [diff] [blame] | 1803 | ret = btrfs_map_block(root->fs_info, rw, logical, |
Chris Mason | f188591 | 2008-04-09 16:28:12 -0400 | [diff] [blame] | 1804 | &map_length, NULL, 0); |
Stefan Behrens | 3ec706c | 2012-11-05 15:46:42 +0100 | [diff] [blame] | 1805 | /* Will always return 0 with map_multi == NULL */ |
Jeff Mahoney | 3444a97 | 2011-10-03 23:23:13 -0400 | [diff] [blame] | 1806 | BUG_ON(ret < 0); |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 1807 | if (map_length < length + size) |
Chris Mason | 239b14b | 2008-03-24 15:02:07 -0400 | [diff] [blame] | 1808 | return 1; |
Jeff Mahoney | 3444a97 | 2011-10-03 23:23:13 -0400 | [diff] [blame] | 1809 | return 0; |
Chris Mason | 239b14b | 2008-03-24 15:02:07 -0400 | [diff] [blame] | 1810 | } |
| 1811 | |
Chris Mason | d352ac6 | 2008-09-29 15:18:18 -0400 | [diff] [blame] | 1812 | /* |
| 1813 | * in order to insert checksums into the metadata in large chunks, |
| 1814 | * we wait until bio submission time. All the pages in the bio are |
| 1815 | * checksummed and sums are attached onto the ordered extent record. |
| 1816 | * |
| 1817 | * At IO completion time the cums attached on the ordered extent record |
| 1818 | * are inserted into the btree |
| 1819 | */ |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 1820 | static int __btrfs_submit_bio_start(struct inode *inode, int rw, |
| 1821 | struct bio *bio, int mirror_num, |
Chris Mason | eaf25d9 | 2010-05-25 09:48:28 -0400 | [diff] [blame] | 1822 | unsigned long bio_flags, |
| 1823 | u64 bio_offset) |
Chris Mason | 065631f | 2008-02-20 12:07:25 -0500 | [diff] [blame] | 1824 | { |
Chris Mason | 065631f | 2008-02-20 12:07:25 -0500 | [diff] [blame] | 1825 | struct btrfs_root *root = BTRFS_I(inode)->root; |
Chris Mason | 065631f | 2008-02-20 12:07:25 -0500 | [diff] [blame] | 1826 | int ret = 0; |
Chris Mason | e015640 | 2008-04-16 11:15:20 -0400 | [diff] [blame] | 1827 | |
Chris Mason | d20f704 | 2008-12-08 16:58:54 -0500 | [diff] [blame] | 1828 | ret = btrfs_csum_one_bio(root, inode, bio, 0, 0); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 1829 | BUG_ON(ret); /* -ENOMEM */ |
Chris Mason | 4a69a41 | 2008-11-06 22:03:00 -0500 | [diff] [blame] | 1830 | return 0; |
| 1831 | } |
Chris Mason | e015640 | 2008-04-16 11:15:20 -0400 | [diff] [blame] | 1832 | |
Chris Mason | 4a69a41 | 2008-11-06 22:03:00 -0500 | [diff] [blame] | 1833 | /* |
| 1834 | * in order to insert checksums into the metadata in large chunks, |
| 1835 | * we wait until bio submission time. All the pages in the bio are |
| 1836 | * checksummed and sums are attached onto the ordered extent record. |
| 1837 | * |
| 1838 | * At IO completion time the cums attached on the ordered extent record |
| 1839 | * are inserted into the btree |
| 1840 | */ |
Christoph Hellwig | b295086 | 2008-12-02 09:54:17 -0500 | [diff] [blame] | 1841 | 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] | 1842 | int mirror_num, unsigned long bio_flags, |
| 1843 | u64 bio_offset) |
Chris Mason | 4a69a41 | 2008-11-06 22:03:00 -0500 | [diff] [blame] | 1844 | { |
| 1845 | struct btrfs_root *root = BTRFS_I(inode)->root; |
Stefan Behrens | 6189192 | 2012-11-05 18:51:52 +0100 | [diff] [blame] | 1846 | int ret; |
| 1847 | |
| 1848 | ret = btrfs_map_bio(root, rw, bio, mirror_num, 1); |
| 1849 | if (ret) |
| 1850 | bio_endio(bio, ret); |
| 1851 | return ret; |
Chris Mason | 44b8bd7 | 2008-04-16 11:14:51 -0400 | [diff] [blame] | 1852 | } |
| 1853 | |
Chris Mason | d352ac6 | 2008-09-29 15:18:18 -0400 | [diff] [blame] | 1854 | /* |
Chris Mason | cad321a | 2008-12-17 14:51:42 -0500 | [diff] [blame] | 1855 | * extent_io.c submission hook. This does the right thing for csum calculation |
| 1856 | * on write, or reading the csums from the tree before a read |
Chris Mason | d352ac6 | 2008-09-29 15:18:18 -0400 | [diff] [blame] | 1857 | */ |
Christoph Hellwig | b295086 | 2008-12-02 09:54:17 -0500 | [diff] [blame] | 1858 | 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] | 1859 | int mirror_num, unsigned long bio_flags, |
| 1860 | u64 bio_offset) |
Chris Mason | 44b8bd7 | 2008-04-16 11:14:51 -0400 | [diff] [blame] | 1861 | { |
| 1862 | struct btrfs_root *root = BTRFS_I(inode)->root; |
| 1863 | int ret = 0; |
Chris Mason | 19b9bdb | 2008-10-30 14:23:13 -0400 | [diff] [blame] | 1864 | int skip_sum; |
Jeff Mahoney | 0417341 | 2011-10-03 23:23:12 -0400 | [diff] [blame] | 1865 | int metadata = 0; |
Josef Bacik | b812ce2 | 2012-11-16 13:56:32 -0500 | [diff] [blame] | 1866 | int async = !atomic_read(&BTRFS_I(inode)->sync_writers); |
Chris Mason | 44b8bd7 | 2008-04-16 11:14:51 -0400 | [diff] [blame] | 1867 | |
Christoph Hellwig | 6cbff00 | 2009-04-17 10:37:41 +0200 | [diff] [blame] | 1868 | skip_sum = BTRFS_I(inode)->flags & BTRFS_INODE_NODATASUM; |
Chris Mason | cad321a | 2008-12-17 14:51:42 -0500 | [diff] [blame] | 1869 | |
Liu Bo | 83eea1f | 2012-07-10 05:28:39 -0600 | [diff] [blame] | 1870 | if (btrfs_is_free_space_inode(inode)) |
Jeff Mahoney | 0417341 | 2011-10-03 23:23:12 -0400 | [diff] [blame] | 1871 | metadata = 2; |
| 1872 | |
Christoph Hellwig | 7b6d91d | 2010-08-07 18:20:39 +0200 | [diff] [blame] | 1873 | if (!(rw & REQ_WRITE)) { |
Josef Bacik | 5fd0204 | 2012-05-02 14:00:54 -0400 | [diff] [blame] | 1874 | ret = btrfs_bio_wq_end_io(root->fs_info, bio, metadata); |
| 1875 | if (ret) |
Stefan Behrens | 6189192 | 2012-11-05 18:51:52 +0100 | [diff] [blame] | 1876 | goto out; |
Josef Bacik | 5fd0204 | 2012-05-02 14:00:54 -0400 | [diff] [blame] | 1877 | |
Chris Mason | d20f704 | 2008-12-08 16:58:54 -0500 | [diff] [blame] | 1878 | if (bio_flags & EXTENT_BIO_COMPRESSED) { |
Stefan Behrens | 6189192 | 2012-11-05 18:51:52 +0100 | [diff] [blame] | 1879 | ret = btrfs_submit_compressed_read(inode, bio, |
| 1880 | mirror_num, |
| 1881 | bio_flags); |
| 1882 | goto out; |
Tsutomu Itoh | c2db107 | 2011-03-01 06:48:31 +0000 | [diff] [blame] | 1883 | } else if (!skip_sum) { |
| 1884 | ret = btrfs_lookup_bio_sums(root, inode, bio, NULL); |
| 1885 | if (ret) |
Stefan Behrens | 6189192 | 2012-11-05 18:51:52 +0100 | [diff] [blame] | 1886 | goto out; |
Tsutomu Itoh | c2db107 | 2011-03-01 06:48:31 +0000 | [diff] [blame] | 1887 | } |
Chris Mason | 4d1b5fb | 2008-08-20 09:44:52 -0400 | [diff] [blame] | 1888 | goto mapit; |
Josef Bacik | b812ce2 | 2012-11-16 13:56:32 -0500 | [diff] [blame] | 1889 | } else if (async && !skip_sum) { |
Yan Zheng | 17d217f | 2008-12-12 10:03:38 -0500 | [diff] [blame] | 1890 | /* csum items have already been cloned */ |
| 1891 | if (root->root_key.objectid == BTRFS_DATA_RELOC_TREE_OBJECTID) |
| 1892 | goto mapit; |
Chris Mason | 19b9bdb | 2008-10-30 14:23:13 -0400 | [diff] [blame] | 1893 | /* we're doing a write, do the async checksumming */ |
Stefan Behrens | 6189192 | 2012-11-05 18:51:52 +0100 | [diff] [blame] | 1894 | ret = btrfs_wq_submit_bio(BTRFS_I(inode)->root->fs_info, |
Chris Mason | 44b8bd7 | 2008-04-16 11:14:51 -0400 | [diff] [blame] | 1895 | inode, rw, bio, mirror_num, |
Chris Mason | eaf25d9 | 2010-05-25 09:48:28 -0400 | [diff] [blame] | 1896 | bio_flags, bio_offset, |
| 1897 | __btrfs_submit_bio_start, |
Chris Mason | 4a69a41 | 2008-11-06 22:03:00 -0500 | [diff] [blame] | 1898 | __btrfs_submit_bio_done); |
Stefan Behrens | 6189192 | 2012-11-05 18:51:52 +0100 | [diff] [blame] | 1899 | goto out; |
Josef Bacik | b812ce2 | 2012-11-16 13:56:32 -0500 | [diff] [blame] | 1900 | } else if (!skip_sum) { |
| 1901 | ret = btrfs_csum_one_bio(root, inode, bio, 0, 0); |
| 1902 | if (ret) |
| 1903 | goto out; |
Chris Mason | 19b9bdb | 2008-10-30 14:23:13 -0400 | [diff] [blame] | 1904 | } |
| 1905 | |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 1906 | mapit: |
Stefan Behrens | 6189192 | 2012-11-05 18:51:52 +0100 | [diff] [blame] | 1907 | ret = btrfs_map_bio(root, rw, bio, mirror_num, 0); |
| 1908 | |
| 1909 | out: |
| 1910 | if (ret < 0) |
| 1911 | bio_endio(bio, ret); |
| 1912 | return ret; |
Chris Mason | 065631f | 2008-02-20 12:07:25 -0500 | [diff] [blame] | 1913 | } |
Chris Mason | 6885f30 | 2008-02-20 16:11:05 -0500 | [diff] [blame] | 1914 | |
Chris Mason | d352ac6 | 2008-09-29 15:18:18 -0400 | [diff] [blame] | 1915 | /* |
| 1916 | * given a list of ordered sums record them in the inode. This happens |
| 1917 | * at IO completion time based on sums calculated at bio submission time. |
| 1918 | */ |
Chris Mason | ba1da2f | 2008-07-17 12:54:15 -0400 | [diff] [blame] | 1919 | static noinline int add_pending_csums(struct btrfs_trans_handle *trans, |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 1920 | struct inode *inode, u64 file_offset, |
| 1921 | struct list_head *list) |
| 1922 | { |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 1923 | struct btrfs_ordered_sum *sum; |
| 1924 | |
Qinghuang Feng | c6e3087 | 2009-01-21 10:59:08 -0500 | [diff] [blame] | 1925 | list_for_each_entry(sum, list, list) { |
Miao Xie | 39847c4 | 2013-03-28 08:08:20 +0000 | [diff] [blame] | 1926 | trans->adding_csums = 1; |
Chris Mason | d20f704 | 2008-12-08 16:58:54 -0500 | [diff] [blame] | 1927 | btrfs_csum_file_blocks(trans, |
| 1928 | BTRFS_I(inode)->root->fs_info->csum_root, sum); |
Miao Xie | 39847c4 | 2013-03-28 08:08:20 +0000 | [diff] [blame] | 1929 | trans->adding_csums = 0; |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 1930 | } |
| 1931 | return 0; |
| 1932 | } |
| 1933 | |
Josef Bacik | 2ac55d4 | 2010-02-03 19:33:23 +0000 | [diff] [blame] | 1934 | int btrfs_set_extent_delalloc(struct inode *inode, u64 start, u64 end, |
| 1935 | struct extent_state **cached_state) |
Chris Mason | ea8c281 | 2008-08-04 23:17:27 -0400 | [diff] [blame] | 1936 | { |
Julia Lawall | 6c1500f | 2012-11-03 20:30:18 +0000 | [diff] [blame] | 1937 | WARN_ON((end & (PAGE_CACHE_SIZE - 1)) == 0); |
Chris Mason | ea8c281 | 2008-08-04 23:17:27 -0400 | [diff] [blame] | 1938 | return set_extent_delalloc(&BTRFS_I(inode)->io_tree, start, end, |
Josef Bacik | 2ac55d4 | 2010-02-03 19:33:23 +0000 | [diff] [blame] | 1939 | cached_state, GFP_NOFS); |
Chris Mason | ea8c281 | 2008-08-04 23:17:27 -0400 | [diff] [blame] | 1940 | } |
| 1941 | |
Chris Mason | d352ac6 | 2008-09-29 15:18:18 -0400 | [diff] [blame] | 1942 | /* see btrfs_writepage_start_hook for details on why this is required */ |
Chris Mason | 247e743 | 2008-07-17 12:53:51 -0400 | [diff] [blame] | 1943 | struct btrfs_writepage_fixup { |
| 1944 | struct page *page; |
| 1945 | struct btrfs_work work; |
| 1946 | }; |
| 1947 | |
Christoph Hellwig | b295086 | 2008-12-02 09:54:17 -0500 | [diff] [blame] | 1948 | static void btrfs_writepage_fixup_worker(struct btrfs_work *work) |
Chris Mason | 247e743 | 2008-07-17 12:53:51 -0400 | [diff] [blame] | 1949 | { |
| 1950 | struct btrfs_writepage_fixup *fixup; |
| 1951 | struct btrfs_ordered_extent *ordered; |
Josef Bacik | 2ac55d4 | 2010-02-03 19:33:23 +0000 | [diff] [blame] | 1952 | struct extent_state *cached_state = NULL; |
Chris Mason | 247e743 | 2008-07-17 12:53:51 -0400 | [diff] [blame] | 1953 | struct page *page; |
| 1954 | struct inode *inode; |
| 1955 | u64 page_start; |
| 1956 | u64 page_end; |
Jeff Mahoney | 87826df | 2012-02-15 16:23:57 +0100 | [diff] [blame] | 1957 | int ret; |
Chris Mason | 247e743 | 2008-07-17 12:53:51 -0400 | [diff] [blame] | 1958 | |
| 1959 | fixup = container_of(work, struct btrfs_writepage_fixup, work); |
| 1960 | page = fixup->page; |
Chris Mason | 4a09675 | 2008-07-21 10:29:44 -0400 | [diff] [blame] | 1961 | again: |
Chris Mason | 247e743 | 2008-07-17 12:53:51 -0400 | [diff] [blame] | 1962 | lock_page(page); |
| 1963 | if (!page->mapping || !PageDirty(page) || !PageChecked(page)) { |
| 1964 | ClearPageChecked(page); |
| 1965 | goto out_page; |
| 1966 | } |
| 1967 | |
| 1968 | inode = page->mapping->host; |
| 1969 | page_start = page_offset(page); |
| 1970 | page_end = page_offset(page) + PAGE_CACHE_SIZE - 1; |
| 1971 | |
Josef Bacik | 2ac55d4 | 2010-02-03 19:33:23 +0000 | [diff] [blame] | 1972 | 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] | 1973 | &cached_state); |
Chris Mason | 4a09675 | 2008-07-21 10:29:44 -0400 | [diff] [blame] | 1974 | |
| 1975 | /* already ordered? We're done */ |
Chris Mason | 8b62b72 | 2009-09-02 16:53:46 -0400 | [diff] [blame] | 1976 | if (PagePrivate2(page)) |
Chris Mason | 247e743 | 2008-07-17 12:53:51 -0400 | [diff] [blame] | 1977 | goto out; |
Chris Mason | 4a09675 | 2008-07-21 10:29:44 -0400 | [diff] [blame] | 1978 | |
| 1979 | ordered = btrfs_lookup_ordered_extent(inode, page_start); |
| 1980 | if (ordered) { |
Josef Bacik | 2ac55d4 | 2010-02-03 19:33:23 +0000 | [diff] [blame] | 1981 | unlock_extent_cached(&BTRFS_I(inode)->io_tree, page_start, |
| 1982 | page_end, &cached_state, GFP_NOFS); |
Chris Mason | 4a09675 | 2008-07-21 10:29:44 -0400 | [diff] [blame] | 1983 | unlock_page(page); |
| 1984 | btrfs_start_ordered_extent(inode, ordered, 1); |
Jeff Mahoney | 87826df | 2012-02-15 16:23:57 +0100 | [diff] [blame] | 1985 | btrfs_put_ordered_extent(ordered); |
Chris Mason | 4a09675 | 2008-07-21 10:29:44 -0400 | [diff] [blame] | 1986 | goto again; |
| 1987 | } |
Chris Mason | 247e743 | 2008-07-17 12:53:51 -0400 | [diff] [blame] | 1988 | |
Jeff Mahoney | 87826df | 2012-02-15 16:23:57 +0100 | [diff] [blame] | 1989 | ret = btrfs_delalloc_reserve_space(inode, PAGE_CACHE_SIZE); |
| 1990 | if (ret) { |
| 1991 | mapping_set_error(page->mapping, ret); |
| 1992 | end_extent_writepage(page, ret, page_start, page_end); |
| 1993 | ClearPageChecked(page); |
| 1994 | goto out; |
| 1995 | } |
| 1996 | |
Josef Bacik | 2ac55d4 | 2010-02-03 19:33:23 +0000 | [diff] [blame] | 1997 | btrfs_set_extent_delalloc(inode, page_start, page_end, &cached_state); |
Chris Mason | 247e743 | 2008-07-17 12:53:51 -0400 | [diff] [blame] | 1998 | ClearPageChecked(page); |
Jeff Mahoney | 87826df | 2012-02-15 16:23:57 +0100 | [diff] [blame] | 1999 | set_page_dirty(page); |
Chris Mason | 247e743 | 2008-07-17 12:53:51 -0400 | [diff] [blame] | 2000 | out: |
Josef Bacik | 2ac55d4 | 2010-02-03 19:33:23 +0000 | [diff] [blame] | 2001 | unlock_extent_cached(&BTRFS_I(inode)->io_tree, page_start, page_end, |
| 2002 | &cached_state, GFP_NOFS); |
Chris Mason | 247e743 | 2008-07-17 12:53:51 -0400 | [diff] [blame] | 2003 | out_page: |
| 2004 | unlock_page(page); |
| 2005 | page_cache_release(page); |
Miao Xie | b897abe | 2011-01-26 16:19:22 +0800 | [diff] [blame] | 2006 | kfree(fixup); |
Chris Mason | 247e743 | 2008-07-17 12:53:51 -0400 | [diff] [blame] | 2007 | } |
| 2008 | |
| 2009 | /* |
| 2010 | * There are a few paths in the higher layers of the kernel that directly |
| 2011 | * set the page dirty bit without asking the filesystem if it is a |
| 2012 | * good idea. This causes problems because we want to make sure COW |
| 2013 | * properly happens and the data=ordered rules are followed. |
| 2014 | * |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 2015 | * 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] | 2016 | * hasn't been properly setup for IO. We kick off an async process |
| 2017 | * to fix it up. The async helper will wait for ordered extents, set |
| 2018 | * the delalloc bit and make it safe to write the page. |
| 2019 | */ |
Christoph Hellwig | b295086 | 2008-12-02 09:54:17 -0500 | [diff] [blame] | 2020 | 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] | 2021 | { |
| 2022 | struct inode *inode = page->mapping->host; |
| 2023 | struct btrfs_writepage_fixup *fixup; |
| 2024 | struct btrfs_root *root = BTRFS_I(inode)->root; |
Chris Mason | 247e743 | 2008-07-17 12:53:51 -0400 | [diff] [blame] | 2025 | |
Chris Mason | 8b62b72 | 2009-09-02 16:53:46 -0400 | [diff] [blame] | 2026 | /* this page is properly in the ordered list */ |
| 2027 | if (TestClearPagePrivate2(page)) |
Chris Mason | 247e743 | 2008-07-17 12:53:51 -0400 | [diff] [blame] | 2028 | return 0; |
| 2029 | |
| 2030 | if (PageChecked(page)) |
| 2031 | return -EAGAIN; |
| 2032 | |
| 2033 | fixup = kzalloc(sizeof(*fixup), GFP_NOFS); |
| 2034 | if (!fixup) |
| 2035 | return -EAGAIN; |
Chris Mason | f421950 | 2008-07-22 11:18:09 -0400 | [diff] [blame] | 2036 | |
Chris Mason | 247e743 | 2008-07-17 12:53:51 -0400 | [diff] [blame] | 2037 | SetPageChecked(page); |
| 2038 | page_cache_get(page); |
Liu Bo | 9e0af23 | 2014-08-15 23:36:53 +0800 | [diff] [blame] | 2039 | btrfs_init_work(&fixup->work, btrfs_fixup_helper, |
| 2040 | btrfs_writepage_fixup_worker, NULL, NULL); |
Chris Mason | 247e743 | 2008-07-17 12:53:51 -0400 | [diff] [blame] | 2041 | fixup->page = page; |
Qu Wenruo | dc6e320 | 2014-02-28 10:46:14 +0800 | [diff] [blame] | 2042 | btrfs_queue_work(root->fs_info->fixup_workers, &fixup->work); |
Jeff Mahoney | 87826df | 2012-02-15 16:23:57 +0100 | [diff] [blame] | 2043 | return -EBUSY; |
Chris Mason | 247e743 | 2008-07-17 12:53:51 -0400 | [diff] [blame] | 2044 | } |
| 2045 | |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 2046 | static int insert_reserved_file_extent(struct btrfs_trans_handle *trans, |
| 2047 | struct inode *inode, u64 file_pos, |
| 2048 | u64 disk_bytenr, u64 disk_num_bytes, |
| 2049 | u64 num_bytes, u64 ram_bytes, |
| 2050 | u8 compression, u8 encryption, |
| 2051 | u16 other_encoding, int extent_type) |
| 2052 | { |
| 2053 | struct btrfs_root *root = BTRFS_I(inode)->root; |
| 2054 | struct btrfs_file_extent_item *fi; |
| 2055 | struct btrfs_path *path; |
| 2056 | struct extent_buffer *leaf; |
| 2057 | struct btrfs_key ins; |
Filipe David Borba Manana | 1acae57 | 2014-01-07 11:42:27 +0000 | [diff] [blame] | 2058 | int extent_inserted = 0; |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 2059 | int ret; |
| 2060 | |
| 2061 | path = btrfs_alloc_path(); |
Mark Fasheh | d8926bb | 2011-07-13 10:38:47 -0700 | [diff] [blame] | 2062 | if (!path) |
| 2063 | return -ENOMEM; |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 2064 | |
Chris Mason | a1ed835 | 2009-09-11 12:27:37 -0400 | [diff] [blame] | 2065 | /* |
| 2066 | * we may be replacing one extent in the tree with another. |
| 2067 | * The new extent is pinned in the extent map, and we don't want |
| 2068 | * to drop it from the cache until it is completely in the btree. |
| 2069 | * |
| 2070 | * So, tell btrfs_drop_extents to leave this extent in the cache. |
| 2071 | * the caller is expected to unpin it and allow it to be merged |
| 2072 | * with the others. |
| 2073 | */ |
Filipe David Borba Manana | 1acae57 | 2014-01-07 11:42:27 +0000 | [diff] [blame] | 2074 | ret = __btrfs_drop_extents(trans, root, inode, path, file_pos, |
| 2075 | file_pos + num_bytes, NULL, 0, |
| 2076 | 1, sizeof(*fi), &extent_inserted); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 2077 | if (ret) |
| 2078 | goto out; |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 2079 | |
Filipe David Borba Manana | 1acae57 | 2014-01-07 11:42:27 +0000 | [diff] [blame] | 2080 | if (!extent_inserted) { |
| 2081 | ins.objectid = btrfs_ino(inode); |
| 2082 | ins.offset = file_pos; |
| 2083 | ins.type = BTRFS_EXTENT_DATA_KEY; |
| 2084 | |
| 2085 | path->leave_spinning = 1; |
| 2086 | ret = btrfs_insert_empty_item(trans, root, path, &ins, |
| 2087 | sizeof(*fi)); |
| 2088 | if (ret) |
| 2089 | goto out; |
| 2090 | } |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 2091 | leaf = path->nodes[0]; |
| 2092 | fi = btrfs_item_ptr(leaf, path->slots[0], |
| 2093 | struct btrfs_file_extent_item); |
| 2094 | btrfs_set_file_extent_generation(leaf, fi, trans->transid); |
| 2095 | btrfs_set_file_extent_type(leaf, fi, extent_type); |
| 2096 | btrfs_set_file_extent_disk_bytenr(leaf, fi, disk_bytenr); |
| 2097 | btrfs_set_file_extent_disk_num_bytes(leaf, fi, disk_num_bytes); |
| 2098 | btrfs_set_file_extent_offset(leaf, fi, 0); |
| 2099 | btrfs_set_file_extent_num_bytes(leaf, fi, num_bytes); |
| 2100 | btrfs_set_file_extent_ram_bytes(leaf, fi, ram_bytes); |
| 2101 | btrfs_set_file_extent_compression(leaf, fi, compression); |
| 2102 | btrfs_set_file_extent_encryption(leaf, fi, encryption); |
| 2103 | btrfs_set_file_extent_other_encoding(leaf, fi, other_encoding); |
Chris Mason | b947343 | 2009-03-13 11:00:37 -0400 | [diff] [blame] | 2104 | |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 2105 | btrfs_mark_buffer_dirty(leaf); |
Josef Bacik | ce19533 | 2012-09-25 15:26:16 -0400 | [diff] [blame] | 2106 | btrfs_release_path(path); |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 2107 | |
| 2108 | inode_add_bytes(inode, num_bytes); |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 2109 | |
| 2110 | ins.objectid = disk_bytenr; |
| 2111 | ins.offset = disk_num_bytes; |
| 2112 | ins.type = BTRFS_EXTENT_ITEM_KEY; |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 2113 | ret = btrfs_alloc_reserved_file_extent(trans, root, |
| 2114 | root->root_key.objectid, |
Li Zefan | 33345d01 | 2011-04-20 10:31:50 +0800 | [diff] [blame] | 2115 | btrfs_ino(inode), file_pos, &ins); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 2116 | out: |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 2117 | btrfs_free_path(path); |
Chris Mason | b947343 | 2009-03-13 11:00:37 -0400 | [diff] [blame] | 2118 | |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 2119 | return ret; |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 2120 | } |
| 2121 | |
Liu Bo | 38c227d | 2013-01-29 03:18:40 +0000 | [diff] [blame] | 2122 | /* snapshot-aware defrag */ |
| 2123 | struct sa_defrag_extent_backref { |
| 2124 | struct rb_node node; |
| 2125 | struct old_sa_defrag_extent *old; |
| 2126 | u64 root_id; |
| 2127 | u64 inum; |
| 2128 | u64 file_pos; |
| 2129 | u64 extent_offset; |
| 2130 | u64 num_bytes; |
| 2131 | u64 generation; |
| 2132 | }; |
| 2133 | |
| 2134 | struct old_sa_defrag_extent { |
| 2135 | struct list_head list; |
| 2136 | struct new_sa_defrag_extent *new; |
| 2137 | |
| 2138 | u64 extent_offset; |
| 2139 | u64 bytenr; |
| 2140 | u64 offset; |
| 2141 | u64 len; |
| 2142 | int count; |
| 2143 | }; |
| 2144 | |
| 2145 | struct new_sa_defrag_extent { |
| 2146 | struct rb_root root; |
| 2147 | struct list_head head; |
| 2148 | struct btrfs_path *path; |
| 2149 | struct inode *inode; |
| 2150 | u64 file_pos; |
| 2151 | u64 len; |
| 2152 | u64 bytenr; |
| 2153 | u64 disk_len; |
| 2154 | u8 compress_type; |
| 2155 | }; |
| 2156 | |
| 2157 | static int backref_comp(struct sa_defrag_extent_backref *b1, |
| 2158 | struct sa_defrag_extent_backref *b2) |
| 2159 | { |
| 2160 | if (b1->root_id < b2->root_id) |
| 2161 | return -1; |
| 2162 | else if (b1->root_id > b2->root_id) |
| 2163 | return 1; |
| 2164 | |
| 2165 | if (b1->inum < b2->inum) |
| 2166 | return -1; |
| 2167 | else if (b1->inum > b2->inum) |
| 2168 | return 1; |
| 2169 | |
| 2170 | if (b1->file_pos < b2->file_pos) |
| 2171 | return -1; |
| 2172 | else if (b1->file_pos > b2->file_pos) |
| 2173 | return 1; |
| 2174 | |
| 2175 | /* |
| 2176 | * [------------------------------] ===> (a range of space) |
| 2177 | * |<--->| |<---->| =============> (fs/file tree A) |
| 2178 | * |<---------------------------->| ===> (fs/file tree B) |
| 2179 | * |
| 2180 | * A range of space can refer to two file extents in one tree while |
| 2181 | * refer to only one file extent in another tree. |
| 2182 | * |
| 2183 | * So we may process a disk offset more than one time(two extents in A) |
| 2184 | * and locate at the same extent(one extent in B), then insert two same |
| 2185 | * backrefs(both refer to the extent in B). |
| 2186 | */ |
| 2187 | return 0; |
| 2188 | } |
| 2189 | |
| 2190 | static void backref_insert(struct rb_root *root, |
| 2191 | struct sa_defrag_extent_backref *backref) |
| 2192 | { |
| 2193 | struct rb_node **p = &root->rb_node; |
| 2194 | struct rb_node *parent = NULL; |
| 2195 | struct sa_defrag_extent_backref *entry; |
| 2196 | int ret; |
| 2197 | |
| 2198 | while (*p) { |
| 2199 | parent = *p; |
| 2200 | entry = rb_entry(parent, struct sa_defrag_extent_backref, node); |
| 2201 | |
| 2202 | ret = backref_comp(backref, entry); |
| 2203 | if (ret < 0) |
| 2204 | p = &(*p)->rb_left; |
| 2205 | else |
| 2206 | p = &(*p)->rb_right; |
| 2207 | } |
| 2208 | |
| 2209 | rb_link_node(&backref->node, parent, p); |
| 2210 | rb_insert_color(&backref->node, root); |
| 2211 | } |
| 2212 | |
| 2213 | /* |
| 2214 | * Note the backref might has changed, and in this case we just return 0. |
| 2215 | */ |
| 2216 | static noinline int record_one_backref(u64 inum, u64 offset, u64 root_id, |
| 2217 | void *ctx) |
| 2218 | { |
| 2219 | struct btrfs_file_extent_item *extent; |
| 2220 | struct btrfs_fs_info *fs_info; |
| 2221 | struct old_sa_defrag_extent *old = ctx; |
| 2222 | struct new_sa_defrag_extent *new = old->new; |
| 2223 | struct btrfs_path *path = new->path; |
| 2224 | struct btrfs_key key; |
| 2225 | struct btrfs_root *root; |
| 2226 | struct sa_defrag_extent_backref *backref; |
| 2227 | struct extent_buffer *leaf; |
| 2228 | struct inode *inode = new->inode; |
| 2229 | int slot; |
| 2230 | int ret; |
| 2231 | u64 extent_offset; |
| 2232 | u64 num_bytes; |
| 2233 | |
| 2234 | if (BTRFS_I(inode)->root->root_key.objectid == root_id && |
| 2235 | inum == btrfs_ino(inode)) |
| 2236 | return 0; |
| 2237 | |
| 2238 | key.objectid = root_id; |
| 2239 | key.type = BTRFS_ROOT_ITEM_KEY; |
| 2240 | key.offset = (u64)-1; |
| 2241 | |
| 2242 | fs_info = BTRFS_I(inode)->root->fs_info; |
| 2243 | root = btrfs_read_fs_root_no_name(fs_info, &key); |
| 2244 | if (IS_ERR(root)) { |
| 2245 | if (PTR_ERR(root) == -ENOENT) |
| 2246 | return 0; |
| 2247 | WARN_ON(1); |
| 2248 | pr_debug("inum=%llu, offset=%llu, root_id=%llu\n", |
| 2249 | inum, offset, root_id); |
| 2250 | return PTR_ERR(root); |
| 2251 | } |
| 2252 | |
| 2253 | key.objectid = inum; |
| 2254 | key.type = BTRFS_EXTENT_DATA_KEY; |
| 2255 | if (offset > (u64)-1 << 32) |
| 2256 | key.offset = 0; |
| 2257 | else |
| 2258 | key.offset = offset; |
| 2259 | |
| 2260 | ret = btrfs_search_slot(NULL, root, &key, path, 0, 0); |
Dulshani Gunawardhana | fae7f21 | 2013-10-31 10:30:08 +0530 | [diff] [blame] | 2261 | if (WARN_ON(ret < 0)) |
Liu Bo | 38c227d | 2013-01-29 03:18:40 +0000 | [diff] [blame] | 2262 | return ret; |
Josef Bacik | 50f1319 | 2013-07-22 12:50:37 -0400 | [diff] [blame] | 2263 | ret = 0; |
Liu Bo | 38c227d | 2013-01-29 03:18:40 +0000 | [diff] [blame] | 2264 | |
| 2265 | while (1) { |
| 2266 | cond_resched(); |
| 2267 | |
| 2268 | leaf = path->nodes[0]; |
| 2269 | slot = path->slots[0]; |
| 2270 | |
| 2271 | if (slot >= btrfs_header_nritems(leaf)) { |
| 2272 | ret = btrfs_next_leaf(root, path); |
| 2273 | if (ret < 0) { |
| 2274 | goto out; |
| 2275 | } else if (ret > 0) { |
| 2276 | ret = 0; |
| 2277 | goto out; |
| 2278 | } |
| 2279 | continue; |
| 2280 | } |
| 2281 | |
| 2282 | path->slots[0]++; |
| 2283 | |
| 2284 | btrfs_item_key_to_cpu(leaf, &key, slot); |
| 2285 | |
| 2286 | if (key.objectid > inum) |
| 2287 | goto out; |
| 2288 | |
| 2289 | if (key.objectid < inum || key.type != BTRFS_EXTENT_DATA_KEY) |
| 2290 | continue; |
| 2291 | |
| 2292 | extent = btrfs_item_ptr(leaf, slot, |
| 2293 | struct btrfs_file_extent_item); |
| 2294 | |
| 2295 | if (btrfs_file_extent_disk_bytenr(leaf, extent) != old->bytenr) |
| 2296 | continue; |
| 2297 | |
Liu Bo | e68afa4 | 2013-07-01 22:13:26 +0800 | [diff] [blame] | 2298 | /* |
| 2299 | * 'offset' refers to the exact key.offset, |
| 2300 | * NOT the 'offset' field in btrfs_extent_data_ref, ie. |
| 2301 | * (key.offset - extent_offset). |
| 2302 | */ |
| 2303 | if (key.offset != offset) |
Liu Bo | 38c227d | 2013-01-29 03:18:40 +0000 | [diff] [blame] | 2304 | continue; |
| 2305 | |
Liu Bo | e68afa4 | 2013-07-01 22:13:26 +0800 | [diff] [blame] | 2306 | extent_offset = btrfs_file_extent_offset(leaf, extent); |
Liu Bo | 38c227d | 2013-01-29 03:18:40 +0000 | [diff] [blame] | 2307 | num_bytes = btrfs_file_extent_num_bytes(leaf, extent); |
Liu Bo | e68afa4 | 2013-07-01 22:13:26 +0800 | [diff] [blame] | 2308 | |
Liu Bo | 38c227d | 2013-01-29 03:18:40 +0000 | [diff] [blame] | 2309 | if (extent_offset >= old->extent_offset + old->offset + |
| 2310 | old->len || extent_offset + num_bytes <= |
| 2311 | old->extent_offset + old->offset) |
| 2312 | continue; |
Liu Bo | 38c227d | 2013-01-29 03:18:40 +0000 | [diff] [blame] | 2313 | break; |
| 2314 | } |
| 2315 | |
| 2316 | backref = kmalloc(sizeof(*backref), GFP_NOFS); |
| 2317 | if (!backref) { |
| 2318 | ret = -ENOENT; |
| 2319 | goto out; |
| 2320 | } |
| 2321 | |
| 2322 | backref->root_id = root_id; |
| 2323 | backref->inum = inum; |
Liu Bo | e68afa4 | 2013-07-01 22:13:26 +0800 | [diff] [blame] | 2324 | backref->file_pos = offset; |
Liu Bo | 38c227d | 2013-01-29 03:18:40 +0000 | [diff] [blame] | 2325 | backref->num_bytes = num_bytes; |
| 2326 | backref->extent_offset = extent_offset; |
| 2327 | backref->generation = btrfs_file_extent_generation(leaf, extent); |
| 2328 | backref->old = old; |
| 2329 | backref_insert(&new->root, backref); |
| 2330 | old->count++; |
| 2331 | out: |
| 2332 | btrfs_release_path(path); |
| 2333 | WARN_ON(ret); |
| 2334 | return ret; |
| 2335 | } |
| 2336 | |
| 2337 | static noinline bool record_extent_backrefs(struct btrfs_path *path, |
| 2338 | struct new_sa_defrag_extent *new) |
| 2339 | { |
| 2340 | struct btrfs_fs_info *fs_info = BTRFS_I(new->inode)->root->fs_info; |
| 2341 | struct old_sa_defrag_extent *old, *tmp; |
| 2342 | int ret; |
| 2343 | |
| 2344 | new->path = path; |
| 2345 | |
| 2346 | list_for_each_entry_safe(old, tmp, &new->head, list) { |
Liu Bo | e68afa4 | 2013-07-01 22:13:26 +0800 | [diff] [blame] | 2347 | ret = iterate_inodes_from_logical(old->bytenr + |
| 2348 | old->extent_offset, fs_info, |
Liu Bo | 38c227d | 2013-01-29 03:18:40 +0000 | [diff] [blame] | 2349 | path, record_one_backref, |
| 2350 | old); |
Josef Bacik | 4724b10 | 2013-11-05 11:11:40 -0500 | [diff] [blame] | 2351 | if (ret < 0 && ret != -ENOENT) |
| 2352 | return false; |
Liu Bo | 38c227d | 2013-01-29 03:18:40 +0000 | [diff] [blame] | 2353 | |
| 2354 | /* no backref to be processed for this extent */ |
| 2355 | if (!old->count) { |
| 2356 | list_del(&old->list); |
| 2357 | kfree(old); |
| 2358 | } |
| 2359 | } |
| 2360 | |
| 2361 | if (list_empty(&new->head)) |
| 2362 | return false; |
| 2363 | |
| 2364 | return true; |
| 2365 | } |
| 2366 | |
| 2367 | static int relink_is_mergable(struct extent_buffer *leaf, |
| 2368 | struct btrfs_file_extent_item *fi, |
Liu Bo | 116e002 | 2013-08-02 16:30:40 +0800 | [diff] [blame] | 2369 | struct new_sa_defrag_extent *new) |
Liu Bo | 38c227d | 2013-01-29 03:18:40 +0000 | [diff] [blame] | 2370 | { |
Liu Bo | 116e002 | 2013-08-02 16:30:40 +0800 | [diff] [blame] | 2371 | if (btrfs_file_extent_disk_bytenr(leaf, fi) != new->bytenr) |
Liu Bo | 38c227d | 2013-01-29 03:18:40 +0000 | [diff] [blame] | 2372 | return 0; |
| 2373 | |
| 2374 | if (btrfs_file_extent_type(leaf, fi) != BTRFS_FILE_EXTENT_REG) |
| 2375 | return 0; |
| 2376 | |
Liu Bo | 116e002 | 2013-08-02 16:30:40 +0800 | [diff] [blame] | 2377 | if (btrfs_file_extent_compression(leaf, fi) != new->compress_type) |
| 2378 | return 0; |
| 2379 | |
| 2380 | if (btrfs_file_extent_encryption(leaf, fi) || |
Liu Bo | 38c227d | 2013-01-29 03:18:40 +0000 | [diff] [blame] | 2381 | btrfs_file_extent_other_encoding(leaf, fi)) |
| 2382 | return 0; |
| 2383 | |
| 2384 | return 1; |
| 2385 | } |
| 2386 | |
| 2387 | /* |
| 2388 | * Note the backref might has changed, and in this case we just return 0. |
| 2389 | */ |
| 2390 | static noinline int relink_extent_backref(struct btrfs_path *path, |
| 2391 | struct sa_defrag_extent_backref *prev, |
| 2392 | struct sa_defrag_extent_backref *backref) |
| 2393 | { |
| 2394 | struct btrfs_file_extent_item *extent; |
| 2395 | struct btrfs_file_extent_item *item; |
| 2396 | struct btrfs_ordered_extent *ordered; |
| 2397 | struct btrfs_trans_handle *trans; |
| 2398 | struct btrfs_fs_info *fs_info; |
| 2399 | struct btrfs_root *root; |
| 2400 | struct btrfs_key key; |
| 2401 | struct extent_buffer *leaf; |
| 2402 | struct old_sa_defrag_extent *old = backref->old; |
| 2403 | struct new_sa_defrag_extent *new = old->new; |
| 2404 | struct inode *src_inode = new->inode; |
| 2405 | struct inode *inode; |
| 2406 | struct extent_state *cached = NULL; |
| 2407 | int ret = 0; |
| 2408 | u64 start; |
| 2409 | u64 len; |
| 2410 | u64 lock_start; |
| 2411 | u64 lock_end; |
| 2412 | bool merge = false; |
| 2413 | int index; |
| 2414 | |
| 2415 | if (prev && prev->root_id == backref->root_id && |
| 2416 | prev->inum == backref->inum && |
| 2417 | prev->file_pos + prev->num_bytes == backref->file_pos) |
| 2418 | merge = true; |
| 2419 | |
| 2420 | /* step 1: get root */ |
| 2421 | key.objectid = backref->root_id; |
| 2422 | key.type = BTRFS_ROOT_ITEM_KEY; |
| 2423 | key.offset = (u64)-1; |
| 2424 | |
| 2425 | fs_info = BTRFS_I(src_inode)->root->fs_info; |
| 2426 | index = srcu_read_lock(&fs_info->subvol_srcu); |
| 2427 | |
| 2428 | root = btrfs_read_fs_root_no_name(fs_info, &key); |
| 2429 | if (IS_ERR(root)) { |
| 2430 | srcu_read_unlock(&fs_info->subvol_srcu, index); |
| 2431 | if (PTR_ERR(root) == -ENOENT) |
| 2432 | return 0; |
| 2433 | return PTR_ERR(root); |
| 2434 | } |
Liu Bo | 38c227d | 2013-01-29 03:18:40 +0000 | [diff] [blame] | 2435 | |
Wang Shilong | bcbba5e | 2014-02-08 23:46:35 +0800 | [diff] [blame] | 2436 | if (btrfs_root_readonly(root)) { |
| 2437 | srcu_read_unlock(&fs_info->subvol_srcu, index); |
| 2438 | return 0; |
| 2439 | } |
| 2440 | |
Liu Bo | 38c227d | 2013-01-29 03:18:40 +0000 | [diff] [blame] | 2441 | /* step 2: get inode */ |
| 2442 | key.objectid = backref->inum; |
| 2443 | key.type = BTRFS_INODE_ITEM_KEY; |
| 2444 | key.offset = 0; |
| 2445 | |
| 2446 | inode = btrfs_iget(fs_info->sb, &key, root, NULL); |
| 2447 | if (IS_ERR(inode)) { |
| 2448 | srcu_read_unlock(&fs_info->subvol_srcu, index); |
| 2449 | return 0; |
| 2450 | } |
| 2451 | |
| 2452 | srcu_read_unlock(&fs_info->subvol_srcu, index); |
| 2453 | |
| 2454 | /* step 3: relink backref */ |
| 2455 | lock_start = backref->file_pos; |
| 2456 | lock_end = backref->file_pos + backref->num_bytes - 1; |
| 2457 | lock_extent_bits(&BTRFS_I(inode)->io_tree, lock_start, lock_end, |
| 2458 | 0, &cached); |
| 2459 | |
| 2460 | ordered = btrfs_lookup_first_ordered_extent(inode, lock_end); |
| 2461 | if (ordered) { |
| 2462 | btrfs_put_ordered_extent(ordered); |
| 2463 | goto out_unlock; |
| 2464 | } |
| 2465 | |
| 2466 | trans = btrfs_join_transaction(root); |
| 2467 | if (IS_ERR(trans)) { |
| 2468 | ret = PTR_ERR(trans); |
| 2469 | goto out_unlock; |
| 2470 | } |
| 2471 | |
| 2472 | key.objectid = backref->inum; |
| 2473 | key.type = BTRFS_EXTENT_DATA_KEY; |
| 2474 | key.offset = backref->file_pos; |
| 2475 | |
| 2476 | ret = btrfs_search_slot(NULL, root, &key, path, 0, 0); |
| 2477 | if (ret < 0) { |
| 2478 | goto out_free_path; |
| 2479 | } else if (ret > 0) { |
| 2480 | ret = 0; |
| 2481 | goto out_free_path; |
| 2482 | } |
| 2483 | |
| 2484 | extent = btrfs_item_ptr(path->nodes[0], path->slots[0], |
| 2485 | struct btrfs_file_extent_item); |
| 2486 | |
| 2487 | if (btrfs_file_extent_generation(path->nodes[0], extent) != |
| 2488 | backref->generation) |
| 2489 | goto out_free_path; |
| 2490 | |
| 2491 | btrfs_release_path(path); |
| 2492 | |
| 2493 | start = backref->file_pos; |
| 2494 | if (backref->extent_offset < old->extent_offset + old->offset) |
| 2495 | start += old->extent_offset + old->offset - |
| 2496 | backref->extent_offset; |
| 2497 | |
| 2498 | len = min(backref->extent_offset + backref->num_bytes, |
| 2499 | old->extent_offset + old->offset + old->len); |
| 2500 | len -= max(backref->extent_offset, old->extent_offset + old->offset); |
| 2501 | |
| 2502 | ret = btrfs_drop_extents(trans, root, inode, start, |
| 2503 | start + len, 1); |
| 2504 | if (ret) |
| 2505 | goto out_free_path; |
| 2506 | again: |
| 2507 | key.objectid = btrfs_ino(inode); |
| 2508 | key.type = BTRFS_EXTENT_DATA_KEY; |
| 2509 | key.offset = start; |
| 2510 | |
Liu Bo | a09a0a7 | 2013-03-11 09:20:58 +0000 | [diff] [blame] | 2511 | path->leave_spinning = 1; |
Liu Bo | 38c227d | 2013-01-29 03:18:40 +0000 | [diff] [blame] | 2512 | if (merge) { |
| 2513 | struct btrfs_file_extent_item *fi; |
| 2514 | u64 extent_len; |
| 2515 | struct btrfs_key found_key; |
| 2516 | |
Gui Hecheng | 3c9665d | 2014-01-23 13:41:09 +0800 | [diff] [blame] | 2517 | ret = btrfs_search_slot(trans, root, &key, path, 0, 1); |
Liu Bo | 38c227d | 2013-01-29 03:18:40 +0000 | [diff] [blame] | 2518 | if (ret < 0) |
| 2519 | goto out_free_path; |
| 2520 | |
| 2521 | path->slots[0]--; |
| 2522 | leaf = path->nodes[0]; |
| 2523 | btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]); |
| 2524 | |
| 2525 | fi = btrfs_item_ptr(leaf, path->slots[0], |
| 2526 | struct btrfs_file_extent_item); |
| 2527 | extent_len = btrfs_file_extent_num_bytes(leaf, fi); |
| 2528 | |
Liu Bo | 116e002 | 2013-08-02 16:30:40 +0800 | [diff] [blame] | 2529 | if (extent_len + found_key.offset == start && |
| 2530 | relink_is_mergable(leaf, fi, new)) { |
Liu Bo | 38c227d | 2013-01-29 03:18:40 +0000 | [diff] [blame] | 2531 | btrfs_set_file_extent_num_bytes(leaf, fi, |
| 2532 | extent_len + len); |
| 2533 | btrfs_mark_buffer_dirty(leaf); |
| 2534 | inode_add_bytes(inode, len); |
| 2535 | |
| 2536 | ret = 1; |
| 2537 | goto out_free_path; |
| 2538 | } else { |
| 2539 | merge = false; |
| 2540 | btrfs_release_path(path); |
| 2541 | goto again; |
| 2542 | } |
| 2543 | } |
| 2544 | |
| 2545 | ret = btrfs_insert_empty_item(trans, root, path, &key, |
| 2546 | sizeof(*extent)); |
| 2547 | if (ret) { |
| 2548 | btrfs_abort_transaction(trans, root, ret); |
| 2549 | goto out_free_path; |
| 2550 | } |
| 2551 | |
| 2552 | leaf = path->nodes[0]; |
| 2553 | item = btrfs_item_ptr(leaf, path->slots[0], |
| 2554 | struct btrfs_file_extent_item); |
| 2555 | btrfs_set_file_extent_disk_bytenr(leaf, item, new->bytenr); |
| 2556 | btrfs_set_file_extent_disk_num_bytes(leaf, item, new->disk_len); |
| 2557 | btrfs_set_file_extent_offset(leaf, item, start - new->file_pos); |
| 2558 | btrfs_set_file_extent_num_bytes(leaf, item, len); |
| 2559 | btrfs_set_file_extent_ram_bytes(leaf, item, new->len); |
| 2560 | btrfs_set_file_extent_generation(leaf, item, trans->transid); |
| 2561 | btrfs_set_file_extent_type(leaf, item, BTRFS_FILE_EXTENT_REG); |
| 2562 | btrfs_set_file_extent_compression(leaf, item, new->compress_type); |
| 2563 | btrfs_set_file_extent_encryption(leaf, item, 0); |
| 2564 | btrfs_set_file_extent_other_encoding(leaf, item, 0); |
| 2565 | |
| 2566 | btrfs_mark_buffer_dirty(leaf); |
| 2567 | inode_add_bytes(inode, len); |
Liu Bo | a09a0a7 | 2013-03-11 09:20:58 +0000 | [diff] [blame] | 2568 | btrfs_release_path(path); |
Liu Bo | 38c227d | 2013-01-29 03:18:40 +0000 | [diff] [blame] | 2569 | |
| 2570 | ret = btrfs_inc_extent_ref(trans, root, new->bytenr, |
| 2571 | new->disk_len, 0, |
| 2572 | backref->root_id, backref->inum, |
| 2573 | new->file_pos, 0); /* start - extent_offset */ |
| 2574 | if (ret) { |
| 2575 | btrfs_abort_transaction(trans, root, ret); |
| 2576 | goto out_free_path; |
| 2577 | } |
| 2578 | |
| 2579 | ret = 1; |
| 2580 | out_free_path: |
| 2581 | btrfs_release_path(path); |
Liu Bo | a09a0a7 | 2013-03-11 09:20:58 +0000 | [diff] [blame] | 2582 | path->leave_spinning = 0; |
Liu Bo | 38c227d | 2013-01-29 03:18:40 +0000 | [diff] [blame] | 2583 | btrfs_end_transaction(trans, root); |
| 2584 | out_unlock: |
| 2585 | unlock_extent_cached(&BTRFS_I(inode)->io_tree, lock_start, lock_end, |
| 2586 | &cached, GFP_NOFS); |
| 2587 | iput(inode); |
| 2588 | return ret; |
| 2589 | } |
| 2590 | |
Liu Bo | 6f51956 | 2013-10-29 10:45:05 +0800 | [diff] [blame] | 2591 | static void free_sa_defrag_extent(struct new_sa_defrag_extent *new) |
| 2592 | { |
| 2593 | struct old_sa_defrag_extent *old, *tmp; |
| 2594 | |
| 2595 | if (!new) |
| 2596 | return; |
| 2597 | |
| 2598 | list_for_each_entry_safe(old, tmp, &new->head, list) { |
| 2599 | list_del(&old->list); |
| 2600 | kfree(old); |
| 2601 | } |
| 2602 | kfree(new); |
| 2603 | } |
| 2604 | |
Liu Bo | 38c227d | 2013-01-29 03:18:40 +0000 | [diff] [blame] | 2605 | static void relink_file_extents(struct new_sa_defrag_extent *new) |
| 2606 | { |
| 2607 | struct btrfs_path *path; |
Liu Bo | 38c227d | 2013-01-29 03:18:40 +0000 | [diff] [blame] | 2608 | struct sa_defrag_extent_backref *backref; |
| 2609 | struct sa_defrag_extent_backref *prev = NULL; |
| 2610 | struct inode *inode; |
| 2611 | struct btrfs_root *root; |
| 2612 | struct rb_node *node; |
| 2613 | int ret; |
| 2614 | |
| 2615 | inode = new->inode; |
| 2616 | root = BTRFS_I(inode)->root; |
| 2617 | |
| 2618 | path = btrfs_alloc_path(); |
| 2619 | if (!path) |
| 2620 | return; |
| 2621 | |
| 2622 | if (!record_extent_backrefs(path, new)) { |
| 2623 | btrfs_free_path(path); |
| 2624 | goto out; |
| 2625 | } |
| 2626 | btrfs_release_path(path); |
| 2627 | |
| 2628 | while (1) { |
| 2629 | node = rb_first(&new->root); |
| 2630 | if (!node) |
| 2631 | break; |
| 2632 | rb_erase(node, &new->root); |
| 2633 | |
| 2634 | backref = rb_entry(node, struct sa_defrag_extent_backref, node); |
| 2635 | |
| 2636 | ret = relink_extent_backref(path, prev, backref); |
| 2637 | WARN_ON(ret < 0); |
| 2638 | |
| 2639 | kfree(prev); |
| 2640 | |
| 2641 | if (ret == 1) |
| 2642 | prev = backref; |
| 2643 | else |
| 2644 | prev = NULL; |
| 2645 | cond_resched(); |
| 2646 | } |
| 2647 | kfree(prev); |
| 2648 | |
| 2649 | btrfs_free_path(path); |
Liu Bo | 38c227d | 2013-01-29 03:18:40 +0000 | [diff] [blame] | 2650 | out: |
Liu Bo | 6f51956 | 2013-10-29 10:45:05 +0800 | [diff] [blame] | 2651 | free_sa_defrag_extent(new); |
| 2652 | |
Liu Bo | 38c227d | 2013-01-29 03:18:40 +0000 | [diff] [blame] | 2653 | atomic_dec(&root->fs_info->defrag_running); |
| 2654 | wake_up(&root->fs_info->transaction_wait); |
Liu Bo | 38c227d | 2013-01-29 03:18:40 +0000 | [diff] [blame] | 2655 | } |
| 2656 | |
| 2657 | static struct new_sa_defrag_extent * |
| 2658 | record_old_file_extents(struct inode *inode, |
| 2659 | struct btrfs_ordered_extent *ordered) |
| 2660 | { |
| 2661 | struct btrfs_root *root = BTRFS_I(inode)->root; |
| 2662 | struct btrfs_path *path; |
| 2663 | struct btrfs_key key; |
Liu Bo | 6f51956 | 2013-10-29 10:45:05 +0800 | [diff] [blame] | 2664 | struct old_sa_defrag_extent *old; |
Liu Bo | 38c227d | 2013-01-29 03:18:40 +0000 | [diff] [blame] | 2665 | struct new_sa_defrag_extent *new; |
| 2666 | int ret; |
| 2667 | |
| 2668 | new = kmalloc(sizeof(*new), GFP_NOFS); |
| 2669 | if (!new) |
| 2670 | return NULL; |
| 2671 | |
| 2672 | new->inode = inode; |
| 2673 | new->file_pos = ordered->file_offset; |
| 2674 | new->len = ordered->len; |
| 2675 | new->bytenr = ordered->start; |
| 2676 | new->disk_len = ordered->disk_len; |
| 2677 | new->compress_type = ordered->compress_type; |
| 2678 | new->root = RB_ROOT; |
| 2679 | INIT_LIST_HEAD(&new->head); |
| 2680 | |
| 2681 | path = btrfs_alloc_path(); |
| 2682 | if (!path) |
| 2683 | goto out_kfree; |
| 2684 | |
| 2685 | key.objectid = btrfs_ino(inode); |
| 2686 | key.type = BTRFS_EXTENT_DATA_KEY; |
| 2687 | key.offset = new->file_pos; |
| 2688 | |
| 2689 | ret = btrfs_search_slot(NULL, root, &key, path, 0, 0); |
| 2690 | if (ret < 0) |
| 2691 | goto out_free_path; |
| 2692 | if (ret > 0 && path->slots[0] > 0) |
| 2693 | path->slots[0]--; |
| 2694 | |
| 2695 | /* find out all the old extents for the file range */ |
| 2696 | while (1) { |
| 2697 | struct btrfs_file_extent_item *extent; |
| 2698 | struct extent_buffer *l; |
| 2699 | int slot; |
| 2700 | u64 num_bytes; |
| 2701 | u64 offset; |
| 2702 | u64 end; |
| 2703 | u64 disk_bytenr; |
| 2704 | u64 extent_offset; |
| 2705 | |
| 2706 | l = path->nodes[0]; |
| 2707 | slot = path->slots[0]; |
| 2708 | |
| 2709 | if (slot >= btrfs_header_nritems(l)) { |
| 2710 | ret = btrfs_next_leaf(root, path); |
| 2711 | if (ret < 0) |
Liu Bo | 6f51956 | 2013-10-29 10:45:05 +0800 | [diff] [blame] | 2712 | goto out_free_path; |
Liu Bo | 38c227d | 2013-01-29 03:18:40 +0000 | [diff] [blame] | 2713 | else if (ret > 0) |
| 2714 | break; |
| 2715 | continue; |
| 2716 | } |
| 2717 | |
| 2718 | btrfs_item_key_to_cpu(l, &key, slot); |
| 2719 | |
| 2720 | if (key.objectid != btrfs_ino(inode)) |
| 2721 | break; |
| 2722 | if (key.type != BTRFS_EXTENT_DATA_KEY) |
| 2723 | break; |
| 2724 | if (key.offset >= new->file_pos + new->len) |
| 2725 | break; |
| 2726 | |
| 2727 | extent = btrfs_item_ptr(l, slot, struct btrfs_file_extent_item); |
| 2728 | |
| 2729 | num_bytes = btrfs_file_extent_num_bytes(l, extent); |
| 2730 | if (key.offset + num_bytes < new->file_pos) |
| 2731 | goto next; |
| 2732 | |
| 2733 | disk_bytenr = btrfs_file_extent_disk_bytenr(l, extent); |
| 2734 | if (!disk_bytenr) |
| 2735 | goto next; |
| 2736 | |
| 2737 | extent_offset = btrfs_file_extent_offset(l, extent); |
| 2738 | |
| 2739 | old = kmalloc(sizeof(*old), GFP_NOFS); |
| 2740 | if (!old) |
Liu Bo | 6f51956 | 2013-10-29 10:45:05 +0800 | [diff] [blame] | 2741 | goto out_free_path; |
Liu Bo | 38c227d | 2013-01-29 03:18:40 +0000 | [diff] [blame] | 2742 | |
| 2743 | offset = max(new->file_pos, key.offset); |
| 2744 | end = min(new->file_pos + new->len, key.offset + num_bytes); |
| 2745 | |
| 2746 | old->bytenr = disk_bytenr; |
| 2747 | old->extent_offset = extent_offset; |
| 2748 | old->offset = offset - key.offset; |
| 2749 | old->len = end - offset; |
| 2750 | old->new = new; |
| 2751 | old->count = 0; |
| 2752 | list_add_tail(&old->list, &new->head); |
| 2753 | next: |
| 2754 | path->slots[0]++; |
| 2755 | cond_resched(); |
| 2756 | } |
| 2757 | |
| 2758 | btrfs_free_path(path); |
| 2759 | atomic_inc(&root->fs_info->defrag_running); |
| 2760 | |
| 2761 | return new; |
| 2762 | |
Liu Bo | 38c227d | 2013-01-29 03:18:40 +0000 | [diff] [blame] | 2763 | out_free_path: |
| 2764 | btrfs_free_path(path); |
| 2765 | out_kfree: |
Liu Bo | 6f51956 | 2013-10-29 10:45:05 +0800 | [diff] [blame] | 2766 | free_sa_defrag_extent(new); |
Liu Bo | 38c227d | 2013-01-29 03:18:40 +0000 | [diff] [blame] | 2767 | return NULL; |
| 2768 | } |
| 2769 | |
Miao Xie | e570fd2 | 2014-06-19 10:42:50 +0800 | [diff] [blame] | 2770 | static void btrfs_release_delalloc_bytes(struct btrfs_root *root, |
| 2771 | u64 start, u64 len) |
| 2772 | { |
| 2773 | struct btrfs_block_group_cache *cache; |
| 2774 | |
| 2775 | cache = btrfs_lookup_block_group(root->fs_info, start); |
| 2776 | ASSERT(cache); |
| 2777 | |
| 2778 | spin_lock(&cache->lock); |
| 2779 | cache->delalloc_bytes -= len; |
| 2780 | spin_unlock(&cache->lock); |
| 2781 | |
| 2782 | btrfs_put_block_group(cache); |
| 2783 | } |
| 2784 | |
Chris Mason | d352ac6 | 2008-09-29 15:18:18 -0400 | [diff] [blame] | 2785 | /* as ordered data IO finishes, this gets called so we can finish |
| 2786 | * an ordered extent if the range of bytes in the file it covers are |
| 2787 | * fully written. |
| 2788 | */ |
Josef Bacik | 5fd0204 | 2012-05-02 14:00:54 -0400 | [diff] [blame] | 2789 | static int btrfs_finish_ordered_io(struct btrfs_ordered_extent *ordered_extent) |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 2790 | { |
Josef Bacik | 5fd0204 | 2012-05-02 14:00:54 -0400 | [diff] [blame] | 2791 | struct inode *inode = ordered_extent->inode; |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 2792 | struct btrfs_root *root = BTRFS_I(inode)->root; |
Yan, Zheng | 0ca1f7c | 2010-05-16 10:48:47 -0400 | [diff] [blame] | 2793 | struct btrfs_trans_handle *trans = NULL; |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 2794 | struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree; |
Josef Bacik | 2ac55d4 | 2010-02-03 19:33:23 +0000 | [diff] [blame] | 2795 | struct extent_state *cached_state = NULL; |
Liu Bo | 38c227d | 2013-01-29 03:18:40 +0000 | [diff] [blame] | 2796 | struct new_sa_defrag_extent *new = NULL; |
Li Zefan | 261507a0 | 2010-12-17 14:21:50 +0800 | [diff] [blame] | 2797 | int compress_type = 0; |
Josef Bacik | 77cef2e | 2013-08-29 13:57:21 -0400 | [diff] [blame] | 2798 | int ret = 0; |
| 2799 | u64 logical_len = ordered_extent->len; |
Li Zefan | 82d5902 | 2011-04-20 10:33:24 +0800 | [diff] [blame] | 2800 | bool nolock; |
Josef Bacik | 77cef2e | 2013-08-29 13:57:21 -0400 | [diff] [blame] | 2801 | bool truncated = false; |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 2802 | |
Liu Bo | 83eea1f | 2012-07-10 05:28:39 -0600 | [diff] [blame] | 2803 | nolock = btrfs_is_free_space_inode(inode); |
Josef Bacik | 0cb59c9 | 2010-07-02 12:14:14 -0400 | [diff] [blame] | 2804 | |
Josef Bacik | 5fd0204 | 2012-05-02 14:00:54 -0400 | [diff] [blame] | 2805 | if (test_bit(BTRFS_ORDERED_IOERR, &ordered_extent->flags)) { |
| 2806 | ret = -EIO; |
| 2807 | goto out; |
| 2808 | } |
| 2809 | |
Miao Xie | f612496 | 2014-09-12 18:44:04 +0800 | [diff] [blame] | 2810 | btrfs_free_io_failure_record(inode, ordered_extent->file_offset, |
| 2811 | ordered_extent->file_offset + |
| 2812 | ordered_extent->len - 1); |
| 2813 | |
Josef Bacik | 77cef2e | 2013-08-29 13:57:21 -0400 | [diff] [blame] | 2814 | if (test_bit(BTRFS_ORDERED_TRUNCATED, &ordered_extent->flags)) { |
| 2815 | truncated = true; |
| 2816 | logical_len = ordered_extent->truncated_len; |
| 2817 | /* Truncated the entire extent, don't bother adding */ |
| 2818 | if (!logical_len) |
| 2819 | goto out; |
| 2820 | } |
| 2821 | |
Yan, Zheng | c216775 | 2009-11-12 09:34:21 +0000 | [diff] [blame] | 2822 | if (test_bit(BTRFS_ORDERED_NOCOW, &ordered_extent->flags)) { |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 2823 | BUG_ON(!list_empty(&ordered_extent->list)); /* Logic error */ |
Josef Bacik | 6c760c0 | 2012-11-09 10:53:21 -0500 | [diff] [blame] | 2824 | btrfs_ordered_update_i_size(inode, 0, ordered_extent); |
| 2825 | if (nolock) |
| 2826 | trans = btrfs_join_transaction_nolock(root); |
| 2827 | else |
| 2828 | trans = btrfs_join_transaction(root); |
| 2829 | if (IS_ERR(trans)) { |
| 2830 | ret = PTR_ERR(trans); |
| 2831 | trans = NULL; |
| 2832 | goto out; |
Yan, Zheng | c216775 | 2009-11-12 09:34:21 +0000 | [diff] [blame] | 2833 | } |
Josef Bacik | 6c760c0 | 2012-11-09 10:53:21 -0500 | [diff] [blame] | 2834 | trans->block_rsv = &root->fs_info->delalloc_block_rsv; |
| 2835 | ret = btrfs_update_inode_fallback(trans, root, inode); |
| 2836 | if (ret) /* -ENOMEM or corruption */ |
| 2837 | btrfs_abort_transaction(trans, root, ret); |
Yan, Zheng | c216775 | 2009-11-12 09:34:21 +0000 | [diff] [blame] | 2838 | goto out; |
| 2839 | } |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 2840 | |
Josef Bacik | 2ac55d4 | 2010-02-03 19:33:23 +0000 | [diff] [blame] | 2841 | lock_extent_bits(io_tree, ordered_extent->file_offset, |
| 2842 | ordered_extent->file_offset + ordered_extent->len - 1, |
Jeff Mahoney | d008237 | 2012-03-01 14:57:19 +0100 | [diff] [blame] | 2843 | 0, &cached_state); |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 2844 | |
Liu Bo | 38c227d | 2013-01-29 03:18:40 +0000 | [diff] [blame] | 2845 | ret = test_range_bit(io_tree, ordered_extent->file_offset, |
| 2846 | ordered_extent->file_offset + ordered_extent->len - 1, |
| 2847 | EXTENT_DEFRAG, 1, cached_state); |
| 2848 | if (ret) { |
| 2849 | u64 last_snapshot = btrfs_root_last_snapshot(&root->root_item); |
Josef Bacik | 8101c8d | 2014-01-29 16:05:30 -0500 | [diff] [blame] | 2850 | if (0 && last_snapshot >= BTRFS_I(inode)->generation) |
Liu Bo | 38c227d | 2013-01-29 03:18:40 +0000 | [diff] [blame] | 2851 | /* the inode is shared */ |
| 2852 | new = record_old_file_extents(inode, ordered_extent); |
| 2853 | |
| 2854 | clear_extent_bit(io_tree, ordered_extent->file_offset, |
| 2855 | ordered_extent->file_offset + ordered_extent->len - 1, |
| 2856 | EXTENT_DEFRAG, 0, 0, &cached_state, GFP_NOFS); |
| 2857 | } |
| 2858 | |
Josef Bacik | 0cb59c9 | 2010-07-02 12:14:14 -0400 | [diff] [blame] | 2859 | if (nolock) |
Josef Bacik | 7a7eaa4 | 2011-04-13 12:54:33 -0400 | [diff] [blame] | 2860 | trans = btrfs_join_transaction_nolock(root); |
Josef Bacik | 0cb59c9 | 2010-07-02 12:14:14 -0400 | [diff] [blame] | 2861 | else |
Josef Bacik | 7a7eaa4 | 2011-04-13 12:54:33 -0400 | [diff] [blame] | 2862 | trans = btrfs_join_transaction(root); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 2863 | if (IS_ERR(trans)) { |
| 2864 | ret = PTR_ERR(trans); |
| 2865 | trans = NULL; |
| 2866 | goto out_unlock; |
| 2867 | } |
Chris Mason | a79b7d4 | 2014-05-22 16:18:52 -0700 | [diff] [blame] | 2868 | |
Yan, Zheng | 0ca1f7c | 2010-05-16 10:48:47 -0400 | [diff] [blame] | 2869 | trans->block_rsv = &root->fs_info->delalloc_block_rsv; |
Yan, Zheng | c216775 | 2009-11-12 09:34:21 +0000 | [diff] [blame] | 2870 | |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 2871 | if (test_bit(BTRFS_ORDERED_COMPRESSED, &ordered_extent->flags)) |
Li Zefan | 261507a0 | 2010-12-17 14:21:50 +0800 | [diff] [blame] | 2872 | compress_type = ordered_extent->compress_type; |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 2873 | if (test_bit(BTRFS_ORDERED_PREALLOC, &ordered_extent->flags)) { |
Li Zefan | 261507a0 | 2010-12-17 14:21:50 +0800 | [diff] [blame] | 2874 | BUG_ON(compress_type); |
Yan, Zheng | 920bbbf | 2009-11-12 09:34:08 +0000 | [diff] [blame] | 2875 | ret = btrfs_mark_extent_written(trans, inode, |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 2876 | ordered_extent->file_offset, |
| 2877 | ordered_extent->file_offset + |
Josef Bacik | 77cef2e | 2013-08-29 13:57:21 -0400 | [diff] [blame] | 2878 | logical_len); |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 2879 | } else { |
Josef Bacik | 0af3d00 | 2010-06-21 14:48:16 -0400 | [diff] [blame] | 2880 | BUG_ON(root == root->fs_info->tree_root); |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 2881 | ret = insert_reserved_file_extent(trans, inode, |
| 2882 | ordered_extent->file_offset, |
| 2883 | ordered_extent->start, |
| 2884 | ordered_extent->disk_len, |
Josef Bacik | 77cef2e | 2013-08-29 13:57:21 -0400 | [diff] [blame] | 2885 | logical_len, logical_len, |
Li Zefan | 261507a0 | 2010-12-17 14:21:50 +0800 | [diff] [blame] | 2886 | compress_type, 0, 0, |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 2887 | BTRFS_FILE_EXTENT_REG); |
Miao Xie | e570fd2 | 2014-06-19 10:42:50 +0800 | [diff] [blame] | 2888 | if (!ret) |
| 2889 | btrfs_release_delalloc_bytes(root, |
| 2890 | ordered_extent->start, |
| 2891 | ordered_extent->disk_len); |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 2892 | } |
Josef Bacik | 5dc562c | 2012-08-17 13:14:17 -0400 | [diff] [blame] | 2893 | unpin_extent_cache(&BTRFS_I(inode)->extent_tree, |
| 2894 | ordered_extent->file_offset, ordered_extent->len, |
| 2895 | trans->transid); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 2896 | if (ret < 0) { |
| 2897 | btrfs_abort_transaction(trans, root, ret); |
Josef Bacik | 5fd0204 | 2012-05-02 14:00:54 -0400 | [diff] [blame] | 2898 | goto out_unlock; |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 2899 | } |
Josef Bacik | 2ac55d4 | 2010-02-03 19:33:23 +0000 | [diff] [blame] | 2900 | |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 2901 | add_pending_csums(trans, inode, ordered_extent->file_offset, |
| 2902 | &ordered_extent->list); |
| 2903 | |
Josef Bacik | 6c760c0 | 2012-11-09 10:53:21 -0500 | [diff] [blame] | 2904 | btrfs_ordered_update_i_size(inode, 0, ordered_extent); |
| 2905 | ret = btrfs_update_inode_fallback(trans, root, inode); |
| 2906 | if (ret) { /* -ENOMEM or corruption */ |
| 2907 | btrfs_abort_transaction(trans, root, ret); |
| 2908 | goto out_unlock; |
Josef Bacik | 1ef30be | 2011-04-05 19:25:36 -0400 | [diff] [blame] | 2909 | } |
| 2910 | ret = 0; |
Josef Bacik | 5fd0204 | 2012-05-02 14:00:54 -0400 | [diff] [blame] | 2911 | out_unlock: |
| 2912 | unlock_extent_cached(io_tree, ordered_extent->file_offset, |
| 2913 | ordered_extent->file_offset + |
| 2914 | ordered_extent->len - 1, &cached_state, GFP_NOFS); |
Yan, Zheng | c216775 | 2009-11-12 09:34:21 +0000 | [diff] [blame] | 2915 | out: |
Josef Bacik | 5b0e95b | 2011-10-06 08:58:24 -0400 | [diff] [blame] | 2916 | if (root != root->fs_info->tree_root) |
Josef Bacik | 0cb59c9 | 2010-07-02 12:14:14 -0400 | [diff] [blame] | 2917 | btrfs_delalloc_release_metadata(inode, ordered_extent->len); |
Miao Xie | a698d075 | 2012-09-20 01:51:59 -0600 | [diff] [blame] | 2918 | if (trans) |
| 2919 | btrfs_end_transaction(trans, root); |
Josef Bacik | 0cb59c9 | 2010-07-02 12:14:14 -0400 | [diff] [blame] | 2920 | |
Josef Bacik | 77cef2e | 2013-08-29 13:57:21 -0400 | [diff] [blame] | 2921 | if (ret || truncated) { |
| 2922 | u64 start, end; |
| 2923 | |
| 2924 | if (truncated) |
| 2925 | start = ordered_extent->file_offset + logical_len; |
| 2926 | else |
| 2927 | start = ordered_extent->file_offset; |
| 2928 | end = ordered_extent->file_offset + ordered_extent->len - 1; |
| 2929 | clear_extent_uptodate(io_tree, start, end, NULL, GFP_NOFS); |
| 2930 | |
| 2931 | /* Drop the cache for the part of the extent we didn't write. */ |
| 2932 | btrfs_drop_extent_cache(inode, start, end, 0); |
Josef Bacik | 5fd0204 | 2012-05-02 14:00:54 -0400 | [diff] [blame] | 2933 | |
Josef Bacik | 0bec9ef | 2013-01-31 14:58:00 -0500 | [diff] [blame] | 2934 | /* |
| 2935 | * If the ordered extent had an IOERR or something else went |
| 2936 | * wrong we need to return the space for this ordered extent |
Josef Bacik | 77cef2e | 2013-08-29 13:57:21 -0400 | [diff] [blame] | 2937 | * back to the allocator. We only free the extent in the |
| 2938 | * truncated case if we didn't write out the extent at all. |
Josef Bacik | 0bec9ef | 2013-01-31 14:58:00 -0500 | [diff] [blame] | 2939 | */ |
Josef Bacik | 77cef2e | 2013-08-29 13:57:21 -0400 | [diff] [blame] | 2940 | if ((ret || !logical_len) && |
| 2941 | !test_bit(BTRFS_ORDERED_NOCOW, &ordered_extent->flags) && |
Josef Bacik | 0bec9ef | 2013-01-31 14:58:00 -0500 | [diff] [blame] | 2942 | !test_bit(BTRFS_ORDERED_PREALLOC, &ordered_extent->flags)) |
| 2943 | btrfs_free_reserved_extent(root, ordered_extent->start, |
Miao Xie | e570fd2 | 2014-06-19 10:42:50 +0800 | [diff] [blame] | 2944 | ordered_extent->disk_len, 1); |
Josef Bacik | 0bec9ef | 2013-01-31 14:58:00 -0500 | [diff] [blame] | 2945 | } |
| 2946 | |
| 2947 | |
Josef Bacik | 5fd0204 | 2012-05-02 14:00:54 -0400 | [diff] [blame] | 2948 | /* |
Liu Bo | 8bad3c0 | 2012-06-18 12:14:23 +0800 | [diff] [blame] | 2949 | * This needs to be done to make sure anybody waiting knows we are done |
| 2950 | * updating everything for this ordered extent. |
Josef Bacik | 5fd0204 | 2012-05-02 14:00:54 -0400 | [diff] [blame] | 2951 | */ |
| 2952 | btrfs_remove_ordered_extent(inode, ordered_extent); |
| 2953 | |
Liu Bo | 38c227d | 2013-01-29 03:18:40 +0000 | [diff] [blame] | 2954 | /* for snapshot-aware defrag */ |
Liu Bo | 6f51956 | 2013-10-29 10:45:05 +0800 | [diff] [blame] | 2955 | if (new) { |
| 2956 | if (ret) { |
| 2957 | free_sa_defrag_extent(new); |
| 2958 | atomic_dec(&root->fs_info->defrag_running); |
| 2959 | } else { |
| 2960 | relink_file_extents(new); |
| 2961 | } |
| 2962 | } |
Liu Bo | 38c227d | 2013-01-29 03:18:40 +0000 | [diff] [blame] | 2963 | |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 2964 | /* once for us */ |
| 2965 | btrfs_put_ordered_extent(ordered_extent); |
| 2966 | /* once for the tree */ |
| 2967 | btrfs_put_ordered_extent(ordered_extent); |
| 2968 | |
Josef Bacik | 5fd0204 | 2012-05-02 14:00:54 -0400 | [diff] [blame] | 2969 | return ret; |
| 2970 | } |
| 2971 | |
| 2972 | static void finish_ordered_fn(struct btrfs_work *work) |
| 2973 | { |
| 2974 | struct btrfs_ordered_extent *ordered_extent; |
| 2975 | ordered_extent = container_of(work, struct btrfs_ordered_extent, work); |
| 2976 | btrfs_finish_ordered_io(ordered_extent); |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 2977 | } |
| 2978 | |
Christoph Hellwig | b295086 | 2008-12-02 09:54:17 -0500 | [diff] [blame] | 2979 | 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] | 2980 | struct extent_state *state, int uptodate) |
| 2981 | { |
Josef Bacik | 5fd0204 | 2012-05-02 14:00:54 -0400 | [diff] [blame] | 2982 | struct inode *inode = page->mapping->host; |
| 2983 | struct btrfs_root *root = BTRFS_I(inode)->root; |
| 2984 | struct btrfs_ordered_extent *ordered_extent = NULL; |
Liu Bo | 9e0af23 | 2014-08-15 23:36:53 +0800 | [diff] [blame] | 2985 | struct btrfs_workqueue *wq; |
| 2986 | btrfs_work_func_t func; |
Josef Bacik | 5fd0204 | 2012-05-02 14:00:54 -0400 | [diff] [blame] | 2987 | |
liubo | 1abe9b8 | 2011-03-24 11:18:59 +0000 | [diff] [blame] | 2988 | trace_btrfs_writepage_end_io_hook(page, start, end, uptodate); |
| 2989 | |
Chris Mason | 8b62b72 | 2009-09-02 16:53:46 -0400 | [diff] [blame] | 2990 | ClearPagePrivate2(page); |
Josef Bacik | 5fd0204 | 2012-05-02 14:00:54 -0400 | [diff] [blame] | 2991 | if (!btrfs_dec_test_ordered_pending(inode, &ordered_extent, start, |
| 2992 | end - start + 1, uptodate)) |
| 2993 | return 0; |
| 2994 | |
Liu Bo | 9e0af23 | 2014-08-15 23:36:53 +0800 | [diff] [blame] | 2995 | if (btrfs_is_free_space_inode(inode)) { |
| 2996 | wq = root->fs_info->endio_freespace_worker; |
| 2997 | func = btrfs_freespace_write_helper; |
| 2998 | } else { |
| 2999 | wq = root->fs_info->endio_write_workers; |
| 3000 | func = btrfs_endio_write_helper; |
| 3001 | } |
Josef Bacik | 5fd0204 | 2012-05-02 14:00:54 -0400 | [diff] [blame] | 3002 | |
Liu Bo | 9e0af23 | 2014-08-15 23:36:53 +0800 | [diff] [blame] | 3003 | btrfs_init_work(&ordered_extent->work, func, finish_ordered_fn, NULL, |
| 3004 | NULL); |
| 3005 | btrfs_queue_work(wq, &ordered_extent->work); |
Josef Bacik | 5fd0204 | 2012-05-02 14:00:54 -0400 | [diff] [blame] | 3006 | |
| 3007 | return 0; |
Chris Mason | 211f90e | 2008-07-18 11:56:15 -0400 | [diff] [blame] | 3008 | } |
| 3009 | |
Miao Xie | dc380ae | 2014-09-12 18:43:55 +0800 | [diff] [blame] | 3010 | static int __readpage_endio_check(struct inode *inode, |
| 3011 | struct btrfs_io_bio *io_bio, |
| 3012 | int icsum, struct page *page, |
| 3013 | int pgoff, u64 start, size_t len) |
| 3014 | { |
| 3015 | char *kaddr; |
| 3016 | u32 csum_expected; |
| 3017 | u32 csum = ~(u32)0; |
| 3018 | static DEFINE_RATELIMIT_STATE(_rs, DEFAULT_RATELIMIT_INTERVAL, |
| 3019 | DEFAULT_RATELIMIT_BURST); |
| 3020 | |
| 3021 | csum_expected = *(((u32 *)io_bio->csum) + icsum); |
| 3022 | |
| 3023 | kaddr = kmap_atomic(page); |
| 3024 | csum = btrfs_csum_data(kaddr + pgoff, csum, len); |
| 3025 | btrfs_csum_final(csum, (char *)&csum); |
| 3026 | if (csum != csum_expected) |
| 3027 | goto zeroit; |
| 3028 | |
| 3029 | kunmap_atomic(kaddr); |
| 3030 | return 0; |
| 3031 | zeroit: |
| 3032 | if (__ratelimit(&_rs)) |
David Sterba | f0954c6 | 2014-12-19 18:38:44 +0100 | [diff] [blame] | 3033 | btrfs_warn(BTRFS_I(inode)->root->fs_info, |
Miao Xie | dc380ae | 2014-09-12 18:43:55 +0800 | [diff] [blame] | 3034 | "csum failed ino %llu off %llu csum %u expected csum %u", |
| 3035 | btrfs_ino(inode), start, csum, csum_expected); |
| 3036 | memset(kaddr + pgoff, 1, len); |
| 3037 | flush_dcache_page(page); |
| 3038 | kunmap_atomic(kaddr); |
| 3039 | if (csum_expected == 0) |
| 3040 | return 0; |
| 3041 | return -EIO; |
| 3042 | } |
| 3043 | |
Chris Mason | d352ac6 | 2008-09-29 15:18:18 -0400 | [diff] [blame] | 3044 | /* |
Chris Mason | d352ac6 | 2008-09-29 15:18:18 -0400 | [diff] [blame] | 3045 | * 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] | 3046 | * if there's a match, we allow the bio to finish. If not, the code in |
| 3047 | * extent_io.c will try to find good copies for us. |
Chris Mason | d352ac6 | 2008-09-29 15:18:18 -0400 | [diff] [blame] | 3048 | */ |
Miao Xie | facc8a22 | 2013-07-25 19:22:34 +0800 | [diff] [blame] | 3049 | static int btrfs_readpage_end_io_hook(struct btrfs_io_bio *io_bio, |
| 3050 | u64 phy_offset, struct page *page, |
| 3051 | u64 start, u64 end, int mirror) |
Chris Mason | 07157aa | 2007-08-30 08:50:51 -0400 | [diff] [blame] | 3052 | { |
Miao Xie | 4eee4fa | 2012-12-21 09:17:45 +0000 | [diff] [blame] | 3053 | size_t offset = start - page_offset(page); |
Chris Mason | 07157aa | 2007-08-30 08:50:51 -0400 | [diff] [blame] | 3054 | struct inode *inode = page->mapping->host; |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 3055 | struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree; |
Chris Mason | ff79f81 | 2007-10-15 16:22:25 -0400 | [diff] [blame] | 3056 | struct btrfs_root *root = BTRFS_I(inode)->root; |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 3057 | |
Chris Mason | d20f704 | 2008-12-08 16:58:54 -0500 | [diff] [blame] | 3058 | if (PageChecked(page)) { |
| 3059 | ClearPageChecked(page); |
Miao Xie | dc380ae | 2014-09-12 18:43:55 +0800 | [diff] [blame] | 3060 | return 0; |
Chris Mason | d20f704 | 2008-12-08 16:58:54 -0500 | [diff] [blame] | 3061 | } |
Christoph Hellwig | 6cbff00 | 2009-04-17 10:37:41 +0200 | [diff] [blame] | 3062 | |
| 3063 | if (BTRFS_I(inode)->flags & BTRFS_INODE_NODATASUM) |
Miao Xie | dc380ae | 2014-09-12 18:43:55 +0800 | [diff] [blame] | 3064 | return 0; |
Chris Mason | d20f704 | 2008-12-08 16:58:54 -0500 | [diff] [blame] | 3065 | |
Yan Zheng | 17d217f | 2008-12-12 10:03:38 -0500 | [diff] [blame] | 3066 | if (root->root_key.objectid == BTRFS_DATA_RELOC_TREE_OBJECTID && |
Chris Mason | 9655d29 | 2009-09-02 15:22:30 -0400 | [diff] [blame] | 3067 | test_range_bit(io_tree, start, end, EXTENT_NODATASUM, 1, NULL)) { |
Yan Zheng | 17d217f | 2008-12-12 10:03:38 -0500 | [diff] [blame] | 3068 | clear_extent_bits(io_tree, start, end, EXTENT_NODATASUM, |
| 3069 | GFP_NOFS); |
| 3070 | return 0; |
| 3071 | } |
| 3072 | |
Miao Xie | facc8a22 | 2013-07-25 19:22:34 +0800 | [diff] [blame] | 3073 | phy_offset >>= inode->i_sb->s_blocksize_bits; |
Miao Xie | dc380ae | 2014-09-12 18:43:55 +0800 | [diff] [blame] | 3074 | return __readpage_endio_check(inode, io_bio, phy_offset, page, offset, |
| 3075 | start, (size_t)(end - start + 1)); |
Chris Mason | 07157aa | 2007-08-30 08:50:51 -0400 | [diff] [blame] | 3076 | } |
Chris Mason | b888db2b | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 3077 | |
Yan, Zheng | 24bbcf0 | 2009-11-12 09:36:34 +0000 | [diff] [blame] | 3078 | struct delayed_iput { |
| 3079 | struct list_head list; |
| 3080 | struct inode *inode; |
| 3081 | }; |
| 3082 | |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 3083 | /* JDM: If this is fs-wide, why can't we add a pointer to |
| 3084 | * btrfs_inode instead and avoid the allocation? */ |
Yan, Zheng | 24bbcf0 | 2009-11-12 09:36:34 +0000 | [diff] [blame] | 3085 | void btrfs_add_delayed_iput(struct inode *inode) |
| 3086 | { |
| 3087 | struct btrfs_fs_info *fs_info = BTRFS_I(inode)->root->fs_info; |
| 3088 | struct delayed_iput *delayed; |
| 3089 | |
| 3090 | if (atomic_add_unless(&inode->i_count, -1, 1)) |
| 3091 | return; |
| 3092 | |
| 3093 | delayed = kmalloc(sizeof(*delayed), GFP_NOFS | __GFP_NOFAIL); |
| 3094 | delayed->inode = inode; |
| 3095 | |
| 3096 | spin_lock(&fs_info->delayed_iput_lock); |
| 3097 | list_add_tail(&delayed->list, &fs_info->delayed_iputs); |
| 3098 | spin_unlock(&fs_info->delayed_iput_lock); |
| 3099 | } |
| 3100 | |
| 3101 | void btrfs_run_delayed_iputs(struct btrfs_root *root) |
| 3102 | { |
| 3103 | LIST_HEAD(list); |
| 3104 | struct btrfs_fs_info *fs_info = root->fs_info; |
| 3105 | struct delayed_iput *delayed; |
| 3106 | int empty; |
| 3107 | |
| 3108 | spin_lock(&fs_info->delayed_iput_lock); |
| 3109 | empty = list_empty(&fs_info->delayed_iputs); |
| 3110 | spin_unlock(&fs_info->delayed_iput_lock); |
| 3111 | if (empty) |
| 3112 | return; |
| 3113 | |
Zhao Lei | d7c1517 | 2015-02-26 10:49:20 +0800 | [diff] [blame^] | 3114 | down_read(&fs_info->delayed_iput_sem); |
| 3115 | |
Yan, Zheng | 24bbcf0 | 2009-11-12 09:36:34 +0000 | [diff] [blame] | 3116 | spin_lock(&fs_info->delayed_iput_lock); |
| 3117 | list_splice_init(&fs_info->delayed_iputs, &list); |
| 3118 | spin_unlock(&fs_info->delayed_iput_lock); |
| 3119 | |
| 3120 | while (!list_empty(&list)) { |
| 3121 | delayed = list_entry(list.next, struct delayed_iput, list); |
| 3122 | list_del(&delayed->list); |
| 3123 | iput(delayed->inode); |
| 3124 | kfree(delayed); |
| 3125 | } |
Zhao Lei | d7c1517 | 2015-02-26 10:49:20 +0800 | [diff] [blame^] | 3126 | |
| 3127 | up_read(&root->fs_info->delayed_iput_sem); |
Yan, Zheng | 24bbcf0 | 2009-11-12 09:36:34 +0000 | [diff] [blame] | 3128 | } |
| 3129 | |
Yan, Zheng | d68fc57 | 2010-05-16 10:49:58 -0400 | [diff] [blame] | 3130 | /* |
Justin P. Mattock | 42b2aa8 | 2011-11-28 20:31:00 -0800 | [diff] [blame] | 3131 | * This is called in transaction commit time. If there are no orphan |
Yan, Zheng | d68fc57 | 2010-05-16 10:49:58 -0400 | [diff] [blame] | 3132 | * files in the subvolume, it removes orphan item and frees block_rsv |
| 3133 | * structure. |
| 3134 | */ |
| 3135 | void btrfs_orphan_commit_root(struct btrfs_trans_handle *trans, |
| 3136 | struct btrfs_root *root) |
| 3137 | { |
Josef Bacik | 90290e1 | 2011-12-02 15:44:12 -0500 | [diff] [blame] | 3138 | struct btrfs_block_rsv *block_rsv; |
Yan, Zheng | d68fc57 | 2010-05-16 10:49:58 -0400 | [diff] [blame] | 3139 | int ret; |
| 3140 | |
Josef Bacik | 8a35d95 | 2012-05-23 14:26:42 -0400 | [diff] [blame] | 3141 | if (atomic_read(&root->orphan_inodes) || |
Yan, Zheng | d68fc57 | 2010-05-16 10:49:58 -0400 | [diff] [blame] | 3142 | root->orphan_cleanup_state != ORPHAN_CLEANUP_DONE) |
| 3143 | return; |
| 3144 | |
Josef Bacik | 90290e1 | 2011-12-02 15:44:12 -0500 | [diff] [blame] | 3145 | spin_lock(&root->orphan_lock); |
Josef Bacik | 8a35d95 | 2012-05-23 14:26:42 -0400 | [diff] [blame] | 3146 | if (atomic_read(&root->orphan_inodes)) { |
Josef Bacik | 90290e1 | 2011-12-02 15:44:12 -0500 | [diff] [blame] | 3147 | spin_unlock(&root->orphan_lock); |
| 3148 | return; |
| 3149 | } |
| 3150 | |
| 3151 | if (root->orphan_cleanup_state != ORPHAN_CLEANUP_DONE) { |
| 3152 | spin_unlock(&root->orphan_lock); |
| 3153 | return; |
| 3154 | } |
| 3155 | |
| 3156 | block_rsv = root->orphan_block_rsv; |
| 3157 | root->orphan_block_rsv = NULL; |
| 3158 | spin_unlock(&root->orphan_lock); |
| 3159 | |
Miao Xie | 27cdeb7 | 2014-04-02 19:51:05 +0800 | [diff] [blame] | 3160 | if (test_bit(BTRFS_ROOT_ORPHAN_ITEM_INSERTED, &root->state) && |
Yan, Zheng | d68fc57 | 2010-05-16 10:49:58 -0400 | [diff] [blame] | 3161 | btrfs_root_refs(&root->root_item) > 0) { |
| 3162 | ret = btrfs_del_orphan_item(trans, root->fs_info->tree_root, |
| 3163 | root->root_key.objectid); |
Josef Bacik | 4ef31a4 | 2013-08-13 14:10:08 -0400 | [diff] [blame] | 3164 | if (ret) |
| 3165 | btrfs_abort_transaction(trans, root, ret); |
| 3166 | else |
Miao Xie | 27cdeb7 | 2014-04-02 19:51:05 +0800 | [diff] [blame] | 3167 | clear_bit(BTRFS_ROOT_ORPHAN_ITEM_INSERTED, |
| 3168 | &root->state); |
Yan, Zheng | d68fc57 | 2010-05-16 10:49:58 -0400 | [diff] [blame] | 3169 | } |
| 3170 | |
Josef Bacik | 90290e1 | 2011-12-02 15:44:12 -0500 | [diff] [blame] | 3171 | if (block_rsv) { |
| 3172 | WARN_ON(block_rsv->size > 0); |
| 3173 | btrfs_free_block_rsv(root, block_rsv); |
Yan, Zheng | d68fc57 | 2010-05-16 10:49:58 -0400 | [diff] [blame] | 3174 | } |
| 3175 | } |
| 3176 | |
| 3177 | /* |
Josef Bacik | 7b12876 | 2008-07-24 12:17:14 -0400 | [diff] [blame] | 3178 | * This creates an orphan entry for the given inode in case something goes |
| 3179 | * wrong in the middle of an unlink/truncate. |
Yan, Zheng | d68fc57 | 2010-05-16 10:49:58 -0400 | [diff] [blame] | 3180 | * |
| 3181 | * NOTE: caller of this function should reserve 5 units of metadata for |
| 3182 | * this function. |
Josef Bacik | 7b12876 | 2008-07-24 12:17:14 -0400 | [diff] [blame] | 3183 | */ |
| 3184 | int btrfs_orphan_add(struct btrfs_trans_handle *trans, struct inode *inode) |
| 3185 | { |
| 3186 | struct btrfs_root *root = BTRFS_I(inode)->root; |
Yan, Zheng | d68fc57 | 2010-05-16 10:49:58 -0400 | [diff] [blame] | 3187 | struct btrfs_block_rsv *block_rsv = NULL; |
| 3188 | int reserve = 0; |
| 3189 | int insert = 0; |
| 3190 | int ret; |
Josef Bacik | 7b12876 | 2008-07-24 12:17:14 -0400 | [diff] [blame] | 3191 | |
Yan, Zheng | d68fc57 | 2010-05-16 10:49:58 -0400 | [diff] [blame] | 3192 | if (!root->orphan_block_rsv) { |
Miao Xie | 66d8f3d | 2012-09-06 04:02:28 -0600 | [diff] [blame] | 3193 | block_rsv = btrfs_alloc_block_rsv(root, BTRFS_BLOCK_RSV_TEMP); |
Tsutomu Itoh | b532402 | 2011-07-19 07:27:20 +0000 | [diff] [blame] | 3194 | if (!block_rsv) |
| 3195 | return -ENOMEM; |
Josef Bacik | 7b12876 | 2008-07-24 12:17:14 -0400 | [diff] [blame] | 3196 | } |
| 3197 | |
Yan, Zheng | d68fc57 | 2010-05-16 10:49:58 -0400 | [diff] [blame] | 3198 | spin_lock(&root->orphan_lock); |
| 3199 | if (!root->orphan_block_rsv) { |
| 3200 | root->orphan_block_rsv = block_rsv; |
| 3201 | } else if (block_rsv) { |
| 3202 | btrfs_free_block_rsv(root, block_rsv); |
| 3203 | block_rsv = NULL; |
| 3204 | } |
Josef Bacik | 7b12876 | 2008-07-24 12:17:14 -0400 | [diff] [blame] | 3205 | |
Josef Bacik | 8a35d95 | 2012-05-23 14:26:42 -0400 | [diff] [blame] | 3206 | if (!test_and_set_bit(BTRFS_INODE_HAS_ORPHAN_ITEM, |
| 3207 | &BTRFS_I(inode)->runtime_flags)) { |
Yan, Zheng | d68fc57 | 2010-05-16 10:49:58 -0400 | [diff] [blame] | 3208 | #if 0 |
| 3209 | /* |
| 3210 | * For proper ENOSPC handling, we should do orphan |
| 3211 | * cleanup when mounting. But this introduces backward |
| 3212 | * compatibility issue. |
| 3213 | */ |
| 3214 | if (!xchg(&root->orphan_item_inserted, 1)) |
| 3215 | insert = 2; |
| 3216 | else |
| 3217 | insert = 1; |
| 3218 | #endif |
| 3219 | insert = 1; |
Miao Xie | 321f0e7 | 2012-08-28 22:13:02 -0600 | [diff] [blame] | 3220 | atomic_inc(&root->orphan_inodes); |
Yan, Zheng | d68fc57 | 2010-05-16 10:49:58 -0400 | [diff] [blame] | 3221 | } |
Josef Bacik | 7b12876 | 2008-07-24 12:17:14 -0400 | [diff] [blame] | 3222 | |
Josef Bacik | 72ac3c0 | 2012-05-23 14:13:11 -0400 | [diff] [blame] | 3223 | if (!test_and_set_bit(BTRFS_INODE_ORPHAN_META_RESERVED, |
| 3224 | &BTRFS_I(inode)->runtime_flags)) |
Yan, Zheng | d68fc57 | 2010-05-16 10:49:58 -0400 | [diff] [blame] | 3225 | reserve = 1; |
Yan, Zheng | d68fc57 | 2010-05-16 10:49:58 -0400 | [diff] [blame] | 3226 | spin_unlock(&root->orphan_lock); |
Josef Bacik | 7b12876 | 2008-07-24 12:17:14 -0400 | [diff] [blame] | 3227 | |
Yan, Zheng | d68fc57 | 2010-05-16 10:49:58 -0400 | [diff] [blame] | 3228 | /* grab metadata reservation from transaction handle */ |
| 3229 | if (reserve) { |
| 3230 | ret = btrfs_orphan_reserve_metadata(trans, inode); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 3231 | BUG_ON(ret); /* -ENOSPC in reservation; Logic error? JDM */ |
Yan, Zheng | d68fc57 | 2010-05-16 10:49:58 -0400 | [diff] [blame] | 3232 | } |
| 3233 | |
| 3234 | /* insert an orphan item to track this unlinked/truncated file */ |
| 3235 | if (insert >= 1) { |
Li Zefan | 33345d01 | 2011-04-20 10:31:50 +0800 | [diff] [blame] | 3236 | ret = btrfs_insert_orphan_item(trans, root, btrfs_ino(inode)); |
Josef Bacik | 4ef31a4 | 2013-08-13 14:10:08 -0400 | [diff] [blame] | 3237 | if (ret) { |
Filipe David Borba Manana | 703c88e | 2013-09-22 21:54:55 +0100 | [diff] [blame] | 3238 | atomic_dec(&root->orphan_inodes); |
Josef Bacik | 4ef31a4 | 2013-08-13 14:10:08 -0400 | [diff] [blame] | 3239 | if (reserve) { |
| 3240 | clear_bit(BTRFS_INODE_ORPHAN_META_RESERVED, |
| 3241 | &BTRFS_I(inode)->runtime_flags); |
| 3242 | btrfs_orphan_release_metadata(inode); |
| 3243 | } |
| 3244 | if (ret != -EEXIST) { |
Josef Bacik | e8e7cff | 2013-08-21 15:54:00 -0400 | [diff] [blame] | 3245 | clear_bit(BTRFS_INODE_HAS_ORPHAN_ITEM, |
| 3246 | &BTRFS_I(inode)->runtime_flags); |
Josef Bacik | 4ef31a4 | 2013-08-13 14:10:08 -0400 | [diff] [blame] | 3247 | btrfs_abort_transaction(trans, root, ret); |
| 3248 | return ret; |
| 3249 | } |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 3250 | } |
| 3251 | ret = 0; |
Yan, Zheng | d68fc57 | 2010-05-16 10:49:58 -0400 | [diff] [blame] | 3252 | } |
| 3253 | |
| 3254 | /* insert an orphan item to track subvolume contains orphan files */ |
| 3255 | if (insert >= 2) { |
| 3256 | ret = btrfs_insert_orphan_item(trans, root->fs_info->tree_root, |
| 3257 | root->root_key.objectid); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 3258 | if (ret && ret != -EEXIST) { |
| 3259 | btrfs_abort_transaction(trans, root, ret); |
| 3260 | return ret; |
| 3261 | } |
Yan, Zheng | d68fc57 | 2010-05-16 10:49:58 -0400 | [diff] [blame] | 3262 | } |
| 3263 | return 0; |
Josef Bacik | 7b12876 | 2008-07-24 12:17:14 -0400 | [diff] [blame] | 3264 | } |
| 3265 | |
| 3266 | /* |
| 3267 | * We have done the truncate/delete so we can go ahead and remove the orphan |
| 3268 | * item for this particular inode. |
| 3269 | */ |
Eric Sandeen | 48a3b63 | 2013-04-25 20:41:01 +0000 | [diff] [blame] | 3270 | static int btrfs_orphan_del(struct btrfs_trans_handle *trans, |
| 3271 | struct inode *inode) |
Josef Bacik | 7b12876 | 2008-07-24 12:17:14 -0400 | [diff] [blame] | 3272 | { |
| 3273 | struct btrfs_root *root = BTRFS_I(inode)->root; |
Yan, Zheng | d68fc57 | 2010-05-16 10:49:58 -0400 | [diff] [blame] | 3274 | int delete_item = 0; |
| 3275 | int release_rsv = 0; |
Josef Bacik | 7b12876 | 2008-07-24 12:17:14 -0400 | [diff] [blame] | 3276 | int ret = 0; |
| 3277 | |
Yan, Zheng | d68fc57 | 2010-05-16 10:49:58 -0400 | [diff] [blame] | 3278 | spin_lock(&root->orphan_lock); |
Josef Bacik | 8a35d95 | 2012-05-23 14:26:42 -0400 | [diff] [blame] | 3279 | if (test_and_clear_bit(BTRFS_INODE_HAS_ORPHAN_ITEM, |
| 3280 | &BTRFS_I(inode)->runtime_flags)) |
Yan, Zheng | d68fc57 | 2010-05-16 10:49:58 -0400 | [diff] [blame] | 3281 | delete_item = 1; |
Josef Bacik | 7b12876 | 2008-07-24 12:17:14 -0400 | [diff] [blame] | 3282 | |
Josef Bacik | 72ac3c0 | 2012-05-23 14:13:11 -0400 | [diff] [blame] | 3283 | if (test_and_clear_bit(BTRFS_INODE_ORPHAN_META_RESERVED, |
| 3284 | &BTRFS_I(inode)->runtime_flags)) |
Yan, Zheng | d68fc57 | 2010-05-16 10:49:58 -0400 | [diff] [blame] | 3285 | release_rsv = 1; |
Yan, Zheng | d68fc57 | 2010-05-16 10:49:58 -0400 | [diff] [blame] | 3286 | spin_unlock(&root->orphan_lock); |
| 3287 | |
Filipe David Borba Manana | 703c88e | 2013-09-22 21:54:55 +0100 | [diff] [blame] | 3288 | if (delete_item) { |
Josef Bacik | 8a35d95 | 2012-05-23 14:26:42 -0400 | [diff] [blame] | 3289 | atomic_dec(&root->orphan_inodes); |
Filipe David Borba Manana | 703c88e | 2013-09-22 21:54:55 +0100 | [diff] [blame] | 3290 | if (trans) |
| 3291 | ret = btrfs_del_orphan_item(trans, root, |
| 3292 | btrfs_ino(inode)); |
Josef Bacik | 8a35d95 | 2012-05-23 14:26:42 -0400 | [diff] [blame] | 3293 | } |
Josef Bacik | 7b12876 | 2008-07-24 12:17:14 -0400 | [diff] [blame] | 3294 | |
Filipe David Borba Manana | 703c88e | 2013-09-22 21:54:55 +0100 | [diff] [blame] | 3295 | if (release_rsv) |
| 3296 | btrfs_orphan_release_metadata(inode); |
| 3297 | |
Josef Bacik | 4ef31a4 | 2013-08-13 14:10:08 -0400 | [diff] [blame] | 3298 | return ret; |
Josef Bacik | 7b12876 | 2008-07-24 12:17:14 -0400 | [diff] [blame] | 3299 | } |
| 3300 | |
| 3301 | /* |
| 3302 | * this cleans up any orphans that may be left on the list from the last use |
| 3303 | * of this root. |
| 3304 | */ |
Josef Bacik | 66b4ffd | 2011-01-31 16:22:42 -0500 | [diff] [blame] | 3305 | int btrfs_orphan_cleanup(struct btrfs_root *root) |
Josef Bacik | 7b12876 | 2008-07-24 12:17:14 -0400 | [diff] [blame] | 3306 | { |
| 3307 | struct btrfs_path *path; |
| 3308 | struct extent_buffer *leaf; |
Josef Bacik | 7b12876 | 2008-07-24 12:17:14 -0400 | [diff] [blame] | 3309 | struct btrfs_key key, found_key; |
| 3310 | struct btrfs_trans_handle *trans; |
| 3311 | struct inode *inode; |
Josef Bacik | 8f6d7f4 | 2011-09-26 15:55:20 -0400 | [diff] [blame] | 3312 | u64 last_objectid = 0; |
Josef Bacik | 7b12876 | 2008-07-24 12:17:14 -0400 | [diff] [blame] | 3313 | int ret = 0, nr_unlink = 0, nr_truncate = 0; |
| 3314 | |
Yan, Zheng | d68fc57 | 2010-05-16 10:49:58 -0400 | [diff] [blame] | 3315 | if (cmpxchg(&root->orphan_cleanup_state, 0, ORPHAN_CLEANUP_STARTED)) |
Josef Bacik | 66b4ffd | 2011-01-31 16:22:42 -0500 | [diff] [blame] | 3316 | return 0; |
Yan, Zheng | c71bf09 | 2009-11-12 09:34:40 +0000 | [diff] [blame] | 3317 | |
| 3318 | path = btrfs_alloc_path(); |
Josef Bacik | 66b4ffd | 2011-01-31 16:22:42 -0500 | [diff] [blame] | 3319 | if (!path) { |
| 3320 | ret = -ENOMEM; |
| 3321 | goto out; |
| 3322 | } |
Josef Bacik | 7b12876 | 2008-07-24 12:17:14 -0400 | [diff] [blame] | 3323 | path->reada = -1; |
| 3324 | |
| 3325 | key.objectid = BTRFS_ORPHAN_OBJECTID; |
David Sterba | 962a298 | 2014-06-04 18:41:45 +0200 | [diff] [blame] | 3326 | key.type = BTRFS_ORPHAN_ITEM_KEY; |
Josef Bacik | 7b12876 | 2008-07-24 12:17:14 -0400 | [diff] [blame] | 3327 | key.offset = (u64)-1; |
| 3328 | |
Josef Bacik | 7b12876 | 2008-07-24 12:17:14 -0400 | [diff] [blame] | 3329 | while (1) { |
| 3330 | ret = btrfs_search_slot(NULL, root, &key, path, 0, 0); |
Josef Bacik | 66b4ffd | 2011-01-31 16:22:42 -0500 | [diff] [blame] | 3331 | if (ret < 0) |
| 3332 | goto out; |
Josef Bacik | 7b12876 | 2008-07-24 12:17:14 -0400 | [diff] [blame] | 3333 | |
| 3334 | /* |
| 3335 | * 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] | 3336 | * 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] | 3337 | * find the key and see if we have stuff that matches |
| 3338 | */ |
| 3339 | if (ret > 0) { |
Josef Bacik | 66b4ffd | 2011-01-31 16:22:42 -0500 | [diff] [blame] | 3340 | ret = 0; |
Josef Bacik | 7b12876 | 2008-07-24 12:17:14 -0400 | [diff] [blame] | 3341 | if (path->slots[0] == 0) |
| 3342 | break; |
| 3343 | path->slots[0]--; |
| 3344 | } |
| 3345 | |
| 3346 | /* pull out the item */ |
| 3347 | leaf = path->nodes[0]; |
Josef Bacik | 7b12876 | 2008-07-24 12:17:14 -0400 | [diff] [blame] | 3348 | btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]); |
| 3349 | |
| 3350 | /* make sure the item matches what we want */ |
| 3351 | if (found_key.objectid != BTRFS_ORPHAN_OBJECTID) |
| 3352 | break; |
David Sterba | 962a298 | 2014-06-04 18:41:45 +0200 | [diff] [blame] | 3353 | if (found_key.type != BTRFS_ORPHAN_ITEM_KEY) |
Josef Bacik | 7b12876 | 2008-07-24 12:17:14 -0400 | [diff] [blame] | 3354 | break; |
| 3355 | |
| 3356 | /* release the path since we're done with it */ |
David Sterba | b3b4aa7 | 2011-04-21 01:20:15 +0200 | [diff] [blame] | 3357 | btrfs_release_path(path); |
Josef Bacik | 7b12876 | 2008-07-24 12:17:14 -0400 | [diff] [blame] | 3358 | |
| 3359 | /* |
| 3360 | * this is where we are basically btrfs_lookup, without the |
| 3361 | * crossing root thing. we store the inode number in the |
| 3362 | * offset of the orphan item. |
| 3363 | */ |
Josef Bacik | 8f6d7f4 | 2011-09-26 15:55:20 -0400 | [diff] [blame] | 3364 | |
| 3365 | if (found_key.offset == last_objectid) { |
Simon Kirby | c2cf52e | 2013-03-19 22:41:23 +0000 | [diff] [blame] | 3366 | btrfs_err(root->fs_info, |
| 3367 | "Error removing orphan entry, stopping orphan cleanup"); |
Josef Bacik | 8f6d7f4 | 2011-09-26 15:55:20 -0400 | [diff] [blame] | 3368 | ret = -EINVAL; |
| 3369 | goto out; |
| 3370 | } |
| 3371 | |
| 3372 | last_objectid = found_key.offset; |
| 3373 | |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 3374 | found_key.objectid = found_key.offset; |
| 3375 | found_key.type = BTRFS_INODE_ITEM_KEY; |
| 3376 | found_key.offset = 0; |
Josef Bacik | 73f7341 | 2009-12-04 17:38:27 +0000 | [diff] [blame] | 3377 | inode = btrfs_iget(root->fs_info->sb, &found_key, root, NULL); |
Rusty Russell | 8c6ffba | 2013-07-15 11:20:32 +0930 | [diff] [blame] | 3378 | ret = PTR_ERR_OR_ZERO(inode); |
Josef Bacik | a8c9e57 | 2011-09-21 16:55:59 -0400 | [diff] [blame] | 3379 | if (ret && ret != -ESTALE) |
Josef Bacik | 66b4ffd | 2011-01-31 16:22:42 -0500 | [diff] [blame] | 3380 | goto out; |
Josef Bacik | a8c9e57 | 2011-09-21 16:55:59 -0400 | [diff] [blame] | 3381 | |
Arne Jansen | f8e9e0b | 2011-12-14 20:12:02 -0500 | [diff] [blame] | 3382 | if (ret == -ESTALE && root == root->fs_info->tree_root) { |
| 3383 | struct btrfs_root *dead_root; |
| 3384 | struct btrfs_fs_info *fs_info = root->fs_info; |
| 3385 | int is_dead_root = 0; |
| 3386 | |
| 3387 | /* |
| 3388 | * this is an orphan in the tree root. Currently these |
| 3389 | * could come from 2 sources: |
| 3390 | * a) a snapshot deletion in progress |
| 3391 | * b) a free space cache inode |
| 3392 | * We need to distinguish those two, as the snapshot |
| 3393 | * orphan must not get deleted. |
| 3394 | * find_dead_roots already ran before us, so if this |
| 3395 | * is a snapshot deletion, we should find the root |
| 3396 | * in the dead_roots list |
| 3397 | */ |
| 3398 | spin_lock(&fs_info->trans_lock); |
| 3399 | list_for_each_entry(dead_root, &fs_info->dead_roots, |
| 3400 | root_list) { |
| 3401 | if (dead_root->root_key.objectid == |
| 3402 | found_key.objectid) { |
| 3403 | is_dead_root = 1; |
| 3404 | break; |
| 3405 | } |
| 3406 | } |
| 3407 | spin_unlock(&fs_info->trans_lock); |
| 3408 | if (is_dead_root) { |
| 3409 | /* prevent this orphan from being found again */ |
| 3410 | key.offset = found_key.objectid - 1; |
| 3411 | continue; |
| 3412 | } |
| 3413 | } |
Josef Bacik | a8c9e57 | 2011-09-21 16:55:59 -0400 | [diff] [blame] | 3414 | /* |
| 3415 | * Inode is already gone but the orphan item is still there, |
| 3416 | * kill the orphan item. |
| 3417 | */ |
| 3418 | if (ret == -ESTALE) { |
| 3419 | trans = btrfs_start_transaction(root, 1); |
| 3420 | if (IS_ERR(trans)) { |
| 3421 | ret = PTR_ERR(trans); |
| 3422 | goto out; |
| 3423 | } |
Simon Kirby | c2cf52e | 2013-03-19 22:41:23 +0000 | [diff] [blame] | 3424 | btrfs_debug(root->fs_info, "auto deleting %Lu", |
| 3425 | found_key.objectid); |
Josef Bacik | a8c9e57 | 2011-09-21 16:55:59 -0400 | [diff] [blame] | 3426 | ret = btrfs_del_orphan_item(trans, root, |
| 3427 | found_key.objectid); |
Josef Bacik | a8c9e57 | 2011-09-21 16:55:59 -0400 | [diff] [blame] | 3428 | btrfs_end_transaction(trans, root); |
Josef Bacik | 4ef31a4 | 2013-08-13 14:10:08 -0400 | [diff] [blame] | 3429 | if (ret) |
| 3430 | goto out; |
Josef Bacik | a8c9e57 | 2011-09-21 16:55:59 -0400 | [diff] [blame] | 3431 | continue; |
Josef Bacik | 66b4ffd | 2011-01-31 16:22:42 -0500 | [diff] [blame] | 3432 | } |
Josef Bacik | 7b12876 | 2008-07-24 12:17:14 -0400 | [diff] [blame] | 3433 | |
Josef Bacik | 7b12876 | 2008-07-24 12:17:14 -0400 | [diff] [blame] | 3434 | /* |
| 3435 | * add this inode to the orphan list so btrfs_orphan_del does |
| 3436 | * the proper thing when we hit it |
| 3437 | */ |
Josef Bacik | 8a35d95 | 2012-05-23 14:26:42 -0400 | [diff] [blame] | 3438 | set_bit(BTRFS_INODE_HAS_ORPHAN_ITEM, |
| 3439 | &BTRFS_I(inode)->runtime_flags); |
Josef Bacik | 925396e | 2013-02-01 15:57:47 -0500 | [diff] [blame] | 3440 | atomic_inc(&root->orphan_inodes); |
Josef Bacik | 7b12876 | 2008-07-24 12:17:14 -0400 | [diff] [blame] | 3441 | |
Josef Bacik | 7b12876 | 2008-07-24 12:17:14 -0400 | [diff] [blame] | 3442 | /* if we have links, this was a truncate, lets do that */ |
| 3443 | if (inode->i_nlink) { |
Dulshani Gunawardhana | fae7f21 | 2013-10-31 10:30:08 +0530 | [diff] [blame] | 3444 | if (WARN_ON(!S_ISREG(inode->i_mode))) { |
Josef Bacik | a41ad39 | 2011-01-31 15:30:16 -0500 | [diff] [blame] | 3445 | iput(inode); |
| 3446 | continue; |
| 3447 | } |
Josef Bacik | 7b12876 | 2008-07-24 12:17:14 -0400 | [diff] [blame] | 3448 | nr_truncate++; |
Josef Bacik | f3fe820 | 2013-01-07 17:03:21 -0500 | [diff] [blame] | 3449 | |
| 3450 | /* 1 for the orphan item deletion. */ |
| 3451 | trans = btrfs_start_transaction(root, 1); |
| 3452 | if (IS_ERR(trans)) { |
Josef Bacik | c69b26b | 2013-06-03 16:51:23 -0400 | [diff] [blame] | 3453 | iput(inode); |
Josef Bacik | f3fe820 | 2013-01-07 17:03:21 -0500 | [diff] [blame] | 3454 | ret = PTR_ERR(trans); |
| 3455 | goto out; |
| 3456 | } |
| 3457 | ret = btrfs_orphan_add(trans, inode); |
| 3458 | btrfs_end_transaction(trans, root); |
Josef Bacik | c69b26b | 2013-06-03 16:51:23 -0400 | [diff] [blame] | 3459 | if (ret) { |
| 3460 | iput(inode); |
Josef Bacik | f3fe820 | 2013-01-07 17:03:21 -0500 | [diff] [blame] | 3461 | goto out; |
Josef Bacik | c69b26b | 2013-06-03 16:51:23 -0400 | [diff] [blame] | 3462 | } |
Josef Bacik | f3fe820 | 2013-01-07 17:03:21 -0500 | [diff] [blame] | 3463 | |
Josef Bacik | 66b4ffd | 2011-01-31 16:22:42 -0500 | [diff] [blame] | 3464 | ret = btrfs_truncate(inode); |
Josef Bacik | 4a7d0f6 | 2013-02-07 16:27:28 -0500 | [diff] [blame] | 3465 | if (ret) |
| 3466 | btrfs_orphan_del(NULL, inode); |
Josef Bacik | 7b12876 | 2008-07-24 12:17:14 -0400 | [diff] [blame] | 3467 | } else { |
| 3468 | nr_unlink++; |
| 3469 | } |
| 3470 | |
| 3471 | /* this will do delete_inode and everything for us */ |
| 3472 | iput(inode); |
Josef Bacik | 66b4ffd | 2011-01-31 16:22:42 -0500 | [diff] [blame] | 3473 | if (ret) |
| 3474 | goto out; |
Josef Bacik | 7b12876 | 2008-07-24 12:17:14 -0400 | [diff] [blame] | 3475 | } |
Miao Xie | 3254c87 | 2011-11-10 20:45:05 -0500 | [diff] [blame] | 3476 | /* release the path since we're done with it */ |
| 3477 | btrfs_release_path(path); |
| 3478 | |
Yan, Zheng | d68fc57 | 2010-05-16 10:49:58 -0400 | [diff] [blame] | 3479 | root->orphan_cleanup_state = ORPHAN_CLEANUP_DONE; |
| 3480 | |
| 3481 | if (root->orphan_block_rsv) |
| 3482 | btrfs_block_rsv_release(root, root->orphan_block_rsv, |
| 3483 | (u64)-1); |
| 3484 | |
Miao Xie | 27cdeb7 | 2014-04-02 19:51:05 +0800 | [diff] [blame] | 3485 | if (root->orphan_block_rsv || |
| 3486 | test_bit(BTRFS_ROOT_ORPHAN_ITEM_INSERTED, &root->state)) { |
Josef Bacik | 7a7eaa4 | 2011-04-13 12:54:33 -0400 | [diff] [blame] | 3487 | trans = btrfs_join_transaction(root); |
Josef Bacik | 66b4ffd | 2011-01-31 16:22:42 -0500 | [diff] [blame] | 3488 | if (!IS_ERR(trans)) |
| 3489 | btrfs_end_transaction(trans, root); |
Yan, Zheng | d68fc57 | 2010-05-16 10:49:58 -0400 | [diff] [blame] | 3490 | } |
Josef Bacik | 7b12876 | 2008-07-24 12:17:14 -0400 | [diff] [blame] | 3491 | |
| 3492 | if (nr_unlink) |
David Sterba | 4884b47 | 2013-03-20 13:31:27 +0000 | [diff] [blame] | 3493 | btrfs_debug(root->fs_info, "unlinked %d orphans", nr_unlink); |
Josef Bacik | 7b12876 | 2008-07-24 12:17:14 -0400 | [diff] [blame] | 3494 | if (nr_truncate) |
David Sterba | 4884b47 | 2013-03-20 13:31:27 +0000 | [diff] [blame] | 3495 | btrfs_debug(root->fs_info, "truncated %d orphans", nr_truncate); |
Josef Bacik | 66b4ffd | 2011-01-31 16:22:42 -0500 | [diff] [blame] | 3496 | |
| 3497 | out: |
| 3498 | if (ret) |
David Sterba | 68b663d | 2014-12-19 18:38:37 +0100 | [diff] [blame] | 3499 | btrfs_err(root->fs_info, |
Simon Kirby | c2cf52e | 2013-03-19 22:41:23 +0000 | [diff] [blame] | 3500 | "could not do orphan cleanup %d", ret); |
Josef Bacik | 66b4ffd | 2011-01-31 16:22:42 -0500 | [diff] [blame] | 3501 | btrfs_free_path(path); |
| 3502 | return ret; |
Josef Bacik | 7b12876 | 2008-07-24 12:17:14 -0400 | [diff] [blame] | 3503 | } |
| 3504 | |
Chris Mason | d352ac6 | 2008-09-29 15:18:18 -0400 | [diff] [blame] | 3505 | /* |
Chris Mason | 46a53cc | 2009-04-27 11:47:50 -0400 | [diff] [blame] | 3506 | * very simple check to peek ahead in the leaf looking for xattrs. If we |
| 3507 | * don't find any xattrs, we know there can't be any acls. |
| 3508 | * |
| 3509 | * slot is the slot the inode is in, objectid is the objectid of the inode |
| 3510 | */ |
| 3511 | 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] | 3512 | int slot, u64 objectid, |
| 3513 | int *first_xattr_slot) |
Chris Mason | 46a53cc | 2009-04-27 11:47:50 -0400 | [diff] [blame] | 3514 | { |
| 3515 | u32 nritems = btrfs_header_nritems(leaf); |
| 3516 | struct btrfs_key found_key; |
Josef Bacik | f23b5a5 | 2013-06-19 10:16:26 -0400 | [diff] [blame] | 3517 | static u64 xattr_access = 0; |
| 3518 | static u64 xattr_default = 0; |
Chris Mason | 46a53cc | 2009-04-27 11:47:50 -0400 | [diff] [blame] | 3519 | int scanned = 0; |
| 3520 | |
Josef Bacik | f23b5a5 | 2013-06-19 10:16:26 -0400 | [diff] [blame] | 3521 | if (!xattr_access) { |
| 3522 | xattr_access = btrfs_name_hash(POSIX_ACL_XATTR_ACCESS, |
| 3523 | strlen(POSIX_ACL_XATTR_ACCESS)); |
| 3524 | xattr_default = btrfs_name_hash(POSIX_ACL_XATTR_DEFAULT, |
| 3525 | strlen(POSIX_ACL_XATTR_DEFAULT)); |
| 3526 | } |
| 3527 | |
Chris Mason | 46a53cc | 2009-04-27 11:47:50 -0400 | [diff] [blame] | 3528 | slot++; |
Filipe David Borba Manana | 6354192 | 2014-01-07 11:47:46 +0000 | [diff] [blame] | 3529 | *first_xattr_slot = -1; |
Chris Mason | 46a53cc | 2009-04-27 11:47:50 -0400 | [diff] [blame] | 3530 | while (slot < nritems) { |
| 3531 | btrfs_item_key_to_cpu(leaf, &found_key, slot); |
| 3532 | |
| 3533 | /* we found a different objectid, there must not be acls */ |
| 3534 | if (found_key.objectid != objectid) |
| 3535 | return 0; |
| 3536 | |
| 3537 | /* we found an xattr, assume we've got an acl */ |
Josef Bacik | f23b5a5 | 2013-06-19 10:16:26 -0400 | [diff] [blame] | 3538 | if (found_key.type == BTRFS_XATTR_ITEM_KEY) { |
Filipe David Borba Manana | 6354192 | 2014-01-07 11:47:46 +0000 | [diff] [blame] | 3539 | if (*first_xattr_slot == -1) |
| 3540 | *first_xattr_slot = slot; |
Josef Bacik | f23b5a5 | 2013-06-19 10:16:26 -0400 | [diff] [blame] | 3541 | if (found_key.offset == xattr_access || |
| 3542 | found_key.offset == xattr_default) |
| 3543 | return 1; |
| 3544 | } |
Chris Mason | 46a53cc | 2009-04-27 11:47:50 -0400 | [diff] [blame] | 3545 | |
| 3546 | /* |
| 3547 | * we found a key greater than an xattr key, there can't |
| 3548 | * be any acls later on |
| 3549 | */ |
| 3550 | if (found_key.type > BTRFS_XATTR_ITEM_KEY) |
| 3551 | return 0; |
| 3552 | |
| 3553 | slot++; |
| 3554 | scanned++; |
| 3555 | |
| 3556 | /* |
| 3557 | * it goes inode, inode backrefs, xattrs, extents, |
| 3558 | * so if there are a ton of hard links to an inode there can |
| 3559 | * be a lot of backrefs. Don't waste time searching too hard, |
| 3560 | * this is just an optimization |
| 3561 | */ |
| 3562 | if (scanned >= 8) |
| 3563 | break; |
| 3564 | } |
| 3565 | /* we hit the end of the leaf before we found an xattr or |
| 3566 | * something larger than an xattr. We have to assume the inode |
| 3567 | * has acls |
| 3568 | */ |
Filipe David Borba Manana | 6354192 | 2014-01-07 11:47:46 +0000 | [diff] [blame] | 3569 | if (*first_xattr_slot == -1) |
| 3570 | *first_xattr_slot = slot; |
Chris Mason | 46a53cc | 2009-04-27 11:47:50 -0400 | [diff] [blame] | 3571 | return 1; |
| 3572 | } |
| 3573 | |
| 3574 | /* |
Chris Mason | d352ac6 | 2008-09-29 15:18:18 -0400 | [diff] [blame] | 3575 | * read an inode from the btree into the in-memory inode |
| 3576 | */ |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 3577 | static void btrfs_read_locked_inode(struct inode *inode) |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3578 | { |
| 3579 | struct btrfs_path *path; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 3580 | struct extent_buffer *leaf; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3581 | struct btrfs_inode_item *inode_item; |
| 3582 | struct btrfs_root *root = BTRFS_I(inode)->root; |
| 3583 | struct btrfs_key location; |
Miao Xie | 67de117 | 2013-12-26 13:07:06 +0800 | [diff] [blame] | 3584 | unsigned long ptr; |
Chris Mason | 46a53cc | 2009-04-27 11:47:50 -0400 | [diff] [blame] | 3585 | int maybe_acls; |
Josef Bacik | 618e21d | 2007-07-11 10:18:17 -0400 | [diff] [blame] | 3586 | u32 rdev; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3587 | int ret; |
Miao Xie | 2f7e33d | 2011-06-23 07:27:13 +0000 | [diff] [blame] | 3588 | bool filled = false; |
Filipe David Borba Manana | 6354192 | 2014-01-07 11:47:46 +0000 | [diff] [blame] | 3589 | int first_xattr_slot; |
Miao Xie | 2f7e33d | 2011-06-23 07:27:13 +0000 | [diff] [blame] | 3590 | |
| 3591 | ret = btrfs_fill_inode(inode, &rdev); |
| 3592 | if (!ret) |
| 3593 | filled = true; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3594 | |
| 3595 | path = btrfs_alloc_path(); |
Mark Fasheh | 1748f84 | 2011-07-12 11:25:31 -0700 | [diff] [blame] | 3596 | if (!path) |
| 3597 | goto make_bad; |
| 3598 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3599 | memcpy(&location, &BTRFS_I(inode)->location, sizeof(location)); |
Chris Mason | dc17ff8 | 2008-01-08 15:46:30 -0500 | [diff] [blame] | 3600 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3601 | ret = btrfs_lookup_inode(NULL, root, path, &location, 0); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 3602 | if (ret) |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3603 | goto make_bad; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3604 | |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 3605 | leaf = path->nodes[0]; |
Miao Xie | 2f7e33d | 2011-06-23 07:27:13 +0000 | [diff] [blame] | 3606 | |
| 3607 | if (filled) |
Miao Xie | 67de117 | 2013-12-26 13:07:06 +0800 | [diff] [blame] | 3608 | goto cache_index; |
Miao Xie | 2f7e33d | 2011-06-23 07:27:13 +0000 | [diff] [blame] | 3609 | |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 3610 | inode_item = btrfs_item_ptr(leaf, path->slots[0], |
| 3611 | struct btrfs_inode_item); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 3612 | inode->i_mode = btrfs_inode_mode(leaf, inode_item); |
Miklos Szeredi | bfe8684 | 2011-10-28 14:13:29 +0200 | [diff] [blame] | 3613 | set_nlink(inode, btrfs_inode_nlink(leaf, inode_item)); |
Eric W. Biederman | 2f2f43d | 2012-02-10 11:05:07 -0800 | [diff] [blame] | 3614 | i_uid_write(inode, btrfs_inode_uid(leaf, inode_item)); |
| 3615 | i_gid_write(inode, btrfs_inode_gid(leaf, inode_item)); |
Chris Mason | dbe674a | 2008-07-17 12:54:05 -0400 | [diff] [blame] | 3616 | btrfs_i_size_write(inode, btrfs_inode_size(leaf, inode_item)); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 3617 | |
David Sterba | a937b97 | 2014-12-12 17:39:12 +0100 | [diff] [blame] | 3618 | inode->i_atime.tv_sec = btrfs_timespec_sec(leaf, &inode_item->atime); |
| 3619 | inode->i_atime.tv_nsec = btrfs_timespec_nsec(leaf, &inode_item->atime); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 3620 | |
David Sterba | a937b97 | 2014-12-12 17:39:12 +0100 | [diff] [blame] | 3621 | inode->i_mtime.tv_sec = btrfs_timespec_sec(leaf, &inode_item->mtime); |
| 3622 | inode->i_mtime.tv_nsec = btrfs_timespec_nsec(leaf, &inode_item->mtime); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 3623 | |
David Sterba | a937b97 | 2014-12-12 17:39:12 +0100 | [diff] [blame] | 3624 | inode->i_ctime.tv_sec = btrfs_timespec_sec(leaf, &inode_item->ctime); |
| 3625 | inode->i_ctime.tv_nsec = btrfs_timespec_nsec(leaf, &inode_item->ctime); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 3626 | |
chandan r | 9cc97d6 | 2012-07-04 12:48:07 +0530 | [diff] [blame] | 3627 | BTRFS_I(inode)->i_otime.tv_sec = |
| 3628 | btrfs_timespec_sec(leaf, &inode_item->otime); |
| 3629 | BTRFS_I(inode)->i_otime.tv_nsec = |
| 3630 | btrfs_timespec_nsec(leaf, &inode_item->otime); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 3631 | |
Yan Zheng | a76a3cd | 2008-10-09 11:46:29 -0400 | [diff] [blame] | 3632 | inode_set_bytes(inode, btrfs_inode_nbytes(leaf, inode_item)); |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 3633 | BTRFS_I(inode)->generation = btrfs_inode_generation(leaf, inode_item); |
Josef Bacik | 5dc562c | 2012-08-17 13:14:17 -0400 | [diff] [blame] | 3634 | BTRFS_I(inode)->last_trans = btrfs_inode_transid(leaf, inode_item); |
| 3635 | |
| 3636 | /* |
| 3637 | * If we were modified in the current generation and evicted from memory |
| 3638 | * and then re-read we need to do a full sync since we don't have any |
| 3639 | * idea about which extents were modified before we were evicted from |
| 3640 | * cache. |
| 3641 | */ |
| 3642 | if (BTRFS_I(inode)->last_trans == root->fs_info->generation) |
| 3643 | set_bit(BTRFS_INODE_NEEDS_FULL_SYNC, |
| 3644 | &BTRFS_I(inode)->runtime_flags); |
| 3645 | |
Josef Bacik | 0c4d2d9 | 2012-04-05 15:03:02 -0400 | [diff] [blame] | 3646 | inode->i_version = btrfs_inode_sequence(leaf, inode_item); |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 3647 | inode->i_generation = BTRFS_I(inode)->generation; |
Josef Bacik | 618e21d | 2007-07-11 10:18:17 -0400 | [diff] [blame] | 3648 | inode->i_rdev = 0; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 3649 | rdev = btrfs_inode_rdev(leaf, inode_item); |
| 3650 | |
Josef Bacik | aec7477 | 2008-07-24 12:12:38 -0400 | [diff] [blame] | 3651 | BTRFS_I(inode)->index_cnt = (u64)-1; |
Yan Zheng | d2fb343 | 2008-12-11 16:30:39 -0500 | [diff] [blame] | 3652 | BTRFS_I(inode)->flags = btrfs_inode_flags(leaf, inode_item); |
Miao Xie | 67de117 | 2013-12-26 13:07:06 +0800 | [diff] [blame] | 3653 | |
| 3654 | cache_index: |
| 3655 | path->slots[0]++; |
| 3656 | if (inode->i_nlink != 1 || |
| 3657 | path->slots[0] >= btrfs_header_nritems(leaf)) |
| 3658 | goto cache_acl; |
| 3659 | |
| 3660 | btrfs_item_key_to_cpu(leaf, &location, path->slots[0]); |
| 3661 | if (location.objectid != btrfs_ino(inode)) |
| 3662 | goto cache_acl; |
| 3663 | |
| 3664 | ptr = btrfs_item_ptr_offset(leaf, path->slots[0]); |
| 3665 | if (location.type == BTRFS_INODE_REF_KEY) { |
| 3666 | struct btrfs_inode_ref *ref; |
| 3667 | |
| 3668 | ref = (struct btrfs_inode_ref *)ptr; |
| 3669 | BTRFS_I(inode)->dir_index = btrfs_inode_ref_index(leaf, ref); |
| 3670 | } else if (location.type == BTRFS_INODE_EXTREF_KEY) { |
| 3671 | struct btrfs_inode_extref *extref; |
| 3672 | |
| 3673 | extref = (struct btrfs_inode_extref *)ptr; |
| 3674 | BTRFS_I(inode)->dir_index = btrfs_inode_extref_index(leaf, |
| 3675 | extref); |
| 3676 | } |
Miao Xie | 2f7e33d | 2011-06-23 07:27:13 +0000 | [diff] [blame] | 3677 | cache_acl: |
Chris Mason | 46a53cc | 2009-04-27 11:47:50 -0400 | [diff] [blame] | 3678 | /* |
| 3679 | * try to precache a NULL acl entry for files that don't have |
| 3680 | * any xattrs or acls |
| 3681 | */ |
Li Zefan | 33345d01 | 2011-04-20 10:31:50 +0800 | [diff] [blame] | 3682 | maybe_acls = acls_after_inode_item(leaf, path->slots[0], |
Filipe David Borba Manana | 6354192 | 2014-01-07 11:47:46 +0000 | [diff] [blame] | 3683 | btrfs_ino(inode), &first_xattr_slot); |
| 3684 | if (first_xattr_slot != -1) { |
| 3685 | path->slots[0] = first_xattr_slot; |
| 3686 | ret = btrfs_load_inode_props(inode, path); |
| 3687 | if (ret) |
| 3688 | btrfs_err(root->fs_info, |
David Sterba | 351fd35 | 2014-05-15 16:48:20 +0200 | [diff] [blame] | 3689 | "error loading props for ino %llu (root %llu): %d", |
Filipe David Borba Manana | 6354192 | 2014-01-07 11:47:46 +0000 | [diff] [blame] | 3690 | btrfs_ino(inode), |
| 3691 | root->root_key.objectid, ret); |
| 3692 | } |
| 3693 | btrfs_free_path(path); |
| 3694 | |
Al Viro | 72c0490 | 2009-06-24 16:58:48 -0400 | [diff] [blame] | 3695 | if (!maybe_acls) |
| 3696 | cache_no_acl(inode); |
Chris Mason | 46a53cc | 2009-04-27 11:47:50 -0400 | [diff] [blame] | 3697 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3698 | switch (inode->i_mode & S_IFMT) { |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3699 | case S_IFREG: |
| 3700 | inode->i_mapping->a_ops = &btrfs_aops; |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 3701 | BTRFS_I(inode)->io_tree.ops = &btrfs_extent_io_ops; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3702 | inode->i_fop = &btrfs_file_operations; |
| 3703 | inode->i_op = &btrfs_file_inode_operations; |
| 3704 | break; |
| 3705 | case S_IFDIR: |
| 3706 | inode->i_fop = &btrfs_dir_file_operations; |
| 3707 | if (root == root->fs_info->tree_root) |
| 3708 | inode->i_op = &btrfs_dir_ro_inode_operations; |
| 3709 | else |
| 3710 | inode->i_op = &btrfs_dir_inode_operations; |
| 3711 | break; |
| 3712 | case S_IFLNK: |
| 3713 | inode->i_op = &btrfs_symlink_inode_operations; |
| 3714 | inode->i_mapping->a_ops = &btrfs_symlink_aops; |
| 3715 | break; |
Josef Bacik | 618e21d | 2007-07-11 10:18:17 -0400 | [diff] [blame] | 3716 | default: |
Jim Owens | 0279b4c | 2009-02-04 09:29:13 -0500 | [diff] [blame] | 3717 | inode->i_op = &btrfs_special_inode_operations; |
Josef Bacik | 618e21d | 2007-07-11 10:18:17 -0400 | [diff] [blame] | 3718 | init_special_inode(inode, inode->i_mode, rdev); |
| 3719 | break; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3720 | } |
Christoph Hellwig | 6cbff00 | 2009-04-17 10:37:41 +0200 | [diff] [blame] | 3721 | |
| 3722 | btrfs_update_iflags(inode); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3723 | return; |
| 3724 | |
| 3725 | make_bad: |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3726 | btrfs_free_path(path); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3727 | make_bad_inode(inode); |
| 3728 | } |
| 3729 | |
Chris Mason | d352ac6 | 2008-09-29 15:18:18 -0400 | [diff] [blame] | 3730 | /* |
| 3731 | * given a leaf and an inode, copy the inode fields into the leaf |
| 3732 | */ |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 3733 | static void fill_inode_item(struct btrfs_trans_handle *trans, |
| 3734 | struct extent_buffer *leaf, |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 3735 | struct btrfs_inode_item *item, |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3736 | struct inode *inode) |
| 3737 | { |
Liu Bo | 51fab69 | 2012-12-27 09:01:21 +0000 | [diff] [blame] | 3738 | struct btrfs_map_token token; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 3739 | |
Liu Bo | 51fab69 | 2012-12-27 09:01:21 +0000 | [diff] [blame] | 3740 | btrfs_init_map_token(&token); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 3741 | |
Liu Bo | 51fab69 | 2012-12-27 09:01:21 +0000 | [diff] [blame] | 3742 | btrfs_set_token_inode_uid(leaf, item, i_uid_read(inode), &token); |
| 3743 | btrfs_set_token_inode_gid(leaf, item, i_gid_read(inode), &token); |
| 3744 | btrfs_set_token_inode_size(leaf, item, BTRFS_I(inode)->disk_i_size, |
| 3745 | &token); |
| 3746 | btrfs_set_token_inode_mode(leaf, item, inode->i_mode, &token); |
| 3747 | btrfs_set_token_inode_nlink(leaf, item, inode->i_nlink, &token); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 3748 | |
David Sterba | a937b97 | 2014-12-12 17:39:12 +0100 | [diff] [blame] | 3749 | btrfs_set_token_timespec_sec(leaf, &item->atime, |
Liu Bo | 51fab69 | 2012-12-27 09:01:21 +0000 | [diff] [blame] | 3750 | inode->i_atime.tv_sec, &token); |
David Sterba | a937b97 | 2014-12-12 17:39:12 +0100 | [diff] [blame] | 3751 | btrfs_set_token_timespec_nsec(leaf, &item->atime, |
Liu Bo | 51fab69 | 2012-12-27 09:01:21 +0000 | [diff] [blame] | 3752 | inode->i_atime.tv_nsec, &token); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 3753 | |
David Sterba | a937b97 | 2014-12-12 17:39:12 +0100 | [diff] [blame] | 3754 | btrfs_set_token_timespec_sec(leaf, &item->mtime, |
Liu Bo | 51fab69 | 2012-12-27 09:01:21 +0000 | [diff] [blame] | 3755 | inode->i_mtime.tv_sec, &token); |
David Sterba | a937b97 | 2014-12-12 17:39:12 +0100 | [diff] [blame] | 3756 | btrfs_set_token_timespec_nsec(leaf, &item->mtime, |
Liu Bo | 51fab69 | 2012-12-27 09:01:21 +0000 | [diff] [blame] | 3757 | inode->i_mtime.tv_nsec, &token); |
| 3758 | |
David Sterba | a937b97 | 2014-12-12 17:39:12 +0100 | [diff] [blame] | 3759 | btrfs_set_token_timespec_sec(leaf, &item->ctime, |
Liu Bo | 51fab69 | 2012-12-27 09:01:21 +0000 | [diff] [blame] | 3760 | inode->i_ctime.tv_sec, &token); |
David Sterba | a937b97 | 2014-12-12 17:39:12 +0100 | [diff] [blame] | 3761 | btrfs_set_token_timespec_nsec(leaf, &item->ctime, |
Liu Bo | 51fab69 | 2012-12-27 09:01:21 +0000 | [diff] [blame] | 3762 | inode->i_ctime.tv_nsec, &token); |
| 3763 | |
chandan r | 9cc97d6 | 2012-07-04 12:48:07 +0530 | [diff] [blame] | 3764 | btrfs_set_token_timespec_sec(leaf, &item->otime, |
| 3765 | BTRFS_I(inode)->i_otime.tv_sec, &token); |
| 3766 | btrfs_set_token_timespec_nsec(leaf, &item->otime, |
| 3767 | BTRFS_I(inode)->i_otime.tv_nsec, &token); |
| 3768 | |
Liu Bo | 51fab69 | 2012-12-27 09:01:21 +0000 | [diff] [blame] | 3769 | btrfs_set_token_inode_nbytes(leaf, item, inode_get_bytes(inode), |
| 3770 | &token); |
| 3771 | btrfs_set_token_inode_generation(leaf, item, BTRFS_I(inode)->generation, |
| 3772 | &token); |
| 3773 | btrfs_set_token_inode_sequence(leaf, item, inode->i_version, &token); |
| 3774 | btrfs_set_token_inode_transid(leaf, item, trans->transid, &token); |
| 3775 | btrfs_set_token_inode_rdev(leaf, item, inode->i_rdev, &token); |
| 3776 | btrfs_set_token_inode_flags(leaf, item, BTRFS_I(inode)->flags, &token); |
| 3777 | btrfs_set_token_inode_block_group(leaf, item, 0, &token); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3778 | } |
| 3779 | |
Chris Mason | d352ac6 | 2008-09-29 15:18:18 -0400 | [diff] [blame] | 3780 | /* |
| 3781 | * copy everything in the in-memory inode into the btree. |
| 3782 | */ |
Chris Mason | 2115133 | 2011-11-10 20:39:08 -0500 | [diff] [blame] | 3783 | static noinline int btrfs_update_inode_item(struct btrfs_trans_handle *trans, |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 3784 | struct btrfs_root *root, struct inode *inode) |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3785 | { |
| 3786 | struct btrfs_inode_item *inode_item; |
| 3787 | struct btrfs_path *path; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 3788 | struct extent_buffer *leaf; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3789 | int ret; |
| 3790 | |
| 3791 | path = btrfs_alloc_path(); |
Miao Xie | 16cdcec | 2011-04-22 18:12:22 +0800 | [diff] [blame] | 3792 | if (!path) |
| 3793 | return -ENOMEM; |
| 3794 | |
Chris Mason | b947343 | 2009-03-13 11:00:37 -0400 | [diff] [blame] | 3795 | path->leave_spinning = 1; |
Miao Xie | 16cdcec | 2011-04-22 18:12:22 +0800 | [diff] [blame] | 3796 | ret = btrfs_lookup_inode(trans, root, path, &BTRFS_I(inode)->location, |
| 3797 | 1); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3798 | if (ret) { |
| 3799 | if (ret > 0) |
| 3800 | ret = -ENOENT; |
| 3801 | goto failed; |
| 3802 | } |
| 3803 | |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 3804 | leaf = path->nodes[0]; |
| 3805 | inode_item = btrfs_item_ptr(leaf, path->slots[0], |
Miao Xie | 16cdcec | 2011-04-22 18:12:22 +0800 | [diff] [blame] | 3806 | struct btrfs_inode_item); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3807 | |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 3808 | fill_inode_item(trans, leaf, inode_item, inode); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 3809 | btrfs_mark_buffer_dirty(leaf); |
Josef Bacik | 15ee9bc | 2007-08-10 16:22:09 -0400 | [diff] [blame] | 3810 | btrfs_set_inode_last_trans(trans, inode); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3811 | ret = 0; |
| 3812 | failed: |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3813 | btrfs_free_path(path); |
| 3814 | return ret; |
| 3815 | } |
| 3816 | |
Chris Mason | d352ac6 | 2008-09-29 15:18:18 -0400 | [diff] [blame] | 3817 | /* |
Chris Mason | 2115133 | 2011-11-10 20:39:08 -0500 | [diff] [blame] | 3818 | * copy everything in the in-memory inode into the btree. |
| 3819 | */ |
| 3820 | noinline int btrfs_update_inode(struct btrfs_trans_handle *trans, |
| 3821 | struct btrfs_root *root, struct inode *inode) |
| 3822 | { |
| 3823 | int ret; |
| 3824 | |
| 3825 | /* |
| 3826 | * If the inode is a free space inode, we can deadlock during commit |
| 3827 | * if we put it into the delayed code. |
| 3828 | * |
| 3829 | * The data relocation inode should also be directly updated |
| 3830 | * without delay |
| 3831 | */ |
Liu Bo | 83eea1f | 2012-07-10 05:28:39 -0600 | [diff] [blame] | 3832 | if (!btrfs_is_free_space_inode(inode) |
Josef Bacik | 1d52c78 | 2014-09-18 11:30:44 -0400 | [diff] [blame] | 3833 | && root->root_key.objectid != BTRFS_DATA_RELOC_TREE_OBJECTID |
| 3834 | && !root->fs_info->log_root_recovering) { |
Alexander Block | 8ea05e3 | 2012-07-25 17:35:53 +0200 | [diff] [blame] | 3835 | btrfs_update_root_times(trans, root); |
| 3836 | |
Chris Mason | 2115133 | 2011-11-10 20:39:08 -0500 | [diff] [blame] | 3837 | ret = btrfs_delayed_update_inode(trans, root, inode); |
| 3838 | if (!ret) |
| 3839 | btrfs_set_inode_last_trans(trans, inode); |
| 3840 | return ret; |
| 3841 | } |
| 3842 | |
| 3843 | return btrfs_update_inode_item(trans, root, inode); |
| 3844 | } |
| 3845 | |
Josef Bacik | be6aef6 | 2012-10-22 15:43:12 -0400 | [diff] [blame] | 3846 | noinline int btrfs_update_inode_fallback(struct btrfs_trans_handle *trans, |
| 3847 | struct btrfs_root *root, |
| 3848 | struct inode *inode) |
Chris Mason | 2115133 | 2011-11-10 20:39:08 -0500 | [diff] [blame] | 3849 | { |
| 3850 | int ret; |
| 3851 | |
| 3852 | ret = btrfs_update_inode(trans, root, inode); |
| 3853 | if (ret == -ENOSPC) |
| 3854 | return btrfs_update_inode_item(trans, root, inode); |
| 3855 | return ret; |
| 3856 | } |
| 3857 | |
| 3858 | /* |
Chris Mason | d352ac6 | 2008-09-29 15:18:18 -0400 | [diff] [blame] | 3859 | * unlink helper that gets used here in inode.c and in the tree logging |
| 3860 | * recovery code. It remove a link in a directory with a given name, and |
| 3861 | * also drops the back refs in the inode to the directory |
| 3862 | */ |
Al Viro | 9298679 | 2011-03-04 17:14:37 +0000 | [diff] [blame] | 3863 | static int __btrfs_unlink_inode(struct btrfs_trans_handle *trans, |
| 3864 | struct btrfs_root *root, |
| 3865 | struct inode *dir, struct inode *inode, |
| 3866 | const char *name, int name_len) |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3867 | { |
| 3868 | struct btrfs_path *path; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3869 | int ret = 0; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 3870 | struct extent_buffer *leaf; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3871 | struct btrfs_dir_item *di; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 3872 | struct btrfs_key key; |
Josef Bacik | aec7477 | 2008-07-24 12:12:38 -0400 | [diff] [blame] | 3873 | u64 index; |
Li Zefan | 33345d01 | 2011-04-20 10:31:50 +0800 | [diff] [blame] | 3874 | u64 ino = btrfs_ino(inode); |
| 3875 | u64 dir_ino = btrfs_ino(dir); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3876 | |
| 3877 | path = btrfs_alloc_path(); |
Chris Mason | 54aa1f4 | 2007-06-22 14:16:25 -0400 | [diff] [blame] | 3878 | if (!path) { |
| 3879 | ret = -ENOMEM; |
Tsutomu Itoh | 554233a | 2011-02-03 03:16:25 +0000 | [diff] [blame] | 3880 | goto out; |
Chris Mason | 54aa1f4 | 2007-06-22 14:16:25 -0400 | [diff] [blame] | 3881 | } |
| 3882 | |
Chris Mason | b947343 | 2009-03-13 11:00:37 -0400 | [diff] [blame] | 3883 | path->leave_spinning = 1; |
Li Zefan | 33345d01 | 2011-04-20 10:31:50 +0800 | [diff] [blame] | 3884 | di = btrfs_lookup_dir_item(trans, root, path, dir_ino, |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3885 | name, name_len, -1); |
| 3886 | if (IS_ERR(di)) { |
| 3887 | ret = PTR_ERR(di); |
| 3888 | goto err; |
| 3889 | } |
| 3890 | if (!di) { |
| 3891 | ret = -ENOENT; |
| 3892 | goto err; |
| 3893 | } |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 3894 | leaf = path->nodes[0]; |
| 3895 | btrfs_dir_item_key_to_cpu(leaf, di, &key); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3896 | ret = btrfs_delete_one_dir_name(trans, root, path, di); |
Chris Mason | 54aa1f4 | 2007-06-22 14:16:25 -0400 | [diff] [blame] | 3897 | if (ret) |
| 3898 | goto err; |
David Sterba | b3b4aa7 | 2011-04-21 01:20:15 +0200 | [diff] [blame] | 3899 | btrfs_release_path(path); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3900 | |
Miao Xie | 67de117 | 2013-12-26 13:07:06 +0800 | [diff] [blame] | 3901 | /* |
| 3902 | * If we don't have dir index, we have to get it by looking up |
| 3903 | * the inode ref, since we get the inode ref, remove it directly, |
| 3904 | * it is unnecessary to do delayed deletion. |
| 3905 | * |
| 3906 | * But if we have dir index, needn't search inode ref to get it. |
| 3907 | * Since the inode ref is close to the inode item, it is better |
| 3908 | * that we delay to delete it, and just do this deletion when |
| 3909 | * we update the inode item. |
| 3910 | */ |
| 3911 | if (BTRFS_I(inode)->dir_index) { |
| 3912 | ret = btrfs_delayed_delete_inode_ref(inode); |
| 3913 | if (!ret) { |
| 3914 | index = BTRFS_I(inode)->dir_index; |
| 3915 | goto skip_backref; |
| 3916 | } |
| 3917 | } |
| 3918 | |
Li Zefan | 33345d01 | 2011-04-20 10:31:50 +0800 | [diff] [blame] | 3919 | ret = btrfs_del_inode_ref(trans, root, name, name_len, ino, |
| 3920 | dir_ino, &index); |
Josef Bacik | aec7477 | 2008-07-24 12:12:38 -0400 | [diff] [blame] | 3921 | if (ret) { |
Simon Kirby | c2cf52e | 2013-03-19 22:41:23 +0000 | [diff] [blame] | 3922 | btrfs_info(root->fs_info, |
| 3923 | "failed to delete reference to %.*s, inode %llu parent %llu", |
Geert Uytterhoeven | c1c9ff7 | 2013-08-20 13:20:07 +0200 | [diff] [blame] | 3924 | name_len, name, ino, dir_ino); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 3925 | btrfs_abort_transaction(trans, root, ret); |
Josef Bacik | aec7477 | 2008-07-24 12:12:38 -0400 | [diff] [blame] | 3926 | goto err; |
| 3927 | } |
Miao Xie | 67de117 | 2013-12-26 13:07:06 +0800 | [diff] [blame] | 3928 | skip_backref: |
Miao Xie | 16cdcec | 2011-04-22 18:12:22 +0800 | [diff] [blame] | 3929 | ret = btrfs_delete_delayed_dir_index(trans, root, dir, index); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 3930 | if (ret) { |
| 3931 | btrfs_abort_transaction(trans, root, ret); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3932 | goto err; |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 3933 | } |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3934 | |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 3935 | ret = btrfs_del_inode_ref_in_log(trans, root, name, name_len, |
Li Zefan | 33345d01 | 2011-04-20 10:31:50 +0800 | [diff] [blame] | 3936 | inode, dir_ino); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 3937 | if (ret != 0 && ret != -ENOENT) { |
| 3938 | btrfs_abort_transaction(trans, root, ret); |
| 3939 | goto err; |
| 3940 | } |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 3941 | |
| 3942 | ret = btrfs_del_dir_entries_in_log(trans, root, name, name_len, |
| 3943 | dir, index); |
Chris Mason | 6418c96 | 2010-10-30 07:34:24 -0400 | [diff] [blame] | 3944 | if (ret == -ENOENT) |
| 3945 | ret = 0; |
Zach Brown | d4e3991 | 2013-04-02 21:02:16 +0000 | [diff] [blame] | 3946 | else if (ret) |
| 3947 | btrfs_abort_transaction(trans, root, ret); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3948 | err: |
| 3949 | btrfs_free_path(path); |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 3950 | if (ret) |
| 3951 | goto out; |
| 3952 | |
| 3953 | btrfs_i_size_write(dir, dir->i_size - name_len * 2); |
Josef Bacik | 0c4d2d9 | 2012-04-05 15:03:02 -0400 | [diff] [blame] | 3954 | inode_inc_iversion(inode); |
| 3955 | inode_inc_iversion(dir); |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 3956 | inode->i_ctime = dir->i_mtime = dir->i_ctime = CURRENT_TIME; |
Tsutomu Itoh | b995929 | 2012-06-25 21:25:22 -0600 | [diff] [blame] | 3957 | ret = btrfs_update_inode(trans, root, dir); |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 3958 | out: |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3959 | return ret; |
| 3960 | } |
| 3961 | |
Al Viro | 9298679 | 2011-03-04 17:14:37 +0000 | [diff] [blame] | 3962 | int btrfs_unlink_inode(struct btrfs_trans_handle *trans, |
| 3963 | struct btrfs_root *root, |
| 3964 | struct inode *dir, struct inode *inode, |
| 3965 | const char *name, int name_len) |
| 3966 | { |
| 3967 | int ret; |
| 3968 | ret = __btrfs_unlink_inode(trans, root, dir, inode, name, name_len); |
| 3969 | if (!ret) { |
Zach Brown | 8b558c5 | 2013-10-16 12:10:34 -0700 | [diff] [blame] | 3970 | drop_nlink(inode); |
Al Viro | 9298679 | 2011-03-04 17:14:37 +0000 | [diff] [blame] | 3971 | ret = btrfs_update_inode(trans, root, inode); |
| 3972 | } |
| 3973 | return ret; |
| 3974 | } |
Yan, Zheng | a22285a | 2010-05-16 10:48:46 -0400 | [diff] [blame] | 3975 | |
| 3976 | /* |
| 3977 | * helper to start transaction for unlink and rmdir. |
| 3978 | * |
Josef Bacik | d52be81 | 2013-05-29 14:54:47 -0400 | [diff] [blame] | 3979 | * unlink and rmdir are special in btrfs, they do not always free space, so |
| 3980 | * if we cannot make our reservations the normal way try and see if there is |
| 3981 | * plenty of slack room in the global reserve to migrate, otherwise we cannot |
| 3982 | * allow the unlink to occur. |
Yan, Zheng | a22285a | 2010-05-16 10:48:46 -0400 | [diff] [blame] | 3983 | */ |
Josef Bacik | d52be81 | 2013-05-29 14:54:47 -0400 | [diff] [blame] | 3984 | static struct btrfs_trans_handle *__unlink_start_trans(struct inode *dir) |
Yan, Zheng | a22285a | 2010-05-16 10:48:46 -0400 | [diff] [blame] | 3985 | { |
| 3986 | struct btrfs_trans_handle *trans; |
| 3987 | struct btrfs_root *root = BTRFS_I(dir)->root; |
Yan, Zheng | a22285a | 2010-05-16 10:48:46 -0400 | [diff] [blame] | 3988 | int ret; |
| 3989 | |
Josef Bacik | e70bea5 | 2011-10-11 14:18:24 -0400 | [diff] [blame] | 3990 | /* |
| 3991 | * 1 for the possible orphan item |
| 3992 | * 1 for the dir item |
| 3993 | * 1 for the dir index |
| 3994 | * 1 for the inode ref |
Josef Bacik | e70bea5 | 2011-10-11 14:18:24 -0400 | [diff] [blame] | 3995 | * 1 for the inode |
| 3996 | */ |
Josef Bacik | 6e137ed | 2013-03-26 15:26:55 -0400 | [diff] [blame] | 3997 | trans = btrfs_start_transaction(root, 5); |
Yan, Zheng | a22285a | 2010-05-16 10:48:46 -0400 | [diff] [blame] | 3998 | if (!IS_ERR(trans) || PTR_ERR(trans) != -ENOSPC) |
| 3999 | return trans; |
| 4000 | |
Josef Bacik | d52be81 | 2013-05-29 14:54:47 -0400 | [diff] [blame] | 4001 | if (PTR_ERR(trans) == -ENOSPC) { |
| 4002 | u64 num_bytes = btrfs_calc_trans_metadata_size(root, 5); |
Yan, Zheng | a22285a | 2010-05-16 10:48:46 -0400 | [diff] [blame] | 4003 | |
Josef Bacik | d52be81 | 2013-05-29 14:54:47 -0400 | [diff] [blame] | 4004 | trans = btrfs_start_transaction(root, 0); |
| 4005 | if (IS_ERR(trans)) |
| 4006 | return trans; |
| 4007 | ret = btrfs_cond_migrate_bytes(root->fs_info, |
| 4008 | &root->fs_info->trans_block_rsv, |
| 4009 | num_bytes, 5); |
| 4010 | if (ret) { |
| 4011 | btrfs_end_transaction(trans, root); |
| 4012 | return ERR_PTR(ret); |
Yan, Zheng | a22285a | 2010-05-16 10:48:46 -0400 | [diff] [blame] | 4013 | } |
Josef Bacik | 5a77d76 | 2011-11-01 14:32:23 -0400 | [diff] [blame] | 4014 | trans->block_rsv = &root->fs_info->trans_block_rsv; |
Josef Bacik | d52be81 | 2013-05-29 14:54:47 -0400 | [diff] [blame] | 4015 | trans->bytes_reserved = num_bytes; |
Yan, Zheng | a22285a | 2010-05-16 10:48:46 -0400 | [diff] [blame] | 4016 | } |
Josef Bacik | d52be81 | 2013-05-29 14:54:47 -0400 | [diff] [blame] | 4017 | return trans; |
Yan, Zheng | a22285a | 2010-05-16 10:48:46 -0400 | [diff] [blame] | 4018 | } |
| 4019 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4020 | static int btrfs_unlink(struct inode *dir, struct dentry *dentry) |
| 4021 | { |
Yan, Zheng | a22285a | 2010-05-16 10:48:46 -0400 | [diff] [blame] | 4022 | struct btrfs_root *root = BTRFS_I(dir)->root; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4023 | struct btrfs_trans_handle *trans; |
Josef Bacik | 7b12876 | 2008-07-24 12:17:14 -0400 | [diff] [blame] | 4024 | struct inode *inode = dentry->d_inode; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4025 | int ret; |
| 4026 | |
Josef Bacik | d52be81 | 2013-05-29 14:54:47 -0400 | [diff] [blame] | 4027 | trans = __unlink_start_trans(dir); |
Yan, Zheng | a22285a | 2010-05-16 10:48:46 -0400 | [diff] [blame] | 4028 | if (IS_ERR(trans)) |
Josef Bacik | 5df6a9f | 2009-11-10 21:23:48 -0500 | [diff] [blame] | 4029 | return PTR_ERR(trans); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 4030 | |
Chris Mason | 12fcfd2 | 2009-03-24 10:24:20 -0400 | [diff] [blame] | 4031 | btrfs_record_unlink_dir(trans, dir, dentry->d_inode, 0); |
| 4032 | |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 4033 | ret = btrfs_unlink_inode(trans, root, dir, dentry->d_inode, |
| 4034 | dentry->d_name.name, dentry->d_name.len); |
Tsutomu Itoh | b532402 | 2011-07-19 07:27:20 +0000 | [diff] [blame] | 4035 | if (ret) |
| 4036 | goto out; |
Josef Bacik | 7b12876 | 2008-07-24 12:17:14 -0400 | [diff] [blame] | 4037 | |
Yan, Zheng | a22285a | 2010-05-16 10:48:46 -0400 | [diff] [blame] | 4038 | if (inode->i_nlink == 0) { |
Josef Bacik | 7b12876 | 2008-07-24 12:17:14 -0400 | [diff] [blame] | 4039 | ret = btrfs_orphan_add(trans, inode); |
Tsutomu Itoh | b532402 | 2011-07-19 07:27:20 +0000 | [diff] [blame] | 4040 | if (ret) |
| 4041 | goto out; |
Yan, Zheng | a22285a | 2010-05-16 10:48:46 -0400 | [diff] [blame] | 4042 | } |
Josef Bacik | 7b12876 | 2008-07-24 12:17:14 -0400 | [diff] [blame] | 4043 | |
Tsutomu Itoh | b532402 | 2011-07-19 07:27:20 +0000 | [diff] [blame] | 4044 | out: |
Josef Bacik | d52be81 | 2013-05-29 14:54:47 -0400 | [diff] [blame] | 4045 | btrfs_end_transaction(trans, root); |
Liu Bo | b53d3f5 | 2012-11-14 14:34:34 +0000 | [diff] [blame] | 4046 | btrfs_btree_balance_dirty(root); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4047 | return ret; |
| 4048 | } |
| 4049 | |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 4050 | int btrfs_unlink_subvol(struct btrfs_trans_handle *trans, |
| 4051 | struct btrfs_root *root, |
| 4052 | struct inode *dir, u64 objectid, |
| 4053 | const char *name, int name_len) |
| 4054 | { |
| 4055 | struct btrfs_path *path; |
| 4056 | struct extent_buffer *leaf; |
| 4057 | struct btrfs_dir_item *di; |
| 4058 | struct btrfs_key key; |
| 4059 | u64 index; |
| 4060 | int ret; |
Li Zefan | 33345d01 | 2011-04-20 10:31:50 +0800 | [diff] [blame] | 4061 | u64 dir_ino = btrfs_ino(dir); |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 4062 | |
| 4063 | path = btrfs_alloc_path(); |
| 4064 | if (!path) |
| 4065 | return -ENOMEM; |
| 4066 | |
Li Zefan | 33345d01 | 2011-04-20 10:31:50 +0800 | [diff] [blame] | 4067 | di = btrfs_lookup_dir_item(trans, root, path, dir_ino, |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 4068 | name, name_len, -1); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 4069 | if (IS_ERR_OR_NULL(di)) { |
| 4070 | if (!di) |
| 4071 | ret = -ENOENT; |
| 4072 | else |
| 4073 | ret = PTR_ERR(di); |
| 4074 | goto out; |
| 4075 | } |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 4076 | |
| 4077 | leaf = path->nodes[0]; |
| 4078 | btrfs_dir_item_key_to_cpu(leaf, di, &key); |
| 4079 | WARN_ON(key.type != BTRFS_ROOT_ITEM_KEY || key.objectid != objectid); |
| 4080 | ret = btrfs_delete_one_dir_name(trans, root, path, di); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 4081 | if (ret) { |
| 4082 | btrfs_abort_transaction(trans, root, ret); |
| 4083 | goto out; |
| 4084 | } |
David Sterba | b3b4aa7 | 2011-04-21 01:20:15 +0200 | [diff] [blame] | 4085 | btrfs_release_path(path); |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 4086 | |
| 4087 | ret = btrfs_del_root_ref(trans, root->fs_info->tree_root, |
| 4088 | objectid, root->root_key.objectid, |
Li Zefan | 33345d01 | 2011-04-20 10:31:50 +0800 | [diff] [blame] | 4089 | dir_ino, &index, name, name_len); |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 4090 | if (ret < 0) { |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 4091 | if (ret != -ENOENT) { |
| 4092 | btrfs_abort_transaction(trans, root, ret); |
| 4093 | goto out; |
| 4094 | } |
Li Zefan | 33345d01 | 2011-04-20 10:31:50 +0800 | [diff] [blame] | 4095 | di = btrfs_search_dir_index_item(root, path, dir_ino, |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 4096 | name, name_len); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 4097 | if (IS_ERR_OR_NULL(di)) { |
| 4098 | if (!di) |
| 4099 | ret = -ENOENT; |
| 4100 | else |
| 4101 | ret = PTR_ERR(di); |
| 4102 | btrfs_abort_transaction(trans, root, ret); |
| 4103 | goto out; |
| 4104 | } |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 4105 | |
| 4106 | leaf = path->nodes[0]; |
| 4107 | btrfs_item_key_to_cpu(leaf, &key, path->slots[0]); |
David Sterba | b3b4aa7 | 2011-04-21 01:20:15 +0200 | [diff] [blame] | 4108 | btrfs_release_path(path); |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 4109 | index = key.offset; |
| 4110 | } |
David Sterba | b3b4aa7 | 2011-04-21 01:20:15 +0200 | [diff] [blame] | 4111 | btrfs_release_path(path); |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 4112 | |
Miao Xie | 16cdcec | 2011-04-22 18:12:22 +0800 | [diff] [blame] | 4113 | ret = btrfs_delete_delayed_dir_index(trans, root, dir, index); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 4114 | if (ret) { |
| 4115 | btrfs_abort_transaction(trans, root, ret); |
| 4116 | goto out; |
| 4117 | } |
Miao Xie | 16cdcec | 2011-04-22 18:12:22 +0800 | [diff] [blame] | 4118 | |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 4119 | btrfs_i_size_write(dir, dir->i_size - name_len * 2); |
Josef Bacik | 0c4d2d9 | 2012-04-05 15:03:02 -0400 | [diff] [blame] | 4120 | inode_inc_iversion(dir); |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 4121 | dir->i_mtime = dir->i_ctime = CURRENT_TIME; |
Josef Bacik | 5a24e84 | 2012-08-08 10:12:59 -0600 | [diff] [blame] | 4122 | ret = btrfs_update_inode_fallback(trans, root, dir); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 4123 | if (ret) |
| 4124 | btrfs_abort_transaction(trans, root, ret); |
| 4125 | out: |
Josef Bacik | 71d7aed | 2011-06-14 14:24:32 -0400 | [diff] [blame] | 4126 | btrfs_free_path(path); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 4127 | return ret; |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 4128 | } |
| 4129 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4130 | static int btrfs_rmdir(struct inode *dir, struct dentry *dentry) |
| 4131 | { |
| 4132 | struct inode *inode = dentry->d_inode; |
Chris Mason | 1832a6d | 2007-12-21 16:27:21 -0500 | [diff] [blame] | 4133 | int err = 0; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4134 | struct btrfs_root *root = BTRFS_I(dir)->root; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4135 | struct btrfs_trans_handle *trans; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4136 | |
David Sterba | b3ae244 | 2012-09-13 16:04:34 -0600 | [diff] [blame] | 4137 | if (inode->i_size > BTRFS_EMPTY_DIR_SIZE) |
Yan | 134d451 | 2007-10-25 15:49:25 -0400 | [diff] [blame] | 4138 | return -ENOTEMPTY; |
David Sterba | b3ae244 | 2012-09-13 16:04:34 -0600 | [diff] [blame] | 4139 | if (btrfs_ino(inode) == BTRFS_FIRST_FREE_OBJECTID) |
| 4140 | return -EPERM; |
Yan | 134d451 | 2007-10-25 15:49:25 -0400 | [diff] [blame] | 4141 | |
Josef Bacik | d52be81 | 2013-05-29 14:54:47 -0400 | [diff] [blame] | 4142 | trans = __unlink_start_trans(dir); |
Yan, Zheng | a22285a | 2010-05-16 10:48:46 -0400 | [diff] [blame] | 4143 | if (IS_ERR(trans)) |
Josef Bacik | 5df6a9f | 2009-11-10 21:23:48 -0500 | [diff] [blame] | 4144 | return PTR_ERR(trans); |
Josef Bacik | 5df6a9f | 2009-11-10 21:23:48 -0500 | [diff] [blame] | 4145 | |
Li Zefan | 33345d01 | 2011-04-20 10:31:50 +0800 | [diff] [blame] | 4146 | if (unlikely(btrfs_ino(inode) == BTRFS_EMPTY_SUBVOL_DIR_OBJECTID)) { |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 4147 | err = btrfs_unlink_subvol(trans, root, dir, |
| 4148 | BTRFS_I(inode)->location.objectid, |
| 4149 | dentry->d_name.name, |
| 4150 | dentry->d_name.len); |
| 4151 | goto out; |
| 4152 | } |
| 4153 | |
Josef Bacik | 7b12876 | 2008-07-24 12:17:14 -0400 | [diff] [blame] | 4154 | err = btrfs_orphan_add(trans, inode); |
| 4155 | if (err) |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 4156 | goto out; |
Josef Bacik | 7b12876 | 2008-07-24 12:17:14 -0400 | [diff] [blame] | 4157 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4158 | /* now the directory is empty */ |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 4159 | err = btrfs_unlink_inode(trans, root, dir, dentry->d_inode, |
| 4160 | dentry->d_name.name, dentry->d_name.len); |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 4161 | if (!err) |
Chris Mason | dbe674a | 2008-07-17 12:54:05 -0400 | [diff] [blame] | 4162 | btrfs_i_size_write(inode, 0); |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 4163 | out: |
Josef Bacik | d52be81 | 2013-05-29 14:54:47 -0400 | [diff] [blame] | 4164 | btrfs_end_transaction(trans, root); |
Liu Bo | b53d3f5 | 2012-11-14 14:34:34 +0000 | [diff] [blame] | 4165 | btrfs_btree_balance_dirty(root); |
Chris Mason | 3954401 | 2007-12-12 14:38:19 -0500 | [diff] [blame] | 4166 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4167 | return err; |
| 4168 | } |
| 4169 | |
Chris Mason | 28f75a0 | 2015-02-04 06:59:29 -0800 | [diff] [blame] | 4170 | static int truncate_space_check(struct btrfs_trans_handle *trans, |
| 4171 | struct btrfs_root *root, |
| 4172 | u64 bytes_deleted) |
| 4173 | { |
| 4174 | int ret; |
| 4175 | |
| 4176 | bytes_deleted = btrfs_csum_bytes_to_leaves(root, bytes_deleted); |
| 4177 | ret = btrfs_block_rsv_add(root, &root->fs_info->trans_block_rsv, |
| 4178 | bytes_deleted, BTRFS_RESERVE_NO_FLUSH); |
| 4179 | if (!ret) |
| 4180 | trans->bytes_reserved += bytes_deleted; |
| 4181 | return ret; |
| 4182 | |
| 4183 | } |
| 4184 | |
Chris Mason | 323ac95 | 2008-10-01 19:05:46 -0400 | [diff] [blame] | 4185 | /* |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4186 | * this can truncate away extent items, csum items and directory items. |
| 4187 | * 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] | 4188 | * any higher than new_size |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4189 | * |
| 4190 | * csum items that cross the new i_size are truncated to the new size |
| 4191 | * as well. |
Josef Bacik | 7b12876 | 2008-07-24 12:17:14 -0400 | [diff] [blame] | 4192 | * |
| 4193 | * min_type is the minimum key type to truncate down to. If set to 0, this |
| 4194 | * 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] | 4195 | */ |
Yan, Zheng | 8082510 | 2009-11-12 09:35:36 +0000 | [diff] [blame] | 4196 | int btrfs_truncate_inode_items(struct btrfs_trans_handle *trans, |
| 4197 | struct btrfs_root *root, |
| 4198 | struct inode *inode, |
| 4199 | u64 new_size, u32 min_type) |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4200 | { |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4201 | struct btrfs_path *path; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 4202 | struct extent_buffer *leaf; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4203 | struct btrfs_file_extent_item *fi; |
Yan, Zheng | 8082510 | 2009-11-12 09:35:36 +0000 | [diff] [blame] | 4204 | struct btrfs_key key; |
| 4205 | struct btrfs_key found_key; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4206 | u64 extent_start = 0; |
Chris Mason | db94535 | 2007-10-15 16:15:53 -0400 | [diff] [blame] | 4207 | u64 extent_num_bytes = 0; |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 4208 | u64 extent_offset = 0; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4209 | u64 item_end = 0; |
Josef Bacik | 7f4f6e0 | 2013-08-29 16:43:28 -0400 | [diff] [blame] | 4210 | u64 last_size = (u64)-1; |
Yan, Zheng | 8082510 | 2009-11-12 09:35:36 +0000 | [diff] [blame] | 4211 | u32 found_type = (u8)-1; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4212 | int found_extent; |
| 4213 | int del_item; |
Chris Mason | 85e21ba | 2008-01-29 15:11:36 -0500 | [diff] [blame] | 4214 | int pending_del_nr = 0; |
| 4215 | int pending_del_slot = 0; |
Chris Mason | 179e29e | 2007-11-01 11:28:41 -0400 | [diff] [blame] | 4216 | int extent_type = -1; |
Yan, Zheng | 8082510 | 2009-11-12 09:35:36 +0000 | [diff] [blame] | 4217 | int ret; |
| 4218 | int err = 0; |
Li Zefan | 33345d01 | 2011-04-20 10:31:50 +0800 | [diff] [blame] | 4219 | u64 ino = btrfs_ino(inode); |
Chris Mason | 28ed134 | 2014-12-17 09:41:04 -0800 | [diff] [blame] | 4220 | u64 bytes_deleted = 0; |
Josef Bacik | 1262133 | 2015-02-03 07:50:16 -0800 | [diff] [blame] | 4221 | bool be_nice = 0; |
| 4222 | bool should_throttle = 0; |
Chris Mason | 28f75a0 | 2015-02-04 06:59:29 -0800 | [diff] [blame] | 4223 | bool should_end = 0; |
Yan, Zheng | 8082510 | 2009-11-12 09:35:36 +0000 | [diff] [blame] | 4224 | |
| 4225 | BUG_ON(new_size > 0 && min_type != BTRFS_EXTENT_DATA_KEY); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4226 | |
Chris Mason | 28ed134 | 2014-12-17 09:41:04 -0800 | [diff] [blame] | 4227 | /* |
| 4228 | * for non-free space inodes and ref cows, we want to back off from |
| 4229 | * time to time |
| 4230 | */ |
| 4231 | if (!btrfs_is_free_space_inode(inode) && |
| 4232 | test_bit(BTRFS_ROOT_REF_COWS, &root->state)) |
| 4233 | be_nice = 1; |
| 4234 | |
Mark Fasheh | 0eb0e19 | 2011-07-12 16:44:10 -0700 | [diff] [blame] | 4235 | path = btrfs_alloc_path(); |
| 4236 | if (!path) |
| 4237 | return -ENOMEM; |
| 4238 | path->reada = -1; |
| 4239 | |
Josef Bacik | 5dc562c | 2012-08-17 13:14:17 -0400 | [diff] [blame] | 4240 | /* |
| 4241 | * We want to drop from the next block forward in case this new size is |
| 4242 | * not block aligned since we will be keeping the last block of the |
| 4243 | * extent just the way it is. |
| 4244 | */ |
Miao Xie | 27cdeb7 | 2014-04-02 19:51:05 +0800 | [diff] [blame] | 4245 | if (test_bit(BTRFS_ROOT_REF_COWS, &root->state) || |
| 4246 | root == root->fs_info->tree_root) |
Qu Wenruo | fda2832 | 2013-02-26 08:10:22 +0000 | [diff] [blame] | 4247 | btrfs_drop_extent_cache(inode, ALIGN(new_size, |
| 4248 | root->sectorsize), (u64)-1, 0); |
Yan, Zheng | 8082510 | 2009-11-12 09:35:36 +0000 | [diff] [blame] | 4249 | |
Miao Xie | 16cdcec | 2011-04-22 18:12:22 +0800 | [diff] [blame] | 4250 | /* |
| 4251 | * This function is also used to drop the items in the log tree before |
| 4252 | * we relog the inode, so if root != BTRFS_I(inode)->root, it means |
| 4253 | * it is used to drop the loged items. So we shouldn't kill the delayed |
| 4254 | * items. |
| 4255 | */ |
| 4256 | if (min_type == 0 && root == BTRFS_I(inode)->root) |
| 4257 | btrfs_kill_delayed_inode_items(inode); |
| 4258 | |
Li Zefan | 33345d01 | 2011-04-20 10:31:50 +0800 | [diff] [blame] | 4259 | key.objectid = ino; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4260 | key.offset = (u64)-1; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 4261 | key.type = (u8)-1; |
| 4262 | |
Chris Mason | 85e21ba | 2008-01-29 15:11:36 -0500 | [diff] [blame] | 4263 | search_again: |
Chris Mason | 28ed134 | 2014-12-17 09:41:04 -0800 | [diff] [blame] | 4264 | /* |
| 4265 | * with a 16K leaf size and 128MB extents, you can actually queue |
| 4266 | * up a huge file in a single leaf. Most of the time that |
| 4267 | * bytes_deleted is > 0, it will be huge by the time we get here |
| 4268 | */ |
| 4269 | if (be_nice && bytes_deleted > 32 * 1024 * 1024) { |
| 4270 | if (btrfs_should_end_transaction(trans, root)) { |
| 4271 | err = -EAGAIN; |
| 4272 | goto error; |
| 4273 | } |
| 4274 | } |
| 4275 | |
| 4276 | |
Chris Mason | b947343 | 2009-03-13 11:00:37 -0400 | [diff] [blame] | 4277 | path->leave_spinning = 1; |
Chris Mason | 85e21ba | 2008-01-29 15:11:36 -0500 | [diff] [blame] | 4278 | ret = btrfs_search_slot(trans, root, &key, path, -1, 1); |
Yan, Zheng | 8082510 | 2009-11-12 09:35:36 +0000 | [diff] [blame] | 4279 | if (ret < 0) { |
| 4280 | err = ret; |
| 4281 | goto out; |
| 4282 | } |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 4283 | |
Chris Mason | 85e21ba | 2008-01-29 15:11:36 -0500 | [diff] [blame] | 4284 | if (ret > 0) { |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 4285 | /* there are no items in the tree for us to truncate, we're |
| 4286 | * done |
| 4287 | */ |
Yan, Zheng | 8082510 | 2009-11-12 09:35:36 +0000 | [diff] [blame] | 4288 | if (path->slots[0] == 0) |
| 4289 | goto out; |
Chris Mason | 85e21ba | 2008-01-29 15:11:36 -0500 | [diff] [blame] | 4290 | path->slots[0]--; |
| 4291 | } |
| 4292 | |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 4293 | while (1) { |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4294 | fi = NULL; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 4295 | leaf = path->nodes[0]; |
| 4296 | btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]); |
David Sterba | 962a298 | 2014-06-04 18:41:45 +0200 | [diff] [blame] | 4297 | found_type = found_key.type; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4298 | |
Li Zefan | 33345d01 | 2011-04-20 10:31:50 +0800 | [diff] [blame] | 4299 | if (found_key.objectid != ino) |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4300 | break; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 4301 | |
Chris Mason | 85e21ba | 2008-01-29 15:11:36 -0500 | [diff] [blame] | 4302 | if (found_type < min_type) |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4303 | break; |
| 4304 | |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 4305 | item_end = found_key.offset; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4306 | if (found_type == BTRFS_EXTENT_DATA_KEY) { |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 4307 | fi = btrfs_item_ptr(leaf, path->slots[0], |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4308 | struct btrfs_file_extent_item); |
Chris Mason | 179e29e | 2007-11-01 11:28:41 -0400 | [diff] [blame] | 4309 | extent_type = btrfs_file_extent_type(leaf, fi); |
| 4310 | if (extent_type != BTRFS_FILE_EXTENT_INLINE) { |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 4311 | item_end += |
Chris Mason | db94535 | 2007-10-15 16:15:53 -0400 | [diff] [blame] | 4312 | btrfs_file_extent_num_bytes(leaf, fi); |
Chris Mason | 179e29e | 2007-11-01 11:28:41 -0400 | [diff] [blame] | 4313 | } else if (extent_type == BTRFS_FILE_EXTENT_INLINE) { |
Chris Mason | 179e29e | 2007-11-01 11:28:41 -0400 | [diff] [blame] | 4314 | item_end += btrfs_file_extent_inline_len(leaf, |
Chris Mason | 514ac8a | 2014-01-03 21:07:00 -0800 | [diff] [blame] | 4315 | path->slots[0], fi); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4316 | } |
Yan | 008630c | 2007-11-07 13:31:09 -0500 | [diff] [blame] | 4317 | item_end--; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4318 | } |
Yan, Zheng | 8082510 | 2009-11-12 09:35:36 +0000 | [diff] [blame] | 4319 | if (found_type > min_type) { |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4320 | del_item = 1; |
Yan, Zheng | 8082510 | 2009-11-12 09:35:36 +0000 | [diff] [blame] | 4321 | } else { |
| 4322 | if (item_end < new_size) |
| 4323 | break; |
| 4324 | if (found_key.offset >= new_size) |
| 4325 | del_item = 1; |
| 4326 | else |
| 4327 | del_item = 0; |
| 4328 | } |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4329 | found_extent = 0; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4330 | /* FIXME, shrink the extent if the ref count is only 1 */ |
Chris Mason | 179e29e | 2007-11-01 11:28:41 -0400 | [diff] [blame] | 4331 | if (found_type != BTRFS_EXTENT_DATA_KEY) |
| 4332 | goto delete; |
| 4333 | |
Josef Bacik | 7f4f6e0 | 2013-08-29 16:43:28 -0400 | [diff] [blame] | 4334 | if (del_item) |
| 4335 | last_size = found_key.offset; |
| 4336 | else |
| 4337 | last_size = new_size; |
| 4338 | |
Chris Mason | 179e29e | 2007-11-01 11:28:41 -0400 | [diff] [blame] | 4339 | if (extent_type != BTRFS_FILE_EXTENT_INLINE) { |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4340 | u64 num_dec; |
Chris Mason | db94535 | 2007-10-15 16:15:53 -0400 | [diff] [blame] | 4341 | extent_start = btrfs_file_extent_disk_bytenr(leaf, fi); |
Miao Xie | f70a9a6b | 2012-01-12 19:10:12 -0500 | [diff] [blame] | 4342 | if (!del_item) { |
Chris Mason | db94535 | 2007-10-15 16:15:53 -0400 | [diff] [blame] | 4343 | u64 orig_num_bytes = |
| 4344 | btrfs_file_extent_num_bytes(leaf, fi); |
Qu Wenruo | fda2832 | 2013-02-26 08:10:22 +0000 | [diff] [blame] | 4345 | extent_num_bytes = ALIGN(new_size - |
| 4346 | found_key.offset, |
| 4347 | root->sectorsize); |
Chris Mason | db94535 | 2007-10-15 16:15:53 -0400 | [diff] [blame] | 4348 | btrfs_set_file_extent_num_bytes(leaf, fi, |
| 4349 | extent_num_bytes); |
| 4350 | num_dec = (orig_num_bytes - |
Chris Mason | 9069218 | 2008-02-08 13:49:28 -0500 | [diff] [blame] | 4351 | extent_num_bytes); |
Miao Xie | 27cdeb7 | 2014-04-02 19:51:05 +0800 | [diff] [blame] | 4352 | if (test_bit(BTRFS_ROOT_REF_COWS, |
| 4353 | &root->state) && |
| 4354 | extent_start != 0) |
Yan Zheng | a76a3cd | 2008-10-09 11:46:29 -0400 | [diff] [blame] | 4355 | inode_sub_bytes(inode, num_dec); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 4356 | btrfs_mark_buffer_dirty(leaf); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4357 | } else { |
Chris Mason | db94535 | 2007-10-15 16:15:53 -0400 | [diff] [blame] | 4358 | extent_num_bytes = |
| 4359 | btrfs_file_extent_disk_num_bytes(leaf, |
| 4360 | fi); |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 4361 | extent_offset = found_key.offset - |
| 4362 | btrfs_file_extent_offset(leaf, fi); |
| 4363 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4364 | /* FIXME blocksize != 4096 */ |
Chris Mason | 9069218 | 2008-02-08 13:49:28 -0500 | [diff] [blame] | 4365 | num_dec = btrfs_file_extent_num_bytes(leaf, fi); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4366 | if (extent_start != 0) { |
| 4367 | found_extent = 1; |
Miao Xie | 27cdeb7 | 2014-04-02 19:51:05 +0800 | [diff] [blame] | 4368 | if (test_bit(BTRFS_ROOT_REF_COWS, |
| 4369 | &root->state)) |
Yan Zheng | a76a3cd | 2008-10-09 11:46:29 -0400 | [diff] [blame] | 4370 | inode_sub_bytes(inode, num_dec); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4371 | } |
| 4372 | } |
Chris Mason | 9069218 | 2008-02-08 13:49:28 -0500 | [diff] [blame] | 4373 | } else if (extent_type == BTRFS_FILE_EXTENT_INLINE) { |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 4374 | /* |
| 4375 | * we can't truncate inline items that have had |
| 4376 | * special encodings |
| 4377 | */ |
| 4378 | if (!del_item && |
| 4379 | btrfs_file_extent_compression(leaf, fi) == 0 && |
| 4380 | btrfs_file_extent_encryption(leaf, fi) == 0 && |
| 4381 | btrfs_file_extent_other_encoding(leaf, fi) == 0) { |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 4382 | u32 size = new_size - found_key.offset; |
| 4383 | |
Miao Xie | 27cdeb7 | 2014-04-02 19:51:05 +0800 | [diff] [blame] | 4384 | if (test_bit(BTRFS_ROOT_REF_COWS, &root->state)) |
Yan Zheng | a76a3cd | 2008-10-09 11:46:29 -0400 | [diff] [blame] | 4385 | inode_sub_bytes(inode, item_end + 1 - |
| 4386 | new_size); |
Chris Mason | 514ac8a | 2014-01-03 21:07:00 -0800 | [diff] [blame] | 4387 | |
| 4388 | /* |
| 4389 | * update the ram bytes to properly reflect |
| 4390 | * the new size of our item |
| 4391 | */ |
| 4392 | btrfs_set_file_extent_ram_bytes(leaf, fi, size); |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 4393 | size = |
| 4394 | btrfs_file_extent_calc_inline_size(size); |
Tsutomu Itoh | afe5fea | 2013-04-16 05:18:22 +0000 | [diff] [blame] | 4395 | btrfs_truncate_item(root, path, size, 1); |
Miao Xie | 27cdeb7 | 2014-04-02 19:51:05 +0800 | [diff] [blame] | 4396 | } else if (test_bit(BTRFS_ROOT_REF_COWS, |
| 4397 | &root->state)) { |
Yan Zheng | a76a3cd | 2008-10-09 11:46:29 -0400 | [diff] [blame] | 4398 | inode_sub_bytes(inode, item_end + 1 - |
| 4399 | found_key.offset); |
Chris Mason | 9069218 | 2008-02-08 13:49:28 -0500 | [diff] [blame] | 4400 | } |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4401 | } |
Chris Mason | 179e29e | 2007-11-01 11:28:41 -0400 | [diff] [blame] | 4402 | delete: |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4403 | if (del_item) { |
Chris Mason | 85e21ba | 2008-01-29 15:11:36 -0500 | [diff] [blame] | 4404 | if (!pending_del_nr) { |
| 4405 | /* no pending yet, add ourselves */ |
| 4406 | pending_del_slot = path->slots[0]; |
| 4407 | pending_del_nr = 1; |
| 4408 | } else if (pending_del_nr && |
| 4409 | path->slots[0] + 1 == pending_del_slot) { |
| 4410 | /* hop on the pending chunk */ |
| 4411 | pending_del_nr++; |
| 4412 | pending_del_slot = path->slots[0]; |
| 4413 | } else { |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 4414 | BUG(); |
Chris Mason | 85e21ba | 2008-01-29 15:11:36 -0500 | [diff] [blame] | 4415 | } |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4416 | } else { |
| 4417 | break; |
| 4418 | } |
Chris Mason | 28f75a0 | 2015-02-04 06:59:29 -0800 | [diff] [blame] | 4419 | should_throttle = 0; |
| 4420 | |
Miao Xie | 27cdeb7 | 2014-04-02 19:51:05 +0800 | [diff] [blame] | 4421 | if (found_extent && |
| 4422 | (test_bit(BTRFS_ROOT_REF_COWS, &root->state) || |
| 4423 | root == root->fs_info->tree_root)) { |
Chris Mason | b947343 | 2009-03-13 11:00:37 -0400 | [diff] [blame] | 4424 | btrfs_set_path_blocking(path); |
Chris Mason | 28ed134 | 2014-12-17 09:41:04 -0800 | [diff] [blame] | 4425 | bytes_deleted += extent_num_bytes; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4426 | ret = btrfs_free_extent(trans, root, extent_start, |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 4427 | extent_num_bytes, 0, |
| 4428 | btrfs_header_owner(leaf), |
Arne Jansen | 66d7e7f | 2011-09-12 15:26:38 +0200 | [diff] [blame] | 4429 | ino, extent_offset, 0); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4430 | BUG_ON(ret); |
Josef Bacik | 1262133 | 2015-02-03 07:50:16 -0800 | [diff] [blame] | 4431 | if (btrfs_should_throttle_delayed_refs(trans, root)) |
Chris Mason | 28ed134 | 2014-12-17 09:41:04 -0800 | [diff] [blame] | 4432 | btrfs_async_run_delayed_refs(root, |
| 4433 | trans->delayed_ref_updates * 2, 0); |
Chris Mason | 28f75a0 | 2015-02-04 06:59:29 -0800 | [diff] [blame] | 4434 | if (be_nice) { |
| 4435 | if (truncate_space_check(trans, root, |
| 4436 | extent_num_bytes)) { |
| 4437 | should_end = 1; |
| 4438 | } |
| 4439 | if (btrfs_should_throttle_delayed_refs(trans, |
| 4440 | root)) { |
| 4441 | should_throttle = 1; |
| 4442 | } |
| 4443 | } |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4444 | } |
Chris Mason | 85e21ba | 2008-01-29 15:11:36 -0500 | [diff] [blame] | 4445 | |
Yan, Zheng | 8082510 | 2009-11-12 09:35:36 +0000 | [diff] [blame] | 4446 | if (found_type == BTRFS_INODE_ITEM_KEY) |
| 4447 | break; |
| 4448 | |
| 4449 | if (path->slots[0] == 0 || |
Josef Bacik | 1262133 | 2015-02-03 07:50:16 -0800 | [diff] [blame] | 4450 | path->slots[0] != pending_del_slot || |
Chris Mason | 28f75a0 | 2015-02-04 06:59:29 -0800 | [diff] [blame] | 4451 | should_throttle || should_end) { |
Yan, Zheng | 8082510 | 2009-11-12 09:35:36 +0000 | [diff] [blame] | 4452 | if (pending_del_nr) { |
| 4453 | ret = btrfs_del_items(trans, root, path, |
| 4454 | pending_del_slot, |
| 4455 | pending_del_nr); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 4456 | if (ret) { |
| 4457 | btrfs_abort_transaction(trans, |
| 4458 | root, ret); |
| 4459 | goto error; |
| 4460 | } |
Yan, Zheng | 8082510 | 2009-11-12 09:35:36 +0000 | [diff] [blame] | 4461 | pending_del_nr = 0; |
| 4462 | } |
David Sterba | b3b4aa7 | 2011-04-21 01:20:15 +0200 | [diff] [blame] | 4463 | btrfs_release_path(path); |
Chris Mason | 28f75a0 | 2015-02-04 06:59:29 -0800 | [diff] [blame] | 4464 | if (should_throttle) { |
Josef Bacik | 1262133 | 2015-02-03 07:50:16 -0800 | [diff] [blame] | 4465 | unsigned long updates = trans->delayed_ref_updates; |
| 4466 | if (updates) { |
| 4467 | trans->delayed_ref_updates = 0; |
| 4468 | ret = btrfs_run_delayed_refs(trans, root, updates * 2); |
| 4469 | if (ret && !err) |
| 4470 | err = ret; |
| 4471 | } |
| 4472 | } |
Chris Mason | 28f75a0 | 2015-02-04 06:59:29 -0800 | [diff] [blame] | 4473 | /* |
| 4474 | * if we failed to refill our space rsv, bail out |
| 4475 | * and let the transaction restart |
| 4476 | */ |
| 4477 | if (should_end) { |
| 4478 | err = -EAGAIN; |
| 4479 | goto error; |
| 4480 | } |
Chris Mason | 85e21ba | 2008-01-29 15:11:36 -0500 | [diff] [blame] | 4481 | goto search_again; |
Yan, Zheng | 8082510 | 2009-11-12 09:35:36 +0000 | [diff] [blame] | 4482 | } else { |
| 4483 | path->slots[0]--; |
Chris Mason | 85e21ba | 2008-01-29 15:11:36 -0500 | [diff] [blame] | 4484 | } |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4485 | } |
Yan, Zheng | 8082510 | 2009-11-12 09:35:36 +0000 | [diff] [blame] | 4486 | out: |
Chris Mason | 85e21ba | 2008-01-29 15:11:36 -0500 | [diff] [blame] | 4487 | if (pending_del_nr) { |
| 4488 | ret = btrfs_del_items(trans, root, path, pending_del_slot, |
| 4489 | pending_del_nr); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 4490 | if (ret) |
| 4491 | btrfs_abort_transaction(trans, root, ret); |
Chris Mason | 85e21ba | 2008-01-29 15:11:36 -0500 | [diff] [blame] | 4492 | } |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 4493 | error: |
Filipe Manana | dac5705 | 2014-08-29 20:54:26 +0100 | [diff] [blame] | 4494 | if (last_size != (u64)-1 && |
| 4495 | root->root_key.objectid != BTRFS_TREE_LOG_OBJECTID) |
Josef Bacik | 7f4f6e0 | 2013-08-29 16:43:28 -0400 | [diff] [blame] | 4496 | btrfs_ordered_update_i_size(inode, last_size, NULL); |
Chris Mason | 28ed134 | 2014-12-17 09:41:04 -0800 | [diff] [blame] | 4497 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4498 | btrfs_free_path(path); |
Chris Mason | 28ed134 | 2014-12-17 09:41:04 -0800 | [diff] [blame] | 4499 | |
Chris Mason | 28f75a0 | 2015-02-04 06:59:29 -0800 | [diff] [blame] | 4500 | if (be_nice && bytes_deleted > 32 * 1024 * 1024) { |
Chris Mason | 28ed134 | 2014-12-17 09:41:04 -0800 | [diff] [blame] | 4501 | unsigned long updates = trans->delayed_ref_updates; |
| 4502 | if (updates) { |
| 4503 | trans->delayed_ref_updates = 0; |
| 4504 | ret = btrfs_run_delayed_refs(trans, root, updates * 2); |
| 4505 | if (ret && !err) |
| 4506 | err = ret; |
| 4507 | } |
| 4508 | } |
Yan, Zheng | 8082510 | 2009-11-12 09:35:36 +0000 | [diff] [blame] | 4509 | return err; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4510 | } |
| 4511 | |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 4512 | /* |
Josef Bacik | 2aaa665 | 2012-08-29 14:27:18 -0400 | [diff] [blame] | 4513 | * btrfs_truncate_page - read, zero a chunk and write a page |
| 4514 | * @inode - inode that we're zeroing |
| 4515 | * @from - the offset to start zeroing |
| 4516 | * @len - the length to zero, 0 to zero the entire range respective to the |
| 4517 | * offset |
| 4518 | * @front - zero up to the offset instead of from the offset on |
| 4519 | * |
| 4520 | * This will find the page for the "from" offset and cow the page and zero the |
| 4521 | * 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] | 4522 | */ |
Josef Bacik | 2aaa665 | 2012-08-29 14:27:18 -0400 | [diff] [blame] | 4523 | int btrfs_truncate_page(struct inode *inode, loff_t from, loff_t len, |
| 4524 | int front) |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 4525 | { |
Josef Bacik | 2aaa665 | 2012-08-29 14:27:18 -0400 | [diff] [blame] | 4526 | struct address_space *mapping = inode->i_mapping; |
Chris Mason | db94535 | 2007-10-15 16:15:53 -0400 | [diff] [blame] | 4527 | struct btrfs_root *root = BTRFS_I(inode)->root; |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 4528 | struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree; |
| 4529 | struct btrfs_ordered_extent *ordered; |
Josef Bacik | 2ac55d4 | 2010-02-03 19:33:23 +0000 | [diff] [blame] | 4530 | struct extent_state *cached_state = NULL; |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 4531 | char *kaddr; |
Chris Mason | db94535 | 2007-10-15 16:15:53 -0400 | [diff] [blame] | 4532 | u32 blocksize = root->sectorsize; |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 4533 | pgoff_t index = from >> PAGE_CACHE_SHIFT; |
| 4534 | unsigned offset = from & (PAGE_CACHE_SIZE-1); |
| 4535 | struct page *page; |
Josef Bacik | 3b16a4e | 2011-09-21 15:05:58 -0400 | [diff] [blame] | 4536 | gfp_t mask = btrfs_alloc_write_mask(mapping); |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 4537 | int ret = 0; |
| 4538 | u64 page_start; |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 4539 | u64 page_end; |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 4540 | |
Josef Bacik | 2aaa665 | 2012-08-29 14:27:18 -0400 | [diff] [blame] | 4541 | if ((offset & (blocksize - 1)) == 0 && |
| 4542 | (!len || ((len & (blocksize - 1)) == 0))) |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 4543 | goto out; |
Yan, Zheng | 0ca1f7c | 2010-05-16 10:48:47 -0400 | [diff] [blame] | 4544 | ret = btrfs_delalloc_reserve_space(inode, PAGE_CACHE_SIZE); |
Josef Bacik | 5d5e103 | 2009-10-13 16:46:49 -0400 | [diff] [blame] | 4545 | if (ret) |
| 4546 | goto out; |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 4547 | |
Chris Mason | 211c17f | 2008-05-15 09:13:45 -0400 | [diff] [blame] | 4548 | again: |
Josef Bacik | 3b16a4e | 2011-09-21 15:05:58 -0400 | [diff] [blame] | 4549 | page = find_or_create_page(mapping, index, mask); |
Josef Bacik | 5d5e103 | 2009-10-13 16:46:49 -0400 | [diff] [blame] | 4550 | if (!page) { |
Yan, Zheng | 0ca1f7c | 2010-05-16 10:48:47 -0400 | [diff] [blame] | 4551 | btrfs_delalloc_release_space(inode, PAGE_CACHE_SIZE); |
Miao Xie | ac6a2b3 | 2012-12-05 10:56:13 +0000 | [diff] [blame] | 4552 | ret = -ENOMEM; |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 4553 | goto out; |
Josef Bacik | 5d5e103 | 2009-10-13 16:46:49 -0400 | [diff] [blame] | 4554 | } |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 4555 | |
| 4556 | page_start = page_offset(page); |
| 4557 | page_end = page_start + PAGE_CACHE_SIZE - 1; |
| 4558 | |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 4559 | if (!PageUptodate(page)) { |
| 4560 | ret = btrfs_readpage(NULL, page); |
| 4561 | lock_page(page); |
Chris Mason | 211c17f | 2008-05-15 09:13:45 -0400 | [diff] [blame] | 4562 | if (page->mapping != mapping) { |
| 4563 | unlock_page(page); |
| 4564 | page_cache_release(page); |
| 4565 | goto again; |
| 4566 | } |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 4567 | if (!PageUptodate(page)) { |
| 4568 | ret = -EIO; |
Chris Mason | 8964222 | 2008-07-24 09:41:53 -0400 | [diff] [blame] | 4569 | goto out_unlock; |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 4570 | } |
| 4571 | } |
Chris Mason | 211c17f | 2008-05-15 09:13:45 -0400 | [diff] [blame] | 4572 | wait_on_page_writeback(page); |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 4573 | |
Jeff Mahoney | d008237 | 2012-03-01 14:57:19 +0100 | [diff] [blame] | 4574 | lock_extent_bits(io_tree, page_start, page_end, 0, &cached_state); |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 4575 | set_page_extent_mapped(page); |
| 4576 | |
| 4577 | ordered = btrfs_lookup_ordered_extent(inode, page_start); |
| 4578 | if (ordered) { |
Josef Bacik | 2ac55d4 | 2010-02-03 19:33:23 +0000 | [diff] [blame] | 4579 | unlock_extent_cached(io_tree, page_start, page_end, |
| 4580 | &cached_state, GFP_NOFS); |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 4581 | unlock_page(page); |
| 4582 | page_cache_release(page); |
Chris Mason | eb84ae0 | 2008-07-17 13:53:27 -0400 | [diff] [blame] | 4583 | btrfs_start_ordered_extent(inode, ordered, 1); |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 4584 | btrfs_put_ordered_extent(ordered); |
| 4585 | goto again; |
| 4586 | } |
| 4587 | |
Josef Bacik | 2ac55d4 | 2010-02-03 19:33:23 +0000 | [diff] [blame] | 4588 | clear_extent_bit(&BTRFS_I(inode)->io_tree, page_start, page_end, |
Liu Bo | 9e8a4a8 | 2012-09-05 19:10:51 -0600 | [diff] [blame] | 4589 | EXTENT_DIRTY | EXTENT_DELALLOC | |
| 4590 | EXTENT_DO_ACCOUNTING | EXTENT_DEFRAG, |
Josef Bacik | 2ac55d4 | 2010-02-03 19:33:23 +0000 | [diff] [blame] | 4591 | 0, 0, &cached_state, GFP_NOFS); |
Josef Bacik | 5d5e103 | 2009-10-13 16:46:49 -0400 | [diff] [blame] | 4592 | |
Josef Bacik | 2ac55d4 | 2010-02-03 19:33:23 +0000 | [diff] [blame] | 4593 | ret = btrfs_set_extent_delalloc(inode, page_start, page_end, |
| 4594 | &cached_state); |
Josef Bacik | 9ed74f2 | 2009-09-11 16:12:44 -0400 | [diff] [blame] | 4595 | if (ret) { |
Josef Bacik | 2ac55d4 | 2010-02-03 19:33:23 +0000 | [diff] [blame] | 4596 | unlock_extent_cached(io_tree, page_start, page_end, |
| 4597 | &cached_state, GFP_NOFS); |
Josef Bacik | 9ed74f2 | 2009-09-11 16:12:44 -0400 | [diff] [blame] | 4598 | goto out_unlock; |
| 4599 | } |
| 4600 | |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 4601 | if (offset != PAGE_CACHE_SIZE) { |
Josef Bacik | 2aaa665 | 2012-08-29 14:27:18 -0400 | [diff] [blame] | 4602 | if (!len) |
| 4603 | len = PAGE_CACHE_SIZE - offset; |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 4604 | kaddr = kmap(page); |
Josef Bacik | 2aaa665 | 2012-08-29 14:27:18 -0400 | [diff] [blame] | 4605 | if (front) |
| 4606 | memset(kaddr, 0, offset); |
| 4607 | else |
| 4608 | memset(kaddr + offset, 0, len); |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 4609 | flush_dcache_page(page); |
| 4610 | kunmap(page); |
| 4611 | } |
Chris Mason | 247e743 | 2008-07-17 12:53:51 -0400 | [diff] [blame] | 4612 | ClearPageChecked(page); |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 4613 | set_page_dirty(page); |
Josef Bacik | 2ac55d4 | 2010-02-03 19:33:23 +0000 | [diff] [blame] | 4614 | unlock_extent_cached(io_tree, page_start, page_end, &cached_state, |
| 4615 | GFP_NOFS); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4616 | |
Chris Mason | 8964222 | 2008-07-24 09:41:53 -0400 | [diff] [blame] | 4617 | out_unlock: |
Josef Bacik | 5d5e103 | 2009-10-13 16:46:49 -0400 | [diff] [blame] | 4618 | if (ret) |
Yan, Zheng | 0ca1f7c | 2010-05-16 10:48:47 -0400 | [diff] [blame] | 4619 | btrfs_delalloc_release_space(inode, PAGE_CACHE_SIZE); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4620 | unlock_page(page); |
| 4621 | page_cache_release(page); |
| 4622 | out: |
| 4623 | return ret; |
| 4624 | } |
| 4625 | |
Josef Bacik | 16e7549 | 2013-10-22 12:18:51 -0400 | [diff] [blame] | 4626 | static int maybe_insert_hole(struct btrfs_root *root, struct inode *inode, |
| 4627 | u64 offset, u64 len) |
| 4628 | { |
| 4629 | struct btrfs_trans_handle *trans; |
| 4630 | int ret; |
| 4631 | |
| 4632 | /* |
| 4633 | * Still need to make sure the inode looks like it's been updated so |
| 4634 | * that any holes get logged if we fsync. |
| 4635 | */ |
| 4636 | if (btrfs_fs_incompat(root->fs_info, NO_HOLES)) { |
| 4637 | BTRFS_I(inode)->last_trans = root->fs_info->generation; |
| 4638 | BTRFS_I(inode)->last_sub_trans = root->log_transid; |
| 4639 | BTRFS_I(inode)->last_log_commit = root->last_log_commit; |
| 4640 | return 0; |
| 4641 | } |
| 4642 | |
| 4643 | /* |
| 4644 | * 1 - for the one we're dropping |
| 4645 | * 1 - for the one we're adding |
| 4646 | * 1 - for updating the inode. |
| 4647 | */ |
| 4648 | trans = btrfs_start_transaction(root, 3); |
| 4649 | if (IS_ERR(trans)) |
| 4650 | return PTR_ERR(trans); |
| 4651 | |
| 4652 | ret = btrfs_drop_extents(trans, root, inode, offset, offset + len, 1); |
| 4653 | if (ret) { |
| 4654 | btrfs_abort_transaction(trans, root, ret); |
| 4655 | btrfs_end_transaction(trans, root); |
| 4656 | return ret; |
| 4657 | } |
| 4658 | |
| 4659 | ret = btrfs_insert_file_extent(trans, root, btrfs_ino(inode), offset, |
| 4660 | 0, 0, len, 0, len, 0, 0, 0); |
| 4661 | if (ret) |
| 4662 | btrfs_abort_transaction(trans, root, ret); |
| 4663 | else |
| 4664 | btrfs_update_inode(trans, root, inode); |
| 4665 | btrfs_end_transaction(trans, root); |
| 4666 | return ret; |
| 4667 | } |
| 4668 | |
Josef Bacik | 695a0d0 | 2011-03-04 15:46:53 -0500 | [diff] [blame] | 4669 | /* |
| 4670 | * This function puts in dummy file extents for the area we're creating a hole |
| 4671 | * for. So if we are truncating this file to a larger size we need to insert |
| 4672 | * these file extents so that btrfs_get_extent will return a EXTENT_MAP_HOLE for |
| 4673 | * the range between oldsize and size |
| 4674 | */ |
Josef Bacik | a41ad39 | 2011-01-31 15:30:16 -0500 | [diff] [blame] | 4675 | 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] | 4676 | { |
Yan Zheng | 9036c10 | 2008-10-30 14:19:41 -0400 | [diff] [blame] | 4677 | struct btrfs_root *root = BTRFS_I(inode)->root; |
| 4678 | struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree; |
Yan, Zheng | a22285a | 2010-05-16 10:48:46 -0400 | [diff] [blame] | 4679 | struct extent_map *em = NULL; |
Josef Bacik | 2ac55d4 | 2010-02-03 19:33:23 +0000 | [diff] [blame] | 4680 | struct extent_state *cached_state = NULL; |
Josef Bacik | 5dc562c | 2012-08-17 13:14:17 -0400 | [diff] [blame] | 4681 | struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree; |
Qu Wenruo | fda2832 | 2013-02-26 08:10:22 +0000 | [diff] [blame] | 4682 | u64 hole_start = ALIGN(oldsize, root->sectorsize); |
| 4683 | u64 block_end = ALIGN(size, root->sectorsize); |
Yan Zheng | 9036c10 | 2008-10-30 14:19:41 -0400 | [diff] [blame] | 4684 | u64 last_byte; |
| 4685 | u64 cur_offset; |
| 4686 | u64 hole_size; |
Josef Bacik | 9ed74f2 | 2009-09-11 16:12:44 -0400 | [diff] [blame] | 4687 | int err = 0; |
Yan Zheng | 9036c10 | 2008-10-30 14:19:41 -0400 | [diff] [blame] | 4688 | |
Josef Bacik | a71754f | 2013-06-17 17:14:39 -0400 | [diff] [blame] | 4689 | /* |
| 4690 | * If our size started in the middle of a page we need to zero out the |
| 4691 | * rest of the page before we expand the i_size, otherwise we could |
| 4692 | * expose stale data. |
| 4693 | */ |
| 4694 | err = btrfs_truncate_page(inode, oldsize, 0, 0); |
| 4695 | if (err) |
| 4696 | return err; |
| 4697 | |
Yan Zheng | 9036c10 | 2008-10-30 14:19:41 -0400 | [diff] [blame] | 4698 | if (size <= hole_start) |
| 4699 | return 0; |
| 4700 | |
Yan Zheng | 9036c10 | 2008-10-30 14:19:41 -0400 | [diff] [blame] | 4701 | while (1) { |
| 4702 | struct btrfs_ordered_extent *ordered; |
Miao Xie | fa7c149 | 2013-09-26 13:15:27 +0800 | [diff] [blame] | 4703 | |
Josef Bacik | 2ac55d4 | 2010-02-03 19:33:23 +0000 | [diff] [blame] | 4704 | lock_extent_bits(io_tree, hole_start, block_end - 1, 0, |
Jeff Mahoney | d008237 | 2012-03-01 14:57:19 +0100 | [diff] [blame] | 4705 | &cached_state); |
Miao Xie | fa7c149 | 2013-09-26 13:15:27 +0800 | [diff] [blame] | 4706 | ordered = btrfs_lookup_ordered_range(inode, hole_start, |
| 4707 | block_end - hole_start); |
Yan Zheng | 9036c10 | 2008-10-30 14:19:41 -0400 | [diff] [blame] | 4708 | if (!ordered) |
| 4709 | break; |
Josef Bacik | 2ac55d4 | 2010-02-03 19:33:23 +0000 | [diff] [blame] | 4710 | unlock_extent_cached(io_tree, hole_start, block_end - 1, |
| 4711 | &cached_state, GFP_NOFS); |
Miao Xie | fa7c149 | 2013-09-26 13:15:27 +0800 | [diff] [blame] | 4712 | btrfs_start_ordered_extent(inode, ordered, 1); |
Yan Zheng | 9036c10 | 2008-10-30 14:19:41 -0400 | [diff] [blame] | 4713 | btrfs_put_ordered_extent(ordered); |
| 4714 | } |
| 4715 | |
Yan Zheng | 9036c10 | 2008-10-30 14:19:41 -0400 | [diff] [blame] | 4716 | cur_offset = hole_start; |
| 4717 | while (1) { |
| 4718 | em = btrfs_get_extent(inode, NULL, 0, cur_offset, |
| 4719 | block_end - cur_offset, 0); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 4720 | if (IS_ERR(em)) { |
| 4721 | err = PTR_ERR(em); |
Zach Brown | f276795 | 2013-01-08 19:37:58 +0000 | [diff] [blame] | 4722 | em = NULL; |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 4723 | break; |
| 4724 | } |
Yan Zheng | 9036c10 | 2008-10-30 14:19:41 -0400 | [diff] [blame] | 4725 | last_byte = min(extent_map_end(em), block_end); |
Qu Wenruo | fda2832 | 2013-02-26 08:10:22 +0000 | [diff] [blame] | 4726 | last_byte = ALIGN(last_byte , root->sectorsize); |
Yan, Zheng | 8082510 | 2009-11-12 09:35:36 +0000 | [diff] [blame] | 4727 | if (!test_bit(EXTENT_FLAG_PREALLOC, &em->flags)) { |
Josef Bacik | 5dc562c | 2012-08-17 13:14:17 -0400 | [diff] [blame] | 4728 | struct extent_map *hole_em; |
Yan Zheng | 9036c10 | 2008-10-30 14:19:41 -0400 | [diff] [blame] | 4729 | hole_size = last_byte - cur_offset; |
Yan, Zheng | 8082510 | 2009-11-12 09:35:36 +0000 | [diff] [blame] | 4730 | |
Josef Bacik | 16e7549 | 2013-10-22 12:18:51 -0400 | [diff] [blame] | 4731 | err = maybe_insert_hole(root, inode, cur_offset, |
| 4732 | hole_size); |
| 4733 | if (err) |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 4734 | break; |
Josef Bacik | 5dc562c | 2012-08-17 13:14:17 -0400 | [diff] [blame] | 4735 | btrfs_drop_extent_cache(inode, cur_offset, |
| 4736 | cur_offset + hole_size - 1, 0); |
| 4737 | hole_em = alloc_extent_map(); |
| 4738 | if (!hole_em) { |
| 4739 | set_bit(BTRFS_INODE_NEEDS_FULL_SYNC, |
| 4740 | &BTRFS_I(inode)->runtime_flags); |
| 4741 | goto next; |
| 4742 | } |
| 4743 | hole_em->start = cur_offset; |
| 4744 | hole_em->len = hole_size; |
| 4745 | hole_em->orig_start = cur_offset; |
Yan, Zheng | 8082510 | 2009-11-12 09:35:36 +0000 | [diff] [blame] | 4746 | |
Josef Bacik | 5dc562c | 2012-08-17 13:14:17 -0400 | [diff] [blame] | 4747 | hole_em->block_start = EXTENT_MAP_HOLE; |
| 4748 | hole_em->block_len = 0; |
Josef Bacik | b493968 | 2012-12-03 10:31:19 -0500 | [diff] [blame] | 4749 | hole_em->orig_block_len = 0; |
Josef Bacik | cc95bef | 2013-04-04 14:31:27 -0400 | [diff] [blame] | 4750 | hole_em->ram_bytes = hole_size; |
Josef Bacik | 5dc562c | 2012-08-17 13:14:17 -0400 | [diff] [blame] | 4751 | hole_em->bdev = root->fs_info->fs_devices->latest_bdev; |
| 4752 | hole_em->compress_type = BTRFS_COMPRESS_NONE; |
Josef Bacik | 16e7549 | 2013-10-22 12:18:51 -0400 | [diff] [blame] | 4753 | hole_em->generation = root->fs_info->generation; |
Josef Bacik | 5dc562c | 2012-08-17 13:14:17 -0400 | [diff] [blame] | 4754 | |
| 4755 | while (1) { |
| 4756 | write_lock(&em_tree->lock); |
Josef Bacik | 09a2a8f9 | 2013-04-05 16:51:15 -0400 | [diff] [blame] | 4757 | err = add_extent_mapping(em_tree, hole_em, 1); |
Josef Bacik | 5dc562c | 2012-08-17 13:14:17 -0400 | [diff] [blame] | 4758 | write_unlock(&em_tree->lock); |
| 4759 | if (err != -EEXIST) |
| 4760 | break; |
| 4761 | btrfs_drop_extent_cache(inode, cur_offset, |
| 4762 | cur_offset + |
| 4763 | hole_size - 1, 0); |
| 4764 | } |
| 4765 | free_extent_map(hole_em); |
Yan Zheng | 9036c10 | 2008-10-30 14:19:41 -0400 | [diff] [blame] | 4766 | } |
Josef Bacik | 16e7549 | 2013-10-22 12:18:51 -0400 | [diff] [blame] | 4767 | next: |
Yan Zheng | 9036c10 | 2008-10-30 14:19:41 -0400 | [diff] [blame] | 4768 | free_extent_map(em); |
Yan, Zheng | a22285a | 2010-05-16 10:48:46 -0400 | [diff] [blame] | 4769 | em = NULL; |
Yan Zheng | 9036c10 | 2008-10-30 14:19:41 -0400 | [diff] [blame] | 4770 | cur_offset = last_byte; |
Yan, Zheng | 8082510 | 2009-11-12 09:35:36 +0000 | [diff] [blame] | 4771 | if (cur_offset >= block_end) |
Yan Zheng | 9036c10 | 2008-10-30 14:19:41 -0400 | [diff] [blame] | 4772 | break; |
| 4773 | } |
Yan, Zheng | a22285a | 2010-05-16 10:48:46 -0400 | [diff] [blame] | 4774 | free_extent_map(em); |
Josef Bacik | 2ac55d4 | 2010-02-03 19:33:23 +0000 | [diff] [blame] | 4775 | unlock_extent_cached(io_tree, hole_start, block_end - 1, &cached_state, |
| 4776 | GFP_NOFS); |
Yan Zheng | 9036c10 | 2008-10-30 14:19:41 -0400 | [diff] [blame] | 4777 | return err; |
| 4778 | } |
| 4779 | |
Filipe Manana | 9ea24bb | 2014-10-29 11:57:59 +0000 | [diff] [blame] | 4780 | static int wait_snapshoting_atomic_t(atomic_t *a) |
| 4781 | { |
| 4782 | schedule(); |
| 4783 | return 0; |
| 4784 | } |
| 4785 | |
| 4786 | static void wait_for_snapshot_creation(struct btrfs_root *root) |
| 4787 | { |
| 4788 | while (true) { |
| 4789 | int ret; |
| 4790 | |
| 4791 | ret = btrfs_start_write_no_snapshoting(root); |
| 4792 | if (ret) |
| 4793 | break; |
| 4794 | wait_on_atomic_t(&root->will_be_snapshoted, |
| 4795 | wait_snapshoting_atomic_t, |
| 4796 | TASK_UNINTERRUPTIBLE); |
| 4797 | } |
| 4798 | } |
| 4799 | |
Eric Sandeen | 3972f26 | 2013-01-12 02:57:22 +0000 | [diff] [blame] | 4800 | static int btrfs_setsize(struct inode *inode, struct iattr *attr) |
Yan, Zheng | 8082510 | 2009-11-12 09:35:36 +0000 | [diff] [blame] | 4801 | { |
Miao Xie | f4a2f4c | 2011-12-14 20:12:01 -0500 | [diff] [blame] | 4802 | struct btrfs_root *root = BTRFS_I(inode)->root; |
| 4803 | struct btrfs_trans_handle *trans; |
Josef Bacik | a41ad39 | 2011-01-31 15:30:16 -0500 | [diff] [blame] | 4804 | loff_t oldsize = i_size_read(inode); |
Eric Sandeen | 3972f26 | 2013-01-12 02:57:22 +0000 | [diff] [blame] | 4805 | loff_t newsize = attr->ia_size; |
| 4806 | int mask = attr->ia_valid; |
Yan, Zheng | 8082510 | 2009-11-12 09:35:36 +0000 | [diff] [blame] | 4807 | int ret; |
| 4808 | |
Eric Sandeen | 3972f26 | 2013-01-12 02:57:22 +0000 | [diff] [blame] | 4809 | /* |
| 4810 | * The regular truncate() case without ATTR_CTIME and ATTR_MTIME is a |
| 4811 | * special case where we need to update the times despite not having |
| 4812 | * these flags set. For all other operations the VFS set these flags |
| 4813 | * explicitly if it wants a timestamp update. |
| 4814 | */ |
Christoph Hellwig | dff6efc | 2013-11-19 07:17:07 -0800 | [diff] [blame] | 4815 | if (newsize != oldsize) { |
| 4816 | inode_inc_iversion(inode); |
| 4817 | if (!(mask & (ATTR_CTIME | ATTR_MTIME))) |
| 4818 | inode->i_ctime = inode->i_mtime = |
| 4819 | current_fs_time(inode->i_sb); |
| 4820 | } |
Eric Sandeen | 3972f26 | 2013-01-12 02:57:22 +0000 | [diff] [blame] | 4821 | |
Josef Bacik | a41ad39 | 2011-01-31 15:30:16 -0500 | [diff] [blame] | 4822 | if (newsize > oldsize) { |
Kirill A. Shutemov | 7caef26 | 2013-09-12 15:13:56 -0700 | [diff] [blame] | 4823 | truncate_pagecache(inode, newsize); |
Filipe Manana | 9ea24bb | 2014-10-29 11:57:59 +0000 | [diff] [blame] | 4824 | /* |
| 4825 | * Don't do an expanding truncate while snapshoting is ongoing. |
| 4826 | * This is to ensure the snapshot captures a fully consistent |
| 4827 | * state of this file - if the snapshot captures this expanding |
| 4828 | * truncation, it must capture all writes that happened before |
| 4829 | * this truncation. |
| 4830 | */ |
| 4831 | wait_for_snapshot_creation(root); |
Josef Bacik | a41ad39 | 2011-01-31 15:30:16 -0500 | [diff] [blame] | 4832 | ret = btrfs_cont_expand(inode, oldsize, newsize); |
Filipe Manana | 9ea24bb | 2014-10-29 11:57:59 +0000 | [diff] [blame] | 4833 | if (ret) { |
| 4834 | btrfs_end_write_no_snapshoting(root); |
Yan, Zheng | 8082510 | 2009-11-12 09:35:36 +0000 | [diff] [blame] | 4835 | return ret; |
Filipe Manana | 9ea24bb | 2014-10-29 11:57:59 +0000 | [diff] [blame] | 4836 | } |
Yan, Zheng | 8082510 | 2009-11-12 09:35:36 +0000 | [diff] [blame] | 4837 | |
Miao Xie | f4a2f4c | 2011-12-14 20:12:01 -0500 | [diff] [blame] | 4838 | trans = btrfs_start_transaction(root, 1); |
Filipe Manana | 9ea24bb | 2014-10-29 11:57:59 +0000 | [diff] [blame] | 4839 | if (IS_ERR(trans)) { |
| 4840 | btrfs_end_write_no_snapshoting(root); |
Miao Xie | f4a2f4c | 2011-12-14 20:12:01 -0500 | [diff] [blame] | 4841 | return PTR_ERR(trans); |
Filipe Manana | 9ea24bb | 2014-10-29 11:57:59 +0000 | [diff] [blame] | 4842 | } |
Miao Xie | f4a2f4c | 2011-12-14 20:12:01 -0500 | [diff] [blame] | 4843 | |
| 4844 | i_size_write(inode, newsize); |
| 4845 | btrfs_ordered_update_i_size(inode, i_size_read(inode), NULL); |
| 4846 | ret = btrfs_update_inode(trans, root, inode); |
Filipe Manana | 9ea24bb | 2014-10-29 11:57:59 +0000 | [diff] [blame] | 4847 | btrfs_end_write_no_snapshoting(root); |
Josef Bacik | 7ad85bb | 2012-01-12 19:10:12 -0500 | [diff] [blame] | 4848 | btrfs_end_transaction(trans, root); |
Josef Bacik | a41ad39 | 2011-01-31 15:30:16 -0500 | [diff] [blame] | 4849 | } else { |
Yan, Zheng | 8082510 | 2009-11-12 09:35:36 +0000 | [diff] [blame] | 4850 | |
Josef Bacik | a41ad39 | 2011-01-31 15:30:16 -0500 | [diff] [blame] | 4851 | /* |
| 4852 | * We're truncating a file that used to have good data down to |
| 4853 | * zero. Make sure it gets into the ordered flush list so that |
| 4854 | * any new writes get down to disk quickly. |
| 4855 | */ |
| 4856 | if (newsize == 0) |
Josef Bacik | 72ac3c0 | 2012-05-23 14:13:11 -0400 | [diff] [blame] | 4857 | set_bit(BTRFS_INODE_ORDERED_DATA_CLOSE, |
| 4858 | &BTRFS_I(inode)->runtime_flags); |
Yan, Zheng | 8082510 | 2009-11-12 09:35:36 +0000 | [diff] [blame] | 4859 | |
Josef Bacik | f3fe820 | 2013-01-07 17:03:21 -0500 | [diff] [blame] | 4860 | /* |
| 4861 | * 1 for the orphan item we're going to add |
| 4862 | * 1 for the orphan item deletion. |
| 4863 | */ |
| 4864 | trans = btrfs_start_transaction(root, 2); |
| 4865 | if (IS_ERR(trans)) |
| 4866 | return PTR_ERR(trans); |
| 4867 | |
| 4868 | /* |
| 4869 | * We need to do this in case we fail at _any_ point during the |
| 4870 | * actual truncate. Once we do the truncate_setsize we could |
| 4871 | * invalidate pages which forces any outstanding ordered io to |
| 4872 | * be instantly completed which will give us extents that need |
| 4873 | * to be truncated. If we fail to get an orphan inode down we |
| 4874 | * could have left over extents that were never meant to live, |
| 4875 | * so we need to garuntee from this point on that everything |
| 4876 | * will be consistent. |
| 4877 | */ |
| 4878 | ret = btrfs_orphan_add(trans, inode); |
| 4879 | btrfs_end_transaction(trans, root); |
| 4880 | if (ret) |
| 4881 | return ret; |
| 4882 | |
Josef Bacik | a41ad39 | 2011-01-31 15:30:16 -0500 | [diff] [blame] | 4883 | /* we don't support swapfiles, so vmtruncate shouldn't fail */ |
| 4884 | truncate_setsize(inode, newsize); |
Miao Xie | 2e60a51 | 2013-02-08 07:01:08 +0000 | [diff] [blame] | 4885 | |
| 4886 | /* Disable nonlocked read DIO to avoid the end less truncate */ |
| 4887 | btrfs_inode_block_unlocked_dio(inode); |
| 4888 | inode_dio_wait(inode); |
| 4889 | btrfs_inode_resume_unlocked_dio(inode); |
| 4890 | |
Josef Bacik | a41ad39 | 2011-01-31 15:30:16 -0500 | [diff] [blame] | 4891 | ret = btrfs_truncate(inode); |
Josef Bacik | 7f4f6e0 | 2013-08-29 16:43:28 -0400 | [diff] [blame] | 4892 | if (ret && inode->i_nlink) { |
| 4893 | int err; |
| 4894 | |
| 4895 | /* |
| 4896 | * failed to truncate, disk_i_size is only adjusted down |
| 4897 | * as we remove extents, so it should represent the true |
| 4898 | * size of the inode, so reset the in memory size and |
| 4899 | * delete our orphan entry. |
| 4900 | */ |
| 4901 | trans = btrfs_join_transaction(root); |
| 4902 | if (IS_ERR(trans)) { |
| 4903 | btrfs_orphan_del(NULL, inode); |
| 4904 | return ret; |
| 4905 | } |
| 4906 | i_size_write(inode, BTRFS_I(inode)->disk_i_size); |
| 4907 | err = btrfs_orphan_del(trans, inode); |
| 4908 | if (err) |
| 4909 | btrfs_abort_transaction(trans, root, err); |
| 4910 | btrfs_end_transaction(trans, root); |
| 4911 | } |
Yan, Zheng | 8082510 | 2009-11-12 09:35:36 +0000 | [diff] [blame] | 4912 | } |
| 4913 | |
Josef Bacik | a41ad39 | 2011-01-31 15:30:16 -0500 | [diff] [blame] | 4914 | return ret; |
Yan, Zheng | 8082510 | 2009-11-12 09:35:36 +0000 | [diff] [blame] | 4915 | } |
| 4916 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4917 | static int btrfs_setattr(struct dentry *dentry, struct iattr *attr) |
| 4918 | { |
| 4919 | struct inode *inode = dentry->d_inode; |
Li Zefan | b83cc96 | 2010-12-20 16:04:08 +0800 | [diff] [blame] | 4920 | struct btrfs_root *root = BTRFS_I(inode)->root; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4921 | int err; |
| 4922 | |
Li Zefan | b83cc96 | 2010-12-20 16:04:08 +0800 | [diff] [blame] | 4923 | if (btrfs_root_readonly(root)) |
| 4924 | return -EROFS; |
| 4925 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4926 | err = inode_change_ok(inode, attr); |
| 4927 | if (err) |
| 4928 | return err; |
| 4929 | |
Chris Mason | 5a3f23d | 2009-03-31 13:27:11 -0400 | [diff] [blame] | 4930 | if (S_ISREG(inode->i_mode) && (attr->ia_valid & ATTR_SIZE)) { |
Eric Sandeen | 3972f26 | 2013-01-12 02:57:22 +0000 | [diff] [blame] | 4931 | err = btrfs_setsize(inode, attr); |
Yan, Zheng | 8082510 | 2009-11-12 09:35:36 +0000 | [diff] [blame] | 4932 | if (err) |
| 4933 | return err; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4934 | } |
Yan Zheng | 9036c10 | 2008-10-30 14:19:41 -0400 | [diff] [blame] | 4935 | |
Christoph Hellwig | 1025774 | 2010-06-04 11:30:02 +0200 | [diff] [blame] | 4936 | if (attr->ia_valid) { |
| 4937 | setattr_copy(inode, attr); |
Josef Bacik | 0c4d2d9 | 2012-04-05 15:03:02 -0400 | [diff] [blame] | 4938 | inode_inc_iversion(inode); |
Josef Bacik | 22c44fe | 2011-11-30 10:45:38 -0500 | [diff] [blame] | 4939 | err = btrfs_dirty_inode(inode); |
Josef Bacik | 33268ea | 2008-07-24 12:16:36 -0400 | [diff] [blame] | 4940 | |
Josef Bacik | 22c44fe | 2011-11-30 10:45:38 -0500 | [diff] [blame] | 4941 | if (!err && attr->ia_valid & ATTR_MODE) |
Christoph Hellwig | 996a710 | 2013-12-20 05:16:43 -0800 | [diff] [blame] | 4942 | err = posix_acl_chmod(inode, inode->i_mode); |
Christoph Hellwig | 1025774 | 2010-06-04 11:30:02 +0200 | [diff] [blame] | 4943 | } |
| 4944 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4945 | return err; |
| 4946 | } |
Chris Mason | 61295eb | 2008-01-14 16:24:38 -0500 | [diff] [blame] | 4947 | |
Filipe David Borba Manana | 131e404 | 2013-11-19 22:29:35 +0000 | [diff] [blame] | 4948 | /* |
| 4949 | * While truncating the inode pages during eviction, we get the VFS calling |
| 4950 | * btrfs_invalidatepage() against each page of the inode. This is slow because |
| 4951 | * the calls to btrfs_invalidatepage() result in a huge amount of calls to |
| 4952 | * lock_extent_bits() and clear_extent_bit(), which keep merging and splitting |
| 4953 | * extent_state structures over and over, wasting lots of time. |
| 4954 | * |
| 4955 | * Therefore if the inode is being evicted, let btrfs_invalidatepage() skip all |
| 4956 | * those expensive operations on a per page basis and do only the ordered io |
| 4957 | * finishing, while we release here the extent_map and extent_state structures, |
| 4958 | * without the excessive merging and splitting. |
| 4959 | */ |
| 4960 | static void evict_inode_truncate_pages(struct inode *inode) |
| 4961 | { |
| 4962 | struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree; |
| 4963 | struct extent_map_tree *map_tree = &BTRFS_I(inode)->extent_tree; |
| 4964 | struct rb_node *node; |
| 4965 | |
| 4966 | ASSERT(inode->i_state & I_FREEING); |
Johannes Weiner | 91b0abe | 2014-04-03 14:47:49 -0700 | [diff] [blame] | 4967 | truncate_inode_pages_final(&inode->i_data); |
Filipe David Borba Manana | 131e404 | 2013-11-19 22:29:35 +0000 | [diff] [blame] | 4968 | |
| 4969 | write_lock(&map_tree->lock); |
| 4970 | while (!RB_EMPTY_ROOT(&map_tree->map)) { |
| 4971 | struct extent_map *em; |
| 4972 | |
| 4973 | node = rb_first(&map_tree->map); |
| 4974 | em = rb_entry(node, struct extent_map, rb_node); |
Wang Shilong | 180589e | 2013-12-14 15:27:31 +0800 | [diff] [blame] | 4975 | clear_bit(EXTENT_FLAG_PINNED, &em->flags); |
| 4976 | clear_bit(EXTENT_FLAG_LOGGING, &em->flags); |
Filipe David Borba Manana | 131e404 | 2013-11-19 22:29:35 +0000 | [diff] [blame] | 4977 | remove_extent_mapping(map_tree, em); |
| 4978 | free_extent_map(em); |
Filipe Manana | 7064dd5 | 2014-08-08 02:47:05 +0100 | [diff] [blame] | 4979 | if (need_resched()) { |
| 4980 | write_unlock(&map_tree->lock); |
| 4981 | cond_resched(); |
| 4982 | write_lock(&map_tree->lock); |
| 4983 | } |
Filipe David Borba Manana | 131e404 | 2013-11-19 22:29:35 +0000 | [diff] [blame] | 4984 | } |
| 4985 | write_unlock(&map_tree->lock); |
| 4986 | |
| 4987 | spin_lock(&io_tree->lock); |
| 4988 | while (!RB_EMPTY_ROOT(&io_tree->state)) { |
| 4989 | struct extent_state *state; |
| 4990 | struct extent_state *cached_state = NULL; |
| 4991 | |
| 4992 | node = rb_first(&io_tree->state); |
| 4993 | state = rb_entry(node, struct extent_state, rb_node); |
| 4994 | atomic_inc(&state->refs); |
| 4995 | spin_unlock(&io_tree->lock); |
| 4996 | |
| 4997 | lock_extent_bits(io_tree, state->start, state->end, |
| 4998 | 0, &cached_state); |
| 4999 | clear_extent_bit(io_tree, state->start, state->end, |
| 5000 | EXTENT_LOCKED | EXTENT_DIRTY | |
| 5001 | EXTENT_DELALLOC | EXTENT_DO_ACCOUNTING | |
| 5002 | EXTENT_DEFRAG, 1, 1, |
| 5003 | &cached_state, GFP_NOFS); |
| 5004 | free_extent_state(state); |
| 5005 | |
Filipe Manana | 7064dd5 | 2014-08-08 02:47:05 +0100 | [diff] [blame] | 5006 | cond_resched(); |
Filipe David Borba Manana | 131e404 | 2013-11-19 22:29:35 +0000 | [diff] [blame] | 5007 | spin_lock(&io_tree->lock); |
| 5008 | } |
| 5009 | spin_unlock(&io_tree->lock); |
| 5010 | } |
| 5011 | |
Al Viro | bd55597 | 2010-06-07 11:35:40 -0400 | [diff] [blame] | 5012 | void btrfs_evict_inode(struct inode *inode) |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5013 | { |
| 5014 | struct btrfs_trans_handle *trans; |
| 5015 | struct btrfs_root *root = BTRFS_I(inode)->root; |
Josef Bacik | 726c35f | 2011-09-26 15:46:06 -0400 | [diff] [blame] | 5016 | struct btrfs_block_rsv *rsv, *global_rsv; |
Josef Bacik | 3bce876 | 2015-02-24 12:35:51 -0800 | [diff] [blame] | 5017 | int steal_from_global = 0; |
Josef Bacik | 0712718 | 2011-08-19 10:29:59 -0400 | [diff] [blame] | 5018 | u64 min_size = btrfs_calc_trunc_metadata_size(root, 1); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5019 | int ret; |
| 5020 | |
liubo | 1abe9b8 | 2011-03-24 11:18:59 +0000 | [diff] [blame] | 5021 | trace_btrfs_inode_evict(inode); |
| 5022 | |
Filipe David Borba Manana | 131e404 | 2013-11-19 22:29:35 +0000 | [diff] [blame] | 5023 | evict_inode_truncate_pages(inode); |
| 5024 | |
Stefan Behrens | 69e9c6c | 2013-09-05 16:58:43 +0200 | [diff] [blame] | 5025 | if (inode->i_nlink && |
| 5026 | ((btrfs_root_refs(&root->root_item) != 0 && |
| 5027 | root->root_key.objectid != BTRFS_ROOT_TREE_OBJECTID) || |
| 5028 | btrfs_is_free_space_inode(inode))) |
Al Viro | bd55597 | 2010-06-07 11:35:40 -0400 | [diff] [blame] | 5029 | goto no_delete; |
| 5030 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5031 | if (is_bad_inode(inode)) { |
Josef Bacik | 7b12876 | 2008-07-24 12:17:14 -0400 | [diff] [blame] | 5032 | btrfs_orphan_del(NULL, inode); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5033 | goto no_delete; |
| 5034 | } |
Al Viro | bd55597 | 2010-06-07 11:35:40 -0400 | [diff] [blame] | 5035 | /* do we really want it for ->i_nlink > 0 and zero btrfs_root_refs? */ |
Chris Mason | 4a09675 | 2008-07-21 10:29:44 -0400 | [diff] [blame] | 5036 | btrfs_wait_ordered_range(inode, 0, (u64)-1); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 5037 | |
Miao Xie | f612496 | 2014-09-12 18:44:04 +0800 | [diff] [blame] | 5038 | btrfs_free_io_failure_record(inode, 0, (u64)-1); |
| 5039 | |
Yan, Zheng | c71bf09 | 2009-11-12 09:34:40 +0000 | [diff] [blame] | 5040 | if (root->fs_info->log_root_recovering) { |
Liu Bo | 6bf0231 | 2012-06-25 21:59:09 -0600 | [diff] [blame] | 5041 | BUG_ON(test_bit(BTRFS_INODE_HAS_ORPHAN_ITEM, |
Josef Bacik | 8a35d95 | 2012-05-23 14:26:42 -0400 | [diff] [blame] | 5042 | &BTRFS_I(inode)->runtime_flags)); |
Yan, Zheng | c71bf09 | 2009-11-12 09:34:40 +0000 | [diff] [blame] | 5043 | goto no_delete; |
| 5044 | } |
| 5045 | |
Yan, Zheng | 76dda93 | 2009-09-21 16:00:26 -0400 | [diff] [blame] | 5046 | if (inode->i_nlink > 0) { |
Stefan Behrens | 69e9c6c | 2013-09-05 16:58:43 +0200 | [diff] [blame] | 5047 | BUG_ON(btrfs_root_refs(&root->root_item) != 0 && |
| 5048 | root->root_key.objectid != BTRFS_ROOT_TREE_OBJECTID); |
Yan, Zheng | 76dda93 | 2009-09-21 16:00:26 -0400 | [diff] [blame] | 5049 | goto no_delete; |
| 5050 | } |
| 5051 | |
Miao Xie | 0e8c36a | 2012-12-19 06:59:51 +0000 | [diff] [blame] | 5052 | ret = btrfs_commit_inode_delayed_inode(inode); |
| 5053 | if (ret) { |
| 5054 | btrfs_orphan_del(NULL, inode); |
| 5055 | goto no_delete; |
| 5056 | } |
| 5057 | |
Miao Xie | 66d8f3d | 2012-09-06 04:02:28 -0600 | [diff] [blame] | 5058 | rsv = btrfs_alloc_block_rsv(root, BTRFS_BLOCK_RSV_TEMP); |
Josef Bacik | 4289a66 | 2011-08-05 13:22:24 -0400 | [diff] [blame] | 5059 | if (!rsv) { |
| 5060 | btrfs_orphan_del(NULL, inode); |
| 5061 | goto no_delete; |
| 5062 | } |
Josef Bacik | 4a33854 | 2011-08-29 11:01:31 -0400 | [diff] [blame] | 5063 | rsv->size = min_size; |
Josef Bacik | ca7e70f | 2012-08-27 17:48:15 -0400 | [diff] [blame] | 5064 | rsv->failfast = 1; |
Josef Bacik | 726c35f | 2011-09-26 15:46:06 -0400 | [diff] [blame] | 5065 | global_rsv = &root->fs_info->global_block_rsv; |
Josef Bacik | 4289a66 | 2011-08-05 13:22:24 -0400 | [diff] [blame] | 5066 | |
Chris Mason | dbe674a | 2008-07-17 12:54:05 -0400 | [diff] [blame] | 5067 | btrfs_i_size_write(inode, 0); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 5068 | |
Josef Bacik | 4289a66 | 2011-08-05 13:22:24 -0400 | [diff] [blame] | 5069 | /* |
Miao Xie | 8407aa4 | 2012-09-07 01:43:32 -0600 | [diff] [blame] | 5070 | * This is a bit simpler than btrfs_truncate since we've already |
| 5071 | * reserved our space for our orphan item in the unlink, so we just |
| 5072 | * need to reserve some slack space in case we add bytes and update |
| 5073 | * inode item when doing the truncate. |
Josef Bacik | 4289a66 | 2011-08-05 13:22:24 -0400 | [diff] [blame] | 5074 | */ |
Yan, Zheng | 8082510 | 2009-11-12 09:35:36 +0000 | [diff] [blame] | 5075 | while (1) { |
Miao Xie | 08e007d | 2012-10-16 11:33:38 +0000 | [diff] [blame] | 5076 | ret = btrfs_block_rsv_refill(root, rsv, min_size, |
| 5077 | BTRFS_RESERVE_FLUSH_LIMIT); |
Yan, Zheng | 8082510 | 2009-11-12 09:35:36 +0000 | [diff] [blame] | 5078 | |
Josef Bacik | 726c35f | 2011-09-26 15:46:06 -0400 | [diff] [blame] | 5079 | /* |
| 5080 | * Try and steal from the global reserve since we will |
| 5081 | * likely not use this space anyway, we want to try as |
| 5082 | * hard as possible to get this to work. |
| 5083 | */ |
| 5084 | if (ret) |
Josef Bacik | 3bce876 | 2015-02-24 12:35:51 -0800 | [diff] [blame] | 5085 | steal_from_global++; |
| 5086 | else |
| 5087 | steal_from_global = 0; |
| 5088 | ret = 0; |
Josef Bacik | 726c35f | 2011-09-26 15:46:06 -0400 | [diff] [blame] | 5089 | |
Josef Bacik | 3bce876 | 2015-02-24 12:35:51 -0800 | [diff] [blame] | 5090 | /* |
| 5091 | * steal_from_global == 0: we reserved stuff, hooray! |
| 5092 | * steal_from_global == 1: we didn't reserve stuff, boo! |
| 5093 | * steal_from_global == 2: we've committed, still not a lot of |
| 5094 | * room but maybe we'll have room in the global reserve this |
| 5095 | * time. |
| 5096 | * steal_from_global == 3: abandon all hope! |
| 5097 | */ |
| 5098 | if (steal_from_global > 2) { |
Simon Kirby | c2cf52e | 2013-03-19 22:41:23 +0000 | [diff] [blame] | 5099 | btrfs_warn(root->fs_info, |
| 5100 | "Could not get space for a delete, will truncate on mount %d", |
| 5101 | ret); |
Josef Bacik | 4289a66 | 2011-08-05 13:22:24 -0400 | [diff] [blame] | 5102 | btrfs_orphan_del(NULL, inode); |
| 5103 | btrfs_free_block_rsv(root, rsv); |
| 5104 | goto no_delete; |
Yan, Zheng | d68fc57 | 2010-05-16 10:49:58 -0400 | [diff] [blame] | 5105 | } |
| 5106 | |
Miao Xie | 0e8c36a | 2012-12-19 06:59:51 +0000 | [diff] [blame] | 5107 | trans = btrfs_join_transaction(root); |
Josef Bacik | 4289a66 | 2011-08-05 13:22:24 -0400 | [diff] [blame] | 5108 | if (IS_ERR(trans)) { |
| 5109 | btrfs_orphan_del(NULL, inode); |
| 5110 | btrfs_free_block_rsv(root, rsv); |
| 5111 | goto no_delete; |
| 5112 | } |
| 5113 | |
Josef Bacik | 3bce876 | 2015-02-24 12:35:51 -0800 | [diff] [blame] | 5114 | /* |
| 5115 | * We can't just steal from the global reserve, we need tomake |
| 5116 | * sure there is room to do it, if not we need to commit and try |
| 5117 | * again. |
| 5118 | */ |
| 5119 | if (steal_from_global) { |
| 5120 | if (!btrfs_check_space_for_delayed_refs(trans, root)) |
| 5121 | ret = btrfs_block_rsv_migrate(global_rsv, rsv, |
| 5122 | min_size); |
| 5123 | else |
| 5124 | ret = -ENOSPC; |
| 5125 | } |
| 5126 | |
| 5127 | /* |
| 5128 | * Couldn't steal from the global reserve, we have too much |
| 5129 | * pending stuff built up, commit the transaction and try it |
| 5130 | * again. |
| 5131 | */ |
| 5132 | if (ret) { |
| 5133 | ret = btrfs_commit_transaction(trans, root); |
| 5134 | if (ret) { |
| 5135 | btrfs_orphan_del(NULL, inode); |
| 5136 | btrfs_free_block_rsv(root, rsv); |
| 5137 | goto no_delete; |
| 5138 | } |
| 5139 | continue; |
| 5140 | } else { |
| 5141 | steal_from_global = 0; |
| 5142 | } |
| 5143 | |
Josef Bacik | 4289a66 | 2011-08-05 13:22:24 -0400 | [diff] [blame] | 5144 | trans->block_rsv = rsv; |
| 5145 | |
Yan, Zheng | d68fc57 | 2010-05-16 10:49:58 -0400 | [diff] [blame] | 5146 | ret = btrfs_truncate_inode_items(trans, root, inode, 0, 0); |
Chris Mason | 28ed134 | 2014-12-17 09:41:04 -0800 | [diff] [blame] | 5147 | if (ret != -ENOSPC && ret != -EAGAIN) |
Yan, Zheng | 8082510 | 2009-11-12 09:35:36 +0000 | [diff] [blame] | 5148 | break; |
| 5149 | |
Miao Xie | 8407aa4 | 2012-09-07 01:43:32 -0600 | [diff] [blame] | 5150 | trans->block_rsv = &root->fs_info->trans_block_rsv; |
Yan, Zheng | 8082510 | 2009-11-12 09:35:36 +0000 | [diff] [blame] | 5151 | btrfs_end_transaction(trans, root); |
| 5152 | trans = NULL; |
Liu Bo | b53d3f5 | 2012-11-14 14:34:34 +0000 | [diff] [blame] | 5153 | btrfs_btree_balance_dirty(root); |
Josef Bacik | 7b12876 | 2008-07-24 12:17:14 -0400 | [diff] [blame] | 5154 | } |
| 5155 | |
Josef Bacik | 4289a66 | 2011-08-05 13:22:24 -0400 | [diff] [blame] | 5156 | btrfs_free_block_rsv(root, rsv); |
| 5157 | |
Josef Bacik | 4ef31a4 | 2013-08-13 14:10:08 -0400 | [diff] [blame] | 5158 | /* |
| 5159 | * Errors here aren't a big deal, it just means we leave orphan items |
| 5160 | * in the tree. They will be cleaned up on the next mount. |
| 5161 | */ |
Yan, Zheng | 8082510 | 2009-11-12 09:35:36 +0000 | [diff] [blame] | 5162 | if (ret == 0) { |
Josef Bacik | 4289a66 | 2011-08-05 13:22:24 -0400 | [diff] [blame] | 5163 | trans->block_rsv = root->orphan_block_rsv; |
Josef Bacik | 4ef31a4 | 2013-08-13 14:10:08 -0400 | [diff] [blame] | 5164 | btrfs_orphan_del(trans, inode); |
| 5165 | } else { |
| 5166 | btrfs_orphan_del(NULL, inode); |
Yan, Zheng | 8082510 | 2009-11-12 09:35:36 +0000 | [diff] [blame] | 5167 | } |
Chris Mason | 85e21ba | 2008-01-29 15:11:36 -0500 | [diff] [blame] | 5168 | |
Josef Bacik | 4289a66 | 2011-08-05 13:22:24 -0400 | [diff] [blame] | 5169 | trans->block_rsv = &root->fs_info->trans_block_rsv; |
Li Zefan | 581bb05 | 2011-04-20 10:06:11 +0800 | [diff] [blame] | 5170 | if (!(root == root->fs_info->tree_root || |
| 5171 | root->root_key.objectid == BTRFS_TREE_RELOC_OBJECTID)) |
Li Zefan | 33345d01 | 2011-04-20 10:31:50 +0800 | [diff] [blame] | 5172 | btrfs_return_ino(root, btrfs_ino(inode)); |
Li Zefan | 581bb05 | 2011-04-20 10:06:11 +0800 | [diff] [blame] | 5173 | |
Chris Mason | 54aa1f4 | 2007-06-22 14:16:25 -0400 | [diff] [blame] | 5174 | btrfs_end_transaction(trans, root); |
Liu Bo | b53d3f5 | 2012-11-14 14:34:34 +0000 | [diff] [blame] | 5175 | btrfs_btree_balance_dirty(root); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5176 | no_delete: |
Miao Xie | 89042e5 | 2013-05-15 07:48:15 +0000 | [diff] [blame] | 5177 | btrfs_remove_delayed_node(inode); |
Jan Kara | dbd5768 | 2012-05-03 14:48:02 +0200 | [diff] [blame] | 5178 | clear_inode(inode); |
Yan, Zheng | 8082510 | 2009-11-12 09:35:36 +0000 | [diff] [blame] | 5179 | return; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5180 | } |
| 5181 | |
| 5182 | /* |
| 5183 | * this returns the key found in the dir entry in the location pointer. |
| 5184 | * If no dir entries were found, location->objectid is 0. |
| 5185 | */ |
| 5186 | static int btrfs_inode_by_name(struct inode *dir, struct dentry *dentry, |
| 5187 | struct btrfs_key *location) |
| 5188 | { |
| 5189 | const char *name = dentry->d_name.name; |
| 5190 | int namelen = dentry->d_name.len; |
| 5191 | struct btrfs_dir_item *di; |
| 5192 | struct btrfs_path *path; |
| 5193 | struct btrfs_root *root = BTRFS_I(dir)->root; |
Yan | 0d9f7f3 | 2007-10-25 15:48:28 -0400 | [diff] [blame] | 5194 | int ret = 0; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5195 | |
| 5196 | path = btrfs_alloc_path(); |
Mark Fasheh | d8926bb | 2011-07-13 10:38:47 -0700 | [diff] [blame] | 5197 | if (!path) |
| 5198 | return -ENOMEM; |
Chris Mason | 3954401 | 2007-12-12 14:38:19 -0500 | [diff] [blame] | 5199 | |
Li Zefan | 33345d01 | 2011-04-20 10:31:50 +0800 | [diff] [blame] | 5200 | di = btrfs_lookup_dir_item(NULL, root, path, btrfs_ino(dir), name, |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5201 | namelen, 0); |
Yan | 0d9f7f3 | 2007-10-25 15:48:28 -0400 | [diff] [blame] | 5202 | if (IS_ERR(di)) |
| 5203 | ret = PTR_ERR(di); |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 5204 | |
David Sterba | c704005 | 2011-04-19 18:00:01 +0200 | [diff] [blame] | 5205 | if (IS_ERR_OR_NULL(di)) |
Chris Mason | 3954401 | 2007-12-12 14:38:19 -0500 | [diff] [blame] | 5206 | goto out_err; |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 5207 | |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 5208 | btrfs_dir_item_key_to_cpu(path->nodes[0], di, location); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5209 | out: |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5210 | btrfs_free_path(path); |
| 5211 | return ret; |
Chris Mason | 3954401 | 2007-12-12 14:38:19 -0500 | [diff] [blame] | 5212 | out_err: |
| 5213 | location->objectid = 0; |
| 5214 | goto out; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5215 | } |
| 5216 | |
| 5217 | /* |
| 5218 | * when we hit a tree root in a directory, the btrfs part of the inode |
| 5219 | * needs to be changed to reflect the root directory of the tree root. This |
| 5220 | * is kind of like crossing a mount point. |
| 5221 | */ |
| 5222 | static int fixup_tree_root_location(struct btrfs_root *root, |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 5223 | struct inode *dir, |
| 5224 | struct dentry *dentry, |
| 5225 | struct btrfs_key *location, |
| 5226 | struct btrfs_root **sub_root) |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5227 | { |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 5228 | struct btrfs_path *path; |
| 5229 | struct btrfs_root *new_root; |
| 5230 | struct btrfs_root_ref *ref; |
| 5231 | struct extent_buffer *leaf; |
David Sterba | 1d4c08e | 2015-01-02 19:36:14 +0100 | [diff] [blame] | 5232 | struct btrfs_key key; |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 5233 | int ret; |
| 5234 | int err = 0; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5235 | |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 5236 | path = btrfs_alloc_path(); |
| 5237 | if (!path) { |
| 5238 | err = -ENOMEM; |
| 5239 | goto out; |
| 5240 | } |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5241 | |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 5242 | err = -ENOENT; |
David Sterba | 1d4c08e | 2015-01-02 19:36:14 +0100 | [diff] [blame] | 5243 | key.objectid = BTRFS_I(dir)->root->root_key.objectid; |
| 5244 | key.type = BTRFS_ROOT_REF_KEY; |
| 5245 | key.offset = location->objectid; |
| 5246 | |
| 5247 | ret = btrfs_search_slot(NULL, root->fs_info->tree_root, &key, path, |
| 5248 | 0, 0); |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 5249 | if (ret) { |
| 5250 | if (ret < 0) |
| 5251 | err = ret; |
| 5252 | goto out; |
| 5253 | } |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5254 | |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 5255 | leaf = path->nodes[0]; |
| 5256 | ref = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_root_ref); |
Li Zefan | 33345d01 | 2011-04-20 10:31:50 +0800 | [diff] [blame] | 5257 | if (btrfs_root_ref_dirid(leaf, ref) != btrfs_ino(dir) || |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 5258 | btrfs_root_ref_name_len(leaf, ref) != dentry->d_name.len) |
| 5259 | goto out; |
| 5260 | |
| 5261 | ret = memcmp_extent_buffer(leaf, dentry->d_name.name, |
| 5262 | (unsigned long)(ref + 1), |
| 5263 | dentry->d_name.len); |
| 5264 | if (ret) |
| 5265 | goto out; |
| 5266 | |
David Sterba | b3b4aa7 | 2011-04-21 01:20:15 +0200 | [diff] [blame] | 5267 | btrfs_release_path(path); |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 5268 | |
| 5269 | new_root = btrfs_read_fs_root_no_name(root->fs_info, location); |
| 5270 | if (IS_ERR(new_root)) { |
| 5271 | err = PTR_ERR(new_root); |
| 5272 | goto out; |
| 5273 | } |
| 5274 | |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 5275 | *sub_root = new_root; |
| 5276 | location->objectid = btrfs_root_dirid(&new_root->root_item); |
| 5277 | location->type = BTRFS_INODE_ITEM_KEY; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5278 | location->offset = 0; |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 5279 | err = 0; |
| 5280 | out: |
| 5281 | btrfs_free_path(path); |
| 5282 | return err; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5283 | } |
| 5284 | |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 5285 | static void inode_tree_add(struct inode *inode) |
| 5286 | { |
| 5287 | struct btrfs_root *root = BTRFS_I(inode)->root; |
| 5288 | struct btrfs_inode *entry; |
From: Nick Piggin | 03e860b | 2009-08-21 10:09:44 +0200 | [diff] [blame] | 5289 | struct rb_node **p; |
| 5290 | struct rb_node *parent; |
Filipe David Borba Manana | cef2193 | 2013-09-02 12:19:13 +0100 | [diff] [blame] | 5291 | struct rb_node *new = &BTRFS_I(inode)->rb_node; |
Li Zefan | 33345d01 | 2011-04-20 10:31:50 +0800 | [diff] [blame] | 5292 | u64 ino = btrfs_ino(inode); |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 5293 | |
Al Viro | 1d3382cb | 2010-10-23 15:19:20 -0400 | [diff] [blame] | 5294 | if (inode_unhashed(inode)) |
Yan, Zheng | 76dda93 | 2009-09-21 16:00:26 -0400 | [diff] [blame] | 5295 | return; |
Miao Xie | e1409ce | 2013-05-15 07:48:16 +0000 | [diff] [blame] | 5296 | parent = NULL; |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 5297 | spin_lock(&root->inode_lock); |
Miao Xie | e1409ce | 2013-05-15 07:48:16 +0000 | [diff] [blame] | 5298 | p = &root->inode_tree.rb_node; |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 5299 | while (*p) { |
| 5300 | parent = *p; |
| 5301 | entry = rb_entry(parent, struct btrfs_inode, rb_node); |
| 5302 | |
Li Zefan | 33345d01 | 2011-04-20 10:31:50 +0800 | [diff] [blame] | 5303 | if (ino < btrfs_ino(&entry->vfs_inode)) |
From: Nick Piggin | 03e860b | 2009-08-21 10:09:44 +0200 | [diff] [blame] | 5304 | p = &parent->rb_left; |
Li Zefan | 33345d01 | 2011-04-20 10:31:50 +0800 | [diff] [blame] | 5305 | else if (ino > btrfs_ino(&entry->vfs_inode)) |
From: Nick Piggin | 03e860b | 2009-08-21 10:09:44 +0200 | [diff] [blame] | 5306 | p = &parent->rb_right; |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 5307 | else { |
| 5308 | WARN_ON(!(entry->vfs_inode.i_state & |
Al Viro | a4ffdde | 2010-06-02 17:38:30 -0400 | [diff] [blame] | 5309 | (I_WILL_FREE | I_FREEING))); |
Filipe David Borba Manana | cef2193 | 2013-09-02 12:19:13 +0100 | [diff] [blame] | 5310 | rb_replace_node(parent, new, &root->inode_tree); |
From: Nick Piggin | 03e860b | 2009-08-21 10:09:44 +0200 | [diff] [blame] | 5311 | RB_CLEAR_NODE(parent); |
| 5312 | spin_unlock(&root->inode_lock); |
Filipe David Borba Manana | cef2193 | 2013-09-02 12:19:13 +0100 | [diff] [blame] | 5313 | return; |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 5314 | } |
| 5315 | } |
Filipe David Borba Manana | cef2193 | 2013-09-02 12:19:13 +0100 | [diff] [blame] | 5316 | rb_link_node(new, parent, p); |
| 5317 | rb_insert_color(new, &root->inode_tree); |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 5318 | spin_unlock(&root->inode_lock); |
| 5319 | } |
| 5320 | |
| 5321 | static void inode_tree_del(struct inode *inode) |
| 5322 | { |
| 5323 | struct btrfs_root *root = BTRFS_I(inode)->root; |
Yan, Zheng | 76dda93 | 2009-09-21 16:00:26 -0400 | [diff] [blame] | 5324 | int empty = 0; |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 5325 | |
From: Nick Piggin | 03e860b | 2009-08-21 10:09:44 +0200 | [diff] [blame] | 5326 | spin_lock(&root->inode_lock); |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 5327 | if (!RB_EMPTY_NODE(&BTRFS_I(inode)->rb_node)) { |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 5328 | rb_erase(&BTRFS_I(inode)->rb_node, &root->inode_tree); |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 5329 | RB_CLEAR_NODE(&BTRFS_I(inode)->rb_node); |
Yan, Zheng | 76dda93 | 2009-09-21 16:00:26 -0400 | [diff] [blame] | 5330 | empty = RB_EMPTY_ROOT(&root->inode_tree); |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 5331 | } |
From: Nick Piggin | 03e860b | 2009-08-21 10:09:44 +0200 | [diff] [blame] | 5332 | spin_unlock(&root->inode_lock); |
Yan, Zheng | 76dda93 | 2009-09-21 16:00:26 -0400 | [diff] [blame] | 5333 | |
Stefan Behrens | 69e9c6c | 2013-09-05 16:58:43 +0200 | [diff] [blame] | 5334 | if (empty && btrfs_root_refs(&root->root_item) == 0) { |
Yan, Zheng | 76dda93 | 2009-09-21 16:00:26 -0400 | [diff] [blame] | 5335 | synchronize_srcu(&root->fs_info->subvol_srcu); |
| 5336 | spin_lock(&root->inode_lock); |
| 5337 | empty = RB_EMPTY_ROOT(&root->inode_tree); |
| 5338 | spin_unlock(&root->inode_lock); |
| 5339 | if (empty) |
| 5340 | btrfs_add_dead_root(root); |
| 5341 | } |
| 5342 | } |
| 5343 | |
Jeff Mahoney | 143bede | 2012-03-01 14:56:26 +0100 | [diff] [blame] | 5344 | void btrfs_invalidate_inodes(struct btrfs_root *root) |
Yan, Zheng | 76dda93 | 2009-09-21 16:00:26 -0400 | [diff] [blame] | 5345 | { |
| 5346 | struct rb_node *node; |
| 5347 | struct rb_node *prev; |
| 5348 | struct btrfs_inode *entry; |
| 5349 | struct inode *inode; |
| 5350 | u64 objectid = 0; |
| 5351 | |
Liu Bo | 7813b3d | 2014-02-10 17:37:25 +0800 | [diff] [blame] | 5352 | if (!test_bit(BTRFS_FS_STATE_ERROR, &root->fs_info->fs_state)) |
| 5353 | WARN_ON(btrfs_root_refs(&root->root_item) != 0); |
Yan, Zheng | 76dda93 | 2009-09-21 16:00:26 -0400 | [diff] [blame] | 5354 | |
| 5355 | spin_lock(&root->inode_lock); |
| 5356 | again: |
| 5357 | node = root->inode_tree.rb_node; |
| 5358 | prev = NULL; |
| 5359 | while (node) { |
| 5360 | prev = node; |
| 5361 | entry = rb_entry(node, struct btrfs_inode, rb_node); |
| 5362 | |
Li Zefan | 33345d01 | 2011-04-20 10:31:50 +0800 | [diff] [blame] | 5363 | if (objectid < btrfs_ino(&entry->vfs_inode)) |
Yan, Zheng | 76dda93 | 2009-09-21 16:00:26 -0400 | [diff] [blame] | 5364 | node = node->rb_left; |
Li Zefan | 33345d01 | 2011-04-20 10:31:50 +0800 | [diff] [blame] | 5365 | else if (objectid > btrfs_ino(&entry->vfs_inode)) |
Yan, Zheng | 76dda93 | 2009-09-21 16:00:26 -0400 | [diff] [blame] | 5366 | node = node->rb_right; |
| 5367 | else |
| 5368 | break; |
| 5369 | } |
| 5370 | if (!node) { |
| 5371 | while (prev) { |
| 5372 | entry = rb_entry(prev, struct btrfs_inode, rb_node); |
Li Zefan | 33345d01 | 2011-04-20 10:31:50 +0800 | [diff] [blame] | 5373 | if (objectid <= btrfs_ino(&entry->vfs_inode)) { |
Yan, Zheng | 76dda93 | 2009-09-21 16:00:26 -0400 | [diff] [blame] | 5374 | node = prev; |
| 5375 | break; |
| 5376 | } |
| 5377 | prev = rb_next(prev); |
| 5378 | } |
| 5379 | } |
| 5380 | while (node) { |
| 5381 | entry = rb_entry(node, struct btrfs_inode, rb_node); |
Li Zefan | 33345d01 | 2011-04-20 10:31:50 +0800 | [diff] [blame] | 5382 | objectid = btrfs_ino(&entry->vfs_inode) + 1; |
Yan, Zheng | 76dda93 | 2009-09-21 16:00:26 -0400 | [diff] [blame] | 5383 | inode = igrab(&entry->vfs_inode); |
| 5384 | if (inode) { |
| 5385 | spin_unlock(&root->inode_lock); |
| 5386 | if (atomic_read(&inode->i_count) > 1) |
| 5387 | d_prune_aliases(inode); |
| 5388 | /* |
Al Viro | 45321ac | 2010-06-07 13:43:19 -0400 | [diff] [blame] | 5389 | * btrfs_drop_inode will have it removed from |
Yan, Zheng | 76dda93 | 2009-09-21 16:00:26 -0400 | [diff] [blame] | 5390 | * the inode cache when its usage count |
| 5391 | * hits zero. |
| 5392 | */ |
| 5393 | iput(inode); |
| 5394 | cond_resched(); |
| 5395 | spin_lock(&root->inode_lock); |
| 5396 | goto again; |
| 5397 | } |
| 5398 | |
| 5399 | if (cond_resched_lock(&root->inode_lock)) |
| 5400 | goto again; |
| 5401 | |
| 5402 | node = rb_next(node); |
| 5403 | } |
| 5404 | spin_unlock(&root->inode_lock); |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 5405 | } |
| 5406 | |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 5407 | static int btrfs_init_locked_inode(struct inode *inode, void *p) |
| 5408 | { |
| 5409 | struct btrfs_iget_args *args = p; |
Chris Mason | 90d3e59 | 2014-01-09 17:28:00 -0800 | [diff] [blame] | 5410 | inode->i_ino = args->location->objectid; |
| 5411 | memcpy(&BTRFS_I(inode)->location, args->location, |
| 5412 | sizeof(*args->location)); |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 5413 | BTRFS_I(inode)->root = args->root; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5414 | return 0; |
| 5415 | } |
| 5416 | |
| 5417 | static int btrfs_find_actor(struct inode *inode, void *opaque) |
| 5418 | { |
| 5419 | struct btrfs_iget_args *args = opaque; |
Chris Mason | 90d3e59 | 2014-01-09 17:28:00 -0800 | [diff] [blame] | 5420 | return args->location->objectid == BTRFS_I(inode)->location.objectid && |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 5421 | args->root == BTRFS_I(inode)->root; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5422 | } |
| 5423 | |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 5424 | static struct inode *btrfs_iget_locked(struct super_block *s, |
Chris Mason | 90d3e59 | 2014-01-09 17:28:00 -0800 | [diff] [blame] | 5425 | struct btrfs_key *location, |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 5426 | struct btrfs_root *root) |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5427 | { |
| 5428 | struct inode *inode; |
| 5429 | struct btrfs_iget_args args; |
Chris Mason | 90d3e59 | 2014-01-09 17:28:00 -0800 | [diff] [blame] | 5430 | unsigned long hashval = btrfs_inode_hash(location->objectid, root); |
Filipe David Borba Manana | 778ba82 | 2013-10-06 22:22:33 +0100 | [diff] [blame] | 5431 | |
Chris Mason | 90d3e59 | 2014-01-09 17:28:00 -0800 | [diff] [blame] | 5432 | args.location = location; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5433 | args.root = root; |
| 5434 | |
Filipe David Borba Manana | 778ba82 | 2013-10-06 22:22:33 +0100 | [diff] [blame] | 5435 | inode = iget5_locked(s, hashval, btrfs_find_actor, |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5436 | btrfs_init_locked_inode, |
| 5437 | (void *)&args); |
| 5438 | return inode; |
| 5439 | } |
| 5440 | |
Balaji Rao | 1a54ef8 | 2008-07-21 02:01:04 +0530 | [diff] [blame] | 5441 | /* Get an inode object given its location and corresponding root. |
| 5442 | * Returns in *is_new if the inode was read from disk |
| 5443 | */ |
| 5444 | struct inode *btrfs_iget(struct super_block *s, struct btrfs_key *location, |
Josef Bacik | 73f7341 | 2009-12-04 17:38:27 +0000 | [diff] [blame] | 5445 | struct btrfs_root *root, int *new) |
Balaji Rao | 1a54ef8 | 2008-07-21 02:01:04 +0530 | [diff] [blame] | 5446 | { |
| 5447 | struct inode *inode; |
| 5448 | |
Chris Mason | 90d3e59 | 2014-01-09 17:28:00 -0800 | [diff] [blame] | 5449 | inode = btrfs_iget_locked(s, location, root); |
Balaji Rao | 1a54ef8 | 2008-07-21 02:01:04 +0530 | [diff] [blame] | 5450 | if (!inode) |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 5451 | return ERR_PTR(-ENOMEM); |
Balaji Rao | 1a54ef8 | 2008-07-21 02:01:04 +0530 | [diff] [blame] | 5452 | |
| 5453 | if (inode->i_state & I_NEW) { |
Balaji Rao | 1a54ef8 | 2008-07-21 02:01:04 +0530 | [diff] [blame] | 5454 | btrfs_read_locked_inode(inode); |
Mark Fasheh | 1748f84 | 2011-07-12 11:25:31 -0700 | [diff] [blame] | 5455 | if (!is_bad_inode(inode)) { |
| 5456 | inode_tree_add(inode); |
| 5457 | unlock_new_inode(inode); |
| 5458 | if (new) |
| 5459 | *new = 1; |
| 5460 | } else { |
Sergei Trofimovich | e0b6d65 | 2011-09-11 10:52:24 -0400 | [diff] [blame] | 5461 | unlock_new_inode(inode); |
| 5462 | iput(inode); |
| 5463 | inode = ERR_PTR(-ESTALE); |
Mark Fasheh | 1748f84 | 2011-07-12 11:25:31 -0700 | [diff] [blame] | 5464 | } |
| 5465 | } |
| 5466 | |
Balaji Rao | 1a54ef8 | 2008-07-21 02:01:04 +0530 | [diff] [blame] | 5467 | return inode; |
| 5468 | } |
| 5469 | |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 5470 | static struct inode *new_simple_dir(struct super_block *s, |
| 5471 | struct btrfs_key *key, |
| 5472 | struct btrfs_root *root) |
| 5473 | { |
| 5474 | struct inode *inode = new_inode(s); |
| 5475 | |
| 5476 | if (!inode) |
| 5477 | return ERR_PTR(-ENOMEM); |
| 5478 | |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 5479 | BTRFS_I(inode)->root = root; |
| 5480 | memcpy(&BTRFS_I(inode)->location, key, sizeof(*key)); |
Josef Bacik | 72ac3c0 | 2012-05-23 14:13:11 -0400 | [diff] [blame] | 5481 | set_bit(BTRFS_INODE_DUMMY, &BTRFS_I(inode)->runtime_flags); |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 5482 | |
| 5483 | inode->i_ino = BTRFS_EMPTY_SUBVOL_DIR_OBJECTID; |
Li Zefan | 848cce0 | 2012-02-21 17:04:28 +0800 | [diff] [blame] | 5484 | inode->i_op = &btrfs_dir_ro_inode_operations; |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 5485 | inode->i_fop = &simple_dir_operations; |
| 5486 | inode->i_mode = S_IFDIR | S_IRUGO | S_IWUSR | S_IXUGO; |
chandan r | 9cc97d6 | 2012-07-04 12:48:07 +0530 | [diff] [blame] | 5487 | inode->i_mtime = CURRENT_TIME; |
| 5488 | inode->i_atime = inode->i_mtime; |
| 5489 | inode->i_ctime = inode->i_mtime; |
| 5490 | BTRFS_I(inode)->i_otime = inode->i_mtime; |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 5491 | |
| 5492 | return inode; |
| 5493 | } |
| 5494 | |
Chris Mason | 3de4586 | 2008-11-17 21:02:50 -0500 | [diff] [blame] | 5495 | struct inode *btrfs_lookup_dentry(struct inode *dir, struct dentry *dentry) |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5496 | { |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 5497 | struct inode *inode; |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 5498 | struct btrfs_root *root = BTRFS_I(dir)->root; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5499 | struct btrfs_root *sub_root = root; |
| 5500 | struct btrfs_key location; |
Yan, Zheng | 76dda93 | 2009-09-21 16:00:26 -0400 | [diff] [blame] | 5501 | int index; |
Josef Bacik | b4aff1f | 2011-06-28 16:18:59 -0400 | [diff] [blame] | 5502 | int ret = 0; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5503 | |
| 5504 | if (dentry->d_name.len > BTRFS_NAME_LEN) |
| 5505 | return ERR_PTR(-ENAMETOOLONG); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 5506 | |
Jeff Layton | 39e3c95 | 2012-11-28 11:30:53 -0500 | [diff] [blame] | 5507 | ret = btrfs_inode_by_name(dir, dentry, &location); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5508 | if (ret < 0) |
| 5509 | return ERR_PTR(ret); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 5510 | |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 5511 | if (location.objectid == 0) |
Tsutomu Itoh | 5662344b3 | 2013-12-13 09:51:42 +0900 | [diff] [blame] | 5512 | return ERR_PTR(-ENOENT); |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 5513 | |
| 5514 | if (location.type == BTRFS_INODE_ITEM_KEY) { |
Josef Bacik | 73f7341 | 2009-12-04 17:38:27 +0000 | [diff] [blame] | 5515 | inode = btrfs_iget(dir->i_sb, &location, root, NULL); |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 5516 | return inode; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5517 | } |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 5518 | |
| 5519 | BUG_ON(location.type != BTRFS_ROOT_ITEM_KEY); |
| 5520 | |
Yan, Zheng | 76dda93 | 2009-09-21 16:00:26 -0400 | [diff] [blame] | 5521 | index = srcu_read_lock(&root->fs_info->subvol_srcu); |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 5522 | ret = fixup_tree_root_location(root, dir, dentry, |
| 5523 | &location, &sub_root); |
| 5524 | if (ret < 0) { |
| 5525 | if (ret != -ENOENT) |
| 5526 | inode = ERR_PTR(ret); |
| 5527 | else |
| 5528 | inode = new_simple_dir(dir->i_sb, &location, sub_root); |
| 5529 | } else { |
Josef Bacik | 73f7341 | 2009-12-04 17:38:27 +0000 | [diff] [blame] | 5530 | inode = btrfs_iget(dir->i_sb, &location, sub_root, NULL); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5531 | } |
Yan, Zheng | 76dda93 | 2009-09-21 16:00:26 -0400 | [diff] [blame] | 5532 | srcu_read_unlock(&root->fs_info->subvol_srcu, index); |
| 5533 | |
Julia Lawall | 34d19ba | 2011-01-24 19:55:19 +0000 | [diff] [blame] | 5534 | if (!IS_ERR(inode) && root != sub_root) { |
Yan, Zheng | c71bf09 | 2009-11-12 09:34:40 +0000 | [diff] [blame] | 5535 | down_read(&root->fs_info->cleanup_work_sem); |
| 5536 | if (!(inode->i_sb->s_flags & MS_RDONLY)) |
Josef Bacik | 66b4ffd | 2011-01-31 16:22:42 -0500 | [diff] [blame] | 5537 | ret = btrfs_orphan_cleanup(sub_root); |
Yan, Zheng | c71bf09 | 2009-11-12 09:34:40 +0000 | [diff] [blame] | 5538 | up_read(&root->fs_info->cleanup_work_sem); |
Josef Bacik | 01cd336 | 2013-06-03 21:39:49 -0400 | [diff] [blame] | 5539 | if (ret) { |
| 5540 | iput(inode); |
Josef Bacik | 66b4ffd | 2011-01-31 16:22:42 -0500 | [diff] [blame] | 5541 | inode = ERR_PTR(ret); |
Josef Bacik | 01cd336 | 2013-06-03 21:39:49 -0400 | [diff] [blame] | 5542 | } |
Yan, Zheng | c71bf09 | 2009-11-12 09:34:40 +0000 | [diff] [blame] | 5543 | } |
| 5544 | |
Chris Mason | 3de4586 | 2008-11-17 21:02:50 -0500 | [diff] [blame] | 5545 | return inode; |
| 5546 | } |
| 5547 | |
Nick Piggin | fe15ce4 | 2011-01-07 17:49:23 +1100 | [diff] [blame] | 5548 | static int btrfs_dentry_delete(const struct dentry *dentry) |
Yan, Zheng | 76dda93 | 2009-09-21 16:00:26 -0400 | [diff] [blame] | 5549 | { |
| 5550 | struct btrfs_root *root; |
Li Zefan | 848cce0 | 2012-02-21 17:04:28 +0800 | [diff] [blame] | 5551 | struct inode *inode = dentry->d_inode; |
Yan, Zheng | 76dda93 | 2009-09-21 16:00:26 -0400 | [diff] [blame] | 5552 | |
Li Zefan | 848cce0 | 2012-02-21 17:04:28 +0800 | [diff] [blame] | 5553 | if (!inode && !IS_ROOT(dentry)) |
| 5554 | inode = dentry->d_parent->d_inode; |
Yan, Zheng | 76dda93 | 2009-09-21 16:00:26 -0400 | [diff] [blame] | 5555 | |
Li Zefan | 848cce0 | 2012-02-21 17:04:28 +0800 | [diff] [blame] | 5556 | if (inode) { |
| 5557 | root = BTRFS_I(inode)->root; |
Yan, Zheng | efefb14 | 2009-10-09 09:25:16 -0400 | [diff] [blame] | 5558 | if (btrfs_root_refs(&root->root_item) == 0) |
| 5559 | return 1; |
Li Zefan | 848cce0 | 2012-02-21 17:04:28 +0800 | [diff] [blame] | 5560 | |
| 5561 | if (btrfs_ino(inode) == BTRFS_EMPTY_SUBVOL_DIR_OBJECTID) |
| 5562 | return 1; |
Yan, Zheng | efefb14 | 2009-10-09 09:25:16 -0400 | [diff] [blame] | 5563 | } |
Yan, Zheng | 76dda93 | 2009-09-21 16:00:26 -0400 | [diff] [blame] | 5564 | return 0; |
| 5565 | } |
| 5566 | |
Josef Bacik | b4aff1f | 2011-06-28 16:18:59 -0400 | [diff] [blame] | 5567 | static void btrfs_dentry_release(struct dentry *dentry) |
| 5568 | { |
Daeseok Youn | 944a451 | 2014-04-14 15:37:02 +0900 | [diff] [blame] | 5569 | kfree(dentry->d_fsdata); |
Josef Bacik | b4aff1f | 2011-06-28 16:18:59 -0400 | [diff] [blame] | 5570 | } |
| 5571 | |
Chris Mason | 3de4586 | 2008-11-17 21:02:50 -0500 | [diff] [blame] | 5572 | static struct dentry *btrfs_lookup(struct inode *dir, struct dentry *dentry, |
Al Viro | 00cd8dd | 2012-06-10 17:13:09 -0400 | [diff] [blame] | 5573 | unsigned int flags) |
Chris Mason | 3de4586 | 2008-11-17 21:02:50 -0500 | [diff] [blame] | 5574 | { |
Tsutomu Itoh | 5662344b3 | 2013-12-13 09:51:42 +0900 | [diff] [blame] | 5575 | struct inode *inode; |
Josef Bacik | a66e7cc | 2011-09-18 10:34:03 -0400 | [diff] [blame] | 5576 | |
Tsutomu Itoh | 5662344b3 | 2013-12-13 09:51:42 +0900 | [diff] [blame] | 5577 | inode = btrfs_lookup_dentry(dir, dentry); |
| 5578 | if (IS_ERR(inode)) { |
| 5579 | if (PTR_ERR(inode) == -ENOENT) |
| 5580 | inode = NULL; |
| 5581 | else |
| 5582 | return ERR_CAST(inode); |
| 5583 | } |
| 5584 | |
Al Viro | 41d28bc | 2014-10-12 22:24:21 -0400 | [diff] [blame] | 5585 | return d_splice_alias(inode, dentry); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5586 | } |
| 5587 | |
Miao Xie | 16cdcec | 2011-04-22 18:12:22 +0800 | [diff] [blame] | 5588 | unsigned char btrfs_filetype_table[] = { |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5589 | DT_UNKNOWN, DT_REG, DT_DIR, DT_CHR, DT_BLK, DT_FIFO, DT_SOCK, DT_LNK |
| 5590 | }; |
| 5591 | |
Al Viro | 9cdda8d | 2013-05-22 16:48:09 -0400 | [diff] [blame] | 5592 | static int btrfs_real_readdir(struct file *file, struct dir_context *ctx) |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5593 | { |
Al Viro | 9cdda8d | 2013-05-22 16:48:09 -0400 | [diff] [blame] | 5594 | struct inode *inode = file_inode(file); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5595 | struct btrfs_root *root = BTRFS_I(inode)->root; |
| 5596 | struct btrfs_item *item; |
| 5597 | struct btrfs_dir_item *di; |
| 5598 | struct btrfs_key key; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 5599 | struct btrfs_key found_key; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5600 | struct btrfs_path *path; |
Miao Xie | 16cdcec | 2011-04-22 18:12:22 +0800 | [diff] [blame] | 5601 | struct list_head ins_list; |
| 5602 | struct list_head del_list; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5603 | int ret; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 5604 | struct extent_buffer *leaf; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5605 | int slot; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5606 | unsigned char d_type; |
| 5607 | int over = 0; |
| 5608 | u32 di_cur; |
| 5609 | u32 di_total; |
| 5610 | u32 di_len; |
| 5611 | int key_type = BTRFS_DIR_INDEX_KEY; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 5612 | char tmp_name[32]; |
| 5613 | char *name_ptr; |
| 5614 | int name_len; |
Al Viro | 9cdda8d | 2013-05-22 16:48:09 -0400 | [diff] [blame] | 5615 | int is_curr = 0; /* ctx->pos points to the current index? */ |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5616 | |
| 5617 | /* FIXME, use a real flag for deciding about the key type */ |
| 5618 | if (root->fs_info->tree_root == root) |
| 5619 | key_type = BTRFS_DIR_ITEM_KEY; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 5620 | |
Al Viro | 9cdda8d | 2013-05-22 16:48:09 -0400 | [diff] [blame] | 5621 | if (!dir_emit_dots(file, ctx)) |
| 5622 | return 0; |
| 5623 | |
David Woodhouse | 49593bf | 2008-08-17 17:08:36 +0100 | [diff] [blame] | 5624 | path = btrfs_alloc_path(); |
Miao Xie | 16cdcec | 2011-04-22 18:12:22 +0800 | [diff] [blame] | 5625 | if (!path) |
| 5626 | return -ENOMEM; |
Chris Mason | ff5714c | 2011-05-28 07:00:39 -0400 | [diff] [blame] | 5627 | |
Josef Bacik | 026fd31 | 2011-05-13 10:32:11 -0400 | [diff] [blame] | 5628 | path->reada = 1; |
David Woodhouse | 49593bf | 2008-08-17 17:08:36 +0100 | [diff] [blame] | 5629 | |
Miao Xie | 16cdcec | 2011-04-22 18:12:22 +0800 | [diff] [blame] | 5630 | if (key_type == BTRFS_DIR_INDEX_KEY) { |
| 5631 | INIT_LIST_HEAD(&ins_list); |
| 5632 | INIT_LIST_HEAD(&del_list); |
| 5633 | btrfs_get_delayed_items(inode, &ins_list, &del_list); |
| 5634 | } |
| 5635 | |
David Sterba | 962a298 | 2014-06-04 18:41:45 +0200 | [diff] [blame] | 5636 | key.type = key_type; |
Al Viro | 9cdda8d | 2013-05-22 16:48:09 -0400 | [diff] [blame] | 5637 | key.offset = ctx->pos; |
Li Zefan | 33345d01 | 2011-04-20 10:31:50 +0800 | [diff] [blame] | 5638 | key.objectid = btrfs_ino(inode); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 5639 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5640 | ret = btrfs_search_slot(NULL, root, &key, path, 0, 0); |
| 5641 | if (ret < 0) |
| 5642 | goto err; |
David Woodhouse | 49593bf | 2008-08-17 17:08:36 +0100 | [diff] [blame] | 5643 | |
| 5644 | while (1) { |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 5645 | leaf = path->nodes[0]; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5646 | slot = path->slots[0]; |
Li Zefan | b9e03af | 2011-03-23 10:43:58 +0800 | [diff] [blame] | 5647 | if (slot >= btrfs_header_nritems(leaf)) { |
| 5648 | ret = btrfs_next_leaf(root, path); |
| 5649 | if (ret < 0) |
| 5650 | goto err; |
| 5651 | else if (ret > 0) |
| 5652 | break; |
| 5653 | continue; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5654 | } |
Chris Mason | 3de4586 | 2008-11-17 21:02:50 -0500 | [diff] [blame] | 5655 | |
Ross Kirk | dd3cc16 | 2013-09-16 15:58:09 +0100 | [diff] [blame] | 5656 | item = btrfs_item_nr(slot); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 5657 | btrfs_item_key_to_cpu(leaf, &found_key, slot); |
| 5658 | |
| 5659 | if (found_key.objectid != key.objectid) |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5660 | break; |
David Sterba | 962a298 | 2014-06-04 18:41:45 +0200 | [diff] [blame] | 5661 | if (found_key.type != key_type) |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5662 | break; |
Al Viro | 9cdda8d | 2013-05-22 16:48:09 -0400 | [diff] [blame] | 5663 | if (found_key.offset < ctx->pos) |
Li Zefan | b9e03af | 2011-03-23 10:43:58 +0800 | [diff] [blame] | 5664 | goto next; |
Miao Xie | 16cdcec | 2011-04-22 18:12:22 +0800 | [diff] [blame] | 5665 | if (key_type == BTRFS_DIR_INDEX_KEY && |
| 5666 | btrfs_should_delete_dir_index(&del_list, |
| 5667 | found_key.offset)) |
| 5668 | goto next; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 5669 | |
Al Viro | 9cdda8d | 2013-05-22 16:48:09 -0400 | [diff] [blame] | 5670 | ctx->pos = found_key.offset; |
Miao Xie | 16cdcec | 2011-04-22 18:12:22 +0800 | [diff] [blame] | 5671 | is_curr = 1; |
David Woodhouse | 49593bf | 2008-08-17 17:08:36 +0100 | [diff] [blame] | 5672 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5673 | di = btrfs_item_ptr(leaf, slot, struct btrfs_dir_item); |
| 5674 | di_cur = 0; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 5675 | di_total = btrfs_item_size(leaf, item); |
David Woodhouse | 49593bf | 2008-08-17 17:08:36 +0100 | [diff] [blame] | 5676 | |
| 5677 | while (di_cur < di_total) { |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 5678 | struct btrfs_key location; |
| 5679 | |
Josef Bacik | 22a94d4 | 2011-03-16 16:47:17 -0400 | [diff] [blame] | 5680 | if (verify_dir_item(root, leaf, di)) |
| 5681 | break; |
| 5682 | |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 5683 | name_len = btrfs_dir_name_len(leaf, di); |
David Woodhouse | 49593bf | 2008-08-17 17:08:36 +0100 | [diff] [blame] | 5684 | if (name_len <= sizeof(tmp_name)) { |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 5685 | name_ptr = tmp_name; |
| 5686 | } else { |
| 5687 | name_ptr = kmalloc(name_len, GFP_NOFS); |
David Woodhouse | 49593bf | 2008-08-17 17:08:36 +0100 | [diff] [blame] | 5688 | if (!name_ptr) { |
| 5689 | ret = -ENOMEM; |
| 5690 | goto err; |
| 5691 | } |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 5692 | } |
| 5693 | read_extent_buffer(leaf, name_ptr, |
| 5694 | (unsigned long)(di + 1), name_len); |
| 5695 | |
| 5696 | d_type = btrfs_filetype_table[btrfs_dir_type(leaf, di)]; |
| 5697 | btrfs_dir_item_key_to_cpu(leaf, di, &location); |
Chris Mason | 3de4586 | 2008-11-17 21:02:50 -0500 | [diff] [blame] | 5698 | |
Josef Bacik | b4aff1f | 2011-06-28 16:18:59 -0400 | [diff] [blame] | 5699 | |
Chris Mason | 3de4586 | 2008-11-17 21:02:50 -0500 | [diff] [blame] | 5700 | /* is this a reference to our own snapshot? If so |
Arne Jansen | 8c9c2bf | 2012-02-25 09:09:30 +0100 | [diff] [blame] | 5701 | * skip it. |
| 5702 | * |
| 5703 | * In contrast to old kernels, we insert the snapshot's |
| 5704 | * dir item and dir index after it has been created, so |
| 5705 | * we won't find a reference to our own snapshot. We |
| 5706 | * still keep the following code for backward |
| 5707 | * compatibility. |
Chris Mason | 3de4586 | 2008-11-17 21:02:50 -0500 | [diff] [blame] | 5708 | */ |
| 5709 | if (location.type == BTRFS_ROOT_ITEM_KEY && |
| 5710 | location.objectid == root->root_key.objectid) { |
| 5711 | over = 0; |
| 5712 | goto skip; |
| 5713 | } |
Al Viro | 9cdda8d | 2013-05-22 16:48:09 -0400 | [diff] [blame] | 5714 | over = !dir_emit(ctx, name_ptr, name_len, |
| 5715 | location.objectid, d_type); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 5716 | |
Chris Mason | 3de4586 | 2008-11-17 21:02:50 -0500 | [diff] [blame] | 5717 | skip: |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 5718 | if (name_ptr != tmp_name) |
| 5719 | kfree(name_ptr); |
| 5720 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5721 | if (over) |
| 5722 | goto nopos; |
Josef Bacik | 5103e94 | 2007-11-16 11:45:54 -0500 | [diff] [blame] | 5723 | di_len = btrfs_dir_name_len(leaf, di) + |
David Woodhouse | 49593bf | 2008-08-17 17:08:36 +0100 | [diff] [blame] | 5724 | btrfs_dir_data_len(leaf, di) + sizeof(*di); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5725 | di_cur += di_len; |
| 5726 | di = (struct btrfs_dir_item *)((char *)di + di_len); |
| 5727 | } |
Li Zefan | b9e03af | 2011-03-23 10:43:58 +0800 | [diff] [blame] | 5728 | next: |
| 5729 | path->slots[0]++; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5730 | } |
David Woodhouse | 49593bf | 2008-08-17 17:08:36 +0100 | [diff] [blame] | 5731 | |
Miao Xie | 16cdcec | 2011-04-22 18:12:22 +0800 | [diff] [blame] | 5732 | if (key_type == BTRFS_DIR_INDEX_KEY) { |
| 5733 | if (is_curr) |
Al Viro | 9cdda8d | 2013-05-22 16:48:09 -0400 | [diff] [blame] | 5734 | ctx->pos++; |
| 5735 | ret = btrfs_readdir_delayed_dir_index(ctx, &ins_list); |
Miao Xie | 16cdcec | 2011-04-22 18:12:22 +0800 | [diff] [blame] | 5736 | if (ret) |
| 5737 | goto nopos; |
| 5738 | } |
| 5739 | |
David Woodhouse | 49593bf | 2008-08-17 17:08:36 +0100 | [diff] [blame] | 5740 | /* Reached end of directory/root. Bump pos past the last item. */ |
Zach Brown | db62efb | 2013-07-11 16:19:42 -0700 | [diff] [blame] | 5741 | ctx->pos++; |
| 5742 | |
| 5743 | /* |
| 5744 | * Stop new entries from being returned after we return the last |
| 5745 | * entry. |
| 5746 | * |
| 5747 | * New directory entries are assigned a strictly increasing |
| 5748 | * offset. This means that new entries created during readdir |
| 5749 | * are *guaranteed* to be seen in the future by that readdir. |
| 5750 | * This has broken buggy programs which operate on names as |
| 5751 | * they're returned by readdir. Until we re-use freed offsets |
| 5752 | * we have this hack to stop new entries from being returned |
| 5753 | * under the assumption that they'll never reach this huge |
| 5754 | * offset. |
| 5755 | * |
| 5756 | * This is being careful not to overflow 32bit loff_t unless the |
| 5757 | * last entry requires it because doing so has broken 32bit apps |
| 5758 | * in the past. |
| 5759 | */ |
| 5760 | if (key_type == BTRFS_DIR_INDEX_KEY) { |
| 5761 | if (ctx->pos >= INT_MAX) |
| 5762 | ctx->pos = LLONG_MAX; |
| 5763 | else |
| 5764 | ctx->pos = INT_MAX; |
| 5765 | } |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5766 | nopos: |
| 5767 | ret = 0; |
| 5768 | err: |
Miao Xie | 16cdcec | 2011-04-22 18:12:22 +0800 | [diff] [blame] | 5769 | if (key_type == BTRFS_DIR_INDEX_KEY) |
| 5770 | btrfs_put_delayed_items(&ins_list, &del_list); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5771 | btrfs_free_path(path); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5772 | return ret; |
| 5773 | } |
| 5774 | |
Christoph Hellwig | a9185b4 | 2010-03-05 09:21:37 +0100 | [diff] [blame] | 5775 | int btrfs_write_inode(struct inode *inode, struct writeback_control *wbc) |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5776 | { |
| 5777 | struct btrfs_root *root = BTRFS_I(inode)->root; |
| 5778 | struct btrfs_trans_handle *trans; |
| 5779 | int ret = 0; |
Josef Bacik | 0af3d00 | 2010-06-21 14:48:16 -0400 | [diff] [blame] | 5780 | bool nolock = false; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5781 | |
Josef Bacik | 72ac3c0 | 2012-05-23 14:13:11 -0400 | [diff] [blame] | 5782 | if (test_bit(BTRFS_INODE_DUMMY, &BTRFS_I(inode)->runtime_flags)) |
Chris Mason | 4ca8b41 | 2008-08-05 13:30:48 -0400 | [diff] [blame] | 5783 | return 0; |
| 5784 | |
Liu Bo | 83eea1f | 2012-07-10 05:28:39 -0600 | [diff] [blame] | 5785 | 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] | 5786 | nolock = true; |
Josef Bacik | 0af3d00 | 2010-06-21 14:48:16 -0400 | [diff] [blame] | 5787 | |
Christoph Hellwig | a9185b4 | 2010-03-05 09:21:37 +0100 | [diff] [blame] | 5788 | if (wbc->sync_mode == WB_SYNC_ALL) { |
Josef Bacik | 0af3d00 | 2010-06-21 14:48:16 -0400 | [diff] [blame] | 5789 | if (nolock) |
Josef Bacik | 7a7eaa4 | 2011-04-13 12:54:33 -0400 | [diff] [blame] | 5790 | trans = btrfs_join_transaction_nolock(root); |
Josef Bacik | 0af3d00 | 2010-06-21 14:48:16 -0400 | [diff] [blame] | 5791 | else |
Josef Bacik | 7a7eaa4 | 2011-04-13 12:54:33 -0400 | [diff] [blame] | 5792 | trans = btrfs_join_transaction(root); |
Tsutomu Itoh | 3612b49 | 2011-01-25 02:51:38 +0000 | [diff] [blame] | 5793 | if (IS_ERR(trans)) |
| 5794 | return PTR_ERR(trans); |
Miao Xie | a698d075 | 2012-09-20 01:51:59 -0600 | [diff] [blame] | 5795 | ret = btrfs_commit_transaction(trans, root); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5796 | } |
| 5797 | return ret; |
| 5798 | } |
| 5799 | |
| 5800 | /* |
Chris Mason | 54aa1f4 | 2007-06-22 14:16:25 -0400 | [diff] [blame] | 5801 | * This is somewhat expensive, updating the tree every time the |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5802 | * inode changes. But, it is most likely to find the inode in cache. |
| 5803 | * FIXME, needs more benchmarking...there are no reasons other than performance |
| 5804 | * to keep or drop this code. |
| 5805 | */ |
Eric Sandeen | 48a3b63 | 2013-04-25 20:41:01 +0000 | [diff] [blame] | 5806 | static int btrfs_dirty_inode(struct inode *inode) |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5807 | { |
| 5808 | struct btrfs_root *root = BTRFS_I(inode)->root; |
| 5809 | struct btrfs_trans_handle *trans; |
Yan, Zheng | 8929ecfa | 2010-05-16 10:49:58 -0400 | [diff] [blame] | 5810 | int ret; |
| 5811 | |
Josef Bacik | 72ac3c0 | 2012-05-23 14:13:11 -0400 | [diff] [blame] | 5812 | if (test_bit(BTRFS_INODE_DUMMY, &BTRFS_I(inode)->runtime_flags)) |
Josef Bacik | 22c44fe | 2011-11-30 10:45:38 -0500 | [diff] [blame] | 5813 | return 0; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5814 | |
Josef Bacik | 7a7eaa4 | 2011-04-13 12:54:33 -0400 | [diff] [blame] | 5815 | trans = btrfs_join_transaction(root); |
Josef Bacik | 22c44fe | 2011-11-30 10:45:38 -0500 | [diff] [blame] | 5816 | if (IS_ERR(trans)) |
| 5817 | return PTR_ERR(trans); |
Yan, Zheng | 8929ecfa | 2010-05-16 10:49:58 -0400 | [diff] [blame] | 5818 | |
| 5819 | ret = btrfs_update_inode(trans, root, inode); |
Chris Mason | 94b6044 | 2010-05-26 11:02:00 -0400 | [diff] [blame] | 5820 | if (ret && ret == -ENOSPC) { |
| 5821 | /* whoops, lets try again with the full transaction */ |
| 5822 | btrfs_end_transaction(trans, root); |
| 5823 | trans = btrfs_start_transaction(root, 1); |
Josef Bacik | 22c44fe | 2011-11-30 10:45:38 -0500 | [diff] [blame] | 5824 | if (IS_ERR(trans)) |
| 5825 | return PTR_ERR(trans); |
Yan, Zheng | 8929ecfa | 2010-05-16 10:49:58 -0400 | [diff] [blame] | 5826 | |
Chris Mason | 94b6044 | 2010-05-26 11:02:00 -0400 | [diff] [blame] | 5827 | ret = btrfs_update_inode(trans, root, inode); |
Chris Mason | 94b6044 | 2010-05-26 11:02:00 -0400 | [diff] [blame] | 5828 | } |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5829 | btrfs_end_transaction(trans, root); |
Miao Xie | 16cdcec | 2011-04-22 18:12:22 +0800 | [diff] [blame] | 5830 | if (BTRFS_I(inode)->delayed_node) |
| 5831 | btrfs_balance_delayed_items(root); |
Josef Bacik | 22c44fe | 2011-11-30 10:45:38 -0500 | [diff] [blame] | 5832 | |
| 5833 | return ret; |
| 5834 | } |
| 5835 | |
| 5836 | /* |
| 5837 | * This is a copy of file_update_time. We need this so we can return error on |
| 5838 | * ENOSPC for updating the inode in the case of file write and mmap writes. |
| 5839 | */ |
Josef Bacik | e41f941 | 2012-03-26 09:46:47 -0400 | [diff] [blame] | 5840 | static int btrfs_update_time(struct inode *inode, struct timespec *now, |
| 5841 | int flags) |
Josef Bacik | 22c44fe | 2011-11-30 10:45:38 -0500 | [diff] [blame] | 5842 | { |
Alexander Block | 2bc556528 | 2012-06-15 09:49:33 +0200 | [diff] [blame] | 5843 | struct btrfs_root *root = BTRFS_I(inode)->root; |
| 5844 | |
| 5845 | if (btrfs_root_readonly(root)) |
| 5846 | return -EROFS; |
| 5847 | |
Josef Bacik | e41f941 | 2012-03-26 09:46:47 -0400 | [diff] [blame] | 5848 | if (flags & S_VERSION) |
Josef Bacik | 22c44fe | 2011-11-30 10:45:38 -0500 | [diff] [blame] | 5849 | inode_inc_iversion(inode); |
Josef Bacik | e41f941 | 2012-03-26 09:46:47 -0400 | [diff] [blame] | 5850 | if (flags & S_CTIME) |
| 5851 | inode->i_ctime = *now; |
| 5852 | if (flags & S_MTIME) |
| 5853 | inode->i_mtime = *now; |
| 5854 | if (flags & S_ATIME) |
| 5855 | inode->i_atime = *now; |
| 5856 | return btrfs_dirty_inode(inode); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5857 | } |
| 5858 | |
Chris Mason | d352ac6 | 2008-09-29 15:18:18 -0400 | [diff] [blame] | 5859 | /* |
| 5860 | * find the highest existing sequence number in a directory |
| 5861 | * and then set the in-memory index_cnt variable to reflect |
| 5862 | * free sequence numbers |
| 5863 | */ |
Josef Bacik | aec7477 | 2008-07-24 12:12:38 -0400 | [diff] [blame] | 5864 | static int btrfs_set_inode_index_count(struct inode *inode) |
| 5865 | { |
| 5866 | struct btrfs_root *root = BTRFS_I(inode)->root; |
| 5867 | struct btrfs_key key, found_key; |
| 5868 | struct btrfs_path *path; |
| 5869 | struct extent_buffer *leaf; |
| 5870 | int ret; |
| 5871 | |
Li Zefan | 33345d01 | 2011-04-20 10:31:50 +0800 | [diff] [blame] | 5872 | key.objectid = btrfs_ino(inode); |
David Sterba | 962a298 | 2014-06-04 18:41:45 +0200 | [diff] [blame] | 5873 | key.type = BTRFS_DIR_INDEX_KEY; |
Josef Bacik | aec7477 | 2008-07-24 12:12:38 -0400 | [diff] [blame] | 5874 | key.offset = (u64)-1; |
| 5875 | |
| 5876 | path = btrfs_alloc_path(); |
| 5877 | if (!path) |
| 5878 | return -ENOMEM; |
| 5879 | |
| 5880 | ret = btrfs_search_slot(NULL, root, &key, path, 0, 0); |
| 5881 | if (ret < 0) |
| 5882 | goto out; |
| 5883 | /* FIXME: we should be able to handle this */ |
| 5884 | if (ret == 0) |
| 5885 | goto out; |
| 5886 | ret = 0; |
| 5887 | |
| 5888 | /* |
| 5889 | * MAGIC NUMBER EXPLANATION: |
| 5890 | * since we search a directory based on f_pos we have to start at 2 |
| 5891 | * since '.' and '..' have f_pos of 0 and 1 respectively, so everybody |
| 5892 | * else has to start at 2 |
| 5893 | */ |
| 5894 | if (path->slots[0] == 0) { |
| 5895 | BTRFS_I(inode)->index_cnt = 2; |
| 5896 | goto out; |
| 5897 | } |
| 5898 | |
| 5899 | path->slots[0]--; |
| 5900 | |
| 5901 | leaf = path->nodes[0]; |
| 5902 | btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]); |
| 5903 | |
Li Zefan | 33345d01 | 2011-04-20 10:31:50 +0800 | [diff] [blame] | 5904 | if (found_key.objectid != btrfs_ino(inode) || |
David Sterba | 962a298 | 2014-06-04 18:41:45 +0200 | [diff] [blame] | 5905 | found_key.type != BTRFS_DIR_INDEX_KEY) { |
Josef Bacik | aec7477 | 2008-07-24 12:12:38 -0400 | [diff] [blame] | 5906 | BTRFS_I(inode)->index_cnt = 2; |
| 5907 | goto out; |
| 5908 | } |
| 5909 | |
| 5910 | BTRFS_I(inode)->index_cnt = found_key.offset + 1; |
| 5911 | out: |
| 5912 | btrfs_free_path(path); |
| 5913 | return ret; |
| 5914 | } |
| 5915 | |
Chris Mason | d352ac6 | 2008-09-29 15:18:18 -0400 | [diff] [blame] | 5916 | /* |
| 5917 | * helper to find a free sequence number in a given directory. This current |
| 5918 | * code is very simple, later versions will do smarter things in the btree |
| 5919 | */ |
Chris Mason | 3de4586 | 2008-11-17 21:02:50 -0500 | [diff] [blame] | 5920 | int btrfs_set_inode_index(struct inode *dir, u64 *index) |
Josef Bacik | aec7477 | 2008-07-24 12:12:38 -0400 | [diff] [blame] | 5921 | { |
| 5922 | int ret = 0; |
| 5923 | |
| 5924 | if (BTRFS_I(dir)->index_cnt == (u64)-1) { |
Miao Xie | 16cdcec | 2011-04-22 18:12:22 +0800 | [diff] [blame] | 5925 | ret = btrfs_inode_delayed_dir_index_count(dir); |
| 5926 | if (ret) { |
| 5927 | ret = btrfs_set_inode_index_count(dir); |
| 5928 | if (ret) |
| 5929 | return ret; |
| 5930 | } |
Josef Bacik | aec7477 | 2008-07-24 12:12:38 -0400 | [diff] [blame] | 5931 | } |
| 5932 | |
Chris Mason | 00e4e6b | 2008-08-05 11:18:09 -0400 | [diff] [blame] | 5933 | *index = BTRFS_I(dir)->index_cnt; |
Josef Bacik | aec7477 | 2008-07-24 12:12:38 -0400 | [diff] [blame] | 5934 | BTRFS_I(dir)->index_cnt++; |
| 5935 | |
| 5936 | return ret; |
| 5937 | } |
| 5938 | |
Chris Mason | b0d5d10 | 2014-09-08 13:08:51 -0700 | [diff] [blame] | 5939 | static int btrfs_insert_inode_locked(struct inode *inode) |
| 5940 | { |
| 5941 | struct btrfs_iget_args args; |
| 5942 | args.location = &BTRFS_I(inode)->location; |
| 5943 | args.root = BTRFS_I(inode)->root; |
| 5944 | |
| 5945 | return insert_inode_locked4(inode, |
| 5946 | btrfs_inode_hash(inode->i_ino, BTRFS_I(inode)->root), |
| 5947 | btrfs_find_actor, &args); |
| 5948 | } |
| 5949 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5950 | static struct inode *btrfs_new_inode(struct btrfs_trans_handle *trans, |
| 5951 | struct btrfs_root *root, |
Josef Bacik | aec7477 | 2008-07-24 12:12:38 -0400 | [diff] [blame] | 5952 | struct inode *dir, |
Chris Mason | 9c58309 | 2008-01-29 15:15:18 -0500 | [diff] [blame] | 5953 | const char *name, int name_len, |
Al Viro | 175a4eb | 2011-07-26 03:30:54 -0400 | [diff] [blame] | 5954 | u64 ref_objectid, u64 objectid, |
| 5955 | umode_t mode, u64 *index) |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5956 | { |
| 5957 | struct inode *inode; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 5958 | struct btrfs_inode_item *inode_item; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5959 | struct btrfs_key *location; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 5960 | struct btrfs_path *path; |
Chris Mason | 9c58309 | 2008-01-29 15:15:18 -0500 | [diff] [blame] | 5961 | struct btrfs_inode_ref *ref; |
| 5962 | struct btrfs_key key[2]; |
| 5963 | u32 sizes[2]; |
Filipe Manana | ef3b9af | 2014-04-27 20:40:45 +0100 | [diff] [blame] | 5964 | int nitems = name ? 2 : 1; |
Chris Mason | 9c58309 | 2008-01-29 15:15:18 -0500 | [diff] [blame] | 5965 | unsigned long ptr; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5966 | int ret; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5967 | |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 5968 | path = btrfs_alloc_path(); |
Mark Fasheh | d8926bb | 2011-07-13 10:38:47 -0700 | [diff] [blame] | 5969 | if (!path) |
| 5970 | return ERR_PTR(-ENOMEM); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 5971 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5972 | inode = new_inode(root->fs_info->sb); |
Yoshinori Sano | 8fb2764 | 2011-04-09 02:30:07 +0000 | [diff] [blame] | 5973 | if (!inode) { |
| 5974 | btrfs_free_path(path); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5975 | return ERR_PTR(-ENOMEM); |
Yoshinori Sano | 8fb2764 | 2011-04-09 02:30:07 +0000 | [diff] [blame] | 5976 | } |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5977 | |
Li Zefan | 581bb05 | 2011-04-20 10:06:11 +0800 | [diff] [blame] | 5978 | /* |
Filipe Manana | 5762b5c | 2014-08-01 00:10:32 +0100 | [diff] [blame] | 5979 | * O_TMPFILE, set link count to 0, so that after this point, |
| 5980 | * we fill in an inode item with the correct link count. |
| 5981 | */ |
| 5982 | if (!name) |
| 5983 | set_nlink(inode, 0); |
| 5984 | |
| 5985 | /* |
Li Zefan | 581bb05 | 2011-04-20 10:06:11 +0800 | [diff] [blame] | 5986 | * we have to initialize this early, so we can reclaim the inode |
| 5987 | * number if we fail afterwards in this function. |
| 5988 | */ |
| 5989 | inode->i_ino = objectid; |
| 5990 | |
Filipe Manana | ef3b9af | 2014-04-27 20:40:45 +0100 | [diff] [blame] | 5991 | if (dir && name) { |
liubo | 1abe9b8 | 2011-03-24 11:18:59 +0000 | [diff] [blame] | 5992 | trace_btrfs_inode_request(dir); |
| 5993 | |
Chris Mason | 3de4586 | 2008-11-17 21:02:50 -0500 | [diff] [blame] | 5994 | ret = btrfs_set_inode_index(dir, index); |
Shen Feng | 0977143 | 2009-04-02 16:46:06 -0400 | [diff] [blame] | 5995 | if (ret) { |
Yoshinori Sano | 8fb2764 | 2011-04-09 02:30:07 +0000 | [diff] [blame] | 5996 | btrfs_free_path(path); |
Shen Feng | 0977143 | 2009-04-02 16:46:06 -0400 | [diff] [blame] | 5997 | iput(inode); |
Josef Bacik | aec7477 | 2008-07-24 12:12:38 -0400 | [diff] [blame] | 5998 | return ERR_PTR(ret); |
Shen Feng | 0977143 | 2009-04-02 16:46:06 -0400 | [diff] [blame] | 5999 | } |
Filipe Manana | ef3b9af | 2014-04-27 20:40:45 +0100 | [diff] [blame] | 6000 | } else if (dir) { |
| 6001 | *index = 0; |
Josef Bacik | aec7477 | 2008-07-24 12:12:38 -0400 | [diff] [blame] | 6002 | } |
| 6003 | /* |
| 6004 | * index_cnt is ignored for everything but a dir, |
| 6005 | * btrfs_get_inode_index_count has an explanation for the magic |
| 6006 | * number |
| 6007 | */ |
| 6008 | BTRFS_I(inode)->index_cnt = 2; |
Miao Xie | 67de117 | 2013-12-26 13:07:06 +0800 | [diff] [blame] | 6009 | BTRFS_I(inode)->dir_index = *index; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 6010 | BTRFS_I(inode)->root = root; |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 6011 | BTRFS_I(inode)->generation = trans->transid; |
Josef Bacik | 7619585 | 2010-11-19 02:18:02 +0000 | [diff] [blame] | 6012 | inode->i_generation = BTRFS_I(inode)->generation; |
Chris Mason | b888db2b | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 6013 | |
Josef Bacik | 5dc562c | 2012-08-17 13:14:17 -0400 | [diff] [blame] | 6014 | /* |
| 6015 | * We could have gotten an inode number from somebody who was fsynced |
| 6016 | * and then removed in this same transaction, so let's just set full |
| 6017 | * sync since it will be a full sync anyway and this will blow away the |
| 6018 | * old info in the log. |
| 6019 | */ |
| 6020 | set_bit(BTRFS_INODE_NEEDS_FULL_SYNC, &BTRFS_I(inode)->runtime_flags); |
| 6021 | |
Chris Mason | 9c58309 | 2008-01-29 15:15:18 -0500 | [diff] [blame] | 6022 | key[0].objectid = objectid; |
David Sterba | 962a298 | 2014-06-04 18:41:45 +0200 | [diff] [blame] | 6023 | key[0].type = BTRFS_INODE_ITEM_KEY; |
Chris Mason | 9c58309 | 2008-01-29 15:15:18 -0500 | [diff] [blame] | 6024 | key[0].offset = 0; |
| 6025 | |
Chris Mason | 9c58309 | 2008-01-29 15:15:18 -0500 | [diff] [blame] | 6026 | sizes[0] = sizeof(struct btrfs_inode_item); |
Filipe Manana | ef3b9af | 2014-04-27 20:40:45 +0100 | [diff] [blame] | 6027 | |
| 6028 | if (name) { |
| 6029 | /* |
| 6030 | * Start new inodes with an inode_ref. This is slightly more |
| 6031 | * efficient for small numbers of hard links since they will |
| 6032 | * be packed into one item. Extended refs will kick in if we |
| 6033 | * add more hard links than can fit in the ref item. |
| 6034 | */ |
| 6035 | key[1].objectid = objectid; |
David Sterba | 962a298 | 2014-06-04 18:41:45 +0200 | [diff] [blame] | 6036 | key[1].type = BTRFS_INODE_REF_KEY; |
Filipe Manana | ef3b9af | 2014-04-27 20:40:45 +0100 | [diff] [blame] | 6037 | key[1].offset = ref_objectid; |
| 6038 | |
| 6039 | sizes[1] = name_len + sizeof(*ref); |
| 6040 | } |
Chris Mason | 9c58309 | 2008-01-29 15:15:18 -0500 | [diff] [blame] | 6041 | |
Chris Mason | b0d5d10 | 2014-09-08 13:08:51 -0700 | [diff] [blame] | 6042 | location = &BTRFS_I(inode)->location; |
| 6043 | location->objectid = objectid; |
| 6044 | location->offset = 0; |
David Sterba | 962a298 | 2014-06-04 18:41:45 +0200 | [diff] [blame] | 6045 | location->type = BTRFS_INODE_ITEM_KEY; |
Chris Mason | b0d5d10 | 2014-09-08 13:08:51 -0700 | [diff] [blame] | 6046 | |
| 6047 | ret = btrfs_insert_inode_locked(inode); |
| 6048 | if (ret < 0) |
| 6049 | goto fail; |
| 6050 | |
Chris Mason | b947343 | 2009-03-13 11:00:37 -0400 | [diff] [blame] | 6051 | path->leave_spinning = 1; |
Filipe Manana | ef3b9af | 2014-04-27 20:40:45 +0100 | [diff] [blame] | 6052 | ret = btrfs_insert_empty_items(trans, root, path, key, sizes, nitems); |
Chris Mason | 9c58309 | 2008-01-29 15:15:18 -0500 | [diff] [blame] | 6053 | if (ret != 0) |
Chris Mason | b0d5d10 | 2014-09-08 13:08:51 -0700 | [diff] [blame] | 6054 | goto fail_unlock; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 6055 | |
Dmitry Monakhov | ecc11fab | 2010-03-04 17:31:47 +0300 | [diff] [blame] | 6056 | inode_init_owner(inode, dir, mode); |
Yan Zheng | a76a3cd | 2008-10-09 11:46:29 -0400 | [diff] [blame] | 6057 | inode_set_bytes(inode, 0); |
chandan r | 9cc97d6 | 2012-07-04 12:48:07 +0530 | [diff] [blame] | 6058 | |
| 6059 | inode->i_mtime = CURRENT_TIME; |
| 6060 | inode->i_atime = inode->i_mtime; |
| 6061 | inode->i_ctime = inode->i_mtime; |
| 6062 | BTRFS_I(inode)->i_otime = inode->i_mtime; |
| 6063 | |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 6064 | inode_item = btrfs_item_ptr(path->nodes[0], path->slots[0], |
| 6065 | struct btrfs_inode_item); |
Li Zefan | 293f7e0 | 2012-07-10 00:58:58 -0600 | [diff] [blame] | 6066 | memset_extent_buffer(path->nodes[0], 0, (unsigned long)inode_item, |
| 6067 | sizeof(*inode_item)); |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 6068 | fill_inode_item(trans, path->nodes[0], inode_item, inode); |
Chris Mason | 9c58309 | 2008-01-29 15:15:18 -0500 | [diff] [blame] | 6069 | |
Filipe Manana | ef3b9af | 2014-04-27 20:40:45 +0100 | [diff] [blame] | 6070 | if (name) { |
| 6071 | ref = btrfs_item_ptr(path->nodes[0], path->slots[0] + 1, |
| 6072 | struct btrfs_inode_ref); |
| 6073 | btrfs_set_inode_ref_name_len(path->nodes[0], ref, name_len); |
| 6074 | btrfs_set_inode_ref_index(path->nodes[0], ref, *index); |
| 6075 | ptr = (unsigned long)(ref + 1); |
| 6076 | write_extent_buffer(path->nodes[0], name, ptr, name_len); |
| 6077 | } |
Chris Mason | 9c58309 | 2008-01-29 15:15:18 -0500 | [diff] [blame] | 6078 | |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 6079 | btrfs_mark_buffer_dirty(path->nodes[0]); |
| 6080 | btrfs_free_path(path); |
| 6081 | |
Christoph Hellwig | 6cbff00 | 2009-04-17 10:37:41 +0200 | [diff] [blame] | 6082 | btrfs_inherit_iflags(inode, dir); |
| 6083 | |
Al Viro | 569254b | 2011-07-24 17:08:40 -0400 | [diff] [blame] | 6084 | if (S_ISREG(mode)) { |
Chris Mason | 9427216 | 2009-07-02 12:26:06 -0400 | [diff] [blame] | 6085 | if (btrfs_test_opt(root, NODATASUM)) |
| 6086 | BTRFS_I(inode)->flags |= BTRFS_INODE_NODATASUM; |
Liu Bo | 213490b | 2012-09-11 08:33:50 -0600 | [diff] [blame] | 6087 | if (btrfs_test_opt(root, NODATACOW)) |
Josef Bacik | f2bdf9a | 2013-02-21 15:28:28 -0500 | [diff] [blame] | 6088 | BTRFS_I(inode)->flags |= BTRFS_INODE_NODATACOW | |
| 6089 | BTRFS_INODE_NODATASUM; |
Chris Mason | 9427216 | 2009-07-02 12:26:06 -0400 | [diff] [blame] | 6090 | } |
| 6091 | |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 6092 | inode_tree_add(inode); |
liubo | 1abe9b8 | 2011-03-24 11:18:59 +0000 | [diff] [blame] | 6093 | |
| 6094 | trace_btrfs_inode_new(inode); |
Chris Mason | 1973f0f | 2011-06-24 13:13:29 -0400 | [diff] [blame] | 6095 | btrfs_set_inode_last_trans(trans, inode); |
liubo | 1abe9b8 | 2011-03-24 11:18:59 +0000 | [diff] [blame] | 6096 | |
Alexander Block | 8ea05e3 | 2012-07-25 17:35:53 +0200 | [diff] [blame] | 6097 | btrfs_update_root_times(trans, root); |
| 6098 | |
Filipe David Borba Manana | 6354192 | 2014-01-07 11:47:46 +0000 | [diff] [blame] | 6099 | ret = btrfs_inode_inherit_props(trans, inode, dir); |
| 6100 | if (ret) |
| 6101 | btrfs_err(root->fs_info, |
| 6102 | "error inheriting props for ino %llu (root %llu): %d", |
| 6103 | btrfs_ino(inode), root->root_key.objectid, ret); |
| 6104 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 6105 | return inode; |
Chris Mason | b0d5d10 | 2014-09-08 13:08:51 -0700 | [diff] [blame] | 6106 | |
| 6107 | fail_unlock: |
| 6108 | unlock_new_inode(inode); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 6109 | fail: |
Filipe Manana | ef3b9af | 2014-04-27 20:40:45 +0100 | [diff] [blame] | 6110 | if (dir && name) |
Josef Bacik | aec7477 | 2008-07-24 12:12:38 -0400 | [diff] [blame] | 6111 | BTRFS_I(dir)->index_cnt--; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 6112 | btrfs_free_path(path); |
Shen Feng | 0977143 | 2009-04-02 16:46:06 -0400 | [diff] [blame] | 6113 | iput(inode); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 6114 | return ERR_PTR(ret); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 6115 | } |
| 6116 | |
| 6117 | static inline u8 btrfs_inode_type(struct inode *inode) |
| 6118 | { |
| 6119 | return btrfs_type_by_mode[(inode->i_mode & S_IFMT) >> S_SHIFT]; |
| 6120 | } |
| 6121 | |
Chris Mason | d352ac6 | 2008-09-29 15:18:18 -0400 | [diff] [blame] | 6122 | /* |
| 6123 | * utility function to add 'inode' into 'parent_inode' with |
| 6124 | * a give name and a given sequence number. |
| 6125 | * if 'add_backref' is true, also insert a backref from the |
| 6126 | * inode to the parent directory. |
| 6127 | */ |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 6128 | int btrfs_add_link(struct btrfs_trans_handle *trans, |
| 6129 | struct inode *parent_inode, struct inode *inode, |
| 6130 | const char *name, int name_len, int add_backref, u64 index) |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 6131 | { |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 6132 | int ret = 0; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 6133 | struct btrfs_key key; |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 6134 | struct btrfs_root *root = BTRFS_I(parent_inode)->root; |
Li Zefan | 33345d01 | 2011-04-20 10:31:50 +0800 | [diff] [blame] | 6135 | u64 ino = btrfs_ino(inode); |
| 6136 | u64 parent_ino = btrfs_ino(parent_inode); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 6137 | |
Li Zefan | 33345d01 | 2011-04-20 10:31:50 +0800 | [diff] [blame] | 6138 | if (unlikely(ino == BTRFS_FIRST_FREE_OBJECTID)) { |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 6139 | memcpy(&key, &BTRFS_I(inode)->root->root_key, sizeof(key)); |
| 6140 | } else { |
Li Zefan | 33345d01 | 2011-04-20 10:31:50 +0800 | [diff] [blame] | 6141 | key.objectid = ino; |
David Sterba | 962a298 | 2014-06-04 18:41:45 +0200 | [diff] [blame] | 6142 | key.type = BTRFS_INODE_ITEM_KEY; |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 6143 | key.offset = 0; |
| 6144 | } |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 6145 | |
Li Zefan | 33345d01 | 2011-04-20 10:31:50 +0800 | [diff] [blame] | 6146 | if (unlikely(ino == BTRFS_FIRST_FREE_OBJECTID)) { |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 6147 | ret = btrfs_add_root_ref(trans, root->fs_info->tree_root, |
| 6148 | key.objectid, root->root_key.objectid, |
Li Zefan | 33345d01 | 2011-04-20 10:31:50 +0800 | [diff] [blame] | 6149 | parent_ino, index, name, name_len); |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 6150 | } else if (add_backref) { |
Li Zefan | 33345d01 | 2011-04-20 10:31:50 +0800 | [diff] [blame] | 6151 | ret = btrfs_insert_inode_ref(trans, root, name, name_len, ino, |
| 6152 | parent_ino, index); |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 6153 | } |
| 6154 | |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 6155 | /* Nothing to clean up yet */ |
| 6156 | if (ret) |
| 6157 | return ret; |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 6158 | |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 6159 | ret = btrfs_insert_dir_item(trans, root, name, name_len, |
| 6160 | parent_inode, &key, |
| 6161 | btrfs_inode_type(inode), index); |
Chris Mason | 9c52057 | 2012-12-17 14:26:57 -0500 | [diff] [blame] | 6162 | if (ret == -EEXIST || ret == -EOVERFLOW) |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 6163 | goto fail_dir_item; |
| 6164 | else if (ret) { |
| 6165 | btrfs_abort_transaction(trans, root, ret); |
| 6166 | return ret; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 6167 | } |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 6168 | |
| 6169 | btrfs_i_size_write(parent_inode, parent_inode->i_size + |
| 6170 | name_len * 2); |
Josef Bacik | 0c4d2d9 | 2012-04-05 15:03:02 -0400 | [diff] [blame] | 6171 | inode_inc_iversion(parent_inode); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 6172 | parent_inode->i_mtime = parent_inode->i_ctime = CURRENT_TIME; |
| 6173 | ret = btrfs_update_inode(trans, root, parent_inode); |
| 6174 | if (ret) |
| 6175 | btrfs_abort_transaction(trans, root, ret); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 6176 | return ret; |
Chris Mason | fe66a05 | 2012-02-20 08:40:56 -0500 | [diff] [blame] | 6177 | |
| 6178 | fail_dir_item: |
| 6179 | if (unlikely(ino == BTRFS_FIRST_FREE_OBJECTID)) { |
| 6180 | u64 local_index; |
| 6181 | int err; |
| 6182 | err = btrfs_del_root_ref(trans, root->fs_info->tree_root, |
| 6183 | key.objectid, root->root_key.objectid, |
| 6184 | parent_ino, &local_index, name, name_len); |
| 6185 | |
| 6186 | } else if (add_backref) { |
| 6187 | u64 local_index; |
| 6188 | int err; |
| 6189 | |
| 6190 | err = btrfs_del_inode_ref(trans, root, name, name_len, |
| 6191 | ino, parent_ino, &local_index); |
| 6192 | } |
| 6193 | return ret; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 6194 | } |
| 6195 | |
| 6196 | static int btrfs_add_nondir(struct btrfs_trans_handle *trans, |
Josef Bacik | a1b075d | 2010-11-19 20:36:11 +0000 | [diff] [blame] | 6197 | struct inode *dir, struct dentry *dentry, |
| 6198 | struct inode *inode, int backref, u64 index) |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 6199 | { |
Josef Bacik | a1b075d | 2010-11-19 20:36:11 +0000 | [diff] [blame] | 6200 | int err = btrfs_add_link(trans, dir, inode, |
| 6201 | dentry->d_name.name, dentry->d_name.len, |
| 6202 | backref, index); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 6203 | if (err > 0) |
| 6204 | err = -EEXIST; |
| 6205 | return err; |
| 6206 | } |
| 6207 | |
Josef Bacik | 618e21d | 2007-07-11 10:18:17 -0400 | [diff] [blame] | 6208 | static int btrfs_mknod(struct inode *dir, struct dentry *dentry, |
Al Viro | 1a67aaf | 2011-07-26 01:52:52 -0400 | [diff] [blame] | 6209 | umode_t mode, dev_t rdev) |
Josef Bacik | 618e21d | 2007-07-11 10:18:17 -0400 | [diff] [blame] | 6210 | { |
| 6211 | struct btrfs_trans_handle *trans; |
| 6212 | struct btrfs_root *root = BTRFS_I(dir)->root; |
Chris Mason | 1832a6d | 2007-12-21 16:27:21 -0500 | [diff] [blame] | 6213 | struct inode *inode = NULL; |
Josef Bacik | 618e21d | 2007-07-11 10:18:17 -0400 | [diff] [blame] | 6214 | int err; |
| 6215 | int drop_inode = 0; |
| 6216 | u64 objectid; |
Chris Mason | 00e4e6b | 2008-08-05 11:18:09 -0400 | [diff] [blame] | 6217 | u64 index = 0; |
Josef Bacik | 618e21d | 2007-07-11 10:18:17 -0400 | [diff] [blame] | 6218 | |
| 6219 | if (!new_valid_dev(rdev)) |
| 6220 | return -EINVAL; |
| 6221 | |
Josef Bacik | 9ed74f2 | 2009-09-11 16:12:44 -0400 | [diff] [blame] | 6222 | /* |
| 6223 | * 2 for inode item and ref |
| 6224 | * 2 for dir items |
| 6225 | * 1 for xattr if selinux is on |
| 6226 | */ |
Yan, Zheng | a22285a | 2010-05-16 10:48:46 -0400 | [diff] [blame] | 6227 | trans = btrfs_start_transaction(root, 5); |
| 6228 | if (IS_ERR(trans)) |
| 6229 | return PTR_ERR(trans); |
Chris Mason | 1832a6d | 2007-12-21 16:27:21 -0500 | [diff] [blame] | 6230 | |
Li Zefan | 581bb05 | 2011-04-20 10:06:11 +0800 | [diff] [blame] | 6231 | err = btrfs_find_free_ino(root, &objectid); |
| 6232 | if (err) |
| 6233 | goto out_unlock; |
| 6234 | |
Josef Bacik | aec7477 | 2008-07-24 12:12:38 -0400 | [diff] [blame] | 6235 | inode = btrfs_new_inode(trans, root, dir, dentry->d_name.name, |
Li Zefan | 33345d01 | 2011-04-20 10:31:50 +0800 | [diff] [blame] | 6236 | dentry->d_name.len, btrfs_ino(dir), objectid, |
Josef Bacik | d82a6f1d | 2011-05-11 15:26:06 -0400 | [diff] [blame] | 6237 | mode, &index); |
Tsutomu Itoh | 7cf96da | 2011-04-25 19:43:53 -0400 | [diff] [blame] | 6238 | if (IS_ERR(inode)) { |
| 6239 | err = PTR_ERR(inode); |
Josef Bacik | 618e21d | 2007-07-11 10:18:17 -0400 | [diff] [blame] | 6240 | goto out_unlock; |
Tsutomu Itoh | 7cf96da | 2011-04-25 19:43:53 -0400 | [diff] [blame] | 6241 | } |
Josef Bacik | 618e21d | 2007-07-11 10:18:17 -0400 | [diff] [blame] | 6242 | |
Casey Schaufler | ad19db7 | 2011-12-15 10:09:07 -0500 | [diff] [blame] | 6243 | /* |
| 6244 | * If the active LSM wants to access the inode during |
| 6245 | * d_instantiate it needs these. Smack checks to see |
| 6246 | * if the filesystem supports xattrs by looking at the |
| 6247 | * ops vector. |
| 6248 | */ |
Casey Schaufler | ad19db7 | 2011-12-15 10:09:07 -0500 | [diff] [blame] | 6249 | inode->i_op = &btrfs_special_inode_operations; |
Chris Mason | b0d5d10 | 2014-09-08 13:08:51 -0700 | [diff] [blame] | 6250 | init_special_inode(inode, inode->i_mode, rdev); |
| 6251 | |
| 6252 | err = btrfs_init_inode_security(trans, inode, dir, &dentry->d_name); |
Josef Bacik | 618e21d | 2007-07-11 10:18:17 -0400 | [diff] [blame] | 6253 | if (err) |
Chris Mason | b0d5d10 | 2014-09-08 13:08:51 -0700 | [diff] [blame] | 6254 | goto out_unlock_inode; |
| 6255 | |
| 6256 | err = btrfs_add_nondir(trans, dir, dentry, inode, 0, index); |
| 6257 | if (err) { |
| 6258 | goto out_unlock_inode; |
| 6259 | } else { |
Yan | 1b4ab1b | 2007-08-29 09:11:44 -0400 | [diff] [blame] | 6260 | btrfs_update_inode(trans, root, inode); |
Chris Mason | b0d5d10 | 2014-09-08 13:08:51 -0700 | [diff] [blame] | 6261 | unlock_new_inode(inode); |
Al Viro | 08c422c | 2011-12-23 07:58:13 -0500 | [diff] [blame] | 6262 | d_instantiate(dentry, inode); |
Josef Bacik | 618e21d | 2007-07-11 10:18:17 -0400 | [diff] [blame] | 6263 | } |
Chris Mason | b0d5d10 | 2014-09-08 13:08:51 -0700 | [diff] [blame] | 6264 | |
Josef Bacik | 618e21d | 2007-07-11 10:18:17 -0400 | [diff] [blame] | 6265 | out_unlock: |
Josef Bacik | 7ad85bb | 2012-01-12 19:10:12 -0500 | [diff] [blame] | 6266 | btrfs_end_transaction(trans, root); |
Josef Bacik | c581afc | 2014-02-06 16:06:06 -0500 | [diff] [blame] | 6267 | btrfs_balance_delayed_items(root); |
Liu Bo | b53d3f5 | 2012-11-14 14:34:34 +0000 | [diff] [blame] | 6268 | btrfs_btree_balance_dirty(root); |
Josef Bacik | 618e21d | 2007-07-11 10:18:17 -0400 | [diff] [blame] | 6269 | if (drop_inode) { |
| 6270 | inode_dec_link_count(inode); |
| 6271 | iput(inode); |
| 6272 | } |
Josef Bacik | 618e21d | 2007-07-11 10:18:17 -0400 | [diff] [blame] | 6273 | return err; |
Chris Mason | b0d5d10 | 2014-09-08 13:08:51 -0700 | [diff] [blame] | 6274 | |
| 6275 | out_unlock_inode: |
| 6276 | drop_inode = 1; |
| 6277 | unlock_new_inode(inode); |
| 6278 | goto out_unlock; |
| 6279 | |
Josef Bacik | 618e21d | 2007-07-11 10:18:17 -0400 | [diff] [blame] | 6280 | } |
| 6281 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 6282 | static int btrfs_create(struct inode *dir, struct dentry *dentry, |
Al Viro | ebfc3b4 | 2012-06-10 18:05:36 -0400 | [diff] [blame] | 6283 | umode_t mode, bool excl) |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 6284 | { |
| 6285 | struct btrfs_trans_handle *trans; |
| 6286 | struct btrfs_root *root = BTRFS_I(dir)->root; |
Chris Mason | 1832a6d | 2007-12-21 16:27:21 -0500 | [diff] [blame] | 6287 | struct inode *inode = NULL; |
Filipe Brandenburger | 43baa57 | 2012-11-30 03:40:09 +0000 | [diff] [blame] | 6288 | int drop_inode_on_err = 0; |
Yan, Zheng | a22285a | 2010-05-16 10:48:46 -0400 | [diff] [blame] | 6289 | int err; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 6290 | u64 objectid; |
Chris Mason | 00e4e6b | 2008-08-05 11:18:09 -0400 | [diff] [blame] | 6291 | u64 index = 0; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 6292 | |
Josef Bacik | 9ed74f2 | 2009-09-11 16:12:44 -0400 | [diff] [blame] | 6293 | /* |
| 6294 | * 2 for inode item and ref |
| 6295 | * 2 for dir items |
| 6296 | * 1 for xattr if selinux is on |
| 6297 | */ |
Yan, Zheng | a22285a | 2010-05-16 10:48:46 -0400 | [diff] [blame] | 6298 | trans = btrfs_start_transaction(root, 5); |
| 6299 | if (IS_ERR(trans)) |
| 6300 | return PTR_ERR(trans); |
Josef Bacik | 9ed74f2 | 2009-09-11 16:12:44 -0400 | [diff] [blame] | 6301 | |
Li Zefan | 581bb05 | 2011-04-20 10:06:11 +0800 | [diff] [blame] | 6302 | err = btrfs_find_free_ino(root, &objectid); |
| 6303 | if (err) |
| 6304 | goto out_unlock; |
| 6305 | |
Josef Bacik | aec7477 | 2008-07-24 12:12:38 -0400 | [diff] [blame] | 6306 | inode = btrfs_new_inode(trans, root, dir, dentry->d_name.name, |
Li Zefan | 33345d01 | 2011-04-20 10:31:50 +0800 | [diff] [blame] | 6307 | dentry->d_name.len, btrfs_ino(dir), objectid, |
Josef Bacik | d82a6f1d | 2011-05-11 15:26:06 -0400 | [diff] [blame] | 6308 | mode, &index); |
Tsutomu Itoh | 7cf96da | 2011-04-25 19:43:53 -0400 | [diff] [blame] | 6309 | if (IS_ERR(inode)) { |
| 6310 | err = PTR_ERR(inode); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 6311 | goto out_unlock; |
Tsutomu Itoh | 7cf96da | 2011-04-25 19:43:53 -0400 | [diff] [blame] | 6312 | } |
Filipe Brandenburger | 43baa57 | 2012-11-30 03:40:09 +0000 | [diff] [blame] | 6313 | drop_inode_on_err = 1; |
Casey Schaufler | ad19db7 | 2011-12-15 10:09:07 -0500 | [diff] [blame] | 6314 | /* |
| 6315 | * If the active LSM wants to access the inode during |
| 6316 | * d_instantiate it needs these. Smack checks to see |
| 6317 | * if the filesystem supports xattrs by looking at the |
| 6318 | * ops vector. |
| 6319 | */ |
| 6320 | inode->i_fop = &btrfs_file_operations; |
| 6321 | inode->i_op = &btrfs_file_inode_operations; |
Chris Mason | b0d5d10 | 2014-09-08 13:08:51 -0700 | [diff] [blame] | 6322 | inode->i_mapping->a_ops = &btrfs_aops; |
Chris Mason | b0d5d10 | 2014-09-08 13:08:51 -0700 | [diff] [blame] | 6323 | |
| 6324 | err = btrfs_init_inode_security(trans, inode, dir, &dentry->d_name); |
| 6325 | if (err) |
| 6326 | goto out_unlock_inode; |
| 6327 | |
| 6328 | err = btrfs_update_inode(trans, root, inode); |
| 6329 | if (err) |
| 6330 | goto out_unlock_inode; |
Casey Schaufler | ad19db7 | 2011-12-15 10:09:07 -0500 | [diff] [blame] | 6331 | |
Josef Bacik | a1b075d | 2010-11-19 20:36:11 +0000 | [diff] [blame] | 6332 | err = btrfs_add_nondir(trans, dir, dentry, inode, 0, index); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 6333 | if (err) |
Chris Mason | b0d5d10 | 2014-09-08 13:08:51 -0700 | [diff] [blame] | 6334 | goto out_unlock_inode; |
Filipe Brandenburger | 43baa57 | 2012-11-30 03:40:09 +0000 | [diff] [blame] | 6335 | |
Filipe Brandenburger | 43baa57 | 2012-11-30 03:40:09 +0000 | [diff] [blame] | 6336 | BTRFS_I(inode)->io_tree.ops = &btrfs_extent_io_ops; |
Chris Mason | b0d5d10 | 2014-09-08 13:08:51 -0700 | [diff] [blame] | 6337 | unlock_new_inode(inode); |
Filipe Brandenburger | 43baa57 | 2012-11-30 03:40:09 +0000 | [diff] [blame] | 6338 | d_instantiate(dentry, inode); |
| 6339 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 6340 | out_unlock: |
Josef Bacik | 7ad85bb | 2012-01-12 19:10:12 -0500 | [diff] [blame] | 6341 | btrfs_end_transaction(trans, root); |
Filipe Brandenburger | 43baa57 | 2012-11-30 03:40:09 +0000 | [diff] [blame] | 6342 | if (err && drop_inode_on_err) { |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 6343 | inode_dec_link_count(inode); |
| 6344 | iput(inode); |
| 6345 | } |
Josef Bacik | c581afc | 2014-02-06 16:06:06 -0500 | [diff] [blame] | 6346 | btrfs_balance_delayed_items(root); |
Liu Bo | b53d3f5 | 2012-11-14 14:34:34 +0000 | [diff] [blame] | 6347 | btrfs_btree_balance_dirty(root); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 6348 | return err; |
Chris Mason | b0d5d10 | 2014-09-08 13:08:51 -0700 | [diff] [blame] | 6349 | |
| 6350 | out_unlock_inode: |
| 6351 | unlock_new_inode(inode); |
| 6352 | goto out_unlock; |
| 6353 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 6354 | } |
| 6355 | |
| 6356 | static int btrfs_link(struct dentry *old_dentry, struct inode *dir, |
| 6357 | struct dentry *dentry) |
| 6358 | { |
| 6359 | struct btrfs_trans_handle *trans; |
| 6360 | struct btrfs_root *root = BTRFS_I(dir)->root; |
| 6361 | struct inode *inode = old_dentry->d_inode; |
Chris Mason | 00e4e6b | 2008-08-05 11:18:09 -0400 | [diff] [blame] | 6362 | u64 index; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 6363 | int err; |
| 6364 | int drop_inode = 0; |
| 6365 | |
TARUISI Hiroaki | 4a8be42 | 2009-11-12 07:14:26 +0000 | [diff] [blame] | 6366 | /* do not allow sys_link's with other subvols of the same device */ |
| 6367 | if (root->objectid != BTRFS_I(inode)->root->objectid) |
Mark Fasheh | 3ab3564 | 2011-03-22 17:20:26 +0000 | [diff] [blame] | 6368 | return -EXDEV; |
TARUISI Hiroaki | 4a8be42 | 2009-11-12 07:14:26 +0000 | [diff] [blame] | 6369 | |
Mark Fasheh | f186373 | 2012-08-08 11:32:27 -0700 | [diff] [blame] | 6370 | if (inode->i_nlink >= BTRFS_LINK_MAX) |
Al Viro | c055e99 | 2011-03-04 17:15:18 +0000 | [diff] [blame] | 6371 | return -EMLINK; |
Josef Bacik | 9ed74f2 | 2009-09-11 16:12:44 -0400 | [diff] [blame] | 6372 | |
Chris Mason | 3de4586 | 2008-11-17 21:02:50 -0500 | [diff] [blame] | 6373 | err = btrfs_set_inode_index(dir, &index); |
Josef Bacik | aec7477 | 2008-07-24 12:12:38 -0400 | [diff] [blame] | 6374 | if (err) |
| 6375 | goto fail; |
| 6376 | |
Yan, Zheng | a22285a | 2010-05-16 10:48:46 -0400 | [diff] [blame] | 6377 | /* |
Miao Xie | 7e6b646 | 2011-02-18 09:21:17 +0000 | [diff] [blame] | 6378 | * 2 items for inode and inode ref |
Yan, Zheng | a22285a | 2010-05-16 10:48:46 -0400 | [diff] [blame] | 6379 | * 2 items for dir items |
Miao Xie | 7e6b646 | 2011-02-18 09:21:17 +0000 | [diff] [blame] | 6380 | * 1 item for parent inode |
Yan, Zheng | a22285a | 2010-05-16 10:48:46 -0400 | [diff] [blame] | 6381 | */ |
Miao Xie | 7e6b646 | 2011-02-18 09:21:17 +0000 | [diff] [blame] | 6382 | trans = btrfs_start_transaction(root, 5); |
Yan, Zheng | a22285a | 2010-05-16 10:48:46 -0400 | [diff] [blame] | 6383 | if (IS_ERR(trans)) { |
| 6384 | err = PTR_ERR(trans); |
| 6385 | goto fail; |
| 6386 | } |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 6387 | |
Miao Xie | 67de117 | 2013-12-26 13:07:06 +0800 | [diff] [blame] | 6388 | /* There are several dir indexes for this inode, clear the cache. */ |
| 6389 | BTRFS_I(inode)->dir_index = 0ULL; |
Zach Brown | 8b558c5 | 2013-10-16 12:10:34 -0700 | [diff] [blame] | 6390 | inc_nlink(inode); |
Josef Bacik | 0c4d2d9 | 2012-04-05 15:03:02 -0400 | [diff] [blame] | 6391 | inode_inc_iversion(inode); |
Miao Xie | 3153495 | 2011-04-13 13:19:21 +0800 | [diff] [blame] | 6392 | inode->i_ctime = CURRENT_TIME; |
Al Viro | 7de9c6ee | 2010-10-23 11:11:40 -0400 | [diff] [blame] | 6393 | ihold(inode); |
Josef Bacik | e997615 | 2012-10-11 15:53:56 -0400 | [diff] [blame] | 6394 | set_bit(BTRFS_INODE_COPY_EVERYTHING, &BTRFS_I(inode)->runtime_flags); |
Josef Bacik | aec7477 | 2008-07-24 12:12:38 -0400 | [diff] [blame] | 6395 | |
Josef Bacik | a1b075d | 2010-11-19 20:36:11 +0000 | [diff] [blame] | 6396 | err = btrfs_add_nondir(trans, dir, dentry, inode, 1, index); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 6397 | |
Yan, Zheng | a571952 | 2009-09-24 09:17:31 -0400 | [diff] [blame] | 6398 | if (err) { |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 6399 | drop_inode = 1; |
Yan, Zheng | a571952 | 2009-09-24 09:17:31 -0400 | [diff] [blame] | 6400 | } else { |
Al Viro | 10d9f30 | 2011-07-16 23:09:10 -0400 | [diff] [blame] | 6401 | struct dentry *parent = dentry->d_parent; |
Yan, Zheng | a571952 | 2009-09-24 09:17:31 -0400 | [diff] [blame] | 6402 | err = btrfs_update_inode(trans, root, inode); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 6403 | if (err) |
| 6404 | goto fail; |
Filipe Manana | ef3b9af | 2014-04-27 20:40:45 +0100 | [diff] [blame] | 6405 | if (inode->i_nlink == 1) { |
| 6406 | /* |
| 6407 | * If new hard link count is 1, it's a file created |
| 6408 | * with open(2) O_TMPFILE flag. |
| 6409 | */ |
| 6410 | err = btrfs_orphan_del(trans, inode); |
| 6411 | if (err) |
| 6412 | goto fail; |
| 6413 | } |
Al Viro | 08c422c | 2011-12-23 07:58:13 -0500 | [diff] [blame] | 6414 | d_instantiate(dentry, inode); |
Josef Bacik | 6a91221 | 2010-11-20 09:48:00 +0000 | [diff] [blame] | 6415 | btrfs_log_new_name(trans, inode, NULL, parent); |
Yan, Zheng | a571952 | 2009-09-24 09:17:31 -0400 | [diff] [blame] | 6416 | } |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 6417 | |
Josef Bacik | 7ad85bb | 2012-01-12 19:10:12 -0500 | [diff] [blame] | 6418 | btrfs_end_transaction(trans, root); |
Josef Bacik | c581afc | 2014-02-06 16:06:06 -0500 | [diff] [blame] | 6419 | btrfs_balance_delayed_items(root); |
Chris Mason | 1832a6d | 2007-12-21 16:27:21 -0500 | [diff] [blame] | 6420 | fail: |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 6421 | if (drop_inode) { |
| 6422 | inode_dec_link_count(inode); |
| 6423 | iput(inode); |
| 6424 | } |
Liu Bo | b53d3f5 | 2012-11-14 14:34:34 +0000 | [diff] [blame] | 6425 | btrfs_btree_balance_dirty(root); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 6426 | return err; |
| 6427 | } |
| 6428 | |
Al Viro | 18bb1db | 2011-07-26 01:41:39 -0400 | [diff] [blame] | 6429 | 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] | 6430 | { |
Chris Mason | b9d8666 | 2008-05-02 16:13:49 -0400 | [diff] [blame] | 6431 | struct inode *inode = NULL; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 6432 | struct btrfs_trans_handle *trans; |
| 6433 | struct btrfs_root *root = BTRFS_I(dir)->root; |
| 6434 | int err = 0; |
| 6435 | int drop_on_err = 0; |
Chris Mason | b9d8666 | 2008-05-02 16:13:49 -0400 | [diff] [blame] | 6436 | u64 objectid = 0; |
Chris Mason | 00e4e6b | 2008-08-05 11:18:09 -0400 | [diff] [blame] | 6437 | u64 index = 0; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 6438 | |
Josef Bacik | 9ed74f2 | 2009-09-11 16:12:44 -0400 | [diff] [blame] | 6439 | /* |
| 6440 | * 2 items for inode and ref |
| 6441 | * 2 items for dir items |
| 6442 | * 1 for xattr if selinux is on |
| 6443 | */ |
Yan, Zheng | a22285a | 2010-05-16 10:48:46 -0400 | [diff] [blame] | 6444 | trans = btrfs_start_transaction(root, 5); |
| 6445 | if (IS_ERR(trans)) |
| 6446 | return PTR_ERR(trans); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 6447 | |
Li Zefan | 581bb05 | 2011-04-20 10:06:11 +0800 | [diff] [blame] | 6448 | err = btrfs_find_free_ino(root, &objectid); |
| 6449 | if (err) |
| 6450 | goto out_fail; |
| 6451 | |
Josef Bacik | aec7477 | 2008-07-24 12:12:38 -0400 | [diff] [blame] | 6452 | inode = btrfs_new_inode(trans, root, dir, dentry->d_name.name, |
Li Zefan | 33345d01 | 2011-04-20 10:31:50 +0800 | [diff] [blame] | 6453 | dentry->d_name.len, btrfs_ino(dir), objectid, |
Josef Bacik | d82a6f1d | 2011-05-11 15:26:06 -0400 | [diff] [blame] | 6454 | S_IFDIR | mode, &index); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 6455 | if (IS_ERR(inode)) { |
| 6456 | err = PTR_ERR(inode); |
| 6457 | goto out_fail; |
| 6458 | } |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 6459 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 6460 | drop_on_err = 1; |
Chris Mason | b0d5d10 | 2014-09-08 13:08:51 -0700 | [diff] [blame] | 6461 | /* these must be set before we unlock the inode */ |
| 6462 | inode->i_op = &btrfs_dir_inode_operations; |
| 6463 | inode->i_fop = &btrfs_dir_file_operations; |
Josef Bacik | 33268ea | 2008-07-24 12:16:36 -0400 | [diff] [blame] | 6464 | |
Eric Paris | 2a7dba3 | 2011-02-01 11:05:39 -0500 | [diff] [blame] | 6465 | err = btrfs_init_inode_security(trans, inode, dir, &dentry->d_name); |
Josef Bacik | 33268ea | 2008-07-24 12:16:36 -0400 | [diff] [blame] | 6466 | if (err) |
Chris Mason | b0d5d10 | 2014-09-08 13:08:51 -0700 | [diff] [blame] | 6467 | goto out_fail_inode; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 6468 | |
Chris Mason | dbe674a | 2008-07-17 12:54:05 -0400 | [diff] [blame] | 6469 | btrfs_i_size_write(inode, 0); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 6470 | err = btrfs_update_inode(trans, root, inode); |
| 6471 | if (err) |
Chris Mason | b0d5d10 | 2014-09-08 13:08:51 -0700 | [diff] [blame] | 6472 | goto out_fail_inode; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 6473 | |
Josef Bacik | a1b075d | 2010-11-19 20:36:11 +0000 | [diff] [blame] | 6474 | err = btrfs_add_link(trans, dir, inode, dentry->d_name.name, |
| 6475 | dentry->d_name.len, 0, index); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 6476 | if (err) |
Chris Mason | b0d5d10 | 2014-09-08 13:08:51 -0700 | [diff] [blame] | 6477 | goto out_fail_inode; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 6478 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 6479 | d_instantiate(dentry, inode); |
Chris Mason | b0d5d10 | 2014-09-08 13:08:51 -0700 | [diff] [blame] | 6480 | /* |
| 6481 | * mkdir is special. We're unlocking after we call d_instantiate |
| 6482 | * to avoid a race with nfsd calling d_instantiate. |
| 6483 | */ |
| 6484 | unlock_new_inode(inode); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 6485 | drop_on_err = 0; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 6486 | |
| 6487 | out_fail: |
Josef Bacik | 7ad85bb | 2012-01-12 19:10:12 -0500 | [diff] [blame] | 6488 | btrfs_end_transaction(trans, root); |
Wang Shilong | c7cfb8a | 2014-12-24 14:45:30 +0800 | [diff] [blame] | 6489 | if (drop_on_err) { |
| 6490 | inode_dec_link_count(inode); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 6491 | iput(inode); |
Wang Shilong | c7cfb8a | 2014-12-24 14:45:30 +0800 | [diff] [blame] | 6492 | } |
Josef Bacik | c581afc | 2014-02-06 16:06:06 -0500 | [diff] [blame] | 6493 | btrfs_balance_delayed_items(root); |
Liu Bo | b53d3f5 | 2012-11-14 14:34:34 +0000 | [diff] [blame] | 6494 | btrfs_btree_balance_dirty(root); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 6495 | return err; |
Chris Mason | b0d5d10 | 2014-09-08 13:08:51 -0700 | [diff] [blame] | 6496 | |
| 6497 | out_fail_inode: |
| 6498 | unlock_new_inode(inode); |
| 6499 | goto out_fail; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 6500 | } |
| 6501 | |
Qu Wenruo | e6c4efd | 2014-09-17 11:53:35 +0800 | [diff] [blame] | 6502 | /* Find next extent map of a given extent map, caller needs to ensure locks */ |
| 6503 | static struct extent_map *next_extent_map(struct extent_map *em) |
| 6504 | { |
| 6505 | struct rb_node *next; |
| 6506 | |
| 6507 | next = rb_next(&em->rb_node); |
| 6508 | if (!next) |
| 6509 | return NULL; |
| 6510 | return container_of(next, struct extent_map, rb_node); |
| 6511 | } |
| 6512 | |
| 6513 | static struct extent_map *prev_extent_map(struct extent_map *em) |
| 6514 | { |
| 6515 | struct rb_node *prev; |
| 6516 | |
| 6517 | prev = rb_prev(&em->rb_node); |
| 6518 | if (!prev) |
| 6519 | return NULL; |
| 6520 | return container_of(prev, struct extent_map, rb_node); |
| 6521 | } |
| 6522 | |
Chris Mason | d352ac6 | 2008-09-29 15:18:18 -0400 | [diff] [blame] | 6523 | /* helper for btfs_get_extent. Given an existing extent in the tree, |
Qu Wenruo | e6c4efd | 2014-09-17 11:53:35 +0800 | [diff] [blame] | 6524 | * the existing extent is the nearest extent to map_start, |
Chris Mason | d352ac6 | 2008-09-29 15:18:18 -0400 | [diff] [blame] | 6525 | * 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] | 6526 | * the best fitted new extent into the tree. |
Chris Mason | d352ac6 | 2008-09-29 15:18:18 -0400 | [diff] [blame] | 6527 | */ |
Chris Mason | 3b95151 | 2008-04-17 11:29:12 -0400 | [diff] [blame] | 6528 | static int merge_extent_mapping(struct extent_map_tree *em_tree, |
| 6529 | struct extent_map *existing, |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 6530 | struct extent_map *em, |
Qu Wenruo | 51f395a | 2014-08-08 13:06:20 +0800 | [diff] [blame] | 6531 | u64 map_start) |
Chris Mason | 3b95151 | 2008-04-17 11:29:12 -0400 | [diff] [blame] | 6532 | { |
Qu Wenruo | e6c4efd | 2014-09-17 11:53:35 +0800 | [diff] [blame] | 6533 | struct extent_map *prev; |
| 6534 | struct extent_map *next; |
| 6535 | u64 start; |
| 6536 | u64 end; |
Chris Mason | 3b95151 | 2008-04-17 11:29:12 -0400 | [diff] [blame] | 6537 | u64 start_diff; |
Chris Mason | 3b95151 | 2008-04-17 11:29:12 -0400 | [diff] [blame] | 6538 | |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 6539 | BUG_ON(map_start < em->start || map_start >= extent_map_end(em)); |
Qu Wenruo | e6c4efd | 2014-09-17 11:53:35 +0800 | [diff] [blame] | 6540 | |
| 6541 | if (existing->start > map_start) { |
| 6542 | next = existing; |
| 6543 | prev = prev_extent_map(next); |
| 6544 | } else { |
| 6545 | prev = existing; |
| 6546 | next = next_extent_map(prev); |
| 6547 | } |
| 6548 | |
| 6549 | start = prev ? extent_map_end(prev) : em->start; |
| 6550 | start = max_t(u64, start, em->start); |
| 6551 | end = next ? next->start : extent_map_end(em); |
| 6552 | end = min_t(u64, end, extent_map_end(em)); |
| 6553 | start_diff = start - em->start; |
| 6554 | em->start = start; |
| 6555 | em->len = end - start; |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 6556 | if (em->block_start < EXTENT_MAP_LAST_BYTE && |
| 6557 | !test_bit(EXTENT_FLAG_COMPRESSED, &em->flags)) { |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 6558 | em->block_start += start_diff; |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 6559 | em->block_len -= start_diff; |
| 6560 | } |
Josef Bacik | 09a2a8f9 | 2013-04-05 16:51:15 -0400 | [diff] [blame] | 6561 | return add_extent_mapping(em_tree, em, 0); |
Chris Mason | 3b95151 | 2008-04-17 11:29:12 -0400 | [diff] [blame] | 6562 | } |
| 6563 | |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 6564 | static noinline int uncompress_inline(struct btrfs_path *path, |
| 6565 | struct inode *inode, struct page *page, |
| 6566 | size_t pg_offset, u64 extent_offset, |
| 6567 | struct btrfs_file_extent_item *item) |
| 6568 | { |
| 6569 | int ret; |
| 6570 | struct extent_buffer *leaf = path->nodes[0]; |
| 6571 | char *tmp; |
| 6572 | size_t max_size; |
| 6573 | unsigned long inline_size; |
| 6574 | unsigned long ptr; |
Li Zefan | 261507a0 | 2010-12-17 14:21:50 +0800 | [diff] [blame] | 6575 | int compress_type; |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 6576 | |
| 6577 | WARN_ON(pg_offset != 0); |
Li Zefan | 261507a0 | 2010-12-17 14:21:50 +0800 | [diff] [blame] | 6578 | compress_type = btrfs_file_extent_compression(leaf, item); |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 6579 | max_size = btrfs_file_extent_ram_bytes(leaf, item); |
| 6580 | inline_size = btrfs_file_extent_inline_item_len(leaf, |
Ross Kirk | dd3cc16 | 2013-09-16 15:58:09 +0100 | [diff] [blame] | 6581 | btrfs_item_nr(path->slots[0])); |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 6582 | tmp = kmalloc(inline_size, GFP_NOFS); |
Tsutomu Itoh | 8d41371 | 2011-04-25 19:43:52 -0400 | [diff] [blame] | 6583 | if (!tmp) |
| 6584 | return -ENOMEM; |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 6585 | ptr = btrfs_file_extent_inline_start(item); |
| 6586 | |
| 6587 | read_extent_buffer(leaf, tmp, ptr, inline_size); |
| 6588 | |
Chris Mason | 5b050f0 | 2008-11-11 09:34:41 -0500 | [diff] [blame] | 6589 | max_size = min_t(unsigned long, PAGE_CACHE_SIZE, max_size); |
Li Zefan | 261507a0 | 2010-12-17 14:21:50 +0800 | [diff] [blame] | 6590 | ret = btrfs_decompress(compress_type, tmp, page, |
| 6591 | extent_offset, inline_size, max_size); |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 6592 | kfree(tmp); |
Zach Brown | 166ae5a | 2014-05-09 17:15:10 -0400 | [diff] [blame] | 6593 | return ret; |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 6594 | } |
| 6595 | |
Chris Mason | d352ac6 | 2008-09-29 15:18:18 -0400 | [diff] [blame] | 6596 | /* |
| 6597 | * 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] | 6598 | * the ugly parts come from merging extents from the disk with the in-ram |
| 6599 | * representation. This gets more complex because of the data=ordered code, |
Chris Mason | d352ac6 | 2008-09-29 15:18:18 -0400 | [diff] [blame] | 6600 | * where the in-ram extents might be locked pending data=ordered completion. |
| 6601 | * |
| 6602 | * This also copies inline extents directly into the page. |
| 6603 | */ |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 6604 | |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 6605 | struct extent_map *btrfs_get_extent(struct inode *inode, struct page *page, |
Chris Mason | 70dec80 | 2008-01-29 09:59:12 -0500 | [diff] [blame] | 6606 | size_t pg_offset, u64 start, u64 len, |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 6607 | int create) |
| 6608 | { |
| 6609 | int ret; |
| 6610 | int err = 0; |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 6611 | u64 extent_start = 0; |
| 6612 | u64 extent_end = 0; |
Li Zefan | 33345d01 | 2011-04-20 10:31:50 +0800 | [diff] [blame] | 6613 | u64 objectid = btrfs_ino(inode); |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 6614 | u32 found_type; |
Chris Mason | f421950 | 2008-07-22 11:18:09 -0400 | [diff] [blame] | 6615 | struct btrfs_path *path = NULL; |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 6616 | struct btrfs_root *root = BTRFS_I(inode)->root; |
| 6617 | struct btrfs_file_extent_item *item; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 6618 | struct extent_buffer *leaf; |
| 6619 | struct btrfs_key found_key; |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 6620 | struct extent_map *em = NULL; |
| 6621 | struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree; |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 6622 | struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree; |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 6623 | struct btrfs_trans_handle *trans = NULL; |
Filipe Manana | 7ffbb59 | 2014-06-09 03:48:05 +0100 | [diff] [blame] | 6624 | const bool new_inline = !page || create; |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 6625 | |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 6626 | again: |
Chris Mason | 890871b | 2009-09-02 16:24:52 -0400 | [diff] [blame] | 6627 | read_lock(&em_tree->lock); |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 6628 | em = lookup_extent_mapping(em_tree, start, len); |
Chris Mason | a061fc8 | 2008-05-07 11:43:44 -0400 | [diff] [blame] | 6629 | if (em) |
| 6630 | em->bdev = root->fs_info->fs_devices->latest_bdev; |
Chris Mason | 890871b | 2009-09-02 16:24:52 -0400 | [diff] [blame] | 6631 | read_unlock(&em_tree->lock); |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 6632 | |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 6633 | if (em) { |
Chris Mason | e1c4b74 | 2008-04-22 13:26:46 -0400 | [diff] [blame] | 6634 | if (em->start > start || em->start + em->len <= start) |
| 6635 | free_extent_map(em); |
| 6636 | else if (em->block_start == EXTENT_MAP_INLINE && page) |
Chris Mason | 70dec80 | 2008-01-29 09:59:12 -0500 | [diff] [blame] | 6637 | free_extent_map(em); |
| 6638 | else |
| 6639 | goto out; |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 6640 | } |
David Sterba | 172ddd6 | 2011-04-21 00:48:27 +0200 | [diff] [blame] | 6641 | em = alloc_extent_map(); |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 6642 | if (!em) { |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 6643 | err = -ENOMEM; |
| 6644 | goto out; |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 6645 | } |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 6646 | em->bdev = root->fs_info->fs_devices->latest_bdev; |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 6647 | em->start = EXTENT_MAP_HOLE; |
Chris Mason | 445a694 | 2008-11-10 11:53:33 -0500 | [diff] [blame] | 6648 | em->orig_start = EXTENT_MAP_HOLE; |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 6649 | em->len = (u64)-1; |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 6650 | em->block_len = (u64)-1; |
Chris Mason | f421950 | 2008-07-22 11:18:09 -0400 | [diff] [blame] | 6651 | |
| 6652 | if (!path) { |
| 6653 | path = btrfs_alloc_path(); |
Josef Bacik | 026fd31 | 2011-05-13 10:32:11 -0400 | [diff] [blame] | 6654 | if (!path) { |
| 6655 | err = -ENOMEM; |
| 6656 | goto out; |
| 6657 | } |
| 6658 | /* |
| 6659 | * Chances are we'll be called again, so go ahead and do |
| 6660 | * readahead |
| 6661 | */ |
| 6662 | path->reada = 1; |
Chris Mason | f421950 | 2008-07-22 11:18:09 -0400 | [diff] [blame] | 6663 | } |
| 6664 | |
Chris Mason | 179e29e | 2007-11-01 11:28:41 -0400 | [diff] [blame] | 6665 | ret = btrfs_lookup_file_extent(trans, root, path, |
| 6666 | objectid, start, trans != NULL); |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 6667 | if (ret < 0) { |
| 6668 | err = ret; |
| 6669 | goto out; |
| 6670 | } |
| 6671 | |
| 6672 | if (ret != 0) { |
| 6673 | if (path->slots[0] == 0) |
| 6674 | goto not_found; |
| 6675 | path->slots[0]--; |
| 6676 | } |
| 6677 | |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 6678 | leaf = path->nodes[0]; |
| 6679 | item = btrfs_item_ptr(leaf, path->slots[0], |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 6680 | struct btrfs_file_extent_item); |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 6681 | /* are we inside the extent that was found? */ |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 6682 | btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]); |
David Sterba | 962a298 | 2014-06-04 18:41:45 +0200 | [diff] [blame] | 6683 | found_type = found_key.type; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 6684 | if (found_key.objectid != objectid || |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 6685 | found_type != BTRFS_EXTENT_DATA_KEY) { |
Josef Bacik | 25a5034 | 2013-10-14 12:08:38 -0400 | [diff] [blame] | 6686 | /* |
| 6687 | * If we backup past the first extent we want to move forward |
| 6688 | * and see if there is an extent in front of us, otherwise we'll |
| 6689 | * say there is a hole for our whole search range which can |
| 6690 | * cause problems. |
| 6691 | */ |
| 6692 | extent_end = start; |
| 6693 | goto next; |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 6694 | } |
| 6695 | |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 6696 | found_type = btrfs_file_extent_type(leaf, item); |
| 6697 | extent_start = found_key.offset; |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 6698 | if (found_type == BTRFS_FILE_EXTENT_REG || |
| 6699 | found_type == BTRFS_FILE_EXTENT_PREALLOC) { |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 6700 | extent_end = extent_start + |
Chris Mason | db94535 | 2007-10-15 16:15:53 -0400 | [diff] [blame] | 6701 | btrfs_file_extent_num_bytes(leaf, item); |
Yan Zheng | 9036c10 | 2008-10-30 14:19:41 -0400 | [diff] [blame] | 6702 | } else if (found_type == BTRFS_FILE_EXTENT_INLINE) { |
| 6703 | size_t size; |
Chris Mason | 514ac8a | 2014-01-03 21:07:00 -0800 | [diff] [blame] | 6704 | size = btrfs_file_extent_inline_len(leaf, path->slots[0], item); |
Qu Wenruo | fda2832 | 2013-02-26 08:10:22 +0000 | [diff] [blame] | 6705 | extent_end = ALIGN(extent_start + size, root->sectorsize); |
Yan Zheng | 9036c10 | 2008-10-30 14:19:41 -0400 | [diff] [blame] | 6706 | } |
Josef Bacik | 25a5034 | 2013-10-14 12:08:38 -0400 | [diff] [blame] | 6707 | next: |
Yan Zheng | 9036c10 | 2008-10-30 14:19:41 -0400 | [diff] [blame] | 6708 | if (start >= extent_end) { |
| 6709 | path->slots[0]++; |
| 6710 | if (path->slots[0] >= btrfs_header_nritems(leaf)) { |
| 6711 | ret = btrfs_next_leaf(root, path); |
| 6712 | if (ret < 0) { |
| 6713 | err = ret; |
| 6714 | goto out; |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 6715 | } |
Yan Zheng | 9036c10 | 2008-10-30 14:19:41 -0400 | [diff] [blame] | 6716 | if (ret > 0) |
| 6717 | goto not_found; |
| 6718 | leaf = path->nodes[0]; |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 6719 | } |
Yan Zheng | 9036c10 | 2008-10-30 14:19:41 -0400 | [diff] [blame] | 6720 | btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]); |
| 6721 | if (found_key.objectid != objectid || |
| 6722 | found_key.type != BTRFS_EXTENT_DATA_KEY) |
| 6723 | goto not_found; |
| 6724 | if (start + len <= found_key.offset) |
| 6725 | goto not_found; |
Wang Shilong | e2eca69 | 2014-07-17 11:44:14 +0800 | [diff] [blame] | 6726 | if (start > found_key.offset) |
| 6727 | goto next; |
Yan Zheng | 9036c10 | 2008-10-30 14:19:41 -0400 | [diff] [blame] | 6728 | em->start = start; |
Josef Bacik | 70c8a91 | 2012-10-11 16:54:30 -0400 | [diff] [blame] | 6729 | em->orig_start = start; |
Yan Zheng | 9036c10 | 2008-10-30 14:19:41 -0400 | [diff] [blame] | 6730 | em->len = found_key.offset - start; |
| 6731 | goto not_found_em; |
| 6732 | } |
| 6733 | |
Filipe Manana | 7ffbb59 | 2014-06-09 03:48:05 +0100 | [diff] [blame] | 6734 | btrfs_extent_item_to_extent_map(inode, path, item, new_inline, em); |
| 6735 | |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 6736 | if (found_type == BTRFS_FILE_EXTENT_REG || |
| 6737 | found_type == BTRFS_FILE_EXTENT_PREALLOC) { |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 6738 | goto insert; |
| 6739 | } else if (found_type == BTRFS_FILE_EXTENT_INLINE) { |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 6740 | unsigned long ptr; |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 6741 | char *map; |
Chris Mason | 3326d1b | 2007-10-15 16:18:25 -0400 | [diff] [blame] | 6742 | size_t size; |
| 6743 | size_t extent_offset; |
| 6744 | size_t copy_size; |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 6745 | |
Filipe Manana | 7ffbb59 | 2014-06-09 03:48:05 +0100 | [diff] [blame] | 6746 | if (new_inline) |
Yan | 689f934 | 2007-10-29 11:41:07 -0400 | [diff] [blame] | 6747 | goto out; |
Yan | 689f934 | 2007-10-29 11:41:07 -0400 | [diff] [blame] | 6748 | |
Chris Mason | 514ac8a | 2014-01-03 21:07:00 -0800 | [diff] [blame] | 6749 | size = btrfs_file_extent_inline_len(leaf, path->slots[0], item); |
Yan Zheng | 9036c10 | 2008-10-30 14:19:41 -0400 | [diff] [blame] | 6750 | extent_offset = page_offset(page) + pg_offset - extent_start; |
Chris Mason | 70dec80 | 2008-01-29 09:59:12 -0500 | [diff] [blame] | 6751 | copy_size = min_t(u64, PAGE_CACHE_SIZE - pg_offset, |
Yan | 689f934 | 2007-10-29 11:41:07 -0400 | [diff] [blame] | 6752 | size - extent_offset); |
Chris Mason | 3326d1b | 2007-10-15 16:18:25 -0400 | [diff] [blame] | 6753 | em->start = extent_start + extent_offset; |
Qu Wenruo | fda2832 | 2013-02-26 08:10:22 +0000 | [diff] [blame] | 6754 | em->len = ALIGN(copy_size, root->sectorsize); |
Josef Bacik | b493968 | 2012-12-03 10:31:19 -0500 | [diff] [blame] | 6755 | em->orig_block_len = em->len; |
Josef Bacik | 70c8a91 | 2012-10-11 16:54:30 -0400 | [diff] [blame] | 6756 | em->orig_start = em->start; |
Yan | 689f934 | 2007-10-29 11:41:07 -0400 | [diff] [blame] | 6757 | ptr = btrfs_file_extent_inline_start(item) + extent_offset; |
Chris Mason | 179e29e | 2007-11-01 11:28:41 -0400 | [diff] [blame] | 6758 | if (create == 0 && !PageUptodate(page)) { |
Li Zefan | 261507a0 | 2010-12-17 14:21:50 +0800 | [diff] [blame] | 6759 | if (btrfs_file_extent_compression(leaf, item) != |
| 6760 | BTRFS_COMPRESS_NONE) { |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 6761 | ret = uncompress_inline(path, inode, page, |
| 6762 | pg_offset, |
| 6763 | extent_offset, item); |
Zach Brown | 166ae5a | 2014-05-09 17:15:10 -0400 | [diff] [blame] | 6764 | if (ret) { |
| 6765 | err = ret; |
| 6766 | goto out; |
| 6767 | } |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 6768 | } else { |
| 6769 | map = kmap(page); |
| 6770 | read_extent_buffer(leaf, map + pg_offset, ptr, |
| 6771 | copy_size); |
Chris Mason | 93c82d5 | 2009-09-11 12:36:29 -0400 | [diff] [blame] | 6772 | if (pg_offset + copy_size < PAGE_CACHE_SIZE) { |
| 6773 | memset(map + pg_offset + copy_size, 0, |
| 6774 | PAGE_CACHE_SIZE - pg_offset - |
| 6775 | copy_size); |
| 6776 | } |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 6777 | kunmap(page); |
| 6778 | } |
Chris Mason | 179e29e | 2007-11-01 11:28:41 -0400 | [diff] [blame] | 6779 | flush_dcache_page(page); |
| 6780 | } else if (create && PageUptodate(page)) { |
Jan Schmidt | 6bf7e08 | 2011-12-01 14:35:19 +0100 | [diff] [blame] | 6781 | BUG(); |
Chris Mason | 179e29e | 2007-11-01 11:28:41 -0400 | [diff] [blame] | 6782 | if (!trans) { |
| 6783 | kunmap(page); |
| 6784 | free_extent_map(em); |
| 6785 | em = NULL; |
Chris Mason | ff5714c | 2011-05-28 07:00:39 -0400 | [diff] [blame] | 6786 | |
David Sterba | b3b4aa7 | 2011-04-21 01:20:15 +0200 | [diff] [blame] | 6787 | btrfs_release_path(path); |
Josef Bacik | 7a7eaa4 | 2011-04-13 12:54:33 -0400 | [diff] [blame] | 6788 | trans = btrfs_join_transaction(root); |
Chris Mason | ff5714c | 2011-05-28 07:00:39 -0400 | [diff] [blame] | 6789 | |
Tsutomu Itoh | 3612b49 | 2011-01-25 02:51:38 +0000 | [diff] [blame] | 6790 | if (IS_ERR(trans)) |
| 6791 | return ERR_CAST(trans); |
Chris Mason | 179e29e | 2007-11-01 11:28:41 -0400 | [diff] [blame] | 6792 | goto again; |
| 6793 | } |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 6794 | map = kmap(page); |
Chris Mason | 70dec80 | 2008-01-29 09:59:12 -0500 | [diff] [blame] | 6795 | write_extent_buffer(leaf, map + pg_offset, ptr, |
Chris Mason | 179e29e | 2007-11-01 11:28:41 -0400 | [diff] [blame] | 6796 | copy_size); |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 6797 | kunmap(page); |
Chris Mason | 179e29e | 2007-11-01 11:28:41 -0400 | [diff] [blame] | 6798 | btrfs_mark_buffer_dirty(leaf); |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 6799 | } |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 6800 | set_extent_uptodate(io_tree, em->start, |
Arne Jansen | 507903b | 2011-04-06 10:02:20 +0000 | [diff] [blame] | 6801 | extent_map_end(em) - 1, NULL, GFP_NOFS); |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 6802 | goto insert; |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 6803 | } |
| 6804 | not_found: |
| 6805 | em->start = start; |
Josef Bacik | 70c8a91 | 2012-10-11 16:54:30 -0400 | [diff] [blame] | 6806 | em->orig_start = start; |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 6807 | em->len = len; |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 6808 | not_found_em: |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 6809 | em->block_start = EXTENT_MAP_HOLE; |
Yan Zheng | 9036c10 | 2008-10-30 14:19:41 -0400 | [diff] [blame] | 6810 | set_bit(EXTENT_FLAG_VACANCY, &em->flags); |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 6811 | insert: |
David Sterba | b3b4aa7 | 2011-04-21 01:20:15 +0200 | [diff] [blame] | 6812 | btrfs_release_path(path); |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 6813 | if (em->start > start || extent_map_end(em) <= start) { |
Simon Kirby | c2cf52e | 2013-03-19 22:41:23 +0000 | [diff] [blame] | 6814 | 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] | 6815 | em->start, em->len, start, len); |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 6816 | err = -EIO; |
| 6817 | goto out; |
| 6818 | } |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 6819 | |
| 6820 | err = 0; |
Chris Mason | 890871b | 2009-09-02 16:24:52 -0400 | [diff] [blame] | 6821 | write_lock(&em_tree->lock); |
Josef Bacik | 09a2a8f9 | 2013-04-05 16:51:15 -0400 | [diff] [blame] | 6822 | ret = add_extent_mapping(em_tree, em, 0); |
Chris Mason | 3b95151 | 2008-04-17 11:29:12 -0400 | [diff] [blame] | 6823 | /* it is possible that someone inserted the extent into the tree |
| 6824 | * while we had the lock dropped. It is also possible that |
| 6825 | * an overlapping map exists in the tree |
| 6826 | */ |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 6827 | if (ret == -EEXIST) { |
Chris Mason | 3b95151 | 2008-04-17 11:29:12 -0400 | [diff] [blame] | 6828 | struct extent_map *existing; |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 6829 | |
| 6830 | ret = 0; |
| 6831 | |
Qu Wenruo | e6c4efd | 2014-09-17 11:53:35 +0800 | [diff] [blame] | 6832 | existing = search_extent_mapping(em_tree, start, len); |
| 6833 | /* |
| 6834 | * existing will always be non-NULL, since there must be |
| 6835 | * extent causing the -EEXIST. |
| 6836 | */ |
| 6837 | if (start >= extent_map_end(existing) || |
Qu Wenruo | 32be3a1 | 2014-09-22 09:13:03 +0800 | [diff] [blame] | 6838 | start <= existing->start) { |
Qu Wenruo | e6c4efd | 2014-09-17 11:53:35 +0800 | [diff] [blame] | 6839 | /* |
| 6840 | * The existing extent map is the one nearest to |
| 6841 | * the [start, start + len) range which overlaps |
| 6842 | */ |
| 6843 | err = merge_extent_mapping(em_tree, existing, |
| 6844 | em, start); |
Chris Mason | e1c4b74 | 2008-04-22 13:26:46 -0400 | [diff] [blame] | 6845 | free_extent_map(existing); |
Qu Wenruo | e6c4efd | 2014-09-17 11:53:35 +0800 | [diff] [blame] | 6846 | if (err) { |
Chris Mason | 3b95151 | 2008-04-17 11:29:12 -0400 | [diff] [blame] | 6847 | free_extent_map(em); |
| 6848 | em = NULL; |
| 6849 | } |
| 6850 | } else { |
| 6851 | free_extent_map(em); |
| 6852 | em = existing; |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 6853 | err = 0; |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 6854 | } |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 6855 | } |
Chris Mason | 890871b | 2009-09-02 16:24:52 -0400 | [diff] [blame] | 6856 | write_unlock(&em_tree->lock); |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 6857 | out: |
liubo | 1abe9b8 | 2011-03-24 11:18:59 +0000 | [diff] [blame] | 6858 | |
Steven Rostedt | 4cd8587 | 2013-11-14 22:57:29 -0500 | [diff] [blame] | 6859 | trace_btrfs_get_extent(root, em); |
liubo | 1abe9b8 | 2011-03-24 11:18:59 +0000 | [diff] [blame] | 6860 | |
Chris Mason | f421950 | 2008-07-22 11:18:09 -0400 | [diff] [blame] | 6861 | if (path) |
| 6862 | btrfs_free_path(path); |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 6863 | if (trans) { |
| 6864 | ret = btrfs_end_transaction(trans, root); |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 6865 | if (!err) |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 6866 | err = ret; |
| 6867 | } |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 6868 | if (err) { |
| 6869 | free_extent_map(em); |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 6870 | return ERR_PTR(err); |
| 6871 | } |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 6872 | BUG_ON(!em); /* Error is always set */ |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 6873 | return em; |
| 6874 | } |
| 6875 | |
Chris Mason | ec29ed5 | 2011-02-23 16:23:20 -0500 | [diff] [blame] | 6876 | struct extent_map *btrfs_get_extent_fiemap(struct inode *inode, struct page *page, |
| 6877 | size_t pg_offset, u64 start, u64 len, |
| 6878 | int create) |
| 6879 | { |
| 6880 | struct extent_map *em; |
| 6881 | struct extent_map *hole_em = NULL; |
| 6882 | u64 range_start = start; |
| 6883 | u64 end; |
| 6884 | u64 found; |
| 6885 | u64 found_end; |
| 6886 | int err = 0; |
| 6887 | |
| 6888 | em = btrfs_get_extent(inode, page, pg_offset, start, len, create); |
| 6889 | if (IS_ERR(em)) |
| 6890 | return em; |
| 6891 | if (em) { |
| 6892 | /* |
Liu Bo | f9e4fb5 | 2013-01-07 10:10:12 +0000 | [diff] [blame] | 6893 | * if our em maps to |
| 6894 | * - a hole or |
| 6895 | * - a pre-alloc extent, |
| 6896 | * there might actually be delalloc bytes behind it. |
Chris Mason | ec29ed5 | 2011-02-23 16:23:20 -0500 | [diff] [blame] | 6897 | */ |
Liu Bo | f9e4fb5 | 2013-01-07 10:10:12 +0000 | [diff] [blame] | 6898 | if (em->block_start != EXTENT_MAP_HOLE && |
| 6899 | !test_bit(EXTENT_FLAG_PREALLOC, &em->flags)) |
Chris Mason | ec29ed5 | 2011-02-23 16:23:20 -0500 | [diff] [blame] | 6900 | return em; |
| 6901 | else |
| 6902 | hole_em = em; |
| 6903 | } |
| 6904 | |
| 6905 | /* check to see if we've wrapped (len == -1 or similar) */ |
| 6906 | end = start + len; |
| 6907 | if (end < start) |
| 6908 | end = (u64)-1; |
| 6909 | else |
| 6910 | end -= 1; |
| 6911 | |
| 6912 | em = NULL; |
| 6913 | |
| 6914 | /* ok, we didn't find anything, lets look for delalloc */ |
| 6915 | found = count_range_bits(&BTRFS_I(inode)->io_tree, &range_start, |
| 6916 | end, len, EXTENT_DELALLOC, 1); |
| 6917 | found_end = range_start + found; |
| 6918 | if (found_end < range_start) |
| 6919 | found_end = (u64)-1; |
| 6920 | |
| 6921 | /* |
| 6922 | * we didn't find anything useful, return |
| 6923 | * the original results from get_extent() |
| 6924 | */ |
| 6925 | if (range_start > end || found_end <= start) { |
| 6926 | em = hole_em; |
| 6927 | hole_em = NULL; |
| 6928 | goto out; |
| 6929 | } |
| 6930 | |
| 6931 | /* adjust the range_start to make sure it doesn't |
| 6932 | * go backwards from the start they passed in |
| 6933 | */ |
Dulshani Gunawardhana | 6787125 | 2013-10-31 10:33:04 +0530 | [diff] [blame] | 6934 | range_start = max(start, range_start); |
Chris Mason | ec29ed5 | 2011-02-23 16:23:20 -0500 | [diff] [blame] | 6935 | found = found_end - range_start; |
| 6936 | |
| 6937 | if (found > 0) { |
| 6938 | u64 hole_start = start; |
| 6939 | u64 hole_len = len; |
| 6940 | |
David Sterba | 172ddd6 | 2011-04-21 00:48:27 +0200 | [diff] [blame] | 6941 | em = alloc_extent_map(); |
Chris Mason | ec29ed5 | 2011-02-23 16:23:20 -0500 | [diff] [blame] | 6942 | if (!em) { |
| 6943 | err = -ENOMEM; |
| 6944 | goto out; |
| 6945 | } |
| 6946 | /* |
| 6947 | * when btrfs_get_extent can't find anything it |
| 6948 | * returns one huge hole |
| 6949 | * |
| 6950 | * make sure what it found really fits our range, and |
| 6951 | * adjust to make sure it is based on the start from |
| 6952 | * the caller |
| 6953 | */ |
| 6954 | if (hole_em) { |
| 6955 | u64 calc_end = extent_map_end(hole_em); |
| 6956 | |
| 6957 | if (calc_end <= start || (hole_em->start > end)) { |
| 6958 | free_extent_map(hole_em); |
| 6959 | hole_em = NULL; |
| 6960 | } else { |
| 6961 | hole_start = max(hole_em->start, start); |
| 6962 | hole_len = calc_end - hole_start; |
| 6963 | } |
| 6964 | } |
| 6965 | em->bdev = NULL; |
| 6966 | if (hole_em && range_start > hole_start) { |
| 6967 | /* our hole starts before our delalloc, so we |
| 6968 | * have to return just the parts of the hole |
| 6969 | * that go until the delalloc starts |
| 6970 | */ |
| 6971 | em->len = min(hole_len, |
| 6972 | range_start - hole_start); |
| 6973 | em->start = hole_start; |
| 6974 | em->orig_start = hole_start; |
| 6975 | /* |
| 6976 | * don't adjust block start at all, |
| 6977 | * it is fixed at EXTENT_MAP_HOLE |
| 6978 | */ |
| 6979 | em->block_start = hole_em->block_start; |
| 6980 | em->block_len = hole_len; |
Liu Bo | f9e4fb5 | 2013-01-07 10:10:12 +0000 | [diff] [blame] | 6981 | if (test_bit(EXTENT_FLAG_PREALLOC, &hole_em->flags)) |
| 6982 | set_bit(EXTENT_FLAG_PREALLOC, &em->flags); |
Chris Mason | ec29ed5 | 2011-02-23 16:23:20 -0500 | [diff] [blame] | 6983 | } else { |
| 6984 | em->start = range_start; |
| 6985 | em->len = found; |
| 6986 | em->orig_start = range_start; |
| 6987 | em->block_start = EXTENT_MAP_DELALLOC; |
| 6988 | em->block_len = found; |
| 6989 | } |
| 6990 | } else if (hole_em) { |
| 6991 | return hole_em; |
| 6992 | } |
| 6993 | out: |
| 6994 | |
| 6995 | free_extent_map(hole_em); |
| 6996 | if (err) { |
| 6997 | free_extent_map(em); |
| 6998 | return ERR_PTR(err); |
| 6999 | } |
| 7000 | return em; |
| 7001 | } |
| 7002 | |
Josef Bacik | 4b46fce | 2010-05-23 11:00:55 -0400 | [diff] [blame] | 7003 | static struct extent_map *btrfs_new_extent_direct(struct inode *inode, |
| 7004 | u64 start, u64 len) |
| 7005 | { |
| 7006 | struct btrfs_root *root = BTRFS_I(inode)->root; |
Josef Bacik | 70c8a91 | 2012-10-11 16:54:30 -0400 | [diff] [blame] | 7007 | struct extent_map *em; |
Josef Bacik | 4b46fce | 2010-05-23 11:00:55 -0400 | [diff] [blame] | 7008 | struct btrfs_key ins; |
| 7009 | u64 alloc_hint; |
| 7010 | int ret; |
Josef Bacik | 4b46fce | 2010-05-23 11:00:55 -0400 | [diff] [blame] | 7011 | |
Josef Bacik | 4b46fce | 2010-05-23 11:00:55 -0400 | [diff] [blame] | 7012 | alloc_hint = get_extent_allocation_hint(inode, start, len); |
Josef Bacik | 0036158 | 2013-08-14 14:02:47 -0400 | [diff] [blame] | 7013 | ret = btrfs_reserve_extent(root, len, root->sectorsize, 0, |
Miao Xie | e570fd2 | 2014-06-19 10:42:50 +0800 | [diff] [blame] | 7014 | alloc_hint, &ins, 1, 1); |
Josef Bacik | 0036158 | 2013-08-14 14:02:47 -0400 | [diff] [blame] | 7015 | if (ret) |
| 7016 | return ERR_PTR(ret); |
Josef Bacik | 4b46fce | 2010-05-23 11:00:55 -0400 | [diff] [blame] | 7017 | |
Josef Bacik | 70c8a91 | 2012-10-11 16:54:30 -0400 | [diff] [blame] | 7018 | em = create_pinned_em(inode, start, ins.offset, start, ins.objectid, |
Josef Bacik | cc95bef | 2013-04-04 14:31:27 -0400 | [diff] [blame] | 7019 | ins.offset, ins.offset, ins.offset, 0); |
Josef Bacik | 0036158 | 2013-08-14 14:02:47 -0400 | [diff] [blame] | 7020 | if (IS_ERR(em)) { |
Miao Xie | e570fd2 | 2014-06-19 10:42:50 +0800 | [diff] [blame] | 7021 | btrfs_free_reserved_extent(root, ins.objectid, ins.offset, 1); |
Josef Bacik | 0036158 | 2013-08-14 14:02:47 -0400 | [diff] [blame] | 7022 | return em; |
| 7023 | } |
Josef Bacik | 4b46fce | 2010-05-23 11:00:55 -0400 | [diff] [blame] | 7024 | |
| 7025 | ret = btrfs_add_ordered_extent_dio(inode, start, ins.objectid, |
| 7026 | ins.offset, ins.offset, 0); |
| 7027 | if (ret) { |
Miao Xie | e570fd2 | 2014-06-19 10:42:50 +0800 | [diff] [blame] | 7028 | btrfs_free_reserved_extent(root, ins.objectid, ins.offset, 1); |
Josef Bacik | 0036158 | 2013-08-14 14:02:47 -0400 | [diff] [blame] | 7029 | free_extent_map(em); |
| 7030 | return ERR_PTR(ret); |
Josef Bacik | 4b46fce | 2010-05-23 11:00:55 -0400 | [diff] [blame] | 7031 | } |
Josef Bacik | 0036158 | 2013-08-14 14:02:47 -0400 | [diff] [blame] | 7032 | |
Josef Bacik | 4b46fce | 2010-05-23 11:00:55 -0400 | [diff] [blame] | 7033 | return em; |
| 7034 | } |
| 7035 | |
Chris Mason | 46bfbb5 | 2010-05-26 11:04:10 -0400 | [diff] [blame] | 7036 | /* |
| 7037 | * returns 1 when the nocow is safe, < 1 on error, 0 if the |
| 7038 | * block must be cow'd |
| 7039 | */ |
Josef Bacik | 0036158 | 2013-08-14 14:02:47 -0400 | [diff] [blame] | 7040 | noinline int can_nocow_extent(struct inode *inode, u64 offset, u64 *len, |
Josef Bacik | 7ee9e44 | 2013-06-21 16:37:03 -0400 | [diff] [blame] | 7041 | u64 *orig_start, u64 *orig_block_len, |
| 7042 | u64 *ram_bytes) |
Chris Mason | 46bfbb5 | 2010-05-26 11:04:10 -0400 | [diff] [blame] | 7043 | { |
Josef Bacik | 0036158 | 2013-08-14 14:02:47 -0400 | [diff] [blame] | 7044 | struct btrfs_trans_handle *trans; |
Chris Mason | 46bfbb5 | 2010-05-26 11:04:10 -0400 | [diff] [blame] | 7045 | struct btrfs_path *path; |
| 7046 | int ret; |
| 7047 | struct extent_buffer *leaf; |
| 7048 | struct btrfs_root *root = BTRFS_I(inode)->root; |
Miao Xie | 7b2b708 | 2014-02-27 13:58:05 +0800 | [diff] [blame] | 7049 | struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree; |
Chris Mason | 46bfbb5 | 2010-05-26 11:04:10 -0400 | [diff] [blame] | 7050 | struct btrfs_file_extent_item *fi; |
| 7051 | struct btrfs_key key; |
| 7052 | u64 disk_bytenr; |
| 7053 | u64 backref_offset; |
| 7054 | u64 extent_end; |
| 7055 | u64 num_bytes; |
| 7056 | int slot; |
| 7057 | int found_type; |
Josef Bacik | 7ee9e44 | 2013-06-21 16:37:03 -0400 | [diff] [blame] | 7058 | bool nocow = (BTRFS_I(inode)->flags & BTRFS_INODE_NODATACOW); |
Miao Xie | e77751a | 2013-12-27 21:11:50 +0800 | [diff] [blame] | 7059 | |
Chris Mason | 46bfbb5 | 2010-05-26 11:04:10 -0400 | [diff] [blame] | 7060 | path = btrfs_alloc_path(); |
| 7061 | if (!path) |
| 7062 | return -ENOMEM; |
| 7063 | |
Josef Bacik | 0036158 | 2013-08-14 14:02:47 -0400 | [diff] [blame] | 7064 | ret = btrfs_lookup_file_extent(NULL, root, path, btrfs_ino(inode), |
Chris Mason | 46bfbb5 | 2010-05-26 11:04:10 -0400 | [diff] [blame] | 7065 | offset, 0); |
| 7066 | if (ret < 0) |
| 7067 | goto out; |
| 7068 | |
| 7069 | slot = path->slots[0]; |
| 7070 | if (ret == 1) { |
| 7071 | if (slot == 0) { |
| 7072 | /* can't find the item, must cow */ |
| 7073 | ret = 0; |
| 7074 | goto out; |
| 7075 | } |
| 7076 | slot--; |
| 7077 | } |
| 7078 | ret = 0; |
| 7079 | leaf = path->nodes[0]; |
| 7080 | btrfs_item_key_to_cpu(leaf, &key, slot); |
Li Zefan | 33345d01 | 2011-04-20 10:31:50 +0800 | [diff] [blame] | 7081 | if (key.objectid != btrfs_ino(inode) || |
Chris Mason | 46bfbb5 | 2010-05-26 11:04:10 -0400 | [diff] [blame] | 7082 | key.type != BTRFS_EXTENT_DATA_KEY) { |
| 7083 | /* not our file or wrong item type, must cow */ |
| 7084 | goto out; |
| 7085 | } |
| 7086 | |
| 7087 | if (key.offset > offset) { |
| 7088 | /* Wrong offset, must cow */ |
| 7089 | goto out; |
| 7090 | } |
| 7091 | |
| 7092 | fi = btrfs_item_ptr(leaf, slot, struct btrfs_file_extent_item); |
| 7093 | found_type = btrfs_file_extent_type(leaf, fi); |
| 7094 | if (found_type != BTRFS_FILE_EXTENT_REG && |
| 7095 | found_type != BTRFS_FILE_EXTENT_PREALLOC) { |
| 7096 | /* not a regular extent, must cow */ |
| 7097 | goto out; |
| 7098 | } |
Josef Bacik | 7ee9e44 | 2013-06-21 16:37:03 -0400 | [diff] [blame] | 7099 | |
| 7100 | if (!nocow && found_type == BTRFS_FILE_EXTENT_REG) |
| 7101 | goto out; |
| 7102 | |
Miao Xie | e77751a | 2013-12-27 21:11:50 +0800 | [diff] [blame] | 7103 | extent_end = key.offset + btrfs_file_extent_num_bytes(leaf, fi); |
| 7104 | if (extent_end <= offset) |
| 7105 | goto out; |
| 7106 | |
Chris Mason | 46bfbb5 | 2010-05-26 11:04:10 -0400 | [diff] [blame] | 7107 | disk_bytenr = btrfs_file_extent_disk_bytenr(leaf, fi); |
Josef Bacik | 7ee9e44 | 2013-06-21 16:37:03 -0400 | [diff] [blame] | 7108 | if (disk_bytenr == 0) |
| 7109 | goto out; |
| 7110 | |
| 7111 | if (btrfs_file_extent_compression(leaf, fi) || |
| 7112 | btrfs_file_extent_encryption(leaf, fi) || |
| 7113 | btrfs_file_extent_other_encoding(leaf, fi)) |
| 7114 | goto out; |
| 7115 | |
Chris Mason | 46bfbb5 | 2010-05-26 11:04:10 -0400 | [diff] [blame] | 7116 | backref_offset = btrfs_file_extent_offset(leaf, fi); |
| 7117 | |
Josef Bacik | 7ee9e44 | 2013-06-21 16:37:03 -0400 | [diff] [blame] | 7118 | if (orig_start) { |
| 7119 | *orig_start = key.offset - backref_offset; |
| 7120 | *orig_block_len = btrfs_file_extent_disk_num_bytes(leaf, fi); |
| 7121 | *ram_bytes = btrfs_file_extent_ram_bytes(leaf, fi); |
| 7122 | } |
Josef Bacik | eb384b5 | 2013-04-24 16:32:55 -0400 | [diff] [blame] | 7123 | |
Chris Mason | 46bfbb5 | 2010-05-26 11:04:10 -0400 | [diff] [blame] | 7124 | if (btrfs_extent_readonly(root, disk_bytenr)) |
| 7125 | goto out; |
Miao Xie | 7b2b708 | 2014-02-27 13:58:05 +0800 | [diff] [blame] | 7126 | |
| 7127 | num_bytes = min(offset + *len, extent_end) - offset; |
| 7128 | if (!nocow && found_type == BTRFS_FILE_EXTENT_PREALLOC) { |
| 7129 | u64 range_end; |
| 7130 | |
| 7131 | range_end = round_up(offset + num_bytes, root->sectorsize) - 1; |
| 7132 | ret = test_range_bit(io_tree, offset, range_end, |
| 7133 | EXTENT_DELALLOC, 0, NULL); |
| 7134 | if (ret) { |
| 7135 | ret = -EAGAIN; |
| 7136 | goto out; |
| 7137 | } |
| 7138 | } |
| 7139 | |
Josef Bacik | 1bda19e | 2013-10-18 12:10:36 -0400 | [diff] [blame] | 7140 | btrfs_release_path(path); |
Chris Mason | 46bfbb5 | 2010-05-26 11:04:10 -0400 | [diff] [blame] | 7141 | |
| 7142 | /* |
| 7143 | * look for other files referencing this extent, if we |
| 7144 | * find any we must cow |
| 7145 | */ |
Josef Bacik | 0036158 | 2013-08-14 14:02:47 -0400 | [diff] [blame] | 7146 | trans = btrfs_join_transaction(root); |
| 7147 | if (IS_ERR(trans)) { |
| 7148 | ret = 0; |
Chris Mason | 46bfbb5 | 2010-05-26 11:04:10 -0400 | [diff] [blame] | 7149 | goto out; |
Josef Bacik | 0036158 | 2013-08-14 14:02:47 -0400 | [diff] [blame] | 7150 | } |
| 7151 | |
| 7152 | ret = btrfs_cross_ref_exist(trans, root, btrfs_ino(inode), |
| 7153 | key.offset - backref_offset, disk_bytenr); |
| 7154 | btrfs_end_transaction(trans, root); |
| 7155 | if (ret) { |
| 7156 | ret = 0; |
| 7157 | goto out; |
| 7158 | } |
Chris Mason | 46bfbb5 | 2010-05-26 11:04:10 -0400 | [diff] [blame] | 7159 | |
| 7160 | /* |
| 7161 | * adjust disk_bytenr and num_bytes to cover just the bytes |
| 7162 | * in this extent we are about to write. If there |
| 7163 | * are any csums in that range we have to cow in order |
| 7164 | * to keep the csums correct |
| 7165 | */ |
| 7166 | disk_bytenr += backref_offset; |
| 7167 | disk_bytenr += offset - key.offset; |
Chris Mason | 46bfbb5 | 2010-05-26 11:04:10 -0400 | [diff] [blame] | 7168 | if (csum_exist_in_range(root, disk_bytenr, num_bytes)) |
| 7169 | goto out; |
| 7170 | /* |
| 7171 | * all of the above have passed, it is safe to overwrite this extent |
| 7172 | * without cow |
| 7173 | */ |
Josef Bacik | eb384b5 | 2013-04-24 16:32:55 -0400 | [diff] [blame] | 7174 | *len = num_bytes; |
Chris Mason | 46bfbb5 | 2010-05-26 11:04:10 -0400 | [diff] [blame] | 7175 | ret = 1; |
| 7176 | out: |
| 7177 | btrfs_free_path(path); |
| 7178 | return ret; |
| 7179 | } |
| 7180 | |
Alex Gartrell | fc4adbf | 2014-05-20 13:07:56 -0700 | [diff] [blame] | 7181 | bool btrfs_page_exists_in_range(struct inode *inode, loff_t start, loff_t end) |
| 7182 | { |
| 7183 | struct radix_tree_root *root = &inode->i_mapping->page_tree; |
| 7184 | int found = false; |
| 7185 | void **pagep = NULL; |
| 7186 | struct page *page = NULL; |
| 7187 | int start_idx; |
| 7188 | int end_idx; |
| 7189 | |
| 7190 | start_idx = start >> PAGE_CACHE_SHIFT; |
| 7191 | |
| 7192 | /* |
| 7193 | * end is the last byte in the last page. end == start is legal |
| 7194 | */ |
| 7195 | end_idx = end >> PAGE_CACHE_SHIFT; |
| 7196 | |
| 7197 | rcu_read_lock(); |
| 7198 | |
| 7199 | /* Most of the code in this while loop is lifted from |
| 7200 | * find_get_page. It's been modified to begin searching from a |
| 7201 | * page and return just the first page found in that range. If the |
| 7202 | * found idx is less than or equal to the end idx then we know that |
| 7203 | * a page exists. If no pages are found or if those pages are |
| 7204 | * outside of the range then we're fine (yay!) */ |
| 7205 | while (page == NULL && |
| 7206 | radix_tree_gang_lookup_slot(root, &pagep, NULL, start_idx, 1)) { |
| 7207 | page = radix_tree_deref_slot(pagep); |
| 7208 | if (unlikely(!page)) |
| 7209 | break; |
| 7210 | |
| 7211 | if (radix_tree_exception(page)) { |
Filipe Manana | 809f901 | 2014-06-05 13:22:25 +0100 | [diff] [blame] | 7212 | if (radix_tree_deref_retry(page)) { |
| 7213 | page = NULL; |
Alex Gartrell | fc4adbf | 2014-05-20 13:07:56 -0700 | [diff] [blame] | 7214 | continue; |
Filipe Manana | 809f901 | 2014-06-05 13:22:25 +0100 | [diff] [blame] | 7215 | } |
Alex Gartrell | fc4adbf | 2014-05-20 13:07:56 -0700 | [diff] [blame] | 7216 | /* |
| 7217 | * Otherwise, shmem/tmpfs must be storing a swap entry |
| 7218 | * here as an exceptional entry: so return it without |
| 7219 | * attempting to raise page count. |
| 7220 | */ |
Filipe Manana | 6fdef6d | 2014-06-05 13:22:26 +0100 | [diff] [blame] | 7221 | page = NULL; |
Alex Gartrell | fc4adbf | 2014-05-20 13:07:56 -0700 | [diff] [blame] | 7222 | break; /* TODO: Is this relevant for this use case? */ |
| 7223 | } |
| 7224 | |
Filipe Manana | 9140515 | 2014-06-05 13:22:24 +0100 | [diff] [blame] | 7225 | if (!page_cache_get_speculative(page)) { |
| 7226 | page = NULL; |
Alex Gartrell | fc4adbf | 2014-05-20 13:07:56 -0700 | [diff] [blame] | 7227 | continue; |
Filipe Manana | 9140515 | 2014-06-05 13:22:24 +0100 | [diff] [blame] | 7228 | } |
Alex Gartrell | fc4adbf | 2014-05-20 13:07:56 -0700 | [diff] [blame] | 7229 | |
| 7230 | /* |
| 7231 | * Has the page moved? |
| 7232 | * This is part of the lockless pagecache protocol. See |
| 7233 | * include/linux/pagemap.h for details. |
| 7234 | */ |
| 7235 | if (unlikely(page != *pagep)) { |
| 7236 | page_cache_release(page); |
| 7237 | page = NULL; |
| 7238 | } |
| 7239 | } |
| 7240 | |
| 7241 | if (page) { |
| 7242 | if (page->index <= end_idx) |
| 7243 | found = true; |
| 7244 | page_cache_release(page); |
| 7245 | } |
| 7246 | |
| 7247 | rcu_read_unlock(); |
| 7248 | return found; |
| 7249 | } |
| 7250 | |
Josef Bacik | eb838e7 | 2012-07-31 16:28:48 -0400 | [diff] [blame] | 7251 | static int lock_extent_direct(struct inode *inode, u64 lockstart, u64 lockend, |
| 7252 | struct extent_state **cached_state, int writing) |
| 7253 | { |
| 7254 | struct btrfs_ordered_extent *ordered; |
| 7255 | int ret = 0; |
| 7256 | |
| 7257 | while (1) { |
| 7258 | lock_extent_bits(&BTRFS_I(inode)->io_tree, lockstart, lockend, |
| 7259 | 0, cached_state); |
| 7260 | /* |
| 7261 | * We're concerned with the entire range that we're going to be |
| 7262 | * doing DIO to, so we need to make sure theres no ordered |
| 7263 | * extents in this range. |
| 7264 | */ |
| 7265 | ordered = btrfs_lookup_ordered_range(inode, lockstart, |
| 7266 | lockend - lockstart + 1); |
| 7267 | |
| 7268 | /* |
| 7269 | * We need to make sure there are no buffered pages in this |
| 7270 | * range either, we could have raced between the invalidate in |
| 7271 | * generic_file_direct_write and locking the extent. The |
| 7272 | * invalidate needs to happen so that reads after a write do not |
| 7273 | * get stale data. |
| 7274 | */ |
Alex Gartrell | fc4adbf | 2014-05-20 13:07:56 -0700 | [diff] [blame] | 7275 | if (!ordered && |
| 7276 | (!writing || |
| 7277 | !btrfs_page_exists_in_range(inode, lockstart, lockend))) |
Josef Bacik | eb838e7 | 2012-07-31 16:28:48 -0400 | [diff] [blame] | 7278 | break; |
| 7279 | |
| 7280 | unlock_extent_cached(&BTRFS_I(inode)->io_tree, lockstart, lockend, |
| 7281 | cached_state, GFP_NOFS); |
| 7282 | |
| 7283 | if (ordered) { |
| 7284 | btrfs_start_ordered_extent(inode, ordered, 1); |
| 7285 | btrfs_put_ordered_extent(ordered); |
| 7286 | } else { |
| 7287 | /* Screw you mmap */ |
Filipe Manana | 728404d | 2014-10-10 09:43:11 +0100 | [diff] [blame] | 7288 | ret = btrfs_fdatawrite_range(inode, lockstart, lockend); |
Filipe Manana | 075bdbd | 2014-10-09 21:18:55 +0100 | [diff] [blame] | 7289 | if (ret) |
| 7290 | break; |
| 7291 | ret = filemap_fdatawait_range(inode->i_mapping, |
| 7292 | lockstart, |
| 7293 | lockend); |
Josef Bacik | eb838e7 | 2012-07-31 16:28:48 -0400 | [diff] [blame] | 7294 | if (ret) |
| 7295 | break; |
| 7296 | |
| 7297 | /* |
| 7298 | * If we found a page that couldn't be invalidated just |
| 7299 | * fall back to buffered. |
| 7300 | */ |
| 7301 | ret = invalidate_inode_pages2_range(inode->i_mapping, |
| 7302 | lockstart >> PAGE_CACHE_SHIFT, |
| 7303 | lockend >> PAGE_CACHE_SHIFT); |
| 7304 | if (ret) |
| 7305 | break; |
| 7306 | } |
| 7307 | |
| 7308 | cond_resched(); |
| 7309 | } |
| 7310 | |
| 7311 | return ret; |
| 7312 | } |
| 7313 | |
Josef Bacik | 69ffb54 | 2012-09-11 15:40:07 -0400 | [diff] [blame] | 7314 | static struct extent_map *create_pinned_em(struct inode *inode, u64 start, |
| 7315 | u64 len, u64 orig_start, |
| 7316 | u64 block_start, u64 block_len, |
Josef Bacik | cc95bef | 2013-04-04 14:31:27 -0400 | [diff] [blame] | 7317 | u64 orig_block_len, u64 ram_bytes, |
| 7318 | int type) |
Josef Bacik | 69ffb54 | 2012-09-11 15:40:07 -0400 | [diff] [blame] | 7319 | { |
| 7320 | struct extent_map_tree *em_tree; |
| 7321 | struct extent_map *em; |
| 7322 | struct btrfs_root *root = BTRFS_I(inode)->root; |
| 7323 | int ret; |
| 7324 | |
| 7325 | em_tree = &BTRFS_I(inode)->extent_tree; |
| 7326 | em = alloc_extent_map(); |
| 7327 | if (!em) |
| 7328 | return ERR_PTR(-ENOMEM); |
| 7329 | |
| 7330 | em->start = start; |
| 7331 | em->orig_start = orig_start; |
Josef Bacik | 2ab28f3 | 2012-10-12 15:27:49 -0400 | [diff] [blame] | 7332 | em->mod_start = start; |
| 7333 | em->mod_len = len; |
Josef Bacik | 69ffb54 | 2012-09-11 15:40:07 -0400 | [diff] [blame] | 7334 | em->len = len; |
| 7335 | em->block_len = block_len; |
| 7336 | em->block_start = block_start; |
| 7337 | em->bdev = root->fs_info->fs_devices->latest_bdev; |
Josef Bacik | b493968 | 2012-12-03 10:31:19 -0500 | [diff] [blame] | 7338 | em->orig_block_len = orig_block_len; |
Josef Bacik | cc95bef | 2013-04-04 14:31:27 -0400 | [diff] [blame] | 7339 | em->ram_bytes = ram_bytes; |
Josef Bacik | 70c8a91 | 2012-10-11 16:54:30 -0400 | [diff] [blame] | 7340 | em->generation = -1; |
Josef Bacik | 69ffb54 | 2012-09-11 15:40:07 -0400 | [diff] [blame] | 7341 | set_bit(EXTENT_FLAG_PINNED, &em->flags); |
| 7342 | if (type == BTRFS_ORDERED_PREALLOC) |
Josef Bacik | b11e234 | 2012-12-03 10:58:15 -0500 | [diff] [blame] | 7343 | set_bit(EXTENT_FLAG_FILLING, &em->flags); |
Josef Bacik | 69ffb54 | 2012-09-11 15:40:07 -0400 | [diff] [blame] | 7344 | |
| 7345 | do { |
| 7346 | btrfs_drop_extent_cache(inode, em->start, |
| 7347 | em->start + em->len - 1, 0); |
| 7348 | write_lock(&em_tree->lock); |
Josef Bacik | 09a2a8f9 | 2013-04-05 16:51:15 -0400 | [diff] [blame] | 7349 | ret = add_extent_mapping(em_tree, em, 1); |
Josef Bacik | 69ffb54 | 2012-09-11 15:40:07 -0400 | [diff] [blame] | 7350 | write_unlock(&em_tree->lock); |
| 7351 | } while (ret == -EEXIST); |
| 7352 | |
| 7353 | if (ret) { |
| 7354 | free_extent_map(em); |
| 7355 | return ERR_PTR(ret); |
| 7356 | } |
| 7357 | |
| 7358 | return em; |
| 7359 | } |
| 7360 | |
| 7361 | |
Josef Bacik | 4b46fce | 2010-05-23 11:00:55 -0400 | [diff] [blame] | 7362 | static int btrfs_get_blocks_direct(struct inode *inode, sector_t iblock, |
| 7363 | struct buffer_head *bh_result, int create) |
| 7364 | { |
| 7365 | struct extent_map *em; |
| 7366 | struct btrfs_root *root = BTRFS_I(inode)->root; |
Josef Bacik | eb838e7 | 2012-07-31 16:28:48 -0400 | [diff] [blame] | 7367 | struct extent_state *cached_state = NULL; |
Josef Bacik | 4b46fce | 2010-05-23 11:00:55 -0400 | [diff] [blame] | 7368 | u64 start = iblock << inode->i_blkbits; |
Josef Bacik | eb838e7 | 2012-07-31 16:28:48 -0400 | [diff] [blame] | 7369 | u64 lockstart, lockend; |
Josef Bacik | 4b46fce | 2010-05-23 11:00:55 -0400 | [diff] [blame] | 7370 | u64 len = bh_result->b_size; |
Josef Bacik | e1cbbfa | 2015-03-17 10:52:28 -0400 | [diff] [blame] | 7371 | u64 *outstanding_extents = NULL; |
Josef Bacik | eb838e7 | 2012-07-31 16:28:48 -0400 | [diff] [blame] | 7372 | int unlock_bits = EXTENT_LOCKED; |
Miao Xie | 0934856 | 2013-02-07 10:12:07 +0000 | [diff] [blame] | 7373 | int ret = 0; |
Josef Bacik | eb838e7 | 2012-07-31 16:28:48 -0400 | [diff] [blame] | 7374 | |
Miao Xie | 172a504 | 2013-02-21 02:48:22 -0700 | [diff] [blame] | 7375 | if (create) |
Josef Bacik | 3266789 | 2015-02-11 15:08:58 -0500 | [diff] [blame] | 7376 | unlock_bits |= EXTENT_DIRTY; |
Miao Xie | 172a504 | 2013-02-21 02:48:22 -0700 | [diff] [blame] | 7377 | else |
Josef Bacik | c329861 | 2012-08-03 16:49:19 -0400 | [diff] [blame] | 7378 | len = min_t(u64, len, root->sectorsize); |
Josef Bacik | eb838e7 | 2012-07-31 16:28:48 -0400 | [diff] [blame] | 7379 | |
Josef Bacik | c329861 | 2012-08-03 16:49:19 -0400 | [diff] [blame] | 7380 | lockstart = start; |
| 7381 | lockend = start + len - 1; |
| 7382 | |
Josef Bacik | e1cbbfa | 2015-03-17 10:52:28 -0400 | [diff] [blame] | 7383 | if (current->journal_info) { |
| 7384 | /* |
| 7385 | * Need to pull our outstanding extents and set journal_info to NULL so |
| 7386 | * that anything that needs to check if there's a transction doesn't get |
| 7387 | * confused. |
| 7388 | */ |
| 7389 | outstanding_extents = current->journal_info; |
| 7390 | current->journal_info = NULL; |
| 7391 | } |
| 7392 | |
Josef Bacik | eb838e7 | 2012-07-31 16:28:48 -0400 | [diff] [blame] | 7393 | /* |
| 7394 | * If this errors out it's because we couldn't invalidate pagecache for |
| 7395 | * this range and we need to fallback to buffered. |
| 7396 | */ |
| 7397 | if (lock_extent_direct(inode, lockstart, lockend, &cached_state, create)) |
| 7398 | return -ENOTBLK; |
| 7399 | |
Josef Bacik | 4b46fce | 2010-05-23 11:00:55 -0400 | [diff] [blame] | 7400 | em = btrfs_get_extent(inode, NULL, 0, start, len, 0); |
Josef Bacik | eb838e7 | 2012-07-31 16:28:48 -0400 | [diff] [blame] | 7401 | if (IS_ERR(em)) { |
| 7402 | ret = PTR_ERR(em); |
| 7403 | goto unlock_err; |
| 7404 | } |
Josef Bacik | 4b46fce | 2010-05-23 11:00:55 -0400 | [diff] [blame] | 7405 | |
| 7406 | /* |
| 7407 | * Ok for INLINE and COMPRESSED extents we need to fallback on buffered |
| 7408 | * io. INLINE is special, and we could probably kludge it in here, but |
| 7409 | * it's still buffered so for safety lets just fall back to the generic |
| 7410 | * buffered path. |
| 7411 | * |
| 7412 | * For COMPRESSED we _have_ to read the entire extent in so we can |
| 7413 | * decompress it, so there will be buffering required no matter what we |
| 7414 | * do, so go ahead and fallback to buffered. |
| 7415 | * |
| 7416 | * We return -ENOTBLK because thats what makes DIO go ahead and go back |
| 7417 | * to buffered IO. Don't blame me, this is the price we pay for using |
| 7418 | * the generic code. |
| 7419 | */ |
| 7420 | if (test_bit(EXTENT_FLAG_COMPRESSED, &em->flags) || |
| 7421 | em->block_start == EXTENT_MAP_INLINE) { |
| 7422 | free_extent_map(em); |
Josef Bacik | eb838e7 | 2012-07-31 16:28:48 -0400 | [diff] [blame] | 7423 | ret = -ENOTBLK; |
| 7424 | goto unlock_err; |
Josef Bacik | 4b46fce | 2010-05-23 11:00:55 -0400 | [diff] [blame] | 7425 | } |
| 7426 | |
| 7427 | /* Just a good old fashioned hole, return */ |
| 7428 | if (!create && (em->block_start == EXTENT_MAP_HOLE || |
| 7429 | test_bit(EXTENT_FLAG_PREALLOC, &em->flags))) { |
| 7430 | free_extent_map(em); |
Josef Bacik | eb838e7 | 2012-07-31 16:28:48 -0400 | [diff] [blame] | 7431 | goto unlock_err; |
Josef Bacik | 4b46fce | 2010-05-23 11:00:55 -0400 | [diff] [blame] | 7432 | } |
| 7433 | |
| 7434 | /* |
| 7435 | * We don't allocate a new extent in the following cases |
| 7436 | * |
| 7437 | * 1) The inode is marked as NODATACOW. In this case we'll just use the |
| 7438 | * existing extent. |
| 7439 | * 2) The extent is marked as PREALLOC. We're good to go here and can |
| 7440 | * just use the extent. |
| 7441 | * |
| 7442 | */ |
Chris Mason | 46bfbb5 | 2010-05-26 11:04:10 -0400 | [diff] [blame] | 7443 | if (!create) { |
Josef Bacik | eb838e7 | 2012-07-31 16:28:48 -0400 | [diff] [blame] | 7444 | len = min(len, em->len - (start - em->start)); |
| 7445 | lockstart = start + len; |
| 7446 | goto unlock; |
Chris Mason | 46bfbb5 | 2010-05-26 11:04:10 -0400 | [diff] [blame] | 7447 | } |
Josef Bacik | 4b46fce | 2010-05-23 11:00:55 -0400 | [diff] [blame] | 7448 | |
| 7449 | if (test_bit(EXTENT_FLAG_PREALLOC, &em->flags) || |
| 7450 | ((BTRFS_I(inode)->flags & BTRFS_INODE_NODATACOW) && |
| 7451 | em->block_start != EXTENT_MAP_HOLE)) { |
Josef Bacik | 4b46fce | 2010-05-23 11:00:55 -0400 | [diff] [blame] | 7452 | int type; |
Josef Bacik | eb384b5 | 2013-04-24 16:32:55 -0400 | [diff] [blame] | 7453 | u64 block_start, orig_start, orig_block_len, ram_bytes; |
Josef Bacik | 4b46fce | 2010-05-23 11:00:55 -0400 | [diff] [blame] | 7454 | |
| 7455 | if (test_bit(EXTENT_FLAG_PREALLOC, &em->flags)) |
| 7456 | type = BTRFS_ORDERED_PREALLOC; |
| 7457 | else |
| 7458 | type = BTRFS_ORDERED_NOCOW; |
Chris Mason | 46bfbb5 | 2010-05-26 11:04:10 -0400 | [diff] [blame] | 7459 | len = min(len, em->len - (start - em->start)); |
Josef Bacik | 4b46fce | 2010-05-23 11:00:55 -0400 | [diff] [blame] | 7460 | block_start = em->block_start + (start - em->start); |
Chris Mason | 46bfbb5 | 2010-05-26 11:04:10 -0400 | [diff] [blame] | 7461 | |
Josef Bacik | 0036158 | 2013-08-14 14:02:47 -0400 | [diff] [blame] | 7462 | if (can_nocow_extent(inode, start, &len, &orig_start, |
Josef Bacik | 7ee9e44 | 2013-06-21 16:37:03 -0400 | [diff] [blame] | 7463 | &orig_block_len, &ram_bytes) == 1) { |
Josef Bacik | 69ffb54 | 2012-09-11 15:40:07 -0400 | [diff] [blame] | 7464 | if (type == BTRFS_ORDERED_PREALLOC) { |
| 7465 | free_extent_map(em); |
| 7466 | em = create_pinned_em(inode, start, len, |
| 7467 | orig_start, |
Josef Bacik | b493968 | 2012-12-03 10:31:19 -0500 | [diff] [blame] | 7468 | block_start, len, |
Josef Bacik | cc95bef | 2013-04-04 14:31:27 -0400 | [diff] [blame] | 7469 | orig_block_len, |
| 7470 | ram_bytes, type); |
Filipe Manana | 555e128 | 2014-07-07 12:35:21 +0100 | [diff] [blame] | 7471 | if (IS_ERR(em)) { |
| 7472 | ret = PTR_ERR(em); |
Josef Bacik | 69ffb54 | 2012-09-11 15:40:07 -0400 | [diff] [blame] | 7473 | goto unlock_err; |
Filipe Manana | 555e128 | 2014-07-07 12:35:21 +0100 | [diff] [blame] | 7474 | } |
Josef Bacik | 69ffb54 | 2012-09-11 15:40:07 -0400 | [diff] [blame] | 7475 | } |
| 7476 | |
Chris Mason | 46bfbb5 | 2010-05-26 11:04:10 -0400 | [diff] [blame] | 7477 | ret = btrfs_add_ordered_extent_dio(inode, start, |
| 7478 | block_start, len, len, type); |
Chris Mason | 46bfbb5 | 2010-05-26 11:04:10 -0400 | [diff] [blame] | 7479 | if (ret) { |
| 7480 | free_extent_map(em); |
Josef Bacik | eb838e7 | 2012-07-31 16:28:48 -0400 | [diff] [blame] | 7481 | goto unlock_err; |
Chris Mason | 46bfbb5 | 2010-05-26 11:04:10 -0400 | [diff] [blame] | 7482 | } |
| 7483 | goto unlock; |
Josef Bacik | 4b46fce | 2010-05-23 11:00:55 -0400 | [diff] [blame] | 7484 | } |
Josef Bacik | 4b46fce | 2010-05-23 11:00:55 -0400 | [diff] [blame] | 7485 | } |
Josef Bacik | 0036158 | 2013-08-14 14:02:47 -0400 | [diff] [blame] | 7486 | |
Chris Mason | 46bfbb5 | 2010-05-26 11:04:10 -0400 | [diff] [blame] | 7487 | /* |
| 7488 | * this will cow the extent, reset the len in case we changed |
| 7489 | * it above |
| 7490 | */ |
| 7491 | len = bh_result->b_size; |
Josef Bacik | 70c8a91 | 2012-10-11 16:54:30 -0400 | [diff] [blame] | 7492 | free_extent_map(em); |
| 7493 | em = btrfs_new_extent_direct(inode, start, len); |
Josef Bacik | eb838e7 | 2012-07-31 16:28:48 -0400 | [diff] [blame] | 7494 | if (IS_ERR(em)) { |
| 7495 | ret = PTR_ERR(em); |
| 7496 | goto unlock_err; |
| 7497 | } |
Chris Mason | 46bfbb5 | 2010-05-26 11:04:10 -0400 | [diff] [blame] | 7498 | len = min(len, em->len - (start - em->start)); |
| 7499 | unlock: |
Josef Bacik | 4b46fce | 2010-05-23 11:00:55 -0400 | [diff] [blame] | 7500 | bh_result->b_blocknr = (em->block_start + (start - em->start)) >> |
| 7501 | inode->i_blkbits; |
Chris Mason | 46bfbb5 | 2010-05-26 11:04:10 -0400 | [diff] [blame] | 7502 | bh_result->b_size = len; |
Josef Bacik | 4b46fce | 2010-05-23 11:00:55 -0400 | [diff] [blame] | 7503 | bh_result->b_bdev = em->bdev; |
| 7504 | set_buffer_mapped(bh_result); |
Josef Bacik | c3473e8 | 2012-06-19 10:59:00 -0400 | [diff] [blame] | 7505 | if (create) { |
| 7506 | if (!test_bit(EXTENT_FLAG_PREALLOC, &em->flags)) |
| 7507 | set_buffer_new(bh_result); |
| 7508 | |
| 7509 | /* |
| 7510 | * Need to update the i_size under the extent lock so buffered |
| 7511 | * readers will get the updated i_size when we unlock. |
| 7512 | */ |
| 7513 | if (start + len > i_size_read(inode)) |
| 7514 | i_size_write(inode, start + len); |
Miao Xie | 0934856 | 2013-02-07 10:12:07 +0000 | [diff] [blame] | 7515 | |
Josef Bacik | e1cbbfa | 2015-03-17 10:52:28 -0400 | [diff] [blame] | 7516 | /* |
| 7517 | * If we have an outstanding_extents count still set then we're |
| 7518 | * within our reservation, otherwise we need to adjust our inode |
| 7519 | * counter appropriately. |
| 7520 | */ |
| 7521 | if (*outstanding_extents) { |
| 7522 | (*outstanding_extents)--; |
| 7523 | } else { |
Josef Bacik | 3e05bde | 2015-02-11 15:08:57 -0500 | [diff] [blame] | 7524 | spin_lock(&BTRFS_I(inode)->lock); |
| 7525 | BTRFS_I(inode)->outstanding_extents++; |
| 7526 | spin_unlock(&BTRFS_I(inode)->lock); |
| 7527 | } |
Josef Bacik | e1cbbfa | 2015-03-17 10:52:28 -0400 | [diff] [blame] | 7528 | |
| 7529 | current->journal_info = outstanding_extents; |
Josef Bacik | 3266789 | 2015-02-11 15:08:58 -0500 | [diff] [blame] | 7530 | btrfs_free_reserved_data_space(inode, len); |
Josef Bacik | c3473e8 | 2012-06-19 10:59:00 -0400 | [diff] [blame] | 7531 | } |
Josef Bacik | 4b46fce | 2010-05-23 11:00:55 -0400 | [diff] [blame] | 7532 | |
Josef Bacik | eb838e7 | 2012-07-31 16:28:48 -0400 | [diff] [blame] | 7533 | /* |
| 7534 | * In the case of write we need to clear and unlock the entire range, |
| 7535 | * in the case of read we need to unlock only the end area that we |
| 7536 | * aren't using if there is any left over space. |
| 7537 | */ |
Liu Bo | 24c03fa | 2012-08-22 20:10:38 -0600 | [diff] [blame] | 7538 | if (lockstart < lockend) { |
Miao Xie | 0934856 | 2013-02-07 10:12:07 +0000 | [diff] [blame] | 7539 | clear_extent_bit(&BTRFS_I(inode)->io_tree, lockstart, |
| 7540 | lockend, unlock_bits, 1, 0, |
| 7541 | &cached_state, GFP_NOFS); |
Liu Bo | 24c03fa | 2012-08-22 20:10:38 -0600 | [diff] [blame] | 7542 | } else { |
Josef Bacik | eb838e7 | 2012-07-31 16:28:48 -0400 | [diff] [blame] | 7543 | free_extent_state(cached_state); |
Liu Bo | 24c03fa | 2012-08-22 20:10:38 -0600 | [diff] [blame] | 7544 | } |
Josef Bacik | eb838e7 | 2012-07-31 16:28:48 -0400 | [diff] [blame] | 7545 | |
Josef Bacik | 4b46fce | 2010-05-23 11:00:55 -0400 | [diff] [blame] | 7546 | free_extent_map(em); |
| 7547 | |
| 7548 | return 0; |
Josef Bacik | eb838e7 | 2012-07-31 16:28:48 -0400 | [diff] [blame] | 7549 | |
| 7550 | unlock_err: |
Josef Bacik | eb838e7 | 2012-07-31 16:28:48 -0400 | [diff] [blame] | 7551 | clear_extent_bit(&BTRFS_I(inode)->io_tree, lockstart, lockend, |
| 7552 | unlock_bits, 1, 0, &cached_state, GFP_NOFS); |
Josef Bacik | e1cbbfa | 2015-03-17 10:52:28 -0400 | [diff] [blame] | 7553 | if (outstanding_extents) |
| 7554 | current->journal_info = outstanding_extents; |
Josef Bacik | eb838e7 | 2012-07-31 16:28:48 -0400 | [diff] [blame] | 7555 | return ret; |
Josef Bacik | 4b46fce | 2010-05-23 11:00:55 -0400 | [diff] [blame] | 7556 | } |
| 7557 | |
Miao Xie | 8b110e3 | 2014-09-12 18:44:03 +0800 | [diff] [blame] | 7558 | static inline int submit_dio_repair_bio(struct inode *inode, struct bio *bio, |
| 7559 | int rw, int mirror_num) |
| 7560 | { |
| 7561 | struct btrfs_root *root = BTRFS_I(inode)->root; |
| 7562 | int ret; |
| 7563 | |
| 7564 | BUG_ON(rw & REQ_WRITE); |
| 7565 | |
| 7566 | bio_get(bio); |
| 7567 | |
| 7568 | ret = btrfs_bio_wq_end_io(root->fs_info, bio, |
| 7569 | BTRFS_WQ_ENDIO_DIO_REPAIR); |
| 7570 | if (ret) |
| 7571 | goto err; |
| 7572 | |
| 7573 | ret = btrfs_map_bio(root, rw, bio, mirror_num, 0); |
| 7574 | err: |
| 7575 | bio_put(bio); |
| 7576 | return ret; |
| 7577 | } |
| 7578 | |
| 7579 | static int btrfs_check_dio_repairable(struct inode *inode, |
| 7580 | struct bio *failed_bio, |
| 7581 | struct io_failure_record *failrec, |
| 7582 | int failed_mirror) |
| 7583 | { |
| 7584 | int num_copies; |
| 7585 | |
| 7586 | num_copies = btrfs_num_copies(BTRFS_I(inode)->root->fs_info, |
| 7587 | failrec->logical, failrec->len); |
| 7588 | if (num_copies == 1) { |
| 7589 | /* |
| 7590 | * we only have a single copy of the data, so don't bother with |
| 7591 | * all the retry and error correction code that follows. no |
| 7592 | * matter what the error is, it is very likely to persist. |
| 7593 | */ |
| 7594 | pr_debug("Check DIO Repairable: cannot repair, num_copies=%d, next_mirror %d, failed_mirror %d\n", |
| 7595 | num_copies, failrec->this_mirror, failed_mirror); |
| 7596 | return 0; |
| 7597 | } |
| 7598 | |
| 7599 | failrec->failed_mirror = failed_mirror; |
| 7600 | failrec->this_mirror++; |
| 7601 | if (failrec->this_mirror == failed_mirror) |
| 7602 | failrec->this_mirror++; |
| 7603 | |
| 7604 | if (failrec->this_mirror > num_copies) { |
| 7605 | pr_debug("Check DIO Repairable: (fail) num_copies=%d, next_mirror %d, failed_mirror %d\n", |
| 7606 | num_copies, failrec->this_mirror, failed_mirror); |
| 7607 | return 0; |
| 7608 | } |
| 7609 | |
| 7610 | return 1; |
| 7611 | } |
| 7612 | |
| 7613 | static int dio_read_error(struct inode *inode, struct bio *failed_bio, |
| 7614 | struct page *page, u64 start, u64 end, |
| 7615 | int failed_mirror, bio_end_io_t *repair_endio, |
| 7616 | void *repair_arg) |
| 7617 | { |
| 7618 | struct io_failure_record *failrec; |
| 7619 | struct bio *bio; |
| 7620 | int isector; |
| 7621 | int read_mode; |
| 7622 | int ret; |
| 7623 | |
| 7624 | BUG_ON(failed_bio->bi_rw & REQ_WRITE); |
| 7625 | |
| 7626 | ret = btrfs_get_io_failure_record(inode, start, end, &failrec); |
| 7627 | if (ret) |
| 7628 | return ret; |
| 7629 | |
| 7630 | ret = btrfs_check_dio_repairable(inode, failed_bio, failrec, |
| 7631 | failed_mirror); |
| 7632 | if (!ret) { |
| 7633 | free_io_failure(inode, failrec); |
| 7634 | return -EIO; |
| 7635 | } |
| 7636 | |
| 7637 | if (failed_bio->bi_vcnt > 1) |
| 7638 | read_mode = READ_SYNC | REQ_FAILFAST_DEV; |
| 7639 | else |
| 7640 | read_mode = READ_SYNC; |
| 7641 | |
| 7642 | isector = start - btrfs_io_bio(failed_bio)->logical; |
| 7643 | isector >>= inode->i_sb->s_blocksize_bits; |
| 7644 | bio = btrfs_create_repair_bio(inode, failed_bio, failrec, page, |
| 7645 | 0, isector, repair_endio, repair_arg); |
| 7646 | if (!bio) { |
| 7647 | free_io_failure(inode, failrec); |
| 7648 | return -EIO; |
| 7649 | } |
| 7650 | |
| 7651 | btrfs_debug(BTRFS_I(inode)->root->fs_info, |
| 7652 | "Repair DIO Read Error: submitting new dio read[%#x] to this_mirror=%d, in_validation=%d\n", |
| 7653 | read_mode, failrec->this_mirror, failrec->in_validation); |
| 7654 | |
| 7655 | ret = submit_dio_repair_bio(inode, bio, read_mode, |
| 7656 | failrec->this_mirror); |
| 7657 | if (ret) { |
| 7658 | free_io_failure(inode, failrec); |
| 7659 | bio_put(bio); |
| 7660 | } |
| 7661 | |
| 7662 | return ret; |
| 7663 | } |
| 7664 | |
| 7665 | struct btrfs_retry_complete { |
| 7666 | struct completion done; |
| 7667 | struct inode *inode; |
| 7668 | u64 start; |
| 7669 | int uptodate; |
| 7670 | }; |
| 7671 | |
| 7672 | static void btrfs_retry_endio_nocsum(struct bio *bio, int err) |
| 7673 | { |
| 7674 | struct btrfs_retry_complete *done = bio->bi_private; |
| 7675 | struct bio_vec *bvec; |
| 7676 | int i; |
| 7677 | |
| 7678 | if (err) |
| 7679 | goto end; |
| 7680 | |
| 7681 | done->uptodate = 1; |
| 7682 | bio_for_each_segment_all(bvec, bio, i) |
| 7683 | clean_io_failure(done->inode, done->start, bvec->bv_page, 0); |
| 7684 | end: |
| 7685 | complete(&done->done); |
| 7686 | bio_put(bio); |
| 7687 | } |
| 7688 | |
| 7689 | static int __btrfs_correct_data_nocsum(struct inode *inode, |
| 7690 | struct btrfs_io_bio *io_bio) |
Josef Bacik | 4b46fce | 2010-05-23 11:00:55 -0400 | [diff] [blame] | 7691 | { |
Kent Overstreet | 2c30c71 | 2013-11-07 12:20:26 -0800 | [diff] [blame] | 7692 | struct bio_vec *bvec; |
Miao Xie | 8b110e3 | 2014-09-12 18:44:03 +0800 | [diff] [blame] | 7693 | struct btrfs_retry_complete done; |
Josef Bacik | 4b46fce | 2010-05-23 11:00:55 -0400 | [diff] [blame] | 7694 | u64 start; |
Kent Overstreet | 2c30c71 | 2013-11-07 12:20:26 -0800 | [diff] [blame] | 7695 | int i; |
Miao Xie | c1dc089 | 2014-09-12 18:43:56 +0800 | [diff] [blame] | 7696 | int ret; |
Miao Xie | dc380ae | 2014-09-12 18:43:55 +0800 | [diff] [blame] | 7697 | |
Miao Xie | c1dc089 | 2014-09-12 18:43:56 +0800 | [diff] [blame] | 7698 | start = io_bio->logical; |
Miao Xie | 8b110e3 | 2014-09-12 18:44:03 +0800 | [diff] [blame] | 7699 | done.inode = inode; |
| 7700 | |
| 7701 | bio_for_each_segment_all(bvec, &io_bio->bio, i) { |
| 7702 | try_again: |
| 7703 | done.uptodate = 0; |
| 7704 | done.start = start; |
| 7705 | init_completion(&done.done); |
| 7706 | |
| 7707 | ret = dio_read_error(inode, &io_bio->bio, bvec->bv_page, start, |
| 7708 | start + bvec->bv_len - 1, |
| 7709 | io_bio->mirror_num, |
| 7710 | btrfs_retry_endio_nocsum, &done); |
| 7711 | if (ret) |
| 7712 | return ret; |
| 7713 | |
| 7714 | wait_for_completion(&done.done); |
| 7715 | |
| 7716 | if (!done.uptodate) { |
| 7717 | /* We might have another mirror, so try again */ |
| 7718 | goto try_again; |
| 7719 | } |
| 7720 | |
| 7721 | start += bvec->bv_len; |
| 7722 | } |
| 7723 | |
| 7724 | return 0; |
| 7725 | } |
| 7726 | |
| 7727 | static void btrfs_retry_endio(struct bio *bio, int err) |
| 7728 | { |
| 7729 | struct btrfs_retry_complete *done = bio->bi_private; |
| 7730 | struct btrfs_io_bio *io_bio = btrfs_io_bio(bio); |
| 7731 | struct bio_vec *bvec; |
| 7732 | int uptodate; |
| 7733 | int ret; |
| 7734 | int i; |
| 7735 | |
| 7736 | if (err) |
| 7737 | goto end; |
| 7738 | |
| 7739 | uptodate = 1; |
| 7740 | bio_for_each_segment_all(bvec, bio, i) { |
| 7741 | ret = __readpage_endio_check(done->inode, io_bio, i, |
| 7742 | bvec->bv_page, 0, |
| 7743 | done->start, bvec->bv_len); |
| 7744 | if (!ret) |
| 7745 | clean_io_failure(done->inode, done->start, |
| 7746 | bvec->bv_page, 0); |
| 7747 | else |
| 7748 | uptodate = 0; |
| 7749 | } |
| 7750 | |
| 7751 | done->uptodate = uptodate; |
| 7752 | end: |
| 7753 | complete(&done->done); |
| 7754 | bio_put(bio); |
| 7755 | } |
| 7756 | |
| 7757 | static int __btrfs_subio_endio_read(struct inode *inode, |
| 7758 | struct btrfs_io_bio *io_bio, int err) |
| 7759 | { |
| 7760 | struct bio_vec *bvec; |
| 7761 | struct btrfs_retry_complete done; |
| 7762 | u64 start; |
| 7763 | u64 offset = 0; |
| 7764 | int i; |
| 7765 | int ret; |
| 7766 | |
| 7767 | err = 0; |
| 7768 | start = io_bio->logical; |
| 7769 | done.inode = inode; |
| 7770 | |
Miao Xie | c1dc089 | 2014-09-12 18:43:56 +0800 | [diff] [blame] | 7771 | bio_for_each_segment_all(bvec, &io_bio->bio, i) { |
Miao Xie | dc380ae | 2014-09-12 18:43:55 +0800 | [diff] [blame] | 7772 | ret = __readpage_endio_check(inode, io_bio, i, bvec->bv_page, |
| 7773 | 0, start, bvec->bv_len); |
Miao Xie | 8b110e3 | 2014-09-12 18:44:03 +0800 | [diff] [blame] | 7774 | if (likely(!ret)) |
| 7775 | goto next; |
| 7776 | try_again: |
| 7777 | done.uptodate = 0; |
| 7778 | done.start = start; |
| 7779 | init_completion(&done.done); |
| 7780 | |
| 7781 | ret = dio_read_error(inode, &io_bio->bio, bvec->bv_page, start, |
| 7782 | start + bvec->bv_len - 1, |
| 7783 | io_bio->mirror_num, |
| 7784 | btrfs_retry_endio, &done); |
| 7785 | if (ret) { |
| 7786 | err = ret; |
| 7787 | goto next; |
| 7788 | } |
| 7789 | |
| 7790 | wait_for_completion(&done.done); |
| 7791 | |
| 7792 | if (!done.uptodate) { |
| 7793 | /* We might have another mirror, so try again */ |
| 7794 | goto try_again; |
| 7795 | } |
| 7796 | next: |
| 7797 | offset += bvec->bv_len; |
Josef Bacik | 4b46fce | 2010-05-23 11:00:55 -0400 | [diff] [blame] | 7798 | start += bvec->bv_len; |
Kent Overstreet | 2c30c71 | 2013-11-07 12:20:26 -0800 | [diff] [blame] | 7799 | } |
Miao Xie | c1dc089 | 2014-09-12 18:43:56 +0800 | [diff] [blame] | 7800 | |
| 7801 | return err; |
| 7802 | } |
| 7803 | |
Miao Xie | 8b110e3 | 2014-09-12 18:44:03 +0800 | [diff] [blame] | 7804 | static int btrfs_subio_endio_read(struct inode *inode, |
| 7805 | struct btrfs_io_bio *io_bio, int err) |
| 7806 | { |
| 7807 | bool skip_csum = BTRFS_I(inode)->flags & BTRFS_INODE_NODATASUM; |
| 7808 | |
| 7809 | if (skip_csum) { |
| 7810 | if (unlikely(err)) |
| 7811 | return __btrfs_correct_data_nocsum(inode, io_bio); |
| 7812 | else |
| 7813 | return 0; |
| 7814 | } else { |
| 7815 | return __btrfs_subio_endio_read(inode, io_bio, err); |
| 7816 | } |
| 7817 | } |
| 7818 | |
Miao Xie | c1dc089 | 2014-09-12 18:43:56 +0800 | [diff] [blame] | 7819 | static void btrfs_endio_direct_read(struct bio *bio, int err) |
| 7820 | { |
| 7821 | struct btrfs_dio_private *dip = bio->bi_private; |
| 7822 | struct inode *inode = dip->inode; |
| 7823 | struct bio *dio_bio; |
| 7824 | struct btrfs_io_bio *io_bio = btrfs_io_bio(bio); |
| 7825 | |
Miao Xie | 8b110e3 | 2014-09-12 18:44:03 +0800 | [diff] [blame] | 7826 | if (dip->flags & BTRFS_DIO_ORIG_BIO_SUBMITTED) |
| 7827 | err = btrfs_subio_endio_read(inode, io_bio, err); |
Miao Xie | c1dc089 | 2014-09-12 18:43:56 +0800 | [diff] [blame] | 7828 | |
Josef Bacik | 4b46fce | 2010-05-23 11:00:55 -0400 | [diff] [blame] | 7829 | unlock_extent(&BTRFS_I(inode)->io_tree, dip->logical_offset, |
Jeff Mahoney | d008237 | 2012-03-01 14:57:19 +0100 | [diff] [blame] | 7830 | dip->logical_offset + dip->bytes - 1); |
Chris Mason | 9be3395 | 2013-05-17 18:30:14 -0400 | [diff] [blame] | 7831 | dio_bio = dip->dio_bio; |
Josef Bacik | 4b46fce | 2010-05-23 11:00:55 -0400 | [diff] [blame] | 7832 | |
Josef Bacik | 4b46fce | 2010-05-23 11:00:55 -0400 | [diff] [blame] | 7833 | kfree(dip); |
Josef Bacik | c0da7aa | 2011-03-22 11:05:07 -0400 | [diff] [blame] | 7834 | |
| 7835 | /* If we had a csum failure make sure to clear the uptodate flag */ |
| 7836 | if (err) |
Chris Mason | 9be3395 | 2013-05-17 18:30:14 -0400 | [diff] [blame] | 7837 | clear_bit(BIO_UPTODATE, &dio_bio->bi_flags); |
| 7838 | dio_end_io(dio_bio, err); |
Miao Xie | 23ea8e5 | 2014-09-12 18:43:54 +0800 | [diff] [blame] | 7839 | |
| 7840 | if (io_bio->end_io) |
| 7841 | io_bio->end_io(io_bio, err); |
Chris Mason | 9be3395 | 2013-05-17 18:30:14 -0400 | [diff] [blame] | 7842 | bio_put(bio); |
Josef Bacik | 4b46fce | 2010-05-23 11:00:55 -0400 | [diff] [blame] | 7843 | } |
| 7844 | |
| 7845 | static void btrfs_endio_direct_write(struct bio *bio, int err) |
| 7846 | { |
| 7847 | struct btrfs_dio_private *dip = bio->bi_private; |
| 7848 | struct inode *inode = dip->inode; |
| 7849 | struct btrfs_root *root = BTRFS_I(inode)->root; |
Josef Bacik | 4b46fce | 2010-05-23 11:00:55 -0400 | [diff] [blame] | 7850 | struct btrfs_ordered_extent *ordered = NULL; |
Chris Mason | 163cf09 | 2010-11-28 19:56:33 -0500 | [diff] [blame] | 7851 | u64 ordered_offset = dip->logical_offset; |
| 7852 | u64 ordered_bytes = dip->bytes; |
Chris Mason | 9be3395 | 2013-05-17 18:30:14 -0400 | [diff] [blame] | 7853 | struct bio *dio_bio; |
Josef Bacik | 4b46fce | 2010-05-23 11:00:55 -0400 | [diff] [blame] | 7854 | int ret; |
| 7855 | |
| 7856 | if (err) |
| 7857 | goto out_done; |
Chris Mason | 163cf09 | 2010-11-28 19:56:33 -0500 | [diff] [blame] | 7858 | again: |
| 7859 | ret = btrfs_dec_test_first_ordered_pending(inode, &ordered, |
| 7860 | &ordered_offset, |
Josef Bacik | 5fd0204 | 2012-05-02 14:00:54 -0400 | [diff] [blame] | 7861 | ordered_bytes, !err); |
Josef Bacik | 4b46fce | 2010-05-23 11:00:55 -0400 | [diff] [blame] | 7862 | if (!ret) |
Chris Mason | 163cf09 | 2010-11-28 19:56:33 -0500 | [diff] [blame] | 7863 | goto out_test; |
Josef Bacik | 4b46fce | 2010-05-23 11:00:55 -0400 | [diff] [blame] | 7864 | |
Liu Bo | 9e0af23 | 2014-08-15 23:36:53 +0800 | [diff] [blame] | 7865 | btrfs_init_work(&ordered->work, btrfs_endio_write_helper, |
| 7866 | finish_ordered_fn, NULL, NULL); |
Qu Wenruo | fccb5d8 | 2014-02-28 10:46:10 +0800 | [diff] [blame] | 7867 | btrfs_queue_work(root->fs_info->endio_write_workers, |
| 7868 | &ordered->work); |
Chris Mason | 163cf09 | 2010-11-28 19:56:33 -0500 | [diff] [blame] | 7869 | out_test: |
| 7870 | /* |
| 7871 | * our bio might span multiple ordered extents. If we haven't |
| 7872 | * completed the accounting for the whole dio, go back and try again |
| 7873 | */ |
| 7874 | if (ordered_offset < dip->logical_offset + dip->bytes) { |
| 7875 | ordered_bytes = dip->logical_offset + dip->bytes - |
| 7876 | ordered_offset; |
Josef Bacik | 5fd0204 | 2012-05-02 14:00:54 -0400 | [diff] [blame] | 7877 | ordered = NULL; |
Chris Mason | 163cf09 | 2010-11-28 19:56:33 -0500 | [diff] [blame] | 7878 | goto again; |
| 7879 | } |
Josef Bacik | 4b46fce | 2010-05-23 11:00:55 -0400 | [diff] [blame] | 7880 | out_done: |
Chris Mason | 9be3395 | 2013-05-17 18:30:14 -0400 | [diff] [blame] | 7881 | dio_bio = dip->dio_bio; |
Josef Bacik | 4b46fce | 2010-05-23 11:00:55 -0400 | [diff] [blame] | 7882 | |
Josef Bacik | 4b46fce | 2010-05-23 11:00:55 -0400 | [diff] [blame] | 7883 | kfree(dip); |
Josef Bacik | c0da7aa | 2011-03-22 11:05:07 -0400 | [diff] [blame] | 7884 | |
| 7885 | /* If we had an error make sure to clear the uptodate flag */ |
| 7886 | if (err) |
Chris Mason | 9be3395 | 2013-05-17 18:30:14 -0400 | [diff] [blame] | 7887 | clear_bit(BIO_UPTODATE, &dio_bio->bi_flags); |
| 7888 | dio_end_io(dio_bio, err); |
| 7889 | bio_put(bio); |
Josef Bacik | 4b46fce | 2010-05-23 11:00:55 -0400 | [diff] [blame] | 7890 | } |
| 7891 | |
Chris Mason | eaf25d9 | 2010-05-25 09:48:28 -0400 | [diff] [blame] | 7892 | static int __btrfs_submit_bio_start_direct_io(struct inode *inode, int rw, |
| 7893 | struct bio *bio, int mirror_num, |
| 7894 | unsigned long bio_flags, u64 offset) |
| 7895 | { |
| 7896 | int ret; |
| 7897 | struct btrfs_root *root = BTRFS_I(inode)->root; |
| 7898 | ret = btrfs_csum_one_bio(root, inode, bio, offset, 1); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 7899 | BUG_ON(ret); /* -ENOMEM */ |
Chris Mason | eaf25d9 | 2010-05-25 09:48:28 -0400 | [diff] [blame] | 7900 | return 0; |
| 7901 | } |
| 7902 | |
Miao Xie | e65e153 | 2010-11-22 03:04:43 +0000 | [diff] [blame] | 7903 | static void btrfs_end_dio_bio(struct bio *bio, int err) |
| 7904 | { |
| 7905 | struct btrfs_dio_private *dip = bio->bi_private; |
| 7906 | |
Miao Xie | 8b110e3 | 2014-09-12 18:44:03 +0800 | [diff] [blame] | 7907 | if (err) |
| 7908 | btrfs_warn(BTRFS_I(dip->inode)->root->fs_info, |
| 7909 | "direct IO failed ino %llu rw %lu sector %#Lx len %u err no %d", |
| 7910 | btrfs_ino(dip->inode), bio->bi_rw, |
| 7911 | (unsigned long long)bio->bi_iter.bi_sector, |
| 7912 | bio->bi_iter.bi_size, err); |
| 7913 | |
| 7914 | if (dip->subio_endio) |
| 7915 | err = dip->subio_endio(dip->inode, btrfs_io_bio(bio), err); |
Miao Xie | c1dc089 | 2014-09-12 18:43:56 +0800 | [diff] [blame] | 7916 | |
| 7917 | if (err) { |
Miao Xie | e65e153 | 2010-11-22 03:04:43 +0000 | [diff] [blame] | 7918 | dip->errors = 1; |
| 7919 | |
| 7920 | /* |
| 7921 | * before atomic variable goto zero, we must make sure |
| 7922 | * dip->errors is perceived to be set. |
| 7923 | */ |
Peter Zijlstra | 4e857c5 | 2014-03-17 18:06:10 +0100 | [diff] [blame] | 7924 | smp_mb__before_atomic(); |
Miao Xie | e65e153 | 2010-11-22 03:04:43 +0000 | [diff] [blame] | 7925 | } |
| 7926 | |
| 7927 | /* if there are more bios still pending for this dio, just exit */ |
| 7928 | if (!atomic_dec_and_test(&dip->pending_bios)) |
| 7929 | goto out; |
| 7930 | |
Chris Mason | 9be3395 | 2013-05-17 18:30:14 -0400 | [diff] [blame] | 7931 | if (dip->errors) { |
Miao Xie | e65e153 | 2010-11-22 03:04:43 +0000 | [diff] [blame] | 7932 | bio_io_error(dip->orig_bio); |
Chris Mason | 9be3395 | 2013-05-17 18:30:14 -0400 | [diff] [blame] | 7933 | } else { |
| 7934 | set_bit(BIO_UPTODATE, &dip->dio_bio->bi_flags); |
Miao Xie | e65e153 | 2010-11-22 03:04:43 +0000 | [diff] [blame] | 7935 | bio_endio(dip->orig_bio, 0); |
| 7936 | } |
| 7937 | out: |
| 7938 | bio_put(bio); |
| 7939 | } |
| 7940 | |
| 7941 | static struct bio *btrfs_dio_bio_alloc(struct block_device *bdev, |
| 7942 | u64 first_sector, gfp_t gfp_flags) |
| 7943 | { |
| 7944 | int nr_vecs = bio_get_nr_vecs(bdev); |
| 7945 | return btrfs_bio_alloc(bdev, first_sector, nr_vecs, gfp_flags); |
| 7946 | } |
| 7947 | |
Miao Xie | c1dc089 | 2014-09-12 18:43:56 +0800 | [diff] [blame] | 7948 | static inline int btrfs_lookup_and_bind_dio_csum(struct btrfs_root *root, |
| 7949 | struct inode *inode, |
| 7950 | struct btrfs_dio_private *dip, |
| 7951 | struct bio *bio, |
| 7952 | u64 file_offset) |
| 7953 | { |
| 7954 | struct btrfs_io_bio *io_bio = btrfs_io_bio(bio); |
| 7955 | struct btrfs_io_bio *orig_io_bio = btrfs_io_bio(dip->orig_bio); |
| 7956 | int ret; |
| 7957 | |
| 7958 | /* |
| 7959 | * We load all the csum data we need when we submit |
| 7960 | * the first bio to reduce the csum tree search and |
| 7961 | * contention. |
| 7962 | */ |
| 7963 | if (dip->logical_offset == file_offset) { |
| 7964 | ret = btrfs_lookup_bio_sums_dio(root, inode, dip->orig_bio, |
| 7965 | file_offset); |
| 7966 | if (ret) |
| 7967 | return ret; |
| 7968 | } |
| 7969 | |
| 7970 | if (bio == dip->orig_bio) |
| 7971 | return 0; |
| 7972 | |
| 7973 | file_offset -= dip->logical_offset; |
| 7974 | file_offset >>= inode->i_sb->s_blocksize_bits; |
| 7975 | io_bio->csum = (u8 *)(((u32 *)orig_io_bio->csum) + file_offset); |
| 7976 | |
| 7977 | return 0; |
| 7978 | } |
| 7979 | |
Miao Xie | e65e153 | 2010-11-22 03:04:43 +0000 | [diff] [blame] | 7980 | static inline int __btrfs_submit_dio_bio(struct bio *bio, struct inode *inode, |
| 7981 | int rw, u64 file_offset, int skip_sum, |
Josef Bacik | c329861 | 2012-08-03 16:49:19 -0400 | [diff] [blame] | 7982 | int async_submit) |
Miao Xie | e65e153 | 2010-11-22 03:04:43 +0000 | [diff] [blame] | 7983 | { |
Miao Xie | facc8a22 | 2013-07-25 19:22:34 +0800 | [diff] [blame] | 7984 | struct btrfs_dio_private *dip = bio->bi_private; |
Miao Xie | e65e153 | 2010-11-22 03:04:43 +0000 | [diff] [blame] | 7985 | int write = rw & REQ_WRITE; |
| 7986 | struct btrfs_root *root = BTRFS_I(inode)->root; |
| 7987 | int ret; |
| 7988 | |
Josef Bacik | b812ce2 | 2012-11-16 13:56:32 -0500 | [diff] [blame] | 7989 | if (async_submit) |
| 7990 | async_submit = !atomic_read(&BTRFS_I(inode)->sync_writers); |
| 7991 | |
Miao Xie | e65e153 | 2010-11-22 03:04:43 +0000 | [diff] [blame] | 7992 | bio_get(bio); |
Josef Bacik | 5fd0204 | 2012-05-02 14:00:54 -0400 | [diff] [blame] | 7993 | |
| 7994 | if (!write) { |
David Sterba | bfebd8b | 2014-07-30 00:25:45 +0200 | [diff] [blame] | 7995 | ret = btrfs_bio_wq_end_io(root->fs_info, bio, |
| 7996 | BTRFS_WQ_ENDIO_DATA); |
Josef Bacik | 5fd0204 | 2012-05-02 14:00:54 -0400 | [diff] [blame] | 7997 | if (ret) |
| 7998 | goto err; |
| 7999 | } |
Miao Xie | e65e153 | 2010-11-22 03:04:43 +0000 | [diff] [blame] | 8000 | |
Josef Bacik | 1ae3993 | 2011-04-06 14:41:34 -0400 | [diff] [blame] | 8001 | if (skip_sum) |
| 8002 | goto map; |
| 8003 | |
| 8004 | if (write && async_submit) { |
Miao Xie | e65e153 | 2010-11-22 03:04:43 +0000 | [diff] [blame] | 8005 | ret = btrfs_wq_submit_bio(root->fs_info, |
| 8006 | inode, rw, bio, 0, 0, |
| 8007 | file_offset, |
| 8008 | __btrfs_submit_bio_start_direct_io, |
| 8009 | __btrfs_submit_bio_done); |
| 8010 | goto err; |
Josef Bacik | 1ae3993 | 2011-04-06 14:41:34 -0400 | [diff] [blame] | 8011 | } else if (write) { |
| 8012 | /* |
| 8013 | * If we aren't doing async submit, calculate the csum of the |
| 8014 | * bio now. |
| 8015 | */ |
| 8016 | ret = btrfs_csum_one_bio(root, inode, bio, file_offset, 1); |
| 8017 | if (ret) |
| 8018 | goto err; |
Miao Xie | 23ea8e5 | 2014-09-12 18:43:54 +0800 | [diff] [blame] | 8019 | } else { |
Miao Xie | c1dc089 | 2014-09-12 18:43:56 +0800 | [diff] [blame] | 8020 | ret = btrfs_lookup_and_bind_dio_csum(root, inode, dip, bio, |
| 8021 | file_offset); |
Tsutomu Itoh | c2db107 | 2011-03-01 06:48:31 +0000 | [diff] [blame] | 8022 | if (ret) |
| 8023 | goto err; |
| 8024 | } |
Josef Bacik | 1ae3993 | 2011-04-06 14:41:34 -0400 | [diff] [blame] | 8025 | map: |
| 8026 | ret = btrfs_map_bio(root, rw, bio, 0, async_submit); |
Miao Xie | e65e153 | 2010-11-22 03:04:43 +0000 | [diff] [blame] | 8027 | err: |
| 8028 | bio_put(bio); |
| 8029 | return ret; |
| 8030 | } |
| 8031 | |
| 8032 | static int btrfs_submit_direct_hook(int rw, struct btrfs_dio_private *dip, |
| 8033 | int skip_sum) |
| 8034 | { |
| 8035 | struct inode *inode = dip->inode; |
| 8036 | struct btrfs_root *root = BTRFS_I(inode)->root; |
Miao Xie | e65e153 | 2010-11-22 03:04:43 +0000 | [diff] [blame] | 8037 | struct bio *bio; |
| 8038 | struct bio *orig_bio = dip->orig_bio; |
| 8039 | struct bio_vec *bvec = orig_bio->bi_io_vec; |
Kent Overstreet | 4f024f3 | 2013-10-11 15:44:27 -0700 | [diff] [blame] | 8040 | u64 start_sector = orig_bio->bi_iter.bi_sector; |
Miao Xie | e65e153 | 2010-11-22 03:04:43 +0000 | [diff] [blame] | 8041 | u64 file_offset = dip->logical_offset; |
| 8042 | u64 submit_len = 0; |
| 8043 | u64 map_length; |
| 8044 | int nr_pages = 0; |
Miao Xie | 23ea8e5 | 2014-09-12 18:43:54 +0800 | [diff] [blame] | 8045 | int ret; |
Josef Bacik | 1ae3993 | 2011-04-06 14:41:34 -0400 | [diff] [blame] | 8046 | int async_submit = 0; |
Miao Xie | e65e153 | 2010-11-22 03:04:43 +0000 | [diff] [blame] | 8047 | |
Kent Overstreet | 4f024f3 | 2013-10-11 15:44:27 -0700 | [diff] [blame] | 8048 | map_length = orig_bio->bi_iter.bi_size; |
David Woodhouse | 53b381b | 2013-01-29 18:40:14 -0500 | [diff] [blame] | 8049 | ret = btrfs_map_block(root->fs_info, rw, start_sector << 9, |
Miao Xie | e65e153 | 2010-11-22 03:04:43 +0000 | [diff] [blame] | 8050 | &map_length, NULL, 0); |
Miao Xie | 7a5c3c9 | 2014-06-17 18:58:59 +0800 | [diff] [blame] | 8051 | if (ret) |
Miao Xie | e65e153 | 2010-11-22 03:04:43 +0000 | [diff] [blame] | 8052 | return -EIO; |
Miao Xie | facc8a22 | 2013-07-25 19:22:34 +0800 | [diff] [blame] | 8053 | |
Kent Overstreet | 4f024f3 | 2013-10-11 15:44:27 -0700 | [diff] [blame] | 8054 | if (map_length >= orig_bio->bi_iter.bi_size) { |
Josef Bacik | 02f57c7 | 2011-04-06 14:25:44 -0400 | [diff] [blame] | 8055 | bio = orig_bio; |
Miao Xie | c1dc089 | 2014-09-12 18:43:56 +0800 | [diff] [blame] | 8056 | dip->flags |= BTRFS_DIO_ORIG_BIO_SUBMITTED; |
Josef Bacik | 02f57c7 | 2011-04-06 14:25:44 -0400 | [diff] [blame] | 8057 | goto submit; |
| 8058 | } |
| 8059 | |
David Woodhouse | 53b381b | 2013-01-29 18:40:14 -0500 | [diff] [blame] | 8060 | /* async crcs make it difficult to collect full stripe writes. */ |
Zhao Lei | ffe2d20 | 2015-01-20 15:11:44 +0800 | [diff] [blame] | 8061 | if (btrfs_get_alloc_profile(root, 1) & BTRFS_BLOCK_GROUP_RAID56_MASK) |
David Woodhouse | 53b381b | 2013-01-29 18:40:14 -0500 | [diff] [blame] | 8062 | async_submit = 0; |
| 8063 | else |
| 8064 | async_submit = 1; |
| 8065 | |
Josef Bacik | 02f57c7 | 2011-04-06 14:25:44 -0400 | [diff] [blame] | 8066 | bio = btrfs_dio_bio_alloc(orig_bio->bi_bdev, start_sector, GFP_NOFS); |
| 8067 | if (!bio) |
| 8068 | return -ENOMEM; |
Miao Xie | 7a5c3c9 | 2014-06-17 18:58:59 +0800 | [diff] [blame] | 8069 | |
Josef Bacik | 02f57c7 | 2011-04-06 14:25:44 -0400 | [diff] [blame] | 8070 | bio->bi_private = dip; |
| 8071 | bio->bi_end_io = btrfs_end_dio_bio; |
Miao Xie | c1dc089 | 2014-09-12 18:43:56 +0800 | [diff] [blame] | 8072 | btrfs_io_bio(bio)->logical = file_offset; |
Josef Bacik | 02f57c7 | 2011-04-06 14:25:44 -0400 | [diff] [blame] | 8073 | atomic_inc(&dip->pending_bios); |
| 8074 | |
Miao Xie | e65e153 | 2010-11-22 03:04:43 +0000 | [diff] [blame] | 8075 | while (bvec <= (orig_bio->bi_io_vec + orig_bio->bi_vcnt - 1)) { |
David Sterba | ee39b43 | 2014-09-30 01:33:33 +0200 | [diff] [blame] | 8076 | if (map_length < submit_len + bvec->bv_len || |
Miao Xie | e65e153 | 2010-11-22 03:04:43 +0000 | [diff] [blame] | 8077 | bio_add_page(bio, bvec->bv_page, bvec->bv_len, |
David Sterba | ee39b43 | 2014-09-30 01:33:33 +0200 | [diff] [blame] | 8078 | bvec->bv_offset) < bvec->bv_len) { |
Miao Xie | e65e153 | 2010-11-22 03:04:43 +0000 | [diff] [blame] | 8079 | /* |
| 8080 | * inc the count before we submit the bio so |
| 8081 | * we know the end IO handler won't happen before |
| 8082 | * we inc the count. Otherwise, the dip might get freed |
| 8083 | * before we're done setting it up |
| 8084 | */ |
| 8085 | atomic_inc(&dip->pending_bios); |
| 8086 | ret = __btrfs_submit_dio_bio(bio, inode, rw, |
| 8087 | file_offset, skip_sum, |
Josef Bacik | c329861 | 2012-08-03 16:49:19 -0400 | [diff] [blame] | 8088 | async_submit); |
Miao Xie | e65e153 | 2010-11-22 03:04:43 +0000 | [diff] [blame] | 8089 | if (ret) { |
| 8090 | bio_put(bio); |
| 8091 | atomic_dec(&dip->pending_bios); |
| 8092 | goto out_err; |
| 8093 | } |
| 8094 | |
Miao Xie | e65e153 | 2010-11-22 03:04:43 +0000 | [diff] [blame] | 8095 | start_sector += submit_len >> 9; |
| 8096 | file_offset += submit_len; |
| 8097 | |
| 8098 | submit_len = 0; |
| 8099 | nr_pages = 0; |
| 8100 | |
| 8101 | bio = btrfs_dio_bio_alloc(orig_bio->bi_bdev, |
| 8102 | start_sector, GFP_NOFS); |
| 8103 | if (!bio) |
| 8104 | goto out_err; |
| 8105 | bio->bi_private = dip; |
| 8106 | bio->bi_end_io = btrfs_end_dio_bio; |
Miao Xie | c1dc089 | 2014-09-12 18:43:56 +0800 | [diff] [blame] | 8107 | btrfs_io_bio(bio)->logical = file_offset; |
Miao Xie | e65e153 | 2010-11-22 03:04:43 +0000 | [diff] [blame] | 8108 | |
Kent Overstreet | 4f024f3 | 2013-10-11 15:44:27 -0700 | [diff] [blame] | 8109 | map_length = orig_bio->bi_iter.bi_size; |
David Woodhouse | 53b381b | 2013-01-29 18:40:14 -0500 | [diff] [blame] | 8110 | ret = btrfs_map_block(root->fs_info, rw, |
Stefan Behrens | 3ec706c | 2012-11-05 15:46:42 +0100 | [diff] [blame] | 8111 | start_sector << 9, |
Miao Xie | e65e153 | 2010-11-22 03:04:43 +0000 | [diff] [blame] | 8112 | &map_length, NULL, 0); |
| 8113 | if (ret) { |
| 8114 | bio_put(bio); |
| 8115 | goto out_err; |
| 8116 | } |
| 8117 | } else { |
| 8118 | submit_len += bvec->bv_len; |
Dulshani Gunawardhana | 6787125 | 2013-10-31 10:33:04 +0530 | [diff] [blame] | 8119 | nr_pages++; |
Miao Xie | e65e153 | 2010-11-22 03:04:43 +0000 | [diff] [blame] | 8120 | bvec++; |
| 8121 | } |
| 8122 | } |
| 8123 | |
Josef Bacik | 02f57c7 | 2011-04-06 14:25:44 -0400 | [diff] [blame] | 8124 | submit: |
Miao Xie | e65e153 | 2010-11-22 03:04:43 +0000 | [diff] [blame] | 8125 | ret = __btrfs_submit_dio_bio(bio, inode, rw, file_offset, skip_sum, |
Josef Bacik | c329861 | 2012-08-03 16:49:19 -0400 | [diff] [blame] | 8126 | async_submit); |
Miao Xie | e65e153 | 2010-11-22 03:04:43 +0000 | [diff] [blame] | 8127 | if (!ret) |
| 8128 | return 0; |
| 8129 | |
| 8130 | bio_put(bio); |
| 8131 | out_err: |
| 8132 | dip->errors = 1; |
| 8133 | /* |
| 8134 | * before atomic variable goto zero, we must |
| 8135 | * make sure dip->errors is perceived to be set. |
| 8136 | */ |
Peter Zijlstra | 4e857c5 | 2014-03-17 18:06:10 +0100 | [diff] [blame] | 8137 | smp_mb__before_atomic(); |
Miao Xie | e65e153 | 2010-11-22 03:04:43 +0000 | [diff] [blame] | 8138 | if (atomic_dec_and_test(&dip->pending_bios)) |
| 8139 | bio_io_error(dip->orig_bio); |
| 8140 | |
| 8141 | /* bio_end_io() will handle error, so we needn't return it */ |
| 8142 | return 0; |
| 8143 | } |
| 8144 | |
Chris Mason | 9be3395 | 2013-05-17 18:30:14 -0400 | [diff] [blame] | 8145 | static void btrfs_submit_direct(int rw, struct bio *dio_bio, |
| 8146 | struct inode *inode, loff_t file_offset) |
Josef Bacik | 4b46fce | 2010-05-23 11:00:55 -0400 | [diff] [blame] | 8147 | { |
| 8148 | struct btrfs_root *root = BTRFS_I(inode)->root; |
| 8149 | struct btrfs_dio_private *dip; |
Chris Mason | 9be3395 | 2013-05-17 18:30:14 -0400 | [diff] [blame] | 8150 | struct bio *io_bio; |
Miao Xie | 23ea8e5 | 2014-09-12 18:43:54 +0800 | [diff] [blame] | 8151 | struct btrfs_io_bio *btrfs_bio; |
Josef Bacik | 4b46fce | 2010-05-23 11:00:55 -0400 | [diff] [blame] | 8152 | int skip_sum; |
Christoph Hellwig | 7b6d91d | 2010-08-07 18:20:39 +0200 | [diff] [blame] | 8153 | int write = rw & REQ_WRITE; |
Josef Bacik | 4b46fce | 2010-05-23 11:00:55 -0400 | [diff] [blame] | 8154 | int ret = 0; |
| 8155 | |
| 8156 | skip_sum = BTRFS_I(inode)->flags & BTRFS_INODE_NODATASUM; |
| 8157 | |
Chris Mason | 9be3395 | 2013-05-17 18:30:14 -0400 | [diff] [blame] | 8158 | io_bio = btrfs_bio_clone(dio_bio, GFP_NOFS); |
Chris Mason | 9be3395 | 2013-05-17 18:30:14 -0400 | [diff] [blame] | 8159 | if (!io_bio) { |
Josef Bacik | 4b46fce | 2010-05-23 11:00:55 -0400 | [diff] [blame] | 8160 | ret = -ENOMEM; |
| 8161 | goto free_ordered; |
| 8162 | } |
Josef Bacik | 4b46fce | 2010-05-23 11:00:55 -0400 | [diff] [blame] | 8163 | |
Miao Xie | c1dc089 | 2014-09-12 18:43:56 +0800 | [diff] [blame] | 8164 | dip = kzalloc(sizeof(*dip), GFP_NOFS); |
Chris Mason | 9be3395 | 2013-05-17 18:30:14 -0400 | [diff] [blame] | 8165 | if (!dip) { |
| 8166 | ret = -ENOMEM; |
| 8167 | goto free_io_bio; |
| 8168 | } |
| 8169 | |
| 8170 | dip->private = dio_bio->bi_private; |
Josef Bacik | 4b46fce | 2010-05-23 11:00:55 -0400 | [diff] [blame] | 8171 | dip->inode = inode; |
| 8172 | dip->logical_offset = file_offset; |
Kent Overstreet | 4f024f3 | 2013-10-11 15:44:27 -0700 | [diff] [blame] | 8173 | dip->bytes = dio_bio->bi_iter.bi_size; |
| 8174 | dip->disk_bytenr = (u64)dio_bio->bi_iter.bi_sector << 9; |
Chris Mason | 9be3395 | 2013-05-17 18:30:14 -0400 | [diff] [blame] | 8175 | io_bio->bi_private = dip; |
Chris Mason | 9be3395 | 2013-05-17 18:30:14 -0400 | [diff] [blame] | 8176 | dip->orig_bio = io_bio; |
| 8177 | dip->dio_bio = dio_bio; |
Miao Xie | e65e153 | 2010-11-22 03:04:43 +0000 | [diff] [blame] | 8178 | atomic_set(&dip->pending_bios, 0); |
Miao Xie | c1dc089 | 2014-09-12 18:43:56 +0800 | [diff] [blame] | 8179 | btrfs_bio = btrfs_io_bio(io_bio); |
| 8180 | btrfs_bio->logical = file_offset; |
Josef Bacik | 4b46fce | 2010-05-23 11:00:55 -0400 | [diff] [blame] | 8181 | |
Miao Xie | c1dc089 | 2014-09-12 18:43:56 +0800 | [diff] [blame] | 8182 | if (write) { |
Chris Mason | 9be3395 | 2013-05-17 18:30:14 -0400 | [diff] [blame] | 8183 | io_bio->bi_end_io = btrfs_endio_direct_write; |
Miao Xie | c1dc089 | 2014-09-12 18:43:56 +0800 | [diff] [blame] | 8184 | } else { |
Chris Mason | 9be3395 | 2013-05-17 18:30:14 -0400 | [diff] [blame] | 8185 | io_bio->bi_end_io = btrfs_endio_direct_read; |
Miao Xie | c1dc089 | 2014-09-12 18:43:56 +0800 | [diff] [blame] | 8186 | dip->subio_endio = btrfs_subio_endio_read; |
| 8187 | } |
Josef Bacik | 4b46fce | 2010-05-23 11:00:55 -0400 | [diff] [blame] | 8188 | |
Miao Xie | e65e153 | 2010-11-22 03:04:43 +0000 | [diff] [blame] | 8189 | ret = btrfs_submit_direct_hook(rw, dip, skip_sum); |
| 8190 | if (!ret) |
Chris Mason | eaf25d9 | 2010-05-25 09:48:28 -0400 | [diff] [blame] | 8191 | return; |
Chris Mason | 9be3395 | 2013-05-17 18:30:14 -0400 | [diff] [blame] | 8192 | |
Miao Xie | 23ea8e5 | 2014-09-12 18:43:54 +0800 | [diff] [blame] | 8193 | if (btrfs_bio->end_io) |
| 8194 | btrfs_bio->end_io(btrfs_bio, ret); |
Chris Mason | 9be3395 | 2013-05-17 18:30:14 -0400 | [diff] [blame] | 8195 | free_io_bio: |
| 8196 | bio_put(io_bio); |
| 8197 | |
Josef Bacik | 4b46fce | 2010-05-23 11:00:55 -0400 | [diff] [blame] | 8198 | free_ordered: |
| 8199 | /* |
| 8200 | * If this is a write, we need to clean up the reserved space and kill |
| 8201 | * the ordered extent. |
| 8202 | */ |
| 8203 | if (write) { |
| 8204 | struct btrfs_ordered_extent *ordered; |
Josef Bacik | 955256f | 2010-11-19 09:41:10 -0500 | [diff] [blame] | 8205 | ordered = btrfs_lookup_ordered_extent(inode, file_offset); |
Josef Bacik | 4b46fce | 2010-05-23 11:00:55 -0400 | [diff] [blame] | 8206 | if (!test_bit(BTRFS_ORDERED_PREALLOC, &ordered->flags) && |
| 8207 | !test_bit(BTRFS_ORDERED_NOCOW, &ordered->flags)) |
| 8208 | btrfs_free_reserved_extent(root, ordered->start, |
Miao Xie | e570fd2 | 2014-06-19 10:42:50 +0800 | [diff] [blame] | 8209 | ordered->disk_len, 1); |
Josef Bacik | 4b46fce | 2010-05-23 11:00:55 -0400 | [diff] [blame] | 8210 | btrfs_put_ordered_extent(ordered); |
| 8211 | btrfs_put_ordered_extent(ordered); |
| 8212 | } |
Chris Mason | 9be3395 | 2013-05-17 18:30:14 -0400 | [diff] [blame] | 8213 | bio_endio(dio_bio, ret); |
Josef Bacik | 4b46fce | 2010-05-23 11:00:55 -0400 | [diff] [blame] | 8214 | } |
| 8215 | |
Chris Mason | 5a5f79b | 2010-05-26 21:33:37 -0400 | [diff] [blame] | 8216 | static ssize_t check_direct_IO(struct btrfs_root *root, int rw, struct kiocb *iocb, |
Al Viro | 28060d5 | 2014-03-22 05:15:17 -0400 | [diff] [blame] | 8217 | const struct iov_iter *iter, loff_t offset) |
Chris Mason | 5a5f79b | 2010-05-26 21:33:37 -0400 | [diff] [blame] | 8218 | { |
| 8219 | int seg; |
Josef Bacik | a1b75f7 | 2011-04-08 15:51:18 +0000 | [diff] [blame] | 8220 | int i; |
Chris Mason | 5a5f79b | 2010-05-26 21:33:37 -0400 | [diff] [blame] | 8221 | unsigned blocksize_mask = root->sectorsize - 1; |
| 8222 | ssize_t retval = -EINVAL; |
Chris Mason | 5a5f79b | 2010-05-26 21:33:37 -0400 | [diff] [blame] | 8223 | |
| 8224 | if (offset & blocksize_mask) |
| 8225 | goto out; |
| 8226 | |
Al Viro | 28060d5 | 2014-03-22 05:15:17 -0400 | [diff] [blame] | 8227 | if (iov_iter_alignment(iter) & blocksize_mask) |
| 8228 | goto out; |
Josef Bacik | a1b75f7 | 2011-04-08 15:51:18 +0000 | [diff] [blame] | 8229 | |
Al Viro | 28060d5 | 2014-03-22 05:15:17 -0400 | [diff] [blame] | 8230 | /* If this is a write we don't need to check anymore */ |
| 8231 | if (rw & WRITE) |
| 8232 | return 0; |
| 8233 | /* |
| 8234 | * Check to make sure we don't have duplicate iov_base's in this |
| 8235 | * iovec, if so return EINVAL, otherwise we'll get csum errors |
| 8236 | * when reading back. |
| 8237 | */ |
| 8238 | for (seg = 0; seg < iter->nr_segs; seg++) { |
| 8239 | for (i = seg + 1; i < iter->nr_segs; i++) { |
| 8240 | if (iter->iov[seg].iov_base == iter->iov[i].iov_base) |
Josef Bacik | a1b75f7 | 2011-04-08 15:51:18 +0000 | [diff] [blame] | 8241 | goto out; |
| 8242 | } |
Chris Mason | 5a5f79b | 2010-05-26 21:33:37 -0400 | [diff] [blame] | 8243 | } |
| 8244 | retval = 0; |
| 8245 | out: |
| 8246 | return retval; |
| 8247 | } |
Josef Bacik | eb838e7 | 2012-07-31 16:28:48 -0400 | [diff] [blame] | 8248 | |
Chris Mason | 1643298 | 2008-04-10 10:23:21 -0400 | [diff] [blame] | 8249 | static ssize_t btrfs_direct_IO(int rw, struct kiocb *iocb, |
Al Viro | d8d3d94 | 2014-03-04 21:27:34 -0500 | [diff] [blame] | 8250 | struct iov_iter *iter, loff_t offset) |
Chris Mason | 1643298 | 2008-04-10 10:23:21 -0400 | [diff] [blame] | 8251 | { |
Josef Bacik | 4b46fce | 2010-05-23 11:00:55 -0400 | [diff] [blame] | 8252 | struct file *file = iocb->ki_filp; |
| 8253 | struct inode *inode = file->f_mapping->host; |
Josef Bacik | e1cbbfa | 2015-03-17 10:52:28 -0400 | [diff] [blame] | 8254 | u64 outstanding_extents = 0; |
Miao Xie | 0934856 | 2013-02-07 10:12:07 +0000 | [diff] [blame] | 8255 | size_t count = 0; |
Miao Xie | 2e60a51 | 2013-02-08 07:01:08 +0000 | [diff] [blame] | 8256 | int flags = 0; |
Miao Xie | 38851cc | 2013-02-08 07:04:11 +0000 | [diff] [blame] | 8257 | bool wakeup = true; |
| 8258 | bool relock = false; |
Miao Xie | 0934856 | 2013-02-07 10:12:07 +0000 | [diff] [blame] | 8259 | ssize_t ret; |
Josef Bacik | 4b46fce | 2010-05-23 11:00:55 -0400 | [diff] [blame] | 8260 | |
Al Viro | 28060d5 | 2014-03-22 05:15:17 -0400 | [diff] [blame] | 8261 | if (check_direct_IO(BTRFS_I(inode)->root, rw, iocb, iter, offset)) |
Chris Mason | 5a5f79b | 2010-05-26 21:33:37 -0400 | [diff] [blame] | 8262 | return 0; |
Chris Mason | 5a5f79b | 2010-05-26 21:33:37 -0400 | [diff] [blame] | 8263 | |
Miao Xie | 38851cc | 2013-02-08 07:04:11 +0000 | [diff] [blame] | 8264 | atomic_inc(&inode->i_dio_count); |
Peter Zijlstra | 4e857c5 | 2014-03-17 18:06:10 +0100 | [diff] [blame] | 8265 | smp_mb__after_atomic(); |
Miao Xie | 38851cc | 2013-02-08 07:04:11 +0000 | [diff] [blame] | 8266 | |
Josef Bacik | 0e267c4 | 2013-07-02 10:38:02 -0400 | [diff] [blame] | 8267 | /* |
Miao Xie | 41bd9ca | 2014-03-06 13:54:57 +0800 | [diff] [blame] | 8268 | * The generic stuff only does filemap_write_and_wait_range, which |
| 8269 | * isn't enough if we've written compressed pages to this area, so |
| 8270 | * we need to flush the dirty pages again to make absolutely sure |
| 8271 | * that any outstanding dirty pages are on disk. |
Josef Bacik | 0e267c4 | 2013-07-02 10:38:02 -0400 | [diff] [blame] | 8272 | */ |
Al Viro | a6cbcd4 | 2014-03-04 22:38:00 -0500 | [diff] [blame] | 8273 | count = iov_iter_count(iter); |
Miao Xie | 41bd9ca | 2014-03-06 13:54:57 +0800 | [diff] [blame] | 8274 | if (test_bit(BTRFS_INODE_HAS_ASYNC_EXTENT, |
| 8275 | &BTRFS_I(inode)->runtime_flags)) |
Wang Shilong | 9a025a0 | 2014-07-17 11:44:13 +0800 | [diff] [blame] | 8276 | filemap_fdatawrite_range(inode->i_mapping, offset, |
| 8277 | offset + count - 1); |
Josef Bacik | 0e267c4 | 2013-07-02 10:38:02 -0400 | [diff] [blame] | 8278 | |
Miao Xie | 0934856 | 2013-02-07 10:12:07 +0000 | [diff] [blame] | 8279 | if (rw & WRITE) { |
Miao Xie | 38851cc | 2013-02-08 07:04:11 +0000 | [diff] [blame] | 8280 | /* |
| 8281 | * If the write DIO is beyond the EOF, we need update |
| 8282 | * the isize, but it is protected by i_mutex. So we can |
| 8283 | * not unlock the i_mutex at this case. |
| 8284 | */ |
| 8285 | if (offset + count <= inode->i_size) { |
| 8286 | mutex_unlock(&inode->i_mutex); |
| 8287 | relock = true; |
| 8288 | } |
Miao Xie | 0934856 | 2013-02-07 10:12:07 +0000 | [diff] [blame] | 8289 | ret = btrfs_delalloc_reserve_space(inode, count); |
| 8290 | if (ret) |
Miao Xie | 38851cc | 2013-02-08 07:04:11 +0000 | [diff] [blame] | 8291 | goto out; |
Josef Bacik | e1cbbfa | 2015-03-17 10:52:28 -0400 | [diff] [blame] | 8292 | outstanding_extents = div64_u64(count + |
| 8293 | BTRFS_MAX_EXTENT_SIZE - 1, |
| 8294 | BTRFS_MAX_EXTENT_SIZE); |
| 8295 | |
| 8296 | /* |
| 8297 | * We need to know how many extents we reserved so that we can |
| 8298 | * do the accounting properly if we go over the number we |
| 8299 | * originally calculated. Abuse current->journal_info for this. |
| 8300 | */ |
| 8301 | current->journal_info = &outstanding_extents; |
David Sterba | ee39b43 | 2014-09-30 01:33:33 +0200 | [diff] [blame] | 8302 | } else if (test_bit(BTRFS_INODE_READDIO_NEED_LOCK, |
| 8303 | &BTRFS_I(inode)->runtime_flags)) { |
Miao Xie | 38851cc | 2013-02-08 07:04:11 +0000 | [diff] [blame] | 8304 | inode_dio_done(inode); |
| 8305 | flags = DIO_LOCKING | DIO_SKIP_HOLES; |
| 8306 | wakeup = false; |
Miao Xie | 0934856 | 2013-02-07 10:12:07 +0000 | [diff] [blame] | 8307 | } |
| 8308 | |
| 8309 | ret = __blockdev_direct_IO(rw, iocb, inode, |
| 8310 | BTRFS_I(inode)->root->fs_info->fs_devices->latest_bdev, |
Al Viro | 31b1403 | 2014-03-05 01:33:16 -0500 | [diff] [blame] | 8311 | iter, offset, btrfs_get_blocks_direct, NULL, |
Miao Xie | 2e60a51 | 2013-02-08 07:01:08 +0000 | [diff] [blame] | 8312 | btrfs_submit_direct, flags); |
Miao Xie | 0934856 | 2013-02-07 10:12:07 +0000 | [diff] [blame] | 8313 | if (rw & WRITE) { |
Josef Bacik | e1cbbfa | 2015-03-17 10:52:28 -0400 | [diff] [blame] | 8314 | current->journal_info = NULL; |
Miao Xie | 0934856 | 2013-02-07 10:12:07 +0000 | [diff] [blame] | 8315 | if (ret < 0 && ret != -EIOCBQUEUED) |
| 8316 | btrfs_delalloc_release_space(inode, count); |
Miao Xie | 172a504 | 2013-02-21 02:48:22 -0700 | [diff] [blame] | 8317 | else if (ret >= 0 && (size_t)ret < count) |
Miao Xie | 0934856 | 2013-02-07 10:12:07 +0000 | [diff] [blame] | 8318 | btrfs_delalloc_release_space(inode, |
| 8319 | count - (size_t)ret); |
Miao Xie | 0934856 | 2013-02-07 10:12:07 +0000 | [diff] [blame] | 8320 | } |
Miao Xie | 38851cc | 2013-02-08 07:04:11 +0000 | [diff] [blame] | 8321 | out: |
Miao Xie | 2e60a51 | 2013-02-08 07:01:08 +0000 | [diff] [blame] | 8322 | if (wakeup) |
| 8323 | inode_dio_done(inode); |
Miao Xie | 38851cc | 2013-02-08 07:04:11 +0000 | [diff] [blame] | 8324 | if (relock) |
| 8325 | mutex_lock(&inode->i_mutex); |
Miao Xie | 0934856 | 2013-02-07 10:12:07 +0000 | [diff] [blame] | 8326 | |
| 8327 | return ret; |
Chris Mason | 1643298 | 2008-04-10 10:23:21 -0400 | [diff] [blame] | 8328 | } |
| 8329 | |
Tsutomu Itoh | 05dadc0 | 2012-11-29 05:08:26 +0000 | [diff] [blame] | 8330 | #define BTRFS_FIEMAP_FLAGS (FIEMAP_FLAG_SYNC) |
| 8331 | |
Yehuda Sadeh | 1506fcc | 2009-01-21 14:39:14 -0500 | [diff] [blame] | 8332 | static int btrfs_fiemap(struct inode *inode, struct fiemap_extent_info *fieinfo, |
| 8333 | __u64 start, __u64 len) |
| 8334 | { |
Tsutomu Itoh | 05dadc0 | 2012-11-29 05:08:26 +0000 | [diff] [blame] | 8335 | int ret; |
| 8336 | |
| 8337 | ret = fiemap_check_flags(fieinfo, BTRFS_FIEMAP_FLAGS); |
| 8338 | if (ret) |
| 8339 | return ret; |
| 8340 | |
Chris Mason | ec29ed5 | 2011-02-23 16:23:20 -0500 | [diff] [blame] | 8341 | return extent_fiemap(inode, fieinfo, start, len, btrfs_get_extent_fiemap); |
Yehuda Sadeh | 1506fcc | 2009-01-21 14:39:14 -0500 | [diff] [blame] | 8342 | } |
| 8343 | |
Chris Mason | 9ebefb18 | 2007-06-15 13:50:00 -0400 | [diff] [blame] | 8344 | int btrfs_readpage(struct file *file, struct page *page) |
| 8345 | { |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 8346 | struct extent_io_tree *tree; |
| 8347 | tree = &BTRFS_I(page->mapping->host)->io_tree; |
Jan Schmidt | 8ddc7d9 | 2011-06-13 20:02:58 +0200 | [diff] [blame] | 8348 | return extent_read_full_page(tree, page, btrfs_get_extent, 0); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 8349 | } |
Chris Mason | 1832a6d | 2007-12-21 16:27:21 -0500 | [diff] [blame] | 8350 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 8351 | static int btrfs_writepage(struct page *page, struct writeback_control *wbc) |
| 8352 | { |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 8353 | struct extent_io_tree *tree; |
Chris Mason | b888db2b | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 8354 | |
| 8355 | |
| 8356 | if (current->flags & PF_MEMALLOC) { |
| 8357 | redirty_page_for_writepage(wbc, page); |
| 8358 | unlock_page(page); |
| 8359 | return 0; |
| 8360 | } |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 8361 | tree = &BTRFS_I(page->mapping->host)->io_tree; |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 8362 | return extent_write_full_page(tree, page, btrfs_get_extent, wbc); |
| 8363 | } |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 8364 | |
Eric Sandeen | 48a3b63 | 2013-04-25 20:41:01 +0000 | [diff] [blame] | 8365 | static int btrfs_writepages(struct address_space *mapping, |
| 8366 | struct writeback_control *wbc) |
Chris Mason | b293f02e | 2007-11-01 19:45:34 -0400 | [diff] [blame] | 8367 | { |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 8368 | struct extent_io_tree *tree; |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 8369 | |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 8370 | tree = &BTRFS_I(mapping->host)->io_tree; |
Chris Mason | b293f02e | 2007-11-01 19:45:34 -0400 | [diff] [blame] | 8371 | return extent_writepages(tree, mapping, btrfs_get_extent, wbc); |
| 8372 | } |
| 8373 | |
Chris Mason | 3ab2fb5 | 2007-11-08 10:59:22 -0500 | [diff] [blame] | 8374 | static int |
| 8375 | btrfs_readpages(struct file *file, struct address_space *mapping, |
| 8376 | struct list_head *pages, unsigned nr_pages) |
| 8377 | { |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 8378 | struct extent_io_tree *tree; |
| 8379 | tree = &BTRFS_I(mapping->host)->io_tree; |
Chris Mason | 3ab2fb5 | 2007-11-08 10:59:22 -0500 | [diff] [blame] | 8380 | return extent_readpages(tree, mapping, pages, nr_pages, |
| 8381 | btrfs_get_extent); |
| 8382 | } |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 8383 | static int __btrfs_releasepage(struct page *page, gfp_t gfp_flags) |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 8384 | { |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 8385 | struct extent_io_tree *tree; |
| 8386 | struct extent_map_tree *map; |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 8387 | int ret; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 8388 | |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 8389 | tree = &BTRFS_I(page->mapping->host)->io_tree; |
| 8390 | map = &BTRFS_I(page->mapping->host)->extent_tree; |
Chris Mason | 70dec80 | 2008-01-29 09:59:12 -0500 | [diff] [blame] | 8391 | ret = try_release_extent_mapping(map, tree, page, gfp_flags); |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 8392 | if (ret == 1) { |
| 8393 | ClearPagePrivate(page); |
| 8394 | set_page_private(page, 0); |
| 8395 | page_cache_release(page); |
| 8396 | } |
| 8397 | return ret; |
| 8398 | } |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 8399 | |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 8400 | static int btrfs_releasepage(struct page *page, gfp_t gfp_flags) |
| 8401 | { |
Chris Mason | 98509cf | 2008-09-11 15:51:43 -0400 | [diff] [blame] | 8402 | if (PageWriteback(page) || PageDirty(page)) |
| 8403 | return 0; |
Yan Zheng | b335b00 | 2009-02-12 10:06:04 -0500 | [diff] [blame] | 8404 | return __btrfs_releasepage(page, gfp_flags & GFP_NOFS); |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 8405 | } |
| 8406 | |
Lukas Czerner | d47992f | 2013-05-21 23:17:23 -0400 | [diff] [blame] | 8407 | static void btrfs_invalidatepage(struct page *page, unsigned int offset, |
| 8408 | unsigned int length) |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 8409 | { |
Josef Bacik | 5fd0204 | 2012-05-02 14:00:54 -0400 | [diff] [blame] | 8410 | struct inode *inode = page->mapping->host; |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 8411 | struct extent_io_tree *tree; |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 8412 | struct btrfs_ordered_extent *ordered; |
Josef Bacik | 2ac55d4 | 2010-02-03 19:33:23 +0000 | [diff] [blame] | 8413 | struct extent_state *cached_state = NULL; |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 8414 | u64 page_start = page_offset(page); |
| 8415 | u64 page_end = page_start + PAGE_CACHE_SIZE - 1; |
Filipe David Borba Manana | 131e404 | 2013-11-19 22:29:35 +0000 | [diff] [blame] | 8416 | int inode_evicting = inode->i_state & I_FREEING; |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 8417 | |
Chris Mason | 8b62b72 | 2009-09-02 16:53:46 -0400 | [diff] [blame] | 8418 | /* |
| 8419 | * we have the page locked, so new writeback can't start, |
| 8420 | * and the dirty bit won't be cleared while we are here. |
| 8421 | * |
| 8422 | * Wait for IO on this page so that we can safely clear |
| 8423 | * the PagePrivate2 bit and do ordered accounting |
| 8424 | */ |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 8425 | wait_on_page_writeback(page); |
Chris Mason | 8b62b72 | 2009-09-02 16:53:46 -0400 | [diff] [blame] | 8426 | |
Josef Bacik | 5fd0204 | 2012-05-02 14:00:54 -0400 | [diff] [blame] | 8427 | tree = &BTRFS_I(inode)->io_tree; |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 8428 | if (offset) { |
| 8429 | btrfs_releasepage(page, GFP_NOFS); |
| 8430 | return; |
| 8431 | } |
Filipe David Borba Manana | 131e404 | 2013-11-19 22:29:35 +0000 | [diff] [blame] | 8432 | |
| 8433 | if (!inode_evicting) |
| 8434 | lock_extent_bits(tree, page_start, page_end, 0, &cached_state); |
| 8435 | ordered = btrfs_lookup_ordered_extent(inode, page_start); |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 8436 | if (ordered) { |
Chris Mason | eb84ae0 | 2008-07-17 13:53:27 -0400 | [diff] [blame] | 8437 | /* |
| 8438 | * IO on this page will never be started, so we need |
| 8439 | * to account for any ordered extents now |
| 8440 | */ |
Filipe David Borba Manana | 131e404 | 2013-11-19 22:29:35 +0000 | [diff] [blame] | 8441 | if (!inode_evicting) |
| 8442 | clear_extent_bit(tree, page_start, page_end, |
| 8443 | EXTENT_DIRTY | EXTENT_DELALLOC | |
| 8444 | EXTENT_LOCKED | EXTENT_DO_ACCOUNTING | |
| 8445 | EXTENT_DEFRAG, 1, 0, &cached_state, |
| 8446 | GFP_NOFS); |
Chris Mason | 8b62b72 | 2009-09-02 16:53:46 -0400 | [diff] [blame] | 8447 | /* |
| 8448 | * whoever cleared the private bit is responsible |
| 8449 | * for the finish_ordered_io |
| 8450 | */ |
Josef Bacik | 77cef2e | 2013-08-29 13:57:21 -0400 | [diff] [blame] | 8451 | if (TestClearPagePrivate2(page)) { |
| 8452 | struct btrfs_ordered_inode_tree *tree; |
| 8453 | u64 new_len; |
| 8454 | |
| 8455 | tree = &BTRFS_I(inode)->ordered_tree; |
| 8456 | |
| 8457 | spin_lock_irq(&tree->lock); |
| 8458 | set_bit(BTRFS_ORDERED_TRUNCATED, &ordered->flags); |
| 8459 | new_len = page_start - ordered->file_offset; |
| 8460 | if (new_len < ordered->truncated_len) |
| 8461 | ordered->truncated_len = new_len; |
| 8462 | spin_unlock_irq(&tree->lock); |
| 8463 | |
| 8464 | if (btrfs_dec_test_ordered_pending(inode, &ordered, |
| 8465 | page_start, |
| 8466 | PAGE_CACHE_SIZE, 1)) |
| 8467 | btrfs_finish_ordered_io(ordered); |
Chris Mason | 8b62b72 | 2009-09-02 16:53:46 -0400 | [diff] [blame] | 8468 | } |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 8469 | btrfs_put_ordered_extent(ordered); |
Filipe David Borba Manana | 131e404 | 2013-11-19 22:29:35 +0000 | [diff] [blame] | 8470 | if (!inode_evicting) { |
| 8471 | cached_state = NULL; |
| 8472 | lock_extent_bits(tree, page_start, page_end, 0, |
| 8473 | &cached_state); |
| 8474 | } |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 8475 | } |
Filipe David Borba Manana | 131e404 | 2013-11-19 22:29:35 +0000 | [diff] [blame] | 8476 | |
| 8477 | if (!inode_evicting) { |
| 8478 | clear_extent_bit(tree, page_start, page_end, |
| 8479 | EXTENT_LOCKED | EXTENT_DIRTY | |
| 8480 | EXTENT_DELALLOC | EXTENT_DO_ACCOUNTING | |
| 8481 | EXTENT_DEFRAG, 1, 1, |
| 8482 | &cached_state, GFP_NOFS); |
| 8483 | |
| 8484 | __btrfs_releasepage(page, GFP_NOFS); |
| 8485 | } |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 8486 | |
Chris Mason | 4a09675 | 2008-07-21 10:29:44 -0400 | [diff] [blame] | 8487 | ClearPageChecked(page); |
Chris Mason | 9ad6b7b | 2008-04-18 16:11:30 -0400 | [diff] [blame] | 8488 | if (PagePrivate(page)) { |
Chris Mason | 9ad6b7b | 2008-04-18 16:11:30 -0400 | [diff] [blame] | 8489 | ClearPagePrivate(page); |
| 8490 | set_page_private(page, 0); |
| 8491 | page_cache_release(page); |
| 8492 | } |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 8493 | } |
| 8494 | |
Chris Mason | 9ebefb18 | 2007-06-15 13:50:00 -0400 | [diff] [blame] | 8495 | /* |
| 8496 | * btrfs_page_mkwrite() is not allowed to change the file size as it gets |
| 8497 | * called from a page fault handler when a page is first dirtied. Hence we must |
| 8498 | * be careful to check for EOF conditions here. We set the page up correctly |
| 8499 | * for a written page which means we get ENOSPC checking when writing into |
| 8500 | * holes and correct delalloc and unwritten extent mapping on filesystems that |
| 8501 | * support these features. |
| 8502 | * |
| 8503 | * We are not allowed to take the i_mutex here so we have to play games to |
| 8504 | * protect against truncate races as the page could now be beyond EOF. Because |
| 8505 | * vmtruncate() writes the inode size before removing pages, once we have the |
| 8506 | * page lock we can determine safely if the page is beyond EOF. If it is not |
| 8507 | * beyond EOF, then the page is guaranteed safe against truncation until we |
| 8508 | * unlock the page. |
| 8509 | */ |
Nick Piggin | c2ec175 | 2009-03-31 15:23:21 -0700 | [diff] [blame] | 8510 | 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] | 8511 | { |
Nick Piggin | c2ec175 | 2009-03-31 15:23:21 -0700 | [diff] [blame] | 8512 | struct page *page = vmf->page; |
Al Viro | 496ad9a | 2013-01-23 17:07:38 -0500 | [diff] [blame] | 8513 | struct inode *inode = file_inode(vma->vm_file); |
Chris Mason | 1832a6d | 2007-12-21 16:27:21 -0500 | [diff] [blame] | 8514 | struct btrfs_root *root = BTRFS_I(inode)->root; |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 8515 | struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree; |
| 8516 | struct btrfs_ordered_extent *ordered; |
Josef Bacik | 2ac55d4 | 2010-02-03 19:33:23 +0000 | [diff] [blame] | 8517 | struct extent_state *cached_state = NULL; |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 8518 | char *kaddr; |
| 8519 | unsigned long zero_start; |
Chris Mason | 9ebefb18 | 2007-06-15 13:50:00 -0400 | [diff] [blame] | 8520 | loff_t size; |
Chris Mason | 1832a6d | 2007-12-21 16:27:21 -0500 | [diff] [blame] | 8521 | int ret; |
Chris Mason | 9998eb7 | 2012-01-25 13:47:40 -0500 | [diff] [blame] | 8522 | int reserved = 0; |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 8523 | u64 page_start; |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 8524 | u64 page_end; |
Chris Mason | 9ebefb18 | 2007-06-15 13:50:00 -0400 | [diff] [blame] | 8525 | |
Jan Kara | b2b5ef5 | 2012-06-12 16:20:45 +0200 | [diff] [blame] | 8526 | sb_start_pagefault(inode->i_sb); |
Yan, Zheng | 0ca1f7c | 2010-05-16 10:48:47 -0400 | [diff] [blame] | 8527 | ret = btrfs_delalloc_reserve_space(inode, PAGE_CACHE_SIZE); |
Chris Mason | 9998eb7 | 2012-01-25 13:47:40 -0500 | [diff] [blame] | 8528 | if (!ret) { |
Josef Bacik | e41f941 | 2012-03-26 09:46:47 -0400 | [diff] [blame] | 8529 | ret = file_update_time(vma->vm_file); |
Chris Mason | 9998eb7 | 2012-01-25 13:47:40 -0500 | [diff] [blame] | 8530 | reserved = 1; |
| 8531 | } |
Nick Piggin | 56a76f8 | 2009-03-31 15:23:23 -0700 | [diff] [blame] | 8532 | if (ret) { |
| 8533 | if (ret == -ENOMEM) |
| 8534 | ret = VM_FAULT_OOM; |
| 8535 | else /* -ENOSPC, -EIO, etc */ |
| 8536 | ret = VM_FAULT_SIGBUS; |
Chris Mason | 9998eb7 | 2012-01-25 13:47:40 -0500 | [diff] [blame] | 8537 | if (reserved) |
| 8538 | goto out; |
| 8539 | goto out_noreserve; |
Nick Piggin | 56a76f8 | 2009-03-31 15:23:23 -0700 | [diff] [blame] | 8540 | } |
Chris Mason | 1832a6d | 2007-12-21 16:27:21 -0500 | [diff] [blame] | 8541 | |
Nick Piggin | 56a76f8 | 2009-03-31 15:23:23 -0700 | [diff] [blame] | 8542 | ret = VM_FAULT_NOPAGE; /* make the VM retry the fault */ |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 8543 | again: |
Chris Mason | 9ebefb18 | 2007-06-15 13:50:00 -0400 | [diff] [blame] | 8544 | lock_page(page); |
Chris Mason | 9ebefb18 | 2007-06-15 13:50:00 -0400 | [diff] [blame] | 8545 | size = i_size_read(inode); |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 8546 | page_start = page_offset(page); |
| 8547 | page_end = page_start + PAGE_CACHE_SIZE - 1; |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 8548 | |
Chris Mason | 9ebefb18 | 2007-06-15 13:50:00 -0400 | [diff] [blame] | 8549 | if ((page->mapping != inode->i_mapping) || |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 8550 | (page_start >= size)) { |
Chris Mason | 9ebefb18 | 2007-06-15 13:50:00 -0400 | [diff] [blame] | 8551 | /* page got truncated out from underneath us */ |
| 8552 | goto out_unlock; |
| 8553 | } |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 8554 | wait_on_page_writeback(page); |
| 8555 | |
Jeff Mahoney | d008237 | 2012-03-01 14:57:19 +0100 | [diff] [blame] | 8556 | lock_extent_bits(io_tree, page_start, page_end, 0, &cached_state); |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 8557 | set_page_extent_mapped(page); |
| 8558 | |
Chris Mason | eb84ae0 | 2008-07-17 13:53:27 -0400 | [diff] [blame] | 8559 | /* |
| 8560 | * we can't set the delalloc bits if there are pending ordered |
| 8561 | * extents. Drop our locks and wait for them to finish |
| 8562 | */ |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 8563 | ordered = btrfs_lookup_ordered_extent(inode, page_start); |
| 8564 | if (ordered) { |
Josef Bacik | 2ac55d4 | 2010-02-03 19:33:23 +0000 | [diff] [blame] | 8565 | unlock_extent_cached(io_tree, page_start, page_end, |
| 8566 | &cached_state, GFP_NOFS); |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 8567 | unlock_page(page); |
Chris Mason | eb84ae0 | 2008-07-17 13:53:27 -0400 | [diff] [blame] | 8568 | btrfs_start_ordered_extent(inode, ordered, 1); |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 8569 | btrfs_put_ordered_extent(ordered); |
| 8570 | goto again; |
| 8571 | } |
| 8572 | |
Josef Bacik | fbf1908 | 2009-10-01 17:10:23 -0400 | [diff] [blame] | 8573 | /* |
| 8574 | * XXX - page_mkwrite gets called every time the page is dirtied, even |
| 8575 | * if it was already dirty, so for space accounting reasons we need to |
| 8576 | * clear any delalloc bits for the range we are fixing to save. There |
| 8577 | * is probably a better way to do this, but for now keep consistent with |
| 8578 | * prepare_pages in the normal write path. |
| 8579 | */ |
Josef Bacik | 2ac55d4 | 2010-02-03 19:33:23 +0000 | [diff] [blame] | 8580 | clear_extent_bit(&BTRFS_I(inode)->io_tree, page_start, page_end, |
Liu Bo | 9e8a4a8 | 2012-09-05 19:10:51 -0600 | [diff] [blame] | 8581 | EXTENT_DIRTY | EXTENT_DELALLOC | |
| 8582 | EXTENT_DO_ACCOUNTING | EXTENT_DEFRAG, |
Josef Bacik | 2ac55d4 | 2010-02-03 19:33:23 +0000 | [diff] [blame] | 8583 | 0, 0, &cached_state, GFP_NOFS); |
Josef Bacik | fbf1908 | 2009-10-01 17:10:23 -0400 | [diff] [blame] | 8584 | |
Josef Bacik | 2ac55d4 | 2010-02-03 19:33:23 +0000 | [diff] [blame] | 8585 | ret = btrfs_set_extent_delalloc(inode, page_start, page_end, |
| 8586 | &cached_state); |
Josef Bacik | 9ed74f2 | 2009-09-11 16:12:44 -0400 | [diff] [blame] | 8587 | if (ret) { |
Josef Bacik | 2ac55d4 | 2010-02-03 19:33:23 +0000 | [diff] [blame] | 8588 | unlock_extent_cached(io_tree, page_start, page_end, |
| 8589 | &cached_state, GFP_NOFS); |
Josef Bacik | 9ed74f2 | 2009-09-11 16:12:44 -0400 | [diff] [blame] | 8590 | ret = VM_FAULT_SIGBUS; |
| 8591 | goto out_unlock; |
| 8592 | } |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 8593 | ret = 0; |
Chris Mason | 9ebefb18 | 2007-06-15 13:50:00 -0400 | [diff] [blame] | 8594 | |
| 8595 | /* page is wholly or partially inside EOF */ |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 8596 | if (page_start + PAGE_CACHE_SIZE > size) |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 8597 | zero_start = size & ~PAGE_CACHE_MASK; |
Chris Mason | 9ebefb18 | 2007-06-15 13:50:00 -0400 | [diff] [blame] | 8598 | else |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 8599 | zero_start = PAGE_CACHE_SIZE; |
Chris Mason | 9ebefb18 | 2007-06-15 13:50:00 -0400 | [diff] [blame] | 8600 | |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 8601 | if (zero_start != PAGE_CACHE_SIZE) { |
| 8602 | kaddr = kmap(page); |
| 8603 | memset(kaddr + zero_start, 0, PAGE_CACHE_SIZE - zero_start); |
| 8604 | flush_dcache_page(page); |
| 8605 | kunmap(page); |
| 8606 | } |
Chris Mason | 247e743 | 2008-07-17 12:53:51 -0400 | [diff] [blame] | 8607 | ClearPageChecked(page); |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 8608 | set_page_dirty(page); |
Chris Mason | 50a9b21 | 2009-09-11 12:33:12 -0400 | [diff] [blame] | 8609 | SetPageUptodate(page); |
Chris Mason | 5a3f23d | 2009-03-31 13:27:11 -0400 | [diff] [blame] | 8610 | |
Chris Mason | 257c62e | 2009-10-13 13:21:08 -0400 | [diff] [blame] | 8611 | BTRFS_I(inode)->last_trans = root->fs_info->generation; |
| 8612 | BTRFS_I(inode)->last_sub_trans = BTRFS_I(inode)->root->log_transid; |
Liu Bo | 46d8bc3 | 2012-08-29 01:07:55 -0600 | [diff] [blame] | 8613 | 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] | 8614 | |
Josef Bacik | 2ac55d4 | 2010-02-03 19:33:23 +0000 | [diff] [blame] | 8615 | 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] | 8616 | |
| 8617 | out_unlock: |
Jan Kara | b2b5ef5 | 2012-06-12 16:20:45 +0200 | [diff] [blame] | 8618 | if (!ret) { |
| 8619 | sb_end_pagefault(inode->i_sb); |
Chris Mason | 50a9b21 | 2009-09-11 12:33:12 -0400 | [diff] [blame] | 8620 | return VM_FAULT_LOCKED; |
Jan Kara | b2b5ef5 | 2012-06-12 16:20:45 +0200 | [diff] [blame] | 8621 | } |
Chris Mason | 9ebefb18 | 2007-06-15 13:50:00 -0400 | [diff] [blame] | 8622 | unlock_page(page); |
Chris Mason | 1832a6d | 2007-12-21 16:27:21 -0500 | [diff] [blame] | 8623 | out: |
Josef Bacik | ec39e18 | 2012-01-12 19:10:12 -0500 | [diff] [blame] | 8624 | btrfs_delalloc_release_space(inode, PAGE_CACHE_SIZE); |
Chris Mason | 9998eb7 | 2012-01-25 13:47:40 -0500 | [diff] [blame] | 8625 | out_noreserve: |
Jan Kara | b2b5ef5 | 2012-06-12 16:20:45 +0200 | [diff] [blame] | 8626 | sb_end_pagefault(inode->i_sb); |
Chris Mason | 9ebefb18 | 2007-06-15 13:50:00 -0400 | [diff] [blame] | 8627 | return ret; |
| 8628 | } |
| 8629 | |
Josef Bacik | a41ad39 | 2011-01-31 15:30:16 -0500 | [diff] [blame] | 8630 | static int btrfs_truncate(struct inode *inode) |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 8631 | { |
| 8632 | struct btrfs_root *root = BTRFS_I(inode)->root; |
Josef Bacik | fcb80c2 | 2011-05-03 10:40:22 -0400 | [diff] [blame] | 8633 | struct btrfs_block_rsv *rsv; |
Josef Bacik | a71754f | 2013-06-17 17:14:39 -0400 | [diff] [blame] | 8634 | int ret = 0; |
Josef Bacik | 3893e33 | 2011-01-31 16:03:11 -0500 | [diff] [blame] | 8635 | int err = 0; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 8636 | struct btrfs_trans_handle *trans; |
Chris Mason | dbe674a | 2008-07-17 12:54:05 -0400 | [diff] [blame] | 8637 | u64 mask = root->sectorsize - 1; |
Josef Bacik | 0712718 | 2011-08-19 10:29:59 -0400 | [diff] [blame] | 8638 | u64 min_size = btrfs_calc_trunc_metadata_size(root, 1); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 8639 | |
Josef Bacik | 0ef8b72 | 2013-10-25 16:13:35 -0400 | [diff] [blame] | 8640 | ret = btrfs_wait_ordered_range(inode, inode->i_size & (~mask), |
| 8641 | (u64)-1); |
| 8642 | if (ret) |
| 8643 | return ret; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 8644 | |
Josef Bacik | fcb80c2 | 2011-05-03 10:40:22 -0400 | [diff] [blame] | 8645 | /* |
| 8646 | * Yes ladies and gentelment, this is indeed ugly. The fact is we have |
| 8647 | * 3 things going on here |
| 8648 | * |
| 8649 | * 1) We need to reserve space for our orphan item and the space to |
| 8650 | * delete our orphan item. Lord knows we don't want to have a dangling |
| 8651 | * orphan item because we didn't reserve space to remove it. |
| 8652 | * |
| 8653 | * 2) We need to reserve space to update our inode. |
| 8654 | * |
| 8655 | * 3) We need to have something to cache all the space that is going to |
| 8656 | * be free'd up by the truncate operation, but also have some slack |
| 8657 | * space reserved in case it uses space during the truncate (thank you |
| 8658 | * very much snapshotting). |
| 8659 | * |
| 8660 | * And we need these to all be seperate. The fact is we can use alot of |
| 8661 | * space doing the truncate, and we have no earthly idea how much space |
| 8662 | * we will use, so we need the truncate reservation to be seperate so it |
| 8663 | * doesn't end up using space reserved for updating the inode or |
| 8664 | * removing the orphan item. We also need to be able to stop the |
| 8665 | * transaction and start a new one, which means we need to be able to |
| 8666 | * update the inode several times, and we have no idea of knowing how |
| 8667 | * many times that will be, so we can't just reserve 1 item for the |
| 8668 | * entirety of the opration, so that has to be done seperately as well. |
| 8669 | * Then there is the orphan item, which does indeed need to be held on |
| 8670 | * to for the whole operation, and we need nobody to touch this reserved |
| 8671 | * space except the orphan code. |
| 8672 | * |
| 8673 | * So that leaves us with |
| 8674 | * |
| 8675 | * 1) root->orphan_block_rsv - for the orphan deletion. |
| 8676 | * 2) rsv - for the truncate reservation, which we will steal from the |
| 8677 | * transaction reservation. |
| 8678 | * 3) fs_info->trans_block_rsv - this will have 1 items worth left for |
| 8679 | * updating the inode. |
| 8680 | */ |
Miao Xie | 66d8f3d | 2012-09-06 04:02:28 -0600 | [diff] [blame] | 8681 | rsv = btrfs_alloc_block_rsv(root, BTRFS_BLOCK_RSV_TEMP); |
Josef Bacik | fcb80c2 | 2011-05-03 10:40:22 -0400 | [diff] [blame] | 8682 | if (!rsv) |
| 8683 | return -ENOMEM; |
Josef Bacik | 4a33854 | 2011-08-29 11:01:31 -0400 | [diff] [blame] | 8684 | rsv->size = min_size; |
Josef Bacik | ca7e70f | 2012-08-27 17:48:15 -0400 | [diff] [blame] | 8685 | rsv->failfast = 1; |
Josef Bacik | f0cd846 | 2011-03-04 14:37:08 -0500 | [diff] [blame] | 8686 | |
Josef Bacik | 907cbce | 2011-08-08 13:46:15 -0400 | [diff] [blame] | 8687 | /* |
Josef Bacik | 0712718 | 2011-08-19 10:29:59 -0400 | [diff] [blame] | 8688 | * 1 for the truncate slack space |
Josef Bacik | 907cbce | 2011-08-08 13:46:15 -0400 | [diff] [blame] | 8689 | * 1 for updating the inode. |
| 8690 | */ |
Josef Bacik | f3fe820 | 2013-01-07 17:03:21 -0500 | [diff] [blame] | 8691 | trans = btrfs_start_transaction(root, 2); |
Josef Bacik | fcb80c2 | 2011-05-03 10:40:22 -0400 | [diff] [blame] | 8692 | if (IS_ERR(trans)) { |
| 8693 | err = PTR_ERR(trans); |
| 8694 | goto out; |
| 8695 | } |
Josef Bacik | f0cd846 | 2011-03-04 14:37:08 -0500 | [diff] [blame] | 8696 | |
Josef Bacik | 907cbce | 2011-08-08 13:46:15 -0400 | [diff] [blame] | 8697 | /* Migrate the slack space for the truncate to our reserve */ |
| 8698 | ret = btrfs_block_rsv_migrate(&root->fs_info->trans_block_rsv, rsv, |
| 8699 | min_size); |
Josef Bacik | fcb80c2 | 2011-05-03 10:40:22 -0400 | [diff] [blame] | 8700 | BUG_ON(ret); |
Josef Bacik | f0cd846 | 2011-03-04 14:37:08 -0500 | [diff] [blame] | 8701 | |
Chris Mason | 5a3f23d | 2009-03-31 13:27:11 -0400 | [diff] [blame] | 8702 | /* |
Josef Bacik | 5dc562c | 2012-08-17 13:14:17 -0400 | [diff] [blame] | 8703 | * So if we truncate and then write and fsync we normally would just |
| 8704 | * write the extents that changed, which is a problem if we need to |
| 8705 | * first truncate that entire inode. So set this flag so we write out |
| 8706 | * all of the extents in the inode to the sync log so we're completely |
| 8707 | * safe. |
| 8708 | */ |
| 8709 | set_bit(BTRFS_INODE_NEEDS_FULL_SYNC, &BTRFS_I(inode)->runtime_flags); |
Josef Bacik | ca7e70f | 2012-08-27 17:48:15 -0400 | [diff] [blame] | 8710 | trans->block_rsv = rsv; |
Josef Bacik | 5dc562c | 2012-08-17 13:14:17 -0400 | [diff] [blame] | 8711 | |
Yan, Zheng | 8082510 | 2009-11-12 09:35:36 +0000 | [diff] [blame] | 8712 | while (1) { |
| 8713 | ret = btrfs_truncate_inode_items(trans, root, inode, |
| 8714 | inode->i_size, |
| 8715 | BTRFS_EXTENT_DATA_KEY); |
Chris Mason | 28ed134 | 2014-12-17 09:41:04 -0800 | [diff] [blame] | 8716 | if (ret != -ENOSPC && ret != -EAGAIN) { |
Josef Bacik | 3893e33 | 2011-01-31 16:03:11 -0500 | [diff] [blame] | 8717 | err = ret; |
Yan, Zheng | 8082510 | 2009-11-12 09:35:36 +0000 | [diff] [blame] | 8718 | break; |
Josef Bacik | 3893e33 | 2011-01-31 16:03:11 -0500 | [diff] [blame] | 8719 | } |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 8720 | |
Josef Bacik | fcb80c2 | 2011-05-03 10:40:22 -0400 | [diff] [blame] | 8721 | trans->block_rsv = &root->fs_info->trans_block_rsv; |
Yan, Zheng | 8082510 | 2009-11-12 09:35:36 +0000 | [diff] [blame] | 8722 | ret = btrfs_update_inode(trans, root, inode); |
Josef Bacik | 3893e33 | 2011-01-31 16:03:11 -0500 | [diff] [blame] | 8723 | if (ret) { |
| 8724 | err = ret; |
| 8725 | break; |
| 8726 | } |
Josef Bacik | ca7e70f | 2012-08-27 17:48:15 -0400 | [diff] [blame] | 8727 | |
Yan, Zheng | 8082510 | 2009-11-12 09:35:36 +0000 | [diff] [blame] | 8728 | btrfs_end_transaction(trans, root); |
Liu Bo | b53d3f5 | 2012-11-14 14:34:34 +0000 | [diff] [blame] | 8729 | btrfs_btree_balance_dirty(root); |
Josef Bacik | ca7e70f | 2012-08-27 17:48:15 -0400 | [diff] [blame] | 8730 | |
| 8731 | trans = btrfs_start_transaction(root, 2); |
| 8732 | if (IS_ERR(trans)) { |
| 8733 | ret = err = PTR_ERR(trans); |
| 8734 | trans = NULL; |
| 8735 | break; |
| 8736 | } |
| 8737 | |
| 8738 | ret = btrfs_block_rsv_migrate(&root->fs_info->trans_block_rsv, |
| 8739 | rsv, min_size); |
| 8740 | BUG_ON(ret); /* shouldn't happen */ |
| 8741 | trans->block_rsv = rsv; |
Yan, Zheng | 8082510 | 2009-11-12 09:35:36 +0000 | [diff] [blame] | 8742 | } |
| 8743 | |
| 8744 | if (ret == 0 && inode->i_nlink > 0) { |
Josef Bacik | fcb80c2 | 2011-05-03 10:40:22 -0400 | [diff] [blame] | 8745 | trans->block_rsv = root->orphan_block_rsv; |
Yan, Zheng | 8082510 | 2009-11-12 09:35:36 +0000 | [diff] [blame] | 8746 | ret = btrfs_orphan_del(trans, inode); |
Josef Bacik | 3893e33 | 2011-01-31 16:03:11 -0500 | [diff] [blame] | 8747 | if (ret) |
| 8748 | err = ret; |
Yan, Zheng | 8082510 | 2009-11-12 09:35:36 +0000 | [diff] [blame] | 8749 | } |
| 8750 | |
Chris Mason | 917c16b | 2011-11-08 14:49:59 -0500 | [diff] [blame] | 8751 | if (trans) { |
| 8752 | trans->block_rsv = &root->fs_info->trans_block_rsv; |
| 8753 | ret = btrfs_update_inode(trans, root, inode); |
| 8754 | if (ret && !err) |
| 8755 | err = ret; |
Josef Bacik | 7b12876 | 2008-07-24 12:17:14 -0400 | [diff] [blame] | 8756 | |
Josef Bacik | 7ad85bb | 2012-01-12 19:10:12 -0500 | [diff] [blame] | 8757 | ret = btrfs_end_transaction(trans, root); |
Liu Bo | b53d3f5 | 2012-11-14 14:34:34 +0000 | [diff] [blame] | 8758 | btrfs_btree_balance_dirty(root); |
Chris Mason | 917c16b | 2011-11-08 14:49:59 -0500 | [diff] [blame] | 8759 | } |
Josef Bacik | fcb80c2 | 2011-05-03 10:40:22 -0400 | [diff] [blame] | 8760 | |
| 8761 | out: |
| 8762 | btrfs_free_block_rsv(root, rsv); |
| 8763 | |
Josef Bacik | 3893e33 | 2011-01-31 16:03:11 -0500 | [diff] [blame] | 8764 | if (ret && !err) |
| 8765 | err = ret; |
Josef Bacik | a41ad39 | 2011-01-31 15:30:16 -0500 | [diff] [blame] | 8766 | |
Josef Bacik | 3893e33 | 2011-01-31 16:03:11 -0500 | [diff] [blame] | 8767 | return err; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 8768 | } |
| 8769 | |
Sven Wegener | 3b96362 | 2008-06-09 21:57:42 -0400 | [diff] [blame] | 8770 | /* |
Chris Mason | d352ac6 | 2008-09-29 15:18:18 -0400 | [diff] [blame] | 8771 | * create a new subvolume directory/inode (helper for the ioctl). |
| 8772 | */ |
Yan Zheng | d2fb343 | 2008-12-11 16:30:39 -0500 | [diff] [blame] | 8773 | int btrfs_create_subvol_root(struct btrfs_trans_handle *trans, |
Filipe David Borba Manana | 6354192 | 2014-01-07 11:47:46 +0000 | [diff] [blame] | 8774 | struct btrfs_root *new_root, |
| 8775 | struct btrfs_root *parent_root, |
| 8776 | u64 new_dirid) |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 8777 | { |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 8778 | struct inode *inode; |
Yan, Zheng | 76dda93 | 2009-09-21 16:00:26 -0400 | [diff] [blame] | 8779 | int err; |
Chris Mason | 00e4e6b | 2008-08-05 11:18:09 -0400 | [diff] [blame] | 8780 | u64 index = 0; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 8781 | |
Florian Albrechtskirchinger | 12fc9d0 | 2012-02-10 22:15:54 +0100 | [diff] [blame] | 8782 | inode = btrfs_new_inode(trans, new_root, NULL, "..", 2, |
| 8783 | new_dirid, new_dirid, |
| 8784 | S_IFDIR | (~current_umask() & S_IRWXUGO), |
| 8785 | &index); |
Chris Mason | 54aa1f4 | 2007-06-22 14:16:25 -0400 | [diff] [blame] | 8786 | if (IS_ERR(inode)) |
Christoph Hellwig | f46b5a6 | 2008-06-11 21:53:53 -0400 | [diff] [blame] | 8787 | return PTR_ERR(inode); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 8788 | inode->i_op = &btrfs_dir_inode_operations; |
| 8789 | inode->i_fop = &btrfs_dir_file_operations; |
| 8790 | |
Miklos Szeredi | bfe8684 | 2011-10-28 14:13:29 +0200 | [diff] [blame] | 8791 | set_nlink(inode, 1); |
Chris Mason | dbe674a | 2008-07-17 12:54:05 -0400 | [diff] [blame] | 8792 | btrfs_i_size_write(inode, 0); |
Chris Mason | b0d5d10 | 2014-09-08 13:08:51 -0700 | [diff] [blame] | 8793 | unlock_new_inode(inode); |
Sven Wegener | 3b96362 | 2008-06-09 21:57:42 -0400 | [diff] [blame] | 8794 | |
Filipe David Borba Manana | 6354192 | 2014-01-07 11:47:46 +0000 | [diff] [blame] | 8795 | err = btrfs_subvol_inherit_props(trans, new_root, parent_root); |
| 8796 | if (err) |
| 8797 | btrfs_err(new_root->fs_info, |
David Sterba | 351fd35 | 2014-05-15 16:48:20 +0200 | [diff] [blame] | 8798 | "error inheriting subvolume %llu properties: %d", |
Filipe David Borba Manana | 6354192 | 2014-01-07 11:47:46 +0000 | [diff] [blame] | 8799 | new_root->root_key.objectid, err); |
| 8800 | |
Yan, Zheng | 76dda93 | 2009-09-21 16:00:26 -0400 | [diff] [blame] | 8801 | err = btrfs_update_inode(trans, new_root, inode); |
Christoph Hellwig | cb8e709 | 2008-10-09 13:39:39 -0400 | [diff] [blame] | 8802 | |
Yan, Zheng | 76dda93 | 2009-09-21 16:00:26 -0400 | [diff] [blame] | 8803 | iput(inode); |
Mark Fasheh | ce59897 | 2011-07-26 11:32:23 -0700 | [diff] [blame] | 8804 | return err; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 8805 | } |
| 8806 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 8807 | struct inode *btrfs_alloc_inode(struct super_block *sb) |
| 8808 | { |
| 8809 | struct btrfs_inode *ei; |
Yan, Zheng | 2ead6ae | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 8810 | struct inode *inode; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 8811 | |
| 8812 | ei = kmem_cache_alloc(btrfs_inode_cachep, GFP_NOFS); |
| 8813 | if (!ei) |
| 8814 | return NULL; |
Yan, Zheng | 2ead6ae | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 8815 | |
| 8816 | ei->root = NULL; |
Yan, Zheng | 2ead6ae | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 8817 | ei->generation = 0; |
Josef Bacik | 15ee9bc | 2007-08-10 16:22:09 -0400 | [diff] [blame] | 8818 | ei->last_trans = 0; |
Chris Mason | 257c62e | 2009-10-13 13:21:08 -0400 | [diff] [blame] | 8819 | ei->last_sub_trans = 0; |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 8820 | ei->logged_trans = 0; |
Yan, Zheng | 2ead6ae | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 8821 | ei->delalloc_bytes = 0; |
Wang Shilong | 47059d9 | 2014-07-03 18:22:07 +0800 | [diff] [blame] | 8822 | ei->defrag_bytes = 0; |
Yan, Zheng | 2ead6ae | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 8823 | ei->disk_i_size = 0; |
| 8824 | ei->flags = 0; |
Josef Bacik | 7709cde | 2011-08-04 10:25:02 -0400 | [diff] [blame] | 8825 | ei->csum_bytes = 0; |
Yan, Zheng | 2ead6ae | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 8826 | ei->index_cnt = (u64)-1; |
Miao Xie | 67de117 | 2013-12-26 13:07:06 +0800 | [diff] [blame] | 8827 | ei->dir_index = 0; |
Yan, Zheng | 2ead6ae | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 8828 | ei->last_unlink_trans = 0; |
Liu Bo | 46d8bc3 | 2012-08-29 01:07:55 -0600 | [diff] [blame] | 8829 | ei->last_log_commit = 0; |
Yan, Zheng | 2ead6ae | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 8830 | |
Josef Bacik | 9e0baf6 | 2011-07-15 15:16:44 +0000 | [diff] [blame] | 8831 | spin_lock_init(&ei->lock); |
| 8832 | ei->outstanding_extents = 0; |
| 8833 | ei->reserved_extents = 0; |
Yan, Zheng | 2ead6ae | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 8834 | |
Josef Bacik | 72ac3c0 | 2012-05-23 14:13:11 -0400 | [diff] [blame] | 8835 | ei->runtime_flags = 0; |
Li Zefan | 261507a0 | 2010-12-17 14:21:50 +0800 | [diff] [blame] | 8836 | ei->force_compress = BTRFS_COMPRESS_NONE; |
Yan, Zheng | 2ead6ae | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 8837 | |
Miao Xie | 16cdcec | 2011-04-22 18:12:22 +0800 | [diff] [blame] | 8838 | ei->delayed_node = NULL; |
| 8839 | |
chandan r | 9cc97d6 | 2012-07-04 12:48:07 +0530 | [diff] [blame] | 8840 | ei->i_otime.tv_sec = 0; |
| 8841 | ei->i_otime.tv_nsec = 0; |
| 8842 | |
Yan, Zheng | 2ead6ae | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 8843 | inode = &ei->vfs_inode; |
David Sterba | a8067e0 | 2011-04-21 00:34:43 +0200 | [diff] [blame] | 8844 | extent_map_tree_init(&ei->extent_tree); |
David Sterba | f993c88 | 2011-04-20 23:35:57 +0200 | [diff] [blame] | 8845 | extent_io_tree_init(&ei->io_tree, &inode->i_data); |
| 8846 | extent_io_tree_init(&ei->io_failure_tree, &inode->i_data); |
Josef Bacik | 0b32f4b | 2012-03-13 09:38:00 -0400 | [diff] [blame] | 8847 | ei->io_tree.track_uptodate = 1; |
| 8848 | ei->io_failure_tree.track_uptodate = 1; |
Josef Bacik | b812ce2 | 2012-11-16 13:56:32 -0500 | [diff] [blame] | 8849 | atomic_set(&ei->sync_writers, 0); |
Yan, Zheng | 2ead6ae | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 8850 | mutex_init(&ei->log_mutex); |
Josef Bacik | f248679 | 2012-01-13 12:09:22 -0500 | [diff] [blame] | 8851 | mutex_init(&ei->delalloc_mutex); |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 8852 | btrfs_ordered_inode_tree_init(&ei->ordered_tree); |
Yan, Zheng | 2ead6ae | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 8853 | INIT_LIST_HEAD(&ei->delalloc_inodes); |
Yan, Zheng | 2ead6ae | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 8854 | RB_CLEAR_NODE(&ei->rb_node); |
| 8855 | |
| 8856 | return inode; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 8857 | } |
| 8858 | |
Josef Bacik | aaedb55 | 2013-10-11 14:44:09 -0400 | [diff] [blame] | 8859 | #ifdef CONFIG_BTRFS_FS_RUN_SANITY_TESTS |
| 8860 | void btrfs_test_destroy_inode(struct inode *inode) |
| 8861 | { |
| 8862 | btrfs_drop_extent_cache(inode, 0, (u64)-1, 0); |
| 8863 | kmem_cache_free(btrfs_inode_cachep, BTRFS_I(inode)); |
| 8864 | } |
| 8865 | #endif |
| 8866 | |
Nick Piggin | fa0d7e3d | 2011-01-07 17:49:49 +1100 | [diff] [blame] | 8867 | static void btrfs_i_callback(struct rcu_head *head) |
| 8868 | { |
| 8869 | struct inode *inode = container_of(head, struct inode, i_rcu); |
Nick Piggin | fa0d7e3d | 2011-01-07 17:49:49 +1100 | [diff] [blame] | 8870 | kmem_cache_free(btrfs_inode_cachep, BTRFS_I(inode)); |
| 8871 | } |
| 8872 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 8873 | void btrfs_destroy_inode(struct inode *inode) |
| 8874 | { |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 8875 | struct btrfs_ordered_extent *ordered; |
Chris Mason | 5a3f23d | 2009-03-31 13:27:11 -0400 | [diff] [blame] | 8876 | struct btrfs_root *root = BTRFS_I(inode)->root; |
| 8877 | |
Al Viro | b3d9b7a | 2012-06-09 13:51:19 -0400 | [diff] [blame] | 8878 | WARN_ON(!hlist_empty(&inode->i_dentry)); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 8879 | WARN_ON(inode->i_data.nrpages); |
Josef Bacik | 9e0baf6 | 2011-07-15 15:16:44 +0000 | [diff] [blame] | 8880 | WARN_ON(BTRFS_I(inode)->outstanding_extents); |
| 8881 | WARN_ON(BTRFS_I(inode)->reserved_extents); |
Josef Bacik | 7709cde | 2011-08-04 10:25:02 -0400 | [diff] [blame] | 8882 | WARN_ON(BTRFS_I(inode)->delalloc_bytes); |
| 8883 | WARN_ON(BTRFS_I(inode)->csum_bytes); |
Wang Shilong | 47059d9 | 2014-07-03 18:22:07 +0800 | [diff] [blame] | 8884 | WARN_ON(BTRFS_I(inode)->defrag_bytes); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 8885 | |
Chris Mason | 5a3f23d | 2009-03-31 13:27:11 -0400 | [diff] [blame] | 8886 | /* |
Josef Bacik | a6dbd42 | 2009-11-11 15:53:34 -0500 | [diff] [blame] | 8887 | * This can happen where we create an inode, but somebody else also |
| 8888 | * created the same inode and we need to destroy the one we already |
| 8889 | * created. |
| 8890 | */ |
| 8891 | if (!root) |
| 8892 | goto free; |
| 8893 | |
Josef Bacik | 8a35d95 | 2012-05-23 14:26:42 -0400 | [diff] [blame] | 8894 | if (test_bit(BTRFS_INODE_HAS_ORPHAN_ITEM, |
| 8895 | &BTRFS_I(inode)->runtime_flags)) { |
Simon Kirby | c2cf52e | 2013-03-19 22:41:23 +0000 | [diff] [blame] | 8896 | btrfs_info(root->fs_info, "inode %llu still on the orphan list", |
Geert Uytterhoeven | c1c9ff7 | 2013-08-20 13:20:07 +0200 | [diff] [blame] | 8897 | btrfs_ino(inode)); |
Josef Bacik | 8a35d95 | 2012-05-23 14:26:42 -0400 | [diff] [blame] | 8898 | atomic_dec(&root->orphan_inodes); |
Josef Bacik | 7b12876 | 2008-07-24 12:17:14 -0400 | [diff] [blame] | 8899 | } |
Josef Bacik | 7b12876 | 2008-07-24 12:17:14 -0400 | [diff] [blame] | 8900 | |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 8901 | while (1) { |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 8902 | ordered = btrfs_lookup_first_ordered_extent(inode, (u64)-1); |
| 8903 | if (!ordered) |
| 8904 | break; |
| 8905 | else { |
Simon Kirby | c2cf52e | 2013-03-19 22:41:23 +0000 | [diff] [blame] | 8906 | 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] | 8907 | ordered->file_offset, ordered->len); |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 8908 | btrfs_remove_ordered_extent(inode, ordered); |
| 8909 | btrfs_put_ordered_extent(ordered); |
| 8910 | btrfs_put_ordered_extent(ordered); |
| 8911 | } |
| 8912 | } |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 8913 | inode_tree_del(inode); |
Zheng Yan | 5b21f2e | 2008-09-26 10:05:38 -0400 | [diff] [blame] | 8914 | btrfs_drop_extent_cache(inode, 0, (u64)-1, 0); |
Josef Bacik | a6dbd42 | 2009-11-11 15:53:34 -0500 | [diff] [blame] | 8915 | free: |
Nick Piggin | fa0d7e3d | 2011-01-07 17:49:49 +1100 | [diff] [blame] | 8916 | call_rcu(&inode->i_rcu, btrfs_i_callback); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 8917 | } |
| 8918 | |
Al Viro | 45321ac | 2010-06-07 13:43:19 -0400 | [diff] [blame] | 8919 | int btrfs_drop_inode(struct inode *inode) |
Yan, Zheng | 76dda93 | 2009-09-21 16:00:26 -0400 | [diff] [blame] | 8920 | { |
| 8921 | struct btrfs_root *root = BTRFS_I(inode)->root; |
Al Viro | 45321ac | 2010-06-07 13:43:19 -0400 | [diff] [blame] | 8922 | |
Naohiro Aota | 6379ef9 | 2013-06-06 09:56:34 +0000 | [diff] [blame] | 8923 | if (root == NULL) |
| 8924 | return 1; |
| 8925 | |
Liu Bo | fa6ac87 | 2013-02-20 14:10:23 +0000 | [diff] [blame] | 8926 | /* the snap/subvol tree is on deleting */ |
Stefan Behrens | 69e9c6c | 2013-09-05 16:58:43 +0200 | [diff] [blame] | 8927 | if (btrfs_root_refs(&root->root_item) == 0) |
Al Viro | 45321ac | 2010-06-07 13:43:19 -0400 | [diff] [blame] | 8928 | return 1; |
Yan, Zheng | 76dda93 | 2009-09-21 16:00:26 -0400 | [diff] [blame] | 8929 | else |
Al Viro | 45321ac | 2010-06-07 13:43:19 -0400 | [diff] [blame] | 8930 | return generic_drop_inode(inode); |
Yan, Zheng | 76dda93 | 2009-09-21 16:00:26 -0400 | [diff] [blame] | 8931 | } |
| 8932 | |
Sven Wegener | 0ee0fda | 2008-07-30 16:54:26 -0400 | [diff] [blame] | 8933 | static void init_once(void *foo) |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 8934 | { |
| 8935 | struct btrfs_inode *ei = (struct btrfs_inode *) foo; |
| 8936 | |
| 8937 | inode_init_once(&ei->vfs_inode); |
| 8938 | } |
| 8939 | |
| 8940 | void btrfs_destroy_cachep(void) |
| 8941 | { |
Kirill A. Shutemov | 8c0a853 | 2012-09-26 11:33:07 +1000 | [diff] [blame] | 8942 | /* |
| 8943 | * Make sure all delayed rcu free inodes are flushed before we |
| 8944 | * destroy cache. |
| 8945 | */ |
| 8946 | rcu_barrier(); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 8947 | if (btrfs_inode_cachep) |
| 8948 | kmem_cache_destroy(btrfs_inode_cachep); |
| 8949 | if (btrfs_trans_handle_cachep) |
| 8950 | kmem_cache_destroy(btrfs_trans_handle_cachep); |
| 8951 | if (btrfs_transaction_cachep) |
| 8952 | kmem_cache_destroy(btrfs_transaction_cachep); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 8953 | if (btrfs_path_cachep) |
| 8954 | kmem_cache_destroy(btrfs_path_cachep); |
Josef Bacik | dc89e98 | 2011-01-28 17:05:48 -0500 | [diff] [blame] | 8955 | if (btrfs_free_space_cachep) |
| 8956 | kmem_cache_destroy(btrfs_free_space_cachep); |
Miao Xie | 8ccf6f19 | 2012-10-25 09:28:04 +0000 | [diff] [blame] | 8957 | if (btrfs_delalloc_work_cachep) |
| 8958 | kmem_cache_destroy(btrfs_delalloc_work_cachep); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 8959 | } |
| 8960 | |
| 8961 | int btrfs_init_cachep(void) |
| 8962 | { |
David Sterba | 837e197 | 2012-09-07 03:00:48 -0600 | [diff] [blame] | 8963 | btrfs_inode_cachep = kmem_cache_create("btrfs_inode", |
Christoph Hellwig | 9601e3f | 2009-04-13 15:33:09 +0200 | [diff] [blame] | 8964 | sizeof(struct btrfs_inode), 0, |
| 8965 | SLAB_RECLAIM_ACCOUNT | SLAB_MEM_SPREAD, init_once); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 8966 | if (!btrfs_inode_cachep) |
| 8967 | goto fail; |
Christoph Hellwig | 9601e3f | 2009-04-13 15:33:09 +0200 | [diff] [blame] | 8968 | |
David Sterba | 837e197 | 2012-09-07 03:00:48 -0600 | [diff] [blame] | 8969 | btrfs_trans_handle_cachep = kmem_cache_create("btrfs_trans_handle", |
Christoph Hellwig | 9601e3f | 2009-04-13 15:33:09 +0200 | [diff] [blame] | 8970 | sizeof(struct btrfs_trans_handle), 0, |
| 8971 | SLAB_RECLAIM_ACCOUNT | SLAB_MEM_SPREAD, NULL); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 8972 | if (!btrfs_trans_handle_cachep) |
| 8973 | goto fail; |
Christoph Hellwig | 9601e3f | 2009-04-13 15:33:09 +0200 | [diff] [blame] | 8974 | |
David Sterba | 837e197 | 2012-09-07 03:00:48 -0600 | [diff] [blame] | 8975 | btrfs_transaction_cachep = kmem_cache_create("btrfs_transaction", |
Christoph Hellwig | 9601e3f | 2009-04-13 15:33:09 +0200 | [diff] [blame] | 8976 | sizeof(struct btrfs_transaction), 0, |
| 8977 | SLAB_RECLAIM_ACCOUNT | SLAB_MEM_SPREAD, NULL); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 8978 | if (!btrfs_transaction_cachep) |
| 8979 | goto fail; |
Christoph Hellwig | 9601e3f | 2009-04-13 15:33:09 +0200 | [diff] [blame] | 8980 | |
David Sterba | 837e197 | 2012-09-07 03:00:48 -0600 | [diff] [blame] | 8981 | btrfs_path_cachep = kmem_cache_create("btrfs_path", |
Christoph Hellwig | 9601e3f | 2009-04-13 15:33:09 +0200 | [diff] [blame] | 8982 | sizeof(struct btrfs_path), 0, |
| 8983 | SLAB_RECLAIM_ACCOUNT | SLAB_MEM_SPREAD, NULL); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 8984 | if (!btrfs_path_cachep) |
| 8985 | goto fail; |
Christoph Hellwig | 9601e3f | 2009-04-13 15:33:09 +0200 | [diff] [blame] | 8986 | |
David Sterba | 837e197 | 2012-09-07 03:00:48 -0600 | [diff] [blame] | 8987 | btrfs_free_space_cachep = kmem_cache_create("btrfs_free_space", |
Josef Bacik | dc89e98 | 2011-01-28 17:05:48 -0500 | [diff] [blame] | 8988 | sizeof(struct btrfs_free_space), 0, |
| 8989 | SLAB_RECLAIM_ACCOUNT | SLAB_MEM_SPREAD, NULL); |
| 8990 | if (!btrfs_free_space_cachep) |
| 8991 | goto fail; |
| 8992 | |
Miao Xie | 8ccf6f19 | 2012-10-25 09:28:04 +0000 | [diff] [blame] | 8993 | btrfs_delalloc_work_cachep = kmem_cache_create("btrfs_delalloc_work", |
| 8994 | sizeof(struct btrfs_delalloc_work), 0, |
| 8995 | SLAB_RECLAIM_ACCOUNT | SLAB_MEM_SPREAD, |
| 8996 | NULL); |
| 8997 | if (!btrfs_delalloc_work_cachep) |
| 8998 | goto fail; |
| 8999 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 9000 | return 0; |
| 9001 | fail: |
| 9002 | btrfs_destroy_cachep(); |
| 9003 | return -ENOMEM; |
| 9004 | } |
| 9005 | |
| 9006 | static int btrfs_getattr(struct vfsmount *mnt, |
| 9007 | struct dentry *dentry, struct kstat *stat) |
| 9008 | { |
Miao Xie | df0af1a | 2013-01-29 10:11:59 +0000 | [diff] [blame] | 9009 | u64 delalloc_bytes; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 9010 | struct inode *inode = dentry->d_inode; |
David Sterba | fadc0d8 | 2011-11-20 07:33:38 -0500 | [diff] [blame] | 9011 | u32 blocksize = inode->i_sb->s_blocksize; |
| 9012 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 9013 | generic_fillattr(inode, stat); |
Al Viro | 0ee5dc6 | 2011-07-07 15:44:25 -0400 | [diff] [blame] | 9014 | stat->dev = BTRFS_I(inode)->root->anon_dev; |
Chris Mason | d666746 | 2008-01-03 14:51:00 -0500 | [diff] [blame] | 9015 | stat->blksize = PAGE_CACHE_SIZE; |
Miao Xie | df0af1a | 2013-01-29 10:11:59 +0000 | [diff] [blame] | 9016 | |
| 9017 | spin_lock(&BTRFS_I(inode)->lock); |
| 9018 | delalloc_bytes = BTRFS_I(inode)->delalloc_bytes; |
| 9019 | spin_unlock(&BTRFS_I(inode)->lock); |
David Sterba | fadc0d8 | 2011-11-20 07:33:38 -0500 | [diff] [blame] | 9020 | stat->blocks = (ALIGN(inode_get_bytes(inode), blocksize) + |
Miao Xie | df0af1a | 2013-01-29 10:11:59 +0000 | [diff] [blame] | 9021 | ALIGN(delalloc_bytes, blocksize)) >> 9; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 9022 | return 0; |
| 9023 | } |
| 9024 | |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 9025 | static int btrfs_rename(struct inode *old_dir, struct dentry *old_dentry, |
| 9026 | struct inode *new_dir, struct dentry *new_dentry) |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 9027 | { |
| 9028 | struct btrfs_trans_handle *trans; |
| 9029 | struct btrfs_root *root = BTRFS_I(old_dir)->root; |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 9030 | struct btrfs_root *dest = BTRFS_I(new_dir)->root; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 9031 | struct inode *new_inode = new_dentry->d_inode; |
| 9032 | struct inode *old_inode = old_dentry->d_inode; |
| 9033 | struct timespec ctime = CURRENT_TIME; |
Chris Mason | 00e4e6b | 2008-08-05 11:18:09 -0400 | [diff] [blame] | 9034 | u64 index = 0; |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 9035 | u64 root_objectid; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 9036 | int ret; |
Li Zefan | 33345d01 | 2011-04-20 10:31:50 +0800 | [diff] [blame] | 9037 | u64 old_ino = btrfs_ino(old_inode); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 9038 | |
Li Zefan | 33345d01 | 2011-04-20 10:31:50 +0800 | [diff] [blame] | 9039 | if (btrfs_ino(new_dir) == BTRFS_EMPTY_SUBVOL_DIR_OBJECTID) |
Yan, Zheng | f679a84 | 2009-09-24 09:17:31 -0400 | [diff] [blame] | 9040 | return -EPERM; |
| 9041 | |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 9042 | /* we only allow rename subvolume link between subvolumes */ |
Li Zefan | 33345d01 | 2011-04-20 10:31:50 +0800 | [diff] [blame] | 9043 | if (old_ino != BTRFS_FIRST_FREE_OBJECTID && root != dest) |
Chris Mason | 3394e16 | 2008-11-17 20:42:26 -0500 | [diff] [blame] | 9044 | return -EXDEV; |
| 9045 | |
Li Zefan | 33345d01 | 2011-04-20 10:31:50 +0800 | [diff] [blame] | 9046 | if (old_ino == BTRFS_EMPTY_SUBVOL_DIR_OBJECTID || |
| 9047 | (new_inode && btrfs_ino(new_inode) == BTRFS_FIRST_FREE_OBJECTID)) |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 9048 | return -ENOTEMPTY; |
| 9049 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 9050 | if (S_ISDIR(old_inode->i_mode) && new_inode && |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 9051 | new_inode->i_size > BTRFS_EMPTY_DIR_SIZE) |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 9052 | return -ENOTEMPTY; |
Chris Mason | 9c52057 | 2012-12-17 14:26:57 -0500 | [diff] [blame] | 9053 | |
| 9054 | |
| 9055 | /* check for collisions, even if the name isn't there */ |
Josef Bacik | 4871c15 | 2013-10-09 12:24:04 -0400 | [diff] [blame] | 9056 | ret = btrfs_check_dir_item_collision(dest, new_dir->i_ino, |
Chris Mason | 9c52057 | 2012-12-17 14:26:57 -0500 | [diff] [blame] | 9057 | new_dentry->d_name.name, |
| 9058 | new_dentry->d_name.len); |
| 9059 | |
| 9060 | if (ret) { |
| 9061 | if (ret == -EEXIST) { |
| 9062 | /* we shouldn't get |
| 9063 | * eexist without a new_inode */ |
Dulshani Gunawardhana | fae7f21 | 2013-10-31 10:30:08 +0530 | [diff] [blame] | 9064 | if (WARN_ON(!new_inode)) { |
Chris Mason | 9c52057 | 2012-12-17 14:26:57 -0500 | [diff] [blame] | 9065 | return ret; |
| 9066 | } |
| 9067 | } else { |
| 9068 | /* maybe -EOVERFLOW */ |
| 9069 | return ret; |
| 9070 | } |
| 9071 | } |
| 9072 | ret = 0; |
| 9073 | |
Chris Mason | 5a3f23d | 2009-03-31 13:27:11 -0400 | [diff] [blame] | 9074 | /* |
Chris Mason | 8d875f9 | 2014-08-12 10:47:42 -0700 | [diff] [blame] | 9075 | * we're using rename to replace one file with another. Start IO on it |
| 9076 | * 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] | 9077 | */ |
Chris Mason | 8d875f9 | 2014-08-12 10:47:42 -0700 | [diff] [blame] | 9078 | 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] | 9079 | filemap_flush(old_inode->i_mapping); |
| 9080 | |
Yan, Zheng | 76dda93 | 2009-09-21 16:00:26 -0400 | [diff] [blame] | 9081 | /* close the racy window with snapshot create/destroy ioctl */ |
Li Zefan | 33345d01 | 2011-04-20 10:31:50 +0800 | [diff] [blame] | 9082 | if (old_ino == BTRFS_FIRST_FREE_OBJECTID) |
Yan, Zheng | 76dda93 | 2009-09-21 16:00:26 -0400 | [diff] [blame] | 9083 | down_read(&root->fs_info->subvol_sem); |
Yan, Zheng | a22285a | 2010-05-16 10:48:46 -0400 | [diff] [blame] | 9084 | /* |
| 9085 | * We want to reserve the absolute worst case amount of items. So if |
| 9086 | * both inodes are subvols and we need to unlink them then that would |
| 9087 | * require 4 item modifications, but if they are both normal inodes it |
| 9088 | * would require 5 item modifications, so we'll assume their normal |
| 9089 | * inodes. So 5 * 2 is 10, plus 1 for the new link, so 11 total items |
| 9090 | * should cover the worst case number of items we'll modify. |
| 9091 | */ |
Josef Bacik | 6e137ed | 2013-03-26 15:26:55 -0400 | [diff] [blame] | 9092 | trans = btrfs_start_transaction(root, 11); |
Johann Lombardi | b44c59a | 2011-03-31 13:23:47 +0000 | [diff] [blame] | 9093 | if (IS_ERR(trans)) { |
| 9094 | ret = PTR_ERR(trans); |
| 9095 | goto out_notrans; |
| 9096 | } |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 9097 | |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 9098 | if (dest != root) |
| 9099 | btrfs_record_root_in_trans(trans, dest); |
| 9100 | |
Yan, Zheng | a571952 | 2009-09-24 09:17:31 -0400 | [diff] [blame] | 9101 | ret = btrfs_set_inode_index(new_dir, &index); |
| 9102 | if (ret) |
| 9103 | goto out_fail; |
Chris Mason | 5a3f23d | 2009-03-31 13:27:11 -0400 | [diff] [blame] | 9104 | |
Miao Xie | 67de117 | 2013-12-26 13:07:06 +0800 | [diff] [blame] | 9105 | BTRFS_I(old_inode)->dir_index = 0ULL; |
Li Zefan | 33345d01 | 2011-04-20 10:31:50 +0800 | [diff] [blame] | 9106 | if (unlikely(old_ino == BTRFS_FIRST_FREE_OBJECTID)) { |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 9107 | /* force full log commit if subvolume involved. */ |
Miao Xie | 995946d | 2014-04-02 19:51:06 +0800 | [diff] [blame] | 9108 | btrfs_set_log_full_commit(root->fs_info, trans); |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 9109 | } else { |
Yan, Zheng | a571952 | 2009-09-24 09:17:31 -0400 | [diff] [blame] | 9110 | ret = btrfs_insert_inode_ref(trans, dest, |
| 9111 | new_dentry->d_name.name, |
| 9112 | new_dentry->d_name.len, |
Li Zefan | 33345d01 | 2011-04-20 10:31:50 +0800 | [diff] [blame] | 9113 | old_ino, |
| 9114 | btrfs_ino(new_dir), index); |
Yan, Zheng | a571952 | 2009-09-24 09:17:31 -0400 | [diff] [blame] | 9115 | if (ret) |
| 9116 | goto out_fail; |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 9117 | /* |
| 9118 | * this is an ugly little race, but the rename is required |
| 9119 | * to make sure that if we crash, the inode is either at the |
| 9120 | * old name or the new one. pinning the log transaction lets |
| 9121 | * us make sure we don't allow a log commit to come in after |
| 9122 | * we unlink the name but before we add the new name back in. |
| 9123 | */ |
| 9124 | btrfs_pin_log_trans(root); |
| 9125 | } |
Chris Mason | 5a3f23d | 2009-03-31 13:27:11 -0400 | [diff] [blame] | 9126 | |
Josef Bacik | 0c4d2d9 | 2012-04-05 15:03:02 -0400 | [diff] [blame] | 9127 | inode_inc_iversion(old_dir); |
| 9128 | inode_inc_iversion(new_dir); |
| 9129 | inode_inc_iversion(old_inode); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 9130 | old_dir->i_ctime = old_dir->i_mtime = ctime; |
| 9131 | new_dir->i_ctime = new_dir->i_mtime = ctime; |
| 9132 | old_inode->i_ctime = ctime; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 9133 | |
Chris Mason | 12fcfd2 | 2009-03-24 10:24:20 -0400 | [diff] [blame] | 9134 | if (old_dentry->d_parent != new_dentry->d_parent) |
| 9135 | btrfs_record_unlink_dir(trans, old_dir, old_inode, 1); |
| 9136 | |
Li Zefan | 33345d01 | 2011-04-20 10:31:50 +0800 | [diff] [blame] | 9137 | if (unlikely(old_ino == BTRFS_FIRST_FREE_OBJECTID)) { |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 9138 | root_objectid = BTRFS_I(old_inode)->root->root_key.objectid; |
| 9139 | ret = btrfs_unlink_subvol(trans, root, old_dir, root_objectid, |
| 9140 | old_dentry->d_name.name, |
| 9141 | old_dentry->d_name.len); |
| 9142 | } else { |
Al Viro | 9298679 | 2011-03-04 17:14:37 +0000 | [diff] [blame] | 9143 | ret = __btrfs_unlink_inode(trans, root, old_dir, |
| 9144 | old_dentry->d_inode, |
| 9145 | old_dentry->d_name.name, |
| 9146 | old_dentry->d_name.len); |
| 9147 | if (!ret) |
| 9148 | ret = btrfs_update_inode(trans, root, old_inode); |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 9149 | } |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 9150 | if (ret) { |
| 9151 | btrfs_abort_transaction(trans, root, ret); |
| 9152 | goto out_fail; |
| 9153 | } |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 9154 | |
| 9155 | if (new_inode) { |
Josef Bacik | 0c4d2d9 | 2012-04-05 15:03:02 -0400 | [diff] [blame] | 9156 | inode_inc_iversion(new_inode); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 9157 | new_inode->i_ctime = CURRENT_TIME; |
Li Zefan | 33345d01 | 2011-04-20 10:31:50 +0800 | [diff] [blame] | 9158 | if (unlikely(btrfs_ino(new_inode) == |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 9159 | BTRFS_EMPTY_SUBVOL_DIR_OBJECTID)) { |
| 9160 | root_objectid = BTRFS_I(new_inode)->location.objectid; |
| 9161 | ret = btrfs_unlink_subvol(trans, dest, new_dir, |
| 9162 | root_objectid, |
| 9163 | new_dentry->d_name.name, |
| 9164 | new_dentry->d_name.len); |
| 9165 | BUG_ON(new_inode->i_nlink == 0); |
| 9166 | } else { |
| 9167 | ret = btrfs_unlink_inode(trans, dest, new_dir, |
| 9168 | new_dentry->d_inode, |
| 9169 | new_dentry->d_name.name, |
| 9170 | new_dentry->d_name.len); |
| 9171 | } |
Josef Bacik | 4ef31a4 | 2013-08-13 14:10:08 -0400 | [diff] [blame] | 9172 | if (!ret && new_inode->i_nlink == 0) |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 9173 | ret = btrfs_orphan_add(trans, new_dentry->d_inode); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 9174 | if (ret) { |
| 9175 | btrfs_abort_transaction(trans, root, ret); |
| 9176 | goto out_fail; |
| 9177 | } |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 9178 | } |
Josef Bacik | aec7477 | 2008-07-24 12:12:38 -0400 | [diff] [blame] | 9179 | |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 9180 | ret = btrfs_add_link(trans, new_dir, old_inode, |
| 9181 | new_dentry->d_name.name, |
Yan, Zheng | a571952 | 2009-09-24 09:17:31 -0400 | [diff] [blame] | 9182 | new_dentry->d_name.len, 0, index); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 9183 | if (ret) { |
| 9184 | btrfs_abort_transaction(trans, root, ret); |
| 9185 | goto out_fail; |
| 9186 | } |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 9187 | |
Miao Xie | 67de117 | 2013-12-26 13:07:06 +0800 | [diff] [blame] | 9188 | if (old_inode->i_nlink == 1) |
| 9189 | BTRFS_I(old_inode)->dir_index = index; |
| 9190 | |
Li Zefan | 33345d01 | 2011-04-20 10:31:50 +0800 | [diff] [blame] | 9191 | if (old_ino != BTRFS_FIRST_FREE_OBJECTID) { |
Al Viro | 10d9f30 | 2011-07-16 23:09:10 -0400 | [diff] [blame] | 9192 | struct dentry *parent = new_dentry->d_parent; |
Josef Bacik | 6a91221 | 2010-11-20 09:48:00 +0000 | [diff] [blame] | 9193 | btrfs_log_new_name(trans, old_inode, old_dir, parent); |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 9194 | btrfs_end_log_trans(root); |
| 9195 | } |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 9196 | out_fail: |
Josef Bacik | 7ad85bb | 2012-01-12 19:10:12 -0500 | [diff] [blame] | 9197 | btrfs_end_transaction(trans, root); |
Johann Lombardi | b44c59a | 2011-03-31 13:23:47 +0000 | [diff] [blame] | 9198 | out_notrans: |
Li Zefan | 33345d01 | 2011-04-20 10:31:50 +0800 | [diff] [blame] | 9199 | if (old_ino == BTRFS_FIRST_FREE_OBJECTID) |
Yan, Zheng | 76dda93 | 2009-09-21 16:00:26 -0400 | [diff] [blame] | 9200 | up_read(&root->fs_info->subvol_sem); |
Josef Bacik | 9ed74f2 | 2009-09-11 16:12:44 -0400 | [diff] [blame] | 9201 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 9202 | return ret; |
| 9203 | } |
| 9204 | |
Miklos Szeredi | 80ace85 | 2014-07-23 15:15:32 +0200 | [diff] [blame] | 9205 | static int btrfs_rename2(struct inode *old_dir, struct dentry *old_dentry, |
| 9206 | struct inode *new_dir, struct dentry *new_dentry, |
| 9207 | unsigned int flags) |
| 9208 | { |
| 9209 | if (flags & ~RENAME_NOREPLACE) |
| 9210 | return -EINVAL; |
| 9211 | |
| 9212 | return btrfs_rename(old_dir, old_dentry, new_dir, new_dentry); |
| 9213 | } |
| 9214 | |
Miao Xie | 8ccf6f19 | 2012-10-25 09:28:04 +0000 | [diff] [blame] | 9215 | static void btrfs_run_delalloc_work(struct btrfs_work *work) |
| 9216 | { |
| 9217 | struct btrfs_delalloc_work *delalloc_work; |
Josef Bacik | 9f23e28 | 2013-10-28 15:03:41 -0400 | [diff] [blame] | 9218 | struct inode *inode; |
Miao Xie | 8ccf6f19 | 2012-10-25 09:28:04 +0000 | [diff] [blame] | 9219 | |
| 9220 | delalloc_work = container_of(work, struct btrfs_delalloc_work, |
| 9221 | work); |
Josef Bacik | 9f23e28 | 2013-10-28 15:03:41 -0400 | [diff] [blame] | 9222 | inode = delalloc_work->inode; |
| 9223 | if (delalloc_work->wait) { |
| 9224 | btrfs_wait_ordered_range(inode, 0, (u64)-1); |
| 9225 | } else { |
| 9226 | filemap_flush(inode->i_mapping); |
| 9227 | if (test_bit(BTRFS_INODE_HAS_ASYNC_EXTENT, |
| 9228 | &BTRFS_I(inode)->runtime_flags)) |
| 9229 | filemap_flush(inode->i_mapping); |
| 9230 | } |
Miao Xie | 8ccf6f19 | 2012-10-25 09:28:04 +0000 | [diff] [blame] | 9231 | |
| 9232 | if (delalloc_work->delay_iput) |
Josef Bacik | 9f23e28 | 2013-10-28 15:03:41 -0400 | [diff] [blame] | 9233 | btrfs_add_delayed_iput(inode); |
Miao Xie | 8ccf6f19 | 2012-10-25 09:28:04 +0000 | [diff] [blame] | 9234 | else |
Josef Bacik | 9f23e28 | 2013-10-28 15:03:41 -0400 | [diff] [blame] | 9235 | iput(inode); |
Miao Xie | 8ccf6f19 | 2012-10-25 09:28:04 +0000 | [diff] [blame] | 9236 | complete(&delalloc_work->completion); |
| 9237 | } |
| 9238 | |
| 9239 | struct btrfs_delalloc_work *btrfs_alloc_delalloc_work(struct inode *inode, |
| 9240 | int wait, int delay_iput) |
| 9241 | { |
| 9242 | struct btrfs_delalloc_work *work; |
| 9243 | |
| 9244 | work = kmem_cache_zalloc(btrfs_delalloc_work_cachep, GFP_NOFS); |
| 9245 | if (!work) |
| 9246 | return NULL; |
| 9247 | |
| 9248 | init_completion(&work->completion); |
| 9249 | INIT_LIST_HEAD(&work->list); |
| 9250 | work->inode = inode; |
| 9251 | work->wait = wait; |
| 9252 | work->delay_iput = delay_iput; |
Liu Bo | 9e0af23 | 2014-08-15 23:36:53 +0800 | [diff] [blame] | 9253 | WARN_ON_ONCE(!inode); |
| 9254 | btrfs_init_work(&work->work, btrfs_flush_delalloc_helper, |
| 9255 | btrfs_run_delalloc_work, NULL, NULL); |
Miao Xie | 8ccf6f19 | 2012-10-25 09:28:04 +0000 | [diff] [blame] | 9256 | |
| 9257 | return work; |
| 9258 | } |
| 9259 | |
| 9260 | void btrfs_wait_and_free_delalloc_work(struct btrfs_delalloc_work *work) |
| 9261 | { |
| 9262 | wait_for_completion(&work->completion); |
| 9263 | kmem_cache_free(btrfs_delalloc_work_cachep, work); |
| 9264 | } |
| 9265 | |
Chris Mason | d352ac6 | 2008-09-29 15:18:18 -0400 | [diff] [blame] | 9266 | /* |
| 9267 | * some fairly slow code that needs optimization. This walks the list |
| 9268 | * of all the inodes with pending delalloc and forces them to disk. |
| 9269 | */ |
Miao Xie | 6c255e6 | 2014-03-06 13:55:01 +0800 | [diff] [blame] | 9270 | static int __start_delalloc_inodes(struct btrfs_root *root, int delay_iput, |
| 9271 | int nr) |
Chris Mason | ea8c281 | 2008-08-04 23:17:27 -0400 | [diff] [blame] | 9272 | { |
Chris Mason | ea8c281 | 2008-08-04 23:17:27 -0400 | [diff] [blame] | 9273 | struct btrfs_inode *binode; |
Zheng Yan | 5b21f2e | 2008-09-26 10:05:38 -0400 | [diff] [blame] | 9274 | struct inode *inode; |
Miao Xie | 8ccf6f19 | 2012-10-25 09:28:04 +0000 | [diff] [blame] | 9275 | struct btrfs_delalloc_work *work, *next; |
| 9276 | struct list_head works; |
Miao Xie | 1eafa6c | 2013-01-22 10:49:00 +0000 | [diff] [blame] | 9277 | struct list_head splice; |
Miao Xie | 8ccf6f19 | 2012-10-25 09:28:04 +0000 | [diff] [blame] | 9278 | int ret = 0; |
Chris Mason | ea8c281 | 2008-08-04 23:17:27 -0400 | [diff] [blame] | 9279 | |
Miao Xie | 8ccf6f19 | 2012-10-25 09:28:04 +0000 | [diff] [blame] | 9280 | INIT_LIST_HEAD(&works); |
Miao Xie | 1eafa6c | 2013-01-22 10:49:00 +0000 | [diff] [blame] | 9281 | INIT_LIST_HEAD(&splice); |
Miao Xie | 63607cc | 2013-01-22 10:50:35 +0000 | [diff] [blame] | 9282 | |
Miao Xie | 573bfb7 | 2014-03-06 13:55:03 +0800 | [diff] [blame] | 9283 | mutex_lock(&root->delalloc_mutex); |
Miao Xie | eb73c1b | 2013-05-15 07:48:22 +0000 | [diff] [blame] | 9284 | spin_lock(&root->delalloc_lock); |
| 9285 | list_splice_init(&root->delalloc_inodes, &splice); |
Miao Xie | 1eafa6c | 2013-01-22 10:49:00 +0000 | [diff] [blame] | 9286 | while (!list_empty(&splice)) { |
| 9287 | binode = list_entry(splice.next, struct btrfs_inode, |
Chris Mason | ea8c281 | 2008-08-04 23:17:27 -0400 | [diff] [blame] | 9288 | delalloc_inodes); |
Miao Xie | 1eafa6c | 2013-01-22 10:49:00 +0000 | [diff] [blame] | 9289 | |
Miao Xie | eb73c1b | 2013-05-15 07:48:22 +0000 | [diff] [blame] | 9290 | list_move_tail(&binode->delalloc_inodes, |
| 9291 | &root->delalloc_inodes); |
Zheng Yan | 5b21f2e | 2008-09-26 10:05:38 -0400 | [diff] [blame] | 9292 | inode = igrab(&binode->vfs_inode); |
Miao Xie | df0af1a | 2013-01-29 10:11:59 +0000 | [diff] [blame] | 9293 | if (!inode) { |
Miao Xie | eb73c1b | 2013-05-15 07:48:22 +0000 | [diff] [blame] | 9294 | cond_resched_lock(&root->delalloc_lock); |
Miao Xie | 1eafa6c | 2013-01-22 10:49:00 +0000 | [diff] [blame] | 9295 | continue; |
Miao Xie | df0af1a | 2013-01-29 10:11:59 +0000 | [diff] [blame] | 9296 | } |
Miao Xie | eb73c1b | 2013-05-15 07:48:22 +0000 | [diff] [blame] | 9297 | spin_unlock(&root->delalloc_lock); |
Miao Xie | 1eafa6c | 2013-01-22 10:49:00 +0000 | [diff] [blame] | 9298 | |
| 9299 | work = btrfs_alloc_delalloc_work(inode, 0, delay_iput); |
David Sterba | 5d99a998 | 2014-09-29 19:20:37 +0200 | [diff] [blame] | 9300 | if (!work) { |
Josef Bacik | f4ab9ea | 2013-09-17 11:25:44 -0400 | [diff] [blame] | 9301 | if (delay_iput) |
| 9302 | btrfs_add_delayed_iput(inode); |
| 9303 | else |
| 9304 | iput(inode); |
Miao Xie | 1eafa6c | 2013-01-22 10:49:00 +0000 | [diff] [blame] | 9305 | ret = -ENOMEM; |
Wang Shilong | a1ecaab | 2014-04-02 19:53:32 +0800 | [diff] [blame] | 9306 | goto out; |
Zheng Yan | 5b21f2e | 2008-09-26 10:05:38 -0400 | [diff] [blame] | 9307 | } |
Miao Xie | 1eafa6c | 2013-01-22 10:49:00 +0000 | [diff] [blame] | 9308 | list_add_tail(&work->list, &works); |
Qu Wenruo | a44903a | 2014-02-28 10:46:09 +0800 | [diff] [blame] | 9309 | btrfs_queue_work(root->fs_info->flush_workers, |
| 9310 | &work->work); |
Miao Xie | 6c255e6 | 2014-03-06 13:55:01 +0800 | [diff] [blame] | 9311 | ret++; |
| 9312 | if (nr != -1 && ret >= nr) |
Wang Shilong | a1ecaab | 2014-04-02 19:53:32 +0800 | [diff] [blame] | 9313 | goto out; |
Zheng Yan | 5b21f2e | 2008-09-26 10:05:38 -0400 | [diff] [blame] | 9314 | cond_resched(); |
Miao Xie | eb73c1b | 2013-05-15 07:48:22 +0000 | [diff] [blame] | 9315 | spin_lock(&root->delalloc_lock); |
Chris Mason | ea8c281 | 2008-08-04 23:17:27 -0400 | [diff] [blame] | 9316 | } |
Miao Xie | eb73c1b | 2013-05-15 07:48:22 +0000 | [diff] [blame] | 9317 | spin_unlock(&root->delalloc_lock); |
Chris Mason | 8c8bee1 | 2008-09-29 11:19:10 -0400 | [diff] [blame] | 9318 | |
Wang Shilong | a1ecaab | 2014-04-02 19:53:32 +0800 | [diff] [blame] | 9319 | out: |
Miao Xie | 1eafa6c | 2013-01-22 10:49:00 +0000 | [diff] [blame] | 9320 | list_for_each_entry_safe(work, next, &works, list) { |
| 9321 | list_del_init(&work->list); |
| 9322 | btrfs_wait_and_free_delalloc_work(work); |
| 9323 | } |
| 9324 | |
Miao Xie | eb73c1b | 2013-05-15 07:48:22 +0000 | [diff] [blame] | 9325 | if (!list_empty_careful(&splice)) { |
| 9326 | spin_lock(&root->delalloc_lock); |
| 9327 | list_splice_tail(&splice, &root->delalloc_inodes); |
| 9328 | spin_unlock(&root->delalloc_lock); |
| 9329 | } |
Miao Xie | 573bfb7 | 2014-03-06 13:55:03 +0800 | [diff] [blame] | 9330 | mutex_unlock(&root->delalloc_mutex); |
Miao Xie | eb73c1b | 2013-05-15 07:48:22 +0000 | [diff] [blame] | 9331 | return ret; |
| 9332 | } |
| 9333 | |
| 9334 | int btrfs_start_delalloc_inodes(struct btrfs_root *root, int delay_iput) |
| 9335 | { |
| 9336 | int ret; |
| 9337 | |
Wang Shilong | 2c21b4d | 2014-01-14 19:42:20 +0800 | [diff] [blame] | 9338 | if (test_bit(BTRFS_FS_STATE_ERROR, &root->fs_info->fs_state)) |
Miao Xie | eb73c1b | 2013-05-15 07:48:22 +0000 | [diff] [blame] | 9339 | return -EROFS; |
| 9340 | |
Miao Xie | 6c255e6 | 2014-03-06 13:55:01 +0800 | [diff] [blame] | 9341 | ret = __start_delalloc_inodes(root, delay_iput, -1); |
| 9342 | if (ret > 0) |
| 9343 | ret = 0; |
Miao Xie | eb73c1b | 2013-05-15 07:48:22 +0000 | [diff] [blame] | 9344 | /* |
| 9345 | * the filemap_flush will queue IO into the worker threads, but |
Chris Mason | 8c8bee1 | 2008-09-29 11:19:10 -0400 | [diff] [blame] | 9346 | * we have to make sure the IO is actually started and that |
| 9347 | * ordered extents get created before we return |
| 9348 | */ |
| 9349 | atomic_inc(&root->fs_info->async_submit_draining); |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 9350 | while (atomic_read(&root->fs_info->nr_async_submits) || |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 9351 | atomic_read(&root->fs_info->async_delalloc_pages)) { |
Chris Mason | 8c8bee1 | 2008-09-29 11:19:10 -0400 | [diff] [blame] | 9352 | wait_event(root->fs_info->async_submit_wait, |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 9353 | (atomic_read(&root->fs_info->nr_async_submits) == 0 && |
| 9354 | atomic_read(&root->fs_info->async_delalloc_pages) == 0)); |
Chris Mason | 8c8bee1 | 2008-09-29 11:19:10 -0400 | [diff] [blame] | 9355 | } |
| 9356 | atomic_dec(&root->fs_info->async_submit_draining); |
Miao Xie | eb73c1b | 2013-05-15 07:48:22 +0000 | [diff] [blame] | 9357 | return ret; |
| 9358 | } |
| 9359 | |
Miao Xie | 6c255e6 | 2014-03-06 13:55:01 +0800 | [diff] [blame] | 9360 | int btrfs_start_delalloc_roots(struct btrfs_fs_info *fs_info, int delay_iput, |
| 9361 | int nr) |
Miao Xie | eb73c1b | 2013-05-15 07:48:22 +0000 | [diff] [blame] | 9362 | { |
| 9363 | struct btrfs_root *root; |
| 9364 | struct list_head splice; |
| 9365 | int ret; |
| 9366 | |
Wang Shilong | 2c21b4d | 2014-01-14 19:42:20 +0800 | [diff] [blame] | 9367 | if (test_bit(BTRFS_FS_STATE_ERROR, &fs_info->fs_state)) |
Miao Xie | eb73c1b | 2013-05-15 07:48:22 +0000 | [diff] [blame] | 9368 | return -EROFS; |
| 9369 | |
| 9370 | INIT_LIST_HEAD(&splice); |
| 9371 | |
Miao Xie | 573bfb7 | 2014-03-06 13:55:03 +0800 | [diff] [blame] | 9372 | mutex_lock(&fs_info->delalloc_root_mutex); |
Miao Xie | eb73c1b | 2013-05-15 07:48:22 +0000 | [diff] [blame] | 9373 | spin_lock(&fs_info->delalloc_root_lock); |
| 9374 | list_splice_init(&fs_info->delalloc_roots, &splice); |
Miao Xie | 6c255e6 | 2014-03-06 13:55:01 +0800 | [diff] [blame] | 9375 | while (!list_empty(&splice) && nr) { |
Miao Xie | eb73c1b | 2013-05-15 07:48:22 +0000 | [diff] [blame] | 9376 | root = list_first_entry(&splice, struct btrfs_root, |
| 9377 | delalloc_root); |
| 9378 | root = btrfs_grab_fs_root(root); |
| 9379 | BUG_ON(!root); |
| 9380 | list_move_tail(&root->delalloc_root, |
| 9381 | &fs_info->delalloc_roots); |
| 9382 | spin_unlock(&fs_info->delalloc_root_lock); |
| 9383 | |
Miao Xie | 6c255e6 | 2014-03-06 13:55:01 +0800 | [diff] [blame] | 9384 | ret = __start_delalloc_inodes(root, delay_iput, nr); |
Miao Xie | eb73c1b | 2013-05-15 07:48:22 +0000 | [diff] [blame] | 9385 | btrfs_put_fs_root(root); |
Miao Xie | 6c255e6 | 2014-03-06 13:55:01 +0800 | [diff] [blame] | 9386 | if (ret < 0) |
Miao Xie | eb73c1b | 2013-05-15 07:48:22 +0000 | [diff] [blame] | 9387 | goto out; |
| 9388 | |
Miao Xie | 6c255e6 | 2014-03-06 13:55:01 +0800 | [diff] [blame] | 9389 | if (nr != -1) { |
| 9390 | nr -= ret; |
| 9391 | WARN_ON(nr < 0); |
| 9392 | } |
Miao Xie | eb73c1b | 2013-05-15 07:48:22 +0000 | [diff] [blame] | 9393 | spin_lock(&fs_info->delalloc_root_lock); |
| 9394 | } |
| 9395 | spin_unlock(&fs_info->delalloc_root_lock); |
| 9396 | |
Miao Xie | 6c255e6 | 2014-03-06 13:55:01 +0800 | [diff] [blame] | 9397 | ret = 0; |
Miao Xie | eb73c1b | 2013-05-15 07:48:22 +0000 | [diff] [blame] | 9398 | atomic_inc(&fs_info->async_submit_draining); |
| 9399 | while (atomic_read(&fs_info->nr_async_submits) || |
| 9400 | atomic_read(&fs_info->async_delalloc_pages)) { |
| 9401 | wait_event(fs_info->async_submit_wait, |
| 9402 | (atomic_read(&fs_info->nr_async_submits) == 0 && |
| 9403 | atomic_read(&fs_info->async_delalloc_pages) == 0)); |
| 9404 | } |
| 9405 | atomic_dec(&fs_info->async_submit_draining); |
Miao Xie | 8ccf6f19 | 2012-10-25 09:28:04 +0000 | [diff] [blame] | 9406 | out: |
Miao Xie | 1eafa6c | 2013-01-22 10:49:00 +0000 | [diff] [blame] | 9407 | if (!list_empty_careful(&splice)) { |
Miao Xie | eb73c1b | 2013-05-15 07:48:22 +0000 | [diff] [blame] | 9408 | spin_lock(&fs_info->delalloc_root_lock); |
| 9409 | list_splice_tail(&splice, &fs_info->delalloc_roots); |
| 9410 | spin_unlock(&fs_info->delalloc_root_lock); |
Miao Xie | 1eafa6c | 2013-01-22 10:49:00 +0000 | [diff] [blame] | 9411 | } |
Miao Xie | 573bfb7 | 2014-03-06 13:55:03 +0800 | [diff] [blame] | 9412 | mutex_unlock(&fs_info->delalloc_root_mutex); |
Miao Xie | 8ccf6f19 | 2012-10-25 09:28:04 +0000 | [diff] [blame] | 9413 | return ret; |
Chris Mason | ea8c281 | 2008-08-04 23:17:27 -0400 | [diff] [blame] | 9414 | } |
| 9415 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 9416 | static int btrfs_symlink(struct inode *dir, struct dentry *dentry, |
| 9417 | const char *symname) |
| 9418 | { |
| 9419 | struct btrfs_trans_handle *trans; |
| 9420 | struct btrfs_root *root = BTRFS_I(dir)->root; |
| 9421 | struct btrfs_path *path; |
| 9422 | struct btrfs_key key; |
Chris Mason | 1832a6d | 2007-12-21 16:27:21 -0500 | [diff] [blame] | 9423 | struct inode *inode = NULL; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 9424 | int err; |
| 9425 | int drop_inode = 0; |
| 9426 | u64 objectid; |
Dulshani Gunawardhana | 6787125 | 2013-10-31 10:33:04 +0530 | [diff] [blame] | 9427 | u64 index = 0; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 9428 | int name_len; |
| 9429 | int datasize; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 9430 | unsigned long ptr; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 9431 | struct btrfs_file_extent_item *ei; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 9432 | struct extent_buffer *leaf; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 9433 | |
Filipe David Borba Manana | f06becc | 2013-09-16 09:53:28 +0100 | [diff] [blame] | 9434 | name_len = strlen(symname); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 9435 | if (name_len > BTRFS_MAX_INLINE_DATA_SIZE(root)) |
| 9436 | return -ENAMETOOLONG; |
Chris Mason | 1832a6d | 2007-12-21 16:27:21 -0500 | [diff] [blame] | 9437 | |
Josef Bacik | 9ed74f2 | 2009-09-11 16:12:44 -0400 | [diff] [blame] | 9438 | /* |
| 9439 | * 2 items for inode item and ref |
| 9440 | * 2 items for dir items |
| 9441 | * 1 item for xattr if selinux is on |
| 9442 | */ |
Yan, Zheng | a22285a | 2010-05-16 10:48:46 -0400 | [diff] [blame] | 9443 | trans = btrfs_start_transaction(root, 5); |
| 9444 | if (IS_ERR(trans)) |
| 9445 | return PTR_ERR(trans); |
Chris Mason | 1832a6d | 2007-12-21 16:27:21 -0500 | [diff] [blame] | 9446 | |
Li Zefan | 581bb05 | 2011-04-20 10:06:11 +0800 | [diff] [blame] | 9447 | err = btrfs_find_free_ino(root, &objectid); |
| 9448 | if (err) |
| 9449 | goto out_unlock; |
| 9450 | |
Josef Bacik | aec7477 | 2008-07-24 12:12:38 -0400 | [diff] [blame] | 9451 | inode = btrfs_new_inode(trans, root, dir, dentry->d_name.name, |
Li Zefan | 33345d01 | 2011-04-20 10:31:50 +0800 | [diff] [blame] | 9452 | dentry->d_name.len, btrfs_ino(dir), objectid, |
Josef Bacik | d82a6f1d | 2011-05-11 15:26:06 -0400 | [diff] [blame] | 9453 | S_IFLNK|S_IRWXUGO, &index); |
Tsutomu Itoh | 7cf96da | 2011-04-25 19:43:53 -0400 | [diff] [blame] | 9454 | if (IS_ERR(inode)) { |
| 9455 | err = PTR_ERR(inode); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 9456 | goto out_unlock; |
Tsutomu Itoh | 7cf96da | 2011-04-25 19:43:53 -0400 | [diff] [blame] | 9457 | } |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 9458 | |
Casey Schaufler | ad19db7 | 2011-12-15 10:09:07 -0500 | [diff] [blame] | 9459 | /* |
| 9460 | * If the active LSM wants to access the inode during |
| 9461 | * d_instantiate it needs these. Smack checks to see |
| 9462 | * if the filesystem supports xattrs by looking at the |
| 9463 | * ops vector. |
| 9464 | */ |
| 9465 | inode->i_fop = &btrfs_file_operations; |
| 9466 | inode->i_op = &btrfs_file_inode_operations; |
Chris Mason | b0d5d10 | 2014-09-08 13:08:51 -0700 | [diff] [blame] | 9467 | inode->i_mapping->a_ops = &btrfs_aops; |
Chris Mason | b0d5d10 | 2014-09-08 13:08:51 -0700 | [diff] [blame] | 9468 | BTRFS_I(inode)->io_tree.ops = &btrfs_extent_io_ops; |
| 9469 | |
| 9470 | err = btrfs_init_inode_security(trans, inode, dir, &dentry->d_name); |
| 9471 | if (err) |
| 9472 | goto out_unlock_inode; |
Casey Schaufler | ad19db7 | 2011-12-15 10:09:07 -0500 | [diff] [blame] | 9473 | |
Josef Bacik | a1b075d | 2010-11-19 20:36:11 +0000 | [diff] [blame] | 9474 | err = btrfs_add_nondir(trans, dir, dentry, inode, 0, index); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 9475 | if (err) |
Chris Mason | b0d5d10 | 2014-09-08 13:08:51 -0700 | [diff] [blame] | 9476 | goto out_unlock_inode; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 9477 | |
| 9478 | path = btrfs_alloc_path(); |
Mark Fasheh | d8926bb | 2011-07-13 10:38:47 -0700 | [diff] [blame] | 9479 | if (!path) { |
| 9480 | err = -ENOMEM; |
Chris Mason | b0d5d10 | 2014-09-08 13:08:51 -0700 | [diff] [blame] | 9481 | goto out_unlock_inode; |
Mark Fasheh | d8926bb | 2011-07-13 10:38:47 -0700 | [diff] [blame] | 9482 | } |
Li Zefan | 33345d01 | 2011-04-20 10:31:50 +0800 | [diff] [blame] | 9483 | key.objectid = btrfs_ino(inode); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 9484 | key.offset = 0; |
David Sterba | 962a298 | 2014-06-04 18:41:45 +0200 | [diff] [blame] | 9485 | key.type = BTRFS_EXTENT_DATA_KEY; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 9486 | datasize = btrfs_file_extent_calc_inline_size(name_len); |
| 9487 | err = btrfs_insert_empty_item(trans, root, path, &key, |
| 9488 | datasize); |
Chris Mason | 54aa1f4 | 2007-06-22 14:16:25 -0400 | [diff] [blame] | 9489 | if (err) { |
Julia Lawall | b083916 | 2011-05-14 07:10:51 +0000 | [diff] [blame] | 9490 | btrfs_free_path(path); |
Chris Mason | b0d5d10 | 2014-09-08 13:08:51 -0700 | [diff] [blame] | 9491 | goto out_unlock_inode; |
Chris Mason | 54aa1f4 | 2007-06-22 14:16:25 -0400 | [diff] [blame] | 9492 | } |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 9493 | leaf = path->nodes[0]; |
| 9494 | ei = btrfs_item_ptr(leaf, path->slots[0], |
| 9495 | struct btrfs_file_extent_item); |
| 9496 | btrfs_set_file_extent_generation(leaf, ei, trans->transid); |
| 9497 | btrfs_set_file_extent_type(leaf, ei, |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 9498 | BTRFS_FILE_EXTENT_INLINE); |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 9499 | btrfs_set_file_extent_encryption(leaf, ei, 0); |
| 9500 | btrfs_set_file_extent_compression(leaf, ei, 0); |
| 9501 | btrfs_set_file_extent_other_encoding(leaf, ei, 0); |
| 9502 | btrfs_set_file_extent_ram_bytes(leaf, ei, name_len); |
| 9503 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 9504 | ptr = btrfs_file_extent_inline_start(ei); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 9505 | write_extent_buffer(leaf, symname, ptr, name_len); |
| 9506 | btrfs_mark_buffer_dirty(leaf); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 9507 | btrfs_free_path(path); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 9508 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 9509 | inode->i_op = &btrfs_symlink_inode_operations; |
| 9510 | inode->i_mapping->a_ops = &btrfs_symlink_aops; |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 9511 | inode_set_bytes(inode, name_len); |
Filipe David Borba Manana | f06becc | 2013-09-16 09:53:28 +0100 | [diff] [blame] | 9512 | btrfs_i_size_write(inode, name_len); |
Chris Mason | 54aa1f4 | 2007-06-22 14:16:25 -0400 | [diff] [blame] | 9513 | err = btrfs_update_inode(trans, root, inode); |
Chris Mason | b0d5d10 | 2014-09-08 13:08:51 -0700 | [diff] [blame] | 9514 | if (err) { |
Chris Mason | 54aa1f4 | 2007-06-22 14:16:25 -0400 | [diff] [blame] | 9515 | drop_inode = 1; |
Chris Mason | b0d5d10 | 2014-09-08 13:08:51 -0700 | [diff] [blame] | 9516 | goto out_unlock_inode; |
| 9517 | } |
| 9518 | |
| 9519 | unlock_new_inode(inode); |
| 9520 | d_instantiate(dentry, inode); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 9521 | |
| 9522 | out_unlock: |
Josef Bacik | 7ad85bb | 2012-01-12 19:10:12 -0500 | [diff] [blame] | 9523 | btrfs_end_transaction(trans, root); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 9524 | if (drop_inode) { |
| 9525 | inode_dec_link_count(inode); |
| 9526 | iput(inode); |
| 9527 | } |
Liu Bo | b53d3f5 | 2012-11-14 14:34:34 +0000 | [diff] [blame] | 9528 | btrfs_btree_balance_dirty(root); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 9529 | return err; |
Chris Mason | b0d5d10 | 2014-09-08 13:08:51 -0700 | [diff] [blame] | 9530 | |
| 9531 | out_unlock_inode: |
| 9532 | drop_inode = 1; |
| 9533 | unlock_new_inode(inode); |
| 9534 | goto out_unlock; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 9535 | } |
Chris Mason | 1643298 | 2008-04-10 10:23:21 -0400 | [diff] [blame] | 9536 | |
Josef Bacik | 0af3d00 | 2010-06-21 14:48:16 -0400 | [diff] [blame] | 9537 | static int __btrfs_prealloc_file_range(struct inode *inode, int mode, |
| 9538 | u64 start, u64 num_bytes, u64 min_size, |
| 9539 | loff_t actual_len, u64 *alloc_hint, |
| 9540 | struct btrfs_trans_handle *trans) |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 9541 | { |
Josef Bacik | 5dc562c | 2012-08-17 13:14:17 -0400 | [diff] [blame] | 9542 | struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree; |
| 9543 | struct extent_map *em; |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 9544 | struct btrfs_root *root = BTRFS_I(inode)->root; |
| 9545 | struct btrfs_key ins; |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 9546 | u64 cur_offset = start; |
Josef Bacik | 55a61d1 | 2010-11-22 18:50:32 +0000 | [diff] [blame] | 9547 | u64 i_size; |
Chris Mason | 154ea28 | 2013-03-05 11:11:26 -0500 | [diff] [blame] | 9548 | u64 cur_bytes; |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 9549 | int ret = 0; |
Josef Bacik | 0af3d00 | 2010-06-21 14:48:16 -0400 | [diff] [blame] | 9550 | bool own_trans = true; |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 9551 | |
Josef Bacik | 0af3d00 | 2010-06-21 14:48:16 -0400 | [diff] [blame] | 9552 | if (trans) |
| 9553 | own_trans = false; |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 9554 | while (num_bytes > 0) { |
Josef Bacik | 0af3d00 | 2010-06-21 14:48:16 -0400 | [diff] [blame] | 9555 | if (own_trans) { |
| 9556 | trans = btrfs_start_transaction(root, 3); |
| 9557 | if (IS_ERR(trans)) { |
| 9558 | ret = PTR_ERR(trans); |
| 9559 | break; |
| 9560 | } |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 9561 | } |
Yan, Zheng | 5a303d5 | 2009-11-12 09:34:52 +0000 | [diff] [blame] | 9562 | |
Chris Mason | 154ea28 | 2013-03-05 11:11:26 -0500 | [diff] [blame] | 9563 | cur_bytes = min(num_bytes, 256ULL * 1024 * 1024); |
| 9564 | cur_bytes = max(cur_bytes, min_size); |
Josef Bacik | 0036158 | 2013-08-14 14:02:47 -0400 | [diff] [blame] | 9565 | ret = btrfs_reserve_extent(root, cur_bytes, min_size, 0, |
Miao Xie | e570fd2 | 2014-06-19 10:42:50 +0800 | [diff] [blame] | 9566 | *alloc_hint, &ins, 1, 0); |
Yan, Zheng | 5a303d5 | 2009-11-12 09:34:52 +0000 | [diff] [blame] | 9567 | if (ret) { |
Josef Bacik | 0af3d00 | 2010-06-21 14:48:16 -0400 | [diff] [blame] | 9568 | if (own_trans) |
| 9569 | btrfs_end_transaction(trans, root); |
Yan, Zheng | a22285a | 2010-05-16 10:48:46 -0400 | [diff] [blame] | 9570 | break; |
Yan, Zheng | 5a303d5 | 2009-11-12 09:34:52 +0000 | [diff] [blame] | 9571 | } |
| 9572 | |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 9573 | ret = insert_reserved_file_extent(trans, inode, |
| 9574 | cur_offset, ins.objectid, |
| 9575 | ins.offset, ins.offset, |
Yan, Zheng | 920bbbf | 2009-11-12 09:34:08 +0000 | [diff] [blame] | 9576 | ins.offset, 0, 0, 0, |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 9577 | BTRFS_FILE_EXTENT_PREALLOC); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 9578 | if (ret) { |
Josef Bacik | 857cc2f | 2013-10-07 15:21:08 -0400 | [diff] [blame] | 9579 | btrfs_free_reserved_extent(root, ins.objectid, |
Miao Xie | e570fd2 | 2014-06-19 10:42:50 +0800 | [diff] [blame] | 9580 | ins.offset, 0); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 9581 | btrfs_abort_transaction(trans, root, ret); |
| 9582 | if (own_trans) |
| 9583 | btrfs_end_transaction(trans, root); |
| 9584 | break; |
| 9585 | } |
Chris Mason | a1ed835 | 2009-09-11 12:27:37 -0400 | [diff] [blame] | 9586 | btrfs_drop_extent_cache(inode, cur_offset, |
| 9587 | cur_offset + ins.offset -1, 0); |
Yan, Zheng | 5a303d5 | 2009-11-12 09:34:52 +0000 | [diff] [blame] | 9588 | |
Josef Bacik | 5dc562c | 2012-08-17 13:14:17 -0400 | [diff] [blame] | 9589 | em = alloc_extent_map(); |
| 9590 | if (!em) { |
| 9591 | set_bit(BTRFS_INODE_NEEDS_FULL_SYNC, |
| 9592 | &BTRFS_I(inode)->runtime_flags); |
| 9593 | goto next; |
| 9594 | } |
| 9595 | |
| 9596 | em->start = cur_offset; |
| 9597 | em->orig_start = cur_offset; |
| 9598 | em->len = ins.offset; |
| 9599 | em->block_start = ins.objectid; |
| 9600 | em->block_len = ins.offset; |
Josef Bacik | b493968 | 2012-12-03 10:31:19 -0500 | [diff] [blame] | 9601 | em->orig_block_len = ins.offset; |
Josef Bacik | cc95bef | 2013-04-04 14:31:27 -0400 | [diff] [blame] | 9602 | em->ram_bytes = ins.offset; |
Josef Bacik | 5dc562c | 2012-08-17 13:14:17 -0400 | [diff] [blame] | 9603 | em->bdev = root->fs_info->fs_devices->latest_bdev; |
| 9604 | set_bit(EXTENT_FLAG_PREALLOC, &em->flags); |
| 9605 | em->generation = trans->transid; |
| 9606 | |
| 9607 | while (1) { |
| 9608 | write_lock(&em_tree->lock); |
Josef Bacik | 09a2a8f9 | 2013-04-05 16:51:15 -0400 | [diff] [blame] | 9609 | ret = add_extent_mapping(em_tree, em, 1); |
Josef Bacik | 5dc562c | 2012-08-17 13:14:17 -0400 | [diff] [blame] | 9610 | write_unlock(&em_tree->lock); |
| 9611 | if (ret != -EEXIST) |
| 9612 | break; |
| 9613 | btrfs_drop_extent_cache(inode, cur_offset, |
| 9614 | cur_offset + ins.offset - 1, |
| 9615 | 0); |
| 9616 | } |
| 9617 | free_extent_map(em); |
| 9618 | next: |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 9619 | num_bytes -= ins.offset; |
| 9620 | cur_offset += ins.offset; |
Yan, Zheng | efa5646 | 2010-05-16 10:49:59 -0400 | [diff] [blame] | 9621 | *alloc_hint = ins.objectid + ins.offset; |
Yan, Zheng | 5a303d5 | 2009-11-12 09:34:52 +0000 | [diff] [blame] | 9622 | |
Josef Bacik | 0c4d2d9 | 2012-04-05 15:03:02 -0400 | [diff] [blame] | 9623 | inode_inc_iversion(inode); |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 9624 | inode->i_ctime = CURRENT_TIME; |
Christoph Hellwig | 6cbff00 | 2009-04-17 10:37:41 +0200 | [diff] [blame] | 9625 | BTRFS_I(inode)->flags |= BTRFS_INODE_PREALLOC; |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 9626 | if (!(mode & FALLOC_FL_KEEP_SIZE) && |
Yan, Zheng | efa5646 | 2010-05-16 10:49:59 -0400 | [diff] [blame] | 9627 | (actual_len > inode->i_size) && |
| 9628 | (cur_offset > inode->i_size)) { |
Aneesh Kumar K.V | d1ea6a6 | 2010-01-20 07:28:54 +0000 | [diff] [blame] | 9629 | if (cur_offset > actual_len) |
Josef Bacik | 55a61d1 | 2010-11-22 18:50:32 +0000 | [diff] [blame] | 9630 | i_size = actual_len; |
Aneesh Kumar K.V | d1ea6a6 | 2010-01-20 07:28:54 +0000 | [diff] [blame] | 9631 | else |
Josef Bacik | 55a61d1 | 2010-11-22 18:50:32 +0000 | [diff] [blame] | 9632 | i_size = cur_offset; |
| 9633 | i_size_write(inode, i_size); |
| 9634 | btrfs_ordered_update_i_size(inode, i_size, NULL); |
Yan, Zheng | 5a303d5 | 2009-11-12 09:34:52 +0000 | [diff] [blame] | 9635 | } |
| 9636 | |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 9637 | ret = btrfs_update_inode(trans, root, inode); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 9638 | |
| 9639 | if (ret) { |
| 9640 | btrfs_abort_transaction(trans, root, ret); |
| 9641 | if (own_trans) |
| 9642 | btrfs_end_transaction(trans, root); |
| 9643 | break; |
| 9644 | } |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 9645 | |
Josef Bacik | 0af3d00 | 2010-06-21 14:48:16 -0400 | [diff] [blame] | 9646 | if (own_trans) |
| 9647 | btrfs_end_transaction(trans, root); |
Yan, Zheng | 5a303d5 | 2009-11-12 09:34:52 +0000 | [diff] [blame] | 9648 | } |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 9649 | return ret; |
| 9650 | } |
| 9651 | |
Josef Bacik | 0af3d00 | 2010-06-21 14:48:16 -0400 | [diff] [blame] | 9652 | int btrfs_prealloc_file_range(struct inode *inode, int mode, |
| 9653 | u64 start, u64 num_bytes, u64 min_size, |
| 9654 | loff_t actual_len, u64 *alloc_hint) |
| 9655 | { |
| 9656 | return __btrfs_prealloc_file_range(inode, mode, start, num_bytes, |
| 9657 | min_size, actual_len, alloc_hint, |
| 9658 | NULL); |
| 9659 | } |
| 9660 | |
| 9661 | int btrfs_prealloc_file_range_trans(struct inode *inode, |
| 9662 | struct btrfs_trans_handle *trans, int mode, |
| 9663 | u64 start, u64 num_bytes, u64 min_size, |
| 9664 | loff_t actual_len, u64 *alloc_hint) |
| 9665 | { |
| 9666 | return __btrfs_prealloc_file_range(inode, mode, start, num_bytes, |
| 9667 | min_size, actual_len, alloc_hint, trans); |
| 9668 | } |
| 9669 | |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 9670 | static int btrfs_set_page_dirty(struct page *page) |
| 9671 | { |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 9672 | return __set_page_dirty_nobuffers(page); |
| 9673 | } |
| 9674 | |
Al Viro | 10556cb | 2011-06-20 19:28:19 -0400 | [diff] [blame] | 9675 | static int btrfs_permission(struct inode *inode, int mask) |
Yan | fdebe2b | 2008-01-14 13:26:08 -0500 | [diff] [blame] | 9676 | { |
Li Zefan | b83cc96 | 2010-12-20 16:04:08 +0800 | [diff] [blame] | 9677 | struct btrfs_root *root = BTRFS_I(inode)->root; |
Jeff Mahoney | cb6db4e | 2011-08-15 17:27:21 +0000 | [diff] [blame] | 9678 | umode_t mode = inode->i_mode; |
Li Zefan | b83cc96 | 2010-12-20 16:04:08 +0800 | [diff] [blame] | 9679 | |
Jeff Mahoney | cb6db4e | 2011-08-15 17:27:21 +0000 | [diff] [blame] | 9680 | if (mask & MAY_WRITE && |
| 9681 | (S_ISREG(mode) || S_ISDIR(mode) || S_ISLNK(mode))) { |
| 9682 | if (btrfs_root_readonly(root)) |
| 9683 | return -EROFS; |
| 9684 | if (BTRFS_I(inode)->flags & BTRFS_INODE_READONLY) |
| 9685 | return -EACCES; |
| 9686 | } |
Al Viro | 2830ba7 | 2011-06-20 19:16:29 -0400 | [diff] [blame] | 9687 | return generic_permission(inode, mask); |
Yan | fdebe2b | 2008-01-14 13:26:08 -0500 | [diff] [blame] | 9688 | } |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 9689 | |
Filipe Manana | ef3b9af | 2014-04-27 20:40:45 +0100 | [diff] [blame] | 9690 | static int btrfs_tmpfile(struct inode *dir, struct dentry *dentry, umode_t mode) |
| 9691 | { |
| 9692 | struct btrfs_trans_handle *trans; |
| 9693 | struct btrfs_root *root = BTRFS_I(dir)->root; |
| 9694 | struct inode *inode = NULL; |
| 9695 | u64 objectid; |
| 9696 | u64 index; |
| 9697 | int ret = 0; |
| 9698 | |
| 9699 | /* |
| 9700 | * 5 units required for adding orphan entry |
| 9701 | */ |
| 9702 | trans = btrfs_start_transaction(root, 5); |
| 9703 | if (IS_ERR(trans)) |
| 9704 | return PTR_ERR(trans); |
| 9705 | |
| 9706 | ret = btrfs_find_free_ino(root, &objectid); |
| 9707 | if (ret) |
| 9708 | goto out; |
| 9709 | |
| 9710 | inode = btrfs_new_inode(trans, root, dir, NULL, 0, |
| 9711 | btrfs_ino(dir), objectid, mode, &index); |
| 9712 | if (IS_ERR(inode)) { |
| 9713 | ret = PTR_ERR(inode); |
| 9714 | inode = NULL; |
| 9715 | goto out; |
| 9716 | } |
| 9717 | |
Filipe Manana | ef3b9af | 2014-04-27 20:40:45 +0100 | [diff] [blame] | 9718 | inode->i_fop = &btrfs_file_operations; |
| 9719 | inode->i_op = &btrfs_file_inode_operations; |
| 9720 | |
| 9721 | inode->i_mapping->a_ops = &btrfs_aops; |
Filipe Manana | ef3b9af | 2014-04-27 20:40:45 +0100 | [diff] [blame] | 9722 | BTRFS_I(inode)->io_tree.ops = &btrfs_extent_io_ops; |
| 9723 | |
Chris Mason | b0d5d10 | 2014-09-08 13:08:51 -0700 | [diff] [blame] | 9724 | ret = btrfs_init_inode_security(trans, inode, dir, NULL); |
| 9725 | if (ret) |
| 9726 | goto out_inode; |
| 9727 | |
| 9728 | ret = btrfs_update_inode(trans, root, inode); |
| 9729 | if (ret) |
| 9730 | goto out_inode; |
Filipe Manana | ef3b9af | 2014-04-27 20:40:45 +0100 | [diff] [blame] | 9731 | ret = btrfs_orphan_add(trans, inode); |
| 9732 | if (ret) |
Chris Mason | b0d5d10 | 2014-09-08 13:08:51 -0700 | [diff] [blame] | 9733 | goto out_inode; |
Filipe Manana | ef3b9af | 2014-04-27 20:40:45 +0100 | [diff] [blame] | 9734 | |
Filipe Manana | 5762b5c | 2014-08-01 00:10:32 +0100 | [diff] [blame] | 9735 | /* |
| 9736 | * We set number of links to 0 in btrfs_new_inode(), and here we set |
| 9737 | * it to 1 because d_tmpfile() will issue a warning if the count is 0, |
| 9738 | * through: |
| 9739 | * |
| 9740 | * d_tmpfile() -> inode_dec_link_count() -> drop_nlink() |
| 9741 | */ |
| 9742 | set_nlink(inode, 1); |
Chris Mason | b0d5d10 | 2014-09-08 13:08:51 -0700 | [diff] [blame] | 9743 | unlock_new_inode(inode); |
Filipe Manana | ef3b9af | 2014-04-27 20:40:45 +0100 | [diff] [blame] | 9744 | d_tmpfile(dentry, inode); |
| 9745 | mark_inode_dirty(inode); |
| 9746 | |
| 9747 | out: |
| 9748 | btrfs_end_transaction(trans, root); |
| 9749 | if (ret) |
| 9750 | iput(inode); |
| 9751 | btrfs_balance_delayed_items(root); |
| 9752 | btrfs_btree_balance_dirty(root); |
Filipe Manana | ef3b9af | 2014-04-27 20:40:45 +0100 | [diff] [blame] | 9753 | return ret; |
Chris Mason | b0d5d10 | 2014-09-08 13:08:51 -0700 | [diff] [blame] | 9754 | |
| 9755 | out_inode: |
| 9756 | unlock_new_inode(inode); |
| 9757 | goto out; |
| 9758 | |
Filipe Manana | ef3b9af | 2014-04-27 20:40:45 +0100 | [diff] [blame] | 9759 | } |
| 9760 | |
Filipe Manana | b38ef71 | 2014-11-13 17:01:45 +0000 | [diff] [blame] | 9761 | /* Inspired by filemap_check_errors() */ |
| 9762 | int btrfs_inode_check_errors(struct inode *inode) |
| 9763 | { |
| 9764 | int ret = 0; |
| 9765 | |
| 9766 | if (test_bit(AS_ENOSPC, &inode->i_mapping->flags) && |
| 9767 | test_and_clear_bit(AS_ENOSPC, &inode->i_mapping->flags)) |
| 9768 | ret = -ENOSPC; |
| 9769 | if (test_bit(AS_EIO, &inode->i_mapping->flags) && |
| 9770 | test_and_clear_bit(AS_EIO, &inode->i_mapping->flags)) |
| 9771 | ret = -EIO; |
| 9772 | |
| 9773 | return ret; |
| 9774 | } |
| 9775 | |
Alexey Dobriyan | 6e1d5dc | 2009-09-21 17:01:11 -0700 | [diff] [blame] | 9776 | static const struct inode_operations btrfs_dir_inode_operations = { |
Chris Mason | 3394e16 | 2008-11-17 20:42:26 -0500 | [diff] [blame] | 9777 | .getattr = btrfs_getattr, |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 9778 | .lookup = btrfs_lookup, |
| 9779 | .create = btrfs_create, |
| 9780 | .unlink = btrfs_unlink, |
| 9781 | .link = btrfs_link, |
| 9782 | .mkdir = btrfs_mkdir, |
| 9783 | .rmdir = btrfs_rmdir, |
Miklos Szeredi | 80ace85 | 2014-07-23 15:15:32 +0200 | [diff] [blame] | 9784 | .rename2 = btrfs_rename2, |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 9785 | .symlink = btrfs_symlink, |
| 9786 | .setattr = btrfs_setattr, |
Josef Bacik | 618e21d | 2007-07-11 10:18:17 -0400 | [diff] [blame] | 9787 | .mknod = btrfs_mknod, |
Christoph Hellwig | 95819c0 | 2008-08-28 06:21:17 -0400 | [diff] [blame] | 9788 | .setxattr = btrfs_setxattr, |
| 9789 | .getxattr = btrfs_getxattr, |
Josef Bacik | 5103e94 | 2007-11-16 11:45:54 -0500 | [diff] [blame] | 9790 | .listxattr = btrfs_listxattr, |
Christoph Hellwig | 95819c0 | 2008-08-28 06:21:17 -0400 | [diff] [blame] | 9791 | .removexattr = btrfs_removexattr, |
Yan | fdebe2b | 2008-01-14 13:26:08 -0500 | [diff] [blame] | 9792 | .permission = btrfs_permission, |
Christoph Hellwig | 4e34e71 | 2011-07-23 17:37:31 +0200 | [diff] [blame] | 9793 | .get_acl = btrfs_get_acl, |
Christoph Hellwig | 996a710 | 2013-12-20 05:16:43 -0800 | [diff] [blame] | 9794 | .set_acl = btrfs_set_acl, |
Guangyu Sun | 93fd63c | 2013-09-16 10:42:03 -0700 | [diff] [blame] | 9795 | .update_time = btrfs_update_time, |
Filipe Manana | ef3b9af | 2014-04-27 20:40:45 +0100 | [diff] [blame] | 9796 | .tmpfile = btrfs_tmpfile, |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 9797 | }; |
Alexey Dobriyan | 6e1d5dc | 2009-09-21 17:01:11 -0700 | [diff] [blame] | 9798 | static const struct inode_operations btrfs_dir_ro_inode_operations = { |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 9799 | .lookup = btrfs_lookup, |
Yan | fdebe2b | 2008-01-14 13:26:08 -0500 | [diff] [blame] | 9800 | .permission = btrfs_permission, |
Christoph Hellwig | 4e34e71 | 2011-07-23 17:37:31 +0200 | [diff] [blame] | 9801 | .get_acl = btrfs_get_acl, |
Christoph Hellwig | 996a710 | 2013-12-20 05:16:43 -0800 | [diff] [blame] | 9802 | .set_acl = btrfs_set_acl, |
Guangyu Sun | 93fd63c | 2013-09-16 10:42:03 -0700 | [diff] [blame] | 9803 | .update_time = btrfs_update_time, |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 9804 | }; |
Yan, Zheng | 76dda93 | 2009-09-21 16:00:26 -0400 | [diff] [blame] | 9805 | |
Alexey Dobriyan | 828c095 | 2009-10-01 15:43:56 -0700 | [diff] [blame] | 9806 | static const struct file_operations btrfs_dir_file_operations = { |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 9807 | .llseek = generic_file_llseek, |
| 9808 | .read = generic_read_dir, |
Al Viro | 9cdda8d | 2013-05-22 16:48:09 -0400 | [diff] [blame] | 9809 | .iterate = btrfs_real_readdir, |
Christoph Hellwig | 34287aa | 2007-09-14 10:22:47 -0400 | [diff] [blame] | 9810 | .unlocked_ioctl = btrfs_ioctl, |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 9811 | #ifdef CONFIG_COMPAT |
Christoph Hellwig | 34287aa | 2007-09-14 10:22:47 -0400 | [diff] [blame] | 9812 | .compat_ioctl = btrfs_ioctl, |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 9813 | #endif |
Sage Weil | 6bf13c0 | 2008-06-10 10:07:39 -0400 | [diff] [blame] | 9814 | .release = btrfs_release_file, |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 9815 | .fsync = btrfs_sync_file, |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 9816 | }; |
| 9817 | |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 9818 | static struct extent_io_ops btrfs_extent_io_ops = { |
Chris Mason | 07157aa | 2007-08-30 08:50:51 -0400 | [diff] [blame] | 9819 | .fill_delalloc = run_delalloc_range, |
Chris Mason | 065631f | 2008-02-20 12:07:25 -0500 | [diff] [blame] | 9820 | .submit_bio_hook = btrfs_submit_bio_hook, |
Chris Mason | 239b14b | 2008-03-24 15:02:07 -0400 | [diff] [blame] | 9821 | .merge_bio_hook = btrfs_merge_bio_hook, |
Chris Mason | 07157aa | 2007-08-30 08:50:51 -0400 | [diff] [blame] | 9822 | .readpage_end_io_hook = btrfs_readpage_end_io_hook, |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 9823 | .writepage_end_io_hook = btrfs_writepage_end_io_hook, |
Chris Mason | 247e743 | 2008-07-17 12:53:51 -0400 | [diff] [blame] | 9824 | .writepage_start_hook = btrfs_writepage_start_hook, |
Chris Mason | b0c68f8 | 2008-01-31 11:05:37 -0500 | [diff] [blame] | 9825 | .set_bit_hook = btrfs_set_bit_hook, |
| 9826 | .clear_bit_hook = btrfs_clear_bit_hook, |
Josef Bacik | 9ed74f2 | 2009-09-11 16:12:44 -0400 | [diff] [blame] | 9827 | .merge_extent_hook = btrfs_merge_extent_hook, |
| 9828 | .split_extent_hook = btrfs_split_extent_hook, |
Chris Mason | 07157aa | 2007-08-30 08:50:51 -0400 | [diff] [blame] | 9829 | }; |
| 9830 | |
Chris Mason | 3505439 | 2009-01-21 13:11:13 -0500 | [diff] [blame] | 9831 | /* |
| 9832 | * btrfs doesn't support the bmap operation because swapfiles |
| 9833 | * use bmap to make a mapping of extents in the file. They assume |
| 9834 | * these extents won't change over the life of the file and they |
| 9835 | * use the bmap result to do IO directly to the drive. |
| 9836 | * |
| 9837 | * the btrfs bmap call would return logical addresses that aren't |
| 9838 | * suitable for IO and they also will change frequently as COW |
| 9839 | * operations happen. So, swapfile + btrfs == corruption. |
| 9840 | * |
| 9841 | * For now we're avoiding this by dropping bmap. |
| 9842 | */ |
Alexey Dobriyan | 7f09410 | 2009-09-21 17:01:10 -0700 | [diff] [blame] | 9843 | static const struct address_space_operations btrfs_aops = { |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 9844 | .readpage = btrfs_readpage, |
| 9845 | .writepage = btrfs_writepage, |
Chris Mason | b293f02e | 2007-11-01 19:45:34 -0400 | [diff] [blame] | 9846 | .writepages = btrfs_writepages, |
Chris Mason | 3ab2fb5 | 2007-11-08 10:59:22 -0500 | [diff] [blame] | 9847 | .readpages = btrfs_readpages, |
Chris Mason | 1643298 | 2008-04-10 10:23:21 -0400 | [diff] [blame] | 9848 | .direct_IO = btrfs_direct_IO, |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 9849 | .invalidatepage = btrfs_invalidatepage, |
| 9850 | .releasepage = btrfs_releasepage, |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 9851 | .set_page_dirty = btrfs_set_page_dirty, |
Andi Kleen | 465fdd9 | 2009-09-16 11:50:18 +0200 | [diff] [blame] | 9852 | .error_remove_page = generic_error_remove_page, |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 9853 | }; |
| 9854 | |
Alexey Dobriyan | 7f09410 | 2009-09-21 17:01:10 -0700 | [diff] [blame] | 9855 | static const struct address_space_operations btrfs_symlink_aops = { |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 9856 | .readpage = btrfs_readpage, |
| 9857 | .writepage = btrfs_writepage, |
Chris Mason | 2bf5a72 | 2007-08-30 11:54:02 -0400 | [diff] [blame] | 9858 | .invalidatepage = btrfs_invalidatepage, |
| 9859 | .releasepage = btrfs_releasepage, |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 9860 | }; |
| 9861 | |
Alexey Dobriyan | 6e1d5dc | 2009-09-21 17:01:11 -0700 | [diff] [blame] | 9862 | static const struct inode_operations btrfs_file_inode_operations = { |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 9863 | .getattr = btrfs_getattr, |
| 9864 | .setattr = btrfs_setattr, |
Christoph Hellwig | 95819c0 | 2008-08-28 06:21:17 -0400 | [diff] [blame] | 9865 | .setxattr = btrfs_setxattr, |
| 9866 | .getxattr = btrfs_getxattr, |
Josef Bacik | 5103e94 | 2007-11-16 11:45:54 -0500 | [diff] [blame] | 9867 | .listxattr = btrfs_listxattr, |
Christoph Hellwig | 95819c0 | 2008-08-28 06:21:17 -0400 | [diff] [blame] | 9868 | .removexattr = btrfs_removexattr, |
Yan | fdebe2b | 2008-01-14 13:26:08 -0500 | [diff] [blame] | 9869 | .permission = btrfs_permission, |
Yehuda Sadeh | 1506fcc | 2009-01-21 14:39:14 -0500 | [diff] [blame] | 9870 | .fiemap = btrfs_fiemap, |
Christoph Hellwig | 4e34e71 | 2011-07-23 17:37:31 +0200 | [diff] [blame] | 9871 | .get_acl = btrfs_get_acl, |
Christoph Hellwig | 996a710 | 2013-12-20 05:16:43 -0800 | [diff] [blame] | 9872 | .set_acl = btrfs_set_acl, |
Josef Bacik | e41f941 | 2012-03-26 09:46:47 -0400 | [diff] [blame] | 9873 | .update_time = btrfs_update_time, |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 9874 | }; |
Alexey Dobriyan | 6e1d5dc | 2009-09-21 17:01:11 -0700 | [diff] [blame] | 9875 | static const struct inode_operations btrfs_special_inode_operations = { |
Josef Bacik | 618e21d | 2007-07-11 10:18:17 -0400 | [diff] [blame] | 9876 | .getattr = btrfs_getattr, |
| 9877 | .setattr = btrfs_setattr, |
Yan | fdebe2b | 2008-01-14 13:26:08 -0500 | [diff] [blame] | 9878 | .permission = btrfs_permission, |
Christoph Hellwig | 95819c0 | 2008-08-28 06:21:17 -0400 | [diff] [blame] | 9879 | .setxattr = btrfs_setxattr, |
| 9880 | .getxattr = btrfs_getxattr, |
Josef Bacik | 33268ea | 2008-07-24 12:16:36 -0400 | [diff] [blame] | 9881 | .listxattr = btrfs_listxattr, |
Christoph Hellwig | 95819c0 | 2008-08-28 06:21:17 -0400 | [diff] [blame] | 9882 | .removexattr = btrfs_removexattr, |
Christoph Hellwig | 4e34e71 | 2011-07-23 17:37:31 +0200 | [diff] [blame] | 9883 | .get_acl = btrfs_get_acl, |
Christoph Hellwig | 996a710 | 2013-12-20 05:16:43 -0800 | [diff] [blame] | 9884 | .set_acl = btrfs_set_acl, |
Josef Bacik | e41f941 | 2012-03-26 09:46:47 -0400 | [diff] [blame] | 9885 | .update_time = btrfs_update_time, |
Josef Bacik | 618e21d | 2007-07-11 10:18:17 -0400 | [diff] [blame] | 9886 | }; |
Alexey Dobriyan | 6e1d5dc | 2009-09-21 17:01:11 -0700 | [diff] [blame] | 9887 | static const struct inode_operations btrfs_symlink_inode_operations = { |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 9888 | .readlink = generic_readlink, |
| 9889 | .follow_link = page_follow_link_light, |
| 9890 | .put_link = page_put_link, |
Li Zefan | f209561 | 2010-11-19 02:05:24 +0000 | [diff] [blame] | 9891 | .getattr = btrfs_getattr, |
Josef Bacik | 22c44fe | 2011-11-30 10:45:38 -0500 | [diff] [blame] | 9892 | .setattr = btrfs_setattr, |
Yan | fdebe2b | 2008-01-14 13:26:08 -0500 | [diff] [blame] | 9893 | .permission = btrfs_permission, |
Jim Owens | 0279b4c | 2009-02-04 09:29:13 -0500 | [diff] [blame] | 9894 | .setxattr = btrfs_setxattr, |
| 9895 | .getxattr = btrfs_getxattr, |
| 9896 | .listxattr = btrfs_listxattr, |
| 9897 | .removexattr = btrfs_removexattr, |
Josef Bacik | e41f941 | 2012-03-26 09:46:47 -0400 | [diff] [blame] | 9898 | .update_time = btrfs_update_time, |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 9899 | }; |
Yan, Zheng | 76dda93 | 2009-09-21 16:00:26 -0400 | [diff] [blame] | 9900 | |
Alexey Dobriyan | 82d339d | 2009-10-09 09:54:36 -0400 | [diff] [blame] | 9901 | const struct dentry_operations btrfs_dentry_operations = { |
Yan, Zheng | 76dda93 | 2009-09-21 16:00:26 -0400 | [diff] [blame] | 9902 | .d_delete = btrfs_dentry_delete, |
Josef Bacik | b4aff1f | 2011-06-28 16:18:59 -0400 | [diff] [blame] | 9903 | .d_release = btrfs_dentry_release, |
Yan, Zheng | 76dda93 | 2009-09-21 16:00:26 -0400 | [diff] [blame] | 9904 | }; |