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 | |
Yan, Zheng | f34f57a | 2009-11-12 09:35:27 +0000 | [diff] [blame] | 111 | static int btrfs_init_inode_security(struct btrfs_trans_handle *trans, |
Eric Paris | 2a7dba3 | 2011-02-01 11:05:39 -0500 | [diff] [blame] | 112 | struct inode *inode, struct inode *dir, |
| 113 | const struct qstr *qstr) |
Jim Owens | 0279b4c | 2009-02-04 09:29:13 -0500 | [diff] [blame] | 114 | { |
| 115 | int err; |
| 116 | |
Yan, Zheng | f34f57a | 2009-11-12 09:35:27 +0000 | [diff] [blame] | 117 | err = btrfs_init_acl(trans, inode, dir); |
Jim Owens | 0279b4c | 2009-02-04 09:29:13 -0500 | [diff] [blame] | 118 | if (!err) |
Eric Paris | 2a7dba3 | 2011-02-01 11:05:39 -0500 | [diff] [blame] | 119 | err = btrfs_xattr_security_init(trans, inode, dir, qstr); |
Jim Owens | 0279b4c | 2009-02-04 09:29:13 -0500 | [diff] [blame] | 120 | return err; |
| 121 | } |
| 122 | |
Chris Mason | d352ac6 | 2008-09-29 15:18:18 -0400 | [diff] [blame] | 123 | /* |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 124 | * this does all the hard work for inserting an inline extent into |
| 125 | * the btree. The caller should have done a btrfs_drop_extents so that |
| 126 | * no overlapping inline items exist in the btree |
| 127 | */ |
Chris Mason | 40f7658 | 2014-05-21 13:35:51 -0700 | [diff] [blame] | 128 | static int insert_inline_extent(struct btrfs_trans_handle *trans, |
Filipe David Borba Manana | 1acae57 | 2014-01-07 11:42:27 +0000 | [diff] [blame] | 129 | struct btrfs_path *path, int extent_inserted, |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 130 | struct btrfs_root *root, struct inode *inode, |
| 131 | u64 start, size_t size, size_t compressed_size, |
Li Zefan | fe3f566 | 2011-03-28 08:30:38 +0000 | [diff] [blame] | 132 | int compress_type, |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 133 | struct page **compressed_pages) |
| 134 | { |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 135 | struct extent_buffer *leaf; |
| 136 | struct page *page = NULL; |
| 137 | char *kaddr; |
| 138 | unsigned long ptr; |
| 139 | struct btrfs_file_extent_item *ei; |
| 140 | int err = 0; |
| 141 | int ret; |
| 142 | size_t cur_size = size; |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 143 | unsigned long offset; |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 144 | |
Li Zefan | fe3f566 | 2011-03-28 08:30:38 +0000 | [diff] [blame] | 145 | if (compressed_size && compressed_pages) |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 146 | cur_size = compressed_size; |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 147 | |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 148 | inode_add_bytes(inode, size); |
Filipe David Borba Manana | 1acae57 | 2014-01-07 11:42:27 +0000 | [diff] [blame] | 149 | |
| 150 | if (!extent_inserted) { |
| 151 | struct btrfs_key key; |
| 152 | size_t datasize; |
| 153 | |
| 154 | key.objectid = btrfs_ino(inode); |
| 155 | key.offset = start; |
David Sterba | 962a298 | 2014-06-04 18:41:45 +0200 | [diff] [blame] | 156 | key.type = BTRFS_EXTENT_DATA_KEY; |
Filipe David Borba Manana | 1acae57 | 2014-01-07 11:42:27 +0000 | [diff] [blame] | 157 | |
| 158 | datasize = btrfs_file_extent_calc_inline_size(cur_size); |
| 159 | path->leave_spinning = 1; |
| 160 | ret = btrfs_insert_empty_item(trans, root, path, &key, |
| 161 | datasize); |
| 162 | if (ret) { |
| 163 | err = ret; |
| 164 | goto fail; |
| 165 | } |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 166 | } |
| 167 | leaf = path->nodes[0]; |
| 168 | ei = btrfs_item_ptr(leaf, path->slots[0], |
| 169 | struct btrfs_file_extent_item); |
| 170 | btrfs_set_file_extent_generation(leaf, ei, trans->transid); |
| 171 | btrfs_set_file_extent_type(leaf, ei, BTRFS_FILE_EXTENT_INLINE); |
| 172 | btrfs_set_file_extent_encryption(leaf, ei, 0); |
| 173 | btrfs_set_file_extent_other_encoding(leaf, ei, 0); |
| 174 | btrfs_set_file_extent_ram_bytes(leaf, ei, size); |
| 175 | ptr = btrfs_file_extent_inline_start(ei); |
| 176 | |
Li Zefan | 261507a0 | 2010-12-17 14:21:50 +0800 | [diff] [blame] | 177 | if (compress_type != BTRFS_COMPRESS_NONE) { |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 178 | struct page *cpage; |
| 179 | int i = 0; |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 180 | while (compressed_size > 0) { |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 181 | cpage = compressed_pages[i]; |
Chris Mason | 5b050f0 | 2008-11-11 09:34:41 -0500 | [diff] [blame] | 182 | cur_size = min_t(unsigned long, compressed_size, |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 183 | PAGE_CACHE_SIZE); |
| 184 | |
Cong Wang | 7ac687d | 2011-11-25 23:14:28 +0800 | [diff] [blame] | 185 | kaddr = kmap_atomic(cpage); |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 186 | write_extent_buffer(leaf, kaddr, ptr, cur_size); |
Cong Wang | 7ac687d | 2011-11-25 23:14:28 +0800 | [diff] [blame] | 187 | kunmap_atomic(kaddr); |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 188 | |
| 189 | i++; |
| 190 | ptr += cur_size; |
| 191 | compressed_size -= cur_size; |
| 192 | } |
| 193 | btrfs_set_file_extent_compression(leaf, ei, |
Li Zefan | 261507a0 | 2010-12-17 14:21:50 +0800 | [diff] [blame] | 194 | compress_type); |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 195 | } else { |
| 196 | page = find_get_page(inode->i_mapping, |
| 197 | start >> PAGE_CACHE_SHIFT); |
| 198 | btrfs_set_file_extent_compression(leaf, ei, 0); |
Cong Wang | 7ac687d | 2011-11-25 23:14:28 +0800 | [diff] [blame] | 199 | kaddr = kmap_atomic(page); |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 200 | offset = start & (PAGE_CACHE_SIZE - 1); |
| 201 | write_extent_buffer(leaf, kaddr + offset, ptr, size); |
Cong Wang | 7ac687d | 2011-11-25 23:14:28 +0800 | [diff] [blame] | 202 | kunmap_atomic(kaddr); |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 203 | page_cache_release(page); |
| 204 | } |
| 205 | btrfs_mark_buffer_dirty(leaf); |
Filipe David Borba Manana | 1acae57 | 2014-01-07 11:42:27 +0000 | [diff] [blame] | 206 | btrfs_release_path(path); |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 207 | |
Yan, Zheng | c216775 | 2009-11-12 09:34:21 +0000 | [diff] [blame] | 208 | /* |
| 209 | * we're an inline extent, so nobody can |
| 210 | * extend the file past i_size without locking |
| 211 | * a page we already have locked. |
| 212 | * |
| 213 | * We must do any isize and inode updates |
| 214 | * before we unlock the pages. Otherwise we |
| 215 | * could end up racing with unlink. |
| 216 | */ |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 217 | BTRFS_I(inode)->disk_i_size = inode->i_size; |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 218 | ret = btrfs_update_inode(trans, root, inode); |
Yan, Zheng | c216775 | 2009-11-12 09:34:21 +0000 | [diff] [blame] | 219 | |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 220 | return ret; |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 221 | fail: |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 222 | return err; |
| 223 | } |
| 224 | |
| 225 | |
| 226 | /* |
| 227 | * conditionally insert an inline extent into the file. This |
| 228 | * does the checks required to make sure the data is small enough |
| 229 | * to fit as an inline extent. |
| 230 | */ |
Josef Bacik | 0036158 | 2013-08-14 14:02:47 -0400 | [diff] [blame] | 231 | static noinline int cow_file_range_inline(struct btrfs_root *root, |
| 232 | struct inode *inode, u64 start, |
| 233 | u64 end, size_t compressed_size, |
| 234 | int compress_type, |
| 235 | struct page **compressed_pages) |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 236 | { |
Josef Bacik | 0036158 | 2013-08-14 14:02:47 -0400 | [diff] [blame] | 237 | struct btrfs_trans_handle *trans; |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 238 | u64 isize = i_size_read(inode); |
| 239 | u64 actual_end = min(end + 1, isize); |
| 240 | u64 inline_len = actual_end - start; |
Qu Wenruo | fda2832 | 2013-02-26 08:10:22 +0000 | [diff] [blame] | 241 | u64 aligned_end = ALIGN(end, root->sectorsize); |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 242 | u64 data_len = inline_len; |
| 243 | int ret; |
Filipe David Borba Manana | 1acae57 | 2014-01-07 11:42:27 +0000 | [diff] [blame] | 244 | struct btrfs_path *path; |
| 245 | int extent_inserted = 0; |
| 246 | u32 extent_item_size; |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 247 | |
| 248 | if (compressed_size) |
| 249 | data_len = compressed_size; |
| 250 | |
| 251 | if (start > 0 || |
Wang Shilong | 354877b | 2014-07-17 11:44:11 +0800 | [diff] [blame] | 252 | actual_end > PAGE_CACHE_SIZE || |
| 253 | data_len > BTRFS_MAX_INLINE_DATA_SIZE(root) || |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 254 | (!compressed_size && |
| 255 | (actual_end & (root->sectorsize - 1)) == 0) || |
| 256 | end + 1 < isize || |
| 257 | data_len > root->fs_info->max_inline) { |
| 258 | return 1; |
| 259 | } |
| 260 | |
Filipe David Borba Manana | 1acae57 | 2014-01-07 11:42:27 +0000 | [diff] [blame] | 261 | path = btrfs_alloc_path(); |
| 262 | if (!path) |
| 263 | return -ENOMEM; |
| 264 | |
Josef Bacik | 0036158 | 2013-08-14 14:02:47 -0400 | [diff] [blame] | 265 | trans = btrfs_join_transaction(root); |
Filipe David Borba Manana | 1acae57 | 2014-01-07 11:42:27 +0000 | [diff] [blame] | 266 | if (IS_ERR(trans)) { |
| 267 | btrfs_free_path(path); |
Josef Bacik | 0036158 | 2013-08-14 14:02:47 -0400 | [diff] [blame] | 268 | return PTR_ERR(trans); |
Filipe David Borba Manana | 1acae57 | 2014-01-07 11:42:27 +0000 | [diff] [blame] | 269 | } |
Josef Bacik | 0036158 | 2013-08-14 14:02:47 -0400 | [diff] [blame] | 270 | trans->block_rsv = &root->fs_info->delalloc_block_rsv; |
| 271 | |
Filipe David Borba Manana | 1acae57 | 2014-01-07 11:42:27 +0000 | [diff] [blame] | 272 | if (compressed_size && compressed_pages) |
| 273 | extent_item_size = btrfs_file_extent_calc_inline_size( |
| 274 | compressed_size); |
| 275 | else |
| 276 | extent_item_size = btrfs_file_extent_calc_inline_size( |
| 277 | inline_len); |
| 278 | |
| 279 | ret = __btrfs_drop_extents(trans, root, inode, path, |
| 280 | start, aligned_end, NULL, |
| 281 | 1, 1, extent_item_size, &extent_inserted); |
Josef Bacik | 0036158 | 2013-08-14 14:02:47 -0400 | [diff] [blame] | 282 | if (ret) { |
| 283 | btrfs_abort_transaction(trans, root, ret); |
| 284 | goto out; |
| 285 | } |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 286 | |
| 287 | if (isize > actual_end) |
| 288 | inline_len = min_t(u64, isize, actual_end); |
Filipe David Borba Manana | 1acae57 | 2014-01-07 11:42:27 +0000 | [diff] [blame] | 289 | ret = insert_inline_extent(trans, path, extent_inserted, |
| 290 | root, inode, start, |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 291 | inline_len, compressed_size, |
Li Zefan | fe3f566 | 2011-03-28 08:30:38 +0000 | [diff] [blame] | 292 | compress_type, compressed_pages); |
Josef Bacik | 2adcac1 | 2012-05-23 16:10:14 -0400 | [diff] [blame] | 293 | if (ret && ret != -ENOSPC) { |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 294 | btrfs_abort_transaction(trans, root, ret); |
Josef Bacik | 0036158 | 2013-08-14 14:02:47 -0400 | [diff] [blame] | 295 | goto out; |
Josef Bacik | 2adcac1 | 2012-05-23 16:10:14 -0400 | [diff] [blame] | 296 | } else if (ret == -ENOSPC) { |
Josef Bacik | 0036158 | 2013-08-14 14:02:47 -0400 | [diff] [blame] | 297 | ret = 1; |
| 298 | goto out; |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 299 | } |
Josef Bacik | 2adcac1 | 2012-05-23 16:10:14 -0400 | [diff] [blame] | 300 | |
Josef Bacik | bdc20e6 | 2013-02-28 13:23:38 -0500 | [diff] [blame] | 301 | set_bit(BTRFS_INODE_NEEDS_FULL_SYNC, &BTRFS_I(inode)->runtime_flags); |
Yan, Zheng | 0ca1f7c | 2010-05-16 10:48:47 -0400 | [diff] [blame] | 302 | btrfs_delalloc_release_metadata(inode, end + 1 - start); |
Chris Mason | a1ed835 | 2009-09-11 12:27:37 -0400 | [diff] [blame] | 303 | btrfs_drop_extent_cache(inode, start, aligned_end - 1, 0); |
Josef Bacik | 0036158 | 2013-08-14 14:02:47 -0400 | [diff] [blame] | 304 | out: |
Filipe David Borba Manana | 1acae57 | 2014-01-07 11:42:27 +0000 | [diff] [blame] | 305 | btrfs_free_path(path); |
Josef Bacik | 0036158 | 2013-08-14 14:02:47 -0400 | [diff] [blame] | 306 | btrfs_end_transaction(trans, root); |
| 307 | return ret; |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 308 | } |
| 309 | |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 310 | struct async_extent { |
| 311 | u64 start; |
| 312 | u64 ram_size; |
| 313 | u64 compressed_size; |
| 314 | struct page **pages; |
| 315 | unsigned long nr_pages; |
Li Zefan | 261507a0 | 2010-12-17 14:21:50 +0800 | [diff] [blame] | 316 | int compress_type; |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 317 | struct list_head list; |
| 318 | }; |
| 319 | |
| 320 | struct async_cow { |
| 321 | struct inode *inode; |
| 322 | struct btrfs_root *root; |
| 323 | struct page *locked_page; |
| 324 | u64 start; |
| 325 | u64 end; |
| 326 | struct list_head extents; |
| 327 | struct btrfs_work work; |
| 328 | }; |
| 329 | |
| 330 | static noinline int add_async_extent(struct async_cow *cow, |
| 331 | u64 start, u64 ram_size, |
| 332 | u64 compressed_size, |
| 333 | struct page **pages, |
Li Zefan | 261507a0 | 2010-12-17 14:21:50 +0800 | [diff] [blame] | 334 | unsigned long nr_pages, |
| 335 | int compress_type) |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 336 | { |
| 337 | struct async_extent *async_extent; |
| 338 | |
| 339 | async_extent = kmalloc(sizeof(*async_extent), GFP_NOFS); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 340 | BUG_ON(!async_extent); /* -ENOMEM */ |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 341 | async_extent->start = start; |
| 342 | async_extent->ram_size = ram_size; |
| 343 | async_extent->compressed_size = compressed_size; |
| 344 | async_extent->pages = pages; |
| 345 | async_extent->nr_pages = nr_pages; |
Li Zefan | 261507a0 | 2010-12-17 14:21:50 +0800 | [diff] [blame] | 346 | async_extent->compress_type = compress_type; |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 347 | list_add_tail(&async_extent->list, &cow->extents); |
| 348 | return 0; |
| 349 | } |
| 350 | |
Wang Shilong | f79707b | 2014-07-17 11:44:09 +0800 | [diff] [blame] | 351 | static inline int inode_need_compress(struct inode *inode) |
| 352 | { |
| 353 | struct btrfs_root *root = BTRFS_I(inode)->root; |
| 354 | |
| 355 | /* force compress */ |
| 356 | if (btrfs_test_opt(root, FORCE_COMPRESS)) |
| 357 | return 1; |
| 358 | /* bad compression ratios */ |
| 359 | if (BTRFS_I(inode)->flags & BTRFS_INODE_NOCOMPRESS) |
| 360 | return 0; |
| 361 | if (btrfs_test_opt(root, COMPRESS) || |
| 362 | BTRFS_I(inode)->flags & BTRFS_INODE_COMPRESS || |
| 363 | BTRFS_I(inode)->force_compress) |
| 364 | return 1; |
| 365 | return 0; |
| 366 | } |
| 367 | |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 368 | /* |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 369 | * we create compressed extents in two phases. The first |
| 370 | * phase compresses a range of pages that have already been |
| 371 | * locked (both pages and state bits are locked). |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 372 | * |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 373 | * This is done inside an ordered work queue, and the compression |
| 374 | * is spread across many cpus. The actual IO submission is step |
| 375 | * two, and the ordered work queue takes care of making sure that |
| 376 | * happens in the same order things were put onto the queue by |
| 377 | * writepages and friends. |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 378 | * |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 379 | * If this code finds it can't get good compression, it puts an |
| 380 | * entry onto the work queue to write the uncompressed bytes. This |
| 381 | * makes sure that both compressed inodes and uncompressed inodes |
Artem Bityutskiy | b257031 | 2012-07-25 18:12:06 +0300 | [diff] [blame] | 382 | * are written in the same order that the flusher thread sent them |
| 383 | * down. |
Chris Mason | d352ac6 | 2008-09-29 15:18:18 -0400 | [diff] [blame] | 384 | */ |
Filipe Manana | c44f649 | 2014-10-09 21:15:44 +0100 | [diff] [blame] | 385 | static noinline void compress_file_range(struct inode *inode, |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 386 | struct page *locked_page, |
| 387 | u64 start, u64 end, |
| 388 | struct async_cow *async_cow, |
| 389 | int *num_added) |
Chris Mason | b888db2b | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 390 | { |
| 391 | struct btrfs_root *root = BTRFS_I(inode)->root; |
Chris Mason | db94535 | 2007-10-15 16:15:53 -0400 | [diff] [blame] | 392 | u64 num_bytes; |
Chris Mason | db94535 | 2007-10-15 16:15:53 -0400 | [diff] [blame] | 393 | u64 blocksize = root->sectorsize; |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 394 | u64 actual_end; |
Chris Mason | 42dc7ba | 2008-12-15 11:44:56 -0500 | [diff] [blame] | 395 | u64 isize = i_size_read(inode); |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 396 | int ret = 0; |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 397 | struct page **pages = NULL; |
| 398 | unsigned long nr_pages; |
| 399 | unsigned long nr_pages_ret = 0; |
| 400 | unsigned long total_compressed = 0; |
| 401 | unsigned long total_in = 0; |
| 402 | unsigned long max_compressed = 128 * 1024; |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 403 | unsigned long max_uncompressed = 128 * 1024; |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 404 | int i; |
| 405 | int will_compress; |
Li Zefan | 261507a0 | 2010-12-17 14:21:50 +0800 | [diff] [blame] | 406 | int compress_type = root->fs_info->compress_type; |
Chris Mason | 4adaa61 | 2013-03-26 13:07:00 -0400 | [diff] [blame] | 407 | int redirty = 0; |
Chris Mason | b888db2b | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 408 | |
Liu Bo | 4cb13e5 | 2012-03-29 09:57:45 -0400 | [diff] [blame] | 409 | /* if this is a small write inside eof, kick off a defrag */ |
| 410 | if ((end - start + 1) < 16 * 1024 && |
| 411 | (start > 0 || end + 1 < BTRFS_I(inode)->disk_i_size)) |
Chris Mason | 4cb5300 | 2011-05-24 15:35:30 -0400 | [diff] [blame] | 412 | btrfs_add_inode_defrag(NULL, inode); |
| 413 | |
Chris Mason | 42dc7ba | 2008-12-15 11:44:56 -0500 | [diff] [blame] | 414 | actual_end = min_t(u64, isize, end + 1); |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 415 | again: |
| 416 | will_compress = 0; |
| 417 | nr_pages = (end >> PAGE_CACHE_SHIFT) - (start >> PAGE_CACHE_SHIFT) + 1; |
| 418 | nr_pages = min(nr_pages, (128 * 1024UL) / PAGE_CACHE_SIZE); |
| 419 | |
Chris Mason | f03d9301f | 2009-02-04 09:31:06 -0500 | [diff] [blame] | 420 | /* |
| 421 | * we don't want to send crud past the end of i_size through |
| 422 | * compression, that's just a waste of CPU time. So, if the |
| 423 | * end of the file is before the start of our current |
| 424 | * requested range of bytes, we bail out to the uncompressed |
| 425 | * cleanup code that can deal with all of this. |
| 426 | * |
| 427 | * It isn't really the fastest way to fix things, but this is a |
| 428 | * very uncommon corner. |
| 429 | */ |
| 430 | if (actual_end <= start) |
| 431 | goto cleanup_and_bail_uncompressed; |
| 432 | |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 433 | total_compressed = actual_end - start; |
| 434 | |
Shilong Wang | 4bcbb33 | 2014-10-07 18:44:35 -0400 | [diff] [blame] | 435 | /* |
| 436 | * skip compression for a small file range(<=blocksize) that |
| 437 | * isn't an inline extent, since it dosen't save disk space at all. |
| 438 | */ |
| 439 | if (total_compressed <= blocksize && |
| 440 | (start > 0 || end + 1 < BTRFS_I(inode)->disk_i_size)) |
| 441 | goto cleanup_and_bail_uncompressed; |
| 442 | |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 443 | /* we want to make sure that amount of ram required to uncompress |
| 444 | * an extent is reasonable, so we limit the total size in ram |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 445 | * of a compressed extent to 128k. This is a crucial number |
| 446 | * because it also controls how easily we can spread reads across |
| 447 | * cpus for decompression. |
| 448 | * |
| 449 | * We also want to make sure the amount of IO required to do |
| 450 | * a random read is reasonably small, so we limit the size of |
| 451 | * a compressed extent to 128k. |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 452 | */ |
| 453 | total_compressed = min(total_compressed, max_uncompressed); |
Qu Wenruo | fda2832 | 2013-02-26 08:10:22 +0000 | [diff] [blame] | 454 | num_bytes = ALIGN(end - start + 1, blocksize); |
Chris Mason | be20aa9 | 2007-12-17 20:14:01 -0500 | [diff] [blame] | 455 | num_bytes = max(blocksize, num_bytes); |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 456 | total_in = 0; |
| 457 | ret = 0; |
Chris Mason | db94535 | 2007-10-15 16:15:53 -0400 | [diff] [blame] | 458 | |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 459 | /* |
| 460 | * we do compression for mount -o compress and when the |
| 461 | * inode has not been flagged as nocompress. This flag can |
| 462 | * change at any time if we discover bad compression ratios. |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 463 | */ |
Wang Shilong | f79707b | 2014-07-17 11:44:09 +0800 | [diff] [blame] | 464 | if (inode_need_compress(inode)) { |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 465 | WARN_ON(pages); |
Chris Mason | cfbc246 | 2008-10-30 13:22:14 -0400 | [diff] [blame] | 466 | pages = kzalloc(sizeof(struct page *) * nr_pages, GFP_NOFS); |
Li Zefan | 560f7d7 | 2011-09-08 10:22:01 +0800 | [diff] [blame] | 467 | if (!pages) { |
| 468 | /* just bail out to the uncompressed code */ |
| 469 | goto cont; |
| 470 | } |
Chris Mason | 179e29e | 2007-11-01 11:28:41 -0400 | [diff] [blame] | 471 | |
Li Zefan | 261507a0 | 2010-12-17 14:21:50 +0800 | [diff] [blame] | 472 | if (BTRFS_I(inode)->force_compress) |
| 473 | compress_type = BTRFS_I(inode)->force_compress; |
| 474 | |
Chris Mason | 4adaa61 | 2013-03-26 13:07:00 -0400 | [diff] [blame] | 475 | /* |
| 476 | * we need to call clear_page_dirty_for_io on each |
| 477 | * page in the range. Otherwise applications with the file |
| 478 | * mmap'd can wander in and change the page contents while |
| 479 | * we are compressing them. |
| 480 | * |
| 481 | * If the compression fails for any reason, we set the pages |
| 482 | * dirty again later on. |
| 483 | */ |
| 484 | extent_range_clear_dirty_for_io(inode, start, end); |
| 485 | redirty = 1; |
Li Zefan | 261507a0 | 2010-12-17 14:21:50 +0800 | [diff] [blame] | 486 | ret = btrfs_compress_pages(compress_type, |
| 487 | inode->i_mapping, start, |
| 488 | total_compressed, pages, |
| 489 | nr_pages, &nr_pages_ret, |
| 490 | &total_in, |
| 491 | &total_compressed, |
| 492 | max_compressed); |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 493 | |
| 494 | if (!ret) { |
| 495 | unsigned long offset = total_compressed & |
| 496 | (PAGE_CACHE_SIZE - 1); |
| 497 | struct page *page = pages[nr_pages_ret - 1]; |
| 498 | char *kaddr; |
| 499 | |
| 500 | /* zero the tail end of the last page, we might be |
| 501 | * sending it down to disk |
| 502 | */ |
| 503 | if (offset) { |
Cong Wang | 7ac687d | 2011-11-25 23:14:28 +0800 | [diff] [blame] | 504 | kaddr = kmap_atomic(page); |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 505 | memset(kaddr + offset, 0, |
| 506 | PAGE_CACHE_SIZE - offset); |
Cong Wang | 7ac687d | 2011-11-25 23:14:28 +0800 | [diff] [blame] | 507 | kunmap_atomic(kaddr); |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 508 | } |
| 509 | will_compress = 1; |
| 510 | } |
| 511 | } |
Li Zefan | 560f7d7 | 2011-09-08 10:22:01 +0800 | [diff] [blame] | 512 | cont: |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 513 | if (start == 0) { |
| 514 | /* lets try to make an inline extent */ |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 515 | if (ret || total_in < (actual_end - start)) { |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 516 | /* we didn't compress the entire range, try |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 517 | * to make an uncompressed inline extent. |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 518 | */ |
Josef Bacik | 0036158 | 2013-08-14 14:02:47 -0400 | [diff] [blame] | 519 | ret = cow_file_range_inline(root, inode, start, end, |
| 520 | 0, 0, NULL); |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 521 | } else { |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 522 | /* try making a compressed inline extent */ |
Josef Bacik | 0036158 | 2013-08-14 14:02:47 -0400 | [diff] [blame] | 523 | ret = cow_file_range_inline(root, inode, start, end, |
Li Zefan | fe3f566 | 2011-03-28 08:30:38 +0000 | [diff] [blame] | 524 | total_compressed, |
| 525 | compress_type, pages); |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 526 | } |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 527 | if (ret <= 0) { |
Josef Bacik | 151a41b | 2013-07-29 13:22:24 -0400 | [diff] [blame] | 528 | unsigned long clear_flags = EXTENT_DELALLOC | |
| 529 | EXTENT_DEFRAG; |
| 530 | clear_flags |= (ret < 0) ? EXTENT_DO_ACCOUNTING : 0; |
| 531 | |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 532 | /* |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 533 | * inline extent creation worked or returned error, |
| 534 | * we don't need to create any more async work items. |
| 535 | * Unlock and free up our temp pages. |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 536 | */ |
Josef Bacik | c2790a2 | 2013-07-29 11:20:47 -0400 | [diff] [blame] | 537 | extent_clear_unlock_delalloc(inode, start, end, NULL, |
Josef Bacik | 151a41b | 2013-07-29 13:22:24 -0400 | [diff] [blame] | 538 | clear_flags, PAGE_UNLOCK | |
Josef Bacik | c2790a2 | 2013-07-29 11:20:47 -0400 | [diff] [blame] | 539 | PAGE_CLEAR_DIRTY | |
| 540 | PAGE_SET_WRITEBACK | |
| 541 | PAGE_END_WRITEBACK); |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 542 | goto free_pages_out; |
| 543 | } |
| 544 | } |
| 545 | |
| 546 | if (will_compress) { |
| 547 | /* |
| 548 | * we aren't doing an inline extent round the compressed size |
| 549 | * up to a block size boundary so the allocator does sane |
| 550 | * things |
| 551 | */ |
Qu Wenruo | fda2832 | 2013-02-26 08:10:22 +0000 | [diff] [blame] | 552 | total_compressed = ALIGN(total_compressed, blocksize); |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 553 | |
| 554 | /* |
| 555 | * one last check to make sure the compression is really a |
| 556 | * win, compare the page count read with the blocks on disk |
| 557 | */ |
Qu Wenruo | fda2832 | 2013-02-26 08:10:22 +0000 | [diff] [blame] | 558 | total_in = ALIGN(total_in, PAGE_CACHE_SIZE); |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 559 | if (total_compressed >= total_in) { |
| 560 | will_compress = 0; |
| 561 | } else { |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 562 | num_bytes = total_in; |
| 563 | } |
| 564 | } |
| 565 | if (!will_compress && pages) { |
| 566 | /* |
| 567 | * the compression code ran but failed to make things smaller, |
| 568 | * free any pages it allocated and our page pointer array |
| 569 | */ |
| 570 | for (i = 0; i < nr_pages_ret; i++) { |
Chris Mason | 70b99e6 | 2008-10-31 12:46:39 -0400 | [diff] [blame] | 571 | WARN_ON(pages[i]->mapping); |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 572 | page_cache_release(pages[i]); |
| 573 | } |
| 574 | kfree(pages); |
| 575 | pages = NULL; |
| 576 | total_compressed = 0; |
| 577 | nr_pages_ret = 0; |
| 578 | |
| 579 | /* flag the file so we don't compress in the future */ |
Chris Mason | 1e701a3 | 2010-03-11 09:42:04 -0500 | [diff] [blame] | 580 | if (!btrfs_test_opt(root, FORCE_COMPRESS) && |
| 581 | !(BTRFS_I(inode)->force_compress)) { |
Chris Mason | a555f81 | 2010-01-28 16:18:15 -0500 | [diff] [blame] | 582 | BTRFS_I(inode)->flags |= BTRFS_INODE_NOCOMPRESS; |
Chris Mason | 1e701a3 | 2010-03-11 09:42:04 -0500 | [diff] [blame] | 583 | } |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 584 | } |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 585 | if (will_compress) { |
| 586 | *num_added += 1; |
| 587 | |
| 588 | /* the async work queues will take care of doing actual |
| 589 | * allocation on disk for these compressed pages, |
| 590 | * and will submit them to the elevator. |
| 591 | */ |
| 592 | add_async_extent(async_cow, start, num_bytes, |
Li Zefan | 261507a0 | 2010-12-17 14:21:50 +0800 | [diff] [blame] | 593 | total_compressed, pages, nr_pages_ret, |
| 594 | compress_type); |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 595 | |
Yan, Zheng | 24ae636 | 2010-12-06 07:02:36 +0000 | [diff] [blame] | 596 | if (start + num_bytes < end) { |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 597 | start += num_bytes; |
| 598 | pages = NULL; |
| 599 | cond_resched(); |
| 600 | goto again; |
| 601 | } |
| 602 | } else { |
Chris Mason | f03d9301f | 2009-02-04 09:31:06 -0500 | [diff] [blame] | 603 | cleanup_and_bail_uncompressed: |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 604 | /* |
| 605 | * No compression, but we still need to write the pages in |
| 606 | * the file we've been given so far. redirty the locked |
| 607 | * page if it corresponds to our extent and set things up |
| 608 | * for the async work queue to run cow_file_range to do |
| 609 | * the normal delalloc dance |
| 610 | */ |
| 611 | if (page_offset(locked_page) >= start && |
| 612 | page_offset(locked_page) <= end) { |
| 613 | __set_page_dirty_nobuffers(locked_page); |
| 614 | /* unlocked later on in the async handlers */ |
| 615 | } |
Chris Mason | 4adaa61 | 2013-03-26 13:07:00 -0400 | [diff] [blame] | 616 | if (redirty) |
| 617 | extent_range_redirty_for_io(inode, start, end); |
Li Zefan | 261507a0 | 2010-12-17 14:21:50 +0800 | [diff] [blame] | 618 | add_async_extent(async_cow, start, end - start + 1, |
| 619 | 0, NULL, 0, BTRFS_COMPRESS_NONE); |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 620 | *num_added += 1; |
| 621 | } |
| 622 | |
Filipe Manana | c44f649 | 2014-10-09 21:15:44 +0100 | [diff] [blame] | 623 | return; |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 624 | |
| 625 | free_pages_out: |
| 626 | for (i = 0; i < nr_pages_ret; i++) { |
| 627 | WARN_ON(pages[i]->mapping); |
| 628 | page_cache_release(pages[i]); |
| 629 | } |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 630 | kfree(pages); |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 631 | } |
| 632 | |
Filipe Manana | 40ae837 | 2014-10-06 22:14:24 +0100 | [diff] [blame] | 633 | static void free_async_extent_pages(struct async_extent *async_extent) |
| 634 | { |
| 635 | int i; |
| 636 | |
| 637 | if (!async_extent->pages) |
| 638 | return; |
| 639 | |
| 640 | for (i = 0; i < async_extent->nr_pages; i++) { |
| 641 | WARN_ON(async_extent->pages[i]->mapping); |
| 642 | page_cache_release(async_extent->pages[i]); |
| 643 | } |
| 644 | kfree(async_extent->pages); |
| 645 | async_extent->nr_pages = 0; |
| 646 | async_extent->pages = NULL; |
| 647 | } |
| 648 | |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 649 | /* |
| 650 | * phase two of compressed writeback. This is the ordered portion |
| 651 | * of the code, which only gets called in the order the work was |
| 652 | * queued. We walk all the async extents created by compress_file_range |
| 653 | * and send them down to the disk. |
| 654 | */ |
Filipe Manana | dec8f17 | 2014-10-06 22:14:26 +0100 | [diff] [blame] | 655 | static noinline void submit_compressed_extents(struct inode *inode, |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 656 | struct async_cow *async_cow) |
| 657 | { |
| 658 | struct async_extent *async_extent; |
| 659 | u64 alloc_hint = 0; |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 660 | struct btrfs_key ins; |
| 661 | struct extent_map *em; |
| 662 | struct btrfs_root *root = BTRFS_I(inode)->root; |
| 663 | struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree; |
| 664 | struct extent_io_tree *io_tree; |
Josef Bacik | f5a84ee3 | 2009-11-10 21:23:48 -0500 | [diff] [blame] | 665 | int ret = 0; |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 666 | |
Josef Bacik | 3e04e7f | 2013-02-06 16:49:15 -0500 | [diff] [blame] | 667 | again: |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 668 | while (!list_empty(&async_cow->extents)) { |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 669 | async_extent = list_entry(async_cow->extents.next, |
| 670 | struct async_extent, list); |
| 671 | list_del(&async_extent->list); |
| 672 | |
| 673 | io_tree = &BTRFS_I(inode)->io_tree; |
| 674 | |
Josef Bacik | f5a84ee3 | 2009-11-10 21:23:48 -0500 | [diff] [blame] | 675 | retry: |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 676 | /* did the compression code fall back to uncompressed IO? */ |
| 677 | if (!async_extent->pages) { |
| 678 | int page_started = 0; |
| 679 | unsigned long nr_written = 0; |
| 680 | |
| 681 | lock_extent(io_tree, async_extent->start, |
Josef Bacik | 2ac55d4 | 2010-02-03 19:33:23 +0000 | [diff] [blame] | 682 | async_extent->start + |
Jeff Mahoney | d008237 | 2012-03-01 14:57:19 +0100 | [diff] [blame] | 683 | async_extent->ram_size - 1); |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 684 | |
| 685 | /* allocate blocks */ |
Josef Bacik | f5a84ee3 | 2009-11-10 21:23:48 -0500 | [diff] [blame] | 686 | ret = cow_file_range(inode, async_cow->locked_page, |
| 687 | async_extent->start, |
| 688 | async_extent->start + |
| 689 | async_extent->ram_size - 1, |
| 690 | &page_started, &nr_written, 0); |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 691 | |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 692 | /* JDM XXX */ |
| 693 | |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 694 | /* |
| 695 | * if page_started, cow_file_range inserted an |
| 696 | * inline extent and took care of all the unlocking |
| 697 | * and IO for us. Otherwise, we need to submit |
| 698 | * all those pages down to the drive. |
| 699 | */ |
Josef Bacik | f5a84ee3 | 2009-11-10 21:23:48 -0500 | [diff] [blame] | 700 | if (!page_started && !ret) |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 701 | extent_write_locked_range(io_tree, |
| 702 | inode, async_extent->start, |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 703 | async_extent->start + |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 704 | async_extent->ram_size - 1, |
| 705 | btrfs_get_extent, |
| 706 | WB_SYNC_ALL); |
Josef Bacik | 3e04e7f | 2013-02-06 16:49:15 -0500 | [diff] [blame] | 707 | else if (ret) |
| 708 | unlock_page(async_cow->locked_page); |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 709 | kfree(async_extent); |
| 710 | cond_resched(); |
| 711 | continue; |
| 712 | } |
| 713 | |
| 714 | lock_extent(io_tree, async_extent->start, |
Jeff Mahoney | d008237 | 2012-03-01 14:57:19 +0100 | [diff] [blame] | 715 | async_extent->start + async_extent->ram_size - 1); |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 716 | |
Josef Bacik | 0036158 | 2013-08-14 14:02:47 -0400 | [diff] [blame] | 717 | ret = btrfs_reserve_extent(root, |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 718 | async_extent->compressed_size, |
| 719 | async_extent->compressed_size, |
Miao Xie | e570fd2 | 2014-06-19 10:42:50 +0800 | [diff] [blame] | 720 | 0, alloc_hint, &ins, 1, 1); |
Josef Bacik | f5a84ee3 | 2009-11-10 21:23:48 -0500 | [diff] [blame] | 721 | if (ret) { |
Filipe Manana | 40ae837 | 2014-10-06 22:14:24 +0100 | [diff] [blame] | 722 | free_async_extent_pages(async_extent); |
Josef Bacik | 3e04e7f | 2013-02-06 16:49:15 -0500 | [diff] [blame] | 723 | |
Josef Bacik | fdf8e2e | 2013-06-14 16:58:23 -0400 | [diff] [blame] | 724 | if (ret == -ENOSPC) { |
| 725 | unlock_extent(io_tree, async_extent->start, |
| 726 | async_extent->start + |
| 727 | async_extent->ram_size - 1); |
Liu Bo | ce62003 | 2014-07-24 22:48:05 +0800 | [diff] [blame] | 728 | |
| 729 | /* |
| 730 | * we need to redirty the pages if we decide to |
| 731 | * fallback to uncompressed IO, otherwise we |
| 732 | * will not submit these pages down to lower |
| 733 | * layers. |
| 734 | */ |
| 735 | extent_range_redirty_for_io(inode, |
| 736 | async_extent->start, |
| 737 | async_extent->start + |
| 738 | async_extent->ram_size - 1); |
| 739 | |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 740 | goto retry; |
Josef Bacik | fdf8e2e | 2013-06-14 16:58:23 -0400 | [diff] [blame] | 741 | } |
Josef Bacik | 3e04e7f | 2013-02-06 16:49:15 -0500 | [diff] [blame] | 742 | goto out_free; |
Josef Bacik | f5a84ee3 | 2009-11-10 21:23:48 -0500 | [diff] [blame] | 743 | } |
| 744 | |
Yan, Zheng | c216775 | 2009-11-12 09:34:21 +0000 | [diff] [blame] | 745 | /* |
| 746 | * here we're doing allocation and writeback of the |
| 747 | * compressed pages |
| 748 | */ |
| 749 | btrfs_drop_extent_cache(inode, async_extent->start, |
| 750 | async_extent->start + |
| 751 | async_extent->ram_size - 1, 0); |
| 752 | |
David Sterba | 172ddd6 | 2011-04-21 00:48:27 +0200 | [diff] [blame] | 753 | em = alloc_extent_map(); |
Liu Bo | b9aa55b | 2013-05-14 02:12:15 +0000 | [diff] [blame] | 754 | if (!em) { |
| 755 | ret = -ENOMEM; |
Josef Bacik | 3e04e7f | 2013-02-06 16:49:15 -0500 | [diff] [blame] | 756 | goto out_free_reserve; |
Liu Bo | b9aa55b | 2013-05-14 02:12:15 +0000 | [diff] [blame] | 757 | } |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 758 | em->start = async_extent->start; |
| 759 | em->len = async_extent->ram_size; |
Chris Mason | 445a694 | 2008-11-10 11:53:33 -0500 | [diff] [blame] | 760 | em->orig_start = em->start; |
Josef Bacik | 2ab28f3 | 2012-10-12 15:27:49 -0400 | [diff] [blame] | 761 | em->mod_start = em->start; |
| 762 | em->mod_len = em->len; |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 763 | |
| 764 | em->block_start = ins.objectid; |
| 765 | em->block_len = ins.offset; |
Josef Bacik | b493968 | 2012-12-03 10:31:19 -0500 | [diff] [blame] | 766 | em->orig_block_len = ins.offset; |
Josef Bacik | cc95bef | 2013-04-04 14:31:27 -0400 | [diff] [blame] | 767 | em->ram_bytes = async_extent->ram_size; |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 768 | em->bdev = root->fs_info->fs_devices->latest_bdev; |
Li Zefan | 261507a0 | 2010-12-17 14:21:50 +0800 | [diff] [blame] | 769 | em->compress_type = async_extent->compress_type; |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 770 | set_bit(EXTENT_FLAG_PINNED, &em->flags); |
| 771 | set_bit(EXTENT_FLAG_COMPRESSED, &em->flags); |
Josef Bacik | 70c8a91 | 2012-10-11 16:54:30 -0400 | [diff] [blame] | 772 | em->generation = -1; |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 773 | |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 774 | while (1) { |
Chris Mason | 890871b | 2009-09-02 16:24:52 -0400 | [diff] [blame] | 775 | write_lock(&em_tree->lock); |
Josef Bacik | 09a2a8f9 | 2013-04-05 16:51:15 -0400 | [diff] [blame] | 776 | ret = add_extent_mapping(em_tree, em, 1); |
Chris Mason | 890871b | 2009-09-02 16:24:52 -0400 | [diff] [blame] | 777 | write_unlock(&em_tree->lock); |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 778 | if (ret != -EEXIST) { |
| 779 | free_extent_map(em); |
| 780 | break; |
| 781 | } |
| 782 | btrfs_drop_extent_cache(inode, async_extent->start, |
| 783 | async_extent->start + |
| 784 | async_extent->ram_size - 1, 0); |
| 785 | } |
| 786 | |
Josef Bacik | 3e04e7f | 2013-02-06 16:49:15 -0500 | [diff] [blame] | 787 | if (ret) |
| 788 | goto out_free_reserve; |
| 789 | |
Li Zefan | 261507a0 | 2010-12-17 14:21:50 +0800 | [diff] [blame] | 790 | ret = btrfs_add_ordered_extent_compress(inode, |
| 791 | async_extent->start, |
| 792 | ins.objectid, |
| 793 | async_extent->ram_size, |
| 794 | ins.offset, |
| 795 | BTRFS_ORDERED_COMPRESSED, |
| 796 | async_extent->compress_type); |
Filipe Manana | d9f8596 | 2014-08-25 10:43:00 +0100 | [diff] [blame] | 797 | if (ret) { |
| 798 | btrfs_drop_extent_cache(inode, async_extent->start, |
| 799 | async_extent->start + |
| 800 | async_extent->ram_size - 1, 0); |
Josef Bacik | 3e04e7f | 2013-02-06 16:49:15 -0500 | [diff] [blame] | 801 | goto out_free_reserve; |
Filipe Manana | d9f8596 | 2014-08-25 10:43:00 +0100 | [diff] [blame] | 802 | } |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 803 | |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 804 | /* |
| 805 | * clear dirty, set writeback and unlock the pages. |
| 806 | */ |
Josef Bacik | c2790a2 | 2013-07-29 11:20:47 -0400 | [diff] [blame] | 807 | extent_clear_unlock_delalloc(inode, async_extent->start, |
Chris Mason | a791e35 | 2009-10-08 11:27:10 -0400 | [diff] [blame] | 808 | async_extent->start + |
| 809 | async_extent->ram_size - 1, |
Josef Bacik | 151a41b | 2013-07-29 13:22:24 -0400 | [diff] [blame] | 810 | NULL, EXTENT_LOCKED | EXTENT_DELALLOC, |
| 811 | PAGE_UNLOCK | PAGE_CLEAR_DIRTY | |
Josef Bacik | c2790a2 | 2013-07-29 11:20:47 -0400 | [diff] [blame] | 812 | PAGE_SET_WRITEBACK); |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 813 | ret = btrfs_submit_compressed_write(inode, |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 814 | async_extent->start, |
| 815 | async_extent->ram_size, |
| 816 | ins.objectid, |
| 817 | ins.offset, async_extent->pages, |
| 818 | async_extent->nr_pages); |
Filipe Manana | fce2a4e | 2014-10-06 22:14:23 +0100 | [diff] [blame] | 819 | if (ret) { |
| 820 | struct extent_io_tree *tree = &BTRFS_I(inode)->io_tree; |
| 821 | struct page *p = async_extent->pages[0]; |
| 822 | const u64 start = async_extent->start; |
| 823 | const u64 end = start + async_extent->ram_size - 1; |
| 824 | |
| 825 | p->mapping = inode->i_mapping; |
| 826 | tree->ops->writepage_end_io_hook(p, start, end, |
| 827 | NULL, 0); |
| 828 | p->mapping = NULL; |
| 829 | extent_clear_unlock_delalloc(inode, start, end, NULL, 0, |
| 830 | PAGE_END_WRITEBACK | |
| 831 | PAGE_SET_ERROR); |
Filipe Manana | 40ae837 | 2014-10-06 22:14:24 +0100 | [diff] [blame] | 832 | free_async_extent_pages(async_extent); |
Filipe Manana | fce2a4e | 2014-10-06 22:14:23 +0100 | [diff] [blame] | 833 | } |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 834 | alloc_hint = ins.objectid + ins.offset; |
| 835 | kfree(async_extent); |
| 836 | cond_resched(); |
| 837 | } |
Filipe Manana | dec8f17 | 2014-10-06 22:14:26 +0100 | [diff] [blame] | 838 | return; |
Josef Bacik | 3e04e7f | 2013-02-06 16:49:15 -0500 | [diff] [blame] | 839 | out_free_reserve: |
Miao Xie | e570fd2 | 2014-06-19 10:42:50 +0800 | [diff] [blame] | 840 | btrfs_free_reserved_extent(root, ins.objectid, ins.offset, 1); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 841 | out_free: |
Josef Bacik | c2790a2 | 2013-07-29 11:20:47 -0400 | [diff] [blame] | 842 | extent_clear_unlock_delalloc(inode, async_extent->start, |
Josef Bacik | 3e04e7f | 2013-02-06 16:49:15 -0500 | [diff] [blame] | 843 | async_extent->start + |
| 844 | async_extent->ram_size - 1, |
Josef Bacik | c2790a2 | 2013-07-29 11:20:47 -0400 | [diff] [blame] | 845 | NULL, EXTENT_LOCKED | EXTENT_DELALLOC | |
Josef Bacik | 151a41b | 2013-07-29 13:22:24 -0400 | [diff] [blame] | 846 | EXTENT_DEFRAG | EXTENT_DO_ACCOUNTING, |
| 847 | PAGE_UNLOCK | PAGE_CLEAR_DIRTY | |
Filipe Manana | 704de49 | 2014-10-06 22:14:22 +0100 | [diff] [blame] | 848 | PAGE_SET_WRITEBACK | PAGE_END_WRITEBACK | |
| 849 | PAGE_SET_ERROR); |
Filipe Manana | 40ae837 | 2014-10-06 22:14:24 +0100 | [diff] [blame] | 850 | free_async_extent_pages(async_extent); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 851 | kfree(async_extent); |
Josef Bacik | 3e04e7f | 2013-02-06 16:49:15 -0500 | [diff] [blame] | 852 | goto again; |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 853 | } |
| 854 | |
Josef Bacik | 4b46fce | 2010-05-23 11:00:55 -0400 | [diff] [blame] | 855 | static u64 get_extent_allocation_hint(struct inode *inode, u64 start, |
| 856 | u64 num_bytes) |
| 857 | { |
| 858 | struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree; |
| 859 | struct extent_map *em; |
| 860 | u64 alloc_hint = 0; |
| 861 | |
| 862 | read_lock(&em_tree->lock); |
| 863 | em = search_extent_mapping(em_tree, start, num_bytes); |
| 864 | if (em) { |
| 865 | /* |
| 866 | * if block start isn't an actual block number then find the |
| 867 | * first block in this inode and use that as a hint. If that |
| 868 | * block is also bogus then just don't worry about it. |
| 869 | */ |
| 870 | if (em->block_start >= EXTENT_MAP_LAST_BYTE) { |
| 871 | free_extent_map(em); |
| 872 | em = search_extent_mapping(em_tree, 0, 0); |
| 873 | if (em && em->block_start < EXTENT_MAP_LAST_BYTE) |
| 874 | alloc_hint = em->block_start; |
| 875 | if (em) |
| 876 | free_extent_map(em); |
| 877 | } else { |
| 878 | alloc_hint = em->block_start; |
| 879 | free_extent_map(em); |
| 880 | } |
| 881 | } |
| 882 | read_unlock(&em_tree->lock); |
| 883 | |
| 884 | return alloc_hint; |
| 885 | } |
| 886 | |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 887 | /* |
| 888 | * when extent_io.c finds a delayed allocation range in the file, |
| 889 | * the call backs end up in this code. The basic idea is to |
| 890 | * allocate extents on disk for the range, and create ordered data structs |
| 891 | * in ram to track those extents. |
| 892 | * |
| 893 | * locked_page is the page that writepage had locked already. We use |
| 894 | * it to make sure we don't do extra locks or unlocks. |
| 895 | * |
| 896 | * *page_started is set to one if we unlock locked_page and do everything |
| 897 | * required to start IO on it. It may be clean and already done with |
| 898 | * IO when we return. |
| 899 | */ |
Josef Bacik | 0036158 | 2013-08-14 14:02:47 -0400 | [diff] [blame] | 900 | static noinline int cow_file_range(struct inode *inode, |
| 901 | struct page *locked_page, |
| 902 | u64 start, u64 end, int *page_started, |
| 903 | unsigned long *nr_written, |
| 904 | int unlock) |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 905 | { |
Josef Bacik | 0036158 | 2013-08-14 14:02:47 -0400 | [diff] [blame] | 906 | struct btrfs_root *root = BTRFS_I(inode)->root; |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 907 | u64 alloc_hint = 0; |
| 908 | u64 num_bytes; |
| 909 | unsigned long ram_size; |
| 910 | u64 disk_num_bytes; |
| 911 | u64 cur_alloc_size; |
| 912 | u64 blocksize = root->sectorsize; |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 913 | struct btrfs_key ins; |
| 914 | struct extent_map *em; |
| 915 | struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree; |
| 916 | int ret = 0; |
| 917 | |
Josef Bacik | 02ecd2c | 2013-10-25 16:19:08 -0400 | [diff] [blame] | 918 | if (btrfs_is_free_space_inode(inode)) { |
| 919 | WARN_ON_ONCE(1); |
Josef Bacik | 29bce2f | 2014-02-07 12:21:23 -0500 | [diff] [blame] | 920 | ret = -EINVAL; |
| 921 | goto out_unlock; |
Josef Bacik | 02ecd2c | 2013-10-25 16:19:08 -0400 | [diff] [blame] | 922 | } |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 923 | |
Qu Wenruo | fda2832 | 2013-02-26 08:10:22 +0000 | [diff] [blame] | 924 | num_bytes = ALIGN(end - start + 1, blocksize); |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 925 | num_bytes = max(blocksize, num_bytes); |
| 926 | disk_num_bytes = num_bytes; |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 927 | |
Chris Mason | 4cb5300 | 2011-05-24 15:35:30 -0400 | [diff] [blame] | 928 | /* if this is a small write inside eof, kick off defrag */ |
Liu Bo | 4cb13e5 | 2012-03-29 09:57:45 -0400 | [diff] [blame] | 929 | if (num_bytes < 64 * 1024 && |
| 930 | (start > 0 || end + 1 < BTRFS_I(inode)->disk_i_size)) |
Josef Bacik | 0036158 | 2013-08-14 14:02:47 -0400 | [diff] [blame] | 931 | btrfs_add_inode_defrag(NULL, inode); |
Chris Mason | 4cb5300 | 2011-05-24 15:35:30 -0400 | [diff] [blame] | 932 | |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 933 | if (start == 0) { |
| 934 | /* lets try to make an inline extent */ |
Josef Bacik | 0036158 | 2013-08-14 14:02:47 -0400 | [diff] [blame] | 935 | ret = cow_file_range_inline(root, inode, start, end, 0, 0, |
| 936 | NULL); |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 937 | if (ret == 0) { |
Josef Bacik | c2790a2 | 2013-07-29 11:20:47 -0400 | [diff] [blame] | 938 | extent_clear_unlock_delalloc(inode, start, end, NULL, |
| 939 | EXTENT_LOCKED | EXTENT_DELALLOC | |
Josef Bacik | 151a41b | 2013-07-29 13:22:24 -0400 | [diff] [blame] | 940 | EXTENT_DEFRAG, PAGE_UNLOCK | |
Josef Bacik | c2790a2 | 2013-07-29 11:20:47 -0400 | [diff] [blame] | 941 | PAGE_CLEAR_DIRTY | PAGE_SET_WRITEBACK | |
| 942 | PAGE_END_WRITEBACK); |
Yan, Zheng | c216775 | 2009-11-12 09:34:21 +0000 | [diff] [blame] | 943 | |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 944 | *nr_written = *nr_written + |
| 945 | (end - start + PAGE_CACHE_SIZE) / PAGE_CACHE_SIZE; |
| 946 | *page_started = 1; |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 947 | goto out; |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 948 | } else if (ret < 0) { |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 949 | goto out_unlock; |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 950 | } |
| 951 | } |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 952 | |
| 953 | BUG_ON(disk_num_bytes > |
David Sterba | 6c41761 | 2011-04-13 15:41:04 +0200 | [diff] [blame] | 954 | btrfs_super_total_bytes(root->fs_info->super_copy)); |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 955 | |
Josef Bacik | 4b46fce | 2010-05-23 11:00:55 -0400 | [diff] [blame] | 956 | alloc_hint = get_extent_allocation_hint(inode, start, num_bytes); |
Zheng Yan | 5b21f2e | 2008-09-26 10:05:38 -0400 | [diff] [blame] | 957 | btrfs_drop_extent_cache(inode, start, start + num_bytes - 1, 0); |
Chris Mason | 3b95151 | 2008-04-17 11:29:12 -0400 | [diff] [blame] | 958 | |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 959 | while (disk_num_bytes > 0) { |
Chris Mason | a791e35 | 2009-10-08 11:27:10 -0400 | [diff] [blame] | 960 | unsigned long op; |
| 961 | |
Josef Bacik | 287a0ab | 2010-03-19 18:07:23 +0000 | [diff] [blame] | 962 | cur_alloc_size = disk_num_bytes; |
Josef Bacik | 0036158 | 2013-08-14 14:02:47 -0400 | [diff] [blame] | 963 | ret = btrfs_reserve_extent(root, cur_alloc_size, |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 964 | root->sectorsize, 0, alloc_hint, |
Miao Xie | e570fd2 | 2014-06-19 10:42:50 +0800 | [diff] [blame] | 965 | &ins, 1, 1); |
Josef Bacik | 0036158 | 2013-08-14 14:02:47 -0400 | [diff] [blame] | 966 | if (ret < 0) |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 967 | goto out_unlock; |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 968 | |
David Sterba | 172ddd6 | 2011-04-21 00:48:27 +0200 | [diff] [blame] | 969 | em = alloc_extent_map(); |
Liu Bo | b9aa55b | 2013-05-14 02:12:15 +0000 | [diff] [blame] | 970 | if (!em) { |
| 971 | ret = -ENOMEM; |
Liu Bo | ace68ba | 2013-04-22 10:53:47 +0000 | [diff] [blame] | 972 | goto out_reserve; |
Liu Bo | b9aa55b | 2013-05-14 02:12:15 +0000 | [diff] [blame] | 973 | } |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 974 | em->start = start; |
Chris Mason | 445a694 | 2008-11-10 11:53:33 -0500 | [diff] [blame] | 975 | em->orig_start = em->start; |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 976 | ram_size = ins.offset; |
| 977 | em->len = ins.offset; |
Josef Bacik | 2ab28f3 | 2012-10-12 15:27:49 -0400 | [diff] [blame] | 978 | em->mod_start = em->start; |
| 979 | em->mod_len = em->len; |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 980 | |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 981 | em->block_start = ins.objectid; |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 982 | em->block_len = ins.offset; |
Josef Bacik | b493968 | 2012-12-03 10:31:19 -0500 | [diff] [blame] | 983 | em->orig_block_len = ins.offset; |
Josef Bacik | cc95bef | 2013-04-04 14:31:27 -0400 | [diff] [blame] | 984 | em->ram_bytes = ram_size; |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 985 | em->bdev = root->fs_info->fs_devices->latest_bdev; |
Chris Mason | 7f3c74f | 2008-07-18 12:01:11 -0400 | [diff] [blame] | 986 | set_bit(EXTENT_FLAG_PINNED, &em->flags); |
Josef Bacik | 70c8a91 | 2012-10-11 16:54:30 -0400 | [diff] [blame] | 987 | em->generation = -1; |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 988 | |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 989 | while (1) { |
Chris Mason | 890871b | 2009-09-02 16:24:52 -0400 | [diff] [blame] | 990 | write_lock(&em_tree->lock); |
Josef Bacik | 09a2a8f9 | 2013-04-05 16:51:15 -0400 | [diff] [blame] | 991 | ret = add_extent_mapping(em_tree, em, 1); |
Chris Mason | 890871b | 2009-09-02 16:24:52 -0400 | [diff] [blame] | 992 | write_unlock(&em_tree->lock); |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 993 | if (ret != -EEXIST) { |
| 994 | free_extent_map(em); |
| 995 | break; |
| 996 | } |
| 997 | btrfs_drop_extent_cache(inode, start, |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 998 | start + ram_size - 1, 0); |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 999 | } |
Liu Bo | ace68ba | 2013-04-22 10:53:47 +0000 | [diff] [blame] | 1000 | if (ret) |
| 1001 | goto out_reserve; |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 1002 | |
Chris Mason | 98d20f6 | 2008-04-14 09:46:10 -0400 | [diff] [blame] | 1003 | cur_alloc_size = ins.offset; |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 1004 | ret = btrfs_add_ordered_extent(inode, start, ins.objectid, |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 1005 | ram_size, cur_alloc_size, 0); |
Liu Bo | ace68ba | 2013-04-22 10:53:47 +0000 | [diff] [blame] | 1006 | if (ret) |
Filipe Manana | d9f8596 | 2014-08-25 10:43:00 +0100 | [diff] [blame] | 1007 | goto out_drop_extent_cache; |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 1008 | |
Yan Zheng | 17d217f | 2008-12-12 10:03:38 -0500 | [diff] [blame] | 1009 | if (root->root_key.objectid == |
| 1010 | BTRFS_DATA_RELOC_TREE_OBJECTID) { |
| 1011 | ret = btrfs_reloc_clone_csums(inode, start, |
| 1012 | cur_alloc_size); |
Josef Bacik | 0036158 | 2013-08-14 14:02:47 -0400 | [diff] [blame] | 1013 | if (ret) |
Filipe Manana | d9f8596 | 2014-08-25 10:43:00 +0100 | [diff] [blame] | 1014 | goto out_drop_extent_cache; |
Yan Zheng | 17d217f | 2008-12-12 10:03:38 -0500 | [diff] [blame] | 1015 | } |
| 1016 | |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 1017 | if (disk_num_bytes < cur_alloc_size) |
Chris Mason | 3b95151 | 2008-04-17 11:29:12 -0400 | [diff] [blame] | 1018 | break; |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 1019 | |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 1020 | /* we're not doing compressed IO, don't unlock the first |
| 1021 | * page (which the caller expects to stay locked), don't |
| 1022 | * clear any dirty bits and don't set any writeback bits |
Chris Mason | 8b62b72 | 2009-09-02 16:53:46 -0400 | [diff] [blame] | 1023 | * |
| 1024 | * Do set the Private2 bit so we know this page was properly |
| 1025 | * setup for writepage |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 1026 | */ |
Josef Bacik | c2790a2 | 2013-07-29 11:20:47 -0400 | [diff] [blame] | 1027 | op = unlock ? PAGE_UNLOCK : 0; |
| 1028 | op |= PAGE_SET_PRIVATE2; |
Chris Mason | a791e35 | 2009-10-08 11:27:10 -0400 | [diff] [blame] | 1029 | |
Josef Bacik | c2790a2 | 2013-07-29 11:20:47 -0400 | [diff] [blame] | 1030 | extent_clear_unlock_delalloc(inode, start, |
| 1031 | start + ram_size - 1, locked_page, |
| 1032 | EXTENT_LOCKED | EXTENT_DELALLOC, |
| 1033 | op); |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 1034 | disk_num_bytes -= cur_alloc_size; |
Chris Mason | c59f895 | 2007-12-17 20:14:04 -0500 | [diff] [blame] | 1035 | num_bytes -= cur_alloc_size; |
| 1036 | alloc_hint = ins.objectid + ins.offset; |
| 1037 | start += cur_alloc_size; |
Chris Mason | b888db2b | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 1038 | } |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 1039 | out: |
Chris Mason | be20aa9 | 2007-12-17 20:14:01 -0500 | [diff] [blame] | 1040 | return ret; |
Miao Xie | b7d5b0a | 2012-11-01 07:32:18 +0000 | [diff] [blame] | 1041 | |
Filipe Manana | d9f8596 | 2014-08-25 10:43:00 +0100 | [diff] [blame] | 1042 | out_drop_extent_cache: |
| 1043 | btrfs_drop_extent_cache(inode, start, start + ram_size - 1, 0); |
Liu Bo | ace68ba | 2013-04-22 10:53:47 +0000 | [diff] [blame] | 1044 | out_reserve: |
Miao Xie | e570fd2 | 2014-06-19 10:42:50 +0800 | [diff] [blame] | 1045 | btrfs_free_reserved_extent(root, ins.objectid, ins.offset, 1); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 1046 | out_unlock: |
Josef Bacik | c2790a2 | 2013-07-29 11:20:47 -0400 | [diff] [blame] | 1047 | extent_clear_unlock_delalloc(inode, start, end, locked_page, |
Josef Bacik | 151a41b | 2013-07-29 13:22:24 -0400 | [diff] [blame] | 1048 | EXTENT_LOCKED | EXTENT_DO_ACCOUNTING | |
| 1049 | EXTENT_DELALLOC | EXTENT_DEFRAG, |
| 1050 | PAGE_UNLOCK | PAGE_CLEAR_DIRTY | |
| 1051 | PAGE_SET_WRITEBACK | PAGE_END_WRITEBACK); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 1052 | goto out; |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 1053 | } |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 1054 | |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 1055 | /* |
| 1056 | * work queue call back to started compression on a file and pages |
| 1057 | */ |
| 1058 | static noinline void async_cow_start(struct btrfs_work *work) |
| 1059 | { |
| 1060 | struct async_cow *async_cow; |
| 1061 | int num_added = 0; |
| 1062 | async_cow = container_of(work, struct async_cow, work); |
| 1063 | |
| 1064 | compress_file_range(async_cow->inode, async_cow->locked_page, |
| 1065 | async_cow->start, async_cow->end, async_cow, |
| 1066 | &num_added); |
Josef Bacik | 8180ef8 | 2012-06-08 15:16:12 -0400 | [diff] [blame] | 1067 | if (num_added == 0) { |
Josef Bacik | cb77fcd | 2012-06-15 12:19:48 -0600 | [diff] [blame] | 1068 | btrfs_add_delayed_iput(async_cow->inode); |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 1069 | async_cow->inode = NULL; |
Josef Bacik | 8180ef8 | 2012-06-08 15:16:12 -0400 | [diff] [blame] | 1070 | } |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 1071 | } |
| 1072 | |
| 1073 | /* |
| 1074 | * work queue call back to submit previously compressed pages |
| 1075 | */ |
| 1076 | static noinline void async_cow_submit(struct btrfs_work *work) |
| 1077 | { |
| 1078 | struct async_cow *async_cow; |
| 1079 | struct btrfs_root *root; |
| 1080 | unsigned long nr_pages; |
| 1081 | |
| 1082 | async_cow = container_of(work, struct async_cow, work); |
| 1083 | |
| 1084 | root = async_cow->root; |
| 1085 | nr_pages = (async_cow->end - async_cow->start + PAGE_CACHE_SIZE) >> |
| 1086 | PAGE_CACHE_SHIFT; |
| 1087 | |
Josef Bacik | 66657b3 | 2012-08-01 15:36:24 -0400 | [diff] [blame] | 1088 | if (atomic_sub_return(nr_pages, &root->fs_info->async_delalloc_pages) < |
Liu Bo | 287082b | 2012-06-28 04:02:24 -0600 | [diff] [blame] | 1089 | 5 * 1024 * 1024 && |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 1090 | waitqueue_active(&root->fs_info->async_submit_wait)) |
| 1091 | wake_up(&root->fs_info->async_submit_wait); |
| 1092 | |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 1093 | if (async_cow->inode) |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 1094 | submit_compressed_extents(async_cow->inode, async_cow); |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 1095 | } |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 1096 | |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 1097 | static noinline void async_cow_free(struct btrfs_work *work) |
| 1098 | { |
| 1099 | struct async_cow *async_cow; |
| 1100 | async_cow = container_of(work, struct async_cow, work); |
Josef Bacik | 8180ef8 | 2012-06-08 15:16:12 -0400 | [diff] [blame] | 1101 | if (async_cow->inode) |
Josef Bacik | cb77fcd | 2012-06-15 12:19:48 -0600 | [diff] [blame] | 1102 | btrfs_add_delayed_iput(async_cow->inode); |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 1103 | kfree(async_cow); |
| 1104 | } |
| 1105 | |
| 1106 | static int cow_file_range_async(struct inode *inode, struct page *locked_page, |
| 1107 | u64 start, u64 end, int *page_started, |
| 1108 | unsigned long *nr_written) |
| 1109 | { |
| 1110 | struct async_cow *async_cow; |
| 1111 | struct btrfs_root *root = BTRFS_I(inode)->root; |
| 1112 | unsigned long nr_pages; |
| 1113 | u64 cur_end; |
Liu Bo | 287082b | 2012-06-28 04:02:24 -0600 | [diff] [blame] | 1114 | int limit = 10 * 1024 * 1024; |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 1115 | |
Chris Mason | a3429ab | 2009-10-08 12:30:20 -0400 | [diff] [blame] | 1116 | clear_extent_bit(&BTRFS_I(inode)->io_tree, start, end, EXTENT_LOCKED, |
| 1117 | 1, 0, NULL, GFP_NOFS); |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 1118 | while (start < end) { |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 1119 | async_cow = kmalloc(sizeof(*async_cow), GFP_NOFS); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 1120 | BUG_ON(!async_cow); /* -ENOMEM */ |
Josef Bacik | 8180ef8 | 2012-06-08 15:16:12 -0400 | [diff] [blame] | 1121 | async_cow->inode = igrab(inode); |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 1122 | async_cow->root = root; |
| 1123 | async_cow->locked_page = locked_page; |
| 1124 | async_cow->start = start; |
| 1125 | |
Wang Shilong | f79707b | 2014-07-17 11:44:09 +0800 | [diff] [blame] | 1126 | if (BTRFS_I(inode)->flags & BTRFS_INODE_NOCOMPRESS && |
| 1127 | !btrfs_test_opt(root, FORCE_COMPRESS)) |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 1128 | cur_end = end; |
| 1129 | else |
| 1130 | cur_end = min(end, start + 512 * 1024 - 1); |
| 1131 | |
| 1132 | async_cow->end = cur_end; |
| 1133 | INIT_LIST_HEAD(&async_cow->extents); |
| 1134 | |
Liu Bo | 9e0af23 | 2014-08-15 23:36:53 +0800 | [diff] [blame] | 1135 | btrfs_init_work(&async_cow->work, |
| 1136 | btrfs_delalloc_helper, |
| 1137 | async_cow_start, async_cow_submit, |
| 1138 | async_cow_free); |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 1139 | |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 1140 | nr_pages = (cur_end - start + PAGE_CACHE_SIZE) >> |
| 1141 | PAGE_CACHE_SHIFT; |
| 1142 | atomic_add(nr_pages, &root->fs_info->async_delalloc_pages); |
| 1143 | |
Qu Wenruo | afe3d24 | 2014-02-28 10:46:07 +0800 | [diff] [blame] | 1144 | btrfs_queue_work(root->fs_info->delalloc_workers, |
| 1145 | &async_cow->work); |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 1146 | |
| 1147 | if (atomic_read(&root->fs_info->async_delalloc_pages) > limit) { |
| 1148 | wait_event(root->fs_info->async_submit_wait, |
| 1149 | (atomic_read(&root->fs_info->async_delalloc_pages) < |
| 1150 | limit)); |
| 1151 | } |
| 1152 | |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 1153 | while (atomic_read(&root->fs_info->async_submit_draining) && |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 1154 | atomic_read(&root->fs_info->async_delalloc_pages)) { |
| 1155 | wait_event(root->fs_info->async_submit_wait, |
| 1156 | (atomic_read(&root->fs_info->async_delalloc_pages) == |
| 1157 | 0)); |
| 1158 | } |
| 1159 | |
| 1160 | *nr_written += nr_pages; |
| 1161 | start = cur_end + 1; |
| 1162 | } |
| 1163 | *page_started = 1; |
| 1164 | return 0; |
Chris Mason | be20aa9 | 2007-12-17 20:14:01 -0500 | [diff] [blame] | 1165 | } |
| 1166 | |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 1167 | static noinline int csum_exist_in_range(struct btrfs_root *root, |
Yan Zheng | 17d217f | 2008-12-12 10:03:38 -0500 | [diff] [blame] | 1168 | u64 bytenr, u64 num_bytes) |
| 1169 | { |
| 1170 | int ret; |
| 1171 | struct btrfs_ordered_sum *sums; |
| 1172 | LIST_HEAD(list); |
| 1173 | |
Yan Zheng | 07d400a | 2009-01-06 11:42:00 -0500 | [diff] [blame] | 1174 | ret = btrfs_lookup_csums_range(root->fs_info->csum_root, bytenr, |
Arne Jansen | a2de733 | 2011-03-08 14:14:00 +0100 | [diff] [blame] | 1175 | bytenr + num_bytes - 1, &list, 0); |
Yan Zheng | 17d217f | 2008-12-12 10:03:38 -0500 | [diff] [blame] | 1176 | if (ret == 0 && list_empty(&list)) |
| 1177 | return 0; |
| 1178 | |
| 1179 | while (!list_empty(&list)) { |
| 1180 | sums = list_entry(list.next, struct btrfs_ordered_sum, list); |
| 1181 | list_del(&sums->list); |
| 1182 | kfree(sums); |
| 1183 | } |
| 1184 | return 1; |
| 1185 | } |
| 1186 | |
Chris Mason | d352ac6 | 2008-09-29 15:18:18 -0400 | [diff] [blame] | 1187 | /* |
| 1188 | * when nowcow writeback call back. This checks for snapshots or COW copies |
| 1189 | * of the extents that exist in the file, and COWs the file as required. |
| 1190 | * |
| 1191 | * If no cow copies or snapshots exist, we write directly to the existing |
| 1192 | * blocks on disk |
| 1193 | */ |
Chris Mason | 7f366cf | 2009-03-12 20:12:45 -0400 | [diff] [blame] | 1194 | static noinline int run_delalloc_nocow(struct inode *inode, |
| 1195 | struct page *locked_page, |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 1196 | u64 start, u64 end, int *page_started, int force, |
| 1197 | unsigned long *nr_written) |
Chris Mason | be20aa9 | 2007-12-17 20:14:01 -0500 | [diff] [blame] | 1198 | { |
Chris Mason | be20aa9 | 2007-12-17 20:14:01 -0500 | [diff] [blame] | 1199 | struct btrfs_root *root = BTRFS_I(inode)->root; |
Yan Zheng | 7ea394f | 2008-08-05 13:05:02 -0400 | [diff] [blame] | 1200 | struct btrfs_trans_handle *trans; |
Chris Mason | be20aa9 | 2007-12-17 20:14:01 -0500 | [diff] [blame] | 1201 | struct extent_buffer *leaf; |
Chris Mason | be20aa9 | 2007-12-17 20:14:01 -0500 | [diff] [blame] | 1202 | struct btrfs_path *path; |
Yan Zheng | 80ff385 | 2008-10-30 14:20:02 -0400 | [diff] [blame] | 1203 | struct btrfs_file_extent_item *fi; |
Chris Mason | be20aa9 | 2007-12-17 20:14:01 -0500 | [diff] [blame] | 1204 | struct btrfs_key found_key; |
Yan Zheng | 80ff385 | 2008-10-30 14:20:02 -0400 | [diff] [blame] | 1205 | u64 cow_start; |
| 1206 | u64 cur_offset; |
| 1207 | u64 extent_end; |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 1208 | u64 extent_offset; |
Yan Zheng | 80ff385 | 2008-10-30 14:20:02 -0400 | [diff] [blame] | 1209 | u64 disk_bytenr; |
| 1210 | u64 num_bytes; |
Josef Bacik | b493968 | 2012-12-03 10:31:19 -0500 | [diff] [blame] | 1211 | u64 disk_num_bytes; |
Josef Bacik | cc95bef | 2013-04-04 14:31:27 -0400 | [diff] [blame] | 1212 | u64 ram_bytes; |
Yan Zheng | 80ff385 | 2008-10-30 14:20:02 -0400 | [diff] [blame] | 1213 | int extent_type; |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 1214 | int ret, err; |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 1215 | int type; |
Yan Zheng | 80ff385 | 2008-10-30 14:20:02 -0400 | [diff] [blame] | 1216 | int nocow; |
| 1217 | int check_prev = 1; |
Li Zefan | 82d5902 | 2011-04-20 10:33:24 +0800 | [diff] [blame] | 1218 | bool nolock; |
Li Zefan | 33345d01 | 2011-04-20 10:31:50 +0800 | [diff] [blame] | 1219 | u64 ino = btrfs_ino(inode); |
Chris Mason | be20aa9 | 2007-12-17 20:14:01 -0500 | [diff] [blame] | 1220 | |
| 1221 | path = btrfs_alloc_path(); |
Josef Bacik | 17ca04a | 2012-05-31 15:58:55 -0400 | [diff] [blame] | 1222 | if (!path) { |
Josef Bacik | c2790a2 | 2013-07-29 11:20:47 -0400 | [diff] [blame] | 1223 | extent_clear_unlock_delalloc(inode, start, end, locked_page, |
| 1224 | EXTENT_LOCKED | EXTENT_DELALLOC | |
Josef Bacik | 151a41b | 2013-07-29 13:22:24 -0400 | [diff] [blame] | 1225 | EXTENT_DO_ACCOUNTING | |
| 1226 | EXTENT_DEFRAG, PAGE_UNLOCK | |
Josef Bacik | c2790a2 | 2013-07-29 11:20:47 -0400 | [diff] [blame] | 1227 | PAGE_CLEAR_DIRTY | |
| 1228 | PAGE_SET_WRITEBACK | |
| 1229 | PAGE_END_WRITEBACK); |
Mark Fasheh | d8926bb | 2011-07-13 10:38:47 -0700 | [diff] [blame] | 1230 | return -ENOMEM; |
Josef Bacik | 17ca04a | 2012-05-31 15:58:55 -0400 | [diff] [blame] | 1231 | } |
Li Zefan | 82d5902 | 2011-04-20 10:33:24 +0800 | [diff] [blame] | 1232 | |
Liu Bo | 83eea1f | 2012-07-10 05:28:39 -0600 | [diff] [blame] | 1233 | nolock = btrfs_is_free_space_inode(inode); |
Li Zefan | 82d5902 | 2011-04-20 10:33:24 +0800 | [diff] [blame] | 1234 | |
| 1235 | if (nolock) |
Josef Bacik | 7a7eaa4 | 2011-04-13 12:54:33 -0400 | [diff] [blame] | 1236 | trans = btrfs_join_transaction_nolock(root); |
Li Zefan | 82d5902 | 2011-04-20 10:33:24 +0800 | [diff] [blame] | 1237 | else |
Josef Bacik | 7a7eaa4 | 2011-04-13 12:54:33 -0400 | [diff] [blame] | 1238 | trans = btrfs_join_transaction(root); |
Chris Mason | ff5714c | 2011-05-28 07:00:39 -0400 | [diff] [blame] | 1239 | |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 1240 | if (IS_ERR(trans)) { |
Josef Bacik | c2790a2 | 2013-07-29 11:20:47 -0400 | [diff] [blame] | 1241 | extent_clear_unlock_delalloc(inode, start, end, locked_page, |
| 1242 | EXTENT_LOCKED | EXTENT_DELALLOC | |
Josef Bacik | 151a41b | 2013-07-29 13:22:24 -0400 | [diff] [blame] | 1243 | EXTENT_DO_ACCOUNTING | |
| 1244 | EXTENT_DEFRAG, PAGE_UNLOCK | |
Josef Bacik | c2790a2 | 2013-07-29 11:20:47 -0400 | [diff] [blame] | 1245 | PAGE_CLEAR_DIRTY | |
| 1246 | PAGE_SET_WRITEBACK | |
| 1247 | PAGE_END_WRITEBACK); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 1248 | btrfs_free_path(path); |
| 1249 | return PTR_ERR(trans); |
| 1250 | } |
| 1251 | |
Josef Bacik | 74b2107 | 2011-04-13 12:02:53 -0400 | [diff] [blame] | 1252 | trans->block_rsv = &root->fs_info->delalloc_block_rsv; |
Chris Mason | be20aa9 | 2007-12-17 20:14:01 -0500 | [diff] [blame] | 1253 | |
Yan Zheng | 80ff385 | 2008-10-30 14:20:02 -0400 | [diff] [blame] | 1254 | cow_start = (u64)-1; |
| 1255 | cur_offset = start; |
| 1256 | while (1) { |
Li Zefan | 33345d01 | 2011-04-20 10:31:50 +0800 | [diff] [blame] | 1257 | ret = btrfs_lookup_file_extent(trans, root, path, ino, |
Yan Zheng | 80ff385 | 2008-10-30 14:20:02 -0400 | [diff] [blame] | 1258 | cur_offset, 0); |
Josef Bacik | d788a34 | 2013-10-25 16:55:08 -0400 | [diff] [blame] | 1259 | if (ret < 0) |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 1260 | goto error; |
Yan Zheng | 80ff385 | 2008-10-30 14:20:02 -0400 | [diff] [blame] | 1261 | if (ret > 0 && path->slots[0] > 0 && check_prev) { |
| 1262 | leaf = path->nodes[0]; |
| 1263 | btrfs_item_key_to_cpu(leaf, &found_key, |
| 1264 | path->slots[0] - 1); |
Li Zefan | 33345d01 | 2011-04-20 10:31:50 +0800 | [diff] [blame] | 1265 | if (found_key.objectid == ino && |
Yan Zheng | 80ff385 | 2008-10-30 14:20:02 -0400 | [diff] [blame] | 1266 | found_key.type == BTRFS_EXTENT_DATA_KEY) |
| 1267 | path->slots[0]--; |
| 1268 | } |
| 1269 | check_prev = 0; |
| 1270 | next_slot: |
| 1271 | leaf = path->nodes[0]; |
| 1272 | if (path->slots[0] >= btrfs_header_nritems(leaf)) { |
| 1273 | ret = btrfs_next_leaf(root, path); |
Josef Bacik | d788a34 | 2013-10-25 16:55:08 -0400 | [diff] [blame] | 1274 | if (ret < 0) |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 1275 | goto error; |
Yan Zheng | 80ff385 | 2008-10-30 14:20:02 -0400 | [diff] [blame] | 1276 | if (ret > 0) |
| 1277 | break; |
| 1278 | leaf = path->nodes[0]; |
| 1279 | } |
Chris Mason | be20aa9 | 2007-12-17 20:14:01 -0500 | [diff] [blame] | 1280 | |
Yan Zheng | 80ff385 | 2008-10-30 14:20:02 -0400 | [diff] [blame] | 1281 | nocow = 0; |
| 1282 | disk_bytenr = 0; |
Yan Zheng | 17d217f | 2008-12-12 10:03:38 -0500 | [diff] [blame] | 1283 | num_bytes = 0; |
Yan Zheng | 80ff385 | 2008-10-30 14:20:02 -0400 | [diff] [blame] | 1284 | btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]); |
Chris Mason | be20aa9 | 2007-12-17 20:14:01 -0500 | [diff] [blame] | 1285 | |
Li Zefan | 33345d01 | 2011-04-20 10:31:50 +0800 | [diff] [blame] | 1286 | if (found_key.objectid > ino || |
Yan Zheng | 80ff385 | 2008-10-30 14:20:02 -0400 | [diff] [blame] | 1287 | found_key.type > BTRFS_EXTENT_DATA_KEY || |
| 1288 | found_key.offset > end) |
| 1289 | break; |
Chris Mason | be20aa9 | 2007-12-17 20:14:01 -0500 | [diff] [blame] | 1290 | |
Yan Zheng | 80ff385 | 2008-10-30 14:20:02 -0400 | [diff] [blame] | 1291 | if (found_key.offset > cur_offset) { |
| 1292 | extent_end = found_key.offset; |
Chris Mason | e9061e2 | 2009-10-09 09:57:45 -0400 | [diff] [blame] | 1293 | extent_type = 0; |
Yan Zheng | 80ff385 | 2008-10-30 14:20:02 -0400 | [diff] [blame] | 1294 | goto out_check; |
| 1295 | } |
Chris Mason | c31f883 | 2008-01-08 15:46:31 -0500 | [diff] [blame] | 1296 | |
Yan Zheng | 80ff385 | 2008-10-30 14:20:02 -0400 | [diff] [blame] | 1297 | fi = btrfs_item_ptr(leaf, path->slots[0], |
| 1298 | struct btrfs_file_extent_item); |
| 1299 | extent_type = btrfs_file_extent_type(leaf, fi); |
Chris Mason | be20aa9 | 2007-12-17 20:14:01 -0500 | [diff] [blame] | 1300 | |
Josef Bacik | cc95bef | 2013-04-04 14:31:27 -0400 | [diff] [blame] | 1301 | ram_bytes = btrfs_file_extent_ram_bytes(leaf, fi); |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 1302 | if (extent_type == BTRFS_FILE_EXTENT_REG || |
| 1303 | extent_type == BTRFS_FILE_EXTENT_PREALLOC) { |
Yan Zheng | 80ff385 | 2008-10-30 14:20:02 -0400 | [diff] [blame] | 1304 | disk_bytenr = btrfs_file_extent_disk_bytenr(leaf, fi); |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 1305 | extent_offset = btrfs_file_extent_offset(leaf, fi); |
Yan Zheng | 80ff385 | 2008-10-30 14:20:02 -0400 | [diff] [blame] | 1306 | extent_end = found_key.offset + |
| 1307 | btrfs_file_extent_num_bytes(leaf, fi); |
Josef Bacik | b493968 | 2012-12-03 10:31:19 -0500 | [diff] [blame] | 1308 | disk_num_bytes = |
| 1309 | btrfs_file_extent_disk_num_bytes(leaf, fi); |
Yan Zheng | 80ff385 | 2008-10-30 14:20:02 -0400 | [diff] [blame] | 1310 | if (extent_end <= start) { |
| 1311 | path->slots[0]++; |
| 1312 | goto next_slot; |
| 1313 | } |
Yan Zheng | 17d217f | 2008-12-12 10:03:38 -0500 | [diff] [blame] | 1314 | if (disk_bytenr == 0) |
| 1315 | goto out_check; |
Yan Zheng | 80ff385 | 2008-10-30 14:20:02 -0400 | [diff] [blame] | 1316 | if (btrfs_file_extent_compression(leaf, fi) || |
| 1317 | btrfs_file_extent_encryption(leaf, fi) || |
| 1318 | btrfs_file_extent_other_encoding(leaf, fi)) |
| 1319 | goto out_check; |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 1320 | if (extent_type == BTRFS_FILE_EXTENT_REG && !force) |
| 1321 | goto out_check; |
Yan Zheng | d2fb343 | 2008-12-11 16:30:39 -0500 | [diff] [blame] | 1322 | if (btrfs_extent_readonly(root, disk_bytenr)) |
Yan Zheng | 80ff385 | 2008-10-30 14:20:02 -0400 | [diff] [blame] | 1323 | goto out_check; |
Li Zefan | 33345d01 | 2011-04-20 10:31:50 +0800 | [diff] [blame] | 1324 | if (btrfs_cross_ref_exist(trans, root, ino, |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 1325 | found_key.offset - |
| 1326 | extent_offset, disk_bytenr)) |
Yan Zheng | 17d217f | 2008-12-12 10:03:38 -0500 | [diff] [blame] | 1327 | goto out_check; |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 1328 | disk_bytenr += extent_offset; |
Yan Zheng | 17d217f | 2008-12-12 10:03:38 -0500 | [diff] [blame] | 1329 | disk_bytenr += cur_offset - found_key.offset; |
| 1330 | num_bytes = min(end + 1, extent_end) - cur_offset; |
| 1331 | /* |
Wang Shilong | e9894fd | 2014-03-27 11:12:25 +0800 | [diff] [blame] | 1332 | * if there are pending snapshots for this root, |
| 1333 | * we fall into common COW way. |
| 1334 | */ |
| 1335 | if (!nolock) { |
| 1336 | err = btrfs_start_nocow_write(root); |
| 1337 | if (!err) |
| 1338 | goto out_check; |
| 1339 | } |
| 1340 | /* |
Yan Zheng | 17d217f | 2008-12-12 10:03:38 -0500 | [diff] [blame] | 1341 | * force cow if csum exists in the range. |
| 1342 | * this ensure that csum for a given extent are |
| 1343 | * either valid or do not exist. |
| 1344 | */ |
| 1345 | if (csum_exist_in_range(root, disk_bytenr, num_bytes)) |
| 1346 | goto out_check; |
Yan Zheng | 80ff385 | 2008-10-30 14:20:02 -0400 | [diff] [blame] | 1347 | nocow = 1; |
| 1348 | } else if (extent_type == BTRFS_FILE_EXTENT_INLINE) { |
| 1349 | extent_end = found_key.offset + |
Chris Mason | 514ac8a | 2014-01-03 21:07:00 -0800 | [diff] [blame] | 1350 | btrfs_file_extent_inline_len(leaf, |
| 1351 | path->slots[0], fi); |
Yan Zheng | 80ff385 | 2008-10-30 14:20:02 -0400 | [diff] [blame] | 1352 | extent_end = ALIGN(extent_end, root->sectorsize); |
| 1353 | } else { |
| 1354 | BUG_ON(1); |
| 1355 | } |
| 1356 | out_check: |
| 1357 | if (extent_end <= start) { |
| 1358 | path->slots[0]++; |
Wang Shilong | e9894fd | 2014-03-27 11:12:25 +0800 | [diff] [blame] | 1359 | if (!nolock && nocow) |
| 1360 | btrfs_end_nocow_write(root); |
Yan Zheng | 80ff385 | 2008-10-30 14:20:02 -0400 | [diff] [blame] | 1361 | goto next_slot; |
| 1362 | } |
| 1363 | if (!nocow) { |
| 1364 | if (cow_start == (u64)-1) |
| 1365 | cow_start = cur_offset; |
| 1366 | cur_offset = extent_end; |
| 1367 | if (cur_offset > end) |
| 1368 | break; |
| 1369 | path->slots[0]++; |
| 1370 | goto next_slot; |
Yan Zheng | 7ea394f | 2008-08-05 13:05:02 -0400 | [diff] [blame] | 1371 | } |
Chris Mason | be20aa9 | 2007-12-17 20:14:01 -0500 | [diff] [blame] | 1372 | |
David Sterba | b3b4aa7 | 2011-04-21 01:20:15 +0200 | [diff] [blame] | 1373 | btrfs_release_path(path); |
Yan Zheng | 80ff385 | 2008-10-30 14:20:02 -0400 | [diff] [blame] | 1374 | if (cow_start != (u64)-1) { |
Josef Bacik | 0036158 | 2013-08-14 14:02:47 -0400 | [diff] [blame] | 1375 | ret = cow_file_range(inode, locked_page, |
| 1376 | cow_start, found_key.offset - 1, |
| 1377 | page_started, nr_written, 1); |
Wang Shilong | e9894fd | 2014-03-27 11:12:25 +0800 | [diff] [blame] | 1378 | if (ret) { |
| 1379 | if (!nolock && nocow) |
| 1380 | btrfs_end_nocow_write(root); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 1381 | goto error; |
Wang Shilong | e9894fd | 2014-03-27 11:12:25 +0800 | [diff] [blame] | 1382 | } |
Yan Zheng | 80ff385 | 2008-10-30 14:20:02 -0400 | [diff] [blame] | 1383 | cow_start = (u64)-1; |
Yan Zheng | 7ea394f | 2008-08-05 13:05:02 -0400 | [diff] [blame] | 1384 | } |
Yan Zheng | 80ff385 | 2008-10-30 14:20:02 -0400 | [diff] [blame] | 1385 | |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 1386 | if (extent_type == BTRFS_FILE_EXTENT_PREALLOC) { |
| 1387 | struct extent_map *em; |
| 1388 | struct extent_map_tree *em_tree; |
| 1389 | em_tree = &BTRFS_I(inode)->extent_tree; |
David Sterba | 172ddd6 | 2011-04-21 00:48:27 +0200 | [diff] [blame] | 1390 | em = alloc_extent_map(); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 1391 | BUG_ON(!em); /* -ENOMEM */ |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 1392 | em->start = cur_offset; |
Josef Bacik | 70c8a91 | 2012-10-11 16:54:30 -0400 | [diff] [blame] | 1393 | em->orig_start = found_key.offset - extent_offset; |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 1394 | em->len = num_bytes; |
| 1395 | em->block_len = num_bytes; |
| 1396 | em->block_start = disk_bytenr; |
Josef Bacik | b493968 | 2012-12-03 10:31:19 -0500 | [diff] [blame] | 1397 | em->orig_block_len = disk_num_bytes; |
Josef Bacik | cc95bef | 2013-04-04 14:31:27 -0400 | [diff] [blame] | 1398 | em->ram_bytes = ram_bytes; |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 1399 | em->bdev = root->fs_info->fs_devices->latest_bdev; |
Josef Bacik | 2ab28f3 | 2012-10-12 15:27:49 -0400 | [diff] [blame] | 1400 | em->mod_start = em->start; |
| 1401 | em->mod_len = em->len; |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 1402 | set_bit(EXTENT_FLAG_PINNED, &em->flags); |
Josef Bacik | b11e234 | 2012-12-03 10:58:15 -0500 | [diff] [blame] | 1403 | set_bit(EXTENT_FLAG_FILLING, &em->flags); |
Josef Bacik | 70c8a91 | 2012-10-11 16:54:30 -0400 | [diff] [blame] | 1404 | em->generation = -1; |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 1405 | while (1) { |
Chris Mason | 890871b | 2009-09-02 16:24:52 -0400 | [diff] [blame] | 1406 | write_lock(&em_tree->lock); |
Josef Bacik | 09a2a8f9 | 2013-04-05 16:51:15 -0400 | [diff] [blame] | 1407 | ret = add_extent_mapping(em_tree, em, 1); |
Chris Mason | 890871b | 2009-09-02 16:24:52 -0400 | [diff] [blame] | 1408 | write_unlock(&em_tree->lock); |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 1409 | if (ret != -EEXIST) { |
| 1410 | free_extent_map(em); |
| 1411 | break; |
| 1412 | } |
| 1413 | btrfs_drop_extent_cache(inode, em->start, |
| 1414 | em->start + em->len - 1, 0); |
| 1415 | } |
| 1416 | type = BTRFS_ORDERED_PREALLOC; |
| 1417 | } else { |
| 1418 | type = BTRFS_ORDERED_NOCOW; |
| 1419 | } |
Yan Zheng | 80ff385 | 2008-10-30 14:20:02 -0400 | [diff] [blame] | 1420 | |
| 1421 | ret = btrfs_add_ordered_extent(inode, cur_offset, disk_bytenr, |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 1422 | num_bytes, num_bytes, type); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 1423 | BUG_ON(ret); /* -ENOMEM */ |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 1424 | |
Yan, Zheng | efa5646 | 2010-05-16 10:49:59 -0400 | [diff] [blame] | 1425 | if (root->root_key.objectid == |
| 1426 | BTRFS_DATA_RELOC_TREE_OBJECTID) { |
| 1427 | ret = btrfs_reloc_clone_csums(inode, cur_offset, |
| 1428 | num_bytes); |
Wang Shilong | e9894fd | 2014-03-27 11:12:25 +0800 | [diff] [blame] | 1429 | if (ret) { |
| 1430 | if (!nolock && nocow) |
| 1431 | btrfs_end_nocow_write(root); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 1432 | goto error; |
Wang Shilong | e9894fd | 2014-03-27 11:12:25 +0800 | [diff] [blame] | 1433 | } |
Yan, Zheng | efa5646 | 2010-05-16 10:49:59 -0400 | [diff] [blame] | 1434 | } |
| 1435 | |
Josef Bacik | c2790a2 | 2013-07-29 11:20:47 -0400 | [diff] [blame] | 1436 | extent_clear_unlock_delalloc(inode, cur_offset, |
| 1437 | cur_offset + num_bytes - 1, |
| 1438 | locked_page, EXTENT_LOCKED | |
| 1439 | EXTENT_DELALLOC, PAGE_UNLOCK | |
| 1440 | PAGE_SET_PRIVATE2); |
Wang Shilong | e9894fd | 2014-03-27 11:12:25 +0800 | [diff] [blame] | 1441 | if (!nolock && nocow) |
| 1442 | btrfs_end_nocow_write(root); |
Yan Zheng | 80ff385 | 2008-10-30 14:20:02 -0400 | [diff] [blame] | 1443 | cur_offset = extent_end; |
| 1444 | if (cur_offset > end) |
| 1445 | break; |
Yan Zheng | 7ea394f | 2008-08-05 13:05:02 -0400 | [diff] [blame] | 1446 | } |
David Sterba | b3b4aa7 | 2011-04-21 01:20:15 +0200 | [diff] [blame] | 1447 | btrfs_release_path(path); |
Yan Zheng | 80ff385 | 2008-10-30 14:20:02 -0400 | [diff] [blame] | 1448 | |
Josef Bacik | 17ca04a | 2012-05-31 15:58:55 -0400 | [diff] [blame] | 1449 | if (cur_offset <= end && cow_start == (u64)-1) { |
Yan Zheng | 80ff385 | 2008-10-30 14:20:02 -0400 | [diff] [blame] | 1450 | cow_start = cur_offset; |
Josef Bacik | 17ca04a | 2012-05-31 15:58:55 -0400 | [diff] [blame] | 1451 | cur_offset = end; |
| 1452 | } |
| 1453 | |
Yan Zheng | 80ff385 | 2008-10-30 14:20:02 -0400 | [diff] [blame] | 1454 | if (cow_start != (u64)-1) { |
Josef Bacik | 0036158 | 2013-08-14 14:02:47 -0400 | [diff] [blame] | 1455 | ret = cow_file_range(inode, locked_page, cow_start, end, |
| 1456 | page_started, nr_written, 1); |
Josef Bacik | d788a34 | 2013-10-25 16:55:08 -0400 | [diff] [blame] | 1457 | if (ret) |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 1458 | goto error; |
Yan Zheng | 80ff385 | 2008-10-30 14:20:02 -0400 | [diff] [blame] | 1459 | } |
| 1460 | |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 1461 | error: |
Miao Xie | a698d075 | 2012-09-20 01:51:59 -0600 | [diff] [blame] | 1462 | err = btrfs_end_transaction(trans, root); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 1463 | if (!ret) |
| 1464 | ret = err; |
| 1465 | |
Josef Bacik | 17ca04a | 2012-05-31 15:58:55 -0400 | [diff] [blame] | 1466 | if (ret && cur_offset < end) |
Josef Bacik | c2790a2 | 2013-07-29 11:20:47 -0400 | [diff] [blame] | 1467 | extent_clear_unlock_delalloc(inode, cur_offset, end, |
| 1468 | locked_page, EXTENT_LOCKED | |
Josef Bacik | 151a41b | 2013-07-29 13:22:24 -0400 | [diff] [blame] | 1469 | EXTENT_DELALLOC | EXTENT_DEFRAG | |
| 1470 | EXTENT_DO_ACCOUNTING, PAGE_UNLOCK | |
| 1471 | PAGE_CLEAR_DIRTY | |
Josef Bacik | c2790a2 | 2013-07-29 11:20:47 -0400 | [diff] [blame] | 1472 | PAGE_SET_WRITEBACK | |
| 1473 | PAGE_END_WRITEBACK); |
Yan Zheng | 7ea394f | 2008-08-05 13:05:02 -0400 | [diff] [blame] | 1474 | btrfs_free_path(path); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 1475 | return ret; |
Chris Mason | be20aa9 | 2007-12-17 20:14:01 -0500 | [diff] [blame] | 1476 | } |
| 1477 | |
Wang Shilong | 47059d9 | 2014-07-03 18:22:07 +0800 | [diff] [blame] | 1478 | static inline int need_force_cow(struct inode *inode, u64 start, u64 end) |
| 1479 | { |
| 1480 | |
| 1481 | if (!(BTRFS_I(inode)->flags & BTRFS_INODE_NODATACOW) && |
| 1482 | !(BTRFS_I(inode)->flags & BTRFS_INODE_PREALLOC)) |
| 1483 | return 0; |
| 1484 | |
| 1485 | /* |
| 1486 | * @defrag_bytes is a hint value, no spinlock held here, |
| 1487 | * if is not zero, it means the file is defragging. |
| 1488 | * Force cow if given extent needs to be defragged. |
| 1489 | */ |
| 1490 | if (BTRFS_I(inode)->defrag_bytes && |
| 1491 | test_range_bit(&BTRFS_I(inode)->io_tree, start, end, |
| 1492 | EXTENT_DEFRAG, 0, NULL)) |
| 1493 | return 1; |
| 1494 | |
| 1495 | return 0; |
| 1496 | } |
| 1497 | |
Chris Mason | d352ac6 | 2008-09-29 15:18:18 -0400 | [diff] [blame] | 1498 | /* |
| 1499 | * extent_io.c call back to do delayed allocation processing |
| 1500 | */ |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 1501 | static int run_delalloc_range(struct inode *inode, struct page *locked_page, |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 1502 | u64 start, u64 end, int *page_started, |
| 1503 | unsigned long *nr_written) |
Chris Mason | be20aa9 | 2007-12-17 20:14:01 -0500 | [diff] [blame] | 1504 | { |
Chris Mason | be20aa9 | 2007-12-17 20:14:01 -0500 | [diff] [blame] | 1505 | int ret; |
Wang Shilong | 47059d9 | 2014-07-03 18:22:07 +0800 | [diff] [blame] | 1506 | int force_cow = need_force_cow(inode, start, end); |
Chris Mason | a213501 | 2008-06-25 16:01:30 -0400 | [diff] [blame] | 1507 | |
Wang Shilong | 47059d9 | 2014-07-03 18:22:07 +0800 | [diff] [blame] | 1508 | if (BTRFS_I(inode)->flags & BTRFS_INODE_NODATACOW && !force_cow) { |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 1509 | ret = run_delalloc_nocow(inode, locked_page, start, end, |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 1510 | page_started, 1, nr_written); |
Wang Shilong | 47059d9 | 2014-07-03 18:22:07 +0800 | [diff] [blame] | 1511 | } else if (BTRFS_I(inode)->flags & BTRFS_INODE_PREALLOC && !force_cow) { |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 1512 | ret = run_delalloc_nocow(inode, locked_page, start, end, |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 1513 | page_started, 0, nr_written); |
Wang Shilong | 7816030 | 2014-07-17 11:44:10 +0800 | [diff] [blame] | 1514 | } else if (!inode_need_compress(inode)) { |
Chris Mason | 7f366cf | 2009-03-12 20:12:45 -0400 | [diff] [blame] | 1515 | ret = cow_file_range(inode, locked_page, start, end, |
| 1516 | page_started, nr_written, 1); |
Josef Bacik | 7ddf5a4 | 2012-06-08 15:26:47 -0400 | [diff] [blame] | 1517 | } else { |
| 1518 | set_bit(BTRFS_INODE_HAS_ASYNC_EXTENT, |
| 1519 | &BTRFS_I(inode)->runtime_flags); |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 1520 | ret = cow_file_range_async(inode, locked_page, start, end, |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 1521 | page_started, nr_written); |
Josef Bacik | 7ddf5a4 | 2012-06-08 15:26:47 -0400 | [diff] [blame] | 1522 | } |
Chris Mason | b888db2b | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 1523 | return ret; |
| 1524 | } |
| 1525 | |
Jeff Mahoney | 1bf8504 | 2011-07-21 16:56:09 +0000 | [diff] [blame] | 1526 | static void btrfs_split_extent_hook(struct inode *inode, |
| 1527 | struct extent_state *orig, u64 split) |
Josef Bacik | 9ed74f2 | 2009-09-11 16:12:44 -0400 | [diff] [blame] | 1528 | { |
Yan, Zheng | 0ca1f7c | 2010-05-16 10:48:47 -0400 | [diff] [blame] | 1529 | /* not delalloc, ignore it */ |
Josef Bacik | 9ed74f2 | 2009-09-11 16:12:44 -0400 | [diff] [blame] | 1530 | if (!(orig->state & EXTENT_DELALLOC)) |
Jeff Mahoney | 1bf8504 | 2011-07-21 16:56:09 +0000 | [diff] [blame] | 1531 | return; |
Josef Bacik | 9ed74f2 | 2009-09-11 16:12:44 -0400 | [diff] [blame] | 1532 | |
Josef Bacik | 9e0baf6 | 2011-07-15 15:16:44 +0000 | [diff] [blame] | 1533 | spin_lock(&BTRFS_I(inode)->lock); |
| 1534 | BTRFS_I(inode)->outstanding_extents++; |
| 1535 | spin_unlock(&BTRFS_I(inode)->lock); |
Josef Bacik | 9ed74f2 | 2009-09-11 16:12:44 -0400 | [diff] [blame] | 1536 | } |
| 1537 | |
| 1538 | /* |
| 1539 | * extent_io.c merge_extent_hook, used to track merged delayed allocation |
| 1540 | * extents so we can keep track of new extents that are just merged onto old |
| 1541 | * extents, such as when we are doing sequential writes, so we can properly |
| 1542 | * account for the metadata space we'll need. |
| 1543 | */ |
Jeff Mahoney | 1bf8504 | 2011-07-21 16:56:09 +0000 | [diff] [blame] | 1544 | static void btrfs_merge_extent_hook(struct inode *inode, |
| 1545 | struct extent_state *new, |
| 1546 | struct extent_state *other) |
Josef Bacik | 9ed74f2 | 2009-09-11 16:12:44 -0400 | [diff] [blame] | 1547 | { |
Josef Bacik | 9ed74f2 | 2009-09-11 16:12:44 -0400 | [diff] [blame] | 1548 | /* not delalloc, ignore it */ |
| 1549 | if (!(other->state & EXTENT_DELALLOC)) |
Jeff Mahoney | 1bf8504 | 2011-07-21 16:56:09 +0000 | [diff] [blame] | 1550 | return; |
Josef Bacik | 9ed74f2 | 2009-09-11 16:12:44 -0400 | [diff] [blame] | 1551 | |
Josef Bacik | 9e0baf6 | 2011-07-15 15:16:44 +0000 | [diff] [blame] | 1552 | spin_lock(&BTRFS_I(inode)->lock); |
| 1553 | BTRFS_I(inode)->outstanding_extents--; |
| 1554 | spin_unlock(&BTRFS_I(inode)->lock); |
Josef Bacik | 9ed74f2 | 2009-09-11 16:12:44 -0400 | [diff] [blame] | 1555 | } |
| 1556 | |
Miao Xie | eb73c1b | 2013-05-15 07:48:22 +0000 | [diff] [blame] | 1557 | static void btrfs_add_delalloc_inodes(struct btrfs_root *root, |
| 1558 | struct inode *inode) |
| 1559 | { |
| 1560 | spin_lock(&root->delalloc_lock); |
| 1561 | if (list_empty(&BTRFS_I(inode)->delalloc_inodes)) { |
| 1562 | list_add_tail(&BTRFS_I(inode)->delalloc_inodes, |
| 1563 | &root->delalloc_inodes); |
| 1564 | set_bit(BTRFS_INODE_IN_DELALLOC_LIST, |
| 1565 | &BTRFS_I(inode)->runtime_flags); |
| 1566 | root->nr_delalloc_inodes++; |
| 1567 | if (root->nr_delalloc_inodes == 1) { |
| 1568 | spin_lock(&root->fs_info->delalloc_root_lock); |
| 1569 | BUG_ON(!list_empty(&root->delalloc_root)); |
| 1570 | list_add_tail(&root->delalloc_root, |
| 1571 | &root->fs_info->delalloc_roots); |
| 1572 | spin_unlock(&root->fs_info->delalloc_root_lock); |
| 1573 | } |
| 1574 | } |
| 1575 | spin_unlock(&root->delalloc_lock); |
| 1576 | } |
| 1577 | |
| 1578 | static void btrfs_del_delalloc_inode(struct btrfs_root *root, |
| 1579 | struct inode *inode) |
| 1580 | { |
| 1581 | spin_lock(&root->delalloc_lock); |
| 1582 | if (!list_empty(&BTRFS_I(inode)->delalloc_inodes)) { |
| 1583 | list_del_init(&BTRFS_I(inode)->delalloc_inodes); |
| 1584 | clear_bit(BTRFS_INODE_IN_DELALLOC_LIST, |
| 1585 | &BTRFS_I(inode)->runtime_flags); |
| 1586 | root->nr_delalloc_inodes--; |
| 1587 | if (!root->nr_delalloc_inodes) { |
| 1588 | spin_lock(&root->fs_info->delalloc_root_lock); |
| 1589 | BUG_ON(list_empty(&root->delalloc_root)); |
| 1590 | list_del_init(&root->delalloc_root); |
| 1591 | spin_unlock(&root->fs_info->delalloc_root_lock); |
| 1592 | } |
| 1593 | } |
| 1594 | spin_unlock(&root->delalloc_lock); |
| 1595 | } |
| 1596 | |
Chris Mason | d352ac6 | 2008-09-29 15:18:18 -0400 | [diff] [blame] | 1597 | /* |
| 1598 | * extent_io.c set_bit_hook, used to track delayed allocation |
| 1599 | * bytes in this file, and to maintain the list of inodes that |
| 1600 | * have pending delalloc work to be done. |
| 1601 | */ |
Jeff Mahoney | 1bf8504 | 2011-07-21 16:56:09 +0000 | [diff] [blame] | 1602 | static void btrfs_set_bit_hook(struct inode *inode, |
David Sterba | 4107488 | 2013-04-29 13:38:46 +0000 | [diff] [blame] | 1603 | struct extent_state *state, unsigned long *bits) |
Chris Mason | 291d673 | 2008-01-29 15:55:23 -0500 | [diff] [blame] | 1604 | { |
Josef Bacik | 9ed74f2 | 2009-09-11 16:12:44 -0400 | [diff] [blame] | 1605 | |
Wang Shilong | 47059d9 | 2014-07-03 18:22:07 +0800 | [diff] [blame] | 1606 | if ((*bits & EXTENT_DEFRAG) && !(*bits & EXTENT_DELALLOC)) |
| 1607 | WARN_ON(1); |
Chris Mason | 75eff68 | 2008-12-15 15:54:40 -0500 | [diff] [blame] | 1608 | /* |
| 1609 | * set_bit and clear bit hooks normally require _irqsave/restore |
Sergei Trofimovich | 27160b6 | 2011-05-20 20:20:32 +0000 | [diff] [blame] | 1610 | * but in this case, we are only testing for the DELALLOC |
Chris Mason | 75eff68 | 2008-12-15 15:54:40 -0500 | [diff] [blame] | 1611 | * bit, which is only set or cleared with irqs on |
| 1612 | */ |
Yan, Zheng | 0ca1f7c | 2010-05-16 10:48:47 -0400 | [diff] [blame] | 1613 | if (!(state->state & EXTENT_DELALLOC) && (*bits & EXTENT_DELALLOC)) { |
Chris Mason | 291d673 | 2008-01-29 15:55:23 -0500 | [diff] [blame] | 1614 | struct btrfs_root *root = BTRFS_I(inode)->root; |
Yan, Zheng | 0ca1f7c | 2010-05-16 10:48:47 -0400 | [diff] [blame] | 1615 | u64 len = state->end + 1 - state->start; |
Liu Bo | 83eea1f | 2012-07-10 05:28:39 -0600 | [diff] [blame] | 1616 | bool do_list = !btrfs_is_free_space_inode(inode); |
Josef Bacik | 9ed74f2 | 2009-09-11 16:12:44 -0400 | [diff] [blame] | 1617 | |
Josef Bacik | 9e0baf6 | 2011-07-15 15:16:44 +0000 | [diff] [blame] | 1618 | if (*bits & EXTENT_FIRST_DELALLOC) { |
Yan, Zheng | 0ca1f7c | 2010-05-16 10:48:47 -0400 | [diff] [blame] | 1619 | *bits &= ~EXTENT_FIRST_DELALLOC; |
Josef Bacik | 9e0baf6 | 2011-07-15 15:16:44 +0000 | [diff] [blame] | 1620 | } else { |
| 1621 | spin_lock(&BTRFS_I(inode)->lock); |
| 1622 | BTRFS_I(inode)->outstanding_extents++; |
| 1623 | spin_unlock(&BTRFS_I(inode)->lock); |
| 1624 | } |
Josef Bacik | 287a0ab | 2010-03-19 18:07:23 +0000 | [diff] [blame] | 1625 | |
Miao Xie | 963d678 | 2013-01-29 10:10:51 +0000 | [diff] [blame] | 1626 | __percpu_counter_add(&root->fs_info->delalloc_bytes, len, |
| 1627 | root->fs_info->delalloc_batch); |
Miao Xie | df0af1a | 2013-01-29 10:11:59 +0000 | [diff] [blame] | 1628 | spin_lock(&BTRFS_I(inode)->lock); |
Yan, Zheng | 0ca1f7c | 2010-05-16 10:48:47 -0400 | [diff] [blame] | 1629 | BTRFS_I(inode)->delalloc_bytes += len; |
Wang Shilong | 47059d9 | 2014-07-03 18:22:07 +0800 | [diff] [blame] | 1630 | if (*bits & EXTENT_DEFRAG) |
| 1631 | BTRFS_I(inode)->defrag_bytes += len; |
Miao Xie | df0af1a | 2013-01-29 10:11:59 +0000 | [diff] [blame] | 1632 | if (do_list && !test_bit(BTRFS_INODE_IN_DELALLOC_LIST, |
Miao Xie | eb73c1b | 2013-05-15 07:48:22 +0000 | [diff] [blame] | 1633 | &BTRFS_I(inode)->runtime_flags)) |
| 1634 | btrfs_add_delalloc_inodes(root, inode); |
Miao Xie | df0af1a | 2013-01-29 10:11:59 +0000 | [diff] [blame] | 1635 | spin_unlock(&BTRFS_I(inode)->lock); |
Chris Mason | 291d673 | 2008-01-29 15:55:23 -0500 | [diff] [blame] | 1636 | } |
Chris Mason | 291d673 | 2008-01-29 15:55:23 -0500 | [diff] [blame] | 1637 | } |
| 1638 | |
Chris Mason | d352ac6 | 2008-09-29 15:18:18 -0400 | [diff] [blame] | 1639 | /* |
| 1640 | * extent_io.c clear_bit_hook, see set_bit_hook for why |
| 1641 | */ |
Jeff Mahoney | 1bf8504 | 2011-07-21 16:56:09 +0000 | [diff] [blame] | 1642 | static void btrfs_clear_bit_hook(struct inode *inode, |
David Sterba | 4107488 | 2013-04-29 13:38:46 +0000 | [diff] [blame] | 1643 | struct extent_state *state, |
| 1644 | unsigned long *bits) |
Chris Mason | 291d673 | 2008-01-29 15:55:23 -0500 | [diff] [blame] | 1645 | { |
Wang Shilong | 47059d9 | 2014-07-03 18:22:07 +0800 | [diff] [blame] | 1646 | u64 len = state->end + 1 - state->start; |
| 1647 | |
| 1648 | spin_lock(&BTRFS_I(inode)->lock); |
| 1649 | if ((state->state & EXTENT_DEFRAG) && (*bits & EXTENT_DEFRAG)) |
| 1650 | BTRFS_I(inode)->defrag_bytes -= len; |
| 1651 | spin_unlock(&BTRFS_I(inode)->lock); |
| 1652 | |
Chris Mason | 75eff68 | 2008-12-15 15:54:40 -0500 | [diff] [blame] | 1653 | /* |
| 1654 | * set_bit and clear bit hooks normally require _irqsave/restore |
Sergei Trofimovich | 27160b6 | 2011-05-20 20:20:32 +0000 | [diff] [blame] | 1655 | * but in this case, we are only testing for the DELALLOC |
Chris Mason | 75eff68 | 2008-12-15 15:54:40 -0500 | [diff] [blame] | 1656 | * bit, which is only set or cleared with irqs on |
| 1657 | */ |
Yan, Zheng | 0ca1f7c | 2010-05-16 10:48:47 -0400 | [diff] [blame] | 1658 | if ((state->state & EXTENT_DELALLOC) && (*bits & EXTENT_DELALLOC)) { |
Chris Mason | 291d673 | 2008-01-29 15:55:23 -0500 | [diff] [blame] | 1659 | struct btrfs_root *root = BTRFS_I(inode)->root; |
Liu Bo | 83eea1f | 2012-07-10 05:28:39 -0600 | [diff] [blame] | 1660 | bool do_list = !btrfs_is_free_space_inode(inode); |
Chris Mason | bcbfce8 | 2008-04-22 13:26:47 -0400 | [diff] [blame] | 1661 | |
Josef Bacik | 9e0baf6 | 2011-07-15 15:16:44 +0000 | [diff] [blame] | 1662 | if (*bits & EXTENT_FIRST_DELALLOC) { |
Yan, Zheng | 0ca1f7c | 2010-05-16 10:48:47 -0400 | [diff] [blame] | 1663 | *bits &= ~EXTENT_FIRST_DELALLOC; |
Josef Bacik | 9e0baf6 | 2011-07-15 15:16:44 +0000 | [diff] [blame] | 1664 | } else if (!(*bits & EXTENT_DO_ACCOUNTING)) { |
| 1665 | spin_lock(&BTRFS_I(inode)->lock); |
| 1666 | BTRFS_I(inode)->outstanding_extents--; |
| 1667 | spin_unlock(&BTRFS_I(inode)->lock); |
| 1668 | } |
Yan, Zheng | 0ca1f7c | 2010-05-16 10:48:47 -0400 | [diff] [blame] | 1669 | |
Josef Bacik | b6d08f0 | 2013-09-27 14:57:43 -0400 | [diff] [blame] | 1670 | /* |
| 1671 | * We don't reserve metadata space for space cache inodes so we |
| 1672 | * don't need to call dellalloc_release_metadata if there is an |
| 1673 | * error. |
| 1674 | */ |
| 1675 | if (*bits & EXTENT_DO_ACCOUNTING && |
| 1676 | root != root->fs_info->tree_root) |
Yan, Zheng | 0ca1f7c | 2010-05-16 10:48:47 -0400 | [diff] [blame] | 1677 | btrfs_delalloc_release_metadata(inode, len); |
| 1678 | |
Josef Bacik | 0cb59c9 | 2010-07-02 12:14:14 -0400 | [diff] [blame] | 1679 | if (root->root_key.objectid != BTRFS_DATA_RELOC_TREE_OBJECTID |
Josef Bacik | 7ee9e44 | 2013-06-21 16:37:03 -0400 | [diff] [blame] | 1680 | && do_list && !(state->state & EXTENT_NORESERVE)) |
Yan, Zheng | 0ca1f7c | 2010-05-16 10:48:47 -0400 | [diff] [blame] | 1681 | btrfs_free_reserved_data_space(inode, len); |
Josef Bacik | 9ed74f2 | 2009-09-11 16:12:44 -0400 | [diff] [blame] | 1682 | |
Miao Xie | 963d678 | 2013-01-29 10:10:51 +0000 | [diff] [blame] | 1683 | __percpu_counter_add(&root->fs_info->delalloc_bytes, -len, |
| 1684 | root->fs_info->delalloc_batch); |
Miao Xie | df0af1a | 2013-01-29 10:11:59 +0000 | [diff] [blame] | 1685 | spin_lock(&BTRFS_I(inode)->lock); |
Yan, Zheng | 0ca1f7c | 2010-05-16 10:48:47 -0400 | [diff] [blame] | 1686 | BTRFS_I(inode)->delalloc_bytes -= len; |
Josef Bacik | 0cb59c9 | 2010-07-02 12:14:14 -0400 | [diff] [blame] | 1687 | if (do_list && BTRFS_I(inode)->delalloc_bytes == 0 && |
Miao Xie | df0af1a | 2013-01-29 10:11:59 +0000 | [diff] [blame] | 1688 | test_bit(BTRFS_INODE_IN_DELALLOC_LIST, |
Miao Xie | eb73c1b | 2013-05-15 07:48:22 +0000 | [diff] [blame] | 1689 | &BTRFS_I(inode)->runtime_flags)) |
| 1690 | btrfs_del_delalloc_inode(root, inode); |
Miao Xie | df0af1a | 2013-01-29 10:11:59 +0000 | [diff] [blame] | 1691 | spin_unlock(&BTRFS_I(inode)->lock); |
Chris Mason | 291d673 | 2008-01-29 15:55:23 -0500 | [diff] [blame] | 1692 | } |
Chris Mason | 291d673 | 2008-01-29 15:55:23 -0500 | [diff] [blame] | 1693 | } |
| 1694 | |
Chris Mason | d352ac6 | 2008-09-29 15:18:18 -0400 | [diff] [blame] | 1695 | /* |
| 1696 | * extent_io.c merge_bio_hook, this must check the chunk tree to make sure |
| 1697 | * we don't create bios that span stripes or chunks |
| 1698 | */ |
David Woodhouse | 64a1670 | 2009-07-15 23:29:37 +0100 | [diff] [blame] | 1699 | 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] | 1700 | size_t size, struct bio *bio, |
| 1701 | unsigned long bio_flags) |
Chris Mason | 239b14b | 2008-03-24 15:02:07 -0400 | [diff] [blame] | 1702 | { |
| 1703 | struct btrfs_root *root = BTRFS_I(page->mapping->host)->root; |
Kent Overstreet | 4f024f3 | 2013-10-11 15:44:27 -0700 | [diff] [blame] | 1704 | u64 logical = (u64)bio->bi_iter.bi_sector << 9; |
Chris Mason | 239b14b | 2008-03-24 15:02:07 -0400 | [diff] [blame] | 1705 | u64 length = 0; |
| 1706 | u64 map_length; |
Chris Mason | 239b14b | 2008-03-24 15:02:07 -0400 | [diff] [blame] | 1707 | int ret; |
| 1708 | |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 1709 | if (bio_flags & EXTENT_BIO_COMPRESSED) |
| 1710 | return 0; |
| 1711 | |
Kent Overstreet | 4f024f3 | 2013-10-11 15:44:27 -0700 | [diff] [blame] | 1712 | length = bio->bi_iter.bi_size; |
Chris Mason | 239b14b | 2008-03-24 15:02:07 -0400 | [diff] [blame] | 1713 | map_length = length; |
David Woodhouse | 64a1670 | 2009-07-15 23:29:37 +0100 | [diff] [blame] | 1714 | ret = btrfs_map_block(root->fs_info, rw, logical, |
Chris Mason | f188591 | 2008-04-09 16:28:12 -0400 | [diff] [blame] | 1715 | &map_length, NULL, 0); |
Stefan Behrens | 3ec706c | 2012-11-05 15:46:42 +0100 | [diff] [blame] | 1716 | /* Will always return 0 with map_multi == NULL */ |
Jeff Mahoney | 3444a97 | 2011-10-03 23:23:13 -0400 | [diff] [blame] | 1717 | BUG_ON(ret < 0); |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 1718 | if (map_length < length + size) |
Chris Mason | 239b14b | 2008-03-24 15:02:07 -0400 | [diff] [blame] | 1719 | return 1; |
Jeff Mahoney | 3444a97 | 2011-10-03 23:23:13 -0400 | [diff] [blame] | 1720 | return 0; |
Chris Mason | 239b14b | 2008-03-24 15:02:07 -0400 | [diff] [blame] | 1721 | } |
| 1722 | |
Chris Mason | d352ac6 | 2008-09-29 15:18:18 -0400 | [diff] [blame] | 1723 | /* |
| 1724 | * in order to insert checksums into the metadata in large chunks, |
| 1725 | * we wait until bio submission time. All the pages in the bio are |
| 1726 | * checksummed and sums are attached onto the ordered extent record. |
| 1727 | * |
| 1728 | * At IO completion time the cums attached on the ordered extent record |
| 1729 | * are inserted into the btree |
| 1730 | */ |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 1731 | static int __btrfs_submit_bio_start(struct inode *inode, int rw, |
| 1732 | struct bio *bio, int mirror_num, |
Chris Mason | eaf25d9 | 2010-05-25 09:48:28 -0400 | [diff] [blame] | 1733 | unsigned long bio_flags, |
| 1734 | u64 bio_offset) |
Chris Mason | 065631f | 2008-02-20 12:07:25 -0500 | [diff] [blame] | 1735 | { |
Chris Mason | 065631f | 2008-02-20 12:07:25 -0500 | [diff] [blame] | 1736 | struct btrfs_root *root = BTRFS_I(inode)->root; |
Chris Mason | 065631f | 2008-02-20 12:07:25 -0500 | [diff] [blame] | 1737 | int ret = 0; |
Chris Mason | e015640 | 2008-04-16 11:15:20 -0400 | [diff] [blame] | 1738 | |
Chris Mason | d20f704 | 2008-12-08 16:58:54 -0500 | [diff] [blame] | 1739 | ret = btrfs_csum_one_bio(root, inode, bio, 0, 0); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 1740 | BUG_ON(ret); /* -ENOMEM */ |
Chris Mason | 4a69a41 | 2008-11-06 22:03:00 -0500 | [diff] [blame] | 1741 | return 0; |
| 1742 | } |
Chris Mason | e015640 | 2008-04-16 11:15:20 -0400 | [diff] [blame] | 1743 | |
Chris Mason | 4a69a41 | 2008-11-06 22:03:00 -0500 | [diff] [blame] | 1744 | /* |
| 1745 | * in order to insert checksums into the metadata in large chunks, |
| 1746 | * we wait until bio submission time. All the pages in the bio are |
| 1747 | * checksummed and sums are attached onto the ordered extent record. |
| 1748 | * |
| 1749 | * At IO completion time the cums attached on the ordered extent record |
| 1750 | * are inserted into the btree |
| 1751 | */ |
Christoph Hellwig | b295086 | 2008-12-02 09:54:17 -0500 | [diff] [blame] | 1752 | 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] | 1753 | int mirror_num, unsigned long bio_flags, |
| 1754 | u64 bio_offset) |
Chris Mason | 4a69a41 | 2008-11-06 22:03:00 -0500 | [diff] [blame] | 1755 | { |
| 1756 | struct btrfs_root *root = BTRFS_I(inode)->root; |
Stefan Behrens | 6189192 | 2012-11-05 18:51:52 +0100 | [diff] [blame] | 1757 | int ret; |
| 1758 | |
| 1759 | ret = btrfs_map_bio(root, rw, bio, mirror_num, 1); |
| 1760 | if (ret) |
| 1761 | bio_endio(bio, ret); |
| 1762 | return ret; |
Chris Mason | 44b8bd7 | 2008-04-16 11:14:51 -0400 | [diff] [blame] | 1763 | } |
| 1764 | |
Chris Mason | d352ac6 | 2008-09-29 15:18:18 -0400 | [diff] [blame] | 1765 | /* |
Chris Mason | cad321a | 2008-12-17 14:51:42 -0500 | [diff] [blame] | 1766 | * extent_io.c submission hook. This does the right thing for csum calculation |
| 1767 | * on write, or reading the csums from the tree before a read |
Chris Mason | d352ac6 | 2008-09-29 15:18:18 -0400 | [diff] [blame] | 1768 | */ |
Christoph Hellwig | b295086 | 2008-12-02 09:54:17 -0500 | [diff] [blame] | 1769 | 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] | 1770 | int mirror_num, unsigned long bio_flags, |
| 1771 | u64 bio_offset) |
Chris Mason | 44b8bd7 | 2008-04-16 11:14:51 -0400 | [diff] [blame] | 1772 | { |
| 1773 | struct btrfs_root *root = BTRFS_I(inode)->root; |
| 1774 | int ret = 0; |
Chris Mason | 19b9bdb | 2008-10-30 14:23:13 -0400 | [diff] [blame] | 1775 | int skip_sum; |
Jeff Mahoney | 0417341 | 2011-10-03 23:23:12 -0400 | [diff] [blame] | 1776 | int metadata = 0; |
Josef Bacik | b812ce2 | 2012-11-16 13:56:32 -0500 | [diff] [blame] | 1777 | int async = !atomic_read(&BTRFS_I(inode)->sync_writers); |
Chris Mason | 44b8bd7 | 2008-04-16 11:14:51 -0400 | [diff] [blame] | 1778 | |
Christoph Hellwig | 6cbff00 | 2009-04-17 10:37:41 +0200 | [diff] [blame] | 1779 | skip_sum = BTRFS_I(inode)->flags & BTRFS_INODE_NODATASUM; |
Chris Mason | cad321a | 2008-12-17 14:51:42 -0500 | [diff] [blame] | 1780 | |
Liu Bo | 83eea1f | 2012-07-10 05:28:39 -0600 | [diff] [blame] | 1781 | if (btrfs_is_free_space_inode(inode)) |
Jeff Mahoney | 0417341 | 2011-10-03 23:23:12 -0400 | [diff] [blame] | 1782 | metadata = 2; |
| 1783 | |
Christoph Hellwig | 7b6d91d | 2010-08-07 18:20:39 +0200 | [diff] [blame] | 1784 | if (!(rw & REQ_WRITE)) { |
Josef Bacik | 5fd0204 | 2012-05-02 14:00:54 -0400 | [diff] [blame] | 1785 | ret = btrfs_bio_wq_end_io(root->fs_info, bio, metadata); |
| 1786 | if (ret) |
Stefan Behrens | 6189192 | 2012-11-05 18:51:52 +0100 | [diff] [blame] | 1787 | goto out; |
Josef Bacik | 5fd0204 | 2012-05-02 14:00:54 -0400 | [diff] [blame] | 1788 | |
Chris Mason | d20f704 | 2008-12-08 16:58:54 -0500 | [diff] [blame] | 1789 | if (bio_flags & EXTENT_BIO_COMPRESSED) { |
Stefan Behrens | 6189192 | 2012-11-05 18:51:52 +0100 | [diff] [blame] | 1790 | ret = btrfs_submit_compressed_read(inode, bio, |
| 1791 | mirror_num, |
| 1792 | bio_flags); |
| 1793 | goto out; |
Tsutomu Itoh | c2db107 | 2011-03-01 06:48:31 +0000 | [diff] [blame] | 1794 | } else if (!skip_sum) { |
| 1795 | ret = btrfs_lookup_bio_sums(root, inode, bio, NULL); |
| 1796 | if (ret) |
Stefan Behrens | 6189192 | 2012-11-05 18:51:52 +0100 | [diff] [blame] | 1797 | goto out; |
Tsutomu Itoh | c2db107 | 2011-03-01 06:48:31 +0000 | [diff] [blame] | 1798 | } |
Chris Mason | 4d1b5fb | 2008-08-20 09:44:52 -0400 | [diff] [blame] | 1799 | goto mapit; |
Josef Bacik | b812ce2 | 2012-11-16 13:56:32 -0500 | [diff] [blame] | 1800 | } else if (async && !skip_sum) { |
Yan Zheng | 17d217f | 2008-12-12 10:03:38 -0500 | [diff] [blame] | 1801 | /* csum items have already been cloned */ |
| 1802 | if (root->root_key.objectid == BTRFS_DATA_RELOC_TREE_OBJECTID) |
| 1803 | goto mapit; |
Chris Mason | 19b9bdb | 2008-10-30 14:23:13 -0400 | [diff] [blame] | 1804 | /* we're doing a write, do the async checksumming */ |
Stefan Behrens | 6189192 | 2012-11-05 18:51:52 +0100 | [diff] [blame] | 1805 | ret = btrfs_wq_submit_bio(BTRFS_I(inode)->root->fs_info, |
Chris Mason | 44b8bd7 | 2008-04-16 11:14:51 -0400 | [diff] [blame] | 1806 | inode, rw, bio, mirror_num, |
Chris Mason | eaf25d9 | 2010-05-25 09:48:28 -0400 | [diff] [blame] | 1807 | bio_flags, bio_offset, |
| 1808 | __btrfs_submit_bio_start, |
Chris Mason | 4a69a41 | 2008-11-06 22:03:00 -0500 | [diff] [blame] | 1809 | __btrfs_submit_bio_done); |
Stefan Behrens | 6189192 | 2012-11-05 18:51:52 +0100 | [diff] [blame] | 1810 | goto out; |
Josef Bacik | b812ce2 | 2012-11-16 13:56:32 -0500 | [diff] [blame] | 1811 | } else if (!skip_sum) { |
| 1812 | ret = btrfs_csum_one_bio(root, inode, bio, 0, 0); |
| 1813 | if (ret) |
| 1814 | goto out; |
Chris Mason | 19b9bdb | 2008-10-30 14:23:13 -0400 | [diff] [blame] | 1815 | } |
| 1816 | |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 1817 | mapit: |
Stefan Behrens | 6189192 | 2012-11-05 18:51:52 +0100 | [diff] [blame] | 1818 | ret = btrfs_map_bio(root, rw, bio, mirror_num, 0); |
| 1819 | |
| 1820 | out: |
| 1821 | if (ret < 0) |
| 1822 | bio_endio(bio, ret); |
| 1823 | return ret; |
Chris Mason | 065631f | 2008-02-20 12:07:25 -0500 | [diff] [blame] | 1824 | } |
Chris Mason | 6885f30 | 2008-02-20 16:11:05 -0500 | [diff] [blame] | 1825 | |
Chris Mason | d352ac6 | 2008-09-29 15:18:18 -0400 | [diff] [blame] | 1826 | /* |
| 1827 | * given a list of ordered sums record them in the inode. This happens |
| 1828 | * at IO completion time based on sums calculated at bio submission time. |
| 1829 | */ |
Chris Mason | ba1da2f | 2008-07-17 12:54:15 -0400 | [diff] [blame] | 1830 | static noinline int add_pending_csums(struct btrfs_trans_handle *trans, |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 1831 | struct inode *inode, u64 file_offset, |
| 1832 | struct list_head *list) |
| 1833 | { |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 1834 | struct btrfs_ordered_sum *sum; |
| 1835 | |
Qinghuang Feng | c6e3087 | 2009-01-21 10:59:08 -0500 | [diff] [blame] | 1836 | list_for_each_entry(sum, list, list) { |
Miao Xie | 39847c4 | 2013-03-28 08:08:20 +0000 | [diff] [blame] | 1837 | trans->adding_csums = 1; |
Chris Mason | d20f704 | 2008-12-08 16:58:54 -0500 | [diff] [blame] | 1838 | btrfs_csum_file_blocks(trans, |
| 1839 | BTRFS_I(inode)->root->fs_info->csum_root, sum); |
Miao Xie | 39847c4 | 2013-03-28 08:08:20 +0000 | [diff] [blame] | 1840 | trans->adding_csums = 0; |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 1841 | } |
| 1842 | return 0; |
| 1843 | } |
| 1844 | |
Josef Bacik | 2ac55d4 | 2010-02-03 19:33:23 +0000 | [diff] [blame] | 1845 | int btrfs_set_extent_delalloc(struct inode *inode, u64 start, u64 end, |
| 1846 | struct extent_state **cached_state) |
Chris Mason | ea8c281 | 2008-08-04 23:17:27 -0400 | [diff] [blame] | 1847 | { |
Julia Lawall | 6c1500f | 2012-11-03 20:30:18 +0000 | [diff] [blame] | 1848 | WARN_ON((end & (PAGE_CACHE_SIZE - 1)) == 0); |
Chris Mason | ea8c281 | 2008-08-04 23:17:27 -0400 | [diff] [blame] | 1849 | return set_extent_delalloc(&BTRFS_I(inode)->io_tree, start, end, |
Josef Bacik | 2ac55d4 | 2010-02-03 19:33:23 +0000 | [diff] [blame] | 1850 | cached_state, GFP_NOFS); |
Chris Mason | ea8c281 | 2008-08-04 23:17:27 -0400 | [diff] [blame] | 1851 | } |
| 1852 | |
Chris Mason | d352ac6 | 2008-09-29 15:18:18 -0400 | [diff] [blame] | 1853 | /* see btrfs_writepage_start_hook for details on why this is required */ |
Chris Mason | 247e743 | 2008-07-17 12:53:51 -0400 | [diff] [blame] | 1854 | struct btrfs_writepage_fixup { |
| 1855 | struct page *page; |
| 1856 | struct btrfs_work work; |
| 1857 | }; |
| 1858 | |
Christoph Hellwig | b295086 | 2008-12-02 09:54:17 -0500 | [diff] [blame] | 1859 | static void btrfs_writepage_fixup_worker(struct btrfs_work *work) |
Chris Mason | 247e743 | 2008-07-17 12:53:51 -0400 | [diff] [blame] | 1860 | { |
| 1861 | struct btrfs_writepage_fixup *fixup; |
| 1862 | struct btrfs_ordered_extent *ordered; |
Josef Bacik | 2ac55d4 | 2010-02-03 19:33:23 +0000 | [diff] [blame] | 1863 | struct extent_state *cached_state = NULL; |
Chris Mason | 247e743 | 2008-07-17 12:53:51 -0400 | [diff] [blame] | 1864 | struct page *page; |
| 1865 | struct inode *inode; |
| 1866 | u64 page_start; |
| 1867 | u64 page_end; |
Jeff Mahoney | 87826df | 2012-02-15 16:23:57 +0100 | [diff] [blame] | 1868 | int ret; |
Chris Mason | 247e743 | 2008-07-17 12:53:51 -0400 | [diff] [blame] | 1869 | |
| 1870 | fixup = container_of(work, struct btrfs_writepage_fixup, work); |
| 1871 | page = fixup->page; |
Chris Mason | 4a09675 | 2008-07-21 10:29:44 -0400 | [diff] [blame] | 1872 | again: |
Chris Mason | 247e743 | 2008-07-17 12:53:51 -0400 | [diff] [blame] | 1873 | lock_page(page); |
| 1874 | if (!page->mapping || !PageDirty(page) || !PageChecked(page)) { |
| 1875 | ClearPageChecked(page); |
| 1876 | goto out_page; |
| 1877 | } |
| 1878 | |
| 1879 | inode = page->mapping->host; |
| 1880 | page_start = page_offset(page); |
| 1881 | page_end = page_offset(page) + PAGE_CACHE_SIZE - 1; |
| 1882 | |
Josef Bacik | 2ac55d4 | 2010-02-03 19:33:23 +0000 | [diff] [blame] | 1883 | 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] | 1884 | &cached_state); |
Chris Mason | 4a09675 | 2008-07-21 10:29:44 -0400 | [diff] [blame] | 1885 | |
| 1886 | /* already ordered? We're done */ |
Chris Mason | 8b62b72 | 2009-09-02 16:53:46 -0400 | [diff] [blame] | 1887 | if (PagePrivate2(page)) |
Chris Mason | 247e743 | 2008-07-17 12:53:51 -0400 | [diff] [blame] | 1888 | goto out; |
Chris Mason | 4a09675 | 2008-07-21 10:29:44 -0400 | [diff] [blame] | 1889 | |
| 1890 | ordered = btrfs_lookup_ordered_extent(inode, page_start); |
| 1891 | if (ordered) { |
Josef Bacik | 2ac55d4 | 2010-02-03 19:33:23 +0000 | [diff] [blame] | 1892 | unlock_extent_cached(&BTRFS_I(inode)->io_tree, page_start, |
| 1893 | page_end, &cached_state, GFP_NOFS); |
Chris Mason | 4a09675 | 2008-07-21 10:29:44 -0400 | [diff] [blame] | 1894 | unlock_page(page); |
| 1895 | btrfs_start_ordered_extent(inode, ordered, 1); |
Jeff Mahoney | 87826df | 2012-02-15 16:23:57 +0100 | [diff] [blame] | 1896 | btrfs_put_ordered_extent(ordered); |
Chris Mason | 4a09675 | 2008-07-21 10:29:44 -0400 | [diff] [blame] | 1897 | goto again; |
| 1898 | } |
Chris Mason | 247e743 | 2008-07-17 12:53:51 -0400 | [diff] [blame] | 1899 | |
Jeff Mahoney | 87826df | 2012-02-15 16:23:57 +0100 | [diff] [blame] | 1900 | ret = btrfs_delalloc_reserve_space(inode, PAGE_CACHE_SIZE); |
| 1901 | if (ret) { |
| 1902 | mapping_set_error(page->mapping, ret); |
| 1903 | end_extent_writepage(page, ret, page_start, page_end); |
| 1904 | ClearPageChecked(page); |
| 1905 | goto out; |
| 1906 | } |
| 1907 | |
Josef Bacik | 2ac55d4 | 2010-02-03 19:33:23 +0000 | [diff] [blame] | 1908 | btrfs_set_extent_delalloc(inode, page_start, page_end, &cached_state); |
Chris Mason | 247e743 | 2008-07-17 12:53:51 -0400 | [diff] [blame] | 1909 | ClearPageChecked(page); |
Jeff Mahoney | 87826df | 2012-02-15 16:23:57 +0100 | [diff] [blame] | 1910 | set_page_dirty(page); |
Chris Mason | 247e743 | 2008-07-17 12:53:51 -0400 | [diff] [blame] | 1911 | out: |
Josef Bacik | 2ac55d4 | 2010-02-03 19:33:23 +0000 | [diff] [blame] | 1912 | unlock_extent_cached(&BTRFS_I(inode)->io_tree, page_start, page_end, |
| 1913 | &cached_state, GFP_NOFS); |
Chris Mason | 247e743 | 2008-07-17 12:53:51 -0400 | [diff] [blame] | 1914 | out_page: |
| 1915 | unlock_page(page); |
| 1916 | page_cache_release(page); |
Miao Xie | b897abe | 2011-01-26 16:19:22 +0800 | [diff] [blame] | 1917 | kfree(fixup); |
Chris Mason | 247e743 | 2008-07-17 12:53:51 -0400 | [diff] [blame] | 1918 | } |
| 1919 | |
| 1920 | /* |
| 1921 | * There are a few paths in the higher layers of the kernel that directly |
| 1922 | * set the page dirty bit without asking the filesystem if it is a |
| 1923 | * good idea. This causes problems because we want to make sure COW |
| 1924 | * properly happens and the data=ordered rules are followed. |
| 1925 | * |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 1926 | * 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] | 1927 | * hasn't been properly setup for IO. We kick off an async process |
| 1928 | * to fix it up. The async helper will wait for ordered extents, set |
| 1929 | * the delalloc bit and make it safe to write the page. |
| 1930 | */ |
Christoph Hellwig | b295086 | 2008-12-02 09:54:17 -0500 | [diff] [blame] | 1931 | 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] | 1932 | { |
| 1933 | struct inode *inode = page->mapping->host; |
| 1934 | struct btrfs_writepage_fixup *fixup; |
| 1935 | struct btrfs_root *root = BTRFS_I(inode)->root; |
Chris Mason | 247e743 | 2008-07-17 12:53:51 -0400 | [diff] [blame] | 1936 | |
Chris Mason | 8b62b72 | 2009-09-02 16:53:46 -0400 | [diff] [blame] | 1937 | /* this page is properly in the ordered list */ |
| 1938 | if (TestClearPagePrivate2(page)) |
Chris Mason | 247e743 | 2008-07-17 12:53:51 -0400 | [diff] [blame] | 1939 | return 0; |
| 1940 | |
| 1941 | if (PageChecked(page)) |
| 1942 | return -EAGAIN; |
| 1943 | |
| 1944 | fixup = kzalloc(sizeof(*fixup), GFP_NOFS); |
| 1945 | if (!fixup) |
| 1946 | return -EAGAIN; |
Chris Mason | f421950 | 2008-07-22 11:18:09 -0400 | [diff] [blame] | 1947 | |
Chris Mason | 247e743 | 2008-07-17 12:53:51 -0400 | [diff] [blame] | 1948 | SetPageChecked(page); |
| 1949 | page_cache_get(page); |
Liu Bo | 9e0af23 | 2014-08-15 23:36:53 +0800 | [diff] [blame] | 1950 | btrfs_init_work(&fixup->work, btrfs_fixup_helper, |
| 1951 | btrfs_writepage_fixup_worker, NULL, NULL); |
Chris Mason | 247e743 | 2008-07-17 12:53:51 -0400 | [diff] [blame] | 1952 | fixup->page = page; |
Qu Wenruo | dc6e320 | 2014-02-28 10:46:14 +0800 | [diff] [blame] | 1953 | btrfs_queue_work(root->fs_info->fixup_workers, &fixup->work); |
Jeff Mahoney | 87826df | 2012-02-15 16:23:57 +0100 | [diff] [blame] | 1954 | return -EBUSY; |
Chris Mason | 247e743 | 2008-07-17 12:53:51 -0400 | [diff] [blame] | 1955 | } |
| 1956 | |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 1957 | static int insert_reserved_file_extent(struct btrfs_trans_handle *trans, |
| 1958 | struct inode *inode, u64 file_pos, |
| 1959 | u64 disk_bytenr, u64 disk_num_bytes, |
| 1960 | u64 num_bytes, u64 ram_bytes, |
| 1961 | u8 compression, u8 encryption, |
| 1962 | u16 other_encoding, int extent_type) |
| 1963 | { |
| 1964 | struct btrfs_root *root = BTRFS_I(inode)->root; |
| 1965 | struct btrfs_file_extent_item *fi; |
| 1966 | struct btrfs_path *path; |
| 1967 | struct extent_buffer *leaf; |
| 1968 | struct btrfs_key ins; |
Filipe David Borba Manana | 1acae57 | 2014-01-07 11:42:27 +0000 | [diff] [blame] | 1969 | int extent_inserted = 0; |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 1970 | int ret; |
| 1971 | |
| 1972 | path = btrfs_alloc_path(); |
Mark Fasheh | d8926bb | 2011-07-13 10:38:47 -0700 | [diff] [blame] | 1973 | if (!path) |
| 1974 | return -ENOMEM; |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 1975 | |
Chris Mason | a1ed835 | 2009-09-11 12:27:37 -0400 | [diff] [blame] | 1976 | /* |
| 1977 | * we may be replacing one extent in the tree with another. |
| 1978 | * The new extent is pinned in the extent map, and we don't want |
| 1979 | * to drop it from the cache until it is completely in the btree. |
| 1980 | * |
| 1981 | * So, tell btrfs_drop_extents to leave this extent in the cache. |
| 1982 | * the caller is expected to unpin it and allow it to be merged |
| 1983 | * with the others. |
| 1984 | */ |
Filipe David Borba Manana | 1acae57 | 2014-01-07 11:42:27 +0000 | [diff] [blame] | 1985 | ret = __btrfs_drop_extents(trans, root, inode, path, file_pos, |
| 1986 | file_pos + num_bytes, NULL, 0, |
| 1987 | 1, sizeof(*fi), &extent_inserted); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 1988 | if (ret) |
| 1989 | goto out; |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 1990 | |
Filipe David Borba Manana | 1acae57 | 2014-01-07 11:42:27 +0000 | [diff] [blame] | 1991 | if (!extent_inserted) { |
| 1992 | ins.objectid = btrfs_ino(inode); |
| 1993 | ins.offset = file_pos; |
| 1994 | ins.type = BTRFS_EXTENT_DATA_KEY; |
| 1995 | |
| 1996 | path->leave_spinning = 1; |
| 1997 | ret = btrfs_insert_empty_item(trans, root, path, &ins, |
| 1998 | sizeof(*fi)); |
| 1999 | if (ret) |
| 2000 | goto out; |
| 2001 | } |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 2002 | leaf = path->nodes[0]; |
| 2003 | fi = btrfs_item_ptr(leaf, path->slots[0], |
| 2004 | struct btrfs_file_extent_item); |
| 2005 | btrfs_set_file_extent_generation(leaf, fi, trans->transid); |
| 2006 | btrfs_set_file_extent_type(leaf, fi, extent_type); |
| 2007 | btrfs_set_file_extent_disk_bytenr(leaf, fi, disk_bytenr); |
| 2008 | btrfs_set_file_extent_disk_num_bytes(leaf, fi, disk_num_bytes); |
| 2009 | btrfs_set_file_extent_offset(leaf, fi, 0); |
| 2010 | btrfs_set_file_extent_num_bytes(leaf, fi, num_bytes); |
| 2011 | btrfs_set_file_extent_ram_bytes(leaf, fi, ram_bytes); |
| 2012 | btrfs_set_file_extent_compression(leaf, fi, compression); |
| 2013 | btrfs_set_file_extent_encryption(leaf, fi, encryption); |
| 2014 | btrfs_set_file_extent_other_encoding(leaf, fi, other_encoding); |
Chris Mason | b947343 | 2009-03-13 11:00:37 -0400 | [diff] [blame] | 2015 | |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 2016 | btrfs_mark_buffer_dirty(leaf); |
Josef Bacik | ce19533 | 2012-09-25 15:26:16 -0400 | [diff] [blame] | 2017 | btrfs_release_path(path); |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 2018 | |
| 2019 | inode_add_bytes(inode, num_bytes); |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 2020 | |
| 2021 | ins.objectid = disk_bytenr; |
| 2022 | ins.offset = disk_num_bytes; |
| 2023 | ins.type = BTRFS_EXTENT_ITEM_KEY; |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 2024 | ret = btrfs_alloc_reserved_file_extent(trans, root, |
| 2025 | root->root_key.objectid, |
Li Zefan | 33345d01 | 2011-04-20 10:31:50 +0800 | [diff] [blame] | 2026 | btrfs_ino(inode), file_pos, &ins); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 2027 | out: |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 2028 | btrfs_free_path(path); |
Chris Mason | b947343 | 2009-03-13 11:00:37 -0400 | [diff] [blame] | 2029 | |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 2030 | return ret; |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 2031 | } |
| 2032 | |
Liu Bo | 38c227d | 2013-01-29 03:18:40 +0000 | [diff] [blame] | 2033 | /* snapshot-aware defrag */ |
| 2034 | struct sa_defrag_extent_backref { |
| 2035 | struct rb_node node; |
| 2036 | struct old_sa_defrag_extent *old; |
| 2037 | u64 root_id; |
| 2038 | u64 inum; |
| 2039 | u64 file_pos; |
| 2040 | u64 extent_offset; |
| 2041 | u64 num_bytes; |
| 2042 | u64 generation; |
| 2043 | }; |
| 2044 | |
| 2045 | struct old_sa_defrag_extent { |
| 2046 | struct list_head list; |
| 2047 | struct new_sa_defrag_extent *new; |
| 2048 | |
| 2049 | u64 extent_offset; |
| 2050 | u64 bytenr; |
| 2051 | u64 offset; |
| 2052 | u64 len; |
| 2053 | int count; |
| 2054 | }; |
| 2055 | |
| 2056 | struct new_sa_defrag_extent { |
| 2057 | struct rb_root root; |
| 2058 | struct list_head head; |
| 2059 | struct btrfs_path *path; |
| 2060 | struct inode *inode; |
| 2061 | u64 file_pos; |
| 2062 | u64 len; |
| 2063 | u64 bytenr; |
| 2064 | u64 disk_len; |
| 2065 | u8 compress_type; |
| 2066 | }; |
| 2067 | |
| 2068 | static int backref_comp(struct sa_defrag_extent_backref *b1, |
| 2069 | struct sa_defrag_extent_backref *b2) |
| 2070 | { |
| 2071 | if (b1->root_id < b2->root_id) |
| 2072 | return -1; |
| 2073 | else if (b1->root_id > b2->root_id) |
| 2074 | return 1; |
| 2075 | |
| 2076 | if (b1->inum < b2->inum) |
| 2077 | return -1; |
| 2078 | else if (b1->inum > b2->inum) |
| 2079 | return 1; |
| 2080 | |
| 2081 | if (b1->file_pos < b2->file_pos) |
| 2082 | return -1; |
| 2083 | else if (b1->file_pos > b2->file_pos) |
| 2084 | return 1; |
| 2085 | |
| 2086 | /* |
| 2087 | * [------------------------------] ===> (a range of space) |
| 2088 | * |<--->| |<---->| =============> (fs/file tree A) |
| 2089 | * |<---------------------------->| ===> (fs/file tree B) |
| 2090 | * |
| 2091 | * A range of space can refer to two file extents in one tree while |
| 2092 | * refer to only one file extent in another tree. |
| 2093 | * |
| 2094 | * So we may process a disk offset more than one time(two extents in A) |
| 2095 | * and locate at the same extent(one extent in B), then insert two same |
| 2096 | * backrefs(both refer to the extent in B). |
| 2097 | */ |
| 2098 | return 0; |
| 2099 | } |
| 2100 | |
| 2101 | static void backref_insert(struct rb_root *root, |
| 2102 | struct sa_defrag_extent_backref *backref) |
| 2103 | { |
| 2104 | struct rb_node **p = &root->rb_node; |
| 2105 | struct rb_node *parent = NULL; |
| 2106 | struct sa_defrag_extent_backref *entry; |
| 2107 | int ret; |
| 2108 | |
| 2109 | while (*p) { |
| 2110 | parent = *p; |
| 2111 | entry = rb_entry(parent, struct sa_defrag_extent_backref, node); |
| 2112 | |
| 2113 | ret = backref_comp(backref, entry); |
| 2114 | if (ret < 0) |
| 2115 | p = &(*p)->rb_left; |
| 2116 | else |
| 2117 | p = &(*p)->rb_right; |
| 2118 | } |
| 2119 | |
| 2120 | rb_link_node(&backref->node, parent, p); |
| 2121 | rb_insert_color(&backref->node, root); |
| 2122 | } |
| 2123 | |
| 2124 | /* |
| 2125 | * Note the backref might has changed, and in this case we just return 0. |
| 2126 | */ |
| 2127 | static noinline int record_one_backref(u64 inum, u64 offset, u64 root_id, |
| 2128 | void *ctx) |
| 2129 | { |
| 2130 | struct btrfs_file_extent_item *extent; |
| 2131 | struct btrfs_fs_info *fs_info; |
| 2132 | struct old_sa_defrag_extent *old = ctx; |
| 2133 | struct new_sa_defrag_extent *new = old->new; |
| 2134 | struct btrfs_path *path = new->path; |
| 2135 | struct btrfs_key key; |
| 2136 | struct btrfs_root *root; |
| 2137 | struct sa_defrag_extent_backref *backref; |
| 2138 | struct extent_buffer *leaf; |
| 2139 | struct inode *inode = new->inode; |
| 2140 | int slot; |
| 2141 | int ret; |
| 2142 | u64 extent_offset; |
| 2143 | u64 num_bytes; |
| 2144 | |
| 2145 | if (BTRFS_I(inode)->root->root_key.objectid == root_id && |
| 2146 | inum == btrfs_ino(inode)) |
| 2147 | return 0; |
| 2148 | |
| 2149 | key.objectid = root_id; |
| 2150 | key.type = BTRFS_ROOT_ITEM_KEY; |
| 2151 | key.offset = (u64)-1; |
| 2152 | |
| 2153 | fs_info = BTRFS_I(inode)->root->fs_info; |
| 2154 | root = btrfs_read_fs_root_no_name(fs_info, &key); |
| 2155 | if (IS_ERR(root)) { |
| 2156 | if (PTR_ERR(root) == -ENOENT) |
| 2157 | return 0; |
| 2158 | WARN_ON(1); |
| 2159 | pr_debug("inum=%llu, offset=%llu, root_id=%llu\n", |
| 2160 | inum, offset, root_id); |
| 2161 | return PTR_ERR(root); |
| 2162 | } |
| 2163 | |
| 2164 | key.objectid = inum; |
| 2165 | key.type = BTRFS_EXTENT_DATA_KEY; |
| 2166 | if (offset > (u64)-1 << 32) |
| 2167 | key.offset = 0; |
| 2168 | else |
| 2169 | key.offset = offset; |
| 2170 | |
| 2171 | ret = btrfs_search_slot(NULL, root, &key, path, 0, 0); |
Dulshani Gunawardhana | fae7f21 | 2013-10-31 10:30:08 +0530 | [diff] [blame] | 2172 | if (WARN_ON(ret < 0)) |
Liu Bo | 38c227d | 2013-01-29 03:18:40 +0000 | [diff] [blame] | 2173 | return ret; |
Josef Bacik | 50f1319 | 2013-07-22 12:50:37 -0400 | [diff] [blame] | 2174 | ret = 0; |
Liu Bo | 38c227d | 2013-01-29 03:18:40 +0000 | [diff] [blame] | 2175 | |
| 2176 | while (1) { |
| 2177 | cond_resched(); |
| 2178 | |
| 2179 | leaf = path->nodes[0]; |
| 2180 | slot = path->slots[0]; |
| 2181 | |
| 2182 | if (slot >= btrfs_header_nritems(leaf)) { |
| 2183 | ret = btrfs_next_leaf(root, path); |
| 2184 | if (ret < 0) { |
| 2185 | goto out; |
| 2186 | } else if (ret > 0) { |
| 2187 | ret = 0; |
| 2188 | goto out; |
| 2189 | } |
| 2190 | continue; |
| 2191 | } |
| 2192 | |
| 2193 | path->slots[0]++; |
| 2194 | |
| 2195 | btrfs_item_key_to_cpu(leaf, &key, slot); |
| 2196 | |
| 2197 | if (key.objectid > inum) |
| 2198 | goto out; |
| 2199 | |
| 2200 | if (key.objectid < inum || key.type != BTRFS_EXTENT_DATA_KEY) |
| 2201 | continue; |
| 2202 | |
| 2203 | extent = btrfs_item_ptr(leaf, slot, |
| 2204 | struct btrfs_file_extent_item); |
| 2205 | |
| 2206 | if (btrfs_file_extent_disk_bytenr(leaf, extent) != old->bytenr) |
| 2207 | continue; |
| 2208 | |
Liu Bo | e68afa4 | 2013-07-01 22:13:26 +0800 | [diff] [blame] | 2209 | /* |
| 2210 | * 'offset' refers to the exact key.offset, |
| 2211 | * NOT the 'offset' field in btrfs_extent_data_ref, ie. |
| 2212 | * (key.offset - extent_offset). |
| 2213 | */ |
| 2214 | if (key.offset != offset) |
Liu Bo | 38c227d | 2013-01-29 03:18:40 +0000 | [diff] [blame] | 2215 | continue; |
| 2216 | |
Liu Bo | e68afa4 | 2013-07-01 22:13:26 +0800 | [diff] [blame] | 2217 | extent_offset = btrfs_file_extent_offset(leaf, extent); |
Liu Bo | 38c227d | 2013-01-29 03:18:40 +0000 | [diff] [blame] | 2218 | num_bytes = btrfs_file_extent_num_bytes(leaf, extent); |
Liu Bo | e68afa4 | 2013-07-01 22:13:26 +0800 | [diff] [blame] | 2219 | |
Liu Bo | 38c227d | 2013-01-29 03:18:40 +0000 | [diff] [blame] | 2220 | if (extent_offset >= old->extent_offset + old->offset + |
| 2221 | old->len || extent_offset + num_bytes <= |
| 2222 | old->extent_offset + old->offset) |
| 2223 | continue; |
Liu Bo | 38c227d | 2013-01-29 03:18:40 +0000 | [diff] [blame] | 2224 | break; |
| 2225 | } |
| 2226 | |
| 2227 | backref = kmalloc(sizeof(*backref), GFP_NOFS); |
| 2228 | if (!backref) { |
| 2229 | ret = -ENOENT; |
| 2230 | goto out; |
| 2231 | } |
| 2232 | |
| 2233 | backref->root_id = root_id; |
| 2234 | backref->inum = inum; |
Liu Bo | e68afa4 | 2013-07-01 22:13:26 +0800 | [diff] [blame] | 2235 | backref->file_pos = offset; |
Liu Bo | 38c227d | 2013-01-29 03:18:40 +0000 | [diff] [blame] | 2236 | backref->num_bytes = num_bytes; |
| 2237 | backref->extent_offset = extent_offset; |
| 2238 | backref->generation = btrfs_file_extent_generation(leaf, extent); |
| 2239 | backref->old = old; |
| 2240 | backref_insert(&new->root, backref); |
| 2241 | old->count++; |
| 2242 | out: |
| 2243 | btrfs_release_path(path); |
| 2244 | WARN_ON(ret); |
| 2245 | return ret; |
| 2246 | } |
| 2247 | |
| 2248 | static noinline bool record_extent_backrefs(struct btrfs_path *path, |
| 2249 | struct new_sa_defrag_extent *new) |
| 2250 | { |
| 2251 | struct btrfs_fs_info *fs_info = BTRFS_I(new->inode)->root->fs_info; |
| 2252 | struct old_sa_defrag_extent *old, *tmp; |
| 2253 | int ret; |
| 2254 | |
| 2255 | new->path = path; |
| 2256 | |
| 2257 | list_for_each_entry_safe(old, tmp, &new->head, list) { |
Liu Bo | e68afa4 | 2013-07-01 22:13:26 +0800 | [diff] [blame] | 2258 | ret = iterate_inodes_from_logical(old->bytenr + |
| 2259 | old->extent_offset, fs_info, |
Liu Bo | 38c227d | 2013-01-29 03:18:40 +0000 | [diff] [blame] | 2260 | path, record_one_backref, |
| 2261 | old); |
Josef Bacik | 4724b10 | 2013-11-05 11:11:40 -0500 | [diff] [blame] | 2262 | if (ret < 0 && ret != -ENOENT) |
| 2263 | return false; |
Liu Bo | 38c227d | 2013-01-29 03:18:40 +0000 | [diff] [blame] | 2264 | |
| 2265 | /* no backref to be processed for this extent */ |
| 2266 | if (!old->count) { |
| 2267 | list_del(&old->list); |
| 2268 | kfree(old); |
| 2269 | } |
| 2270 | } |
| 2271 | |
| 2272 | if (list_empty(&new->head)) |
| 2273 | return false; |
| 2274 | |
| 2275 | return true; |
| 2276 | } |
| 2277 | |
| 2278 | static int relink_is_mergable(struct extent_buffer *leaf, |
| 2279 | struct btrfs_file_extent_item *fi, |
Liu Bo | 116e002 | 2013-08-02 16:30:40 +0800 | [diff] [blame] | 2280 | struct new_sa_defrag_extent *new) |
Liu Bo | 38c227d | 2013-01-29 03:18:40 +0000 | [diff] [blame] | 2281 | { |
Liu Bo | 116e002 | 2013-08-02 16:30:40 +0800 | [diff] [blame] | 2282 | if (btrfs_file_extent_disk_bytenr(leaf, fi) != new->bytenr) |
Liu Bo | 38c227d | 2013-01-29 03:18:40 +0000 | [diff] [blame] | 2283 | return 0; |
| 2284 | |
| 2285 | if (btrfs_file_extent_type(leaf, fi) != BTRFS_FILE_EXTENT_REG) |
| 2286 | return 0; |
| 2287 | |
Liu Bo | 116e002 | 2013-08-02 16:30:40 +0800 | [diff] [blame] | 2288 | if (btrfs_file_extent_compression(leaf, fi) != new->compress_type) |
| 2289 | return 0; |
| 2290 | |
| 2291 | if (btrfs_file_extent_encryption(leaf, fi) || |
Liu Bo | 38c227d | 2013-01-29 03:18:40 +0000 | [diff] [blame] | 2292 | btrfs_file_extent_other_encoding(leaf, fi)) |
| 2293 | return 0; |
| 2294 | |
| 2295 | return 1; |
| 2296 | } |
| 2297 | |
| 2298 | /* |
| 2299 | * Note the backref might has changed, and in this case we just return 0. |
| 2300 | */ |
| 2301 | static noinline int relink_extent_backref(struct btrfs_path *path, |
| 2302 | struct sa_defrag_extent_backref *prev, |
| 2303 | struct sa_defrag_extent_backref *backref) |
| 2304 | { |
| 2305 | struct btrfs_file_extent_item *extent; |
| 2306 | struct btrfs_file_extent_item *item; |
| 2307 | struct btrfs_ordered_extent *ordered; |
| 2308 | struct btrfs_trans_handle *trans; |
| 2309 | struct btrfs_fs_info *fs_info; |
| 2310 | struct btrfs_root *root; |
| 2311 | struct btrfs_key key; |
| 2312 | struct extent_buffer *leaf; |
| 2313 | struct old_sa_defrag_extent *old = backref->old; |
| 2314 | struct new_sa_defrag_extent *new = old->new; |
| 2315 | struct inode *src_inode = new->inode; |
| 2316 | struct inode *inode; |
| 2317 | struct extent_state *cached = NULL; |
| 2318 | int ret = 0; |
| 2319 | u64 start; |
| 2320 | u64 len; |
| 2321 | u64 lock_start; |
| 2322 | u64 lock_end; |
| 2323 | bool merge = false; |
| 2324 | int index; |
| 2325 | |
| 2326 | if (prev && prev->root_id == backref->root_id && |
| 2327 | prev->inum == backref->inum && |
| 2328 | prev->file_pos + prev->num_bytes == backref->file_pos) |
| 2329 | merge = true; |
| 2330 | |
| 2331 | /* step 1: get root */ |
| 2332 | key.objectid = backref->root_id; |
| 2333 | key.type = BTRFS_ROOT_ITEM_KEY; |
| 2334 | key.offset = (u64)-1; |
| 2335 | |
| 2336 | fs_info = BTRFS_I(src_inode)->root->fs_info; |
| 2337 | index = srcu_read_lock(&fs_info->subvol_srcu); |
| 2338 | |
| 2339 | root = btrfs_read_fs_root_no_name(fs_info, &key); |
| 2340 | if (IS_ERR(root)) { |
| 2341 | srcu_read_unlock(&fs_info->subvol_srcu, index); |
| 2342 | if (PTR_ERR(root) == -ENOENT) |
| 2343 | return 0; |
| 2344 | return PTR_ERR(root); |
| 2345 | } |
Liu Bo | 38c227d | 2013-01-29 03:18:40 +0000 | [diff] [blame] | 2346 | |
Wang Shilong | bcbba5e | 2014-02-08 23:46:35 +0800 | [diff] [blame] | 2347 | if (btrfs_root_readonly(root)) { |
| 2348 | srcu_read_unlock(&fs_info->subvol_srcu, index); |
| 2349 | return 0; |
| 2350 | } |
| 2351 | |
Liu Bo | 38c227d | 2013-01-29 03:18:40 +0000 | [diff] [blame] | 2352 | /* step 2: get inode */ |
| 2353 | key.objectid = backref->inum; |
| 2354 | key.type = BTRFS_INODE_ITEM_KEY; |
| 2355 | key.offset = 0; |
| 2356 | |
| 2357 | inode = btrfs_iget(fs_info->sb, &key, root, NULL); |
| 2358 | if (IS_ERR(inode)) { |
| 2359 | srcu_read_unlock(&fs_info->subvol_srcu, index); |
| 2360 | return 0; |
| 2361 | } |
| 2362 | |
| 2363 | srcu_read_unlock(&fs_info->subvol_srcu, index); |
| 2364 | |
| 2365 | /* step 3: relink backref */ |
| 2366 | lock_start = backref->file_pos; |
| 2367 | lock_end = backref->file_pos + backref->num_bytes - 1; |
| 2368 | lock_extent_bits(&BTRFS_I(inode)->io_tree, lock_start, lock_end, |
| 2369 | 0, &cached); |
| 2370 | |
| 2371 | ordered = btrfs_lookup_first_ordered_extent(inode, lock_end); |
| 2372 | if (ordered) { |
| 2373 | btrfs_put_ordered_extent(ordered); |
| 2374 | goto out_unlock; |
| 2375 | } |
| 2376 | |
| 2377 | trans = btrfs_join_transaction(root); |
| 2378 | if (IS_ERR(trans)) { |
| 2379 | ret = PTR_ERR(trans); |
| 2380 | goto out_unlock; |
| 2381 | } |
| 2382 | |
| 2383 | key.objectid = backref->inum; |
| 2384 | key.type = BTRFS_EXTENT_DATA_KEY; |
| 2385 | key.offset = backref->file_pos; |
| 2386 | |
| 2387 | ret = btrfs_search_slot(NULL, root, &key, path, 0, 0); |
| 2388 | if (ret < 0) { |
| 2389 | goto out_free_path; |
| 2390 | } else if (ret > 0) { |
| 2391 | ret = 0; |
| 2392 | goto out_free_path; |
| 2393 | } |
| 2394 | |
| 2395 | extent = btrfs_item_ptr(path->nodes[0], path->slots[0], |
| 2396 | struct btrfs_file_extent_item); |
| 2397 | |
| 2398 | if (btrfs_file_extent_generation(path->nodes[0], extent) != |
| 2399 | backref->generation) |
| 2400 | goto out_free_path; |
| 2401 | |
| 2402 | btrfs_release_path(path); |
| 2403 | |
| 2404 | start = backref->file_pos; |
| 2405 | if (backref->extent_offset < old->extent_offset + old->offset) |
| 2406 | start += old->extent_offset + old->offset - |
| 2407 | backref->extent_offset; |
| 2408 | |
| 2409 | len = min(backref->extent_offset + backref->num_bytes, |
| 2410 | old->extent_offset + old->offset + old->len); |
| 2411 | len -= max(backref->extent_offset, old->extent_offset + old->offset); |
| 2412 | |
| 2413 | ret = btrfs_drop_extents(trans, root, inode, start, |
| 2414 | start + len, 1); |
| 2415 | if (ret) |
| 2416 | goto out_free_path; |
| 2417 | again: |
| 2418 | key.objectid = btrfs_ino(inode); |
| 2419 | key.type = BTRFS_EXTENT_DATA_KEY; |
| 2420 | key.offset = start; |
| 2421 | |
Liu Bo | a09a0a7 | 2013-03-11 09:20:58 +0000 | [diff] [blame] | 2422 | path->leave_spinning = 1; |
Liu Bo | 38c227d | 2013-01-29 03:18:40 +0000 | [diff] [blame] | 2423 | if (merge) { |
| 2424 | struct btrfs_file_extent_item *fi; |
| 2425 | u64 extent_len; |
| 2426 | struct btrfs_key found_key; |
| 2427 | |
Gui Hecheng | 3c9665d | 2014-01-23 13:41:09 +0800 | [diff] [blame] | 2428 | ret = btrfs_search_slot(trans, root, &key, path, 0, 1); |
Liu Bo | 38c227d | 2013-01-29 03:18:40 +0000 | [diff] [blame] | 2429 | if (ret < 0) |
| 2430 | goto out_free_path; |
| 2431 | |
| 2432 | path->slots[0]--; |
| 2433 | leaf = path->nodes[0]; |
| 2434 | btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]); |
| 2435 | |
| 2436 | fi = btrfs_item_ptr(leaf, path->slots[0], |
| 2437 | struct btrfs_file_extent_item); |
| 2438 | extent_len = btrfs_file_extent_num_bytes(leaf, fi); |
| 2439 | |
Liu Bo | 116e002 | 2013-08-02 16:30:40 +0800 | [diff] [blame] | 2440 | if (extent_len + found_key.offset == start && |
| 2441 | relink_is_mergable(leaf, fi, new)) { |
Liu Bo | 38c227d | 2013-01-29 03:18:40 +0000 | [diff] [blame] | 2442 | btrfs_set_file_extent_num_bytes(leaf, fi, |
| 2443 | extent_len + len); |
| 2444 | btrfs_mark_buffer_dirty(leaf); |
| 2445 | inode_add_bytes(inode, len); |
| 2446 | |
| 2447 | ret = 1; |
| 2448 | goto out_free_path; |
| 2449 | } else { |
| 2450 | merge = false; |
| 2451 | btrfs_release_path(path); |
| 2452 | goto again; |
| 2453 | } |
| 2454 | } |
| 2455 | |
| 2456 | ret = btrfs_insert_empty_item(trans, root, path, &key, |
| 2457 | sizeof(*extent)); |
| 2458 | if (ret) { |
| 2459 | btrfs_abort_transaction(trans, root, ret); |
| 2460 | goto out_free_path; |
| 2461 | } |
| 2462 | |
| 2463 | leaf = path->nodes[0]; |
| 2464 | item = btrfs_item_ptr(leaf, path->slots[0], |
| 2465 | struct btrfs_file_extent_item); |
| 2466 | btrfs_set_file_extent_disk_bytenr(leaf, item, new->bytenr); |
| 2467 | btrfs_set_file_extent_disk_num_bytes(leaf, item, new->disk_len); |
| 2468 | btrfs_set_file_extent_offset(leaf, item, start - new->file_pos); |
| 2469 | btrfs_set_file_extent_num_bytes(leaf, item, len); |
| 2470 | btrfs_set_file_extent_ram_bytes(leaf, item, new->len); |
| 2471 | btrfs_set_file_extent_generation(leaf, item, trans->transid); |
| 2472 | btrfs_set_file_extent_type(leaf, item, BTRFS_FILE_EXTENT_REG); |
| 2473 | btrfs_set_file_extent_compression(leaf, item, new->compress_type); |
| 2474 | btrfs_set_file_extent_encryption(leaf, item, 0); |
| 2475 | btrfs_set_file_extent_other_encoding(leaf, item, 0); |
| 2476 | |
| 2477 | btrfs_mark_buffer_dirty(leaf); |
| 2478 | inode_add_bytes(inode, len); |
Liu Bo | a09a0a7 | 2013-03-11 09:20:58 +0000 | [diff] [blame] | 2479 | btrfs_release_path(path); |
Liu Bo | 38c227d | 2013-01-29 03:18:40 +0000 | [diff] [blame] | 2480 | |
| 2481 | ret = btrfs_inc_extent_ref(trans, root, new->bytenr, |
| 2482 | new->disk_len, 0, |
| 2483 | backref->root_id, backref->inum, |
| 2484 | new->file_pos, 0); /* start - extent_offset */ |
| 2485 | if (ret) { |
| 2486 | btrfs_abort_transaction(trans, root, ret); |
| 2487 | goto out_free_path; |
| 2488 | } |
| 2489 | |
| 2490 | ret = 1; |
| 2491 | out_free_path: |
| 2492 | btrfs_release_path(path); |
Liu Bo | a09a0a7 | 2013-03-11 09:20:58 +0000 | [diff] [blame] | 2493 | path->leave_spinning = 0; |
Liu Bo | 38c227d | 2013-01-29 03:18:40 +0000 | [diff] [blame] | 2494 | btrfs_end_transaction(trans, root); |
| 2495 | out_unlock: |
| 2496 | unlock_extent_cached(&BTRFS_I(inode)->io_tree, lock_start, lock_end, |
| 2497 | &cached, GFP_NOFS); |
| 2498 | iput(inode); |
| 2499 | return ret; |
| 2500 | } |
| 2501 | |
Liu Bo | 6f51956 | 2013-10-29 10:45:05 +0800 | [diff] [blame] | 2502 | static void free_sa_defrag_extent(struct new_sa_defrag_extent *new) |
| 2503 | { |
| 2504 | struct old_sa_defrag_extent *old, *tmp; |
| 2505 | |
| 2506 | if (!new) |
| 2507 | return; |
| 2508 | |
| 2509 | list_for_each_entry_safe(old, tmp, &new->head, list) { |
| 2510 | list_del(&old->list); |
| 2511 | kfree(old); |
| 2512 | } |
| 2513 | kfree(new); |
| 2514 | } |
| 2515 | |
Liu Bo | 38c227d | 2013-01-29 03:18:40 +0000 | [diff] [blame] | 2516 | static void relink_file_extents(struct new_sa_defrag_extent *new) |
| 2517 | { |
| 2518 | struct btrfs_path *path; |
Liu Bo | 38c227d | 2013-01-29 03:18:40 +0000 | [diff] [blame] | 2519 | struct sa_defrag_extent_backref *backref; |
| 2520 | struct sa_defrag_extent_backref *prev = NULL; |
| 2521 | struct inode *inode; |
| 2522 | struct btrfs_root *root; |
| 2523 | struct rb_node *node; |
| 2524 | int ret; |
| 2525 | |
| 2526 | inode = new->inode; |
| 2527 | root = BTRFS_I(inode)->root; |
| 2528 | |
| 2529 | path = btrfs_alloc_path(); |
| 2530 | if (!path) |
| 2531 | return; |
| 2532 | |
| 2533 | if (!record_extent_backrefs(path, new)) { |
| 2534 | btrfs_free_path(path); |
| 2535 | goto out; |
| 2536 | } |
| 2537 | btrfs_release_path(path); |
| 2538 | |
| 2539 | while (1) { |
| 2540 | node = rb_first(&new->root); |
| 2541 | if (!node) |
| 2542 | break; |
| 2543 | rb_erase(node, &new->root); |
| 2544 | |
| 2545 | backref = rb_entry(node, struct sa_defrag_extent_backref, node); |
| 2546 | |
| 2547 | ret = relink_extent_backref(path, prev, backref); |
| 2548 | WARN_ON(ret < 0); |
| 2549 | |
| 2550 | kfree(prev); |
| 2551 | |
| 2552 | if (ret == 1) |
| 2553 | prev = backref; |
| 2554 | else |
| 2555 | prev = NULL; |
| 2556 | cond_resched(); |
| 2557 | } |
| 2558 | kfree(prev); |
| 2559 | |
| 2560 | btrfs_free_path(path); |
Liu Bo | 38c227d | 2013-01-29 03:18:40 +0000 | [diff] [blame] | 2561 | out: |
Liu Bo | 6f51956 | 2013-10-29 10:45:05 +0800 | [diff] [blame] | 2562 | free_sa_defrag_extent(new); |
| 2563 | |
Liu Bo | 38c227d | 2013-01-29 03:18:40 +0000 | [diff] [blame] | 2564 | atomic_dec(&root->fs_info->defrag_running); |
| 2565 | wake_up(&root->fs_info->transaction_wait); |
Liu Bo | 38c227d | 2013-01-29 03:18:40 +0000 | [diff] [blame] | 2566 | } |
| 2567 | |
| 2568 | static struct new_sa_defrag_extent * |
| 2569 | record_old_file_extents(struct inode *inode, |
| 2570 | struct btrfs_ordered_extent *ordered) |
| 2571 | { |
| 2572 | struct btrfs_root *root = BTRFS_I(inode)->root; |
| 2573 | struct btrfs_path *path; |
| 2574 | struct btrfs_key key; |
Liu Bo | 6f51956 | 2013-10-29 10:45:05 +0800 | [diff] [blame] | 2575 | struct old_sa_defrag_extent *old; |
Liu Bo | 38c227d | 2013-01-29 03:18:40 +0000 | [diff] [blame] | 2576 | struct new_sa_defrag_extent *new; |
| 2577 | int ret; |
| 2578 | |
| 2579 | new = kmalloc(sizeof(*new), GFP_NOFS); |
| 2580 | if (!new) |
| 2581 | return NULL; |
| 2582 | |
| 2583 | new->inode = inode; |
| 2584 | new->file_pos = ordered->file_offset; |
| 2585 | new->len = ordered->len; |
| 2586 | new->bytenr = ordered->start; |
| 2587 | new->disk_len = ordered->disk_len; |
| 2588 | new->compress_type = ordered->compress_type; |
| 2589 | new->root = RB_ROOT; |
| 2590 | INIT_LIST_HEAD(&new->head); |
| 2591 | |
| 2592 | path = btrfs_alloc_path(); |
| 2593 | if (!path) |
| 2594 | goto out_kfree; |
| 2595 | |
| 2596 | key.objectid = btrfs_ino(inode); |
| 2597 | key.type = BTRFS_EXTENT_DATA_KEY; |
| 2598 | key.offset = new->file_pos; |
| 2599 | |
| 2600 | ret = btrfs_search_slot(NULL, root, &key, path, 0, 0); |
| 2601 | if (ret < 0) |
| 2602 | goto out_free_path; |
| 2603 | if (ret > 0 && path->slots[0] > 0) |
| 2604 | path->slots[0]--; |
| 2605 | |
| 2606 | /* find out all the old extents for the file range */ |
| 2607 | while (1) { |
| 2608 | struct btrfs_file_extent_item *extent; |
| 2609 | struct extent_buffer *l; |
| 2610 | int slot; |
| 2611 | u64 num_bytes; |
| 2612 | u64 offset; |
| 2613 | u64 end; |
| 2614 | u64 disk_bytenr; |
| 2615 | u64 extent_offset; |
| 2616 | |
| 2617 | l = path->nodes[0]; |
| 2618 | slot = path->slots[0]; |
| 2619 | |
| 2620 | if (slot >= btrfs_header_nritems(l)) { |
| 2621 | ret = btrfs_next_leaf(root, path); |
| 2622 | if (ret < 0) |
Liu Bo | 6f51956 | 2013-10-29 10:45:05 +0800 | [diff] [blame] | 2623 | goto out_free_path; |
Liu Bo | 38c227d | 2013-01-29 03:18:40 +0000 | [diff] [blame] | 2624 | else if (ret > 0) |
| 2625 | break; |
| 2626 | continue; |
| 2627 | } |
| 2628 | |
| 2629 | btrfs_item_key_to_cpu(l, &key, slot); |
| 2630 | |
| 2631 | if (key.objectid != btrfs_ino(inode)) |
| 2632 | break; |
| 2633 | if (key.type != BTRFS_EXTENT_DATA_KEY) |
| 2634 | break; |
| 2635 | if (key.offset >= new->file_pos + new->len) |
| 2636 | break; |
| 2637 | |
| 2638 | extent = btrfs_item_ptr(l, slot, struct btrfs_file_extent_item); |
| 2639 | |
| 2640 | num_bytes = btrfs_file_extent_num_bytes(l, extent); |
| 2641 | if (key.offset + num_bytes < new->file_pos) |
| 2642 | goto next; |
| 2643 | |
| 2644 | disk_bytenr = btrfs_file_extent_disk_bytenr(l, extent); |
| 2645 | if (!disk_bytenr) |
| 2646 | goto next; |
| 2647 | |
| 2648 | extent_offset = btrfs_file_extent_offset(l, extent); |
| 2649 | |
| 2650 | old = kmalloc(sizeof(*old), GFP_NOFS); |
| 2651 | if (!old) |
Liu Bo | 6f51956 | 2013-10-29 10:45:05 +0800 | [diff] [blame] | 2652 | goto out_free_path; |
Liu Bo | 38c227d | 2013-01-29 03:18:40 +0000 | [diff] [blame] | 2653 | |
| 2654 | offset = max(new->file_pos, key.offset); |
| 2655 | end = min(new->file_pos + new->len, key.offset + num_bytes); |
| 2656 | |
| 2657 | old->bytenr = disk_bytenr; |
| 2658 | old->extent_offset = extent_offset; |
| 2659 | old->offset = offset - key.offset; |
| 2660 | old->len = end - offset; |
| 2661 | old->new = new; |
| 2662 | old->count = 0; |
| 2663 | list_add_tail(&old->list, &new->head); |
| 2664 | next: |
| 2665 | path->slots[0]++; |
| 2666 | cond_resched(); |
| 2667 | } |
| 2668 | |
| 2669 | btrfs_free_path(path); |
| 2670 | atomic_inc(&root->fs_info->defrag_running); |
| 2671 | |
| 2672 | return new; |
| 2673 | |
Liu Bo | 38c227d | 2013-01-29 03:18:40 +0000 | [diff] [blame] | 2674 | out_free_path: |
| 2675 | btrfs_free_path(path); |
| 2676 | out_kfree: |
Liu Bo | 6f51956 | 2013-10-29 10:45:05 +0800 | [diff] [blame] | 2677 | free_sa_defrag_extent(new); |
Liu Bo | 38c227d | 2013-01-29 03:18:40 +0000 | [diff] [blame] | 2678 | return NULL; |
| 2679 | } |
| 2680 | |
Miao Xie | e570fd2 | 2014-06-19 10:42:50 +0800 | [diff] [blame] | 2681 | static void btrfs_release_delalloc_bytes(struct btrfs_root *root, |
| 2682 | u64 start, u64 len) |
| 2683 | { |
| 2684 | struct btrfs_block_group_cache *cache; |
| 2685 | |
| 2686 | cache = btrfs_lookup_block_group(root->fs_info, start); |
| 2687 | ASSERT(cache); |
| 2688 | |
| 2689 | spin_lock(&cache->lock); |
| 2690 | cache->delalloc_bytes -= len; |
| 2691 | spin_unlock(&cache->lock); |
| 2692 | |
| 2693 | btrfs_put_block_group(cache); |
| 2694 | } |
| 2695 | |
Chris Mason | d352ac6 | 2008-09-29 15:18:18 -0400 | [diff] [blame] | 2696 | /* as ordered data IO finishes, this gets called so we can finish |
| 2697 | * an ordered extent if the range of bytes in the file it covers are |
| 2698 | * fully written. |
| 2699 | */ |
Josef Bacik | 5fd0204 | 2012-05-02 14:00:54 -0400 | [diff] [blame] | 2700 | static int btrfs_finish_ordered_io(struct btrfs_ordered_extent *ordered_extent) |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 2701 | { |
Josef Bacik | 5fd0204 | 2012-05-02 14:00:54 -0400 | [diff] [blame] | 2702 | struct inode *inode = ordered_extent->inode; |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 2703 | struct btrfs_root *root = BTRFS_I(inode)->root; |
Yan, Zheng | 0ca1f7c | 2010-05-16 10:48:47 -0400 | [diff] [blame] | 2704 | struct btrfs_trans_handle *trans = NULL; |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 2705 | struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree; |
Josef Bacik | 2ac55d4 | 2010-02-03 19:33:23 +0000 | [diff] [blame] | 2706 | struct extent_state *cached_state = NULL; |
Liu Bo | 38c227d | 2013-01-29 03:18:40 +0000 | [diff] [blame] | 2707 | struct new_sa_defrag_extent *new = NULL; |
Li Zefan | 261507a0 | 2010-12-17 14:21:50 +0800 | [diff] [blame] | 2708 | int compress_type = 0; |
Josef Bacik | 77cef2e | 2013-08-29 13:57:21 -0400 | [diff] [blame] | 2709 | int ret = 0; |
| 2710 | u64 logical_len = ordered_extent->len; |
Li Zefan | 82d5902 | 2011-04-20 10:33:24 +0800 | [diff] [blame] | 2711 | bool nolock; |
Josef Bacik | 77cef2e | 2013-08-29 13:57:21 -0400 | [diff] [blame] | 2712 | bool truncated = false; |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 2713 | |
Liu Bo | 83eea1f | 2012-07-10 05:28:39 -0600 | [diff] [blame] | 2714 | nolock = btrfs_is_free_space_inode(inode); |
Josef Bacik | 0cb59c9 | 2010-07-02 12:14:14 -0400 | [diff] [blame] | 2715 | |
Josef Bacik | 5fd0204 | 2012-05-02 14:00:54 -0400 | [diff] [blame] | 2716 | if (test_bit(BTRFS_ORDERED_IOERR, &ordered_extent->flags)) { |
| 2717 | ret = -EIO; |
| 2718 | goto out; |
| 2719 | } |
| 2720 | |
Miao Xie | f612496 | 2014-09-12 18:44:04 +0800 | [diff] [blame] | 2721 | btrfs_free_io_failure_record(inode, ordered_extent->file_offset, |
| 2722 | ordered_extent->file_offset + |
| 2723 | ordered_extent->len - 1); |
| 2724 | |
Josef Bacik | 77cef2e | 2013-08-29 13:57:21 -0400 | [diff] [blame] | 2725 | if (test_bit(BTRFS_ORDERED_TRUNCATED, &ordered_extent->flags)) { |
| 2726 | truncated = true; |
| 2727 | logical_len = ordered_extent->truncated_len; |
| 2728 | /* Truncated the entire extent, don't bother adding */ |
| 2729 | if (!logical_len) |
| 2730 | goto out; |
| 2731 | } |
| 2732 | |
Yan, Zheng | c216775 | 2009-11-12 09:34:21 +0000 | [diff] [blame] | 2733 | if (test_bit(BTRFS_ORDERED_NOCOW, &ordered_extent->flags)) { |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 2734 | BUG_ON(!list_empty(&ordered_extent->list)); /* Logic error */ |
Josef Bacik | 6c760c0 | 2012-11-09 10:53:21 -0500 | [diff] [blame] | 2735 | btrfs_ordered_update_i_size(inode, 0, ordered_extent); |
| 2736 | if (nolock) |
| 2737 | trans = btrfs_join_transaction_nolock(root); |
| 2738 | else |
| 2739 | trans = btrfs_join_transaction(root); |
| 2740 | if (IS_ERR(trans)) { |
| 2741 | ret = PTR_ERR(trans); |
| 2742 | trans = NULL; |
| 2743 | goto out; |
Yan, Zheng | c216775 | 2009-11-12 09:34:21 +0000 | [diff] [blame] | 2744 | } |
Josef Bacik | 6c760c0 | 2012-11-09 10:53:21 -0500 | [diff] [blame] | 2745 | trans->block_rsv = &root->fs_info->delalloc_block_rsv; |
| 2746 | ret = btrfs_update_inode_fallback(trans, root, inode); |
| 2747 | if (ret) /* -ENOMEM or corruption */ |
| 2748 | btrfs_abort_transaction(trans, root, ret); |
Yan, Zheng | c216775 | 2009-11-12 09:34:21 +0000 | [diff] [blame] | 2749 | goto out; |
| 2750 | } |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 2751 | |
Josef Bacik | 2ac55d4 | 2010-02-03 19:33:23 +0000 | [diff] [blame] | 2752 | lock_extent_bits(io_tree, ordered_extent->file_offset, |
| 2753 | ordered_extent->file_offset + ordered_extent->len - 1, |
Jeff Mahoney | d008237 | 2012-03-01 14:57:19 +0100 | [diff] [blame] | 2754 | 0, &cached_state); |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 2755 | |
Liu Bo | 38c227d | 2013-01-29 03:18:40 +0000 | [diff] [blame] | 2756 | ret = test_range_bit(io_tree, ordered_extent->file_offset, |
| 2757 | ordered_extent->file_offset + ordered_extent->len - 1, |
| 2758 | EXTENT_DEFRAG, 1, cached_state); |
| 2759 | if (ret) { |
| 2760 | u64 last_snapshot = btrfs_root_last_snapshot(&root->root_item); |
Josef Bacik | 8101c8d | 2014-01-29 16:05:30 -0500 | [diff] [blame] | 2761 | if (0 && last_snapshot >= BTRFS_I(inode)->generation) |
Liu Bo | 38c227d | 2013-01-29 03:18:40 +0000 | [diff] [blame] | 2762 | /* the inode is shared */ |
| 2763 | new = record_old_file_extents(inode, ordered_extent); |
| 2764 | |
| 2765 | clear_extent_bit(io_tree, ordered_extent->file_offset, |
| 2766 | ordered_extent->file_offset + ordered_extent->len - 1, |
| 2767 | EXTENT_DEFRAG, 0, 0, &cached_state, GFP_NOFS); |
| 2768 | } |
| 2769 | |
Josef Bacik | 0cb59c9 | 2010-07-02 12:14:14 -0400 | [diff] [blame] | 2770 | if (nolock) |
Josef Bacik | 7a7eaa4 | 2011-04-13 12:54:33 -0400 | [diff] [blame] | 2771 | trans = btrfs_join_transaction_nolock(root); |
Josef Bacik | 0cb59c9 | 2010-07-02 12:14:14 -0400 | [diff] [blame] | 2772 | else |
Josef Bacik | 7a7eaa4 | 2011-04-13 12:54:33 -0400 | [diff] [blame] | 2773 | trans = btrfs_join_transaction(root); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 2774 | if (IS_ERR(trans)) { |
| 2775 | ret = PTR_ERR(trans); |
| 2776 | trans = NULL; |
| 2777 | goto out_unlock; |
| 2778 | } |
Chris Mason | a79b7d4 | 2014-05-22 16:18:52 -0700 | [diff] [blame] | 2779 | |
Yan, Zheng | 0ca1f7c | 2010-05-16 10:48:47 -0400 | [diff] [blame] | 2780 | trans->block_rsv = &root->fs_info->delalloc_block_rsv; |
Yan, Zheng | c216775 | 2009-11-12 09:34:21 +0000 | [diff] [blame] | 2781 | |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 2782 | if (test_bit(BTRFS_ORDERED_COMPRESSED, &ordered_extent->flags)) |
Li Zefan | 261507a0 | 2010-12-17 14:21:50 +0800 | [diff] [blame] | 2783 | compress_type = ordered_extent->compress_type; |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 2784 | if (test_bit(BTRFS_ORDERED_PREALLOC, &ordered_extent->flags)) { |
Li Zefan | 261507a0 | 2010-12-17 14:21:50 +0800 | [diff] [blame] | 2785 | BUG_ON(compress_type); |
Yan, Zheng | 920bbbf | 2009-11-12 09:34:08 +0000 | [diff] [blame] | 2786 | ret = btrfs_mark_extent_written(trans, inode, |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 2787 | ordered_extent->file_offset, |
| 2788 | ordered_extent->file_offset + |
Josef Bacik | 77cef2e | 2013-08-29 13:57:21 -0400 | [diff] [blame] | 2789 | logical_len); |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 2790 | } else { |
Josef Bacik | 0af3d00 | 2010-06-21 14:48:16 -0400 | [diff] [blame] | 2791 | BUG_ON(root == root->fs_info->tree_root); |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 2792 | ret = insert_reserved_file_extent(trans, inode, |
| 2793 | ordered_extent->file_offset, |
| 2794 | ordered_extent->start, |
| 2795 | ordered_extent->disk_len, |
Josef Bacik | 77cef2e | 2013-08-29 13:57:21 -0400 | [diff] [blame] | 2796 | logical_len, logical_len, |
Li Zefan | 261507a0 | 2010-12-17 14:21:50 +0800 | [diff] [blame] | 2797 | compress_type, 0, 0, |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 2798 | BTRFS_FILE_EXTENT_REG); |
Miao Xie | e570fd2 | 2014-06-19 10:42:50 +0800 | [diff] [blame] | 2799 | if (!ret) |
| 2800 | btrfs_release_delalloc_bytes(root, |
| 2801 | ordered_extent->start, |
| 2802 | ordered_extent->disk_len); |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 2803 | } |
Josef Bacik | 5dc562c | 2012-08-17 13:14:17 -0400 | [diff] [blame] | 2804 | unpin_extent_cache(&BTRFS_I(inode)->extent_tree, |
| 2805 | ordered_extent->file_offset, ordered_extent->len, |
| 2806 | trans->transid); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 2807 | if (ret < 0) { |
| 2808 | btrfs_abort_transaction(trans, root, ret); |
Josef Bacik | 5fd0204 | 2012-05-02 14:00:54 -0400 | [diff] [blame] | 2809 | goto out_unlock; |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 2810 | } |
Josef Bacik | 2ac55d4 | 2010-02-03 19:33:23 +0000 | [diff] [blame] | 2811 | |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 2812 | add_pending_csums(trans, inode, ordered_extent->file_offset, |
| 2813 | &ordered_extent->list); |
| 2814 | |
Josef Bacik | 6c760c0 | 2012-11-09 10:53:21 -0500 | [diff] [blame] | 2815 | btrfs_ordered_update_i_size(inode, 0, ordered_extent); |
| 2816 | ret = btrfs_update_inode_fallback(trans, root, inode); |
| 2817 | if (ret) { /* -ENOMEM or corruption */ |
| 2818 | btrfs_abort_transaction(trans, root, ret); |
| 2819 | goto out_unlock; |
Josef Bacik | 1ef30be | 2011-04-05 19:25:36 -0400 | [diff] [blame] | 2820 | } |
| 2821 | ret = 0; |
Josef Bacik | 5fd0204 | 2012-05-02 14:00:54 -0400 | [diff] [blame] | 2822 | out_unlock: |
| 2823 | unlock_extent_cached(io_tree, ordered_extent->file_offset, |
| 2824 | ordered_extent->file_offset + |
| 2825 | ordered_extent->len - 1, &cached_state, GFP_NOFS); |
Yan, Zheng | c216775 | 2009-11-12 09:34:21 +0000 | [diff] [blame] | 2826 | out: |
Josef Bacik | 5b0e95b | 2011-10-06 08:58:24 -0400 | [diff] [blame] | 2827 | if (root != root->fs_info->tree_root) |
Josef Bacik | 0cb59c9 | 2010-07-02 12:14:14 -0400 | [diff] [blame] | 2828 | btrfs_delalloc_release_metadata(inode, ordered_extent->len); |
Miao Xie | a698d075 | 2012-09-20 01:51:59 -0600 | [diff] [blame] | 2829 | if (trans) |
| 2830 | btrfs_end_transaction(trans, root); |
Josef Bacik | 0cb59c9 | 2010-07-02 12:14:14 -0400 | [diff] [blame] | 2831 | |
Josef Bacik | 77cef2e | 2013-08-29 13:57:21 -0400 | [diff] [blame] | 2832 | if (ret || truncated) { |
| 2833 | u64 start, end; |
| 2834 | |
| 2835 | if (truncated) |
| 2836 | start = ordered_extent->file_offset + logical_len; |
| 2837 | else |
| 2838 | start = ordered_extent->file_offset; |
| 2839 | end = ordered_extent->file_offset + ordered_extent->len - 1; |
| 2840 | clear_extent_uptodate(io_tree, start, end, NULL, GFP_NOFS); |
| 2841 | |
| 2842 | /* Drop the cache for the part of the extent we didn't write. */ |
| 2843 | btrfs_drop_extent_cache(inode, start, end, 0); |
Josef Bacik | 5fd0204 | 2012-05-02 14:00:54 -0400 | [diff] [blame] | 2844 | |
Josef Bacik | 0bec9ef | 2013-01-31 14:58:00 -0500 | [diff] [blame] | 2845 | /* |
| 2846 | * If the ordered extent had an IOERR or something else went |
| 2847 | * wrong we need to return the space for this ordered extent |
Josef Bacik | 77cef2e | 2013-08-29 13:57:21 -0400 | [diff] [blame] | 2848 | * back to the allocator. We only free the extent in the |
| 2849 | * truncated case if we didn't write out the extent at all. |
Josef Bacik | 0bec9ef | 2013-01-31 14:58:00 -0500 | [diff] [blame] | 2850 | */ |
Josef Bacik | 77cef2e | 2013-08-29 13:57:21 -0400 | [diff] [blame] | 2851 | if ((ret || !logical_len) && |
| 2852 | !test_bit(BTRFS_ORDERED_NOCOW, &ordered_extent->flags) && |
Josef Bacik | 0bec9ef | 2013-01-31 14:58:00 -0500 | [diff] [blame] | 2853 | !test_bit(BTRFS_ORDERED_PREALLOC, &ordered_extent->flags)) |
| 2854 | btrfs_free_reserved_extent(root, ordered_extent->start, |
Miao Xie | e570fd2 | 2014-06-19 10:42:50 +0800 | [diff] [blame] | 2855 | ordered_extent->disk_len, 1); |
Josef Bacik | 0bec9ef | 2013-01-31 14:58:00 -0500 | [diff] [blame] | 2856 | } |
| 2857 | |
| 2858 | |
Josef Bacik | 5fd0204 | 2012-05-02 14:00:54 -0400 | [diff] [blame] | 2859 | /* |
Liu Bo | 8bad3c0 | 2012-06-18 12:14:23 +0800 | [diff] [blame] | 2860 | * This needs to be done to make sure anybody waiting knows we are done |
| 2861 | * updating everything for this ordered extent. |
Josef Bacik | 5fd0204 | 2012-05-02 14:00:54 -0400 | [diff] [blame] | 2862 | */ |
| 2863 | btrfs_remove_ordered_extent(inode, ordered_extent); |
| 2864 | |
Liu Bo | 38c227d | 2013-01-29 03:18:40 +0000 | [diff] [blame] | 2865 | /* for snapshot-aware defrag */ |
Liu Bo | 6f51956 | 2013-10-29 10:45:05 +0800 | [diff] [blame] | 2866 | if (new) { |
| 2867 | if (ret) { |
| 2868 | free_sa_defrag_extent(new); |
| 2869 | atomic_dec(&root->fs_info->defrag_running); |
| 2870 | } else { |
| 2871 | relink_file_extents(new); |
| 2872 | } |
| 2873 | } |
Liu Bo | 38c227d | 2013-01-29 03:18:40 +0000 | [diff] [blame] | 2874 | |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 2875 | /* once for us */ |
| 2876 | btrfs_put_ordered_extent(ordered_extent); |
| 2877 | /* once for the tree */ |
| 2878 | btrfs_put_ordered_extent(ordered_extent); |
| 2879 | |
Josef Bacik | 5fd0204 | 2012-05-02 14:00:54 -0400 | [diff] [blame] | 2880 | return ret; |
| 2881 | } |
| 2882 | |
| 2883 | static void finish_ordered_fn(struct btrfs_work *work) |
| 2884 | { |
| 2885 | struct btrfs_ordered_extent *ordered_extent; |
| 2886 | ordered_extent = container_of(work, struct btrfs_ordered_extent, work); |
| 2887 | btrfs_finish_ordered_io(ordered_extent); |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 2888 | } |
| 2889 | |
Christoph Hellwig | b295086 | 2008-12-02 09:54:17 -0500 | [diff] [blame] | 2890 | 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] | 2891 | struct extent_state *state, int uptodate) |
| 2892 | { |
Josef Bacik | 5fd0204 | 2012-05-02 14:00:54 -0400 | [diff] [blame] | 2893 | struct inode *inode = page->mapping->host; |
| 2894 | struct btrfs_root *root = BTRFS_I(inode)->root; |
| 2895 | struct btrfs_ordered_extent *ordered_extent = NULL; |
Liu Bo | 9e0af23 | 2014-08-15 23:36:53 +0800 | [diff] [blame] | 2896 | struct btrfs_workqueue *wq; |
| 2897 | btrfs_work_func_t func; |
Josef Bacik | 5fd0204 | 2012-05-02 14:00:54 -0400 | [diff] [blame] | 2898 | |
liubo | 1abe9b8 | 2011-03-24 11:18:59 +0000 | [diff] [blame] | 2899 | trace_btrfs_writepage_end_io_hook(page, start, end, uptodate); |
| 2900 | |
Chris Mason | 8b62b72 | 2009-09-02 16:53:46 -0400 | [diff] [blame] | 2901 | ClearPagePrivate2(page); |
Josef Bacik | 5fd0204 | 2012-05-02 14:00:54 -0400 | [diff] [blame] | 2902 | if (!btrfs_dec_test_ordered_pending(inode, &ordered_extent, start, |
| 2903 | end - start + 1, uptodate)) |
| 2904 | return 0; |
| 2905 | |
Liu Bo | 9e0af23 | 2014-08-15 23:36:53 +0800 | [diff] [blame] | 2906 | if (btrfs_is_free_space_inode(inode)) { |
| 2907 | wq = root->fs_info->endio_freespace_worker; |
| 2908 | func = btrfs_freespace_write_helper; |
| 2909 | } else { |
| 2910 | wq = root->fs_info->endio_write_workers; |
| 2911 | func = btrfs_endio_write_helper; |
| 2912 | } |
Josef Bacik | 5fd0204 | 2012-05-02 14:00:54 -0400 | [diff] [blame] | 2913 | |
Liu Bo | 9e0af23 | 2014-08-15 23:36:53 +0800 | [diff] [blame] | 2914 | btrfs_init_work(&ordered_extent->work, func, finish_ordered_fn, NULL, |
| 2915 | NULL); |
| 2916 | btrfs_queue_work(wq, &ordered_extent->work); |
Josef Bacik | 5fd0204 | 2012-05-02 14:00:54 -0400 | [diff] [blame] | 2917 | |
| 2918 | return 0; |
Chris Mason | 211f90e | 2008-07-18 11:56:15 -0400 | [diff] [blame] | 2919 | } |
| 2920 | |
Miao Xie | dc380ae | 2014-09-12 18:43:55 +0800 | [diff] [blame] | 2921 | static int __readpage_endio_check(struct inode *inode, |
| 2922 | struct btrfs_io_bio *io_bio, |
| 2923 | int icsum, struct page *page, |
| 2924 | int pgoff, u64 start, size_t len) |
| 2925 | { |
| 2926 | char *kaddr; |
| 2927 | u32 csum_expected; |
| 2928 | u32 csum = ~(u32)0; |
| 2929 | static DEFINE_RATELIMIT_STATE(_rs, DEFAULT_RATELIMIT_INTERVAL, |
| 2930 | DEFAULT_RATELIMIT_BURST); |
| 2931 | |
| 2932 | csum_expected = *(((u32 *)io_bio->csum) + icsum); |
| 2933 | |
| 2934 | kaddr = kmap_atomic(page); |
| 2935 | csum = btrfs_csum_data(kaddr + pgoff, csum, len); |
| 2936 | btrfs_csum_final(csum, (char *)&csum); |
| 2937 | if (csum != csum_expected) |
| 2938 | goto zeroit; |
| 2939 | |
| 2940 | kunmap_atomic(kaddr); |
| 2941 | return 0; |
| 2942 | zeroit: |
| 2943 | if (__ratelimit(&_rs)) |
| 2944 | btrfs_info(BTRFS_I(inode)->root->fs_info, |
| 2945 | "csum failed ino %llu off %llu csum %u expected csum %u", |
| 2946 | btrfs_ino(inode), start, csum, csum_expected); |
| 2947 | memset(kaddr + pgoff, 1, len); |
| 2948 | flush_dcache_page(page); |
| 2949 | kunmap_atomic(kaddr); |
| 2950 | if (csum_expected == 0) |
| 2951 | return 0; |
| 2952 | return -EIO; |
| 2953 | } |
| 2954 | |
Chris Mason | d352ac6 | 2008-09-29 15:18:18 -0400 | [diff] [blame] | 2955 | /* |
Chris Mason | d352ac6 | 2008-09-29 15:18:18 -0400 | [diff] [blame] | 2956 | * 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] | 2957 | * if there's a match, we allow the bio to finish. If not, the code in |
| 2958 | * extent_io.c will try to find good copies for us. |
Chris Mason | d352ac6 | 2008-09-29 15:18:18 -0400 | [diff] [blame] | 2959 | */ |
Miao Xie | facc8a22 | 2013-07-25 19:22:34 +0800 | [diff] [blame] | 2960 | static int btrfs_readpage_end_io_hook(struct btrfs_io_bio *io_bio, |
| 2961 | u64 phy_offset, struct page *page, |
| 2962 | u64 start, u64 end, int mirror) |
Chris Mason | 07157aa | 2007-08-30 08:50:51 -0400 | [diff] [blame] | 2963 | { |
Miao Xie | 4eee4fa | 2012-12-21 09:17:45 +0000 | [diff] [blame] | 2964 | size_t offset = start - page_offset(page); |
Chris Mason | 07157aa | 2007-08-30 08:50:51 -0400 | [diff] [blame] | 2965 | struct inode *inode = page->mapping->host; |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 2966 | struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree; |
Chris Mason | ff79f81 | 2007-10-15 16:22:25 -0400 | [diff] [blame] | 2967 | struct btrfs_root *root = BTRFS_I(inode)->root; |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 2968 | |
Chris Mason | d20f704 | 2008-12-08 16:58:54 -0500 | [diff] [blame] | 2969 | if (PageChecked(page)) { |
| 2970 | ClearPageChecked(page); |
Miao Xie | dc380ae | 2014-09-12 18:43:55 +0800 | [diff] [blame] | 2971 | return 0; |
Chris Mason | d20f704 | 2008-12-08 16:58:54 -0500 | [diff] [blame] | 2972 | } |
Christoph Hellwig | 6cbff00 | 2009-04-17 10:37:41 +0200 | [diff] [blame] | 2973 | |
| 2974 | if (BTRFS_I(inode)->flags & BTRFS_INODE_NODATASUM) |
Miao Xie | dc380ae | 2014-09-12 18:43:55 +0800 | [diff] [blame] | 2975 | return 0; |
Chris Mason | d20f704 | 2008-12-08 16:58:54 -0500 | [diff] [blame] | 2976 | |
Yan Zheng | 17d217f | 2008-12-12 10:03:38 -0500 | [diff] [blame] | 2977 | if (root->root_key.objectid == BTRFS_DATA_RELOC_TREE_OBJECTID && |
Chris Mason | 9655d29 | 2009-09-02 15:22:30 -0400 | [diff] [blame] | 2978 | test_range_bit(io_tree, start, end, EXTENT_NODATASUM, 1, NULL)) { |
Yan Zheng | 17d217f | 2008-12-12 10:03:38 -0500 | [diff] [blame] | 2979 | clear_extent_bits(io_tree, start, end, EXTENT_NODATASUM, |
| 2980 | GFP_NOFS); |
| 2981 | return 0; |
| 2982 | } |
| 2983 | |
Miao Xie | facc8a22 | 2013-07-25 19:22:34 +0800 | [diff] [blame] | 2984 | phy_offset >>= inode->i_sb->s_blocksize_bits; |
Miao Xie | dc380ae | 2014-09-12 18:43:55 +0800 | [diff] [blame] | 2985 | return __readpage_endio_check(inode, io_bio, phy_offset, page, offset, |
| 2986 | start, (size_t)(end - start + 1)); |
Chris Mason | 07157aa | 2007-08-30 08:50:51 -0400 | [diff] [blame] | 2987 | } |
Chris Mason | b888db2b | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 2988 | |
Yan, Zheng | 24bbcf0 | 2009-11-12 09:36:34 +0000 | [diff] [blame] | 2989 | struct delayed_iput { |
| 2990 | struct list_head list; |
| 2991 | struct inode *inode; |
| 2992 | }; |
| 2993 | |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 2994 | /* JDM: If this is fs-wide, why can't we add a pointer to |
| 2995 | * btrfs_inode instead and avoid the allocation? */ |
Yan, Zheng | 24bbcf0 | 2009-11-12 09:36:34 +0000 | [diff] [blame] | 2996 | void btrfs_add_delayed_iput(struct inode *inode) |
| 2997 | { |
| 2998 | struct btrfs_fs_info *fs_info = BTRFS_I(inode)->root->fs_info; |
| 2999 | struct delayed_iput *delayed; |
| 3000 | |
| 3001 | if (atomic_add_unless(&inode->i_count, -1, 1)) |
| 3002 | return; |
| 3003 | |
| 3004 | delayed = kmalloc(sizeof(*delayed), GFP_NOFS | __GFP_NOFAIL); |
| 3005 | delayed->inode = inode; |
| 3006 | |
| 3007 | spin_lock(&fs_info->delayed_iput_lock); |
| 3008 | list_add_tail(&delayed->list, &fs_info->delayed_iputs); |
| 3009 | spin_unlock(&fs_info->delayed_iput_lock); |
| 3010 | } |
| 3011 | |
| 3012 | void btrfs_run_delayed_iputs(struct btrfs_root *root) |
| 3013 | { |
| 3014 | LIST_HEAD(list); |
| 3015 | struct btrfs_fs_info *fs_info = root->fs_info; |
| 3016 | struct delayed_iput *delayed; |
| 3017 | int empty; |
| 3018 | |
| 3019 | spin_lock(&fs_info->delayed_iput_lock); |
| 3020 | empty = list_empty(&fs_info->delayed_iputs); |
| 3021 | spin_unlock(&fs_info->delayed_iput_lock); |
| 3022 | if (empty) |
| 3023 | return; |
| 3024 | |
Yan, Zheng | 24bbcf0 | 2009-11-12 09:36:34 +0000 | [diff] [blame] | 3025 | spin_lock(&fs_info->delayed_iput_lock); |
| 3026 | list_splice_init(&fs_info->delayed_iputs, &list); |
| 3027 | spin_unlock(&fs_info->delayed_iput_lock); |
| 3028 | |
| 3029 | while (!list_empty(&list)) { |
| 3030 | delayed = list_entry(list.next, struct delayed_iput, list); |
| 3031 | list_del(&delayed->list); |
| 3032 | iput(delayed->inode); |
| 3033 | kfree(delayed); |
| 3034 | } |
Yan, Zheng | 24bbcf0 | 2009-11-12 09:36:34 +0000 | [diff] [blame] | 3035 | } |
| 3036 | |
Yan, Zheng | d68fc57 | 2010-05-16 10:49:58 -0400 | [diff] [blame] | 3037 | /* |
Justin P. Mattock | 42b2aa8 | 2011-11-28 20:31:00 -0800 | [diff] [blame] | 3038 | * This is called in transaction commit time. If there are no orphan |
Yan, Zheng | d68fc57 | 2010-05-16 10:49:58 -0400 | [diff] [blame] | 3039 | * files in the subvolume, it removes orphan item and frees block_rsv |
| 3040 | * structure. |
| 3041 | */ |
| 3042 | void btrfs_orphan_commit_root(struct btrfs_trans_handle *trans, |
| 3043 | struct btrfs_root *root) |
| 3044 | { |
Josef Bacik | 90290e1 | 2011-12-02 15:44:12 -0500 | [diff] [blame] | 3045 | struct btrfs_block_rsv *block_rsv; |
Yan, Zheng | d68fc57 | 2010-05-16 10:49:58 -0400 | [diff] [blame] | 3046 | int ret; |
| 3047 | |
Josef Bacik | 8a35d95 | 2012-05-23 14:26:42 -0400 | [diff] [blame] | 3048 | if (atomic_read(&root->orphan_inodes) || |
Yan, Zheng | d68fc57 | 2010-05-16 10:49:58 -0400 | [diff] [blame] | 3049 | root->orphan_cleanup_state != ORPHAN_CLEANUP_DONE) |
| 3050 | return; |
| 3051 | |
Josef Bacik | 90290e1 | 2011-12-02 15:44:12 -0500 | [diff] [blame] | 3052 | spin_lock(&root->orphan_lock); |
Josef Bacik | 8a35d95 | 2012-05-23 14:26:42 -0400 | [diff] [blame] | 3053 | if (atomic_read(&root->orphan_inodes)) { |
Josef Bacik | 90290e1 | 2011-12-02 15:44:12 -0500 | [diff] [blame] | 3054 | spin_unlock(&root->orphan_lock); |
| 3055 | return; |
| 3056 | } |
| 3057 | |
| 3058 | if (root->orphan_cleanup_state != ORPHAN_CLEANUP_DONE) { |
| 3059 | spin_unlock(&root->orphan_lock); |
| 3060 | return; |
| 3061 | } |
| 3062 | |
| 3063 | block_rsv = root->orphan_block_rsv; |
| 3064 | root->orphan_block_rsv = NULL; |
| 3065 | spin_unlock(&root->orphan_lock); |
| 3066 | |
Miao Xie | 27cdeb7 | 2014-04-02 19:51:05 +0800 | [diff] [blame] | 3067 | if (test_bit(BTRFS_ROOT_ORPHAN_ITEM_INSERTED, &root->state) && |
Yan, Zheng | d68fc57 | 2010-05-16 10:49:58 -0400 | [diff] [blame] | 3068 | btrfs_root_refs(&root->root_item) > 0) { |
| 3069 | ret = btrfs_del_orphan_item(trans, root->fs_info->tree_root, |
| 3070 | root->root_key.objectid); |
Josef Bacik | 4ef31a4 | 2013-08-13 14:10:08 -0400 | [diff] [blame] | 3071 | if (ret) |
| 3072 | btrfs_abort_transaction(trans, root, ret); |
| 3073 | else |
Miao Xie | 27cdeb7 | 2014-04-02 19:51:05 +0800 | [diff] [blame] | 3074 | clear_bit(BTRFS_ROOT_ORPHAN_ITEM_INSERTED, |
| 3075 | &root->state); |
Yan, Zheng | d68fc57 | 2010-05-16 10:49:58 -0400 | [diff] [blame] | 3076 | } |
| 3077 | |
Josef Bacik | 90290e1 | 2011-12-02 15:44:12 -0500 | [diff] [blame] | 3078 | if (block_rsv) { |
| 3079 | WARN_ON(block_rsv->size > 0); |
| 3080 | btrfs_free_block_rsv(root, block_rsv); |
Yan, Zheng | d68fc57 | 2010-05-16 10:49:58 -0400 | [diff] [blame] | 3081 | } |
| 3082 | } |
| 3083 | |
| 3084 | /* |
Josef Bacik | 7b12876 | 2008-07-24 12:17:14 -0400 | [diff] [blame] | 3085 | * This creates an orphan entry for the given inode in case something goes |
| 3086 | * wrong in the middle of an unlink/truncate. |
Yan, Zheng | d68fc57 | 2010-05-16 10:49:58 -0400 | [diff] [blame] | 3087 | * |
| 3088 | * NOTE: caller of this function should reserve 5 units of metadata for |
| 3089 | * this function. |
Josef Bacik | 7b12876 | 2008-07-24 12:17:14 -0400 | [diff] [blame] | 3090 | */ |
| 3091 | int btrfs_orphan_add(struct btrfs_trans_handle *trans, struct inode *inode) |
| 3092 | { |
| 3093 | struct btrfs_root *root = BTRFS_I(inode)->root; |
Yan, Zheng | d68fc57 | 2010-05-16 10:49:58 -0400 | [diff] [blame] | 3094 | struct btrfs_block_rsv *block_rsv = NULL; |
| 3095 | int reserve = 0; |
| 3096 | int insert = 0; |
| 3097 | int ret; |
Josef Bacik | 7b12876 | 2008-07-24 12:17:14 -0400 | [diff] [blame] | 3098 | |
Yan, Zheng | d68fc57 | 2010-05-16 10:49:58 -0400 | [diff] [blame] | 3099 | if (!root->orphan_block_rsv) { |
Miao Xie | 66d8f3d | 2012-09-06 04:02:28 -0600 | [diff] [blame] | 3100 | block_rsv = btrfs_alloc_block_rsv(root, BTRFS_BLOCK_RSV_TEMP); |
Tsutomu Itoh | b532402 | 2011-07-19 07:27:20 +0000 | [diff] [blame] | 3101 | if (!block_rsv) |
| 3102 | return -ENOMEM; |
Josef Bacik | 7b12876 | 2008-07-24 12:17:14 -0400 | [diff] [blame] | 3103 | } |
| 3104 | |
Yan, Zheng | d68fc57 | 2010-05-16 10:49:58 -0400 | [diff] [blame] | 3105 | spin_lock(&root->orphan_lock); |
| 3106 | if (!root->orphan_block_rsv) { |
| 3107 | root->orphan_block_rsv = block_rsv; |
| 3108 | } else if (block_rsv) { |
| 3109 | btrfs_free_block_rsv(root, block_rsv); |
| 3110 | block_rsv = NULL; |
| 3111 | } |
Josef Bacik | 7b12876 | 2008-07-24 12:17:14 -0400 | [diff] [blame] | 3112 | |
Josef Bacik | 8a35d95 | 2012-05-23 14:26:42 -0400 | [diff] [blame] | 3113 | if (!test_and_set_bit(BTRFS_INODE_HAS_ORPHAN_ITEM, |
| 3114 | &BTRFS_I(inode)->runtime_flags)) { |
Yan, Zheng | d68fc57 | 2010-05-16 10:49:58 -0400 | [diff] [blame] | 3115 | #if 0 |
| 3116 | /* |
| 3117 | * For proper ENOSPC handling, we should do orphan |
| 3118 | * cleanup when mounting. But this introduces backward |
| 3119 | * compatibility issue. |
| 3120 | */ |
| 3121 | if (!xchg(&root->orphan_item_inserted, 1)) |
| 3122 | insert = 2; |
| 3123 | else |
| 3124 | insert = 1; |
| 3125 | #endif |
| 3126 | insert = 1; |
Miao Xie | 321f0e7 | 2012-08-28 22:13:02 -0600 | [diff] [blame] | 3127 | atomic_inc(&root->orphan_inodes); |
Yan, Zheng | d68fc57 | 2010-05-16 10:49:58 -0400 | [diff] [blame] | 3128 | } |
Josef Bacik | 7b12876 | 2008-07-24 12:17:14 -0400 | [diff] [blame] | 3129 | |
Josef Bacik | 72ac3c0 | 2012-05-23 14:13:11 -0400 | [diff] [blame] | 3130 | if (!test_and_set_bit(BTRFS_INODE_ORPHAN_META_RESERVED, |
| 3131 | &BTRFS_I(inode)->runtime_flags)) |
Yan, Zheng | d68fc57 | 2010-05-16 10:49:58 -0400 | [diff] [blame] | 3132 | reserve = 1; |
Yan, Zheng | d68fc57 | 2010-05-16 10:49:58 -0400 | [diff] [blame] | 3133 | spin_unlock(&root->orphan_lock); |
Josef Bacik | 7b12876 | 2008-07-24 12:17:14 -0400 | [diff] [blame] | 3134 | |
Yan, Zheng | d68fc57 | 2010-05-16 10:49:58 -0400 | [diff] [blame] | 3135 | /* grab metadata reservation from transaction handle */ |
| 3136 | if (reserve) { |
| 3137 | ret = btrfs_orphan_reserve_metadata(trans, inode); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 3138 | BUG_ON(ret); /* -ENOSPC in reservation; Logic error? JDM */ |
Yan, Zheng | d68fc57 | 2010-05-16 10:49:58 -0400 | [diff] [blame] | 3139 | } |
| 3140 | |
| 3141 | /* insert an orphan item to track this unlinked/truncated file */ |
| 3142 | if (insert >= 1) { |
Li Zefan | 33345d01 | 2011-04-20 10:31:50 +0800 | [diff] [blame] | 3143 | ret = btrfs_insert_orphan_item(trans, root, btrfs_ino(inode)); |
Josef Bacik | 4ef31a4 | 2013-08-13 14:10:08 -0400 | [diff] [blame] | 3144 | if (ret) { |
Filipe David Borba Manana | 703c88e | 2013-09-22 21:54:55 +0100 | [diff] [blame] | 3145 | atomic_dec(&root->orphan_inodes); |
Josef Bacik | 4ef31a4 | 2013-08-13 14:10:08 -0400 | [diff] [blame] | 3146 | if (reserve) { |
| 3147 | clear_bit(BTRFS_INODE_ORPHAN_META_RESERVED, |
| 3148 | &BTRFS_I(inode)->runtime_flags); |
| 3149 | btrfs_orphan_release_metadata(inode); |
| 3150 | } |
| 3151 | if (ret != -EEXIST) { |
Josef Bacik | e8e7cff | 2013-08-21 15:54:00 -0400 | [diff] [blame] | 3152 | clear_bit(BTRFS_INODE_HAS_ORPHAN_ITEM, |
| 3153 | &BTRFS_I(inode)->runtime_flags); |
Josef Bacik | 4ef31a4 | 2013-08-13 14:10:08 -0400 | [diff] [blame] | 3154 | btrfs_abort_transaction(trans, root, ret); |
| 3155 | return ret; |
| 3156 | } |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 3157 | } |
| 3158 | ret = 0; |
Yan, Zheng | d68fc57 | 2010-05-16 10:49:58 -0400 | [diff] [blame] | 3159 | } |
| 3160 | |
| 3161 | /* insert an orphan item to track subvolume contains orphan files */ |
| 3162 | if (insert >= 2) { |
| 3163 | ret = btrfs_insert_orphan_item(trans, root->fs_info->tree_root, |
| 3164 | root->root_key.objectid); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 3165 | if (ret && ret != -EEXIST) { |
| 3166 | btrfs_abort_transaction(trans, root, ret); |
| 3167 | return ret; |
| 3168 | } |
Yan, Zheng | d68fc57 | 2010-05-16 10:49:58 -0400 | [diff] [blame] | 3169 | } |
| 3170 | return 0; |
Josef Bacik | 7b12876 | 2008-07-24 12:17:14 -0400 | [diff] [blame] | 3171 | } |
| 3172 | |
| 3173 | /* |
| 3174 | * We have done the truncate/delete so we can go ahead and remove the orphan |
| 3175 | * item for this particular inode. |
| 3176 | */ |
Eric Sandeen | 48a3b63 | 2013-04-25 20:41:01 +0000 | [diff] [blame] | 3177 | static int btrfs_orphan_del(struct btrfs_trans_handle *trans, |
| 3178 | struct inode *inode) |
Josef Bacik | 7b12876 | 2008-07-24 12:17:14 -0400 | [diff] [blame] | 3179 | { |
| 3180 | struct btrfs_root *root = BTRFS_I(inode)->root; |
Yan, Zheng | d68fc57 | 2010-05-16 10:49:58 -0400 | [diff] [blame] | 3181 | int delete_item = 0; |
| 3182 | int release_rsv = 0; |
Josef Bacik | 7b12876 | 2008-07-24 12:17:14 -0400 | [diff] [blame] | 3183 | int ret = 0; |
| 3184 | |
Yan, Zheng | d68fc57 | 2010-05-16 10:49:58 -0400 | [diff] [blame] | 3185 | spin_lock(&root->orphan_lock); |
Josef Bacik | 8a35d95 | 2012-05-23 14:26:42 -0400 | [diff] [blame] | 3186 | if (test_and_clear_bit(BTRFS_INODE_HAS_ORPHAN_ITEM, |
| 3187 | &BTRFS_I(inode)->runtime_flags)) |
Yan, Zheng | d68fc57 | 2010-05-16 10:49:58 -0400 | [diff] [blame] | 3188 | delete_item = 1; |
Josef Bacik | 7b12876 | 2008-07-24 12:17:14 -0400 | [diff] [blame] | 3189 | |
Josef Bacik | 72ac3c0 | 2012-05-23 14:13:11 -0400 | [diff] [blame] | 3190 | if (test_and_clear_bit(BTRFS_INODE_ORPHAN_META_RESERVED, |
| 3191 | &BTRFS_I(inode)->runtime_flags)) |
Yan, Zheng | d68fc57 | 2010-05-16 10:49:58 -0400 | [diff] [blame] | 3192 | release_rsv = 1; |
Yan, Zheng | d68fc57 | 2010-05-16 10:49:58 -0400 | [diff] [blame] | 3193 | spin_unlock(&root->orphan_lock); |
| 3194 | |
Filipe David Borba Manana | 703c88e | 2013-09-22 21:54:55 +0100 | [diff] [blame] | 3195 | if (delete_item) { |
Josef Bacik | 8a35d95 | 2012-05-23 14:26:42 -0400 | [diff] [blame] | 3196 | atomic_dec(&root->orphan_inodes); |
Filipe David Borba Manana | 703c88e | 2013-09-22 21:54:55 +0100 | [diff] [blame] | 3197 | if (trans) |
| 3198 | ret = btrfs_del_orphan_item(trans, root, |
| 3199 | btrfs_ino(inode)); |
Josef Bacik | 8a35d95 | 2012-05-23 14:26:42 -0400 | [diff] [blame] | 3200 | } |
Josef Bacik | 7b12876 | 2008-07-24 12:17:14 -0400 | [diff] [blame] | 3201 | |
Filipe David Borba Manana | 703c88e | 2013-09-22 21:54:55 +0100 | [diff] [blame] | 3202 | if (release_rsv) |
| 3203 | btrfs_orphan_release_metadata(inode); |
| 3204 | |
Josef Bacik | 4ef31a4 | 2013-08-13 14:10:08 -0400 | [diff] [blame] | 3205 | return ret; |
Josef Bacik | 7b12876 | 2008-07-24 12:17:14 -0400 | [diff] [blame] | 3206 | } |
| 3207 | |
| 3208 | /* |
| 3209 | * this cleans up any orphans that may be left on the list from the last use |
| 3210 | * of this root. |
| 3211 | */ |
Josef Bacik | 66b4ffd | 2011-01-31 16:22:42 -0500 | [diff] [blame] | 3212 | int btrfs_orphan_cleanup(struct btrfs_root *root) |
Josef Bacik | 7b12876 | 2008-07-24 12:17:14 -0400 | [diff] [blame] | 3213 | { |
| 3214 | struct btrfs_path *path; |
| 3215 | struct extent_buffer *leaf; |
Josef Bacik | 7b12876 | 2008-07-24 12:17:14 -0400 | [diff] [blame] | 3216 | struct btrfs_key key, found_key; |
| 3217 | struct btrfs_trans_handle *trans; |
| 3218 | struct inode *inode; |
Josef Bacik | 8f6d7f4 | 2011-09-26 15:55:20 -0400 | [diff] [blame] | 3219 | u64 last_objectid = 0; |
Josef Bacik | 7b12876 | 2008-07-24 12:17:14 -0400 | [diff] [blame] | 3220 | int ret = 0, nr_unlink = 0, nr_truncate = 0; |
| 3221 | |
Yan, Zheng | d68fc57 | 2010-05-16 10:49:58 -0400 | [diff] [blame] | 3222 | if (cmpxchg(&root->orphan_cleanup_state, 0, ORPHAN_CLEANUP_STARTED)) |
Josef Bacik | 66b4ffd | 2011-01-31 16:22:42 -0500 | [diff] [blame] | 3223 | return 0; |
Yan, Zheng | c71bf09 | 2009-11-12 09:34:40 +0000 | [diff] [blame] | 3224 | |
| 3225 | path = btrfs_alloc_path(); |
Josef Bacik | 66b4ffd | 2011-01-31 16:22:42 -0500 | [diff] [blame] | 3226 | if (!path) { |
| 3227 | ret = -ENOMEM; |
| 3228 | goto out; |
| 3229 | } |
Josef Bacik | 7b12876 | 2008-07-24 12:17:14 -0400 | [diff] [blame] | 3230 | path->reada = -1; |
| 3231 | |
| 3232 | key.objectid = BTRFS_ORPHAN_OBJECTID; |
David Sterba | 962a298 | 2014-06-04 18:41:45 +0200 | [diff] [blame] | 3233 | key.type = BTRFS_ORPHAN_ITEM_KEY; |
Josef Bacik | 7b12876 | 2008-07-24 12:17:14 -0400 | [diff] [blame] | 3234 | key.offset = (u64)-1; |
| 3235 | |
Josef Bacik | 7b12876 | 2008-07-24 12:17:14 -0400 | [diff] [blame] | 3236 | while (1) { |
| 3237 | ret = btrfs_search_slot(NULL, root, &key, path, 0, 0); |
Josef Bacik | 66b4ffd | 2011-01-31 16:22:42 -0500 | [diff] [blame] | 3238 | if (ret < 0) |
| 3239 | goto out; |
Josef Bacik | 7b12876 | 2008-07-24 12:17:14 -0400 | [diff] [blame] | 3240 | |
| 3241 | /* |
| 3242 | * 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] | 3243 | * 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] | 3244 | * find the key and see if we have stuff that matches |
| 3245 | */ |
| 3246 | if (ret > 0) { |
Josef Bacik | 66b4ffd | 2011-01-31 16:22:42 -0500 | [diff] [blame] | 3247 | ret = 0; |
Josef Bacik | 7b12876 | 2008-07-24 12:17:14 -0400 | [diff] [blame] | 3248 | if (path->slots[0] == 0) |
| 3249 | break; |
| 3250 | path->slots[0]--; |
| 3251 | } |
| 3252 | |
| 3253 | /* pull out the item */ |
| 3254 | leaf = path->nodes[0]; |
Josef Bacik | 7b12876 | 2008-07-24 12:17:14 -0400 | [diff] [blame] | 3255 | btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]); |
| 3256 | |
| 3257 | /* make sure the item matches what we want */ |
| 3258 | if (found_key.objectid != BTRFS_ORPHAN_OBJECTID) |
| 3259 | break; |
David Sterba | 962a298 | 2014-06-04 18:41:45 +0200 | [diff] [blame] | 3260 | if (found_key.type != BTRFS_ORPHAN_ITEM_KEY) |
Josef Bacik | 7b12876 | 2008-07-24 12:17:14 -0400 | [diff] [blame] | 3261 | break; |
| 3262 | |
| 3263 | /* release the path since we're done with it */ |
David Sterba | b3b4aa7 | 2011-04-21 01:20:15 +0200 | [diff] [blame] | 3264 | btrfs_release_path(path); |
Josef Bacik | 7b12876 | 2008-07-24 12:17:14 -0400 | [diff] [blame] | 3265 | |
| 3266 | /* |
| 3267 | * this is where we are basically btrfs_lookup, without the |
| 3268 | * crossing root thing. we store the inode number in the |
| 3269 | * offset of the orphan item. |
| 3270 | */ |
Josef Bacik | 8f6d7f4 | 2011-09-26 15:55:20 -0400 | [diff] [blame] | 3271 | |
| 3272 | if (found_key.offset == last_objectid) { |
Simon Kirby | c2cf52e | 2013-03-19 22:41:23 +0000 | [diff] [blame] | 3273 | btrfs_err(root->fs_info, |
| 3274 | "Error removing orphan entry, stopping orphan cleanup"); |
Josef Bacik | 8f6d7f4 | 2011-09-26 15:55:20 -0400 | [diff] [blame] | 3275 | ret = -EINVAL; |
| 3276 | goto out; |
| 3277 | } |
| 3278 | |
| 3279 | last_objectid = found_key.offset; |
| 3280 | |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 3281 | found_key.objectid = found_key.offset; |
| 3282 | found_key.type = BTRFS_INODE_ITEM_KEY; |
| 3283 | found_key.offset = 0; |
Josef Bacik | 73f7341 | 2009-12-04 17:38:27 +0000 | [diff] [blame] | 3284 | inode = btrfs_iget(root->fs_info->sb, &found_key, root, NULL); |
Rusty Russell | 8c6ffba | 2013-07-15 11:20:32 +0930 | [diff] [blame] | 3285 | ret = PTR_ERR_OR_ZERO(inode); |
Josef Bacik | a8c9e57 | 2011-09-21 16:55:59 -0400 | [diff] [blame] | 3286 | if (ret && ret != -ESTALE) |
Josef Bacik | 66b4ffd | 2011-01-31 16:22:42 -0500 | [diff] [blame] | 3287 | goto out; |
Josef Bacik | a8c9e57 | 2011-09-21 16:55:59 -0400 | [diff] [blame] | 3288 | |
Arne Jansen | f8e9e0b | 2011-12-14 20:12:02 -0500 | [diff] [blame] | 3289 | if (ret == -ESTALE && root == root->fs_info->tree_root) { |
| 3290 | struct btrfs_root *dead_root; |
| 3291 | struct btrfs_fs_info *fs_info = root->fs_info; |
| 3292 | int is_dead_root = 0; |
| 3293 | |
| 3294 | /* |
| 3295 | * this is an orphan in the tree root. Currently these |
| 3296 | * could come from 2 sources: |
| 3297 | * a) a snapshot deletion in progress |
| 3298 | * b) a free space cache inode |
| 3299 | * We need to distinguish those two, as the snapshot |
| 3300 | * orphan must not get deleted. |
| 3301 | * find_dead_roots already ran before us, so if this |
| 3302 | * is a snapshot deletion, we should find the root |
| 3303 | * in the dead_roots list |
| 3304 | */ |
| 3305 | spin_lock(&fs_info->trans_lock); |
| 3306 | list_for_each_entry(dead_root, &fs_info->dead_roots, |
| 3307 | root_list) { |
| 3308 | if (dead_root->root_key.objectid == |
| 3309 | found_key.objectid) { |
| 3310 | is_dead_root = 1; |
| 3311 | break; |
| 3312 | } |
| 3313 | } |
| 3314 | spin_unlock(&fs_info->trans_lock); |
| 3315 | if (is_dead_root) { |
| 3316 | /* prevent this orphan from being found again */ |
| 3317 | key.offset = found_key.objectid - 1; |
| 3318 | continue; |
| 3319 | } |
| 3320 | } |
Josef Bacik | a8c9e57 | 2011-09-21 16:55:59 -0400 | [diff] [blame] | 3321 | /* |
| 3322 | * Inode is already gone but the orphan item is still there, |
| 3323 | * kill the orphan item. |
| 3324 | */ |
| 3325 | if (ret == -ESTALE) { |
| 3326 | trans = btrfs_start_transaction(root, 1); |
| 3327 | if (IS_ERR(trans)) { |
| 3328 | ret = PTR_ERR(trans); |
| 3329 | goto out; |
| 3330 | } |
Simon Kirby | c2cf52e | 2013-03-19 22:41:23 +0000 | [diff] [blame] | 3331 | btrfs_debug(root->fs_info, "auto deleting %Lu", |
| 3332 | found_key.objectid); |
Josef Bacik | a8c9e57 | 2011-09-21 16:55:59 -0400 | [diff] [blame] | 3333 | ret = btrfs_del_orphan_item(trans, root, |
| 3334 | found_key.objectid); |
Josef Bacik | a8c9e57 | 2011-09-21 16:55:59 -0400 | [diff] [blame] | 3335 | btrfs_end_transaction(trans, root); |
Josef Bacik | 4ef31a4 | 2013-08-13 14:10:08 -0400 | [diff] [blame] | 3336 | if (ret) |
| 3337 | goto out; |
Josef Bacik | a8c9e57 | 2011-09-21 16:55:59 -0400 | [diff] [blame] | 3338 | continue; |
Josef Bacik | 66b4ffd | 2011-01-31 16:22:42 -0500 | [diff] [blame] | 3339 | } |
Josef Bacik | 7b12876 | 2008-07-24 12:17:14 -0400 | [diff] [blame] | 3340 | |
Josef Bacik | 7b12876 | 2008-07-24 12:17:14 -0400 | [diff] [blame] | 3341 | /* |
| 3342 | * add this inode to the orphan list so btrfs_orphan_del does |
| 3343 | * the proper thing when we hit it |
| 3344 | */ |
Josef Bacik | 8a35d95 | 2012-05-23 14:26:42 -0400 | [diff] [blame] | 3345 | set_bit(BTRFS_INODE_HAS_ORPHAN_ITEM, |
| 3346 | &BTRFS_I(inode)->runtime_flags); |
Josef Bacik | 925396e | 2013-02-01 15:57:47 -0500 | [diff] [blame] | 3347 | atomic_inc(&root->orphan_inodes); |
Josef Bacik | 7b12876 | 2008-07-24 12:17:14 -0400 | [diff] [blame] | 3348 | |
Josef Bacik | 7b12876 | 2008-07-24 12:17:14 -0400 | [diff] [blame] | 3349 | /* if we have links, this was a truncate, lets do that */ |
| 3350 | if (inode->i_nlink) { |
Dulshani Gunawardhana | fae7f21 | 2013-10-31 10:30:08 +0530 | [diff] [blame] | 3351 | if (WARN_ON(!S_ISREG(inode->i_mode))) { |
Josef Bacik | a41ad39 | 2011-01-31 15:30:16 -0500 | [diff] [blame] | 3352 | iput(inode); |
| 3353 | continue; |
| 3354 | } |
Josef Bacik | 7b12876 | 2008-07-24 12:17:14 -0400 | [diff] [blame] | 3355 | nr_truncate++; |
Josef Bacik | f3fe820 | 2013-01-07 17:03:21 -0500 | [diff] [blame] | 3356 | |
| 3357 | /* 1 for the orphan item deletion. */ |
| 3358 | trans = btrfs_start_transaction(root, 1); |
| 3359 | if (IS_ERR(trans)) { |
Josef Bacik | c69b26b | 2013-06-03 16:51:23 -0400 | [diff] [blame] | 3360 | iput(inode); |
Josef Bacik | f3fe820 | 2013-01-07 17:03:21 -0500 | [diff] [blame] | 3361 | ret = PTR_ERR(trans); |
| 3362 | goto out; |
| 3363 | } |
| 3364 | ret = btrfs_orphan_add(trans, inode); |
| 3365 | btrfs_end_transaction(trans, root); |
Josef Bacik | c69b26b | 2013-06-03 16:51:23 -0400 | [diff] [blame] | 3366 | if (ret) { |
| 3367 | iput(inode); |
Josef Bacik | f3fe820 | 2013-01-07 17:03:21 -0500 | [diff] [blame] | 3368 | goto out; |
Josef Bacik | c69b26b | 2013-06-03 16:51:23 -0400 | [diff] [blame] | 3369 | } |
Josef Bacik | f3fe820 | 2013-01-07 17:03:21 -0500 | [diff] [blame] | 3370 | |
Josef Bacik | 66b4ffd | 2011-01-31 16:22:42 -0500 | [diff] [blame] | 3371 | ret = btrfs_truncate(inode); |
Josef Bacik | 4a7d0f6 | 2013-02-07 16:27:28 -0500 | [diff] [blame] | 3372 | if (ret) |
| 3373 | btrfs_orphan_del(NULL, inode); |
Josef Bacik | 7b12876 | 2008-07-24 12:17:14 -0400 | [diff] [blame] | 3374 | } else { |
| 3375 | nr_unlink++; |
| 3376 | } |
| 3377 | |
| 3378 | /* this will do delete_inode and everything for us */ |
| 3379 | iput(inode); |
Josef Bacik | 66b4ffd | 2011-01-31 16:22:42 -0500 | [diff] [blame] | 3380 | if (ret) |
| 3381 | goto out; |
Josef Bacik | 7b12876 | 2008-07-24 12:17:14 -0400 | [diff] [blame] | 3382 | } |
Miao Xie | 3254c87 | 2011-11-10 20:45:05 -0500 | [diff] [blame] | 3383 | /* release the path since we're done with it */ |
| 3384 | btrfs_release_path(path); |
| 3385 | |
Yan, Zheng | d68fc57 | 2010-05-16 10:49:58 -0400 | [diff] [blame] | 3386 | root->orphan_cleanup_state = ORPHAN_CLEANUP_DONE; |
| 3387 | |
| 3388 | if (root->orphan_block_rsv) |
| 3389 | btrfs_block_rsv_release(root, root->orphan_block_rsv, |
| 3390 | (u64)-1); |
| 3391 | |
Miao Xie | 27cdeb7 | 2014-04-02 19:51:05 +0800 | [diff] [blame] | 3392 | if (root->orphan_block_rsv || |
| 3393 | test_bit(BTRFS_ROOT_ORPHAN_ITEM_INSERTED, &root->state)) { |
Josef Bacik | 7a7eaa4 | 2011-04-13 12:54:33 -0400 | [diff] [blame] | 3394 | trans = btrfs_join_transaction(root); |
Josef Bacik | 66b4ffd | 2011-01-31 16:22:42 -0500 | [diff] [blame] | 3395 | if (!IS_ERR(trans)) |
| 3396 | btrfs_end_transaction(trans, root); |
Yan, Zheng | d68fc57 | 2010-05-16 10:49:58 -0400 | [diff] [blame] | 3397 | } |
Josef Bacik | 7b12876 | 2008-07-24 12:17:14 -0400 | [diff] [blame] | 3398 | |
| 3399 | if (nr_unlink) |
David Sterba | 4884b47 | 2013-03-20 13:31:27 +0000 | [diff] [blame] | 3400 | btrfs_debug(root->fs_info, "unlinked %d orphans", nr_unlink); |
Josef Bacik | 7b12876 | 2008-07-24 12:17:14 -0400 | [diff] [blame] | 3401 | if (nr_truncate) |
David Sterba | 4884b47 | 2013-03-20 13:31:27 +0000 | [diff] [blame] | 3402 | btrfs_debug(root->fs_info, "truncated %d orphans", nr_truncate); |
Josef Bacik | 66b4ffd | 2011-01-31 16:22:42 -0500 | [diff] [blame] | 3403 | |
| 3404 | out: |
| 3405 | if (ret) |
Simon Kirby | c2cf52e | 2013-03-19 22:41:23 +0000 | [diff] [blame] | 3406 | btrfs_crit(root->fs_info, |
| 3407 | "could not do orphan cleanup %d", ret); |
Josef Bacik | 66b4ffd | 2011-01-31 16:22:42 -0500 | [diff] [blame] | 3408 | btrfs_free_path(path); |
| 3409 | return ret; |
Josef Bacik | 7b12876 | 2008-07-24 12:17:14 -0400 | [diff] [blame] | 3410 | } |
| 3411 | |
Chris Mason | d352ac6 | 2008-09-29 15:18:18 -0400 | [diff] [blame] | 3412 | /* |
Chris Mason | 46a53cc | 2009-04-27 11:47:50 -0400 | [diff] [blame] | 3413 | * very simple check to peek ahead in the leaf looking for xattrs. If we |
| 3414 | * don't find any xattrs, we know there can't be any acls. |
| 3415 | * |
| 3416 | * slot is the slot the inode is in, objectid is the objectid of the inode |
| 3417 | */ |
| 3418 | 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] | 3419 | int slot, u64 objectid, |
| 3420 | int *first_xattr_slot) |
Chris Mason | 46a53cc | 2009-04-27 11:47:50 -0400 | [diff] [blame] | 3421 | { |
| 3422 | u32 nritems = btrfs_header_nritems(leaf); |
| 3423 | struct btrfs_key found_key; |
Josef Bacik | f23b5a5 | 2013-06-19 10:16:26 -0400 | [diff] [blame] | 3424 | static u64 xattr_access = 0; |
| 3425 | static u64 xattr_default = 0; |
Chris Mason | 46a53cc | 2009-04-27 11:47:50 -0400 | [diff] [blame] | 3426 | int scanned = 0; |
| 3427 | |
Josef Bacik | f23b5a5 | 2013-06-19 10:16:26 -0400 | [diff] [blame] | 3428 | if (!xattr_access) { |
| 3429 | xattr_access = btrfs_name_hash(POSIX_ACL_XATTR_ACCESS, |
| 3430 | strlen(POSIX_ACL_XATTR_ACCESS)); |
| 3431 | xattr_default = btrfs_name_hash(POSIX_ACL_XATTR_DEFAULT, |
| 3432 | strlen(POSIX_ACL_XATTR_DEFAULT)); |
| 3433 | } |
| 3434 | |
Chris Mason | 46a53cc | 2009-04-27 11:47:50 -0400 | [diff] [blame] | 3435 | slot++; |
Filipe David Borba Manana | 6354192 | 2014-01-07 11:47:46 +0000 | [diff] [blame] | 3436 | *first_xattr_slot = -1; |
Chris Mason | 46a53cc | 2009-04-27 11:47:50 -0400 | [diff] [blame] | 3437 | while (slot < nritems) { |
| 3438 | btrfs_item_key_to_cpu(leaf, &found_key, slot); |
| 3439 | |
| 3440 | /* we found a different objectid, there must not be acls */ |
| 3441 | if (found_key.objectid != objectid) |
| 3442 | return 0; |
| 3443 | |
| 3444 | /* we found an xattr, assume we've got an acl */ |
Josef Bacik | f23b5a5 | 2013-06-19 10:16:26 -0400 | [diff] [blame] | 3445 | if (found_key.type == BTRFS_XATTR_ITEM_KEY) { |
Filipe David Borba Manana | 6354192 | 2014-01-07 11:47:46 +0000 | [diff] [blame] | 3446 | if (*first_xattr_slot == -1) |
| 3447 | *first_xattr_slot = slot; |
Josef Bacik | f23b5a5 | 2013-06-19 10:16:26 -0400 | [diff] [blame] | 3448 | if (found_key.offset == xattr_access || |
| 3449 | found_key.offset == xattr_default) |
| 3450 | return 1; |
| 3451 | } |
Chris Mason | 46a53cc | 2009-04-27 11:47:50 -0400 | [diff] [blame] | 3452 | |
| 3453 | /* |
| 3454 | * we found a key greater than an xattr key, there can't |
| 3455 | * be any acls later on |
| 3456 | */ |
| 3457 | if (found_key.type > BTRFS_XATTR_ITEM_KEY) |
| 3458 | return 0; |
| 3459 | |
| 3460 | slot++; |
| 3461 | scanned++; |
| 3462 | |
| 3463 | /* |
| 3464 | * it goes inode, inode backrefs, xattrs, extents, |
| 3465 | * so if there are a ton of hard links to an inode there can |
| 3466 | * be a lot of backrefs. Don't waste time searching too hard, |
| 3467 | * this is just an optimization |
| 3468 | */ |
| 3469 | if (scanned >= 8) |
| 3470 | break; |
| 3471 | } |
| 3472 | /* we hit the end of the leaf before we found an xattr or |
| 3473 | * something larger than an xattr. We have to assume the inode |
| 3474 | * has acls |
| 3475 | */ |
Filipe David Borba Manana | 6354192 | 2014-01-07 11:47:46 +0000 | [diff] [blame] | 3476 | if (*first_xattr_slot == -1) |
| 3477 | *first_xattr_slot = slot; |
Chris Mason | 46a53cc | 2009-04-27 11:47:50 -0400 | [diff] [blame] | 3478 | return 1; |
| 3479 | } |
| 3480 | |
| 3481 | /* |
Chris Mason | d352ac6 | 2008-09-29 15:18:18 -0400 | [diff] [blame] | 3482 | * read an inode from the btree into the in-memory inode |
| 3483 | */ |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 3484 | static void btrfs_read_locked_inode(struct inode *inode) |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3485 | { |
| 3486 | struct btrfs_path *path; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 3487 | struct extent_buffer *leaf; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3488 | struct btrfs_inode_item *inode_item; |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 3489 | struct btrfs_timespec *tspec; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3490 | struct btrfs_root *root = BTRFS_I(inode)->root; |
| 3491 | struct btrfs_key location; |
Miao Xie | 67de117 | 2013-12-26 13:07:06 +0800 | [diff] [blame] | 3492 | unsigned long ptr; |
Chris Mason | 46a53cc | 2009-04-27 11:47:50 -0400 | [diff] [blame] | 3493 | int maybe_acls; |
Josef Bacik | 618e21d | 2007-07-11 10:18:17 -0400 | [diff] [blame] | 3494 | u32 rdev; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3495 | int ret; |
Miao Xie | 2f7e33d | 2011-06-23 07:27:13 +0000 | [diff] [blame] | 3496 | bool filled = false; |
Filipe David Borba Manana | 6354192 | 2014-01-07 11:47:46 +0000 | [diff] [blame] | 3497 | int first_xattr_slot; |
Miao Xie | 2f7e33d | 2011-06-23 07:27:13 +0000 | [diff] [blame] | 3498 | |
| 3499 | ret = btrfs_fill_inode(inode, &rdev); |
| 3500 | if (!ret) |
| 3501 | filled = true; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3502 | |
| 3503 | path = btrfs_alloc_path(); |
Mark Fasheh | 1748f84 | 2011-07-12 11:25:31 -0700 | [diff] [blame] | 3504 | if (!path) |
| 3505 | goto make_bad; |
| 3506 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3507 | memcpy(&location, &BTRFS_I(inode)->location, sizeof(location)); |
Chris Mason | dc17ff8 | 2008-01-08 15:46:30 -0500 | [diff] [blame] | 3508 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3509 | ret = btrfs_lookup_inode(NULL, root, path, &location, 0); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 3510 | if (ret) |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3511 | goto make_bad; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3512 | |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 3513 | leaf = path->nodes[0]; |
Miao Xie | 2f7e33d | 2011-06-23 07:27:13 +0000 | [diff] [blame] | 3514 | |
| 3515 | if (filled) |
Miao Xie | 67de117 | 2013-12-26 13:07:06 +0800 | [diff] [blame] | 3516 | goto cache_index; |
Miao Xie | 2f7e33d | 2011-06-23 07:27:13 +0000 | [diff] [blame] | 3517 | |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 3518 | inode_item = btrfs_item_ptr(leaf, path->slots[0], |
| 3519 | struct btrfs_inode_item); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 3520 | inode->i_mode = btrfs_inode_mode(leaf, inode_item); |
Miklos Szeredi | bfe8684 | 2011-10-28 14:13:29 +0200 | [diff] [blame] | 3521 | set_nlink(inode, btrfs_inode_nlink(leaf, inode_item)); |
Eric W. Biederman | 2f2f43d | 2012-02-10 11:05:07 -0800 | [diff] [blame] | 3522 | i_uid_write(inode, btrfs_inode_uid(leaf, inode_item)); |
| 3523 | i_gid_write(inode, btrfs_inode_gid(leaf, inode_item)); |
Chris Mason | dbe674a | 2008-07-17 12:54:05 -0400 | [diff] [blame] | 3524 | btrfs_i_size_write(inode, btrfs_inode_size(leaf, inode_item)); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 3525 | |
| 3526 | tspec = btrfs_inode_atime(inode_item); |
| 3527 | inode->i_atime.tv_sec = btrfs_timespec_sec(leaf, tspec); |
| 3528 | inode->i_atime.tv_nsec = btrfs_timespec_nsec(leaf, tspec); |
| 3529 | |
| 3530 | tspec = btrfs_inode_mtime(inode_item); |
| 3531 | inode->i_mtime.tv_sec = btrfs_timespec_sec(leaf, tspec); |
| 3532 | inode->i_mtime.tv_nsec = btrfs_timespec_nsec(leaf, tspec); |
| 3533 | |
| 3534 | tspec = btrfs_inode_ctime(inode_item); |
| 3535 | inode->i_ctime.tv_sec = btrfs_timespec_sec(leaf, tspec); |
| 3536 | inode->i_ctime.tv_nsec = btrfs_timespec_nsec(leaf, tspec); |
| 3537 | |
Yan Zheng | a76a3cd | 2008-10-09 11:46:29 -0400 | [diff] [blame] | 3538 | inode_set_bytes(inode, btrfs_inode_nbytes(leaf, inode_item)); |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 3539 | BTRFS_I(inode)->generation = btrfs_inode_generation(leaf, inode_item); |
Josef Bacik | 5dc562c | 2012-08-17 13:14:17 -0400 | [diff] [blame] | 3540 | BTRFS_I(inode)->last_trans = btrfs_inode_transid(leaf, inode_item); |
| 3541 | |
| 3542 | /* |
| 3543 | * If we were modified in the current generation and evicted from memory |
| 3544 | * and then re-read we need to do a full sync since we don't have any |
| 3545 | * idea about which extents were modified before we were evicted from |
| 3546 | * cache. |
| 3547 | */ |
| 3548 | if (BTRFS_I(inode)->last_trans == root->fs_info->generation) |
| 3549 | set_bit(BTRFS_INODE_NEEDS_FULL_SYNC, |
| 3550 | &BTRFS_I(inode)->runtime_flags); |
| 3551 | |
Josef Bacik | 0c4d2d9 | 2012-04-05 15:03:02 -0400 | [diff] [blame] | 3552 | inode->i_version = btrfs_inode_sequence(leaf, inode_item); |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 3553 | inode->i_generation = BTRFS_I(inode)->generation; |
Josef Bacik | 618e21d | 2007-07-11 10:18:17 -0400 | [diff] [blame] | 3554 | inode->i_rdev = 0; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 3555 | rdev = btrfs_inode_rdev(leaf, inode_item); |
| 3556 | |
Josef Bacik | aec7477 | 2008-07-24 12:12:38 -0400 | [diff] [blame] | 3557 | BTRFS_I(inode)->index_cnt = (u64)-1; |
Yan Zheng | d2fb343 | 2008-12-11 16:30:39 -0500 | [diff] [blame] | 3558 | BTRFS_I(inode)->flags = btrfs_inode_flags(leaf, inode_item); |
Miao Xie | 67de117 | 2013-12-26 13:07:06 +0800 | [diff] [blame] | 3559 | |
| 3560 | cache_index: |
| 3561 | path->slots[0]++; |
| 3562 | if (inode->i_nlink != 1 || |
| 3563 | path->slots[0] >= btrfs_header_nritems(leaf)) |
| 3564 | goto cache_acl; |
| 3565 | |
| 3566 | btrfs_item_key_to_cpu(leaf, &location, path->slots[0]); |
| 3567 | if (location.objectid != btrfs_ino(inode)) |
| 3568 | goto cache_acl; |
| 3569 | |
| 3570 | ptr = btrfs_item_ptr_offset(leaf, path->slots[0]); |
| 3571 | if (location.type == BTRFS_INODE_REF_KEY) { |
| 3572 | struct btrfs_inode_ref *ref; |
| 3573 | |
| 3574 | ref = (struct btrfs_inode_ref *)ptr; |
| 3575 | BTRFS_I(inode)->dir_index = btrfs_inode_ref_index(leaf, ref); |
| 3576 | } else if (location.type == BTRFS_INODE_EXTREF_KEY) { |
| 3577 | struct btrfs_inode_extref *extref; |
| 3578 | |
| 3579 | extref = (struct btrfs_inode_extref *)ptr; |
| 3580 | BTRFS_I(inode)->dir_index = btrfs_inode_extref_index(leaf, |
| 3581 | extref); |
| 3582 | } |
Miao Xie | 2f7e33d | 2011-06-23 07:27:13 +0000 | [diff] [blame] | 3583 | cache_acl: |
Chris Mason | 46a53cc | 2009-04-27 11:47:50 -0400 | [diff] [blame] | 3584 | /* |
| 3585 | * try to precache a NULL acl entry for files that don't have |
| 3586 | * any xattrs or acls |
| 3587 | */ |
Li Zefan | 33345d01 | 2011-04-20 10:31:50 +0800 | [diff] [blame] | 3588 | maybe_acls = acls_after_inode_item(leaf, path->slots[0], |
Filipe David Borba Manana | 6354192 | 2014-01-07 11:47:46 +0000 | [diff] [blame] | 3589 | btrfs_ino(inode), &first_xattr_slot); |
| 3590 | if (first_xattr_slot != -1) { |
| 3591 | path->slots[0] = first_xattr_slot; |
| 3592 | ret = btrfs_load_inode_props(inode, path); |
| 3593 | if (ret) |
| 3594 | btrfs_err(root->fs_info, |
David Sterba | 351fd35 | 2014-05-15 16:48:20 +0200 | [diff] [blame] | 3595 | "error loading props for ino %llu (root %llu): %d", |
Filipe David Borba Manana | 6354192 | 2014-01-07 11:47:46 +0000 | [diff] [blame] | 3596 | btrfs_ino(inode), |
| 3597 | root->root_key.objectid, ret); |
| 3598 | } |
| 3599 | btrfs_free_path(path); |
| 3600 | |
Al Viro | 72c0490 | 2009-06-24 16:58:48 -0400 | [diff] [blame] | 3601 | if (!maybe_acls) |
| 3602 | cache_no_acl(inode); |
Chris Mason | 46a53cc | 2009-04-27 11:47:50 -0400 | [diff] [blame] | 3603 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3604 | switch (inode->i_mode & S_IFMT) { |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3605 | case S_IFREG: |
| 3606 | inode->i_mapping->a_ops = &btrfs_aops; |
Chris Mason | 0416008 | 2008-03-26 10:28:07 -0400 | [diff] [blame] | 3607 | inode->i_mapping->backing_dev_info = &root->fs_info->bdi; |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 3608 | BTRFS_I(inode)->io_tree.ops = &btrfs_extent_io_ops; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3609 | inode->i_fop = &btrfs_file_operations; |
| 3610 | inode->i_op = &btrfs_file_inode_operations; |
| 3611 | break; |
| 3612 | case S_IFDIR: |
| 3613 | inode->i_fop = &btrfs_dir_file_operations; |
| 3614 | if (root == root->fs_info->tree_root) |
| 3615 | inode->i_op = &btrfs_dir_ro_inode_operations; |
| 3616 | else |
| 3617 | inode->i_op = &btrfs_dir_inode_operations; |
| 3618 | break; |
| 3619 | case S_IFLNK: |
| 3620 | inode->i_op = &btrfs_symlink_inode_operations; |
| 3621 | inode->i_mapping->a_ops = &btrfs_symlink_aops; |
Chris Mason | 0416008 | 2008-03-26 10:28:07 -0400 | [diff] [blame] | 3622 | inode->i_mapping->backing_dev_info = &root->fs_info->bdi; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3623 | break; |
Josef Bacik | 618e21d | 2007-07-11 10:18:17 -0400 | [diff] [blame] | 3624 | default: |
Jim Owens | 0279b4c | 2009-02-04 09:29:13 -0500 | [diff] [blame] | 3625 | inode->i_op = &btrfs_special_inode_operations; |
Josef Bacik | 618e21d | 2007-07-11 10:18:17 -0400 | [diff] [blame] | 3626 | init_special_inode(inode, inode->i_mode, rdev); |
| 3627 | break; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3628 | } |
Christoph Hellwig | 6cbff00 | 2009-04-17 10:37:41 +0200 | [diff] [blame] | 3629 | |
| 3630 | btrfs_update_iflags(inode); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3631 | return; |
| 3632 | |
| 3633 | make_bad: |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3634 | btrfs_free_path(path); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3635 | make_bad_inode(inode); |
| 3636 | } |
| 3637 | |
Chris Mason | d352ac6 | 2008-09-29 15:18:18 -0400 | [diff] [blame] | 3638 | /* |
| 3639 | * given a leaf and an inode, copy the inode fields into the leaf |
| 3640 | */ |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 3641 | static void fill_inode_item(struct btrfs_trans_handle *trans, |
| 3642 | struct extent_buffer *leaf, |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 3643 | struct btrfs_inode_item *item, |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3644 | struct inode *inode) |
| 3645 | { |
Liu Bo | 51fab69 | 2012-12-27 09:01:21 +0000 | [diff] [blame] | 3646 | struct btrfs_map_token token; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 3647 | |
Liu Bo | 51fab69 | 2012-12-27 09:01:21 +0000 | [diff] [blame] | 3648 | btrfs_init_map_token(&token); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 3649 | |
Liu Bo | 51fab69 | 2012-12-27 09:01:21 +0000 | [diff] [blame] | 3650 | btrfs_set_token_inode_uid(leaf, item, i_uid_read(inode), &token); |
| 3651 | btrfs_set_token_inode_gid(leaf, item, i_gid_read(inode), &token); |
| 3652 | btrfs_set_token_inode_size(leaf, item, BTRFS_I(inode)->disk_i_size, |
| 3653 | &token); |
| 3654 | btrfs_set_token_inode_mode(leaf, item, inode->i_mode, &token); |
| 3655 | btrfs_set_token_inode_nlink(leaf, item, inode->i_nlink, &token); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 3656 | |
Liu Bo | 51fab69 | 2012-12-27 09:01:21 +0000 | [diff] [blame] | 3657 | btrfs_set_token_timespec_sec(leaf, btrfs_inode_atime(item), |
| 3658 | inode->i_atime.tv_sec, &token); |
| 3659 | btrfs_set_token_timespec_nsec(leaf, btrfs_inode_atime(item), |
| 3660 | inode->i_atime.tv_nsec, &token); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 3661 | |
Liu Bo | 51fab69 | 2012-12-27 09:01:21 +0000 | [diff] [blame] | 3662 | btrfs_set_token_timespec_sec(leaf, btrfs_inode_mtime(item), |
| 3663 | inode->i_mtime.tv_sec, &token); |
| 3664 | btrfs_set_token_timespec_nsec(leaf, btrfs_inode_mtime(item), |
| 3665 | inode->i_mtime.tv_nsec, &token); |
| 3666 | |
| 3667 | btrfs_set_token_timespec_sec(leaf, btrfs_inode_ctime(item), |
| 3668 | inode->i_ctime.tv_sec, &token); |
| 3669 | btrfs_set_token_timespec_nsec(leaf, btrfs_inode_ctime(item), |
| 3670 | inode->i_ctime.tv_nsec, &token); |
| 3671 | |
| 3672 | btrfs_set_token_inode_nbytes(leaf, item, inode_get_bytes(inode), |
| 3673 | &token); |
| 3674 | btrfs_set_token_inode_generation(leaf, item, BTRFS_I(inode)->generation, |
| 3675 | &token); |
| 3676 | btrfs_set_token_inode_sequence(leaf, item, inode->i_version, &token); |
| 3677 | btrfs_set_token_inode_transid(leaf, item, trans->transid, &token); |
| 3678 | btrfs_set_token_inode_rdev(leaf, item, inode->i_rdev, &token); |
| 3679 | btrfs_set_token_inode_flags(leaf, item, BTRFS_I(inode)->flags, &token); |
| 3680 | btrfs_set_token_inode_block_group(leaf, item, 0, &token); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3681 | } |
| 3682 | |
Chris Mason | d352ac6 | 2008-09-29 15:18:18 -0400 | [diff] [blame] | 3683 | /* |
| 3684 | * copy everything in the in-memory inode into the btree. |
| 3685 | */ |
Chris Mason | 2115133 | 2011-11-10 20:39:08 -0500 | [diff] [blame] | 3686 | static noinline int btrfs_update_inode_item(struct btrfs_trans_handle *trans, |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 3687 | struct btrfs_root *root, struct inode *inode) |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3688 | { |
| 3689 | struct btrfs_inode_item *inode_item; |
| 3690 | struct btrfs_path *path; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 3691 | struct extent_buffer *leaf; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3692 | int ret; |
| 3693 | |
| 3694 | path = btrfs_alloc_path(); |
Miao Xie | 16cdcec | 2011-04-22 18:12:22 +0800 | [diff] [blame] | 3695 | if (!path) |
| 3696 | return -ENOMEM; |
| 3697 | |
Chris Mason | b947343 | 2009-03-13 11:00:37 -0400 | [diff] [blame] | 3698 | path->leave_spinning = 1; |
Miao Xie | 16cdcec | 2011-04-22 18:12:22 +0800 | [diff] [blame] | 3699 | ret = btrfs_lookup_inode(trans, root, path, &BTRFS_I(inode)->location, |
| 3700 | 1); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3701 | if (ret) { |
| 3702 | if (ret > 0) |
| 3703 | ret = -ENOENT; |
| 3704 | goto failed; |
| 3705 | } |
| 3706 | |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 3707 | leaf = path->nodes[0]; |
| 3708 | inode_item = btrfs_item_ptr(leaf, path->slots[0], |
Miao Xie | 16cdcec | 2011-04-22 18:12:22 +0800 | [diff] [blame] | 3709 | struct btrfs_inode_item); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3710 | |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 3711 | fill_inode_item(trans, leaf, inode_item, inode); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 3712 | btrfs_mark_buffer_dirty(leaf); |
Josef Bacik | 15ee9bc | 2007-08-10 16:22:09 -0400 | [diff] [blame] | 3713 | btrfs_set_inode_last_trans(trans, inode); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3714 | ret = 0; |
| 3715 | failed: |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3716 | btrfs_free_path(path); |
| 3717 | return ret; |
| 3718 | } |
| 3719 | |
Chris Mason | d352ac6 | 2008-09-29 15:18:18 -0400 | [diff] [blame] | 3720 | /* |
Chris Mason | 2115133 | 2011-11-10 20:39:08 -0500 | [diff] [blame] | 3721 | * copy everything in the in-memory inode into the btree. |
| 3722 | */ |
| 3723 | noinline int btrfs_update_inode(struct btrfs_trans_handle *trans, |
| 3724 | struct btrfs_root *root, struct inode *inode) |
| 3725 | { |
| 3726 | int ret; |
| 3727 | |
| 3728 | /* |
| 3729 | * If the inode is a free space inode, we can deadlock during commit |
| 3730 | * if we put it into the delayed code. |
| 3731 | * |
| 3732 | * The data relocation inode should also be directly updated |
| 3733 | * without delay |
| 3734 | */ |
Liu Bo | 83eea1f | 2012-07-10 05:28:39 -0600 | [diff] [blame] | 3735 | if (!btrfs_is_free_space_inode(inode) |
Josef Bacik | 1d52c78 | 2014-09-18 11:30:44 -0400 | [diff] [blame] | 3736 | && root->root_key.objectid != BTRFS_DATA_RELOC_TREE_OBJECTID |
| 3737 | && !root->fs_info->log_root_recovering) { |
Alexander Block | 8ea05e3 | 2012-07-25 17:35:53 +0200 | [diff] [blame] | 3738 | btrfs_update_root_times(trans, root); |
| 3739 | |
Chris Mason | 2115133 | 2011-11-10 20:39:08 -0500 | [diff] [blame] | 3740 | ret = btrfs_delayed_update_inode(trans, root, inode); |
| 3741 | if (!ret) |
| 3742 | btrfs_set_inode_last_trans(trans, inode); |
| 3743 | return ret; |
| 3744 | } |
| 3745 | |
| 3746 | return btrfs_update_inode_item(trans, root, inode); |
| 3747 | } |
| 3748 | |
Josef Bacik | be6aef6 | 2012-10-22 15:43:12 -0400 | [diff] [blame] | 3749 | noinline int btrfs_update_inode_fallback(struct btrfs_trans_handle *trans, |
| 3750 | struct btrfs_root *root, |
| 3751 | struct inode *inode) |
Chris Mason | 2115133 | 2011-11-10 20:39:08 -0500 | [diff] [blame] | 3752 | { |
| 3753 | int ret; |
| 3754 | |
| 3755 | ret = btrfs_update_inode(trans, root, inode); |
| 3756 | if (ret == -ENOSPC) |
| 3757 | return btrfs_update_inode_item(trans, root, inode); |
| 3758 | return ret; |
| 3759 | } |
| 3760 | |
| 3761 | /* |
Chris Mason | d352ac6 | 2008-09-29 15:18:18 -0400 | [diff] [blame] | 3762 | * unlink helper that gets used here in inode.c and in the tree logging |
| 3763 | * recovery code. It remove a link in a directory with a given name, and |
| 3764 | * also drops the back refs in the inode to the directory |
| 3765 | */ |
Al Viro | 9298679 | 2011-03-04 17:14:37 +0000 | [diff] [blame] | 3766 | static int __btrfs_unlink_inode(struct btrfs_trans_handle *trans, |
| 3767 | struct btrfs_root *root, |
| 3768 | struct inode *dir, struct inode *inode, |
| 3769 | const char *name, int name_len) |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3770 | { |
| 3771 | struct btrfs_path *path; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3772 | int ret = 0; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 3773 | struct extent_buffer *leaf; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3774 | struct btrfs_dir_item *di; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 3775 | struct btrfs_key key; |
Josef Bacik | aec7477 | 2008-07-24 12:12:38 -0400 | [diff] [blame] | 3776 | u64 index; |
Li Zefan | 33345d01 | 2011-04-20 10:31:50 +0800 | [diff] [blame] | 3777 | u64 ino = btrfs_ino(inode); |
| 3778 | u64 dir_ino = btrfs_ino(dir); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3779 | |
| 3780 | path = btrfs_alloc_path(); |
Chris Mason | 54aa1f4 | 2007-06-22 14:16:25 -0400 | [diff] [blame] | 3781 | if (!path) { |
| 3782 | ret = -ENOMEM; |
Tsutomu Itoh | 554233a | 2011-02-03 03:16:25 +0000 | [diff] [blame] | 3783 | goto out; |
Chris Mason | 54aa1f4 | 2007-06-22 14:16:25 -0400 | [diff] [blame] | 3784 | } |
| 3785 | |
Chris Mason | b947343 | 2009-03-13 11:00:37 -0400 | [diff] [blame] | 3786 | path->leave_spinning = 1; |
Li Zefan | 33345d01 | 2011-04-20 10:31:50 +0800 | [diff] [blame] | 3787 | di = btrfs_lookup_dir_item(trans, root, path, dir_ino, |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3788 | name, name_len, -1); |
| 3789 | if (IS_ERR(di)) { |
| 3790 | ret = PTR_ERR(di); |
| 3791 | goto err; |
| 3792 | } |
| 3793 | if (!di) { |
| 3794 | ret = -ENOENT; |
| 3795 | goto err; |
| 3796 | } |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 3797 | leaf = path->nodes[0]; |
| 3798 | btrfs_dir_item_key_to_cpu(leaf, di, &key); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3799 | ret = btrfs_delete_one_dir_name(trans, root, path, di); |
Chris Mason | 54aa1f4 | 2007-06-22 14:16:25 -0400 | [diff] [blame] | 3800 | if (ret) |
| 3801 | goto err; |
David Sterba | b3b4aa7 | 2011-04-21 01:20:15 +0200 | [diff] [blame] | 3802 | btrfs_release_path(path); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3803 | |
Miao Xie | 67de117 | 2013-12-26 13:07:06 +0800 | [diff] [blame] | 3804 | /* |
| 3805 | * If we don't have dir index, we have to get it by looking up |
| 3806 | * the inode ref, since we get the inode ref, remove it directly, |
| 3807 | * it is unnecessary to do delayed deletion. |
| 3808 | * |
| 3809 | * But if we have dir index, needn't search inode ref to get it. |
| 3810 | * Since the inode ref is close to the inode item, it is better |
| 3811 | * that we delay to delete it, and just do this deletion when |
| 3812 | * we update the inode item. |
| 3813 | */ |
| 3814 | if (BTRFS_I(inode)->dir_index) { |
| 3815 | ret = btrfs_delayed_delete_inode_ref(inode); |
| 3816 | if (!ret) { |
| 3817 | index = BTRFS_I(inode)->dir_index; |
| 3818 | goto skip_backref; |
| 3819 | } |
| 3820 | } |
| 3821 | |
Li Zefan | 33345d01 | 2011-04-20 10:31:50 +0800 | [diff] [blame] | 3822 | ret = btrfs_del_inode_ref(trans, root, name, name_len, ino, |
| 3823 | dir_ino, &index); |
Josef Bacik | aec7477 | 2008-07-24 12:12:38 -0400 | [diff] [blame] | 3824 | if (ret) { |
Simon Kirby | c2cf52e | 2013-03-19 22:41:23 +0000 | [diff] [blame] | 3825 | btrfs_info(root->fs_info, |
| 3826 | "failed to delete reference to %.*s, inode %llu parent %llu", |
Geert Uytterhoeven | c1c9ff7 | 2013-08-20 13:20:07 +0200 | [diff] [blame] | 3827 | name_len, name, ino, dir_ino); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 3828 | btrfs_abort_transaction(trans, root, ret); |
Josef Bacik | aec7477 | 2008-07-24 12:12:38 -0400 | [diff] [blame] | 3829 | goto err; |
| 3830 | } |
Miao Xie | 67de117 | 2013-12-26 13:07:06 +0800 | [diff] [blame] | 3831 | skip_backref: |
Miao Xie | 16cdcec | 2011-04-22 18:12:22 +0800 | [diff] [blame] | 3832 | ret = btrfs_delete_delayed_dir_index(trans, root, dir, index); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 3833 | if (ret) { |
| 3834 | btrfs_abort_transaction(trans, root, ret); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3835 | goto err; |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 3836 | } |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3837 | |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 3838 | ret = btrfs_del_inode_ref_in_log(trans, root, name, name_len, |
Li Zefan | 33345d01 | 2011-04-20 10:31:50 +0800 | [diff] [blame] | 3839 | inode, dir_ino); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 3840 | if (ret != 0 && ret != -ENOENT) { |
| 3841 | btrfs_abort_transaction(trans, root, ret); |
| 3842 | goto err; |
| 3843 | } |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 3844 | |
| 3845 | ret = btrfs_del_dir_entries_in_log(trans, root, name, name_len, |
| 3846 | dir, index); |
Chris Mason | 6418c96 | 2010-10-30 07:34:24 -0400 | [diff] [blame] | 3847 | if (ret == -ENOENT) |
| 3848 | ret = 0; |
Zach Brown | d4e3991 | 2013-04-02 21:02:16 +0000 | [diff] [blame] | 3849 | else if (ret) |
| 3850 | btrfs_abort_transaction(trans, root, ret); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3851 | err: |
| 3852 | btrfs_free_path(path); |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 3853 | if (ret) |
| 3854 | goto out; |
| 3855 | |
| 3856 | btrfs_i_size_write(dir, dir->i_size - name_len * 2); |
Josef Bacik | 0c4d2d9 | 2012-04-05 15:03:02 -0400 | [diff] [blame] | 3857 | inode_inc_iversion(inode); |
| 3858 | inode_inc_iversion(dir); |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 3859 | inode->i_ctime = dir->i_mtime = dir->i_ctime = CURRENT_TIME; |
Tsutomu Itoh | b995929 | 2012-06-25 21:25:22 -0600 | [diff] [blame] | 3860 | ret = btrfs_update_inode(trans, root, dir); |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 3861 | out: |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3862 | return ret; |
| 3863 | } |
| 3864 | |
Al Viro | 9298679 | 2011-03-04 17:14:37 +0000 | [diff] [blame] | 3865 | int btrfs_unlink_inode(struct btrfs_trans_handle *trans, |
| 3866 | struct btrfs_root *root, |
| 3867 | struct inode *dir, struct inode *inode, |
| 3868 | const char *name, int name_len) |
| 3869 | { |
| 3870 | int ret; |
| 3871 | ret = __btrfs_unlink_inode(trans, root, dir, inode, name, name_len); |
| 3872 | if (!ret) { |
Zach Brown | 8b558c5 | 2013-10-16 12:10:34 -0700 | [diff] [blame] | 3873 | drop_nlink(inode); |
Al Viro | 9298679 | 2011-03-04 17:14:37 +0000 | [diff] [blame] | 3874 | ret = btrfs_update_inode(trans, root, inode); |
| 3875 | } |
| 3876 | return ret; |
| 3877 | } |
Yan, Zheng | a22285a | 2010-05-16 10:48:46 -0400 | [diff] [blame] | 3878 | |
| 3879 | /* |
| 3880 | * helper to start transaction for unlink and rmdir. |
| 3881 | * |
Josef Bacik | d52be81 | 2013-05-29 14:54:47 -0400 | [diff] [blame] | 3882 | * unlink and rmdir are special in btrfs, they do not always free space, so |
| 3883 | * if we cannot make our reservations the normal way try and see if there is |
| 3884 | * plenty of slack room in the global reserve to migrate, otherwise we cannot |
| 3885 | * allow the unlink to occur. |
Yan, Zheng | a22285a | 2010-05-16 10:48:46 -0400 | [diff] [blame] | 3886 | */ |
Josef Bacik | d52be81 | 2013-05-29 14:54:47 -0400 | [diff] [blame] | 3887 | static struct btrfs_trans_handle *__unlink_start_trans(struct inode *dir) |
Yan, Zheng | a22285a | 2010-05-16 10:48:46 -0400 | [diff] [blame] | 3888 | { |
| 3889 | struct btrfs_trans_handle *trans; |
| 3890 | struct btrfs_root *root = BTRFS_I(dir)->root; |
Yan, Zheng | a22285a | 2010-05-16 10:48:46 -0400 | [diff] [blame] | 3891 | int ret; |
| 3892 | |
Josef Bacik | e70bea5 | 2011-10-11 14:18:24 -0400 | [diff] [blame] | 3893 | /* |
| 3894 | * 1 for the possible orphan item |
| 3895 | * 1 for the dir item |
| 3896 | * 1 for the dir index |
| 3897 | * 1 for the inode ref |
Josef Bacik | e70bea5 | 2011-10-11 14:18:24 -0400 | [diff] [blame] | 3898 | * 1 for the inode |
| 3899 | */ |
Josef Bacik | 6e137ed | 2013-03-26 15:26:55 -0400 | [diff] [blame] | 3900 | trans = btrfs_start_transaction(root, 5); |
Yan, Zheng | a22285a | 2010-05-16 10:48:46 -0400 | [diff] [blame] | 3901 | if (!IS_ERR(trans) || PTR_ERR(trans) != -ENOSPC) |
| 3902 | return trans; |
| 3903 | |
Josef Bacik | d52be81 | 2013-05-29 14:54:47 -0400 | [diff] [blame] | 3904 | if (PTR_ERR(trans) == -ENOSPC) { |
| 3905 | u64 num_bytes = btrfs_calc_trans_metadata_size(root, 5); |
Yan, Zheng | a22285a | 2010-05-16 10:48:46 -0400 | [diff] [blame] | 3906 | |
Josef Bacik | d52be81 | 2013-05-29 14:54:47 -0400 | [diff] [blame] | 3907 | trans = btrfs_start_transaction(root, 0); |
| 3908 | if (IS_ERR(trans)) |
| 3909 | return trans; |
| 3910 | ret = btrfs_cond_migrate_bytes(root->fs_info, |
| 3911 | &root->fs_info->trans_block_rsv, |
| 3912 | num_bytes, 5); |
| 3913 | if (ret) { |
| 3914 | btrfs_end_transaction(trans, root); |
| 3915 | return ERR_PTR(ret); |
Yan, Zheng | a22285a | 2010-05-16 10:48:46 -0400 | [diff] [blame] | 3916 | } |
Josef Bacik | 5a77d76 | 2011-11-01 14:32:23 -0400 | [diff] [blame] | 3917 | trans->block_rsv = &root->fs_info->trans_block_rsv; |
Josef Bacik | d52be81 | 2013-05-29 14:54:47 -0400 | [diff] [blame] | 3918 | trans->bytes_reserved = num_bytes; |
Yan, Zheng | a22285a | 2010-05-16 10:48:46 -0400 | [diff] [blame] | 3919 | } |
Josef Bacik | d52be81 | 2013-05-29 14:54:47 -0400 | [diff] [blame] | 3920 | return trans; |
Yan, Zheng | a22285a | 2010-05-16 10:48:46 -0400 | [diff] [blame] | 3921 | } |
| 3922 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3923 | static int btrfs_unlink(struct inode *dir, struct dentry *dentry) |
| 3924 | { |
Yan, Zheng | a22285a | 2010-05-16 10:48:46 -0400 | [diff] [blame] | 3925 | struct btrfs_root *root = BTRFS_I(dir)->root; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3926 | struct btrfs_trans_handle *trans; |
Josef Bacik | 7b12876 | 2008-07-24 12:17:14 -0400 | [diff] [blame] | 3927 | struct inode *inode = dentry->d_inode; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3928 | int ret; |
| 3929 | |
Josef Bacik | d52be81 | 2013-05-29 14:54:47 -0400 | [diff] [blame] | 3930 | trans = __unlink_start_trans(dir); |
Yan, Zheng | a22285a | 2010-05-16 10:48:46 -0400 | [diff] [blame] | 3931 | if (IS_ERR(trans)) |
Josef Bacik | 5df6a9f | 2009-11-10 21:23:48 -0500 | [diff] [blame] | 3932 | return PTR_ERR(trans); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 3933 | |
Chris Mason | 12fcfd2 | 2009-03-24 10:24:20 -0400 | [diff] [blame] | 3934 | btrfs_record_unlink_dir(trans, dir, dentry->d_inode, 0); |
| 3935 | |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 3936 | ret = btrfs_unlink_inode(trans, root, dir, dentry->d_inode, |
| 3937 | dentry->d_name.name, dentry->d_name.len); |
Tsutomu Itoh | b532402 | 2011-07-19 07:27:20 +0000 | [diff] [blame] | 3938 | if (ret) |
| 3939 | goto out; |
Josef Bacik | 7b12876 | 2008-07-24 12:17:14 -0400 | [diff] [blame] | 3940 | |
Yan, Zheng | a22285a | 2010-05-16 10:48:46 -0400 | [diff] [blame] | 3941 | if (inode->i_nlink == 0) { |
Josef Bacik | 7b12876 | 2008-07-24 12:17:14 -0400 | [diff] [blame] | 3942 | ret = btrfs_orphan_add(trans, inode); |
Tsutomu Itoh | b532402 | 2011-07-19 07:27:20 +0000 | [diff] [blame] | 3943 | if (ret) |
| 3944 | goto out; |
Yan, Zheng | a22285a | 2010-05-16 10:48:46 -0400 | [diff] [blame] | 3945 | } |
Josef Bacik | 7b12876 | 2008-07-24 12:17:14 -0400 | [diff] [blame] | 3946 | |
Tsutomu Itoh | b532402 | 2011-07-19 07:27:20 +0000 | [diff] [blame] | 3947 | out: |
Josef Bacik | d52be81 | 2013-05-29 14:54:47 -0400 | [diff] [blame] | 3948 | btrfs_end_transaction(trans, root); |
Liu Bo | b53d3f5 | 2012-11-14 14:34:34 +0000 | [diff] [blame] | 3949 | btrfs_btree_balance_dirty(root); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3950 | return ret; |
| 3951 | } |
| 3952 | |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 3953 | int btrfs_unlink_subvol(struct btrfs_trans_handle *trans, |
| 3954 | struct btrfs_root *root, |
| 3955 | struct inode *dir, u64 objectid, |
| 3956 | const char *name, int name_len) |
| 3957 | { |
| 3958 | struct btrfs_path *path; |
| 3959 | struct extent_buffer *leaf; |
| 3960 | struct btrfs_dir_item *di; |
| 3961 | struct btrfs_key key; |
| 3962 | u64 index; |
| 3963 | int ret; |
Li Zefan | 33345d01 | 2011-04-20 10:31:50 +0800 | [diff] [blame] | 3964 | u64 dir_ino = btrfs_ino(dir); |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 3965 | |
| 3966 | path = btrfs_alloc_path(); |
| 3967 | if (!path) |
| 3968 | return -ENOMEM; |
| 3969 | |
Li Zefan | 33345d01 | 2011-04-20 10:31:50 +0800 | [diff] [blame] | 3970 | di = btrfs_lookup_dir_item(trans, root, path, dir_ino, |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 3971 | name, name_len, -1); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 3972 | if (IS_ERR_OR_NULL(di)) { |
| 3973 | if (!di) |
| 3974 | ret = -ENOENT; |
| 3975 | else |
| 3976 | ret = PTR_ERR(di); |
| 3977 | goto out; |
| 3978 | } |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 3979 | |
| 3980 | leaf = path->nodes[0]; |
| 3981 | btrfs_dir_item_key_to_cpu(leaf, di, &key); |
| 3982 | WARN_ON(key.type != BTRFS_ROOT_ITEM_KEY || key.objectid != objectid); |
| 3983 | ret = btrfs_delete_one_dir_name(trans, root, path, di); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 3984 | if (ret) { |
| 3985 | btrfs_abort_transaction(trans, root, ret); |
| 3986 | goto out; |
| 3987 | } |
David Sterba | b3b4aa7 | 2011-04-21 01:20:15 +0200 | [diff] [blame] | 3988 | btrfs_release_path(path); |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 3989 | |
| 3990 | ret = btrfs_del_root_ref(trans, root->fs_info->tree_root, |
| 3991 | objectid, root->root_key.objectid, |
Li Zefan | 33345d01 | 2011-04-20 10:31:50 +0800 | [diff] [blame] | 3992 | dir_ino, &index, name, name_len); |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 3993 | if (ret < 0) { |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 3994 | if (ret != -ENOENT) { |
| 3995 | btrfs_abort_transaction(trans, root, ret); |
| 3996 | goto out; |
| 3997 | } |
Li Zefan | 33345d01 | 2011-04-20 10:31:50 +0800 | [diff] [blame] | 3998 | di = btrfs_search_dir_index_item(root, path, dir_ino, |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 3999 | name, name_len); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 4000 | if (IS_ERR_OR_NULL(di)) { |
| 4001 | if (!di) |
| 4002 | ret = -ENOENT; |
| 4003 | else |
| 4004 | ret = PTR_ERR(di); |
| 4005 | btrfs_abort_transaction(trans, root, ret); |
| 4006 | goto out; |
| 4007 | } |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 4008 | |
| 4009 | leaf = path->nodes[0]; |
| 4010 | btrfs_item_key_to_cpu(leaf, &key, path->slots[0]); |
David Sterba | b3b4aa7 | 2011-04-21 01:20:15 +0200 | [diff] [blame] | 4011 | btrfs_release_path(path); |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 4012 | index = key.offset; |
| 4013 | } |
David Sterba | b3b4aa7 | 2011-04-21 01:20:15 +0200 | [diff] [blame] | 4014 | btrfs_release_path(path); |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 4015 | |
Miao Xie | 16cdcec | 2011-04-22 18:12:22 +0800 | [diff] [blame] | 4016 | ret = btrfs_delete_delayed_dir_index(trans, root, dir, index); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 4017 | if (ret) { |
| 4018 | btrfs_abort_transaction(trans, root, ret); |
| 4019 | goto out; |
| 4020 | } |
Miao Xie | 16cdcec | 2011-04-22 18:12:22 +0800 | [diff] [blame] | 4021 | |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 4022 | btrfs_i_size_write(dir, dir->i_size - name_len * 2); |
Josef Bacik | 0c4d2d9 | 2012-04-05 15:03:02 -0400 | [diff] [blame] | 4023 | inode_inc_iversion(dir); |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 4024 | dir->i_mtime = dir->i_ctime = CURRENT_TIME; |
Josef Bacik | 5a24e84 | 2012-08-08 10:12:59 -0600 | [diff] [blame] | 4025 | ret = btrfs_update_inode_fallback(trans, root, dir); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 4026 | if (ret) |
| 4027 | btrfs_abort_transaction(trans, root, ret); |
| 4028 | out: |
Josef Bacik | 71d7aed | 2011-06-14 14:24:32 -0400 | [diff] [blame] | 4029 | btrfs_free_path(path); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 4030 | return ret; |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 4031 | } |
| 4032 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4033 | static int btrfs_rmdir(struct inode *dir, struct dentry *dentry) |
| 4034 | { |
| 4035 | struct inode *inode = dentry->d_inode; |
Chris Mason | 1832a6d | 2007-12-21 16:27:21 -0500 | [diff] [blame] | 4036 | int err = 0; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4037 | struct btrfs_root *root = BTRFS_I(dir)->root; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4038 | struct btrfs_trans_handle *trans; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4039 | |
David Sterba | b3ae244 | 2012-09-13 16:04:34 -0600 | [diff] [blame] | 4040 | if (inode->i_size > BTRFS_EMPTY_DIR_SIZE) |
Yan | 134d451 | 2007-10-25 15:49:25 -0400 | [diff] [blame] | 4041 | return -ENOTEMPTY; |
David Sterba | b3ae244 | 2012-09-13 16:04:34 -0600 | [diff] [blame] | 4042 | if (btrfs_ino(inode) == BTRFS_FIRST_FREE_OBJECTID) |
| 4043 | return -EPERM; |
Yan | 134d451 | 2007-10-25 15:49:25 -0400 | [diff] [blame] | 4044 | |
Josef Bacik | d52be81 | 2013-05-29 14:54:47 -0400 | [diff] [blame] | 4045 | trans = __unlink_start_trans(dir); |
Yan, Zheng | a22285a | 2010-05-16 10:48:46 -0400 | [diff] [blame] | 4046 | if (IS_ERR(trans)) |
Josef Bacik | 5df6a9f | 2009-11-10 21:23:48 -0500 | [diff] [blame] | 4047 | return PTR_ERR(trans); |
Josef Bacik | 5df6a9f | 2009-11-10 21:23:48 -0500 | [diff] [blame] | 4048 | |
Li Zefan | 33345d01 | 2011-04-20 10:31:50 +0800 | [diff] [blame] | 4049 | if (unlikely(btrfs_ino(inode) == BTRFS_EMPTY_SUBVOL_DIR_OBJECTID)) { |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 4050 | err = btrfs_unlink_subvol(trans, root, dir, |
| 4051 | BTRFS_I(inode)->location.objectid, |
| 4052 | dentry->d_name.name, |
| 4053 | dentry->d_name.len); |
| 4054 | goto out; |
| 4055 | } |
| 4056 | |
Josef Bacik | 7b12876 | 2008-07-24 12:17:14 -0400 | [diff] [blame] | 4057 | err = btrfs_orphan_add(trans, inode); |
| 4058 | if (err) |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 4059 | goto out; |
Josef Bacik | 7b12876 | 2008-07-24 12:17:14 -0400 | [diff] [blame] | 4060 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4061 | /* now the directory is empty */ |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 4062 | err = btrfs_unlink_inode(trans, root, dir, dentry->d_inode, |
| 4063 | dentry->d_name.name, dentry->d_name.len); |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 4064 | if (!err) |
Chris Mason | dbe674a | 2008-07-17 12:54:05 -0400 | [diff] [blame] | 4065 | btrfs_i_size_write(inode, 0); |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 4066 | out: |
Josef Bacik | d52be81 | 2013-05-29 14:54:47 -0400 | [diff] [blame] | 4067 | btrfs_end_transaction(trans, root); |
Liu Bo | b53d3f5 | 2012-11-14 14:34:34 +0000 | [diff] [blame] | 4068 | btrfs_btree_balance_dirty(root); |
Chris Mason | 3954401 | 2007-12-12 14:38:19 -0500 | [diff] [blame] | 4069 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4070 | return err; |
| 4071 | } |
| 4072 | |
Chris Mason | 323ac95 | 2008-10-01 19:05:46 -0400 | [diff] [blame] | 4073 | /* |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4074 | * this can truncate away extent items, csum items and directory items. |
| 4075 | * 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] | 4076 | * any higher than new_size |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4077 | * |
| 4078 | * csum items that cross the new i_size are truncated to the new size |
| 4079 | * as well. |
Josef Bacik | 7b12876 | 2008-07-24 12:17:14 -0400 | [diff] [blame] | 4080 | * |
| 4081 | * min_type is the minimum key type to truncate down to. If set to 0, this |
| 4082 | * 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] | 4083 | */ |
Yan, Zheng | 8082510 | 2009-11-12 09:35:36 +0000 | [diff] [blame] | 4084 | int btrfs_truncate_inode_items(struct btrfs_trans_handle *trans, |
| 4085 | struct btrfs_root *root, |
| 4086 | struct inode *inode, |
| 4087 | u64 new_size, u32 min_type) |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4088 | { |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4089 | struct btrfs_path *path; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 4090 | struct extent_buffer *leaf; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4091 | struct btrfs_file_extent_item *fi; |
Yan, Zheng | 8082510 | 2009-11-12 09:35:36 +0000 | [diff] [blame] | 4092 | struct btrfs_key key; |
| 4093 | struct btrfs_key found_key; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4094 | u64 extent_start = 0; |
Chris Mason | db94535 | 2007-10-15 16:15:53 -0400 | [diff] [blame] | 4095 | u64 extent_num_bytes = 0; |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 4096 | u64 extent_offset = 0; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4097 | u64 item_end = 0; |
Josef Bacik | 7f4f6e0 | 2013-08-29 16:43:28 -0400 | [diff] [blame] | 4098 | u64 last_size = (u64)-1; |
Yan, Zheng | 8082510 | 2009-11-12 09:35:36 +0000 | [diff] [blame] | 4099 | u32 found_type = (u8)-1; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4100 | int found_extent; |
| 4101 | int del_item; |
Chris Mason | 85e21ba | 2008-01-29 15:11:36 -0500 | [diff] [blame] | 4102 | int pending_del_nr = 0; |
| 4103 | int pending_del_slot = 0; |
Chris Mason | 179e29e | 2007-11-01 11:28:41 -0400 | [diff] [blame] | 4104 | int extent_type = -1; |
Yan, Zheng | 8082510 | 2009-11-12 09:35:36 +0000 | [diff] [blame] | 4105 | int ret; |
| 4106 | int err = 0; |
Li Zefan | 33345d01 | 2011-04-20 10:31:50 +0800 | [diff] [blame] | 4107 | u64 ino = btrfs_ino(inode); |
Yan, Zheng | 8082510 | 2009-11-12 09:35:36 +0000 | [diff] [blame] | 4108 | |
| 4109 | BUG_ON(new_size > 0 && min_type != BTRFS_EXTENT_DATA_KEY); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4110 | |
Mark Fasheh | 0eb0e19 | 2011-07-12 16:44:10 -0700 | [diff] [blame] | 4111 | path = btrfs_alloc_path(); |
| 4112 | if (!path) |
| 4113 | return -ENOMEM; |
| 4114 | path->reada = -1; |
| 4115 | |
Josef Bacik | 5dc562c | 2012-08-17 13:14:17 -0400 | [diff] [blame] | 4116 | /* |
| 4117 | * We want to drop from the next block forward in case this new size is |
| 4118 | * not block aligned since we will be keeping the last block of the |
| 4119 | * extent just the way it is. |
| 4120 | */ |
Miao Xie | 27cdeb7 | 2014-04-02 19:51:05 +0800 | [diff] [blame] | 4121 | if (test_bit(BTRFS_ROOT_REF_COWS, &root->state) || |
| 4122 | root == root->fs_info->tree_root) |
Qu Wenruo | fda2832 | 2013-02-26 08:10:22 +0000 | [diff] [blame] | 4123 | btrfs_drop_extent_cache(inode, ALIGN(new_size, |
| 4124 | root->sectorsize), (u64)-1, 0); |
Yan, Zheng | 8082510 | 2009-11-12 09:35:36 +0000 | [diff] [blame] | 4125 | |
Miao Xie | 16cdcec | 2011-04-22 18:12:22 +0800 | [diff] [blame] | 4126 | /* |
| 4127 | * This function is also used to drop the items in the log tree before |
| 4128 | * we relog the inode, so if root != BTRFS_I(inode)->root, it means |
| 4129 | * it is used to drop the loged items. So we shouldn't kill the delayed |
| 4130 | * items. |
| 4131 | */ |
| 4132 | if (min_type == 0 && root == BTRFS_I(inode)->root) |
| 4133 | btrfs_kill_delayed_inode_items(inode); |
| 4134 | |
Li Zefan | 33345d01 | 2011-04-20 10:31:50 +0800 | [diff] [blame] | 4135 | key.objectid = ino; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4136 | key.offset = (u64)-1; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 4137 | key.type = (u8)-1; |
| 4138 | |
Chris Mason | 85e21ba | 2008-01-29 15:11:36 -0500 | [diff] [blame] | 4139 | search_again: |
Chris Mason | b947343 | 2009-03-13 11:00:37 -0400 | [diff] [blame] | 4140 | path->leave_spinning = 1; |
Chris Mason | 85e21ba | 2008-01-29 15:11:36 -0500 | [diff] [blame] | 4141 | ret = btrfs_search_slot(trans, root, &key, path, -1, 1); |
Yan, Zheng | 8082510 | 2009-11-12 09:35:36 +0000 | [diff] [blame] | 4142 | if (ret < 0) { |
| 4143 | err = ret; |
| 4144 | goto out; |
| 4145 | } |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 4146 | |
Chris Mason | 85e21ba | 2008-01-29 15:11:36 -0500 | [diff] [blame] | 4147 | if (ret > 0) { |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 4148 | /* there are no items in the tree for us to truncate, we're |
| 4149 | * done |
| 4150 | */ |
Yan, Zheng | 8082510 | 2009-11-12 09:35:36 +0000 | [diff] [blame] | 4151 | if (path->slots[0] == 0) |
| 4152 | goto out; |
Chris Mason | 85e21ba | 2008-01-29 15:11:36 -0500 | [diff] [blame] | 4153 | path->slots[0]--; |
| 4154 | } |
| 4155 | |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 4156 | while (1) { |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4157 | fi = NULL; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 4158 | leaf = path->nodes[0]; |
| 4159 | btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]); |
David Sterba | 962a298 | 2014-06-04 18:41:45 +0200 | [diff] [blame] | 4160 | found_type = found_key.type; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4161 | |
Li Zefan | 33345d01 | 2011-04-20 10:31:50 +0800 | [diff] [blame] | 4162 | if (found_key.objectid != ino) |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4163 | break; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 4164 | |
Chris Mason | 85e21ba | 2008-01-29 15:11:36 -0500 | [diff] [blame] | 4165 | if (found_type < min_type) |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4166 | break; |
| 4167 | |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 4168 | item_end = found_key.offset; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4169 | if (found_type == BTRFS_EXTENT_DATA_KEY) { |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 4170 | fi = btrfs_item_ptr(leaf, path->slots[0], |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4171 | struct btrfs_file_extent_item); |
Chris Mason | 179e29e | 2007-11-01 11:28:41 -0400 | [diff] [blame] | 4172 | extent_type = btrfs_file_extent_type(leaf, fi); |
| 4173 | if (extent_type != BTRFS_FILE_EXTENT_INLINE) { |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 4174 | item_end += |
Chris Mason | db94535 | 2007-10-15 16:15:53 -0400 | [diff] [blame] | 4175 | btrfs_file_extent_num_bytes(leaf, fi); |
Chris Mason | 179e29e | 2007-11-01 11:28:41 -0400 | [diff] [blame] | 4176 | } else if (extent_type == BTRFS_FILE_EXTENT_INLINE) { |
Chris Mason | 179e29e | 2007-11-01 11:28:41 -0400 | [diff] [blame] | 4177 | item_end += btrfs_file_extent_inline_len(leaf, |
Chris Mason | 514ac8a | 2014-01-03 21:07:00 -0800 | [diff] [blame] | 4178 | path->slots[0], fi); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4179 | } |
Yan | 008630c | 2007-11-07 13:31:09 -0500 | [diff] [blame] | 4180 | item_end--; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4181 | } |
Yan, Zheng | 8082510 | 2009-11-12 09:35:36 +0000 | [diff] [blame] | 4182 | if (found_type > min_type) { |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4183 | del_item = 1; |
Yan, Zheng | 8082510 | 2009-11-12 09:35:36 +0000 | [diff] [blame] | 4184 | } else { |
| 4185 | if (item_end < new_size) |
| 4186 | break; |
| 4187 | if (found_key.offset >= new_size) |
| 4188 | del_item = 1; |
| 4189 | else |
| 4190 | del_item = 0; |
| 4191 | } |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4192 | found_extent = 0; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4193 | /* FIXME, shrink the extent if the ref count is only 1 */ |
Chris Mason | 179e29e | 2007-11-01 11:28:41 -0400 | [diff] [blame] | 4194 | if (found_type != BTRFS_EXTENT_DATA_KEY) |
| 4195 | goto delete; |
| 4196 | |
Josef Bacik | 7f4f6e0 | 2013-08-29 16:43:28 -0400 | [diff] [blame] | 4197 | if (del_item) |
| 4198 | last_size = found_key.offset; |
| 4199 | else |
| 4200 | last_size = new_size; |
| 4201 | |
Chris Mason | 179e29e | 2007-11-01 11:28:41 -0400 | [diff] [blame] | 4202 | if (extent_type != BTRFS_FILE_EXTENT_INLINE) { |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4203 | u64 num_dec; |
Chris Mason | db94535 | 2007-10-15 16:15:53 -0400 | [diff] [blame] | 4204 | extent_start = btrfs_file_extent_disk_bytenr(leaf, fi); |
Miao Xie | f70a9a6b | 2012-01-12 19:10:12 -0500 | [diff] [blame] | 4205 | if (!del_item) { |
Chris Mason | db94535 | 2007-10-15 16:15:53 -0400 | [diff] [blame] | 4206 | u64 orig_num_bytes = |
| 4207 | btrfs_file_extent_num_bytes(leaf, fi); |
Qu Wenruo | fda2832 | 2013-02-26 08:10:22 +0000 | [diff] [blame] | 4208 | extent_num_bytes = ALIGN(new_size - |
| 4209 | found_key.offset, |
| 4210 | root->sectorsize); |
Chris Mason | db94535 | 2007-10-15 16:15:53 -0400 | [diff] [blame] | 4211 | btrfs_set_file_extent_num_bytes(leaf, fi, |
| 4212 | extent_num_bytes); |
| 4213 | num_dec = (orig_num_bytes - |
Chris Mason | 9069218 | 2008-02-08 13:49:28 -0500 | [diff] [blame] | 4214 | extent_num_bytes); |
Miao Xie | 27cdeb7 | 2014-04-02 19:51:05 +0800 | [diff] [blame] | 4215 | if (test_bit(BTRFS_ROOT_REF_COWS, |
| 4216 | &root->state) && |
| 4217 | extent_start != 0) |
Yan Zheng | a76a3cd | 2008-10-09 11:46:29 -0400 | [diff] [blame] | 4218 | inode_sub_bytes(inode, num_dec); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 4219 | btrfs_mark_buffer_dirty(leaf); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4220 | } else { |
Chris Mason | db94535 | 2007-10-15 16:15:53 -0400 | [diff] [blame] | 4221 | extent_num_bytes = |
| 4222 | btrfs_file_extent_disk_num_bytes(leaf, |
| 4223 | fi); |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 4224 | extent_offset = found_key.offset - |
| 4225 | btrfs_file_extent_offset(leaf, fi); |
| 4226 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4227 | /* FIXME blocksize != 4096 */ |
Chris Mason | 9069218 | 2008-02-08 13:49:28 -0500 | [diff] [blame] | 4228 | num_dec = btrfs_file_extent_num_bytes(leaf, fi); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4229 | if (extent_start != 0) { |
| 4230 | found_extent = 1; |
Miao Xie | 27cdeb7 | 2014-04-02 19:51:05 +0800 | [diff] [blame] | 4231 | if (test_bit(BTRFS_ROOT_REF_COWS, |
| 4232 | &root->state)) |
Yan Zheng | a76a3cd | 2008-10-09 11:46:29 -0400 | [diff] [blame] | 4233 | inode_sub_bytes(inode, num_dec); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4234 | } |
| 4235 | } |
Chris Mason | 9069218 | 2008-02-08 13:49:28 -0500 | [diff] [blame] | 4236 | } else if (extent_type == BTRFS_FILE_EXTENT_INLINE) { |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 4237 | /* |
| 4238 | * we can't truncate inline items that have had |
| 4239 | * special encodings |
| 4240 | */ |
| 4241 | if (!del_item && |
| 4242 | btrfs_file_extent_compression(leaf, fi) == 0 && |
| 4243 | btrfs_file_extent_encryption(leaf, fi) == 0 && |
| 4244 | btrfs_file_extent_other_encoding(leaf, fi) == 0) { |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 4245 | u32 size = new_size - found_key.offset; |
| 4246 | |
Miao Xie | 27cdeb7 | 2014-04-02 19:51:05 +0800 | [diff] [blame] | 4247 | if (test_bit(BTRFS_ROOT_REF_COWS, &root->state)) |
Yan Zheng | a76a3cd | 2008-10-09 11:46:29 -0400 | [diff] [blame] | 4248 | inode_sub_bytes(inode, item_end + 1 - |
| 4249 | new_size); |
Chris Mason | 514ac8a | 2014-01-03 21:07:00 -0800 | [diff] [blame] | 4250 | |
| 4251 | /* |
| 4252 | * update the ram bytes to properly reflect |
| 4253 | * the new size of our item |
| 4254 | */ |
| 4255 | btrfs_set_file_extent_ram_bytes(leaf, fi, size); |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 4256 | size = |
| 4257 | btrfs_file_extent_calc_inline_size(size); |
Tsutomu Itoh | afe5fea | 2013-04-16 05:18:22 +0000 | [diff] [blame] | 4258 | btrfs_truncate_item(root, path, size, 1); |
Miao Xie | 27cdeb7 | 2014-04-02 19:51:05 +0800 | [diff] [blame] | 4259 | } else if (test_bit(BTRFS_ROOT_REF_COWS, |
| 4260 | &root->state)) { |
Yan Zheng | a76a3cd | 2008-10-09 11:46:29 -0400 | [diff] [blame] | 4261 | inode_sub_bytes(inode, item_end + 1 - |
| 4262 | found_key.offset); |
Chris Mason | 9069218 | 2008-02-08 13:49:28 -0500 | [diff] [blame] | 4263 | } |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4264 | } |
Chris Mason | 179e29e | 2007-11-01 11:28:41 -0400 | [diff] [blame] | 4265 | delete: |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4266 | if (del_item) { |
Chris Mason | 85e21ba | 2008-01-29 15:11:36 -0500 | [diff] [blame] | 4267 | if (!pending_del_nr) { |
| 4268 | /* no pending yet, add ourselves */ |
| 4269 | pending_del_slot = path->slots[0]; |
| 4270 | pending_del_nr = 1; |
| 4271 | } else if (pending_del_nr && |
| 4272 | path->slots[0] + 1 == pending_del_slot) { |
| 4273 | /* hop on the pending chunk */ |
| 4274 | pending_del_nr++; |
| 4275 | pending_del_slot = path->slots[0]; |
| 4276 | } else { |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 4277 | BUG(); |
Chris Mason | 85e21ba | 2008-01-29 15:11:36 -0500 | [diff] [blame] | 4278 | } |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4279 | } else { |
| 4280 | break; |
| 4281 | } |
Miao Xie | 27cdeb7 | 2014-04-02 19:51:05 +0800 | [diff] [blame] | 4282 | if (found_extent && |
| 4283 | (test_bit(BTRFS_ROOT_REF_COWS, &root->state) || |
| 4284 | root == root->fs_info->tree_root)) { |
Chris Mason | b947343 | 2009-03-13 11:00:37 -0400 | [diff] [blame] | 4285 | btrfs_set_path_blocking(path); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4286 | ret = btrfs_free_extent(trans, root, extent_start, |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 4287 | extent_num_bytes, 0, |
| 4288 | btrfs_header_owner(leaf), |
Arne Jansen | 66d7e7f | 2011-09-12 15:26:38 +0200 | [diff] [blame] | 4289 | ino, extent_offset, 0); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4290 | BUG_ON(ret); |
| 4291 | } |
Chris Mason | 85e21ba | 2008-01-29 15:11:36 -0500 | [diff] [blame] | 4292 | |
Yan, Zheng | 8082510 | 2009-11-12 09:35:36 +0000 | [diff] [blame] | 4293 | if (found_type == BTRFS_INODE_ITEM_KEY) |
| 4294 | break; |
| 4295 | |
| 4296 | if (path->slots[0] == 0 || |
| 4297 | path->slots[0] != pending_del_slot) { |
Yan, Zheng | 8082510 | 2009-11-12 09:35:36 +0000 | [diff] [blame] | 4298 | if (pending_del_nr) { |
| 4299 | ret = btrfs_del_items(trans, root, path, |
| 4300 | pending_del_slot, |
| 4301 | pending_del_nr); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 4302 | if (ret) { |
| 4303 | btrfs_abort_transaction(trans, |
| 4304 | root, ret); |
| 4305 | goto error; |
| 4306 | } |
Yan, Zheng | 8082510 | 2009-11-12 09:35:36 +0000 | [diff] [blame] | 4307 | pending_del_nr = 0; |
| 4308 | } |
David Sterba | b3b4aa7 | 2011-04-21 01:20:15 +0200 | [diff] [blame] | 4309 | btrfs_release_path(path); |
Chris Mason | 85e21ba | 2008-01-29 15:11:36 -0500 | [diff] [blame] | 4310 | goto search_again; |
Yan, Zheng | 8082510 | 2009-11-12 09:35:36 +0000 | [diff] [blame] | 4311 | } else { |
| 4312 | path->slots[0]--; |
Chris Mason | 85e21ba | 2008-01-29 15:11:36 -0500 | [diff] [blame] | 4313 | } |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4314 | } |
Yan, Zheng | 8082510 | 2009-11-12 09:35:36 +0000 | [diff] [blame] | 4315 | out: |
Chris Mason | 85e21ba | 2008-01-29 15:11:36 -0500 | [diff] [blame] | 4316 | if (pending_del_nr) { |
| 4317 | ret = btrfs_del_items(trans, root, path, pending_del_slot, |
| 4318 | pending_del_nr); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 4319 | if (ret) |
| 4320 | btrfs_abort_transaction(trans, root, ret); |
Chris Mason | 85e21ba | 2008-01-29 15:11:36 -0500 | [diff] [blame] | 4321 | } |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 4322 | error: |
Filipe Manana | dac5705 | 2014-08-29 20:54:26 +0100 | [diff] [blame] | 4323 | if (last_size != (u64)-1 && |
| 4324 | root->root_key.objectid != BTRFS_TREE_LOG_OBJECTID) |
Josef Bacik | 7f4f6e0 | 2013-08-29 16:43:28 -0400 | [diff] [blame] | 4325 | btrfs_ordered_update_i_size(inode, last_size, NULL); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4326 | btrfs_free_path(path); |
Yan, Zheng | 8082510 | 2009-11-12 09:35:36 +0000 | [diff] [blame] | 4327 | return err; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4328 | } |
| 4329 | |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 4330 | /* |
Josef Bacik | 2aaa665 | 2012-08-29 14:27:18 -0400 | [diff] [blame] | 4331 | * btrfs_truncate_page - read, zero a chunk and write a page |
| 4332 | * @inode - inode that we're zeroing |
| 4333 | * @from - the offset to start zeroing |
| 4334 | * @len - the length to zero, 0 to zero the entire range respective to the |
| 4335 | * offset |
| 4336 | * @front - zero up to the offset instead of from the offset on |
| 4337 | * |
| 4338 | * This will find the page for the "from" offset and cow the page and zero the |
| 4339 | * 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] | 4340 | */ |
Josef Bacik | 2aaa665 | 2012-08-29 14:27:18 -0400 | [diff] [blame] | 4341 | int btrfs_truncate_page(struct inode *inode, loff_t from, loff_t len, |
| 4342 | int front) |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 4343 | { |
Josef Bacik | 2aaa665 | 2012-08-29 14:27:18 -0400 | [diff] [blame] | 4344 | struct address_space *mapping = inode->i_mapping; |
Chris Mason | db94535 | 2007-10-15 16:15:53 -0400 | [diff] [blame] | 4345 | struct btrfs_root *root = BTRFS_I(inode)->root; |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 4346 | struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree; |
| 4347 | struct btrfs_ordered_extent *ordered; |
Josef Bacik | 2ac55d4 | 2010-02-03 19:33:23 +0000 | [diff] [blame] | 4348 | struct extent_state *cached_state = NULL; |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 4349 | char *kaddr; |
Chris Mason | db94535 | 2007-10-15 16:15:53 -0400 | [diff] [blame] | 4350 | u32 blocksize = root->sectorsize; |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 4351 | pgoff_t index = from >> PAGE_CACHE_SHIFT; |
| 4352 | unsigned offset = from & (PAGE_CACHE_SIZE-1); |
| 4353 | struct page *page; |
Josef Bacik | 3b16a4e | 2011-09-21 15:05:58 -0400 | [diff] [blame] | 4354 | gfp_t mask = btrfs_alloc_write_mask(mapping); |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 4355 | int ret = 0; |
| 4356 | u64 page_start; |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 4357 | u64 page_end; |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 4358 | |
Josef Bacik | 2aaa665 | 2012-08-29 14:27:18 -0400 | [diff] [blame] | 4359 | if ((offset & (blocksize - 1)) == 0 && |
| 4360 | (!len || ((len & (blocksize - 1)) == 0))) |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 4361 | goto out; |
Yan, Zheng | 0ca1f7c | 2010-05-16 10:48:47 -0400 | [diff] [blame] | 4362 | ret = btrfs_delalloc_reserve_space(inode, PAGE_CACHE_SIZE); |
Josef Bacik | 5d5e103 | 2009-10-13 16:46:49 -0400 | [diff] [blame] | 4363 | if (ret) |
| 4364 | goto out; |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 4365 | |
Chris Mason | 211c17f | 2008-05-15 09:13:45 -0400 | [diff] [blame] | 4366 | again: |
Josef Bacik | 3b16a4e | 2011-09-21 15:05:58 -0400 | [diff] [blame] | 4367 | page = find_or_create_page(mapping, index, mask); |
Josef Bacik | 5d5e103 | 2009-10-13 16:46:49 -0400 | [diff] [blame] | 4368 | if (!page) { |
Yan, Zheng | 0ca1f7c | 2010-05-16 10:48:47 -0400 | [diff] [blame] | 4369 | btrfs_delalloc_release_space(inode, PAGE_CACHE_SIZE); |
Miao Xie | ac6a2b3 | 2012-12-05 10:56:13 +0000 | [diff] [blame] | 4370 | ret = -ENOMEM; |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 4371 | goto out; |
Josef Bacik | 5d5e103 | 2009-10-13 16:46:49 -0400 | [diff] [blame] | 4372 | } |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 4373 | |
| 4374 | page_start = page_offset(page); |
| 4375 | page_end = page_start + PAGE_CACHE_SIZE - 1; |
| 4376 | |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 4377 | if (!PageUptodate(page)) { |
| 4378 | ret = btrfs_readpage(NULL, page); |
| 4379 | lock_page(page); |
Chris Mason | 211c17f | 2008-05-15 09:13:45 -0400 | [diff] [blame] | 4380 | if (page->mapping != mapping) { |
| 4381 | unlock_page(page); |
| 4382 | page_cache_release(page); |
| 4383 | goto again; |
| 4384 | } |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 4385 | if (!PageUptodate(page)) { |
| 4386 | ret = -EIO; |
Chris Mason | 8964222 | 2008-07-24 09:41:53 -0400 | [diff] [blame] | 4387 | goto out_unlock; |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 4388 | } |
| 4389 | } |
Chris Mason | 211c17f | 2008-05-15 09:13:45 -0400 | [diff] [blame] | 4390 | wait_on_page_writeback(page); |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 4391 | |
Jeff Mahoney | d008237 | 2012-03-01 14:57:19 +0100 | [diff] [blame] | 4392 | lock_extent_bits(io_tree, page_start, page_end, 0, &cached_state); |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 4393 | set_page_extent_mapped(page); |
| 4394 | |
| 4395 | ordered = btrfs_lookup_ordered_extent(inode, page_start); |
| 4396 | if (ordered) { |
Josef Bacik | 2ac55d4 | 2010-02-03 19:33:23 +0000 | [diff] [blame] | 4397 | unlock_extent_cached(io_tree, page_start, page_end, |
| 4398 | &cached_state, GFP_NOFS); |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 4399 | unlock_page(page); |
| 4400 | page_cache_release(page); |
Chris Mason | eb84ae0 | 2008-07-17 13:53:27 -0400 | [diff] [blame] | 4401 | btrfs_start_ordered_extent(inode, ordered, 1); |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 4402 | btrfs_put_ordered_extent(ordered); |
| 4403 | goto again; |
| 4404 | } |
| 4405 | |
Josef Bacik | 2ac55d4 | 2010-02-03 19:33:23 +0000 | [diff] [blame] | 4406 | clear_extent_bit(&BTRFS_I(inode)->io_tree, page_start, page_end, |
Liu Bo | 9e8a4a8 | 2012-09-05 19:10:51 -0600 | [diff] [blame] | 4407 | EXTENT_DIRTY | EXTENT_DELALLOC | |
| 4408 | EXTENT_DO_ACCOUNTING | EXTENT_DEFRAG, |
Josef Bacik | 2ac55d4 | 2010-02-03 19:33:23 +0000 | [diff] [blame] | 4409 | 0, 0, &cached_state, GFP_NOFS); |
Josef Bacik | 5d5e103 | 2009-10-13 16:46:49 -0400 | [diff] [blame] | 4410 | |
Josef Bacik | 2ac55d4 | 2010-02-03 19:33:23 +0000 | [diff] [blame] | 4411 | ret = btrfs_set_extent_delalloc(inode, page_start, page_end, |
| 4412 | &cached_state); |
Josef Bacik | 9ed74f2 | 2009-09-11 16:12:44 -0400 | [diff] [blame] | 4413 | if (ret) { |
Josef Bacik | 2ac55d4 | 2010-02-03 19:33:23 +0000 | [diff] [blame] | 4414 | unlock_extent_cached(io_tree, page_start, page_end, |
| 4415 | &cached_state, GFP_NOFS); |
Josef Bacik | 9ed74f2 | 2009-09-11 16:12:44 -0400 | [diff] [blame] | 4416 | goto out_unlock; |
| 4417 | } |
| 4418 | |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 4419 | if (offset != PAGE_CACHE_SIZE) { |
Josef Bacik | 2aaa665 | 2012-08-29 14:27:18 -0400 | [diff] [blame] | 4420 | if (!len) |
| 4421 | len = PAGE_CACHE_SIZE - offset; |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 4422 | kaddr = kmap(page); |
Josef Bacik | 2aaa665 | 2012-08-29 14:27:18 -0400 | [diff] [blame] | 4423 | if (front) |
| 4424 | memset(kaddr, 0, offset); |
| 4425 | else |
| 4426 | memset(kaddr + offset, 0, len); |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 4427 | flush_dcache_page(page); |
| 4428 | kunmap(page); |
| 4429 | } |
Chris Mason | 247e743 | 2008-07-17 12:53:51 -0400 | [diff] [blame] | 4430 | ClearPageChecked(page); |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 4431 | set_page_dirty(page); |
Josef Bacik | 2ac55d4 | 2010-02-03 19:33:23 +0000 | [diff] [blame] | 4432 | unlock_extent_cached(io_tree, page_start, page_end, &cached_state, |
| 4433 | GFP_NOFS); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4434 | |
Chris Mason | 8964222 | 2008-07-24 09:41:53 -0400 | [diff] [blame] | 4435 | out_unlock: |
Josef Bacik | 5d5e103 | 2009-10-13 16:46:49 -0400 | [diff] [blame] | 4436 | if (ret) |
Yan, Zheng | 0ca1f7c | 2010-05-16 10:48:47 -0400 | [diff] [blame] | 4437 | btrfs_delalloc_release_space(inode, PAGE_CACHE_SIZE); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4438 | unlock_page(page); |
| 4439 | page_cache_release(page); |
| 4440 | out: |
| 4441 | return ret; |
| 4442 | } |
| 4443 | |
Josef Bacik | 16e7549 | 2013-10-22 12:18:51 -0400 | [diff] [blame] | 4444 | static int maybe_insert_hole(struct btrfs_root *root, struct inode *inode, |
| 4445 | u64 offset, u64 len) |
| 4446 | { |
| 4447 | struct btrfs_trans_handle *trans; |
| 4448 | int ret; |
| 4449 | |
| 4450 | /* |
| 4451 | * Still need to make sure the inode looks like it's been updated so |
| 4452 | * that any holes get logged if we fsync. |
| 4453 | */ |
| 4454 | if (btrfs_fs_incompat(root->fs_info, NO_HOLES)) { |
| 4455 | BTRFS_I(inode)->last_trans = root->fs_info->generation; |
| 4456 | BTRFS_I(inode)->last_sub_trans = root->log_transid; |
| 4457 | BTRFS_I(inode)->last_log_commit = root->last_log_commit; |
| 4458 | return 0; |
| 4459 | } |
| 4460 | |
| 4461 | /* |
| 4462 | * 1 - for the one we're dropping |
| 4463 | * 1 - for the one we're adding |
| 4464 | * 1 - for updating the inode. |
| 4465 | */ |
| 4466 | trans = btrfs_start_transaction(root, 3); |
| 4467 | if (IS_ERR(trans)) |
| 4468 | return PTR_ERR(trans); |
| 4469 | |
| 4470 | ret = btrfs_drop_extents(trans, root, inode, offset, offset + len, 1); |
| 4471 | if (ret) { |
| 4472 | btrfs_abort_transaction(trans, root, ret); |
| 4473 | btrfs_end_transaction(trans, root); |
| 4474 | return ret; |
| 4475 | } |
| 4476 | |
| 4477 | ret = btrfs_insert_file_extent(trans, root, btrfs_ino(inode), offset, |
| 4478 | 0, 0, len, 0, len, 0, 0, 0); |
| 4479 | if (ret) |
| 4480 | btrfs_abort_transaction(trans, root, ret); |
| 4481 | else |
| 4482 | btrfs_update_inode(trans, root, inode); |
| 4483 | btrfs_end_transaction(trans, root); |
| 4484 | return ret; |
| 4485 | } |
| 4486 | |
Josef Bacik | 695a0d0 | 2011-03-04 15:46:53 -0500 | [diff] [blame] | 4487 | /* |
| 4488 | * This function puts in dummy file extents for the area we're creating a hole |
| 4489 | * for. So if we are truncating this file to a larger size we need to insert |
| 4490 | * these file extents so that btrfs_get_extent will return a EXTENT_MAP_HOLE for |
| 4491 | * the range between oldsize and size |
| 4492 | */ |
Josef Bacik | a41ad39 | 2011-01-31 15:30:16 -0500 | [diff] [blame] | 4493 | 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] | 4494 | { |
Yan Zheng | 9036c10 | 2008-10-30 14:19:41 -0400 | [diff] [blame] | 4495 | struct btrfs_root *root = BTRFS_I(inode)->root; |
| 4496 | struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree; |
Yan, Zheng | a22285a | 2010-05-16 10:48:46 -0400 | [diff] [blame] | 4497 | struct extent_map *em = NULL; |
Josef Bacik | 2ac55d4 | 2010-02-03 19:33:23 +0000 | [diff] [blame] | 4498 | struct extent_state *cached_state = NULL; |
Josef Bacik | 5dc562c | 2012-08-17 13:14:17 -0400 | [diff] [blame] | 4499 | struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree; |
Qu Wenruo | fda2832 | 2013-02-26 08:10:22 +0000 | [diff] [blame] | 4500 | u64 hole_start = ALIGN(oldsize, root->sectorsize); |
| 4501 | u64 block_end = ALIGN(size, root->sectorsize); |
Yan Zheng | 9036c10 | 2008-10-30 14:19:41 -0400 | [diff] [blame] | 4502 | u64 last_byte; |
| 4503 | u64 cur_offset; |
| 4504 | u64 hole_size; |
Josef Bacik | 9ed74f2 | 2009-09-11 16:12:44 -0400 | [diff] [blame] | 4505 | int err = 0; |
Yan Zheng | 9036c10 | 2008-10-30 14:19:41 -0400 | [diff] [blame] | 4506 | |
Josef Bacik | a71754f | 2013-06-17 17:14:39 -0400 | [diff] [blame] | 4507 | /* |
| 4508 | * If our size started in the middle of a page we need to zero out the |
| 4509 | * rest of the page before we expand the i_size, otherwise we could |
| 4510 | * expose stale data. |
| 4511 | */ |
| 4512 | err = btrfs_truncate_page(inode, oldsize, 0, 0); |
| 4513 | if (err) |
| 4514 | return err; |
| 4515 | |
Yan Zheng | 9036c10 | 2008-10-30 14:19:41 -0400 | [diff] [blame] | 4516 | if (size <= hole_start) |
| 4517 | return 0; |
| 4518 | |
Yan Zheng | 9036c10 | 2008-10-30 14:19:41 -0400 | [diff] [blame] | 4519 | while (1) { |
| 4520 | struct btrfs_ordered_extent *ordered; |
Miao Xie | fa7c149 | 2013-09-26 13:15:27 +0800 | [diff] [blame] | 4521 | |
Josef Bacik | 2ac55d4 | 2010-02-03 19:33:23 +0000 | [diff] [blame] | 4522 | lock_extent_bits(io_tree, hole_start, block_end - 1, 0, |
Jeff Mahoney | d008237 | 2012-03-01 14:57:19 +0100 | [diff] [blame] | 4523 | &cached_state); |
Miao Xie | fa7c149 | 2013-09-26 13:15:27 +0800 | [diff] [blame] | 4524 | ordered = btrfs_lookup_ordered_range(inode, hole_start, |
| 4525 | block_end - hole_start); |
Yan Zheng | 9036c10 | 2008-10-30 14:19:41 -0400 | [diff] [blame] | 4526 | if (!ordered) |
| 4527 | break; |
Josef Bacik | 2ac55d4 | 2010-02-03 19:33:23 +0000 | [diff] [blame] | 4528 | unlock_extent_cached(io_tree, hole_start, block_end - 1, |
| 4529 | &cached_state, GFP_NOFS); |
Miao Xie | fa7c149 | 2013-09-26 13:15:27 +0800 | [diff] [blame] | 4530 | btrfs_start_ordered_extent(inode, ordered, 1); |
Yan Zheng | 9036c10 | 2008-10-30 14:19:41 -0400 | [diff] [blame] | 4531 | btrfs_put_ordered_extent(ordered); |
| 4532 | } |
| 4533 | |
Yan Zheng | 9036c10 | 2008-10-30 14:19:41 -0400 | [diff] [blame] | 4534 | cur_offset = hole_start; |
| 4535 | while (1) { |
| 4536 | em = btrfs_get_extent(inode, NULL, 0, cur_offset, |
| 4537 | block_end - cur_offset, 0); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 4538 | if (IS_ERR(em)) { |
| 4539 | err = PTR_ERR(em); |
Zach Brown | f276795 | 2013-01-08 19:37:58 +0000 | [diff] [blame] | 4540 | em = NULL; |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 4541 | break; |
| 4542 | } |
Yan Zheng | 9036c10 | 2008-10-30 14:19:41 -0400 | [diff] [blame] | 4543 | last_byte = min(extent_map_end(em), block_end); |
Qu Wenruo | fda2832 | 2013-02-26 08:10:22 +0000 | [diff] [blame] | 4544 | last_byte = ALIGN(last_byte , root->sectorsize); |
Yan, Zheng | 8082510 | 2009-11-12 09:35:36 +0000 | [diff] [blame] | 4545 | if (!test_bit(EXTENT_FLAG_PREALLOC, &em->flags)) { |
Josef Bacik | 5dc562c | 2012-08-17 13:14:17 -0400 | [diff] [blame] | 4546 | struct extent_map *hole_em; |
Yan Zheng | 9036c10 | 2008-10-30 14:19:41 -0400 | [diff] [blame] | 4547 | hole_size = last_byte - cur_offset; |
Yan, Zheng | 8082510 | 2009-11-12 09:35:36 +0000 | [diff] [blame] | 4548 | |
Josef Bacik | 16e7549 | 2013-10-22 12:18:51 -0400 | [diff] [blame] | 4549 | err = maybe_insert_hole(root, inode, cur_offset, |
| 4550 | hole_size); |
| 4551 | if (err) |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 4552 | break; |
Josef Bacik | 5dc562c | 2012-08-17 13:14:17 -0400 | [diff] [blame] | 4553 | btrfs_drop_extent_cache(inode, cur_offset, |
| 4554 | cur_offset + hole_size - 1, 0); |
| 4555 | hole_em = alloc_extent_map(); |
| 4556 | if (!hole_em) { |
| 4557 | set_bit(BTRFS_INODE_NEEDS_FULL_SYNC, |
| 4558 | &BTRFS_I(inode)->runtime_flags); |
| 4559 | goto next; |
| 4560 | } |
| 4561 | hole_em->start = cur_offset; |
| 4562 | hole_em->len = hole_size; |
| 4563 | hole_em->orig_start = cur_offset; |
Yan, Zheng | 8082510 | 2009-11-12 09:35:36 +0000 | [diff] [blame] | 4564 | |
Josef Bacik | 5dc562c | 2012-08-17 13:14:17 -0400 | [diff] [blame] | 4565 | hole_em->block_start = EXTENT_MAP_HOLE; |
| 4566 | hole_em->block_len = 0; |
Josef Bacik | b493968 | 2012-12-03 10:31:19 -0500 | [diff] [blame] | 4567 | hole_em->orig_block_len = 0; |
Josef Bacik | cc95bef | 2013-04-04 14:31:27 -0400 | [diff] [blame] | 4568 | hole_em->ram_bytes = hole_size; |
Josef Bacik | 5dc562c | 2012-08-17 13:14:17 -0400 | [diff] [blame] | 4569 | hole_em->bdev = root->fs_info->fs_devices->latest_bdev; |
| 4570 | hole_em->compress_type = BTRFS_COMPRESS_NONE; |
Josef Bacik | 16e7549 | 2013-10-22 12:18:51 -0400 | [diff] [blame] | 4571 | hole_em->generation = root->fs_info->generation; |
Josef Bacik | 5dc562c | 2012-08-17 13:14:17 -0400 | [diff] [blame] | 4572 | |
| 4573 | while (1) { |
| 4574 | write_lock(&em_tree->lock); |
Josef Bacik | 09a2a8f9 | 2013-04-05 16:51:15 -0400 | [diff] [blame] | 4575 | err = add_extent_mapping(em_tree, hole_em, 1); |
Josef Bacik | 5dc562c | 2012-08-17 13:14:17 -0400 | [diff] [blame] | 4576 | write_unlock(&em_tree->lock); |
| 4577 | if (err != -EEXIST) |
| 4578 | break; |
| 4579 | btrfs_drop_extent_cache(inode, cur_offset, |
| 4580 | cur_offset + |
| 4581 | hole_size - 1, 0); |
| 4582 | } |
| 4583 | free_extent_map(hole_em); |
Yan Zheng | 9036c10 | 2008-10-30 14:19:41 -0400 | [diff] [blame] | 4584 | } |
Josef Bacik | 16e7549 | 2013-10-22 12:18:51 -0400 | [diff] [blame] | 4585 | next: |
Yan Zheng | 9036c10 | 2008-10-30 14:19:41 -0400 | [diff] [blame] | 4586 | free_extent_map(em); |
Yan, Zheng | a22285a | 2010-05-16 10:48:46 -0400 | [diff] [blame] | 4587 | em = NULL; |
Yan Zheng | 9036c10 | 2008-10-30 14:19:41 -0400 | [diff] [blame] | 4588 | cur_offset = last_byte; |
Yan, Zheng | 8082510 | 2009-11-12 09:35:36 +0000 | [diff] [blame] | 4589 | if (cur_offset >= block_end) |
Yan Zheng | 9036c10 | 2008-10-30 14:19:41 -0400 | [diff] [blame] | 4590 | break; |
| 4591 | } |
Yan, Zheng | a22285a | 2010-05-16 10:48:46 -0400 | [diff] [blame] | 4592 | free_extent_map(em); |
Josef Bacik | 2ac55d4 | 2010-02-03 19:33:23 +0000 | [diff] [blame] | 4593 | unlock_extent_cached(io_tree, hole_start, block_end - 1, &cached_state, |
| 4594 | GFP_NOFS); |
Yan Zheng | 9036c10 | 2008-10-30 14:19:41 -0400 | [diff] [blame] | 4595 | return err; |
| 4596 | } |
| 4597 | |
Eric Sandeen | 3972f26 | 2013-01-12 02:57:22 +0000 | [diff] [blame] | 4598 | static int btrfs_setsize(struct inode *inode, struct iattr *attr) |
Yan, Zheng | 8082510 | 2009-11-12 09:35:36 +0000 | [diff] [blame] | 4599 | { |
Miao Xie | f4a2f4c | 2011-12-14 20:12:01 -0500 | [diff] [blame] | 4600 | struct btrfs_root *root = BTRFS_I(inode)->root; |
| 4601 | struct btrfs_trans_handle *trans; |
Josef Bacik | a41ad39 | 2011-01-31 15:30:16 -0500 | [diff] [blame] | 4602 | loff_t oldsize = i_size_read(inode); |
Eric Sandeen | 3972f26 | 2013-01-12 02:57:22 +0000 | [diff] [blame] | 4603 | loff_t newsize = attr->ia_size; |
| 4604 | int mask = attr->ia_valid; |
Yan, Zheng | 8082510 | 2009-11-12 09:35:36 +0000 | [diff] [blame] | 4605 | int ret; |
| 4606 | |
Eric Sandeen | 3972f26 | 2013-01-12 02:57:22 +0000 | [diff] [blame] | 4607 | /* |
| 4608 | * The regular truncate() case without ATTR_CTIME and ATTR_MTIME is a |
| 4609 | * special case where we need to update the times despite not having |
| 4610 | * these flags set. For all other operations the VFS set these flags |
| 4611 | * explicitly if it wants a timestamp update. |
| 4612 | */ |
Christoph Hellwig | dff6efc | 2013-11-19 07:17:07 -0800 | [diff] [blame] | 4613 | if (newsize != oldsize) { |
| 4614 | inode_inc_iversion(inode); |
| 4615 | if (!(mask & (ATTR_CTIME | ATTR_MTIME))) |
| 4616 | inode->i_ctime = inode->i_mtime = |
| 4617 | current_fs_time(inode->i_sb); |
| 4618 | } |
Eric Sandeen | 3972f26 | 2013-01-12 02:57:22 +0000 | [diff] [blame] | 4619 | |
Josef Bacik | a41ad39 | 2011-01-31 15:30:16 -0500 | [diff] [blame] | 4620 | if (newsize > oldsize) { |
Kirill A. Shutemov | 7caef26 | 2013-09-12 15:13:56 -0700 | [diff] [blame] | 4621 | truncate_pagecache(inode, newsize); |
Josef Bacik | a41ad39 | 2011-01-31 15:30:16 -0500 | [diff] [blame] | 4622 | ret = btrfs_cont_expand(inode, oldsize, newsize); |
Miao Xie | f4a2f4c | 2011-12-14 20:12:01 -0500 | [diff] [blame] | 4623 | if (ret) |
Yan, Zheng | 8082510 | 2009-11-12 09:35:36 +0000 | [diff] [blame] | 4624 | return ret; |
Yan, Zheng | 8082510 | 2009-11-12 09:35:36 +0000 | [diff] [blame] | 4625 | |
Miao Xie | f4a2f4c | 2011-12-14 20:12:01 -0500 | [diff] [blame] | 4626 | trans = btrfs_start_transaction(root, 1); |
| 4627 | if (IS_ERR(trans)) |
| 4628 | return PTR_ERR(trans); |
| 4629 | |
| 4630 | i_size_write(inode, newsize); |
| 4631 | btrfs_ordered_update_i_size(inode, i_size_read(inode), NULL); |
| 4632 | ret = btrfs_update_inode(trans, root, inode); |
Josef Bacik | 7ad85bb | 2012-01-12 19:10:12 -0500 | [diff] [blame] | 4633 | btrfs_end_transaction(trans, root); |
Josef Bacik | a41ad39 | 2011-01-31 15:30:16 -0500 | [diff] [blame] | 4634 | } else { |
Yan, Zheng | 8082510 | 2009-11-12 09:35:36 +0000 | [diff] [blame] | 4635 | |
Josef Bacik | a41ad39 | 2011-01-31 15:30:16 -0500 | [diff] [blame] | 4636 | /* |
| 4637 | * We're truncating a file that used to have good data down to |
| 4638 | * zero. Make sure it gets into the ordered flush list so that |
| 4639 | * any new writes get down to disk quickly. |
| 4640 | */ |
| 4641 | if (newsize == 0) |
Josef Bacik | 72ac3c0 | 2012-05-23 14:13:11 -0400 | [diff] [blame] | 4642 | set_bit(BTRFS_INODE_ORDERED_DATA_CLOSE, |
| 4643 | &BTRFS_I(inode)->runtime_flags); |
Yan, Zheng | 8082510 | 2009-11-12 09:35:36 +0000 | [diff] [blame] | 4644 | |
Josef Bacik | f3fe820 | 2013-01-07 17:03:21 -0500 | [diff] [blame] | 4645 | /* |
| 4646 | * 1 for the orphan item we're going to add |
| 4647 | * 1 for the orphan item deletion. |
| 4648 | */ |
| 4649 | trans = btrfs_start_transaction(root, 2); |
| 4650 | if (IS_ERR(trans)) |
| 4651 | return PTR_ERR(trans); |
| 4652 | |
| 4653 | /* |
| 4654 | * We need to do this in case we fail at _any_ point during the |
| 4655 | * actual truncate. Once we do the truncate_setsize we could |
| 4656 | * invalidate pages which forces any outstanding ordered io to |
| 4657 | * be instantly completed which will give us extents that need |
| 4658 | * to be truncated. If we fail to get an orphan inode down we |
| 4659 | * could have left over extents that were never meant to live, |
| 4660 | * so we need to garuntee from this point on that everything |
| 4661 | * will be consistent. |
| 4662 | */ |
| 4663 | ret = btrfs_orphan_add(trans, inode); |
| 4664 | btrfs_end_transaction(trans, root); |
| 4665 | if (ret) |
| 4666 | return ret; |
| 4667 | |
Josef Bacik | a41ad39 | 2011-01-31 15:30:16 -0500 | [diff] [blame] | 4668 | /* we don't support swapfiles, so vmtruncate shouldn't fail */ |
| 4669 | truncate_setsize(inode, newsize); |
Miao Xie | 2e60a51 | 2013-02-08 07:01:08 +0000 | [diff] [blame] | 4670 | |
| 4671 | /* Disable nonlocked read DIO to avoid the end less truncate */ |
| 4672 | btrfs_inode_block_unlocked_dio(inode); |
| 4673 | inode_dio_wait(inode); |
| 4674 | btrfs_inode_resume_unlocked_dio(inode); |
| 4675 | |
Josef Bacik | a41ad39 | 2011-01-31 15:30:16 -0500 | [diff] [blame] | 4676 | ret = btrfs_truncate(inode); |
Josef Bacik | 7f4f6e0 | 2013-08-29 16:43:28 -0400 | [diff] [blame] | 4677 | if (ret && inode->i_nlink) { |
| 4678 | int err; |
| 4679 | |
| 4680 | /* |
| 4681 | * failed to truncate, disk_i_size is only adjusted down |
| 4682 | * as we remove extents, so it should represent the true |
| 4683 | * size of the inode, so reset the in memory size and |
| 4684 | * delete our orphan entry. |
| 4685 | */ |
| 4686 | trans = btrfs_join_transaction(root); |
| 4687 | if (IS_ERR(trans)) { |
| 4688 | btrfs_orphan_del(NULL, inode); |
| 4689 | return ret; |
| 4690 | } |
| 4691 | i_size_write(inode, BTRFS_I(inode)->disk_i_size); |
| 4692 | err = btrfs_orphan_del(trans, inode); |
| 4693 | if (err) |
| 4694 | btrfs_abort_transaction(trans, root, err); |
| 4695 | btrfs_end_transaction(trans, root); |
| 4696 | } |
Yan, Zheng | 8082510 | 2009-11-12 09:35:36 +0000 | [diff] [blame] | 4697 | } |
| 4698 | |
Josef Bacik | a41ad39 | 2011-01-31 15:30:16 -0500 | [diff] [blame] | 4699 | return ret; |
Yan, Zheng | 8082510 | 2009-11-12 09:35:36 +0000 | [diff] [blame] | 4700 | } |
| 4701 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4702 | static int btrfs_setattr(struct dentry *dentry, struct iattr *attr) |
| 4703 | { |
| 4704 | struct inode *inode = dentry->d_inode; |
Li Zefan | b83cc96 | 2010-12-20 16:04:08 +0800 | [diff] [blame] | 4705 | struct btrfs_root *root = BTRFS_I(inode)->root; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4706 | int err; |
| 4707 | |
Li Zefan | b83cc96 | 2010-12-20 16:04:08 +0800 | [diff] [blame] | 4708 | if (btrfs_root_readonly(root)) |
| 4709 | return -EROFS; |
| 4710 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4711 | err = inode_change_ok(inode, attr); |
| 4712 | if (err) |
| 4713 | return err; |
| 4714 | |
Chris Mason | 5a3f23d | 2009-03-31 13:27:11 -0400 | [diff] [blame] | 4715 | if (S_ISREG(inode->i_mode) && (attr->ia_valid & ATTR_SIZE)) { |
Eric Sandeen | 3972f26 | 2013-01-12 02:57:22 +0000 | [diff] [blame] | 4716 | err = btrfs_setsize(inode, attr); |
Yan, Zheng | 8082510 | 2009-11-12 09:35:36 +0000 | [diff] [blame] | 4717 | if (err) |
| 4718 | return err; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4719 | } |
Yan Zheng | 9036c10 | 2008-10-30 14:19:41 -0400 | [diff] [blame] | 4720 | |
Christoph Hellwig | 1025774 | 2010-06-04 11:30:02 +0200 | [diff] [blame] | 4721 | if (attr->ia_valid) { |
| 4722 | setattr_copy(inode, attr); |
Josef Bacik | 0c4d2d9 | 2012-04-05 15:03:02 -0400 | [diff] [blame] | 4723 | inode_inc_iversion(inode); |
Josef Bacik | 22c44fe | 2011-11-30 10:45:38 -0500 | [diff] [blame] | 4724 | err = btrfs_dirty_inode(inode); |
Josef Bacik | 33268ea | 2008-07-24 12:16:36 -0400 | [diff] [blame] | 4725 | |
Josef Bacik | 22c44fe | 2011-11-30 10:45:38 -0500 | [diff] [blame] | 4726 | if (!err && attr->ia_valid & ATTR_MODE) |
Christoph Hellwig | 996a710 | 2013-12-20 05:16:43 -0800 | [diff] [blame] | 4727 | err = posix_acl_chmod(inode, inode->i_mode); |
Christoph Hellwig | 1025774 | 2010-06-04 11:30:02 +0200 | [diff] [blame] | 4728 | } |
| 4729 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4730 | return err; |
| 4731 | } |
Chris Mason | 61295eb | 2008-01-14 16:24:38 -0500 | [diff] [blame] | 4732 | |
Filipe David Borba Manana | 131e404 | 2013-11-19 22:29:35 +0000 | [diff] [blame] | 4733 | /* |
| 4734 | * While truncating the inode pages during eviction, we get the VFS calling |
| 4735 | * btrfs_invalidatepage() against each page of the inode. This is slow because |
| 4736 | * the calls to btrfs_invalidatepage() result in a huge amount of calls to |
| 4737 | * lock_extent_bits() and clear_extent_bit(), which keep merging and splitting |
| 4738 | * extent_state structures over and over, wasting lots of time. |
| 4739 | * |
| 4740 | * Therefore if the inode is being evicted, let btrfs_invalidatepage() skip all |
| 4741 | * those expensive operations on a per page basis and do only the ordered io |
| 4742 | * finishing, while we release here the extent_map and extent_state structures, |
| 4743 | * without the excessive merging and splitting. |
| 4744 | */ |
| 4745 | static void evict_inode_truncate_pages(struct inode *inode) |
| 4746 | { |
| 4747 | struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree; |
| 4748 | struct extent_map_tree *map_tree = &BTRFS_I(inode)->extent_tree; |
| 4749 | struct rb_node *node; |
| 4750 | |
| 4751 | ASSERT(inode->i_state & I_FREEING); |
Johannes Weiner | 91b0abe | 2014-04-03 14:47:49 -0700 | [diff] [blame] | 4752 | truncate_inode_pages_final(&inode->i_data); |
Filipe David Borba Manana | 131e404 | 2013-11-19 22:29:35 +0000 | [diff] [blame] | 4753 | |
| 4754 | write_lock(&map_tree->lock); |
| 4755 | while (!RB_EMPTY_ROOT(&map_tree->map)) { |
| 4756 | struct extent_map *em; |
| 4757 | |
| 4758 | node = rb_first(&map_tree->map); |
| 4759 | em = rb_entry(node, struct extent_map, rb_node); |
Wang Shilong | 180589e | 2013-12-14 15:27:31 +0800 | [diff] [blame] | 4760 | clear_bit(EXTENT_FLAG_PINNED, &em->flags); |
| 4761 | clear_bit(EXTENT_FLAG_LOGGING, &em->flags); |
Filipe David Borba Manana | 131e404 | 2013-11-19 22:29:35 +0000 | [diff] [blame] | 4762 | remove_extent_mapping(map_tree, em); |
| 4763 | free_extent_map(em); |
Filipe Manana | 7064dd5 | 2014-08-08 02:47:05 +0100 | [diff] [blame] | 4764 | if (need_resched()) { |
| 4765 | write_unlock(&map_tree->lock); |
| 4766 | cond_resched(); |
| 4767 | write_lock(&map_tree->lock); |
| 4768 | } |
Filipe David Borba Manana | 131e404 | 2013-11-19 22:29:35 +0000 | [diff] [blame] | 4769 | } |
| 4770 | write_unlock(&map_tree->lock); |
| 4771 | |
| 4772 | spin_lock(&io_tree->lock); |
| 4773 | while (!RB_EMPTY_ROOT(&io_tree->state)) { |
| 4774 | struct extent_state *state; |
| 4775 | struct extent_state *cached_state = NULL; |
| 4776 | |
| 4777 | node = rb_first(&io_tree->state); |
| 4778 | state = rb_entry(node, struct extent_state, rb_node); |
| 4779 | atomic_inc(&state->refs); |
| 4780 | spin_unlock(&io_tree->lock); |
| 4781 | |
| 4782 | lock_extent_bits(io_tree, state->start, state->end, |
| 4783 | 0, &cached_state); |
| 4784 | clear_extent_bit(io_tree, state->start, state->end, |
| 4785 | EXTENT_LOCKED | EXTENT_DIRTY | |
| 4786 | EXTENT_DELALLOC | EXTENT_DO_ACCOUNTING | |
| 4787 | EXTENT_DEFRAG, 1, 1, |
| 4788 | &cached_state, GFP_NOFS); |
| 4789 | free_extent_state(state); |
| 4790 | |
Filipe Manana | 7064dd5 | 2014-08-08 02:47:05 +0100 | [diff] [blame] | 4791 | cond_resched(); |
Filipe David Borba Manana | 131e404 | 2013-11-19 22:29:35 +0000 | [diff] [blame] | 4792 | spin_lock(&io_tree->lock); |
| 4793 | } |
| 4794 | spin_unlock(&io_tree->lock); |
| 4795 | } |
| 4796 | |
Al Viro | bd55597 | 2010-06-07 11:35:40 -0400 | [diff] [blame] | 4797 | void btrfs_evict_inode(struct inode *inode) |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4798 | { |
| 4799 | struct btrfs_trans_handle *trans; |
| 4800 | struct btrfs_root *root = BTRFS_I(inode)->root; |
Josef Bacik | 726c35f | 2011-09-26 15:46:06 -0400 | [diff] [blame] | 4801 | struct btrfs_block_rsv *rsv, *global_rsv; |
Josef Bacik | 0712718 | 2011-08-19 10:29:59 -0400 | [diff] [blame] | 4802 | u64 min_size = btrfs_calc_trunc_metadata_size(root, 1); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4803 | int ret; |
| 4804 | |
liubo | 1abe9b8 | 2011-03-24 11:18:59 +0000 | [diff] [blame] | 4805 | trace_btrfs_inode_evict(inode); |
| 4806 | |
Filipe David Borba Manana | 131e404 | 2013-11-19 22:29:35 +0000 | [diff] [blame] | 4807 | evict_inode_truncate_pages(inode); |
| 4808 | |
Stefan Behrens | 69e9c6c | 2013-09-05 16:58:43 +0200 | [diff] [blame] | 4809 | if (inode->i_nlink && |
| 4810 | ((btrfs_root_refs(&root->root_item) != 0 && |
| 4811 | root->root_key.objectid != BTRFS_ROOT_TREE_OBJECTID) || |
| 4812 | btrfs_is_free_space_inode(inode))) |
Al Viro | bd55597 | 2010-06-07 11:35:40 -0400 | [diff] [blame] | 4813 | goto no_delete; |
| 4814 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4815 | if (is_bad_inode(inode)) { |
Josef Bacik | 7b12876 | 2008-07-24 12:17:14 -0400 | [diff] [blame] | 4816 | btrfs_orphan_del(NULL, inode); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4817 | goto no_delete; |
| 4818 | } |
Al Viro | bd55597 | 2010-06-07 11:35:40 -0400 | [diff] [blame] | 4819 | /* 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] | 4820 | btrfs_wait_ordered_range(inode, 0, (u64)-1); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 4821 | |
Miao Xie | f612496 | 2014-09-12 18:44:04 +0800 | [diff] [blame] | 4822 | btrfs_free_io_failure_record(inode, 0, (u64)-1); |
| 4823 | |
Yan, Zheng | c71bf09 | 2009-11-12 09:34:40 +0000 | [diff] [blame] | 4824 | if (root->fs_info->log_root_recovering) { |
Liu Bo | 6bf0231 | 2012-06-25 21:59:09 -0600 | [diff] [blame] | 4825 | BUG_ON(test_bit(BTRFS_INODE_HAS_ORPHAN_ITEM, |
Josef Bacik | 8a35d95 | 2012-05-23 14:26:42 -0400 | [diff] [blame] | 4826 | &BTRFS_I(inode)->runtime_flags)); |
Yan, Zheng | c71bf09 | 2009-11-12 09:34:40 +0000 | [diff] [blame] | 4827 | goto no_delete; |
| 4828 | } |
| 4829 | |
Yan, Zheng | 76dda93 | 2009-09-21 16:00:26 -0400 | [diff] [blame] | 4830 | if (inode->i_nlink > 0) { |
Stefan Behrens | 69e9c6c | 2013-09-05 16:58:43 +0200 | [diff] [blame] | 4831 | BUG_ON(btrfs_root_refs(&root->root_item) != 0 && |
| 4832 | root->root_key.objectid != BTRFS_ROOT_TREE_OBJECTID); |
Yan, Zheng | 76dda93 | 2009-09-21 16:00:26 -0400 | [diff] [blame] | 4833 | goto no_delete; |
| 4834 | } |
| 4835 | |
Miao Xie | 0e8c36a | 2012-12-19 06:59:51 +0000 | [diff] [blame] | 4836 | ret = btrfs_commit_inode_delayed_inode(inode); |
| 4837 | if (ret) { |
| 4838 | btrfs_orphan_del(NULL, inode); |
| 4839 | goto no_delete; |
| 4840 | } |
| 4841 | |
Miao Xie | 66d8f3d | 2012-09-06 04:02:28 -0600 | [diff] [blame] | 4842 | rsv = btrfs_alloc_block_rsv(root, BTRFS_BLOCK_RSV_TEMP); |
Josef Bacik | 4289a66 | 2011-08-05 13:22:24 -0400 | [diff] [blame] | 4843 | if (!rsv) { |
| 4844 | btrfs_orphan_del(NULL, inode); |
| 4845 | goto no_delete; |
| 4846 | } |
Josef Bacik | 4a33854 | 2011-08-29 11:01:31 -0400 | [diff] [blame] | 4847 | rsv->size = min_size; |
Josef Bacik | ca7e70f | 2012-08-27 17:48:15 -0400 | [diff] [blame] | 4848 | rsv->failfast = 1; |
Josef Bacik | 726c35f | 2011-09-26 15:46:06 -0400 | [diff] [blame] | 4849 | global_rsv = &root->fs_info->global_block_rsv; |
Josef Bacik | 4289a66 | 2011-08-05 13:22:24 -0400 | [diff] [blame] | 4850 | |
Chris Mason | dbe674a | 2008-07-17 12:54:05 -0400 | [diff] [blame] | 4851 | btrfs_i_size_write(inode, 0); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 4852 | |
Josef Bacik | 4289a66 | 2011-08-05 13:22:24 -0400 | [diff] [blame] | 4853 | /* |
Miao Xie | 8407aa4 | 2012-09-07 01:43:32 -0600 | [diff] [blame] | 4854 | * This is a bit simpler than btrfs_truncate since we've already |
| 4855 | * reserved our space for our orphan item in the unlink, so we just |
| 4856 | * need to reserve some slack space in case we add bytes and update |
| 4857 | * inode item when doing the truncate. |
Josef Bacik | 4289a66 | 2011-08-05 13:22:24 -0400 | [diff] [blame] | 4858 | */ |
Yan, Zheng | 8082510 | 2009-11-12 09:35:36 +0000 | [diff] [blame] | 4859 | while (1) { |
Miao Xie | 08e007d | 2012-10-16 11:33:38 +0000 | [diff] [blame] | 4860 | ret = btrfs_block_rsv_refill(root, rsv, min_size, |
| 4861 | BTRFS_RESERVE_FLUSH_LIMIT); |
Yan, Zheng | 8082510 | 2009-11-12 09:35:36 +0000 | [diff] [blame] | 4862 | |
Josef Bacik | 726c35f | 2011-09-26 15:46:06 -0400 | [diff] [blame] | 4863 | /* |
| 4864 | * Try and steal from the global reserve since we will |
| 4865 | * likely not use this space anyway, we want to try as |
| 4866 | * hard as possible to get this to work. |
| 4867 | */ |
| 4868 | if (ret) |
| 4869 | ret = btrfs_block_rsv_migrate(global_rsv, rsv, min_size); |
| 4870 | |
Yan, Zheng | d68fc57 | 2010-05-16 10:49:58 -0400 | [diff] [blame] | 4871 | if (ret) { |
Simon Kirby | c2cf52e | 2013-03-19 22:41:23 +0000 | [diff] [blame] | 4872 | btrfs_warn(root->fs_info, |
| 4873 | "Could not get space for a delete, will truncate on mount %d", |
| 4874 | ret); |
Josef Bacik | 4289a66 | 2011-08-05 13:22:24 -0400 | [diff] [blame] | 4875 | btrfs_orphan_del(NULL, inode); |
| 4876 | btrfs_free_block_rsv(root, rsv); |
| 4877 | goto no_delete; |
Yan, Zheng | d68fc57 | 2010-05-16 10:49:58 -0400 | [diff] [blame] | 4878 | } |
| 4879 | |
Miao Xie | 0e8c36a | 2012-12-19 06:59:51 +0000 | [diff] [blame] | 4880 | trans = btrfs_join_transaction(root); |
Josef Bacik | 4289a66 | 2011-08-05 13:22:24 -0400 | [diff] [blame] | 4881 | if (IS_ERR(trans)) { |
| 4882 | btrfs_orphan_del(NULL, inode); |
| 4883 | btrfs_free_block_rsv(root, rsv); |
| 4884 | goto no_delete; |
| 4885 | } |
| 4886 | |
| 4887 | trans->block_rsv = rsv; |
| 4888 | |
Yan, Zheng | d68fc57 | 2010-05-16 10:49:58 -0400 | [diff] [blame] | 4889 | ret = btrfs_truncate_inode_items(trans, root, inode, 0, 0); |
Josef Bacik | ca7e70f | 2012-08-27 17:48:15 -0400 | [diff] [blame] | 4890 | if (ret != -ENOSPC) |
Yan, Zheng | 8082510 | 2009-11-12 09:35:36 +0000 | [diff] [blame] | 4891 | break; |
| 4892 | |
Miao Xie | 8407aa4 | 2012-09-07 01:43:32 -0600 | [diff] [blame] | 4893 | trans->block_rsv = &root->fs_info->trans_block_rsv; |
Yan, Zheng | 8082510 | 2009-11-12 09:35:36 +0000 | [diff] [blame] | 4894 | btrfs_end_transaction(trans, root); |
| 4895 | trans = NULL; |
Liu Bo | b53d3f5 | 2012-11-14 14:34:34 +0000 | [diff] [blame] | 4896 | btrfs_btree_balance_dirty(root); |
Josef Bacik | 7b12876 | 2008-07-24 12:17:14 -0400 | [diff] [blame] | 4897 | } |
| 4898 | |
Josef Bacik | 4289a66 | 2011-08-05 13:22:24 -0400 | [diff] [blame] | 4899 | btrfs_free_block_rsv(root, rsv); |
| 4900 | |
Josef Bacik | 4ef31a4 | 2013-08-13 14:10:08 -0400 | [diff] [blame] | 4901 | /* |
| 4902 | * Errors here aren't a big deal, it just means we leave orphan items |
| 4903 | * in the tree. They will be cleaned up on the next mount. |
| 4904 | */ |
Yan, Zheng | 8082510 | 2009-11-12 09:35:36 +0000 | [diff] [blame] | 4905 | if (ret == 0) { |
Josef Bacik | 4289a66 | 2011-08-05 13:22:24 -0400 | [diff] [blame] | 4906 | trans->block_rsv = root->orphan_block_rsv; |
Josef Bacik | 4ef31a4 | 2013-08-13 14:10:08 -0400 | [diff] [blame] | 4907 | btrfs_orphan_del(trans, inode); |
| 4908 | } else { |
| 4909 | btrfs_orphan_del(NULL, inode); |
Yan, Zheng | 8082510 | 2009-11-12 09:35:36 +0000 | [diff] [blame] | 4910 | } |
Chris Mason | 85e21ba | 2008-01-29 15:11:36 -0500 | [diff] [blame] | 4911 | |
Josef Bacik | 4289a66 | 2011-08-05 13:22:24 -0400 | [diff] [blame] | 4912 | trans->block_rsv = &root->fs_info->trans_block_rsv; |
Li Zefan | 581bb05 | 2011-04-20 10:06:11 +0800 | [diff] [blame] | 4913 | if (!(root == root->fs_info->tree_root || |
| 4914 | root->root_key.objectid == BTRFS_TREE_RELOC_OBJECTID)) |
Li Zefan | 33345d01 | 2011-04-20 10:31:50 +0800 | [diff] [blame] | 4915 | btrfs_return_ino(root, btrfs_ino(inode)); |
Li Zefan | 581bb05 | 2011-04-20 10:06:11 +0800 | [diff] [blame] | 4916 | |
Chris Mason | 54aa1f4 | 2007-06-22 14:16:25 -0400 | [diff] [blame] | 4917 | btrfs_end_transaction(trans, root); |
Liu Bo | b53d3f5 | 2012-11-14 14:34:34 +0000 | [diff] [blame] | 4918 | btrfs_btree_balance_dirty(root); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4919 | no_delete: |
Miao Xie | 89042e5 | 2013-05-15 07:48:15 +0000 | [diff] [blame] | 4920 | btrfs_remove_delayed_node(inode); |
Jan Kara | dbd5768 | 2012-05-03 14:48:02 +0200 | [diff] [blame] | 4921 | clear_inode(inode); |
Yan, Zheng | 8082510 | 2009-11-12 09:35:36 +0000 | [diff] [blame] | 4922 | return; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4923 | } |
| 4924 | |
| 4925 | /* |
| 4926 | * this returns the key found in the dir entry in the location pointer. |
| 4927 | * If no dir entries were found, location->objectid is 0. |
| 4928 | */ |
| 4929 | static int btrfs_inode_by_name(struct inode *dir, struct dentry *dentry, |
| 4930 | struct btrfs_key *location) |
| 4931 | { |
| 4932 | const char *name = dentry->d_name.name; |
| 4933 | int namelen = dentry->d_name.len; |
| 4934 | struct btrfs_dir_item *di; |
| 4935 | struct btrfs_path *path; |
| 4936 | struct btrfs_root *root = BTRFS_I(dir)->root; |
Yan | 0d9f7f3 | 2007-10-25 15:48:28 -0400 | [diff] [blame] | 4937 | int ret = 0; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4938 | |
| 4939 | path = btrfs_alloc_path(); |
Mark Fasheh | d8926bb | 2011-07-13 10:38:47 -0700 | [diff] [blame] | 4940 | if (!path) |
| 4941 | return -ENOMEM; |
Chris Mason | 3954401 | 2007-12-12 14:38:19 -0500 | [diff] [blame] | 4942 | |
Li Zefan | 33345d01 | 2011-04-20 10:31:50 +0800 | [diff] [blame] | 4943 | di = btrfs_lookup_dir_item(NULL, root, path, btrfs_ino(dir), name, |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4944 | namelen, 0); |
Yan | 0d9f7f3 | 2007-10-25 15:48:28 -0400 | [diff] [blame] | 4945 | if (IS_ERR(di)) |
| 4946 | ret = PTR_ERR(di); |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 4947 | |
David Sterba | c704005 | 2011-04-19 18:00:01 +0200 | [diff] [blame] | 4948 | if (IS_ERR_OR_NULL(di)) |
Chris Mason | 3954401 | 2007-12-12 14:38:19 -0500 | [diff] [blame] | 4949 | goto out_err; |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 4950 | |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 4951 | btrfs_dir_item_key_to_cpu(path->nodes[0], di, location); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4952 | out: |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4953 | btrfs_free_path(path); |
| 4954 | return ret; |
Chris Mason | 3954401 | 2007-12-12 14:38:19 -0500 | [diff] [blame] | 4955 | out_err: |
| 4956 | location->objectid = 0; |
| 4957 | goto out; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4958 | } |
| 4959 | |
| 4960 | /* |
| 4961 | * when we hit a tree root in a directory, the btrfs part of the inode |
| 4962 | * needs to be changed to reflect the root directory of the tree root. This |
| 4963 | * is kind of like crossing a mount point. |
| 4964 | */ |
| 4965 | static int fixup_tree_root_location(struct btrfs_root *root, |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 4966 | struct inode *dir, |
| 4967 | struct dentry *dentry, |
| 4968 | struct btrfs_key *location, |
| 4969 | struct btrfs_root **sub_root) |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4970 | { |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 4971 | struct btrfs_path *path; |
| 4972 | struct btrfs_root *new_root; |
| 4973 | struct btrfs_root_ref *ref; |
| 4974 | struct extent_buffer *leaf; |
| 4975 | int ret; |
| 4976 | int err = 0; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4977 | |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 4978 | path = btrfs_alloc_path(); |
| 4979 | if (!path) { |
| 4980 | err = -ENOMEM; |
| 4981 | goto out; |
| 4982 | } |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4983 | |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 4984 | err = -ENOENT; |
Kelley Nielsen | 75ac2dd | 2013-11-04 19:35:58 -0800 | [diff] [blame] | 4985 | ret = btrfs_find_item(root->fs_info->tree_root, path, |
| 4986 | BTRFS_I(dir)->root->root_key.objectid, |
| 4987 | location->objectid, BTRFS_ROOT_REF_KEY, NULL); |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 4988 | if (ret) { |
| 4989 | if (ret < 0) |
| 4990 | err = ret; |
| 4991 | goto out; |
| 4992 | } |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4993 | |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 4994 | leaf = path->nodes[0]; |
| 4995 | ref = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_root_ref); |
Li Zefan | 33345d01 | 2011-04-20 10:31:50 +0800 | [diff] [blame] | 4996 | if (btrfs_root_ref_dirid(leaf, ref) != btrfs_ino(dir) || |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 4997 | btrfs_root_ref_name_len(leaf, ref) != dentry->d_name.len) |
| 4998 | goto out; |
| 4999 | |
| 5000 | ret = memcmp_extent_buffer(leaf, dentry->d_name.name, |
| 5001 | (unsigned long)(ref + 1), |
| 5002 | dentry->d_name.len); |
| 5003 | if (ret) |
| 5004 | goto out; |
| 5005 | |
David Sterba | b3b4aa7 | 2011-04-21 01:20:15 +0200 | [diff] [blame] | 5006 | btrfs_release_path(path); |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 5007 | |
| 5008 | new_root = btrfs_read_fs_root_no_name(root->fs_info, location); |
| 5009 | if (IS_ERR(new_root)) { |
| 5010 | err = PTR_ERR(new_root); |
| 5011 | goto out; |
| 5012 | } |
| 5013 | |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 5014 | *sub_root = new_root; |
| 5015 | location->objectid = btrfs_root_dirid(&new_root->root_item); |
| 5016 | location->type = BTRFS_INODE_ITEM_KEY; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5017 | location->offset = 0; |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 5018 | err = 0; |
| 5019 | out: |
| 5020 | btrfs_free_path(path); |
| 5021 | return err; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5022 | } |
| 5023 | |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 5024 | static void inode_tree_add(struct inode *inode) |
| 5025 | { |
| 5026 | struct btrfs_root *root = BTRFS_I(inode)->root; |
| 5027 | struct btrfs_inode *entry; |
From: Nick Piggin | 03e860b | 2009-08-21 10:09:44 +0200 | [diff] [blame] | 5028 | struct rb_node **p; |
| 5029 | struct rb_node *parent; |
Filipe David Borba Manana | cef2193 | 2013-09-02 12:19:13 +0100 | [diff] [blame] | 5030 | struct rb_node *new = &BTRFS_I(inode)->rb_node; |
Li Zefan | 33345d01 | 2011-04-20 10:31:50 +0800 | [diff] [blame] | 5031 | u64 ino = btrfs_ino(inode); |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 5032 | |
Al Viro | 1d3382cb | 2010-10-23 15:19:20 -0400 | [diff] [blame] | 5033 | if (inode_unhashed(inode)) |
Yan, Zheng | 76dda93 | 2009-09-21 16:00:26 -0400 | [diff] [blame] | 5034 | return; |
Miao Xie | e1409ce | 2013-05-15 07:48:16 +0000 | [diff] [blame] | 5035 | parent = NULL; |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 5036 | spin_lock(&root->inode_lock); |
Miao Xie | e1409ce | 2013-05-15 07:48:16 +0000 | [diff] [blame] | 5037 | p = &root->inode_tree.rb_node; |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 5038 | while (*p) { |
| 5039 | parent = *p; |
| 5040 | entry = rb_entry(parent, struct btrfs_inode, rb_node); |
| 5041 | |
Li Zefan | 33345d01 | 2011-04-20 10:31:50 +0800 | [diff] [blame] | 5042 | if (ino < btrfs_ino(&entry->vfs_inode)) |
From: Nick Piggin | 03e860b | 2009-08-21 10:09:44 +0200 | [diff] [blame] | 5043 | p = &parent->rb_left; |
Li Zefan | 33345d01 | 2011-04-20 10:31:50 +0800 | [diff] [blame] | 5044 | else if (ino > btrfs_ino(&entry->vfs_inode)) |
From: Nick Piggin | 03e860b | 2009-08-21 10:09:44 +0200 | [diff] [blame] | 5045 | p = &parent->rb_right; |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 5046 | else { |
| 5047 | WARN_ON(!(entry->vfs_inode.i_state & |
Al Viro | a4ffdde | 2010-06-02 17:38:30 -0400 | [diff] [blame] | 5048 | (I_WILL_FREE | I_FREEING))); |
Filipe David Borba Manana | cef2193 | 2013-09-02 12:19:13 +0100 | [diff] [blame] | 5049 | rb_replace_node(parent, new, &root->inode_tree); |
From: Nick Piggin | 03e860b | 2009-08-21 10:09:44 +0200 | [diff] [blame] | 5050 | RB_CLEAR_NODE(parent); |
| 5051 | spin_unlock(&root->inode_lock); |
Filipe David Borba Manana | cef2193 | 2013-09-02 12:19:13 +0100 | [diff] [blame] | 5052 | return; |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 5053 | } |
| 5054 | } |
Filipe David Borba Manana | cef2193 | 2013-09-02 12:19:13 +0100 | [diff] [blame] | 5055 | rb_link_node(new, parent, p); |
| 5056 | rb_insert_color(new, &root->inode_tree); |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 5057 | spin_unlock(&root->inode_lock); |
| 5058 | } |
| 5059 | |
| 5060 | static void inode_tree_del(struct inode *inode) |
| 5061 | { |
| 5062 | struct btrfs_root *root = BTRFS_I(inode)->root; |
Yan, Zheng | 76dda93 | 2009-09-21 16:00:26 -0400 | [diff] [blame] | 5063 | int empty = 0; |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 5064 | |
From: Nick Piggin | 03e860b | 2009-08-21 10:09:44 +0200 | [diff] [blame] | 5065 | spin_lock(&root->inode_lock); |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 5066 | if (!RB_EMPTY_NODE(&BTRFS_I(inode)->rb_node)) { |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 5067 | rb_erase(&BTRFS_I(inode)->rb_node, &root->inode_tree); |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 5068 | RB_CLEAR_NODE(&BTRFS_I(inode)->rb_node); |
Yan, Zheng | 76dda93 | 2009-09-21 16:00:26 -0400 | [diff] [blame] | 5069 | empty = RB_EMPTY_ROOT(&root->inode_tree); |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 5070 | } |
From: Nick Piggin | 03e860b | 2009-08-21 10:09:44 +0200 | [diff] [blame] | 5071 | spin_unlock(&root->inode_lock); |
Yan, Zheng | 76dda93 | 2009-09-21 16:00:26 -0400 | [diff] [blame] | 5072 | |
Stefan Behrens | 69e9c6c | 2013-09-05 16:58:43 +0200 | [diff] [blame] | 5073 | if (empty && btrfs_root_refs(&root->root_item) == 0) { |
Yan, Zheng | 76dda93 | 2009-09-21 16:00:26 -0400 | [diff] [blame] | 5074 | synchronize_srcu(&root->fs_info->subvol_srcu); |
| 5075 | spin_lock(&root->inode_lock); |
| 5076 | empty = RB_EMPTY_ROOT(&root->inode_tree); |
| 5077 | spin_unlock(&root->inode_lock); |
| 5078 | if (empty) |
| 5079 | btrfs_add_dead_root(root); |
| 5080 | } |
| 5081 | } |
| 5082 | |
Jeff Mahoney | 143bede | 2012-03-01 14:56:26 +0100 | [diff] [blame] | 5083 | void btrfs_invalidate_inodes(struct btrfs_root *root) |
Yan, Zheng | 76dda93 | 2009-09-21 16:00:26 -0400 | [diff] [blame] | 5084 | { |
| 5085 | struct rb_node *node; |
| 5086 | struct rb_node *prev; |
| 5087 | struct btrfs_inode *entry; |
| 5088 | struct inode *inode; |
| 5089 | u64 objectid = 0; |
| 5090 | |
Liu Bo | 7813b3d | 2014-02-10 17:37:25 +0800 | [diff] [blame] | 5091 | if (!test_bit(BTRFS_FS_STATE_ERROR, &root->fs_info->fs_state)) |
| 5092 | WARN_ON(btrfs_root_refs(&root->root_item) != 0); |
Yan, Zheng | 76dda93 | 2009-09-21 16:00:26 -0400 | [diff] [blame] | 5093 | |
| 5094 | spin_lock(&root->inode_lock); |
| 5095 | again: |
| 5096 | node = root->inode_tree.rb_node; |
| 5097 | prev = NULL; |
| 5098 | while (node) { |
| 5099 | prev = node; |
| 5100 | entry = rb_entry(node, struct btrfs_inode, rb_node); |
| 5101 | |
Li Zefan | 33345d01 | 2011-04-20 10:31:50 +0800 | [diff] [blame] | 5102 | if (objectid < btrfs_ino(&entry->vfs_inode)) |
Yan, Zheng | 76dda93 | 2009-09-21 16:00:26 -0400 | [diff] [blame] | 5103 | node = node->rb_left; |
Li Zefan | 33345d01 | 2011-04-20 10:31:50 +0800 | [diff] [blame] | 5104 | else if (objectid > btrfs_ino(&entry->vfs_inode)) |
Yan, Zheng | 76dda93 | 2009-09-21 16:00:26 -0400 | [diff] [blame] | 5105 | node = node->rb_right; |
| 5106 | else |
| 5107 | break; |
| 5108 | } |
| 5109 | if (!node) { |
| 5110 | while (prev) { |
| 5111 | entry = rb_entry(prev, struct btrfs_inode, rb_node); |
Li Zefan | 33345d01 | 2011-04-20 10:31:50 +0800 | [diff] [blame] | 5112 | if (objectid <= btrfs_ino(&entry->vfs_inode)) { |
Yan, Zheng | 76dda93 | 2009-09-21 16:00:26 -0400 | [diff] [blame] | 5113 | node = prev; |
| 5114 | break; |
| 5115 | } |
| 5116 | prev = rb_next(prev); |
| 5117 | } |
| 5118 | } |
| 5119 | while (node) { |
| 5120 | entry = rb_entry(node, struct btrfs_inode, rb_node); |
Li Zefan | 33345d01 | 2011-04-20 10:31:50 +0800 | [diff] [blame] | 5121 | objectid = btrfs_ino(&entry->vfs_inode) + 1; |
Yan, Zheng | 76dda93 | 2009-09-21 16:00:26 -0400 | [diff] [blame] | 5122 | inode = igrab(&entry->vfs_inode); |
| 5123 | if (inode) { |
| 5124 | spin_unlock(&root->inode_lock); |
| 5125 | if (atomic_read(&inode->i_count) > 1) |
| 5126 | d_prune_aliases(inode); |
| 5127 | /* |
Al Viro | 45321ac | 2010-06-07 13:43:19 -0400 | [diff] [blame] | 5128 | * btrfs_drop_inode will have it removed from |
Yan, Zheng | 76dda93 | 2009-09-21 16:00:26 -0400 | [diff] [blame] | 5129 | * the inode cache when its usage count |
| 5130 | * hits zero. |
| 5131 | */ |
| 5132 | iput(inode); |
| 5133 | cond_resched(); |
| 5134 | spin_lock(&root->inode_lock); |
| 5135 | goto again; |
| 5136 | } |
| 5137 | |
| 5138 | if (cond_resched_lock(&root->inode_lock)) |
| 5139 | goto again; |
| 5140 | |
| 5141 | node = rb_next(node); |
| 5142 | } |
| 5143 | spin_unlock(&root->inode_lock); |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 5144 | } |
| 5145 | |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 5146 | static int btrfs_init_locked_inode(struct inode *inode, void *p) |
| 5147 | { |
| 5148 | struct btrfs_iget_args *args = p; |
Chris Mason | 90d3e59 | 2014-01-09 17:28:00 -0800 | [diff] [blame] | 5149 | inode->i_ino = args->location->objectid; |
| 5150 | memcpy(&BTRFS_I(inode)->location, args->location, |
| 5151 | sizeof(*args->location)); |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 5152 | BTRFS_I(inode)->root = args->root; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5153 | return 0; |
| 5154 | } |
| 5155 | |
| 5156 | static int btrfs_find_actor(struct inode *inode, void *opaque) |
| 5157 | { |
| 5158 | struct btrfs_iget_args *args = opaque; |
Chris Mason | 90d3e59 | 2014-01-09 17:28:00 -0800 | [diff] [blame] | 5159 | return args->location->objectid == BTRFS_I(inode)->location.objectid && |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 5160 | args->root == BTRFS_I(inode)->root; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5161 | } |
| 5162 | |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 5163 | static struct inode *btrfs_iget_locked(struct super_block *s, |
Chris Mason | 90d3e59 | 2014-01-09 17:28:00 -0800 | [diff] [blame] | 5164 | struct btrfs_key *location, |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 5165 | struct btrfs_root *root) |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5166 | { |
| 5167 | struct inode *inode; |
| 5168 | struct btrfs_iget_args args; |
Chris Mason | 90d3e59 | 2014-01-09 17:28:00 -0800 | [diff] [blame] | 5169 | unsigned long hashval = btrfs_inode_hash(location->objectid, root); |
Filipe David Borba Manana | 778ba82 | 2013-10-06 22:22:33 +0100 | [diff] [blame] | 5170 | |
Chris Mason | 90d3e59 | 2014-01-09 17:28:00 -0800 | [diff] [blame] | 5171 | args.location = location; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5172 | args.root = root; |
| 5173 | |
Filipe David Borba Manana | 778ba82 | 2013-10-06 22:22:33 +0100 | [diff] [blame] | 5174 | inode = iget5_locked(s, hashval, btrfs_find_actor, |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5175 | btrfs_init_locked_inode, |
| 5176 | (void *)&args); |
| 5177 | return inode; |
| 5178 | } |
| 5179 | |
Balaji Rao | 1a54ef8 | 2008-07-21 02:01:04 +0530 | [diff] [blame] | 5180 | /* Get an inode object given its location and corresponding root. |
| 5181 | * Returns in *is_new if the inode was read from disk |
| 5182 | */ |
| 5183 | struct inode *btrfs_iget(struct super_block *s, struct btrfs_key *location, |
Josef Bacik | 73f7341 | 2009-12-04 17:38:27 +0000 | [diff] [blame] | 5184 | struct btrfs_root *root, int *new) |
Balaji Rao | 1a54ef8 | 2008-07-21 02:01:04 +0530 | [diff] [blame] | 5185 | { |
| 5186 | struct inode *inode; |
| 5187 | |
Chris Mason | 90d3e59 | 2014-01-09 17:28:00 -0800 | [diff] [blame] | 5188 | inode = btrfs_iget_locked(s, location, root); |
Balaji Rao | 1a54ef8 | 2008-07-21 02:01:04 +0530 | [diff] [blame] | 5189 | if (!inode) |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 5190 | return ERR_PTR(-ENOMEM); |
Balaji Rao | 1a54ef8 | 2008-07-21 02:01:04 +0530 | [diff] [blame] | 5191 | |
| 5192 | if (inode->i_state & I_NEW) { |
Balaji Rao | 1a54ef8 | 2008-07-21 02:01:04 +0530 | [diff] [blame] | 5193 | btrfs_read_locked_inode(inode); |
Mark Fasheh | 1748f84 | 2011-07-12 11:25:31 -0700 | [diff] [blame] | 5194 | if (!is_bad_inode(inode)) { |
| 5195 | inode_tree_add(inode); |
| 5196 | unlock_new_inode(inode); |
| 5197 | if (new) |
| 5198 | *new = 1; |
| 5199 | } else { |
Sergei Trofimovich | e0b6d65 | 2011-09-11 10:52:24 -0400 | [diff] [blame] | 5200 | unlock_new_inode(inode); |
| 5201 | iput(inode); |
| 5202 | inode = ERR_PTR(-ESTALE); |
Mark Fasheh | 1748f84 | 2011-07-12 11:25:31 -0700 | [diff] [blame] | 5203 | } |
| 5204 | } |
| 5205 | |
Balaji Rao | 1a54ef8 | 2008-07-21 02:01:04 +0530 | [diff] [blame] | 5206 | return inode; |
| 5207 | } |
| 5208 | |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 5209 | static struct inode *new_simple_dir(struct super_block *s, |
| 5210 | struct btrfs_key *key, |
| 5211 | struct btrfs_root *root) |
| 5212 | { |
| 5213 | struct inode *inode = new_inode(s); |
| 5214 | |
| 5215 | if (!inode) |
| 5216 | return ERR_PTR(-ENOMEM); |
| 5217 | |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 5218 | BTRFS_I(inode)->root = root; |
| 5219 | memcpy(&BTRFS_I(inode)->location, key, sizeof(*key)); |
Josef Bacik | 72ac3c0 | 2012-05-23 14:13:11 -0400 | [diff] [blame] | 5220 | set_bit(BTRFS_INODE_DUMMY, &BTRFS_I(inode)->runtime_flags); |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 5221 | |
| 5222 | inode->i_ino = BTRFS_EMPTY_SUBVOL_DIR_OBJECTID; |
Li Zefan | 848cce0 | 2012-02-21 17:04:28 +0800 | [diff] [blame] | 5223 | inode->i_op = &btrfs_dir_ro_inode_operations; |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 5224 | inode->i_fop = &simple_dir_operations; |
| 5225 | inode->i_mode = S_IFDIR | S_IRUGO | S_IWUSR | S_IXUGO; |
| 5226 | inode->i_mtime = inode->i_atime = inode->i_ctime = CURRENT_TIME; |
| 5227 | |
| 5228 | return inode; |
| 5229 | } |
| 5230 | |
Chris Mason | 3de4586 | 2008-11-17 21:02:50 -0500 | [diff] [blame] | 5231 | struct inode *btrfs_lookup_dentry(struct inode *dir, struct dentry *dentry) |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5232 | { |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 5233 | struct inode *inode; |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 5234 | struct btrfs_root *root = BTRFS_I(dir)->root; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5235 | struct btrfs_root *sub_root = root; |
| 5236 | struct btrfs_key location; |
Yan, Zheng | 76dda93 | 2009-09-21 16:00:26 -0400 | [diff] [blame] | 5237 | int index; |
Josef Bacik | b4aff1f | 2011-06-28 16:18:59 -0400 | [diff] [blame] | 5238 | int ret = 0; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5239 | |
| 5240 | if (dentry->d_name.len > BTRFS_NAME_LEN) |
| 5241 | return ERR_PTR(-ENAMETOOLONG); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 5242 | |
Jeff Layton | 39e3c95 | 2012-11-28 11:30:53 -0500 | [diff] [blame] | 5243 | ret = btrfs_inode_by_name(dir, dentry, &location); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5244 | if (ret < 0) |
| 5245 | return ERR_PTR(ret); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 5246 | |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 5247 | if (location.objectid == 0) |
Tsutomu Itoh | 5662344b3 | 2013-12-13 09:51:42 +0900 | [diff] [blame] | 5248 | return ERR_PTR(-ENOENT); |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 5249 | |
| 5250 | if (location.type == BTRFS_INODE_ITEM_KEY) { |
Josef Bacik | 73f7341 | 2009-12-04 17:38:27 +0000 | [diff] [blame] | 5251 | inode = btrfs_iget(dir->i_sb, &location, root, NULL); |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 5252 | return inode; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5253 | } |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 5254 | |
| 5255 | BUG_ON(location.type != BTRFS_ROOT_ITEM_KEY); |
| 5256 | |
Yan, Zheng | 76dda93 | 2009-09-21 16:00:26 -0400 | [diff] [blame] | 5257 | index = srcu_read_lock(&root->fs_info->subvol_srcu); |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 5258 | ret = fixup_tree_root_location(root, dir, dentry, |
| 5259 | &location, &sub_root); |
| 5260 | if (ret < 0) { |
| 5261 | if (ret != -ENOENT) |
| 5262 | inode = ERR_PTR(ret); |
| 5263 | else |
| 5264 | inode = new_simple_dir(dir->i_sb, &location, sub_root); |
| 5265 | } else { |
Josef Bacik | 73f7341 | 2009-12-04 17:38:27 +0000 | [diff] [blame] | 5266 | inode = btrfs_iget(dir->i_sb, &location, sub_root, NULL); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5267 | } |
Yan, Zheng | 76dda93 | 2009-09-21 16:00:26 -0400 | [diff] [blame] | 5268 | srcu_read_unlock(&root->fs_info->subvol_srcu, index); |
| 5269 | |
Julia Lawall | 34d19ba | 2011-01-24 19:55:19 +0000 | [diff] [blame] | 5270 | if (!IS_ERR(inode) && root != sub_root) { |
Yan, Zheng | c71bf09 | 2009-11-12 09:34:40 +0000 | [diff] [blame] | 5271 | down_read(&root->fs_info->cleanup_work_sem); |
| 5272 | if (!(inode->i_sb->s_flags & MS_RDONLY)) |
Josef Bacik | 66b4ffd | 2011-01-31 16:22:42 -0500 | [diff] [blame] | 5273 | ret = btrfs_orphan_cleanup(sub_root); |
Yan, Zheng | c71bf09 | 2009-11-12 09:34:40 +0000 | [diff] [blame] | 5274 | up_read(&root->fs_info->cleanup_work_sem); |
Josef Bacik | 01cd336 | 2013-06-03 21:39:49 -0400 | [diff] [blame] | 5275 | if (ret) { |
| 5276 | iput(inode); |
Josef Bacik | 66b4ffd | 2011-01-31 16:22:42 -0500 | [diff] [blame] | 5277 | inode = ERR_PTR(ret); |
Josef Bacik | 01cd336 | 2013-06-03 21:39:49 -0400 | [diff] [blame] | 5278 | } |
Yan, Zheng | c71bf09 | 2009-11-12 09:34:40 +0000 | [diff] [blame] | 5279 | } |
| 5280 | |
Chris Mason | 3de4586 | 2008-11-17 21:02:50 -0500 | [diff] [blame] | 5281 | return inode; |
| 5282 | } |
| 5283 | |
Nick Piggin | fe15ce4 | 2011-01-07 17:49:23 +1100 | [diff] [blame] | 5284 | static int btrfs_dentry_delete(const struct dentry *dentry) |
Yan, Zheng | 76dda93 | 2009-09-21 16:00:26 -0400 | [diff] [blame] | 5285 | { |
| 5286 | struct btrfs_root *root; |
Li Zefan | 848cce0 | 2012-02-21 17:04:28 +0800 | [diff] [blame] | 5287 | struct inode *inode = dentry->d_inode; |
Yan, Zheng | 76dda93 | 2009-09-21 16:00:26 -0400 | [diff] [blame] | 5288 | |
Li Zefan | 848cce0 | 2012-02-21 17:04:28 +0800 | [diff] [blame] | 5289 | if (!inode && !IS_ROOT(dentry)) |
| 5290 | inode = dentry->d_parent->d_inode; |
Yan, Zheng | 76dda93 | 2009-09-21 16:00:26 -0400 | [diff] [blame] | 5291 | |
Li Zefan | 848cce0 | 2012-02-21 17:04:28 +0800 | [diff] [blame] | 5292 | if (inode) { |
| 5293 | root = BTRFS_I(inode)->root; |
Yan, Zheng | efefb14 | 2009-10-09 09:25:16 -0400 | [diff] [blame] | 5294 | if (btrfs_root_refs(&root->root_item) == 0) |
| 5295 | return 1; |
Li Zefan | 848cce0 | 2012-02-21 17:04:28 +0800 | [diff] [blame] | 5296 | |
| 5297 | if (btrfs_ino(inode) == BTRFS_EMPTY_SUBVOL_DIR_OBJECTID) |
| 5298 | return 1; |
Yan, Zheng | efefb14 | 2009-10-09 09:25:16 -0400 | [diff] [blame] | 5299 | } |
Yan, Zheng | 76dda93 | 2009-09-21 16:00:26 -0400 | [diff] [blame] | 5300 | return 0; |
| 5301 | } |
| 5302 | |
Josef Bacik | b4aff1f | 2011-06-28 16:18:59 -0400 | [diff] [blame] | 5303 | static void btrfs_dentry_release(struct dentry *dentry) |
| 5304 | { |
Daeseok Youn | 944a451 | 2014-04-14 15:37:02 +0900 | [diff] [blame] | 5305 | kfree(dentry->d_fsdata); |
Josef Bacik | b4aff1f | 2011-06-28 16:18:59 -0400 | [diff] [blame] | 5306 | } |
| 5307 | |
Chris Mason | 3de4586 | 2008-11-17 21:02:50 -0500 | [diff] [blame] | 5308 | static struct dentry *btrfs_lookup(struct inode *dir, struct dentry *dentry, |
Al Viro | 00cd8dd | 2012-06-10 17:13:09 -0400 | [diff] [blame] | 5309 | unsigned int flags) |
Chris Mason | 3de4586 | 2008-11-17 21:02:50 -0500 | [diff] [blame] | 5310 | { |
Tsutomu Itoh | 5662344b3 | 2013-12-13 09:51:42 +0900 | [diff] [blame] | 5311 | struct inode *inode; |
Josef Bacik | a66e7cc | 2011-09-18 10:34:03 -0400 | [diff] [blame] | 5312 | |
Tsutomu Itoh | 5662344b3 | 2013-12-13 09:51:42 +0900 | [diff] [blame] | 5313 | inode = btrfs_lookup_dentry(dir, dentry); |
| 5314 | if (IS_ERR(inode)) { |
| 5315 | if (PTR_ERR(inode) == -ENOENT) |
| 5316 | inode = NULL; |
| 5317 | else |
| 5318 | return ERR_CAST(inode); |
| 5319 | } |
| 5320 | |
Josef Bacik | 3a0dfa6 | 2014-02-14 13:43:48 -0500 | [diff] [blame] | 5321 | return d_materialise_unique(dentry, inode); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5322 | } |
| 5323 | |
Miao Xie | 16cdcec | 2011-04-22 18:12:22 +0800 | [diff] [blame] | 5324 | unsigned char btrfs_filetype_table[] = { |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5325 | DT_UNKNOWN, DT_REG, DT_DIR, DT_CHR, DT_BLK, DT_FIFO, DT_SOCK, DT_LNK |
| 5326 | }; |
| 5327 | |
Al Viro | 9cdda8d | 2013-05-22 16:48:09 -0400 | [diff] [blame] | 5328 | static int btrfs_real_readdir(struct file *file, struct dir_context *ctx) |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5329 | { |
Al Viro | 9cdda8d | 2013-05-22 16:48:09 -0400 | [diff] [blame] | 5330 | struct inode *inode = file_inode(file); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5331 | struct btrfs_root *root = BTRFS_I(inode)->root; |
| 5332 | struct btrfs_item *item; |
| 5333 | struct btrfs_dir_item *di; |
| 5334 | struct btrfs_key key; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 5335 | struct btrfs_key found_key; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5336 | struct btrfs_path *path; |
Miao Xie | 16cdcec | 2011-04-22 18:12:22 +0800 | [diff] [blame] | 5337 | struct list_head ins_list; |
| 5338 | struct list_head del_list; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5339 | int ret; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 5340 | struct extent_buffer *leaf; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5341 | int slot; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5342 | unsigned char d_type; |
| 5343 | int over = 0; |
| 5344 | u32 di_cur; |
| 5345 | u32 di_total; |
| 5346 | u32 di_len; |
| 5347 | int key_type = BTRFS_DIR_INDEX_KEY; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 5348 | char tmp_name[32]; |
| 5349 | char *name_ptr; |
| 5350 | int name_len; |
Al Viro | 9cdda8d | 2013-05-22 16:48:09 -0400 | [diff] [blame] | 5351 | int is_curr = 0; /* ctx->pos points to the current index? */ |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5352 | |
| 5353 | /* FIXME, use a real flag for deciding about the key type */ |
| 5354 | if (root->fs_info->tree_root == root) |
| 5355 | key_type = BTRFS_DIR_ITEM_KEY; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 5356 | |
Al Viro | 9cdda8d | 2013-05-22 16:48:09 -0400 | [diff] [blame] | 5357 | if (!dir_emit_dots(file, ctx)) |
| 5358 | return 0; |
| 5359 | |
David Woodhouse | 49593bf | 2008-08-17 17:08:36 +0100 | [diff] [blame] | 5360 | path = btrfs_alloc_path(); |
Miao Xie | 16cdcec | 2011-04-22 18:12:22 +0800 | [diff] [blame] | 5361 | if (!path) |
| 5362 | return -ENOMEM; |
Chris Mason | ff5714c | 2011-05-28 07:00:39 -0400 | [diff] [blame] | 5363 | |
Josef Bacik | 026fd31 | 2011-05-13 10:32:11 -0400 | [diff] [blame] | 5364 | path->reada = 1; |
David Woodhouse | 49593bf | 2008-08-17 17:08:36 +0100 | [diff] [blame] | 5365 | |
Miao Xie | 16cdcec | 2011-04-22 18:12:22 +0800 | [diff] [blame] | 5366 | if (key_type == BTRFS_DIR_INDEX_KEY) { |
| 5367 | INIT_LIST_HEAD(&ins_list); |
| 5368 | INIT_LIST_HEAD(&del_list); |
| 5369 | btrfs_get_delayed_items(inode, &ins_list, &del_list); |
| 5370 | } |
| 5371 | |
David Sterba | 962a298 | 2014-06-04 18:41:45 +0200 | [diff] [blame] | 5372 | key.type = key_type; |
Al Viro | 9cdda8d | 2013-05-22 16:48:09 -0400 | [diff] [blame] | 5373 | key.offset = ctx->pos; |
Li Zefan | 33345d01 | 2011-04-20 10:31:50 +0800 | [diff] [blame] | 5374 | key.objectid = btrfs_ino(inode); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 5375 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5376 | ret = btrfs_search_slot(NULL, root, &key, path, 0, 0); |
| 5377 | if (ret < 0) |
| 5378 | goto err; |
David Woodhouse | 49593bf | 2008-08-17 17:08:36 +0100 | [diff] [blame] | 5379 | |
| 5380 | while (1) { |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 5381 | leaf = path->nodes[0]; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5382 | slot = path->slots[0]; |
Li Zefan | b9e03af | 2011-03-23 10:43:58 +0800 | [diff] [blame] | 5383 | if (slot >= btrfs_header_nritems(leaf)) { |
| 5384 | ret = btrfs_next_leaf(root, path); |
| 5385 | if (ret < 0) |
| 5386 | goto err; |
| 5387 | else if (ret > 0) |
| 5388 | break; |
| 5389 | continue; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5390 | } |
Chris Mason | 3de4586 | 2008-11-17 21:02:50 -0500 | [diff] [blame] | 5391 | |
Ross Kirk | dd3cc16 | 2013-09-16 15:58:09 +0100 | [diff] [blame] | 5392 | item = btrfs_item_nr(slot); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 5393 | btrfs_item_key_to_cpu(leaf, &found_key, slot); |
| 5394 | |
| 5395 | if (found_key.objectid != key.objectid) |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5396 | break; |
David Sterba | 962a298 | 2014-06-04 18:41:45 +0200 | [diff] [blame] | 5397 | if (found_key.type != key_type) |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5398 | break; |
Al Viro | 9cdda8d | 2013-05-22 16:48:09 -0400 | [diff] [blame] | 5399 | if (found_key.offset < ctx->pos) |
Li Zefan | b9e03af | 2011-03-23 10:43:58 +0800 | [diff] [blame] | 5400 | goto next; |
Miao Xie | 16cdcec | 2011-04-22 18:12:22 +0800 | [diff] [blame] | 5401 | if (key_type == BTRFS_DIR_INDEX_KEY && |
| 5402 | btrfs_should_delete_dir_index(&del_list, |
| 5403 | found_key.offset)) |
| 5404 | goto next; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 5405 | |
Al Viro | 9cdda8d | 2013-05-22 16:48:09 -0400 | [diff] [blame] | 5406 | ctx->pos = found_key.offset; |
Miao Xie | 16cdcec | 2011-04-22 18:12:22 +0800 | [diff] [blame] | 5407 | is_curr = 1; |
David Woodhouse | 49593bf | 2008-08-17 17:08:36 +0100 | [diff] [blame] | 5408 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5409 | di = btrfs_item_ptr(leaf, slot, struct btrfs_dir_item); |
| 5410 | di_cur = 0; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 5411 | di_total = btrfs_item_size(leaf, item); |
David Woodhouse | 49593bf | 2008-08-17 17:08:36 +0100 | [diff] [blame] | 5412 | |
| 5413 | while (di_cur < di_total) { |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 5414 | struct btrfs_key location; |
| 5415 | |
Josef Bacik | 22a94d4 | 2011-03-16 16:47:17 -0400 | [diff] [blame] | 5416 | if (verify_dir_item(root, leaf, di)) |
| 5417 | break; |
| 5418 | |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 5419 | name_len = btrfs_dir_name_len(leaf, di); |
David Woodhouse | 49593bf | 2008-08-17 17:08:36 +0100 | [diff] [blame] | 5420 | if (name_len <= sizeof(tmp_name)) { |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 5421 | name_ptr = tmp_name; |
| 5422 | } else { |
| 5423 | name_ptr = kmalloc(name_len, GFP_NOFS); |
David Woodhouse | 49593bf | 2008-08-17 17:08:36 +0100 | [diff] [blame] | 5424 | if (!name_ptr) { |
| 5425 | ret = -ENOMEM; |
| 5426 | goto err; |
| 5427 | } |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 5428 | } |
| 5429 | read_extent_buffer(leaf, name_ptr, |
| 5430 | (unsigned long)(di + 1), name_len); |
| 5431 | |
| 5432 | d_type = btrfs_filetype_table[btrfs_dir_type(leaf, di)]; |
| 5433 | btrfs_dir_item_key_to_cpu(leaf, di, &location); |
Chris Mason | 3de4586 | 2008-11-17 21:02:50 -0500 | [diff] [blame] | 5434 | |
Josef Bacik | b4aff1f | 2011-06-28 16:18:59 -0400 | [diff] [blame] | 5435 | |
Chris Mason | 3de4586 | 2008-11-17 21:02:50 -0500 | [diff] [blame] | 5436 | /* is this a reference to our own snapshot? If so |
Arne Jansen | 8c9c2bf | 2012-02-25 09:09:30 +0100 | [diff] [blame] | 5437 | * skip it. |
| 5438 | * |
| 5439 | * In contrast to old kernels, we insert the snapshot's |
| 5440 | * dir item and dir index after it has been created, so |
| 5441 | * we won't find a reference to our own snapshot. We |
| 5442 | * still keep the following code for backward |
| 5443 | * compatibility. |
Chris Mason | 3de4586 | 2008-11-17 21:02:50 -0500 | [diff] [blame] | 5444 | */ |
| 5445 | if (location.type == BTRFS_ROOT_ITEM_KEY && |
| 5446 | location.objectid == root->root_key.objectid) { |
| 5447 | over = 0; |
| 5448 | goto skip; |
| 5449 | } |
Al Viro | 9cdda8d | 2013-05-22 16:48:09 -0400 | [diff] [blame] | 5450 | over = !dir_emit(ctx, name_ptr, name_len, |
| 5451 | location.objectid, d_type); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 5452 | |
Chris Mason | 3de4586 | 2008-11-17 21:02:50 -0500 | [diff] [blame] | 5453 | skip: |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 5454 | if (name_ptr != tmp_name) |
| 5455 | kfree(name_ptr); |
| 5456 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5457 | if (over) |
| 5458 | goto nopos; |
Josef Bacik | 5103e94 | 2007-11-16 11:45:54 -0500 | [diff] [blame] | 5459 | di_len = btrfs_dir_name_len(leaf, di) + |
David Woodhouse | 49593bf | 2008-08-17 17:08:36 +0100 | [diff] [blame] | 5460 | btrfs_dir_data_len(leaf, di) + sizeof(*di); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5461 | di_cur += di_len; |
| 5462 | di = (struct btrfs_dir_item *)((char *)di + di_len); |
| 5463 | } |
Li Zefan | b9e03af | 2011-03-23 10:43:58 +0800 | [diff] [blame] | 5464 | next: |
| 5465 | path->slots[0]++; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5466 | } |
David Woodhouse | 49593bf | 2008-08-17 17:08:36 +0100 | [diff] [blame] | 5467 | |
Miao Xie | 16cdcec | 2011-04-22 18:12:22 +0800 | [diff] [blame] | 5468 | if (key_type == BTRFS_DIR_INDEX_KEY) { |
| 5469 | if (is_curr) |
Al Viro | 9cdda8d | 2013-05-22 16:48:09 -0400 | [diff] [blame] | 5470 | ctx->pos++; |
| 5471 | ret = btrfs_readdir_delayed_dir_index(ctx, &ins_list); |
Miao Xie | 16cdcec | 2011-04-22 18:12:22 +0800 | [diff] [blame] | 5472 | if (ret) |
| 5473 | goto nopos; |
| 5474 | } |
| 5475 | |
David Woodhouse | 49593bf | 2008-08-17 17:08:36 +0100 | [diff] [blame] | 5476 | /* Reached end of directory/root. Bump pos past the last item. */ |
Zach Brown | db62efb | 2013-07-11 16:19:42 -0700 | [diff] [blame] | 5477 | ctx->pos++; |
| 5478 | |
| 5479 | /* |
| 5480 | * Stop new entries from being returned after we return the last |
| 5481 | * entry. |
| 5482 | * |
| 5483 | * New directory entries are assigned a strictly increasing |
| 5484 | * offset. This means that new entries created during readdir |
| 5485 | * are *guaranteed* to be seen in the future by that readdir. |
| 5486 | * This has broken buggy programs which operate on names as |
| 5487 | * they're returned by readdir. Until we re-use freed offsets |
| 5488 | * we have this hack to stop new entries from being returned |
| 5489 | * under the assumption that they'll never reach this huge |
| 5490 | * offset. |
| 5491 | * |
| 5492 | * This is being careful not to overflow 32bit loff_t unless the |
| 5493 | * last entry requires it because doing so has broken 32bit apps |
| 5494 | * in the past. |
| 5495 | */ |
| 5496 | if (key_type == BTRFS_DIR_INDEX_KEY) { |
| 5497 | if (ctx->pos >= INT_MAX) |
| 5498 | ctx->pos = LLONG_MAX; |
| 5499 | else |
| 5500 | ctx->pos = INT_MAX; |
| 5501 | } |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5502 | nopos: |
| 5503 | ret = 0; |
| 5504 | err: |
Miao Xie | 16cdcec | 2011-04-22 18:12:22 +0800 | [diff] [blame] | 5505 | if (key_type == BTRFS_DIR_INDEX_KEY) |
| 5506 | btrfs_put_delayed_items(&ins_list, &del_list); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5507 | btrfs_free_path(path); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5508 | return ret; |
| 5509 | } |
| 5510 | |
Christoph Hellwig | a9185b4 | 2010-03-05 09:21:37 +0100 | [diff] [blame] | 5511 | int btrfs_write_inode(struct inode *inode, struct writeback_control *wbc) |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5512 | { |
| 5513 | struct btrfs_root *root = BTRFS_I(inode)->root; |
| 5514 | struct btrfs_trans_handle *trans; |
| 5515 | int ret = 0; |
Josef Bacik | 0af3d00 | 2010-06-21 14:48:16 -0400 | [diff] [blame] | 5516 | bool nolock = false; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5517 | |
Josef Bacik | 72ac3c0 | 2012-05-23 14:13:11 -0400 | [diff] [blame] | 5518 | if (test_bit(BTRFS_INODE_DUMMY, &BTRFS_I(inode)->runtime_flags)) |
Chris Mason | 4ca8b41 | 2008-08-05 13:30:48 -0400 | [diff] [blame] | 5519 | return 0; |
| 5520 | |
Liu Bo | 83eea1f | 2012-07-10 05:28:39 -0600 | [diff] [blame] | 5521 | 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] | 5522 | nolock = true; |
Josef Bacik | 0af3d00 | 2010-06-21 14:48:16 -0400 | [diff] [blame] | 5523 | |
Christoph Hellwig | a9185b4 | 2010-03-05 09:21:37 +0100 | [diff] [blame] | 5524 | if (wbc->sync_mode == WB_SYNC_ALL) { |
Josef Bacik | 0af3d00 | 2010-06-21 14:48:16 -0400 | [diff] [blame] | 5525 | if (nolock) |
Josef Bacik | 7a7eaa4 | 2011-04-13 12:54:33 -0400 | [diff] [blame] | 5526 | trans = btrfs_join_transaction_nolock(root); |
Josef Bacik | 0af3d00 | 2010-06-21 14:48:16 -0400 | [diff] [blame] | 5527 | else |
Josef Bacik | 7a7eaa4 | 2011-04-13 12:54:33 -0400 | [diff] [blame] | 5528 | trans = btrfs_join_transaction(root); |
Tsutomu Itoh | 3612b49 | 2011-01-25 02:51:38 +0000 | [diff] [blame] | 5529 | if (IS_ERR(trans)) |
| 5530 | return PTR_ERR(trans); |
Miao Xie | a698d075 | 2012-09-20 01:51:59 -0600 | [diff] [blame] | 5531 | ret = btrfs_commit_transaction(trans, root); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5532 | } |
| 5533 | return ret; |
| 5534 | } |
| 5535 | |
| 5536 | /* |
Chris Mason | 54aa1f4 | 2007-06-22 14:16:25 -0400 | [diff] [blame] | 5537 | * This is somewhat expensive, updating the tree every time the |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5538 | * inode changes. But, it is most likely to find the inode in cache. |
| 5539 | * FIXME, needs more benchmarking...there are no reasons other than performance |
| 5540 | * to keep or drop this code. |
| 5541 | */ |
Eric Sandeen | 48a3b63 | 2013-04-25 20:41:01 +0000 | [diff] [blame] | 5542 | static int btrfs_dirty_inode(struct inode *inode) |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5543 | { |
| 5544 | struct btrfs_root *root = BTRFS_I(inode)->root; |
| 5545 | struct btrfs_trans_handle *trans; |
Yan, Zheng | 8929ecfa | 2010-05-16 10:49:58 -0400 | [diff] [blame] | 5546 | int ret; |
| 5547 | |
Josef Bacik | 72ac3c0 | 2012-05-23 14:13:11 -0400 | [diff] [blame] | 5548 | if (test_bit(BTRFS_INODE_DUMMY, &BTRFS_I(inode)->runtime_flags)) |
Josef Bacik | 22c44fe | 2011-11-30 10:45:38 -0500 | [diff] [blame] | 5549 | return 0; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5550 | |
Josef Bacik | 7a7eaa4 | 2011-04-13 12:54:33 -0400 | [diff] [blame] | 5551 | trans = btrfs_join_transaction(root); |
Josef Bacik | 22c44fe | 2011-11-30 10:45:38 -0500 | [diff] [blame] | 5552 | if (IS_ERR(trans)) |
| 5553 | return PTR_ERR(trans); |
Yan, Zheng | 8929ecfa | 2010-05-16 10:49:58 -0400 | [diff] [blame] | 5554 | |
| 5555 | ret = btrfs_update_inode(trans, root, inode); |
Chris Mason | 94b6044 | 2010-05-26 11:02:00 -0400 | [diff] [blame] | 5556 | if (ret && ret == -ENOSPC) { |
| 5557 | /* whoops, lets try again with the full transaction */ |
| 5558 | btrfs_end_transaction(trans, root); |
| 5559 | trans = btrfs_start_transaction(root, 1); |
Josef Bacik | 22c44fe | 2011-11-30 10:45:38 -0500 | [diff] [blame] | 5560 | if (IS_ERR(trans)) |
| 5561 | return PTR_ERR(trans); |
Yan, Zheng | 8929ecfa | 2010-05-16 10:49:58 -0400 | [diff] [blame] | 5562 | |
Chris Mason | 94b6044 | 2010-05-26 11:02:00 -0400 | [diff] [blame] | 5563 | ret = btrfs_update_inode(trans, root, inode); |
Chris Mason | 94b6044 | 2010-05-26 11:02:00 -0400 | [diff] [blame] | 5564 | } |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5565 | btrfs_end_transaction(trans, root); |
Miao Xie | 16cdcec | 2011-04-22 18:12:22 +0800 | [diff] [blame] | 5566 | if (BTRFS_I(inode)->delayed_node) |
| 5567 | btrfs_balance_delayed_items(root); |
Josef Bacik | 22c44fe | 2011-11-30 10:45:38 -0500 | [diff] [blame] | 5568 | |
| 5569 | return ret; |
| 5570 | } |
| 5571 | |
| 5572 | /* |
| 5573 | * This is a copy of file_update_time. We need this so we can return error on |
| 5574 | * ENOSPC for updating the inode in the case of file write and mmap writes. |
| 5575 | */ |
Josef Bacik | e41f941 | 2012-03-26 09:46:47 -0400 | [diff] [blame] | 5576 | static int btrfs_update_time(struct inode *inode, struct timespec *now, |
| 5577 | int flags) |
Josef Bacik | 22c44fe | 2011-11-30 10:45:38 -0500 | [diff] [blame] | 5578 | { |
Alexander Block | 2bc556528 | 2012-06-15 09:49:33 +0200 | [diff] [blame] | 5579 | struct btrfs_root *root = BTRFS_I(inode)->root; |
| 5580 | |
| 5581 | if (btrfs_root_readonly(root)) |
| 5582 | return -EROFS; |
| 5583 | |
Josef Bacik | e41f941 | 2012-03-26 09:46:47 -0400 | [diff] [blame] | 5584 | if (flags & S_VERSION) |
Josef Bacik | 22c44fe | 2011-11-30 10:45:38 -0500 | [diff] [blame] | 5585 | inode_inc_iversion(inode); |
Josef Bacik | e41f941 | 2012-03-26 09:46:47 -0400 | [diff] [blame] | 5586 | if (flags & S_CTIME) |
| 5587 | inode->i_ctime = *now; |
| 5588 | if (flags & S_MTIME) |
| 5589 | inode->i_mtime = *now; |
| 5590 | if (flags & S_ATIME) |
| 5591 | inode->i_atime = *now; |
| 5592 | return btrfs_dirty_inode(inode); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5593 | } |
| 5594 | |
Chris Mason | d352ac6 | 2008-09-29 15:18:18 -0400 | [diff] [blame] | 5595 | /* |
| 5596 | * find the highest existing sequence number in a directory |
| 5597 | * and then set the in-memory index_cnt variable to reflect |
| 5598 | * free sequence numbers |
| 5599 | */ |
Josef Bacik | aec7477 | 2008-07-24 12:12:38 -0400 | [diff] [blame] | 5600 | static int btrfs_set_inode_index_count(struct inode *inode) |
| 5601 | { |
| 5602 | struct btrfs_root *root = BTRFS_I(inode)->root; |
| 5603 | struct btrfs_key key, found_key; |
| 5604 | struct btrfs_path *path; |
| 5605 | struct extent_buffer *leaf; |
| 5606 | int ret; |
| 5607 | |
Li Zefan | 33345d01 | 2011-04-20 10:31:50 +0800 | [diff] [blame] | 5608 | key.objectid = btrfs_ino(inode); |
David Sterba | 962a298 | 2014-06-04 18:41:45 +0200 | [diff] [blame] | 5609 | key.type = BTRFS_DIR_INDEX_KEY; |
Josef Bacik | aec7477 | 2008-07-24 12:12:38 -0400 | [diff] [blame] | 5610 | key.offset = (u64)-1; |
| 5611 | |
| 5612 | path = btrfs_alloc_path(); |
| 5613 | if (!path) |
| 5614 | return -ENOMEM; |
| 5615 | |
| 5616 | ret = btrfs_search_slot(NULL, root, &key, path, 0, 0); |
| 5617 | if (ret < 0) |
| 5618 | goto out; |
| 5619 | /* FIXME: we should be able to handle this */ |
| 5620 | if (ret == 0) |
| 5621 | goto out; |
| 5622 | ret = 0; |
| 5623 | |
| 5624 | /* |
| 5625 | * MAGIC NUMBER EXPLANATION: |
| 5626 | * since we search a directory based on f_pos we have to start at 2 |
| 5627 | * since '.' and '..' have f_pos of 0 and 1 respectively, so everybody |
| 5628 | * else has to start at 2 |
| 5629 | */ |
| 5630 | if (path->slots[0] == 0) { |
| 5631 | BTRFS_I(inode)->index_cnt = 2; |
| 5632 | goto out; |
| 5633 | } |
| 5634 | |
| 5635 | path->slots[0]--; |
| 5636 | |
| 5637 | leaf = path->nodes[0]; |
| 5638 | btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]); |
| 5639 | |
Li Zefan | 33345d01 | 2011-04-20 10:31:50 +0800 | [diff] [blame] | 5640 | if (found_key.objectid != btrfs_ino(inode) || |
David Sterba | 962a298 | 2014-06-04 18:41:45 +0200 | [diff] [blame] | 5641 | found_key.type != BTRFS_DIR_INDEX_KEY) { |
Josef Bacik | aec7477 | 2008-07-24 12:12:38 -0400 | [diff] [blame] | 5642 | BTRFS_I(inode)->index_cnt = 2; |
| 5643 | goto out; |
| 5644 | } |
| 5645 | |
| 5646 | BTRFS_I(inode)->index_cnt = found_key.offset + 1; |
| 5647 | out: |
| 5648 | btrfs_free_path(path); |
| 5649 | return ret; |
| 5650 | } |
| 5651 | |
Chris Mason | d352ac6 | 2008-09-29 15:18:18 -0400 | [diff] [blame] | 5652 | /* |
| 5653 | * helper to find a free sequence number in a given directory. This current |
| 5654 | * code is very simple, later versions will do smarter things in the btree |
| 5655 | */ |
Chris Mason | 3de4586 | 2008-11-17 21:02:50 -0500 | [diff] [blame] | 5656 | int btrfs_set_inode_index(struct inode *dir, u64 *index) |
Josef Bacik | aec7477 | 2008-07-24 12:12:38 -0400 | [diff] [blame] | 5657 | { |
| 5658 | int ret = 0; |
| 5659 | |
| 5660 | if (BTRFS_I(dir)->index_cnt == (u64)-1) { |
Miao Xie | 16cdcec | 2011-04-22 18:12:22 +0800 | [diff] [blame] | 5661 | ret = btrfs_inode_delayed_dir_index_count(dir); |
| 5662 | if (ret) { |
| 5663 | ret = btrfs_set_inode_index_count(dir); |
| 5664 | if (ret) |
| 5665 | return ret; |
| 5666 | } |
Josef Bacik | aec7477 | 2008-07-24 12:12:38 -0400 | [diff] [blame] | 5667 | } |
| 5668 | |
Chris Mason | 00e4e6b | 2008-08-05 11:18:09 -0400 | [diff] [blame] | 5669 | *index = BTRFS_I(dir)->index_cnt; |
Josef Bacik | aec7477 | 2008-07-24 12:12:38 -0400 | [diff] [blame] | 5670 | BTRFS_I(dir)->index_cnt++; |
| 5671 | |
| 5672 | return ret; |
| 5673 | } |
| 5674 | |
Chris Mason | b0d5d10 | 2014-09-08 13:08:51 -0700 | [diff] [blame] | 5675 | static int btrfs_insert_inode_locked(struct inode *inode) |
| 5676 | { |
| 5677 | struct btrfs_iget_args args; |
| 5678 | args.location = &BTRFS_I(inode)->location; |
| 5679 | args.root = BTRFS_I(inode)->root; |
| 5680 | |
| 5681 | return insert_inode_locked4(inode, |
| 5682 | btrfs_inode_hash(inode->i_ino, BTRFS_I(inode)->root), |
| 5683 | btrfs_find_actor, &args); |
| 5684 | } |
| 5685 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5686 | static struct inode *btrfs_new_inode(struct btrfs_trans_handle *trans, |
| 5687 | struct btrfs_root *root, |
Josef Bacik | aec7477 | 2008-07-24 12:12:38 -0400 | [diff] [blame] | 5688 | struct inode *dir, |
Chris Mason | 9c58309 | 2008-01-29 15:15:18 -0500 | [diff] [blame] | 5689 | const char *name, int name_len, |
Al Viro | 175a4eb | 2011-07-26 03:30:54 -0400 | [diff] [blame] | 5690 | u64 ref_objectid, u64 objectid, |
| 5691 | umode_t mode, u64 *index) |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5692 | { |
| 5693 | struct inode *inode; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 5694 | struct btrfs_inode_item *inode_item; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5695 | struct btrfs_key *location; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 5696 | struct btrfs_path *path; |
Chris Mason | 9c58309 | 2008-01-29 15:15:18 -0500 | [diff] [blame] | 5697 | struct btrfs_inode_ref *ref; |
| 5698 | struct btrfs_key key[2]; |
| 5699 | u32 sizes[2]; |
Filipe Manana | ef3b9af | 2014-04-27 20:40:45 +0100 | [diff] [blame] | 5700 | int nitems = name ? 2 : 1; |
Chris Mason | 9c58309 | 2008-01-29 15:15:18 -0500 | [diff] [blame] | 5701 | unsigned long ptr; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5702 | int ret; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5703 | |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 5704 | path = btrfs_alloc_path(); |
Mark Fasheh | d8926bb | 2011-07-13 10:38:47 -0700 | [diff] [blame] | 5705 | if (!path) |
| 5706 | return ERR_PTR(-ENOMEM); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 5707 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5708 | inode = new_inode(root->fs_info->sb); |
Yoshinori Sano | 8fb2764 | 2011-04-09 02:30:07 +0000 | [diff] [blame] | 5709 | if (!inode) { |
| 5710 | btrfs_free_path(path); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5711 | return ERR_PTR(-ENOMEM); |
Yoshinori Sano | 8fb2764 | 2011-04-09 02:30:07 +0000 | [diff] [blame] | 5712 | } |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5713 | |
Li Zefan | 581bb05 | 2011-04-20 10:06:11 +0800 | [diff] [blame] | 5714 | /* |
Filipe Manana | 5762b5c | 2014-08-01 00:10:32 +0100 | [diff] [blame] | 5715 | * O_TMPFILE, set link count to 0, so that after this point, |
| 5716 | * we fill in an inode item with the correct link count. |
| 5717 | */ |
| 5718 | if (!name) |
| 5719 | set_nlink(inode, 0); |
| 5720 | |
| 5721 | /* |
Li Zefan | 581bb05 | 2011-04-20 10:06:11 +0800 | [diff] [blame] | 5722 | * we have to initialize this early, so we can reclaim the inode |
| 5723 | * number if we fail afterwards in this function. |
| 5724 | */ |
| 5725 | inode->i_ino = objectid; |
| 5726 | |
Filipe Manana | ef3b9af | 2014-04-27 20:40:45 +0100 | [diff] [blame] | 5727 | if (dir && name) { |
liubo | 1abe9b8 | 2011-03-24 11:18:59 +0000 | [diff] [blame] | 5728 | trace_btrfs_inode_request(dir); |
| 5729 | |
Chris Mason | 3de4586 | 2008-11-17 21:02:50 -0500 | [diff] [blame] | 5730 | ret = btrfs_set_inode_index(dir, index); |
Shen Feng | 0977143 | 2009-04-02 16:46:06 -0400 | [diff] [blame] | 5731 | if (ret) { |
Yoshinori Sano | 8fb2764 | 2011-04-09 02:30:07 +0000 | [diff] [blame] | 5732 | btrfs_free_path(path); |
Shen Feng | 0977143 | 2009-04-02 16:46:06 -0400 | [diff] [blame] | 5733 | iput(inode); |
Josef Bacik | aec7477 | 2008-07-24 12:12:38 -0400 | [diff] [blame] | 5734 | return ERR_PTR(ret); |
Shen Feng | 0977143 | 2009-04-02 16:46:06 -0400 | [diff] [blame] | 5735 | } |
Filipe Manana | ef3b9af | 2014-04-27 20:40:45 +0100 | [diff] [blame] | 5736 | } else if (dir) { |
| 5737 | *index = 0; |
Josef Bacik | aec7477 | 2008-07-24 12:12:38 -0400 | [diff] [blame] | 5738 | } |
| 5739 | /* |
| 5740 | * index_cnt is ignored for everything but a dir, |
| 5741 | * btrfs_get_inode_index_count has an explanation for the magic |
| 5742 | * number |
| 5743 | */ |
| 5744 | BTRFS_I(inode)->index_cnt = 2; |
Miao Xie | 67de117 | 2013-12-26 13:07:06 +0800 | [diff] [blame] | 5745 | BTRFS_I(inode)->dir_index = *index; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5746 | BTRFS_I(inode)->root = root; |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 5747 | BTRFS_I(inode)->generation = trans->transid; |
Josef Bacik | 7619585 | 2010-11-19 02:18:02 +0000 | [diff] [blame] | 5748 | inode->i_generation = BTRFS_I(inode)->generation; |
Chris Mason | b888db2b | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 5749 | |
Josef Bacik | 5dc562c | 2012-08-17 13:14:17 -0400 | [diff] [blame] | 5750 | /* |
| 5751 | * We could have gotten an inode number from somebody who was fsynced |
| 5752 | * and then removed in this same transaction, so let's just set full |
| 5753 | * sync since it will be a full sync anyway and this will blow away the |
| 5754 | * old info in the log. |
| 5755 | */ |
| 5756 | set_bit(BTRFS_INODE_NEEDS_FULL_SYNC, &BTRFS_I(inode)->runtime_flags); |
| 5757 | |
Chris Mason | 9c58309 | 2008-01-29 15:15:18 -0500 | [diff] [blame] | 5758 | key[0].objectid = objectid; |
David Sterba | 962a298 | 2014-06-04 18:41:45 +0200 | [diff] [blame] | 5759 | key[0].type = BTRFS_INODE_ITEM_KEY; |
Chris Mason | 9c58309 | 2008-01-29 15:15:18 -0500 | [diff] [blame] | 5760 | key[0].offset = 0; |
| 5761 | |
Chris Mason | 9c58309 | 2008-01-29 15:15:18 -0500 | [diff] [blame] | 5762 | sizes[0] = sizeof(struct btrfs_inode_item); |
Filipe Manana | ef3b9af | 2014-04-27 20:40:45 +0100 | [diff] [blame] | 5763 | |
| 5764 | if (name) { |
| 5765 | /* |
| 5766 | * Start new inodes with an inode_ref. This is slightly more |
| 5767 | * efficient for small numbers of hard links since they will |
| 5768 | * be packed into one item. Extended refs will kick in if we |
| 5769 | * add more hard links than can fit in the ref item. |
| 5770 | */ |
| 5771 | key[1].objectid = objectid; |
David Sterba | 962a298 | 2014-06-04 18:41:45 +0200 | [diff] [blame] | 5772 | key[1].type = BTRFS_INODE_REF_KEY; |
Filipe Manana | ef3b9af | 2014-04-27 20:40:45 +0100 | [diff] [blame] | 5773 | key[1].offset = ref_objectid; |
| 5774 | |
| 5775 | sizes[1] = name_len + sizeof(*ref); |
| 5776 | } |
Chris Mason | 9c58309 | 2008-01-29 15:15:18 -0500 | [diff] [blame] | 5777 | |
Chris Mason | b0d5d10 | 2014-09-08 13:08:51 -0700 | [diff] [blame] | 5778 | location = &BTRFS_I(inode)->location; |
| 5779 | location->objectid = objectid; |
| 5780 | location->offset = 0; |
David Sterba | 962a298 | 2014-06-04 18:41:45 +0200 | [diff] [blame] | 5781 | location->type = BTRFS_INODE_ITEM_KEY; |
Chris Mason | b0d5d10 | 2014-09-08 13:08:51 -0700 | [diff] [blame] | 5782 | |
| 5783 | ret = btrfs_insert_inode_locked(inode); |
| 5784 | if (ret < 0) |
| 5785 | goto fail; |
| 5786 | |
Chris Mason | b947343 | 2009-03-13 11:00:37 -0400 | [diff] [blame] | 5787 | path->leave_spinning = 1; |
Filipe Manana | ef3b9af | 2014-04-27 20:40:45 +0100 | [diff] [blame] | 5788 | ret = btrfs_insert_empty_items(trans, root, path, key, sizes, nitems); |
Chris Mason | 9c58309 | 2008-01-29 15:15:18 -0500 | [diff] [blame] | 5789 | if (ret != 0) |
Chris Mason | b0d5d10 | 2014-09-08 13:08:51 -0700 | [diff] [blame] | 5790 | goto fail_unlock; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 5791 | |
Dmitry Monakhov | ecc11fab | 2010-03-04 17:31:47 +0300 | [diff] [blame] | 5792 | inode_init_owner(inode, dir, mode); |
Yan Zheng | a76a3cd | 2008-10-09 11:46:29 -0400 | [diff] [blame] | 5793 | inode_set_bytes(inode, 0); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5794 | inode->i_mtime = inode->i_atime = inode->i_ctime = CURRENT_TIME; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 5795 | inode_item = btrfs_item_ptr(path->nodes[0], path->slots[0], |
| 5796 | struct btrfs_inode_item); |
Li Zefan | 293f7e0 | 2012-07-10 00:58:58 -0600 | [diff] [blame] | 5797 | memset_extent_buffer(path->nodes[0], 0, (unsigned long)inode_item, |
| 5798 | sizeof(*inode_item)); |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 5799 | fill_inode_item(trans, path->nodes[0], inode_item, inode); |
Chris Mason | 9c58309 | 2008-01-29 15:15:18 -0500 | [diff] [blame] | 5800 | |
Filipe Manana | ef3b9af | 2014-04-27 20:40:45 +0100 | [diff] [blame] | 5801 | if (name) { |
| 5802 | ref = btrfs_item_ptr(path->nodes[0], path->slots[0] + 1, |
| 5803 | struct btrfs_inode_ref); |
| 5804 | btrfs_set_inode_ref_name_len(path->nodes[0], ref, name_len); |
| 5805 | btrfs_set_inode_ref_index(path->nodes[0], ref, *index); |
| 5806 | ptr = (unsigned long)(ref + 1); |
| 5807 | write_extent_buffer(path->nodes[0], name, ptr, name_len); |
| 5808 | } |
Chris Mason | 9c58309 | 2008-01-29 15:15:18 -0500 | [diff] [blame] | 5809 | |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 5810 | btrfs_mark_buffer_dirty(path->nodes[0]); |
| 5811 | btrfs_free_path(path); |
| 5812 | |
Christoph Hellwig | 6cbff00 | 2009-04-17 10:37:41 +0200 | [diff] [blame] | 5813 | btrfs_inherit_iflags(inode, dir); |
| 5814 | |
Al Viro | 569254b | 2011-07-24 17:08:40 -0400 | [diff] [blame] | 5815 | if (S_ISREG(mode)) { |
Chris Mason | 9427216 | 2009-07-02 12:26:06 -0400 | [diff] [blame] | 5816 | if (btrfs_test_opt(root, NODATASUM)) |
| 5817 | BTRFS_I(inode)->flags |= BTRFS_INODE_NODATASUM; |
Liu Bo | 213490b | 2012-09-11 08:33:50 -0600 | [diff] [blame] | 5818 | if (btrfs_test_opt(root, NODATACOW)) |
Josef Bacik | f2bdf9a | 2013-02-21 15:28:28 -0500 | [diff] [blame] | 5819 | BTRFS_I(inode)->flags |= BTRFS_INODE_NODATACOW | |
| 5820 | BTRFS_INODE_NODATASUM; |
Chris Mason | 9427216 | 2009-07-02 12:26:06 -0400 | [diff] [blame] | 5821 | } |
| 5822 | |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 5823 | inode_tree_add(inode); |
liubo | 1abe9b8 | 2011-03-24 11:18:59 +0000 | [diff] [blame] | 5824 | |
| 5825 | trace_btrfs_inode_new(inode); |
Chris Mason | 1973f0f | 2011-06-24 13:13:29 -0400 | [diff] [blame] | 5826 | btrfs_set_inode_last_trans(trans, inode); |
liubo | 1abe9b8 | 2011-03-24 11:18:59 +0000 | [diff] [blame] | 5827 | |
Alexander Block | 8ea05e3 | 2012-07-25 17:35:53 +0200 | [diff] [blame] | 5828 | btrfs_update_root_times(trans, root); |
| 5829 | |
Filipe David Borba Manana | 6354192 | 2014-01-07 11:47:46 +0000 | [diff] [blame] | 5830 | ret = btrfs_inode_inherit_props(trans, inode, dir); |
| 5831 | if (ret) |
| 5832 | btrfs_err(root->fs_info, |
| 5833 | "error inheriting props for ino %llu (root %llu): %d", |
| 5834 | btrfs_ino(inode), root->root_key.objectid, ret); |
| 5835 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5836 | return inode; |
Chris Mason | b0d5d10 | 2014-09-08 13:08:51 -0700 | [diff] [blame] | 5837 | |
| 5838 | fail_unlock: |
| 5839 | unlock_new_inode(inode); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 5840 | fail: |
Filipe Manana | ef3b9af | 2014-04-27 20:40:45 +0100 | [diff] [blame] | 5841 | if (dir && name) |
Josef Bacik | aec7477 | 2008-07-24 12:12:38 -0400 | [diff] [blame] | 5842 | BTRFS_I(dir)->index_cnt--; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 5843 | btrfs_free_path(path); |
Shen Feng | 0977143 | 2009-04-02 16:46:06 -0400 | [diff] [blame] | 5844 | iput(inode); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 5845 | return ERR_PTR(ret); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5846 | } |
| 5847 | |
| 5848 | static inline u8 btrfs_inode_type(struct inode *inode) |
| 5849 | { |
| 5850 | return btrfs_type_by_mode[(inode->i_mode & S_IFMT) >> S_SHIFT]; |
| 5851 | } |
| 5852 | |
Chris Mason | d352ac6 | 2008-09-29 15:18:18 -0400 | [diff] [blame] | 5853 | /* |
| 5854 | * utility function to add 'inode' into 'parent_inode' with |
| 5855 | * a give name and a given sequence number. |
| 5856 | * if 'add_backref' is true, also insert a backref from the |
| 5857 | * inode to the parent directory. |
| 5858 | */ |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 5859 | int btrfs_add_link(struct btrfs_trans_handle *trans, |
| 5860 | struct inode *parent_inode, struct inode *inode, |
| 5861 | const char *name, int name_len, int add_backref, u64 index) |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5862 | { |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 5863 | int ret = 0; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5864 | struct btrfs_key key; |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 5865 | struct btrfs_root *root = BTRFS_I(parent_inode)->root; |
Li Zefan | 33345d01 | 2011-04-20 10:31:50 +0800 | [diff] [blame] | 5866 | u64 ino = btrfs_ino(inode); |
| 5867 | u64 parent_ino = btrfs_ino(parent_inode); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 5868 | |
Li Zefan | 33345d01 | 2011-04-20 10:31:50 +0800 | [diff] [blame] | 5869 | if (unlikely(ino == BTRFS_FIRST_FREE_OBJECTID)) { |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 5870 | memcpy(&key, &BTRFS_I(inode)->root->root_key, sizeof(key)); |
| 5871 | } else { |
Li Zefan | 33345d01 | 2011-04-20 10:31:50 +0800 | [diff] [blame] | 5872 | key.objectid = ino; |
David Sterba | 962a298 | 2014-06-04 18:41:45 +0200 | [diff] [blame] | 5873 | key.type = BTRFS_INODE_ITEM_KEY; |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 5874 | key.offset = 0; |
| 5875 | } |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5876 | |
Li Zefan | 33345d01 | 2011-04-20 10:31:50 +0800 | [diff] [blame] | 5877 | if (unlikely(ino == BTRFS_FIRST_FREE_OBJECTID)) { |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 5878 | ret = btrfs_add_root_ref(trans, root->fs_info->tree_root, |
| 5879 | key.objectid, root->root_key.objectid, |
Li Zefan | 33345d01 | 2011-04-20 10:31:50 +0800 | [diff] [blame] | 5880 | parent_ino, index, name, name_len); |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 5881 | } else if (add_backref) { |
Li Zefan | 33345d01 | 2011-04-20 10:31:50 +0800 | [diff] [blame] | 5882 | ret = btrfs_insert_inode_ref(trans, root, name, name_len, ino, |
| 5883 | parent_ino, index); |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 5884 | } |
| 5885 | |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 5886 | /* Nothing to clean up yet */ |
| 5887 | if (ret) |
| 5888 | return ret; |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 5889 | |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 5890 | ret = btrfs_insert_dir_item(trans, root, name, name_len, |
| 5891 | parent_inode, &key, |
| 5892 | btrfs_inode_type(inode), index); |
Chris Mason | 9c52057 | 2012-12-17 14:26:57 -0500 | [diff] [blame] | 5893 | if (ret == -EEXIST || ret == -EOVERFLOW) |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 5894 | goto fail_dir_item; |
| 5895 | else if (ret) { |
| 5896 | btrfs_abort_transaction(trans, root, ret); |
| 5897 | return ret; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5898 | } |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 5899 | |
| 5900 | btrfs_i_size_write(parent_inode, parent_inode->i_size + |
| 5901 | name_len * 2); |
Josef Bacik | 0c4d2d9 | 2012-04-05 15:03:02 -0400 | [diff] [blame] | 5902 | inode_inc_iversion(parent_inode); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 5903 | parent_inode->i_mtime = parent_inode->i_ctime = CURRENT_TIME; |
| 5904 | ret = btrfs_update_inode(trans, root, parent_inode); |
| 5905 | if (ret) |
| 5906 | btrfs_abort_transaction(trans, root, ret); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5907 | return ret; |
Chris Mason | fe66a05 | 2012-02-20 08:40:56 -0500 | [diff] [blame] | 5908 | |
| 5909 | fail_dir_item: |
| 5910 | if (unlikely(ino == BTRFS_FIRST_FREE_OBJECTID)) { |
| 5911 | u64 local_index; |
| 5912 | int err; |
| 5913 | err = btrfs_del_root_ref(trans, root->fs_info->tree_root, |
| 5914 | key.objectid, root->root_key.objectid, |
| 5915 | parent_ino, &local_index, name, name_len); |
| 5916 | |
| 5917 | } else if (add_backref) { |
| 5918 | u64 local_index; |
| 5919 | int err; |
| 5920 | |
| 5921 | err = btrfs_del_inode_ref(trans, root, name, name_len, |
| 5922 | ino, parent_ino, &local_index); |
| 5923 | } |
| 5924 | return ret; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5925 | } |
| 5926 | |
| 5927 | static int btrfs_add_nondir(struct btrfs_trans_handle *trans, |
Josef Bacik | a1b075d | 2010-11-19 20:36:11 +0000 | [diff] [blame] | 5928 | struct inode *dir, struct dentry *dentry, |
| 5929 | struct inode *inode, int backref, u64 index) |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5930 | { |
Josef Bacik | a1b075d | 2010-11-19 20:36:11 +0000 | [diff] [blame] | 5931 | int err = btrfs_add_link(trans, dir, inode, |
| 5932 | dentry->d_name.name, dentry->d_name.len, |
| 5933 | backref, index); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5934 | if (err > 0) |
| 5935 | err = -EEXIST; |
| 5936 | return err; |
| 5937 | } |
| 5938 | |
Josef Bacik | 618e21d | 2007-07-11 10:18:17 -0400 | [diff] [blame] | 5939 | static int btrfs_mknod(struct inode *dir, struct dentry *dentry, |
Al Viro | 1a67aaf | 2011-07-26 01:52:52 -0400 | [diff] [blame] | 5940 | umode_t mode, dev_t rdev) |
Josef Bacik | 618e21d | 2007-07-11 10:18:17 -0400 | [diff] [blame] | 5941 | { |
| 5942 | struct btrfs_trans_handle *trans; |
| 5943 | struct btrfs_root *root = BTRFS_I(dir)->root; |
Chris Mason | 1832a6d | 2007-12-21 16:27:21 -0500 | [diff] [blame] | 5944 | struct inode *inode = NULL; |
Josef Bacik | 618e21d | 2007-07-11 10:18:17 -0400 | [diff] [blame] | 5945 | int err; |
| 5946 | int drop_inode = 0; |
| 5947 | u64 objectid; |
Chris Mason | 00e4e6b | 2008-08-05 11:18:09 -0400 | [diff] [blame] | 5948 | u64 index = 0; |
Josef Bacik | 618e21d | 2007-07-11 10:18:17 -0400 | [diff] [blame] | 5949 | |
| 5950 | if (!new_valid_dev(rdev)) |
| 5951 | return -EINVAL; |
| 5952 | |
Josef Bacik | 9ed74f2 | 2009-09-11 16:12:44 -0400 | [diff] [blame] | 5953 | /* |
| 5954 | * 2 for inode item and ref |
| 5955 | * 2 for dir items |
| 5956 | * 1 for xattr if selinux is on |
| 5957 | */ |
Yan, Zheng | a22285a | 2010-05-16 10:48:46 -0400 | [diff] [blame] | 5958 | trans = btrfs_start_transaction(root, 5); |
| 5959 | if (IS_ERR(trans)) |
| 5960 | return PTR_ERR(trans); |
Chris Mason | 1832a6d | 2007-12-21 16:27:21 -0500 | [diff] [blame] | 5961 | |
Li Zefan | 581bb05 | 2011-04-20 10:06:11 +0800 | [diff] [blame] | 5962 | err = btrfs_find_free_ino(root, &objectid); |
| 5963 | if (err) |
| 5964 | goto out_unlock; |
| 5965 | |
Josef Bacik | aec7477 | 2008-07-24 12:12:38 -0400 | [diff] [blame] | 5966 | inode = btrfs_new_inode(trans, root, dir, dentry->d_name.name, |
Li Zefan | 33345d01 | 2011-04-20 10:31:50 +0800 | [diff] [blame] | 5967 | dentry->d_name.len, btrfs_ino(dir), objectid, |
Josef Bacik | d82a6f1d | 2011-05-11 15:26:06 -0400 | [diff] [blame] | 5968 | mode, &index); |
Tsutomu Itoh | 7cf96da | 2011-04-25 19:43:53 -0400 | [diff] [blame] | 5969 | if (IS_ERR(inode)) { |
| 5970 | err = PTR_ERR(inode); |
Josef Bacik | 618e21d | 2007-07-11 10:18:17 -0400 | [diff] [blame] | 5971 | goto out_unlock; |
Tsutomu Itoh | 7cf96da | 2011-04-25 19:43:53 -0400 | [diff] [blame] | 5972 | } |
Josef Bacik | 618e21d | 2007-07-11 10:18:17 -0400 | [diff] [blame] | 5973 | |
Casey Schaufler | ad19db7 | 2011-12-15 10:09:07 -0500 | [diff] [blame] | 5974 | /* |
| 5975 | * If the active LSM wants to access the inode during |
| 5976 | * d_instantiate it needs these. Smack checks to see |
| 5977 | * if the filesystem supports xattrs by looking at the |
| 5978 | * ops vector. |
| 5979 | */ |
Casey Schaufler | ad19db7 | 2011-12-15 10:09:07 -0500 | [diff] [blame] | 5980 | inode->i_op = &btrfs_special_inode_operations; |
Chris Mason | b0d5d10 | 2014-09-08 13:08:51 -0700 | [diff] [blame] | 5981 | init_special_inode(inode, inode->i_mode, rdev); |
| 5982 | |
| 5983 | err = btrfs_init_inode_security(trans, inode, dir, &dentry->d_name); |
Josef Bacik | 618e21d | 2007-07-11 10:18:17 -0400 | [diff] [blame] | 5984 | if (err) |
Chris Mason | b0d5d10 | 2014-09-08 13:08:51 -0700 | [diff] [blame] | 5985 | goto out_unlock_inode; |
| 5986 | |
| 5987 | err = btrfs_add_nondir(trans, dir, dentry, inode, 0, index); |
| 5988 | if (err) { |
| 5989 | goto out_unlock_inode; |
| 5990 | } else { |
Yan | 1b4ab1b | 2007-08-29 09:11:44 -0400 | [diff] [blame] | 5991 | btrfs_update_inode(trans, root, inode); |
Chris Mason | b0d5d10 | 2014-09-08 13:08:51 -0700 | [diff] [blame] | 5992 | unlock_new_inode(inode); |
Al Viro | 08c422c | 2011-12-23 07:58:13 -0500 | [diff] [blame] | 5993 | d_instantiate(dentry, inode); |
Josef Bacik | 618e21d | 2007-07-11 10:18:17 -0400 | [diff] [blame] | 5994 | } |
Chris Mason | b0d5d10 | 2014-09-08 13:08:51 -0700 | [diff] [blame] | 5995 | |
Josef Bacik | 618e21d | 2007-07-11 10:18:17 -0400 | [diff] [blame] | 5996 | out_unlock: |
Josef Bacik | 7ad85bb | 2012-01-12 19:10:12 -0500 | [diff] [blame] | 5997 | btrfs_end_transaction(trans, root); |
Josef Bacik | c581afc | 2014-02-06 16:06:06 -0500 | [diff] [blame] | 5998 | btrfs_balance_delayed_items(root); |
Liu Bo | b53d3f5 | 2012-11-14 14:34:34 +0000 | [diff] [blame] | 5999 | btrfs_btree_balance_dirty(root); |
Josef Bacik | 618e21d | 2007-07-11 10:18:17 -0400 | [diff] [blame] | 6000 | if (drop_inode) { |
| 6001 | inode_dec_link_count(inode); |
| 6002 | iput(inode); |
| 6003 | } |
Josef Bacik | 618e21d | 2007-07-11 10:18:17 -0400 | [diff] [blame] | 6004 | return err; |
Chris Mason | b0d5d10 | 2014-09-08 13:08:51 -0700 | [diff] [blame] | 6005 | |
| 6006 | out_unlock_inode: |
| 6007 | drop_inode = 1; |
| 6008 | unlock_new_inode(inode); |
| 6009 | goto out_unlock; |
| 6010 | |
Josef Bacik | 618e21d | 2007-07-11 10:18:17 -0400 | [diff] [blame] | 6011 | } |
| 6012 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 6013 | static int btrfs_create(struct inode *dir, struct dentry *dentry, |
Al Viro | ebfc3b4 | 2012-06-10 18:05:36 -0400 | [diff] [blame] | 6014 | umode_t mode, bool excl) |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 6015 | { |
| 6016 | struct btrfs_trans_handle *trans; |
| 6017 | struct btrfs_root *root = BTRFS_I(dir)->root; |
Chris Mason | 1832a6d | 2007-12-21 16:27:21 -0500 | [diff] [blame] | 6018 | struct inode *inode = NULL; |
Filipe Brandenburger | 43baa57 | 2012-11-30 03:40:09 +0000 | [diff] [blame] | 6019 | int drop_inode_on_err = 0; |
Yan, Zheng | a22285a | 2010-05-16 10:48:46 -0400 | [diff] [blame] | 6020 | int err; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 6021 | u64 objectid; |
Chris Mason | 00e4e6b | 2008-08-05 11:18:09 -0400 | [diff] [blame] | 6022 | u64 index = 0; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 6023 | |
Josef Bacik | 9ed74f2 | 2009-09-11 16:12:44 -0400 | [diff] [blame] | 6024 | /* |
| 6025 | * 2 for inode item and ref |
| 6026 | * 2 for dir items |
| 6027 | * 1 for xattr if selinux is on |
| 6028 | */ |
Yan, Zheng | a22285a | 2010-05-16 10:48:46 -0400 | [diff] [blame] | 6029 | trans = btrfs_start_transaction(root, 5); |
| 6030 | if (IS_ERR(trans)) |
| 6031 | return PTR_ERR(trans); |
Josef Bacik | 9ed74f2 | 2009-09-11 16:12:44 -0400 | [diff] [blame] | 6032 | |
Li Zefan | 581bb05 | 2011-04-20 10:06:11 +0800 | [diff] [blame] | 6033 | err = btrfs_find_free_ino(root, &objectid); |
| 6034 | if (err) |
| 6035 | goto out_unlock; |
| 6036 | |
Josef Bacik | aec7477 | 2008-07-24 12:12:38 -0400 | [diff] [blame] | 6037 | inode = btrfs_new_inode(trans, root, dir, dentry->d_name.name, |
Li Zefan | 33345d01 | 2011-04-20 10:31:50 +0800 | [diff] [blame] | 6038 | dentry->d_name.len, btrfs_ino(dir), objectid, |
Josef Bacik | d82a6f1d | 2011-05-11 15:26:06 -0400 | [diff] [blame] | 6039 | mode, &index); |
Tsutomu Itoh | 7cf96da | 2011-04-25 19:43:53 -0400 | [diff] [blame] | 6040 | if (IS_ERR(inode)) { |
| 6041 | err = PTR_ERR(inode); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 6042 | goto out_unlock; |
Tsutomu Itoh | 7cf96da | 2011-04-25 19:43:53 -0400 | [diff] [blame] | 6043 | } |
Filipe Brandenburger | 43baa57 | 2012-11-30 03:40:09 +0000 | [diff] [blame] | 6044 | drop_inode_on_err = 1; |
Casey Schaufler | ad19db7 | 2011-12-15 10:09:07 -0500 | [diff] [blame] | 6045 | /* |
| 6046 | * If the active LSM wants to access the inode during |
| 6047 | * d_instantiate it needs these. Smack checks to see |
| 6048 | * if the filesystem supports xattrs by looking at the |
| 6049 | * ops vector. |
| 6050 | */ |
| 6051 | inode->i_fop = &btrfs_file_operations; |
| 6052 | inode->i_op = &btrfs_file_inode_operations; |
Chris Mason | b0d5d10 | 2014-09-08 13:08:51 -0700 | [diff] [blame] | 6053 | inode->i_mapping->a_ops = &btrfs_aops; |
| 6054 | inode->i_mapping->backing_dev_info = &root->fs_info->bdi; |
| 6055 | |
| 6056 | err = btrfs_init_inode_security(trans, inode, dir, &dentry->d_name); |
| 6057 | if (err) |
| 6058 | goto out_unlock_inode; |
| 6059 | |
| 6060 | err = btrfs_update_inode(trans, root, inode); |
| 6061 | if (err) |
| 6062 | goto out_unlock_inode; |
Casey Schaufler | ad19db7 | 2011-12-15 10:09:07 -0500 | [diff] [blame] | 6063 | |
Josef Bacik | a1b075d | 2010-11-19 20:36:11 +0000 | [diff] [blame] | 6064 | err = btrfs_add_nondir(trans, dir, dentry, inode, 0, index); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 6065 | if (err) |
Chris Mason | b0d5d10 | 2014-09-08 13:08:51 -0700 | [diff] [blame] | 6066 | goto out_unlock_inode; |
Filipe Brandenburger | 43baa57 | 2012-11-30 03:40:09 +0000 | [diff] [blame] | 6067 | |
Filipe Brandenburger | 43baa57 | 2012-11-30 03:40:09 +0000 | [diff] [blame] | 6068 | BTRFS_I(inode)->io_tree.ops = &btrfs_extent_io_ops; |
Chris Mason | b0d5d10 | 2014-09-08 13:08:51 -0700 | [diff] [blame] | 6069 | unlock_new_inode(inode); |
Filipe Brandenburger | 43baa57 | 2012-11-30 03:40:09 +0000 | [diff] [blame] | 6070 | d_instantiate(dentry, inode); |
| 6071 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 6072 | out_unlock: |
Josef Bacik | 7ad85bb | 2012-01-12 19:10:12 -0500 | [diff] [blame] | 6073 | btrfs_end_transaction(trans, root); |
Filipe Brandenburger | 43baa57 | 2012-11-30 03:40:09 +0000 | [diff] [blame] | 6074 | if (err && drop_inode_on_err) { |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 6075 | inode_dec_link_count(inode); |
| 6076 | iput(inode); |
| 6077 | } |
Josef Bacik | c581afc | 2014-02-06 16:06:06 -0500 | [diff] [blame] | 6078 | btrfs_balance_delayed_items(root); |
Liu Bo | b53d3f5 | 2012-11-14 14:34:34 +0000 | [diff] [blame] | 6079 | btrfs_btree_balance_dirty(root); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 6080 | return err; |
Chris Mason | b0d5d10 | 2014-09-08 13:08:51 -0700 | [diff] [blame] | 6081 | |
| 6082 | out_unlock_inode: |
| 6083 | unlock_new_inode(inode); |
| 6084 | goto out_unlock; |
| 6085 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 6086 | } |
| 6087 | |
| 6088 | static int btrfs_link(struct dentry *old_dentry, struct inode *dir, |
| 6089 | struct dentry *dentry) |
| 6090 | { |
| 6091 | struct btrfs_trans_handle *trans; |
| 6092 | struct btrfs_root *root = BTRFS_I(dir)->root; |
| 6093 | struct inode *inode = old_dentry->d_inode; |
Chris Mason | 00e4e6b | 2008-08-05 11:18:09 -0400 | [diff] [blame] | 6094 | u64 index; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 6095 | int err; |
| 6096 | int drop_inode = 0; |
| 6097 | |
TARUISI Hiroaki | 4a8be42 | 2009-11-12 07:14:26 +0000 | [diff] [blame] | 6098 | /* do not allow sys_link's with other subvols of the same device */ |
| 6099 | if (root->objectid != BTRFS_I(inode)->root->objectid) |
Mark Fasheh | 3ab3564 | 2011-03-22 17:20:26 +0000 | [diff] [blame] | 6100 | return -EXDEV; |
TARUISI Hiroaki | 4a8be42 | 2009-11-12 07:14:26 +0000 | [diff] [blame] | 6101 | |
Mark Fasheh | f186373 | 2012-08-08 11:32:27 -0700 | [diff] [blame] | 6102 | if (inode->i_nlink >= BTRFS_LINK_MAX) |
Al Viro | c055e99 | 2011-03-04 17:15:18 +0000 | [diff] [blame] | 6103 | return -EMLINK; |
Josef Bacik | 9ed74f2 | 2009-09-11 16:12:44 -0400 | [diff] [blame] | 6104 | |
Chris Mason | 3de4586 | 2008-11-17 21:02:50 -0500 | [diff] [blame] | 6105 | err = btrfs_set_inode_index(dir, &index); |
Josef Bacik | aec7477 | 2008-07-24 12:12:38 -0400 | [diff] [blame] | 6106 | if (err) |
| 6107 | goto fail; |
| 6108 | |
Yan, Zheng | a22285a | 2010-05-16 10:48:46 -0400 | [diff] [blame] | 6109 | /* |
Miao Xie | 7e6b646 | 2011-02-18 09:21:17 +0000 | [diff] [blame] | 6110 | * 2 items for inode and inode ref |
Yan, Zheng | a22285a | 2010-05-16 10:48:46 -0400 | [diff] [blame] | 6111 | * 2 items for dir items |
Miao Xie | 7e6b646 | 2011-02-18 09:21:17 +0000 | [diff] [blame] | 6112 | * 1 item for parent inode |
Yan, Zheng | a22285a | 2010-05-16 10:48:46 -0400 | [diff] [blame] | 6113 | */ |
Miao Xie | 7e6b646 | 2011-02-18 09:21:17 +0000 | [diff] [blame] | 6114 | trans = btrfs_start_transaction(root, 5); |
Yan, Zheng | a22285a | 2010-05-16 10:48:46 -0400 | [diff] [blame] | 6115 | if (IS_ERR(trans)) { |
| 6116 | err = PTR_ERR(trans); |
| 6117 | goto fail; |
| 6118 | } |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 6119 | |
Miao Xie | 67de117 | 2013-12-26 13:07:06 +0800 | [diff] [blame] | 6120 | /* There are several dir indexes for this inode, clear the cache. */ |
| 6121 | BTRFS_I(inode)->dir_index = 0ULL; |
Zach Brown | 8b558c5 | 2013-10-16 12:10:34 -0700 | [diff] [blame] | 6122 | inc_nlink(inode); |
Josef Bacik | 0c4d2d9 | 2012-04-05 15:03:02 -0400 | [diff] [blame] | 6123 | inode_inc_iversion(inode); |
Miao Xie | 3153495 | 2011-04-13 13:19:21 +0800 | [diff] [blame] | 6124 | inode->i_ctime = CURRENT_TIME; |
Al Viro | 7de9c6ee | 2010-10-23 11:11:40 -0400 | [diff] [blame] | 6125 | ihold(inode); |
Josef Bacik | e997615 | 2012-10-11 15:53:56 -0400 | [diff] [blame] | 6126 | set_bit(BTRFS_INODE_COPY_EVERYTHING, &BTRFS_I(inode)->runtime_flags); |
Josef Bacik | aec7477 | 2008-07-24 12:12:38 -0400 | [diff] [blame] | 6127 | |
Josef Bacik | a1b075d | 2010-11-19 20:36:11 +0000 | [diff] [blame] | 6128 | err = btrfs_add_nondir(trans, dir, dentry, inode, 1, index); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 6129 | |
Yan, Zheng | a571952 | 2009-09-24 09:17:31 -0400 | [diff] [blame] | 6130 | if (err) { |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 6131 | drop_inode = 1; |
Yan, Zheng | a571952 | 2009-09-24 09:17:31 -0400 | [diff] [blame] | 6132 | } else { |
Al Viro | 10d9f30 | 2011-07-16 23:09:10 -0400 | [diff] [blame] | 6133 | struct dentry *parent = dentry->d_parent; |
Yan, Zheng | a571952 | 2009-09-24 09:17:31 -0400 | [diff] [blame] | 6134 | err = btrfs_update_inode(trans, root, inode); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 6135 | if (err) |
| 6136 | goto fail; |
Filipe Manana | ef3b9af | 2014-04-27 20:40:45 +0100 | [diff] [blame] | 6137 | if (inode->i_nlink == 1) { |
| 6138 | /* |
| 6139 | * If new hard link count is 1, it's a file created |
| 6140 | * with open(2) O_TMPFILE flag. |
| 6141 | */ |
| 6142 | err = btrfs_orphan_del(trans, inode); |
| 6143 | if (err) |
| 6144 | goto fail; |
| 6145 | } |
Al Viro | 08c422c | 2011-12-23 07:58:13 -0500 | [diff] [blame] | 6146 | d_instantiate(dentry, inode); |
Josef Bacik | 6a91221 | 2010-11-20 09:48:00 +0000 | [diff] [blame] | 6147 | btrfs_log_new_name(trans, inode, NULL, parent); |
Yan, Zheng | a571952 | 2009-09-24 09:17:31 -0400 | [diff] [blame] | 6148 | } |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 6149 | |
Josef Bacik | 7ad85bb | 2012-01-12 19:10:12 -0500 | [diff] [blame] | 6150 | btrfs_end_transaction(trans, root); |
Josef Bacik | c581afc | 2014-02-06 16:06:06 -0500 | [diff] [blame] | 6151 | btrfs_balance_delayed_items(root); |
Chris Mason | 1832a6d | 2007-12-21 16:27:21 -0500 | [diff] [blame] | 6152 | fail: |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 6153 | if (drop_inode) { |
| 6154 | inode_dec_link_count(inode); |
| 6155 | iput(inode); |
| 6156 | } |
Liu Bo | b53d3f5 | 2012-11-14 14:34:34 +0000 | [diff] [blame] | 6157 | btrfs_btree_balance_dirty(root); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 6158 | return err; |
| 6159 | } |
| 6160 | |
Al Viro | 18bb1db | 2011-07-26 01:41:39 -0400 | [diff] [blame] | 6161 | 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] | 6162 | { |
Chris Mason | b9d8666 | 2008-05-02 16:13:49 -0400 | [diff] [blame] | 6163 | struct inode *inode = NULL; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 6164 | struct btrfs_trans_handle *trans; |
| 6165 | struct btrfs_root *root = BTRFS_I(dir)->root; |
| 6166 | int err = 0; |
| 6167 | int drop_on_err = 0; |
Chris Mason | b9d8666 | 2008-05-02 16:13:49 -0400 | [diff] [blame] | 6168 | u64 objectid = 0; |
Chris Mason | 00e4e6b | 2008-08-05 11:18:09 -0400 | [diff] [blame] | 6169 | u64 index = 0; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 6170 | |
Josef Bacik | 9ed74f2 | 2009-09-11 16:12:44 -0400 | [diff] [blame] | 6171 | /* |
| 6172 | * 2 items for inode and ref |
| 6173 | * 2 items for dir items |
| 6174 | * 1 for xattr if selinux is on |
| 6175 | */ |
Yan, Zheng | a22285a | 2010-05-16 10:48:46 -0400 | [diff] [blame] | 6176 | trans = btrfs_start_transaction(root, 5); |
| 6177 | if (IS_ERR(trans)) |
| 6178 | return PTR_ERR(trans); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 6179 | |
Li Zefan | 581bb05 | 2011-04-20 10:06:11 +0800 | [diff] [blame] | 6180 | err = btrfs_find_free_ino(root, &objectid); |
| 6181 | if (err) |
| 6182 | goto out_fail; |
| 6183 | |
Josef Bacik | aec7477 | 2008-07-24 12:12:38 -0400 | [diff] [blame] | 6184 | inode = btrfs_new_inode(trans, root, dir, dentry->d_name.name, |
Li Zefan | 33345d01 | 2011-04-20 10:31:50 +0800 | [diff] [blame] | 6185 | dentry->d_name.len, btrfs_ino(dir), objectid, |
Josef Bacik | d82a6f1d | 2011-05-11 15:26:06 -0400 | [diff] [blame] | 6186 | S_IFDIR | mode, &index); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 6187 | if (IS_ERR(inode)) { |
| 6188 | err = PTR_ERR(inode); |
| 6189 | goto out_fail; |
| 6190 | } |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 6191 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 6192 | drop_on_err = 1; |
Chris Mason | b0d5d10 | 2014-09-08 13:08:51 -0700 | [diff] [blame] | 6193 | /* these must be set before we unlock the inode */ |
| 6194 | inode->i_op = &btrfs_dir_inode_operations; |
| 6195 | inode->i_fop = &btrfs_dir_file_operations; |
Josef Bacik | 33268ea | 2008-07-24 12:16:36 -0400 | [diff] [blame] | 6196 | |
Eric Paris | 2a7dba3 | 2011-02-01 11:05:39 -0500 | [diff] [blame] | 6197 | err = btrfs_init_inode_security(trans, inode, dir, &dentry->d_name); |
Josef Bacik | 33268ea | 2008-07-24 12:16:36 -0400 | [diff] [blame] | 6198 | if (err) |
Chris Mason | b0d5d10 | 2014-09-08 13:08:51 -0700 | [diff] [blame] | 6199 | goto out_fail_inode; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 6200 | |
Chris Mason | dbe674a | 2008-07-17 12:54:05 -0400 | [diff] [blame] | 6201 | btrfs_i_size_write(inode, 0); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 6202 | err = btrfs_update_inode(trans, root, inode); |
| 6203 | if (err) |
Chris Mason | b0d5d10 | 2014-09-08 13:08:51 -0700 | [diff] [blame] | 6204 | goto out_fail_inode; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 6205 | |
Josef Bacik | a1b075d | 2010-11-19 20:36:11 +0000 | [diff] [blame] | 6206 | err = btrfs_add_link(trans, dir, inode, dentry->d_name.name, |
| 6207 | dentry->d_name.len, 0, index); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 6208 | if (err) |
Chris Mason | b0d5d10 | 2014-09-08 13:08:51 -0700 | [diff] [blame] | 6209 | goto out_fail_inode; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 6210 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 6211 | d_instantiate(dentry, inode); |
Chris Mason | b0d5d10 | 2014-09-08 13:08:51 -0700 | [diff] [blame] | 6212 | /* |
| 6213 | * mkdir is special. We're unlocking after we call d_instantiate |
| 6214 | * to avoid a race with nfsd calling d_instantiate. |
| 6215 | */ |
| 6216 | unlock_new_inode(inode); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 6217 | drop_on_err = 0; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 6218 | |
| 6219 | out_fail: |
Josef Bacik | 7ad85bb | 2012-01-12 19:10:12 -0500 | [diff] [blame] | 6220 | btrfs_end_transaction(trans, root); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 6221 | if (drop_on_err) |
| 6222 | iput(inode); |
Josef Bacik | c581afc | 2014-02-06 16:06:06 -0500 | [diff] [blame] | 6223 | btrfs_balance_delayed_items(root); |
Liu Bo | b53d3f5 | 2012-11-14 14:34:34 +0000 | [diff] [blame] | 6224 | btrfs_btree_balance_dirty(root); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 6225 | return err; |
Chris Mason | b0d5d10 | 2014-09-08 13:08:51 -0700 | [diff] [blame] | 6226 | |
| 6227 | out_fail_inode: |
| 6228 | unlock_new_inode(inode); |
| 6229 | goto out_fail; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 6230 | } |
| 6231 | |
Qu Wenruo | e6c4efd | 2014-09-17 11:53:35 +0800 | [diff] [blame] | 6232 | /* Find next extent map of a given extent map, caller needs to ensure locks */ |
| 6233 | static struct extent_map *next_extent_map(struct extent_map *em) |
| 6234 | { |
| 6235 | struct rb_node *next; |
| 6236 | |
| 6237 | next = rb_next(&em->rb_node); |
| 6238 | if (!next) |
| 6239 | return NULL; |
| 6240 | return container_of(next, struct extent_map, rb_node); |
| 6241 | } |
| 6242 | |
| 6243 | static struct extent_map *prev_extent_map(struct extent_map *em) |
| 6244 | { |
| 6245 | struct rb_node *prev; |
| 6246 | |
| 6247 | prev = rb_prev(&em->rb_node); |
| 6248 | if (!prev) |
| 6249 | return NULL; |
| 6250 | return container_of(prev, struct extent_map, rb_node); |
| 6251 | } |
| 6252 | |
Chris Mason | d352ac6 | 2008-09-29 15:18:18 -0400 | [diff] [blame] | 6253 | /* helper for btfs_get_extent. Given an existing extent in the tree, |
Qu Wenruo | e6c4efd | 2014-09-17 11:53:35 +0800 | [diff] [blame] | 6254 | * the existing extent is the nearest extent to map_start, |
Chris Mason | d352ac6 | 2008-09-29 15:18:18 -0400 | [diff] [blame] | 6255 | * 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] | 6256 | * the best fitted new extent into the tree. |
Chris Mason | d352ac6 | 2008-09-29 15:18:18 -0400 | [diff] [blame] | 6257 | */ |
Chris Mason | 3b95151 | 2008-04-17 11:29:12 -0400 | [diff] [blame] | 6258 | static int merge_extent_mapping(struct extent_map_tree *em_tree, |
| 6259 | struct extent_map *existing, |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 6260 | struct extent_map *em, |
Qu Wenruo | 51f395a | 2014-08-08 13:06:20 +0800 | [diff] [blame] | 6261 | u64 map_start) |
Chris Mason | 3b95151 | 2008-04-17 11:29:12 -0400 | [diff] [blame] | 6262 | { |
Qu Wenruo | e6c4efd | 2014-09-17 11:53:35 +0800 | [diff] [blame] | 6263 | struct extent_map *prev; |
| 6264 | struct extent_map *next; |
| 6265 | u64 start; |
| 6266 | u64 end; |
Chris Mason | 3b95151 | 2008-04-17 11:29:12 -0400 | [diff] [blame] | 6267 | u64 start_diff; |
Chris Mason | 3b95151 | 2008-04-17 11:29:12 -0400 | [diff] [blame] | 6268 | |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 6269 | BUG_ON(map_start < em->start || map_start >= extent_map_end(em)); |
Qu Wenruo | e6c4efd | 2014-09-17 11:53:35 +0800 | [diff] [blame] | 6270 | |
| 6271 | if (existing->start > map_start) { |
| 6272 | next = existing; |
| 6273 | prev = prev_extent_map(next); |
| 6274 | } else { |
| 6275 | prev = existing; |
| 6276 | next = next_extent_map(prev); |
| 6277 | } |
| 6278 | |
| 6279 | start = prev ? extent_map_end(prev) : em->start; |
| 6280 | start = max_t(u64, start, em->start); |
| 6281 | end = next ? next->start : extent_map_end(em); |
| 6282 | end = min_t(u64, end, extent_map_end(em)); |
| 6283 | start_diff = start - em->start; |
| 6284 | em->start = start; |
| 6285 | em->len = end - start; |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 6286 | if (em->block_start < EXTENT_MAP_LAST_BYTE && |
| 6287 | !test_bit(EXTENT_FLAG_COMPRESSED, &em->flags)) { |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 6288 | em->block_start += start_diff; |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 6289 | em->block_len -= start_diff; |
| 6290 | } |
Josef Bacik | 09a2a8f9 | 2013-04-05 16:51:15 -0400 | [diff] [blame] | 6291 | return add_extent_mapping(em_tree, em, 0); |
Chris Mason | 3b95151 | 2008-04-17 11:29:12 -0400 | [diff] [blame] | 6292 | } |
| 6293 | |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 6294 | static noinline int uncompress_inline(struct btrfs_path *path, |
| 6295 | struct inode *inode, struct page *page, |
| 6296 | size_t pg_offset, u64 extent_offset, |
| 6297 | struct btrfs_file_extent_item *item) |
| 6298 | { |
| 6299 | int ret; |
| 6300 | struct extent_buffer *leaf = path->nodes[0]; |
| 6301 | char *tmp; |
| 6302 | size_t max_size; |
| 6303 | unsigned long inline_size; |
| 6304 | unsigned long ptr; |
Li Zefan | 261507a0 | 2010-12-17 14:21:50 +0800 | [diff] [blame] | 6305 | int compress_type; |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 6306 | |
| 6307 | WARN_ON(pg_offset != 0); |
Li Zefan | 261507a0 | 2010-12-17 14:21:50 +0800 | [diff] [blame] | 6308 | compress_type = btrfs_file_extent_compression(leaf, item); |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 6309 | max_size = btrfs_file_extent_ram_bytes(leaf, item); |
| 6310 | inline_size = btrfs_file_extent_inline_item_len(leaf, |
Ross Kirk | dd3cc16 | 2013-09-16 15:58:09 +0100 | [diff] [blame] | 6311 | btrfs_item_nr(path->slots[0])); |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 6312 | tmp = kmalloc(inline_size, GFP_NOFS); |
Tsutomu Itoh | 8d41371 | 2011-04-25 19:43:52 -0400 | [diff] [blame] | 6313 | if (!tmp) |
| 6314 | return -ENOMEM; |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 6315 | ptr = btrfs_file_extent_inline_start(item); |
| 6316 | |
| 6317 | read_extent_buffer(leaf, tmp, ptr, inline_size); |
| 6318 | |
Chris Mason | 5b050f0 | 2008-11-11 09:34:41 -0500 | [diff] [blame] | 6319 | max_size = min_t(unsigned long, PAGE_CACHE_SIZE, max_size); |
Li Zefan | 261507a0 | 2010-12-17 14:21:50 +0800 | [diff] [blame] | 6320 | ret = btrfs_decompress(compress_type, tmp, page, |
| 6321 | extent_offset, inline_size, max_size); |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 6322 | kfree(tmp); |
Zach Brown | 166ae5a | 2014-05-09 17:15:10 -0400 | [diff] [blame] | 6323 | return ret; |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 6324 | } |
| 6325 | |
Chris Mason | d352ac6 | 2008-09-29 15:18:18 -0400 | [diff] [blame] | 6326 | /* |
| 6327 | * 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] | 6328 | * the ugly parts come from merging extents from the disk with the in-ram |
| 6329 | * representation. This gets more complex because of the data=ordered code, |
Chris Mason | d352ac6 | 2008-09-29 15:18:18 -0400 | [diff] [blame] | 6330 | * where the in-ram extents might be locked pending data=ordered completion. |
| 6331 | * |
| 6332 | * This also copies inline extents directly into the page. |
| 6333 | */ |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 6334 | |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 6335 | struct extent_map *btrfs_get_extent(struct inode *inode, struct page *page, |
Chris Mason | 70dec80 | 2008-01-29 09:59:12 -0500 | [diff] [blame] | 6336 | size_t pg_offset, u64 start, u64 len, |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 6337 | int create) |
| 6338 | { |
| 6339 | int ret; |
| 6340 | int err = 0; |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 6341 | u64 extent_start = 0; |
| 6342 | u64 extent_end = 0; |
Li Zefan | 33345d01 | 2011-04-20 10:31:50 +0800 | [diff] [blame] | 6343 | u64 objectid = btrfs_ino(inode); |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 6344 | u32 found_type; |
Chris Mason | f421950 | 2008-07-22 11:18:09 -0400 | [diff] [blame] | 6345 | struct btrfs_path *path = NULL; |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 6346 | struct btrfs_root *root = BTRFS_I(inode)->root; |
| 6347 | struct btrfs_file_extent_item *item; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 6348 | struct extent_buffer *leaf; |
| 6349 | struct btrfs_key found_key; |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 6350 | struct extent_map *em = NULL; |
| 6351 | struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree; |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 6352 | struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree; |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 6353 | struct btrfs_trans_handle *trans = NULL; |
Filipe Manana | 7ffbb59 | 2014-06-09 03:48:05 +0100 | [diff] [blame] | 6354 | const bool new_inline = !page || create; |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 6355 | |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 6356 | again: |
Chris Mason | 890871b | 2009-09-02 16:24:52 -0400 | [diff] [blame] | 6357 | read_lock(&em_tree->lock); |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 6358 | em = lookup_extent_mapping(em_tree, start, len); |
Chris Mason | a061fc8 | 2008-05-07 11:43:44 -0400 | [diff] [blame] | 6359 | if (em) |
| 6360 | em->bdev = root->fs_info->fs_devices->latest_bdev; |
Chris Mason | 890871b | 2009-09-02 16:24:52 -0400 | [diff] [blame] | 6361 | read_unlock(&em_tree->lock); |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 6362 | |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 6363 | if (em) { |
Chris Mason | e1c4b74 | 2008-04-22 13:26:46 -0400 | [diff] [blame] | 6364 | if (em->start > start || em->start + em->len <= start) |
| 6365 | free_extent_map(em); |
| 6366 | else if (em->block_start == EXTENT_MAP_INLINE && page) |
Chris Mason | 70dec80 | 2008-01-29 09:59:12 -0500 | [diff] [blame] | 6367 | free_extent_map(em); |
| 6368 | else |
| 6369 | goto out; |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 6370 | } |
David Sterba | 172ddd6 | 2011-04-21 00:48:27 +0200 | [diff] [blame] | 6371 | em = alloc_extent_map(); |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 6372 | if (!em) { |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 6373 | err = -ENOMEM; |
| 6374 | goto out; |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 6375 | } |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 6376 | em->bdev = root->fs_info->fs_devices->latest_bdev; |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 6377 | em->start = EXTENT_MAP_HOLE; |
Chris Mason | 445a694 | 2008-11-10 11:53:33 -0500 | [diff] [blame] | 6378 | em->orig_start = EXTENT_MAP_HOLE; |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 6379 | em->len = (u64)-1; |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 6380 | em->block_len = (u64)-1; |
Chris Mason | f421950 | 2008-07-22 11:18:09 -0400 | [diff] [blame] | 6381 | |
| 6382 | if (!path) { |
| 6383 | path = btrfs_alloc_path(); |
Josef Bacik | 026fd31 | 2011-05-13 10:32:11 -0400 | [diff] [blame] | 6384 | if (!path) { |
| 6385 | err = -ENOMEM; |
| 6386 | goto out; |
| 6387 | } |
| 6388 | /* |
| 6389 | * Chances are we'll be called again, so go ahead and do |
| 6390 | * readahead |
| 6391 | */ |
| 6392 | path->reada = 1; |
Chris Mason | f421950 | 2008-07-22 11:18:09 -0400 | [diff] [blame] | 6393 | } |
| 6394 | |
Chris Mason | 179e29e | 2007-11-01 11:28:41 -0400 | [diff] [blame] | 6395 | ret = btrfs_lookup_file_extent(trans, root, path, |
| 6396 | objectid, start, trans != NULL); |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 6397 | if (ret < 0) { |
| 6398 | err = ret; |
| 6399 | goto out; |
| 6400 | } |
| 6401 | |
| 6402 | if (ret != 0) { |
| 6403 | if (path->slots[0] == 0) |
| 6404 | goto not_found; |
| 6405 | path->slots[0]--; |
| 6406 | } |
| 6407 | |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 6408 | leaf = path->nodes[0]; |
| 6409 | item = btrfs_item_ptr(leaf, path->slots[0], |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 6410 | struct btrfs_file_extent_item); |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 6411 | /* are we inside the extent that was found? */ |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 6412 | btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]); |
David Sterba | 962a298 | 2014-06-04 18:41:45 +0200 | [diff] [blame] | 6413 | found_type = found_key.type; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 6414 | if (found_key.objectid != objectid || |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 6415 | found_type != BTRFS_EXTENT_DATA_KEY) { |
Josef Bacik | 25a5034 | 2013-10-14 12:08:38 -0400 | [diff] [blame] | 6416 | /* |
| 6417 | * If we backup past the first extent we want to move forward |
| 6418 | * and see if there is an extent in front of us, otherwise we'll |
| 6419 | * say there is a hole for our whole search range which can |
| 6420 | * cause problems. |
| 6421 | */ |
| 6422 | extent_end = start; |
| 6423 | goto next; |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 6424 | } |
| 6425 | |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 6426 | found_type = btrfs_file_extent_type(leaf, item); |
| 6427 | extent_start = found_key.offset; |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 6428 | if (found_type == BTRFS_FILE_EXTENT_REG || |
| 6429 | found_type == BTRFS_FILE_EXTENT_PREALLOC) { |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 6430 | extent_end = extent_start + |
Chris Mason | db94535 | 2007-10-15 16:15:53 -0400 | [diff] [blame] | 6431 | btrfs_file_extent_num_bytes(leaf, item); |
Yan Zheng | 9036c10 | 2008-10-30 14:19:41 -0400 | [diff] [blame] | 6432 | } else if (found_type == BTRFS_FILE_EXTENT_INLINE) { |
| 6433 | size_t size; |
Chris Mason | 514ac8a | 2014-01-03 21:07:00 -0800 | [diff] [blame] | 6434 | size = btrfs_file_extent_inline_len(leaf, path->slots[0], item); |
Qu Wenruo | fda2832 | 2013-02-26 08:10:22 +0000 | [diff] [blame] | 6435 | extent_end = ALIGN(extent_start + size, root->sectorsize); |
Yan Zheng | 9036c10 | 2008-10-30 14:19:41 -0400 | [diff] [blame] | 6436 | } |
Josef Bacik | 25a5034 | 2013-10-14 12:08:38 -0400 | [diff] [blame] | 6437 | next: |
Yan Zheng | 9036c10 | 2008-10-30 14:19:41 -0400 | [diff] [blame] | 6438 | if (start >= extent_end) { |
| 6439 | path->slots[0]++; |
| 6440 | if (path->slots[0] >= btrfs_header_nritems(leaf)) { |
| 6441 | ret = btrfs_next_leaf(root, path); |
| 6442 | if (ret < 0) { |
| 6443 | err = ret; |
| 6444 | goto out; |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 6445 | } |
Yan Zheng | 9036c10 | 2008-10-30 14:19:41 -0400 | [diff] [blame] | 6446 | if (ret > 0) |
| 6447 | goto not_found; |
| 6448 | leaf = path->nodes[0]; |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 6449 | } |
Yan Zheng | 9036c10 | 2008-10-30 14:19:41 -0400 | [diff] [blame] | 6450 | btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]); |
| 6451 | if (found_key.objectid != objectid || |
| 6452 | found_key.type != BTRFS_EXTENT_DATA_KEY) |
| 6453 | goto not_found; |
| 6454 | if (start + len <= found_key.offset) |
| 6455 | goto not_found; |
Wang Shilong | e2eca69 | 2014-07-17 11:44:14 +0800 | [diff] [blame] | 6456 | if (start > found_key.offset) |
| 6457 | goto next; |
Yan Zheng | 9036c10 | 2008-10-30 14:19:41 -0400 | [diff] [blame] | 6458 | em->start = start; |
Josef Bacik | 70c8a91 | 2012-10-11 16:54:30 -0400 | [diff] [blame] | 6459 | em->orig_start = start; |
Yan Zheng | 9036c10 | 2008-10-30 14:19:41 -0400 | [diff] [blame] | 6460 | em->len = found_key.offset - start; |
| 6461 | goto not_found_em; |
| 6462 | } |
| 6463 | |
Filipe Manana | 7ffbb59 | 2014-06-09 03:48:05 +0100 | [diff] [blame] | 6464 | btrfs_extent_item_to_extent_map(inode, path, item, new_inline, em); |
| 6465 | |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 6466 | if (found_type == BTRFS_FILE_EXTENT_REG || |
| 6467 | found_type == BTRFS_FILE_EXTENT_PREALLOC) { |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 6468 | goto insert; |
| 6469 | } else if (found_type == BTRFS_FILE_EXTENT_INLINE) { |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 6470 | unsigned long ptr; |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 6471 | char *map; |
Chris Mason | 3326d1b | 2007-10-15 16:18:25 -0400 | [diff] [blame] | 6472 | size_t size; |
| 6473 | size_t extent_offset; |
| 6474 | size_t copy_size; |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 6475 | |
Filipe Manana | 7ffbb59 | 2014-06-09 03:48:05 +0100 | [diff] [blame] | 6476 | if (new_inline) |
Yan | 689f934 | 2007-10-29 11:41:07 -0400 | [diff] [blame] | 6477 | goto out; |
Yan | 689f934 | 2007-10-29 11:41:07 -0400 | [diff] [blame] | 6478 | |
Chris Mason | 514ac8a | 2014-01-03 21:07:00 -0800 | [diff] [blame] | 6479 | size = btrfs_file_extent_inline_len(leaf, path->slots[0], item); |
Yan Zheng | 9036c10 | 2008-10-30 14:19:41 -0400 | [diff] [blame] | 6480 | extent_offset = page_offset(page) + pg_offset - extent_start; |
Chris Mason | 70dec80 | 2008-01-29 09:59:12 -0500 | [diff] [blame] | 6481 | copy_size = min_t(u64, PAGE_CACHE_SIZE - pg_offset, |
Yan | 689f934 | 2007-10-29 11:41:07 -0400 | [diff] [blame] | 6482 | size - extent_offset); |
Chris Mason | 3326d1b | 2007-10-15 16:18:25 -0400 | [diff] [blame] | 6483 | em->start = extent_start + extent_offset; |
Qu Wenruo | fda2832 | 2013-02-26 08:10:22 +0000 | [diff] [blame] | 6484 | em->len = ALIGN(copy_size, root->sectorsize); |
Josef Bacik | b493968 | 2012-12-03 10:31:19 -0500 | [diff] [blame] | 6485 | em->orig_block_len = em->len; |
Josef Bacik | 70c8a91 | 2012-10-11 16:54:30 -0400 | [diff] [blame] | 6486 | em->orig_start = em->start; |
Yan | 689f934 | 2007-10-29 11:41:07 -0400 | [diff] [blame] | 6487 | ptr = btrfs_file_extent_inline_start(item) + extent_offset; |
Chris Mason | 179e29e | 2007-11-01 11:28:41 -0400 | [diff] [blame] | 6488 | if (create == 0 && !PageUptodate(page)) { |
Li Zefan | 261507a0 | 2010-12-17 14:21:50 +0800 | [diff] [blame] | 6489 | if (btrfs_file_extent_compression(leaf, item) != |
| 6490 | BTRFS_COMPRESS_NONE) { |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 6491 | ret = uncompress_inline(path, inode, page, |
| 6492 | pg_offset, |
| 6493 | extent_offset, item); |
Zach Brown | 166ae5a | 2014-05-09 17:15:10 -0400 | [diff] [blame] | 6494 | if (ret) { |
| 6495 | err = ret; |
| 6496 | goto out; |
| 6497 | } |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 6498 | } else { |
| 6499 | map = kmap(page); |
| 6500 | read_extent_buffer(leaf, map + pg_offset, ptr, |
| 6501 | copy_size); |
Chris Mason | 93c82d5 | 2009-09-11 12:36:29 -0400 | [diff] [blame] | 6502 | if (pg_offset + copy_size < PAGE_CACHE_SIZE) { |
| 6503 | memset(map + pg_offset + copy_size, 0, |
| 6504 | PAGE_CACHE_SIZE - pg_offset - |
| 6505 | copy_size); |
| 6506 | } |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 6507 | kunmap(page); |
| 6508 | } |
Chris Mason | 179e29e | 2007-11-01 11:28:41 -0400 | [diff] [blame] | 6509 | flush_dcache_page(page); |
| 6510 | } else if (create && PageUptodate(page)) { |
Jan Schmidt | 6bf7e08 | 2011-12-01 14:35:19 +0100 | [diff] [blame] | 6511 | BUG(); |
Chris Mason | 179e29e | 2007-11-01 11:28:41 -0400 | [diff] [blame] | 6512 | if (!trans) { |
| 6513 | kunmap(page); |
| 6514 | free_extent_map(em); |
| 6515 | em = NULL; |
Chris Mason | ff5714c | 2011-05-28 07:00:39 -0400 | [diff] [blame] | 6516 | |
David Sterba | b3b4aa7 | 2011-04-21 01:20:15 +0200 | [diff] [blame] | 6517 | btrfs_release_path(path); |
Josef Bacik | 7a7eaa4 | 2011-04-13 12:54:33 -0400 | [diff] [blame] | 6518 | trans = btrfs_join_transaction(root); |
Chris Mason | ff5714c | 2011-05-28 07:00:39 -0400 | [diff] [blame] | 6519 | |
Tsutomu Itoh | 3612b49 | 2011-01-25 02:51:38 +0000 | [diff] [blame] | 6520 | if (IS_ERR(trans)) |
| 6521 | return ERR_CAST(trans); |
Chris Mason | 179e29e | 2007-11-01 11:28:41 -0400 | [diff] [blame] | 6522 | goto again; |
| 6523 | } |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 6524 | map = kmap(page); |
Chris Mason | 70dec80 | 2008-01-29 09:59:12 -0500 | [diff] [blame] | 6525 | write_extent_buffer(leaf, map + pg_offset, ptr, |
Chris Mason | 179e29e | 2007-11-01 11:28:41 -0400 | [diff] [blame] | 6526 | copy_size); |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 6527 | kunmap(page); |
Chris Mason | 179e29e | 2007-11-01 11:28:41 -0400 | [diff] [blame] | 6528 | btrfs_mark_buffer_dirty(leaf); |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 6529 | } |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 6530 | set_extent_uptodate(io_tree, em->start, |
Arne Jansen | 507903b | 2011-04-06 10:02:20 +0000 | [diff] [blame] | 6531 | extent_map_end(em) - 1, NULL, GFP_NOFS); |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 6532 | goto insert; |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 6533 | } |
| 6534 | not_found: |
| 6535 | em->start = start; |
Josef Bacik | 70c8a91 | 2012-10-11 16:54:30 -0400 | [diff] [blame] | 6536 | em->orig_start = start; |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 6537 | em->len = len; |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 6538 | not_found_em: |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 6539 | em->block_start = EXTENT_MAP_HOLE; |
Yan Zheng | 9036c10 | 2008-10-30 14:19:41 -0400 | [diff] [blame] | 6540 | set_bit(EXTENT_FLAG_VACANCY, &em->flags); |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 6541 | insert: |
David Sterba | b3b4aa7 | 2011-04-21 01:20:15 +0200 | [diff] [blame] | 6542 | btrfs_release_path(path); |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 6543 | if (em->start > start || extent_map_end(em) <= start) { |
Simon Kirby | c2cf52e | 2013-03-19 22:41:23 +0000 | [diff] [blame] | 6544 | 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] | 6545 | em->start, em->len, start, len); |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 6546 | err = -EIO; |
| 6547 | goto out; |
| 6548 | } |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 6549 | |
| 6550 | err = 0; |
Chris Mason | 890871b | 2009-09-02 16:24:52 -0400 | [diff] [blame] | 6551 | write_lock(&em_tree->lock); |
Josef Bacik | 09a2a8f9 | 2013-04-05 16:51:15 -0400 | [diff] [blame] | 6552 | ret = add_extent_mapping(em_tree, em, 0); |
Chris Mason | 3b95151 | 2008-04-17 11:29:12 -0400 | [diff] [blame] | 6553 | /* it is possible that someone inserted the extent into the tree |
| 6554 | * while we had the lock dropped. It is also possible that |
| 6555 | * an overlapping map exists in the tree |
| 6556 | */ |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 6557 | if (ret == -EEXIST) { |
Chris Mason | 3b95151 | 2008-04-17 11:29:12 -0400 | [diff] [blame] | 6558 | struct extent_map *existing; |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 6559 | |
| 6560 | ret = 0; |
| 6561 | |
Qu Wenruo | e6c4efd | 2014-09-17 11:53:35 +0800 | [diff] [blame] | 6562 | existing = search_extent_mapping(em_tree, start, len); |
| 6563 | /* |
| 6564 | * existing will always be non-NULL, since there must be |
| 6565 | * extent causing the -EEXIST. |
| 6566 | */ |
| 6567 | if (start >= extent_map_end(existing) || |
Qu Wenruo | 32be3a1 | 2014-09-22 09:13:03 +0800 | [diff] [blame] | 6568 | start <= existing->start) { |
Qu Wenruo | e6c4efd | 2014-09-17 11:53:35 +0800 | [diff] [blame] | 6569 | /* |
| 6570 | * The existing extent map is the one nearest to |
| 6571 | * the [start, start + len) range which overlaps |
| 6572 | */ |
| 6573 | err = merge_extent_mapping(em_tree, existing, |
| 6574 | em, start); |
Chris Mason | e1c4b74 | 2008-04-22 13:26:46 -0400 | [diff] [blame] | 6575 | free_extent_map(existing); |
Qu Wenruo | e6c4efd | 2014-09-17 11:53:35 +0800 | [diff] [blame] | 6576 | if (err) { |
Chris Mason | 3b95151 | 2008-04-17 11:29:12 -0400 | [diff] [blame] | 6577 | free_extent_map(em); |
| 6578 | em = NULL; |
| 6579 | } |
| 6580 | } else { |
| 6581 | free_extent_map(em); |
| 6582 | em = existing; |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 6583 | err = 0; |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 6584 | } |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 6585 | } |
Chris Mason | 890871b | 2009-09-02 16:24:52 -0400 | [diff] [blame] | 6586 | write_unlock(&em_tree->lock); |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 6587 | out: |
liubo | 1abe9b8 | 2011-03-24 11:18:59 +0000 | [diff] [blame] | 6588 | |
Steven Rostedt | 4cd8587 | 2013-11-14 22:57:29 -0500 | [diff] [blame] | 6589 | trace_btrfs_get_extent(root, em); |
liubo | 1abe9b8 | 2011-03-24 11:18:59 +0000 | [diff] [blame] | 6590 | |
Chris Mason | f421950 | 2008-07-22 11:18:09 -0400 | [diff] [blame] | 6591 | if (path) |
| 6592 | btrfs_free_path(path); |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 6593 | if (trans) { |
| 6594 | ret = btrfs_end_transaction(trans, root); |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 6595 | if (!err) |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 6596 | err = ret; |
| 6597 | } |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 6598 | if (err) { |
| 6599 | free_extent_map(em); |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 6600 | return ERR_PTR(err); |
| 6601 | } |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 6602 | BUG_ON(!em); /* Error is always set */ |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 6603 | return em; |
| 6604 | } |
| 6605 | |
Chris Mason | ec29ed5 | 2011-02-23 16:23:20 -0500 | [diff] [blame] | 6606 | struct extent_map *btrfs_get_extent_fiemap(struct inode *inode, struct page *page, |
| 6607 | size_t pg_offset, u64 start, u64 len, |
| 6608 | int create) |
| 6609 | { |
| 6610 | struct extent_map *em; |
| 6611 | struct extent_map *hole_em = NULL; |
| 6612 | u64 range_start = start; |
| 6613 | u64 end; |
| 6614 | u64 found; |
| 6615 | u64 found_end; |
| 6616 | int err = 0; |
| 6617 | |
| 6618 | em = btrfs_get_extent(inode, page, pg_offset, start, len, create); |
| 6619 | if (IS_ERR(em)) |
| 6620 | return em; |
| 6621 | if (em) { |
| 6622 | /* |
Liu Bo | f9e4fb5 | 2013-01-07 10:10:12 +0000 | [diff] [blame] | 6623 | * if our em maps to |
| 6624 | * - a hole or |
| 6625 | * - a pre-alloc extent, |
| 6626 | * there might actually be delalloc bytes behind it. |
Chris Mason | ec29ed5 | 2011-02-23 16:23:20 -0500 | [diff] [blame] | 6627 | */ |
Liu Bo | f9e4fb5 | 2013-01-07 10:10:12 +0000 | [diff] [blame] | 6628 | if (em->block_start != EXTENT_MAP_HOLE && |
| 6629 | !test_bit(EXTENT_FLAG_PREALLOC, &em->flags)) |
Chris Mason | ec29ed5 | 2011-02-23 16:23:20 -0500 | [diff] [blame] | 6630 | return em; |
| 6631 | else |
| 6632 | hole_em = em; |
| 6633 | } |
| 6634 | |
| 6635 | /* check to see if we've wrapped (len == -1 or similar) */ |
| 6636 | end = start + len; |
| 6637 | if (end < start) |
| 6638 | end = (u64)-1; |
| 6639 | else |
| 6640 | end -= 1; |
| 6641 | |
| 6642 | em = NULL; |
| 6643 | |
| 6644 | /* ok, we didn't find anything, lets look for delalloc */ |
| 6645 | found = count_range_bits(&BTRFS_I(inode)->io_tree, &range_start, |
| 6646 | end, len, EXTENT_DELALLOC, 1); |
| 6647 | found_end = range_start + found; |
| 6648 | if (found_end < range_start) |
| 6649 | found_end = (u64)-1; |
| 6650 | |
| 6651 | /* |
| 6652 | * we didn't find anything useful, return |
| 6653 | * the original results from get_extent() |
| 6654 | */ |
| 6655 | if (range_start > end || found_end <= start) { |
| 6656 | em = hole_em; |
| 6657 | hole_em = NULL; |
| 6658 | goto out; |
| 6659 | } |
| 6660 | |
| 6661 | /* adjust the range_start to make sure it doesn't |
| 6662 | * go backwards from the start they passed in |
| 6663 | */ |
Dulshani Gunawardhana | 6787125 | 2013-10-31 10:33:04 +0530 | [diff] [blame] | 6664 | range_start = max(start, range_start); |
Chris Mason | ec29ed5 | 2011-02-23 16:23:20 -0500 | [diff] [blame] | 6665 | found = found_end - range_start; |
| 6666 | |
| 6667 | if (found > 0) { |
| 6668 | u64 hole_start = start; |
| 6669 | u64 hole_len = len; |
| 6670 | |
David Sterba | 172ddd6 | 2011-04-21 00:48:27 +0200 | [diff] [blame] | 6671 | em = alloc_extent_map(); |
Chris Mason | ec29ed5 | 2011-02-23 16:23:20 -0500 | [diff] [blame] | 6672 | if (!em) { |
| 6673 | err = -ENOMEM; |
| 6674 | goto out; |
| 6675 | } |
| 6676 | /* |
| 6677 | * when btrfs_get_extent can't find anything it |
| 6678 | * returns one huge hole |
| 6679 | * |
| 6680 | * make sure what it found really fits our range, and |
| 6681 | * adjust to make sure it is based on the start from |
| 6682 | * the caller |
| 6683 | */ |
| 6684 | if (hole_em) { |
| 6685 | u64 calc_end = extent_map_end(hole_em); |
| 6686 | |
| 6687 | if (calc_end <= start || (hole_em->start > end)) { |
| 6688 | free_extent_map(hole_em); |
| 6689 | hole_em = NULL; |
| 6690 | } else { |
| 6691 | hole_start = max(hole_em->start, start); |
| 6692 | hole_len = calc_end - hole_start; |
| 6693 | } |
| 6694 | } |
| 6695 | em->bdev = NULL; |
| 6696 | if (hole_em && range_start > hole_start) { |
| 6697 | /* our hole starts before our delalloc, so we |
| 6698 | * have to return just the parts of the hole |
| 6699 | * that go until the delalloc starts |
| 6700 | */ |
| 6701 | em->len = min(hole_len, |
| 6702 | range_start - hole_start); |
| 6703 | em->start = hole_start; |
| 6704 | em->orig_start = hole_start; |
| 6705 | /* |
| 6706 | * don't adjust block start at all, |
| 6707 | * it is fixed at EXTENT_MAP_HOLE |
| 6708 | */ |
| 6709 | em->block_start = hole_em->block_start; |
| 6710 | em->block_len = hole_len; |
Liu Bo | f9e4fb5 | 2013-01-07 10:10:12 +0000 | [diff] [blame] | 6711 | if (test_bit(EXTENT_FLAG_PREALLOC, &hole_em->flags)) |
| 6712 | set_bit(EXTENT_FLAG_PREALLOC, &em->flags); |
Chris Mason | ec29ed5 | 2011-02-23 16:23:20 -0500 | [diff] [blame] | 6713 | } else { |
| 6714 | em->start = range_start; |
| 6715 | em->len = found; |
| 6716 | em->orig_start = range_start; |
| 6717 | em->block_start = EXTENT_MAP_DELALLOC; |
| 6718 | em->block_len = found; |
| 6719 | } |
| 6720 | } else if (hole_em) { |
| 6721 | return hole_em; |
| 6722 | } |
| 6723 | out: |
| 6724 | |
| 6725 | free_extent_map(hole_em); |
| 6726 | if (err) { |
| 6727 | free_extent_map(em); |
| 6728 | return ERR_PTR(err); |
| 6729 | } |
| 6730 | return em; |
| 6731 | } |
| 6732 | |
Josef Bacik | 4b46fce | 2010-05-23 11:00:55 -0400 | [diff] [blame] | 6733 | static struct extent_map *btrfs_new_extent_direct(struct inode *inode, |
| 6734 | u64 start, u64 len) |
| 6735 | { |
| 6736 | struct btrfs_root *root = BTRFS_I(inode)->root; |
Josef Bacik | 70c8a91 | 2012-10-11 16:54:30 -0400 | [diff] [blame] | 6737 | struct extent_map *em; |
Josef Bacik | 4b46fce | 2010-05-23 11:00:55 -0400 | [diff] [blame] | 6738 | struct btrfs_key ins; |
| 6739 | u64 alloc_hint; |
| 6740 | int ret; |
Josef Bacik | 4b46fce | 2010-05-23 11:00:55 -0400 | [diff] [blame] | 6741 | |
Josef Bacik | 4b46fce | 2010-05-23 11:00:55 -0400 | [diff] [blame] | 6742 | alloc_hint = get_extent_allocation_hint(inode, start, len); |
Josef Bacik | 0036158 | 2013-08-14 14:02:47 -0400 | [diff] [blame] | 6743 | ret = btrfs_reserve_extent(root, len, root->sectorsize, 0, |
Miao Xie | e570fd2 | 2014-06-19 10:42:50 +0800 | [diff] [blame] | 6744 | alloc_hint, &ins, 1, 1); |
Josef Bacik | 0036158 | 2013-08-14 14:02:47 -0400 | [diff] [blame] | 6745 | if (ret) |
| 6746 | return ERR_PTR(ret); |
Josef Bacik | 4b46fce | 2010-05-23 11:00:55 -0400 | [diff] [blame] | 6747 | |
Josef Bacik | 70c8a91 | 2012-10-11 16:54:30 -0400 | [diff] [blame] | 6748 | em = create_pinned_em(inode, start, ins.offset, start, ins.objectid, |
Josef Bacik | cc95bef | 2013-04-04 14:31:27 -0400 | [diff] [blame] | 6749 | ins.offset, ins.offset, ins.offset, 0); |
Josef Bacik | 0036158 | 2013-08-14 14:02:47 -0400 | [diff] [blame] | 6750 | if (IS_ERR(em)) { |
Miao Xie | e570fd2 | 2014-06-19 10:42:50 +0800 | [diff] [blame] | 6751 | btrfs_free_reserved_extent(root, ins.objectid, ins.offset, 1); |
Josef Bacik | 0036158 | 2013-08-14 14:02:47 -0400 | [diff] [blame] | 6752 | return em; |
| 6753 | } |
Josef Bacik | 4b46fce | 2010-05-23 11:00:55 -0400 | [diff] [blame] | 6754 | |
| 6755 | ret = btrfs_add_ordered_extent_dio(inode, start, ins.objectid, |
| 6756 | ins.offset, ins.offset, 0); |
| 6757 | if (ret) { |
Miao Xie | e570fd2 | 2014-06-19 10:42:50 +0800 | [diff] [blame] | 6758 | btrfs_free_reserved_extent(root, ins.objectid, ins.offset, 1); |
Josef Bacik | 0036158 | 2013-08-14 14:02:47 -0400 | [diff] [blame] | 6759 | free_extent_map(em); |
| 6760 | return ERR_PTR(ret); |
Josef Bacik | 4b46fce | 2010-05-23 11:00:55 -0400 | [diff] [blame] | 6761 | } |
Josef Bacik | 0036158 | 2013-08-14 14:02:47 -0400 | [diff] [blame] | 6762 | |
Josef Bacik | 4b46fce | 2010-05-23 11:00:55 -0400 | [diff] [blame] | 6763 | return em; |
| 6764 | } |
| 6765 | |
Chris Mason | 46bfbb5 | 2010-05-26 11:04:10 -0400 | [diff] [blame] | 6766 | /* |
| 6767 | * returns 1 when the nocow is safe, < 1 on error, 0 if the |
| 6768 | * block must be cow'd |
| 6769 | */ |
Josef Bacik | 0036158 | 2013-08-14 14:02:47 -0400 | [diff] [blame] | 6770 | noinline int can_nocow_extent(struct inode *inode, u64 offset, u64 *len, |
Josef Bacik | 7ee9e44 | 2013-06-21 16:37:03 -0400 | [diff] [blame] | 6771 | u64 *orig_start, u64 *orig_block_len, |
| 6772 | u64 *ram_bytes) |
Chris Mason | 46bfbb5 | 2010-05-26 11:04:10 -0400 | [diff] [blame] | 6773 | { |
Josef Bacik | 0036158 | 2013-08-14 14:02:47 -0400 | [diff] [blame] | 6774 | struct btrfs_trans_handle *trans; |
Chris Mason | 46bfbb5 | 2010-05-26 11:04:10 -0400 | [diff] [blame] | 6775 | struct btrfs_path *path; |
| 6776 | int ret; |
| 6777 | struct extent_buffer *leaf; |
| 6778 | struct btrfs_root *root = BTRFS_I(inode)->root; |
Miao Xie | 7b2b708 | 2014-02-27 13:58:05 +0800 | [diff] [blame] | 6779 | struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree; |
Chris Mason | 46bfbb5 | 2010-05-26 11:04:10 -0400 | [diff] [blame] | 6780 | struct btrfs_file_extent_item *fi; |
| 6781 | struct btrfs_key key; |
| 6782 | u64 disk_bytenr; |
| 6783 | u64 backref_offset; |
| 6784 | u64 extent_end; |
| 6785 | u64 num_bytes; |
| 6786 | int slot; |
| 6787 | int found_type; |
Josef Bacik | 7ee9e44 | 2013-06-21 16:37:03 -0400 | [diff] [blame] | 6788 | bool nocow = (BTRFS_I(inode)->flags & BTRFS_INODE_NODATACOW); |
Miao Xie | e77751a | 2013-12-27 21:11:50 +0800 | [diff] [blame] | 6789 | |
Chris Mason | 46bfbb5 | 2010-05-26 11:04:10 -0400 | [diff] [blame] | 6790 | path = btrfs_alloc_path(); |
| 6791 | if (!path) |
| 6792 | return -ENOMEM; |
| 6793 | |
Josef Bacik | 0036158 | 2013-08-14 14:02:47 -0400 | [diff] [blame] | 6794 | ret = btrfs_lookup_file_extent(NULL, root, path, btrfs_ino(inode), |
Chris Mason | 46bfbb5 | 2010-05-26 11:04:10 -0400 | [diff] [blame] | 6795 | offset, 0); |
| 6796 | if (ret < 0) |
| 6797 | goto out; |
| 6798 | |
| 6799 | slot = path->slots[0]; |
| 6800 | if (ret == 1) { |
| 6801 | if (slot == 0) { |
| 6802 | /* can't find the item, must cow */ |
| 6803 | ret = 0; |
| 6804 | goto out; |
| 6805 | } |
| 6806 | slot--; |
| 6807 | } |
| 6808 | ret = 0; |
| 6809 | leaf = path->nodes[0]; |
| 6810 | btrfs_item_key_to_cpu(leaf, &key, slot); |
Li Zefan | 33345d01 | 2011-04-20 10:31:50 +0800 | [diff] [blame] | 6811 | if (key.objectid != btrfs_ino(inode) || |
Chris Mason | 46bfbb5 | 2010-05-26 11:04:10 -0400 | [diff] [blame] | 6812 | key.type != BTRFS_EXTENT_DATA_KEY) { |
| 6813 | /* not our file or wrong item type, must cow */ |
| 6814 | goto out; |
| 6815 | } |
| 6816 | |
| 6817 | if (key.offset > offset) { |
| 6818 | /* Wrong offset, must cow */ |
| 6819 | goto out; |
| 6820 | } |
| 6821 | |
| 6822 | fi = btrfs_item_ptr(leaf, slot, struct btrfs_file_extent_item); |
| 6823 | found_type = btrfs_file_extent_type(leaf, fi); |
| 6824 | if (found_type != BTRFS_FILE_EXTENT_REG && |
| 6825 | found_type != BTRFS_FILE_EXTENT_PREALLOC) { |
| 6826 | /* not a regular extent, must cow */ |
| 6827 | goto out; |
| 6828 | } |
Josef Bacik | 7ee9e44 | 2013-06-21 16:37:03 -0400 | [diff] [blame] | 6829 | |
| 6830 | if (!nocow && found_type == BTRFS_FILE_EXTENT_REG) |
| 6831 | goto out; |
| 6832 | |
Miao Xie | e77751a | 2013-12-27 21:11:50 +0800 | [diff] [blame] | 6833 | extent_end = key.offset + btrfs_file_extent_num_bytes(leaf, fi); |
| 6834 | if (extent_end <= offset) |
| 6835 | goto out; |
| 6836 | |
Chris Mason | 46bfbb5 | 2010-05-26 11:04:10 -0400 | [diff] [blame] | 6837 | disk_bytenr = btrfs_file_extent_disk_bytenr(leaf, fi); |
Josef Bacik | 7ee9e44 | 2013-06-21 16:37:03 -0400 | [diff] [blame] | 6838 | if (disk_bytenr == 0) |
| 6839 | goto out; |
| 6840 | |
| 6841 | if (btrfs_file_extent_compression(leaf, fi) || |
| 6842 | btrfs_file_extent_encryption(leaf, fi) || |
| 6843 | btrfs_file_extent_other_encoding(leaf, fi)) |
| 6844 | goto out; |
| 6845 | |
Chris Mason | 46bfbb5 | 2010-05-26 11:04:10 -0400 | [diff] [blame] | 6846 | backref_offset = btrfs_file_extent_offset(leaf, fi); |
| 6847 | |
Josef Bacik | 7ee9e44 | 2013-06-21 16:37:03 -0400 | [diff] [blame] | 6848 | if (orig_start) { |
| 6849 | *orig_start = key.offset - backref_offset; |
| 6850 | *orig_block_len = btrfs_file_extent_disk_num_bytes(leaf, fi); |
| 6851 | *ram_bytes = btrfs_file_extent_ram_bytes(leaf, fi); |
| 6852 | } |
Josef Bacik | eb384b5 | 2013-04-24 16:32:55 -0400 | [diff] [blame] | 6853 | |
Chris Mason | 46bfbb5 | 2010-05-26 11:04:10 -0400 | [diff] [blame] | 6854 | if (btrfs_extent_readonly(root, disk_bytenr)) |
| 6855 | goto out; |
Miao Xie | 7b2b708 | 2014-02-27 13:58:05 +0800 | [diff] [blame] | 6856 | |
| 6857 | num_bytes = min(offset + *len, extent_end) - offset; |
| 6858 | if (!nocow && found_type == BTRFS_FILE_EXTENT_PREALLOC) { |
| 6859 | u64 range_end; |
| 6860 | |
| 6861 | range_end = round_up(offset + num_bytes, root->sectorsize) - 1; |
| 6862 | ret = test_range_bit(io_tree, offset, range_end, |
| 6863 | EXTENT_DELALLOC, 0, NULL); |
| 6864 | if (ret) { |
| 6865 | ret = -EAGAIN; |
| 6866 | goto out; |
| 6867 | } |
| 6868 | } |
| 6869 | |
Josef Bacik | 1bda19e | 2013-10-18 12:10:36 -0400 | [diff] [blame] | 6870 | btrfs_release_path(path); |
Chris Mason | 46bfbb5 | 2010-05-26 11:04:10 -0400 | [diff] [blame] | 6871 | |
| 6872 | /* |
| 6873 | * look for other files referencing this extent, if we |
| 6874 | * find any we must cow |
| 6875 | */ |
Josef Bacik | 0036158 | 2013-08-14 14:02:47 -0400 | [diff] [blame] | 6876 | trans = btrfs_join_transaction(root); |
| 6877 | if (IS_ERR(trans)) { |
| 6878 | ret = 0; |
Chris Mason | 46bfbb5 | 2010-05-26 11:04:10 -0400 | [diff] [blame] | 6879 | goto out; |
Josef Bacik | 0036158 | 2013-08-14 14:02:47 -0400 | [diff] [blame] | 6880 | } |
| 6881 | |
| 6882 | ret = btrfs_cross_ref_exist(trans, root, btrfs_ino(inode), |
| 6883 | key.offset - backref_offset, disk_bytenr); |
| 6884 | btrfs_end_transaction(trans, root); |
| 6885 | if (ret) { |
| 6886 | ret = 0; |
| 6887 | goto out; |
| 6888 | } |
Chris Mason | 46bfbb5 | 2010-05-26 11:04:10 -0400 | [diff] [blame] | 6889 | |
| 6890 | /* |
| 6891 | * adjust disk_bytenr and num_bytes to cover just the bytes |
| 6892 | * in this extent we are about to write. If there |
| 6893 | * are any csums in that range we have to cow in order |
| 6894 | * to keep the csums correct |
| 6895 | */ |
| 6896 | disk_bytenr += backref_offset; |
| 6897 | disk_bytenr += offset - key.offset; |
Chris Mason | 46bfbb5 | 2010-05-26 11:04:10 -0400 | [diff] [blame] | 6898 | if (csum_exist_in_range(root, disk_bytenr, num_bytes)) |
| 6899 | goto out; |
| 6900 | /* |
| 6901 | * all of the above have passed, it is safe to overwrite this extent |
| 6902 | * without cow |
| 6903 | */ |
Josef Bacik | eb384b5 | 2013-04-24 16:32:55 -0400 | [diff] [blame] | 6904 | *len = num_bytes; |
Chris Mason | 46bfbb5 | 2010-05-26 11:04:10 -0400 | [diff] [blame] | 6905 | ret = 1; |
| 6906 | out: |
| 6907 | btrfs_free_path(path); |
| 6908 | return ret; |
| 6909 | } |
| 6910 | |
Alex Gartrell | fc4adbf | 2014-05-20 13:07:56 -0700 | [diff] [blame] | 6911 | bool btrfs_page_exists_in_range(struct inode *inode, loff_t start, loff_t end) |
| 6912 | { |
| 6913 | struct radix_tree_root *root = &inode->i_mapping->page_tree; |
| 6914 | int found = false; |
| 6915 | void **pagep = NULL; |
| 6916 | struct page *page = NULL; |
| 6917 | int start_idx; |
| 6918 | int end_idx; |
| 6919 | |
| 6920 | start_idx = start >> PAGE_CACHE_SHIFT; |
| 6921 | |
| 6922 | /* |
| 6923 | * end is the last byte in the last page. end == start is legal |
| 6924 | */ |
| 6925 | end_idx = end >> PAGE_CACHE_SHIFT; |
| 6926 | |
| 6927 | rcu_read_lock(); |
| 6928 | |
| 6929 | /* Most of the code in this while loop is lifted from |
| 6930 | * find_get_page. It's been modified to begin searching from a |
| 6931 | * page and return just the first page found in that range. If the |
| 6932 | * found idx is less than or equal to the end idx then we know that |
| 6933 | * a page exists. If no pages are found or if those pages are |
| 6934 | * outside of the range then we're fine (yay!) */ |
| 6935 | while (page == NULL && |
| 6936 | radix_tree_gang_lookup_slot(root, &pagep, NULL, start_idx, 1)) { |
| 6937 | page = radix_tree_deref_slot(pagep); |
| 6938 | if (unlikely(!page)) |
| 6939 | break; |
| 6940 | |
| 6941 | if (radix_tree_exception(page)) { |
Filipe Manana | 809f901 | 2014-06-05 13:22:25 +0100 | [diff] [blame] | 6942 | if (radix_tree_deref_retry(page)) { |
| 6943 | page = NULL; |
Alex Gartrell | fc4adbf | 2014-05-20 13:07:56 -0700 | [diff] [blame] | 6944 | continue; |
Filipe Manana | 809f901 | 2014-06-05 13:22:25 +0100 | [diff] [blame] | 6945 | } |
Alex Gartrell | fc4adbf | 2014-05-20 13:07:56 -0700 | [diff] [blame] | 6946 | /* |
| 6947 | * Otherwise, shmem/tmpfs must be storing a swap entry |
| 6948 | * here as an exceptional entry: so return it without |
| 6949 | * attempting to raise page count. |
| 6950 | */ |
Filipe Manana | 6fdef6d | 2014-06-05 13:22:26 +0100 | [diff] [blame] | 6951 | page = NULL; |
Alex Gartrell | fc4adbf | 2014-05-20 13:07:56 -0700 | [diff] [blame] | 6952 | break; /* TODO: Is this relevant for this use case? */ |
| 6953 | } |
| 6954 | |
Filipe Manana | 9140515 | 2014-06-05 13:22:24 +0100 | [diff] [blame] | 6955 | if (!page_cache_get_speculative(page)) { |
| 6956 | page = NULL; |
Alex Gartrell | fc4adbf | 2014-05-20 13:07:56 -0700 | [diff] [blame] | 6957 | continue; |
Filipe Manana | 9140515 | 2014-06-05 13:22:24 +0100 | [diff] [blame] | 6958 | } |
Alex Gartrell | fc4adbf | 2014-05-20 13:07:56 -0700 | [diff] [blame] | 6959 | |
| 6960 | /* |
| 6961 | * Has the page moved? |
| 6962 | * This is part of the lockless pagecache protocol. See |
| 6963 | * include/linux/pagemap.h for details. |
| 6964 | */ |
| 6965 | if (unlikely(page != *pagep)) { |
| 6966 | page_cache_release(page); |
| 6967 | page = NULL; |
| 6968 | } |
| 6969 | } |
| 6970 | |
| 6971 | if (page) { |
| 6972 | if (page->index <= end_idx) |
| 6973 | found = true; |
| 6974 | page_cache_release(page); |
| 6975 | } |
| 6976 | |
| 6977 | rcu_read_unlock(); |
| 6978 | return found; |
| 6979 | } |
| 6980 | |
Josef Bacik | eb838e7 | 2012-07-31 16:28:48 -0400 | [diff] [blame] | 6981 | static int lock_extent_direct(struct inode *inode, u64 lockstart, u64 lockend, |
| 6982 | struct extent_state **cached_state, int writing) |
| 6983 | { |
| 6984 | struct btrfs_ordered_extent *ordered; |
| 6985 | int ret = 0; |
| 6986 | |
| 6987 | while (1) { |
| 6988 | lock_extent_bits(&BTRFS_I(inode)->io_tree, lockstart, lockend, |
| 6989 | 0, cached_state); |
| 6990 | /* |
| 6991 | * We're concerned with the entire range that we're going to be |
| 6992 | * doing DIO to, so we need to make sure theres no ordered |
| 6993 | * extents in this range. |
| 6994 | */ |
| 6995 | ordered = btrfs_lookup_ordered_range(inode, lockstart, |
| 6996 | lockend - lockstart + 1); |
| 6997 | |
| 6998 | /* |
| 6999 | * We need to make sure there are no buffered pages in this |
| 7000 | * range either, we could have raced between the invalidate in |
| 7001 | * generic_file_direct_write and locking the extent. The |
| 7002 | * invalidate needs to happen so that reads after a write do not |
| 7003 | * get stale data. |
| 7004 | */ |
Alex Gartrell | fc4adbf | 2014-05-20 13:07:56 -0700 | [diff] [blame] | 7005 | if (!ordered && |
| 7006 | (!writing || |
| 7007 | !btrfs_page_exists_in_range(inode, lockstart, lockend))) |
Josef Bacik | eb838e7 | 2012-07-31 16:28:48 -0400 | [diff] [blame] | 7008 | break; |
| 7009 | |
| 7010 | unlock_extent_cached(&BTRFS_I(inode)->io_tree, lockstart, lockend, |
| 7011 | cached_state, GFP_NOFS); |
| 7012 | |
| 7013 | if (ordered) { |
| 7014 | btrfs_start_ordered_extent(inode, ordered, 1); |
| 7015 | btrfs_put_ordered_extent(ordered); |
| 7016 | } else { |
| 7017 | /* Screw you mmap */ |
Filipe Manana | 728404d | 2014-10-10 09:43:11 +0100 | [diff] [blame^] | 7018 | ret = btrfs_fdatawrite_range(inode, lockstart, lockend); |
Filipe Manana | 075bdbd | 2014-10-09 21:18:55 +0100 | [diff] [blame] | 7019 | if (ret) |
| 7020 | break; |
| 7021 | ret = filemap_fdatawait_range(inode->i_mapping, |
| 7022 | lockstart, |
| 7023 | lockend); |
Josef Bacik | eb838e7 | 2012-07-31 16:28:48 -0400 | [diff] [blame] | 7024 | if (ret) |
| 7025 | break; |
| 7026 | |
| 7027 | /* |
| 7028 | * If we found a page that couldn't be invalidated just |
| 7029 | * fall back to buffered. |
| 7030 | */ |
| 7031 | ret = invalidate_inode_pages2_range(inode->i_mapping, |
| 7032 | lockstart >> PAGE_CACHE_SHIFT, |
| 7033 | lockend >> PAGE_CACHE_SHIFT); |
| 7034 | if (ret) |
| 7035 | break; |
| 7036 | } |
| 7037 | |
| 7038 | cond_resched(); |
| 7039 | } |
| 7040 | |
| 7041 | return ret; |
| 7042 | } |
| 7043 | |
Josef Bacik | 69ffb54 | 2012-09-11 15:40:07 -0400 | [diff] [blame] | 7044 | static struct extent_map *create_pinned_em(struct inode *inode, u64 start, |
| 7045 | u64 len, u64 orig_start, |
| 7046 | u64 block_start, u64 block_len, |
Josef Bacik | cc95bef | 2013-04-04 14:31:27 -0400 | [diff] [blame] | 7047 | u64 orig_block_len, u64 ram_bytes, |
| 7048 | int type) |
Josef Bacik | 69ffb54 | 2012-09-11 15:40:07 -0400 | [diff] [blame] | 7049 | { |
| 7050 | struct extent_map_tree *em_tree; |
| 7051 | struct extent_map *em; |
| 7052 | struct btrfs_root *root = BTRFS_I(inode)->root; |
| 7053 | int ret; |
| 7054 | |
| 7055 | em_tree = &BTRFS_I(inode)->extent_tree; |
| 7056 | em = alloc_extent_map(); |
| 7057 | if (!em) |
| 7058 | return ERR_PTR(-ENOMEM); |
| 7059 | |
| 7060 | em->start = start; |
| 7061 | em->orig_start = orig_start; |
Josef Bacik | 2ab28f3 | 2012-10-12 15:27:49 -0400 | [diff] [blame] | 7062 | em->mod_start = start; |
| 7063 | em->mod_len = len; |
Josef Bacik | 69ffb54 | 2012-09-11 15:40:07 -0400 | [diff] [blame] | 7064 | em->len = len; |
| 7065 | em->block_len = block_len; |
| 7066 | em->block_start = block_start; |
| 7067 | em->bdev = root->fs_info->fs_devices->latest_bdev; |
Josef Bacik | b493968 | 2012-12-03 10:31:19 -0500 | [diff] [blame] | 7068 | em->orig_block_len = orig_block_len; |
Josef Bacik | cc95bef | 2013-04-04 14:31:27 -0400 | [diff] [blame] | 7069 | em->ram_bytes = ram_bytes; |
Josef Bacik | 70c8a91 | 2012-10-11 16:54:30 -0400 | [diff] [blame] | 7070 | em->generation = -1; |
Josef Bacik | 69ffb54 | 2012-09-11 15:40:07 -0400 | [diff] [blame] | 7071 | set_bit(EXTENT_FLAG_PINNED, &em->flags); |
| 7072 | if (type == BTRFS_ORDERED_PREALLOC) |
Josef Bacik | b11e234 | 2012-12-03 10:58:15 -0500 | [diff] [blame] | 7073 | set_bit(EXTENT_FLAG_FILLING, &em->flags); |
Josef Bacik | 69ffb54 | 2012-09-11 15:40:07 -0400 | [diff] [blame] | 7074 | |
| 7075 | do { |
| 7076 | btrfs_drop_extent_cache(inode, em->start, |
| 7077 | em->start + em->len - 1, 0); |
| 7078 | write_lock(&em_tree->lock); |
Josef Bacik | 09a2a8f9 | 2013-04-05 16:51:15 -0400 | [diff] [blame] | 7079 | ret = add_extent_mapping(em_tree, em, 1); |
Josef Bacik | 69ffb54 | 2012-09-11 15:40:07 -0400 | [diff] [blame] | 7080 | write_unlock(&em_tree->lock); |
| 7081 | } while (ret == -EEXIST); |
| 7082 | |
| 7083 | if (ret) { |
| 7084 | free_extent_map(em); |
| 7085 | return ERR_PTR(ret); |
| 7086 | } |
| 7087 | |
| 7088 | return em; |
| 7089 | } |
| 7090 | |
| 7091 | |
Josef Bacik | 4b46fce | 2010-05-23 11:00:55 -0400 | [diff] [blame] | 7092 | static int btrfs_get_blocks_direct(struct inode *inode, sector_t iblock, |
| 7093 | struct buffer_head *bh_result, int create) |
| 7094 | { |
| 7095 | struct extent_map *em; |
| 7096 | struct btrfs_root *root = BTRFS_I(inode)->root; |
Josef Bacik | eb838e7 | 2012-07-31 16:28:48 -0400 | [diff] [blame] | 7097 | struct extent_state *cached_state = NULL; |
Josef Bacik | 4b46fce | 2010-05-23 11:00:55 -0400 | [diff] [blame] | 7098 | u64 start = iblock << inode->i_blkbits; |
Josef Bacik | eb838e7 | 2012-07-31 16:28:48 -0400 | [diff] [blame] | 7099 | u64 lockstart, lockend; |
Josef Bacik | 4b46fce | 2010-05-23 11:00:55 -0400 | [diff] [blame] | 7100 | u64 len = bh_result->b_size; |
Josef Bacik | eb838e7 | 2012-07-31 16:28:48 -0400 | [diff] [blame] | 7101 | int unlock_bits = EXTENT_LOCKED; |
Miao Xie | 0934856 | 2013-02-07 10:12:07 +0000 | [diff] [blame] | 7102 | int ret = 0; |
Josef Bacik | eb838e7 | 2012-07-31 16:28:48 -0400 | [diff] [blame] | 7103 | |
Miao Xie | 172a504 | 2013-02-21 02:48:22 -0700 | [diff] [blame] | 7104 | if (create) |
Josef Bacik | eb838e7 | 2012-07-31 16:28:48 -0400 | [diff] [blame] | 7105 | unlock_bits |= EXTENT_DELALLOC | EXTENT_DIRTY; |
Miao Xie | 172a504 | 2013-02-21 02:48:22 -0700 | [diff] [blame] | 7106 | else |
Josef Bacik | c329861 | 2012-08-03 16:49:19 -0400 | [diff] [blame] | 7107 | len = min_t(u64, len, root->sectorsize); |
Josef Bacik | eb838e7 | 2012-07-31 16:28:48 -0400 | [diff] [blame] | 7108 | |
Josef Bacik | c329861 | 2012-08-03 16:49:19 -0400 | [diff] [blame] | 7109 | lockstart = start; |
| 7110 | lockend = start + len - 1; |
| 7111 | |
Josef Bacik | eb838e7 | 2012-07-31 16:28:48 -0400 | [diff] [blame] | 7112 | /* |
| 7113 | * If this errors out it's because we couldn't invalidate pagecache for |
| 7114 | * this range and we need to fallback to buffered. |
| 7115 | */ |
| 7116 | if (lock_extent_direct(inode, lockstart, lockend, &cached_state, create)) |
| 7117 | return -ENOTBLK; |
| 7118 | |
Josef Bacik | 4b46fce | 2010-05-23 11:00:55 -0400 | [diff] [blame] | 7119 | em = btrfs_get_extent(inode, NULL, 0, start, len, 0); |
Josef Bacik | eb838e7 | 2012-07-31 16:28:48 -0400 | [diff] [blame] | 7120 | if (IS_ERR(em)) { |
| 7121 | ret = PTR_ERR(em); |
| 7122 | goto unlock_err; |
| 7123 | } |
Josef Bacik | 4b46fce | 2010-05-23 11:00:55 -0400 | [diff] [blame] | 7124 | |
| 7125 | /* |
| 7126 | * Ok for INLINE and COMPRESSED extents we need to fallback on buffered |
| 7127 | * io. INLINE is special, and we could probably kludge it in here, but |
| 7128 | * it's still buffered so for safety lets just fall back to the generic |
| 7129 | * buffered path. |
| 7130 | * |
| 7131 | * For COMPRESSED we _have_ to read the entire extent in so we can |
| 7132 | * decompress it, so there will be buffering required no matter what we |
| 7133 | * do, so go ahead and fallback to buffered. |
| 7134 | * |
| 7135 | * We return -ENOTBLK because thats what makes DIO go ahead and go back |
| 7136 | * to buffered IO. Don't blame me, this is the price we pay for using |
| 7137 | * the generic code. |
| 7138 | */ |
| 7139 | if (test_bit(EXTENT_FLAG_COMPRESSED, &em->flags) || |
| 7140 | em->block_start == EXTENT_MAP_INLINE) { |
| 7141 | free_extent_map(em); |
Josef Bacik | eb838e7 | 2012-07-31 16:28:48 -0400 | [diff] [blame] | 7142 | ret = -ENOTBLK; |
| 7143 | goto unlock_err; |
Josef Bacik | 4b46fce | 2010-05-23 11:00:55 -0400 | [diff] [blame] | 7144 | } |
| 7145 | |
| 7146 | /* Just a good old fashioned hole, return */ |
| 7147 | if (!create && (em->block_start == EXTENT_MAP_HOLE || |
| 7148 | test_bit(EXTENT_FLAG_PREALLOC, &em->flags))) { |
| 7149 | free_extent_map(em); |
Josef Bacik | eb838e7 | 2012-07-31 16:28:48 -0400 | [diff] [blame] | 7150 | goto unlock_err; |
Josef Bacik | 4b46fce | 2010-05-23 11:00:55 -0400 | [diff] [blame] | 7151 | } |
| 7152 | |
| 7153 | /* |
| 7154 | * We don't allocate a new extent in the following cases |
| 7155 | * |
| 7156 | * 1) The inode is marked as NODATACOW. In this case we'll just use the |
| 7157 | * existing extent. |
| 7158 | * 2) The extent is marked as PREALLOC. We're good to go here and can |
| 7159 | * just use the extent. |
| 7160 | * |
| 7161 | */ |
Chris Mason | 46bfbb5 | 2010-05-26 11:04:10 -0400 | [diff] [blame] | 7162 | if (!create) { |
Josef Bacik | eb838e7 | 2012-07-31 16:28:48 -0400 | [diff] [blame] | 7163 | len = min(len, em->len - (start - em->start)); |
| 7164 | lockstart = start + len; |
| 7165 | goto unlock; |
Chris Mason | 46bfbb5 | 2010-05-26 11:04:10 -0400 | [diff] [blame] | 7166 | } |
Josef Bacik | 4b46fce | 2010-05-23 11:00:55 -0400 | [diff] [blame] | 7167 | |
| 7168 | if (test_bit(EXTENT_FLAG_PREALLOC, &em->flags) || |
| 7169 | ((BTRFS_I(inode)->flags & BTRFS_INODE_NODATACOW) && |
| 7170 | em->block_start != EXTENT_MAP_HOLE)) { |
Josef Bacik | 4b46fce | 2010-05-23 11:00:55 -0400 | [diff] [blame] | 7171 | int type; |
| 7172 | int ret; |
Josef Bacik | eb384b5 | 2013-04-24 16:32:55 -0400 | [diff] [blame] | 7173 | u64 block_start, orig_start, orig_block_len, ram_bytes; |
Josef Bacik | 4b46fce | 2010-05-23 11:00:55 -0400 | [diff] [blame] | 7174 | |
| 7175 | if (test_bit(EXTENT_FLAG_PREALLOC, &em->flags)) |
| 7176 | type = BTRFS_ORDERED_PREALLOC; |
| 7177 | else |
| 7178 | type = BTRFS_ORDERED_NOCOW; |
Chris Mason | 46bfbb5 | 2010-05-26 11:04:10 -0400 | [diff] [blame] | 7179 | len = min(len, em->len - (start - em->start)); |
Josef Bacik | 4b46fce | 2010-05-23 11:00:55 -0400 | [diff] [blame] | 7180 | block_start = em->block_start + (start - em->start); |
Chris Mason | 46bfbb5 | 2010-05-26 11:04:10 -0400 | [diff] [blame] | 7181 | |
Josef Bacik | 0036158 | 2013-08-14 14:02:47 -0400 | [diff] [blame] | 7182 | if (can_nocow_extent(inode, start, &len, &orig_start, |
Josef Bacik | 7ee9e44 | 2013-06-21 16:37:03 -0400 | [diff] [blame] | 7183 | &orig_block_len, &ram_bytes) == 1) { |
Josef Bacik | 69ffb54 | 2012-09-11 15:40:07 -0400 | [diff] [blame] | 7184 | if (type == BTRFS_ORDERED_PREALLOC) { |
| 7185 | free_extent_map(em); |
| 7186 | em = create_pinned_em(inode, start, len, |
| 7187 | orig_start, |
Josef Bacik | b493968 | 2012-12-03 10:31:19 -0500 | [diff] [blame] | 7188 | block_start, len, |
Josef Bacik | cc95bef | 2013-04-04 14:31:27 -0400 | [diff] [blame] | 7189 | orig_block_len, |
| 7190 | ram_bytes, type); |
Filipe Manana | 555e128 | 2014-07-07 12:35:21 +0100 | [diff] [blame] | 7191 | if (IS_ERR(em)) { |
| 7192 | ret = PTR_ERR(em); |
Josef Bacik | 69ffb54 | 2012-09-11 15:40:07 -0400 | [diff] [blame] | 7193 | goto unlock_err; |
Filipe Manana | 555e128 | 2014-07-07 12:35:21 +0100 | [diff] [blame] | 7194 | } |
Josef Bacik | 69ffb54 | 2012-09-11 15:40:07 -0400 | [diff] [blame] | 7195 | } |
| 7196 | |
Chris Mason | 46bfbb5 | 2010-05-26 11:04:10 -0400 | [diff] [blame] | 7197 | ret = btrfs_add_ordered_extent_dio(inode, start, |
| 7198 | block_start, len, len, type); |
Chris Mason | 46bfbb5 | 2010-05-26 11:04:10 -0400 | [diff] [blame] | 7199 | if (ret) { |
| 7200 | free_extent_map(em); |
Josef Bacik | eb838e7 | 2012-07-31 16:28:48 -0400 | [diff] [blame] | 7201 | goto unlock_err; |
Chris Mason | 46bfbb5 | 2010-05-26 11:04:10 -0400 | [diff] [blame] | 7202 | } |
| 7203 | goto unlock; |
Josef Bacik | 4b46fce | 2010-05-23 11:00:55 -0400 | [diff] [blame] | 7204 | } |
Josef Bacik | 4b46fce | 2010-05-23 11:00:55 -0400 | [diff] [blame] | 7205 | } |
Josef Bacik | 0036158 | 2013-08-14 14:02:47 -0400 | [diff] [blame] | 7206 | |
Chris Mason | 46bfbb5 | 2010-05-26 11:04:10 -0400 | [diff] [blame] | 7207 | /* |
| 7208 | * this will cow the extent, reset the len in case we changed |
| 7209 | * it above |
| 7210 | */ |
| 7211 | len = bh_result->b_size; |
Josef Bacik | 70c8a91 | 2012-10-11 16:54:30 -0400 | [diff] [blame] | 7212 | free_extent_map(em); |
| 7213 | em = btrfs_new_extent_direct(inode, start, len); |
Josef Bacik | eb838e7 | 2012-07-31 16:28:48 -0400 | [diff] [blame] | 7214 | if (IS_ERR(em)) { |
| 7215 | ret = PTR_ERR(em); |
| 7216 | goto unlock_err; |
| 7217 | } |
Chris Mason | 46bfbb5 | 2010-05-26 11:04:10 -0400 | [diff] [blame] | 7218 | len = min(len, em->len - (start - em->start)); |
| 7219 | unlock: |
Josef Bacik | 4b46fce | 2010-05-23 11:00:55 -0400 | [diff] [blame] | 7220 | bh_result->b_blocknr = (em->block_start + (start - em->start)) >> |
| 7221 | inode->i_blkbits; |
Chris Mason | 46bfbb5 | 2010-05-26 11:04:10 -0400 | [diff] [blame] | 7222 | bh_result->b_size = len; |
Josef Bacik | 4b46fce | 2010-05-23 11:00:55 -0400 | [diff] [blame] | 7223 | bh_result->b_bdev = em->bdev; |
| 7224 | set_buffer_mapped(bh_result); |
Josef Bacik | c3473e8 | 2012-06-19 10:59:00 -0400 | [diff] [blame] | 7225 | if (create) { |
| 7226 | if (!test_bit(EXTENT_FLAG_PREALLOC, &em->flags)) |
| 7227 | set_buffer_new(bh_result); |
| 7228 | |
| 7229 | /* |
| 7230 | * Need to update the i_size under the extent lock so buffered |
| 7231 | * readers will get the updated i_size when we unlock. |
| 7232 | */ |
| 7233 | if (start + len > i_size_read(inode)) |
| 7234 | i_size_write(inode, start + len); |
Miao Xie | 0934856 | 2013-02-07 10:12:07 +0000 | [diff] [blame] | 7235 | |
Miao Xie | 172a504 | 2013-02-21 02:48:22 -0700 | [diff] [blame] | 7236 | spin_lock(&BTRFS_I(inode)->lock); |
| 7237 | BTRFS_I(inode)->outstanding_extents++; |
| 7238 | spin_unlock(&BTRFS_I(inode)->lock); |
| 7239 | |
Miao Xie | 0934856 | 2013-02-07 10:12:07 +0000 | [diff] [blame] | 7240 | ret = set_extent_bit(&BTRFS_I(inode)->io_tree, lockstart, |
| 7241 | lockstart + len - 1, EXTENT_DELALLOC, NULL, |
| 7242 | &cached_state, GFP_NOFS); |
| 7243 | BUG_ON(ret); |
Josef Bacik | c3473e8 | 2012-06-19 10:59:00 -0400 | [diff] [blame] | 7244 | } |
Josef Bacik | 4b46fce | 2010-05-23 11:00:55 -0400 | [diff] [blame] | 7245 | |
Josef Bacik | eb838e7 | 2012-07-31 16:28:48 -0400 | [diff] [blame] | 7246 | /* |
| 7247 | * In the case of write we need to clear and unlock the entire range, |
| 7248 | * in the case of read we need to unlock only the end area that we |
| 7249 | * aren't using if there is any left over space. |
| 7250 | */ |
Liu Bo | 24c03fa | 2012-08-22 20:10:38 -0600 | [diff] [blame] | 7251 | if (lockstart < lockend) { |
Miao Xie | 0934856 | 2013-02-07 10:12:07 +0000 | [diff] [blame] | 7252 | clear_extent_bit(&BTRFS_I(inode)->io_tree, lockstart, |
| 7253 | lockend, unlock_bits, 1, 0, |
| 7254 | &cached_state, GFP_NOFS); |
Liu Bo | 24c03fa | 2012-08-22 20:10:38 -0600 | [diff] [blame] | 7255 | } else { |
Josef Bacik | eb838e7 | 2012-07-31 16:28:48 -0400 | [diff] [blame] | 7256 | free_extent_state(cached_state); |
Liu Bo | 24c03fa | 2012-08-22 20:10:38 -0600 | [diff] [blame] | 7257 | } |
Josef Bacik | eb838e7 | 2012-07-31 16:28:48 -0400 | [diff] [blame] | 7258 | |
Josef Bacik | 4b46fce | 2010-05-23 11:00:55 -0400 | [diff] [blame] | 7259 | free_extent_map(em); |
| 7260 | |
| 7261 | return 0; |
Josef Bacik | eb838e7 | 2012-07-31 16:28:48 -0400 | [diff] [blame] | 7262 | |
| 7263 | unlock_err: |
Josef Bacik | eb838e7 | 2012-07-31 16:28:48 -0400 | [diff] [blame] | 7264 | clear_extent_bit(&BTRFS_I(inode)->io_tree, lockstart, lockend, |
| 7265 | unlock_bits, 1, 0, &cached_state, GFP_NOFS); |
| 7266 | return ret; |
Josef Bacik | 4b46fce | 2010-05-23 11:00:55 -0400 | [diff] [blame] | 7267 | } |
| 7268 | |
Miao Xie | 8b110e3 | 2014-09-12 18:44:03 +0800 | [diff] [blame] | 7269 | static inline int submit_dio_repair_bio(struct inode *inode, struct bio *bio, |
| 7270 | int rw, int mirror_num) |
| 7271 | { |
| 7272 | struct btrfs_root *root = BTRFS_I(inode)->root; |
| 7273 | int ret; |
| 7274 | |
| 7275 | BUG_ON(rw & REQ_WRITE); |
| 7276 | |
| 7277 | bio_get(bio); |
| 7278 | |
| 7279 | ret = btrfs_bio_wq_end_io(root->fs_info, bio, |
| 7280 | BTRFS_WQ_ENDIO_DIO_REPAIR); |
| 7281 | if (ret) |
| 7282 | goto err; |
| 7283 | |
| 7284 | ret = btrfs_map_bio(root, rw, bio, mirror_num, 0); |
| 7285 | err: |
| 7286 | bio_put(bio); |
| 7287 | return ret; |
| 7288 | } |
| 7289 | |
| 7290 | static int btrfs_check_dio_repairable(struct inode *inode, |
| 7291 | struct bio *failed_bio, |
| 7292 | struct io_failure_record *failrec, |
| 7293 | int failed_mirror) |
| 7294 | { |
| 7295 | int num_copies; |
| 7296 | |
| 7297 | num_copies = btrfs_num_copies(BTRFS_I(inode)->root->fs_info, |
| 7298 | failrec->logical, failrec->len); |
| 7299 | if (num_copies == 1) { |
| 7300 | /* |
| 7301 | * we only have a single copy of the data, so don't bother with |
| 7302 | * all the retry and error correction code that follows. no |
| 7303 | * matter what the error is, it is very likely to persist. |
| 7304 | */ |
| 7305 | pr_debug("Check DIO Repairable: cannot repair, num_copies=%d, next_mirror %d, failed_mirror %d\n", |
| 7306 | num_copies, failrec->this_mirror, failed_mirror); |
| 7307 | return 0; |
| 7308 | } |
| 7309 | |
| 7310 | failrec->failed_mirror = failed_mirror; |
| 7311 | failrec->this_mirror++; |
| 7312 | if (failrec->this_mirror == failed_mirror) |
| 7313 | failrec->this_mirror++; |
| 7314 | |
| 7315 | if (failrec->this_mirror > num_copies) { |
| 7316 | pr_debug("Check DIO Repairable: (fail) num_copies=%d, next_mirror %d, failed_mirror %d\n", |
| 7317 | num_copies, failrec->this_mirror, failed_mirror); |
| 7318 | return 0; |
| 7319 | } |
| 7320 | |
| 7321 | return 1; |
| 7322 | } |
| 7323 | |
| 7324 | static int dio_read_error(struct inode *inode, struct bio *failed_bio, |
| 7325 | struct page *page, u64 start, u64 end, |
| 7326 | int failed_mirror, bio_end_io_t *repair_endio, |
| 7327 | void *repair_arg) |
| 7328 | { |
| 7329 | struct io_failure_record *failrec; |
| 7330 | struct bio *bio; |
| 7331 | int isector; |
| 7332 | int read_mode; |
| 7333 | int ret; |
| 7334 | |
| 7335 | BUG_ON(failed_bio->bi_rw & REQ_WRITE); |
| 7336 | |
| 7337 | ret = btrfs_get_io_failure_record(inode, start, end, &failrec); |
| 7338 | if (ret) |
| 7339 | return ret; |
| 7340 | |
| 7341 | ret = btrfs_check_dio_repairable(inode, failed_bio, failrec, |
| 7342 | failed_mirror); |
| 7343 | if (!ret) { |
| 7344 | free_io_failure(inode, failrec); |
| 7345 | return -EIO; |
| 7346 | } |
| 7347 | |
| 7348 | if (failed_bio->bi_vcnt > 1) |
| 7349 | read_mode = READ_SYNC | REQ_FAILFAST_DEV; |
| 7350 | else |
| 7351 | read_mode = READ_SYNC; |
| 7352 | |
| 7353 | isector = start - btrfs_io_bio(failed_bio)->logical; |
| 7354 | isector >>= inode->i_sb->s_blocksize_bits; |
| 7355 | bio = btrfs_create_repair_bio(inode, failed_bio, failrec, page, |
| 7356 | 0, isector, repair_endio, repair_arg); |
| 7357 | if (!bio) { |
| 7358 | free_io_failure(inode, failrec); |
| 7359 | return -EIO; |
| 7360 | } |
| 7361 | |
| 7362 | btrfs_debug(BTRFS_I(inode)->root->fs_info, |
| 7363 | "Repair DIO Read Error: submitting new dio read[%#x] to this_mirror=%d, in_validation=%d\n", |
| 7364 | read_mode, failrec->this_mirror, failrec->in_validation); |
| 7365 | |
| 7366 | ret = submit_dio_repair_bio(inode, bio, read_mode, |
| 7367 | failrec->this_mirror); |
| 7368 | if (ret) { |
| 7369 | free_io_failure(inode, failrec); |
| 7370 | bio_put(bio); |
| 7371 | } |
| 7372 | |
| 7373 | return ret; |
| 7374 | } |
| 7375 | |
| 7376 | struct btrfs_retry_complete { |
| 7377 | struct completion done; |
| 7378 | struct inode *inode; |
| 7379 | u64 start; |
| 7380 | int uptodate; |
| 7381 | }; |
| 7382 | |
| 7383 | static void btrfs_retry_endio_nocsum(struct bio *bio, int err) |
| 7384 | { |
| 7385 | struct btrfs_retry_complete *done = bio->bi_private; |
| 7386 | struct bio_vec *bvec; |
| 7387 | int i; |
| 7388 | |
| 7389 | if (err) |
| 7390 | goto end; |
| 7391 | |
| 7392 | done->uptodate = 1; |
| 7393 | bio_for_each_segment_all(bvec, bio, i) |
| 7394 | clean_io_failure(done->inode, done->start, bvec->bv_page, 0); |
| 7395 | end: |
| 7396 | complete(&done->done); |
| 7397 | bio_put(bio); |
| 7398 | } |
| 7399 | |
| 7400 | static int __btrfs_correct_data_nocsum(struct inode *inode, |
| 7401 | struct btrfs_io_bio *io_bio) |
Josef Bacik | 4b46fce | 2010-05-23 11:00:55 -0400 | [diff] [blame] | 7402 | { |
Kent Overstreet | 2c30c71 | 2013-11-07 12:20:26 -0800 | [diff] [blame] | 7403 | struct bio_vec *bvec; |
Miao Xie | 8b110e3 | 2014-09-12 18:44:03 +0800 | [diff] [blame] | 7404 | struct btrfs_retry_complete done; |
Josef Bacik | 4b46fce | 2010-05-23 11:00:55 -0400 | [diff] [blame] | 7405 | u64 start; |
Kent Overstreet | 2c30c71 | 2013-11-07 12:20:26 -0800 | [diff] [blame] | 7406 | int i; |
Miao Xie | c1dc089 | 2014-09-12 18:43:56 +0800 | [diff] [blame] | 7407 | int ret; |
Miao Xie | dc380ae | 2014-09-12 18:43:55 +0800 | [diff] [blame] | 7408 | |
Miao Xie | c1dc089 | 2014-09-12 18:43:56 +0800 | [diff] [blame] | 7409 | start = io_bio->logical; |
Miao Xie | 8b110e3 | 2014-09-12 18:44:03 +0800 | [diff] [blame] | 7410 | done.inode = inode; |
| 7411 | |
| 7412 | bio_for_each_segment_all(bvec, &io_bio->bio, i) { |
| 7413 | try_again: |
| 7414 | done.uptodate = 0; |
| 7415 | done.start = start; |
| 7416 | init_completion(&done.done); |
| 7417 | |
| 7418 | ret = dio_read_error(inode, &io_bio->bio, bvec->bv_page, start, |
| 7419 | start + bvec->bv_len - 1, |
| 7420 | io_bio->mirror_num, |
| 7421 | btrfs_retry_endio_nocsum, &done); |
| 7422 | if (ret) |
| 7423 | return ret; |
| 7424 | |
| 7425 | wait_for_completion(&done.done); |
| 7426 | |
| 7427 | if (!done.uptodate) { |
| 7428 | /* We might have another mirror, so try again */ |
| 7429 | goto try_again; |
| 7430 | } |
| 7431 | |
| 7432 | start += bvec->bv_len; |
| 7433 | } |
| 7434 | |
| 7435 | return 0; |
| 7436 | } |
| 7437 | |
| 7438 | static void btrfs_retry_endio(struct bio *bio, int err) |
| 7439 | { |
| 7440 | struct btrfs_retry_complete *done = bio->bi_private; |
| 7441 | struct btrfs_io_bio *io_bio = btrfs_io_bio(bio); |
| 7442 | struct bio_vec *bvec; |
| 7443 | int uptodate; |
| 7444 | int ret; |
| 7445 | int i; |
| 7446 | |
| 7447 | if (err) |
| 7448 | goto end; |
| 7449 | |
| 7450 | uptodate = 1; |
| 7451 | bio_for_each_segment_all(bvec, bio, i) { |
| 7452 | ret = __readpage_endio_check(done->inode, io_bio, i, |
| 7453 | bvec->bv_page, 0, |
| 7454 | done->start, bvec->bv_len); |
| 7455 | if (!ret) |
| 7456 | clean_io_failure(done->inode, done->start, |
| 7457 | bvec->bv_page, 0); |
| 7458 | else |
| 7459 | uptodate = 0; |
| 7460 | } |
| 7461 | |
| 7462 | done->uptodate = uptodate; |
| 7463 | end: |
| 7464 | complete(&done->done); |
| 7465 | bio_put(bio); |
| 7466 | } |
| 7467 | |
| 7468 | static int __btrfs_subio_endio_read(struct inode *inode, |
| 7469 | struct btrfs_io_bio *io_bio, int err) |
| 7470 | { |
| 7471 | struct bio_vec *bvec; |
| 7472 | struct btrfs_retry_complete done; |
| 7473 | u64 start; |
| 7474 | u64 offset = 0; |
| 7475 | int i; |
| 7476 | int ret; |
| 7477 | |
| 7478 | err = 0; |
| 7479 | start = io_bio->logical; |
| 7480 | done.inode = inode; |
| 7481 | |
Miao Xie | c1dc089 | 2014-09-12 18:43:56 +0800 | [diff] [blame] | 7482 | bio_for_each_segment_all(bvec, &io_bio->bio, i) { |
Miao Xie | dc380ae | 2014-09-12 18:43:55 +0800 | [diff] [blame] | 7483 | ret = __readpage_endio_check(inode, io_bio, i, bvec->bv_page, |
| 7484 | 0, start, bvec->bv_len); |
Miao Xie | 8b110e3 | 2014-09-12 18:44:03 +0800 | [diff] [blame] | 7485 | if (likely(!ret)) |
| 7486 | goto next; |
| 7487 | try_again: |
| 7488 | done.uptodate = 0; |
| 7489 | done.start = start; |
| 7490 | init_completion(&done.done); |
| 7491 | |
| 7492 | ret = dio_read_error(inode, &io_bio->bio, bvec->bv_page, start, |
| 7493 | start + bvec->bv_len - 1, |
| 7494 | io_bio->mirror_num, |
| 7495 | btrfs_retry_endio, &done); |
| 7496 | if (ret) { |
| 7497 | err = ret; |
| 7498 | goto next; |
| 7499 | } |
| 7500 | |
| 7501 | wait_for_completion(&done.done); |
| 7502 | |
| 7503 | if (!done.uptodate) { |
| 7504 | /* We might have another mirror, so try again */ |
| 7505 | goto try_again; |
| 7506 | } |
| 7507 | next: |
| 7508 | offset += bvec->bv_len; |
Josef Bacik | 4b46fce | 2010-05-23 11:00:55 -0400 | [diff] [blame] | 7509 | start += bvec->bv_len; |
Kent Overstreet | 2c30c71 | 2013-11-07 12:20:26 -0800 | [diff] [blame] | 7510 | } |
Miao Xie | c1dc089 | 2014-09-12 18:43:56 +0800 | [diff] [blame] | 7511 | |
| 7512 | return err; |
| 7513 | } |
| 7514 | |
Miao Xie | 8b110e3 | 2014-09-12 18:44:03 +0800 | [diff] [blame] | 7515 | static int btrfs_subio_endio_read(struct inode *inode, |
| 7516 | struct btrfs_io_bio *io_bio, int err) |
| 7517 | { |
| 7518 | bool skip_csum = BTRFS_I(inode)->flags & BTRFS_INODE_NODATASUM; |
| 7519 | |
| 7520 | if (skip_csum) { |
| 7521 | if (unlikely(err)) |
| 7522 | return __btrfs_correct_data_nocsum(inode, io_bio); |
| 7523 | else |
| 7524 | return 0; |
| 7525 | } else { |
| 7526 | return __btrfs_subio_endio_read(inode, io_bio, err); |
| 7527 | } |
| 7528 | } |
| 7529 | |
Miao Xie | c1dc089 | 2014-09-12 18:43:56 +0800 | [diff] [blame] | 7530 | static void btrfs_endio_direct_read(struct bio *bio, int err) |
| 7531 | { |
| 7532 | struct btrfs_dio_private *dip = bio->bi_private; |
| 7533 | struct inode *inode = dip->inode; |
| 7534 | struct bio *dio_bio; |
| 7535 | struct btrfs_io_bio *io_bio = btrfs_io_bio(bio); |
| 7536 | |
Miao Xie | 8b110e3 | 2014-09-12 18:44:03 +0800 | [diff] [blame] | 7537 | if (dip->flags & BTRFS_DIO_ORIG_BIO_SUBMITTED) |
| 7538 | err = btrfs_subio_endio_read(inode, io_bio, err); |
Miao Xie | c1dc089 | 2014-09-12 18:43:56 +0800 | [diff] [blame] | 7539 | |
Josef Bacik | 4b46fce | 2010-05-23 11:00:55 -0400 | [diff] [blame] | 7540 | unlock_extent(&BTRFS_I(inode)->io_tree, dip->logical_offset, |
Jeff Mahoney | d008237 | 2012-03-01 14:57:19 +0100 | [diff] [blame] | 7541 | dip->logical_offset + dip->bytes - 1); |
Chris Mason | 9be3395 | 2013-05-17 18:30:14 -0400 | [diff] [blame] | 7542 | dio_bio = dip->dio_bio; |
Josef Bacik | 4b46fce | 2010-05-23 11:00:55 -0400 | [diff] [blame] | 7543 | |
Josef Bacik | 4b46fce | 2010-05-23 11:00:55 -0400 | [diff] [blame] | 7544 | kfree(dip); |
Josef Bacik | c0da7aa | 2011-03-22 11:05:07 -0400 | [diff] [blame] | 7545 | |
| 7546 | /* If we had a csum failure make sure to clear the uptodate flag */ |
| 7547 | if (err) |
Chris Mason | 9be3395 | 2013-05-17 18:30:14 -0400 | [diff] [blame] | 7548 | clear_bit(BIO_UPTODATE, &dio_bio->bi_flags); |
| 7549 | dio_end_io(dio_bio, err); |
Miao Xie | 23ea8e5 | 2014-09-12 18:43:54 +0800 | [diff] [blame] | 7550 | |
| 7551 | if (io_bio->end_io) |
| 7552 | io_bio->end_io(io_bio, err); |
Chris Mason | 9be3395 | 2013-05-17 18:30:14 -0400 | [diff] [blame] | 7553 | bio_put(bio); |
Josef Bacik | 4b46fce | 2010-05-23 11:00:55 -0400 | [diff] [blame] | 7554 | } |
| 7555 | |
| 7556 | static void btrfs_endio_direct_write(struct bio *bio, int err) |
| 7557 | { |
| 7558 | struct btrfs_dio_private *dip = bio->bi_private; |
| 7559 | struct inode *inode = dip->inode; |
| 7560 | struct btrfs_root *root = BTRFS_I(inode)->root; |
Josef Bacik | 4b46fce | 2010-05-23 11:00:55 -0400 | [diff] [blame] | 7561 | struct btrfs_ordered_extent *ordered = NULL; |
Chris Mason | 163cf09 | 2010-11-28 19:56:33 -0500 | [diff] [blame] | 7562 | u64 ordered_offset = dip->logical_offset; |
| 7563 | u64 ordered_bytes = dip->bytes; |
Chris Mason | 9be3395 | 2013-05-17 18:30:14 -0400 | [diff] [blame] | 7564 | struct bio *dio_bio; |
Josef Bacik | 4b46fce | 2010-05-23 11:00:55 -0400 | [diff] [blame] | 7565 | int ret; |
| 7566 | |
| 7567 | if (err) |
| 7568 | goto out_done; |
Chris Mason | 163cf09 | 2010-11-28 19:56:33 -0500 | [diff] [blame] | 7569 | again: |
| 7570 | ret = btrfs_dec_test_first_ordered_pending(inode, &ordered, |
| 7571 | &ordered_offset, |
Josef Bacik | 5fd0204 | 2012-05-02 14:00:54 -0400 | [diff] [blame] | 7572 | ordered_bytes, !err); |
Josef Bacik | 4b46fce | 2010-05-23 11:00:55 -0400 | [diff] [blame] | 7573 | if (!ret) |
Chris Mason | 163cf09 | 2010-11-28 19:56:33 -0500 | [diff] [blame] | 7574 | goto out_test; |
Josef Bacik | 4b46fce | 2010-05-23 11:00:55 -0400 | [diff] [blame] | 7575 | |
Liu Bo | 9e0af23 | 2014-08-15 23:36:53 +0800 | [diff] [blame] | 7576 | btrfs_init_work(&ordered->work, btrfs_endio_write_helper, |
| 7577 | finish_ordered_fn, NULL, NULL); |
Qu Wenruo | fccb5d8 | 2014-02-28 10:46:10 +0800 | [diff] [blame] | 7578 | btrfs_queue_work(root->fs_info->endio_write_workers, |
| 7579 | &ordered->work); |
Chris Mason | 163cf09 | 2010-11-28 19:56:33 -0500 | [diff] [blame] | 7580 | out_test: |
| 7581 | /* |
| 7582 | * our bio might span multiple ordered extents. If we haven't |
| 7583 | * completed the accounting for the whole dio, go back and try again |
| 7584 | */ |
| 7585 | if (ordered_offset < dip->logical_offset + dip->bytes) { |
| 7586 | ordered_bytes = dip->logical_offset + dip->bytes - |
| 7587 | ordered_offset; |
Josef Bacik | 5fd0204 | 2012-05-02 14:00:54 -0400 | [diff] [blame] | 7588 | ordered = NULL; |
Chris Mason | 163cf09 | 2010-11-28 19:56:33 -0500 | [diff] [blame] | 7589 | goto again; |
| 7590 | } |
Josef Bacik | 4b46fce | 2010-05-23 11:00:55 -0400 | [diff] [blame] | 7591 | out_done: |
Chris Mason | 9be3395 | 2013-05-17 18:30:14 -0400 | [diff] [blame] | 7592 | dio_bio = dip->dio_bio; |
Josef Bacik | 4b46fce | 2010-05-23 11:00:55 -0400 | [diff] [blame] | 7593 | |
Josef Bacik | 4b46fce | 2010-05-23 11:00:55 -0400 | [diff] [blame] | 7594 | kfree(dip); |
Josef Bacik | c0da7aa | 2011-03-22 11:05:07 -0400 | [diff] [blame] | 7595 | |
| 7596 | /* If we had an error make sure to clear the uptodate flag */ |
| 7597 | if (err) |
Chris Mason | 9be3395 | 2013-05-17 18:30:14 -0400 | [diff] [blame] | 7598 | clear_bit(BIO_UPTODATE, &dio_bio->bi_flags); |
| 7599 | dio_end_io(dio_bio, err); |
| 7600 | bio_put(bio); |
Josef Bacik | 4b46fce | 2010-05-23 11:00:55 -0400 | [diff] [blame] | 7601 | } |
| 7602 | |
Chris Mason | eaf25d9 | 2010-05-25 09:48:28 -0400 | [diff] [blame] | 7603 | static int __btrfs_submit_bio_start_direct_io(struct inode *inode, int rw, |
| 7604 | struct bio *bio, int mirror_num, |
| 7605 | unsigned long bio_flags, u64 offset) |
| 7606 | { |
| 7607 | int ret; |
| 7608 | struct btrfs_root *root = BTRFS_I(inode)->root; |
| 7609 | ret = btrfs_csum_one_bio(root, inode, bio, offset, 1); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 7610 | BUG_ON(ret); /* -ENOMEM */ |
Chris Mason | eaf25d9 | 2010-05-25 09:48:28 -0400 | [diff] [blame] | 7611 | return 0; |
| 7612 | } |
| 7613 | |
Miao Xie | e65e153 | 2010-11-22 03:04:43 +0000 | [diff] [blame] | 7614 | static void btrfs_end_dio_bio(struct bio *bio, int err) |
| 7615 | { |
| 7616 | struct btrfs_dio_private *dip = bio->bi_private; |
| 7617 | |
Miao Xie | 8b110e3 | 2014-09-12 18:44:03 +0800 | [diff] [blame] | 7618 | if (err) |
| 7619 | btrfs_warn(BTRFS_I(dip->inode)->root->fs_info, |
| 7620 | "direct IO failed ino %llu rw %lu sector %#Lx len %u err no %d", |
| 7621 | btrfs_ino(dip->inode), bio->bi_rw, |
| 7622 | (unsigned long long)bio->bi_iter.bi_sector, |
| 7623 | bio->bi_iter.bi_size, err); |
| 7624 | |
| 7625 | if (dip->subio_endio) |
| 7626 | err = dip->subio_endio(dip->inode, btrfs_io_bio(bio), err); |
Miao Xie | c1dc089 | 2014-09-12 18:43:56 +0800 | [diff] [blame] | 7627 | |
| 7628 | if (err) { |
Miao Xie | e65e153 | 2010-11-22 03:04:43 +0000 | [diff] [blame] | 7629 | dip->errors = 1; |
| 7630 | |
| 7631 | /* |
| 7632 | * before atomic variable goto zero, we must make sure |
| 7633 | * dip->errors is perceived to be set. |
| 7634 | */ |
Peter Zijlstra | 4e857c5 | 2014-03-17 18:06:10 +0100 | [diff] [blame] | 7635 | smp_mb__before_atomic(); |
Miao Xie | e65e153 | 2010-11-22 03:04:43 +0000 | [diff] [blame] | 7636 | } |
| 7637 | |
| 7638 | /* if there are more bios still pending for this dio, just exit */ |
| 7639 | if (!atomic_dec_and_test(&dip->pending_bios)) |
| 7640 | goto out; |
| 7641 | |
Chris Mason | 9be3395 | 2013-05-17 18:30:14 -0400 | [diff] [blame] | 7642 | if (dip->errors) { |
Miao Xie | e65e153 | 2010-11-22 03:04:43 +0000 | [diff] [blame] | 7643 | bio_io_error(dip->orig_bio); |
Chris Mason | 9be3395 | 2013-05-17 18:30:14 -0400 | [diff] [blame] | 7644 | } else { |
| 7645 | set_bit(BIO_UPTODATE, &dip->dio_bio->bi_flags); |
Miao Xie | e65e153 | 2010-11-22 03:04:43 +0000 | [diff] [blame] | 7646 | bio_endio(dip->orig_bio, 0); |
| 7647 | } |
| 7648 | out: |
| 7649 | bio_put(bio); |
| 7650 | } |
| 7651 | |
| 7652 | static struct bio *btrfs_dio_bio_alloc(struct block_device *bdev, |
| 7653 | u64 first_sector, gfp_t gfp_flags) |
| 7654 | { |
| 7655 | int nr_vecs = bio_get_nr_vecs(bdev); |
| 7656 | return btrfs_bio_alloc(bdev, first_sector, nr_vecs, gfp_flags); |
| 7657 | } |
| 7658 | |
Miao Xie | c1dc089 | 2014-09-12 18:43:56 +0800 | [diff] [blame] | 7659 | static inline int btrfs_lookup_and_bind_dio_csum(struct btrfs_root *root, |
| 7660 | struct inode *inode, |
| 7661 | struct btrfs_dio_private *dip, |
| 7662 | struct bio *bio, |
| 7663 | u64 file_offset) |
| 7664 | { |
| 7665 | struct btrfs_io_bio *io_bio = btrfs_io_bio(bio); |
| 7666 | struct btrfs_io_bio *orig_io_bio = btrfs_io_bio(dip->orig_bio); |
| 7667 | int ret; |
| 7668 | |
| 7669 | /* |
| 7670 | * We load all the csum data we need when we submit |
| 7671 | * the first bio to reduce the csum tree search and |
| 7672 | * contention. |
| 7673 | */ |
| 7674 | if (dip->logical_offset == file_offset) { |
| 7675 | ret = btrfs_lookup_bio_sums_dio(root, inode, dip->orig_bio, |
| 7676 | file_offset); |
| 7677 | if (ret) |
| 7678 | return ret; |
| 7679 | } |
| 7680 | |
| 7681 | if (bio == dip->orig_bio) |
| 7682 | return 0; |
| 7683 | |
| 7684 | file_offset -= dip->logical_offset; |
| 7685 | file_offset >>= inode->i_sb->s_blocksize_bits; |
| 7686 | io_bio->csum = (u8 *)(((u32 *)orig_io_bio->csum) + file_offset); |
| 7687 | |
| 7688 | return 0; |
| 7689 | } |
| 7690 | |
Miao Xie | e65e153 | 2010-11-22 03:04:43 +0000 | [diff] [blame] | 7691 | static inline int __btrfs_submit_dio_bio(struct bio *bio, struct inode *inode, |
| 7692 | int rw, u64 file_offset, int skip_sum, |
Josef Bacik | c329861 | 2012-08-03 16:49:19 -0400 | [diff] [blame] | 7693 | int async_submit) |
Miao Xie | e65e153 | 2010-11-22 03:04:43 +0000 | [diff] [blame] | 7694 | { |
Miao Xie | facc8a22 | 2013-07-25 19:22:34 +0800 | [diff] [blame] | 7695 | struct btrfs_dio_private *dip = bio->bi_private; |
Miao Xie | e65e153 | 2010-11-22 03:04:43 +0000 | [diff] [blame] | 7696 | int write = rw & REQ_WRITE; |
| 7697 | struct btrfs_root *root = BTRFS_I(inode)->root; |
| 7698 | int ret; |
| 7699 | |
Josef Bacik | b812ce2 | 2012-11-16 13:56:32 -0500 | [diff] [blame] | 7700 | if (async_submit) |
| 7701 | async_submit = !atomic_read(&BTRFS_I(inode)->sync_writers); |
| 7702 | |
Miao Xie | e65e153 | 2010-11-22 03:04:43 +0000 | [diff] [blame] | 7703 | bio_get(bio); |
Josef Bacik | 5fd0204 | 2012-05-02 14:00:54 -0400 | [diff] [blame] | 7704 | |
| 7705 | if (!write) { |
David Sterba | bfebd8b | 2014-07-30 00:25:45 +0200 | [diff] [blame] | 7706 | ret = btrfs_bio_wq_end_io(root->fs_info, bio, |
| 7707 | BTRFS_WQ_ENDIO_DATA); |
Josef Bacik | 5fd0204 | 2012-05-02 14:00:54 -0400 | [diff] [blame] | 7708 | if (ret) |
| 7709 | goto err; |
| 7710 | } |
Miao Xie | e65e153 | 2010-11-22 03:04:43 +0000 | [diff] [blame] | 7711 | |
Josef Bacik | 1ae3993 | 2011-04-06 14:41:34 -0400 | [diff] [blame] | 7712 | if (skip_sum) |
| 7713 | goto map; |
| 7714 | |
| 7715 | if (write && async_submit) { |
Miao Xie | e65e153 | 2010-11-22 03:04:43 +0000 | [diff] [blame] | 7716 | ret = btrfs_wq_submit_bio(root->fs_info, |
| 7717 | inode, rw, bio, 0, 0, |
| 7718 | file_offset, |
| 7719 | __btrfs_submit_bio_start_direct_io, |
| 7720 | __btrfs_submit_bio_done); |
| 7721 | goto err; |
Josef Bacik | 1ae3993 | 2011-04-06 14:41:34 -0400 | [diff] [blame] | 7722 | } else if (write) { |
| 7723 | /* |
| 7724 | * If we aren't doing async submit, calculate the csum of the |
| 7725 | * bio now. |
| 7726 | */ |
| 7727 | ret = btrfs_csum_one_bio(root, inode, bio, file_offset, 1); |
| 7728 | if (ret) |
| 7729 | goto err; |
Miao Xie | 23ea8e5 | 2014-09-12 18:43:54 +0800 | [diff] [blame] | 7730 | } else { |
Miao Xie | c1dc089 | 2014-09-12 18:43:56 +0800 | [diff] [blame] | 7731 | ret = btrfs_lookup_and_bind_dio_csum(root, inode, dip, bio, |
| 7732 | file_offset); |
Tsutomu Itoh | c2db107 | 2011-03-01 06:48:31 +0000 | [diff] [blame] | 7733 | if (ret) |
| 7734 | goto err; |
| 7735 | } |
Josef Bacik | 1ae3993 | 2011-04-06 14:41:34 -0400 | [diff] [blame] | 7736 | map: |
| 7737 | ret = btrfs_map_bio(root, rw, bio, 0, async_submit); |
Miao Xie | e65e153 | 2010-11-22 03:04:43 +0000 | [diff] [blame] | 7738 | err: |
| 7739 | bio_put(bio); |
| 7740 | return ret; |
| 7741 | } |
| 7742 | |
| 7743 | static int btrfs_submit_direct_hook(int rw, struct btrfs_dio_private *dip, |
| 7744 | int skip_sum) |
| 7745 | { |
| 7746 | struct inode *inode = dip->inode; |
| 7747 | struct btrfs_root *root = BTRFS_I(inode)->root; |
Miao Xie | e65e153 | 2010-11-22 03:04:43 +0000 | [diff] [blame] | 7748 | struct bio *bio; |
| 7749 | struct bio *orig_bio = dip->orig_bio; |
| 7750 | struct bio_vec *bvec = orig_bio->bi_io_vec; |
Kent Overstreet | 4f024f3 | 2013-10-11 15:44:27 -0700 | [diff] [blame] | 7751 | u64 start_sector = orig_bio->bi_iter.bi_sector; |
Miao Xie | e65e153 | 2010-11-22 03:04:43 +0000 | [diff] [blame] | 7752 | u64 file_offset = dip->logical_offset; |
| 7753 | u64 submit_len = 0; |
| 7754 | u64 map_length; |
| 7755 | int nr_pages = 0; |
Miao Xie | 23ea8e5 | 2014-09-12 18:43:54 +0800 | [diff] [blame] | 7756 | int ret; |
Josef Bacik | 1ae3993 | 2011-04-06 14:41:34 -0400 | [diff] [blame] | 7757 | int async_submit = 0; |
Miao Xie | e65e153 | 2010-11-22 03:04:43 +0000 | [diff] [blame] | 7758 | |
Kent Overstreet | 4f024f3 | 2013-10-11 15:44:27 -0700 | [diff] [blame] | 7759 | map_length = orig_bio->bi_iter.bi_size; |
David Woodhouse | 53b381b | 2013-01-29 18:40:14 -0500 | [diff] [blame] | 7760 | ret = btrfs_map_block(root->fs_info, rw, start_sector << 9, |
Miao Xie | e65e153 | 2010-11-22 03:04:43 +0000 | [diff] [blame] | 7761 | &map_length, NULL, 0); |
Miao Xie | 7a5c3c9 | 2014-06-17 18:58:59 +0800 | [diff] [blame] | 7762 | if (ret) |
Miao Xie | e65e153 | 2010-11-22 03:04:43 +0000 | [diff] [blame] | 7763 | return -EIO; |
Miao Xie | facc8a22 | 2013-07-25 19:22:34 +0800 | [diff] [blame] | 7764 | |
Kent Overstreet | 4f024f3 | 2013-10-11 15:44:27 -0700 | [diff] [blame] | 7765 | if (map_length >= orig_bio->bi_iter.bi_size) { |
Josef Bacik | 02f57c7 | 2011-04-06 14:25:44 -0400 | [diff] [blame] | 7766 | bio = orig_bio; |
Miao Xie | c1dc089 | 2014-09-12 18:43:56 +0800 | [diff] [blame] | 7767 | dip->flags |= BTRFS_DIO_ORIG_BIO_SUBMITTED; |
Josef Bacik | 02f57c7 | 2011-04-06 14:25:44 -0400 | [diff] [blame] | 7768 | goto submit; |
| 7769 | } |
| 7770 | |
David Woodhouse | 53b381b | 2013-01-29 18:40:14 -0500 | [diff] [blame] | 7771 | /* async crcs make it difficult to collect full stripe writes. */ |
| 7772 | if (btrfs_get_alloc_profile(root, 1) & |
| 7773 | (BTRFS_BLOCK_GROUP_RAID5 | BTRFS_BLOCK_GROUP_RAID6)) |
| 7774 | async_submit = 0; |
| 7775 | else |
| 7776 | async_submit = 1; |
| 7777 | |
Josef Bacik | 02f57c7 | 2011-04-06 14:25:44 -0400 | [diff] [blame] | 7778 | bio = btrfs_dio_bio_alloc(orig_bio->bi_bdev, start_sector, GFP_NOFS); |
| 7779 | if (!bio) |
| 7780 | return -ENOMEM; |
Miao Xie | 7a5c3c9 | 2014-06-17 18:58:59 +0800 | [diff] [blame] | 7781 | |
Josef Bacik | 02f57c7 | 2011-04-06 14:25:44 -0400 | [diff] [blame] | 7782 | bio->bi_private = dip; |
| 7783 | bio->bi_end_io = btrfs_end_dio_bio; |
Miao Xie | c1dc089 | 2014-09-12 18:43:56 +0800 | [diff] [blame] | 7784 | btrfs_io_bio(bio)->logical = file_offset; |
Josef Bacik | 02f57c7 | 2011-04-06 14:25:44 -0400 | [diff] [blame] | 7785 | atomic_inc(&dip->pending_bios); |
| 7786 | |
Miao Xie | e65e153 | 2010-11-22 03:04:43 +0000 | [diff] [blame] | 7787 | while (bvec <= (orig_bio->bi_io_vec + orig_bio->bi_vcnt - 1)) { |
David Sterba | ee39b43 | 2014-09-30 01:33:33 +0200 | [diff] [blame] | 7788 | if (map_length < submit_len + bvec->bv_len || |
Miao Xie | e65e153 | 2010-11-22 03:04:43 +0000 | [diff] [blame] | 7789 | bio_add_page(bio, bvec->bv_page, bvec->bv_len, |
David Sterba | ee39b43 | 2014-09-30 01:33:33 +0200 | [diff] [blame] | 7790 | bvec->bv_offset) < bvec->bv_len) { |
Miao Xie | e65e153 | 2010-11-22 03:04:43 +0000 | [diff] [blame] | 7791 | /* |
| 7792 | * inc the count before we submit the bio so |
| 7793 | * we know the end IO handler won't happen before |
| 7794 | * we inc the count. Otherwise, the dip might get freed |
| 7795 | * before we're done setting it up |
| 7796 | */ |
| 7797 | atomic_inc(&dip->pending_bios); |
| 7798 | ret = __btrfs_submit_dio_bio(bio, inode, rw, |
| 7799 | file_offset, skip_sum, |
Josef Bacik | c329861 | 2012-08-03 16:49:19 -0400 | [diff] [blame] | 7800 | async_submit); |
Miao Xie | e65e153 | 2010-11-22 03:04:43 +0000 | [diff] [blame] | 7801 | if (ret) { |
| 7802 | bio_put(bio); |
| 7803 | atomic_dec(&dip->pending_bios); |
| 7804 | goto out_err; |
| 7805 | } |
| 7806 | |
Miao Xie | e65e153 | 2010-11-22 03:04:43 +0000 | [diff] [blame] | 7807 | start_sector += submit_len >> 9; |
| 7808 | file_offset += submit_len; |
| 7809 | |
| 7810 | submit_len = 0; |
| 7811 | nr_pages = 0; |
| 7812 | |
| 7813 | bio = btrfs_dio_bio_alloc(orig_bio->bi_bdev, |
| 7814 | start_sector, GFP_NOFS); |
| 7815 | if (!bio) |
| 7816 | goto out_err; |
| 7817 | bio->bi_private = dip; |
| 7818 | bio->bi_end_io = btrfs_end_dio_bio; |
Miao Xie | c1dc089 | 2014-09-12 18:43:56 +0800 | [diff] [blame] | 7819 | btrfs_io_bio(bio)->logical = file_offset; |
Miao Xie | e65e153 | 2010-11-22 03:04:43 +0000 | [diff] [blame] | 7820 | |
Kent Overstreet | 4f024f3 | 2013-10-11 15:44:27 -0700 | [diff] [blame] | 7821 | map_length = orig_bio->bi_iter.bi_size; |
David Woodhouse | 53b381b | 2013-01-29 18:40:14 -0500 | [diff] [blame] | 7822 | ret = btrfs_map_block(root->fs_info, rw, |
Stefan Behrens | 3ec706c | 2012-11-05 15:46:42 +0100 | [diff] [blame] | 7823 | start_sector << 9, |
Miao Xie | e65e153 | 2010-11-22 03:04:43 +0000 | [diff] [blame] | 7824 | &map_length, NULL, 0); |
| 7825 | if (ret) { |
| 7826 | bio_put(bio); |
| 7827 | goto out_err; |
| 7828 | } |
| 7829 | } else { |
| 7830 | submit_len += bvec->bv_len; |
Dulshani Gunawardhana | 6787125 | 2013-10-31 10:33:04 +0530 | [diff] [blame] | 7831 | nr_pages++; |
Miao Xie | e65e153 | 2010-11-22 03:04:43 +0000 | [diff] [blame] | 7832 | bvec++; |
| 7833 | } |
| 7834 | } |
| 7835 | |
Josef Bacik | 02f57c7 | 2011-04-06 14:25:44 -0400 | [diff] [blame] | 7836 | submit: |
Miao Xie | e65e153 | 2010-11-22 03:04:43 +0000 | [diff] [blame] | 7837 | ret = __btrfs_submit_dio_bio(bio, inode, rw, file_offset, skip_sum, |
Josef Bacik | c329861 | 2012-08-03 16:49:19 -0400 | [diff] [blame] | 7838 | async_submit); |
Miao Xie | e65e153 | 2010-11-22 03:04:43 +0000 | [diff] [blame] | 7839 | if (!ret) |
| 7840 | return 0; |
| 7841 | |
| 7842 | bio_put(bio); |
| 7843 | out_err: |
| 7844 | dip->errors = 1; |
| 7845 | /* |
| 7846 | * before atomic variable goto zero, we must |
| 7847 | * make sure dip->errors is perceived to be set. |
| 7848 | */ |
Peter Zijlstra | 4e857c5 | 2014-03-17 18:06:10 +0100 | [diff] [blame] | 7849 | smp_mb__before_atomic(); |
Miao Xie | e65e153 | 2010-11-22 03:04:43 +0000 | [diff] [blame] | 7850 | if (atomic_dec_and_test(&dip->pending_bios)) |
| 7851 | bio_io_error(dip->orig_bio); |
| 7852 | |
| 7853 | /* bio_end_io() will handle error, so we needn't return it */ |
| 7854 | return 0; |
| 7855 | } |
| 7856 | |
Chris Mason | 9be3395 | 2013-05-17 18:30:14 -0400 | [diff] [blame] | 7857 | static void btrfs_submit_direct(int rw, struct bio *dio_bio, |
| 7858 | struct inode *inode, loff_t file_offset) |
Josef Bacik | 4b46fce | 2010-05-23 11:00:55 -0400 | [diff] [blame] | 7859 | { |
| 7860 | struct btrfs_root *root = BTRFS_I(inode)->root; |
| 7861 | struct btrfs_dio_private *dip; |
Chris Mason | 9be3395 | 2013-05-17 18:30:14 -0400 | [diff] [blame] | 7862 | struct bio *io_bio; |
Miao Xie | 23ea8e5 | 2014-09-12 18:43:54 +0800 | [diff] [blame] | 7863 | struct btrfs_io_bio *btrfs_bio; |
Josef Bacik | 4b46fce | 2010-05-23 11:00:55 -0400 | [diff] [blame] | 7864 | int skip_sum; |
Christoph Hellwig | 7b6d91d | 2010-08-07 18:20:39 +0200 | [diff] [blame] | 7865 | int write = rw & REQ_WRITE; |
Josef Bacik | 4b46fce | 2010-05-23 11:00:55 -0400 | [diff] [blame] | 7866 | int ret = 0; |
| 7867 | |
| 7868 | skip_sum = BTRFS_I(inode)->flags & BTRFS_INODE_NODATASUM; |
| 7869 | |
Chris Mason | 9be3395 | 2013-05-17 18:30:14 -0400 | [diff] [blame] | 7870 | io_bio = btrfs_bio_clone(dio_bio, GFP_NOFS); |
Chris Mason | 9be3395 | 2013-05-17 18:30:14 -0400 | [diff] [blame] | 7871 | if (!io_bio) { |
Josef Bacik | 4b46fce | 2010-05-23 11:00:55 -0400 | [diff] [blame] | 7872 | ret = -ENOMEM; |
| 7873 | goto free_ordered; |
| 7874 | } |
Josef Bacik | 4b46fce | 2010-05-23 11:00:55 -0400 | [diff] [blame] | 7875 | |
Miao Xie | c1dc089 | 2014-09-12 18:43:56 +0800 | [diff] [blame] | 7876 | dip = kzalloc(sizeof(*dip), GFP_NOFS); |
Chris Mason | 9be3395 | 2013-05-17 18:30:14 -0400 | [diff] [blame] | 7877 | if (!dip) { |
| 7878 | ret = -ENOMEM; |
| 7879 | goto free_io_bio; |
| 7880 | } |
| 7881 | |
| 7882 | dip->private = dio_bio->bi_private; |
Josef Bacik | 4b46fce | 2010-05-23 11:00:55 -0400 | [diff] [blame] | 7883 | dip->inode = inode; |
| 7884 | dip->logical_offset = file_offset; |
Kent Overstreet | 4f024f3 | 2013-10-11 15:44:27 -0700 | [diff] [blame] | 7885 | dip->bytes = dio_bio->bi_iter.bi_size; |
| 7886 | dip->disk_bytenr = (u64)dio_bio->bi_iter.bi_sector << 9; |
Chris Mason | 9be3395 | 2013-05-17 18:30:14 -0400 | [diff] [blame] | 7887 | io_bio->bi_private = dip; |
Chris Mason | 9be3395 | 2013-05-17 18:30:14 -0400 | [diff] [blame] | 7888 | dip->orig_bio = io_bio; |
| 7889 | dip->dio_bio = dio_bio; |
Miao Xie | e65e153 | 2010-11-22 03:04:43 +0000 | [diff] [blame] | 7890 | atomic_set(&dip->pending_bios, 0); |
Miao Xie | c1dc089 | 2014-09-12 18:43:56 +0800 | [diff] [blame] | 7891 | btrfs_bio = btrfs_io_bio(io_bio); |
| 7892 | btrfs_bio->logical = file_offset; |
Josef Bacik | 4b46fce | 2010-05-23 11:00:55 -0400 | [diff] [blame] | 7893 | |
Miao Xie | c1dc089 | 2014-09-12 18:43:56 +0800 | [diff] [blame] | 7894 | if (write) { |
Chris Mason | 9be3395 | 2013-05-17 18:30:14 -0400 | [diff] [blame] | 7895 | io_bio->bi_end_io = btrfs_endio_direct_write; |
Miao Xie | c1dc089 | 2014-09-12 18:43:56 +0800 | [diff] [blame] | 7896 | } else { |
Chris Mason | 9be3395 | 2013-05-17 18:30:14 -0400 | [diff] [blame] | 7897 | io_bio->bi_end_io = btrfs_endio_direct_read; |
Miao Xie | c1dc089 | 2014-09-12 18:43:56 +0800 | [diff] [blame] | 7898 | dip->subio_endio = btrfs_subio_endio_read; |
| 7899 | } |
Josef Bacik | 4b46fce | 2010-05-23 11:00:55 -0400 | [diff] [blame] | 7900 | |
Miao Xie | e65e153 | 2010-11-22 03:04:43 +0000 | [diff] [blame] | 7901 | ret = btrfs_submit_direct_hook(rw, dip, skip_sum); |
| 7902 | if (!ret) |
Chris Mason | eaf25d9 | 2010-05-25 09:48:28 -0400 | [diff] [blame] | 7903 | return; |
Chris Mason | 9be3395 | 2013-05-17 18:30:14 -0400 | [diff] [blame] | 7904 | |
Miao Xie | 23ea8e5 | 2014-09-12 18:43:54 +0800 | [diff] [blame] | 7905 | if (btrfs_bio->end_io) |
| 7906 | btrfs_bio->end_io(btrfs_bio, ret); |
Chris Mason | 9be3395 | 2013-05-17 18:30:14 -0400 | [diff] [blame] | 7907 | free_io_bio: |
| 7908 | bio_put(io_bio); |
| 7909 | |
Josef Bacik | 4b46fce | 2010-05-23 11:00:55 -0400 | [diff] [blame] | 7910 | free_ordered: |
| 7911 | /* |
| 7912 | * If this is a write, we need to clean up the reserved space and kill |
| 7913 | * the ordered extent. |
| 7914 | */ |
| 7915 | if (write) { |
| 7916 | struct btrfs_ordered_extent *ordered; |
Josef Bacik | 955256f | 2010-11-19 09:41:10 -0500 | [diff] [blame] | 7917 | ordered = btrfs_lookup_ordered_extent(inode, file_offset); |
Josef Bacik | 4b46fce | 2010-05-23 11:00:55 -0400 | [diff] [blame] | 7918 | if (!test_bit(BTRFS_ORDERED_PREALLOC, &ordered->flags) && |
| 7919 | !test_bit(BTRFS_ORDERED_NOCOW, &ordered->flags)) |
| 7920 | btrfs_free_reserved_extent(root, ordered->start, |
Miao Xie | e570fd2 | 2014-06-19 10:42:50 +0800 | [diff] [blame] | 7921 | ordered->disk_len, 1); |
Josef Bacik | 4b46fce | 2010-05-23 11:00:55 -0400 | [diff] [blame] | 7922 | btrfs_put_ordered_extent(ordered); |
| 7923 | btrfs_put_ordered_extent(ordered); |
| 7924 | } |
Chris Mason | 9be3395 | 2013-05-17 18:30:14 -0400 | [diff] [blame] | 7925 | bio_endio(dio_bio, ret); |
Josef Bacik | 4b46fce | 2010-05-23 11:00:55 -0400 | [diff] [blame] | 7926 | } |
| 7927 | |
Chris Mason | 5a5f79b | 2010-05-26 21:33:37 -0400 | [diff] [blame] | 7928 | 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] | 7929 | const struct iov_iter *iter, loff_t offset) |
Chris Mason | 5a5f79b | 2010-05-26 21:33:37 -0400 | [diff] [blame] | 7930 | { |
| 7931 | int seg; |
Josef Bacik | a1b75f7 | 2011-04-08 15:51:18 +0000 | [diff] [blame] | 7932 | int i; |
Chris Mason | 5a5f79b | 2010-05-26 21:33:37 -0400 | [diff] [blame] | 7933 | unsigned blocksize_mask = root->sectorsize - 1; |
| 7934 | ssize_t retval = -EINVAL; |
Chris Mason | 5a5f79b | 2010-05-26 21:33:37 -0400 | [diff] [blame] | 7935 | |
| 7936 | if (offset & blocksize_mask) |
| 7937 | goto out; |
| 7938 | |
Al Viro | 28060d5 | 2014-03-22 05:15:17 -0400 | [diff] [blame] | 7939 | if (iov_iter_alignment(iter) & blocksize_mask) |
| 7940 | goto out; |
Josef Bacik | a1b75f7 | 2011-04-08 15:51:18 +0000 | [diff] [blame] | 7941 | |
Al Viro | 28060d5 | 2014-03-22 05:15:17 -0400 | [diff] [blame] | 7942 | /* If this is a write we don't need to check anymore */ |
| 7943 | if (rw & WRITE) |
| 7944 | return 0; |
| 7945 | /* |
| 7946 | * Check to make sure we don't have duplicate iov_base's in this |
| 7947 | * iovec, if so return EINVAL, otherwise we'll get csum errors |
| 7948 | * when reading back. |
| 7949 | */ |
| 7950 | for (seg = 0; seg < iter->nr_segs; seg++) { |
| 7951 | for (i = seg + 1; i < iter->nr_segs; i++) { |
| 7952 | if (iter->iov[seg].iov_base == iter->iov[i].iov_base) |
Josef Bacik | a1b75f7 | 2011-04-08 15:51:18 +0000 | [diff] [blame] | 7953 | goto out; |
| 7954 | } |
Chris Mason | 5a5f79b | 2010-05-26 21:33:37 -0400 | [diff] [blame] | 7955 | } |
| 7956 | retval = 0; |
| 7957 | out: |
| 7958 | return retval; |
| 7959 | } |
Josef Bacik | eb838e7 | 2012-07-31 16:28:48 -0400 | [diff] [blame] | 7960 | |
Chris Mason | 1643298 | 2008-04-10 10:23:21 -0400 | [diff] [blame] | 7961 | static ssize_t btrfs_direct_IO(int rw, struct kiocb *iocb, |
Al Viro | d8d3d94 | 2014-03-04 21:27:34 -0500 | [diff] [blame] | 7962 | struct iov_iter *iter, loff_t offset) |
Chris Mason | 1643298 | 2008-04-10 10:23:21 -0400 | [diff] [blame] | 7963 | { |
Josef Bacik | 4b46fce | 2010-05-23 11:00:55 -0400 | [diff] [blame] | 7964 | struct file *file = iocb->ki_filp; |
| 7965 | struct inode *inode = file->f_mapping->host; |
Miao Xie | 0934856 | 2013-02-07 10:12:07 +0000 | [diff] [blame] | 7966 | size_t count = 0; |
Miao Xie | 2e60a51 | 2013-02-08 07:01:08 +0000 | [diff] [blame] | 7967 | int flags = 0; |
Miao Xie | 38851cc | 2013-02-08 07:04:11 +0000 | [diff] [blame] | 7968 | bool wakeup = true; |
| 7969 | bool relock = false; |
Miao Xie | 0934856 | 2013-02-07 10:12:07 +0000 | [diff] [blame] | 7970 | ssize_t ret; |
Josef Bacik | 4b46fce | 2010-05-23 11:00:55 -0400 | [diff] [blame] | 7971 | |
Al Viro | 28060d5 | 2014-03-22 05:15:17 -0400 | [diff] [blame] | 7972 | if (check_direct_IO(BTRFS_I(inode)->root, rw, iocb, iter, offset)) |
Chris Mason | 5a5f79b | 2010-05-26 21:33:37 -0400 | [diff] [blame] | 7973 | return 0; |
Chris Mason | 5a5f79b | 2010-05-26 21:33:37 -0400 | [diff] [blame] | 7974 | |
Miao Xie | 38851cc | 2013-02-08 07:04:11 +0000 | [diff] [blame] | 7975 | atomic_inc(&inode->i_dio_count); |
Peter Zijlstra | 4e857c5 | 2014-03-17 18:06:10 +0100 | [diff] [blame] | 7976 | smp_mb__after_atomic(); |
Miao Xie | 38851cc | 2013-02-08 07:04:11 +0000 | [diff] [blame] | 7977 | |
Josef Bacik | 0e267c4 | 2013-07-02 10:38:02 -0400 | [diff] [blame] | 7978 | /* |
Miao Xie | 41bd9ca | 2014-03-06 13:54:57 +0800 | [diff] [blame] | 7979 | * The generic stuff only does filemap_write_and_wait_range, which |
| 7980 | * isn't enough if we've written compressed pages to this area, so |
| 7981 | * we need to flush the dirty pages again to make absolutely sure |
| 7982 | * that any outstanding dirty pages are on disk. |
Josef Bacik | 0e267c4 | 2013-07-02 10:38:02 -0400 | [diff] [blame] | 7983 | */ |
Al Viro | a6cbcd4 | 2014-03-04 22:38:00 -0500 | [diff] [blame] | 7984 | count = iov_iter_count(iter); |
Miao Xie | 41bd9ca | 2014-03-06 13:54:57 +0800 | [diff] [blame] | 7985 | if (test_bit(BTRFS_INODE_HAS_ASYNC_EXTENT, |
| 7986 | &BTRFS_I(inode)->runtime_flags)) |
Wang Shilong | 9a025a0 | 2014-07-17 11:44:13 +0800 | [diff] [blame] | 7987 | filemap_fdatawrite_range(inode->i_mapping, offset, |
| 7988 | offset + count - 1); |
Josef Bacik | 0e267c4 | 2013-07-02 10:38:02 -0400 | [diff] [blame] | 7989 | |
Miao Xie | 0934856 | 2013-02-07 10:12:07 +0000 | [diff] [blame] | 7990 | if (rw & WRITE) { |
Miao Xie | 38851cc | 2013-02-08 07:04:11 +0000 | [diff] [blame] | 7991 | /* |
| 7992 | * If the write DIO is beyond the EOF, we need update |
| 7993 | * the isize, but it is protected by i_mutex. So we can |
| 7994 | * not unlock the i_mutex at this case. |
| 7995 | */ |
| 7996 | if (offset + count <= inode->i_size) { |
| 7997 | mutex_unlock(&inode->i_mutex); |
| 7998 | relock = true; |
| 7999 | } |
Miao Xie | 0934856 | 2013-02-07 10:12:07 +0000 | [diff] [blame] | 8000 | ret = btrfs_delalloc_reserve_space(inode, count); |
| 8001 | if (ret) |
Miao Xie | 38851cc | 2013-02-08 07:04:11 +0000 | [diff] [blame] | 8002 | goto out; |
David Sterba | ee39b43 | 2014-09-30 01:33:33 +0200 | [diff] [blame] | 8003 | } else if (test_bit(BTRFS_INODE_READDIO_NEED_LOCK, |
| 8004 | &BTRFS_I(inode)->runtime_flags)) { |
Miao Xie | 38851cc | 2013-02-08 07:04:11 +0000 | [diff] [blame] | 8005 | inode_dio_done(inode); |
| 8006 | flags = DIO_LOCKING | DIO_SKIP_HOLES; |
| 8007 | wakeup = false; |
Miao Xie | 0934856 | 2013-02-07 10:12:07 +0000 | [diff] [blame] | 8008 | } |
| 8009 | |
| 8010 | ret = __blockdev_direct_IO(rw, iocb, inode, |
| 8011 | BTRFS_I(inode)->root->fs_info->fs_devices->latest_bdev, |
Al Viro | 31b1403 | 2014-03-05 01:33:16 -0500 | [diff] [blame] | 8012 | iter, offset, btrfs_get_blocks_direct, NULL, |
Miao Xie | 2e60a51 | 2013-02-08 07:01:08 +0000 | [diff] [blame] | 8013 | btrfs_submit_direct, flags); |
Miao Xie | 0934856 | 2013-02-07 10:12:07 +0000 | [diff] [blame] | 8014 | if (rw & WRITE) { |
| 8015 | if (ret < 0 && ret != -EIOCBQUEUED) |
| 8016 | btrfs_delalloc_release_space(inode, count); |
Miao Xie | 172a504 | 2013-02-21 02:48:22 -0700 | [diff] [blame] | 8017 | else if (ret >= 0 && (size_t)ret < count) |
Miao Xie | 0934856 | 2013-02-07 10:12:07 +0000 | [diff] [blame] | 8018 | btrfs_delalloc_release_space(inode, |
| 8019 | count - (size_t)ret); |
Miao Xie | 172a504 | 2013-02-21 02:48:22 -0700 | [diff] [blame] | 8020 | else |
| 8021 | btrfs_delalloc_release_metadata(inode, 0); |
Miao Xie | 0934856 | 2013-02-07 10:12:07 +0000 | [diff] [blame] | 8022 | } |
Miao Xie | 38851cc | 2013-02-08 07:04:11 +0000 | [diff] [blame] | 8023 | out: |
Miao Xie | 2e60a51 | 2013-02-08 07:01:08 +0000 | [diff] [blame] | 8024 | if (wakeup) |
| 8025 | inode_dio_done(inode); |
Miao Xie | 38851cc | 2013-02-08 07:04:11 +0000 | [diff] [blame] | 8026 | if (relock) |
| 8027 | mutex_lock(&inode->i_mutex); |
Miao Xie | 0934856 | 2013-02-07 10:12:07 +0000 | [diff] [blame] | 8028 | |
| 8029 | return ret; |
Chris Mason | 1643298 | 2008-04-10 10:23:21 -0400 | [diff] [blame] | 8030 | } |
| 8031 | |
Tsutomu Itoh | 05dadc0 | 2012-11-29 05:08:26 +0000 | [diff] [blame] | 8032 | #define BTRFS_FIEMAP_FLAGS (FIEMAP_FLAG_SYNC) |
| 8033 | |
Yehuda Sadeh | 1506fcc | 2009-01-21 14:39:14 -0500 | [diff] [blame] | 8034 | static int btrfs_fiemap(struct inode *inode, struct fiemap_extent_info *fieinfo, |
| 8035 | __u64 start, __u64 len) |
| 8036 | { |
Tsutomu Itoh | 05dadc0 | 2012-11-29 05:08:26 +0000 | [diff] [blame] | 8037 | int ret; |
| 8038 | |
| 8039 | ret = fiemap_check_flags(fieinfo, BTRFS_FIEMAP_FLAGS); |
| 8040 | if (ret) |
| 8041 | return ret; |
| 8042 | |
Chris Mason | ec29ed5 | 2011-02-23 16:23:20 -0500 | [diff] [blame] | 8043 | return extent_fiemap(inode, fieinfo, start, len, btrfs_get_extent_fiemap); |
Yehuda Sadeh | 1506fcc | 2009-01-21 14:39:14 -0500 | [diff] [blame] | 8044 | } |
| 8045 | |
Chris Mason | 9ebefb18 | 2007-06-15 13:50:00 -0400 | [diff] [blame] | 8046 | int btrfs_readpage(struct file *file, struct page *page) |
| 8047 | { |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 8048 | struct extent_io_tree *tree; |
| 8049 | tree = &BTRFS_I(page->mapping->host)->io_tree; |
Jan Schmidt | 8ddc7d9 | 2011-06-13 20:02:58 +0200 | [diff] [blame] | 8050 | return extent_read_full_page(tree, page, btrfs_get_extent, 0); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 8051 | } |
Chris Mason | 1832a6d | 2007-12-21 16:27:21 -0500 | [diff] [blame] | 8052 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 8053 | static int btrfs_writepage(struct page *page, struct writeback_control *wbc) |
| 8054 | { |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 8055 | struct extent_io_tree *tree; |
Chris Mason | b888db2b | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 8056 | |
| 8057 | |
| 8058 | if (current->flags & PF_MEMALLOC) { |
| 8059 | redirty_page_for_writepage(wbc, page); |
| 8060 | unlock_page(page); |
| 8061 | return 0; |
| 8062 | } |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 8063 | tree = &BTRFS_I(page->mapping->host)->io_tree; |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 8064 | return extent_write_full_page(tree, page, btrfs_get_extent, wbc); |
| 8065 | } |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 8066 | |
Eric Sandeen | 48a3b63 | 2013-04-25 20:41:01 +0000 | [diff] [blame] | 8067 | static int btrfs_writepages(struct address_space *mapping, |
| 8068 | struct writeback_control *wbc) |
Chris Mason | b293f02e | 2007-11-01 19:45:34 -0400 | [diff] [blame] | 8069 | { |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 8070 | struct extent_io_tree *tree; |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 8071 | |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 8072 | tree = &BTRFS_I(mapping->host)->io_tree; |
Chris Mason | b293f02e | 2007-11-01 19:45:34 -0400 | [diff] [blame] | 8073 | return extent_writepages(tree, mapping, btrfs_get_extent, wbc); |
| 8074 | } |
| 8075 | |
Chris Mason | 3ab2fb5 | 2007-11-08 10:59:22 -0500 | [diff] [blame] | 8076 | static int |
| 8077 | btrfs_readpages(struct file *file, struct address_space *mapping, |
| 8078 | struct list_head *pages, unsigned nr_pages) |
| 8079 | { |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 8080 | struct extent_io_tree *tree; |
| 8081 | tree = &BTRFS_I(mapping->host)->io_tree; |
Chris Mason | 3ab2fb5 | 2007-11-08 10:59:22 -0500 | [diff] [blame] | 8082 | return extent_readpages(tree, mapping, pages, nr_pages, |
| 8083 | btrfs_get_extent); |
| 8084 | } |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 8085 | static int __btrfs_releasepage(struct page *page, gfp_t gfp_flags) |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 8086 | { |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 8087 | struct extent_io_tree *tree; |
| 8088 | struct extent_map_tree *map; |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 8089 | int ret; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 8090 | |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 8091 | tree = &BTRFS_I(page->mapping->host)->io_tree; |
| 8092 | map = &BTRFS_I(page->mapping->host)->extent_tree; |
Chris Mason | 70dec80 | 2008-01-29 09:59:12 -0500 | [diff] [blame] | 8093 | ret = try_release_extent_mapping(map, tree, page, gfp_flags); |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 8094 | if (ret == 1) { |
| 8095 | ClearPagePrivate(page); |
| 8096 | set_page_private(page, 0); |
| 8097 | page_cache_release(page); |
| 8098 | } |
| 8099 | return ret; |
| 8100 | } |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 8101 | |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 8102 | static int btrfs_releasepage(struct page *page, gfp_t gfp_flags) |
| 8103 | { |
Chris Mason | 98509cf | 2008-09-11 15:51:43 -0400 | [diff] [blame] | 8104 | if (PageWriteback(page) || PageDirty(page)) |
| 8105 | return 0; |
Yan Zheng | b335b00 | 2009-02-12 10:06:04 -0500 | [diff] [blame] | 8106 | return __btrfs_releasepage(page, gfp_flags & GFP_NOFS); |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 8107 | } |
| 8108 | |
Lukas Czerner | d47992f | 2013-05-21 23:17:23 -0400 | [diff] [blame] | 8109 | static void btrfs_invalidatepage(struct page *page, unsigned int offset, |
| 8110 | unsigned int length) |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 8111 | { |
Josef Bacik | 5fd0204 | 2012-05-02 14:00:54 -0400 | [diff] [blame] | 8112 | struct inode *inode = page->mapping->host; |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 8113 | struct extent_io_tree *tree; |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 8114 | struct btrfs_ordered_extent *ordered; |
Josef Bacik | 2ac55d4 | 2010-02-03 19:33:23 +0000 | [diff] [blame] | 8115 | struct extent_state *cached_state = NULL; |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 8116 | u64 page_start = page_offset(page); |
| 8117 | u64 page_end = page_start + PAGE_CACHE_SIZE - 1; |
Filipe David Borba Manana | 131e404 | 2013-11-19 22:29:35 +0000 | [diff] [blame] | 8118 | int inode_evicting = inode->i_state & I_FREEING; |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 8119 | |
Chris Mason | 8b62b72 | 2009-09-02 16:53:46 -0400 | [diff] [blame] | 8120 | /* |
| 8121 | * we have the page locked, so new writeback can't start, |
| 8122 | * and the dirty bit won't be cleared while we are here. |
| 8123 | * |
| 8124 | * Wait for IO on this page so that we can safely clear |
| 8125 | * the PagePrivate2 bit and do ordered accounting |
| 8126 | */ |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 8127 | wait_on_page_writeback(page); |
Chris Mason | 8b62b72 | 2009-09-02 16:53:46 -0400 | [diff] [blame] | 8128 | |
Josef Bacik | 5fd0204 | 2012-05-02 14:00:54 -0400 | [diff] [blame] | 8129 | tree = &BTRFS_I(inode)->io_tree; |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 8130 | if (offset) { |
| 8131 | btrfs_releasepage(page, GFP_NOFS); |
| 8132 | return; |
| 8133 | } |
Filipe David Borba Manana | 131e404 | 2013-11-19 22:29:35 +0000 | [diff] [blame] | 8134 | |
| 8135 | if (!inode_evicting) |
| 8136 | lock_extent_bits(tree, page_start, page_end, 0, &cached_state); |
| 8137 | ordered = btrfs_lookup_ordered_extent(inode, page_start); |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 8138 | if (ordered) { |
Chris Mason | eb84ae0 | 2008-07-17 13:53:27 -0400 | [diff] [blame] | 8139 | /* |
| 8140 | * IO on this page will never be started, so we need |
| 8141 | * to account for any ordered extents now |
| 8142 | */ |
Filipe David Borba Manana | 131e404 | 2013-11-19 22:29:35 +0000 | [diff] [blame] | 8143 | if (!inode_evicting) |
| 8144 | clear_extent_bit(tree, page_start, page_end, |
| 8145 | EXTENT_DIRTY | EXTENT_DELALLOC | |
| 8146 | EXTENT_LOCKED | EXTENT_DO_ACCOUNTING | |
| 8147 | EXTENT_DEFRAG, 1, 0, &cached_state, |
| 8148 | GFP_NOFS); |
Chris Mason | 8b62b72 | 2009-09-02 16:53:46 -0400 | [diff] [blame] | 8149 | /* |
| 8150 | * whoever cleared the private bit is responsible |
| 8151 | * for the finish_ordered_io |
| 8152 | */ |
Josef Bacik | 77cef2e | 2013-08-29 13:57:21 -0400 | [diff] [blame] | 8153 | if (TestClearPagePrivate2(page)) { |
| 8154 | struct btrfs_ordered_inode_tree *tree; |
| 8155 | u64 new_len; |
| 8156 | |
| 8157 | tree = &BTRFS_I(inode)->ordered_tree; |
| 8158 | |
| 8159 | spin_lock_irq(&tree->lock); |
| 8160 | set_bit(BTRFS_ORDERED_TRUNCATED, &ordered->flags); |
| 8161 | new_len = page_start - ordered->file_offset; |
| 8162 | if (new_len < ordered->truncated_len) |
| 8163 | ordered->truncated_len = new_len; |
| 8164 | spin_unlock_irq(&tree->lock); |
| 8165 | |
| 8166 | if (btrfs_dec_test_ordered_pending(inode, &ordered, |
| 8167 | page_start, |
| 8168 | PAGE_CACHE_SIZE, 1)) |
| 8169 | btrfs_finish_ordered_io(ordered); |
Chris Mason | 8b62b72 | 2009-09-02 16:53:46 -0400 | [diff] [blame] | 8170 | } |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 8171 | btrfs_put_ordered_extent(ordered); |
Filipe David Borba Manana | 131e404 | 2013-11-19 22:29:35 +0000 | [diff] [blame] | 8172 | if (!inode_evicting) { |
| 8173 | cached_state = NULL; |
| 8174 | lock_extent_bits(tree, page_start, page_end, 0, |
| 8175 | &cached_state); |
| 8176 | } |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 8177 | } |
Filipe David Borba Manana | 131e404 | 2013-11-19 22:29:35 +0000 | [diff] [blame] | 8178 | |
| 8179 | if (!inode_evicting) { |
| 8180 | clear_extent_bit(tree, page_start, page_end, |
| 8181 | EXTENT_LOCKED | EXTENT_DIRTY | |
| 8182 | EXTENT_DELALLOC | EXTENT_DO_ACCOUNTING | |
| 8183 | EXTENT_DEFRAG, 1, 1, |
| 8184 | &cached_state, GFP_NOFS); |
| 8185 | |
| 8186 | __btrfs_releasepage(page, GFP_NOFS); |
| 8187 | } |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 8188 | |
Chris Mason | 4a09675 | 2008-07-21 10:29:44 -0400 | [diff] [blame] | 8189 | ClearPageChecked(page); |
Chris Mason | 9ad6b7b | 2008-04-18 16:11:30 -0400 | [diff] [blame] | 8190 | if (PagePrivate(page)) { |
Chris Mason | 9ad6b7b | 2008-04-18 16:11:30 -0400 | [diff] [blame] | 8191 | ClearPagePrivate(page); |
| 8192 | set_page_private(page, 0); |
| 8193 | page_cache_release(page); |
| 8194 | } |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 8195 | } |
| 8196 | |
Chris Mason | 9ebefb18 | 2007-06-15 13:50:00 -0400 | [diff] [blame] | 8197 | /* |
| 8198 | * btrfs_page_mkwrite() is not allowed to change the file size as it gets |
| 8199 | * called from a page fault handler when a page is first dirtied. Hence we must |
| 8200 | * be careful to check for EOF conditions here. We set the page up correctly |
| 8201 | * for a written page which means we get ENOSPC checking when writing into |
| 8202 | * holes and correct delalloc and unwritten extent mapping on filesystems that |
| 8203 | * support these features. |
| 8204 | * |
| 8205 | * We are not allowed to take the i_mutex here so we have to play games to |
| 8206 | * protect against truncate races as the page could now be beyond EOF. Because |
| 8207 | * vmtruncate() writes the inode size before removing pages, once we have the |
| 8208 | * page lock we can determine safely if the page is beyond EOF. If it is not |
| 8209 | * beyond EOF, then the page is guaranteed safe against truncation until we |
| 8210 | * unlock the page. |
| 8211 | */ |
Nick Piggin | c2ec175 | 2009-03-31 15:23:21 -0700 | [diff] [blame] | 8212 | 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] | 8213 | { |
Nick Piggin | c2ec175 | 2009-03-31 15:23:21 -0700 | [diff] [blame] | 8214 | struct page *page = vmf->page; |
Al Viro | 496ad9a | 2013-01-23 17:07:38 -0500 | [diff] [blame] | 8215 | struct inode *inode = file_inode(vma->vm_file); |
Chris Mason | 1832a6d | 2007-12-21 16:27:21 -0500 | [diff] [blame] | 8216 | struct btrfs_root *root = BTRFS_I(inode)->root; |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 8217 | struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree; |
| 8218 | struct btrfs_ordered_extent *ordered; |
Josef Bacik | 2ac55d4 | 2010-02-03 19:33:23 +0000 | [diff] [blame] | 8219 | struct extent_state *cached_state = NULL; |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 8220 | char *kaddr; |
| 8221 | unsigned long zero_start; |
Chris Mason | 9ebefb18 | 2007-06-15 13:50:00 -0400 | [diff] [blame] | 8222 | loff_t size; |
Chris Mason | 1832a6d | 2007-12-21 16:27:21 -0500 | [diff] [blame] | 8223 | int ret; |
Chris Mason | 9998eb7 | 2012-01-25 13:47:40 -0500 | [diff] [blame] | 8224 | int reserved = 0; |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 8225 | u64 page_start; |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 8226 | u64 page_end; |
Chris Mason | 9ebefb18 | 2007-06-15 13:50:00 -0400 | [diff] [blame] | 8227 | |
Jan Kara | b2b5ef5 | 2012-06-12 16:20:45 +0200 | [diff] [blame] | 8228 | sb_start_pagefault(inode->i_sb); |
Yan, Zheng | 0ca1f7c | 2010-05-16 10:48:47 -0400 | [diff] [blame] | 8229 | ret = btrfs_delalloc_reserve_space(inode, PAGE_CACHE_SIZE); |
Chris Mason | 9998eb7 | 2012-01-25 13:47:40 -0500 | [diff] [blame] | 8230 | if (!ret) { |
Josef Bacik | e41f941 | 2012-03-26 09:46:47 -0400 | [diff] [blame] | 8231 | ret = file_update_time(vma->vm_file); |
Chris Mason | 9998eb7 | 2012-01-25 13:47:40 -0500 | [diff] [blame] | 8232 | reserved = 1; |
| 8233 | } |
Nick Piggin | 56a76f8 | 2009-03-31 15:23:23 -0700 | [diff] [blame] | 8234 | if (ret) { |
| 8235 | if (ret == -ENOMEM) |
| 8236 | ret = VM_FAULT_OOM; |
| 8237 | else /* -ENOSPC, -EIO, etc */ |
| 8238 | ret = VM_FAULT_SIGBUS; |
Chris Mason | 9998eb7 | 2012-01-25 13:47:40 -0500 | [diff] [blame] | 8239 | if (reserved) |
| 8240 | goto out; |
| 8241 | goto out_noreserve; |
Nick Piggin | 56a76f8 | 2009-03-31 15:23:23 -0700 | [diff] [blame] | 8242 | } |
Chris Mason | 1832a6d | 2007-12-21 16:27:21 -0500 | [diff] [blame] | 8243 | |
Nick Piggin | 56a76f8 | 2009-03-31 15:23:23 -0700 | [diff] [blame] | 8244 | ret = VM_FAULT_NOPAGE; /* make the VM retry the fault */ |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 8245 | again: |
Chris Mason | 9ebefb18 | 2007-06-15 13:50:00 -0400 | [diff] [blame] | 8246 | lock_page(page); |
Chris Mason | 9ebefb18 | 2007-06-15 13:50:00 -0400 | [diff] [blame] | 8247 | size = i_size_read(inode); |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 8248 | page_start = page_offset(page); |
| 8249 | page_end = page_start + PAGE_CACHE_SIZE - 1; |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 8250 | |
Chris Mason | 9ebefb18 | 2007-06-15 13:50:00 -0400 | [diff] [blame] | 8251 | if ((page->mapping != inode->i_mapping) || |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 8252 | (page_start >= size)) { |
Chris Mason | 9ebefb18 | 2007-06-15 13:50:00 -0400 | [diff] [blame] | 8253 | /* page got truncated out from underneath us */ |
| 8254 | goto out_unlock; |
| 8255 | } |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 8256 | wait_on_page_writeback(page); |
| 8257 | |
Jeff Mahoney | d008237 | 2012-03-01 14:57:19 +0100 | [diff] [blame] | 8258 | lock_extent_bits(io_tree, page_start, page_end, 0, &cached_state); |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 8259 | set_page_extent_mapped(page); |
| 8260 | |
Chris Mason | eb84ae0 | 2008-07-17 13:53:27 -0400 | [diff] [blame] | 8261 | /* |
| 8262 | * we can't set the delalloc bits if there are pending ordered |
| 8263 | * extents. Drop our locks and wait for them to finish |
| 8264 | */ |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 8265 | ordered = btrfs_lookup_ordered_extent(inode, page_start); |
| 8266 | if (ordered) { |
Josef Bacik | 2ac55d4 | 2010-02-03 19:33:23 +0000 | [diff] [blame] | 8267 | unlock_extent_cached(io_tree, page_start, page_end, |
| 8268 | &cached_state, GFP_NOFS); |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 8269 | unlock_page(page); |
Chris Mason | eb84ae0 | 2008-07-17 13:53:27 -0400 | [diff] [blame] | 8270 | btrfs_start_ordered_extent(inode, ordered, 1); |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 8271 | btrfs_put_ordered_extent(ordered); |
| 8272 | goto again; |
| 8273 | } |
| 8274 | |
Josef Bacik | fbf1908 | 2009-10-01 17:10:23 -0400 | [diff] [blame] | 8275 | /* |
| 8276 | * XXX - page_mkwrite gets called every time the page is dirtied, even |
| 8277 | * if it was already dirty, so for space accounting reasons we need to |
| 8278 | * clear any delalloc bits for the range we are fixing to save. There |
| 8279 | * is probably a better way to do this, but for now keep consistent with |
| 8280 | * prepare_pages in the normal write path. |
| 8281 | */ |
Josef Bacik | 2ac55d4 | 2010-02-03 19:33:23 +0000 | [diff] [blame] | 8282 | clear_extent_bit(&BTRFS_I(inode)->io_tree, page_start, page_end, |
Liu Bo | 9e8a4a8 | 2012-09-05 19:10:51 -0600 | [diff] [blame] | 8283 | EXTENT_DIRTY | EXTENT_DELALLOC | |
| 8284 | EXTENT_DO_ACCOUNTING | EXTENT_DEFRAG, |
Josef Bacik | 2ac55d4 | 2010-02-03 19:33:23 +0000 | [diff] [blame] | 8285 | 0, 0, &cached_state, GFP_NOFS); |
Josef Bacik | fbf1908 | 2009-10-01 17:10:23 -0400 | [diff] [blame] | 8286 | |
Josef Bacik | 2ac55d4 | 2010-02-03 19:33:23 +0000 | [diff] [blame] | 8287 | ret = btrfs_set_extent_delalloc(inode, page_start, page_end, |
| 8288 | &cached_state); |
Josef Bacik | 9ed74f2 | 2009-09-11 16:12:44 -0400 | [diff] [blame] | 8289 | if (ret) { |
Josef Bacik | 2ac55d4 | 2010-02-03 19:33:23 +0000 | [diff] [blame] | 8290 | unlock_extent_cached(io_tree, page_start, page_end, |
| 8291 | &cached_state, GFP_NOFS); |
Josef Bacik | 9ed74f2 | 2009-09-11 16:12:44 -0400 | [diff] [blame] | 8292 | ret = VM_FAULT_SIGBUS; |
| 8293 | goto out_unlock; |
| 8294 | } |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 8295 | ret = 0; |
Chris Mason | 9ebefb18 | 2007-06-15 13:50:00 -0400 | [diff] [blame] | 8296 | |
| 8297 | /* page is wholly or partially inside EOF */ |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 8298 | if (page_start + PAGE_CACHE_SIZE > size) |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 8299 | zero_start = size & ~PAGE_CACHE_MASK; |
Chris Mason | 9ebefb18 | 2007-06-15 13:50:00 -0400 | [diff] [blame] | 8300 | else |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 8301 | zero_start = PAGE_CACHE_SIZE; |
Chris Mason | 9ebefb18 | 2007-06-15 13:50:00 -0400 | [diff] [blame] | 8302 | |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 8303 | if (zero_start != PAGE_CACHE_SIZE) { |
| 8304 | kaddr = kmap(page); |
| 8305 | memset(kaddr + zero_start, 0, PAGE_CACHE_SIZE - zero_start); |
| 8306 | flush_dcache_page(page); |
| 8307 | kunmap(page); |
| 8308 | } |
Chris Mason | 247e743 | 2008-07-17 12:53:51 -0400 | [diff] [blame] | 8309 | ClearPageChecked(page); |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 8310 | set_page_dirty(page); |
Chris Mason | 50a9b21 | 2009-09-11 12:33:12 -0400 | [diff] [blame] | 8311 | SetPageUptodate(page); |
Chris Mason | 5a3f23d | 2009-03-31 13:27:11 -0400 | [diff] [blame] | 8312 | |
Chris Mason | 257c62e | 2009-10-13 13:21:08 -0400 | [diff] [blame] | 8313 | BTRFS_I(inode)->last_trans = root->fs_info->generation; |
| 8314 | BTRFS_I(inode)->last_sub_trans = BTRFS_I(inode)->root->log_transid; |
Liu Bo | 46d8bc3 | 2012-08-29 01:07:55 -0600 | [diff] [blame] | 8315 | 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] | 8316 | |
Josef Bacik | 2ac55d4 | 2010-02-03 19:33:23 +0000 | [diff] [blame] | 8317 | 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] | 8318 | |
| 8319 | out_unlock: |
Jan Kara | b2b5ef5 | 2012-06-12 16:20:45 +0200 | [diff] [blame] | 8320 | if (!ret) { |
| 8321 | sb_end_pagefault(inode->i_sb); |
Chris Mason | 50a9b21 | 2009-09-11 12:33:12 -0400 | [diff] [blame] | 8322 | return VM_FAULT_LOCKED; |
Jan Kara | b2b5ef5 | 2012-06-12 16:20:45 +0200 | [diff] [blame] | 8323 | } |
Chris Mason | 9ebefb18 | 2007-06-15 13:50:00 -0400 | [diff] [blame] | 8324 | unlock_page(page); |
Chris Mason | 1832a6d | 2007-12-21 16:27:21 -0500 | [diff] [blame] | 8325 | out: |
Josef Bacik | ec39e18 | 2012-01-12 19:10:12 -0500 | [diff] [blame] | 8326 | btrfs_delalloc_release_space(inode, PAGE_CACHE_SIZE); |
Chris Mason | 9998eb7 | 2012-01-25 13:47:40 -0500 | [diff] [blame] | 8327 | out_noreserve: |
Jan Kara | b2b5ef5 | 2012-06-12 16:20:45 +0200 | [diff] [blame] | 8328 | sb_end_pagefault(inode->i_sb); |
Chris Mason | 9ebefb18 | 2007-06-15 13:50:00 -0400 | [diff] [blame] | 8329 | return ret; |
| 8330 | } |
| 8331 | |
Josef Bacik | a41ad39 | 2011-01-31 15:30:16 -0500 | [diff] [blame] | 8332 | static int btrfs_truncate(struct inode *inode) |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 8333 | { |
| 8334 | struct btrfs_root *root = BTRFS_I(inode)->root; |
Josef Bacik | fcb80c2 | 2011-05-03 10:40:22 -0400 | [diff] [blame] | 8335 | struct btrfs_block_rsv *rsv; |
Josef Bacik | a71754f | 2013-06-17 17:14:39 -0400 | [diff] [blame] | 8336 | int ret = 0; |
Josef Bacik | 3893e33 | 2011-01-31 16:03:11 -0500 | [diff] [blame] | 8337 | int err = 0; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 8338 | struct btrfs_trans_handle *trans; |
Chris Mason | dbe674a | 2008-07-17 12:54:05 -0400 | [diff] [blame] | 8339 | u64 mask = root->sectorsize - 1; |
Josef Bacik | 0712718 | 2011-08-19 10:29:59 -0400 | [diff] [blame] | 8340 | u64 min_size = btrfs_calc_trunc_metadata_size(root, 1); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 8341 | |
Josef Bacik | 0ef8b72 | 2013-10-25 16:13:35 -0400 | [diff] [blame] | 8342 | ret = btrfs_wait_ordered_range(inode, inode->i_size & (~mask), |
| 8343 | (u64)-1); |
| 8344 | if (ret) |
| 8345 | return ret; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 8346 | |
Josef Bacik | fcb80c2 | 2011-05-03 10:40:22 -0400 | [diff] [blame] | 8347 | /* |
| 8348 | * Yes ladies and gentelment, this is indeed ugly. The fact is we have |
| 8349 | * 3 things going on here |
| 8350 | * |
| 8351 | * 1) We need to reserve space for our orphan item and the space to |
| 8352 | * delete our orphan item. Lord knows we don't want to have a dangling |
| 8353 | * orphan item because we didn't reserve space to remove it. |
| 8354 | * |
| 8355 | * 2) We need to reserve space to update our inode. |
| 8356 | * |
| 8357 | * 3) We need to have something to cache all the space that is going to |
| 8358 | * be free'd up by the truncate operation, but also have some slack |
| 8359 | * space reserved in case it uses space during the truncate (thank you |
| 8360 | * very much snapshotting). |
| 8361 | * |
| 8362 | * And we need these to all be seperate. The fact is we can use alot of |
| 8363 | * space doing the truncate, and we have no earthly idea how much space |
| 8364 | * we will use, so we need the truncate reservation to be seperate so it |
| 8365 | * doesn't end up using space reserved for updating the inode or |
| 8366 | * removing the orphan item. We also need to be able to stop the |
| 8367 | * transaction and start a new one, which means we need to be able to |
| 8368 | * update the inode several times, and we have no idea of knowing how |
| 8369 | * many times that will be, so we can't just reserve 1 item for the |
| 8370 | * entirety of the opration, so that has to be done seperately as well. |
| 8371 | * Then there is the orphan item, which does indeed need to be held on |
| 8372 | * to for the whole operation, and we need nobody to touch this reserved |
| 8373 | * space except the orphan code. |
| 8374 | * |
| 8375 | * So that leaves us with |
| 8376 | * |
| 8377 | * 1) root->orphan_block_rsv - for the orphan deletion. |
| 8378 | * 2) rsv - for the truncate reservation, which we will steal from the |
| 8379 | * transaction reservation. |
| 8380 | * 3) fs_info->trans_block_rsv - this will have 1 items worth left for |
| 8381 | * updating the inode. |
| 8382 | */ |
Miao Xie | 66d8f3d | 2012-09-06 04:02:28 -0600 | [diff] [blame] | 8383 | rsv = btrfs_alloc_block_rsv(root, BTRFS_BLOCK_RSV_TEMP); |
Josef Bacik | fcb80c2 | 2011-05-03 10:40:22 -0400 | [diff] [blame] | 8384 | if (!rsv) |
| 8385 | return -ENOMEM; |
Josef Bacik | 4a33854 | 2011-08-29 11:01:31 -0400 | [diff] [blame] | 8386 | rsv->size = min_size; |
Josef Bacik | ca7e70f | 2012-08-27 17:48:15 -0400 | [diff] [blame] | 8387 | rsv->failfast = 1; |
Josef Bacik | f0cd846 | 2011-03-04 14:37:08 -0500 | [diff] [blame] | 8388 | |
Josef Bacik | 907cbce | 2011-08-08 13:46:15 -0400 | [diff] [blame] | 8389 | /* |
Josef Bacik | 0712718 | 2011-08-19 10:29:59 -0400 | [diff] [blame] | 8390 | * 1 for the truncate slack space |
Josef Bacik | 907cbce | 2011-08-08 13:46:15 -0400 | [diff] [blame] | 8391 | * 1 for updating the inode. |
| 8392 | */ |
Josef Bacik | f3fe820 | 2013-01-07 17:03:21 -0500 | [diff] [blame] | 8393 | trans = btrfs_start_transaction(root, 2); |
Josef Bacik | fcb80c2 | 2011-05-03 10:40:22 -0400 | [diff] [blame] | 8394 | if (IS_ERR(trans)) { |
| 8395 | err = PTR_ERR(trans); |
| 8396 | goto out; |
| 8397 | } |
Josef Bacik | f0cd846 | 2011-03-04 14:37:08 -0500 | [diff] [blame] | 8398 | |
Josef Bacik | 907cbce | 2011-08-08 13:46:15 -0400 | [diff] [blame] | 8399 | /* Migrate the slack space for the truncate to our reserve */ |
| 8400 | ret = btrfs_block_rsv_migrate(&root->fs_info->trans_block_rsv, rsv, |
| 8401 | min_size); |
Josef Bacik | fcb80c2 | 2011-05-03 10:40:22 -0400 | [diff] [blame] | 8402 | BUG_ON(ret); |
Josef Bacik | f0cd846 | 2011-03-04 14:37:08 -0500 | [diff] [blame] | 8403 | |
Chris Mason | 5a3f23d | 2009-03-31 13:27:11 -0400 | [diff] [blame] | 8404 | /* |
Josef Bacik | 5dc562c | 2012-08-17 13:14:17 -0400 | [diff] [blame] | 8405 | * So if we truncate and then write and fsync we normally would just |
| 8406 | * write the extents that changed, which is a problem if we need to |
| 8407 | * first truncate that entire inode. So set this flag so we write out |
| 8408 | * all of the extents in the inode to the sync log so we're completely |
| 8409 | * safe. |
| 8410 | */ |
| 8411 | set_bit(BTRFS_INODE_NEEDS_FULL_SYNC, &BTRFS_I(inode)->runtime_flags); |
Josef Bacik | ca7e70f | 2012-08-27 17:48:15 -0400 | [diff] [blame] | 8412 | trans->block_rsv = rsv; |
Josef Bacik | 5dc562c | 2012-08-17 13:14:17 -0400 | [diff] [blame] | 8413 | |
Yan, Zheng | 8082510 | 2009-11-12 09:35:36 +0000 | [diff] [blame] | 8414 | while (1) { |
| 8415 | ret = btrfs_truncate_inode_items(trans, root, inode, |
| 8416 | inode->i_size, |
| 8417 | BTRFS_EXTENT_DATA_KEY); |
Josef Bacik | ca7e70f | 2012-08-27 17:48:15 -0400 | [diff] [blame] | 8418 | if (ret != -ENOSPC) { |
Josef Bacik | 3893e33 | 2011-01-31 16:03:11 -0500 | [diff] [blame] | 8419 | err = ret; |
Yan, Zheng | 8082510 | 2009-11-12 09:35:36 +0000 | [diff] [blame] | 8420 | break; |
Josef Bacik | 3893e33 | 2011-01-31 16:03:11 -0500 | [diff] [blame] | 8421 | } |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 8422 | |
Josef Bacik | fcb80c2 | 2011-05-03 10:40:22 -0400 | [diff] [blame] | 8423 | trans->block_rsv = &root->fs_info->trans_block_rsv; |
Yan, Zheng | 8082510 | 2009-11-12 09:35:36 +0000 | [diff] [blame] | 8424 | ret = btrfs_update_inode(trans, root, inode); |
Josef Bacik | 3893e33 | 2011-01-31 16:03:11 -0500 | [diff] [blame] | 8425 | if (ret) { |
| 8426 | err = ret; |
| 8427 | break; |
| 8428 | } |
Josef Bacik | ca7e70f | 2012-08-27 17:48:15 -0400 | [diff] [blame] | 8429 | |
Yan, Zheng | 8082510 | 2009-11-12 09:35:36 +0000 | [diff] [blame] | 8430 | btrfs_end_transaction(trans, root); |
Liu Bo | b53d3f5 | 2012-11-14 14:34:34 +0000 | [diff] [blame] | 8431 | btrfs_btree_balance_dirty(root); |
Josef Bacik | ca7e70f | 2012-08-27 17:48:15 -0400 | [diff] [blame] | 8432 | |
| 8433 | trans = btrfs_start_transaction(root, 2); |
| 8434 | if (IS_ERR(trans)) { |
| 8435 | ret = err = PTR_ERR(trans); |
| 8436 | trans = NULL; |
| 8437 | break; |
| 8438 | } |
| 8439 | |
| 8440 | ret = btrfs_block_rsv_migrate(&root->fs_info->trans_block_rsv, |
| 8441 | rsv, min_size); |
| 8442 | BUG_ON(ret); /* shouldn't happen */ |
| 8443 | trans->block_rsv = rsv; |
Yan, Zheng | 8082510 | 2009-11-12 09:35:36 +0000 | [diff] [blame] | 8444 | } |
| 8445 | |
| 8446 | if (ret == 0 && inode->i_nlink > 0) { |
Josef Bacik | fcb80c2 | 2011-05-03 10:40:22 -0400 | [diff] [blame] | 8447 | trans->block_rsv = root->orphan_block_rsv; |
Yan, Zheng | 8082510 | 2009-11-12 09:35:36 +0000 | [diff] [blame] | 8448 | ret = btrfs_orphan_del(trans, inode); |
Josef Bacik | 3893e33 | 2011-01-31 16:03:11 -0500 | [diff] [blame] | 8449 | if (ret) |
| 8450 | err = ret; |
Yan, Zheng | 8082510 | 2009-11-12 09:35:36 +0000 | [diff] [blame] | 8451 | } |
| 8452 | |
Chris Mason | 917c16b | 2011-11-08 14:49:59 -0500 | [diff] [blame] | 8453 | if (trans) { |
| 8454 | trans->block_rsv = &root->fs_info->trans_block_rsv; |
| 8455 | ret = btrfs_update_inode(trans, root, inode); |
| 8456 | if (ret && !err) |
| 8457 | err = ret; |
Josef Bacik | 7b12876 | 2008-07-24 12:17:14 -0400 | [diff] [blame] | 8458 | |
Josef Bacik | 7ad85bb | 2012-01-12 19:10:12 -0500 | [diff] [blame] | 8459 | ret = btrfs_end_transaction(trans, root); |
Liu Bo | b53d3f5 | 2012-11-14 14:34:34 +0000 | [diff] [blame] | 8460 | btrfs_btree_balance_dirty(root); |
Chris Mason | 917c16b | 2011-11-08 14:49:59 -0500 | [diff] [blame] | 8461 | } |
Josef Bacik | fcb80c2 | 2011-05-03 10:40:22 -0400 | [diff] [blame] | 8462 | |
| 8463 | out: |
| 8464 | btrfs_free_block_rsv(root, rsv); |
| 8465 | |
Josef Bacik | 3893e33 | 2011-01-31 16:03:11 -0500 | [diff] [blame] | 8466 | if (ret && !err) |
| 8467 | err = ret; |
Josef Bacik | a41ad39 | 2011-01-31 15:30:16 -0500 | [diff] [blame] | 8468 | |
Josef Bacik | 3893e33 | 2011-01-31 16:03:11 -0500 | [diff] [blame] | 8469 | return err; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 8470 | } |
| 8471 | |
Sven Wegener | 3b96362 | 2008-06-09 21:57:42 -0400 | [diff] [blame] | 8472 | /* |
Chris Mason | d352ac6 | 2008-09-29 15:18:18 -0400 | [diff] [blame] | 8473 | * create a new subvolume directory/inode (helper for the ioctl). |
| 8474 | */ |
Yan Zheng | d2fb343 | 2008-12-11 16:30:39 -0500 | [diff] [blame] | 8475 | int btrfs_create_subvol_root(struct btrfs_trans_handle *trans, |
Filipe David Borba Manana | 6354192 | 2014-01-07 11:47:46 +0000 | [diff] [blame] | 8476 | struct btrfs_root *new_root, |
| 8477 | struct btrfs_root *parent_root, |
| 8478 | u64 new_dirid) |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 8479 | { |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 8480 | struct inode *inode; |
Yan, Zheng | 76dda93 | 2009-09-21 16:00:26 -0400 | [diff] [blame] | 8481 | int err; |
Chris Mason | 00e4e6b | 2008-08-05 11:18:09 -0400 | [diff] [blame] | 8482 | u64 index = 0; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 8483 | |
Florian Albrechtskirchinger | 12fc9d0 | 2012-02-10 22:15:54 +0100 | [diff] [blame] | 8484 | inode = btrfs_new_inode(trans, new_root, NULL, "..", 2, |
| 8485 | new_dirid, new_dirid, |
| 8486 | S_IFDIR | (~current_umask() & S_IRWXUGO), |
| 8487 | &index); |
Chris Mason | 54aa1f4 | 2007-06-22 14:16:25 -0400 | [diff] [blame] | 8488 | if (IS_ERR(inode)) |
Christoph Hellwig | f46b5a6 | 2008-06-11 21:53:53 -0400 | [diff] [blame] | 8489 | return PTR_ERR(inode); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 8490 | inode->i_op = &btrfs_dir_inode_operations; |
| 8491 | inode->i_fop = &btrfs_dir_file_operations; |
| 8492 | |
Miklos Szeredi | bfe8684 | 2011-10-28 14:13:29 +0200 | [diff] [blame] | 8493 | set_nlink(inode, 1); |
Chris Mason | dbe674a | 2008-07-17 12:54:05 -0400 | [diff] [blame] | 8494 | btrfs_i_size_write(inode, 0); |
Chris Mason | b0d5d10 | 2014-09-08 13:08:51 -0700 | [diff] [blame] | 8495 | unlock_new_inode(inode); |
Sven Wegener | 3b96362 | 2008-06-09 21:57:42 -0400 | [diff] [blame] | 8496 | |
Filipe David Borba Manana | 6354192 | 2014-01-07 11:47:46 +0000 | [diff] [blame] | 8497 | err = btrfs_subvol_inherit_props(trans, new_root, parent_root); |
| 8498 | if (err) |
| 8499 | btrfs_err(new_root->fs_info, |
David Sterba | 351fd35 | 2014-05-15 16:48:20 +0200 | [diff] [blame] | 8500 | "error inheriting subvolume %llu properties: %d", |
Filipe David Borba Manana | 6354192 | 2014-01-07 11:47:46 +0000 | [diff] [blame] | 8501 | new_root->root_key.objectid, err); |
| 8502 | |
Yan, Zheng | 76dda93 | 2009-09-21 16:00:26 -0400 | [diff] [blame] | 8503 | err = btrfs_update_inode(trans, new_root, inode); |
Christoph Hellwig | cb8e709 | 2008-10-09 13:39:39 -0400 | [diff] [blame] | 8504 | |
Yan, Zheng | 76dda93 | 2009-09-21 16:00:26 -0400 | [diff] [blame] | 8505 | iput(inode); |
Mark Fasheh | ce59897 | 2011-07-26 11:32:23 -0700 | [diff] [blame] | 8506 | return err; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 8507 | } |
| 8508 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 8509 | struct inode *btrfs_alloc_inode(struct super_block *sb) |
| 8510 | { |
| 8511 | struct btrfs_inode *ei; |
Yan, Zheng | 2ead6ae | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 8512 | struct inode *inode; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 8513 | |
| 8514 | ei = kmem_cache_alloc(btrfs_inode_cachep, GFP_NOFS); |
| 8515 | if (!ei) |
| 8516 | return NULL; |
Yan, Zheng | 2ead6ae | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 8517 | |
| 8518 | ei->root = NULL; |
Yan, Zheng | 2ead6ae | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 8519 | ei->generation = 0; |
Josef Bacik | 15ee9bc | 2007-08-10 16:22:09 -0400 | [diff] [blame] | 8520 | ei->last_trans = 0; |
Chris Mason | 257c62e | 2009-10-13 13:21:08 -0400 | [diff] [blame] | 8521 | ei->last_sub_trans = 0; |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 8522 | ei->logged_trans = 0; |
Yan, Zheng | 2ead6ae | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 8523 | ei->delalloc_bytes = 0; |
Wang Shilong | 47059d9 | 2014-07-03 18:22:07 +0800 | [diff] [blame] | 8524 | ei->defrag_bytes = 0; |
Yan, Zheng | 2ead6ae | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 8525 | ei->disk_i_size = 0; |
| 8526 | ei->flags = 0; |
Josef Bacik | 7709cde | 2011-08-04 10:25:02 -0400 | [diff] [blame] | 8527 | ei->csum_bytes = 0; |
Yan, Zheng | 2ead6ae | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 8528 | ei->index_cnt = (u64)-1; |
Miao Xie | 67de117 | 2013-12-26 13:07:06 +0800 | [diff] [blame] | 8529 | ei->dir_index = 0; |
Yan, Zheng | 2ead6ae | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 8530 | ei->last_unlink_trans = 0; |
Liu Bo | 46d8bc3 | 2012-08-29 01:07:55 -0600 | [diff] [blame] | 8531 | ei->last_log_commit = 0; |
Yan, Zheng | 2ead6ae | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 8532 | |
Josef Bacik | 9e0baf6 | 2011-07-15 15:16:44 +0000 | [diff] [blame] | 8533 | spin_lock_init(&ei->lock); |
| 8534 | ei->outstanding_extents = 0; |
| 8535 | ei->reserved_extents = 0; |
Yan, Zheng | 2ead6ae | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 8536 | |
Josef Bacik | 72ac3c0 | 2012-05-23 14:13:11 -0400 | [diff] [blame] | 8537 | ei->runtime_flags = 0; |
Li Zefan | 261507a0 | 2010-12-17 14:21:50 +0800 | [diff] [blame] | 8538 | ei->force_compress = BTRFS_COMPRESS_NONE; |
Yan, Zheng | 2ead6ae | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 8539 | |
Miao Xie | 16cdcec | 2011-04-22 18:12:22 +0800 | [diff] [blame] | 8540 | ei->delayed_node = NULL; |
| 8541 | |
Yan, Zheng | 2ead6ae | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 8542 | inode = &ei->vfs_inode; |
David Sterba | a8067e0 | 2011-04-21 00:34:43 +0200 | [diff] [blame] | 8543 | extent_map_tree_init(&ei->extent_tree); |
David Sterba | f993c88 | 2011-04-20 23:35:57 +0200 | [diff] [blame] | 8544 | extent_io_tree_init(&ei->io_tree, &inode->i_data); |
| 8545 | extent_io_tree_init(&ei->io_failure_tree, &inode->i_data); |
Josef Bacik | 0b32f4b | 2012-03-13 09:38:00 -0400 | [diff] [blame] | 8546 | ei->io_tree.track_uptodate = 1; |
| 8547 | ei->io_failure_tree.track_uptodate = 1; |
Josef Bacik | b812ce2 | 2012-11-16 13:56:32 -0500 | [diff] [blame] | 8548 | atomic_set(&ei->sync_writers, 0); |
Yan, Zheng | 2ead6ae | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 8549 | mutex_init(&ei->log_mutex); |
Josef Bacik | f248679 | 2012-01-13 12:09:22 -0500 | [diff] [blame] | 8550 | mutex_init(&ei->delalloc_mutex); |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 8551 | btrfs_ordered_inode_tree_init(&ei->ordered_tree); |
Yan, Zheng | 2ead6ae | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 8552 | INIT_LIST_HEAD(&ei->delalloc_inodes); |
Yan, Zheng | 2ead6ae | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 8553 | RB_CLEAR_NODE(&ei->rb_node); |
| 8554 | |
| 8555 | return inode; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 8556 | } |
| 8557 | |
Josef Bacik | aaedb55 | 2013-10-11 14:44:09 -0400 | [diff] [blame] | 8558 | #ifdef CONFIG_BTRFS_FS_RUN_SANITY_TESTS |
| 8559 | void btrfs_test_destroy_inode(struct inode *inode) |
| 8560 | { |
| 8561 | btrfs_drop_extent_cache(inode, 0, (u64)-1, 0); |
| 8562 | kmem_cache_free(btrfs_inode_cachep, BTRFS_I(inode)); |
| 8563 | } |
| 8564 | #endif |
| 8565 | |
Nick Piggin | fa0d7e3d | 2011-01-07 17:49:49 +1100 | [diff] [blame] | 8566 | static void btrfs_i_callback(struct rcu_head *head) |
| 8567 | { |
| 8568 | struct inode *inode = container_of(head, struct inode, i_rcu); |
Nick Piggin | fa0d7e3d | 2011-01-07 17:49:49 +1100 | [diff] [blame] | 8569 | kmem_cache_free(btrfs_inode_cachep, BTRFS_I(inode)); |
| 8570 | } |
| 8571 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 8572 | void btrfs_destroy_inode(struct inode *inode) |
| 8573 | { |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 8574 | struct btrfs_ordered_extent *ordered; |
Chris Mason | 5a3f23d | 2009-03-31 13:27:11 -0400 | [diff] [blame] | 8575 | struct btrfs_root *root = BTRFS_I(inode)->root; |
| 8576 | |
Al Viro | b3d9b7a | 2012-06-09 13:51:19 -0400 | [diff] [blame] | 8577 | WARN_ON(!hlist_empty(&inode->i_dentry)); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 8578 | WARN_ON(inode->i_data.nrpages); |
Josef Bacik | 9e0baf6 | 2011-07-15 15:16:44 +0000 | [diff] [blame] | 8579 | WARN_ON(BTRFS_I(inode)->outstanding_extents); |
| 8580 | WARN_ON(BTRFS_I(inode)->reserved_extents); |
Josef Bacik | 7709cde | 2011-08-04 10:25:02 -0400 | [diff] [blame] | 8581 | WARN_ON(BTRFS_I(inode)->delalloc_bytes); |
| 8582 | WARN_ON(BTRFS_I(inode)->csum_bytes); |
Wang Shilong | 47059d9 | 2014-07-03 18:22:07 +0800 | [diff] [blame] | 8583 | WARN_ON(BTRFS_I(inode)->defrag_bytes); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 8584 | |
Chris Mason | 5a3f23d | 2009-03-31 13:27:11 -0400 | [diff] [blame] | 8585 | /* |
Josef Bacik | a6dbd42 | 2009-11-11 15:53:34 -0500 | [diff] [blame] | 8586 | * This can happen where we create an inode, but somebody else also |
| 8587 | * created the same inode and we need to destroy the one we already |
| 8588 | * created. |
| 8589 | */ |
| 8590 | if (!root) |
| 8591 | goto free; |
| 8592 | |
Josef Bacik | 8a35d95 | 2012-05-23 14:26:42 -0400 | [diff] [blame] | 8593 | if (test_bit(BTRFS_INODE_HAS_ORPHAN_ITEM, |
| 8594 | &BTRFS_I(inode)->runtime_flags)) { |
Simon Kirby | c2cf52e | 2013-03-19 22:41:23 +0000 | [diff] [blame] | 8595 | btrfs_info(root->fs_info, "inode %llu still on the orphan list", |
Geert Uytterhoeven | c1c9ff7 | 2013-08-20 13:20:07 +0200 | [diff] [blame] | 8596 | btrfs_ino(inode)); |
Josef Bacik | 8a35d95 | 2012-05-23 14:26:42 -0400 | [diff] [blame] | 8597 | atomic_dec(&root->orphan_inodes); |
Josef Bacik | 7b12876 | 2008-07-24 12:17:14 -0400 | [diff] [blame] | 8598 | } |
Josef Bacik | 7b12876 | 2008-07-24 12:17:14 -0400 | [diff] [blame] | 8599 | |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 8600 | while (1) { |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 8601 | ordered = btrfs_lookup_first_ordered_extent(inode, (u64)-1); |
| 8602 | if (!ordered) |
| 8603 | break; |
| 8604 | else { |
Simon Kirby | c2cf52e | 2013-03-19 22:41:23 +0000 | [diff] [blame] | 8605 | 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] | 8606 | ordered->file_offset, ordered->len); |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 8607 | btrfs_remove_ordered_extent(inode, ordered); |
| 8608 | btrfs_put_ordered_extent(ordered); |
| 8609 | btrfs_put_ordered_extent(ordered); |
| 8610 | } |
| 8611 | } |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 8612 | inode_tree_del(inode); |
Zheng Yan | 5b21f2e | 2008-09-26 10:05:38 -0400 | [diff] [blame] | 8613 | btrfs_drop_extent_cache(inode, 0, (u64)-1, 0); |
Josef Bacik | a6dbd42 | 2009-11-11 15:53:34 -0500 | [diff] [blame] | 8614 | free: |
Nick Piggin | fa0d7e3d | 2011-01-07 17:49:49 +1100 | [diff] [blame] | 8615 | call_rcu(&inode->i_rcu, btrfs_i_callback); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 8616 | } |
| 8617 | |
Al Viro | 45321ac | 2010-06-07 13:43:19 -0400 | [diff] [blame] | 8618 | int btrfs_drop_inode(struct inode *inode) |
Yan, Zheng | 76dda93 | 2009-09-21 16:00:26 -0400 | [diff] [blame] | 8619 | { |
| 8620 | struct btrfs_root *root = BTRFS_I(inode)->root; |
Al Viro | 45321ac | 2010-06-07 13:43:19 -0400 | [diff] [blame] | 8621 | |
Naohiro Aota | 6379ef9 | 2013-06-06 09:56:34 +0000 | [diff] [blame] | 8622 | if (root == NULL) |
| 8623 | return 1; |
| 8624 | |
Liu Bo | fa6ac87 | 2013-02-20 14:10:23 +0000 | [diff] [blame] | 8625 | /* the snap/subvol tree is on deleting */ |
Stefan Behrens | 69e9c6c | 2013-09-05 16:58:43 +0200 | [diff] [blame] | 8626 | if (btrfs_root_refs(&root->root_item) == 0) |
Al Viro | 45321ac | 2010-06-07 13:43:19 -0400 | [diff] [blame] | 8627 | return 1; |
Yan, Zheng | 76dda93 | 2009-09-21 16:00:26 -0400 | [diff] [blame] | 8628 | else |
Al Viro | 45321ac | 2010-06-07 13:43:19 -0400 | [diff] [blame] | 8629 | return generic_drop_inode(inode); |
Yan, Zheng | 76dda93 | 2009-09-21 16:00:26 -0400 | [diff] [blame] | 8630 | } |
| 8631 | |
Sven Wegener | 0ee0fda | 2008-07-30 16:54:26 -0400 | [diff] [blame] | 8632 | static void init_once(void *foo) |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 8633 | { |
| 8634 | struct btrfs_inode *ei = (struct btrfs_inode *) foo; |
| 8635 | |
| 8636 | inode_init_once(&ei->vfs_inode); |
| 8637 | } |
| 8638 | |
| 8639 | void btrfs_destroy_cachep(void) |
| 8640 | { |
Kirill A. Shutemov | 8c0a853 | 2012-09-26 11:33:07 +1000 | [diff] [blame] | 8641 | /* |
| 8642 | * Make sure all delayed rcu free inodes are flushed before we |
| 8643 | * destroy cache. |
| 8644 | */ |
| 8645 | rcu_barrier(); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 8646 | if (btrfs_inode_cachep) |
| 8647 | kmem_cache_destroy(btrfs_inode_cachep); |
| 8648 | if (btrfs_trans_handle_cachep) |
| 8649 | kmem_cache_destroy(btrfs_trans_handle_cachep); |
| 8650 | if (btrfs_transaction_cachep) |
| 8651 | kmem_cache_destroy(btrfs_transaction_cachep); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 8652 | if (btrfs_path_cachep) |
| 8653 | kmem_cache_destroy(btrfs_path_cachep); |
Josef Bacik | dc89e98 | 2011-01-28 17:05:48 -0500 | [diff] [blame] | 8654 | if (btrfs_free_space_cachep) |
| 8655 | kmem_cache_destroy(btrfs_free_space_cachep); |
Miao Xie | 8ccf6f19 | 2012-10-25 09:28:04 +0000 | [diff] [blame] | 8656 | if (btrfs_delalloc_work_cachep) |
| 8657 | kmem_cache_destroy(btrfs_delalloc_work_cachep); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 8658 | } |
| 8659 | |
| 8660 | int btrfs_init_cachep(void) |
| 8661 | { |
David Sterba | 837e197 | 2012-09-07 03:00:48 -0600 | [diff] [blame] | 8662 | btrfs_inode_cachep = kmem_cache_create("btrfs_inode", |
Christoph Hellwig | 9601e3f | 2009-04-13 15:33:09 +0200 | [diff] [blame] | 8663 | sizeof(struct btrfs_inode), 0, |
| 8664 | SLAB_RECLAIM_ACCOUNT | SLAB_MEM_SPREAD, init_once); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 8665 | if (!btrfs_inode_cachep) |
| 8666 | goto fail; |
Christoph Hellwig | 9601e3f | 2009-04-13 15:33:09 +0200 | [diff] [blame] | 8667 | |
David Sterba | 837e197 | 2012-09-07 03:00:48 -0600 | [diff] [blame] | 8668 | btrfs_trans_handle_cachep = kmem_cache_create("btrfs_trans_handle", |
Christoph Hellwig | 9601e3f | 2009-04-13 15:33:09 +0200 | [diff] [blame] | 8669 | sizeof(struct btrfs_trans_handle), 0, |
| 8670 | SLAB_RECLAIM_ACCOUNT | SLAB_MEM_SPREAD, NULL); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 8671 | if (!btrfs_trans_handle_cachep) |
| 8672 | goto fail; |
Christoph Hellwig | 9601e3f | 2009-04-13 15:33:09 +0200 | [diff] [blame] | 8673 | |
David Sterba | 837e197 | 2012-09-07 03:00:48 -0600 | [diff] [blame] | 8674 | btrfs_transaction_cachep = kmem_cache_create("btrfs_transaction", |
Christoph Hellwig | 9601e3f | 2009-04-13 15:33:09 +0200 | [diff] [blame] | 8675 | sizeof(struct btrfs_transaction), 0, |
| 8676 | SLAB_RECLAIM_ACCOUNT | SLAB_MEM_SPREAD, NULL); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 8677 | if (!btrfs_transaction_cachep) |
| 8678 | goto fail; |
Christoph Hellwig | 9601e3f | 2009-04-13 15:33:09 +0200 | [diff] [blame] | 8679 | |
David Sterba | 837e197 | 2012-09-07 03:00:48 -0600 | [diff] [blame] | 8680 | btrfs_path_cachep = kmem_cache_create("btrfs_path", |
Christoph Hellwig | 9601e3f | 2009-04-13 15:33:09 +0200 | [diff] [blame] | 8681 | sizeof(struct btrfs_path), 0, |
| 8682 | SLAB_RECLAIM_ACCOUNT | SLAB_MEM_SPREAD, NULL); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 8683 | if (!btrfs_path_cachep) |
| 8684 | goto fail; |
Christoph Hellwig | 9601e3f | 2009-04-13 15:33:09 +0200 | [diff] [blame] | 8685 | |
David Sterba | 837e197 | 2012-09-07 03:00:48 -0600 | [diff] [blame] | 8686 | btrfs_free_space_cachep = kmem_cache_create("btrfs_free_space", |
Josef Bacik | dc89e98 | 2011-01-28 17:05:48 -0500 | [diff] [blame] | 8687 | sizeof(struct btrfs_free_space), 0, |
| 8688 | SLAB_RECLAIM_ACCOUNT | SLAB_MEM_SPREAD, NULL); |
| 8689 | if (!btrfs_free_space_cachep) |
| 8690 | goto fail; |
| 8691 | |
Miao Xie | 8ccf6f19 | 2012-10-25 09:28:04 +0000 | [diff] [blame] | 8692 | btrfs_delalloc_work_cachep = kmem_cache_create("btrfs_delalloc_work", |
| 8693 | sizeof(struct btrfs_delalloc_work), 0, |
| 8694 | SLAB_RECLAIM_ACCOUNT | SLAB_MEM_SPREAD, |
| 8695 | NULL); |
| 8696 | if (!btrfs_delalloc_work_cachep) |
| 8697 | goto fail; |
| 8698 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 8699 | return 0; |
| 8700 | fail: |
| 8701 | btrfs_destroy_cachep(); |
| 8702 | return -ENOMEM; |
| 8703 | } |
| 8704 | |
| 8705 | static int btrfs_getattr(struct vfsmount *mnt, |
| 8706 | struct dentry *dentry, struct kstat *stat) |
| 8707 | { |
Miao Xie | df0af1a | 2013-01-29 10:11:59 +0000 | [diff] [blame] | 8708 | u64 delalloc_bytes; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 8709 | struct inode *inode = dentry->d_inode; |
David Sterba | fadc0d8 | 2011-11-20 07:33:38 -0500 | [diff] [blame] | 8710 | u32 blocksize = inode->i_sb->s_blocksize; |
| 8711 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 8712 | generic_fillattr(inode, stat); |
Al Viro | 0ee5dc6 | 2011-07-07 15:44:25 -0400 | [diff] [blame] | 8713 | stat->dev = BTRFS_I(inode)->root->anon_dev; |
Chris Mason | d666746 | 2008-01-03 14:51:00 -0500 | [diff] [blame] | 8714 | stat->blksize = PAGE_CACHE_SIZE; |
Miao Xie | df0af1a | 2013-01-29 10:11:59 +0000 | [diff] [blame] | 8715 | |
| 8716 | spin_lock(&BTRFS_I(inode)->lock); |
| 8717 | delalloc_bytes = BTRFS_I(inode)->delalloc_bytes; |
| 8718 | spin_unlock(&BTRFS_I(inode)->lock); |
David Sterba | fadc0d8 | 2011-11-20 07:33:38 -0500 | [diff] [blame] | 8719 | stat->blocks = (ALIGN(inode_get_bytes(inode), blocksize) + |
Miao Xie | df0af1a | 2013-01-29 10:11:59 +0000 | [diff] [blame] | 8720 | ALIGN(delalloc_bytes, blocksize)) >> 9; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 8721 | return 0; |
| 8722 | } |
| 8723 | |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 8724 | static int btrfs_rename(struct inode *old_dir, struct dentry *old_dentry, |
| 8725 | struct inode *new_dir, struct dentry *new_dentry) |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 8726 | { |
| 8727 | struct btrfs_trans_handle *trans; |
| 8728 | struct btrfs_root *root = BTRFS_I(old_dir)->root; |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 8729 | struct btrfs_root *dest = BTRFS_I(new_dir)->root; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 8730 | struct inode *new_inode = new_dentry->d_inode; |
| 8731 | struct inode *old_inode = old_dentry->d_inode; |
| 8732 | struct timespec ctime = CURRENT_TIME; |
Chris Mason | 00e4e6b | 2008-08-05 11:18:09 -0400 | [diff] [blame] | 8733 | u64 index = 0; |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 8734 | u64 root_objectid; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 8735 | int ret; |
Li Zefan | 33345d01 | 2011-04-20 10:31:50 +0800 | [diff] [blame] | 8736 | u64 old_ino = btrfs_ino(old_inode); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 8737 | |
Li Zefan | 33345d01 | 2011-04-20 10:31:50 +0800 | [diff] [blame] | 8738 | if (btrfs_ino(new_dir) == BTRFS_EMPTY_SUBVOL_DIR_OBJECTID) |
Yan, Zheng | f679a84 | 2009-09-24 09:17:31 -0400 | [diff] [blame] | 8739 | return -EPERM; |
| 8740 | |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 8741 | /* we only allow rename subvolume link between subvolumes */ |
Li Zefan | 33345d01 | 2011-04-20 10:31:50 +0800 | [diff] [blame] | 8742 | if (old_ino != BTRFS_FIRST_FREE_OBJECTID && root != dest) |
Chris Mason | 3394e16 | 2008-11-17 20:42:26 -0500 | [diff] [blame] | 8743 | return -EXDEV; |
| 8744 | |
Li Zefan | 33345d01 | 2011-04-20 10:31:50 +0800 | [diff] [blame] | 8745 | if (old_ino == BTRFS_EMPTY_SUBVOL_DIR_OBJECTID || |
| 8746 | (new_inode && btrfs_ino(new_inode) == BTRFS_FIRST_FREE_OBJECTID)) |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 8747 | return -ENOTEMPTY; |
| 8748 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 8749 | if (S_ISDIR(old_inode->i_mode) && new_inode && |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 8750 | new_inode->i_size > BTRFS_EMPTY_DIR_SIZE) |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 8751 | return -ENOTEMPTY; |
Chris Mason | 9c52057 | 2012-12-17 14:26:57 -0500 | [diff] [blame] | 8752 | |
| 8753 | |
| 8754 | /* check for collisions, even if the name isn't there */ |
Josef Bacik | 4871c15 | 2013-10-09 12:24:04 -0400 | [diff] [blame] | 8755 | ret = btrfs_check_dir_item_collision(dest, new_dir->i_ino, |
Chris Mason | 9c52057 | 2012-12-17 14:26:57 -0500 | [diff] [blame] | 8756 | new_dentry->d_name.name, |
| 8757 | new_dentry->d_name.len); |
| 8758 | |
| 8759 | if (ret) { |
| 8760 | if (ret == -EEXIST) { |
| 8761 | /* we shouldn't get |
| 8762 | * eexist without a new_inode */ |
Dulshani Gunawardhana | fae7f21 | 2013-10-31 10:30:08 +0530 | [diff] [blame] | 8763 | if (WARN_ON(!new_inode)) { |
Chris Mason | 9c52057 | 2012-12-17 14:26:57 -0500 | [diff] [blame] | 8764 | return ret; |
| 8765 | } |
| 8766 | } else { |
| 8767 | /* maybe -EOVERFLOW */ |
| 8768 | return ret; |
| 8769 | } |
| 8770 | } |
| 8771 | ret = 0; |
| 8772 | |
Chris Mason | 5a3f23d | 2009-03-31 13:27:11 -0400 | [diff] [blame] | 8773 | /* |
Chris Mason | 8d875f9 | 2014-08-12 10:47:42 -0700 | [diff] [blame] | 8774 | * we're using rename to replace one file with another. Start IO on it |
| 8775 | * 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] | 8776 | */ |
Chris Mason | 8d875f9 | 2014-08-12 10:47:42 -0700 | [diff] [blame] | 8777 | 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] | 8778 | filemap_flush(old_inode->i_mapping); |
| 8779 | |
Yan, Zheng | 76dda93 | 2009-09-21 16:00:26 -0400 | [diff] [blame] | 8780 | /* close the racy window with snapshot create/destroy ioctl */ |
Li Zefan | 33345d01 | 2011-04-20 10:31:50 +0800 | [diff] [blame] | 8781 | if (old_ino == BTRFS_FIRST_FREE_OBJECTID) |
Yan, Zheng | 76dda93 | 2009-09-21 16:00:26 -0400 | [diff] [blame] | 8782 | down_read(&root->fs_info->subvol_sem); |
Yan, Zheng | a22285a | 2010-05-16 10:48:46 -0400 | [diff] [blame] | 8783 | /* |
| 8784 | * We want to reserve the absolute worst case amount of items. So if |
| 8785 | * both inodes are subvols and we need to unlink them then that would |
| 8786 | * require 4 item modifications, but if they are both normal inodes it |
| 8787 | * would require 5 item modifications, so we'll assume their normal |
| 8788 | * inodes. So 5 * 2 is 10, plus 1 for the new link, so 11 total items |
| 8789 | * should cover the worst case number of items we'll modify. |
| 8790 | */ |
Josef Bacik | 6e137ed | 2013-03-26 15:26:55 -0400 | [diff] [blame] | 8791 | trans = btrfs_start_transaction(root, 11); |
Johann Lombardi | b44c59a | 2011-03-31 13:23:47 +0000 | [diff] [blame] | 8792 | if (IS_ERR(trans)) { |
| 8793 | ret = PTR_ERR(trans); |
| 8794 | goto out_notrans; |
| 8795 | } |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 8796 | |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 8797 | if (dest != root) |
| 8798 | btrfs_record_root_in_trans(trans, dest); |
| 8799 | |
Yan, Zheng | a571952 | 2009-09-24 09:17:31 -0400 | [diff] [blame] | 8800 | ret = btrfs_set_inode_index(new_dir, &index); |
| 8801 | if (ret) |
| 8802 | goto out_fail; |
Chris Mason | 5a3f23d | 2009-03-31 13:27:11 -0400 | [diff] [blame] | 8803 | |
Miao Xie | 67de117 | 2013-12-26 13:07:06 +0800 | [diff] [blame] | 8804 | BTRFS_I(old_inode)->dir_index = 0ULL; |
Li Zefan | 33345d01 | 2011-04-20 10:31:50 +0800 | [diff] [blame] | 8805 | if (unlikely(old_ino == BTRFS_FIRST_FREE_OBJECTID)) { |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 8806 | /* force full log commit if subvolume involved. */ |
Miao Xie | 995946d | 2014-04-02 19:51:06 +0800 | [diff] [blame] | 8807 | btrfs_set_log_full_commit(root->fs_info, trans); |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 8808 | } else { |
Yan, Zheng | a571952 | 2009-09-24 09:17:31 -0400 | [diff] [blame] | 8809 | ret = btrfs_insert_inode_ref(trans, dest, |
| 8810 | new_dentry->d_name.name, |
| 8811 | new_dentry->d_name.len, |
Li Zefan | 33345d01 | 2011-04-20 10:31:50 +0800 | [diff] [blame] | 8812 | old_ino, |
| 8813 | btrfs_ino(new_dir), index); |
Yan, Zheng | a571952 | 2009-09-24 09:17:31 -0400 | [diff] [blame] | 8814 | if (ret) |
| 8815 | goto out_fail; |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 8816 | /* |
| 8817 | * this is an ugly little race, but the rename is required |
| 8818 | * to make sure that if we crash, the inode is either at the |
| 8819 | * old name or the new one. pinning the log transaction lets |
| 8820 | * us make sure we don't allow a log commit to come in after |
| 8821 | * we unlink the name but before we add the new name back in. |
| 8822 | */ |
| 8823 | btrfs_pin_log_trans(root); |
| 8824 | } |
Chris Mason | 5a3f23d | 2009-03-31 13:27:11 -0400 | [diff] [blame] | 8825 | |
Josef Bacik | 0c4d2d9 | 2012-04-05 15:03:02 -0400 | [diff] [blame] | 8826 | inode_inc_iversion(old_dir); |
| 8827 | inode_inc_iversion(new_dir); |
| 8828 | inode_inc_iversion(old_inode); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 8829 | old_dir->i_ctime = old_dir->i_mtime = ctime; |
| 8830 | new_dir->i_ctime = new_dir->i_mtime = ctime; |
| 8831 | old_inode->i_ctime = ctime; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 8832 | |
Chris Mason | 12fcfd2 | 2009-03-24 10:24:20 -0400 | [diff] [blame] | 8833 | if (old_dentry->d_parent != new_dentry->d_parent) |
| 8834 | btrfs_record_unlink_dir(trans, old_dir, old_inode, 1); |
| 8835 | |
Li Zefan | 33345d01 | 2011-04-20 10:31:50 +0800 | [diff] [blame] | 8836 | if (unlikely(old_ino == BTRFS_FIRST_FREE_OBJECTID)) { |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 8837 | root_objectid = BTRFS_I(old_inode)->root->root_key.objectid; |
| 8838 | ret = btrfs_unlink_subvol(trans, root, old_dir, root_objectid, |
| 8839 | old_dentry->d_name.name, |
| 8840 | old_dentry->d_name.len); |
| 8841 | } else { |
Al Viro | 9298679 | 2011-03-04 17:14:37 +0000 | [diff] [blame] | 8842 | ret = __btrfs_unlink_inode(trans, root, old_dir, |
| 8843 | old_dentry->d_inode, |
| 8844 | old_dentry->d_name.name, |
| 8845 | old_dentry->d_name.len); |
| 8846 | if (!ret) |
| 8847 | ret = btrfs_update_inode(trans, root, old_inode); |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 8848 | } |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 8849 | if (ret) { |
| 8850 | btrfs_abort_transaction(trans, root, ret); |
| 8851 | goto out_fail; |
| 8852 | } |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 8853 | |
| 8854 | if (new_inode) { |
Josef Bacik | 0c4d2d9 | 2012-04-05 15:03:02 -0400 | [diff] [blame] | 8855 | inode_inc_iversion(new_inode); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 8856 | new_inode->i_ctime = CURRENT_TIME; |
Li Zefan | 33345d01 | 2011-04-20 10:31:50 +0800 | [diff] [blame] | 8857 | if (unlikely(btrfs_ino(new_inode) == |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 8858 | BTRFS_EMPTY_SUBVOL_DIR_OBJECTID)) { |
| 8859 | root_objectid = BTRFS_I(new_inode)->location.objectid; |
| 8860 | ret = btrfs_unlink_subvol(trans, dest, new_dir, |
| 8861 | root_objectid, |
| 8862 | new_dentry->d_name.name, |
| 8863 | new_dentry->d_name.len); |
| 8864 | BUG_ON(new_inode->i_nlink == 0); |
| 8865 | } else { |
| 8866 | ret = btrfs_unlink_inode(trans, dest, new_dir, |
| 8867 | new_dentry->d_inode, |
| 8868 | new_dentry->d_name.name, |
| 8869 | new_dentry->d_name.len); |
| 8870 | } |
Josef Bacik | 4ef31a4 | 2013-08-13 14:10:08 -0400 | [diff] [blame] | 8871 | if (!ret && new_inode->i_nlink == 0) |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 8872 | ret = btrfs_orphan_add(trans, new_dentry->d_inode); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 8873 | if (ret) { |
| 8874 | btrfs_abort_transaction(trans, root, ret); |
| 8875 | goto out_fail; |
| 8876 | } |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 8877 | } |
Josef Bacik | aec7477 | 2008-07-24 12:12:38 -0400 | [diff] [blame] | 8878 | |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 8879 | ret = btrfs_add_link(trans, new_dir, old_inode, |
| 8880 | new_dentry->d_name.name, |
Yan, Zheng | a571952 | 2009-09-24 09:17:31 -0400 | [diff] [blame] | 8881 | new_dentry->d_name.len, 0, index); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 8882 | if (ret) { |
| 8883 | btrfs_abort_transaction(trans, root, ret); |
| 8884 | goto out_fail; |
| 8885 | } |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 8886 | |
Miao Xie | 67de117 | 2013-12-26 13:07:06 +0800 | [diff] [blame] | 8887 | if (old_inode->i_nlink == 1) |
| 8888 | BTRFS_I(old_inode)->dir_index = index; |
| 8889 | |
Li Zefan | 33345d01 | 2011-04-20 10:31:50 +0800 | [diff] [blame] | 8890 | if (old_ino != BTRFS_FIRST_FREE_OBJECTID) { |
Al Viro | 10d9f30 | 2011-07-16 23:09:10 -0400 | [diff] [blame] | 8891 | struct dentry *parent = new_dentry->d_parent; |
Josef Bacik | 6a91221 | 2010-11-20 09:48:00 +0000 | [diff] [blame] | 8892 | btrfs_log_new_name(trans, old_inode, old_dir, parent); |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 8893 | btrfs_end_log_trans(root); |
| 8894 | } |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 8895 | out_fail: |
Josef Bacik | 7ad85bb | 2012-01-12 19:10:12 -0500 | [diff] [blame] | 8896 | btrfs_end_transaction(trans, root); |
Johann Lombardi | b44c59a | 2011-03-31 13:23:47 +0000 | [diff] [blame] | 8897 | out_notrans: |
Li Zefan | 33345d01 | 2011-04-20 10:31:50 +0800 | [diff] [blame] | 8898 | if (old_ino == BTRFS_FIRST_FREE_OBJECTID) |
Yan, Zheng | 76dda93 | 2009-09-21 16:00:26 -0400 | [diff] [blame] | 8899 | up_read(&root->fs_info->subvol_sem); |
Josef Bacik | 9ed74f2 | 2009-09-11 16:12:44 -0400 | [diff] [blame] | 8900 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 8901 | return ret; |
| 8902 | } |
| 8903 | |
Miklos Szeredi | 80ace85 | 2014-07-23 15:15:32 +0200 | [diff] [blame] | 8904 | static int btrfs_rename2(struct inode *old_dir, struct dentry *old_dentry, |
| 8905 | struct inode *new_dir, struct dentry *new_dentry, |
| 8906 | unsigned int flags) |
| 8907 | { |
| 8908 | if (flags & ~RENAME_NOREPLACE) |
| 8909 | return -EINVAL; |
| 8910 | |
| 8911 | return btrfs_rename(old_dir, old_dentry, new_dir, new_dentry); |
| 8912 | } |
| 8913 | |
Miao Xie | 8ccf6f19 | 2012-10-25 09:28:04 +0000 | [diff] [blame] | 8914 | static void btrfs_run_delalloc_work(struct btrfs_work *work) |
| 8915 | { |
| 8916 | struct btrfs_delalloc_work *delalloc_work; |
Josef Bacik | 9f23e28 | 2013-10-28 15:03:41 -0400 | [diff] [blame] | 8917 | struct inode *inode; |
Miao Xie | 8ccf6f19 | 2012-10-25 09:28:04 +0000 | [diff] [blame] | 8918 | |
| 8919 | delalloc_work = container_of(work, struct btrfs_delalloc_work, |
| 8920 | work); |
Josef Bacik | 9f23e28 | 2013-10-28 15:03:41 -0400 | [diff] [blame] | 8921 | inode = delalloc_work->inode; |
| 8922 | if (delalloc_work->wait) { |
| 8923 | btrfs_wait_ordered_range(inode, 0, (u64)-1); |
| 8924 | } else { |
| 8925 | filemap_flush(inode->i_mapping); |
| 8926 | if (test_bit(BTRFS_INODE_HAS_ASYNC_EXTENT, |
| 8927 | &BTRFS_I(inode)->runtime_flags)) |
| 8928 | filemap_flush(inode->i_mapping); |
| 8929 | } |
Miao Xie | 8ccf6f19 | 2012-10-25 09:28:04 +0000 | [diff] [blame] | 8930 | |
| 8931 | if (delalloc_work->delay_iput) |
Josef Bacik | 9f23e28 | 2013-10-28 15:03:41 -0400 | [diff] [blame] | 8932 | btrfs_add_delayed_iput(inode); |
Miao Xie | 8ccf6f19 | 2012-10-25 09:28:04 +0000 | [diff] [blame] | 8933 | else |
Josef Bacik | 9f23e28 | 2013-10-28 15:03:41 -0400 | [diff] [blame] | 8934 | iput(inode); |
Miao Xie | 8ccf6f19 | 2012-10-25 09:28:04 +0000 | [diff] [blame] | 8935 | complete(&delalloc_work->completion); |
| 8936 | } |
| 8937 | |
| 8938 | struct btrfs_delalloc_work *btrfs_alloc_delalloc_work(struct inode *inode, |
| 8939 | int wait, int delay_iput) |
| 8940 | { |
| 8941 | struct btrfs_delalloc_work *work; |
| 8942 | |
| 8943 | work = kmem_cache_zalloc(btrfs_delalloc_work_cachep, GFP_NOFS); |
| 8944 | if (!work) |
| 8945 | return NULL; |
| 8946 | |
| 8947 | init_completion(&work->completion); |
| 8948 | INIT_LIST_HEAD(&work->list); |
| 8949 | work->inode = inode; |
| 8950 | work->wait = wait; |
| 8951 | work->delay_iput = delay_iput; |
Liu Bo | 9e0af23 | 2014-08-15 23:36:53 +0800 | [diff] [blame] | 8952 | WARN_ON_ONCE(!inode); |
| 8953 | btrfs_init_work(&work->work, btrfs_flush_delalloc_helper, |
| 8954 | btrfs_run_delalloc_work, NULL, NULL); |
Miao Xie | 8ccf6f19 | 2012-10-25 09:28:04 +0000 | [diff] [blame] | 8955 | |
| 8956 | return work; |
| 8957 | } |
| 8958 | |
| 8959 | void btrfs_wait_and_free_delalloc_work(struct btrfs_delalloc_work *work) |
| 8960 | { |
| 8961 | wait_for_completion(&work->completion); |
| 8962 | kmem_cache_free(btrfs_delalloc_work_cachep, work); |
| 8963 | } |
| 8964 | |
Chris Mason | d352ac6 | 2008-09-29 15:18:18 -0400 | [diff] [blame] | 8965 | /* |
| 8966 | * some fairly slow code that needs optimization. This walks the list |
| 8967 | * of all the inodes with pending delalloc and forces them to disk. |
| 8968 | */ |
Miao Xie | 6c255e6 | 2014-03-06 13:55:01 +0800 | [diff] [blame] | 8969 | static int __start_delalloc_inodes(struct btrfs_root *root, int delay_iput, |
| 8970 | int nr) |
Chris Mason | ea8c281 | 2008-08-04 23:17:27 -0400 | [diff] [blame] | 8971 | { |
Chris Mason | ea8c281 | 2008-08-04 23:17:27 -0400 | [diff] [blame] | 8972 | struct btrfs_inode *binode; |
Zheng Yan | 5b21f2e | 2008-09-26 10:05:38 -0400 | [diff] [blame] | 8973 | struct inode *inode; |
Miao Xie | 8ccf6f19 | 2012-10-25 09:28:04 +0000 | [diff] [blame] | 8974 | struct btrfs_delalloc_work *work, *next; |
| 8975 | struct list_head works; |
Miao Xie | 1eafa6c | 2013-01-22 10:49:00 +0000 | [diff] [blame] | 8976 | struct list_head splice; |
Miao Xie | 8ccf6f19 | 2012-10-25 09:28:04 +0000 | [diff] [blame] | 8977 | int ret = 0; |
Chris Mason | ea8c281 | 2008-08-04 23:17:27 -0400 | [diff] [blame] | 8978 | |
Miao Xie | 8ccf6f19 | 2012-10-25 09:28:04 +0000 | [diff] [blame] | 8979 | INIT_LIST_HEAD(&works); |
Miao Xie | 1eafa6c | 2013-01-22 10:49:00 +0000 | [diff] [blame] | 8980 | INIT_LIST_HEAD(&splice); |
Miao Xie | 63607cc | 2013-01-22 10:50:35 +0000 | [diff] [blame] | 8981 | |
Miao Xie | 573bfb7 | 2014-03-06 13:55:03 +0800 | [diff] [blame] | 8982 | mutex_lock(&root->delalloc_mutex); |
Miao Xie | eb73c1b | 2013-05-15 07:48:22 +0000 | [diff] [blame] | 8983 | spin_lock(&root->delalloc_lock); |
| 8984 | list_splice_init(&root->delalloc_inodes, &splice); |
Miao Xie | 1eafa6c | 2013-01-22 10:49:00 +0000 | [diff] [blame] | 8985 | while (!list_empty(&splice)) { |
| 8986 | binode = list_entry(splice.next, struct btrfs_inode, |
Chris Mason | ea8c281 | 2008-08-04 23:17:27 -0400 | [diff] [blame] | 8987 | delalloc_inodes); |
Miao Xie | 1eafa6c | 2013-01-22 10:49:00 +0000 | [diff] [blame] | 8988 | |
Miao Xie | eb73c1b | 2013-05-15 07:48:22 +0000 | [diff] [blame] | 8989 | list_move_tail(&binode->delalloc_inodes, |
| 8990 | &root->delalloc_inodes); |
Zheng Yan | 5b21f2e | 2008-09-26 10:05:38 -0400 | [diff] [blame] | 8991 | inode = igrab(&binode->vfs_inode); |
Miao Xie | df0af1a | 2013-01-29 10:11:59 +0000 | [diff] [blame] | 8992 | if (!inode) { |
Miao Xie | eb73c1b | 2013-05-15 07:48:22 +0000 | [diff] [blame] | 8993 | cond_resched_lock(&root->delalloc_lock); |
Miao Xie | 1eafa6c | 2013-01-22 10:49:00 +0000 | [diff] [blame] | 8994 | continue; |
Miao Xie | df0af1a | 2013-01-29 10:11:59 +0000 | [diff] [blame] | 8995 | } |
Miao Xie | eb73c1b | 2013-05-15 07:48:22 +0000 | [diff] [blame] | 8996 | spin_unlock(&root->delalloc_lock); |
Miao Xie | 1eafa6c | 2013-01-22 10:49:00 +0000 | [diff] [blame] | 8997 | |
| 8998 | work = btrfs_alloc_delalloc_work(inode, 0, delay_iput); |
David Sterba | 5d99a998 | 2014-09-29 19:20:37 +0200 | [diff] [blame] | 8999 | if (!work) { |
Josef Bacik | f4ab9ea | 2013-09-17 11:25:44 -0400 | [diff] [blame] | 9000 | if (delay_iput) |
| 9001 | btrfs_add_delayed_iput(inode); |
| 9002 | else |
| 9003 | iput(inode); |
Miao Xie | 1eafa6c | 2013-01-22 10:49:00 +0000 | [diff] [blame] | 9004 | ret = -ENOMEM; |
Wang Shilong | a1ecaab | 2014-04-02 19:53:32 +0800 | [diff] [blame] | 9005 | goto out; |
Zheng Yan | 5b21f2e | 2008-09-26 10:05:38 -0400 | [diff] [blame] | 9006 | } |
Miao Xie | 1eafa6c | 2013-01-22 10:49:00 +0000 | [diff] [blame] | 9007 | list_add_tail(&work->list, &works); |
Qu Wenruo | a44903a | 2014-02-28 10:46:09 +0800 | [diff] [blame] | 9008 | btrfs_queue_work(root->fs_info->flush_workers, |
| 9009 | &work->work); |
Miao Xie | 6c255e6 | 2014-03-06 13:55:01 +0800 | [diff] [blame] | 9010 | ret++; |
| 9011 | if (nr != -1 && ret >= nr) |
Wang Shilong | a1ecaab | 2014-04-02 19:53:32 +0800 | [diff] [blame] | 9012 | goto out; |
Zheng Yan | 5b21f2e | 2008-09-26 10:05:38 -0400 | [diff] [blame] | 9013 | cond_resched(); |
Miao Xie | eb73c1b | 2013-05-15 07:48:22 +0000 | [diff] [blame] | 9014 | spin_lock(&root->delalloc_lock); |
Chris Mason | ea8c281 | 2008-08-04 23:17:27 -0400 | [diff] [blame] | 9015 | } |
Miao Xie | eb73c1b | 2013-05-15 07:48:22 +0000 | [diff] [blame] | 9016 | spin_unlock(&root->delalloc_lock); |
Chris Mason | 8c8bee1 | 2008-09-29 11:19:10 -0400 | [diff] [blame] | 9017 | |
Wang Shilong | a1ecaab | 2014-04-02 19:53:32 +0800 | [diff] [blame] | 9018 | out: |
Miao Xie | 1eafa6c | 2013-01-22 10:49:00 +0000 | [diff] [blame] | 9019 | list_for_each_entry_safe(work, next, &works, list) { |
| 9020 | list_del_init(&work->list); |
| 9021 | btrfs_wait_and_free_delalloc_work(work); |
| 9022 | } |
| 9023 | |
Miao Xie | eb73c1b | 2013-05-15 07:48:22 +0000 | [diff] [blame] | 9024 | if (!list_empty_careful(&splice)) { |
| 9025 | spin_lock(&root->delalloc_lock); |
| 9026 | list_splice_tail(&splice, &root->delalloc_inodes); |
| 9027 | spin_unlock(&root->delalloc_lock); |
| 9028 | } |
Miao Xie | 573bfb7 | 2014-03-06 13:55:03 +0800 | [diff] [blame] | 9029 | mutex_unlock(&root->delalloc_mutex); |
Miao Xie | eb73c1b | 2013-05-15 07:48:22 +0000 | [diff] [blame] | 9030 | return ret; |
| 9031 | } |
| 9032 | |
| 9033 | int btrfs_start_delalloc_inodes(struct btrfs_root *root, int delay_iput) |
| 9034 | { |
| 9035 | int ret; |
| 9036 | |
Wang Shilong | 2c21b4d | 2014-01-14 19:42:20 +0800 | [diff] [blame] | 9037 | if (test_bit(BTRFS_FS_STATE_ERROR, &root->fs_info->fs_state)) |
Miao Xie | eb73c1b | 2013-05-15 07:48:22 +0000 | [diff] [blame] | 9038 | return -EROFS; |
| 9039 | |
Miao Xie | 6c255e6 | 2014-03-06 13:55:01 +0800 | [diff] [blame] | 9040 | ret = __start_delalloc_inodes(root, delay_iput, -1); |
| 9041 | if (ret > 0) |
| 9042 | ret = 0; |
Miao Xie | eb73c1b | 2013-05-15 07:48:22 +0000 | [diff] [blame] | 9043 | /* |
| 9044 | * the filemap_flush will queue IO into the worker threads, but |
Chris Mason | 8c8bee1 | 2008-09-29 11:19:10 -0400 | [diff] [blame] | 9045 | * we have to make sure the IO is actually started and that |
| 9046 | * ordered extents get created before we return |
| 9047 | */ |
| 9048 | atomic_inc(&root->fs_info->async_submit_draining); |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 9049 | while (atomic_read(&root->fs_info->nr_async_submits) || |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 9050 | atomic_read(&root->fs_info->async_delalloc_pages)) { |
Chris Mason | 8c8bee1 | 2008-09-29 11:19:10 -0400 | [diff] [blame] | 9051 | wait_event(root->fs_info->async_submit_wait, |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 9052 | (atomic_read(&root->fs_info->nr_async_submits) == 0 && |
| 9053 | atomic_read(&root->fs_info->async_delalloc_pages) == 0)); |
Chris Mason | 8c8bee1 | 2008-09-29 11:19:10 -0400 | [diff] [blame] | 9054 | } |
| 9055 | atomic_dec(&root->fs_info->async_submit_draining); |
Miao Xie | eb73c1b | 2013-05-15 07:48:22 +0000 | [diff] [blame] | 9056 | return ret; |
| 9057 | } |
| 9058 | |
Miao Xie | 6c255e6 | 2014-03-06 13:55:01 +0800 | [diff] [blame] | 9059 | int btrfs_start_delalloc_roots(struct btrfs_fs_info *fs_info, int delay_iput, |
| 9060 | int nr) |
Miao Xie | eb73c1b | 2013-05-15 07:48:22 +0000 | [diff] [blame] | 9061 | { |
| 9062 | struct btrfs_root *root; |
| 9063 | struct list_head splice; |
| 9064 | int ret; |
| 9065 | |
Wang Shilong | 2c21b4d | 2014-01-14 19:42:20 +0800 | [diff] [blame] | 9066 | if (test_bit(BTRFS_FS_STATE_ERROR, &fs_info->fs_state)) |
Miao Xie | eb73c1b | 2013-05-15 07:48:22 +0000 | [diff] [blame] | 9067 | return -EROFS; |
| 9068 | |
| 9069 | INIT_LIST_HEAD(&splice); |
| 9070 | |
Miao Xie | 573bfb7 | 2014-03-06 13:55:03 +0800 | [diff] [blame] | 9071 | mutex_lock(&fs_info->delalloc_root_mutex); |
Miao Xie | eb73c1b | 2013-05-15 07:48:22 +0000 | [diff] [blame] | 9072 | spin_lock(&fs_info->delalloc_root_lock); |
| 9073 | list_splice_init(&fs_info->delalloc_roots, &splice); |
Miao Xie | 6c255e6 | 2014-03-06 13:55:01 +0800 | [diff] [blame] | 9074 | while (!list_empty(&splice) && nr) { |
Miao Xie | eb73c1b | 2013-05-15 07:48:22 +0000 | [diff] [blame] | 9075 | root = list_first_entry(&splice, struct btrfs_root, |
| 9076 | delalloc_root); |
| 9077 | root = btrfs_grab_fs_root(root); |
| 9078 | BUG_ON(!root); |
| 9079 | list_move_tail(&root->delalloc_root, |
| 9080 | &fs_info->delalloc_roots); |
| 9081 | spin_unlock(&fs_info->delalloc_root_lock); |
| 9082 | |
Miao Xie | 6c255e6 | 2014-03-06 13:55:01 +0800 | [diff] [blame] | 9083 | ret = __start_delalloc_inodes(root, delay_iput, nr); |
Miao Xie | eb73c1b | 2013-05-15 07:48:22 +0000 | [diff] [blame] | 9084 | btrfs_put_fs_root(root); |
Miao Xie | 6c255e6 | 2014-03-06 13:55:01 +0800 | [diff] [blame] | 9085 | if (ret < 0) |
Miao Xie | eb73c1b | 2013-05-15 07:48:22 +0000 | [diff] [blame] | 9086 | goto out; |
| 9087 | |
Miao Xie | 6c255e6 | 2014-03-06 13:55:01 +0800 | [diff] [blame] | 9088 | if (nr != -1) { |
| 9089 | nr -= ret; |
| 9090 | WARN_ON(nr < 0); |
| 9091 | } |
Miao Xie | eb73c1b | 2013-05-15 07:48:22 +0000 | [diff] [blame] | 9092 | spin_lock(&fs_info->delalloc_root_lock); |
| 9093 | } |
| 9094 | spin_unlock(&fs_info->delalloc_root_lock); |
| 9095 | |
Miao Xie | 6c255e6 | 2014-03-06 13:55:01 +0800 | [diff] [blame] | 9096 | ret = 0; |
Miao Xie | eb73c1b | 2013-05-15 07:48:22 +0000 | [diff] [blame] | 9097 | atomic_inc(&fs_info->async_submit_draining); |
| 9098 | while (atomic_read(&fs_info->nr_async_submits) || |
| 9099 | atomic_read(&fs_info->async_delalloc_pages)) { |
| 9100 | wait_event(fs_info->async_submit_wait, |
| 9101 | (atomic_read(&fs_info->nr_async_submits) == 0 && |
| 9102 | atomic_read(&fs_info->async_delalloc_pages) == 0)); |
| 9103 | } |
| 9104 | atomic_dec(&fs_info->async_submit_draining); |
Miao Xie | 8ccf6f19 | 2012-10-25 09:28:04 +0000 | [diff] [blame] | 9105 | out: |
Miao Xie | 1eafa6c | 2013-01-22 10:49:00 +0000 | [diff] [blame] | 9106 | if (!list_empty_careful(&splice)) { |
Miao Xie | eb73c1b | 2013-05-15 07:48:22 +0000 | [diff] [blame] | 9107 | spin_lock(&fs_info->delalloc_root_lock); |
| 9108 | list_splice_tail(&splice, &fs_info->delalloc_roots); |
| 9109 | spin_unlock(&fs_info->delalloc_root_lock); |
Miao Xie | 1eafa6c | 2013-01-22 10:49:00 +0000 | [diff] [blame] | 9110 | } |
Miao Xie | 573bfb7 | 2014-03-06 13:55:03 +0800 | [diff] [blame] | 9111 | mutex_unlock(&fs_info->delalloc_root_mutex); |
Miao Xie | 8ccf6f19 | 2012-10-25 09:28:04 +0000 | [diff] [blame] | 9112 | return ret; |
Chris Mason | ea8c281 | 2008-08-04 23:17:27 -0400 | [diff] [blame] | 9113 | } |
| 9114 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 9115 | static int btrfs_symlink(struct inode *dir, struct dentry *dentry, |
| 9116 | const char *symname) |
| 9117 | { |
| 9118 | struct btrfs_trans_handle *trans; |
| 9119 | struct btrfs_root *root = BTRFS_I(dir)->root; |
| 9120 | struct btrfs_path *path; |
| 9121 | struct btrfs_key key; |
Chris Mason | 1832a6d | 2007-12-21 16:27:21 -0500 | [diff] [blame] | 9122 | struct inode *inode = NULL; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 9123 | int err; |
| 9124 | int drop_inode = 0; |
| 9125 | u64 objectid; |
Dulshani Gunawardhana | 6787125 | 2013-10-31 10:33:04 +0530 | [diff] [blame] | 9126 | u64 index = 0; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 9127 | int name_len; |
| 9128 | int datasize; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 9129 | unsigned long ptr; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 9130 | struct btrfs_file_extent_item *ei; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 9131 | struct extent_buffer *leaf; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 9132 | |
Filipe David Borba Manana | f06becc | 2013-09-16 09:53:28 +0100 | [diff] [blame] | 9133 | name_len = strlen(symname); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 9134 | if (name_len > BTRFS_MAX_INLINE_DATA_SIZE(root)) |
| 9135 | return -ENAMETOOLONG; |
Chris Mason | 1832a6d | 2007-12-21 16:27:21 -0500 | [diff] [blame] | 9136 | |
Josef Bacik | 9ed74f2 | 2009-09-11 16:12:44 -0400 | [diff] [blame] | 9137 | /* |
| 9138 | * 2 items for inode item and ref |
| 9139 | * 2 items for dir items |
| 9140 | * 1 item for xattr if selinux is on |
| 9141 | */ |
Yan, Zheng | a22285a | 2010-05-16 10:48:46 -0400 | [diff] [blame] | 9142 | trans = btrfs_start_transaction(root, 5); |
| 9143 | if (IS_ERR(trans)) |
| 9144 | return PTR_ERR(trans); |
Chris Mason | 1832a6d | 2007-12-21 16:27:21 -0500 | [diff] [blame] | 9145 | |
Li Zefan | 581bb05 | 2011-04-20 10:06:11 +0800 | [diff] [blame] | 9146 | err = btrfs_find_free_ino(root, &objectid); |
| 9147 | if (err) |
| 9148 | goto out_unlock; |
| 9149 | |
Josef Bacik | aec7477 | 2008-07-24 12:12:38 -0400 | [diff] [blame] | 9150 | inode = btrfs_new_inode(trans, root, dir, dentry->d_name.name, |
Li Zefan | 33345d01 | 2011-04-20 10:31:50 +0800 | [diff] [blame] | 9151 | dentry->d_name.len, btrfs_ino(dir), objectid, |
Josef Bacik | d82a6f1d | 2011-05-11 15:26:06 -0400 | [diff] [blame] | 9152 | S_IFLNK|S_IRWXUGO, &index); |
Tsutomu Itoh | 7cf96da | 2011-04-25 19:43:53 -0400 | [diff] [blame] | 9153 | if (IS_ERR(inode)) { |
| 9154 | err = PTR_ERR(inode); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 9155 | goto out_unlock; |
Tsutomu Itoh | 7cf96da | 2011-04-25 19:43:53 -0400 | [diff] [blame] | 9156 | } |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 9157 | |
Casey Schaufler | ad19db7 | 2011-12-15 10:09:07 -0500 | [diff] [blame] | 9158 | /* |
| 9159 | * If the active LSM wants to access the inode during |
| 9160 | * d_instantiate it needs these. Smack checks to see |
| 9161 | * if the filesystem supports xattrs by looking at the |
| 9162 | * ops vector. |
| 9163 | */ |
| 9164 | inode->i_fop = &btrfs_file_operations; |
| 9165 | inode->i_op = &btrfs_file_inode_operations; |
Chris Mason | b0d5d10 | 2014-09-08 13:08:51 -0700 | [diff] [blame] | 9166 | inode->i_mapping->a_ops = &btrfs_aops; |
| 9167 | inode->i_mapping->backing_dev_info = &root->fs_info->bdi; |
| 9168 | BTRFS_I(inode)->io_tree.ops = &btrfs_extent_io_ops; |
| 9169 | |
| 9170 | err = btrfs_init_inode_security(trans, inode, dir, &dentry->d_name); |
| 9171 | if (err) |
| 9172 | goto out_unlock_inode; |
Casey Schaufler | ad19db7 | 2011-12-15 10:09:07 -0500 | [diff] [blame] | 9173 | |
Josef Bacik | a1b075d | 2010-11-19 20:36:11 +0000 | [diff] [blame] | 9174 | err = btrfs_add_nondir(trans, dir, dentry, inode, 0, index); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 9175 | if (err) |
Chris Mason | b0d5d10 | 2014-09-08 13:08:51 -0700 | [diff] [blame] | 9176 | goto out_unlock_inode; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 9177 | |
| 9178 | path = btrfs_alloc_path(); |
Mark Fasheh | d8926bb | 2011-07-13 10:38:47 -0700 | [diff] [blame] | 9179 | if (!path) { |
| 9180 | err = -ENOMEM; |
Chris Mason | b0d5d10 | 2014-09-08 13:08:51 -0700 | [diff] [blame] | 9181 | goto out_unlock_inode; |
Mark Fasheh | d8926bb | 2011-07-13 10:38:47 -0700 | [diff] [blame] | 9182 | } |
Li Zefan | 33345d01 | 2011-04-20 10:31:50 +0800 | [diff] [blame] | 9183 | key.objectid = btrfs_ino(inode); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 9184 | key.offset = 0; |
David Sterba | 962a298 | 2014-06-04 18:41:45 +0200 | [diff] [blame] | 9185 | key.type = BTRFS_EXTENT_DATA_KEY; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 9186 | datasize = btrfs_file_extent_calc_inline_size(name_len); |
| 9187 | err = btrfs_insert_empty_item(trans, root, path, &key, |
| 9188 | datasize); |
Chris Mason | 54aa1f4 | 2007-06-22 14:16:25 -0400 | [diff] [blame] | 9189 | if (err) { |
Julia Lawall | b083916 | 2011-05-14 07:10:51 +0000 | [diff] [blame] | 9190 | btrfs_free_path(path); |
Chris Mason | b0d5d10 | 2014-09-08 13:08:51 -0700 | [diff] [blame] | 9191 | goto out_unlock_inode; |
Chris Mason | 54aa1f4 | 2007-06-22 14:16:25 -0400 | [diff] [blame] | 9192 | } |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 9193 | leaf = path->nodes[0]; |
| 9194 | ei = btrfs_item_ptr(leaf, path->slots[0], |
| 9195 | struct btrfs_file_extent_item); |
| 9196 | btrfs_set_file_extent_generation(leaf, ei, trans->transid); |
| 9197 | btrfs_set_file_extent_type(leaf, ei, |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 9198 | BTRFS_FILE_EXTENT_INLINE); |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 9199 | btrfs_set_file_extent_encryption(leaf, ei, 0); |
| 9200 | btrfs_set_file_extent_compression(leaf, ei, 0); |
| 9201 | btrfs_set_file_extent_other_encoding(leaf, ei, 0); |
| 9202 | btrfs_set_file_extent_ram_bytes(leaf, ei, name_len); |
| 9203 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 9204 | ptr = btrfs_file_extent_inline_start(ei); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 9205 | write_extent_buffer(leaf, symname, ptr, name_len); |
| 9206 | btrfs_mark_buffer_dirty(leaf); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 9207 | btrfs_free_path(path); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 9208 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 9209 | inode->i_op = &btrfs_symlink_inode_operations; |
| 9210 | inode->i_mapping->a_ops = &btrfs_symlink_aops; |
Chris Mason | 0416008 | 2008-03-26 10:28:07 -0400 | [diff] [blame] | 9211 | inode->i_mapping->backing_dev_info = &root->fs_info->bdi; |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 9212 | inode_set_bytes(inode, name_len); |
Filipe David Borba Manana | f06becc | 2013-09-16 09:53:28 +0100 | [diff] [blame] | 9213 | btrfs_i_size_write(inode, name_len); |
Chris Mason | 54aa1f4 | 2007-06-22 14:16:25 -0400 | [diff] [blame] | 9214 | err = btrfs_update_inode(trans, root, inode); |
Chris Mason | b0d5d10 | 2014-09-08 13:08:51 -0700 | [diff] [blame] | 9215 | if (err) { |
Chris Mason | 54aa1f4 | 2007-06-22 14:16:25 -0400 | [diff] [blame] | 9216 | drop_inode = 1; |
Chris Mason | b0d5d10 | 2014-09-08 13:08:51 -0700 | [diff] [blame] | 9217 | goto out_unlock_inode; |
| 9218 | } |
| 9219 | |
| 9220 | unlock_new_inode(inode); |
| 9221 | d_instantiate(dentry, inode); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 9222 | |
| 9223 | out_unlock: |
Josef Bacik | 7ad85bb | 2012-01-12 19:10:12 -0500 | [diff] [blame] | 9224 | btrfs_end_transaction(trans, root); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 9225 | if (drop_inode) { |
| 9226 | inode_dec_link_count(inode); |
| 9227 | iput(inode); |
| 9228 | } |
Liu Bo | b53d3f5 | 2012-11-14 14:34:34 +0000 | [diff] [blame] | 9229 | btrfs_btree_balance_dirty(root); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 9230 | return err; |
Chris Mason | b0d5d10 | 2014-09-08 13:08:51 -0700 | [diff] [blame] | 9231 | |
| 9232 | out_unlock_inode: |
| 9233 | drop_inode = 1; |
| 9234 | unlock_new_inode(inode); |
| 9235 | goto out_unlock; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 9236 | } |
Chris Mason | 1643298 | 2008-04-10 10:23:21 -0400 | [diff] [blame] | 9237 | |
Josef Bacik | 0af3d00 | 2010-06-21 14:48:16 -0400 | [diff] [blame] | 9238 | static int __btrfs_prealloc_file_range(struct inode *inode, int mode, |
| 9239 | u64 start, u64 num_bytes, u64 min_size, |
| 9240 | loff_t actual_len, u64 *alloc_hint, |
| 9241 | struct btrfs_trans_handle *trans) |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 9242 | { |
Josef Bacik | 5dc562c | 2012-08-17 13:14:17 -0400 | [diff] [blame] | 9243 | struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree; |
| 9244 | struct extent_map *em; |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 9245 | struct btrfs_root *root = BTRFS_I(inode)->root; |
| 9246 | struct btrfs_key ins; |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 9247 | u64 cur_offset = start; |
Josef Bacik | 55a61d1 | 2010-11-22 18:50:32 +0000 | [diff] [blame] | 9248 | u64 i_size; |
Chris Mason | 154ea28 | 2013-03-05 11:11:26 -0500 | [diff] [blame] | 9249 | u64 cur_bytes; |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 9250 | int ret = 0; |
Josef Bacik | 0af3d00 | 2010-06-21 14:48:16 -0400 | [diff] [blame] | 9251 | bool own_trans = true; |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 9252 | |
Josef Bacik | 0af3d00 | 2010-06-21 14:48:16 -0400 | [diff] [blame] | 9253 | if (trans) |
| 9254 | own_trans = false; |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 9255 | while (num_bytes > 0) { |
Josef Bacik | 0af3d00 | 2010-06-21 14:48:16 -0400 | [diff] [blame] | 9256 | if (own_trans) { |
| 9257 | trans = btrfs_start_transaction(root, 3); |
| 9258 | if (IS_ERR(trans)) { |
| 9259 | ret = PTR_ERR(trans); |
| 9260 | break; |
| 9261 | } |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 9262 | } |
Yan, Zheng | 5a303d5 | 2009-11-12 09:34:52 +0000 | [diff] [blame] | 9263 | |
Chris Mason | 154ea28 | 2013-03-05 11:11:26 -0500 | [diff] [blame] | 9264 | cur_bytes = min(num_bytes, 256ULL * 1024 * 1024); |
| 9265 | cur_bytes = max(cur_bytes, min_size); |
Josef Bacik | 0036158 | 2013-08-14 14:02:47 -0400 | [diff] [blame] | 9266 | ret = btrfs_reserve_extent(root, cur_bytes, min_size, 0, |
Miao Xie | e570fd2 | 2014-06-19 10:42:50 +0800 | [diff] [blame] | 9267 | *alloc_hint, &ins, 1, 0); |
Yan, Zheng | 5a303d5 | 2009-11-12 09:34:52 +0000 | [diff] [blame] | 9268 | if (ret) { |
Josef Bacik | 0af3d00 | 2010-06-21 14:48:16 -0400 | [diff] [blame] | 9269 | if (own_trans) |
| 9270 | btrfs_end_transaction(trans, root); |
Yan, Zheng | a22285a | 2010-05-16 10:48:46 -0400 | [diff] [blame] | 9271 | break; |
Yan, Zheng | 5a303d5 | 2009-11-12 09:34:52 +0000 | [diff] [blame] | 9272 | } |
| 9273 | |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 9274 | ret = insert_reserved_file_extent(trans, inode, |
| 9275 | cur_offset, ins.objectid, |
| 9276 | ins.offset, ins.offset, |
Yan, Zheng | 920bbbf | 2009-11-12 09:34:08 +0000 | [diff] [blame] | 9277 | ins.offset, 0, 0, 0, |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 9278 | BTRFS_FILE_EXTENT_PREALLOC); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 9279 | if (ret) { |
Josef Bacik | 857cc2f | 2013-10-07 15:21:08 -0400 | [diff] [blame] | 9280 | btrfs_free_reserved_extent(root, ins.objectid, |
Miao Xie | e570fd2 | 2014-06-19 10:42:50 +0800 | [diff] [blame] | 9281 | ins.offset, 0); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 9282 | btrfs_abort_transaction(trans, root, ret); |
| 9283 | if (own_trans) |
| 9284 | btrfs_end_transaction(trans, root); |
| 9285 | break; |
| 9286 | } |
Chris Mason | a1ed835 | 2009-09-11 12:27:37 -0400 | [diff] [blame] | 9287 | btrfs_drop_extent_cache(inode, cur_offset, |
| 9288 | cur_offset + ins.offset -1, 0); |
Yan, Zheng | 5a303d5 | 2009-11-12 09:34:52 +0000 | [diff] [blame] | 9289 | |
Josef Bacik | 5dc562c | 2012-08-17 13:14:17 -0400 | [diff] [blame] | 9290 | em = alloc_extent_map(); |
| 9291 | if (!em) { |
| 9292 | set_bit(BTRFS_INODE_NEEDS_FULL_SYNC, |
| 9293 | &BTRFS_I(inode)->runtime_flags); |
| 9294 | goto next; |
| 9295 | } |
| 9296 | |
| 9297 | em->start = cur_offset; |
| 9298 | em->orig_start = cur_offset; |
| 9299 | em->len = ins.offset; |
| 9300 | em->block_start = ins.objectid; |
| 9301 | em->block_len = ins.offset; |
Josef Bacik | b493968 | 2012-12-03 10:31:19 -0500 | [diff] [blame] | 9302 | em->orig_block_len = ins.offset; |
Josef Bacik | cc95bef | 2013-04-04 14:31:27 -0400 | [diff] [blame] | 9303 | em->ram_bytes = ins.offset; |
Josef Bacik | 5dc562c | 2012-08-17 13:14:17 -0400 | [diff] [blame] | 9304 | em->bdev = root->fs_info->fs_devices->latest_bdev; |
| 9305 | set_bit(EXTENT_FLAG_PREALLOC, &em->flags); |
| 9306 | em->generation = trans->transid; |
| 9307 | |
| 9308 | while (1) { |
| 9309 | write_lock(&em_tree->lock); |
Josef Bacik | 09a2a8f9 | 2013-04-05 16:51:15 -0400 | [diff] [blame] | 9310 | ret = add_extent_mapping(em_tree, em, 1); |
Josef Bacik | 5dc562c | 2012-08-17 13:14:17 -0400 | [diff] [blame] | 9311 | write_unlock(&em_tree->lock); |
| 9312 | if (ret != -EEXIST) |
| 9313 | break; |
| 9314 | btrfs_drop_extent_cache(inode, cur_offset, |
| 9315 | cur_offset + ins.offset - 1, |
| 9316 | 0); |
| 9317 | } |
| 9318 | free_extent_map(em); |
| 9319 | next: |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 9320 | num_bytes -= ins.offset; |
| 9321 | cur_offset += ins.offset; |
Yan, Zheng | efa5646 | 2010-05-16 10:49:59 -0400 | [diff] [blame] | 9322 | *alloc_hint = ins.objectid + ins.offset; |
Yan, Zheng | 5a303d5 | 2009-11-12 09:34:52 +0000 | [diff] [blame] | 9323 | |
Josef Bacik | 0c4d2d9 | 2012-04-05 15:03:02 -0400 | [diff] [blame] | 9324 | inode_inc_iversion(inode); |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 9325 | inode->i_ctime = CURRENT_TIME; |
Christoph Hellwig | 6cbff00 | 2009-04-17 10:37:41 +0200 | [diff] [blame] | 9326 | BTRFS_I(inode)->flags |= BTRFS_INODE_PREALLOC; |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 9327 | if (!(mode & FALLOC_FL_KEEP_SIZE) && |
Yan, Zheng | efa5646 | 2010-05-16 10:49:59 -0400 | [diff] [blame] | 9328 | (actual_len > inode->i_size) && |
| 9329 | (cur_offset > inode->i_size)) { |
Aneesh Kumar K.V | d1ea6a6 | 2010-01-20 07:28:54 +0000 | [diff] [blame] | 9330 | if (cur_offset > actual_len) |
Josef Bacik | 55a61d1 | 2010-11-22 18:50:32 +0000 | [diff] [blame] | 9331 | i_size = actual_len; |
Aneesh Kumar K.V | d1ea6a6 | 2010-01-20 07:28:54 +0000 | [diff] [blame] | 9332 | else |
Josef Bacik | 55a61d1 | 2010-11-22 18:50:32 +0000 | [diff] [blame] | 9333 | i_size = cur_offset; |
| 9334 | i_size_write(inode, i_size); |
| 9335 | btrfs_ordered_update_i_size(inode, i_size, NULL); |
Yan, Zheng | 5a303d5 | 2009-11-12 09:34:52 +0000 | [diff] [blame] | 9336 | } |
| 9337 | |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 9338 | ret = btrfs_update_inode(trans, root, inode); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 9339 | |
| 9340 | if (ret) { |
| 9341 | btrfs_abort_transaction(trans, root, ret); |
| 9342 | if (own_trans) |
| 9343 | btrfs_end_transaction(trans, root); |
| 9344 | break; |
| 9345 | } |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 9346 | |
Josef Bacik | 0af3d00 | 2010-06-21 14:48:16 -0400 | [diff] [blame] | 9347 | if (own_trans) |
| 9348 | btrfs_end_transaction(trans, root); |
Yan, Zheng | 5a303d5 | 2009-11-12 09:34:52 +0000 | [diff] [blame] | 9349 | } |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 9350 | return ret; |
| 9351 | } |
| 9352 | |
Josef Bacik | 0af3d00 | 2010-06-21 14:48:16 -0400 | [diff] [blame] | 9353 | int btrfs_prealloc_file_range(struct inode *inode, int mode, |
| 9354 | u64 start, u64 num_bytes, u64 min_size, |
| 9355 | loff_t actual_len, u64 *alloc_hint) |
| 9356 | { |
| 9357 | return __btrfs_prealloc_file_range(inode, mode, start, num_bytes, |
| 9358 | min_size, actual_len, alloc_hint, |
| 9359 | NULL); |
| 9360 | } |
| 9361 | |
| 9362 | int btrfs_prealloc_file_range_trans(struct inode *inode, |
| 9363 | struct btrfs_trans_handle *trans, int mode, |
| 9364 | u64 start, u64 num_bytes, u64 min_size, |
| 9365 | loff_t actual_len, u64 *alloc_hint) |
| 9366 | { |
| 9367 | return __btrfs_prealloc_file_range(inode, mode, start, num_bytes, |
| 9368 | min_size, actual_len, alloc_hint, trans); |
| 9369 | } |
| 9370 | |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 9371 | static int btrfs_set_page_dirty(struct page *page) |
| 9372 | { |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 9373 | return __set_page_dirty_nobuffers(page); |
| 9374 | } |
| 9375 | |
Al Viro | 10556cb | 2011-06-20 19:28:19 -0400 | [diff] [blame] | 9376 | static int btrfs_permission(struct inode *inode, int mask) |
Yan | fdebe2b | 2008-01-14 13:26:08 -0500 | [diff] [blame] | 9377 | { |
Li Zefan | b83cc96 | 2010-12-20 16:04:08 +0800 | [diff] [blame] | 9378 | struct btrfs_root *root = BTRFS_I(inode)->root; |
Jeff Mahoney | cb6db4e | 2011-08-15 17:27:21 +0000 | [diff] [blame] | 9379 | umode_t mode = inode->i_mode; |
Li Zefan | b83cc96 | 2010-12-20 16:04:08 +0800 | [diff] [blame] | 9380 | |
Jeff Mahoney | cb6db4e | 2011-08-15 17:27:21 +0000 | [diff] [blame] | 9381 | if (mask & MAY_WRITE && |
| 9382 | (S_ISREG(mode) || S_ISDIR(mode) || S_ISLNK(mode))) { |
| 9383 | if (btrfs_root_readonly(root)) |
| 9384 | return -EROFS; |
| 9385 | if (BTRFS_I(inode)->flags & BTRFS_INODE_READONLY) |
| 9386 | return -EACCES; |
| 9387 | } |
Al Viro | 2830ba7 | 2011-06-20 19:16:29 -0400 | [diff] [blame] | 9388 | return generic_permission(inode, mask); |
Yan | fdebe2b | 2008-01-14 13:26:08 -0500 | [diff] [blame] | 9389 | } |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 9390 | |
Filipe Manana | ef3b9af | 2014-04-27 20:40:45 +0100 | [diff] [blame] | 9391 | static int btrfs_tmpfile(struct inode *dir, struct dentry *dentry, umode_t mode) |
| 9392 | { |
| 9393 | struct btrfs_trans_handle *trans; |
| 9394 | struct btrfs_root *root = BTRFS_I(dir)->root; |
| 9395 | struct inode *inode = NULL; |
| 9396 | u64 objectid; |
| 9397 | u64 index; |
| 9398 | int ret = 0; |
| 9399 | |
| 9400 | /* |
| 9401 | * 5 units required for adding orphan entry |
| 9402 | */ |
| 9403 | trans = btrfs_start_transaction(root, 5); |
| 9404 | if (IS_ERR(trans)) |
| 9405 | return PTR_ERR(trans); |
| 9406 | |
| 9407 | ret = btrfs_find_free_ino(root, &objectid); |
| 9408 | if (ret) |
| 9409 | goto out; |
| 9410 | |
| 9411 | inode = btrfs_new_inode(trans, root, dir, NULL, 0, |
| 9412 | btrfs_ino(dir), objectid, mode, &index); |
| 9413 | if (IS_ERR(inode)) { |
| 9414 | ret = PTR_ERR(inode); |
| 9415 | inode = NULL; |
| 9416 | goto out; |
| 9417 | } |
| 9418 | |
Filipe Manana | ef3b9af | 2014-04-27 20:40:45 +0100 | [diff] [blame] | 9419 | inode->i_fop = &btrfs_file_operations; |
| 9420 | inode->i_op = &btrfs_file_inode_operations; |
| 9421 | |
| 9422 | inode->i_mapping->a_ops = &btrfs_aops; |
| 9423 | inode->i_mapping->backing_dev_info = &root->fs_info->bdi; |
| 9424 | BTRFS_I(inode)->io_tree.ops = &btrfs_extent_io_ops; |
| 9425 | |
Chris Mason | b0d5d10 | 2014-09-08 13:08:51 -0700 | [diff] [blame] | 9426 | ret = btrfs_init_inode_security(trans, inode, dir, NULL); |
| 9427 | if (ret) |
| 9428 | goto out_inode; |
| 9429 | |
| 9430 | ret = btrfs_update_inode(trans, root, inode); |
| 9431 | if (ret) |
| 9432 | goto out_inode; |
Filipe Manana | ef3b9af | 2014-04-27 20:40:45 +0100 | [diff] [blame] | 9433 | ret = btrfs_orphan_add(trans, inode); |
| 9434 | if (ret) |
Chris Mason | b0d5d10 | 2014-09-08 13:08:51 -0700 | [diff] [blame] | 9435 | goto out_inode; |
Filipe Manana | ef3b9af | 2014-04-27 20:40:45 +0100 | [diff] [blame] | 9436 | |
Filipe Manana | 5762b5c | 2014-08-01 00:10:32 +0100 | [diff] [blame] | 9437 | /* |
| 9438 | * We set number of links to 0 in btrfs_new_inode(), and here we set |
| 9439 | * it to 1 because d_tmpfile() will issue a warning if the count is 0, |
| 9440 | * through: |
| 9441 | * |
| 9442 | * d_tmpfile() -> inode_dec_link_count() -> drop_nlink() |
| 9443 | */ |
| 9444 | set_nlink(inode, 1); |
Chris Mason | b0d5d10 | 2014-09-08 13:08:51 -0700 | [diff] [blame] | 9445 | unlock_new_inode(inode); |
Filipe Manana | ef3b9af | 2014-04-27 20:40:45 +0100 | [diff] [blame] | 9446 | d_tmpfile(dentry, inode); |
| 9447 | mark_inode_dirty(inode); |
| 9448 | |
| 9449 | out: |
| 9450 | btrfs_end_transaction(trans, root); |
| 9451 | if (ret) |
| 9452 | iput(inode); |
| 9453 | btrfs_balance_delayed_items(root); |
| 9454 | btrfs_btree_balance_dirty(root); |
Filipe Manana | ef3b9af | 2014-04-27 20:40:45 +0100 | [diff] [blame] | 9455 | return ret; |
Chris Mason | b0d5d10 | 2014-09-08 13:08:51 -0700 | [diff] [blame] | 9456 | |
| 9457 | out_inode: |
| 9458 | unlock_new_inode(inode); |
| 9459 | goto out; |
| 9460 | |
Filipe Manana | ef3b9af | 2014-04-27 20:40:45 +0100 | [diff] [blame] | 9461 | } |
| 9462 | |
Alexey Dobriyan | 6e1d5dc | 2009-09-21 17:01:11 -0700 | [diff] [blame] | 9463 | static const struct inode_operations btrfs_dir_inode_operations = { |
Chris Mason | 3394e16 | 2008-11-17 20:42:26 -0500 | [diff] [blame] | 9464 | .getattr = btrfs_getattr, |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 9465 | .lookup = btrfs_lookup, |
| 9466 | .create = btrfs_create, |
| 9467 | .unlink = btrfs_unlink, |
| 9468 | .link = btrfs_link, |
| 9469 | .mkdir = btrfs_mkdir, |
| 9470 | .rmdir = btrfs_rmdir, |
Miklos Szeredi | 80ace85 | 2014-07-23 15:15:32 +0200 | [diff] [blame] | 9471 | .rename2 = btrfs_rename2, |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 9472 | .symlink = btrfs_symlink, |
| 9473 | .setattr = btrfs_setattr, |
Josef Bacik | 618e21d | 2007-07-11 10:18:17 -0400 | [diff] [blame] | 9474 | .mknod = btrfs_mknod, |
Christoph Hellwig | 95819c0 | 2008-08-28 06:21:17 -0400 | [diff] [blame] | 9475 | .setxattr = btrfs_setxattr, |
| 9476 | .getxattr = btrfs_getxattr, |
Josef Bacik | 5103e94 | 2007-11-16 11:45:54 -0500 | [diff] [blame] | 9477 | .listxattr = btrfs_listxattr, |
Christoph Hellwig | 95819c0 | 2008-08-28 06:21:17 -0400 | [diff] [blame] | 9478 | .removexattr = btrfs_removexattr, |
Yan | fdebe2b | 2008-01-14 13:26:08 -0500 | [diff] [blame] | 9479 | .permission = btrfs_permission, |
Christoph Hellwig | 4e34e71 | 2011-07-23 17:37:31 +0200 | [diff] [blame] | 9480 | .get_acl = btrfs_get_acl, |
Christoph Hellwig | 996a710 | 2013-12-20 05:16:43 -0800 | [diff] [blame] | 9481 | .set_acl = btrfs_set_acl, |
Guangyu Sun | 93fd63c | 2013-09-16 10:42:03 -0700 | [diff] [blame] | 9482 | .update_time = btrfs_update_time, |
Filipe Manana | ef3b9af | 2014-04-27 20:40:45 +0100 | [diff] [blame] | 9483 | .tmpfile = btrfs_tmpfile, |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 9484 | }; |
Alexey Dobriyan | 6e1d5dc | 2009-09-21 17:01:11 -0700 | [diff] [blame] | 9485 | static const struct inode_operations btrfs_dir_ro_inode_operations = { |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 9486 | .lookup = btrfs_lookup, |
Yan | fdebe2b | 2008-01-14 13:26:08 -0500 | [diff] [blame] | 9487 | .permission = btrfs_permission, |
Christoph Hellwig | 4e34e71 | 2011-07-23 17:37:31 +0200 | [diff] [blame] | 9488 | .get_acl = btrfs_get_acl, |
Christoph Hellwig | 996a710 | 2013-12-20 05:16:43 -0800 | [diff] [blame] | 9489 | .set_acl = btrfs_set_acl, |
Guangyu Sun | 93fd63c | 2013-09-16 10:42:03 -0700 | [diff] [blame] | 9490 | .update_time = btrfs_update_time, |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 9491 | }; |
Yan, Zheng | 76dda93 | 2009-09-21 16:00:26 -0400 | [diff] [blame] | 9492 | |
Alexey Dobriyan | 828c095 | 2009-10-01 15:43:56 -0700 | [diff] [blame] | 9493 | static const struct file_operations btrfs_dir_file_operations = { |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 9494 | .llseek = generic_file_llseek, |
| 9495 | .read = generic_read_dir, |
Al Viro | 9cdda8d | 2013-05-22 16:48:09 -0400 | [diff] [blame] | 9496 | .iterate = btrfs_real_readdir, |
Christoph Hellwig | 34287aa | 2007-09-14 10:22:47 -0400 | [diff] [blame] | 9497 | .unlocked_ioctl = btrfs_ioctl, |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 9498 | #ifdef CONFIG_COMPAT |
Christoph Hellwig | 34287aa | 2007-09-14 10:22:47 -0400 | [diff] [blame] | 9499 | .compat_ioctl = btrfs_ioctl, |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 9500 | #endif |
Sage Weil | 6bf13c0 | 2008-06-10 10:07:39 -0400 | [diff] [blame] | 9501 | .release = btrfs_release_file, |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 9502 | .fsync = btrfs_sync_file, |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 9503 | }; |
| 9504 | |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 9505 | static struct extent_io_ops btrfs_extent_io_ops = { |
Chris Mason | 07157aa | 2007-08-30 08:50:51 -0400 | [diff] [blame] | 9506 | .fill_delalloc = run_delalloc_range, |
Chris Mason | 065631f | 2008-02-20 12:07:25 -0500 | [diff] [blame] | 9507 | .submit_bio_hook = btrfs_submit_bio_hook, |
Chris Mason | 239b14b | 2008-03-24 15:02:07 -0400 | [diff] [blame] | 9508 | .merge_bio_hook = btrfs_merge_bio_hook, |
Chris Mason | 07157aa | 2007-08-30 08:50:51 -0400 | [diff] [blame] | 9509 | .readpage_end_io_hook = btrfs_readpage_end_io_hook, |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 9510 | .writepage_end_io_hook = btrfs_writepage_end_io_hook, |
Chris Mason | 247e743 | 2008-07-17 12:53:51 -0400 | [diff] [blame] | 9511 | .writepage_start_hook = btrfs_writepage_start_hook, |
Chris Mason | b0c68f8 | 2008-01-31 11:05:37 -0500 | [diff] [blame] | 9512 | .set_bit_hook = btrfs_set_bit_hook, |
| 9513 | .clear_bit_hook = btrfs_clear_bit_hook, |
Josef Bacik | 9ed74f2 | 2009-09-11 16:12:44 -0400 | [diff] [blame] | 9514 | .merge_extent_hook = btrfs_merge_extent_hook, |
| 9515 | .split_extent_hook = btrfs_split_extent_hook, |
Chris Mason | 07157aa | 2007-08-30 08:50:51 -0400 | [diff] [blame] | 9516 | }; |
| 9517 | |
Chris Mason | 3505439 | 2009-01-21 13:11:13 -0500 | [diff] [blame] | 9518 | /* |
| 9519 | * btrfs doesn't support the bmap operation because swapfiles |
| 9520 | * use bmap to make a mapping of extents in the file. They assume |
| 9521 | * these extents won't change over the life of the file and they |
| 9522 | * use the bmap result to do IO directly to the drive. |
| 9523 | * |
| 9524 | * the btrfs bmap call would return logical addresses that aren't |
| 9525 | * suitable for IO and they also will change frequently as COW |
| 9526 | * operations happen. So, swapfile + btrfs == corruption. |
| 9527 | * |
| 9528 | * For now we're avoiding this by dropping bmap. |
| 9529 | */ |
Alexey Dobriyan | 7f09410 | 2009-09-21 17:01:10 -0700 | [diff] [blame] | 9530 | static const struct address_space_operations btrfs_aops = { |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 9531 | .readpage = btrfs_readpage, |
| 9532 | .writepage = btrfs_writepage, |
Chris Mason | b293f02e | 2007-11-01 19:45:34 -0400 | [diff] [blame] | 9533 | .writepages = btrfs_writepages, |
Chris Mason | 3ab2fb5 | 2007-11-08 10:59:22 -0500 | [diff] [blame] | 9534 | .readpages = btrfs_readpages, |
Chris Mason | 1643298 | 2008-04-10 10:23:21 -0400 | [diff] [blame] | 9535 | .direct_IO = btrfs_direct_IO, |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 9536 | .invalidatepage = btrfs_invalidatepage, |
| 9537 | .releasepage = btrfs_releasepage, |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 9538 | .set_page_dirty = btrfs_set_page_dirty, |
Andi Kleen | 465fdd9 | 2009-09-16 11:50:18 +0200 | [diff] [blame] | 9539 | .error_remove_page = generic_error_remove_page, |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 9540 | }; |
| 9541 | |
Alexey Dobriyan | 7f09410 | 2009-09-21 17:01:10 -0700 | [diff] [blame] | 9542 | static const struct address_space_operations btrfs_symlink_aops = { |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 9543 | .readpage = btrfs_readpage, |
| 9544 | .writepage = btrfs_writepage, |
Chris Mason | 2bf5a72 | 2007-08-30 11:54:02 -0400 | [diff] [blame] | 9545 | .invalidatepage = btrfs_invalidatepage, |
| 9546 | .releasepage = btrfs_releasepage, |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 9547 | }; |
| 9548 | |
Alexey Dobriyan | 6e1d5dc | 2009-09-21 17:01:11 -0700 | [diff] [blame] | 9549 | static const struct inode_operations btrfs_file_inode_operations = { |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 9550 | .getattr = btrfs_getattr, |
| 9551 | .setattr = btrfs_setattr, |
Christoph Hellwig | 95819c0 | 2008-08-28 06:21:17 -0400 | [diff] [blame] | 9552 | .setxattr = btrfs_setxattr, |
| 9553 | .getxattr = btrfs_getxattr, |
Josef Bacik | 5103e94 | 2007-11-16 11:45:54 -0500 | [diff] [blame] | 9554 | .listxattr = btrfs_listxattr, |
Christoph Hellwig | 95819c0 | 2008-08-28 06:21:17 -0400 | [diff] [blame] | 9555 | .removexattr = btrfs_removexattr, |
Yan | fdebe2b | 2008-01-14 13:26:08 -0500 | [diff] [blame] | 9556 | .permission = btrfs_permission, |
Yehuda Sadeh | 1506fcc | 2009-01-21 14:39:14 -0500 | [diff] [blame] | 9557 | .fiemap = btrfs_fiemap, |
Christoph Hellwig | 4e34e71 | 2011-07-23 17:37:31 +0200 | [diff] [blame] | 9558 | .get_acl = btrfs_get_acl, |
Christoph Hellwig | 996a710 | 2013-12-20 05:16:43 -0800 | [diff] [blame] | 9559 | .set_acl = btrfs_set_acl, |
Josef Bacik | e41f941 | 2012-03-26 09:46:47 -0400 | [diff] [blame] | 9560 | .update_time = btrfs_update_time, |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 9561 | }; |
Alexey Dobriyan | 6e1d5dc | 2009-09-21 17:01:11 -0700 | [diff] [blame] | 9562 | static const struct inode_operations btrfs_special_inode_operations = { |
Josef Bacik | 618e21d | 2007-07-11 10:18:17 -0400 | [diff] [blame] | 9563 | .getattr = btrfs_getattr, |
| 9564 | .setattr = btrfs_setattr, |
Yan | fdebe2b | 2008-01-14 13:26:08 -0500 | [diff] [blame] | 9565 | .permission = btrfs_permission, |
Christoph Hellwig | 95819c0 | 2008-08-28 06:21:17 -0400 | [diff] [blame] | 9566 | .setxattr = btrfs_setxattr, |
| 9567 | .getxattr = btrfs_getxattr, |
Josef Bacik | 33268ea | 2008-07-24 12:16:36 -0400 | [diff] [blame] | 9568 | .listxattr = btrfs_listxattr, |
Christoph Hellwig | 95819c0 | 2008-08-28 06:21:17 -0400 | [diff] [blame] | 9569 | .removexattr = btrfs_removexattr, |
Christoph Hellwig | 4e34e71 | 2011-07-23 17:37:31 +0200 | [diff] [blame] | 9570 | .get_acl = btrfs_get_acl, |
Christoph Hellwig | 996a710 | 2013-12-20 05:16:43 -0800 | [diff] [blame] | 9571 | .set_acl = btrfs_set_acl, |
Josef Bacik | e41f941 | 2012-03-26 09:46:47 -0400 | [diff] [blame] | 9572 | .update_time = btrfs_update_time, |
Josef Bacik | 618e21d | 2007-07-11 10:18:17 -0400 | [diff] [blame] | 9573 | }; |
Alexey Dobriyan | 6e1d5dc | 2009-09-21 17:01:11 -0700 | [diff] [blame] | 9574 | static const struct inode_operations btrfs_symlink_inode_operations = { |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 9575 | .readlink = generic_readlink, |
| 9576 | .follow_link = page_follow_link_light, |
| 9577 | .put_link = page_put_link, |
Li Zefan | f209561 | 2010-11-19 02:05:24 +0000 | [diff] [blame] | 9578 | .getattr = btrfs_getattr, |
Josef Bacik | 22c44fe | 2011-11-30 10:45:38 -0500 | [diff] [blame] | 9579 | .setattr = btrfs_setattr, |
Yan | fdebe2b | 2008-01-14 13:26:08 -0500 | [diff] [blame] | 9580 | .permission = btrfs_permission, |
Jim Owens | 0279b4c | 2009-02-04 09:29:13 -0500 | [diff] [blame] | 9581 | .setxattr = btrfs_setxattr, |
| 9582 | .getxattr = btrfs_getxattr, |
| 9583 | .listxattr = btrfs_listxattr, |
| 9584 | .removexattr = btrfs_removexattr, |
Josef Bacik | e41f941 | 2012-03-26 09:46:47 -0400 | [diff] [blame] | 9585 | .update_time = btrfs_update_time, |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 9586 | }; |
Yan, Zheng | 76dda93 | 2009-09-21 16:00:26 -0400 | [diff] [blame] | 9587 | |
Alexey Dobriyan | 82d339d | 2009-10-09 09:54:36 -0400 | [diff] [blame] | 9588 | const struct dentry_operations btrfs_dentry_operations = { |
Yan, Zheng | 76dda93 | 2009-09-21 16:00:26 -0400 | [diff] [blame] | 9589 | .d_delete = btrfs_dentry_delete, |
Josef Bacik | b4aff1f | 2011-06-28 16:18:59 -0400 | [diff] [blame] | 9590 | .d_release = btrfs_dentry_release, |
Yan, Zheng | 76dda93 | 2009-09-21 16:00:26 -0400 | [diff] [blame] | 9591 | }; |