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 | b888db2 | 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 | b888db2 | 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; |
Filipe Manana | e6eb431 | 2014-10-10 10:45:12 +0100 | [diff] [blame] | 530 | unsigned long page_error_op; |
| 531 | |
Josef Bacik | 151a41b | 2013-07-29 13:22:24 -0400 | [diff] [blame] | 532 | clear_flags |= (ret < 0) ? EXTENT_DO_ACCOUNTING : 0; |
Filipe Manana | e6eb431 | 2014-10-10 10:45:12 +0100 | [diff] [blame] | 533 | page_error_op = ret < 0 ? PAGE_SET_ERROR : 0; |
Josef Bacik | 151a41b | 2013-07-29 13:22:24 -0400 | [diff] [blame] | 534 | |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 535 | /* |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 536 | * inline extent creation worked or returned error, |
| 537 | * we don't need to create any more async work items. |
| 538 | * Unlock and free up our temp pages. |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 539 | */ |
Josef Bacik | c2790a2 | 2013-07-29 11:20:47 -0400 | [diff] [blame] | 540 | extent_clear_unlock_delalloc(inode, start, end, NULL, |
Josef Bacik | 151a41b | 2013-07-29 13:22:24 -0400 | [diff] [blame] | 541 | clear_flags, PAGE_UNLOCK | |
Josef Bacik | c2790a2 | 2013-07-29 11:20:47 -0400 | [diff] [blame] | 542 | PAGE_CLEAR_DIRTY | |
| 543 | PAGE_SET_WRITEBACK | |
Filipe Manana | e6eb431 | 2014-10-10 10:45:12 +0100 | [diff] [blame] | 544 | page_error_op | |
Josef Bacik | c2790a2 | 2013-07-29 11:20:47 -0400 | [diff] [blame] | 545 | PAGE_END_WRITEBACK); |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 546 | goto free_pages_out; |
| 547 | } |
| 548 | } |
| 549 | |
| 550 | if (will_compress) { |
| 551 | /* |
| 552 | * we aren't doing an inline extent round the compressed size |
| 553 | * up to a block size boundary so the allocator does sane |
| 554 | * things |
| 555 | */ |
Qu Wenruo | fda2832 | 2013-02-26 08:10:22 +0000 | [diff] [blame] | 556 | total_compressed = ALIGN(total_compressed, blocksize); |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 557 | |
| 558 | /* |
| 559 | * one last check to make sure the compression is really a |
| 560 | * win, compare the page count read with the blocks on disk |
| 561 | */ |
Qu Wenruo | fda2832 | 2013-02-26 08:10:22 +0000 | [diff] [blame] | 562 | total_in = ALIGN(total_in, PAGE_CACHE_SIZE); |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 563 | if (total_compressed >= total_in) { |
| 564 | will_compress = 0; |
| 565 | } else { |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 566 | num_bytes = total_in; |
| 567 | } |
| 568 | } |
| 569 | if (!will_compress && pages) { |
| 570 | /* |
| 571 | * the compression code ran but failed to make things smaller, |
| 572 | * free any pages it allocated and our page pointer array |
| 573 | */ |
| 574 | for (i = 0; i < nr_pages_ret; i++) { |
Chris Mason | 70b99e6 | 2008-10-31 12:46:39 -0400 | [diff] [blame] | 575 | WARN_ON(pages[i]->mapping); |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 576 | page_cache_release(pages[i]); |
| 577 | } |
| 578 | kfree(pages); |
| 579 | pages = NULL; |
| 580 | total_compressed = 0; |
| 581 | nr_pages_ret = 0; |
| 582 | |
| 583 | /* flag the file so we don't compress in the future */ |
Chris Mason | 1e701a3 | 2010-03-11 09:42:04 -0500 | [diff] [blame] | 584 | if (!btrfs_test_opt(root, FORCE_COMPRESS) && |
| 585 | !(BTRFS_I(inode)->force_compress)) { |
Chris Mason | a555f81 | 2010-01-28 16:18:15 -0500 | [diff] [blame] | 586 | BTRFS_I(inode)->flags |= BTRFS_INODE_NOCOMPRESS; |
Chris Mason | 1e701a3 | 2010-03-11 09:42:04 -0500 | [diff] [blame] | 587 | } |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 588 | } |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 589 | if (will_compress) { |
| 590 | *num_added += 1; |
| 591 | |
| 592 | /* the async work queues will take care of doing actual |
| 593 | * allocation on disk for these compressed pages, |
| 594 | * and will submit them to the elevator. |
| 595 | */ |
| 596 | add_async_extent(async_cow, start, num_bytes, |
Li Zefan | 261507a0 | 2010-12-17 14:21:50 +0800 | [diff] [blame] | 597 | total_compressed, pages, nr_pages_ret, |
| 598 | compress_type); |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 599 | |
Yan, Zheng | 24ae636 | 2010-12-06 07:02:36 +0000 | [diff] [blame] | 600 | if (start + num_bytes < end) { |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 601 | start += num_bytes; |
| 602 | pages = NULL; |
| 603 | cond_resched(); |
| 604 | goto again; |
| 605 | } |
| 606 | } else { |
Chris Mason | f03d9301f | 2009-02-04 09:31:06 -0500 | [diff] [blame] | 607 | cleanup_and_bail_uncompressed: |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 608 | /* |
| 609 | * No compression, but we still need to write the pages in |
| 610 | * the file we've been given so far. redirty the locked |
| 611 | * page if it corresponds to our extent and set things up |
| 612 | * for the async work queue to run cow_file_range to do |
| 613 | * the normal delalloc dance |
| 614 | */ |
| 615 | if (page_offset(locked_page) >= start && |
| 616 | page_offset(locked_page) <= end) { |
| 617 | __set_page_dirty_nobuffers(locked_page); |
| 618 | /* unlocked later on in the async handlers */ |
| 619 | } |
Chris Mason | 4adaa61 | 2013-03-26 13:07:00 -0400 | [diff] [blame] | 620 | if (redirty) |
| 621 | extent_range_redirty_for_io(inode, start, end); |
Li Zefan | 261507a0 | 2010-12-17 14:21:50 +0800 | [diff] [blame] | 622 | add_async_extent(async_cow, start, end - start + 1, |
| 623 | 0, NULL, 0, BTRFS_COMPRESS_NONE); |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 624 | *num_added += 1; |
| 625 | } |
| 626 | |
Filipe Manana | c44f649 | 2014-10-09 21:15:44 +0100 | [diff] [blame] | 627 | return; |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 628 | |
| 629 | free_pages_out: |
| 630 | for (i = 0; i < nr_pages_ret; i++) { |
| 631 | WARN_ON(pages[i]->mapping); |
| 632 | page_cache_release(pages[i]); |
| 633 | } |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 634 | kfree(pages); |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 635 | } |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 636 | |
Filipe Manana | 40ae837 | 2014-10-06 22:14:24 +0100 | [diff] [blame] | 637 | static void free_async_extent_pages(struct async_extent *async_extent) |
| 638 | { |
| 639 | int i; |
| 640 | |
| 641 | if (!async_extent->pages) |
| 642 | return; |
| 643 | |
| 644 | for (i = 0; i < async_extent->nr_pages; i++) { |
| 645 | WARN_ON(async_extent->pages[i]->mapping); |
| 646 | page_cache_release(async_extent->pages[i]); |
| 647 | } |
| 648 | kfree(async_extent->pages); |
| 649 | async_extent->nr_pages = 0; |
| 650 | async_extent->pages = NULL; |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 651 | } |
| 652 | |
| 653 | /* |
| 654 | * phase two of compressed writeback. This is the ordered portion |
| 655 | * of the code, which only gets called in the order the work was |
| 656 | * queued. We walk all the async extents created by compress_file_range |
| 657 | * and send them down to the disk. |
| 658 | */ |
Filipe Manana | dec8f17 | 2014-10-06 22:14:26 +0100 | [diff] [blame] | 659 | static noinline void submit_compressed_extents(struct inode *inode, |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 660 | struct async_cow *async_cow) |
| 661 | { |
| 662 | struct async_extent *async_extent; |
| 663 | u64 alloc_hint = 0; |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 664 | struct btrfs_key ins; |
| 665 | struct extent_map *em; |
| 666 | struct btrfs_root *root = BTRFS_I(inode)->root; |
| 667 | struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree; |
| 668 | struct extent_io_tree *io_tree; |
Josef Bacik | f5a84ee3 | 2009-11-10 21:23:48 -0500 | [diff] [blame] | 669 | int ret = 0; |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 670 | |
Josef Bacik | 3e04e7f | 2013-02-06 16:49:15 -0500 | [diff] [blame] | 671 | again: |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 672 | while (!list_empty(&async_cow->extents)) { |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 673 | async_extent = list_entry(async_cow->extents.next, |
| 674 | struct async_extent, list); |
| 675 | list_del(&async_extent->list); |
| 676 | |
| 677 | io_tree = &BTRFS_I(inode)->io_tree; |
| 678 | |
Josef Bacik | f5a84ee3 | 2009-11-10 21:23:48 -0500 | [diff] [blame] | 679 | retry: |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 680 | /* did the compression code fall back to uncompressed IO? */ |
| 681 | if (!async_extent->pages) { |
| 682 | int page_started = 0; |
| 683 | unsigned long nr_written = 0; |
| 684 | |
| 685 | lock_extent(io_tree, async_extent->start, |
Josef Bacik | 2ac55d4 | 2010-02-03 19:33:23 +0000 | [diff] [blame] | 686 | async_extent->start + |
Jeff Mahoney | d008237 | 2012-03-01 14:57:19 +0100 | [diff] [blame] | 687 | async_extent->ram_size - 1); |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 688 | |
| 689 | /* allocate blocks */ |
Josef Bacik | f5a84ee3 | 2009-11-10 21:23:48 -0500 | [diff] [blame] | 690 | ret = cow_file_range(inode, async_cow->locked_page, |
| 691 | async_extent->start, |
| 692 | async_extent->start + |
| 693 | async_extent->ram_size - 1, |
| 694 | &page_started, &nr_written, 0); |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 695 | |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 696 | /* JDM XXX */ |
| 697 | |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 698 | /* |
| 699 | * if page_started, cow_file_range inserted an |
| 700 | * inline extent and took care of all the unlocking |
| 701 | * and IO for us. Otherwise, we need to submit |
| 702 | * all those pages down to the drive. |
| 703 | */ |
Josef Bacik | f5a84ee3 | 2009-11-10 21:23:48 -0500 | [diff] [blame] | 704 | if (!page_started && !ret) |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 705 | extent_write_locked_range(io_tree, |
| 706 | inode, async_extent->start, |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 707 | async_extent->start + |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 708 | async_extent->ram_size - 1, |
| 709 | btrfs_get_extent, |
| 710 | WB_SYNC_ALL); |
Josef Bacik | 3e04e7f | 2013-02-06 16:49:15 -0500 | [diff] [blame] | 711 | else if (ret) |
| 712 | unlock_page(async_cow->locked_page); |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 713 | kfree(async_extent); |
| 714 | cond_resched(); |
| 715 | continue; |
| 716 | } |
| 717 | |
| 718 | lock_extent(io_tree, async_extent->start, |
Jeff Mahoney | d008237 | 2012-03-01 14:57:19 +0100 | [diff] [blame] | 719 | async_extent->start + async_extent->ram_size - 1); |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 720 | |
Josef Bacik | 0036158 | 2013-08-14 14:02:47 -0400 | [diff] [blame] | 721 | ret = btrfs_reserve_extent(root, |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 722 | async_extent->compressed_size, |
| 723 | async_extent->compressed_size, |
Miao Xie | e570fd2 | 2014-06-19 10:42:50 +0800 | [diff] [blame] | 724 | 0, alloc_hint, &ins, 1, 1); |
Josef Bacik | f5a84ee3 | 2009-11-10 21:23:48 -0500 | [diff] [blame] | 725 | if (ret) { |
Filipe Manana | 40ae837 | 2014-10-06 22:14:24 +0100 | [diff] [blame] | 726 | free_async_extent_pages(async_extent); |
Josef Bacik | 3e04e7f | 2013-02-06 16:49:15 -0500 | [diff] [blame] | 727 | |
Josef Bacik | fdf8e2e | 2013-06-14 16:58:23 -0400 | [diff] [blame] | 728 | if (ret == -ENOSPC) { |
| 729 | unlock_extent(io_tree, async_extent->start, |
| 730 | async_extent->start + |
| 731 | async_extent->ram_size - 1); |
Liu Bo | ce62003 | 2014-07-24 22:48:05 +0800 | [diff] [blame] | 732 | |
| 733 | /* |
| 734 | * we need to redirty the pages if we decide to |
| 735 | * fallback to uncompressed IO, otherwise we |
| 736 | * will not submit these pages down to lower |
| 737 | * layers. |
| 738 | */ |
| 739 | extent_range_redirty_for_io(inode, |
| 740 | async_extent->start, |
| 741 | async_extent->start + |
| 742 | async_extent->ram_size - 1); |
| 743 | |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 744 | goto retry; |
Josef Bacik | fdf8e2e | 2013-06-14 16:58:23 -0400 | [diff] [blame] | 745 | } |
Josef Bacik | 3e04e7f | 2013-02-06 16:49:15 -0500 | [diff] [blame] | 746 | goto out_free; |
Josef Bacik | f5a84ee3 | 2009-11-10 21:23:48 -0500 | [diff] [blame] | 747 | } |
| 748 | |
Yan, Zheng | c216775 | 2009-11-12 09:34:21 +0000 | [diff] [blame] | 749 | /* |
| 750 | * here we're doing allocation and writeback of the |
| 751 | * compressed pages |
| 752 | */ |
| 753 | btrfs_drop_extent_cache(inode, async_extent->start, |
| 754 | async_extent->start + |
| 755 | async_extent->ram_size - 1, 0); |
| 756 | |
David Sterba | 172ddd6 | 2011-04-21 00:48:27 +0200 | [diff] [blame] | 757 | em = alloc_extent_map(); |
Liu Bo | b9aa55b | 2013-05-14 02:12:15 +0000 | [diff] [blame] | 758 | if (!em) { |
| 759 | ret = -ENOMEM; |
Josef Bacik | 3e04e7f | 2013-02-06 16:49:15 -0500 | [diff] [blame] | 760 | goto out_free_reserve; |
Liu Bo | b9aa55b | 2013-05-14 02:12:15 +0000 | [diff] [blame] | 761 | } |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 762 | em->start = async_extent->start; |
| 763 | em->len = async_extent->ram_size; |
Chris Mason | 445a694 | 2008-11-10 11:53:33 -0500 | [diff] [blame] | 764 | em->orig_start = em->start; |
Josef Bacik | 2ab28f3 | 2012-10-12 15:27:49 -0400 | [diff] [blame] | 765 | em->mod_start = em->start; |
| 766 | em->mod_len = em->len; |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 767 | |
| 768 | em->block_start = ins.objectid; |
| 769 | em->block_len = ins.offset; |
Josef Bacik | b493968 | 2012-12-03 10:31:19 -0500 | [diff] [blame] | 770 | em->orig_block_len = ins.offset; |
Josef Bacik | cc95bef | 2013-04-04 14:31:27 -0400 | [diff] [blame] | 771 | em->ram_bytes = async_extent->ram_size; |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 772 | em->bdev = root->fs_info->fs_devices->latest_bdev; |
Li Zefan | 261507a0 | 2010-12-17 14:21:50 +0800 | [diff] [blame] | 773 | em->compress_type = async_extent->compress_type; |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 774 | set_bit(EXTENT_FLAG_PINNED, &em->flags); |
| 775 | set_bit(EXTENT_FLAG_COMPRESSED, &em->flags); |
Josef Bacik | 70c8a91 | 2012-10-11 16:54:30 -0400 | [diff] [blame] | 776 | em->generation = -1; |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 777 | |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 778 | while (1) { |
Chris Mason | 890871b | 2009-09-02 16:24:52 -0400 | [diff] [blame] | 779 | write_lock(&em_tree->lock); |
Josef Bacik | 09a2a8f9 | 2013-04-05 16:51:15 -0400 | [diff] [blame] | 780 | ret = add_extent_mapping(em_tree, em, 1); |
Chris Mason | 890871b | 2009-09-02 16:24:52 -0400 | [diff] [blame] | 781 | write_unlock(&em_tree->lock); |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 782 | if (ret != -EEXIST) { |
| 783 | free_extent_map(em); |
| 784 | break; |
| 785 | } |
| 786 | btrfs_drop_extent_cache(inode, async_extent->start, |
| 787 | async_extent->start + |
| 788 | async_extent->ram_size - 1, 0); |
| 789 | } |
| 790 | |
Josef Bacik | 3e04e7f | 2013-02-06 16:49:15 -0500 | [diff] [blame] | 791 | if (ret) |
| 792 | goto out_free_reserve; |
| 793 | |
Li Zefan | 261507a0 | 2010-12-17 14:21:50 +0800 | [diff] [blame] | 794 | ret = btrfs_add_ordered_extent_compress(inode, |
| 795 | async_extent->start, |
| 796 | ins.objectid, |
| 797 | async_extent->ram_size, |
| 798 | ins.offset, |
| 799 | BTRFS_ORDERED_COMPRESSED, |
| 800 | async_extent->compress_type); |
Filipe Manana | d9f8596 | 2014-08-25 10:43:00 +0100 | [diff] [blame] | 801 | if (ret) { |
| 802 | btrfs_drop_extent_cache(inode, async_extent->start, |
| 803 | async_extent->start + |
| 804 | async_extent->ram_size - 1, 0); |
Josef Bacik | 3e04e7f | 2013-02-06 16:49:15 -0500 | [diff] [blame] | 805 | goto out_free_reserve; |
Filipe Manana | d9f8596 | 2014-08-25 10:43:00 +0100 | [diff] [blame] | 806 | } |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 807 | |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 808 | /* |
| 809 | * clear dirty, set writeback and unlock the pages. |
| 810 | */ |
Josef Bacik | c2790a2 | 2013-07-29 11:20:47 -0400 | [diff] [blame] | 811 | extent_clear_unlock_delalloc(inode, async_extent->start, |
Chris Mason | a791e35 | 2009-10-08 11:27:10 -0400 | [diff] [blame] | 812 | async_extent->start + |
| 813 | async_extent->ram_size - 1, |
Josef Bacik | 151a41b | 2013-07-29 13:22:24 -0400 | [diff] [blame] | 814 | NULL, EXTENT_LOCKED | EXTENT_DELALLOC, |
| 815 | PAGE_UNLOCK | PAGE_CLEAR_DIRTY | |
Josef Bacik | c2790a2 | 2013-07-29 11:20:47 -0400 | [diff] [blame] | 816 | PAGE_SET_WRITEBACK); |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 817 | ret = btrfs_submit_compressed_write(inode, |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 818 | async_extent->start, |
| 819 | async_extent->ram_size, |
| 820 | ins.objectid, |
| 821 | ins.offset, async_extent->pages, |
| 822 | async_extent->nr_pages); |
Filipe Manana | fce2a4e | 2014-10-06 22:14:23 +0100 | [diff] [blame] | 823 | if (ret) { |
| 824 | struct extent_io_tree *tree = &BTRFS_I(inode)->io_tree; |
| 825 | struct page *p = async_extent->pages[0]; |
| 826 | const u64 start = async_extent->start; |
| 827 | const u64 end = start + async_extent->ram_size - 1; |
| 828 | |
| 829 | p->mapping = inode->i_mapping; |
| 830 | tree->ops->writepage_end_io_hook(p, start, end, |
| 831 | NULL, 0); |
| 832 | p->mapping = NULL; |
| 833 | extent_clear_unlock_delalloc(inode, start, end, NULL, 0, |
| 834 | PAGE_END_WRITEBACK | |
| 835 | PAGE_SET_ERROR); |
Filipe Manana | 40ae837 | 2014-10-06 22:14:24 +0100 | [diff] [blame] | 836 | free_async_extent_pages(async_extent); |
Filipe Manana | fce2a4e | 2014-10-06 22:14:23 +0100 | [diff] [blame] | 837 | } |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 838 | alloc_hint = ins.objectid + ins.offset; |
| 839 | kfree(async_extent); |
| 840 | cond_resched(); |
| 841 | } |
Filipe Manana | dec8f17 | 2014-10-06 22:14:26 +0100 | [diff] [blame] | 842 | return; |
Josef Bacik | 3e04e7f | 2013-02-06 16:49:15 -0500 | [diff] [blame] | 843 | out_free_reserve: |
Miao Xie | e570fd2 | 2014-06-19 10:42:50 +0800 | [diff] [blame] | 844 | btrfs_free_reserved_extent(root, ins.objectid, ins.offset, 1); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 845 | out_free: |
Josef Bacik | c2790a2 | 2013-07-29 11:20:47 -0400 | [diff] [blame] | 846 | extent_clear_unlock_delalloc(inode, async_extent->start, |
Josef Bacik | 3e04e7f | 2013-02-06 16:49:15 -0500 | [diff] [blame] | 847 | async_extent->start + |
| 848 | async_extent->ram_size - 1, |
Josef Bacik | c2790a2 | 2013-07-29 11:20:47 -0400 | [diff] [blame] | 849 | NULL, EXTENT_LOCKED | EXTENT_DELALLOC | |
Josef Bacik | 151a41b | 2013-07-29 13:22:24 -0400 | [diff] [blame] | 850 | EXTENT_DEFRAG | EXTENT_DO_ACCOUNTING, |
| 851 | PAGE_UNLOCK | PAGE_CLEAR_DIRTY | |
Filipe Manana | 704de49 | 2014-10-06 22:14:22 +0100 | [diff] [blame] | 852 | PAGE_SET_WRITEBACK | PAGE_END_WRITEBACK | |
| 853 | PAGE_SET_ERROR); |
Filipe Manana | 40ae837 | 2014-10-06 22:14:24 +0100 | [diff] [blame] | 854 | free_async_extent_pages(async_extent); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 855 | kfree(async_extent); |
Josef Bacik | 3e04e7f | 2013-02-06 16:49:15 -0500 | [diff] [blame] | 856 | goto again; |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 857 | } |
| 858 | |
Josef Bacik | 4b46fce | 2010-05-23 11:00:55 -0400 | [diff] [blame] | 859 | static u64 get_extent_allocation_hint(struct inode *inode, u64 start, |
| 860 | u64 num_bytes) |
| 861 | { |
| 862 | struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree; |
| 863 | struct extent_map *em; |
| 864 | u64 alloc_hint = 0; |
| 865 | |
| 866 | read_lock(&em_tree->lock); |
| 867 | em = search_extent_mapping(em_tree, start, num_bytes); |
| 868 | if (em) { |
| 869 | /* |
| 870 | * if block start isn't an actual block number then find the |
| 871 | * first block in this inode and use that as a hint. If that |
| 872 | * block is also bogus then just don't worry about it. |
| 873 | */ |
| 874 | if (em->block_start >= EXTENT_MAP_LAST_BYTE) { |
| 875 | free_extent_map(em); |
| 876 | em = search_extent_mapping(em_tree, 0, 0); |
| 877 | if (em && em->block_start < EXTENT_MAP_LAST_BYTE) |
| 878 | alloc_hint = em->block_start; |
| 879 | if (em) |
| 880 | free_extent_map(em); |
| 881 | } else { |
| 882 | alloc_hint = em->block_start; |
| 883 | free_extent_map(em); |
| 884 | } |
| 885 | } |
| 886 | read_unlock(&em_tree->lock); |
| 887 | |
| 888 | return alloc_hint; |
| 889 | } |
| 890 | |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 891 | /* |
| 892 | * when extent_io.c finds a delayed allocation range in the file, |
| 893 | * the call backs end up in this code. The basic idea is to |
| 894 | * allocate extents on disk for the range, and create ordered data structs |
| 895 | * in ram to track those extents. |
| 896 | * |
| 897 | * locked_page is the page that writepage had locked already. We use |
| 898 | * it to make sure we don't do extra locks or unlocks. |
| 899 | * |
| 900 | * *page_started is set to one if we unlock locked_page and do everything |
| 901 | * required to start IO on it. It may be clean and already done with |
| 902 | * IO when we return. |
| 903 | */ |
Josef Bacik | 0036158 | 2013-08-14 14:02:47 -0400 | [diff] [blame] | 904 | static noinline int cow_file_range(struct inode *inode, |
| 905 | struct page *locked_page, |
| 906 | u64 start, u64 end, int *page_started, |
| 907 | unsigned long *nr_written, |
| 908 | int unlock) |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 909 | { |
Josef Bacik | 0036158 | 2013-08-14 14:02:47 -0400 | [diff] [blame] | 910 | struct btrfs_root *root = BTRFS_I(inode)->root; |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 911 | u64 alloc_hint = 0; |
| 912 | u64 num_bytes; |
| 913 | unsigned long ram_size; |
| 914 | u64 disk_num_bytes; |
| 915 | u64 cur_alloc_size; |
| 916 | u64 blocksize = root->sectorsize; |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 917 | struct btrfs_key ins; |
| 918 | struct extent_map *em; |
| 919 | struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree; |
| 920 | int ret = 0; |
| 921 | |
Josef Bacik | 02ecd2c | 2013-10-25 16:19:08 -0400 | [diff] [blame] | 922 | if (btrfs_is_free_space_inode(inode)) { |
| 923 | WARN_ON_ONCE(1); |
Josef Bacik | 29bce2f | 2014-02-07 12:21:23 -0500 | [diff] [blame] | 924 | ret = -EINVAL; |
| 925 | goto out_unlock; |
Josef Bacik | 02ecd2c | 2013-10-25 16:19:08 -0400 | [diff] [blame] | 926 | } |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 927 | |
Qu Wenruo | fda2832 | 2013-02-26 08:10:22 +0000 | [diff] [blame] | 928 | num_bytes = ALIGN(end - start + 1, blocksize); |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 929 | num_bytes = max(blocksize, num_bytes); |
| 930 | disk_num_bytes = num_bytes; |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 931 | |
Chris Mason | 4cb5300 | 2011-05-24 15:35:30 -0400 | [diff] [blame] | 932 | /* if this is a small write inside eof, kick off defrag */ |
Liu Bo | 4cb13e5 | 2012-03-29 09:57:45 -0400 | [diff] [blame] | 933 | if (num_bytes < 64 * 1024 && |
| 934 | (start > 0 || end + 1 < BTRFS_I(inode)->disk_i_size)) |
Josef Bacik | 0036158 | 2013-08-14 14:02:47 -0400 | [diff] [blame] | 935 | btrfs_add_inode_defrag(NULL, inode); |
Chris Mason | 4cb5300 | 2011-05-24 15:35:30 -0400 | [diff] [blame] | 936 | |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 937 | if (start == 0) { |
| 938 | /* lets try to make an inline extent */ |
Josef Bacik | 0036158 | 2013-08-14 14:02:47 -0400 | [diff] [blame] | 939 | ret = cow_file_range_inline(root, inode, start, end, 0, 0, |
| 940 | NULL); |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 941 | if (ret == 0) { |
Josef Bacik | c2790a2 | 2013-07-29 11:20:47 -0400 | [diff] [blame] | 942 | extent_clear_unlock_delalloc(inode, start, end, NULL, |
| 943 | EXTENT_LOCKED | EXTENT_DELALLOC | |
Josef Bacik | 151a41b | 2013-07-29 13:22:24 -0400 | [diff] [blame] | 944 | EXTENT_DEFRAG, PAGE_UNLOCK | |
Josef Bacik | c2790a2 | 2013-07-29 11:20:47 -0400 | [diff] [blame] | 945 | PAGE_CLEAR_DIRTY | PAGE_SET_WRITEBACK | |
| 946 | PAGE_END_WRITEBACK); |
Yan, Zheng | c216775 | 2009-11-12 09:34:21 +0000 | [diff] [blame] | 947 | |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 948 | *nr_written = *nr_written + |
| 949 | (end - start + PAGE_CACHE_SIZE) / PAGE_CACHE_SIZE; |
| 950 | *page_started = 1; |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 951 | goto out; |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 952 | } else if (ret < 0) { |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 953 | goto out_unlock; |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 954 | } |
| 955 | } |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 956 | |
| 957 | BUG_ON(disk_num_bytes > |
David Sterba | 6c41761 | 2011-04-13 15:41:04 +0200 | [diff] [blame] | 958 | btrfs_super_total_bytes(root->fs_info->super_copy)); |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 959 | |
Josef Bacik | 4b46fce | 2010-05-23 11:00:55 -0400 | [diff] [blame] | 960 | alloc_hint = get_extent_allocation_hint(inode, start, num_bytes); |
Zheng Yan | 5b21f2e | 2008-09-26 10:05:38 -0400 | [diff] [blame] | 961 | btrfs_drop_extent_cache(inode, start, start + num_bytes - 1, 0); |
Chris Mason | 3b95151 | 2008-04-17 11:29:12 -0400 | [diff] [blame] | 962 | |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 963 | while (disk_num_bytes > 0) { |
Chris Mason | a791e35 | 2009-10-08 11:27:10 -0400 | [diff] [blame] | 964 | unsigned long op; |
| 965 | |
Josef Bacik | 287a0ab | 2010-03-19 18:07:23 +0000 | [diff] [blame] | 966 | cur_alloc_size = disk_num_bytes; |
Josef Bacik | 0036158 | 2013-08-14 14:02:47 -0400 | [diff] [blame] | 967 | ret = btrfs_reserve_extent(root, cur_alloc_size, |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 968 | root->sectorsize, 0, alloc_hint, |
Miao Xie | e570fd2 | 2014-06-19 10:42:50 +0800 | [diff] [blame] | 969 | &ins, 1, 1); |
Josef Bacik | 0036158 | 2013-08-14 14:02:47 -0400 | [diff] [blame] | 970 | if (ret < 0) |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 971 | goto out_unlock; |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 972 | |
David Sterba | 172ddd6 | 2011-04-21 00:48:27 +0200 | [diff] [blame] | 973 | em = alloc_extent_map(); |
Liu Bo | b9aa55b | 2013-05-14 02:12:15 +0000 | [diff] [blame] | 974 | if (!em) { |
| 975 | ret = -ENOMEM; |
Liu Bo | ace68ba | 2013-04-22 10:53:47 +0000 | [diff] [blame] | 976 | goto out_reserve; |
Liu Bo | b9aa55b | 2013-05-14 02:12:15 +0000 | [diff] [blame] | 977 | } |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 978 | em->start = start; |
Chris Mason | 445a694 | 2008-11-10 11:53:33 -0500 | [diff] [blame] | 979 | em->orig_start = em->start; |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 980 | ram_size = ins.offset; |
| 981 | em->len = ins.offset; |
Josef Bacik | 2ab28f3 | 2012-10-12 15:27:49 -0400 | [diff] [blame] | 982 | em->mod_start = em->start; |
| 983 | em->mod_len = em->len; |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 984 | |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 985 | em->block_start = ins.objectid; |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 986 | em->block_len = ins.offset; |
Josef Bacik | b493968 | 2012-12-03 10:31:19 -0500 | [diff] [blame] | 987 | em->orig_block_len = ins.offset; |
Josef Bacik | cc95bef | 2013-04-04 14:31:27 -0400 | [diff] [blame] | 988 | em->ram_bytes = ram_size; |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 989 | em->bdev = root->fs_info->fs_devices->latest_bdev; |
Chris Mason | 7f3c74f | 2008-07-18 12:01:11 -0400 | [diff] [blame] | 990 | set_bit(EXTENT_FLAG_PINNED, &em->flags); |
Josef Bacik | 70c8a91 | 2012-10-11 16:54:30 -0400 | [diff] [blame] | 991 | em->generation = -1; |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 992 | |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 993 | while (1) { |
Chris Mason | 890871b | 2009-09-02 16:24:52 -0400 | [diff] [blame] | 994 | write_lock(&em_tree->lock); |
Josef Bacik | 09a2a8f9 | 2013-04-05 16:51:15 -0400 | [diff] [blame] | 995 | ret = add_extent_mapping(em_tree, em, 1); |
Chris Mason | 890871b | 2009-09-02 16:24:52 -0400 | [diff] [blame] | 996 | write_unlock(&em_tree->lock); |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 997 | if (ret != -EEXIST) { |
| 998 | free_extent_map(em); |
| 999 | break; |
| 1000 | } |
| 1001 | btrfs_drop_extent_cache(inode, start, |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 1002 | start + ram_size - 1, 0); |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 1003 | } |
Liu Bo | ace68ba | 2013-04-22 10:53:47 +0000 | [diff] [blame] | 1004 | if (ret) |
| 1005 | goto out_reserve; |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 1006 | |
Chris Mason | 98d20f6 | 2008-04-14 09:46:10 -0400 | [diff] [blame] | 1007 | cur_alloc_size = ins.offset; |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 1008 | ret = btrfs_add_ordered_extent(inode, start, ins.objectid, |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 1009 | ram_size, cur_alloc_size, 0); |
Liu Bo | ace68ba | 2013-04-22 10:53:47 +0000 | [diff] [blame] | 1010 | if (ret) |
Filipe Manana | d9f8596 | 2014-08-25 10:43:00 +0100 | [diff] [blame] | 1011 | goto out_drop_extent_cache; |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 1012 | |
Yan Zheng | 17d217f | 2008-12-12 10:03:38 -0500 | [diff] [blame] | 1013 | if (root->root_key.objectid == |
| 1014 | BTRFS_DATA_RELOC_TREE_OBJECTID) { |
| 1015 | ret = btrfs_reloc_clone_csums(inode, start, |
| 1016 | cur_alloc_size); |
Josef Bacik | 0036158 | 2013-08-14 14:02:47 -0400 | [diff] [blame] | 1017 | if (ret) |
Filipe Manana | d9f8596 | 2014-08-25 10:43:00 +0100 | [diff] [blame] | 1018 | goto out_drop_extent_cache; |
Yan Zheng | 17d217f | 2008-12-12 10:03:38 -0500 | [diff] [blame] | 1019 | } |
| 1020 | |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 1021 | if (disk_num_bytes < cur_alloc_size) |
Chris Mason | 3b95151 | 2008-04-17 11:29:12 -0400 | [diff] [blame] | 1022 | break; |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 1023 | |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 1024 | /* we're not doing compressed IO, don't unlock the first |
| 1025 | * page (which the caller expects to stay locked), don't |
| 1026 | * clear any dirty bits and don't set any writeback bits |
Chris Mason | 8b62b72 | 2009-09-02 16:53:46 -0400 | [diff] [blame] | 1027 | * |
| 1028 | * Do set the Private2 bit so we know this page was properly |
| 1029 | * setup for writepage |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 1030 | */ |
Josef Bacik | c2790a2 | 2013-07-29 11:20:47 -0400 | [diff] [blame] | 1031 | op = unlock ? PAGE_UNLOCK : 0; |
| 1032 | op |= PAGE_SET_PRIVATE2; |
Chris Mason | a791e35 | 2009-10-08 11:27:10 -0400 | [diff] [blame] | 1033 | |
Josef Bacik | c2790a2 | 2013-07-29 11:20:47 -0400 | [diff] [blame] | 1034 | extent_clear_unlock_delalloc(inode, start, |
| 1035 | start + ram_size - 1, locked_page, |
| 1036 | EXTENT_LOCKED | EXTENT_DELALLOC, |
| 1037 | op); |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 1038 | disk_num_bytes -= cur_alloc_size; |
Chris Mason | c59f895 | 2007-12-17 20:14:04 -0500 | [diff] [blame] | 1039 | num_bytes -= cur_alloc_size; |
| 1040 | alloc_hint = ins.objectid + ins.offset; |
| 1041 | start += cur_alloc_size; |
Chris Mason | b888db2 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 1042 | } |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 1043 | out: |
Chris Mason | be20aa9 | 2007-12-17 20:14:01 -0500 | [diff] [blame] | 1044 | return ret; |
Miao Xie | b7d5b0a | 2012-11-01 07:32:18 +0000 | [diff] [blame] | 1045 | |
Filipe Manana | d9f8596 | 2014-08-25 10:43:00 +0100 | [diff] [blame] | 1046 | out_drop_extent_cache: |
| 1047 | btrfs_drop_extent_cache(inode, start, start + ram_size - 1, 0); |
Liu Bo | ace68ba | 2013-04-22 10:53:47 +0000 | [diff] [blame] | 1048 | out_reserve: |
Miao Xie | e570fd2 | 2014-06-19 10:42:50 +0800 | [diff] [blame] | 1049 | btrfs_free_reserved_extent(root, ins.objectid, ins.offset, 1); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 1050 | out_unlock: |
Josef Bacik | c2790a2 | 2013-07-29 11:20:47 -0400 | [diff] [blame] | 1051 | extent_clear_unlock_delalloc(inode, start, end, locked_page, |
Josef Bacik | 151a41b | 2013-07-29 13:22:24 -0400 | [diff] [blame] | 1052 | EXTENT_LOCKED | EXTENT_DO_ACCOUNTING | |
| 1053 | EXTENT_DELALLOC | EXTENT_DEFRAG, |
| 1054 | PAGE_UNLOCK | PAGE_CLEAR_DIRTY | |
| 1055 | PAGE_SET_WRITEBACK | PAGE_END_WRITEBACK); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 1056 | goto out; |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 1057 | } |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 1058 | |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 1059 | /* |
| 1060 | * work queue call back to started compression on a file and pages |
| 1061 | */ |
| 1062 | static noinline void async_cow_start(struct btrfs_work *work) |
| 1063 | { |
| 1064 | struct async_cow *async_cow; |
| 1065 | int num_added = 0; |
| 1066 | async_cow = container_of(work, struct async_cow, work); |
| 1067 | |
| 1068 | compress_file_range(async_cow->inode, async_cow->locked_page, |
| 1069 | async_cow->start, async_cow->end, async_cow, |
| 1070 | &num_added); |
Josef Bacik | 8180ef8 | 2012-06-08 15:16:12 -0400 | [diff] [blame] | 1071 | if (num_added == 0) { |
Josef Bacik | cb77fcd | 2012-06-15 12:19:48 -0600 | [diff] [blame] | 1072 | btrfs_add_delayed_iput(async_cow->inode); |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 1073 | async_cow->inode = NULL; |
Josef Bacik | 8180ef8 | 2012-06-08 15:16:12 -0400 | [diff] [blame] | 1074 | } |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 1075 | } |
| 1076 | |
| 1077 | /* |
| 1078 | * work queue call back to submit previously compressed pages |
| 1079 | */ |
| 1080 | static noinline void async_cow_submit(struct btrfs_work *work) |
| 1081 | { |
| 1082 | struct async_cow *async_cow; |
| 1083 | struct btrfs_root *root; |
| 1084 | unsigned long nr_pages; |
| 1085 | |
| 1086 | async_cow = container_of(work, struct async_cow, work); |
| 1087 | |
| 1088 | root = async_cow->root; |
| 1089 | nr_pages = (async_cow->end - async_cow->start + PAGE_CACHE_SIZE) >> |
| 1090 | PAGE_CACHE_SHIFT; |
| 1091 | |
Josef Bacik | 66657b3 | 2012-08-01 15:36:24 -0400 | [diff] [blame] | 1092 | if (atomic_sub_return(nr_pages, &root->fs_info->async_delalloc_pages) < |
Liu Bo | 287082b | 2012-06-28 04:02:24 -0600 | [diff] [blame] | 1093 | 5 * 1024 * 1024 && |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 1094 | waitqueue_active(&root->fs_info->async_submit_wait)) |
| 1095 | wake_up(&root->fs_info->async_submit_wait); |
| 1096 | |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 1097 | if (async_cow->inode) |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 1098 | submit_compressed_extents(async_cow->inode, async_cow); |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 1099 | } |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 1100 | |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 1101 | static noinline void async_cow_free(struct btrfs_work *work) |
| 1102 | { |
| 1103 | struct async_cow *async_cow; |
| 1104 | async_cow = container_of(work, struct async_cow, work); |
Josef Bacik | 8180ef8 | 2012-06-08 15:16:12 -0400 | [diff] [blame] | 1105 | if (async_cow->inode) |
Josef Bacik | cb77fcd | 2012-06-15 12:19:48 -0600 | [diff] [blame] | 1106 | btrfs_add_delayed_iput(async_cow->inode); |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 1107 | kfree(async_cow); |
| 1108 | } |
| 1109 | |
| 1110 | static int cow_file_range_async(struct inode *inode, struct page *locked_page, |
| 1111 | u64 start, u64 end, int *page_started, |
| 1112 | unsigned long *nr_written) |
| 1113 | { |
| 1114 | struct async_cow *async_cow; |
| 1115 | struct btrfs_root *root = BTRFS_I(inode)->root; |
| 1116 | unsigned long nr_pages; |
| 1117 | u64 cur_end; |
Liu Bo | 287082b | 2012-06-28 04:02:24 -0600 | [diff] [blame] | 1118 | int limit = 10 * 1024 * 1024; |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 1119 | |
Chris Mason | a3429ab | 2009-10-08 12:30:20 -0400 | [diff] [blame] | 1120 | clear_extent_bit(&BTRFS_I(inode)->io_tree, start, end, EXTENT_LOCKED, |
| 1121 | 1, 0, NULL, GFP_NOFS); |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 1122 | while (start < end) { |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 1123 | async_cow = kmalloc(sizeof(*async_cow), GFP_NOFS); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 1124 | BUG_ON(!async_cow); /* -ENOMEM */ |
Josef Bacik | 8180ef8 | 2012-06-08 15:16:12 -0400 | [diff] [blame] | 1125 | async_cow->inode = igrab(inode); |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 1126 | async_cow->root = root; |
| 1127 | async_cow->locked_page = locked_page; |
| 1128 | async_cow->start = start; |
| 1129 | |
Wang Shilong | f79707b | 2014-07-17 11:44:09 +0800 | [diff] [blame] | 1130 | if (BTRFS_I(inode)->flags & BTRFS_INODE_NOCOMPRESS && |
| 1131 | !btrfs_test_opt(root, FORCE_COMPRESS)) |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 1132 | cur_end = end; |
| 1133 | else |
| 1134 | cur_end = min(end, start + 512 * 1024 - 1); |
| 1135 | |
| 1136 | async_cow->end = cur_end; |
| 1137 | INIT_LIST_HEAD(&async_cow->extents); |
| 1138 | |
Liu Bo | 9e0af23 | 2014-08-15 23:36:53 +0800 | [diff] [blame] | 1139 | btrfs_init_work(&async_cow->work, |
| 1140 | btrfs_delalloc_helper, |
| 1141 | async_cow_start, async_cow_submit, |
| 1142 | async_cow_free); |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 1143 | |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 1144 | nr_pages = (cur_end - start + PAGE_CACHE_SIZE) >> |
| 1145 | PAGE_CACHE_SHIFT; |
| 1146 | atomic_add(nr_pages, &root->fs_info->async_delalloc_pages); |
| 1147 | |
Qu Wenruo | afe3d24 | 2014-02-28 10:46:07 +0800 | [diff] [blame] | 1148 | btrfs_queue_work(root->fs_info->delalloc_workers, |
| 1149 | &async_cow->work); |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 1150 | |
| 1151 | if (atomic_read(&root->fs_info->async_delalloc_pages) > limit) { |
| 1152 | wait_event(root->fs_info->async_submit_wait, |
| 1153 | (atomic_read(&root->fs_info->async_delalloc_pages) < |
| 1154 | limit)); |
| 1155 | } |
| 1156 | |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 1157 | while (atomic_read(&root->fs_info->async_submit_draining) && |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 1158 | atomic_read(&root->fs_info->async_delalloc_pages)) { |
| 1159 | wait_event(root->fs_info->async_submit_wait, |
| 1160 | (atomic_read(&root->fs_info->async_delalloc_pages) == |
| 1161 | 0)); |
| 1162 | } |
| 1163 | |
| 1164 | *nr_written += nr_pages; |
| 1165 | start = cur_end + 1; |
| 1166 | } |
| 1167 | *page_started = 1; |
| 1168 | return 0; |
Chris Mason | be20aa9 | 2007-12-17 20:14:01 -0500 | [diff] [blame] | 1169 | } |
| 1170 | |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 1171 | static noinline int csum_exist_in_range(struct btrfs_root *root, |
Yan Zheng | 17d217f | 2008-12-12 10:03:38 -0500 | [diff] [blame] | 1172 | u64 bytenr, u64 num_bytes) |
| 1173 | { |
| 1174 | int ret; |
| 1175 | struct btrfs_ordered_sum *sums; |
| 1176 | LIST_HEAD(list); |
| 1177 | |
Yan Zheng | 07d400a | 2009-01-06 11:42:00 -0500 | [diff] [blame] | 1178 | ret = btrfs_lookup_csums_range(root->fs_info->csum_root, bytenr, |
Arne Jansen | a2de733 | 2011-03-08 14:14:00 +0100 | [diff] [blame] | 1179 | bytenr + num_bytes - 1, &list, 0); |
Yan Zheng | 17d217f | 2008-12-12 10:03:38 -0500 | [diff] [blame] | 1180 | if (ret == 0 && list_empty(&list)) |
| 1181 | return 0; |
| 1182 | |
| 1183 | while (!list_empty(&list)) { |
| 1184 | sums = list_entry(list.next, struct btrfs_ordered_sum, list); |
| 1185 | list_del(&sums->list); |
| 1186 | kfree(sums); |
| 1187 | } |
| 1188 | return 1; |
| 1189 | } |
| 1190 | |
Chris Mason | d352ac6 | 2008-09-29 15:18:18 -0400 | [diff] [blame] | 1191 | /* |
| 1192 | * when nowcow writeback call back. This checks for snapshots or COW copies |
| 1193 | * of the extents that exist in the file, and COWs the file as required. |
| 1194 | * |
| 1195 | * If no cow copies or snapshots exist, we write directly to the existing |
| 1196 | * blocks on disk |
| 1197 | */ |
Chris Mason | 7f366cf | 2009-03-12 20:12:45 -0400 | [diff] [blame] | 1198 | static noinline int run_delalloc_nocow(struct inode *inode, |
| 1199 | struct page *locked_page, |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 1200 | u64 start, u64 end, int *page_started, int force, |
| 1201 | unsigned long *nr_written) |
Chris Mason | be20aa9 | 2007-12-17 20:14:01 -0500 | [diff] [blame] | 1202 | { |
Chris Mason | be20aa9 | 2007-12-17 20:14:01 -0500 | [diff] [blame] | 1203 | struct btrfs_root *root = BTRFS_I(inode)->root; |
Yan Zheng | 7ea394f | 2008-08-05 13:05:02 -0400 | [diff] [blame] | 1204 | struct btrfs_trans_handle *trans; |
Chris Mason | be20aa9 | 2007-12-17 20:14:01 -0500 | [diff] [blame] | 1205 | struct extent_buffer *leaf; |
Chris Mason | be20aa9 | 2007-12-17 20:14:01 -0500 | [diff] [blame] | 1206 | struct btrfs_path *path; |
Yan Zheng | 80ff385 | 2008-10-30 14:20:02 -0400 | [diff] [blame] | 1207 | struct btrfs_file_extent_item *fi; |
Chris Mason | be20aa9 | 2007-12-17 20:14:01 -0500 | [diff] [blame] | 1208 | struct btrfs_key found_key; |
Yan Zheng | 80ff385 | 2008-10-30 14:20:02 -0400 | [diff] [blame] | 1209 | u64 cow_start; |
| 1210 | u64 cur_offset; |
| 1211 | u64 extent_end; |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 1212 | u64 extent_offset; |
Yan Zheng | 80ff385 | 2008-10-30 14:20:02 -0400 | [diff] [blame] | 1213 | u64 disk_bytenr; |
| 1214 | u64 num_bytes; |
Josef Bacik | b493968 | 2012-12-03 10:31:19 -0500 | [diff] [blame] | 1215 | u64 disk_num_bytes; |
Josef Bacik | cc95bef | 2013-04-04 14:31:27 -0400 | [diff] [blame] | 1216 | u64 ram_bytes; |
Yan Zheng | 80ff385 | 2008-10-30 14:20:02 -0400 | [diff] [blame] | 1217 | int extent_type; |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 1218 | int ret, err; |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 1219 | int type; |
Yan Zheng | 80ff385 | 2008-10-30 14:20:02 -0400 | [diff] [blame] | 1220 | int nocow; |
| 1221 | int check_prev = 1; |
Li Zefan | 82d5902 | 2011-04-20 10:33:24 +0800 | [diff] [blame] | 1222 | bool nolock; |
Li Zefan | 33345d01 | 2011-04-20 10:31:50 +0800 | [diff] [blame] | 1223 | u64 ino = btrfs_ino(inode); |
Chris Mason | be20aa9 | 2007-12-17 20:14:01 -0500 | [diff] [blame] | 1224 | |
| 1225 | path = btrfs_alloc_path(); |
Josef Bacik | 17ca04a | 2012-05-31 15:58:55 -0400 | [diff] [blame] | 1226 | if (!path) { |
Josef Bacik | c2790a2 | 2013-07-29 11:20:47 -0400 | [diff] [blame] | 1227 | extent_clear_unlock_delalloc(inode, start, end, locked_page, |
| 1228 | EXTENT_LOCKED | EXTENT_DELALLOC | |
Josef Bacik | 151a41b | 2013-07-29 13:22:24 -0400 | [diff] [blame] | 1229 | EXTENT_DO_ACCOUNTING | |
| 1230 | EXTENT_DEFRAG, PAGE_UNLOCK | |
Josef Bacik | c2790a2 | 2013-07-29 11:20:47 -0400 | [diff] [blame] | 1231 | PAGE_CLEAR_DIRTY | |
| 1232 | PAGE_SET_WRITEBACK | |
| 1233 | PAGE_END_WRITEBACK); |
Mark Fasheh | d8926bb | 2011-07-13 10:38:47 -0700 | [diff] [blame] | 1234 | return -ENOMEM; |
Josef Bacik | 17ca04a | 2012-05-31 15:58:55 -0400 | [diff] [blame] | 1235 | } |
Li Zefan | 82d5902 | 2011-04-20 10:33:24 +0800 | [diff] [blame] | 1236 | |
Liu Bo | 83eea1f | 2012-07-10 05:28:39 -0600 | [diff] [blame] | 1237 | nolock = btrfs_is_free_space_inode(inode); |
Li Zefan | 82d5902 | 2011-04-20 10:33:24 +0800 | [diff] [blame] | 1238 | |
| 1239 | if (nolock) |
Josef Bacik | 7a7eaa4 | 2011-04-13 12:54:33 -0400 | [diff] [blame] | 1240 | trans = btrfs_join_transaction_nolock(root); |
Li Zefan | 82d5902 | 2011-04-20 10:33:24 +0800 | [diff] [blame] | 1241 | else |
Josef Bacik | 7a7eaa4 | 2011-04-13 12:54:33 -0400 | [diff] [blame] | 1242 | trans = btrfs_join_transaction(root); |
Chris Mason | ff5714c | 2011-05-28 07:00:39 -0400 | [diff] [blame] | 1243 | |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 1244 | if (IS_ERR(trans)) { |
Josef Bacik | c2790a2 | 2013-07-29 11:20:47 -0400 | [diff] [blame] | 1245 | extent_clear_unlock_delalloc(inode, start, end, locked_page, |
| 1246 | EXTENT_LOCKED | EXTENT_DELALLOC | |
Josef Bacik | 151a41b | 2013-07-29 13:22:24 -0400 | [diff] [blame] | 1247 | EXTENT_DO_ACCOUNTING | |
| 1248 | EXTENT_DEFRAG, PAGE_UNLOCK | |
Josef Bacik | c2790a2 | 2013-07-29 11:20:47 -0400 | [diff] [blame] | 1249 | PAGE_CLEAR_DIRTY | |
| 1250 | PAGE_SET_WRITEBACK | |
| 1251 | PAGE_END_WRITEBACK); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 1252 | btrfs_free_path(path); |
| 1253 | return PTR_ERR(trans); |
| 1254 | } |
| 1255 | |
Josef Bacik | 74b2107 | 2011-04-13 12:02:53 -0400 | [diff] [blame] | 1256 | trans->block_rsv = &root->fs_info->delalloc_block_rsv; |
Chris Mason | be20aa9 | 2007-12-17 20:14:01 -0500 | [diff] [blame] | 1257 | |
Yan Zheng | 80ff385 | 2008-10-30 14:20:02 -0400 | [diff] [blame] | 1258 | cow_start = (u64)-1; |
| 1259 | cur_offset = start; |
| 1260 | while (1) { |
Li Zefan | 33345d01 | 2011-04-20 10:31:50 +0800 | [diff] [blame] | 1261 | ret = btrfs_lookup_file_extent(trans, root, path, ino, |
Yan Zheng | 80ff385 | 2008-10-30 14:20:02 -0400 | [diff] [blame] | 1262 | cur_offset, 0); |
Josef Bacik | d788a34 | 2013-10-25 16:55:08 -0400 | [diff] [blame] | 1263 | if (ret < 0) |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 1264 | goto error; |
Yan Zheng | 80ff385 | 2008-10-30 14:20:02 -0400 | [diff] [blame] | 1265 | if (ret > 0 && path->slots[0] > 0 && check_prev) { |
| 1266 | leaf = path->nodes[0]; |
| 1267 | btrfs_item_key_to_cpu(leaf, &found_key, |
| 1268 | path->slots[0] - 1); |
Li Zefan | 33345d01 | 2011-04-20 10:31:50 +0800 | [diff] [blame] | 1269 | if (found_key.objectid == ino && |
Yan Zheng | 80ff385 | 2008-10-30 14:20:02 -0400 | [diff] [blame] | 1270 | found_key.type == BTRFS_EXTENT_DATA_KEY) |
| 1271 | path->slots[0]--; |
| 1272 | } |
| 1273 | check_prev = 0; |
| 1274 | next_slot: |
| 1275 | leaf = path->nodes[0]; |
| 1276 | if (path->slots[0] >= btrfs_header_nritems(leaf)) { |
| 1277 | ret = btrfs_next_leaf(root, path); |
Josef Bacik | d788a34 | 2013-10-25 16:55:08 -0400 | [diff] [blame] | 1278 | if (ret < 0) |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 1279 | goto error; |
Yan Zheng | 80ff385 | 2008-10-30 14:20:02 -0400 | [diff] [blame] | 1280 | if (ret > 0) |
| 1281 | break; |
| 1282 | leaf = path->nodes[0]; |
| 1283 | } |
Chris Mason | be20aa9 | 2007-12-17 20:14:01 -0500 | [diff] [blame] | 1284 | |
Yan Zheng | 80ff385 | 2008-10-30 14:20:02 -0400 | [diff] [blame] | 1285 | nocow = 0; |
| 1286 | disk_bytenr = 0; |
Yan Zheng | 17d217f | 2008-12-12 10:03:38 -0500 | [diff] [blame] | 1287 | num_bytes = 0; |
Yan Zheng | 80ff385 | 2008-10-30 14:20:02 -0400 | [diff] [blame] | 1288 | btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]); |
Chris Mason | be20aa9 | 2007-12-17 20:14:01 -0500 | [diff] [blame] | 1289 | |
Li Zefan | 33345d01 | 2011-04-20 10:31:50 +0800 | [diff] [blame] | 1290 | if (found_key.objectid > ino || |
Yan Zheng | 80ff385 | 2008-10-30 14:20:02 -0400 | [diff] [blame] | 1291 | found_key.type > BTRFS_EXTENT_DATA_KEY || |
| 1292 | found_key.offset > end) |
| 1293 | break; |
Chris Mason | be20aa9 | 2007-12-17 20:14:01 -0500 | [diff] [blame] | 1294 | |
Yan Zheng | 80ff385 | 2008-10-30 14:20:02 -0400 | [diff] [blame] | 1295 | if (found_key.offset > cur_offset) { |
| 1296 | extent_end = found_key.offset; |
Chris Mason | e9061e2 | 2009-10-09 09:57:45 -0400 | [diff] [blame] | 1297 | extent_type = 0; |
Yan Zheng | 80ff385 | 2008-10-30 14:20:02 -0400 | [diff] [blame] | 1298 | goto out_check; |
| 1299 | } |
Chris Mason | c31f883 | 2008-01-08 15:46:31 -0500 | [diff] [blame] | 1300 | |
Yan Zheng | 80ff385 | 2008-10-30 14:20:02 -0400 | [diff] [blame] | 1301 | fi = btrfs_item_ptr(leaf, path->slots[0], |
| 1302 | struct btrfs_file_extent_item); |
| 1303 | extent_type = btrfs_file_extent_type(leaf, fi); |
Chris Mason | be20aa9 | 2007-12-17 20:14:01 -0500 | [diff] [blame] | 1304 | |
Josef Bacik | cc95bef | 2013-04-04 14:31:27 -0400 | [diff] [blame] | 1305 | ram_bytes = btrfs_file_extent_ram_bytes(leaf, fi); |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 1306 | if (extent_type == BTRFS_FILE_EXTENT_REG || |
| 1307 | extent_type == BTRFS_FILE_EXTENT_PREALLOC) { |
Yan Zheng | 80ff385 | 2008-10-30 14:20:02 -0400 | [diff] [blame] | 1308 | disk_bytenr = btrfs_file_extent_disk_bytenr(leaf, fi); |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 1309 | extent_offset = btrfs_file_extent_offset(leaf, fi); |
Yan Zheng | 80ff385 | 2008-10-30 14:20:02 -0400 | [diff] [blame] | 1310 | extent_end = found_key.offset + |
| 1311 | btrfs_file_extent_num_bytes(leaf, fi); |
Josef Bacik | b493968 | 2012-12-03 10:31:19 -0500 | [diff] [blame] | 1312 | disk_num_bytes = |
| 1313 | btrfs_file_extent_disk_num_bytes(leaf, fi); |
Yan Zheng | 80ff385 | 2008-10-30 14:20:02 -0400 | [diff] [blame] | 1314 | if (extent_end <= start) { |
| 1315 | path->slots[0]++; |
| 1316 | goto next_slot; |
| 1317 | } |
Yan Zheng | 17d217f | 2008-12-12 10:03:38 -0500 | [diff] [blame] | 1318 | if (disk_bytenr == 0) |
| 1319 | goto out_check; |
Yan Zheng | 80ff385 | 2008-10-30 14:20:02 -0400 | [diff] [blame] | 1320 | if (btrfs_file_extent_compression(leaf, fi) || |
| 1321 | btrfs_file_extent_encryption(leaf, fi) || |
| 1322 | btrfs_file_extent_other_encoding(leaf, fi)) |
| 1323 | goto out_check; |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 1324 | if (extent_type == BTRFS_FILE_EXTENT_REG && !force) |
| 1325 | goto out_check; |
Yan Zheng | d2fb343 | 2008-12-11 16:30:39 -0500 | [diff] [blame] | 1326 | if (btrfs_extent_readonly(root, disk_bytenr)) |
Yan Zheng | 80ff385 | 2008-10-30 14:20:02 -0400 | [diff] [blame] | 1327 | goto out_check; |
Li Zefan | 33345d01 | 2011-04-20 10:31:50 +0800 | [diff] [blame] | 1328 | if (btrfs_cross_ref_exist(trans, root, ino, |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 1329 | found_key.offset - |
| 1330 | extent_offset, disk_bytenr)) |
Yan Zheng | 17d217f | 2008-12-12 10:03:38 -0500 | [diff] [blame] | 1331 | goto out_check; |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 1332 | disk_bytenr += extent_offset; |
Yan Zheng | 17d217f | 2008-12-12 10:03:38 -0500 | [diff] [blame] | 1333 | disk_bytenr += cur_offset - found_key.offset; |
| 1334 | num_bytes = min(end + 1, extent_end) - cur_offset; |
| 1335 | /* |
Wang Shilong | e9894fd | 2014-03-27 11:12:25 +0800 | [diff] [blame] | 1336 | * if there are pending snapshots for this root, |
| 1337 | * we fall into common COW way. |
| 1338 | */ |
| 1339 | if (!nolock) { |
Filipe Manana | 9ea24bb | 2014-10-29 11:57:59 +0000 | [diff] [blame] | 1340 | err = btrfs_start_write_no_snapshoting(root); |
Wang Shilong | e9894fd | 2014-03-27 11:12:25 +0800 | [diff] [blame] | 1341 | if (!err) |
| 1342 | goto out_check; |
| 1343 | } |
| 1344 | /* |
Yan Zheng | 17d217f | 2008-12-12 10:03:38 -0500 | [diff] [blame] | 1345 | * force cow if csum exists in the range. |
| 1346 | * this ensure that csum for a given extent are |
| 1347 | * either valid or do not exist. |
| 1348 | */ |
| 1349 | if (csum_exist_in_range(root, disk_bytenr, num_bytes)) |
| 1350 | goto out_check; |
Yan Zheng | 80ff385 | 2008-10-30 14:20:02 -0400 | [diff] [blame] | 1351 | nocow = 1; |
| 1352 | } else if (extent_type == BTRFS_FILE_EXTENT_INLINE) { |
| 1353 | extent_end = found_key.offset + |
Chris Mason | 514ac8a | 2014-01-03 21:07:00 -0800 | [diff] [blame] | 1354 | btrfs_file_extent_inline_len(leaf, |
| 1355 | path->slots[0], fi); |
Yan Zheng | 80ff385 | 2008-10-30 14:20:02 -0400 | [diff] [blame] | 1356 | extent_end = ALIGN(extent_end, root->sectorsize); |
| 1357 | } else { |
| 1358 | BUG_ON(1); |
| 1359 | } |
| 1360 | out_check: |
| 1361 | if (extent_end <= start) { |
| 1362 | path->slots[0]++; |
Wang Shilong | e9894fd | 2014-03-27 11:12:25 +0800 | [diff] [blame] | 1363 | if (!nolock && nocow) |
Filipe Manana | 9ea24bb | 2014-10-29 11:57:59 +0000 | [diff] [blame] | 1364 | btrfs_end_write_no_snapshoting(root); |
Yan Zheng | 80ff385 | 2008-10-30 14:20:02 -0400 | [diff] [blame] | 1365 | goto next_slot; |
| 1366 | } |
| 1367 | if (!nocow) { |
| 1368 | if (cow_start == (u64)-1) |
| 1369 | cow_start = cur_offset; |
| 1370 | cur_offset = extent_end; |
| 1371 | if (cur_offset > end) |
| 1372 | break; |
| 1373 | path->slots[0]++; |
| 1374 | goto next_slot; |
Yan Zheng | 7ea394f | 2008-08-05 13:05:02 -0400 | [diff] [blame] | 1375 | } |
Chris Mason | be20aa9 | 2007-12-17 20:14:01 -0500 | [diff] [blame] | 1376 | |
David Sterba | b3b4aa7 | 2011-04-21 01:20:15 +0200 | [diff] [blame] | 1377 | btrfs_release_path(path); |
Yan Zheng | 80ff385 | 2008-10-30 14:20:02 -0400 | [diff] [blame] | 1378 | if (cow_start != (u64)-1) { |
Josef Bacik | 0036158 | 2013-08-14 14:02:47 -0400 | [diff] [blame] | 1379 | ret = cow_file_range(inode, locked_page, |
| 1380 | cow_start, found_key.offset - 1, |
| 1381 | page_started, nr_written, 1); |
Wang Shilong | e9894fd | 2014-03-27 11:12:25 +0800 | [diff] [blame] | 1382 | if (ret) { |
| 1383 | if (!nolock && nocow) |
Filipe Manana | 9ea24bb | 2014-10-29 11:57:59 +0000 | [diff] [blame] | 1384 | btrfs_end_write_no_snapshoting(root); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 1385 | goto error; |
Wang Shilong | e9894fd | 2014-03-27 11:12:25 +0800 | [diff] [blame] | 1386 | } |
Yan Zheng | 80ff385 | 2008-10-30 14:20:02 -0400 | [diff] [blame] | 1387 | cow_start = (u64)-1; |
Yan Zheng | 7ea394f | 2008-08-05 13:05:02 -0400 | [diff] [blame] | 1388 | } |
Yan Zheng | 80ff385 | 2008-10-30 14:20:02 -0400 | [diff] [blame] | 1389 | |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 1390 | if (extent_type == BTRFS_FILE_EXTENT_PREALLOC) { |
| 1391 | struct extent_map *em; |
| 1392 | struct extent_map_tree *em_tree; |
| 1393 | em_tree = &BTRFS_I(inode)->extent_tree; |
David Sterba | 172ddd6 | 2011-04-21 00:48:27 +0200 | [diff] [blame] | 1394 | em = alloc_extent_map(); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 1395 | BUG_ON(!em); /* -ENOMEM */ |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 1396 | em->start = cur_offset; |
Josef Bacik | 70c8a91 | 2012-10-11 16:54:30 -0400 | [diff] [blame] | 1397 | em->orig_start = found_key.offset - extent_offset; |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 1398 | em->len = num_bytes; |
| 1399 | em->block_len = num_bytes; |
| 1400 | em->block_start = disk_bytenr; |
Josef Bacik | b493968 | 2012-12-03 10:31:19 -0500 | [diff] [blame] | 1401 | em->orig_block_len = disk_num_bytes; |
Josef Bacik | cc95bef | 2013-04-04 14:31:27 -0400 | [diff] [blame] | 1402 | em->ram_bytes = ram_bytes; |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 1403 | em->bdev = root->fs_info->fs_devices->latest_bdev; |
Josef Bacik | 2ab28f3 | 2012-10-12 15:27:49 -0400 | [diff] [blame] | 1404 | em->mod_start = em->start; |
| 1405 | em->mod_len = em->len; |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 1406 | set_bit(EXTENT_FLAG_PINNED, &em->flags); |
Josef Bacik | b11e234 | 2012-12-03 10:58:15 -0500 | [diff] [blame] | 1407 | set_bit(EXTENT_FLAG_FILLING, &em->flags); |
Josef Bacik | 70c8a91 | 2012-10-11 16:54:30 -0400 | [diff] [blame] | 1408 | em->generation = -1; |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 1409 | while (1) { |
Chris Mason | 890871b | 2009-09-02 16:24:52 -0400 | [diff] [blame] | 1410 | write_lock(&em_tree->lock); |
Josef Bacik | 09a2a8f9 | 2013-04-05 16:51:15 -0400 | [diff] [blame] | 1411 | ret = add_extent_mapping(em_tree, em, 1); |
Chris Mason | 890871b | 2009-09-02 16:24:52 -0400 | [diff] [blame] | 1412 | write_unlock(&em_tree->lock); |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 1413 | if (ret != -EEXIST) { |
| 1414 | free_extent_map(em); |
| 1415 | break; |
| 1416 | } |
| 1417 | btrfs_drop_extent_cache(inode, em->start, |
| 1418 | em->start + em->len - 1, 0); |
| 1419 | } |
| 1420 | type = BTRFS_ORDERED_PREALLOC; |
| 1421 | } else { |
| 1422 | type = BTRFS_ORDERED_NOCOW; |
| 1423 | } |
Yan Zheng | 80ff385 | 2008-10-30 14:20:02 -0400 | [diff] [blame] | 1424 | |
| 1425 | ret = btrfs_add_ordered_extent(inode, cur_offset, disk_bytenr, |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 1426 | num_bytes, num_bytes, type); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 1427 | BUG_ON(ret); /* -ENOMEM */ |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 1428 | |
Yan, Zheng | efa5646 | 2010-05-16 10:49:59 -0400 | [diff] [blame] | 1429 | if (root->root_key.objectid == |
| 1430 | BTRFS_DATA_RELOC_TREE_OBJECTID) { |
| 1431 | ret = btrfs_reloc_clone_csums(inode, cur_offset, |
| 1432 | num_bytes); |
Wang Shilong | e9894fd | 2014-03-27 11:12:25 +0800 | [diff] [blame] | 1433 | if (ret) { |
| 1434 | if (!nolock && nocow) |
Filipe Manana | 9ea24bb | 2014-10-29 11:57:59 +0000 | [diff] [blame] | 1435 | btrfs_end_write_no_snapshoting(root); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 1436 | goto error; |
Wang Shilong | e9894fd | 2014-03-27 11:12:25 +0800 | [diff] [blame] | 1437 | } |
Yan, Zheng | efa5646 | 2010-05-16 10:49:59 -0400 | [diff] [blame] | 1438 | } |
| 1439 | |
Josef Bacik | c2790a2 | 2013-07-29 11:20:47 -0400 | [diff] [blame] | 1440 | extent_clear_unlock_delalloc(inode, cur_offset, |
| 1441 | cur_offset + num_bytes - 1, |
| 1442 | locked_page, EXTENT_LOCKED | |
| 1443 | EXTENT_DELALLOC, PAGE_UNLOCK | |
| 1444 | PAGE_SET_PRIVATE2); |
Wang Shilong | e9894fd | 2014-03-27 11:12:25 +0800 | [diff] [blame] | 1445 | if (!nolock && nocow) |
Filipe Manana | 9ea24bb | 2014-10-29 11:57:59 +0000 | [diff] [blame] | 1446 | btrfs_end_write_no_snapshoting(root); |
Yan Zheng | 80ff385 | 2008-10-30 14:20:02 -0400 | [diff] [blame] | 1447 | cur_offset = extent_end; |
| 1448 | if (cur_offset > end) |
| 1449 | break; |
Yan Zheng | 7ea394f | 2008-08-05 13:05:02 -0400 | [diff] [blame] | 1450 | } |
David Sterba | b3b4aa7 | 2011-04-21 01:20:15 +0200 | [diff] [blame] | 1451 | btrfs_release_path(path); |
Yan Zheng | 80ff385 | 2008-10-30 14:20:02 -0400 | [diff] [blame] | 1452 | |
Josef Bacik | 17ca04a | 2012-05-31 15:58:55 -0400 | [diff] [blame] | 1453 | if (cur_offset <= end && cow_start == (u64)-1) { |
Yan Zheng | 80ff385 | 2008-10-30 14:20:02 -0400 | [diff] [blame] | 1454 | cow_start = cur_offset; |
Josef Bacik | 17ca04a | 2012-05-31 15:58:55 -0400 | [diff] [blame] | 1455 | cur_offset = end; |
| 1456 | } |
| 1457 | |
Yan Zheng | 80ff385 | 2008-10-30 14:20:02 -0400 | [diff] [blame] | 1458 | if (cow_start != (u64)-1) { |
Josef Bacik | 0036158 | 2013-08-14 14:02:47 -0400 | [diff] [blame] | 1459 | ret = cow_file_range(inode, locked_page, cow_start, end, |
| 1460 | page_started, nr_written, 1); |
Josef Bacik | d788a34 | 2013-10-25 16:55:08 -0400 | [diff] [blame] | 1461 | if (ret) |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 1462 | goto error; |
Yan Zheng | 80ff385 | 2008-10-30 14:20:02 -0400 | [diff] [blame] | 1463 | } |
| 1464 | |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 1465 | error: |
Miao Xie | a698d075 | 2012-09-20 01:51:59 -0600 | [diff] [blame] | 1466 | err = btrfs_end_transaction(trans, root); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 1467 | if (!ret) |
| 1468 | ret = err; |
| 1469 | |
Josef Bacik | 17ca04a | 2012-05-31 15:58:55 -0400 | [diff] [blame] | 1470 | if (ret && cur_offset < end) |
Josef Bacik | c2790a2 | 2013-07-29 11:20:47 -0400 | [diff] [blame] | 1471 | extent_clear_unlock_delalloc(inode, cur_offset, end, |
| 1472 | locked_page, EXTENT_LOCKED | |
Josef Bacik | 151a41b | 2013-07-29 13:22:24 -0400 | [diff] [blame] | 1473 | EXTENT_DELALLOC | EXTENT_DEFRAG | |
| 1474 | EXTENT_DO_ACCOUNTING, PAGE_UNLOCK | |
| 1475 | PAGE_CLEAR_DIRTY | |
Josef Bacik | c2790a2 | 2013-07-29 11:20:47 -0400 | [diff] [blame] | 1476 | PAGE_SET_WRITEBACK | |
| 1477 | PAGE_END_WRITEBACK); |
Yan Zheng | 7ea394f | 2008-08-05 13:05:02 -0400 | [diff] [blame] | 1478 | btrfs_free_path(path); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 1479 | return ret; |
Chris Mason | be20aa9 | 2007-12-17 20:14:01 -0500 | [diff] [blame] | 1480 | } |
| 1481 | |
Wang Shilong | 47059d9 | 2014-07-03 18:22:07 +0800 | [diff] [blame] | 1482 | static inline int need_force_cow(struct inode *inode, u64 start, u64 end) |
| 1483 | { |
| 1484 | |
| 1485 | if (!(BTRFS_I(inode)->flags & BTRFS_INODE_NODATACOW) && |
| 1486 | !(BTRFS_I(inode)->flags & BTRFS_INODE_PREALLOC)) |
| 1487 | return 0; |
| 1488 | |
| 1489 | /* |
| 1490 | * @defrag_bytes is a hint value, no spinlock held here, |
| 1491 | * if is not zero, it means the file is defragging. |
| 1492 | * Force cow if given extent needs to be defragged. |
| 1493 | */ |
| 1494 | if (BTRFS_I(inode)->defrag_bytes && |
| 1495 | test_range_bit(&BTRFS_I(inode)->io_tree, start, end, |
| 1496 | EXTENT_DEFRAG, 0, NULL)) |
| 1497 | return 1; |
| 1498 | |
| 1499 | return 0; |
| 1500 | } |
| 1501 | |
Chris Mason | d352ac6 | 2008-09-29 15:18:18 -0400 | [diff] [blame] | 1502 | /* |
| 1503 | * extent_io.c call back to do delayed allocation processing |
| 1504 | */ |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 1505 | static int run_delalloc_range(struct inode *inode, struct page *locked_page, |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 1506 | u64 start, u64 end, int *page_started, |
| 1507 | unsigned long *nr_written) |
Chris Mason | be20aa9 | 2007-12-17 20:14:01 -0500 | [diff] [blame] | 1508 | { |
Chris Mason | be20aa9 | 2007-12-17 20:14:01 -0500 | [diff] [blame] | 1509 | int ret; |
Wang Shilong | 47059d9 | 2014-07-03 18:22:07 +0800 | [diff] [blame] | 1510 | int force_cow = need_force_cow(inode, start, end); |
Chris Mason | a213501 | 2008-06-25 16:01:30 -0400 | [diff] [blame] | 1511 | |
Wang Shilong | 47059d9 | 2014-07-03 18:22:07 +0800 | [diff] [blame] | 1512 | if (BTRFS_I(inode)->flags & BTRFS_INODE_NODATACOW && !force_cow) { |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 1513 | ret = run_delalloc_nocow(inode, locked_page, start, end, |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 1514 | page_started, 1, nr_written); |
Wang Shilong | 47059d9 | 2014-07-03 18:22:07 +0800 | [diff] [blame] | 1515 | } else if (BTRFS_I(inode)->flags & BTRFS_INODE_PREALLOC && !force_cow) { |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 1516 | ret = run_delalloc_nocow(inode, locked_page, start, end, |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 1517 | page_started, 0, nr_written); |
Wang Shilong | 7816030 | 2014-07-17 11:44:10 +0800 | [diff] [blame] | 1518 | } else if (!inode_need_compress(inode)) { |
Chris Mason | 7f366cf | 2009-03-12 20:12:45 -0400 | [diff] [blame] | 1519 | ret = cow_file_range(inode, locked_page, start, end, |
| 1520 | page_started, nr_written, 1); |
Josef Bacik | 7ddf5a4 | 2012-06-08 15:26:47 -0400 | [diff] [blame] | 1521 | } else { |
| 1522 | set_bit(BTRFS_INODE_HAS_ASYNC_EXTENT, |
| 1523 | &BTRFS_I(inode)->runtime_flags); |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 1524 | ret = cow_file_range_async(inode, locked_page, start, end, |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 1525 | page_started, nr_written); |
Josef Bacik | 7ddf5a4 | 2012-06-08 15:26:47 -0400 | [diff] [blame] | 1526 | } |
Chris Mason | b888db2 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 1527 | return ret; |
| 1528 | } |
| 1529 | |
Jeff Mahoney | 1bf8504 | 2011-07-21 16:56:09 +0000 | [diff] [blame] | 1530 | static void btrfs_split_extent_hook(struct inode *inode, |
| 1531 | struct extent_state *orig, u64 split) |
Josef Bacik | 9ed74f2 | 2009-09-11 16:12:44 -0400 | [diff] [blame] | 1532 | { |
Josef Bacik | dcab6a3 | 2015-02-11 15:08:59 -0500 | [diff] [blame] | 1533 | u64 size; |
| 1534 | |
Yan, Zheng | 0ca1f7c | 2010-05-16 10:48:47 -0400 | [diff] [blame] | 1535 | /* not delalloc, ignore it */ |
Josef Bacik | 9ed74f2 | 2009-09-11 16:12:44 -0400 | [diff] [blame] | 1536 | if (!(orig->state & EXTENT_DELALLOC)) |
Jeff Mahoney | 1bf8504 | 2011-07-21 16:56:09 +0000 | [diff] [blame] | 1537 | return; |
Josef Bacik | 9ed74f2 | 2009-09-11 16:12:44 -0400 | [diff] [blame] | 1538 | |
Josef Bacik | dcab6a3 | 2015-02-11 15:08:59 -0500 | [diff] [blame] | 1539 | size = orig->end - orig->start + 1; |
| 1540 | if (size > BTRFS_MAX_EXTENT_SIZE) { |
| 1541 | u64 num_extents; |
| 1542 | u64 new_size; |
| 1543 | |
| 1544 | /* |
| 1545 | * We need the largest size of the remaining extent to see if we |
| 1546 | * need to add a new outstanding extent. Think of the following |
| 1547 | * case |
| 1548 | * |
| 1549 | * [MEAX_EXTENT_SIZEx2 - 4k][4k] |
| 1550 | * |
| 1551 | * The new_size would just be 4k and we'd think we had enough |
| 1552 | * outstanding extents for this if we only took one side of the |
| 1553 | * split, same goes for the other direction. We need to see if |
| 1554 | * the larger size still is the same amount of extents as the |
| 1555 | * original size, because if it is we need to add a new |
| 1556 | * outstanding extent. But if we split up and the larger size |
| 1557 | * is less than the original then we are good to go since we've |
| 1558 | * already accounted for the extra extent in our original |
| 1559 | * accounting. |
| 1560 | */ |
| 1561 | new_size = orig->end - split + 1; |
| 1562 | if ((split - orig->start) > new_size) |
| 1563 | new_size = split - orig->start; |
| 1564 | |
| 1565 | num_extents = div64_u64(size + BTRFS_MAX_EXTENT_SIZE - 1, |
| 1566 | BTRFS_MAX_EXTENT_SIZE); |
| 1567 | if (div64_u64(new_size + BTRFS_MAX_EXTENT_SIZE - 1, |
| 1568 | BTRFS_MAX_EXTENT_SIZE) < num_extents) |
| 1569 | return; |
| 1570 | } |
| 1571 | |
Josef Bacik | 9e0baf6 | 2011-07-15 15:16:44 +0000 | [diff] [blame] | 1572 | spin_lock(&BTRFS_I(inode)->lock); |
| 1573 | BTRFS_I(inode)->outstanding_extents++; |
| 1574 | spin_unlock(&BTRFS_I(inode)->lock); |
Josef Bacik | 9ed74f2 | 2009-09-11 16:12:44 -0400 | [diff] [blame] | 1575 | } |
| 1576 | |
| 1577 | /* |
| 1578 | * extent_io.c merge_extent_hook, used to track merged delayed allocation |
| 1579 | * extents so we can keep track of new extents that are just merged onto old |
| 1580 | * extents, such as when we are doing sequential writes, so we can properly |
| 1581 | * account for the metadata space we'll need. |
| 1582 | */ |
Jeff Mahoney | 1bf8504 | 2011-07-21 16:56:09 +0000 | [diff] [blame] | 1583 | static void btrfs_merge_extent_hook(struct inode *inode, |
| 1584 | struct extent_state *new, |
| 1585 | struct extent_state *other) |
Josef Bacik | 9ed74f2 | 2009-09-11 16:12:44 -0400 | [diff] [blame] | 1586 | { |
Josef Bacik | dcab6a3 | 2015-02-11 15:08:59 -0500 | [diff] [blame] | 1587 | u64 new_size, old_size; |
| 1588 | u64 num_extents; |
| 1589 | |
Josef Bacik | 9ed74f2 | 2009-09-11 16:12:44 -0400 | [diff] [blame] | 1590 | /* not delalloc, ignore it */ |
| 1591 | if (!(other->state & EXTENT_DELALLOC)) |
Jeff Mahoney | 1bf8504 | 2011-07-21 16:56:09 +0000 | [diff] [blame] | 1592 | return; |
Josef Bacik | 9ed74f2 | 2009-09-11 16:12:44 -0400 | [diff] [blame] | 1593 | |
Josef Bacik | dcab6a3 | 2015-02-11 15:08:59 -0500 | [diff] [blame] | 1594 | old_size = other->end - other->start + 1; |
Josef Bacik | 8461a3d | 2015-03-13 15:12:08 -0400 | [diff] [blame^] | 1595 | if (old_size < (new->end - new->start + 1)) |
| 1596 | old_size = (new->end - new->start + 1); |
| 1597 | if (new->start > other->start) |
| 1598 | new_size = new->end - other->start + 1; |
| 1599 | else |
| 1600 | new_size = other->end - new->start + 1; |
Josef Bacik | dcab6a3 | 2015-02-11 15:08:59 -0500 | [diff] [blame] | 1601 | |
| 1602 | /* we're not bigger than the max, unreserve the space and go */ |
| 1603 | if (new_size <= BTRFS_MAX_EXTENT_SIZE) { |
| 1604 | spin_lock(&BTRFS_I(inode)->lock); |
| 1605 | BTRFS_I(inode)->outstanding_extents--; |
| 1606 | spin_unlock(&BTRFS_I(inode)->lock); |
| 1607 | return; |
| 1608 | } |
| 1609 | |
| 1610 | /* |
| 1611 | * If we grew by another max_extent, just return, we want to keep that |
| 1612 | * reserved amount. |
| 1613 | */ |
| 1614 | num_extents = div64_u64(old_size + BTRFS_MAX_EXTENT_SIZE - 1, |
| 1615 | BTRFS_MAX_EXTENT_SIZE); |
| 1616 | if (div64_u64(new_size + BTRFS_MAX_EXTENT_SIZE - 1, |
| 1617 | BTRFS_MAX_EXTENT_SIZE) > num_extents) |
| 1618 | return; |
| 1619 | |
Josef Bacik | 9e0baf6 | 2011-07-15 15:16:44 +0000 | [diff] [blame] | 1620 | spin_lock(&BTRFS_I(inode)->lock); |
| 1621 | BTRFS_I(inode)->outstanding_extents--; |
| 1622 | spin_unlock(&BTRFS_I(inode)->lock); |
Josef Bacik | 9ed74f2 | 2009-09-11 16:12:44 -0400 | [diff] [blame] | 1623 | } |
| 1624 | |
Miao Xie | eb73c1b | 2013-05-15 07:48:22 +0000 | [diff] [blame] | 1625 | static void btrfs_add_delalloc_inodes(struct btrfs_root *root, |
| 1626 | struct inode *inode) |
| 1627 | { |
| 1628 | spin_lock(&root->delalloc_lock); |
| 1629 | if (list_empty(&BTRFS_I(inode)->delalloc_inodes)) { |
| 1630 | list_add_tail(&BTRFS_I(inode)->delalloc_inodes, |
| 1631 | &root->delalloc_inodes); |
| 1632 | set_bit(BTRFS_INODE_IN_DELALLOC_LIST, |
| 1633 | &BTRFS_I(inode)->runtime_flags); |
| 1634 | root->nr_delalloc_inodes++; |
| 1635 | if (root->nr_delalloc_inodes == 1) { |
| 1636 | spin_lock(&root->fs_info->delalloc_root_lock); |
| 1637 | BUG_ON(!list_empty(&root->delalloc_root)); |
| 1638 | list_add_tail(&root->delalloc_root, |
| 1639 | &root->fs_info->delalloc_roots); |
| 1640 | spin_unlock(&root->fs_info->delalloc_root_lock); |
| 1641 | } |
| 1642 | } |
| 1643 | spin_unlock(&root->delalloc_lock); |
| 1644 | } |
| 1645 | |
| 1646 | static void btrfs_del_delalloc_inode(struct btrfs_root *root, |
| 1647 | struct inode *inode) |
| 1648 | { |
| 1649 | spin_lock(&root->delalloc_lock); |
| 1650 | if (!list_empty(&BTRFS_I(inode)->delalloc_inodes)) { |
| 1651 | list_del_init(&BTRFS_I(inode)->delalloc_inodes); |
| 1652 | clear_bit(BTRFS_INODE_IN_DELALLOC_LIST, |
| 1653 | &BTRFS_I(inode)->runtime_flags); |
| 1654 | root->nr_delalloc_inodes--; |
| 1655 | if (!root->nr_delalloc_inodes) { |
| 1656 | spin_lock(&root->fs_info->delalloc_root_lock); |
| 1657 | BUG_ON(list_empty(&root->delalloc_root)); |
| 1658 | list_del_init(&root->delalloc_root); |
| 1659 | spin_unlock(&root->fs_info->delalloc_root_lock); |
| 1660 | } |
| 1661 | } |
| 1662 | spin_unlock(&root->delalloc_lock); |
| 1663 | } |
| 1664 | |
Chris Mason | d352ac6 | 2008-09-29 15:18:18 -0400 | [diff] [blame] | 1665 | /* |
| 1666 | * extent_io.c set_bit_hook, used to track delayed allocation |
| 1667 | * bytes in this file, and to maintain the list of inodes that |
| 1668 | * have pending delalloc work to be done. |
| 1669 | */ |
Jeff Mahoney | 1bf8504 | 2011-07-21 16:56:09 +0000 | [diff] [blame] | 1670 | static void btrfs_set_bit_hook(struct inode *inode, |
David Sterba | 9ee49a04 | 2015-01-14 19:52:13 +0100 | [diff] [blame] | 1671 | struct extent_state *state, unsigned *bits) |
Chris Mason | 291d673 | 2008-01-29 15:55:23 -0500 | [diff] [blame] | 1672 | { |
Josef Bacik | 9ed74f2 | 2009-09-11 16:12:44 -0400 | [diff] [blame] | 1673 | |
Wang Shilong | 47059d9 | 2014-07-03 18:22:07 +0800 | [diff] [blame] | 1674 | if ((*bits & EXTENT_DEFRAG) && !(*bits & EXTENT_DELALLOC)) |
| 1675 | WARN_ON(1); |
Chris Mason | 75eff68 | 2008-12-15 15:54:40 -0500 | [diff] [blame] | 1676 | /* |
| 1677 | * set_bit and clear bit hooks normally require _irqsave/restore |
Sergei Trofimovich | 27160b6 | 2011-05-20 20:20:32 +0000 | [diff] [blame] | 1678 | * but in this case, we are only testing for the DELALLOC |
Chris Mason | 75eff68 | 2008-12-15 15:54:40 -0500 | [diff] [blame] | 1679 | * bit, which is only set or cleared with irqs on |
| 1680 | */ |
Yan, Zheng | 0ca1f7c | 2010-05-16 10:48:47 -0400 | [diff] [blame] | 1681 | if (!(state->state & EXTENT_DELALLOC) && (*bits & EXTENT_DELALLOC)) { |
Chris Mason | 291d673 | 2008-01-29 15:55:23 -0500 | [diff] [blame] | 1682 | struct btrfs_root *root = BTRFS_I(inode)->root; |
Yan, Zheng | 0ca1f7c | 2010-05-16 10:48:47 -0400 | [diff] [blame] | 1683 | u64 len = state->end + 1 - state->start; |
Liu Bo | 83eea1f | 2012-07-10 05:28:39 -0600 | [diff] [blame] | 1684 | bool do_list = !btrfs_is_free_space_inode(inode); |
Josef Bacik | 9ed74f2 | 2009-09-11 16:12:44 -0400 | [diff] [blame] | 1685 | |
Josef Bacik | 9e0baf6 | 2011-07-15 15:16:44 +0000 | [diff] [blame] | 1686 | if (*bits & EXTENT_FIRST_DELALLOC) { |
Yan, Zheng | 0ca1f7c | 2010-05-16 10:48:47 -0400 | [diff] [blame] | 1687 | *bits &= ~EXTENT_FIRST_DELALLOC; |
Josef Bacik | 9e0baf6 | 2011-07-15 15:16:44 +0000 | [diff] [blame] | 1688 | } else { |
| 1689 | spin_lock(&BTRFS_I(inode)->lock); |
| 1690 | BTRFS_I(inode)->outstanding_extents++; |
| 1691 | spin_unlock(&BTRFS_I(inode)->lock); |
| 1692 | } |
Josef Bacik | 287a0ab | 2010-03-19 18:07:23 +0000 | [diff] [blame] | 1693 | |
Miao Xie | 963d678 | 2013-01-29 10:10:51 +0000 | [diff] [blame] | 1694 | __percpu_counter_add(&root->fs_info->delalloc_bytes, len, |
| 1695 | root->fs_info->delalloc_batch); |
Miao Xie | df0af1a | 2013-01-29 10:11:59 +0000 | [diff] [blame] | 1696 | spin_lock(&BTRFS_I(inode)->lock); |
Yan, Zheng | 0ca1f7c | 2010-05-16 10:48:47 -0400 | [diff] [blame] | 1697 | BTRFS_I(inode)->delalloc_bytes += len; |
Wang Shilong | 47059d9 | 2014-07-03 18:22:07 +0800 | [diff] [blame] | 1698 | if (*bits & EXTENT_DEFRAG) |
| 1699 | BTRFS_I(inode)->defrag_bytes += len; |
Miao Xie | df0af1a | 2013-01-29 10:11:59 +0000 | [diff] [blame] | 1700 | if (do_list && !test_bit(BTRFS_INODE_IN_DELALLOC_LIST, |
Miao Xie | eb73c1b | 2013-05-15 07:48:22 +0000 | [diff] [blame] | 1701 | &BTRFS_I(inode)->runtime_flags)) |
| 1702 | btrfs_add_delalloc_inodes(root, inode); |
Miao Xie | df0af1a | 2013-01-29 10:11:59 +0000 | [diff] [blame] | 1703 | spin_unlock(&BTRFS_I(inode)->lock); |
Chris Mason | 291d673 | 2008-01-29 15:55:23 -0500 | [diff] [blame] | 1704 | } |
Chris Mason | 291d673 | 2008-01-29 15:55:23 -0500 | [diff] [blame] | 1705 | } |
| 1706 | |
Chris Mason | d352ac6 | 2008-09-29 15:18:18 -0400 | [diff] [blame] | 1707 | /* |
| 1708 | * extent_io.c clear_bit_hook, see set_bit_hook for why |
| 1709 | */ |
Jeff Mahoney | 1bf8504 | 2011-07-21 16:56:09 +0000 | [diff] [blame] | 1710 | static void btrfs_clear_bit_hook(struct inode *inode, |
David Sterba | 4107488 | 2013-04-29 13:38:46 +0000 | [diff] [blame] | 1711 | struct extent_state *state, |
David Sterba | 9ee49a04 | 2015-01-14 19:52:13 +0100 | [diff] [blame] | 1712 | unsigned *bits) |
Chris Mason | 291d673 | 2008-01-29 15:55:23 -0500 | [diff] [blame] | 1713 | { |
Wang Shilong | 47059d9 | 2014-07-03 18:22:07 +0800 | [diff] [blame] | 1714 | u64 len = state->end + 1 - state->start; |
Josef Bacik | dcab6a3 | 2015-02-11 15:08:59 -0500 | [diff] [blame] | 1715 | u64 num_extents = div64_u64(len + BTRFS_MAX_EXTENT_SIZE -1, |
| 1716 | BTRFS_MAX_EXTENT_SIZE); |
Wang Shilong | 47059d9 | 2014-07-03 18:22:07 +0800 | [diff] [blame] | 1717 | |
| 1718 | spin_lock(&BTRFS_I(inode)->lock); |
| 1719 | if ((state->state & EXTENT_DEFRAG) && (*bits & EXTENT_DEFRAG)) |
| 1720 | BTRFS_I(inode)->defrag_bytes -= len; |
| 1721 | spin_unlock(&BTRFS_I(inode)->lock); |
| 1722 | |
Chris Mason | 75eff68 | 2008-12-15 15:54:40 -0500 | [diff] [blame] | 1723 | /* |
| 1724 | * set_bit and clear bit hooks normally require _irqsave/restore |
Sergei Trofimovich | 27160b6 | 2011-05-20 20:20:32 +0000 | [diff] [blame] | 1725 | * but in this case, we are only testing for the DELALLOC |
Chris Mason | 75eff68 | 2008-12-15 15:54:40 -0500 | [diff] [blame] | 1726 | * bit, which is only set or cleared with irqs on |
| 1727 | */ |
Yan, Zheng | 0ca1f7c | 2010-05-16 10:48:47 -0400 | [diff] [blame] | 1728 | if ((state->state & EXTENT_DELALLOC) && (*bits & EXTENT_DELALLOC)) { |
Chris Mason | 291d673 | 2008-01-29 15:55:23 -0500 | [diff] [blame] | 1729 | struct btrfs_root *root = BTRFS_I(inode)->root; |
Liu Bo | 83eea1f | 2012-07-10 05:28:39 -0600 | [diff] [blame] | 1730 | bool do_list = !btrfs_is_free_space_inode(inode); |
Chris Mason | bcbfce8 | 2008-04-22 13:26:47 -0400 | [diff] [blame] | 1731 | |
Josef Bacik | 9e0baf6 | 2011-07-15 15:16:44 +0000 | [diff] [blame] | 1732 | if (*bits & EXTENT_FIRST_DELALLOC) { |
Yan, Zheng | 0ca1f7c | 2010-05-16 10:48:47 -0400 | [diff] [blame] | 1733 | *bits &= ~EXTENT_FIRST_DELALLOC; |
Josef Bacik | 9e0baf6 | 2011-07-15 15:16:44 +0000 | [diff] [blame] | 1734 | } else if (!(*bits & EXTENT_DO_ACCOUNTING)) { |
| 1735 | spin_lock(&BTRFS_I(inode)->lock); |
Josef Bacik | dcab6a3 | 2015-02-11 15:08:59 -0500 | [diff] [blame] | 1736 | BTRFS_I(inode)->outstanding_extents -= num_extents; |
Josef Bacik | 9e0baf6 | 2011-07-15 15:16:44 +0000 | [diff] [blame] | 1737 | spin_unlock(&BTRFS_I(inode)->lock); |
| 1738 | } |
Yan, Zheng | 0ca1f7c | 2010-05-16 10:48:47 -0400 | [diff] [blame] | 1739 | |
Josef Bacik | b6d08f0 | 2013-09-27 14:57:43 -0400 | [diff] [blame] | 1740 | /* |
| 1741 | * We don't reserve metadata space for space cache inodes so we |
| 1742 | * don't need to call dellalloc_release_metadata if there is an |
| 1743 | * error. |
| 1744 | */ |
| 1745 | if (*bits & EXTENT_DO_ACCOUNTING && |
| 1746 | root != root->fs_info->tree_root) |
Yan, Zheng | 0ca1f7c | 2010-05-16 10:48:47 -0400 | [diff] [blame] | 1747 | btrfs_delalloc_release_metadata(inode, len); |
| 1748 | |
Josef Bacik | 0cb59c9 | 2010-07-02 12:14:14 -0400 | [diff] [blame] | 1749 | if (root->root_key.objectid != BTRFS_DATA_RELOC_TREE_OBJECTID |
Josef Bacik | 7ee9e44 | 2013-06-21 16:37:03 -0400 | [diff] [blame] | 1750 | && do_list && !(state->state & EXTENT_NORESERVE)) |
Yan, Zheng | 0ca1f7c | 2010-05-16 10:48:47 -0400 | [diff] [blame] | 1751 | btrfs_free_reserved_data_space(inode, len); |
Josef Bacik | 9ed74f2 | 2009-09-11 16:12:44 -0400 | [diff] [blame] | 1752 | |
Miao Xie | 963d678 | 2013-01-29 10:10:51 +0000 | [diff] [blame] | 1753 | __percpu_counter_add(&root->fs_info->delalloc_bytes, -len, |
| 1754 | root->fs_info->delalloc_batch); |
Miao Xie | df0af1a | 2013-01-29 10:11:59 +0000 | [diff] [blame] | 1755 | spin_lock(&BTRFS_I(inode)->lock); |
Yan, Zheng | 0ca1f7c | 2010-05-16 10:48:47 -0400 | [diff] [blame] | 1756 | BTRFS_I(inode)->delalloc_bytes -= len; |
Josef Bacik | 0cb59c9 | 2010-07-02 12:14:14 -0400 | [diff] [blame] | 1757 | if (do_list && BTRFS_I(inode)->delalloc_bytes == 0 && |
Miao Xie | df0af1a | 2013-01-29 10:11:59 +0000 | [diff] [blame] | 1758 | test_bit(BTRFS_INODE_IN_DELALLOC_LIST, |
Miao Xie | eb73c1b | 2013-05-15 07:48:22 +0000 | [diff] [blame] | 1759 | &BTRFS_I(inode)->runtime_flags)) |
| 1760 | btrfs_del_delalloc_inode(root, inode); |
Miao Xie | df0af1a | 2013-01-29 10:11:59 +0000 | [diff] [blame] | 1761 | spin_unlock(&BTRFS_I(inode)->lock); |
Chris Mason | 291d673 | 2008-01-29 15:55:23 -0500 | [diff] [blame] | 1762 | } |
Chris Mason | 291d673 | 2008-01-29 15:55:23 -0500 | [diff] [blame] | 1763 | } |
| 1764 | |
Chris Mason | d352ac6 | 2008-09-29 15:18:18 -0400 | [diff] [blame] | 1765 | /* |
| 1766 | * extent_io.c merge_bio_hook, this must check the chunk tree to make sure |
| 1767 | * we don't create bios that span stripes or chunks |
| 1768 | */ |
David Woodhouse | 64a1670 | 2009-07-15 23:29:37 +0100 | [diff] [blame] | 1769 | 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] | 1770 | size_t size, struct bio *bio, |
| 1771 | unsigned long bio_flags) |
Chris Mason | 239b14b | 2008-03-24 15:02:07 -0400 | [diff] [blame] | 1772 | { |
| 1773 | struct btrfs_root *root = BTRFS_I(page->mapping->host)->root; |
Kent Overstreet | 4f024f3 | 2013-10-11 15:44:27 -0700 | [diff] [blame] | 1774 | u64 logical = (u64)bio->bi_iter.bi_sector << 9; |
Chris Mason | 239b14b | 2008-03-24 15:02:07 -0400 | [diff] [blame] | 1775 | u64 length = 0; |
| 1776 | u64 map_length; |
Chris Mason | 239b14b | 2008-03-24 15:02:07 -0400 | [diff] [blame] | 1777 | int ret; |
| 1778 | |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 1779 | if (bio_flags & EXTENT_BIO_COMPRESSED) |
| 1780 | return 0; |
| 1781 | |
Kent Overstreet | 4f024f3 | 2013-10-11 15:44:27 -0700 | [diff] [blame] | 1782 | length = bio->bi_iter.bi_size; |
Chris Mason | 239b14b | 2008-03-24 15:02:07 -0400 | [diff] [blame] | 1783 | map_length = length; |
David Woodhouse | 64a1670 | 2009-07-15 23:29:37 +0100 | [diff] [blame] | 1784 | ret = btrfs_map_block(root->fs_info, rw, logical, |
Chris Mason | f188591 | 2008-04-09 16:28:12 -0400 | [diff] [blame] | 1785 | &map_length, NULL, 0); |
Stefan Behrens | 3ec706c | 2012-11-05 15:46:42 +0100 | [diff] [blame] | 1786 | /* Will always return 0 with map_multi == NULL */ |
Jeff Mahoney | 3444a97 | 2011-10-03 23:23:13 -0400 | [diff] [blame] | 1787 | BUG_ON(ret < 0); |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 1788 | if (map_length < length + size) |
Chris Mason | 239b14b | 2008-03-24 15:02:07 -0400 | [diff] [blame] | 1789 | return 1; |
Jeff Mahoney | 3444a97 | 2011-10-03 23:23:13 -0400 | [diff] [blame] | 1790 | return 0; |
Chris Mason | 239b14b | 2008-03-24 15:02:07 -0400 | [diff] [blame] | 1791 | } |
| 1792 | |
Chris Mason | d352ac6 | 2008-09-29 15:18:18 -0400 | [diff] [blame] | 1793 | /* |
| 1794 | * in order to insert checksums into the metadata in large chunks, |
| 1795 | * we wait until bio submission time. All the pages in the bio are |
| 1796 | * checksummed and sums are attached onto the ordered extent record. |
| 1797 | * |
| 1798 | * At IO completion time the cums attached on the ordered extent record |
| 1799 | * are inserted into the btree |
| 1800 | */ |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 1801 | static int __btrfs_submit_bio_start(struct inode *inode, int rw, |
| 1802 | struct bio *bio, int mirror_num, |
Chris Mason | eaf25d9 | 2010-05-25 09:48:28 -0400 | [diff] [blame] | 1803 | unsigned long bio_flags, |
| 1804 | u64 bio_offset) |
Chris Mason | 065631f | 2008-02-20 12:07:25 -0500 | [diff] [blame] | 1805 | { |
Chris Mason | 065631f | 2008-02-20 12:07:25 -0500 | [diff] [blame] | 1806 | struct btrfs_root *root = BTRFS_I(inode)->root; |
Chris Mason | 065631f | 2008-02-20 12:07:25 -0500 | [diff] [blame] | 1807 | int ret = 0; |
Chris Mason | e015640 | 2008-04-16 11:15:20 -0400 | [diff] [blame] | 1808 | |
Chris Mason | d20f704 | 2008-12-08 16:58:54 -0500 | [diff] [blame] | 1809 | ret = btrfs_csum_one_bio(root, inode, bio, 0, 0); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 1810 | BUG_ON(ret); /* -ENOMEM */ |
Chris Mason | 4a69a41 | 2008-11-06 22:03:00 -0500 | [diff] [blame] | 1811 | return 0; |
| 1812 | } |
Chris Mason | e015640 | 2008-04-16 11:15:20 -0400 | [diff] [blame] | 1813 | |
Chris Mason | 4a69a41 | 2008-11-06 22:03:00 -0500 | [diff] [blame] | 1814 | /* |
| 1815 | * in order to insert checksums into the metadata in large chunks, |
| 1816 | * we wait until bio submission time. All the pages in the bio are |
| 1817 | * checksummed and sums are attached onto the ordered extent record. |
| 1818 | * |
| 1819 | * At IO completion time the cums attached on the ordered extent record |
| 1820 | * are inserted into the btree |
| 1821 | */ |
Christoph Hellwig | b295086 | 2008-12-02 09:54:17 -0500 | [diff] [blame] | 1822 | 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] | 1823 | int mirror_num, unsigned long bio_flags, |
| 1824 | u64 bio_offset) |
Chris Mason | 4a69a41 | 2008-11-06 22:03:00 -0500 | [diff] [blame] | 1825 | { |
| 1826 | struct btrfs_root *root = BTRFS_I(inode)->root; |
Stefan Behrens | 6189192 | 2012-11-05 18:51:52 +0100 | [diff] [blame] | 1827 | int ret; |
| 1828 | |
| 1829 | ret = btrfs_map_bio(root, rw, bio, mirror_num, 1); |
| 1830 | if (ret) |
| 1831 | bio_endio(bio, ret); |
| 1832 | return ret; |
Chris Mason | 44b8bd7 | 2008-04-16 11:14:51 -0400 | [diff] [blame] | 1833 | } |
| 1834 | |
Chris Mason | d352ac6 | 2008-09-29 15:18:18 -0400 | [diff] [blame] | 1835 | /* |
Chris Mason | cad321a | 2008-12-17 14:51:42 -0500 | [diff] [blame] | 1836 | * extent_io.c submission hook. This does the right thing for csum calculation |
| 1837 | * on write, or reading the csums from the tree before a read |
Chris Mason | d352ac6 | 2008-09-29 15:18:18 -0400 | [diff] [blame] | 1838 | */ |
Christoph Hellwig | b295086 | 2008-12-02 09:54:17 -0500 | [diff] [blame] | 1839 | 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] | 1840 | int mirror_num, unsigned long bio_flags, |
| 1841 | u64 bio_offset) |
Chris Mason | 44b8bd7 | 2008-04-16 11:14:51 -0400 | [diff] [blame] | 1842 | { |
| 1843 | struct btrfs_root *root = BTRFS_I(inode)->root; |
| 1844 | int ret = 0; |
Chris Mason | 19b9bdb | 2008-10-30 14:23:13 -0400 | [diff] [blame] | 1845 | int skip_sum; |
Jeff Mahoney | 0417341 | 2011-10-03 23:23:12 -0400 | [diff] [blame] | 1846 | int metadata = 0; |
Josef Bacik | b812ce2 | 2012-11-16 13:56:32 -0500 | [diff] [blame] | 1847 | int async = !atomic_read(&BTRFS_I(inode)->sync_writers); |
Chris Mason | 44b8bd7 | 2008-04-16 11:14:51 -0400 | [diff] [blame] | 1848 | |
Christoph Hellwig | 6cbff00 | 2009-04-17 10:37:41 +0200 | [diff] [blame] | 1849 | skip_sum = BTRFS_I(inode)->flags & BTRFS_INODE_NODATASUM; |
Chris Mason | cad321a | 2008-12-17 14:51:42 -0500 | [diff] [blame] | 1850 | |
Liu Bo | 83eea1f | 2012-07-10 05:28:39 -0600 | [diff] [blame] | 1851 | if (btrfs_is_free_space_inode(inode)) |
Jeff Mahoney | 0417341 | 2011-10-03 23:23:12 -0400 | [diff] [blame] | 1852 | metadata = 2; |
| 1853 | |
Christoph Hellwig | 7b6d91d | 2010-08-07 18:20:39 +0200 | [diff] [blame] | 1854 | if (!(rw & REQ_WRITE)) { |
Josef Bacik | 5fd0204 | 2012-05-02 14:00:54 -0400 | [diff] [blame] | 1855 | ret = btrfs_bio_wq_end_io(root->fs_info, bio, metadata); |
| 1856 | if (ret) |
Stefan Behrens | 6189192 | 2012-11-05 18:51:52 +0100 | [diff] [blame] | 1857 | goto out; |
Josef Bacik | 5fd0204 | 2012-05-02 14:00:54 -0400 | [diff] [blame] | 1858 | |
Chris Mason | d20f704 | 2008-12-08 16:58:54 -0500 | [diff] [blame] | 1859 | if (bio_flags & EXTENT_BIO_COMPRESSED) { |
Stefan Behrens | 6189192 | 2012-11-05 18:51:52 +0100 | [diff] [blame] | 1860 | ret = btrfs_submit_compressed_read(inode, bio, |
| 1861 | mirror_num, |
| 1862 | bio_flags); |
| 1863 | goto out; |
Tsutomu Itoh | c2db107 | 2011-03-01 06:48:31 +0000 | [diff] [blame] | 1864 | } else if (!skip_sum) { |
| 1865 | ret = btrfs_lookup_bio_sums(root, inode, bio, NULL); |
| 1866 | if (ret) |
Stefan Behrens | 6189192 | 2012-11-05 18:51:52 +0100 | [diff] [blame] | 1867 | goto out; |
Tsutomu Itoh | c2db107 | 2011-03-01 06:48:31 +0000 | [diff] [blame] | 1868 | } |
Chris Mason | 4d1b5fb | 2008-08-20 09:44:52 -0400 | [diff] [blame] | 1869 | goto mapit; |
Josef Bacik | b812ce2 | 2012-11-16 13:56:32 -0500 | [diff] [blame] | 1870 | } else if (async && !skip_sum) { |
Yan Zheng | 17d217f | 2008-12-12 10:03:38 -0500 | [diff] [blame] | 1871 | /* csum items have already been cloned */ |
| 1872 | if (root->root_key.objectid == BTRFS_DATA_RELOC_TREE_OBJECTID) |
| 1873 | goto mapit; |
Chris Mason | 19b9bdb | 2008-10-30 14:23:13 -0400 | [diff] [blame] | 1874 | /* we're doing a write, do the async checksumming */ |
Stefan Behrens | 6189192 | 2012-11-05 18:51:52 +0100 | [diff] [blame] | 1875 | ret = btrfs_wq_submit_bio(BTRFS_I(inode)->root->fs_info, |
Chris Mason | 44b8bd7 | 2008-04-16 11:14:51 -0400 | [diff] [blame] | 1876 | inode, rw, bio, mirror_num, |
Chris Mason | eaf25d9 | 2010-05-25 09:48:28 -0400 | [diff] [blame] | 1877 | bio_flags, bio_offset, |
| 1878 | __btrfs_submit_bio_start, |
Chris Mason | 4a69a41 | 2008-11-06 22:03:00 -0500 | [diff] [blame] | 1879 | __btrfs_submit_bio_done); |
Stefan Behrens | 6189192 | 2012-11-05 18:51:52 +0100 | [diff] [blame] | 1880 | goto out; |
Josef Bacik | b812ce2 | 2012-11-16 13:56:32 -0500 | [diff] [blame] | 1881 | } else if (!skip_sum) { |
| 1882 | ret = btrfs_csum_one_bio(root, inode, bio, 0, 0); |
| 1883 | if (ret) |
| 1884 | goto out; |
Chris Mason | 19b9bdb | 2008-10-30 14:23:13 -0400 | [diff] [blame] | 1885 | } |
| 1886 | |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 1887 | mapit: |
Stefan Behrens | 6189192 | 2012-11-05 18:51:52 +0100 | [diff] [blame] | 1888 | ret = btrfs_map_bio(root, rw, bio, mirror_num, 0); |
| 1889 | |
| 1890 | out: |
| 1891 | if (ret < 0) |
| 1892 | bio_endio(bio, ret); |
| 1893 | return ret; |
Chris Mason | 065631f | 2008-02-20 12:07:25 -0500 | [diff] [blame] | 1894 | } |
Chris Mason | 6885f30 | 2008-02-20 16:11:05 -0500 | [diff] [blame] | 1895 | |
Chris Mason | d352ac6 | 2008-09-29 15:18:18 -0400 | [diff] [blame] | 1896 | /* |
| 1897 | * given a list of ordered sums record them in the inode. This happens |
| 1898 | * at IO completion time based on sums calculated at bio submission time. |
| 1899 | */ |
Chris Mason | ba1da2f | 2008-07-17 12:54:15 -0400 | [diff] [blame] | 1900 | static noinline int add_pending_csums(struct btrfs_trans_handle *trans, |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 1901 | struct inode *inode, u64 file_offset, |
| 1902 | struct list_head *list) |
| 1903 | { |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 1904 | struct btrfs_ordered_sum *sum; |
| 1905 | |
Qinghuang Feng | c6e3087 | 2009-01-21 10:59:08 -0500 | [diff] [blame] | 1906 | list_for_each_entry(sum, list, list) { |
Miao Xie | 39847c4 | 2013-03-28 08:08:20 +0000 | [diff] [blame] | 1907 | trans->adding_csums = 1; |
Chris Mason | d20f704 | 2008-12-08 16:58:54 -0500 | [diff] [blame] | 1908 | btrfs_csum_file_blocks(trans, |
| 1909 | BTRFS_I(inode)->root->fs_info->csum_root, sum); |
Miao Xie | 39847c4 | 2013-03-28 08:08:20 +0000 | [diff] [blame] | 1910 | trans->adding_csums = 0; |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 1911 | } |
| 1912 | return 0; |
| 1913 | } |
| 1914 | |
Josef Bacik | 2ac55d4 | 2010-02-03 19:33:23 +0000 | [diff] [blame] | 1915 | int btrfs_set_extent_delalloc(struct inode *inode, u64 start, u64 end, |
| 1916 | struct extent_state **cached_state) |
Chris Mason | ea8c281 | 2008-08-04 23:17:27 -0400 | [diff] [blame] | 1917 | { |
Julia Lawall | 6c1500f | 2012-11-03 20:30:18 +0000 | [diff] [blame] | 1918 | WARN_ON((end & (PAGE_CACHE_SIZE - 1)) == 0); |
Chris Mason | ea8c281 | 2008-08-04 23:17:27 -0400 | [diff] [blame] | 1919 | return set_extent_delalloc(&BTRFS_I(inode)->io_tree, start, end, |
Josef Bacik | 2ac55d4 | 2010-02-03 19:33:23 +0000 | [diff] [blame] | 1920 | cached_state, GFP_NOFS); |
Chris Mason | ea8c281 | 2008-08-04 23:17:27 -0400 | [diff] [blame] | 1921 | } |
| 1922 | |
Chris Mason | d352ac6 | 2008-09-29 15:18:18 -0400 | [diff] [blame] | 1923 | /* see btrfs_writepage_start_hook for details on why this is required */ |
Chris Mason | 247e743 | 2008-07-17 12:53:51 -0400 | [diff] [blame] | 1924 | struct btrfs_writepage_fixup { |
| 1925 | struct page *page; |
| 1926 | struct btrfs_work work; |
| 1927 | }; |
| 1928 | |
Christoph Hellwig | b295086 | 2008-12-02 09:54:17 -0500 | [diff] [blame] | 1929 | static void btrfs_writepage_fixup_worker(struct btrfs_work *work) |
Chris Mason | 247e743 | 2008-07-17 12:53:51 -0400 | [diff] [blame] | 1930 | { |
| 1931 | struct btrfs_writepage_fixup *fixup; |
| 1932 | struct btrfs_ordered_extent *ordered; |
Josef Bacik | 2ac55d4 | 2010-02-03 19:33:23 +0000 | [diff] [blame] | 1933 | struct extent_state *cached_state = NULL; |
Chris Mason | 247e743 | 2008-07-17 12:53:51 -0400 | [diff] [blame] | 1934 | struct page *page; |
| 1935 | struct inode *inode; |
| 1936 | u64 page_start; |
| 1937 | u64 page_end; |
Jeff Mahoney | 87826df | 2012-02-15 16:23:57 +0100 | [diff] [blame] | 1938 | int ret; |
Chris Mason | 247e743 | 2008-07-17 12:53:51 -0400 | [diff] [blame] | 1939 | |
| 1940 | fixup = container_of(work, struct btrfs_writepage_fixup, work); |
| 1941 | page = fixup->page; |
Chris Mason | 4a09675 | 2008-07-21 10:29:44 -0400 | [diff] [blame] | 1942 | again: |
Chris Mason | 247e743 | 2008-07-17 12:53:51 -0400 | [diff] [blame] | 1943 | lock_page(page); |
| 1944 | if (!page->mapping || !PageDirty(page) || !PageChecked(page)) { |
| 1945 | ClearPageChecked(page); |
| 1946 | goto out_page; |
| 1947 | } |
| 1948 | |
| 1949 | inode = page->mapping->host; |
| 1950 | page_start = page_offset(page); |
| 1951 | page_end = page_offset(page) + PAGE_CACHE_SIZE - 1; |
| 1952 | |
Josef Bacik | 2ac55d4 | 2010-02-03 19:33:23 +0000 | [diff] [blame] | 1953 | 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] | 1954 | &cached_state); |
Chris Mason | 4a09675 | 2008-07-21 10:29:44 -0400 | [diff] [blame] | 1955 | |
| 1956 | /* already ordered? We're done */ |
Chris Mason | 8b62b72 | 2009-09-02 16:53:46 -0400 | [diff] [blame] | 1957 | if (PagePrivate2(page)) |
Chris Mason | 247e743 | 2008-07-17 12:53:51 -0400 | [diff] [blame] | 1958 | goto out; |
Chris Mason | 4a09675 | 2008-07-21 10:29:44 -0400 | [diff] [blame] | 1959 | |
| 1960 | ordered = btrfs_lookup_ordered_extent(inode, page_start); |
| 1961 | if (ordered) { |
Josef Bacik | 2ac55d4 | 2010-02-03 19:33:23 +0000 | [diff] [blame] | 1962 | unlock_extent_cached(&BTRFS_I(inode)->io_tree, page_start, |
| 1963 | page_end, &cached_state, GFP_NOFS); |
Chris Mason | 4a09675 | 2008-07-21 10:29:44 -0400 | [diff] [blame] | 1964 | unlock_page(page); |
| 1965 | btrfs_start_ordered_extent(inode, ordered, 1); |
Jeff Mahoney | 87826df | 2012-02-15 16:23:57 +0100 | [diff] [blame] | 1966 | btrfs_put_ordered_extent(ordered); |
Chris Mason | 4a09675 | 2008-07-21 10:29:44 -0400 | [diff] [blame] | 1967 | goto again; |
| 1968 | } |
Chris Mason | 247e743 | 2008-07-17 12:53:51 -0400 | [diff] [blame] | 1969 | |
Jeff Mahoney | 87826df | 2012-02-15 16:23:57 +0100 | [diff] [blame] | 1970 | ret = btrfs_delalloc_reserve_space(inode, PAGE_CACHE_SIZE); |
| 1971 | if (ret) { |
| 1972 | mapping_set_error(page->mapping, ret); |
| 1973 | end_extent_writepage(page, ret, page_start, page_end); |
| 1974 | ClearPageChecked(page); |
| 1975 | goto out; |
| 1976 | } |
| 1977 | |
Josef Bacik | 2ac55d4 | 2010-02-03 19:33:23 +0000 | [diff] [blame] | 1978 | btrfs_set_extent_delalloc(inode, page_start, page_end, &cached_state); |
Chris Mason | 247e743 | 2008-07-17 12:53:51 -0400 | [diff] [blame] | 1979 | ClearPageChecked(page); |
Jeff Mahoney | 87826df | 2012-02-15 16:23:57 +0100 | [diff] [blame] | 1980 | set_page_dirty(page); |
Chris Mason | 247e743 | 2008-07-17 12:53:51 -0400 | [diff] [blame] | 1981 | out: |
Josef Bacik | 2ac55d4 | 2010-02-03 19:33:23 +0000 | [diff] [blame] | 1982 | unlock_extent_cached(&BTRFS_I(inode)->io_tree, page_start, page_end, |
| 1983 | &cached_state, GFP_NOFS); |
Chris Mason | 247e743 | 2008-07-17 12:53:51 -0400 | [diff] [blame] | 1984 | out_page: |
| 1985 | unlock_page(page); |
| 1986 | page_cache_release(page); |
Miao Xie | b897abe | 2011-01-26 16:19:22 +0800 | [diff] [blame] | 1987 | kfree(fixup); |
Chris Mason | 247e743 | 2008-07-17 12:53:51 -0400 | [diff] [blame] | 1988 | } |
| 1989 | |
| 1990 | /* |
| 1991 | * There are a few paths in the higher layers of the kernel that directly |
| 1992 | * set the page dirty bit without asking the filesystem if it is a |
| 1993 | * good idea. This causes problems because we want to make sure COW |
| 1994 | * properly happens and the data=ordered rules are followed. |
| 1995 | * |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 1996 | * 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] | 1997 | * hasn't been properly setup for IO. We kick off an async process |
| 1998 | * to fix it up. The async helper will wait for ordered extents, set |
| 1999 | * the delalloc bit and make it safe to write the page. |
| 2000 | */ |
Christoph Hellwig | b295086 | 2008-12-02 09:54:17 -0500 | [diff] [blame] | 2001 | 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] | 2002 | { |
| 2003 | struct inode *inode = page->mapping->host; |
| 2004 | struct btrfs_writepage_fixup *fixup; |
| 2005 | struct btrfs_root *root = BTRFS_I(inode)->root; |
Chris Mason | 247e743 | 2008-07-17 12:53:51 -0400 | [diff] [blame] | 2006 | |
Chris Mason | 8b62b72 | 2009-09-02 16:53:46 -0400 | [diff] [blame] | 2007 | /* this page is properly in the ordered list */ |
| 2008 | if (TestClearPagePrivate2(page)) |
Chris Mason | 247e743 | 2008-07-17 12:53:51 -0400 | [diff] [blame] | 2009 | return 0; |
| 2010 | |
| 2011 | if (PageChecked(page)) |
| 2012 | return -EAGAIN; |
| 2013 | |
| 2014 | fixup = kzalloc(sizeof(*fixup), GFP_NOFS); |
| 2015 | if (!fixup) |
| 2016 | return -EAGAIN; |
Chris Mason | f421950 | 2008-07-22 11:18:09 -0400 | [diff] [blame] | 2017 | |
Chris Mason | 247e743 | 2008-07-17 12:53:51 -0400 | [diff] [blame] | 2018 | SetPageChecked(page); |
| 2019 | page_cache_get(page); |
Liu Bo | 9e0af23 | 2014-08-15 23:36:53 +0800 | [diff] [blame] | 2020 | btrfs_init_work(&fixup->work, btrfs_fixup_helper, |
| 2021 | btrfs_writepage_fixup_worker, NULL, NULL); |
Chris Mason | 247e743 | 2008-07-17 12:53:51 -0400 | [diff] [blame] | 2022 | fixup->page = page; |
Qu Wenruo | dc6e320 | 2014-02-28 10:46:14 +0800 | [diff] [blame] | 2023 | btrfs_queue_work(root->fs_info->fixup_workers, &fixup->work); |
Jeff Mahoney | 87826df | 2012-02-15 16:23:57 +0100 | [diff] [blame] | 2024 | return -EBUSY; |
Chris Mason | 247e743 | 2008-07-17 12:53:51 -0400 | [diff] [blame] | 2025 | } |
| 2026 | |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 2027 | static int insert_reserved_file_extent(struct btrfs_trans_handle *trans, |
| 2028 | struct inode *inode, u64 file_pos, |
| 2029 | u64 disk_bytenr, u64 disk_num_bytes, |
| 2030 | u64 num_bytes, u64 ram_bytes, |
| 2031 | u8 compression, u8 encryption, |
| 2032 | u16 other_encoding, int extent_type) |
| 2033 | { |
| 2034 | struct btrfs_root *root = BTRFS_I(inode)->root; |
| 2035 | struct btrfs_file_extent_item *fi; |
| 2036 | struct btrfs_path *path; |
| 2037 | struct extent_buffer *leaf; |
| 2038 | struct btrfs_key ins; |
Filipe David Borba Manana | 1acae57 | 2014-01-07 11:42:27 +0000 | [diff] [blame] | 2039 | int extent_inserted = 0; |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 2040 | int ret; |
| 2041 | |
| 2042 | path = btrfs_alloc_path(); |
Mark Fasheh | d8926bb | 2011-07-13 10:38:47 -0700 | [diff] [blame] | 2043 | if (!path) |
| 2044 | return -ENOMEM; |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 2045 | |
Chris Mason | a1ed835 | 2009-09-11 12:27:37 -0400 | [diff] [blame] | 2046 | /* |
| 2047 | * we may be replacing one extent in the tree with another. |
| 2048 | * The new extent is pinned in the extent map, and we don't want |
| 2049 | * to drop it from the cache until it is completely in the btree. |
| 2050 | * |
| 2051 | * So, tell btrfs_drop_extents to leave this extent in the cache. |
| 2052 | * the caller is expected to unpin it and allow it to be merged |
| 2053 | * with the others. |
| 2054 | */ |
Filipe David Borba Manana | 1acae57 | 2014-01-07 11:42:27 +0000 | [diff] [blame] | 2055 | ret = __btrfs_drop_extents(trans, root, inode, path, file_pos, |
| 2056 | file_pos + num_bytes, NULL, 0, |
| 2057 | 1, sizeof(*fi), &extent_inserted); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 2058 | if (ret) |
| 2059 | goto out; |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 2060 | |
Filipe David Borba Manana | 1acae57 | 2014-01-07 11:42:27 +0000 | [diff] [blame] | 2061 | if (!extent_inserted) { |
| 2062 | ins.objectid = btrfs_ino(inode); |
| 2063 | ins.offset = file_pos; |
| 2064 | ins.type = BTRFS_EXTENT_DATA_KEY; |
| 2065 | |
| 2066 | path->leave_spinning = 1; |
| 2067 | ret = btrfs_insert_empty_item(trans, root, path, &ins, |
| 2068 | sizeof(*fi)); |
| 2069 | if (ret) |
| 2070 | goto out; |
| 2071 | } |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 2072 | leaf = path->nodes[0]; |
| 2073 | fi = btrfs_item_ptr(leaf, path->slots[0], |
| 2074 | struct btrfs_file_extent_item); |
| 2075 | btrfs_set_file_extent_generation(leaf, fi, trans->transid); |
| 2076 | btrfs_set_file_extent_type(leaf, fi, extent_type); |
| 2077 | btrfs_set_file_extent_disk_bytenr(leaf, fi, disk_bytenr); |
| 2078 | btrfs_set_file_extent_disk_num_bytes(leaf, fi, disk_num_bytes); |
| 2079 | btrfs_set_file_extent_offset(leaf, fi, 0); |
| 2080 | btrfs_set_file_extent_num_bytes(leaf, fi, num_bytes); |
| 2081 | btrfs_set_file_extent_ram_bytes(leaf, fi, ram_bytes); |
| 2082 | btrfs_set_file_extent_compression(leaf, fi, compression); |
| 2083 | btrfs_set_file_extent_encryption(leaf, fi, encryption); |
| 2084 | btrfs_set_file_extent_other_encoding(leaf, fi, other_encoding); |
Chris Mason | b947343 | 2009-03-13 11:00:37 -0400 | [diff] [blame] | 2085 | |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 2086 | btrfs_mark_buffer_dirty(leaf); |
Josef Bacik | ce19533 | 2012-09-25 15:26:16 -0400 | [diff] [blame] | 2087 | btrfs_release_path(path); |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 2088 | |
| 2089 | inode_add_bytes(inode, num_bytes); |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 2090 | |
| 2091 | ins.objectid = disk_bytenr; |
| 2092 | ins.offset = disk_num_bytes; |
| 2093 | ins.type = BTRFS_EXTENT_ITEM_KEY; |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 2094 | ret = btrfs_alloc_reserved_file_extent(trans, root, |
| 2095 | root->root_key.objectid, |
Li Zefan | 33345d01 | 2011-04-20 10:31:50 +0800 | [diff] [blame] | 2096 | btrfs_ino(inode), file_pos, &ins); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 2097 | out: |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 2098 | btrfs_free_path(path); |
Chris Mason | b947343 | 2009-03-13 11:00:37 -0400 | [diff] [blame] | 2099 | |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 2100 | return ret; |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 2101 | } |
| 2102 | |
Liu Bo | 38c227d | 2013-01-29 03:18:40 +0000 | [diff] [blame] | 2103 | /* snapshot-aware defrag */ |
| 2104 | struct sa_defrag_extent_backref { |
| 2105 | struct rb_node node; |
| 2106 | struct old_sa_defrag_extent *old; |
| 2107 | u64 root_id; |
| 2108 | u64 inum; |
| 2109 | u64 file_pos; |
| 2110 | u64 extent_offset; |
| 2111 | u64 num_bytes; |
| 2112 | u64 generation; |
| 2113 | }; |
| 2114 | |
| 2115 | struct old_sa_defrag_extent { |
| 2116 | struct list_head list; |
| 2117 | struct new_sa_defrag_extent *new; |
| 2118 | |
| 2119 | u64 extent_offset; |
| 2120 | u64 bytenr; |
| 2121 | u64 offset; |
| 2122 | u64 len; |
| 2123 | int count; |
| 2124 | }; |
| 2125 | |
| 2126 | struct new_sa_defrag_extent { |
| 2127 | struct rb_root root; |
| 2128 | struct list_head head; |
| 2129 | struct btrfs_path *path; |
| 2130 | struct inode *inode; |
| 2131 | u64 file_pos; |
| 2132 | u64 len; |
| 2133 | u64 bytenr; |
| 2134 | u64 disk_len; |
| 2135 | u8 compress_type; |
| 2136 | }; |
| 2137 | |
| 2138 | static int backref_comp(struct sa_defrag_extent_backref *b1, |
| 2139 | struct sa_defrag_extent_backref *b2) |
| 2140 | { |
| 2141 | if (b1->root_id < b2->root_id) |
| 2142 | return -1; |
| 2143 | else if (b1->root_id > b2->root_id) |
| 2144 | return 1; |
| 2145 | |
| 2146 | if (b1->inum < b2->inum) |
| 2147 | return -1; |
| 2148 | else if (b1->inum > b2->inum) |
| 2149 | return 1; |
| 2150 | |
| 2151 | if (b1->file_pos < b2->file_pos) |
| 2152 | return -1; |
| 2153 | else if (b1->file_pos > b2->file_pos) |
| 2154 | return 1; |
| 2155 | |
| 2156 | /* |
| 2157 | * [------------------------------] ===> (a range of space) |
| 2158 | * |<--->| |<---->| =============> (fs/file tree A) |
| 2159 | * |<---------------------------->| ===> (fs/file tree B) |
| 2160 | * |
| 2161 | * A range of space can refer to two file extents in one tree while |
| 2162 | * refer to only one file extent in another tree. |
| 2163 | * |
| 2164 | * So we may process a disk offset more than one time(two extents in A) |
| 2165 | * and locate at the same extent(one extent in B), then insert two same |
| 2166 | * backrefs(both refer to the extent in B). |
| 2167 | */ |
| 2168 | return 0; |
| 2169 | } |
| 2170 | |
| 2171 | static void backref_insert(struct rb_root *root, |
| 2172 | struct sa_defrag_extent_backref *backref) |
| 2173 | { |
| 2174 | struct rb_node **p = &root->rb_node; |
| 2175 | struct rb_node *parent = NULL; |
| 2176 | struct sa_defrag_extent_backref *entry; |
| 2177 | int ret; |
| 2178 | |
| 2179 | while (*p) { |
| 2180 | parent = *p; |
| 2181 | entry = rb_entry(parent, struct sa_defrag_extent_backref, node); |
| 2182 | |
| 2183 | ret = backref_comp(backref, entry); |
| 2184 | if (ret < 0) |
| 2185 | p = &(*p)->rb_left; |
| 2186 | else |
| 2187 | p = &(*p)->rb_right; |
| 2188 | } |
| 2189 | |
| 2190 | rb_link_node(&backref->node, parent, p); |
| 2191 | rb_insert_color(&backref->node, root); |
| 2192 | } |
| 2193 | |
| 2194 | /* |
| 2195 | * Note the backref might has changed, and in this case we just return 0. |
| 2196 | */ |
| 2197 | static noinline int record_one_backref(u64 inum, u64 offset, u64 root_id, |
| 2198 | void *ctx) |
| 2199 | { |
| 2200 | struct btrfs_file_extent_item *extent; |
| 2201 | struct btrfs_fs_info *fs_info; |
| 2202 | struct old_sa_defrag_extent *old = ctx; |
| 2203 | struct new_sa_defrag_extent *new = old->new; |
| 2204 | struct btrfs_path *path = new->path; |
| 2205 | struct btrfs_key key; |
| 2206 | struct btrfs_root *root; |
| 2207 | struct sa_defrag_extent_backref *backref; |
| 2208 | struct extent_buffer *leaf; |
| 2209 | struct inode *inode = new->inode; |
| 2210 | int slot; |
| 2211 | int ret; |
| 2212 | u64 extent_offset; |
| 2213 | u64 num_bytes; |
| 2214 | |
| 2215 | if (BTRFS_I(inode)->root->root_key.objectid == root_id && |
| 2216 | inum == btrfs_ino(inode)) |
| 2217 | return 0; |
| 2218 | |
| 2219 | key.objectid = root_id; |
| 2220 | key.type = BTRFS_ROOT_ITEM_KEY; |
| 2221 | key.offset = (u64)-1; |
| 2222 | |
| 2223 | fs_info = BTRFS_I(inode)->root->fs_info; |
| 2224 | root = btrfs_read_fs_root_no_name(fs_info, &key); |
| 2225 | if (IS_ERR(root)) { |
| 2226 | if (PTR_ERR(root) == -ENOENT) |
| 2227 | return 0; |
| 2228 | WARN_ON(1); |
| 2229 | pr_debug("inum=%llu, offset=%llu, root_id=%llu\n", |
| 2230 | inum, offset, root_id); |
| 2231 | return PTR_ERR(root); |
| 2232 | } |
| 2233 | |
| 2234 | key.objectid = inum; |
| 2235 | key.type = BTRFS_EXTENT_DATA_KEY; |
| 2236 | if (offset > (u64)-1 << 32) |
| 2237 | key.offset = 0; |
| 2238 | else |
| 2239 | key.offset = offset; |
| 2240 | |
| 2241 | ret = btrfs_search_slot(NULL, root, &key, path, 0, 0); |
Dulshani Gunawardhana | fae7f21 | 2013-10-31 10:30:08 +0530 | [diff] [blame] | 2242 | if (WARN_ON(ret < 0)) |
Liu Bo | 38c227d | 2013-01-29 03:18:40 +0000 | [diff] [blame] | 2243 | return ret; |
Josef Bacik | 50f1319 | 2013-07-22 12:50:37 -0400 | [diff] [blame] | 2244 | ret = 0; |
Liu Bo | 38c227d | 2013-01-29 03:18:40 +0000 | [diff] [blame] | 2245 | |
| 2246 | while (1) { |
| 2247 | cond_resched(); |
| 2248 | |
| 2249 | leaf = path->nodes[0]; |
| 2250 | slot = path->slots[0]; |
| 2251 | |
| 2252 | if (slot >= btrfs_header_nritems(leaf)) { |
| 2253 | ret = btrfs_next_leaf(root, path); |
| 2254 | if (ret < 0) { |
| 2255 | goto out; |
| 2256 | } else if (ret > 0) { |
| 2257 | ret = 0; |
| 2258 | goto out; |
| 2259 | } |
| 2260 | continue; |
| 2261 | } |
| 2262 | |
| 2263 | path->slots[0]++; |
| 2264 | |
| 2265 | btrfs_item_key_to_cpu(leaf, &key, slot); |
| 2266 | |
| 2267 | if (key.objectid > inum) |
| 2268 | goto out; |
| 2269 | |
| 2270 | if (key.objectid < inum || key.type != BTRFS_EXTENT_DATA_KEY) |
| 2271 | continue; |
| 2272 | |
| 2273 | extent = btrfs_item_ptr(leaf, slot, |
| 2274 | struct btrfs_file_extent_item); |
| 2275 | |
| 2276 | if (btrfs_file_extent_disk_bytenr(leaf, extent) != old->bytenr) |
| 2277 | continue; |
| 2278 | |
Liu Bo | e68afa4 | 2013-07-01 22:13:26 +0800 | [diff] [blame] | 2279 | /* |
| 2280 | * 'offset' refers to the exact key.offset, |
| 2281 | * NOT the 'offset' field in btrfs_extent_data_ref, ie. |
| 2282 | * (key.offset - extent_offset). |
| 2283 | */ |
| 2284 | if (key.offset != offset) |
Liu Bo | 38c227d | 2013-01-29 03:18:40 +0000 | [diff] [blame] | 2285 | continue; |
| 2286 | |
Liu Bo | e68afa4 | 2013-07-01 22:13:26 +0800 | [diff] [blame] | 2287 | extent_offset = btrfs_file_extent_offset(leaf, extent); |
Liu Bo | 38c227d | 2013-01-29 03:18:40 +0000 | [diff] [blame] | 2288 | num_bytes = btrfs_file_extent_num_bytes(leaf, extent); |
Liu Bo | e68afa4 | 2013-07-01 22:13:26 +0800 | [diff] [blame] | 2289 | |
Liu Bo | 38c227d | 2013-01-29 03:18:40 +0000 | [diff] [blame] | 2290 | if (extent_offset >= old->extent_offset + old->offset + |
| 2291 | old->len || extent_offset + num_bytes <= |
| 2292 | old->extent_offset + old->offset) |
| 2293 | continue; |
Liu Bo | 38c227d | 2013-01-29 03:18:40 +0000 | [diff] [blame] | 2294 | break; |
| 2295 | } |
| 2296 | |
| 2297 | backref = kmalloc(sizeof(*backref), GFP_NOFS); |
| 2298 | if (!backref) { |
| 2299 | ret = -ENOENT; |
| 2300 | goto out; |
| 2301 | } |
| 2302 | |
| 2303 | backref->root_id = root_id; |
| 2304 | backref->inum = inum; |
Liu Bo | e68afa4 | 2013-07-01 22:13:26 +0800 | [diff] [blame] | 2305 | backref->file_pos = offset; |
Liu Bo | 38c227d | 2013-01-29 03:18:40 +0000 | [diff] [blame] | 2306 | backref->num_bytes = num_bytes; |
| 2307 | backref->extent_offset = extent_offset; |
| 2308 | backref->generation = btrfs_file_extent_generation(leaf, extent); |
| 2309 | backref->old = old; |
| 2310 | backref_insert(&new->root, backref); |
| 2311 | old->count++; |
| 2312 | out: |
| 2313 | btrfs_release_path(path); |
| 2314 | WARN_ON(ret); |
| 2315 | return ret; |
| 2316 | } |
| 2317 | |
| 2318 | static noinline bool record_extent_backrefs(struct btrfs_path *path, |
| 2319 | struct new_sa_defrag_extent *new) |
| 2320 | { |
| 2321 | struct btrfs_fs_info *fs_info = BTRFS_I(new->inode)->root->fs_info; |
| 2322 | struct old_sa_defrag_extent *old, *tmp; |
| 2323 | int ret; |
| 2324 | |
| 2325 | new->path = path; |
| 2326 | |
| 2327 | list_for_each_entry_safe(old, tmp, &new->head, list) { |
Liu Bo | e68afa4 | 2013-07-01 22:13:26 +0800 | [diff] [blame] | 2328 | ret = iterate_inodes_from_logical(old->bytenr + |
| 2329 | old->extent_offset, fs_info, |
Liu Bo | 38c227d | 2013-01-29 03:18:40 +0000 | [diff] [blame] | 2330 | path, record_one_backref, |
| 2331 | old); |
Josef Bacik | 4724b10 | 2013-11-05 11:11:40 -0500 | [diff] [blame] | 2332 | if (ret < 0 && ret != -ENOENT) |
| 2333 | return false; |
Liu Bo | 38c227d | 2013-01-29 03:18:40 +0000 | [diff] [blame] | 2334 | |
| 2335 | /* no backref to be processed for this extent */ |
| 2336 | if (!old->count) { |
| 2337 | list_del(&old->list); |
| 2338 | kfree(old); |
| 2339 | } |
| 2340 | } |
| 2341 | |
| 2342 | if (list_empty(&new->head)) |
| 2343 | return false; |
| 2344 | |
| 2345 | return true; |
| 2346 | } |
| 2347 | |
| 2348 | static int relink_is_mergable(struct extent_buffer *leaf, |
| 2349 | struct btrfs_file_extent_item *fi, |
Liu Bo | 116e002 | 2013-08-02 16:30:40 +0800 | [diff] [blame] | 2350 | struct new_sa_defrag_extent *new) |
Liu Bo | 38c227d | 2013-01-29 03:18:40 +0000 | [diff] [blame] | 2351 | { |
Liu Bo | 116e002 | 2013-08-02 16:30:40 +0800 | [diff] [blame] | 2352 | if (btrfs_file_extent_disk_bytenr(leaf, fi) != new->bytenr) |
Liu Bo | 38c227d | 2013-01-29 03:18:40 +0000 | [diff] [blame] | 2353 | return 0; |
| 2354 | |
| 2355 | if (btrfs_file_extent_type(leaf, fi) != BTRFS_FILE_EXTENT_REG) |
| 2356 | return 0; |
| 2357 | |
Liu Bo | 116e002 | 2013-08-02 16:30:40 +0800 | [diff] [blame] | 2358 | if (btrfs_file_extent_compression(leaf, fi) != new->compress_type) |
| 2359 | return 0; |
| 2360 | |
| 2361 | if (btrfs_file_extent_encryption(leaf, fi) || |
Liu Bo | 38c227d | 2013-01-29 03:18:40 +0000 | [diff] [blame] | 2362 | btrfs_file_extent_other_encoding(leaf, fi)) |
| 2363 | return 0; |
| 2364 | |
| 2365 | return 1; |
| 2366 | } |
| 2367 | |
| 2368 | /* |
| 2369 | * Note the backref might has changed, and in this case we just return 0. |
| 2370 | */ |
| 2371 | static noinline int relink_extent_backref(struct btrfs_path *path, |
| 2372 | struct sa_defrag_extent_backref *prev, |
| 2373 | struct sa_defrag_extent_backref *backref) |
| 2374 | { |
| 2375 | struct btrfs_file_extent_item *extent; |
| 2376 | struct btrfs_file_extent_item *item; |
| 2377 | struct btrfs_ordered_extent *ordered; |
| 2378 | struct btrfs_trans_handle *trans; |
| 2379 | struct btrfs_fs_info *fs_info; |
| 2380 | struct btrfs_root *root; |
| 2381 | struct btrfs_key key; |
| 2382 | struct extent_buffer *leaf; |
| 2383 | struct old_sa_defrag_extent *old = backref->old; |
| 2384 | struct new_sa_defrag_extent *new = old->new; |
| 2385 | struct inode *src_inode = new->inode; |
| 2386 | struct inode *inode; |
| 2387 | struct extent_state *cached = NULL; |
| 2388 | int ret = 0; |
| 2389 | u64 start; |
| 2390 | u64 len; |
| 2391 | u64 lock_start; |
| 2392 | u64 lock_end; |
| 2393 | bool merge = false; |
| 2394 | int index; |
| 2395 | |
| 2396 | if (prev && prev->root_id == backref->root_id && |
| 2397 | prev->inum == backref->inum && |
| 2398 | prev->file_pos + prev->num_bytes == backref->file_pos) |
| 2399 | merge = true; |
| 2400 | |
| 2401 | /* step 1: get root */ |
| 2402 | key.objectid = backref->root_id; |
| 2403 | key.type = BTRFS_ROOT_ITEM_KEY; |
| 2404 | key.offset = (u64)-1; |
| 2405 | |
| 2406 | fs_info = BTRFS_I(src_inode)->root->fs_info; |
| 2407 | index = srcu_read_lock(&fs_info->subvol_srcu); |
| 2408 | |
| 2409 | root = btrfs_read_fs_root_no_name(fs_info, &key); |
| 2410 | if (IS_ERR(root)) { |
| 2411 | srcu_read_unlock(&fs_info->subvol_srcu, index); |
| 2412 | if (PTR_ERR(root) == -ENOENT) |
| 2413 | return 0; |
| 2414 | return PTR_ERR(root); |
| 2415 | } |
Liu Bo | 38c227d | 2013-01-29 03:18:40 +0000 | [diff] [blame] | 2416 | |
Wang Shilong | bcbba5e | 2014-02-08 23:46:35 +0800 | [diff] [blame] | 2417 | if (btrfs_root_readonly(root)) { |
| 2418 | srcu_read_unlock(&fs_info->subvol_srcu, index); |
| 2419 | return 0; |
| 2420 | } |
| 2421 | |
Liu Bo | 38c227d | 2013-01-29 03:18:40 +0000 | [diff] [blame] | 2422 | /* step 2: get inode */ |
| 2423 | key.objectid = backref->inum; |
| 2424 | key.type = BTRFS_INODE_ITEM_KEY; |
| 2425 | key.offset = 0; |
| 2426 | |
| 2427 | inode = btrfs_iget(fs_info->sb, &key, root, NULL); |
| 2428 | if (IS_ERR(inode)) { |
| 2429 | srcu_read_unlock(&fs_info->subvol_srcu, index); |
| 2430 | return 0; |
| 2431 | } |
| 2432 | |
| 2433 | srcu_read_unlock(&fs_info->subvol_srcu, index); |
| 2434 | |
| 2435 | /* step 3: relink backref */ |
| 2436 | lock_start = backref->file_pos; |
| 2437 | lock_end = backref->file_pos + backref->num_bytes - 1; |
| 2438 | lock_extent_bits(&BTRFS_I(inode)->io_tree, lock_start, lock_end, |
| 2439 | 0, &cached); |
| 2440 | |
| 2441 | ordered = btrfs_lookup_first_ordered_extent(inode, lock_end); |
| 2442 | if (ordered) { |
| 2443 | btrfs_put_ordered_extent(ordered); |
| 2444 | goto out_unlock; |
| 2445 | } |
| 2446 | |
| 2447 | trans = btrfs_join_transaction(root); |
| 2448 | if (IS_ERR(trans)) { |
| 2449 | ret = PTR_ERR(trans); |
| 2450 | goto out_unlock; |
| 2451 | } |
| 2452 | |
| 2453 | key.objectid = backref->inum; |
| 2454 | key.type = BTRFS_EXTENT_DATA_KEY; |
| 2455 | key.offset = backref->file_pos; |
| 2456 | |
| 2457 | ret = btrfs_search_slot(NULL, root, &key, path, 0, 0); |
| 2458 | if (ret < 0) { |
| 2459 | goto out_free_path; |
| 2460 | } else if (ret > 0) { |
| 2461 | ret = 0; |
| 2462 | goto out_free_path; |
| 2463 | } |
| 2464 | |
| 2465 | extent = btrfs_item_ptr(path->nodes[0], path->slots[0], |
| 2466 | struct btrfs_file_extent_item); |
| 2467 | |
| 2468 | if (btrfs_file_extent_generation(path->nodes[0], extent) != |
| 2469 | backref->generation) |
| 2470 | goto out_free_path; |
| 2471 | |
| 2472 | btrfs_release_path(path); |
| 2473 | |
| 2474 | start = backref->file_pos; |
| 2475 | if (backref->extent_offset < old->extent_offset + old->offset) |
| 2476 | start += old->extent_offset + old->offset - |
| 2477 | backref->extent_offset; |
| 2478 | |
| 2479 | len = min(backref->extent_offset + backref->num_bytes, |
| 2480 | old->extent_offset + old->offset + old->len); |
| 2481 | len -= max(backref->extent_offset, old->extent_offset + old->offset); |
| 2482 | |
| 2483 | ret = btrfs_drop_extents(trans, root, inode, start, |
| 2484 | start + len, 1); |
| 2485 | if (ret) |
| 2486 | goto out_free_path; |
| 2487 | again: |
| 2488 | key.objectid = btrfs_ino(inode); |
| 2489 | key.type = BTRFS_EXTENT_DATA_KEY; |
| 2490 | key.offset = start; |
| 2491 | |
Liu Bo | a09a0a7 | 2013-03-11 09:20:58 +0000 | [diff] [blame] | 2492 | path->leave_spinning = 1; |
Liu Bo | 38c227d | 2013-01-29 03:18:40 +0000 | [diff] [blame] | 2493 | if (merge) { |
| 2494 | struct btrfs_file_extent_item *fi; |
| 2495 | u64 extent_len; |
| 2496 | struct btrfs_key found_key; |
| 2497 | |
Gui Hecheng | 3c9665d | 2014-01-23 13:41:09 +0800 | [diff] [blame] | 2498 | ret = btrfs_search_slot(trans, root, &key, path, 0, 1); |
Liu Bo | 38c227d | 2013-01-29 03:18:40 +0000 | [diff] [blame] | 2499 | if (ret < 0) |
| 2500 | goto out_free_path; |
| 2501 | |
| 2502 | path->slots[0]--; |
| 2503 | leaf = path->nodes[0]; |
| 2504 | btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]); |
| 2505 | |
| 2506 | fi = btrfs_item_ptr(leaf, path->slots[0], |
| 2507 | struct btrfs_file_extent_item); |
| 2508 | extent_len = btrfs_file_extent_num_bytes(leaf, fi); |
| 2509 | |
Liu Bo | 116e002 | 2013-08-02 16:30:40 +0800 | [diff] [blame] | 2510 | if (extent_len + found_key.offset == start && |
| 2511 | relink_is_mergable(leaf, fi, new)) { |
Liu Bo | 38c227d | 2013-01-29 03:18:40 +0000 | [diff] [blame] | 2512 | btrfs_set_file_extent_num_bytes(leaf, fi, |
| 2513 | extent_len + len); |
| 2514 | btrfs_mark_buffer_dirty(leaf); |
| 2515 | inode_add_bytes(inode, len); |
| 2516 | |
| 2517 | ret = 1; |
| 2518 | goto out_free_path; |
| 2519 | } else { |
| 2520 | merge = false; |
| 2521 | btrfs_release_path(path); |
| 2522 | goto again; |
| 2523 | } |
| 2524 | } |
| 2525 | |
| 2526 | ret = btrfs_insert_empty_item(trans, root, path, &key, |
| 2527 | sizeof(*extent)); |
| 2528 | if (ret) { |
| 2529 | btrfs_abort_transaction(trans, root, ret); |
| 2530 | goto out_free_path; |
| 2531 | } |
| 2532 | |
| 2533 | leaf = path->nodes[0]; |
| 2534 | item = btrfs_item_ptr(leaf, path->slots[0], |
| 2535 | struct btrfs_file_extent_item); |
| 2536 | btrfs_set_file_extent_disk_bytenr(leaf, item, new->bytenr); |
| 2537 | btrfs_set_file_extent_disk_num_bytes(leaf, item, new->disk_len); |
| 2538 | btrfs_set_file_extent_offset(leaf, item, start - new->file_pos); |
| 2539 | btrfs_set_file_extent_num_bytes(leaf, item, len); |
| 2540 | btrfs_set_file_extent_ram_bytes(leaf, item, new->len); |
| 2541 | btrfs_set_file_extent_generation(leaf, item, trans->transid); |
| 2542 | btrfs_set_file_extent_type(leaf, item, BTRFS_FILE_EXTENT_REG); |
| 2543 | btrfs_set_file_extent_compression(leaf, item, new->compress_type); |
| 2544 | btrfs_set_file_extent_encryption(leaf, item, 0); |
| 2545 | btrfs_set_file_extent_other_encoding(leaf, item, 0); |
| 2546 | |
| 2547 | btrfs_mark_buffer_dirty(leaf); |
| 2548 | inode_add_bytes(inode, len); |
Liu Bo | a09a0a7 | 2013-03-11 09:20:58 +0000 | [diff] [blame] | 2549 | btrfs_release_path(path); |
Liu Bo | 38c227d | 2013-01-29 03:18:40 +0000 | [diff] [blame] | 2550 | |
| 2551 | ret = btrfs_inc_extent_ref(trans, root, new->bytenr, |
| 2552 | new->disk_len, 0, |
| 2553 | backref->root_id, backref->inum, |
| 2554 | new->file_pos, 0); /* start - extent_offset */ |
| 2555 | if (ret) { |
| 2556 | btrfs_abort_transaction(trans, root, ret); |
| 2557 | goto out_free_path; |
| 2558 | } |
| 2559 | |
| 2560 | ret = 1; |
| 2561 | out_free_path: |
| 2562 | btrfs_release_path(path); |
Liu Bo | a09a0a7 | 2013-03-11 09:20:58 +0000 | [diff] [blame] | 2563 | path->leave_spinning = 0; |
Liu Bo | 38c227d | 2013-01-29 03:18:40 +0000 | [diff] [blame] | 2564 | btrfs_end_transaction(trans, root); |
| 2565 | out_unlock: |
| 2566 | unlock_extent_cached(&BTRFS_I(inode)->io_tree, lock_start, lock_end, |
| 2567 | &cached, GFP_NOFS); |
| 2568 | iput(inode); |
| 2569 | return ret; |
| 2570 | } |
| 2571 | |
Liu Bo | 6f51956 | 2013-10-29 10:45:05 +0800 | [diff] [blame] | 2572 | static void free_sa_defrag_extent(struct new_sa_defrag_extent *new) |
| 2573 | { |
| 2574 | struct old_sa_defrag_extent *old, *tmp; |
| 2575 | |
| 2576 | if (!new) |
| 2577 | return; |
| 2578 | |
| 2579 | list_for_each_entry_safe(old, tmp, &new->head, list) { |
| 2580 | list_del(&old->list); |
| 2581 | kfree(old); |
| 2582 | } |
| 2583 | kfree(new); |
| 2584 | } |
| 2585 | |
Liu Bo | 38c227d | 2013-01-29 03:18:40 +0000 | [diff] [blame] | 2586 | static void relink_file_extents(struct new_sa_defrag_extent *new) |
| 2587 | { |
| 2588 | struct btrfs_path *path; |
Liu Bo | 38c227d | 2013-01-29 03:18:40 +0000 | [diff] [blame] | 2589 | struct sa_defrag_extent_backref *backref; |
| 2590 | struct sa_defrag_extent_backref *prev = NULL; |
| 2591 | struct inode *inode; |
| 2592 | struct btrfs_root *root; |
| 2593 | struct rb_node *node; |
| 2594 | int ret; |
| 2595 | |
| 2596 | inode = new->inode; |
| 2597 | root = BTRFS_I(inode)->root; |
| 2598 | |
| 2599 | path = btrfs_alloc_path(); |
| 2600 | if (!path) |
| 2601 | return; |
| 2602 | |
| 2603 | if (!record_extent_backrefs(path, new)) { |
| 2604 | btrfs_free_path(path); |
| 2605 | goto out; |
| 2606 | } |
| 2607 | btrfs_release_path(path); |
| 2608 | |
| 2609 | while (1) { |
| 2610 | node = rb_first(&new->root); |
| 2611 | if (!node) |
| 2612 | break; |
| 2613 | rb_erase(node, &new->root); |
| 2614 | |
| 2615 | backref = rb_entry(node, struct sa_defrag_extent_backref, node); |
| 2616 | |
| 2617 | ret = relink_extent_backref(path, prev, backref); |
| 2618 | WARN_ON(ret < 0); |
| 2619 | |
| 2620 | kfree(prev); |
| 2621 | |
| 2622 | if (ret == 1) |
| 2623 | prev = backref; |
| 2624 | else |
| 2625 | prev = NULL; |
| 2626 | cond_resched(); |
| 2627 | } |
| 2628 | kfree(prev); |
| 2629 | |
| 2630 | btrfs_free_path(path); |
Liu Bo | 38c227d | 2013-01-29 03:18:40 +0000 | [diff] [blame] | 2631 | out: |
Liu Bo | 6f51956 | 2013-10-29 10:45:05 +0800 | [diff] [blame] | 2632 | free_sa_defrag_extent(new); |
| 2633 | |
Liu Bo | 38c227d | 2013-01-29 03:18:40 +0000 | [diff] [blame] | 2634 | atomic_dec(&root->fs_info->defrag_running); |
| 2635 | wake_up(&root->fs_info->transaction_wait); |
Liu Bo | 38c227d | 2013-01-29 03:18:40 +0000 | [diff] [blame] | 2636 | } |
| 2637 | |
| 2638 | static struct new_sa_defrag_extent * |
| 2639 | record_old_file_extents(struct inode *inode, |
| 2640 | struct btrfs_ordered_extent *ordered) |
| 2641 | { |
| 2642 | struct btrfs_root *root = BTRFS_I(inode)->root; |
| 2643 | struct btrfs_path *path; |
| 2644 | struct btrfs_key key; |
Liu Bo | 6f51956 | 2013-10-29 10:45:05 +0800 | [diff] [blame] | 2645 | struct old_sa_defrag_extent *old; |
Liu Bo | 38c227d | 2013-01-29 03:18:40 +0000 | [diff] [blame] | 2646 | struct new_sa_defrag_extent *new; |
| 2647 | int ret; |
| 2648 | |
| 2649 | new = kmalloc(sizeof(*new), GFP_NOFS); |
| 2650 | if (!new) |
| 2651 | return NULL; |
| 2652 | |
| 2653 | new->inode = inode; |
| 2654 | new->file_pos = ordered->file_offset; |
| 2655 | new->len = ordered->len; |
| 2656 | new->bytenr = ordered->start; |
| 2657 | new->disk_len = ordered->disk_len; |
| 2658 | new->compress_type = ordered->compress_type; |
| 2659 | new->root = RB_ROOT; |
| 2660 | INIT_LIST_HEAD(&new->head); |
| 2661 | |
| 2662 | path = btrfs_alloc_path(); |
| 2663 | if (!path) |
| 2664 | goto out_kfree; |
| 2665 | |
| 2666 | key.objectid = btrfs_ino(inode); |
| 2667 | key.type = BTRFS_EXTENT_DATA_KEY; |
| 2668 | key.offset = new->file_pos; |
| 2669 | |
| 2670 | ret = btrfs_search_slot(NULL, root, &key, path, 0, 0); |
| 2671 | if (ret < 0) |
| 2672 | goto out_free_path; |
| 2673 | if (ret > 0 && path->slots[0] > 0) |
| 2674 | path->slots[0]--; |
| 2675 | |
| 2676 | /* find out all the old extents for the file range */ |
| 2677 | while (1) { |
| 2678 | struct btrfs_file_extent_item *extent; |
| 2679 | struct extent_buffer *l; |
| 2680 | int slot; |
| 2681 | u64 num_bytes; |
| 2682 | u64 offset; |
| 2683 | u64 end; |
| 2684 | u64 disk_bytenr; |
| 2685 | u64 extent_offset; |
| 2686 | |
| 2687 | l = path->nodes[0]; |
| 2688 | slot = path->slots[0]; |
| 2689 | |
| 2690 | if (slot >= btrfs_header_nritems(l)) { |
| 2691 | ret = btrfs_next_leaf(root, path); |
| 2692 | if (ret < 0) |
Liu Bo | 6f51956 | 2013-10-29 10:45:05 +0800 | [diff] [blame] | 2693 | goto out_free_path; |
Liu Bo | 38c227d | 2013-01-29 03:18:40 +0000 | [diff] [blame] | 2694 | else if (ret > 0) |
| 2695 | break; |
| 2696 | continue; |
| 2697 | } |
| 2698 | |
| 2699 | btrfs_item_key_to_cpu(l, &key, slot); |
| 2700 | |
| 2701 | if (key.objectid != btrfs_ino(inode)) |
| 2702 | break; |
| 2703 | if (key.type != BTRFS_EXTENT_DATA_KEY) |
| 2704 | break; |
| 2705 | if (key.offset >= new->file_pos + new->len) |
| 2706 | break; |
| 2707 | |
| 2708 | extent = btrfs_item_ptr(l, slot, struct btrfs_file_extent_item); |
| 2709 | |
| 2710 | num_bytes = btrfs_file_extent_num_bytes(l, extent); |
| 2711 | if (key.offset + num_bytes < new->file_pos) |
| 2712 | goto next; |
| 2713 | |
| 2714 | disk_bytenr = btrfs_file_extent_disk_bytenr(l, extent); |
| 2715 | if (!disk_bytenr) |
| 2716 | goto next; |
| 2717 | |
| 2718 | extent_offset = btrfs_file_extent_offset(l, extent); |
| 2719 | |
| 2720 | old = kmalloc(sizeof(*old), GFP_NOFS); |
| 2721 | if (!old) |
Liu Bo | 6f51956 | 2013-10-29 10:45:05 +0800 | [diff] [blame] | 2722 | goto out_free_path; |
Liu Bo | 38c227d | 2013-01-29 03:18:40 +0000 | [diff] [blame] | 2723 | |
| 2724 | offset = max(new->file_pos, key.offset); |
| 2725 | end = min(new->file_pos + new->len, key.offset + num_bytes); |
| 2726 | |
| 2727 | old->bytenr = disk_bytenr; |
| 2728 | old->extent_offset = extent_offset; |
| 2729 | old->offset = offset - key.offset; |
| 2730 | old->len = end - offset; |
| 2731 | old->new = new; |
| 2732 | old->count = 0; |
| 2733 | list_add_tail(&old->list, &new->head); |
| 2734 | next: |
| 2735 | path->slots[0]++; |
| 2736 | cond_resched(); |
| 2737 | } |
| 2738 | |
| 2739 | btrfs_free_path(path); |
| 2740 | atomic_inc(&root->fs_info->defrag_running); |
| 2741 | |
| 2742 | return new; |
| 2743 | |
Liu Bo | 38c227d | 2013-01-29 03:18:40 +0000 | [diff] [blame] | 2744 | out_free_path: |
| 2745 | btrfs_free_path(path); |
| 2746 | out_kfree: |
Liu Bo | 6f51956 | 2013-10-29 10:45:05 +0800 | [diff] [blame] | 2747 | free_sa_defrag_extent(new); |
Liu Bo | 38c227d | 2013-01-29 03:18:40 +0000 | [diff] [blame] | 2748 | return NULL; |
| 2749 | } |
| 2750 | |
Miao Xie | e570fd2 | 2014-06-19 10:42:50 +0800 | [diff] [blame] | 2751 | static void btrfs_release_delalloc_bytes(struct btrfs_root *root, |
| 2752 | u64 start, u64 len) |
| 2753 | { |
| 2754 | struct btrfs_block_group_cache *cache; |
| 2755 | |
| 2756 | cache = btrfs_lookup_block_group(root->fs_info, start); |
| 2757 | ASSERT(cache); |
| 2758 | |
| 2759 | spin_lock(&cache->lock); |
| 2760 | cache->delalloc_bytes -= len; |
| 2761 | spin_unlock(&cache->lock); |
| 2762 | |
| 2763 | btrfs_put_block_group(cache); |
| 2764 | } |
| 2765 | |
Chris Mason | d352ac6 | 2008-09-29 15:18:18 -0400 | [diff] [blame] | 2766 | /* as ordered data IO finishes, this gets called so we can finish |
| 2767 | * an ordered extent if the range of bytes in the file it covers are |
| 2768 | * fully written. |
| 2769 | */ |
Josef Bacik | 5fd0204 | 2012-05-02 14:00:54 -0400 | [diff] [blame] | 2770 | static int btrfs_finish_ordered_io(struct btrfs_ordered_extent *ordered_extent) |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 2771 | { |
Josef Bacik | 5fd0204 | 2012-05-02 14:00:54 -0400 | [diff] [blame] | 2772 | struct inode *inode = ordered_extent->inode; |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 2773 | struct btrfs_root *root = BTRFS_I(inode)->root; |
Yan, Zheng | 0ca1f7c | 2010-05-16 10:48:47 -0400 | [diff] [blame] | 2774 | struct btrfs_trans_handle *trans = NULL; |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 2775 | struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree; |
Josef Bacik | 2ac55d4 | 2010-02-03 19:33:23 +0000 | [diff] [blame] | 2776 | struct extent_state *cached_state = NULL; |
Liu Bo | 38c227d | 2013-01-29 03:18:40 +0000 | [diff] [blame] | 2777 | struct new_sa_defrag_extent *new = NULL; |
Li Zefan | 261507a0 | 2010-12-17 14:21:50 +0800 | [diff] [blame] | 2778 | int compress_type = 0; |
Josef Bacik | 77cef2e | 2013-08-29 13:57:21 -0400 | [diff] [blame] | 2779 | int ret = 0; |
| 2780 | u64 logical_len = ordered_extent->len; |
Li Zefan | 82d5902 | 2011-04-20 10:33:24 +0800 | [diff] [blame] | 2781 | bool nolock; |
Josef Bacik | 77cef2e | 2013-08-29 13:57:21 -0400 | [diff] [blame] | 2782 | bool truncated = false; |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 2783 | |
Liu Bo | 83eea1f | 2012-07-10 05:28:39 -0600 | [diff] [blame] | 2784 | nolock = btrfs_is_free_space_inode(inode); |
Josef Bacik | 0cb59c9 | 2010-07-02 12:14:14 -0400 | [diff] [blame] | 2785 | |
Josef Bacik | 5fd0204 | 2012-05-02 14:00:54 -0400 | [diff] [blame] | 2786 | if (test_bit(BTRFS_ORDERED_IOERR, &ordered_extent->flags)) { |
| 2787 | ret = -EIO; |
| 2788 | goto out; |
| 2789 | } |
| 2790 | |
Miao Xie | f612496 | 2014-09-12 18:44:04 +0800 | [diff] [blame] | 2791 | btrfs_free_io_failure_record(inode, ordered_extent->file_offset, |
| 2792 | ordered_extent->file_offset + |
| 2793 | ordered_extent->len - 1); |
| 2794 | |
Josef Bacik | 77cef2e | 2013-08-29 13:57:21 -0400 | [diff] [blame] | 2795 | if (test_bit(BTRFS_ORDERED_TRUNCATED, &ordered_extent->flags)) { |
| 2796 | truncated = true; |
| 2797 | logical_len = ordered_extent->truncated_len; |
| 2798 | /* Truncated the entire extent, don't bother adding */ |
| 2799 | if (!logical_len) |
| 2800 | goto out; |
| 2801 | } |
| 2802 | |
Yan, Zheng | c216775 | 2009-11-12 09:34:21 +0000 | [diff] [blame] | 2803 | if (test_bit(BTRFS_ORDERED_NOCOW, &ordered_extent->flags)) { |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 2804 | BUG_ON(!list_empty(&ordered_extent->list)); /* Logic error */ |
Josef Bacik | 6c760c0 | 2012-11-09 10:53:21 -0500 | [diff] [blame] | 2805 | btrfs_ordered_update_i_size(inode, 0, ordered_extent); |
| 2806 | if (nolock) |
| 2807 | trans = btrfs_join_transaction_nolock(root); |
| 2808 | else |
| 2809 | trans = btrfs_join_transaction(root); |
| 2810 | if (IS_ERR(trans)) { |
| 2811 | ret = PTR_ERR(trans); |
| 2812 | trans = NULL; |
| 2813 | goto out; |
Yan, Zheng | c216775 | 2009-11-12 09:34:21 +0000 | [diff] [blame] | 2814 | } |
Josef Bacik | 6c760c0 | 2012-11-09 10:53:21 -0500 | [diff] [blame] | 2815 | trans->block_rsv = &root->fs_info->delalloc_block_rsv; |
| 2816 | ret = btrfs_update_inode_fallback(trans, root, inode); |
| 2817 | if (ret) /* -ENOMEM or corruption */ |
| 2818 | btrfs_abort_transaction(trans, root, ret); |
Yan, Zheng | c216775 | 2009-11-12 09:34:21 +0000 | [diff] [blame] | 2819 | goto out; |
| 2820 | } |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 2821 | |
Josef Bacik | 2ac55d4 | 2010-02-03 19:33:23 +0000 | [diff] [blame] | 2822 | lock_extent_bits(io_tree, ordered_extent->file_offset, |
| 2823 | ordered_extent->file_offset + ordered_extent->len - 1, |
Jeff Mahoney | d008237 | 2012-03-01 14:57:19 +0100 | [diff] [blame] | 2824 | 0, &cached_state); |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 2825 | |
Liu Bo | 38c227d | 2013-01-29 03:18:40 +0000 | [diff] [blame] | 2826 | ret = test_range_bit(io_tree, ordered_extent->file_offset, |
| 2827 | ordered_extent->file_offset + ordered_extent->len - 1, |
| 2828 | EXTENT_DEFRAG, 1, cached_state); |
| 2829 | if (ret) { |
| 2830 | u64 last_snapshot = btrfs_root_last_snapshot(&root->root_item); |
Josef Bacik | 8101c8d | 2014-01-29 16:05:30 -0500 | [diff] [blame] | 2831 | if (0 && last_snapshot >= BTRFS_I(inode)->generation) |
Liu Bo | 38c227d | 2013-01-29 03:18:40 +0000 | [diff] [blame] | 2832 | /* the inode is shared */ |
| 2833 | new = record_old_file_extents(inode, ordered_extent); |
| 2834 | |
| 2835 | clear_extent_bit(io_tree, ordered_extent->file_offset, |
| 2836 | ordered_extent->file_offset + ordered_extent->len - 1, |
| 2837 | EXTENT_DEFRAG, 0, 0, &cached_state, GFP_NOFS); |
| 2838 | } |
| 2839 | |
Josef Bacik | 0cb59c9 | 2010-07-02 12:14:14 -0400 | [diff] [blame] | 2840 | if (nolock) |
Josef Bacik | 7a7eaa4 | 2011-04-13 12:54:33 -0400 | [diff] [blame] | 2841 | trans = btrfs_join_transaction_nolock(root); |
Josef Bacik | 0cb59c9 | 2010-07-02 12:14:14 -0400 | [diff] [blame] | 2842 | else |
Josef Bacik | 7a7eaa4 | 2011-04-13 12:54:33 -0400 | [diff] [blame] | 2843 | trans = btrfs_join_transaction(root); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 2844 | if (IS_ERR(trans)) { |
| 2845 | ret = PTR_ERR(trans); |
| 2846 | trans = NULL; |
| 2847 | goto out_unlock; |
| 2848 | } |
Chris Mason | a79b7d4 | 2014-05-22 16:18:52 -0700 | [diff] [blame] | 2849 | |
Yan, Zheng | 0ca1f7c | 2010-05-16 10:48:47 -0400 | [diff] [blame] | 2850 | trans->block_rsv = &root->fs_info->delalloc_block_rsv; |
Yan, Zheng | c216775 | 2009-11-12 09:34:21 +0000 | [diff] [blame] | 2851 | |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 2852 | if (test_bit(BTRFS_ORDERED_COMPRESSED, &ordered_extent->flags)) |
Li Zefan | 261507a0 | 2010-12-17 14:21:50 +0800 | [diff] [blame] | 2853 | compress_type = ordered_extent->compress_type; |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 2854 | if (test_bit(BTRFS_ORDERED_PREALLOC, &ordered_extent->flags)) { |
Li Zefan | 261507a0 | 2010-12-17 14:21:50 +0800 | [diff] [blame] | 2855 | BUG_ON(compress_type); |
Yan, Zheng | 920bbbf | 2009-11-12 09:34:08 +0000 | [diff] [blame] | 2856 | ret = btrfs_mark_extent_written(trans, inode, |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 2857 | ordered_extent->file_offset, |
| 2858 | ordered_extent->file_offset + |
Josef Bacik | 77cef2e | 2013-08-29 13:57:21 -0400 | [diff] [blame] | 2859 | logical_len); |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 2860 | } else { |
Josef Bacik | 0af3d00 | 2010-06-21 14:48:16 -0400 | [diff] [blame] | 2861 | BUG_ON(root == root->fs_info->tree_root); |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 2862 | ret = insert_reserved_file_extent(trans, inode, |
| 2863 | ordered_extent->file_offset, |
| 2864 | ordered_extent->start, |
| 2865 | ordered_extent->disk_len, |
Josef Bacik | 77cef2e | 2013-08-29 13:57:21 -0400 | [diff] [blame] | 2866 | logical_len, logical_len, |
Li Zefan | 261507a0 | 2010-12-17 14:21:50 +0800 | [diff] [blame] | 2867 | compress_type, 0, 0, |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 2868 | BTRFS_FILE_EXTENT_REG); |
Miao Xie | e570fd2 | 2014-06-19 10:42:50 +0800 | [diff] [blame] | 2869 | if (!ret) |
| 2870 | btrfs_release_delalloc_bytes(root, |
| 2871 | ordered_extent->start, |
| 2872 | ordered_extent->disk_len); |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 2873 | } |
Josef Bacik | 5dc562c | 2012-08-17 13:14:17 -0400 | [diff] [blame] | 2874 | unpin_extent_cache(&BTRFS_I(inode)->extent_tree, |
| 2875 | ordered_extent->file_offset, ordered_extent->len, |
| 2876 | trans->transid); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 2877 | if (ret < 0) { |
| 2878 | btrfs_abort_transaction(trans, root, ret); |
Josef Bacik | 5fd0204 | 2012-05-02 14:00:54 -0400 | [diff] [blame] | 2879 | goto out_unlock; |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 2880 | } |
Josef Bacik | 2ac55d4 | 2010-02-03 19:33:23 +0000 | [diff] [blame] | 2881 | |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 2882 | add_pending_csums(trans, inode, ordered_extent->file_offset, |
| 2883 | &ordered_extent->list); |
| 2884 | |
Josef Bacik | 6c760c0 | 2012-11-09 10:53:21 -0500 | [diff] [blame] | 2885 | btrfs_ordered_update_i_size(inode, 0, ordered_extent); |
| 2886 | ret = btrfs_update_inode_fallback(trans, root, inode); |
| 2887 | if (ret) { /* -ENOMEM or corruption */ |
| 2888 | btrfs_abort_transaction(trans, root, ret); |
| 2889 | goto out_unlock; |
Josef Bacik | 1ef30be | 2011-04-05 19:25:36 -0400 | [diff] [blame] | 2890 | } |
| 2891 | ret = 0; |
Josef Bacik | 5fd0204 | 2012-05-02 14:00:54 -0400 | [diff] [blame] | 2892 | out_unlock: |
| 2893 | unlock_extent_cached(io_tree, ordered_extent->file_offset, |
| 2894 | ordered_extent->file_offset + |
| 2895 | ordered_extent->len - 1, &cached_state, GFP_NOFS); |
Yan, Zheng | c216775 | 2009-11-12 09:34:21 +0000 | [diff] [blame] | 2896 | out: |
Josef Bacik | 5b0e95b | 2011-10-06 08:58:24 -0400 | [diff] [blame] | 2897 | if (root != root->fs_info->tree_root) |
Josef Bacik | 0cb59c9 | 2010-07-02 12:14:14 -0400 | [diff] [blame] | 2898 | btrfs_delalloc_release_metadata(inode, ordered_extent->len); |
Miao Xie | a698d075 | 2012-09-20 01:51:59 -0600 | [diff] [blame] | 2899 | if (trans) |
| 2900 | btrfs_end_transaction(trans, root); |
Josef Bacik | 0cb59c9 | 2010-07-02 12:14:14 -0400 | [diff] [blame] | 2901 | |
Josef Bacik | 77cef2e | 2013-08-29 13:57:21 -0400 | [diff] [blame] | 2902 | if (ret || truncated) { |
| 2903 | u64 start, end; |
| 2904 | |
| 2905 | if (truncated) |
| 2906 | start = ordered_extent->file_offset + logical_len; |
| 2907 | else |
| 2908 | start = ordered_extent->file_offset; |
| 2909 | end = ordered_extent->file_offset + ordered_extent->len - 1; |
| 2910 | clear_extent_uptodate(io_tree, start, end, NULL, GFP_NOFS); |
| 2911 | |
| 2912 | /* Drop the cache for the part of the extent we didn't write. */ |
| 2913 | btrfs_drop_extent_cache(inode, start, end, 0); |
Josef Bacik | 5fd0204 | 2012-05-02 14:00:54 -0400 | [diff] [blame] | 2914 | |
Josef Bacik | 0bec9ef | 2013-01-31 14:58:00 -0500 | [diff] [blame] | 2915 | /* |
| 2916 | * If the ordered extent had an IOERR or something else went |
| 2917 | * wrong we need to return the space for this ordered extent |
Josef Bacik | 77cef2e | 2013-08-29 13:57:21 -0400 | [diff] [blame] | 2918 | * back to the allocator. We only free the extent in the |
| 2919 | * truncated case if we didn't write out the extent at all. |
Josef Bacik | 0bec9ef | 2013-01-31 14:58:00 -0500 | [diff] [blame] | 2920 | */ |
Josef Bacik | 77cef2e | 2013-08-29 13:57:21 -0400 | [diff] [blame] | 2921 | if ((ret || !logical_len) && |
| 2922 | !test_bit(BTRFS_ORDERED_NOCOW, &ordered_extent->flags) && |
Josef Bacik | 0bec9ef | 2013-01-31 14:58:00 -0500 | [diff] [blame] | 2923 | !test_bit(BTRFS_ORDERED_PREALLOC, &ordered_extent->flags)) |
| 2924 | btrfs_free_reserved_extent(root, ordered_extent->start, |
Miao Xie | e570fd2 | 2014-06-19 10:42:50 +0800 | [diff] [blame] | 2925 | ordered_extent->disk_len, 1); |
Josef Bacik | 0bec9ef | 2013-01-31 14:58:00 -0500 | [diff] [blame] | 2926 | } |
| 2927 | |
| 2928 | |
Josef Bacik | 5fd0204 | 2012-05-02 14:00:54 -0400 | [diff] [blame] | 2929 | /* |
Liu Bo | 8bad3c0 | 2012-06-18 12:14:23 +0800 | [diff] [blame] | 2930 | * This needs to be done to make sure anybody waiting knows we are done |
| 2931 | * updating everything for this ordered extent. |
Josef Bacik | 5fd0204 | 2012-05-02 14:00:54 -0400 | [diff] [blame] | 2932 | */ |
| 2933 | btrfs_remove_ordered_extent(inode, ordered_extent); |
| 2934 | |
Liu Bo | 38c227d | 2013-01-29 03:18:40 +0000 | [diff] [blame] | 2935 | /* for snapshot-aware defrag */ |
Liu Bo | 6f51956 | 2013-10-29 10:45:05 +0800 | [diff] [blame] | 2936 | if (new) { |
| 2937 | if (ret) { |
| 2938 | free_sa_defrag_extent(new); |
| 2939 | atomic_dec(&root->fs_info->defrag_running); |
| 2940 | } else { |
| 2941 | relink_file_extents(new); |
| 2942 | } |
| 2943 | } |
Liu Bo | 38c227d | 2013-01-29 03:18:40 +0000 | [diff] [blame] | 2944 | |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 2945 | /* once for us */ |
| 2946 | btrfs_put_ordered_extent(ordered_extent); |
| 2947 | /* once for the tree */ |
| 2948 | btrfs_put_ordered_extent(ordered_extent); |
| 2949 | |
Josef Bacik | 5fd0204 | 2012-05-02 14:00:54 -0400 | [diff] [blame] | 2950 | return ret; |
| 2951 | } |
| 2952 | |
| 2953 | static void finish_ordered_fn(struct btrfs_work *work) |
| 2954 | { |
| 2955 | struct btrfs_ordered_extent *ordered_extent; |
| 2956 | ordered_extent = container_of(work, struct btrfs_ordered_extent, work); |
| 2957 | btrfs_finish_ordered_io(ordered_extent); |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 2958 | } |
| 2959 | |
Christoph Hellwig | b295086 | 2008-12-02 09:54:17 -0500 | [diff] [blame] | 2960 | 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] | 2961 | struct extent_state *state, int uptodate) |
| 2962 | { |
Josef Bacik | 5fd0204 | 2012-05-02 14:00:54 -0400 | [diff] [blame] | 2963 | struct inode *inode = page->mapping->host; |
| 2964 | struct btrfs_root *root = BTRFS_I(inode)->root; |
| 2965 | struct btrfs_ordered_extent *ordered_extent = NULL; |
Liu Bo | 9e0af23 | 2014-08-15 23:36:53 +0800 | [diff] [blame] | 2966 | struct btrfs_workqueue *wq; |
| 2967 | btrfs_work_func_t func; |
Josef Bacik | 5fd0204 | 2012-05-02 14:00:54 -0400 | [diff] [blame] | 2968 | |
liubo | 1abe9b8 | 2011-03-24 11:18:59 +0000 | [diff] [blame] | 2969 | trace_btrfs_writepage_end_io_hook(page, start, end, uptodate); |
| 2970 | |
Chris Mason | 8b62b72 | 2009-09-02 16:53:46 -0400 | [diff] [blame] | 2971 | ClearPagePrivate2(page); |
Josef Bacik | 5fd0204 | 2012-05-02 14:00:54 -0400 | [diff] [blame] | 2972 | if (!btrfs_dec_test_ordered_pending(inode, &ordered_extent, start, |
| 2973 | end - start + 1, uptodate)) |
| 2974 | return 0; |
| 2975 | |
Liu Bo | 9e0af23 | 2014-08-15 23:36:53 +0800 | [diff] [blame] | 2976 | if (btrfs_is_free_space_inode(inode)) { |
| 2977 | wq = root->fs_info->endio_freespace_worker; |
| 2978 | func = btrfs_freespace_write_helper; |
| 2979 | } else { |
| 2980 | wq = root->fs_info->endio_write_workers; |
| 2981 | func = btrfs_endio_write_helper; |
| 2982 | } |
Josef Bacik | 5fd0204 | 2012-05-02 14:00:54 -0400 | [diff] [blame] | 2983 | |
Liu Bo | 9e0af23 | 2014-08-15 23:36:53 +0800 | [diff] [blame] | 2984 | btrfs_init_work(&ordered_extent->work, func, finish_ordered_fn, NULL, |
| 2985 | NULL); |
| 2986 | btrfs_queue_work(wq, &ordered_extent->work); |
Josef Bacik | 5fd0204 | 2012-05-02 14:00:54 -0400 | [diff] [blame] | 2987 | |
| 2988 | return 0; |
Chris Mason | 211f90e | 2008-07-18 11:56:15 -0400 | [diff] [blame] | 2989 | } |
| 2990 | |
Miao Xie | dc380ae | 2014-09-12 18:43:55 +0800 | [diff] [blame] | 2991 | static int __readpage_endio_check(struct inode *inode, |
| 2992 | struct btrfs_io_bio *io_bio, |
| 2993 | int icsum, struct page *page, |
| 2994 | int pgoff, u64 start, size_t len) |
| 2995 | { |
| 2996 | char *kaddr; |
| 2997 | u32 csum_expected; |
| 2998 | u32 csum = ~(u32)0; |
| 2999 | static DEFINE_RATELIMIT_STATE(_rs, DEFAULT_RATELIMIT_INTERVAL, |
| 3000 | DEFAULT_RATELIMIT_BURST); |
| 3001 | |
| 3002 | csum_expected = *(((u32 *)io_bio->csum) + icsum); |
| 3003 | |
| 3004 | kaddr = kmap_atomic(page); |
| 3005 | csum = btrfs_csum_data(kaddr + pgoff, csum, len); |
| 3006 | btrfs_csum_final(csum, (char *)&csum); |
| 3007 | if (csum != csum_expected) |
| 3008 | goto zeroit; |
| 3009 | |
| 3010 | kunmap_atomic(kaddr); |
| 3011 | return 0; |
| 3012 | zeroit: |
| 3013 | if (__ratelimit(&_rs)) |
David Sterba | f0954c6 | 2014-12-19 18:38:44 +0100 | [diff] [blame] | 3014 | btrfs_warn(BTRFS_I(inode)->root->fs_info, |
Miao Xie | dc380ae | 2014-09-12 18:43:55 +0800 | [diff] [blame] | 3015 | "csum failed ino %llu off %llu csum %u expected csum %u", |
| 3016 | btrfs_ino(inode), start, csum, csum_expected); |
| 3017 | memset(kaddr + pgoff, 1, len); |
| 3018 | flush_dcache_page(page); |
| 3019 | kunmap_atomic(kaddr); |
| 3020 | if (csum_expected == 0) |
| 3021 | return 0; |
| 3022 | return -EIO; |
| 3023 | } |
| 3024 | |
Chris Mason | d352ac6 | 2008-09-29 15:18:18 -0400 | [diff] [blame] | 3025 | /* |
Chris Mason | d352ac6 | 2008-09-29 15:18:18 -0400 | [diff] [blame] | 3026 | * 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] | 3027 | * if there's a match, we allow the bio to finish. If not, the code in |
| 3028 | * extent_io.c will try to find good copies for us. |
Chris Mason | d352ac6 | 2008-09-29 15:18:18 -0400 | [diff] [blame] | 3029 | */ |
Miao Xie | facc8a22 | 2013-07-25 19:22:34 +0800 | [diff] [blame] | 3030 | static int btrfs_readpage_end_io_hook(struct btrfs_io_bio *io_bio, |
| 3031 | u64 phy_offset, struct page *page, |
| 3032 | u64 start, u64 end, int mirror) |
Chris Mason | 07157aa | 2007-08-30 08:50:51 -0400 | [diff] [blame] | 3033 | { |
Miao Xie | 4eee4fa | 2012-12-21 09:17:45 +0000 | [diff] [blame] | 3034 | size_t offset = start - page_offset(page); |
Chris Mason | 07157aa | 2007-08-30 08:50:51 -0400 | [diff] [blame] | 3035 | struct inode *inode = page->mapping->host; |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 3036 | struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree; |
Chris Mason | ff79f81 | 2007-10-15 16:22:25 -0400 | [diff] [blame] | 3037 | struct btrfs_root *root = BTRFS_I(inode)->root; |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 3038 | |
Chris Mason | d20f704 | 2008-12-08 16:58:54 -0500 | [diff] [blame] | 3039 | if (PageChecked(page)) { |
| 3040 | ClearPageChecked(page); |
Miao Xie | dc380ae | 2014-09-12 18:43:55 +0800 | [diff] [blame] | 3041 | return 0; |
Chris Mason | d20f704 | 2008-12-08 16:58:54 -0500 | [diff] [blame] | 3042 | } |
Christoph Hellwig | 6cbff00 | 2009-04-17 10:37:41 +0200 | [diff] [blame] | 3043 | |
| 3044 | if (BTRFS_I(inode)->flags & BTRFS_INODE_NODATASUM) |
Miao Xie | dc380ae | 2014-09-12 18:43:55 +0800 | [diff] [blame] | 3045 | return 0; |
Chris Mason | d20f704 | 2008-12-08 16:58:54 -0500 | [diff] [blame] | 3046 | |
Yan Zheng | 17d217f | 2008-12-12 10:03:38 -0500 | [diff] [blame] | 3047 | if (root->root_key.objectid == BTRFS_DATA_RELOC_TREE_OBJECTID && |
Chris Mason | 9655d29 | 2009-09-02 15:22:30 -0400 | [diff] [blame] | 3048 | test_range_bit(io_tree, start, end, EXTENT_NODATASUM, 1, NULL)) { |
Yan Zheng | 17d217f | 2008-12-12 10:03:38 -0500 | [diff] [blame] | 3049 | clear_extent_bits(io_tree, start, end, EXTENT_NODATASUM, |
| 3050 | GFP_NOFS); |
| 3051 | return 0; |
| 3052 | } |
| 3053 | |
Miao Xie | facc8a22 | 2013-07-25 19:22:34 +0800 | [diff] [blame] | 3054 | phy_offset >>= inode->i_sb->s_blocksize_bits; |
Miao Xie | dc380ae | 2014-09-12 18:43:55 +0800 | [diff] [blame] | 3055 | return __readpage_endio_check(inode, io_bio, phy_offset, page, offset, |
| 3056 | start, (size_t)(end - start + 1)); |
Chris Mason | 07157aa | 2007-08-30 08:50:51 -0400 | [diff] [blame] | 3057 | } |
Chris Mason | b888db2 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 3058 | |
Yan, Zheng | 24bbcf0 | 2009-11-12 09:36:34 +0000 | [diff] [blame] | 3059 | struct delayed_iput { |
| 3060 | struct list_head list; |
| 3061 | struct inode *inode; |
| 3062 | }; |
| 3063 | |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 3064 | /* JDM: If this is fs-wide, why can't we add a pointer to |
| 3065 | * btrfs_inode instead and avoid the allocation? */ |
Yan, Zheng | 24bbcf0 | 2009-11-12 09:36:34 +0000 | [diff] [blame] | 3066 | void btrfs_add_delayed_iput(struct inode *inode) |
| 3067 | { |
| 3068 | struct btrfs_fs_info *fs_info = BTRFS_I(inode)->root->fs_info; |
| 3069 | struct delayed_iput *delayed; |
| 3070 | |
| 3071 | if (atomic_add_unless(&inode->i_count, -1, 1)) |
| 3072 | return; |
| 3073 | |
| 3074 | delayed = kmalloc(sizeof(*delayed), GFP_NOFS | __GFP_NOFAIL); |
| 3075 | delayed->inode = inode; |
| 3076 | |
| 3077 | spin_lock(&fs_info->delayed_iput_lock); |
| 3078 | list_add_tail(&delayed->list, &fs_info->delayed_iputs); |
| 3079 | spin_unlock(&fs_info->delayed_iput_lock); |
| 3080 | } |
| 3081 | |
| 3082 | void btrfs_run_delayed_iputs(struct btrfs_root *root) |
| 3083 | { |
| 3084 | LIST_HEAD(list); |
| 3085 | struct btrfs_fs_info *fs_info = root->fs_info; |
| 3086 | struct delayed_iput *delayed; |
| 3087 | int empty; |
| 3088 | |
| 3089 | spin_lock(&fs_info->delayed_iput_lock); |
| 3090 | empty = list_empty(&fs_info->delayed_iputs); |
| 3091 | spin_unlock(&fs_info->delayed_iput_lock); |
| 3092 | if (empty) |
| 3093 | return; |
| 3094 | |
Yan, Zheng | 24bbcf0 | 2009-11-12 09:36:34 +0000 | [diff] [blame] | 3095 | spin_lock(&fs_info->delayed_iput_lock); |
| 3096 | list_splice_init(&fs_info->delayed_iputs, &list); |
| 3097 | spin_unlock(&fs_info->delayed_iput_lock); |
| 3098 | |
| 3099 | while (!list_empty(&list)) { |
| 3100 | delayed = list_entry(list.next, struct delayed_iput, list); |
| 3101 | list_del(&delayed->list); |
| 3102 | iput(delayed->inode); |
| 3103 | kfree(delayed); |
| 3104 | } |
Yan, Zheng | 24bbcf0 | 2009-11-12 09:36:34 +0000 | [diff] [blame] | 3105 | } |
| 3106 | |
Yan, Zheng | d68fc57 | 2010-05-16 10:49:58 -0400 | [diff] [blame] | 3107 | /* |
Justin P. Mattock | 42b2aa8 | 2011-11-28 20:31:00 -0800 | [diff] [blame] | 3108 | * This is called in transaction commit time. If there are no orphan |
Yan, Zheng | d68fc57 | 2010-05-16 10:49:58 -0400 | [diff] [blame] | 3109 | * files in the subvolume, it removes orphan item and frees block_rsv |
| 3110 | * structure. |
| 3111 | */ |
| 3112 | void btrfs_orphan_commit_root(struct btrfs_trans_handle *trans, |
| 3113 | struct btrfs_root *root) |
| 3114 | { |
Josef Bacik | 90290e1 | 2011-12-02 15:44:12 -0500 | [diff] [blame] | 3115 | struct btrfs_block_rsv *block_rsv; |
Yan, Zheng | d68fc57 | 2010-05-16 10:49:58 -0400 | [diff] [blame] | 3116 | int ret; |
| 3117 | |
Josef Bacik | 8a35d95 | 2012-05-23 14:26:42 -0400 | [diff] [blame] | 3118 | if (atomic_read(&root->orphan_inodes) || |
Yan, Zheng | d68fc57 | 2010-05-16 10:49:58 -0400 | [diff] [blame] | 3119 | root->orphan_cleanup_state != ORPHAN_CLEANUP_DONE) |
| 3120 | return; |
| 3121 | |
Josef Bacik | 90290e1 | 2011-12-02 15:44:12 -0500 | [diff] [blame] | 3122 | spin_lock(&root->orphan_lock); |
Josef Bacik | 8a35d95 | 2012-05-23 14:26:42 -0400 | [diff] [blame] | 3123 | if (atomic_read(&root->orphan_inodes)) { |
Josef Bacik | 90290e1 | 2011-12-02 15:44:12 -0500 | [diff] [blame] | 3124 | spin_unlock(&root->orphan_lock); |
| 3125 | return; |
| 3126 | } |
| 3127 | |
| 3128 | if (root->orphan_cleanup_state != ORPHAN_CLEANUP_DONE) { |
| 3129 | spin_unlock(&root->orphan_lock); |
| 3130 | return; |
| 3131 | } |
| 3132 | |
| 3133 | block_rsv = root->orphan_block_rsv; |
| 3134 | root->orphan_block_rsv = NULL; |
| 3135 | spin_unlock(&root->orphan_lock); |
| 3136 | |
Miao Xie | 27cdeb7 | 2014-04-02 19:51:05 +0800 | [diff] [blame] | 3137 | if (test_bit(BTRFS_ROOT_ORPHAN_ITEM_INSERTED, &root->state) && |
Yan, Zheng | d68fc57 | 2010-05-16 10:49:58 -0400 | [diff] [blame] | 3138 | btrfs_root_refs(&root->root_item) > 0) { |
| 3139 | ret = btrfs_del_orphan_item(trans, root->fs_info->tree_root, |
| 3140 | root->root_key.objectid); |
Josef Bacik | 4ef31a4 | 2013-08-13 14:10:08 -0400 | [diff] [blame] | 3141 | if (ret) |
| 3142 | btrfs_abort_transaction(trans, root, ret); |
| 3143 | else |
Miao Xie | 27cdeb7 | 2014-04-02 19:51:05 +0800 | [diff] [blame] | 3144 | clear_bit(BTRFS_ROOT_ORPHAN_ITEM_INSERTED, |
| 3145 | &root->state); |
Yan, Zheng | d68fc57 | 2010-05-16 10:49:58 -0400 | [diff] [blame] | 3146 | } |
| 3147 | |
Josef Bacik | 90290e1 | 2011-12-02 15:44:12 -0500 | [diff] [blame] | 3148 | if (block_rsv) { |
| 3149 | WARN_ON(block_rsv->size > 0); |
| 3150 | btrfs_free_block_rsv(root, block_rsv); |
Yan, Zheng | d68fc57 | 2010-05-16 10:49:58 -0400 | [diff] [blame] | 3151 | } |
| 3152 | } |
| 3153 | |
| 3154 | /* |
Josef Bacik | 7b12876 | 2008-07-24 12:17:14 -0400 | [diff] [blame] | 3155 | * This creates an orphan entry for the given inode in case something goes |
| 3156 | * wrong in the middle of an unlink/truncate. |
Yan, Zheng | d68fc57 | 2010-05-16 10:49:58 -0400 | [diff] [blame] | 3157 | * |
| 3158 | * NOTE: caller of this function should reserve 5 units of metadata for |
| 3159 | * this function. |
Josef Bacik | 7b12876 | 2008-07-24 12:17:14 -0400 | [diff] [blame] | 3160 | */ |
| 3161 | int btrfs_orphan_add(struct btrfs_trans_handle *trans, struct inode *inode) |
| 3162 | { |
| 3163 | struct btrfs_root *root = BTRFS_I(inode)->root; |
Yan, Zheng | d68fc57 | 2010-05-16 10:49:58 -0400 | [diff] [blame] | 3164 | struct btrfs_block_rsv *block_rsv = NULL; |
| 3165 | int reserve = 0; |
| 3166 | int insert = 0; |
| 3167 | int ret; |
Josef Bacik | 7b12876 | 2008-07-24 12:17:14 -0400 | [diff] [blame] | 3168 | |
Yan, Zheng | d68fc57 | 2010-05-16 10:49:58 -0400 | [diff] [blame] | 3169 | if (!root->orphan_block_rsv) { |
Miao Xie | 66d8f3d | 2012-09-06 04:02:28 -0600 | [diff] [blame] | 3170 | block_rsv = btrfs_alloc_block_rsv(root, BTRFS_BLOCK_RSV_TEMP); |
Tsutomu Itoh | b532402 | 2011-07-19 07:27:20 +0000 | [diff] [blame] | 3171 | if (!block_rsv) |
| 3172 | return -ENOMEM; |
Josef Bacik | 7b12876 | 2008-07-24 12:17:14 -0400 | [diff] [blame] | 3173 | } |
| 3174 | |
Yan, Zheng | d68fc57 | 2010-05-16 10:49:58 -0400 | [diff] [blame] | 3175 | spin_lock(&root->orphan_lock); |
| 3176 | if (!root->orphan_block_rsv) { |
| 3177 | root->orphan_block_rsv = block_rsv; |
| 3178 | } else if (block_rsv) { |
| 3179 | btrfs_free_block_rsv(root, block_rsv); |
| 3180 | block_rsv = NULL; |
| 3181 | } |
Josef Bacik | 7b12876 | 2008-07-24 12:17:14 -0400 | [diff] [blame] | 3182 | |
Josef Bacik | 8a35d95 | 2012-05-23 14:26:42 -0400 | [diff] [blame] | 3183 | if (!test_and_set_bit(BTRFS_INODE_HAS_ORPHAN_ITEM, |
| 3184 | &BTRFS_I(inode)->runtime_flags)) { |
Yan, Zheng | d68fc57 | 2010-05-16 10:49:58 -0400 | [diff] [blame] | 3185 | #if 0 |
| 3186 | /* |
| 3187 | * For proper ENOSPC handling, we should do orphan |
| 3188 | * cleanup when mounting. But this introduces backward |
| 3189 | * compatibility issue. |
| 3190 | */ |
| 3191 | if (!xchg(&root->orphan_item_inserted, 1)) |
| 3192 | insert = 2; |
| 3193 | else |
| 3194 | insert = 1; |
| 3195 | #endif |
| 3196 | insert = 1; |
Miao Xie | 321f0e7 | 2012-08-28 22:13:02 -0600 | [diff] [blame] | 3197 | atomic_inc(&root->orphan_inodes); |
Yan, Zheng | d68fc57 | 2010-05-16 10:49:58 -0400 | [diff] [blame] | 3198 | } |
Josef Bacik | 7b12876 | 2008-07-24 12:17:14 -0400 | [diff] [blame] | 3199 | |
Josef Bacik | 72ac3c0 | 2012-05-23 14:13:11 -0400 | [diff] [blame] | 3200 | if (!test_and_set_bit(BTRFS_INODE_ORPHAN_META_RESERVED, |
| 3201 | &BTRFS_I(inode)->runtime_flags)) |
Yan, Zheng | d68fc57 | 2010-05-16 10:49:58 -0400 | [diff] [blame] | 3202 | reserve = 1; |
Yan, Zheng | d68fc57 | 2010-05-16 10:49:58 -0400 | [diff] [blame] | 3203 | spin_unlock(&root->orphan_lock); |
Josef Bacik | 7b12876 | 2008-07-24 12:17:14 -0400 | [diff] [blame] | 3204 | |
Yan, Zheng | d68fc57 | 2010-05-16 10:49:58 -0400 | [diff] [blame] | 3205 | /* grab metadata reservation from transaction handle */ |
| 3206 | if (reserve) { |
| 3207 | ret = btrfs_orphan_reserve_metadata(trans, inode); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 3208 | BUG_ON(ret); /* -ENOSPC in reservation; Logic error? JDM */ |
Yan, Zheng | d68fc57 | 2010-05-16 10:49:58 -0400 | [diff] [blame] | 3209 | } |
| 3210 | |
| 3211 | /* insert an orphan item to track this unlinked/truncated file */ |
| 3212 | if (insert >= 1) { |
Li Zefan | 33345d01 | 2011-04-20 10:31:50 +0800 | [diff] [blame] | 3213 | ret = btrfs_insert_orphan_item(trans, root, btrfs_ino(inode)); |
Josef Bacik | 4ef31a4 | 2013-08-13 14:10:08 -0400 | [diff] [blame] | 3214 | if (ret) { |
Filipe David Borba Manana | 703c88e | 2013-09-22 21:54:55 +0100 | [diff] [blame] | 3215 | atomic_dec(&root->orphan_inodes); |
Josef Bacik | 4ef31a4 | 2013-08-13 14:10:08 -0400 | [diff] [blame] | 3216 | if (reserve) { |
| 3217 | clear_bit(BTRFS_INODE_ORPHAN_META_RESERVED, |
| 3218 | &BTRFS_I(inode)->runtime_flags); |
| 3219 | btrfs_orphan_release_metadata(inode); |
| 3220 | } |
| 3221 | if (ret != -EEXIST) { |
Josef Bacik | e8e7cff | 2013-08-21 15:54:00 -0400 | [diff] [blame] | 3222 | clear_bit(BTRFS_INODE_HAS_ORPHAN_ITEM, |
| 3223 | &BTRFS_I(inode)->runtime_flags); |
Josef Bacik | 4ef31a4 | 2013-08-13 14:10:08 -0400 | [diff] [blame] | 3224 | btrfs_abort_transaction(trans, root, ret); |
| 3225 | return ret; |
| 3226 | } |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 3227 | } |
| 3228 | ret = 0; |
Yan, Zheng | d68fc57 | 2010-05-16 10:49:58 -0400 | [diff] [blame] | 3229 | } |
| 3230 | |
| 3231 | /* insert an orphan item to track subvolume contains orphan files */ |
| 3232 | if (insert >= 2) { |
| 3233 | ret = btrfs_insert_orphan_item(trans, root->fs_info->tree_root, |
| 3234 | root->root_key.objectid); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 3235 | if (ret && ret != -EEXIST) { |
| 3236 | btrfs_abort_transaction(trans, root, ret); |
| 3237 | return ret; |
| 3238 | } |
Yan, Zheng | d68fc57 | 2010-05-16 10:49:58 -0400 | [diff] [blame] | 3239 | } |
| 3240 | return 0; |
Josef Bacik | 7b12876 | 2008-07-24 12:17:14 -0400 | [diff] [blame] | 3241 | } |
| 3242 | |
| 3243 | /* |
| 3244 | * We have done the truncate/delete so we can go ahead and remove the orphan |
| 3245 | * item for this particular inode. |
| 3246 | */ |
Eric Sandeen | 48a3b63 | 2013-04-25 20:41:01 +0000 | [diff] [blame] | 3247 | static int btrfs_orphan_del(struct btrfs_trans_handle *trans, |
| 3248 | struct inode *inode) |
Josef Bacik | 7b12876 | 2008-07-24 12:17:14 -0400 | [diff] [blame] | 3249 | { |
| 3250 | struct btrfs_root *root = BTRFS_I(inode)->root; |
Yan, Zheng | d68fc57 | 2010-05-16 10:49:58 -0400 | [diff] [blame] | 3251 | int delete_item = 0; |
| 3252 | int release_rsv = 0; |
Josef Bacik | 7b12876 | 2008-07-24 12:17:14 -0400 | [diff] [blame] | 3253 | int ret = 0; |
| 3254 | |
Yan, Zheng | d68fc57 | 2010-05-16 10:49:58 -0400 | [diff] [blame] | 3255 | spin_lock(&root->orphan_lock); |
Josef Bacik | 8a35d95 | 2012-05-23 14:26:42 -0400 | [diff] [blame] | 3256 | if (test_and_clear_bit(BTRFS_INODE_HAS_ORPHAN_ITEM, |
| 3257 | &BTRFS_I(inode)->runtime_flags)) |
Yan, Zheng | d68fc57 | 2010-05-16 10:49:58 -0400 | [diff] [blame] | 3258 | delete_item = 1; |
Josef Bacik | 7b12876 | 2008-07-24 12:17:14 -0400 | [diff] [blame] | 3259 | |
Josef Bacik | 72ac3c0 | 2012-05-23 14:13:11 -0400 | [diff] [blame] | 3260 | if (test_and_clear_bit(BTRFS_INODE_ORPHAN_META_RESERVED, |
| 3261 | &BTRFS_I(inode)->runtime_flags)) |
Yan, Zheng | d68fc57 | 2010-05-16 10:49:58 -0400 | [diff] [blame] | 3262 | release_rsv = 1; |
Yan, Zheng | d68fc57 | 2010-05-16 10:49:58 -0400 | [diff] [blame] | 3263 | spin_unlock(&root->orphan_lock); |
| 3264 | |
Filipe David Borba Manana | 703c88e | 2013-09-22 21:54:55 +0100 | [diff] [blame] | 3265 | if (delete_item) { |
Josef Bacik | 8a35d95 | 2012-05-23 14:26:42 -0400 | [diff] [blame] | 3266 | atomic_dec(&root->orphan_inodes); |
Filipe David Borba Manana | 703c88e | 2013-09-22 21:54:55 +0100 | [diff] [blame] | 3267 | if (trans) |
| 3268 | ret = btrfs_del_orphan_item(trans, root, |
| 3269 | btrfs_ino(inode)); |
Josef Bacik | 8a35d95 | 2012-05-23 14:26:42 -0400 | [diff] [blame] | 3270 | } |
Josef Bacik | 7b12876 | 2008-07-24 12:17:14 -0400 | [diff] [blame] | 3271 | |
Filipe David Borba Manana | 703c88e | 2013-09-22 21:54:55 +0100 | [diff] [blame] | 3272 | if (release_rsv) |
| 3273 | btrfs_orphan_release_metadata(inode); |
| 3274 | |
Josef Bacik | 4ef31a4 | 2013-08-13 14:10:08 -0400 | [diff] [blame] | 3275 | return ret; |
Josef Bacik | 7b12876 | 2008-07-24 12:17:14 -0400 | [diff] [blame] | 3276 | } |
| 3277 | |
| 3278 | /* |
| 3279 | * this cleans up any orphans that may be left on the list from the last use |
| 3280 | * of this root. |
| 3281 | */ |
Josef Bacik | 66b4ffd | 2011-01-31 16:22:42 -0500 | [diff] [blame] | 3282 | int btrfs_orphan_cleanup(struct btrfs_root *root) |
Josef Bacik | 7b12876 | 2008-07-24 12:17:14 -0400 | [diff] [blame] | 3283 | { |
| 3284 | struct btrfs_path *path; |
| 3285 | struct extent_buffer *leaf; |
Josef Bacik | 7b12876 | 2008-07-24 12:17:14 -0400 | [diff] [blame] | 3286 | struct btrfs_key key, found_key; |
| 3287 | struct btrfs_trans_handle *trans; |
| 3288 | struct inode *inode; |
Josef Bacik | 8f6d7f4 | 2011-09-26 15:55:20 -0400 | [diff] [blame] | 3289 | u64 last_objectid = 0; |
Josef Bacik | 7b12876 | 2008-07-24 12:17:14 -0400 | [diff] [blame] | 3290 | int ret = 0, nr_unlink = 0, nr_truncate = 0; |
| 3291 | |
Yan, Zheng | d68fc57 | 2010-05-16 10:49:58 -0400 | [diff] [blame] | 3292 | if (cmpxchg(&root->orphan_cleanup_state, 0, ORPHAN_CLEANUP_STARTED)) |
Josef Bacik | 66b4ffd | 2011-01-31 16:22:42 -0500 | [diff] [blame] | 3293 | return 0; |
Yan, Zheng | c71bf09 | 2009-11-12 09:34:40 +0000 | [diff] [blame] | 3294 | |
| 3295 | path = btrfs_alloc_path(); |
Josef Bacik | 66b4ffd | 2011-01-31 16:22:42 -0500 | [diff] [blame] | 3296 | if (!path) { |
| 3297 | ret = -ENOMEM; |
| 3298 | goto out; |
| 3299 | } |
Josef Bacik | 7b12876 | 2008-07-24 12:17:14 -0400 | [diff] [blame] | 3300 | path->reada = -1; |
| 3301 | |
| 3302 | key.objectid = BTRFS_ORPHAN_OBJECTID; |
David Sterba | 962a298 | 2014-06-04 18:41:45 +0200 | [diff] [blame] | 3303 | key.type = BTRFS_ORPHAN_ITEM_KEY; |
Josef Bacik | 7b12876 | 2008-07-24 12:17:14 -0400 | [diff] [blame] | 3304 | key.offset = (u64)-1; |
| 3305 | |
Josef Bacik | 7b12876 | 2008-07-24 12:17:14 -0400 | [diff] [blame] | 3306 | while (1) { |
| 3307 | ret = btrfs_search_slot(NULL, root, &key, path, 0, 0); |
Josef Bacik | 66b4ffd | 2011-01-31 16:22:42 -0500 | [diff] [blame] | 3308 | if (ret < 0) |
| 3309 | goto out; |
Josef Bacik | 7b12876 | 2008-07-24 12:17:14 -0400 | [diff] [blame] | 3310 | |
| 3311 | /* |
| 3312 | * 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] | 3313 | * 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] | 3314 | * find the key and see if we have stuff that matches |
| 3315 | */ |
| 3316 | if (ret > 0) { |
Josef Bacik | 66b4ffd | 2011-01-31 16:22:42 -0500 | [diff] [blame] | 3317 | ret = 0; |
Josef Bacik | 7b12876 | 2008-07-24 12:17:14 -0400 | [diff] [blame] | 3318 | if (path->slots[0] == 0) |
| 3319 | break; |
| 3320 | path->slots[0]--; |
| 3321 | } |
| 3322 | |
| 3323 | /* pull out the item */ |
| 3324 | leaf = path->nodes[0]; |
Josef Bacik | 7b12876 | 2008-07-24 12:17:14 -0400 | [diff] [blame] | 3325 | btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]); |
| 3326 | |
| 3327 | /* make sure the item matches what we want */ |
| 3328 | if (found_key.objectid != BTRFS_ORPHAN_OBJECTID) |
| 3329 | break; |
David Sterba | 962a298 | 2014-06-04 18:41:45 +0200 | [diff] [blame] | 3330 | if (found_key.type != BTRFS_ORPHAN_ITEM_KEY) |
Josef Bacik | 7b12876 | 2008-07-24 12:17:14 -0400 | [diff] [blame] | 3331 | break; |
| 3332 | |
| 3333 | /* release the path since we're done with it */ |
David Sterba | b3b4aa7 | 2011-04-21 01:20:15 +0200 | [diff] [blame] | 3334 | btrfs_release_path(path); |
Josef Bacik | 7b12876 | 2008-07-24 12:17:14 -0400 | [diff] [blame] | 3335 | |
| 3336 | /* |
| 3337 | * this is where we are basically btrfs_lookup, without the |
| 3338 | * crossing root thing. we store the inode number in the |
| 3339 | * offset of the orphan item. |
| 3340 | */ |
Josef Bacik | 8f6d7f4 | 2011-09-26 15:55:20 -0400 | [diff] [blame] | 3341 | |
| 3342 | if (found_key.offset == last_objectid) { |
Simon Kirby | c2cf52e | 2013-03-19 22:41:23 +0000 | [diff] [blame] | 3343 | btrfs_err(root->fs_info, |
| 3344 | "Error removing orphan entry, stopping orphan cleanup"); |
Josef Bacik | 8f6d7f4 | 2011-09-26 15:55:20 -0400 | [diff] [blame] | 3345 | ret = -EINVAL; |
| 3346 | goto out; |
| 3347 | } |
| 3348 | |
| 3349 | last_objectid = found_key.offset; |
| 3350 | |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 3351 | found_key.objectid = found_key.offset; |
| 3352 | found_key.type = BTRFS_INODE_ITEM_KEY; |
| 3353 | found_key.offset = 0; |
Josef Bacik | 73f7341 | 2009-12-04 17:38:27 +0000 | [diff] [blame] | 3354 | inode = btrfs_iget(root->fs_info->sb, &found_key, root, NULL); |
Rusty Russell | 8c6ffba | 2013-07-15 11:20:32 +0930 | [diff] [blame] | 3355 | ret = PTR_ERR_OR_ZERO(inode); |
Josef Bacik | a8c9e57 | 2011-09-21 16:55:59 -0400 | [diff] [blame] | 3356 | if (ret && ret != -ESTALE) |
Josef Bacik | 66b4ffd | 2011-01-31 16:22:42 -0500 | [diff] [blame] | 3357 | goto out; |
Josef Bacik | a8c9e57 | 2011-09-21 16:55:59 -0400 | [diff] [blame] | 3358 | |
Arne Jansen | f8e9e0b | 2011-12-14 20:12:02 -0500 | [diff] [blame] | 3359 | if (ret == -ESTALE && root == root->fs_info->tree_root) { |
| 3360 | struct btrfs_root *dead_root; |
| 3361 | struct btrfs_fs_info *fs_info = root->fs_info; |
| 3362 | int is_dead_root = 0; |
| 3363 | |
| 3364 | /* |
| 3365 | * this is an orphan in the tree root. Currently these |
| 3366 | * could come from 2 sources: |
| 3367 | * a) a snapshot deletion in progress |
| 3368 | * b) a free space cache inode |
| 3369 | * We need to distinguish those two, as the snapshot |
| 3370 | * orphan must not get deleted. |
| 3371 | * find_dead_roots already ran before us, so if this |
| 3372 | * is a snapshot deletion, we should find the root |
| 3373 | * in the dead_roots list |
| 3374 | */ |
| 3375 | spin_lock(&fs_info->trans_lock); |
| 3376 | list_for_each_entry(dead_root, &fs_info->dead_roots, |
| 3377 | root_list) { |
| 3378 | if (dead_root->root_key.objectid == |
| 3379 | found_key.objectid) { |
| 3380 | is_dead_root = 1; |
| 3381 | break; |
| 3382 | } |
| 3383 | } |
| 3384 | spin_unlock(&fs_info->trans_lock); |
| 3385 | if (is_dead_root) { |
| 3386 | /* prevent this orphan from being found again */ |
| 3387 | key.offset = found_key.objectid - 1; |
| 3388 | continue; |
| 3389 | } |
| 3390 | } |
Josef Bacik | a8c9e57 | 2011-09-21 16:55:59 -0400 | [diff] [blame] | 3391 | /* |
| 3392 | * Inode is already gone but the orphan item is still there, |
| 3393 | * kill the orphan item. |
| 3394 | */ |
| 3395 | if (ret == -ESTALE) { |
| 3396 | trans = btrfs_start_transaction(root, 1); |
| 3397 | if (IS_ERR(trans)) { |
| 3398 | ret = PTR_ERR(trans); |
| 3399 | goto out; |
| 3400 | } |
Simon Kirby | c2cf52e | 2013-03-19 22:41:23 +0000 | [diff] [blame] | 3401 | btrfs_debug(root->fs_info, "auto deleting %Lu", |
| 3402 | found_key.objectid); |
Josef Bacik | a8c9e57 | 2011-09-21 16:55:59 -0400 | [diff] [blame] | 3403 | ret = btrfs_del_orphan_item(trans, root, |
| 3404 | found_key.objectid); |
Josef Bacik | a8c9e57 | 2011-09-21 16:55:59 -0400 | [diff] [blame] | 3405 | btrfs_end_transaction(trans, root); |
Josef Bacik | 4ef31a4 | 2013-08-13 14:10:08 -0400 | [diff] [blame] | 3406 | if (ret) |
| 3407 | goto out; |
Josef Bacik | a8c9e57 | 2011-09-21 16:55:59 -0400 | [diff] [blame] | 3408 | continue; |
Josef Bacik | 66b4ffd | 2011-01-31 16:22:42 -0500 | [diff] [blame] | 3409 | } |
Josef Bacik | 7b12876 | 2008-07-24 12:17:14 -0400 | [diff] [blame] | 3410 | |
Josef Bacik | 7b12876 | 2008-07-24 12:17:14 -0400 | [diff] [blame] | 3411 | /* |
| 3412 | * add this inode to the orphan list so btrfs_orphan_del does |
| 3413 | * the proper thing when we hit it |
| 3414 | */ |
Josef Bacik | 8a35d95 | 2012-05-23 14:26:42 -0400 | [diff] [blame] | 3415 | set_bit(BTRFS_INODE_HAS_ORPHAN_ITEM, |
| 3416 | &BTRFS_I(inode)->runtime_flags); |
Josef Bacik | 925396e | 2013-02-01 15:57:47 -0500 | [diff] [blame] | 3417 | atomic_inc(&root->orphan_inodes); |
Josef Bacik | 7b12876 | 2008-07-24 12:17:14 -0400 | [diff] [blame] | 3418 | |
Josef Bacik | 7b12876 | 2008-07-24 12:17:14 -0400 | [diff] [blame] | 3419 | /* if we have links, this was a truncate, lets do that */ |
| 3420 | if (inode->i_nlink) { |
Dulshani Gunawardhana | fae7f21 | 2013-10-31 10:30:08 +0530 | [diff] [blame] | 3421 | if (WARN_ON(!S_ISREG(inode->i_mode))) { |
Josef Bacik | a41ad39 | 2011-01-31 15:30:16 -0500 | [diff] [blame] | 3422 | iput(inode); |
| 3423 | continue; |
| 3424 | } |
Josef Bacik | 7b12876 | 2008-07-24 12:17:14 -0400 | [diff] [blame] | 3425 | nr_truncate++; |
Josef Bacik | f3fe820 | 2013-01-07 17:03:21 -0500 | [diff] [blame] | 3426 | |
| 3427 | /* 1 for the orphan item deletion. */ |
| 3428 | trans = btrfs_start_transaction(root, 1); |
| 3429 | if (IS_ERR(trans)) { |
Josef Bacik | c69b26b | 2013-06-03 16:51:23 -0400 | [diff] [blame] | 3430 | iput(inode); |
Josef Bacik | f3fe820 | 2013-01-07 17:03:21 -0500 | [diff] [blame] | 3431 | ret = PTR_ERR(trans); |
| 3432 | goto out; |
| 3433 | } |
| 3434 | ret = btrfs_orphan_add(trans, inode); |
| 3435 | btrfs_end_transaction(trans, root); |
Josef Bacik | c69b26b | 2013-06-03 16:51:23 -0400 | [diff] [blame] | 3436 | if (ret) { |
| 3437 | iput(inode); |
Josef Bacik | f3fe820 | 2013-01-07 17:03:21 -0500 | [diff] [blame] | 3438 | goto out; |
Josef Bacik | c69b26b | 2013-06-03 16:51:23 -0400 | [diff] [blame] | 3439 | } |
Josef Bacik | f3fe820 | 2013-01-07 17:03:21 -0500 | [diff] [blame] | 3440 | |
Josef Bacik | 66b4ffd | 2011-01-31 16:22:42 -0500 | [diff] [blame] | 3441 | ret = btrfs_truncate(inode); |
Josef Bacik | 4a7d0f6 | 2013-02-07 16:27:28 -0500 | [diff] [blame] | 3442 | if (ret) |
| 3443 | btrfs_orphan_del(NULL, inode); |
Josef Bacik | 7b12876 | 2008-07-24 12:17:14 -0400 | [diff] [blame] | 3444 | } else { |
| 3445 | nr_unlink++; |
| 3446 | } |
| 3447 | |
| 3448 | /* this will do delete_inode and everything for us */ |
| 3449 | iput(inode); |
Josef Bacik | 66b4ffd | 2011-01-31 16:22:42 -0500 | [diff] [blame] | 3450 | if (ret) |
| 3451 | goto out; |
Josef Bacik | 7b12876 | 2008-07-24 12:17:14 -0400 | [diff] [blame] | 3452 | } |
Miao Xie | 3254c87 | 2011-11-10 20:45:05 -0500 | [diff] [blame] | 3453 | /* release the path since we're done with it */ |
| 3454 | btrfs_release_path(path); |
| 3455 | |
Yan, Zheng | d68fc57 | 2010-05-16 10:49:58 -0400 | [diff] [blame] | 3456 | root->orphan_cleanup_state = ORPHAN_CLEANUP_DONE; |
| 3457 | |
| 3458 | if (root->orphan_block_rsv) |
| 3459 | btrfs_block_rsv_release(root, root->orphan_block_rsv, |
| 3460 | (u64)-1); |
| 3461 | |
Miao Xie | 27cdeb7 | 2014-04-02 19:51:05 +0800 | [diff] [blame] | 3462 | if (root->orphan_block_rsv || |
| 3463 | test_bit(BTRFS_ROOT_ORPHAN_ITEM_INSERTED, &root->state)) { |
Josef Bacik | 7a7eaa4 | 2011-04-13 12:54:33 -0400 | [diff] [blame] | 3464 | trans = btrfs_join_transaction(root); |
Josef Bacik | 66b4ffd | 2011-01-31 16:22:42 -0500 | [diff] [blame] | 3465 | if (!IS_ERR(trans)) |
| 3466 | btrfs_end_transaction(trans, root); |
Yan, Zheng | d68fc57 | 2010-05-16 10:49:58 -0400 | [diff] [blame] | 3467 | } |
Josef Bacik | 7b12876 | 2008-07-24 12:17:14 -0400 | [diff] [blame] | 3468 | |
| 3469 | if (nr_unlink) |
David Sterba | 4884b47 | 2013-03-20 13:31:27 +0000 | [diff] [blame] | 3470 | btrfs_debug(root->fs_info, "unlinked %d orphans", nr_unlink); |
Josef Bacik | 7b12876 | 2008-07-24 12:17:14 -0400 | [diff] [blame] | 3471 | if (nr_truncate) |
David Sterba | 4884b47 | 2013-03-20 13:31:27 +0000 | [diff] [blame] | 3472 | btrfs_debug(root->fs_info, "truncated %d orphans", nr_truncate); |
Josef Bacik | 66b4ffd | 2011-01-31 16:22:42 -0500 | [diff] [blame] | 3473 | |
| 3474 | out: |
| 3475 | if (ret) |
David Sterba | 68b663d | 2014-12-19 18:38:37 +0100 | [diff] [blame] | 3476 | btrfs_err(root->fs_info, |
Simon Kirby | c2cf52e | 2013-03-19 22:41:23 +0000 | [diff] [blame] | 3477 | "could not do orphan cleanup %d", ret); |
Josef Bacik | 66b4ffd | 2011-01-31 16:22:42 -0500 | [diff] [blame] | 3478 | btrfs_free_path(path); |
| 3479 | return ret; |
Josef Bacik | 7b12876 | 2008-07-24 12:17:14 -0400 | [diff] [blame] | 3480 | } |
| 3481 | |
Chris Mason | d352ac6 | 2008-09-29 15:18:18 -0400 | [diff] [blame] | 3482 | /* |
Chris Mason | 46a53cc | 2009-04-27 11:47:50 -0400 | [diff] [blame] | 3483 | * very simple check to peek ahead in the leaf looking for xattrs. If we |
| 3484 | * don't find any xattrs, we know there can't be any acls. |
| 3485 | * |
| 3486 | * slot is the slot the inode is in, objectid is the objectid of the inode |
| 3487 | */ |
| 3488 | 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] | 3489 | int slot, u64 objectid, |
| 3490 | int *first_xattr_slot) |
Chris Mason | 46a53cc | 2009-04-27 11:47:50 -0400 | [diff] [blame] | 3491 | { |
| 3492 | u32 nritems = btrfs_header_nritems(leaf); |
| 3493 | struct btrfs_key found_key; |
Josef Bacik | f23b5a5 | 2013-06-19 10:16:26 -0400 | [diff] [blame] | 3494 | static u64 xattr_access = 0; |
| 3495 | static u64 xattr_default = 0; |
Chris Mason | 46a53cc | 2009-04-27 11:47:50 -0400 | [diff] [blame] | 3496 | int scanned = 0; |
| 3497 | |
Josef Bacik | f23b5a5 | 2013-06-19 10:16:26 -0400 | [diff] [blame] | 3498 | if (!xattr_access) { |
| 3499 | xattr_access = btrfs_name_hash(POSIX_ACL_XATTR_ACCESS, |
| 3500 | strlen(POSIX_ACL_XATTR_ACCESS)); |
| 3501 | xattr_default = btrfs_name_hash(POSIX_ACL_XATTR_DEFAULT, |
| 3502 | strlen(POSIX_ACL_XATTR_DEFAULT)); |
| 3503 | } |
| 3504 | |
Chris Mason | 46a53cc | 2009-04-27 11:47:50 -0400 | [diff] [blame] | 3505 | slot++; |
Filipe David Borba Manana | 6354192 | 2014-01-07 11:47:46 +0000 | [diff] [blame] | 3506 | *first_xattr_slot = -1; |
Chris Mason | 46a53cc | 2009-04-27 11:47:50 -0400 | [diff] [blame] | 3507 | while (slot < nritems) { |
| 3508 | btrfs_item_key_to_cpu(leaf, &found_key, slot); |
| 3509 | |
| 3510 | /* we found a different objectid, there must not be acls */ |
| 3511 | if (found_key.objectid != objectid) |
| 3512 | return 0; |
| 3513 | |
| 3514 | /* we found an xattr, assume we've got an acl */ |
Josef Bacik | f23b5a5 | 2013-06-19 10:16:26 -0400 | [diff] [blame] | 3515 | if (found_key.type == BTRFS_XATTR_ITEM_KEY) { |
Filipe David Borba Manana | 6354192 | 2014-01-07 11:47:46 +0000 | [diff] [blame] | 3516 | if (*first_xattr_slot == -1) |
| 3517 | *first_xattr_slot = slot; |
Josef Bacik | f23b5a5 | 2013-06-19 10:16:26 -0400 | [diff] [blame] | 3518 | if (found_key.offset == xattr_access || |
| 3519 | found_key.offset == xattr_default) |
| 3520 | return 1; |
| 3521 | } |
Chris Mason | 46a53cc | 2009-04-27 11:47:50 -0400 | [diff] [blame] | 3522 | |
| 3523 | /* |
| 3524 | * we found a key greater than an xattr key, there can't |
| 3525 | * be any acls later on |
| 3526 | */ |
| 3527 | if (found_key.type > BTRFS_XATTR_ITEM_KEY) |
| 3528 | return 0; |
| 3529 | |
| 3530 | slot++; |
| 3531 | scanned++; |
| 3532 | |
| 3533 | /* |
| 3534 | * it goes inode, inode backrefs, xattrs, extents, |
| 3535 | * so if there are a ton of hard links to an inode there can |
| 3536 | * be a lot of backrefs. Don't waste time searching too hard, |
| 3537 | * this is just an optimization |
| 3538 | */ |
| 3539 | if (scanned >= 8) |
| 3540 | break; |
| 3541 | } |
| 3542 | /* we hit the end of the leaf before we found an xattr or |
| 3543 | * something larger than an xattr. We have to assume the inode |
| 3544 | * has acls |
| 3545 | */ |
Filipe David Borba Manana | 6354192 | 2014-01-07 11:47:46 +0000 | [diff] [blame] | 3546 | if (*first_xattr_slot == -1) |
| 3547 | *first_xattr_slot = slot; |
Chris Mason | 46a53cc | 2009-04-27 11:47:50 -0400 | [diff] [blame] | 3548 | return 1; |
| 3549 | } |
| 3550 | |
| 3551 | /* |
Chris Mason | d352ac6 | 2008-09-29 15:18:18 -0400 | [diff] [blame] | 3552 | * read an inode from the btree into the in-memory inode |
| 3553 | */ |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 3554 | static void btrfs_read_locked_inode(struct inode *inode) |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3555 | { |
| 3556 | struct btrfs_path *path; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 3557 | struct extent_buffer *leaf; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3558 | struct btrfs_inode_item *inode_item; |
| 3559 | struct btrfs_root *root = BTRFS_I(inode)->root; |
| 3560 | struct btrfs_key location; |
Miao Xie | 67de117 | 2013-12-26 13:07:06 +0800 | [diff] [blame] | 3561 | unsigned long ptr; |
Chris Mason | 46a53cc | 2009-04-27 11:47:50 -0400 | [diff] [blame] | 3562 | int maybe_acls; |
Josef Bacik | 618e21d | 2007-07-11 10:18:17 -0400 | [diff] [blame] | 3563 | u32 rdev; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3564 | int ret; |
Miao Xie | 2f7e33d | 2011-06-23 07:27:13 +0000 | [diff] [blame] | 3565 | bool filled = false; |
Filipe David Borba Manana | 6354192 | 2014-01-07 11:47:46 +0000 | [diff] [blame] | 3566 | int first_xattr_slot; |
Miao Xie | 2f7e33d | 2011-06-23 07:27:13 +0000 | [diff] [blame] | 3567 | |
| 3568 | ret = btrfs_fill_inode(inode, &rdev); |
| 3569 | if (!ret) |
| 3570 | filled = true; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3571 | |
| 3572 | path = btrfs_alloc_path(); |
Mark Fasheh | 1748f84 | 2011-07-12 11:25:31 -0700 | [diff] [blame] | 3573 | if (!path) |
| 3574 | goto make_bad; |
| 3575 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3576 | memcpy(&location, &BTRFS_I(inode)->location, sizeof(location)); |
Chris Mason | dc17ff8 | 2008-01-08 15:46:30 -0500 | [diff] [blame] | 3577 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3578 | ret = btrfs_lookup_inode(NULL, root, path, &location, 0); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 3579 | if (ret) |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3580 | goto make_bad; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3581 | |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 3582 | leaf = path->nodes[0]; |
Miao Xie | 2f7e33d | 2011-06-23 07:27:13 +0000 | [diff] [blame] | 3583 | |
| 3584 | if (filled) |
Miao Xie | 67de117 | 2013-12-26 13:07:06 +0800 | [diff] [blame] | 3585 | goto cache_index; |
Miao Xie | 2f7e33d | 2011-06-23 07:27:13 +0000 | [diff] [blame] | 3586 | |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 3587 | inode_item = btrfs_item_ptr(leaf, path->slots[0], |
| 3588 | struct btrfs_inode_item); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 3589 | inode->i_mode = btrfs_inode_mode(leaf, inode_item); |
Miklos Szeredi | bfe8684 | 2011-10-28 14:13:29 +0200 | [diff] [blame] | 3590 | set_nlink(inode, btrfs_inode_nlink(leaf, inode_item)); |
Eric W. Biederman | 2f2f43d | 2012-02-10 11:05:07 -0800 | [diff] [blame] | 3591 | i_uid_write(inode, btrfs_inode_uid(leaf, inode_item)); |
| 3592 | i_gid_write(inode, btrfs_inode_gid(leaf, inode_item)); |
Chris Mason | dbe674a | 2008-07-17 12:54:05 -0400 | [diff] [blame] | 3593 | btrfs_i_size_write(inode, btrfs_inode_size(leaf, inode_item)); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 3594 | |
David Sterba | a937b97 | 2014-12-12 17:39:12 +0100 | [diff] [blame] | 3595 | inode->i_atime.tv_sec = btrfs_timespec_sec(leaf, &inode_item->atime); |
| 3596 | inode->i_atime.tv_nsec = btrfs_timespec_nsec(leaf, &inode_item->atime); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 3597 | |
David Sterba | a937b97 | 2014-12-12 17:39:12 +0100 | [diff] [blame] | 3598 | inode->i_mtime.tv_sec = btrfs_timespec_sec(leaf, &inode_item->mtime); |
| 3599 | inode->i_mtime.tv_nsec = btrfs_timespec_nsec(leaf, &inode_item->mtime); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 3600 | |
David Sterba | a937b97 | 2014-12-12 17:39:12 +0100 | [diff] [blame] | 3601 | inode->i_ctime.tv_sec = btrfs_timespec_sec(leaf, &inode_item->ctime); |
| 3602 | inode->i_ctime.tv_nsec = btrfs_timespec_nsec(leaf, &inode_item->ctime); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 3603 | |
chandan r | 9cc97d6 | 2012-07-04 12:48:07 +0530 | [diff] [blame] | 3604 | BTRFS_I(inode)->i_otime.tv_sec = |
| 3605 | btrfs_timespec_sec(leaf, &inode_item->otime); |
| 3606 | BTRFS_I(inode)->i_otime.tv_nsec = |
| 3607 | btrfs_timespec_nsec(leaf, &inode_item->otime); |
| 3608 | |
Yan Zheng | a76a3cd | 2008-10-09 11:46:29 -0400 | [diff] [blame] | 3609 | inode_set_bytes(inode, btrfs_inode_nbytes(leaf, inode_item)); |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 3610 | BTRFS_I(inode)->generation = btrfs_inode_generation(leaf, inode_item); |
Josef Bacik | 5dc562c | 2012-08-17 13:14:17 -0400 | [diff] [blame] | 3611 | BTRFS_I(inode)->last_trans = btrfs_inode_transid(leaf, inode_item); |
| 3612 | |
| 3613 | /* |
| 3614 | * If we were modified in the current generation and evicted from memory |
| 3615 | * and then re-read we need to do a full sync since we don't have any |
| 3616 | * idea about which extents were modified before we were evicted from |
| 3617 | * cache. |
| 3618 | */ |
| 3619 | if (BTRFS_I(inode)->last_trans == root->fs_info->generation) |
| 3620 | set_bit(BTRFS_INODE_NEEDS_FULL_SYNC, |
| 3621 | &BTRFS_I(inode)->runtime_flags); |
| 3622 | |
Josef Bacik | 0c4d2d9 | 2012-04-05 15:03:02 -0400 | [diff] [blame] | 3623 | inode->i_version = btrfs_inode_sequence(leaf, inode_item); |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 3624 | inode->i_generation = BTRFS_I(inode)->generation; |
Josef Bacik | 618e21d | 2007-07-11 10:18:17 -0400 | [diff] [blame] | 3625 | inode->i_rdev = 0; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 3626 | rdev = btrfs_inode_rdev(leaf, inode_item); |
| 3627 | |
Josef Bacik | aec7477 | 2008-07-24 12:12:38 -0400 | [diff] [blame] | 3628 | BTRFS_I(inode)->index_cnt = (u64)-1; |
Yan Zheng | d2fb343 | 2008-12-11 16:30:39 -0500 | [diff] [blame] | 3629 | BTRFS_I(inode)->flags = btrfs_inode_flags(leaf, inode_item); |
Miao Xie | 67de117 | 2013-12-26 13:07:06 +0800 | [diff] [blame] | 3630 | |
| 3631 | cache_index: |
| 3632 | path->slots[0]++; |
| 3633 | if (inode->i_nlink != 1 || |
| 3634 | path->slots[0] >= btrfs_header_nritems(leaf)) |
| 3635 | goto cache_acl; |
| 3636 | |
| 3637 | btrfs_item_key_to_cpu(leaf, &location, path->slots[0]); |
| 3638 | if (location.objectid != btrfs_ino(inode)) |
| 3639 | goto cache_acl; |
| 3640 | |
| 3641 | ptr = btrfs_item_ptr_offset(leaf, path->slots[0]); |
| 3642 | if (location.type == BTRFS_INODE_REF_KEY) { |
| 3643 | struct btrfs_inode_ref *ref; |
| 3644 | |
| 3645 | ref = (struct btrfs_inode_ref *)ptr; |
| 3646 | BTRFS_I(inode)->dir_index = btrfs_inode_ref_index(leaf, ref); |
| 3647 | } else if (location.type == BTRFS_INODE_EXTREF_KEY) { |
| 3648 | struct btrfs_inode_extref *extref; |
| 3649 | |
| 3650 | extref = (struct btrfs_inode_extref *)ptr; |
| 3651 | BTRFS_I(inode)->dir_index = btrfs_inode_extref_index(leaf, |
| 3652 | extref); |
| 3653 | } |
Miao Xie | 2f7e33d | 2011-06-23 07:27:13 +0000 | [diff] [blame] | 3654 | cache_acl: |
Chris Mason | 46a53cc | 2009-04-27 11:47:50 -0400 | [diff] [blame] | 3655 | /* |
| 3656 | * try to precache a NULL acl entry for files that don't have |
| 3657 | * any xattrs or acls |
| 3658 | */ |
Li Zefan | 33345d01 | 2011-04-20 10:31:50 +0800 | [diff] [blame] | 3659 | maybe_acls = acls_after_inode_item(leaf, path->slots[0], |
Filipe David Borba Manana | 6354192 | 2014-01-07 11:47:46 +0000 | [diff] [blame] | 3660 | btrfs_ino(inode), &first_xattr_slot); |
| 3661 | if (first_xattr_slot != -1) { |
| 3662 | path->slots[0] = first_xattr_slot; |
| 3663 | ret = btrfs_load_inode_props(inode, path); |
| 3664 | if (ret) |
| 3665 | btrfs_err(root->fs_info, |
David Sterba | 351fd35 | 2014-05-15 16:48:20 +0200 | [diff] [blame] | 3666 | "error loading props for ino %llu (root %llu): %d", |
Filipe David Borba Manana | 6354192 | 2014-01-07 11:47:46 +0000 | [diff] [blame] | 3667 | btrfs_ino(inode), |
| 3668 | root->root_key.objectid, ret); |
| 3669 | } |
| 3670 | btrfs_free_path(path); |
| 3671 | |
Al Viro | 72c0490 | 2009-06-24 16:58:48 -0400 | [diff] [blame] | 3672 | if (!maybe_acls) |
| 3673 | cache_no_acl(inode); |
Chris Mason | 46a53cc | 2009-04-27 11:47:50 -0400 | [diff] [blame] | 3674 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3675 | switch (inode->i_mode & S_IFMT) { |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3676 | case S_IFREG: |
| 3677 | inode->i_mapping->a_ops = &btrfs_aops; |
Chris Mason | 0416008 | 2008-03-26 10:28:07 -0400 | [diff] [blame] | 3678 | inode->i_mapping->backing_dev_info = &root->fs_info->bdi; |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 3679 | BTRFS_I(inode)->io_tree.ops = &btrfs_extent_io_ops; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3680 | inode->i_fop = &btrfs_file_operations; |
| 3681 | inode->i_op = &btrfs_file_inode_operations; |
| 3682 | break; |
| 3683 | case S_IFDIR: |
| 3684 | inode->i_fop = &btrfs_dir_file_operations; |
| 3685 | if (root == root->fs_info->tree_root) |
| 3686 | inode->i_op = &btrfs_dir_ro_inode_operations; |
| 3687 | else |
| 3688 | inode->i_op = &btrfs_dir_inode_operations; |
| 3689 | break; |
| 3690 | case S_IFLNK: |
| 3691 | inode->i_op = &btrfs_symlink_inode_operations; |
| 3692 | inode->i_mapping->a_ops = &btrfs_symlink_aops; |
Chris Mason | 0416008 | 2008-03-26 10:28:07 -0400 | [diff] [blame] | 3693 | inode->i_mapping->backing_dev_info = &root->fs_info->bdi; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3694 | break; |
Josef Bacik | 618e21d | 2007-07-11 10:18:17 -0400 | [diff] [blame] | 3695 | default: |
Jim Owens | 0279b4c | 2009-02-04 09:29:13 -0500 | [diff] [blame] | 3696 | inode->i_op = &btrfs_special_inode_operations; |
Josef Bacik | 618e21d | 2007-07-11 10:18:17 -0400 | [diff] [blame] | 3697 | init_special_inode(inode, inode->i_mode, rdev); |
| 3698 | break; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3699 | } |
Christoph Hellwig | 6cbff00 | 2009-04-17 10:37:41 +0200 | [diff] [blame] | 3700 | |
| 3701 | btrfs_update_iflags(inode); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3702 | return; |
| 3703 | |
| 3704 | make_bad: |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3705 | btrfs_free_path(path); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3706 | make_bad_inode(inode); |
| 3707 | } |
| 3708 | |
Chris Mason | d352ac6 | 2008-09-29 15:18:18 -0400 | [diff] [blame] | 3709 | /* |
| 3710 | * given a leaf and an inode, copy the inode fields into the leaf |
| 3711 | */ |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 3712 | static void fill_inode_item(struct btrfs_trans_handle *trans, |
| 3713 | struct extent_buffer *leaf, |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 3714 | struct btrfs_inode_item *item, |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3715 | struct inode *inode) |
| 3716 | { |
Liu Bo | 51fab69 | 2012-12-27 09:01:21 +0000 | [diff] [blame] | 3717 | struct btrfs_map_token token; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 3718 | |
Liu Bo | 51fab69 | 2012-12-27 09:01:21 +0000 | [diff] [blame] | 3719 | btrfs_init_map_token(&token); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 3720 | |
Liu Bo | 51fab69 | 2012-12-27 09:01:21 +0000 | [diff] [blame] | 3721 | btrfs_set_token_inode_uid(leaf, item, i_uid_read(inode), &token); |
| 3722 | btrfs_set_token_inode_gid(leaf, item, i_gid_read(inode), &token); |
| 3723 | btrfs_set_token_inode_size(leaf, item, BTRFS_I(inode)->disk_i_size, |
| 3724 | &token); |
| 3725 | btrfs_set_token_inode_mode(leaf, item, inode->i_mode, &token); |
| 3726 | btrfs_set_token_inode_nlink(leaf, item, inode->i_nlink, &token); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 3727 | |
David Sterba | a937b97 | 2014-12-12 17:39:12 +0100 | [diff] [blame] | 3728 | btrfs_set_token_timespec_sec(leaf, &item->atime, |
Liu Bo | 51fab69 | 2012-12-27 09:01:21 +0000 | [diff] [blame] | 3729 | inode->i_atime.tv_sec, &token); |
David Sterba | a937b97 | 2014-12-12 17:39:12 +0100 | [diff] [blame] | 3730 | btrfs_set_token_timespec_nsec(leaf, &item->atime, |
Liu Bo | 51fab69 | 2012-12-27 09:01:21 +0000 | [diff] [blame] | 3731 | inode->i_atime.tv_nsec, &token); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 3732 | |
David Sterba | a937b97 | 2014-12-12 17:39:12 +0100 | [diff] [blame] | 3733 | btrfs_set_token_timespec_sec(leaf, &item->mtime, |
Liu Bo | 51fab69 | 2012-12-27 09:01:21 +0000 | [diff] [blame] | 3734 | inode->i_mtime.tv_sec, &token); |
David Sterba | a937b97 | 2014-12-12 17:39:12 +0100 | [diff] [blame] | 3735 | btrfs_set_token_timespec_nsec(leaf, &item->mtime, |
Liu Bo | 51fab69 | 2012-12-27 09:01:21 +0000 | [diff] [blame] | 3736 | inode->i_mtime.tv_nsec, &token); |
| 3737 | |
David Sterba | a937b97 | 2014-12-12 17:39:12 +0100 | [diff] [blame] | 3738 | btrfs_set_token_timespec_sec(leaf, &item->ctime, |
Liu Bo | 51fab69 | 2012-12-27 09:01:21 +0000 | [diff] [blame] | 3739 | inode->i_ctime.tv_sec, &token); |
David Sterba | a937b97 | 2014-12-12 17:39:12 +0100 | [diff] [blame] | 3740 | btrfs_set_token_timespec_nsec(leaf, &item->ctime, |
Liu Bo | 51fab69 | 2012-12-27 09:01:21 +0000 | [diff] [blame] | 3741 | inode->i_ctime.tv_nsec, &token); |
| 3742 | |
chandan r | 9cc97d6 | 2012-07-04 12:48:07 +0530 | [diff] [blame] | 3743 | btrfs_set_token_timespec_sec(leaf, &item->otime, |
| 3744 | BTRFS_I(inode)->i_otime.tv_sec, &token); |
| 3745 | btrfs_set_token_timespec_nsec(leaf, &item->otime, |
| 3746 | BTRFS_I(inode)->i_otime.tv_nsec, &token); |
| 3747 | |
Liu Bo | 51fab69 | 2012-12-27 09:01:21 +0000 | [diff] [blame] | 3748 | btrfs_set_token_inode_nbytes(leaf, item, inode_get_bytes(inode), |
| 3749 | &token); |
| 3750 | btrfs_set_token_inode_generation(leaf, item, BTRFS_I(inode)->generation, |
| 3751 | &token); |
| 3752 | btrfs_set_token_inode_sequence(leaf, item, inode->i_version, &token); |
| 3753 | btrfs_set_token_inode_transid(leaf, item, trans->transid, &token); |
| 3754 | btrfs_set_token_inode_rdev(leaf, item, inode->i_rdev, &token); |
| 3755 | btrfs_set_token_inode_flags(leaf, item, BTRFS_I(inode)->flags, &token); |
| 3756 | btrfs_set_token_inode_block_group(leaf, item, 0, &token); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3757 | } |
| 3758 | |
Chris Mason | d352ac6 | 2008-09-29 15:18:18 -0400 | [diff] [blame] | 3759 | /* |
| 3760 | * copy everything in the in-memory inode into the btree. |
| 3761 | */ |
Chris Mason | 2115133 | 2011-11-10 20:39:08 -0500 | [diff] [blame] | 3762 | static noinline int btrfs_update_inode_item(struct btrfs_trans_handle *trans, |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 3763 | struct btrfs_root *root, struct inode *inode) |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3764 | { |
| 3765 | struct btrfs_inode_item *inode_item; |
| 3766 | struct btrfs_path *path; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 3767 | struct extent_buffer *leaf; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3768 | int ret; |
| 3769 | |
| 3770 | path = btrfs_alloc_path(); |
Miao Xie | 16cdcec | 2011-04-22 18:12:22 +0800 | [diff] [blame] | 3771 | if (!path) |
| 3772 | return -ENOMEM; |
| 3773 | |
Chris Mason | b947343 | 2009-03-13 11:00:37 -0400 | [diff] [blame] | 3774 | path->leave_spinning = 1; |
Miao Xie | 16cdcec | 2011-04-22 18:12:22 +0800 | [diff] [blame] | 3775 | ret = btrfs_lookup_inode(trans, root, path, &BTRFS_I(inode)->location, |
| 3776 | 1); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3777 | if (ret) { |
| 3778 | if (ret > 0) |
| 3779 | ret = -ENOENT; |
| 3780 | goto failed; |
| 3781 | } |
| 3782 | |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 3783 | leaf = path->nodes[0]; |
| 3784 | inode_item = btrfs_item_ptr(leaf, path->slots[0], |
Miao Xie | 16cdcec | 2011-04-22 18:12:22 +0800 | [diff] [blame] | 3785 | struct btrfs_inode_item); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3786 | |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 3787 | fill_inode_item(trans, leaf, inode_item, inode); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 3788 | btrfs_mark_buffer_dirty(leaf); |
Josef Bacik | 15ee9bc | 2007-08-10 16:22:09 -0400 | [diff] [blame] | 3789 | btrfs_set_inode_last_trans(trans, inode); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3790 | ret = 0; |
| 3791 | failed: |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3792 | btrfs_free_path(path); |
| 3793 | return ret; |
| 3794 | } |
| 3795 | |
Chris Mason | d352ac6 | 2008-09-29 15:18:18 -0400 | [diff] [blame] | 3796 | /* |
Chris Mason | 2115133 | 2011-11-10 20:39:08 -0500 | [diff] [blame] | 3797 | * copy everything in the in-memory inode into the btree. |
| 3798 | */ |
| 3799 | noinline int btrfs_update_inode(struct btrfs_trans_handle *trans, |
| 3800 | struct btrfs_root *root, struct inode *inode) |
| 3801 | { |
| 3802 | int ret; |
| 3803 | |
| 3804 | /* |
| 3805 | * If the inode is a free space inode, we can deadlock during commit |
| 3806 | * if we put it into the delayed code. |
| 3807 | * |
| 3808 | * The data relocation inode should also be directly updated |
| 3809 | * without delay |
| 3810 | */ |
Liu Bo | 83eea1f | 2012-07-10 05:28:39 -0600 | [diff] [blame] | 3811 | if (!btrfs_is_free_space_inode(inode) |
Josef Bacik | 1d52c78 | 2014-09-18 11:30:44 -0400 | [diff] [blame] | 3812 | && root->root_key.objectid != BTRFS_DATA_RELOC_TREE_OBJECTID |
| 3813 | && !root->fs_info->log_root_recovering) { |
Alexander Block | 8ea05e3 | 2012-07-25 17:35:53 +0200 | [diff] [blame] | 3814 | btrfs_update_root_times(trans, root); |
| 3815 | |
Chris Mason | 2115133 | 2011-11-10 20:39:08 -0500 | [diff] [blame] | 3816 | ret = btrfs_delayed_update_inode(trans, root, inode); |
| 3817 | if (!ret) |
| 3818 | btrfs_set_inode_last_trans(trans, inode); |
| 3819 | return ret; |
| 3820 | } |
| 3821 | |
| 3822 | return btrfs_update_inode_item(trans, root, inode); |
| 3823 | } |
| 3824 | |
Josef Bacik | be6aef6 | 2012-10-22 15:43:12 -0400 | [diff] [blame] | 3825 | noinline int btrfs_update_inode_fallback(struct btrfs_trans_handle *trans, |
| 3826 | struct btrfs_root *root, |
| 3827 | struct inode *inode) |
Chris Mason | 2115133 | 2011-11-10 20:39:08 -0500 | [diff] [blame] | 3828 | { |
| 3829 | int ret; |
| 3830 | |
| 3831 | ret = btrfs_update_inode(trans, root, inode); |
| 3832 | if (ret == -ENOSPC) |
| 3833 | return btrfs_update_inode_item(trans, root, inode); |
| 3834 | return ret; |
| 3835 | } |
| 3836 | |
| 3837 | /* |
Chris Mason | d352ac6 | 2008-09-29 15:18:18 -0400 | [diff] [blame] | 3838 | * unlink helper that gets used here in inode.c and in the tree logging |
| 3839 | * recovery code. It remove a link in a directory with a given name, and |
| 3840 | * also drops the back refs in the inode to the directory |
| 3841 | */ |
Al Viro | 9298679 | 2011-03-04 17:14:37 +0000 | [diff] [blame] | 3842 | static int __btrfs_unlink_inode(struct btrfs_trans_handle *trans, |
| 3843 | struct btrfs_root *root, |
| 3844 | struct inode *dir, struct inode *inode, |
| 3845 | const char *name, int name_len) |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3846 | { |
| 3847 | struct btrfs_path *path; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3848 | int ret = 0; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 3849 | struct extent_buffer *leaf; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3850 | struct btrfs_dir_item *di; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 3851 | struct btrfs_key key; |
Josef Bacik | aec7477 | 2008-07-24 12:12:38 -0400 | [diff] [blame] | 3852 | u64 index; |
Li Zefan | 33345d01 | 2011-04-20 10:31:50 +0800 | [diff] [blame] | 3853 | u64 ino = btrfs_ino(inode); |
| 3854 | u64 dir_ino = btrfs_ino(dir); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3855 | |
| 3856 | path = btrfs_alloc_path(); |
Chris Mason | 54aa1f4 | 2007-06-22 14:16:25 -0400 | [diff] [blame] | 3857 | if (!path) { |
| 3858 | ret = -ENOMEM; |
Tsutomu Itoh | 554233a | 2011-02-03 03:16:25 +0000 | [diff] [blame] | 3859 | goto out; |
Chris Mason | 54aa1f4 | 2007-06-22 14:16:25 -0400 | [diff] [blame] | 3860 | } |
| 3861 | |
Chris Mason | b947343 | 2009-03-13 11:00:37 -0400 | [diff] [blame] | 3862 | path->leave_spinning = 1; |
Li Zefan | 33345d01 | 2011-04-20 10:31:50 +0800 | [diff] [blame] | 3863 | di = btrfs_lookup_dir_item(trans, root, path, dir_ino, |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3864 | name, name_len, -1); |
| 3865 | if (IS_ERR(di)) { |
| 3866 | ret = PTR_ERR(di); |
| 3867 | goto err; |
| 3868 | } |
| 3869 | if (!di) { |
| 3870 | ret = -ENOENT; |
| 3871 | goto err; |
| 3872 | } |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 3873 | leaf = path->nodes[0]; |
| 3874 | btrfs_dir_item_key_to_cpu(leaf, di, &key); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3875 | ret = btrfs_delete_one_dir_name(trans, root, path, di); |
Chris Mason | 54aa1f4 | 2007-06-22 14:16:25 -0400 | [diff] [blame] | 3876 | if (ret) |
| 3877 | goto err; |
David Sterba | b3b4aa7 | 2011-04-21 01:20:15 +0200 | [diff] [blame] | 3878 | btrfs_release_path(path); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3879 | |
Miao Xie | 67de117 | 2013-12-26 13:07:06 +0800 | [diff] [blame] | 3880 | /* |
| 3881 | * If we don't have dir index, we have to get it by looking up |
| 3882 | * the inode ref, since we get the inode ref, remove it directly, |
| 3883 | * it is unnecessary to do delayed deletion. |
| 3884 | * |
| 3885 | * But if we have dir index, needn't search inode ref to get it. |
| 3886 | * Since the inode ref is close to the inode item, it is better |
| 3887 | * that we delay to delete it, and just do this deletion when |
| 3888 | * we update the inode item. |
| 3889 | */ |
| 3890 | if (BTRFS_I(inode)->dir_index) { |
| 3891 | ret = btrfs_delayed_delete_inode_ref(inode); |
| 3892 | if (!ret) { |
| 3893 | index = BTRFS_I(inode)->dir_index; |
| 3894 | goto skip_backref; |
| 3895 | } |
| 3896 | } |
| 3897 | |
Li Zefan | 33345d01 | 2011-04-20 10:31:50 +0800 | [diff] [blame] | 3898 | ret = btrfs_del_inode_ref(trans, root, name, name_len, ino, |
| 3899 | dir_ino, &index); |
Josef Bacik | aec7477 | 2008-07-24 12:12:38 -0400 | [diff] [blame] | 3900 | if (ret) { |
Simon Kirby | c2cf52e | 2013-03-19 22:41:23 +0000 | [diff] [blame] | 3901 | btrfs_info(root->fs_info, |
| 3902 | "failed to delete reference to %.*s, inode %llu parent %llu", |
Geert Uytterhoeven | c1c9ff7 | 2013-08-20 13:20:07 +0200 | [diff] [blame] | 3903 | name_len, name, ino, dir_ino); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 3904 | btrfs_abort_transaction(trans, root, ret); |
Josef Bacik | aec7477 | 2008-07-24 12:12:38 -0400 | [diff] [blame] | 3905 | goto err; |
| 3906 | } |
Miao Xie | 67de117 | 2013-12-26 13:07:06 +0800 | [diff] [blame] | 3907 | skip_backref: |
Miao Xie | 16cdcec | 2011-04-22 18:12:22 +0800 | [diff] [blame] | 3908 | ret = btrfs_delete_delayed_dir_index(trans, root, dir, index); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 3909 | if (ret) { |
| 3910 | btrfs_abort_transaction(trans, root, ret); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3911 | goto err; |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 3912 | } |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3913 | |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 3914 | ret = btrfs_del_inode_ref_in_log(trans, root, name, name_len, |
Li Zefan | 33345d01 | 2011-04-20 10:31:50 +0800 | [diff] [blame] | 3915 | inode, dir_ino); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 3916 | if (ret != 0 && ret != -ENOENT) { |
| 3917 | btrfs_abort_transaction(trans, root, ret); |
| 3918 | goto err; |
| 3919 | } |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 3920 | |
| 3921 | ret = btrfs_del_dir_entries_in_log(trans, root, name, name_len, |
| 3922 | dir, index); |
Chris Mason | 6418c96 | 2010-10-30 07:34:24 -0400 | [diff] [blame] | 3923 | if (ret == -ENOENT) |
| 3924 | ret = 0; |
Zach Brown | d4e3991 | 2013-04-02 21:02:16 +0000 | [diff] [blame] | 3925 | else if (ret) |
| 3926 | btrfs_abort_transaction(trans, root, ret); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3927 | err: |
| 3928 | btrfs_free_path(path); |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 3929 | if (ret) |
| 3930 | goto out; |
| 3931 | |
| 3932 | btrfs_i_size_write(dir, dir->i_size - name_len * 2); |
Josef Bacik | 0c4d2d9 | 2012-04-05 15:03:02 -0400 | [diff] [blame] | 3933 | inode_inc_iversion(inode); |
| 3934 | inode_inc_iversion(dir); |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 3935 | inode->i_ctime = dir->i_mtime = dir->i_ctime = CURRENT_TIME; |
Tsutomu Itoh | b995929 | 2012-06-25 21:25:22 -0600 | [diff] [blame] | 3936 | ret = btrfs_update_inode(trans, root, dir); |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 3937 | out: |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3938 | return ret; |
| 3939 | } |
| 3940 | |
Al Viro | 9298679 | 2011-03-04 17:14:37 +0000 | [diff] [blame] | 3941 | int btrfs_unlink_inode(struct btrfs_trans_handle *trans, |
| 3942 | struct btrfs_root *root, |
| 3943 | struct inode *dir, struct inode *inode, |
| 3944 | const char *name, int name_len) |
| 3945 | { |
| 3946 | int ret; |
| 3947 | ret = __btrfs_unlink_inode(trans, root, dir, inode, name, name_len); |
| 3948 | if (!ret) { |
Zach Brown | 8b558c5 | 2013-10-16 12:10:34 -0700 | [diff] [blame] | 3949 | drop_nlink(inode); |
Al Viro | 9298679 | 2011-03-04 17:14:37 +0000 | [diff] [blame] | 3950 | ret = btrfs_update_inode(trans, root, inode); |
| 3951 | } |
| 3952 | return ret; |
| 3953 | } |
Yan, Zheng | a22285a | 2010-05-16 10:48:46 -0400 | [diff] [blame] | 3954 | |
| 3955 | /* |
| 3956 | * helper to start transaction for unlink and rmdir. |
| 3957 | * |
Josef Bacik | d52be81 | 2013-05-29 14:54:47 -0400 | [diff] [blame] | 3958 | * unlink and rmdir are special in btrfs, they do not always free space, so |
| 3959 | * if we cannot make our reservations the normal way try and see if there is |
| 3960 | * plenty of slack room in the global reserve to migrate, otherwise we cannot |
| 3961 | * allow the unlink to occur. |
Yan, Zheng | a22285a | 2010-05-16 10:48:46 -0400 | [diff] [blame] | 3962 | */ |
Josef Bacik | d52be81 | 2013-05-29 14:54:47 -0400 | [diff] [blame] | 3963 | static struct btrfs_trans_handle *__unlink_start_trans(struct inode *dir) |
Yan, Zheng | a22285a | 2010-05-16 10:48:46 -0400 | [diff] [blame] | 3964 | { |
| 3965 | struct btrfs_trans_handle *trans; |
| 3966 | struct btrfs_root *root = BTRFS_I(dir)->root; |
Yan, Zheng | a22285a | 2010-05-16 10:48:46 -0400 | [diff] [blame] | 3967 | int ret; |
| 3968 | |
Josef Bacik | e70bea5 | 2011-10-11 14:18:24 -0400 | [diff] [blame] | 3969 | /* |
| 3970 | * 1 for the possible orphan item |
| 3971 | * 1 for the dir item |
| 3972 | * 1 for the dir index |
| 3973 | * 1 for the inode ref |
Josef Bacik | e70bea5 | 2011-10-11 14:18:24 -0400 | [diff] [blame] | 3974 | * 1 for the inode |
| 3975 | */ |
Josef Bacik | 6e137ed | 2013-03-26 15:26:55 -0400 | [diff] [blame] | 3976 | trans = btrfs_start_transaction(root, 5); |
Yan, Zheng | a22285a | 2010-05-16 10:48:46 -0400 | [diff] [blame] | 3977 | if (!IS_ERR(trans) || PTR_ERR(trans) != -ENOSPC) |
| 3978 | return trans; |
| 3979 | |
Josef Bacik | d52be81 | 2013-05-29 14:54:47 -0400 | [diff] [blame] | 3980 | if (PTR_ERR(trans) == -ENOSPC) { |
| 3981 | u64 num_bytes = btrfs_calc_trans_metadata_size(root, 5); |
Yan, Zheng | a22285a | 2010-05-16 10:48:46 -0400 | [diff] [blame] | 3982 | |
Josef Bacik | d52be81 | 2013-05-29 14:54:47 -0400 | [diff] [blame] | 3983 | trans = btrfs_start_transaction(root, 0); |
| 3984 | if (IS_ERR(trans)) |
| 3985 | return trans; |
| 3986 | ret = btrfs_cond_migrate_bytes(root->fs_info, |
| 3987 | &root->fs_info->trans_block_rsv, |
| 3988 | num_bytes, 5); |
| 3989 | if (ret) { |
| 3990 | btrfs_end_transaction(trans, root); |
| 3991 | return ERR_PTR(ret); |
Yan, Zheng | a22285a | 2010-05-16 10:48:46 -0400 | [diff] [blame] | 3992 | } |
Josef Bacik | 5a77d76 | 2011-11-01 14:32:23 -0400 | [diff] [blame] | 3993 | trans->block_rsv = &root->fs_info->trans_block_rsv; |
Josef Bacik | d52be81 | 2013-05-29 14:54:47 -0400 | [diff] [blame] | 3994 | trans->bytes_reserved = num_bytes; |
Yan, Zheng | a22285a | 2010-05-16 10:48:46 -0400 | [diff] [blame] | 3995 | } |
Josef Bacik | d52be81 | 2013-05-29 14:54:47 -0400 | [diff] [blame] | 3996 | return trans; |
Yan, Zheng | a22285a | 2010-05-16 10:48:46 -0400 | [diff] [blame] | 3997 | } |
| 3998 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3999 | static int btrfs_unlink(struct inode *dir, struct dentry *dentry) |
| 4000 | { |
Yan, Zheng | a22285a | 2010-05-16 10:48:46 -0400 | [diff] [blame] | 4001 | struct btrfs_root *root = BTRFS_I(dir)->root; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4002 | struct btrfs_trans_handle *trans; |
Josef Bacik | 7b12876 | 2008-07-24 12:17:14 -0400 | [diff] [blame] | 4003 | struct inode *inode = dentry->d_inode; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4004 | int ret; |
| 4005 | |
Josef Bacik | d52be81 | 2013-05-29 14:54:47 -0400 | [diff] [blame] | 4006 | trans = __unlink_start_trans(dir); |
Yan, Zheng | a22285a | 2010-05-16 10:48:46 -0400 | [diff] [blame] | 4007 | if (IS_ERR(trans)) |
Josef Bacik | 5df6a9f | 2009-11-10 21:23:48 -0500 | [diff] [blame] | 4008 | return PTR_ERR(trans); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 4009 | |
Chris Mason | 12fcfd2 | 2009-03-24 10:24:20 -0400 | [diff] [blame] | 4010 | btrfs_record_unlink_dir(trans, dir, dentry->d_inode, 0); |
| 4011 | |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 4012 | ret = btrfs_unlink_inode(trans, root, dir, dentry->d_inode, |
| 4013 | dentry->d_name.name, dentry->d_name.len); |
Tsutomu Itoh | b532402 | 2011-07-19 07:27:20 +0000 | [diff] [blame] | 4014 | if (ret) |
| 4015 | goto out; |
Josef Bacik | 7b12876 | 2008-07-24 12:17:14 -0400 | [diff] [blame] | 4016 | |
Yan, Zheng | a22285a | 2010-05-16 10:48:46 -0400 | [diff] [blame] | 4017 | if (inode->i_nlink == 0) { |
Josef Bacik | 7b12876 | 2008-07-24 12:17:14 -0400 | [diff] [blame] | 4018 | ret = btrfs_orphan_add(trans, inode); |
Tsutomu Itoh | b532402 | 2011-07-19 07:27:20 +0000 | [diff] [blame] | 4019 | if (ret) |
| 4020 | goto out; |
Yan, Zheng | a22285a | 2010-05-16 10:48:46 -0400 | [diff] [blame] | 4021 | } |
Josef Bacik | 7b12876 | 2008-07-24 12:17:14 -0400 | [diff] [blame] | 4022 | |
Tsutomu Itoh | b532402 | 2011-07-19 07:27:20 +0000 | [diff] [blame] | 4023 | out: |
Josef Bacik | d52be81 | 2013-05-29 14:54:47 -0400 | [diff] [blame] | 4024 | btrfs_end_transaction(trans, root); |
Liu Bo | b53d3f5 | 2012-11-14 14:34:34 +0000 | [diff] [blame] | 4025 | btrfs_btree_balance_dirty(root); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4026 | return ret; |
| 4027 | } |
| 4028 | |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 4029 | int btrfs_unlink_subvol(struct btrfs_trans_handle *trans, |
| 4030 | struct btrfs_root *root, |
| 4031 | struct inode *dir, u64 objectid, |
| 4032 | const char *name, int name_len) |
| 4033 | { |
| 4034 | struct btrfs_path *path; |
| 4035 | struct extent_buffer *leaf; |
| 4036 | struct btrfs_dir_item *di; |
| 4037 | struct btrfs_key key; |
| 4038 | u64 index; |
| 4039 | int ret; |
Li Zefan | 33345d01 | 2011-04-20 10:31:50 +0800 | [diff] [blame] | 4040 | u64 dir_ino = btrfs_ino(dir); |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 4041 | |
| 4042 | path = btrfs_alloc_path(); |
| 4043 | if (!path) |
| 4044 | return -ENOMEM; |
| 4045 | |
Li Zefan | 33345d01 | 2011-04-20 10:31:50 +0800 | [diff] [blame] | 4046 | di = btrfs_lookup_dir_item(trans, root, path, dir_ino, |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 4047 | name, name_len, -1); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 4048 | if (IS_ERR_OR_NULL(di)) { |
| 4049 | if (!di) |
| 4050 | ret = -ENOENT; |
| 4051 | else |
| 4052 | ret = PTR_ERR(di); |
| 4053 | goto out; |
| 4054 | } |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 4055 | |
| 4056 | leaf = path->nodes[0]; |
| 4057 | btrfs_dir_item_key_to_cpu(leaf, di, &key); |
| 4058 | WARN_ON(key.type != BTRFS_ROOT_ITEM_KEY || key.objectid != objectid); |
| 4059 | ret = btrfs_delete_one_dir_name(trans, root, path, di); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 4060 | if (ret) { |
| 4061 | btrfs_abort_transaction(trans, root, ret); |
| 4062 | goto out; |
| 4063 | } |
David Sterba | b3b4aa7 | 2011-04-21 01:20:15 +0200 | [diff] [blame] | 4064 | btrfs_release_path(path); |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 4065 | |
| 4066 | ret = btrfs_del_root_ref(trans, root->fs_info->tree_root, |
| 4067 | objectid, root->root_key.objectid, |
Li Zefan | 33345d01 | 2011-04-20 10:31:50 +0800 | [diff] [blame] | 4068 | dir_ino, &index, name, name_len); |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 4069 | if (ret < 0) { |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 4070 | if (ret != -ENOENT) { |
| 4071 | btrfs_abort_transaction(trans, root, ret); |
| 4072 | goto out; |
| 4073 | } |
Li Zefan | 33345d01 | 2011-04-20 10:31:50 +0800 | [diff] [blame] | 4074 | di = btrfs_search_dir_index_item(root, path, dir_ino, |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 4075 | name, name_len); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 4076 | if (IS_ERR_OR_NULL(di)) { |
| 4077 | if (!di) |
| 4078 | ret = -ENOENT; |
| 4079 | else |
| 4080 | ret = PTR_ERR(di); |
| 4081 | btrfs_abort_transaction(trans, root, ret); |
| 4082 | goto out; |
| 4083 | } |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 4084 | |
| 4085 | leaf = path->nodes[0]; |
| 4086 | btrfs_item_key_to_cpu(leaf, &key, path->slots[0]); |
David Sterba | b3b4aa7 | 2011-04-21 01:20:15 +0200 | [diff] [blame] | 4087 | btrfs_release_path(path); |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 4088 | index = key.offset; |
| 4089 | } |
David Sterba | b3b4aa7 | 2011-04-21 01:20:15 +0200 | [diff] [blame] | 4090 | btrfs_release_path(path); |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 4091 | |
Miao Xie | 16cdcec | 2011-04-22 18:12:22 +0800 | [diff] [blame] | 4092 | ret = btrfs_delete_delayed_dir_index(trans, root, dir, index); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 4093 | if (ret) { |
| 4094 | btrfs_abort_transaction(trans, root, ret); |
| 4095 | goto out; |
| 4096 | } |
Miao Xie | 16cdcec | 2011-04-22 18:12:22 +0800 | [diff] [blame] | 4097 | |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 4098 | btrfs_i_size_write(dir, dir->i_size - name_len * 2); |
Josef Bacik | 0c4d2d9 | 2012-04-05 15:03:02 -0400 | [diff] [blame] | 4099 | inode_inc_iversion(dir); |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 4100 | dir->i_mtime = dir->i_ctime = CURRENT_TIME; |
Josef Bacik | 5a24e84 | 2012-08-08 10:12:59 -0600 | [diff] [blame] | 4101 | ret = btrfs_update_inode_fallback(trans, root, dir); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 4102 | if (ret) |
| 4103 | btrfs_abort_transaction(trans, root, ret); |
| 4104 | out: |
Josef Bacik | 71d7aed | 2011-06-14 14:24:32 -0400 | [diff] [blame] | 4105 | btrfs_free_path(path); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 4106 | return ret; |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 4107 | } |
| 4108 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4109 | static int btrfs_rmdir(struct inode *dir, struct dentry *dentry) |
| 4110 | { |
| 4111 | struct inode *inode = dentry->d_inode; |
Chris Mason | 1832a6d | 2007-12-21 16:27:21 -0500 | [diff] [blame] | 4112 | int err = 0; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4113 | struct btrfs_root *root = BTRFS_I(dir)->root; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4114 | struct btrfs_trans_handle *trans; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4115 | |
David Sterba | b3ae244 | 2012-09-13 16:04:34 -0600 | [diff] [blame] | 4116 | if (inode->i_size > BTRFS_EMPTY_DIR_SIZE) |
Yan | 134d451 | 2007-10-25 15:49:25 -0400 | [diff] [blame] | 4117 | return -ENOTEMPTY; |
David Sterba | b3ae244 | 2012-09-13 16:04:34 -0600 | [diff] [blame] | 4118 | if (btrfs_ino(inode) == BTRFS_FIRST_FREE_OBJECTID) |
| 4119 | return -EPERM; |
Yan | 134d451 | 2007-10-25 15:49:25 -0400 | [diff] [blame] | 4120 | |
Josef Bacik | d52be81 | 2013-05-29 14:54:47 -0400 | [diff] [blame] | 4121 | trans = __unlink_start_trans(dir); |
Yan, Zheng | a22285a | 2010-05-16 10:48:46 -0400 | [diff] [blame] | 4122 | if (IS_ERR(trans)) |
Josef Bacik | 5df6a9f | 2009-11-10 21:23:48 -0500 | [diff] [blame] | 4123 | return PTR_ERR(trans); |
Josef Bacik | 5df6a9f | 2009-11-10 21:23:48 -0500 | [diff] [blame] | 4124 | |
Li Zefan | 33345d01 | 2011-04-20 10:31:50 +0800 | [diff] [blame] | 4125 | if (unlikely(btrfs_ino(inode) == BTRFS_EMPTY_SUBVOL_DIR_OBJECTID)) { |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 4126 | err = btrfs_unlink_subvol(trans, root, dir, |
| 4127 | BTRFS_I(inode)->location.objectid, |
| 4128 | dentry->d_name.name, |
| 4129 | dentry->d_name.len); |
| 4130 | goto out; |
| 4131 | } |
| 4132 | |
Josef Bacik | 7b12876 | 2008-07-24 12:17:14 -0400 | [diff] [blame] | 4133 | err = btrfs_orphan_add(trans, inode); |
| 4134 | if (err) |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 4135 | goto out; |
Josef Bacik | 7b12876 | 2008-07-24 12:17:14 -0400 | [diff] [blame] | 4136 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4137 | /* now the directory is empty */ |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 4138 | err = btrfs_unlink_inode(trans, root, dir, dentry->d_inode, |
| 4139 | dentry->d_name.name, dentry->d_name.len); |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 4140 | if (!err) |
Chris Mason | dbe674a | 2008-07-17 12:54:05 -0400 | [diff] [blame] | 4141 | btrfs_i_size_write(inode, 0); |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 4142 | out: |
Josef Bacik | d52be81 | 2013-05-29 14:54:47 -0400 | [diff] [blame] | 4143 | btrfs_end_transaction(trans, root); |
Liu Bo | b53d3f5 | 2012-11-14 14:34:34 +0000 | [diff] [blame] | 4144 | btrfs_btree_balance_dirty(root); |
Chris Mason | 3954401 | 2007-12-12 14:38:19 -0500 | [diff] [blame] | 4145 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4146 | return err; |
| 4147 | } |
| 4148 | |
Chris Mason | 323ac95 | 2008-10-01 19:05:46 -0400 | [diff] [blame] | 4149 | /* |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4150 | * this can truncate away extent items, csum items and directory items. |
| 4151 | * 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] | 4152 | * any higher than new_size |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4153 | * |
| 4154 | * csum items that cross the new i_size are truncated to the new size |
| 4155 | * as well. |
Josef Bacik | 7b12876 | 2008-07-24 12:17:14 -0400 | [diff] [blame] | 4156 | * |
| 4157 | * min_type is the minimum key type to truncate down to. If set to 0, this |
| 4158 | * 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] | 4159 | */ |
Yan, Zheng | 8082510 | 2009-11-12 09:35:36 +0000 | [diff] [blame] | 4160 | int btrfs_truncate_inode_items(struct btrfs_trans_handle *trans, |
| 4161 | struct btrfs_root *root, |
| 4162 | struct inode *inode, |
| 4163 | u64 new_size, u32 min_type) |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4164 | { |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4165 | struct btrfs_path *path; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 4166 | struct extent_buffer *leaf; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4167 | struct btrfs_file_extent_item *fi; |
Yan, Zheng | 8082510 | 2009-11-12 09:35:36 +0000 | [diff] [blame] | 4168 | struct btrfs_key key; |
| 4169 | struct btrfs_key found_key; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4170 | u64 extent_start = 0; |
Chris Mason | db94535 | 2007-10-15 16:15:53 -0400 | [diff] [blame] | 4171 | u64 extent_num_bytes = 0; |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 4172 | u64 extent_offset = 0; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4173 | u64 item_end = 0; |
Josef Bacik | 7f4f6e0 | 2013-08-29 16:43:28 -0400 | [diff] [blame] | 4174 | u64 last_size = (u64)-1; |
Yan, Zheng | 8082510 | 2009-11-12 09:35:36 +0000 | [diff] [blame] | 4175 | u32 found_type = (u8)-1; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4176 | int found_extent; |
| 4177 | int del_item; |
Chris Mason | 85e21ba | 2008-01-29 15:11:36 -0500 | [diff] [blame] | 4178 | int pending_del_nr = 0; |
| 4179 | int pending_del_slot = 0; |
Chris Mason | 179e29e | 2007-11-01 11:28:41 -0400 | [diff] [blame] | 4180 | int extent_type = -1; |
Yan, Zheng | 8082510 | 2009-11-12 09:35:36 +0000 | [diff] [blame] | 4181 | int ret; |
| 4182 | int err = 0; |
Li Zefan | 33345d01 | 2011-04-20 10:31:50 +0800 | [diff] [blame] | 4183 | u64 ino = btrfs_ino(inode); |
Yan, Zheng | 8082510 | 2009-11-12 09:35:36 +0000 | [diff] [blame] | 4184 | |
| 4185 | BUG_ON(new_size > 0 && min_type != BTRFS_EXTENT_DATA_KEY); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4186 | |
Mark Fasheh | 0eb0e19 | 2011-07-12 16:44:10 -0700 | [diff] [blame] | 4187 | path = btrfs_alloc_path(); |
| 4188 | if (!path) |
| 4189 | return -ENOMEM; |
| 4190 | path->reada = -1; |
| 4191 | |
Josef Bacik | 5dc562c | 2012-08-17 13:14:17 -0400 | [diff] [blame] | 4192 | /* |
| 4193 | * We want to drop from the next block forward in case this new size is |
| 4194 | * not block aligned since we will be keeping the last block of the |
| 4195 | * extent just the way it is. |
| 4196 | */ |
Miao Xie | 27cdeb7 | 2014-04-02 19:51:05 +0800 | [diff] [blame] | 4197 | if (test_bit(BTRFS_ROOT_REF_COWS, &root->state) || |
| 4198 | root == root->fs_info->tree_root) |
Qu Wenruo | fda2832 | 2013-02-26 08:10:22 +0000 | [diff] [blame] | 4199 | btrfs_drop_extent_cache(inode, ALIGN(new_size, |
| 4200 | root->sectorsize), (u64)-1, 0); |
Yan, Zheng | 8082510 | 2009-11-12 09:35:36 +0000 | [diff] [blame] | 4201 | |
Miao Xie | 16cdcec | 2011-04-22 18:12:22 +0800 | [diff] [blame] | 4202 | /* |
| 4203 | * This function is also used to drop the items in the log tree before |
| 4204 | * we relog the inode, so if root != BTRFS_I(inode)->root, it means |
| 4205 | * it is used to drop the loged items. So we shouldn't kill the delayed |
| 4206 | * items. |
| 4207 | */ |
| 4208 | if (min_type == 0 && root == BTRFS_I(inode)->root) |
| 4209 | btrfs_kill_delayed_inode_items(inode); |
| 4210 | |
Li Zefan | 33345d01 | 2011-04-20 10:31:50 +0800 | [diff] [blame] | 4211 | key.objectid = ino; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4212 | key.offset = (u64)-1; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 4213 | key.type = (u8)-1; |
| 4214 | |
Chris Mason | 85e21ba | 2008-01-29 15:11:36 -0500 | [diff] [blame] | 4215 | search_again: |
Chris Mason | b947343 | 2009-03-13 11:00:37 -0400 | [diff] [blame] | 4216 | path->leave_spinning = 1; |
Chris Mason | 85e21ba | 2008-01-29 15:11:36 -0500 | [diff] [blame] | 4217 | ret = btrfs_search_slot(trans, root, &key, path, -1, 1); |
Yan, Zheng | 8082510 | 2009-11-12 09:35:36 +0000 | [diff] [blame] | 4218 | if (ret < 0) { |
| 4219 | err = ret; |
| 4220 | goto out; |
| 4221 | } |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 4222 | |
Chris Mason | 85e21ba | 2008-01-29 15:11:36 -0500 | [diff] [blame] | 4223 | if (ret > 0) { |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 4224 | /* there are no items in the tree for us to truncate, we're |
| 4225 | * done |
| 4226 | */ |
Yan, Zheng | 8082510 | 2009-11-12 09:35:36 +0000 | [diff] [blame] | 4227 | if (path->slots[0] == 0) |
| 4228 | goto out; |
Chris Mason | 85e21ba | 2008-01-29 15:11:36 -0500 | [diff] [blame] | 4229 | path->slots[0]--; |
| 4230 | } |
| 4231 | |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 4232 | while (1) { |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4233 | fi = NULL; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 4234 | leaf = path->nodes[0]; |
| 4235 | btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]); |
David Sterba | 962a298 | 2014-06-04 18:41:45 +0200 | [diff] [blame] | 4236 | found_type = found_key.type; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4237 | |
Li Zefan | 33345d01 | 2011-04-20 10:31:50 +0800 | [diff] [blame] | 4238 | if (found_key.objectid != ino) |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4239 | break; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 4240 | |
Chris Mason | 85e21ba | 2008-01-29 15:11:36 -0500 | [diff] [blame] | 4241 | if (found_type < min_type) |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4242 | break; |
| 4243 | |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 4244 | item_end = found_key.offset; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4245 | if (found_type == BTRFS_EXTENT_DATA_KEY) { |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 4246 | fi = btrfs_item_ptr(leaf, path->slots[0], |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4247 | struct btrfs_file_extent_item); |
Chris Mason | 179e29e | 2007-11-01 11:28:41 -0400 | [diff] [blame] | 4248 | extent_type = btrfs_file_extent_type(leaf, fi); |
| 4249 | if (extent_type != BTRFS_FILE_EXTENT_INLINE) { |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 4250 | item_end += |
Chris Mason | db94535 | 2007-10-15 16:15:53 -0400 | [diff] [blame] | 4251 | btrfs_file_extent_num_bytes(leaf, fi); |
Chris Mason | 179e29e | 2007-11-01 11:28:41 -0400 | [diff] [blame] | 4252 | } else if (extent_type == BTRFS_FILE_EXTENT_INLINE) { |
Chris Mason | 179e29e | 2007-11-01 11:28:41 -0400 | [diff] [blame] | 4253 | item_end += btrfs_file_extent_inline_len(leaf, |
Chris Mason | 514ac8a | 2014-01-03 21:07:00 -0800 | [diff] [blame] | 4254 | path->slots[0], fi); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4255 | } |
Yan | 008630c | 2007-11-07 13:31:09 -0500 | [diff] [blame] | 4256 | item_end--; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4257 | } |
Yan, Zheng | 8082510 | 2009-11-12 09:35:36 +0000 | [diff] [blame] | 4258 | if (found_type > min_type) { |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4259 | del_item = 1; |
Yan, Zheng | 8082510 | 2009-11-12 09:35:36 +0000 | [diff] [blame] | 4260 | } else { |
| 4261 | if (item_end < new_size) |
| 4262 | break; |
| 4263 | if (found_key.offset >= new_size) |
| 4264 | del_item = 1; |
| 4265 | else |
| 4266 | del_item = 0; |
| 4267 | } |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4268 | found_extent = 0; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4269 | /* FIXME, shrink the extent if the ref count is only 1 */ |
Chris Mason | 179e29e | 2007-11-01 11:28:41 -0400 | [diff] [blame] | 4270 | if (found_type != BTRFS_EXTENT_DATA_KEY) |
| 4271 | goto delete; |
| 4272 | |
Josef Bacik | 7f4f6e0 | 2013-08-29 16:43:28 -0400 | [diff] [blame] | 4273 | if (del_item) |
| 4274 | last_size = found_key.offset; |
| 4275 | else |
| 4276 | last_size = new_size; |
| 4277 | |
Chris Mason | 179e29e | 2007-11-01 11:28:41 -0400 | [diff] [blame] | 4278 | if (extent_type != BTRFS_FILE_EXTENT_INLINE) { |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4279 | u64 num_dec; |
Chris Mason | db94535 | 2007-10-15 16:15:53 -0400 | [diff] [blame] | 4280 | extent_start = btrfs_file_extent_disk_bytenr(leaf, fi); |
Miao Xie | f70a9a6b | 2012-01-12 19:10:12 -0500 | [diff] [blame] | 4281 | if (!del_item) { |
Chris Mason | db94535 | 2007-10-15 16:15:53 -0400 | [diff] [blame] | 4282 | u64 orig_num_bytes = |
| 4283 | btrfs_file_extent_num_bytes(leaf, fi); |
Qu Wenruo | fda2832 | 2013-02-26 08:10:22 +0000 | [diff] [blame] | 4284 | extent_num_bytes = ALIGN(new_size - |
| 4285 | found_key.offset, |
| 4286 | root->sectorsize); |
Chris Mason | db94535 | 2007-10-15 16:15:53 -0400 | [diff] [blame] | 4287 | btrfs_set_file_extent_num_bytes(leaf, fi, |
| 4288 | extent_num_bytes); |
| 4289 | num_dec = (orig_num_bytes - |
Chris Mason | 9069218 | 2008-02-08 13:49:28 -0500 | [diff] [blame] | 4290 | extent_num_bytes); |
Miao Xie | 27cdeb7 | 2014-04-02 19:51:05 +0800 | [diff] [blame] | 4291 | if (test_bit(BTRFS_ROOT_REF_COWS, |
| 4292 | &root->state) && |
| 4293 | extent_start != 0) |
Yan Zheng | a76a3cd | 2008-10-09 11:46:29 -0400 | [diff] [blame] | 4294 | inode_sub_bytes(inode, num_dec); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 4295 | btrfs_mark_buffer_dirty(leaf); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4296 | } else { |
Chris Mason | db94535 | 2007-10-15 16:15:53 -0400 | [diff] [blame] | 4297 | extent_num_bytes = |
| 4298 | btrfs_file_extent_disk_num_bytes(leaf, |
| 4299 | fi); |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 4300 | extent_offset = found_key.offset - |
| 4301 | btrfs_file_extent_offset(leaf, fi); |
| 4302 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4303 | /* FIXME blocksize != 4096 */ |
Chris Mason | 9069218 | 2008-02-08 13:49:28 -0500 | [diff] [blame] | 4304 | num_dec = btrfs_file_extent_num_bytes(leaf, fi); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4305 | if (extent_start != 0) { |
| 4306 | found_extent = 1; |
Miao Xie | 27cdeb7 | 2014-04-02 19:51:05 +0800 | [diff] [blame] | 4307 | if (test_bit(BTRFS_ROOT_REF_COWS, |
| 4308 | &root->state)) |
Yan Zheng | a76a3cd | 2008-10-09 11:46:29 -0400 | [diff] [blame] | 4309 | inode_sub_bytes(inode, num_dec); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4310 | } |
| 4311 | } |
Chris Mason | 9069218 | 2008-02-08 13:49:28 -0500 | [diff] [blame] | 4312 | } else if (extent_type == BTRFS_FILE_EXTENT_INLINE) { |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 4313 | /* |
| 4314 | * we can't truncate inline items that have had |
| 4315 | * special encodings |
| 4316 | */ |
| 4317 | if (!del_item && |
| 4318 | btrfs_file_extent_compression(leaf, fi) == 0 && |
| 4319 | btrfs_file_extent_encryption(leaf, fi) == 0 && |
| 4320 | btrfs_file_extent_other_encoding(leaf, fi) == 0) { |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 4321 | u32 size = new_size - found_key.offset; |
| 4322 | |
Miao Xie | 27cdeb7 | 2014-04-02 19:51:05 +0800 | [diff] [blame] | 4323 | if (test_bit(BTRFS_ROOT_REF_COWS, &root->state)) |
Yan Zheng | a76a3cd | 2008-10-09 11:46:29 -0400 | [diff] [blame] | 4324 | inode_sub_bytes(inode, item_end + 1 - |
| 4325 | new_size); |
Chris Mason | 514ac8a | 2014-01-03 21:07:00 -0800 | [diff] [blame] | 4326 | |
| 4327 | /* |
| 4328 | * update the ram bytes to properly reflect |
| 4329 | * the new size of our item |
| 4330 | */ |
| 4331 | btrfs_set_file_extent_ram_bytes(leaf, fi, size); |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 4332 | size = |
| 4333 | btrfs_file_extent_calc_inline_size(size); |
Tsutomu Itoh | afe5fea | 2013-04-16 05:18:22 +0000 | [diff] [blame] | 4334 | btrfs_truncate_item(root, path, size, 1); |
Miao Xie | 27cdeb7 | 2014-04-02 19:51:05 +0800 | [diff] [blame] | 4335 | } else if (test_bit(BTRFS_ROOT_REF_COWS, |
| 4336 | &root->state)) { |
Yan Zheng | a76a3cd | 2008-10-09 11:46:29 -0400 | [diff] [blame] | 4337 | inode_sub_bytes(inode, item_end + 1 - |
| 4338 | found_key.offset); |
Chris Mason | 9069218 | 2008-02-08 13:49:28 -0500 | [diff] [blame] | 4339 | } |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4340 | } |
Chris Mason | 179e29e | 2007-11-01 11:28:41 -0400 | [diff] [blame] | 4341 | delete: |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4342 | if (del_item) { |
Chris Mason | 85e21ba | 2008-01-29 15:11:36 -0500 | [diff] [blame] | 4343 | if (!pending_del_nr) { |
| 4344 | /* no pending yet, add ourselves */ |
| 4345 | pending_del_slot = path->slots[0]; |
| 4346 | pending_del_nr = 1; |
| 4347 | } else if (pending_del_nr && |
| 4348 | path->slots[0] + 1 == pending_del_slot) { |
| 4349 | /* hop on the pending chunk */ |
| 4350 | pending_del_nr++; |
| 4351 | pending_del_slot = path->slots[0]; |
| 4352 | } else { |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 4353 | BUG(); |
Chris Mason | 85e21ba | 2008-01-29 15:11:36 -0500 | [diff] [blame] | 4354 | } |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4355 | } else { |
| 4356 | break; |
| 4357 | } |
Miao Xie | 27cdeb7 | 2014-04-02 19:51:05 +0800 | [diff] [blame] | 4358 | if (found_extent && |
| 4359 | (test_bit(BTRFS_ROOT_REF_COWS, &root->state) || |
| 4360 | root == root->fs_info->tree_root)) { |
Chris Mason | b947343 | 2009-03-13 11:00:37 -0400 | [diff] [blame] | 4361 | btrfs_set_path_blocking(path); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4362 | ret = btrfs_free_extent(trans, root, extent_start, |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 4363 | extent_num_bytes, 0, |
| 4364 | btrfs_header_owner(leaf), |
Arne Jansen | 66d7e7f | 2011-09-12 15:26:38 +0200 | [diff] [blame] | 4365 | ino, extent_offset, 0); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4366 | BUG_ON(ret); |
| 4367 | } |
Chris Mason | 85e21ba | 2008-01-29 15:11:36 -0500 | [diff] [blame] | 4368 | |
Yan, Zheng | 8082510 | 2009-11-12 09:35:36 +0000 | [diff] [blame] | 4369 | if (found_type == BTRFS_INODE_ITEM_KEY) |
| 4370 | break; |
| 4371 | |
| 4372 | if (path->slots[0] == 0 || |
| 4373 | path->slots[0] != pending_del_slot) { |
Yan, Zheng | 8082510 | 2009-11-12 09:35:36 +0000 | [diff] [blame] | 4374 | if (pending_del_nr) { |
| 4375 | ret = btrfs_del_items(trans, root, path, |
| 4376 | pending_del_slot, |
| 4377 | pending_del_nr); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 4378 | if (ret) { |
| 4379 | btrfs_abort_transaction(trans, |
| 4380 | root, ret); |
| 4381 | goto error; |
| 4382 | } |
Yan, Zheng | 8082510 | 2009-11-12 09:35:36 +0000 | [diff] [blame] | 4383 | pending_del_nr = 0; |
| 4384 | } |
David Sterba | b3b4aa7 | 2011-04-21 01:20:15 +0200 | [diff] [blame] | 4385 | btrfs_release_path(path); |
Chris Mason | 85e21ba | 2008-01-29 15:11:36 -0500 | [diff] [blame] | 4386 | goto search_again; |
Yan, Zheng | 8082510 | 2009-11-12 09:35:36 +0000 | [diff] [blame] | 4387 | } else { |
| 4388 | path->slots[0]--; |
Chris Mason | 85e21ba | 2008-01-29 15:11:36 -0500 | [diff] [blame] | 4389 | } |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4390 | } |
Yan, Zheng | 8082510 | 2009-11-12 09:35:36 +0000 | [diff] [blame] | 4391 | out: |
Chris Mason | 85e21ba | 2008-01-29 15:11:36 -0500 | [diff] [blame] | 4392 | if (pending_del_nr) { |
| 4393 | ret = btrfs_del_items(trans, root, path, pending_del_slot, |
| 4394 | pending_del_nr); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 4395 | if (ret) |
| 4396 | btrfs_abort_transaction(trans, root, ret); |
Chris Mason | 85e21ba | 2008-01-29 15:11:36 -0500 | [diff] [blame] | 4397 | } |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 4398 | error: |
Filipe Manana | dac5705 | 2014-08-29 20:54:26 +0100 | [diff] [blame] | 4399 | if (last_size != (u64)-1 && |
| 4400 | root->root_key.objectid != BTRFS_TREE_LOG_OBJECTID) |
Josef Bacik | 7f4f6e0 | 2013-08-29 16:43:28 -0400 | [diff] [blame] | 4401 | btrfs_ordered_update_i_size(inode, last_size, NULL); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4402 | btrfs_free_path(path); |
Yan, Zheng | 8082510 | 2009-11-12 09:35:36 +0000 | [diff] [blame] | 4403 | return err; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4404 | } |
| 4405 | |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 4406 | /* |
Josef Bacik | 2aaa665 | 2012-08-29 14:27:18 -0400 | [diff] [blame] | 4407 | * btrfs_truncate_page - read, zero a chunk and write a page |
| 4408 | * @inode - inode that we're zeroing |
| 4409 | * @from - the offset to start zeroing |
| 4410 | * @len - the length to zero, 0 to zero the entire range respective to the |
| 4411 | * offset |
| 4412 | * @front - zero up to the offset instead of from the offset on |
| 4413 | * |
| 4414 | * This will find the page for the "from" offset and cow the page and zero the |
| 4415 | * 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] | 4416 | */ |
Josef Bacik | 2aaa665 | 2012-08-29 14:27:18 -0400 | [diff] [blame] | 4417 | int btrfs_truncate_page(struct inode *inode, loff_t from, loff_t len, |
| 4418 | int front) |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 4419 | { |
Josef Bacik | 2aaa665 | 2012-08-29 14:27:18 -0400 | [diff] [blame] | 4420 | struct address_space *mapping = inode->i_mapping; |
Chris Mason | db94535 | 2007-10-15 16:15:53 -0400 | [diff] [blame] | 4421 | struct btrfs_root *root = BTRFS_I(inode)->root; |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 4422 | struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree; |
| 4423 | struct btrfs_ordered_extent *ordered; |
Josef Bacik | 2ac55d4 | 2010-02-03 19:33:23 +0000 | [diff] [blame] | 4424 | struct extent_state *cached_state = NULL; |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 4425 | char *kaddr; |
Chris Mason | db94535 | 2007-10-15 16:15:53 -0400 | [diff] [blame] | 4426 | u32 blocksize = root->sectorsize; |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 4427 | pgoff_t index = from >> PAGE_CACHE_SHIFT; |
| 4428 | unsigned offset = from & (PAGE_CACHE_SIZE-1); |
| 4429 | struct page *page; |
Josef Bacik | 3b16a4e | 2011-09-21 15:05:58 -0400 | [diff] [blame] | 4430 | gfp_t mask = btrfs_alloc_write_mask(mapping); |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 4431 | int ret = 0; |
| 4432 | u64 page_start; |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 4433 | u64 page_end; |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 4434 | |
Josef Bacik | 2aaa665 | 2012-08-29 14:27:18 -0400 | [diff] [blame] | 4435 | if ((offset & (blocksize - 1)) == 0 && |
| 4436 | (!len || ((len & (blocksize - 1)) == 0))) |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 4437 | goto out; |
Yan, Zheng | 0ca1f7c | 2010-05-16 10:48:47 -0400 | [diff] [blame] | 4438 | ret = btrfs_delalloc_reserve_space(inode, PAGE_CACHE_SIZE); |
Josef Bacik | 5d5e103 | 2009-10-13 16:46:49 -0400 | [diff] [blame] | 4439 | if (ret) |
| 4440 | goto out; |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 4441 | |
Chris Mason | 211c17f | 2008-05-15 09:13:45 -0400 | [diff] [blame] | 4442 | again: |
Josef Bacik | 3b16a4e | 2011-09-21 15:05:58 -0400 | [diff] [blame] | 4443 | page = find_or_create_page(mapping, index, mask); |
Josef Bacik | 5d5e103 | 2009-10-13 16:46:49 -0400 | [diff] [blame] | 4444 | if (!page) { |
Yan, Zheng | 0ca1f7c | 2010-05-16 10:48:47 -0400 | [diff] [blame] | 4445 | btrfs_delalloc_release_space(inode, PAGE_CACHE_SIZE); |
Miao Xie | ac6a2b3 | 2012-12-05 10:56:13 +0000 | [diff] [blame] | 4446 | ret = -ENOMEM; |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 4447 | goto out; |
Josef Bacik | 5d5e103 | 2009-10-13 16:46:49 -0400 | [diff] [blame] | 4448 | } |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 4449 | |
| 4450 | page_start = page_offset(page); |
| 4451 | page_end = page_start + PAGE_CACHE_SIZE - 1; |
| 4452 | |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 4453 | if (!PageUptodate(page)) { |
| 4454 | ret = btrfs_readpage(NULL, page); |
| 4455 | lock_page(page); |
Chris Mason | 211c17f | 2008-05-15 09:13:45 -0400 | [diff] [blame] | 4456 | if (page->mapping != mapping) { |
| 4457 | unlock_page(page); |
| 4458 | page_cache_release(page); |
| 4459 | goto again; |
| 4460 | } |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 4461 | if (!PageUptodate(page)) { |
| 4462 | ret = -EIO; |
Chris Mason | 8964222 | 2008-07-24 09:41:53 -0400 | [diff] [blame] | 4463 | goto out_unlock; |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 4464 | } |
| 4465 | } |
Chris Mason | 211c17f | 2008-05-15 09:13:45 -0400 | [diff] [blame] | 4466 | wait_on_page_writeback(page); |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 4467 | |
Jeff Mahoney | d008237 | 2012-03-01 14:57:19 +0100 | [diff] [blame] | 4468 | lock_extent_bits(io_tree, page_start, page_end, 0, &cached_state); |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 4469 | set_page_extent_mapped(page); |
| 4470 | |
| 4471 | ordered = btrfs_lookup_ordered_extent(inode, page_start); |
| 4472 | if (ordered) { |
Josef Bacik | 2ac55d4 | 2010-02-03 19:33:23 +0000 | [diff] [blame] | 4473 | unlock_extent_cached(io_tree, page_start, page_end, |
| 4474 | &cached_state, GFP_NOFS); |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 4475 | unlock_page(page); |
| 4476 | page_cache_release(page); |
Chris Mason | eb84ae0 | 2008-07-17 13:53:27 -0400 | [diff] [blame] | 4477 | btrfs_start_ordered_extent(inode, ordered, 1); |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 4478 | btrfs_put_ordered_extent(ordered); |
| 4479 | goto again; |
| 4480 | } |
| 4481 | |
Josef Bacik | 2ac55d4 | 2010-02-03 19:33:23 +0000 | [diff] [blame] | 4482 | clear_extent_bit(&BTRFS_I(inode)->io_tree, page_start, page_end, |
Liu Bo | 9e8a4a8 | 2012-09-05 19:10:51 -0600 | [diff] [blame] | 4483 | EXTENT_DIRTY | EXTENT_DELALLOC | |
| 4484 | EXTENT_DO_ACCOUNTING | EXTENT_DEFRAG, |
Josef Bacik | 2ac55d4 | 2010-02-03 19:33:23 +0000 | [diff] [blame] | 4485 | 0, 0, &cached_state, GFP_NOFS); |
Josef Bacik | 5d5e103 | 2009-10-13 16:46:49 -0400 | [diff] [blame] | 4486 | |
Josef Bacik | 2ac55d4 | 2010-02-03 19:33:23 +0000 | [diff] [blame] | 4487 | ret = btrfs_set_extent_delalloc(inode, page_start, page_end, |
| 4488 | &cached_state); |
Josef Bacik | 9ed74f2 | 2009-09-11 16:12:44 -0400 | [diff] [blame] | 4489 | if (ret) { |
Josef Bacik | 2ac55d4 | 2010-02-03 19:33:23 +0000 | [diff] [blame] | 4490 | unlock_extent_cached(io_tree, page_start, page_end, |
| 4491 | &cached_state, GFP_NOFS); |
Josef Bacik | 9ed74f2 | 2009-09-11 16:12:44 -0400 | [diff] [blame] | 4492 | goto out_unlock; |
| 4493 | } |
| 4494 | |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 4495 | if (offset != PAGE_CACHE_SIZE) { |
Josef Bacik | 2aaa665 | 2012-08-29 14:27:18 -0400 | [diff] [blame] | 4496 | if (!len) |
| 4497 | len = PAGE_CACHE_SIZE - offset; |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 4498 | kaddr = kmap(page); |
Josef Bacik | 2aaa665 | 2012-08-29 14:27:18 -0400 | [diff] [blame] | 4499 | if (front) |
| 4500 | memset(kaddr, 0, offset); |
| 4501 | else |
| 4502 | memset(kaddr + offset, 0, len); |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 4503 | flush_dcache_page(page); |
| 4504 | kunmap(page); |
| 4505 | } |
Chris Mason | 247e743 | 2008-07-17 12:53:51 -0400 | [diff] [blame] | 4506 | ClearPageChecked(page); |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 4507 | set_page_dirty(page); |
Josef Bacik | 2ac55d4 | 2010-02-03 19:33:23 +0000 | [diff] [blame] | 4508 | unlock_extent_cached(io_tree, page_start, page_end, &cached_state, |
| 4509 | GFP_NOFS); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4510 | |
Chris Mason | 8964222 | 2008-07-24 09:41:53 -0400 | [diff] [blame] | 4511 | out_unlock: |
Josef Bacik | 5d5e103 | 2009-10-13 16:46:49 -0400 | [diff] [blame] | 4512 | if (ret) |
Yan, Zheng | 0ca1f7c | 2010-05-16 10:48:47 -0400 | [diff] [blame] | 4513 | btrfs_delalloc_release_space(inode, PAGE_CACHE_SIZE); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4514 | unlock_page(page); |
| 4515 | page_cache_release(page); |
| 4516 | out: |
| 4517 | return ret; |
| 4518 | } |
| 4519 | |
Josef Bacik | 16e7549 | 2013-10-22 12:18:51 -0400 | [diff] [blame] | 4520 | static int maybe_insert_hole(struct btrfs_root *root, struct inode *inode, |
| 4521 | u64 offset, u64 len) |
| 4522 | { |
| 4523 | struct btrfs_trans_handle *trans; |
| 4524 | int ret; |
| 4525 | |
| 4526 | /* |
| 4527 | * Still need to make sure the inode looks like it's been updated so |
| 4528 | * that any holes get logged if we fsync. |
| 4529 | */ |
| 4530 | if (btrfs_fs_incompat(root->fs_info, NO_HOLES)) { |
| 4531 | BTRFS_I(inode)->last_trans = root->fs_info->generation; |
| 4532 | BTRFS_I(inode)->last_sub_trans = root->log_transid; |
| 4533 | BTRFS_I(inode)->last_log_commit = root->last_log_commit; |
| 4534 | return 0; |
| 4535 | } |
| 4536 | |
| 4537 | /* |
| 4538 | * 1 - for the one we're dropping |
| 4539 | * 1 - for the one we're adding |
| 4540 | * 1 - for updating the inode. |
| 4541 | */ |
| 4542 | trans = btrfs_start_transaction(root, 3); |
| 4543 | if (IS_ERR(trans)) |
| 4544 | return PTR_ERR(trans); |
| 4545 | |
| 4546 | ret = btrfs_drop_extents(trans, root, inode, offset, offset + len, 1); |
| 4547 | if (ret) { |
| 4548 | btrfs_abort_transaction(trans, root, ret); |
| 4549 | btrfs_end_transaction(trans, root); |
| 4550 | return ret; |
| 4551 | } |
| 4552 | |
| 4553 | ret = btrfs_insert_file_extent(trans, root, btrfs_ino(inode), offset, |
| 4554 | 0, 0, len, 0, len, 0, 0, 0); |
| 4555 | if (ret) |
| 4556 | btrfs_abort_transaction(trans, root, ret); |
| 4557 | else |
| 4558 | btrfs_update_inode(trans, root, inode); |
| 4559 | btrfs_end_transaction(trans, root); |
| 4560 | return ret; |
| 4561 | } |
| 4562 | |
Josef Bacik | 695a0d0 | 2011-03-04 15:46:53 -0500 | [diff] [blame] | 4563 | /* |
| 4564 | * This function puts in dummy file extents for the area we're creating a hole |
| 4565 | * for. So if we are truncating this file to a larger size we need to insert |
| 4566 | * these file extents so that btrfs_get_extent will return a EXTENT_MAP_HOLE for |
| 4567 | * the range between oldsize and size |
| 4568 | */ |
Josef Bacik | a41ad39 | 2011-01-31 15:30:16 -0500 | [diff] [blame] | 4569 | 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] | 4570 | { |
Yan Zheng | 9036c10 | 2008-10-30 14:19:41 -0400 | [diff] [blame] | 4571 | struct btrfs_root *root = BTRFS_I(inode)->root; |
| 4572 | struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree; |
Yan, Zheng | a22285a | 2010-05-16 10:48:46 -0400 | [diff] [blame] | 4573 | struct extent_map *em = NULL; |
Josef Bacik | 2ac55d4 | 2010-02-03 19:33:23 +0000 | [diff] [blame] | 4574 | struct extent_state *cached_state = NULL; |
Josef Bacik | 5dc562c | 2012-08-17 13:14:17 -0400 | [diff] [blame] | 4575 | struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree; |
Qu Wenruo | fda2832 | 2013-02-26 08:10:22 +0000 | [diff] [blame] | 4576 | u64 hole_start = ALIGN(oldsize, root->sectorsize); |
| 4577 | u64 block_end = ALIGN(size, root->sectorsize); |
Yan Zheng | 9036c10 | 2008-10-30 14:19:41 -0400 | [diff] [blame] | 4578 | u64 last_byte; |
| 4579 | u64 cur_offset; |
| 4580 | u64 hole_size; |
Josef Bacik | 9ed74f2 | 2009-09-11 16:12:44 -0400 | [diff] [blame] | 4581 | int err = 0; |
Yan Zheng | 9036c10 | 2008-10-30 14:19:41 -0400 | [diff] [blame] | 4582 | |
Josef Bacik | a71754f | 2013-06-17 17:14:39 -0400 | [diff] [blame] | 4583 | /* |
| 4584 | * If our size started in the middle of a page we need to zero out the |
| 4585 | * rest of the page before we expand the i_size, otherwise we could |
| 4586 | * expose stale data. |
| 4587 | */ |
| 4588 | err = btrfs_truncate_page(inode, oldsize, 0, 0); |
| 4589 | if (err) |
| 4590 | return err; |
| 4591 | |
Yan Zheng | 9036c10 | 2008-10-30 14:19:41 -0400 | [diff] [blame] | 4592 | if (size <= hole_start) |
| 4593 | return 0; |
| 4594 | |
Yan Zheng | 9036c10 | 2008-10-30 14:19:41 -0400 | [diff] [blame] | 4595 | while (1) { |
| 4596 | struct btrfs_ordered_extent *ordered; |
Miao Xie | fa7c149 | 2013-09-26 13:15:27 +0800 | [diff] [blame] | 4597 | |
Josef Bacik | 2ac55d4 | 2010-02-03 19:33:23 +0000 | [diff] [blame] | 4598 | lock_extent_bits(io_tree, hole_start, block_end - 1, 0, |
Jeff Mahoney | d008237 | 2012-03-01 14:57:19 +0100 | [diff] [blame] | 4599 | &cached_state); |
Miao Xie | fa7c149 | 2013-09-26 13:15:27 +0800 | [diff] [blame] | 4600 | ordered = btrfs_lookup_ordered_range(inode, hole_start, |
| 4601 | block_end - hole_start); |
Yan Zheng | 9036c10 | 2008-10-30 14:19:41 -0400 | [diff] [blame] | 4602 | if (!ordered) |
| 4603 | break; |
Josef Bacik | 2ac55d4 | 2010-02-03 19:33:23 +0000 | [diff] [blame] | 4604 | unlock_extent_cached(io_tree, hole_start, block_end - 1, |
| 4605 | &cached_state, GFP_NOFS); |
Miao Xie | fa7c149 | 2013-09-26 13:15:27 +0800 | [diff] [blame] | 4606 | btrfs_start_ordered_extent(inode, ordered, 1); |
Yan Zheng | 9036c10 | 2008-10-30 14:19:41 -0400 | [diff] [blame] | 4607 | btrfs_put_ordered_extent(ordered); |
| 4608 | } |
| 4609 | |
Yan Zheng | 9036c10 | 2008-10-30 14:19:41 -0400 | [diff] [blame] | 4610 | cur_offset = hole_start; |
| 4611 | while (1) { |
| 4612 | em = btrfs_get_extent(inode, NULL, 0, cur_offset, |
| 4613 | block_end - cur_offset, 0); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 4614 | if (IS_ERR(em)) { |
| 4615 | err = PTR_ERR(em); |
Zach Brown | f276795 | 2013-01-08 19:37:58 +0000 | [diff] [blame] | 4616 | em = NULL; |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 4617 | break; |
| 4618 | } |
Yan Zheng | 9036c10 | 2008-10-30 14:19:41 -0400 | [diff] [blame] | 4619 | last_byte = min(extent_map_end(em), block_end); |
Qu Wenruo | fda2832 | 2013-02-26 08:10:22 +0000 | [diff] [blame] | 4620 | last_byte = ALIGN(last_byte , root->sectorsize); |
Yan, Zheng | 8082510 | 2009-11-12 09:35:36 +0000 | [diff] [blame] | 4621 | if (!test_bit(EXTENT_FLAG_PREALLOC, &em->flags)) { |
Josef Bacik | 5dc562c | 2012-08-17 13:14:17 -0400 | [diff] [blame] | 4622 | struct extent_map *hole_em; |
Yan Zheng | 9036c10 | 2008-10-30 14:19:41 -0400 | [diff] [blame] | 4623 | hole_size = last_byte - cur_offset; |
Yan, Zheng | 8082510 | 2009-11-12 09:35:36 +0000 | [diff] [blame] | 4624 | |
Josef Bacik | 16e7549 | 2013-10-22 12:18:51 -0400 | [diff] [blame] | 4625 | err = maybe_insert_hole(root, inode, cur_offset, |
| 4626 | hole_size); |
| 4627 | if (err) |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 4628 | break; |
Josef Bacik | 5dc562c | 2012-08-17 13:14:17 -0400 | [diff] [blame] | 4629 | btrfs_drop_extent_cache(inode, cur_offset, |
| 4630 | cur_offset + hole_size - 1, 0); |
| 4631 | hole_em = alloc_extent_map(); |
| 4632 | if (!hole_em) { |
| 4633 | set_bit(BTRFS_INODE_NEEDS_FULL_SYNC, |
| 4634 | &BTRFS_I(inode)->runtime_flags); |
| 4635 | goto next; |
| 4636 | } |
| 4637 | hole_em->start = cur_offset; |
| 4638 | hole_em->len = hole_size; |
| 4639 | hole_em->orig_start = cur_offset; |
Yan, Zheng | 8082510 | 2009-11-12 09:35:36 +0000 | [diff] [blame] | 4640 | |
Josef Bacik | 5dc562c | 2012-08-17 13:14:17 -0400 | [diff] [blame] | 4641 | hole_em->block_start = EXTENT_MAP_HOLE; |
| 4642 | hole_em->block_len = 0; |
Josef Bacik | b493968 | 2012-12-03 10:31:19 -0500 | [diff] [blame] | 4643 | hole_em->orig_block_len = 0; |
Josef Bacik | cc95bef | 2013-04-04 14:31:27 -0400 | [diff] [blame] | 4644 | hole_em->ram_bytes = hole_size; |
Josef Bacik | 5dc562c | 2012-08-17 13:14:17 -0400 | [diff] [blame] | 4645 | hole_em->bdev = root->fs_info->fs_devices->latest_bdev; |
| 4646 | hole_em->compress_type = BTRFS_COMPRESS_NONE; |
Josef Bacik | 16e7549 | 2013-10-22 12:18:51 -0400 | [diff] [blame] | 4647 | hole_em->generation = root->fs_info->generation; |
Josef Bacik | 5dc562c | 2012-08-17 13:14:17 -0400 | [diff] [blame] | 4648 | |
| 4649 | while (1) { |
| 4650 | write_lock(&em_tree->lock); |
Josef Bacik | 09a2a8f9 | 2013-04-05 16:51:15 -0400 | [diff] [blame] | 4651 | err = add_extent_mapping(em_tree, hole_em, 1); |
Josef Bacik | 5dc562c | 2012-08-17 13:14:17 -0400 | [diff] [blame] | 4652 | write_unlock(&em_tree->lock); |
| 4653 | if (err != -EEXIST) |
| 4654 | break; |
| 4655 | btrfs_drop_extent_cache(inode, cur_offset, |
| 4656 | cur_offset + |
| 4657 | hole_size - 1, 0); |
| 4658 | } |
| 4659 | free_extent_map(hole_em); |
Yan Zheng | 9036c10 | 2008-10-30 14:19:41 -0400 | [diff] [blame] | 4660 | } |
Josef Bacik | 16e7549 | 2013-10-22 12:18:51 -0400 | [diff] [blame] | 4661 | next: |
Yan Zheng | 9036c10 | 2008-10-30 14:19:41 -0400 | [diff] [blame] | 4662 | free_extent_map(em); |
Yan, Zheng | a22285a | 2010-05-16 10:48:46 -0400 | [diff] [blame] | 4663 | em = NULL; |
Yan Zheng | 9036c10 | 2008-10-30 14:19:41 -0400 | [diff] [blame] | 4664 | cur_offset = last_byte; |
Yan, Zheng | 8082510 | 2009-11-12 09:35:36 +0000 | [diff] [blame] | 4665 | if (cur_offset >= block_end) |
Yan Zheng | 9036c10 | 2008-10-30 14:19:41 -0400 | [diff] [blame] | 4666 | break; |
| 4667 | } |
Yan, Zheng | a22285a | 2010-05-16 10:48:46 -0400 | [diff] [blame] | 4668 | free_extent_map(em); |
Josef Bacik | 2ac55d4 | 2010-02-03 19:33:23 +0000 | [diff] [blame] | 4669 | unlock_extent_cached(io_tree, hole_start, block_end - 1, &cached_state, |
| 4670 | GFP_NOFS); |
Yan Zheng | 9036c10 | 2008-10-30 14:19:41 -0400 | [diff] [blame] | 4671 | return err; |
| 4672 | } |
| 4673 | |
Filipe Manana | 9ea24bb | 2014-10-29 11:57:59 +0000 | [diff] [blame] | 4674 | static int wait_snapshoting_atomic_t(atomic_t *a) |
| 4675 | { |
| 4676 | schedule(); |
| 4677 | return 0; |
| 4678 | } |
| 4679 | |
| 4680 | static void wait_for_snapshot_creation(struct btrfs_root *root) |
| 4681 | { |
| 4682 | while (true) { |
| 4683 | int ret; |
| 4684 | |
| 4685 | ret = btrfs_start_write_no_snapshoting(root); |
| 4686 | if (ret) |
| 4687 | break; |
| 4688 | wait_on_atomic_t(&root->will_be_snapshoted, |
| 4689 | wait_snapshoting_atomic_t, |
| 4690 | TASK_UNINTERRUPTIBLE); |
| 4691 | } |
| 4692 | } |
| 4693 | |
Eric Sandeen | 3972f26 | 2013-01-12 02:57:22 +0000 | [diff] [blame] | 4694 | static int btrfs_setsize(struct inode *inode, struct iattr *attr) |
Yan, Zheng | 8082510 | 2009-11-12 09:35:36 +0000 | [diff] [blame] | 4695 | { |
Miao Xie | f4a2f4c | 2011-12-14 20:12:01 -0500 | [diff] [blame] | 4696 | struct btrfs_root *root = BTRFS_I(inode)->root; |
| 4697 | struct btrfs_trans_handle *trans; |
Josef Bacik | a41ad39 | 2011-01-31 15:30:16 -0500 | [diff] [blame] | 4698 | loff_t oldsize = i_size_read(inode); |
Eric Sandeen | 3972f26 | 2013-01-12 02:57:22 +0000 | [diff] [blame] | 4699 | loff_t newsize = attr->ia_size; |
| 4700 | int mask = attr->ia_valid; |
Yan, Zheng | 8082510 | 2009-11-12 09:35:36 +0000 | [diff] [blame] | 4701 | int ret; |
| 4702 | |
Eric Sandeen | 3972f26 | 2013-01-12 02:57:22 +0000 | [diff] [blame] | 4703 | /* |
| 4704 | * The regular truncate() case without ATTR_CTIME and ATTR_MTIME is a |
| 4705 | * special case where we need to update the times despite not having |
| 4706 | * these flags set. For all other operations the VFS set these flags |
| 4707 | * explicitly if it wants a timestamp update. |
| 4708 | */ |
Christoph Hellwig | dff6efc | 2013-11-19 07:17:07 -0800 | [diff] [blame] | 4709 | if (newsize != oldsize) { |
| 4710 | inode_inc_iversion(inode); |
| 4711 | if (!(mask & (ATTR_CTIME | ATTR_MTIME))) |
| 4712 | inode->i_ctime = inode->i_mtime = |
| 4713 | current_fs_time(inode->i_sb); |
| 4714 | } |
Eric Sandeen | 3972f26 | 2013-01-12 02:57:22 +0000 | [diff] [blame] | 4715 | |
Josef Bacik | a41ad39 | 2011-01-31 15:30:16 -0500 | [diff] [blame] | 4716 | if (newsize > oldsize) { |
Kirill A. Shutemov | 7caef26 | 2013-09-12 15:13:56 -0700 | [diff] [blame] | 4717 | truncate_pagecache(inode, newsize); |
Filipe Manana | 9ea24bb | 2014-10-29 11:57:59 +0000 | [diff] [blame] | 4718 | /* |
| 4719 | * Don't do an expanding truncate while snapshoting is ongoing. |
| 4720 | * This is to ensure the snapshot captures a fully consistent |
| 4721 | * state of this file - if the snapshot captures this expanding |
| 4722 | * truncation, it must capture all writes that happened before |
| 4723 | * this truncation. |
| 4724 | */ |
| 4725 | wait_for_snapshot_creation(root); |
Josef Bacik | a41ad39 | 2011-01-31 15:30:16 -0500 | [diff] [blame] | 4726 | ret = btrfs_cont_expand(inode, oldsize, newsize); |
Filipe Manana | 9ea24bb | 2014-10-29 11:57:59 +0000 | [diff] [blame] | 4727 | if (ret) { |
| 4728 | btrfs_end_write_no_snapshoting(root); |
Yan, Zheng | 8082510 | 2009-11-12 09:35:36 +0000 | [diff] [blame] | 4729 | return ret; |
Filipe Manana | 9ea24bb | 2014-10-29 11:57:59 +0000 | [diff] [blame] | 4730 | } |
Yan, Zheng | 8082510 | 2009-11-12 09:35:36 +0000 | [diff] [blame] | 4731 | |
Miao Xie | f4a2f4c | 2011-12-14 20:12:01 -0500 | [diff] [blame] | 4732 | trans = btrfs_start_transaction(root, 1); |
Filipe Manana | 9ea24bb | 2014-10-29 11:57:59 +0000 | [diff] [blame] | 4733 | if (IS_ERR(trans)) { |
| 4734 | btrfs_end_write_no_snapshoting(root); |
Miao Xie | f4a2f4c | 2011-12-14 20:12:01 -0500 | [diff] [blame] | 4735 | return PTR_ERR(trans); |
Filipe Manana | 9ea24bb | 2014-10-29 11:57:59 +0000 | [diff] [blame] | 4736 | } |
Miao Xie | f4a2f4c | 2011-12-14 20:12:01 -0500 | [diff] [blame] | 4737 | |
| 4738 | i_size_write(inode, newsize); |
| 4739 | btrfs_ordered_update_i_size(inode, i_size_read(inode), NULL); |
| 4740 | ret = btrfs_update_inode(trans, root, inode); |
Filipe Manana | 9ea24bb | 2014-10-29 11:57:59 +0000 | [diff] [blame] | 4741 | btrfs_end_write_no_snapshoting(root); |
Josef Bacik | 7ad85bb | 2012-01-12 19:10:12 -0500 | [diff] [blame] | 4742 | btrfs_end_transaction(trans, root); |
Josef Bacik | a41ad39 | 2011-01-31 15:30:16 -0500 | [diff] [blame] | 4743 | } else { |
Yan, Zheng | 8082510 | 2009-11-12 09:35:36 +0000 | [diff] [blame] | 4744 | |
Josef Bacik | a41ad39 | 2011-01-31 15:30:16 -0500 | [diff] [blame] | 4745 | /* |
| 4746 | * We're truncating a file that used to have good data down to |
| 4747 | * zero. Make sure it gets into the ordered flush list so that |
| 4748 | * any new writes get down to disk quickly. |
| 4749 | */ |
| 4750 | if (newsize == 0) |
Josef Bacik | 72ac3c0 | 2012-05-23 14:13:11 -0400 | [diff] [blame] | 4751 | set_bit(BTRFS_INODE_ORDERED_DATA_CLOSE, |
| 4752 | &BTRFS_I(inode)->runtime_flags); |
Yan, Zheng | 8082510 | 2009-11-12 09:35:36 +0000 | [diff] [blame] | 4753 | |
Josef Bacik | f3fe820 | 2013-01-07 17:03:21 -0500 | [diff] [blame] | 4754 | /* |
| 4755 | * 1 for the orphan item we're going to add |
| 4756 | * 1 for the orphan item deletion. |
| 4757 | */ |
| 4758 | trans = btrfs_start_transaction(root, 2); |
| 4759 | if (IS_ERR(trans)) |
| 4760 | return PTR_ERR(trans); |
| 4761 | |
| 4762 | /* |
| 4763 | * We need to do this in case we fail at _any_ point during the |
| 4764 | * actual truncate. Once we do the truncate_setsize we could |
| 4765 | * invalidate pages which forces any outstanding ordered io to |
| 4766 | * be instantly completed which will give us extents that need |
| 4767 | * to be truncated. If we fail to get an orphan inode down we |
| 4768 | * could have left over extents that were never meant to live, |
| 4769 | * so we need to garuntee from this point on that everything |
| 4770 | * will be consistent. |
| 4771 | */ |
| 4772 | ret = btrfs_orphan_add(trans, inode); |
| 4773 | btrfs_end_transaction(trans, root); |
| 4774 | if (ret) |
| 4775 | return ret; |
| 4776 | |
Josef Bacik | a41ad39 | 2011-01-31 15:30:16 -0500 | [diff] [blame] | 4777 | /* we don't support swapfiles, so vmtruncate shouldn't fail */ |
| 4778 | truncate_setsize(inode, newsize); |
Miao Xie | 2e60a51 | 2013-02-08 07:01:08 +0000 | [diff] [blame] | 4779 | |
| 4780 | /* Disable nonlocked read DIO to avoid the end less truncate */ |
| 4781 | btrfs_inode_block_unlocked_dio(inode); |
| 4782 | inode_dio_wait(inode); |
| 4783 | btrfs_inode_resume_unlocked_dio(inode); |
| 4784 | |
Josef Bacik | a41ad39 | 2011-01-31 15:30:16 -0500 | [diff] [blame] | 4785 | ret = btrfs_truncate(inode); |
Josef Bacik | 7f4f6e0 | 2013-08-29 16:43:28 -0400 | [diff] [blame] | 4786 | if (ret && inode->i_nlink) { |
| 4787 | int err; |
| 4788 | |
| 4789 | /* |
| 4790 | * failed to truncate, disk_i_size is only adjusted down |
| 4791 | * as we remove extents, so it should represent the true |
| 4792 | * size of the inode, so reset the in memory size and |
| 4793 | * delete our orphan entry. |
| 4794 | */ |
| 4795 | trans = btrfs_join_transaction(root); |
| 4796 | if (IS_ERR(trans)) { |
| 4797 | btrfs_orphan_del(NULL, inode); |
| 4798 | return ret; |
| 4799 | } |
| 4800 | i_size_write(inode, BTRFS_I(inode)->disk_i_size); |
| 4801 | err = btrfs_orphan_del(trans, inode); |
| 4802 | if (err) |
| 4803 | btrfs_abort_transaction(trans, root, err); |
| 4804 | btrfs_end_transaction(trans, root); |
| 4805 | } |
Yan, Zheng | 8082510 | 2009-11-12 09:35:36 +0000 | [diff] [blame] | 4806 | } |
| 4807 | |
Josef Bacik | a41ad39 | 2011-01-31 15:30:16 -0500 | [diff] [blame] | 4808 | return ret; |
Yan, Zheng | 8082510 | 2009-11-12 09:35:36 +0000 | [diff] [blame] | 4809 | } |
| 4810 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4811 | static int btrfs_setattr(struct dentry *dentry, struct iattr *attr) |
| 4812 | { |
| 4813 | struct inode *inode = dentry->d_inode; |
Li Zefan | b83cc96 | 2010-12-20 16:04:08 +0800 | [diff] [blame] | 4814 | struct btrfs_root *root = BTRFS_I(inode)->root; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4815 | int err; |
| 4816 | |
Li Zefan | b83cc96 | 2010-12-20 16:04:08 +0800 | [diff] [blame] | 4817 | if (btrfs_root_readonly(root)) |
| 4818 | return -EROFS; |
| 4819 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4820 | err = inode_change_ok(inode, attr); |
| 4821 | if (err) |
| 4822 | return err; |
| 4823 | |
Chris Mason | 5a3f23d | 2009-03-31 13:27:11 -0400 | [diff] [blame] | 4824 | if (S_ISREG(inode->i_mode) && (attr->ia_valid & ATTR_SIZE)) { |
Eric Sandeen | 3972f26 | 2013-01-12 02:57:22 +0000 | [diff] [blame] | 4825 | err = btrfs_setsize(inode, attr); |
Yan, Zheng | 8082510 | 2009-11-12 09:35:36 +0000 | [diff] [blame] | 4826 | if (err) |
| 4827 | return err; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4828 | } |
Yan Zheng | 9036c10 | 2008-10-30 14:19:41 -0400 | [diff] [blame] | 4829 | |
Christoph Hellwig | 1025774 | 2010-06-04 11:30:02 +0200 | [diff] [blame] | 4830 | if (attr->ia_valid) { |
| 4831 | setattr_copy(inode, attr); |
Josef Bacik | 0c4d2d9 | 2012-04-05 15:03:02 -0400 | [diff] [blame] | 4832 | inode_inc_iversion(inode); |
Josef Bacik | 22c44fe | 2011-11-30 10:45:38 -0500 | [diff] [blame] | 4833 | err = btrfs_dirty_inode(inode); |
Josef Bacik | 33268ea | 2008-07-24 12:16:36 -0400 | [diff] [blame] | 4834 | |
Josef Bacik | 22c44fe | 2011-11-30 10:45:38 -0500 | [diff] [blame] | 4835 | if (!err && attr->ia_valid & ATTR_MODE) |
Christoph Hellwig | 996a710 | 2013-12-20 05:16:43 -0800 | [diff] [blame] | 4836 | err = posix_acl_chmod(inode, inode->i_mode); |
Christoph Hellwig | 1025774 | 2010-06-04 11:30:02 +0200 | [diff] [blame] | 4837 | } |
| 4838 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4839 | return err; |
| 4840 | } |
Chris Mason | 61295eb | 2008-01-14 16:24:38 -0500 | [diff] [blame] | 4841 | |
Filipe David Borba Manana | 131e404 | 2013-11-19 22:29:35 +0000 | [diff] [blame] | 4842 | /* |
| 4843 | * While truncating the inode pages during eviction, we get the VFS calling |
| 4844 | * btrfs_invalidatepage() against each page of the inode. This is slow because |
| 4845 | * the calls to btrfs_invalidatepage() result in a huge amount of calls to |
| 4846 | * lock_extent_bits() and clear_extent_bit(), which keep merging and splitting |
| 4847 | * extent_state structures over and over, wasting lots of time. |
| 4848 | * |
| 4849 | * Therefore if the inode is being evicted, let btrfs_invalidatepage() skip all |
| 4850 | * those expensive operations on a per page basis and do only the ordered io |
| 4851 | * finishing, while we release here the extent_map and extent_state structures, |
| 4852 | * without the excessive merging and splitting. |
| 4853 | */ |
| 4854 | static void evict_inode_truncate_pages(struct inode *inode) |
| 4855 | { |
| 4856 | struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree; |
| 4857 | struct extent_map_tree *map_tree = &BTRFS_I(inode)->extent_tree; |
| 4858 | struct rb_node *node; |
| 4859 | |
| 4860 | ASSERT(inode->i_state & I_FREEING); |
Johannes Weiner | 91b0abe | 2014-04-03 14:47:49 -0700 | [diff] [blame] | 4861 | truncate_inode_pages_final(&inode->i_data); |
Filipe David Borba Manana | 131e404 | 2013-11-19 22:29:35 +0000 | [diff] [blame] | 4862 | |
| 4863 | write_lock(&map_tree->lock); |
| 4864 | while (!RB_EMPTY_ROOT(&map_tree->map)) { |
| 4865 | struct extent_map *em; |
| 4866 | |
| 4867 | node = rb_first(&map_tree->map); |
| 4868 | em = rb_entry(node, struct extent_map, rb_node); |
Wang Shilong | 180589e | 2013-12-14 15:27:31 +0800 | [diff] [blame] | 4869 | clear_bit(EXTENT_FLAG_PINNED, &em->flags); |
| 4870 | clear_bit(EXTENT_FLAG_LOGGING, &em->flags); |
Filipe David Borba Manana | 131e404 | 2013-11-19 22:29:35 +0000 | [diff] [blame] | 4871 | remove_extent_mapping(map_tree, em); |
| 4872 | free_extent_map(em); |
Filipe Manana | 7064dd5 | 2014-08-08 02:47:05 +0100 | [diff] [blame] | 4873 | if (need_resched()) { |
| 4874 | write_unlock(&map_tree->lock); |
| 4875 | cond_resched(); |
| 4876 | write_lock(&map_tree->lock); |
| 4877 | } |
Filipe David Borba Manana | 131e404 | 2013-11-19 22:29:35 +0000 | [diff] [blame] | 4878 | } |
| 4879 | write_unlock(&map_tree->lock); |
| 4880 | |
| 4881 | spin_lock(&io_tree->lock); |
| 4882 | while (!RB_EMPTY_ROOT(&io_tree->state)) { |
| 4883 | struct extent_state *state; |
| 4884 | struct extent_state *cached_state = NULL; |
| 4885 | |
| 4886 | node = rb_first(&io_tree->state); |
| 4887 | state = rb_entry(node, struct extent_state, rb_node); |
| 4888 | atomic_inc(&state->refs); |
| 4889 | spin_unlock(&io_tree->lock); |
| 4890 | |
| 4891 | lock_extent_bits(io_tree, state->start, state->end, |
| 4892 | 0, &cached_state); |
| 4893 | clear_extent_bit(io_tree, state->start, state->end, |
| 4894 | EXTENT_LOCKED | EXTENT_DIRTY | |
| 4895 | EXTENT_DELALLOC | EXTENT_DO_ACCOUNTING | |
| 4896 | EXTENT_DEFRAG, 1, 1, |
| 4897 | &cached_state, GFP_NOFS); |
| 4898 | free_extent_state(state); |
| 4899 | |
Filipe Manana | 7064dd5 | 2014-08-08 02:47:05 +0100 | [diff] [blame] | 4900 | cond_resched(); |
Filipe David Borba Manana | 131e404 | 2013-11-19 22:29:35 +0000 | [diff] [blame] | 4901 | spin_lock(&io_tree->lock); |
| 4902 | } |
| 4903 | spin_unlock(&io_tree->lock); |
| 4904 | } |
| 4905 | |
Al Viro | bd55597 | 2010-06-07 11:35:40 -0400 | [diff] [blame] | 4906 | void btrfs_evict_inode(struct inode *inode) |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4907 | { |
| 4908 | struct btrfs_trans_handle *trans; |
| 4909 | struct btrfs_root *root = BTRFS_I(inode)->root; |
Josef Bacik | 726c35f | 2011-09-26 15:46:06 -0400 | [diff] [blame] | 4910 | struct btrfs_block_rsv *rsv, *global_rsv; |
Josef Bacik | 0712718 | 2011-08-19 10:29:59 -0400 | [diff] [blame] | 4911 | u64 min_size = btrfs_calc_trunc_metadata_size(root, 1); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4912 | int ret; |
| 4913 | |
liubo | 1abe9b8 | 2011-03-24 11:18:59 +0000 | [diff] [blame] | 4914 | trace_btrfs_inode_evict(inode); |
| 4915 | |
Filipe David Borba Manana | 131e404 | 2013-11-19 22:29:35 +0000 | [diff] [blame] | 4916 | evict_inode_truncate_pages(inode); |
| 4917 | |
Stefan Behrens | 69e9c6c | 2013-09-05 16:58:43 +0200 | [diff] [blame] | 4918 | if (inode->i_nlink && |
| 4919 | ((btrfs_root_refs(&root->root_item) != 0 && |
| 4920 | root->root_key.objectid != BTRFS_ROOT_TREE_OBJECTID) || |
| 4921 | btrfs_is_free_space_inode(inode))) |
Al Viro | bd55597 | 2010-06-07 11:35:40 -0400 | [diff] [blame] | 4922 | goto no_delete; |
| 4923 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4924 | if (is_bad_inode(inode)) { |
Josef Bacik | 7b12876 | 2008-07-24 12:17:14 -0400 | [diff] [blame] | 4925 | btrfs_orphan_del(NULL, inode); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4926 | goto no_delete; |
| 4927 | } |
Al Viro | bd55597 | 2010-06-07 11:35:40 -0400 | [diff] [blame] | 4928 | /* 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] | 4929 | btrfs_wait_ordered_range(inode, 0, (u64)-1); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 4930 | |
Miao Xie | f612496 | 2014-09-12 18:44:04 +0800 | [diff] [blame] | 4931 | btrfs_free_io_failure_record(inode, 0, (u64)-1); |
| 4932 | |
Yan, Zheng | c71bf09 | 2009-11-12 09:34:40 +0000 | [diff] [blame] | 4933 | if (root->fs_info->log_root_recovering) { |
Liu Bo | 6bf0231 | 2012-06-25 21:59:09 -0600 | [diff] [blame] | 4934 | BUG_ON(test_bit(BTRFS_INODE_HAS_ORPHAN_ITEM, |
Josef Bacik | 8a35d95 | 2012-05-23 14:26:42 -0400 | [diff] [blame] | 4935 | &BTRFS_I(inode)->runtime_flags)); |
Yan, Zheng | c71bf09 | 2009-11-12 09:34:40 +0000 | [diff] [blame] | 4936 | goto no_delete; |
| 4937 | } |
| 4938 | |
Yan, Zheng | 76dda93 | 2009-09-21 16:00:26 -0400 | [diff] [blame] | 4939 | if (inode->i_nlink > 0) { |
Stefan Behrens | 69e9c6c | 2013-09-05 16:58:43 +0200 | [diff] [blame] | 4940 | BUG_ON(btrfs_root_refs(&root->root_item) != 0 && |
| 4941 | root->root_key.objectid != BTRFS_ROOT_TREE_OBJECTID); |
Yan, Zheng | 76dda93 | 2009-09-21 16:00:26 -0400 | [diff] [blame] | 4942 | goto no_delete; |
| 4943 | } |
| 4944 | |
Miao Xie | 0e8c36a | 2012-12-19 06:59:51 +0000 | [diff] [blame] | 4945 | ret = btrfs_commit_inode_delayed_inode(inode); |
| 4946 | if (ret) { |
| 4947 | btrfs_orphan_del(NULL, inode); |
| 4948 | goto no_delete; |
| 4949 | } |
| 4950 | |
Miao Xie | 66d8f3d | 2012-09-06 04:02:28 -0600 | [diff] [blame] | 4951 | rsv = btrfs_alloc_block_rsv(root, BTRFS_BLOCK_RSV_TEMP); |
Josef Bacik | 4289a66 | 2011-08-05 13:22:24 -0400 | [diff] [blame] | 4952 | if (!rsv) { |
| 4953 | btrfs_orphan_del(NULL, inode); |
| 4954 | goto no_delete; |
| 4955 | } |
Josef Bacik | 4a33854 | 2011-08-29 11:01:31 -0400 | [diff] [blame] | 4956 | rsv->size = min_size; |
Josef Bacik | ca7e70f | 2012-08-27 17:48:15 -0400 | [diff] [blame] | 4957 | rsv->failfast = 1; |
Josef Bacik | 726c35f | 2011-09-26 15:46:06 -0400 | [diff] [blame] | 4958 | global_rsv = &root->fs_info->global_block_rsv; |
Josef Bacik | 4289a66 | 2011-08-05 13:22:24 -0400 | [diff] [blame] | 4959 | |
Chris Mason | dbe674a | 2008-07-17 12:54:05 -0400 | [diff] [blame] | 4960 | btrfs_i_size_write(inode, 0); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 4961 | |
Josef Bacik | 4289a66 | 2011-08-05 13:22:24 -0400 | [diff] [blame] | 4962 | /* |
Miao Xie | 8407aa4 | 2012-09-07 01:43:32 -0600 | [diff] [blame] | 4963 | * This is a bit simpler than btrfs_truncate since we've already |
| 4964 | * reserved our space for our orphan item in the unlink, so we just |
| 4965 | * need to reserve some slack space in case we add bytes and update |
| 4966 | * inode item when doing the truncate. |
Josef Bacik | 4289a66 | 2011-08-05 13:22:24 -0400 | [diff] [blame] | 4967 | */ |
Yan, Zheng | 8082510 | 2009-11-12 09:35:36 +0000 | [diff] [blame] | 4968 | while (1) { |
Miao Xie | 08e007d | 2012-10-16 11:33:38 +0000 | [diff] [blame] | 4969 | ret = btrfs_block_rsv_refill(root, rsv, min_size, |
| 4970 | BTRFS_RESERVE_FLUSH_LIMIT); |
Yan, Zheng | 8082510 | 2009-11-12 09:35:36 +0000 | [diff] [blame] | 4971 | |
Josef Bacik | 726c35f | 2011-09-26 15:46:06 -0400 | [diff] [blame] | 4972 | /* |
| 4973 | * Try and steal from the global reserve since we will |
| 4974 | * likely not use this space anyway, we want to try as |
| 4975 | * hard as possible to get this to work. |
| 4976 | */ |
| 4977 | if (ret) |
| 4978 | ret = btrfs_block_rsv_migrate(global_rsv, rsv, min_size); |
| 4979 | |
Yan, Zheng | d68fc57 | 2010-05-16 10:49:58 -0400 | [diff] [blame] | 4980 | if (ret) { |
Simon Kirby | c2cf52e | 2013-03-19 22:41:23 +0000 | [diff] [blame] | 4981 | btrfs_warn(root->fs_info, |
| 4982 | "Could not get space for a delete, will truncate on mount %d", |
| 4983 | ret); |
Josef Bacik | 4289a66 | 2011-08-05 13:22:24 -0400 | [diff] [blame] | 4984 | btrfs_orphan_del(NULL, inode); |
| 4985 | btrfs_free_block_rsv(root, rsv); |
| 4986 | goto no_delete; |
Yan, Zheng | d68fc57 | 2010-05-16 10:49:58 -0400 | [diff] [blame] | 4987 | } |
| 4988 | |
Miao Xie | 0e8c36a | 2012-12-19 06:59:51 +0000 | [diff] [blame] | 4989 | trans = btrfs_join_transaction(root); |
Josef Bacik | 4289a66 | 2011-08-05 13:22:24 -0400 | [diff] [blame] | 4990 | if (IS_ERR(trans)) { |
| 4991 | btrfs_orphan_del(NULL, inode); |
| 4992 | btrfs_free_block_rsv(root, rsv); |
| 4993 | goto no_delete; |
| 4994 | } |
| 4995 | |
| 4996 | trans->block_rsv = rsv; |
| 4997 | |
Yan, Zheng | d68fc57 | 2010-05-16 10:49:58 -0400 | [diff] [blame] | 4998 | ret = btrfs_truncate_inode_items(trans, root, inode, 0, 0); |
Josef Bacik | ca7e70f | 2012-08-27 17:48:15 -0400 | [diff] [blame] | 4999 | if (ret != -ENOSPC) |
Yan, Zheng | 8082510 | 2009-11-12 09:35:36 +0000 | [diff] [blame] | 5000 | break; |
| 5001 | |
Miao Xie | 8407aa4 | 2012-09-07 01:43:32 -0600 | [diff] [blame] | 5002 | trans->block_rsv = &root->fs_info->trans_block_rsv; |
Yan, Zheng | 8082510 | 2009-11-12 09:35:36 +0000 | [diff] [blame] | 5003 | btrfs_end_transaction(trans, root); |
| 5004 | trans = NULL; |
Liu Bo | b53d3f5 | 2012-11-14 14:34:34 +0000 | [diff] [blame] | 5005 | btrfs_btree_balance_dirty(root); |
Josef Bacik | 7b12876 | 2008-07-24 12:17:14 -0400 | [diff] [blame] | 5006 | } |
| 5007 | |
Josef Bacik | 4289a66 | 2011-08-05 13:22:24 -0400 | [diff] [blame] | 5008 | btrfs_free_block_rsv(root, rsv); |
| 5009 | |
Josef Bacik | 4ef31a4 | 2013-08-13 14:10:08 -0400 | [diff] [blame] | 5010 | /* |
| 5011 | * Errors here aren't a big deal, it just means we leave orphan items |
| 5012 | * in the tree. They will be cleaned up on the next mount. |
| 5013 | */ |
Yan, Zheng | 8082510 | 2009-11-12 09:35:36 +0000 | [diff] [blame] | 5014 | if (ret == 0) { |
Josef Bacik | 4289a66 | 2011-08-05 13:22:24 -0400 | [diff] [blame] | 5015 | trans->block_rsv = root->orphan_block_rsv; |
Josef Bacik | 4ef31a4 | 2013-08-13 14:10:08 -0400 | [diff] [blame] | 5016 | btrfs_orphan_del(trans, inode); |
| 5017 | } else { |
| 5018 | btrfs_orphan_del(NULL, inode); |
Yan, Zheng | 8082510 | 2009-11-12 09:35:36 +0000 | [diff] [blame] | 5019 | } |
Chris Mason | 85e21ba | 2008-01-29 15:11:36 -0500 | [diff] [blame] | 5020 | |
Josef Bacik | 4289a66 | 2011-08-05 13:22:24 -0400 | [diff] [blame] | 5021 | trans->block_rsv = &root->fs_info->trans_block_rsv; |
Li Zefan | 581bb05 | 2011-04-20 10:06:11 +0800 | [diff] [blame] | 5022 | if (!(root == root->fs_info->tree_root || |
| 5023 | root->root_key.objectid == BTRFS_TREE_RELOC_OBJECTID)) |
Li Zefan | 33345d01 | 2011-04-20 10:31:50 +0800 | [diff] [blame] | 5024 | btrfs_return_ino(root, btrfs_ino(inode)); |
Li Zefan | 581bb05 | 2011-04-20 10:06:11 +0800 | [diff] [blame] | 5025 | |
Chris Mason | 54aa1f4 | 2007-06-22 14:16:25 -0400 | [diff] [blame] | 5026 | btrfs_end_transaction(trans, root); |
Liu Bo | b53d3f5 | 2012-11-14 14:34:34 +0000 | [diff] [blame] | 5027 | btrfs_btree_balance_dirty(root); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5028 | no_delete: |
Miao Xie | 89042e5 | 2013-05-15 07:48:15 +0000 | [diff] [blame] | 5029 | btrfs_remove_delayed_node(inode); |
Jan Kara | dbd5768 | 2012-05-03 14:48:02 +0200 | [diff] [blame] | 5030 | clear_inode(inode); |
Yan, Zheng | 8082510 | 2009-11-12 09:35:36 +0000 | [diff] [blame] | 5031 | return; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5032 | } |
| 5033 | |
| 5034 | /* |
| 5035 | * this returns the key found in the dir entry in the location pointer. |
| 5036 | * If no dir entries were found, location->objectid is 0. |
| 5037 | */ |
| 5038 | static int btrfs_inode_by_name(struct inode *dir, struct dentry *dentry, |
| 5039 | struct btrfs_key *location) |
| 5040 | { |
| 5041 | const char *name = dentry->d_name.name; |
| 5042 | int namelen = dentry->d_name.len; |
| 5043 | struct btrfs_dir_item *di; |
| 5044 | struct btrfs_path *path; |
| 5045 | struct btrfs_root *root = BTRFS_I(dir)->root; |
Yan | 0d9f7f3 | 2007-10-25 15:48:28 -0400 | [diff] [blame] | 5046 | int ret = 0; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5047 | |
| 5048 | path = btrfs_alloc_path(); |
Mark Fasheh | d8926bb | 2011-07-13 10:38:47 -0700 | [diff] [blame] | 5049 | if (!path) |
| 5050 | return -ENOMEM; |
Chris Mason | 3954401 | 2007-12-12 14:38:19 -0500 | [diff] [blame] | 5051 | |
Li Zefan | 33345d01 | 2011-04-20 10:31:50 +0800 | [diff] [blame] | 5052 | di = btrfs_lookup_dir_item(NULL, root, path, btrfs_ino(dir), name, |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5053 | namelen, 0); |
Yan | 0d9f7f3 | 2007-10-25 15:48:28 -0400 | [diff] [blame] | 5054 | if (IS_ERR(di)) |
| 5055 | ret = PTR_ERR(di); |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 5056 | |
David Sterba | c704005 | 2011-04-19 18:00:01 +0200 | [diff] [blame] | 5057 | if (IS_ERR_OR_NULL(di)) |
Chris Mason | 3954401 | 2007-12-12 14:38:19 -0500 | [diff] [blame] | 5058 | goto out_err; |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 5059 | |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 5060 | btrfs_dir_item_key_to_cpu(path->nodes[0], di, location); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5061 | out: |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5062 | btrfs_free_path(path); |
| 5063 | return ret; |
Chris Mason | 3954401 | 2007-12-12 14:38:19 -0500 | [diff] [blame] | 5064 | out_err: |
| 5065 | location->objectid = 0; |
| 5066 | goto out; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5067 | } |
| 5068 | |
| 5069 | /* |
| 5070 | * when we hit a tree root in a directory, the btrfs part of the inode |
| 5071 | * needs to be changed to reflect the root directory of the tree root. This |
| 5072 | * is kind of like crossing a mount point. |
| 5073 | */ |
| 5074 | static int fixup_tree_root_location(struct btrfs_root *root, |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 5075 | struct inode *dir, |
| 5076 | struct dentry *dentry, |
| 5077 | struct btrfs_key *location, |
| 5078 | struct btrfs_root **sub_root) |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5079 | { |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 5080 | struct btrfs_path *path; |
| 5081 | struct btrfs_root *new_root; |
| 5082 | struct btrfs_root_ref *ref; |
| 5083 | struct extent_buffer *leaf; |
David Sterba | 1d4c08e | 2015-01-02 19:36:14 +0100 | [diff] [blame] | 5084 | struct btrfs_key key; |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 5085 | int ret; |
| 5086 | int err = 0; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5087 | |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 5088 | path = btrfs_alloc_path(); |
| 5089 | if (!path) { |
| 5090 | err = -ENOMEM; |
| 5091 | goto out; |
| 5092 | } |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5093 | |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 5094 | err = -ENOENT; |
David Sterba | 1d4c08e | 2015-01-02 19:36:14 +0100 | [diff] [blame] | 5095 | key.objectid = BTRFS_I(dir)->root->root_key.objectid; |
| 5096 | key.type = BTRFS_ROOT_REF_KEY; |
| 5097 | key.offset = location->objectid; |
| 5098 | |
| 5099 | ret = btrfs_search_slot(NULL, root->fs_info->tree_root, &key, path, |
| 5100 | 0, 0); |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 5101 | if (ret) { |
| 5102 | if (ret < 0) |
| 5103 | err = ret; |
| 5104 | goto out; |
| 5105 | } |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5106 | |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 5107 | leaf = path->nodes[0]; |
| 5108 | ref = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_root_ref); |
Li Zefan | 33345d01 | 2011-04-20 10:31:50 +0800 | [diff] [blame] | 5109 | if (btrfs_root_ref_dirid(leaf, ref) != btrfs_ino(dir) || |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 5110 | btrfs_root_ref_name_len(leaf, ref) != dentry->d_name.len) |
| 5111 | goto out; |
| 5112 | |
| 5113 | ret = memcmp_extent_buffer(leaf, dentry->d_name.name, |
| 5114 | (unsigned long)(ref + 1), |
| 5115 | dentry->d_name.len); |
| 5116 | if (ret) |
| 5117 | goto out; |
| 5118 | |
David Sterba | b3b4aa7 | 2011-04-21 01:20:15 +0200 | [diff] [blame] | 5119 | btrfs_release_path(path); |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 5120 | |
| 5121 | new_root = btrfs_read_fs_root_no_name(root->fs_info, location); |
| 5122 | if (IS_ERR(new_root)) { |
| 5123 | err = PTR_ERR(new_root); |
| 5124 | goto out; |
| 5125 | } |
| 5126 | |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 5127 | *sub_root = new_root; |
| 5128 | location->objectid = btrfs_root_dirid(&new_root->root_item); |
| 5129 | location->type = BTRFS_INODE_ITEM_KEY; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5130 | location->offset = 0; |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 5131 | err = 0; |
| 5132 | out: |
| 5133 | btrfs_free_path(path); |
| 5134 | return err; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5135 | } |
| 5136 | |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 5137 | static void inode_tree_add(struct inode *inode) |
| 5138 | { |
| 5139 | struct btrfs_root *root = BTRFS_I(inode)->root; |
| 5140 | struct btrfs_inode *entry; |
From: Nick Piggin | 03e860b | 2009-08-21 10:09:44 +0200 | [diff] [blame] | 5141 | struct rb_node **p; |
| 5142 | struct rb_node *parent; |
Filipe David Borba Manana | cef2193 | 2013-09-02 12:19:13 +0100 | [diff] [blame] | 5143 | struct rb_node *new = &BTRFS_I(inode)->rb_node; |
Li Zefan | 33345d01 | 2011-04-20 10:31:50 +0800 | [diff] [blame] | 5144 | u64 ino = btrfs_ino(inode); |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 5145 | |
Al Viro | 1d3382cb | 2010-10-23 15:19:20 -0400 | [diff] [blame] | 5146 | if (inode_unhashed(inode)) |
Yan, Zheng | 76dda93 | 2009-09-21 16:00:26 -0400 | [diff] [blame] | 5147 | return; |
Miao Xie | e1409ce | 2013-05-15 07:48:16 +0000 | [diff] [blame] | 5148 | parent = NULL; |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 5149 | spin_lock(&root->inode_lock); |
Miao Xie | e1409ce | 2013-05-15 07:48:16 +0000 | [diff] [blame] | 5150 | p = &root->inode_tree.rb_node; |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 5151 | while (*p) { |
| 5152 | parent = *p; |
| 5153 | entry = rb_entry(parent, struct btrfs_inode, rb_node); |
| 5154 | |
Li Zefan | 33345d01 | 2011-04-20 10:31:50 +0800 | [diff] [blame] | 5155 | if (ino < btrfs_ino(&entry->vfs_inode)) |
From: Nick Piggin | 03e860b | 2009-08-21 10:09:44 +0200 | [diff] [blame] | 5156 | p = &parent->rb_left; |
Li Zefan | 33345d01 | 2011-04-20 10:31:50 +0800 | [diff] [blame] | 5157 | else if (ino > btrfs_ino(&entry->vfs_inode)) |
From: Nick Piggin | 03e860b | 2009-08-21 10:09:44 +0200 | [diff] [blame] | 5158 | p = &parent->rb_right; |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 5159 | else { |
| 5160 | WARN_ON(!(entry->vfs_inode.i_state & |
Al Viro | a4ffdde | 2010-06-02 17:38:30 -0400 | [diff] [blame] | 5161 | (I_WILL_FREE | I_FREEING))); |
Filipe David Borba Manana | cef2193 | 2013-09-02 12:19:13 +0100 | [diff] [blame] | 5162 | rb_replace_node(parent, new, &root->inode_tree); |
From: Nick Piggin | 03e860b | 2009-08-21 10:09:44 +0200 | [diff] [blame] | 5163 | RB_CLEAR_NODE(parent); |
| 5164 | spin_unlock(&root->inode_lock); |
Filipe David Borba Manana | cef2193 | 2013-09-02 12:19:13 +0100 | [diff] [blame] | 5165 | return; |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 5166 | } |
| 5167 | } |
Filipe David Borba Manana | cef2193 | 2013-09-02 12:19:13 +0100 | [diff] [blame] | 5168 | rb_link_node(new, parent, p); |
| 5169 | rb_insert_color(new, &root->inode_tree); |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 5170 | spin_unlock(&root->inode_lock); |
| 5171 | } |
| 5172 | |
| 5173 | static void inode_tree_del(struct inode *inode) |
| 5174 | { |
| 5175 | struct btrfs_root *root = BTRFS_I(inode)->root; |
Yan, Zheng | 76dda93 | 2009-09-21 16:00:26 -0400 | [diff] [blame] | 5176 | int empty = 0; |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 5177 | |
From: Nick Piggin | 03e860b | 2009-08-21 10:09:44 +0200 | [diff] [blame] | 5178 | spin_lock(&root->inode_lock); |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 5179 | if (!RB_EMPTY_NODE(&BTRFS_I(inode)->rb_node)) { |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 5180 | rb_erase(&BTRFS_I(inode)->rb_node, &root->inode_tree); |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 5181 | RB_CLEAR_NODE(&BTRFS_I(inode)->rb_node); |
Yan, Zheng | 76dda93 | 2009-09-21 16:00:26 -0400 | [diff] [blame] | 5182 | empty = RB_EMPTY_ROOT(&root->inode_tree); |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 5183 | } |
From: Nick Piggin | 03e860b | 2009-08-21 10:09:44 +0200 | [diff] [blame] | 5184 | spin_unlock(&root->inode_lock); |
Yan, Zheng | 76dda93 | 2009-09-21 16:00:26 -0400 | [diff] [blame] | 5185 | |
Stefan Behrens | 69e9c6c | 2013-09-05 16:58:43 +0200 | [diff] [blame] | 5186 | if (empty && btrfs_root_refs(&root->root_item) == 0) { |
Yan, Zheng | 76dda93 | 2009-09-21 16:00:26 -0400 | [diff] [blame] | 5187 | synchronize_srcu(&root->fs_info->subvol_srcu); |
| 5188 | spin_lock(&root->inode_lock); |
| 5189 | empty = RB_EMPTY_ROOT(&root->inode_tree); |
| 5190 | spin_unlock(&root->inode_lock); |
| 5191 | if (empty) |
| 5192 | btrfs_add_dead_root(root); |
| 5193 | } |
| 5194 | } |
| 5195 | |
Jeff Mahoney | 143bede | 2012-03-01 14:56:26 +0100 | [diff] [blame] | 5196 | void btrfs_invalidate_inodes(struct btrfs_root *root) |
Yan, Zheng | 76dda93 | 2009-09-21 16:00:26 -0400 | [diff] [blame] | 5197 | { |
| 5198 | struct rb_node *node; |
| 5199 | struct rb_node *prev; |
| 5200 | struct btrfs_inode *entry; |
| 5201 | struct inode *inode; |
| 5202 | u64 objectid = 0; |
| 5203 | |
Liu Bo | 7813b3d | 2014-02-10 17:37:25 +0800 | [diff] [blame] | 5204 | if (!test_bit(BTRFS_FS_STATE_ERROR, &root->fs_info->fs_state)) |
| 5205 | WARN_ON(btrfs_root_refs(&root->root_item) != 0); |
Yan, Zheng | 76dda93 | 2009-09-21 16:00:26 -0400 | [diff] [blame] | 5206 | |
| 5207 | spin_lock(&root->inode_lock); |
| 5208 | again: |
| 5209 | node = root->inode_tree.rb_node; |
| 5210 | prev = NULL; |
| 5211 | while (node) { |
| 5212 | prev = node; |
| 5213 | entry = rb_entry(node, struct btrfs_inode, rb_node); |
| 5214 | |
Li Zefan | 33345d01 | 2011-04-20 10:31:50 +0800 | [diff] [blame] | 5215 | if (objectid < btrfs_ino(&entry->vfs_inode)) |
Yan, Zheng | 76dda93 | 2009-09-21 16:00:26 -0400 | [diff] [blame] | 5216 | node = node->rb_left; |
Li Zefan | 33345d01 | 2011-04-20 10:31:50 +0800 | [diff] [blame] | 5217 | else if (objectid > btrfs_ino(&entry->vfs_inode)) |
Yan, Zheng | 76dda93 | 2009-09-21 16:00:26 -0400 | [diff] [blame] | 5218 | node = node->rb_right; |
| 5219 | else |
| 5220 | break; |
| 5221 | } |
| 5222 | if (!node) { |
| 5223 | while (prev) { |
| 5224 | entry = rb_entry(prev, struct btrfs_inode, rb_node); |
Li Zefan | 33345d01 | 2011-04-20 10:31:50 +0800 | [diff] [blame] | 5225 | if (objectid <= btrfs_ino(&entry->vfs_inode)) { |
Yan, Zheng | 76dda93 | 2009-09-21 16:00:26 -0400 | [diff] [blame] | 5226 | node = prev; |
| 5227 | break; |
| 5228 | } |
| 5229 | prev = rb_next(prev); |
| 5230 | } |
| 5231 | } |
| 5232 | while (node) { |
| 5233 | entry = rb_entry(node, struct btrfs_inode, rb_node); |
Li Zefan | 33345d01 | 2011-04-20 10:31:50 +0800 | [diff] [blame] | 5234 | objectid = btrfs_ino(&entry->vfs_inode) + 1; |
Yan, Zheng | 76dda93 | 2009-09-21 16:00:26 -0400 | [diff] [blame] | 5235 | inode = igrab(&entry->vfs_inode); |
| 5236 | if (inode) { |
| 5237 | spin_unlock(&root->inode_lock); |
| 5238 | if (atomic_read(&inode->i_count) > 1) |
| 5239 | d_prune_aliases(inode); |
| 5240 | /* |
Al Viro | 45321ac | 2010-06-07 13:43:19 -0400 | [diff] [blame] | 5241 | * btrfs_drop_inode will have it removed from |
Yan, Zheng | 76dda93 | 2009-09-21 16:00:26 -0400 | [diff] [blame] | 5242 | * the inode cache when its usage count |
| 5243 | * hits zero. |
| 5244 | */ |
| 5245 | iput(inode); |
| 5246 | cond_resched(); |
| 5247 | spin_lock(&root->inode_lock); |
| 5248 | goto again; |
| 5249 | } |
| 5250 | |
| 5251 | if (cond_resched_lock(&root->inode_lock)) |
| 5252 | goto again; |
| 5253 | |
| 5254 | node = rb_next(node); |
| 5255 | } |
| 5256 | spin_unlock(&root->inode_lock); |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 5257 | } |
| 5258 | |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 5259 | static int btrfs_init_locked_inode(struct inode *inode, void *p) |
| 5260 | { |
| 5261 | struct btrfs_iget_args *args = p; |
Chris Mason | 90d3e59 | 2014-01-09 17:28:00 -0800 | [diff] [blame] | 5262 | inode->i_ino = args->location->objectid; |
| 5263 | memcpy(&BTRFS_I(inode)->location, args->location, |
| 5264 | sizeof(*args->location)); |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 5265 | BTRFS_I(inode)->root = args->root; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5266 | return 0; |
| 5267 | } |
| 5268 | |
| 5269 | static int btrfs_find_actor(struct inode *inode, void *opaque) |
| 5270 | { |
| 5271 | struct btrfs_iget_args *args = opaque; |
Chris Mason | 90d3e59 | 2014-01-09 17:28:00 -0800 | [diff] [blame] | 5272 | return args->location->objectid == BTRFS_I(inode)->location.objectid && |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 5273 | args->root == BTRFS_I(inode)->root; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5274 | } |
| 5275 | |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 5276 | static struct inode *btrfs_iget_locked(struct super_block *s, |
Chris Mason | 90d3e59 | 2014-01-09 17:28:00 -0800 | [diff] [blame] | 5277 | struct btrfs_key *location, |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 5278 | struct btrfs_root *root) |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5279 | { |
| 5280 | struct inode *inode; |
| 5281 | struct btrfs_iget_args args; |
Chris Mason | 90d3e59 | 2014-01-09 17:28:00 -0800 | [diff] [blame] | 5282 | unsigned long hashval = btrfs_inode_hash(location->objectid, root); |
Filipe David Borba Manana | 778ba82 | 2013-10-06 22:22:33 +0100 | [diff] [blame] | 5283 | |
Chris Mason | 90d3e59 | 2014-01-09 17:28:00 -0800 | [diff] [blame] | 5284 | args.location = location; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5285 | args.root = root; |
| 5286 | |
Filipe David Borba Manana | 778ba82 | 2013-10-06 22:22:33 +0100 | [diff] [blame] | 5287 | inode = iget5_locked(s, hashval, btrfs_find_actor, |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5288 | btrfs_init_locked_inode, |
| 5289 | (void *)&args); |
| 5290 | return inode; |
| 5291 | } |
| 5292 | |
Balaji Rao | 1a54ef8 | 2008-07-21 02:01:04 +0530 | [diff] [blame] | 5293 | /* Get an inode object given its location and corresponding root. |
| 5294 | * Returns in *is_new if the inode was read from disk |
| 5295 | */ |
| 5296 | struct inode *btrfs_iget(struct super_block *s, struct btrfs_key *location, |
Josef Bacik | 73f7341 | 2009-12-04 17:38:27 +0000 | [diff] [blame] | 5297 | struct btrfs_root *root, int *new) |
Balaji Rao | 1a54ef8 | 2008-07-21 02:01:04 +0530 | [diff] [blame] | 5298 | { |
| 5299 | struct inode *inode; |
| 5300 | |
Chris Mason | 90d3e59 | 2014-01-09 17:28:00 -0800 | [diff] [blame] | 5301 | inode = btrfs_iget_locked(s, location, root); |
Balaji Rao | 1a54ef8 | 2008-07-21 02:01:04 +0530 | [diff] [blame] | 5302 | if (!inode) |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 5303 | return ERR_PTR(-ENOMEM); |
Balaji Rao | 1a54ef8 | 2008-07-21 02:01:04 +0530 | [diff] [blame] | 5304 | |
| 5305 | if (inode->i_state & I_NEW) { |
Balaji Rao | 1a54ef8 | 2008-07-21 02:01:04 +0530 | [diff] [blame] | 5306 | btrfs_read_locked_inode(inode); |
Mark Fasheh | 1748f84 | 2011-07-12 11:25:31 -0700 | [diff] [blame] | 5307 | if (!is_bad_inode(inode)) { |
| 5308 | inode_tree_add(inode); |
| 5309 | unlock_new_inode(inode); |
| 5310 | if (new) |
| 5311 | *new = 1; |
| 5312 | } else { |
Sergei Trofimovich | e0b6d65 | 2011-09-11 10:52:24 -0400 | [diff] [blame] | 5313 | unlock_new_inode(inode); |
| 5314 | iput(inode); |
| 5315 | inode = ERR_PTR(-ESTALE); |
Mark Fasheh | 1748f84 | 2011-07-12 11:25:31 -0700 | [diff] [blame] | 5316 | } |
| 5317 | } |
| 5318 | |
Balaji Rao | 1a54ef8 | 2008-07-21 02:01:04 +0530 | [diff] [blame] | 5319 | return inode; |
| 5320 | } |
| 5321 | |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 5322 | static struct inode *new_simple_dir(struct super_block *s, |
| 5323 | struct btrfs_key *key, |
| 5324 | struct btrfs_root *root) |
| 5325 | { |
| 5326 | struct inode *inode = new_inode(s); |
| 5327 | |
| 5328 | if (!inode) |
| 5329 | return ERR_PTR(-ENOMEM); |
| 5330 | |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 5331 | BTRFS_I(inode)->root = root; |
| 5332 | memcpy(&BTRFS_I(inode)->location, key, sizeof(*key)); |
Josef Bacik | 72ac3c0 | 2012-05-23 14:13:11 -0400 | [diff] [blame] | 5333 | set_bit(BTRFS_INODE_DUMMY, &BTRFS_I(inode)->runtime_flags); |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 5334 | |
| 5335 | inode->i_ino = BTRFS_EMPTY_SUBVOL_DIR_OBJECTID; |
Li Zefan | 848cce0 | 2012-02-21 17:04:28 +0800 | [diff] [blame] | 5336 | inode->i_op = &btrfs_dir_ro_inode_operations; |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 5337 | inode->i_fop = &simple_dir_operations; |
| 5338 | inode->i_mode = S_IFDIR | S_IRUGO | S_IWUSR | S_IXUGO; |
chandan r | 9cc97d6 | 2012-07-04 12:48:07 +0530 | [diff] [blame] | 5339 | inode->i_mtime = CURRENT_TIME; |
| 5340 | inode->i_atime = inode->i_mtime; |
| 5341 | inode->i_ctime = inode->i_mtime; |
| 5342 | BTRFS_I(inode)->i_otime = inode->i_mtime; |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 5343 | |
| 5344 | return inode; |
| 5345 | } |
| 5346 | |
Chris Mason | 3de4586 | 2008-11-17 21:02:50 -0500 | [diff] [blame] | 5347 | struct inode *btrfs_lookup_dentry(struct inode *dir, struct dentry *dentry) |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5348 | { |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 5349 | struct inode *inode; |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 5350 | struct btrfs_root *root = BTRFS_I(dir)->root; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5351 | struct btrfs_root *sub_root = root; |
| 5352 | struct btrfs_key location; |
Yan, Zheng | 76dda93 | 2009-09-21 16:00:26 -0400 | [diff] [blame] | 5353 | int index; |
Josef Bacik | b4aff1f | 2011-06-28 16:18:59 -0400 | [diff] [blame] | 5354 | int ret = 0; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5355 | |
| 5356 | if (dentry->d_name.len > BTRFS_NAME_LEN) |
| 5357 | return ERR_PTR(-ENAMETOOLONG); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 5358 | |
Jeff Layton | 39e3c95 | 2012-11-28 11:30:53 -0500 | [diff] [blame] | 5359 | ret = btrfs_inode_by_name(dir, dentry, &location); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5360 | if (ret < 0) |
| 5361 | return ERR_PTR(ret); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 5362 | |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 5363 | if (location.objectid == 0) |
Tsutomu Itoh | 5662344b3 | 2013-12-13 09:51:42 +0900 | [diff] [blame] | 5364 | return ERR_PTR(-ENOENT); |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 5365 | |
| 5366 | if (location.type == BTRFS_INODE_ITEM_KEY) { |
Josef Bacik | 73f7341 | 2009-12-04 17:38:27 +0000 | [diff] [blame] | 5367 | inode = btrfs_iget(dir->i_sb, &location, root, NULL); |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 5368 | return inode; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5369 | } |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 5370 | |
| 5371 | BUG_ON(location.type != BTRFS_ROOT_ITEM_KEY); |
| 5372 | |
Yan, Zheng | 76dda93 | 2009-09-21 16:00:26 -0400 | [diff] [blame] | 5373 | index = srcu_read_lock(&root->fs_info->subvol_srcu); |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 5374 | ret = fixup_tree_root_location(root, dir, dentry, |
| 5375 | &location, &sub_root); |
| 5376 | if (ret < 0) { |
| 5377 | if (ret != -ENOENT) |
| 5378 | inode = ERR_PTR(ret); |
| 5379 | else |
| 5380 | inode = new_simple_dir(dir->i_sb, &location, sub_root); |
| 5381 | } else { |
Josef Bacik | 73f7341 | 2009-12-04 17:38:27 +0000 | [diff] [blame] | 5382 | inode = btrfs_iget(dir->i_sb, &location, sub_root, NULL); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5383 | } |
Yan, Zheng | 76dda93 | 2009-09-21 16:00:26 -0400 | [diff] [blame] | 5384 | srcu_read_unlock(&root->fs_info->subvol_srcu, index); |
| 5385 | |
Julia Lawall | 34d19ba | 2011-01-24 19:55:19 +0000 | [diff] [blame] | 5386 | if (!IS_ERR(inode) && root != sub_root) { |
Yan, Zheng | c71bf09 | 2009-11-12 09:34:40 +0000 | [diff] [blame] | 5387 | down_read(&root->fs_info->cleanup_work_sem); |
| 5388 | if (!(inode->i_sb->s_flags & MS_RDONLY)) |
Josef Bacik | 66b4ffd | 2011-01-31 16:22:42 -0500 | [diff] [blame] | 5389 | ret = btrfs_orphan_cleanup(sub_root); |
Yan, Zheng | c71bf09 | 2009-11-12 09:34:40 +0000 | [diff] [blame] | 5390 | up_read(&root->fs_info->cleanup_work_sem); |
Josef Bacik | 01cd336 | 2013-06-03 21:39:49 -0400 | [diff] [blame] | 5391 | if (ret) { |
| 5392 | iput(inode); |
Josef Bacik | 66b4ffd | 2011-01-31 16:22:42 -0500 | [diff] [blame] | 5393 | inode = ERR_PTR(ret); |
Josef Bacik | 01cd336 | 2013-06-03 21:39:49 -0400 | [diff] [blame] | 5394 | } |
Yan, Zheng | c71bf09 | 2009-11-12 09:34:40 +0000 | [diff] [blame] | 5395 | } |
| 5396 | |
Chris Mason | 3de4586 | 2008-11-17 21:02:50 -0500 | [diff] [blame] | 5397 | return inode; |
| 5398 | } |
| 5399 | |
Nick Piggin | fe15ce4 | 2011-01-07 17:49:23 +1100 | [diff] [blame] | 5400 | static int btrfs_dentry_delete(const struct dentry *dentry) |
Yan, Zheng | 76dda93 | 2009-09-21 16:00:26 -0400 | [diff] [blame] | 5401 | { |
| 5402 | struct btrfs_root *root; |
Li Zefan | 848cce0 | 2012-02-21 17:04:28 +0800 | [diff] [blame] | 5403 | struct inode *inode = dentry->d_inode; |
Yan, Zheng | 76dda93 | 2009-09-21 16:00:26 -0400 | [diff] [blame] | 5404 | |
Li Zefan | 848cce0 | 2012-02-21 17:04:28 +0800 | [diff] [blame] | 5405 | if (!inode && !IS_ROOT(dentry)) |
| 5406 | inode = dentry->d_parent->d_inode; |
Yan, Zheng | 76dda93 | 2009-09-21 16:00:26 -0400 | [diff] [blame] | 5407 | |
Li Zefan | 848cce0 | 2012-02-21 17:04:28 +0800 | [diff] [blame] | 5408 | if (inode) { |
| 5409 | root = BTRFS_I(inode)->root; |
Yan, Zheng | efefb14 | 2009-10-09 09:25:16 -0400 | [diff] [blame] | 5410 | if (btrfs_root_refs(&root->root_item) == 0) |
| 5411 | return 1; |
Li Zefan | 848cce0 | 2012-02-21 17:04:28 +0800 | [diff] [blame] | 5412 | |
| 5413 | if (btrfs_ino(inode) == BTRFS_EMPTY_SUBVOL_DIR_OBJECTID) |
| 5414 | return 1; |
Yan, Zheng | efefb14 | 2009-10-09 09:25:16 -0400 | [diff] [blame] | 5415 | } |
Yan, Zheng | 76dda93 | 2009-09-21 16:00:26 -0400 | [diff] [blame] | 5416 | return 0; |
| 5417 | } |
| 5418 | |
Josef Bacik | b4aff1f | 2011-06-28 16:18:59 -0400 | [diff] [blame] | 5419 | static void btrfs_dentry_release(struct dentry *dentry) |
| 5420 | { |
Daeseok Youn | 944a451 | 2014-04-14 15:37:02 +0900 | [diff] [blame] | 5421 | kfree(dentry->d_fsdata); |
Josef Bacik | b4aff1f | 2011-06-28 16:18:59 -0400 | [diff] [blame] | 5422 | } |
| 5423 | |
Chris Mason | 3de4586 | 2008-11-17 21:02:50 -0500 | [diff] [blame] | 5424 | static struct dentry *btrfs_lookup(struct inode *dir, struct dentry *dentry, |
Al Viro | 00cd8dd | 2012-06-10 17:13:09 -0400 | [diff] [blame] | 5425 | unsigned int flags) |
Chris Mason | 3de4586 | 2008-11-17 21:02:50 -0500 | [diff] [blame] | 5426 | { |
Tsutomu Itoh | 5662344b3 | 2013-12-13 09:51:42 +0900 | [diff] [blame] | 5427 | struct inode *inode; |
Josef Bacik | a66e7cc | 2011-09-18 10:34:03 -0400 | [diff] [blame] | 5428 | |
Tsutomu Itoh | 5662344b3 | 2013-12-13 09:51:42 +0900 | [diff] [blame] | 5429 | inode = btrfs_lookup_dentry(dir, dentry); |
| 5430 | if (IS_ERR(inode)) { |
| 5431 | if (PTR_ERR(inode) == -ENOENT) |
| 5432 | inode = NULL; |
| 5433 | else |
| 5434 | return ERR_CAST(inode); |
| 5435 | } |
| 5436 | |
Al Viro | 41d28bc | 2014-10-12 22:24:21 -0400 | [diff] [blame] | 5437 | return d_splice_alias(inode, dentry); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5438 | } |
| 5439 | |
Miao Xie | 16cdcec | 2011-04-22 18:12:22 +0800 | [diff] [blame] | 5440 | unsigned char btrfs_filetype_table[] = { |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5441 | DT_UNKNOWN, DT_REG, DT_DIR, DT_CHR, DT_BLK, DT_FIFO, DT_SOCK, DT_LNK |
| 5442 | }; |
| 5443 | |
Al Viro | 9cdda8d | 2013-05-22 16:48:09 -0400 | [diff] [blame] | 5444 | static int btrfs_real_readdir(struct file *file, struct dir_context *ctx) |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5445 | { |
Al Viro | 9cdda8d | 2013-05-22 16:48:09 -0400 | [diff] [blame] | 5446 | struct inode *inode = file_inode(file); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5447 | struct btrfs_root *root = BTRFS_I(inode)->root; |
| 5448 | struct btrfs_item *item; |
| 5449 | struct btrfs_dir_item *di; |
| 5450 | struct btrfs_key key; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 5451 | struct btrfs_key found_key; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5452 | struct btrfs_path *path; |
Miao Xie | 16cdcec | 2011-04-22 18:12:22 +0800 | [diff] [blame] | 5453 | struct list_head ins_list; |
| 5454 | struct list_head del_list; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5455 | int ret; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 5456 | struct extent_buffer *leaf; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5457 | int slot; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5458 | unsigned char d_type; |
| 5459 | int over = 0; |
| 5460 | u32 di_cur; |
| 5461 | u32 di_total; |
| 5462 | u32 di_len; |
| 5463 | int key_type = BTRFS_DIR_INDEX_KEY; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 5464 | char tmp_name[32]; |
| 5465 | char *name_ptr; |
| 5466 | int name_len; |
Al Viro | 9cdda8d | 2013-05-22 16:48:09 -0400 | [diff] [blame] | 5467 | int is_curr = 0; /* ctx->pos points to the current index? */ |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5468 | |
| 5469 | /* FIXME, use a real flag for deciding about the key type */ |
| 5470 | if (root->fs_info->tree_root == root) |
| 5471 | key_type = BTRFS_DIR_ITEM_KEY; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 5472 | |
Al Viro | 9cdda8d | 2013-05-22 16:48:09 -0400 | [diff] [blame] | 5473 | if (!dir_emit_dots(file, ctx)) |
| 5474 | return 0; |
| 5475 | |
David Woodhouse | 49593bf | 2008-08-17 17:08:36 +0100 | [diff] [blame] | 5476 | path = btrfs_alloc_path(); |
Miao Xie | 16cdcec | 2011-04-22 18:12:22 +0800 | [diff] [blame] | 5477 | if (!path) |
| 5478 | return -ENOMEM; |
Chris Mason | ff5714c | 2011-05-28 07:00:39 -0400 | [diff] [blame] | 5479 | |
Josef Bacik | 026fd31 | 2011-05-13 10:32:11 -0400 | [diff] [blame] | 5480 | path->reada = 1; |
David Woodhouse | 49593bf | 2008-08-17 17:08:36 +0100 | [diff] [blame] | 5481 | |
Miao Xie | 16cdcec | 2011-04-22 18:12:22 +0800 | [diff] [blame] | 5482 | if (key_type == BTRFS_DIR_INDEX_KEY) { |
| 5483 | INIT_LIST_HEAD(&ins_list); |
| 5484 | INIT_LIST_HEAD(&del_list); |
| 5485 | btrfs_get_delayed_items(inode, &ins_list, &del_list); |
| 5486 | } |
| 5487 | |
David Sterba | 962a298 | 2014-06-04 18:41:45 +0200 | [diff] [blame] | 5488 | key.type = key_type; |
Al Viro | 9cdda8d | 2013-05-22 16:48:09 -0400 | [diff] [blame] | 5489 | key.offset = ctx->pos; |
Li Zefan | 33345d01 | 2011-04-20 10:31:50 +0800 | [diff] [blame] | 5490 | key.objectid = btrfs_ino(inode); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 5491 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5492 | ret = btrfs_search_slot(NULL, root, &key, path, 0, 0); |
| 5493 | if (ret < 0) |
| 5494 | goto err; |
David Woodhouse | 49593bf | 2008-08-17 17:08:36 +0100 | [diff] [blame] | 5495 | |
| 5496 | while (1) { |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 5497 | leaf = path->nodes[0]; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5498 | slot = path->slots[0]; |
Li Zefan | b9e03af | 2011-03-23 10:43:58 +0800 | [diff] [blame] | 5499 | if (slot >= btrfs_header_nritems(leaf)) { |
| 5500 | ret = btrfs_next_leaf(root, path); |
| 5501 | if (ret < 0) |
| 5502 | goto err; |
| 5503 | else if (ret > 0) |
| 5504 | break; |
| 5505 | continue; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5506 | } |
Chris Mason | 3de4586 | 2008-11-17 21:02:50 -0500 | [diff] [blame] | 5507 | |
Ross Kirk | dd3cc16 | 2013-09-16 15:58:09 +0100 | [diff] [blame] | 5508 | item = btrfs_item_nr(slot); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 5509 | btrfs_item_key_to_cpu(leaf, &found_key, slot); |
| 5510 | |
| 5511 | if (found_key.objectid != key.objectid) |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5512 | break; |
David Sterba | 962a298 | 2014-06-04 18:41:45 +0200 | [diff] [blame] | 5513 | if (found_key.type != key_type) |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5514 | break; |
Al Viro | 9cdda8d | 2013-05-22 16:48:09 -0400 | [diff] [blame] | 5515 | if (found_key.offset < ctx->pos) |
Li Zefan | b9e03af | 2011-03-23 10:43:58 +0800 | [diff] [blame] | 5516 | goto next; |
Miao Xie | 16cdcec | 2011-04-22 18:12:22 +0800 | [diff] [blame] | 5517 | if (key_type == BTRFS_DIR_INDEX_KEY && |
| 5518 | btrfs_should_delete_dir_index(&del_list, |
| 5519 | found_key.offset)) |
| 5520 | goto next; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 5521 | |
Al Viro | 9cdda8d | 2013-05-22 16:48:09 -0400 | [diff] [blame] | 5522 | ctx->pos = found_key.offset; |
Miao Xie | 16cdcec | 2011-04-22 18:12:22 +0800 | [diff] [blame] | 5523 | is_curr = 1; |
David Woodhouse | 49593bf | 2008-08-17 17:08:36 +0100 | [diff] [blame] | 5524 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5525 | di = btrfs_item_ptr(leaf, slot, struct btrfs_dir_item); |
| 5526 | di_cur = 0; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 5527 | di_total = btrfs_item_size(leaf, item); |
David Woodhouse | 49593bf | 2008-08-17 17:08:36 +0100 | [diff] [blame] | 5528 | |
| 5529 | while (di_cur < di_total) { |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 5530 | struct btrfs_key location; |
| 5531 | |
Josef Bacik | 22a94d4 | 2011-03-16 16:47:17 -0400 | [diff] [blame] | 5532 | if (verify_dir_item(root, leaf, di)) |
| 5533 | break; |
| 5534 | |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 5535 | name_len = btrfs_dir_name_len(leaf, di); |
David Woodhouse | 49593bf | 2008-08-17 17:08:36 +0100 | [diff] [blame] | 5536 | if (name_len <= sizeof(tmp_name)) { |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 5537 | name_ptr = tmp_name; |
| 5538 | } else { |
| 5539 | name_ptr = kmalloc(name_len, GFP_NOFS); |
David Woodhouse | 49593bf | 2008-08-17 17:08:36 +0100 | [diff] [blame] | 5540 | if (!name_ptr) { |
| 5541 | ret = -ENOMEM; |
| 5542 | goto err; |
| 5543 | } |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 5544 | } |
| 5545 | read_extent_buffer(leaf, name_ptr, |
| 5546 | (unsigned long)(di + 1), name_len); |
| 5547 | |
| 5548 | d_type = btrfs_filetype_table[btrfs_dir_type(leaf, di)]; |
| 5549 | btrfs_dir_item_key_to_cpu(leaf, di, &location); |
Chris Mason | 3de4586 | 2008-11-17 21:02:50 -0500 | [diff] [blame] | 5550 | |
Josef Bacik | b4aff1f | 2011-06-28 16:18:59 -0400 | [diff] [blame] | 5551 | |
Chris Mason | 3de4586 | 2008-11-17 21:02:50 -0500 | [diff] [blame] | 5552 | /* is this a reference to our own snapshot? If so |
Arne Jansen | 8c9c2bf | 2012-02-25 09:09:30 +0100 | [diff] [blame] | 5553 | * skip it. |
| 5554 | * |
| 5555 | * In contrast to old kernels, we insert the snapshot's |
| 5556 | * dir item and dir index after it has been created, so |
| 5557 | * we won't find a reference to our own snapshot. We |
| 5558 | * still keep the following code for backward |
| 5559 | * compatibility. |
Chris Mason | 3de4586 | 2008-11-17 21:02:50 -0500 | [diff] [blame] | 5560 | */ |
| 5561 | if (location.type == BTRFS_ROOT_ITEM_KEY && |
| 5562 | location.objectid == root->root_key.objectid) { |
| 5563 | over = 0; |
| 5564 | goto skip; |
| 5565 | } |
Al Viro | 9cdda8d | 2013-05-22 16:48:09 -0400 | [diff] [blame] | 5566 | over = !dir_emit(ctx, name_ptr, name_len, |
| 5567 | location.objectid, d_type); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 5568 | |
Chris Mason | 3de4586 | 2008-11-17 21:02:50 -0500 | [diff] [blame] | 5569 | skip: |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 5570 | if (name_ptr != tmp_name) |
| 5571 | kfree(name_ptr); |
| 5572 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5573 | if (over) |
| 5574 | goto nopos; |
Josef Bacik | 5103e94 | 2007-11-16 11:45:54 -0500 | [diff] [blame] | 5575 | di_len = btrfs_dir_name_len(leaf, di) + |
David Woodhouse | 49593bf | 2008-08-17 17:08:36 +0100 | [diff] [blame] | 5576 | btrfs_dir_data_len(leaf, di) + sizeof(*di); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5577 | di_cur += di_len; |
| 5578 | di = (struct btrfs_dir_item *)((char *)di + di_len); |
| 5579 | } |
Li Zefan | b9e03af | 2011-03-23 10:43:58 +0800 | [diff] [blame] | 5580 | next: |
| 5581 | path->slots[0]++; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5582 | } |
David Woodhouse | 49593bf | 2008-08-17 17:08:36 +0100 | [diff] [blame] | 5583 | |
Miao Xie | 16cdcec | 2011-04-22 18:12:22 +0800 | [diff] [blame] | 5584 | if (key_type == BTRFS_DIR_INDEX_KEY) { |
| 5585 | if (is_curr) |
Al Viro | 9cdda8d | 2013-05-22 16:48:09 -0400 | [diff] [blame] | 5586 | ctx->pos++; |
| 5587 | ret = btrfs_readdir_delayed_dir_index(ctx, &ins_list); |
Miao Xie | 16cdcec | 2011-04-22 18:12:22 +0800 | [diff] [blame] | 5588 | if (ret) |
| 5589 | goto nopos; |
| 5590 | } |
| 5591 | |
David Woodhouse | 49593bf | 2008-08-17 17:08:36 +0100 | [diff] [blame] | 5592 | /* Reached end of directory/root. Bump pos past the last item. */ |
Zach Brown | db62efb | 2013-07-11 16:19:42 -0700 | [diff] [blame] | 5593 | ctx->pos++; |
| 5594 | |
| 5595 | /* |
| 5596 | * Stop new entries from being returned after we return the last |
| 5597 | * entry. |
| 5598 | * |
| 5599 | * New directory entries are assigned a strictly increasing |
| 5600 | * offset. This means that new entries created during readdir |
| 5601 | * are *guaranteed* to be seen in the future by that readdir. |
| 5602 | * This has broken buggy programs which operate on names as |
| 5603 | * they're returned by readdir. Until we re-use freed offsets |
| 5604 | * we have this hack to stop new entries from being returned |
| 5605 | * under the assumption that they'll never reach this huge |
| 5606 | * offset. |
| 5607 | * |
| 5608 | * This is being careful not to overflow 32bit loff_t unless the |
| 5609 | * last entry requires it because doing so has broken 32bit apps |
| 5610 | * in the past. |
| 5611 | */ |
| 5612 | if (key_type == BTRFS_DIR_INDEX_KEY) { |
| 5613 | if (ctx->pos >= INT_MAX) |
| 5614 | ctx->pos = LLONG_MAX; |
| 5615 | else |
| 5616 | ctx->pos = INT_MAX; |
| 5617 | } |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5618 | nopos: |
| 5619 | ret = 0; |
| 5620 | err: |
Miao Xie | 16cdcec | 2011-04-22 18:12:22 +0800 | [diff] [blame] | 5621 | if (key_type == BTRFS_DIR_INDEX_KEY) |
| 5622 | btrfs_put_delayed_items(&ins_list, &del_list); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5623 | btrfs_free_path(path); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5624 | return ret; |
| 5625 | } |
| 5626 | |
Christoph Hellwig | a9185b4 | 2010-03-05 09:21:37 +0100 | [diff] [blame] | 5627 | int btrfs_write_inode(struct inode *inode, struct writeback_control *wbc) |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5628 | { |
| 5629 | struct btrfs_root *root = BTRFS_I(inode)->root; |
| 5630 | struct btrfs_trans_handle *trans; |
| 5631 | int ret = 0; |
Josef Bacik | 0af3d00 | 2010-06-21 14:48:16 -0400 | [diff] [blame] | 5632 | bool nolock = false; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5633 | |
Josef Bacik | 72ac3c0 | 2012-05-23 14:13:11 -0400 | [diff] [blame] | 5634 | if (test_bit(BTRFS_INODE_DUMMY, &BTRFS_I(inode)->runtime_flags)) |
Chris Mason | 4ca8b41 | 2008-08-05 13:30:48 -0400 | [diff] [blame] | 5635 | return 0; |
| 5636 | |
Liu Bo | 83eea1f | 2012-07-10 05:28:39 -0600 | [diff] [blame] | 5637 | 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] | 5638 | nolock = true; |
Josef Bacik | 0af3d00 | 2010-06-21 14:48:16 -0400 | [diff] [blame] | 5639 | |
Christoph Hellwig | a9185b4 | 2010-03-05 09:21:37 +0100 | [diff] [blame] | 5640 | if (wbc->sync_mode == WB_SYNC_ALL) { |
Josef Bacik | 0af3d00 | 2010-06-21 14:48:16 -0400 | [diff] [blame] | 5641 | if (nolock) |
Josef Bacik | 7a7eaa4 | 2011-04-13 12:54:33 -0400 | [diff] [blame] | 5642 | trans = btrfs_join_transaction_nolock(root); |
Josef Bacik | 0af3d00 | 2010-06-21 14:48:16 -0400 | [diff] [blame] | 5643 | else |
Josef Bacik | 7a7eaa4 | 2011-04-13 12:54:33 -0400 | [diff] [blame] | 5644 | trans = btrfs_join_transaction(root); |
Tsutomu Itoh | 3612b49 | 2011-01-25 02:51:38 +0000 | [diff] [blame] | 5645 | if (IS_ERR(trans)) |
| 5646 | return PTR_ERR(trans); |
Miao Xie | a698d075 | 2012-09-20 01:51:59 -0600 | [diff] [blame] | 5647 | ret = btrfs_commit_transaction(trans, root); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5648 | } |
| 5649 | return ret; |
| 5650 | } |
| 5651 | |
| 5652 | /* |
Chris Mason | 54aa1f4 | 2007-06-22 14:16:25 -0400 | [diff] [blame] | 5653 | * This is somewhat expensive, updating the tree every time the |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5654 | * inode changes. But, it is most likely to find the inode in cache. |
| 5655 | * FIXME, needs more benchmarking...there are no reasons other than performance |
| 5656 | * to keep or drop this code. |
| 5657 | */ |
Eric Sandeen | 48a3b63 | 2013-04-25 20:41:01 +0000 | [diff] [blame] | 5658 | static int btrfs_dirty_inode(struct inode *inode) |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5659 | { |
| 5660 | struct btrfs_root *root = BTRFS_I(inode)->root; |
| 5661 | struct btrfs_trans_handle *trans; |
Yan, Zheng | 8929ecfa | 2010-05-16 10:49:58 -0400 | [diff] [blame] | 5662 | int ret; |
| 5663 | |
Josef Bacik | 72ac3c0 | 2012-05-23 14:13:11 -0400 | [diff] [blame] | 5664 | if (test_bit(BTRFS_INODE_DUMMY, &BTRFS_I(inode)->runtime_flags)) |
Josef Bacik | 22c44fe | 2011-11-30 10:45:38 -0500 | [diff] [blame] | 5665 | return 0; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5666 | |
Josef Bacik | 7a7eaa4 | 2011-04-13 12:54:33 -0400 | [diff] [blame] | 5667 | trans = btrfs_join_transaction(root); |
Josef Bacik | 22c44fe | 2011-11-30 10:45:38 -0500 | [diff] [blame] | 5668 | if (IS_ERR(trans)) |
| 5669 | return PTR_ERR(trans); |
Yan, Zheng | 8929ecfa | 2010-05-16 10:49:58 -0400 | [diff] [blame] | 5670 | |
| 5671 | ret = btrfs_update_inode(trans, root, inode); |
Chris Mason | 94b6044 | 2010-05-26 11:02:00 -0400 | [diff] [blame] | 5672 | if (ret && ret == -ENOSPC) { |
| 5673 | /* whoops, lets try again with the full transaction */ |
| 5674 | btrfs_end_transaction(trans, root); |
| 5675 | trans = btrfs_start_transaction(root, 1); |
Josef Bacik | 22c44fe | 2011-11-30 10:45:38 -0500 | [diff] [blame] | 5676 | if (IS_ERR(trans)) |
| 5677 | return PTR_ERR(trans); |
Yan, Zheng | 8929ecfa | 2010-05-16 10:49:58 -0400 | [diff] [blame] | 5678 | |
Chris Mason | 94b6044 | 2010-05-26 11:02:00 -0400 | [diff] [blame] | 5679 | ret = btrfs_update_inode(trans, root, inode); |
Chris Mason | 94b6044 | 2010-05-26 11:02:00 -0400 | [diff] [blame] | 5680 | } |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5681 | btrfs_end_transaction(trans, root); |
Miao Xie | 16cdcec | 2011-04-22 18:12:22 +0800 | [diff] [blame] | 5682 | if (BTRFS_I(inode)->delayed_node) |
| 5683 | btrfs_balance_delayed_items(root); |
Josef Bacik | 22c44fe | 2011-11-30 10:45:38 -0500 | [diff] [blame] | 5684 | |
| 5685 | return ret; |
| 5686 | } |
| 5687 | |
| 5688 | /* |
| 5689 | * This is a copy of file_update_time. We need this so we can return error on |
| 5690 | * ENOSPC for updating the inode in the case of file write and mmap writes. |
| 5691 | */ |
Josef Bacik | e41f941 | 2012-03-26 09:46:47 -0400 | [diff] [blame] | 5692 | static int btrfs_update_time(struct inode *inode, struct timespec *now, |
| 5693 | int flags) |
Josef Bacik | 22c44fe | 2011-11-30 10:45:38 -0500 | [diff] [blame] | 5694 | { |
Alexander Block | 2bc556528 | 2012-06-15 09:49:33 +0200 | [diff] [blame] | 5695 | struct btrfs_root *root = BTRFS_I(inode)->root; |
| 5696 | |
| 5697 | if (btrfs_root_readonly(root)) |
| 5698 | return -EROFS; |
| 5699 | |
Josef Bacik | e41f941 | 2012-03-26 09:46:47 -0400 | [diff] [blame] | 5700 | if (flags & S_VERSION) |
Josef Bacik | 22c44fe | 2011-11-30 10:45:38 -0500 | [diff] [blame] | 5701 | inode_inc_iversion(inode); |
Josef Bacik | e41f941 | 2012-03-26 09:46:47 -0400 | [diff] [blame] | 5702 | if (flags & S_CTIME) |
| 5703 | inode->i_ctime = *now; |
| 5704 | if (flags & S_MTIME) |
| 5705 | inode->i_mtime = *now; |
| 5706 | if (flags & S_ATIME) |
| 5707 | inode->i_atime = *now; |
| 5708 | return btrfs_dirty_inode(inode); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5709 | } |
| 5710 | |
Chris Mason | d352ac6 | 2008-09-29 15:18:18 -0400 | [diff] [blame] | 5711 | /* |
| 5712 | * find the highest existing sequence number in a directory |
| 5713 | * and then set the in-memory index_cnt variable to reflect |
| 5714 | * free sequence numbers |
| 5715 | */ |
Josef Bacik | aec7477 | 2008-07-24 12:12:38 -0400 | [diff] [blame] | 5716 | static int btrfs_set_inode_index_count(struct inode *inode) |
| 5717 | { |
| 5718 | struct btrfs_root *root = BTRFS_I(inode)->root; |
| 5719 | struct btrfs_key key, found_key; |
| 5720 | struct btrfs_path *path; |
| 5721 | struct extent_buffer *leaf; |
| 5722 | int ret; |
| 5723 | |
Li Zefan | 33345d01 | 2011-04-20 10:31:50 +0800 | [diff] [blame] | 5724 | key.objectid = btrfs_ino(inode); |
David Sterba | 962a298 | 2014-06-04 18:41:45 +0200 | [diff] [blame] | 5725 | key.type = BTRFS_DIR_INDEX_KEY; |
Josef Bacik | aec7477 | 2008-07-24 12:12:38 -0400 | [diff] [blame] | 5726 | key.offset = (u64)-1; |
| 5727 | |
| 5728 | path = btrfs_alloc_path(); |
| 5729 | if (!path) |
| 5730 | return -ENOMEM; |
| 5731 | |
| 5732 | ret = btrfs_search_slot(NULL, root, &key, path, 0, 0); |
| 5733 | if (ret < 0) |
| 5734 | goto out; |
| 5735 | /* FIXME: we should be able to handle this */ |
| 5736 | if (ret == 0) |
| 5737 | goto out; |
| 5738 | ret = 0; |
| 5739 | |
| 5740 | /* |
| 5741 | * MAGIC NUMBER EXPLANATION: |
| 5742 | * since we search a directory based on f_pos we have to start at 2 |
| 5743 | * since '.' and '..' have f_pos of 0 and 1 respectively, so everybody |
| 5744 | * else has to start at 2 |
| 5745 | */ |
| 5746 | if (path->slots[0] == 0) { |
| 5747 | BTRFS_I(inode)->index_cnt = 2; |
| 5748 | goto out; |
| 5749 | } |
| 5750 | |
| 5751 | path->slots[0]--; |
| 5752 | |
| 5753 | leaf = path->nodes[0]; |
| 5754 | btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]); |
| 5755 | |
Li Zefan | 33345d01 | 2011-04-20 10:31:50 +0800 | [diff] [blame] | 5756 | if (found_key.objectid != btrfs_ino(inode) || |
David Sterba | 962a298 | 2014-06-04 18:41:45 +0200 | [diff] [blame] | 5757 | found_key.type != BTRFS_DIR_INDEX_KEY) { |
Josef Bacik | aec7477 | 2008-07-24 12:12:38 -0400 | [diff] [blame] | 5758 | BTRFS_I(inode)->index_cnt = 2; |
| 5759 | goto out; |
| 5760 | } |
| 5761 | |
| 5762 | BTRFS_I(inode)->index_cnt = found_key.offset + 1; |
| 5763 | out: |
| 5764 | btrfs_free_path(path); |
| 5765 | return ret; |
| 5766 | } |
| 5767 | |
Chris Mason | d352ac6 | 2008-09-29 15:18:18 -0400 | [diff] [blame] | 5768 | /* |
| 5769 | * helper to find a free sequence number in a given directory. This current |
| 5770 | * code is very simple, later versions will do smarter things in the btree |
| 5771 | */ |
Chris Mason | 3de4586 | 2008-11-17 21:02:50 -0500 | [diff] [blame] | 5772 | int btrfs_set_inode_index(struct inode *dir, u64 *index) |
Josef Bacik | aec7477 | 2008-07-24 12:12:38 -0400 | [diff] [blame] | 5773 | { |
| 5774 | int ret = 0; |
| 5775 | |
| 5776 | if (BTRFS_I(dir)->index_cnt == (u64)-1) { |
Miao Xie | 16cdcec | 2011-04-22 18:12:22 +0800 | [diff] [blame] | 5777 | ret = btrfs_inode_delayed_dir_index_count(dir); |
| 5778 | if (ret) { |
| 5779 | ret = btrfs_set_inode_index_count(dir); |
| 5780 | if (ret) |
| 5781 | return ret; |
| 5782 | } |
Josef Bacik | aec7477 | 2008-07-24 12:12:38 -0400 | [diff] [blame] | 5783 | } |
| 5784 | |
Chris Mason | 00e4e6b | 2008-08-05 11:18:09 -0400 | [diff] [blame] | 5785 | *index = BTRFS_I(dir)->index_cnt; |
Josef Bacik | aec7477 | 2008-07-24 12:12:38 -0400 | [diff] [blame] | 5786 | BTRFS_I(dir)->index_cnt++; |
| 5787 | |
| 5788 | return ret; |
| 5789 | } |
| 5790 | |
Chris Mason | b0d5d10 | 2014-09-08 13:08:51 -0700 | [diff] [blame] | 5791 | static int btrfs_insert_inode_locked(struct inode *inode) |
| 5792 | { |
| 5793 | struct btrfs_iget_args args; |
| 5794 | args.location = &BTRFS_I(inode)->location; |
| 5795 | args.root = BTRFS_I(inode)->root; |
| 5796 | |
| 5797 | return insert_inode_locked4(inode, |
| 5798 | btrfs_inode_hash(inode->i_ino, BTRFS_I(inode)->root), |
| 5799 | btrfs_find_actor, &args); |
| 5800 | } |
| 5801 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5802 | static struct inode *btrfs_new_inode(struct btrfs_trans_handle *trans, |
| 5803 | struct btrfs_root *root, |
Josef Bacik | aec7477 | 2008-07-24 12:12:38 -0400 | [diff] [blame] | 5804 | struct inode *dir, |
Chris Mason | 9c58309 | 2008-01-29 15:15:18 -0500 | [diff] [blame] | 5805 | const char *name, int name_len, |
Al Viro | 175a4eb | 2011-07-26 03:30:54 -0400 | [diff] [blame] | 5806 | u64 ref_objectid, u64 objectid, |
| 5807 | umode_t mode, u64 *index) |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5808 | { |
| 5809 | struct inode *inode; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 5810 | struct btrfs_inode_item *inode_item; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5811 | struct btrfs_key *location; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 5812 | struct btrfs_path *path; |
Chris Mason | 9c58309 | 2008-01-29 15:15:18 -0500 | [diff] [blame] | 5813 | struct btrfs_inode_ref *ref; |
| 5814 | struct btrfs_key key[2]; |
| 5815 | u32 sizes[2]; |
Filipe Manana | ef3b9af | 2014-04-27 20:40:45 +0100 | [diff] [blame] | 5816 | int nitems = name ? 2 : 1; |
Chris Mason | 9c58309 | 2008-01-29 15:15:18 -0500 | [diff] [blame] | 5817 | unsigned long ptr; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5818 | int ret; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5819 | |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 5820 | path = btrfs_alloc_path(); |
Mark Fasheh | d8926bb | 2011-07-13 10:38:47 -0700 | [diff] [blame] | 5821 | if (!path) |
| 5822 | return ERR_PTR(-ENOMEM); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 5823 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5824 | inode = new_inode(root->fs_info->sb); |
Yoshinori Sano | 8fb2764 | 2011-04-09 02:30:07 +0000 | [diff] [blame] | 5825 | if (!inode) { |
| 5826 | btrfs_free_path(path); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5827 | return ERR_PTR(-ENOMEM); |
Yoshinori Sano | 8fb2764 | 2011-04-09 02:30:07 +0000 | [diff] [blame] | 5828 | } |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5829 | |
Li Zefan | 581bb05 | 2011-04-20 10:06:11 +0800 | [diff] [blame] | 5830 | /* |
Filipe Manana | 5762b5c | 2014-08-01 00:10:32 +0100 | [diff] [blame] | 5831 | * O_TMPFILE, set link count to 0, so that after this point, |
| 5832 | * we fill in an inode item with the correct link count. |
| 5833 | */ |
| 5834 | if (!name) |
| 5835 | set_nlink(inode, 0); |
| 5836 | |
| 5837 | /* |
Li Zefan | 581bb05 | 2011-04-20 10:06:11 +0800 | [diff] [blame] | 5838 | * we have to initialize this early, so we can reclaim the inode |
| 5839 | * number if we fail afterwards in this function. |
| 5840 | */ |
| 5841 | inode->i_ino = objectid; |
| 5842 | |
Filipe Manana | ef3b9af | 2014-04-27 20:40:45 +0100 | [diff] [blame] | 5843 | if (dir && name) { |
liubo | 1abe9b8 | 2011-03-24 11:18:59 +0000 | [diff] [blame] | 5844 | trace_btrfs_inode_request(dir); |
| 5845 | |
Chris Mason | 3de4586 | 2008-11-17 21:02:50 -0500 | [diff] [blame] | 5846 | ret = btrfs_set_inode_index(dir, index); |
Shen Feng | 0977143 | 2009-04-02 16:46:06 -0400 | [diff] [blame] | 5847 | if (ret) { |
Yoshinori Sano | 8fb2764 | 2011-04-09 02:30:07 +0000 | [diff] [blame] | 5848 | btrfs_free_path(path); |
Shen Feng | 0977143 | 2009-04-02 16:46:06 -0400 | [diff] [blame] | 5849 | iput(inode); |
Josef Bacik | aec7477 | 2008-07-24 12:12:38 -0400 | [diff] [blame] | 5850 | return ERR_PTR(ret); |
Shen Feng | 0977143 | 2009-04-02 16:46:06 -0400 | [diff] [blame] | 5851 | } |
Filipe Manana | ef3b9af | 2014-04-27 20:40:45 +0100 | [diff] [blame] | 5852 | } else if (dir) { |
| 5853 | *index = 0; |
Josef Bacik | aec7477 | 2008-07-24 12:12:38 -0400 | [diff] [blame] | 5854 | } |
| 5855 | /* |
| 5856 | * index_cnt is ignored for everything but a dir, |
| 5857 | * btrfs_get_inode_index_count has an explanation for the magic |
| 5858 | * number |
| 5859 | */ |
| 5860 | BTRFS_I(inode)->index_cnt = 2; |
Miao Xie | 67de117 | 2013-12-26 13:07:06 +0800 | [diff] [blame] | 5861 | BTRFS_I(inode)->dir_index = *index; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5862 | BTRFS_I(inode)->root = root; |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 5863 | BTRFS_I(inode)->generation = trans->transid; |
Josef Bacik | 7619585 | 2010-11-19 02:18:02 +0000 | [diff] [blame] | 5864 | inode->i_generation = BTRFS_I(inode)->generation; |
Chris Mason | b888db2 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 5865 | |
Josef Bacik | 5dc562c | 2012-08-17 13:14:17 -0400 | [diff] [blame] | 5866 | /* |
| 5867 | * We could have gotten an inode number from somebody who was fsynced |
| 5868 | * and then removed in this same transaction, so let's just set full |
| 5869 | * sync since it will be a full sync anyway and this will blow away the |
| 5870 | * old info in the log. |
| 5871 | */ |
| 5872 | set_bit(BTRFS_INODE_NEEDS_FULL_SYNC, &BTRFS_I(inode)->runtime_flags); |
| 5873 | |
Chris Mason | 9c58309 | 2008-01-29 15:15:18 -0500 | [diff] [blame] | 5874 | key[0].objectid = objectid; |
David Sterba | 962a298 | 2014-06-04 18:41:45 +0200 | [diff] [blame] | 5875 | key[0].type = BTRFS_INODE_ITEM_KEY; |
Chris Mason | 9c58309 | 2008-01-29 15:15:18 -0500 | [diff] [blame] | 5876 | key[0].offset = 0; |
| 5877 | |
Chris Mason | 9c58309 | 2008-01-29 15:15:18 -0500 | [diff] [blame] | 5878 | sizes[0] = sizeof(struct btrfs_inode_item); |
Filipe Manana | ef3b9af | 2014-04-27 20:40:45 +0100 | [diff] [blame] | 5879 | |
| 5880 | if (name) { |
| 5881 | /* |
| 5882 | * Start new inodes with an inode_ref. This is slightly more |
| 5883 | * efficient for small numbers of hard links since they will |
| 5884 | * be packed into one item. Extended refs will kick in if we |
| 5885 | * add more hard links than can fit in the ref item. |
| 5886 | */ |
| 5887 | key[1].objectid = objectid; |
David Sterba | 962a298 | 2014-06-04 18:41:45 +0200 | [diff] [blame] | 5888 | key[1].type = BTRFS_INODE_REF_KEY; |
Filipe Manana | ef3b9af | 2014-04-27 20:40:45 +0100 | [diff] [blame] | 5889 | key[1].offset = ref_objectid; |
| 5890 | |
| 5891 | sizes[1] = name_len + sizeof(*ref); |
| 5892 | } |
Chris Mason | 9c58309 | 2008-01-29 15:15:18 -0500 | [diff] [blame] | 5893 | |
Chris Mason | b0d5d10 | 2014-09-08 13:08:51 -0700 | [diff] [blame] | 5894 | location = &BTRFS_I(inode)->location; |
| 5895 | location->objectid = objectid; |
| 5896 | location->offset = 0; |
David Sterba | 962a298 | 2014-06-04 18:41:45 +0200 | [diff] [blame] | 5897 | location->type = BTRFS_INODE_ITEM_KEY; |
Chris Mason | b0d5d10 | 2014-09-08 13:08:51 -0700 | [diff] [blame] | 5898 | |
| 5899 | ret = btrfs_insert_inode_locked(inode); |
| 5900 | if (ret < 0) |
| 5901 | goto fail; |
| 5902 | |
Chris Mason | b947343 | 2009-03-13 11:00:37 -0400 | [diff] [blame] | 5903 | path->leave_spinning = 1; |
Filipe Manana | ef3b9af | 2014-04-27 20:40:45 +0100 | [diff] [blame] | 5904 | ret = btrfs_insert_empty_items(trans, root, path, key, sizes, nitems); |
Chris Mason | 9c58309 | 2008-01-29 15:15:18 -0500 | [diff] [blame] | 5905 | if (ret != 0) |
Chris Mason | b0d5d10 | 2014-09-08 13:08:51 -0700 | [diff] [blame] | 5906 | goto fail_unlock; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 5907 | |
Dmitry Monakhov | ecc11fab | 2010-03-04 17:31:47 +0300 | [diff] [blame] | 5908 | inode_init_owner(inode, dir, mode); |
Yan Zheng | a76a3cd | 2008-10-09 11:46:29 -0400 | [diff] [blame] | 5909 | inode_set_bytes(inode, 0); |
chandan r | 9cc97d6 | 2012-07-04 12:48:07 +0530 | [diff] [blame] | 5910 | |
| 5911 | inode->i_mtime = CURRENT_TIME; |
| 5912 | inode->i_atime = inode->i_mtime; |
| 5913 | inode->i_ctime = inode->i_mtime; |
| 5914 | BTRFS_I(inode)->i_otime = inode->i_mtime; |
| 5915 | |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 5916 | inode_item = btrfs_item_ptr(path->nodes[0], path->slots[0], |
| 5917 | struct btrfs_inode_item); |
Li Zefan | 293f7e0 | 2012-07-10 00:58:58 -0600 | [diff] [blame] | 5918 | memset_extent_buffer(path->nodes[0], 0, (unsigned long)inode_item, |
| 5919 | sizeof(*inode_item)); |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 5920 | fill_inode_item(trans, path->nodes[0], inode_item, inode); |
Chris Mason | 9c58309 | 2008-01-29 15:15:18 -0500 | [diff] [blame] | 5921 | |
Filipe Manana | ef3b9af | 2014-04-27 20:40:45 +0100 | [diff] [blame] | 5922 | if (name) { |
| 5923 | ref = btrfs_item_ptr(path->nodes[0], path->slots[0] + 1, |
| 5924 | struct btrfs_inode_ref); |
| 5925 | btrfs_set_inode_ref_name_len(path->nodes[0], ref, name_len); |
| 5926 | btrfs_set_inode_ref_index(path->nodes[0], ref, *index); |
| 5927 | ptr = (unsigned long)(ref + 1); |
| 5928 | write_extent_buffer(path->nodes[0], name, ptr, name_len); |
| 5929 | } |
Chris Mason | 9c58309 | 2008-01-29 15:15:18 -0500 | [diff] [blame] | 5930 | |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 5931 | btrfs_mark_buffer_dirty(path->nodes[0]); |
| 5932 | btrfs_free_path(path); |
| 5933 | |
Christoph Hellwig | 6cbff00 | 2009-04-17 10:37:41 +0200 | [diff] [blame] | 5934 | btrfs_inherit_iflags(inode, dir); |
| 5935 | |
Al Viro | 569254b | 2011-07-24 17:08:40 -0400 | [diff] [blame] | 5936 | if (S_ISREG(mode)) { |
Chris Mason | 9427216 | 2009-07-02 12:26:06 -0400 | [diff] [blame] | 5937 | if (btrfs_test_opt(root, NODATASUM)) |
| 5938 | BTRFS_I(inode)->flags |= BTRFS_INODE_NODATASUM; |
Liu Bo | 213490b | 2012-09-11 08:33:50 -0600 | [diff] [blame] | 5939 | if (btrfs_test_opt(root, NODATACOW)) |
Josef Bacik | f2bdf9a | 2013-02-21 15:28:28 -0500 | [diff] [blame] | 5940 | BTRFS_I(inode)->flags |= BTRFS_INODE_NODATACOW | |
| 5941 | BTRFS_INODE_NODATASUM; |
Chris Mason | 9427216 | 2009-07-02 12:26:06 -0400 | [diff] [blame] | 5942 | } |
| 5943 | |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 5944 | inode_tree_add(inode); |
liubo | 1abe9b8 | 2011-03-24 11:18:59 +0000 | [diff] [blame] | 5945 | |
| 5946 | trace_btrfs_inode_new(inode); |
Chris Mason | 1973f0f | 2011-06-24 13:13:29 -0400 | [diff] [blame] | 5947 | btrfs_set_inode_last_trans(trans, inode); |
liubo | 1abe9b8 | 2011-03-24 11:18:59 +0000 | [diff] [blame] | 5948 | |
Alexander Block | 8ea05e3 | 2012-07-25 17:35:53 +0200 | [diff] [blame] | 5949 | btrfs_update_root_times(trans, root); |
| 5950 | |
Filipe David Borba Manana | 6354192 | 2014-01-07 11:47:46 +0000 | [diff] [blame] | 5951 | ret = btrfs_inode_inherit_props(trans, inode, dir); |
| 5952 | if (ret) |
| 5953 | btrfs_err(root->fs_info, |
| 5954 | "error inheriting props for ino %llu (root %llu): %d", |
| 5955 | btrfs_ino(inode), root->root_key.objectid, ret); |
| 5956 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5957 | return inode; |
Chris Mason | b0d5d10 | 2014-09-08 13:08:51 -0700 | [diff] [blame] | 5958 | |
| 5959 | fail_unlock: |
| 5960 | unlock_new_inode(inode); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 5961 | fail: |
Filipe Manana | ef3b9af | 2014-04-27 20:40:45 +0100 | [diff] [blame] | 5962 | if (dir && name) |
Josef Bacik | aec7477 | 2008-07-24 12:12:38 -0400 | [diff] [blame] | 5963 | BTRFS_I(dir)->index_cnt--; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 5964 | btrfs_free_path(path); |
Shen Feng | 0977143 | 2009-04-02 16:46:06 -0400 | [diff] [blame] | 5965 | iput(inode); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 5966 | return ERR_PTR(ret); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5967 | } |
| 5968 | |
| 5969 | static inline u8 btrfs_inode_type(struct inode *inode) |
| 5970 | { |
| 5971 | return btrfs_type_by_mode[(inode->i_mode & S_IFMT) >> S_SHIFT]; |
| 5972 | } |
| 5973 | |
Chris Mason | d352ac6 | 2008-09-29 15:18:18 -0400 | [diff] [blame] | 5974 | /* |
| 5975 | * utility function to add 'inode' into 'parent_inode' with |
| 5976 | * a give name and a given sequence number. |
| 5977 | * if 'add_backref' is true, also insert a backref from the |
| 5978 | * inode to the parent directory. |
| 5979 | */ |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 5980 | int btrfs_add_link(struct btrfs_trans_handle *trans, |
| 5981 | struct inode *parent_inode, struct inode *inode, |
| 5982 | const char *name, int name_len, int add_backref, u64 index) |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5983 | { |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 5984 | int ret = 0; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5985 | struct btrfs_key key; |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 5986 | struct btrfs_root *root = BTRFS_I(parent_inode)->root; |
Li Zefan | 33345d01 | 2011-04-20 10:31:50 +0800 | [diff] [blame] | 5987 | u64 ino = btrfs_ino(inode); |
| 5988 | u64 parent_ino = btrfs_ino(parent_inode); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 5989 | |
Li Zefan | 33345d01 | 2011-04-20 10:31:50 +0800 | [diff] [blame] | 5990 | if (unlikely(ino == BTRFS_FIRST_FREE_OBJECTID)) { |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 5991 | memcpy(&key, &BTRFS_I(inode)->root->root_key, sizeof(key)); |
| 5992 | } else { |
Li Zefan | 33345d01 | 2011-04-20 10:31:50 +0800 | [diff] [blame] | 5993 | key.objectid = ino; |
David Sterba | 962a298 | 2014-06-04 18:41:45 +0200 | [diff] [blame] | 5994 | key.type = BTRFS_INODE_ITEM_KEY; |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 5995 | key.offset = 0; |
| 5996 | } |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5997 | |
Li Zefan | 33345d01 | 2011-04-20 10:31:50 +0800 | [diff] [blame] | 5998 | if (unlikely(ino == BTRFS_FIRST_FREE_OBJECTID)) { |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 5999 | ret = btrfs_add_root_ref(trans, root->fs_info->tree_root, |
| 6000 | key.objectid, root->root_key.objectid, |
Li Zefan | 33345d01 | 2011-04-20 10:31:50 +0800 | [diff] [blame] | 6001 | parent_ino, index, name, name_len); |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 6002 | } else if (add_backref) { |
Li Zefan | 33345d01 | 2011-04-20 10:31:50 +0800 | [diff] [blame] | 6003 | ret = btrfs_insert_inode_ref(trans, root, name, name_len, ino, |
| 6004 | parent_ino, index); |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 6005 | } |
| 6006 | |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 6007 | /* Nothing to clean up yet */ |
| 6008 | if (ret) |
| 6009 | return ret; |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 6010 | |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 6011 | ret = btrfs_insert_dir_item(trans, root, name, name_len, |
| 6012 | parent_inode, &key, |
| 6013 | btrfs_inode_type(inode), index); |
Chris Mason | 9c52057 | 2012-12-17 14:26:57 -0500 | [diff] [blame] | 6014 | if (ret == -EEXIST || ret == -EOVERFLOW) |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 6015 | goto fail_dir_item; |
| 6016 | else if (ret) { |
| 6017 | btrfs_abort_transaction(trans, root, ret); |
| 6018 | return ret; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 6019 | } |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 6020 | |
| 6021 | btrfs_i_size_write(parent_inode, parent_inode->i_size + |
| 6022 | name_len * 2); |
Josef Bacik | 0c4d2d9 | 2012-04-05 15:03:02 -0400 | [diff] [blame] | 6023 | inode_inc_iversion(parent_inode); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 6024 | parent_inode->i_mtime = parent_inode->i_ctime = CURRENT_TIME; |
| 6025 | ret = btrfs_update_inode(trans, root, parent_inode); |
| 6026 | if (ret) |
| 6027 | btrfs_abort_transaction(trans, root, ret); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 6028 | return ret; |
Chris Mason | fe66a05 | 2012-02-20 08:40:56 -0500 | [diff] [blame] | 6029 | |
| 6030 | fail_dir_item: |
| 6031 | if (unlikely(ino == BTRFS_FIRST_FREE_OBJECTID)) { |
| 6032 | u64 local_index; |
| 6033 | int err; |
| 6034 | err = btrfs_del_root_ref(trans, root->fs_info->tree_root, |
| 6035 | key.objectid, root->root_key.objectid, |
| 6036 | parent_ino, &local_index, name, name_len); |
| 6037 | |
| 6038 | } else if (add_backref) { |
| 6039 | u64 local_index; |
| 6040 | int err; |
| 6041 | |
| 6042 | err = btrfs_del_inode_ref(trans, root, name, name_len, |
| 6043 | ino, parent_ino, &local_index); |
| 6044 | } |
| 6045 | return ret; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 6046 | } |
| 6047 | |
| 6048 | static int btrfs_add_nondir(struct btrfs_trans_handle *trans, |
Josef Bacik | a1b075d | 2010-11-19 20:36:11 +0000 | [diff] [blame] | 6049 | struct inode *dir, struct dentry *dentry, |
| 6050 | struct inode *inode, int backref, u64 index) |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 6051 | { |
Josef Bacik | a1b075d | 2010-11-19 20:36:11 +0000 | [diff] [blame] | 6052 | int err = btrfs_add_link(trans, dir, inode, |
| 6053 | dentry->d_name.name, dentry->d_name.len, |
| 6054 | backref, index); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 6055 | if (err > 0) |
| 6056 | err = -EEXIST; |
| 6057 | return err; |
| 6058 | } |
| 6059 | |
Josef Bacik | 618e21d | 2007-07-11 10:18:17 -0400 | [diff] [blame] | 6060 | static int btrfs_mknod(struct inode *dir, struct dentry *dentry, |
Al Viro | 1a67aaf | 2011-07-26 01:52:52 -0400 | [diff] [blame] | 6061 | umode_t mode, dev_t rdev) |
Josef Bacik | 618e21d | 2007-07-11 10:18:17 -0400 | [diff] [blame] | 6062 | { |
| 6063 | struct btrfs_trans_handle *trans; |
| 6064 | struct btrfs_root *root = BTRFS_I(dir)->root; |
Chris Mason | 1832a6d | 2007-12-21 16:27:21 -0500 | [diff] [blame] | 6065 | struct inode *inode = NULL; |
Josef Bacik | 618e21d | 2007-07-11 10:18:17 -0400 | [diff] [blame] | 6066 | int err; |
| 6067 | int drop_inode = 0; |
| 6068 | u64 objectid; |
Chris Mason | 00e4e6b | 2008-08-05 11:18:09 -0400 | [diff] [blame] | 6069 | u64 index = 0; |
Josef Bacik | 618e21d | 2007-07-11 10:18:17 -0400 | [diff] [blame] | 6070 | |
| 6071 | if (!new_valid_dev(rdev)) |
| 6072 | return -EINVAL; |
| 6073 | |
Josef Bacik | 9ed74f2 | 2009-09-11 16:12:44 -0400 | [diff] [blame] | 6074 | /* |
| 6075 | * 2 for inode item and ref |
| 6076 | * 2 for dir items |
| 6077 | * 1 for xattr if selinux is on |
| 6078 | */ |
Yan, Zheng | a22285a | 2010-05-16 10:48:46 -0400 | [diff] [blame] | 6079 | trans = btrfs_start_transaction(root, 5); |
| 6080 | if (IS_ERR(trans)) |
| 6081 | return PTR_ERR(trans); |
Chris Mason | 1832a6d | 2007-12-21 16:27:21 -0500 | [diff] [blame] | 6082 | |
Li Zefan | 581bb05 | 2011-04-20 10:06:11 +0800 | [diff] [blame] | 6083 | err = btrfs_find_free_ino(root, &objectid); |
| 6084 | if (err) |
| 6085 | goto out_unlock; |
| 6086 | |
Josef Bacik | aec7477 | 2008-07-24 12:12:38 -0400 | [diff] [blame] | 6087 | inode = btrfs_new_inode(trans, root, dir, dentry->d_name.name, |
Li Zefan | 33345d01 | 2011-04-20 10:31:50 +0800 | [diff] [blame] | 6088 | dentry->d_name.len, btrfs_ino(dir), objectid, |
Josef Bacik | d82a6f1d | 2011-05-11 15:26:06 -0400 | [diff] [blame] | 6089 | mode, &index); |
Tsutomu Itoh | 7cf96da | 2011-04-25 19:43:53 -0400 | [diff] [blame] | 6090 | if (IS_ERR(inode)) { |
| 6091 | err = PTR_ERR(inode); |
Josef Bacik | 618e21d | 2007-07-11 10:18:17 -0400 | [diff] [blame] | 6092 | goto out_unlock; |
Tsutomu Itoh | 7cf96da | 2011-04-25 19:43:53 -0400 | [diff] [blame] | 6093 | } |
Josef Bacik | 618e21d | 2007-07-11 10:18:17 -0400 | [diff] [blame] | 6094 | |
Casey Schaufler | ad19db7 | 2011-12-15 10:09:07 -0500 | [diff] [blame] | 6095 | /* |
| 6096 | * If the active LSM wants to access the inode during |
| 6097 | * d_instantiate it needs these. Smack checks to see |
| 6098 | * if the filesystem supports xattrs by looking at the |
| 6099 | * ops vector. |
| 6100 | */ |
Casey Schaufler | ad19db7 | 2011-12-15 10:09:07 -0500 | [diff] [blame] | 6101 | inode->i_op = &btrfs_special_inode_operations; |
Chris Mason | b0d5d10 | 2014-09-08 13:08:51 -0700 | [diff] [blame] | 6102 | init_special_inode(inode, inode->i_mode, rdev); |
| 6103 | |
| 6104 | err = btrfs_init_inode_security(trans, inode, dir, &dentry->d_name); |
Josef Bacik | 618e21d | 2007-07-11 10:18:17 -0400 | [diff] [blame] | 6105 | if (err) |
Chris Mason | b0d5d10 | 2014-09-08 13:08:51 -0700 | [diff] [blame] | 6106 | goto out_unlock_inode; |
| 6107 | |
| 6108 | err = btrfs_add_nondir(trans, dir, dentry, inode, 0, index); |
| 6109 | if (err) { |
| 6110 | goto out_unlock_inode; |
| 6111 | } else { |
Yan | 1b4ab1b | 2007-08-29 09:11:44 -0400 | [diff] [blame] | 6112 | btrfs_update_inode(trans, root, inode); |
Chris Mason | b0d5d10 | 2014-09-08 13:08:51 -0700 | [diff] [blame] | 6113 | unlock_new_inode(inode); |
Al Viro | 08c422c | 2011-12-23 07:58:13 -0500 | [diff] [blame] | 6114 | d_instantiate(dentry, inode); |
Josef Bacik | 618e21d | 2007-07-11 10:18:17 -0400 | [diff] [blame] | 6115 | } |
Chris Mason | b0d5d10 | 2014-09-08 13:08:51 -0700 | [diff] [blame] | 6116 | |
Josef Bacik | 618e21d | 2007-07-11 10:18:17 -0400 | [diff] [blame] | 6117 | out_unlock: |
Josef Bacik | 7ad85bb | 2012-01-12 19:10:12 -0500 | [diff] [blame] | 6118 | btrfs_end_transaction(trans, root); |
Josef Bacik | c581afc | 2014-02-06 16:06:06 -0500 | [diff] [blame] | 6119 | btrfs_balance_delayed_items(root); |
Liu Bo | b53d3f5 | 2012-11-14 14:34:34 +0000 | [diff] [blame] | 6120 | btrfs_btree_balance_dirty(root); |
Josef Bacik | 618e21d | 2007-07-11 10:18:17 -0400 | [diff] [blame] | 6121 | if (drop_inode) { |
| 6122 | inode_dec_link_count(inode); |
| 6123 | iput(inode); |
| 6124 | } |
Josef Bacik | 618e21d | 2007-07-11 10:18:17 -0400 | [diff] [blame] | 6125 | return err; |
Chris Mason | b0d5d10 | 2014-09-08 13:08:51 -0700 | [diff] [blame] | 6126 | |
| 6127 | out_unlock_inode: |
| 6128 | drop_inode = 1; |
| 6129 | unlock_new_inode(inode); |
| 6130 | goto out_unlock; |
| 6131 | |
Josef Bacik | 618e21d | 2007-07-11 10:18:17 -0400 | [diff] [blame] | 6132 | } |
| 6133 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 6134 | static int btrfs_create(struct inode *dir, struct dentry *dentry, |
Al Viro | ebfc3b4 | 2012-06-10 18:05:36 -0400 | [diff] [blame] | 6135 | umode_t mode, bool excl) |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 6136 | { |
| 6137 | struct btrfs_trans_handle *trans; |
| 6138 | struct btrfs_root *root = BTRFS_I(dir)->root; |
Chris Mason | 1832a6d | 2007-12-21 16:27:21 -0500 | [diff] [blame] | 6139 | struct inode *inode = NULL; |
Filipe Brandenburger | 43baa57 | 2012-11-30 03:40:09 +0000 | [diff] [blame] | 6140 | int drop_inode_on_err = 0; |
Yan, Zheng | a22285a | 2010-05-16 10:48:46 -0400 | [diff] [blame] | 6141 | int err; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 6142 | u64 objectid; |
Chris Mason | 00e4e6b | 2008-08-05 11:18:09 -0400 | [diff] [blame] | 6143 | u64 index = 0; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 6144 | |
Josef Bacik | 9ed74f2 | 2009-09-11 16:12:44 -0400 | [diff] [blame] | 6145 | /* |
| 6146 | * 2 for inode item and ref |
| 6147 | * 2 for dir items |
| 6148 | * 1 for xattr if selinux is on |
| 6149 | */ |
Yan, Zheng | a22285a | 2010-05-16 10:48:46 -0400 | [diff] [blame] | 6150 | trans = btrfs_start_transaction(root, 5); |
| 6151 | if (IS_ERR(trans)) |
| 6152 | return PTR_ERR(trans); |
Josef Bacik | 9ed74f2 | 2009-09-11 16:12:44 -0400 | [diff] [blame] | 6153 | |
Li Zefan | 581bb05 | 2011-04-20 10:06:11 +0800 | [diff] [blame] | 6154 | err = btrfs_find_free_ino(root, &objectid); |
| 6155 | if (err) |
| 6156 | goto out_unlock; |
| 6157 | |
Josef Bacik | aec7477 | 2008-07-24 12:12:38 -0400 | [diff] [blame] | 6158 | inode = btrfs_new_inode(trans, root, dir, dentry->d_name.name, |
Li Zefan | 33345d01 | 2011-04-20 10:31:50 +0800 | [diff] [blame] | 6159 | dentry->d_name.len, btrfs_ino(dir), objectid, |
Josef Bacik | d82a6f1d | 2011-05-11 15:26:06 -0400 | [diff] [blame] | 6160 | mode, &index); |
Tsutomu Itoh | 7cf96da | 2011-04-25 19:43:53 -0400 | [diff] [blame] | 6161 | if (IS_ERR(inode)) { |
| 6162 | err = PTR_ERR(inode); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 6163 | goto out_unlock; |
Tsutomu Itoh | 7cf96da | 2011-04-25 19:43:53 -0400 | [diff] [blame] | 6164 | } |
Filipe Brandenburger | 43baa57 | 2012-11-30 03:40:09 +0000 | [diff] [blame] | 6165 | drop_inode_on_err = 1; |
Casey Schaufler | ad19db7 | 2011-12-15 10:09:07 -0500 | [diff] [blame] | 6166 | /* |
| 6167 | * If the active LSM wants to access the inode during |
| 6168 | * d_instantiate it needs these. Smack checks to see |
| 6169 | * if the filesystem supports xattrs by looking at the |
| 6170 | * ops vector. |
| 6171 | */ |
| 6172 | inode->i_fop = &btrfs_file_operations; |
| 6173 | inode->i_op = &btrfs_file_inode_operations; |
Chris Mason | b0d5d10 | 2014-09-08 13:08:51 -0700 | [diff] [blame] | 6174 | inode->i_mapping->a_ops = &btrfs_aops; |
| 6175 | inode->i_mapping->backing_dev_info = &root->fs_info->bdi; |
| 6176 | |
| 6177 | err = btrfs_init_inode_security(trans, inode, dir, &dentry->d_name); |
| 6178 | if (err) |
| 6179 | goto out_unlock_inode; |
| 6180 | |
| 6181 | err = btrfs_update_inode(trans, root, inode); |
| 6182 | if (err) |
| 6183 | goto out_unlock_inode; |
Casey Schaufler | ad19db7 | 2011-12-15 10:09:07 -0500 | [diff] [blame] | 6184 | |
Josef Bacik | a1b075d | 2010-11-19 20:36:11 +0000 | [diff] [blame] | 6185 | err = btrfs_add_nondir(trans, dir, dentry, inode, 0, index); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 6186 | if (err) |
Chris Mason | b0d5d10 | 2014-09-08 13:08:51 -0700 | [diff] [blame] | 6187 | goto out_unlock_inode; |
Filipe Brandenburger | 43baa57 | 2012-11-30 03:40:09 +0000 | [diff] [blame] | 6188 | |
Filipe Brandenburger | 43baa57 | 2012-11-30 03:40:09 +0000 | [diff] [blame] | 6189 | BTRFS_I(inode)->io_tree.ops = &btrfs_extent_io_ops; |
Chris Mason | b0d5d10 | 2014-09-08 13:08:51 -0700 | [diff] [blame] | 6190 | unlock_new_inode(inode); |
Filipe Brandenburger | 43baa57 | 2012-11-30 03:40:09 +0000 | [diff] [blame] | 6191 | d_instantiate(dentry, inode); |
| 6192 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 6193 | out_unlock: |
Josef Bacik | 7ad85bb | 2012-01-12 19:10:12 -0500 | [diff] [blame] | 6194 | btrfs_end_transaction(trans, root); |
Filipe Brandenburger | 43baa57 | 2012-11-30 03:40:09 +0000 | [diff] [blame] | 6195 | if (err && drop_inode_on_err) { |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 6196 | inode_dec_link_count(inode); |
| 6197 | iput(inode); |
| 6198 | } |
Josef Bacik | c581afc | 2014-02-06 16:06:06 -0500 | [diff] [blame] | 6199 | btrfs_balance_delayed_items(root); |
Liu Bo | b53d3f5 | 2012-11-14 14:34:34 +0000 | [diff] [blame] | 6200 | btrfs_btree_balance_dirty(root); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 6201 | return err; |
Chris Mason | b0d5d10 | 2014-09-08 13:08:51 -0700 | [diff] [blame] | 6202 | |
| 6203 | out_unlock_inode: |
| 6204 | unlock_new_inode(inode); |
| 6205 | goto out_unlock; |
| 6206 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 6207 | } |
| 6208 | |
| 6209 | static int btrfs_link(struct dentry *old_dentry, struct inode *dir, |
| 6210 | struct dentry *dentry) |
| 6211 | { |
| 6212 | struct btrfs_trans_handle *trans; |
| 6213 | struct btrfs_root *root = BTRFS_I(dir)->root; |
| 6214 | struct inode *inode = old_dentry->d_inode; |
Chris Mason | 00e4e6b | 2008-08-05 11:18:09 -0400 | [diff] [blame] | 6215 | u64 index; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 6216 | int err; |
| 6217 | int drop_inode = 0; |
| 6218 | |
TARUISI Hiroaki | 4a8be42 | 2009-11-12 07:14:26 +0000 | [diff] [blame] | 6219 | /* do not allow sys_link's with other subvols of the same device */ |
| 6220 | if (root->objectid != BTRFS_I(inode)->root->objectid) |
Mark Fasheh | 3ab3564 | 2011-03-22 17:20:26 +0000 | [diff] [blame] | 6221 | return -EXDEV; |
TARUISI Hiroaki | 4a8be42 | 2009-11-12 07:14:26 +0000 | [diff] [blame] | 6222 | |
Mark Fasheh | f186373 | 2012-08-08 11:32:27 -0700 | [diff] [blame] | 6223 | if (inode->i_nlink >= BTRFS_LINK_MAX) |
Al Viro | c055e99 | 2011-03-04 17:15:18 +0000 | [diff] [blame] | 6224 | return -EMLINK; |
Josef Bacik | 9ed74f2 | 2009-09-11 16:12:44 -0400 | [diff] [blame] | 6225 | |
Chris Mason | 3de4586 | 2008-11-17 21:02:50 -0500 | [diff] [blame] | 6226 | err = btrfs_set_inode_index(dir, &index); |
Josef Bacik | aec7477 | 2008-07-24 12:12:38 -0400 | [diff] [blame] | 6227 | if (err) |
| 6228 | goto fail; |
| 6229 | |
Yan, Zheng | a22285a | 2010-05-16 10:48:46 -0400 | [diff] [blame] | 6230 | /* |
Miao Xie | 7e6b646 | 2011-02-18 09:21:17 +0000 | [diff] [blame] | 6231 | * 2 items for inode and inode ref |
Yan, Zheng | a22285a | 2010-05-16 10:48:46 -0400 | [diff] [blame] | 6232 | * 2 items for dir items |
Miao Xie | 7e6b646 | 2011-02-18 09:21:17 +0000 | [diff] [blame] | 6233 | * 1 item for parent inode |
Yan, Zheng | a22285a | 2010-05-16 10:48:46 -0400 | [diff] [blame] | 6234 | */ |
Miao Xie | 7e6b646 | 2011-02-18 09:21:17 +0000 | [diff] [blame] | 6235 | trans = btrfs_start_transaction(root, 5); |
Yan, Zheng | a22285a | 2010-05-16 10:48:46 -0400 | [diff] [blame] | 6236 | if (IS_ERR(trans)) { |
| 6237 | err = PTR_ERR(trans); |
| 6238 | goto fail; |
| 6239 | } |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 6240 | |
Miao Xie | 67de117 | 2013-12-26 13:07:06 +0800 | [diff] [blame] | 6241 | /* There are several dir indexes for this inode, clear the cache. */ |
| 6242 | BTRFS_I(inode)->dir_index = 0ULL; |
Zach Brown | 8b558c5 | 2013-10-16 12:10:34 -0700 | [diff] [blame] | 6243 | inc_nlink(inode); |
Josef Bacik | 0c4d2d9 | 2012-04-05 15:03:02 -0400 | [diff] [blame] | 6244 | inode_inc_iversion(inode); |
Miao Xie | 3153495 | 2011-04-13 13:19:21 +0800 | [diff] [blame] | 6245 | inode->i_ctime = CURRENT_TIME; |
Al Viro | 7de9c6ee | 2010-10-23 11:11:40 -0400 | [diff] [blame] | 6246 | ihold(inode); |
Josef Bacik | e997615 | 2012-10-11 15:53:56 -0400 | [diff] [blame] | 6247 | set_bit(BTRFS_INODE_COPY_EVERYTHING, &BTRFS_I(inode)->runtime_flags); |
Josef Bacik | aec7477 | 2008-07-24 12:12:38 -0400 | [diff] [blame] | 6248 | |
Josef Bacik | a1b075d | 2010-11-19 20:36:11 +0000 | [diff] [blame] | 6249 | err = btrfs_add_nondir(trans, dir, dentry, inode, 1, index); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 6250 | |
Yan, Zheng | a571952 | 2009-09-24 09:17:31 -0400 | [diff] [blame] | 6251 | if (err) { |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 6252 | drop_inode = 1; |
Yan, Zheng | a571952 | 2009-09-24 09:17:31 -0400 | [diff] [blame] | 6253 | } else { |
Al Viro | 10d9f30 | 2011-07-16 23:09:10 -0400 | [diff] [blame] | 6254 | struct dentry *parent = dentry->d_parent; |
Yan, Zheng | a571952 | 2009-09-24 09:17:31 -0400 | [diff] [blame] | 6255 | err = btrfs_update_inode(trans, root, inode); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 6256 | if (err) |
| 6257 | goto fail; |
Filipe Manana | ef3b9af | 2014-04-27 20:40:45 +0100 | [diff] [blame] | 6258 | if (inode->i_nlink == 1) { |
| 6259 | /* |
| 6260 | * If new hard link count is 1, it's a file created |
| 6261 | * with open(2) O_TMPFILE flag. |
| 6262 | */ |
| 6263 | err = btrfs_orphan_del(trans, inode); |
| 6264 | if (err) |
| 6265 | goto fail; |
| 6266 | } |
Al Viro | 08c422c | 2011-12-23 07:58:13 -0500 | [diff] [blame] | 6267 | d_instantiate(dentry, inode); |
Josef Bacik | 6a91221 | 2010-11-20 09:48:00 +0000 | [diff] [blame] | 6268 | btrfs_log_new_name(trans, inode, NULL, parent); |
Yan, Zheng | a571952 | 2009-09-24 09:17:31 -0400 | [diff] [blame] | 6269 | } |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 6270 | |
Josef Bacik | 7ad85bb | 2012-01-12 19:10:12 -0500 | [diff] [blame] | 6271 | btrfs_end_transaction(trans, root); |
Josef Bacik | c581afc | 2014-02-06 16:06:06 -0500 | [diff] [blame] | 6272 | btrfs_balance_delayed_items(root); |
Chris Mason | 1832a6d | 2007-12-21 16:27:21 -0500 | [diff] [blame] | 6273 | fail: |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 6274 | if (drop_inode) { |
| 6275 | inode_dec_link_count(inode); |
| 6276 | iput(inode); |
| 6277 | } |
Liu Bo | b53d3f5 | 2012-11-14 14:34:34 +0000 | [diff] [blame] | 6278 | btrfs_btree_balance_dirty(root); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 6279 | return err; |
| 6280 | } |
| 6281 | |
Al Viro | 18bb1db | 2011-07-26 01:41:39 -0400 | [diff] [blame] | 6282 | 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] | 6283 | { |
Chris Mason | b9d8666 | 2008-05-02 16:13:49 -0400 | [diff] [blame] | 6284 | struct inode *inode = NULL; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 6285 | struct btrfs_trans_handle *trans; |
| 6286 | struct btrfs_root *root = BTRFS_I(dir)->root; |
| 6287 | int err = 0; |
| 6288 | int drop_on_err = 0; |
Chris Mason | b9d8666 | 2008-05-02 16:13:49 -0400 | [diff] [blame] | 6289 | u64 objectid = 0; |
Chris Mason | 00e4e6b | 2008-08-05 11:18:09 -0400 | [diff] [blame] | 6290 | u64 index = 0; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 6291 | |
Josef Bacik | 9ed74f2 | 2009-09-11 16:12:44 -0400 | [diff] [blame] | 6292 | /* |
| 6293 | * 2 items for inode and ref |
| 6294 | * 2 items for dir items |
| 6295 | * 1 for xattr if selinux is on |
| 6296 | */ |
Yan, Zheng | a22285a | 2010-05-16 10:48:46 -0400 | [diff] [blame] | 6297 | trans = btrfs_start_transaction(root, 5); |
| 6298 | if (IS_ERR(trans)) |
| 6299 | return PTR_ERR(trans); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 6300 | |
Li Zefan | 581bb05 | 2011-04-20 10:06:11 +0800 | [diff] [blame] | 6301 | err = btrfs_find_free_ino(root, &objectid); |
| 6302 | if (err) |
| 6303 | goto out_fail; |
| 6304 | |
Josef Bacik | aec7477 | 2008-07-24 12:12:38 -0400 | [diff] [blame] | 6305 | inode = btrfs_new_inode(trans, root, dir, dentry->d_name.name, |
Li Zefan | 33345d01 | 2011-04-20 10:31:50 +0800 | [diff] [blame] | 6306 | dentry->d_name.len, btrfs_ino(dir), objectid, |
Josef Bacik | d82a6f1d | 2011-05-11 15:26:06 -0400 | [diff] [blame] | 6307 | S_IFDIR | mode, &index); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 6308 | if (IS_ERR(inode)) { |
| 6309 | err = PTR_ERR(inode); |
| 6310 | goto out_fail; |
| 6311 | } |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 6312 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 6313 | drop_on_err = 1; |
Chris Mason | b0d5d10 | 2014-09-08 13:08:51 -0700 | [diff] [blame] | 6314 | /* these must be set before we unlock the inode */ |
| 6315 | inode->i_op = &btrfs_dir_inode_operations; |
| 6316 | inode->i_fop = &btrfs_dir_file_operations; |
Josef Bacik | 33268ea | 2008-07-24 12:16:36 -0400 | [diff] [blame] | 6317 | |
Eric Paris | 2a7dba3 | 2011-02-01 11:05:39 -0500 | [diff] [blame] | 6318 | err = btrfs_init_inode_security(trans, inode, dir, &dentry->d_name); |
Josef Bacik | 33268ea | 2008-07-24 12:16:36 -0400 | [diff] [blame] | 6319 | if (err) |
Chris Mason | b0d5d10 | 2014-09-08 13:08:51 -0700 | [diff] [blame] | 6320 | goto out_fail_inode; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 6321 | |
Chris Mason | dbe674a | 2008-07-17 12:54:05 -0400 | [diff] [blame] | 6322 | btrfs_i_size_write(inode, 0); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 6323 | err = btrfs_update_inode(trans, root, inode); |
| 6324 | if (err) |
Chris Mason | b0d5d10 | 2014-09-08 13:08:51 -0700 | [diff] [blame] | 6325 | goto out_fail_inode; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 6326 | |
Josef Bacik | a1b075d | 2010-11-19 20:36:11 +0000 | [diff] [blame] | 6327 | err = btrfs_add_link(trans, dir, inode, dentry->d_name.name, |
| 6328 | dentry->d_name.len, 0, index); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 6329 | if (err) |
Chris Mason | b0d5d10 | 2014-09-08 13:08:51 -0700 | [diff] [blame] | 6330 | goto out_fail_inode; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 6331 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 6332 | d_instantiate(dentry, inode); |
Chris Mason | b0d5d10 | 2014-09-08 13:08:51 -0700 | [diff] [blame] | 6333 | /* |
| 6334 | * mkdir is special. We're unlocking after we call d_instantiate |
| 6335 | * to avoid a race with nfsd calling d_instantiate. |
| 6336 | */ |
| 6337 | unlock_new_inode(inode); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 6338 | drop_on_err = 0; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 6339 | |
| 6340 | out_fail: |
Josef Bacik | 7ad85bb | 2012-01-12 19:10:12 -0500 | [diff] [blame] | 6341 | btrfs_end_transaction(trans, root); |
Wang Shilong | c7cfb8a | 2014-12-24 14:45:30 +0800 | [diff] [blame] | 6342 | if (drop_on_err) { |
| 6343 | inode_dec_link_count(inode); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 6344 | iput(inode); |
Wang Shilong | c7cfb8a | 2014-12-24 14:45:30 +0800 | [diff] [blame] | 6345 | } |
Josef Bacik | c581afc | 2014-02-06 16:06:06 -0500 | [diff] [blame] | 6346 | btrfs_balance_delayed_items(root); |
Liu Bo | b53d3f5 | 2012-11-14 14:34:34 +0000 | [diff] [blame] | 6347 | btrfs_btree_balance_dirty(root); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 6348 | return err; |
Chris Mason | b0d5d10 | 2014-09-08 13:08:51 -0700 | [diff] [blame] | 6349 | |
| 6350 | out_fail_inode: |
| 6351 | unlock_new_inode(inode); |
| 6352 | goto out_fail; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 6353 | } |
| 6354 | |
Qu Wenruo | e6c4efd | 2014-09-17 11:53:35 +0800 | [diff] [blame] | 6355 | /* Find next extent map of a given extent map, caller needs to ensure locks */ |
| 6356 | static struct extent_map *next_extent_map(struct extent_map *em) |
| 6357 | { |
| 6358 | struct rb_node *next; |
| 6359 | |
| 6360 | next = rb_next(&em->rb_node); |
| 6361 | if (!next) |
| 6362 | return NULL; |
| 6363 | return container_of(next, struct extent_map, rb_node); |
| 6364 | } |
| 6365 | |
| 6366 | static struct extent_map *prev_extent_map(struct extent_map *em) |
| 6367 | { |
| 6368 | struct rb_node *prev; |
| 6369 | |
| 6370 | prev = rb_prev(&em->rb_node); |
| 6371 | if (!prev) |
| 6372 | return NULL; |
| 6373 | return container_of(prev, struct extent_map, rb_node); |
| 6374 | } |
| 6375 | |
Chris Mason | d352ac6 | 2008-09-29 15:18:18 -0400 | [diff] [blame] | 6376 | /* helper for btfs_get_extent. Given an existing extent in the tree, |
Qu Wenruo | e6c4efd | 2014-09-17 11:53:35 +0800 | [diff] [blame] | 6377 | * the existing extent is the nearest extent to map_start, |
Chris Mason | d352ac6 | 2008-09-29 15:18:18 -0400 | [diff] [blame] | 6378 | * 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] | 6379 | * the best fitted new extent into the tree. |
Chris Mason | d352ac6 | 2008-09-29 15:18:18 -0400 | [diff] [blame] | 6380 | */ |
Chris Mason | 3b95151 | 2008-04-17 11:29:12 -0400 | [diff] [blame] | 6381 | static int merge_extent_mapping(struct extent_map_tree *em_tree, |
| 6382 | struct extent_map *existing, |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 6383 | struct extent_map *em, |
Qu Wenruo | 51f395a | 2014-08-08 13:06:20 +0800 | [diff] [blame] | 6384 | u64 map_start) |
Chris Mason | 3b95151 | 2008-04-17 11:29:12 -0400 | [diff] [blame] | 6385 | { |
Qu Wenruo | e6c4efd | 2014-09-17 11:53:35 +0800 | [diff] [blame] | 6386 | struct extent_map *prev; |
| 6387 | struct extent_map *next; |
| 6388 | u64 start; |
| 6389 | u64 end; |
Chris Mason | 3b95151 | 2008-04-17 11:29:12 -0400 | [diff] [blame] | 6390 | u64 start_diff; |
Chris Mason | 3b95151 | 2008-04-17 11:29:12 -0400 | [diff] [blame] | 6391 | |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 6392 | BUG_ON(map_start < em->start || map_start >= extent_map_end(em)); |
Qu Wenruo | e6c4efd | 2014-09-17 11:53:35 +0800 | [diff] [blame] | 6393 | |
| 6394 | if (existing->start > map_start) { |
| 6395 | next = existing; |
| 6396 | prev = prev_extent_map(next); |
| 6397 | } else { |
| 6398 | prev = existing; |
| 6399 | next = next_extent_map(prev); |
| 6400 | } |
| 6401 | |
| 6402 | start = prev ? extent_map_end(prev) : em->start; |
| 6403 | start = max_t(u64, start, em->start); |
| 6404 | end = next ? next->start : extent_map_end(em); |
| 6405 | end = min_t(u64, end, extent_map_end(em)); |
| 6406 | start_diff = start - em->start; |
| 6407 | em->start = start; |
| 6408 | em->len = end - start; |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 6409 | if (em->block_start < EXTENT_MAP_LAST_BYTE && |
| 6410 | !test_bit(EXTENT_FLAG_COMPRESSED, &em->flags)) { |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 6411 | em->block_start += start_diff; |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 6412 | em->block_len -= start_diff; |
| 6413 | } |
Josef Bacik | 09a2a8f9 | 2013-04-05 16:51:15 -0400 | [diff] [blame] | 6414 | return add_extent_mapping(em_tree, em, 0); |
Chris Mason | 3b95151 | 2008-04-17 11:29:12 -0400 | [diff] [blame] | 6415 | } |
| 6416 | |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 6417 | static noinline int uncompress_inline(struct btrfs_path *path, |
| 6418 | struct inode *inode, struct page *page, |
| 6419 | size_t pg_offset, u64 extent_offset, |
| 6420 | struct btrfs_file_extent_item *item) |
| 6421 | { |
| 6422 | int ret; |
| 6423 | struct extent_buffer *leaf = path->nodes[0]; |
| 6424 | char *tmp; |
| 6425 | size_t max_size; |
| 6426 | unsigned long inline_size; |
| 6427 | unsigned long ptr; |
Li Zefan | 261507a0 | 2010-12-17 14:21:50 +0800 | [diff] [blame] | 6428 | int compress_type; |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 6429 | |
| 6430 | WARN_ON(pg_offset != 0); |
Li Zefan | 261507a0 | 2010-12-17 14:21:50 +0800 | [diff] [blame] | 6431 | compress_type = btrfs_file_extent_compression(leaf, item); |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 6432 | max_size = btrfs_file_extent_ram_bytes(leaf, item); |
| 6433 | inline_size = btrfs_file_extent_inline_item_len(leaf, |
Ross Kirk | dd3cc16 | 2013-09-16 15:58:09 +0100 | [diff] [blame] | 6434 | btrfs_item_nr(path->slots[0])); |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 6435 | tmp = kmalloc(inline_size, GFP_NOFS); |
Tsutomu Itoh | 8d41371 | 2011-04-25 19:43:52 -0400 | [diff] [blame] | 6436 | if (!tmp) |
| 6437 | return -ENOMEM; |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 6438 | ptr = btrfs_file_extent_inline_start(item); |
| 6439 | |
| 6440 | read_extent_buffer(leaf, tmp, ptr, inline_size); |
| 6441 | |
Chris Mason | 5b050f0 | 2008-11-11 09:34:41 -0500 | [diff] [blame] | 6442 | max_size = min_t(unsigned long, PAGE_CACHE_SIZE, max_size); |
Li Zefan | 261507a0 | 2010-12-17 14:21:50 +0800 | [diff] [blame] | 6443 | ret = btrfs_decompress(compress_type, tmp, page, |
| 6444 | extent_offset, inline_size, max_size); |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 6445 | kfree(tmp); |
Zach Brown | 166ae5a | 2014-05-09 17:15:10 -0400 | [diff] [blame] | 6446 | return ret; |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 6447 | } |
| 6448 | |
Chris Mason | d352ac6 | 2008-09-29 15:18:18 -0400 | [diff] [blame] | 6449 | /* |
| 6450 | * 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] | 6451 | * the ugly parts come from merging extents from the disk with the in-ram |
| 6452 | * representation. This gets more complex because of the data=ordered code, |
Chris Mason | d352ac6 | 2008-09-29 15:18:18 -0400 | [diff] [blame] | 6453 | * where the in-ram extents might be locked pending data=ordered completion. |
| 6454 | * |
| 6455 | * This also copies inline extents directly into the page. |
| 6456 | */ |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 6457 | |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 6458 | struct extent_map *btrfs_get_extent(struct inode *inode, struct page *page, |
Chris Mason | 70dec80 | 2008-01-29 09:59:12 -0500 | [diff] [blame] | 6459 | size_t pg_offset, u64 start, u64 len, |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 6460 | int create) |
| 6461 | { |
| 6462 | int ret; |
| 6463 | int err = 0; |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 6464 | u64 extent_start = 0; |
| 6465 | u64 extent_end = 0; |
Li Zefan | 33345d01 | 2011-04-20 10:31:50 +0800 | [diff] [blame] | 6466 | u64 objectid = btrfs_ino(inode); |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 6467 | u32 found_type; |
Chris Mason | f421950 | 2008-07-22 11:18:09 -0400 | [diff] [blame] | 6468 | struct btrfs_path *path = NULL; |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 6469 | struct btrfs_root *root = BTRFS_I(inode)->root; |
| 6470 | struct btrfs_file_extent_item *item; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 6471 | struct extent_buffer *leaf; |
| 6472 | struct btrfs_key found_key; |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 6473 | struct extent_map *em = NULL; |
| 6474 | struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree; |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 6475 | struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree; |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 6476 | struct btrfs_trans_handle *trans = NULL; |
Filipe Manana | 7ffbb59 | 2014-06-09 03:48:05 +0100 | [diff] [blame] | 6477 | const bool new_inline = !page || create; |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 6478 | |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 6479 | again: |
Chris Mason | 890871b | 2009-09-02 16:24:52 -0400 | [diff] [blame] | 6480 | read_lock(&em_tree->lock); |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 6481 | em = lookup_extent_mapping(em_tree, start, len); |
Chris Mason | a061fc8 | 2008-05-07 11:43:44 -0400 | [diff] [blame] | 6482 | if (em) |
| 6483 | em->bdev = root->fs_info->fs_devices->latest_bdev; |
Chris Mason | 890871b | 2009-09-02 16:24:52 -0400 | [diff] [blame] | 6484 | read_unlock(&em_tree->lock); |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 6485 | |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 6486 | if (em) { |
Chris Mason | e1c4b74 | 2008-04-22 13:26:46 -0400 | [diff] [blame] | 6487 | if (em->start > start || em->start + em->len <= start) |
| 6488 | free_extent_map(em); |
| 6489 | else if (em->block_start == EXTENT_MAP_INLINE && page) |
Chris Mason | 70dec80 | 2008-01-29 09:59:12 -0500 | [diff] [blame] | 6490 | free_extent_map(em); |
| 6491 | else |
| 6492 | goto out; |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 6493 | } |
David Sterba | 172ddd6 | 2011-04-21 00:48:27 +0200 | [diff] [blame] | 6494 | em = alloc_extent_map(); |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 6495 | if (!em) { |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 6496 | err = -ENOMEM; |
| 6497 | goto out; |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 6498 | } |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 6499 | em->bdev = root->fs_info->fs_devices->latest_bdev; |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 6500 | em->start = EXTENT_MAP_HOLE; |
Chris Mason | 445a694 | 2008-11-10 11:53:33 -0500 | [diff] [blame] | 6501 | em->orig_start = EXTENT_MAP_HOLE; |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 6502 | em->len = (u64)-1; |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 6503 | em->block_len = (u64)-1; |
Chris Mason | f421950 | 2008-07-22 11:18:09 -0400 | [diff] [blame] | 6504 | |
| 6505 | if (!path) { |
| 6506 | path = btrfs_alloc_path(); |
Josef Bacik | 026fd31 | 2011-05-13 10:32:11 -0400 | [diff] [blame] | 6507 | if (!path) { |
| 6508 | err = -ENOMEM; |
| 6509 | goto out; |
| 6510 | } |
| 6511 | /* |
| 6512 | * Chances are we'll be called again, so go ahead and do |
| 6513 | * readahead |
| 6514 | */ |
| 6515 | path->reada = 1; |
Chris Mason | f421950 | 2008-07-22 11:18:09 -0400 | [diff] [blame] | 6516 | } |
| 6517 | |
Chris Mason | 179e29e | 2007-11-01 11:28:41 -0400 | [diff] [blame] | 6518 | ret = btrfs_lookup_file_extent(trans, root, path, |
| 6519 | objectid, start, trans != NULL); |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 6520 | if (ret < 0) { |
| 6521 | err = ret; |
| 6522 | goto out; |
| 6523 | } |
| 6524 | |
| 6525 | if (ret != 0) { |
| 6526 | if (path->slots[0] == 0) |
| 6527 | goto not_found; |
| 6528 | path->slots[0]--; |
| 6529 | } |
| 6530 | |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 6531 | leaf = path->nodes[0]; |
| 6532 | item = btrfs_item_ptr(leaf, path->slots[0], |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 6533 | struct btrfs_file_extent_item); |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 6534 | /* are we inside the extent that was found? */ |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 6535 | btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]); |
David Sterba | 962a298 | 2014-06-04 18:41:45 +0200 | [diff] [blame] | 6536 | found_type = found_key.type; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 6537 | if (found_key.objectid != objectid || |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 6538 | found_type != BTRFS_EXTENT_DATA_KEY) { |
Josef Bacik | 25a5034 | 2013-10-14 12:08:38 -0400 | [diff] [blame] | 6539 | /* |
| 6540 | * If we backup past the first extent we want to move forward |
| 6541 | * and see if there is an extent in front of us, otherwise we'll |
| 6542 | * say there is a hole for our whole search range which can |
| 6543 | * cause problems. |
| 6544 | */ |
| 6545 | extent_end = start; |
| 6546 | goto next; |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 6547 | } |
| 6548 | |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 6549 | found_type = btrfs_file_extent_type(leaf, item); |
| 6550 | extent_start = found_key.offset; |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 6551 | if (found_type == BTRFS_FILE_EXTENT_REG || |
| 6552 | found_type == BTRFS_FILE_EXTENT_PREALLOC) { |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 6553 | extent_end = extent_start + |
Chris Mason | db94535 | 2007-10-15 16:15:53 -0400 | [diff] [blame] | 6554 | btrfs_file_extent_num_bytes(leaf, item); |
Yan Zheng | 9036c10 | 2008-10-30 14:19:41 -0400 | [diff] [blame] | 6555 | } else if (found_type == BTRFS_FILE_EXTENT_INLINE) { |
| 6556 | size_t size; |
Chris Mason | 514ac8a | 2014-01-03 21:07:00 -0800 | [diff] [blame] | 6557 | size = btrfs_file_extent_inline_len(leaf, path->slots[0], item); |
Qu Wenruo | fda2832 | 2013-02-26 08:10:22 +0000 | [diff] [blame] | 6558 | extent_end = ALIGN(extent_start + size, root->sectorsize); |
Yan Zheng | 9036c10 | 2008-10-30 14:19:41 -0400 | [diff] [blame] | 6559 | } |
Josef Bacik | 25a5034 | 2013-10-14 12:08:38 -0400 | [diff] [blame] | 6560 | next: |
Yan Zheng | 9036c10 | 2008-10-30 14:19:41 -0400 | [diff] [blame] | 6561 | if (start >= extent_end) { |
| 6562 | path->slots[0]++; |
| 6563 | if (path->slots[0] >= btrfs_header_nritems(leaf)) { |
| 6564 | ret = btrfs_next_leaf(root, path); |
| 6565 | if (ret < 0) { |
| 6566 | err = ret; |
| 6567 | goto out; |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 6568 | } |
Yan Zheng | 9036c10 | 2008-10-30 14:19:41 -0400 | [diff] [blame] | 6569 | if (ret > 0) |
| 6570 | goto not_found; |
| 6571 | leaf = path->nodes[0]; |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 6572 | } |
Yan Zheng | 9036c10 | 2008-10-30 14:19:41 -0400 | [diff] [blame] | 6573 | btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]); |
| 6574 | if (found_key.objectid != objectid || |
| 6575 | found_key.type != BTRFS_EXTENT_DATA_KEY) |
| 6576 | goto not_found; |
| 6577 | if (start + len <= found_key.offset) |
| 6578 | goto not_found; |
Wang Shilong | e2eca69 | 2014-07-17 11:44:14 +0800 | [diff] [blame] | 6579 | if (start > found_key.offset) |
| 6580 | goto next; |
Yan Zheng | 9036c10 | 2008-10-30 14:19:41 -0400 | [diff] [blame] | 6581 | em->start = start; |
Josef Bacik | 70c8a91 | 2012-10-11 16:54:30 -0400 | [diff] [blame] | 6582 | em->orig_start = start; |
Yan Zheng | 9036c10 | 2008-10-30 14:19:41 -0400 | [diff] [blame] | 6583 | em->len = found_key.offset - start; |
| 6584 | goto not_found_em; |
| 6585 | } |
| 6586 | |
Filipe Manana | 7ffbb59 | 2014-06-09 03:48:05 +0100 | [diff] [blame] | 6587 | btrfs_extent_item_to_extent_map(inode, path, item, new_inline, em); |
| 6588 | |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 6589 | if (found_type == BTRFS_FILE_EXTENT_REG || |
| 6590 | found_type == BTRFS_FILE_EXTENT_PREALLOC) { |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 6591 | goto insert; |
| 6592 | } else if (found_type == BTRFS_FILE_EXTENT_INLINE) { |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 6593 | unsigned long ptr; |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 6594 | char *map; |
Chris Mason | 3326d1b | 2007-10-15 16:18:25 -0400 | [diff] [blame] | 6595 | size_t size; |
| 6596 | size_t extent_offset; |
| 6597 | size_t copy_size; |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 6598 | |
Filipe Manana | 7ffbb59 | 2014-06-09 03:48:05 +0100 | [diff] [blame] | 6599 | if (new_inline) |
Yan | 689f934 | 2007-10-29 11:41:07 -0400 | [diff] [blame] | 6600 | goto out; |
Yan | 689f934 | 2007-10-29 11:41:07 -0400 | [diff] [blame] | 6601 | |
Chris Mason | 514ac8a | 2014-01-03 21:07:00 -0800 | [diff] [blame] | 6602 | size = btrfs_file_extent_inline_len(leaf, path->slots[0], item); |
Yan Zheng | 9036c10 | 2008-10-30 14:19:41 -0400 | [diff] [blame] | 6603 | extent_offset = page_offset(page) + pg_offset - extent_start; |
Chris Mason | 70dec80 | 2008-01-29 09:59:12 -0500 | [diff] [blame] | 6604 | copy_size = min_t(u64, PAGE_CACHE_SIZE - pg_offset, |
Yan | 689f934 | 2007-10-29 11:41:07 -0400 | [diff] [blame] | 6605 | size - extent_offset); |
Chris Mason | 3326d1b | 2007-10-15 16:18:25 -0400 | [diff] [blame] | 6606 | em->start = extent_start + extent_offset; |
Qu Wenruo | fda2832 | 2013-02-26 08:10:22 +0000 | [diff] [blame] | 6607 | em->len = ALIGN(copy_size, root->sectorsize); |
Josef Bacik | b493968 | 2012-12-03 10:31:19 -0500 | [diff] [blame] | 6608 | em->orig_block_len = em->len; |
Josef Bacik | 70c8a91 | 2012-10-11 16:54:30 -0400 | [diff] [blame] | 6609 | em->orig_start = em->start; |
Yan | 689f934 | 2007-10-29 11:41:07 -0400 | [diff] [blame] | 6610 | ptr = btrfs_file_extent_inline_start(item) + extent_offset; |
Chris Mason | 179e29e | 2007-11-01 11:28:41 -0400 | [diff] [blame] | 6611 | if (create == 0 && !PageUptodate(page)) { |
Li Zefan | 261507a0 | 2010-12-17 14:21:50 +0800 | [diff] [blame] | 6612 | if (btrfs_file_extent_compression(leaf, item) != |
| 6613 | BTRFS_COMPRESS_NONE) { |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 6614 | ret = uncompress_inline(path, inode, page, |
| 6615 | pg_offset, |
| 6616 | extent_offset, item); |
Zach Brown | 166ae5a | 2014-05-09 17:15:10 -0400 | [diff] [blame] | 6617 | if (ret) { |
| 6618 | err = ret; |
| 6619 | goto out; |
| 6620 | } |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 6621 | } else { |
| 6622 | map = kmap(page); |
| 6623 | read_extent_buffer(leaf, map + pg_offset, ptr, |
| 6624 | copy_size); |
Chris Mason | 93c82d5 | 2009-09-11 12:36:29 -0400 | [diff] [blame] | 6625 | if (pg_offset + copy_size < PAGE_CACHE_SIZE) { |
| 6626 | memset(map + pg_offset + copy_size, 0, |
| 6627 | PAGE_CACHE_SIZE - pg_offset - |
| 6628 | copy_size); |
| 6629 | } |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 6630 | kunmap(page); |
| 6631 | } |
Chris Mason | 179e29e | 2007-11-01 11:28:41 -0400 | [diff] [blame] | 6632 | flush_dcache_page(page); |
| 6633 | } else if (create && PageUptodate(page)) { |
Jan Schmidt | 6bf7e08 | 2011-12-01 14:35:19 +0100 | [diff] [blame] | 6634 | BUG(); |
Chris Mason | 179e29e | 2007-11-01 11:28:41 -0400 | [diff] [blame] | 6635 | if (!trans) { |
| 6636 | kunmap(page); |
| 6637 | free_extent_map(em); |
| 6638 | em = NULL; |
Chris Mason | ff5714c | 2011-05-28 07:00:39 -0400 | [diff] [blame] | 6639 | |
David Sterba | b3b4aa7 | 2011-04-21 01:20:15 +0200 | [diff] [blame] | 6640 | btrfs_release_path(path); |
Josef Bacik | 7a7eaa4 | 2011-04-13 12:54:33 -0400 | [diff] [blame] | 6641 | trans = btrfs_join_transaction(root); |
Chris Mason | ff5714c | 2011-05-28 07:00:39 -0400 | [diff] [blame] | 6642 | |
Tsutomu Itoh | 3612b49 | 2011-01-25 02:51:38 +0000 | [diff] [blame] | 6643 | if (IS_ERR(trans)) |
| 6644 | return ERR_CAST(trans); |
Chris Mason | 179e29e | 2007-11-01 11:28:41 -0400 | [diff] [blame] | 6645 | goto again; |
| 6646 | } |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 6647 | map = kmap(page); |
Chris Mason | 70dec80 | 2008-01-29 09:59:12 -0500 | [diff] [blame] | 6648 | write_extent_buffer(leaf, map + pg_offset, ptr, |
Chris Mason | 179e29e | 2007-11-01 11:28:41 -0400 | [diff] [blame] | 6649 | copy_size); |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 6650 | kunmap(page); |
Chris Mason | 179e29e | 2007-11-01 11:28:41 -0400 | [diff] [blame] | 6651 | btrfs_mark_buffer_dirty(leaf); |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 6652 | } |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 6653 | set_extent_uptodate(io_tree, em->start, |
Arne Jansen | 507903b | 2011-04-06 10:02:20 +0000 | [diff] [blame] | 6654 | extent_map_end(em) - 1, NULL, GFP_NOFS); |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 6655 | goto insert; |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 6656 | } |
| 6657 | not_found: |
| 6658 | em->start = start; |
Josef Bacik | 70c8a91 | 2012-10-11 16:54:30 -0400 | [diff] [blame] | 6659 | em->orig_start = start; |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 6660 | em->len = len; |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 6661 | not_found_em: |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 6662 | em->block_start = EXTENT_MAP_HOLE; |
Yan Zheng | 9036c10 | 2008-10-30 14:19:41 -0400 | [diff] [blame] | 6663 | set_bit(EXTENT_FLAG_VACANCY, &em->flags); |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 6664 | insert: |
David Sterba | b3b4aa7 | 2011-04-21 01:20:15 +0200 | [diff] [blame] | 6665 | btrfs_release_path(path); |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 6666 | if (em->start > start || extent_map_end(em) <= start) { |
Simon Kirby | c2cf52e | 2013-03-19 22:41:23 +0000 | [diff] [blame] | 6667 | 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] | 6668 | em->start, em->len, start, len); |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 6669 | err = -EIO; |
| 6670 | goto out; |
| 6671 | } |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 6672 | |
| 6673 | err = 0; |
Chris Mason | 890871b | 2009-09-02 16:24:52 -0400 | [diff] [blame] | 6674 | write_lock(&em_tree->lock); |
Josef Bacik | 09a2a8f9 | 2013-04-05 16:51:15 -0400 | [diff] [blame] | 6675 | ret = add_extent_mapping(em_tree, em, 0); |
Chris Mason | 3b95151 | 2008-04-17 11:29:12 -0400 | [diff] [blame] | 6676 | /* it is possible that someone inserted the extent into the tree |
| 6677 | * while we had the lock dropped. It is also possible that |
| 6678 | * an overlapping map exists in the tree |
| 6679 | */ |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 6680 | if (ret == -EEXIST) { |
Chris Mason | 3b95151 | 2008-04-17 11:29:12 -0400 | [diff] [blame] | 6681 | struct extent_map *existing; |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 6682 | |
| 6683 | ret = 0; |
| 6684 | |
Qu Wenruo | e6c4efd | 2014-09-17 11:53:35 +0800 | [diff] [blame] | 6685 | existing = search_extent_mapping(em_tree, start, len); |
| 6686 | /* |
| 6687 | * existing will always be non-NULL, since there must be |
| 6688 | * extent causing the -EEXIST. |
| 6689 | */ |
| 6690 | if (start >= extent_map_end(existing) || |
Qu Wenruo | 32be3a1 | 2014-09-22 09:13:03 +0800 | [diff] [blame] | 6691 | start <= existing->start) { |
Qu Wenruo | e6c4efd | 2014-09-17 11:53:35 +0800 | [diff] [blame] | 6692 | /* |
| 6693 | * The existing extent map is the one nearest to |
| 6694 | * the [start, start + len) range which overlaps |
| 6695 | */ |
| 6696 | err = merge_extent_mapping(em_tree, existing, |
| 6697 | em, start); |
Chris Mason | e1c4b74 | 2008-04-22 13:26:46 -0400 | [diff] [blame] | 6698 | free_extent_map(existing); |
Qu Wenruo | e6c4efd | 2014-09-17 11:53:35 +0800 | [diff] [blame] | 6699 | if (err) { |
Chris Mason | 3b95151 | 2008-04-17 11:29:12 -0400 | [diff] [blame] | 6700 | free_extent_map(em); |
| 6701 | em = NULL; |
| 6702 | } |
| 6703 | } else { |
| 6704 | free_extent_map(em); |
| 6705 | em = existing; |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 6706 | err = 0; |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 6707 | } |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 6708 | } |
Chris Mason | 890871b | 2009-09-02 16:24:52 -0400 | [diff] [blame] | 6709 | write_unlock(&em_tree->lock); |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 6710 | out: |
liubo | 1abe9b8 | 2011-03-24 11:18:59 +0000 | [diff] [blame] | 6711 | |
Steven Rostedt | 4cd8587 | 2013-11-14 22:57:29 -0500 | [diff] [blame] | 6712 | trace_btrfs_get_extent(root, em); |
liubo | 1abe9b8 | 2011-03-24 11:18:59 +0000 | [diff] [blame] | 6713 | |
Chris Mason | f421950 | 2008-07-22 11:18:09 -0400 | [diff] [blame] | 6714 | if (path) |
| 6715 | btrfs_free_path(path); |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 6716 | if (trans) { |
| 6717 | ret = btrfs_end_transaction(trans, root); |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 6718 | if (!err) |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 6719 | err = ret; |
| 6720 | } |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 6721 | if (err) { |
| 6722 | free_extent_map(em); |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 6723 | return ERR_PTR(err); |
| 6724 | } |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 6725 | BUG_ON(!em); /* Error is always set */ |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 6726 | return em; |
| 6727 | } |
| 6728 | |
Chris Mason | ec29ed5 | 2011-02-23 16:23:20 -0500 | [diff] [blame] | 6729 | struct extent_map *btrfs_get_extent_fiemap(struct inode *inode, struct page *page, |
| 6730 | size_t pg_offset, u64 start, u64 len, |
| 6731 | int create) |
| 6732 | { |
| 6733 | struct extent_map *em; |
| 6734 | struct extent_map *hole_em = NULL; |
| 6735 | u64 range_start = start; |
| 6736 | u64 end; |
| 6737 | u64 found; |
| 6738 | u64 found_end; |
| 6739 | int err = 0; |
| 6740 | |
| 6741 | em = btrfs_get_extent(inode, page, pg_offset, start, len, create); |
| 6742 | if (IS_ERR(em)) |
| 6743 | return em; |
| 6744 | if (em) { |
| 6745 | /* |
Liu Bo | f9e4fb5 | 2013-01-07 10:10:12 +0000 | [diff] [blame] | 6746 | * if our em maps to |
| 6747 | * - a hole or |
| 6748 | * - a pre-alloc extent, |
| 6749 | * there might actually be delalloc bytes behind it. |
Chris Mason | ec29ed5 | 2011-02-23 16:23:20 -0500 | [diff] [blame] | 6750 | */ |
Liu Bo | f9e4fb5 | 2013-01-07 10:10:12 +0000 | [diff] [blame] | 6751 | if (em->block_start != EXTENT_MAP_HOLE && |
| 6752 | !test_bit(EXTENT_FLAG_PREALLOC, &em->flags)) |
Chris Mason | ec29ed5 | 2011-02-23 16:23:20 -0500 | [diff] [blame] | 6753 | return em; |
| 6754 | else |
| 6755 | hole_em = em; |
| 6756 | } |
| 6757 | |
| 6758 | /* check to see if we've wrapped (len == -1 or similar) */ |
| 6759 | end = start + len; |
| 6760 | if (end < start) |
| 6761 | end = (u64)-1; |
| 6762 | else |
| 6763 | end -= 1; |
| 6764 | |
| 6765 | em = NULL; |
| 6766 | |
| 6767 | /* ok, we didn't find anything, lets look for delalloc */ |
| 6768 | found = count_range_bits(&BTRFS_I(inode)->io_tree, &range_start, |
| 6769 | end, len, EXTENT_DELALLOC, 1); |
| 6770 | found_end = range_start + found; |
| 6771 | if (found_end < range_start) |
| 6772 | found_end = (u64)-1; |
| 6773 | |
| 6774 | /* |
| 6775 | * we didn't find anything useful, return |
| 6776 | * the original results from get_extent() |
| 6777 | */ |
| 6778 | if (range_start > end || found_end <= start) { |
| 6779 | em = hole_em; |
| 6780 | hole_em = NULL; |
| 6781 | goto out; |
| 6782 | } |
| 6783 | |
| 6784 | /* adjust the range_start to make sure it doesn't |
| 6785 | * go backwards from the start they passed in |
| 6786 | */ |
Dulshani Gunawardhana | 6787125 | 2013-10-31 10:33:04 +0530 | [diff] [blame] | 6787 | range_start = max(start, range_start); |
Chris Mason | ec29ed5 | 2011-02-23 16:23:20 -0500 | [diff] [blame] | 6788 | found = found_end - range_start; |
| 6789 | |
| 6790 | if (found > 0) { |
| 6791 | u64 hole_start = start; |
| 6792 | u64 hole_len = len; |
| 6793 | |
David Sterba | 172ddd6 | 2011-04-21 00:48:27 +0200 | [diff] [blame] | 6794 | em = alloc_extent_map(); |
Chris Mason | ec29ed5 | 2011-02-23 16:23:20 -0500 | [diff] [blame] | 6795 | if (!em) { |
| 6796 | err = -ENOMEM; |
| 6797 | goto out; |
| 6798 | } |
| 6799 | /* |
| 6800 | * when btrfs_get_extent can't find anything it |
| 6801 | * returns one huge hole |
| 6802 | * |
| 6803 | * make sure what it found really fits our range, and |
| 6804 | * adjust to make sure it is based on the start from |
| 6805 | * the caller |
| 6806 | */ |
| 6807 | if (hole_em) { |
| 6808 | u64 calc_end = extent_map_end(hole_em); |
| 6809 | |
| 6810 | if (calc_end <= start || (hole_em->start > end)) { |
| 6811 | free_extent_map(hole_em); |
| 6812 | hole_em = NULL; |
| 6813 | } else { |
| 6814 | hole_start = max(hole_em->start, start); |
| 6815 | hole_len = calc_end - hole_start; |
| 6816 | } |
| 6817 | } |
| 6818 | em->bdev = NULL; |
| 6819 | if (hole_em && range_start > hole_start) { |
| 6820 | /* our hole starts before our delalloc, so we |
| 6821 | * have to return just the parts of the hole |
| 6822 | * that go until the delalloc starts |
| 6823 | */ |
| 6824 | em->len = min(hole_len, |
| 6825 | range_start - hole_start); |
| 6826 | em->start = hole_start; |
| 6827 | em->orig_start = hole_start; |
| 6828 | /* |
| 6829 | * don't adjust block start at all, |
| 6830 | * it is fixed at EXTENT_MAP_HOLE |
| 6831 | */ |
| 6832 | em->block_start = hole_em->block_start; |
| 6833 | em->block_len = hole_len; |
Liu Bo | f9e4fb5 | 2013-01-07 10:10:12 +0000 | [diff] [blame] | 6834 | if (test_bit(EXTENT_FLAG_PREALLOC, &hole_em->flags)) |
| 6835 | set_bit(EXTENT_FLAG_PREALLOC, &em->flags); |
Chris Mason | ec29ed5 | 2011-02-23 16:23:20 -0500 | [diff] [blame] | 6836 | } else { |
| 6837 | em->start = range_start; |
| 6838 | em->len = found; |
| 6839 | em->orig_start = range_start; |
| 6840 | em->block_start = EXTENT_MAP_DELALLOC; |
| 6841 | em->block_len = found; |
| 6842 | } |
| 6843 | } else if (hole_em) { |
| 6844 | return hole_em; |
| 6845 | } |
| 6846 | out: |
| 6847 | |
| 6848 | free_extent_map(hole_em); |
| 6849 | if (err) { |
| 6850 | free_extent_map(em); |
| 6851 | return ERR_PTR(err); |
| 6852 | } |
| 6853 | return em; |
| 6854 | } |
| 6855 | |
Josef Bacik | 4b46fce | 2010-05-23 11:00:55 -0400 | [diff] [blame] | 6856 | static struct extent_map *btrfs_new_extent_direct(struct inode *inode, |
| 6857 | u64 start, u64 len) |
| 6858 | { |
| 6859 | struct btrfs_root *root = BTRFS_I(inode)->root; |
Josef Bacik | 70c8a91 | 2012-10-11 16:54:30 -0400 | [diff] [blame] | 6860 | struct extent_map *em; |
Josef Bacik | 4b46fce | 2010-05-23 11:00:55 -0400 | [diff] [blame] | 6861 | struct btrfs_key ins; |
| 6862 | u64 alloc_hint; |
| 6863 | int ret; |
Josef Bacik | 4b46fce | 2010-05-23 11:00:55 -0400 | [diff] [blame] | 6864 | |
Josef Bacik | 4b46fce | 2010-05-23 11:00:55 -0400 | [diff] [blame] | 6865 | alloc_hint = get_extent_allocation_hint(inode, start, len); |
Josef Bacik | 0036158 | 2013-08-14 14:02:47 -0400 | [diff] [blame] | 6866 | ret = btrfs_reserve_extent(root, len, root->sectorsize, 0, |
Miao Xie | e570fd2 | 2014-06-19 10:42:50 +0800 | [diff] [blame] | 6867 | alloc_hint, &ins, 1, 1); |
Josef Bacik | 0036158 | 2013-08-14 14:02:47 -0400 | [diff] [blame] | 6868 | if (ret) |
| 6869 | return ERR_PTR(ret); |
Josef Bacik | 4b46fce | 2010-05-23 11:00:55 -0400 | [diff] [blame] | 6870 | |
Josef Bacik | 70c8a91 | 2012-10-11 16:54:30 -0400 | [diff] [blame] | 6871 | em = create_pinned_em(inode, start, ins.offset, start, ins.objectid, |
Josef Bacik | cc95bef | 2013-04-04 14:31:27 -0400 | [diff] [blame] | 6872 | ins.offset, ins.offset, ins.offset, 0); |
Josef Bacik | 0036158 | 2013-08-14 14:02:47 -0400 | [diff] [blame] | 6873 | if (IS_ERR(em)) { |
Miao Xie | e570fd2 | 2014-06-19 10:42:50 +0800 | [diff] [blame] | 6874 | btrfs_free_reserved_extent(root, ins.objectid, ins.offset, 1); |
Josef Bacik | 0036158 | 2013-08-14 14:02:47 -0400 | [diff] [blame] | 6875 | return em; |
| 6876 | } |
Josef Bacik | 4b46fce | 2010-05-23 11:00:55 -0400 | [diff] [blame] | 6877 | |
| 6878 | ret = btrfs_add_ordered_extent_dio(inode, start, ins.objectid, |
| 6879 | ins.offset, ins.offset, 0); |
| 6880 | if (ret) { |
Miao Xie | e570fd2 | 2014-06-19 10:42:50 +0800 | [diff] [blame] | 6881 | btrfs_free_reserved_extent(root, ins.objectid, ins.offset, 1); |
Josef Bacik | 0036158 | 2013-08-14 14:02:47 -0400 | [diff] [blame] | 6882 | free_extent_map(em); |
| 6883 | return ERR_PTR(ret); |
Josef Bacik | 4b46fce | 2010-05-23 11:00:55 -0400 | [diff] [blame] | 6884 | } |
Josef Bacik | 0036158 | 2013-08-14 14:02:47 -0400 | [diff] [blame] | 6885 | |
Josef Bacik | 4b46fce | 2010-05-23 11:00:55 -0400 | [diff] [blame] | 6886 | return em; |
| 6887 | } |
| 6888 | |
Chris Mason | 46bfbb5 | 2010-05-26 11:04:10 -0400 | [diff] [blame] | 6889 | /* |
| 6890 | * returns 1 when the nocow is safe, < 1 on error, 0 if the |
| 6891 | * block must be cow'd |
| 6892 | */ |
Josef Bacik | 0036158 | 2013-08-14 14:02:47 -0400 | [diff] [blame] | 6893 | noinline int can_nocow_extent(struct inode *inode, u64 offset, u64 *len, |
Josef Bacik | 7ee9e44 | 2013-06-21 16:37:03 -0400 | [diff] [blame] | 6894 | u64 *orig_start, u64 *orig_block_len, |
| 6895 | u64 *ram_bytes) |
Chris Mason | 46bfbb5 | 2010-05-26 11:04:10 -0400 | [diff] [blame] | 6896 | { |
Josef Bacik | 0036158 | 2013-08-14 14:02:47 -0400 | [diff] [blame] | 6897 | struct btrfs_trans_handle *trans; |
Chris Mason | 46bfbb5 | 2010-05-26 11:04:10 -0400 | [diff] [blame] | 6898 | struct btrfs_path *path; |
| 6899 | int ret; |
| 6900 | struct extent_buffer *leaf; |
| 6901 | struct btrfs_root *root = BTRFS_I(inode)->root; |
Miao Xie | 7b2b708 | 2014-02-27 13:58:05 +0800 | [diff] [blame] | 6902 | struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree; |
Chris Mason | 46bfbb5 | 2010-05-26 11:04:10 -0400 | [diff] [blame] | 6903 | struct btrfs_file_extent_item *fi; |
| 6904 | struct btrfs_key key; |
| 6905 | u64 disk_bytenr; |
| 6906 | u64 backref_offset; |
| 6907 | u64 extent_end; |
| 6908 | u64 num_bytes; |
| 6909 | int slot; |
| 6910 | int found_type; |
Josef Bacik | 7ee9e44 | 2013-06-21 16:37:03 -0400 | [diff] [blame] | 6911 | bool nocow = (BTRFS_I(inode)->flags & BTRFS_INODE_NODATACOW); |
Miao Xie | e77751a | 2013-12-27 21:11:50 +0800 | [diff] [blame] | 6912 | |
Chris Mason | 46bfbb5 | 2010-05-26 11:04:10 -0400 | [diff] [blame] | 6913 | path = btrfs_alloc_path(); |
| 6914 | if (!path) |
| 6915 | return -ENOMEM; |
| 6916 | |
Josef Bacik | 0036158 | 2013-08-14 14:02:47 -0400 | [diff] [blame] | 6917 | ret = btrfs_lookup_file_extent(NULL, root, path, btrfs_ino(inode), |
Chris Mason | 46bfbb5 | 2010-05-26 11:04:10 -0400 | [diff] [blame] | 6918 | offset, 0); |
| 6919 | if (ret < 0) |
| 6920 | goto out; |
| 6921 | |
| 6922 | slot = path->slots[0]; |
| 6923 | if (ret == 1) { |
| 6924 | if (slot == 0) { |
| 6925 | /* can't find the item, must cow */ |
| 6926 | ret = 0; |
| 6927 | goto out; |
| 6928 | } |
| 6929 | slot--; |
| 6930 | } |
| 6931 | ret = 0; |
| 6932 | leaf = path->nodes[0]; |
| 6933 | btrfs_item_key_to_cpu(leaf, &key, slot); |
Li Zefan | 33345d01 | 2011-04-20 10:31:50 +0800 | [diff] [blame] | 6934 | if (key.objectid != btrfs_ino(inode) || |
Chris Mason | 46bfbb5 | 2010-05-26 11:04:10 -0400 | [diff] [blame] | 6935 | key.type != BTRFS_EXTENT_DATA_KEY) { |
| 6936 | /* not our file or wrong item type, must cow */ |
| 6937 | goto out; |
| 6938 | } |
| 6939 | |
| 6940 | if (key.offset > offset) { |
| 6941 | /* Wrong offset, must cow */ |
| 6942 | goto out; |
| 6943 | } |
| 6944 | |
| 6945 | fi = btrfs_item_ptr(leaf, slot, struct btrfs_file_extent_item); |
| 6946 | found_type = btrfs_file_extent_type(leaf, fi); |
| 6947 | if (found_type != BTRFS_FILE_EXTENT_REG && |
| 6948 | found_type != BTRFS_FILE_EXTENT_PREALLOC) { |
| 6949 | /* not a regular extent, must cow */ |
| 6950 | goto out; |
| 6951 | } |
Josef Bacik | 7ee9e44 | 2013-06-21 16:37:03 -0400 | [diff] [blame] | 6952 | |
| 6953 | if (!nocow && found_type == BTRFS_FILE_EXTENT_REG) |
| 6954 | goto out; |
| 6955 | |
Miao Xie | e77751a | 2013-12-27 21:11:50 +0800 | [diff] [blame] | 6956 | extent_end = key.offset + btrfs_file_extent_num_bytes(leaf, fi); |
| 6957 | if (extent_end <= offset) |
| 6958 | goto out; |
| 6959 | |
Chris Mason | 46bfbb5 | 2010-05-26 11:04:10 -0400 | [diff] [blame] | 6960 | disk_bytenr = btrfs_file_extent_disk_bytenr(leaf, fi); |
Josef Bacik | 7ee9e44 | 2013-06-21 16:37:03 -0400 | [diff] [blame] | 6961 | if (disk_bytenr == 0) |
| 6962 | goto out; |
| 6963 | |
| 6964 | if (btrfs_file_extent_compression(leaf, fi) || |
| 6965 | btrfs_file_extent_encryption(leaf, fi) || |
| 6966 | btrfs_file_extent_other_encoding(leaf, fi)) |
| 6967 | goto out; |
| 6968 | |
Chris Mason | 46bfbb5 | 2010-05-26 11:04:10 -0400 | [diff] [blame] | 6969 | backref_offset = btrfs_file_extent_offset(leaf, fi); |
| 6970 | |
Josef Bacik | 7ee9e44 | 2013-06-21 16:37:03 -0400 | [diff] [blame] | 6971 | if (orig_start) { |
| 6972 | *orig_start = key.offset - backref_offset; |
| 6973 | *orig_block_len = btrfs_file_extent_disk_num_bytes(leaf, fi); |
| 6974 | *ram_bytes = btrfs_file_extent_ram_bytes(leaf, fi); |
| 6975 | } |
Josef Bacik | eb384b5 | 2013-04-24 16:32:55 -0400 | [diff] [blame] | 6976 | |
Chris Mason | 46bfbb5 | 2010-05-26 11:04:10 -0400 | [diff] [blame] | 6977 | if (btrfs_extent_readonly(root, disk_bytenr)) |
| 6978 | goto out; |
Miao Xie | 7b2b708 | 2014-02-27 13:58:05 +0800 | [diff] [blame] | 6979 | |
| 6980 | num_bytes = min(offset + *len, extent_end) - offset; |
| 6981 | if (!nocow && found_type == BTRFS_FILE_EXTENT_PREALLOC) { |
| 6982 | u64 range_end; |
| 6983 | |
| 6984 | range_end = round_up(offset + num_bytes, root->sectorsize) - 1; |
| 6985 | ret = test_range_bit(io_tree, offset, range_end, |
| 6986 | EXTENT_DELALLOC, 0, NULL); |
| 6987 | if (ret) { |
| 6988 | ret = -EAGAIN; |
| 6989 | goto out; |
| 6990 | } |
| 6991 | } |
| 6992 | |
Josef Bacik | 1bda19e | 2013-10-18 12:10:36 -0400 | [diff] [blame] | 6993 | btrfs_release_path(path); |
Chris Mason | 46bfbb5 | 2010-05-26 11:04:10 -0400 | [diff] [blame] | 6994 | |
| 6995 | /* |
| 6996 | * look for other files referencing this extent, if we |
| 6997 | * find any we must cow |
| 6998 | */ |
Josef Bacik | 0036158 | 2013-08-14 14:02:47 -0400 | [diff] [blame] | 6999 | trans = btrfs_join_transaction(root); |
| 7000 | if (IS_ERR(trans)) { |
| 7001 | ret = 0; |
Chris Mason | 46bfbb5 | 2010-05-26 11:04:10 -0400 | [diff] [blame] | 7002 | goto out; |
Josef Bacik | 0036158 | 2013-08-14 14:02:47 -0400 | [diff] [blame] | 7003 | } |
| 7004 | |
| 7005 | ret = btrfs_cross_ref_exist(trans, root, btrfs_ino(inode), |
| 7006 | key.offset - backref_offset, disk_bytenr); |
| 7007 | btrfs_end_transaction(trans, root); |
| 7008 | if (ret) { |
| 7009 | ret = 0; |
| 7010 | goto out; |
| 7011 | } |
Chris Mason | 46bfbb5 | 2010-05-26 11:04:10 -0400 | [diff] [blame] | 7012 | |
| 7013 | /* |
| 7014 | * adjust disk_bytenr and num_bytes to cover just the bytes |
| 7015 | * in this extent we are about to write. If there |
| 7016 | * are any csums in that range we have to cow in order |
| 7017 | * to keep the csums correct |
| 7018 | */ |
| 7019 | disk_bytenr += backref_offset; |
| 7020 | disk_bytenr += offset - key.offset; |
Chris Mason | 46bfbb5 | 2010-05-26 11:04:10 -0400 | [diff] [blame] | 7021 | if (csum_exist_in_range(root, disk_bytenr, num_bytes)) |
| 7022 | goto out; |
| 7023 | /* |
| 7024 | * all of the above have passed, it is safe to overwrite this extent |
| 7025 | * without cow |
| 7026 | */ |
Josef Bacik | eb384b5 | 2013-04-24 16:32:55 -0400 | [diff] [blame] | 7027 | *len = num_bytes; |
Chris Mason | 46bfbb5 | 2010-05-26 11:04:10 -0400 | [diff] [blame] | 7028 | ret = 1; |
| 7029 | out: |
| 7030 | btrfs_free_path(path); |
| 7031 | return ret; |
| 7032 | } |
| 7033 | |
Alex Gartrell | fc4adbff | 2014-05-20 13:07:56 -0700 | [diff] [blame] | 7034 | bool btrfs_page_exists_in_range(struct inode *inode, loff_t start, loff_t end) |
| 7035 | { |
| 7036 | struct radix_tree_root *root = &inode->i_mapping->page_tree; |
| 7037 | int found = false; |
| 7038 | void **pagep = NULL; |
| 7039 | struct page *page = NULL; |
| 7040 | int start_idx; |
| 7041 | int end_idx; |
| 7042 | |
| 7043 | start_idx = start >> PAGE_CACHE_SHIFT; |
| 7044 | |
| 7045 | /* |
| 7046 | * end is the last byte in the last page. end == start is legal |
| 7047 | */ |
| 7048 | end_idx = end >> PAGE_CACHE_SHIFT; |
| 7049 | |
| 7050 | rcu_read_lock(); |
| 7051 | |
| 7052 | /* Most of the code in this while loop is lifted from |
| 7053 | * find_get_page. It's been modified to begin searching from a |
| 7054 | * page and return just the first page found in that range. If the |
| 7055 | * found idx is less than or equal to the end idx then we know that |
| 7056 | * a page exists. If no pages are found or if those pages are |
| 7057 | * outside of the range then we're fine (yay!) */ |
| 7058 | while (page == NULL && |
| 7059 | radix_tree_gang_lookup_slot(root, &pagep, NULL, start_idx, 1)) { |
| 7060 | page = radix_tree_deref_slot(pagep); |
| 7061 | if (unlikely(!page)) |
| 7062 | break; |
| 7063 | |
| 7064 | if (radix_tree_exception(page)) { |
Filipe Manana | 809f901 | 2014-06-05 13:22:25 +0100 | [diff] [blame] | 7065 | if (radix_tree_deref_retry(page)) { |
| 7066 | page = NULL; |
Alex Gartrell | fc4adbff | 2014-05-20 13:07:56 -0700 | [diff] [blame] | 7067 | continue; |
Filipe Manana | 809f901 | 2014-06-05 13:22:25 +0100 | [diff] [blame] | 7068 | } |
Alex Gartrell | fc4adbff | 2014-05-20 13:07:56 -0700 | [diff] [blame] | 7069 | /* |
| 7070 | * Otherwise, shmem/tmpfs must be storing a swap entry |
| 7071 | * here as an exceptional entry: so return it without |
| 7072 | * attempting to raise page count. |
| 7073 | */ |
Filipe Manana | 6fdef6d | 2014-06-05 13:22:26 +0100 | [diff] [blame] | 7074 | page = NULL; |
Alex Gartrell | fc4adbff | 2014-05-20 13:07:56 -0700 | [diff] [blame] | 7075 | break; /* TODO: Is this relevant for this use case? */ |
| 7076 | } |
| 7077 | |
Filipe Manana | 9140515 | 2014-06-05 13:22:24 +0100 | [diff] [blame] | 7078 | if (!page_cache_get_speculative(page)) { |
| 7079 | page = NULL; |
Alex Gartrell | fc4adbff | 2014-05-20 13:07:56 -0700 | [diff] [blame] | 7080 | continue; |
Filipe Manana | 9140515 | 2014-06-05 13:22:24 +0100 | [diff] [blame] | 7081 | } |
Alex Gartrell | fc4adbff | 2014-05-20 13:07:56 -0700 | [diff] [blame] | 7082 | |
| 7083 | /* |
| 7084 | * Has the page moved? |
| 7085 | * This is part of the lockless pagecache protocol. See |
| 7086 | * include/linux/pagemap.h for details. |
| 7087 | */ |
| 7088 | if (unlikely(page != *pagep)) { |
| 7089 | page_cache_release(page); |
| 7090 | page = NULL; |
| 7091 | } |
| 7092 | } |
| 7093 | |
| 7094 | if (page) { |
| 7095 | if (page->index <= end_idx) |
| 7096 | found = true; |
| 7097 | page_cache_release(page); |
| 7098 | } |
| 7099 | |
| 7100 | rcu_read_unlock(); |
| 7101 | return found; |
| 7102 | } |
| 7103 | |
Josef Bacik | eb838e7 | 2012-07-31 16:28:48 -0400 | [diff] [blame] | 7104 | static int lock_extent_direct(struct inode *inode, u64 lockstart, u64 lockend, |
| 7105 | struct extent_state **cached_state, int writing) |
| 7106 | { |
| 7107 | struct btrfs_ordered_extent *ordered; |
| 7108 | int ret = 0; |
| 7109 | |
| 7110 | while (1) { |
| 7111 | lock_extent_bits(&BTRFS_I(inode)->io_tree, lockstart, lockend, |
| 7112 | 0, cached_state); |
| 7113 | /* |
| 7114 | * We're concerned with the entire range that we're going to be |
| 7115 | * doing DIO to, so we need to make sure theres no ordered |
| 7116 | * extents in this range. |
| 7117 | */ |
| 7118 | ordered = btrfs_lookup_ordered_range(inode, lockstart, |
| 7119 | lockend - lockstart + 1); |
| 7120 | |
| 7121 | /* |
| 7122 | * We need to make sure there are no buffered pages in this |
| 7123 | * range either, we could have raced between the invalidate in |
| 7124 | * generic_file_direct_write and locking the extent. The |
| 7125 | * invalidate needs to happen so that reads after a write do not |
| 7126 | * get stale data. |
| 7127 | */ |
Alex Gartrell | fc4adbff | 2014-05-20 13:07:56 -0700 | [diff] [blame] | 7128 | if (!ordered && |
| 7129 | (!writing || |
| 7130 | !btrfs_page_exists_in_range(inode, lockstart, lockend))) |
Josef Bacik | eb838e7 | 2012-07-31 16:28:48 -0400 | [diff] [blame] | 7131 | break; |
| 7132 | |
| 7133 | unlock_extent_cached(&BTRFS_I(inode)->io_tree, lockstart, lockend, |
| 7134 | cached_state, GFP_NOFS); |
| 7135 | |
| 7136 | if (ordered) { |
| 7137 | btrfs_start_ordered_extent(inode, ordered, 1); |
| 7138 | btrfs_put_ordered_extent(ordered); |
| 7139 | } else { |
| 7140 | /* Screw you mmap */ |
Filipe Manana | 728404d | 2014-10-10 09:43:11 +0100 | [diff] [blame] | 7141 | ret = btrfs_fdatawrite_range(inode, lockstart, lockend); |
Filipe Manana | 075bdbd | 2014-10-09 21:18:55 +0100 | [diff] [blame] | 7142 | if (ret) |
| 7143 | break; |
| 7144 | ret = filemap_fdatawait_range(inode->i_mapping, |
| 7145 | lockstart, |
| 7146 | lockend); |
Josef Bacik | eb838e7 | 2012-07-31 16:28:48 -0400 | [diff] [blame] | 7147 | if (ret) |
| 7148 | break; |
| 7149 | |
| 7150 | /* |
| 7151 | * If we found a page that couldn't be invalidated just |
| 7152 | * fall back to buffered. |
| 7153 | */ |
| 7154 | ret = invalidate_inode_pages2_range(inode->i_mapping, |
| 7155 | lockstart >> PAGE_CACHE_SHIFT, |
| 7156 | lockend >> PAGE_CACHE_SHIFT); |
| 7157 | if (ret) |
| 7158 | break; |
| 7159 | } |
| 7160 | |
| 7161 | cond_resched(); |
| 7162 | } |
| 7163 | |
| 7164 | return ret; |
| 7165 | } |
| 7166 | |
Josef Bacik | 69ffb54 | 2012-09-11 15:40:07 -0400 | [diff] [blame] | 7167 | static struct extent_map *create_pinned_em(struct inode *inode, u64 start, |
| 7168 | u64 len, u64 orig_start, |
| 7169 | u64 block_start, u64 block_len, |
Josef Bacik | cc95bef | 2013-04-04 14:31:27 -0400 | [diff] [blame] | 7170 | u64 orig_block_len, u64 ram_bytes, |
| 7171 | int type) |
Josef Bacik | 69ffb54 | 2012-09-11 15:40:07 -0400 | [diff] [blame] | 7172 | { |
| 7173 | struct extent_map_tree *em_tree; |
| 7174 | struct extent_map *em; |
| 7175 | struct btrfs_root *root = BTRFS_I(inode)->root; |
| 7176 | int ret; |
| 7177 | |
| 7178 | em_tree = &BTRFS_I(inode)->extent_tree; |
| 7179 | em = alloc_extent_map(); |
| 7180 | if (!em) |
| 7181 | return ERR_PTR(-ENOMEM); |
| 7182 | |
| 7183 | em->start = start; |
| 7184 | em->orig_start = orig_start; |
Josef Bacik | 2ab28f3 | 2012-10-12 15:27:49 -0400 | [diff] [blame] | 7185 | em->mod_start = start; |
| 7186 | em->mod_len = len; |
Josef Bacik | 69ffb54 | 2012-09-11 15:40:07 -0400 | [diff] [blame] | 7187 | em->len = len; |
| 7188 | em->block_len = block_len; |
| 7189 | em->block_start = block_start; |
| 7190 | em->bdev = root->fs_info->fs_devices->latest_bdev; |
Josef Bacik | b493968 | 2012-12-03 10:31:19 -0500 | [diff] [blame] | 7191 | em->orig_block_len = orig_block_len; |
Josef Bacik | cc95bef | 2013-04-04 14:31:27 -0400 | [diff] [blame] | 7192 | em->ram_bytes = ram_bytes; |
Josef Bacik | 70c8a91 | 2012-10-11 16:54:30 -0400 | [diff] [blame] | 7193 | em->generation = -1; |
Josef Bacik | 69ffb54 | 2012-09-11 15:40:07 -0400 | [diff] [blame] | 7194 | set_bit(EXTENT_FLAG_PINNED, &em->flags); |
| 7195 | if (type == BTRFS_ORDERED_PREALLOC) |
Josef Bacik | b11e234 | 2012-12-03 10:58:15 -0500 | [diff] [blame] | 7196 | set_bit(EXTENT_FLAG_FILLING, &em->flags); |
Josef Bacik | 69ffb54 | 2012-09-11 15:40:07 -0400 | [diff] [blame] | 7197 | |
| 7198 | do { |
| 7199 | btrfs_drop_extent_cache(inode, em->start, |
| 7200 | em->start + em->len - 1, 0); |
| 7201 | write_lock(&em_tree->lock); |
Josef Bacik | 09a2a8f9 | 2013-04-05 16:51:15 -0400 | [diff] [blame] | 7202 | ret = add_extent_mapping(em_tree, em, 1); |
Josef Bacik | 69ffb54 | 2012-09-11 15:40:07 -0400 | [diff] [blame] | 7203 | write_unlock(&em_tree->lock); |
| 7204 | } while (ret == -EEXIST); |
| 7205 | |
| 7206 | if (ret) { |
| 7207 | free_extent_map(em); |
| 7208 | return ERR_PTR(ret); |
| 7209 | } |
| 7210 | |
| 7211 | return em; |
| 7212 | } |
| 7213 | |
| 7214 | |
Josef Bacik | 4b46fce | 2010-05-23 11:00:55 -0400 | [diff] [blame] | 7215 | static int btrfs_get_blocks_direct(struct inode *inode, sector_t iblock, |
| 7216 | struct buffer_head *bh_result, int create) |
| 7217 | { |
| 7218 | struct extent_map *em; |
| 7219 | struct btrfs_root *root = BTRFS_I(inode)->root; |
Josef Bacik | eb838e7 | 2012-07-31 16:28:48 -0400 | [diff] [blame] | 7220 | struct extent_state *cached_state = NULL; |
Josef Bacik | 4b46fce | 2010-05-23 11:00:55 -0400 | [diff] [blame] | 7221 | u64 start = iblock << inode->i_blkbits; |
Josef Bacik | eb838e7 | 2012-07-31 16:28:48 -0400 | [diff] [blame] | 7222 | u64 lockstart, lockend; |
Josef Bacik | 4b46fce | 2010-05-23 11:00:55 -0400 | [diff] [blame] | 7223 | u64 len = bh_result->b_size; |
Josef Bacik | 3e05bde | 2015-02-11 15:08:57 -0500 | [diff] [blame] | 7224 | u64 orig_len = len; |
Josef Bacik | eb838e7 | 2012-07-31 16:28:48 -0400 | [diff] [blame] | 7225 | int unlock_bits = EXTENT_LOCKED; |
Miao Xie | 0934856 | 2013-02-07 10:12:07 +0000 | [diff] [blame] | 7226 | int ret = 0; |
Josef Bacik | eb838e7 | 2012-07-31 16:28:48 -0400 | [diff] [blame] | 7227 | |
Miao Xie | 172a504 | 2013-02-21 02:48:22 -0700 | [diff] [blame] | 7228 | if (create) |
Josef Bacik | 3266789 | 2015-02-11 15:08:58 -0500 | [diff] [blame] | 7229 | unlock_bits |= EXTENT_DIRTY; |
Miao Xie | 172a504 | 2013-02-21 02:48:22 -0700 | [diff] [blame] | 7230 | else |
Josef Bacik | c329861 | 2012-08-03 16:49:19 -0400 | [diff] [blame] | 7231 | len = min_t(u64, len, root->sectorsize); |
Josef Bacik | eb838e7 | 2012-07-31 16:28:48 -0400 | [diff] [blame] | 7232 | |
Josef Bacik | c329861 | 2012-08-03 16:49:19 -0400 | [diff] [blame] | 7233 | lockstart = start; |
| 7234 | lockend = start + len - 1; |
| 7235 | |
Josef Bacik | eb838e7 | 2012-07-31 16:28:48 -0400 | [diff] [blame] | 7236 | /* |
| 7237 | * If this errors out it's because we couldn't invalidate pagecache for |
| 7238 | * this range and we need to fallback to buffered. |
| 7239 | */ |
| 7240 | if (lock_extent_direct(inode, lockstart, lockend, &cached_state, create)) |
| 7241 | return -ENOTBLK; |
| 7242 | |
Josef Bacik | 4b46fce | 2010-05-23 11:00:55 -0400 | [diff] [blame] | 7243 | em = btrfs_get_extent(inode, NULL, 0, start, len, 0); |
Josef Bacik | eb838e7 | 2012-07-31 16:28:48 -0400 | [diff] [blame] | 7244 | if (IS_ERR(em)) { |
| 7245 | ret = PTR_ERR(em); |
| 7246 | goto unlock_err; |
| 7247 | } |
Josef Bacik | 4b46fce | 2010-05-23 11:00:55 -0400 | [diff] [blame] | 7248 | |
| 7249 | /* |
| 7250 | * Ok for INLINE and COMPRESSED extents we need to fallback on buffered |
| 7251 | * io. INLINE is special, and we could probably kludge it in here, but |
| 7252 | * it's still buffered so for safety lets just fall back to the generic |
| 7253 | * buffered path. |
| 7254 | * |
| 7255 | * For COMPRESSED we _have_ to read the entire extent in so we can |
| 7256 | * decompress it, so there will be buffering required no matter what we |
| 7257 | * do, so go ahead and fallback to buffered. |
| 7258 | * |
| 7259 | * We return -ENOTBLK because thats what makes DIO go ahead and go back |
| 7260 | * to buffered IO. Don't blame me, this is the price we pay for using |
| 7261 | * the generic code. |
| 7262 | */ |
| 7263 | if (test_bit(EXTENT_FLAG_COMPRESSED, &em->flags) || |
| 7264 | em->block_start == EXTENT_MAP_INLINE) { |
| 7265 | free_extent_map(em); |
Josef Bacik | eb838e7 | 2012-07-31 16:28:48 -0400 | [diff] [blame] | 7266 | ret = -ENOTBLK; |
| 7267 | goto unlock_err; |
Josef Bacik | 4b46fce | 2010-05-23 11:00:55 -0400 | [diff] [blame] | 7268 | } |
| 7269 | |
| 7270 | /* Just a good old fashioned hole, return */ |
| 7271 | if (!create && (em->block_start == EXTENT_MAP_HOLE || |
| 7272 | test_bit(EXTENT_FLAG_PREALLOC, &em->flags))) { |
| 7273 | free_extent_map(em); |
Josef Bacik | eb838e7 | 2012-07-31 16:28:48 -0400 | [diff] [blame] | 7274 | goto unlock_err; |
Josef Bacik | 4b46fce | 2010-05-23 11:00:55 -0400 | [diff] [blame] | 7275 | } |
| 7276 | |
| 7277 | /* |
| 7278 | * We don't allocate a new extent in the following cases |
| 7279 | * |
| 7280 | * 1) The inode is marked as NODATACOW. In this case we'll just use the |
| 7281 | * existing extent. |
| 7282 | * 2) The extent is marked as PREALLOC. We're good to go here and can |
| 7283 | * just use the extent. |
| 7284 | * |
| 7285 | */ |
Chris Mason | 46bfbb5 | 2010-05-26 11:04:10 -0400 | [diff] [blame] | 7286 | if (!create) { |
Josef Bacik | eb838e7 | 2012-07-31 16:28:48 -0400 | [diff] [blame] | 7287 | len = min(len, em->len - (start - em->start)); |
| 7288 | lockstart = start + len; |
| 7289 | goto unlock; |
Chris Mason | 46bfbb5 | 2010-05-26 11:04:10 -0400 | [diff] [blame] | 7290 | } |
Josef Bacik | 4b46fce | 2010-05-23 11:00:55 -0400 | [diff] [blame] | 7291 | |
| 7292 | if (test_bit(EXTENT_FLAG_PREALLOC, &em->flags) || |
| 7293 | ((BTRFS_I(inode)->flags & BTRFS_INODE_NODATACOW) && |
| 7294 | em->block_start != EXTENT_MAP_HOLE)) { |
Josef Bacik | 4b46fce | 2010-05-23 11:00:55 -0400 | [diff] [blame] | 7295 | int type; |
Josef Bacik | eb384b5 | 2013-04-24 16:32:55 -0400 | [diff] [blame] | 7296 | u64 block_start, orig_start, orig_block_len, ram_bytes; |
Josef Bacik | 4b46fce | 2010-05-23 11:00:55 -0400 | [diff] [blame] | 7297 | |
| 7298 | if (test_bit(EXTENT_FLAG_PREALLOC, &em->flags)) |
| 7299 | type = BTRFS_ORDERED_PREALLOC; |
| 7300 | else |
| 7301 | type = BTRFS_ORDERED_NOCOW; |
Chris Mason | 46bfbb5 | 2010-05-26 11:04:10 -0400 | [diff] [blame] | 7302 | len = min(len, em->len - (start - em->start)); |
Josef Bacik | 4b46fce | 2010-05-23 11:00:55 -0400 | [diff] [blame] | 7303 | block_start = em->block_start + (start - em->start); |
Chris Mason | 46bfbb5 | 2010-05-26 11:04:10 -0400 | [diff] [blame] | 7304 | |
Josef Bacik | 0036158 | 2013-08-14 14:02:47 -0400 | [diff] [blame] | 7305 | if (can_nocow_extent(inode, start, &len, &orig_start, |
Josef Bacik | 7ee9e44 | 2013-06-21 16:37:03 -0400 | [diff] [blame] | 7306 | &orig_block_len, &ram_bytes) == 1) { |
Josef Bacik | 69ffb54 | 2012-09-11 15:40:07 -0400 | [diff] [blame] | 7307 | if (type == BTRFS_ORDERED_PREALLOC) { |
| 7308 | free_extent_map(em); |
| 7309 | em = create_pinned_em(inode, start, len, |
| 7310 | orig_start, |
Josef Bacik | b493968 | 2012-12-03 10:31:19 -0500 | [diff] [blame] | 7311 | block_start, len, |
Josef Bacik | cc95bef | 2013-04-04 14:31:27 -0400 | [diff] [blame] | 7312 | orig_block_len, |
| 7313 | ram_bytes, type); |
Filipe Manana | 555e128 | 2014-07-07 12:35:21 +0100 | [diff] [blame] | 7314 | if (IS_ERR(em)) { |
| 7315 | ret = PTR_ERR(em); |
Josef Bacik | 69ffb54 | 2012-09-11 15:40:07 -0400 | [diff] [blame] | 7316 | goto unlock_err; |
Filipe Manana | 555e128 | 2014-07-07 12:35:21 +0100 | [diff] [blame] | 7317 | } |
Josef Bacik | 69ffb54 | 2012-09-11 15:40:07 -0400 | [diff] [blame] | 7318 | } |
| 7319 | |
Chris Mason | 46bfbb5 | 2010-05-26 11:04:10 -0400 | [diff] [blame] | 7320 | ret = btrfs_add_ordered_extent_dio(inode, start, |
| 7321 | block_start, len, len, type); |
Chris Mason | 46bfbb5 | 2010-05-26 11:04:10 -0400 | [diff] [blame] | 7322 | if (ret) { |
| 7323 | free_extent_map(em); |
Josef Bacik | eb838e7 | 2012-07-31 16:28:48 -0400 | [diff] [blame] | 7324 | goto unlock_err; |
Chris Mason | 46bfbb5 | 2010-05-26 11:04:10 -0400 | [diff] [blame] | 7325 | } |
| 7326 | goto unlock; |
Josef Bacik | 4b46fce | 2010-05-23 11:00:55 -0400 | [diff] [blame] | 7327 | } |
Josef Bacik | 4b46fce | 2010-05-23 11:00:55 -0400 | [diff] [blame] | 7328 | } |
Josef Bacik | 0036158 | 2013-08-14 14:02:47 -0400 | [diff] [blame] | 7329 | |
Chris Mason | 46bfbb5 | 2010-05-26 11:04:10 -0400 | [diff] [blame] | 7330 | /* |
| 7331 | * this will cow the extent, reset the len in case we changed |
| 7332 | * it above |
| 7333 | */ |
| 7334 | len = bh_result->b_size; |
Josef Bacik | 70c8a91 | 2012-10-11 16:54:30 -0400 | [diff] [blame] | 7335 | free_extent_map(em); |
| 7336 | em = btrfs_new_extent_direct(inode, start, len); |
Josef Bacik | eb838e7 | 2012-07-31 16:28:48 -0400 | [diff] [blame] | 7337 | if (IS_ERR(em)) { |
| 7338 | ret = PTR_ERR(em); |
| 7339 | goto unlock_err; |
| 7340 | } |
Chris Mason | 46bfbb5 | 2010-05-26 11:04:10 -0400 | [diff] [blame] | 7341 | len = min(len, em->len - (start - em->start)); |
| 7342 | unlock: |
Josef Bacik | 4b46fce | 2010-05-23 11:00:55 -0400 | [diff] [blame] | 7343 | bh_result->b_blocknr = (em->block_start + (start - em->start)) >> |
| 7344 | inode->i_blkbits; |
Chris Mason | 46bfbb5 | 2010-05-26 11:04:10 -0400 | [diff] [blame] | 7345 | bh_result->b_size = len; |
Josef Bacik | 4b46fce | 2010-05-23 11:00:55 -0400 | [diff] [blame] | 7346 | bh_result->b_bdev = em->bdev; |
| 7347 | set_buffer_mapped(bh_result); |
Josef Bacik | c3473e8 | 2012-06-19 10:59:00 -0400 | [diff] [blame] | 7348 | if (create) { |
| 7349 | if (!test_bit(EXTENT_FLAG_PREALLOC, &em->flags)) |
| 7350 | set_buffer_new(bh_result); |
| 7351 | |
| 7352 | /* |
| 7353 | * Need to update the i_size under the extent lock so buffered |
| 7354 | * readers will get the updated i_size when we unlock. |
| 7355 | */ |
| 7356 | if (start + len > i_size_read(inode)) |
| 7357 | i_size_write(inode, start + len); |
Miao Xie | 0934856 | 2013-02-07 10:12:07 +0000 | [diff] [blame] | 7358 | |
Josef Bacik | 3e05bde | 2015-02-11 15:08:57 -0500 | [diff] [blame] | 7359 | if (len < orig_len) { |
| 7360 | spin_lock(&BTRFS_I(inode)->lock); |
| 7361 | BTRFS_I(inode)->outstanding_extents++; |
| 7362 | spin_unlock(&BTRFS_I(inode)->lock); |
| 7363 | } |
Josef Bacik | 3266789 | 2015-02-11 15:08:58 -0500 | [diff] [blame] | 7364 | btrfs_free_reserved_data_space(inode, len); |
Josef Bacik | c3473e8 | 2012-06-19 10:59:00 -0400 | [diff] [blame] | 7365 | } |
Josef Bacik | 4b46fce | 2010-05-23 11:00:55 -0400 | [diff] [blame] | 7366 | |
Josef Bacik | eb838e7 | 2012-07-31 16:28:48 -0400 | [diff] [blame] | 7367 | /* |
| 7368 | * In the case of write we need to clear and unlock the entire range, |
| 7369 | * in the case of read we need to unlock only the end area that we |
| 7370 | * aren't using if there is any left over space. |
| 7371 | */ |
Liu Bo | 24c03fa | 2012-08-22 20:10:38 -0600 | [diff] [blame] | 7372 | if (lockstart < lockend) { |
Miao Xie | 0934856 | 2013-02-07 10:12:07 +0000 | [diff] [blame] | 7373 | clear_extent_bit(&BTRFS_I(inode)->io_tree, lockstart, |
| 7374 | lockend, unlock_bits, 1, 0, |
| 7375 | &cached_state, GFP_NOFS); |
Liu Bo | 24c03fa | 2012-08-22 20:10:38 -0600 | [diff] [blame] | 7376 | } else { |
Josef Bacik | eb838e7 | 2012-07-31 16:28:48 -0400 | [diff] [blame] | 7377 | free_extent_state(cached_state); |
Liu Bo | 24c03fa | 2012-08-22 20:10:38 -0600 | [diff] [blame] | 7378 | } |
Josef Bacik | eb838e7 | 2012-07-31 16:28:48 -0400 | [diff] [blame] | 7379 | |
Josef Bacik | 4b46fce | 2010-05-23 11:00:55 -0400 | [diff] [blame] | 7380 | free_extent_map(em); |
| 7381 | |
| 7382 | return 0; |
Josef Bacik | eb838e7 | 2012-07-31 16:28:48 -0400 | [diff] [blame] | 7383 | |
| 7384 | unlock_err: |
Josef Bacik | eb838e7 | 2012-07-31 16:28:48 -0400 | [diff] [blame] | 7385 | clear_extent_bit(&BTRFS_I(inode)->io_tree, lockstart, lockend, |
| 7386 | unlock_bits, 1, 0, &cached_state, GFP_NOFS); |
| 7387 | return ret; |
Josef Bacik | 4b46fce | 2010-05-23 11:00:55 -0400 | [diff] [blame] | 7388 | } |
| 7389 | |
Miao Xie | 8b110e3 | 2014-09-12 18:44:03 +0800 | [diff] [blame] | 7390 | static inline int submit_dio_repair_bio(struct inode *inode, struct bio *bio, |
| 7391 | int rw, int mirror_num) |
| 7392 | { |
| 7393 | struct btrfs_root *root = BTRFS_I(inode)->root; |
| 7394 | int ret; |
| 7395 | |
| 7396 | BUG_ON(rw & REQ_WRITE); |
| 7397 | |
| 7398 | bio_get(bio); |
| 7399 | |
| 7400 | ret = btrfs_bio_wq_end_io(root->fs_info, bio, |
| 7401 | BTRFS_WQ_ENDIO_DIO_REPAIR); |
| 7402 | if (ret) |
| 7403 | goto err; |
| 7404 | |
| 7405 | ret = btrfs_map_bio(root, rw, bio, mirror_num, 0); |
| 7406 | err: |
| 7407 | bio_put(bio); |
| 7408 | return ret; |
| 7409 | } |
| 7410 | |
| 7411 | static int btrfs_check_dio_repairable(struct inode *inode, |
| 7412 | struct bio *failed_bio, |
| 7413 | struct io_failure_record *failrec, |
| 7414 | int failed_mirror) |
| 7415 | { |
| 7416 | int num_copies; |
| 7417 | |
| 7418 | num_copies = btrfs_num_copies(BTRFS_I(inode)->root->fs_info, |
| 7419 | failrec->logical, failrec->len); |
| 7420 | if (num_copies == 1) { |
| 7421 | /* |
| 7422 | * we only have a single copy of the data, so don't bother with |
| 7423 | * all the retry and error correction code that follows. no |
| 7424 | * matter what the error is, it is very likely to persist. |
| 7425 | */ |
| 7426 | pr_debug("Check DIO Repairable: cannot repair, num_copies=%d, next_mirror %d, failed_mirror %d\n", |
| 7427 | num_copies, failrec->this_mirror, failed_mirror); |
| 7428 | return 0; |
| 7429 | } |
| 7430 | |
| 7431 | failrec->failed_mirror = failed_mirror; |
| 7432 | failrec->this_mirror++; |
| 7433 | if (failrec->this_mirror == failed_mirror) |
| 7434 | failrec->this_mirror++; |
| 7435 | |
| 7436 | if (failrec->this_mirror > num_copies) { |
| 7437 | pr_debug("Check DIO Repairable: (fail) num_copies=%d, next_mirror %d, failed_mirror %d\n", |
| 7438 | num_copies, failrec->this_mirror, failed_mirror); |
| 7439 | return 0; |
| 7440 | } |
| 7441 | |
| 7442 | return 1; |
| 7443 | } |
| 7444 | |
| 7445 | static int dio_read_error(struct inode *inode, struct bio *failed_bio, |
| 7446 | struct page *page, u64 start, u64 end, |
| 7447 | int failed_mirror, bio_end_io_t *repair_endio, |
| 7448 | void *repair_arg) |
| 7449 | { |
| 7450 | struct io_failure_record *failrec; |
| 7451 | struct bio *bio; |
| 7452 | int isector; |
| 7453 | int read_mode; |
| 7454 | int ret; |
| 7455 | |
| 7456 | BUG_ON(failed_bio->bi_rw & REQ_WRITE); |
| 7457 | |
| 7458 | ret = btrfs_get_io_failure_record(inode, start, end, &failrec); |
| 7459 | if (ret) |
| 7460 | return ret; |
| 7461 | |
| 7462 | ret = btrfs_check_dio_repairable(inode, failed_bio, failrec, |
| 7463 | failed_mirror); |
| 7464 | if (!ret) { |
| 7465 | free_io_failure(inode, failrec); |
| 7466 | return -EIO; |
| 7467 | } |
| 7468 | |
| 7469 | if (failed_bio->bi_vcnt > 1) |
| 7470 | read_mode = READ_SYNC | REQ_FAILFAST_DEV; |
| 7471 | else |
| 7472 | read_mode = READ_SYNC; |
| 7473 | |
| 7474 | isector = start - btrfs_io_bio(failed_bio)->logical; |
| 7475 | isector >>= inode->i_sb->s_blocksize_bits; |
| 7476 | bio = btrfs_create_repair_bio(inode, failed_bio, failrec, page, |
| 7477 | 0, isector, repair_endio, repair_arg); |
| 7478 | if (!bio) { |
| 7479 | free_io_failure(inode, failrec); |
| 7480 | return -EIO; |
| 7481 | } |
| 7482 | |
| 7483 | btrfs_debug(BTRFS_I(inode)->root->fs_info, |
| 7484 | "Repair DIO Read Error: submitting new dio read[%#x] to this_mirror=%d, in_validation=%d\n", |
| 7485 | read_mode, failrec->this_mirror, failrec->in_validation); |
| 7486 | |
| 7487 | ret = submit_dio_repair_bio(inode, bio, read_mode, |
| 7488 | failrec->this_mirror); |
| 7489 | if (ret) { |
| 7490 | free_io_failure(inode, failrec); |
| 7491 | bio_put(bio); |
| 7492 | } |
| 7493 | |
| 7494 | return ret; |
| 7495 | } |
| 7496 | |
| 7497 | struct btrfs_retry_complete { |
| 7498 | struct completion done; |
| 7499 | struct inode *inode; |
| 7500 | u64 start; |
| 7501 | int uptodate; |
| 7502 | }; |
| 7503 | |
| 7504 | static void btrfs_retry_endio_nocsum(struct bio *bio, int err) |
| 7505 | { |
| 7506 | struct btrfs_retry_complete *done = bio->bi_private; |
| 7507 | struct bio_vec *bvec; |
| 7508 | int i; |
| 7509 | |
| 7510 | if (err) |
| 7511 | goto end; |
| 7512 | |
| 7513 | done->uptodate = 1; |
| 7514 | bio_for_each_segment_all(bvec, bio, i) |
| 7515 | clean_io_failure(done->inode, done->start, bvec->bv_page, 0); |
| 7516 | end: |
| 7517 | complete(&done->done); |
| 7518 | bio_put(bio); |
| 7519 | } |
| 7520 | |
| 7521 | static int __btrfs_correct_data_nocsum(struct inode *inode, |
| 7522 | struct btrfs_io_bio *io_bio) |
Josef Bacik | 4b46fce | 2010-05-23 11:00:55 -0400 | [diff] [blame] | 7523 | { |
Kent Overstreet | 2c30c71 | 2013-11-07 12:20:26 -0800 | [diff] [blame] | 7524 | struct bio_vec *bvec; |
Miao Xie | 8b110e3 | 2014-09-12 18:44:03 +0800 | [diff] [blame] | 7525 | struct btrfs_retry_complete done; |
Josef Bacik | 4b46fce | 2010-05-23 11:00:55 -0400 | [diff] [blame] | 7526 | u64 start; |
Kent Overstreet | 2c30c71 | 2013-11-07 12:20:26 -0800 | [diff] [blame] | 7527 | int i; |
Miao Xie | c1dc089 | 2014-09-12 18:43:56 +0800 | [diff] [blame] | 7528 | int ret; |
Miao Xie | dc380ae | 2014-09-12 18:43:55 +0800 | [diff] [blame] | 7529 | |
Miao Xie | c1dc089 | 2014-09-12 18:43:56 +0800 | [diff] [blame] | 7530 | start = io_bio->logical; |
Miao Xie | 8b110e3 | 2014-09-12 18:44:03 +0800 | [diff] [blame] | 7531 | done.inode = inode; |
| 7532 | |
| 7533 | bio_for_each_segment_all(bvec, &io_bio->bio, i) { |
| 7534 | try_again: |
| 7535 | done.uptodate = 0; |
| 7536 | done.start = start; |
| 7537 | init_completion(&done.done); |
| 7538 | |
| 7539 | ret = dio_read_error(inode, &io_bio->bio, bvec->bv_page, start, |
| 7540 | start + bvec->bv_len - 1, |
| 7541 | io_bio->mirror_num, |
| 7542 | btrfs_retry_endio_nocsum, &done); |
| 7543 | if (ret) |
| 7544 | return ret; |
| 7545 | |
| 7546 | wait_for_completion(&done.done); |
| 7547 | |
| 7548 | if (!done.uptodate) { |
| 7549 | /* We might have another mirror, so try again */ |
| 7550 | goto try_again; |
| 7551 | } |
| 7552 | |
| 7553 | start += bvec->bv_len; |
| 7554 | } |
| 7555 | |
| 7556 | return 0; |
| 7557 | } |
| 7558 | |
| 7559 | static void btrfs_retry_endio(struct bio *bio, int err) |
| 7560 | { |
| 7561 | struct btrfs_retry_complete *done = bio->bi_private; |
| 7562 | struct btrfs_io_bio *io_bio = btrfs_io_bio(bio); |
| 7563 | struct bio_vec *bvec; |
| 7564 | int uptodate; |
| 7565 | int ret; |
| 7566 | int i; |
| 7567 | |
| 7568 | if (err) |
| 7569 | goto end; |
| 7570 | |
| 7571 | uptodate = 1; |
| 7572 | bio_for_each_segment_all(bvec, bio, i) { |
| 7573 | ret = __readpage_endio_check(done->inode, io_bio, i, |
| 7574 | bvec->bv_page, 0, |
| 7575 | done->start, bvec->bv_len); |
| 7576 | if (!ret) |
| 7577 | clean_io_failure(done->inode, done->start, |
| 7578 | bvec->bv_page, 0); |
| 7579 | else |
| 7580 | uptodate = 0; |
| 7581 | } |
| 7582 | |
| 7583 | done->uptodate = uptodate; |
| 7584 | end: |
| 7585 | complete(&done->done); |
| 7586 | bio_put(bio); |
| 7587 | } |
| 7588 | |
| 7589 | static int __btrfs_subio_endio_read(struct inode *inode, |
| 7590 | struct btrfs_io_bio *io_bio, int err) |
| 7591 | { |
| 7592 | struct bio_vec *bvec; |
| 7593 | struct btrfs_retry_complete done; |
| 7594 | u64 start; |
| 7595 | u64 offset = 0; |
| 7596 | int i; |
| 7597 | int ret; |
| 7598 | |
| 7599 | err = 0; |
| 7600 | start = io_bio->logical; |
| 7601 | done.inode = inode; |
| 7602 | |
Miao Xie | c1dc089 | 2014-09-12 18:43:56 +0800 | [diff] [blame] | 7603 | bio_for_each_segment_all(bvec, &io_bio->bio, i) { |
Miao Xie | dc380ae | 2014-09-12 18:43:55 +0800 | [diff] [blame] | 7604 | ret = __readpage_endio_check(inode, io_bio, i, bvec->bv_page, |
| 7605 | 0, start, bvec->bv_len); |
Miao Xie | 8b110e3 | 2014-09-12 18:44:03 +0800 | [diff] [blame] | 7606 | if (likely(!ret)) |
| 7607 | goto next; |
| 7608 | try_again: |
| 7609 | done.uptodate = 0; |
| 7610 | done.start = start; |
| 7611 | init_completion(&done.done); |
| 7612 | |
| 7613 | ret = dio_read_error(inode, &io_bio->bio, bvec->bv_page, start, |
| 7614 | start + bvec->bv_len - 1, |
| 7615 | io_bio->mirror_num, |
| 7616 | btrfs_retry_endio, &done); |
| 7617 | if (ret) { |
| 7618 | err = ret; |
| 7619 | goto next; |
| 7620 | } |
| 7621 | |
| 7622 | wait_for_completion(&done.done); |
| 7623 | |
| 7624 | if (!done.uptodate) { |
| 7625 | /* We might have another mirror, so try again */ |
| 7626 | goto try_again; |
| 7627 | } |
| 7628 | next: |
| 7629 | offset += bvec->bv_len; |
Josef Bacik | 4b46fce | 2010-05-23 11:00:55 -0400 | [diff] [blame] | 7630 | start += bvec->bv_len; |
Kent Overstreet | 2c30c71 | 2013-11-07 12:20:26 -0800 | [diff] [blame] | 7631 | } |
Miao Xie | c1dc089 | 2014-09-12 18:43:56 +0800 | [diff] [blame] | 7632 | |
| 7633 | return err; |
| 7634 | } |
| 7635 | |
Miao Xie | 8b110e3 | 2014-09-12 18:44:03 +0800 | [diff] [blame] | 7636 | static int btrfs_subio_endio_read(struct inode *inode, |
| 7637 | struct btrfs_io_bio *io_bio, int err) |
| 7638 | { |
| 7639 | bool skip_csum = BTRFS_I(inode)->flags & BTRFS_INODE_NODATASUM; |
| 7640 | |
| 7641 | if (skip_csum) { |
| 7642 | if (unlikely(err)) |
| 7643 | return __btrfs_correct_data_nocsum(inode, io_bio); |
| 7644 | else |
| 7645 | return 0; |
| 7646 | } else { |
| 7647 | return __btrfs_subio_endio_read(inode, io_bio, err); |
| 7648 | } |
| 7649 | } |
| 7650 | |
Miao Xie | c1dc089 | 2014-09-12 18:43:56 +0800 | [diff] [blame] | 7651 | static void btrfs_endio_direct_read(struct bio *bio, int err) |
| 7652 | { |
| 7653 | struct btrfs_dio_private *dip = bio->bi_private; |
| 7654 | struct inode *inode = dip->inode; |
| 7655 | struct bio *dio_bio; |
| 7656 | struct btrfs_io_bio *io_bio = btrfs_io_bio(bio); |
| 7657 | |
Miao Xie | 8b110e3 | 2014-09-12 18:44:03 +0800 | [diff] [blame] | 7658 | if (dip->flags & BTRFS_DIO_ORIG_BIO_SUBMITTED) |
| 7659 | err = btrfs_subio_endio_read(inode, io_bio, err); |
Miao Xie | c1dc089 | 2014-09-12 18:43:56 +0800 | [diff] [blame] | 7660 | |
Josef Bacik | 4b46fce | 2010-05-23 11:00:55 -0400 | [diff] [blame] | 7661 | unlock_extent(&BTRFS_I(inode)->io_tree, dip->logical_offset, |
Jeff Mahoney | d008237 | 2012-03-01 14:57:19 +0100 | [diff] [blame] | 7662 | dip->logical_offset + dip->bytes - 1); |
Chris Mason | 9be3395 | 2013-05-17 18:30:14 -0400 | [diff] [blame] | 7663 | dio_bio = dip->dio_bio; |
Josef Bacik | 4b46fce | 2010-05-23 11:00:55 -0400 | [diff] [blame] | 7664 | |
Josef Bacik | 4b46fce | 2010-05-23 11:00:55 -0400 | [diff] [blame] | 7665 | kfree(dip); |
Josef Bacik | c0da7aa | 2011-03-22 11:05:07 -0400 | [diff] [blame] | 7666 | |
| 7667 | /* If we had a csum failure make sure to clear the uptodate flag */ |
| 7668 | if (err) |
Chris Mason | 9be3395 | 2013-05-17 18:30:14 -0400 | [diff] [blame] | 7669 | clear_bit(BIO_UPTODATE, &dio_bio->bi_flags); |
| 7670 | dio_end_io(dio_bio, err); |
Miao Xie | 23ea8e5 | 2014-09-12 18:43:54 +0800 | [diff] [blame] | 7671 | |
| 7672 | if (io_bio->end_io) |
| 7673 | io_bio->end_io(io_bio, err); |
Chris Mason | 9be3395 | 2013-05-17 18:30:14 -0400 | [diff] [blame] | 7674 | bio_put(bio); |
Josef Bacik | 4b46fce | 2010-05-23 11:00:55 -0400 | [diff] [blame] | 7675 | } |
| 7676 | |
| 7677 | static void btrfs_endio_direct_write(struct bio *bio, int err) |
| 7678 | { |
| 7679 | struct btrfs_dio_private *dip = bio->bi_private; |
| 7680 | struct inode *inode = dip->inode; |
| 7681 | struct btrfs_root *root = BTRFS_I(inode)->root; |
Josef Bacik | 4b46fce | 2010-05-23 11:00:55 -0400 | [diff] [blame] | 7682 | struct btrfs_ordered_extent *ordered = NULL; |
Chris Mason | 163cf09 | 2010-11-28 19:56:33 -0500 | [diff] [blame] | 7683 | u64 ordered_offset = dip->logical_offset; |
| 7684 | u64 ordered_bytes = dip->bytes; |
Chris Mason | 9be3395 | 2013-05-17 18:30:14 -0400 | [diff] [blame] | 7685 | struct bio *dio_bio; |
Josef Bacik | 4b46fce | 2010-05-23 11:00:55 -0400 | [diff] [blame] | 7686 | int ret; |
| 7687 | |
| 7688 | if (err) |
| 7689 | goto out_done; |
Chris Mason | 163cf09 | 2010-11-28 19:56:33 -0500 | [diff] [blame] | 7690 | again: |
| 7691 | ret = btrfs_dec_test_first_ordered_pending(inode, &ordered, |
| 7692 | &ordered_offset, |
Josef Bacik | 5fd0204 | 2012-05-02 14:00:54 -0400 | [diff] [blame] | 7693 | ordered_bytes, !err); |
Josef Bacik | 4b46fce | 2010-05-23 11:00:55 -0400 | [diff] [blame] | 7694 | if (!ret) |
Chris Mason | 163cf09 | 2010-11-28 19:56:33 -0500 | [diff] [blame] | 7695 | goto out_test; |
Josef Bacik | 4b46fce | 2010-05-23 11:00:55 -0400 | [diff] [blame] | 7696 | |
Liu Bo | 9e0af23 | 2014-08-15 23:36:53 +0800 | [diff] [blame] | 7697 | btrfs_init_work(&ordered->work, btrfs_endio_write_helper, |
| 7698 | finish_ordered_fn, NULL, NULL); |
Qu Wenruo | fccb5d8 | 2014-02-28 10:46:10 +0800 | [diff] [blame] | 7699 | btrfs_queue_work(root->fs_info->endio_write_workers, |
| 7700 | &ordered->work); |
Chris Mason | 163cf09 | 2010-11-28 19:56:33 -0500 | [diff] [blame] | 7701 | out_test: |
| 7702 | /* |
| 7703 | * our bio might span multiple ordered extents. If we haven't |
| 7704 | * completed the accounting for the whole dio, go back and try again |
| 7705 | */ |
| 7706 | if (ordered_offset < dip->logical_offset + dip->bytes) { |
| 7707 | ordered_bytes = dip->logical_offset + dip->bytes - |
| 7708 | ordered_offset; |
Josef Bacik | 5fd0204 | 2012-05-02 14:00:54 -0400 | [diff] [blame] | 7709 | ordered = NULL; |
Chris Mason | 163cf09 | 2010-11-28 19:56:33 -0500 | [diff] [blame] | 7710 | goto again; |
| 7711 | } |
Josef Bacik | 4b46fce | 2010-05-23 11:00:55 -0400 | [diff] [blame] | 7712 | out_done: |
Chris Mason | 9be3395 | 2013-05-17 18:30:14 -0400 | [diff] [blame] | 7713 | dio_bio = dip->dio_bio; |
Josef Bacik | 4b46fce | 2010-05-23 11:00:55 -0400 | [diff] [blame] | 7714 | |
Josef Bacik | 4b46fce | 2010-05-23 11:00:55 -0400 | [diff] [blame] | 7715 | kfree(dip); |
Josef Bacik | c0da7aa | 2011-03-22 11:05:07 -0400 | [diff] [blame] | 7716 | |
| 7717 | /* If we had an error make sure to clear the uptodate flag */ |
| 7718 | if (err) |
Chris Mason | 9be3395 | 2013-05-17 18:30:14 -0400 | [diff] [blame] | 7719 | clear_bit(BIO_UPTODATE, &dio_bio->bi_flags); |
| 7720 | dio_end_io(dio_bio, err); |
| 7721 | bio_put(bio); |
Josef Bacik | 4b46fce | 2010-05-23 11:00:55 -0400 | [diff] [blame] | 7722 | } |
| 7723 | |
Chris Mason | eaf25d9 | 2010-05-25 09:48:28 -0400 | [diff] [blame] | 7724 | static int __btrfs_submit_bio_start_direct_io(struct inode *inode, int rw, |
| 7725 | struct bio *bio, int mirror_num, |
| 7726 | unsigned long bio_flags, u64 offset) |
| 7727 | { |
| 7728 | int ret; |
| 7729 | struct btrfs_root *root = BTRFS_I(inode)->root; |
| 7730 | ret = btrfs_csum_one_bio(root, inode, bio, offset, 1); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 7731 | BUG_ON(ret); /* -ENOMEM */ |
Chris Mason | eaf25d9 | 2010-05-25 09:48:28 -0400 | [diff] [blame] | 7732 | return 0; |
| 7733 | } |
| 7734 | |
Miao Xie | e65e153 | 2010-11-22 03:04:43 +0000 | [diff] [blame] | 7735 | static void btrfs_end_dio_bio(struct bio *bio, int err) |
| 7736 | { |
| 7737 | struct btrfs_dio_private *dip = bio->bi_private; |
| 7738 | |
Miao Xie | 8b110e3 | 2014-09-12 18:44:03 +0800 | [diff] [blame] | 7739 | if (err) |
| 7740 | btrfs_warn(BTRFS_I(dip->inode)->root->fs_info, |
| 7741 | "direct IO failed ino %llu rw %lu sector %#Lx len %u err no %d", |
| 7742 | btrfs_ino(dip->inode), bio->bi_rw, |
| 7743 | (unsigned long long)bio->bi_iter.bi_sector, |
| 7744 | bio->bi_iter.bi_size, err); |
| 7745 | |
| 7746 | if (dip->subio_endio) |
| 7747 | err = dip->subio_endio(dip->inode, btrfs_io_bio(bio), err); |
Miao Xie | c1dc089 | 2014-09-12 18:43:56 +0800 | [diff] [blame] | 7748 | |
| 7749 | if (err) { |
Miao Xie | e65e153 | 2010-11-22 03:04:43 +0000 | [diff] [blame] | 7750 | dip->errors = 1; |
| 7751 | |
| 7752 | /* |
| 7753 | * before atomic variable goto zero, we must make sure |
| 7754 | * dip->errors is perceived to be set. |
| 7755 | */ |
Peter Zijlstra | 4e857c5 | 2014-03-17 18:06:10 +0100 | [diff] [blame] | 7756 | smp_mb__before_atomic(); |
Miao Xie | e65e153 | 2010-11-22 03:04:43 +0000 | [diff] [blame] | 7757 | } |
| 7758 | |
| 7759 | /* if there are more bios still pending for this dio, just exit */ |
| 7760 | if (!atomic_dec_and_test(&dip->pending_bios)) |
| 7761 | goto out; |
| 7762 | |
Chris Mason | 9be3395 | 2013-05-17 18:30:14 -0400 | [diff] [blame] | 7763 | if (dip->errors) { |
Miao Xie | e65e153 | 2010-11-22 03:04:43 +0000 | [diff] [blame] | 7764 | bio_io_error(dip->orig_bio); |
Chris Mason | 9be3395 | 2013-05-17 18:30:14 -0400 | [diff] [blame] | 7765 | } else { |
| 7766 | set_bit(BIO_UPTODATE, &dip->dio_bio->bi_flags); |
Miao Xie | e65e153 | 2010-11-22 03:04:43 +0000 | [diff] [blame] | 7767 | bio_endio(dip->orig_bio, 0); |
| 7768 | } |
| 7769 | out: |
| 7770 | bio_put(bio); |
| 7771 | } |
| 7772 | |
| 7773 | static struct bio *btrfs_dio_bio_alloc(struct block_device *bdev, |
| 7774 | u64 first_sector, gfp_t gfp_flags) |
| 7775 | { |
| 7776 | int nr_vecs = bio_get_nr_vecs(bdev); |
| 7777 | return btrfs_bio_alloc(bdev, first_sector, nr_vecs, gfp_flags); |
| 7778 | } |
| 7779 | |
Miao Xie | c1dc089 | 2014-09-12 18:43:56 +0800 | [diff] [blame] | 7780 | static inline int btrfs_lookup_and_bind_dio_csum(struct btrfs_root *root, |
| 7781 | struct inode *inode, |
| 7782 | struct btrfs_dio_private *dip, |
| 7783 | struct bio *bio, |
| 7784 | u64 file_offset) |
| 7785 | { |
| 7786 | struct btrfs_io_bio *io_bio = btrfs_io_bio(bio); |
| 7787 | struct btrfs_io_bio *orig_io_bio = btrfs_io_bio(dip->orig_bio); |
| 7788 | int ret; |
| 7789 | |
| 7790 | /* |
| 7791 | * We load all the csum data we need when we submit |
| 7792 | * the first bio to reduce the csum tree search and |
| 7793 | * contention. |
| 7794 | */ |
| 7795 | if (dip->logical_offset == file_offset) { |
| 7796 | ret = btrfs_lookup_bio_sums_dio(root, inode, dip->orig_bio, |
| 7797 | file_offset); |
| 7798 | if (ret) |
| 7799 | return ret; |
| 7800 | } |
| 7801 | |
| 7802 | if (bio == dip->orig_bio) |
| 7803 | return 0; |
| 7804 | |
| 7805 | file_offset -= dip->logical_offset; |
| 7806 | file_offset >>= inode->i_sb->s_blocksize_bits; |
| 7807 | io_bio->csum = (u8 *)(((u32 *)orig_io_bio->csum) + file_offset); |
| 7808 | |
| 7809 | return 0; |
| 7810 | } |
| 7811 | |
Miao Xie | e65e153 | 2010-11-22 03:04:43 +0000 | [diff] [blame] | 7812 | static inline int __btrfs_submit_dio_bio(struct bio *bio, struct inode *inode, |
| 7813 | int rw, u64 file_offset, int skip_sum, |
Josef Bacik | c329861 | 2012-08-03 16:49:19 -0400 | [diff] [blame] | 7814 | int async_submit) |
Miao Xie | e65e153 | 2010-11-22 03:04:43 +0000 | [diff] [blame] | 7815 | { |
Miao Xie | facc8a22 | 2013-07-25 19:22:34 +0800 | [diff] [blame] | 7816 | struct btrfs_dio_private *dip = bio->bi_private; |
Miao Xie | e65e153 | 2010-11-22 03:04:43 +0000 | [diff] [blame] | 7817 | int write = rw & REQ_WRITE; |
| 7818 | struct btrfs_root *root = BTRFS_I(inode)->root; |
| 7819 | int ret; |
| 7820 | |
Josef Bacik | b812ce2 | 2012-11-16 13:56:32 -0500 | [diff] [blame] | 7821 | if (async_submit) |
| 7822 | async_submit = !atomic_read(&BTRFS_I(inode)->sync_writers); |
| 7823 | |
Miao Xie | e65e153 | 2010-11-22 03:04:43 +0000 | [diff] [blame] | 7824 | bio_get(bio); |
Josef Bacik | 5fd0204 | 2012-05-02 14:00:54 -0400 | [diff] [blame] | 7825 | |
| 7826 | if (!write) { |
David Sterba | bfebd8b | 2014-07-30 00:25:45 +0200 | [diff] [blame] | 7827 | ret = btrfs_bio_wq_end_io(root->fs_info, bio, |
| 7828 | BTRFS_WQ_ENDIO_DATA); |
Josef Bacik | 5fd0204 | 2012-05-02 14:00:54 -0400 | [diff] [blame] | 7829 | if (ret) |
| 7830 | goto err; |
| 7831 | } |
Miao Xie | e65e153 | 2010-11-22 03:04:43 +0000 | [diff] [blame] | 7832 | |
Josef Bacik | 1ae3993 | 2011-04-06 14:41:34 -0400 | [diff] [blame] | 7833 | if (skip_sum) |
| 7834 | goto map; |
| 7835 | |
| 7836 | if (write && async_submit) { |
Miao Xie | e65e153 | 2010-11-22 03:04:43 +0000 | [diff] [blame] | 7837 | ret = btrfs_wq_submit_bio(root->fs_info, |
| 7838 | inode, rw, bio, 0, 0, |
| 7839 | file_offset, |
| 7840 | __btrfs_submit_bio_start_direct_io, |
| 7841 | __btrfs_submit_bio_done); |
| 7842 | goto err; |
Josef Bacik | 1ae3993 | 2011-04-06 14:41:34 -0400 | [diff] [blame] | 7843 | } else if (write) { |
| 7844 | /* |
| 7845 | * If we aren't doing async submit, calculate the csum of the |
| 7846 | * bio now. |
| 7847 | */ |
| 7848 | ret = btrfs_csum_one_bio(root, inode, bio, file_offset, 1); |
| 7849 | if (ret) |
| 7850 | goto err; |
Miao Xie | 23ea8e5 | 2014-09-12 18:43:54 +0800 | [diff] [blame] | 7851 | } else { |
Miao Xie | c1dc089 | 2014-09-12 18:43:56 +0800 | [diff] [blame] | 7852 | ret = btrfs_lookup_and_bind_dio_csum(root, inode, dip, bio, |
| 7853 | file_offset); |
Tsutomu Itoh | c2db107 | 2011-03-01 06:48:31 +0000 | [diff] [blame] | 7854 | if (ret) |
| 7855 | goto err; |
| 7856 | } |
Josef Bacik | 1ae3993 | 2011-04-06 14:41:34 -0400 | [diff] [blame] | 7857 | map: |
| 7858 | ret = btrfs_map_bio(root, rw, bio, 0, async_submit); |
Miao Xie | e65e153 | 2010-11-22 03:04:43 +0000 | [diff] [blame] | 7859 | err: |
| 7860 | bio_put(bio); |
| 7861 | return ret; |
| 7862 | } |
| 7863 | |
| 7864 | static int btrfs_submit_direct_hook(int rw, struct btrfs_dio_private *dip, |
| 7865 | int skip_sum) |
| 7866 | { |
| 7867 | struct inode *inode = dip->inode; |
| 7868 | struct btrfs_root *root = BTRFS_I(inode)->root; |
Miao Xie | e65e153 | 2010-11-22 03:04:43 +0000 | [diff] [blame] | 7869 | struct bio *bio; |
| 7870 | struct bio *orig_bio = dip->orig_bio; |
| 7871 | struct bio_vec *bvec = orig_bio->bi_io_vec; |
Kent Overstreet | 4f024f3 | 2013-10-11 15:44:27 -0700 | [diff] [blame] | 7872 | u64 start_sector = orig_bio->bi_iter.bi_sector; |
Miao Xie | e65e153 | 2010-11-22 03:04:43 +0000 | [diff] [blame] | 7873 | u64 file_offset = dip->logical_offset; |
| 7874 | u64 submit_len = 0; |
| 7875 | u64 map_length; |
| 7876 | int nr_pages = 0; |
Miao Xie | 23ea8e5 | 2014-09-12 18:43:54 +0800 | [diff] [blame] | 7877 | int ret; |
Josef Bacik | 1ae3993 | 2011-04-06 14:41:34 -0400 | [diff] [blame] | 7878 | int async_submit = 0; |
Miao Xie | e65e153 | 2010-11-22 03:04:43 +0000 | [diff] [blame] | 7879 | |
Kent Overstreet | 4f024f3 | 2013-10-11 15:44:27 -0700 | [diff] [blame] | 7880 | map_length = orig_bio->bi_iter.bi_size; |
David Woodhouse | 53b381b | 2013-01-29 18:40:14 -0500 | [diff] [blame] | 7881 | ret = btrfs_map_block(root->fs_info, rw, start_sector << 9, |
Miao Xie | e65e153 | 2010-11-22 03:04:43 +0000 | [diff] [blame] | 7882 | &map_length, NULL, 0); |
Miao Xie | 7a5c3c9 | 2014-06-17 18:58:59 +0800 | [diff] [blame] | 7883 | if (ret) |
Miao Xie | e65e153 | 2010-11-22 03:04:43 +0000 | [diff] [blame] | 7884 | return -EIO; |
Miao Xie | facc8a22 | 2013-07-25 19:22:34 +0800 | [diff] [blame] | 7885 | |
Kent Overstreet | 4f024f3 | 2013-10-11 15:44:27 -0700 | [diff] [blame] | 7886 | if (map_length >= orig_bio->bi_iter.bi_size) { |
Josef Bacik | 02f57c7 | 2011-04-06 14:25:44 -0400 | [diff] [blame] | 7887 | bio = orig_bio; |
Miao Xie | c1dc089 | 2014-09-12 18:43:56 +0800 | [diff] [blame] | 7888 | dip->flags |= BTRFS_DIO_ORIG_BIO_SUBMITTED; |
Josef Bacik | 02f57c7 | 2011-04-06 14:25:44 -0400 | [diff] [blame] | 7889 | goto submit; |
| 7890 | } |
| 7891 | |
David Woodhouse | 53b381b | 2013-01-29 18:40:14 -0500 | [diff] [blame] | 7892 | /* async crcs make it difficult to collect full stripe writes. */ |
Zhao Lei | ffe2d20 | 2015-01-20 15:11:44 +0800 | [diff] [blame] | 7893 | if (btrfs_get_alloc_profile(root, 1) & BTRFS_BLOCK_GROUP_RAID56_MASK) |
David Woodhouse | 53b381b | 2013-01-29 18:40:14 -0500 | [diff] [blame] | 7894 | async_submit = 0; |
| 7895 | else |
| 7896 | async_submit = 1; |
| 7897 | |
Josef Bacik | 02f57c7 | 2011-04-06 14:25:44 -0400 | [diff] [blame] | 7898 | bio = btrfs_dio_bio_alloc(orig_bio->bi_bdev, start_sector, GFP_NOFS); |
| 7899 | if (!bio) |
| 7900 | return -ENOMEM; |
Miao Xie | 7a5c3c9 | 2014-06-17 18:58:59 +0800 | [diff] [blame] | 7901 | |
Josef Bacik | 02f57c7 | 2011-04-06 14:25:44 -0400 | [diff] [blame] | 7902 | bio->bi_private = dip; |
| 7903 | bio->bi_end_io = btrfs_end_dio_bio; |
Miao Xie | c1dc089 | 2014-09-12 18:43:56 +0800 | [diff] [blame] | 7904 | btrfs_io_bio(bio)->logical = file_offset; |
Josef Bacik | 02f57c7 | 2011-04-06 14:25:44 -0400 | [diff] [blame] | 7905 | atomic_inc(&dip->pending_bios); |
| 7906 | |
Miao Xie | e65e153 | 2010-11-22 03:04:43 +0000 | [diff] [blame] | 7907 | while (bvec <= (orig_bio->bi_io_vec + orig_bio->bi_vcnt - 1)) { |
David Sterba | ee39b43 | 2014-09-30 01:33:33 +0200 | [diff] [blame] | 7908 | if (map_length < submit_len + bvec->bv_len || |
Miao Xie | e65e153 | 2010-11-22 03:04:43 +0000 | [diff] [blame] | 7909 | bio_add_page(bio, bvec->bv_page, bvec->bv_len, |
David Sterba | ee39b43 | 2014-09-30 01:33:33 +0200 | [diff] [blame] | 7910 | bvec->bv_offset) < bvec->bv_len) { |
Miao Xie | e65e153 | 2010-11-22 03:04:43 +0000 | [diff] [blame] | 7911 | /* |
| 7912 | * inc the count before we submit the bio so |
| 7913 | * we know the end IO handler won't happen before |
| 7914 | * we inc the count. Otherwise, the dip might get freed |
| 7915 | * before we're done setting it up |
| 7916 | */ |
| 7917 | atomic_inc(&dip->pending_bios); |
| 7918 | ret = __btrfs_submit_dio_bio(bio, inode, rw, |
| 7919 | file_offset, skip_sum, |
Josef Bacik | c329861 | 2012-08-03 16:49:19 -0400 | [diff] [blame] | 7920 | async_submit); |
Miao Xie | e65e153 | 2010-11-22 03:04:43 +0000 | [diff] [blame] | 7921 | if (ret) { |
| 7922 | bio_put(bio); |
| 7923 | atomic_dec(&dip->pending_bios); |
| 7924 | goto out_err; |
| 7925 | } |
| 7926 | |
Miao Xie | e65e153 | 2010-11-22 03:04:43 +0000 | [diff] [blame] | 7927 | start_sector += submit_len >> 9; |
| 7928 | file_offset += submit_len; |
| 7929 | |
| 7930 | submit_len = 0; |
| 7931 | nr_pages = 0; |
| 7932 | |
| 7933 | bio = btrfs_dio_bio_alloc(orig_bio->bi_bdev, |
| 7934 | start_sector, GFP_NOFS); |
| 7935 | if (!bio) |
| 7936 | goto out_err; |
| 7937 | bio->bi_private = dip; |
| 7938 | bio->bi_end_io = btrfs_end_dio_bio; |
Miao Xie | c1dc089 | 2014-09-12 18:43:56 +0800 | [diff] [blame] | 7939 | btrfs_io_bio(bio)->logical = file_offset; |
Miao Xie | e65e153 | 2010-11-22 03:04:43 +0000 | [diff] [blame] | 7940 | |
Kent Overstreet | 4f024f3 | 2013-10-11 15:44:27 -0700 | [diff] [blame] | 7941 | map_length = orig_bio->bi_iter.bi_size; |
David Woodhouse | 53b381b | 2013-01-29 18:40:14 -0500 | [diff] [blame] | 7942 | ret = btrfs_map_block(root->fs_info, rw, |
Stefan Behrens | 3ec706c | 2012-11-05 15:46:42 +0100 | [diff] [blame] | 7943 | start_sector << 9, |
Miao Xie | e65e153 | 2010-11-22 03:04:43 +0000 | [diff] [blame] | 7944 | &map_length, NULL, 0); |
| 7945 | if (ret) { |
| 7946 | bio_put(bio); |
| 7947 | goto out_err; |
| 7948 | } |
| 7949 | } else { |
| 7950 | submit_len += bvec->bv_len; |
Dulshani Gunawardhana | 6787125 | 2013-10-31 10:33:04 +0530 | [diff] [blame] | 7951 | nr_pages++; |
Miao Xie | e65e153 | 2010-11-22 03:04:43 +0000 | [diff] [blame] | 7952 | bvec++; |
| 7953 | } |
| 7954 | } |
| 7955 | |
Josef Bacik | 02f57c7 | 2011-04-06 14:25:44 -0400 | [diff] [blame] | 7956 | submit: |
Miao Xie | e65e153 | 2010-11-22 03:04:43 +0000 | [diff] [blame] | 7957 | ret = __btrfs_submit_dio_bio(bio, inode, rw, file_offset, skip_sum, |
Josef Bacik | c329861 | 2012-08-03 16:49:19 -0400 | [diff] [blame] | 7958 | async_submit); |
Miao Xie | e65e153 | 2010-11-22 03:04:43 +0000 | [diff] [blame] | 7959 | if (!ret) |
| 7960 | return 0; |
| 7961 | |
| 7962 | bio_put(bio); |
| 7963 | out_err: |
| 7964 | dip->errors = 1; |
| 7965 | /* |
| 7966 | * before atomic variable goto zero, we must |
| 7967 | * make sure dip->errors is perceived to be set. |
| 7968 | */ |
Peter Zijlstra | 4e857c5 | 2014-03-17 18:06:10 +0100 | [diff] [blame] | 7969 | smp_mb__before_atomic(); |
Miao Xie | e65e153 | 2010-11-22 03:04:43 +0000 | [diff] [blame] | 7970 | if (atomic_dec_and_test(&dip->pending_bios)) |
| 7971 | bio_io_error(dip->orig_bio); |
| 7972 | |
| 7973 | /* bio_end_io() will handle error, so we needn't return it */ |
| 7974 | return 0; |
| 7975 | } |
| 7976 | |
Chris Mason | 9be3395 | 2013-05-17 18:30:14 -0400 | [diff] [blame] | 7977 | static void btrfs_submit_direct(int rw, struct bio *dio_bio, |
| 7978 | struct inode *inode, loff_t file_offset) |
Josef Bacik | 4b46fce | 2010-05-23 11:00:55 -0400 | [diff] [blame] | 7979 | { |
| 7980 | struct btrfs_root *root = BTRFS_I(inode)->root; |
| 7981 | struct btrfs_dio_private *dip; |
Chris Mason | 9be3395 | 2013-05-17 18:30:14 -0400 | [diff] [blame] | 7982 | struct bio *io_bio; |
Miao Xie | 23ea8e5 | 2014-09-12 18:43:54 +0800 | [diff] [blame] | 7983 | struct btrfs_io_bio *btrfs_bio; |
Josef Bacik | 4b46fce | 2010-05-23 11:00:55 -0400 | [diff] [blame] | 7984 | int skip_sum; |
Christoph Hellwig | 7b6d91d | 2010-08-07 18:20:39 +0200 | [diff] [blame] | 7985 | int write = rw & REQ_WRITE; |
Josef Bacik | 4b46fce | 2010-05-23 11:00:55 -0400 | [diff] [blame] | 7986 | int ret = 0; |
| 7987 | |
| 7988 | skip_sum = BTRFS_I(inode)->flags & BTRFS_INODE_NODATASUM; |
| 7989 | |
Chris Mason | 9be3395 | 2013-05-17 18:30:14 -0400 | [diff] [blame] | 7990 | io_bio = btrfs_bio_clone(dio_bio, GFP_NOFS); |
Chris Mason | 9be3395 | 2013-05-17 18:30:14 -0400 | [diff] [blame] | 7991 | if (!io_bio) { |
Josef Bacik | 4b46fce | 2010-05-23 11:00:55 -0400 | [diff] [blame] | 7992 | ret = -ENOMEM; |
| 7993 | goto free_ordered; |
| 7994 | } |
Josef Bacik | 4b46fce | 2010-05-23 11:00:55 -0400 | [diff] [blame] | 7995 | |
Miao Xie | c1dc089 | 2014-09-12 18:43:56 +0800 | [diff] [blame] | 7996 | dip = kzalloc(sizeof(*dip), GFP_NOFS); |
Chris Mason | 9be3395 | 2013-05-17 18:30:14 -0400 | [diff] [blame] | 7997 | if (!dip) { |
| 7998 | ret = -ENOMEM; |
| 7999 | goto free_io_bio; |
| 8000 | } |
| 8001 | |
| 8002 | dip->private = dio_bio->bi_private; |
Josef Bacik | 4b46fce | 2010-05-23 11:00:55 -0400 | [diff] [blame] | 8003 | dip->inode = inode; |
| 8004 | dip->logical_offset = file_offset; |
Kent Overstreet | 4f024f3 | 2013-10-11 15:44:27 -0700 | [diff] [blame] | 8005 | dip->bytes = dio_bio->bi_iter.bi_size; |
| 8006 | dip->disk_bytenr = (u64)dio_bio->bi_iter.bi_sector << 9; |
Chris Mason | 9be3395 | 2013-05-17 18:30:14 -0400 | [diff] [blame] | 8007 | io_bio->bi_private = dip; |
Chris Mason | 9be3395 | 2013-05-17 18:30:14 -0400 | [diff] [blame] | 8008 | dip->orig_bio = io_bio; |
| 8009 | dip->dio_bio = dio_bio; |
Miao Xie | e65e153 | 2010-11-22 03:04:43 +0000 | [diff] [blame] | 8010 | atomic_set(&dip->pending_bios, 0); |
Miao Xie | c1dc089 | 2014-09-12 18:43:56 +0800 | [diff] [blame] | 8011 | btrfs_bio = btrfs_io_bio(io_bio); |
| 8012 | btrfs_bio->logical = file_offset; |
Josef Bacik | 4b46fce | 2010-05-23 11:00:55 -0400 | [diff] [blame] | 8013 | |
Miao Xie | c1dc089 | 2014-09-12 18:43:56 +0800 | [diff] [blame] | 8014 | if (write) { |
Chris Mason | 9be3395 | 2013-05-17 18:30:14 -0400 | [diff] [blame] | 8015 | io_bio->bi_end_io = btrfs_endio_direct_write; |
Miao Xie | c1dc089 | 2014-09-12 18:43:56 +0800 | [diff] [blame] | 8016 | } else { |
Chris Mason | 9be3395 | 2013-05-17 18:30:14 -0400 | [diff] [blame] | 8017 | io_bio->bi_end_io = btrfs_endio_direct_read; |
Miao Xie | c1dc089 | 2014-09-12 18:43:56 +0800 | [diff] [blame] | 8018 | dip->subio_endio = btrfs_subio_endio_read; |
| 8019 | } |
Josef Bacik | 4b46fce | 2010-05-23 11:00:55 -0400 | [diff] [blame] | 8020 | |
Miao Xie | e65e153 | 2010-11-22 03:04:43 +0000 | [diff] [blame] | 8021 | ret = btrfs_submit_direct_hook(rw, dip, skip_sum); |
| 8022 | if (!ret) |
Chris Mason | eaf25d9 | 2010-05-25 09:48:28 -0400 | [diff] [blame] | 8023 | return; |
Chris Mason | 9be3395 | 2013-05-17 18:30:14 -0400 | [diff] [blame] | 8024 | |
Miao Xie | 23ea8e5 | 2014-09-12 18:43:54 +0800 | [diff] [blame] | 8025 | if (btrfs_bio->end_io) |
| 8026 | btrfs_bio->end_io(btrfs_bio, ret); |
Chris Mason | 9be3395 | 2013-05-17 18:30:14 -0400 | [diff] [blame] | 8027 | free_io_bio: |
| 8028 | bio_put(io_bio); |
| 8029 | |
Josef Bacik | 4b46fce | 2010-05-23 11:00:55 -0400 | [diff] [blame] | 8030 | free_ordered: |
| 8031 | /* |
| 8032 | * If this is a write, we need to clean up the reserved space and kill |
| 8033 | * the ordered extent. |
| 8034 | */ |
| 8035 | if (write) { |
| 8036 | struct btrfs_ordered_extent *ordered; |
Josef Bacik | 955256f | 2010-11-19 09:41:10 -0500 | [diff] [blame] | 8037 | ordered = btrfs_lookup_ordered_extent(inode, file_offset); |
Josef Bacik | 4b46fce | 2010-05-23 11:00:55 -0400 | [diff] [blame] | 8038 | if (!test_bit(BTRFS_ORDERED_PREALLOC, &ordered->flags) && |
| 8039 | !test_bit(BTRFS_ORDERED_NOCOW, &ordered->flags)) |
| 8040 | btrfs_free_reserved_extent(root, ordered->start, |
Miao Xie | e570fd2 | 2014-06-19 10:42:50 +0800 | [diff] [blame] | 8041 | ordered->disk_len, 1); |
Josef Bacik | 4b46fce | 2010-05-23 11:00:55 -0400 | [diff] [blame] | 8042 | btrfs_put_ordered_extent(ordered); |
| 8043 | btrfs_put_ordered_extent(ordered); |
| 8044 | } |
Chris Mason | 9be3395 | 2013-05-17 18:30:14 -0400 | [diff] [blame] | 8045 | bio_endio(dio_bio, ret); |
Josef Bacik | 4b46fce | 2010-05-23 11:00:55 -0400 | [diff] [blame] | 8046 | } |
| 8047 | |
Chris Mason | 5a5f79b | 2010-05-26 21:33:37 -0400 | [diff] [blame] | 8048 | 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] | 8049 | const struct iov_iter *iter, loff_t offset) |
Chris Mason | 5a5f79b | 2010-05-26 21:33:37 -0400 | [diff] [blame] | 8050 | { |
| 8051 | int seg; |
Josef Bacik | a1b75f7 | 2011-04-08 15:51:18 +0000 | [diff] [blame] | 8052 | int i; |
Chris Mason | 5a5f79b | 2010-05-26 21:33:37 -0400 | [diff] [blame] | 8053 | unsigned blocksize_mask = root->sectorsize - 1; |
| 8054 | ssize_t retval = -EINVAL; |
Chris Mason | 5a5f79b | 2010-05-26 21:33:37 -0400 | [diff] [blame] | 8055 | |
| 8056 | if (offset & blocksize_mask) |
| 8057 | goto out; |
| 8058 | |
Al Viro | 28060d5 | 2014-03-22 05:15:17 -0400 | [diff] [blame] | 8059 | if (iov_iter_alignment(iter) & blocksize_mask) |
| 8060 | goto out; |
Josef Bacik | a1b75f7 | 2011-04-08 15:51:18 +0000 | [diff] [blame] | 8061 | |
Al Viro | 28060d5 | 2014-03-22 05:15:17 -0400 | [diff] [blame] | 8062 | /* If this is a write we don't need to check anymore */ |
| 8063 | if (rw & WRITE) |
| 8064 | return 0; |
| 8065 | /* |
| 8066 | * Check to make sure we don't have duplicate iov_base's in this |
| 8067 | * iovec, if so return EINVAL, otherwise we'll get csum errors |
| 8068 | * when reading back. |
| 8069 | */ |
| 8070 | for (seg = 0; seg < iter->nr_segs; seg++) { |
| 8071 | for (i = seg + 1; i < iter->nr_segs; i++) { |
| 8072 | if (iter->iov[seg].iov_base == iter->iov[i].iov_base) |
Josef Bacik | a1b75f7 | 2011-04-08 15:51:18 +0000 | [diff] [blame] | 8073 | goto out; |
| 8074 | } |
Chris Mason | 5a5f79b | 2010-05-26 21:33:37 -0400 | [diff] [blame] | 8075 | } |
| 8076 | retval = 0; |
| 8077 | out: |
| 8078 | return retval; |
| 8079 | } |
Josef Bacik | eb838e7 | 2012-07-31 16:28:48 -0400 | [diff] [blame] | 8080 | |
Chris Mason | 1643298 | 2008-04-10 10:23:21 -0400 | [diff] [blame] | 8081 | static ssize_t btrfs_direct_IO(int rw, struct kiocb *iocb, |
Al Viro | d8d3d94 | 2014-03-04 21:27:34 -0500 | [diff] [blame] | 8082 | struct iov_iter *iter, loff_t offset) |
Chris Mason | 1643298 | 2008-04-10 10:23:21 -0400 | [diff] [blame] | 8083 | { |
Josef Bacik | 4b46fce | 2010-05-23 11:00:55 -0400 | [diff] [blame] | 8084 | struct file *file = iocb->ki_filp; |
| 8085 | struct inode *inode = file->f_mapping->host; |
Miao Xie | 0934856 | 2013-02-07 10:12:07 +0000 | [diff] [blame] | 8086 | size_t count = 0; |
Miao Xie | 2e60a51 | 2013-02-08 07:01:08 +0000 | [diff] [blame] | 8087 | int flags = 0; |
Miao Xie | 38851cc | 2013-02-08 07:04:11 +0000 | [diff] [blame] | 8088 | bool wakeup = true; |
| 8089 | bool relock = false; |
Miao Xie | 0934856 | 2013-02-07 10:12:07 +0000 | [diff] [blame] | 8090 | ssize_t ret; |
Josef Bacik | 4b46fce | 2010-05-23 11:00:55 -0400 | [diff] [blame] | 8091 | |
Al Viro | 28060d5 | 2014-03-22 05:15:17 -0400 | [diff] [blame] | 8092 | if (check_direct_IO(BTRFS_I(inode)->root, rw, iocb, iter, offset)) |
Chris Mason | 5a5f79b | 2010-05-26 21:33:37 -0400 | [diff] [blame] | 8093 | return 0; |
Chris Mason | 5a5f79b | 2010-05-26 21:33:37 -0400 | [diff] [blame] | 8094 | |
Miao Xie | 38851cc | 2013-02-08 07:04:11 +0000 | [diff] [blame] | 8095 | atomic_inc(&inode->i_dio_count); |
Peter Zijlstra | 4e857c5 | 2014-03-17 18:06:10 +0100 | [diff] [blame] | 8096 | smp_mb__after_atomic(); |
Miao Xie | 38851cc | 2013-02-08 07:04:11 +0000 | [diff] [blame] | 8097 | |
Josef Bacik | 0e267c4 | 2013-07-02 10:38:02 -0400 | [diff] [blame] | 8098 | /* |
Miao Xie | 41bd9ca | 2014-03-06 13:54:57 +0800 | [diff] [blame] | 8099 | * The generic stuff only does filemap_write_and_wait_range, which |
| 8100 | * isn't enough if we've written compressed pages to this area, so |
| 8101 | * we need to flush the dirty pages again to make absolutely sure |
| 8102 | * that any outstanding dirty pages are on disk. |
Josef Bacik | 0e267c4 | 2013-07-02 10:38:02 -0400 | [diff] [blame] | 8103 | */ |
Al Viro | a6cbcd4 | 2014-03-04 22:38:00 -0500 | [diff] [blame] | 8104 | count = iov_iter_count(iter); |
Miao Xie | 41bd9ca | 2014-03-06 13:54:57 +0800 | [diff] [blame] | 8105 | if (test_bit(BTRFS_INODE_HAS_ASYNC_EXTENT, |
| 8106 | &BTRFS_I(inode)->runtime_flags)) |
Wang Shilong | 9a025a0 | 2014-07-17 11:44:13 +0800 | [diff] [blame] | 8107 | filemap_fdatawrite_range(inode->i_mapping, offset, |
| 8108 | offset + count - 1); |
Josef Bacik | 0e267c4 | 2013-07-02 10:38:02 -0400 | [diff] [blame] | 8109 | |
Miao Xie | 0934856 | 2013-02-07 10:12:07 +0000 | [diff] [blame] | 8110 | if (rw & WRITE) { |
Miao Xie | 38851cc | 2013-02-08 07:04:11 +0000 | [diff] [blame] | 8111 | /* |
| 8112 | * If the write DIO is beyond the EOF, we need update |
| 8113 | * the isize, but it is protected by i_mutex. So we can |
| 8114 | * not unlock the i_mutex at this case. |
| 8115 | */ |
| 8116 | if (offset + count <= inode->i_size) { |
| 8117 | mutex_unlock(&inode->i_mutex); |
| 8118 | relock = true; |
| 8119 | } |
Miao Xie | 0934856 | 2013-02-07 10:12:07 +0000 | [diff] [blame] | 8120 | ret = btrfs_delalloc_reserve_space(inode, count); |
| 8121 | if (ret) |
Miao Xie | 38851cc | 2013-02-08 07:04:11 +0000 | [diff] [blame] | 8122 | goto out; |
David Sterba | ee39b43 | 2014-09-30 01:33:33 +0200 | [diff] [blame] | 8123 | } else if (test_bit(BTRFS_INODE_READDIO_NEED_LOCK, |
| 8124 | &BTRFS_I(inode)->runtime_flags)) { |
Miao Xie | 38851cc | 2013-02-08 07:04:11 +0000 | [diff] [blame] | 8125 | inode_dio_done(inode); |
| 8126 | flags = DIO_LOCKING | DIO_SKIP_HOLES; |
| 8127 | wakeup = false; |
Miao Xie | 0934856 | 2013-02-07 10:12:07 +0000 | [diff] [blame] | 8128 | } |
| 8129 | |
| 8130 | ret = __blockdev_direct_IO(rw, iocb, inode, |
| 8131 | BTRFS_I(inode)->root->fs_info->fs_devices->latest_bdev, |
Al Viro | 31b1403 | 2014-03-05 01:33:16 -0500 | [diff] [blame] | 8132 | iter, offset, btrfs_get_blocks_direct, NULL, |
Miao Xie | 2e60a51 | 2013-02-08 07:01:08 +0000 | [diff] [blame] | 8133 | btrfs_submit_direct, flags); |
Miao Xie | 0934856 | 2013-02-07 10:12:07 +0000 | [diff] [blame] | 8134 | if (rw & WRITE) { |
| 8135 | if (ret < 0 && ret != -EIOCBQUEUED) |
| 8136 | btrfs_delalloc_release_space(inode, count); |
Miao Xie | 172a504 | 2013-02-21 02:48:22 -0700 | [diff] [blame] | 8137 | else if (ret >= 0 && (size_t)ret < count) |
Miao Xie | 0934856 | 2013-02-07 10:12:07 +0000 | [diff] [blame] | 8138 | btrfs_delalloc_release_space(inode, |
| 8139 | count - (size_t)ret); |
Miao Xie | 0934856 | 2013-02-07 10:12:07 +0000 | [diff] [blame] | 8140 | } |
Miao Xie | 38851cc | 2013-02-08 07:04:11 +0000 | [diff] [blame] | 8141 | out: |
Miao Xie | 2e60a51 | 2013-02-08 07:01:08 +0000 | [diff] [blame] | 8142 | if (wakeup) |
| 8143 | inode_dio_done(inode); |
Miao Xie | 38851cc | 2013-02-08 07:04:11 +0000 | [diff] [blame] | 8144 | if (relock) |
| 8145 | mutex_lock(&inode->i_mutex); |
Miao Xie | 0934856 | 2013-02-07 10:12:07 +0000 | [diff] [blame] | 8146 | |
| 8147 | return ret; |
Chris Mason | 1643298 | 2008-04-10 10:23:21 -0400 | [diff] [blame] | 8148 | } |
| 8149 | |
Tsutomu Itoh | 05dadc0 | 2012-11-29 05:08:26 +0000 | [diff] [blame] | 8150 | #define BTRFS_FIEMAP_FLAGS (FIEMAP_FLAG_SYNC) |
| 8151 | |
Yehuda Sadeh | 1506fcc | 2009-01-21 14:39:14 -0500 | [diff] [blame] | 8152 | static int btrfs_fiemap(struct inode *inode, struct fiemap_extent_info *fieinfo, |
| 8153 | __u64 start, __u64 len) |
| 8154 | { |
Tsutomu Itoh | 05dadc0 | 2012-11-29 05:08:26 +0000 | [diff] [blame] | 8155 | int ret; |
| 8156 | |
| 8157 | ret = fiemap_check_flags(fieinfo, BTRFS_FIEMAP_FLAGS); |
| 8158 | if (ret) |
| 8159 | return ret; |
| 8160 | |
Chris Mason | ec29ed5 | 2011-02-23 16:23:20 -0500 | [diff] [blame] | 8161 | return extent_fiemap(inode, fieinfo, start, len, btrfs_get_extent_fiemap); |
Yehuda Sadeh | 1506fcc | 2009-01-21 14:39:14 -0500 | [diff] [blame] | 8162 | } |
| 8163 | |
Chris Mason | 9ebefb18 | 2007-06-15 13:50:00 -0400 | [diff] [blame] | 8164 | int btrfs_readpage(struct file *file, struct page *page) |
| 8165 | { |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 8166 | struct extent_io_tree *tree; |
| 8167 | tree = &BTRFS_I(page->mapping->host)->io_tree; |
Jan Schmidt | 8ddc7d9 | 2011-06-13 20:02:58 +0200 | [diff] [blame] | 8168 | return extent_read_full_page(tree, page, btrfs_get_extent, 0); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 8169 | } |
Chris Mason | 1832a6d | 2007-12-21 16:27:21 -0500 | [diff] [blame] | 8170 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 8171 | static int btrfs_writepage(struct page *page, struct writeback_control *wbc) |
| 8172 | { |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 8173 | struct extent_io_tree *tree; |
Chris Mason | b888db2 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 8174 | |
| 8175 | |
| 8176 | if (current->flags & PF_MEMALLOC) { |
| 8177 | redirty_page_for_writepage(wbc, page); |
| 8178 | unlock_page(page); |
| 8179 | return 0; |
| 8180 | } |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 8181 | tree = &BTRFS_I(page->mapping->host)->io_tree; |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 8182 | return extent_write_full_page(tree, page, btrfs_get_extent, wbc); |
| 8183 | } |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 8184 | |
Eric Sandeen | 48a3b63 | 2013-04-25 20:41:01 +0000 | [diff] [blame] | 8185 | static int btrfs_writepages(struct address_space *mapping, |
| 8186 | struct writeback_control *wbc) |
Chris Mason | b293f02e | 2007-11-01 19:45:34 -0400 | [diff] [blame] | 8187 | { |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 8188 | struct extent_io_tree *tree; |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 8189 | |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 8190 | tree = &BTRFS_I(mapping->host)->io_tree; |
Chris Mason | b293f02e | 2007-11-01 19:45:34 -0400 | [diff] [blame] | 8191 | return extent_writepages(tree, mapping, btrfs_get_extent, wbc); |
| 8192 | } |
| 8193 | |
Chris Mason | 3ab2fb5 | 2007-11-08 10:59:22 -0500 | [diff] [blame] | 8194 | static int |
| 8195 | btrfs_readpages(struct file *file, struct address_space *mapping, |
| 8196 | struct list_head *pages, unsigned nr_pages) |
| 8197 | { |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 8198 | struct extent_io_tree *tree; |
| 8199 | tree = &BTRFS_I(mapping->host)->io_tree; |
Chris Mason | 3ab2fb5 | 2007-11-08 10:59:22 -0500 | [diff] [blame] | 8200 | return extent_readpages(tree, mapping, pages, nr_pages, |
| 8201 | btrfs_get_extent); |
| 8202 | } |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 8203 | static int __btrfs_releasepage(struct page *page, gfp_t gfp_flags) |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 8204 | { |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 8205 | struct extent_io_tree *tree; |
| 8206 | struct extent_map_tree *map; |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 8207 | int ret; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 8208 | |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 8209 | tree = &BTRFS_I(page->mapping->host)->io_tree; |
| 8210 | map = &BTRFS_I(page->mapping->host)->extent_tree; |
Chris Mason | 70dec80 | 2008-01-29 09:59:12 -0500 | [diff] [blame] | 8211 | ret = try_release_extent_mapping(map, tree, page, gfp_flags); |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 8212 | if (ret == 1) { |
| 8213 | ClearPagePrivate(page); |
| 8214 | set_page_private(page, 0); |
| 8215 | page_cache_release(page); |
| 8216 | } |
| 8217 | return ret; |
| 8218 | } |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 8219 | |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 8220 | static int btrfs_releasepage(struct page *page, gfp_t gfp_flags) |
| 8221 | { |
Chris Mason | 98509cf | 2008-09-11 15:51:43 -0400 | [diff] [blame] | 8222 | if (PageWriteback(page) || PageDirty(page)) |
| 8223 | return 0; |
Yan Zheng | b335b00 | 2009-02-12 10:06:04 -0500 | [diff] [blame] | 8224 | return __btrfs_releasepage(page, gfp_flags & GFP_NOFS); |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 8225 | } |
| 8226 | |
Lukas Czerner | d47992f | 2013-05-21 23:17:23 -0400 | [diff] [blame] | 8227 | static void btrfs_invalidatepage(struct page *page, unsigned int offset, |
| 8228 | unsigned int length) |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 8229 | { |
Josef Bacik | 5fd0204 | 2012-05-02 14:00:54 -0400 | [diff] [blame] | 8230 | struct inode *inode = page->mapping->host; |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 8231 | struct extent_io_tree *tree; |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 8232 | struct btrfs_ordered_extent *ordered; |
Josef Bacik | 2ac55d4 | 2010-02-03 19:33:23 +0000 | [diff] [blame] | 8233 | struct extent_state *cached_state = NULL; |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 8234 | u64 page_start = page_offset(page); |
| 8235 | u64 page_end = page_start + PAGE_CACHE_SIZE - 1; |
Filipe David Borba Manana | 131e404 | 2013-11-19 22:29:35 +0000 | [diff] [blame] | 8236 | int inode_evicting = inode->i_state & I_FREEING; |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 8237 | |
Chris Mason | 8b62b72 | 2009-09-02 16:53:46 -0400 | [diff] [blame] | 8238 | /* |
| 8239 | * we have the page locked, so new writeback can't start, |
| 8240 | * and the dirty bit won't be cleared while we are here. |
| 8241 | * |
| 8242 | * Wait for IO on this page so that we can safely clear |
| 8243 | * the PagePrivate2 bit and do ordered accounting |
| 8244 | */ |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 8245 | wait_on_page_writeback(page); |
Chris Mason | 8b62b72 | 2009-09-02 16:53:46 -0400 | [diff] [blame] | 8246 | |
Josef Bacik | 5fd0204 | 2012-05-02 14:00:54 -0400 | [diff] [blame] | 8247 | tree = &BTRFS_I(inode)->io_tree; |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 8248 | if (offset) { |
| 8249 | btrfs_releasepage(page, GFP_NOFS); |
| 8250 | return; |
| 8251 | } |
Filipe David Borba Manana | 131e404 | 2013-11-19 22:29:35 +0000 | [diff] [blame] | 8252 | |
| 8253 | if (!inode_evicting) |
| 8254 | lock_extent_bits(tree, page_start, page_end, 0, &cached_state); |
| 8255 | ordered = btrfs_lookup_ordered_extent(inode, page_start); |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 8256 | if (ordered) { |
Chris Mason | eb84ae0 | 2008-07-17 13:53:27 -0400 | [diff] [blame] | 8257 | /* |
| 8258 | * IO on this page will never be started, so we need |
| 8259 | * to account for any ordered extents now |
| 8260 | */ |
Filipe David Borba Manana | 131e404 | 2013-11-19 22:29:35 +0000 | [diff] [blame] | 8261 | if (!inode_evicting) |
| 8262 | clear_extent_bit(tree, page_start, page_end, |
| 8263 | EXTENT_DIRTY | EXTENT_DELALLOC | |
| 8264 | EXTENT_LOCKED | EXTENT_DO_ACCOUNTING | |
| 8265 | EXTENT_DEFRAG, 1, 0, &cached_state, |
| 8266 | GFP_NOFS); |
Chris Mason | 8b62b72 | 2009-09-02 16:53:46 -0400 | [diff] [blame] | 8267 | /* |
| 8268 | * whoever cleared the private bit is responsible |
| 8269 | * for the finish_ordered_io |
| 8270 | */ |
Josef Bacik | 77cef2e | 2013-08-29 13:57:21 -0400 | [diff] [blame] | 8271 | if (TestClearPagePrivate2(page)) { |
| 8272 | struct btrfs_ordered_inode_tree *tree; |
| 8273 | u64 new_len; |
| 8274 | |
| 8275 | tree = &BTRFS_I(inode)->ordered_tree; |
| 8276 | |
| 8277 | spin_lock_irq(&tree->lock); |
| 8278 | set_bit(BTRFS_ORDERED_TRUNCATED, &ordered->flags); |
| 8279 | new_len = page_start - ordered->file_offset; |
| 8280 | if (new_len < ordered->truncated_len) |
| 8281 | ordered->truncated_len = new_len; |
| 8282 | spin_unlock_irq(&tree->lock); |
| 8283 | |
| 8284 | if (btrfs_dec_test_ordered_pending(inode, &ordered, |
| 8285 | page_start, |
| 8286 | PAGE_CACHE_SIZE, 1)) |
| 8287 | btrfs_finish_ordered_io(ordered); |
Chris Mason | 8b62b72 | 2009-09-02 16:53:46 -0400 | [diff] [blame] | 8288 | } |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 8289 | btrfs_put_ordered_extent(ordered); |
Filipe David Borba Manana | 131e404 | 2013-11-19 22:29:35 +0000 | [diff] [blame] | 8290 | if (!inode_evicting) { |
| 8291 | cached_state = NULL; |
| 8292 | lock_extent_bits(tree, page_start, page_end, 0, |
| 8293 | &cached_state); |
| 8294 | } |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 8295 | } |
Filipe David Borba Manana | 131e404 | 2013-11-19 22:29:35 +0000 | [diff] [blame] | 8296 | |
| 8297 | if (!inode_evicting) { |
| 8298 | clear_extent_bit(tree, page_start, page_end, |
| 8299 | EXTENT_LOCKED | EXTENT_DIRTY | |
| 8300 | EXTENT_DELALLOC | EXTENT_DO_ACCOUNTING | |
| 8301 | EXTENT_DEFRAG, 1, 1, |
| 8302 | &cached_state, GFP_NOFS); |
| 8303 | |
| 8304 | __btrfs_releasepage(page, GFP_NOFS); |
| 8305 | } |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 8306 | |
Chris Mason | 4a09675 | 2008-07-21 10:29:44 -0400 | [diff] [blame] | 8307 | ClearPageChecked(page); |
Chris Mason | 9ad6b7bc | 2008-04-18 16:11:30 -0400 | [diff] [blame] | 8308 | if (PagePrivate(page)) { |
Chris Mason | 9ad6b7bc | 2008-04-18 16:11:30 -0400 | [diff] [blame] | 8309 | ClearPagePrivate(page); |
| 8310 | set_page_private(page, 0); |
| 8311 | page_cache_release(page); |
| 8312 | } |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 8313 | } |
| 8314 | |
Chris Mason | 9ebefb18 | 2007-06-15 13:50:00 -0400 | [diff] [blame] | 8315 | /* |
| 8316 | * btrfs_page_mkwrite() is not allowed to change the file size as it gets |
| 8317 | * called from a page fault handler when a page is first dirtied. Hence we must |
| 8318 | * be careful to check for EOF conditions here. We set the page up correctly |
| 8319 | * for a written page which means we get ENOSPC checking when writing into |
| 8320 | * holes and correct delalloc and unwritten extent mapping on filesystems that |
| 8321 | * support these features. |
| 8322 | * |
| 8323 | * We are not allowed to take the i_mutex here so we have to play games to |
| 8324 | * protect against truncate races as the page could now be beyond EOF. Because |
| 8325 | * vmtruncate() writes the inode size before removing pages, once we have the |
| 8326 | * page lock we can determine safely if the page is beyond EOF. If it is not |
| 8327 | * beyond EOF, then the page is guaranteed safe against truncation until we |
| 8328 | * unlock the page. |
| 8329 | */ |
Nick Piggin | c2ec175 | 2009-03-31 15:23:21 -0700 | [diff] [blame] | 8330 | 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] | 8331 | { |
Nick Piggin | c2ec175 | 2009-03-31 15:23:21 -0700 | [diff] [blame] | 8332 | struct page *page = vmf->page; |
Al Viro | 496ad9a | 2013-01-23 17:07:38 -0500 | [diff] [blame] | 8333 | struct inode *inode = file_inode(vma->vm_file); |
Chris Mason | 1832a6d | 2007-12-21 16:27:21 -0500 | [diff] [blame] | 8334 | struct btrfs_root *root = BTRFS_I(inode)->root; |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 8335 | struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree; |
| 8336 | struct btrfs_ordered_extent *ordered; |
Josef Bacik | 2ac55d4 | 2010-02-03 19:33:23 +0000 | [diff] [blame] | 8337 | struct extent_state *cached_state = NULL; |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 8338 | char *kaddr; |
| 8339 | unsigned long zero_start; |
Chris Mason | 9ebefb18 | 2007-06-15 13:50:00 -0400 | [diff] [blame] | 8340 | loff_t size; |
Chris Mason | 1832a6d | 2007-12-21 16:27:21 -0500 | [diff] [blame] | 8341 | int ret; |
Chris Mason | 9998eb7 | 2012-01-25 13:47:40 -0500 | [diff] [blame] | 8342 | int reserved = 0; |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 8343 | u64 page_start; |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 8344 | u64 page_end; |
Chris Mason | 9ebefb18 | 2007-06-15 13:50:00 -0400 | [diff] [blame] | 8345 | |
Jan Kara | b2b5ef5 | 2012-06-12 16:20:45 +0200 | [diff] [blame] | 8346 | sb_start_pagefault(inode->i_sb); |
Yan, Zheng | 0ca1f7c | 2010-05-16 10:48:47 -0400 | [diff] [blame] | 8347 | ret = btrfs_delalloc_reserve_space(inode, PAGE_CACHE_SIZE); |
Chris Mason | 9998eb7 | 2012-01-25 13:47:40 -0500 | [diff] [blame] | 8348 | if (!ret) { |
Josef Bacik | e41f941 | 2012-03-26 09:46:47 -0400 | [diff] [blame] | 8349 | ret = file_update_time(vma->vm_file); |
Chris Mason | 9998eb7 | 2012-01-25 13:47:40 -0500 | [diff] [blame] | 8350 | reserved = 1; |
| 8351 | } |
Nick Piggin | 56a76f8 | 2009-03-31 15:23:23 -0700 | [diff] [blame] | 8352 | if (ret) { |
| 8353 | if (ret == -ENOMEM) |
| 8354 | ret = VM_FAULT_OOM; |
| 8355 | else /* -ENOSPC, -EIO, etc */ |
| 8356 | ret = VM_FAULT_SIGBUS; |
Chris Mason | 9998eb7 | 2012-01-25 13:47:40 -0500 | [diff] [blame] | 8357 | if (reserved) |
| 8358 | goto out; |
| 8359 | goto out_noreserve; |
Nick Piggin | 56a76f8 | 2009-03-31 15:23:23 -0700 | [diff] [blame] | 8360 | } |
Chris Mason | 1832a6d | 2007-12-21 16:27:21 -0500 | [diff] [blame] | 8361 | |
Nick Piggin | 56a76f8 | 2009-03-31 15:23:23 -0700 | [diff] [blame] | 8362 | ret = VM_FAULT_NOPAGE; /* make the VM retry the fault */ |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 8363 | again: |
Chris Mason | 9ebefb18 | 2007-06-15 13:50:00 -0400 | [diff] [blame] | 8364 | lock_page(page); |
Chris Mason | 9ebefb18 | 2007-06-15 13:50:00 -0400 | [diff] [blame] | 8365 | size = i_size_read(inode); |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 8366 | page_start = page_offset(page); |
| 8367 | page_end = page_start + PAGE_CACHE_SIZE - 1; |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 8368 | |
Chris Mason | 9ebefb18 | 2007-06-15 13:50:00 -0400 | [diff] [blame] | 8369 | if ((page->mapping != inode->i_mapping) || |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 8370 | (page_start >= size)) { |
Chris Mason | 9ebefb18 | 2007-06-15 13:50:00 -0400 | [diff] [blame] | 8371 | /* page got truncated out from underneath us */ |
| 8372 | goto out_unlock; |
| 8373 | } |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 8374 | wait_on_page_writeback(page); |
| 8375 | |
Jeff Mahoney | d008237 | 2012-03-01 14:57:19 +0100 | [diff] [blame] | 8376 | lock_extent_bits(io_tree, page_start, page_end, 0, &cached_state); |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 8377 | set_page_extent_mapped(page); |
| 8378 | |
Chris Mason | eb84ae0 | 2008-07-17 13:53:27 -0400 | [diff] [blame] | 8379 | /* |
| 8380 | * we can't set the delalloc bits if there are pending ordered |
| 8381 | * extents. Drop our locks and wait for them to finish |
| 8382 | */ |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 8383 | ordered = btrfs_lookup_ordered_extent(inode, page_start); |
| 8384 | if (ordered) { |
Josef Bacik | 2ac55d4 | 2010-02-03 19:33:23 +0000 | [diff] [blame] | 8385 | unlock_extent_cached(io_tree, page_start, page_end, |
| 8386 | &cached_state, GFP_NOFS); |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 8387 | unlock_page(page); |
Chris Mason | eb84ae0 | 2008-07-17 13:53:27 -0400 | [diff] [blame] | 8388 | btrfs_start_ordered_extent(inode, ordered, 1); |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 8389 | btrfs_put_ordered_extent(ordered); |
| 8390 | goto again; |
| 8391 | } |
| 8392 | |
Josef Bacik | fbf1908 | 2009-10-01 17:10:23 -0400 | [diff] [blame] | 8393 | /* |
| 8394 | * XXX - page_mkwrite gets called every time the page is dirtied, even |
| 8395 | * if it was already dirty, so for space accounting reasons we need to |
| 8396 | * clear any delalloc bits for the range we are fixing to save. There |
| 8397 | * is probably a better way to do this, but for now keep consistent with |
| 8398 | * prepare_pages in the normal write path. |
| 8399 | */ |
Josef Bacik | 2ac55d4 | 2010-02-03 19:33:23 +0000 | [diff] [blame] | 8400 | clear_extent_bit(&BTRFS_I(inode)->io_tree, page_start, page_end, |
Liu Bo | 9e8a4a8 | 2012-09-05 19:10:51 -0600 | [diff] [blame] | 8401 | EXTENT_DIRTY | EXTENT_DELALLOC | |
| 8402 | EXTENT_DO_ACCOUNTING | EXTENT_DEFRAG, |
Josef Bacik | 2ac55d4 | 2010-02-03 19:33:23 +0000 | [diff] [blame] | 8403 | 0, 0, &cached_state, GFP_NOFS); |
Josef Bacik | fbf1908 | 2009-10-01 17:10:23 -0400 | [diff] [blame] | 8404 | |
Josef Bacik | 2ac55d4 | 2010-02-03 19:33:23 +0000 | [diff] [blame] | 8405 | ret = btrfs_set_extent_delalloc(inode, page_start, page_end, |
| 8406 | &cached_state); |
Josef Bacik | 9ed74f2 | 2009-09-11 16:12:44 -0400 | [diff] [blame] | 8407 | if (ret) { |
Josef Bacik | 2ac55d4 | 2010-02-03 19:33:23 +0000 | [diff] [blame] | 8408 | unlock_extent_cached(io_tree, page_start, page_end, |
| 8409 | &cached_state, GFP_NOFS); |
Josef Bacik | 9ed74f2 | 2009-09-11 16:12:44 -0400 | [diff] [blame] | 8410 | ret = VM_FAULT_SIGBUS; |
| 8411 | goto out_unlock; |
| 8412 | } |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 8413 | ret = 0; |
Chris Mason | 9ebefb18 | 2007-06-15 13:50:00 -0400 | [diff] [blame] | 8414 | |
| 8415 | /* page is wholly or partially inside EOF */ |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 8416 | if (page_start + PAGE_CACHE_SIZE > size) |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 8417 | zero_start = size & ~PAGE_CACHE_MASK; |
Chris Mason | 9ebefb18 | 2007-06-15 13:50:00 -0400 | [diff] [blame] | 8418 | else |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 8419 | zero_start = PAGE_CACHE_SIZE; |
Chris Mason | 9ebefb18 | 2007-06-15 13:50:00 -0400 | [diff] [blame] | 8420 | |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 8421 | if (zero_start != PAGE_CACHE_SIZE) { |
| 8422 | kaddr = kmap(page); |
| 8423 | memset(kaddr + zero_start, 0, PAGE_CACHE_SIZE - zero_start); |
| 8424 | flush_dcache_page(page); |
| 8425 | kunmap(page); |
| 8426 | } |
Chris Mason | 247e743 | 2008-07-17 12:53:51 -0400 | [diff] [blame] | 8427 | ClearPageChecked(page); |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 8428 | set_page_dirty(page); |
Chris Mason | 50a9b21 | 2009-09-11 12:33:12 -0400 | [diff] [blame] | 8429 | SetPageUptodate(page); |
Chris Mason | 5a3f23d | 2009-03-31 13:27:11 -0400 | [diff] [blame] | 8430 | |
Chris Mason | 257c62e | 2009-10-13 13:21:08 -0400 | [diff] [blame] | 8431 | BTRFS_I(inode)->last_trans = root->fs_info->generation; |
| 8432 | BTRFS_I(inode)->last_sub_trans = BTRFS_I(inode)->root->log_transid; |
Liu Bo | 46d8bc3 | 2012-08-29 01:07:55 -0600 | [diff] [blame] | 8433 | 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] | 8434 | |
Josef Bacik | 2ac55d4 | 2010-02-03 19:33:23 +0000 | [diff] [blame] | 8435 | 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] | 8436 | |
| 8437 | out_unlock: |
Jan Kara | b2b5ef5 | 2012-06-12 16:20:45 +0200 | [diff] [blame] | 8438 | if (!ret) { |
| 8439 | sb_end_pagefault(inode->i_sb); |
Chris Mason | 50a9b21 | 2009-09-11 12:33:12 -0400 | [diff] [blame] | 8440 | return VM_FAULT_LOCKED; |
Jan Kara | b2b5ef5 | 2012-06-12 16:20:45 +0200 | [diff] [blame] | 8441 | } |
Chris Mason | 9ebefb18 | 2007-06-15 13:50:00 -0400 | [diff] [blame] | 8442 | unlock_page(page); |
Chris Mason | 1832a6d | 2007-12-21 16:27:21 -0500 | [diff] [blame] | 8443 | out: |
Josef Bacik | ec39e18 | 2012-01-12 19:10:12 -0500 | [diff] [blame] | 8444 | btrfs_delalloc_release_space(inode, PAGE_CACHE_SIZE); |
Chris Mason | 9998eb7 | 2012-01-25 13:47:40 -0500 | [diff] [blame] | 8445 | out_noreserve: |
Jan Kara | b2b5ef5 | 2012-06-12 16:20:45 +0200 | [diff] [blame] | 8446 | sb_end_pagefault(inode->i_sb); |
Chris Mason | 9ebefb18 | 2007-06-15 13:50:00 -0400 | [diff] [blame] | 8447 | return ret; |
| 8448 | } |
| 8449 | |
Josef Bacik | a41ad39 | 2011-01-31 15:30:16 -0500 | [diff] [blame] | 8450 | static int btrfs_truncate(struct inode *inode) |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 8451 | { |
| 8452 | struct btrfs_root *root = BTRFS_I(inode)->root; |
Josef Bacik | fcb80c2 | 2011-05-03 10:40:22 -0400 | [diff] [blame] | 8453 | struct btrfs_block_rsv *rsv; |
Josef Bacik | a71754f | 2013-06-17 17:14:39 -0400 | [diff] [blame] | 8454 | int ret = 0; |
Josef Bacik | 3893e33 | 2011-01-31 16:03:11 -0500 | [diff] [blame] | 8455 | int err = 0; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 8456 | struct btrfs_trans_handle *trans; |
Chris Mason | dbe674a | 2008-07-17 12:54:05 -0400 | [diff] [blame] | 8457 | u64 mask = root->sectorsize - 1; |
Josef Bacik | 0712718 | 2011-08-19 10:29:59 -0400 | [diff] [blame] | 8458 | u64 min_size = btrfs_calc_trunc_metadata_size(root, 1); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 8459 | |
Josef Bacik | 0ef8b72 | 2013-10-25 16:13:35 -0400 | [diff] [blame] | 8460 | ret = btrfs_wait_ordered_range(inode, inode->i_size & (~mask), |
| 8461 | (u64)-1); |
| 8462 | if (ret) |
| 8463 | return ret; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 8464 | |
Josef Bacik | fcb80c2 | 2011-05-03 10:40:22 -0400 | [diff] [blame] | 8465 | /* |
| 8466 | * Yes ladies and gentelment, this is indeed ugly. The fact is we have |
| 8467 | * 3 things going on here |
| 8468 | * |
| 8469 | * 1) We need to reserve space for our orphan item and the space to |
| 8470 | * delete our orphan item. Lord knows we don't want to have a dangling |
| 8471 | * orphan item because we didn't reserve space to remove it. |
| 8472 | * |
| 8473 | * 2) We need to reserve space to update our inode. |
| 8474 | * |
| 8475 | * 3) We need to have something to cache all the space that is going to |
| 8476 | * be free'd up by the truncate operation, but also have some slack |
| 8477 | * space reserved in case it uses space during the truncate (thank you |
| 8478 | * very much snapshotting). |
| 8479 | * |
| 8480 | * And we need these to all be seperate. The fact is we can use alot of |
| 8481 | * space doing the truncate, and we have no earthly idea how much space |
| 8482 | * we will use, so we need the truncate reservation to be seperate so it |
| 8483 | * doesn't end up using space reserved for updating the inode or |
| 8484 | * removing the orphan item. We also need to be able to stop the |
| 8485 | * transaction and start a new one, which means we need to be able to |
| 8486 | * update the inode several times, and we have no idea of knowing how |
| 8487 | * many times that will be, so we can't just reserve 1 item for the |
| 8488 | * entirety of the opration, so that has to be done seperately as well. |
| 8489 | * Then there is the orphan item, which does indeed need to be held on |
| 8490 | * to for the whole operation, and we need nobody to touch this reserved |
| 8491 | * space except the orphan code. |
| 8492 | * |
| 8493 | * So that leaves us with |
| 8494 | * |
| 8495 | * 1) root->orphan_block_rsv - for the orphan deletion. |
| 8496 | * 2) rsv - for the truncate reservation, which we will steal from the |
| 8497 | * transaction reservation. |
| 8498 | * 3) fs_info->trans_block_rsv - this will have 1 items worth left for |
| 8499 | * updating the inode. |
| 8500 | */ |
Miao Xie | 66d8f3d | 2012-09-06 04:02:28 -0600 | [diff] [blame] | 8501 | rsv = btrfs_alloc_block_rsv(root, BTRFS_BLOCK_RSV_TEMP); |
Josef Bacik | fcb80c2 | 2011-05-03 10:40:22 -0400 | [diff] [blame] | 8502 | if (!rsv) |
| 8503 | return -ENOMEM; |
Josef Bacik | 4a33854 | 2011-08-29 11:01:31 -0400 | [diff] [blame] | 8504 | rsv->size = min_size; |
Josef Bacik | ca7e70f | 2012-08-27 17:48:15 -0400 | [diff] [blame] | 8505 | rsv->failfast = 1; |
Josef Bacik | f0cd846 | 2011-03-04 14:37:08 -0500 | [diff] [blame] | 8506 | |
Josef Bacik | 907cbce | 2011-08-08 13:46:15 -0400 | [diff] [blame] | 8507 | /* |
Josef Bacik | 0712718 | 2011-08-19 10:29:59 -0400 | [diff] [blame] | 8508 | * 1 for the truncate slack space |
Josef Bacik | 907cbce | 2011-08-08 13:46:15 -0400 | [diff] [blame] | 8509 | * 1 for updating the inode. |
| 8510 | */ |
Josef Bacik | f3fe820 | 2013-01-07 17:03:21 -0500 | [diff] [blame] | 8511 | trans = btrfs_start_transaction(root, 2); |
Josef Bacik | fcb80c2 | 2011-05-03 10:40:22 -0400 | [diff] [blame] | 8512 | if (IS_ERR(trans)) { |
| 8513 | err = PTR_ERR(trans); |
| 8514 | goto out; |
| 8515 | } |
Josef Bacik | f0cd846 | 2011-03-04 14:37:08 -0500 | [diff] [blame] | 8516 | |
Josef Bacik | 907cbce | 2011-08-08 13:46:15 -0400 | [diff] [blame] | 8517 | /* Migrate the slack space for the truncate to our reserve */ |
| 8518 | ret = btrfs_block_rsv_migrate(&root->fs_info->trans_block_rsv, rsv, |
| 8519 | min_size); |
Josef Bacik | fcb80c2 | 2011-05-03 10:40:22 -0400 | [diff] [blame] | 8520 | BUG_ON(ret); |
Josef Bacik | f0cd846 | 2011-03-04 14:37:08 -0500 | [diff] [blame] | 8521 | |
Chris Mason | 5a3f23d | 2009-03-31 13:27:11 -0400 | [diff] [blame] | 8522 | /* |
Josef Bacik | 5dc562c | 2012-08-17 13:14:17 -0400 | [diff] [blame] | 8523 | * So if we truncate and then write and fsync we normally would just |
| 8524 | * write the extents that changed, which is a problem if we need to |
| 8525 | * first truncate that entire inode. So set this flag so we write out |
| 8526 | * all of the extents in the inode to the sync log so we're completely |
| 8527 | * safe. |
| 8528 | */ |
| 8529 | set_bit(BTRFS_INODE_NEEDS_FULL_SYNC, &BTRFS_I(inode)->runtime_flags); |
Josef Bacik | ca7e70f | 2012-08-27 17:48:15 -0400 | [diff] [blame] | 8530 | trans->block_rsv = rsv; |
Josef Bacik | 5dc562c | 2012-08-17 13:14:17 -0400 | [diff] [blame] | 8531 | |
Yan, Zheng | 8082510 | 2009-11-12 09:35:36 +0000 | [diff] [blame] | 8532 | while (1) { |
| 8533 | ret = btrfs_truncate_inode_items(trans, root, inode, |
| 8534 | inode->i_size, |
| 8535 | BTRFS_EXTENT_DATA_KEY); |
Josef Bacik | ca7e70f | 2012-08-27 17:48:15 -0400 | [diff] [blame] | 8536 | if (ret != -ENOSPC) { |
Josef Bacik | 3893e33 | 2011-01-31 16:03:11 -0500 | [diff] [blame] | 8537 | err = ret; |
Yan, Zheng | 8082510 | 2009-11-12 09:35:36 +0000 | [diff] [blame] | 8538 | break; |
Josef Bacik | 3893e33 | 2011-01-31 16:03:11 -0500 | [diff] [blame] | 8539 | } |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 8540 | |
Josef Bacik | fcb80c2 | 2011-05-03 10:40:22 -0400 | [diff] [blame] | 8541 | trans->block_rsv = &root->fs_info->trans_block_rsv; |
Yan, Zheng | 8082510 | 2009-11-12 09:35:36 +0000 | [diff] [blame] | 8542 | ret = btrfs_update_inode(trans, root, inode); |
Josef Bacik | 3893e33 | 2011-01-31 16:03:11 -0500 | [diff] [blame] | 8543 | if (ret) { |
| 8544 | err = ret; |
| 8545 | break; |
| 8546 | } |
Josef Bacik | ca7e70f | 2012-08-27 17:48:15 -0400 | [diff] [blame] | 8547 | |
Yan, Zheng | 8082510 | 2009-11-12 09:35:36 +0000 | [diff] [blame] | 8548 | btrfs_end_transaction(trans, root); |
Liu Bo | b53d3f5 | 2012-11-14 14:34:34 +0000 | [diff] [blame] | 8549 | btrfs_btree_balance_dirty(root); |
Josef Bacik | ca7e70f | 2012-08-27 17:48:15 -0400 | [diff] [blame] | 8550 | |
| 8551 | trans = btrfs_start_transaction(root, 2); |
| 8552 | if (IS_ERR(trans)) { |
| 8553 | ret = err = PTR_ERR(trans); |
| 8554 | trans = NULL; |
| 8555 | break; |
| 8556 | } |
| 8557 | |
| 8558 | ret = btrfs_block_rsv_migrate(&root->fs_info->trans_block_rsv, |
| 8559 | rsv, min_size); |
| 8560 | BUG_ON(ret); /* shouldn't happen */ |
| 8561 | trans->block_rsv = rsv; |
Yan, Zheng | 8082510 | 2009-11-12 09:35:36 +0000 | [diff] [blame] | 8562 | } |
| 8563 | |
| 8564 | if (ret == 0 && inode->i_nlink > 0) { |
Josef Bacik | fcb80c2 | 2011-05-03 10:40:22 -0400 | [diff] [blame] | 8565 | trans->block_rsv = root->orphan_block_rsv; |
Yan, Zheng | 8082510 | 2009-11-12 09:35:36 +0000 | [diff] [blame] | 8566 | ret = btrfs_orphan_del(trans, inode); |
Josef Bacik | 3893e33 | 2011-01-31 16:03:11 -0500 | [diff] [blame] | 8567 | if (ret) |
| 8568 | err = ret; |
Yan, Zheng | 8082510 | 2009-11-12 09:35:36 +0000 | [diff] [blame] | 8569 | } |
| 8570 | |
Chris Mason | 917c16b | 2011-11-08 14:49:59 -0500 | [diff] [blame] | 8571 | if (trans) { |
| 8572 | trans->block_rsv = &root->fs_info->trans_block_rsv; |
| 8573 | ret = btrfs_update_inode(trans, root, inode); |
| 8574 | if (ret && !err) |
| 8575 | err = ret; |
Josef Bacik | 7b12876 | 2008-07-24 12:17:14 -0400 | [diff] [blame] | 8576 | |
Josef Bacik | 7ad85bb | 2012-01-12 19:10:12 -0500 | [diff] [blame] | 8577 | ret = btrfs_end_transaction(trans, root); |
Liu Bo | b53d3f5 | 2012-11-14 14:34:34 +0000 | [diff] [blame] | 8578 | btrfs_btree_balance_dirty(root); |
Chris Mason | 917c16b | 2011-11-08 14:49:59 -0500 | [diff] [blame] | 8579 | } |
Josef Bacik | fcb80c2 | 2011-05-03 10:40:22 -0400 | [diff] [blame] | 8580 | |
| 8581 | out: |
| 8582 | btrfs_free_block_rsv(root, rsv); |
| 8583 | |
Josef Bacik | 3893e33 | 2011-01-31 16:03:11 -0500 | [diff] [blame] | 8584 | if (ret && !err) |
| 8585 | err = ret; |
Josef Bacik | a41ad39 | 2011-01-31 15:30:16 -0500 | [diff] [blame] | 8586 | |
Josef Bacik | 3893e33 | 2011-01-31 16:03:11 -0500 | [diff] [blame] | 8587 | return err; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 8588 | } |
| 8589 | |
Sven Wegener | 3b96362 | 2008-06-09 21:57:42 -0400 | [diff] [blame] | 8590 | /* |
Chris Mason | d352ac6 | 2008-09-29 15:18:18 -0400 | [diff] [blame] | 8591 | * create a new subvolume directory/inode (helper for the ioctl). |
| 8592 | */ |
Yan Zheng | d2fb343 | 2008-12-11 16:30:39 -0500 | [diff] [blame] | 8593 | int btrfs_create_subvol_root(struct btrfs_trans_handle *trans, |
Filipe David Borba Manana | 6354192 | 2014-01-07 11:47:46 +0000 | [diff] [blame] | 8594 | struct btrfs_root *new_root, |
| 8595 | struct btrfs_root *parent_root, |
| 8596 | u64 new_dirid) |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 8597 | { |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 8598 | struct inode *inode; |
Yan, Zheng | 76dda93 | 2009-09-21 16:00:26 -0400 | [diff] [blame] | 8599 | int err; |
Chris Mason | 00e4e6b | 2008-08-05 11:18:09 -0400 | [diff] [blame] | 8600 | u64 index = 0; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 8601 | |
Florian Albrechtskirchinger | 12fc9d0 | 2012-02-10 22:15:54 +0100 | [diff] [blame] | 8602 | inode = btrfs_new_inode(trans, new_root, NULL, "..", 2, |
| 8603 | new_dirid, new_dirid, |
| 8604 | S_IFDIR | (~current_umask() & S_IRWXUGO), |
| 8605 | &index); |
Chris Mason | 54aa1f4 | 2007-06-22 14:16:25 -0400 | [diff] [blame] | 8606 | if (IS_ERR(inode)) |
Christoph Hellwig | f46b5a6 | 2008-06-11 21:53:53 -0400 | [diff] [blame] | 8607 | return PTR_ERR(inode); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 8608 | inode->i_op = &btrfs_dir_inode_operations; |
| 8609 | inode->i_fop = &btrfs_dir_file_operations; |
| 8610 | |
Miklos Szeredi | bfe8684 | 2011-10-28 14:13:29 +0200 | [diff] [blame] | 8611 | set_nlink(inode, 1); |
Chris Mason | dbe674a | 2008-07-17 12:54:05 -0400 | [diff] [blame] | 8612 | btrfs_i_size_write(inode, 0); |
Chris Mason | b0d5d10 | 2014-09-08 13:08:51 -0700 | [diff] [blame] | 8613 | unlock_new_inode(inode); |
Sven Wegener | 3b96362 | 2008-06-09 21:57:42 -0400 | [diff] [blame] | 8614 | |
Filipe David Borba Manana | 6354192 | 2014-01-07 11:47:46 +0000 | [diff] [blame] | 8615 | err = btrfs_subvol_inherit_props(trans, new_root, parent_root); |
| 8616 | if (err) |
| 8617 | btrfs_err(new_root->fs_info, |
David Sterba | 351fd35 | 2014-05-15 16:48:20 +0200 | [diff] [blame] | 8618 | "error inheriting subvolume %llu properties: %d", |
Filipe David Borba Manana | 6354192 | 2014-01-07 11:47:46 +0000 | [diff] [blame] | 8619 | new_root->root_key.objectid, err); |
| 8620 | |
Yan, Zheng | 76dda93 | 2009-09-21 16:00:26 -0400 | [diff] [blame] | 8621 | err = btrfs_update_inode(trans, new_root, inode); |
Christoph Hellwig | cb8e709 | 2008-10-09 13:39:39 -0400 | [diff] [blame] | 8622 | |
Yan, Zheng | 76dda93 | 2009-09-21 16:00:26 -0400 | [diff] [blame] | 8623 | iput(inode); |
Mark Fasheh | ce59897 | 2011-07-26 11:32:23 -0700 | [diff] [blame] | 8624 | return err; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 8625 | } |
| 8626 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 8627 | struct inode *btrfs_alloc_inode(struct super_block *sb) |
| 8628 | { |
| 8629 | struct btrfs_inode *ei; |
Yan, Zheng | 2ead6ae | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 8630 | struct inode *inode; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 8631 | |
| 8632 | ei = kmem_cache_alloc(btrfs_inode_cachep, GFP_NOFS); |
| 8633 | if (!ei) |
| 8634 | return NULL; |
Yan, Zheng | 2ead6ae | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 8635 | |
| 8636 | ei->root = NULL; |
Yan, Zheng | 2ead6ae | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 8637 | ei->generation = 0; |
Josef Bacik | 15ee9bc | 2007-08-10 16:22:09 -0400 | [diff] [blame] | 8638 | ei->last_trans = 0; |
Chris Mason | 257c62e | 2009-10-13 13:21:08 -0400 | [diff] [blame] | 8639 | ei->last_sub_trans = 0; |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 8640 | ei->logged_trans = 0; |
Yan, Zheng | 2ead6ae | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 8641 | ei->delalloc_bytes = 0; |
Wang Shilong | 47059d9 | 2014-07-03 18:22:07 +0800 | [diff] [blame] | 8642 | ei->defrag_bytes = 0; |
Yan, Zheng | 2ead6ae | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 8643 | ei->disk_i_size = 0; |
| 8644 | ei->flags = 0; |
Josef Bacik | 7709cde | 2011-08-04 10:25:02 -0400 | [diff] [blame] | 8645 | ei->csum_bytes = 0; |
Yan, Zheng | 2ead6ae | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 8646 | ei->index_cnt = (u64)-1; |
Miao Xie | 67de117 | 2013-12-26 13:07:06 +0800 | [diff] [blame] | 8647 | ei->dir_index = 0; |
Yan, Zheng | 2ead6ae | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 8648 | ei->last_unlink_trans = 0; |
Liu Bo | 46d8bc3 | 2012-08-29 01:07:55 -0600 | [diff] [blame] | 8649 | ei->last_log_commit = 0; |
Yan, Zheng | 2ead6ae | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 8650 | |
Josef Bacik | 9e0baf6 | 2011-07-15 15:16:44 +0000 | [diff] [blame] | 8651 | spin_lock_init(&ei->lock); |
| 8652 | ei->outstanding_extents = 0; |
| 8653 | ei->reserved_extents = 0; |
Yan, Zheng | 2ead6ae | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 8654 | |
Josef Bacik | 72ac3c0 | 2012-05-23 14:13:11 -0400 | [diff] [blame] | 8655 | ei->runtime_flags = 0; |
Li Zefan | 261507a0 | 2010-12-17 14:21:50 +0800 | [diff] [blame] | 8656 | ei->force_compress = BTRFS_COMPRESS_NONE; |
Yan, Zheng | 2ead6ae | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 8657 | |
Miao Xie | 16cdcec | 2011-04-22 18:12:22 +0800 | [diff] [blame] | 8658 | ei->delayed_node = NULL; |
| 8659 | |
chandan r | 9cc97d6 | 2012-07-04 12:48:07 +0530 | [diff] [blame] | 8660 | ei->i_otime.tv_sec = 0; |
| 8661 | ei->i_otime.tv_nsec = 0; |
| 8662 | |
Yan, Zheng | 2ead6ae | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 8663 | inode = &ei->vfs_inode; |
David Sterba | a8067e0 | 2011-04-21 00:34:43 +0200 | [diff] [blame] | 8664 | extent_map_tree_init(&ei->extent_tree); |
David Sterba | f993c88 | 2011-04-20 23:35:57 +0200 | [diff] [blame] | 8665 | extent_io_tree_init(&ei->io_tree, &inode->i_data); |
| 8666 | extent_io_tree_init(&ei->io_failure_tree, &inode->i_data); |
Josef Bacik | 0b32f4b | 2012-03-13 09:38:00 -0400 | [diff] [blame] | 8667 | ei->io_tree.track_uptodate = 1; |
| 8668 | ei->io_failure_tree.track_uptodate = 1; |
Josef Bacik | b812ce2 | 2012-11-16 13:56:32 -0500 | [diff] [blame] | 8669 | atomic_set(&ei->sync_writers, 0); |
Yan, Zheng | 2ead6ae | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 8670 | mutex_init(&ei->log_mutex); |
Josef Bacik | f248679 | 2012-01-13 12:09:22 -0500 | [diff] [blame] | 8671 | mutex_init(&ei->delalloc_mutex); |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 8672 | btrfs_ordered_inode_tree_init(&ei->ordered_tree); |
Yan, Zheng | 2ead6ae | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 8673 | INIT_LIST_HEAD(&ei->delalloc_inodes); |
Yan, Zheng | 2ead6ae | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 8674 | RB_CLEAR_NODE(&ei->rb_node); |
| 8675 | |
| 8676 | return inode; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 8677 | } |
| 8678 | |
Josef Bacik | aaedb55 | 2013-10-11 14:44:09 -0400 | [diff] [blame] | 8679 | #ifdef CONFIG_BTRFS_FS_RUN_SANITY_TESTS |
| 8680 | void btrfs_test_destroy_inode(struct inode *inode) |
| 8681 | { |
| 8682 | btrfs_drop_extent_cache(inode, 0, (u64)-1, 0); |
| 8683 | kmem_cache_free(btrfs_inode_cachep, BTRFS_I(inode)); |
| 8684 | } |
| 8685 | #endif |
| 8686 | |
Nick Piggin | fa0d7e3d | 2011-01-07 17:49:49 +1100 | [diff] [blame] | 8687 | static void btrfs_i_callback(struct rcu_head *head) |
| 8688 | { |
| 8689 | struct inode *inode = container_of(head, struct inode, i_rcu); |
Nick Piggin | fa0d7e3d | 2011-01-07 17:49:49 +1100 | [diff] [blame] | 8690 | kmem_cache_free(btrfs_inode_cachep, BTRFS_I(inode)); |
| 8691 | } |
| 8692 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 8693 | void btrfs_destroy_inode(struct inode *inode) |
| 8694 | { |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 8695 | struct btrfs_ordered_extent *ordered; |
Chris Mason | 5a3f23d | 2009-03-31 13:27:11 -0400 | [diff] [blame] | 8696 | struct btrfs_root *root = BTRFS_I(inode)->root; |
| 8697 | |
Al Viro | b3d9b7a | 2012-06-09 13:51:19 -0400 | [diff] [blame] | 8698 | WARN_ON(!hlist_empty(&inode->i_dentry)); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 8699 | WARN_ON(inode->i_data.nrpages); |
Josef Bacik | 9e0baf6 | 2011-07-15 15:16:44 +0000 | [diff] [blame] | 8700 | WARN_ON(BTRFS_I(inode)->outstanding_extents); |
| 8701 | WARN_ON(BTRFS_I(inode)->reserved_extents); |
Josef Bacik | 7709cde | 2011-08-04 10:25:02 -0400 | [diff] [blame] | 8702 | WARN_ON(BTRFS_I(inode)->delalloc_bytes); |
| 8703 | WARN_ON(BTRFS_I(inode)->csum_bytes); |
Wang Shilong | 47059d9 | 2014-07-03 18:22:07 +0800 | [diff] [blame] | 8704 | WARN_ON(BTRFS_I(inode)->defrag_bytes); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 8705 | |
Chris Mason | 5a3f23d | 2009-03-31 13:27:11 -0400 | [diff] [blame] | 8706 | /* |
Josef Bacik | a6dbd42 | 2009-11-11 15:53:34 -0500 | [diff] [blame] | 8707 | * This can happen where we create an inode, but somebody else also |
| 8708 | * created the same inode and we need to destroy the one we already |
| 8709 | * created. |
| 8710 | */ |
| 8711 | if (!root) |
| 8712 | goto free; |
| 8713 | |
Josef Bacik | 8a35d95 | 2012-05-23 14:26:42 -0400 | [diff] [blame] | 8714 | if (test_bit(BTRFS_INODE_HAS_ORPHAN_ITEM, |
| 8715 | &BTRFS_I(inode)->runtime_flags)) { |
Simon Kirby | c2cf52e | 2013-03-19 22:41:23 +0000 | [diff] [blame] | 8716 | btrfs_info(root->fs_info, "inode %llu still on the orphan list", |
Geert Uytterhoeven | c1c9ff7 | 2013-08-20 13:20:07 +0200 | [diff] [blame] | 8717 | btrfs_ino(inode)); |
Josef Bacik | 8a35d95 | 2012-05-23 14:26:42 -0400 | [diff] [blame] | 8718 | atomic_dec(&root->orphan_inodes); |
Josef Bacik | 7b12876 | 2008-07-24 12:17:14 -0400 | [diff] [blame] | 8719 | } |
Josef Bacik | 7b12876 | 2008-07-24 12:17:14 -0400 | [diff] [blame] | 8720 | |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 8721 | while (1) { |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 8722 | ordered = btrfs_lookup_first_ordered_extent(inode, (u64)-1); |
| 8723 | if (!ordered) |
| 8724 | break; |
| 8725 | else { |
Simon Kirby | c2cf52e | 2013-03-19 22:41:23 +0000 | [diff] [blame] | 8726 | 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] | 8727 | ordered->file_offset, ordered->len); |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 8728 | btrfs_remove_ordered_extent(inode, ordered); |
| 8729 | btrfs_put_ordered_extent(ordered); |
| 8730 | btrfs_put_ordered_extent(ordered); |
| 8731 | } |
| 8732 | } |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 8733 | inode_tree_del(inode); |
Zheng Yan | 5b21f2e | 2008-09-26 10:05:38 -0400 | [diff] [blame] | 8734 | btrfs_drop_extent_cache(inode, 0, (u64)-1, 0); |
Josef Bacik | a6dbd42 | 2009-11-11 15:53:34 -0500 | [diff] [blame] | 8735 | free: |
Nick Piggin | fa0d7e3d | 2011-01-07 17:49:49 +1100 | [diff] [blame] | 8736 | call_rcu(&inode->i_rcu, btrfs_i_callback); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 8737 | } |
| 8738 | |
Al Viro | 45321ac | 2010-06-07 13:43:19 -0400 | [diff] [blame] | 8739 | int btrfs_drop_inode(struct inode *inode) |
Yan, Zheng | 76dda93 | 2009-09-21 16:00:26 -0400 | [diff] [blame] | 8740 | { |
| 8741 | struct btrfs_root *root = BTRFS_I(inode)->root; |
Al Viro | 45321ac | 2010-06-07 13:43:19 -0400 | [diff] [blame] | 8742 | |
Naohiro Aota | 6379ef9 | 2013-06-06 09:56:34 +0000 | [diff] [blame] | 8743 | if (root == NULL) |
| 8744 | return 1; |
| 8745 | |
Liu Bo | fa6ac87 | 2013-02-20 14:10:23 +0000 | [diff] [blame] | 8746 | /* the snap/subvol tree is on deleting */ |
Stefan Behrens | 69e9c6c | 2013-09-05 16:58:43 +0200 | [diff] [blame] | 8747 | if (btrfs_root_refs(&root->root_item) == 0) |
Al Viro | 45321ac | 2010-06-07 13:43:19 -0400 | [diff] [blame] | 8748 | return 1; |
Yan, Zheng | 76dda93 | 2009-09-21 16:00:26 -0400 | [diff] [blame] | 8749 | else |
Al Viro | 45321ac | 2010-06-07 13:43:19 -0400 | [diff] [blame] | 8750 | return generic_drop_inode(inode); |
Yan, Zheng | 76dda93 | 2009-09-21 16:00:26 -0400 | [diff] [blame] | 8751 | } |
| 8752 | |
Sven Wegener | 0ee0fda | 2008-07-30 16:54:26 -0400 | [diff] [blame] | 8753 | static void init_once(void *foo) |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 8754 | { |
| 8755 | struct btrfs_inode *ei = (struct btrfs_inode *) foo; |
| 8756 | |
| 8757 | inode_init_once(&ei->vfs_inode); |
| 8758 | } |
| 8759 | |
| 8760 | void btrfs_destroy_cachep(void) |
| 8761 | { |
Kirill A. Shutemov | 8c0a853 | 2012-09-26 11:33:07 +1000 | [diff] [blame] | 8762 | /* |
| 8763 | * Make sure all delayed rcu free inodes are flushed before we |
| 8764 | * destroy cache. |
| 8765 | */ |
| 8766 | rcu_barrier(); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 8767 | if (btrfs_inode_cachep) |
| 8768 | kmem_cache_destroy(btrfs_inode_cachep); |
| 8769 | if (btrfs_trans_handle_cachep) |
| 8770 | kmem_cache_destroy(btrfs_trans_handle_cachep); |
| 8771 | if (btrfs_transaction_cachep) |
| 8772 | kmem_cache_destroy(btrfs_transaction_cachep); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 8773 | if (btrfs_path_cachep) |
| 8774 | kmem_cache_destroy(btrfs_path_cachep); |
Josef Bacik | dc89e98 | 2011-01-28 17:05:48 -0500 | [diff] [blame] | 8775 | if (btrfs_free_space_cachep) |
| 8776 | kmem_cache_destroy(btrfs_free_space_cachep); |
Miao Xie | 8ccf6f19 | 2012-10-25 09:28:04 +0000 | [diff] [blame] | 8777 | if (btrfs_delalloc_work_cachep) |
| 8778 | kmem_cache_destroy(btrfs_delalloc_work_cachep); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 8779 | } |
| 8780 | |
| 8781 | int btrfs_init_cachep(void) |
| 8782 | { |
David Sterba | 837e197 | 2012-09-07 03:00:48 -0600 | [diff] [blame] | 8783 | btrfs_inode_cachep = kmem_cache_create("btrfs_inode", |
Christoph Hellwig | 9601e3f | 2009-04-13 15:33:09 +0200 | [diff] [blame] | 8784 | sizeof(struct btrfs_inode), 0, |
| 8785 | SLAB_RECLAIM_ACCOUNT | SLAB_MEM_SPREAD, init_once); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 8786 | if (!btrfs_inode_cachep) |
| 8787 | goto fail; |
Christoph Hellwig | 9601e3f | 2009-04-13 15:33:09 +0200 | [diff] [blame] | 8788 | |
David Sterba | 837e197 | 2012-09-07 03:00:48 -0600 | [diff] [blame] | 8789 | btrfs_trans_handle_cachep = kmem_cache_create("btrfs_trans_handle", |
Christoph Hellwig | 9601e3f | 2009-04-13 15:33:09 +0200 | [diff] [blame] | 8790 | sizeof(struct btrfs_trans_handle), 0, |
| 8791 | SLAB_RECLAIM_ACCOUNT | SLAB_MEM_SPREAD, NULL); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 8792 | if (!btrfs_trans_handle_cachep) |
| 8793 | goto fail; |
Christoph Hellwig | 9601e3f | 2009-04-13 15:33:09 +0200 | [diff] [blame] | 8794 | |
David Sterba | 837e197 | 2012-09-07 03:00:48 -0600 | [diff] [blame] | 8795 | btrfs_transaction_cachep = kmem_cache_create("btrfs_transaction", |
Christoph Hellwig | 9601e3f | 2009-04-13 15:33:09 +0200 | [diff] [blame] | 8796 | sizeof(struct btrfs_transaction), 0, |
| 8797 | SLAB_RECLAIM_ACCOUNT | SLAB_MEM_SPREAD, NULL); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 8798 | if (!btrfs_transaction_cachep) |
| 8799 | goto fail; |
Christoph Hellwig | 9601e3f | 2009-04-13 15:33:09 +0200 | [diff] [blame] | 8800 | |
David Sterba | 837e197 | 2012-09-07 03:00:48 -0600 | [diff] [blame] | 8801 | btrfs_path_cachep = kmem_cache_create("btrfs_path", |
Christoph Hellwig | 9601e3f | 2009-04-13 15:33:09 +0200 | [diff] [blame] | 8802 | sizeof(struct btrfs_path), 0, |
| 8803 | SLAB_RECLAIM_ACCOUNT | SLAB_MEM_SPREAD, NULL); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 8804 | if (!btrfs_path_cachep) |
| 8805 | goto fail; |
Christoph Hellwig | 9601e3f | 2009-04-13 15:33:09 +0200 | [diff] [blame] | 8806 | |
David Sterba | 837e197 | 2012-09-07 03:00:48 -0600 | [diff] [blame] | 8807 | btrfs_free_space_cachep = kmem_cache_create("btrfs_free_space", |
Josef Bacik | dc89e98 | 2011-01-28 17:05:48 -0500 | [diff] [blame] | 8808 | sizeof(struct btrfs_free_space), 0, |
| 8809 | SLAB_RECLAIM_ACCOUNT | SLAB_MEM_SPREAD, NULL); |
| 8810 | if (!btrfs_free_space_cachep) |
| 8811 | goto fail; |
| 8812 | |
Miao Xie | 8ccf6f19 | 2012-10-25 09:28:04 +0000 | [diff] [blame] | 8813 | btrfs_delalloc_work_cachep = kmem_cache_create("btrfs_delalloc_work", |
| 8814 | sizeof(struct btrfs_delalloc_work), 0, |
| 8815 | SLAB_RECLAIM_ACCOUNT | SLAB_MEM_SPREAD, |
| 8816 | NULL); |
| 8817 | if (!btrfs_delalloc_work_cachep) |
| 8818 | goto fail; |
| 8819 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 8820 | return 0; |
| 8821 | fail: |
| 8822 | btrfs_destroy_cachep(); |
| 8823 | return -ENOMEM; |
| 8824 | } |
| 8825 | |
| 8826 | static int btrfs_getattr(struct vfsmount *mnt, |
| 8827 | struct dentry *dentry, struct kstat *stat) |
| 8828 | { |
Miao Xie | df0af1a | 2013-01-29 10:11:59 +0000 | [diff] [blame] | 8829 | u64 delalloc_bytes; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 8830 | struct inode *inode = dentry->d_inode; |
David Sterba | fadc0d8 | 2011-11-20 07:33:38 -0500 | [diff] [blame] | 8831 | u32 blocksize = inode->i_sb->s_blocksize; |
| 8832 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 8833 | generic_fillattr(inode, stat); |
Al Viro | 0ee5dc6 | 2011-07-07 15:44:25 -0400 | [diff] [blame] | 8834 | stat->dev = BTRFS_I(inode)->root->anon_dev; |
Chris Mason | d666746 | 2008-01-03 14:51:00 -0500 | [diff] [blame] | 8835 | stat->blksize = PAGE_CACHE_SIZE; |
Miao Xie | df0af1a | 2013-01-29 10:11:59 +0000 | [diff] [blame] | 8836 | |
| 8837 | spin_lock(&BTRFS_I(inode)->lock); |
| 8838 | delalloc_bytes = BTRFS_I(inode)->delalloc_bytes; |
| 8839 | spin_unlock(&BTRFS_I(inode)->lock); |
David Sterba | fadc0d8 | 2011-11-20 07:33:38 -0500 | [diff] [blame] | 8840 | stat->blocks = (ALIGN(inode_get_bytes(inode), blocksize) + |
Miao Xie | df0af1a | 2013-01-29 10:11:59 +0000 | [diff] [blame] | 8841 | ALIGN(delalloc_bytes, blocksize)) >> 9; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 8842 | return 0; |
| 8843 | } |
| 8844 | |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 8845 | static int btrfs_rename(struct inode *old_dir, struct dentry *old_dentry, |
| 8846 | struct inode *new_dir, struct dentry *new_dentry) |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 8847 | { |
| 8848 | struct btrfs_trans_handle *trans; |
| 8849 | struct btrfs_root *root = BTRFS_I(old_dir)->root; |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 8850 | struct btrfs_root *dest = BTRFS_I(new_dir)->root; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 8851 | struct inode *new_inode = new_dentry->d_inode; |
| 8852 | struct inode *old_inode = old_dentry->d_inode; |
| 8853 | struct timespec ctime = CURRENT_TIME; |
Chris Mason | 00e4e6b | 2008-08-05 11:18:09 -0400 | [diff] [blame] | 8854 | u64 index = 0; |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 8855 | u64 root_objectid; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 8856 | int ret; |
Li Zefan | 33345d01 | 2011-04-20 10:31:50 +0800 | [diff] [blame] | 8857 | u64 old_ino = btrfs_ino(old_inode); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 8858 | |
Li Zefan | 33345d01 | 2011-04-20 10:31:50 +0800 | [diff] [blame] | 8859 | if (btrfs_ino(new_dir) == BTRFS_EMPTY_SUBVOL_DIR_OBJECTID) |
Yan, Zheng | f679a84 | 2009-09-24 09:17:31 -0400 | [diff] [blame] | 8860 | return -EPERM; |
| 8861 | |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 8862 | /* we only allow rename subvolume link between subvolumes */ |
Li Zefan | 33345d01 | 2011-04-20 10:31:50 +0800 | [diff] [blame] | 8863 | if (old_ino != BTRFS_FIRST_FREE_OBJECTID && root != dest) |
Chris Mason | 3394e16 | 2008-11-17 20:42:26 -0500 | [diff] [blame] | 8864 | return -EXDEV; |
| 8865 | |
Li Zefan | 33345d01 | 2011-04-20 10:31:50 +0800 | [diff] [blame] | 8866 | if (old_ino == BTRFS_EMPTY_SUBVOL_DIR_OBJECTID || |
| 8867 | (new_inode && btrfs_ino(new_inode) == BTRFS_FIRST_FREE_OBJECTID)) |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 8868 | return -ENOTEMPTY; |
| 8869 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 8870 | if (S_ISDIR(old_inode->i_mode) && new_inode && |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 8871 | new_inode->i_size > BTRFS_EMPTY_DIR_SIZE) |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 8872 | return -ENOTEMPTY; |
Chris Mason | 9c52057 | 2012-12-17 14:26:57 -0500 | [diff] [blame] | 8873 | |
| 8874 | |
| 8875 | /* check for collisions, even if the name isn't there */ |
Josef Bacik | 4871c15 | 2013-10-09 12:24:04 -0400 | [diff] [blame] | 8876 | ret = btrfs_check_dir_item_collision(dest, new_dir->i_ino, |
Chris Mason | 9c52057 | 2012-12-17 14:26:57 -0500 | [diff] [blame] | 8877 | new_dentry->d_name.name, |
| 8878 | new_dentry->d_name.len); |
| 8879 | |
| 8880 | if (ret) { |
| 8881 | if (ret == -EEXIST) { |
| 8882 | /* we shouldn't get |
| 8883 | * eexist without a new_inode */ |
Dulshani Gunawardhana | fae7f21 | 2013-10-31 10:30:08 +0530 | [diff] [blame] | 8884 | if (WARN_ON(!new_inode)) { |
Chris Mason | 9c52057 | 2012-12-17 14:26:57 -0500 | [diff] [blame] | 8885 | return ret; |
| 8886 | } |
| 8887 | } else { |
| 8888 | /* maybe -EOVERFLOW */ |
| 8889 | return ret; |
| 8890 | } |
| 8891 | } |
| 8892 | ret = 0; |
| 8893 | |
Chris Mason | 5a3f23d | 2009-03-31 13:27:11 -0400 | [diff] [blame] | 8894 | /* |
Chris Mason | 8d875f9 | 2014-08-12 10:47:42 -0700 | [diff] [blame] | 8895 | * we're using rename to replace one file with another. Start IO on it |
| 8896 | * 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] | 8897 | */ |
Chris Mason | 8d875f9 | 2014-08-12 10:47:42 -0700 | [diff] [blame] | 8898 | 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] | 8899 | filemap_flush(old_inode->i_mapping); |
| 8900 | |
Yan, Zheng | 76dda93 | 2009-09-21 16:00:26 -0400 | [diff] [blame] | 8901 | /* close the racy window with snapshot create/destroy ioctl */ |
Li Zefan | 33345d01 | 2011-04-20 10:31:50 +0800 | [diff] [blame] | 8902 | if (old_ino == BTRFS_FIRST_FREE_OBJECTID) |
Yan, Zheng | 76dda93 | 2009-09-21 16:00:26 -0400 | [diff] [blame] | 8903 | down_read(&root->fs_info->subvol_sem); |
Yan, Zheng | a22285a | 2010-05-16 10:48:46 -0400 | [diff] [blame] | 8904 | /* |
| 8905 | * We want to reserve the absolute worst case amount of items. So if |
| 8906 | * both inodes are subvols and we need to unlink them then that would |
| 8907 | * require 4 item modifications, but if they are both normal inodes it |
| 8908 | * would require 5 item modifications, so we'll assume their normal |
| 8909 | * inodes. So 5 * 2 is 10, plus 1 for the new link, so 11 total items |
| 8910 | * should cover the worst case number of items we'll modify. |
| 8911 | */ |
Josef Bacik | 6e137ed | 2013-03-26 15:26:55 -0400 | [diff] [blame] | 8912 | trans = btrfs_start_transaction(root, 11); |
Johann Lombardi | b44c59a | 2011-03-31 13:23:47 +0000 | [diff] [blame] | 8913 | if (IS_ERR(trans)) { |
| 8914 | ret = PTR_ERR(trans); |
| 8915 | goto out_notrans; |
| 8916 | } |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 8917 | |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 8918 | if (dest != root) |
| 8919 | btrfs_record_root_in_trans(trans, dest); |
| 8920 | |
Yan, Zheng | a571952 | 2009-09-24 09:17:31 -0400 | [diff] [blame] | 8921 | ret = btrfs_set_inode_index(new_dir, &index); |
| 8922 | if (ret) |
| 8923 | goto out_fail; |
Chris Mason | 5a3f23d | 2009-03-31 13:27:11 -0400 | [diff] [blame] | 8924 | |
Miao Xie | 67de117 | 2013-12-26 13:07:06 +0800 | [diff] [blame] | 8925 | BTRFS_I(old_inode)->dir_index = 0ULL; |
Li Zefan | 33345d01 | 2011-04-20 10:31:50 +0800 | [diff] [blame] | 8926 | if (unlikely(old_ino == BTRFS_FIRST_FREE_OBJECTID)) { |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 8927 | /* force full log commit if subvolume involved. */ |
Miao Xie | 995946d | 2014-04-02 19:51:06 +0800 | [diff] [blame] | 8928 | btrfs_set_log_full_commit(root->fs_info, trans); |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 8929 | } else { |
Yan, Zheng | a571952 | 2009-09-24 09:17:31 -0400 | [diff] [blame] | 8930 | ret = btrfs_insert_inode_ref(trans, dest, |
| 8931 | new_dentry->d_name.name, |
| 8932 | new_dentry->d_name.len, |
Li Zefan | 33345d01 | 2011-04-20 10:31:50 +0800 | [diff] [blame] | 8933 | old_ino, |
| 8934 | btrfs_ino(new_dir), index); |
Yan, Zheng | a571952 | 2009-09-24 09:17:31 -0400 | [diff] [blame] | 8935 | if (ret) |
| 8936 | goto out_fail; |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 8937 | /* |
| 8938 | * this is an ugly little race, but the rename is required |
| 8939 | * to make sure that if we crash, the inode is either at the |
| 8940 | * old name or the new one. pinning the log transaction lets |
| 8941 | * us make sure we don't allow a log commit to come in after |
| 8942 | * we unlink the name but before we add the new name back in. |
| 8943 | */ |
| 8944 | btrfs_pin_log_trans(root); |
| 8945 | } |
Chris Mason | 5a3f23d | 2009-03-31 13:27:11 -0400 | [diff] [blame] | 8946 | |
Josef Bacik | 0c4d2d9 | 2012-04-05 15:03:02 -0400 | [diff] [blame] | 8947 | inode_inc_iversion(old_dir); |
| 8948 | inode_inc_iversion(new_dir); |
| 8949 | inode_inc_iversion(old_inode); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 8950 | old_dir->i_ctime = old_dir->i_mtime = ctime; |
| 8951 | new_dir->i_ctime = new_dir->i_mtime = ctime; |
| 8952 | old_inode->i_ctime = ctime; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 8953 | |
Chris Mason | 12fcfd2 | 2009-03-24 10:24:20 -0400 | [diff] [blame] | 8954 | if (old_dentry->d_parent != new_dentry->d_parent) |
| 8955 | btrfs_record_unlink_dir(trans, old_dir, old_inode, 1); |
| 8956 | |
Li Zefan | 33345d01 | 2011-04-20 10:31:50 +0800 | [diff] [blame] | 8957 | if (unlikely(old_ino == BTRFS_FIRST_FREE_OBJECTID)) { |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 8958 | root_objectid = BTRFS_I(old_inode)->root->root_key.objectid; |
| 8959 | ret = btrfs_unlink_subvol(trans, root, old_dir, root_objectid, |
| 8960 | old_dentry->d_name.name, |
| 8961 | old_dentry->d_name.len); |
| 8962 | } else { |
Al Viro | 9298679 | 2011-03-04 17:14:37 +0000 | [diff] [blame] | 8963 | ret = __btrfs_unlink_inode(trans, root, old_dir, |
| 8964 | old_dentry->d_inode, |
| 8965 | old_dentry->d_name.name, |
| 8966 | old_dentry->d_name.len); |
| 8967 | if (!ret) |
| 8968 | ret = btrfs_update_inode(trans, root, old_inode); |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 8969 | } |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 8970 | if (ret) { |
| 8971 | btrfs_abort_transaction(trans, root, ret); |
| 8972 | goto out_fail; |
| 8973 | } |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 8974 | |
| 8975 | if (new_inode) { |
Josef Bacik | 0c4d2d9 | 2012-04-05 15:03:02 -0400 | [diff] [blame] | 8976 | inode_inc_iversion(new_inode); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 8977 | new_inode->i_ctime = CURRENT_TIME; |
Li Zefan | 33345d01 | 2011-04-20 10:31:50 +0800 | [diff] [blame] | 8978 | if (unlikely(btrfs_ino(new_inode) == |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 8979 | BTRFS_EMPTY_SUBVOL_DIR_OBJECTID)) { |
| 8980 | root_objectid = BTRFS_I(new_inode)->location.objectid; |
| 8981 | ret = btrfs_unlink_subvol(trans, dest, new_dir, |
| 8982 | root_objectid, |
| 8983 | new_dentry->d_name.name, |
| 8984 | new_dentry->d_name.len); |
| 8985 | BUG_ON(new_inode->i_nlink == 0); |
| 8986 | } else { |
| 8987 | ret = btrfs_unlink_inode(trans, dest, new_dir, |
| 8988 | new_dentry->d_inode, |
| 8989 | new_dentry->d_name.name, |
| 8990 | new_dentry->d_name.len); |
| 8991 | } |
Josef Bacik | 4ef31a4 | 2013-08-13 14:10:08 -0400 | [diff] [blame] | 8992 | if (!ret && new_inode->i_nlink == 0) |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 8993 | ret = btrfs_orphan_add(trans, new_dentry->d_inode); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 8994 | if (ret) { |
| 8995 | btrfs_abort_transaction(trans, root, ret); |
| 8996 | goto out_fail; |
| 8997 | } |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 8998 | } |
Josef Bacik | aec7477 | 2008-07-24 12:12:38 -0400 | [diff] [blame] | 8999 | |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 9000 | ret = btrfs_add_link(trans, new_dir, old_inode, |
| 9001 | new_dentry->d_name.name, |
Yan, Zheng | a571952 | 2009-09-24 09:17:31 -0400 | [diff] [blame] | 9002 | new_dentry->d_name.len, 0, index); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 9003 | if (ret) { |
| 9004 | btrfs_abort_transaction(trans, root, ret); |
| 9005 | goto out_fail; |
| 9006 | } |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 9007 | |
Miao Xie | 67de117 | 2013-12-26 13:07:06 +0800 | [diff] [blame] | 9008 | if (old_inode->i_nlink == 1) |
| 9009 | BTRFS_I(old_inode)->dir_index = index; |
| 9010 | |
Li Zefan | 33345d01 | 2011-04-20 10:31:50 +0800 | [diff] [blame] | 9011 | if (old_ino != BTRFS_FIRST_FREE_OBJECTID) { |
Al Viro | 10d9f30 | 2011-07-16 23:09:10 -0400 | [diff] [blame] | 9012 | struct dentry *parent = new_dentry->d_parent; |
Josef Bacik | 6a91221 | 2010-11-20 09:48:00 +0000 | [diff] [blame] | 9013 | btrfs_log_new_name(trans, old_inode, old_dir, parent); |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 9014 | btrfs_end_log_trans(root); |
| 9015 | } |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 9016 | out_fail: |
Josef Bacik | 7ad85bb | 2012-01-12 19:10:12 -0500 | [diff] [blame] | 9017 | btrfs_end_transaction(trans, root); |
Johann Lombardi | b44c59a | 2011-03-31 13:23:47 +0000 | [diff] [blame] | 9018 | out_notrans: |
Li Zefan | 33345d01 | 2011-04-20 10:31:50 +0800 | [diff] [blame] | 9019 | if (old_ino == BTRFS_FIRST_FREE_OBJECTID) |
Yan, Zheng | 76dda93 | 2009-09-21 16:00:26 -0400 | [diff] [blame] | 9020 | up_read(&root->fs_info->subvol_sem); |
Josef Bacik | 9ed74f2 | 2009-09-11 16:12:44 -0400 | [diff] [blame] | 9021 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 9022 | return ret; |
| 9023 | } |
| 9024 | |
Miklos Szeredi | 80ace85 | 2014-07-23 15:15:32 +0200 | [diff] [blame] | 9025 | static int btrfs_rename2(struct inode *old_dir, struct dentry *old_dentry, |
| 9026 | struct inode *new_dir, struct dentry *new_dentry, |
| 9027 | unsigned int flags) |
| 9028 | { |
| 9029 | if (flags & ~RENAME_NOREPLACE) |
| 9030 | return -EINVAL; |
| 9031 | |
| 9032 | return btrfs_rename(old_dir, old_dentry, new_dir, new_dentry); |
| 9033 | } |
| 9034 | |
Miao Xie | 8ccf6f19 | 2012-10-25 09:28:04 +0000 | [diff] [blame] | 9035 | static void btrfs_run_delalloc_work(struct btrfs_work *work) |
| 9036 | { |
| 9037 | struct btrfs_delalloc_work *delalloc_work; |
Josef Bacik | 9f23e28 | 2013-10-28 15:03:41 -0400 | [diff] [blame] | 9038 | struct inode *inode; |
Miao Xie | 8ccf6f19 | 2012-10-25 09:28:04 +0000 | [diff] [blame] | 9039 | |
| 9040 | delalloc_work = container_of(work, struct btrfs_delalloc_work, |
| 9041 | work); |
Josef Bacik | 9f23e28 | 2013-10-28 15:03:41 -0400 | [diff] [blame] | 9042 | inode = delalloc_work->inode; |
| 9043 | if (delalloc_work->wait) { |
| 9044 | btrfs_wait_ordered_range(inode, 0, (u64)-1); |
| 9045 | } else { |
| 9046 | filemap_flush(inode->i_mapping); |
| 9047 | if (test_bit(BTRFS_INODE_HAS_ASYNC_EXTENT, |
| 9048 | &BTRFS_I(inode)->runtime_flags)) |
| 9049 | filemap_flush(inode->i_mapping); |
| 9050 | } |
Miao Xie | 8ccf6f19 | 2012-10-25 09:28:04 +0000 | [diff] [blame] | 9051 | |
| 9052 | if (delalloc_work->delay_iput) |
Josef Bacik | 9f23e28 | 2013-10-28 15:03:41 -0400 | [diff] [blame] | 9053 | btrfs_add_delayed_iput(inode); |
Miao Xie | 8ccf6f19 | 2012-10-25 09:28:04 +0000 | [diff] [blame] | 9054 | else |
Josef Bacik | 9f23e28 | 2013-10-28 15:03:41 -0400 | [diff] [blame] | 9055 | iput(inode); |
Miao Xie | 8ccf6f19 | 2012-10-25 09:28:04 +0000 | [diff] [blame] | 9056 | complete(&delalloc_work->completion); |
| 9057 | } |
| 9058 | |
| 9059 | struct btrfs_delalloc_work *btrfs_alloc_delalloc_work(struct inode *inode, |
| 9060 | int wait, int delay_iput) |
| 9061 | { |
| 9062 | struct btrfs_delalloc_work *work; |
| 9063 | |
| 9064 | work = kmem_cache_zalloc(btrfs_delalloc_work_cachep, GFP_NOFS); |
| 9065 | if (!work) |
| 9066 | return NULL; |
| 9067 | |
| 9068 | init_completion(&work->completion); |
| 9069 | INIT_LIST_HEAD(&work->list); |
| 9070 | work->inode = inode; |
| 9071 | work->wait = wait; |
| 9072 | work->delay_iput = delay_iput; |
Liu Bo | 9e0af23 | 2014-08-15 23:36:53 +0800 | [diff] [blame] | 9073 | WARN_ON_ONCE(!inode); |
| 9074 | btrfs_init_work(&work->work, btrfs_flush_delalloc_helper, |
| 9075 | btrfs_run_delalloc_work, NULL, NULL); |
Miao Xie | 8ccf6f19 | 2012-10-25 09:28:04 +0000 | [diff] [blame] | 9076 | |
| 9077 | return work; |
| 9078 | } |
| 9079 | |
| 9080 | void btrfs_wait_and_free_delalloc_work(struct btrfs_delalloc_work *work) |
| 9081 | { |
| 9082 | wait_for_completion(&work->completion); |
| 9083 | kmem_cache_free(btrfs_delalloc_work_cachep, work); |
| 9084 | } |
| 9085 | |
Chris Mason | d352ac6 | 2008-09-29 15:18:18 -0400 | [diff] [blame] | 9086 | /* |
| 9087 | * some fairly slow code that needs optimization. This walks the list |
| 9088 | * of all the inodes with pending delalloc and forces them to disk. |
| 9089 | */ |
Miao Xie | 6c255e6 | 2014-03-06 13:55:01 +0800 | [diff] [blame] | 9090 | static int __start_delalloc_inodes(struct btrfs_root *root, int delay_iput, |
| 9091 | int nr) |
Chris Mason | ea8c281 | 2008-08-04 23:17:27 -0400 | [diff] [blame] | 9092 | { |
Chris Mason | ea8c281 | 2008-08-04 23:17:27 -0400 | [diff] [blame] | 9093 | struct btrfs_inode *binode; |
Zheng Yan | 5b21f2e | 2008-09-26 10:05:38 -0400 | [diff] [blame] | 9094 | struct inode *inode; |
Miao Xie | 8ccf6f19 | 2012-10-25 09:28:04 +0000 | [diff] [blame] | 9095 | struct btrfs_delalloc_work *work, *next; |
| 9096 | struct list_head works; |
Miao Xie | 1eafa6c | 2013-01-22 10:49:00 +0000 | [diff] [blame] | 9097 | struct list_head splice; |
Miao Xie | 8ccf6f19 | 2012-10-25 09:28:04 +0000 | [diff] [blame] | 9098 | int ret = 0; |
Chris Mason | ea8c281 | 2008-08-04 23:17:27 -0400 | [diff] [blame] | 9099 | |
Miao Xie | 8ccf6f19 | 2012-10-25 09:28:04 +0000 | [diff] [blame] | 9100 | INIT_LIST_HEAD(&works); |
Miao Xie | 1eafa6c | 2013-01-22 10:49:00 +0000 | [diff] [blame] | 9101 | INIT_LIST_HEAD(&splice); |
Miao Xie | 63607cc | 2013-01-22 10:50:35 +0000 | [diff] [blame] | 9102 | |
Miao Xie | 573bfb7 | 2014-03-06 13:55:03 +0800 | [diff] [blame] | 9103 | mutex_lock(&root->delalloc_mutex); |
Miao Xie | eb73c1b | 2013-05-15 07:48:22 +0000 | [diff] [blame] | 9104 | spin_lock(&root->delalloc_lock); |
| 9105 | list_splice_init(&root->delalloc_inodes, &splice); |
Miao Xie | 1eafa6c | 2013-01-22 10:49:00 +0000 | [diff] [blame] | 9106 | while (!list_empty(&splice)) { |
| 9107 | binode = list_entry(splice.next, struct btrfs_inode, |
Chris Mason | ea8c281 | 2008-08-04 23:17:27 -0400 | [diff] [blame] | 9108 | delalloc_inodes); |
Miao Xie | 1eafa6c | 2013-01-22 10:49:00 +0000 | [diff] [blame] | 9109 | |
Miao Xie | eb73c1b | 2013-05-15 07:48:22 +0000 | [diff] [blame] | 9110 | list_move_tail(&binode->delalloc_inodes, |
| 9111 | &root->delalloc_inodes); |
Zheng Yan | 5b21f2e | 2008-09-26 10:05:38 -0400 | [diff] [blame] | 9112 | inode = igrab(&binode->vfs_inode); |
Miao Xie | df0af1a | 2013-01-29 10:11:59 +0000 | [diff] [blame] | 9113 | if (!inode) { |
Miao Xie | eb73c1b | 2013-05-15 07:48:22 +0000 | [diff] [blame] | 9114 | cond_resched_lock(&root->delalloc_lock); |
Miao Xie | 1eafa6c | 2013-01-22 10:49:00 +0000 | [diff] [blame] | 9115 | continue; |
Miao Xie | df0af1a | 2013-01-29 10:11:59 +0000 | [diff] [blame] | 9116 | } |
Miao Xie | eb73c1b | 2013-05-15 07:48:22 +0000 | [diff] [blame] | 9117 | spin_unlock(&root->delalloc_lock); |
Miao Xie | 1eafa6c | 2013-01-22 10:49:00 +0000 | [diff] [blame] | 9118 | |
| 9119 | work = btrfs_alloc_delalloc_work(inode, 0, delay_iput); |
David Sterba | 5d99a998 | 2014-09-29 19:20:37 +0200 | [diff] [blame] | 9120 | if (!work) { |
Josef Bacik | f4ab9ea | 2013-09-17 11:25:44 -0400 | [diff] [blame] | 9121 | if (delay_iput) |
| 9122 | btrfs_add_delayed_iput(inode); |
| 9123 | else |
| 9124 | iput(inode); |
Miao Xie | 1eafa6c | 2013-01-22 10:49:00 +0000 | [diff] [blame] | 9125 | ret = -ENOMEM; |
Wang Shilong | a1ecaab | 2014-04-02 19:53:32 +0800 | [diff] [blame] | 9126 | goto out; |
Zheng Yan | 5b21f2e | 2008-09-26 10:05:38 -0400 | [diff] [blame] | 9127 | } |
Miao Xie | 1eafa6c | 2013-01-22 10:49:00 +0000 | [diff] [blame] | 9128 | list_add_tail(&work->list, &works); |
Qu Wenruo | a44903a | 2014-02-28 10:46:09 +0800 | [diff] [blame] | 9129 | btrfs_queue_work(root->fs_info->flush_workers, |
| 9130 | &work->work); |
Miao Xie | 6c255e6 | 2014-03-06 13:55:01 +0800 | [diff] [blame] | 9131 | ret++; |
| 9132 | if (nr != -1 && ret >= nr) |
Wang Shilong | a1ecaab | 2014-04-02 19:53:32 +0800 | [diff] [blame] | 9133 | goto out; |
Zheng Yan | 5b21f2e | 2008-09-26 10:05:38 -0400 | [diff] [blame] | 9134 | cond_resched(); |
Miao Xie | eb73c1b | 2013-05-15 07:48:22 +0000 | [diff] [blame] | 9135 | spin_lock(&root->delalloc_lock); |
Chris Mason | ea8c281 | 2008-08-04 23:17:27 -0400 | [diff] [blame] | 9136 | } |
Miao Xie | eb73c1b | 2013-05-15 07:48:22 +0000 | [diff] [blame] | 9137 | spin_unlock(&root->delalloc_lock); |
Chris Mason | 8c8bee1 | 2008-09-29 11:19:10 -0400 | [diff] [blame] | 9138 | |
Wang Shilong | a1ecaab | 2014-04-02 19:53:32 +0800 | [diff] [blame] | 9139 | out: |
Miao Xie | 1eafa6c | 2013-01-22 10:49:00 +0000 | [diff] [blame] | 9140 | list_for_each_entry_safe(work, next, &works, list) { |
| 9141 | list_del_init(&work->list); |
| 9142 | btrfs_wait_and_free_delalloc_work(work); |
| 9143 | } |
| 9144 | |
Miao Xie | eb73c1b | 2013-05-15 07:48:22 +0000 | [diff] [blame] | 9145 | if (!list_empty_careful(&splice)) { |
| 9146 | spin_lock(&root->delalloc_lock); |
| 9147 | list_splice_tail(&splice, &root->delalloc_inodes); |
| 9148 | spin_unlock(&root->delalloc_lock); |
| 9149 | } |
Miao Xie | 573bfb7 | 2014-03-06 13:55:03 +0800 | [diff] [blame] | 9150 | mutex_unlock(&root->delalloc_mutex); |
Miao Xie | eb73c1b | 2013-05-15 07:48:22 +0000 | [diff] [blame] | 9151 | return ret; |
| 9152 | } |
| 9153 | |
| 9154 | int btrfs_start_delalloc_inodes(struct btrfs_root *root, int delay_iput) |
| 9155 | { |
| 9156 | int ret; |
| 9157 | |
Wang Shilong | 2c21b4d | 2014-01-14 19:42:20 +0800 | [diff] [blame] | 9158 | if (test_bit(BTRFS_FS_STATE_ERROR, &root->fs_info->fs_state)) |
Miao Xie | eb73c1b | 2013-05-15 07:48:22 +0000 | [diff] [blame] | 9159 | return -EROFS; |
| 9160 | |
Miao Xie | 6c255e6 | 2014-03-06 13:55:01 +0800 | [diff] [blame] | 9161 | ret = __start_delalloc_inodes(root, delay_iput, -1); |
| 9162 | if (ret > 0) |
| 9163 | ret = 0; |
Miao Xie | eb73c1b | 2013-05-15 07:48:22 +0000 | [diff] [blame] | 9164 | /* |
| 9165 | * the filemap_flush will queue IO into the worker threads, but |
Chris Mason | 8c8bee1 | 2008-09-29 11:19:10 -0400 | [diff] [blame] | 9166 | * we have to make sure the IO is actually started and that |
| 9167 | * ordered extents get created before we return |
| 9168 | */ |
| 9169 | atomic_inc(&root->fs_info->async_submit_draining); |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 9170 | while (atomic_read(&root->fs_info->nr_async_submits) || |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 9171 | atomic_read(&root->fs_info->async_delalloc_pages)) { |
Chris Mason | 8c8bee1 | 2008-09-29 11:19:10 -0400 | [diff] [blame] | 9172 | wait_event(root->fs_info->async_submit_wait, |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 9173 | (atomic_read(&root->fs_info->nr_async_submits) == 0 && |
| 9174 | atomic_read(&root->fs_info->async_delalloc_pages) == 0)); |
Chris Mason | 8c8bee1 | 2008-09-29 11:19:10 -0400 | [diff] [blame] | 9175 | } |
| 9176 | atomic_dec(&root->fs_info->async_submit_draining); |
Miao Xie | eb73c1b | 2013-05-15 07:48:22 +0000 | [diff] [blame] | 9177 | return ret; |
| 9178 | } |
| 9179 | |
Miao Xie | 6c255e6 | 2014-03-06 13:55:01 +0800 | [diff] [blame] | 9180 | int btrfs_start_delalloc_roots(struct btrfs_fs_info *fs_info, int delay_iput, |
| 9181 | int nr) |
Miao Xie | eb73c1b | 2013-05-15 07:48:22 +0000 | [diff] [blame] | 9182 | { |
| 9183 | struct btrfs_root *root; |
| 9184 | struct list_head splice; |
| 9185 | int ret; |
| 9186 | |
Wang Shilong | 2c21b4d | 2014-01-14 19:42:20 +0800 | [diff] [blame] | 9187 | if (test_bit(BTRFS_FS_STATE_ERROR, &fs_info->fs_state)) |
Miao Xie | eb73c1b | 2013-05-15 07:48:22 +0000 | [diff] [blame] | 9188 | return -EROFS; |
| 9189 | |
| 9190 | INIT_LIST_HEAD(&splice); |
| 9191 | |
Miao Xie | 573bfb7 | 2014-03-06 13:55:03 +0800 | [diff] [blame] | 9192 | mutex_lock(&fs_info->delalloc_root_mutex); |
Miao Xie | eb73c1b | 2013-05-15 07:48:22 +0000 | [diff] [blame] | 9193 | spin_lock(&fs_info->delalloc_root_lock); |
| 9194 | list_splice_init(&fs_info->delalloc_roots, &splice); |
Miao Xie | 6c255e6 | 2014-03-06 13:55:01 +0800 | [diff] [blame] | 9195 | while (!list_empty(&splice) && nr) { |
Miao Xie | eb73c1b | 2013-05-15 07:48:22 +0000 | [diff] [blame] | 9196 | root = list_first_entry(&splice, struct btrfs_root, |
| 9197 | delalloc_root); |
| 9198 | root = btrfs_grab_fs_root(root); |
| 9199 | BUG_ON(!root); |
| 9200 | list_move_tail(&root->delalloc_root, |
| 9201 | &fs_info->delalloc_roots); |
| 9202 | spin_unlock(&fs_info->delalloc_root_lock); |
| 9203 | |
Miao Xie | 6c255e6 | 2014-03-06 13:55:01 +0800 | [diff] [blame] | 9204 | ret = __start_delalloc_inodes(root, delay_iput, nr); |
Miao Xie | eb73c1b | 2013-05-15 07:48:22 +0000 | [diff] [blame] | 9205 | btrfs_put_fs_root(root); |
Miao Xie | 6c255e6 | 2014-03-06 13:55:01 +0800 | [diff] [blame] | 9206 | if (ret < 0) |
Miao Xie | eb73c1b | 2013-05-15 07:48:22 +0000 | [diff] [blame] | 9207 | goto out; |
| 9208 | |
Miao Xie | 6c255e6 | 2014-03-06 13:55:01 +0800 | [diff] [blame] | 9209 | if (nr != -1) { |
| 9210 | nr -= ret; |
| 9211 | WARN_ON(nr < 0); |
| 9212 | } |
Miao Xie | eb73c1b | 2013-05-15 07:48:22 +0000 | [diff] [blame] | 9213 | spin_lock(&fs_info->delalloc_root_lock); |
| 9214 | } |
| 9215 | spin_unlock(&fs_info->delalloc_root_lock); |
| 9216 | |
Miao Xie | 6c255e6 | 2014-03-06 13:55:01 +0800 | [diff] [blame] | 9217 | ret = 0; |
Miao Xie | eb73c1b | 2013-05-15 07:48:22 +0000 | [diff] [blame] | 9218 | atomic_inc(&fs_info->async_submit_draining); |
| 9219 | while (atomic_read(&fs_info->nr_async_submits) || |
| 9220 | atomic_read(&fs_info->async_delalloc_pages)) { |
| 9221 | wait_event(fs_info->async_submit_wait, |
| 9222 | (atomic_read(&fs_info->nr_async_submits) == 0 && |
| 9223 | atomic_read(&fs_info->async_delalloc_pages) == 0)); |
| 9224 | } |
| 9225 | atomic_dec(&fs_info->async_submit_draining); |
Miao Xie | 8ccf6f19 | 2012-10-25 09:28:04 +0000 | [diff] [blame] | 9226 | out: |
Miao Xie | 1eafa6c | 2013-01-22 10:49:00 +0000 | [diff] [blame] | 9227 | if (!list_empty_careful(&splice)) { |
Miao Xie | eb73c1b | 2013-05-15 07:48:22 +0000 | [diff] [blame] | 9228 | spin_lock(&fs_info->delalloc_root_lock); |
| 9229 | list_splice_tail(&splice, &fs_info->delalloc_roots); |
| 9230 | spin_unlock(&fs_info->delalloc_root_lock); |
Miao Xie | 1eafa6c | 2013-01-22 10:49:00 +0000 | [diff] [blame] | 9231 | } |
Miao Xie | 573bfb7 | 2014-03-06 13:55:03 +0800 | [diff] [blame] | 9232 | mutex_unlock(&fs_info->delalloc_root_mutex); |
Miao Xie | 8ccf6f19 | 2012-10-25 09:28:04 +0000 | [diff] [blame] | 9233 | return ret; |
Chris Mason | ea8c281 | 2008-08-04 23:17:27 -0400 | [diff] [blame] | 9234 | } |
| 9235 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 9236 | static int btrfs_symlink(struct inode *dir, struct dentry *dentry, |
| 9237 | const char *symname) |
| 9238 | { |
| 9239 | struct btrfs_trans_handle *trans; |
| 9240 | struct btrfs_root *root = BTRFS_I(dir)->root; |
| 9241 | struct btrfs_path *path; |
| 9242 | struct btrfs_key key; |
Chris Mason | 1832a6d | 2007-12-21 16:27:21 -0500 | [diff] [blame] | 9243 | struct inode *inode = NULL; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 9244 | int err; |
| 9245 | int drop_inode = 0; |
| 9246 | u64 objectid; |
Dulshani Gunawardhana | 6787125 | 2013-10-31 10:33:04 +0530 | [diff] [blame] | 9247 | u64 index = 0; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 9248 | int name_len; |
| 9249 | int datasize; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 9250 | unsigned long ptr; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 9251 | struct btrfs_file_extent_item *ei; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 9252 | struct extent_buffer *leaf; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 9253 | |
Filipe David Borba Manana | f06becc | 2013-09-16 09:53:28 +0100 | [diff] [blame] | 9254 | name_len = strlen(symname); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 9255 | if (name_len > BTRFS_MAX_INLINE_DATA_SIZE(root)) |
| 9256 | return -ENAMETOOLONG; |
Chris Mason | 1832a6d | 2007-12-21 16:27:21 -0500 | [diff] [blame] | 9257 | |
Josef Bacik | 9ed74f2 | 2009-09-11 16:12:44 -0400 | [diff] [blame] | 9258 | /* |
| 9259 | * 2 items for inode item and ref |
| 9260 | * 2 items for dir items |
| 9261 | * 1 item for xattr if selinux is on |
| 9262 | */ |
Yan, Zheng | a22285a | 2010-05-16 10:48:46 -0400 | [diff] [blame] | 9263 | trans = btrfs_start_transaction(root, 5); |
| 9264 | if (IS_ERR(trans)) |
| 9265 | return PTR_ERR(trans); |
Chris Mason | 1832a6d | 2007-12-21 16:27:21 -0500 | [diff] [blame] | 9266 | |
Li Zefan | 581bb05 | 2011-04-20 10:06:11 +0800 | [diff] [blame] | 9267 | err = btrfs_find_free_ino(root, &objectid); |
| 9268 | if (err) |
| 9269 | goto out_unlock; |
| 9270 | |
Josef Bacik | aec7477 | 2008-07-24 12:12:38 -0400 | [diff] [blame] | 9271 | inode = btrfs_new_inode(trans, root, dir, dentry->d_name.name, |
Li Zefan | 33345d01 | 2011-04-20 10:31:50 +0800 | [diff] [blame] | 9272 | dentry->d_name.len, btrfs_ino(dir), objectid, |
Josef Bacik | d82a6f1d | 2011-05-11 15:26:06 -0400 | [diff] [blame] | 9273 | S_IFLNK|S_IRWXUGO, &index); |
Tsutomu Itoh | 7cf96da | 2011-04-25 19:43:53 -0400 | [diff] [blame] | 9274 | if (IS_ERR(inode)) { |
| 9275 | err = PTR_ERR(inode); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 9276 | goto out_unlock; |
Tsutomu Itoh | 7cf96da | 2011-04-25 19:43:53 -0400 | [diff] [blame] | 9277 | } |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 9278 | |
Casey Schaufler | ad19db7 | 2011-12-15 10:09:07 -0500 | [diff] [blame] | 9279 | /* |
| 9280 | * If the active LSM wants to access the inode during |
| 9281 | * d_instantiate it needs these. Smack checks to see |
| 9282 | * if the filesystem supports xattrs by looking at the |
| 9283 | * ops vector. |
| 9284 | */ |
| 9285 | inode->i_fop = &btrfs_file_operations; |
| 9286 | inode->i_op = &btrfs_file_inode_operations; |
Chris Mason | b0d5d10 | 2014-09-08 13:08:51 -0700 | [diff] [blame] | 9287 | inode->i_mapping->a_ops = &btrfs_aops; |
| 9288 | inode->i_mapping->backing_dev_info = &root->fs_info->bdi; |
| 9289 | BTRFS_I(inode)->io_tree.ops = &btrfs_extent_io_ops; |
| 9290 | |
| 9291 | err = btrfs_init_inode_security(trans, inode, dir, &dentry->d_name); |
| 9292 | if (err) |
| 9293 | goto out_unlock_inode; |
Casey Schaufler | ad19db7 | 2011-12-15 10:09:07 -0500 | [diff] [blame] | 9294 | |
Josef Bacik | a1b075d | 2010-11-19 20:36:11 +0000 | [diff] [blame] | 9295 | err = btrfs_add_nondir(trans, dir, dentry, inode, 0, index); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 9296 | if (err) |
Chris Mason | b0d5d10 | 2014-09-08 13:08:51 -0700 | [diff] [blame] | 9297 | goto out_unlock_inode; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 9298 | |
| 9299 | path = btrfs_alloc_path(); |
Mark Fasheh | d8926bb | 2011-07-13 10:38:47 -0700 | [diff] [blame] | 9300 | if (!path) { |
| 9301 | err = -ENOMEM; |
Chris Mason | b0d5d10 | 2014-09-08 13:08:51 -0700 | [diff] [blame] | 9302 | goto out_unlock_inode; |
Mark Fasheh | d8926bb | 2011-07-13 10:38:47 -0700 | [diff] [blame] | 9303 | } |
Li Zefan | 33345d01 | 2011-04-20 10:31:50 +0800 | [diff] [blame] | 9304 | key.objectid = btrfs_ino(inode); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 9305 | key.offset = 0; |
David Sterba | 962a298 | 2014-06-04 18:41:45 +0200 | [diff] [blame] | 9306 | key.type = BTRFS_EXTENT_DATA_KEY; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 9307 | datasize = btrfs_file_extent_calc_inline_size(name_len); |
| 9308 | err = btrfs_insert_empty_item(trans, root, path, &key, |
| 9309 | datasize); |
Chris Mason | 54aa1f4 | 2007-06-22 14:16:25 -0400 | [diff] [blame] | 9310 | if (err) { |
Julia Lawall | b083916 | 2011-05-14 07:10:51 +0000 | [diff] [blame] | 9311 | btrfs_free_path(path); |
Chris Mason | b0d5d10 | 2014-09-08 13:08:51 -0700 | [diff] [blame] | 9312 | goto out_unlock_inode; |
Chris Mason | 54aa1f4 | 2007-06-22 14:16:25 -0400 | [diff] [blame] | 9313 | } |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 9314 | leaf = path->nodes[0]; |
| 9315 | ei = btrfs_item_ptr(leaf, path->slots[0], |
| 9316 | struct btrfs_file_extent_item); |
| 9317 | btrfs_set_file_extent_generation(leaf, ei, trans->transid); |
| 9318 | btrfs_set_file_extent_type(leaf, ei, |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 9319 | BTRFS_FILE_EXTENT_INLINE); |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 9320 | btrfs_set_file_extent_encryption(leaf, ei, 0); |
| 9321 | btrfs_set_file_extent_compression(leaf, ei, 0); |
| 9322 | btrfs_set_file_extent_other_encoding(leaf, ei, 0); |
| 9323 | btrfs_set_file_extent_ram_bytes(leaf, ei, name_len); |
| 9324 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 9325 | ptr = btrfs_file_extent_inline_start(ei); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 9326 | write_extent_buffer(leaf, symname, ptr, name_len); |
| 9327 | btrfs_mark_buffer_dirty(leaf); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 9328 | btrfs_free_path(path); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 9329 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 9330 | inode->i_op = &btrfs_symlink_inode_operations; |
| 9331 | inode->i_mapping->a_ops = &btrfs_symlink_aops; |
Chris Mason | 0416008 | 2008-03-26 10:28:07 -0400 | [diff] [blame] | 9332 | inode->i_mapping->backing_dev_info = &root->fs_info->bdi; |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 9333 | inode_set_bytes(inode, name_len); |
Filipe David Borba Manana | f06becc | 2013-09-16 09:53:28 +0100 | [diff] [blame] | 9334 | btrfs_i_size_write(inode, name_len); |
Chris Mason | 54aa1f4 | 2007-06-22 14:16:25 -0400 | [diff] [blame] | 9335 | err = btrfs_update_inode(trans, root, inode); |
Chris Mason | b0d5d10 | 2014-09-08 13:08:51 -0700 | [diff] [blame] | 9336 | if (err) { |
Chris Mason | 54aa1f4 | 2007-06-22 14:16:25 -0400 | [diff] [blame] | 9337 | drop_inode = 1; |
Chris Mason | b0d5d10 | 2014-09-08 13:08:51 -0700 | [diff] [blame] | 9338 | goto out_unlock_inode; |
| 9339 | } |
| 9340 | |
| 9341 | unlock_new_inode(inode); |
| 9342 | d_instantiate(dentry, inode); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 9343 | |
| 9344 | out_unlock: |
Josef Bacik | 7ad85bb | 2012-01-12 19:10:12 -0500 | [diff] [blame] | 9345 | btrfs_end_transaction(trans, root); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 9346 | if (drop_inode) { |
| 9347 | inode_dec_link_count(inode); |
| 9348 | iput(inode); |
| 9349 | } |
Liu Bo | b53d3f5 | 2012-11-14 14:34:34 +0000 | [diff] [blame] | 9350 | btrfs_btree_balance_dirty(root); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 9351 | return err; |
Chris Mason | b0d5d10 | 2014-09-08 13:08:51 -0700 | [diff] [blame] | 9352 | |
| 9353 | out_unlock_inode: |
| 9354 | drop_inode = 1; |
| 9355 | unlock_new_inode(inode); |
| 9356 | goto out_unlock; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 9357 | } |
Chris Mason | 1643298 | 2008-04-10 10:23:21 -0400 | [diff] [blame] | 9358 | |
Josef Bacik | 0af3d00 | 2010-06-21 14:48:16 -0400 | [diff] [blame] | 9359 | static int __btrfs_prealloc_file_range(struct inode *inode, int mode, |
| 9360 | u64 start, u64 num_bytes, u64 min_size, |
| 9361 | loff_t actual_len, u64 *alloc_hint, |
| 9362 | struct btrfs_trans_handle *trans) |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 9363 | { |
Josef Bacik | 5dc562c | 2012-08-17 13:14:17 -0400 | [diff] [blame] | 9364 | struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree; |
| 9365 | struct extent_map *em; |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 9366 | struct btrfs_root *root = BTRFS_I(inode)->root; |
| 9367 | struct btrfs_key ins; |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 9368 | u64 cur_offset = start; |
Josef Bacik | 55a61d1 | 2010-11-22 18:50:32 +0000 | [diff] [blame] | 9369 | u64 i_size; |
Chris Mason | 154ea28 | 2013-03-05 11:11:26 -0500 | [diff] [blame] | 9370 | u64 cur_bytes; |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 9371 | int ret = 0; |
Josef Bacik | 0af3d00 | 2010-06-21 14:48:16 -0400 | [diff] [blame] | 9372 | bool own_trans = true; |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 9373 | |
Josef Bacik | 0af3d00 | 2010-06-21 14:48:16 -0400 | [diff] [blame] | 9374 | if (trans) |
| 9375 | own_trans = false; |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 9376 | while (num_bytes > 0) { |
Josef Bacik | 0af3d00 | 2010-06-21 14:48:16 -0400 | [diff] [blame] | 9377 | if (own_trans) { |
| 9378 | trans = btrfs_start_transaction(root, 3); |
| 9379 | if (IS_ERR(trans)) { |
| 9380 | ret = PTR_ERR(trans); |
| 9381 | break; |
| 9382 | } |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 9383 | } |
Yan, Zheng | 5a303d5 | 2009-11-12 09:34:52 +0000 | [diff] [blame] | 9384 | |
Chris Mason | 154ea28 | 2013-03-05 11:11:26 -0500 | [diff] [blame] | 9385 | cur_bytes = min(num_bytes, 256ULL * 1024 * 1024); |
| 9386 | cur_bytes = max(cur_bytes, min_size); |
Josef Bacik | 0036158 | 2013-08-14 14:02:47 -0400 | [diff] [blame] | 9387 | ret = btrfs_reserve_extent(root, cur_bytes, min_size, 0, |
Miao Xie | e570fd2 | 2014-06-19 10:42:50 +0800 | [diff] [blame] | 9388 | *alloc_hint, &ins, 1, 0); |
Yan, Zheng | 5a303d5 | 2009-11-12 09:34:52 +0000 | [diff] [blame] | 9389 | if (ret) { |
Josef Bacik | 0af3d00 | 2010-06-21 14:48:16 -0400 | [diff] [blame] | 9390 | if (own_trans) |
| 9391 | btrfs_end_transaction(trans, root); |
Yan, Zheng | a22285a | 2010-05-16 10:48:46 -0400 | [diff] [blame] | 9392 | break; |
Yan, Zheng | 5a303d5 | 2009-11-12 09:34:52 +0000 | [diff] [blame] | 9393 | } |
| 9394 | |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 9395 | ret = insert_reserved_file_extent(trans, inode, |
| 9396 | cur_offset, ins.objectid, |
| 9397 | ins.offset, ins.offset, |
Yan, Zheng | 920bbbf | 2009-11-12 09:34:08 +0000 | [diff] [blame] | 9398 | ins.offset, 0, 0, 0, |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 9399 | BTRFS_FILE_EXTENT_PREALLOC); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 9400 | if (ret) { |
Josef Bacik | 857cc2f | 2013-10-07 15:21:08 -0400 | [diff] [blame] | 9401 | btrfs_free_reserved_extent(root, ins.objectid, |
Miao Xie | e570fd2 | 2014-06-19 10:42:50 +0800 | [diff] [blame] | 9402 | ins.offset, 0); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 9403 | btrfs_abort_transaction(trans, root, ret); |
| 9404 | if (own_trans) |
| 9405 | btrfs_end_transaction(trans, root); |
| 9406 | break; |
| 9407 | } |
Chris Mason | a1ed835 | 2009-09-11 12:27:37 -0400 | [diff] [blame] | 9408 | btrfs_drop_extent_cache(inode, cur_offset, |
| 9409 | cur_offset + ins.offset -1, 0); |
Yan, Zheng | 5a303d5 | 2009-11-12 09:34:52 +0000 | [diff] [blame] | 9410 | |
Josef Bacik | 5dc562c | 2012-08-17 13:14:17 -0400 | [diff] [blame] | 9411 | em = alloc_extent_map(); |
| 9412 | if (!em) { |
| 9413 | set_bit(BTRFS_INODE_NEEDS_FULL_SYNC, |
| 9414 | &BTRFS_I(inode)->runtime_flags); |
| 9415 | goto next; |
| 9416 | } |
| 9417 | |
| 9418 | em->start = cur_offset; |
| 9419 | em->orig_start = cur_offset; |
| 9420 | em->len = ins.offset; |
| 9421 | em->block_start = ins.objectid; |
| 9422 | em->block_len = ins.offset; |
Josef Bacik | b493968 | 2012-12-03 10:31:19 -0500 | [diff] [blame] | 9423 | em->orig_block_len = ins.offset; |
Josef Bacik | cc95bef | 2013-04-04 14:31:27 -0400 | [diff] [blame] | 9424 | em->ram_bytes = ins.offset; |
Josef Bacik | 5dc562c | 2012-08-17 13:14:17 -0400 | [diff] [blame] | 9425 | em->bdev = root->fs_info->fs_devices->latest_bdev; |
| 9426 | set_bit(EXTENT_FLAG_PREALLOC, &em->flags); |
| 9427 | em->generation = trans->transid; |
| 9428 | |
| 9429 | while (1) { |
| 9430 | write_lock(&em_tree->lock); |
Josef Bacik | 09a2a8f9 | 2013-04-05 16:51:15 -0400 | [diff] [blame] | 9431 | ret = add_extent_mapping(em_tree, em, 1); |
Josef Bacik | 5dc562c | 2012-08-17 13:14:17 -0400 | [diff] [blame] | 9432 | write_unlock(&em_tree->lock); |
| 9433 | if (ret != -EEXIST) |
| 9434 | break; |
| 9435 | btrfs_drop_extent_cache(inode, cur_offset, |
| 9436 | cur_offset + ins.offset - 1, |
| 9437 | 0); |
| 9438 | } |
| 9439 | free_extent_map(em); |
| 9440 | next: |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 9441 | num_bytes -= ins.offset; |
| 9442 | cur_offset += ins.offset; |
Yan, Zheng | efa5646 | 2010-05-16 10:49:59 -0400 | [diff] [blame] | 9443 | *alloc_hint = ins.objectid + ins.offset; |
Yan, Zheng | 5a303d5 | 2009-11-12 09:34:52 +0000 | [diff] [blame] | 9444 | |
Josef Bacik | 0c4d2d9 | 2012-04-05 15:03:02 -0400 | [diff] [blame] | 9445 | inode_inc_iversion(inode); |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 9446 | inode->i_ctime = CURRENT_TIME; |
Christoph Hellwig | 6cbff00 | 2009-04-17 10:37:41 +0200 | [diff] [blame] | 9447 | BTRFS_I(inode)->flags |= BTRFS_INODE_PREALLOC; |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 9448 | if (!(mode & FALLOC_FL_KEEP_SIZE) && |
Yan, Zheng | efa5646 | 2010-05-16 10:49:59 -0400 | [diff] [blame] | 9449 | (actual_len > inode->i_size) && |
| 9450 | (cur_offset > inode->i_size)) { |
Aneesh Kumar K.V | d1ea6a6 | 2010-01-20 07:28:54 +0000 | [diff] [blame] | 9451 | if (cur_offset > actual_len) |
Josef Bacik | 55a61d1 | 2010-11-22 18:50:32 +0000 | [diff] [blame] | 9452 | i_size = actual_len; |
Aneesh Kumar K.V | d1ea6a6 | 2010-01-20 07:28:54 +0000 | [diff] [blame] | 9453 | else |
Josef Bacik | 55a61d1 | 2010-11-22 18:50:32 +0000 | [diff] [blame] | 9454 | i_size = cur_offset; |
| 9455 | i_size_write(inode, i_size); |
| 9456 | btrfs_ordered_update_i_size(inode, i_size, NULL); |
Yan, Zheng | 5a303d5 | 2009-11-12 09:34:52 +0000 | [diff] [blame] | 9457 | } |
| 9458 | |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 9459 | ret = btrfs_update_inode(trans, root, inode); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 9460 | |
| 9461 | if (ret) { |
| 9462 | btrfs_abort_transaction(trans, root, ret); |
| 9463 | if (own_trans) |
| 9464 | btrfs_end_transaction(trans, root); |
| 9465 | break; |
| 9466 | } |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 9467 | |
Josef Bacik | 0af3d00 | 2010-06-21 14:48:16 -0400 | [diff] [blame] | 9468 | if (own_trans) |
| 9469 | btrfs_end_transaction(trans, root); |
Yan, Zheng | 5a303d5 | 2009-11-12 09:34:52 +0000 | [diff] [blame] | 9470 | } |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 9471 | return ret; |
| 9472 | } |
| 9473 | |
Josef Bacik | 0af3d00 | 2010-06-21 14:48:16 -0400 | [diff] [blame] | 9474 | int btrfs_prealloc_file_range(struct inode *inode, int mode, |
| 9475 | u64 start, u64 num_bytes, u64 min_size, |
| 9476 | loff_t actual_len, u64 *alloc_hint) |
| 9477 | { |
| 9478 | return __btrfs_prealloc_file_range(inode, mode, start, num_bytes, |
| 9479 | min_size, actual_len, alloc_hint, |
| 9480 | NULL); |
| 9481 | } |
| 9482 | |
| 9483 | int btrfs_prealloc_file_range_trans(struct inode *inode, |
| 9484 | struct btrfs_trans_handle *trans, int mode, |
| 9485 | u64 start, u64 num_bytes, u64 min_size, |
| 9486 | loff_t actual_len, u64 *alloc_hint) |
| 9487 | { |
| 9488 | return __btrfs_prealloc_file_range(inode, mode, start, num_bytes, |
| 9489 | min_size, actual_len, alloc_hint, trans); |
| 9490 | } |
| 9491 | |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 9492 | static int btrfs_set_page_dirty(struct page *page) |
| 9493 | { |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 9494 | return __set_page_dirty_nobuffers(page); |
| 9495 | } |
| 9496 | |
Al Viro | 10556cb2 | 2011-06-20 19:28:19 -0400 | [diff] [blame] | 9497 | static int btrfs_permission(struct inode *inode, int mask) |
Yan | fdebe2b | 2008-01-14 13:26:08 -0500 | [diff] [blame] | 9498 | { |
Li Zefan | b83cc96 | 2010-12-20 16:04:08 +0800 | [diff] [blame] | 9499 | struct btrfs_root *root = BTRFS_I(inode)->root; |
Jeff Mahoney | cb6db4e | 2011-08-15 17:27:21 +0000 | [diff] [blame] | 9500 | umode_t mode = inode->i_mode; |
Li Zefan | b83cc96 | 2010-12-20 16:04:08 +0800 | [diff] [blame] | 9501 | |
Jeff Mahoney | cb6db4e | 2011-08-15 17:27:21 +0000 | [diff] [blame] | 9502 | if (mask & MAY_WRITE && |
| 9503 | (S_ISREG(mode) || S_ISDIR(mode) || S_ISLNK(mode))) { |
| 9504 | if (btrfs_root_readonly(root)) |
| 9505 | return -EROFS; |
| 9506 | if (BTRFS_I(inode)->flags & BTRFS_INODE_READONLY) |
| 9507 | return -EACCES; |
| 9508 | } |
Al Viro | 2830ba7 | 2011-06-20 19:16:29 -0400 | [diff] [blame] | 9509 | return generic_permission(inode, mask); |
Yan | fdebe2b | 2008-01-14 13:26:08 -0500 | [diff] [blame] | 9510 | } |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 9511 | |
Filipe Manana | ef3b9af | 2014-04-27 20:40:45 +0100 | [diff] [blame] | 9512 | static int btrfs_tmpfile(struct inode *dir, struct dentry *dentry, umode_t mode) |
| 9513 | { |
| 9514 | struct btrfs_trans_handle *trans; |
| 9515 | struct btrfs_root *root = BTRFS_I(dir)->root; |
| 9516 | struct inode *inode = NULL; |
| 9517 | u64 objectid; |
| 9518 | u64 index; |
| 9519 | int ret = 0; |
| 9520 | |
| 9521 | /* |
| 9522 | * 5 units required for adding orphan entry |
| 9523 | */ |
| 9524 | trans = btrfs_start_transaction(root, 5); |
| 9525 | if (IS_ERR(trans)) |
| 9526 | return PTR_ERR(trans); |
| 9527 | |
| 9528 | ret = btrfs_find_free_ino(root, &objectid); |
| 9529 | if (ret) |
| 9530 | goto out; |
| 9531 | |
| 9532 | inode = btrfs_new_inode(trans, root, dir, NULL, 0, |
| 9533 | btrfs_ino(dir), objectid, mode, &index); |
| 9534 | if (IS_ERR(inode)) { |
| 9535 | ret = PTR_ERR(inode); |
| 9536 | inode = NULL; |
| 9537 | goto out; |
| 9538 | } |
| 9539 | |
Filipe Manana | ef3b9af | 2014-04-27 20:40:45 +0100 | [diff] [blame] | 9540 | inode->i_fop = &btrfs_file_operations; |
| 9541 | inode->i_op = &btrfs_file_inode_operations; |
| 9542 | |
| 9543 | inode->i_mapping->a_ops = &btrfs_aops; |
| 9544 | inode->i_mapping->backing_dev_info = &root->fs_info->bdi; |
| 9545 | BTRFS_I(inode)->io_tree.ops = &btrfs_extent_io_ops; |
| 9546 | |
Chris Mason | b0d5d10 | 2014-09-08 13:08:51 -0700 | [diff] [blame] | 9547 | ret = btrfs_init_inode_security(trans, inode, dir, NULL); |
| 9548 | if (ret) |
| 9549 | goto out_inode; |
| 9550 | |
| 9551 | ret = btrfs_update_inode(trans, root, inode); |
| 9552 | if (ret) |
| 9553 | goto out_inode; |
Filipe Manana | ef3b9af | 2014-04-27 20:40:45 +0100 | [diff] [blame] | 9554 | ret = btrfs_orphan_add(trans, inode); |
| 9555 | if (ret) |
Chris Mason | b0d5d10 | 2014-09-08 13:08:51 -0700 | [diff] [blame] | 9556 | goto out_inode; |
Filipe Manana | ef3b9af | 2014-04-27 20:40:45 +0100 | [diff] [blame] | 9557 | |
Filipe Manana | 5762b5c | 2014-08-01 00:10:32 +0100 | [diff] [blame] | 9558 | /* |
| 9559 | * We set number of links to 0 in btrfs_new_inode(), and here we set |
| 9560 | * it to 1 because d_tmpfile() will issue a warning if the count is 0, |
| 9561 | * through: |
| 9562 | * |
| 9563 | * d_tmpfile() -> inode_dec_link_count() -> drop_nlink() |
| 9564 | */ |
| 9565 | set_nlink(inode, 1); |
Chris Mason | b0d5d10 | 2014-09-08 13:08:51 -0700 | [diff] [blame] | 9566 | unlock_new_inode(inode); |
Filipe Manana | ef3b9af | 2014-04-27 20:40:45 +0100 | [diff] [blame] | 9567 | d_tmpfile(dentry, inode); |
| 9568 | mark_inode_dirty(inode); |
| 9569 | |
| 9570 | out: |
| 9571 | btrfs_end_transaction(trans, root); |
| 9572 | if (ret) |
| 9573 | iput(inode); |
| 9574 | btrfs_balance_delayed_items(root); |
| 9575 | btrfs_btree_balance_dirty(root); |
Filipe Manana | ef3b9af | 2014-04-27 20:40:45 +0100 | [diff] [blame] | 9576 | return ret; |
Chris Mason | b0d5d10 | 2014-09-08 13:08:51 -0700 | [diff] [blame] | 9577 | |
| 9578 | out_inode: |
| 9579 | unlock_new_inode(inode); |
| 9580 | goto out; |
| 9581 | |
Filipe Manana | ef3b9af | 2014-04-27 20:40:45 +0100 | [diff] [blame] | 9582 | } |
| 9583 | |
Filipe Manana | b38ef71 | 2014-11-13 17:01:45 +0000 | [diff] [blame] | 9584 | /* Inspired by filemap_check_errors() */ |
| 9585 | int btrfs_inode_check_errors(struct inode *inode) |
| 9586 | { |
| 9587 | int ret = 0; |
| 9588 | |
| 9589 | if (test_bit(AS_ENOSPC, &inode->i_mapping->flags) && |
| 9590 | test_and_clear_bit(AS_ENOSPC, &inode->i_mapping->flags)) |
| 9591 | ret = -ENOSPC; |
| 9592 | if (test_bit(AS_EIO, &inode->i_mapping->flags) && |
| 9593 | test_and_clear_bit(AS_EIO, &inode->i_mapping->flags)) |
| 9594 | ret = -EIO; |
| 9595 | |
| 9596 | return ret; |
| 9597 | } |
| 9598 | |
Alexey Dobriyan | 6e1d5dc | 2009-09-21 17:01:11 -0700 | [diff] [blame] | 9599 | static const struct inode_operations btrfs_dir_inode_operations = { |
Chris Mason | 3394e16 | 2008-11-17 20:42:26 -0500 | [diff] [blame] | 9600 | .getattr = btrfs_getattr, |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 9601 | .lookup = btrfs_lookup, |
| 9602 | .create = btrfs_create, |
| 9603 | .unlink = btrfs_unlink, |
| 9604 | .link = btrfs_link, |
| 9605 | .mkdir = btrfs_mkdir, |
| 9606 | .rmdir = btrfs_rmdir, |
Miklos Szeredi | 80ace85 | 2014-07-23 15:15:32 +0200 | [diff] [blame] | 9607 | .rename2 = btrfs_rename2, |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 9608 | .symlink = btrfs_symlink, |
| 9609 | .setattr = btrfs_setattr, |
Josef Bacik | 618e21d | 2007-07-11 10:18:17 -0400 | [diff] [blame] | 9610 | .mknod = btrfs_mknod, |
Christoph Hellwig | 95819c0 | 2008-08-28 06:21:17 -0400 | [diff] [blame] | 9611 | .setxattr = btrfs_setxattr, |
| 9612 | .getxattr = btrfs_getxattr, |
Josef Bacik | 5103e94 | 2007-11-16 11:45:54 -0500 | [diff] [blame] | 9613 | .listxattr = btrfs_listxattr, |
Christoph Hellwig | 95819c0 | 2008-08-28 06:21:17 -0400 | [diff] [blame] | 9614 | .removexattr = btrfs_removexattr, |
Yan | fdebe2b | 2008-01-14 13:26:08 -0500 | [diff] [blame] | 9615 | .permission = btrfs_permission, |
Christoph Hellwig | 4e34e71 | 2011-07-23 17:37:31 +0200 | [diff] [blame] | 9616 | .get_acl = btrfs_get_acl, |
Christoph Hellwig | 996a710 | 2013-12-20 05:16:43 -0800 | [diff] [blame] | 9617 | .set_acl = btrfs_set_acl, |
Guangyu Sun | 93fd63c | 2013-09-16 10:42:03 -0700 | [diff] [blame] | 9618 | .update_time = btrfs_update_time, |
Filipe Manana | ef3b9af | 2014-04-27 20:40:45 +0100 | [diff] [blame] | 9619 | .tmpfile = btrfs_tmpfile, |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 9620 | }; |
Alexey Dobriyan | 6e1d5dc | 2009-09-21 17:01:11 -0700 | [diff] [blame] | 9621 | static const struct inode_operations btrfs_dir_ro_inode_operations = { |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 9622 | .lookup = btrfs_lookup, |
Yan | fdebe2b | 2008-01-14 13:26:08 -0500 | [diff] [blame] | 9623 | .permission = btrfs_permission, |
Christoph Hellwig | 4e34e71 | 2011-07-23 17:37:31 +0200 | [diff] [blame] | 9624 | .get_acl = btrfs_get_acl, |
Christoph Hellwig | 996a710 | 2013-12-20 05:16:43 -0800 | [diff] [blame] | 9625 | .set_acl = btrfs_set_acl, |
Guangyu Sun | 93fd63c | 2013-09-16 10:42:03 -0700 | [diff] [blame] | 9626 | .update_time = btrfs_update_time, |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 9627 | }; |
Yan, Zheng | 76dda93 | 2009-09-21 16:00:26 -0400 | [diff] [blame] | 9628 | |
Alexey Dobriyan | 828c095 | 2009-10-01 15:43:56 -0700 | [diff] [blame] | 9629 | static const struct file_operations btrfs_dir_file_operations = { |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 9630 | .llseek = generic_file_llseek, |
| 9631 | .read = generic_read_dir, |
Al Viro | 9cdda8d | 2013-05-22 16:48:09 -0400 | [diff] [blame] | 9632 | .iterate = btrfs_real_readdir, |
Christoph Hellwig | 34287aa | 2007-09-14 10:22:47 -0400 | [diff] [blame] | 9633 | .unlocked_ioctl = btrfs_ioctl, |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 9634 | #ifdef CONFIG_COMPAT |
Christoph Hellwig | 34287aa | 2007-09-14 10:22:47 -0400 | [diff] [blame] | 9635 | .compat_ioctl = btrfs_ioctl, |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 9636 | #endif |
Sage Weil | 6bf13c0 | 2008-06-10 10:07:39 -0400 | [diff] [blame] | 9637 | .release = btrfs_release_file, |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 9638 | .fsync = btrfs_sync_file, |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 9639 | }; |
| 9640 | |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 9641 | static struct extent_io_ops btrfs_extent_io_ops = { |
Chris Mason | 07157aa | 2007-08-30 08:50:51 -0400 | [diff] [blame] | 9642 | .fill_delalloc = run_delalloc_range, |
Chris Mason | 065631f | 2008-02-20 12:07:25 -0500 | [diff] [blame] | 9643 | .submit_bio_hook = btrfs_submit_bio_hook, |
Chris Mason | 239b14b | 2008-03-24 15:02:07 -0400 | [diff] [blame] | 9644 | .merge_bio_hook = btrfs_merge_bio_hook, |
Chris Mason | 07157aa | 2007-08-30 08:50:51 -0400 | [diff] [blame] | 9645 | .readpage_end_io_hook = btrfs_readpage_end_io_hook, |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 9646 | .writepage_end_io_hook = btrfs_writepage_end_io_hook, |
Chris Mason | 247e743 | 2008-07-17 12:53:51 -0400 | [diff] [blame] | 9647 | .writepage_start_hook = btrfs_writepage_start_hook, |
Chris Mason | b0c68f8 | 2008-01-31 11:05:37 -0500 | [diff] [blame] | 9648 | .set_bit_hook = btrfs_set_bit_hook, |
| 9649 | .clear_bit_hook = btrfs_clear_bit_hook, |
Josef Bacik | 9ed74f2 | 2009-09-11 16:12:44 -0400 | [diff] [blame] | 9650 | .merge_extent_hook = btrfs_merge_extent_hook, |
| 9651 | .split_extent_hook = btrfs_split_extent_hook, |
Chris Mason | 07157aa | 2007-08-30 08:50:51 -0400 | [diff] [blame] | 9652 | }; |
| 9653 | |
Chris Mason | 3505439 | 2009-01-21 13:11:13 -0500 | [diff] [blame] | 9654 | /* |
| 9655 | * btrfs doesn't support the bmap operation because swapfiles |
| 9656 | * use bmap to make a mapping of extents in the file. They assume |
| 9657 | * these extents won't change over the life of the file and they |
| 9658 | * use the bmap result to do IO directly to the drive. |
| 9659 | * |
| 9660 | * the btrfs bmap call would return logical addresses that aren't |
| 9661 | * suitable for IO and they also will change frequently as COW |
| 9662 | * operations happen. So, swapfile + btrfs == corruption. |
| 9663 | * |
| 9664 | * For now we're avoiding this by dropping bmap. |
| 9665 | */ |
Alexey Dobriyan | 7f09410 | 2009-09-21 17:01:10 -0700 | [diff] [blame] | 9666 | static const struct address_space_operations btrfs_aops = { |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 9667 | .readpage = btrfs_readpage, |
| 9668 | .writepage = btrfs_writepage, |
Chris Mason | b293f02e | 2007-11-01 19:45:34 -0400 | [diff] [blame] | 9669 | .writepages = btrfs_writepages, |
Chris Mason | 3ab2fb5 | 2007-11-08 10:59:22 -0500 | [diff] [blame] | 9670 | .readpages = btrfs_readpages, |
Chris Mason | 1643298 | 2008-04-10 10:23:21 -0400 | [diff] [blame] | 9671 | .direct_IO = btrfs_direct_IO, |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 9672 | .invalidatepage = btrfs_invalidatepage, |
| 9673 | .releasepage = btrfs_releasepage, |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 9674 | .set_page_dirty = btrfs_set_page_dirty, |
Andi Kleen | 465fdd9 | 2009-09-16 11:50:18 +0200 | [diff] [blame] | 9675 | .error_remove_page = generic_error_remove_page, |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 9676 | }; |
| 9677 | |
Alexey Dobriyan | 7f09410 | 2009-09-21 17:01:10 -0700 | [diff] [blame] | 9678 | static const struct address_space_operations btrfs_symlink_aops = { |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 9679 | .readpage = btrfs_readpage, |
| 9680 | .writepage = btrfs_writepage, |
Chris Mason | 2bf5a72 | 2007-08-30 11:54:02 -0400 | [diff] [blame] | 9681 | .invalidatepage = btrfs_invalidatepage, |
| 9682 | .releasepage = btrfs_releasepage, |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 9683 | }; |
| 9684 | |
Alexey Dobriyan | 6e1d5dc | 2009-09-21 17:01:11 -0700 | [diff] [blame] | 9685 | static const struct inode_operations btrfs_file_inode_operations = { |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 9686 | .getattr = btrfs_getattr, |
| 9687 | .setattr = btrfs_setattr, |
Christoph Hellwig | 95819c0 | 2008-08-28 06:21:17 -0400 | [diff] [blame] | 9688 | .setxattr = btrfs_setxattr, |
| 9689 | .getxattr = btrfs_getxattr, |
Josef Bacik | 5103e94 | 2007-11-16 11:45:54 -0500 | [diff] [blame] | 9690 | .listxattr = btrfs_listxattr, |
Christoph Hellwig | 95819c0 | 2008-08-28 06:21:17 -0400 | [diff] [blame] | 9691 | .removexattr = btrfs_removexattr, |
Yan | fdebe2b | 2008-01-14 13:26:08 -0500 | [diff] [blame] | 9692 | .permission = btrfs_permission, |
Yehuda Sadeh | 1506fcc | 2009-01-21 14:39:14 -0500 | [diff] [blame] | 9693 | .fiemap = btrfs_fiemap, |
Christoph Hellwig | 4e34e71 | 2011-07-23 17:37:31 +0200 | [diff] [blame] | 9694 | .get_acl = btrfs_get_acl, |
Christoph Hellwig | 996a710 | 2013-12-20 05:16:43 -0800 | [diff] [blame] | 9695 | .set_acl = btrfs_set_acl, |
Josef Bacik | e41f941 | 2012-03-26 09:46:47 -0400 | [diff] [blame] | 9696 | .update_time = btrfs_update_time, |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 9697 | }; |
Alexey Dobriyan | 6e1d5dc | 2009-09-21 17:01:11 -0700 | [diff] [blame] | 9698 | static const struct inode_operations btrfs_special_inode_operations = { |
Josef Bacik | 618e21d | 2007-07-11 10:18:17 -0400 | [diff] [blame] | 9699 | .getattr = btrfs_getattr, |
| 9700 | .setattr = btrfs_setattr, |
Yan | fdebe2b | 2008-01-14 13:26:08 -0500 | [diff] [blame] | 9701 | .permission = btrfs_permission, |
Christoph Hellwig | 95819c0 | 2008-08-28 06:21:17 -0400 | [diff] [blame] | 9702 | .setxattr = btrfs_setxattr, |
| 9703 | .getxattr = btrfs_getxattr, |
Josef Bacik | 33268ea | 2008-07-24 12:16:36 -0400 | [diff] [blame] | 9704 | .listxattr = btrfs_listxattr, |
Christoph Hellwig | 95819c0 | 2008-08-28 06:21:17 -0400 | [diff] [blame] | 9705 | .removexattr = btrfs_removexattr, |
Christoph Hellwig | 4e34e71 | 2011-07-23 17:37:31 +0200 | [diff] [blame] | 9706 | .get_acl = btrfs_get_acl, |
Christoph Hellwig | 996a710 | 2013-12-20 05:16:43 -0800 | [diff] [blame] | 9707 | .set_acl = btrfs_set_acl, |
Josef Bacik | e41f941 | 2012-03-26 09:46:47 -0400 | [diff] [blame] | 9708 | .update_time = btrfs_update_time, |
Josef Bacik | 618e21d | 2007-07-11 10:18:17 -0400 | [diff] [blame] | 9709 | }; |
Alexey Dobriyan | 6e1d5dc | 2009-09-21 17:01:11 -0700 | [diff] [blame] | 9710 | static const struct inode_operations btrfs_symlink_inode_operations = { |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 9711 | .readlink = generic_readlink, |
| 9712 | .follow_link = page_follow_link_light, |
| 9713 | .put_link = page_put_link, |
Li Zefan | f209561 | 2010-11-19 02:05:24 +0000 | [diff] [blame] | 9714 | .getattr = btrfs_getattr, |
Josef Bacik | 22c44fe | 2011-11-30 10:45:38 -0500 | [diff] [blame] | 9715 | .setattr = btrfs_setattr, |
Yan | fdebe2b | 2008-01-14 13:26:08 -0500 | [diff] [blame] | 9716 | .permission = btrfs_permission, |
Jim Owens | 0279b4c | 2009-02-04 09:29:13 -0500 | [diff] [blame] | 9717 | .setxattr = btrfs_setxattr, |
| 9718 | .getxattr = btrfs_getxattr, |
| 9719 | .listxattr = btrfs_listxattr, |
| 9720 | .removexattr = btrfs_removexattr, |
Josef Bacik | e41f941 | 2012-03-26 09:46:47 -0400 | [diff] [blame] | 9721 | .update_time = btrfs_update_time, |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 9722 | }; |
Yan, Zheng | 76dda93 | 2009-09-21 16:00:26 -0400 | [diff] [blame] | 9723 | |
Alexey Dobriyan | 82d339d | 2009-10-09 09:54:36 -0400 | [diff] [blame] | 9724 | const struct dentry_operations btrfs_dentry_operations = { |
Yan, Zheng | 76dda93 | 2009-09-21 16:00:26 -0400 | [diff] [blame] | 9725 | .d_delete = btrfs_dentry_delete, |
Josef Bacik | b4aff1f | 2011-06-28 16:18:59 -0400 | [diff] [blame] | 9726 | .d_release = btrfs_dentry_release, |
Yan, Zheng | 76dda93 | 2009-09-21 16:00:26 -0400 | [diff] [blame] | 9727 | }; |