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> |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 46 | #include "ctree.h" |
| 47 | #include "disk-io.h" |
| 48 | #include "transaction.h" |
| 49 | #include "btrfs_inode.h" |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 50 | #include "print-tree.h" |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 51 | #include "ordered-data.h" |
Christoph Hellwig | 95819c0 | 2008-08-28 06:21:17 -0400 | [diff] [blame] | 52 | #include "xattr.h" |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 53 | #include "tree-log.h" |
Jan Schmidt | 4a54c8c | 2011-07-22 15:41:52 +0200 | [diff] [blame] | 54 | #include "volumes.h" |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 55 | #include "compression.h" |
Chris Mason | b4ce94d | 2009-02-04 09:25:08 -0500 | [diff] [blame] | 56 | #include "locking.h" |
Josef Bacik | dc89e98 | 2011-01-28 17:05:48 -0500 | [diff] [blame] | 57 | #include "free-space-cache.h" |
Li Zefan | 581bb05 | 2011-04-20 10:06:11 +0800 | [diff] [blame] | 58 | #include "inode-map.h" |
Liu Bo | 38c227d | 2013-01-29 03:18:40 +0000 | [diff] [blame] | 59 | #include "backref.h" |
Josef Bacik | f23b5a5 | 2013-06-19 10:16:26 -0400 | [diff] [blame] | 60 | #include "hash.h" |
Filipe David Borba Manana | 6354192 | 2014-01-07 11:47:46 +0000 | [diff] [blame] | 61 | #include "props.h" |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 62 | |
| 63 | struct btrfs_iget_args { |
Chris Mason | 90d3e59 | 2014-01-09 17:28:00 -0800 | [diff] [blame] | 64 | struct btrfs_key *location; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 65 | struct btrfs_root *root; |
| 66 | }; |
| 67 | |
Alexey Dobriyan | 6e1d5dc | 2009-09-21 17:01:11 -0700 | [diff] [blame] | 68 | static const struct inode_operations btrfs_dir_inode_operations; |
| 69 | static const struct inode_operations btrfs_symlink_inode_operations; |
| 70 | static const struct inode_operations btrfs_dir_ro_inode_operations; |
| 71 | static const struct inode_operations btrfs_special_inode_operations; |
| 72 | static const struct inode_operations btrfs_file_inode_operations; |
Alexey Dobriyan | 7f09410 | 2009-09-21 17:01:10 -0700 | [diff] [blame] | 73 | static const struct address_space_operations btrfs_aops; |
| 74 | static const struct address_space_operations btrfs_symlink_aops; |
Alexey Dobriyan | 828c095 | 2009-10-01 15:43:56 -0700 | [diff] [blame] | 75 | static const struct file_operations btrfs_dir_file_operations; |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 76 | static struct extent_io_ops btrfs_extent_io_ops; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 77 | |
| 78 | static struct kmem_cache *btrfs_inode_cachep; |
Miao Xie | 8ccf6f19 | 2012-10-25 09:28:04 +0000 | [diff] [blame] | 79 | static struct kmem_cache *btrfs_delalloc_work_cachep; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 80 | struct kmem_cache *btrfs_trans_handle_cachep; |
| 81 | struct kmem_cache *btrfs_transaction_cachep; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 82 | struct kmem_cache *btrfs_path_cachep; |
Josef Bacik | dc89e98 | 2011-01-28 17:05:48 -0500 | [diff] [blame] | 83 | struct kmem_cache *btrfs_free_space_cachep; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 84 | |
| 85 | #define S_SHIFT 12 |
| 86 | static unsigned char btrfs_type_by_mode[S_IFMT >> S_SHIFT] = { |
| 87 | [S_IFREG >> S_SHIFT] = BTRFS_FT_REG_FILE, |
| 88 | [S_IFDIR >> S_SHIFT] = BTRFS_FT_DIR, |
| 89 | [S_IFCHR >> S_SHIFT] = BTRFS_FT_CHRDEV, |
| 90 | [S_IFBLK >> S_SHIFT] = BTRFS_FT_BLKDEV, |
| 91 | [S_IFIFO >> S_SHIFT] = BTRFS_FT_FIFO, |
| 92 | [S_IFSOCK >> S_SHIFT] = BTRFS_FT_SOCK, |
| 93 | [S_IFLNK >> S_SHIFT] = BTRFS_FT_SYMLINK, |
| 94 | }; |
| 95 | |
Eric Sandeen | 3972f26 | 2013-01-12 02:57:22 +0000 | [diff] [blame] | 96 | static int btrfs_setsize(struct inode *inode, struct iattr *attr); |
Josef Bacik | a41ad39 | 2011-01-31 15:30:16 -0500 | [diff] [blame] | 97 | static int btrfs_truncate(struct inode *inode); |
Josef Bacik | 5fd0204 | 2012-05-02 14:00:54 -0400 | [diff] [blame] | 98 | static int btrfs_finish_ordered_io(struct btrfs_ordered_extent *ordered_extent); |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 99 | static noinline int cow_file_range(struct inode *inode, |
| 100 | struct page *locked_page, |
| 101 | u64 start, u64 end, int *page_started, |
| 102 | unsigned long *nr_written, int unlock); |
Josef Bacik | 70c8a91 | 2012-10-11 16:54:30 -0400 | [diff] [blame] | 103 | static struct extent_map *create_pinned_em(struct inode *inode, u64 start, |
| 104 | u64 len, u64 orig_start, |
| 105 | u64 block_start, u64 block_len, |
Josef Bacik | cc95bef | 2013-04-04 14:31:27 -0400 | [diff] [blame] | 106 | u64 orig_block_len, u64 ram_bytes, |
| 107 | int type); |
Josef Bacik | 7b12876 | 2008-07-24 12:17:14 -0400 | [diff] [blame] | 108 | |
Eric Sandeen | 48a3b63 | 2013-04-25 20:41:01 +0000 | [diff] [blame] | 109 | static int btrfs_dirty_inode(struct inode *inode); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 110 | |
Josef Bacik | 6a3891c | 2015-03-16 17:38:52 -0400 | [diff] [blame] | 111 | #ifdef CONFIG_BTRFS_FS_RUN_SANITY_TESTS |
| 112 | void btrfs_test_inode_set_ops(struct inode *inode) |
| 113 | { |
| 114 | BTRFS_I(inode)->io_tree.ops = &btrfs_extent_io_ops; |
| 115 | } |
| 116 | #endif |
| 117 | |
Yan, Zheng | f34f57a | 2009-11-12 09:35:27 +0000 | [diff] [blame] | 118 | static int btrfs_init_inode_security(struct btrfs_trans_handle *trans, |
Eric Paris | 2a7dba3 | 2011-02-01 11:05:39 -0500 | [diff] [blame] | 119 | struct inode *inode, struct inode *dir, |
| 120 | const struct qstr *qstr) |
Jim Owens | 0279b4c | 2009-02-04 09:29:13 -0500 | [diff] [blame] | 121 | { |
| 122 | int err; |
| 123 | |
Yan, Zheng | f34f57a | 2009-11-12 09:35:27 +0000 | [diff] [blame] | 124 | err = btrfs_init_acl(trans, inode, dir); |
Jim Owens | 0279b4c | 2009-02-04 09:29:13 -0500 | [diff] [blame] | 125 | if (!err) |
Eric Paris | 2a7dba3 | 2011-02-01 11:05:39 -0500 | [diff] [blame] | 126 | err = btrfs_xattr_security_init(trans, inode, dir, qstr); |
Jim Owens | 0279b4c | 2009-02-04 09:29:13 -0500 | [diff] [blame] | 127 | return err; |
| 128 | } |
| 129 | |
Chris Mason | d352ac6 | 2008-09-29 15:18:18 -0400 | [diff] [blame] | 130 | /* |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 131 | * this does all the hard work for inserting an inline extent into |
| 132 | * the btree. The caller should have done a btrfs_drop_extents so that |
| 133 | * no overlapping inline items exist in the btree |
| 134 | */ |
Chris Mason | 40f7658 | 2014-05-21 13:35:51 -0700 | [diff] [blame] | 135 | static int insert_inline_extent(struct btrfs_trans_handle *trans, |
Filipe David Borba Manana | 1acae57 | 2014-01-07 11:42:27 +0000 | [diff] [blame] | 136 | struct btrfs_path *path, int extent_inserted, |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 137 | struct btrfs_root *root, struct inode *inode, |
| 138 | u64 start, size_t size, size_t compressed_size, |
Li Zefan | fe3f566 | 2011-03-28 08:30:38 +0000 | [diff] [blame] | 139 | int compress_type, |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 140 | struct page **compressed_pages) |
| 141 | { |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 142 | struct extent_buffer *leaf; |
| 143 | struct page *page = NULL; |
| 144 | char *kaddr; |
| 145 | unsigned long ptr; |
| 146 | struct btrfs_file_extent_item *ei; |
| 147 | int err = 0; |
| 148 | int ret; |
| 149 | size_t cur_size = size; |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 150 | unsigned long offset; |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 151 | |
Li Zefan | fe3f566 | 2011-03-28 08:30:38 +0000 | [diff] [blame] | 152 | if (compressed_size && compressed_pages) |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 153 | cur_size = compressed_size; |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 154 | |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 155 | inode_add_bytes(inode, size); |
Filipe David Borba Manana | 1acae57 | 2014-01-07 11:42:27 +0000 | [diff] [blame] | 156 | |
| 157 | if (!extent_inserted) { |
| 158 | struct btrfs_key key; |
| 159 | size_t datasize; |
| 160 | |
| 161 | key.objectid = btrfs_ino(inode); |
| 162 | key.offset = start; |
David Sterba | 962a298 | 2014-06-04 18:41:45 +0200 | [diff] [blame] | 163 | key.type = BTRFS_EXTENT_DATA_KEY; |
Filipe David Borba Manana | 1acae57 | 2014-01-07 11:42:27 +0000 | [diff] [blame] | 164 | |
| 165 | datasize = btrfs_file_extent_calc_inline_size(cur_size); |
| 166 | path->leave_spinning = 1; |
| 167 | ret = btrfs_insert_empty_item(trans, root, path, &key, |
| 168 | datasize); |
| 169 | if (ret) { |
| 170 | err = ret; |
| 171 | goto fail; |
| 172 | } |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 173 | } |
| 174 | leaf = path->nodes[0]; |
| 175 | ei = btrfs_item_ptr(leaf, path->slots[0], |
| 176 | struct btrfs_file_extent_item); |
| 177 | btrfs_set_file_extent_generation(leaf, ei, trans->transid); |
| 178 | btrfs_set_file_extent_type(leaf, ei, BTRFS_FILE_EXTENT_INLINE); |
| 179 | btrfs_set_file_extent_encryption(leaf, ei, 0); |
| 180 | btrfs_set_file_extent_other_encoding(leaf, ei, 0); |
| 181 | btrfs_set_file_extent_ram_bytes(leaf, ei, size); |
| 182 | ptr = btrfs_file_extent_inline_start(ei); |
| 183 | |
Li Zefan | 261507a0 | 2010-12-17 14:21:50 +0800 | [diff] [blame] | 184 | if (compress_type != BTRFS_COMPRESS_NONE) { |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 185 | struct page *cpage; |
| 186 | int i = 0; |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 187 | while (compressed_size > 0) { |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 188 | cpage = compressed_pages[i]; |
Chris Mason | 5b050f0 | 2008-11-11 09:34:41 -0500 | [diff] [blame] | 189 | cur_size = min_t(unsigned long, compressed_size, |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 190 | PAGE_CACHE_SIZE); |
| 191 | |
Cong Wang | 7ac687d | 2011-11-25 23:14:28 +0800 | [diff] [blame] | 192 | kaddr = kmap_atomic(cpage); |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 193 | write_extent_buffer(leaf, kaddr, ptr, cur_size); |
Cong Wang | 7ac687d | 2011-11-25 23:14:28 +0800 | [diff] [blame] | 194 | kunmap_atomic(kaddr); |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 195 | |
| 196 | i++; |
| 197 | ptr += cur_size; |
| 198 | compressed_size -= cur_size; |
| 199 | } |
| 200 | btrfs_set_file_extent_compression(leaf, ei, |
Li Zefan | 261507a0 | 2010-12-17 14:21:50 +0800 | [diff] [blame] | 201 | compress_type); |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 202 | } else { |
| 203 | page = find_get_page(inode->i_mapping, |
| 204 | start >> PAGE_CACHE_SHIFT); |
| 205 | btrfs_set_file_extent_compression(leaf, ei, 0); |
Cong Wang | 7ac687d | 2011-11-25 23:14:28 +0800 | [diff] [blame] | 206 | kaddr = kmap_atomic(page); |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 207 | offset = start & (PAGE_CACHE_SIZE - 1); |
| 208 | write_extent_buffer(leaf, kaddr + offset, ptr, size); |
Cong Wang | 7ac687d | 2011-11-25 23:14:28 +0800 | [diff] [blame] | 209 | kunmap_atomic(kaddr); |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 210 | page_cache_release(page); |
| 211 | } |
| 212 | btrfs_mark_buffer_dirty(leaf); |
Filipe David Borba Manana | 1acae57 | 2014-01-07 11:42:27 +0000 | [diff] [blame] | 213 | btrfs_release_path(path); |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 214 | |
Yan, Zheng | c216775 | 2009-11-12 09:34:21 +0000 | [diff] [blame] | 215 | /* |
| 216 | * we're an inline extent, so nobody can |
| 217 | * extend the file past i_size without locking |
| 218 | * a page we already have locked. |
| 219 | * |
| 220 | * We must do any isize and inode updates |
| 221 | * before we unlock the pages. Otherwise we |
| 222 | * could end up racing with unlink. |
| 223 | */ |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 224 | BTRFS_I(inode)->disk_i_size = inode->i_size; |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 225 | ret = btrfs_update_inode(trans, root, inode); |
Yan, Zheng | c216775 | 2009-11-12 09:34:21 +0000 | [diff] [blame] | 226 | |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 227 | return ret; |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 228 | fail: |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 229 | return err; |
| 230 | } |
| 231 | |
| 232 | |
| 233 | /* |
| 234 | * conditionally insert an inline extent into the file. This |
| 235 | * does the checks required to make sure the data is small enough |
| 236 | * to fit as an inline extent. |
| 237 | */ |
Josef Bacik | 0036158 | 2013-08-14 14:02:47 -0400 | [diff] [blame] | 238 | static noinline int cow_file_range_inline(struct btrfs_root *root, |
| 239 | struct inode *inode, u64 start, |
| 240 | u64 end, size_t compressed_size, |
| 241 | int compress_type, |
| 242 | struct page **compressed_pages) |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 243 | { |
Josef Bacik | 0036158 | 2013-08-14 14:02:47 -0400 | [diff] [blame] | 244 | struct btrfs_trans_handle *trans; |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 245 | u64 isize = i_size_read(inode); |
| 246 | u64 actual_end = min(end + 1, isize); |
| 247 | u64 inline_len = actual_end - start; |
Qu Wenruo | fda2832 | 2013-02-26 08:10:22 +0000 | [diff] [blame] | 248 | u64 aligned_end = ALIGN(end, root->sectorsize); |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 249 | u64 data_len = inline_len; |
| 250 | int ret; |
Filipe David Borba Manana | 1acae57 | 2014-01-07 11:42:27 +0000 | [diff] [blame] | 251 | struct btrfs_path *path; |
| 252 | int extent_inserted = 0; |
| 253 | u32 extent_item_size; |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 254 | |
| 255 | if (compressed_size) |
| 256 | data_len = compressed_size; |
| 257 | |
| 258 | if (start > 0 || |
Wang Shilong | 354877b | 2014-07-17 11:44:11 +0800 | [diff] [blame] | 259 | actual_end > PAGE_CACHE_SIZE || |
| 260 | data_len > BTRFS_MAX_INLINE_DATA_SIZE(root) || |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 261 | (!compressed_size && |
| 262 | (actual_end & (root->sectorsize - 1)) == 0) || |
| 263 | end + 1 < isize || |
| 264 | data_len > root->fs_info->max_inline) { |
| 265 | return 1; |
| 266 | } |
| 267 | |
Filipe David Borba Manana | 1acae57 | 2014-01-07 11:42:27 +0000 | [diff] [blame] | 268 | path = btrfs_alloc_path(); |
| 269 | if (!path) |
| 270 | return -ENOMEM; |
| 271 | |
Josef Bacik | 0036158 | 2013-08-14 14:02:47 -0400 | [diff] [blame] | 272 | trans = btrfs_join_transaction(root); |
Filipe David Borba Manana | 1acae57 | 2014-01-07 11:42:27 +0000 | [diff] [blame] | 273 | if (IS_ERR(trans)) { |
| 274 | btrfs_free_path(path); |
Josef Bacik | 0036158 | 2013-08-14 14:02:47 -0400 | [diff] [blame] | 275 | return PTR_ERR(trans); |
Filipe David Borba Manana | 1acae57 | 2014-01-07 11:42:27 +0000 | [diff] [blame] | 276 | } |
Josef Bacik | 0036158 | 2013-08-14 14:02:47 -0400 | [diff] [blame] | 277 | trans->block_rsv = &root->fs_info->delalloc_block_rsv; |
| 278 | |
Filipe David Borba Manana | 1acae57 | 2014-01-07 11:42:27 +0000 | [diff] [blame] | 279 | if (compressed_size && compressed_pages) |
| 280 | extent_item_size = btrfs_file_extent_calc_inline_size( |
| 281 | compressed_size); |
| 282 | else |
| 283 | extent_item_size = btrfs_file_extent_calc_inline_size( |
| 284 | inline_len); |
| 285 | |
| 286 | ret = __btrfs_drop_extents(trans, root, inode, path, |
| 287 | start, aligned_end, NULL, |
| 288 | 1, 1, extent_item_size, &extent_inserted); |
Josef Bacik | 0036158 | 2013-08-14 14:02:47 -0400 | [diff] [blame] | 289 | if (ret) { |
| 290 | btrfs_abort_transaction(trans, root, ret); |
| 291 | goto out; |
| 292 | } |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 293 | |
| 294 | if (isize > actual_end) |
| 295 | inline_len = min_t(u64, isize, actual_end); |
Filipe David Borba Manana | 1acae57 | 2014-01-07 11:42:27 +0000 | [diff] [blame] | 296 | ret = insert_inline_extent(trans, path, extent_inserted, |
| 297 | root, inode, start, |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 298 | inline_len, compressed_size, |
Li Zefan | fe3f566 | 2011-03-28 08:30:38 +0000 | [diff] [blame] | 299 | compress_type, compressed_pages); |
Josef Bacik | 2adcac1 | 2012-05-23 16:10:14 -0400 | [diff] [blame] | 300 | if (ret && ret != -ENOSPC) { |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 301 | btrfs_abort_transaction(trans, root, ret); |
Josef Bacik | 0036158 | 2013-08-14 14:02:47 -0400 | [diff] [blame] | 302 | goto out; |
Josef Bacik | 2adcac1 | 2012-05-23 16:10:14 -0400 | [diff] [blame] | 303 | } else if (ret == -ENOSPC) { |
Josef Bacik | 0036158 | 2013-08-14 14:02:47 -0400 | [diff] [blame] | 304 | ret = 1; |
| 305 | goto out; |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 306 | } |
Josef Bacik | 2adcac1 | 2012-05-23 16:10:14 -0400 | [diff] [blame] | 307 | |
Josef Bacik | bdc20e6 | 2013-02-28 13:23:38 -0500 | [diff] [blame] | 308 | set_bit(BTRFS_INODE_NEEDS_FULL_SYNC, &BTRFS_I(inode)->runtime_flags); |
Yan, Zheng | 0ca1f7c | 2010-05-16 10:48:47 -0400 | [diff] [blame] | 309 | btrfs_delalloc_release_metadata(inode, end + 1 - start); |
Chris Mason | a1ed835 | 2009-09-11 12:27:37 -0400 | [diff] [blame] | 310 | btrfs_drop_extent_cache(inode, start, aligned_end - 1, 0); |
Josef Bacik | 0036158 | 2013-08-14 14:02:47 -0400 | [diff] [blame] | 311 | out: |
Filipe David Borba Manana | 1acae57 | 2014-01-07 11:42:27 +0000 | [diff] [blame] | 312 | btrfs_free_path(path); |
Josef Bacik | 0036158 | 2013-08-14 14:02:47 -0400 | [diff] [blame] | 313 | btrfs_end_transaction(trans, root); |
| 314 | return ret; |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 315 | } |
| 316 | |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 317 | struct async_extent { |
| 318 | u64 start; |
| 319 | u64 ram_size; |
| 320 | u64 compressed_size; |
| 321 | struct page **pages; |
| 322 | unsigned long nr_pages; |
Li Zefan | 261507a0 | 2010-12-17 14:21:50 +0800 | [diff] [blame] | 323 | int compress_type; |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 324 | struct list_head list; |
| 325 | }; |
| 326 | |
| 327 | struct async_cow { |
| 328 | struct inode *inode; |
| 329 | struct btrfs_root *root; |
| 330 | struct page *locked_page; |
| 331 | u64 start; |
| 332 | u64 end; |
| 333 | struct list_head extents; |
| 334 | struct btrfs_work work; |
| 335 | }; |
| 336 | |
| 337 | static noinline int add_async_extent(struct async_cow *cow, |
| 338 | u64 start, u64 ram_size, |
| 339 | u64 compressed_size, |
| 340 | struct page **pages, |
Li Zefan | 261507a0 | 2010-12-17 14:21:50 +0800 | [diff] [blame] | 341 | unsigned long nr_pages, |
| 342 | int compress_type) |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 343 | { |
| 344 | struct async_extent *async_extent; |
| 345 | |
| 346 | async_extent = kmalloc(sizeof(*async_extent), GFP_NOFS); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 347 | BUG_ON(!async_extent); /* -ENOMEM */ |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 348 | async_extent->start = start; |
| 349 | async_extent->ram_size = ram_size; |
| 350 | async_extent->compressed_size = compressed_size; |
| 351 | async_extent->pages = pages; |
| 352 | async_extent->nr_pages = nr_pages; |
Li Zefan | 261507a0 | 2010-12-17 14:21:50 +0800 | [diff] [blame] | 353 | async_extent->compress_type = compress_type; |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 354 | list_add_tail(&async_extent->list, &cow->extents); |
| 355 | return 0; |
| 356 | } |
| 357 | |
Wang Shilong | f79707b | 2014-07-17 11:44:09 +0800 | [diff] [blame] | 358 | static inline int inode_need_compress(struct inode *inode) |
| 359 | { |
| 360 | struct btrfs_root *root = BTRFS_I(inode)->root; |
| 361 | |
| 362 | /* force compress */ |
| 363 | if (btrfs_test_opt(root, FORCE_COMPRESS)) |
| 364 | return 1; |
| 365 | /* bad compression ratios */ |
| 366 | if (BTRFS_I(inode)->flags & BTRFS_INODE_NOCOMPRESS) |
| 367 | return 0; |
| 368 | if (btrfs_test_opt(root, COMPRESS) || |
| 369 | BTRFS_I(inode)->flags & BTRFS_INODE_COMPRESS || |
| 370 | BTRFS_I(inode)->force_compress) |
| 371 | return 1; |
| 372 | return 0; |
| 373 | } |
| 374 | |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 375 | /* |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 376 | * we create compressed extents in two phases. The first |
| 377 | * phase compresses a range of pages that have already been |
| 378 | * locked (both pages and state bits are locked). |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 379 | * |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 380 | * This is done inside an ordered work queue, and the compression |
| 381 | * is spread across many cpus. The actual IO submission is step |
| 382 | * two, and the ordered work queue takes care of making sure that |
| 383 | * happens in the same order things were put onto the queue by |
| 384 | * writepages and friends. |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 385 | * |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 386 | * If this code finds it can't get good compression, it puts an |
| 387 | * entry onto the work queue to write the uncompressed bytes. This |
| 388 | * makes sure that both compressed inodes and uncompressed inodes |
Artem Bityutskiy | b257031 | 2012-07-25 18:12:06 +0300 | [diff] [blame] | 389 | * are written in the same order that the flusher thread sent them |
| 390 | * down. |
Chris Mason | d352ac6 | 2008-09-29 15:18:18 -0400 | [diff] [blame] | 391 | */ |
Filipe Manana | c44f649 | 2014-10-09 21:15:44 +0100 | [diff] [blame] | 392 | static noinline void compress_file_range(struct inode *inode, |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 393 | struct page *locked_page, |
| 394 | u64 start, u64 end, |
| 395 | struct async_cow *async_cow, |
| 396 | int *num_added) |
Chris Mason | b888db2 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 397 | { |
| 398 | struct btrfs_root *root = BTRFS_I(inode)->root; |
Chris Mason | db94535 | 2007-10-15 16:15:53 -0400 | [diff] [blame] | 399 | u64 num_bytes; |
Chris Mason | db94535 | 2007-10-15 16:15:53 -0400 | [diff] [blame] | 400 | u64 blocksize = root->sectorsize; |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 401 | u64 actual_end; |
Chris Mason | 42dc7ba | 2008-12-15 11:44:56 -0500 | [diff] [blame] | 402 | u64 isize = i_size_read(inode); |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 403 | int ret = 0; |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 404 | struct page **pages = NULL; |
| 405 | unsigned long nr_pages; |
| 406 | unsigned long nr_pages_ret = 0; |
| 407 | unsigned long total_compressed = 0; |
| 408 | unsigned long total_in = 0; |
| 409 | unsigned long max_compressed = 128 * 1024; |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 410 | unsigned long max_uncompressed = 128 * 1024; |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 411 | int i; |
| 412 | int will_compress; |
Li Zefan | 261507a0 | 2010-12-17 14:21:50 +0800 | [diff] [blame] | 413 | int compress_type = root->fs_info->compress_type; |
Chris Mason | 4adaa61 | 2013-03-26 13:07:00 -0400 | [diff] [blame] | 414 | int redirty = 0; |
Chris Mason | b888db2 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 415 | |
Liu Bo | 4cb13e5 | 2012-03-29 09:57:45 -0400 | [diff] [blame] | 416 | /* if this is a small write inside eof, kick off a defrag */ |
| 417 | if ((end - start + 1) < 16 * 1024 && |
| 418 | (start > 0 || end + 1 < BTRFS_I(inode)->disk_i_size)) |
Chris Mason | 4cb5300 | 2011-05-24 15:35:30 -0400 | [diff] [blame] | 419 | btrfs_add_inode_defrag(NULL, inode); |
| 420 | |
Chris Mason | 42dc7ba | 2008-12-15 11:44:56 -0500 | [diff] [blame] | 421 | actual_end = min_t(u64, isize, end + 1); |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 422 | again: |
| 423 | will_compress = 0; |
| 424 | nr_pages = (end >> PAGE_CACHE_SHIFT) - (start >> PAGE_CACHE_SHIFT) + 1; |
| 425 | nr_pages = min(nr_pages, (128 * 1024UL) / PAGE_CACHE_SIZE); |
| 426 | |
Chris Mason | f03d930 | 2009-02-04 09:31:06 -0500 | [diff] [blame] | 427 | /* |
| 428 | * we don't want to send crud past the end of i_size through |
| 429 | * compression, that's just a waste of CPU time. So, if the |
| 430 | * end of the file is before the start of our current |
| 431 | * requested range of bytes, we bail out to the uncompressed |
| 432 | * cleanup code that can deal with all of this. |
| 433 | * |
| 434 | * It isn't really the fastest way to fix things, but this is a |
| 435 | * very uncommon corner. |
| 436 | */ |
| 437 | if (actual_end <= start) |
| 438 | goto cleanup_and_bail_uncompressed; |
| 439 | |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 440 | total_compressed = actual_end - start; |
| 441 | |
Shilong Wang | 4bcbb33 | 2014-10-07 18:44:35 -0400 | [diff] [blame] | 442 | /* |
| 443 | * skip compression for a small file range(<=blocksize) that |
| 444 | * isn't an inline extent, since it dosen't save disk space at all. |
| 445 | */ |
| 446 | if (total_compressed <= blocksize && |
| 447 | (start > 0 || end + 1 < BTRFS_I(inode)->disk_i_size)) |
| 448 | goto cleanup_and_bail_uncompressed; |
| 449 | |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 450 | /* we want to make sure that amount of ram required to uncompress |
| 451 | * an extent is reasonable, so we limit the total size in ram |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 452 | * of a compressed extent to 128k. This is a crucial number |
| 453 | * because it also controls how easily we can spread reads across |
| 454 | * cpus for decompression. |
| 455 | * |
| 456 | * We also want to make sure the amount of IO required to do |
| 457 | * a random read is reasonably small, so we limit the size of |
| 458 | * a compressed extent to 128k. |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 459 | */ |
| 460 | total_compressed = min(total_compressed, max_uncompressed); |
Qu Wenruo | fda2832 | 2013-02-26 08:10:22 +0000 | [diff] [blame] | 461 | num_bytes = ALIGN(end - start + 1, blocksize); |
Chris Mason | be20aa9 | 2007-12-17 20:14:01 -0500 | [diff] [blame] | 462 | num_bytes = max(blocksize, num_bytes); |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 463 | total_in = 0; |
| 464 | ret = 0; |
Chris Mason | db94535 | 2007-10-15 16:15:53 -0400 | [diff] [blame] | 465 | |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 466 | /* |
| 467 | * we do compression for mount -o compress and when the |
| 468 | * inode has not been flagged as nocompress. This flag can |
| 469 | * change at any time if we discover bad compression ratios. |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 470 | */ |
Wang Shilong | f79707b | 2014-07-17 11:44:09 +0800 | [diff] [blame] | 471 | if (inode_need_compress(inode)) { |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 472 | WARN_ON(pages); |
Chris Mason | cfbc246 | 2008-10-30 13:22:14 -0400 | [diff] [blame] | 473 | pages = kzalloc(sizeof(struct page *) * nr_pages, GFP_NOFS); |
Li Zefan | 560f7d7 | 2011-09-08 10:22:01 +0800 | [diff] [blame] | 474 | if (!pages) { |
| 475 | /* just bail out to the uncompressed code */ |
| 476 | goto cont; |
| 477 | } |
Chris Mason | 179e29e | 2007-11-01 11:28:41 -0400 | [diff] [blame] | 478 | |
Li Zefan | 261507a0 | 2010-12-17 14:21:50 +0800 | [diff] [blame] | 479 | if (BTRFS_I(inode)->force_compress) |
| 480 | compress_type = BTRFS_I(inode)->force_compress; |
| 481 | |
Chris Mason | 4adaa61 | 2013-03-26 13:07:00 -0400 | [diff] [blame] | 482 | /* |
| 483 | * we need to call clear_page_dirty_for_io on each |
| 484 | * page in the range. Otherwise applications with the file |
| 485 | * mmap'd can wander in and change the page contents while |
| 486 | * we are compressing them. |
| 487 | * |
| 488 | * If the compression fails for any reason, we set the pages |
| 489 | * dirty again later on. |
| 490 | */ |
| 491 | extent_range_clear_dirty_for_io(inode, start, end); |
| 492 | redirty = 1; |
Li Zefan | 261507a0 | 2010-12-17 14:21:50 +0800 | [diff] [blame] | 493 | ret = btrfs_compress_pages(compress_type, |
| 494 | inode->i_mapping, start, |
| 495 | total_compressed, pages, |
| 496 | nr_pages, &nr_pages_ret, |
| 497 | &total_in, |
| 498 | &total_compressed, |
| 499 | max_compressed); |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 500 | |
| 501 | if (!ret) { |
| 502 | unsigned long offset = total_compressed & |
| 503 | (PAGE_CACHE_SIZE - 1); |
| 504 | struct page *page = pages[nr_pages_ret - 1]; |
| 505 | char *kaddr; |
| 506 | |
| 507 | /* zero the tail end of the last page, we might be |
| 508 | * sending it down to disk |
| 509 | */ |
| 510 | if (offset) { |
Cong Wang | 7ac687d | 2011-11-25 23:14:28 +0800 | [diff] [blame] | 511 | kaddr = kmap_atomic(page); |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 512 | memset(kaddr + offset, 0, |
| 513 | PAGE_CACHE_SIZE - offset); |
Cong Wang | 7ac687d | 2011-11-25 23:14:28 +0800 | [diff] [blame] | 514 | kunmap_atomic(kaddr); |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 515 | } |
| 516 | will_compress = 1; |
| 517 | } |
| 518 | } |
Li Zefan | 560f7d7 | 2011-09-08 10:22:01 +0800 | [diff] [blame] | 519 | cont: |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 520 | if (start == 0) { |
| 521 | /* lets try to make an inline extent */ |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 522 | if (ret || total_in < (actual_end - start)) { |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 523 | /* we didn't compress the entire range, try |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 524 | * to make an uncompressed inline extent. |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 525 | */ |
Josef Bacik | 0036158 | 2013-08-14 14:02:47 -0400 | [diff] [blame] | 526 | ret = cow_file_range_inline(root, inode, start, end, |
| 527 | 0, 0, NULL); |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 528 | } else { |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 529 | /* try making a compressed inline extent */ |
Josef Bacik | 0036158 | 2013-08-14 14:02:47 -0400 | [diff] [blame] | 530 | ret = cow_file_range_inline(root, inode, start, end, |
Li Zefan | fe3f566 | 2011-03-28 08:30:38 +0000 | [diff] [blame] | 531 | total_compressed, |
| 532 | compress_type, pages); |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 533 | } |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 534 | if (ret <= 0) { |
Josef Bacik | 151a41b | 2013-07-29 13:22:24 -0400 | [diff] [blame] | 535 | unsigned long clear_flags = EXTENT_DELALLOC | |
| 536 | EXTENT_DEFRAG; |
Filipe Manana | e6eb431 | 2014-10-10 10:45:12 +0100 | [diff] [blame] | 537 | unsigned long page_error_op; |
| 538 | |
Josef Bacik | 151a41b | 2013-07-29 13:22:24 -0400 | [diff] [blame] | 539 | clear_flags |= (ret < 0) ? EXTENT_DO_ACCOUNTING : 0; |
Filipe Manana | e6eb431 | 2014-10-10 10:45:12 +0100 | [diff] [blame] | 540 | page_error_op = ret < 0 ? PAGE_SET_ERROR : 0; |
Josef Bacik | 151a41b | 2013-07-29 13:22:24 -0400 | [diff] [blame] | 541 | |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 542 | /* |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 543 | * inline extent creation worked or returned error, |
| 544 | * we don't need to create any more async work items. |
| 545 | * Unlock and free up our temp pages. |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 546 | */ |
Josef Bacik | c2790a2 | 2013-07-29 11:20:47 -0400 | [diff] [blame] | 547 | extent_clear_unlock_delalloc(inode, start, end, NULL, |
Josef Bacik | 151a41b | 2013-07-29 13:22:24 -0400 | [diff] [blame] | 548 | clear_flags, PAGE_UNLOCK | |
Josef Bacik | c2790a2 | 2013-07-29 11:20:47 -0400 | [diff] [blame] | 549 | PAGE_CLEAR_DIRTY | |
| 550 | PAGE_SET_WRITEBACK | |
Filipe Manana | e6eb431 | 2014-10-10 10:45:12 +0100 | [diff] [blame] | 551 | page_error_op | |
Josef Bacik | c2790a2 | 2013-07-29 11:20:47 -0400 | [diff] [blame] | 552 | PAGE_END_WRITEBACK); |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 553 | goto free_pages_out; |
| 554 | } |
| 555 | } |
| 556 | |
| 557 | if (will_compress) { |
| 558 | /* |
| 559 | * we aren't doing an inline extent round the compressed size |
| 560 | * up to a block size boundary so the allocator does sane |
| 561 | * things |
| 562 | */ |
Qu Wenruo | fda2832 | 2013-02-26 08:10:22 +0000 | [diff] [blame] | 563 | total_compressed = ALIGN(total_compressed, blocksize); |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 564 | |
| 565 | /* |
| 566 | * one last check to make sure the compression is really a |
| 567 | * win, compare the page count read with the blocks on disk |
| 568 | */ |
Qu Wenruo | fda2832 | 2013-02-26 08:10:22 +0000 | [diff] [blame] | 569 | total_in = ALIGN(total_in, PAGE_CACHE_SIZE); |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 570 | if (total_compressed >= total_in) { |
| 571 | will_compress = 0; |
| 572 | } else { |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 573 | num_bytes = total_in; |
| 574 | } |
| 575 | } |
| 576 | if (!will_compress && pages) { |
| 577 | /* |
| 578 | * the compression code ran but failed to make things smaller, |
| 579 | * free any pages it allocated and our page pointer array |
| 580 | */ |
| 581 | for (i = 0; i < nr_pages_ret; i++) { |
Chris Mason | 70b99e6 | 2008-10-31 12:46:39 -0400 | [diff] [blame] | 582 | WARN_ON(pages[i]->mapping); |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 583 | page_cache_release(pages[i]); |
| 584 | } |
| 585 | kfree(pages); |
| 586 | pages = NULL; |
| 587 | total_compressed = 0; |
| 588 | nr_pages_ret = 0; |
| 589 | |
| 590 | /* flag the file so we don't compress in the future */ |
Chris Mason | 1e701a3 | 2010-03-11 09:42:04 -0500 | [diff] [blame] | 591 | if (!btrfs_test_opt(root, FORCE_COMPRESS) && |
| 592 | !(BTRFS_I(inode)->force_compress)) { |
Chris Mason | a555f81 | 2010-01-28 16:18:15 -0500 | [diff] [blame] | 593 | BTRFS_I(inode)->flags |= BTRFS_INODE_NOCOMPRESS; |
Chris Mason | 1e701a3 | 2010-03-11 09:42:04 -0500 | [diff] [blame] | 594 | } |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 595 | } |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 596 | if (will_compress) { |
| 597 | *num_added += 1; |
| 598 | |
| 599 | /* the async work queues will take care of doing actual |
| 600 | * allocation on disk for these compressed pages, |
| 601 | * and will submit them to the elevator. |
| 602 | */ |
| 603 | add_async_extent(async_cow, start, num_bytes, |
Li Zefan | 261507a0 | 2010-12-17 14:21:50 +0800 | [diff] [blame] | 604 | total_compressed, pages, nr_pages_ret, |
| 605 | compress_type); |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 606 | |
Yan, Zheng | 24ae636 | 2010-12-06 07:02:36 +0000 | [diff] [blame] | 607 | if (start + num_bytes < end) { |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 608 | start += num_bytes; |
| 609 | pages = NULL; |
| 610 | cond_resched(); |
| 611 | goto again; |
| 612 | } |
| 613 | } else { |
Chris Mason | f03d930 | 2009-02-04 09:31:06 -0500 | [diff] [blame] | 614 | cleanup_and_bail_uncompressed: |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 615 | /* |
| 616 | * No compression, but we still need to write the pages in |
| 617 | * the file we've been given so far. redirty the locked |
| 618 | * page if it corresponds to our extent and set things up |
| 619 | * for the async work queue to run cow_file_range to do |
| 620 | * the normal delalloc dance |
| 621 | */ |
| 622 | if (page_offset(locked_page) >= start && |
| 623 | page_offset(locked_page) <= end) { |
| 624 | __set_page_dirty_nobuffers(locked_page); |
| 625 | /* unlocked later on in the async handlers */ |
| 626 | } |
Chris Mason | 4adaa61 | 2013-03-26 13:07:00 -0400 | [diff] [blame] | 627 | if (redirty) |
| 628 | extent_range_redirty_for_io(inode, start, end); |
Li Zefan | 261507a0 | 2010-12-17 14:21:50 +0800 | [diff] [blame] | 629 | add_async_extent(async_cow, start, end - start + 1, |
| 630 | 0, NULL, 0, BTRFS_COMPRESS_NONE); |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 631 | *num_added += 1; |
| 632 | } |
| 633 | |
Filipe Manana | c44f649 | 2014-10-09 21:15:44 +0100 | [diff] [blame] | 634 | return; |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 635 | |
| 636 | free_pages_out: |
| 637 | for (i = 0; i < nr_pages_ret; i++) { |
| 638 | WARN_ON(pages[i]->mapping); |
| 639 | page_cache_release(pages[i]); |
| 640 | } |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 641 | kfree(pages); |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 642 | } |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 643 | |
Filipe Manana | 40ae837 | 2014-10-06 22:14:24 +0100 | [diff] [blame] | 644 | static void free_async_extent_pages(struct async_extent *async_extent) |
| 645 | { |
| 646 | int i; |
| 647 | |
| 648 | if (!async_extent->pages) |
| 649 | return; |
| 650 | |
| 651 | for (i = 0; i < async_extent->nr_pages; i++) { |
| 652 | WARN_ON(async_extent->pages[i]->mapping); |
| 653 | page_cache_release(async_extent->pages[i]); |
| 654 | } |
| 655 | kfree(async_extent->pages); |
| 656 | async_extent->nr_pages = 0; |
| 657 | async_extent->pages = NULL; |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 658 | } |
| 659 | |
| 660 | /* |
| 661 | * phase two of compressed writeback. This is the ordered portion |
| 662 | * of the code, which only gets called in the order the work was |
| 663 | * queued. We walk all the async extents created by compress_file_range |
| 664 | * and send them down to the disk. |
| 665 | */ |
Filipe Manana | dec8f17 | 2014-10-06 22:14:26 +0100 | [diff] [blame] | 666 | static noinline void submit_compressed_extents(struct inode *inode, |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 667 | struct async_cow *async_cow) |
| 668 | { |
| 669 | struct async_extent *async_extent; |
| 670 | u64 alloc_hint = 0; |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 671 | struct btrfs_key ins; |
| 672 | struct extent_map *em; |
| 673 | struct btrfs_root *root = BTRFS_I(inode)->root; |
| 674 | struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree; |
| 675 | struct extent_io_tree *io_tree; |
Josef Bacik | f5a84ee3 | 2009-11-10 21:23:48 -0500 | [diff] [blame] | 676 | int ret = 0; |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 677 | |
Josef Bacik | 3e04e7f | 2013-02-06 16:49:15 -0500 | [diff] [blame] | 678 | again: |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 679 | while (!list_empty(&async_cow->extents)) { |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 680 | async_extent = list_entry(async_cow->extents.next, |
| 681 | struct async_extent, list); |
| 682 | list_del(&async_extent->list); |
| 683 | |
| 684 | io_tree = &BTRFS_I(inode)->io_tree; |
| 685 | |
Josef Bacik | f5a84ee3 | 2009-11-10 21:23:48 -0500 | [diff] [blame] | 686 | retry: |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 687 | /* did the compression code fall back to uncompressed IO? */ |
| 688 | if (!async_extent->pages) { |
| 689 | int page_started = 0; |
| 690 | unsigned long nr_written = 0; |
| 691 | |
| 692 | lock_extent(io_tree, async_extent->start, |
Josef Bacik | 2ac55d4 | 2010-02-03 19:33:23 +0000 | [diff] [blame] | 693 | async_extent->start + |
Jeff Mahoney | d008237 | 2012-03-01 14:57:19 +0100 | [diff] [blame] | 694 | async_extent->ram_size - 1); |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 695 | |
| 696 | /* allocate blocks */ |
Josef Bacik | f5a84ee3 | 2009-11-10 21:23:48 -0500 | [diff] [blame] | 697 | ret = cow_file_range(inode, async_cow->locked_page, |
| 698 | async_extent->start, |
| 699 | async_extent->start + |
| 700 | async_extent->ram_size - 1, |
| 701 | &page_started, &nr_written, 0); |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 702 | |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 703 | /* JDM XXX */ |
| 704 | |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 705 | /* |
| 706 | * if page_started, cow_file_range inserted an |
| 707 | * inline extent and took care of all the unlocking |
| 708 | * and IO for us. Otherwise, we need to submit |
| 709 | * all those pages down to the drive. |
| 710 | */ |
Josef Bacik | f5a84ee3 | 2009-11-10 21:23:48 -0500 | [diff] [blame] | 711 | if (!page_started && !ret) |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 712 | extent_write_locked_range(io_tree, |
| 713 | inode, async_extent->start, |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 714 | async_extent->start + |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 715 | async_extent->ram_size - 1, |
| 716 | btrfs_get_extent, |
| 717 | WB_SYNC_ALL); |
Josef Bacik | 3e04e7f | 2013-02-06 16:49:15 -0500 | [diff] [blame] | 718 | else if (ret) |
| 719 | unlock_page(async_cow->locked_page); |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 720 | kfree(async_extent); |
| 721 | cond_resched(); |
| 722 | continue; |
| 723 | } |
| 724 | |
| 725 | lock_extent(io_tree, async_extent->start, |
Jeff Mahoney | d008237 | 2012-03-01 14:57:19 +0100 | [diff] [blame] | 726 | async_extent->start + async_extent->ram_size - 1); |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 727 | |
Josef Bacik | 0036158 | 2013-08-14 14:02:47 -0400 | [diff] [blame] | 728 | ret = btrfs_reserve_extent(root, |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 729 | async_extent->compressed_size, |
| 730 | async_extent->compressed_size, |
Miao Xie | e570fd2 | 2014-06-19 10:42:50 +0800 | [diff] [blame] | 731 | 0, alloc_hint, &ins, 1, 1); |
Josef Bacik | f5a84ee3 | 2009-11-10 21:23:48 -0500 | [diff] [blame] | 732 | if (ret) { |
Filipe Manana | 40ae837 | 2014-10-06 22:14:24 +0100 | [diff] [blame] | 733 | free_async_extent_pages(async_extent); |
Josef Bacik | 3e04e7f | 2013-02-06 16:49:15 -0500 | [diff] [blame] | 734 | |
Josef Bacik | fdf8e2e | 2013-06-14 16:58:23 -0400 | [diff] [blame] | 735 | if (ret == -ENOSPC) { |
| 736 | unlock_extent(io_tree, async_extent->start, |
| 737 | async_extent->start + |
| 738 | async_extent->ram_size - 1); |
Liu Bo | ce62003 | 2014-07-24 22:48:05 +0800 | [diff] [blame] | 739 | |
| 740 | /* |
| 741 | * we need to redirty the pages if we decide to |
| 742 | * fallback to uncompressed IO, otherwise we |
| 743 | * will not submit these pages down to lower |
| 744 | * layers. |
| 745 | */ |
| 746 | extent_range_redirty_for_io(inode, |
| 747 | async_extent->start, |
| 748 | async_extent->start + |
| 749 | async_extent->ram_size - 1); |
| 750 | |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 751 | goto retry; |
Josef Bacik | fdf8e2e | 2013-06-14 16:58:23 -0400 | [diff] [blame] | 752 | } |
Josef Bacik | 3e04e7f | 2013-02-06 16:49:15 -0500 | [diff] [blame] | 753 | goto out_free; |
Josef Bacik | f5a84ee3 | 2009-11-10 21:23:48 -0500 | [diff] [blame] | 754 | } |
| 755 | |
Yan, Zheng | c216775 | 2009-11-12 09:34:21 +0000 | [diff] [blame] | 756 | /* |
| 757 | * here we're doing allocation and writeback of the |
| 758 | * compressed pages |
| 759 | */ |
| 760 | btrfs_drop_extent_cache(inode, async_extent->start, |
| 761 | async_extent->start + |
| 762 | async_extent->ram_size - 1, 0); |
| 763 | |
David Sterba | 172ddd6 | 2011-04-21 00:48:27 +0200 | [diff] [blame] | 764 | em = alloc_extent_map(); |
Liu Bo | b9aa55b | 2013-05-14 02:12:15 +0000 | [diff] [blame] | 765 | if (!em) { |
| 766 | ret = -ENOMEM; |
Josef Bacik | 3e04e7f | 2013-02-06 16:49:15 -0500 | [diff] [blame] | 767 | goto out_free_reserve; |
Liu Bo | b9aa55b | 2013-05-14 02:12:15 +0000 | [diff] [blame] | 768 | } |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 769 | em->start = async_extent->start; |
| 770 | em->len = async_extent->ram_size; |
Chris Mason | 445a694 | 2008-11-10 11:53:33 -0500 | [diff] [blame] | 771 | em->orig_start = em->start; |
Josef Bacik | 2ab28f3 | 2012-10-12 15:27:49 -0400 | [diff] [blame] | 772 | em->mod_start = em->start; |
| 773 | em->mod_len = em->len; |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 774 | |
| 775 | em->block_start = ins.objectid; |
| 776 | em->block_len = ins.offset; |
Josef Bacik | b493968 | 2012-12-03 10:31:19 -0500 | [diff] [blame] | 777 | em->orig_block_len = ins.offset; |
Josef Bacik | cc95bef | 2013-04-04 14:31:27 -0400 | [diff] [blame] | 778 | em->ram_bytes = async_extent->ram_size; |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 779 | em->bdev = root->fs_info->fs_devices->latest_bdev; |
Li Zefan | 261507a0 | 2010-12-17 14:21:50 +0800 | [diff] [blame] | 780 | em->compress_type = async_extent->compress_type; |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 781 | set_bit(EXTENT_FLAG_PINNED, &em->flags); |
| 782 | set_bit(EXTENT_FLAG_COMPRESSED, &em->flags); |
Josef Bacik | 70c8a91 | 2012-10-11 16:54:30 -0400 | [diff] [blame] | 783 | em->generation = -1; |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 784 | |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 785 | while (1) { |
Chris Mason | 890871b | 2009-09-02 16:24:52 -0400 | [diff] [blame] | 786 | write_lock(&em_tree->lock); |
Josef Bacik | 09a2a8f9 | 2013-04-05 16:51:15 -0400 | [diff] [blame] | 787 | ret = add_extent_mapping(em_tree, em, 1); |
Chris Mason | 890871b | 2009-09-02 16:24:52 -0400 | [diff] [blame] | 788 | write_unlock(&em_tree->lock); |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 789 | if (ret != -EEXIST) { |
| 790 | free_extent_map(em); |
| 791 | break; |
| 792 | } |
| 793 | btrfs_drop_extent_cache(inode, async_extent->start, |
| 794 | async_extent->start + |
| 795 | async_extent->ram_size - 1, 0); |
| 796 | } |
| 797 | |
Josef Bacik | 3e04e7f | 2013-02-06 16:49:15 -0500 | [diff] [blame] | 798 | if (ret) |
| 799 | goto out_free_reserve; |
| 800 | |
Li Zefan | 261507a0 | 2010-12-17 14:21:50 +0800 | [diff] [blame] | 801 | ret = btrfs_add_ordered_extent_compress(inode, |
| 802 | async_extent->start, |
| 803 | ins.objectid, |
| 804 | async_extent->ram_size, |
| 805 | ins.offset, |
| 806 | BTRFS_ORDERED_COMPRESSED, |
| 807 | async_extent->compress_type); |
Filipe Manana | d9f8596 | 2014-08-25 10:43:00 +0100 | [diff] [blame] | 808 | if (ret) { |
| 809 | btrfs_drop_extent_cache(inode, async_extent->start, |
| 810 | async_extent->start + |
| 811 | async_extent->ram_size - 1, 0); |
Josef Bacik | 3e04e7f | 2013-02-06 16:49:15 -0500 | [diff] [blame] | 812 | goto out_free_reserve; |
Filipe Manana | d9f8596 | 2014-08-25 10:43:00 +0100 | [diff] [blame] | 813 | } |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 814 | |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 815 | /* |
| 816 | * clear dirty, set writeback and unlock the pages. |
| 817 | */ |
Josef Bacik | c2790a2 | 2013-07-29 11:20:47 -0400 | [diff] [blame] | 818 | extent_clear_unlock_delalloc(inode, async_extent->start, |
Chris Mason | a791e35 | 2009-10-08 11:27:10 -0400 | [diff] [blame] | 819 | async_extent->start + |
| 820 | async_extent->ram_size - 1, |
Josef Bacik | 151a41b | 2013-07-29 13:22:24 -0400 | [diff] [blame] | 821 | NULL, EXTENT_LOCKED | EXTENT_DELALLOC, |
| 822 | PAGE_UNLOCK | PAGE_CLEAR_DIRTY | |
Josef Bacik | c2790a2 | 2013-07-29 11:20:47 -0400 | [diff] [blame] | 823 | PAGE_SET_WRITEBACK); |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 824 | ret = btrfs_submit_compressed_write(inode, |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 825 | async_extent->start, |
| 826 | async_extent->ram_size, |
| 827 | ins.objectid, |
| 828 | ins.offset, async_extent->pages, |
| 829 | async_extent->nr_pages); |
Filipe Manana | fce2a4e | 2014-10-06 22:14:23 +0100 | [diff] [blame] | 830 | if (ret) { |
| 831 | struct extent_io_tree *tree = &BTRFS_I(inode)->io_tree; |
| 832 | struct page *p = async_extent->pages[0]; |
| 833 | const u64 start = async_extent->start; |
| 834 | const u64 end = start + async_extent->ram_size - 1; |
| 835 | |
| 836 | p->mapping = inode->i_mapping; |
| 837 | tree->ops->writepage_end_io_hook(p, start, end, |
| 838 | NULL, 0); |
| 839 | p->mapping = NULL; |
| 840 | extent_clear_unlock_delalloc(inode, start, end, NULL, 0, |
| 841 | PAGE_END_WRITEBACK | |
| 842 | PAGE_SET_ERROR); |
Filipe Manana | 40ae837 | 2014-10-06 22:14:24 +0100 | [diff] [blame] | 843 | free_async_extent_pages(async_extent); |
Filipe Manana | fce2a4e | 2014-10-06 22:14:23 +0100 | [diff] [blame] | 844 | } |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 845 | alloc_hint = ins.objectid + ins.offset; |
| 846 | kfree(async_extent); |
| 847 | cond_resched(); |
| 848 | } |
Filipe Manana | dec8f17 | 2014-10-06 22:14:26 +0100 | [diff] [blame] | 849 | return; |
Josef Bacik | 3e04e7f | 2013-02-06 16:49:15 -0500 | [diff] [blame] | 850 | out_free_reserve: |
Miao Xie | e570fd2 | 2014-06-19 10:42:50 +0800 | [diff] [blame] | 851 | btrfs_free_reserved_extent(root, ins.objectid, ins.offset, 1); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 852 | out_free: |
Josef Bacik | c2790a2 | 2013-07-29 11:20:47 -0400 | [diff] [blame] | 853 | extent_clear_unlock_delalloc(inode, async_extent->start, |
Josef Bacik | 3e04e7f | 2013-02-06 16:49:15 -0500 | [diff] [blame] | 854 | async_extent->start + |
| 855 | async_extent->ram_size - 1, |
Josef Bacik | c2790a2 | 2013-07-29 11:20:47 -0400 | [diff] [blame] | 856 | NULL, EXTENT_LOCKED | EXTENT_DELALLOC | |
Josef Bacik | 151a41b | 2013-07-29 13:22:24 -0400 | [diff] [blame] | 857 | EXTENT_DEFRAG | EXTENT_DO_ACCOUNTING, |
| 858 | PAGE_UNLOCK | PAGE_CLEAR_DIRTY | |
Filipe Manana | 704de49 | 2014-10-06 22:14:22 +0100 | [diff] [blame] | 859 | PAGE_SET_WRITEBACK | PAGE_END_WRITEBACK | |
| 860 | PAGE_SET_ERROR); |
Filipe Manana | 40ae837 | 2014-10-06 22:14:24 +0100 | [diff] [blame] | 861 | free_async_extent_pages(async_extent); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 862 | kfree(async_extent); |
Josef Bacik | 3e04e7f | 2013-02-06 16:49:15 -0500 | [diff] [blame] | 863 | goto again; |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 864 | } |
| 865 | |
Josef Bacik | 4b46fce | 2010-05-23 11:00:55 -0400 | [diff] [blame] | 866 | static u64 get_extent_allocation_hint(struct inode *inode, u64 start, |
| 867 | u64 num_bytes) |
| 868 | { |
| 869 | struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree; |
| 870 | struct extent_map *em; |
| 871 | u64 alloc_hint = 0; |
| 872 | |
| 873 | read_lock(&em_tree->lock); |
| 874 | em = search_extent_mapping(em_tree, start, num_bytes); |
| 875 | if (em) { |
| 876 | /* |
| 877 | * if block start isn't an actual block number then find the |
| 878 | * first block in this inode and use that as a hint. If that |
| 879 | * block is also bogus then just don't worry about it. |
| 880 | */ |
| 881 | if (em->block_start >= EXTENT_MAP_LAST_BYTE) { |
| 882 | free_extent_map(em); |
| 883 | em = search_extent_mapping(em_tree, 0, 0); |
| 884 | if (em && em->block_start < EXTENT_MAP_LAST_BYTE) |
| 885 | alloc_hint = em->block_start; |
| 886 | if (em) |
| 887 | free_extent_map(em); |
| 888 | } else { |
| 889 | alloc_hint = em->block_start; |
| 890 | free_extent_map(em); |
| 891 | } |
| 892 | } |
| 893 | read_unlock(&em_tree->lock); |
| 894 | |
| 895 | return alloc_hint; |
| 896 | } |
| 897 | |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 898 | /* |
| 899 | * when extent_io.c finds a delayed allocation range in the file, |
| 900 | * the call backs end up in this code. The basic idea is to |
| 901 | * allocate extents on disk for the range, and create ordered data structs |
| 902 | * in ram to track those extents. |
| 903 | * |
| 904 | * locked_page is the page that writepage had locked already. We use |
| 905 | * it to make sure we don't do extra locks or unlocks. |
| 906 | * |
| 907 | * *page_started is set to one if we unlock locked_page and do everything |
| 908 | * required to start IO on it. It may be clean and already done with |
| 909 | * IO when we return. |
| 910 | */ |
Josef Bacik | 0036158 | 2013-08-14 14:02:47 -0400 | [diff] [blame] | 911 | static noinline int cow_file_range(struct inode *inode, |
| 912 | struct page *locked_page, |
| 913 | u64 start, u64 end, int *page_started, |
| 914 | unsigned long *nr_written, |
| 915 | int unlock) |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 916 | { |
Josef Bacik | 0036158 | 2013-08-14 14:02:47 -0400 | [diff] [blame] | 917 | struct btrfs_root *root = BTRFS_I(inode)->root; |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 918 | u64 alloc_hint = 0; |
| 919 | u64 num_bytes; |
| 920 | unsigned long ram_size; |
| 921 | u64 disk_num_bytes; |
| 922 | u64 cur_alloc_size; |
| 923 | u64 blocksize = root->sectorsize; |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 924 | struct btrfs_key ins; |
| 925 | struct extent_map *em; |
| 926 | struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree; |
| 927 | int ret = 0; |
| 928 | |
Josef Bacik | 02ecd2c | 2013-10-25 16:19:08 -0400 | [diff] [blame] | 929 | if (btrfs_is_free_space_inode(inode)) { |
| 930 | WARN_ON_ONCE(1); |
Josef Bacik | 29bce2f | 2014-02-07 12:21:23 -0500 | [diff] [blame] | 931 | ret = -EINVAL; |
| 932 | goto out_unlock; |
Josef Bacik | 02ecd2c | 2013-10-25 16:19:08 -0400 | [diff] [blame] | 933 | } |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 934 | |
Qu Wenruo | fda2832 | 2013-02-26 08:10:22 +0000 | [diff] [blame] | 935 | num_bytes = ALIGN(end - start + 1, blocksize); |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 936 | num_bytes = max(blocksize, num_bytes); |
| 937 | disk_num_bytes = num_bytes; |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 938 | |
Chris Mason | 4cb5300 | 2011-05-24 15:35:30 -0400 | [diff] [blame] | 939 | /* if this is a small write inside eof, kick off defrag */ |
Liu Bo | 4cb13e5 | 2012-03-29 09:57:45 -0400 | [diff] [blame] | 940 | if (num_bytes < 64 * 1024 && |
| 941 | (start > 0 || end + 1 < BTRFS_I(inode)->disk_i_size)) |
Josef Bacik | 0036158 | 2013-08-14 14:02:47 -0400 | [diff] [blame] | 942 | btrfs_add_inode_defrag(NULL, inode); |
Chris Mason | 4cb5300 | 2011-05-24 15:35:30 -0400 | [diff] [blame] | 943 | |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 944 | if (start == 0) { |
| 945 | /* lets try to make an inline extent */ |
Josef Bacik | 0036158 | 2013-08-14 14:02:47 -0400 | [diff] [blame] | 946 | ret = cow_file_range_inline(root, inode, start, end, 0, 0, |
| 947 | NULL); |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 948 | if (ret == 0) { |
Josef Bacik | c2790a2 | 2013-07-29 11:20:47 -0400 | [diff] [blame] | 949 | extent_clear_unlock_delalloc(inode, start, end, NULL, |
| 950 | EXTENT_LOCKED | EXTENT_DELALLOC | |
Josef Bacik | 151a41b | 2013-07-29 13:22:24 -0400 | [diff] [blame] | 951 | EXTENT_DEFRAG, PAGE_UNLOCK | |
Josef Bacik | c2790a2 | 2013-07-29 11:20:47 -0400 | [diff] [blame] | 952 | PAGE_CLEAR_DIRTY | PAGE_SET_WRITEBACK | |
| 953 | PAGE_END_WRITEBACK); |
Yan, Zheng | c216775 | 2009-11-12 09:34:21 +0000 | [diff] [blame] | 954 | |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 955 | *nr_written = *nr_written + |
| 956 | (end - start + PAGE_CACHE_SIZE) / PAGE_CACHE_SIZE; |
| 957 | *page_started = 1; |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 958 | goto out; |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 959 | } else if (ret < 0) { |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 960 | goto out_unlock; |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 961 | } |
| 962 | } |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 963 | |
| 964 | BUG_ON(disk_num_bytes > |
David Sterba | 6c41761 | 2011-04-13 15:41:04 +0200 | [diff] [blame] | 965 | btrfs_super_total_bytes(root->fs_info->super_copy)); |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 966 | |
Josef Bacik | 4b46fce | 2010-05-23 11:00:55 -0400 | [diff] [blame] | 967 | alloc_hint = get_extent_allocation_hint(inode, start, num_bytes); |
Zheng Yan | 5b21f2e | 2008-09-26 10:05:38 -0400 | [diff] [blame] | 968 | btrfs_drop_extent_cache(inode, start, start + num_bytes - 1, 0); |
Chris Mason | 3b95151 | 2008-04-17 11:29:12 -0400 | [diff] [blame] | 969 | |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 970 | while (disk_num_bytes > 0) { |
Chris Mason | a791e35 | 2009-10-08 11:27:10 -0400 | [diff] [blame] | 971 | unsigned long op; |
| 972 | |
Josef Bacik | 287a0ab | 2010-03-19 18:07:23 +0000 | [diff] [blame] | 973 | cur_alloc_size = disk_num_bytes; |
Josef Bacik | 0036158 | 2013-08-14 14:02:47 -0400 | [diff] [blame] | 974 | ret = btrfs_reserve_extent(root, cur_alloc_size, |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 975 | root->sectorsize, 0, alloc_hint, |
Miao Xie | e570fd2 | 2014-06-19 10:42:50 +0800 | [diff] [blame] | 976 | &ins, 1, 1); |
Josef Bacik | 0036158 | 2013-08-14 14:02:47 -0400 | [diff] [blame] | 977 | if (ret < 0) |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 978 | goto out_unlock; |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 979 | |
David Sterba | 172ddd6 | 2011-04-21 00:48:27 +0200 | [diff] [blame] | 980 | em = alloc_extent_map(); |
Liu Bo | b9aa55b | 2013-05-14 02:12:15 +0000 | [diff] [blame] | 981 | if (!em) { |
| 982 | ret = -ENOMEM; |
Liu Bo | ace68ba | 2013-04-22 10:53:47 +0000 | [diff] [blame] | 983 | goto out_reserve; |
Liu Bo | b9aa55b | 2013-05-14 02:12:15 +0000 | [diff] [blame] | 984 | } |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 985 | em->start = start; |
Chris Mason | 445a694 | 2008-11-10 11:53:33 -0500 | [diff] [blame] | 986 | em->orig_start = em->start; |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 987 | ram_size = ins.offset; |
| 988 | em->len = ins.offset; |
Josef Bacik | 2ab28f3 | 2012-10-12 15:27:49 -0400 | [diff] [blame] | 989 | em->mod_start = em->start; |
| 990 | em->mod_len = em->len; |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 991 | |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 992 | em->block_start = ins.objectid; |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 993 | em->block_len = ins.offset; |
Josef Bacik | b493968 | 2012-12-03 10:31:19 -0500 | [diff] [blame] | 994 | em->orig_block_len = ins.offset; |
Josef Bacik | cc95bef | 2013-04-04 14:31:27 -0400 | [diff] [blame] | 995 | em->ram_bytes = ram_size; |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 996 | em->bdev = root->fs_info->fs_devices->latest_bdev; |
Chris Mason | 7f3c74f | 2008-07-18 12:01:11 -0400 | [diff] [blame] | 997 | set_bit(EXTENT_FLAG_PINNED, &em->flags); |
Josef Bacik | 70c8a91 | 2012-10-11 16:54:30 -0400 | [diff] [blame] | 998 | em->generation = -1; |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 999 | |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 1000 | while (1) { |
Chris Mason | 890871b | 2009-09-02 16:24:52 -0400 | [diff] [blame] | 1001 | write_lock(&em_tree->lock); |
Josef Bacik | 09a2a8f9 | 2013-04-05 16:51:15 -0400 | [diff] [blame] | 1002 | ret = add_extent_mapping(em_tree, em, 1); |
Chris Mason | 890871b | 2009-09-02 16:24:52 -0400 | [diff] [blame] | 1003 | write_unlock(&em_tree->lock); |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 1004 | if (ret != -EEXIST) { |
| 1005 | free_extent_map(em); |
| 1006 | break; |
| 1007 | } |
| 1008 | btrfs_drop_extent_cache(inode, start, |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 1009 | start + ram_size - 1, 0); |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 1010 | } |
Liu Bo | ace68ba | 2013-04-22 10:53:47 +0000 | [diff] [blame] | 1011 | if (ret) |
| 1012 | goto out_reserve; |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 1013 | |
Chris Mason | 98d20f6 | 2008-04-14 09:46:10 -0400 | [diff] [blame] | 1014 | cur_alloc_size = ins.offset; |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 1015 | ret = btrfs_add_ordered_extent(inode, start, ins.objectid, |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 1016 | ram_size, cur_alloc_size, 0); |
Liu Bo | ace68ba | 2013-04-22 10:53:47 +0000 | [diff] [blame] | 1017 | if (ret) |
Filipe Manana | d9f8596 | 2014-08-25 10:43:00 +0100 | [diff] [blame] | 1018 | goto out_drop_extent_cache; |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 1019 | |
Yan Zheng | 17d217f | 2008-12-12 10:03:38 -0500 | [diff] [blame] | 1020 | if (root->root_key.objectid == |
| 1021 | BTRFS_DATA_RELOC_TREE_OBJECTID) { |
| 1022 | ret = btrfs_reloc_clone_csums(inode, start, |
| 1023 | cur_alloc_size); |
Josef Bacik | 0036158 | 2013-08-14 14:02:47 -0400 | [diff] [blame] | 1024 | if (ret) |
Filipe Manana | d9f8596 | 2014-08-25 10:43:00 +0100 | [diff] [blame] | 1025 | goto out_drop_extent_cache; |
Yan Zheng | 17d217f | 2008-12-12 10:03:38 -0500 | [diff] [blame] | 1026 | } |
| 1027 | |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 1028 | if (disk_num_bytes < cur_alloc_size) |
Chris Mason | 3b95151 | 2008-04-17 11:29:12 -0400 | [diff] [blame] | 1029 | break; |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 1030 | |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 1031 | /* we're not doing compressed IO, don't unlock the first |
| 1032 | * page (which the caller expects to stay locked), don't |
| 1033 | * clear any dirty bits and don't set any writeback bits |
Chris Mason | 8b62b72 | 2009-09-02 16:53:46 -0400 | [diff] [blame] | 1034 | * |
| 1035 | * Do set the Private2 bit so we know this page was properly |
| 1036 | * setup for writepage |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 1037 | */ |
Josef Bacik | c2790a2 | 2013-07-29 11:20:47 -0400 | [diff] [blame] | 1038 | op = unlock ? PAGE_UNLOCK : 0; |
| 1039 | op |= PAGE_SET_PRIVATE2; |
Chris Mason | a791e35 | 2009-10-08 11:27:10 -0400 | [diff] [blame] | 1040 | |
Josef Bacik | c2790a2 | 2013-07-29 11:20:47 -0400 | [diff] [blame] | 1041 | extent_clear_unlock_delalloc(inode, start, |
| 1042 | start + ram_size - 1, locked_page, |
| 1043 | EXTENT_LOCKED | EXTENT_DELALLOC, |
| 1044 | op); |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 1045 | disk_num_bytes -= cur_alloc_size; |
Chris Mason | c59f895 | 2007-12-17 20:14:04 -0500 | [diff] [blame] | 1046 | num_bytes -= cur_alloc_size; |
| 1047 | alloc_hint = ins.objectid + ins.offset; |
| 1048 | start += cur_alloc_size; |
Chris Mason | b888db2 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 1049 | } |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 1050 | out: |
Chris Mason | be20aa9 | 2007-12-17 20:14:01 -0500 | [diff] [blame] | 1051 | return ret; |
Miao Xie | b7d5b0a | 2012-11-01 07:32:18 +0000 | [diff] [blame] | 1052 | |
Filipe Manana | d9f8596 | 2014-08-25 10:43:00 +0100 | [diff] [blame] | 1053 | out_drop_extent_cache: |
| 1054 | btrfs_drop_extent_cache(inode, start, start + ram_size - 1, 0); |
Liu Bo | ace68ba | 2013-04-22 10:53:47 +0000 | [diff] [blame] | 1055 | out_reserve: |
Miao Xie | e570fd2 | 2014-06-19 10:42:50 +0800 | [diff] [blame] | 1056 | btrfs_free_reserved_extent(root, ins.objectid, ins.offset, 1); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 1057 | out_unlock: |
Josef Bacik | c2790a2 | 2013-07-29 11:20:47 -0400 | [diff] [blame] | 1058 | extent_clear_unlock_delalloc(inode, start, end, locked_page, |
Josef Bacik | 151a41b | 2013-07-29 13:22:24 -0400 | [diff] [blame] | 1059 | EXTENT_LOCKED | EXTENT_DO_ACCOUNTING | |
| 1060 | EXTENT_DELALLOC | EXTENT_DEFRAG, |
| 1061 | PAGE_UNLOCK | PAGE_CLEAR_DIRTY | |
| 1062 | PAGE_SET_WRITEBACK | PAGE_END_WRITEBACK); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 1063 | goto out; |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 1064 | } |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 1065 | |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 1066 | /* |
| 1067 | * work queue call back to started compression on a file and pages |
| 1068 | */ |
| 1069 | static noinline void async_cow_start(struct btrfs_work *work) |
| 1070 | { |
| 1071 | struct async_cow *async_cow; |
| 1072 | int num_added = 0; |
| 1073 | async_cow = container_of(work, struct async_cow, work); |
| 1074 | |
| 1075 | compress_file_range(async_cow->inode, async_cow->locked_page, |
| 1076 | async_cow->start, async_cow->end, async_cow, |
| 1077 | &num_added); |
Josef Bacik | 8180ef8 | 2012-06-08 15:16:12 -0400 | [diff] [blame] | 1078 | if (num_added == 0) { |
Josef Bacik | cb77fcd | 2012-06-15 12:19:48 -0600 | [diff] [blame] | 1079 | btrfs_add_delayed_iput(async_cow->inode); |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 1080 | async_cow->inode = NULL; |
Josef Bacik | 8180ef8 | 2012-06-08 15:16:12 -0400 | [diff] [blame] | 1081 | } |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 1082 | } |
| 1083 | |
| 1084 | /* |
| 1085 | * work queue call back to submit previously compressed pages |
| 1086 | */ |
| 1087 | static noinline void async_cow_submit(struct btrfs_work *work) |
| 1088 | { |
| 1089 | struct async_cow *async_cow; |
| 1090 | struct btrfs_root *root; |
| 1091 | unsigned long nr_pages; |
| 1092 | |
| 1093 | async_cow = container_of(work, struct async_cow, work); |
| 1094 | |
| 1095 | root = async_cow->root; |
| 1096 | nr_pages = (async_cow->end - async_cow->start + PAGE_CACHE_SIZE) >> |
| 1097 | PAGE_CACHE_SHIFT; |
| 1098 | |
Josef Bacik | 66657b3 | 2012-08-01 15:36:24 -0400 | [diff] [blame] | 1099 | if (atomic_sub_return(nr_pages, &root->fs_info->async_delalloc_pages) < |
Liu Bo | 287082b | 2012-06-28 04:02:24 -0600 | [diff] [blame] | 1100 | 5 * 1024 * 1024 && |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 1101 | waitqueue_active(&root->fs_info->async_submit_wait)) |
| 1102 | wake_up(&root->fs_info->async_submit_wait); |
| 1103 | |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 1104 | if (async_cow->inode) |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 1105 | submit_compressed_extents(async_cow->inode, async_cow); |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 1106 | } |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 1107 | |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 1108 | static noinline void async_cow_free(struct btrfs_work *work) |
| 1109 | { |
| 1110 | struct async_cow *async_cow; |
| 1111 | async_cow = container_of(work, struct async_cow, work); |
Josef Bacik | 8180ef8 | 2012-06-08 15:16:12 -0400 | [diff] [blame] | 1112 | if (async_cow->inode) |
Josef Bacik | cb77fcd | 2012-06-15 12:19:48 -0600 | [diff] [blame] | 1113 | btrfs_add_delayed_iput(async_cow->inode); |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 1114 | kfree(async_cow); |
| 1115 | } |
| 1116 | |
| 1117 | static int cow_file_range_async(struct inode *inode, struct page *locked_page, |
| 1118 | u64 start, u64 end, int *page_started, |
| 1119 | unsigned long *nr_written) |
| 1120 | { |
| 1121 | struct async_cow *async_cow; |
| 1122 | struct btrfs_root *root = BTRFS_I(inode)->root; |
| 1123 | unsigned long nr_pages; |
| 1124 | u64 cur_end; |
Liu Bo | 287082b | 2012-06-28 04:02:24 -0600 | [diff] [blame] | 1125 | int limit = 10 * 1024 * 1024; |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 1126 | |
Chris Mason | a3429ab | 2009-10-08 12:30:20 -0400 | [diff] [blame] | 1127 | clear_extent_bit(&BTRFS_I(inode)->io_tree, start, end, EXTENT_LOCKED, |
| 1128 | 1, 0, NULL, GFP_NOFS); |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 1129 | while (start < end) { |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 1130 | async_cow = kmalloc(sizeof(*async_cow), GFP_NOFS); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 1131 | BUG_ON(!async_cow); /* -ENOMEM */ |
Josef Bacik | 8180ef8 | 2012-06-08 15:16:12 -0400 | [diff] [blame] | 1132 | async_cow->inode = igrab(inode); |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 1133 | async_cow->root = root; |
| 1134 | async_cow->locked_page = locked_page; |
| 1135 | async_cow->start = start; |
| 1136 | |
Wang Shilong | f79707b | 2014-07-17 11:44:09 +0800 | [diff] [blame] | 1137 | if (BTRFS_I(inode)->flags & BTRFS_INODE_NOCOMPRESS && |
| 1138 | !btrfs_test_opt(root, FORCE_COMPRESS)) |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 1139 | cur_end = end; |
| 1140 | else |
| 1141 | cur_end = min(end, start + 512 * 1024 - 1); |
| 1142 | |
| 1143 | async_cow->end = cur_end; |
| 1144 | INIT_LIST_HEAD(&async_cow->extents); |
| 1145 | |
Liu Bo | 9e0af23 | 2014-08-15 23:36:53 +0800 | [diff] [blame] | 1146 | btrfs_init_work(&async_cow->work, |
| 1147 | btrfs_delalloc_helper, |
| 1148 | async_cow_start, async_cow_submit, |
| 1149 | async_cow_free); |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 1150 | |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 1151 | nr_pages = (cur_end - start + PAGE_CACHE_SIZE) >> |
| 1152 | PAGE_CACHE_SHIFT; |
| 1153 | atomic_add(nr_pages, &root->fs_info->async_delalloc_pages); |
| 1154 | |
Qu Wenruo | afe3d24 | 2014-02-28 10:46:07 +0800 | [diff] [blame] | 1155 | btrfs_queue_work(root->fs_info->delalloc_workers, |
| 1156 | &async_cow->work); |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 1157 | |
| 1158 | if (atomic_read(&root->fs_info->async_delalloc_pages) > limit) { |
| 1159 | wait_event(root->fs_info->async_submit_wait, |
| 1160 | (atomic_read(&root->fs_info->async_delalloc_pages) < |
| 1161 | limit)); |
| 1162 | } |
| 1163 | |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 1164 | while (atomic_read(&root->fs_info->async_submit_draining) && |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 1165 | atomic_read(&root->fs_info->async_delalloc_pages)) { |
| 1166 | wait_event(root->fs_info->async_submit_wait, |
| 1167 | (atomic_read(&root->fs_info->async_delalloc_pages) == |
| 1168 | 0)); |
| 1169 | } |
| 1170 | |
| 1171 | *nr_written += nr_pages; |
| 1172 | start = cur_end + 1; |
| 1173 | } |
| 1174 | *page_started = 1; |
| 1175 | return 0; |
Chris Mason | be20aa9 | 2007-12-17 20:14:01 -0500 | [diff] [blame] | 1176 | } |
| 1177 | |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 1178 | static noinline int csum_exist_in_range(struct btrfs_root *root, |
Yan Zheng | 17d217f | 2008-12-12 10:03:38 -0500 | [diff] [blame] | 1179 | u64 bytenr, u64 num_bytes) |
| 1180 | { |
| 1181 | int ret; |
| 1182 | struct btrfs_ordered_sum *sums; |
| 1183 | LIST_HEAD(list); |
| 1184 | |
Yan Zheng | 07d400a | 2009-01-06 11:42:00 -0500 | [diff] [blame] | 1185 | ret = btrfs_lookup_csums_range(root->fs_info->csum_root, bytenr, |
Arne Jansen | a2de733 | 2011-03-08 14:14:00 +0100 | [diff] [blame] | 1186 | bytenr + num_bytes - 1, &list, 0); |
Yan Zheng | 17d217f | 2008-12-12 10:03:38 -0500 | [diff] [blame] | 1187 | if (ret == 0 && list_empty(&list)) |
| 1188 | return 0; |
| 1189 | |
| 1190 | while (!list_empty(&list)) { |
| 1191 | sums = list_entry(list.next, struct btrfs_ordered_sum, list); |
| 1192 | list_del(&sums->list); |
| 1193 | kfree(sums); |
| 1194 | } |
| 1195 | return 1; |
| 1196 | } |
| 1197 | |
Chris Mason | d352ac6 | 2008-09-29 15:18:18 -0400 | [diff] [blame] | 1198 | /* |
| 1199 | * when nowcow writeback call back. This checks for snapshots or COW copies |
| 1200 | * of the extents that exist in the file, and COWs the file as required. |
| 1201 | * |
| 1202 | * If no cow copies or snapshots exist, we write directly to the existing |
| 1203 | * blocks on disk |
| 1204 | */ |
Chris Mason | 7f366cf | 2009-03-12 20:12:45 -0400 | [diff] [blame] | 1205 | static noinline int run_delalloc_nocow(struct inode *inode, |
| 1206 | struct page *locked_page, |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 1207 | u64 start, u64 end, int *page_started, int force, |
| 1208 | unsigned long *nr_written) |
Chris Mason | be20aa9 | 2007-12-17 20:14:01 -0500 | [diff] [blame] | 1209 | { |
Chris Mason | be20aa9 | 2007-12-17 20:14:01 -0500 | [diff] [blame] | 1210 | struct btrfs_root *root = BTRFS_I(inode)->root; |
Yan Zheng | 7ea394f | 2008-08-05 13:05:02 -0400 | [diff] [blame] | 1211 | struct btrfs_trans_handle *trans; |
Chris Mason | be20aa9 | 2007-12-17 20:14:01 -0500 | [diff] [blame] | 1212 | struct extent_buffer *leaf; |
Chris Mason | be20aa9 | 2007-12-17 20:14:01 -0500 | [diff] [blame] | 1213 | struct btrfs_path *path; |
Yan Zheng | 80ff385 | 2008-10-30 14:20:02 -0400 | [diff] [blame] | 1214 | struct btrfs_file_extent_item *fi; |
Chris Mason | be20aa9 | 2007-12-17 20:14:01 -0500 | [diff] [blame] | 1215 | struct btrfs_key found_key; |
Yan Zheng | 80ff385 | 2008-10-30 14:20:02 -0400 | [diff] [blame] | 1216 | u64 cow_start; |
| 1217 | u64 cur_offset; |
| 1218 | u64 extent_end; |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 1219 | u64 extent_offset; |
Yan Zheng | 80ff385 | 2008-10-30 14:20:02 -0400 | [diff] [blame] | 1220 | u64 disk_bytenr; |
| 1221 | u64 num_bytes; |
Josef Bacik | b493968 | 2012-12-03 10:31:19 -0500 | [diff] [blame] | 1222 | u64 disk_num_bytes; |
Josef Bacik | cc95bef | 2013-04-04 14:31:27 -0400 | [diff] [blame] | 1223 | u64 ram_bytes; |
Yan Zheng | 80ff385 | 2008-10-30 14:20:02 -0400 | [diff] [blame] | 1224 | int extent_type; |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 1225 | int ret, err; |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 1226 | int type; |
Yan Zheng | 80ff385 | 2008-10-30 14:20:02 -0400 | [diff] [blame] | 1227 | int nocow; |
| 1228 | int check_prev = 1; |
Li Zefan | 82d5902 | 2011-04-20 10:33:24 +0800 | [diff] [blame] | 1229 | bool nolock; |
Li Zefan | 33345d01 | 2011-04-20 10:31:50 +0800 | [diff] [blame] | 1230 | u64 ino = btrfs_ino(inode); |
Chris Mason | be20aa9 | 2007-12-17 20:14:01 -0500 | [diff] [blame] | 1231 | |
| 1232 | path = btrfs_alloc_path(); |
Josef Bacik | 17ca04a | 2012-05-31 15:58:55 -0400 | [diff] [blame] | 1233 | if (!path) { |
Josef Bacik | c2790a2 | 2013-07-29 11:20:47 -0400 | [diff] [blame] | 1234 | extent_clear_unlock_delalloc(inode, start, end, locked_page, |
| 1235 | EXTENT_LOCKED | EXTENT_DELALLOC | |
Josef Bacik | 151a41b | 2013-07-29 13:22:24 -0400 | [diff] [blame] | 1236 | EXTENT_DO_ACCOUNTING | |
| 1237 | EXTENT_DEFRAG, PAGE_UNLOCK | |
Josef Bacik | c2790a2 | 2013-07-29 11:20:47 -0400 | [diff] [blame] | 1238 | PAGE_CLEAR_DIRTY | |
| 1239 | PAGE_SET_WRITEBACK | |
| 1240 | PAGE_END_WRITEBACK); |
Mark Fasheh | d8926bb | 2011-07-13 10:38:47 -0700 | [diff] [blame] | 1241 | return -ENOMEM; |
Josef Bacik | 17ca04a | 2012-05-31 15:58:55 -0400 | [diff] [blame] | 1242 | } |
Li Zefan | 82d5902 | 2011-04-20 10:33:24 +0800 | [diff] [blame] | 1243 | |
Liu Bo | 83eea1f | 2012-07-10 05:28:39 -0600 | [diff] [blame] | 1244 | nolock = btrfs_is_free_space_inode(inode); |
Li Zefan | 82d5902 | 2011-04-20 10:33:24 +0800 | [diff] [blame] | 1245 | |
| 1246 | if (nolock) |
Josef Bacik | 7a7eaa4 | 2011-04-13 12:54:33 -0400 | [diff] [blame] | 1247 | trans = btrfs_join_transaction_nolock(root); |
Li Zefan | 82d5902 | 2011-04-20 10:33:24 +0800 | [diff] [blame] | 1248 | else |
Josef Bacik | 7a7eaa4 | 2011-04-13 12:54:33 -0400 | [diff] [blame] | 1249 | trans = btrfs_join_transaction(root); |
Chris Mason | ff5714c | 2011-05-28 07:00:39 -0400 | [diff] [blame] | 1250 | |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 1251 | if (IS_ERR(trans)) { |
Josef Bacik | c2790a2 | 2013-07-29 11:20:47 -0400 | [diff] [blame] | 1252 | extent_clear_unlock_delalloc(inode, start, end, locked_page, |
| 1253 | EXTENT_LOCKED | EXTENT_DELALLOC | |
Josef Bacik | 151a41b | 2013-07-29 13:22:24 -0400 | [diff] [blame] | 1254 | EXTENT_DO_ACCOUNTING | |
| 1255 | EXTENT_DEFRAG, PAGE_UNLOCK | |
Josef Bacik | c2790a2 | 2013-07-29 11:20:47 -0400 | [diff] [blame] | 1256 | PAGE_CLEAR_DIRTY | |
| 1257 | PAGE_SET_WRITEBACK | |
| 1258 | PAGE_END_WRITEBACK); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 1259 | btrfs_free_path(path); |
| 1260 | return PTR_ERR(trans); |
| 1261 | } |
| 1262 | |
Josef Bacik | 74b2107 | 2011-04-13 12:02:53 -0400 | [diff] [blame] | 1263 | trans->block_rsv = &root->fs_info->delalloc_block_rsv; |
Chris Mason | be20aa9 | 2007-12-17 20:14:01 -0500 | [diff] [blame] | 1264 | |
Yan Zheng | 80ff385 | 2008-10-30 14:20:02 -0400 | [diff] [blame] | 1265 | cow_start = (u64)-1; |
| 1266 | cur_offset = start; |
| 1267 | while (1) { |
Li Zefan | 33345d01 | 2011-04-20 10:31:50 +0800 | [diff] [blame] | 1268 | ret = btrfs_lookup_file_extent(trans, root, path, ino, |
Yan Zheng | 80ff385 | 2008-10-30 14:20:02 -0400 | [diff] [blame] | 1269 | cur_offset, 0); |
Josef Bacik | d788a34 | 2013-10-25 16:55:08 -0400 | [diff] [blame] | 1270 | if (ret < 0) |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 1271 | goto error; |
Yan Zheng | 80ff385 | 2008-10-30 14:20:02 -0400 | [diff] [blame] | 1272 | if (ret > 0 && path->slots[0] > 0 && check_prev) { |
| 1273 | leaf = path->nodes[0]; |
| 1274 | btrfs_item_key_to_cpu(leaf, &found_key, |
| 1275 | path->slots[0] - 1); |
Li Zefan | 33345d01 | 2011-04-20 10:31:50 +0800 | [diff] [blame] | 1276 | if (found_key.objectid == ino && |
Yan Zheng | 80ff385 | 2008-10-30 14:20:02 -0400 | [diff] [blame] | 1277 | found_key.type == BTRFS_EXTENT_DATA_KEY) |
| 1278 | path->slots[0]--; |
| 1279 | } |
| 1280 | check_prev = 0; |
| 1281 | next_slot: |
| 1282 | leaf = path->nodes[0]; |
| 1283 | if (path->slots[0] >= btrfs_header_nritems(leaf)) { |
| 1284 | ret = btrfs_next_leaf(root, path); |
Josef Bacik | d788a34 | 2013-10-25 16:55:08 -0400 | [diff] [blame] | 1285 | if (ret < 0) |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 1286 | goto error; |
Yan Zheng | 80ff385 | 2008-10-30 14:20:02 -0400 | [diff] [blame] | 1287 | if (ret > 0) |
| 1288 | break; |
| 1289 | leaf = path->nodes[0]; |
| 1290 | } |
Chris Mason | be20aa9 | 2007-12-17 20:14:01 -0500 | [diff] [blame] | 1291 | |
Yan Zheng | 80ff385 | 2008-10-30 14:20:02 -0400 | [diff] [blame] | 1292 | nocow = 0; |
| 1293 | disk_bytenr = 0; |
Yan Zheng | 17d217f | 2008-12-12 10:03:38 -0500 | [diff] [blame] | 1294 | num_bytes = 0; |
Yan Zheng | 80ff385 | 2008-10-30 14:20:02 -0400 | [diff] [blame] | 1295 | btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]); |
Chris Mason | be20aa9 | 2007-12-17 20:14:01 -0500 | [diff] [blame] | 1296 | |
Li Zefan | 33345d01 | 2011-04-20 10:31:50 +0800 | [diff] [blame] | 1297 | if (found_key.objectid > ino || |
Yan Zheng | 80ff385 | 2008-10-30 14:20:02 -0400 | [diff] [blame] | 1298 | found_key.type > BTRFS_EXTENT_DATA_KEY || |
| 1299 | found_key.offset > end) |
| 1300 | break; |
Chris Mason | be20aa9 | 2007-12-17 20:14:01 -0500 | [diff] [blame] | 1301 | |
Yan Zheng | 80ff385 | 2008-10-30 14:20:02 -0400 | [diff] [blame] | 1302 | if (found_key.offset > cur_offset) { |
| 1303 | extent_end = found_key.offset; |
Chris Mason | e9061e2 | 2009-10-09 09:57:45 -0400 | [diff] [blame] | 1304 | extent_type = 0; |
Yan Zheng | 80ff385 | 2008-10-30 14:20:02 -0400 | [diff] [blame] | 1305 | goto out_check; |
| 1306 | } |
Chris Mason | c31f883 | 2008-01-08 15:46:31 -0500 | [diff] [blame] | 1307 | |
Yan Zheng | 80ff385 | 2008-10-30 14:20:02 -0400 | [diff] [blame] | 1308 | fi = btrfs_item_ptr(leaf, path->slots[0], |
| 1309 | struct btrfs_file_extent_item); |
| 1310 | extent_type = btrfs_file_extent_type(leaf, fi); |
Chris Mason | be20aa9 | 2007-12-17 20:14:01 -0500 | [diff] [blame] | 1311 | |
Josef Bacik | cc95bef | 2013-04-04 14:31:27 -0400 | [diff] [blame] | 1312 | ram_bytes = btrfs_file_extent_ram_bytes(leaf, fi); |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 1313 | if (extent_type == BTRFS_FILE_EXTENT_REG || |
| 1314 | extent_type == BTRFS_FILE_EXTENT_PREALLOC) { |
Yan Zheng | 80ff385 | 2008-10-30 14:20:02 -0400 | [diff] [blame] | 1315 | disk_bytenr = btrfs_file_extent_disk_bytenr(leaf, fi); |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 1316 | extent_offset = btrfs_file_extent_offset(leaf, fi); |
Yan Zheng | 80ff385 | 2008-10-30 14:20:02 -0400 | [diff] [blame] | 1317 | extent_end = found_key.offset + |
| 1318 | btrfs_file_extent_num_bytes(leaf, fi); |
Josef Bacik | b493968 | 2012-12-03 10:31:19 -0500 | [diff] [blame] | 1319 | disk_num_bytes = |
| 1320 | btrfs_file_extent_disk_num_bytes(leaf, fi); |
Yan Zheng | 80ff385 | 2008-10-30 14:20:02 -0400 | [diff] [blame] | 1321 | if (extent_end <= start) { |
| 1322 | path->slots[0]++; |
| 1323 | goto next_slot; |
| 1324 | } |
Yan Zheng | 17d217f | 2008-12-12 10:03:38 -0500 | [diff] [blame] | 1325 | if (disk_bytenr == 0) |
| 1326 | goto out_check; |
Yan Zheng | 80ff385 | 2008-10-30 14:20:02 -0400 | [diff] [blame] | 1327 | if (btrfs_file_extent_compression(leaf, fi) || |
| 1328 | btrfs_file_extent_encryption(leaf, fi) || |
| 1329 | btrfs_file_extent_other_encoding(leaf, fi)) |
| 1330 | goto out_check; |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 1331 | if (extent_type == BTRFS_FILE_EXTENT_REG && !force) |
| 1332 | goto out_check; |
Yan Zheng | d2fb343 | 2008-12-11 16:30:39 -0500 | [diff] [blame] | 1333 | if (btrfs_extent_readonly(root, disk_bytenr)) |
Yan Zheng | 80ff385 | 2008-10-30 14:20:02 -0400 | [diff] [blame] | 1334 | goto out_check; |
Li Zefan | 33345d01 | 2011-04-20 10:31:50 +0800 | [diff] [blame] | 1335 | if (btrfs_cross_ref_exist(trans, root, ino, |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 1336 | found_key.offset - |
| 1337 | extent_offset, disk_bytenr)) |
Yan Zheng | 17d217f | 2008-12-12 10:03:38 -0500 | [diff] [blame] | 1338 | goto out_check; |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 1339 | disk_bytenr += extent_offset; |
Yan Zheng | 17d217f | 2008-12-12 10:03:38 -0500 | [diff] [blame] | 1340 | disk_bytenr += cur_offset - found_key.offset; |
| 1341 | num_bytes = min(end + 1, extent_end) - cur_offset; |
| 1342 | /* |
Wang Shilong | e9894fd | 2014-03-27 11:12:25 +0800 | [diff] [blame] | 1343 | * if there are pending snapshots for this root, |
| 1344 | * we fall into common COW way. |
| 1345 | */ |
| 1346 | if (!nolock) { |
Filipe Manana | 9ea24bb | 2014-10-29 11:57:59 +0000 | [diff] [blame] | 1347 | err = btrfs_start_write_no_snapshoting(root); |
Wang Shilong | e9894fd | 2014-03-27 11:12:25 +0800 | [diff] [blame] | 1348 | if (!err) |
| 1349 | goto out_check; |
| 1350 | } |
| 1351 | /* |
Yan Zheng | 17d217f | 2008-12-12 10:03:38 -0500 | [diff] [blame] | 1352 | * force cow if csum exists in the range. |
| 1353 | * this ensure that csum for a given extent are |
| 1354 | * either valid or do not exist. |
| 1355 | */ |
| 1356 | if (csum_exist_in_range(root, disk_bytenr, num_bytes)) |
| 1357 | goto out_check; |
Yan Zheng | 80ff385 | 2008-10-30 14:20:02 -0400 | [diff] [blame] | 1358 | nocow = 1; |
| 1359 | } else if (extent_type == BTRFS_FILE_EXTENT_INLINE) { |
| 1360 | extent_end = found_key.offset + |
Chris Mason | 514ac8a | 2014-01-03 21:07:00 -0800 | [diff] [blame] | 1361 | btrfs_file_extent_inline_len(leaf, |
| 1362 | path->slots[0], fi); |
Yan Zheng | 80ff385 | 2008-10-30 14:20:02 -0400 | [diff] [blame] | 1363 | extent_end = ALIGN(extent_end, root->sectorsize); |
| 1364 | } else { |
| 1365 | BUG_ON(1); |
| 1366 | } |
| 1367 | out_check: |
| 1368 | if (extent_end <= start) { |
| 1369 | path->slots[0]++; |
Wang Shilong | e9894fd | 2014-03-27 11:12:25 +0800 | [diff] [blame] | 1370 | if (!nolock && nocow) |
Filipe Manana | 9ea24bb | 2014-10-29 11:57:59 +0000 | [diff] [blame] | 1371 | btrfs_end_write_no_snapshoting(root); |
Yan Zheng | 80ff385 | 2008-10-30 14:20:02 -0400 | [diff] [blame] | 1372 | goto next_slot; |
| 1373 | } |
| 1374 | if (!nocow) { |
| 1375 | if (cow_start == (u64)-1) |
| 1376 | cow_start = cur_offset; |
| 1377 | cur_offset = extent_end; |
| 1378 | if (cur_offset > end) |
| 1379 | break; |
| 1380 | path->slots[0]++; |
| 1381 | goto next_slot; |
Yan Zheng | 7ea394f | 2008-08-05 13:05:02 -0400 | [diff] [blame] | 1382 | } |
Chris Mason | be20aa9 | 2007-12-17 20:14:01 -0500 | [diff] [blame] | 1383 | |
David Sterba | b3b4aa7 | 2011-04-21 01:20:15 +0200 | [diff] [blame] | 1384 | btrfs_release_path(path); |
Yan Zheng | 80ff385 | 2008-10-30 14:20:02 -0400 | [diff] [blame] | 1385 | if (cow_start != (u64)-1) { |
Josef Bacik | 0036158 | 2013-08-14 14:02:47 -0400 | [diff] [blame] | 1386 | ret = cow_file_range(inode, locked_page, |
| 1387 | cow_start, found_key.offset - 1, |
| 1388 | page_started, nr_written, 1); |
Wang Shilong | e9894fd | 2014-03-27 11:12:25 +0800 | [diff] [blame] | 1389 | if (ret) { |
| 1390 | if (!nolock && nocow) |
Filipe Manana | 9ea24bb | 2014-10-29 11:57:59 +0000 | [diff] [blame] | 1391 | btrfs_end_write_no_snapshoting(root); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 1392 | goto error; |
Wang Shilong | e9894fd | 2014-03-27 11:12:25 +0800 | [diff] [blame] | 1393 | } |
Yan Zheng | 80ff385 | 2008-10-30 14:20:02 -0400 | [diff] [blame] | 1394 | cow_start = (u64)-1; |
Yan Zheng | 7ea394f | 2008-08-05 13:05:02 -0400 | [diff] [blame] | 1395 | } |
Yan Zheng | 80ff385 | 2008-10-30 14:20:02 -0400 | [diff] [blame] | 1396 | |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 1397 | if (extent_type == BTRFS_FILE_EXTENT_PREALLOC) { |
| 1398 | struct extent_map *em; |
| 1399 | struct extent_map_tree *em_tree; |
| 1400 | em_tree = &BTRFS_I(inode)->extent_tree; |
David Sterba | 172ddd6 | 2011-04-21 00:48:27 +0200 | [diff] [blame] | 1401 | em = alloc_extent_map(); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 1402 | BUG_ON(!em); /* -ENOMEM */ |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 1403 | em->start = cur_offset; |
Josef Bacik | 70c8a91 | 2012-10-11 16:54:30 -0400 | [diff] [blame] | 1404 | em->orig_start = found_key.offset - extent_offset; |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 1405 | em->len = num_bytes; |
| 1406 | em->block_len = num_bytes; |
| 1407 | em->block_start = disk_bytenr; |
Josef Bacik | b493968 | 2012-12-03 10:31:19 -0500 | [diff] [blame] | 1408 | em->orig_block_len = disk_num_bytes; |
Josef Bacik | cc95bef | 2013-04-04 14:31:27 -0400 | [diff] [blame] | 1409 | em->ram_bytes = ram_bytes; |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 1410 | em->bdev = root->fs_info->fs_devices->latest_bdev; |
Josef Bacik | 2ab28f3 | 2012-10-12 15:27:49 -0400 | [diff] [blame] | 1411 | em->mod_start = em->start; |
| 1412 | em->mod_len = em->len; |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 1413 | set_bit(EXTENT_FLAG_PINNED, &em->flags); |
Josef Bacik | b11e234 | 2012-12-03 10:58:15 -0500 | [diff] [blame] | 1414 | set_bit(EXTENT_FLAG_FILLING, &em->flags); |
Josef Bacik | 70c8a91 | 2012-10-11 16:54:30 -0400 | [diff] [blame] | 1415 | em->generation = -1; |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 1416 | while (1) { |
Chris Mason | 890871b | 2009-09-02 16:24:52 -0400 | [diff] [blame] | 1417 | write_lock(&em_tree->lock); |
Josef Bacik | 09a2a8f9 | 2013-04-05 16:51:15 -0400 | [diff] [blame] | 1418 | ret = add_extent_mapping(em_tree, em, 1); |
Chris Mason | 890871b | 2009-09-02 16:24:52 -0400 | [diff] [blame] | 1419 | write_unlock(&em_tree->lock); |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 1420 | if (ret != -EEXIST) { |
| 1421 | free_extent_map(em); |
| 1422 | break; |
| 1423 | } |
| 1424 | btrfs_drop_extent_cache(inode, em->start, |
| 1425 | em->start + em->len - 1, 0); |
| 1426 | } |
| 1427 | type = BTRFS_ORDERED_PREALLOC; |
| 1428 | } else { |
| 1429 | type = BTRFS_ORDERED_NOCOW; |
| 1430 | } |
Yan Zheng | 80ff385 | 2008-10-30 14:20:02 -0400 | [diff] [blame] | 1431 | |
| 1432 | ret = btrfs_add_ordered_extent(inode, cur_offset, disk_bytenr, |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 1433 | num_bytes, num_bytes, type); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 1434 | BUG_ON(ret); /* -ENOMEM */ |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 1435 | |
Yan, Zheng | efa5646 | 2010-05-16 10:49:59 -0400 | [diff] [blame] | 1436 | if (root->root_key.objectid == |
| 1437 | BTRFS_DATA_RELOC_TREE_OBJECTID) { |
| 1438 | ret = btrfs_reloc_clone_csums(inode, cur_offset, |
| 1439 | num_bytes); |
Wang Shilong | e9894fd | 2014-03-27 11:12:25 +0800 | [diff] [blame] | 1440 | if (ret) { |
| 1441 | if (!nolock && nocow) |
Filipe Manana | 9ea24bb | 2014-10-29 11:57:59 +0000 | [diff] [blame] | 1442 | btrfs_end_write_no_snapshoting(root); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 1443 | goto error; |
Wang Shilong | e9894fd | 2014-03-27 11:12:25 +0800 | [diff] [blame] | 1444 | } |
Yan, Zheng | efa5646 | 2010-05-16 10:49:59 -0400 | [diff] [blame] | 1445 | } |
| 1446 | |
Josef Bacik | c2790a2 | 2013-07-29 11:20:47 -0400 | [diff] [blame] | 1447 | extent_clear_unlock_delalloc(inode, cur_offset, |
| 1448 | cur_offset + num_bytes - 1, |
| 1449 | locked_page, EXTENT_LOCKED | |
| 1450 | EXTENT_DELALLOC, PAGE_UNLOCK | |
| 1451 | PAGE_SET_PRIVATE2); |
Wang Shilong | e9894fd | 2014-03-27 11:12:25 +0800 | [diff] [blame] | 1452 | if (!nolock && nocow) |
Filipe Manana | 9ea24bb | 2014-10-29 11:57:59 +0000 | [diff] [blame] | 1453 | btrfs_end_write_no_snapshoting(root); |
Yan Zheng | 80ff385 | 2008-10-30 14:20:02 -0400 | [diff] [blame] | 1454 | cur_offset = extent_end; |
| 1455 | if (cur_offset > end) |
| 1456 | break; |
Yan Zheng | 7ea394f | 2008-08-05 13:05:02 -0400 | [diff] [blame] | 1457 | } |
David Sterba | b3b4aa7 | 2011-04-21 01:20:15 +0200 | [diff] [blame] | 1458 | btrfs_release_path(path); |
Yan Zheng | 80ff385 | 2008-10-30 14:20:02 -0400 | [diff] [blame] | 1459 | |
Josef Bacik | 17ca04a | 2012-05-31 15:58:55 -0400 | [diff] [blame] | 1460 | if (cur_offset <= end && cow_start == (u64)-1) { |
Yan Zheng | 80ff385 | 2008-10-30 14:20:02 -0400 | [diff] [blame] | 1461 | cow_start = cur_offset; |
Josef Bacik | 17ca04a | 2012-05-31 15:58:55 -0400 | [diff] [blame] | 1462 | cur_offset = end; |
| 1463 | } |
| 1464 | |
Yan Zheng | 80ff385 | 2008-10-30 14:20:02 -0400 | [diff] [blame] | 1465 | if (cow_start != (u64)-1) { |
Josef Bacik | 0036158 | 2013-08-14 14:02:47 -0400 | [diff] [blame] | 1466 | ret = cow_file_range(inode, locked_page, cow_start, end, |
| 1467 | page_started, nr_written, 1); |
Josef Bacik | d788a34 | 2013-10-25 16:55:08 -0400 | [diff] [blame] | 1468 | if (ret) |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 1469 | goto error; |
Yan Zheng | 80ff385 | 2008-10-30 14:20:02 -0400 | [diff] [blame] | 1470 | } |
| 1471 | |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 1472 | error: |
Miao Xie | a698d075 | 2012-09-20 01:51:59 -0600 | [diff] [blame] | 1473 | err = btrfs_end_transaction(trans, root); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 1474 | if (!ret) |
| 1475 | ret = err; |
| 1476 | |
Josef Bacik | 17ca04a | 2012-05-31 15:58:55 -0400 | [diff] [blame] | 1477 | if (ret && cur_offset < end) |
Josef Bacik | c2790a2 | 2013-07-29 11:20:47 -0400 | [diff] [blame] | 1478 | extent_clear_unlock_delalloc(inode, cur_offset, end, |
| 1479 | locked_page, EXTENT_LOCKED | |
Josef Bacik | 151a41b | 2013-07-29 13:22:24 -0400 | [diff] [blame] | 1480 | EXTENT_DELALLOC | EXTENT_DEFRAG | |
| 1481 | EXTENT_DO_ACCOUNTING, PAGE_UNLOCK | |
| 1482 | PAGE_CLEAR_DIRTY | |
Josef Bacik | c2790a2 | 2013-07-29 11:20:47 -0400 | [diff] [blame] | 1483 | PAGE_SET_WRITEBACK | |
| 1484 | PAGE_END_WRITEBACK); |
Yan Zheng | 7ea394f | 2008-08-05 13:05:02 -0400 | [diff] [blame] | 1485 | btrfs_free_path(path); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 1486 | return ret; |
Chris Mason | be20aa9 | 2007-12-17 20:14:01 -0500 | [diff] [blame] | 1487 | } |
| 1488 | |
Wang Shilong | 47059d9 | 2014-07-03 18:22:07 +0800 | [diff] [blame] | 1489 | static inline int need_force_cow(struct inode *inode, u64 start, u64 end) |
| 1490 | { |
| 1491 | |
| 1492 | if (!(BTRFS_I(inode)->flags & BTRFS_INODE_NODATACOW) && |
| 1493 | !(BTRFS_I(inode)->flags & BTRFS_INODE_PREALLOC)) |
| 1494 | return 0; |
| 1495 | |
| 1496 | /* |
| 1497 | * @defrag_bytes is a hint value, no spinlock held here, |
| 1498 | * if is not zero, it means the file is defragging. |
| 1499 | * Force cow if given extent needs to be defragged. |
| 1500 | */ |
| 1501 | if (BTRFS_I(inode)->defrag_bytes && |
| 1502 | test_range_bit(&BTRFS_I(inode)->io_tree, start, end, |
| 1503 | EXTENT_DEFRAG, 0, NULL)) |
| 1504 | return 1; |
| 1505 | |
| 1506 | return 0; |
| 1507 | } |
| 1508 | |
Chris Mason | d352ac6 | 2008-09-29 15:18:18 -0400 | [diff] [blame] | 1509 | /* |
| 1510 | * extent_io.c call back to do delayed allocation processing |
| 1511 | */ |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 1512 | static int run_delalloc_range(struct inode *inode, struct page *locked_page, |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 1513 | u64 start, u64 end, int *page_started, |
| 1514 | unsigned long *nr_written) |
Chris Mason | be20aa9 | 2007-12-17 20:14:01 -0500 | [diff] [blame] | 1515 | { |
Chris Mason | be20aa9 | 2007-12-17 20:14:01 -0500 | [diff] [blame] | 1516 | int ret; |
Wang Shilong | 47059d9 | 2014-07-03 18:22:07 +0800 | [diff] [blame] | 1517 | int force_cow = need_force_cow(inode, start, end); |
Chris Mason | a213501 | 2008-06-25 16:01:30 -0400 | [diff] [blame] | 1518 | |
Wang Shilong | 47059d9 | 2014-07-03 18:22:07 +0800 | [diff] [blame] | 1519 | if (BTRFS_I(inode)->flags & BTRFS_INODE_NODATACOW && !force_cow) { |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 1520 | ret = run_delalloc_nocow(inode, locked_page, start, end, |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 1521 | page_started, 1, nr_written); |
Wang Shilong | 47059d9 | 2014-07-03 18:22:07 +0800 | [diff] [blame] | 1522 | } else if (BTRFS_I(inode)->flags & BTRFS_INODE_PREALLOC && !force_cow) { |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 1523 | ret = run_delalloc_nocow(inode, locked_page, start, end, |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 1524 | page_started, 0, nr_written); |
Wang Shilong | 7816030 | 2014-07-17 11:44:10 +0800 | [diff] [blame] | 1525 | } else if (!inode_need_compress(inode)) { |
Chris Mason | 7f366cf | 2009-03-12 20:12:45 -0400 | [diff] [blame] | 1526 | ret = cow_file_range(inode, locked_page, start, end, |
| 1527 | page_started, nr_written, 1); |
Josef Bacik | 7ddf5a4 | 2012-06-08 15:26:47 -0400 | [diff] [blame] | 1528 | } else { |
| 1529 | set_bit(BTRFS_INODE_HAS_ASYNC_EXTENT, |
| 1530 | &BTRFS_I(inode)->runtime_flags); |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 1531 | ret = cow_file_range_async(inode, locked_page, start, end, |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 1532 | page_started, nr_written); |
Josef Bacik | 7ddf5a4 | 2012-06-08 15:26:47 -0400 | [diff] [blame] | 1533 | } |
Chris Mason | b888db2 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 1534 | return ret; |
| 1535 | } |
| 1536 | |
Jeff Mahoney | 1bf8504 | 2011-07-21 16:56:09 +0000 | [diff] [blame] | 1537 | static void btrfs_split_extent_hook(struct inode *inode, |
| 1538 | struct extent_state *orig, u64 split) |
Josef Bacik | 9ed74f2 | 2009-09-11 16:12:44 -0400 | [diff] [blame] | 1539 | { |
Josef Bacik | dcab6a3 | 2015-02-11 15:08:59 -0500 | [diff] [blame] | 1540 | u64 size; |
| 1541 | |
Yan, Zheng | 0ca1f7c | 2010-05-16 10:48:47 -0400 | [diff] [blame] | 1542 | /* not delalloc, ignore it */ |
Josef Bacik | 9ed74f2 | 2009-09-11 16:12:44 -0400 | [diff] [blame] | 1543 | if (!(orig->state & EXTENT_DELALLOC)) |
Jeff Mahoney | 1bf8504 | 2011-07-21 16:56:09 +0000 | [diff] [blame] | 1544 | return; |
Josef Bacik | 9ed74f2 | 2009-09-11 16:12:44 -0400 | [diff] [blame] | 1545 | |
Josef Bacik | dcab6a3 | 2015-02-11 15:08:59 -0500 | [diff] [blame] | 1546 | size = orig->end - orig->start + 1; |
| 1547 | if (size > BTRFS_MAX_EXTENT_SIZE) { |
| 1548 | u64 num_extents; |
| 1549 | u64 new_size; |
| 1550 | |
| 1551 | /* |
Josef Bacik | ba11721 | 2015-03-13 15:01:24 -0400 | [diff] [blame] | 1552 | * See the explanation in btrfs_merge_extent_hook, the same |
| 1553 | * applies here, just in reverse. |
Josef Bacik | dcab6a3 | 2015-02-11 15:08:59 -0500 | [diff] [blame] | 1554 | */ |
| 1555 | new_size = orig->end - split + 1; |
Josef Bacik | ba11721 | 2015-03-13 15:01:24 -0400 | [diff] [blame] | 1556 | num_extents = div64_u64(new_size + BTRFS_MAX_EXTENT_SIZE - 1, |
Josef Bacik | dcab6a3 | 2015-02-11 15:08:59 -0500 | [diff] [blame] | 1557 | BTRFS_MAX_EXTENT_SIZE); |
Josef Bacik | ba11721 | 2015-03-13 15:01:24 -0400 | [diff] [blame] | 1558 | new_size = split - orig->start; |
| 1559 | num_extents += div64_u64(new_size + BTRFS_MAX_EXTENT_SIZE - 1, |
| 1560 | BTRFS_MAX_EXTENT_SIZE); |
| 1561 | if (div64_u64(size + BTRFS_MAX_EXTENT_SIZE - 1, |
| 1562 | BTRFS_MAX_EXTENT_SIZE) >= num_extents) |
Josef Bacik | dcab6a3 | 2015-02-11 15:08:59 -0500 | [diff] [blame] | 1563 | return; |
| 1564 | } |
| 1565 | |
Josef Bacik | 9e0baf6 | 2011-07-15 15:16:44 +0000 | [diff] [blame] | 1566 | spin_lock(&BTRFS_I(inode)->lock); |
| 1567 | BTRFS_I(inode)->outstanding_extents++; |
| 1568 | spin_unlock(&BTRFS_I(inode)->lock); |
Josef Bacik | 9ed74f2 | 2009-09-11 16:12:44 -0400 | [diff] [blame] | 1569 | } |
| 1570 | |
| 1571 | /* |
| 1572 | * extent_io.c merge_extent_hook, used to track merged delayed allocation |
| 1573 | * extents so we can keep track of new extents that are just merged onto old |
| 1574 | * extents, such as when we are doing sequential writes, so we can properly |
| 1575 | * account for the metadata space we'll need. |
| 1576 | */ |
Jeff Mahoney | 1bf8504 | 2011-07-21 16:56:09 +0000 | [diff] [blame] | 1577 | static void btrfs_merge_extent_hook(struct inode *inode, |
| 1578 | struct extent_state *new, |
| 1579 | struct extent_state *other) |
Josef Bacik | 9ed74f2 | 2009-09-11 16:12:44 -0400 | [diff] [blame] | 1580 | { |
Josef Bacik | dcab6a3 | 2015-02-11 15:08:59 -0500 | [diff] [blame] | 1581 | u64 new_size, old_size; |
| 1582 | u64 num_extents; |
| 1583 | |
Josef Bacik | 9ed74f2 | 2009-09-11 16:12:44 -0400 | [diff] [blame] | 1584 | /* not delalloc, ignore it */ |
| 1585 | if (!(other->state & EXTENT_DELALLOC)) |
Jeff Mahoney | 1bf8504 | 2011-07-21 16:56:09 +0000 | [diff] [blame] | 1586 | return; |
Josef Bacik | 9ed74f2 | 2009-09-11 16:12:44 -0400 | [diff] [blame] | 1587 | |
Josef Bacik | 8461a3d | 2015-03-13 15:12:08 -0400 | [diff] [blame] | 1588 | if (new->start > other->start) |
| 1589 | new_size = new->end - other->start + 1; |
| 1590 | else |
| 1591 | new_size = other->end - new->start + 1; |
Josef Bacik | dcab6a3 | 2015-02-11 15:08:59 -0500 | [diff] [blame] | 1592 | |
| 1593 | /* we're not bigger than the max, unreserve the space and go */ |
| 1594 | if (new_size <= BTRFS_MAX_EXTENT_SIZE) { |
| 1595 | spin_lock(&BTRFS_I(inode)->lock); |
| 1596 | BTRFS_I(inode)->outstanding_extents--; |
| 1597 | spin_unlock(&BTRFS_I(inode)->lock); |
| 1598 | return; |
| 1599 | } |
| 1600 | |
| 1601 | /* |
Josef Bacik | ba11721 | 2015-03-13 15:01:24 -0400 | [diff] [blame] | 1602 | * We have to add up either side to figure out how many extents were |
| 1603 | * accounted for before we merged into one big extent. If the number of |
| 1604 | * extents we accounted for is <= the amount we need for the new range |
| 1605 | * then we can return, otherwise drop. Think of it like this |
| 1606 | * |
| 1607 | * [ 4k][MAX_SIZE] |
| 1608 | * |
| 1609 | * So we've grown the extent by a MAX_SIZE extent, this would mean we |
| 1610 | * need 2 outstanding extents, on one side we have 1 and the other side |
| 1611 | * we have 1 so they are == and we can return. But in this case |
| 1612 | * |
| 1613 | * [MAX_SIZE+4k][MAX_SIZE+4k] |
| 1614 | * |
| 1615 | * Each range on their own accounts for 2 extents, but merged together |
| 1616 | * they are only 3 extents worth of accounting, so we need to drop in |
| 1617 | * this case. |
Josef Bacik | dcab6a3 | 2015-02-11 15:08:59 -0500 | [diff] [blame] | 1618 | */ |
Josef Bacik | ba11721 | 2015-03-13 15:01:24 -0400 | [diff] [blame] | 1619 | old_size = other->end - other->start + 1; |
Josef Bacik | dcab6a3 | 2015-02-11 15:08:59 -0500 | [diff] [blame] | 1620 | num_extents = div64_u64(old_size + BTRFS_MAX_EXTENT_SIZE - 1, |
| 1621 | BTRFS_MAX_EXTENT_SIZE); |
Josef Bacik | ba11721 | 2015-03-13 15:01:24 -0400 | [diff] [blame] | 1622 | old_size = new->end - new->start + 1; |
| 1623 | num_extents += div64_u64(old_size + BTRFS_MAX_EXTENT_SIZE - 1, |
| 1624 | BTRFS_MAX_EXTENT_SIZE); |
| 1625 | |
Josef Bacik | dcab6a3 | 2015-02-11 15:08:59 -0500 | [diff] [blame] | 1626 | if (div64_u64(new_size + BTRFS_MAX_EXTENT_SIZE - 1, |
Josef Bacik | ba11721 | 2015-03-13 15:01:24 -0400 | [diff] [blame] | 1627 | BTRFS_MAX_EXTENT_SIZE) >= num_extents) |
Josef Bacik | dcab6a3 | 2015-02-11 15:08:59 -0500 | [diff] [blame] | 1628 | return; |
| 1629 | |
Josef Bacik | 9e0baf6 | 2011-07-15 15:16:44 +0000 | [diff] [blame] | 1630 | spin_lock(&BTRFS_I(inode)->lock); |
| 1631 | BTRFS_I(inode)->outstanding_extents--; |
| 1632 | spin_unlock(&BTRFS_I(inode)->lock); |
Josef Bacik | 9ed74f2 | 2009-09-11 16:12:44 -0400 | [diff] [blame] | 1633 | } |
| 1634 | |
Miao Xie | eb73c1b | 2013-05-15 07:48:22 +0000 | [diff] [blame] | 1635 | static void btrfs_add_delalloc_inodes(struct btrfs_root *root, |
| 1636 | struct inode *inode) |
| 1637 | { |
| 1638 | spin_lock(&root->delalloc_lock); |
| 1639 | if (list_empty(&BTRFS_I(inode)->delalloc_inodes)) { |
| 1640 | list_add_tail(&BTRFS_I(inode)->delalloc_inodes, |
| 1641 | &root->delalloc_inodes); |
| 1642 | set_bit(BTRFS_INODE_IN_DELALLOC_LIST, |
| 1643 | &BTRFS_I(inode)->runtime_flags); |
| 1644 | root->nr_delalloc_inodes++; |
| 1645 | if (root->nr_delalloc_inodes == 1) { |
| 1646 | spin_lock(&root->fs_info->delalloc_root_lock); |
| 1647 | BUG_ON(!list_empty(&root->delalloc_root)); |
| 1648 | list_add_tail(&root->delalloc_root, |
| 1649 | &root->fs_info->delalloc_roots); |
| 1650 | spin_unlock(&root->fs_info->delalloc_root_lock); |
| 1651 | } |
| 1652 | } |
| 1653 | spin_unlock(&root->delalloc_lock); |
| 1654 | } |
| 1655 | |
| 1656 | static void btrfs_del_delalloc_inode(struct btrfs_root *root, |
| 1657 | struct inode *inode) |
| 1658 | { |
| 1659 | spin_lock(&root->delalloc_lock); |
| 1660 | if (!list_empty(&BTRFS_I(inode)->delalloc_inodes)) { |
| 1661 | list_del_init(&BTRFS_I(inode)->delalloc_inodes); |
| 1662 | clear_bit(BTRFS_INODE_IN_DELALLOC_LIST, |
| 1663 | &BTRFS_I(inode)->runtime_flags); |
| 1664 | root->nr_delalloc_inodes--; |
| 1665 | if (!root->nr_delalloc_inodes) { |
| 1666 | spin_lock(&root->fs_info->delalloc_root_lock); |
| 1667 | BUG_ON(list_empty(&root->delalloc_root)); |
| 1668 | list_del_init(&root->delalloc_root); |
| 1669 | spin_unlock(&root->fs_info->delalloc_root_lock); |
| 1670 | } |
| 1671 | } |
| 1672 | spin_unlock(&root->delalloc_lock); |
| 1673 | } |
| 1674 | |
Chris Mason | d352ac6 | 2008-09-29 15:18:18 -0400 | [diff] [blame] | 1675 | /* |
| 1676 | * extent_io.c set_bit_hook, used to track delayed allocation |
| 1677 | * bytes in this file, and to maintain the list of inodes that |
| 1678 | * have pending delalloc work to be done. |
| 1679 | */ |
Jeff Mahoney | 1bf8504 | 2011-07-21 16:56:09 +0000 | [diff] [blame] | 1680 | static void btrfs_set_bit_hook(struct inode *inode, |
David Sterba | 9ee49a04 | 2015-01-14 19:52:13 +0100 | [diff] [blame] | 1681 | struct extent_state *state, unsigned *bits) |
Chris Mason | 291d673 | 2008-01-29 15:55:23 -0500 | [diff] [blame] | 1682 | { |
Josef Bacik | 9ed74f2 | 2009-09-11 16:12:44 -0400 | [diff] [blame] | 1683 | |
Wang Shilong | 47059d9 | 2014-07-03 18:22:07 +0800 | [diff] [blame] | 1684 | if ((*bits & EXTENT_DEFRAG) && !(*bits & EXTENT_DELALLOC)) |
| 1685 | WARN_ON(1); |
Chris Mason | 75eff68 | 2008-12-15 15:54:40 -0500 | [diff] [blame] | 1686 | /* |
| 1687 | * set_bit and clear bit hooks normally require _irqsave/restore |
Sergei Trofimovich | 27160b6 | 2011-05-20 20:20:32 +0000 | [diff] [blame] | 1688 | * but in this case, we are only testing for the DELALLOC |
Chris Mason | 75eff68 | 2008-12-15 15:54:40 -0500 | [diff] [blame] | 1689 | * bit, which is only set or cleared with irqs on |
| 1690 | */ |
Yan, Zheng | 0ca1f7c | 2010-05-16 10:48:47 -0400 | [diff] [blame] | 1691 | if (!(state->state & EXTENT_DELALLOC) && (*bits & EXTENT_DELALLOC)) { |
Chris Mason | 291d673 | 2008-01-29 15:55:23 -0500 | [diff] [blame] | 1692 | struct btrfs_root *root = BTRFS_I(inode)->root; |
Yan, Zheng | 0ca1f7c | 2010-05-16 10:48:47 -0400 | [diff] [blame] | 1693 | u64 len = state->end + 1 - state->start; |
Liu Bo | 83eea1f | 2012-07-10 05:28:39 -0600 | [diff] [blame] | 1694 | bool do_list = !btrfs_is_free_space_inode(inode); |
Josef Bacik | 9ed74f2 | 2009-09-11 16:12:44 -0400 | [diff] [blame] | 1695 | |
Josef Bacik | 9e0baf6 | 2011-07-15 15:16:44 +0000 | [diff] [blame] | 1696 | if (*bits & EXTENT_FIRST_DELALLOC) { |
Yan, Zheng | 0ca1f7c | 2010-05-16 10:48:47 -0400 | [diff] [blame] | 1697 | *bits &= ~EXTENT_FIRST_DELALLOC; |
Josef Bacik | 9e0baf6 | 2011-07-15 15:16:44 +0000 | [diff] [blame] | 1698 | } else { |
| 1699 | spin_lock(&BTRFS_I(inode)->lock); |
| 1700 | BTRFS_I(inode)->outstanding_extents++; |
| 1701 | spin_unlock(&BTRFS_I(inode)->lock); |
| 1702 | } |
Josef Bacik | 287a0ab | 2010-03-19 18:07:23 +0000 | [diff] [blame] | 1703 | |
Josef Bacik | 6a3891c | 2015-03-16 17:38:52 -0400 | [diff] [blame] | 1704 | /* For sanity tests */ |
| 1705 | if (btrfs_test_is_dummy_root(root)) |
| 1706 | return; |
| 1707 | |
Miao Xie | 963d678 | 2013-01-29 10:10:51 +0000 | [diff] [blame] | 1708 | __percpu_counter_add(&root->fs_info->delalloc_bytes, len, |
| 1709 | root->fs_info->delalloc_batch); |
Miao Xie | df0af1a | 2013-01-29 10:11:59 +0000 | [diff] [blame] | 1710 | spin_lock(&BTRFS_I(inode)->lock); |
Yan, Zheng | 0ca1f7c | 2010-05-16 10:48:47 -0400 | [diff] [blame] | 1711 | BTRFS_I(inode)->delalloc_bytes += len; |
Wang Shilong | 47059d9 | 2014-07-03 18:22:07 +0800 | [diff] [blame] | 1712 | if (*bits & EXTENT_DEFRAG) |
| 1713 | BTRFS_I(inode)->defrag_bytes += len; |
Miao Xie | df0af1a | 2013-01-29 10:11:59 +0000 | [diff] [blame] | 1714 | if (do_list && !test_bit(BTRFS_INODE_IN_DELALLOC_LIST, |
Miao Xie | eb73c1b | 2013-05-15 07:48:22 +0000 | [diff] [blame] | 1715 | &BTRFS_I(inode)->runtime_flags)) |
| 1716 | btrfs_add_delalloc_inodes(root, inode); |
Miao Xie | df0af1a | 2013-01-29 10:11:59 +0000 | [diff] [blame] | 1717 | spin_unlock(&BTRFS_I(inode)->lock); |
Chris Mason | 291d673 | 2008-01-29 15:55:23 -0500 | [diff] [blame] | 1718 | } |
Chris Mason | 291d673 | 2008-01-29 15:55:23 -0500 | [diff] [blame] | 1719 | } |
| 1720 | |
Chris Mason | d352ac6 | 2008-09-29 15:18:18 -0400 | [diff] [blame] | 1721 | /* |
| 1722 | * extent_io.c clear_bit_hook, see set_bit_hook for why |
| 1723 | */ |
Jeff Mahoney | 1bf8504 | 2011-07-21 16:56:09 +0000 | [diff] [blame] | 1724 | static void btrfs_clear_bit_hook(struct inode *inode, |
David Sterba | 4107488 | 2013-04-29 13:38:46 +0000 | [diff] [blame] | 1725 | struct extent_state *state, |
David Sterba | 9ee49a04 | 2015-01-14 19:52:13 +0100 | [diff] [blame] | 1726 | unsigned *bits) |
Chris Mason | 291d673 | 2008-01-29 15:55:23 -0500 | [diff] [blame] | 1727 | { |
Wang Shilong | 47059d9 | 2014-07-03 18:22:07 +0800 | [diff] [blame] | 1728 | u64 len = state->end + 1 - state->start; |
Josef Bacik | dcab6a3 | 2015-02-11 15:08:59 -0500 | [diff] [blame] | 1729 | u64 num_extents = div64_u64(len + BTRFS_MAX_EXTENT_SIZE -1, |
| 1730 | BTRFS_MAX_EXTENT_SIZE); |
Wang Shilong | 47059d9 | 2014-07-03 18:22:07 +0800 | [diff] [blame] | 1731 | |
| 1732 | spin_lock(&BTRFS_I(inode)->lock); |
| 1733 | if ((state->state & EXTENT_DEFRAG) && (*bits & EXTENT_DEFRAG)) |
| 1734 | BTRFS_I(inode)->defrag_bytes -= len; |
| 1735 | spin_unlock(&BTRFS_I(inode)->lock); |
| 1736 | |
Chris Mason | 75eff68 | 2008-12-15 15:54:40 -0500 | [diff] [blame] | 1737 | /* |
| 1738 | * set_bit and clear bit hooks normally require _irqsave/restore |
Sergei Trofimovich | 27160b6 | 2011-05-20 20:20:32 +0000 | [diff] [blame] | 1739 | * but in this case, we are only testing for the DELALLOC |
Chris Mason | 75eff68 | 2008-12-15 15:54:40 -0500 | [diff] [blame] | 1740 | * bit, which is only set or cleared with irqs on |
| 1741 | */ |
Yan, Zheng | 0ca1f7c | 2010-05-16 10:48:47 -0400 | [diff] [blame] | 1742 | if ((state->state & EXTENT_DELALLOC) && (*bits & EXTENT_DELALLOC)) { |
Chris Mason | 291d673 | 2008-01-29 15:55:23 -0500 | [diff] [blame] | 1743 | struct btrfs_root *root = BTRFS_I(inode)->root; |
Liu Bo | 83eea1f | 2012-07-10 05:28:39 -0600 | [diff] [blame] | 1744 | bool do_list = !btrfs_is_free_space_inode(inode); |
Chris Mason | bcbfce8 | 2008-04-22 13:26:47 -0400 | [diff] [blame] | 1745 | |
Josef Bacik | 9e0baf6 | 2011-07-15 15:16:44 +0000 | [diff] [blame] | 1746 | if (*bits & EXTENT_FIRST_DELALLOC) { |
Yan, Zheng | 0ca1f7c | 2010-05-16 10:48:47 -0400 | [diff] [blame] | 1747 | *bits &= ~EXTENT_FIRST_DELALLOC; |
Josef Bacik | 9e0baf6 | 2011-07-15 15:16:44 +0000 | [diff] [blame] | 1748 | } else if (!(*bits & EXTENT_DO_ACCOUNTING)) { |
| 1749 | spin_lock(&BTRFS_I(inode)->lock); |
Josef Bacik | dcab6a3 | 2015-02-11 15:08:59 -0500 | [diff] [blame] | 1750 | BTRFS_I(inode)->outstanding_extents -= num_extents; |
Josef Bacik | 9e0baf6 | 2011-07-15 15:16:44 +0000 | [diff] [blame] | 1751 | spin_unlock(&BTRFS_I(inode)->lock); |
| 1752 | } |
Yan, Zheng | 0ca1f7c | 2010-05-16 10:48:47 -0400 | [diff] [blame] | 1753 | |
Josef Bacik | b6d08f0 | 2013-09-27 14:57:43 -0400 | [diff] [blame] | 1754 | /* |
| 1755 | * We don't reserve metadata space for space cache inodes so we |
| 1756 | * don't need to call dellalloc_release_metadata if there is an |
| 1757 | * error. |
| 1758 | */ |
| 1759 | if (*bits & EXTENT_DO_ACCOUNTING && |
| 1760 | root != root->fs_info->tree_root) |
Yan, Zheng | 0ca1f7c | 2010-05-16 10:48:47 -0400 | [diff] [blame] | 1761 | btrfs_delalloc_release_metadata(inode, len); |
| 1762 | |
Josef Bacik | 6a3891c | 2015-03-16 17:38:52 -0400 | [diff] [blame] | 1763 | /* For sanity tests. */ |
| 1764 | if (btrfs_test_is_dummy_root(root)) |
| 1765 | return; |
| 1766 | |
Josef Bacik | 0cb59c9 | 2010-07-02 12:14:14 -0400 | [diff] [blame] | 1767 | if (root->root_key.objectid != BTRFS_DATA_RELOC_TREE_OBJECTID |
Josef Bacik | 7ee9e44 | 2013-06-21 16:37:03 -0400 | [diff] [blame] | 1768 | && do_list && !(state->state & EXTENT_NORESERVE)) |
Yan, Zheng | 0ca1f7c | 2010-05-16 10:48:47 -0400 | [diff] [blame] | 1769 | btrfs_free_reserved_data_space(inode, len); |
Josef Bacik | 9ed74f2 | 2009-09-11 16:12:44 -0400 | [diff] [blame] | 1770 | |
Miao Xie | 963d678 | 2013-01-29 10:10:51 +0000 | [diff] [blame] | 1771 | __percpu_counter_add(&root->fs_info->delalloc_bytes, -len, |
| 1772 | root->fs_info->delalloc_batch); |
Miao Xie | df0af1a | 2013-01-29 10:11:59 +0000 | [diff] [blame] | 1773 | spin_lock(&BTRFS_I(inode)->lock); |
Yan, Zheng | 0ca1f7c | 2010-05-16 10:48:47 -0400 | [diff] [blame] | 1774 | BTRFS_I(inode)->delalloc_bytes -= len; |
Josef Bacik | 0cb59c9 | 2010-07-02 12:14:14 -0400 | [diff] [blame] | 1775 | if (do_list && BTRFS_I(inode)->delalloc_bytes == 0 && |
Miao Xie | df0af1a | 2013-01-29 10:11:59 +0000 | [diff] [blame] | 1776 | test_bit(BTRFS_INODE_IN_DELALLOC_LIST, |
Miao Xie | eb73c1b | 2013-05-15 07:48:22 +0000 | [diff] [blame] | 1777 | &BTRFS_I(inode)->runtime_flags)) |
| 1778 | btrfs_del_delalloc_inode(root, inode); |
Miao Xie | df0af1a | 2013-01-29 10:11:59 +0000 | [diff] [blame] | 1779 | spin_unlock(&BTRFS_I(inode)->lock); |
Chris Mason | 291d673 | 2008-01-29 15:55:23 -0500 | [diff] [blame] | 1780 | } |
Chris Mason | 291d673 | 2008-01-29 15:55:23 -0500 | [diff] [blame] | 1781 | } |
| 1782 | |
Chris Mason | d352ac6 | 2008-09-29 15:18:18 -0400 | [diff] [blame] | 1783 | /* |
| 1784 | * extent_io.c merge_bio_hook, this must check the chunk tree to make sure |
| 1785 | * we don't create bios that span stripes or chunks |
| 1786 | */ |
David Woodhouse | 64a1670 | 2009-07-15 23:29:37 +0100 | [diff] [blame] | 1787 | 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] | 1788 | size_t size, struct bio *bio, |
| 1789 | unsigned long bio_flags) |
Chris Mason | 239b14b | 2008-03-24 15:02:07 -0400 | [diff] [blame] | 1790 | { |
| 1791 | struct btrfs_root *root = BTRFS_I(page->mapping->host)->root; |
Kent Overstreet | 4f024f3 | 2013-10-11 15:44:27 -0700 | [diff] [blame] | 1792 | u64 logical = (u64)bio->bi_iter.bi_sector << 9; |
Chris Mason | 239b14b | 2008-03-24 15:02:07 -0400 | [diff] [blame] | 1793 | u64 length = 0; |
| 1794 | u64 map_length; |
Chris Mason | 239b14b | 2008-03-24 15:02:07 -0400 | [diff] [blame] | 1795 | int ret; |
| 1796 | |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 1797 | if (bio_flags & EXTENT_BIO_COMPRESSED) |
| 1798 | return 0; |
| 1799 | |
Kent Overstreet | 4f024f3 | 2013-10-11 15:44:27 -0700 | [diff] [blame] | 1800 | length = bio->bi_iter.bi_size; |
Chris Mason | 239b14b | 2008-03-24 15:02:07 -0400 | [diff] [blame] | 1801 | map_length = length; |
David Woodhouse | 64a1670 | 2009-07-15 23:29:37 +0100 | [diff] [blame] | 1802 | ret = btrfs_map_block(root->fs_info, rw, logical, |
Chris Mason | f188591 | 2008-04-09 16:28:12 -0400 | [diff] [blame] | 1803 | &map_length, NULL, 0); |
Stefan Behrens | 3ec706c | 2012-11-05 15:46:42 +0100 | [diff] [blame] | 1804 | /* Will always return 0 with map_multi == NULL */ |
Jeff Mahoney | 3444a97 | 2011-10-03 23:23:13 -0400 | [diff] [blame] | 1805 | BUG_ON(ret < 0); |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 1806 | if (map_length < length + size) |
Chris Mason | 239b14b | 2008-03-24 15:02:07 -0400 | [diff] [blame] | 1807 | return 1; |
Jeff Mahoney | 3444a97 | 2011-10-03 23:23:13 -0400 | [diff] [blame] | 1808 | return 0; |
Chris Mason | 239b14b | 2008-03-24 15:02:07 -0400 | [diff] [blame] | 1809 | } |
| 1810 | |
Chris Mason | d352ac6 | 2008-09-29 15:18:18 -0400 | [diff] [blame] | 1811 | /* |
| 1812 | * in order to insert checksums into the metadata in large chunks, |
| 1813 | * we wait until bio submission time. All the pages in the bio are |
| 1814 | * checksummed and sums are attached onto the ordered extent record. |
| 1815 | * |
| 1816 | * At IO completion time the cums attached on the ordered extent record |
| 1817 | * are inserted into the btree |
| 1818 | */ |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 1819 | static int __btrfs_submit_bio_start(struct inode *inode, int rw, |
| 1820 | struct bio *bio, int mirror_num, |
Chris Mason | eaf25d9 | 2010-05-25 09:48:28 -0400 | [diff] [blame] | 1821 | unsigned long bio_flags, |
| 1822 | u64 bio_offset) |
Chris Mason | 065631f | 2008-02-20 12:07:25 -0500 | [diff] [blame] | 1823 | { |
Chris Mason | 065631f | 2008-02-20 12:07:25 -0500 | [diff] [blame] | 1824 | struct btrfs_root *root = BTRFS_I(inode)->root; |
Chris Mason | 065631f | 2008-02-20 12:07:25 -0500 | [diff] [blame] | 1825 | int ret = 0; |
Chris Mason | e015640 | 2008-04-16 11:15:20 -0400 | [diff] [blame] | 1826 | |
Chris Mason | d20f704 | 2008-12-08 16:58:54 -0500 | [diff] [blame] | 1827 | ret = btrfs_csum_one_bio(root, inode, bio, 0, 0); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 1828 | BUG_ON(ret); /* -ENOMEM */ |
Chris Mason | 4a69a41 | 2008-11-06 22:03:00 -0500 | [diff] [blame] | 1829 | return 0; |
| 1830 | } |
Chris Mason | e015640 | 2008-04-16 11:15:20 -0400 | [diff] [blame] | 1831 | |
Chris Mason | 4a69a41 | 2008-11-06 22:03:00 -0500 | [diff] [blame] | 1832 | /* |
| 1833 | * in order to insert checksums into the metadata in large chunks, |
| 1834 | * we wait until bio submission time. All the pages in the bio are |
| 1835 | * checksummed and sums are attached onto the ordered extent record. |
| 1836 | * |
| 1837 | * At IO completion time the cums attached on the ordered extent record |
| 1838 | * are inserted into the btree |
| 1839 | */ |
Christoph Hellwig | b295086 | 2008-12-02 09:54:17 -0500 | [diff] [blame] | 1840 | 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] | 1841 | int mirror_num, unsigned long bio_flags, |
| 1842 | u64 bio_offset) |
Chris Mason | 4a69a41 | 2008-11-06 22:03:00 -0500 | [diff] [blame] | 1843 | { |
| 1844 | struct btrfs_root *root = BTRFS_I(inode)->root; |
Stefan Behrens | 6189192 | 2012-11-05 18:51:52 +0100 | [diff] [blame] | 1845 | int ret; |
| 1846 | |
| 1847 | ret = btrfs_map_bio(root, rw, bio, mirror_num, 1); |
| 1848 | if (ret) |
| 1849 | bio_endio(bio, ret); |
| 1850 | return ret; |
Chris Mason | 44b8bd7 | 2008-04-16 11:14:51 -0400 | [diff] [blame] | 1851 | } |
| 1852 | |
Chris Mason | d352ac6 | 2008-09-29 15:18:18 -0400 | [diff] [blame] | 1853 | /* |
Chris Mason | cad321a | 2008-12-17 14:51:42 -0500 | [diff] [blame] | 1854 | * extent_io.c submission hook. This does the right thing for csum calculation |
| 1855 | * on write, or reading the csums from the tree before a read |
Chris Mason | d352ac6 | 2008-09-29 15:18:18 -0400 | [diff] [blame] | 1856 | */ |
Christoph Hellwig | b295086 | 2008-12-02 09:54:17 -0500 | [diff] [blame] | 1857 | 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] | 1858 | int mirror_num, unsigned long bio_flags, |
| 1859 | u64 bio_offset) |
Chris Mason | 44b8bd7 | 2008-04-16 11:14:51 -0400 | [diff] [blame] | 1860 | { |
| 1861 | struct btrfs_root *root = BTRFS_I(inode)->root; |
| 1862 | int ret = 0; |
Chris Mason | 19b9bdb | 2008-10-30 14:23:13 -0400 | [diff] [blame] | 1863 | int skip_sum; |
Jeff Mahoney | 0417341 | 2011-10-03 23:23:12 -0400 | [diff] [blame] | 1864 | int metadata = 0; |
Josef Bacik | b812ce2 | 2012-11-16 13:56:32 -0500 | [diff] [blame] | 1865 | int async = !atomic_read(&BTRFS_I(inode)->sync_writers); |
Chris Mason | 44b8bd7 | 2008-04-16 11:14:51 -0400 | [diff] [blame] | 1866 | |
Christoph Hellwig | 6cbff00 | 2009-04-17 10:37:41 +0200 | [diff] [blame] | 1867 | skip_sum = BTRFS_I(inode)->flags & BTRFS_INODE_NODATASUM; |
Chris Mason | cad321a | 2008-12-17 14:51:42 -0500 | [diff] [blame] | 1868 | |
Liu Bo | 83eea1f | 2012-07-10 05:28:39 -0600 | [diff] [blame] | 1869 | if (btrfs_is_free_space_inode(inode)) |
Jeff Mahoney | 0417341 | 2011-10-03 23:23:12 -0400 | [diff] [blame] | 1870 | metadata = 2; |
| 1871 | |
Christoph Hellwig | 7b6d91d | 2010-08-07 18:20:39 +0200 | [diff] [blame] | 1872 | if (!(rw & REQ_WRITE)) { |
Josef Bacik | 5fd0204 | 2012-05-02 14:00:54 -0400 | [diff] [blame] | 1873 | ret = btrfs_bio_wq_end_io(root->fs_info, bio, metadata); |
| 1874 | if (ret) |
Stefan Behrens | 6189192 | 2012-11-05 18:51:52 +0100 | [diff] [blame] | 1875 | goto out; |
Josef Bacik | 5fd0204 | 2012-05-02 14:00:54 -0400 | [diff] [blame] | 1876 | |
Chris Mason | d20f704 | 2008-12-08 16:58:54 -0500 | [diff] [blame] | 1877 | if (bio_flags & EXTENT_BIO_COMPRESSED) { |
Stefan Behrens | 6189192 | 2012-11-05 18:51:52 +0100 | [diff] [blame] | 1878 | ret = btrfs_submit_compressed_read(inode, bio, |
| 1879 | mirror_num, |
| 1880 | bio_flags); |
| 1881 | goto out; |
Tsutomu Itoh | c2db107 | 2011-03-01 06:48:31 +0000 | [diff] [blame] | 1882 | } else if (!skip_sum) { |
| 1883 | ret = btrfs_lookup_bio_sums(root, inode, bio, NULL); |
| 1884 | if (ret) |
Stefan Behrens | 6189192 | 2012-11-05 18:51:52 +0100 | [diff] [blame] | 1885 | goto out; |
Tsutomu Itoh | c2db107 | 2011-03-01 06:48:31 +0000 | [diff] [blame] | 1886 | } |
Chris Mason | 4d1b5fb | 2008-08-20 09:44:52 -0400 | [diff] [blame] | 1887 | goto mapit; |
Josef Bacik | b812ce2 | 2012-11-16 13:56:32 -0500 | [diff] [blame] | 1888 | } else if (async && !skip_sum) { |
Yan Zheng | 17d217f | 2008-12-12 10:03:38 -0500 | [diff] [blame] | 1889 | /* csum items have already been cloned */ |
| 1890 | if (root->root_key.objectid == BTRFS_DATA_RELOC_TREE_OBJECTID) |
| 1891 | goto mapit; |
Chris Mason | 19b9bdb | 2008-10-30 14:23:13 -0400 | [diff] [blame] | 1892 | /* we're doing a write, do the async checksumming */ |
Stefan Behrens | 6189192 | 2012-11-05 18:51:52 +0100 | [diff] [blame] | 1893 | ret = btrfs_wq_submit_bio(BTRFS_I(inode)->root->fs_info, |
Chris Mason | 44b8bd7 | 2008-04-16 11:14:51 -0400 | [diff] [blame] | 1894 | inode, rw, bio, mirror_num, |
Chris Mason | eaf25d9 | 2010-05-25 09:48:28 -0400 | [diff] [blame] | 1895 | bio_flags, bio_offset, |
| 1896 | __btrfs_submit_bio_start, |
Chris Mason | 4a69a41 | 2008-11-06 22:03:00 -0500 | [diff] [blame] | 1897 | __btrfs_submit_bio_done); |
Stefan Behrens | 6189192 | 2012-11-05 18:51:52 +0100 | [diff] [blame] | 1898 | goto out; |
Josef Bacik | b812ce2 | 2012-11-16 13:56:32 -0500 | [diff] [blame] | 1899 | } else if (!skip_sum) { |
| 1900 | ret = btrfs_csum_one_bio(root, inode, bio, 0, 0); |
| 1901 | if (ret) |
| 1902 | goto out; |
Chris Mason | 19b9bdb | 2008-10-30 14:23:13 -0400 | [diff] [blame] | 1903 | } |
| 1904 | |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 1905 | mapit: |
Stefan Behrens | 6189192 | 2012-11-05 18:51:52 +0100 | [diff] [blame] | 1906 | ret = btrfs_map_bio(root, rw, bio, mirror_num, 0); |
| 1907 | |
| 1908 | out: |
| 1909 | if (ret < 0) |
| 1910 | bio_endio(bio, ret); |
| 1911 | return ret; |
Chris Mason | 065631f | 2008-02-20 12:07:25 -0500 | [diff] [blame] | 1912 | } |
Chris Mason | 6885f30 | 2008-02-20 16:11:05 -0500 | [diff] [blame] | 1913 | |
Chris Mason | d352ac6 | 2008-09-29 15:18:18 -0400 | [diff] [blame] | 1914 | /* |
| 1915 | * given a list of ordered sums record them in the inode. This happens |
| 1916 | * at IO completion time based on sums calculated at bio submission time. |
| 1917 | */ |
Chris Mason | ba1da2f | 2008-07-17 12:54:15 -0400 | [diff] [blame] | 1918 | static noinline int add_pending_csums(struct btrfs_trans_handle *trans, |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 1919 | struct inode *inode, u64 file_offset, |
| 1920 | struct list_head *list) |
| 1921 | { |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 1922 | struct btrfs_ordered_sum *sum; |
| 1923 | |
Qinghuang Feng | c6e3087 | 2009-01-21 10:59:08 -0500 | [diff] [blame] | 1924 | list_for_each_entry(sum, list, list) { |
Miao Xie | 39847c4 | 2013-03-28 08:08:20 +0000 | [diff] [blame] | 1925 | trans->adding_csums = 1; |
Chris Mason | d20f704 | 2008-12-08 16:58:54 -0500 | [diff] [blame] | 1926 | btrfs_csum_file_blocks(trans, |
| 1927 | BTRFS_I(inode)->root->fs_info->csum_root, sum); |
Miao Xie | 39847c4 | 2013-03-28 08:08:20 +0000 | [diff] [blame] | 1928 | trans->adding_csums = 0; |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 1929 | } |
| 1930 | return 0; |
| 1931 | } |
| 1932 | |
Josef Bacik | 2ac55d4 | 2010-02-03 19:33:23 +0000 | [diff] [blame] | 1933 | int btrfs_set_extent_delalloc(struct inode *inode, u64 start, u64 end, |
| 1934 | struct extent_state **cached_state) |
Chris Mason | ea8c281 | 2008-08-04 23:17:27 -0400 | [diff] [blame] | 1935 | { |
Julia Lawall | 6c1500f | 2012-11-03 20:30:18 +0000 | [diff] [blame] | 1936 | WARN_ON((end & (PAGE_CACHE_SIZE - 1)) == 0); |
Chris Mason | ea8c281 | 2008-08-04 23:17:27 -0400 | [diff] [blame] | 1937 | return set_extent_delalloc(&BTRFS_I(inode)->io_tree, start, end, |
Josef Bacik | 2ac55d4 | 2010-02-03 19:33:23 +0000 | [diff] [blame] | 1938 | cached_state, GFP_NOFS); |
Chris Mason | ea8c281 | 2008-08-04 23:17:27 -0400 | [diff] [blame] | 1939 | } |
| 1940 | |
Chris Mason | d352ac6 | 2008-09-29 15:18:18 -0400 | [diff] [blame] | 1941 | /* see btrfs_writepage_start_hook for details on why this is required */ |
Chris Mason | 247e743 | 2008-07-17 12:53:51 -0400 | [diff] [blame] | 1942 | struct btrfs_writepage_fixup { |
| 1943 | struct page *page; |
| 1944 | struct btrfs_work work; |
| 1945 | }; |
| 1946 | |
Christoph Hellwig | b295086 | 2008-12-02 09:54:17 -0500 | [diff] [blame] | 1947 | static void btrfs_writepage_fixup_worker(struct btrfs_work *work) |
Chris Mason | 247e743 | 2008-07-17 12:53:51 -0400 | [diff] [blame] | 1948 | { |
| 1949 | struct btrfs_writepage_fixup *fixup; |
| 1950 | struct btrfs_ordered_extent *ordered; |
Josef Bacik | 2ac55d4 | 2010-02-03 19:33:23 +0000 | [diff] [blame] | 1951 | struct extent_state *cached_state = NULL; |
Chris Mason | 247e743 | 2008-07-17 12:53:51 -0400 | [diff] [blame] | 1952 | struct page *page; |
| 1953 | struct inode *inode; |
| 1954 | u64 page_start; |
| 1955 | u64 page_end; |
Jeff Mahoney | 87826df | 2012-02-15 16:23:57 +0100 | [diff] [blame] | 1956 | int ret; |
Chris Mason | 247e743 | 2008-07-17 12:53:51 -0400 | [diff] [blame] | 1957 | |
| 1958 | fixup = container_of(work, struct btrfs_writepage_fixup, work); |
| 1959 | page = fixup->page; |
Chris Mason | 4a09675 | 2008-07-21 10:29:44 -0400 | [diff] [blame] | 1960 | again: |
Chris Mason | 247e743 | 2008-07-17 12:53:51 -0400 | [diff] [blame] | 1961 | lock_page(page); |
| 1962 | if (!page->mapping || !PageDirty(page) || !PageChecked(page)) { |
| 1963 | ClearPageChecked(page); |
| 1964 | goto out_page; |
| 1965 | } |
| 1966 | |
| 1967 | inode = page->mapping->host; |
| 1968 | page_start = page_offset(page); |
| 1969 | page_end = page_offset(page) + PAGE_CACHE_SIZE - 1; |
| 1970 | |
Josef Bacik | 2ac55d4 | 2010-02-03 19:33:23 +0000 | [diff] [blame] | 1971 | 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] | 1972 | &cached_state); |
Chris Mason | 4a09675 | 2008-07-21 10:29:44 -0400 | [diff] [blame] | 1973 | |
| 1974 | /* already ordered? We're done */ |
Chris Mason | 8b62b72 | 2009-09-02 16:53:46 -0400 | [diff] [blame] | 1975 | if (PagePrivate2(page)) |
Chris Mason | 247e743 | 2008-07-17 12:53:51 -0400 | [diff] [blame] | 1976 | goto out; |
Chris Mason | 4a09675 | 2008-07-21 10:29:44 -0400 | [diff] [blame] | 1977 | |
| 1978 | ordered = btrfs_lookup_ordered_extent(inode, page_start); |
| 1979 | if (ordered) { |
Josef Bacik | 2ac55d4 | 2010-02-03 19:33:23 +0000 | [diff] [blame] | 1980 | unlock_extent_cached(&BTRFS_I(inode)->io_tree, page_start, |
| 1981 | page_end, &cached_state, GFP_NOFS); |
Chris Mason | 4a09675 | 2008-07-21 10:29:44 -0400 | [diff] [blame] | 1982 | unlock_page(page); |
| 1983 | btrfs_start_ordered_extent(inode, ordered, 1); |
Jeff Mahoney | 87826df | 2012-02-15 16:23:57 +0100 | [diff] [blame] | 1984 | btrfs_put_ordered_extent(ordered); |
Chris Mason | 4a09675 | 2008-07-21 10:29:44 -0400 | [diff] [blame] | 1985 | goto again; |
| 1986 | } |
Chris Mason | 247e743 | 2008-07-17 12:53:51 -0400 | [diff] [blame] | 1987 | |
Jeff Mahoney | 87826df | 2012-02-15 16:23:57 +0100 | [diff] [blame] | 1988 | ret = btrfs_delalloc_reserve_space(inode, PAGE_CACHE_SIZE); |
| 1989 | if (ret) { |
| 1990 | mapping_set_error(page->mapping, ret); |
| 1991 | end_extent_writepage(page, ret, page_start, page_end); |
| 1992 | ClearPageChecked(page); |
| 1993 | goto out; |
| 1994 | } |
| 1995 | |
Josef Bacik | 2ac55d4 | 2010-02-03 19:33:23 +0000 | [diff] [blame] | 1996 | btrfs_set_extent_delalloc(inode, page_start, page_end, &cached_state); |
Chris Mason | 247e743 | 2008-07-17 12:53:51 -0400 | [diff] [blame] | 1997 | ClearPageChecked(page); |
Jeff Mahoney | 87826df | 2012-02-15 16:23:57 +0100 | [diff] [blame] | 1998 | set_page_dirty(page); |
Chris Mason | 247e743 | 2008-07-17 12:53:51 -0400 | [diff] [blame] | 1999 | out: |
Josef Bacik | 2ac55d4 | 2010-02-03 19:33:23 +0000 | [diff] [blame] | 2000 | unlock_extent_cached(&BTRFS_I(inode)->io_tree, page_start, page_end, |
| 2001 | &cached_state, GFP_NOFS); |
Chris Mason | 247e743 | 2008-07-17 12:53:51 -0400 | [diff] [blame] | 2002 | out_page: |
| 2003 | unlock_page(page); |
| 2004 | page_cache_release(page); |
Miao Xie | b897abe | 2011-01-26 16:19:22 +0800 | [diff] [blame] | 2005 | kfree(fixup); |
Chris Mason | 247e743 | 2008-07-17 12:53:51 -0400 | [diff] [blame] | 2006 | } |
| 2007 | |
| 2008 | /* |
| 2009 | * There are a few paths in the higher layers of the kernel that directly |
| 2010 | * set the page dirty bit without asking the filesystem if it is a |
| 2011 | * good idea. This causes problems because we want to make sure COW |
| 2012 | * properly happens and the data=ordered rules are followed. |
| 2013 | * |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 2014 | * 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] | 2015 | * hasn't been properly setup for IO. We kick off an async process |
| 2016 | * to fix it up. The async helper will wait for ordered extents, set |
| 2017 | * the delalloc bit and make it safe to write the page. |
| 2018 | */ |
Christoph Hellwig | b295086 | 2008-12-02 09:54:17 -0500 | [diff] [blame] | 2019 | 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] | 2020 | { |
| 2021 | struct inode *inode = page->mapping->host; |
| 2022 | struct btrfs_writepage_fixup *fixup; |
| 2023 | struct btrfs_root *root = BTRFS_I(inode)->root; |
Chris Mason | 247e743 | 2008-07-17 12:53:51 -0400 | [diff] [blame] | 2024 | |
Chris Mason | 8b62b72 | 2009-09-02 16:53:46 -0400 | [diff] [blame] | 2025 | /* this page is properly in the ordered list */ |
| 2026 | if (TestClearPagePrivate2(page)) |
Chris Mason | 247e743 | 2008-07-17 12:53:51 -0400 | [diff] [blame] | 2027 | return 0; |
| 2028 | |
| 2029 | if (PageChecked(page)) |
| 2030 | return -EAGAIN; |
| 2031 | |
| 2032 | fixup = kzalloc(sizeof(*fixup), GFP_NOFS); |
| 2033 | if (!fixup) |
| 2034 | return -EAGAIN; |
Chris Mason | f421950 | 2008-07-22 11:18:09 -0400 | [diff] [blame] | 2035 | |
Chris Mason | 247e743 | 2008-07-17 12:53:51 -0400 | [diff] [blame] | 2036 | SetPageChecked(page); |
| 2037 | page_cache_get(page); |
Liu Bo | 9e0af23 | 2014-08-15 23:36:53 +0800 | [diff] [blame] | 2038 | btrfs_init_work(&fixup->work, btrfs_fixup_helper, |
| 2039 | btrfs_writepage_fixup_worker, NULL, NULL); |
Chris Mason | 247e743 | 2008-07-17 12:53:51 -0400 | [diff] [blame] | 2040 | fixup->page = page; |
Qu Wenruo | dc6e320 | 2014-02-28 10:46:14 +0800 | [diff] [blame] | 2041 | btrfs_queue_work(root->fs_info->fixup_workers, &fixup->work); |
Jeff Mahoney | 87826df | 2012-02-15 16:23:57 +0100 | [diff] [blame] | 2042 | return -EBUSY; |
Chris Mason | 247e743 | 2008-07-17 12:53:51 -0400 | [diff] [blame] | 2043 | } |
| 2044 | |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 2045 | static int insert_reserved_file_extent(struct btrfs_trans_handle *trans, |
| 2046 | struct inode *inode, u64 file_pos, |
| 2047 | u64 disk_bytenr, u64 disk_num_bytes, |
| 2048 | u64 num_bytes, u64 ram_bytes, |
| 2049 | u8 compression, u8 encryption, |
| 2050 | u16 other_encoding, int extent_type) |
| 2051 | { |
| 2052 | struct btrfs_root *root = BTRFS_I(inode)->root; |
| 2053 | struct btrfs_file_extent_item *fi; |
| 2054 | struct btrfs_path *path; |
| 2055 | struct extent_buffer *leaf; |
| 2056 | struct btrfs_key ins; |
Filipe David Borba Manana | 1acae57 | 2014-01-07 11:42:27 +0000 | [diff] [blame] | 2057 | int extent_inserted = 0; |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 2058 | int ret; |
| 2059 | |
| 2060 | path = btrfs_alloc_path(); |
Mark Fasheh | d8926bb | 2011-07-13 10:38:47 -0700 | [diff] [blame] | 2061 | if (!path) |
| 2062 | return -ENOMEM; |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 2063 | |
Chris Mason | a1ed835 | 2009-09-11 12:27:37 -0400 | [diff] [blame] | 2064 | /* |
| 2065 | * we may be replacing one extent in the tree with another. |
| 2066 | * The new extent is pinned in the extent map, and we don't want |
| 2067 | * to drop it from the cache until it is completely in the btree. |
| 2068 | * |
| 2069 | * So, tell btrfs_drop_extents to leave this extent in the cache. |
| 2070 | * the caller is expected to unpin it and allow it to be merged |
| 2071 | * with the others. |
| 2072 | */ |
Filipe David Borba Manana | 1acae57 | 2014-01-07 11:42:27 +0000 | [diff] [blame] | 2073 | ret = __btrfs_drop_extents(trans, root, inode, path, file_pos, |
| 2074 | file_pos + num_bytes, NULL, 0, |
| 2075 | 1, sizeof(*fi), &extent_inserted); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 2076 | if (ret) |
| 2077 | goto out; |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 2078 | |
Filipe David Borba Manana | 1acae57 | 2014-01-07 11:42:27 +0000 | [diff] [blame] | 2079 | if (!extent_inserted) { |
| 2080 | ins.objectid = btrfs_ino(inode); |
| 2081 | ins.offset = file_pos; |
| 2082 | ins.type = BTRFS_EXTENT_DATA_KEY; |
| 2083 | |
| 2084 | path->leave_spinning = 1; |
| 2085 | ret = btrfs_insert_empty_item(trans, root, path, &ins, |
| 2086 | sizeof(*fi)); |
| 2087 | if (ret) |
| 2088 | goto out; |
| 2089 | } |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 2090 | leaf = path->nodes[0]; |
| 2091 | fi = btrfs_item_ptr(leaf, path->slots[0], |
| 2092 | struct btrfs_file_extent_item); |
| 2093 | btrfs_set_file_extent_generation(leaf, fi, trans->transid); |
| 2094 | btrfs_set_file_extent_type(leaf, fi, extent_type); |
| 2095 | btrfs_set_file_extent_disk_bytenr(leaf, fi, disk_bytenr); |
| 2096 | btrfs_set_file_extent_disk_num_bytes(leaf, fi, disk_num_bytes); |
| 2097 | btrfs_set_file_extent_offset(leaf, fi, 0); |
| 2098 | btrfs_set_file_extent_num_bytes(leaf, fi, num_bytes); |
| 2099 | btrfs_set_file_extent_ram_bytes(leaf, fi, ram_bytes); |
| 2100 | btrfs_set_file_extent_compression(leaf, fi, compression); |
| 2101 | btrfs_set_file_extent_encryption(leaf, fi, encryption); |
| 2102 | btrfs_set_file_extent_other_encoding(leaf, fi, other_encoding); |
Chris Mason | b947343 | 2009-03-13 11:00:37 -0400 | [diff] [blame] | 2103 | |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 2104 | btrfs_mark_buffer_dirty(leaf); |
Josef Bacik | ce19533 | 2012-09-25 15:26:16 -0400 | [diff] [blame] | 2105 | btrfs_release_path(path); |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 2106 | |
| 2107 | inode_add_bytes(inode, num_bytes); |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 2108 | |
| 2109 | ins.objectid = disk_bytenr; |
| 2110 | ins.offset = disk_num_bytes; |
| 2111 | ins.type = BTRFS_EXTENT_ITEM_KEY; |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 2112 | ret = btrfs_alloc_reserved_file_extent(trans, root, |
| 2113 | root->root_key.objectid, |
Li Zefan | 33345d01 | 2011-04-20 10:31:50 +0800 | [diff] [blame] | 2114 | btrfs_ino(inode), file_pos, &ins); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 2115 | out: |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 2116 | btrfs_free_path(path); |
Chris Mason | b947343 | 2009-03-13 11:00:37 -0400 | [diff] [blame] | 2117 | |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 2118 | return ret; |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 2119 | } |
| 2120 | |
Liu Bo | 38c227d | 2013-01-29 03:18:40 +0000 | [diff] [blame] | 2121 | /* snapshot-aware defrag */ |
| 2122 | struct sa_defrag_extent_backref { |
| 2123 | struct rb_node node; |
| 2124 | struct old_sa_defrag_extent *old; |
| 2125 | u64 root_id; |
| 2126 | u64 inum; |
| 2127 | u64 file_pos; |
| 2128 | u64 extent_offset; |
| 2129 | u64 num_bytes; |
| 2130 | u64 generation; |
| 2131 | }; |
| 2132 | |
| 2133 | struct old_sa_defrag_extent { |
| 2134 | struct list_head list; |
| 2135 | struct new_sa_defrag_extent *new; |
| 2136 | |
| 2137 | u64 extent_offset; |
| 2138 | u64 bytenr; |
| 2139 | u64 offset; |
| 2140 | u64 len; |
| 2141 | int count; |
| 2142 | }; |
| 2143 | |
| 2144 | struct new_sa_defrag_extent { |
| 2145 | struct rb_root root; |
| 2146 | struct list_head head; |
| 2147 | struct btrfs_path *path; |
| 2148 | struct inode *inode; |
| 2149 | u64 file_pos; |
| 2150 | u64 len; |
| 2151 | u64 bytenr; |
| 2152 | u64 disk_len; |
| 2153 | u8 compress_type; |
| 2154 | }; |
| 2155 | |
| 2156 | static int backref_comp(struct sa_defrag_extent_backref *b1, |
| 2157 | struct sa_defrag_extent_backref *b2) |
| 2158 | { |
| 2159 | if (b1->root_id < b2->root_id) |
| 2160 | return -1; |
| 2161 | else if (b1->root_id > b2->root_id) |
| 2162 | return 1; |
| 2163 | |
| 2164 | if (b1->inum < b2->inum) |
| 2165 | return -1; |
| 2166 | else if (b1->inum > b2->inum) |
| 2167 | return 1; |
| 2168 | |
| 2169 | if (b1->file_pos < b2->file_pos) |
| 2170 | return -1; |
| 2171 | else if (b1->file_pos > b2->file_pos) |
| 2172 | return 1; |
| 2173 | |
| 2174 | /* |
| 2175 | * [------------------------------] ===> (a range of space) |
| 2176 | * |<--->| |<---->| =============> (fs/file tree A) |
| 2177 | * |<---------------------------->| ===> (fs/file tree B) |
| 2178 | * |
| 2179 | * A range of space can refer to two file extents in one tree while |
| 2180 | * refer to only one file extent in another tree. |
| 2181 | * |
| 2182 | * So we may process a disk offset more than one time(two extents in A) |
| 2183 | * and locate at the same extent(one extent in B), then insert two same |
| 2184 | * backrefs(both refer to the extent in B). |
| 2185 | */ |
| 2186 | return 0; |
| 2187 | } |
| 2188 | |
| 2189 | static void backref_insert(struct rb_root *root, |
| 2190 | struct sa_defrag_extent_backref *backref) |
| 2191 | { |
| 2192 | struct rb_node **p = &root->rb_node; |
| 2193 | struct rb_node *parent = NULL; |
| 2194 | struct sa_defrag_extent_backref *entry; |
| 2195 | int ret; |
| 2196 | |
| 2197 | while (*p) { |
| 2198 | parent = *p; |
| 2199 | entry = rb_entry(parent, struct sa_defrag_extent_backref, node); |
| 2200 | |
| 2201 | ret = backref_comp(backref, entry); |
| 2202 | if (ret < 0) |
| 2203 | p = &(*p)->rb_left; |
| 2204 | else |
| 2205 | p = &(*p)->rb_right; |
| 2206 | } |
| 2207 | |
| 2208 | rb_link_node(&backref->node, parent, p); |
| 2209 | rb_insert_color(&backref->node, root); |
| 2210 | } |
| 2211 | |
| 2212 | /* |
| 2213 | * Note the backref might has changed, and in this case we just return 0. |
| 2214 | */ |
| 2215 | static noinline int record_one_backref(u64 inum, u64 offset, u64 root_id, |
| 2216 | void *ctx) |
| 2217 | { |
| 2218 | struct btrfs_file_extent_item *extent; |
| 2219 | struct btrfs_fs_info *fs_info; |
| 2220 | struct old_sa_defrag_extent *old = ctx; |
| 2221 | struct new_sa_defrag_extent *new = old->new; |
| 2222 | struct btrfs_path *path = new->path; |
| 2223 | struct btrfs_key key; |
| 2224 | struct btrfs_root *root; |
| 2225 | struct sa_defrag_extent_backref *backref; |
| 2226 | struct extent_buffer *leaf; |
| 2227 | struct inode *inode = new->inode; |
| 2228 | int slot; |
| 2229 | int ret; |
| 2230 | u64 extent_offset; |
| 2231 | u64 num_bytes; |
| 2232 | |
| 2233 | if (BTRFS_I(inode)->root->root_key.objectid == root_id && |
| 2234 | inum == btrfs_ino(inode)) |
| 2235 | return 0; |
| 2236 | |
| 2237 | key.objectid = root_id; |
| 2238 | key.type = BTRFS_ROOT_ITEM_KEY; |
| 2239 | key.offset = (u64)-1; |
| 2240 | |
| 2241 | fs_info = BTRFS_I(inode)->root->fs_info; |
| 2242 | root = btrfs_read_fs_root_no_name(fs_info, &key); |
| 2243 | if (IS_ERR(root)) { |
| 2244 | if (PTR_ERR(root) == -ENOENT) |
| 2245 | return 0; |
| 2246 | WARN_ON(1); |
| 2247 | pr_debug("inum=%llu, offset=%llu, root_id=%llu\n", |
| 2248 | inum, offset, root_id); |
| 2249 | return PTR_ERR(root); |
| 2250 | } |
| 2251 | |
| 2252 | key.objectid = inum; |
| 2253 | key.type = BTRFS_EXTENT_DATA_KEY; |
| 2254 | if (offset > (u64)-1 << 32) |
| 2255 | key.offset = 0; |
| 2256 | else |
| 2257 | key.offset = offset; |
| 2258 | |
| 2259 | ret = btrfs_search_slot(NULL, root, &key, path, 0, 0); |
Dulshani Gunawardhana | fae7f21 | 2013-10-31 10:30:08 +0530 | [diff] [blame] | 2260 | if (WARN_ON(ret < 0)) |
Liu Bo | 38c227d | 2013-01-29 03:18:40 +0000 | [diff] [blame] | 2261 | return ret; |
Josef Bacik | 50f1319 | 2013-07-22 12:50:37 -0400 | [diff] [blame] | 2262 | ret = 0; |
Liu Bo | 38c227d | 2013-01-29 03:18:40 +0000 | [diff] [blame] | 2263 | |
| 2264 | while (1) { |
| 2265 | cond_resched(); |
| 2266 | |
| 2267 | leaf = path->nodes[0]; |
| 2268 | slot = path->slots[0]; |
| 2269 | |
| 2270 | if (slot >= btrfs_header_nritems(leaf)) { |
| 2271 | ret = btrfs_next_leaf(root, path); |
| 2272 | if (ret < 0) { |
| 2273 | goto out; |
| 2274 | } else if (ret > 0) { |
| 2275 | ret = 0; |
| 2276 | goto out; |
| 2277 | } |
| 2278 | continue; |
| 2279 | } |
| 2280 | |
| 2281 | path->slots[0]++; |
| 2282 | |
| 2283 | btrfs_item_key_to_cpu(leaf, &key, slot); |
| 2284 | |
| 2285 | if (key.objectid > inum) |
| 2286 | goto out; |
| 2287 | |
| 2288 | if (key.objectid < inum || key.type != BTRFS_EXTENT_DATA_KEY) |
| 2289 | continue; |
| 2290 | |
| 2291 | extent = btrfs_item_ptr(leaf, slot, |
| 2292 | struct btrfs_file_extent_item); |
| 2293 | |
| 2294 | if (btrfs_file_extent_disk_bytenr(leaf, extent) != old->bytenr) |
| 2295 | continue; |
| 2296 | |
Liu Bo | e68afa4 | 2013-07-01 22:13:26 +0800 | [diff] [blame] | 2297 | /* |
| 2298 | * 'offset' refers to the exact key.offset, |
| 2299 | * NOT the 'offset' field in btrfs_extent_data_ref, ie. |
| 2300 | * (key.offset - extent_offset). |
| 2301 | */ |
| 2302 | if (key.offset != offset) |
Liu Bo | 38c227d | 2013-01-29 03:18:40 +0000 | [diff] [blame] | 2303 | continue; |
| 2304 | |
Liu Bo | e68afa4 | 2013-07-01 22:13:26 +0800 | [diff] [blame] | 2305 | extent_offset = btrfs_file_extent_offset(leaf, extent); |
Liu Bo | 38c227d | 2013-01-29 03:18:40 +0000 | [diff] [blame] | 2306 | num_bytes = btrfs_file_extent_num_bytes(leaf, extent); |
Liu Bo | e68afa4 | 2013-07-01 22:13:26 +0800 | [diff] [blame] | 2307 | |
Liu Bo | 38c227d | 2013-01-29 03:18:40 +0000 | [diff] [blame] | 2308 | if (extent_offset >= old->extent_offset + old->offset + |
| 2309 | old->len || extent_offset + num_bytes <= |
| 2310 | old->extent_offset + old->offset) |
| 2311 | continue; |
Liu Bo | 38c227d | 2013-01-29 03:18:40 +0000 | [diff] [blame] | 2312 | break; |
| 2313 | } |
| 2314 | |
| 2315 | backref = kmalloc(sizeof(*backref), GFP_NOFS); |
| 2316 | if (!backref) { |
| 2317 | ret = -ENOENT; |
| 2318 | goto out; |
| 2319 | } |
| 2320 | |
| 2321 | backref->root_id = root_id; |
| 2322 | backref->inum = inum; |
Liu Bo | e68afa4 | 2013-07-01 22:13:26 +0800 | [diff] [blame] | 2323 | backref->file_pos = offset; |
Liu Bo | 38c227d | 2013-01-29 03:18:40 +0000 | [diff] [blame] | 2324 | backref->num_bytes = num_bytes; |
| 2325 | backref->extent_offset = extent_offset; |
| 2326 | backref->generation = btrfs_file_extent_generation(leaf, extent); |
| 2327 | backref->old = old; |
| 2328 | backref_insert(&new->root, backref); |
| 2329 | old->count++; |
| 2330 | out: |
| 2331 | btrfs_release_path(path); |
| 2332 | WARN_ON(ret); |
| 2333 | return ret; |
| 2334 | } |
| 2335 | |
| 2336 | static noinline bool record_extent_backrefs(struct btrfs_path *path, |
| 2337 | struct new_sa_defrag_extent *new) |
| 2338 | { |
| 2339 | struct btrfs_fs_info *fs_info = BTRFS_I(new->inode)->root->fs_info; |
| 2340 | struct old_sa_defrag_extent *old, *tmp; |
| 2341 | int ret; |
| 2342 | |
| 2343 | new->path = path; |
| 2344 | |
| 2345 | list_for_each_entry_safe(old, tmp, &new->head, list) { |
Liu Bo | e68afa4 | 2013-07-01 22:13:26 +0800 | [diff] [blame] | 2346 | ret = iterate_inodes_from_logical(old->bytenr + |
| 2347 | old->extent_offset, fs_info, |
Liu Bo | 38c227d | 2013-01-29 03:18:40 +0000 | [diff] [blame] | 2348 | path, record_one_backref, |
| 2349 | old); |
Josef Bacik | 4724b10 | 2013-11-05 11:11:40 -0500 | [diff] [blame] | 2350 | if (ret < 0 && ret != -ENOENT) |
| 2351 | return false; |
Liu Bo | 38c227d | 2013-01-29 03:18:40 +0000 | [diff] [blame] | 2352 | |
| 2353 | /* no backref to be processed for this extent */ |
| 2354 | if (!old->count) { |
| 2355 | list_del(&old->list); |
| 2356 | kfree(old); |
| 2357 | } |
| 2358 | } |
| 2359 | |
| 2360 | if (list_empty(&new->head)) |
| 2361 | return false; |
| 2362 | |
| 2363 | return true; |
| 2364 | } |
| 2365 | |
| 2366 | static int relink_is_mergable(struct extent_buffer *leaf, |
| 2367 | struct btrfs_file_extent_item *fi, |
Liu Bo | 116e002 | 2013-08-02 16:30:40 +0800 | [diff] [blame] | 2368 | struct new_sa_defrag_extent *new) |
Liu Bo | 38c227d | 2013-01-29 03:18:40 +0000 | [diff] [blame] | 2369 | { |
Liu Bo | 116e002 | 2013-08-02 16:30:40 +0800 | [diff] [blame] | 2370 | if (btrfs_file_extent_disk_bytenr(leaf, fi) != new->bytenr) |
Liu Bo | 38c227d | 2013-01-29 03:18:40 +0000 | [diff] [blame] | 2371 | return 0; |
| 2372 | |
| 2373 | if (btrfs_file_extent_type(leaf, fi) != BTRFS_FILE_EXTENT_REG) |
| 2374 | return 0; |
| 2375 | |
Liu Bo | 116e002 | 2013-08-02 16:30:40 +0800 | [diff] [blame] | 2376 | if (btrfs_file_extent_compression(leaf, fi) != new->compress_type) |
| 2377 | return 0; |
| 2378 | |
| 2379 | if (btrfs_file_extent_encryption(leaf, fi) || |
Liu Bo | 38c227d | 2013-01-29 03:18:40 +0000 | [diff] [blame] | 2380 | btrfs_file_extent_other_encoding(leaf, fi)) |
| 2381 | return 0; |
| 2382 | |
| 2383 | return 1; |
| 2384 | } |
| 2385 | |
| 2386 | /* |
| 2387 | * Note the backref might has changed, and in this case we just return 0. |
| 2388 | */ |
| 2389 | static noinline int relink_extent_backref(struct btrfs_path *path, |
| 2390 | struct sa_defrag_extent_backref *prev, |
| 2391 | struct sa_defrag_extent_backref *backref) |
| 2392 | { |
| 2393 | struct btrfs_file_extent_item *extent; |
| 2394 | struct btrfs_file_extent_item *item; |
| 2395 | struct btrfs_ordered_extent *ordered; |
| 2396 | struct btrfs_trans_handle *trans; |
| 2397 | struct btrfs_fs_info *fs_info; |
| 2398 | struct btrfs_root *root; |
| 2399 | struct btrfs_key key; |
| 2400 | struct extent_buffer *leaf; |
| 2401 | struct old_sa_defrag_extent *old = backref->old; |
| 2402 | struct new_sa_defrag_extent *new = old->new; |
| 2403 | struct inode *src_inode = new->inode; |
| 2404 | struct inode *inode; |
| 2405 | struct extent_state *cached = NULL; |
| 2406 | int ret = 0; |
| 2407 | u64 start; |
| 2408 | u64 len; |
| 2409 | u64 lock_start; |
| 2410 | u64 lock_end; |
| 2411 | bool merge = false; |
| 2412 | int index; |
| 2413 | |
| 2414 | if (prev && prev->root_id == backref->root_id && |
| 2415 | prev->inum == backref->inum && |
| 2416 | prev->file_pos + prev->num_bytes == backref->file_pos) |
| 2417 | merge = true; |
| 2418 | |
| 2419 | /* step 1: get root */ |
| 2420 | key.objectid = backref->root_id; |
| 2421 | key.type = BTRFS_ROOT_ITEM_KEY; |
| 2422 | key.offset = (u64)-1; |
| 2423 | |
| 2424 | fs_info = BTRFS_I(src_inode)->root->fs_info; |
| 2425 | index = srcu_read_lock(&fs_info->subvol_srcu); |
| 2426 | |
| 2427 | root = btrfs_read_fs_root_no_name(fs_info, &key); |
| 2428 | if (IS_ERR(root)) { |
| 2429 | srcu_read_unlock(&fs_info->subvol_srcu, index); |
| 2430 | if (PTR_ERR(root) == -ENOENT) |
| 2431 | return 0; |
| 2432 | return PTR_ERR(root); |
| 2433 | } |
Liu Bo | 38c227d | 2013-01-29 03:18:40 +0000 | [diff] [blame] | 2434 | |
Wang Shilong | bcbba5e | 2014-02-08 23:46:35 +0800 | [diff] [blame] | 2435 | if (btrfs_root_readonly(root)) { |
| 2436 | srcu_read_unlock(&fs_info->subvol_srcu, index); |
| 2437 | return 0; |
| 2438 | } |
| 2439 | |
Liu Bo | 38c227d | 2013-01-29 03:18:40 +0000 | [diff] [blame] | 2440 | /* step 2: get inode */ |
| 2441 | key.objectid = backref->inum; |
| 2442 | key.type = BTRFS_INODE_ITEM_KEY; |
| 2443 | key.offset = 0; |
| 2444 | |
| 2445 | inode = btrfs_iget(fs_info->sb, &key, root, NULL); |
| 2446 | if (IS_ERR(inode)) { |
| 2447 | srcu_read_unlock(&fs_info->subvol_srcu, index); |
| 2448 | return 0; |
| 2449 | } |
| 2450 | |
| 2451 | srcu_read_unlock(&fs_info->subvol_srcu, index); |
| 2452 | |
| 2453 | /* step 3: relink backref */ |
| 2454 | lock_start = backref->file_pos; |
| 2455 | lock_end = backref->file_pos + backref->num_bytes - 1; |
| 2456 | lock_extent_bits(&BTRFS_I(inode)->io_tree, lock_start, lock_end, |
| 2457 | 0, &cached); |
| 2458 | |
| 2459 | ordered = btrfs_lookup_first_ordered_extent(inode, lock_end); |
| 2460 | if (ordered) { |
| 2461 | btrfs_put_ordered_extent(ordered); |
| 2462 | goto out_unlock; |
| 2463 | } |
| 2464 | |
| 2465 | trans = btrfs_join_transaction(root); |
| 2466 | if (IS_ERR(trans)) { |
| 2467 | ret = PTR_ERR(trans); |
| 2468 | goto out_unlock; |
| 2469 | } |
| 2470 | |
| 2471 | key.objectid = backref->inum; |
| 2472 | key.type = BTRFS_EXTENT_DATA_KEY; |
| 2473 | key.offset = backref->file_pos; |
| 2474 | |
| 2475 | ret = btrfs_search_slot(NULL, root, &key, path, 0, 0); |
| 2476 | if (ret < 0) { |
| 2477 | goto out_free_path; |
| 2478 | } else if (ret > 0) { |
| 2479 | ret = 0; |
| 2480 | goto out_free_path; |
| 2481 | } |
| 2482 | |
| 2483 | extent = btrfs_item_ptr(path->nodes[0], path->slots[0], |
| 2484 | struct btrfs_file_extent_item); |
| 2485 | |
| 2486 | if (btrfs_file_extent_generation(path->nodes[0], extent) != |
| 2487 | backref->generation) |
| 2488 | goto out_free_path; |
| 2489 | |
| 2490 | btrfs_release_path(path); |
| 2491 | |
| 2492 | start = backref->file_pos; |
| 2493 | if (backref->extent_offset < old->extent_offset + old->offset) |
| 2494 | start += old->extent_offset + old->offset - |
| 2495 | backref->extent_offset; |
| 2496 | |
| 2497 | len = min(backref->extent_offset + backref->num_bytes, |
| 2498 | old->extent_offset + old->offset + old->len); |
| 2499 | len -= max(backref->extent_offset, old->extent_offset + old->offset); |
| 2500 | |
| 2501 | ret = btrfs_drop_extents(trans, root, inode, start, |
| 2502 | start + len, 1); |
| 2503 | if (ret) |
| 2504 | goto out_free_path; |
| 2505 | again: |
| 2506 | key.objectid = btrfs_ino(inode); |
| 2507 | key.type = BTRFS_EXTENT_DATA_KEY; |
| 2508 | key.offset = start; |
| 2509 | |
Liu Bo | a09a0a7 | 2013-03-11 09:20:58 +0000 | [diff] [blame] | 2510 | path->leave_spinning = 1; |
Liu Bo | 38c227d | 2013-01-29 03:18:40 +0000 | [diff] [blame] | 2511 | if (merge) { |
| 2512 | struct btrfs_file_extent_item *fi; |
| 2513 | u64 extent_len; |
| 2514 | struct btrfs_key found_key; |
| 2515 | |
Gui Hecheng | 3c9665d | 2014-01-23 13:41:09 +0800 | [diff] [blame] | 2516 | ret = btrfs_search_slot(trans, root, &key, path, 0, 1); |
Liu Bo | 38c227d | 2013-01-29 03:18:40 +0000 | [diff] [blame] | 2517 | if (ret < 0) |
| 2518 | goto out_free_path; |
| 2519 | |
| 2520 | path->slots[0]--; |
| 2521 | leaf = path->nodes[0]; |
| 2522 | btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]); |
| 2523 | |
| 2524 | fi = btrfs_item_ptr(leaf, path->slots[0], |
| 2525 | struct btrfs_file_extent_item); |
| 2526 | extent_len = btrfs_file_extent_num_bytes(leaf, fi); |
| 2527 | |
Liu Bo | 116e002 | 2013-08-02 16:30:40 +0800 | [diff] [blame] | 2528 | if (extent_len + found_key.offset == start && |
| 2529 | relink_is_mergable(leaf, fi, new)) { |
Liu Bo | 38c227d | 2013-01-29 03:18:40 +0000 | [diff] [blame] | 2530 | btrfs_set_file_extent_num_bytes(leaf, fi, |
| 2531 | extent_len + len); |
| 2532 | btrfs_mark_buffer_dirty(leaf); |
| 2533 | inode_add_bytes(inode, len); |
| 2534 | |
| 2535 | ret = 1; |
| 2536 | goto out_free_path; |
| 2537 | } else { |
| 2538 | merge = false; |
| 2539 | btrfs_release_path(path); |
| 2540 | goto again; |
| 2541 | } |
| 2542 | } |
| 2543 | |
| 2544 | ret = btrfs_insert_empty_item(trans, root, path, &key, |
| 2545 | sizeof(*extent)); |
| 2546 | if (ret) { |
| 2547 | btrfs_abort_transaction(trans, root, ret); |
| 2548 | goto out_free_path; |
| 2549 | } |
| 2550 | |
| 2551 | leaf = path->nodes[0]; |
| 2552 | item = btrfs_item_ptr(leaf, path->slots[0], |
| 2553 | struct btrfs_file_extent_item); |
| 2554 | btrfs_set_file_extent_disk_bytenr(leaf, item, new->bytenr); |
| 2555 | btrfs_set_file_extent_disk_num_bytes(leaf, item, new->disk_len); |
| 2556 | btrfs_set_file_extent_offset(leaf, item, start - new->file_pos); |
| 2557 | btrfs_set_file_extent_num_bytes(leaf, item, len); |
| 2558 | btrfs_set_file_extent_ram_bytes(leaf, item, new->len); |
| 2559 | btrfs_set_file_extent_generation(leaf, item, trans->transid); |
| 2560 | btrfs_set_file_extent_type(leaf, item, BTRFS_FILE_EXTENT_REG); |
| 2561 | btrfs_set_file_extent_compression(leaf, item, new->compress_type); |
| 2562 | btrfs_set_file_extent_encryption(leaf, item, 0); |
| 2563 | btrfs_set_file_extent_other_encoding(leaf, item, 0); |
| 2564 | |
| 2565 | btrfs_mark_buffer_dirty(leaf); |
| 2566 | inode_add_bytes(inode, len); |
Liu Bo | a09a0a7 | 2013-03-11 09:20:58 +0000 | [diff] [blame] | 2567 | btrfs_release_path(path); |
Liu Bo | 38c227d | 2013-01-29 03:18:40 +0000 | [diff] [blame] | 2568 | |
| 2569 | ret = btrfs_inc_extent_ref(trans, root, new->bytenr, |
| 2570 | new->disk_len, 0, |
| 2571 | backref->root_id, backref->inum, |
| 2572 | new->file_pos, 0); /* start - extent_offset */ |
| 2573 | if (ret) { |
| 2574 | btrfs_abort_transaction(trans, root, ret); |
| 2575 | goto out_free_path; |
| 2576 | } |
| 2577 | |
| 2578 | ret = 1; |
| 2579 | out_free_path: |
| 2580 | btrfs_release_path(path); |
Liu Bo | a09a0a7 | 2013-03-11 09:20:58 +0000 | [diff] [blame] | 2581 | path->leave_spinning = 0; |
Liu Bo | 38c227d | 2013-01-29 03:18:40 +0000 | [diff] [blame] | 2582 | btrfs_end_transaction(trans, root); |
| 2583 | out_unlock: |
| 2584 | unlock_extent_cached(&BTRFS_I(inode)->io_tree, lock_start, lock_end, |
| 2585 | &cached, GFP_NOFS); |
| 2586 | iput(inode); |
| 2587 | return ret; |
| 2588 | } |
| 2589 | |
Liu Bo | 6f51956 | 2013-10-29 10:45:05 +0800 | [diff] [blame] | 2590 | static void free_sa_defrag_extent(struct new_sa_defrag_extent *new) |
| 2591 | { |
| 2592 | struct old_sa_defrag_extent *old, *tmp; |
| 2593 | |
| 2594 | if (!new) |
| 2595 | return; |
| 2596 | |
| 2597 | list_for_each_entry_safe(old, tmp, &new->head, list) { |
| 2598 | list_del(&old->list); |
| 2599 | kfree(old); |
| 2600 | } |
| 2601 | kfree(new); |
| 2602 | } |
| 2603 | |
Liu Bo | 38c227d | 2013-01-29 03:18:40 +0000 | [diff] [blame] | 2604 | static void relink_file_extents(struct new_sa_defrag_extent *new) |
| 2605 | { |
| 2606 | struct btrfs_path *path; |
Liu Bo | 38c227d | 2013-01-29 03:18:40 +0000 | [diff] [blame] | 2607 | struct sa_defrag_extent_backref *backref; |
| 2608 | struct sa_defrag_extent_backref *prev = NULL; |
| 2609 | struct inode *inode; |
| 2610 | struct btrfs_root *root; |
| 2611 | struct rb_node *node; |
| 2612 | int ret; |
| 2613 | |
| 2614 | inode = new->inode; |
| 2615 | root = BTRFS_I(inode)->root; |
| 2616 | |
| 2617 | path = btrfs_alloc_path(); |
| 2618 | if (!path) |
| 2619 | return; |
| 2620 | |
| 2621 | if (!record_extent_backrefs(path, new)) { |
| 2622 | btrfs_free_path(path); |
| 2623 | goto out; |
| 2624 | } |
| 2625 | btrfs_release_path(path); |
| 2626 | |
| 2627 | while (1) { |
| 2628 | node = rb_first(&new->root); |
| 2629 | if (!node) |
| 2630 | break; |
| 2631 | rb_erase(node, &new->root); |
| 2632 | |
| 2633 | backref = rb_entry(node, struct sa_defrag_extent_backref, node); |
| 2634 | |
| 2635 | ret = relink_extent_backref(path, prev, backref); |
| 2636 | WARN_ON(ret < 0); |
| 2637 | |
| 2638 | kfree(prev); |
| 2639 | |
| 2640 | if (ret == 1) |
| 2641 | prev = backref; |
| 2642 | else |
| 2643 | prev = NULL; |
| 2644 | cond_resched(); |
| 2645 | } |
| 2646 | kfree(prev); |
| 2647 | |
| 2648 | btrfs_free_path(path); |
Liu Bo | 38c227d | 2013-01-29 03:18:40 +0000 | [diff] [blame] | 2649 | out: |
Liu Bo | 6f51956 | 2013-10-29 10:45:05 +0800 | [diff] [blame] | 2650 | free_sa_defrag_extent(new); |
| 2651 | |
Liu Bo | 38c227d | 2013-01-29 03:18:40 +0000 | [diff] [blame] | 2652 | atomic_dec(&root->fs_info->defrag_running); |
| 2653 | wake_up(&root->fs_info->transaction_wait); |
Liu Bo | 38c227d | 2013-01-29 03:18:40 +0000 | [diff] [blame] | 2654 | } |
| 2655 | |
| 2656 | static struct new_sa_defrag_extent * |
| 2657 | record_old_file_extents(struct inode *inode, |
| 2658 | struct btrfs_ordered_extent *ordered) |
| 2659 | { |
| 2660 | struct btrfs_root *root = BTRFS_I(inode)->root; |
| 2661 | struct btrfs_path *path; |
| 2662 | struct btrfs_key key; |
Liu Bo | 6f51956 | 2013-10-29 10:45:05 +0800 | [diff] [blame] | 2663 | struct old_sa_defrag_extent *old; |
Liu Bo | 38c227d | 2013-01-29 03:18:40 +0000 | [diff] [blame] | 2664 | struct new_sa_defrag_extent *new; |
| 2665 | int ret; |
| 2666 | |
| 2667 | new = kmalloc(sizeof(*new), GFP_NOFS); |
| 2668 | if (!new) |
| 2669 | return NULL; |
| 2670 | |
| 2671 | new->inode = inode; |
| 2672 | new->file_pos = ordered->file_offset; |
| 2673 | new->len = ordered->len; |
| 2674 | new->bytenr = ordered->start; |
| 2675 | new->disk_len = ordered->disk_len; |
| 2676 | new->compress_type = ordered->compress_type; |
| 2677 | new->root = RB_ROOT; |
| 2678 | INIT_LIST_HEAD(&new->head); |
| 2679 | |
| 2680 | path = btrfs_alloc_path(); |
| 2681 | if (!path) |
| 2682 | goto out_kfree; |
| 2683 | |
| 2684 | key.objectid = btrfs_ino(inode); |
| 2685 | key.type = BTRFS_EXTENT_DATA_KEY; |
| 2686 | key.offset = new->file_pos; |
| 2687 | |
| 2688 | ret = btrfs_search_slot(NULL, root, &key, path, 0, 0); |
| 2689 | if (ret < 0) |
| 2690 | goto out_free_path; |
| 2691 | if (ret > 0 && path->slots[0] > 0) |
| 2692 | path->slots[0]--; |
| 2693 | |
| 2694 | /* find out all the old extents for the file range */ |
| 2695 | while (1) { |
| 2696 | struct btrfs_file_extent_item *extent; |
| 2697 | struct extent_buffer *l; |
| 2698 | int slot; |
| 2699 | u64 num_bytes; |
| 2700 | u64 offset; |
| 2701 | u64 end; |
| 2702 | u64 disk_bytenr; |
| 2703 | u64 extent_offset; |
| 2704 | |
| 2705 | l = path->nodes[0]; |
| 2706 | slot = path->slots[0]; |
| 2707 | |
| 2708 | if (slot >= btrfs_header_nritems(l)) { |
| 2709 | ret = btrfs_next_leaf(root, path); |
| 2710 | if (ret < 0) |
Liu Bo | 6f51956 | 2013-10-29 10:45:05 +0800 | [diff] [blame] | 2711 | goto out_free_path; |
Liu Bo | 38c227d | 2013-01-29 03:18:40 +0000 | [diff] [blame] | 2712 | else if (ret > 0) |
| 2713 | break; |
| 2714 | continue; |
| 2715 | } |
| 2716 | |
| 2717 | btrfs_item_key_to_cpu(l, &key, slot); |
| 2718 | |
| 2719 | if (key.objectid != btrfs_ino(inode)) |
| 2720 | break; |
| 2721 | if (key.type != BTRFS_EXTENT_DATA_KEY) |
| 2722 | break; |
| 2723 | if (key.offset >= new->file_pos + new->len) |
| 2724 | break; |
| 2725 | |
| 2726 | extent = btrfs_item_ptr(l, slot, struct btrfs_file_extent_item); |
| 2727 | |
| 2728 | num_bytes = btrfs_file_extent_num_bytes(l, extent); |
| 2729 | if (key.offset + num_bytes < new->file_pos) |
| 2730 | goto next; |
| 2731 | |
| 2732 | disk_bytenr = btrfs_file_extent_disk_bytenr(l, extent); |
| 2733 | if (!disk_bytenr) |
| 2734 | goto next; |
| 2735 | |
| 2736 | extent_offset = btrfs_file_extent_offset(l, extent); |
| 2737 | |
| 2738 | old = kmalloc(sizeof(*old), GFP_NOFS); |
| 2739 | if (!old) |
Liu Bo | 6f51956 | 2013-10-29 10:45:05 +0800 | [diff] [blame] | 2740 | goto out_free_path; |
Liu Bo | 38c227d | 2013-01-29 03:18:40 +0000 | [diff] [blame] | 2741 | |
| 2742 | offset = max(new->file_pos, key.offset); |
| 2743 | end = min(new->file_pos + new->len, key.offset + num_bytes); |
| 2744 | |
| 2745 | old->bytenr = disk_bytenr; |
| 2746 | old->extent_offset = extent_offset; |
| 2747 | old->offset = offset - key.offset; |
| 2748 | old->len = end - offset; |
| 2749 | old->new = new; |
| 2750 | old->count = 0; |
| 2751 | list_add_tail(&old->list, &new->head); |
| 2752 | next: |
| 2753 | path->slots[0]++; |
| 2754 | cond_resched(); |
| 2755 | } |
| 2756 | |
| 2757 | btrfs_free_path(path); |
| 2758 | atomic_inc(&root->fs_info->defrag_running); |
| 2759 | |
| 2760 | return new; |
| 2761 | |
Liu Bo | 38c227d | 2013-01-29 03:18:40 +0000 | [diff] [blame] | 2762 | out_free_path: |
| 2763 | btrfs_free_path(path); |
| 2764 | out_kfree: |
Liu Bo | 6f51956 | 2013-10-29 10:45:05 +0800 | [diff] [blame] | 2765 | free_sa_defrag_extent(new); |
Liu Bo | 38c227d | 2013-01-29 03:18:40 +0000 | [diff] [blame] | 2766 | return NULL; |
| 2767 | } |
| 2768 | |
Miao Xie | e570fd2 | 2014-06-19 10:42:50 +0800 | [diff] [blame] | 2769 | static void btrfs_release_delalloc_bytes(struct btrfs_root *root, |
| 2770 | u64 start, u64 len) |
| 2771 | { |
| 2772 | struct btrfs_block_group_cache *cache; |
| 2773 | |
| 2774 | cache = btrfs_lookup_block_group(root->fs_info, start); |
| 2775 | ASSERT(cache); |
| 2776 | |
| 2777 | spin_lock(&cache->lock); |
| 2778 | cache->delalloc_bytes -= len; |
| 2779 | spin_unlock(&cache->lock); |
| 2780 | |
| 2781 | btrfs_put_block_group(cache); |
| 2782 | } |
| 2783 | |
Chris Mason | d352ac6 | 2008-09-29 15:18:18 -0400 | [diff] [blame] | 2784 | /* as ordered data IO finishes, this gets called so we can finish |
| 2785 | * an ordered extent if the range of bytes in the file it covers are |
| 2786 | * fully written. |
| 2787 | */ |
Josef Bacik | 5fd0204 | 2012-05-02 14:00:54 -0400 | [diff] [blame] | 2788 | static int btrfs_finish_ordered_io(struct btrfs_ordered_extent *ordered_extent) |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 2789 | { |
Josef Bacik | 5fd0204 | 2012-05-02 14:00:54 -0400 | [diff] [blame] | 2790 | struct inode *inode = ordered_extent->inode; |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 2791 | struct btrfs_root *root = BTRFS_I(inode)->root; |
Yan, Zheng | 0ca1f7c | 2010-05-16 10:48:47 -0400 | [diff] [blame] | 2792 | struct btrfs_trans_handle *trans = NULL; |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 2793 | struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree; |
Josef Bacik | 2ac55d4 | 2010-02-03 19:33:23 +0000 | [diff] [blame] | 2794 | struct extent_state *cached_state = NULL; |
Liu Bo | 38c227d | 2013-01-29 03:18:40 +0000 | [diff] [blame] | 2795 | struct new_sa_defrag_extent *new = NULL; |
Li Zefan | 261507a0 | 2010-12-17 14:21:50 +0800 | [diff] [blame] | 2796 | int compress_type = 0; |
Josef Bacik | 77cef2e | 2013-08-29 13:57:21 -0400 | [diff] [blame] | 2797 | int ret = 0; |
| 2798 | u64 logical_len = ordered_extent->len; |
Li Zefan | 82d5902 | 2011-04-20 10:33:24 +0800 | [diff] [blame] | 2799 | bool nolock; |
Josef Bacik | 77cef2e | 2013-08-29 13:57:21 -0400 | [diff] [blame] | 2800 | bool truncated = false; |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 2801 | |
Liu Bo | 83eea1f | 2012-07-10 05:28:39 -0600 | [diff] [blame] | 2802 | nolock = btrfs_is_free_space_inode(inode); |
Josef Bacik | 0cb59c9 | 2010-07-02 12:14:14 -0400 | [diff] [blame] | 2803 | |
Josef Bacik | 5fd0204 | 2012-05-02 14:00:54 -0400 | [diff] [blame] | 2804 | if (test_bit(BTRFS_ORDERED_IOERR, &ordered_extent->flags)) { |
| 2805 | ret = -EIO; |
| 2806 | goto out; |
| 2807 | } |
| 2808 | |
Miao Xie | f612496 | 2014-09-12 18:44:04 +0800 | [diff] [blame] | 2809 | btrfs_free_io_failure_record(inode, ordered_extent->file_offset, |
| 2810 | ordered_extent->file_offset + |
| 2811 | ordered_extent->len - 1); |
| 2812 | |
Josef Bacik | 77cef2e | 2013-08-29 13:57:21 -0400 | [diff] [blame] | 2813 | if (test_bit(BTRFS_ORDERED_TRUNCATED, &ordered_extent->flags)) { |
| 2814 | truncated = true; |
| 2815 | logical_len = ordered_extent->truncated_len; |
| 2816 | /* Truncated the entire extent, don't bother adding */ |
| 2817 | if (!logical_len) |
| 2818 | goto out; |
| 2819 | } |
| 2820 | |
Yan, Zheng | c216775 | 2009-11-12 09:34:21 +0000 | [diff] [blame] | 2821 | if (test_bit(BTRFS_ORDERED_NOCOW, &ordered_extent->flags)) { |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 2822 | BUG_ON(!list_empty(&ordered_extent->list)); /* Logic error */ |
Josef Bacik | 6c760c0 | 2012-11-09 10:53:21 -0500 | [diff] [blame] | 2823 | btrfs_ordered_update_i_size(inode, 0, ordered_extent); |
| 2824 | if (nolock) |
| 2825 | trans = btrfs_join_transaction_nolock(root); |
| 2826 | else |
| 2827 | trans = btrfs_join_transaction(root); |
| 2828 | if (IS_ERR(trans)) { |
| 2829 | ret = PTR_ERR(trans); |
| 2830 | trans = NULL; |
| 2831 | goto out; |
Yan, Zheng | c216775 | 2009-11-12 09:34:21 +0000 | [diff] [blame] | 2832 | } |
Josef Bacik | 6c760c0 | 2012-11-09 10:53:21 -0500 | [diff] [blame] | 2833 | trans->block_rsv = &root->fs_info->delalloc_block_rsv; |
| 2834 | ret = btrfs_update_inode_fallback(trans, root, inode); |
| 2835 | if (ret) /* -ENOMEM or corruption */ |
| 2836 | btrfs_abort_transaction(trans, root, ret); |
Yan, Zheng | c216775 | 2009-11-12 09:34:21 +0000 | [diff] [blame] | 2837 | goto out; |
| 2838 | } |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 2839 | |
Josef Bacik | 2ac55d4 | 2010-02-03 19:33:23 +0000 | [diff] [blame] | 2840 | lock_extent_bits(io_tree, ordered_extent->file_offset, |
| 2841 | ordered_extent->file_offset + ordered_extent->len - 1, |
Jeff Mahoney | d008237 | 2012-03-01 14:57:19 +0100 | [diff] [blame] | 2842 | 0, &cached_state); |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 2843 | |
Liu Bo | 38c227d | 2013-01-29 03:18:40 +0000 | [diff] [blame] | 2844 | ret = test_range_bit(io_tree, ordered_extent->file_offset, |
| 2845 | ordered_extent->file_offset + ordered_extent->len - 1, |
| 2846 | EXTENT_DEFRAG, 1, cached_state); |
| 2847 | if (ret) { |
| 2848 | u64 last_snapshot = btrfs_root_last_snapshot(&root->root_item); |
Josef Bacik | 8101c8d | 2014-01-29 16:05:30 -0500 | [diff] [blame] | 2849 | if (0 && last_snapshot >= BTRFS_I(inode)->generation) |
Liu Bo | 38c227d | 2013-01-29 03:18:40 +0000 | [diff] [blame] | 2850 | /* the inode is shared */ |
| 2851 | new = record_old_file_extents(inode, ordered_extent); |
| 2852 | |
| 2853 | clear_extent_bit(io_tree, ordered_extent->file_offset, |
| 2854 | ordered_extent->file_offset + ordered_extent->len - 1, |
| 2855 | EXTENT_DEFRAG, 0, 0, &cached_state, GFP_NOFS); |
| 2856 | } |
| 2857 | |
Josef Bacik | 0cb59c9 | 2010-07-02 12:14:14 -0400 | [diff] [blame] | 2858 | if (nolock) |
Josef Bacik | 7a7eaa4 | 2011-04-13 12:54:33 -0400 | [diff] [blame] | 2859 | trans = btrfs_join_transaction_nolock(root); |
Josef Bacik | 0cb59c9 | 2010-07-02 12:14:14 -0400 | [diff] [blame] | 2860 | else |
Josef Bacik | 7a7eaa4 | 2011-04-13 12:54:33 -0400 | [diff] [blame] | 2861 | trans = btrfs_join_transaction(root); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 2862 | if (IS_ERR(trans)) { |
| 2863 | ret = PTR_ERR(trans); |
| 2864 | trans = NULL; |
| 2865 | goto out_unlock; |
| 2866 | } |
Chris Mason | a79b7d4 | 2014-05-22 16:18:52 -0700 | [diff] [blame] | 2867 | |
Yan, Zheng | 0ca1f7c | 2010-05-16 10:48:47 -0400 | [diff] [blame] | 2868 | trans->block_rsv = &root->fs_info->delalloc_block_rsv; |
Yan, Zheng | c216775 | 2009-11-12 09:34:21 +0000 | [diff] [blame] | 2869 | |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 2870 | if (test_bit(BTRFS_ORDERED_COMPRESSED, &ordered_extent->flags)) |
Li Zefan | 261507a0 | 2010-12-17 14:21:50 +0800 | [diff] [blame] | 2871 | compress_type = ordered_extent->compress_type; |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 2872 | if (test_bit(BTRFS_ORDERED_PREALLOC, &ordered_extent->flags)) { |
Li Zefan | 261507a0 | 2010-12-17 14:21:50 +0800 | [diff] [blame] | 2873 | BUG_ON(compress_type); |
Yan, Zheng | 920bbbf | 2009-11-12 09:34:08 +0000 | [diff] [blame] | 2874 | ret = btrfs_mark_extent_written(trans, inode, |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 2875 | ordered_extent->file_offset, |
| 2876 | ordered_extent->file_offset + |
Josef Bacik | 77cef2e | 2013-08-29 13:57:21 -0400 | [diff] [blame] | 2877 | logical_len); |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 2878 | } else { |
Josef Bacik | 0af3d00 | 2010-06-21 14:48:16 -0400 | [diff] [blame] | 2879 | BUG_ON(root == root->fs_info->tree_root); |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 2880 | ret = insert_reserved_file_extent(trans, inode, |
| 2881 | ordered_extent->file_offset, |
| 2882 | ordered_extent->start, |
| 2883 | ordered_extent->disk_len, |
Josef Bacik | 77cef2e | 2013-08-29 13:57:21 -0400 | [diff] [blame] | 2884 | logical_len, logical_len, |
Li Zefan | 261507a0 | 2010-12-17 14:21:50 +0800 | [diff] [blame] | 2885 | compress_type, 0, 0, |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 2886 | BTRFS_FILE_EXTENT_REG); |
Miao Xie | e570fd2 | 2014-06-19 10:42:50 +0800 | [diff] [blame] | 2887 | if (!ret) |
| 2888 | btrfs_release_delalloc_bytes(root, |
| 2889 | ordered_extent->start, |
| 2890 | ordered_extent->disk_len); |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 2891 | } |
Josef Bacik | 5dc562c | 2012-08-17 13:14:17 -0400 | [diff] [blame] | 2892 | unpin_extent_cache(&BTRFS_I(inode)->extent_tree, |
| 2893 | ordered_extent->file_offset, ordered_extent->len, |
| 2894 | trans->transid); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 2895 | if (ret < 0) { |
| 2896 | btrfs_abort_transaction(trans, root, ret); |
Josef Bacik | 5fd0204 | 2012-05-02 14:00:54 -0400 | [diff] [blame] | 2897 | goto out_unlock; |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 2898 | } |
Josef Bacik | 2ac55d4 | 2010-02-03 19:33:23 +0000 | [diff] [blame] | 2899 | |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 2900 | add_pending_csums(trans, inode, ordered_extent->file_offset, |
| 2901 | &ordered_extent->list); |
| 2902 | |
Josef Bacik | 6c760c0 | 2012-11-09 10:53:21 -0500 | [diff] [blame] | 2903 | btrfs_ordered_update_i_size(inode, 0, ordered_extent); |
| 2904 | ret = btrfs_update_inode_fallback(trans, root, inode); |
| 2905 | if (ret) { /* -ENOMEM or corruption */ |
| 2906 | btrfs_abort_transaction(trans, root, ret); |
| 2907 | goto out_unlock; |
Josef Bacik | 1ef30be | 2011-04-05 19:25:36 -0400 | [diff] [blame] | 2908 | } |
| 2909 | ret = 0; |
Josef Bacik | 5fd0204 | 2012-05-02 14:00:54 -0400 | [diff] [blame] | 2910 | out_unlock: |
| 2911 | unlock_extent_cached(io_tree, ordered_extent->file_offset, |
| 2912 | ordered_extent->file_offset + |
| 2913 | ordered_extent->len - 1, &cached_state, GFP_NOFS); |
Yan, Zheng | c216775 | 2009-11-12 09:34:21 +0000 | [diff] [blame] | 2914 | out: |
Josef Bacik | 5b0e95b | 2011-10-06 08:58:24 -0400 | [diff] [blame] | 2915 | if (root != root->fs_info->tree_root) |
Josef Bacik | 0cb59c9 | 2010-07-02 12:14:14 -0400 | [diff] [blame] | 2916 | btrfs_delalloc_release_metadata(inode, ordered_extent->len); |
Miao Xie | a698d075 | 2012-09-20 01:51:59 -0600 | [diff] [blame] | 2917 | if (trans) |
| 2918 | btrfs_end_transaction(trans, root); |
Josef Bacik | 0cb59c9 | 2010-07-02 12:14:14 -0400 | [diff] [blame] | 2919 | |
Josef Bacik | 77cef2e | 2013-08-29 13:57:21 -0400 | [diff] [blame] | 2920 | if (ret || truncated) { |
| 2921 | u64 start, end; |
| 2922 | |
| 2923 | if (truncated) |
| 2924 | start = ordered_extent->file_offset + logical_len; |
| 2925 | else |
| 2926 | start = ordered_extent->file_offset; |
| 2927 | end = ordered_extent->file_offset + ordered_extent->len - 1; |
| 2928 | clear_extent_uptodate(io_tree, start, end, NULL, GFP_NOFS); |
| 2929 | |
| 2930 | /* Drop the cache for the part of the extent we didn't write. */ |
| 2931 | btrfs_drop_extent_cache(inode, start, end, 0); |
Josef Bacik | 5fd0204 | 2012-05-02 14:00:54 -0400 | [diff] [blame] | 2932 | |
Josef Bacik | 0bec9ef | 2013-01-31 14:58:00 -0500 | [diff] [blame] | 2933 | /* |
| 2934 | * If the ordered extent had an IOERR or something else went |
| 2935 | * wrong we need to return the space for this ordered extent |
Josef Bacik | 77cef2e | 2013-08-29 13:57:21 -0400 | [diff] [blame] | 2936 | * back to the allocator. We only free the extent in the |
| 2937 | * truncated case if we didn't write out the extent at all. |
Josef Bacik | 0bec9ef | 2013-01-31 14:58:00 -0500 | [diff] [blame] | 2938 | */ |
Josef Bacik | 77cef2e | 2013-08-29 13:57:21 -0400 | [diff] [blame] | 2939 | if ((ret || !logical_len) && |
| 2940 | !test_bit(BTRFS_ORDERED_NOCOW, &ordered_extent->flags) && |
Josef Bacik | 0bec9ef | 2013-01-31 14:58:00 -0500 | [diff] [blame] | 2941 | !test_bit(BTRFS_ORDERED_PREALLOC, &ordered_extent->flags)) |
| 2942 | btrfs_free_reserved_extent(root, ordered_extent->start, |
Miao Xie | e570fd2 | 2014-06-19 10:42:50 +0800 | [diff] [blame] | 2943 | ordered_extent->disk_len, 1); |
Josef Bacik | 0bec9ef | 2013-01-31 14:58:00 -0500 | [diff] [blame] | 2944 | } |
| 2945 | |
| 2946 | |
Josef Bacik | 5fd0204 | 2012-05-02 14:00:54 -0400 | [diff] [blame] | 2947 | /* |
Liu Bo | 8bad3c0 | 2012-06-18 12:14:23 +0800 | [diff] [blame] | 2948 | * This needs to be done to make sure anybody waiting knows we are done |
| 2949 | * updating everything for this ordered extent. |
Josef Bacik | 5fd0204 | 2012-05-02 14:00:54 -0400 | [diff] [blame] | 2950 | */ |
| 2951 | btrfs_remove_ordered_extent(inode, ordered_extent); |
| 2952 | |
Liu Bo | 38c227d | 2013-01-29 03:18:40 +0000 | [diff] [blame] | 2953 | /* for snapshot-aware defrag */ |
Liu Bo | 6f51956 | 2013-10-29 10:45:05 +0800 | [diff] [blame] | 2954 | if (new) { |
| 2955 | if (ret) { |
| 2956 | free_sa_defrag_extent(new); |
| 2957 | atomic_dec(&root->fs_info->defrag_running); |
| 2958 | } else { |
| 2959 | relink_file_extents(new); |
| 2960 | } |
| 2961 | } |
Liu Bo | 38c227d | 2013-01-29 03:18:40 +0000 | [diff] [blame] | 2962 | |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 2963 | /* once for us */ |
| 2964 | btrfs_put_ordered_extent(ordered_extent); |
| 2965 | /* once for the tree */ |
| 2966 | btrfs_put_ordered_extent(ordered_extent); |
| 2967 | |
Josef Bacik | 5fd0204 | 2012-05-02 14:00:54 -0400 | [diff] [blame] | 2968 | return ret; |
| 2969 | } |
| 2970 | |
| 2971 | static void finish_ordered_fn(struct btrfs_work *work) |
| 2972 | { |
| 2973 | struct btrfs_ordered_extent *ordered_extent; |
| 2974 | ordered_extent = container_of(work, struct btrfs_ordered_extent, work); |
| 2975 | btrfs_finish_ordered_io(ordered_extent); |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 2976 | } |
| 2977 | |
Christoph Hellwig | b295086 | 2008-12-02 09:54:17 -0500 | [diff] [blame] | 2978 | 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] | 2979 | struct extent_state *state, int uptodate) |
| 2980 | { |
Josef Bacik | 5fd0204 | 2012-05-02 14:00:54 -0400 | [diff] [blame] | 2981 | struct inode *inode = page->mapping->host; |
| 2982 | struct btrfs_root *root = BTRFS_I(inode)->root; |
| 2983 | struct btrfs_ordered_extent *ordered_extent = NULL; |
Liu Bo | 9e0af23 | 2014-08-15 23:36:53 +0800 | [diff] [blame] | 2984 | struct btrfs_workqueue *wq; |
| 2985 | btrfs_work_func_t func; |
Josef Bacik | 5fd0204 | 2012-05-02 14:00:54 -0400 | [diff] [blame] | 2986 | |
liubo | 1abe9b8 | 2011-03-24 11:18:59 +0000 | [diff] [blame] | 2987 | trace_btrfs_writepage_end_io_hook(page, start, end, uptodate); |
| 2988 | |
Chris Mason | 8b62b72 | 2009-09-02 16:53:46 -0400 | [diff] [blame] | 2989 | ClearPagePrivate2(page); |
Josef Bacik | 5fd0204 | 2012-05-02 14:00:54 -0400 | [diff] [blame] | 2990 | if (!btrfs_dec_test_ordered_pending(inode, &ordered_extent, start, |
| 2991 | end - start + 1, uptodate)) |
| 2992 | return 0; |
| 2993 | |
Liu Bo | 9e0af23 | 2014-08-15 23:36:53 +0800 | [diff] [blame] | 2994 | if (btrfs_is_free_space_inode(inode)) { |
| 2995 | wq = root->fs_info->endio_freespace_worker; |
| 2996 | func = btrfs_freespace_write_helper; |
| 2997 | } else { |
| 2998 | wq = root->fs_info->endio_write_workers; |
| 2999 | func = btrfs_endio_write_helper; |
| 3000 | } |
Josef Bacik | 5fd0204 | 2012-05-02 14:00:54 -0400 | [diff] [blame] | 3001 | |
Liu Bo | 9e0af23 | 2014-08-15 23:36:53 +0800 | [diff] [blame] | 3002 | btrfs_init_work(&ordered_extent->work, func, finish_ordered_fn, NULL, |
| 3003 | NULL); |
| 3004 | btrfs_queue_work(wq, &ordered_extent->work); |
Josef Bacik | 5fd0204 | 2012-05-02 14:00:54 -0400 | [diff] [blame] | 3005 | |
| 3006 | return 0; |
Chris Mason | 211f90e | 2008-07-18 11:56:15 -0400 | [diff] [blame] | 3007 | } |
| 3008 | |
Miao Xie | dc380ae | 2014-09-12 18:43:55 +0800 | [diff] [blame] | 3009 | static int __readpage_endio_check(struct inode *inode, |
| 3010 | struct btrfs_io_bio *io_bio, |
| 3011 | int icsum, struct page *page, |
| 3012 | int pgoff, u64 start, size_t len) |
| 3013 | { |
| 3014 | char *kaddr; |
| 3015 | u32 csum_expected; |
| 3016 | u32 csum = ~(u32)0; |
| 3017 | static DEFINE_RATELIMIT_STATE(_rs, DEFAULT_RATELIMIT_INTERVAL, |
| 3018 | DEFAULT_RATELIMIT_BURST); |
| 3019 | |
| 3020 | csum_expected = *(((u32 *)io_bio->csum) + icsum); |
| 3021 | |
| 3022 | kaddr = kmap_atomic(page); |
| 3023 | csum = btrfs_csum_data(kaddr + pgoff, csum, len); |
| 3024 | btrfs_csum_final(csum, (char *)&csum); |
| 3025 | if (csum != csum_expected) |
| 3026 | goto zeroit; |
| 3027 | |
| 3028 | kunmap_atomic(kaddr); |
| 3029 | return 0; |
| 3030 | zeroit: |
| 3031 | if (__ratelimit(&_rs)) |
David Sterba | f0954c6 | 2014-12-19 18:38:44 +0100 | [diff] [blame] | 3032 | btrfs_warn(BTRFS_I(inode)->root->fs_info, |
Miao Xie | dc380ae | 2014-09-12 18:43:55 +0800 | [diff] [blame] | 3033 | "csum failed ino %llu off %llu csum %u expected csum %u", |
| 3034 | btrfs_ino(inode), start, csum, csum_expected); |
| 3035 | memset(kaddr + pgoff, 1, len); |
| 3036 | flush_dcache_page(page); |
| 3037 | kunmap_atomic(kaddr); |
| 3038 | if (csum_expected == 0) |
| 3039 | return 0; |
| 3040 | return -EIO; |
| 3041 | } |
| 3042 | |
Chris Mason | d352ac6 | 2008-09-29 15:18:18 -0400 | [diff] [blame] | 3043 | /* |
Chris Mason | d352ac6 | 2008-09-29 15:18:18 -0400 | [diff] [blame] | 3044 | * 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] | 3045 | * if there's a match, we allow the bio to finish. If not, the code in |
| 3046 | * extent_io.c will try to find good copies for us. |
Chris Mason | d352ac6 | 2008-09-29 15:18:18 -0400 | [diff] [blame] | 3047 | */ |
Miao Xie | facc8a22 | 2013-07-25 19:22:34 +0800 | [diff] [blame] | 3048 | static int btrfs_readpage_end_io_hook(struct btrfs_io_bio *io_bio, |
| 3049 | u64 phy_offset, struct page *page, |
| 3050 | u64 start, u64 end, int mirror) |
Chris Mason | 07157aa | 2007-08-30 08:50:51 -0400 | [diff] [blame] | 3051 | { |
Miao Xie | 4eee4fa | 2012-12-21 09:17:45 +0000 | [diff] [blame] | 3052 | size_t offset = start - page_offset(page); |
Chris Mason | 07157aa | 2007-08-30 08:50:51 -0400 | [diff] [blame] | 3053 | struct inode *inode = page->mapping->host; |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 3054 | struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree; |
Chris Mason | ff79f81 | 2007-10-15 16:22:25 -0400 | [diff] [blame] | 3055 | struct btrfs_root *root = BTRFS_I(inode)->root; |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 3056 | |
Chris Mason | d20f704 | 2008-12-08 16:58:54 -0500 | [diff] [blame] | 3057 | if (PageChecked(page)) { |
| 3058 | ClearPageChecked(page); |
Miao Xie | dc380ae | 2014-09-12 18:43:55 +0800 | [diff] [blame] | 3059 | return 0; |
Chris Mason | d20f704 | 2008-12-08 16:58:54 -0500 | [diff] [blame] | 3060 | } |
Christoph Hellwig | 6cbff00 | 2009-04-17 10:37:41 +0200 | [diff] [blame] | 3061 | |
| 3062 | if (BTRFS_I(inode)->flags & BTRFS_INODE_NODATASUM) |
Miao Xie | dc380ae | 2014-09-12 18:43:55 +0800 | [diff] [blame] | 3063 | return 0; |
Chris Mason | d20f704 | 2008-12-08 16:58:54 -0500 | [diff] [blame] | 3064 | |
Yan Zheng | 17d217f | 2008-12-12 10:03:38 -0500 | [diff] [blame] | 3065 | if (root->root_key.objectid == BTRFS_DATA_RELOC_TREE_OBJECTID && |
Chris Mason | 9655d29 | 2009-09-02 15:22:30 -0400 | [diff] [blame] | 3066 | test_range_bit(io_tree, start, end, EXTENT_NODATASUM, 1, NULL)) { |
Yan Zheng | 17d217f | 2008-12-12 10:03:38 -0500 | [diff] [blame] | 3067 | clear_extent_bits(io_tree, start, end, EXTENT_NODATASUM, |
| 3068 | GFP_NOFS); |
| 3069 | return 0; |
| 3070 | } |
| 3071 | |
Miao Xie | facc8a22 | 2013-07-25 19:22:34 +0800 | [diff] [blame] | 3072 | phy_offset >>= inode->i_sb->s_blocksize_bits; |
Miao Xie | dc380ae | 2014-09-12 18:43:55 +0800 | [diff] [blame] | 3073 | return __readpage_endio_check(inode, io_bio, phy_offset, page, offset, |
| 3074 | start, (size_t)(end - start + 1)); |
Chris Mason | 07157aa | 2007-08-30 08:50:51 -0400 | [diff] [blame] | 3075 | } |
Chris Mason | b888db2 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 3076 | |
Yan, Zheng | 24bbcf0 | 2009-11-12 09:36:34 +0000 | [diff] [blame] | 3077 | struct delayed_iput { |
| 3078 | struct list_head list; |
| 3079 | struct inode *inode; |
| 3080 | }; |
| 3081 | |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 3082 | /* JDM: If this is fs-wide, why can't we add a pointer to |
| 3083 | * btrfs_inode instead and avoid the allocation? */ |
Yan, Zheng | 24bbcf0 | 2009-11-12 09:36:34 +0000 | [diff] [blame] | 3084 | void btrfs_add_delayed_iput(struct inode *inode) |
| 3085 | { |
| 3086 | struct btrfs_fs_info *fs_info = BTRFS_I(inode)->root->fs_info; |
| 3087 | struct delayed_iput *delayed; |
| 3088 | |
| 3089 | if (atomic_add_unless(&inode->i_count, -1, 1)) |
| 3090 | return; |
| 3091 | |
| 3092 | delayed = kmalloc(sizeof(*delayed), GFP_NOFS | __GFP_NOFAIL); |
| 3093 | delayed->inode = inode; |
| 3094 | |
| 3095 | spin_lock(&fs_info->delayed_iput_lock); |
| 3096 | list_add_tail(&delayed->list, &fs_info->delayed_iputs); |
| 3097 | spin_unlock(&fs_info->delayed_iput_lock); |
| 3098 | } |
| 3099 | |
| 3100 | void btrfs_run_delayed_iputs(struct btrfs_root *root) |
| 3101 | { |
| 3102 | LIST_HEAD(list); |
| 3103 | struct btrfs_fs_info *fs_info = root->fs_info; |
| 3104 | struct delayed_iput *delayed; |
| 3105 | int empty; |
| 3106 | |
| 3107 | spin_lock(&fs_info->delayed_iput_lock); |
| 3108 | empty = list_empty(&fs_info->delayed_iputs); |
| 3109 | spin_unlock(&fs_info->delayed_iput_lock); |
| 3110 | if (empty) |
| 3111 | return; |
| 3112 | |
Yan, Zheng | 24bbcf0 | 2009-11-12 09:36:34 +0000 | [diff] [blame] | 3113 | spin_lock(&fs_info->delayed_iput_lock); |
| 3114 | list_splice_init(&fs_info->delayed_iputs, &list); |
| 3115 | spin_unlock(&fs_info->delayed_iput_lock); |
| 3116 | |
| 3117 | while (!list_empty(&list)) { |
| 3118 | delayed = list_entry(list.next, struct delayed_iput, list); |
| 3119 | list_del(&delayed->list); |
| 3120 | iput(delayed->inode); |
| 3121 | kfree(delayed); |
| 3122 | } |
Yan, Zheng | 24bbcf0 | 2009-11-12 09:36:34 +0000 | [diff] [blame] | 3123 | } |
| 3124 | |
Yan, Zheng | d68fc57 | 2010-05-16 10:49:58 -0400 | [diff] [blame] | 3125 | /* |
Justin P. Mattock | 42b2aa8 | 2011-11-28 20:31:00 -0800 | [diff] [blame] | 3126 | * This is called in transaction commit time. If there are no orphan |
Yan, Zheng | d68fc57 | 2010-05-16 10:49:58 -0400 | [diff] [blame] | 3127 | * files in the subvolume, it removes orphan item and frees block_rsv |
| 3128 | * structure. |
| 3129 | */ |
| 3130 | void btrfs_orphan_commit_root(struct btrfs_trans_handle *trans, |
| 3131 | struct btrfs_root *root) |
| 3132 | { |
Josef Bacik | 90290e1 | 2011-12-02 15:44:12 -0500 | [diff] [blame] | 3133 | struct btrfs_block_rsv *block_rsv; |
Yan, Zheng | d68fc57 | 2010-05-16 10:49:58 -0400 | [diff] [blame] | 3134 | int ret; |
| 3135 | |
Josef Bacik | 8a35d95 | 2012-05-23 14:26:42 -0400 | [diff] [blame] | 3136 | if (atomic_read(&root->orphan_inodes) || |
Yan, Zheng | d68fc57 | 2010-05-16 10:49:58 -0400 | [diff] [blame] | 3137 | root->orphan_cleanup_state != ORPHAN_CLEANUP_DONE) |
| 3138 | return; |
| 3139 | |
Josef Bacik | 90290e1 | 2011-12-02 15:44:12 -0500 | [diff] [blame] | 3140 | spin_lock(&root->orphan_lock); |
Josef Bacik | 8a35d95 | 2012-05-23 14:26:42 -0400 | [diff] [blame] | 3141 | if (atomic_read(&root->orphan_inodes)) { |
Josef Bacik | 90290e1 | 2011-12-02 15:44:12 -0500 | [diff] [blame] | 3142 | spin_unlock(&root->orphan_lock); |
| 3143 | return; |
| 3144 | } |
| 3145 | |
| 3146 | if (root->orphan_cleanup_state != ORPHAN_CLEANUP_DONE) { |
| 3147 | spin_unlock(&root->orphan_lock); |
| 3148 | return; |
| 3149 | } |
| 3150 | |
| 3151 | block_rsv = root->orphan_block_rsv; |
| 3152 | root->orphan_block_rsv = NULL; |
| 3153 | spin_unlock(&root->orphan_lock); |
| 3154 | |
Miao Xie | 27cdeb7 | 2014-04-02 19:51:05 +0800 | [diff] [blame] | 3155 | if (test_bit(BTRFS_ROOT_ORPHAN_ITEM_INSERTED, &root->state) && |
Yan, Zheng | d68fc57 | 2010-05-16 10:49:58 -0400 | [diff] [blame] | 3156 | btrfs_root_refs(&root->root_item) > 0) { |
| 3157 | ret = btrfs_del_orphan_item(trans, root->fs_info->tree_root, |
| 3158 | root->root_key.objectid); |
Josef Bacik | 4ef31a4 | 2013-08-13 14:10:08 -0400 | [diff] [blame] | 3159 | if (ret) |
| 3160 | btrfs_abort_transaction(trans, root, ret); |
| 3161 | else |
Miao Xie | 27cdeb7 | 2014-04-02 19:51:05 +0800 | [diff] [blame] | 3162 | clear_bit(BTRFS_ROOT_ORPHAN_ITEM_INSERTED, |
| 3163 | &root->state); |
Yan, Zheng | d68fc57 | 2010-05-16 10:49:58 -0400 | [diff] [blame] | 3164 | } |
| 3165 | |
Josef Bacik | 90290e1 | 2011-12-02 15:44:12 -0500 | [diff] [blame] | 3166 | if (block_rsv) { |
| 3167 | WARN_ON(block_rsv->size > 0); |
| 3168 | btrfs_free_block_rsv(root, block_rsv); |
Yan, Zheng | d68fc57 | 2010-05-16 10:49:58 -0400 | [diff] [blame] | 3169 | } |
| 3170 | } |
| 3171 | |
| 3172 | /* |
Josef Bacik | 7b12876 | 2008-07-24 12:17:14 -0400 | [diff] [blame] | 3173 | * This creates an orphan entry for the given inode in case something goes |
| 3174 | * wrong in the middle of an unlink/truncate. |
Yan, Zheng | d68fc57 | 2010-05-16 10:49:58 -0400 | [diff] [blame] | 3175 | * |
| 3176 | * NOTE: caller of this function should reserve 5 units of metadata for |
| 3177 | * this function. |
Josef Bacik | 7b12876 | 2008-07-24 12:17:14 -0400 | [diff] [blame] | 3178 | */ |
| 3179 | int btrfs_orphan_add(struct btrfs_trans_handle *trans, struct inode *inode) |
| 3180 | { |
| 3181 | struct btrfs_root *root = BTRFS_I(inode)->root; |
Yan, Zheng | d68fc57 | 2010-05-16 10:49:58 -0400 | [diff] [blame] | 3182 | struct btrfs_block_rsv *block_rsv = NULL; |
| 3183 | int reserve = 0; |
| 3184 | int insert = 0; |
| 3185 | int ret; |
Josef Bacik | 7b12876 | 2008-07-24 12:17:14 -0400 | [diff] [blame] | 3186 | |
Yan, Zheng | d68fc57 | 2010-05-16 10:49:58 -0400 | [diff] [blame] | 3187 | if (!root->orphan_block_rsv) { |
Miao Xie | 66d8f3d | 2012-09-06 04:02:28 -0600 | [diff] [blame] | 3188 | block_rsv = btrfs_alloc_block_rsv(root, BTRFS_BLOCK_RSV_TEMP); |
Tsutomu Itoh | b532402 | 2011-07-19 07:27:20 +0000 | [diff] [blame] | 3189 | if (!block_rsv) |
| 3190 | return -ENOMEM; |
Josef Bacik | 7b12876 | 2008-07-24 12:17:14 -0400 | [diff] [blame] | 3191 | } |
| 3192 | |
Yan, Zheng | d68fc57 | 2010-05-16 10:49:58 -0400 | [diff] [blame] | 3193 | spin_lock(&root->orphan_lock); |
| 3194 | if (!root->orphan_block_rsv) { |
| 3195 | root->orphan_block_rsv = block_rsv; |
| 3196 | } else if (block_rsv) { |
| 3197 | btrfs_free_block_rsv(root, block_rsv); |
| 3198 | block_rsv = NULL; |
| 3199 | } |
Josef Bacik | 7b12876 | 2008-07-24 12:17:14 -0400 | [diff] [blame] | 3200 | |
Josef Bacik | 8a35d95 | 2012-05-23 14:26:42 -0400 | [diff] [blame] | 3201 | if (!test_and_set_bit(BTRFS_INODE_HAS_ORPHAN_ITEM, |
| 3202 | &BTRFS_I(inode)->runtime_flags)) { |
Yan, Zheng | d68fc57 | 2010-05-16 10:49:58 -0400 | [diff] [blame] | 3203 | #if 0 |
| 3204 | /* |
| 3205 | * For proper ENOSPC handling, we should do orphan |
| 3206 | * cleanup when mounting. But this introduces backward |
| 3207 | * compatibility issue. |
| 3208 | */ |
| 3209 | if (!xchg(&root->orphan_item_inserted, 1)) |
| 3210 | insert = 2; |
| 3211 | else |
| 3212 | insert = 1; |
| 3213 | #endif |
| 3214 | insert = 1; |
Miao Xie | 321f0e7 | 2012-08-28 22:13:02 -0600 | [diff] [blame] | 3215 | atomic_inc(&root->orphan_inodes); |
Yan, Zheng | d68fc57 | 2010-05-16 10:49:58 -0400 | [diff] [blame] | 3216 | } |
Josef Bacik | 7b12876 | 2008-07-24 12:17:14 -0400 | [diff] [blame] | 3217 | |
Josef Bacik | 72ac3c0 | 2012-05-23 14:13:11 -0400 | [diff] [blame] | 3218 | if (!test_and_set_bit(BTRFS_INODE_ORPHAN_META_RESERVED, |
| 3219 | &BTRFS_I(inode)->runtime_flags)) |
Yan, Zheng | d68fc57 | 2010-05-16 10:49:58 -0400 | [diff] [blame] | 3220 | reserve = 1; |
Yan, Zheng | d68fc57 | 2010-05-16 10:49:58 -0400 | [diff] [blame] | 3221 | spin_unlock(&root->orphan_lock); |
Josef Bacik | 7b12876 | 2008-07-24 12:17:14 -0400 | [diff] [blame] | 3222 | |
Yan, Zheng | d68fc57 | 2010-05-16 10:49:58 -0400 | [diff] [blame] | 3223 | /* grab metadata reservation from transaction handle */ |
| 3224 | if (reserve) { |
| 3225 | ret = btrfs_orphan_reserve_metadata(trans, inode); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 3226 | BUG_ON(ret); /* -ENOSPC in reservation; Logic error? JDM */ |
Yan, Zheng | d68fc57 | 2010-05-16 10:49:58 -0400 | [diff] [blame] | 3227 | } |
| 3228 | |
| 3229 | /* insert an orphan item to track this unlinked/truncated file */ |
| 3230 | if (insert >= 1) { |
Li Zefan | 33345d01 | 2011-04-20 10:31:50 +0800 | [diff] [blame] | 3231 | ret = btrfs_insert_orphan_item(trans, root, btrfs_ino(inode)); |
Josef Bacik | 4ef31a4 | 2013-08-13 14:10:08 -0400 | [diff] [blame] | 3232 | if (ret) { |
Filipe David Borba Manana | 703c88e | 2013-09-22 21:54:55 +0100 | [diff] [blame] | 3233 | atomic_dec(&root->orphan_inodes); |
Josef Bacik | 4ef31a4 | 2013-08-13 14:10:08 -0400 | [diff] [blame] | 3234 | if (reserve) { |
| 3235 | clear_bit(BTRFS_INODE_ORPHAN_META_RESERVED, |
| 3236 | &BTRFS_I(inode)->runtime_flags); |
| 3237 | btrfs_orphan_release_metadata(inode); |
| 3238 | } |
| 3239 | if (ret != -EEXIST) { |
Josef Bacik | e8e7cff | 2013-08-21 15:54:00 -0400 | [diff] [blame] | 3240 | clear_bit(BTRFS_INODE_HAS_ORPHAN_ITEM, |
| 3241 | &BTRFS_I(inode)->runtime_flags); |
Josef Bacik | 4ef31a4 | 2013-08-13 14:10:08 -0400 | [diff] [blame] | 3242 | btrfs_abort_transaction(trans, root, ret); |
| 3243 | return ret; |
| 3244 | } |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 3245 | } |
| 3246 | ret = 0; |
Yan, Zheng | d68fc57 | 2010-05-16 10:49:58 -0400 | [diff] [blame] | 3247 | } |
| 3248 | |
| 3249 | /* insert an orphan item to track subvolume contains orphan files */ |
| 3250 | if (insert >= 2) { |
| 3251 | ret = btrfs_insert_orphan_item(trans, root->fs_info->tree_root, |
| 3252 | root->root_key.objectid); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 3253 | if (ret && ret != -EEXIST) { |
| 3254 | btrfs_abort_transaction(trans, root, ret); |
| 3255 | return ret; |
| 3256 | } |
Yan, Zheng | d68fc57 | 2010-05-16 10:49:58 -0400 | [diff] [blame] | 3257 | } |
| 3258 | return 0; |
Josef Bacik | 7b12876 | 2008-07-24 12:17:14 -0400 | [diff] [blame] | 3259 | } |
| 3260 | |
| 3261 | /* |
| 3262 | * We have done the truncate/delete so we can go ahead and remove the orphan |
| 3263 | * item for this particular inode. |
| 3264 | */ |
Eric Sandeen | 48a3b63 | 2013-04-25 20:41:01 +0000 | [diff] [blame] | 3265 | static int btrfs_orphan_del(struct btrfs_trans_handle *trans, |
| 3266 | struct inode *inode) |
Josef Bacik | 7b12876 | 2008-07-24 12:17:14 -0400 | [diff] [blame] | 3267 | { |
| 3268 | struct btrfs_root *root = BTRFS_I(inode)->root; |
Yan, Zheng | d68fc57 | 2010-05-16 10:49:58 -0400 | [diff] [blame] | 3269 | int delete_item = 0; |
| 3270 | int release_rsv = 0; |
Josef Bacik | 7b12876 | 2008-07-24 12:17:14 -0400 | [diff] [blame] | 3271 | int ret = 0; |
| 3272 | |
Yan, Zheng | d68fc57 | 2010-05-16 10:49:58 -0400 | [diff] [blame] | 3273 | spin_lock(&root->orphan_lock); |
Josef Bacik | 8a35d95 | 2012-05-23 14:26:42 -0400 | [diff] [blame] | 3274 | if (test_and_clear_bit(BTRFS_INODE_HAS_ORPHAN_ITEM, |
| 3275 | &BTRFS_I(inode)->runtime_flags)) |
Yan, Zheng | d68fc57 | 2010-05-16 10:49:58 -0400 | [diff] [blame] | 3276 | delete_item = 1; |
Josef Bacik | 7b12876 | 2008-07-24 12:17:14 -0400 | [diff] [blame] | 3277 | |
Josef Bacik | 72ac3c0 | 2012-05-23 14:13:11 -0400 | [diff] [blame] | 3278 | if (test_and_clear_bit(BTRFS_INODE_ORPHAN_META_RESERVED, |
| 3279 | &BTRFS_I(inode)->runtime_flags)) |
Yan, Zheng | d68fc57 | 2010-05-16 10:49:58 -0400 | [diff] [blame] | 3280 | release_rsv = 1; |
Yan, Zheng | d68fc57 | 2010-05-16 10:49:58 -0400 | [diff] [blame] | 3281 | spin_unlock(&root->orphan_lock); |
| 3282 | |
Filipe David Borba Manana | 703c88e | 2013-09-22 21:54:55 +0100 | [diff] [blame] | 3283 | if (delete_item) { |
Josef Bacik | 8a35d95 | 2012-05-23 14:26:42 -0400 | [diff] [blame] | 3284 | atomic_dec(&root->orphan_inodes); |
Filipe David Borba Manana | 703c88e | 2013-09-22 21:54:55 +0100 | [diff] [blame] | 3285 | if (trans) |
| 3286 | ret = btrfs_del_orphan_item(trans, root, |
| 3287 | btrfs_ino(inode)); |
Josef Bacik | 8a35d95 | 2012-05-23 14:26:42 -0400 | [diff] [blame] | 3288 | } |
Josef Bacik | 7b12876 | 2008-07-24 12:17:14 -0400 | [diff] [blame] | 3289 | |
Filipe David Borba Manana | 703c88e | 2013-09-22 21:54:55 +0100 | [diff] [blame] | 3290 | if (release_rsv) |
| 3291 | btrfs_orphan_release_metadata(inode); |
| 3292 | |
Josef Bacik | 4ef31a4 | 2013-08-13 14:10:08 -0400 | [diff] [blame] | 3293 | return ret; |
Josef Bacik | 7b12876 | 2008-07-24 12:17:14 -0400 | [diff] [blame] | 3294 | } |
| 3295 | |
| 3296 | /* |
| 3297 | * this cleans up any orphans that may be left on the list from the last use |
| 3298 | * of this root. |
| 3299 | */ |
Josef Bacik | 66b4ffd | 2011-01-31 16:22:42 -0500 | [diff] [blame] | 3300 | int btrfs_orphan_cleanup(struct btrfs_root *root) |
Josef Bacik | 7b12876 | 2008-07-24 12:17:14 -0400 | [diff] [blame] | 3301 | { |
| 3302 | struct btrfs_path *path; |
| 3303 | struct extent_buffer *leaf; |
Josef Bacik | 7b12876 | 2008-07-24 12:17:14 -0400 | [diff] [blame] | 3304 | struct btrfs_key key, found_key; |
| 3305 | struct btrfs_trans_handle *trans; |
| 3306 | struct inode *inode; |
Josef Bacik | 8f6d7f4 | 2011-09-26 15:55:20 -0400 | [diff] [blame] | 3307 | u64 last_objectid = 0; |
Josef Bacik | 7b12876 | 2008-07-24 12:17:14 -0400 | [diff] [blame] | 3308 | int ret = 0, nr_unlink = 0, nr_truncate = 0; |
| 3309 | |
Yan, Zheng | d68fc57 | 2010-05-16 10:49:58 -0400 | [diff] [blame] | 3310 | if (cmpxchg(&root->orphan_cleanup_state, 0, ORPHAN_CLEANUP_STARTED)) |
Josef Bacik | 66b4ffd | 2011-01-31 16:22:42 -0500 | [diff] [blame] | 3311 | return 0; |
Yan, Zheng | c71bf09 | 2009-11-12 09:34:40 +0000 | [diff] [blame] | 3312 | |
| 3313 | path = btrfs_alloc_path(); |
Josef Bacik | 66b4ffd | 2011-01-31 16:22:42 -0500 | [diff] [blame] | 3314 | if (!path) { |
| 3315 | ret = -ENOMEM; |
| 3316 | goto out; |
| 3317 | } |
Josef Bacik | 7b12876 | 2008-07-24 12:17:14 -0400 | [diff] [blame] | 3318 | path->reada = -1; |
| 3319 | |
| 3320 | key.objectid = BTRFS_ORPHAN_OBJECTID; |
David Sterba | 962a298 | 2014-06-04 18:41:45 +0200 | [diff] [blame] | 3321 | key.type = BTRFS_ORPHAN_ITEM_KEY; |
Josef Bacik | 7b12876 | 2008-07-24 12:17:14 -0400 | [diff] [blame] | 3322 | key.offset = (u64)-1; |
| 3323 | |
Josef Bacik | 7b12876 | 2008-07-24 12:17:14 -0400 | [diff] [blame] | 3324 | while (1) { |
| 3325 | ret = btrfs_search_slot(NULL, root, &key, path, 0, 0); |
Josef Bacik | 66b4ffd | 2011-01-31 16:22:42 -0500 | [diff] [blame] | 3326 | if (ret < 0) |
| 3327 | goto out; |
Josef Bacik | 7b12876 | 2008-07-24 12:17:14 -0400 | [diff] [blame] | 3328 | |
| 3329 | /* |
| 3330 | * 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] | 3331 | * 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] | 3332 | * find the key and see if we have stuff that matches |
| 3333 | */ |
| 3334 | if (ret > 0) { |
Josef Bacik | 66b4ffd | 2011-01-31 16:22:42 -0500 | [diff] [blame] | 3335 | ret = 0; |
Josef Bacik | 7b12876 | 2008-07-24 12:17:14 -0400 | [diff] [blame] | 3336 | if (path->slots[0] == 0) |
| 3337 | break; |
| 3338 | path->slots[0]--; |
| 3339 | } |
| 3340 | |
| 3341 | /* pull out the item */ |
| 3342 | leaf = path->nodes[0]; |
Josef Bacik | 7b12876 | 2008-07-24 12:17:14 -0400 | [diff] [blame] | 3343 | btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]); |
| 3344 | |
| 3345 | /* make sure the item matches what we want */ |
| 3346 | if (found_key.objectid != BTRFS_ORPHAN_OBJECTID) |
| 3347 | break; |
David Sterba | 962a298 | 2014-06-04 18:41:45 +0200 | [diff] [blame] | 3348 | if (found_key.type != BTRFS_ORPHAN_ITEM_KEY) |
Josef Bacik | 7b12876 | 2008-07-24 12:17:14 -0400 | [diff] [blame] | 3349 | break; |
| 3350 | |
| 3351 | /* release the path since we're done with it */ |
David Sterba | b3b4aa7 | 2011-04-21 01:20:15 +0200 | [diff] [blame] | 3352 | btrfs_release_path(path); |
Josef Bacik | 7b12876 | 2008-07-24 12:17:14 -0400 | [diff] [blame] | 3353 | |
| 3354 | /* |
| 3355 | * this is where we are basically btrfs_lookup, without the |
| 3356 | * crossing root thing. we store the inode number in the |
| 3357 | * offset of the orphan item. |
| 3358 | */ |
Josef Bacik | 8f6d7f4 | 2011-09-26 15:55:20 -0400 | [diff] [blame] | 3359 | |
| 3360 | if (found_key.offset == last_objectid) { |
Simon Kirby | c2cf52e | 2013-03-19 22:41:23 +0000 | [diff] [blame] | 3361 | btrfs_err(root->fs_info, |
| 3362 | "Error removing orphan entry, stopping orphan cleanup"); |
Josef Bacik | 8f6d7f4 | 2011-09-26 15:55:20 -0400 | [diff] [blame] | 3363 | ret = -EINVAL; |
| 3364 | goto out; |
| 3365 | } |
| 3366 | |
| 3367 | last_objectid = found_key.offset; |
| 3368 | |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 3369 | found_key.objectid = found_key.offset; |
| 3370 | found_key.type = BTRFS_INODE_ITEM_KEY; |
| 3371 | found_key.offset = 0; |
Josef Bacik | 73f7341 | 2009-12-04 17:38:27 +0000 | [diff] [blame] | 3372 | inode = btrfs_iget(root->fs_info->sb, &found_key, root, NULL); |
Rusty Russell | 8c6ffba | 2013-07-15 11:20:32 +0930 | [diff] [blame] | 3373 | ret = PTR_ERR_OR_ZERO(inode); |
Josef Bacik | a8c9e57 | 2011-09-21 16:55:59 -0400 | [diff] [blame] | 3374 | if (ret && ret != -ESTALE) |
Josef Bacik | 66b4ffd | 2011-01-31 16:22:42 -0500 | [diff] [blame] | 3375 | goto out; |
Josef Bacik | a8c9e57 | 2011-09-21 16:55:59 -0400 | [diff] [blame] | 3376 | |
Arne Jansen | f8e9e0b | 2011-12-14 20:12:02 -0500 | [diff] [blame] | 3377 | if (ret == -ESTALE && root == root->fs_info->tree_root) { |
| 3378 | struct btrfs_root *dead_root; |
| 3379 | struct btrfs_fs_info *fs_info = root->fs_info; |
| 3380 | int is_dead_root = 0; |
| 3381 | |
| 3382 | /* |
| 3383 | * this is an orphan in the tree root. Currently these |
| 3384 | * could come from 2 sources: |
| 3385 | * a) a snapshot deletion in progress |
| 3386 | * b) a free space cache inode |
| 3387 | * We need to distinguish those two, as the snapshot |
| 3388 | * orphan must not get deleted. |
| 3389 | * find_dead_roots already ran before us, so if this |
| 3390 | * is a snapshot deletion, we should find the root |
| 3391 | * in the dead_roots list |
| 3392 | */ |
| 3393 | spin_lock(&fs_info->trans_lock); |
| 3394 | list_for_each_entry(dead_root, &fs_info->dead_roots, |
| 3395 | root_list) { |
| 3396 | if (dead_root->root_key.objectid == |
| 3397 | found_key.objectid) { |
| 3398 | is_dead_root = 1; |
| 3399 | break; |
| 3400 | } |
| 3401 | } |
| 3402 | spin_unlock(&fs_info->trans_lock); |
| 3403 | if (is_dead_root) { |
| 3404 | /* prevent this orphan from being found again */ |
| 3405 | key.offset = found_key.objectid - 1; |
| 3406 | continue; |
| 3407 | } |
| 3408 | } |
Josef Bacik | a8c9e57 | 2011-09-21 16:55:59 -0400 | [diff] [blame] | 3409 | /* |
| 3410 | * Inode is already gone but the orphan item is still there, |
| 3411 | * kill the orphan item. |
| 3412 | */ |
| 3413 | if (ret == -ESTALE) { |
| 3414 | trans = btrfs_start_transaction(root, 1); |
| 3415 | if (IS_ERR(trans)) { |
| 3416 | ret = PTR_ERR(trans); |
| 3417 | goto out; |
| 3418 | } |
Simon Kirby | c2cf52e | 2013-03-19 22:41:23 +0000 | [diff] [blame] | 3419 | btrfs_debug(root->fs_info, "auto deleting %Lu", |
| 3420 | found_key.objectid); |
Josef Bacik | a8c9e57 | 2011-09-21 16:55:59 -0400 | [diff] [blame] | 3421 | ret = btrfs_del_orphan_item(trans, root, |
| 3422 | found_key.objectid); |
Josef Bacik | a8c9e57 | 2011-09-21 16:55:59 -0400 | [diff] [blame] | 3423 | btrfs_end_transaction(trans, root); |
Josef Bacik | 4ef31a4 | 2013-08-13 14:10:08 -0400 | [diff] [blame] | 3424 | if (ret) |
| 3425 | goto out; |
Josef Bacik | a8c9e57 | 2011-09-21 16:55:59 -0400 | [diff] [blame] | 3426 | continue; |
Josef Bacik | 66b4ffd | 2011-01-31 16:22:42 -0500 | [diff] [blame] | 3427 | } |
Josef Bacik | 7b12876 | 2008-07-24 12:17:14 -0400 | [diff] [blame] | 3428 | |
Josef Bacik | 7b12876 | 2008-07-24 12:17:14 -0400 | [diff] [blame] | 3429 | /* |
| 3430 | * add this inode to the orphan list so btrfs_orphan_del does |
| 3431 | * the proper thing when we hit it |
| 3432 | */ |
Josef Bacik | 8a35d95 | 2012-05-23 14:26:42 -0400 | [diff] [blame] | 3433 | set_bit(BTRFS_INODE_HAS_ORPHAN_ITEM, |
| 3434 | &BTRFS_I(inode)->runtime_flags); |
Josef Bacik | 925396e | 2013-02-01 15:57:47 -0500 | [diff] [blame] | 3435 | atomic_inc(&root->orphan_inodes); |
Josef Bacik | 7b12876 | 2008-07-24 12:17:14 -0400 | [diff] [blame] | 3436 | |
Josef Bacik | 7b12876 | 2008-07-24 12:17:14 -0400 | [diff] [blame] | 3437 | /* if we have links, this was a truncate, lets do that */ |
| 3438 | if (inode->i_nlink) { |
Dulshani Gunawardhana | fae7f21 | 2013-10-31 10:30:08 +0530 | [diff] [blame] | 3439 | if (WARN_ON(!S_ISREG(inode->i_mode))) { |
Josef Bacik | a41ad39 | 2011-01-31 15:30:16 -0500 | [diff] [blame] | 3440 | iput(inode); |
| 3441 | continue; |
| 3442 | } |
Josef Bacik | 7b12876 | 2008-07-24 12:17:14 -0400 | [diff] [blame] | 3443 | nr_truncate++; |
Josef Bacik | f3fe820 | 2013-01-07 17:03:21 -0500 | [diff] [blame] | 3444 | |
| 3445 | /* 1 for the orphan item deletion. */ |
| 3446 | trans = btrfs_start_transaction(root, 1); |
| 3447 | if (IS_ERR(trans)) { |
Josef Bacik | c69b26b | 2013-06-03 16:51:23 -0400 | [diff] [blame] | 3448 | iput(inode); |
Josef Bacik | f3fe820 | 2013-01-07 17:03:21 -0500 | [diff] [blame] | 3449 | ret = PTR_ERR(trans); |
| 3450 | goto out; |
| 3451 | } |
| 3452 | ret = btrfs_orphan_add(trans, inode); |
| 3453 | btrfs_end_transaction(trans, root); |
Josef Bacik | c69b26b | 2013-06-03 16:51:23 -0400 | [diff] [blame] | 3454 | if (ret) { |
| 3455 | iput(inode); |
Josef Bacik | f3fe820 | 2013-01-07 17:03:21 -0500 | [diff] [blame] | 3456 | goto out; |
Josef Bacik | c69b26b | 2013-06-03 16:51:23 -0400 | [diff] [blame] | 3457 | } |
Josef Bacik | f3fe820 | 2013-01-07 17:03:21 -0500 | [diff] [blame] | 3458 | |
Josef Bacik | 66b4ffd | 2011-01-31 16:22:42 -0500 | [diff] [blame] | 3459 | ret = btrfs_truncate(inode); |
Josef Bacik | 4a7d0f6 | 2013-02-07 16:27:28 -0500 | [diff] [blame] | 3460 | if (ret) |
| 3461 | btrfs_orphan_del(NULL, inode); |
Josef Bacik | 7b12876 | 2008-07-24 12:17:14 -0400 | [diff] [blame] | 3462 | } else { |
| 3463 | nr_unlink++; |
| 3464 | } |
| 3465 | |
| 3466 | /* this will do delete_inode and everything for us */ |
| 3467 | iput(inode); |
Josef Bacik | 66b4ffd | 2011-01-31 16:22:42 -0500 | [diff] [blame] | 3468 | if (ret) |
| 3469 | goto out; |
Josef Bacik | 7b12876 | 2008-07-24 12:17:14 -0400 | [diff] [blame] | 3470 | } |
Miao Xie | 3254c87 | 2011-11-10 20:45:05 -0500 | [diff] [blame] | 3471 | /* release the path since we're done with it */ |
| 3472 | btrfs_release_path(path); |
| 3473 | |
Yan, Zheng | d68fc57 | 2010-05-16 10:49:58 -0400 | [diff] [blame] | 3474 | root->orphan_cleanup_state = ORPHAN_CLEANUP_DONE; |
| 3475 | |
| 3476 | if (root->orphan_block_rsv) |
| 3477 | btrfs_block_rsv_release(root, root->orphan_block_rsv, |
| 3478 | (u64)-1); |
| 3479 | |
Miao Xie | 27cdeb7 | 2014-04-02 19:51:05 +0800 | [diff] [blame] | 3480 | if (root->orphan_block_rsv || |
| 3481 | test_bit(BTRFS_ROOT_ORPHAN_ITEM_INSERTED, &root->state)) { |
Josef Bacik | 7a7eaa4 | 2011-04-13 12:54:33 -0400 | [diff] [blame] | 3482 | trans = btrfs_join_transaction(root); |
Josef Bacik | 66b4ffd | 2011-01-31 16:22:42 -0500 | [diff] [blame] | 3483 | if (!IS_ERR(trans)) |
| 3484 | btrfs_end_transaction(trans, root); |
Yan, Zheng | d68fc57 | 2010-05-16 10:49:58 -0400 | [diff] [blame] | 3485 | } |
Josef Bacik | 7b12876 | 2008-07-24 12:17:14 -0400 | [diff] [blame] | 3486 | |
| 3487 | if (nr_unlink) |
David Sterba | 4884b47 | 2013-03-20 13:31:27 +0000 | [diff] [blame] | 3488 | btrfs_debug(root->fs_info, "unlinked %d orphans", nr_unlink); |
Josef Bacik | 7b12876 | 2008-07-24 12:17:14 -0400 | [diff] [blame] | 3489 | if (nr_truncate) |
David Sterba | 4884b47 | 2013-03-20 13:31:27 +0000 | [diff] [blame] | 3490 | btrfs_debug(root->fs_info, "truncated %d orphans", nr_truncate); |
Josef Bacik | 66b4ffd | 2011-01-31 16:22:42 -0500 | [diff] [blame] | 3491 | |
| 3492 | out: |
| 3493 | if (ret) |
David Sterba | 68b663d | 2014-12-19 18:38:37 +0100 | [diff] [blame] | 3494 | btrfs_err(root->fs_info, |
Simon Kirby | c2cf52e | 2013-03-19 22:41:23 +0000 | [diff] [blame] | 3495 | "could not do orphan cleanup %d", ret); |
Josef Bacik | 66b4ffd | 2011-01-31 16:22:42 -0500 | [diff] [blame] | 3496 | btrfs_free_path(path); |
| 3497 | return ret; |
Josef Bacik | 7b12876 | 2008-07-24 12:17:14 -0400 | [diff] [blame] | 3498 | } |
| 3499 | |
Chris Mason | d352ac6 | 2008-09-29 15:18:18 -0400 | [diff] [blame] | 3500 | /* |
Chris Mason | 46a53cc | 2009-04-27 11:47:50 -0400 | [diff] [blame] | 3501 | * very simple check to peek ahead in the leaf looking for xattrs. If we |
| 3502 | * don't find any xattrs, we know there can't be any acls. |
| 3503 | * |
| 3504 | * slot is the slot the inode is in, objectid is the objectid of the inode |
| 3505 | */ |
| 3506 | 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] | 3507 | int slot, u64 objectid, |
| 3508 | int *first_xattr_slot) |
Chris Mason | 46a53cc | 2009-04-27 11:47:50 -0400 | [diff] [blame] | 3509 | { |
| 3510 | u32 nritems = btrfs_header_nritems(leaf); |
| 3511 | struct btrfs_key found_key; |
Josef Bacik | f23b5a5 | 2013-06-19 10:16:26 -0400 | [diff] [blame] | 3512 | static u64 xattr_access = 0; |
| 3513 | static u64 xattr_default = 0; |
Chris Mason | 46a53cc | 2009-04-27 11:47:50 -0400 | [diff] [blame] | 3514 | int scanned = 0; |
| 3515 | |
Josef Bacik | f23b5a5 | 2013-06-19 10:16:26 -0400 | [diff] [blame] | 3516 | if (!xattr_access) { |
| 3517 | xattr_access = btrfs_name_hash(POSIX_ACL_XATTR_ACCESS, |
| 3518 | strlen(POSIX_ACL_XATTR_ACCESS)); |
| 3519 | xattr_default = btrfs_name_hash(POSIX_ACL_XATTR_DEFAULT, |
| 3520 | strlen(POSIX_ACL_XATTR_DEFAULT)); |
| 3521 | } |
| 3522 | |
Chris Mason | 46a53cc | 2009-04-27 11:47:50 -0400 | [diff] [blame] | 3523 | slot++; |
Filipe David Borba Manana | 6354192 | 2014-01-07 11:47:46 +0000 | [diff] [blame] | 3524 | *first_xattr_slot = -1; |
Chris Mason | 46a53cc | 2009-04-27 11:47:50 -0400 | [diff] [blame] | 3525 | while (slot < nritems) { |
| 3526 | btrfs_item_key_to_cpu(leaf, &found_key, slot); |
| 3527 | |
| 3528 | /* we found a different objectid, there must not be acls */ |
| 3529 | if (found_key.objectid != objectid) |
| 3530 | return 0; |
| 3531 | |
| 3532 | /* we found an xattr, assume we've got an acl */ |
Josef Bacik | f23b5a5 | 2013-06-19 10:16:26 -0400 | [diff] [blame] | 3533 | if (found_key.type == BTRFS_XATTR_ITEM_KEY) { |
Filipe David Borba Manana | 6354192 | 2014-01-07 11:47:46 +0000 | [diff] [blame] | 3534 | if (*first_xattr_slot == -1) |
| 3535 | *first_xattr_slot = slot; |
Josef Bacik | f23b5a5 | 2013-06-19 10:16:26 -0400 | [diff] [blame] | 3536 | if (found_key.offset == xattr_access || |
| 3537 | found_key.offset == xattr_default) |
| 3538 | return 1; |
| 3539 | } |
Chris Mason | 46a53cc | 2009-04-27 11:47:50 -0400 | [diff] [blame] | 3540 | |
| 3541 | /* |
| 3542 | * we found a key greater than an xattr key, there can't |
| 3543 | * be any acls later on |
| 3544 | */ |
| 3545 | if (found_key.type > BTRFS_XATTR_ITEM_KEY) |
| 3546 | return 0; |
| 3547 | |
| 3548 | slot++; |
| 3549 | scanned++; |
| 3550 | |
| 3551 | /* |
| 3552 | * it goes inode, inode backrefs, xattrs, extents, |
| 3553 | * so if there are a ton of hard links to an inode there can |
| 3554 | * be a lot of backrefs. Don't waste time searching too hard, |
| 3555 | * this is just an optimization |
| 3556 | */ |
| 3557 | if (scanned >= 8) |
| 3558 | break; |
| 3559 | } |
| 3560 | /* we hit the end of the leaf before we found an xattr or |
| 3561 | * something larger than an xattr. We have to assume the inode |
| 3562 | * has acls |
| 3563 | */ |
Filipe David Borba Manana | 6354192 | 2014-01-07 11:47:46 +0000 | [diff] [blame] | 3564 | if (*first_xattr_slot == -1) |
| 3565 | *first_xattr_slot = slot; |
Chris Mason | 46a53cc | 2009-04-27 11:47:50 -0400 | [diff] [blame] | 3566 | return 1; |
| 3567 | } |
| 3568 | |
| 3569 | /* |
Chris Mason | d352ac6 | 2008-09-29 15:18:18 -0400 | [diff] [blame] | 3570 | * read an inode from the btree into the in-memory inode |
| 3571 | */ |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 3572 | static void btrfs_read_locked_inode(struct inode *inode) |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3573 | { |
| 3574 | struct btrfs_path *path; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 3575 | struct extent_buffer *leaf; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3576 | struct btrfs_inode_item *inode_item; |
| 3577 | struct btrfs_root *root = BTRFS_I(inode)->root; |
| 3578 | struct btrfs_key location; |
Miao Xie | 67de117 | 2013-12-26 13:07:06 +0800 | [diff] [blame] | 3579 | unsigned long ptr; |
Chris Mason | 46a53cc | 2009-04-27 11:47:50 -0400 | [diff] [blame] | 3580 | int maybe_acls; |
Josef Bacik | 618e21d | 2007-07-11 10:18:17 -0400 | [diff] [blame] | 3581 | u32 rdev; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3582 | int ret; |
Miao Xie | 2f7e33d | 2011-06-23 07:27:13 +0000 | [diff] [blame] | 3583 | bool filled = false; |
Filipe David Borba Manana | 6354192 | 2014-01-07 11:47:46 +0000 | [diff] [blame] | 3584 | int first_xattr_slot; |
Miao Xie | 2f7e33d | 2011-06-23 07:27:13 +0000 | [diff] [blame] | 3585 | |
| 3586 | ret = btrfs_fill_inode(inode, &rdev); |
| 3587 | if (!ret) |
| 3588 | filled = true; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3589 | |
| 3590 | path = btrfs_alloc_path(); |
Mark Fasheh | 1748f84 | 2011-07-12 11:25:31 -0700 | [diff] [blame] | 3591 | if (!path) |
| 3592 | goto make_bad; |
| 3593 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3594 | memcpy(&location, &BTRFS_I(inode)->location, sizeof(location)); |
Chris Mason | dc17ff8 | 2008-01-08 15:46:30 -0500 | [diff] [blame] | 3595 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3596 | ret = btrfs_lookup_inode(NULL, root, path, &location, 0); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 3597 | if (ret) |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3598 | goto make_bad; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3599 | |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 3600 | leaf = path->nodes[0]; |
Miao Xie | 2f7e33d | 2011-06-23 07:27:13 +0000 | [diff] [blame] | 3601 | |
| 3602 | if (filled) |
Miao Xie | 67de117 | 2013-12-26 13:07:06 +0800 | [diff] [blame] | 3603 | goto cache_index; |
Miao Xie | 2f7e33d | 2011-06-23 07:27:13 +0000 | [diff] [blame] | 3604 | |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 3605 | inode_item = btrfs_item_ptr(leaf, path->slots[0], |
| 3606 | struct btrfs_inode_item); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 3607 | inode->i_mode = btrfs_inode_mode(leaf, inode_item); |
Miklos Szeredi | bfe8684 | 2011-10-28 14:13:29 +0200 | [diff] [blame] | 3608 | set_nlink(inode, btrfs_inode_nlink(leaf, inode_item)); |
Eric W. Biederman | 2f2f43d | 2012-02-10 11:05:07 -0800 | [diff] [blame] | 3609 | i_uid_write(inode, btrfs_inode_uid(leaf, inode_item)); |
| 3610 | i_gid_write(inode, btrfs_inode_gid(leaf, inode_item)); |
Chris Mason | dbe674a | 2008-07-17 12:54:05 -0400 | [diff] [blame] | 3611 | btrfs_i_size_write(inode, btrfs_inode_size(leaf, inode_item)); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 3612 | |
David Sterba | a937b97 | 2014-12-12 17:39:12 +0100 | [diff] [blame] | 3613 | inode->i_atime.tv_sec = btrfs_timespec_sec(leaf, &inode_item->atime); |
| 3614 | inode->i_atime.tv_nsec = btrfs_timespec_nsec(leaf, &inode_item->atime); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 3615 | |
David Sterba | a937b97 | 2014-12-12 17:39:12 +0100 | [diff] [blame] | 3616 | inode->i_mtime.tv_sec = btrfs_timespec_sec(leaf, &inode_item->mtime); |
| 3617 | inode->i_mtime.tv_nsec = btrfs_timespec_nsec(leaf, &inode_item->mtime); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 3618 | |
David Sterba | a937b97 | 2014-12-12 17:39:12 +0100 | [diff] [blame] | 3619 | inode->i_ctime.tv_sec = btrfs_timespec_sec(leaf, &inode_item->ctime); |
| 3620 | inode->i_ctime.tv_nsec = btrfs_timespec_nsec(leaf, &inode_item->ctime); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 3621 | |
chandan r | 9cc97d6 | 2012-07-04 12:48:07 +0530 | [diff] [blame] | 3622 | BTRFS_I(inode)->i_otime.tv_sec = |
| 3623 | btrfs_timespec_sec(leaf, &inode_item->otime); |
| 3624 | BTRFS_I(inode)->i_otime.tv_nsec = |
| 3625 | btrfs_timespec_nsec(leaf, &inode_item->otime); |
| 3626 | |
Yan Zheng | a76a3cd | 2008-10-09 11:46:29 -0400 | [diff] [blame] | 3627 | inode_set_bytes(inode, btrfs_inode_nbytes(leaf, inode_item)); |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 3628 | BTRFS_I(inode)->generation = btrfs_inode_generation(leaf, inode_item); |
Josef Bacik | 5dc562c | 2012-08-17 13:14:17 -0400 | [diff] [blame] | 3629 | BTRFS_I(inode)->last_trans = btrfs_inode_transid(leaf, inode_item); |
| 3630 | |
| 3631 | /* |
| 3632 | * If we were modified in the current generation and evicted from memory |
| 3633 | * and then re-read we need to do a full sync since we don't have any |
| 3634 | * idea about which extents were modified before we were evicted from |
| 3635 | * cache. |
| 3636 | */ |
| 3637 | if (BTRFS_I(inode)->last_trans == root->fs_info->generation) |
| 3638 | set_bit(BTRFS_INODE_NEEDS_FULL_SYNC, |
| 3639 | &BTRFS_I(inode)->runtime_flags); |
| 3640 | |
Josef Bacik | 0c4d2d9 | 2012-04-05 15:03:02 -0400 | [diff] [blame] | 3641 | inode->i_version = btrfs_inode_sequence(leaf, inode_item); |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 3642 | inode->i_generation = BTRFS_I(inode)->generation; |
Josef Bacik | 618e21d | 2007-07-11 10:18:17 -0400 | [diff] [blame] | 3643 | inode->i_rdev = 0; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 3644 | rdev = btrfs_inode_rdev(leaf, inode_item); |
| 3645 | |
Josef Bacik | aec7477 | 2008-07-24 12:12:38 -0400 | [diff] [blame] | 3646 | BTRFS_I(inode)->index_cnt = (u64)-1; |
Yan Zheng | d2fb343 | 2008-12-11 16:30:39 -0500 | [diff] [blame] | 3647 | BTRFS_I(inode)->flags = btrfs_inode_flags(leaf, inode_item); |
Miao Xie | 67de117 | 2013-12-26 13:07:06 +0800 | [diff] [blame] | 3648 | |
| 3649 | cache_index: |
| 3650 | path->slots[0]++; |
| 3651 | if (inode->i_nlink != 1 || |
| 3652 | path->slots[0] >= btrfs_header_nritems(leaf)) |
| 3653 | goto cache_acl; |
| 3654 | |
| 3655 | btrfs_item_key_to_cpu(leaf, &location, path->slots[0]); |
| 3656 | if (location.objectid != btrfs_ino(inode)) |
| 3657 | goto cache_acl; |
| 3658 | |
| 3659 | ptr = btrfs_item_ptr_offset(leaf, path->slots[0]); |
| 3660 | if (location.type == BTRFS_INODE_REF_KEY) { |
| 3661 | struct btrfs_inode_ref *ref; |
| 3662 | |
| 3663 | ref = (struct btrfs_inode_ref *)ptr; |
| 3664 | BTRFS_I(inode)->dir_index = btrfs_inode_ref_index(leaf, ref); |
| 3665 | } else if (location.type == BTRFS_INODE_EXTREF_KEY) { |
| 3666 | struct btrfs_inode_extref *extref; |
| 3667 | |
| 3668 | extref = (struct btrfs_inode_extref *)ptr; |
| 3669 | BTRFS_I(inode)->dir_index = btrfs_inode_extref_index(leaf, |
| 3670 | extref); |
| 3671 | } |
Miao Xie | 2f7e33d | 2011-06-23 07:27:13 +0000 | [diff] [blame] | 3672 | cache_acl: |
Chris Mason | 46a53cc | 2009-04-27 11:47:50 -0400 | [diff] [blame] | 3673 | /* |
| 3674 | * try to precache a NULL acl entry for files that don't have |
| 3675 | * any xattrs or acls |
| 3676 | */ |
Li Zefan | 33345d01 | 2011-04-20 10:31:50 +0800 | [diff] [blame] | 3677 | maybe_acls = acls_after_inode_item(leaf, path->slots[0], |
Filipe David Borba Manana | 6354192 | 2014-01-07 11:47:46 +0000 | [diff] [blame] | 3678 | btrfs_ino(inode), &first_xattr_slot); |
| 3679 | if (first_xattr_slot != -1) { |
| 3680 | path->slots[0] = first_xattr_slot; |
| 3681 | ret = btrfs_load_inode_props(inode, path); |
| 3682 | if (ret) |
| 3683 | btrfs_err(root->fs_info, |
David Sterba | 351fd35 | 2014-05-15 16:48:20 +0200 | [diff] [blame] | 3684 | "error loading props for ino %llu (root %llu): %d", |
Filipe David Borba Manana | 6354192 | 2014-01-07 11:47:46 +0000 | [diff] [blame] | 3685 | btrfs_ino(inode), |
| 3686 | root->root_key.objectid, ret); |
| 3687 | } |
| 3688 | btrfs_free_path(path); |
| 3689 | |
Al Viro | 72c0490 | 2009-06-24 16:58:48 -0400 | [diff] [blame] | 3690 | if (!maybe_acls) |
| 3691 | cache_no_acl(inode); |
Chris Mason | 46a53cc | 2009-04-27 11:47:50 -0400 | [diff] [blame] | 3692 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3693 | switch (inode->i_mode & S_IFMT) { |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3694 | case S_IFREG: |
| 3695 | inode->i_mapping->a_ops = &btrfs_aops; |
Chris Mason | 0416008 | 2008-03-26 10:28:07 -0400 | [diff] [blame] | 3696 | inode->i_mapping->backing_dev_info = &root->fs_info->bdi; |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 3697 | BTRFS_I(inode)->io_tree.ops = &btrfs_extent_io_ops; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3698 | inode->i_fop = &btrfs_file_operations; |
| 3699 | inode->i_op = &btrfs_file_inode_operations; |
| 3700 | break; |
| 3701 | case S_IFDIR: |
| 3702 | inode->i_fop = &btrfs_dir_file_operations; |
| 3703 | if (root == root->fs_info->tree_root) |
| 3704 | inode->i_op = &btrfs_dir_ro_inode_operations; |
| 3705 | else |
| 3706 | inode->i_op = &btrfs_dir_inode_operations; |
| 3707 | break; |
| 3708 | case S_IFLNK: |
| 3709 | inode->i_op = &btrfs_symlink_inode_operations; |
| 3710 | inode->i_mapping->a_ops = &btrfs_symlink_aops; |
Chris Mason | 0416008 | 2008-03-26 10:28:07 -0400 | [diff] [blame] | 3711 | inode->i_mapping->backing_dev_info = &root->fs_info->bdi; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3712 | break; |
Josef Bacik | 618e21d | 2007-07-11 10:18:17 -0400 | [diff] [blame] | 3713 | default: |
Jim Owens | 0279b4c | 2009-02-04 09:29:13 -0500 | [diff] [blame] | 3714 | inode->i_op = &btrfs_special_inode_operations; |
Josef Bacik | 618e21d | 2007-07-11 10:18:17 -0400 | [diff] [blame] | 3715 | init_special_inode(inode, inode->i_mode, rdev); |
| 3716 | break; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3717 | } |
Christoph Hellwig | 6cbff00 | 2009-04-17 10:37:41 +0200 | [diff] [blame] | 3718 | |
| 3719 | btrfs_update_iflags(inode); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3720 | return; |
| 3721 | |
| 3722 | make_bad: |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3723 | btrfs_free_path(path); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3724 | make_bad_inode(inode); |
| 3725 | } |
| 3726 | |
Chris Mason | d352ac6 | 2008-09-29 15:18:18 -0400 | [diff] [blame] | 3727 | /* |
| 3728 | * given a leaf and an inode, copy the inode fields into the leaf |
| 3729 | */ |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 3730 | static void fill_inode_item(struct btrfs_trans_handle *trans, |
| 3731 | struct extent_buffer *leaf, |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 3732 | struct btrfs_inode_item *item, |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3733 | struct inode *inode) |
| 3734 | { |
Liu Bo | 51fab69 | 2012-12-27 09:01:21 +0000 | [diff] [blame] | 3735 | struct btrfs_map_token token; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 3736 | |
Liu Bo | 51fab69 | 2012-12-27 09:01:21 +0000 | [diff] [blame] | 3737 | btrfs_init_map_token(&token); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 3738 | |
Liu Bo | 51fab69 | 2012-12-27 09:01:21 +0000 | [diff] [blame] | 3739 | btrfs_set_token_inode_uid(leaf, item, i_uid_read(inode), &token); |
| 3740 | btrfs_set_token_inode_gid(leaf, item, i_gid_read(inode), &token); |
| 3741 | btrfs_set_token_inode_size(leaf, item, BTRFS_I(inode)->disk_i_size, |
| 3742 | &token); |
| 3743 | btrfs_set_token_inode_mode(leaf, item, inode->i_mode, &token); |
| 3744 | btrfs_set_token_inode_nlink(leaf, item, inode->i_nlink, &token); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 3745 | |
David Sterba | a937b97 | 2014-12-12 17:39:12 +0100 | [diff] [blame] | 3746 | btrfs_set_token_timespec_sec(leaf, &item->atime, |
Liu Bo | 51fab69 | 2012-12-27 09:01:21 +0000 | [diff] [blame] | 3747 | inode->i_atime.tv_sec, &token); |
David Sterba | a937b97 | 2014-12-12 17:39:12 +0100 | [diff] [blame] | 3748 | btrfs_set_token_timespec_nsec(leaf, &item->atime, |
Liu Bo | 51fab69 | 2012-12-27 09:01:21 +0000 | [diff] [blame] | 3749 | inode->i_atime.tv_nsec, &token); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 3750 | |
David Sterba | a937b97 | 2014-12-12 17:39:12 +0100 | [diff] [blame] | 3751 | btrfs_set_token_timespec_sec(leaf, &item->mtime, |
Liu Bo | 51fab69 | 2012-12-27 09:01:21 +0000 | [diff] [blame] | 3752 | inode->i_mtime.tv_sec, &token); |
David Sterba | a937b97 | 2014-12-12 17:39:12 +0100 | [diff] [blame] | 3753 | btrfs_set_token_timespec_nsec(leaf, &item->mtime, |
Liu Bo | 51fab69 | 2012-12-27 09:01:21 +0000 | [diff] [blame] | 3754 | inode->i_mtime.tv_nsec, &token); |
| 3755 | |
David Sterba | a937b97 | 2014-12-12 17:39:12 +0100 | [diff] [blame] | 3756 | btrfs_set_token_timespec_sec(leaf, &item->ctime, |
Liu Bo | 51fab69 | 2012-12-27 09:01:21 +0000 | [diff] [blame] | 3757 | inode->i_ctime.tv_sec, &token); |
David Sterba | a937b97 | 2014-12-12 17:39:12 +0100 | [diff] [blame] | 3758 | btrfs_set_token_timespec_nsec(leaf, &item->ctime, |
Liu Bo | 51fab69 | 2012-12-27 09:01:21 +0000 | [diff] [blame] | 3759 | inode->i_ctime.tv_nsec, &token); |
| 3760 | |
chandan r | 9cc97d6 | 2012-07-04 12:48:07 +0530 | [diff] [blame] | 3761 | btrfs_set_token_timespec_sec(leaf, &item->otime, |
| 3762 | BTRFS_I(inode)->i_otime.tv_sec, &token); |
| 3763 | btrfs_set_token_timespec_nsec(leaf, &item->otime, |
| 3764 | BTRFS_I(inode)->i_otime.tv_nsec, &token); |
| 3765 | |
Liu Bo | 51fab69 | 2012-12-27 09:01:21 +0000 | [diff] [blame] | 3766 | btrfs_set_token_inode_nbytes(leaf, item, inode_get_bytes(inode), |
| 3767 | &token); |
| 3768 | btrfs_set_token_inode_generation(leaf, item, BTRFS_I(inode)->generation, |
| 3769 | &token); |
| 3770 | btrfs_set_token_inode_sequence(leaf, item, inode->i_version, &token); |
| 3771 | btrfs_set_token_inode_transid(leaf, item, trans->transid, &token); |
| 3772 | btrfs_set_token_inode_rdev(leaf, item, inode->i_rdev, &token); |
| 3773 | btrfs_set_token_inode_flags(leaf, item, BTRFS_I(inode)->flags, &token); |
| 3774 | btrfs_set_token_inode_block_group(leaf, item, 0, &token); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3775 | } |
| 3776 | |
Chris Mason | d352ac6 | 2008-09-29 15:18:18 -0400 | [diff] [blame] | 3777 | /* |
| 3778 | * copy everything in the in-memory inode into the btree. |
| 3779 | */ |
Chris Mason | 2115133 | 2011-11-10 20:39:08 -0500 | [diff] [blame] | 3780 | static noinline int btrfs_update_inode_item(struct btrfs_trans_handle *trans, |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 3781 | struct btrfs_root *root, struct inode *inode) |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3782 | { |
| 3783 | struct btrfs_inode_item *inode_item; |
| 3784 | struct btrfs_path *path; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 3785 | struct extent_buffer *leaf; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3786 | int ret; |
| 3787 | |
| 3788 | path = btrfs_alloc_path(); |
Miao Xie | 16cdcec | 2011-04-22 18:12:22 +0800 | [diff] [blame] | 3789 | if (!path) |
| 3790 | return -ENOMEM; |
| 3791 | |
Chris Mason | b947343 | 2009-03-13 11:00:37 -0400 | [diff] [blame] | 3792 | path->leave_spinning = 1; |
Miao Xie | 16cdcec | 2011-04-22 18:12:22 +0800 | [diff] [blame] | 3793 | ret = btrfs_lookup_inode(trans, root, path, &BTRFS_I(inode)->location, |
| 3794 | 1); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3795 | if (ret) { |
| 3796 | if (ret > 0) |
| 3797 | ret = -ENOENT; |
| 3798 | goto failed; |
| 3799 | } |
| 3800 | |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 3801 | leaf = path->nodes[0]; |
| 3802 | inode_item = btrfs_item_ptr(leaf, path->slots[0], |
Miao Xie | 16cdcec | 2011-04-22 18:12:22 +0800 | [diff] [blame] | 3803 | struct btrfs_inode_item); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3804 | |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 3805 | fill_inode_item(trans, leaf, inode_item, inode); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 3806 | btrfs_mark_buffer_dirty(leaf); |
Josef Bacik | 15ee9bc | 2007-08-10 16:22:09 -0400 | [diff] [blame] | 3807 | btrfs_set_inode_last_trans(trans, inode); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3808 | ret = 0; |
| 3809 | failed: |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3810 | btrfs_free_path(path); |
| 3811 | return ret; |
| 3812 | } |
| 3813 | |
Chris Mason | d352ac6 | 2008-09-29 15:18:18 -0400 | [diff] [blame] | 3814 | /* |
Chris Mason | 2115133 | 2011-11-10 20:39:08 -0500 | [diff] [blame] | 3815 | * copy everything in the in-memory inode into the btree. |
| 3816 | */ |
| 3817 | noinline int btrfs_update_inode(struct btrfs_trans_handle *trans, |
| 3818 | struct btrfs_root *root, struct inode *inode) |
| 3819 | { |
| 3820 | int ret; |
| 3821 | |
| 3822 | /* |
| 3823 | * If the inode is a free space inode, we can deadlock during commit |
| 3824 | * if we put it into the delayed code. |
| 3825 | * |
| 3826 | * The data relocation inode should also be directly updated |
| 3827 | * without delay |
| 3828 | */ |
Liu Bo | 83eea1f | 2012-07-10 05:28:39 -0600 | [diff] [blame] | 3829 | if (!btrfs_is_free_space_inode(inode) |
Josef Bacik | 1d52c78 | 2014-09-18 11:30:44 -0400 | [diff] [blame] | 3830 | && root->root_key.objectid != BTRFS_DATA_RELOC_TREE_OBJECTID |
| 3831 | && !root->fs_info->log_root_recovering) { |
Alexander Block | 8ea05e3 | 2012-07-25 17:35:53 +0200 | [diff] [blame] | 3832 | btrfs_update_root_times(trans, root); |
| 3833 | |
Chris Mason | 2115133 | 2011-11-10 20:39:08 -0500 | [diff] [blame] | 3834 | ret = btrfs_delayed_update_inode(trans, root, inode); |
| 3835 | if (!ret) |
| 3836 | btrfs_set_inode_last_trans(trans, inode); |
| 3837 | return ret; |
| 3838 | } |
| 3839 | |
| 3840 | return btrfs_update_inode_item(trans, root, inode); |
| 3841 | } |
| 3842 | |
Josef Bacik | be6aef6 | 2012-10-22 15:43:12 -0400 | [diff] [blame] | 3843 | noinline int btrfs_update_inode_fallback(struct btrfs_trans_handle *trans, |
| 3844 | struct btrfs_root *root, |
| 3845 | struct inode *inode) |
Chris Mason | 2115133 | 2011-11-10 20:39:08 -0500 | [diff] [blame] | 3846 | { |
| 3847 | int ret; |
| 3848 | |
| 3849 | ret = btrfs_update_inode(trans, root, inode); |
| 3850 | if (ret == -ENOSPC) |
| 3851 | return btrfs_update_inode_item(trans, root, inode); |
| 3852 | return ret; |
| 3853 | } |
| 3854 | |
| 3855 | /* |
Chris Mason | d352ac6 | 2008-09-29 15:18:18 -0400 | [diff] [blame] | 3856 | * unlink helper that gets used here in inode.c and in the tree logging |
| 3857 | * recovery code. It remove a link in a directory with a given name, and |
| 3858 | * also drops the back refs in the inode to the directory |
| 3859 | */ |
Al Viro | 9298679 | 2011-03-04 17:14:37 +0000 | [diff] [blame] | 3860 | static int __btrfs_unlink_inode(struct btrfs_trans_handle *trans, |
| 3861 | struct btrfs_root *root, |
| 3862 | struct inode *dir, struct inode *inode, |
| 3863 | const char *name, int name_len) |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3864 | { |
| 3865 | struct btrfs_path *path; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3866 | int ret = 0; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 3867 | struct extent_buffer *leaf; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3868 | struct btrfs_dir_item *di; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 3869 | struct btrfs_key key; |
Josef Bacik | aec7477 | 2008-07-24 12:12:38 -0400 | [diff] [blame] | 3870 | u64 index; |
Li Zefan | 33345d01 | 2011-04-20 10:31:50 +0800 | [diff] [blame] | 3871 | u64 ino = btrfs_ino(inode); |
| 3872 | u64 dir_ino = btrfs_ino(dir); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3873 | |
| 3874 | path = btrfs_alloc_path(); |
Chris Mason | 54aa1f4 | 2007-06-22 14:16:25 -0400 | [diff] [blame] | 3875 | if (!path) { |
| 3876 | ret = -ENOMEM; |
Tsutomu Itoh | 554233a | 2011-02-03 03:16:25 +0000 | [diff] [blame] | 3877 | goto out; |
Chris Mason | 54aa1f4 | 2007-06-22 14:16:25 -0400 | [diff] [blame] | 3878 | } |
| 3879 | |
Chris Mason | b947343 | 2009-03-13 11:00:37 -0400 | [diff] [blame] | 3880 | path->leave_spinning = 1; |
Li Zefan | 33345d01 | 2011-04-20 10:31:50 +0800 | [diff] [blame] | 3881 | di = btrfs_lookup_dir_item(trans, root, path, dir_ino, |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3882 | name, name_len, -1); |
| 3883 | if (IS_ERR(di)) { |
| 3884 | ret = PTR_ERR(di); |
| 3885 | goto err; |
| 3886 | } |
| 3887 | if (!di) { |
| 3888 | ret = -ENOENT; |
| 3889 | goto err; |
| 3890 | } |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 3891 | leaf = path->nodes[0]; |
| 3892 | btrfs_dir_item_key_to_cpu(leaf, di, &key); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3893 | ret = btrfs_delete_one_dir_name(trans, root, path, di); |
Chris Mason | 54aa1f4 | 2007-06-22 14:16:25 -0400 | [diff] [blame] | 3894 | if (ret) |
| 3895 | goto err; |
David Sterba | b3b4aa7 | 2011-04-21 01:20:15 +0200 | [diff] [blame] | 3896 | btrfs_release_path(path); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3897 | |
Miao Xie | 67de117 | 2013-12-26 13:07:06 +0800 | [diff] [blame] | 3898 | /* |
| 3899 | * If we don't have dir index, we have to get it by looking up |
| 3900 | * the inode ref, since we get the inode ref, remove it directly, |
| 3901 | * it is unnecessary to do delayed deletion. |
| 3902 | * |
| 3903 | * But if we have dir index, needn't search inode ref to get it. |
| 3904 | * Since the inode ref is close to the inode item, it is better |
| 3905 | * that we delay to delete it, and just do this deletion when |
| 3906 | * we update the inode item. |
| 3907 | */ |
| 3908 | if (BTRFS_I(inode)->dir_index) { |
| 3909 | ret = btrfs_delayed_delete_inode_ref(inode); |
| 3910 | if (!ret) { |
| 3911 | index = BTRFS_I(inode)->dir_index; |
| 3912 | goto skip_backref; |
| 3913 | } |
| 3914 | } |
| 3915 | |
Li Zefan | 33345d01 | 2011-04-20 10:31:50 +0800 | [diff] [blame] | 3916 | ret = btrfs_del_inode_ref(trans, root, name, name_len, ino, |
| 3917 | dir_ino, &index); |
Josef Bacik | aec7477 | 2008-07-24 12:12:38 -0400 | [diff] [blame] | 3918 | if (ret) { |
Simon Kirby | c2cf52e | 2013-03-19 22:41:23 +0000 | [diff] [blame] | 3919 | btrfs_info(root->fs_info, |
| 3920 | "failed to delete reference to %.*s, inode %llu parent %llu", |
Geert Uytterhoeven | c1c9ff7 | 2013-08-20 13:20:07 +0200 | [diff] [blame] | 3921 | name_len, name, ino, dir_ino); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 3922 | btrfs_abort_transaction(trans, root, ret); |
Josef Bacik | aec7477 | 2008-07-24 12:12:38 -0400 | [diff] [blame] | 3923 | goto err; |
| 3924 | } |
Miao Xie | 67de117 | 2013-12-26 13:07:06 +0800 | [diff] [blame] | 3925 | skip_backref: |
Miao Xie | 16cdcec | 2011-04-22 18:12:22 +0800 | [diff] [blame] | 3926 | ret = btrfs_delete_delayed_dir_index(trans, root, dir, index); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 3927 | if (ret) { |
| 3928 | btrfs_abort_transaction(trans, root, ret); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3929 | goto err; |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 3930 | } |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3931 | |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 3932 | ret = btrfs_del_inode_ref_in_log(trans, root, name, name_len, |
Li Zefan | 33345d01 | 2011-04-20 10:31:50 +0800 | [diff] [blame] | 3933 | inode, dir_ino); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 3934 | if (ret != 0 && ret != -ENOENT) { |
| 3935 | btrfs_abort_transaction(trans, root, ret); |
| 3936 | goto err; |
| 3937 | } |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 3938 | |
| 3939 | ret = btrfs_del_dir_entries_in_log(trans, root, name, name_len, |
| 3940 | dir, index); |
Chris Mason | 6418c96 | 2010-10-30 07:34:24 -0400 | [diff] [blame] | 3941 | if (ret == -ENOENT) |
| 3942 | ret = 0; |
Zach Brown | d4e3991 | 2013-04-02 21:02:16 +0000 | [diff] [blame] | 3943 | else if (ret) |
| 3944 | btrfs_abort_transaction(trans, root, ret); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3945 | err: |
| 3946 | btrfs_free_path(path); |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 3947 | if (ret) |
| 3948 | goto out; |
| 3949 | |
| 3950 | btrfs_i_size_write(dir, dir->i_size - name_len * 2); |
Josef Bacik | 0c4d2d9 | 2012-04-05 15:03:02 -0400 | [diff] [blame] | 3951 | inode_inc_iversion(inode); |
| 3952 | inode_inc_iversion(dir); |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 3953 | inode->i_ctime = dir->i_mtime = dir->i_ctime = CURRENT_TIME; |
Tsutomu Itoh | b995929 | 2012-06-25 21:25:22 -0600 | [diff] [blame] | 3954 | ret = btrfs_update_inode(trans, root, dir); |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 3955 | out: |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3956 | return ret; |
| 3957 | } |
| 3958 | |
Al Viro | 9298679 | 2011-03-04 17:14:37 +0000 | [diff] [blame] | 3959 | int btrfs_unlink_inode(struct btrfs_trans_handle *trans, |
| 3960 | struct btrfs_root *root, |
| 3961 | struct inode *dir, struct inode *inode, |
| 3962 | const char *name, int name_len) |
| 3963 | { |
| 3964 | int ret; |
| 3965 | ret = __btrfs_unlink_inode(trans, root, dir, inode, name, name_len); |
| 3966 | if (!ret) { |
Zach Brown | 8b558c5 | 2013-10-16 12:10:34 -0700 | [diff] [blame] | 3967 | drop_nlink(inode); |
Al Viro | 9298679 | 2011-03-04 17:14:37 +0000 | [diff] [blame] | 3968 | ret = btrfs_update_inode(trans, root, inode); |
| 3969 | } |
| 3970 | return ret; |
| 3971 | } |
Yan, Zheng | a22285a | 2010-05-16 10:48:46 -0400 | [diff] [blame] | 3972 | |
| 3973 | /* |
| 3974 | * helper to start transaction for unlink and rmdir. |
| 3975 | * |
Josef Bacik | d52be81 | 2013-05-29 14:54:47 -0400 | [diff] [blame] | 3976 | * unlink and rmdir are special in btrfs, they do not always free space, so |
| 3977 | * if we cannot make our reservations the normal way try and see if there is |
| 3978 | * plenty of slack room in the global reserve to migrate, otherwise we cannot |
| 3979 | * allow the unlink to occur. |
Yan, Zheng | a22285a | 2010-05-16 10:48:46 -0400 | [diff] [blame] | 3980 | */ |
Josef Bacik | d52be81 | 2013-05-29 14:54:47 -0400 | [diff] [blame] | 3981 | static struct btrfs_trans_handle *__unlink_start_trans(struct inode *dir) |
Yan, Zheng | a22285a | 2010-05-16 10:48:46 -0400 | [diff] [blame] | 3982 | { |
| 3983 | struct btrfs_trans_handle *trans; |
| 3984 | struct btrfs_root *root = BTRFS_I(dir)->root; |
Yan, Zheng | a22285a | 2010-05-16 10:48:46 -0400 | [diff] [blame] | 3985 | int ret; |
| 3986 | |
Josef Bacik | e70bea5 | 2011-10-11 14:18:24 -0400 | [diff] [blame] | 3987 | /* |
| 3988 | * 1 for the possible orphan item |
| 3989 | * 1 for the dir item |
| 3990 | * 1 for the dir index |
| 3991 | * 1 for the inode ref |
Josef Bacik | e70bea5 | 2011-10-11 14:18:24 -0400 | [diff] [blame] | 3992 | * 1 for the inode |
| 3993 | */ |
Josef Bacik | 6e137ed | 2013-03-26 15:26:55 -0400 | [diff] [blame] | 3994 | trans = btrfs_start_transaction(root, 5); |
Yan, Zheng | a22285a | 2010-05-16 10:48:46 -0400 | [diff] [blame] | 3995 | if (!IS_ERR(trans) || PTR_ERR(trans) != -ENOSPC) |
| 3996 | return trans; |
| 3997 | |
Josef Bacik | d52be81 | 2013-05-29 14:54:47 -0400 | [diff] [blame] | 3998 | if (PTR_ERR(trans) == -ENOSPC) { |
| 3999 | u64 num_bytes = btrfs_calc_trans_metadata_size(root, 5); |
Yan, Zheng | a22285a | 2010-05-16 10:48:46 -0400 | [diff] [blame] | 4000 | |
Josef Bacik | d52be81 | 2013-05-29 14:54:47 -0400 | [diff] [blame] | 4001 | trans = btrfs_start_transaction(root, 0); |
| 4002 | if (IS_ERR(trans)) |
| 4003 | return trans; |
| 4004 | ret = btrfs_cond_migrate_bytes(root->fs_info, |
| 4005 | &root->fs_info->trans_block_rsv, |
| 4006 | num_bytes, 5); |
| 4007 | if (ret) { |
| 4008 | btrfs_end_transaction(trans, root); |
| 4009 | return ERR_PTR(ret); |
Yan, Zheng | a22285a | 2010-05-16 10:48:46 -0400 | [diff] [blame] | 4010 | } |
Josef Bacik | 5a77d76 | 2011-11-01 14:32:23 -0400 | [diff] [blame] | 4011 | trans->block_rsv = &root->fs_info->trans_block_rsv; |
Josef Bacik | d52be81 | 2013-05-29 14:54:47 -0400 | [diff] [blame] | 4012 | trans->bytes_reserved = num_bytes; |
Yan, Zheng | a22285a | 2010-05-16 10:48:46 -0400 | [diff] [blame] | 4013 | } |
Josef Bacik | d52be81 | 2013-05-29 14:54:47 -0400 | [diff] [blame] | 4014 | return trans; |
Yan, Zheng | a22285a | 2010-05-16 10:48:46 -0400 | [diff] [blame] | 4015 | } |
| 4016 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4017 | static int btrfs_unlink(struct inode *dir, struct dentry *dentry) |
| 4018 | { |
Yan, Zheng | a22285a | 2010-05-16 10:48:46 -0400 | [diff] [blame] | 4019 | struct btrfs_root *root = BTRFS_I(dir)->root; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4020 | struct btrfs_trans_handle *trans; |
Josef Bacik | 7b12876 | 2008-07-24 12:17:14 -0400 | [diff] [blame] | 4021 | struct inode *inode = dentry->d_inode; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4022 | int ret; |
| 4023 | |
Josef Bacik | d52be81 | 2013-05-29 14:54:47 -0400 | [diff] [blame] | 4024 | trans = __unlink_start_trans(dir); |
Yan, Zheng | a22285a | 2010-05-16 10:48:46 -0400 | [diff] [blame] | 4025 | if (IS_ERR(trans)) |
Josef Bacik | 5df6a9f | 2009-11-10 21:23:48 -0500 | [diff] [blame] | 4026 | return PTR_ERR(trans); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 4027 | |
Chris Mason | 12fcfd2 | 2009-03-24 10:24:20 -0400 | [diff] [blame] | 4028 | btrfs_record_unlink_dir(trans, dir, dentry->d_inode, 0); |
| 4029 | |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 4030 | ret = btrfs_unlink_inode(trans, root, dir, dentry->d_inode, |
| 4031 | dentry->d_name.name, dentry->d_name.len); |
Tsutomu Itoh | b532402 | 2011-07-19 07:27:20 +0000 | [diff] [blame] | 4032 | if (ret) |
| 4033 | goto out; |
Josef Bacik | 7b12876 | 2008-07-24 12:17:14 -0400 | [diff] [blame] | 4034 | |
Yan, Zheng | a22285a | 2010-05-16 10:48:46 -0400 | [diff] [blame] | 4035 | if (inode->i_nlink == 0) { |
Josef Bacik | 7b12876 | 2008-07-24 12:17:14 -0400 | [diff] [blame] | 4036 | ret = btrfs_orphan_add(trans, inode); |
Tsutomu Itoh | b532402 | 2011-07-19 07:27:20 +0000 | [diff] [blame] | 4037 | if (ret) |
| 4038 | goto out; |
Yan, Zheng | a22285a | 2010-05-16 10:48:46 -0400 | [diff] [blame] | 4039 | } |
Josef Bacik | 7b12876 | 2008-07-24 12:17:14 -0400 | [diff] [blame] | 4040 | |
Tsutomu Itoh | b532402 | 2011-07-19 07:27:20 +0000 | [diff] [blame] | 4041 | out: |
Josef Bacik | d52be81 | 2013-05-29 14:54:47 -0400 | [diff] [blame] | 4042 | btrfs_end_transaction(trans, root); |
Liu Bo | b53d3f5 | 2012-11-14 14:34:34 +0000 | [diff] [blame] | 4043 | btrfs_btree_balance_dirty(root); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4044 | return ret; |
| 4045 | } |
| 4046 | |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 4047 | int btrfs_unlink_subvol(struct btrfs_trans_handle *trans, |
| 4048 | struct btrfs_root *root, |
| 4049 | struct inode *dir, u64 objectid, |
| 4050 | const char *name, int name_len) |
| 4051 | { |
| 4052 | struct btrfs_path *path; |
| 4053 | struct extent_buffer *leaf; |
| 4054 | struct btrfs_dir_item *di; |
| 4055 | struct btrfs_key key; |
| 4056 | u64 index; |
| 4057 | int ret; |
Li Zefan | 33345d01 | 2011-04-20 10:31:50 +0800 | [diff] [blame] | 4058 | u64 dir_ino = btrfs_ino(dir); |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 4059 | |
| 4060 | path = btrfs_alloc_path(); |
| 4061 | if (!path) |
| 4062 | return -ENOMEM; |
| 4063 | |
Li Zefan | 33345d01 | 2011-04-20 10:31:50 +0800 | [diff] [blame] | 4064 | di = btrfs_lookup_dir_item(trans, root, path, dir_ino, |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 4065 | name, name_len, -1); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 4066 | if (IS_ERR_OR_NULL(di)) { |
| 4067 | if (!di) |
| 4068 | ret = -ENOENT; |
| 4069 | else |
| 4070 | ret = PTR_ERR(di); |
| 4071 | goto out; |
| 4072 | } |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 4073 | |
| 4074 | leaf = path->nodes[0]; |
| 4075 | btrfs_dir_item_key_to_cpu(leaf, di, &key); |
| 4076 | WARN_ON(key.type != BTRFS_ROOT_ITEM_KEY || key.objectid != objectid); |
| 4077 | ret = btrfs_delete_one_dir_name(trans, root, path, di); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 4078 | if (ret) { |
| 4079 | btrfs_abort_transaction(trans, root, ret); |
| 4080 | goto out; |
| 4081 | } |
David Sterba | b3b4aa7 | 2011-04-21 01:20:15 +0200 | [diff] [blame] | 4082 | btrfs_release_path(path); |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 4083 | |
| 4084 | ret = btrfs_del_root_ref(trans, root->fs_info->tree_root, |
| 4085 | objectid, root->root_key.objectid, |
Li Zefan | 33345d01 | 2011-04-20 10:31:50 +0800 | [diff] [blame] | 4086 | dir_ino, &index, name, name_len); |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 4087 | if (ret < 0) { |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 4088 | if (ret != -ENOENT) { |
| 4089 | btrfs_abort_transaction(trans, root, ret); |
| 4090 | goto out; |
| 4091 | } |
Li Zefan | 33345d01 | 2011-04-20 10:31:50 +0800 | [diff] [blame] | 4092 | di = btrfs_search_dir_index_item(root, path, dir_ino, |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 4093 | name, name_len); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 4094 | if (IS_ERR_OR_NULL(di)) { |
| 4095 | if (!di) |
| 4096 | ret = -ENOENT; |
| 4097 | else |
| 4098 | ret = PTR_ERR(di); |
| 4099 | btrfs_abort_transaction(trans, root, ret); |
| 4100 | goto out; |
| 4101 | } |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 4102 | |
| 4103 | leaf = path->nodes[0]; |
| 4104 | btrfs_item_key_to_cpu(leaf, &key, path->slots[0]); |
David Sterba | b3b4aa7 | 2011-04-21 01:20:15 +0200 | [diff] [blame] | 4105 | btrfs_release_path(path); |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 4106 | index = key.offset; |
| 4107 | } |
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 | |
Miao Xie | 16cdcec | 2011-04-22 18:12:22 +0800 | [diff] [blame] | 4110 | ret = btrfs_delete_delayed_dir_index(trans, root, dir, index); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 4111 | if (ret) { |
| 4112 | btrfs_abort_transaction(trans, root, ret); |
| 4113 | goto out; |
| 4114 | } |
Miao Xie | 16cdcec | 2011-04-22 18:12:22 +0800 | [diff] [blame] | 4115 | |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 4116 | btrfs_i_size_write(dir, dir->i_size - name_len * 2); |
Josef Bacik | 0c4d2d9 | 2012-04-05 15:03:02 -0400 | [diff] [blame] | 4117 | inode_inc_iversion(dir); |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 4118 | dir->i_mtime = dir->i_ctime = CURRENT_TIME; |
Josef Bacik | 5a24e84 | 2012-08-08 10:12:59 -0600 | [diff] [blame] | 4119 | ret = btrfs_update_inode_fallback(trans, root, dir); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 4120 | if (ret) |
| 4121 | btrfs_abort_transaction(trans, root, ret); |
| 4122 | out: |
Josef Bacik | 71d7aed | 2011-06-14 14:24:32 -0400 | [diff] [blame] | 4123 | btrfs_free_path(path); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 4124 | return ret; |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 4125 | } |
| 4126 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4127 | static int btrfs_rmdir(struct inode *dir, struct dentry *dentry) |
| 4128 | { |
| 4129 | struct inode *inode = dentry->d_inode; |
Chris Mason | 1832a6d | 2007-12-21 16:27:21 -0500 | [diff] [blame] | 4130 | int err = 0; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4131 | struct btrfs_root *root = BTRFS_I(dir)->root; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4132 | struct btrfs_trans_handle *trans; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4133 | |
David Sterba | b3ae244 | 2012-09-13 16:04:34 -0600 | [diff] [blame] | 4134 | if (inode->i_size > BTRFS_EMPTY_DIR_SIZE) |
Yan | 134d451 | 2007-10-25 15:49:25 -0400 | [diff] [blame] | 4135 | return -ENOTEMPTY; |
David Sterba | b3ae244 | 2012-09-13 16:04:34 -0600 | [diff] [blame] | 4136 | if (btrfs_ino(inode) == BTRFS_FIRST_FREE_OBJECTID) |
| 4137 | return -EPERM; |
Yan | 134d451 | 2007-10-25 15:49:25 -0400 | [diff] [blame] | 4138 | |
Josef Bacik | d52be81 | 2013-05-29 14:54:47 -0400 | [diff] [blame] | 4139 | trans = __unlink_start_trans(dir); |
Yan, Zheng | a22285a | 2010-05-16 10:48:46 -0400 | [diff] [blame] | 4140 | if (IS_ERR(trans)) |
Josef Bacik | 5df6a9f | 2009-11-10 21:23:48 -0500 | [diff] [blame] | 4141 | return PTR_ERR(trans); |
Josef Bacik | 5df6a9f | 2009-11-10 21:23:48 -0500 | [diff] [blame] | 4142 | |
Li Zefan | 33345d01 | 2011-04-20 10:31:50 +0800 | [diff] [blame] | 4143 | if (unlikely(btrfs_ino(inode) == BTRFS_EMPTY_SUBVOL_DIR_OBJECTID)) { |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 4144 | err = btrfs_unlink_subvol(trans, root, dir, |
| 4145 | BTRFS_I(inode)->location.objectid, |
| 4146 | dentry->d_name.name, |
| 4147 | dentry->d_name.len); |
| 4148 | goto out; |
| 4149 | } |
| 4150 | |
Josef Bacik | 7b12876 | 2008-07-24 12:17:14 -0400 | [diff] [blame] | 4151 | err = btrfs_orphan_add(trans, inode); |
| 4152 | if (err) |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 4153 | goto out; |
Josef Bacik | 7b12876 | 2008-07-24 12:17:14 -0400 | [diff] [blame] | 4154 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4155 | /* now the directory is empty */ |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 4156 | err = btrfs_unlink_inode(trans, root, dir, dentry->d_inode, |
| 4157 | dentry->d_name.name, dentry->d_name.len); |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 4158 | if (!err) |
Chris Mason | dbe674a | 2008-07-17 12:54:05 -0400 | [diff] [blame] | 4159 | btrfs_i_size_write(inode, 0); |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 4160 | out: |
Josef Bacik | d52be81 | 2013-05-29 14:54:47 -0400 | [diff] [blame] | 4161 | btrfs_end_transaction(trans, root); |
Liu Bo | b53d3f5 | 2012-11-14 14:34:34 +0000 | [diff] [blame] | 4162 | btrfs_btree_balance_dirty(root); |
Chris Mason | 3954401 | 2007-12-12 14:38:19 -0500 | [diff] [blame] | 4163 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4164 | return err; |
| 4165 | } |
| 4166 | |
Chris Mason | 323ac95 | 2008-10-01 19:05:46 -0400 | [diff] [blame] | 4167 | /* |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4168 | * this can truncate away extent items, csum items and directory items. |
| 4169 | * 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] | 4170 | * any higher than new_size |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4171 | * |
| 4172 | * csum items that cross the new i_size are truncated to the new size |
| 4173 | * as well. |
Josef Bacik | 7b12876 | 2008-07-24 12:17:14 -0400 | [diff] [blame] | 4174 | * |
| 4175 | * min_type is the minimum key type to truncate down to. If set to 0, this |
| 4176 | * 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] | 4177 | */ |
Yan, Zheng | 8082510 | 2009-11-12 09:35:36 +0000 | [diff] [blame] | 4178 | int btrfs_truncate_inode_items(struct btrfs_trans_handle *trans, |
| 4179 | struct btrfs_root *root, |
| 4180 | struct inode *inode, |
| 4181 | u64 new_size, u32 min_type) |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4182 | { |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4183 | struct btrfs_path *path; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 4184 | struct extent_buffer *leaf; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4185 | struct btrfs_file_extent_item *fi; |
Yan, Zheng | 8082510 | 2009-11-12 09:35:36 +0000 | [diff] [blame] | 4186 | struct btrfs_key key; |
| 4187 | struct btrfs_key found_key; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4188 | u64 extent_start = 0; |
Chris Mason | db94535 | 2007-10-15 16:15:53 -0400 | [diff] [blame] | 4189 | u64 extent_num_bytes = 0; |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 4190 | u64 extent_offset = 0; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4191 | u64 item_end = 0; |
Josef Bacik | 7f4f6e0 | 2013-08-29 16:43:28 -0400 | [diff] [blame] | 4192 | u64 last_size = (u64)-1; |
Yan, Zheng | 8082510 | 2009-11-12 09:35:36 +0000 | [diff] [blame] | 4193 | u32 found_type = (u8)-1; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4194 | int found_extent; |
| 4195 | int del_item; |
Chris Mason | 85e21ba | 2008-01-29 15:11:36 -0500 | [diff] [blame] | 4196 | int pending_del_nr = 0; |
| 4197 | int pending_del_slot = 0; |
Chris Mason | 179e29e | 2007-11-01 11:28:41 -0400 | [diff] [blame] | 4198 | int extent_type = -1; |
Yan, Zheng | 8082510 | 2009-11-12 09:35:36 +0000 | [diff] [blame] | 4199 | int ret; |
| 4200 | int err = 0; |
Li Zefan | 33345d01 | 2011-04-20 10:31:50 +0800 | [diff] [blame] | 4201 | u64 ino = btrfs_ino(inode); |
Yan, Zheng | 8082510 | 2009-11-12 09:35:36 +0000 | [diff] [blame] | 4202 | |
| 4203 | BUG_ON(new_size > 0 && min_type != BTRFS_EXTENT_DATA_KEY); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4204 | |
Mark Fasheh | 0eb0e19 | 2011-07-12 16:44:10 -0700 | [diff] [blame] | 4205 | path = btrfs_alloc_path(); |
| 4206 | if (!path) |
| 4207 | return -ENOMEM; |
| 4208 | path->reada = -1; |
| 4209 | |
Josef Bacik | 5dc562c | 2012-08-17 13:14:17 -0400 | [diff] [blame] | 4210 | /* |
| 4211 | * We want to drop from the next block forward in case this new size is |
| 4212 | * not block aligned since we will be keeping the last block of the |
| 4213 | * extent just the way it is. |
| 4214 | */ |
Miao Xie | 27cdeb7 | 2014-04-02 19:51:05 +0800 | [diff] [blame] | 4215 | if (test_bit(BTRFS_ROOT_REF_COWS, &root->state) || |
| 4216 | root == root->fs_info->tree_root) |
Qu Wenruo | fda2832 | 2013-02-26 08:10:22 +0000 | [diff] [blame] | 4217 | btrfs_drop_extent_cache(inode, ALIGN(new_size, |
| 4218 | root->sectorsize), (u64)-1, 0); |
Yan, Zheng | 8082510 | 2009-11-12 09:35:36 +0000 | [diff] [blame] | 4219 | |
Miao Xie | 16cdcec | 2011-04-22 18:12:22 +0800 | [diff] [blame] | 4220 | /* |
| 4221 | * This function is also used to drop the items in the log tree before |
| 4222 | * we relog the inode, so if root != BTRFS_I(inode)->root, it means |
| 4223 | * it is used to drop the loged items. So we shouldn't kill the delayed |
| 4224 | * items. |
| 4225 | */ |
| 4226 | if (min_type == 0 && root == BTRFS_I(inode)->root) |
| 4227 | btrfs_kill_delayed_inode_items(inode); |
| 4228 | |
Li Zefan | 33345d01 | 2011-04-20 10:31:50 +0800 | [diff] [blame] | 4229 | key.objectid = ino; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4230 | key.offset = (u64)-1; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 4231 | key.type = (u8)-1; |
| 4232 | |
Chris Mason | 85e21ba | 2008-01-29 15:11:36 -0500 | [diff] [blame] | 4233 | search_again: |
Chris Mason | b947343 | 2009-03-13 11:00:37 -0400 | [diff] [blame] | 4234 | path->leave_spinning = 1; |
Chris Mason | 85e21ba | 2008-01-29 15:11:36 -0500 | [diff] [blame] | 4235 | ret = btrfs_search_slot(trans, root, &key, path, -1, 1); |
Yan, Zheng | 8082510 | 2009-11-12 09:35:36 +0000 | [diff] [blame] | 4236 | if (ret < 0) { |
| 4237 | err = ret; |
| 4238 | goto out; |
| 4239 | } |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 4240 | |
Chris Mason | 85e21ba | 2008-01-29 15:11:36 -0500 | [diff] [blame] | 4241 | if (ret > 0) { |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 4242 | /* there are no items in the tree for us to truncate, we're |
| 4243 | * done |
| 4244 | */ |
Yan, Zheng | 8082510 | 2009-11-12 09:35:36 +0000 | [diff] [blame] | 4245 | if (path->slots[0] == 0) |
| 4246 | goto out; |
Chris Mason | 85e21ba | 2008-01-29 15:11:36 -0500 | [diff] [blame] | 4247 | path->slots[0]--; |
| 4248 | } |
| 4249 | |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 4250 | while (1) { |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4251 | fi = NULL; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 4252 | leaf = path->nodes[0]; |
| 4253 | btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]); |
David Sterba | 962a298 | 2014-06-04 18:41:45 +0200 | [diff] [blame] | 4254 | found_type = found_key.type; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4255 | |
Li Zefan | 33345d01 | 2011-04-20 10:31:50 +0800 | [diff] [blame] | 4256 | if (found_key.objectid != ino) |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4257 | break; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 4258 | |
Chris Mason | 85e21ba | 2008-01-29 15:11:36 -0500 | [diff] [blame] | 4259 | if (found_type < min_type) |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4260 | break; |
| 4261 | |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 4262 | item_end = found_key.offset; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4263 | if (found_type == BTRFS_EXTENT_DATA_KEY) { |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 4264 | fi = btrfs_item_ptr(leaf, path->slots[0], |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4265 | struct btrfs_file_extent_item); |
Chris Mason | 179e29e | 2007-11-01 11:28:41 -0400 | [diff] [blame] | 4266 | extent_type = btrfs_file_extent_type(leaf, fi); |
| 4267 | if (extent_type != BTRFS_FILE_EXTENT_INLINE) { |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 4268 | item_end += |
Chris Mason | db94535 | 2007-10-15 16:15:53 -0400 | [diff] [blame] | 4269 | btrfs_file_extent_num_bytes(leaf, fi); |
Chris Mason | 179e29e | 2007-11-01 11:28:41 -0400 | [diff] [blame] | 4270 | } else if (extent_type == BTRFS_FILE_EXTENT_INLINE) { |
Chris Mason | 179e29e | 2007-11-01 11:28:41 -0400 | [diff] [blame] | 4271 | item_end += btrfs_file_extent_inline_len(leaf, |
Chris Mason | 514ac8a | 2014-01-03 21:07:00 -0800 | [diff] [blame] | 4272 | path->slots[0], fi); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4273 | } |
Yan | 008630c | 2007-11-07 13:31:09 -0500 | [diff] [blame] | 4274 | item_end--; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4275 | } |
Yan, Zheng | 8082510 | 2009-11-12 09:35:36 +0000 | [diff] [blame] | 4276 | if (found_type > min_type) { |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4277 | del_item = 1; |
Yan, Zheng | 8082510 | 2009-11-12 09:35:36 +0000 | [diff] [blame] | 4278 | } else { |
| 4279 | if (item_end < new_size) |
| 4280 | break; |
| 4281 | if (found_key.offset >= new_size) |
| 4282 | del_item = 1; |
| 4283 | else |
| 4284 | del_item = 0; |
| 4285 | } |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4286 | found_extent = 0; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4287 | /* FIXME, shrink the extent if the ref count is only 1 */ |
Chris Mason | 179e29e | 2007-11-01 11:28:41 -0400 | [diff] [blame] | 4288 | if (found_type != BTRFS_EXTENT_DATA_KEY) |
| 4289 | goto delete; |
| 4290 | |
Josef Bacik | 7f4f6e0 | 2013-08-29 16:43:28 -0400 | [diff] [blame] | 4291 | if (del_item) |
| 4292 | last_size = found_key.offset; |
| 4293 | else |
| 4294 | last_size = new_size; |
| 4295 | |
Chris Mason | 179e29e | 2007-11-01 11:28:41 -0400 | [diff] [blame] | 4296 | if (extent_type != BTRFS_FILE_EXTENT_INLINE) { |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4297 | u64 num_dec; |
Chris Mason | db94535 | 2007-10-15 16:15:53 -0400 | [diff] [blame] | 4298 | extent_start = btrfs_file_extent_disk_bytenr(leaf, fi); |
Miao Xie | f70a9a6b | 2012-01-12 19:10:12 -0500 | [diff] [blame] | 4299 | if (!del_item) { |
Chris Mason | db94535 | 2007-10-15 16:15:53 -0400 | [diff] [blame] | 4300 | u64 orig_num_bytes = |
| 4301 | btrfs_file_extent_num_bytes(leaf, fi); |
Qu Wenruo | fda2832 | 2013-02-26 08:10:22 +0000 | [diff] [blame] | 4302 | extent_num_bytes = ALIGN(new_size - |
| 4303 | found_key.offset, |
| 4304 | root->sectorsize); |
Chris Mason | db94535 | 2007-10-15 16:15:53 -0400 | [diff] [blame] | 4305 | btrfs_set_file_extent_num_bytes(leaf, fi, |
| 4306 | extent_num_bytes); |
| 4307 | num_dec = (orig_num_bytes - |
Chris Mason | 9069218 | 2008-02-08 13:49:28 -0500 | [diff] [blame] | 4308 | extent_num_bytes); |
Miao Xie | 27cdeb7 | 2014-04-02 19:51:05 +0800 | [diff] [blame] | 4309 | if (test_bit(BTRFS_ROOT_REF_COWS, |
| 4310 | &root->state) && |
| 4311 | extent_start != 0) |
Yan Zheng | a76a3cd | 2008-10-09 11:46:29 -0400 | [diff] [blame] | 4312 | inode_sub_bytes(inode, num_dec); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 4313 | btrfs_mark_buffer_dirty(leaf); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4314 | } else { |
Chris Mason | db94535 | 2007-10-15 16:15:53 -0400 | [diff] [blame] | 4315 | extent_num_bytes = |
| 4316 | btrfs_file_extent_disk_num_bytes(leaf, |
| 4317 | fi); |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 4318 | extent_offset = found_key.offset - |
| 4319 | btrfs_file_extent_offset(leaf, fi); |
| 4320 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4321 | /* FIXME blocksize != 4096 */ |
Chris Mason | 9069218 | 2008-02-08 13:49:28 -0500 | [diff] [blame] | 4322 | num_dec = btrfs_file_extent_num_bytes(leaf, fi); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4323 | if (extent_start != 0) { |
| 4324 | found_extent = 1; |
Miao Xie | 27cdeb7 | 2014-04-02 19:51:05 +0800 | [diff] [blame] | 4325 | if (test_bit(BTRFS_ROOT_REF_COWS, |
| 4326 | &root->state)) |
Yan Zheng | a76a3cd | 2008-10-09 11:46:29 -0400 | [diff] [blame] | 4327 | inode_sub_bytes(inode, num_dec); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4328 | } |
| 4329 | } |
Chris Mason | 9069218 | 2008-02-08 13:49:28 -0500 | [diff] [blame] | 4330 | } else if (extent_type == BTRFS_FILE_EXTENT_INLINE) { |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 4331 | /* |
| 4332 | * we can't truncate inline items that have had |
| 4333 | * special encodings |
| 4334 | */ |
| 4335 | if (!del_item && |
| 4336 | btrfs_file_extent_compression(leaf, fi) == 0 && |
| 4337 | btrfs_file_extent_encryption(leaf, fi) == 0 && |
| 4338 | btrfs_file_extent_other_encoding(leaf, fi) == 0) { |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 4339 | u32 size = new_size - found_key.offset; |
| 4340 | |
Miao Xie | 27cdeb7 | 2014-04-02 19:51:05 +0800 | [diff] [blame] | 4341 | if (test_bit(BTRFS_ROOT_REF_COWS, &root->state)) |
Yan Zheng | a76a3cd | 2008-10-09 11:46:29 -0400 | [diff] [blame] | 4342 | inode_sub_bytes(inode, item_end + 1 - |
| 4343 | new_size); |
Chris Mason | 514ac8a | 2014-01-03 21:07:00 -0800 | [diff] [blame] | 4344 | |
| 4345 | /* |
| 4346 | * update the ram bytes to properly reflect |
| 4347 | * the new size of our item |
| 4348 | */ |
| 4349 | btrfs_set_file_extent_ram_bytes(leaf, fi, size); |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 4350 | size = |
| 4351 | btrfs_file_extent_calc_inline_size(size); |
Tsutomu Itoh | afe5fea | 2013-04-16 05:18:22 +0000 | [diff] [blame] | 4352 | btrfs_truncate_item(root, path, size, 1); |
Miao Xie | 27cdeb7 | 2014-04-02 19:51:05 +0800 | [diff] [blame] | 4353 | } else if (test_bit(BTRFS_ROOT_REF_COWS, |
| 4354 | &root->state)) { |
Yan Zheng | a76a3cd | 2008-10-09 11:46:29 -0400 | [diff] [blame] | 4355 | inode_sub_bytes(inode, item_end + 1 - |
| 4356 | found_key.offset); |
Chris Mason | 9069218 | 2008-02-08 13:49:28 -0500 | [diff] [blame] | 4357 | } |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4358 | } |
Chris Mason | 179e29e | 2007-11-01 11:28:41 -0400 | [diff] [blame] | 4359 | delete: |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4360 | if (del_item) { |
Chris Mason | 85e21ba | 2008-01-29 15:11:36 -0500 | [diff] [blame] | 4361 | if (!pending_del_nr) { |
| 4362 | /* no pending yet, add ourselves */ |
| 4363 | pending_del_slot = path->slots[0]; |
| 4364 | pending_del_nr = 1; |
| 4365 | } else if (pending_del_nr && |
| 4366 | path->slots[0] + 1 == pending_del_slot) { |
| 4367 | /* hop on the pending chunk */ |
| 4368 | pending_del_nr++; |
| 4369 | pending_del_slot = path->slots[0]; |
| 4370 | } else { |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 4371 | BUG(); |
Chris Mason | 85e21ba | 2008-01-29 15:11:36 -0500 | [diff] [blame] | 4372 | } |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4373 | } else { |
| 4374 | break; |
| 4375 | } |
Miao Xie | 27cdeb7 | 2014-04-02 19:51:05 +0800 | [diff] [blame] | 4376 | if (found_extent && |
| 4377 | (test_bit(BTRFS_ROOT_REF_COWS, &root->state) || |
| 4378 | root == root->fs_info->tree_root)) { |
Chris Mason | b947343 | 2009-03-13 11:00:37 -0400 | [diff] [blame] | 4379 | btrfs_set_path_blocking(path); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4380 | ret = btrfs_free_extent(trans, root, extent_start, |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 4381 | extent_num_bytes, 0, |
| 4382 | btrfs_header_owner(leaf), |
Arne Jansen | 66d7e7f | 2011-09-12 15:26:38 +0200 | [diff] [blame] | 4383 | ino, extent_offset, 0); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4384 | BUG_ON(ret); |
| 4385 | } |
Chris Mason | 85e21ba | 2008-01-29 15:11:36 -0500 | [diff] [blame] | 4386 | |
Yan, Zheng | 8082510 | 2009-11-12 09:35:36 +0000 | [diff] [blame] | 4387 | if (found_type == BTRFS_INODE_ITEM_KEY) |
| 4388 | break; |
| 4389 | |
| 4390 | if (path->slots[0] == 0 || |
| 4391 | path->slots[0] != pending_del_slot) { |
Yan, Zheng | 8082510 | 2009-11-12 09:35:36 +0000 | [diff] [blame] | 4392 | if (pending_del_nr) { |
| 4393 | ret = btrfs_del_items(trans, root, path, |
| 4394 | pending_del_slot, |
| 4395 | pending_del_nr); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 4396 | if (ret) { |
| 4397 | btrfs_abort_transaction(trans, |
| 4398 | root, ret); |
| 4399 | goto error; |
| 4400 | } |
Yan, Zheng | 8082510 | 2009-11-12 09:35:36 +0000 | [diff] [blame] | 4401 | pending_del_nr = 0; |
| 4402 | } |
David Sterba | b3b4aa7 | 2011-04-21 01:20:15 +0200 | [diff] [blame] | 4403 | btrfs_release_path(path); |
Chris Mason | 85e21ba | 2008-01-29 15:11:36 -0500 | [diff] [blame] | 4404 | goto search_again; |
Yan, Zheng | 8082510 | 2009-11-12 09:35:36 +0000 | [diff] [blame] | 4405 | } else { |
| 4406 | path->slots[0]--; |
Chris Mason | 85e21ba | 2008-01-29 15:11:36 -0500 | [diff] [blame] | 4407 | } |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4408 | } |
Yan, Zheng | 8082510 | 2009-11-12 09:35:36 +0000 | [diff] [blame] | 4409 | out: |
Chris Mason | 85e21ba | 2008-01-29 15:11:36 -0500 | [diff] [blame] | 4410 | if (pending_del_nr) { |
| 4411 | ret = btrfs_del_items(trans, root, path, pending_del_slot, |
| 4412 | pending_del_nr); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 4413 | if (ret) |
| 4414 | btrfs_abort_transaction(trans, root, ret); |
Chris Mason | 85e21ba | 2008-01-29 15:11:36 -0500 | [diff] [blame] | 4415 | } |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 4416 | error: |
Filipe Manana | dac5705 | 2014-08-29 20:54:26 +0100 | [diff] [blame] | 4417 | if (last_size != (u64)-1 && |
| 4418 | root->root_key.objectid != BTRFS_TREE_LOG_OBJECTID) |
Josef Bacik | 7f4f6e0 | 2013-08-29 16:43:28 -0400 | [diff] [blame] | 4419 | btrfs_ordered_update_i_size(inode, last_size, NULL); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4420 | btrfs_free_path(path); |
Yan, Zheng | 8082510 | 2009-11-12 09:35:36 +0000 | [diff] [blame] | 4421 | return err; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4422 | } |
| 4423 | |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 4424 | /* |
Josef Bacik | 2aaa665 | 2012-08-29 14:27:18 -0400 | [diff] [blame] | 4425 | * btrfs_truncate_page - read, zero a chunk and write a page |
| 4426 | * @inode - inode that we're zeroing |
| 4427 | * @from - the offset to start zeroing |
| 4428 | * @len - the length to zero, 0 to zero the entire range respective to the |
| 4429 | * offset |
| 4430 | * @front - zero up to the offset instead of from the offset on |
| 4431 | * |
| 4432 | * This will find the page for the "from" offset and cow the page and zero the |
| 4433 | * 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] | 4434 | */ |
Josef Bacik | 2aaa665 | 2012-08-29 14:27:18 -0400 | [diff] [blame] | 4435 | int btrfs_truncate_page(struct inode *inode, loff_t from, loff_t len, |
| 4436 | int front) |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 4437 | { |
Josef Bacik | 2aaa665 | 2012-08-29 14:27:18 -0400 | [diff] [blame] | 4438 | struct address_space *mapping = inode->i_mapping; |
Chris Mason | db94535 | 2007-10-15 16:15:53 -0400 | [diff] [blame] | 4439 | struct btrfs_root *root = BTRFS_I(inode)->root; |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 4440 | struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree; |
| 4441 | struct btrfs_ordered_extent *ordered; |
Josef Bacik | 2ac55d4 | 2010-02-03 19:33:23 +0000 | [diff] [blame] | 4442 | struct extent_state *cached_state = NULL; |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 4443 | char *kaddr; |
Chris Mason | db94535 | 2007-10-15 16:15:53 -0400 | [diff] [blame] | 4444 | u32 blocksize = root->sectorsize; |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 4445 | pgoff_t index = from >> PAGE_CACHE_SHIFT; |
| 4446 | unsigned offset = from & (PAGE_CACHE_SIZE-1); |
| 4447 | struct page *page; |
Josef Bacik | 3b16a4e | 2011-09-21 15:05:58 -0400 | [diff] [blame] | 4448 | gfp_t mask = btrfs_alloc_write_mask(mapping); |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 4449 | int ret = 0; |
| 4450 | u64 page_start; |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 4451 | u64 page_end; |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 4452 | |
Josef Bacik | 2aaa665 | 2012-08-29 14:27:18 -0400 | [diff] [blame] | 4453 | if ((offset & (blocksize - 1)) == 0 && |
| 4454 | (!len || ((len & (blocksize - 1)) == 0))) |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 4455 | goto out; |
Yan, Zheng | 0ca1f7c | 2010-05-16 10:48:47 -0400 | [diff] [blame] | 4456 | ret = btrfs_delalloc_reserve_space(inode, PAGE_CACHE_SIZE); |
Josef Bacik | 5d5e103 | 2009-10-13 16:46:49 -0400 | [diff] [blame] | 4457 | if (ret) |
| 4458 | goto out; |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 4459 | |
Chris Mason | 211c17f | 2008-05-15 09:13:45 -0400 | [diff] [blame] | 4460 | again: |
Josef Bacik | 3b16a4e | 2011-09-21 15:05:58 -0400 | [diff] [blame] | 4461 | page = find_or_create_page(mapping, index, mask); |
Josef Bacik | 5d5e103 | 2009-10-13 16:46:49 -0400 | [diff] [blame] | 4462 | if (!page) { |
Yan, Zheng | 0ca1f7c | 2010-05-16 10:48:47 -0400 | [diff] [blame] | 4463 | btrfs_delalloc_release_space(inode, PAGE_CACHE_SIZE); |
Miao Xie | ac6a2b3 | 2012-12-05 10:56:13 +0000 | [diff] [blame] | 4464 | ret = -ENOMEM; |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 4465 | goto out; |
Josef Bacik | 5d5e103 | 2009-10-13 16:46:49 -0400 | [diff] [blame] | 4466 | } |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 4467 | |
| 4468 | page_start = page_offset(page); |
| 4469 | page_end = page_start + PAGE_CACHE_SIZE - 1; |
| 4470 | |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 4471 | if (!PageUptodate(page)) { |
| 4472 | ret = btrfs_readpage(NULL, page); |
| 4473 | lock_page(page); |
Chris Mason | 211c17f | 2008-05-15 09:13:45 -0400 | [diff] [blame] | 4474 | if (page->mapping != mapping) { |
| 4475 | unlock_page(page); |
| 4476 | page_cache_release(page); |
| 4477 | goto again; |
| 4478 | } |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 4479 | if (!PageUptodate(page)) { |
| 4480 | ret = -EIO; |
Chris Mason | 8964222 | 2008-07-24 09:41:53 -0400 | [diff] [blame] | 4481 | goto out_unlock; |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 4482 | } |
| 4483 | } |
Chris Mason | 211c17f | 2008-05-15 09:13:45 -0400 | [diff] [blame] | 4484 | wait_on_page_writeback(page); |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 4485 | |
Jeff Mahoney | d008237 | 2012-03-01 14:57:19 +0100 | [diff] [blame] | 4486 | lock_extent_bits(io_tree, page_start, page_end, 0, &cached_state); |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 4487 | set_page_extent_mapped(page); |
| 4488 | |
| 4489 | ordered = btrfs_lookup_ordered_extent(inode, page_start); |
| 4490 | if (ordered) { |
Josef Bacik | 2ac55d4 | 2010-02-03 19:33:23 +0000 | [diff] [blame] | 4491 | unlock_extent_cached(io_tree, page_start, page_end, |
| 4492 | &cached_state, GFP_NOFS); |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 4493 | unlock_page(page); |
| 4494 | page_cache_release(page); |
Chris Mason | eb84ae0 | 2008-07-17 13:53:27 -0400 | [diff] [blame] | 4495 | btrfs_start_ordered_extent(inode, ordered, 1); |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 4496 | btrfs_put_ordered_extent(ordered); |
| 4497 | goto again; |
| 4498 | } |
| 4499 | |
Josef Bacik | 2ac55d4 | 2010-02-03 19:33:23 +0000 | [diff] [blame] | 4500 | clear_extent_bit(&BTRFS_I(inode)->io_tree, page_start, page_end, |
Liu Bo | 9e8a4a8 | 2012-09-05 19:10:51 -0600 | [diff] [blame] | 4501 | EXTENT_DIRTY | EXTENT_DELALLOC | |
| 4502 | EXTENT_DO_ACCOUNTING | EXTENT_DEFRAG, |
Josef Bacik | 2ac55d4 | 2010-02-03 19:33:23 +0000 | [diff] [blame] | 4503 | 0, 0, &cached_state, GFP_NOFS); |
Josef Bacik | 5d5e103 | 2009-10-13 16:46:49 -0400 | [diff] [blame] | 4504 | |
Josef Bacik | 2ac55d4 | 2010-02-03 19:33:23 +0000 | [diff] [blame] | 4505 | ret = btrfs_set_extent_delalloc(inode, page_start, page_end, |
| 4506 | &cached_state); |
Josef Bacik | 9ed74f2 | 2009-09-11 16:12:44 -0400 | [diff] [blame] | 4507 | if (ret) { |
Josef Bacik | 2ac55d4 | 2010-02-03 19:33:23 +0000 | [diff] [blame] | 4508 | unlock_extent_cached(io_tree, page_start, page_end, |
| 4509 | &cached_state, GFP_NOFS); |
Josef Bacik | 9ed74f2 | 2009-09-11 16:12:44 -0400 | [diff] [blame] | 4510 | goto out_unlock; |
| 4511 | } |
| 4512 | |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 4513 | if (offset != PAGE_CACHE_SIZE) { |
Josef Bacik | 2aaa665 | 2012-08-29 14:27:18 -0400 | [diff] [blame] | 4514 | if (!len) |
| 4515 | len = PAGE_CACHE_SIZE - offset; |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 4516 | kaddr = kmap(page); |
Josef Bacik | 2aaa665 | 2012-08-29 14:27:18 -0400 | [diff] [blame] | 4517 | if (front) |
| 4518 | memset(kaddr, 0, offset); |
| 4519 | else |
| 4520 | memset(kaddr + offset, 0, len); |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 4521 | flush_dcache_page(page); |
| 4522 | kunmap(page); |
| 4523 | } |
Chris Mason | 247e743 | 2008-07-17 12:53:51 -0400 | [diff] [blame] | 4524 | ClearPageChecked(page); |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 4525 | set_page_dirty(page); |
Josef Bacik | 2ac55d4 | 2010-02-03 19:33:23 +0000 | [diff] [blame] | 4526 | unlock_extent_cached(io_tree, page_start, page_end, &cached_state, |
| 4527 | GFP_NOFS); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4528 | |
Chris Mason | 8964222 | 2008-07-24 09:41:53 -0400 | [diff] [blame] | 4529 | out_unlock: |
Josef Bacik | 5d5e103 | 2009-10-13 16:46:49 -0400 | [diff] [blame] | 4530 | if (ret) |
Yan, Zheng | 0ca1f7c | 2010-05-16 10:48:47 -0400 | [diff] [blame] | 4531 | btrfs_delalloc_release_space(inode, PAGE_CACHE_SIZE); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4532 | unlock_page(page); |
| 4533 | page_cache_release(page); |
| 4534 | out: |
| 4535 | return ret; |
| 4536 | } |
| 4537 | |
Josef Bacik | 16e7549 | 2013-10-22 12:18:51 -0400 | [diff] [blame] | 4538 | static int maybe_insert_hole(struct btrfs_root *root, struct inode *inode, |
| 4539 | u64 offset, u64 len) |
| 4540 | { |
| 4541 | struct btrfs_trans_handle *trans; |
| 4542 | int ret; |
| 4543 | |
| 4544 | /* |
| 4545 | * Still need to make sure the inode looks like it's been updated so |
| 4546 | * that any holes get logged if we fsync. |
| 4547 | */ |
| 4548 | if (btrfs_fs_incompat(root->fs_info, NO_HOLES)) { |
| 4549 | BTRFS_I(inode)->last_trans = root->fs_info->generation; |
| 4550 | BTRFS_I(inode)->last_sub_trans = root->log_transid; |
| 4551 | BTRFS_I(inode)->last_log_commit = root->last_log_commit; |
| 4552 | return 0; |
| 4553 | } |
| 4554 | |
| 4555 | /* |
| 4556 | * 1 - for the one we're dropping |
| 4557 | * 1 - for the one we're adding |
| 4558 | * 1 - for updating the inode. |
| 4559 | */ |
| 4560 | trans = btrfs_start_transaction(root, 3); |
| 4561 | if (IS_ERR(trans)) |
| 4562 | return PTR_ERR(trans); |
| 4563 | |
| 4564 | ret = btrfs_drop_extents(trans, root, inode, offset, offset + len, 1); |
| 4565 | if (ret) { |
| 4566 | btrfs_abort_transaction(trans, root, ret); |
| 4567 | btrfs_end_transaction(trans, root); |
| 4568 | return ret; |
| 4569 | } |
| 4570 | |
| 4571 | ret = btrfs_insert_file_extent(trans, root, btrfs_ino(inode), offset, |
| 4572 | 0, 0, len, 0, len, 0, 0, 0); |
| 4573 | if (ret) |
| 4574 | btrfs_abort_transaction(trans, root, ret); |
| 4575 | else |
| 4576 | btrfs_update_inode(trans, root, inode); |
| 4577 | btrfs_end_transaction(trans, root); |
| 4578 | return ret; |
| 4579 | } |
| 4580 | |
Josef Bacik | 695a0d0 | 2011-03-04 15:46:53 -0500 | [diff] [blame] | 4581 | /* |
| 4582 | * This function puts in dummy file extents for the area we're creating a hole |
| 4583 | * for. So if we are truncating this file to a larger size we need to insert |
| 4584 | * these file extents so that btrfs_get_extent will return a EXTENT_MAP_HOLE for |
| 4585 | * the range between oldsize and size |
| 4586 | */ |
Josef Bacik | a41ad39 | 2011-01-31 15:30:16 -0500 | [diff] [blame] | 4587 | 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] | 4588 | { |
Yan Zheng | 9036c10 | 2008-10-30 14:19:41 -0400 | [diff] [blame] | 4589 | struct btrfs_root *root = BTRFS_I(inode)->root; |
| 4590 | struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree; |
Yan, Zheng | a22285a | 2010-05-16 10:48:46 -0400 | [diff] [blame] | 4591 | struct extent_map *em = NULL; |
Josef Bacik | 2ac55d4 | 2010-02-03 19:33:23 +0000 | [diff] [blame] | 4592 | struct extent_state *cached_state = NULL; |
Josef Bacik | 5dc562c | 2012-08-17 13:14:17 -0400 | [diff] [blame] | 4593 | struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree; |
Qu Wenruo | fda2832 | 2013-02-26 08:10:22 +0000 | [diff] [blame] | 4594 | u64 hole_start = ALIGN(oldsize, root->sectorsize); |
| 4595 | u64 block_end = ALIGN(size, root->sectorsize); |
Yan Zheng | 9036c10 | 2008-10-30 14:19:41 -0400 | [diff] [blame] | 4596 | u64 last_byte; |
| 4597 | u64 cur_offset; |
| 4598 | u64 hole_size; |
Josef Bacik | 9ed74f2 | 2009-09-11 16:12:44 -0400 | [diff] [blame] | 4599 | int err = 0; |
Yan Zheng | 9036c10 | 2008-10-30 14:19:41 -0400 | [diff] [blame] | 4600 | |
Josef Bacik | a71754f | 2013-06-17 17:14:39 -0400 | [diff] [blame] | 4601 | /* |
| 4602 | * If our size started in the middle of a page we need to zero out the |
| 4603 | * rest of the page before we expand the i_size, otherwise we could |
| 4604 | * expose stale data. |
| 4605 | */ |
| 4606 | err = btrfs_truncate_page(inode, oldsize, 0, 0); |
| 4607 | if (err) |
| 4608 | return err; |
| 4609 | |
Yan Zheng | 9036c10 | 2008-10-30 14:19:41 -0400 | [diff] [blame] | 4610 | if (size <= hole_start) |
| 4611 | return 0; |
| 4612 | |
Yan Zheng | 9036c10 | 2008-10-30 14:19:41 -0400 | [diff] [blame] | 4613 | while (1) { |
| 4614 | struct btrfs_ordered_extent *ordered; |
Miao Xie | fa7c149 | 2013-09-26 13:15:27 +0800 | [diff] [blame] | 4615 | |
Josef Bacik | 2ac55d4 | 2010-02-03 19:33:23 +0000 | [diff] [blame] | 4616 | lock_extent_bits(io_tree, hole_start, block_end - 1, 0, |
Jeff Mahoney | d008237 | 2012-03-01 14:57:19 +0100 | [diff] [blame] | 4617 | &cached_state); |
Miao Xie | fa7c149 | 2013-09-26 13:15:27 +0800 | [diff] [blame] | 4618 | ordered = btrfs_lookup_ordered_range(inode, hole_start, |
| 4619 | block_end - hole_start); |
Yan Zheng | 9036c10 | 2008-10-30 14:19:41 -0400 | [diff] [blame] | 4620 | if (!ordered) |
| 4621 | break; |
Josef Bacik | 2ac55d4 | 2010-02-03 19:33:23 +0000 | [diff] [blame] | 4622 | unlock_extent_cached(io_tree, hole_start, block_end - 1, |
| 4623 | &cached_state, GFP_NOFS); |
Miao Xie | fa7c149 | 2013-09-26 13:15:27 +0800 | [diff] [blame] | 4624 | btrfs_start_ordered_extent(inode, ordered, 1); |
Yan Zheng | 9036c10 | 2008-10-30 14:19:41 -0400 | [diff] [blame] | 4625 | btrfs_put_ordered_extent(ordered); |
| 4626 | } |
| 4627 | |
Yan Zheng | 9036c10 | 2008-10-30 14:19:41 -0400 | [diff] [blame] | 4628 | cur_offset = hole_start; |
| 4629 | while (1) { |
| 4630 | em = btrfs_get_extent(inode, NULL, 0, cur_offset, |
| 4631 | block_end - cur_offset, 0); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 4632 | if (IS_ERR(em)) { |
| 4633 | err = PTR_ERR(em); |
Zach Brown | f276795 | 2013-01-08 19:37:58 +0000 | [diff] [blame] | 4634 | em = NULL; |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 4635 | break; |
| 4636 | } |
Yan Zheng | 9036c10 | 2008-10-30 14:19:41 -0400 | [diff] [blame] | 4637 | last_byte = min(extent_map_end(em), block_end); |
Qu Wenruo | fda2832 | 2013-02-26 08:10:22 +0000 | [diff] [blame] | 4638 | last_byte = ALIGN(last_byte , root->sectorsize); |
Yan, Zheng | 8082510 | 2009-11-12 09:35:36 +0000 | [diff] [blame] | 4639 | if (!test_bit(EXTENT_FLAG_PREALLOC, &em->flags)) { |
Josef Bacik | 5dc562c | 2012-08-17 13:14:17 -0400 | [diff] [blame] | 4640 | struct extent_map *hole_em; |
Yan Zheng | 9036c10 | 2008-10-30 14:19:41 -0400 | [diff] [blame] | 4641 | hole_size = last_byte - cur_offset; |
Yan, Zheng | 8082510 | 2009-11-12 09:35:36 +0000 | [diff] [blame] | 4642 | |
Josef Bacik | 16e7549 | 2013-10-22 12:18:51 -0400 | [diff] [blame] | 4643 | err = maybe_insert_hole(root, inode, cur_offset, |
| 4644 | hole_size); |
| 4645 | if (err) |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 4646 | break; |
Josef Bacik | 5dc562c | 2012-08-17 13:14:17 -0400 | [diff] [blame] | 4647 | btrfs_drop_extent_cache(inode, cur_offset, |
| 4648 | cur_offset + hole_size - 1, 0); |
| 4649 | hole_em = alloc_extent_map(); |
| 4650 | if (!hole_em) { |
| 4651 | set_bit(BTRFS_INODE_NEEDS_FULL_SYNC, |
| 4652 | &BTRFS_I(inode)->runtime_flags); |
| 4653 | goto next; |
| 4654 | } |
| 4655 | hole_em->start = cur_offset; |
| 4656 | hole_em->len = hole_size; |
| 4657 | hole_em->orig_start = cur_offset; |
Yan, Zheng | 8082510 | 2009-11-12 09:35:36 +0000 | [diff] [blame] | 4658 | |
Josef Bacik | 5dc562c | 2012-08-17 13:14:17 -0400 | [diff] [blame] | 4659 | hole_em->block_start = EXTENT_MAP_HOLE; |
| 4660 | hole_em->block_len = 0; |
Josef Bacik | b493968 | 2012-12-03 10:31:19 -0500 | [diff] [blame] | 4661 | hole_em->orig_block_len = 0; |
Josef Bacik | cc95bef | 2013-04-04 14:31:27 -0400 | [diff] [blame] | 4662 | hole_em->ram_bytes = hole_size; |
Josef Bacik | 5dc562c | 2012-08-17 13:14:17 -0400 | [diff] [blame] | 4663 | hole_em->bdev = root->fs_info->fs_devices->latest_bdev; |
| 4664 | hole_em->compress_type = BTRFS_COMPRESS_NONE; |
Josef Bacik | 16e7549 | 2013-10-22 12:18:51 -0400 | [diff] [blame] | 4665 | hole_em->generation = root->fs_info->generation; |
Josef Bacik | 5dc562c | 2012-08-17 13:14:17 -0400 | [diff] [blame] | 4666 | |
| 4667 | while (1) { |
| 4668 | write_lock(&em_tree->lock); |
Josef Bacik | 09a2a8f9 | 2013-04-05 16:51:15 -0400 | [diff] [blame] | 4669 | err = add_extent_mapping(em_tree, hole_em, 1); |
Josef Bacik | 5dc562c | 2012-08-17 13:14:17 -0400 | [diff] [blame] | 4670 | write_unlock(&em_tree->lock); |
| 4671 | if (err != -EEXIST) |
| 4672 | break; |
| 4673 | btrfs_drop_extent_cache(inode, cur_offset, |
| 4674 | cur_offset + |
| 4675 | hole_size - 1, 0); |
| 4676 | } |
| 4677 | free_extent_map(hole_em); |
Yan Zheng | 9036c10 | 2008-10-30 14:19:41 -0400 | [diff] [blame] | 4678 | } |
Josef Bacik | 16e7549 | 2013-10-22 12:18:51 -0400 | [diff] [blame] | 4679 | next: |
Yan Zheng | 9036c10 | 2008-10-30 14:19:41 -0400 | [diff] [blame] | 4680 | free_extent_map(em); |
Yan, Zheng | a22285a | 2010-05-16 10:48:46 -0400 | [diff] [blame] | 4681 | em = NULL; |
Yan Zheng | 9036c10 | 2008-10-30 14:19:41 -0400 | [diff] [blame] | 4682 | cur_offset = last_byte; |
Yan, Zheng | 8082510 | 2009-11-12 09:35:36 +0000 | [diff] [blame] | 4683 | if (cur_offset >= block_end) |
Yan Zheng | 9036c10 | 2008-10-30 14:19:41 -0400 | [diff] [blame] | 4684 | break; |
| 4685 | } |
Yan, Zheng | a22285a | 2010-05-16 10:48:46 -0400 | [diff] [blame] | 4686 | free_extent_map(em); |
Josef Bacik | 2ac55d4 | 2010-02-03 19:33:23 +0000 | [diff] [blame] | 4687 | unlock_extent_cached(io_tree, hole_start, block_end - 1, &cached_state, |
| 4688 | GFP_NOFS); |
Yan Zheng | 9036c10 | 2008-10-30 14:19:41 -0400 | [diff] [blame] | 4689 | return err; |
| 4690 | } |
| 4691 | |
Filipe Manana | 9ea24bb | 2014-10-29 11:57:59 +0000 | [diff] [blame] | 4692 | static int wait_snapshoting_atomic_t(atomic_t *a) |
| 4693 | { |
| 4694 | schedule(); |
| 4695 | return 0; |
| 4696 | } |
| 4697 | |
| 4698 | static void wait_for_snapshot_creation(struct btrfs_root *root) |
| 4699 | { |
| 4700 | while (true) { |
| 4701 | int ret; |
| 4702 | |
| 4703 | ret = btrfs_start_write_no_snapshoting(root); |
| 4704 | if (ret) |
| 4705 | break; |
| 4706 | wait_on_atomic_t(&root->will_be_snapshoted, |
| 4707 | wait_snapshoting_atomic_t, |
| 4708 | TASK_UNINTERRUPTIBLE); |
| 4709 | } |
| 4710 | } |
| 4711 | |
Eric Sandeen | 3972f26 | 2013-01-12 02:57:22 +0000 | [diff] [blame] | 4712 | static int btrfs_setsize(struct inode *inode, struct iattr *attr) |
Yan, Zheng | 8082510 | 2009-11-12 09:35:36 +0000 | [diff] [blame] | 4713 | { |
Miao Xie | f4a2f4c | 2011-12-14 20:12:01 -0500 | [diff] [blame] | 4714 | struct btrfs_root *root = BTRFS_I(inode)->root; |
| 4715 | struct btrfs_trans_handle *trans; |
Josef Bacik | a41ad39 | 2011-01-31 15:30:16 -0500 | [diff] [blame] | 4716 | loff_t oldsize = i_size_read(inode); |
Eric Sandeen | 3972f26 | 2013-01-12 02:57:22 +0000 | [diff] [blame] | 4717 | loff_t newsize = attr->ia_size; |
| 4718 | int mask = attr->ia_valid; |
Yan, Zheng | 8082510 | 2009-11-12 09:35:36 +0000 | [diff] [blame] | 4719 | int ret; |
| 4720 | |
Eric Sandeen | 3972f26 | 2013-01-12 02:57:22 +0000 | [diff] [blame] | 4721 | /* |
| 4722 | * The regular truncate() case without ATTR_CTIME and ATTR_MTIME is a |
| 4723 | * special case where we need to update the times despite not having |
| 4724 | * these flags set. For all other operations the VFS set these flags |
| 4725 | * explicitly if it wants a timestamp update. |
| 4726 | */ |
Christoph Hellwig | dff6efc | 2013-11-19 07:17:07 -0800 | [diff] [blame] | 4727 | if (newsize != oldsize) { |
| 4728 | inode_inc_iversion(inode); |
| 4729 | if (!(mask & (ATTR_CTIME | ATTR_MTIME))) |
| 4730 | inode->i_ctime = inode->i_mtime = |
| 4731 | current_fs_time(inode->i_sb); |
| 4732 | } |
Eric Sandeen | 3972f26 | 2013-01-12 02:57:22 +0000 | [diff] [blame] | 4733 | |
Josef Bacik | a41ad39 | 2011-01-31 15:30:16 -0500 | [diff] [blame] | 4734 | if (newsize > oldsize) { |
Kirill A. Shutemov | 7caef26 | 2013-09-12 15:13:56 -0700 | [diff] [blame] | 4735 | truncate_pagecache(inode, newsize); |
Filipe Manana | 9ea24bb | 2014-10-29 11:57:59 +0000 | [diff] [blame] | 4736 | /* |
| 4737 | * Don't do an expanding truncate while snapshoting is ongoing. |
| 4738 | * This is to ensure the snapshot captures a fully consistent |
| 4739 | * state of this file - if the snapshot captures this expanding |
| 4740 | * truncation, it must capture all writes that happened before |
| 4741 | * this truncation. |
| 4742 | */ |
| 4743 | wait_for_snapshot_creation(root); |
Josef Bacik | a41ad39 | 2011-01-31 15:30:16 -0500 | [diff] [blame] | 4744 | ret = btrfs_cont_expand(inode, oldsize, newsize); |
Filipe Manana | 9ea24bb | 2014-10-29 11:57:59 +0000 | [diff] [blame] | 4745 | if (ret) { |
| 4746 | btrfs_end_write_no_snapshoting(root); |
Yan, Zheng | 8082510 | 2009-11-12 09:35:36 +0000 | [diff] [blame] | 4747 | return ret; |
Filipe Manana | 9ea24bb | 2014-10-29 11:57:59 +0000 | [diff] [blame] | 4748 | } |
Yan, Zheng | 8082510 | 2009-11-12 09:35:36 +0000 | [diff] [blame] | 4749 | |
Miao Xie | f4a2f4c | 2011-12-14 20:12:01 -0500 | [diff] [blame] | 4750 | trans = btrfs_start_transaction(root, 1); |
Filipe Manana | 9ea24bb | 2014-10-29 11:57:59 +0000 | [diff] [blame] | 4751 | if (IS_ERR(trans)) { |
| 4752 | btrfs_end_write_no_snapshoting(root); |
Miao Xie | f4a2f4c | 2011-12-14 20:12:01 -0500 | [diff] [blame] | 4753 | return PTR_ERR(trans); |
Filipe Manana | 9ea24bb | 2014-10-29 11:57:59 +0000 | [diff] [blame] | 4754 | } |
Miao Xie | f4a2f4c | 2011-12-14 20:12:01 -0500 | [diff] [blame] | 4755 | |
| 4756 | i_size_write(inode, newsize); |
| 4757 | btrfs_ordered_update_i_size(inode, i_size_read(inode), NULL); |
| 4758 | ret = btrfs_update_inode(trans, root, inode); |
Filipe Manana | 9ea24bb | 2014-10-29 11:57:59 +0000 | [diff] [blame] | 4759 | btrfs_end_write_no_snapshoting(root); |
Josef Bacik | 7ad85bb | 2012-01-12 19:10:12 -0500 | [diff] [blame] | 4760 | btrfs_end_transaction(trans, root); |
Josef Bacik | a41ad39 | 2011-01-31 15:30:16 -0500 | [diff] [blame] | 4761 | } else { |
Yan, Zheng | 8082510 | 2009-11-12 09:35:36 +0000 | [diff] [blame] | 4762 | |
Josef Bacik | a41ad39 | 2011-01-31 15:30:16 -0500 | [diff] [blame] | 4763 | /* |
| 4764 | * We're truncating a file that used to have good data down to |
| 4765 | * zero. Make sure it gets into the ordered flush list so that |
| 4766 | * any new writes get down to disk quickly. |
| 4767 | */ |
| 4768 | if (newsize == 0) |
Josef Bacik | 72ac3c0 | 2012-05-23 14:13:11 -0400 | [diff] [blame] | 4769 | set_bit(BTRFS_INODE_ORDERED_DATA_CLOSE, |
| 4770 | &BTRFS_I(inode)->runtime_flags); |
Yan, Zheng | 8082510 | 2009-11-12 09:35:36 +0000 | [diff] [blame] | 4771 | |
Josef Bacik | f3fe820 | 2013-01-07 17:03:21 -0500 | [diff] [blame] | 4772 | /* |
| 4773 | * 1 for the orphan item we're going to add |
| 4774 | * 1 for the orphan item deletion. |
| 4775 | */ |
| 4776 | trans = btrfs_start_transaction(root, 2); |
| 4777 | if (IS_ERR(trans)) |
| 4778 | return PTR_ERR(trans); |
| 4779 | |
| 4780 | /* |
| 4781 | * We need to do this in case we fail at _any_ point during the |
| 4782 | * actual truncate. Once we do the truncate_setsize we could |
| 4783 | * invalidate pages which forces any outstanding ordered io to |
| 4784 | * be instantly completed which will give us extents that need |
| 4785 | * to be truncated. If we fail to get an orphan inode down we |
| 4786 | * could have left over extents that were never meant to live, |
| 4787 | * so we need to garuntee from this point on that everything |
| 4788 | * will be consistent. |
| 4789 | */ |
| 4790 | ret = btrfs_orphan_add(trans, inode); |
| 4791 | btrfs_end_transaction(trans, root); |
| 4792 | if (ret) |
| 4793 | return ret; |
| 4794 | |
Josef Bacik | a41ad39 | 2011-01-31 15:30:16 -0500 | [diff] [blame] | 4795 | /* we don't support swapfiles, so vmtruncate shouldn't fail */ |
| 4796 | truncate_setsize(inode, newsize); |
Miao Xie | 2e60a51 | 2013-02-08 07:01:08 +0000 | [diff] [blame] | 4797 | |
| 4798 | /* Disable nonlocked read DIO to avoid the end less truncate */ |
| 4799 | btrfs_inode_block_unlocked_dio(inode); |
| 4800 | inode_dio_wait(inode); |
| 4801 | btrfs_inode_resume_unlocked_dio(inode); |
| 4802 | |
Josef Bacik | a41ad39 | 2011-01-31 15:30:16 -0500 | [diff] [blame] | 4803 | ret = btrfs_truncate(inode); |
Josef Bacik | 7f4f6e0 | 2013-08-29 16:43:28 -0400 | [diff] [blame] | 4804 | if (ret && inode->i_nlink) { |
| 4805 | int err; |
| 4806 | |
| 4807 | /* |
| 4808 | * failed to truncate, disk_i_size is only adjusted down |
| 4809 | * as we remove extents, so it should represent the true |
| 4810 | * size of the inode, so reset the in memory size and |
| 4811 | * delete our orphan entry. |
| 4812 | */ |
| 4813 | trans = btrfs_join_transaction(root); |
| 4814 | if (IS_ERR(trans)) { |
| 4815 | btrfs_orphan_del(NULL, inode); |
| 4816 | return ret; |
| 4817 | } |
| 4818 | i_size_write(inode, BTRFS_I(inode)->disk_i_size); |
| 4819 | err = btrfs_orphan_del(trans, inode); |
| 4820 | if (err) |
| 4821 | btrfs_abort_transaction(trans, root, err); |
| 4822 | btrfs_end_transaction(trans, root); |
| 4823 | } |
Yan, Zheng | 8082510 | 2009-11-12 09:35:36 +0000 | [diff] [blame] | 4824 | } |
| 4825 | |
Josef Bacik | a41ad39 | 2011-01-31 15:30:16 -0500 | [diff] [blame] | 4826 | return ret; |
Yan, Zheng | 8082510 | 2009-11-12 09:35:36 +0000 | [diff] [blame] | 4827 | } |
| 4828 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4829 | static int btrfs_setattr(struct dentry *dentry, struct iattr *attr) |
| 4830 | { |
| 4831 | struct inode *inode = dentry->d_inode; |
Li Zefan | b83cc96 | 2010-12-20 16:04:08 +0800 | [diff] [blame] | 4832 | struct btrfs_root *root = BTRFS_I(inode)->root; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4833 | int err; |
| 4834 | |
Li Zefan | b83cc96 | 2010-12-20 16:04:08 +0800 | [diff] [blame] | 4835 | if (btrfs_root_readonly(root)) |
| 4836 | return -EROFS; |
| 4837 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4838 | err = inode_change_ok(inode, attr); |
| 4839 | if (err) |
| 4840 | return err; |
| 4841 | |
Chris Mason | 5a3f23d | 2009-03-31 13:27:11 -0400 | [diff] [blame] | 4842 | if (S_ISREG(inode->i_mode) && (attr->ia_valid & ATTR_SIZE)) { |
Eric Sandeen | 3972f26 | 2013-01-12 02:57:22 +0000 | [diff] [blame] | 4843 | err = btrfs_setsize(inode, attr); |
Yan, Zheng | 8082510 | 2009-11-12 09:35:36 +0000 | [diff] [blame] | 4844 | if (err) |
| 4845 | return err; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4846 | } |
Yan Zheng | 9036c10 | 2008-10-30 14:19:41 -0400 | [diff] [blame] | 4847 | |
Christoph Hellwig | 1025774 | 2010-06-04 11:30:02 +0200 | [diff] [blame] | 4848 | if (attr->ia_valid) { |
| 4849 | setattr_copy(inode, attr); |
Josef Bacik | 0c4d2d9 | 2012-04-05 15:03:02 -0400 | [diff] [blame] | 4850 | inode_inc_iversion(inode); |
Josef Bacik | 22c44fe | 2011-11-30 10:45:38 -0500 | [diff] [blame] | 4851 | err = btrfs_dirty_inode(inode); |
Josef Bacik | 33268ea | 2008-07-24 12:16:36 -0400 | [diff] [blame] | 4852 | |
Josef Bacik | 22c44fe | 2011-11-30 10:45:38 -0500 | [diff] [blame] | 4853 | if (!err && attr->ia_valid & ATTR_MODE) |
Christoph Hellwig | 996a710 | 2013-12-20 05:16:43 -0800 | [diff] [blame] | 4854 | err = posix_acl_chmod(inode, inode->i_mode); |
Christoph Hellwig | 1025774 | 2010-06-04 11:30:02 +0200 | [diff] [blame] | 4855 | } |
| 4856 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4857 | return err; |
| 4858 | } |
Chris Mason | 61295eb | 2008-01-14 16:24:38 -0500 | [diff] [blame] | 4859 | |
Filipe David Borba Manana | 131e404 | 2013-11-19 22:29:35 +0000 | [diff] [blame] | 4860 | /* |
| 4861 | * While truncating the inode pages during eviction, we get the VFS calling |
| 4862 | * btrfs_invalidatepage() against each page of the inode. This is slow because |
| 4863 | * the calls to btrfs_invalidatepage() result in a huge amount of calls to |
| 4864 | * lock_extent_bits() and clear_extent_bit(), which keep merging and splitting |
| 4865 | * extent_state structures over and over, wasting lots of time. |
| 4866 | * |
| 4867 | * Therefore if the inode is being evicted, let btrfs_invalidatepage() skip all |
| 4868 | * those expensive operations on a per page basis and do only the ordered io |
| 4869 | * finishing, while we release here the extent_map and extent_state structures, |
| 4870 | * without the excessive merging and splitting. |
| 4871 | */ |
| 4872 | static void evict_inode_truncate_pages(struct inode *inode) |
| 4873 | { |
| 4874 | struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree; |
| 4875 | struct extent_map_tree *map_tree = &BTRFS_I(inode)->extent_tree; |
| 4876 | struct rb_node *node; |
| 4877 | |
| 4878 | ASSERT(inode->i_state & I_FREEING); |
Johannes Weiner | 91b0abe | 2014-04-03 14:47:49 -0700 | [diff] [blame] | 4879 | truncate_inode_pages_final(&inode->i_data); |
Filipe David Borba Manana | 131e404 | 2013-11-19 22:29:35 +0000 | [diff] [blame] | 4880 | |
| 4881 | write_lock(&map_tree->lock); |
| 4882 | while (!RB_EMPTY_ROOT(&map_tree->map)) { |
| 4883 | struct extent_map *em; |
| 4884 | |
| 4885 | node = rb_first(&map_tree->map); |
| 4886 | em = rb_entry(node, struct extent_map, rb_node); |
Wang Shilong | 180589e | 2013-12-14 15:27:31 +0800 | [diff] [blame] | 4887 | clear_bit(EXTENT_FLAG_PINNED, &em->flags); |
| 4888 | clear_bit(EXTENT_FLAG_LOGGING, &em->flags); |
Filipe David Borba Manana | 131e404 | 2013-11-19 22:29:35 +0000 | [diff] [blame] | 4889 | remove_extent_mapping(map_tree, em); |
| 4890 | free_extent_map(em); |
Filipe Manana | 7064dd5 | 2014-08-08 02:47:05 +0100 | [diff] [blame] | 4891 | if (need_resched()) { |
| 4892 | write_unlock(&map_tree->lock); |
| 4893 | cond_resched(); |
| 4894 | write_lock(&map_tree->lock); |
| 4895 | } |
Filipe David Borba Manana | 131e404 | 2013-11-19 22:29:35 +0000 | [diff] [blame] | 4896 | } |
| 4897 | write_unlock(&map_tree->lock); |
| 4898 | |
| 4899 | spin_lock(&io_tree->lock); |
| 4900 | while (!RB_EMPTY_ROOT(&io_tree->state)) { |
| 4901 | struct extent_state *state; |
| 4902 | struct extent_state *cached_state = NULL; |
| 4903 | |
| 4904 | node = rb_first(&io_tree->state); |
| 4905 | state = rb_entry(node, struct extent_state, rb_node); |
| 4906 | atomic_inc(&state->refs); |
| 4907 | spin_unlock(&io_tree->lock); |
| 4908 | |
| 4909 | lock_extent_bits(io_tree, state->start, state->end, |
| 4910 | 0, &cached_state); |
| 4911 | clear_extent_bit(io_tree, state->start, state->end, |
| 4912 | EXTENT_LOCKED | EXTENT_DIRTY | |
| 4913 | EXTENT_DELALLOC | EXTENT_DO_ACCOUNTING | |
| 4914 | EXTENT_DEFRAG, 1, 1, |
| 4915 | &cached_state, GFP_NOFS); |
| 4916 | free_extent_state(state); |
| 4917 | |
Filipe Manana | 7064dd5 | 2014-08-08 02:47:05 +0100 | [diff] [blame] | 4918 | cond_resched(); |
Filipe David Borba Manana | 131e404 | 2013-11-19 22:29:35 +0000 | [diff] [blame] | 4919 | spin_lock(&io_tree->lock); |
| 4920 | } |
| 4921 | spin_unlock(&io_tree->lock); |
| 4922 | } |
| 4923 | |
Al Viro | bd55597 | 2010-06-07 11:35:40 -0400 | [diff] [blame] | 4924 | void btrfs_evict_inode(struct inode *inode) |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4925 | { |
| 4926 | struct btrfs_trans_handle *trans; |
| 4927 | struct btrfs_root *root = BTRFS_I(inode)->root; |
Josef Bacik | 726c35f | 2011-09-26 15:46:06 -0400 | [diff] [blame] | 4928 | struct btrfs_block_rsv *rsv, *global_rsv; |
Josef Bacik | 0712718 | 2011-08-19 10:29:59 -0400 | [diff] [blame] | 4929 | u64 min_size = btrfs_calc_trunc_metadata_size(root, 1); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4930 | int ret; |
| 4931 | |
liubo | 1abe9b8 | 2011-03-24 11:18:59 +0000 | [diff] [blame] | 4932 | trace_btrfs_inode_evict(inode); |
| 4933 | |
Filipe David Borba Manana | 131e404 | 2013-11-19 22:29:35 +0000 | [diff] [blame] | 4934 | evict_inode_truncate_pages(inode); |
| 4935 | |
Stefan Behrens | 69e9c6c | 2013-09-05 16:58:43 +0200 | [diff] [blame] | 4936 | if (inode->i_nlink && |
| 4937 | ((btrfs_root_refs(&root->root_item) != 0 && |
| 4938 | root->root_key.objectid != BTRFS_ROOT_TREE_OBJECTID) || |
| 4939 | btrfs_is_free_space_inode(inode))) |
Al Viro | bd55597 | 2010-06-07 11:35:40 -0400 | [diff] [blame] | 4940 | goto no_delete; |
| 4941 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4942 | if (is_bad_inode(inode)) { |
Josef Bacik | 7b12876 | 2008-07-24 12:17:14 -0400 | [diff] [blame] | 4943 | btrfs_orphan_del(NULL, inode); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4944 | goto no_delete; |
| 4945 | } |
Al Viro | bd55597 | 2010-06-07 11:35:40 -0400 | [diff] [blame] | 4946 | /* 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] | 4947 | btrfs_wait_ordered_range(inode, 0, (u64)-1); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 4948 | |
Miao Xie | f612496 | 2014-09-12 18:44:04 +0800 | [diff] [blame] | 4949 | btrfs_free_io_failure_record(inode, 0, (u64)-1); |
| 4950 | |
Yan, Zheng | c71bf09 | 2009-11-12 09:34:40 +0000 | [diff] [blame] | 4951 | if (root->fs_info->log_root_recovering) { |
Liu Bo | 6bf0231 | 2012-06-25 21:59:09 -0600 | [diff] [blame] | 4952 | BUG_ON(test_bit(BTRFS_INODE_HAS_ORPHAN_ITEM, |
Josef Bacik | 8a35d95 | 2012-05-23 14:26:42 -0400 | [diff] [blame] | 4953 | &BTRFS_I(inode)->runtime_flags)); |
Yan, Zheng | c71bf09 | 2009-11-12 09:34:40 +0000 | [diff] [blame] | 4954 | goto no_delete; |
| 4955 | } |
| 4956 | |
Yan, Zheng | 76dda93 | 2009-09-21 16:00:26 -0400 | [diff] [blame] | 4957 | if (inode->i_nlink > 0) { |
Stefan Behrens | 69e9c6c | 2013-09-05 16:58:43 +0200 | [diff] [blame] | 4958 | BUG_ON(btrfs_root_refs(&root->root_item) != 0 && |
| 4959 | root->root_key.objectid != BTRFS_ROOT_TREE_OBJECTID); |
Yan, Zheng | 76dda93 | 2009-09-21 16:00:26 -0400 | [diff] [blame] | 4960 | goto no_delete; |
| 4961 | } |
| 4962 | |
Miao Xie | 0e8c36a | 2012-12-19 06:59:51 +0000 | [diff] [blame] | 4963 | ret = btrfs_commit_inode_delayed_inode(inode); |
| 4964 | if (ret) { |
| 4965 | btrfs_orphan_del(NULL, inode); |
| 4966 | goto no_delete; |
| 4967 | } |
| 4968 | |
Miao Xie | 66d8f3d | 2012-09-06 04:02:28 -0600 | [diff] [blame] | 4969 | rsv = btrfs_alloc_block_rsv(root, BTRFS_BLOCK_RSV_TEMP); |
Josef Bacik | 4289a66 | 2011-08-05 13:22:24 -0400 | [diff] [blame] | 4970 | if (!rsv) { |
| 4971 | btrfs_orphan_del(NULL, inode); |
| 4972 | goto no_delete; |
| 4973 | } |
Josef Bacik | 4a33854 | 2011-08-29 11:01:31 -0400 | [diff] [blame] | 4974 | rsv->size = min_size; |
Josef Bacik | ca7e70f | 2012-08-27 17:48:15 -0400 | [diff] [blame] | 4975 | rsv->failfast = 1; |
Josef Bacik | 726c35f | 2011-09-26 15:46:06 -0400 | [diff] [blame] | 4976 | global_rsv = &root->fs_info->global_block_rsv; |
Josef Bacik | 4289a66 | 2011-08-05 13:22:24 -0400 | [diff] [blame] | 4977 | |
Chris Mason | dbe674a | 2008-07-17 12:54:05 -0400 | [diff] [blame] | 4978 | btrfs_i_size_write(inode, 0); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 4979 | |
Josef Bacik | 4289a66 | 2011-08-05 13:22:24 -0400 | [diff] [blame] | 4980 | /* |
Miao Xie | 8407aa4 | 2012-09-07 01:43:32 -0600 | [diff] [blame] | 4981 | * This is a bit simpler than btrfs_truncate since we've already |
| 4982 | * reserved our space for our orphan item in the unlink, so we just |
| 4983 | * need to reserve some slack space in case we add bytes and update |
| 4984 | * inode item when doing the truncate. |
Josef Bacik | 4289a66 | 2011-08-05 13:22:24 -0400 | [diff] [blame] | 4985 | */ |
Yan, Zheng | 8082510 | 2009-11-12 09:35:36 +0000 | [diff] [blame] | 4986 | while (1) { |
Miao Xie | 08e007d | 2012-10-16 11:33:38 +0000 | [diff] [blame] | 4987 | ret = btrfs_block_rsv_refill(root, rsv, min_size, |
| 4988 | BTRFS_RESERVE_FLUSH_LIMIT); |
Yan, Zheng | 8082510 | 2009-11-12 09:35:36 +0000 | [diff] [blame] | 4989 | |
Josef Bacik | 726c35f | 2011-09-26 15:46:06 -0400 | [diff] [blame] | 4990 | /* |
| 4991 | * Try and steal from the global reserve since we will |
| 4992 | * likely not use this space anyway, we want to try as |
| 4993 | * hard as possible to get this to work. |
| 4994 | */ |
| 4995 | if (ret) |
| 4996 | ret = btrfs_block_rsv_migrate(global_rsv, rsv, min_size); |
| 4997 | |
Yan, Zheng | d68fc57 | 2010-05-16 10:49:58 -0400 | [diff] [blame] | 4998 | if (ret) { |
Simon Kirby | c2cf52e | 2013-03-19 22:41:23 +0000 | [diff] [blame] | 4999 | btrfs_warn(root->fs_info, |
| 5000 | "Could not get space for a delete, will truncate on mount %d", |
| 5001 | ret); |
Josef Bacik | 4289a66 | 2011-08-05 13:22:24 -0400 | [diff] [blame] | 5002 | btrfs_orphan_del(NULL, inode); |
| 5003 | btrfs_free_block_rsv(root, rsv); |
| 5004 | goto no_delete; |
Yan, Zheng | d68fc57 | 2010-05-16 10:49:58 -0400 | [diff] [blame] | 5005 | } |
| 5006 | |
Miao Xie | 0e8c36a | 2012-12-19 06:59:51 +0000 | [diff] [blame] | 5007 | trans = btrfs_join_transaction(root); |
Josef Bacik | 4289a66 | 2011-08-05 13:22:24 -0400 | [diff] [blame] | 5008 | if (IS_ERR(trans)) { |
| 5009 | btrfs_orphan_del(NULL, inode); |
| 5010 | btrfs_free_block_rsv(root, rsv); |
| 5011 | goto no_delete; |
| 5012 | } |
| 5013 | |
| 5014 | trans->block_rsv = rsv; |
| 5015 | |
Yan, Zheng | d68fc57 | 2010-05-16 10:49:58 -0400 | [diff] [blame] | 5016 | ret = btrfs_truncate_inode_items(trans, root, inode, 0, 0); |
Josef Bacik | ca7e70f | 2012-08-27 17:48:15 -0400 | [diff] [blame] | 5017 | if (ret != -ENOSPC) |
Yan, Zheng | 8082510 | 2009-11-12 09:35:36 +0000 | [diff] [blame] | 5018 | break; |
| 5019 | |
Miao Xie | 8407aa4 | 2012-09-07 01:43:32 -0600 | [diff] [blame] | 5020 | trans->block_rsv = &root->fs_info->trans_block_rsv; |
Yan, Zheng | 8082510 | 2009-11-12 09:35:36 +0000 | [diff] [blame] | 5021 | btrfs_end_transaction(trans, root); |
| 5022 | trans = NULL; |
Liu Bo | b53d3f5 | 2012-11-14 14:34:34 +0000 | [diff] [blame] | 5023 | btrfs_btree_balance_dirty(root); |
Josef Bacik | 7b12876 | 2008-07-24 12:17:14 -0400 | [diff] [blame] | 5024 | } |
| 5025 | |
Josef Bacik | 4289a66 | 2011-08-05 13:22:24 -0400 | [diff] [blame] | 5026 | btrfs_free_block_rsv(root, rsv); |
| 5027 | |
Josef Bacik | 4ef31a4 | 2013-08-13 14:10:08 -0400 | [diff] [blame] | 5028 | /* |
| 5029 | * Errors here aren't a big deal, it just means we leave orphan items |
| 5030 | * in the tree. They will be cleaned up on the next mount. |
| 5031 | */ |
Yan, Zheng | 8082510 | 2009-11-12 09:35:36 +0000 | [diff] [blame] | 5032 | if (ret == 0) { |
Josef Bacik | 4289a66 | 2011-08-05 13:22:24 -0400 | [diff] [blame] | 5033 | trans->block_rsv = root->orphan_block_rsv; |
Josef Bacik | 4ef31a4 | 2013-08-13 14:10:08 -0400 | [diff] [blame] | 5034 | btrfs_orphan_del(trans, inode); |
| 5035 | } else { |
| 5036 | btrfs_orphan_del(NULL, inode); |
Yan, Zheng | 8082510 | 2009-11-12 09:35:36 +0000 | [diff] [blame] | 5037 | } |
Chris Mason | 85e21ba | 2008-01-29 15:11:36 -0500 | [diff] [blame] | 5038 | |
Josef Bacik | 4289a66 | 2011-08-05 13:22:24 -0400 | [diff] [blame] | 5039 | trans->block_rsv = &root->fs_info->trans_block_rsv; |
Li Zefan | 581bb05 | 2011-04-20 10:06:11 +0800 | [diff] [blame] | 5040 | if (!(root == root->fs_info->tree_root || |
| 5041 | root->root_key.objectid == BTRFS_TREE_RELOC_OBJECTID)) |
Li Zefan | 33345d01 | 2011-04-20 10:31:50 +0800 | [diff] [blame] | 5042 | btrfs_return_ino(root, btrfs_ino(inode)); |
Li Zefan | 581bb05 | 2011-04-20 10:06:11 +0800 | [diff] [blame] | 5043 | |
Chris Mason | 54aa1f4 | 2007-06-22 14:16:25 -0400 | [diff] [blame] | 5044 | btrfs_end_transaction(trans, root); |
Liu Bo | b53d3f5 | 2012-11-14 14:34:34 +0000 | [diff] [blame] | 5045 | btrfs_btree_balance_dirty(root); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5046 | no_delete: |
Miao Xie | 89042e5 | 2013-05-15 07:48:15 +0000 | [diff] [blame] | 5047 | btrfs_remove_delayed_node(inode); |
Jan Kara | dbd5768 | 2012-05-03 14:48:02 +0200 | [diff] [blame] | 5048 | clear_inode(inode); |
Yan, Zheng | 8082510 | 2009-11-12 09:35:36 +0000 | [diff] [blame] | 5049 | return; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5050 | } |
| 5051 | |
| 5052 | /* |
| 5053 | * this returns the key found in the dir entry in the location pointer. |
| 5054 | * If no dir entries were found, location->objectid is 0. |
| 5055 | */ |
| 5056 | static int btrfs_inode_by_name(struct inode *dir, struct dentry *dentry, |
| 5057 | struct btrfs_key *location) |
| 5058 | { |
| 5059 | const char *name = dentry->d_name.name; |
| 5060 | int namelen = dentry->d_name.len; |
| 5061 | struct btrfs_dir_item *di; |
| 5062 | struct btrfs_path *path; |
| 5063 | struct btrfs_root *root = BTRFS_I(dir)->root; |
Yan | 0d9f7f3 | 2007-10-25 15:48:28 -0400 | [diff] [blame] | 5064 | int ret = 0; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5065 | |
| 5066 | path = btrfs_alloc_path(); |
Mark Fasheh | d8926bb | 2011-07-13 10:38:47 -0700 | [diff] [blame] | 5067 | if (!path) |
| 5068 | return -ENOMEM; |
Chris Mason | 3954401 | 2007-12-12 14:38:19 -0500 | [diff] [blame] | 5069 | |
Li Zefan | 33345d01 | 2011-04-20 10:31:50 +0800 | [diff] [blame] | 5070 | di = btrfs_lookup_dir_item(NULL, root, path, btrfs_ino(dir), name, |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5071 | namelen, 0); |
Yan | 0d9f7f3 | 2007-10-25 15:48:28 -0400 | [diff] [blame] | 5072 | if (IS_ERR(di)) |
| 5073 | ret = PTR_ERR(di); |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 5074 | |
David Sterba | c704005 | 2011-04-19 18:00:01 +0200 | [diff] [blame] | 5075 | if (IS_ERR_OR_NULL(di)) |
Chris Mason | 3954401 | 2007-12-12 14:38:19 -0500 | [diff] [blame] | 5076 | goto out_err; |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 5077 | |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 5078 | btrfs_dir_item_key_to_cpu(path->nodes[0], di, location); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5079 | out: |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5080 | btrfs_free_path(path); |
| 5081 | return ret; |
Chris Mason | 3954401 | 2007-12-12 14:38:19 -0500 | [diff] [blame] | 5082 | out_err: |
| 5083 | location->objectid = 0; |
| 5084 | goto out; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5085 | } |
| 5086 | |
| 5087 | /* |
| 5088 | * when we hit a tree root in a directory, the btrfs part of the inode |
| 5089 | * needs to be changed to reflect the root directory of the tree root. This |
| 5090 | * is kind of like crossing a mount point. |
| 5091 | */ |
| 5092 | static int fixup_tree_root_location(struct btrfs_root *root, |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 5093 | struct inode *dir, |
| 5094 | struct dentry *dentry, |
| 5095 | struct btrfs_key *location, |
| 5096 | struct btrfs_root **sub_root) |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5097 | { |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 5098 | struct btrfs_path *path; |
| 5099 | struct btrfs_root *new_root; |
| 5100 | struct btrfs_root_ref *ref; |
| 5101 | struct extent_buffer *leaf; |
David Sterba | 1d4c08e | 2015-01-02 19:36:14 +0100 | [diff] [blame] | 5102 | struct btrfs_key key; |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 5103 | int ret; |
| 5104 | int err = 0; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5105 | |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 5106 | path = btrfs_alloc_path(); |
| 5107 | if (!path) { |
| 5108 | err = -ENOMEM; |
| 5109 | goto out; |
| 5110 | } |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5111 | |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 5112 | err = -ENOENT; |
David Sterba | 1d4c08e | 2015-01-02 19:36:14 +0100 | [diff] [blame] | 5113 | key.objectid = BTRFS_I(dir)->root->root_key.objectid; |
| 5114 | key.type = BTRFS_ROOT_REF_KEY; |
| 5115 | key.offset = location->objectid; |
| 5116 | |
| 5117 | ret = btrfs_search_slot(NULL, root->fs_info->tree_root, &key, path, |
| 5118 | 0, 0); |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 5119 | if (ret) { |
| 5120 | if (ret < 0) |
| 5121 | err = ret; |
| 5122 | goto out; |
| 5123 | } |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5124 | |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 5125 | leaf = path->nodes[0]; |
| 5126 | ref = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_root_ref); |
Li Zefan | 33345d01 | 2011-04-20 10:31:50 +0800 | [diff] [blame] | 5127 | if (btrfs_root_ref_dirid(leaf, ref) != btrfs_ino(dir) || |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 5128 | btrfs_root_ref_name_len(leaf, ref) != dentry->d_name.len) |
| 5129 | goto out; |
| 5130 | |
| 5131 | ret = memcmp_extent_buffer(leaf, dentry->d_name.name, |
| 5132 | (unsigned long)(ref + 1), |
| 5133 | dentry->d_name.len); |
| 5134 | if (ret) |
| 5135 | goto out; |
| 5136 | |
David Sterba | b3b4aa7 | 2011-04-21 01:20:15 +0200 | [diff] [blame] | 5137 | btrfs_release_path(path); |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 5138 | |
| 5139 | new_root = btrfs_read_fs_root_no_name(root->fs_info, location); |
| 5140 | if (IS_ERR(new_root)) { |
| 5141 | err = PTR_ERR(new_root); |
| 5142 | goto out; |
| 5143 | } |
| 5144 | |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 5145 | *sub_root = new_root; |
| 5146 | location->objectid = btrfs_root_dirid(&new_root->root_item); |
| 5147 | location->type = BTRFS_INODE_ITEM_KEY; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5148 | location->offset = 0; |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 5149 | err = 0; |
| 5150 | out: |
| 5151 | btrfs_free_path(path); |
| 5152 | return err; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5153 | } |
| 5154 | |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 5155 | static void inode_tree_add(struct inode *inode) |
| 5156 | { |
| 5157 | struct btrfs_root *root = BTRFS_I(inode)->root; |
| 5158 | struct btrfs_inode *entry; |
From: Nick Piggin | 03e860b | 2009-08-21 10:09:44 +0200 | [diff] [blame] | 5159 | struct rb_node **p; |
| 5160 | struct rb_node *parent; |
Filipe David Borba Manana | cef2193 | 2013-09-02 12:19:13 +0100 | [diff] [blame] | 5161 | struct rb_node *new = &BTRFS_I(inode)->rb_node; |
Li Zefan | 33345d01 | 2011-04-20 10:31:50 +0800 | [diff] [blame] | 5162 | u64 ino = btrfs_ino(inode); |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 5163 | |
Al Viro | 1d3382cb | 2010-10-23 15:19:20 -0400 | [diff] [blame] | 5164 | if (inode_unhashed(inode)) |
Yan, Zheng | 76dda93 | 2009-09-21 16:00:26 -0400 | [diff] [blame] | 5165 | return; |
Miao Xie | e1409ce | 2013-05-15 07:48:16 +0000 | [diff] [blame] | 5166 | parent = NULL; |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 5167 | spin_lock(&root->inode_lock); |
Miao Xie | e1409ce | 2013-05-15 07:48:16 +0000 | [diff] [blame] | 5168 | p = &root->inode_tree.rb_node; |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 5169 | while (*p) { |
| 5170 | parent = *p; |
| 5171 | entry = rb_entry(parent, struct btrfs_inode, rb_node); |
| 5172 | |
Li Zefan | 33345d01 | 2011-04-20 10:31:50 +0800 | [diff] [blame] | 5173 | if (ino < btrfs_ino(&entry->vfs_inode)) |
From: Nick Piggin | 03e860b | 2009-08-21 10:09:44 +0200 | [diff] [blame] | 5174 | p = &parent->rb_left; |
Li Zefan | 33345d01 | 2011-04-20 10:31:50 +0800 | [diff] [blame] | 5175 | else if (ino > btrfs_ino(&entry->vfs_inode)) |
From: Nick Piggin | 03e860b | 2009-08-21 10:09:44 +0200 | [diff] [blame] | 5176 | p = &parent->rb_right; |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 5177 | else { |
| 5178 | WARN_ON(!(entry->vfs_inode.i_state & |
Al Viro | a4ffdde | 2010-06-02 17:38:30 -0400 | [diff] [blame] | 5179 | (I_WILL_FREE | I_FREEING))); |
Filipe David Borba Manana | cef2193 | 2013-09-02 12:19:13 +0100 | [diff] [blame] | 5180 | rb_replace_node(parent, new, &root->inode_tree); |
From: Nick Piggin | 03e860b | 2009-08-21 10:09:44 +0200 | [diff] [blame] | 5181 | RB_CLEAR_NODE(parent); |
| 5182 | spin_unlock(&root->inode_lock); |
Filipe David Borba Manana | cef2193 | 2013-09-02 12:19:13 +0100 | [diff] [blame] | 5183 | return; |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 5184 | } |
| 5185 | } |
Filipe David Borba Manana | cef2193 | 2013-09-02 12:19:13 +0100 | [diff] [blame] | 5186 | rb_link_node(new, parent, p); |
| 5187 | rb_insert_color(new, &root->inode_tree); |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 5188 | spin_unlock(&root->inode_lock); |
| 5189 | } |
| 5190 | |
| 5191 | static void inode_tree_del(struct inode *inode) |
| 5192 | { |
| 5193 | struct btrfs_root *root = BTRFS_I(inode)->root; |
Yan, Zheng | 76dda93 | 2009-09-21 16:00:26 -0400 | [diff] [blame] | 5194 | int empty = 0; |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 5195 | |
From: Nick Piggin | 03e860b | 2009-08-21 10:09:44 +0200 | [diff] [blame] | 5196 | spin_lock(&root->inode_lock); |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 5197 | if (!RB_EMPTY_NODE(&BTRFS_I(inode)->rb_node)) { |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 5198 | rb_erase(&BTRFS_I(inode)->rb_node, &root->inode_tree); |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 5199 | RB_CLEAR_NODE(&BTRFS_I(inode)->rb_node); |
Yan, Zheng | 76dda93 | 2009-09-21 16:00:26 -0400 | [diff] [blame] | 5200 | empty = RB_EMPTY_ROOT(&root->inode_tree); |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 5201 | } |
From: Nick Piggin | 03e860b | 2009-08-21 10:09:44 +0200 | [diff] [blame] | 5202 | spin_unlock(&root->inode_lock); |
Yan, Zheng | 76dda93 | 2009-09-21 16:00:26 -0400 | [diff] [blame] | 5203 | |
Stefan Behrens | 69e9c6c | 2013-09-05 16:58:43 +0200 | [diff] [blame] | 5204 | if (empty && btrfs_root_refs(&root->root_item) == 0) { |
Yan, Zheng | 76dda93 | 2009-09-21 16:00:26 -0400 | [diff] [blame] | 5205 | synchronize_srcu(&root->fs_info->subvol_srcu); |
| 5206 | spin_lock(&root->inode_lock); |
| 5207 | empty = RB_EMPTY_ROOT(&root->inode_tree); |
| 5208 | spin_unlock(&root->inode_lock); |
| 5209 | if (empty) |
| 5210 | btrfs_add_dead_root(root); |
| 5211 | } |
| 5212 | } |
| 5213 | |
Jeff Mahoney | 143bede | 2012-03-01 14:56:26 +0100 | [diff] [blame] | 5214 | void btrfs_invalidate_inodes(struct btrfs_root *root) |
Yan, Zheng | 76dda93 | 2009-09-21 16:00:26 -0400 | [diff] [blame] | 5215 | { |
| 5216 | struct rb_node *node; |
| 5217 | struct rb_node *prev; |
| 5218 | struct btrfs_inode *entry; |
| 5219 | struct inode *inode; |
| 5220 | u64 objectid = 0; |
| 5221 | |
Liu Bo | 7813b3d | 2014-02-10 17:37:25 +0800 | [diff] [blame] | 5222 | if (!test_bit(BTRFS_FS_STATE_ERROR, &root->fs_info->fs_state)) |
| 5223 | WARN_ON(btrfs_root_refs(&root->root_item) != 0); |
Yan, Zheng | 76dda93 | 2009-09-21 16:00:26 -0400 | [diff] [blame] | 5224 | |
| 5225 | spin_lock(&root->inode_lock); |
| 5226 | again: |
| 5227 | node = root->inode_tree.rb_node; |
| 5228 | prev = NULL; |
| 5229 | while (node) { |
| 5230 | prev = node; |
| 5231 | entry = rb_entry(node, struct btrfs_inode, rb_node); |
| 5232 | |
Li Zefan | 33345d01 | 2011-04-20 10:31:50 +0800 | [diff] [blame] | 5233 | if (objectid < btrfs_ino(&entry->vfs_inode)) |
Yan, Zheng | 76dda93 | 2009-09-21 16:00:26 -0400 | [diff] [blame] | 5234 | node = node->rb_left; |
Li Zefan | 33345d01 | 2011-04-20 10:31:50 +0800 | [diff] [blame] | 5235 | else if (objectid > btrfs_ino(&entry->vfs_inode)) |
Yan, Zheng | 76dda93 | 2009-09-21 16:00:26 -0400 | [diff] [blame] | 5236 | node = node->rb_right; |
| 5237 | else |
| 5238 | break; |
| 5239 | } |
| 5240 | if (!node) { |
| 5241 | while (prev) { |
| 5242 | entry = rb_entry(prev, struct btrfs_inode, rb_node); |
Li Zefan | 33345d01 | 2011-04-20 10:31:50 +0800 | [diff] [blame] | 5243 | if (objectid <= btrfs_ino(&entry->vfs_inode)) { |
Yan, Zheng | 76dda93 | 2009-09-21 16:00:26 -0400 | [diff] [blame] | 5244 | node = prev; |
| 5245 | break; |
| 5246 | } |
| 5247 | prev = rb_next(prev); |
| 5248 | } |
| 5249 | } |
| 5250 | while (node) { |
| 5251 | entry = rb_entry(node, struct btrfs_inode, rb_node); |
Li Zefan | 33345d01 | 2011-04-20 10:31:50 +0800 | [diff] [blame] | 5252 | objectid = btrfs_ino(&entry->vfs_inode) + 1; |
Yan, Zheng | 76dda93 | 2009-09-21 16:00:26 -0400 | [diff] [blame] | 5253 | inode = igrab(&entry->vfs_inode); |
| 5254 | if (inode) { |
| 5255 | spin_unlock(&root->inode_lock); |
| 5256 | if (atomic_read(&inode->i_count) > 1) |
| 5257 | d_prune_aliases(inode); |
| 5258 | /* |
Al Viro | 45321ac | 2010-06-07 13:43:19 -0400 | [diff] [blame] | 5259 | * btrfs_drop_inode will have it removed from |
Yan, Zheng | 76dda93 | 2009-09-21 16:00:26 -0400 | [diff] [blame] | 5260 | * the inode cache when its usage count |
| 5261 | * hits zero. |
| 5262 | */ |
| 5263 | iput(inode); |
| 5264 | cond_resched(); |
| 5265 | spin_lock(&root->inode_lock); |
| 5266 | goto again; |
| 5267 | } |
| 5268 | |
| 5269 | if (cond_resched_lock(&root->inode_lock)) |
| 5270 | goto again; |
| 5271 | |
| 5272 | node = rb_next(node); |
| 5273 | } |
| 5274 | spin_unlock(&root->inode_lock); |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 5275 | } |
| 5276 | |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 5277 | static int btrfs_init_locked_inode(struct inode *inode, void *p) |
| 5278 | { |
| 5279 | struct btrfs_iget_args *args = p; |
Chris Mason | 90d3e59 | 2014-01-09 17:28:00 -0800 | [diff] [blame] | 5280 | inode->i_ino = args->location->objectid; |
| 5281 | memcpy(&BTRFS_I(inode)->location, args->location, |
| 5282 | sizeof(*args->location)); |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 5283 | BTRFS_I(inode)->root = args->root; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5284 | return 0; |
| 5285 | } |
| 5286 | |
| 5287 | static int btrfs_find_actor(struct inode *inode, void *opaque) |
| 5288 | { |
| 5289 | struct btrfs_iget_args *args = opaque; |
Chris Mason | 90d3e59 | 2014-01-09 17:28:00 -0800 | [diff] [blame] | 5290 | return args->location->objectid == BTRFS_I(inode)->location.objectid && |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 5291 | args->root == BTRFS_I(inode)->root; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5292 | } |
| 5293 | |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 5294 | static struct inode *btrfs_iget_locked(struct super_block *s, |
Chris Mason | 90d3e59 | 2014-01-09 17:28:00 -0800 | [diff] [blame] | 5295 | struct btrfs_key *location, |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 5296 | struct btrfs_root *root) |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5297 | { |
| 5298 | struct inode *inode; |
| 5299 | struct btrfs_iget_args args; |
Chris Mason | 90d3e59 | 2014-01-09 17:28:00 -0800 | [diff] [blame] | 5300 | unsigned long hashval = btrfs_inode_hash(location->objectid, root); |
Filipe David Borba Manana | 778ba82 | 2013-10-06 22:22:33 +0100 | [diff] [blame] | 5301 | |
Chris Mason | 90d3e59 | 2014-01-09 17:28:00 -0800 | [diff] [blame] | 5302 | args.location = location; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5303 | args.root = root; |
| 5304 | |
Filipe David Borba Manana | 778ba82 | 2013-10-06 22:22:33 +0100 | [diff] [blame] | 5305 | inode = iget5_locked(s, hashval, btrfs_find_actor, |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5306 | btrfs_init_locked_inode, |
| 5307 | (void *)&args); |
| 5308 | return inode; |
| 5309 | } |
| 5310 | |
Balaji Rao | 1a54ef8 | 2008-07-21 02:01:04 +0530 | [diff] [blame] | 5311 | /* Get an inode object given its location and corresponding root. |
| 5312 | * Returns in *is_new if the inode was read from disk |
| 5313 | */ |
| 5314 | struct inode *btrfs_iget(struct super_block *s, struct btrfs_key *location, |
Josef Bacik | 73f7341 | 2009-12-04 17:38:27 +0000 | [diff] [blame] | 5315 | struct btrfs_root *root, int *new) |
Balaji Rao | 1a54ef8 | 2008-07-21 02:01:04 +0530 | [diff] [blame] | 5316 | { |
| 5317 | struct inode *inode; |
| 5318 | |
Chris Mason | 90d3e59 | 2014-01-09 17:28:00 -0800 | [diff] [blame] | 5319 | inode = btrfs_iget_locked(s, location, root); |
Balaji Rao | 1a54ef8 | 2008-07-21 02:01:04 +0530 | [diff] [blame] | 5320 | if (!inode) |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 5321 | return ERR_PTR(-ENOMEM); |
Balaji Rao | 1a54ef8 | 2008-07-21 02:01:04 +0530 | [diff] [blame] | 5322 | |
| 5323 | if (inode->i_state & I_NEW) { |
Balaji Rao | 1a54ef8 | 2008-07-21 02:01:04 +0530 | [diff] [blame] | 5324 | btrfs_read_locked_inode(inode); |
Mark Fasheh | 1748f84 | 2011-07-12 11:25:31 -0700 | [diff] [blame] | 5325 | if (!is_bad_inode(inode)) { |
| 5326 | inode_tree_add(inode); |
| 5327 | unlock_new_inode(inode); |
| 5328 | if (new) |
| 5329 | *new = 1; |
| 5330 | } else { |
Sergei Trofimovich | e0b6d65 | 2011-09-11 10:52:24 -0400 | [diff] [blame] | 5331 | unlock_new_inode(inode); |
| 5332 | iput(inode); |
| 5333 | inode = ERR_PTR(-ESTALE); |
Mark Fasheh | 1748f84 | 2011-07-12 11:25:31 -0700 | [diff] [blame] | 5334 | } |
| 5335 | } |
| 5336 | |
Balaji Rao | 1a54ef8 | 2008-07-21 02:01:04 +0530 | [diff] [blame] | 5337 | return inode; |
| 5338 | } |
| 5339 | |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 5340 | static struct inode *new_simple_dir(struct super_block *s, |
| 5341 | struct btrfs_key *key, |
| 5342 | struct btrfs_root *root) |
| 5343 | { |
| 5344 | struct inode *inode = new_inode(s); |
| 5345 | |
| 5346 | if (!inode) |
| 5347 | return ERR_PTR(-ENOMEM); |
| 5348 | |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 5349 | BTRFS_I(inode)->root = root; |
| 5350 | memcpy(&BTRFS_I(inode)->location, key, sizeof(*key)); |
Josef Bacik | 72ac3c0 | 2012-05-23 14:13:11 -0400 | [diff] [blame] | 5351 | set_bit(BTRFS_INODE_DUMMY, &BTRFS_I(inode)->runtime_flags); |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 5352 | |
| 5353 | inode->i_ino = BTRFS_EMPTY_SUBVOL_DIR_OBJECTID; |
Li Zefan | 848cce0 | 2012-02-21 17:04:28 +0800 | [diff] [blame] | 5354 | inode->i_op = &btrfs_dir_ro_inode_operations; |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 5355 | inode->i_fop = &simple_dir_operations; |
| 5356 | inode->i_mode = S_IFDIR | S_IRUGO | S_IWUSR | S_IXUGO; |
chandan r | 9cc97d6 | 2012-07-04 12:48:07 +0530 | [diff] [blame] | 5357 | inode->i_mtime = CURRENT_TIME; |
| 5358 | inode->i_atime = inode->i_mtime; |
| 5359 | inode->i_ctime = inode->i_mtime; |
| 5360 | BTRFS_I(inode)->i_otime = inode->i_mtime; |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 5361 | |
| 5362 | return inode; |
| 5363 | } |
| 5364 | |
Chris Mason | 3de4586 | 2008-11-17 21:02:50 -0500 | [diff] [blame] | 5365 | struct inode *btrfs_lookup_dentry(struct inode *dir, struct dentry *dentry) |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5366 | { |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 5367 | struct inode *inode; |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 5368 | struct btrfs_root *root = BTRFS_I(dir)->root; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5369 | struct btrfs_root *sub_root = root; |
| 5370 | struct btrfs_key location; |
Yan, Zheng | 76dda93 | 2009-09-21 16:00:26 -0400 | [diff] [blame] | 5371 | int index; |
Josef Bacik | b4aff1f | 2011-06-28 16:18:59 -0400 | [diff] [blame] | 5372 | int ret = 0; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5373 | |
| 5374 | if (dentry->d_name.len > BTRFS_NAME_LEN) |
| 5375 | return ERR_PTR(-ENAMETOOLONG); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 5376 | |
Jeff Layton | 39e3c95 | 2012-11-28 11:30:53 -0500 | [diff] [blame] | 5377 | ret = btrfs_inode_by_name(dir, dentry, &location); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5378 | if (ret < 0) |
| 5379 | return ERR_PTR(ret); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 5380 | |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 5381 | if (location.objectid == 0) |
Tsutomu Itoh | 5662344b3 | 2013-12-13 09:51:42 +0900 | [diff] [blame] | 5382 | return ERR_PTR(-ENOENT); |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 5383 | |
| 5384 | if (location.type == BTRFS_INODE_ITEM_KEY) { |
Josef Bacik | 73f7341 | 2009-12-04 17:38:27 +0000 | [diff] [blame] | 5385 | inode = btrfs_iget(dir->i_sb, &location, root, NULL); |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 5386 | return inode; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5387 | } |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 5388 | |
| 5389 | BUG_ON(location.type != BTRFS_ROOT_ITEM_KEY); |
| 5390 | |
Yan, Zheng | 76dda93 | 2009-09-21 16:00:26 -0400 | [diff] [blame] | 5391 | index = srcu_read_lock(&root->fs_info->subvol_srcu); |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 5392 | ret = fixup_tree_root_location(root, dir, dentry, |
| 5393 | &location, &sub_root); |
| 5394 | if (ret < 0) { |
| 5395 | if (ret != -ENOENT) |
| 5396 | inode = ERR_PTR(ret); |
| 5397 | else |
| 5398 | inode = new_simple_dir(dir->i_sb, &location, sub_root); |
| 5399 | } else { |
Josef Bacik | 73f7341 | 2009-12-04 17:38:27 +0000 | [diff] [blame] | 5400 | inode = btrfs_iget(dir->i_sb, &location, sub_root, NULL); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5401 | } |
Yan, Zheng | 76dda93 | 2009-09-21 16:00:26 -0400 | [diff] [blame] | 5402 | srcu_read_unlock(&root->fs_info->subvol_srcu, index); |
| 5403 | |
Julia Lawall | 34d19ba | 2011-01-24 19:55:19 +0000 | [diff] [blame] | 5404 | if (!IS_ERR(inode) && root != sub_root) { |
Yan, Zheng | c71bf09 | 2009-11-12 09:34:40 +0000 | [diff] [blame] | 5405 | down_read(&root->fs_info->cleanup_work_sem); |
| 5406 | if (!(inode->i_sb->s_flags & MS_RDONLY)) |
Josef Bacik | 66b4ffd | 2011-01-31 16:22:42 -0500 | [diff] [blame] | 5407 | ret = btrfs_orphan_cleanup(sub_root); |
Yan, Zheng | c71bf09 | 2009-11-12 09:34:40 +0000 | [diff] [blame] | 5408 | up_read(&root->fs_info->cleanup_work_sem); |
Josef Bacik | 01cd336 | 2013-06-03 21:39:49 -0400 | [diff] [blame] | 5409 | if (ret) { |
| 5410 | iput(inode); |
Josef Bacik | 66b4ffd | 2011-01-31 16:22:42 -0500 | [diff] [blame] | 5411 | inode = ERR_PTR(ret); |
Josef Bacik | 01cd336 | 2013-06-03 21:39:49 -0400 | [diff] [blame] | 5412 | } |
Yan, Zheng | c71bf09 | 2009-11-12 09:34:40 +0000 | [diff] [blame] | 5413 | } |
| 5414 | |
Chris Mason | 3de4586 | 2008-11-17 21:02:50 -0500 | [diff] [blame] | 5415 | return inode; |
| 5416 | } |
| 5417 | |
Nick Piggin | fe15ce4 | 2011-01-07 17:49:23 +1100 | [diff] [blame] | 5418 | static int btrfs_dentry_delete(const struct dentry *dentry) |
Yan, Zheng | 76dda93 | 2009-09-21 16:00:26 -0400 | [diff] [blame] | 5419 | { |
| 5420 | struct btrfs_root *root; |
Li Zefan | 848cce0 | 2012-02-21 17:04:28 +0800 | [diff] [blame] | 5421 | struct inode *inode = dentry->d_inode; |
Yan, Zheng | 76dda93 | 2009-09-21 16:00:26 -0400 | [diff] [blame] | 5422 | |
Li Zefan | 848cce0 | 2012-02-21 17:04:28 +0800 | [diff] [blame] | 5423 | if (!inode && !IS_ROOT(dentry)) |
| 5424 | inode = dentry->d_parent->d_inode; |
Yan, Zheng | 76dda93 | 2009-09-21 16:00:26 -0400 | [diff] [blame] | 5425 | |
Li Zefan | 848cce0 | 2012-02-21 17:04:28 +0800 | [diff] [blame] | 5426 | if (inode) { |
| 5427 | root = BTRFS_I(inode)->root; |
Yan, Zheng | efefb14 | 2009-10-09 09:25:16 -0400 | [diff] [blame] | 5428 | if (btrfs_root_refs(&root->root_item) == 0) |
| 5429 | return 1; |
Li Zefan | 848cce0 | 2012-02-21 17:04:28 +0800 | [diff] [blame] | 5430 | |
| 5431 | if (btrfs_ino(inode) == BTRFS_EMPTY_SUBVOL_DIR_OBJECTID) |
| 5432 | return 1; |
Yan, Zheng | efefb14 | 2009-10-09 09:25:16 -0400 | [diff] [blame] | 5433 | } |
Yan, Zheng | 76dda93 | 2009-09-21 16:00:26 -0400 | [diff] [blame] | 5434 | return 0; |
| 5435 | } |
| 5436 | |
Josef Bacik | b4aff1f | 2011-06-28 16:18:59 -0400 | [diff] [blame] | 5437 | static void btrfs_dentry_release(struct dentry *dentry) |
| 5438 | { |
Daeseok Youn | 944a451 | 2014-04-14 15:37:02 +0900 | [diff] [blame] | 5439 | kfree(dentry->d_fsdata); |
Josef Bacik | b4aff1f | 2011-06-28 16:18:59 -0400 | [diff] [blame] | 5440 | } |
| 5441 | |
Chris Mason | 3de4586 | 2008-11-17 21:02:50 -0500 | [diff] [blame] | 5442 | static struct dentry *btrfs_lookup(struct inode *dir, struct dentry *dentry, |
Al Viro | 00cd8dd | 2012-06-10 17:13:09 -0400 | [diff] [blame] | 5443 | unsigned int flags) |
Chris Mason | 3de4586 | 2008-11-17 21:02:50 -0500 | [diff] [blame] | 5444 | { |
Tsutomu Itoh | 5662344b3 | 2013-12-13 09:51:42 +0900 | [diff] [blame] | 5445 | struct inode *inode; |
Josef Bacik | a66e7cc | 2011-09-18 10:34:03 -0400 | [diff] [blame] | 5446 | |
Tsutomu Itoh | 5662344b3 | 2013-12-13 09:51:42 +0900 | [diff] [blame] | 5447 | inode = btrfs_lookup_dentry(dir, dentry); |
| 5448 | if (IS_ERR(inode)) { |
| 5449 | if (PTR_ERR(inode) == -ENOENT) |
| 5450 | inode = NULL; |
| 5451 | else |
| 5452 | return ERR_CAST(inode); |
| 5453 | } |
| 5454 | |
Al Viro | 41d28bc | 2014-10-12 22:24:21 -0400 | [diff] [blame] | 5455 | return d_splice_alias(inode, dentry); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5456 | } |
| 5457 | |
Miao Xie | 16cdcec | 2011-04-22 18:12:22 +0800 | [diff] [blame] | 5458 | unsigned char btrfs_filetype_table[] = { |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5459 | DT_UNKNOWN, DT_REG, DT_DIR, DT_CHR, DT_BLK, DT_FIFO, DT_SOCK, DT_LNK |
| 5460 | }; |
| 5461 | |
Al Viro | 9cdda8d | 2013-05-22 16:48:09 -0400 | [diff] [blame] | 5462 | static int btrfs_real_readdir(struct file *file, struct dir_context *ctx) |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5463 | { |
Al Viro | 9cdda8d | 2013-05-22 16:48:09 -0400 | [diff] [blame] | 5464 | struct inode *inode = file_inode(file); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5465 | struct btrfs_root *root = BTRFS_I(inode)->root; |
| 5466 | struct btrfs_item *item; |
| 5467 | struct btrfs_dir_item *di; |
| 5468 | struct btrfs_key key; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 5469 | struct btrfs_key found_key; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5470 | struct btrfs_path *path; |
Miao Xie | 16cdcec | 2011-04-22 18:12:22 +0800 | [diff] [blame] | 5471 | struct list_head ins_list; |
| 5472 | struct list_head del_list; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5473 | int ret; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 5474 | struct extent_buffer *leaf; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5475 | int slot; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5476 | unsigned char d_type; |
| 5477 | int over = 0; |
| 5478 | u32 di_cur; |
| 5479 | u32 di_total; |
| 5480 | u32 di_len; |
| 5481 | int key_type = BTRFS_DIR_INDEX_KEY; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 5482 | char tmp_name[32]; |
| 5483 | char *name_ptr; |
| 5484 | int name_len; |
Al Viro | 9cdda8d | 2013-05-22 16:48:09 -0400 | [diff] [blame] | 5485 | int is_curr = 0; /* ctx->pos points to the current index? */ |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5486 | |
| 5487 | /* FIXME, use a real flag for deciding about the key type */ |
| 5488 | if (root->fs_info->tree_root == root) |
| 5489 | key_type = BTRFS_DIR_ITEM_KEY; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 5490 | |
Al Viro | 9cdda8d | 2013-05-22 16:48:09 -0400 | [diff] [blame] | 5491 | if (!dir_emit_dots(file, ctx)) |
| 5492 | return 0; |
| 5493 | |
David Woodhouse | 49593bf | 2008-08-17 17:08:36 +0100 | [diff] [blame] | 5494 | path = btrfs_alloc_path(); |
Miao Xie | 16cdcec | 2011-04-22 18:12:22 +0800 | [diff] [blame] | 5495 | if (!path) |
| 5496 | return -ENOMEM; |
Chris Mason | ff5714c | 2011-05-28 07:00:39 -0400 | [diff] [blame] | 5497 | |
Josef Bacik | 026fd31 | 2011-05-13 10:32:11 -0400 | [diff] [blame] | 5498 | path->reada = 1; |
David Woodhouse | 49593bf | 2008-08-17 17:08:36 +0100 | [diff] [blame] | 5499 | |
Miao Xie | 16cdcec | 2011-04-22 18:12:22 +0800 | [diff] [blame] | 5500 | if (key_type == BTRFS_DIR_INDEX_KEY) { |
| 5501 | INIT_LIST_HEAD(&ins_list); |
| 5502 | INIT_LIST_HEAD(&del_list); |
| 5503 | btrfs_get_delayed_items(inode, &ins_list, &del_list); |
| 5504 | } |
| 5505 | |
David Sterba | 962a298 | 2014-06-04 18:41:45 +0200 | [diff] [blame] | 5506 | key.type = key_type; |
Al Viro | 9cdda8d | 2013-05-22 16:48:09 -0400 | [diff] [blame] | 5507 | key.offset = ctx->pos; |
Li Zefan | 33345d01 | 2011-04-20 10:31:50 +0800 | [diff] [blame] | 5508 | key.objectid = btrfs_ino(inode); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 5509 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5510 | ret = btrfs_search_slot(NULL, root, &key, path, 0, 0); |
| 5511 | if (ret < 0) |
| 5512 | goto err; |
David Woodhouse | 49593bf | 2008-08-17 17:08:36 +0100 | [diff] [blame] | 5513 | |
| 5514 | while (1) { |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 5515 | leaf = path->nodes[0]; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5516 | slot = path->slots[0]; |
Li Zefan | b9e03af | 2011-03-23 10:43:58 +0800 | [diff] [blame] | 5517 | if (slot >= btrfs_header_nritems(leaf)) { |
| 5518 | ret = btrfs_next_leaf(root, path); |
| 5519 | if (ret < 0) |
| 5520 | goto err; |
| 5521 | else if (ret > 0) |
| 5522 | break; |
| 5523 | continue; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5524 | } |
Chris Mason | 3de4586 | 2008-11-17 21:02:50 -0500 | [diff] [blame] | 5525 | |
Ross Kirk | dd3cc16 | 2013-09-16 15:58:09 +0100 | [diff] [blame] | 5526 | item = btrfs_item_nr(slot); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 5527 | btrfs_item_key_to_cpu(leaf, &found_key, slot); |
| 5528 | |
| 5529 | if (found_key.objectid != key.objectid) |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5530 | break; |
David Sterba | 962a298 | 2014-06-04 18:41:45 +0200 | [diff] [blame] | 5531 | if (found_key.type != key_type) |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5532 | break; |
Al Viro | 9cdda8d | 2013-05-22 16:48:09 -0400 | [diff] [blame] | 5533 | if (found_key.offset < ctx->pos) |
Li Zefan | b9e03af | 2011-03-23 10:43:58 +0800 | [diff] [blame] | 5534 | goto next; |
Miao Xie | 16cdcec | 2011-04-22 18:12:22 +0800 | [diff] [blame] | 5535 | if (key_type == BTRFS_DIR_INDEX_KEY && |
| 5536 | btrfs_should_delete_dir_index(&del_list, |
| 5537 | found_key.offset)) |
| 5538 | goto next; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 5539 | |
Al Viro | 9cdda8d | 2013-05-22 16:48:09 -0400 | [diff] [blame] | 5540 | ctx->pos = found_key.offset; |
Miao Xie | 16cdcec | 2011-04-22 18:12:22 +0800 | [diff] [blame] | 5541 | is_curr = 1; |
David Woodhouse | 49593bf | 2008-08-17 17:08:36 +0100 | [diff] [blame] | 5542 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5543 | di = btrfs_item_ptr(leaf, slot, struct btrfs_dir_item); |
| 5544 | di_cur = 0; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 5545 | di_total = btrfs_item_size(leaf, item); |
David Woodhouse | 49593bf | 2008-08-17 17:08:36 +0100 | [diff] [blame] | 5546 | |
| 5547 | while (di_cur < di_total) { |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 5548 | struct btrfs_key location; |
| 5549 | |
Josef Bacik | 22a94d4 | 2011-03-16 16:47:17 -0400 | [diff] [blame] | 5550 | if (verify_dir_item(root, leaf, di)) |
| 5551 | break; |
| 5552 | |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 5553 | name_len = btrfs_dir_name_len(leaf, di); |
David Woodhouse | 49593bf | 2008-08-17 17:08:36 +0100 | [diff] [blame] | 5554 | if (name_len <= sizeof(tmp_name)) { |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 5555 | name_ptr = tmp_name; |
| 5556 | } else { |
| 5557 | name_ptr = kmalloc(name_len, GFP_NOFS); |
David Woodhouse | 49593bf | 2008-08-17 17:08:36 +0100 | [diff] [blame] | 5558 | if (!name_ptr) { |
| 5559 | ret = -ENOMEM; |
| 5560 | goto err; |
| 5561 | } |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 5562 | } |
| 5563 | read_extent_buffer(leaf, name_ptr, |
| 5564 | (unsigned long)(di + 1), name_len); |
| 5565 | |
| 5566 | d_type = btrfs_filetype_table[btrfs_dir_type(leaf, di)]; |
| 5567 | btrfs_dir_item_key_to_cpu(leaf, di, &location); |
Chris Mason | 3de4586 | 2008-11-17 21:02:50 -0500 | [diff] [blame] | 5568 | |
Josef Bacik | b4aff1f | 2011-06-28 16:18:59 -0400 | [diff] [blame] | 5569 | |
Chris Mason | 3de4586 | 2008-11-17 21:02:50 -0500 | [diff] [blame] | 5570 | /* is this a reference to our own snapshot? If so |
Arne Jansen | 8c9c2bf | 2012-02-25 09:09:30 +0100 | [diff] [blame] | 5571 | * skip it. |
| 5572 | * |
| 5573 | * In contrast to old kernels, we insert the snapshot's |
| 5574 | * dir item and dir index after it has been created, so |
| 5575 | * we won't find a reference to our own snapshot. We |
| 5576 | * still keep the following code for backward |
| 5577 | * compatibility. |
Chris Mason | 3de4586 | 2008-11-17 21:02:50 -0500 | [diff] [blame] | 5578 | */ |
| 5579 | if (location.type == BTRFS_ROOT_ITEM_KEY && |
| 5580 | location.objectid == root->root_key.objectid) { |
| 5581 | over = 0; |
| 5582 | goto skip; |
| 5583 | } |
Al Viro | 9cdda8d | 2013-05-22 16:48:09 -0400 | [diff] [blame] | 5584 | over = !dir_emit(ctx, name_ptr, name_len, |
| 5585 | location.objectid, d_type); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 5586 | |
Chris Mason | 3de4586 | 2008-11-17 21:02:50 -0500 | [diff] [blame] | 5587 | skip: |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 5588 | if (name_ptr != tmp_name) |
| 5589 | kfree(name_ptr); |
| 5590 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5591 | if (over) |
| 5592 | goto nopos; |
Josef Bacik | 5103e94 | 2007-11-16 11:45:54 -0500 | [diff] [blame] | 5593 | di_len = btrfs_dir_name_len(leaf, di) + |
David Woodhouse | 49593bf | 2008-08-17 17:08:36 +0100 | [diff] [blame] | 5594 | btrfs_dir_data_len(leaf, di) + sizeof(*di); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5595 | di_cur += di_len; |
| 5596 | di = (struct btrfs_dir_item *)((char *)di + di_len); |
| 5597 | } |
Li Zefan | b9e03af | 2011-03-23 10:43:58 +0800 | [diff] [blame] | 5598 | next: |
| 5599 | path->slots[0]++; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5600 | } |
David Woodhouse | 49593bf | 2008-08-17 17:08:36 +0100 | [diff] [blame] | 5601 | |
Miao Xie | 16cdcec | 2011-04-22 18:12:22 +0800 | [diff] [blame] | 5602 | if (key_type == BTRFS_DIR_INDEX_KEY) { |
| 5603 | if (is_curr) |
Al Viro | 9cdda8d | 2013-05-22 16:48:09 -0400 | [diff] [blame] | 5604 | ctx->pos++; |
| 5605 | ret = btrfs_readdir_delayed_dir_index(ctx, &ins_list); |
Miao Xie | 16cdcec | 2011-04-22 18:12:22 +0800 | [diff] [blame] | 5606 | if (ret) |
| 5607 | goto nopos; |
| 5608 | } |
| 5609 | |
David Woodhouse | 49593bf | 2008-08-17 17:08:36 +0100 | [diff] [blame] | 5610 | /* Reached end of directory/root. Bump pos past the last item. */ |
Zach Brown | db62efb | 2013-07-11 16:19:42 -0700 | [diff] [blame] | 5611 | ctx->pos++; |
| 5612 | |
| 5613 | /* |
| 5614 | * Stop new entries from being returned after we return the last |
| 5615 | * entry. |
| 5616 | * |
| 5617 | * New directory entries are assigned a strictly increasing |
| 5618 | * offset. This means that new entries created during readdir |
| 5619 | * are *guaranteed* to be seen in the future by that readdir. |
| 5620 | * This has broken buggy programs which operate on names as |
| 5621 | * they're returned by readdir. Until we re-use freed offsets |
| 5622 | * we have this hack to stop new entries from being returned |
| 5623 | * under the assumption that they'll never reach this huge |
| 5624 | * offset. |
| 5625 | * |
| 5626 | * This is being careful not to overflow 32bit loff_t unless the |
| 5627 | * last entry requires it because doing so has broken 32bit apps |
| 5628 | * in the past. |
| 5629 | */ |
| 5630 | if (key_type == BTRFS_DIR_INDEX_KEY) { |
| 5631 | if (ctx->pos >= INT_MAX) |
| 5632 | ctx->pos = LLONG_MAX; |
| 5633 | else |
| 5634 | ctx->pos = INT_MAX; |
| 5635 | } |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5636 | nopos: |
| 5637 | ret = 0; |
| 5638 | err: |
Miao Xie | 16cdcec | 2011-04-22 18:12:22 +0800 | [diff] [blame] | 5639 | if (key_type == BTRFS_DIR_INDEX_KEY) |
| 5640 | btrfs_put_delayed_items(&ins_list, &del_list); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5641 | btrfs_free_path(path); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5642 | return ret; |
| 5643 | } |
| 5644 | |
Christoph Hellwig | a9185b4 | 2010-03-05 09:21:37 +0100 | [diff] [blame] | 5645 | int btrfs_write_inode(struct inode *inode, struct writeback_control *wbc) |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5646 | { |
| 5647 | struct btrfs_root *root = BTRFS_I(inode)->root; |
| 5648 | struct btrfs_trans_handle *trans; |
| 5649 | int ret = 0; |
Josef Bacik | 0af3d00 | 2010-06-21 14:48:16 -0400 | [diff] [blame] | 5650 | bool nolock = false; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5651 | |
Josef Bacik | 72ac3c0 | 2012-05-23 14:13:11 -0400 | [diff] [blame] | 5652 | if (test_bit(BTRFS_INODE_DUMMY, &BTRFS_I(inode)->runtime_flags)) |
Chris Mason | 4ca8b41 | 2008-08-05 13:30:48 -0400 | [diff] [blame] | 5653 | return 0; |
| 5654 | |
Liu Bo | 83eea1f | 2012-07-10 05:28:39 -0600 | [diff] [blame] | 5655 | 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] | 5656 | nolock = true; |
Josef Bacik | 0af3d00 | 2010-06-21 14:48:16 -0400 | [diff] [blame] | 5657 | |
Christoph Hellwig | a9185b4 | 2010-03-05 09:21:37 +0100 | [diff] [blame] | 5658 | if (wbc->sync_mode == WB_SYNC_ALL) { |
Josef Bacik | 0af3d00 | 2010-06-21 14:48:16 -0400 | [diff] [blame] | 5659 | if (nolock) |
Josef Bacik | 7a7eaa4 | 2011-04-13 12:54:33 -0400 | [diff] [blame] | 5660 | trans = btrfs_join_transaction_nolock(root); |
Josef Bacik | 0af3d00 | 2010-06-21 14:48:16 -0400 | [diff] [blame] | 5661 | else |
Josef Bacik | 7a7eaa4 | 2011-04-13 12:54:33 -0400 | [diff] [blame] | 5662 | trans = btrfs_join_transaction(root); |
Tsutomu Itoh | 3612b49 | 2011-01-25 02:51:38 +0000 | [diff] [blame] | 5663 | if (IS_ERR(trans)) |
| 5664 | return PTR_ERR(trans); |
Miao Xie | a698d075 | 2012-09-20 01:51:59 -0600 | [diff] [blame] | 5665 | ret = btrfs_commit_transaction(trans, root); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5666 | } |
| 5667 | return ret; |
| 5668 | } |
| 5669 | |
| 5670 | /* |
Chris Mason | 54aa1f4 | 2007-06-22 14:16:25 -0400 | [diff] [blame] | 5671 | * This is somewhat expensive, updating the tree every time the |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5672 | * inode changes. But, it is most likely to find the inode in cache. |
| 5673 | * FIXME, needs more benchmarking...there are no reasons other than performance |
| 5674 | * to keep or drop this code. |
| 5675 | */ |
Eric Sandeen | 48a3b63 | 2013-04-25 20:41:01 +0000 | [diff] [blame] | 5676 | static int btrfs_dirty_inode(struct inode *inode) |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5677 | { |
| 5678 | struct btrfs_root *root = BTRFS_I(inode)->root; |
| 5679 | struct btrfs_trans_handle *trans; |
Yan, Zheng | 8929ecfa | 2010-05-16 10:49:58 -0400 | [diff] [blame] | 5680 | int ret; |
| 5681 | |
Josef Bacik | 72ac3c0 | 2012-05-23 14:13:11 -0400 | [diff] [blame] | 5682 | if (test_bit(BTRFS_INODE_DUMMY, &BTRFS_I(inode)->runtime_flags)) |
Josef Bacik | 22c44fe | 2011-11-30 10:45:38 -0500 | [diff] [blame] | 5683 | return 0; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5684 | |
Josef Bacik | 7a7eaa4 | 2011-04-13 12:54:33 -0400 | [diff] [blame] | 5685 | trans = btrfs_join_transaction(root); |
Josef Bacik | 22c44fe | 2011-11-30 10:45:38 -0500 | [diff] [blame] | 5686 | if (IS_ERR(trans)) |
| 5687 | return PTR_ERR(trans); |
Yan, Zheng | 8929ecfa | 2010-05-16 10:49:58 -0400 | [diff] [blame] | 5688 | |
| 5689 | ret = btrfs_update_inode(trans, root, inode); |
Chris Mason | 94b6044 | 2010-05-26 11:02:00 -0400 | [diff] [blame] | 5690 | if (ret && ret == -ENOSPC) { |
| 5691 | /* whoops, lets try again with the full transaction */ |
| 5692 | btrfs_end_transaction(trans, root); |
| 5693 | trans = btrfs_start_transaction(root, 1); |
Josef Bacik | 22c44fe | 2011-11-30 10:45:38 -0500 | [diff] [blame] | 5694 | if (IS_ERR(trans)) |
| 5695 | return PTR_ERR(trans); |
Yan, Zheng | 8929ecfa | 2010-05-16 10:49:58 -0400 | [diff] [blame] | 5696 | |
Chris Mason | 94b6044 | 2010-05-26 11:02:00 -0400 | [diff] [blame] | 5697 | ret = btrfs_update_inode(trans, root, inode); |
Chris Mason | 94b6044 | 2010-05-26 11:02:00 -0400 | [diff] [blame] | 5698 | } |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5699 | btrfs_end_transaction(trans, root); |
Miao Xie | 16cdcec | 2011-04-22 18:12:22 +0800 | [diff] [blame] | 5700 | if (BTRFS_I(inode)->delayed_node) |
| 5701 | btrfs_balance_delayed_items(root); |
Josef Bacik | 22c44fe | 2011-11-30 10:45:38 -0500 | [diff] [blame] | 5702 | |
| 5703 | return ret; |
| 5704 | } |
| 5705 | |
| 5706 | /* |
| 5707 | * This is a copy of file_update_time. We need this so we can return error on |
| 5708 | * ENOSPC for updating the inode in the case of file write and mmap writes. |
| 5709 | */ |
Josef Bacik | e41f941 | 2012-03-26 09:46:47 -0400 | [diff] [blame] | 5710 | static int btrfs_update_time(struct inode *inode, struct timespec *now, |
| 5711 | int flags) |
Josef Bacik | 22c44fe | 2011-11-30 10:45:38 -0500 | [diff] [blame] | 5712 | { |
Alexander Block | 2bc556528 | 2012-06-15 09:49:33 +0200 | [diff] [blame] | 5713 | struct btrfs_root *root = BTRFS_I(inode)->root; |
| 5714 | |
| 5715 | if (btrfs_root_readonly(root)) |
| 5716 | return -EROFS; |
| 5717 | |
Josef Bacik | e41f941 | 2012-03-26 09:46:47 -0400 | [diff] [blame] | 5718 | if (flags & S_VERSION) |
Josef Bacik | 22c44fe | 2011-11-30 10:45:38 -0500 | [diff] [blame] | 5719 | inode_inc_iversion(inode); |
Josef Bacik | e41f941 | 2012-03-26 09:46:47 -0400 | [diff] [blame] | 5720 | if (flags & S_CTIME) |
| 5721 | inode->i_ctime = *now; |
| 5722 | if (flags & S_MTIME) |
| 5723 | inode->i_mtime = *now; |
| 5724 | if (flags & S_ATIME) |
| 5725 | inode->i_atime = *now; |
| 5726 | return btrfs_dirty_inode(inode); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5727 | } |
| 5728 | |
Chris Mason | d352ac6 | 2008-09-29 15:18:18 -0400 | [diff] [blame] | 5729 | /* |
| 5730 | * find the highest existing sequence number in a directory |
| 5731 | * and then set the in-memory index_cnt variable to reflect |
| 5732 | * free sequence numbers |
| 5733 | */ |
Josef Bacik | aec7477 | 2008-07-24 12:12:38 -0400 | [diff] [blame] | 5734 | static int btrfs_set_inode_index_count(struct inode *inode) |
| 5735 | { |
| 5736 | struct btrfs_root *root = BTRFS_I(inode)->root; |
| 5737 | struct btrfs_key key, found_key; |
| 5738 | struct btrfs_path *path; |
| 5739 | struct extent_buffer *leaf; |
| 5740 | int ret; |
| 5741 | |
Li Zefan | 33345d01 | 2011-04-20 10:31:50 +0800 | [diff] [blame] | 5742 | key.objectid = btrfs_ino(inode); |
David Sterba | 962a298 | 2014-06-04 18:41:45 +0200 | [diff] [blame] | 5743 | key.type = BTRFS_DIR_INDEX_KEY; |
Josef Bacik | aec7477 | 2008-07-24 12:12:38 -0400 | [diff] [blame] | 5744 | key.offset = (u64)-1; |
| 5745 | |
| 5746 | path = btrfs_alloc_path(); |
| 5747 | if (!path) |
| 5748 | return -ENOMEM; |
| 5749 | |
| 5750 | ret = btrfs_search_slot(NULL, root, &key, path, 0, 0); |
| 5751 | if (ret < 0) |
| 5752 | goto out; |
| 5753 | /* FIXME: we should be able to handle this */ |
| 5754 | if (ret == 0) |
| 5755 | goto out; |
| 5756 | ret = 0; |
| 5757 | |
| 5758 | /* |
| 5759 | * MAGIC NUMBER EXPLANATION: |
| 5760 | * since we search a directory based on f_pos we have to start at 2 |
| 5761 | * since '.' and '..' have f_pos of 0 and 1 respectively, so everybody |
| 5762 | * else has to start at 2 |
| 5763 | */ |
| 5764 | if (path->slots[0] == 0) { |
| 5765 | BTRFS_I(inode)->index_cnt = 2; |
| 5766 | goto out; |
| 5767 | } |
| 5768 | |
| 5769 | path->slots[0]--; |
| 5770 | |
| 5771 | leaf = path->nodes[0]; |
| 5772 | btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]); |
| 5773 | |
Li Zefan | 33345d01 | 2011-04-20 10:31:50 +0800 | [diff] [blame] | 5774 | if (found_key.objectid != btrfs_ino(inode) || |
David Sterba | 962a298 | 2014-06-04 18:41:45 +0200 | [diff] [blame] | 5775 | found_key.type != BTRFS_DIR_INDEX_KEY) { |
Josef Bacik | aec7477 | 2008-07-24 12:12:38 -0400 | [diff] [blame] | 5776 | BTRFS_I(inode)->index_cnt = 2; |
| 5777 | goto out; |
| 5778 | } |
| 5779 | |
| 5780 | BTRFS_I(inode)->index_cnt = found_key.offset + 1; |
| 5781 | out: |
| 5782 | btrfs_free_path(path); |
| 5783 | return ret; |
| 5784 | } |
| 5785 | |
Chris Mason | d352ac6 | 2008-09-29 15:18:18 -0400 | [diff] [blame] | 5786 | /* |
| 5787 | * helper to find a free sequence number in a given directory. This current |
| 5788 | * code is very simple, later versions will do smarter things in the btree |
| 5789 | */ |
Chris Mason | 3de4586 | 2008-11-17 21:02:50 -0500 | [diff] [blame] | 5790 | int btrfs_set_inode_index(struct inode *dir, u64 *index) |
Josef Bacik | aec7477 | 2008-07-24 12:12:38 -0400 | [diff] [blame] | 5791 | { |
| 5792 | int ret = 0; |
| 5793 | |
| 5794 | if (BTRFS_I(dir)->index_cnt == (u64)-1) { |
Miao Xie | 16cdcec | 2011-04-22 18:12:22 +0800 | [diff] [blame] | 5795 | ret = btrfs_inode_delayed_dir_index_count(dir); |
| 5796 | if (ret) { |
| 5797 | ret = btrfs_set_inode_index_count(dir); |
| 5798 | if (ret) |
| 5799 | return ret; |
| 5800 | } |
Josef Bacik | aec7477 | 2008-07-24 12:12:38 -0400 | [diff] [blame] | 5801 | } |
| 5802 | |
Chris Mason | 00e4e6b | 2008-08-05 11:18:09 -0400 | [diff] [blame] | 5803 | *index = BTRFS_I(dir)->index_cnt; |
Josef Bacik | aec7477 | 2008-07-24 12:12:38 -0400 | [diff] [blame] | 5804 | BTRFS_I(dir)->index_cnt++; |
| 5805 | |
| 5806 | return ret; |
| 5807 | } |
| 5808 | |
Chris Mason | b0d5d10 | 2014-09-08 13:08:51 -0700 | [diff] [blame] | 5809 | static int btrfs_insert_inode_locked(struct inode *inode) |
| 5810 | { |
| 5811 | struct btrfs_iget_args args; |
| 5812 | args.location = &BTRFS_I(inode)->location; |
| 5813 | args.root = BTRFS_I(inode)->root; |
| 5814 | |
| 5815 | return insert_inode_locked4(inode, |
| 5816 | btrfs_inode_hash(inode->i_ino, BTRFS_I(inode)->root), |
| 5817 | btrfs_find_actor, &args); |
| 5818 | } |
| 5819 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5820 | static struct inode *btrfs_new_inode(struct btrfs_trans_handle *trans, |
| 5821 | struct btrfs_root *root, |
Josef Bacik | aec7477 | 2008-07-24 12:12:38 -0400 | [diff] [blame] | 5822 | struct inode *dir, |
Chris Mason | 9c58309 | 2008-01-29 15:15:18 -0500 | [diff] [blame] | 5823 | const char *name, int name_len, |
Al Viro | 175a4eb | 2011-07-26 03:30:54 -0400 | [diff] [blame] | 5824 | u64 ref_objectid, u64 objectid, |
| 5825 | umode_t mode, u64 *index) |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5826 | { |
| 5827 | struct inode *inode; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 5828 | struct btrfs_inode_item *inode_item; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5829 | struct btrfs_key *location; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 5830 | struct btrfs_path *path; |
Chris Mason | 9c58309 | 2008-01-29 15:15:18 -0500 | [diff] [blame] | 5831 | struct btrfs_inode_ref *ref; |
| 5832 | struct btrfs_key key[2]; |
| 5833 | u32 sizes[2]; |
Filipe Manana | ef3b9af | 2014-04-27 20:40:45 +0100 | [diff] [blame] | 5834 | int nitems = name ? 2 : 1; |
Chris Mason | 9c58309 | 2008-01-29 15:15:18 -0500 | [diff] [blame] | 5835 | unsigned long ptr; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5836 | int ret; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5837 | |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 5838 | path = btrfs_alloc_path(); |
Mark Fasheh | d8926bb | 2011-07-13 10:38:47 -0700 | [diff] [blame] | 5839 | if (!path) |
| 5840 | return ERR_PTR(-ENOMEM); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 5841 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5842 | inode = new_inode(root->fs_info->sb); |
Yoshinori Sano | 8fb2764 | 2011-04-09 02:30:07 +0000 | [diff] [blame] | 5843 | if (!inode) { |
| 5844 | btrfs_free_path(path); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5845 | return ERR_PTR(-ENOMEM); |
Yoshinori Sano | 8fb2764 | 2011-04-09 02:30:07 +0000 | [diff] [blame] | 5846 | } |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5847 | |
Li Zefan | 581bb05 | 2011-04-20 10:06:11 +0800 | [diff] [blame] | 5848 | /* |
Filipe Manana | 5762b5c | 2014-08-01 00:10:32 +0100 | [diff] [blame] | 5849 | * O_TMPFILE, set link count to 0, so that after this point, |
| 5850 | * we fill in an inode item with the correct link count. |
| 5851 | */ |
| 5852 | if (!name) |
| 5853 | set_nlink(inode, 0); |
| 5854 | |
| 5855 | /* |
Li Zefan | 581bb05 | 2011-04-20 10:06:11 +0800 | [diff] [blame] | 5856 | * we have to initialize this early, so we can reclaim the inode |
| 5857 | * number if we fail afterwards in this function. |
| 5858 | */ |
| 5859 | inode->i_ino = objectid; |
| 5860 | |
Filipe Manana | ef3b9af | 2014-04-27 20:40:45 +0100 | [diff] [blame] | 5861 | if (dir && name) { |
liubo | 1abe9b8 | 2011-03-24 11:18:59 +0000 | [diff] [blame] | 5862 | trace_btrfs_inode_request(dir); |
| 5863 | |
Chris Mason | 3de4586 | 2008-11-17 21:02:50 -0500 | [diff] [blame] | 5864 | ret = btrfs_set_inode_index(dir, index); |
Shen Feng | 0977143 | 2009-04-02 16:46:06 -0400 | [diff] [blame] | 5865 | if (ret) { |
Yoshinori Sano | 8fb2764 | 2011-04-09 02:30:07 +0000 | [diff] [blame] | 5866 | btrfs_free_path(path); |
Shen Feng | 0977143 | 2009-04-02 16:46:06 -0400 | [diff] [blame] | 5867 | iput(inode); |
Josef Bacik | aec7477 | 2008-07-24 12:12:38 -0400 | [diff] [blame] | 5868 | return ERR_PTR(ret); |
Shen Feng | 0977143 | 2009-04-02 16:46:06 -0400 | [diff] [blame] | 5869 | } |
Filipe Manana | ef3b9af | 2014-04-27 20:40:45 +0100 | [diff] [blame] | 5870 | } else if (dir) { |
| 5871 | *index = 0; |
Josef Bacik | aec7477 | 2008-07-24 12:12:38 -0400 | [diff] [blame] | 5872 | } |
| 5873 | /* |
| 5874 | * index_cnt is ignored for everything but a dir, |
| 5875 | * btrfs_get_inode_index_count has an explanation for the magic |
| 5876 | * number |
| 5877 | */ |
| 5878 | BTRFS_I(inode)->index_cnt = 2; |
Miao Xie | 67de117 | 2013-12-26 13:07:06 +0800 | [diff] [blame] | 5879 | BTRFS_I(inode)->dir_index = *index; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5880 | BTRFS_I(inode)->root = root; |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 5881 | BTRFS_I(inode)->generation = trans->transid; |
Josef Bacik | 7619585 | 2010-11-19 02:18:02 +0000 | [diff] [blame] | 5882 | inode->i_generation = BTRFS_I(inode)->generation; |
Chris Mason | b888db2 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 5883 | |
Josef Bacik | 5dc562c | 2012-08-17 13:14:17 -0400 | [diff] [blame] | 5884 | /* |
| 5885 | * We could have gotten an inode number from somebody who was fsynced |
| 5886 | * and then removed in this same transaction, so let's just set full |
| 5887 | * sync since it will be a full sync anyway and this will blow away the |
| 5888 | * old info in the log. |
| 5889 | */ |
| 5890 | set_bit(BTRFS_INODE_NEEDS_FULL_SYNC, &BTRFS_I(inode)->runtime_flags); |
| 5891 | |
Chris Mason | 9c58309 | 2008-01-29 15:15:18 -0500 | [diff] [blame] | 5892 | key[0].objectid = objectid; |
David Sterba | 962a298 | 2014-06-04 18:41:45 +0200 | [diff] [blame] | 5893 | key[0].type = BTRFS_INODE_ITEM_KEY; |
Chris Mason | 9c58309 | 2008-01-29 15:15:18 -0500 | [diff] [blame] | 5894 | key[0].offset = 0; |
| 5895 | |
Chris Mason | 9c58309 | 2008-01-29 15:15:18 -0500 | [diff] [blame] | 5896 | sizes[0] = sizeof(struct btrfs_inode_item); |
Filipe Manana | ef3b9af | 2014-04-27 20:40:45 +0100 | [diff] [blame] | 5897 | |
| 5898 | if (name) { |
| 5899 | /* |
| 5900 | * Start new inodes with an inode_ref. This is slightly more |
| 5901 | * efficient for small numbers of hard links since they will |
| 5902 | * be packed into one item. Extended refs will kick in if we |
| 5903 | * add more hard links than can fit in the ref item. |
| 5904 | */ |
| 5905 | key[1].objectid = objectid; |
David Sterba | 962a298 | 2014-06-04 18:41:45 +0200 | [diff] [blame] | 5906 | key[1].type = BTRFS_INODE_REF_KEY; |
Filipe Manana | ef3b9af | 2014-04-27 20:40:45 +0100 | [diff] [blame] | 5907 | key[1].offset = ref_objectid; |
| 5908 | |
| 5909 | sizes[1] = name_len + sizeof(*ref); |
| 5910 | } |
Chris Mason | 9c58309 | 2008-01-29 15:15:18 -0500 | [diff] [blame] | 5911 | |
Chris Mason | b0d5d10 | 2014-09-08 13:08:51 -0700 | [diff] [blame] | 5912 | location = &BTRFS_I(inode)->location; |
| 5913 | location->objectid = objectid; |
| 5914 | location->offset = 0; |
David Sterba | 962a298 | 2014-06-04 18:41:45 +0200 | [diff] [blame] | 5915 | location->type = BTRFS_INODE_ITEM_KEY; |
Chris Mason | b0d5d10 | 2014-09-08 13:08:51 -0700 | [diff] [blame] | 5916 | |
| 5917 | ret = btrfs_insert_inode_locked(inode); |
| 5918 | if (ret < 0) |
| 5919 | goto fail; |
| 5920 | |
Chris Mason | b947343 | 2009-03-13 11:00:37 -0400 | [diff] [blame] | 5921 | path->leave_spinning = 1; |
Filipe Manana | ef3b9af | 2014-04-27 20:40:45 +0100 | [diff] [blame] | 5922 | ret = btrfs_insert_empty_items(trans, root, path, key, sizes, nitems); |
Chris Mason | 9c58309 | 2008-01-29 15:15:18 -0500 | [diff] [blame] | 5923 | if (ret != 0) |
Chris Mason | b0d5d10 | 2014-09-08 13:08:51 -0700 | [diff] [blame] | 5924 | goto fail_unlock; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 5925 | |
Dmitry Monakhov | ecc11fab | 2010-03-04 17:31:47 +0300 | [diff] [blame] | 5926 | inode_init_owner(inode, dir, mode); |
Yan Zheng | a76a3cd | 2008-10-09 11:46:29 -0400 | [diff] [blame] | 5927 | inode_set_bytes(inode, 0); |
chandan r | 9cc97d6 | 2012-07-04 12:48:07 +0530 | [diff] [blame] | 5928 | |
| 5929 | inode->i_mtime = CURRENT_TIME; |
| 5930 | inode->i_atime = inode->i_mtime; |
| 5931 | inode->i_ctime = inode->i_mtime; |
| 5932 | BTRFS_I(inode)->i_otime = inode->i_mtime; |
| 5933 | |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 5934 | inode_item = btrfs_item_ptr(path->nodes[0], path->slots[0], |
| 5935 | struct btrfs_inode_item); |
Li Zefan | 293f7e0 | 2012-07-10 00:58:58 -0600 | [diff] [blame] | 5936 | memset_extent_buffer(path->nodes[0], 0, (unsigned long)inode_item, |
| 5937 | sizeof(*inode_item)); |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 5938 | fill_inode_item(trans, path->nodes[0], inode_item, inode); |
Chris Mason | 9c58309 | 2008-01-29 15:15:18 -0500 | [diff] [blame] | 5939 | |
Filipe Manana | ef3b9af | 2014-04-27 20:40:45 +0100 | [diff] [blame] | 5940 | if (name) { |
| 5941 | ref = btrfs_item_ptr(path->nodes[0], path->slots[0] + 1, |
| 5942 | struct btrfs_inode_ref); |
| 5943 | btrfs_set_inode_ref_name_len(path->nodes[0], ref, name_len); |
| 5944 | btrfs_set_inode_ref_index(path->nodes[0], ref, *index); |
| 5945 | ptr = (unsigned long)(ref + 1); |
| 5946 | write_extent_buffer(path->nodes[0], name, ptr, name_len); |
| 5947 | } |
Chris Mason | 9c58309 | 2008-01-29 15:15:18 -0500 | [diff] [blame] | 5948 | |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 5949 | btrfs_mark_buffer_dirty(path->nodes[0]); |
| 5950 | btrfs_free_path(path); |
| 5951 | |
Christoph Hellwig | 6cbff00 | 2009-04-17 10:37:41 +0200 | [diff] [blame] | 5952 | btrfs_inherit_iflags(inode, dir); |
| 5953 | |
Al Viro | 569254b | 2011-07-24 17:08:40 -0400 | [diff] [blame] | 5954 | if (S_ISREG(mode)) { |
Chris Mason | 9427216 | 2009-07-02 12:26:06 -0400 | [diff] [blame] | 5955 | if (btrfs_test_opt(root, NODATASUM)) |
| 5956 | BTRFS_I(inode)->flags |= BTRFS_INODE_NODATASUM; |
Liu Bo | 213490b | 2012-09-11 08:33:50 -0600 | [diff] [blame] | 5957 | if (btrfs_test_opt(root, NODATACOW)) |
Josef Bacik | f2bdf9a | 2013-02-21 15:28:28 -0500 | [diff] [blame] | 5958 | BTRFS_I(inode)->flags |= BTRFS_INODE_NODATACOW | |
| 5959 | BTRFS_INODE_NODATASUM; |
Chris Mason | 9427216 | 2009-07-02 12:26:06 -0400 | [diff] [blame] | 5960 | } |
| 5961 | |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 5962 | inode_tree_add(inode); |
liubo | 1abe9b8 | 2011-03-24 11:18:59 +0000 | [diff] [blame] | 5963 | |
| 5964 | trace_btrfs_inode_new(inode); |
Chris Mason | 1973f0f | 2011-06-24 13:13:29 -0400 | [diff] [blame] | 5965 | btrfs_set_inode_last_trans(trans, inode); |
liubo | 1abe9b8 | 2011-03-24 11:18:59 +0000 | [diff] [blame] | 5966 | |
Alexander Block | 8ea05e3 | 2012-07-25 17:35:53 +0200 | [diff] [blame] | 5967 | btrfs_update_root_times(trans, root); |
| 5968 | |
Filipe David Borba Manana | 6354192 | 2014-01-07 11:47:46 +0000 | [diff] [blame] | 5969 | ret = btrfs_inode_inherit_props(trans, inode, dir); |
| 5970 | if (ret) |
| 5971 | btrfs_err(root->fs_info, |
| 5972 | "error inheriting props for ino %llu (root %llu): %d", |
| 5973 | btrfs_ino(inode), root->root_key.objectid, ret); |
| 5974 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5975 | return inode; |
Chris Mason | b0d5d10 | 2014-09-08 13:08:51 -0700 | [diff] [blame] | 5976 | |
| 5977 | fail_unlock: |
| 5978 | unlock_new_inode(inode); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 5979 | fail: |
Filipe Manana | ef3b9af | 2014-04-27 20:40:45 +0100 | [diff] [blame] | 5980 | if (dir && name) |
Josef Bacik | aec7477 | 2008-07-24 12:12:38 -0400 | [diff] [blame] | 5981 | BTRFS_I(dir)->index_cnt--; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 5982 | btrfs_free_path(path); |
Shen Feng | 0977143 | 2009-04-02 16:46:06 -0400 | [diff] [blame] | 5983 | iput(inode); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 5984 | return ERR_PTR(ret); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5985 | } |
| 5986 | |
| 5987 | static inline u8 btrfs_inode_type(struct inode *inode) |
| 5988 | { |
| 5989 | return btrfs_type_by_mode[(inode->i_mode & S_IFMT) >> S_SHIFT]; |
| 5990 | } |
| 5991 | |
Chris Mason | d352ac6 | 2008-09-29 15:18:18 -0400 | [diff] [blame] | 5992 | /* |
| 5993 | * utility function to add 'inode' into 'parent_inode' with |
| 5994 | * a give name and a given sequence number. |
| 5995 | * if 'add_backref' is true, also insert a backref from the |
| 5996 | * inode to the parent directory. |
| 5997 | */ |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 5998 | int btrfs_add_link(struct btrfs_trans_handle *trans, |
| 5999 | struct inode *parent_inode, struct inode *inode, |
| 6000 | const char *name, int name_len, int add_backref, u64 index) |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 6001 | { |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 6002 | int ret = 0; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 6003 | struct btrfs_key key; |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 6004 | struct btrfs_root *root = BTRFS_I(parent_inode)->root; |
Li Zefan | 33345d01 | 2011-04-20 10:31:50 +0800 | [diff] [blame] | 6005 | u64 ino = btrfs_ino(inode); |
| 6006 | u64 parent_ino = btrfs_ino(parent_inode); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 6007 | |
Li Zefan | 33345d01 | 2011-04-20 10:31:50 +0800 | [diff] [blame] | 6008 | if (unlikely(ino == BTRFS_FIRST_FREE_OBJECTID)) { |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 6009 | memcpy(&key, &BTRFS_I(inode)->root->root_key, sizeof(key)); |
| 6010 | } else { |
Li Zefan | 33345d01 | 2011-04-20 10:31:50 +0800 | [diff] [blame] | 6011 | key.objectid = ino; |
David Sterba | 962a298 | 2014-06-04 18:41:45 +0200 | [diff] [blame] | 6012 | key.type = BTRFS_INODE_ITEM_KEY; |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 6013 | key.offset = 0; |
| 6014 | } |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 6015 | |
Li Zefan | 33345d01 | 2011-04-20 10:31:50 +0800 | [diff] [blame] | 6016 | if (unlikely(ino == BTRFS_FIRST_FREE_OBJECTID)) { |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 6017 | ret = btrfs_add_root_ref(trans, root->fs_info->tree_root, |
| 6018 | key.objectid, root->root_key.objectid, |
Li Zefan | 33345d01 | 2011-04-20 10:31:50 +0800 | [diff] [blame] | 6019 | parent_ino, index, name, name_len); |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 6020 | } else if (add_backref) { |
Li Zefan | 33345d01 | 2011-04-20 10:31:50 +0800 | [diff] [blame] | 6021 | ret = btrfs_insert_inode_ref(trans, root, name, name_len, ino, |
| 6022 | parent_ino, index); |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 6023 | } |
| 6024 | |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 6025 | /* Nothing to clean up yet */ |
| 6026 | if (ret) |
| 6027 | return ret; |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 6028 | |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 6029 | ret = btrfs_insert_dir_item(trans, root, name, name_len, |
| 6030 | parent_inode, &key, |
| 6031 | btrfs_inode_type(inode), index); |
Chris Mason | 9c52057 | 2012-12-17 14:26:57 -0500 | [diff] [blame] | 6032 | if (ret == -EEXIST || ret == -EOVERFLOW) |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 6033 | goto fail_dir_item; |
| 6034 | else if (ret) { |
| 6035 | btrfs_abort_transaction(trans, root, ret); |
| 6036 | return ret; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 6037 | } |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 6038 | |
| 6039 | btrfs_i_size_write(parent_inode, parent_inode->i_size + |
| 6040 | name_len * 2); |
Josef Bacik | 0c4d2d9 | 2012-04-05 15:03:02 -0400 | [diff] [blame] | 6041 | inode_inc_iversion(parent_inode); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 6042 | parent_inode->i_mtime = parent_inode->i_ctime = CURRENT_TIME; |
| 6043 | ret = btrfs_update_inode(trans, root, parent_inode); |
| 6044 | if (ret) |
| 6045 | btrfs_abort_transaction(trans, root, ret); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 6046 | return ret; |
Chris Mason | fe66a05 | 2012-02-20 08:40:56 -0500 | [diff] [blame] | 6047 | |
| 6048 | fail_dir_item: |
| 6049 | if (unlikely(ino == BTRFS_FIRST_FREE_OBJECTID)) { |
| 6050 | u64 local_index; |
| 6051 | int err; |
| 6052 | err = btrfs_del_root_ref(trans, root->fs_info->tree_root, |
| 6053 | key.objectid, root->root_key.objectid, |
| 6054 | parent_ino, &local_index, name, name_len); |
| 6055 | |
| 6056 | } else if (add_backref) { |
| 6057 | u64 local_index; |
| 6058 | int err; |
| 6059 | |
| 6060 | err = btrfs_del_inode_ref(trans, root, name, name_len, |
| 6061 | ino, parent_ino, &local_index); |
| 6062 | } |
| 6063 | return ret; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 6064 | } |
| 6065 | |
| 6066 | static int btrfs_add_nondir(struct btrfs_trans_handle *trans, |
Josef Bacik | a1b075d | 2010-11-19 20:36:11 +0000 | [diff] [blame] | 6067 | struct inode *dir, struct dentry *dentry, |
| 6068 | struct inode *inode, int backref, u64 index) |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 6069 | { |
Josef Bacik | a1b075d | 2010-11-19 20:36:11 +0000 | [diff] [blame] | 6070 | int err = btrfs_add_link(trans, dir, inode, |
| 6071 | dentry->d_name.name, dentry->d_name.len, |
| 6072 | backref, index); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 6073 | if (err > 0) |
| 6074 | err = -EEXIST; |
| 6075 | return err; |
| 6076 | } |
| 6077 | |
Josef Bacik | 618e21d | 2007-07-11 10:18:17 -0400 | [diff] [blame] | 6078 | static int btrfs_mknod(struct inode *dir, struct dentry *dentry, |
Al Viro | 1a67aaf | 2011-07-26 01:52:52 -0400 | [diff] [blame] | 6079 | umode_t mode, dev_t rdev) |
Josef Bacik | 618e21d | 2007-07-11 10:18:17 -0400 | [diff] [blame] | 6080 | { |
| 6081 | struct btrfs_trans_handle *trans; |
| 6082 | struct btrfs_root *root = BTRFS_I(dir)->root; |
Chris Mason | 1832a6d | 2007-12-21 16:27:21 -0500 | [diff] [blame] | 6083 | struct inode *inode = NULL; |
Josef Bacik | 618e21d | 2007-07-11 10:18:17 -0400 | [diff] [blame] | 6084 | int err; |
| 6085 | int drop_inode = 0; |
| 6086 | u64 objectid; |
Chris Mason | 00e4e6b | 2008-08-05 11:18:09 -0400 | [diff] [blame] | 6087 | u64 index = 0; |
Josef Bacik | 618e21d | 2007-07-11 10:18:17 -0400 | [diff] [blame] | 6088 | |
| 6089 | if (!new_valid_dev(rdev)) |
| 6090 | return -EINVAL; |
| 6091 | |
Josef Bacik | 9ed74f2 | 2009-09-11 16:12:44 -0400 | [diff] [blame] | 6092 | /* |
| 6093 | * 2 for inode item and ref |
| 6094 | * 2 for dir items |
| 6095 | * 1 for xattr if selinux is on |
| 6096 | */ |
Yan, Zheng | a22285a | 2010-05-16 10:48:46 -0400 | [diff] [blame] | 6097 | trans = btrfs_start_transaction(root, 5); |
| 6098 | if (IS_ERR(trans)) |
| 6099 | return PTR_ERR(trans); |
Chris Mason | 1832a6d | 2007-12-21 16:27:21 -0500 | [diff] [blame] | 6100 | |
Li Zefan | 581bb05 | 2011-04-20 10:06:11 +0800 | [diff] [blame] | 6101 | err = btrfs_find_free_ino(root, &objectid); |
| 6102 | if (err) |
| 6103 | goto out_unlock; |
| 6104 | |
Josef Bacik | aec7477 | 2008-07-24 12:12:38 -0400 | [diff] [blame] | 6105 | inode = btrfs_new_inode(trans, root, dir, dentry->d_name.name, |
Li Zefan | 33345d01 | 2011-04-20 10:31:50 +0800 | [diff] [blame] | 6106 | dentry->d_name.len, btrfs_ino(dir), objectid, |
Josef Bacik | d82a6f1 | 2011-05-11 15:26:06 -0400 | [diff] [blame] | 6107 | mode, &index); |
Tsutomu Itoh | 7cf96da | 2011-04-25 19:43:53 -0400 | [diff] [blame] | 6108 | if (IS_ERR(inode)) { |
| 6109 | err = PTR_ERR(inode); |
Josef Bacik | 618e21d | 2007-07-11 10:18:17 -0400 | [diff] [blame] | 6110 | goto out_unlock; |
Tsutomu Itoh | 7cf96da | 2011-04-25 19:43:53 -0400 | [diff] [blame] | 6111 | } |
Josef Bacik | 618e21d | 2007-07-11 10:18:17 -0400 | [diff] [blame] | 6112 | |
Casey Schaufler | ad19db7 | 2011-12-15 10:09:07 -0500 | [diff] [blame] | 6113 | /* |
| 6114 | * If the active LSM wants to access the inode during |
| 6115 | * d_instantiate it needs these. Smack checks to see |
| 6116 | * if the filesystem supports xattrs by looking at the |
| 6117 | * ops vector. |
| 6118 | */ |
Casey Schaufler | ad19db7 | 2011-12-15 10:09:07 -0500 | [diff] [blame] | 6119 | inode->i_op = &btrfs_special_inode_operations; |
Chris Mason | b0d5d10 | 2014-09-08 13:08:51 -0700 | [diff] [blame] | 6120 | init_special_inode(inode, inode->i_mode, rdev); |
| 6121 | |
| 6122 | err = btrfs_init_inode_security(trans, inode, dir, &dentry->d_name); |
Josef Bacik | 618e21d | 2007-07-11 10:18:17 -0400 | [diff] [blame] | 6123 | if (err) |
Chris Mason | b0d5d10 | 2014-09-08 13:08:51 -0700 | [diff] [blame] | 6124 | goto out_unlock_inode; |
| 6125 | |
| 6126 | err = btrfs_add_nondir(trans, dir, dentry, inode, 0, index); |
| 6127 | if (err) { |
| 6128 | goto out_unlock_inode; |
| 6129 | } else { |
Yan | 1b4ab1b | 2007-08-29 09:11:44 -0400 | [diff] [blame] | 6130 | btrfs_update_inode(trans, root, inode); |
Chris Mason | b0d5d10 | 2014-09-08 13:08:51 -0700 | [diff] [blame] | 6131 | unlock_new_inode(inode); |
Al Viro | 08c422c | 2011-12-23 07:58:13 -0500 | [diff] [blame] | 6132 | d_instantiate(dentry, inode); |
Josef Bacik | 618e21d | 2007-07-11 10:18:17 -0400 | [diff] [blame] | 6133 | } |
Chris Mason | b0d5d10 | 2014-09-08 13:08:51 -0700 | [diff] [blame] | 6134 | |
Josef Bacik | 618e21d | 2007-07-11 10:18:17 -0400 | [diff] [blame] | 6135 | out_unlock: |
Josef Bacik | 7ad85bb | 2012-01-12 19:10:12 -0500 | [diff] [blame] | 6136 | btrfs_end_transaction(trans, root); |
Josef Bacik | c581afc | 2014-02-06 16:06:06 -0500 | [diff] [blame] | 6137 | btrfs_balance_delayed_items(root); |
Liu Bo | b53d3f5 | 2012-11-14 14:34:34 +0000 | [diff] [blame] | 6138 | btrfs_btree_balance_dirty(root); |
Josef Bacik | 618e21d | 2007-07-11 10:18:17 -0400 | [diff] [blame] | 6139 | if (drop_inode) { |
| 6140 | inode_dec_link_count(inode); |
| 6141 | iput(inode); |
| 6142 | } |
Josef Bacik | 618e21d | 2007-07-11 10:18:17 -0400 | [diff] [blame] | 6143 | return err; |
Chris Mason | b0d5d10 | 2014-09-08 13:08:51 -0700 | [diff] [blame] | 6144 | |
| 6145 | out_unlock_inode: |
| 6146 | drop_inode = 1; |
| 6147 | unlock_new_inode(inode); |
| 6148 | goto out_unlock; |
| 6149 | |
Josef Bacik | 618e21d | 2007-07-11 10:18:17 -0400 | [diff] [blame] | 6150 | } |
| 6151 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 6152 | static int btrfs_create(struct inode *dir, struct dentry *dentry, |
Al Viro | ebfc3b4 | 2012-06-10 18:05:36 -0400 | [diff] [blame] | 6153 | umode_t mode, bool excl) |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 6154 | { |
| 6155 | struct btrfs_trans_handle *trans; |
| 6156 | struct btrfs_root *root = BTRFS_I(dir)->root; |
Chris Mason | 1832a6d | 2007-12-21 16:27:21 -0500 | [diff] [blame] | 6157 | struct inode *inode = NULL; |
Filipe Brandenburger | 43baa57 | 2012-11-30 03:40:09 +0000 | [diff] [blame] | 6158 | int drop_inode_on_err = 0; |
Yan, Zheng | a22285a | 2010-05-16 10:48:46 -0400 | [diff] [blame] | 6159 | int err; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 6160 | u64 objectid; |
Chris Mason | 00e4e6b | 2008-08-05 11:18:09 -0400 | [diff] [blame] | 6161 | u64 index = 0; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 6162 | |
Josef Bacik | 9ed74f2 | 2009-09-11 16:12:44 -0400 | [diff] [blame] | 6163 | /* |
| 6164 | * 2 for inode item and ref |
| 6165 | * 2 for dir items |
| 6166 | * 1 for xattr if selinux is on |
| 6167 | */ |
Yan, Zheng | a22285a | 2010-05-16 10:48:46 -0400 | [diff] [blame] | 6168 | trans = btrfs_start_transaction(root, 5); |
| 6169 | if (IS_ERR(trans)) |
| 6170 | return PTR_ERR(trans); |
Josef Bacik | 9ed74f2 | 2009-09-11 16:12:44 -0400 | [diff] [blame] | 6171 | |
Li Zefan | 581bb05 | 2011-04-20 10:06:11 +0800 | [diff] [blame] | 6172 | err = btrfs_find_free_ino(root, &objectid); |
| 6173 | if (err) |
| 6174 | goto out_unlock; |
| 6175 | |
Josef Bacik | aec7477 | 2008-07-24 12:12:38 -0400 | [diff] [blame] | 6176 | inode = btrfs_new_inode(trans, root, dir, dentry->d_name.name, |
Li Zefan | 33345d01 | 2011-04-20 10:31:50 +0800 | [diff] [blame] | 6177 | dentry->d_name.len, btrfs_ino(dir), objectid, |
Josef Bacik | d82a6f1 | 2011-05-11 15:26:06 -0400 | [diff] [blame] | 6178 | mode, &index); |
Tsutomu Itoh | 7cf96da | 2011-04-25 19:43:53 -0400 | [diff] [blame] | 6179 | if (IS_ERR(inode)) { |
| 6180 | err = PTR_ERR(inode); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 6181 | goto out_unlock; |
Tsutomu Itoh | 7cf96da | 2011-04-25 19:43:53 -0400 | [diff] [blame] | 6182 | } |
Filipe Brandenburger | 43baa57 | 2012-11-30 03:40:09 +0000 | [diff] [blame] | 6183 | drop_inode_on_err = 1; |
Casey Schaufler | ad19db7 | 2011-12-15 10:09:07 -0500 | [diff] [blame] | 6184 | /* |
| 6185 | * If the active LSM wants to access the inode during |
| 6186 | * d_instantiate it needs these. Smack checks to see |
| 6187 | * if the filesystem supports xattrs by looking at the |
| 6188 | * ops vector. |
| 6189 | */ |
| 6190 | inode->i_fop = &btrfs_file_operations; |
| 6191 | inode->i_op = &btrfs_file_inode_operations; |
Chris Mason | b0d5d10 | 2014-09-08 13:08:51 -0700 | [diff] [blame] | 6192 | inode->i_mapping->a_ops = &btrfs_aops; |
| 6193 | inode->i_mapping->backing_dev_info = &root->fs_info->bdi; |
| 6194 | |
| 6195 | err = btrfs_init_inode_security(trans, inode, dir, &dentry->d_name); |
| 6196 | if (err) |
| 6197 | goto out_unlock_inode; |
| 6198 | |
| 6199 | err = btrfs_update_inode(trans, root, inode); |
| 6200 | if (err) |
| 6201 | goto out_unlock_inode; |
Casey Schaufler | ad19db7 | 2011-12-15 10:09:07 -0500 | [diff] [blame] | 6202 | |
Josef Bacik | a1b075d | 2010-11-19 20:36:11 +0000 | [diff] [blame] | 6203 | err = btrfs_add_nondir(trans, dir, dentry, inode, 0, index); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 6204 | if (err) |
Chris Mason | b0d5d10 | 2014-09-08 13:08:51 -0700 | [diff] [blame] | 6205 | goto out_unlock_inode; |
Filipe Brandenburger | 43baa57 | 2012-11-30 03:40:09 +0000 | [diff] [blame] | 6206 | |
Filipe Brandenburger | 43baa57 | 2012-11-30 03:40:09 +0000 | [diff] [blame] | 6207 | BTRFS_I(inode)->io_tree.ops = &btrfs_extent_io_ops; |
Chris Mason | b0d5d10 | 2014-09-08 13:08:51 -0700 | [diff] [blame] | 6208 | unlock_new_inode(inode); |
Filipe Brandenburger | 43baa57 | 2012-11-30 03:40:09 +0000 | [diff] [blame] | 6209 | d_instantiate(dentry, inode); |
| 6210 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 6211 | out_unlock: |
Josef Bacik | 7ad85bb | 2012-01-12 19:10:12 -0500 | [diff] [blame] | 6212 | btrfs_end_transaction(trans, root); |
Filipe Brandenburger | 43baa57 | 2012-11-30 03:40:09 +0000 | [diff] [blame] | 6213 | if (err && drop_inode_on_err) { |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 6214 | inode_dec_link_count(inode); |
| 6215 | iput(inode); |
| 6216 | } |
Josef Bacik | c581afc | 2014-02-06 16:06:06 -0500 | [diff] [blame] | 6217 | btrfs_balance_delayed_items(root); |
Liu Bo | b53d3f5 | 2012-11-14 14:34:34 +0000 | [diff] [blame] | 6218 | btrfs_btree_balance_dirty(root); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 6219 | return err; |
Chris Mason | b0d5d10 | 2014-09-08 13:08:51 -0700 | [diff] [blame] | 6220 | |
| 6221 | out_unlock_inode: |
| 6222 | unlock_new_inode(inode); |
| 6223 | goto out_unlock; |
| 6224 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 6225 | } |
| 6226 | |
| 6227 | static int btrfs_link(struct dentry *old_dentry, struct inode *dir, |
| 6228 | struct dentry *dentry) |
| 6229 | { |
| 6230 | struct btrfs_trans_handle *trans; |
| 6231 | struct btrfs_root *root = BTRFS_I(dir)->root; |
| 6232 | struct inode *inode = old_dentry->d_inode; |
Chris Mason | 00e4e6b | 2008-08-05 11:18:09 -0400 | [diff] [blame] | 6233 | u64 index; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 6234 | int err; |
| 6235 | int drop_inode = 0; |
| 6236 | |
TARUISI Hiroaki | 4a8be42 | 2009-11-12 07:14:26 +0000 | [diff] [blame] | 6237 | /* do not allow sys_link's with other subvols of the same device */ |
| 6238 | if (root->objectid != BTRFS_I(inode)->root->objectid) |
Mark Fasheh | 3ab3564 | 2011-03-22 17:20:26 +0000 | [diff] [blame] | 6239 | return -EXDEV; |
TARUISI Hiroaki | 4a8be42 | 2009-11-12 07:14:26 +0000 | [diff] [blame] | 6240 | |
Mark Fasheh | f186373 | 2012-08-08 11:32:27 -0700 | [diff] [blame] | 6241 | if (inode->i_nlink >= BTRFS_LINK_MAX) |
Al Viro | c055e99 | 2011-03-04 17:15:18 +0000 | [diff] [blame] | 6242 | return -EMLINK; |
Josef Bacik | 9ed74f2 | 2009-09-11 16:12:44 -0400 | [diff] [blame] | 6243 | |
Chris Mason | 3de4586 | 2008-11-17 21:02:50 -0500 | [diff] [blame] | 6244 | err = btrfs_set_inode_index(dir, &index); |
Josef Bacik | aec7477 | 2008-07-24 12:12:38 -0400 | [diff] [blame] | 6245 | if (err) |
| 6246 | goto fail; |
| 6247 | |
Yan, Zheng | a22285a | 2010-05-16 10:48:46 -0400 | [diff] [blame] | 6248 | /* |
Miao Xie | 7e6b646 | 2011-02-18 09:21:17 +0000 | [diff] [blame] | 6249 | * 2 items for inode and inode ref |
Yan, Zheng | a22285a | 2010-05-16 10:48:46 -0400 | [diff] [blame] | 6250 | * 2 items for dir items |
Miao Xie | 7e6b646 | 2011-02-18 09:21:17 +0000 | [diff] [blame] | 6251 | * 1 item for parent inode |
Yan, Zheng | a22285a | 2010-05-16 10:48:46 -0400 | [diff] [blame] | 6252 | */ |
Miao Xie | 7e6b646 | 2011-02-18 09:21:17 +0000 | [diff] [blame] | 6253 | trans = btrfs_start_transaction(root, 5); |
Yan, Zheng | a22285a | 2010-05-16 10:48:46 -0400 | [diff] [blame] | 6254 | if (IS_ERR(trans)) { |
| 6255 | err = PTR_ERR(trans); |
| 6256 | goto fail; |
| 6257 | } |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 6258 | |
Miao Xie | 67de117 | 2013-12-26 13:07:06 +0800 | [diff] [blame] | 6259 | /* There are several dir indexes for this inode, clear the cache. */ |
| 6260 | BTRFS_I(inode)->dir_index = 0ULL; |
Zach Brown | 8b558c5 | 2013-10-16 12:10:34 -0700 | [diff] [blame] | 6261 | inc_nlink(inode); |
Josef Bacik | 0c4d2d9 | 2012-04-05 15:03:02 -0400 | [diff] [blame] | 6262 | inode_inc_iversion(inode); |
Miao Xie | 3153495 | 2011-04-13 13:19:21 +0800 | [diff] [blame] | 6263 | inode->i_ctime = CURRENT_TIME; |
Al Viro | 7de9c6ee | 2010-10-23 11:11:40 -0400 | [diff] [blame] | 6264 | ihold(inode); |
Josef Bacik | e997615 | 2012-10-11 15:53:56 -0400 | [diff] [blame] | 6265 | set_bit(BTRFS_INODE_COPY_EVERYTHING, &BTRFS_I(inode)->runtime_flags); |
Josef Bacik | aec7477 | 2008-07-24 12:12:38 -0400 | [diff] [blame] | 6266 | |
Josef Bacik | a1b075d | 2010-11-19 20:36:11 +0000 | [diff] [blame] | 6267 | err = btrfs_add_nondir(trans, dir, dentry, inode, 1, index); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 6268 | |
Yan, Zheng | a571952 | 2009-09-24 09:17:31 -0400 | [diff] [blame] | 6269 | if (err) { |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 6270 | drop_inode = 1; |
Yan, Zheng | a571952 | 2009-09-24 09:17:31 -0400 | [diff] [blame] | 6271 | } else { |
Al Viro | 10d9f30 | 2011-07-16 23:09:10 -0400 | [diff] [blame] | 6272 | struct dentry *parent = dentry->d_parent; |
Yan, Zheng | a571952 | 2009-09-24 09:17:31 -0400 | [diff] [blame] | 6273 | err = btrfs_update_inode(trans, root, inode); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 6274 | if (err) |
| 6275 | goto fail; |
Filipe Manana | ef3b9af | 2014-04-27 20:40:45 +0100 | [diff] [blame] | 6276 | if (inode->i_nlink == 1) { |
| 6277 | /* |
| 6278 | * If new hard link count is 1, it's a file created |
| 6279 | * with open(2) O_TMPFILE flag. |
| 6280 | */ |
| 6281 | err = btrfs_orphan_del(trans, inode); |
| 6282 | if (err) |
| 6283 | goto fail; |
| 6284 | } |
Al Viro | 08c422c | 2011-12-23 07:58:13 -0500 | [diff] [blame] | 6285 | d_instantiate(dentry, inode); |
Josef Bacik | 6a91221 | 2010-11-20 09:48:00 +0000 | [diff] [blame] | 6286 | btrfs_log_new_name(trans, inode, NULL, parent); |
Yan, Zheng | a571952 | 2009-09-24 09:17:31 -0400 | [diff] [blame] | 6287 | } |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 6288 | |
Josef Bacik | 7ad85bb | 2012-01-12 19:10:12 -0500 | [diff] [blame] | 6289 | btrfs_end_transaction(trans, root); |
Josef Bacik | c581afc | 2014-02-06 16:06:06 -0500 | [diff] [blame] | 6290 | btrfs_balance_delayed_items(root); |
Chris Mason | 1832a6d | 2007-12-21 16:27:21 -0500 | [diff] [blame] | 6291 | fail: |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 6292 | if (drop_inode) { |
| 6293 | inode_dec_link_count(inode); |
| 6294 | iput(inode); |
| 6295 | } |
Liu Bo | b53d3f5 | 2012-11-14 14:34:34 +0000 | [diff] [blame] | 6296 | btrfs_btree_balance_dirty(root); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 6297 | return err; |
| 6298 | } |
| 6299 | |
Al Viro | 18bb1db | 2011-07-26 01:41:39 -0400 | [diff] [blame] | 6300 | 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] | 6301 | { |
Chris Mason | b9d8666 | 2008-05-02 16:13:49 -0400 | [diff] [blame] | 6302 | struct inode *inode = NULL; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 6303 | struct btrfs_trans_handle *trans; |
| 6304 | struct btrfs_root *root = BTRFS_I(dir)->root; |
| 6305 | int err = 0; |
| 6306 | int drop_on_err = 0; |
Chris Mason | b9d8666 | 2008-05-02 16:13:49 -0400 | [diff] [blame] | 6307 | u64 objectid = 0; |
Chris Mason | 00e4e6b | 2008-08-05 11:18:09 -0400 | [diff] [blame] | 6308 | u64 index = 0; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 6309 | |
Josef Bacik | 9ed74f2 | 2009-09-11 16:12:44 -0400 | [diff] [blame] | 6310 | /* |
| 6311 | * 2 items for inode and ref |
| 6312 | * 2 items for dir items |
| 6313 | * 1 for xattr if selinux is on |
| 6314 | */ |
Yan, Zheng | a22285a | 2010-05-16 10:48:46 -0400 | [diff] [blame] | 6315 | trans = btrfs_start_transaction(root, 5); |
| 6316 | if (IS_ERR(trans)) |
| 6317 | return PTR_ERR(trans); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 6318 | |
Li Zefan | 581bb05 | 2011-04-20 10:06:11 +0800 | [diff] [blame] | 6319 | err = btrfs_find_free_ino(root, &objectid); |
| 6320 | if (err) |
| 6321 | goto out_fail; |
| 6322 | |
Josef Bacik | aec7477 | 2008-07-24 12:12:38 -0400 | [diff] [blame] | 6323 | inode = btrfs_new_inode(trans, root, dir, dentry->d_name.name, |
Li Zefan | 33345d01 | 2011-04-20 10:31:50 +0800 | [diff] [blame] | 6324 | dentry->d_name.len, btrfs_ino(dir), objectid, |
Josef Bacik | d82a6f1 | 2011-05-11 15:26:06 -0400 | [diff] [blame] | 6325 | S_IFDIR | mode, &index); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 6326 | if (IS_ERR(inode)) { |
| 6327 | err = PTR_ERR(inode); |
| 6328 | goto out_fail; |
| 6329 | } |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 6330 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 6331 | drop_on_err = 1; |
Chris Mason | b0d5d10 | 2014-09-08 13:08:51 -0700 | [diff] [blame] | 6332 | /* these must be set before we unlock the inode */ |
| 6333 | inode->i_op = &btrfs_dir_inode_operations; |
| 6334 | inode->i_fop = &btrfs_dir_file_operations; |
Josef Bacik | 33268ea | 2008-07-24 12:16:36 -0400 | [diff] [blame] | 6335 | |
Eric Paris | 2a7dba3 | 2011-02-01 11:05:39 -0500 | [diff] [blame] | 6336 | err = btrfs_init_inode_security(trans, inode, dir, &dentry->d_name); |
Josef Bacik | 33268ea | 2008-07-24 12:16:36 -0400 | [diff] [blame] | 6337 | if (err) |
Chris Mason | b0d5d10 | 2014-09-08 13:08:51 -0700 | [diff] [blame] | 6338 | goto out_fail_inode; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 6339 | |
Chris Mason | dbe674a | 2008-07-17 12:54:05 -0400 | [diff] [blame] | 6340 | btrfs_i_size_write(inode, 0); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 6341 | err = btrfs_update_inode(trans, root, inode); |
| 6342 | if (err) |
Chris Mason | b0d5d10 | 2014-09-08 13:08:51 -0700 | [diff] [blame] | 6343 | goto out_fail_inode; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 6344 | |
Josef Bacik | a1b075d | 2010-11-19 20:36:11 +0000 | [diff] [blame] | 6345 | err = btrfs_add_link(trans, dir, inode, dentry->d_name.name, |
| 6346 | dentry->d_name.len, 0, index); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 6347 | if (err) |
Chris Mason | b0d5d10 | 2014-09-08 13:08:51 -0700 | [diff] [blame] | 6348 | goto out_fail_inode; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 6349 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 6350 | d_instantiate(dentry, inode); |
Chris Mason | b0d5d10 | 2014-09-08 13:08:51 -0700 | [diff] [blame] | 6351 | /* |
| 6352 | * mkdir is special. We're unlocking after we call d_instantiate |
| 6353 | * to avoid a race with nfsd calling d_instantiate. |
| 6354 | */ |
| 6355 | unlock_new_inode(inode); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 6356 | drop_on_err = 0; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 6357 | |
| 6358 | out_fail: |
Josef Bacik | 7ad85bb | 2012-01-12 19:10:12 -0500 | [diff] [blame] | 6359 | btrfs_end_transaction(trans, root); |
Wang Shilong | c7cfb8a | 2014-12-24 14:45:30 +0800 | [diff] [blame] | 6360 | if (drop_on_err) { |
| 6361 | inode_dec_link_count(inode); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 6362 | iput(inode); |
Wang Shilong | c7cfb8a | 2014-12-24 14:45:30 +0800 | [diff] [blame] | 6363 | } |
Josef Bacik | c581afc | 2014-02-06 16:06:06 -0500 | [diff] [blame] | 6364 | btrfs_balance_delayed_items(root); |
Liu Bo | b53d3f5 | 2012-11-14 14:34:34 +0000 | [diff] [blame] | 6365 | btrfs_btree_balance_dirty(root); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 6366 | return err; |
Chris Mason | b0d5d10 | 2014-09-08 13:08:51 -0700 | [diff] [blame] | 6367 | |
| 6368 | out_fail_inode: |
| 6369 | unlock_new_inode(inode); |
| 6370 | goto out_fail; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 6371 | } |
| 6372 | |
Qu Wenruo | e6c4efd | 2014-09-17 11:53:35 +0800 | [diff] [blame] | 6373 | /* Find next extent map of a given extent map, caller needs to ensure locks */ |
| 6374 | static struct extent_map *next_extent_map(struct extent_map *em) |
| 6375 | { |
| 6376 | struct rb_node *next; |
| 6377 | |
| 6378 | next = rb_next(&em->rb_node); |
| 6379 | if (!next) |
| 6380 | return NULL; |
| 6381 | return container_of(next, struct extent_map, rb_node); |
| 6382 | } |
| 6383 | |
| 6384 | static struct extent_map *prev_extent_map(struct extent_map *em) |
| 6385 | { |
| 6386 | struct rb_node *prev; |
| 6387 | |
| 6388 | prev = rb_prev(&em->rb_node); |
| 6389 | if (!prev) |
| 6390 | return NULL; |
| 6391 | return container_of(prev, struct extent_map, rb_node); |
| 6392 | } |
| 6393 | |
Chris Mason | d352ac6 | 2008-09-29 15:18:18 -0400 | [diff] [blame] | 6394 | /* helper for btfs_get_extent. Given an existing extent in the tree, |
Qu Wenruo | e6c4efd | 2014-09-17 11:53:35 +0800 | [diff] [blame] | 6395 | * the existing extent is the nearest extent to map_start, |
Chris Mason | d352ac6 | 2008-09-29 15:18:18 -0400 | [diff] [blame] | 6396 | * 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] | 6397 | * the best fitted new extent into the tree. |
Chris Mason | d352ac6 | 2008-09-29 15:18:18 -0400 | [diff] [blame] | 6398 | */ |
Chris Mason | 3b95151 | 2008-04-17 11:29:12 -0400 | [diff] [blame] | 6399 | static int merge_extent_mapping(struct extent_map_tree *em_tree, |
| 6400 | struct extent_map *existing, |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 6401 | struct extent_map *em, |
Qu Wenruo | 51f395a | 2014-08-08 13:06:20 +0800 | [diff] [blame] | 6402 | u64 map_start) |
Chris Mason | 3b95151 | 2008-04-17 11:29:12 -0400 | [diff] [blame] | 6403 | { |
Qu Wenruo | e6c4efd | 2014-09-17 11:53:35 +0800 | [diff] [blame] | 6404 | struct extent_map *prev; |
| 6405 | struct extent_map *next; |
| 6406 | u64 start; |
| 6407 | u64 end; |
Chris Mason | 3b95151 | 2008-04-17 11:29:12 -0400 | [diff] [blame] | 6408 | u64 start_diff; |
Chris Mason | 3b95151 | 2008-04-17 11:29:12 -0400 | [diff] [blame] | 6409 | |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 6410 | BUG_ON(map_start < em->start || map_start >= extent_map_end(em)); |
Qu Wenruo | e6c4efd | 2014-09-17 11:53:35 +0800 | [diff] [blame] | 6411 | |
| 6412 | if (existing->start > map_start) { |
| 6413 | next = existing; |
| 6414 | prev = prev_extent_map(next); |
| 6415 | } else { |
| 6416 | prev = existing; |
| 6417 | next = next_extent_map(prev); |
| 6418 | } |
| 6419 | |
| 6420 | start = prev ? extent_map_end(prev) : em->start; |
| 6421 | start = max_t(u64, start, em->start); |
| 6422 | end = next ? next->start : extent_map_end(em); |
| 6423 | end = min_t(u64, end, extent_map_end(em)); |
| 6424 | start_diff = start - em->start; |
| 6425 | em->start = start; |
| 6426 | em->len = end - start; |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 6427 | if (em->block_start < EXTENT_MAP_LAST_BYTE && |
| 6428 | !test_bit(EXTENT_FLAG_COMPRESSED, &em->flags)) { |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 6429 | em->block_start += start_diff; |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 6430 | em->block_len -= start_diff; |
| 6431 | } |
Josef Bacik | 09a2a8f9 | 2013-04-05 16:51:15 -0400 | [diff] [blame] | 6432 | return add_extent_mapping(em_tree, em, 0); |
Chris Mason | 3b95151 | 2008-04-17 11:29:12 -0400 | [diff] [blame] | 6433 | } |
| 6434 | |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 6435 | static noinline int uncompress_inline(struct btrfs_path *path, |
| 6436 | struct inode *inode, struct page *page, |
| 6437 | size_t pg_offset, u64 extent_offset, |
| 6438 | struct btrfs_file_extent_item *item) |
| 6439 | { |
| 6440 | int ret; |
| 6441 | struct extent_buffer *leaf = path->nodes[0]; |
| 6442 | char *tmp; |
| 6443 | size_t max_size; |
| 6444 | unsigned long inline_size; |
| 6445 | unsigned long ptr; |
Li Zefan | 261507a0 | 2010-12-17 14:21:50 +0800 | [diff] [blame] | 6446 | int compress_type; |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 6447 | |
| 6448 | WARN_ON(pg_offset != 0); |
Li Zefan | 261507a0 | 2010-12-17 14:21:50 +0800 | [diff] [blame] | 6449 | compress_type = btrfs_file_extent_compression(leaf, item); |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 6450 | max_size = btrfs_file_extent_ram_bytes(leaf, item); |
| 6451 | inline_size = btrfs_file_extent_inline_item_len(leaf, |
Ross Kirk | dd3cc16 | 2013-09-16 15:58:09 +0100 | [diff] [blame] | 6452 | btrfs_item_nr(path->slots[0])); |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 6453 | tmp = kmalloc(inline_size, GFP_NOFS); |
Tsutomu Itoh | 8d41371 | 2011-04-25 19:43:52 -0400 | [diff] [blame] | 6454 | if (!tmp) |
| 6455 | return -ENOMEM; |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 6456 | ptr = btrfs_file_extent_inline_start(item); |
| 6457 | |
| 6458 | read_extent_buffer(leaf, tmp, ptr, inline_size); |
| 6459 | |
Chris Mason | 5b050f0 | 2008-11-11 09:34:41 -0500 | [diff] [blame] | 6460 | max_size = min_t(unsigned long, PAGE_CACHE_SIZE, max_size); |
Li Zefan | 261507a0 | 2010-12-17 14:21:50 +0800 | [diff] [blame] | 6461 | ret = btrfs_decompress(compress_type, tmp, page, |
| 6462 | extent_offset, inline_size, max_size); |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 6463 | kfree(tmp); |
Zach Brown | 166ae5a | 2014-05-09 17:15:10 -0400 | [diff] [blame] | 6464 | return ret; |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 6465 | } |
| 6466 | |
Chris Mason | d352ac6 | 2008-09-29 15:18:18 -0400 | [diff] [blame] | 6467 | /* |
| 6468 | * 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] | 6469 | * the ugly parts come from merging extents from the disk with the in-ram |
| 6470 | * representation. This gets more complex because of the data=ordered code, |
Chris Mason | d352ac6 | 2008-09-29 15:18:18 -0400 | [diff] [blame] | 6471 | * where the in-ram extents might be locked pending data=ordered completion. |
| 6472 | * |
| 6473 | * This also copies inline extents directly into the page. |
| 6474 | */ |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 6475 | |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 6476 | struct extent_map *btrfs_get_extent(struct inode *inode, struct page *page, |
Chris Mason | 70dec80 | 2008-01-29 09:59:12 -0500 | [diff] [blame] | 6477 | size_t pg_offset, u64 start, u64 len, |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 6478 | int create) |
| 6479 | { |
| 6480 | int ret; |
| 6481 | int err = 0; |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 6482 | u64 extent_start = 0; |
| 6483 | u64 extent_end = 0; |
Li Zefan | 33345d01 | 2011-04-20 10:31:50 +0800 | [diff] [blame] | 6484 | u64 objectid = btrfs_ino(inode); |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 6485 | u32 found_type; |
Chris Mason | f421950 | 2008-07-22 11:18:09 -0400 | [diff] [blame] | 6486 | struct btrfs_path *path = NULL; |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 6487 | struct btrfs_root *root = BTRFS_I(inode)->root; |
| 6488 | struct btrfs_file_extent_item *item; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 6489 | struct extent_buffer *leaf; |
| 6490 | struct btrfs_key found_key; |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 6491 | struct extent_map *em = NULL; |
| 6492 | struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree; |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 6493 | struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree; |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 6494 | struct btrfs_trans_handle *trans = NULL; |
Filipe Manana | 7ffbb59 | 2014-06-09 03:48:05 +0100 | [diff] [blame] | 6495 | const bool new_inline = !page || create; |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 6496 | |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 6497 | again: |
Chris Mason | 890871b | 2009-09-02 16:24:52 -0400 | [diff] [blame] | 6498 | read_lock(&em_tree->lock); |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 6499 | em = lookup_extent_mapping(em_tree, start, len); |
Chris Mason | a061fc8 | 2008-05-07 11:43:44 -0400 | [diff] [blame] | 6500 | if (em) |
| 6501 | em->bdev = root->fs_info->fs_devices->latest_bdev; |
Chris Mason | 890871b | 2009-09-02 16:24:52 -0400 | [diff] [blame] | 6502 | read_unlock(&em_tree->lock); |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 6503 | |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 6504 | if (em) { |
Chris Mason | e1c4b74 | 2008-04-22 13:26:46 -0400 | [diff] [blame] | 6505 | if (em->start > start || em->start + em->len <= start) |
| 6506 | free_extent_map(em); |
| 6507 | else if (em->block_start == EXTENT_MAP_INLINE && page) |
Chris Mason | 70dec80 | 2008-01-29 09:59:12 -0500 | [diff] [blame] | 6508 | free_extent_map(em); |
| 6509 | else |
| 6510 | goto out; |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 6511 | } |
David Sterba | 172ddd6 | 2011-04-21 00:48:27 +0200 | [diff] [blame] | 6512 | em = alloc_extent_map(); |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 6513 | if (!em) { |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 6514 | err = -ENOMEM; |
| 6515 | goto out; |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 6516 | } |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 6517 | em->bdev = root->fs_info->fs_devices->latest_bdev; |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 6518 | em->start = EXTENT_MAP_HOLE; |
Chris Mason | 445a694 | 2008-11-10 11:53:33 -0500 | [diff] [blame] | 6519 | em->orig_start = EXTENT_MAP_HOLE; |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 6520 | em->len = (u64)-1; |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 6521 | em->block_len = (u64)-1; |
Chris Mason | f421950 | 2008-07-22 11:18:09 -0400 | [diff] [blame] | 6522 | |
| 6523 | if (!path) { |
| 6524 | path = btrfs_alloc_path(); |
Josef Bacik | 026fd31 | 2011-05-13 10:32:11 -0400 | [diff] [blame] | 6525 | if (!path) { |
| 6526 | err = -ENOMEM; |
| 6527 | goto out; |
| 6528 | } |
| 6529 | /* |
| 6530 | * Chances are we'll be called again, so go ahead and do |
| 6531 | * readahead |
| 6532 | */ |
| 6533 | path->reada = 1; |
Chris Mason | f421950 | 2008-07-22 11:18:09 -0400 | [diff] [blame] | 6534 | } |
| 6535 | |
Chris Mason | 179e29e | 2007-11-01 11:28:41 -0400 | [diff] [blame] | 6536 | ret = btrfs_lookup_file_extent(trans, root, path, |
| 6537 | objectid, start, trans != NULL); |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 6538 | if (ret < 0) { |
| 6539 | err = ret; |
| 6540 | goto out; |
| 6541 | } |
| 6542 | |
| 6543 | if (ret != 0) { |
| 6544 | if (path->slots[0] == 0) |
| 6545 | goto not_found; |
| 6546 | path->slots[0]--; |
| 6547 | } |
| 6548 | |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 6549 | leaf = path->nodes[0]; |
| 6550 | item = btrfs_item_ptr(leaf, path->slots[0], |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 6551 | struct btrfs_file_extent_item); |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 6552 | /* are we inside the extent that was found? */ |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 6553 | btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]); |
David Sterba | 962a298 | 2014-06-04 18:41:45 +0200 | [diff] [blame] | 6554 | found_type = found_key.type; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 6555 | if (found_key.objectid != objectid || |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 6556 | found_type != BTRFS_EXTENT_DATA_KEY) { |
Josef Bacik | 25a5034 | 2013-10-14 12:08:38 -0400 | [diff] [blame] | 6557 | /* |
| 6558 | * If we backup past the first extent we want to move forward |
| 6559 | * and see if there is an extent in front of us, otherwise we'll |
| 6560 | * say there is a hole for our whole search range which can |
| 6561 | * cause problems. |
| 6562 | */ |
| 6563 | extent_end = start; |
| 6564 | goto next; |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 6565 | } |
| 6566 | |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 6567 | found_type = btrfs_file_extent_type(leaf, item); |
| 6568 | extent_start = found_key.offset; |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 6569 | if (found_type == BTRFS_FILE_EXTENT_REG || |
| 6570 | found_type == BTRFS_FILE_EXTENT_PREALLOC) { |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 6571 | extent_end = extent_start + |
Chris Mason | db94535 | 2007-10-15 16:15:53 -0400 | [diff] [blame] | 6572 | btrfs_file_extent_num_bytes(leaf, item); |
Yan Zheng | 9036c10 | 2008-10-30 14:19:41 -0400 | [diff] [blame] | 6573 | } else if (found_type == BTRFS_FILE_EXTENT_INLINE) { |
| 6574 | size_t size; |
Chris Mason | 514ac8a | 2014-01-03 21:07:00 -0800 | [diff] [blame] | 6575 | size = btrfs_file_extent_inline_len(leaf, path->slots[0], item); |
Qu Wenruo | fda2832 | 2013-02-26 08:10:22 +0000 | [diff] [blame] | 6576 | extent_end = ALIGN(extent_start + size, root->sectorsize); |
Yan Zheng | 9036c10 | 2008-10-30 14:19:41 -0400 | [diff] [blame] | 6577 | } |
Josef Bacik | 25a5034 | 2013-10-14 12:08:38 -0400 | [diff] [blame] | 6578 | next: |
Yan Zheng | 9036c10 | 2008-10-30 14:19:41 -0400 | [diff] [blame] | 6579 | if (start >= extent_end) { |
| 6580 | path->slots[0]++; |
| 6581 | if (path->slots[0] >= btrfs_header_nritems(leaf)) { |
| 6582 | ret = btrfs_next_leaf(root, path); |
| 6583 | if (ret < 0) { |
| 6584 | err = ret; |
| 6585 | goto out; |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 6586 | } |
Yan Zheng | 9036c10 | 2008-10-30 14:19:41 -0400 | [diff] [blame] | 6587 | if (ret > 0) |
| 6588 | goto not_found; |
| 6589 | leaf = path->nodes[0]; |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 6590 | } |
Yan Zheng | 9036c10 | 2008-10-30 14:19:41 -0400 | [diff] [blame] | 6591 | btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]); |
| 6592 | if (found_key.objectid != objectid || |
| 6593 | found_key.type != BTRFS_EXTENT_DATA_KEY) |
| 6594 | goto not_found; |
| 6595 | if (start + len <= found_key.offset) |
| 6596 | goto not_found; |
Wang Shilong | e2eca69 | 2014-07-17 11:44:14 +0800 | [diff] [blame] | 6597 | if (start > found_key.offset) |
| 6598 | goto next; |
Yan Zheng | 9036c10 | 2008-10-30 14:19:41 -0400 | [diff] [blame] | 6599 | em->start = start; |
Josef Bacik | 70c8a91 | 2012-10-11 16:54:30 -0400 | [diff] [blame] | 6600 | em->orig_start = start; |
Yan Zheng | 9036c10 | 2008-10-30 14:19:41 -0400 | [diff] [blame] | 6601 | em->len = found_key.offset - start; |
| 6602 | goto not_found_em; |
| 6603 | } |
| 6604 | |
Filipe Manana | 7ffbb59 | 2014-06-09 03:48:05 +0100 | [diff] [blame] | 6605 | btrfs_extent_item_to_extent_map(inode, path, item, new_inline, em); |
| 6606 | |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 6607 | if (found_type == BTRFS_FILE_EXTENT_REG || |
| 6608 | found_type == BTRFS_FILE_EXTENT_PREALLOC) { |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 6609 | goto insert; |
| 6610 | } else if (found_type == BTRFS_FILE_EXTENT_INLINE) { |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 6611 | unsigned long ptr; |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 6612 | char *map; |
Chris Mason | 3326d1b | 2007-10-15 16:18:25 -0400 | [diff] [blame] | 6613 | size_t size; |
| 6614 | size_t extent_offset; |
| 6615 | size_t copy_size; |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 6616 | |
Filipe Manana | 7ffbb59 | 2014-06-09 03:48:05 +0100 | [diff] [blame] | 6617 | if (new_inline) |
Yan | 689f934 | 2007-10-29 11:41:07 -0400 | [diff] [blame] | 6618 | goto out; |
Yan | 689f934 | 2007-10-29 11:41:07 -0400 | [diff] [blame] | 6619 | |
Chris Mason | 514ac8a | 2014-01-03 21:07:00 -0800 | [diff] [blame] | 6620 | size = btrfs_file_extent_inline_len(leaf, path->slots[0], item); |
Yan Zheng | 9036c10 | 2008-10-30 14:19:41 -0400 | [diff] [blame] | 6621 | extent_offset = page_offset(page) + pg_offset - extent_start; |
Chris Mason | 70dec80 | 2008-01-29 09:59:12 -0500 | [diff] [blame] | 6622 | copy_size = min_t(u64, PAGE_CACHE_SIZE - pg_offset, |
Yan | 689f934 | 2007-10-29 11:41:07 -0400 | [diff] [blame] | 6623 | size - extent_offset); |
Chris Mason | 3326d1b | 2007-10-15 16:18:25 -0400 | [diff] [blame] | 6624 | em->start = extent_start + extent_offset; |
Qu Wenruo | fda2832 | 2013-02-26 08:10:22 +0000 | [diff] [blame] | 6625 | em->len = ALIGN(copy_size, root->sectorsize); |
Josef Bacik | b493968 | 2012-12-03 10:31:19 -0500 | [diff] [blame] | 6626 | em->orig_block_len = em->len; |
Josef Bacik | 70c8a91 | 2012-10-11 16:54:30 -0400 | [diff] [blame] | 6627 | em->orig_start = em->start; |
Yan | 689f934 | 2007-10-29 11:41:07 -0400 | [diff] [blame] | 6628 | ptr = btrfs_file_extent_inline_start(item) + extent_offset; |
Chris Mason | 179e29e | 2007-11-01 11:28:41 -0400 | [diff] [blame] | 6629 | if (create == 0 && !PageUptodate(page)) { |
Li Zefan | 261507a0 | 2010-12-17 14:21:50 +0800 | [diff] [blame] | 6630 | if (btrfs_file_extent_compression(leaf, item) != |
| 6631 | BTRFS_COMPRESS_NONE) { |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 6632 | ret = uncompress_inline(path, inode, page, |
| 6633 | pg_offset, |
| 6634 | extent_offset, item); |
Zach Brown | 166ae5a | 2014-05-09 17:15:10 -0400 | [diff] [blame] | 6635 | if (ret) { |
| 6636 | err = ret; |
| 6637 | goto out; |
| 6638 | } |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 6639 | } else { |
| 6640 | map = kmap(page); |
| 6641 | read_extent_buffer(leaf, map + pg_offset, ptr, |
| 6642 | copy_size); |
Chris Mason | 93c82d5 | 2009-09-11 12:36:29 -0400 | [diff] [blame] | 6643 | if (pg_offset + copy_size < PAGE_CACHE_SIZE) { |
| 6644 | memset(map + pg_offset + copy_size, 0, |
| 6645 | PAGE_CACHE_SIZE - pg_offset - |
| 6646 | copy_size); |
| 6647 | } |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 6648 | kunmap(page); |
| 6649 | } |
Chris Mason | 179e29e | 2007-11-01 11:28:41 -0400 | [diff] [blame] | 6650 | flush_dcache_page(page); |
| 6651 | } else if (create && PageUptodate(page)) { |
Jan Schmidt | 6bf7e08 | 2011-12-01 14:35:19 +0100 | [diff] [blame] | 6652 | BUG(); |
Chris Mason | 179e29e | 2007-11-01 11:28:41 -0400 | [diff] [blame] | 6653 | if (!trans) { |
| 6654 | kunmap(page); |
| 6655 | free_extent_map(em); |
| 6656 | em = NULL; |
Chris Mason | ff5714c | 2011-05-28 07:00:39 -0400 | [diff] [blame] | 6657 | |
David Sterba | b3b4aa7 | 2011-04-21 01:20:15 +0200 | [diff] [blame] | 6658 | btrfs_release_path(path); |
Josef Bacik | 7a7eaa4 | 2011-04-13 12:54:33 -0400 | [diff] [blame] | 6659 | trans = btrfs_join_transaction(root); |
Chris Mason | ff5714c | 2011-05-28 07:00:39 -0400 | [diff] [blame] | 6660 | |
Tsutomu Itoh | 3612b49 | 2011-01-25 02:51:38 +0000 | [diff] [blame] | 6661 | if (IS_ERR(trans)) |
| 6662 | return ERR_CAST(trans); |
Chris Mason | 179e29e | 2007-11-01 11:28:41 -0400 | [diff] [blame] | 6663 | goto again; |
| 6664 | } |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 6665 | map = kmap(page); |
Chris Mason | 70dec80 | 2008-01-29 09:59:12 -0500 | [diff] [blame] | 6666 | write_extent_buffer(leaf, map + pg_offset, ptr, |
Chris Mason | 179e29e | 2007-11-01 11:28:41 -0400 | [diff] [blame] | 6667 | copy_size); |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 6668 | kunmap(page); |
Chris Mason | 179e29e | 2007-11-01 11:28:41 -0400 | [diff] [blame] | 6669 | btrfs_mark_buffer_dirty(leaf); |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 6670 | } |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 6671 | set_extent_uptodate(io_tree, em->start, |
Arne Jansen | 507903b | 2011-04-06 10:02:20 +0000 | [diff] [blame] | 6672 | extent_map_end(em) - 1, NULL, GFP_NOFS); |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 6673 | goto insert; |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 6674 | } |
| 6675 | not_found: |
| 6676 | em->start = start; |
Josef Bacik | 70c8a91 | 2012-10-11 16:54:30 -0400 | [diff] [blame] | 6677 | em->orig_start = start; |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 6678 | em->len = len; |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 6679 | not_found_em: |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 6680 | em->block_start = EXTENT_MAP_HOLE; |
Yan Zheng | 9036c10 | 2008-10-30 14:19:41 -0400 | [diff] [blame] | 6681 | set_bit(EXTENT_FLAG_VACANCY, &em->flags); |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 6682 | insert: |
David Sterba | b3b4aa7 | 2011-04-21 01:20:15 +0200 | [diff] [blame] | 6683 | btrfs_release_path(path); |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 6684 | if (em->start > start || extent_map_end(em) <= start) { |
Simon Kirby | c2cf52e | 2013-03-19 22:41:23 +0000 | [diff] [blame] | 6685 | 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] | 6686 | em->start, em->len, start, len); |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 6687 | err = -EIO; |
| 6688 | goto out; |
| 6689 | } |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 6690 | |
| 6691 | err = 0; |
Chris Mason | 890871b | 2009-09-02 16:24:52 -0400 | [diff] [blame] | 6692 | write_lock(&em_tree->lock); |
Josef Bacik | 09a2a8f9 | 2013-04-05 16:51:15 -0400 | [diff] [blame] | 6693 | ret = add_extent_mapping(em_tree, em, 0); |
Chris Mason | 3b95151 | 2008-04-17 11:29:12 -0400 | [diff] [blame] | 6694 | /* it is possible that someone inserted the extent into the tree |
| 6695 | * while we had the lock dropped. It is also possible that |
| 6696 | * an overlapping map exists in the tree |
| 6697 | */ |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 6698 | if (ret == -EEXIST) { |
Chris Mason | 3b95151 | 2008-04-17 11:29:12 -0400 | [diff] [blame] | 6699 | struct extent_map *existing; |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 6700 | |
| 6701 | ret = 0; |
| 6702 | |
Qu Wenruo | e6c4efd | 2014-09-17 11:53:35 +0800 | [diff] [blame] | 6703 | existing = search_extent_mapping(em_tree, start, len); |
| 6704 | /* |
| 6705 | * existing will always be non-NULL, since there must be |
| 6706 | * extent causing the -EEXIST. |
| 6707 | */ |
| 6708 | if (start >= extent_map_end(existing) || |
Qu Wenruo | 32be3a1 | 2014-09-22 09:13:03 +0800 | [diff] [blame] | 6709 | start <= existing->start) { |
Qu Wenruo | e6c4efd | 2014-09-17 11:53:35 +0800 | [diff] [blame] | 6710 | /* |
| 6711 | * The existing extent map is the one nearest to |
| 6712 | * the [start, start + len) range which overlaps |
| 6713 | */ |
| 6714 | err = merge_extent_mapping(em_tree, existing, |
| 6715 | em, start); |
Chris Mason | e1c4b74 | 2008-04-22 13:26:46 -0400 | [diff] [blame] | 6716 | free_extent_map(existing); |
Qu Wenruo | e6c4efd | 2014-09-17 11:53:35 +0800 | [diff] [blame] | 6717 | if (err) { |
Chris Mason | 3b95151 | 2008-04-17 11:29:12 -0400 | [diff] [blame] | 6718 | free_extent_map(em); |
| 6719 | em = NULL; |
| 6720 | } |
| 6721 | } else { |
| 6722 | free_extent_map(em); |
| 6723 | em = existing; |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 6724 | err = 0; |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 6725 | } |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 6726 | } |
Chris Mason | 890871b | 2009-09-02 16:24:52 -0400 | [diff] [blame] | 6727 | write_unlock(&em_tree->lock); |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 6728 | out: |
liubo | 1abe9b8 | 2011-03-24 11:18:59 +0000 | [diff] [blame] | 6729 | |
Steven Rostedt | 4cd8587c | 2013-11-14 22:57:29 -0500 | [diff] [blame] | 6730 | trace_btrfs_get_extent(root, em); |
liubo | 1abe9b8 | 2011-03-24 11:18:59 +0000 | [diff] [blame] | 6731 | |
Chris Mason | f421950 | 2008-07-22 11:18:09 -0400 | [diff] [blame] | 6732 | if (path) |
| 6733 | btrfs_free_path(path); |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 6734 | if (trans) { |
| 6735 | ret = btrfs_end_transaction(trans, root); |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 6736 | if (!err) |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 6737 | err = ret; |
| 6738 | } |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 6739 | if (err) { |
| 6740 | free_extent_map(em); |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 6741 | return ERR_PTR(err); |
| 6742 | } |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 6743 | BUG_ON(!em); /* Error is always set */ |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 6744 | return em; |
| 6745 | } |
| 6746 | |
Chris Mason | ec29ed5 | 2011-02-23 16:23:20 -0500 | [diff] [blame] | 6747 | struct extent_map *btrfs_get_extent_fiemap(struct inode *inode, struct page *page, |
| 6748 | size_t pg_offset, u64 start, u64 len, |
| 6749 | int create) |
| 6750 | { |
| 6751 | struct extent_map *em; |
| 6752 | struct extent_map *hole_em = NULL; |
| 6753 | u64 range_start = start; |
| 6754 | u64 end; |
| 6755 | u64 found; |
| 6756 | u64 found_end; |
| 6757 | int err = 0; |
| 6758 | |
| 6759 | em = btrfs_get_extent(inode, page, pg_offset, start, len, create); |
| 6760 | if (IS_ERR(em)) |
| 6761 | return em; |
| 6762 | if (em) { |
| 6763 | /* |
Liu Bo | f9e4fb5 | 2013-01-07 10:10:12 +0000 | [diff] [blame] | 6764 | * if our em maps to |
| 6765 | * - a hole or |
| 6766 | * - a pre-alloc extent, |
| 6767 | * there might actually be delalloc bytes behind it. |
Chris Mason | ec29ed5 | 2011-02-23 16:23:20 -0500 | [diff] [blame] | 6768 | */ |
Liu Bo | f9e4fb5 | 2013-01-07 10:10:12 +0000 | [diff] [blame] | 6769 | if (em->block_start != EXTENT_MAP_HOLE && |
| 6770 | !test_bit(EXTENT_FLAG_PREALLOC, &em->flags)) |
Chris Mason | ec29ed5 | 2011-02-23 16:23:20 -0500 | [diff] [blame] | 6771 | return em; |
| 6772 | else |
| 6773 | hole_em = em; |
| 6774 | } |
| 6775 | |
| 6776 | /* check to see if we've wrapped (len == -1 or similar) */ |
| 6777 | end = start + len; |
| 6778 | if (end < start) |
| 6779 | end = (u64)-1; |
| 6780 | else |
| 6781 | end -= 1; |
| 6782 | |
| 6783 | em = NULL; |
| 6784 | |
| 6785 | /* ok, we didn't find anything, lets look for delalloc */ |
| 6786 | found = count_range_bits(&BTRFS_I(inode)->io_tree, &range_start, |
| 6787 | end, len, EXTENT_DELALLOC, 1); |
| 6788 | found_end = range_start + found; |
| 6789 | if (found_end < range_start) |
| 6790 | found_end = (u64)-1; |
| 6791 | |
| 6792 | /* |
| 6793 | * we didn't find anything useful, return |
| 6794 | * the original results from get_extent() |
| 6795 | */ |
| 6796 | if (range_start > end || found_end <= start) { |
| 6797 | em = hole_em; |
| 6798 | hole_em = NULL; |
| 6799 | goto out; |
| 6800 | } |
| 6801 | |
| 6802 | /* adjust the range_start to make sure it doesn't |
| 6803 | * go backwards from the start they passed in |
| 6804 | */ |
Dulshani Gunawardhana | 6787125 | 2013-10-31 10:33:04 +0530 | [diff] [blame] | 6805 | range_start = max(start, range_start); |
Chris Mason | ec29ed5 | 2011-02-23 16:23:20 -0500 | [diff] [blame] | 6806 | found = found_end - range_start; |
| 6807 | |
| 6808 | if (found > 0) { |
| 6809 | u64 hole_start = start; |
| 6810 | u64 hole_len = len; |
| 6811 | |
David Sterba | 172ddd6 | 2011-04-21 00:48:27 +0200 | [diff] [blame] | 6812 | em = alloc_extent_map(); |
Chris Mason | ec29ed5 | 2011-02-23 16:23:20 -0500 | [diff] [blame] | 6813 | if (!em) { |
| 6814 | err = -ENOMEM; |
| 6815 | goto out; |
| 6816 | } |
| 6817 | /* |
| 6818 | * when btrfs_get_extent can't find anything it |
| 6819 | * returns one huge hole |
| 6820 | * |
| 6821 | * make sure what it found really fits our range, and |
| 6822 | * adjust to make sure it is based on the start from |
| 6823 | * the caller |
| 6824 | */ |
| 6825 | if (hole_em) { |
| 6826 | u64 calc_end = extent_map_end(hole_em); |
| 6827 | |
| 6828 | if (calc_end <= start || (hole_em->start > end)) { |
| 6829 | free_extent_map(hole_em); |
| 6830 | hole_em = NULL; |
| 6831 | } else { |
| 6832 | hole_start = max(hole_em->start, start); |
| 6833 | hole_len = calc_end - hole_start; |
| 6834 | } |
| 6835 | } |
| 6836 | em->bdev = NULL; |
| 6837 | if (hole_em && range_start > hole_start) { |
| 6838 | /* our hole starts before our delalloc, so we |
| 6839 | * have to return just the parts of the hole |
| 6840 | * that go until the delalloc starts |
| 6841 | */ |
| 6842 | em->len = min(hole_len, |
| 6843 | range_start - hole_start); |
| 6844 | em->start = hole_start; |
| 6845 | em->orig_start = hole_start; |
| 6846 | /* |
| 6847 | * don't adjust block start at all, |
| 6848 | * it is fixed at EXTENT_MAP_HOLE |
| 6849 | */ |
| 6850 | em->block_start = hole_em->block_start; |
| 6851 | em->block_len = hole_len; |
Liu Bo | f9e4fb5 | 2013-01-07 10:10:12 +0000 | [diff] [blame] | 6852 | if (test_bit(EXTENT_FLAG_PREALLOC, &hole_em->flags)) |
| 6853 | set_bit(EXTENT_FLAG_PREALLOC, &em->flags); |
Chris Mason | ec29ed5 | 2011-02-23 16:23:20 -0500 | [diff] [blame] | 6854 | } else { |
| 6855 | em->start = range_start; |
| 6856 | em->len = found; |
| 6857 | em->orig_start = range_start; |
| 6858 | em->block_start = EXTENT_MAP_DELALLOC; |
| 6859 | em->block_len = found; |
| 6860 | } |
| 6861 | } else if (hole_em) { |
| 6862 | return hole_em; |
| 6863 | } |
| 6864 | out: |
| 6865 | |
| 6866 | free_extent_map(hole_em); |
| 6867 | if (err) { |
| 6868 | free_extent_map(em); |
| 6869 | return ERR_PTR(err); |
| 6870 | } |
| 6871 | return em; |
| 6872 | } |
| 6873 | |
Josef Bacik | 4b46fce | 2010-05-23 11:00:55 -0400 | [diff] [blame] | 6874 | static struct extent_map *btrfs_new_extent_direct(struct inode *inode, |
| 6875 | u64 start, u64 len) |
| 6876 | { |
| 6877 | struct btrfs_root *root = BTRFS_I(inode)->root; |
Josef Bacik | 70c8a91 | 2012-10-11 16:54:30 -0400 | [diff] [blame] | 6878 | struct extent_map *em; |
Josef Bacik | 4b46fce | 2010-05-23 11:00:55 -0400 | [diff] [blame] | 6879 | struct btrfs_key ins; |
| 6880 | u64 alloc_hint; |
| 6881 | int ret; |
Josef Bacik | 4b46fce | 2010-05-23 11:00:55 -0400 | [diff] [blame] | 6882 | |
Josef Bacik | 4b46fce | 2010-05-23 11:00:55 -0400 | [diff] [blame] | 6883 | alloc_hint = get_extent_allocation_hint(inode, start, len); |
Josef Bacik | 0036158 | 2013-08-14 14:02:47 -0400 | [diff] [blame] | 6884 | ret = btrfs_reserve_extent(root, len, root->sectorsize, 0, |
Miao Xie | e570fd2 | 2014-06-19 10:42:50 +0800 | [diff] [blame] | 6885 | alloc_hint, &ins, 1, 1); |
Josef Bacik | 0036158 | 2013-08-14 14:02:47 -0400 | [diff] [blame] | 6886 | if (ret) |
| 6887 | return ERR_PTR(ret); |
Josef Bacik | 4b46fce | 2010-05-23 11:00:55 -0400 | [diff] [blame] | 6888 | |
Josef Bacik | 70c8a91 | 2012-10-11 16:54:30 -0400 | [diff] [blame] | 6889 | em = create_pinned_em(inode, start, ins.offset, start, ins.objectid, |
Josef Bacik | cc95bef | 2013-04-04 14:31:27 -0400 | [diff] [blame] | 6890 | ins.offset, ins.offset, ins.offset, 0); |
Josef Bacik | 0036158 | 2013-08-14 14:02:47 -0400 | [diff] [blame] | 6891 | if (IS_ERR(em)) { |
Miao Xie | e570fd2 | 2014-06-19 10:42:50 +0800 | [diff] [blame] | 6892 | btrfs_free_reserved_extent(root, ins.objectid, ins.offset, 1); |
Josef Bacik | 0036158 | 2013-08-14 14:02:47 -0400 | [diff] [blame] | 6893 | return em; |
| 6894 | } |
Josef Bacik | 4b46fce | 2010-05-23 11:00:55 -0400 | [diff] [blame] | 6895 | |
| 6896 | ret = btrfs_add_ordered_extent_dio(inode, start, ins.objectid, |
| 6897 | ins.offset, ins.offset, 0); |
| 6898 | if (ret) { |
Miao Xie | e570fd2 | 2014-06-19 10:42:50 +0800 | [diff] [blame] | 6899 | btrfs_free_reserved_extent(root, ins.objectid, ins.offset, 1); |
Josef Bacik | 0036158 | 2013-08-14 14:02:47 -0400 | [diff] [blame] | 6900 | free_extent_map(em); |
| 6901 | return ERR_PTR(ret); |
Josef Bacik | 4b46fce | 2010-05-23 11:00:55 -0400 | [diff] [blame] | 6902 | } |
Josef Bacik | 0036158 | 2013-08-14 14:02:47 -0400 | [diff] [blame] | 6903 | |
Josef Bacik | 4b46fce | 2010-05-23 11:00:55 -0400 | [diff] [blame] | 6904 | return em; |
| 6905 | } |
| 6906 | |
Chris Mason | 46bfbb5 | 2010-05-26 11:04:10 -0400 | [diff] [blame] | 6907 | /* |
| 6908 | * returns 1 when the nocow is safe, < 1 on error, 0 if the |
| 6909 | * block must be cow'd |
| 6910 | */ |
Josef Bacik | 0036158 | 2013-08-14 14:02:47 -0400 | [diff] [blame] | 6911 | noinline int can_nocow_extent(struct inode *inode, u64 offset, u64 *len, |
Josef Bacik | 7ee9e44 | 2013-06-21 16:37:03 -0400 | [diff] [blame] | 6912 | u64 *orig_start, u64 *orig_block_len, |
| 6913 | u64 *ram_bytes) |
Chris Mason | 46bfbb5 | 2010-05-26 11:04:10 -0400 | [diff] [blame] | 6914 | { |
Josef Bacik | 0036158 | 2013-08-14 14:02:47 -0400 | [diff] [blame] | 6915 | struct btrfs_trans_handle *trans; |
Chris Mason | 46bfbb5 | 2010-05-26 11:04:10 -0400 | [diff] [blame] | 6916 | struct btrfs_path *path; |
| 6917 | int ret; |
| 6918 | struct extent_buffer *leaf; |
| 6919 | struct btrfs_root *root = BTRFS_I(inode)->root; |
Miao Xie | 7b2b708 | 2014-02-27 13:58:05 +0800 | [diff] [blame] | 6920 | struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree; |
Chris Mason | 46bfbb5 | 2010-05-26 11:04:10 -0400 | [diff] [blame] | 6921 | struct btrfs_file_extent_item *fi; |
| 6922 | struct btrfs_key key; |
| 6923 | u64 disk_bytenr; |
| 6924 | u64 backref_offset; |
| 6925 | u64 extent_end; |
| 6926 | u64 num_bytes; |
| 6927 | int slot; |
| 6928 | int found_type; |
Josef Bacik | 7ee9e44 | 2013-06-21 16:37:03 -0400 | [diff] [blame] | 6929 | bool nocow = (BTRFS_I(inode)->flags & BTRFS_INODE_NODATACOW); |
Miao Xie | e77751a | 2013-12-27 21:11:50 +0800 | [diff] [blame] | 6930 | |
Chris Mason | 46bfbb5 | 2010-05-26 11:04:10 -0400 | [diff] [blame] | 6931 | path = btrfs_alloc_path(); |
| 6932 | if (!path) |
| 6933 | return -ENOMEM; |
| 6934 | |
Josef Bacik | 0036158 | 2013-08-14 14:02:47 -0400 | [diff] [blame] | 6935 | ret = btrfs_lookup_file_extent(NULL, root, path, btrfs_ino(inode), |
Chris Mason | 46bfbb5 | 2010-05-26 11:04:10 -0400 | [diff] [blame] | 6936 | offset, 0); |
| 6937 | if (ret < 0) |
| 6938 | goto out; |
| 6939 | |
| 6940 | slot = path->slots[0]; |
| 6941 | if (ret == 1) { |
| 6942 | if (slot == 0) { |
| 6943 | /* can't find the item, must cow */ |
| 6944 | ret = 0; |
| 6945 | goto out; |
| 6946 | } |
| 6947 | slot--; |
| 6948 | } |
| 6949 | ret = 0; |
| 6950 | leaf = path->nodes[0]; |
| 6951 | btrfs_item_key_to_cpu(leaf, &key, slot); |
Li Zefan | 33345d01 | 2011-04-20 10:31:50 +0800 | [diff] [blame] | 6952 | if (key.objectid != btrfs_ino(inode) || |
Chris Mason | 46bfbb5 | 2010-05-26 11:04:10 -0400 | [diff] [blame] | 6953 | key.type != BTRFS_EXTENT_DATA_KEY) { |
| 6954 | /* not our file or wrong item type, must cow */ |
| 6955 | goto out; |
| 6956 | } |
| 6957 | |
| 6958 | if (key.offset > offset) { |
| 6959 | /* Wrong offset, must cow */ |
| 6960 | goto out; |
| 6961 | } |
| 6962 | |
| 6963 | fi = btrfs_item_ptr(leaf, slot, struct btrfs_file_extent_item); |
| 6964 | found_type = btrfs_file_extent_type(leaf, fi); |
| 6965 | if (found_type != BTRFS_FILE_EXTENT_REG && |
| 6966 | found_type != BTRFS_FILE_EXTENT_PREALLOC) { |
| 6967 | /* not a regular extent, must cow */ |
| 6968 | goto out; |
| 6969 | } |
Josef Bacik | 7ee9e44 | 2013-06-21 16:37:03 -0400 | [diff] [blame] | 6970 | |
| 6971 | if (!nocow && found_type == BTRFS_FILE_EXTENT_REG) |
| 6972 | goto out; |
| 6973 | |
Miao Xie | e77751a | 2013-12-27 21:11:50 +0800 | [diff] [blame] | 6974 | extent_end = key.offset + btrfs_file_extent_num_bytes(leaf, fi); |
| 6975 | if (extent_end <= offset) |
| 6976 | goto out; |
| 6977 | |
Chris Mason | 46bfbb5 | 2010-05-26 11:04:10 -0400 | [diff] [blame] | 6978 | disk_bytenr = btrfs_file_extent_disk_bytenr(leaf, fi); |
Josef Bacik | 7ee9e44 | 2013-06-21 16:37:03 -0400 | [diff] [blame] | 6979 | if (disk_bytenr == 0) |
| 6980 | goto out; |
| 6981 | |
| 6982 | if (btrfs_file_extent_compression(leaf, fi) || |
| 6983 | btrfs_file_extent_encryption(leaf, fi) || |
| 6984 | btrfs_file_extent_other_encoding(leaf, fi)) |
| 6985 | goto out; |
| 6986 | |
Chris Mason | 46bfbb5 | 2010-05-26 11:04:10 -0400 | [diff] [blame] | 6987 | backref_offset = btrfs_file_extent_offset(leaf, fi); |
| 6988 | |
Josef Bacik | 7ee9e44 | 2013-06-21 16:37:03 -0400 | [diff] [blame] | 6989 | if (orig_start) { |
| 6990 | *orig_start = key.offset - backref_offset; |
| 6991 | *orig_block_len = btrfs_file_extent_disk_num_bytes(leaf, fi); |
| 6992 | *ram_bytes = btrfs_file_extent_ram_bytes(leaf, fi); |
| 6993 | } |
Josef Bacik | eb384b5 | 2013-04-24 16:32:55 -0400 | [diff] [blame] | 6994 | |
Chris Mason | 46bfbb5 | 2010-05-26 11:04:10 -0400 | [diff] [blame] | 6995 | if (btrfs_extent_readonly(root, disk_bytenr)) |
| 6996 | goto out; |
Miao Xie | 7b2b708 | 2014-02-27 13:58:05 +0800 | [diff] [blame] | 6997 | |
| 6998 | num_bytes = min(offset + *len, extent_end) - offset; |
| 6999 | if (!nocow && found_type == BTRFS_FILE_EXTENT_PREALLOC) { |
| 7000 | u64 range_end; |
| 7001 | |
| 7002 | range_end = round_up(offset + num_bytes, root->sectorsize) - 1; |
| 7003 | ret = test_range_bit(io_tree, offset, range_end, |
| 7004 | EXTENT_DELALLOC, 0, NULL); |
| 7005 | if (ret) { |
| 7006 | ret = -EAGAIN; |
| 7007 | goto out; |
| 7008 | } |
| 7009 | } |
| 7010 | |
Josef Bacik | 1bda19e | 2013-10-18 12:10:36 -0400 | [diff] [blame] | 7011 | btrfs_release_path(path); |
Chris Mason | 46bfbb5 | 2010-05-26 11:04:10 -0400 | [diff] [blame] | 7012 | |
| 7013 | /* |
| 7014 | * look for other files referencing this extent, if we |
| 7015 | * find any we must cow |
| 7016 | */ |
Josef Bacik | 0036158 | 2013-08-14 14:02:47 -0400 | [diff] [blame] | 7017 | trans = btrfs_join_transaction(root); |
| 7018 | if (IS_ERR(trans)) { |
| 7019 | ret = 0; |
Chris Mason | 46bfbb5 | 2010-05-26 11:04:10 -0400 | [diff] [blame] | 7020 | goto out; |
Josef Bacik | 0036158 | 2013-08-14 14:02:47 -0400 | [diff] [blame] | 7021 | } |
| 7022 | |
| 7023 | ret = btrfs_cross_ref_exist(trans, root, btrfs_ino(inode), |
| 7024 | key.offset - backref_offset, disk_bytenr); |
| 7025 | btrfs_end_transaction(trans, root); |
| 7026 | if (ret) { |
| 7027 | ret = 0; |
| 7028 | goto out; |
| 7029 | } |
Chris Mason | 46bfbb5 | 2010-05-26 11:04:10 -0400 | [diff] [blame] | 7030 | |
| 7031 | /* |
| 7032 | * adjust disk_bytenr and num_bytes to cover just the bytes |
| 7033 | * in this extent we are about to write. If there |
| 7034 | * are any csums in that range we have to cow in order |
| 7035 | * to keep the csums correct |
| 7036 | */ |
| 7037 | disk_bytenr += backref_offset; |
| 7038 | disk_bytenr += offset - key.offset; |
Chris Mason | 46bfbb5 | 2010-05-26 11:04:10 -0400 | [diff] [blame] | 7039 | if (csum_exist_in_range(root, disk_bytenr, num_bytes)) |
| 7040 | goto out; |
| 7041 | /* |
| 7042 | * all of the above have passed, it is safe to overwrite this extent |
| 7043 | * without cow |
| 7044 | */ |
Josef Bacik | eb384b5 | 2013-04-24 16:32:55 -0400 | [diff] [blame] | 7045 | *len = num_bytes; |
Chris Mason | 46bfbb5 | 2010-05-26 11:04:10 -0400 | [diff] [blame] | 7046 | ret = 1; |
| 7047 | out: |
| 7048 | btrfs_free_path(path); |
| 7049 | return ret; |
| 7050 | } |
| 7051 | |
Alex Gartrell | fc4adbf | 2014-05-20 13:07:56 -0700 | [diff] [blame] | 7052 | bool btrfs_page_exists_in_range(struct inode *inode, loff_t start, loff_t end) |
| 7053 | { |
| 7054 | struct radix_tree_root *root = &inode->i_mapping->page_tree; |
| 7055 | int found = false; |
| 7056 | void **pagep = NULL; |
| 7057 | struct page *page = NULL; |
| 7058 | int start_idx; |
| 7059 | int end_idx; |
| 7060 | |
| 7061 | start_idx = start >> PAGE_CACHE_SHIFT; |
| 7062 | |
| 7063 | /* |
| 7064 | * end is the last byte in the last page. end == start is legal |
| 7065 | */ |
| 7066 | end_idx = end >> PAGE_CACHE_SHIFT; |
| 7067 | |
| 7068 | rcu_read_lock(); |
| 7069 | |
| 7070 | /* Most of the code in this while loop is lifted from |
| 7071 | * find_get_page. It's been modified to begin searching from a |
| 7072 | * page and return just the first page found in that range. If the |
| 7073 | * found idx is less than or equal to the end idx then we know that |
| 7074 | * a page exists. If no pages are found or if those pages are |
| 7075 | * outside of the range then we're fine (yay!) */ |
| 7076 | while (page == NULL && |
| 7077 | radix_tree_gang_lookup_slot(root, &pagep, NULL, start_idx, 1)) { |
| 7078 | page = radix_tree_deref_slot(pagep); |
| 7079 | if (unlikely(!page)) |
| 7080 | break; |
| 7081 | |
| 7082 | if (radix_tree_exception(page)) { |
Filipe Manana | 809f901 | 2014-06-05 13:22:25 +0100 | [diff] [blame] | 7083 | if (radix_tree_deref_retry(page)) { |
| 7084 | page = NULL; |
Alex Gartrell | fc4adbf | 2014-05-20 13:07:56 -0700 | [diff] [blame] | 7085 | continue; |
Filipe Manana | 809f901 | 2014-06-05 13:22:25 +0100 | [diff] [blame] | 7086 | } |
Alex Gartrell | fc4adbf | 2014-05-20 13:07:56 -0700 | [diff] [blame] | 7087 | /* |
| 7088 | * Otherwise, shmem/tmpfs must be storing a swap entry |
| 7089 | * here as an exceptional entry: so return it without |
| 7090 | * attempting to raise page count. |
| 7091 | */ |
Filipe Manana | 6fdef6d | 2014-06-05 13:22:26 +0100 | [diff] [blame] | 7092 | page = NULL; |
Alex Gartrell | fc4adbf | 2014-05-20 13:07:56 -0700 | [diff] [blame] | 7093 | break; /* TODO: Is this relevant for this use case? */ |
| 7094 | } |
| 7095 | |
Filipe Manana | 9140515 | 2014-06-05 13:22:24 +0100 | [diff] [blame] | 7096 | if (!page_cache_get_speculative(page)) { |
| 7097 | page = NULL; |
Alex Gartrell | fc4adbf | 2014-05-20 13:07:56 -0700 | [diff] [blame] | 7098 | continue; |
Filipe Manana | 9140515 | 2014-06-05 13:22:24 +0100 | [diff] [blame] | 7099 | } |
Alex Gartrell | fc4adbf | 2014-05-20 13:07:56 -0700 | [diff] [blame] | 7100 | |
| 7101 | /* |
| 7102 | * Has the page moved? |
| 7103 | * This is part of the lockless pagecache protocol. See |
| 7104 | * include/linux/pagemap.h for details. |
| 7105 | */ |
| 7106 | if (unlikely(page != *pagep)) { |
| 7107 | page_cache_release(page); |
| 7108 | page = NULL; |
| 7109 | } |
| 7110 | } |
| 7111 | |
| 7112 | if (page) { |
| 7113 | if (page->index <= end_idx) |
| 7114 | found = true; |
| 7115 | page_cache_release(page); |
| 7116 | } |
| 7117 | |
| 7118 | rcu_read_unlock(); |
| 7119 | return found; |
| 7120 | } |
| 7121 | |
Josef Bacik | eb838e7 | 2012-07-31 16:28:48 -0400 | [diff] [blame] | 7122 | static int lock_extent_direct(struct inode *inode, u64 lockstart, u64 lockend, |
| 7123 | struct extent_state **cached_state, int writing) |
| 7124 | { |
| 7125 | struct btrfs_ordered_extent *ordered; |
| 7126 | int ret = 0; |
| 7127 | |
| 7128 | while (1) { |
| 7129 | lock_extent_bits(&BTRFS_I(inode)->io_tree, lockstart, lockend, |
| 7130 | 0, cached_state); |
| 7131 | /* |
| 7132 | * We're concerned with the entire range that we're going to be |
| 7133 | * doing DIO to, so we need to make sure theres no ordered |
| 7134 | * extents in this range. |
| 7135 | */ |
| 7136 | ordered = btrfs_lookup_ordered_range(inode, lockstart, |
| 7137 | lockend - lockstart + 1); |
| 7138 | |
| 7139 | /* |
| 7140 | * We need to make sure there are no buffered pages in this |
| 7141 | * range either, we could have raced between the invalidate in |
| 7142 | * generic_file_direct_write and locking the extent. The |
| 7143 | * invalidate needs to happen so that reads after a write do not |
| 7144 | * get stale data. |
| 7145 | */ |
Alex Gartrell | fc4adbf | 2014-05-20 13:07:56 -0700 | [diff] [blame] | 7146 | if (!ordered && |
| 7147 | (!writing || |
| 7148 | !btrfs_page_exists_in_range(inode, lockstart, lockend))) |
Josef Bacik | eb838e7 | 2012-07-31 16:28:48 -0400 | [diff] [blame] | 7149 | break; |
| 7150 | |
| 7151 | unlock_extent_cached(&BTRFS_I(inode)->io_tree, lockstart, lockend, |
| 7152 | cached_state, GFP_NOFS); |
| 7153 | |
| 7154 | if (ordered) { |
| 7155 | btrfs_start_ordered_extent(inode, ordered, 1); |
| 7156 | btrfs_put_ordered_extent(ordered); |
| 7157 | } else { |
| 7158 | /* Screw you mmap */ |
Filipe Manana | 728404d | 2014-10-10 09:43:11 +0100 | [diff] [blame] | 7159 | ret = btrfs_fdatawrite_range(inode, lockstart, lockend); |
Filipe Manana | 075bdbd | 2014-10-09 21:18:55 +0100 | [diff] [blame] | 7160 | if (ret) |
| 7161 | break; |
| 7162 | ret = filemap_fdatawait_range(inode->i_mapping, |
| 7163 | lockstart, |
| 7164 | lockend); |
Josef Bacik | eb838e7 | 2012-07-31 16:28:48 -0400 | [diff] [blame] | 7165 | if (ret) |
| 7166 | break; |
| 7167 | |
| 7168 | /* |
| 7169 | * If we found a page that couldn't be invalidated just |
| 7170 | * fall back to buffered. |
| 7171 | */ |
| 7172 | ret = invalidate_inode_pages2_range(inode->i_mapping, |
| 7173 | lockstart >> PAGE_CACHE_SHIFT, |
| 7174 | lockend >> PAGE_CACHE_SHIFT); |
| 7175 | if (ret) |
| 7176 | break; |
| 7177 | } |
| 7178 | |
| 7179 | cond_resched(); |
| 7180 | } |
| 7181 | |
| 7182 | return ret; |
| 7183 | } |
| 7184 | |
Josef Bacik | 69ffb54 | 2012-09-11 15:40:07 -0400 | [diff] [blame] | 7185 | static struct extent_map *create_pinned_em(struct inode *inode, u64 start, |
| 7186 | u64 len, u64 orig_start, |
| 7187 | u64 block_start, u64 block_len, |
Josef Bacik | cc95bef | 2013-04-04 14:31:27 -0400 | [diff] [blame] | 7188 | u64 orig_block_len, u64 ram_bytes, |
| 7189 | int type) |
Josef Bacik | 69ffb54 | 2012-09-11 15:40:07 -0400 | [diff] [blame] | 7190 | { |
| 7191 | struct extent_map_tree *em_tree; |
| 7192 | struct extent_map *em; |
| 7193 | struct btrfs_root *root = BTRFS_I(inode)->root; |
| 7194 | int ret; |
| 7195 | |
| 7196 | em_tree = &BTRFS_I(inode)->extent_tree; |
| 7197 | em = alloc_extent_map(); |
| 7198 | if (!em) |
| 7199 | return ERR_PTR(-ENOMEM); |
| 7200 | |
| 7201 | em->start = start; |
| 7202 | em->orig_start = orig_start; |
Josef Bacik | 2ab28f3 | 2012-10-12 15:27:49 -0400 | [diff] [blame] | 7203 | em->mod_start = start; |
| 7204 | em->mod_len = len; |
Josef Bacik | 69ffb54 | 2012-09-11 15:40:07 -0400 | [diff] [blame] | 7205 | em->len = len; |
| 7206 | em->block_len = block_len; |
| 7207 | em->block_start = block_start; |
| 7208 | em->bdev = root->fs_info->fs_devices->latest_bdev; |
Josef Bacik | b493968 | 2012-12-03 10:31:19 -0500 | [diff] [blame] | 7209 | em->orig_block_len = orig_block_len; |
Josef Bacik | cc95bef | 2013-04-04 14:31:27 -0400 | [diff] [blame] | 7210 | em->ram_bytes = ram_bytes; |
Josef Bacik | 70c8a91 | 2012-10-11 16:54:30 -0400 | [diff] [blame] | 7211 | em->generation = -1; |
Josef Bacik | 69ffb54 | 2012-09-11 15:40:07 -0400 | [diff] [blame] | 7212 | set_bit(EXTENT_FLAG_PINNED, &em->flags); |
| 7213 | if (type == BTRFS_ORDERED_PREALLOC) |
Josef Bacik | b11e234 | 2012-12-03 10:58:15 -0500 | [diff] [blame] | 7214 | set_bit(EXTENT_FLAG_FILLING, &em->flags); |
Josef Bacik | 69ffb54 | 2012-09-11 15:40:07 -0400 | [diff] [blame] | 7215 | |
| 7216 | do { |
| 7217 | btrfs_drop_extent_cache(inode, em->start, |
| 7218 | em->start + em->len - 1, 0); |
| 7219 | write_lock(&em_tree->lock); |
Josef Bacik | 09a2a8f9 | 2013-04-05 16:51:15 -0400 | [diff] [blame] | 7220 | ret = add_extent_mapping(em_tree, em, 1); |
Josef Bacik | 69ffb54 | 2012-09-11 15:40:07 -0400 | [diff] [blame] | 7221 | write_unlock(&em_tree->lock); |
| 7222 | } while (ret == -EEXIST); |
| 7223 | |
| 7224 | if (ret) { |
| 7225 | free_extent_map(em); |
| 7226 | return ERR_PTR(ret); |
| 7227 | } |
| 7228 | |
| 7229 | return em; |
| 7230 | } |
| 7231 | |
| 7232 | |
Josef Bacik | 4b46fce | 2010-05-23 11:00:55 -0400 | [diff] [blame] | 7233 | static int btrfs_get_blocks_direct(struct inode *inode, sector_t iblock, |
| 7234 | struct buffer_head *bh_result, int create) |
| 7235 | { |
| 7236 | struct extent_map *em; |
| 7237 | struct btrfs_root *root = BTRFS_I(inode)->root; |
Josef Bacik | eb838e7 | 2012-07-31 16:28:48 -0400 | [diff] [blame] | 7238 | struct extent_state *cached_state = NULL; |
Josef Bacik | 4b46fce | 2010-05-23 11:00:55 -0400 | [diff] [blame] | 7239 | u64 start = iblock << inode->i_blkbits; |
Josef Bacik | eb838e7 | 2012-07-31 16:28:48 -0400 | [diff] [blame] | 7240 | u64 lockstart, lockend; |
Josef Bacik | 4b46fce | 2010-05-23 11:00:55 -0400 | [diff] [blame] | 7241 | u64 len = bh_result->b_size; |
Josef Bacik | e1cbbfa | 2015-03-17 10:52:28 -0400 | [diff] [blame^] | 7242 | u64 *outstanding_extents = NULL; |
Josef Bacik | eb838e7 | 2012-07-31 16:28:48 -0400 | [diff] [blame] | 7243 | int unlock_bits = EXTENT_LOCKED; |
Miao Xie | 0934856 | 2013-02-07 10:12:07 +0000 | [diff] [blame] | 7244 | int ret = 0; |
Josef Bacik | eb838e7 | 2012-07-31 16:28:48 -0400 | [diff] [blame] | 7245 | |
Miao Xie | 172a504 | 2013-02-21 02:48:22 -0700 | [diff] [blame] | 7246 | if (create) |
Josef Bacik | 3266789 | 2015-02-11 15:08:58 -0500 | [diff] [blame] | 7247 | unlock_bits |= EXTENT_DIRTY; |
Miao Xie | 172a504 | 2013-02-21 02:48:22 -0700 | [diff] [blame] | 7248 | else |
Josef Bacik | c329861 | 2012-08-03 16:49:19 -0400 | [diff] [blame] | 7249 | len = min_t(u64, len, root->sectorsize); |
Josef Bacik | eb838e7 | 2012-07-31 16:28:48 -0400 | [diff] [blame] | 7250 | |
Josef Bacik | c329861 | 2012-08-03 16:49:19 -0400 | [diff] [blame] | 7251 | lockstart = start; |
| 7252 | lockend = start + len - 1; |
| 7253 | |
Josef Bacik | e1cbbfa | 2015-03-17 10:52:28 -0400 | [diff] [blame^] | 7254 | if (current->journal_info) { |
| 7255 | /* |
| 7256 | * Need to pull our outstanding extents and set journal_info to NULL so |
| 7257 | * that anything that needs to check if there's a transction doesn't get |
| 7258 | * confused. |
| 7259 | */ |
| 7260 | outstanding_extents = current->journal_info; |
| 7261 | current->journal_info = NULL; |
| 7262 | } |
| 7263 | |
Josef Bacik | eb838e7 | 2012-07-31 16:28:48 -0400 | [diff] [blame] | 7264 | /* |
| 7265 | * If this errors out it's because we couldn't invalidate pagecache for |
| 7266 | * this range and we need to fallback to buffered. |
| 7267 | */ |
| 7268 | if (lock_extent_direct(inode, lockstart, lockend, &cached_state, create)) |
| 7269 | return -ENOTBLK; |
| 7270 | |
Josef Bacik | 4b46fce | 2010-05-23 11:00:55 -0400 | [diff] [blame] | 7271 | em = btrfs_get_extent(inode, NULL, 0, start, len, 0); |
Josef Bacik | eb838e7 | 2012-07-31 16:28:48 -0400 | [diff] [blame] | 7272 | if (IS_ERR(em)) { |
| 7273 | ret = PTR_ERR(em); |
| 7274 | goto unlock_err; |
| 7275 | } |
Josef Bacik | 4b46fce | 2010-05-23 11:00:55 -0400 | [diff] [blame] | 7276 | |
| 7277 | /* |
| 7278 | * Ok for INLINE and COMPRESSED extents we need to fallback on buffered |
| 7279 | * io. INLINE is special, and we could probably kludge it in here, but |
| 7280 | * it's still buffered so for safety lets just fall back to the generic |
| 7281 | * buffered path. |
| 7282 | * |
| 7283 | * For COMPRESSED we _have_ to read the entire extent in so we can |
| 7284 | * decompress it, so there will be buffering required no matter what we |
| 7285 | * do, so go ahead and fallback to buffered. |
| 7286 | * |
| 7287 | * We return -ENOTBLK because thats what makes DIO go ahead and go back |
| 7288 | * to buffered IO. Don't blame me, this is the price we pay for using |
| 7289 | * the generic code. |
| 7290 | */ |
| 7291 | if (test_bit(EXTENT_FLAG_COMPRESSED, &em->flags) || |
| 7292 | em->block_start == EXTENT_MAP_INLINE) { |
| 7293 | free_extent_map(em); |
Josef Bacik | eb838e7 | 2012-07-31 16:28:48 -0400 | [diff] [blame] | 7294 | ret = -ENOTBLK; |
| 7295 | goto unlock_err; |
Josef Bacik | 4b46fce | 2010-05-23 11:00:55 -0400 | [diff] [blame] | 7296 | } |
| 7297 | |
| 7298 | /* Just a good old fashioned hole, return */ |
| 7299 | if (!create && (em->block_start == EXTENT_MAP_HOLE || |
| 7300 | test_bit(EXTENT_FLAG_PREALLOC, &em->flags))) { |
| 7301 | free_extent_map(em); |
Josef Bacik | eb838e7 | 2012-07-31 16:28:48 -0400 | [diff] [blame] | 7302 | goto unlock_err; |
Josef Bacik | 4b46fce | 2010-05-23 11:00:55 -0400 | [diff] [blame] | 7303 | } |
| 7304 | |
| 7305 | /* |
| 7306 | * We don't allocate a new extent in the following cases |
| 7307 | * |
| 7308 | * 1) The inode is marked as NODATACOW. In this case we'll just use the |
| 7309 | * existing extent. |
| 7310 | * 2) The extent is marked as PREALLOC. We're good to go here and can |
| 7311 | * just use the extent. |
| 7312 | * |
| 7313 | */ |
Chris Mason | 46bfbb5 | 2010-05-26 11:04:10 -0400 | [diff] [blame] | 7314 | if (!create) { |
Josef Bacik | eb838e7 | 2012-07-31 16:28:48 -0400 | [diff] [blame] | 7315 | len = min(len, em->len - (start - em->start)); |
| 7316 | lockstart = start + len; |
| 7317 | goto unlock; |
Chris Mason | 46bfbb5 | 2010-05-26 11:04:10 -0400 | [diff] [blame] | 7318 | } |
Josef Bacik | 4b46fce | 2010-05-23 11:00:55 -0400 | [diff] [blame] | 7319 | |
| 7320 | if (test_bit(EXTENT_FLAG_PREALLOC, &em->flags) || |
| 7321 | ((BTRFS_I(inode)->flags & BTRFS_INODE_NODATACOW) && |
| 7322 | em->block_start != EXTENT_MAP_HOLE)) { |
Josef Bacik | 4b46fce | 2010-05-23 11:00:55 -0400 | [diff] [blame] | 7323 | int type; |
Josef Bacik | eb384b5 | 2013-04-24 16:32:55 -0400 | [diff] [blame] | 7324 | u64 block_start, orig_start, orig_block_len, ram_bytes; |
Josef Bacik | 4b46fce | 2010-05-23 11:00:55 -0400 | [diff] [blame] | 7325 | |
| 7326 | if (test_bit(EXTENT_FLAG_PREALLOC, &em->flags)) |
| 7327 | type = BTRFS_ORDERED_PREALLOC; |
| 7328 | else |
| 7329 | type = BTRFS_ORDERED_NOCOW; |
Chris Mason | 46bfbb5 | 2010-05-26 11:04:10 -0400 | [diff] [blame] | 7330 | len = min(len, em->len - (start - em->start)); |
Josef Bacik | 4b46fce | 2010-05-23 11:00:55 -0400 | [diff] [blame] | 7331 | block_start = em->block_start + (start - em->start); |
Chris Mason | 46bfbb5 | 2010-05-26 11:04:10 -0400 | [diff] [blame] | 7332 | |
Josef Bacik | 0036158 | 2013-08-14 14:02:47 -0400 | [diff] [blame] | 7333 | if (can_nocow_extent(inode, start, &len, &orig_start, |
Josef Bacik | 7ee9e44 | 2013-06-21 16:37:03 -0400 | [diff] [blame] | 7334 | &orig_block_len, &ram_bytes) == 1) { |
Josef Bacik | 69ffb54 | 2012-09-11 15:40:07 -0400 | [diff] [blame] | 7335 | if (type == BTRFS_ORDERED_PREALLOC) { |
| 7336 | free_extent_map(em); |
| 7337 | em = create_pinned_em(inode, start, len, |
| 7338 | orig_start, |
Josef Bacik | b493968 | 2012-12-03 10:31:19 -0500 | [diff] [blame] | 7339 | block_start, len, |
Josef Bacik | cc95bef | 2013-04-04 14:31:27 -0400 | [diff] [blame] | 7340 | orig_block_len, |
| 7341 | ram_bytes, type); |
Filipe Manana | 555e128 | 2014-07-07 12:35:21 +0100 | [diff] [blame] | 7342 | if (IS_ERR(em)) { |
| 7343 | ret = PTR_ERR(em); |
Josef Bacik | 69ffb54 | 2012-09-11 15:40:07 -0400 | [diff] [blame] | 7344 | goto unlock_err; |
Filipe Manana | 555e128 | 2014-07-07 12:35:21 +0100 | [diff] [blame] | 7345 | } |
Josef Bacik | 69ffb54 | 2012-09-11 15:40:07 -0400 | [diff] [blame] | 7346 | } |
| 7347 | |
Chris Mason | 46bfbb5 | 2010-05-26 11:04:10 -0400 | [diff] [blame] | 7348 | ret = btrfs_add_ordered_extent_dio(inode, start, |
| 7349 | block_start, len, len, type); |
Chris Mason | 46bfbb5 | 2010-05-26 11:04:10 -0400 | [diff] [blame] | 7350 | if (ret) { |
| 7351 | free_extent_map(em); |
Josef Bacik | eb838e7 | 2012-07-31 16:28:48 -0400 | [diff] [blame] | 7352 | goto unlock_err; |
Chris Mason | 46bfbb5 | 2010-05-26 11:04:10 -0400 | [diff] [blame] | 7353 | } |
| 7354 | goto unlock; |
Josef Bacik | 4b46fce | 2010-05-23 11:00:55 -0400 | [diff] [blame] | 7355 | } |
Josef Bacik | 4b46fce | 2010-05-23 11:00:55 -0400 | [diff] [blame] | 7356 | } |
Josef Bacik | 0036158 | 2013-08-14 14:02:47 -0400 | [diff] [blame] | 7357 | |
Chris Mason | 46bfbb5 | 2010-05-26 11:04:10 -0400 | [diff] [blame] | 7358 | /* |
| 7359 | * this will cow the extent, reset the len in case we changed |
| 7360 | * it above |
| 7361 | */ |
| 7362 | len = bh_result->b_size; |
Josef Bacik | 70c8a91 | 2012-10-11 16:54:30 -0400 | [diff] [blame] | 7363 | free_extent_map(em); |
| 7364 | em = btrfs_new_extent_direct(inode, start, len); |
Josef Bacik | eb838e7 | 2012-07-31 16:28:48 -0400 | [diff] [blame] | 7365 | if (IS_ERR(em)) { |
| 7366 | ret = PTR_ERR(em); |
| 7367 | goto unlock_err; |
| 7368 | } |
Chris Mason | 46bfbb5 | 2010-05-26 11:04:10 -0400 | [diff] [blame] | 7369 | len = min(len, em->len - (start - em->start)); |
| 7370 | unlock: |
Josef Bacik | 4b46fce | 2010-05-23 11:00:55 -0400 | [diff] [blame] | 7371 | bh_result->b_blocknr = (em->block_start + (start - em->start)) >> |
| 7372 | inode->i_blkbits; |
Chris Mason | 46bfbb5 | 2010-05-26 11:04:10 -0400 | [diff] [blame] | 7373 | bh_result->b_size = len; |
Josef Bacik | 4b46fce | 2010-05-23 11:00:55 -0400 | [diff] [blame] | 7374 | bh_result->b_bdev = em->bdev; |
| 7375 | set_buffer_mapped(bh_result); |
Josef Bacik | c3473e8 | 2012-06-19 10:59:00 -0400 | [diff] [blame] | 7376 | if (create) { |
| 7377 | if (!test_bit(EXTENT_FLAG_PREALLOC, &em->flags)) |
| 7378 | set_buffer_new(bh_result); |
| 7379 | |
| 7380 | /* |
| 7381 | * Need to update the i_size under the extent lock so buffered |
| 7382 | * readers will get the updated i_size when we unlock. |
| 7383 | */ |
| 7384 | if (start + len > i_size_read(inode)) |
| 7385 | i_size_write(inode, start + len); |
Miao Xie | 0934856 | 2013-02-07 10:12:07 +0000 | [diff] [blame] | 7386 | |
Josef Bacik | e1cbbfa | 2015-03-17 10:52:28 -0400 | [diff] [blame^] | 7387 | /* |
| 7388 | * If we have an outstanding_extents count still set then we're |
| 7389 | * within our reservation, otherwise we need to adjust our inode |
| 7390 | * counter appropriately. |
| 7391 | */ |
| 7392 | if (*outstanding_extents) { |
| 7393 | (*outstanding_extents)--; |
| 7394 | } else { |
Josef Bacik | 3e05bde | 2015-02-11 15:08:57 -0500 | [diff] [blame] | 7395 | spin_lock(&BTRFS_I(inode)->lock); |
| 7396 | BTRFS_I(inode)->outstanding_extents++; |
| 7397 | spin_unlock(&BTRFS_I(inode)->lock); |
| 7398 | } |
Josef Bacik | e1cbbfa | 2015-03-17 10:52:28 -0400 | [diff] [blame^] | 7399 | |
| 7400 | current->journal_info = outstanding_extents; |
Josef Bacik | 3266789 | 2015-02-11 15:08:58 -0500 | [diff] [blame] | 7401 | btrfs_free_reserved_data_space(inode, len); |
Josef Bacik | c3473e8 | 2012-06-19 10:59:00 -0400 | [diff] [blame] | 7402 | } |
Josef Bacik | 4b46fce | 2010-05-23 11:00:55 -0400 | [diff] [blame] | 7403 | |
Josef Bacik | eb838e7 | 2012-07-31 16:28:48 -0400 | [diff] [blame] | 7404 | /* |
| 7405 | * In the case of write we need to clear and unlock the entire range, |
| 7406 | * in the case of read we need to unlock only the end area that we |
| 7407 | * aren't using if there is any left over space. |
| 7408 | */ |
Liu Bo | 24c03fa | 2012-08-22 20:10:38 -0600 | [diff] [blame] | 7409 | if (lockstart < lockend) { |
Miao Xie | 0934856 | 2013-02-07 10:12:07 +0000 | [diff] [blame] | 7410 | clear_extent_bit(&BTRFS_I(inode)->io_tree, lockstart, |
| 7411 | lockend, unlock_bits, 1, 0, |
| 7412 | &cached_state, GFP_NOFS); |
Liu Bo | 24c03fa | 2012-08-22 20:10:38 -0600 | [diff] [blame] | 7413 | } else { |
Josef Bacik | eb838e7 | 2012-07-31 16:28:48 -0400 | [diff] [blame] | 7414 | free_extent_state(cached_state); |
Liu Bo | 24c03fa | 2012-08-22 20:10:38 -0600 | [diff] [blame] | 7415 | } |
Josef Bacik | eb838e7 | 2012-07-31 16:28:48 -0400 | [diff] [blame] | 7416 | |
Josef Bacik | 4b46fce | 2010-05-23 11:00:55 -0400 | [diff] [blame] | 7417 | free_extent_map(em); |
| 7418 | |
| 7419 | return 0; |
Josef Bacik | eb838e7 | 2012-07-31 16:28:48 -0400 | [diff] [blame] | 7420 | |
| 7421 | unlock_err: |
Josef Bacik | eb838e7 | 2012-07-31 16:28:48 -0400 | [diff] [blame] | 7422 | clear_extent_bit(&BTRFS_I(inode)->io_tree, lockstart, lockend, |
| 7423 | unlock_bits, 1, 0, &cached_state, GFP_NOFS); |
Josef Bacik | e1cbbfa | 2015-03-17 10:52:28 -0400 | [diff] [blame^] | 7424 | if (outstanding_extents) |
| 7425 | current->journal_info = outstanding_extents; |
Josef Bacik | eb838e7 | 2012-07-31 16:28:48 -0400 | [diff] [blame] | 7426 | return ret; |
Josef Bacik | 4b46fce | 2010-05-23 11:00:55 -0400 | [diff] [blame] | 7427 | } |
| 7428 | |
Miao Xie | 8b110e3 | 2014-09-12 18:44:03 +0800 | [diff] [blame] | 7429 | static inline int submit_dio_repair_bio(struct inode *inode, struct bio *bio, |
| 7430 | int rw, int mirror_num) |
| 7431 | { |
| 7432 | struct btrfs_root *root = BTRFS_I(inode)->root; |
| 7433 | int ret; |
| 7434 | |
| 7435 | BUG_ON(rw & REQ_WRITE); |
| 7436 | |
| 7437 | bio_get(bio); |
| 7438 | |
| 7439 | ret = btrfs_bio_wq_end_io(root->fs_info, bio, |
| 7440 | BTRFS_WQ_ENDIO_DIO_REPAIR); |
| 7441 | if (ret) |
| 7442 | goto err; |
| 7443 | |
| 7444 | ret = btrfs_map_bio(root, rw, bio, mirror_num, 0); |
| 7445 | err: |
| 7446 | bio_put(bio); |
| 7447 | return ret; |
| 7448 | } |
| 7449 | |
| 7450 | static int btrfs_check_dio_repairable(struct inode *inode, |
| 7451 | struct bio *failed_bio, |
| 7452 | struct io_failure_record *failrec, |
| 7453 | int failed_mirror) |
| 7454 | { |
| 7455 | int num_copies; |
| 7456 | |
| 7457 | num_copies = btrfs_num_copies(BTRFS_I(inode)->root->fs_info, |
| 7458 | failrec->logical, failrec->len); |
| 7459 | if (num_copies == 1) { |
| 7460 | /* |
| 7461 | * we only have a single copy of the data, so don't bother with |
| 7462 | * all the retry and error correction code that follows. no |
| 7463 | * matter what the error is, it is very likely to persist. |
| 7464 | */ |
| 7465 | pr_debug("Check DIO Repairable: cannot repair, num_copies=%d, next_mirror %d, failed_mirror %d\n", |
| 7466 | num_copies, failrec->this_mirror, failed_mirror); |
| 7467 | return 0; |
| 7468 | } |
| 7469 | |
| 7470 | failrec->failed_mirror = failed_mirror; |
| 7471 | failrec->this_mirror++; |
| 7472 | if (failrec->this_mirror == failed_mirror) |
| 7473 | failrec->this_mirror++; |
| 7474 | |
| 7475 | if (failrec->this_mirror > num_copies) { |
| 7476 | pr_debug("Check DIO Repairable: (fail) num_copies=%d, next_mirror %d, failed_mirror %d\n", |
| 7477 | num_copies, failrec->this_mirror, failed_mirror); |
| 7478 | return 0; |
| 7479 | } |
| 7480 | |
| 7481 | return 1; |
| 7482 | } |
| 7483 | |
| 7484 | static int dio_read_error(struct inode *inode, struct bio *failed_bio, |
| 7485 | struct page *page, u64 start, u64 end, |
| 7486 | int failed_mirror, bio_end_io_t *repair_endio, |
| 7487 | void *repair_arg) |
| 7488 | { |
| 7489 | struct io_failure_record *failrec; |
| 7490 | struct bio *bio; |
| 7491 | int isector; |
| 7492 | int read_mode; |
| 7493 | int ret; |
| 7494 | |
| 7495 | BUG_ON(failed_bio->bi_rw & REQ_WRITE); |
| 7496 | |
| 7497 | ret = btrfs_get_io_failure_record(inode, start, end, &failrec); |
| 7498 | if (ret) |
| 7499 | return ret; |
| 7500 | |
| 7501 | ret = btrfs_check_dio_repairable(inode, failed_bio, failrec, |
| 7502 | failed_mirror); |
| 7503 | if (!ret) { |
| 7504 | free_io_failure(inode, failrec); |
| 7505 | return -EIO; |
| 7506 | } |
| 7507 | |
| 7508 | if (failed_bio->bi_vcnt > 1) |
| 7509 | read_mode = READ_SYNC | REQ_FAILFAST_DEV; |
| 7510 | else |
| 7511 | read_mode = READ_SYNC; |
| 7512 | |
| 7513 | isector = start - btrfs_io_bio(failed_bio)->logical; |
| 7514 | isector >>= inode->i_sb->s_blocksize_bits; |
| 7515 | bio = btrfs_create_repair_bio(inode, failed_bio, failrec, page, |
| 7516 | 0, isector, repair_endio, repair_arg); |
| 7517 | if (!bio) { |
| 7518 | free_io_failure(inode, failrec); |
| 7519 | return -EIO; |
| 7520 | } |
| 7521 | |
| 7522 | btrfs_debug(BTRFS_I(inode)->root->fs_info, |
| 7523 | "Repair DIO Read Error: submitting new dio read[%#x] to this_mirror=%d, in_validation=%d\n", |
| 7524 | read_mode, failrec->this_mirror, failrec->in_validation); |
| 7525 | |
| 7526 | ret = submit_dio_repair_bio(inode, bio, read_mode, |
| 7527 | failrec->this_mirror); |
| 7528 | if (ret) { |
| 7529 | free_io_failure(inode, failrec); |
| 7530 | bio_put(bio); |
| 7531 | } |
| 7532 | |
| 7533 | return ret; |
| 7534 | } |
| 7535 | |
| 7536 | struct btrfs_retry_complete { |
| 7537 | struct completion done; |
| 7538 | struct inode *inode; |
| 7539 | u64 start; |
| 7540 | int uptodate; |
| 7541 | }; |
| 7542 | |
| 7543 | static void btrfs_retry_endio_nocsum(struct bio *bio, int err) |
| 7544 | { |
| 7545 | struct btrfs_retry_complete *done = bio->bi_private; |
| 7546 | struct bio_vec *bvec; |
| 7547 | int i; |
| 7548 | |
| 7549 | if (err) |
| 7550 | goto end; |
| 7551 | |
| 7552 | done->uptodate = 1; |
| 7553 | bio_for_each_segment_all(bvec, bio, i) |
| 7554 | clean_io_failure(done->inode, done->start, bvec->bv_page, 0); |
| 7555 | end: |
| 7556 | complete(&done->done); |
| 7557 | bio_put(bio); |
| 7558 | } |
| 7559 | |
| 7560 | static int __btrfs_correct_data_nocsum(struct inode *inode, |
| 7561 | struct btrfs_io_bio *io_bio) |
Josef Bacik | 4b46fce | 2010-05-23 11:00:55 -0400 | [diff] [blame] | 7562 | { |
Kent Overstreet | 2c30c71 | 2013-11-07 12:20:26 -0800 | [diff] [blame] | 7563 | struct bio_vec *bvec; |
Miao Xie | 8b110e3 | 2014-09-12 18:44:03 +0800 | [diff] [blame] | 7564 | struct btrfs_retry_complete done; |
Josef Bacik | 4b46fce | 2010-05-23 11:00:55 -0400 | [diff] [blame] | 7565 | u64 start; |
Kent Overstreet | 2c30c71 | 2013-11-07 12:20:26 -0800 | [diff] [blame] | 7566 | int i; |
Miao Xie | c1dc089 | 2014-09-12 18:43:56 +0800 | [diff] [blame] | 7567 | int ret; |
Miao Xie | dc380ae | 2014-09-12 18:43:55 +0800 | [diff] [blame] | 7568 | |
Miao Xie | c1dc089 | 2014-09-12 18:43:56 +0800 | [diff] [blame] | 7569 | start = io_bio->logical; |
Miao Xie | 8b110e3 | 2014-09-12 18:44:03 +0800 | [diff] [blame] | 7570 | done.inode = inode; |
| 7571 | |
| 7572 | bio_for_each_segment_all(bvec, &io_bio->bio, i) { |
| 7573 | try_again: |
| 7574 | done.uptodate = 0; |
| 7575 | done.start = start; |
| 7576 | init_completion(&done.done); |
| 7577 | |
| 7578 | ret = dio_read_error(inode, &io_bio->bio, bvec->bv_page, start, |
| 7579 | start + bvec->bv_len - 1, |
| 7580 | io_bio->mirror_num, |
| 7581 | btrfs_retry_endio_nocsum, &done); |
| 7582 | if (ret) |
| 7583 | return ret; |
| 7584 | |
| 7585 | wait_for_completion(&done.done); |
| 7586 | |
| 7587 | if (!done.uptodate) { |
| 7588 | /* We might have another mirror, so try again */ |
| 7589 | goto try_again; |
| 7590 | } |
| 7591 | |
| 7592 | start += bvec->bv_len; |
| 7593 | } |
| 7594 | |
| 7595 | return 0; |
| 7596 | } |
| 7597 | |
| 7598 | static void btrfs_retry_endio(struct bio *bio, int err) |
| 7599 | { |
| 7600 | struct btrfs_retry_complete *done = bio->bi_private; |
| 7601 | struct btrfs_io_bio *io_bio = btrfs_io_bio(bio); |
| 7602 | struct bio_vec *bvec; |
| 7603 | int uptodate; |
| 7604 | int ret; |
| 7605 | int i; |
| 7606 | |
| 7607 | if (err) |
| 7608 | goto end; |
| 7609 | |
| 7610 | uptodate = 1; |
| 7611 | bio_for_each_segment_all(bvec, bio, i) { |
| 7612 | ret = __readpage_endio_check(done->inode, io_bio, i, |
| 7613 | bvec->bv_page, 0, |
| 7614 | done->start, bvec->bv_len); |
| 7615 | if (!ret) |
| 7616 | clean_io_failure(done->inode, done->start, |
| 7617 | bvec->bv_page, 0); |
| 7618 | else |
| 7619 | uptodate = 0; |
| 7620 | } |
| 7621 | |
| 7622 | done->uptodate = uptodate; |
| 7623 | end: |
| 7624 | complete(&done->done); |
| 7625 | bio_put(bio); |
| 7626 | } |
| 7627 | |
| 7628 | static int __btrfs_subio_endio_read(struct inode *inode, |
| 7629 | struct btrfs_io_bio *io_bio, int err) |
| 7630 | { |
| 7631 | struct bio_vec *bvec; |
| 7632 | struct btrfs_retry_complete done; |
| 7633 | u64 start; |
| 7634 | u64 offset = 0; |
| 7635 | int i; |
| 7636 | int ret; |
| 7637 | |
| 7638 | err = 0; |
| 7639 | start = io_bio->logical; |
| 7640 | done.inode = inode; |
| 7641 | |
Miao Xie | c1dc089 | 2014-09-12 18:43:56 +0800 | [diff] [blame] | 7642 | bio_for_each_segment_all(bvec, &io_bio->bio, i) { |
Miao Xie | dc380ae | 2014-09-12 18:43:55 +0800 | [diff] [blame] | 7643 | ret = __readpage_endio_check(inode, io_bio, i, bvec->bv_page, |
| 7644 | 0, start, bvec->bv_len); |
Miao Xie | 8b110e3 | 2014-09-12 18:44:03 +0800 | [diff] [blame] | 7645 | if (likely(!ret)) |
| 7646 | goto next; |
| 7647 | try_again: |
| 7648 | done.uptodate = 0; |
| 7649 | done.start = start; |
| 7650 | init_completion(&done.done); |
| 7651 | |
| 7652 | ret = dio_read_error(inode, &io_bio->bio, bvec->bv_page, start, |
| 7653 | start + bvec->bv_len - 1, |
| 7654 | io_bio->mirror_num, |
| 7655 | btrfs_retry_endio, &done); |
| 7656 | if (ret) { |
| 7657 | err = ret; |
| 7658 | goto next; |
| 7659 | } |
| 7660 | |
| 7661 | wait_for_completion(&done.done); |
| 7662 | |
| 7663 | if (!done.uptodate) { |
| 7664 | /* We might have another mirror, so try again */ |
| 7665 | goto try_again; |
| 7666 | } |
| 7667 | next: |
| 7668 | offset += bvec->bv_len; |
Josef Bacik | 4b46fce | 2010-05-23 11:00:55 -0400 | [diff] [blame] | 7669 | start += bvec->bv_len; |
Kent Overstreet | 2c30c71 | 2013-11-07 12:20:26 -0800 | [diff] [blame] | 7670 | } |
Miao Xie | c1dc089 | 2014-09-12 18:43:56 +0800 | [diff] [blame] | 7671 | |
| 7672 | return err; |
| 7673 | } |
| 7674 | |
Miao Xie | 8b110e3 | 2014-09-12 18:44:03 +0800 | [diff] [blame] | 7675 | static int btrfs_subio_endio_read(struct inode *inode, |
| 7676 | struct btrfs_io_bio *io_bio, int err) |
| 7677 | { |
| 7678 | bool skip_csum = BTRFS_I(inode)->flags & BTRFS_INODE_NODATASUM; |
| 7679 | |
| 7680 | if (skip_csum) { |
| 7681 | if (unlikely(err)) |
| 7682 | return __btrfs_correct_data_nocsum(inode, io_bio); |
| 7683 | else |
| 7684 | return 0; |
| 7685 | } else { |
| 7686 | return __btrfs_subio_endio_read(inode, io_bio, err); |
| 7687 | } |
| 7688 | } |
| 7689 | |
Miao Xie | c1dc089 | 2014-09-12 18:43:56 +0800 | [diff] [blame] | 7690 | static void btrfs_endio_direct_read(struct bio *bio, int err) |
| 7691 | { |
| 7692 | struct btrfs_dio_private *dip = bio->bi_private; |
| 7693 | struct inode *inode = dip->inode; |
| 7694 | struct bio *dio_bio; |
| 7695 | struct btrfs_io_bio *io_bio = btrfs_io_bio(bio); |
| 7696 | |
Miao Xie | 8b110e3 | 2014-09-12 18:44:03 +0800 | [diff] [blame] | 7697 | if (dip->flags & BTRFS_DIO_ORIG_BIO_SUBMITTED) |
| 7698 | err = btrfs_subio_endio_read(inode, io_bio, err); |
Miao Xie | c1dc089 | 2014-09-12 18:43:56 +0800 | [diff] [blame] | 7699 | |
Josef Bacik | 4b46fce | 2010-05-23 11:00:55 -0400 | [diff] [blame] | 7700 | unlock_extent(&BTRFS_I(inode)->io_tree, dip->logical_offset, |
Jeff Mahoney | d008237 | 2012-03-01 14:57:19 +0100 | [diff] [blame] | 7701 | dip->logical_offset + dip->bytes - 1); |
Chris Mason | 9be3395 | 2013-05-17 18:30:14 -0400 | [diff] [blame] | 7702 | dio_bio = dip->dio_bio; |
Josef Bacik | 4b46fce | 2010-05-23 11:00:55 -0400 | [diff] [blame] | 7703 | |
Josef Bacik | 4b46fce | 2010-05-23 11:00:55 -0400 | [diff] [blame] | 7704 | kfree(dip); |
Josef Bacik | c0da7aa | 2011-03-22 11:05:07 -0400 | [diff] [blame] | 7705 | |
| 7706 | /* If we had a csum failure make sure to clear the uptodate flag */ |
| 7707 | if (err) |
Chris Mason | 9be3395 | 2013-05-17 18:30:14 -0400 | [diff] [blame] | 7708 | clear_bit(BIO_UPTODATE, &dio_bio->bi_flags); |
| 7709 | dio_end_io(dio_bio, err); |
Miao Xie | 23ea8e5 | 2014-09-12 18:43:54 +0800 | [diff] [blame] | 7710 | |
| 7711 | if (io_bio->end_io) |
| 7712 | io_bio->end_io(io_bio, err); |
Chris Mason | 9be3395 | 2013-05-17 18:30:14 -0400 | [diff] [blame] | 7713 | bio_put(bio); |
Josef Bacik | 4b46fce | 2010-05-23 11:00:55 -0400 | [diff] [blame] | 7714 | } |
| 7715 | |
| 7716 | static void btrfs_endio_direct_write(struct bio *bio, int err) |
| 7717 | { |
| 7718 | struct btrfs_dio_private *dip = bio->bi_private; |
| 7719 | struct inode *inode = dip->inode; |
| 7720 | struct btrfs_root *root = BTRFS_I(inode)->root; |
Josef Bacik | 4b46fce | 2010-05-23 11:00:55 -0400 | [diff] [blame] | 7721 | struct btrfs_ordered_extent *ordered = NULL; |
Chris Mason | 163cf09 | 2010-11-28 19:56:33 -0500 | [diff] [blame] | 7722 | u64 ordered_offset = dip->logical_offset; |
| 7723 | u64 ordered_bytes = dip->bytes; |
Chris Mason | 9be3395 | 2013-05-17 18:30:14 -0400 | [diff] [blame] | 7724 | struct bio *dio_bio; |
Josef Bacik | 4b46fce | 2010-05-23 11:00:55 -0400 | [diff] [blame] | 7725 | int ret; |
| 7726 | |
| 7727 | if (err) |
| 7728 | goto out_done; |
Chris Mason | 163cf09 | 2010-11-28 19:56:33 -0500 | [diff] [blame] | 7729 | again: |
| 7730 | ret = btrfs_dec_test_first_ordered_pending(inode, &ordered, |
| 7731 | &ordered_offset, |
Josef Bacik | 5fd0204 | 2012-05-02 14:00:54 -0400 | [diff] [blame] | 7732 | ordered_bytes, !err); |
Josef Bacik | 4b46fce | 2010-05-23 11:00:55 -0400 | [diff] [blame] | 7733 | if (!ret) |
Chris Mason | 163cf09 | 2010-11-28 19:56:33 -0500 | [diff] [blame] | 7734 | goto out_test; |
Josef Bacik | 4b46fce | 2010-05-23 11:00:55 -0400 | [diff] [blame] | 7735 | |
Liu Bo | 9e0af23 | 2014-08-15 23:36:53 +0800 | [diff] [blame] | 7736 | btrfs_init_work(&ordered->work, btrfs_endio_write_helper, |
| 7737 | finish_ordered_fn, NULL, NULL); |
Qu Wenruo | fccb5d8 | 2014-02-28 10:46:10 +0800 | [diff] [blame] | 7738 | btrfs_queue_work(root->fs_info->endio_write_workers, |
| 7739 | &ordered->work); |
Chris Mason | 163cf09 | 2010-11-28 19:56:33 -0500 | [diff] [blame] | 7740 | out_test: |
| 7741 | /* |
| 7742 | * our bio might span multiple ordered extents. If we haven't |
| 7743 | * completed the accounting for the whole dio, go back and try again |
| 7744 | */ |
| 7745 | if (ordered_offset < dip->logical_offset + dip->bytes) { |
| 7746 | ordered_bytes = dip->logical_offset + dip->bytes - |
| 7747 | ordered_offset; |
Josef Bacik | 5fd0204 | 2012-05-02 14:00:54 -0400 | [diff] [blame] | 7748 | ordered = NULL; |
Chris Mason | 163cf09 | 2010-11-28 19:56:33 -0500 | [diff] [blame] | 7749 | goto again; |
| 7750 | } |
Josef Bacik | 4b46fce | 2010-05-23 11:00:55 -0400 | [diff] [blame] | 7751 | out_done: |
Chris Mason | 9be3395 | 2013-05-17 18:30:14 -0400 | [diff] [blame] | 7752 | dio_bio = dip->dio_bio; |
Josef Bacik | 4b46fce | 2010-05-23 11:00:55 -0400 | [diff] [blame] | 7753 | |
Josef Bacik | 4b46fce | 2010-05-23 11:00:55 -0400 | [diff] [blame] | 7754 | kfree(dip); |
Josef Bacik | c0da7aa | 2011-03-22 11:05:07 -0400 | [diff] [blame] | 7755 | |
| 7756 | /* If we had an error make sure to clear the uptodate flag */ |
| 7757 | if (err) |
Chris Mason | 9be3395 | 2013-05-17 18:30:14 -0400 | [diff] [blame] | 7758 | clear_bit(BIO_UPTODATE, &dio_bio->bi_flags); |
| 7759 | dio_end_io(dio_bio, err); |
| 7760 | bio_put(bio); |
Josef Bacik | 4b46fce | 2010-05-23 11:00:55 -0400 | [diff] [blame] | 7761 | } |
| 7762 | |
Chris Mason | eaf25d9 | 2010-05-25 09:48:28 -0400 | [diff] [blame] | 7763 | static int __btrfs_submit_bio_start_direct_io(struct inode *inode, int rw, |
| 7764 | struct bio *bio, int mirror_num, |
| 7765 | unsigned long bio_flags, u64 offset) |
| 7766 | { |
| 7767 | int ret; |
| 7768 | struct btrfs_root *root = BTRFS_I(inode)->root; |
| 7769 | ret = btrfs_csum_one_bio(root, inode, bio, offset, 1); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 7770 | BUG_ON(ret); /* -ENOMEM */ |
Chris Mason | eaf25d9 | 2010-05-25 09:48:28 -0400 | [diff] [blame] | 7771 | return 0; |
| 7772 | } |
| 7773 | |
Miao Xie | e65e153 | 2010-11-22 03:04:43 +0000 | [diff] [blame] | 7774 | static void btrfs_end_dio_bio(struct bio *bio, int err) |
| 7775 | { |
| 7776 | struct btrfs_dio_private *dip = bio->bi_private; |
| 7777 | |
Miao Xie | 8b110e3 | 2014-09-12 18:44:03 +0800 | [diff] [blame] | 7778 | if (err) |
| 7779 | btrfs_warn(BTRFS_I(dip->inode)->root->fs_info, |
| 7780 | "direct IO failed ino %llu rw %lu sector %#Lx len %u err no %d", |
| 7781 | btrfs_ino(dip->inode), bio->bi_rw, |
| 7782 | (unsigned long long)bio->bi_iter.bi_sector, |
| 7783 | bio->bi_iter.bi_size, err); |
| 7784 | |
| 7785 | if (dip->subio_endio) |
| 7786 | err = dip->subio_endio(dip->inode, btrfs_io_bio(bio), err); |
Miao Xie | c1dc089 | 2014-09-12 18:43:56 +0800 | [diff] [blame] | 7787 | |
| 7788 | if (err) { |
Miao Xie | e65e153 | 2010-11-22 03:04:43 +0000 | [diff] [blame] | 7789 | dip->errors = 1; |
| 7790 | |
| 7791 | /* |
| 7792 | * before atomic variable goto zero, we must make sure |
| 7793 | * dip->errors is perceived to be set. |
| 7794 | */ |
Peter Zijlstra | 4e857c5 | 2014-03-17 18:06:10 +0100 | [diff] [blame] | 7795 | smp_mb__before_atomic(); |
Miao Xie | e65e153 | 2010-11-22 03:04:43 +0000 | [diff] [blame] | 7796 | } |
| 7797 | |
| 7798 | /* if there are more bios still pending for this dio, just exit */ |
| 7799 | if (!atomic_dec_and_test(&dip->pending_bios)) |
| 7800 | goto out; |
| 7801 | |
Chris Mason | 9be3395 | 2013-05-17 18:30:14 -0400 | [diff] [blame] | 7802 | if (dip->errors) { |
Miao Xie | e65e153 | 2010-11-22 03:04:43 +0000 | [diff] [blame] | 7803 | bio_io_error(dip->orig_bio); |
Chris Mason | 9be3395 | 2013-05-17 18:30:14 -0400 | [diff] [blame] | 7804 | } else { |
| 7805 | set_bit(BIO_UPTODATE, &dip->dio_bio->bi_flags); |
Miao Xie | e65e153 | 2010-11-22 03:04:43 +0000 | [diff] [blame] | 7806 | bio_endio(dip->orig_bio, 0); |
| 7807 | } |
| 7808 | out: |
| 7809 | bio_put(bio); |
| 7810 | } |
| 7811 | |
| 7812 | static struct bio *btrfs_dio_bio_alloc(struct block_device *bdev, |
| 7813 | u64 first_sector, gfp_t gfp_flags) |
| 7814 | { |
| 7815 | int nr_vecs = bio_get_nr_vecs(bdev); |
| 7816 | return btrfs_bio_alloc(bdev, first_sector, nr_vecs, gfp_flags); |
| 7817 | } |
| 7818 | |
Miao Xie | c1dc089 | 2014-09-12 18:43:56 +0800 | [diff] [blame] | 7819 | static inline int btrfs_lookup_and_bind_dio_csum(struct btrfs_root *root, |
| 7820 | struct inode *inode, |
| 7821 | struct btrfs_dio_private *dip, |
| 7822 | struct bio *bio, |
| 7823 | u64 file_offset) |
| 7824 | { |
| 7825 | struct btrfs_io_bio *io_bio = btrfs_io_bio(bio); |
| 7826 | struct btrfs_io_bio *orig_io_bio = btrfs_io_bio(dip->orig_bio); |
| 7827 | int ret; |
| 7828 | |
| 7829 | /* |
| 7830 | * We load all the csum data we need when we submit |
| 7831 | * the first bio to reduce the csum tree search and |
| 7832 | * contention. |
| 7833 | */ |
| 7834 | if (dip->logical_offset == file_offset) { |
| 7835 | ret = btrfs_lookup_bio_sums_dio(root, inode, dip->orig_bio, |
| 7836 | file_offset); |
| 7837 | if (ret) |
| 7838 | return ret; |
| 7839 | } |
| 7840 | |
| 7841 | if (bio == dip->orig_bio) |
| 7842 | return 0; |
| 7843 | |
| 7844 | file_offset -= dip->logical_offset; |
| 7845 | file_offset >>= inode->i_sb->s_blocksize_bits; |
| 7846 | io_bio->csum = (u8 *)(((u32 *)orig_io_bio->csum) + file_offset); |
| 7847 | |
| 7848 | return 0; |
| 7849 | } |
| 7850 | |
Miao Xie | e65e153 | 2010-11-22 03:04:43 +0000 | [diff] [blame] | 7851 | static inline int __btrfs_submit_dio_bio(struct bio *bio, struct inode *inode, |
| 7852 | int rw, u64 file_offset, int skip_sum, |
Josef Bacik | c329861 | 2012-08-03 16:49:19 -0400 | [diff] [blame] | 7853 | int async_submit) |
Miao Xie | e65e153 | 2010-11-22 03:04:43 +0000 | [diff] [blame] | 7854 | { |
Miao Xie | facc8a22 | 2013-07-25 19:22:34 +0800 | [diff] [blame] | 7855 | struct btrfs_dio_private *dip = bio->bi_private; |
Miao Xie | e65e153 | 2010-11-22 03:04:43 +0000 | [diff] [blame] | 7856 | int write = rw & REQ_WRITE; |
| 7857 | struct btrfs_root *root = BTRFS_I(inode)->root; |
| 7858 | int ret; |
| 7859 | |
Josef Bacik | b812ce2 | 2012-11-16 13:56:32 -0500 | [diff] [blame] | 7860 | if (async_submit) |
| 7861 | async_submit = !atomic_read(&BTRFS_I(inode)->sync_writers); |
| 7862 | |
Miao Xie | e65e153 | 2010-11-22 03:04:43 +0000 | [diff] [blame] | 7863 | bio_get(bio); |
Josef Bacik | 5fd0204 | 2012-05-02 14:00:54 -0400 | [diff] [blame] | 7864 | |
| 7865 | if (!write) { |
David Sterba | bfebd8b | 2014-07-30 00:25:45 +0200 | [diff] [blame] | 7866 | ret = btrfs_bio_wq_end_io(root->fs_info, bio, |
| 7867 | BTRFS_WQ_ENDIO_DATA); |
Josef Bacik | 5fd0204 | 2012-05-02 14:00:54 -0400 | [diff] [blame] | 7868 | if (ret) |
| 7869 | goto err; |
| 7870 | } |
Miao Xie | e65e153 | 2010-11-22 03:04:43 +0000 | [diff] [blame] | 7871 | |
Josef Bacik | 1ae3993 | 2011-04-06 14:41:34 -0400 | [diff] [blame] | 7872 | if (skip_sum) |
| 7873 | goto map; |
| 7874 | |
| 7875 | if (write && async_submit) { |
Miao Xie | e65e153 | 2010-11-22 03:04:43 +0000 | [diff] [blame] | 7876 | ret = btrfs_wq_submit_bio(root->fs_info, |
| 7877 | inode, rw, bio, 0, 0, |
| 7878 | file_offset, |
| 7879 | __btrfs_submit_bio_start_direct_io, |
| 7880 | __btrfs_submit_bio_done); |
| 7881 | goto err; |
Josef Bacik | 1ae3993 | 2011-04-06 14:41:34 -0400 | [diff] [blame] | 7882 | } else if (write) { |
| 7883 | /* |
| 7884 | * If we aren't doing async submit, calculate the csum of the |
| 7885 | * bio now. |
| 7886 | */ |
| 7887 | ret = btrfs_csum_one_bio(root, inode, bio, file_offset, 1); |
| 7888 | if (ret) |
| 7889 | goto err; |
Miao Xie | 23ea8e5 | 2014-09-12 18:43:54 +0800 | [diff] [blame] | 7890 | } else { |
Miao Xie | c1dc089 | 2014-09-12 18:43:56 +0800 | [diff] [blame] | 7891 | ret = btrfs_lookup_and_bind_dio_csum(root, inode, dip, bio, |
| 7892 | file_offset); |
Tsutomu Itoh | c2db107 | 2011-03-01 06:48:31 +0000 | [diff] [blame] | 7893 | if (ret) |
| 7894 | goto err; |
| 7895 | } |
Josef Bacik | 1ae3993 | 2011-04-06 14:41:34 -0400 | [diff] [blame] | 7896 | map: |
| 7897 | ret = btrfs_map_bio(root, rw, bio, 0, async_submit); |
Miao Xie | e65e153 | 2010-11-22 03:04:43 +0000 | [diff] [blame] | 7898 | err: |
| 7899 | bio_put(bio); |
| 7900 | return ret; |
| 7901 | } |
| 7902 | |
| 7903 | static int btrfs_submit_direct_hook(int rw, struct btrfs_dio_private *dip, |
| 7904 | int skip_sum) |
| 7905 | { |
| 7906 | struct inode *inode = dip->inode; |
| 7907 | struct btrfs_root *root = BTRFS_I(inode)->root; |
Miao Xie | e65e153 | 2010-11-22 03:04:43 +0000 | [diff] [blame] | 7908 | struct bio *bio; |
| 7909 | struct bio *orig_bio = dip->orig_bio; |
| 7910 | struct bio_vec *bvec = orig_bio->bi_io_vec; |
Kent Overstreet | 4f024f3 | 2013-10-11 15:44:27 -0700 | [diff] [blame] | 7911 | u64 start_sector = orig_bio->bi_iter.bi_sector; |
Miao Xie | e65e153 | 2010-11-22 03:04:43 +0000 | [diff] [blame] | 7912 | u64 file_offset = dip->logical_offset; |
| 7913 | u64 submit_len = 0; |
| 7914 | u64 map_length; |
| 7915 | int nr_pages = 0; |
Miao Xie | 23ea8e5 | 2014-09-12 18:43:54 +0800 | [diff] [blame] | 7916 | int ret; |
Josef Bacik | 1ae3993 | 2011-04-06 14:41:34 -0400 | [diff] [blame] | 7917 | int async_submit = 0; |
Miao Xie | e65e153 | 2010-11-22 03:04:43 +0000 | [diff] [blame] | 7918 | |
Kent Overstreet | 4f024f3 | 2013-10-11 15:44:27 -0700 | [diff] [blame] | 7919 | map_length = orig_bio->bi_iter.bi_size; |
David Woodhouse | 53b381b | 2013-01-29 18:40:14 -0500 | [diff] [blame] | 7920 | ret = btrfs_map_block(root->fs_info, rw, start_sector << 9, |
Miao Xie | e65e153 | 2010-11-22 03:04:43 +0000 | [diff] [blame] | 7921 | &map_length, NULL, 0); |
Miao Xie | 7a5c3c9 | 2014-06-17 18:58:59 +0800 | [diff] [blame] | 7922 | if (ret) |
Miao Xie | e65e153 | 2010-11-22 03:04:43 +0000 | [diff] [blame] | 7923 | return -EIO; |
Miao Xie | facc8a22 | 2013-07-25 19:22:34 +0800 | [diff] [blame] | 7924 | |
Kent Overstreet | 4f024f3 | 2013-10-11 15:44:27 -0700 | [diff] [blame] | 7925 | if (map_length >= orig_bio->bi_iter.bi_size) { |
Josef Bacik | 02f57c7 | 2011-04-06 14:25:44 -0400 | [diff] [blame] | 7926 | bio = orig_bio; |
Miao Xie | c1dc089 | 2014-09-12 18:43:56 +0800 | [diff] [blame] | 7927 | dip->flags |= BTRFS_DIO_ORIG_BIO_SUBMITTED; |
Josef Bacik | 02f57c7 | 2011-04-06 14:25:44 -0400 | [diff] [blame] | 7928 | goto submit; |
| 7929 | } |
| 7930 | |
David Woodhouse | 53b381b | 2013-01-29 18:40:14 -0500 | [diff] [blame] | 7931 | /* async crcs make it difficult to collect full stripe writes. */ |
Zhao Lei | ffe2d20 | 2015-01-20 15:11:44 +0800 | [diff] [blame] | 7932 | if (btrfs_get_alloc_profile(root, 1) & BTRFS_BLOCK_GROUP_RAID56_MASK) |
David Woodhouse | 53b381b | 2013-01-29 18:40:14 -0500 | [diff] [blame] | 7933 | async_submit = 0; |
| 7934 | else |
| 7935 | async_submit = 1; |
| 7936 | |
Josef Bacik | 02f57c7 | 2011-04-06 14:25:44 -0400 | [diff] [blame] | 7937 | bio = btrfs_dio_bio_alloc(orig_bio->bi_bdev, start_sector, GFP_NOFS); |
| 7938 | if (!bio) |
| 7939 | return -ENOMEM; |
Miao Xie | 7a5c3c9 | 2014-06-17 18:58:59 +0800 | [diff] [blame] | 7940 | |
Josef Bacik | 02f57c7 | 2011-04-06 14:25:44 -0400 | [diff] [blame] | 7941 | bio->bi_private = dip; |
| 7942 | bio->bi_end_io = btrfs_end_dio_bio; |
Miao Xie | c1dc089 | 2014-09-12 18:43:56 +0800 | [diff] [blame] | 7943 | btrfs_io_bio(bio)->logical = file_offset; |
Josef Bacik | 02f57c7 | 2011-04-06 14:25:44 -0400 | [diff] [blame] | 7944 | atomic_inc(&dip->pending_bios); |
| 7945 | |
Miao Xie | e65e153 | 2010-11-22 03:04:43 +0000 | [diff] [blame] | 7946 | while (bvec <= (orig_bio->bi_io_vec + orig_bio->bi_vcnt - 1)) { |
David Sterba | ee39b43 | 2014-09-30 01:33:33 +0200 | [diff] [blame] | 7947 | if (map_length < submit_len + bvec->bv_len || |
Miao Xie | e65e153 | 2010-11-22 03:04:43 +0000 | [diff] [blame] | 7948 | bio_add_page(bio, bvec->bv_page, bvec->bv_len, |
David Sterba | ee39b43 | 2014-09-30 01:33:33 +0200 | [diff] [blame] | 7949 | bvec->bv_offset) < bvec->bv_len) { |
Miao Xie | e65e153 | 2010-11-22 03:04:43 +0000 | [diff] [blame] | 7950 | /* |
| 7951 | * inc the count before we submit the bio so |
| 7952 | * we know the end IO handler won't happen before |
| 7953 | * we inc the count. Otherwise, the dip might get freed |
| 7954 | * before we're done setting it up |
| 7955 | */ |
| 7956 | atomic_inc(&dip->pending_bios); |
| 7957 | ret = __btrfs_submit_dio_bio(bio, inode, rw, |
| 7958 | file_offset, skip_sum, |
Josef Bacik | c329861 | 2012-08-03 16:49:19 -0400 | [diff] [blame] | 7959 | async_submit); |
Miao Xie | e65e153 | 2010-11-22 03:04:43 +0000 | [diff] [blame] | 7960 | if (ret) { |
| 7961 | bio_put(bio); |
| 7962 | atomic_dec(&dip->pending_bios); |
| 7963 | goto out_err; |
| 7964 | } |
| 7965 | |
Miao Xie | e65e153 | 2010-11-22 03:04:43 +0000 | [diff] [blame] | 7966 | start_sector += submit_len >> 9; |
| 7967 | file_offset += submit_len; |
| 7968 | |
| 7969 | submit_len = 0; |
| 7970 | nr_pages = 0; |
| 7971 | |
| 7972 | bio = btrfs_dio_bio_alloc(orig_bio->bi_bdev, |
| 7973 | start_sector, GFP_NOFS); |
| 7974 | if (!bio) |
| 7975 | goto out_err; |
| 7976 | bio->bi_private = dip; |
| 7977 | bio->bi_end_io = btrfs_end_dio_bio; |
Miao Xie | c1dc089 | 2014-09-12 18:43:56 +0800 | [diff] [blame] | 7978 | btrfs_io_bio(bio)->logical = file_offset; |
Miao Xie | e65e153 | 2010-11-22 03:04:43 +0000 | [diff] [blame] | 7979 | |
Kent Overstreet | 4f024f3 | 2013-10-11 15:44:27 -0700 | [diff] [blame] | 7980 | map_length = orig_bio->bi_iter.bi_size; |
David Woodhouse | 53b381b | 2013-01-29 18:40:14 -0500 | [diff] [blame] | 7981 | ret = btrfs_map_block(root->fs_info, rw, |
Stefan Behrens | 3ec706c | 2012-11-05 15:46:42 +0100 | [diff] [blame] | 7982 | start_sector << 9, |
Miao Xie | e65e153 | 2010-11-22 03:04:43 +0000 | [diff] [blame] | 7983 | &map_length, NULL, 0); |
| 7984 | if (ret) { |
| 7985 | bio_put(bio); |
| 7986 | goto out_err; |
| 7987 | } |
| 7988 | } else { |
| 7989 | submit_len += bvec->bv_len; |
Dulshani Gunawardhana | 6787125 | 2013-10-31 10:33:04 +0530 | [diff] [blame] | 7990 | nr_pages++; |
Miao Xie | e65e153 | 2010-11-22 03:04:43 +0000 | [diff] [blame] | 7991 | bvec++; |
| 7992 | } |
| 7993 | } |
| 7994 | |
Josef Bacik | 02f57c7 | 2011-04-06 14:25:44 -0400 | [diff] [blame] | 7995 | submit: |
Miao Xie | e65e153 | 2010-11-22 03:04:43 +0000 | [diff] [blame] | 7996 | ret = __btrfs_submit_dio_bio(bio, inode, rw, file_offset, skip_sum, |
Josef Bacik | c329861 | 2012-08-03 16:49:19 -0400 | [diff] [blame] | 7997 | async_submit); |
Miao Xie | e65e153 | 2010-11-22 03:04:43 +0000 | [diff] [blame] | 7998 | if (!ret) |
| 7999 | return 0; |
| 8000 | |
| 8001 | bio_put(bio); |
| 8002 | out_err: |
| 8003 | dip->errors = 1; |
| 8004 | /* |
| 8005 | * before atomic variable goto zero, we must |
| 8006 | * make sure dip->errors is perceived to be set. |
| 8007 | */ |
Peter Zijlstra | 4e857c5 | 2014-03-17 18:06:10 +0100 | [diff] [blame] | 8008 | smp_mb__before_atomic(); |
Miao Xie | e65e153 | 2010-11-22 03:04:43 +0000 | [diff] [blame] | 8009 | if (atomic_dec_and_test(&dip->pending_bios)) |
| 8010 | bio_io_error(dip->orig_bio); |
| 8011 | |
| 8012 | /* bio_end_io() will handle error, so we needn't return it */ |
| 8013 | return 0; |
| 8014 | } |
| 8015 | |
Chris Mason | 9be3395 | 2013-05-17 18:30:14 -0400 | [diff] [blame] | 8016 | static void btrfs_submit_direct(int rw, struct bio *dio_bio, |
| 8017 | struct inode *inode, loff_t file_offset) |
Josef Bacik | 4b46fce | 2010-05-23 11:00:55 -0400 | [diff] [blame] | 8018 | { |
| 8019 | struct btrfs_root *root = BTRFS_I(inode)->root; |
| 8020 | struct btrfs_dio_private *dip; |
Chris Mason | 9be3395 | 2013-05-17 18:30:14 -0400 | [diff] [blame] | 8021 | struct bio *io_bio; |
Miao Xie | 23ea8e5 | 2014-09-12 18:43:54 +0800 | [diff] [blame] | 8022 | struct btrfs_io_bio *btrfs_bio; |
Josef Bacik | 4b46fce | 2010-05-23 11:00:55 -0400 | [diff] [blame] | 8023 | int skip_sum; |
Christoph Hellwig | 7b6d91d | 2010-08-07 18:20:39 +0200 | [diff] [blame] | 8024 | int write = rw & REQ_WRITE; |
Josef Bacik | 4b46fce | 2010-05-23 11:00:55 -0400 | [diff] [blame] | 8025 | int ret = 0; |
| 8026 | |
| 8027 | skip_sum = BTRFS_I(inode)->flags & BTRFS_INODE_NODATASUM; |
| 8028 | |
Chris Mason | 9be3395 | 2013-05-17 18:30:14 -0400 | [diff] [blame] | 8029 | io_bio = btrfs_bio_clone(dio_bio, GFP_NOFS); |
Chris Mason | 9be3395 | 2013-05-17 18:30:14 -0400 | [diff] [blame] | 8030 | if (!io_bio) { |
Josef Bacik | 4b46fce | 2010-05-23 11:00:55 -0400 | [diff] [blame] | 8031 | ret = -ENOMEM; |
| 8032 | goto free_ordered; |
| 8033 | } |
Josef Bacik | 4b46fce | 2010-05-23 11:00:55 -0400 | [diff] [blame] | 8034 | |
Miao Xie | c1dc089 | 2014-09-12 18:43:56 +0800 | [diff] [blame] | 8035 | dip = kzalloc(sizeof(*dip), GFP_NOFS); |
Chris Mason | 9be3395 | 2013-05-17 18:30:14 -0400 | [diff] [blame] | 8036 | if (!dip) { |
| 8037 | ret = -ENOMEM; |
| 8038 | goto free_io_bio; |
| 8039 | } |
| 8040 | |
| 8041 | dip->private = dio_bio->bi_private; |
Josef Bacik | 4b46fce | 2010-05-23 11:00:55 -0400 | [diff] [blame] | 8042 | dip->inode = inode; |
| 8043 | dip->logical_offset = file_offset; |
Kent Overstreet | 4f024f3 | 2013-10-11 15:44:27 -0700 | [diff] [blame] | 8044 | dip->bytes = dio_bio->bi_iter.bi_size; |
| 8045 | dip->disk_bytenr = (u64)dio_bio->bi_iter.bi_sector << 9; |
Chris Mason | 9be3395 | 2013-05-17 18:30:14 -0400 | [diff] [blame] | 8046 | io_bio->bi_private = dip; |
Chris Mason | 9be3395 | 2013-05-17 18:30:14 -0400 | [diff] [blame] | 8047 | dip->orig_bio = io_bio; |
| 8048 | dip->dio_bio = dio_bio; |
Miao Xie | e65e153 | 2010-11-22 03:04:43 +0000 | [diff] [blame] | 8049 | atomic_set(&dip->pending_bios, 0); |
Miao Xie | c1dc089 | 2014-09-12 18:43:56 +0800 | [diff] [blame] | 8050 | btrfs_bio = btrfs_io_bio(io_bio); |
| 8051 | btrfs_bio->logical = file_offset; |
Josef Bacik | 4b46fce | 2010-05-23 11:00:55 -0400 | [diff] [blame] | 8052 | |
Miao Xie | c1dc089 | 2014-09-12 18:43:56 +0800 | [diff] [blame] | 8053 | if (write) { |
Chris Mason | 9be3395 | 2013-05-17 18:30:14 -0400 | [diff] [blame] | 8054 | io_bio->bi_end_io = btrfs_endio_direct_write; |
Miao Xie | c1dc089 | 2014-09-12 18:43:56 +0800 | [diff] [blame] | 8055 | } else { |
Chris Mason | 9be3395 | 2013-05-17 18:30:14 -0400 | [diff] [blame] | 8056 | io_bio->bi_end_io = btrfs_endio_direct_read; |
Miao Xie | c1dc089 | 2014-09-12 18:43:56 +0800 | [diff] [blame] | 8057 | dip->subio_endio = btrfs_subio_endio_read; |
| 8058 | } |
Josef Bacik | 4b46fce | 2010-05-23 11:00:55 -0400 | [diff] [blame] | 8059 | |
Miao Xie | e65e153 | 2010-11-22 03:04:43 +0000 | [diff] [blame] | 8060 | ret = btrfs_submit_direct_hook(rw, dip, skip_sum); |
| 8061 | if (!ret) |
Chris Mason | eaf25d9 | 2010-05-25 09:48:28 -0400 | [diff] [blame] | 8062 | return; |
Chris Mason | 9be3395 | 2013-05-17 18:30:14 -0400 | [diff] [blame] | 8063 | |
Miao Xie | 23ea8e5 | 2014-09-12 18:43:54 +0800 | [diff] [blame] | 8064 | if (btrfs_bio->end_io) |
| 8065 | btrfs_bio->end_io(btrfs_bio, ret); |
Chris Mason | 9be3395 | 2013-05-17 18:30:14 -0400 | [diff] [blame] | 8066 | free_io_bio: |
| 8067 | bio_put(io_bio); |
| 8068 | |
Josef Bacik | 4b46fce | 2010-05-23 11:00:55 -0400 | [diff] [blame] | 8069 | free_ordered: |
| 8070 | /* |
| 8071 | * If this is a write, we need to clean up the reserved space and kill |
| 8072 | * the ordered extent. |
| 8073 | */ |
| 8074 | if (write) { |
| 8075 | struct btrfs_ordered_extent *ordered; |
Josef Bacik | 955256f | 2010-11-19 09:41:10 -0500 | [diff] [blame] | 8076 | ordered = btrfs_lookup_ordered_extent(inode, file_offset); |
Josef Bacik | 4b46fce | 2010-05-23 11:00:55 -0400 | [diff] [blame] | 8077 | if (!test_bit(BTRFS_ORDERED_PREALLOC, &ordered->flags) && |
| 8078 | !test_bit(BTRFS_ORDERED_NOCOW, &ordered->flags)) |
| 8079 | btrfs_free_reserved_extent(root, ordered->start, |
Miao Xie | e570fd2 | 2014-06-19 10:42:50 +0800 | [diff] [blame] | 8080 | ordered->disk_len, 1); |
Josef Bacik | 4b46fce | 2010-05-23 11:00:55 -0400 | [diff] [blame] | 8081 | btrfs_put_ordered_extent(ordered); |
| 8082 | btrfs_put_ordered_extent(ordered); |
| 8083 | } |
Chris Mason | 9be3395 | 2013-05-17 18:30:14 -0400 | [diff] [blame] | 8084 | bio_endio(dio_bio, ret); |
Josef Bacik | 4b46fce | 2010-05-23 11:00:55 -0400 | [diff] [blame] | 8085 | } |
| 8086 | |
Chris Mason | 5a5f79b | 2010-05-26 21:33:37 -0400 | [diff] [blame] | 8087 | 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] | 8088 | const struct iov_iter *iter, loff_t offset) |
Chris Mason | 5a5f79b | 2010-05-26 21:33:37 -0400 | [diff] [blame] | 8089 | { |
| 8090 | int seg; |
Josef Bacik | a1b75f7 | 2011-04-08 15:51:18 +0000 | [diff] [blame] | 8091 | int i; |
Chris Mason | 5a5f79b | 2010-05-26 21:33:37 -0400 | [diff] [blame] | 8092 | unsigned blocksize_mask = root->sectorsize - 1; |
| 8093 | ssize_t retval = -EINVAL; |
Chris Mason | 5a5f79b | 2010-05-26 21:33:37 -0400 | [diff] [blame] | 8094 | |
| 8095 | if (offset & blocksize_mask) |
| 8096 | goto out; |
| 8097 | |
Al Viro | 28060d5 | 2014-03-22 05:15:17 -0400 | [diff] [blame] | 8098 | if (iov_iter_alignment(iter) & blocksize_mask) |
| 8099 | goto out; |
Josef Bacik | a1b75f7 | 2011-04-08 15:51:18 +0000 | [diff] [blame] | 8100 | |
Al Viro | 28060d5 | 2014-03-22 05:15:17 -0400 | [diff] [blame] | 8101 | /* If this is a write we don't need to check anymore */ |
| 8102 | if (rw & WRITE) |
| 8103 | return 0; |
| 8104 | /* |
| 8105 | * Check to make sure we don't have duplicate iov_base's in this |
| 8106 | * iovec, if so return EINVAL, otherwise we'll get csum errors |
| 8107 | * when reading back. |
| 8108 | */ |
| 8109 | for (seg = 0; seg < iter->nr_segs; seg++) { |
| 8110 | for (i = seg + 1; i < iter->nr_segs; i++) { |
| 8111 | if (iter->iov[seg].iov_base == iter->iov[i].iov_base) |
Josef Bacik | a1b75f7 | 2011-04-08 15:51:18 +0000 | [diff] [blame] | 8112 | goto out; |
| 8113 | } |
Chris Mason | 5a5f79b | 2010-05-26 21:33:37 -0400 | [diff] [blame] | 8114 | } |
| 8115 | retval = 0; |
| 8116 | out: |
| 8117 | return retval; |
| 8118 | } |
Josef Bacik | eb838e7 | 2012-07-31 16:28:48 -0400 | [diff] [blame] | 8119 | |
Chris Mason | 1643298 | 2008-04-10 10:23:21 -0400 | [diff] [blame] | 8120 | static ssize_t btrfs_direct_IO(int rw, struct kiocb *iocb, |
Al Viro | d8d3d94 | 2014-03-04 21:27:34 -0500 | [diff] [blame] | 8121 | struct iov_iter *iter, loff_t offset) |
Chris Mason | 1643298 | 2008-04-10 10:23:21 -0400 | [diff] [blame] | 8122 | { |
Josef Bacik | 4b46fce | 2010-05-23 11:00:55 -0400 | [diff] [blame] | 8123 | struct file *file = iocb->ki_filp; |
| 8124 | struct inode *inode = file->f_mapping->host; |
Josef Bacik | e1cbbfa | 2015-03-17 10:52:28 -0400 | [diff] [blame^] | 8125 | u64 outstanding_extents = 0; |
Miao Xie | 0934856 | 2013-02-07 10:12:07 +0000 | [diff] [blame] | 8126 | size_t count = 0; |
Miao Xie | 2e60a51 | 2013-02-08 07:01:08 +0000 | [diff] [blame] | 8127 | int flags = 0; |
Miao Xie | 38851cc | 2013-02-08 07:04:11 +0000 | [diff] [blame] | 8128 | bool wakeup = true; |
| 8129 | bool relock = false; |
Miao Xie | 0934856 | 2013-02-07 10:12:07 +0000 | [diff] [blame] | 8130 | ssize_t ret; |
Josef Bacik | 4b46fce | 2010-05-23 11:00:55 -0400 | [diff] [blame] | 8131 | |
Al Viro | 28060d5 | 2014-03-22 05:15:17 -0400 | [diff] [blame] | 8132 | if (check_direct_IO(BTRFS_I(inode)->root, rw, iocb, iter, offset)) |
Chris Mason | 5a5f79b | 2010-05-26 21:33:37 -0400 | [diff] [blame] | 8133 | return 0; |
Chris Mason | 5a5f79b | 2010-05-26 21:33:37 -0400 | [diff] [blame] | 8134 | |
Miao Xie | 38851cc | 2013-02-08 07:04:11 +0000 | [diff] [blame] | 8135 | atomic_inc(&inode->i_dio_count); |
Peter Zijlstra | 4e857c5 | 2014-03-17 18:06:10 +0100 | [diff] [blame] | 8136 | smp_mb__after_atomic(); |
Miao Xie | 38851cc | 2013-02-08 07:04:11 +0000 | [diff] [blame] | 8137 | |
Josef Bacik | 0e267c4 | 2013-07-02 10:38:02 -0400 | [diff] [blame] | 8138 | /* |
Miao Xie | 41bd9ca | 2014-03-06 13:54:57 +0800 | [diff] [blame] | 8139 | * The generic stuff only does filemap_write_and_wait_range, which |
| 8140 | * isn't enough if we've written compressed pages to this area, so |
| 8141 | * we need to flush the dirty pages again to make absolutely sure |
| 8142 | * that any outstanding dirty pages are on disk. |
Josef Bacik | 0e267c4 | 2013-07-02 10:38:02 -0400 | [diff] [blame] | 8143 | */ |
Al Viro | a6cbcd4 | 2014-03-04 22:38:00 -0500 | [diff] [blame] | 8144 | count = iov_iter_count(iter); |
Miao Xie | 41bd9ca | 2014-03-06 13:54:57 +0800 | [diff] [blame] | 8145 | if (test_bit(BTRFS_INODE_HAS_ASYNC_EXTENT, |
| 8146 | &BTRFS_I(inode)->runtime_flags)) |
Wang Shilong | 9a025a0 | 2014-07-17 11:44:13 +0800 | [diff] [blame] | 8147 | filemap_fdatawrite_range(inode->i_mapping, offset, |
| 8148 | offset + count - 1); |
Josef Bacik | 0e267c4 | 2013-07-02 10:38:02 -0400 | [diff] [blame] | 8149 | |
Miao Xie | 0934856 | 2013-02-07 10:12:07 +0000 | [diff] [blame] | 8150 | if (rw & WRITE) { |
Miao Xie | 38851cc | 2013-02-08 07:04:11 +0000 | [diff] [blame] | 8151 | /* |
| 8152 | * If the write DIO is beyond the EOF, we need update |
| 8153 | * the isize, but it is protected by i_mutex. So we can |
| 8154 | * not unlock the i_mutex at this case. |
| 8155 | */ |
| 8156 | if (offset + count <= inode->i_size) { |
| 8157 | mutex_unlock(&inode->i_mutex); |
| 8158 | relock = true; |
| 8159 | } |
Miao Xie | 0934856 | 2013-02-07 10:12:07 +0000 | [diff] [blame] | 8160 | ret = btrfs_delalloc_reserve_space(inode, count); |
| 8161 | if (ret) |
Miao Xie | 38851cc | 2013-02-08 07:04:11 +0000 | [diff] [blame] | 8162 | goto out; |
Josef Bacik | e1cbbfa | 2015-03-17 10:52:28 -0400 | [diff] [blame^] | 8163 | outstanding_extents = div64_u64(count + |
| 8164 | BTRFS_MAX_EXTENT_SIZE - 1, |
| 8165 | BTRFS_MAX_EXTENT_SIZE); |
| 8166 | |
| 8167 | /* |
| 8168 | * We need to know how many extents we reserved so that we can |
| 8169 | * do the accounting properly if we go over the number we |
| 8170 | * originally calculated. Abuse current->journal_info for this. |
| 8171 | */ |
| 8172 | current->journal_info = &outstanding_extents; |
David Sterba | ee39b43 | 2014-09-30 01:33:33 +0200 | [diff] [blame] | 8173 | } else if (test_bit(BTRFS_INODE_READDIO_NEED_LOCK, |
| 8174 | &BTRFS_I(inode)->runtime_flags)) { |
Miao Xie | 38851cc | 2013-02-08 07:04:11 +0000 | [diff] [blame] | 8175 | inode_dio_done(inode); |
| 8176 | flags = DIO_LOCKING | DIO_SKIP_HOLES; |
| 8177 | wakeup = false; |
Miao Xie | 0934856 | 2013-02-07 10:12:07 +0000 | [diff] [blame] | 8178 | } |
| 8179 | |
| 8180 | ret = __blockdev_direct_IO(rw, iocb, inode, |
| 8181 | BTRFS_I(inode)->root->fs_info->fs_devices->latest_bdev, |
Al Viro | 31b1403 | 2014-03-05 01:33:16 -0500 | [diff] [blame] | 8182 | iter, offset, btrfs_get_blocks_direct, NULL, |
Miao Xie | 2e60a51 | 2013-02-08 07:01:08 +0000 | [diff] [blame] | 8183 | btrfs_submit_direct, flags); |
Miao Xie | 0934856 | 2013-02-07 10:12:07 +0000 | [diff] [blame] | 8184 | if (rw & WRITE) { |
Josef Bacik | e1cbbfa | 2015-03-17 10:52:28 -0400 | [diff] [blame^] | 8185 | current->journal_info = NULL; |
Miao Xie | 0934856 | 2013-02-07 10:12:07 +0000 | [diff] [blame] | 8186 | if (ret < 0 && ret != -EIOCBQUEUED) |
| 8187 | btrfs_delalloc_release_space(inode, count); |
Miao Xie | 172a504 | 2013-02-21 02:48:22 -0700 | [diff] [blame] | 8188 | else if (ret >= 0 && (size_t)ret < count) |
Miao Xie | 0934856 | 2013-02-07 10:12:07 +0000 | [diff] [blame] | 8189 | btrfs_delalloc_release_space(inode, |
| 8190 | count - (size_t)ret); |
Miao Xie | 0934856 | 2013-02-07 10:12:07 +0000 | [diff] [blame] | 8191 | } |
Miao Xie | 38851cc | 2013-02-08 07:04:11 +0000 | [diff] [blame] | 8192 | out: |
Miao Xie | 2e60a51 | 2013-02-08 07:01:08 +0000 | [diff] [blame] | 8193 | if (wakeup) |
| 8194 | inode_dio_done(inode); |
Miao Xie | 38851cc | 2013-02-08 07:04:11 +0000 | [diff] [blame] | 8195 | if (relock) |
| 8196 | mutex_lock(&inode->i_mutex); |
Miao Xie | 0934856 | 2013-02-07 10:12:07 +0000 | [diff] [blame] | 8197 | |
| 8198 | return ret; |
Chris Mason | 1643298 | 2008-04-10 10:23:21 -0400 | [diff] [blame] | 8199 | } |
| 8200 | |
Tsutomu Itoh | 05dadc0 | 2012-11-29 05:08:26 +0000 | [diff] [blame] | 8201 | #define BTRFS_FIEMAP_FLAGS (FIEMAP_FLAG_SYNC) |
| 8202 | |
Yehuda Sadeh | 1506fcc | 2009-01-21 14:39:14 -0500 | [diff] [blame] | 8203 | static int btrfs_fiemap(struct inode *inode, struct fiemap_extent_info *fieinfo, |
| 8204 | __u64 start, __u64 len) |
| 8205 | { |
Tsutomu Itoh | 05dadc0 | 2012-11-29 05:08:26 +0000 | [diff] [blame] | 8206 | int ret; |
| 8207 | |
| 8208 | ret = fiemap_check_flags(fieinfo, BTRFS_FIEMAP_FLAGS); |
| 8209 | if (ret) |
| 8210 | return ret; |
| 8211 | |
Chris Mason | ec29ed5 | 2011-02-23 16:23:20 -0500 | [diff] [blame] | 8212 | return extent_fiemap(inode, fieinfo, start, len, btrfs_get_extent_fiemap); |
Yehuda Sadeh | 1506fcc | 2009-01-21 14:39:14 -0500 | [diff] [blame] | 8213 | } |
| 8214 | |
Chris Mason | 9ebefb18 | 2007-06-15 13:50:00 -0400 | [diff] [blame] | 8215 | int btrfs_readpage(struct file *file, struct page *page) |
| 8216 | { |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 8217 | struct extent_io_tree *tree; |
| 8218 | tree = &BTRFS_I(page->mapping->host)->io_tree; |
Jan Schmidt | 8ddc7d9 | 2011-06-13 20:02:58 +0200 | [diff] [blame] | 8219 | return extent_read_full_page(tree, page, btrfs_get_extent, 0); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 8220 | } |
Chris Mason | 1832a6d | 2007-12-21 16:27:21 -0500 | [diff] [blame] | 8221 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 8222 | static int btrfs_writepage(struct page *page, struct writeback_control *wbc) |
| 8223 | { |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 8224 | struct extent_io_tree *tree; |
Chris Mason | b888db2 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 8225 | |
| 8226 | |
| 8227 | if (current->flags & PF_MEMALLOC) { |
| 8228 | redirty_page_for_writepage(wbc, page); |
| 8229 | unlock_page(page); |
| 8230 | return 0; |
| 8231 | } |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 8232 | tree = &BTRFS_I(page->mapping->host)->io_tree; |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 8233 | return extent_write_full_page(tree, page, btrfs_get_extent, wbc); |
| 8234 | } |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 8235 | |
Eric Sandeen | 48a3b63 | 2013-04-25 20:41:01 +0000 | [diff] [blame] | 8236 | static int btrfs_writepages(struct address_space *mapping, |
| 8237 | struct writeback_control *wbc) |
Chris Mason | b293f02 | 2007-11-01 19:45:34 -0400 | [diff] [blame] | 8238 | { |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 8239 | struct extent_io_tree *tree; |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 8240 | |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 8241 | tree = &BTRFS_I(mapping->host)->io_tree; |
Chris Mason | b293f02 | 2007-11-01 19:45:34 -0400 | [diff] [blame] | 8242 | return extent_writepages(tree, mapping, btrfs_get_extent, wbc); |
| 8243 | } |
| 8244 | |
Chris Mason | 3ab2fb5 | 2007-11-08 10:59:22 -0500 | [diff] [blame] | 8245 | static int |
| 8246 | btrfs_readpages(struct file *file, struct address_space *mapping, |
| 8247 | struct list_head *pages, unsigned nr_pages) |
| 8248 | { |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 8249 | struct extent_io_tree *tree; |
| 8250 | tree = &BTRFS_I(mapping->host)->io_tree; |
Chris Mason | 3ab2fb5 | 2007-11-08 10:59:22 -0500 | [diff] [blame] | 8251 | return extent_readpages(tree, mapping, pages, nr_pages, |
| 8252 | btrfs_get_extent); |
| 8253 | } |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 8254 | static int __btrfs_releasepage(struct page *page, gfp_t gfp_flags) |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 8255 | { |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 8256 | struct extent_io_tree *tree; |
| 8257 | struct extent_map_tree *map; |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 8258 | int ret; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 8259 | |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 8260 | tree = &BTRFS_I(page->mapping->host)->io_tree; |
| 8261 | map = &BTRFS_I(page->mapping->host)->extent_tree; |
Chris Mason | 70dec80 | 2008-01-29 09:59:12 -0500 | [diff] [blame] | 8262 | ret = try_release_extent_mapping(map, tree, page, gfp_flags); |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 8263 | if (ret == 1) { |
| 8264 | ClearPagePrivate(page); |
| 8265 | set_page_private(page, 0); |
| 8266 | page_cache_release(page); |
| 8267 | } |
| 8268 | return ret; |
| 8269 | } |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 8270 | |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 8271 | static int btrfs_releasepage(struct page *page, gfp_t gfp_flags) |
| 8272 | { |
Chris Mason | 98509cf | 2008-09-11 15:51:43 -0400 | [diff] [blame] | 8273 | if (PageWriteback(page) || PageDirty(page)) |
| 8274 | return 0; |
Yan Zheng | b335b00 | 2009-02-12 10:06:04 -0500 | [diff] [blame] | 8275 | return __btrfs_releasepage(page, gfp_flags & GFP_NOFS); |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 8276 | } |
| 8277 | |
Lukas Czerner | d47992f | 2013-05-21 23:17:23 -0400 | [diff] [blame] | 8278 | static void btrfs_invalidatepage(struct page *page, unsigned int offset, |
| 8279 | unsigned int length) |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 8280 | { |
Josef Bacik | 5fd0204 | 2012-05-02 14:00:54 -0400 | [diff] [blame] | 8281 | struct inode *inode = page->mapping->host; |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 8282 | struct extent_io_tree *tree; |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 8283 | struct btrfs_ordered_extent *ordered; |
Josef Bacik | 2ac55d4 | 2010-02-03 19:33:23 +0000 | [diff] [blame] | 8284 | struct extent_state *cached_state = NULL; |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 8285 | u64 page_start = page_offset(page); |
| 8286 | u64 page_end = page_start + PAGE_CACHE_SIZE - 1; |
Filipe David Borba Manana | 131e404 | 2013-11-19 22:29:35 +0000 | [diff] [blame] | 8287 | int inode_evicting = inode->i_state & I_FREEING; |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 8288 | |
Chris Mason | 8b62b72 | 2009-09-02 16:53:46 -0400 | [diff] [blame] | 8289 | /* |
| 8290 | * we have the page locked, so new writeback can't start, |
| 8291 | * and the dirty bit won't be cleared while we are here. |
| 8292 | * |
| 8293 | * Wait for IO on this page so that we can safely clear |
| 8294 | * the PagePrivate2 bit and do ordered accounting |
| 8295 | */ |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 8296 | wait_on_page_writeback(page); |
Chris Mason | 8b62b72 | 2009-09-02 16:53:46 -0400 | [diff] [blame] | 8297 | |
Josef Bacik | 5fd0204 | 2012-05-02 14:00:54 -0400 | [diff] [blame] | 8298 | tree = &BTRFS_I(inode)->io_tree; |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 8299 | if (offset) { |
| 8300 | btrfs_releasepage(page, GFP_NOFS); |
| 8301 | return; |
| 8302 | } |
Filipe David Borba Manana | 131e404 | 2013-11-19 22:29:35 +0000 | [diff] [blame] | 8303 | |
| 8304 | if (!inode_evicting) |
| 8305 | lock_extent_bits(tree, page_start, page_end, 0, &cached_state); |
| 8306 | ordered = btrfs_lookup_ordered_extent(inode, page_start); |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 8307 | if (ordered) { |
Chris Mason | eb84ae0 | 2008-07-17 13:53:27 -0400 | [diff] [blame] | 8308 | /* |
| 8309 | * IO on this page will never be started, so we need |
| 8310 | * to account for any ordered extents now |
| 8311 | */ |
Filipe David Borba Manana | 131e404 | 2013-11-19 22:29:35 +0000 | [diff] [blame] | 8312 | if (!inode_evicting) |
| 8313 | clear_extent_bit(tree, page_start, page_end, |
| 8314 | EXTENT_DIRTY | EXTENT_DELALLOC | |
| 8315 | EXTENT_LOCKED | EXTENT_DO_ACCOUNTING | |
| 8316 | EXTENT_DEFRAG, 1, 0, &cached_state, |
| 8317 | GFP_NOFS); |
Chris Mason | 8b62b72 | 2009-09-02 16:53:46 -0400 | [diff] [blame] | 8318 | /* |
| 8319 | * whoever cleared the private bit is responsible |
| 8320 | * for the finish_ordered_io |
| 8321 | */ |
Josef Bacik | 77cef2e | 2013-08-29 13:57:21 -0400 | [diff] [blame] | 8322 | if (TestClearPagePrivate2(page)) { |
| 8323 | struct btrfs_ordered_inode_tree *tree; |
| 8324 | u64 new_len; |
| 8325 | |
| 8326 | tree = &BTRFS_I(inode)->ordered_tree; |
| 8327 | |
| 8328 | spin_lock_irq(&tree->lock); |
| 8329 | set_bit(BTRFS_ORDERED_TRUNCATED, &ordered->flags); |
| 8330 | new_len = page_start - ordered->file_offset; |
| 8331 | if (new_len < ordered->truncated_len) |
| 8332 | ordered->truncated_len = new_len; |
| 8333 | spin_unlock_irq(&tree->lock); |
| 8334 | |
| 8335 | if (btrfs_dec_test_ordered_pending(inode, &ordered, |
| 8336 | page_start, |
| 8337 | PAGE_CACHE_SIZE, 1)) |
| 8338 | btrfs_finish_ordered_io(ordered); |
Chris Mason | 8b62b72 | 2009-09-02 16:53:46 -0400 | [diff] [blame] | 8339 | } |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 8340 | btrfs_put_ordered_extent(ordered); |
Filipe David Borba Manana | 131e404 | 2013-11-19 22:29:35 +0000 | [diff] [blame] | 8341 | if (!inode_evicting) { |
| 8342 | cached_state = NULL; |
| 8343 | lock_extent_bits(tree, page_start, page_end, 0, |
| 8344 | &cached_state); |
| 8345 | } |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 8346 | } |
Filipe David Borba Manana | 131e404 | 2013-11-19 22:29:35 +0000 | [diff] [blame] | 8347 | |
| 8348 | if (!inode_evicting) { |
| 8349 | clear_extent_bit(tree, page_start, page_end, |
| 8350 | EXTENT_LOCKED | EXTENT_DIRTY | |
| 8351 | EXTENT_DELALLOC | EXTENT_DO_ACCOUNTING | |
| 8352 | EXTENT_DEFRAG, 1, 1, |
| 8353 | &cached_state, GFP_NOFS); |
| 8354 | |
| 8355 | __btrfs_releasepage(page, GFP_NOFS); |
| 8356 | } |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 8357 | |
Chris Mason | 4a09675 | 2008-07-21 10:29:44 -0400 | [diff] [blame] | 8358 | ClearPageChecked(page); |
Chris Mason | 9ad6b7b | 2008-04-18 16:11:30 -0400 | [diff] [blame] | 8359 | if (PagePrivate(page)) { |
Chris Mason | 9ad6b7b | 2008-04-18 16:11:30 -0400 | [diff] [blame] | 8360 | ClearPagePrivate(page); |
| 8361 | set_page_private(page, 0); |
| 8362 | page_cache_release(page); |
| 8363 | } |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 8364 | } |
| 8365 | |
Chris Mason | 9ebefb18 | 2007-06-15 13:50:00 -0400 | [diff] [blame] | 8366 | /* |
| 8367 | * btrfs_page_mkwrite() is not allowed to change the file size as it gets |
| 8368 | * called from a page fault handler when a page is first dirtied. Hence we must |
| 8369 | * be careful to check for EOF conditions here. We set the page up correctly |
| 8370 | * for a written page which means we get ENOSPC checking when writing into |
| 8371 | * holes and correct delalloc and unwritten extent mapping on filesystems that |
| 8372 | * support these features. |
| 8373 | * |
| 8374 | * We are not allowed to take the i_mutex here so we have to play games to |
| 8375 | * protect against truncate races as the page could now be beyond EOF. Because |
| 8376 | * vmtruncate() writes the inode size before removing pages, once we have the |
| 8377 | * page lock we can determine safely if the page is beyond EOF. If it is not |
| 8378 | * beyond EOF, then the page is guaranteed safe against truncation until we |
| 8379 | * unlock the page. |
| 8380 | */ |
Nick Piggin | c2ec175 | 2009-03-31 15:23:21 -0700 | [diff] [blame] | 8381 | 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] | 8382 | { |
Nick Piggin | c2ec175 | 2009-03-31 15:23:21 -0700 | [diff] [blame] | 8383 | struct page *page = vmf->page; |
Al Viro | 496ad9a | 2013-01-23 17:07:38 -0500 | [diff] [blame] | 8384 | struct inode *inode = file_inode(vma->vm_file); |
Chris Mason | 1832a6d | 2007-12-21 16:27:21 -0500 | [diff] [blame] | 8385 | struct btrfs_root *root = BTRFS_I(inode)->root; |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 8386 | struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree; |
| 8387 | struct btrfs_ordered_extent *ordered; |
Josef Bacik | 2ac55d4 | 2010-02-03 19:33:23 +0000 | [diff] [blame] | 8388 | struct extent_state *cached_state = NULL; |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 8389 | char *kaddr; |
| 8390 | unsigned long zero_start; |
Chris Mason | 9ebefb18 | 2007-06-15 13:50:00 -0400 | [diff] [blame] | 8391 | loff_t size; |
Chris Mason | 1832a6d | 2007-12-21 16:27:21 -0500 | [diff] [blame] | 8392 | int ret; |
Chris Mason | 9998eb7 | 2012-01-25 13:47:40 -0500 | [diff] [blame] | 8393 | int reserved = 0; |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 8394 | u64 page_start; |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 8395 | u64 page_end; |
Chris Mason | 9ebefb18 | 2007-06-15 13:50:00 -0400 | [diff] [blame] | 8396 | |
Jan Kara | b2b5ef5 | 2012-06-12 16:20:45 +0200 | [diff] [blame] | 8397 | sb_start_pagefault(inode->i_sb); |
Yan, Zheng | 0ca1f7c | 2010-05-16 10:48:47 -0400 | [diff] [blame] | 8398 | ret = btrfs_delalloc_reserve_space(inode, PAGE_CACHE_SIZE); |
Chris Mason | 9998eb7 | 2012-01-25 13:47:40 -0500 | [diff] [blame] | 8399 | if (!ret) { |
Josef Bacik | e41f941 | 2012-03-26 09:46:47 -0400 | [diff] [blame] | 8400 | ret = file_update_time(vma->vm_file); |
Chris Mason | 9998eb7 | 2012-01-25 13:47:40 -0500 | [diff] [blame] | 8401 | reserved = 1; |
| 8402 | } |
Nick Piggin | 56a76f8 | 2009-03-31 15:23:23 -0700 | [diff] [blame] | 8403 | if (ret) { |
| 8404 | if (ret == -ENOMEM) |
| 8405 | ret = VM_FAULT_OOM; |
| 8406 | else /* -ENOSPC, -EIO, etc */ |
| 8407 | ret = VM_FAULT_SIGBUS; |
Chris Mason | 9998eb7 | 2012-01-25 13:47:40 -0500 | [diff] [blame] | 8408 | if (reserved) |
| 8409 | goto out; |
| 8410 | goto out_noreserve; |
Nick Piggin | 56a76f8 | 2009-03-31 15:23:23 -0700 | [diff] [blame] | 8411 | } |
Chris Mason | 1832a6d | 2007-12-21 16:27:21 -0500 | [diff] [blame] | 8412 | |
Nick Piggin | 56a76f8 | 2009-03-31 15:23:23 -0700 | [diff] [blame] | 8413 | ret = VM_FAULT_NOPAGE; /* make the VM retry the fault */ |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 8414 | again: |
Chris Mason | 9ebefb18 | 2007-06-15 13:50:00 -0400 | [diff] [blame] | 8415 | lock_page(page); |
Chris Mason | 9ebefb18 | 2007-06-15 13:50:00 -0400 | [diff] [blame] | 8416 | size = i_size_read(inode); |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 8417 | page_start = page_offset(page); |
| 8418 | page_end = page_start + PAGE_CACHE_SIZE - 1; |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 8419 | |
Chris Mason | 9ebefb18 | 2007-06-15 13:50:00 -0400 | [diff] [blame] | 8420 | if ((page->mapping != inode->i_mapping) || |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 8421 | (page_start >= size)) { |
Chris Mason | 9ebefb18 | 2007-06-15 13:50:00 -0400 | [diff] [blame] | 8422 | /* page got truncated out from underneath us */ |
| 8423 | goto out_unlock; |
| 8424 | } |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 8425 | wait_on_page_writeback(page); |
| 8426 | |
Jeff Mahoney | d008237 | 2012-03-01 14:57:19 +0100 | [diff] [blame] | 8427 | lock_extent_bits(io_tree, page_start, page_end, 0, &cached_state); |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 8428 | set_page_extent_mapped(page); |
| 8429 | |
Chris Mason | eb84ae0 | 2008-07-17 13:53:27 -0400 | [diff] [blame] | 8430 | /* |
| 8431 | * we can't set the delalloc bits if there are pending ordered |
| 8432 | * extents. Drop our locks and wait for them to finish |
| 8433 | */ |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 8434 | ordered = btrfs_lookup_ordered_extent(inode, page_start); |
| 8435 | if (ordered) { |
Josef Bacik | 2ac55d4 | 2010-02-03 19:33:23 +0000 | [diff] [blame] | 8436 | unlock_extent_cached(io_tree, page_start, page_end, |
| 8437 | &cached_state, GFP_NOFS); |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 8438 | unlock_page(page); |
Chris Mason | eb84ae0 | 2008-07-17 13:53:27 -0400 | [diff] [blame] | 8439 | btrfs_start_ordered_extent(inode, ordered, 1); |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 8440 | btrfs_put_ordered_extent(ordered); |
| 8441 | goto again; |
| 8442 | } |
| 8443 | |
Josef Bacik | fbf1908 | 2009-10-01 17:10:23 -0400 | [diff] [blame] | 8444 | /* |
| 8445 | * XXX - page_mkwrite gets called every time the page is dirtied, even |
| 8446 | * if it was already dirty, so for space accounting reasons we need to |
| 8447 | * clear any delalloc bits for the range we are fixing to save. There |
| 8448 | * is probably a better way to do this, but for now keep consistent with |
| 8449 | * prepare_pages in the normal write path. |
| 8450 | */ |
Josef Bacik | 2ac55d4 | 2010-02-03 19:33:23 +0000 | [diff] [blame] | 8451 | clear_extent_bit(&BTRFS_I(inode)->io_tree, page_start, page_end, |
Liu Bo | 9e8a4a8 | 2012-09-05 19:10:51 -0600 | [diff] [blame] | 8452 | EXTENT_DIRTY | EXTENT_DELALLOC | |
| 8453 | EXTENT_DO_ACCOUNTING | EXTENT_DEFRAG, |
Josef Bacik | 2ac55d4 | 2010-02-03 19:33:23 +0000 | [diff] [blame] | 8454 | 0, 0, &cached_state, GFP_NOFS); |
Josef Bacik | fbf1908 | 2009-10-01 17:10:23 -0400 | [diff] [blame] | 8455 | |
Josef Bacik | 2ac55d4 | 2010-02-03 19:33:23 +0000 | [diff] [blame] | 8456 | ret = btrfs_set_extent_delalloc(inode, page_start, page_end, |
| 8457 | &cached_state); |
Josef Bacik | 9ed74f2 | 2009-09-11 16:12:44 -0400 | [diff] [blame] | 8458 | if (ret) { |
Josef Bacik | 2ac55d4 | 2010-02-03 19:33:23 +0000 | [diff] [blame] | 8459 | unlock_extent_cached(io_tree, page_start, page_end, |
| 8460 | &cached_state, GFP_NOFS); |
Josef Bacik | 9ed74f2 | 2009-09-11 16:12:44 -0400 | [diff] [blame] | 8461 | ret = VM_FAULT_SIGBUS; |
| 8462 | goto out_unlock; |
| 8463 | } |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 8464 | ret = 0; |
Chris Mason | 9ebefb18 | 2007-06-15 13:50:00 -0400 | [diff] [blame] | 8465 | |
| 8466 | /* page is wholly or partially inside EOF */ |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 8467 | if (page_start + PAGE_CACHE_SIZE > size) |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 8468 | zero_start = size & ~PAGE_CACHE_MASK; |
Chris Mason | 9ebefb18 | 2007-06-15 13:50:00 -0400 | [diff] [blame] | 8469 | else |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 8470 | zero_start = PAGE_CACHE_SIZE; |
Chris Mason | 9ebefb18 | 2007-06-15 13:50:00 -0400 | [diff] [blame] | 8471 | |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 8472 | if (zero_start != PAGE_CACHE_SIZE) { |
| 8473 | kaddr = kmap(page); |
| 8474 | memset(kaddr + zero_start, 0, PAGE_CACHE_SIZE - zero_start); |
| 8475 | flush_dcache_page(page); |
| 8476 | kunmap(page); |
| 8477 | } |
Chris Mason | 247e743 | 2008-07-17 12:53:51 -0400 | [diff] [blame] | 8478 | ClearPageChecked(page); |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 8479 | set_page_dirty(page); |
Chris Mason | 50a9b21 | 2009-09-11 12:33:12 -0400 | [diff] [blame] | 8480 | SetPageUptodate(page); |
Chris Mason | 5a3f23d | 2009-03-31 13:27:11 -0400 | [diff] [blame] | 8481 | |
Chris Mason | 257c62e | 2009-10-13 13:21:08 -0400 | [diff] [blame] | 8482 | BTRFS_I(inode)->last_trans = root->fs_info->generation; |
| 8483 | BTRFS_I(inode)->last_sub_trans = BTRFS_I(inode)->root->log_transid; |
Liu Bo | 46d8bc3 | 2012-08-29 01:07:55 -0600 | [diff] [blame] | 8484 | 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] | 8485 | |
Josef Bacik | 2ac55d4 | 2010-02-03 19:33:23 +0000 | [diff] [blame] | 8486 | 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] | 8487 | |
| 8488 | out_unlock: |
Jan Kara | b2b5ef5 | 2012-06-12 16:20:45 +0200 | [diff] [blame] | 8489 | if (!ret) { |
| 8490 | sb_end_pagefault(inode->i_sb); |
Chris Mason | 50a9b21 | 2009-09-11 12:33:12 -0400 | [diff] [blame] | 8491 | return VM_FAULT_LOCKED; |
Jan Kara | b2b5ef5 | 2012-06-12 16:20:45 +0200 | [diff] [blame] | 8492 | } |
Chris Mason | 9ebefb18 | 2007-06-15 13:50:00 -0400 | [diff] [blame] | 8493 | unlock_page(page); |
Chris Mason | 1832a6d | 2007-12-21 16:27:21 -0500 | [diff] [blame] | 8494 | out: |
Josef Bacik | ec39e18 | 2012-01-12 19:10:12 -0500 | [diff] [blame] | 8495 | btrfs_delalloc_release_space(inode, PAGE_CACHE_SIZE); |
Chris Mason | 9998eb7 | 2012-01-25 13:47:40 -0500 | [diff] [blame] | 8496 | out_noreserve: |
Jan Kara | b2b5ef5 | 2012-06-12 16:20:45 +0200 | [diff] [blame] | 8497 | sb_end_pagefault(inode->i_sb); |
Chris Mason | 9ebefb18 | 2007-06-15 13:50:00 -0400 | [diff] [blame] | 8498 | return ret; |
| 8499 | } |
| 8500 | |
Josef Bacik | a41ad39 | 2011-01-31 15:30:16 -0500 | [diff] [blame] | 8501 | static int btrfs_truncate(struct inode *inode) |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 8502 | { |
| 8503 | struct btrfs_root *root = BTRFS_I(inode)->root; |
Josef Bacik | fcb80c2 | 2011-05-03 10:40:22 -0400 | [diff] [blame] | 8504 | struct btrfs_block_rsv *rsv; |
Josef Bacik | a71754f | 2013-06-17 17:14:39 -0400 | [diff] [blame] | 8505 | int ret = 0; |
Josef Bacik | 3893e33 | 2011-01-31 16:03:11 -0500 | [diff] [blame] | 8506 | int err = 0; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 8507 | struct btrfs_trans_handle *trans; |
Chris Mason | dbe674a | 2008-07-17 12:54:05 -0400 | [diff] [blame] | 8508 | u64 mask = root->sectorsize - 1; |
Josef Bacik | 0712718 | 2011-08-19 10:29:59 -0400 | [diff] [blame] | 8509 | u64 min_size = btrfs_calc_trunc_metadata_size(root, 1); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 8510 | |
Josef Bacik | 0ef8b72 | 2013-10-25 16:13:35 -0400 | [diff] [blame] | 8511 | ret = btrfs_wait_ordered_range(inode, inode->i_size & (~mask), |
| 8512 | (u64)-1); |
| 8513 | if (ret) |
| 8514 | return ret; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 8515 | |
Josef Bacik | fcb80c2 | 2011-05-03 10:40:22 -0400 | [diff] [blame] | 8516 | /* |
| 8517 | * Yes ladies and gentelment, this is indeed ugly. The fact is we have |
| 8518 | * 3 things going on here |
| 8519 | * |
| 8520 | * 1) We need to reserve space for our orphan item and the space to |
| 8521 | * delete our orphan item. Lord knows we don't want to have a dangling |
| 8522 | * orphan item because we didn't reserve space to remove it. |
| 8523 | * |
| 8524 | * 2) We need to reserve space to update our inode. |
| 8525 | * |
| 8526 | * 3) We need to have something to cache all the space that is going to |
| 8527 | * be free'd up by the truncate operation, but also have some slack |
| 8528 | * space reserved in case it uses space during the truncate (thank you |
| 8529 | * very much snapshotting). |
| 8530 | * |
| 8531 | * And we need these to all be seperate. The fact is we can use alot of |
| 8532 | * space doing the truncate, and we have no earthly idea how much space |
| 8533 | * we will use, so we need the truncate reservation to be seperate so it |
| 8534 | * doesn't end up using space reserved for updating the inode or |
| 8535 | * removing the orphan item. We also need to be able to stop the |
| 8536 | * transaction and start a new one, which means we need to be able to |
| 8537 | * update the inode several times, and we have no idea of knowing how |
| 8538 | * many times that will be, so we can't just reserve 1 item for the |
| 8539 | * entirety of the opration, so that has to be done seperately as well. |
| 8540 | * Then there is the orphan item, which does indeed need to be held on |
| 8541 | * to for the whole operation, and we need nobody to touch this reserved |
| 8542 | * space except the orphan code. |
| 8543 | * |
| 8544 | * So that leaves us with |
| 8545 | * |
| 8546 | * 1) root->orphan_block_rsv - for the orphan deletion. |
| 8547 | * 2) rsv - for the truncate reservation, which we will steal from the |
| 8548 | * transaction reservation. |
| 8549 | * 3) fs_info->trans_block_rsv - this will have 1 items worth left for |
| 8550 | * updating the inode. |
| 8551 | */ |
Miao Xie | 66d8f3d | 2012-09-06 04:02:28 -0600 | [diff] [blame] | 8552 | rsv = btrfs_alloc_block_rsv(root, BTRFS_BLOCK_RSV_TEMP); |
Josef Bacik | fcb80c2 | 2011-05-03 10:40:22 -0400 | [diff] [blame] | 8553 | if (!rsv) |
| 8554 | return -ENOMEM; |
Josef Bacik | 4a33854 | 2011-08-29 11:01:31 -0400 | [diff] [blame] | 8555 | rsv->size = min_size; |
Josef Bacik | ca7e70f | 2012-08-27 17:48:15 -0400 | [diff] [blame] | 8556 | rsv->failfast = 1; |
Josef Bacik | f0cd846 | 2011-03-04 14:37:08 -0500 | [diff] [blame] | 8557 | |
Josef Bacik | 907cbce | 2011-08-08 13:46:15 -0400 | [diff] [blame] | 8558 | /* |
Josef Bacik | 0712718 | 2011-08-19 10:29:59 -0400 | [diff] [blame] | 8559 | * 1 for the truncate slack space |
Josef Bacik | 907cbce | 2011-08-08 13:46:15 -0400 | [diff] [blame] | 8560 | * 1 for updating the inode. |
| 8561 | */ |
Josef Bacik | f3fe820 | 2013-01-07 17:03:21 -0500 | [diff] [blame] | 8562 | trans = btrfs_start_transaction(root, 2); |
Josef Bacik | fcb80c2 | 2011-05-03 10:40:22 -0400 | [diff] [blame] | 8563 | if (IS_ERR(trans)) { |
| 8564 | err = PTR_ERR(trans); |
| 8565 | goto out; |
| 8566 | } |
Josef Bacik | f0cd846 | 2011-03-04 14:37:08 -0500 | [diff] [blame] | 8567 | |
Josef Bacik | 907cbce | 2011-08-08 13:46:15 -0400 | [diff] [blame] | 8568 | /* Migrate the slack space for the truncate to our reserve */ |
| 8569 | ret = btrfs_block_rsv_migrate(&root->fs_info->trans_block_rsv, rsv, |
| 8570 | min_size); |
Josef Bacik | fcb80c2 | 2011-05-03 10:40:22 -0400 | [diff] [blame] | 8571 | BUG_ON(ret); |
Josef Bacik | f0cd846 | 2011-03-04 14:37:08 -0500 | [diff] [blame] | 8572 | |
Chris Mason | 5a3f23d | 2009-03-31 13:27:11 -0400 | [diff] [blame] | 8573 | /* |
Josef Bacik | 5dc562c | 2012-08-17 13:14:17 -0400 | [diff] [blame] | 8574 | * So if we truncate and then write and fsync we normally would just |
| 8575 | * write the extents that changed, which is a problem if we need to |
| 8576 | * first truncate that entire inode. So set this flag so we write out |
| 8577 | * all of the extents in the inode to the sync log so we're completely |
| 8578 | * safe. |
| 8579 | */ |
| 8580 | set_bit(BTRFS_INODE_NEEDS_FULL_SYNC, &BTRFS_I(inode)->runtime_flags); |
Josef Bacik | ca7e70f | 2012-08-27 17:48:15 -0400 | [diff] [blame] | 8581 | trans->block_rsv = rsv; |
Josef Bacik | 5dc562c | 2012-08-17 13:14:17 -0400 | [diff] [blame] | 8582 | |
Yan, Zheng | 8082510 | 2009-11-12 09:35:36 +0000 | [diff] [blame] | 8583 | while (1) { |
| 8584 | ret = btrfs_truncate_inode_items(trans, root, inode, |
| 8585 | inode->i_size, |
| 8586 | BTRFS_EXTENT_DATA_KEY); |
Josef Bacik | ca7e70f | 2012-08-27 17:48:15 -0400 | [diff] [blame] | 8587 | if (ret != -ENOSPC) { |
Josef Bacik | 3893e33 | 2011-01-31 16:03:11 -0500 | [diff] [blame] | 8588 | err = ret; |
Yan, Zheng | 8082510 | 2009-11-12 09:35:36 +0000 | [diff] [blame] | 8589 | break; |
Josef Bacik | 3893e33 | 2011-01-31 16:03:11 -0500 | [diff] [blame] | 8590 | } |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 8591 | |
Josef Bacik | fcb80c2 | 2011-05-03 10:40:22 -0400 | [diff] [blame] | 8592 | trans->block_rsv = &root->fs_info->trans_block_rsv; |
Yan, Zheng | 8082510 | 2009-11-12 09:35:36 +0000 | [diff] [blame] | 8593 | ret = btrfs_update_inode(trans, root, inode); |
Josef Bacik | 3893e33 | 2011-01-31 16:03:11 -0500 | [diff] [blame] | 8594 | if (ret) { |
| 8595 | err = ret; |
| 8596 | break; |
| 8597 | } |
Josef Bacik | ca7e70f | 2012-08-27 17:48:15 -0400 | [diff] [blame] | 8598 | |
Yan, Zheng | 8082510 | 2009-11-12 09:35:36 +0000 | [diff] [blame] | 8599 | btrfs_end_transaction(trans, root); |
Liu Bo | b53d3f5 | 2012-11-14 14:34:34 +0000 | [diff] [blame] | 8600 | btrfs_btree_balance_dirty(root); |
Josef Bacik | ca7e70f | 2012-08-27 17:48:15 -0400 | [diff] [blame] | 8601 | |
| 8602 | trans = btrfs_start_transaction(root, 2); |
| 8603 | if (IS_ERR(trans)) { |
| 8604 | ret = err = PTR_ERR(trans); |
| 8605 | trans = NULL; |
| 8606 | break; |
| 8607 | } |
| 8608 | |
| 8609 | ret = btrfs_block_rsv_migrate(&root->fs_info->trans_block_rsv, |
| 8610 | rsv, min_size); |
| 8611 | BUG_ON(ret); /* shouldn't happen */ |
| 8612 | trans->block_rsv = rsv; |
Yan, Zheng | 8082510 | 2009-11-12 09:35:36 +0000 | [diff] [blame] | 8613 | } |
| 8614 | |
| 8615 | if (ret == 0 && inode->i_nlink > 0) { |
Josef Bacik | fcb80c2 | 2011-05-03 10:40:22 -0400 | [diff] [blame] | 8616 | trans->block_rsv = root->orphan_block_rsv; |
Yan, Zheng | 8082510 | 2009-11-12 09:35:36 +0000 | [diff] [blame] | 8617 | ret = btrfs_orphan_del(trans, inode); |
Josef Bacik | 3893e33 | 2011-01-31 16:03:11 -0500 | [diff] [blame] | 8618 | if (ret) |
| 8619 | err = ret; |
Yan, Zheng | 8082510 | 2009-11-12 09:35:36 +0000 | [diff] [blame] | 8620 | } |
| 8621 | |
Chris Mason | 917c16b | 2011-11-08 14:49:59 -0500 | [diff] [blame] | 8622 | if (trans) { |
| 8623 | trans->block_rsv = &root->fs_info->trans_block_rsv; |
| 8624 | ret = btrfs_update_inode(trans, root, inode); |
| 8625 | if (ret && !err) |
| 8626 | err = ret; |
Josef Bacik | 7b12876 | 2008-07-24 12:17:14 -0400 | [diff] [blame] | 8627 | |
Josef Bacik | 7ad85bb | 2012-01-12 19:10:12 -0500 | [diff] [blame] | 8628 | ret = btrfs_end_transaction(trans, root); |
Liu Bo | b53d3f5 | 2012-11-14 14:34:34 +0000 | [diff] [blame] | 8629 | btrfs_btree_balance_dirty(root); |
Chris Mason | 917c16b | 2011-11-08 14:49:59 -0500 | [diff] [blame] | 8630 | } |
Josef Bacik | fcb80c2 | 2011-05-03 10:40:22 -0400 | [diff] [blame] | 8631 | |
| 8632 | out: |
| 8633 | btrfs_free_block_rsv(root, rsv); |
| 8634 | |
Josef Bacik | 3893e33 | 2011-01-31 16:03:11 -0500 | [diff] [blame] | 8635 | if (ret && !err) |
| 8636 | err = ret; |
Josef Bacik | a41ad39 | 2011-01-31 15:30:16 -0500 | [diff] [blame] | 8637 | |
Josef Bacik | 3893e33 | 2011-01-31 16:03:11 -0500 | [diff] [blame] | 8638 | return err; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 8639 | } |
| 8640 | |
Sven Wegener | 3b96362 | 2008-06-09 21:57:42 -0400 | [diff] [blame] | 8641 | /* |
Chris Mason | d352ac6 | 2008-09-29 15:18:18 -0400 | [diff] [blame] | 8642 | * create a new subvolume directory/inode (helper for the ioctl). |
| 8643 | */ |
Yan Zheng | d2fb343 | 2008-12-11 16:30:39 -0500 | [diff] [blame] | 8644 | int btrfs_create_subvol_root(struct btrfs_trans_handle *trans, |
Filipe David Borba Manana | 6354192 | 2014-01-07 11:47:46 +0000 | [diff] [blame] | 8645 | struct btrfs_root *new_root, |
| 8646 | struct btrfs_root *parent_root, |
| 8647 | u64 new_dirid) |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 8648 | { |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 8649 | struct inode *inode; |
Yan, Zheng | 76dda93 | 2009-09-21 16:00:26 -0400 | [diff] [blame] | 8650 | int err; |
Chris Mason | 00e4e6b | 2008-08-05 11:18:09 -0400 | [diff] [blame] | 8651 | u64 index = 0; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 8652 | |
Florian Albrechtskirchinger | 12fc9d0 | 2012-02-10 22:15:54 +0100 | [diff] [blame] | 8653 | inode = btrfs_new_inode(trans, new_root, NULL, "..", 2, |
| 8654 | new_dirid, new_dirid, |
| 8655 | S_IFDIR | (~current_umask() & S_IRWXUGO), |
| 8656 | &index); |
Chris Mason | 54aa1f4 | 2007-06-22 14:16:25 -0400 | [diff] [blame] | 8657 | if (IS_ERR(inode)) |
Christoph Hellwig | f46b5a6 | 2008-06-11 21:53:53 -0400 | [diff] [blame] | 8658 | return PTR_ERR(inode); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 8659 | inode->i_op = &btrfs_dir_inode_operations; |
| 8660 | inode->i_fop = &btrfs_dir_file_operations; |
| 8661 | |
Miklos Szeredi | bfe8684 | 2011-10-28 14:13:29 +0200 | [diff] [blame] | 8662 | set_nlink(inode, 1); |
Chris Mason | dbe674a | 2008-07-17 12:54:05 -0400 | [diff] [blame] | 8663 | btrfs_i_size_write(inode, 0); |
Chris Mason | b0d5d10 | 2014-09-08 13:08:51 -0700 | [diff] [blame] | 8664 | unlock_new_inode(inode); |
Sven Wegener | 3b96362 | 2008-06-09 21:57:42 -0400 | [diff] [blame] | 8665 | |
Filipe David Borba Manana | 6354192 | 2014-01-07 11:47:46 +0000 | [diff] [blame] | 8666 | err = btrfs_subvol_inherit_props(trans, new_root, parent_root); |
| 8667 | if (err) |
| 8668 | btrfs_err(new_root->fs_info, |
David Sterba | 351fd35 | 2014-05-15 16:48:20 +0200 | [diff] [blame] | 8669 | "error inheriting subvolume %llu properties: %d", |
Filipe David Borba Manana | 6354192 | 2014-01-07 11:47:46 +0000 | [diff] [blame] | 8670 | new_root->root_key.objectid, err); |
| 8671 | |
Yan, Zheng | 76dda93 | 2009-09-21 16:00:26 -0400 | [diff] [blame] | 8672 | err = btrfs_update_inode(trans, new_root, inode); |
Christoph Hellwig | cb8e709 | 2008-10-09 13:39:39 -0400 | [diff] [blame] | 8673 | |
Yan, Zheng | 76dda93 | 2009-09-21 16:00:26 -0400 | [diff] [blame] | 8674 | iput(inode); |
Mark Fasheh | ce59897 | 2011-07-26 11:32:23 -0700 | [diff] [blame] | 8675 | return err; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 8676 | } |
| 8677 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 8678 | struct inode *btrfs_alloc_inode(struct super_block *sb) |
| 8679 | { |
| 8680 | struct btrfs_inode *ei; |
Yan, Zheng | 2ead6ae | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 8681 | struct inode *inode; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 8682 | |
| 8683 | ei = kmem_cache_alloc(btrfs_inode_cachep, GFP_NOFS); |
| 8684 | if (!ei) |
| 8685 | return NULL; |
Yan, Zheng | 2ead6ae | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 8686 | |
| 8687 | ei->root = NULL; |
Yan, Zheng | 2ead6ae | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 8688 | ei->generation = 0; |
Josef Bacik | 15ee9bc | 2007-08-10 16:22:09 -0400 | [diff] [blame] | 8689 | ei->last_trans = 0; |
Chris Mason | 257c62e | 2009-10-13 13:21:08 -0400 | [diff] [blame] | 8690 | ei->last_sub_trans = 0; |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 8691 | ei->logged_trans = 0; |
Yan, Zheng | 2ead6ae | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 8692 | ei->delalloc_bytes = 0; |
Wang Shilong | 47059d9 | 2014-07-03 18:22:07 +0800 | [diff] [blame] | 8693 | ei->defrag_bytes = 0; |
Yan, Zheng | 2ead6ae | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 8694 | ei->disk_i_size = 0; |
| 8695 | ei->flags = 0; |
Josef Bacik | 7709cde | 2011-08-04 10:25:02 -0400 | [diff] [blame] | 8696 | ei->csum_bytes = 0; |
Yan, Zheng | 2ead6ae | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 8697 | ei->index_cnt = (u64)-1; |
Miao Xie | 67de117 | 2013-12-26 13:07:06 +0800 | [diff] [blame] | 8698 | ei->dir_index = 0; |
Yan, Zheng | 2ead6ae | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 8699 | ei->last_unlink_trans = 0; |
Liu Bo | 46d8bc3 | 2012-08-29 01:07:55 -0600 | [diff] [blame] | 8700 | ei->last_log_commit = 0; |
Yan, Zheng | 2ead6ae | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 8701 | |
Josef Bacik | 9e0baf6 | 2011-07-15 15:16:44 +0000 | [diff] [blame] | 8702 | spin_lock_init(&ei->lock); |
| 8703 | ei->outstanding_extents = 0; |
| 8704 | ei->reserved_extents = 0; |
Yan, Zheng | 2ead6ae | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 8705 | |
Josef Bacik | 72ac3c0 | 2012-05-23 14:13:11 -0400 | [diff] [blame] | 8706 | ei->runtime_flags = 0; |
Li Zefan | 261507a0 | 2010-12-17 14:21:50 +0800 | [diff] [blame] | 8707 | ei->force_compress = BTRFS_COMPRESS_NONE; |
Yan, Zheng | 2ead6ae | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 8708 | |
Miao Xie | 16cdcec | 2011-04-22 18:12:22 +0800 | [diff] [blame] | 8709 | ei->delayed_node = NULL; |
| 8710 | |
chandan r | 9cc97d6 | 2012-07-04 12:48:07 +0530 | [diff] [blame] | 8711 | ei->i_otime.tv_sec = 0; |
| 8712 | ei->i_otime.tv_nsec = 0; |
| 8713 | |
Yan, Zheng | 2ead6ae | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 8714 | inode = &ei->vfs_inode; |
David Sterba | a8067e0 | 2011-04-21 00:34:43 +0200 | [diff] [blame] | 8715 | extent_map_tree_init(&ei->extent_tree); |
David Sterba | f993c88 | 2011-04-20 23:35:57 +0200 | [diff] [blame] | 8716 | extent_io_tree_init(&ei->io_tree, &inode->i_data); |
| 8717 | extent_io_tree_init(&ei->io_failure_tree, &inode->i_data); |
Josef Bacik | 0b32f4b | 2012-03-13 09:38:00 -0400 | [diff] [blame] | 8718 | ei->io_tree.track_uptodate = 1; |
| 8719 | ei->io_failure_tree.track_uptodate = 1; |
Josef Bacik | b812ce2 | 2012-11-16 13:56:32 -0500 | [diff] [blame] | 8720 | atomic_set(&ei->sync_writers, 0); |
Yan, Zheng | 2ead6ae | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 8721 | mutex_init(&ei->log_mutex); |
Josef Bacik | f248679 | 2012-01-13 12:09:22 -0500 | [diff] [blame] | 8722 | mutex_init(&ei->delalloc_mutex); |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 8723 | btrfs_ordered_inode_tree_init(&ei->ordered_tree); |
Yan, Zheng | 2ead6ae | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 8724 | INIT_LIST_HEAD(&ei->delalloc_inodes); |
Yan, Zheng | 2ead6ae | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 8725 | RB_CLEAR_NODE(&ei->rb_node); |
| 8726 | |
| 8727 | return inode; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 8728 | } |
| 8729 | |
Josef Bacik | aaedb55 | 2013-10-11 14:44:09 -0400 | [diff] [blame] | 8730 | #ifdef CONFIG_BTRFS_FS_RUN_SANITY_TESTS |
| 8731 | void btrfs_test_destroy_inode(struct inode *inode) |
| 8732 | { |
| 8733 | btrfs_drop_extent_cache(inode, 0, (u64)-1, 0); |
| 8734 | kmem_cache_free(btrfs_inode_cachep, BTRFS_I(inode)); |
| 8735 | } |
| 8736 | #endif |
| 8737 | |
Nick Piggin | fa0d7e3d | 2011-01-07 17:49:49 +1100 | [diff] [blame] | 8738 | static void btrfs_i_callback(struct rcu_head *head) |
| 8739 | { |
| 8740 | struct inode *inode = container_of(head, struct inode, i_rcu); |
Nick Piggin | fa0d7e3d | 2011-01-07 17:49:49 +1100 | [diff] [blame] | 8741 | kmem_cache_free(btrfs_inode_cachep, BTRFS_I(inode)); |
| 8742 | } |
| 8743 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 8744 | void btrfs_destroy_inode(struct inode *inode) |
| 8745 | { |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 8746 | struct btrfs_ordered_extent *ordered; |
Chris Mason | 5a3f23d | 2009-03-31 13:27:11 -0400 | [diff] [blame] | 8747 | struct btrfs_root *root = BTRFS_I(inode)->root; |
| 8748 | |
Al Viro | b3d9b7a | 2012-06-09 13:51:19 -0400 | [diff] [blame] | 8749 | WARN_ON(!hlist_empty(&inode->i_dentry)); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 8750 | WARN_ON(inode->i_data.nrpages); |
Josef Bacik | 9e0baf6 | 2011-07-15 15:16:44 +0000 | [diff] [blame] | 8751 | WARN_ON(BTRFS_I(inode)->outstanding_extents); |
| 8752 | WARN_ON(BTRFS_I(inode)->reserved_extents); |
Josef Bacik | 7709cde | 2011-08-04 10:25:02 -0400 | [diff] [blame] | 8753 | WARN_ON(BTRFS_I(inode)->delalloc_bytes); |
| 8754 | WARN_ON(BTRFS_I(inode)->csum_bytes); |
Wang Shilong | 47059d9 | 2014-07-03 18:22:07 +0800 | [diff] [blame] | 8755 | WARN_ON(BTRFS_I(inode)->defrag_bytes); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 8756 | |
Chris Mason | 5a3f23d | 2009-03-31 13:27:11 -0400 | [diff] [blame] | 8757 | /* |
Josef Bacik | a6dbd42 | 2009-11-11 15:53:34 -0500 | [diff] [blame] | 8758 | * This can happen where we create an inode, but somebody else also |
| 8759 | * created the same inode and we need to destroy the one we already |
| 8760 | * created. |
| 8761 | */ |
| 8762 | if (!root) |
| 8763 | goto free; |
| 8764 | |
Josef Bacik | 8a35d95 | 2012-05-23 14:26:42 -0400 | [diff] [blame] | 8765 | if (test_bit(BTRFS_INODE_HAS_ORPHAN_ITEM, |
| 8766 | &BTRFS_I(inode)->runtime_flags)) { |
Simon Kirby | c2cf52e | 2013-03-19 22:41:23 +0000 | [diff] [blame] | 8767 | btrfs_info(root->fs_info, "inode %llu still on the orphan list", |
Geert Uytterhoeven | c1c9ff7 | 2013-08-20 13:20:07 +0200 | [diff] [blame] | 8768 | btrfs_ino(inode)); |
Josef Bacik | 8a35d95 | 2012-05-23 14:26:42 -0400 | [diff] [blame] | 8769 | atomic_dec(&root->orphan_inodes); |
Josef Bacik | 7b12876 | 2008-07-24 12:17:14 -0400 | [diff] [blame] | 8770 | } |
Josef Bacik | 7b12876 | 2008-07-24 12:17:14 -0400 | [diff] [blame] | 8771 | |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 8772 | while (1) { |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 8773 | ordered = btrfs_lookup_first_ordered_extent(inode, (u64)-1); |
| 8774 | if (!ordered) |
| 8775 | break; |
| 8776 | else { |
Simon Kirby | c2cf52e | 2013-03-19 22:41:23 +0000 | [diff] [blame] | 8777 | 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] | 8778 | ordered->file_offset, ordered->len); |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 8779 | btrfs_remove_ordered_extent(inode, ordered); |
| 8780 | btrfs_put_ordered_extent(ordered); |
| 8781 | btrfs_put_ordered_extent(ordered); |
| 8782 | } |
| 8783 | } |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 8784 | inode_tree_del(inode); |
Zheng Yan | 5b21f2e | 2008-09-26 10:05:38 -0400 | [diff] [blame] | 8785 | btrfs_drop_extent_cache(inode, 0, (u64)-1, 0); |
Josef Bacik | a6dbd42 | 2009-11-11 15:53:34 -0500 | [diff] [blame] | 8786 | free: |
Nick Piggin | fa0d7e3d | 2011-01-07 17:49:49 +1100 | [diff] [blame] | 8787 | call_rcu(&inode->i_rcu, btrfs_i_callback); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 8788 | } |
| 8789 | |
Al Viro | 45321ac | 2010-06-07 13:43:19 -0400 | [diff] [blame] | 8790 | int btrfs_drop_inode(struct inode *inode) |
Yan, Zheng | 76dda93 | 2009-09-21 16:00:26 -0400 | [diff] [blame] | 8791 | { |
| 8792 | struct btrfs_root *root = BTRFS_I(inode)->root; |
Al Viro | 45321ac | 2010-06-07 13:43:19 -0400 | [diff] [blame] | 8793 | |
Naohiro Aota | 6379ef9 | 2013-06-06 09:56:34 +0000 | [diff] [blame] | 8794 | if (root == NULL) |
| 8795 | return 1; |
| 8796 | |
Liu Bo | fa6ac87 | 2013-02-20 14:10:23 +0000 | [diff] [blame] | 8797 | /* the snap/subvol tree is on deleting */ |
Stefan Behrens | 69e9c6c | 2013-09-05 16:58:43 +0200 | [diff] [blame] | 8798 | if (btrfs_root_refs(&root->root_item) == 0) |
Al Viro | 45321ac | 2010-06-07 13:43:19 -0400 | [diff] [blame] | 8799 | return 1; |
Yan, Zheng | 76dda93 | 2009-09-21 16:00:26 -0400 | [diff] [blame] | 8800 | else |
Al Viro | 45321ac | 2010-06-07 13:43:19 -0400 | [diff] [blame] | 8801 | return generic_drop_inode(inode); |
Yan, Zheng | 76dda93 | 2009-09-21 16:00:26 -0400 | [diff] [blame] | 8802 | } |
| 8803 | |
Sven Wegener | 0ee0fda | 2008-07-30 16:54:26 -0400 | [diff] [blame] | 8804 | static void init_once(void *foo) |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 8805 | { |
| 8806 | struct btrfs_inode *ei = (struct btrfs_inode *) foo; |
| 8807 | |
| 8808 | inode_init_once(&ei->vfs_inode); |
| 8809 | } |
| 8810 | |
| 8811 | void btrfs_destroy_cachep(void) |
| 8812 | { |
Kirill A. Shutemov | 8c0a853 | 2012-09-26 11:33:07 +1000 | [diff] [blame] | 8813 | /* |
| 8814 | * Make sure all delayed rcu free inodes are flushed before we |
| 8815 | * destroy cache. |
| 8816 | */ |
| 8817 | rcu_barrier(); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 8818 | if (btrfs_inode_cachep) |
| 8819 | kmem_cache_destroy(btrfs_inode_cachep); |
| 8820 | if (btrfs_trans_handle_cachep) |
| 8821 | kmem_cache_destroy(btrfs_trans_handle_cachep); |
| 8822 | if (btrfs_transaction_cachep) |
| 8823 | kmem_cache_destroy(btrfs_transaction_cachep); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 8824 | if (btrfs_path_cachep) |
| 8825 | kmem_cache_destroy(btrfs_path_cachep); |
Josef Bacik | dc89e98 | 2011-01-28 17:05:48 -0500 | [diff] [blame] | 8826 | if (btrfs_free_space_cachep) |
| 8827 | kmem_cache_destroy(btrfs_free_space_cachep); |
Miao Xie | 8ccf6f19 | 2012-10-25 09:28:04 +0000 | [diff] [blame] | 8828 | if (btrfs_delalloc_work_cachep) |
| 8829 | kmem_cache_destroy(btrfs_delalloc_work_cachep); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 8830 | } |
| 8831 | |
| 8832 | int btrfs_init_cachep(void) |
| 8833 | { |
David Sterba | 837e197 | 2012-09-07 03:00:48 -0600 | [diff] [blame] | 8834 | btrfs_inode_cachep = kmem_cache_create("btrfs_inode", |
Christoph Hellwig | 9601e3f | 2009-04-13 15:33:09 +0200 | [diff] [blame] | 8835 | sizeof(struct btrfs_inode), 0, |
| 8836 | SLAB_RECLAIM_ACCOUNT | SLAB_MEM_SPREAD, init_once); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 8837 | if (!btrfs_inode_cachep) |
| 8838 | goto fail; |
Christoph Hellwig | 9601e3f | 2009-04-13 15:33:09 +0200 | [diff] [blame] | 8839 | |
David Sterba | 837e197 | 2012-09-07 03:00:48 -0600 | [diff] [blame] | 8840 | btrfs_trans_handle_cachep = kmem_cache_create("btrfs_trans_handle", |
Christoph Hellwig | 9601e3f | 2009-04-13 15:33:09 +0200 | [diff] [blame] | 8841 | sizeof(struct btrfs_trans_handle), 0, |
| 8842 | SLAB_RECLAIM_ACCOUNT | SLAB_MEM_SPREAD, NULL); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 8843 | if (!btrfs_trans_handle_cachep) |
| 8844 | goto fail; |
Christoph Hellwig | 9601e3f | 2009-04-13 15:33:09 +0200 | [diff] [blame] | 8845 | |
David Sterba | 837e197 | 2012-09-07 03:00:48 -0600 | [diff] [blame] | 8846 | btrfs_transaction_cachep = kmem_cache_create("btrfs_transaction", |
Christoph Hellwig | 9601e3f | 2009-04-13 15:33:09 +0200 | [diff] [blame] | 8847 | sizeof(struct btrfs_transaction), 0, |
| 8848 | SLAB_RECLAIM_ACCOUNT | SLAB_MEM_SPREAD, NULL); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 8849 | if (!btrfs_transaction_cachep) |
| 8850 | goto fail; |
Christoph Hellwig | 9601e3f | 2009-04-13 15:33:09 +0200 | [diff] [blame] | 8851 | |
David Sterba | 837e197 | 2012-09-07 03:00:48 -0600 | [diff] [blame] | 8852 | btrfs_path_cachep = kmem_cache_create("btrfs_path", |
Christoph Hellwig | 9601e3f | 2009-04-13 15:33:09 +0200 | [diff] [blame] | 8853 | sizeof(struct btrfs_path), 0, |
| 8854 | SLAB_RECLAIM_ACCOUNT | SLAB_MEM_SPREAD, NULL); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 8855 | if (!btrfs_path_cachep) |
| 8856 | goto fail; |
Christoph Hellwig | 9601e3f | 2009-04-13 15:33:09 +0200 | [diff] [blame] | 8857 | |
David Sterba | 837e197 | 2012-09-07 03:00:48 -0600 | [diff] [blame] | 8858 | btrfs_free_space_cachep = kmem_cache_create("btrfs_free_space", |
Josef Bacik | dc89e98 | 2011-01-28 17:05:48 -0500 | [diff] [blame] | 8859 | sizeof(struct btrfs_free_space), 0, |
| 8860 | SLAB_RECLAIM_ACCOUNT | SLAB_MEM_SPREAD, NULL); |
| 8861 | if (!btrfs_free_space_cachep) |
| 8862 | goto fail; |
| 8863 | |
Miao Xie | 8ccf6f19 | 2012-10-25 09:28:04 +0000 | [diff] [blame] | 8864 | btrfs_delalloc_work_cachep = kmem_cache_create("btrfs_delalloc_work", |
| 8865 | sizeof(struct btrfs_delalloc_work), 0, |
| 8866 | SLAB_RECLAIM_ACCOUNT | SLAB_MEM_SPREAD, |
| 8867 | NULL); |
| 8868 | if (!btrfs_delalloc_work_cachep) |
| 8869 | goto fail; |
| 8870 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 8871 | return 0; |
| 8872 | fail: |
| 8873 | btrfs_destroy_cachep(); |
| 8874 | return -ENOMEM; |
| 8875 | } |
| 8876 | |
| 8877 | static int btrfs_getattr(struct vfsmount *mnt, |
| 8878 | struct dentry *dentry, struct kstat *stat) |
| 8879 | { |
Miao Xie | df0af1a | 2013-01-29 10:11:59 +0000 | [diff] [blame] | 8880 | u64 delalloc_bytes; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 8881 | struct inode *inode = dentry->d_inode; |
David Sterba | fadc0d8 | 2011-11-20 07:33:38 -0500 | [diff] [blame] | 8882 | u32 blocksize = inode->i_sb->s_blocksize; |
| 8883 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 8884 | generic_fillattr(inode, stat); |
Al Viro | 0ee5dc6 | 2011-07-07 15:44:25 -0400 | [diff] [blame] | 8885 | stat->dev = BTRFS_I(inode)->root->anon_dev; |
Chris Mason | d666746 | 2008-01-03 14:51:00 -0500 | [diff] [blame] | 8886 | stat->blksize = PAGE_CACHE_SIZE; |
Miao Xie | df0af1a | 2013-01-29 10:11:59 +0000 | [diff] [blame] | 8887 | |
| 8888 | spin_lock(&BTRFS_I(inode)->lock); |
| 8889 | delalloc_bytes = BTRFS_I(inode)->delalloc_bytes; |
| 8890 | spin_unlock(&BTRFS_I(inode)->lock); |
David Sterba | fadc0d8 | 2011-11-20 07:33:38 -0500 | [diff] [blame] | 8891 | stat->blocks = (ALIGN(inode_get_bytes(inode), blocksize) + |
Miao Xie | df0af1a | 2013-01-29 10:11:59 +0000 | [diff] [blame] | 8892 | ALIGN(delalloc_bytes, blocksize)) >> 9; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 8893 | return 0; |
| 8894 | } |
| 8895 | |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 8896 | static int btrfs_rename(struct inode *old_dir, struct dentry *old_dentry, |
| 8897 | struct inode *new_dir, struct dentry *new_dentry) |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 8898 | { |
| 8899 | struct btrfs_trans_handle *trans; |
| 8900 | struct btrfs_root *root = BTRFS_I(old_dir)->root; |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 8901 | struct btrfs_root *dest = BTRFS_I(new_dir)->root; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 8902 | struct inode *new_inode = new_dentry->d_inode; |
| 8903 | struct inode *old_inode = old_dentry->d_inode; |
| 8904 | struct timespec ctime = CURRENT_TIME; |
Chris Mason | 00e4e6b | 2008-08-05 11:18:09 -0400 | [diff] [blame] | 8905 | u64 index = 0; |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 8906 | u64 root_objectid; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 8907 | int ret; |
Li Zefan | 33345d01 | 2011-04-20 10:31:50 +0800 | [diff] [blame] | 8908 | u64 old_ino = btrfs_ino(old_inode); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 8909 | |
Li Zefan | 33345d01 | 2011-04-20 10:31:50 +0800 | [diff] [blame] | 8910 | if (btrfs_ino(new_dir) == BTRFS_EMPTY_SUBVOL_DIR_OBJECTID) |
Yan, Zheng | f679a84 | 2009-09-24 09:17:31 -0400 | [diff] [blame] | 8911 | return -EPERM; |
| 8912 | |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 8913 | /* we only allow rename subvolume link between subvolumes */ |
Li Zefan | 33345d01 | 2011-04-20 10:31:50 +0800 | [diff] [blame] | 8914 | if (old_ino != BTRFS_FIRST_FREE_OBJECTID && root != dest) |
Chris Mason | 3394e16 | 2008-11-17 20:42:26 -0500 | [diff] [blame] | 8915 | return -EXDEV; |
| 8916 | |
Li Zefan | 33345d01 | 2011-04-20 10:31:50 +0800 | [diff] [blame] | 8917 | if (old_ino == BTRFS_EMPTY_SUBVOL_DIR_OBJECTID || |
| 8918 | (new_inode && btrfs_ino(new_inode) == BTRFS_FIRST_FREE_OBJECTID)) |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 8919 | return -ENOTEMPTY; |
| 8920 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 8921 | if (S_ISDIR(old_inode->i_mode) && new_inode && |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 8922 | new_inode->i_size > BTRFS_EMPTY_DIR_SIZE) |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 8923 | return -ENOTEMPTY; |
Chris Mason | 9c52057 | 2012-12-17 14:26:57 -0500 | [diff] [blame] | 8924 | |
| 8925 | |
| 8926 | /* check for collisions, even if the name isn't there */ |
Josef Bacik | 4871c15 | 2013-10-09 12:24:04 -0400 | [diff] [blame] | 8927 | ret = btrfs_check_dir_item_collision(dest, new_dir->i_ino, |
Chris Mason | 9c52057 | 2012-12-17 14:26:57 -0500 | [diff] [blame] | 8928 | new_dentry->d_name.name, |
| 8929 | new_dentry->d_name.len); |
| 8930 | |
| 8931 | if (ret) { |
| 8932 | if (ret == -EEXIST) { |
| 8933 | /* we shouldn't get |
| 8934 | * eexist without a new_inode */ |
Dulshani Gunawardhana | fae7f21 | 2013-10-31 10:30:08 +0530 | [diff] [blame] | 8935 | if (WARN_ON(!new_inode)) { |
Chris Mason | 9c52057 | 2012-12-17 14:26:57 -0500 | [diff] [blame] | 8936 | return ret; |
| 8937 | } |
| 8938 | } else { |
| 8939 | /* maybe -EOVERFLOW */ |
| 8940 | return ret; |
| 8941 | } |
| 8942 | } |
| 8943 | ret = 0; |
| 8944 | |
Chris Mason | 5a3f23d | 2009-03-31 13:27:11 -0400 | [diff] [blame] | 8945 | /* |
Chris Mason | 8d875f9 | 2014-08-12 10:47:42 -0700 | [diff] [blame] | 8946 | * we're using rename to replace one file with another. Start IO on it |
| 8947 | * 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] | 8948 | */ |
Chris Mason | 8d875f9 | 2014-08-12 10:47:42 -0700 | [diff] [blame] | 8949 | 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] | 8950 | filemap_flush(old_inode->i_mapping); |
| 8951 | |
Yan, Zheng | 76dda93 | 2009-09-21 16:00:26 -0400 | [diff] [blame] | 8952 | /* close the racy window with snapshot create/destroy ioctl */ |
Li Zefan | 33345d01 | 2011-04-20 10:31:50 +0800 | [diff] [blame] | 8953 | if (old_ino == BTRFS_FIRST_FREE_OBJECTID) |
Yan, Zheng | 76dda93 | 2009-09-21 16:00:26 -0400 | [diff] [blame] | 8954 | down_read(&root->fs_info->subvol_sem); |
Yan, Zheng | a22285a | 2010-05-16 10:48:46 -0400 | [diff] [blame] | 8955 | /* |
| 8956 | * We want to reserve the absolute worst case amount of items. So if |
| 8957 | * both inodes are subvols and we need to unlink them then that would |
| 8958 | * require 4 item modifications, but if they are both normal inodes it |
| 8959 | * would require 5 item modifications, so we'll assume their normal |
| 8960 | * inodes. So 5 * 2 is 10, plus 1 for the new link, so 11 total items |
| 8961 | * should cover the worst case number of items we'll modify. |
| 8962 | */ |
Josef Bacik | 6e137ed | 2013-03-26 15:26:55 -0400 | [diff] [blame] | 8963 | trans = btrfs_start_transaction(root, 11); |
Johann Lombardi | b44c59a | 2011-03-31 13:23:47 +0000 | [diff] [blame] | 8964 | if (IS_ERR(trans)) { |
| 8965 | ret = PTR_ERR(trans); |
| 8966 | goto out_notrans; |
| 8967 | } |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 8968 | |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 8969 | if (dest != root) |
| 8970 | btrfs_record_root_in_trans(trans, dest); |
| 8971 | |
Yan, Zheng | a571952 | 2009-09-24 09:17:31 -0400 | [diff] [blame] | 8972 | ret = btrfs_set_inode_index(new_dir, &index); |
| 8973 | if (ret) |
| 8974 | goto out_fail; |
Chris Mason | 5a3f23d | 2009-03-31 13:27:11 -0400 | [diff] [blame] | 8975 | |
Miao Xie | 67de117 | 2013-12-26 13:07:06 +0800 | [diff] [blame] | 8976 | BTRFS_I(old_inode)->dir_index = 0ULL; |
Li Zefan | 33345d01 | 2011-04-20 10:31:50 +0800 | [diff] [blame] | 8977 | if (unlikely(old_ino == BTRFS_FIRST_FREE_OBJECTID)) { |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 8978 | /* force full log commit if subvolume involved. */ |
Miao Xie | 995946d | 2014-04-02 19:51:06 +0800 | [diff] [blame] | 8979 | btrfs_set_log_full_commit(root->fs_info, trans); |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 8980 | } else { |
Yan, Zheng | a571952 | 2009-09-24 09:17:31 -0400 | [diff] [blame] | 8981 | ret = btrfs_insert_inode_ref(trans, dest, |
| 8982 | new_dentry->d_name.name, |
| 8983 | new_dentry->d_name.len, |
Li Zefan | 33345d01 | 2011-04-20 10:31:50 +0800 | [diff] [blame] | 8984 | old_ino, |
| 8985 | btrfs_ino(new_dir), index); |
Yan, Zheng | a571952 | 2009-09-24 09:17:31 -0400 | [diff] [blame] | 8986 | if (ret) |
| 8987 | goto out_fail; |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 8988 | /* |
| 8989 | * this is an ugly little race, but the rename is required |
| 8990 | * to make sure that if we crash, the inode is either at the |
| 8991 | * old name or the new one. pinning the log transaction lets |
| 8992 | * us make sure we don't allow a log commit to come in after |
| 8993 | * we unlink the name but before we add the new name back in. |
| 8994 | */ |
| 8995 | btrfs_pin_log_trans(root); |
| 8996 | } |
Chris Mason | 5a3f23d | 2009-03-31 13:27:11 -0400 | [diff] [blame] | 8997 | |
Josef Bacik | 0c4d2d9 | 2012-04-05 15:03:02 -0400 | [diff] [blame] | 8998 | inode_inc_iversion(old_dir); |
| 8999 | inode_inc_iversion(new_dir); |
| 9000 | inode_inc_iversion(old_inode); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 9001 | old_dir->i_ctime = old_dir->i_mtime = ctime; |
| 9002 | new_dir->i_ctime = new_dir->i_mtime = ctime; |
| 9003 | old_inode->i_ctime = ctime; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 9004 | |
Chris Mason | 12fcfd2 | 2009-03-24 10:24:20 -0400 | [diff] [blame] | 9005 | if (old_dentry->d_parent != new_dentry->d_parent) |
| 9006 | btrfs_record_unlink_dir(trans, old_dir, old_inode, 1); |
| 9007 | |
Li Zefan | 33345d01 | 2011-04-20 10:31:50 +0800 | [diff] [blame] | 9008 | if (unlikely(old_ino == BTRFS_FIRST_FREE_OBJECTID)) { |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 9009 | root_objectid = BTRFS_I(old_inode)->root->root_key.objectid; |
| 9010 | ret = btrfs_unlink_subvol(trans, root, old_dir, root_objectid, |
| 9011 | old_dentry->d_name.name, |
| 9012 | old_dentry->d_name.len); |
| 9013 | } else { |
Al Viro | 9298679 | 2011-03-04 17:14:37 +0000 | [diff] [blame] | 9014 | ret = __btrfs_unlink_inode(trans, root, old_dir, |
| 9015 | old_dentry->d_inode, |
| 9016 | old_dentry->d_name.name, |
| 9017 | old_dentry->d_name.len); |
| 9018 | if (!ret) |
| 9019 | ret = btrfs_update_inode(trans, root, old_inode); |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 9020 | } |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 9021 | if (ret) { |
| 9022 | btrfs_abort_transaction(trans, root, ret); |
| 9023 | goto out_fail; |
| 9024 | } |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 9025 | |
| 9026 | if (new_inode) { |
Josef Bacik | 0c4d2d9 | 2012-04-05 15:03:02 -0400 | [diff] [blame] | 9027 | inode_inc_iversion(new_inode); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 9028 | new_inode->i_ctime = CURRENT_TIME; |
Li Zefan | 33345d01 | 2011-04-20 10:31:50 +0800 | [diff] [blame] | 9029 | if (unlikely(btrfs_ino(new_inode) == |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 9030 | BTRFS_EMPTY_SUBVOL_DIR_OBJECTID)) { |
| 9031 | root_objectid = BTRFS_I(new_inode)->location.objectid; |
| 9032 | ret = btrfs_unlink_subvol(trans, dest, new_dir, |
| 9033 | root_objectid, |
| 9034 | new_dentry->d_name.name, |
| 9035 | new_dentry->d_name.len); |
| 9036 | BUG_ON(new_inode->i_nlink == 0); |
| 9037 | } else { |
| 9038 | ret = btrfs_unlink_inode(trans, dest, new_dir, |
| 9039 | new_dentry->d_inode, |
| 9040 | new_dentry->d_name.name, |
| 9041 | new_dentry->d_name.len); |
| 9042 | } |
Josef Bacik | 4ef31a4 | 2013-08-13 14:10:08 -0400 | [diff] [blame] | 9043 | if (!ret && new_inode->i_nlink == 0) |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 9044 | ret = btrfs_orphan_add(trans, new_dentry->d_inode); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 9045 | if (ret) { |
| 9046 | btrfs_abort_transaction(trans, root, ret); |
| 9047 | goto out_fail; |
| 9048 | } |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 9049 | } |
Josef Bacik | aec7477 | 2008-07-24 12:12:38 -0400 | [diff] [blame] | 9050 | |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 9051 | ret = btrfs_add_link(trans, new_dir, old_inode, |
| 9052 | new_dentry->d_name.name, |
Yan, Zheng | a571952 | 2009-09-24 09:17:31 -0400 | [diff] [blame] | 9053 | new_dentry->d_name.len, 0, index); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 9054 | if (ret) { |
| 9055 | btrfs_abort_transaction(trans, root, ret); |
| 9056 | goto out_fail; |
| 9057 | } |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 9058 | |
Miao Xie | 67de117 | 2013-12-26 13:07:06 +0800 | [diff] [blame] | 9059 | if (old_inode->i_nlink == 1) |
| 9060 | BTRFS_I(old_inode)->dir_index = index; |
| 9061 | |
Li Zefan | 33345d01 | 2011-04-20 10:31:50 +0800 | [diff] [blame] | 9062 | if (old_ino != BTRFS_FIRST_FREE_OBJECTID) { |
Al Viro | 10d9f30 | 2011-07-16 23:09:10 -0400 | [diff] [blame] | 9063 | struct dentry *parent = new_dentry->d_parent; |
Josef Bacik | 6a91221 | 2010-11-20 09:48:00 +0000 | [diff] [blame] | 9064 | btrfs_log_new_name(trans, old_inode, old_dir, parent); |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 9065 | btrfs_end_log_trans(root); |
| 9066 | } |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 9067 | out_fail: |
Josef Bacik | 7ad85bb | 2012-01-12 19:10:12 -0500 | [diff] [blame] | 9068 | btrfs_end_transaction(trans, root); |
Johann Lombardi | b44c59a | 2011-03-31 13:23:47 +0000 | [diff] [blame] | 9069 | out_notrans: |
Li Zefan | 33345d01 | 2011-04-20 10:31:50 +0800 | [diff] [blame] | 9070 | if (old_ino == BTRFS_FIRST_FREE_OBJECTID) |
Yan, Zheng | 76dda93 | 2009-09-21 16:00:26 -0400 | [diff] [blame] | 9071 | up_read(&root->fs_info->subvol_sem); |
Josef Bacik | 9ed74f2 | 2009-09-11 16:12:44 -0400 | [diff] [blame] | 9072 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 9073 | return ret; |
| 9074 | } |
| 9075 | |
Miklos Szeredi | 80ace85 | 2014-07-23 15:15:32 +0200 | [diff] [blame] | 9076 | static int btrfs_rename2(struct inode *old_dir, struct dentry *old_dentry, |
| 9077 | struct inode *new_dir, struct dentry *new_dentry, |
| 9078 | unsigned int flags) |
| 9079 | { |
| 9080 | if (flags & ~RENAME_NOREPLACE) |
| 9081 | return -EINVAL; |
| 9082 | |
| 9083 | return btrfs_rename(old_dir, old_dentry, new_dir, new_dentry); |
| 9084 | } |
| 9085 | |
Miao Xie | 8ccf6f19 | 2012-10-25 09:28:04 +0000 | [diff] [blame] | 9086 | static void btrfs_run_delalloc_work(struct btrfs_work *work) |
| 9087 | { |
| 9088 | struct btrfs_delalloc_work *delalloc_work; |
Josef Bacik | 9f23e28 | 2013-10-28 15:03:41 -0400 | [diff] [blame] | 9089 | struct inode *inode; |
Miao Xie | 8ccf6f19 | 2012-10-25 09:28:04 +0000 | [diff] [blame] | 9090 | |
| 9091 | delalloc_work = container_of(work, struct btrfs_delalloc_work, |
| 9092 | work); |
Josef Bacik | 9f23e28 | 2013-10-28 15:03:41 -0400 | [diff] [blame] | 9093 | inode = delalloc_work->inode; |
| 9094 | if (delalloc_work->wait) { |
| 9095 | btrfs_wait_ordered_range(inode, 0, (u64)-1); |
| 9096 | } else { |
| 9097 | filemap_flush(inode->i_mapping); |
| 9098 | if (test_bit(BTRFS_INODE_HAS_ASYNC_EXTENT, |
| 9099 | &BTRFS_I(inode)->runtime_flags)) |
| 9100 | filemap_flush(inode->i_mapping); |
| 9101 | } |
Miao Xie | 8ccf6f19 | 2012-10-25 09:28:04 +0000 | [diff] [blame] | 9102 | |
| 9103 | if (delalloc_work->delay_iput) |
Josef Bacik | 9f23e28 | 2013-10-28 15:03:41 -0400 | [diff] [blame] | 9104 | btrfs_add_delayed_iput(inode); |
Miao Xie | 8ccf6f19 | 2012-10-25 09:28:04 +0000 | [diff] [blame] | 9105 | else |
Josef Bacik | 9f23e28 | 2013-10-28 15:03:41 -0400 | [diff] [blame] | 9106 | iput(inode); |
Miao Xie | 8ccf6f19 | 2012-10-25 09:28:04 +0000 | [diff] [blame] | 9107 | complete(&delalloc_work->completion); |
| 9108 | } |
| 9109 | |
| 9110 | struct btrfs_delalloc_work *btrfs_alloc_delalloc_work(struct inode *inode, |
| 9111 | int wait, int delay_iput) |
| 9112 | { |
| 9113 | struct btrfs_delalloc_work *work; |
| 9114 | |
| 9115 | work = kmem_cache_zalloc(btrfs_delalloc_work_cachep, GFP_NOFS); |
| 9116 | if (!work) |
| 9117 | return NULL; |
| 9118 | |
| 9119 | init_completion(&work->completion); |
| 9120 | INIT_LIST_HEAD(&work->list); |
| 9121 | work->inode = inode; |
| 9122 | work->wait = wait; |
| 9123 | work->delay_iput = delay_iput; |
Liu Bo | 9e0af23 | 2014-08-15 23:36:53 +0800 | [diff] [blame] | 9124 | WARN_ON_ONCE(!inode); |
| 9125 | btrfs_init_work(&work->work, btrfs_flush_delalloc_helper, |
| 9126 | btrfs_run_delalloc_work, NULL, NULL); |
Miao Xie | 8ccf6f19 | 2012-10-25 09:28:04 +0000 | [diff] [blame] | 9127 | |
| 9128 | return work; |
| 9129 | } |
| 9130 | |
| 9131 | void btrfs_wait_and_free_delalloc_work(struct btrfs_delalloc_work *work) |
| 9132 | { |
| 9133 | wait_for_completion(&work->completion); |
| 9134 | kmem_cache_free(btrfs_delalloc_work_cachep, work); |
| 9135 | } |
| 9136 | |
Chris Mason | d352ac6 | 2008-09-29 15:18:18 -0400 | [diff] [blame] | 9137 | /* |
| 9138 | * some fairly slow code that needs optimization. This walks the list |
| 9139 | * of all the inodes with pending delalloc and forces them to disk. |
| 9140 | */ |
Miao Xie | 6c255e6 | 2014-03-06 13:55:01 +0800 | [diff] [blame] | 9141 | static int __start_delalloc_inodes(struct btrfs_root *root, int delay_iput, |
| 9142 | int nr) |
Chris Mason | ea8c281 | 2008-08-04 23:17:27 -0400 | [diff] [blame] | 9143 | { |
Chris Mason | ea8c281 | 2008-08-04 23:17:27 -0400 | [diff] [blame] | 9144 | struct btrfs_inode *binode; |
Zheng Yan | 5b21f2e | 2008-09-26 10:05:38 -0400 | [diff] [blame] | 9145 | struct inode *inode; |
Miao Xie | 8ccf6f19 | 2012-10-25 09:28:04 +0000 | [diff] [blame] | 9146 | struct btrfs_delalloc_work *work, *next; |
| 9147 | struct list_head works; |
Miao Xie | 1eafa6c | 2013-01-22 10:49:00 +0000 | [diff] [blame] | 9148 | struct list_head splice; |
Miao Xie | 8ccf6f19 | 2012-10-25 09:28:04 +0000 | [diff] [blame] | 9149 | int ret = 0; |
Chris Mason | ea8c281 | 2008-08-04 23:17:27 -0400 | [diff] [blame] | 9150 | |
Miao Xie | 8ccf6f19 | 2012-10-25 09:28:04 +0000 | [diff] [blame] | 9151 | INIT_LIST_HEAD(&works); |
Miao Xie | 1eafa6c | 2013-01-22 10:49:00 +0000 | [diff] [blame] | 9152 | INIT_LIST_HEAD(&splice); |
Miao Xie | 63607cc | 2013-01-22 10:50:35 +0000 | [diff] [blame] | 9153 | |
Miao Xie | 573bfb7 | 2014-03-06 13:55:03 +0800 | [diff] [blame] | 9154 | mutex_lock(&root->delalloc_mutex); |
Miao Xie | eb73c1b | 2013-05-15 07:48:22 +0000 | [diff] [blame] | 9155 | spin_lock(&root->delalloc_lock); |
| 9156 | list_splice_init(&root->delalloc_inodes, &splice); |
Miao Xie | 1eafa6c | 2013-01-22 10:49:00 +0000 | [diff] [blame] | 9157 | while (!list_empty(&splice)) { |
| 9158 | binode = list_entry(splice.next, struct btrfs_inode, |
Chris Mason | ea8c281 | 2008-08-04 23:17:27 -0400 | [diff] [blame] | 9159 | delalloc_inodes); |
Miao Xie | 1eafa6c | 2013-01-22 10:49:00 +0000 | [diff] [blame] | 9160 | |
Miao Xie | eb73c1b | 2013-05-15 07:48:22 +0000 | [diff] [blame] | 9161 | list_move_tail(&binode->delalloc_inodes, |
| 9162 | &root->delalloc_inodes); |
Zheng Yan | 5b21f2e | 2008-09-26 10:05:38 -0400 | [diff] [blame] | 9163 | inode = igrab(&binode->vfs_inode); |
Miao Xie | df0af1a | 2013-01-29 10:11:59 +0000 | [diff] [blame] | 9164 | if (!inode) { |
Miao Xie | eb73c1b | 2013-05-15 07:48:22 +0000 | [diff] [blame] | 9165 | cond_resched_lock(&root->delalloc_lock); |
Miao Xie | 1eafa6c | 2013-01-22 10:49:00 +0000 | [diff] [blame] | 9166 | continue; |
Miao Xie | df0af1a | 2013-01-29 10:11:59 +0000 | [diff] [blame] | 9167 | } |
Miao Xie | eb73c1b | 2013-05-15 07:48:22 +0000 | [diff] [blame] | 9168 | spin_unlock(&root->delalloc_lock); |
Miao Xie | 1eafa6c | 2013-01-22 10:49:00 +0000 | [diff] [blame] | 9169 | |
| 9170 | work = btrfs_alloc_delalloc_work(inode, 0, delay_iput); |
David Sterba | 5d99a998 | 2014-09-29 19:20:37 +0200 | [diff] [blame] | 9171 | if (!work) { |
Josef Bacik | f4ab9ea | 2013-09-17 11:25:44 -0400 | [diff] [blame] | 9172 | if (delay_iput) |
| 9173 | btrfs_add_delayed_iput(inode); |
| 9174 | else |
| 9175 | iput(inode); |
Miao Xie | 1eafa6c | 2013-01-22 10:49:00 +0000 | [diff] [blame] | 9176 | ret = -ENOMEM; |
Wang Shilong | a1ecaab | 2014-04-02 19:53:32 +0800 | [diff] [blame] | 9177 | goto out; |
Zheng Yan | 5b21f2e | 2008-09-26 10:05:38 -0400 | [diff] [blame] | 9178 | } |
Miao Xie | 1eafa6c | 2013-01-22 10:49:00 +0000 | [diff] [blame] | 9179 | list_add_tail(&work->list, &works); |
Qu Wenruo | a44903a | 2014-02-28 10:46:09 +0800 | [diff] [blame] | 9180 | btrfs_queue_work(root->fs_info->flush_workers, |
| 9181 | &work->work); |
Miao Xie | 6c255e6 | 2014-03-06 13:55:01 +0800 | [diff] [blame] | 9182 | ret++; |
| 9183 | if (nr != -1 && ret >= nr) |
Wang Shilong | a1ecaab | 2014-04-02 19:53:32 +0800 | [diff] [blame] | 9184 | goto out; |
Zheng Yan | 5b21f2e | 2008-09-26 10:05:38 -0400 | [diff] [blame] | 9185 | cond_resched(); |
Miao Xie | eb73c1b | 2013-05-15 07:48:22 +0000 | [diff] [blame] | 9186 | spin_lock(&root->delalloc_lock); |
Chris Mason | ea8c281 | 2008-08-04 23:17:27 -0400 | [diff] [blame] | 9187 | } |
Miao Xie | eb73c1b | 2013-05-15 07:48:22 +0000 | [diff] [blame] | 9188 | spin_unlock(&root->delalloc_lock); |
Chris Mason | 8c8bee1 | 2008-09-29 11:19:10 -0400 | [diff] [blame] | 9189 | |
Wang Shilong | a1ecaab | 2014-04-02 19:53:32 +0800 | [diff] [blame] | 9190 | out: |
Miao Xie | 1eafa6c | 2013-01-22 10:49:00 +0000 | [diff] [blame] | 9191 | list_for_each_entry_safe(work, next, &works, list) { |
| 9192 | list_del_init(&work->list); |
| 9193 | btrfs_wait_and_free_delalloc_work(work); |
| 9194 | } |
| 9195 | |
Miao Xie | eb73c1b | 2013-05-15 07:48:22 +0000 | [diff] [blame] | 9196 | if (!list_empty_careful(&splice)) { |
| 9197 | spin_lock(&root->delalloc_lock); |
| 9198 | list_splice_tail(&splice, &root->delalloc_inodes); |
| 9199 | spin_unlock(&root->delalloc_lock); |
| 9200 | } |
Miao Xie | 573bfb7 | 2014-03-06 13:55:03 +0800 | [diff] [blame] | 9201 | mutex_unlock(&root->delalloc_mutex); |
Miao Xie | eb73c1b | 2013-05-15 07:48:22 +0000 | [diff] [blame] | 9202 | return ret; |
| 9203 | } |
| 9204 | |
| 9205 | int btrfs_start_delalloc_inodes(struct btrfs_root *root, int delay_iput) |
| 9206 | { |
| 9207 | int ret; |
| 9208 | |
Wang Shilong | 2c21b4d | 2014-01-14 19:42:20 +0800 | [diff] [blame] | 9209 | if (test_bit(BTRFS_FS_STATE_ERROR, &root->fs_info->fs_state)) |
Miao Xie | eb73c1b | 2013-05-15 07:48:22 +0000 | [diff] [blame] | 9210 | return -EROFS; |
| 9211 | |
Miao Xie | 6c255e6 | 2014-03-06 13:55:01 +0800 | [diff] [blame] | 9212 | ret = __start_delalloc_inodes(root, delay_iput, -1); |
| 9213 | if (ret > 0) |
| 9214 | ret = 0; |
Miao Xie | eb73c1b | 2013-05-15 07:48:22 +0000 | [diff] [blame] | 9215 | /* |
| 9216 | * the filemap_flush will queue IO into the worker threads, but |
Chris Mason | 8c8bee1 | 2008-09-29 11:19:10 -0400 | [diff] [blame] | 9217 | * we have to make sure the IO is actually started and that |
| 9218 | * ordered extents get created before we return |
| 9219 | */ |
| 9220 | atomic_inc(&root->fs_info->async_submit_draining); |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 9221 | while (atomic_read(&root->fs_info->nr_async_submits) || |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 9222 | atomic_read(&root->fs_info->async_delalloc_pages)) { |
Chris Mason | 8c8bee1 | 2008-09-29 11:19:10 -0400 | [diff] [blame] | 9223 | wait_event(root->fs_info->async_submit_wait, |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 9224 | (atomic_read(&root->fs_info->nr_async_submits) == 0 && |
| 9225 | atomic_read(&root->fs_info->async_delalloc_pages) == 0)); |
Chris Mason | 8c8bee1 | 2008-09-29 11:19:10 -0400 | [diff] [blame] | 9226 | } |
| 9227 | atomic_dec(&root->fs_info->async_submit_draining); |
Miao Xie | eb73c1b | 2013-05-15 07:48:22 +0000 | [diff] [blame] | 9228 | return ret; |
| 9229 | } |
| 9230 | |
Miao Xie | 6c255e6 | 2014-03-06 13:55:01 +0800 | [diff] [blame] | 9231 | int btrfs_start_delalloc_roots(struct btrfs_fs_info *fs_info, int delay_iput, |
| 9232 | int nr) |
Miao Xie | eb73c1b | 2013-05-15 07:48:22 +0000 | [diff] [blame] | 9233 | { |
| 9234 | struct btrfs_root *root; |
| 9235 | struct list_head splice; |
| 9236 | int ret; |
| 9237 | |
Wang Shilong | 2c21b4d | 2014-01-14 19:42:20 +0800 | [diff] [blame] | 9238 | if (test_bit(BTRFS_FS_STATE_ERROR, &fs_info->fs_state)) |
Miao Xie | eb73c1b | 2013-05-15 07:48:22 +0000 | [diff] [blame] | 9239 | return -EROFS; |
| 9240 | |
| 9241 | INIT_LIST_HEAD(&splice); |
| 9242 | |
Miao Xie | 573bfb7 | 2014-03-06 13:55:03 +0800 | [diff] [blame] | 9243 | mutex_lock(&fs_info->delalloc_root_mutex); |
Miao Xie | eb73c1b | 2013-05-15 07:48:22 +0000 | [diff] [blame] | 9244 | spin_lock(&fs_info->delalloc_root_lock); |
| 9245 | list_splice_init(&fs_info->delalloc_roots, &splice); |
Miao Xie | 6c255e6 | 2014-03-06 13:55:01 +0800 | [diff] [blame] | 9246 | while (!list_empty(&splice) && nr) { |
Miao Xie | eb73c1b | 2013-05-15 07:48:22 +0000 | [diff] [blame] | 9247 | root = list_first_entry(&splice, struct btrfs_root, |
| 9248 | delalloc_root); |
| 9249 | root = btrfs_grab_fs_root(root); |
| 9250 | BUG_ON(!root); |
| 9251 | list_move_tail(&root->delalloc_root, |
| 9252 | &fs_info->delalloc_roots); |
| 9253 | spin_unlock(&fs_info->delalloc_root_lock); |
| 9254 | |
Miao Xie | 6c255e6 | 2014-03-06 13:55:01 +0800 | [diff] [blame] | 9255 | ret = __start_delalloc_inodes(root, delay_iput, nr); |
Miao Xie | eb73c1b | 2013-05-15 07:48:22 +0000 | [diff] [blame] | 9256 | btrfs_put_fs_root(root); |
Miao Xie | 6c255e6 | 2014-03-06 13:55:01 +0800 | [diff] [blame] | 9257 | if (ret < 0) |
Miao Xie | eb73c1b | 2013-05-15 07:48:22 +0000 | [diff] [blame] | 9258 | goto out; |
| 9259 | |
Miao Xie | 6c255e6 | 2014-03-06 13:55:01 +0800 | [diff] [blame] | 9260 | if (nr != -1) { |
| 9261 | nr -= ret; |
| 9262 | WARN_ON(nr < 0); |
| 9263 | } |
Miao Xie | eb73c1b | 2013-05-15 07:48:22 +0000 | [diff] [blame] | 9264 | spin_lock(&fs_info->delalloc_root_lock); |
| 9265 | } |
| 9266 | spin_unlock(&fs_info->delalloc_root_lock); |
| 9267 | |
Miao Xie | 6c255e6 | 2014-03-06 13:55:01 +0800 | [diff] [blame] | 9268 | ret = 0; |
Miao Xie | eb73c1b | 2013-05-15 07:48:22 +0000 | [diff] [blame] | 9269 | atomic_inc(&fs_info->async_submit_draining); |
| 9270 | while (atomic_read(&fs_info->nr_async_submits) || |
| 9271 | atomic_read(&fs_info->async_delalloc_pages)) { |
| 9272 | wait_event(fs_info->async_submit_wait, |
| 9273 | (atomic_read(&fs_info->nr_async_submits) == 0 && |
| 9274 | atomic_read(&fs_info->async_delalloc_pages) == 0)); |
| 9275 | } |
| 9276 | atomic_dec(&fs_info->async_submit_draining); |
Miao Xie | 8ccf6f19 | 2012-10-25 09:28:04 +0000 | [diff] [blame] | 9277 | out: |
Miao Xie | 1eafa6c | 2013-01-22 10:49:00 +0000 | [diff] [blame] | 9278 | if (!list_empty_careful(&splice)) { |
Miao Xie | eb73c1b | 2013-05-15 07:48:22 +0000 | [diff] [blame] | 9279 | spin_lock(&fs_info->delalloc_root_lock); |
| 9280 | list_splice_tail(&splice, &fs_info->delalloc_roots); |
| 9281 | spin_unlock(&fs_info->delalloc_root_lock); |
Miao Xie | 1eafa6c | 2013-01-22 10:49:00 +0000 | [diff] [blame] | 9282 | } |
Miao Xie | 573bfb7 | 2014-03-06 13:55:03 +0800 | [diff] [blame] | 9283 | mutex_unlock(&fs_info->delalloc_root_mutex); |
Miao Xie | 8ccf6f19 | 2012-10-25 09:28:04 +0000 | [diff] [blame] | 9284 | return ret; |
Chris Mason | ea8c281 | 2008-08-04 23:17:27 -0400 | [diff] [blame] | 9285 | } |
| 9286 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 9287 | static int btrfs_symlink(struct inode *dir, struct dentry *dentry, |
| 9288 | const char *symname) |
| 9289 | { |
| 9290 | struct btrfs_trans_handle *trans; |
| 9291 | struct btrfs_root *root = BTRFS_I(dir)->root; |
| 9292 | struct btrfs_path *path; |
| 9293 | struct btrfs_key key; |
Chris Mason | 1832a6d | 2007-12-21 16:27:21 -0500 | [diff] [blame] | 9294 | struct inode *inode = NULL; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 9295 | int err; |
| 9296 | int drop_inode = 0; |
| 9297 | u64 objectid; |
Dulshani Gunawardhana | 6787125 | 2013-10-31 10:33:04 +0530 | [diff] [blame] | 9298 | u64 index = 0; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 9299 | int name_len; |
| 9300 | int datasize; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 9301 | unsigned long ptr; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 9302 | struct btrfs_file_extent_item *ei; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 9303 | struct extent_buffer *leaf; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 9304 | |
Filipe David Borba Manana | f06becc | 2013-09-16 09:53:28 +0100 | [diff] [blame] | 9305 | name_len = strlen(symname); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 9306 | if (name_len > BTRFS_MAX_INLINE_DATA_SIZE(root)) |
| 9307 | return -ENAMETOOLONG; |
Chris Mason | 1832a6d | 2007-12-21 16:27:21 -0500 | [diff] [blame] | 9308 | |
Josef Bacik | 9ed74f2 | 2009-09-11 16:12:44 -0400 | [diff] [blame] | 9309 | /* |
| 9310 | * 2 items for inode item and ref |
| 9311 | * 2 items for dir items |
| 9312 | * 1 item for xattr if selinux is on |
| 9313 | */ |
Yan, Zheng | a22285a | 2010-05-16 10:48:46 -0400 | [diff] [blame] | 9314 | trans = btrfs_start_transaction(root, 5); |
| 9315 | if (IS_ERR(trans)) |
| 9316 | return PTR_ERR(trans); |
Chris Mason | 1832a6d | 2007-12-21 16:27:21 -0500 | [diff] [blame] | 9317 | |
Li Zefan | 581bb05 | 2011-04-20 10:06:11 +0800 | [diff] [blame] | 9318 | err = btrfs_find_free_ino(root, &objectid); |
| 9319 | if (err) |
| 9320 | goto out_unlock; |
| 9321 | |
Josef Bacik | aec7477 | 2008-07-24 12:12:38 -0400 | [diff] [blame] | 9322 | inode = btrfs_new_inode(trans, root, dir, dentry->d_name.name, |
Li Zefan | 33345d01 | 2011-04-20 10:31:50 +0800 | [diff] [blame] | 9323 | dentry->d_name.len, btrfs_ino(dir), objectid, |
Josef Bacik | d82a6f1 | 2011-05-11 15:26:06 -0400 | [diff] [blame] | 9324 | S_IFLNK|S_IRWXUGO, &index); |
Tsutomu Itoh | 7cf96da | 2011-04-25 19:43:53 -0400 | [diff] [blame] | 9325 | if (IS_ERR(inode)) { |
| 9326 | err = PTR_ERR(inode); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 9327 | goto out_unlock; |
Tsutomu Itoh | 7cf96da | 2011-04-25 19:43:53 -0400 | [diff] [blame] | 9328 | } |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 9329 | |
Casey Schaufler | ad19db7 | 2011-12-15 10:09:07 -0500 | [diff] [blame] | 9330 | /* |
| 9331 | * If the active LSM wants to access the inode during |
| 9332 | * d_instantiate it needs these. Smack checks to see |
| 9333 | * if the filesystem supports xattrs by looking at the |
| 9334 | * ops vector. |
| 9335 | */ |
| 9336 | inode->i_fop = &btrfs_file_operations; |
| 9337 | inode->i_op = &btrfs_file_inode_operations; |
Chris Mason | b0d5d10 | 2014-09-08 13:08:51 -0700 | [diff] [blame] | 9338 | inode->i_mapping->a_ops = &btrfs_aops; |
| 9339 | inode->i_mapping->backing_dev_info = &root->fs_info->bdi; |
| 9340 | BTRFS_I(inode)->io_tree.ops = &btrfs_extent_io_ops; |
| 9341 | |
| 9342 | err = btrfs_init_inode_security(trans, inode, dir, &dentry->d_name); |
| 9343 | if (err) |
| 9344 | goto out_unlock_inode; |
Casey Schaufler | ad19db7 | 2011-12-15 10:09:07 -0500 | [diff] [blame] | 9345 | |
Josef Bacik | a1b075d | 2010-11-19 20:36:11 +0000 | [diff] [blame] | 9346 | err = btrfs_add_nondir(trans, dir, dentry, inode, 0, index); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 9347 | if (err) |
Chris Mason | b0d5d10 | 2014-09-08 13:08:51 -0700 | [diff] [blame] | 9348 | goto out_unlock_inode; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 9349 | |
| 9350 | path = btrfs_alloc_path(); |
Mark Fasheh | d8926bb | 2011-07-13 10:38:47 -0700 | [diff] [blame] | 9351 | if (!path) { |
| 9352 | err = -ENOMEM; |
Chris Mason | b0d5d10 | 2014-09-08 13:08:51 -0700 | [diff] [blame] | 9353 | goto out_unlock_inode; |
Mark Fasheh | d8926bb | 2011-07-13 10:38:47 -0700 | [diff] [blame] | 9354 | } |
Li Zefan | 33345d01 | 2011-04-20 10:31:50 +0800 | [diff] [blame] | 9355 | key.objectid = btrfs_ino(inode); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 9356 | key.offset = 0; |
David Sterba | 962a298 | 2014-06-04 18:41:45 +0200 | [diff] [blame] | 9357 | key.type = BTRFS_EXTENT_DATA_KEY; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 9358 | datasize = btrfs_file_extent_calc_inline_size(name_len); |
| 9359 | err = btrfs_insert_empty_item(trans, root, path, &key, |
| 9360 | datasize); |
Chris Mason | 54aa1f4 | 2007-06-22 14:16:25 -0400 | [diff] [blame] | 9361 | if (err) { |
Julia Lawall | b083916 | 2011-05-14 07:10:51 +0000 | [diff] [blame] | 9362 | btrfs_free_path(path); |
Chris Mason | b0d5d10 | 2014-09-08 13:08:51 -0700 | [diff] [blame] | 9363 | goto out_unlock_inode; |
Chris Mason | 54aa1f4 | 2007-06-22 14:16:25 -0400 | [diff] [blame] | 9364 | } |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 9365 | leaf = path->nodes[0]; |
| 9366 | ei = btrfs_item_ptr(leaf, path->slots[0], |
| 9367 | struct btrfs_file_extent_item); |
| 9368 | btrfs_set_file_extent_generation(leaf, ei, trans->transid); |
| 9369 | btrfs_set_file_extent_type(leaf, ei, |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 9370 | BTRFS_FILE_EXTENT_INLINE); |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 9371 | btrfs_set_file_extent_encryption(leaf, ei, 0); |
| 9372 | btrfs_set_file_extent_compression(leaf, ei, 0); |
| 9373 | btrfs_set_file_extent_other_encoding(leaf, ei, 0); |
| 9374 | btrfs_set_file_extent_ram_bytes(leaf, ei, name_len); |
| 9375 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 9376 | ptr = btrfs_file_extent_inline_start(ei); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 9377 | write_extent_buffer(leaf, symname, ptr, name_len); |
| 9378 | btrfs_mark_buffer_dirty(leaf); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 9379 | btrfs_free_path(path); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 9380 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 9381 | inode->i_op = &btrfs_symlink_inode_operations; |
| 9382 | inode->i_mapping->a_ops = &btrfs_symlink_aops; |
Chris Mason | 0416008 | 2008-03-26 10:28:07 -0400 | [diff] [blame] | 9383 | inode->i_mapping->backing_dev_info = &root->fs_info->bdi; |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 9384 | inode_set_bytes(inode, name_len); |
Filipe David Borba Manana | f06becc | 2013-09-16 09:53:28 +0100 | [diff] [blame] | 9385 | btrfs_i_size_write(inode, name_len); |
Chris Mason | 54aa1f4 | 2007-06-22 14:16:25 -0400 | [diff] [blame] | 9386 | err = btrfs_update_inode(trans, root, inode); |
Chris Mason | b0d5d10 | 2014-09-08 13:08:51 -0700 | [diff] [blame] | 9387 | if (err) { |
Chris Mason | 54aa1f4 | 2007-06-22 14:16:25 -0400 | [diff] [blame] | 9388 | drop_inode = 1; |
Chris Mason | b0d5d10 | 2014-09-08 13:08:51 -0700 | [diff] [blame] | 9389 | goto out_unlock_inode; |
| 9390 | } |
| 9391 | |
| 9392 | unlock_new_inode(inode); |
| 9393 | d_instantiate(dentry, inode); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 9394 | |
| 9395 | out_unlock: |
Josef Bacik | 7ad85bb | 2012-01-12 19:10:12 -0500 | [diff] [blame] | 9396 | btrfs_end_transaction(trans, root); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 9397 | if (drop_inode) { |
| 9398 | inode_dec_link_count(inode); |
| 9399 | iput(inode); |
| 9400 | } |
Liu Bo | b53d3f5 | 2012-11-14 14:34:34 +0000 | [diff] [blame] | 9401 | btrfs_btree_balance_dirty(root); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 9402 | return err; |
Chris Mason | b0d5d10 | 2014-09-08 13:08:51 -0700 | [diff] [blame] | 9403 | |
| 9404 | out_unlock_inode: |
| 9405 | drop_inode = 1; |
| 9406 | unlock_new_inode(inode); |
| 9407 | goto out_unlock; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 9408 | } |
Chris Mason | 1643298 | 2008-04-10 10:23:21 -0400 | [diff] [blame] | 9409 | |
Josef Bacik | 0af3d00 | 2010-06-21 14:48:16 -0400 | [diff] [blame] | 9410 | static int __btrfs_prealloc_file_range(struct inode *inode, int mode, |
| 9411 | u64 start, u64 num_bytes, u64 min_size, |
| 9412 | loff_t actual_len, u64 *alloc_hint, |
| 9413 | struct btrfs_trans_handle *trans) |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 9414 | { |
Josef Bacik | 5dc562c | 2012-08-17 13:14:17 -0400 | [diff] [blame] | 9415 | struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree; |
| 9416 | struct extent_map *em; |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 9417 | struct btrfs_root *root = BTRFS_I(inode)->root; |
| 9418 | struct btrfs_key ins; |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 9419 | u64 cur_offset = start; |
Josef Bacik | 55a61d1 | 2010-11-22 18:50:32 +0000 | [diff] [blame] | 9420 | u64 i_size; |
Chris Mason | 154ea28 | 2013-03-05 11:11:26 -0500 | [diff] [blame] | 9421 | u64 cur_bytes; |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 9422 | int ret = 0; |
Josef Bacik | 0af3d00 | 2010-06-21 14:48:16 -0400 | [diff] [blame] | 9423 | bool own_trans = true; |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 9424 | |
Josef Bacik | 0af3d00 | 2010-06-21 14:48:16 -0400 | [diff] [blame] | 9425 | if (trans) |
| 9426 | own_trans = false; |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 9427 | while (num_bytes > 0) { |
Josef Bacik | 0af3d00 | 2010-06-21 14:48:16 -0400 | [diff] [blame] | 9428 | if (own_trans) { |
| 9429 | trans = btrfs_start_transaction(root, 3); |
| 9430 | if (IS_ERR(trans)) { |
| 9431 | ret = PTR_ERR(trans); |
| 9432 | break; |
| 9433 | } |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 9434 | } |
Yan, Zheng | 5a303d5 | 2009-11-12 09:34:52 +0000 | [diff] [blame] | 9435 | |
Chris Mason | 154ea28 | 2013-03-05 11:11:26 -0500 | [diff] [blame] | 9436 | cur_bytes = min(num_bytes, 256ULL * 1024 * 1024); |
| 9437 | cur_bytes = max(cur_bytes, min_size); |
Josef Bacik | 0036158 | 2013-08-14 14:02:47 -0400 | [diff] [blame] | 9438 | ret = btrfs_reserve_extent(root, cur_bytes, min_size, 0, |
Miao Xie | e570fd2 | 2014-06-19 10:42:50 +0800 | [diff] [blame] | 9439 | *alloc_hint, &ins, 1, 0); |
Yan, Zheng | 5a303d5 | 2009-11-12 09:34:52 +0000 | [diff] [blame] | 9440 | if (ret) { |
Josef Bacik | 0af3d00 | 2010-06-21 14:48:16 -0400 | [diff] [blame] | 9441 | if (own_trans) |
| 9442 | btrfs_end_transaction(trans, root); |
Yan, Zheng | a22285a | 2010-05-16 10:48:46 -0400 | [diff] [blame] | 9443 | break; |
Yan, Zheng | 5a303d5 | 2009-11-12 09:34:52 +0000 | [diff] [blame] | 9444 | } |
| 9445 | |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 9446 | ret = insert_reserved_file_extent(trans, inode, |
| 9447 | cur_offset, ins.objectid, |
| 9448 | ins.offset, ins.offset, |
Yan, Zheng | 920bbbf | 2009-11-12 09:34:08 +0000 | [diff] [blame] | 9449 | ins.offset, 0, 0, 0, |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 9450 | BTRFS_FILE_EXTENT_PREALLOC); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 9451 | if (ret) { |
Josef Bacik | 857cc2f | 2013-10-07 15:21:08 -0400 | [diff] [blame] | 9452 | btrfs_free_reserved_extent(root, ins.objectid, |
Miao Xie | e570fd2 | 2014-06-19 10:42:50 +0800 | [diff] [blame] | 9453 | ins.offset, 0); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 9454 | btrfs_abort_transaction(trans, root, ret); |
| 9455 | if (own_trans) |
| 9456 | btrfs_end_transaction(trans, root); |
| 9457 | break; |
| 9458 | } |
Chris Mason | a1ed835 | 2009-09-11 12:27:37 -0400 | [diff] [blame] | 9459 | btrfs_drop_extent_cache(inode, cur_offset, |
| 9460 | cur_offset + ins.offset -1, 0); |
Yan, Zheng | 5a303d5 | 2009-11-12 09:34:52 +0000 | [diff] [blame] | 9461 | |
Josef Bacik | 5dc562c | 2012-08-17 13:14:17 -0400 | [diff] [blame] | 9462 | em = alloc_extent_map(); |
| 9463 | if (!em) { |
| 9464 | set_bit(BTRFS_INODE_NEEDS_FULL_SYNC, |
| 9465 | &BTRFS_I(inode)->runtime_flags); |
| 9466 | goto next; |
| 9467 | } |
| 9468 | |
| 9469 | em->start = cur_offset; |
| 9470 | em->orig_start = cur_offset; |
| 9471 | em->len = ins.offset; |
| 9472 | em->block_start = ins.objectid; |
| 9473 | em->block_len = ins.offset; |
Josef Bacik | b493968 | 2012-12-03 10:31:19 -0500 | [diff] [blame] | 9474 | em->orig_block_len = ins.offset; |
Josef Bacik | cc95bef | 2013-04-04 14:31:27 -0400 | [diff] [blame] | 9475 | em->ram_bytes = ins.offset; |
Josef Bacik | 5dc562c | 2012-08-17 13:14:17 -0400 | [diff] [blame] | 9476 | em->bdev = root->fs_info->fs_devices->latest_bdev; |
| 9477 | set_bit(EXTENT_FLAG_PREALLOC, &em->flags); |
| 9478 | em->generation = trans->transid; |
| 9479 | |
| 9480 | while (1) { |
| 9481 | write_lock(&em_tree->lock); |
Josef Bacik | 09a2a8f9 | 2013-04-05 16:51:15 -0400 | [diff] [blame] | 9482 | ret = add_extent_mapping(em_tree, em, 1); |
Josef Bacik | 5dc562c | 2012-08-17 13:14:17 -0400 | [diff] [blame] | 9483 | write_unlock(&em_tree->lock); |
| 9484 | if (ret != -EEXIST) |
| 9485 | break; |
| 9486 | btrfs_drop_extent_cache(inode, cur_offset, |
| 9487 | cur_offset + ins.offset - 1, |
| 9488 | 0); |
| 9489 | } |
| 9490 | free_extent_map(em); |
| 9491 | next: |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 9492 | num_bytes -= ins.offset; |
| 9493 | cur_offset += ins.offset; |
Yan, Zheng | efa5646 | 2010-05-16 10:49:59 -0400 | [diff] [blame] | 9494 | *alloc_hint = ins.objectid + ins.offset; |
Yan, Zheng | 5a303d5 | 2009-11-12 09:34:52 +0000 | [diff] [blame] | 9495 | |
Josef Bacik | 0c4d2d9 | 2012-04-05 15:03:02 -0400 | [diff] [blame] | 9496 | inode_inc_iversion(inode); |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 9497 | inode->i_ctime = CURRENT_TIME; |
Christoph Hellwig | 6cbff00 | 2009-04-17 10:37:41 +0200 | [diff] [blame] | 9498 | BTRFS_I(inode)->flags |= BTRFS_INODE_PREALLOC; |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 9499 | if (!(mode & FALLOC_FL_KEEP_SIZE) && |
Yan, Zheng | efa5646 | 2010-05-16 10:49:59 -0400 | [diff] [blame] | 9500 | (actual_len > inode->i_size) && |
| 9501 | (cur_offset > inode->i_size)) { |
Aneesh Kumar K.V | d1ea6a6 | 2010-01-20 07:28:54 +0000 | [diff] [blame] | 9502 | if (cur_offset > actual_len) |
Josef Bacik | 55a61d1 | 2010-11-22 18:50:32 +0000 | [diff] [blame] | 9503 | i_size = actual_len; |
Aneesh Kumar K.V | d1ea6a6 | 2010-01-20 07:28:54 +0000 | [diff] [blame] | 9504 | else |
Josef Bacik | 55a61d1 | 2010-11-22 18:50:32 +0000 | [diff] [blame] | 9505 | i_size = cur_offset; |
| 9506 | i_size_write(inode, i_size); |
| 9507 | btrfs_ordered_update_i_size(inode, i_size, NULL); |
Yan, Zheng | 5a303d5 | 2009-11-12 09:34:52 +0000 | [diff] [blame] | 9508 | } |
| 9509 | |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 9510 | ret = btrfs_update_inode(trans, root, inode); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 9511 | |
| 9512 | if (ret) { |
| 9513 | btrfs_abort_transaction(trans, root, ret); |
| 9514 | if (own_trans) |
| 9515 | btrfs_end_transaction(trans, root); |
| 9516 | break; |
| 9517 | } |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 9518 | |
Josef Bacik | 0af3d00 | 2010-06-21 14:48:16 -0400 | [diff] [blame] | 9519 | if (own_trans) |
| 9520 | btrfs_end_transaction(trans, root); |
Yan, Zheng | 5a303d5 | 2009-11-12 09:34:52 +0000 | [diff] [blame] | 9521 | } |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 9522 | return ret; |
| 9523 | } |
| 9524 | |
Josef Bacik | 0af3d00 | 2010-06-21 14:48:16 -0400 | [diff] [blame] | 9525 | int btrfs_prealloc_file_range(struct inode *inode, int mode, |
| 9526 | u64 start, u64 num_bytes, u64 min_size, |
| 9527 | loff_t actual_len, u64 *alloc_hint) |
| 9528 | { |
| 9529 | return __btrfs_prealloc_file_range(inode, mode, start, num_bytes, |
| 9530 | min_size, actual_len, alloc_hint, |
| 9531 | NULL); |
| 9532 | } |
| 9533 | |
| 9534 | int btrfs_prealloc_file_range_trans(struct inode *inode, |
| 9535 | struct btrfs_trans_handle *trans, int mode, |
| 9536 | u64 start, u64 num_bytes, u64 min_size, |
| 9537 | loff_t actual_len, u64 *alloc_hint) |
| 9538 | { |
| 9539 | return __btrfs_prealloc_file_range(inode, mode, start, num_bytes, |
| 9540 | min_size, actual_len, alloc_hint, trans); |
| 9541 | } |
| 9542 | |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 9543 | static int btrfs_set_page_dirty(struct page *page) |
| 9544 | { |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 9545 | return __set_page_dirty_nobuffers(page); |
| 9546 | } |
| 9547 | |
Al Viro | 10556cb | 2011-06-20 19:28:19 -0400 | [diff] [blame] | 9548 | static int btrfs_permission(struct inode *inode, int mask) |
Yan | fdebe2b | 2008-01-14 13:26:08 -0500 | [diff] [blame] | 9549 | { |
Li Zefan | b83cc96 | 2010-12-20 16:04:08 +0800 | [diff] [blame] | 9550 | struct btrfs_root *root = BTRFS_I(inode)->root; |
Jeff Mahoney | cb6db4e | 2011-08-15 17:27:21 +0000 | [diff] [blame] | 9551 | umode_t mode = inode->i_mode; |
Li Zefan | b83cc96 | 2010-12-20 16:04:08 +0800 | [diff] [blame] | 9552 | |
Jeff Mahoney | cb6db4e | 2011-08-15 17:27:21 +0000 | [diff] [blame] | 9553 | if (mask & MAY_WRITE && |
| 9554 | (S_ISREG(mode) || S_ISDIR(mode) || S_ISLNK(mode))) { |
| 9555 | if (btrfs_root_readonly(root)) |
| 9556 | return -EROFS; |
| 9557 | if (BTRFS_I(inode)->flags & BTRFS_INODE_READONLY) |
| 9558 | return -EACCES; |
| 9559 | } |
Al Viro | 2830ba7 | 2011-06-20 19:16:29 -0400 | [diff] [blame] | 9560 | return generic_permission(inode, mask); |
Yan | fdebe2b | 2008-01-14 13:26:08 -0500 | [diff] [blame] | 9561 | } |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 9562 | |
Filipe Manana | ef3b9af | 2014-04-27 20:40:45 +0100 | [diff] [blame] | 9563 | static int btrfs_tmpfile(struct inode *dir, struct dentry *dentry, umode_t mode) |
| 9564 | { |
| 9565 | struct btrfs_trans_handle *trans; |
| 9566 | struct btrfs_root *root = BTRFS_I(dir)->root; |
| 9567 | struct inode *inode = NULL; |
| 9568 | u64 objectid; |
| 9569 | u64 index; |
| 9570 | int ret = 0; |
| 9571 | |
| 9572 | /* |
| 9573 | * 5 units required for adding orphan entry |
| 9574 | */ |
| 9575 | trans = btrfs_start_transaction(root, 5); |
| 9576 | if (IS_ERR(trans)) |
| 9577 | return PTR_ERR(trans); |
| 9578 | |
| 9579 | ret = btrfs_find_free_ino(root, &objectid); |
| 9580 | if (ret) |
| 9581 | goto out; |
| 9582 | |
| 9583 | inode = btrfs_new_inode(trans, root, dir, NULL, 0, |
| 9584 | btrfs_ino(dir), objectid, mode, &index); |
| 9585 | if (IS_ERR(inode)) { |
| 9586 | ret = PTR_ERR(inode); |
| 9587 | inode = NULL; |
| 9588 | goto out; |
| 9589 | } |
| 9590 | |
Filipe Manana | ef3b9af | 2014-04-27 20:40:45 +0100 | [diff] [blame] | 9591 | inode->i_fop = &btrfs_file_operations; |
| 9592 | inode->i_op = &btrfs_file_inode_operations; |
| 9593 | |
| 9594 | inode->i_mapping->a_ops = &btrfs_aops; |
| 9595 | inode->i_mapping->backing_dev_info = &root->fs_info->bdi; |
| 9596 | BTRFS_I(inode)->io_tree.ops = &btrfs_extent_io_ops; |
| 9597 | |
Chris Mason | b0d5d10 | 2014-09-08 13:08:51 -0700 | [diff] [blame] | 9598 | ret = btrfs_init_inode_security(trans, inode, dir, NULL); |
| 9599 | if (ret) |
| 9600 | goto out_inode; |
| 9601 | |
| 9602 | ret = btrfs_update_inode(trans, root, inode); |
| 9603 | if (ret) |
| 9604 | goto out_inode; |
Filipe Manana | ef3b9af | 2014-04-27 20:40:45 +0100 | [diff] [blame] | 9605 | ret = btrfs_orphan_add(trans, inode); |
| 9606 | if (ret) |
Chris Mason | b0d5d10 | 2014-09-08 13:08:51 -0700 | [diff] [blame] | 9607 | goto out_inode; |
Filipe Manana | ef3b9af | 2014-04-27 20:40:45 +0100 | [diff] [blame] | 9608 | |
Filipe Manana | 5762b5c | 2014-08-01 00:10:32 +0100 | [diff] [blame] | 9609 | /* |
| 9610 | * We set number of links to 0 in btrfs_new_inode(), and here we set |
| 9611 | * it to 1 because d_tmpfile() will issue a warning if the count is 0, |
| 9612 | * through: |
| 9613 | * |
| 9614 | * d_tmpfile() -> inode_dec_link_count() -> drop_nlink() |
| 9615 | */ |
| 9616 | set_nlink(inode, 1); |
Chris Mason | b0d5d10 | 2014-09-08 13:08:51 -0700 | [diff] [blame] | 9617 | unlock_new_inode(inode); |
Filipe Manana | ef3b9af | 2014-04-27 20:40:45 +0100 | [diff] [blame] | 9618 | d_tmpfile(dentry, inode); |
| 9619 | mark_inode_dirty(inode); |
| 9620 | |
| 9621 | out: |
| 9622 | btrfs_end_transaction(trans, root); |
| 9623 | if (ret) |
| 9624 | iput(inode); |
| 9625 | btrfs_balance_delayed_items(root); |
| 9626 | btrfs_btree_balance_dirty(root); |
Filipe Manana | ef3b9af | 2014-04-27 20:40:45 +0100 | [diff] [blame] | 9627 | return ret; |
Chris Mason | b0d5d10 | 2014-09-08 13:08:51 -0700 | [diff] [blame] | 9628 | |
| 9629 | out_inode: |
| 9630 | unlock_new_inode(inode); |
| 9631 | goto out; |
| 9632 | |
Filipe Manana | ef3b9af | 2014-04-27 20:40:45 +0100 | [diff] [blame] | 9633 | } |
| 9634 | |
Filipe Manana | b38ef71 | 2014-11-13 17:01:45 +0000 | [diff] [blame] | 9635 | /* Inspired by filemap_check_errors() */ |
| 9636 | int btrfs_inode_check_errors(struct inode *inode) |
| 9637 | { |
| 9638 | int ret = 0; |
| 9639 | |
| 9640 | if (test_bit(AS_ENOSPC, &inode->i_mapping->flags) && |
| 9641 | test_and_clear_bit(AS_ENOSPC, &inode->i_mapping->flags)) |
| 9642 | ret = -ENOSPC; |
| 9643 | if (test_bit(AS_EIO, &inode->i_mapping->flags) && |
| 9644 | test_and_clear_bit(AS_EIO, &inode->i_mapping->flags)) |
| 9645 | ret = -EIO; |
| 9646 | |
| 9647 | return ret; |
| 9648 | } |
| 9649 | |
Alexey Dobriyan | 6e1d5dc | 2009-09-21 17:01:11 -0700 | [diff] [blame] | 9650 | static const struct inode_operations btrfs_dir_inode_operations = { |
Chris Mason | 3394e16 | 2008-11-17 20:42:26 -0500 | [diff] [blame] | 9651 | .getattr = btrfs_getattr, |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 9652 | .lookup = btrfs_lookup, |
| 9653 | .create = btrfs_create, |
| 9654 | .unlink = btrfs_unlink, |
| 9655 | .link = btrfs_link, |
| 9656 | .mkdir = btrfs_mkdir, |
| 9657 | .rmdir = btrfs_rmdir, |
Miklos Szeredi | 80ace85 | 2014-07-23 15:15:32 +0200 | [diff] [blame] | 9658 | .rename2 = btrfs_rename2, |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 9659 | .symlink = btrfs_symlink, |
| 9660 | .setattr = btrfs_setattr, |
Josef Bacik | 618e21d | 2007-07-11 10:18:17 -0400 | [diff] [blame] | 9661 | .mknod = btrfs_mknod, |
Christoph Hellwig | 95819c0 | 2008-08-28 06:21:17 -0400 | [diff] [blame] | 9662 | .setxattr = btrfs_setxattr, |
| 9663 | .getxattr = btrfs_getxattr, |
Josef Bacik | 5103e94 | 2007-11-16 11:45:54 -0500 | [diff] [blame] | 9664 | .listxattr = btrfs_listxattr, |
Christoph Hellwig | 95819c0 | 2008-08-28 06:21:17 -0400 | [diff] [blame] | 9665 | .removexattr = btrfs_removexattr, |
Yan | fdebe2b | 2008-01-14 13:26:08 -0500 | [diff] [blame] | 9666 | .permission = btrfs_permission, |
Christoph Hellwig | 4e34e71 | 2011-07-23 17:37:31 +0200 | [diff] [blame] | 9667 | .get_acl = btrfs_get_acl, |
Christoph Hellwig | 996a710 | 2013-12-20 05:16:43 -0800 | [diff] [blame] | 9668 | .set_acl = btrfs_set_acl, |
Guangyu Sun | 93fd63c | 2013-09-16 10:42:03 -0700 | [diff] [blame] | 9669 | .update_time = btrfs_update_time, |
Filipe Manana | ef3b9af | 2014-04-27 20:40:45 +0100 | [diff] [blame] | 9670 | .tmpfile = btrfs_tmpfile, |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 9671 | }; |
Alexey Dobriyan | 6e1d5dc | 2009-09-21 17:01:11 -0700 | [diff] [blame] | 9672 | static const struct inode_operations btrfs_dir_ro_inode_operations = { |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 9673 | .lookup = btrfs_lookup, |
Yan | fdebe2b | 2008-01-14 13:26:08 -0500 | [diff] [blame] | 9674 | .permission = btrfs_permission, |
Christoph Hellwig | 4e34e71 | 2011-07-23 17:37:31 +0200 | [diff] [blame] | 9675 | .get_acl = btrfs_get_acl, |
Christoph Hellwig | 996a710 | 2013-12-20 05:16:43 -0800 | [diff] [blame] | 9676 | .set_acl = btrfs_set_acl, |
Guangyu Sun | 93fd63c | 2013-09-16 10:42:03 -0700 | [diff] [blame] | 9677 | .update_time = btrfs_update_time, |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 9678 | }; |
Yan, Zheng | 76dda93 | 2009-09-21 16:00:26 -0400 | [diff] [blame] | 9679 | |
Alexey Dobriyan | 828c095 | 2009-10-01 15:43:56 -0700 | [diff] [blame] | 9680 | static const struct file_operations btrfs_dir_file_operations = { |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 9681 | .llseek = generic_file_llseek, |
| 9682 | .read = generic_read_dir, |
Al Viro | 9cdda8d | 2013-05-22 16:48:09 -0400 | [diff] [blame] | 9683 | .iterate = btrfs_real_readdir, |
Christoph Hellwig | 34287aa | 2007-09-14 10:22:47 -0400 | [diff] [blame] | 9684 | .unlocked_ioctl = btrfs_ioctl, |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 9685 | #ifdef CONFIG_COMPAT |
Christoph Hellwig | 34287aa | 2007-09-14 10:22:47 -0400 | [diff] [blame] | 9686 | .compat_ioctl = btrfs_ioctl, |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 9687 | #endif |
Sage Weil | 6bf13c0 | 2008-06-10 10:07:39 -0400 | [diff] [blame] | 9688 | .release = btrfs_release_file, |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 9689 | .fsync = btrfs_sync_file, |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 9690 | }; |
| 9691 | |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 9692 | static struct extent_io_ops btrfs_extent_io_ops = { |
Chris Mason | 07157aa | 2007-08-30 08:50:51 -0400 | [diff] [blame] | 9693 | .fill_delalloc = run_delalloc_range, |
Chris Mason | 065631f | 2008-02-20 12:07:25 -0500 | [diff] [blame] | 9694 | .submit_bio_hook = btrfs_submit_bio_hook, |
Chris Mason | 239b14b | 2008-03-24 15:02:07 -0400 | [diff] [blame] | 9695 | .merge_bio_hook = btrfs_merge_bio_hook, |
Chris Mason | 07157aa | 2007-08-30 08:50:51 -0400 | [diff] [blame] | 9696 | .readpage_end_io_hook = btrfs_readpage_end_io_hook, |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 9697 | .writepage_end_io_hook = btrfs_writepage_end_io_hook, |
Chris Mason | 247e743 | 2008-07-17 12:53:51 -0400 | [diff] [blame] | 9698 | .writepage_start_hook = btrfs_writepage_start_hook, |
Chris Mason | b0c68f8 | 2008-01-31 11:05:37 -0500 | [diff] [blame] | 9699 | .set_bit_hook = btrfs_set_bit_hook, |
| 9700 | .clear_bit_hook = btrfs_clear_bit_hook, |
Josef Bacik | 9ed74f2 | 2009-09-11 16:12:44 -0400 | [diff] [blame] | 9701 | .merge_extent_hook = btrfs_merge_extent_hook, |
| 9702 | .split_extent_hook = btrfs_split_extent_hook, |
Chris Mason | 07157aa | 2007-08-30 08:50:51 -0400 | [diff] [blame] | 9703 | }; |
| 9704 | |
Chris Mason | 3505439 | 2009-01-21 13:11:13 -0500 | [diff] [blame] | 9705 | /* |
| 9706 | * btrfs doesn't support the bmap operation because swapfiles |
| 9707 | * use bmap to make a mapping of extents in the file. They assume |
| 9708 | * these extents won't change over the life of the file and they |
| 9709 | * use the bmap result to do IO directly to the drive. |
| 9710 | * |
| 9711 | * the btrfs bmap call would return logical addresses that aren't |
| 9712 | * suitable for IO and they also will change frequently as COW |
| 9713 | * operations happen. So, swapfile + btrfs == corruption. |
| 9714 | * |
| 9715 | * For now we're avoiding this by dropping bmap. |
| 9716 | */ |
Alexey Dobriyan | 7f09410 | 2009-09-21 17:01:10 -0700 | [diff] [blame] | 9717 | static const struct address_space_operations btrfs_aops = { |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 9718 | .readpage = btrfs_readpage, |
| 9719 | .writepage = btrfs_writepage, |
Chris Mason | b293f02 | 2007-11-01 19:45:34 -0400 | [diff] [blame] | 9720 | .writepages = btrfs_writepages, |
Chris Mason | 3ab2fb5 | 2007-11-08 10:59:22 -0500 | [diff] [blame] | 9721 | .readpages = btrfs_readpages, |
Chris Mason | 1643298 | 2008-04-10 10:23:21 -0400 | [diff] [blame] | 9722 | .direct_IO = btrfs_direct_IO, |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 9723 | .invalidatepage = btrfs_invalidatepage, |
| 9724 | .releasepage = btrfs_releasepage, |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 9725 | .set_page_dirty = btrfs_set_page_dirty, |
Andi Kleen | 465fdd9 | 2009-09-16 11:50:18 +0200 | [diff] [blame] | 9726 | .error_remove_page = generic_error_remove_page, |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 9727 | }; |
| 9728 | |
Alexey Dobriyan | 7f09410 | 2009-09-21 17:01:10 -0700 | [diff] [blame] | 9729 | static const struct address_space_operations btrfs_symlink_aops = { |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 9730 | .readpage = btrfs_readpage, |
| 9731 | .writepage = btrfs_writepage, |
Chris Mason | 2bf5a72 | 2007-08-30 11:54:02 -0400 | [diff] [blame] | 9732 | .invalidatepage = btrfs_invalidatepage, |
| 9733 | .releasepage = btrfs_releasepage, |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 9734 | }; |
| 9735 | |
Alexey Dobriyan | 6e1d5dc | 2009-09-21 17:01:11 -0700 | [diff] [blame] | 9736 | static const struct inode_operations btrfs_file_inode_operations = { |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 9737 | .getattr = btrfs_getattr, |
| 9738 | .setattr = btrfs_setattr, |
Christoph Hellwig | 95819c0 | 2008-08-28 06:21:17 -0400 | [diff] [blame] | 9739 | .setxattr = btrfs_setxattr, |
| 9740 | .getxattr = btrfs_getxattr, |
Josef Bacik | 5103e94 | 2007-11-16 11:45:54 -0500 | [diff] [blame] | 9741 | .listxattr = btrfs_listxattr, |
Christoph Hellwig | 95819c0 | 2008-08-28 06:21:17 -0400 | [diff] [blame] | 9742 | .removexattr = btrfs_removexattr, |
Yan | fdebe2b | 2008-01-14 13:26:08 -0500 | [diff] [blame] | 9743 | .permission = btrfs_permission, |
Yehuda Sadeh | 1506fcc | 2009-01-21 14:39:14 -0500 | [diff] [blame] | 9744 | .fiemap = btrfs_fiemap, |
Christoph Hellwig | 4e34e71 | 2011-07-23 17:37:31 +0200 | [diff] [blame] | 9745 | .get_acl = btrfs_get_acl, |
Christoph Hellwig | 996a710 | 2013-12-20 05:16:43 -0800 | [diff] [blame] | 9746 | .set_acl = btrfs_set_acl, |
Josef Bacik | e41f941 | 2012-03-26 09:46:47 -0400 | [diff] [blame] | 9747 | .update_time = btrfs_update_time, |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 9748 | }; |
Alexey Dobriyan | 6e1d5dc | 2009-09-21 17:01:11 -0700 | [diff] [blame] | 9749 | static const struct inode_operations btrfs_special_inode_operations = { |
Josef Bacik | 618e21d | 2007-07-11 10:18:17 -0400 | [diff] [blame] | 9750 | .getattr = btrfs_getattr, |
| 9751 | .setattr = btrfs_setattr, |
Yan | fdebe2b | 2008-01-14 13:26:08 -0500 | [diff] [blame] | 9752 | .permission = btrfs_permission, |
Christoph Hellwig | 95819c0 | 2008-08-28 06:21:17 -0400 | [diff] [blame] | 9753 | .setxattr = btrfs_setxattr, |
| 9754 | .getxattr = btrfs_getxattr, |
Josef Bacik | 33268ea | 2008-07-24 12:16:36 -0400 | [diff] [blame] | 9755 | .listxattr = btrfs_listxattr, |
Christoph Hellwig | 95819c0 | 2008-08-28 06:21:17 -0400 | [diff] [blame] | 9756 | .removexattr = btrfs_removexattr, |
Christoph Hellwig | 4e34e71 | 2011-07-23 17:37:31 +0200 | [diff] [blame] | 9757 | .get_acl = btrfs_get_acl, |
Christoph Hellwig | 996a710 | 2013-12-20 05:16:43 -0800 | [diff] [blame] | 9758 | .set_acl = btrfs_set_acl, |
Josef Bacik | e41f941 | 2012-03-26 09:46:47 -0400 | [diff] [blame] | 9759 | .update_time = btrfs_update_time, |
Josef Bacik | 618e21d | 2007-07-11 10:18:17 -0400 | [diff] [blame] | 9760 | }; |
Alexey Dobriyan | 6e1d5dc | 2009-09-21 17:01:11 -0700 | [diff] [blame] | 9761 | static const struct inode_operations btrfs_symlink_inode_operations = { |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 9762 | .readlink = generic_readlink, |
| 9763 | .follow_link = page_follow_link_light, |
| 9764 | .put_link = page_put_link, |
Li Zefan | f209561 | 2010-11-19 02:05:24 +0000 | [diff] [blame] | 9765 | .getattr = btrfs_getattr, |
Josef Bacik | 22c44fe | 2011-11-30 10:45:38 -0500 | [diff] [blame] | 9766 | .setattr = btrfs_setattr, |
Yan | fdebe2b | 2008-01-14 13:26:08 -0500 | [diff] [blame] | 9767 | .permission = btrfs_permission, |
Jim Owens | 0279b4c | 2009-02-04 09:29:13 -0500 | [diff] [blame] | 9768 | .setxattr = btrfs_setxattr, |
| 9769 | .getxattr = btrfs_getxattr, |
| 9770 | .listxattr = btrfs_listxattr, |
| 9771 | .removexattr = btrfs_removexattr, |
Josef Bacik | e41f941 | 2012-03-26 09:46:47 -0400 | [diff] [blame] | 9772 | .update_time = btrfs_update_time, |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 9773 | }; |
Yan, Zheng | 76dda93 | 2009-09-21 16:00:26 -0400 | [diff] [blame] | 9774 | |
Alexey Dobriyan | 82d339d | 2009-10-09 09:54:36 -0400 | [diff] [blame] | 9775 | const struct dentry_operations btrfs_dentry_operations = { |
Yan, Zheng | 76dda93 | 2009-09-21 16:00:26 -0400 | [diff] [blame] | 9776 | .d_delete = btrfs_dentry_delete, |
Josef Bacik | b4aff1f | 2011-06-28 16:18:59 -0400 | [diff] [blame] | 9777 | .d_release = btrfs_dentry_release, |
Yan, Zheng | 76dda93 | 2009-09-21 16:00:26 -0400 | [diff] [blame] | 9778 | }; |