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 | { |
Yan, Zheng | 0ca1f7c | 2010-05-16 10:48:47 -0400 | [diff] [blame] | 1533 | /* not delalloc, ignore it */ |
Josef Bacik | 9ed74f2 | 2009-09-11 16:12:44 -0400 | [diff] [blame] | 1534 | if (!(orig->state & EXTENT_DELALLOC)) |
Jeff Mahoney | 1bf8504 | 2011-07-21 16:56:09 +0000 | [diff] [blame] | 1535 | return; |
Josef Bacik | 9ed74f2 | 2009-09-11 16:12:44 -0400 | [diff] [blame] | 1536 | |
Josef Bacik | 9e0baf6 | 2011-07-15 15:16:44 +0000 | [diff] [blame] | 1537 | spin_lock(&BTRFS_I(inode)->lock); |
| 1538 | BTRFS_I(inode)->outstanding_extents++; |
| 1539 | spin_unlock(&BTRFS_I(inode)->lock); |
Josef Bacik | 9ed74f2 | 2009-09-11 16:12:44 -0400 | [diff] [blame] | 1540 | } |
| 1541 | |
| 1542 | /* |
| 1543 | * extent_io.c merge_extent_hook, used to track merged delayed allocation |
| 1544 | * extents so we can keep track of new extents that are just merged onto old |
| 1545 | * extents, such as when we are doing sequential writes, so we can properly |
| 1546 | * account for the metadata space we'll need. |
| 1547 | */ |
Jeff Mahoney | 1bf8504 | 2011-07-21 16:56:09 +0000 | [diff] [blame] | 1548 | static void btrfs_merge_extent_hook(struct inode *inode, |
| 1549 | struct extent_state *new, |
| 1550 | struct extent_state *other) |
Josef Bacik | 9ed74f2 | 2009-09-11 16:12:44 -0400 | [diff] [blame] | 1551 | { |
Josef Bacik | 9ed74f2 | 2009-09-11 16:12:44 -0400 | [diff] [blame] | 1552 | /* not delalloc, ignore it */ |
| 1553 | if (!(other->state & EXTENT_DELALLOC)) |
Jeff Mahoney | 1bf8504 | 2011-07-21 16:56:09 +0000 | [diff] [blame] | 1554 | return; |
Josef Bacik | 9ed74f2 | 2009-09-11 16:12:44 -0400 | [diff] [blame] | 1555 | |
Josef Bacik | 9e0baf6 | 2011-07-15 15:16:44 +0000 | [diff] [blame] | 1556 | spin_lock(&BTRFS_I(inode)->lock); |
| 1557 | BTRFS_I(inode)->outstanding_extents--; |
| 1558 | spin_unlock(&BTRFS_I(inode)->lock); |
Josef Bacik | 9ed74f2 | 2009-09-11 16:12:44 -0400 | [diff] [blame] | 1559 | } |
| 1560 | |
Miao Xie | eb73c1b | 2013-05-15 07:48:22 +0000 | [diff] [blame] | 1561 | static void btrfs_add_delalloc_inodes(struct btrfs_root *root, |
| 1562 | struct inode *inode) |
| 1563 | { |
| 1564 | spin_lock(&root->delalloc_lock); |
| 1565 | if (list_empty(&BTRFS_I(inode)->delalloc_inodes)) { |
| 1566 | list_add_tail(&BTRFS_I(inode)->delalloc_inodes, |
| 1567 | &root->delalloc_inodes); |
| 1568 | set_bit(BTRFS_INODE_IN_DELALLOC_LIST, |
| 1569 | &BTRFS_I(inode)->runtime_flags); |
| 1570 | root->nr_delalloc_inodes++; |
| 1571 | if (root->nr_delalloc_inodes == 1) { |
| 1572 | spin_lock(&root->fs_info->delalloc_root_lock); |
| 1573 | BUG_ON(!list_empty(&root->delalloc_root)); |
| 1574 | list_add_tail(&root->delalloc_root, |
| 1575 | &root->fs_info->delalloc_roots); |
| 1576 | spin_unlock(&root->fs_info->delalloc_root_lock); |
| 1577 | } |
| 1578 | } |
| 1579 | spin_unlock(&root->delalloc_lock); |
| 1580 | } |
| 1581 | |
| 1582 | static void btrfs_del_delalloc_inode(struct btrfs_root *root, |
| 1583 | struct inode *inode) |
| 1584 | { |
| 1585 | spin_lock(&root->delalloc_lock); |
| 1586 | if (!list_empty(&BTRFS_I(inode)->delalloc_inodes)) { |
| 1587 | list_del_init(&BTRFS_I(inode)->delalloc_inodes); |
| 1588 | clear_bit(BTRFS_INODE_IN_DELALLOC_LIST, |
| 1589 | &BTRFS_I(inode)->runtime_flags); |
| 1590 | root->nr_delalloc_inodes--; |
| 1591 | if (!root->nr_delalloc_inodes) { |
| 1592 | spin_lock(&root->fs_info->delalloc_root_lock); |
| 1593 | BUG_ON(list_empty(&root->delalloc_root)); |
| 1594 | list_del_init(&root->delalloc_root); |
| 1595 | spin_unlock(&root->fs_info->delalloc_root_lock); |
| 1596 | } |
| 1597 | } |
| 1598 | spin_unlock(&root->delalloc_lock); |
| 1599 | } |
| 1600 | |
Chris Mason | d352ac6 | 2008-09-29 15:18:18 -0400 | [diff] [blame] | 1601 | /* |
| 1602 | * extent_io.c set_bit_hook, used to track delayed allocation |
| 1603 | * bytes in this file, and to maintain the list of inodes that |
| 1604 | * have pending delalloc work to be done. |
| 1605 | */ |
Jeff Mahoney | 1bf8504 | 2011-07-21 16:56:09 +0000 | [diff] [blame] | 1606 | static void btrfs_set_bit_hook(struct inode *inode, |
David Sterba | 4107488 | 2013-04-29 13:38:46 +0000 | [diff] [blame] | 1607 | struct extent_state *state, unsigned long *bits) |
Chris Mason | 291d673 | 2008-01-29 15:55:23 -0500 | [diff] [blame] | 1608 | { |
Josef Bacik | 9ed74f2 | 2009-09-11 16:12:44 -0400 | [diff] [blame] | 1609 | |
Wang Shilong | 47059d9 | 2014-07-03 18:22:07 +0800 | [diff] [blame] | 1610 | if ((*bits & EXTENT_DEFRAG) && !(*bits & EXTENT_DELALLOC)) |
| 1611 | WARN_ON(1); |
Chris Mason | 75eff68 | 2008-12-15 15:54:40 -0500 | [diff] [blame] | 1612 | /* |
| 1613 | * set_bit and clear bit hooks normally require _irqsave/restore |
Sergei Trofimovich | 27160b6 | 2011-05-20 20:20:32 +0000 | [diff] [blame] | 1614 | * but in this case, we are only testing for the DELALLOC |
Chris Mason | 75eff68 | 2008-12-15 15:54:40 -0500 | [diff] [blame] | 1615 | * bit, which is only set or cleared with irqs on |
| 1616 | */ |
Yan, Zheng | 0ca1f7c | 2010-05-16 10:48:47 -0400 | [diff] [blame] | 1617 | if (!(state->state & EXTENT_DELALLOC) && (*bits & EXTENT_DELALLOC)) { |
Chris Mason | 291d673 | 2008-01-29 15:55:23 -0500 | [diff] [blame] | 1618 | struct btrfs_root *root = BTRFS_I(inode)->root; |
Yan, Zheng | 0ca1f7c | 2010-05-16 10:48:47 -0400 | [diff] [blame] | 1619 | u64 len = state->end + 1 - state->start; |
Liu Bo | 83eea1f | 2012-07-10 05:28:39 -0600 | [diff] [blame] | 1620 | bool do_list = !btrfs_is_free_space_inode(inode); |
Josef Bacik | 9ed74f2 | 2009-09-11 16:12:44 -0400 | [diff] [blame] | 1621 | |
Josef Bacik | 9e0baf6 | 2011-07-15 15:16:44 +0000 | [diff] [blame] | 1622 | if (*bits & EXTENT_FIRST_DELALLOC) { |
Yan, Zheng | 0ca1f7c | 2010-05-16 10:48:47 -0400 | [diff] [blame] | 1623 | *bits &= ~EXTENT_FIRST_DELALLOC; |
Josef Bacik | 9e0baf6 | 2011-07-15 15:16:44 +0000 | [diff] [blame] | 1624 | } else { |
| 1625 | spin_lock(&BTRFS_I(inode)->lock); |
| 1626 | BTRFS_I(inode)->outstanding_extents++; |
| 1627 | spin_unlock(&BTRFS_I(inode)->lock); |
| 1628 | } |
Josef Bacik | 287a0ab | 2010-03-19 18:07:23 +0000 | [diff] [blame] | 1629 | |
Miao Xie | 963d678 | 2013-01-29 10:10:51 +0000 | [diff] [blame] | 1630 | __percpu_counter_add(&root->fs_info->delalloc_bytes, len, |
| 1631 | root->fs_info->delalloc_batch); |
Miao Xie | df0af1a | 2013-01-29 10:11:59 +0000 | [diff] [blame] | 1632 | spin_lock(&BTRFS_I(inode)->lock); |
Yan, Zheng | 0ca1f7c | 2010-05-16 10:48:47 -0400 | [diff] [blame] | 1633 | BTRFS_I(inode)->delalloc_bytes += len; |
Wang Shilong | 47059d9 | 2014-07-03 18:22:07 +0800 | [diff] [blame] | 1634 | if (*bits & EXTENT_DEFRAG) |
| 1635 | BTRFS_I(inode)->defrag_bytes += len; |
Miao Xie | df0af1a | 2013-01-29 10:11:59 +0000 | [diff] [blame] | 1636 | if (do_list && !test_bit(BTRFS_INODE_IN_DELALLOC_LIST, |
Miao Xie | eb73c1b | 2013-05-15 07:48:22 +0000 | [diff] [blame] | 1637 | &BTRFS_I(inode)->runtime_flags)) |
| 1638 | btrfs_add_delalloc_inodes(root, inode); |
Miao Xie | df0af1a | 2013-01-29 10:11:59 +0000 | [diff] [blame] | 1639 | spin_unlock(&BTRFS_I(inode)->lock); |
Chris Mason | 291d673 | 2008-01-29 15:55:23 -0500 | [diff] [blame] | 1640 | } |
Chris Mason | 291d673 | 2008-01-29 15:55:23 -0500 | [diff] [blame] | 1641 | } |
| 1642 | |
Chris Mason | d352ac6 | 2008-09-29 15:18:18 -0400 | [diff] [blame] | 1643 | /* |
| 1644 | * extent_io.c clear_bit_hook, see set_bit_hook for why |
| 1645 | */ |
Jeff Mahoney | 1bf8504 | 2011-07-21 16:56:09 +0000 | [diff] [blame] | 1646 | static void btrfs_clear_bit_hook(struct inode *inode, |
David Sterba | 4107488 | 2013-04-29 13:38:46 +0000 | [diff] [blame] | 1647 | struct extent_state *state, |
| 1648 | unsigned long *bits) |
Chris Mason | 291d673 | 2008-01-29 15:55:23 -0500 | [diff] [blame] | 1649 | { |
Wang Shilong | 47059d9 | 2014-07-03 18:22:07 +0800 | [diff] [blame] | 1650 | u64 len = state->end + 1 - state->start; |
| 1651 | |
| 1652 | spin_lock(&BTRFS_I(inode)->lock); |
| 1653 | if ((state->state & EXTENT_DEFRAG) && (*bits & EXTENT_DEFRAG)) |
| 1654 | BTRFS_I(inode)->defrag_bytes -= len; |
| 1655 | spin_unlock(&BTRFS_I(inode)->lock); |
| 1656 | |
Chris Mason | 75eff68 | 2008-12-15 15:54:40 -0500 | [diff] [blame] | 1657 | /* |
| 1658 | * set_bit and clear bit hooks normally require _irqsave/restore |
Sergei Trofimovich | 27160b6 | 2011-05-20 20:20:32 +0000 | [diff] [blame] | 1659 | * but in this case, we are only testing for the DELALLOC |
Chris Mason | 75eff68 | 2008-12-15 15:54:40 -0500 | [diff] [blame] | 1660 | * bit, which is only set or cleared with irqs on |
| 1661 | */ |
Yan, Zheng | 0ca1f7c | 2010-05-16 10:48:47 -0400 | [diff] [blame] | 1662 | if ((state->state & EXTENT_DELALLOC) && (*bits & EXTENT_DELALLOC)) { |
Chris Mason | 291d673 | 2008-01-29 15:55:23 -0500 | [diff] [blame] | 1663 | struct btrfs_root *root = BTRFS_I(inode)->root; |
Liu Bo | 83eea1f | 2012-07-10 05:28:39 -0600 | [diff] [blame] | 1664 | bool do_list = !btrfs_is_free_space_inode(inode); |
Chris Mason | bcbfce8 | 2008-04-22 13:26:47 -0400 | [diff] [blame] | 1665 | |
Josef Bacik | 9e0baf6 | 2011-07-15 15:16:44 +0000 | [diff] [blame] | 1666 | if (*bits & EXTENT_FIRST_DELALLOC) { |
Yan, Zheng | 0ca1f7c | 2010-05-16 10:48:47 -0400 | [diff] [blame] | 1667 | *bits &= ~EXTENT_FIRST_DELALLOC; |
Josef Bacik | 9e0baf6 | 2011-07-15 15:16:44 +0000 | [diff] [blame] | 1668 | } else if (!(*bits & EXTENT_DO_ACCOUNTING)) { |
| 1669 | spin_lock(&BTRFS_I(inode)->lock); |
| 1670 | BTRFS_I(inode)->outstanding_extents--; |
| 1671 | spin_unlock(&BTRFS_I(inode)->lock); |
| 1672 | } |
Yan, Zheng | 0ca1f7c | 2010-05-16 10:48:47 -0400 | [diff] [blame] | 1673 | |
Josef Bacik | b6d08f0 | 2013-09-27 14:57:43 -0400 | [diff] [blame] | 1674 | /* |
| 1675 | * We don't reserve metadata space for space cache inodes so we |
| 1676 | * don't need to call dellalloc_release_metadata if there is an |
| 1677 | * error. |
| 1678 | */ |
| 1679 | if (*bits & EXTENT_DO_ACCOUNTING && |
| 1680 | root != root->fs_info->tree_root) |
Yan, Zheng | 0ca1f7c | 2010-05-16 10:48:47 -0400 | [diff] [blame] | 1681 | btrfs_delalloc_release_metadata(inode, len); |
| 1682 | |
Josef Bacik | 0cb59c9 | 2010-07-02 12:14:14 -0400 | [diff] [blame] | 1683 | if (root->root_key.objectid != BTRFS_DATA_RELOC_TREE_OBJECTID |
Josef Bacik | 7ee9e44 | 2013-06-21 16:37:03 -0400 | [diff] [blame] | 1684 | && do_list && !(state->state & EXTENT_NORESERVE)) |
Yan, Zheng | 0ca1f7c | 2010-05-16 10:48:47 -0400 | [diff] [blame] | 1685 | btrfs_free_reserved_data_space(inode, len); |
Josef Bacik | 9ed74f2 | 2009-09-11 16:12:44 -0400 | [diff] [blame] | 1686 | |
Miao Xie | 963d678 | 2013-01-29 10:10:51 +0000 | [diff] [blame] | 1687 | __percpu_counter_add(&root->fs_info->delalloc_bytes, -len, |
| 1688 | root->fs_info->delalloc_batch); |
Miao Xie | df0af1a | 2013-01-29 10:11:59 +0000 | [diff] [blame] | 1689 | spin_lock(&BTRFS_I(inode)->lock); |
Yan, Zheng | 0ca1f7c | 2010-05-16 10:48:47 -0400 | [diff] [blame] | 1690 | BTRFS_I(inode)->delalloc_bytes -= len; |
Josef Bacik | 0cb59c9 | 2010-07-02 12:14:14 -0400 | [diff] [blame] | 1691 | if (do_list && BTRFS_I(inode)->delalloc_bytes == 0 && |
Miao Xie | df0af1a | 2013-01-29 10:11:59 +0000 | [diff] [blame] | 1692 | test_bit(BTRFS_INODE_IN_DELALLOC_LIST, |
Miao Xie | eb73c1b | 2013-05-15 07:48:22 +0000 | [diff] [blame] | 1693 | &BTRFS_I(inode)->runtime_flags)) |
| 1694 | btrfs_del_delalloc_inode(root, inode); |
Miao Xie | df0af1a | 2013-01-29 10:11:59 +0000 | [diff] [blame] | 1695 | spin_unlock(&BTRFS_I(inode)->lock); |
Chris Mason | 291d673 | 2008-01-29 15:55:23 -0500 | [diff] [blame] | 1696 | } |
Chris Mason | 291d673 | 2008-01-29 15:55:23 -0500 | [diff] [blame] | 1697 | } |
| 1698 | |
Chris Mason | d352ac6 | 2008-09-29 15:18:18 -0400 | [diff] [blame] | 1699 | /* |
| 1700 | * extent_io.c merge_bio_hook, this must check the chunk tree to make sure |
| 1701 | * we don't create bios that span stripes or chunks |
| 1702 | */ |
David Woodhouse | 64a1670 | 2009-07-15 23:29:37 +0100 | [diff] [blame] | 1703 | 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] | 1704 | size_t size, struct bio *bio, |
| 1705 | unsigned long bio_flags) |
Chris Mason | 239b14b | 2008-03-24 15:02:07 -0400 | [diff] [blame] | 1706 | { |
| 1707 | struct btrfs_root *root = BTRFS_I(page->mapping->host)->root; |
Kent Overstreet | 4f024f3 | 2013-10-11 15:44:27 -0700 | [diff] [blame] | 1708 | u64 logical = (u64)bio->bi_iter.bi_sector << 9; |
Chris Mason | 239b14b | 2008-03-24 15:02:07 -0400 | [diff] [blame] | 1709 | u64 length = 0; |
| 1710 | u64 map_length; |
Chris Mason | 239b14b | 2008-03-24 15:02:07 -0400 | [diff] [blame] | 1711 | int ret; |
| 1712 | |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 1713 | if (bio_flags & EXTENT_BIO_COMPRESSED) |
| 1714 | return 0; |
| 1715 | |
Kent Overstreet | 4f024f3 | 2013-10-11 15:44:27 -0700 | [diff] [blame] | 1716 | length = bio->bi_iter.bi_size; |
Chris Mason | 239b14b | 2008-03-24 15:02:07 -0400 | [diff] [blame] | 1717 | map_length = length; |
David Woodhouse | 64a1670 | 2009-07-15 23:29:37 +0100 | [diff] [blame] | 1718 | ret = btrfs_map_block(root->fs_info, rw, logical, |
Chris Mason | f188591 | 2008-04-09 16:28:12 -0400 | [diff] [blame] | 1719 | &map_length, NULL, 0); |
Stefan Behrens | 3ec706c | 2012-11-05 15:46:42 +0100 | [diff] [blame] | 1720 | /* Will always return 0 with map_multi == NULL */ |
Jeff Mahoney | 3444a97 | 2011-10-03 23:23:13 -0400 | [diff] [blame] | 1721 | BUG_ON(ret < 0); |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 1722 | if (map_length < length + size) |
Chris Mason | 239b14b | 2008-03-24 15:02:07 -0400 | [diff] [blame] | 1723 | return 1; |
Jeff Mahoney | 3444a97 | 2011-10-03 23:23:13 -0400 | [diff] [blame] | 1724 | return 0; |
Chris Mason | 239b14b | 2008-03-24 15:02:07 -0400 | [diff] [blame] | 1725 | } |
| 1726 | |
Chris Mason | d352ac6 | 2008-09-29 15:18:18 -0400 | [diff] [blame] | 1727 | /* |
| 1728 | * in order to insert checksums into the metadata in large chunks, |
| 1729 | * we wait until bio submission time. All the pages in the bio are |
| 1730 | * checksummed and sums are attached onto the ordered extent record. |
| 1731 | * |
| 1732 | * At IO completion time the cums attached on the ordered extent record |
| 1733 | * are inserted into the btree |
| 1734 | */ |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 1735 | static int __btrfs_submit_bio_start(struct inode *inode, int rw, |
| 1736 | struct bio *bio, int mirror_num, |
Chris Mason | eaf25d9 | 2010-05-25 09:48:28 -0400 | [diff] [blame] | 1737 | unsigned long bio_flags, |
| 1738 | u64 bio_offset) |
Chris Mason | 065631f | 2008-02-20 12:07:25 -0500 | [diff] [blame] | 1739 | { |
Chris Mason | 065631f | 2008-02-20 12:07:25 -0500 | [diff] [blame] | 1740 | struct btrfs_root *root = BTRFS_I(inode)->root; |
Chris Mason | 065631f | 2008-02-20 12:07:25 -0500 | [diff] [blame] | 1741 | int ret = 0; |
Chris Mason | e015640 | 2008-04-16 11:15:20 -0400 | [diff] [blame] | 1742 | |
Chris Mason | d20f704 | 2008-12-08 16:58:54 -0500 | [diff] [blame] | 1743 | ret = btrfs_csum_one_bio(root, inode, bio, 0, 0); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 1744 | BUG_ON(ret); /* -ENOMEM */ |
Chris Mason | 4a69a41 | 2008-11-06 22:03:00 -0500 | [diff] [blame] | 1745 | return 0; |
| 1746 | } |
Chris Mason | e015640 | 2008-04-16 11:15:20 -0400 | [diff] [blame] | 1747 | |
Chris Mason | 4a69a41 | 2008-11-06 22:03:00 -0500 | [diff] [blame] | 1748 | /* |
| 1749 | * in order to insert checksums into the metadata in large chunks, |
| 1750 | * we wait until bio submission time. All the pages in the bio are |
| 1751 | * checksummed and sums are attached onto the ordered extent record. |
| 1752 | * |
| 1753 | * At IO completion time the cums attached on the ordered extent record |
| 1754 | * are inserted into the btree |
| 1755 | */ |
Christoph Hellwig | b295086 | 2008-12-02 09:54:17 -0500 | [diff] [blame] | 1756 | 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] | 1757 | int mirror_num, unsigned long bio_flags, |
| 1758 | u64 bio_offset) |
Chris Mason | 4a69a41 | 2008-11-06 22:03:00 -0500 | [diff] [blame] | 1759 | { |
| 1760 | struct btrfs_root *root = BTRFS_I(inode)->root; |
Stefan Behrens | 6189192 | 2012-11-05 18:51:52 +0100 | [diff] [blame] | 1761 | int ret; |
| 1762 | |
| 1763 | ret = btrfs_map_bio(root, rw, bio, mirror_num, 1); |
| 1764 | if (ret) |
| 1765 | bio_endio(bio, ret); |
| 1766 | return ret; |
Chris Mason | 44b8bd7 | 2008-04-16 11:14:51 -0400 | [diff] [blame] | 1767 | } |
| 1768 | |
Chris Mason | d352ac6 | 2008-09-29 15:18:18 -0400 | [diff] [blame] | 1769 | /* |
Chris Mason | cad321a | 2008-12-17 14:51:42 -0500 | [diff] [blame] | 1770 | * extent_io.c submission hook. This does the right thing for csum calculation |
| 1771 | * on write, or reading the csums from the tree before a read |
Chris Mason | d352ac6 | 2008-09-29 15:18:18 -0400 | [diff] [blame] | 1772 | */ |
Christoph Hellwig | b295086 | 2008-12-02 09:54:17 -0500 | [diff] [blame] | 1773 | 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] | 1774 | int mirror_num, unsigned long bio_flags, |
| 1775 | u64 bio_offset) |
Chris Mason | 44b8bd7 | 2008-04-16 11:14:51 -0400 | [diff] [blame] | 1776 | { |
| 1777 | struct btrfs_root *root = BTRFS_I(inode)->root; |
| 1778 | int ret = 0; |
Chris Mason | 19b9bdb | 2008-10-30 14:23:13 -0400 | [diff] [blame] | 1779 | int skip_sum; |
Jeff Mahoney | 0417341 | 2011-10-03 23:23:12 -0400 | [diff] [blame] | 1780 | int metadata = 0; |
Josef Bacik | b812ce2 | 2012-11-16 13:56:32 -0500 | [diff] [blame] | 1781 | int async = !atomic_read(&BTRFS_I(inode)->sync_writers); |
Chris Mason | 44b8bd7 | 2008-04-16 11:14:51 -0400 | [diff] [blame] | 1782 | |
Christoph Hellwig | 6cbff00 | 2009-04-17 10:37:41 +0200 | [diff] [blame] | 1783 | skip_sum = BTRFS_I(inode)->flags & BTRFS_INODE_NODATASUM; |
Chris Mason | cad321a | 2008-12-17 14:51:42 -0500 | [diff] [blame] | 1784 | |
Liu Bo | 83eea1f | 2012-07-10 05:28:39 -0600 | [diff] [blame] | 1785 | if (btrfs_is_free_space_inode(inode)) |
Jeff Mahoney | 0417341 | 2011-10-03 23:23:12 -0400 | [diff] [blame] | 1786 | metadata = 2; |
| 1787 | |
Christoph Hellwig | 7b6d91d | 2010-08-07 18:20:39 +0200 | [diff] [blame] | 1788 | if (!(rw & REQ_WRITE)) { |
Josef Bacik | 5fd0204 | 2012-05-02 14:00:54 -0400 | [diff] [blame] | 1789 | ret = btrfs_bio_wq_end_io(root->fs_info, bio, metadata); |
| 1790 | if (ret) |
Stefan Behrens | 6189192 | 2012-11-05 18:51:52 +0100 | [diff] [blame] | 1791 | goto out; |
Josef Bacik | 5fd0204 | 2012-05-02 14:00:54 -0400 | [diff] [blame] | 1792 | |
Chris Mason | d20f704 | 2008-12-08 16:58:54 -0500 | [diff] [blame] | 1793 | if (bio_flags & EXTENT_BIO_COMPRESSED) { |
Stefan Behrens | 6189192 | 2012-11-05 18:51:52 +0100 | [diff] [blame] | 1794 | ret = btrfs_submit_compressed_read(inode, bio, |
| 1795 | mirror_num, |
| 1796 | bio_flags); |
| 1797 | goto out; |
Tsutomu Itoh | c2db107 | 2011-03-01 06:48:31 +0000 | [diff] [blame] | 1798 | } else if (!skip_sum) { |
| 1799 | ret = btrfs_lookup_bio_sums(root, inode, bio, NULL); |
| 1800 | if (ret) |
Stefan Behrens | 6189192 | 2012-11-05 18:51:52 +0100 | [diff] [blame] | 1801 | goto out; |
Tsutomu Itoh | c2db107 | 2011-03-01 06:48:31 +0000 | [diff] [blame] | 1802 | } |
Chris Mason | 4d1b5fb | 2008-08-20 09:44:52 -0400 | [diff] [blame] | 1803 | goto mapit; |
Josef Bacik | b812ce2 | 2012-11-16 13:56:32 -0500 | [diff] [blame] | 1804 | } else if (async && !skip_sum) { |
Yan Zheng | 17d217f | 2008-12-12 10:03:38 -0500 | [diff] [blame] | 1805 | /* csum items have already been cloned */ |
| 1806 | if (root->root_key.objectid == BTRFS_DATA_RELOC_TREE_OBJECTID) |
| 1807 | goto mapit; |
Chris Mason | 19b9bdb | 2008-10-30 14:23:13 -0400 | [diff] [blame] | 1808 | /* we're doing a write, do the async checksumming */ |
Stefan Behrens | 6189192 | 2012-11-05 18:51:52 +0100 | [diff] [blame] | 1809 | ret = btrfs_wq_submit_bio(BTRFS_I(inode)->root->fs_info, |
Chris Mason | 44b8bd7 | 2008-04-16 11:14:51 -0400 | [diff] [blame] | 1810 | inode, rw, bio, mirror_num, |
Chris Mason | eaf25d9 | 2010-05-25 09:48:28 -0400 | [diff] [blame] | 1811 | bio_flags, bio_offset, |
| 1812 | __btrfs_submit_bio_start, |
Chris Mason | 4a69a41 | 2008-11-06 22:03:00 -0500 | [diff] [blame] | 1813 | __btrfs_submit_bio_done); |
Stefan Behrens | 6189192 | 2012-11-05 18:51:52 +0100 | [diff] [blame] | 1814 | goto out; |
Josef Bacik | b812ce2 | 2012-11-16 13:56:32 -0500 | [diff] [blame] | 1815 | } else if (!skip_sum) { |
| 1816 | ret = btrfs_csum_one_bio(root, inode, bio, 0, 0); |
| 1817 | if (ret) |
| 1818 | goto out; |
Chris Mason | 19b9bdb | 2008-10-30 14:23:13 -0400 | [diff] [blame] | 1819 | } |
| 1820 | |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 1821 | mapit: |
Stefan Behrens | 6189192 | 2012-11-05 18:51:52 +0100 | [diff] [blame] | 1822 | ret = btrfs_map_bio(root, rw, bio, mirror_num, 0); |
| 1823 | |
| 1824 | out: |
| 1825 | if (ret < 0) |
| 1826 | bio_endio(bio, ret); |
| 1827 | return ret; |
Chris Mason | 065631f | 2008-02-20 12:07:25 -0500 | [diff] [blame] | 1828 | } |
Chris Mason | 6885f30 | 2008-02-20 16:11:05 -0500 | [diff] [blame] | 1829 | |
Chris Mason | d352ac6 | 2008-09-29 15:18:18 -0400 | [diff] [blame] | 1830 | /* |
| 1831 | * given a list of ordered sums record them in the inode. This happens |
| 1832 | * at IO completion time based on sums calculated at bio submission time. |
| 1833 | */ |
Chris Mason | ba1da2f | 2008-07-17 12:54:15 -0400 | [diff] [blame] | 1834 | static noinline int add_pending_csums(struct btrfs_trans_handle *trans, |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 1835 | struct inode *inode, u64 file_offset, |
| 1836 | struct list_head *list) |
| 1837 | { |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 1838 | struct btrfs_ordered_sum *sum; |
| 1839 | |
Qinghuang Feng | c6e3087 | 2009-01-21 10:59:08 -0500 | [diff] [blame] | 1840 | list_for_each_entry(sum, list, list) { |
Miao Xie | 39847c4 | 2013-03-28 08:08:20 +0000 | [diff] [blame] | 1841 | trans->adding_csums = 1; |
Chris Mason | d20f704 | 2008-12-08 16:58:54 -0500 | [diff] [blame] | 1842 | btrfs_csum_file_blocks(trans, |
| 1843 | BTRFS_I(inode)->root->fs_info->csum_root, sum); |
Miao Xie | 39847c4 | 2013-03-28 08:08:20 +0000 | [diff] [blame] | 1844 | trans->adding_csums = 0; |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 1845 | } |
| 1846 | return 0; |
| 1847 | } |
| 1848 | |
Josef Bacik | 2ac55d4 | 2010-02-03 19:33:23 +0000 | [diff] [blame] | 1849 | int btrfs_set_extent_delalloc(struct inode *inode, u64 start, u64 end, |
| 1850 | struct extent_state **cached_state) |
Chris Mason | ea8c281 | 2008-08-04 23:17:27 -0400 | [diff] [blame] | 1851 | { |
Julia Lawall | 6c1500f | 2012-11-03 20:30:18 +0000 | [diff] [blame] | 1852 | WARN_ON((end & (PAGE_CACHE_SIZE - 1)) == 0); |
Chris Mason | ea8c281 | 2008-08-04 23:17:27 -0400 | [diff] [blame] | 1853 | return set_extent_delalloc(&BTRFS_I(inode)->io_tree, start, end, |
Josef Bacik | 2ac55d4 | 2010-02-03 19:33:23 +0000 | [diff] [blame] | 1854 | cached_state, GFP_NOFS); |
Chris Mason | ea8c281 | 2008-08-04 23:17:27 -0400 | [diff] [blame] | 1855 | } |
| 1856 | |
Chris Mason | d352ac6 | 2008-09-29 15:18:18 -0400 | [diff] [blame] | 1857 | /* see btrfs_writepage_start_hook for details on why this is required */ |
Chris Mason | 247e743 | 2008-07-17 12:53:51 -0400 | [diff] [blame] | 1858 | struct btrfs_writepage_fixup { |
| 1859 | struct page *page; |
| 1860 | struct btrfs_work work; |
| 1861 | }; |
| 1862 | |
Christoph Hellwig | b295086 | 2008-12-02 09:54:17 -0500 | [diff] [blame] | 1863 | static void btrfs_writepage_fixup_worker(struct btrfs_work *work) |
Chris Mason | 247e743 | 2008-07-17 12:53:51 -0400 | [diff] [blame] | 1864 | { |
| 1865 | struct btrfs_writepage_fixup *fixup; |
| 1866 | struct btrfs_ordered_extent *ordered; |
Josef Bacik | 2ac55d4 | 2010-02-03 19:33:23 +0000 | [diff] [blame] | 1867 | struct extent_state *cached_state = NULL; |
Chris Mason | 247e743 | 2008-07-17 12:53:51 -0400 | [diff] [blame] | 1868 | struct page *page; |
| 1869 | struct inode *inode; |
| 1870 | u64 page_start; |
| 1871 | u64 page_end; |
Jeff Mahoney | 87826df | 2012-02-15 16:23:57 +0100 | [diff] [blame] | 1872 | int ret; |
Chris Mason | 247e743 | 2008-07-17 12:53:51 -0400 | [diff] [blame] | 1873 | |
| 1874 | fixup = container_of(work, struct btrfs_writepage_fixup, work); |
| 1875 | page = fixup->page; |
Chris Mason | 4a09675 | 2008-07-21 10:29:44 -0400 | [diff] [blame] | 1876 | again: |
Chris Mason | 247e743 | 2008-07-17 12:53:51 -0400 | [diff] [blame] | 1877 | lock_page(page); |
| 1878 | if (!page->mapping || !PageDirty(page) || !PageChecked(page)) { |
| 1879 | ClearPageChecked(page); |
| 1880 | goto out_page; |
| 1881 | } |
| 1882 | |
| 1883 | inode = page->mapping->host; |
| 1884 | page_start = page_offset(page); |
| 1885 | page_end = page_offset(page) + PAGE_CACHE_SIZE - 1; |
| 1886 | |
Josef Bacik | 2ac55d4 | 2010-02-03 19:33:23 +0000 | [diff] [blame] | 1887 | 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] | 1888 | &cached_state); |
Chris Mason | 4a09675 | 2008-07-21 10:29:44 -0400 | [diff] [blame] | 1889 | |
| 1890 | /* already ordered? We're done */ |
Chris Mason | 8b62b72 | 2009-09-02 16:53:46 -0400 | [diff] [blame] | 1891 | if (PagePrivate2(page)) |
Chris Mason | 247e743 | 2008-07-17 12:53:51 -0400 | [diff] [blame] | 1892 | goto out; |
Chris Mason | 4a09675 | 2008-07-21 10:29:44 -0400 | [diff] [blame] | 1893 | |
| 1894 | ordered = btrfs_lookup_ordered_extent(inode, page_start); |
| 1895 | if (ordered) { |
Josef Bacik | 2ac55d4 | 2010-02-03 19:33:23 +0000 | [diff] [blame] | 1896 | unlock_extent_cached(&BTRFS_I(inode)->io_tree, page_start, |
| 1897 | page_end, &cached_state, GFP_NOFS); |
Chris Mason | 4a09675 | 2008-07-21 10:29:44 -0400 | [diff] [blame] | 1898 | unlock_page(page); |
| 1899 | btrfs_start_ordered_extent(inode, ordered, 1); |
Jeff Mahoney | 87826df | 2012-02-15 16:23:57 +0100 | [diff] [blame] | 1900 | btrfs_put_ordered_extent(ordered); |
Chris Mason | 4a09675 | 2008-07-21 10:29:44 -0400 | [diff] [blame] | 1901 | goto again; |
| 1902 | } |
Chris Mason | 247e743 | 2008-07-17 12:53:51 -0400 | [diff] [blame] | 1903 | |
Jeff Mahoney | 87826df | 2012-02-15 16:23:57 +0100 | [diff] [blame] | 1904 | ret = btrfs_delalloc_reserve_space(inode, PAGE_CACHE_SIZE); |
| 1905 | if (ret) { |
| 1906 | mapping_set_error(page->mapping, ret); |
| 1907 | end_extent_writepage(page, ret, page_start, page_end); |
| 1908 | ClearPageChecked(page); |
| 1909 | goto out; |
| 1910 | } |
| 1911 | |
Josef Bacik | 2ac55d4 | 2010-02-03 19:33:23 +0000 | [diff] [blame] | 1912 | btrfs_set_extent_delalloc(inode, page_start, page_end, &cached_state); |
Chris Mason | 247e743 | 2008-07-17 12:53:51 -0400 | [diff] [blame] | 1913 | ClearPageChecked(page); |
Jeff Mahoney | 87826df | 2012-02-15 16:23:57 +0100 | [diff] [blame] | 1914 | set_page_dirty(page); |
Chris Mason | 247e743 | 2008-07-17 12:53:51 -0400 | [diff] [blame] | 1915 | out: |
Josef Bacik | 2ac55d4 | 2010-02-03 19:33:23 +0000 | [diff] [blame] | 1916 | unlock_extent_cached(&BTRFS_I(inode)->io_tree, page_start, page_end, |
| 1917 | &cached_state, GFP_NOFS); |
Chris Mason | 247e743 | 2008-07-17 12:53:51 -0400 | [diff] [blame] | 1918 | out_page: |
| 1919 | unlock_page(page); |
| 1920 | page_cache_release(page); |
Miao Xie | b897abe | 2011-01-26 16:19:22 +0800 | [diff] [blame] | 1921 | kfree(fixup); |
Chris Mason | 247e743 | 2008-07-17 12:53:51 -0400 | [diff] [blame] | 1922 | } |
| 1923 | |
| 1924 | /* |
| 1925 | * There are a few paths in the higher layers of the kernel that directly |
| 1926 | * set the page dirty bit without asking the filesystem if it is a |
| 1927 | * good idea. This causes problems because we want to make sure COW |
| 1928 | * properly happens and the data=ordered rules are followed. |
| 1929 | * |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 1930 | * 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] | 1931 | * hasn't been properly setup for IO. We kick off an async process |
| 1932 | * to fix it up. The async helper will wait for ordered extents, set |
| 1933 | * the delalloc bit and make it safe to write the page. |
| 1934 | */ |
Christoph Hellwig | b295086 | 2008-12-02 09:54:17 -0500 | [diff] [blame] | 1935 | 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] | 1936 | { |
| 1937 | struct inode *inode = page->mapping->host; |
| 1938 | struct btrfs_writepage_fixup *fixup; |
| 1939 | struct btrfs_root *root = BTRFS_I(inode)->root; |
Chris Mason | 247e743 | 2008-07-17 12:53:51 -0400 | [diff] [blame] | 1940 | |
Chris Mason | 8b62b72 | 2009-09-02 16:53:46 -0400 | [diff] [blame] | 1941 | /* this page is properly in the ordered list */ |
| 1942 | if (TestClearPagePrivate2(page)) |
Chris Mason | 247e743 | 2008-07-17 12:53:51 -0400 | [diff] [blame] | 1943 | return 0; |
| 1944 | |
| 1945 | if (PageChecked(page)) |
| 1946 | return -EAGAIN; |
| 1947 | |
| 1948 | fixup = kzalloc(sizeof(*fixup), GFP_NOFS); |
| 1949 | if (!fixup) |
| 1950 | return -EAGAIN; |
Chris Mason | f421950 | 2008-07-22 11:18:09 -0400 | [diff] [blame] | 1951 | |
Chris Mason | 247e743 | 2008-07-17 12:53:51 -0400 | [diff] [blame] | 1952 | SetPageChecked(page); |
| 1953 | page_cache_get(page); |
Liu Bo | 9e0af23 | 2014-08-15 23:36:53 +0800 | [diff] [blame] | 1954 | btrfs_init_work(&fixup->work, btrfs_fixup_helper, |
| 1955 | btrfs_writepage_fixup_worker, NULL, NULL); |
Chris Mason | 247e743 | 2008-07-17 12:53:51 -0400 | [diff] [blame] | 1956 | fixup->page = page; |
Qu Wenruo | dc6e320 | 2014-02-28 10:46:14 +0800 | [diff] [blame] | 1957 | btrfs_queue_work(root->fs_info->fixup_workers, &fixup->work); |
Jeff Mahoney | 87826df | 2012-02-15 16:23:57 +0100 | [diff] [blame] | 1958 | return -EBUSY; |
Chris Mason | 247e743 | 2008-07-17 12:53:51 -0400 | [diff] [blame] | 1959 | } |
| 1960 | |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 1961 | static int insert_reserved_file_extent(struct btrfs_trans_handle *trans, |
| 1962 | struct inode *inode, u64 file_pos, |
| 1963 | u64 disk_bytenr, u64 disk_num_bytes, |
| 1964 | u64 num_bytes, u64 ram_bytes, |
| 1965 | u8 compression, u8 encryption, |
| 1966 | u16 other_encoding, int extent_type) |
| 1967 | { |
| 1968 | struct btrfs_root *root = BTRFS_I(inode)->root; |
| 1969 | struct btrfs_file_extent_item *fi; |
| 1970 | struct btrfs_path *path; |
| 1971 | struct extent_buffer *leaf; |
| 1972 | struct btrfs_key ins; |
Filipe David Borba Manana | 1acae57 | 2014-01-07 11:42:27 +0000 | [diff] [blame] | 1973 | int extent_inserted = 0; |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 1974 | int ret; |
| 1975 | |
| 1976 | path = btrfs_alloc_path(); |
Mark Fasheh | d8926bb | 2011-07-13 10:38:47 -0700 | [diff] [blame] | 1977 | if (!path) |
| 1978 | return -ENOMEM; |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 1979 | |
Chris Mason | a1ed835 | 2009-09-11 12:27:37 -0400 | [diff] [blame] | 1980 | /* |
| 1981 | * we may be replacing one extent in the tree with another. |
| 1982 | * The new extent is pinned in the extent map, and we don't want |
| 1983 | * to drop it from the cache until it is completely in the btree. |
| 1984 | * |
| 1985 | * So, tell btrfs_drop_extents to leave this extent in the cache. |
| 1986 | * the caller is expected to unpin it and allow it to be merged |
| 1987 | * with the others. |
| 1988 | */ |
Filipe David Borba Manana | 1acae57 | 2014-01-07 11:42:27 +0000 | [diff] [blame] | 1989 | ret = __btrfs_drop_extents(trans, root, inode, path, file_pos, |
| 1990 | file_pos + num_bytes, NULL, 0, |
| 1991 | 1, sizeof(*fi), &extent_inserted); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 1992 | if (ret) |
| 1993 | goto out; |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 1994 | |
Filipe David Borba Manana | 1acae57 | 2014-01-07 11:42:27 +0000 | [diff] [blame] | 1995 | if (!extent_inserted) { |
| 1996 | ins.objectid = btrfs_ino(inode); |
| 1997 | ins.offset = file_pos; |
| 1998 | ins.type = BTRFS_EXTENT_DATA_KEY; |
| 1999 | |
| 2000 | path->leave_spinning = 1; |
| 2001 | ret = btrfs_insert_empty_item(trans, root, path, &ins, |
| 2002 | sizeof(*fi)); |
| 2003 | if (ret) |
| 2004 | goto out; |
| 2005 | } |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 2006 | leaf = path->nodes[0]; |
| 2007 | fi = btrfs_item_ptr(leaf, path->slots[0], |
| 2008 | struct btrfs_file_extent_item); |
| 2009 | btrfs_set_file_extent_generation(leaf, fi, trans->transid); |
| 2010 | btrfs_set_file_extent_type(leaf, fi, extent_type); |
| 2011 | btrfs_set_file_extent_disk_bytenr(leaf, fi, disk_bytenr); |
| 2012 | btrfs_set_file_extent_disk_num_bytes(leaf, fi, disk_num_bytes); |
| 2013 | btrfs_set_file_extent_offset(leaf, fi, 0); |
| 2014 | btrfs_set_file_extent_num_bytes(leaf, fi, num_bytes); |
| 2015 | btrfs_set_file_extent_ram_bytes(leaf, fi, ram_bytes); |
| 2016 | btrfs_set_file_extent_compression(leaf, fi, compression); |
| 2017 | btrfs_set_file_extent_encryption(leaf, fi, encryption); |
| 2018 | btrfs_set_file_extent_other_encoding(leaf, fi, other_encoding); |
Chris Mason | b947343 | 2009-03-13 11:00:37 -0400 | [diff] [blame] | 2019 | |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 2020 | btrfs_mark_buffer_dirty(leaf); |
Josef Bacik | ce19533 | 2012-09-25 15:26:16 -0400 | [diff] [blame] | 2021 | btrfs_release_path(path); |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 2022 | |
| 2023 | inode_add_bytes(inode, num_bytes); |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 2024 | |
| 2025 | ins.objectid = disk_bytenr; |
| 2026 | ins.offset = disk_num_bytes; |
| 2027 | ins.type = BTRFS_EXTENT_ITEM_KEY; |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 2028 | ret = btrfs_alloc_reserved_file_extent(trans, root, |
| 2029 | root->root_key.objectid, |
Li Zefan | 33345d01 | 2011-04-20 10:31:50 +0800 | [diff] [blame] | 2030 | btrfs_ino(inode), file_pos, &ins); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 2031 | out: |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 2032 | btrfs_free_path(path); |
Chris Mason | b947343 | 2009-03-13 11:00:37 -0400 | [diff] [blame] | 2033 | |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 2034 | return ret; |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 2035 | } |
| 2036 | |
Liu Bo | 38c227d | 2013-01-29 03:18:40 +0000 | [diff] [blame] | 2037 | /* snapshot-aware defrag */ |
| 2038 | struct sa_defrag_extent_backref { |
| 2039 | struct rb_node node; |
| 2040 | struct old_sa_defrag_extent *old; |
| 2041 | u64 root_id; |
| 2042 | u64 inum; |
| 2043 | u64 file_pos; |
| 2044 | u64 extent_offset; |
| 2045 | u64 num_bytes; |
| 2046 | u64 generation; |
| 2047 | }; |
| 2048 | |
| 2049 | struct old_sa_defrag_extent { |
| 2050 | struct list_head list; |
| 2051 | struct new_sa_defrag_extent *new; |
| 2052 | |
| 2053 | u64 extent_offset; |
| 2054 | u64 bytenr; |
| 2055 | u64 offset; |
| 2056 | u64 len; |
| 2057 | int count; |
| 2058 | }; |
| 2059 | |
| 2060 | struct new_sa_defrag_extent { |
| 2061 | struct rb_root root; |
| 2062 | struct list_head head; |
| 2063 | struct btrfs_path *path; |
| 2064 | struct inode *inode; |
| 2065 | u64 file_pos; |
| 2066 | u64 len; |
| 2067 | u64 bytenr; |
| 2068 | u64 disk_len; |
| 2069 | u8 compress_type; |
| 2070 | }; |
| 2071 | |
| 2072 | static int backref_comp(struct sa_defrag_extent_backref *b1, |
| 2073 | struct sa_defrag_extent_backref *b2) |
| 2074 | { |
| 2075 | if (b1->root_id < b2->root_id) |
| 2076 | return -1; |
| 2077 | else if (b1->root_id > b2->root_id) |
| 2078 | return 1; |
| 2079 | |
| 2080 | if (b1->inum < b2->inum) |
| 2081 | return -1; |
| 2082 | else if (b1->inum > b2->inum) |
| 2083 | return 1; |
| 2084 | |
| 2085 | if (b1->file_pos < b2->file_pos) |
| 2086 | return -1; |
| 2087 | else if (b1->file_pos > b2->file_pos) |
| 2088 | return 1; |
| 2089 | |
| 2090 | /* |
| 2091 | * [------------------------------] ===> (a range of space) |
| 2092 | * |<--->| |<---->| =============> (fs/file tree A) |
| 2093 | * |<---------------------------->| ===> (fs/file tree B) |
| 2094 | * |
| 2095 | * A range of space can refer to two file extents in one tree while |
| 2096 | * refer to only one file extent in another tree. |
| 2097 | * |
| 2098 | * So we may process a disk offset more than one time(two extents in A) |
| 2099 | * and locate at the same extent(one extent in B), then insert two same |
| 2100 | * backrefs(both refer to the extent in B). |
| 2101 | */ |
| 2102 | return 0; |
| 2103 | } |
| 2104 | |
| 2105 | static void backref_insert(struct rb_root *root, |
| 2106 | struct sa_defrag_extent_backref *backref) |
| 2107 | { |
| 2108 | struct rb_node **p = &root->rb_node; |
| 2109 | struct rb_node *parent = NULL; |
| 2110 | struct sa_defrag_extent_backref *entry; |
| 2111 | int ret; |
| 2112 | |
| 2113 | while (*p) { |
| 2114 | parent = *p; |
| 2115 | entry = rb_entry(parent, struct sa_defrag_extent_backref, node); |
| 2116 | |
| 2117 | ret = backref_comp(backref, entry); |
| 2118 | if (ret < 0) |
| 2119 | p = &(*p)->rb_left; |
| 2120 | else |
| 2121 | p = &(*p)->rb_right; |
| 2122 | } |
| 2123 | |
| 2124 | rb_link_node(&backref->node, parent, p); |
| 2125 | rb_insert_color(&backref->node, root); |
| 2126 | } |
| 2127 | |
| 2128 | /* |
| 2129 | * Note the backref might has changed, and in this case we just return 0. |
| 2130 | */ |
| 2131 | static noinline int record_one_backref(u64 inum, u64 offset, u64 root_id, |
| 2132 | void *ctx) |
| 2133 | { |
| 2134 | struct btrfs_file_extent_item *extent; |
| 2135 | struct btrfs_fs_info *fs_info; |
| 2136 | struct old_sa_defrag_extent *old = ctx; |
| 2137 | struct new_sa_defrag_extent *new = old->new; |
| 2138 | struct btrfs_path *path = new->path; |
| 2139 | struct btrfs_key key; |
| 2140 | struct btrfs_root *root; |
| 2141 | struct sa_defrag_extent_backref *backref; |
| 2142 | struct extent_buffer *leaf; |
| 2143 | struct inode *inode = new->inode; |
| 2144 | int slot; |
| 2145 | int ret; |
| 2146 | u64 extent_offset; |
| 2147 | u64 num_bytes; |
| 2148 | |
| 2149 | if (BTRFS_I(inode)->root->root_key.objectid == root_id && |
| 2150 | inum == btrfs_ino(inode)) |
| 2151 | return 0; |
| 2152 | |
| 2153 | key.objectid = root_id; |
| 2154 | key.type = BTRFS_ROOT_ITEM_KEY; |
| 2155 | key.offset = (u64)-1; |
| 2156 | |
| 2157 | fs_info = BTRFS_I(inode)->root->fs_info; |
| 2158 | root = btrfs_read_fs_root_no_name(fs_info, &key); |
| 2159 | if (IS_ERR(root)) { |
| 2160 | if (PTR_ERR(root) == -ENOENT) |
| 2161 | return 0; |
| 2162 | WARN_ON(1); |
| 2163 | pr_debug("inum=%llu, offset=%llu, root_id=%llu\n", |
| 2164 | inum, offset, root_id); |
| 2165 | return PTR_ERR(root); |
| 2166 | } |
| 2167 | |
| 2168 | key.objectid = inum; |
| 2169 | key.type = BTRFS_EXTENT_DATA_KEY; |
| 2170 | if (offset > (u64)-1 << 32) |
| 2171 | key.offset = 0; |
| 2172 | else |
| 2173 | key.offset = offset; |
| 2174 | |
| 2175 | ret = btrfs_search_slot(NULL, root, &key, path, 0, 0); |
Dulshani Gunawardhana | fae7f21 | 2013-10-31 10:30:08 +0530 | [diff] [blame] | 2176 | if (WARN_ON(ret < 0)) |
Liu Bo | 38c227d | 2013-01-29 03:18:40 +0000 | [diff] [blame] | 2177 | return ret; |
Josef Bacik | 50f1319 | 2013-07-22 12:50:37 -0400 | [diff] [blame] | 2178 | ret = 0; |
Liu Bo | 38c227d | 2013-01-29 03:18:40 +0000 | [diff] [blame] | 2179 | |
| 2180 | while (1) { |
| 2181 | cond_resched(); |
| 2182 | |
| 2183 | leaf = path->nodes[0]; |
| 2184 | slot = path->slots[0]; |
| 2185 | |
| 2186 | if (slot >= btrfs_header_nritems(leaf)) { |
| 2187 | ret = btrfs_next_leaf(root, path); |
| 2188 | if (ret < 0) { |
| 2189 | goto out; |
| 2190 | } else if (ret > 0) { |
| 2191 | ret = 0; |
| 2192 | goto out; |
| 2193 | } |
| 2194 | continue; |
| 2195 | } |
| 2196 | |
| 2197 | path->slots[0]++; |
| 2198 | |
| 2199 | btrfs_item_key_to_cpu(leaf, &key, slot); |
| 2200 | |
| 2201 | if (key.objectid > inum) |
| 2202 | goto out; |
| 2203 | |
| 2204 | if (key.objectid < inum || key.type != BTRFS_EXTENT_DATA_KEY) |
| 2205 | continue; |
| 2206 | |
| 2207 | extent = btrfs_item_ptr(leaf, slot, |
| 2208 | struct btrfs_file_extent_item); |
| 2209 | |
| 2210 | if (btrfs_file_extent_disk_bytenr(leaf, extent) != old->bytenr) |
| 2211 | continue; |
| 2212 | |
Liu Bo | e68afa4 | 2013-07-01 22:13:26 +0800 | [diff] [blame] | 2213 | /* |
| 2214 | * 'offset' refers to the exact key.offset, |
| 2215 | * NOT the 'offset' field in btrfs_extent_data_ref, ie. |
| 2216 | * (key.offset - extent_offset). |
| 2217 | */ |
| 2218 | if (key.offset != offset) |
Liu Bo | 38c227d | 2013-01-29 03:18:40 +0000 | [diff] [blame] | 2219 | continue; |
| 2220 | |
Liu Bo | e68afa4 | 2013-07-01 22:13:26 +0800 | [diff] [blame] | 2221 | extent_offset = btrfs_file_extent_offset(leaf, extent); |
Liu Bo | 38c227d | 2013-01-29 03:18:40 +0000 | [diff] [blame] | 2222 | num_bytes = btrfs_file_extent_num_bytes(leaf, extent); |
Liu Bo | e68afa4 | 2013-07-01 22:13:26 +0800 | [diff] [blame] | 2223 | |
Liu Bo | 38c227d | 2013-01-29 03:18:40 +0000 | [diff] [blame] | 2224 | if (extent_offset >= old->extent_offset + old->offset + |
| 2225 | old->len || extent_offset + num_bytes <= |
| 2226 | old->extent_offset + old->offset) |
| 2227 | continue; |
Liu Bo | 38c227d | 2013-01-29 03:18:40 +0000 | [diff] [blame] | 2228 | break; |
| 2229 | } |
| 2230 | |
| 2231 | backref = kmalloc(sizeof(*backref), GFP_NOFS); |
| 2232 | if (!backref) { |
| 2233 | ret = -ENOENT; |
| 2234 | goto out; |
| 2235 | } |
| 2236 | |
| 2237 | backref->root_id = root_id; |
| 2238 | backref->inum = inum; |
Liu Bo | e68afa4 | 2013-07-01 22:13:26 +0800 | [diff] [blame] | 2239 | backref->file_pos = offset; |
Liu Bo | 38c227d | 2013-01-29 03:18:40 +0000 | [diff] [blame] | 2240 | backref->num_bytes = num_bytes; |
| 2241 | backref->extent_offset = extent_offset; |
| 2242 | backref->generation = btrfs_file_extent_generation(leaf, extent); |
| 2243 | backref->old = old; |
| 2244 | backref_insert(&new->root, backref); |
| 2245 | old->count++; |
| 2246 | out: |
| 2247 | btrfs_release_path(path); |
| 2248 | WARN_ON(ret); |
| 2249 | return ret; |
| 2250 | } |
| 2251 | |
| 2252 | static noinline bool record_extent_backrefs(struct btrfs_path *path, |
| 2253 | struct new_sa_defrag_extent *new) |
| 2254 | { |
| 2255 | struct btrfs_fs_info *fs_info = BTRFS_I(new->inode)->root->fs_info; |
| 2256 | struct old_sa_defrag_extent *old, *tmp; |
| 2257 | int ret; |
| 2258 | |
| 2259 | new->path = path; |
| 2260 | |
| 2261 | list_for_each_entry_safe(old, tmp, &new->head, list) { |
Liu Bo | e68afa4 | 2013-07-01 22:13:26 +0800 | [diff] [blame] | 2262 | ret = iterate_inodes_from_logical(old->bytenr + |
| 2263 | old->extent_offset, fs_info, |
Liu Bo | 38c227d | 2013-01-29 03:18:40 +0000 | [diff] [blame] | 2264 | path, record_one_backref, |
| 2265 | old); |
Josef Bacik | 4724b10 | 2013-11-05 11:11:40 -0500 | [diff] [blame] | 2266 | if (ret < 0 && ret != -ENOENT) |
| 2267 | return false; |
Liu Bo | 38c227d | 2013-01-29 03:18:40 +0000 | [diff] [blame] | 2268 | |
| 2269 | /* no backref to be processed for this extent */ |
| 2270 | if (!old->count) { |
| 2271 | list_del(&old->list); |
| 2272 | kfree(old); |
| 2273 | } |
| 2274 | } |
| 2275 | |
| 2276 | if (list_empty(&new->head)) |
| 2277 | return false; |
| 2278 | |
| 2279 | return true; |
| 2280 | } |
| 2281 | |
| 2282 | static int relink_is_mergable(struct extent_buffer *leaf, |
| 2283 | struct btrfs_file_extent_item *fi, |
Liu Bo | 116e002 | 2013-08-02 16:30:40 +0800 | [diff] [blame] | 2284 | struct new_sa_defrag_extent *new) |
Liu Bo | 38c227d | 2013-01-29 03:18:40 +0000 | [diff] [blame] | 2285 | { |
Liu Bo | 116e002 | 2013-08-02 16:30:40 +0800 | [diff] [blame] | 2286 | if (btrfs_file_extent_disk_bytenr(leaf, fi) != new->bytenr) |
Liu Bo | 38c227d | 2013-01-29 03:18:40 +0000 | [diff] [blame] | 2287 | return 0; |
| 2288 | |
| 2289 | if (btrfs_file_extent_type(leaf, fi) != BTRFS_FILE_EXTENT_REG) |
| 2290 | return 0; |
| 2291 | |
Liu Bo | 116e002 | 2013-08-02 16:30:40 +0800 | [diff] [blame] | 2292 | if (btrfs_file_extent_compression(leaf, fi) != new->compress_type) |
| 2293 | return 0; |
| 2294 | |
| 2295 | if (btrfs_file_extent_encryption(leaf, fi) || |
Liu Bo | 38c227d | 2013-01-29 03:18:40 +0000 | [diff] [blame] | 2296 | btrfs_file_extent_other_encoding(leaf, fi)) |
| 2297 | return 0; |
| 2298 | |
| 2299 | return 1; |
| 2300 | } |
| 2301 | |
| 2302 | /* |
| 2303 | * Note the backref might has changed, and in this case we just return 0. |
| 2304 | */ |
| 2305 | static noinline int relink_extent_backref(struct btrfs_path *path, |
| 2306 | struct sa_defrag_extent_backref *prev, |
| 2307 | struct sa_defrag_extent_backref *backref) |
| 2308 | { |
| 2309 | struct btrfs_file_extent_item *extent; |
| 2310 | struct btrfs_file_extent_item *item; |
| 2311 | struct btrfs_ordered_extent *ordered; |
| 2312 | struct btrfs_trans_handle *trans; |
| 2313 | struct btrfs_fs_info *fs_info; |
| 2314 | struct btrfs_root *root; |
| 2315 | struct btrfs_key key; |
| 2316 | struct extent_buffer *leaf; |
| 2317 | struct old_sa_defrag_extent *old = backref->old; |
| 2318 | struct new_sa_defrag_extent *new = old->new; |
| 2319 | struct inode *src_inode = new->inode; |
| 2320 | struct inode *inode; |
| 2321 | struct extent_state *cached = NULL; |
| 2322 | int ret = 0; |
| 2323 | u64 start; |
| 2324 | u64 len; |
| 2325 | u64 lock_start; |
| 2326 | u64 lock_end; |
| 2327 | bool merge = false; |
| 2328 | int index; |
| 2329 | |
| 2330 | if (prev && prev->root_id == backref->root_id && |
| 2331 | prev->inum == backref->inum && |
| 2332 | prev->file_pos + prev->num_bytes == backref->file_pos) |
| 2333 | merge = true; |
| 2334 | |
| 2335 | /* step 1: get root */ |
| 2336 | key.objectid = backref->root_id; |
| 2337 | key.type = BTRFS_ROOT_ITEM_KEY; |
| 2338 | key.offset = (u64)-1; |
| 2339 | |
| 2340 | fs_info = BTRFS_I(src_inode)->root->fs_info; |
| 2341 | index = srcu_read_lock(&fs_info->subvol_srcu); |
| 2342 | |
| 2343 | root = btrfs_read_fs_root_no_name(fs_info, &key); |
| 2344 | if (IS_ERR(root)) { |
| 2345 | srcu_read_unlock(&fs_info->subvol_srcu, index); |
| 2346 | if (PTR_ERR(root) == -ENOENT) |
| 2347 | return 0; |
| 2348 | return PTR_ERR(root); |
| 2349 | } |
Liu Bo | 38c227d | 2013-01-29 03:18:40 +0000 | [diff] [blame] | 2350 | |
Wang Shilong | bcbba5e | 2014-02-08 23:46:35 +0800 | [diff] [blame] | 2351 | if (btrfs_root_readonly(root)) { |
| 2352 | srcu_read_unlock(&fs_info->subvol_srcu, index); |
| 2353 | return 0; |
| 2354 | } |
| 2355 | |
Liu Bo | 38c227d | 2013-01-29 03:18:40 +0000 | [diff] [blame] | 2356 | /* step 2: get inode */ |
| 2357 | key.objectid = backref->inum; |
| 2358 | key.type = BTRFS_INODE_ITEM_KEY; |
| 2359 | key.offset = 0; |
| 2360 | |
| 2361 | inode = btrfs_iget(fs_info->sb, &key, root, NULL); |
| 2362 | if (IS_ERR(inode)) { |
| 2363 | srcu_read_unlock(&fs_info->subvol_srcu, index); |
| 2364 | return 0; |
| 2365 | } |
| 2366 | |
| 2367 | srcu_read_unlock(&fs_info->subvol_srcu, index); |
| 2368 | |
| 2369 | /* step 3: relink backref */ |
| 2370 | lock_start = backref->file_pos; |
| 2371 | lock_end = backref->file_pos + backref->num_bytes - 1; |
| 2372 | lock_extent_bits(&BTRFS_I(inode)->io_tree, lock_start, lock_end, |
| 2373 | 0, &cached); |
| 2374 | |
| 2375 | ordered = btrfs_lookup_first_ordered_extent(inode, lock_end); |
| 2376 | if (ordered) { |
| 2377 | btrfs_put_ordered_extent(ordered); |
| 2378 | goto out_unlock; |
| 2379 | } |
| 2380 | |
| 2381 | trans = btrfs_join_transaction(root); |
| 2382 | if (IS_ERR(trans)) { |
| 2383 | ret = PTR_ERR(trans); |
| 2384 | goto out_unlock; |
| 2385 | } |
| 2386 | |
| 2387 | key.objectid = backref->inum; |
| 2388 | key.type = BTRFS_EXTENT_DATA_KEY; |
| 2389 | key.offset = backref->file_pos; |
| 2390 | |
| 2391 | ret = btrfs_search_slot(NULL, root, &key, path, 0, 0); |
| 2392 | if (ret < 0) { |
| 2393 | goto out_free_path; |
| 2394 | } else if (ret > 0) { |
| 2395 | ret = 0; |
| 2396 | goto out_free_path; |
| 2397 | } |
| 2398 | |
| 2399 | extent = btrfs_item_ptr(path->nodes[0], path->slots[0], |
| 2400 | struct btrfs_file_extent_item); |
| 2401 | |
| 2402 | if (btrfs_file_extent_generation(path->nodes[0], extent) != |
| 2403 | backref->generation) |
| 2404 | goto out_free_path; |
| 2405 | |
| 2406 | btrfs_release_path(path); |
| 2407 | |
| 2408 | start = backref->file_pos; |
| 2409 | if (backref->extent_offset < old->extent_offset + old->offset) |
| 2410 | start += old->extent_offset + old->offset - |
| 2411 | backref->extent_offset; |
| 2412 | |
| 2413 | len = min(backref->extent_offset + backref->num_bytes, |
| 2414 | old->extent_offset + old->offset + old->len); |
| 2415 | len -= max(backref->extent_offset, old->extent_offset + old->offset); |
| 2416 | |
| 2417 | ret = btrfs_drop_extents(trans, root, inode, start, |
| 2418 | start + len, 1); |
| 2419 | if (ret) |
| 2420 | goto out_free_path; |
| 2421 | again: |
| 2422 | key.objectid = btrfs_ino(inode); |
| 2423 | key.type = BTRFS_EXTENT_DATA_KEY; |
| 2424 | key.offset = start; |
| 2425 | |
Liu Bo | a09a0a7 | 2013-03-11 09:20:58 +0000 | [diff] [blame] | 2426 | path->leave_spinning = 1; |
Liu Bo | 38c227d | 2013-01-29 03:18:40 +0000 | [diff] [blame] | 2427 | if (merge) { |
| 2428 | struct btrfs_file_extent_item *fi; |
| 2429 | u64 extent_len; |
| 2430 | struct btrfs_key found_key; |
| 2431 | |
Gui Hecheng | 3c9665d | 2014-01-23 13:41:09 +0800 | [diff] [blame] | 2432 | ret = btrfs_search_slot(trans, root, &key, path, 0, 1); |
Liu Bo | 38c227d | 2013-01-29 03:18:40 +0000 | [diff] [blame] | 2433 | if (ret < 0) |
| 2434 | goto out_free_path; |
| 2435 | |
| 2436 | path->slots[0]--; |
| 2437 | leaf = path->nodes[0]; |
| 2438 | btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]); |
| 2439 | |
| 2440 | fi = btrfs_item_ptr(leaf, path->slots[0], |
| 2441 | struct btrfs_file_extent_item); |
| 2442 | extent_len = btrfs_file_extent_num_bytes(leaf, fi); |
| 2443 | |
Liu Bo | 116e002 | 2013-08-02 16:30:40 +0800 | [diff] [blame] | 2444 | if (extent_len + found_key.offset == start && |
| 2445 | relink_is_mergable(leaf, fi, new)) { |
Liu Bo | 38c227d | 2013-01-29 03:18:40 +0000 | [diff] [blame] | 2446 | btrfs_set_file_extent_num_bytes(leaf, fi, |
| 2447 | extent_len + len); |
| 2448 | btrfs_mark_buffer_dirty(leaf); |
| 2449 | inode_add_bytes(inode, len); |
| 2450 | |
| 2451 | ret = 1; |
| 2452 | goto out_free_path; |
| 2453 | } else { |
| 2454 | merge = false; |
| 2455 | btrfs_release_path(path); |
| 2456 | goto again; |
| 2457 | } |
| 2458 | } |
| 2459 | |
| 2460 | ret = btrfs_insert_empty_item(trans, root, path, &key, |
| 2461 | sizeof(*extent)); |
| 2462 | if (ret) { |
| 2463 | btrfs_abort_transaction(trans, root, ret); |
| 2464 | goto out_free_path; |
| 2465 | } |
| 2466 | |
| 2467 | leaf = path->nodes[0]; |
| 2468 | item = btrfs_item_ptr(leaf, path->slots[0], |
| 2469 | struct btrfs_file_extent_item); |
| 2470 | btrfs_set_file_extent_disk_bytenr(leaf, item, new->bytenr); |
| 2471 | btrfs_set_file_extent_disk_num_bytes(leaf, item, new->disk_len); |
| 2472 | btrfs_set_file_extent_offset(leaf, item, start - new->file_pos); |
| 2473 | btrfs_set_file_extent_num_bytes(leaf, item, len); |
| 2474 | btrfs_set_file_extent_ram_bytes(leaf, item, new->len); |
| 2475 | btrfs_set_file_extent_generation(leaf, item, trans->transid); |
| 2476 | btrfs_set_file_extent_type(leaf, item, BTRFS_FILE_EXTENT_REG); |
| 2477 | btrfs_set_file_extent_compression(leaf, item, new->compress_type); |
| 2478 | btrfs_set_file_extent_encryption(leaf, item, 0); |
| 2479 | btrfs_set_file_extent_other_encoding(leaf, item, 0); |
| 2480 | |
| 2481 | btrfs_mark_buffer_dirty(leaf); |
| 2482 | inode_add_bytes(inode, len); |
Liu Bo | a09a0a7 | 2013-03-11 09:20:58 +0000 | [diff] [blame] | 2483 | btrfs_release_path(path); |
Liu Bo | 38c227d | 2013-01-29 03:18:40 +0000 | [diff] [blame] | 2484 | |
| 2485 | ret = btrfs_inc_extent_ref(trans, root, new->bytenr, |
| 2486 | new->disk_len, 0, |
| 2487 | backref->root_id, backref->inum, |
| 2488 | new->file_pos, 0); /* start - extent_offset */ |
| 2489 | if (ret) { |
| 2490 | btrfs_abort_transaction(trans, root, ret); |
| 2491 | goto out_free_path; |
| 2492 | } |
| 2493 | |
| 2494 | ret = 1; |
| 2495 | out_free_path: |
| 2496 | btrfs_release_path(path); |
Liu Bo | a09a0a7 | 2013-03-11 09:20:58 +0000 | [diff] [blame] | 2497 | path->leave_spinning = 0; |
Liu Bo | 38c227d | 2013-01-29 03:18:40 +0000 | [diff] [blame] | 2498 | btrfs_end_transaction(trans, root); |
| 2499 | out_unlock: |
| 2500 | unlock_extent_cached(&BTRFS_I(inode)->io_tree, lock_start, lock_end, |
| 2501 | &cached, GFP_NOFS); |
| 2502 | iput(inode); |
| 2503 | return ret; |
| 2504 | } |
| 2505 | |
Liu Bo | 6f51956 | 2013-10-29 10:45:05 +0800 | [diff] [blame] | 2506 | static void free_sa_defrag_extent(struct new_sa_defrag_extent *new) |
| 2507 | { |
| 2508 | struct old_sa_defrag_extent *old, *tmp; |
| 2509 | |
| 2510 | if (!new) |
| 2511 | return; |
| 2512 | |
| 2513 | list_for_each_entry_safe(old, tmp, &new->head, list) { |
| 2514 | list_del(&old->list); |
| 2515 | kfree(old); |
| 2516 | } |
| 2517 | kfree(new); |
| 2518 | } |
| 2519 | |
Liu Bo | 38c227d | 2013-01-29 03:18:40 +0000 | [diff] [blame] | 2520 | static void relink_file_extents(struct new_sa_defrag_extent *new) |
| 2521 | { |
| 2522 | struct btrfs_path *path; |
Liu Bo | 38c227d | 2013-01-29 03:18:40 +0000 | [diff] [blame] | 2523 | struct sa_defrag_extent_backref *backref; |
| 2524 | struct sa_defrag_extent_backref *prev = NULL; |
| 2525 | struct inode *inode; |
| 2526 | struct btrfs_root *root; |
| 2527 | struct rb_node *node; |
| 2528 | int ret; |
| 2529 | |
| 2530 | inode = new->inode; |
| 2531 | root = BTRFS_I(inode)->root; |
| 2532 | |
| 2533 | path = btrfs_alloc_path(); |
| 2534 | if (!path) |
| 2535 | return; |
| 2536 | |
| 2537 | if (!record_extent_backrefs(path, new)) { |
| 2538 | btrfs_free_path(path); |
| 2539 | goto out; |
| 2540 | } |
| 2541 | btrfs_release_path(path); |
| 2542 | |
| 2543 | while (1) { |
| 2544 | node = rb_first(&new->root); |
| 2545 | if (!node) |
| 2546 | break; |
| 2547 | rb_erase(node, &new->root); |
| 2548 | |
| 2549 | backref = rb_entry(node, struct sa_defrag_extent_backref, node); |
| 2550 | |
| 2551 | ret = relink_extent_backref(path, prev, backref); |
| 2552 | WARN_ON(ret < 0); |
| 2553 | |
| 2554 | kfree(prev); |
| 2555 | |
| 2556 | if (ret == 1) |
| 2557 | prev = backref; |
| 2558 | else |
| 2559 | prev = NULL; |
| 2560 | cond_resched(); |
| 2561 | } |
| 2562 | kfree(prev); |
| 2563 | |
| 2564 | btrfs_free_path(path); |
Liu Bo | 38c227d | 2013-01-29 03:18:40 +0000 | [diff] [blame] | 2565 | out: |
Liu Bo | 6f51956 | 2013-10-29 10:45:05 +0800 | [diff] [blame] | 2566 | free_sa_defrag_extent(new); |
| 2567 | |
Liu Bo | 38c227d | 2013-01-29 03:18:40 +0000 | [diff] [blame] | 2568 | atomic_dec(&root->fs_info->defrag_running); |
| 2569 | wake_up(&root->fs_info->transaction_wait); |
Liu Bo | 38c227d | 2013-01-29 03:18:40 +0000 | [diff] [blame] | 2570 | } |
| 2571 | |
| 2572 | static struct new_sa_defrag_extent * |
| 2573 | record_old_file_extents(struct inode *inode, |
| 2574 | struct btrfs_ordered_extent *ordered) |
| 2575 | { |
| 2576 | struct btrfs_root *root = BTRFS_I(inode)->root; |
| 2577 | struct btrfs_path *path; |
| 2578 | struct btrfs_key key; |
Liu Bo | 6f51956 | 2013-10-29 10:45:05 +0800 | [diff] [blame] | 2579 | struct old_sa_defrag_extent *old; |
Liu Bo | 38c227d | 2013-01-29 03:18:40 +0000 | [diff] [blame] | 2580 | struct new_sa_defrag_extent *new; |
| 2581 | int ret; |
| 2582 | |
| 2583 | new = kmalloc(sizeof(*new), GFP_NOFS); |
| 2584 | if (!new) |
| 2585 | return NULL; |
| 2586 | |
| 2587 | new->inode = inode; |
| 2588 | new->file_pos = ordered->file_offset; |
| 2589 | new->len = ordered->len; |
| 2590 | new->bytenr = ordered->start; |
| 2591 | new->disk_len = ordered->disk_len; |
| 2592 | new->compress_type = ordered->compress_type; |
| 2593 | new->root = RB_ROOT; |
| 2594 | INIT_LIST_HEAD(&new->head); |
| 2595 | |
| 2596 | path = btrfs_alloc_path(); |
| 2597 | if (!path) |
| 2598 | goto out_kfree; |
| 2599 | |
| 2600 | key.objectid = btrfs_ino(inode); |
| 2601 | key.type = BTRFS_EXTENT_DATA_KEY; |
| 2602 | key.offset = new->file_pos; |
| 2603 | |
| 2604 | ret = btrfs_search_slot(NULL, root, &key, path, 0, 0); |
| 2605 | if (ret < 0) |
| 2606 | goto out_free_path; |
| 2607 | if (ret > 0 && path->slots[0] > 0) |
| 2608 | path->slots[0]--; |
| 2609 | |
| 2610 | /* find out all the old extents for the file range */ |
| 2611 | while (1) { |
| 2612 | struct btrfs_file_extent_item *extent; |
| 2613 | struct extent_buffer *l; |
| 2614 | int slot; |
| 2615 | u64 num_bytes; |
| 2616 | u64 offset; |
| 2617 | u64 end; |
| 2618 | u64 disk_bytenr; |
| 2619 | u64 extent_offset; |
| 2620 | |
| 2621 | l = path->nodes[0]; |
| 2622 | slot = path->slots[0]; |
| 2623 | |
| 2624 | if (slot >= btrfs_header_nritems(l)) { |
| 2625 | ret = btrfs_next_leaf(root, path); |
| 2626 | if (ret < 0) |
Liu Bo | 6f51956 | 2013-10-29 10:45:05 +0800 | [diff] [blame] | 2627 | goto out_free_path; |
Liu Bo | 38c227d | 2013-01-29 03:18:40 +0000 | [diff] [blame] | 2628 | else if (ret > 0) |
| 2629 | break; |
| 2630 | continue; |
| 2631 | } |
| 2632 | |
| 2633 | btrfs_item_key_to_cpu(l, &key, slot); |
| 2634 | |
| 2635 | if (key.objectid != btrfs_ino(inode)) |
| 2636 | break; |
| 2637 | if (key.type != BTRFS_EXTENT_DATA_KEY) |
| 2638 | break; |
| 2639 | if (key.offset >= new->file_pos + new->len) |
| 2640 | break; |
| 2641 | |
| 2642 | extent = btrfs_item_ptr(l, slot, struct btrfs_file_extent_item); |
| 2643 | |
| 2644 | num_bytes = btrfs_file_extent_num_bytes(l, extent); |
| 2645 | if (key.offset + num_bytes < new->file_pos) |
| 2646 | goto next; |
| 2647 | |
| 2648 | disk_bytenr = btrfs_file_extent_disk_bytenr(l, extent); |
| 2649 | if (!disk_bytenr) |
| 2650 | goto next; |
| 2651 | |
| 2652 | extent_offset = btrfs_file_extent_offset(l, extent); |
| 2653 | |
| 2654 | old = kmalloc(sizeof(*old), GFP_NOFS); |
| 2655 | if (!old) |
Liu Bo | 6f51956 | 2013-10-29 10:45:05 +0800 | [diff] [blame] | 2656 | goto out_free_path; |
Liu Bo | 38c227d | 2013-01-29 03:18:40 +0000 | [diff] [blame] | 2657 | |
| 2658 | offset = max(new->file_pos, key.offset); |
| 2659 | end = min(new->file_pos + new->len, key.offset + num_bytes); |
| 2660 | |
| 2661 | old->bytenr = disk_bytenr; |
| 2662 | old->extent_offset = extent_offset; |
| 2663 | old->offset = offset - key.offset; |
| 2664 | old->len = end - offset; |
| 2665 | old->new = new; |
| 2666 | old->count = 0; |
| 2667 | list_add_tail(&old->list, &new->head); |
| 2668 | next: |
| 2669 | path->slots[0]++; |
| 2670 | cond_resched(); |
| 2671 | } |
| 2672 | |
| 2673 | btrfs_free_path(path); |
| 2674 | atomic_inc(&root->fs_info->defrag_running); |
| 2675 | |
| 2676 | return new; |
| 2677 | |
Liu Bo | 38c227d | 2013-01-29 03:18:40 +0000 | [diff] [blame] | 2678 | out_free_path: |
| 2679 | btrfs_free_path(path); |
| 2680 | out_kfree: |
Liu Bo | 6f51956 | 2013-10-29 10:45:05 +0800 | [diff] [blame] | 2681 | free_sa_defrag_extent(new); |
Liu Bo | 38c227d | 2013-01-29 03:18:40 +0000 | [diff] [blame] | 2682 | return NULL; |
| 2683 | } |
| 2684 | |
Miao Xie | e570fd2 | 2014-06-19 10:42:50 +0800 | [diff] [blame] | 2685 | static void btrfs_release_delalloc_bytes(struct btrfs_root *root, |
| 2686 | u64 start, u64 len) |
| 2687 | { |
| 2688 | struct btrfs_block_group_cache *cache; |
| 2689 | |
| 2690 | cache = btrfs_lookup_block_group(root->fs_info, start); |
| 2691 | ASSERT(cache); |
| 2692 | |
| 2693 | spin_lock(&cache->lock); |
| 2694 | cache->delalloc_bytes -= len; |
| 2695 | spin_unlock(&cache->lock); |
| 2696 | |
| 2697 | btrfs_put_block_group(cache); |
| 2698 | } |
| 2699 | |
Chris Mason | d352ac6 | 2008-09-29 15:18:18 -0400 | [diff] [blame] | 2700 | /* as ordered data IO finishes, this gets called so we can finish |
| 2701 | * an ordered extent if the range of bytes in the file it covers are |
| 2702 | * fully written. |
| 2703 | */ |
Josef Bacik | 5fd0204 | 2012-05-02 14:00:54 -0400 | [diff] [blame] | 2704 | static int btrfs_finish_ordered_io(struct btrfs_ordered_extent *ordered_extent) |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 2705 | { |
Josef Bacik | 5fd0204 | 2012-05-02 14:00:54 -0400 | [diff] [blame] | 2706 | struct inode *inode = ordered_extent->inode; |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 2707 | struct btrfs_root *root = BTRFS_I(inode)->root; |
Yan, Zheng | 0ca1f7c | 2010-05-16 10:48:47 -0400 | [diff] [blame] | 2708 | struct btrfs_trans_handle *trans = NULL; |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 2709 | struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree; |
Josef Bacik | 2ac55d4 | 2010-02-03 19:33:23 +0000 | [diff] [blame] | 2710 | struct extent_state *cached_state = NULL; |
Liu Bo | 38c227d | 2013-01-29 03:18:40 +0000 | [diff] [blame] | 2711 | struct new_sa_defrag_extent *new = NULL; |
Li Zefan | 261507a0 | 2010-12-17 14:21:50 +0800 | [diff] [blame] | 2712 | int compress_type = 0; |
Josef Bacik | 77cef2e | 2013-08-29 13:57:21 -0400 | [diff] [blame] | 2713 | int ret = 0; |
| 2714 | u64 logical_len = ordered_extent->len; |
Li Zefan | 82d5902 | 2011-04-20 10:33:24 +0800 | [diff] [blame] | 2715 | bool nolock; |
Josef Bacik | 77cef2e | 2013-08-29 13:57:21 -0400 | [diff] [blame] | 2716 | bool truncated = false; |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 2717 | |
Liu Bo | 83eea1f | 2012-07-10 05:28:39 -0600 | [diff] [blame] | 2718 | nolock = btrfs_is_free_space_inode(inode); |
Josef Bacik | 0cb59c9 | 2010-07-02 12:14:14 -0400 | [diff] [blame] | 2719 | |
Josef Bacik | 5fd0204 | 2012-05-02 14:00:54 -0400 | [diff] [blame] | 2720 | if (test_bit(BTRFS_ORDERED_IOERR, &ordered_extent->flags)) { |
| 2721 | ret = -EIO; |
| 2722 | goto out; |
| 2723 | } |
| 2724 | |
Miao Xie | f612496 | 2014-09-12 18:44:04 +0800 | [diff] [blame] | 2725 | btrfs_free_io_failure_record(inode, ordered_extent->file_offset, |
| 2726 | ordered_extent->file_offset + |
| 2727 | ordered_extent->len - 1); |
| 2728 | |
Josef Bacik | 77cef2e | 2013-08-29 13:57:21 -0400 | [diff] [blame] | 2729 | if (test_bit(BTRFS_ORDERED_TRUNCATED, &ordered_extent->flags)) { |
| 2730 | truncated = true; |
| 2731 | logical_len = ordered_extent->truncated_len; |
| 2732 | /* Truncated the entire extent, don't bother adding */ |
| 2733 | if (!logical_len) |
| 2734 | goto out; |
| 2735 | } |
| 2736 | |
Yan, Zheng | c216775 | 2009-11-12 09:34:21 +0000 | [diff] [blame] | 2737 | if (test_bit(BTRFS_ORDERED_NOCOW, &ordered_extent->flags)) { |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 2738 | BUG_ON(!list_empty(&ordered_extent->list)); /* Logic error */ |
Josef Bacik | 6c760c0 | 2012-11-09 10:53:21 -0500 | [diff] [blame] | 2739 | btrfs_ordered_update_i_size(inode, 0, ordered_extent); |
| 2740 | if (nolock) |
| 2741 | trans = btrfs_join_transaction_nolock(root); |
| 2742 | else |
| 2743 | trans = btrfs_join_transaction(root); |
| 2744 | if (IS_ERR(trans)) { |
| 2745 | ret = PTR_ERR(trans); |
| 2746 | trans = NULL; |
| 2747 | goto out; |
Yan, Zheng | c216775 | 2009-11-12 09:34:21 +0000 | [diff] [blame] | 2748 | } |
Josef Bacik | 6c760c0 | 2012-11-09 10:53:21 -0500 | [diff] [blame] | 2749 | trans->block_rsv = &root->fs_info->delalloc_block_rsv; |
| 2750 | ret = btrfs_update_inode_fallback(trans, root, inode); |
| 2751 | if (ret) /* -ENOMEM or corruption */ |
| 2752 | btrfs_abort_transaction(trans, root, ret); |
Yan, Zheng | c216775 | 2009-11-12 09:34:21 +0000 | [diff] [blame] | 2753 | goto out; |
| 2754 | } |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 2755 | |
Josef Bacik | 2ac55d4 | 2010-02-03 19:33:23 +0000 | [diff] [blame] | 2756 | lock_extent_bits(io_tree, ordered_extent->file_offset, |
| 2757 | ordered_extent->file_offset + ordered_extent->len - 1, |
Jeff Mahoney | d008237 | 2012-03-01 14:57:19 +0100 | [diff] [blame] | 2758 | 0, &cached_state); |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 2759 | |
Liu Bo | 38c227d | 2013-01-29 03:18:40 +0000 | [diff] [blame] | 2760 | ret = test_range_bit(io_tree, ordered_extent->file_offset, |
| 2761 | ordered_extent->file_offset + ordered_extent->len - 1, |
| 2762 | EXTENT_DEFRAG, 1, cached_state); |
| 2763 | if (ret) { |
| 2764 | u64 last_snapshot = btrfs_root_last_snapshot(&root->root_item); |
Josef Bacik | 8101c8d | 2014-01-29 16:05:30 -0500 | [diff] [blame] | 2765 | if (0 && last_snapshot >= BTRFS_I(inode)->generation) |
Liu Bo | 38c227d | 2013-01-29 03:18:40 +0000 | [diff] [blame] | 2766 | /* the inode is shared */ |
| 2767 | new = record_old_file_extents(inode, ordered_extent); |
| 2768 | |
| 2769 | clear_extent_bit(io_tree, ordered_extent->file_offset, |
| 2770 | ordered_extent->file_offset + ordered_extent->len - 1, |
| 2771 | EXTENT_DEFRAG, 0, 0, &cached_state, GFP_NOFS); |
| 2772 | } |
| 2773 | |
Josef Bacik | 0cb59c9 | 2010-07-02 12:14:14 -0400 | [diff] [blame] | 2774 | if (nolock) |
Josef Bacik | 7a7eaa4 | 2011-04-13 12:54:33 -0400 | [diff] [blame] | 2775 | trans = btrfs_join_transaction_nolock(root); |
Josef Bacik | 0cb59c9 | 2010-07-02 12:14:14 -0400 | [diff] [blame] | 2776 | else |
Josef Bacik | 7a7eaa4 | 2011-04-13 12:54:33 -0400 | [diff] [blame] | 2777 | trans = btrfs_join_transaction(root); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 2778 | if (IS_ERR(trans)) { |
| 2779 | ret = PTR_ERR(trans); |
| 2780 | trans = NULL; |
| 2781 | goto out_unlock; |
| 2782 | } |
Chris Mason | a79b7d4 | 2014-05-22 16:18:52 -0700 | [diff] [blame] | 2783 | |
Yan, Zheng | 0ca1f7c | 2010-05-16 10:48:47 -0400 | [diff] [blame] | 2784 | trans->block_rsv = &root->fs_info->delalloc_block_rsv; |
Yan, Zheng | c216775 | 2009-11-12 09:34:21 +0000 | [diff] [blame] | 2785 | |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 2786 | if (test_bit(BTRFS_ORDERED_COMPRESSED, &ordered_extent->flags)) |
Li Zefan | 261507a0 | 2010-12-17 14:21:50 +0800 | [diff] [blame] | 2787 | compress_type = ordered_extent->compress_type; |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 2788 | if (test_bit(BTRFS_ORDERED_PREALLOC, &ordered_extent->flags)) { |
Li Zefan | 261507a0 | 2010-12-17 14:21:50 +0800 | [diff] [blame] | 2789 | BUG_ON(compress_type); |
Yan, Zheng | 920bbbf | 2009-11-12 09:34:08 +0000 | [diff] [blame] | 2790 | ret = btrfs_mark_extent_written(trans, inode, |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 2791 | ordered_extent->file_offset, |
| 2792 | ordered_extent->file_offset + |
Josef Bacik | 77cef2e | 2013-08-29 13:57:21 -0400 | [diff] [blame] | 2793 | logical_len); |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 2794 | } else { |
Josef Bacik | 0af3d00 | 2010-06-21 14:48:16 -0400 | [diff] [blame] | 2795 | BUG_ON(root == root->fs_info->tree_root); |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 2796 | ret = insert_reserved_file_extent(trans, inode, |
| 2797 | ordered_extent->file_offset, |
| 2798 | ordered_extent->start, |
| 2799 | ordered_extent->disk_len, |
Josef Bacik | 77cef2e | 2013-08-29 13:57:21 -0400 | [diff] [blame] | 2800 | logical_len, logical_len, |
Li Zefan | 261507a0 | 2010-12-17 14:21:50 +0800 | [diff] [blame] | 2801 | compress_type, 0, 0, |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 2802 | BTRFS_FILE_EXTENT_REG); |
Miao Xie | e570fd2 | 2014-06-19 10:42:50 +0800 | [diff] [blame] | 2803 | if (!ret) |
| 2804 | btrfs_release_delalloc_bytes(root, |
| 2805 | ordered_extent->start, |
| 2806 | ordered_extent->disk_len); |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 2807 | } |
Josef Bacik | 5dc562c | 2012-08-17 13:14:17 -0400 | [diff] [blame] | 2808 | unpin_extent_cache(&BTRFS_I(inode)->extent_tree, |
| 2809 | ordered_extent->file_offset, ordered_extent->len, |
| 2810 | trans->transid); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 2811 | if (ret < 0) { |
| 2812 | btrfs_abort_transaction(trans, root, ret); |
Josef Bacik | 5fd0204 | 2012-05-02 14:00:54 -0400 | [diff] [blame] | 2813 | goto out_unlock; |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 2814 | } |
Josef Bacik | 2ac55d4 | 2010-02-03 19:33:23 +0000 | [diff] [blame] | 2815 | |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 2816 | add_pending_csums(trans, inode, ordered_extent->file_offset, |
| 2817 | &ordered_extent->list); |
| 2818 | |
Josef Bacik | 6c760c0 | 2012-11-09 10:53:21 -0500 | [diff] [blame] | 2819 | btrfs_ordered_update_i_size(inode, 0, ordered_extent); |
| 2820 | ret = btrfs_update_inode_fallback(trans, root, inode); |
| 2821 | if (ret) { /* -ENOMEM or corruption */ |
| 2822 | btrfs_abort_transaction(trans, root, ret); |
| 2823 | goto out_unlock; |
Josef Bacik | 1ef30be | 2011-04-05 19:25:36 -0400 | [diff] [blame] | 2824 | } |
| 2825 | ret = 0; |
Josef Bacik | 5fd0204 | 2012-05-02 14:00:54 -0400 | [diff] [blame] | 2826 | out_unlock: |
| 2827 | unlock_extent_cached(io_tree, ordered_extent->file_offset, |
| 2828 | ordered_extent->file_offset + |
| 2829 | ordered_extent->len - 1, &cached_state, GFP_NOFS); |
Yan, Zheng | c216775 | 2009-11-12 09:34:21 +0000 | [diff] [blame] | 2830 | out: |
Josef Bacik | 5b0e95b | 2011-10-06 08:58:24 -0400 | [diff] [blame] | 2831 | if (root != root->fs_info->tree_root) |
Josef Bacik | 0cb59c9 | 2010-07-02 12:14:14 -0400 | [diff] [blame] | 2832 | btrfs_delalloc_release_metadata(inode, ordered_extent->len); |
Miao Xie | a698d075 | 2012-09-20 01:51:59 -0600 | [diff] [blame] | 2833 | if (trans) |
| 2834 | btrfs_end_transaction(trans, root); |
Josef Bacik | 0cb59c9 | 2010-07-02 12:14:14 -0400 | [diff] [blame] | 2835 | |
Josef Bacik | 77cef2e | 2013-08-29 13:57:21 -0400 | [diff] [blame] | 2836 | if (ret || truncated) { |
| 2837 | u64 start, end; |
| 2838 | |
| 2839 | if (truncated) |
| 2840 | start = ordered_extent->file_offset + logical_len; |
| 2841 | else |
| 2842 | start = ordered_extent->file_offset; |
| 2843 | end = ordered_extent->file_offset + ordered_extent->len - 1; |
| 2844 | clear_extent_uptodate(io_tree, start, end, NULL, GFP_NOFS); |
| 2845 | |
| 2846 | /* Drop the cache for the part of the extent we didn't write. */ |
| 2847 | btrfs_drop_extent_cache(inode, start, end, 0); |
Josef Bacik | 5fd0204 | 2012-05-02 14:00:54 -0400 | [diff] [blame] | 2848 | |
Josef Bacik | 0bec9ef | 2013-01-31 14:58:00 -0500 | [diff] [blame] | 2849 | /* |
| 2850 | * If the ordered extent had an IOERR or something else went |
| 2851 | * wrong we need to return the space for this ordered extent |
Josef Bacik | 77cef2e | 2013-08-29 13:57:21 -0400 | [diff] [blame] | 2852 | * back to the allocator. We only free the extent in the |
| 2853 | * truncated case if we didn't write out the extent at all. |
Josef Bacik | 0bec9ef | 2013-01-31 14:58:00 -0500 | [diff] [blame] | 2854 | */ |
Josef Bacik | 77cef2e | 2013-08-29 13:57:21 -0400 | [diff] [blame] | 2855 | if ((ret || !logical_len) && |
| 2856 | !test_bit(BTRFS_ORDERED_NOCOW, &ordered_extent->flags) && |
Josef Bacik | 0bec9ef | 2013-01-31 14:58:00 -0500 | [diff] [blame] | 2857 | !test_bit(BTRFS_ORDERED_PREALLOC, &ordered_extent->flags)) |
| 2858 | btrfs_free_reserved_extent(root, ordered_extent->start, |
Miao Xie | e570fd2 | 2014-06-19 10:42:50 +0800 | [diff] [blame] | 2859 | ordered_extent->disk_len, 1); |
Josef Bacik | 0bec9ef | 2013-01-31 14:58:00 -0500 | [diff] [blame] | 2860 | } |
| 2861 | |
| 2862 | |
Josef Bacik | 5fd0204 | 2012-05-02 14:00:54 -0400 | [diff] [blame] | 2863 | /* |
Liu Bo | 8bad3c0 | 2012-06-18 12:14:23 +0800 | [diff] [blame] | 2864 | * This needs to be done to make sure anybody waiting knows we are done |
| 2865 | * updating everything for this ordered extent. |
Josef Bacik | 5fd0204 | 2012-05-02 14:00:54 -0400 | [diff] [blame] | 2866 | */ |
| 2867 | btrfs_remove_ordered_extent(inode, ordered_extent); |
| 2868 | |
Liu Bo | 38c227d | 2013-01-29 03:18:40 +0000 | [diff] [blame] | 2869 | /* for snapshot-aware defrag */ |
Liu Bo | 6f51956 | 2013-10-29 10:45:05 +0800 | [diff] [blame] | 2870 | if (new) { |
| 2871 | if (ret) { |
| 2872 | free_sa_defrag_extent(new); |
| 2873 | atomic_dec(&root->fs_info->defrag_running); |
| 2874 | } else { |
| 2875 | relink_file_extents(new); |
| 2876 | } |
| 2877 | } |
Liu Bo | 38c227d | 2013-01-29 03:18:40 +0000 | [diff] [blame] | 2878 | |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 2879 | /* once for us */ |
| 2880 | btrfs_put_ordered_extent(ordered_extent); |
| 2881 | /* once for the tree */ |
| 2882 | btrfs_put_ordered_extent(ordered_extent); |
| 2883 | |
Josef Bacik | 5fd0204 | 2012-05-02 14:00:54 -0400 | [diff] [blame] | 2884 | return ret; |
| 2885 | } |
| 2886 | |
| 2887 | static void finish_ordered_fn(struct btrfs_work *work) |
| 2888 | { |
| 2889 | struct btrfs_ordered_extent *ordered_extent; |
| 2890 | ordered_extent = container_of(work, struct btrfs_ordered_extent, work); |
| 2891 | btrfs_finish_ordered_io(ordered_extent); |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 2892 | } |
| 2893 | |
Christoph Hellwig | b295086 | 2008-12-02 09:54:17 -0500 | [diff] [blame] | 2894 | 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] | 2895 | struct extent_state *state, int uptodate) |
| 2896 | { |
Josef Bacik | 5fd0204 | 2012-05-02 14:00:54 -0400 | [diff] [blame] | 2897 | struct inode *inode = page->mapping->host; |
| 2898 | struct btrfs_root *root = BTRFS_I(inode)->root; |
| 2899 | struct btrfs_ordered_extent *ordered_extent = NULL; |
Liu Bo | 9e0af23 | 2014-08-15 23:36:53 +0800 | [diff] [blame] | 2900 | struct btrfs_workqueue *wq; |
| 2901 | btrfs_work_func_t func; |
Josef Bacik | 5fd0204 | 2012-05-02 14:00:54 -0400 | [diff] [blame] | 2902 | |
liubo | 1abe9b8 | 2011-03-24 11:18:59 +0000 | [diff] [blame] | 2903 | trace_btrfs_writepage_end_io_hook(page, start, end, uptodate); |
| 2904 | |
Chris Mason | 8b62b72 | 2009-09-02 16:53:46 -0400 | [diff] [blame] | 2905 | ClearPagePrivate2(page); |
Josef Bacik | 5fd0204 | 2012-05-02 14:00:54 -0400 | [diff] [blame] | 2906 | if (!btrfs_dec_test_ordered_pending(inode, &ordered_extent, start, |
| 2907 | end - start + 1, uptodate)) |
| 2908 | return 0; |
| 2909 | |
Liu Bo | 9e0af23 | 2014-08-15 23:36:53 +0800 | [diff] [blame] | 2910 | if (btrfs_is_free_space_inode(inode)) { |
| 2911 | wq = root->fs_info->endio_freespace_worker; |
| 2912 | func = btrfs_freespace_write_helper; |
| 2913 | } else { |
| 2914 | wq = root->fs_info->endio_write_workers; |
| 2915 | func = btrfs_endio_write_helper; |
| 2916 | } |
Josef Bacik | 5fd0204 | 2012-05-02 14:00:54 -0400 | [diff] [blame] | 2917 | |
Liu Bo | 9e0af23 | 2014-08-15 23:36:53 +0800 | [diff] [blame] | 2918 | btrfs_init_work(&ordered_extent->work, func, finish_ordered_fn, NULL, |
| 2919 | NULL); |
| 2920 | btrfs_queue_work(wq, &ordered_extent->work); |
Josef Bacik | 5fd0204 | 2012-05-02 14:00:54 -0400 | [diff] [blame] | 2921 | |
| 2922 | return 0; |
Chris Mason | 211f90e | 2008-07-18 11:56:15 -0400 | [diff] [blame] | 2923 | } |
| 2924 | |
Miao Xie | dc380ae | 2014-09-12 18:43:55 +0800 | [diff] [blame] | 2925 | static int __readpage_endio_check(struct inode *inode, |
| 2926 | struct btrfs_io_bio *io_bio, |
| 2927 | int icsum, struct page *page, |
| 2928 | int pgoff, u64 start, size_t len) |
| 2929 | { |
| 2930 | char *kaddr; |
| 2931 | u32 csum_expected; |
| 2932 | u32 csum = ~(u32)0; |
| 2933 | static DEFINE_RATELIMIT_STATE(_rs, DEFAULT_RATELIMIT_INTERVAL, |
| 2934 | DEFAULT_RATELIMIT_BURST); |
| 2935 | |
| 2936 | csum_expected = *(((u32 *)io_bio->csum) + icsum); |
| 2937 | |
| 2938 | kaddr = kmap_atomic(page); |
| 2939 | csum = btrfs_csum_data(kaddr + pgoff, csum, len); |
| 2940 | btrfs_csum_final(csum, (char *)&csum); |
| 2941 | if (csum != csum_expected) |
| 2942 | goto zeroit; |
| 2943 | |
| 2944 | kunmap_atomic(kaddr); |
| 2945 | return 0; |
| 2946 | zeroit: |
| 2947 | if (__ratelimit(&_rs)) |
| 2948 | btrfs_info(BTRFS_I(inode)->root->fs_info, |
| 2949 | "csum failed ino %llu off %llu csum %u expected csum %u", |
| 2950 | btrfs_ino(inode), start, csum, csum_expected); |
| 2951 | memset(kaddr + pgoff, 1, len); |
| 2952 | flush_dcache_page(page); |
| 2953 | kunmap_atomic(kaddr); |
| 2954 | if (csum_expected == 0) |
| 2955 | return 0; |
| 2956 | return -EIO; |
| 2957 | } |
| 2958 | |
Chris Mason | d352ac6 | 2008-09-29 15:18:18 -0400 | [diff] [blame] | 2959 | /* |
Chris Mason | d352ac6 | 2008-09-29 15:18:18 -0400 | [diff] [blame] | 2960 | * 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] | 2961 | * if there's a match, we allow the bio to finish. If not, the code in |
| 2962 | * extent_io.c will try to find good copies for us. |
Chris Mason | d352ac6 | 2008-09-29 15:18:18 -0400 | [diff] [blame] | 2963 | */ |
Miao Xie | facc8a22 | 2013-07-25 19:22:34 +0800 | [diff] [blame] | 2964 | static int btrfs_readpage_end_io_hook(struct btrfs_io_bio *io_bio, |
| 2965 | u64 phy_offset, struct page *page, |
| 2966 | u64 start, u64 end, int mirror) |
Chris Mason | 07157aa | 2007-08-30 08:50:51 -0400 | [diff] [blame] | 2967 | { |
Miao Xie | 4eee4fa | 2012-12-21 09:17:45 +0000 | [diff] [blame] | 2968 | size_t offset = start - page_offset(page); |
Chris Mason | 07157aa | 2007-08-30 08:50:51 -0400 | [diff] [blame] | 2969 | struct inode *inode = page->mapping->host; |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 2970 | struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree; |
Chris Mason | ff79f81 | 2007-10-15 16:22:25 -0400 | [diff] [blame] | 2971 | struct btrfs_root *root = BTRFS_I(inode)->root; |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 2972 | |
Chris Mason | d20f704 | 2008-12-08 16:58:54 -0500 | [diff] [blame] | 2973 | if (PageChecked(page)) { |
| 2974 | ClearPageChecked(page); |
Miao Xie | dc380ae | 2014-09-12 18:43:55 +0800 | [diff] [blame] | 2975 | return 0; |
Chris Mason | d20f704 | 2008-12-08 16:58:54 -0500 | [diff] [blame] | 2976 | } |
Christoph Hellwig | 6cbff00 | 2009-04-17 10:37:41 +0200 | [diff] [blame] | 2977 | |
| 2978 | if (BTRFS_I(inode)->flags & BTRFS_INODE_NODATASUM) |
Miao Xie | dc380ae | 2014-09-12 18:43:55 +0800 | [diff] [blame] | 2979 | return 0; |
Chris Mason | d20f704 | 2008-12-08 16:58:54 -0500 | [diff] [blame] | 2980 | |
Yan Zheng | 17d217f | 2008-12-12 10:03:38 -0500 | [diff] [blame] | 2981 | if (root->root_key.objectid == BTRFS_DATA_RELOC_TREE_OBJECTID && |
Chris Mason | 9655d29 | 2009-09-02 15:22:30 -0400 | [diff] [blame] | 2982 | test_range_bit(io_tree, start, end, EXTENT_NODATASUM, 1, NULL)) { |
Yan Zheng | 17d217f | 2008-12-12 10:03:38 -0500 | [diff] [blame] | 2983 | clear_extent_bits(io_tree, start, end, EXTENT_NODATASUM, |
| 2984 | GFP_NOFS); |
| 2985 | return 0; |
| 2986 | } |
| 2987 | |
Miao Xie | facc8a22 | 2013-07-25 19:22:34 +0800 | [diff] [blame] | 2988 | phy_offset >>= inode->i_sb->s_blocksize_bits; |
Miao Xie | dc380ae | 2014-09-12 18:43:55 +0800 | [diff] [blame] | 2989 | return __readpage_endio_check(inode, io_bio, phy_offset, page, offset, |
| 2990 | start, (size_t)(end - start + 1)); |
Chris Mason | 07157aa | 2007-08-30 08:50:51 -0400 | [diff] [blame] | 2991 | } |
Chris Mason | b888db2 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 2992 | |
Yan, Zheng | 24bbcf0 | 2009-11-12 09:36:34 +0000 | [diff] [blame] | 2993 | struct delayed_iput { |
| 2994 | struct list_head list; |
| 2995 | struct inode *inode; |
| 2996 | }; |
| 2997 | |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 2998 | /* JDM: If this is fs-wide, why can't we add a pointer to |
| 2999 | * btrfs_inode instead and avoid the allocation? */ |
Yan, Zheng | 24bbcf0 | 2009-11-12 09:36:34 +0000 | [diff] [blame] | 3000 | void btrfs_add_delayed_iput(struct inode *inode) |
| 3001 | { |
| 3002 | struct btrfs_fs_info *fs_info = BTRFS_I(inode)->root->fs_info; |
| 3003 | struct delayed_iput *delayed; |
| 3004 | |
| 3005 | if (atomic_add_unless(&inode->i_count, -1, 1)) |
| 3006 | return; |
| 3007 | |
| 3008 | delayed = kmalloc(sizeof(*delayed), GFP_NOFS | __GFP_NOFAIL); |
| 3009 | delayed->inode = inode; |
| 3010 | |
| 3011 | spin_lock(&fs_info->delayed_iput_lock); |
| 3012 | list_add_tail(&delayed->list, &fs_info->delayed_iputs); |
| 3013 | spin_unlock(&fs_info->delayed_iput_lock); |
| 3014 | } |
| 3015 | |
| 3016 | void btrfs_run_delayed_iputs(struct btrfs_root *root) |
| 3017 | { |
| 3018 | LIST_HEAD(list); |
| 3019 | struct btrfs_fs_info *fs_info = root->fs_info; |
| 3020 | struct delayed_iput *delayed; |
| 3021 | int empty; |
| 3022 | |
| 3023 | spin_lock(&fs_info->delayed_iput_lock); |
| 3024 | empty = list_empty(&fs_info->delayed_iputs); |
| 3025 | spin_unlock(&fs_info->delayed_iput_lock); |
| 3026 | if (empty) |
| 3027 | return; |
| 3028 | |
Yan, Zheng | 24bbcf0 | 2009-11-12 09:36:34 +0000 | [diff] [blame] | 3029 | spin_lock(&fs_info->delayed_iput_lock); |
| 3030 | list_splice_init(&fs_info->delayed_iputs, &list); |
| 3031 | spin_unlock(&fs_info->delayed_iput_lock); |
| 3032 | |
| 3033 | while (!list_empty(&list)) { |
| 3034 | delayed = list_entry(list.next, struct delayed_iput, list); |
| 3035 | list_del(&delayed->list); |
| 3036 | iput(delayed->inode); |
| 3037 | kfree(delayed); |
| 3038 | } |
Yan, Zheng | 24bbcf0 | 2009-11-12 09:36:34 +0000 | [diff] [blame] | 3039 | } |
| 3040 | |
Yan, Zheng | d68fc57 | 2010-05-16 10:49:58 -0400 | [diff] [blame] | 3041 | /* |
Justin P. Mattock | 42b2aa8 | 2011-11-28 20:31:00 -0800 | [diff] [blame] | 3042 | * This is called in transaction commit time. If there are no orphan |
Yan, Zheng | d68fc57 | 2010-05-16 10:49:58 -0400 | [diff] [blame] | 3043 | * files in the subvolume, it removes orphan item and frees block_rsv |
| 3044 | * structure. |
| 3045 | */ |
| 3046 | void btrfs_orphan_commit_root(struct btrfs_trans_handle *trans, |
| 3047 | struct btrfs_root *root) |
| 3048 | { |
Josef Bacik | 90290e1 | 2011-12-02 15:44:12 -0500 | [diff] [blame] | 3049 | struct btrfs_block_rsv *block_rsv; |
Yan, Zheng | d68fc57 | 2010-05-16 10:49:58 -0400 | [diff] [blame] | 3050 | int ret; |
| 3051 | |
Josef Bacik | 8a35d95 | 2012-05-23 14:26:42 -0400 | [diff] [blame] | 3052 | if (atomic_read(&root->orphan_inodes) || |
Yan, Zheng | d68fc57 | 2010-05-16 10:49:58 -0400 | [diff] [blame] | 3053 | root->orphan_cleanup_state != ORPHAN_CLEANUP_DONE) |
| 3054 | return; |
| 3055 | |
Josef Bacik | 90290e1 | 2011-12-02 15:44:12 -0500 | [diff] [blame] | 3056 | spin_lock(&root->orphan_lock); |
Josef Bacik | 8a35d95 | 2012-05-23 14:26:42 -0400 | [diff] [blame] | 3057 | if (atomic_read(&root->orphan_inodes)) { |
Josef Bacik | 90290e1 | 2011-12-02 15:44:12 -0500 | [diff] [blame] | 3058 | spin_unlock(&root->orphan_lock); |
| 3059 | return; |
| 3060 | } |
| 3061 | |
| 3062 | if (root->orphan_cleanup_state != ORPHAN_CLEANUP_DONE) { |
| 3063 | spin_unlock(&root->orphan_lock); |
| 3064 | return; |
| 3065 | } |
| 3066 | |
| 3067 | block_rsv = root->orphan_block_rsv; |
| 3068 | root->orphan_block_rsv = NULL; |
| 3069 | spin_unlock(&root->orphan_lock); |
| 3070 | |
Miao Xie | 27cdeb7 | 2014-04-02 19:51:05 +0800 | [diff] [blame] | 3071 | if (test_bit(BTRFS_ROOT_ORPHAN_ITEM_INSERTED, &root->state) && |
Yan, Zheng | d68fc57 | 2010-05-16 10:49:58 -0400 | [diff] [blame] | 3072 | btrfs_root_refs(&root->root_item) > 0) { |
| 3073 | ret = btrfs_del_orphan_item(trans, root->fs_info->tree_root, |
| 3074 | root->root_key.objectid); |
Josef Bacik | 4ef31a4 | 2013-08-13 14:10:08 -0400 | [diff] [blame] | 3075 | if (ret) |
| 3076 | btrfs_abort_transaction(trans, root, ret); |
| 3077 | else |
Miao Xie | 27cdeb7 | 2014-04-02 19:51:05 +0800 | [diff] [blame] | 3078 | clear_bit(BTRFS_ROOT_ORPHAN_ITEM_INSERTED, |
| 3079 | &root->state); |
Yan, Zheng | d68fc57 | 2010-05-16 10:49:58 -0400 | [diff] [blame] | 3080 | } |
| 3081 | |
Josef Bacik | 90290e1 | 2011-12-02 15:44:12 -0500 | [diff] [blame] | 3082 | if (block_rsv) { |
| 3083 | WARN_ON(block_rsv->size > 0); |
| 3084 | btrfs_free_block_rsv(root, block_rsv); |
Yan, Zheng | d68fc57 | 2010-05-16 10:49:58 -0400 | [diff] [blame] | 3085 | } |
| 3086 | } |
| 3087 | |
| 3088 | /* |
Josef Bacik | 7b12876 | 2008-07-24 12:17:14 -0400 | [diff] [blame] | 3089 | * This creates an orphan entry for the given inode in case something goes |
| 3090 | * wrong in the middle of an unlink/truncate. |
Yan, Zheng | d68fc57 | 2010-05-16 10:49:58 -0400 | [diff] [blame] | 3091 | * |
| 3092 | * NOTE: caller of this function should reserve 5 units of metadata for |
| 3093 | * this function. |
Josef Bacik | 7b12876 | 2008-07-24 12:17:14 -0400 | [diff] [blame] | 3094 | */ |
| 3095 | int btrfs_orphan_add(struct btrfs_trans_handle *trans, struct inode *inode) |
| 3096 | { |
| 3097 | struct btrfs_root *root = BTRFS_I(inode)->root; |
Yan, Zheng | d68fc57 | 2010-05-16 10:49:58 -0400 | [diff] [blame] | 3098 | struct btrfs_block_rsv *block_rsv = NULL; |
| 3099 | int reserve = 0; |
| 3100 | int insert = 0; |
| 3101 | int ret; |
Josef Bacik | 7b12876 | 2008-07-24 12:17:14 -0400 | [diff] [blame] | 3102 | |
Yan, Zheng | d68fc57 | 2010-05-16 10:49:58 -0400 | [diff] [blame] | 3103 | if (!root->orphan_block_rsv) { |
Miao Xie | 66d8f3d | 2012-09-06 04:02:28 -0600 | [diff] [blame] | 3104 | block_rsv = btrfs_alloc_block_rsv(root, BTRFS_BLOCK_RSV_TEMP); |
Tsutomu Itoh | b532402 | 2011-07-19 07:27:20 +0000 | [diff] [blame] | 3105 | if (!block_rsv) |
| 3106 | return -ENOMEM; |
Josef Bacik | 7b12876 | 2008-07-24 12:17:14 -0400 | [diff] [blame] | 3107 | } |
| 3108 | |
Yan, Zheng | d68fc57 | 2010-05-16 10:49:58 -0400 | [diff] [blame] | 3109 | spin_lock(&root->orphan_lock); |
| 3110 | if (!root->orphan_block_rsv) { |
| 3111 | root->orphan_block_rsv = block_rsv; |
| 3112 | } else if (block_rsv) { |
| 3113 | btrfs_free_block_rsv(root, block_rsv); |
| 3114 | block_rsv = NULL; |
| 3115 | } |
Josef Bacik | 7b12876 | 2008-07-24 12:17:14 -0400 | [diff] [blame] | 3116 | |
Josef Bacik | 8a35d95 | 2012-05-23 14:26:42 -0400 | [diff] [blame] | 3117 | if (!test_and_set_bit(BTRFS_INODE_HAS_ORPHAN_ITEM, |
| 3118 | &BTRFS_I(inode)->runtime_flags)) { |
Yan, Zheng | d68fc57 | 2010-05-16 10:49:58 -0400 | [diff] [blame] | 3119 | #if 0 |
| 3120 | /* |
| 3121 | * For proper ENOSPC handling, we should do orphan |
| 3122 | * cleanup when mounting. But this introduces backward |
| 3123 | * compatibility issue. |
| 3124 | */ |
| 3125 | if (!xchg(&root->orphan_item_inserted, 1)) |
| 3126 | insert = 2; |
| 3127 | else |
| 3128 | insert = 1; |
| 3129 | #endif |
| 3130 | insert = 1; |
Miao Xie | 321f0e7 | 2012-08-28 22:13:02 -0600 | [diff] [blame] | 3131 | atomic_inc(&root->orphan_inodes); |
Yan, Zheng | d68fc57 | 2010-05-16 10:49:58 -0400 | [diff] [blame] | 3132 | } |
Josef Bacik | 7b12876 | 2008-07-24 12:17:14 -0400 | [diff] [blame] | 3133 | |
Josef Bacik | 72ac3c0 | 2012-05-23 14:13:11 -0400 | [diff] [blame] | 3134 | if (!test_and_set_bit(BTRFS_INODE_ORPHAN_META_RESERVED, |
| 3135 | &BTRFS_I(inode)->runtime_flags)) |
Yan, Zheng | d68fc57 | 2010-05-16 10:49:58 -0400 | [diff] [blame] | 3136 | reserve = 1; |
Yan, Zheng | d68fc57 | 2010-05-16 10:49:58 -0400 | [diff] [blame] | 3137 | spin_unlock(&root->orphan_lock); |
Josef Bacik | 7b12876 | 2008-07-24 12:17:14 -0400 | [diff] [blame] | 3138 | |
Yan, Zheng | d68fc57 | 2010-05-16 10:49:58 -0400 | [diff] [blame] | 3139 | /* grab metadata reservation from transaction handle */ |
| 3140 | if (reserve) { |
| 3141 | ret = btrfs_orphan_reserve_metadata(trans, inode); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 3142 | BUG_ON(ret); /* -ENOSPC in reservation; Logic error? JDM */ |
Yan, Zheng | d68fc57 | 2010-05-16 10:49:58 -0400 | [diff] [blame] | 3143 | } |
| 3144 | |
| 3145 | /* insert an orphan item to track this unlinked/truncated file */ |
| 3146 | if (insert >= 1) { |
Li Zefan | 33345d01 | 2011-04-20 10:31:50 +0800 | [diff] [blame] | 3147 | ret = btrfs_insert_orphan_item(trans, root, btrfs_ino(inode)); |
Josef Bacik | 4ef31a4 | 2013-08-13 14:10:08 -0400 | [diff] [blame] | 3148 | if (ret) { |
Filipe David Borba Manana | 703c88e | 2013-09-22 21:54:55 +0100 | [diff] [blame] | 3149 | atomic_dec(&root->orphan_inodes); |
Josef Bacik | 4ef31a4 | 2013-08-13 14:10:08 -0400 | [diff] [blame] | 3150 | if (reserve) { |
| 3151 | clear_bit(BTRFS_INODE_ORPHAN_META_RESERVED, |
| 3152 | &BTRFS_I(inode)->runtime_flags); |
| 3153 | btrfs_orphan_release_metadata(inode); |
| 3154 | } |
| 3155 | if (ret != -EEXIST) { |
Josef Bacik | e8e7cff | 2013-08-21 15:54:00 -0400 | [diff] [blame] | 3156 | clear_bit(BTRFS_INODE_HAS_ORPHAN_ITEM, |
| 3157 | &BTRFS_I(inode)->runtime_flags); |
Josef Bacik | 4ef31a4 | 2013-08-13 14:10:08 -0400 | [diff] [blame] | 3158 | btrfs_abort_transaction(trans, root, ret); |
| 3159 | return ret; |
| 3160 | } |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 3161 | } |
| 3162 | ret = 0; |
Yan, Zheng | d68fc57 | 2010-05-16 10:49:58 -0400 | [diff] [blame] | 3163 | } |
| 3164 | |
| 3165 | /* insert an orphan item to track subvolume contains orphan files */ |
| 3166 | if (insert >= 2) { |
| 3167 | ret = btrfs_insert_orphan_item(trans, root->fs_info->tree_root, |
| 3168 | root->root_key.objectid); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 3169 | if (ret && ret != -EEXIST) { |
| 3170 | btrfs_abort_transaction(trans, root, ret); |
| 3171 | return ret; |
| 3172 | } |
Yan, Zheng | d68fc57 | 2010-05-16 10:49:58 -0400 | [diff] [blame] | 3173 | } |
| 3174 | return 0; |
Josef Bacik | 7b12876 | 2008-07-24 12:17:14 -0400 | [diff] [blame] | 3175 | } |
| 3176 | |
| 3177 | /* |
| 3178 | * We have done the truncate/delete so we can go ahead and remove the orphan |
| 3179 | * item for this particular inode. |
| 3180 | */ |
Eric Sandeen | 48a3b63 | 2013-04-25 20:41:01 +0000 | [diff] [blame] | 3181 | static int btrfs_orphan_del(struct btrfs_trans_handle *trans, |
| 3182 | struct inode *inode) |
Josef Bacik | 7b12876 | 2008-07-24 12:17:14 -0400 | [diff] [blame] | 3183 | { |
| 3184 | struct btrfs_root *root = BTRFS_I(inode)->root; |
Yan, Zheng | d68fc57 | 2010-05-16 10:49:58 -0400 | [diff] [blame] | 3185 | int delete_item = 0; |
| 3186 | int release_rsv = 0; |
Josef Bacik | 7b12876 | 2008-07-24 12:17:14 -0400 | [diff] [blame] | 3187 | int ret = 0; |
| 3188 | |
Yan, Zheng | d68fc57 | 2010-05-16 10:49:58 -0400 | [diff] [blame] | 3189 | spin_lock(&root->orphan_lock); |
Josef Bacik | 8a35d95 | 2012-05-23 14:26:42 -0400 | [diff] [blame] | 3190 | if (test_and_clear_bit(BTRFS_INODE_HAS_ORPHAN_ITEM, |
| 3191 | &BTRFS_I(inode)->runtime_flags)) |
Yan, Zheng | d68fc57 | 2010-05-16 10:49:58 -0400 | [diff] [blame] | 3192 | delete_item = 1; |
Josef Bacik | 7b12876 | 2008-07-24 12:17:14 -0400 | [diff] [blame] | 3193 | |
Josef Bacik | 72ac3c0 | 2012-05-23 14:13:11 -0400 | [diff] [blame] | 3194 | if (test_and_clear_bit(BTRFS_INODE_ORPHAN_META_RESERVED, |
| 3195 | &BTRFS_I(inode)->runtime_flags)) |
Yan, Zheng | d68fc57 | 2010-05-16 10:49:58 -0400 | [diff] [blame] | 3196 | release_rsv = 1; |
Yan, Zheng | d68fc57 | 2010-05-16 10:49:58 -0400 | [diff] [blame] | 3197 | spin_unlock(&root->orphan_lock); |
| 3198 | |
Filipe David Borba Manana | 703c88e | 2013-09-22 21:54:55 +0100 | [diff] [blame] | 3199 | if (delete_item) { |
Josef Bacik | 8a35d95 | 2012-05-23 14:26:42 -0400 | [diff] [blame] | 3200 | atomic_dec(&root->orphan_inodes); |
Filipe David Borba Manana | 703c88e | 2013-09-22 21:54:55 +0100 | [diff] [blame] | 3201 | if (trans) |
| 3202 | ret = btrfs_del_orphan_item(trans, root, |
| 3203 | btrfs_ino(inode)); |
Josef Bacik | 8a35d95 | 2012-05-23 14:26:42 -0400 | [diff] [blame] | 3204 | } |
Josef Bacik | 7b12876 | 2008-07-24 12:17:14 -0400 | [diff] [blame] | 3205 | |
Filipe David Borba Manana | 703c88e | 2013-09-22 21:54:55 +0100 | [diff] [blame] | 3206 | if (release_rsv) |
| 3207 | btrfs_orphan_release_metadata(inode); |
| 3208 | |
Josef Bacik | 4ef31a4 | 2013-08-13 14:10:08 -0400 | [diff] [blame] | 3209 | return ret; |
Josef Bacik | 7b12876 | 2008-07-24 12:17:14 -0400 | [diff] [blame] | 3210 | } |
| 3211 | |
| 3212 | /* |
| 3213 | * this cleans up any orphans that may be left on the list from the last use |
| 3214 | * of this root. |
| 3215 | */ |
Josef Bacik | 66b4ffd | 2011-01-31 16:22:42 -0500 | [diff] [blame] | 3216 | int btrfs_orphan_cleanup(struct btrfs_root *root) |
Josef Bacik | 7b12876 | 2008-07-24 12:17:14 -0400 | [diff] [blame] | 3217 | { |
| 3218 | struct btrfs_path *path; |
| 3219 | struct extent_buffer *leaf; |
Josef Bacik | 7b12876 | 2008-07-24 12:17:14 -0400 | [diff] [blame] | 3220 | struct btrfs_key key, found_key; |
| 3221 | struct btrfs_trans_handle *trans; |
| 3222 | struct inode *inode; |
Josef Bacik | 8f6d7f4 | 2011-09-26 15:55:20 -0400 | [diff] [blame] | 3223 | u64 last_objectid = 0; |
Josef Bacik | 7b12876 | 2008-07-24 12:17:14 -0400 | [diff] [blame] | 3224 | int ret = 0, nr_unlink = 0, nr_truncate = 0; |
| 3225 | |
Yan, Zheng | d68fc57 | 2010-05-16 10:49:58 -0400 | [diff] [blame] | 3226 | if (cmpxchg(&root->orphan_cleanup_state, 0, ORPHAN_CLEANUP_STARTED)) |
Josef Bacik | 66b4ffd | 2011-01-31 16:22:42 -0500 | [diff] [blame] | 3227 | return 0; |
Yan, Zheng | c71bf09 | 2009-11-12 09:34:40 +0000 | [diff] [blame] | 3228 | |
| 3229 | path = btrfs_alloc_path(); |
Josef Bacik | 66b4ffd | 2011-01-31 16:22:42 -0500 | [diff] [blame] | 3230 | if (!path) { |
| 3231 | ret = -ENOMEM; |
| 3232 | goto out; |
| 3233 | } |
Josef Bacik | 7b12876 | 2008-07-24 12:17:14 -0400 | [diff] [blame] | 3234 | path->reada = -1; |
| 3235 | |
| 3236 | key.objectid = BTRFS_ORPHAN_OBJECTID; |
David Sterba | 962a298 | 2014-06-04 18:41:45 +0200 | [diff] [blame] | 3237 | key.type = BTRFS_ORPHAN_ITEM_KEY; |
Josef Bacik | 7b12876 | 2008-07-24 12:17:14 -0400 | [diff] [blame] | 3238 | key.offset = (u64)-1; |
| 3239 | |
Josef Bacik | 7b12876 | 2008-07-24 12:17:14 -0400 | [diff] [blame] | 3240 | while (1) { |
| 3241 | ret = btrfs_search_slot(NULL, root, &key, path, 0, 0); |
Josef Bacik | 66b4ffd | 2011-01-31 16:22:42 -0500 | [diff] [blame] | 3242 | if (ret < 0) |
| 3243 | goto out; |
Josef Bacik | 7b12876 | 2008-07-24 12:17:14 -0400 | [diff] [blame] | 3244 | |
| 3245 | /* |
| 3246 | * 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] | 3247 | * 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] | 3248 | * find the key and see if we have stuff that matches |
| 3249 | */ |
| 3250 | if (ret > 0) { |
Josef Bacik | 66b4ffd | 2011-01-31 16:22:42 -0500 | [diff] [blame] | 3251 | ret = 0; |
Josef Bacik | 7b12876 | 2008-07-24 12:17:14 -0400 | [diff] [blame] | 3252 | if (path->slots[0] == 0) |
| 3253 | break; |
| 3254 | path->slots[0]--; |
| 3255 | } |
| 3256 | |
| 3257 | /* pull out the item */ |
| 3258 | leaf = path->nodes[0]; |
Josef Bacik | 7b12876 | 2008-07-24 12:17:14 -0400 | [diff] [blame] | 3259 | btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]); |
| 3260 | |
| 3261 | /* make sure the item matches what we want */ |
| 3262 | if (found_key.objectid != BTRFS_ORPHAN_OBJECTID) |
| 3263 | break; |
David Sterba | 962a298 | 2014-06-04 18:41:45 +0200 | [diff] [blame] | 3264 | if (found_key.type != BTRFS_ORPHAN_ITEM_KEY) |
Josef Bacik | 7b12876 | 2008-07-24 12:17:14 -0400 | [diff] [blame] | 3265 | break; |
| 3266 | |
| 3267 | /* release the path since we're done with it */ |
David Sterba | b3b4aa7 | 2011-04-21 01:20:15 +0200 | [diff] [blame] | 3268 | btrfs_release_path(path); |
Josef Bacik | 7b12876 | 2008-07-24 12:17:14 -0400 | [diff] [blame] | 3269 | |
| 3270 | /* |
| 3271 | * this is where we are basically btrfs_lookup, without the |
| 3272 | * crossing root thing. we store the inode number in the |
| 3273 | * offset of the orphan item. |
| 3274 | */ |
Josef Bacik | 8f6d7f4 | 2011-09-26 15:55:20 -0400 | [diff] [blame] | 3275 | |
| 3276 | if (found_key.offset == last_objectid) { |
Simon Kirby | c2cf52e | 2013-03-19 22:41:23 +0000 | [diff] [blame] | 3277 | btrfs_err(root->fs_info, |
| 3278 | "Error removing orphan entry, stopping orphan cleanup"); |
Josef Bacik | 8f6d7f4 | 2011-09-26 15:55:20 -0400 | [diff] [blame] | 3279 | ret = -EINVAL; |
| 3280 | goto out; |
| 3281 | } |
| 3282 | |
| 3283 | last_objectid = found_key.offset; |
| 3284 | |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 3285 | found_key.objectid = found_key.offset; |
| 3286 | found_key.type = BTRFS_INODE_ITEM_KEY; |
| 3287 | found_key.offset = 0; |
Josef Bacik | 73f7341 | 2009-12-04 17:38:27 +0000 | [diff] [blame] | 3288 | inode = btrfs_iget(root->fs_info->sb, &found_key, root, NULL); |
Rusty Russell | 8c6ffba | 2013-07-15 11:20:32 +0930 | [diff] [blame] | 3289 | ret = PTR_ERR_OR_ZERO(inode); |
Josef Bacik | a8c9e57 | 2011-09-21 16:55:59 -0400 | [diff] [blame] | 3290 | if (ret && ret != -ESTALE) |
Josef Bacik | 66b4ffd | 2011-01-31 16:22:42 -0500 | [diff] [blame] | 3291 | goto out; |
Josef Bacik | a8c9e57 | 2011-09-21 16:55:59 -0400 | [diff] [blame] | 3292 | |
Arne Jansen | f8e9e0b | 2011-12-14 20:12:02 -0500 | [diff] [blame] | 3293 | if (ret == -ESTALE && root == root->fs_info->tree_root) { |
| 3294 | struct btrfs_root *dead_root; |
| 3295 | struct btrfs_fs_info *fs_info = root->fs_info; |
| 3296 | int is_dead_root = 0; |
| 3297 | |
| 3298 | /* |
| 3299 | * this is an orphan in the tree root. Currently these |
| 3300 | * could come from 2 sources: |
| 3301 | * a) a snapshot deletion in progress |
| 3302 | * b) a free space cache inode |
| 3303 | * We need to distinguish those two, as the snapshot |
| 3304 | * orphan must not get deleted. |
| 3305 | * find_dead_roots already ran before us, so if this |
| 3306 | * is a snapshot deletion, we should find the root |
| 3307 | * in the dead_roots list |
| 3308 | */ |
| 3309 | spin_lock(&fs_info->trans_lock); |
| 3310 | list_for_each_entry(dead_root, &fs_info->dead_roots, |
| 3311 | root_list) { |
| 3312 | if (dead_root->root_key.objectid == |
| 3313 | found_key.objectid) { |
| 3314 | is_dead_root = 1; |
| 3315 | break; |
| 3316 | } |
| 3317 | } |
| 3318 | spin_unlock(&fs_info->trans_lock); |
| 3319 | if (is_dead_root) { |
| 3320 | /* prevent this orphan from being found again */ |
| 3321 | key.offset = found_key.objectid - 1; |
| 3322 | continue; |
| 3323 | } |
| 3324 | } |
Josef Bacik | a8c9e57 | 2011-09-21 16:55:59 -0400 | [diff] [blame] | 3325 | /* |
| 3326 | * Inode is already gone but the orphan item is still there, |
| 3327 | * kill the orphan item. |
| 3328 | */ |
| 3329 | if (ret == -ESTALE) { |
| 3330 | trans = btrfs_start_transaction(root, 1); |
| 3331 | if (IS_ERR(trans)) { |
| 3332 | ret = PTR_ERR(trans); |
| 3333 | goto out; |
| 3334 | } |
Simon Kirby | c2cf52e | 2013-03-19 22:41:23 +0000 | [diff] [blame] | 3335 | btrfs_debug(root->fs_info, "auto deleting %Lu", |
| 3336 | found_key.objectid); |
Josef Bacik | a8c9e57 | 2011-09-21 16:55:59 -0400 | [diff] [blame] | 3337 | ret = btrfs_del_orphan_item(trans, root, |
| 3338 | found_key.objectid); |
Josef Bacik | a8c9e57 | 2011-09-21 16:55:59 -0400 | [diff] [blame] | 3339 | btrfs_end_transaction(trans, root); |
Josef Bacik | 4ef31a4 | 2013-08-13 14:10:08 -0400 | [diff] [blame] | 3340 | if (ret) |
| 3341 | goto out; |
Josef Bacik | a8c9e57 | 2011-09-21 16:55:59 -0400 | [diff] [blame] | 3342 | continue; |
Josef Bacik | 66b4ffd | 2011-01-31 16:22:42 -0500 | [diff] [blame] | 3343 | } |
Josef Bacik | 7b12876 | 2008-07-24 12:17:14 -0400 | [diff] [blame] | 3344 | |
Josef Bacik | 7b12876 | 2008-07-24 12:17:14 -0400 | [diff] [blame] | 3345 | /* |
| 3346 | * add this inode to the orphan list so btrfs_orphan_del does |
| 3347 | * the proper thing when we hit it |
| 3348 | */ |
Josef Bacik | 8a35d95 | 2012-05-23 14:26:42 -0400 | [diff] [blame] | 3349 | set_bit(BTRFS_INODE_HAS_ORPHAN_ITEM, |
| 3350 | &BTRFS_I(inode)->runtime_flags); |
Josef Bacik | 925396e | 2013-02-01 15:57:47 -0500 | [diff] [blame] | 3351 | atomic_inc(&root->orphan_inodes); |
Josef Bacik | 7b12876 | 2008-07-24 12:17:14 -0400 | [diff] [blame] | 3352 | |
Josef Bacik | 7b12876 | 2008-07-24 12:17:14 -0400 | [diff] [blame] | 3353 | /* if we have links, this was a truncate, lets do that */ |
| 3354 | if (inode->i_nlink) { |
Dulshani Gunawardhana | fae7f21 | 2013-10-31 10:30:08 +0530 | [diff] [blame] | 3355 | if (WARN_ON(!S_ISREG(inode->i_mode))) { |
Josef Bacik | a41ad39 | 2011-01-31 15:30:16 -0500 | [diff] [blame] | 3356 | iput(inode); |
| 3357 | continue; |
| 3358 | } |
Josef Bacik | 7b12876 | 2008-07-24 12:17:14 -0400 | [diff] [blame] | 3359 | nr_truncate++; |
Josef Bacik | f3fe820 | 2013-01-07 17:03:21 -0500 | [diff] [blame] | 3360 | |
| 3361 | /* 1 for the orphan item deletion. */ |
| 3362 | trans = btrfs_start_transaction(root, 1); |
| 3363 | if (IS_ERR(trans)) { |
Josef Bacik | c69b26b | 2013-06-03 16:51:23 -0400 | [diff] [blame] | 3364 | iput(inode); |
Josef Bacik | f3fe820 | 2013-01-07 17:03:21 -0500 | [diff] [blame] | 3365 | ret = PTR_ERR(trans); |
| 3366 | goto out; |
| 3367 | } |
| 3368 | ret = btrfs_orphan_add(trans, inode); |
| 3369 | btrfs_end_transaction(trans, root); |
Josef Bacik | c69b26b | 2013-06-03 16:51:23 -0400 | [diff] [blame] | 3370 | if (ret) { |
| 3371 | iput(inode); |
Josef Bacik | f3fe820 | 2013-01-07 17:03:21 -0500 | [diff] [blame] | 3372 | goto out; |
Josef Bacik | c69b26b | 2013-06-03 16:51:23 -0400 | [diff] [blame] | 3373 | } |
Josef Bacik | f3fe820 | 2013-01-07 17:03:21 -0500 | [diff] [blame] | 3374 | |
Josef Bacik | 66b4ffd | 2011-01-31 16:22:42 -0500 | [diff] [blame] | 3375 | ret = btrfs_truncate(inode); |
Josef Bacik | 4a7d0f6 | 2013-02-07 16:27:28 -0500 | [diff] [blame] | 3376 | if (ret) |
| 3377 | btrfs_orphan_del(NULL, inode); |
Josef Bacik | 7b12876 | 2008-07-24 12:17:14 -0400 | [diff] [blame] | 3378 | } else { |
| 3379 | nr_unlink++; |
| 3380 | } |
| 3381 | |
| 3382 | /* this will do delete_inode and everything for us */ |
| 3383 | iput(inode); |
Josef Bacik | 66b4ffd | 2011-01-31 16:22:42 -0500 | [diff] [blame] | 3384 | if (ret) |
| 3385 | goto out; |
Josef Bacik | 7b12876 | 2008-07-24 12:17:14 -0400 | [diff] [blame] | 3386 | } |
Miao Xie | 3254c87 | 2011-11-10 20:45:05 -0500 | [diff] [blame] | 3387 | /* release the path since we're done with it */ |
| 3388 | btrfs_release_path(path); |
| 3389 | |
Yan, Zheng | d68fc57 | 2010-05-16 10:49:58 -0400 | [diff] [blame] | 3390 | root->orphan_cleanup_state = ORPHAN_CLEANUP_DONE; |
| 3391 | |
| 3392 | if (root->orphan_block_rsv) |
| 3393 | btrfs_block_rsv_release(root, root->orphan_block_rsv, |
| 3394 | (u64)-1); |
| 3395 | |
Miao Xie | 27cdeb7 | 2014-04-02 19:51:05 +0800 | [diff] [blame] | 3396 | if (root->orphan_block_rsv || |
| 3397 | test_bit(BTRFS_ROOT_ORPHAN_ITEM_INSERTED, &root->state)) { |
Josef Bacik | 7a7eaa4 | 2011-04-13 12:54:33 -0400 | [diff] [blame] | 3398 | trans = btrfs_join_transaction(root); |
Josef Bacik | 66b4ffd | 2011-01-31 16:22:42 -0500 | [diff] [blame] | 3399 | if (!IS_ERR(trans)) |
| 3400 | btrfs_end_transaction(trans, root); |
Yan, Zheng | d68fc57 | 2010-05-16 10:49:58 -0400 | [diff] [blame] | 3401 | } |
Josef Bacik | 7b12876 | 2008-07-24 12:17:14 -0400 | [diff] [blame] | 3402 | |
| 3403 | if (nr_unlink) |
David Sterba | 4884b47 | 2013-03-20 13:31:27 +0000 | [diff] [blame] | 3404 | btrfs_debug(root->fs_info, "unlinked %d orphans", nr_unlink); |
Josef Bacik | 7b12876 | 2008-07-24 12:17:14 -0400 | [diff] [blame] | 3405 | if (nr_truncate) |
David Sterba | 4884b47 | 2013-03-20 13:31:27 +0000 | [diff] [blame] | 3406 | btrfs_debug(root->fs_info, "truncated %d orphans", nr_truncate); |
Josef Bacik | 66b4ffd | 2011-01-31 16:22:42 -0500 | [diff] [blame] | 3407 | |
| 3408 | out: |
| 3409 | if (ret) |
Simon Kirby | c2cf52e | 2013-03-19 22:41:23 +0000 | [diff] [blame] | 3410 | btrfs_crit(root->fs_info, |
| 3411 | "could not do orphan cleanup %d", ret); |
Josef Bacik | 66b4ffd | 2011-01-31 16:22:42 -0500 | [diff] [blame] | 3412 | btrfs_free_path(path); |
| 3413 | return ret; |
Josef Bacik | 7b12876 | 2008-07-24 12:17:14 -0400 | [diff] [blame] | 3414 | } |
| 3415 | |
Chris Mason | d352ac6 | 2008-09-29 15:18:18 -0400 | [diff] [blame] | 3416 | /* |
Chris Mason | 46a53cc | 2009-04-27 11:47:50 -0400 | [diff] [blame] | 3417 | * very simple check to peek ahead in the leaf looking for xattrs. If we |
| 3418 | * don't find any xattrs, we know there can't be any acls. |
| 3419 | * |
| 3420 | * slot is the slot the inode is in, objectid is the objectid of the inode |
| 3421 | */ |
| 3422 | 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] | 3423 | int slot, u64 objectid, |
| 3424 | int *first_xattr_slot) |
Chris Mason | 46a53cc | 2009-04-27 11:47:50 -0400 | [diff] [blame] | 3425 | { |
| 3426 | u32 nritems = btrfs_header_nritems(leaf); |
| 3427 | struct btrfs_key found_key; |
Josef Bacik | f23b5a5 | 2013-06-19 10:16:26 -0400 | [diff] [blame] | 3428 | static u64 xattr_access = 0; |
| 3429 | static u64 xattr_default = 0; |
Chris Mason | 46a53cc | 2009-04-27 11:47:50 -0400 | [diff] [blame] | 3430 | int scanned = 0; |
| 3431 | |
Josef Bacik | f23b5a5 | 2013-06-19 10:16:26 -0400 | [diff] [blame] | 3432 | if (!xattr_access) { |
| 3433 | xattr_access = btrfs_name_hash(POSIX_ACL_XATTR_ACCESS, |
| 3434 | strlen(POSIX_ACL_XATTR_ACCESS)); |
| 3435 | xattr_default = btrfs_name_hash(POSIX_ACL_XATTR_DEFAULT, |
| 3436 | strlen(POSIX_ACL_XATTR_DEFAULT)); |
| 3437 | } |
| 3438 | |
Chris Mason | 46a53cc | 2009-04-27 11:47:50 -0400 | [diff] [blame] | 3439 | slot++; |
Filipe David Borba Manana | 6354192 | 2014-01-07 11:47:46 +0000 | [diff] [blame] | 3440 | *first_xattr_slot = -1; |
Chris Mason | 46a53cc | 2009-04-27 11:47:50 -0400 | [diff] [blame] | 3441 | while (slot < nritems) { |
| 3442 | btrfs_item_key_to_cpu(leaf, &found_key, slot); |
| 3443 | |
| 3444 | /* we found a different objectid, there must not be acls */ |
| 3445 | if (found_key.objectid != objectid) |
| 3446 | return 0; |
| 3447 | |
| 3448 | /* we found an xattr, assume we've got an acl */ |
Josef Bacik | f23b5a5 | 2013-06-19 10:16:26 -0400 | [diff] [blame] | 3449 | if (found_key.type == BTRFS_XATTR_ITEM_KEY) { |
Filipe David Borba Manana | 6354192 | 2014-01-07 11:47:46 +0000 | [diff] [blame] | 3450 | if (*first_xattr_slot == -1) |
| 3451 | *first_xattr_slot = slot; |
Josef Bacik | f23b5a5 | 2013-06-19 10:16:26 -0400 | [diff] [blame] | 3452 | if (found_key.offset == xattr_access || |
| 3453 | found_key.offset == xattr_default) |
| 3454 | return 1; |
| 3455 | } |
Chris Mason | 46a53cc | 2009-04-27 11:47:50 -0400 | [diff] [blame] | 3456 | |
| 3457 | /* |
| 3458 | * we found a key greater than an xattr key, there can't |
| 3459 | * be any acls later on |
| 3460 | */ |
| 3461 | if (found_key.type > BTRFS_XATTR_ITEM_KEY) |
| 3462 | return 0; |
| 3463 | |
| 3464 | slot++; |
| 3465 | scanned++; |
| 3466 | |
| 3467 | /* |
| 3468 | * it goes inode, inode backrefs, xattrs, extents, |
| 3469 | * so if there are a ton of hard links to an inode there can |
| 3470 | * be a lot of backrefs. Don't waste time searching too hard, |
| 3471 | * this is just an optimization |
| 3472 | */ |
| 3473 | if (scanned >= 8) |
| 3474 | break; |
| 3475 | } |
| 3476 | /* we hit the end of the leaf before we found an xattr or |
| 3477 | * something larger than an xattr. We have to assume the inode |
| 3478 | * has acls |
| 3479 | */ |
Filipe David Borba Manana | 6354192 | 2014-01-07 11:47:46 +0000 | [diff] [blame] | 3480 | if (*first_xattr_slot == -1) |
| 3481 | *first_xattr_slot = slot; |
Chris Mason | 46a53cc | 2009-04-27 11:47:50 -0400 | [diff] [blame] | 3482 | return 1; |
| 3483 | } |
| 3484 | |
| 3485 | /* |
Chris Mason | d352ac6 | 2008-09-29 15:18:18 -0400 | [diff] [blame] | 3486 | * read an inode from the btree into the in-memory inode |
| 3487 | */ |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 3488 | static void btrfs_read_locked_inode(struct inode *inode) |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3489 | { |
| 3490 | struct btrfs_path *path; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 3491 | struct extent_buffer *leaf; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3492 | struct btrfs_inode_item *inode_item; |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 3493 | struct btrfs_timespec *tspec; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3494 | struct btrfs_root *root = BTRFS_I(inode)->root; |
| 3495 | struct btrfs_key location; |
Miao Xie | 67de117 | 2013-12-26 13:07:06 +0800 | [diff] [blame] | 3496 | unsigned long ptr; |
Chris Mason | 46a53cc | 2009-04-27 11:47:50 -0400 | [diff] [blame] | 3497 | int maybe_acls; |
Josef Bacik | 618e21d | 2007-07-11 10:18:17 -0400 | [diff] [blame] | 3498 | u32 rdev; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3499 | int ret; |
Miao Xie | 2f7e33d | 2011-06-23 07:27:13 +0000 | [diff] [blame] | 3500 | bool filled = false; |
Filipe David Borba Manana | 6354192 | 2014-01-07 11:47:46 +0000 | [diff] [blame] | 3501 | int first_xattr_slot; |
Miao Xie | 2f7e33d | 2011-06-23 07:27:13 +0000 | [diff] [blame] | 3502 | |
| 3503 | ret = btrfs_fill_inode(inode, &rdev); |
| 3504 | if (!ret) |
| 3505 | filled = true; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3506 | |
| 3507 | path = btrfs_alloc_path(); |
Mark Fasheh | 1748f84 | 2011-07-12 11:25:31 -0700 | [diff] [blame] | 3508 | if (!path) |
| 3509 | goto make_bad; |
| 3510 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3511 | memcpy(&location, &BTRFS_I(inode)->location, sizeof(location)); |
Chris Mason | dc17ff8 | 2008-01-08 15:46:30 -0500 | [diff] [blame] | 3512 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3513 | ret = btrfs_lookup_inode(NULL, root, path, &location, 0); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 3514 | if (ret) |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3515 | goto make_bad; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3516 | |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 3517 | leaf = path->nodes[0]; |
Miao Xie | 2f7e33d | 2011-06-23 07:27:13 +0000 | [diff] [blame] | 3518 | |
| 3519 | if (filled) |
Miao Xie | 67de117 | 2013-12-26 13:07:06 +0800 | [diff] [blame] | 3520 | goto cache_index; |
Miao Xie | 2f7e33d | 2011-06-23 07:27:13 +0000 | [diff] [blame] | 3521 | |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 3522 | inode_item = btrfs_item_ptr(leaf, path->slots[0], |
| 3523 | struct btrfs_inode_item); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 3524 | inode->i_mode = btrfs_inode_mode(leaf, inode_item); |
Miklos Szeredi | bfe8684 | 2011-10-28 14:13:29 +0200 | [diff] [blame] | 3525 | set_nlink(inode, btrfs_inode_nlink(leaf, inode_item)); |
Eric W. Biederman | 2f2f43d | 2012-02-10 11:05:07 -0800 | [diff] [blame] | 3526 | i_uid_write(inode, btrfs_inode_uid(leaf, inode_item)); |
| 3527 | i_gid_write(inode, btrfs_inode_gid(leaf, inode_item)); |
Chris Mason | dbe674a | 2008-07-17 12:54:05 -0400 | [diff] [blame] | 3528 | btrfs_i_size_write(inode, btrfs_inode_size(leaf, inode_item)); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 3529 | |
| 3530 | tspec = btrfs_inode_atime(inode_item); |
| 3531 | inode->i_atime.tv_sec = btrfs_timespec_sec(leaf, tspec); |
| 3532 | inode->i_atime.tv_nsec = btrfs_timespec_nsec(leaf, tspec); |
| 3533 | |
| 3534 | tspec = btrfs_inode_mtime(inode_item); |
| 3535 | inode->i_mtime.tv_sec = btrfs_timespec_sec(leaf, tspec); |
| 3536 | inode->i_mtime.tv_nsec = btrfs_timespec_nsec(leaf, tspec); |
| 3537 | |
| 3538 | tspec = btrfs_inode_ctime(inode_item); |
| 3539 | inode->i_ctime.tv_sec = btrfs_timespec_sec(leaf, tspec); |
| 3540 | inode->i_ctime.tv_nsec = btrfs_timespec_nsec(leaf, tspec); |
| 3541 | |
Yan Zheng | a76a3cd | 2008-10-09 11:46:29 -0400 | [diff] [blame] | 3542 | inode_set_bytes(inode, btrfs_inode_nbytes(leaf, inode_item)); |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 3543 | BTRFS_I(inode)->generation = btrfs_inode_generation(leaf, inode_item); |
Josef Bacik | 5dc562c | 2012-08-17 13:14:17 -0400 | [diff] [blame] | 3544 | BTRFS_I(inode)->last_trans = btrfs_inode_transid(leaf, inode_item); |
| 3545 | |
| 3546 | /* |
| 3547 | * If we were modified in the current generation and evicted from memory |
| 3548 | * and then re-read we need to do a full sync since we don't have any |
| 3549 | * idea about which extents were modified before we were evicted from |
| 3550 | * cache. |
| 3551 | */ |
| 3552 | if (BTRFS_I(inode)->last_trans == root->fs_info->generation) |
| 3553 | set_bit(BTRFS_INODE_NEEDS_FULL_SYNC, |
| 3554 | &BTRFS_I(inode)->runtime_flags); |
| 3555 | |
Josef Bacik | 0c4d2d9 | 2012-04-05 15:03:02 -0400 | [diff] [blame] | 3556 | inode->i_version = btrfs_inode_sequence(leaf, inode_item); |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 3557 | inode->i_generation = BTRFS_I(inode)->generation; |
Josef Bacik | 618e21d | 2007-07-11 10:18:17 -0400 | [diff] [blame] | 3558 | inode->i_rdev = 0; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 3559 | rdev = btrfs_inode_rdev(leaf, inode_item); |
| 3560 | |
Josef Bacik | aec7477 | 2008-07-24 12:12:38 -0400 | [diff] [blame] | 3561 | BTRFS_I(inode)->index_cnt = (u64)-1; |
Yan Zheng | d2fb343 | 2008-12-11 16:30:39 -0500 | [diff] [blame] | 3562 | BTRFS_I(inode)->flags = btrfs_inode_flags(leaf, inode_item); |
Miao Xie | 67de117 | 2013-12-26 13:07:06 +0800 | [diff] [blame] | 3563 | |
| 3564 | cache_index: |
| 3565 | path->slots[0]++; |
| 3566 | if (inode->i_nlink != 1 || |
| 3567 | path->slots[0] >= btrfs_header_nritems(leaf)) |
| 3568 | goto cache_acl; |
| 3569 | |
| 3570 | btrfs_item_key_to_cpu(leaf, &location, path->slots[0]); |
| 3571 | if (location.objectid != btrfs_ino(inode)) |
| 3572 | goto cache_acl; |
| 3573 | |
| 3574 | ptr = btrfs_item_ptr_offset(leaf, path->slots[0]); |
| 3575 | if (location.type == BTRFS_INODE_REF_KEY) { |
| 3576 | struct btrfs_inode_ref *ref; |
| 3577 | |
| 3578 | ref = (struct btrfs_inode_ref *)ptr; |
| 3579 | BTRFS_I(inode)->dir_index = btrfs_inode_ref_index(leaf, ref); |
| 3580 | } else if (location.type == BTRFS_INODE_EXTREF_KEY) { |
| 3581 | struct btrfs_inode_extref *extref; |
| 3582 | |
| 3583 | extref = (struct btrfs_inode_extref *)ptr; |
| 3584 | BTRFS_I(inode)->dir_index = btrfs_inode_extref_index(leaf, |
| 3585 | extref); |
| 3586 | } |
Miao Xie | 2f7e33d | 2011-06-23 07:27:13 +0000 | [diff] [blame] | 3587 | cache_acl: |
Chris Mason | 46a53cc | 2009-04-27 11:47:50 -0400 | [diff] [blame] | 3588 | /* |
| 3589 | * try to precache a NULL acl entry for files that don't have |
| 3590 | * any xattrs or acls |
| 3591 | */ |
Li Zefan | 33345d01 | 2011-04-20 10:31:50 +0800 | [diff] [blame] | 3592 | maybe_acls = acls_after_inode_item(leaf, path->slots[0], |
Filipe David Borba Manana | 6354192 | 2014-01-07 11:47:46 +0000 | [diff] [blame] | 3593 | btrfs_ino(inode), &first_xattr_slot); |
| 3594 | if (first_xattr_slot != -1) { |
| 3595 | path->slots[0] = first_xattr_slot; |
| 3596 | ret = btrfs_load_inode_props(inode, path); |
| 3597 | if (ret) |
| 3598 | btrfs_err(root->fs_info, |
David Sterba | 351fd35 | 2014-05-15 16:48:20 +0200 | [diff] [blame] | 3599 | "error loading props for ino %llu (root %llu): %d", |
Filipe David Borba Manana | 6354192 | 2014-01-07 11:47:46 +0000 | [diff] [blame] | 3600 | btrfs_ino(inode), |
| 3601 | root->root_key.objectid, ret); |
| 3602 | } |
| 3603 | btrfs_free_path(path); |
| 3604 | |
Al Viro | 72c0490 | 2009-06-24 16:58:48 -0400 | [diff] [blame] | 3605 | if (!maybe_acls) |
| 3606 | cache_no_acl(inode); |
Chris Mason | 46a53cc | 2009-04-27 11:47:50 -0400 | [diff] [blame] | 3607 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3608 | switch (inode->i_mode & S_IFMT) { |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3609 | case S_IFREG: |
| 3610 | inode->i_mapping->a_ops = &btrfs_aops; |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 3611 | BTRFS_I(inode)->io_tree.ops = &btrfs_extent_io_ops; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3612 | inode->i_fop = &btrfs_file_operations; |
| 3613 | inode->i_op = &btrfs_file_inode_operations; |
| 3614 | break; |
| 3615 | case S_IFDIR: |
| 3616 | inode->i_fop = &btrfs_dir_file_operations; |
| 3617 | if (root == root->fs_info->tree_root) |
| 3618 | inode->i_op = &btrfs_dir_ro_inode_operations; |
| 3619 | else |
| 3620 | inode->i_op = &btrfs_dir_inode_operations; |
| 3621 | break; |
| 3622 | case S_IFLNK: |
| 3623 | inode->i_op = &btrfs_symlink_inode_operations; |
| 3624 | inode->i_mapping->a_ops = &btrfs_symlink_aops; |
| 3625 | break; |
Josef Bacik | 618e21d | 2007-07-11 10:18:17 -0400 | [diff] [blame] | 3626 | default: |
Jim Owens | 0279b4c | 2009-02-04 09:29:13 -0500 | [diff] [blame] | 3627 | inode->i_op = &btrfs_special_inode_operations; |
Josef Bacik | 618e21d | 2007-07-11 10:18:17 -0400 | [diff] [blame] | 3628 | init_special_inode(inode, inode->i_mode, rdev); |
| 3629 | break; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3630 | } |
Christoph Hellwig | 6cbff00 | 2009-04-17 10:37:41 +0200 | [diff] [blame] | 3631 | |
| 3632 | btrfs_update_iflags(inode); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3633 | return; |
| 3634 | |
| 3635 | make_bad: |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3636 | btrfs_free_path(path); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3637 | make_bad_inode(inode); |
| 3638 | } |
| 3639 | |
Chris Mason | d352ac6 | 2008-09-29 15:18:18 -0400 | [diff] [blame] | 3640 | /* |
| 3641 | * given a leaf and an inode, copy the inode fields into the leaf |
| 3642 | */ |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 3643 | static void fill_inode_item(struct btrfs_trans_handle *trans, |
| 3644 | struct extent_buffer *leaf, |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 3645 | struct btrfs_inode_item *item, |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3646 | struct inode *inode) |
| 3647 | { |
Liu Bo | 51fab69 | 2012-12-27 09:01:21 +0000 | [diff] [blame] | 3648 | struct btrfs_map_token token; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 3649 | |
Liu Bo | 51fab69 | 2012-12-27 09:01:21 +0000 | [diff] [blame] | 3650 | btrfs_init_map_token(&token); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 3651 | |
Liu Bo | 51fab69 | 2012-12-27 09:01:21 +0000 | [diff] [blame] | 3652 | btrfs_set_token_inode_uid(leaf, item, i_uid_read(inode), &token); |
| 3653 | btrfs_set_token_inode_gid(leaf, item, i_gid_read(inode), &token); |
| 3654 | btrfs_set_token_inode_size(leaf, item, BTRFS_I(inode)->disk_i_size, |
| 3655 | &token); |
| 3656 | btrfs_set_token_inode_mode(leaf, item, inode->i_mode, &token); |
| 3657 | btrfs_set_token_inode_nlink(leaf, item, inode->i_nlink, &token); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 3658 | |
Liu Bo | 51fab69 | 2012-12-27 09:01:21 +0000 | [diff] [blame] | 3659 | btrfs_set_token_timespec_sec(leaf, btrfs_inode_atime(item), |
| 3660 | inode->i_atime.tv_sec, &token); |
| 3661 | btrfs_set_token_timespec_nsec(leaf, btrfs_inode_atime(item), |
| 3662 | inode->i_atime.tv_nsec, &token); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 3663 | |
Liu Bo | 51fab69 | 2012-12-27 09:01:21 +0000 | [diff] [blame] | 3664 | btrfs_set_token_timespec_sec(leaf, btrfs_inode_mtime(item), |
| 3665 | inode->i_mtime.tv_sec, &token); |
| 3666 | btrfs_set_token_timespec_nsec(leaf, btrfs_inode_mtime(item), |
| 3667 | inode->i_mtime.tv_nsec, &token); |
| 3668 | |
| 3669 | btrfs_set_token_timespec_sec(leaf, btrfs_inode_ctime(item), |
| 3670 | inode->i_ctime.tv_sec, &token); |
| 3671 | btrfs_set_token_timespec_nsec(leaf, btrfs_inode_ctime(item), |
| 3672 | inode->i_ctime.tv_nsec, &token); |
| 3673 | |
| 3674 | btrfs_set_token_inode_nbytes(leaf, item, inode_get_bytes(inode), |
| 3675 | &token); |
| 3676 | btrfs_set_token_inode_generation(leaf, item, BTRFS_I(inode)->generation, |
| 3677 | &token); |
| 3678 | btrfs_set_token_inode_sequence(leaf, item, inode->i_version, &token); |
| 3679 | btrfs_set_token_inode_transid(leaf, item, trans->transid, &token); |
| 3680 | btrfs_set_token_inode_rdev(leaf, item, inode->i_rdev, &token); |
| 3681 | btrfs_set_token_inode_flags(leaf, item, BTRFS_I(inode)->flags, &token); |
| 3682 | btrfs_set_token_inode_block_group(leaf, item, 0, &token); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3683 | } |
| 3684 | |
Chris Mason | d352ac6 | 2008-09-29 15:18:18 -0400 | [diff] [blame] | 3685 | /* |
| 3686 | * copy everything in the in-memory inode into the btree. |
| 3687 | */ |
Chris Mason | 2115133 | 2011-11-10 20:39:08 -0500 | [diff] [blame] | 3688 | static noinline int btrfs_update_inode_item(struct btrfs_trans_handle *trans, |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 3689 | struct btrfs_root *root, struct inode *inode) |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3690 | { |
| 3691 | struct btrfs_inode_item *inode_item; |
| 3692 | struct btrfs_path *path; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 3693 | struct extent_buffer *leaf; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3694 | int ret; |
| 3695 | |
| 3696 | path = btrfs_alloc_path(); |
Miao Xie | 16cdcec | 2011-04-22 18:12:22 +0800 | [diff] [blame] | 3697 | if (!path) |
| 3698 | return -ENOMEM; |
| 3699 | |
Chris Mason | b947343 | 2009-03-13 11:00:37 -0400 | [diff] [blame] | 3700 | path->leave_spinning = 1; |
Miao Xie | 16cdcec | 2011-04-22 18:12:22 +0800 | [diff] [blame] | 3701 | ret = btrfs_lookup_inode(trans, root, path, &BTRFS_I(inode)->location, |
| 3702 | 1); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3703 | if (ret) { |
| 3704 | if (ret > 0) |
| 3705 | ret = -ENOENT; |
| 3706 | goto failed; |
| 3707 | } |
| 3708 | |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 3709 | leaf = path->nodes[0]; |
| 3710 | inode_item = btrfs_item_ptr(leaf, path->slots[0], |
Miao Xie | 16cdcec | 2011-04-22 18:12:22 +0800 | [diff] [blame] | 3711 | struct btrfs_inode_item); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3712 | |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 3713 | fill_inode_item(trans, leaf, inode_item, inode); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 3714 | btrfs_mark_buffer_dirty(leaf); |
Josef Bacik | 15ee9bc | 2007-08-10 16:22:09 -0400 | [diff] [blame] | 3715 | btrfs_set_inode_last_trans(trans, inode); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3716 | ret = 0; |
| 3717 | failed: |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3718 | btrfs_free_path(path); |
| 3719 | return ret; |
| 3720 | } |
| 3721 | |
Chris Mason | d352ac6 | 2008-09-29 15:18:18 -0400 | [diff] [blame] | 3722 | /* |
Chris Mason | 2115133 | 2011-11-10 20:39:08 -0500 | [diff] [blame] | 3723 | * copy everything in the in-memory inode into the btree. |
| 3724 | */ |
| 3725 | noinline int btrfs_update_inode(struct btrfs_trans_handle *trans, |
| 3726 | struct btrfs_root *root, struct inode *inode) |
| 3727 | { |
| 3728 | int ret; |
| 3729 | |
| 3730 | /* |
| 3731 | * If the inode is a free space inode, we can deadlock during commit |
| 3732 | * if we put it into the delayed code. |
| 3733 | * |
| 3734 | * The data relocation inode should also be directly updated |
| 3735 | * without delay |
| 3736 | */ |
Liu Bo | 83eea1f | 2012-07-10 05:28:39 -0600 | [diff] [blame] | 3737 | if (!btrfs_is_free_space_inode(inode) |
Josef Bacik | 1d52c78 | 2014-09-18 11:30:44 -0400 | [diff] [blame] | 3738 | && root->root_key.objectid != BTRFS_DATA_RELOC_TREE_OBJECTID |
| 3739 | && !root->fs_info->log_root_recovering) { |
Alexander Block | 8ea05e3 | 2012-07-25 17:35:53 +0200 | [diff] [blame] | 3740 | btrfs_update_root_times(trans, root); |
| 3741 | |
Chris Mason | 2115133 | 2011-11-10 20:39:08 -0500 | [diff] [blame] | 3742 | ret = btrfs_delayed_update_inode(trans, root, inode); |
| 3743 | if (!ret) |
| 3744 | btrfs_set_inode_last_trans(trans, inode); |
| 3745 | return ret; |
| 3746 | } |
| 3747 | |
| 3748 | return btrfs_update_inode_item(trans, root, inode); |
| 3749 | } |
| 3750 | |
Josef Bacik | be6aef6 | 2012-10-22 15:43:12 -0400 | [diff] [blame] | 3751 | noinline int btrfs_update_inode_fallback(struct btrfs_trans_handle *trans, |
| 3752 | struct btrfs_root *root, |
| 3753 | struct inode *inode) |
Chris Mason | 2115133 | 2011-11-10 20:39:08 -0500 | [diff] [blame] | 3754 | { |
| 3755 | int ret; |
| 3756 | |
| 3757 | ret = btrfs_update_inode(trans, root, inode); |
| 3758 | if (ret == -ENOSPC) |
| 3759 | return btrfs_update_inode_item(trans, root, inode); |
| 3760 | return ret; |
| 3761 | } |
| 3762 | |
| 3763 | /* |
Chris Mason | d352ac6 | 2008-09-29 15:18:18 -0400 | [diff] [blame] | 3764 | * unlink helper that gets used here in inode.c and in the tree logging |
| 3765 | * recovery code. It remove a link in a directory with a given name, and |
| 3766 | * also drops the back refs in the inode to the directory |
| 3767 | */ |
Al Viro | 9298679 | 2011-03-04 17:14:37 +0000 | [diff] [blame] | 3768 | static int __btrfs_unlink_inode(struct btrfs_trans_handle *trans, |
| 3769 | struct btrfs_root *root, |
| 3770 | struct inode *dir, struct inode *inode, |
| 3771 | const char *name, int name_len) |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3772 | { |
| 3773 | struct btrfs_path *path; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3774 | int ret = 0; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 3775 | struct extent_buffer *leaf; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3776 | struct btrfs_dir_item *di; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 3777 | struct btrfs_key key; |
Josef Bacik | aec7477 | 2008-07-24 12:12:38 -0400 | [diff] [blame] | 3778 | u64 index; |
Li Zefan | 33345d01 | 2011-04-20 10:31:50 +0800 | [diff] [blame] | 3779 | u64 ino = btrfs_ino(inode); |
| 3780 | u64 dir_ino = btrfs_ino(dir); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3781 | |
| 3782 | path = btrfs_alloc_path(); |
Chris Mason | 54aa1f4 | 2007-06-22 14:16:25 -0400 | [diff] [blame] | 3783 | if (!path) { |
| 3784 | ret = -ENOMEM; |
Tsutomu Itoh | 554233a | 2011-02-03 03:16:25 +0000 | [diff] [blame] | 3785 | goto out; |
Chris Mason | 54aa1f4 | 2007-06-22 14:16:25 -0400 | [diff] [blame] | 3786 | } |
| 3787 | |
Chris Mason | b947343 | 2009-03-13 11:00:37 -0400 | [diff] [blame] | 3788 | path->leave_spinning = 1; |
Li Zefan | 33345d01 | 2011-04-20 10:31:50 +0800 | [diff] [blame] | 3789 | di = btrfs_lookup_dir_item(trans, root, path, dir_ino, |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3790 | name, name_len, -1); |
| 3791 | if (IS_ERR(di)) { |
| 3792 | ret = PTR_ERR(di); |
| 3793 | goto err; |
| 3794 | } |
| 3795 | if (!di) { |
| 3796 | ret = -ENOENT; |
| 3797 | goto err; |
| 3798 | } |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 3799 | leaf = path->nodes[0]; |
| 3800 | btrfs_dir_item_key_to_cpu(leaf, di, &key); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3801 | ret = btrfs_delete_one_dir_name(trans, root, path, di); |
Chris Mason | 54aa1f4 | 2007-06-22 14:16:25 -0400 | [diff] [blame] | 3802 | if (ret) |
| 3803 | goto err; |
David Sterba | b3b4aa7 | 2011-04-21 01:20:15 +0200 | [diff] [blame] | 3804 | btrfs_release_path(path); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3805 | |
Miao Xie | 67de117 | 2013-12-26 13:07:06 +0800 | [diff] [blame] | 3806 | /* |
| 3807 | * If we don't have dir index, we have to get it by looking up |
| 3808 | * the inode ref, since we get the inode ref, remove it directly, |
| 3809 | * it is unnecessary to do delayed deletion. |
| 3810 | * |
| 3811 | * But if we have dir index, needn't search inode ref to get it. |
| 3812 | * Since the inode ref is close to the inode item, it is better |
| 3813 | * that we delay to delete it, and just do this deletion when |
| 3814 | * we update the inode item. |
| 3815 | */ |
| 3816 | if (BTRFS_I(inode)->dir_index) { |
| 3817 | ret = btrfs_delayed_delete_inode_ref(inode); |
| 3818 | if (!ret) { |
| 3819 | index = BTRFS_I(inode)->dir_index; |
| 3820 | goto skip_backref; |
| 3821 | } |
| 3822 | } |
| 3823 | |
Li Zefan | 33345d01 | 2011-04-20 10:31:50 +0800 | [diff] [blame] | 3824 | ret = btrfs_del_inode_ref(trans, root, name, name_len, ino, |
| 3825 | dir_ino, &index); |
Josef Bacik | aec7477 | 2008-07-24 12:12:38 -0400 | [diff] [blame] | 3826 | if (ret) { |
Simon Kirby | c2cf52e | 2013-03-19 22:41:23 +0000 | [diff] [blame] | 3827 | btrfs_info(root->fs_info, |
| 3828 | "failed to delete reference to %.*s, inode %llu parent %llu", |
Geert Uytterhoeven | c1c9ff7 | 2013-08-20 13:20:07 +0200 | [diff] [blame] | 3829 | name_len, name, ino, dir_ino); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 3830 | btrfs_abort_transaction(trans, root, ret); |
Josef Bacik | aec7477 | 2008-07-24 12:12:38 -0400 | [diff] [blame] | 3831 | goto err; |
| 3832 | } |
Miao Xie | 67de117 | 2013-12-26 13:07:06 +0800 | [diff] [blame] | 3833 | skip_backref: |
Miao Xie | 16cdcec | 2011-04-22 18:12:22 +0800 | [diff] [blame] | 3834 | ret = btrfs_delete_delayed_dir_index(trans, root, dir, index); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 3835 | if (ret) { |
| 3836 | btrfs_abort_transaction(trans, root, ret); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3837 | goto err; |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 3838 | } |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3839 | |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 3840 | ret = btrfs_del_inode_ref_in_log(trans, root, name, name_len, |
Li Zefan | 33345d01 | 2011-04-20 10:31:50 +0800 | [diff] [blame] | 3841 | inode, dir_ino); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 3842 | if (ret != 0 && ret != -ENOENT) { |
| 3843 | btrfs_abort_transaction(trans, root, ret); |
| 3844 | goto err; |
| 3845 | } |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 3846 | |
| 3847 | ret = btrfs_del_dir_entries_in_log(trans, root, name, name_len, |
| 3848 | dir, index); |
Chris Mason | 6418c96 | 2010-10-30 07:34:24 -0400 | [diff] [blame] | 3849 | if (ret == -ENOENT) |
| 3850 | ret = 0; |
Zach Brown | d4e3991 | 2013-04-02 21:02:16 +0000 | [diff] [blame] | 3851 | else if (ret) |
| 3852 | btrfs_abort_transaction(trans, root, ret); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3853 | err: |
| 3854 | btrfs_free_path(path); |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 3855 | if (ret) |
| 3856 | goto out; |
| 3857 | |
| 3858 | btrfs_i_size_write(dir, dir->i_size - name_len * 2); |
Josef Bacik | 0c4d2d9 | 2012-04-05 15:03:02 -0400 | [diff] [blame] | 3859 | inode_inc_iversion(inode); |
| 3860 | inode_inc_iversion(dir); |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 3861 | inode->i_ctime = dir->i_mtime = dir->i_ctime = CURRENT_TIME; |
Tsutomu Itoh | b995929 | 2012-06-25 21:25:22 -0600 | [diff] [blame] | 3862 | ret = btrfs_update_inode(trans, root, dir); |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 3863 | out: |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3864 | return ret; |
| 3865 | } |
| 3866 | |
Al Viro | 9298679 | 2011-03-04 17:14:37 +0000 | [diff] [blame] | 3867 | int btrfs_unlink_inode(struct btrfs_trans_handle *trans, |
| 3868 | struct btrfs_root *root, |
| 3869 | struct inode *dir, struct inode *inode, |
| 3870 | const char *name, int name_len) |
| 3871 | { |
| 3872 | int ret; |
| 3873 | ret = __btrfs_unlink_inode(trans, root, dir, inode, name, name_len); |
| 3874 | if (!ret) { |
Zach Brown | 8b558c5 | 2013-10-16 12:10:34 -0700 | [diff] [blame] | 3875 | drop_nlink(inode); |
Al Viro | 9298679 | 2011-03-04 17:14:37 +0000 | [diff] [blame] | 3876 | ret = btrfs_update_inode(trans, root, inode); |
| 3877 | } |
| 3878 | return ret; |
| 3879 | } |
Yan, Zheng | a22285a | 2010-05-16 10:48:46 -0400 | [diff] [blame] | 3880 | |
| 3881 | /* |
| 3882 | * helper to start transaction for unlink and rmdir. |
| 3883 | * |
Josef Bacik | d52be81 | 2013-05-29 14:54:47 -0400 | [diff] [blame] | 3884 | * unlink and rmdir are special in btrfs, they do not always free space, so |
| 3885 | * if we cannot make our reservations the normal way try and see if there is |
| 3886 | * plenty of slack room in the global reserve to migrate, otherwise we cannot |
| 3887 | * allow the unlink to occur. |
Yan, Zheng | a22285a | 2010-05-16 10:48:46 -0400 | [diff] [blame] | 3888 | */ |
Josef Bacik | d52be81 | 2013-05-29 14:54:47 -0400 | [diff] [blame] | 3889 | static struct btrfs_trans_handle *__unlink_start_trans(struct inode *dir) |
Yan, Zheng | a22285a | 2010-05-16 10:48:46 -0400 | [diff] [blame] | 3890 | { |
| 3891 | struct btrfs_trans_handle *trans; |
| 3892 | struct btrfs_root *root = BTRFS_I(dir)->root; |
Yan, Zheng | a22285a | 2010-05-16 10:48:46 -0400 | [diff] [blame] | 3893 | int ret; |
| 3894 | |
Josef Bacik | e70bea5 | 2011-10-11 14:18:24 -0400 | [diff] [blame] | 3895 | /* |
| 3896 | * 1 for the possible orphan item |
| 3897 | * 1 for the dir item |
| 3898 | * 1 for the dir index |
| 3899 | * 1 for the inode ref |
Josef Bacik | e70bea5 | 2011-10-11 14:18:24 -0400 | [diff] [blame] | 3900 | * 1 for the inode |
| 3901 | */ |
Josef Bacik | 6e137ed | 2013-03-26 15:26:55 -0400 | [diff] [blame] | 3902 | trans = btrfs_start_transaction(root, 5); |
Yan, Zheng | a22285a | 2010-05-16 10:48:46 -0400 | [diff] [blame] | 3903 | if (!IS_ERR(trans) || PTR_ERR(trans) != -ENOSPC) |
| 3904 | return trans; |
| 3905 | |
Josef Bacik | d52be81 | 2013-05-29 14:54:47 -0400 | [diff] [blame] | 3906 | if (PTR_ERR(trans) == -ENOSPC) { |
| 3907 | u64 num_bytes = btrfs_calc_trans_metadata_size(root, 5); |
Yan, Zheng | a22285a | 2010-05-16 10:48:46 -0400 | [diff] [blame] | 3908 | |
Josef Bacik | d52be81 | 2013-05-29 14:54:47 -0400 | [diff] [blame] | 3909 | trans = btrfs_start_transaction(root, 0); |
| 3910 | if (IS_ERR(trans)) |
| 3911 | return trans; |
| 3912 | ret = btrfs_cond_migrate_bytes(root->fs_info, |
| 3913 | &root->fs_info->trans_block_rsv, |
| 3914 | num_bytes, 5); |
| 3915 | if (ret) { |
| 3916 | btrfs_end_transaction(trans, root); |
| 3917 | return ERR_PTR(ret); |
Yan, Zheng | a22285a | 2010-05-16 10:48:46 -0400 | [diff] [blame] | 3918 | } |
Josef Bacik | 5a77d76 | 2011-11-01 14:32:23 -0400 | [diff] [blame] | 3919 | trans->block_rsv = &root->fs_info->trans_block_rsv; |
Josef Bacik | d52be81 | 2013-05-29 14:54:47 -0400 | [diff] [blame] | 3920 | trans->bytes_reserved = num_bytes; |
Yan, Zheng | a22285a | 2010-05-16 10:48:46 -0400 | [diff] [blame] | 3921 | } |
Josef Bacik | d52be81 | 2013-05-29 14:54:47 -0400 | [diff] [blame] | 3922 | return trans; |
Yan, Zheng | a22285a | 2010-05-16 10:48:46 -0400 | [diff] [blame] | 3923 | } |
| 3924 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3925 | static int btrfs_unlink(struct inode *dir, struct dentry *dentry) |
| 3926 | { |
Yan, Zheng | a22285a | 2010-05-16 10:48:46 -0400 | [diff] [blame] | 3927 | struct btrfs_root *root = BTRFS_I(dir)->root; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3928 | struct btrfs_trans_handle *trans; |
Josef Bacik | 7b12876 | 2008-07-24 12:17:14 -0400 | [diff] [blame] | 3929 | struct inode *inode = dentry->d_inode; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3930 | int ret; |
| 3931 | |
Josef Bacik | d52be81 | 2013-05-29 14:54:47 -0400 | [diff] [blame] | 3932 | trans = __unlink_start_trans(dir); |
Yan, Zheng | a22285a | 2010-05-16 10:48:46 -0400 | [diff] [blame] | 3933 | if (IS_ERR(trans)) |
Josef Bacik | 5df6a9f | 2009-11-10 21:23:48 -0500 | [diff] [blame] | 3934 | return PTR_ERR(trans); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 3935 | |
Chris Mason | 12fcfd2 | 2009-03-24 10:24:20 -0400 | [diff] [blame] | 3936 | btrfs_record_unlink_dir(trans, dir, dentry->d_inode, 0); |
| 3937 | |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 3938 | ret = btrfs_unlink_inode(trans, root, dir, dentry->d_inode, |
| 3939 | dentry->d_name.name, dentry->d_name.len); |
Tsutomu Itoh | b532402 | 2011-07-19 07:27:20 +0000 | [diff] [blame] | 3940 | if (ret) |
| 3941 | goto out; |
Josef Bacik | 7b12876 | 2008-07-24 12:17:14 -0400 | [diff] [blame] | 3942 | |
Yan, Zheng | a22285a | 2010-05-16 10:48:46 -0400 | [diff] [blame] | 3943 | if (inode->i_nlink == 0) { |
Josef Bacik | 7b12876 | 2008-07-24 12:17:14 -0400 | [diff] [blame] | 3944 | ret = btrfs_orphan_add(trans, inode); |
Tsutomu Itoh | b532402 | 2011-07-19 07:27:20 +0000 | [diff] [blame] | 3945 | if (ret) |
| 3946 | goto out; |
Yan, Zheng | a22285a | 2010-05-16 10:48:46 -0400 | [diff] [blame] | 3947 | } |
Josef Bacik | 7b12876 | 2008-07-24 12:17:14 -0400 | [diff] [blame] | 3948 | |
Tsutomu Itoh | b532402 | 2011-07-19 07:27:20 +0000 | [diff] [blame] | 3949 | out: |
Josef Bacik | d52be81 | 2013-05-29 14:54:47 -0400 | [diff] [blame] | 3950 | btrfs_end_transaction(trans, root); |
Liu Bo | b53d3f5 | 2012-11-14 14:34:34 +0000 | [diff] [blame] | 3951 | btrfs_btree_balance_dirty(root); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3952 | return ret; |
| 3953 | } |
| 3954 | |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 3955 | int btrfs_unlink_subvol(struct btrfs_trans_handle *trans, |
| 3956 | struct btrfs_root *root, |
| 3957 | struct inode *dir, u64 objectid, |
| 3958 | const char *name, int name_len) |
| 3959 | { |
| 3960 | struct btrfs_path *path; |
| 3961 | struct extent_buffer *leaf; |
| 3962 | struct btrfs_dir_item *di; |
| 3963 | struct btrfs_key key; |
| 3964 | u64 index; |
| 3965 | int ret; |
Li Zefan | 33345d01 | 2011-04-20 10:31:50 +0800 | [diff] [blame] | 3966 | u64 dir_ino = btrfs_ino(dir); |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 3967 | |
| 3968 | path = btrfs_alloc_path(); |
| 3969 | if (!path) |
| 3970 | return -ENOMEM; |
| 3971 | |
Li Zefan | 33345d01 | 2011-04-20 10:31:50 +0800 | [diff] [blame] | 3972 | di = btrfs_lookup_dir_item(trans, root, path, dir_ino, |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 3973 | name, name_len, -1); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 3974 | if (IS_ERR_OR_NULL(di)) { |
| 3975 | if (!di) |
| 3976 | ret = -ENOENT; |
| 3977 | else |
| 3978 | ret = PTR_ERR(di); |
| 3979 | goto out; |
| 3980 | } |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 3981 | |
| 3982 | leaf = path->nodes[0]; |
| 3983 | btrfs_dir_item_key_to_cpu(leaf, di, &key); |
| 3984 | WARN_ON(key.type != BTRFS_ROOT_ITEM_KEY || key.objectid != objectid); |
| 3985 | ret = btrfs_delete_one_dir_name(trans, root, path, di); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 3986 | if (ret) { |
| 3987 | btrfs_abort_transaction(trans, root, ret); |
| 3988 | goto out; |
| 3989 | } |
David Sterba | b3b4aa7 | 2011-04-21 01:20:15 +0200 | [diff] [blame] | 3990 | btrfs_release_path(path); |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 3991 | |
| 3992 | ret = btrfs_del_root_ref(trans, root->fs_info->tree_root, |
| 3993 | objectid, root->root_key.objectid, |
Li Zefan | 33345d01 | 2011-04-20 10:31:50 +0800 | [diff] [blame] | 3994 | dir_ino, &index, name, name_len); |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 3995 | if (ret < 0) { |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 3996 | if (ret != -ENOENT) { |
| 3997 | btrfs_abort_transaction(trans, root, ret); |
| 3998 | goto out; |
| 3999 | } |
Li Zefan | 33345d01 | 2011-04-20 10:31:50 +0800 | [diff] [blame] | 4000 | di = btrfs_search_dir_index_item(root, path, dir_ino, |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 4001 | name, name_len); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 4002 | if (IS_ERR_OR_NULL(di)) { |
| 4003 | if (!di) |
| 4004 | ret = -ENOENT; |
| 4005 | else |
| 4006 | ret = PTR_ERR(di); |
| 4007 | btrfs_abort_transaction(trans, root, ret); |
| 4008 | goto out; |
| 4009 | } |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 4010 | |
| 4011 | leaf = path->nodes[0]; |
| 4012 | btrfs_item_key_to_cpu(leaf, &key, path->slots[0]); |
David Sterba | b3b4aa7 | 2011-04-21 01:20:15 +0200 | [diff] [blame] | 4013 | btrfs_release_path(path); |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 4014 | index = key.offset; |
| 4015 | } |
David Sterba | b3b4aa7 | 2011-04-21 01:20:15 +0200 | [diff] [blame] | 4016 | btrfs_release_path(path); |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 4017 | |
Miao Xie | 16cdcec | 2011-04-22 18:12:22 +0800 | [diff] [blame] | 4018 | ret = btrfs_delete_delayed_dir_index(trans, root, dir, index); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 4019 | if (ret) { |
| 4020 | btrfs_abort_transaction(trans, root, ret); |
| 4021 | goto out; |
| 4022 | } |
Miao Xie | 16cdcec | 2011-04-22 18:12:22 +0800 | [diff] [blame] | 4023 | |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 4024 | btrfs_i_size_write(dir, dir->i_size - name_len * 2); |
Josef Bacik | 0c4d2d9 | 2012-04-05 15:03:02 -0400 | [diff] [blame] | 4025 | inode_inc_iversion(dir); |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 4026 | dir->i_mtime = dir->i_ctime = CURRENT_TIME; |
Josef Bacik | 5a24e84 | 2012-08-08 10:12:59 -0600 | [diff] [blame] | 4027 | ret = btrfs_update_inode_fallback(trans, root, dir); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 4028 | if (ret) |
| 4029 | btrfs_abort_transaction(trans, root, ret); |
| 4030 | out: |
Josef Bacik | 71d7aed | 2011-06-14 14:24:32 -0400 | [diff] [blame] | 4031 | btrfs_free_path(path); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 4032 | return ret; |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 4033 | } |
| 4034 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4035 | static int btrfs_rmdir(struct inode *dir, struct dentry *dentry) |
| 4036 | { |
| 4037 | struct inode *inode = dentry->d_inode; |
Chris Mason | 1832a6d | 2007-12-21 16:27:21 -0500 | [diff] [blame] | 4038 | int err = 0; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4039 | struct btrfs_root *root = BTRFS_I(dir)->root; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4040 | struct btrfs_trans_handle *trans; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4041 | |
David Sterba | b3ae244 | 2012-09-13 16:04:34 -0600 | [diff] [blame] | 4042 | if (inode->i_size > BTRFS_EMPTY_DIR_SIZE) |
Yan | 134d451 | 2007-10-25 15:49:25 -0400 | [diff] [blame] | 4043 | return -ENOTEMPTY; |
David Sterba | b3ae244 | 2012-09-13 16:04:34 -0600 | [diff] [blame] | 4044 | if (btrfs_ino(inode) == BTRFS_FIRST_FREE_OBJECTID) |
| 4045 | return -EPERM; |
Yan | 134d451 | 2007-10-25 15:49:25 -0400 | [diff] [blame] | 4046 | |
Josef Bacik | d52be81 | 2013-05-29 14:54:47 -0400 | [diff] [blame] | 4047 | trans = __unlink_start_trans(dir); |
Yan, Zheng | a22285a | 2010-05-16 10:48:46 -0400 | [diff] [blame] | 4048 | if (IS_ERR(trans)) |
Josef Bacik | 5df6a9f | 2009-11-10 21:23:48 -0500 | [diff] [blame] | 4049 | return PTR_ERR(trans); |
Josef Bacik | 5df6a9f | 2009-11-10 21:23:48 -0500 | [diff] [blame] | 4050 | |
Li Zefan | 33345d01 | 2011-04-20 10:31:50 +0800 | [diff] [blame] | 4051 | if (unlikely(btrfs_ino(inode) == BTRFS_EMPTY_SUBVOL_DIR_OBJECTID)) { |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 4052 | err = btrfs_unlink_subvol(trans, root, dir, |
| 4053 | BTRFS_I(inode)->location.objectid, |
| 4054 | dentry->d_name.name, |
| 4055 | dentry->d_name.len); |
| 4056 | goto out; |
| 4057 | } |
| 4058 | |
Josef Bacik | 7b12876 | 2008-07-24 12:17:14 -0400 | [diff] [blame] | 4059 | err = btrfs_orphan_add(trans, inode); |
| 4060 | if (err) |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 4061 | goto out; |
Josef Bacik | 7b12876 | 2008-07-24 12:17:14 -0400 | [diff] [blame] | 4062 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4063 | /* now the directory is empty */ |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 4064 | err = btrfs_unlink_inode(trans, root, dir, dentry->d_inode, |
| 4065 | dentry->d_name.name, dentry->d_name.len); |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 4066 | if (!err) |
Chris Mason | dbe674a | 2008-07-17 12:54:05 -0400 | [diff] [blame] | 4067 | btrfs_i_size_write(inode, 0); |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 4068 | out: |
Josef Bacik | d52be81 | 2013-05-29 14:54:47 -0400 | [diff] [blame] | 4069 | btrfs_end_transaction(trans, root); |
Liu Bo | b53d3f5 | 2012-11-14 14:34:34 +0000 | [diff] [blame] | 4070 | btrfs_btree_balance_dirty(root); |
Chris Mason | 3954401 | 2007-12-12 14:38:19 -0500 | [diff] [blame] | 4071 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4072 | return err; |
| 4073 | } |
| 4074 | |
Chris Mason | 323ac95 | 2008-10-01 19:05:46 -0400 | [diff] [blame] | 4075 | /* |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4076 | * this can truncate away extent items, csum items and directory items. |
| 4077 | * 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] | 4078 | * any higher than new_size |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4079 | * |
| 4080 | * csum items that cross the new i_size are truncated to the new size |
| 4081 | * as well. |
Josef Bacik | 7b12876 | 2008-07-24 12:17:14 -0400 | [diff] [blame] | 4082 | * |
| 4083 | * min_type is the minimum key type to truncate down to. If set to 0, this |
| 4084 | * 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] | 4085 | */ |
Yan, Zheng | 8082510 | 2009-11-12 09:35:36 +0000 | [diff] [blame] | 4086 | int btrfs_truncate_inode_items(struct btrfs_trans_handle *trans, |
| 4087 | struct btrfs_root *root, |
| 4088 | struct inode *inode, |
| 4089 | u64 new_size, u32 min_type) |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4090 | { |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4091 | struct btrfs_path *path; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 4092 | struct extent_buffer *leaf; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4093 | struct btrfs_file_extent_item *fi; |
Yan, Zheng | 8082510 | 2009-11-12 09:35:36 +0000 | [diff] [blame] | 4094 | struct btrfs_key key; |
| 4095 | struct btrfs_key found_key; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4096 | u64 extent_start = 0; |
Chris Mason | db94535 | 2007-10-15 16:15:53 -0400 | [diff] [blame] | 4097 | u64 extent_num_bytes = 0; |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 4098 | u64 extent_offset = 0; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4099 | u64 item_end = 0; |
Josef Bacik | 7f4f6e0 | 2013-08-29 16:43:28 -0400 | [diff] [blame] | 4100 | u64 last_size = (u64)-1; |
Yan, Zheng | 8082510 | 2009-11-12 09:35:36 +0000 | [diff] [blame] | 4101 | u32 found_type = (u8)-1; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4102 | int found_extent; |
| 4103 | int del_item; |
Chris Mason | 85e21ba | 2008-01-29 15:11:36 -0500 | [diff] [blame] | 4104 | int pending_del_nr = 0; |
| 4105 | int pending_del_slot = 0; |
Chris Mason | 179e29e | 2007-11-01 11:28:41 -0400 | [diff] [blame] | 4106 | int extent_type = -1; |
Yan, Zheng | 8082510 | 2009-11-12 09:35:36 +0000 | [diff] [blame] | 4107 | int ret; |
| 4108 | int err = 0; |
Li Zefan | 33345d01 | 2011-04-20 10:31:50 +0800 | [diff] [blame] | 4109 | u64 ino = btrfs_ino(inode); |
Yan, Zheng | 8082510 | 2009-11-12 09:35:36 +0000 | [diff] [blame] | 4110 | |
| 4111 | BUG_ON(new_size > 0 && min_type != BTRFS_EXTENT_DATA_KEY); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4112 | |
Mark Fasheh | 0eb0e19 | 2011-07-12 16:44:10 -0700 | [diff] [blame] | 4113 | path = btrfs_alloc_path(); |
| 4114 | if (!path) |
| 4115 | return -ENOMEM; |
| 4116 | path->reada = -1; |
| 4117 | |
Josef Bacik | 5dc562c | 2012-08-17 13:14:17 -0400 | [diff] [blame] | 4118 | /* |
| 4119 | * We want to drop from the next block forward in case this new size is |
| 4120 | * not block aligned since we will be keeping the last block of the |
| 4121 | * extent just the way it is. |
| 4122 | */ |
Miao Xie | 27cdeb7 | 2014-04-02 19:51:05 +0800 | [diff] [blame] | 4123 | if (test_bit(BTRFS_ROOT_REF_COWS, &root->state) || |
| 4124 | root == root->fs_info->tree_root) |
Qu Wenruo | fda2832 | 2013-02-26 08:10:22 +0000 | [diff] [blame] | 4125 | btrfs_drop_extent_cache(inode, ALIGN(new_size, |
| 4126 | root->sectorsize), (u64)-1, 0); |
Yan, Zheng | 8082510 | 2009-11-12 09:35:36 +0000 | [diff] [blame] | 4127 | |
Miao Xie | 16cdcec | 2011-04-22 18:12:22 +0800 | [diff] [blame] | 4128 | /* |
| 4129 | * This function is also used to drop the items in the log tree before |
| 4130 | * we relog the inode, so if root != BTRFS_I(inode)->root, it means |
| 4131 | * it is used to drop the loged items. So we shouldn't kill the delayed |
| 4132 | * items. |
| 4133 | */ |
| 4134 | if (min_type == 0 && root == BTRFS_I(inode)->root) |
| 4135 | btrfs_kill_delayed_inode_items(inode); |
| 4136 | |
Li Zefan | 33345d01 | 2011-04-20 10:31:50 +0800 | [diff] [blame] | 4137 | key.objectid = ino; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4138 | key.offset = (u64)-1; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 4139 | key.type = (u8)-1; |
| 4140 | |
Chris Mason | 85e21ba | 2008-01-29 15:11:36 -0500 | [diff] [blame] | 4141 | search_again: |
Chris Mason | b947343 | 2009-03-13 11:00:37 -0400 | [diff] [blame] | 4142 | path->leave_spinning = 1; |
Chris Mason | 85e21ba | 2008-01-29 15:11:36 -0500 | [diff] [blame] | 4143 | ret = btrfs_search_slot(trans, root, &key, path, -1, 1); |
Yan, Zheng | 8082510 | 2009-11-12 09:35:36 +0000 | [diff] [blame] | 4144 | if (ret < 0) { |
| 4145 | err = ret; |
| 4146 | goto out; |
| 4147 | } |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 4148 | |
Chris Mason | 85e21ba | 2008-01-29 15:11:36 -0500 | [diff] [blame] | 4149 | if (ret > 0) { |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 4150 | /* there are no items in the tree for us to truncate, we're |
| 4151 | * done |
| 4152 | */ |
Yan, Zheng | 8082510 | 2009-11-12 09:35:36 +0000 | [diff] [blame] | 4153 | if (path->slots[0] == 0) |
| 4154 | goto out; |
Chris Mason | 85e21ba | 2008-01-29 15:11:36 -0500 | [diff] [blame] | 4155 | path->slots[0]--; |
| 4156 | } |
| 4157 | |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 4158 | while (1) { |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4159 | fi = NULL; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 4160 | leaf = path->nodes[0]; |
| 4161 | btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]); |
David Sterba | 962a298 | 2014-06-04 18:41:45 +0200 | [diff] [blame] | 4162 | found_type = found_key.type; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4163 | |
Li Zefan | 33345d01 | 2011-04-20 10:31:50 +0800 | [diff] [blame] | 4164 | if (found_key.objectid != ino) |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4165 | break; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 4166 | |
Chris Mason | 85e21ba | 2008-01-29 15:11:36 -0500 | [diff] [blame] | 4167 | if (found_type < min_type) |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4168 | break; |
| 4169 | |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 4170 | item_end = found_key.offset; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4171 | if (found_type == BTRFS_EXTENT_DATA_KEY) { |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 4172 | fi = btrfs_item_ptr(leaf, path->slots[0], |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4173 | struct btrfs_file_extent_item); |
Chris Mason | 179e29e | 2007-11-01 11:28:41 -0400 | [diff] [blame] | 4174 | extent_type = btrfs_file_extent_type(leaf, fi); |
| 4175 | if (extent_type != BTRFS_FILE_EXTENT_INLINE) { |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 4176 | item_end += |
Chris Mason | db94535 | 2007-10-15 16:15:53 -0400 | [diff] [blame] | 4177 | btrfs_file_extent_num_bytes(leaf, fi); |
Chris Mason | 179e29e | 2007-11-01 11:28:41 -0400 | [diff] [blame] | 4178 | } else if (extent_type == BTRFS_FILE_EXTENT_INLINE) { |
Chris Mason | 179e29e | 2007-11-01 11:28:41 -0400 | [diff] [blame] | 4179 | item_end += btrfs_file_extent_inline_len(leaf, |
Chris Mason | 514ac8a | 2014-01-03 21:07:00 -0800 | [diff] [blame] | 4180 | path->slots[0], fi); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4181 | } |
Yan | 008630c | 2007-11-07 13:31:09 -0500 | [diff] [blame] | 4182 | item_end--; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4183 | } |
Yan, Zheng | 8082510 | 2009-11-12 09:35:36 +0000 | [diff] [blame] | 4184 | if (found_type > min_type) { |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4185 | del_item = 1; |
Yan, Zheng | 8082510 | 2009-11-12 09:35:36 +0000 | [diff] [blame] | 4186 | } else { |
| 4187 | if (item_end < new_size) |
| 4188 | break; |
| 4189 | if (found_key.offset >= new_size) |
| 4190 | del_item = 1; |
| 4191 | else |
| 4192 | del_item = 0; |
| 4193 | } |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4194 | found_extent = 0; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4195 | /* FIXME, shrink the extent if the ref count is only 1 */ |
Chris Mason | 179e29e | 2007-11-01 11:28:41 -0400 | [diff] [blame] | 4196 | if (found_type != BTRFS_EXTENT_DATA_KEY) |
| 4197 | goto delete; |
| 4198 | |
Josef Bacik | 7f4f6e0 | 2013-08-29 16:43:28 -0400 | [diff] [blame] | 4199 | if (del_item) |
| 4200 | last_size = found_key.offset; |
| 4201 | else |
| 4202 | last_size = new_size; |
| 4203 | |
Chris Mason | 179e29e | 2007-11-01 11:28:41 -0400 | [diff] [blame] | 4204 | if (extent_type != BTRFS_FILE_EXTENT_INLINE) { |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4205 | u64 num_dec; |
Chris Mason | db94535 | 2007-10-15 16:15:53 -0400 | [diff] [blame] | 4206 | extent_start = btrfs_file_extent_disk_bytenr(leaf, fi); |
Miao Xie | f70a9a6b | 2012-01-12 19:10:12 -0500 | [diff] [blame] | 4207 | if (!del_item) { |
Chris Mason | db94535 | 2007-10-15 16:15:53 -0400 | [diff] [blame] | 4208 | u64 orig_num_bytes = |
| 4209 | btrfs_file_extent_num_bytes(leaf, fi); |
Qu Wenruo | fda2832 | 2013-02-26 08:10:22 +0000 | [diff] [blame] | 4210 | extent_num_bytes = ALIGN(new_size - |
| 4211 | found_key.offset, |
| 4212 | root->sectorsize); |
Chris Mason | db94535 | 2007-10-15 16:15:53 -0400 | [diff] [blame] | 4213 | btrfs_set_file_extent_num_bytes(leaf, fi, |
| 4214 | extent_num_bytes); |
| 4215 | num_dec = (orig_num_bytes - |
Chris Mason | 9069218 | 2008-02-08 13:49:28 -0500 | [diff] [blame] | 4216 | extent_num_bytes); |
Miao Xie | 27cdeb7 | 2014-04-02 19:51:05 +0800 | [diff] [blame] | 4217 | if (test_bit(BTRFS_ROOT_REF_COWS, |
| 4218 | &root->state) && |
| 4219 | extent_start != 0) |
Yan Zheng | a76a3cd | 2008-10-09 11:46:29 -0400 | [diff] [blame] | 4220 | inode_sub_bytes(inode, num_dec); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 4221 | btrfs_mark_buffer_dirty(leaf); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4222 | } else { |
Chris Mason | db94535 | 2007-10-15 16:15:53 -0400 | [diff] [blame] | 4223 | extent_num_bytes = |
| 4224 | btrfs_file_extent_disk_num_bytes(leaf, |
| 4225 | fi); |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 4226 | extent_offset = found_key.offset - |
| 4227 | btrfs_file_extent_offset(leaf, fi); |
| 4228 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4229 | /* FIXME blocksize != 4096 */ |
Chris Mason | 9069218 | 2008-02-08 13:49:28 -0500 | [diff] [blame] | 4230 | num_dec = btrfs_file_extent_num_bytes(leaf, fi); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4231 | if (extent_start != 0) { |
| 4232 | found_extent = 1; |
Miao Xie | 27cdeb7 | 2014-04-02 19:51:05 +0800 | [diff] [blame] | 4233 | if (test_bit(BTRFS_ROOT_REF_COWS, |
| 4234 | &root->state)) |
Yan Zheng | a76a3cd | 2008-10-09 11:46:29 -0400 | [diff] [blame] | 4235 | inode_sub_bytes(inode, num_dec); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4236 | } |
| 4237 | } |
Chris Mason | 9069218 | 2008-02-08 13:49:28 -0500 | [diff] [blame] | 4238 | } else if (extent_type == BTRFS_FILE_EXTENT_INLINE) { |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 4239 | /* |
| 4240 | * we can't truncate inline items that have had |
| 4241 | * special encodings |
| 4242 | */ |
| 4243 | if (!del_item && |
| 4244 | btrfs_file_extent_compression(leaf, fi) == 0 && |
| 4245 | btrfs_file_extent_encryption(leaf, fi) == 0 && |
| 4246 | btrfs_file_extent_other_encoding(leaf, fi) == 0) { |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 4247 | u32 size = new_size - found_key.offset; |
| 4248 | |
Miao Xie | 27cdeb7 | 2014-04-02 19:51:05 +0800 | [diff] [blame] | 4249 | if (test_bit(BTRFS_ROOT_REF_COWS, &root->state)) |
Yan Zheng | a76a3cd | 2008-10-09 11:46:29 -0400 | [diff] [blame] | 4250 | inode_sub_bytes(inode, item_end + 1 - |
| 4251 | new_size); |
Chris Mason | 514ac8a | 2014-01-03 21:07:00 -0800 | [diff] [blame] | 4252 | |
| 4253 | /* |
| 4254 | * update the ram bytes to properly reflect |
| 4255 | * the new size of our item |
| 4256 | */ |
| 4257 | btrfs_set_file_extent_ram_bytes(leaf, fi, size); |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 4258 | size = |
| 4259 | btrfs_file_extent_calc_inline_size(size); |
Tsutomu Itoh | afe5fea | 2013-04-16 05:18:22 +0000 | [diff] [blame] | 4260 | btrfs_truncate_item(root, path, size, 1); |
Miao Xie | 27cdeb7 | 2014-04-02 19:51:05 +0800 | [diff] [blame] | 4261 | } else if (test_bit(BTRFS_ROOT_REF_COWS, |
| 4262 | &root->state)) { |
Yan Zheng | a76a3cd | 2008-10-09 11:46:29 -0400 | [diff] [blame] | 4263 | inode_sub_bytes(inode, item_end + 1 - |
| 4264 | found_key.offset); |
Chris Mason | 9069218 | 2008-02-08 13:49:28 -0500 | [diff] [blame] | 4265 | } |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4266 | } |
Chris Mason | 179e29e | 2007-11-01 11:28:41 -0400 | [diff] [blame] | 4267 | delete: |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4268 | if (del_item) { |
Chris Mason | 85e21ba | 2008-01-29 15:11:36 -0500 | [diff] [blame] | 4269 | if (!pending_del_nr) { |
| 4270 | /* no pending yet, add ourselves */ |
| 4271 | pending_del_slot = path->slots[0]; |
| 4272 | pending_del_nr = 1; |
| 4273 | } else if (pending_del_nr && |
| 4274 | path->slots[0] + 1 == pending_del_slot) { |
| 4275 | /* hop on the pending chunk */ |
| 4276 | pending_del_nr++; |
| 4277 | pending_del_slot = path->slots[0]; |
| 4278 | } else { |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 4279 | BUG(); |
Chris Mason | 85e21ba | 2008-01-29 15:11:36 -0500 | [diff] [blame] | 4280 | } |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4281 | } else { |
| 4282 | break; |
| 4283 | } |
Miao Xie | 27cdeb7 | 2014-04-02 19:51:05 +0800 | [diff] [blame] | 4284 | if (found_extent && |
| 4285 | (test_bit(BTRFS_ROOT_REF_COWS, &root->state) || |
| 4286 | root == root->fs_info->tree_root)) { |
Chris Mason | b947343 | 2009-03-13 11:00:37 -0400 | [diff] [blame] | 4287 | btrfs_set_path_blocking(path); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4288 | ret = btrfs_free_extent(trans, root, extent_start, |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 4289 | extent_num_bytes, 0, |
| 4290 | btrfs_header_owner(leaf), |
Arne Jansen | 66d7e7f | 2011-09-12 15:26:38 +0200 | [diff] [blame] | 4291 | ino, extent_offset, 0); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4292 | BUG_ON(ret); |
| 4293 | } |
Chris Mason | 85e21ba | 2008-01-29 15:11:36 -0500 | [diff] [blame] | 4294 | |
Yan, Zheng | 8082510 | 2009-11-12 09:35:36 +0000 | [diff] [blame] | 4295 | if (found_type == BTRFS_INODE_ITEM_KEY) |
| 4296 | break; |
| 4297 | |
| 4298 | if (path->slots[0] == 0 || |
| 4299 | path->slots[0] != pending_del_slot) { |
Yan, Zheng | 8082510 | 2009-11-12 09:35:36 +0000 | [diff] [blame] | 4300 | if (pending_del_nr) { |
| 4301 | ret = btrfs_del_items(trans, root, path, |
| 4302 | pending_del_slot, |
| 4303 | pending_del_nr); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 4304 | if (ret) { |
| 4305 | btrfs_abort_transaction(trans, |
| 4306 | root, ret); |
| 4307 | goto error; |
| 4308 | } |
Yan, Zheng | 8082510 | 2009-11-12 09:35:36 +0000 | [diff] [blame] | 4309 | pending_del_nr = 0; |
| 4310 | } |
David Sterba | b3b4aa7 | 2011-04-21 01:20:15 +0200 | [diff] [blame] | 4311 | btrfs_release_path(path); |
Chris Mason | 85e21ba | 2008-01-29 15:11:36 -0500 | [diff] [blame] | 4312 | goto search_again; |
Yan, Zheng | 8082510 | 2009-11-12 09:35:36 +0000 | [diff] [blame] | 4313 | } else { |
| 4314 | path->slots[0]--; |
Chris Mason | 85e21ba | 2008-01-29 15:11:36 -0500 | [diff] [blame] | 4315 | } |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4316 | } |
Yan, Zheng | 8082510 | 2009-11-12 09:35:36 +0000 | [diff] [blame] | 4317 | out: |
Chris Mason | 85e21ba | 2008-01-29 15:11:36 -0500 | [diff] [blame] | 4318 | if (pending_del_nr) { |
| 4319 | ret = btrfs_del_items(trans, root, path, pending_del_slot, |
| 4320 | pending_del_nr); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 4321 | if (ret) |
| 4322 | btrfs_abort_transaction(trans, root, ret); |
Chris Mason | 85e21ba | 2008-01-29 15:11:36 -0500 | [diff] [blame] | 4323 | } |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 4324 | error: |
Filipe Manana | dac5705 | 2014-08-29 20:54:26 +0100 | [diff] [blame] | 4325 | if (last_size != (u64)-1 && |
| 4326 | root->root_key.objectid != BTRFS_TREE_LOG_OBJECTID) |
Josef Bacik | 7f4f6e0 | 2013-08-29 16:43:28 -0400 | [diff] [blame] | 4327 | btrfs_ordered_update_i_size(inode, last_size, NULL); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4328 | btrfs_free_path(path); |
Yan, Zheng | 8082510 | 2009-11-12 09:35:36 +0000 | [diff] [blame] | 4329 | return err; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4330 | } |
| 4331 | |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 4332 | /* |
Josef Bacik | 2aaa665 | 2012-08-29 14:27:18 -0400 | [diff] [blame] | 4333 | * btrfs_truncate_page - read, zero a chunk and write a page |
| 4334 | * @inode - inode that we're zeroing |
| 4335 | * @from - the offset to start zeroing |
| 4336 | * @len - the length to zero, 0 to zero the entire range respective to the |
| 4337 | * offset |
| 4338 | * @front - zero up to the offset instead of from the offset on |
| 4339 | * |
| 4340 | * This will find the page for the "from" offset and cow the page and zero the |
| 4341 | * 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] | 4342 | */ |
Josef Bacik | 2aaa665 | 2012-08-29 14:27:18 -0400 | [diff] [blame] | 4343 | int btrfs_truncate_page(struct inode *inode, loff_t from, loff_t len, |
| 4344 | int front) |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 4345 | { |
Josef Bacik | 2aaa665 | 2012-08-29 14:27:18 -0400 | [diff] [blame] | 4346 | struct address_space *mapping = inode->i_mapping; |
Chris Mason | db94535 | 2007-10-15 16:15:53 -0400 | [diff] [blame] | 4347 | struct btrfs_root *root = BTRFS_I(inode)->root; |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 4348 | struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree; |
| 4349 | struct btrfs_ordered_extent *ordered; |
Josef Bacik | 2ac55d4 | 2010-02-03 19:33:23 +0000 | [diff] [blame] | 4350 | struct extent_state *cached_state = NULL; |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 4351 | char *kaddr; |
Chris Mason | db94535 | 2007-10-15 16:15:53 -0400 | [diff] [blame] | 4352 | u32 blocksize = root->sectorsize; |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 4353 | pgoff_t index = from >> PAGE_CACHE_SHIFT; |
| 4354 | unsigned offset = from & (PAGE_CACHE_SIZE-1); |
| 4355 | struct page *page; |
Josef Bacik | 3b16a4e | 2011-09-21 15:05:58 -0400 | [diff] [blame] | 4356 | gfp_t mask = btrfs_alloc_write_mask(mapping); |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 4357 | int ret = 0; |
| 4358 | u64 page_start; |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 4359 | u64 page_end; |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 4360 | |
Josef Bacik | 2aaa665 | 2012-08-29 14:27:18 -0400 | [diff] [blame] | 4361 | if ((offset & (blocksize - 1)) == 0 && |
| 4362 | (!len || ((len & (blocksize - 1)) == 0))) |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 4363 | goto out; |
Yan, Zheng | 0ca1f7c | 2010-05-16 10:48:47 -0400 | [diff] [blame] | 4364 | ret = btrfs_delalloc_reserve_space(inode, PAGE_CACHE_SIZE); |
Josef Bacik | 5d5e103 | 2009-10-13 16:46:49 -0400 | [diff] [blame] | 4365 | if (ret) |
| 4366 | goto out; |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 4367 | |
Chris Mason | 211c17f | 2008-05-15 09:13:45 -0400 | [diff] [blame] | 4368 | again: |
Josef Bacik | 3b16a4e | 2011-09-21 15:05:58 -0400 | [diff] [blame] | 4369 | page = find_or_create_page(mapping, index, mask); |
Josef Bacik | 5d5e103 | 2009-10-13 16:46:49 -0400 | [diff] [blame] | 4370 | if (!page) { |
Yan, Zheng | 0ca1f7c | 2010-05-16 10:48:47 -0400 | [diff] [blame] | 4371 | btrfs_delalloc_release_space(inode, PAGE_CACHE_SIZE); |
Miao Xie | ac6a2b3 | 2012-12-05 10:56:13 +0000 | [diff] [blame] | 4372 | ret = -ENOMEM; |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 4373 | goto out; |
Josef Bacik | 5d5e103 | 2009-10-13 16:46:49 -0400 | [diff] [blame] | 4374 | } |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 4375 | |
| 4376 | page_start = page_offset(page); |
| 4377 | page_end = page_start + PAGE_CACHE_SIZE - 1; |
| 4378 | |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 4379 | if (!PageUptodate(page)) { |
| 4380 | ret = btrfs_readpage(NULL, page); |
| 4381 | lock_page(page); |
Chris Mason | 211c17f | 2008-05-15 09:13:45 -0400 | [diff] [blame] | 4382 | if (page->mapping != mapping) { |
| 4383 | unlock_page(page); |
| 4384 | page_cache_release(page); |
| 4385 | goto again; |
| 4386 | } |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 4387 | if (!PageUptodate(page)) { |
| 4388 | ret = -EIO; |
Chris Mason | 8964222 | 2008-07-24 09:41:53 -0400 | [diff] [blame] | 4389 | goto out_unlock; |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 4390 | } |
| 4391 | } |
Chris Mason | 211c17f | 2008-05-15 09:13:45 -0400 | [diff] [blame] | 4392 | wait_on_page_writeback(page); |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 4393 | |
Jeff Mahoney | d008237 | 2012-03-01 14:57:19 +0100 | [diff] [blame] | 4394 | lock_extent_bits(io_tree, page_start, page_end, 0, &cached_state); |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 4395 | set_page_extent_mapped(page); |
| 4396 | |
| 4397 | ordered = btrfs_lookup_ordered_extent(inode, page_start); |
| 4398 | if (ordered) { |
Josef Bacik | 2ac55d4 | 2010-02-03 19:33:23 +0000 | [diff] [blame] | 4399 | unlock_extent_cached(io_tree, page_start, page_end, |
| 4400 | &cached_state, GFP_NOFS); |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 4401 | unlock_page(page); |
| 4402 | page_cache_release(page); |
Chris Mason | eb84ae0 | 2008-07-17 13:53:27 -0400 | [diff] [blame] | 4403 | btrfs_start_ordered_extent(inode, ordered, 1); |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 4404 | btrfs_put_ordered_extent(ordered); |
| 4405 | goto again; |
| 4406 | } |
| 4407 | |
Josef Bacik | 2ac55d4 | 2010-02-03 19:33:23 +0000 | [diff] [blame] | 4408 | clear_extent_bit(&BTRFS_I(inode)->io_tree, page_start, page_end, |
Liu Bo | 9e8a4a8 | 2012-09-05 19:10:51 -0600 | [diff] [blame] | 4409 | EXTENT_DIRTY | EXTENT_DELALLOC | |
| 4410 | EXTENT_DO_ACCOUNTING | EXTENT_DEFRAG, |
Josef Bacik | 2ac55d4 | 2010-02-03 19:33:23 +0000 | [diff] [blame] | 4411 | 0, 0, &cached_state, GFP_NOFS); |
Josef Bacik | 5d5e103 | 2009-10-13 16:46:49 -0400 | [diff] [blame] | 4412 | |
Josef Bacik | 2ac55d4 | 2010-02-03 19:33:23 +0000 | [diff] [blame] | 4413 | ret = btrfs_set_extent_delalloc(inode, page_start, page_end, |
| 4414 | &cached_state); |
Josef Bacik | 9ed74f2 | 2009-09-11 16:12:44 -0400 | [diff] [blame] | 4415 | if (ret) { |
Josef Bacik | 2ac55d4 | 2010-02-03 19:33:23 +0000 | [diff] [blame] | 4416 | unlock_extent_cached(io_tree, page_start, page_end, |
| 4417 | &cached_state, GFP_NOFS); |
Josef Bacik | 9ed74f2 | 2009-09-11 16:12:44 -0400 | [diff] [blame] | 4418 | goto out_unlock; |
| 4419 | } |
| 4420 | |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 4421 | if (offset != PAGE_CACHE_SIZE) { |
Josef Bacik | 2aaa665 | 2012-08-29 14:27:18 -0400 | [diff] [blame] | 4422 | if (!len) |
| 4423 | len = PAGE_CACHE_SIZE - offset; |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 4424 | kaddr = kmap(page); |
Josef Bacik | 2aaa665 | 2012-08-29 14:27:18 -0400 | [diff] [blame] | 4425 | if (front) |
| 4426 | memset(kaddr, 0, offset); |
| 4427 | else |
| 4428 | memset(kaddr + offset, 0, len); |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 4429 | flush_dcache_page(page); |
| 4430 | kunmap(page); |
| 4431 | } |
Chris Mason | 247e743 | 2008-07-17 12:53:51 -0400 | [diff] [blame] | 4432 | ClearPageChecked(page); |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 4433 | set_page_dirty(page); |
Josef Bacik | 2ac55d4 | 2010-02-03 19:33:23 +0000 | [diff] [blame] | 4434 | unlock_extent_cached(io_tree, page_start, page_end, &cached_state, |
| 4435 | GFP_NOFS); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4436 | |
Chris Mason | 8964222 | 2008-07-24 09:41:53 -0400 | [diff] [blame] | 4437 | out_unlock: |
Josef Bacik | 5d5e103 | 2009-10-13 16:46:49 -0400 | [diff] [blame] | 4438 | if (ret) |
Yan, Zheng | 0ca1f7c | 2010-05-16 10:48:47 -0400 | [diff] [blame] | 4439 | btrfs_delalloc_release_space(inode, PAGE_CACHE_SIZE); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4440 | unlock_page(page); |
| 4441 | page_cache_release(page); |
| 4442 | out: |
| 4443 | return ret; |
| 4444 | } |
| 4445 | |
Josef Bacik | 16e7549 | 2013-10-22 12:18:51 -0400 | [diff] [blame] | 4446 | static int maybe_insert_hole(struct btrfs_root *root, struct inode *inode, |
| 4447 | u64 offset, u64 len) |
| 4448 | { |
| 4449 | struct btrfs_trans_handle *trans; |
| 4450 | int ret; |
| 4451 | |
| 4452 | /* |
| 4453 | * Still need to make sure the inode looks like it's been updated so |
| 4454 | * that any holes get logged if we fsync. |
| 4455 | */ |
| 4456 | if (btrfs_fs_incompat(root->fs_info, NO_HOLES)) { |
| 4457 | BTRFS_I(inode)->last_trans = root->fs_info->generation; |
| 4458 | BTRFS_I(inode)->last_sub_trans = root->log_transid; |
| 4459 | BTRFS_I(inode)->last_log_commit = root->last_log_commit; |
| 4460 | return 0; |
| 4461 | } |
| 4462 | |
| 4463 | /* |
| 4464 | * 1 - for the one we're dropping |
| 4465 | * 1 - for the one we're adding |
| 4466 | * 1 - for updating the inode. |
| 4467 | */ |
| 4468 | trans = btrfs_start_transaction(root, 3); |
| 4469 | if (IS_ERR(trans)) |
| 4470 | return PTR_ERR(trans); |
| 4471 | |
| 4472 | ret = btrfs_drop_extents(trans, root, inode, offset, offset + len, 1); |
| 4473 | if (ret) { |
| 4474 | btrfs_abort_transaction(trans, root, ret); |
| 4475 | btrfs_end_transaction(trans, root); |
| 4476 | return ret; |
| 4477 | } |
| 4478 | |
| 4479 | ret = btrfs_insert_file_extent(trans, root, btrfs_ino(inode), offset, |
| 4480 | 0, 0, len, 0, len, 0, 0, 0); |
| 4481 | if (ret) |
| 4482 | btrfs_abort_transaction(trans, root, ret); |
| 4483 | else |
| 4484 | btrfs_update_inode(trans, root, inode); |
| 4485 | btrfs_end_transaction(trans, root); |
| 4486 | return ret; |
| 4487 | } |
| 4488 | |
Josef Bacik | 695a0d0 | 2011-03-04 15:46:53 -0500 | [diff] [blame] | 4489 | /* |
| 4490 | * This function puts in dummy file extents for the area we're creating a hole |
| 4491 | * for. So if we are truncating this file to a larger size we need to insert |
| 4492 | * these file extents so that btrfs_get_extent will return a EXTENT_MAP_HOLE for |
| 4493 | * the range between oldsize and size |
| 4494 | */ |
Josef Bacik | a41ad39 | 2011-01-31 15:30:16 -0500 | [diff] [blame] | 4495 | 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] | 4496 | { |
Yan Zheng | 9036c10 | 2008-10-30 14:19:41 -0400 | [diff] [blame] | 4497 | struct btrfs_root *root = BTRFS_I(inode)->root; |
| 4498 | struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree; |
Yan, Zheng | a22285a | 2010-05-16 10:48:46 -0400 | [diff] [blame] | 4499 | struct extent_map *em = NULL; |
Josef Bacik | 2ac55d4 | 2010-02-03 19:33:23 +0000 | [diff] [blame] | 4500 | struct extent_state *cached_state = NULL; |
Josef Bacik | 5dc562c | 2012-08-17 13:14:17 -0400 | [diff] [blame] | 4501 | struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree; |
Qu Wenruo | fda2832 | 2013-02-26 08:10:22 +0000 | [diff] [blame] | 4502 | u64 hole_start = ALIGN(oldsize, root->sectorsize); |
| 4503 | u64 block_end = ALIGN(size, root->sectorsize); |
Yan Zheng | 9036c10 | 2008-10-30 14:19:41 -0400 | [diff] [blame] | 4504 | u64 last_byte; |
| 4505 | u64 cur_offset; |
| 4506 | u64 hole_size; |
Josef Bacik | 9ed74f2 | 2009-09-11 16:12:44 -0400 | [diff] [blame] | 4507 | int err = 0; |
Yan Zheng | 9036c10 | 2008-10-30 14:19:41 -0400 | [diff] [blame] | 4508 | |
Josef Bacik | a71754f | 2013-06-17 17:14:39 -0400 | [diff] [blame] | 4509 | /* |
| 4510 | * If our size started in the middle of a page we need to zero out the |
| 4511 | * rest of the page before we expand the i_size, otherwise we could |
| 4512 | * expose stale data. |
| 4513 | */ |
| 4514 | err = btrfs_truncate_page(inode, oldsize, 0, 0); |
| 4515 | if (err) |
| 4516 | return err; |
| 4517 | |
Yan Zheng | 9036c10 | 2008-10-30 14:19:41 -0400 | [diff] [blame] | 4518 | if (size <= hole_start) |
| 4519 | return 0; |
| 4520 | |
Yan Zheng | 9036c10 | 2008-10-30 14:19:41 -0400 | [diff] [blame] | 4521 | while (1) { |
| 4522 | struct btrfs_ordered_extent *ordered; |
Miao Xie | fa7c149 | 2013-09-26 13:15:27 +0800 | [diff] [blame] | 4523 | |
Josef Bacik | 2ac55d4 | 2010-02-03 19:33:23 +0000 | [diff] [blame] | 4524 | lock_extent_bits(io_tree, hole_start, block_end - 1, 0, |
Jeff Mahoney | d008237 | 2012-03-01 14:57:19 +0100 | [diff] [blame] | 4525 | &cached_state); |
Miao Xie | fa7c149 | 2013-09-26 13:15:27 +0800 | [diff] [blame] | 4526 | ordered = btrfs_lookup_ordered_range(inode, hole_start, |
| 4527 | block_end - hole_start); |
Yan Zheng | 9036c10 | 2008-10-30 14:19:41 -0400 | [diff] [blame] | 4528 | if (!ordered) |
| 4529 | break; |
Josef Bacik | 2ac55d4 | 2010-02-03 19:33:23 +0000 | [diff] [blame] | 4530 | unlock_extent_cached(io_tree, hole_start, block_end - 1, |
| 4531 | &cached_state, GFP_NOFS); |
Miao Xie | fa7c149 | 2013-09-26 13:15:27 +0800 | [diff] [blame] | 4532 | btrfs_start_ordered_extent(inode, ordered, 1); |
Yan Zheng | 9036c10 | 2008-10-30 14:19:41 -0400 | [diff] [blame] | 4533 | btrfs_put_ordered_extent(ordered); |
| 4534 | } |
| 4535 | |
Yan Zheng | 9036c10 | 2008-10-30 14:19:41 -0400 | [diff] [blame] | 4536 | cur_offset = hole_start; |
| 4537 | while (1) { |
| 4538 | em = btrfs_get_extent(inode, NULL, 0, cur_offset, |
| 4539 | block_end - cur_offset, 0); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 4540 | if (IS_ERR(em)) { |
| 4541 | err = PTR_ERR(em); |
Zach Brown | f276795 | 2013-01-08 19:37:58 +0000 | [diff] [blame] | 4542 | em = NULL; |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 4543 | break; |
| 4544 | } |
Yan Zheng | 9036c10 | 2008-10-30 14:19:41 -0400 | [diff] [blame] | 4545 | last_byte = min(extent_map_end(em), block_end); |
Qu Wenruo | fda2832 | 2013-02-26 08:10:22 +0000 | [diff] [blame] | 4546 | last_byte = ALIGN(last_byte , root->sectorsize); |
Yan, Zheng | 8082510 | 2009-11-12 09:35:36 +0000 | [diff] [blame] | 4547 | if (!test_bit(EXTENT_FLAG_PREALLOC, &em->flags)) { |
Josef Bacik | 5dc562c | 2012-08-17 13:14:17 -0400 | [diff] [blame] | 4548 | struct extent_map *hole_em; |
Yan Zheng | 9036c10 | 2008-10-30 14:19:41 -0400 | [diff] [blame] | 4549 | hole_size = last_byte - cur_offset; |
Yan, Zheng | 8082510 | 2009-11-12 09:35:36 +0000 | [diff] [blame] | 4550 | |
Josef Bacik | 16e7549 | 2013-10-22 12:18:51 -0400 | [diff] [blame] | 4551 | err = maybe_insert_hole(root, inode, cur_offset, |
| 4552 | hole_size); |
| 4553 | if (err) |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 4554 | break; |
Josef Bacik | 5dc562c | 2012-08-17 13:14:17 -0400 | [diff] [blame] | 4555 | btrfs_drop_extent_cache(inode, cur_offset, |
| 4556 | cur_offset + hole_size - 1, 0); |
| 4557 | hole_em = alloc_extent_map(); |
| 4558 | if (!hole_em) { |
| 4559 | set_bit(BTRFS_INODE_NEEDS_FULL_SYNC, |
| 4560 | &BTRFS_I(inode)->runtime_flags); |
| 4561 | goto next; |
| 4562 | } |
| 4563 | hole_em->start = cur_offset; |
| 4564 | hole_em->len = hole_size; |
| 4565 | hole_em->orig_start = cur_offset; |
Yan, Zheng | 8082510 | 2009-11-12 09:35:36 +0000 | [diff] [blame] | 4566 | |
Josef Bacik | 5dc562c | 2012-08-17 13:14:17 -0400 | [diff] [blame] | 4567 | hole_em->block_start = EXTENT_MAP_HOLE; |
| 4568 | hole_em->block_len = 0; |
Josef Bacik | b493968 | 2012-12-03 10:31:19 -0500 | [diff] [blame] | 4569 | hole_em->orig_block_len = 0; |
Josef Bacik | cc95bef | 2013-04-04 14:31:27 -0400 | [diff] [blame] | 4570 | hole_em->ram_bytes = hole_size; |
Josef Bacik | 5dc562c | 2012-08-17 13:14:17 -0400 | [diff] [blame] | 4571 | hole_em->bdev = root->fs_info->fs_devices->latest_bdev; |
| 4572 | hole_em->compress_type = BTRFS_COMPRESS_NONE; |
Josef Bacik | 16e7549 | 2013-10-22 12:18:51 -0400 | [diff] [blame] | 4573 | hole_em->generation = root->fs_info->generation; |
Josef Bacik | 5dc562c | 2012-08-17 13:14:17 -0400 | [diff] [blame] | 4574 | |
| 4575 | while (1) { |
| 4576 | write_lock(&em_tree->lock); |
Josef Bacik | 09a2a8f9 | 2013-04-05 16:51:15 -0400 | [diff] [blame] | 4577 | err = add_extent_mapping(em_tree, hole_em, 1); |
Josef Bacik | 5dc562c | 2012-08-17 13:14:17 -0400 | [diff] [blame] | 4578 | write_unlock(&em_tree->lock); |
| 4579 | if (err != -EEXIST) |
| 4580 | break; |
| 4581 | btrfs_drop_extent_cache(inode, cur_offset, |
| 4582 | cur_offset + |
| 4583 | hole_size - 1, 0); |
| 4584 | } |
| 4585 | free_extent_map(hole_em); |
Yan Zheng | 9036c10 | 2008-10-30 14:19:41 -0400 | [diff] [blame] | 4586 | } |
Josef Bacik | 16e7549 | 2013-10-22 12:18:51 -0400 | [diff] [blame] | 4587 | next: |
Yan Zheng | 9036c10 | 2008-10-30 14:19:41 -0400 | [diff] [blame] | 4588 | free_extent_map(em); |
Yan, Zheng | a22285a | 2010-05-16 10:48:46 -0400 | [diff] [blame] | 4589 | em = NULL; |
Yan Zheng | 9036c10 | 2008-10-30 14:19:41 -0400 | [diff] [blame] | 4590 | cur_offset = last_byte; |
Yan, Zheng | 8082510 | 2009-11-12 09:35:36 +0000 | [diff] [blame] | 4591 | if (cur_offset >= block_end) |
Yan Zheng | 9036c10 | 2008-10-30 14:19:41 -0400 | [diff] [blame] | 4592 | break; |
| 4593 | } |
Yan, Zheng | a22285a | 2010-05-16 10:48:46 -0400 | [diff] [blame] | 4594 | free_extent_map(em); |
Josef Bacik | 2ac55d4 | 2010-02-03 19:33:23 +0000 | [diff] [blame] | 4595 | unlock_extent_cached(io_tree, hole_start, block_end - 1, &cached_state, |
| 4596 | GFP_NOFS); |
Yan Zheng | 9036c10 | 2008-10-30 14:19:41 -0400 | [diff] [blame] | 4597 | return err; |
| 4598 | } |
| 4599 | |
Filipe Manana | 9ea24bb | 2014-10-29 11:57:59 +0000 | [diff] [blame] | 4600 | static int wait_snapshoting_atomic_t(atomic_t *a) |
| 4601 | { |
| 4602 | schedule(); |
| 4603 | return 0; |
| 4604 | } |
| 4605 | |
| 4606 | static void wait_for_snapshot_creation(struct btrfs_root *root) |
| 4607 | { |
| 4608 | while (true) { |
| 4609 | int ret; |
| 4610 | |
| 4611 | ret = btrfs_start_write_no_snapshoting(root); |
| 4612 | if (ret) |
| 4613 | break; |
| 4614 | wait_on_atomic_t(&root->will_be_snapshoted, |
| 4615 | wait_snapshoting_atomic_t, |
| 4616 | TASK_UNINTERRUPTIBLE); |
| 4617 | } |
| 4618 | } |
| 4619 | |
Eric Sandeen | 3972f26 | 2013-01-12 02:57:22 +0000 | [diff] [blame] | 4620 | static int btrfs_setsize(struct inode *inode, struct iattr *attr) |
Yan, Zheng | 8082510 | 2009-11-12 09:35:36 +0000 | [diff] [blame] | 4621 | { |
Miao Xie | f4a2f4c | 2011-12-14 20:12:01 -0500 | [diff] [blame] | 4622 | struct btrfs_root *root = BTRFS_I(inode)->root; |
| 4623 | struct btrfs_trans_handle *trans; |
Josef Bacik | a41ad39 | 2011-01-31 15:30:16 -0500 | [diff] [blame] | 4624 | loff_t oldsize = i_size_read(inode); |
Eric Sandeen | 3972f26 | 2013-01-12 02:57:22 +0000 | [diff] [blame] | 4625 | loff_t newsize = attr->ia_size; |
| 4626 | int mask = attr->ia_valid; |
Yan, Zheng | 8082510 | 2009-11-12 09:35:36 +0000 | [diff] [blame] | 4627 | int ret; |
| 4628 | |
Eric Sandeen | 3972f26 | 2013-01-12 02:57:22 +0000 | [diff] [blame] | 4629 | /* |
| 4630 | * The regular truncate() case without ATTR_CTIME and ATTR_MTIME is a |
| 4631 | * special case where we need to update the times despite not having |
| 4632 | * these flags set. For all other operations the VFS set these flags |
| 4633 | * explicitly if it wants a timestamp update. |
| 4634 | */ |
Christoph Hellwig | dff6efc | 2013-11-19 07:17:07 -0800 | [diff] [blame] | 4635 | if (newsize != oldsize) { |
| 4636 | inode_inc_iversion(inode); |
| 4637 | if (!(mask & (ATTR_CTIME | ATTR_MTIME))) |
| 4638 | inode->i_ctime = inode->i_mtime = |
| 4639 | current_fs_time(inode->i_sb); |
| 4640 | } |
Eric Sandeen | 3972f26 | 2013-01-12 02:57:22 +0000 | [diff] [blame] | 4641 | |
Josef Bacik | a41ad39 | 2011-01-31 15:30:16 -0500 | [diff] [blame] | 4642 | if (newsize > oldsize) { |
Kirill A. Shutemov | 7caef26 | 2013-09-12 15:13:56 -0700 | [diff] [blame] | 4643 | truncate_pagecache(inode, newsize); |
Filipe Manana | 9ea24bb | 2014-10-29 11:57:59 +0000 | [diff] [blame] | 4644 | /* |
| 4645 | * Don't do an expanding truncate while snapshoting is ongoing. |
| 4646 | * This is to ensure the snapshot captures a fully consistent |
| 4647 | * state of this file - if the snapshot captures this expanding |
| 4648 | * truncation, it must capture all writes that happened before |
| 4649 | * this truncation. |
| 4650 | */ |
| 4651 | wait_for_snapshot_creation(root); |
Josef Bacik | a41ad39 | 2011-01-31 15:30:16 -0500 | [diff] [blame] | 4652 | ret = btrfs_cont_expand(inode, oldsize, newsize); |
Filipe Manana | 9ea24bb | 2014-10-29 11:57:59 +0000 | [diff] [blame] | 4653 | if (ret) { |
| 4654 | btrfs_end_write_no_snapshoting(root); |
Yan, Zheng | 8082510 | 2009-11-12 09:35:36 +0000 | [diff] [blame] | 4655 | return ret; |
Filipe Manana | 9ea24bb | 2014-10-29 11:57:59 +0000 | [diff] [blame] | 4656 | } |
Yan, Zheng | 8082510 | 2009-11-12 09:35:36 +0000 | [diff] [blame] | 4657 | |
Miao Xie | f4a2f4c | 2011-12-14 20:12:01 -0500 | [diff] [blame] | 4658 | trans = btrfs_start_transaction(root, 1); |
Filipe Manana | 9ea24bb | 2014-10-29 11:57:59 +0000 | [diff] [blame] | 4659 | if (IS_ERR(trans)) { |
| 4660 | btrfs_end_write_no_snapshoting(root); |
Miao Xie | f4a2f4c | 2011-12-14 20:12:01 -0500 | [diff] [blame] | 4661 | return PTR_ERR(trans); |
Filipe Manana | 9ea24bb | 2014-10-29 11:57:59 +0000 | [diff] [blame] | 4662 | } |
Miao Xie | f4a2f4c | 2011-12-14 20:12:01 -0500 | [diff] [blame] | 4663 | |
| 4664 | i_size_write(inode, newsize); |
| 4665 | btrfs_ordered_update_i_size(inode, i_size_read(inode), NULL); |
| 4666 | ret = btrfs_update_inode(trans, root, inode); |
Filipe Manana | 9ea24bb | 2014-10-29 11:57:59 +0000 | [diff] [blame] | 4667 | btrfs_end_write_no_snapshoting(root); |
Josef Bacik | 7ad85bb | 2012-01-12 19:10:12 -0500 | [diff] [blame] | 4668 | btrfs_end_transaction(trans, root); |
Josef Bacik | a41ad39 | 2011-01-31 15:30:16 -0500 | [diff] [blame] | 4669 | } else { |
Yan, Zheng | 8082510 | 2009-11-12 09:35:36 +0000 | [diff] [blame] | 4670 | |
Josef Bacik | a41ad39 | 2011-01-31 15:30:16 -0500 | [diff] [blame] | 4671 | /* |
| 4672 | * We're truncating a file that used to have good data down to |
| 4673 | * zero. Make sure it gets into the ordered flush list so that |
| 4674 | * any new writes get down to disk quickly. |
| 4675 | */ |
| 4676 | if (newsize == 0) |
Josef Bacik | 72ac3c0 | 2012-05-23 14:13:11 -0400 | [diff] [blame] | 4677 | set_bit(BTRFS_INODE_ORDERED_DATA_CLOSE, |
| 4678 | &BTRFS_I(inode)->runtime_flags); |
Yan, Zheng | 8082510 | 2009-11-12 09:35:36 +0000 | [diff] [blame] | 4679 | |
Josef Bacik | f3fe820 | 2013-01-07 17:03:21 -0500 | [diff] [blame] | 4680 | /* |
| 4681 | * 1 for the orphan item we're going to add |
| 4682 | * 1 for the orphan item deletion. |
| 4683 | */ |
| 4684 | trans = btrfs_start_transaction(root, 2); |
| 4685 | if (IS_ERR(trans)) |
| 4686 | return PTR_ERR(trans); |
| 4687 | |
| 4688 | /* |
| 4689 | * We need to do this in case we fail at _any_ point during the |
| 4690 | * actual truncate. Once we do the truncate_setsize we could |
| 4691 | * invalidate pages which forces any outstanding ordered io to |
| 4692 | * be instantly completed which will give us extents that need |
| 4693 | * to be truncated. If we fail to get an orphan inode down we |
| 4694 | * could have left over extents that were never meant to live, |
| 4695 | * so we need to garuntee from this point on that everything |
| 4696 | * will be consistent. |
| 4697 | */ |
| 4698 | ret = btrfs_orphan_add(trans, inode); |
| 4699 | btrfs_end_transaction(trans, root); |
| 4700 | if (ret) |
| 4701 | return ret; |
| 4702 | |
Josef Bacik | a41ad39 | 2011-01-31 15:30:16 -0500 | [diff] [blame] | 4703 | /* we don't support swapfiles, so vmtruncate shouldn't fail */ |
| 4704 | truncate_setsize(inode, newsize); |
Miao Xie | 2e60a51 | 2013-02-08 07:01:08 +0000 | [diff] [blame] | 4705 | |
| 4706 | /* Disable nonlocked read DIO to avoid the end less truncate */ |
| 4707 | btrfs_inode_block_unlocked_dio(inode); |
| 4708 | inode_dio_wait(inode); |
| 4709 | btrfs_inode_resume_unlocked_dio(inode); |
| 4710 | |
Josef Bacik | a41ad39 | 2011-01-31 15:30:16 -0500 | [diff] [blame] | 4711 | ret = btrfs_truncate(inode); |
Josef Bacik | 7f4f6e0 | 2013-08-29 16:43:28 -0400 | [diff] [blame] | 4712 | if (ret && inode->i_nlink) { |
| 4713 | int err; |
| 4714 | |
| 4715 | /* |
| 4716 | * failed to truncate, disk_i_size is only adjusted down |
| 4717 | * as we remove extents, so it should represent the true |
| 4718 | * size of the inode, so reset the in memory size and |
| 4719 | * delete our orphan entry. |
| 4720 | */ |
| 4721 | trans = btrfs_join_transaction(root); |
| 4722 | if (IS_ERR(trans)) { |
| 4723 | btrfs_orphan_del(NULL, inode); |
| 4724 | return ret; |
| 4725 | } |
| 4726 | i_size_write(inode, BTRFS_I(inode)->disk_i_size); |
| 4727 | err = btrfs_orphan_del(trans, inode); |
| 4728 | if (err) |
| 4729 | btrfs_abort_transaction(trans, root, err); |
| 4730 | btrfs_end_transaction(trans, root); |
| 4731 | } |
Yan, Zheng | 8082510 | 2009-11-12 09:35:36 +0000 | [diff] [blame] | 4732 | } |
| 4733 | |
Josef Bacik | a41ad39 | 2011-01-31 15:30:16 -0500 | [diff] [blame] | 4734 | return ret; |
Yan, Zheng | 8082510 | 2009-11-12 09:35:36 +0000 | [diff] [blame] | 4735 | } |
| 4736 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4737 | static int btrfs_setattr(struct dentry *dentry, struct iattr *attr) |
| 4738 | { |
| 4739 | struct inode *inode = dentry->d_inode; |
Li Zefan | b83cc96 | 2010-12-20 16:04:08 +0800 | [diff] [blame] | 4740 | struct btrfs_root *root = BTRFS_I(inode)->root; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4741 | int err; |
| 4742 | |
Li Zefan | b83cc96 | 2010-12-20 16:04:08 +0800 | [diff] [blame] | 4743 | if (btrfs_root_readonly(root)) |
| 4744 | return -EROFS; |
| 4745 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4746 | err = inode_change_ok(inode, attr); |
| 4747 | if (err) |
| 4748 | return err; |
| 4749 | |
Chris Mason | 5a3f23d | 2009-03-31 13:27:11 -0400 | [diff] [blame] | 4750 | if (S_ISREG(inode->i_mode) && (attr->ia_valid & ATTR_SIZE)) { |
Eric Sandeen | 3972f26 | 2013-01-12 02:57:22 +0000 | [diff] [blame] | 4751 | err = btrfs_setsize(inode, attr); |
Yan, Zheng | 8082510 | 2009-11-12 09:35:36 +0000 | [diff] [blame] | 4752 | if (err) |
| 4753 | return err; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4754 | } |
Yan Zheng | 9036c10 | 2008-10-30 14:19:41 -0400 | [diff] [blame] | 4755 | |
Christoph Hellwig | 1025774 | 2010-06-04 11:30:02 +0200 | [diff] [blame] | 4756 | if (attr->ia_valid) { |
| 4757 | setattr_copy(inode, attr); |
Josef Bacik | 0c4d2d9 | 2012-04-05 15:03:02 -0400 | [diff] [blame] | 4758 | inode_inc_iversion(inode); |
Josef Bacik | 22c44fe | 2011-11-30 10:45:38 -0500 | [diff] [blame] | 4759 | err = btrfs_dirty_inode(inode); |
Josef Bacik | 33268ea | 2008-07-24 12:16:36 -0400 | [diff] [blame] | 4760 | |
Josef Bacik | 22c44fe | 2011-11-30 10:45:38 -0500 | [diff] [blame] | 4761 | if (!err && attr->ia_valid & ATTR_MODE) |
Christoph Hellwig | 996a710 | 2013-12-20 05:16:43 -0800 | [diff] [blame] | 4762 | err = posix_acl_chmod(inode, inode->i_mode); |
Christoph Hellwig | 1025774 | 2010-06-04 11:30:02 +0200 | [diff] [blame] | 4763 | } |
| 4764 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4765 | return err; |
| 4766 | } |
Chris Mason | 61295eb | 2008-01-14 16:24:38 -0500 | [diff] [blame] | 4767 | |
Filipe David Borba Manana | 131e404 | 2013-11-19 22:29:35 +0000 | [diff] [blame] | 4768 | /* |
| 4769 | * While truncating the inode pages during eviction, we get the VFS calling |
| 4770 | * btrfs_invalidatepage() against each page of the inode. This is slow because |
| 4771 | * the calls to btrfs_invalidatepage() result in a huge amount of calls to |
| 4772 | * lock_extent_bits() and clear_extent_bit(), which keep merging and splitting |
| 4773 | * extent_state structures over and over, wasting lots of time. |
| 4774 | * |
| 4775 | * Therefore if the inode is being evicted, let btrfs_invalidatepage() skip all |
| 4776 | * those expensive operations on a per page basis and do only the ordered io |
| 4777 | * finishing, while we release here the extent_map and extent_state structures, |
| 4778 | * without the excessive merging and splitting. |
| 4779 | */ |
| 4780 | static void evict_inode_truncate_pages(struct inode *inode) |
| 4781 | { |
| 4782 | struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree; |
| 4783 | struct extent_map_tree *map_tree = &BTRFS_I(inode)->extent_tree; |
| 4784 | struct rb_node *node; |
| 4785 | |
| 4786 | ASSERT(inode->i_state & I_FREEING); |
Johannes Weiner | 91b0abe | 2014-04-03 14:47:49 -0700 | [diff] [blame] | 4787 | truncate_inode_pages_final(&inode->i_data); |
Filipe David Borba Manana | 131e404 | 2013-11-19 22:29:35 +0000 | [diff] [blame] | 4788 | |
| 4789 | write_lock(&map_tree->lock); |
| 4790 | while (!RB_EMPTY_ROOT(&map_tree->map)) { |
| 4791 | struct extent_map *em; |
| 4792 | |
| 4793 | node = rb_first(&map_tree->map); |
| 4794 | em = rb_entry(node, struct extent_map, rb_node); |
Wang Shilong | 180589e | 2013-12-14 15:27:31 +0800 | [diff] [blame] | 4795 | clear_bit(EXTENT_FLAG_PINNED, &em->flags); |
| 4796 | clear_bit(EXTENT_FLAG_LOGGING, &em->flags); |
Filipe David Borba Manana | 131e404 | 2013-11-19 22:29:35 +0000 | [diff] [blame] | 4797 | remove_extent_mapping(map_tree, em); |
| 4798 | free_extent_map(em); |
Filipe Manana | 7064dd5 | 2014-08-08 02:47:05 +0100 | [diff] [blame] | 4799 | if (need_resched()) { |
| 4800 | write_unlock(&map_tree->lock); |
| 4801 | cond_resched(); |
| 4802 | write_lock(&map_tree->lock); |
| 4803 | } |
Filipe David Borba Manana | 131e404 | 2013-11-19 22:29:35 +0000 | [diff] [blame] | 4804 | } |
| 4805 | write_unlock(&map_tree->lock); |
| 4806 | |
| 4807 | spin_lock(&io_tree->lock); |
| 4808 | while (!RB_EMPTY_ROOT(&io_tree->state)) { |
| 4809 | struct extent_state *state; |
| 4810 | struct extent_state *cached_state = NULL; |
| 4811 | |
| 4812 | node = rb_first(&io_tree->state); |
| 4813 | state = rb_entry(node, struct extent_state, rb_node); |
| 4814 | atomic_inc(&state->refs); |
| 4815 | spin_unlock(&io_tree->lock); |
| 4816 | |
| 4817 | lock_extent_bits(io_tree, state->start, state->end, |
| 4818 | 0, &cached_state); |
| 4819 | clear_extent_bit(io_tree, state->start, state->end, |
| 4820 | EXTENT_LOCKED | EXTENT_DIRTY | |
| 4821 | EXTENT_DELALLOC | EXTENT_DO_ACCOUNTING | |
| 4822 | EXTENT_DEFRAG, 1, 1, |
| 4823 | &cached_state, GFP_NOFS); |
| 4824 | free_extent_state(state); |
| 4825 | |
Filipe Manana | 7064dd5 | 2014-08-08 02:47:05 +0100 | [diff] [blame] | 4826 | cond_resched(); |
Filipe David Borba Manana | 131e404 | 2013-11-19 22:29:35 +0000 | [diff] [blame] | 4827 | spin_lock(&io_tree->lock); |
| 4828 | } |
| 4829 | spin_unlock(&io_tree->lock); |
| 4830 | } |
| 4831 | |
Al Viro | bd55597 | 2010-06-07 11:35:40 -0400 | [diff] [blame] | 4832 | void btrfs_evict_inode(struct inode *inode) |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4833 | { |
| 4834 | struct btrfs_trans_handle *trans; |
| 4835 | struct btrfs_root *root = BTRFS_I(inode)->root; |
Josef Bacik | 726c35f | 2011-09-26 15:46:06 -0400 | [diff] [blame] | 4836 | struct btrfs_block_rsv *rsv, *global_rsv; |
Josef Bacik | 0712718 | 2011-08-19 10:29:59 -0400 | [diff] [blame] | 4837 | u64 min_size = btrfs_calc_trunc_metadata_size(root, 1); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4838 | int ret; |
| 4839 | |
liubo | 1abe9b8 | 2011-03-24 11:18:59 +0000 | [diff] [blame] | 4840 | trace_btrfs_inode_evict(inode); |
| 4841 | |
Filipe David Borba Manana | 131e404 | 2013-11-19 22:29:35 +0000 | [diff] [blame] | 4842 | evict_inode_truncate_pages(inode); |
| 4843 | |
Stefan Behrens | 69e9c6c | 2013-09-05 16:58:43 +0200 | [diff] [blame] | 4844 | if (inode->i_nlink && |
| 4845 | ((btrfs_root_refs(&root->root_item) != 0 && |
| 4846 | root->root_key.objectid != BTRFS_ROOT_TREE_OBJECTID) || |
| 4847 | btrfs_is_free_space_inode(inode))) |
Al Viro | bd55597 | 2010-06-07 11:35:40 -0400 | [diff] [blame] | 4848 | goto no_delete; |
| 4849 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4850 | if (is_bad_inode(inode)) { |
Josef Bacik | 7b12876 | 2008-07-24 12:17:14 -0400 | [diff] [blame] | 4851 | btrfs_orphan_del(NULL, inode); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4852 | goto no_delete; |
| 4853 | } |
Al Viro | bd55597 | 2010-06-07 11:35:40 -0400 | [diff] [blame] | 4854 | /* 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] | 4855 | btrfs_wait_ordered_range(inode, 0, (u64)-1); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 4856 | |
Miao Xie | f612496 | 2014-09-12 18:44:04 +0800 | [diff] [blame] | 4857 | btrfs_free_io_failure_record(inode, 0, (u64)-1); |
| 4858 | |
Yan, Zheng | c71bf09 | 2009-11-12 09:34:40 +0000 | [diff] [blame] | 4859 | if (root->fs_info->log_root_recovering) { |
Liu Bo | 6bf0231 | 2012-06-25 21:59:09 -0600 | [diff] [blame] | 4860 | BUG_ON(test_bit(BTRFS_INODE_HAS_ORPHAN_ITEM, |
Josef Bacik | 8a35d95 | 2012-05-23 14:26:42 -0400 | [diff] [blame] | 4861 | &BTRFS_I(inode)->runtime_flags)); |
Yan, Zheng | c71bf09 | 2009-11-12 09:34:40 +0000 | [diff] [blame] | 4862 | goto no_delete; |
| 4863 | } |
| 4864 | |
Yan, Zheng | 76dda93 | 2009-09-21 16:00:26 -0400 | [diff] [blame] | 4865 | if (inode->i_nlink > 0) { |
Stefan Behrens | 69e9c6c | 2013-09-05 16:58:43 +0200 | [diff] [blame] | 4866 | BUG_ON(btrfs_root_refs(&root->root_item) != 0 && |
| 4867 | root->root_key.objectid != BTRFS_ROOT_TREE_OBJECTID); |
Yan, Zheng | 76dda93 | 2009-09-21 16:00:26 -0400 | [diff] [blame] | 4868 | goto no_delete; |
| 4869 | } |
| 4870 | |
Miao Xie | 0e8c36a | 2012-12-19 06:59:51 +0000 | [diff] [blame] | 4871 | ret = btrfs_commit_inode_delayed_inode(inode); |
| 4872 | if (ret) { |
| 4873 | btrfs_orphan_del(NULL, inode); |
| 4874 | goto no_delete; |
| 4875 | } |
| 4876 | |
Miao Xie | 66d8f3d | 2012-09-06 04:02:28 -0600 | [diff] [blame] | 4877 | rsv = btrfs_alloc_block_rsv(root, BTRFS_BLOCK_RSV_TEMP); |
Josef Bacik | 4289a66 | 2011-08-05 13:22:24 -0400 | [diff] [blame] | 4878 | if (!rsv) { |
| 4879 | btrfs_orphan_del(NULL, inode); |
| 4880 | goto no_delete; |
| 4881 | } |
Josef Bacik | 4a33854 | 2011-08-29 11:01:31 -0400 | [diff] [blame] | 4882 | rsv->size = min_size; |
Josef Bacik | ca7e70f | 2012-08-27 17:48:15 -0400 | [diff] [blame] | 4883 | rsv->failfast = 1; |
Josef Bacik | 726c35f | 2011-09-26 15:46:06 -0400 | [diff] [blame] | 4884 | global_rsv = &root->fs_info->global_block_rsv; |
Josef Bacik | 4289a66 | 2011-08-05 13:22:24 -0400 | [diff] [blame] | 4885 | |
Chris Mason | dbe674a | 2008-07-17 12:54:05 -0400 | [diff] [blame] | 4886 | btrfs_i_size_write(inode, 0); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 4887 | |
Josef Bacik | 4289a66 | 2011-08-05 13:22:24 -0400 | [diff] [blame] | 4888 | /* |
Miao Xie | 8407aa4 | 2012-09-07 01:43:32 -0600 | [diff] [blame] | 4889 | * This is a bit simpler than btrfs_truncate since we've already |
| 4890 | * reserved our space for our orphan item in the unlink, so we just |
| 4891 | * need to reserve some slack space in case we add bytes and update |
| 4892 | * inode item when doing the truncate. |
Josef Bacik | 4289a66 | 2011-08-05 13:22:24 -0400 | [diff] [blame] | 4893 | */ |
Yan, Zheng | 8082510 | 2009-11-12 09:35:36 +0000 | [diff] [blame] | 4894 | while (1) { |
Miao Xie | 08e007d | 2012-10-16 11:33:38 +0000 | [diff] [blame] | 4895 | ret = btrfs_block_rsv_refill(root, rsv, min_size, |
| 4896 | BTRFS_RESERVE_FLUSH_LIMIT); |
Yan, Zheng | 8082510 | 2009-11-12 09:35:36 +0000 | [diff] [blame] | 4897 | |
Josef Bacik | 726c35f | 2011-09-26 15:46:06 -0400 | [diff] [blame] | 4898 | /* |
| 4899 | * Try and steal from the global reserve since we will |
| 4900 | * likely not use this space anyway, we want to try as |
| 4901 | * hard as possible to get this to work. |
| 4902 | */ |
| 4903 | if (ret) |
| 4904 | ret = btrfs_block_rsv_migrate(global_rsv, rsv, min_size); |
| 4905 | |
Yan, Zheng | d68fc57 | 2010-05-16 10:49:58 -0400 | [diff] [blame] | 4906 | if (ret) { |
Simon Kirby | c2cf52e | 2013-03-19 22:41:23 +0000 | [diff] [blame] | 4907 | btrfs_warn(root->fs_info, |
| 4908 | "Could not get space for a delete, will truncate on mount %d", |
| 4909 | ret); |
Josef Bacik | 4289a66 | 2011-08-05 13:22:24 -0400 | [diff] [blame] | 4910 | btrfs_orphan_del(NULL, inode); |
| 4911 | btrfs_free_block_rsv(root, rsv); |
| 4912 | goto no_delete; |
Yan, Zheng | d68fc57 | 2010-05-16 10:49:58 -0400 | [diff] [blame] | 4913 | } |
| 4914 | |
Miao Xie | 0e8c36a | 2012-12-19 06:59:51 +0000 | [diff] [blame] | 4915 | trans = btrfs_join_transaction(root); |
Josef Bacik | 4289a66 | 2011-08-05 13:22:24 -0400 | [diff] [blame] | 4916 | if (IS_ERR(trans)) { |
| 4917 | btrfs_orphan_del(NULL, inode); |
| 4918 | btrfs_free_block_rsv(root, rsv); |
| 4919 | goto no_delete; |
| 4920 | } |
| 4921 | |
| 4922 | trans->block_rsv = rsv; |
| 4923 | |
Yan, Zheng | d68fc57 | 2010-05-16 10:49:58 -0400 | [diff] [blame] | 4924 | ret = btrfs_truncate_inode_items(trans, root, inode, 0, 0); |
Josef Bacik | ca7e70f | 2012-08-27 17:48:15 -0400 | [diff] [blame] | 4925 | if (ret != -ENOSPC) |
Yan, Zheng | 8082510 | 2009-11-12 09:35:36 +0000 | [diff] [blame] | 4926 | break; |
| 4927 | |
Miao Xie | 8407aa4 | 2012-09-07 01:43:32 -0600 | [diff] [blame] | 4928 | trans->block_rsv = &root->fs_info->trans_block_rsv; |
Yan, Zheng | 8082510 | 2009-11-12 09:35:36 +0000 | [diff] [blame] | 4929 | btrfs_end_transaction(trans, root); |
| 4930 | trans = NULL; |
Liu Bo | b53d3f5 | 2012-11-14 14:34:34 +0000 | [diff] [blame] | 4931 | btrfs_btree_balance_dirty(root); |
Josef Bacik | 7b12876 | 2008-07-24 12:17:14 -0400 | [diff] [blame] | 4932 | } |
| 4933 | |
Josef Bacik | 4289a66 | 2011-08-05 13:22:24 -0400 | [diff] [blame] | 4934 | btrfs_free_block_rsv(root, rsv); |
| 4935 | |
Josef Bacik | 4ef31a4 | 2013-08-13 14:10:08 -0400 | [diff] [blame] | 4936 | /* |
| 4937 | * Errors here aren't a big deal, it just means we leave orphan items |
| 4938 | * in the tree. They will be cleaned up on the next mount. |
| 4939 | */ |
Yan, Zheng | 8082510 | 2009-11-12 09:35:36 +0000 | [diff] [blame] | 4940 | if (ret == 0) { |
Josef Bacik | 4289a66 | 2011-08-05 13:22:24 -0400 | [diff] [blame] | 4941 | trans->block_rsv = root->orphan_block_rsv; |
Josef Bacik | 4ef31a4 | 2013-08-13 14:10:08 -0400 | [diff] [blame] | 4942 | btrfs_orphan_del(trans, inode); |
| 4943 | } else { |
| 4944 | btrfs_orphan_del(NULL, inode); |
Yan, Zheng | 8082510 | 2009-11-12 09:35:36 +0000 | [diff] [blame] | 4945 | } |
Chris Mason | 85e21ba | 2008-01-29 15:11:36 -0500 | [diff] [blame] | 4946 | |
Josef Bacik | 4289a66 | 2011-08-05 13:22:24 -0400 | [diff] [blame] | 4947 | trans->block_rsv = &root->fs_info->trans_block_rsv; |
Li Zefan | 581bb05 | 2011-04-20 10:06:11 +0800 | [diff] [blame] | 4948 | if (!(root == root->fs_info->tree_root || |
| 4949 | root->root_key.objectid == BTRFS_TREE_RELOC_OBJECTID)) |
Li Zefan | 33345d01 | 2011-04-20 10:31:50 +0800 | [diff] [blame] | 4950 | btrfs_return_ino(root, btrfs_ino(inode)); |
Li Zefan | 581bb05 | 2011-04-20 10:06:11 +0800 | [diff] [blame] | 4951 | |
Chris Mason | 54aa1f4 | 2007-06-22 14:16:25 -0400 | [diff] [blame] | 4952 | btrfs_end_transaction(trans, root); |
Liu Bo | b53d3f5 | 2012-11-14 14:34:34 +0000 | [diff] [blame] | 4953 | btrfs_btree_balance_dirty(root); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4954 | no_delete: |
Miao Xie | 89042e5 | 2013-05-15 07:48:15 +0000 | [diff] [blame] | 4955 | btrfs_remove_delayed_node(inode); |
Jan Kara | dbd5768 | 2012-05-03 14:48:02 +0200 | [diff] [blame] | 4956 | clear_inode(inode); |
Yan, Zheng | 8082510 | 2009-11-12 09:35:36 +0000 | [diff] [blame] | 4957 | return; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4958 | } |
| 4959 | |
| 4960 | /* |
| 4961 | * this returns the key found in the dir entry in the location pointer. |
| 4962 | * If no dir entries were found, location->objectid is 0. |
| 4963 | */ |
| 4964 | static int btrfs_inode_by_name(struct inode *dir, struct dentry *dentry, |
| 4965 | struct btrfs_key *location) |
| 4966 | { |
| 4967 | const char *name = dentry->d_name.name; |
| 4968 | int namelen = dentry->d_name.len; |
| 4969 | struct btrfs_dir_item *di; |
| 4970 | struct btrfs_path *path; |
| 4971 | struct btrfs_root *root = BTRFS_I(dir)->root; |
Yan | 0d9f7f3 | 2007-10-25 15:48:28 -0400 | [diff] [blame] | 4972 | int ret = 0; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4973 | |
| 4974 | path = btrfs_alloc_path(); |
Mark Fasheh | d8926bb | 2011-07-13 10:38:47 -0700 | [diff] [blame] | 4975 | if (!path) |
| 4976 | return -ENOMEM; |
Chris Mason | 3954401 | 2007-12-12 14:38:19 -0500 | [diff] [blame] | 4977 | |
Li Zefan | 33345d01 | 2011-04-20 10:31:50 +0800 | [diff] [blame] | 4978 | di = btrfs_lookup_dir_item(NULL, root, path, btrfs_ino(dir), name, |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4979 | namelen, 0); |
Yan | 0d9f7f3 | 2007-10-25 15:48:28 -0400 | [diff] [blame] | 4980 | if (IS_ERR(di)) |
| 4981 | ret = PTR_ERR(di); |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 4982 | |
David Sterba | c704005 | 2011-04-19 18:00:01 +0200 | [diff] [blame] | 4983 | if (IS_ERR_OR_NULL(di)) |
Chris Mason | 3954401 | 2007-12-12 14:38:19 -0500 | [diff] [blame] | 4984 | goto out_err; |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 4985 | |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 4986 | btrfs_dir_item_key_to_cpu(path->nodes[0], di, location); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4987 | out: |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4988 | btrfs_free_path(path); |
| 4989 | return ret; |
Chris Mason | 3954401 | 2007-12-12 14:38:19 -0500 | [diff] [blame] | 4990 | out_err: |
| 4991 | location->objectid = 0; |
| 4992 | goto out; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4993 | } |
| 4994 | |
| 4995 | /* |
| 4996 | * when we hit a tree root in a directory, the btrfs part of the inode |
| 4997 | * needs to be changed to reflect the root directory of the tree root. This |
| 4998 | * is kind of like crossing a mount point. |
| 4999 | */ |
| 5000 | static int fixup_tree_root_location(struct btrfs_root *root, |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 5001 | struct inode *dir, |
| 5002 | struct dentry *dentry, |
| 5003 | struct btrfs_key *location, |
| 5004 | struct btrfs_root **sub_root) |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5005 | { |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 5006 | struct btrfs_path *path; |
| 5007 | struct btrfs_root *new_root; |
| 5008 | struct btrfs_root_ref *ref; |
| 5009 | struct extent_buffer *leaf; |
| 5010 | int ret; |
| 5011 | int err = 0; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5012 | |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 5013 | path = btrfs_alloc_path(); |
| 5014 | if (!path) { |
| 5015 | err = -ENOMEM; |
| 5016 | goto out; |
| 5017 | } |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5018 | |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 5019 | err = -ENOENT; |
Kelley Nielsen | 75ac2dd | 2013-11-04 19:35:58 -0800 | [diff] [blame] | 5020 | ret = btrfs_find_item(root->fs_info->tree_root, path, |
| 5021 | BTRFS_I(dir)->root->root_key.objectid, |
| 5022 | location->objectid, BTRFS_ROOT_REF_KEY, NULL); |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 5023 | if (ret) { |
| 5024 | if (ret < 0) |
| 5025 | err = ret; |
| 5026 | goto out; |
| 5027 | } |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5028 | |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 5029 | leaf = path->nodes[0]; |
| 5030 | ref = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_root_ref); |
Li Zefan | 33345d01 | 2011-04-20 10:31:50 +0800 | [diff] [blame] | 5031 | if (btrfs_root_ref_dirid(leaf, ref) != btrfs_ino(dir) || |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 5032 | btrfs_root_ref_name_len(leaf, ref) != dentry->d_name.len) |
| 5033 | goto out; |
| 5034 | |
| 5035 | ret = memcmp_extent_buffer(leaf, dentry->d_name.name, |
| 5036 | (unsigned long)(ref + 1), |
| 5037 | dentry->d_name.len); |
| 5038 | if (ret) |
| 5039 | goto out; |
| 5040 | |
David Sterba | b3b4aa7 | 2011-04-21 01:20:15 +0200 | [diff] [blame] | 5041 | btrfs_release_path(path); |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 5042 | |
| 5043 | new_root = btrfs_read_fs_root_no_name(root->fs_info, location); |
| 5044 | if (IS_ERR(new_root)) { |
| 5045 | err = PTR_ERR(new_root); |
| 5046 | goto out; |
| 5047 | } |
| 5048 | |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 5049 | *sub_root = new_root; |
| 5050 | location->objectid = btrfs_root_dirid(&new_root->root_item); |
| 5051 | location->type = BTRFS_INODE_ITEM_KEY; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5052 | location->offset = 0; |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 5053 | err = 0; |
| 5054 | out: |
| 5055 | btrfs_free_path(path); |
| 5056 | return err; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5057 | } |
| 5058 | |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 5059 | static void inode_tree_add(struct inode *inode) |
| 5060 | { |
| 5061 | struct btrfs_root *root = BTRFS_I(inode)->root; |
| 5062 | struct btrfs_inode *entry; |
From: Nick Piggin | 03e860b | 2009-08-21 10:09:44 +0200 | [diff] [blame] | 5063 | struct rb_node **p; |
| 5064 | struct rb_node *parent; |
Filipe David Borba Manana | cef2193 | 2013-09-02 12:19:13 +0100 | [diff] [blame] | 5065 | struct rb_node *new = &BTRFS_I(inode)->rb_node; |
Li Zefan | 33345d01 | 2011-04-20 10:31:50 +0800 | [diff] [blame] | 5066 | u64 ino = btrfs_ino(inode); |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 5067 | |
Al Viro | 1d3382cb | 2010-10-23 15:19:20 -0400 | [diff] [blame] | 5068 | if (inode_unhashed(inode)) |
Yan, Zheng | 76dda93 | 2009-09-21 16:00:26 -0400 | [diff] [blame] | 5069 | return; |
Miao Xie | e1409ce | 2013-05-15 07:48:16 +0000 | [diff] [blame] | 5070 | parent = NULL; |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 5071 | spin_lock(&root->inode_lock); |
Miao Xie | e1409ce | 2013-05-15 07:48:16 +0000 | [diff] [blame] | 5072 | p = &root->inode_tree.rb_node; |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 5073 | while (*p) { |
| 5074 | parent = *p; |
| 5075 | entry = rb_entry(parent, struct btrfs_inode, rb_node); |
| 5076 | |
Li Zefan | 33345d01 | 2011-04-20 10:31:50 +0800 | [diff] [blame] | 5077 | if (ino < btrfs_ino(&entry->vfs_inode)) |
From: Nick Piggin | 03e860b | 2009-08-21 10:09:44 +0200 | [diff] [blame] | 5078 | p = &parent->rb_left; |
Li Zefan | 33345d01 | 2011-04-20 10:31:50 +0800 | [diff] [blame] | 5079 | else if (ino > btrfs_ino(&entry->vfs_inode)) |
From: Nick Piggin | 03e860b | 2009-08-21 10:09:44 +0200 | [diff] [blame] | 5080 | p = &parent->rb_right; |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 5081 | else { |
| 5082 | WARN_ON(!(entry->vfs_inode.i_state & |
Al Viro | a4ffdde | 2010-06-02 17:38:30 -0400 | [diff] [blame] | 5083 | (I_WILL_FREE | I_FREEING))); |
Filipe David Borba Manana | cef2193 | 2013-09-02 12:19:13 +0100 | [diff] [blame] | 5084 | rb_replace_node(parent, new, &root->inode_tree); |
From: Nick Piggin | 03e860b | 2009-08-21 10:09:44 +0200 | [diff] [blame] | 5085 | RB_CLEAR_NODE(parent); |
| 5086 | spin_unlock(&root->inode_lock); |
Filipe David Borba Manana | cef2193 | 2013-09-02 12:19:13 +0100 | [diff] [blame] | 5087 | return; |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 5088 | } |
| 5089 | } |
Filipe David Borba Manana | cef2193 | 2013-09-02 12:19:13 +0100 | [diff] [blame] | 5090 | rb_link_node(new, parent, p); |
| 5091 | rb_insert_color(new, &root->inode_tree); |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 5092 | spin_unlock(&root->inode_lock); |
| 5093 | } |
| 5094 | |
| 5095 | static void inode_tree_del(struct inode *inode) |
| 5096 | { |
| 5097 | struct btrfs_root *root = BTRFS_I(inode)->root; |
Yan, Zheng | 76dda93 | 2009-09-21 16:00:26 -0400 | [diff] [blame] | 5098 | int empty = 0; |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 5099 | |
From: Nick Piggin | 03e860b | 2009-08-21 10:09:44 +0200 | [diff] [blame] | 5100 | spin_lock(&root->inode_lock); |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 5101 | if (!RB_EMPTY_NODE(&BTRFS_I(inode)->rb_node)) { |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 5102 | rb_erase(&BTRFS_I(inode)->rb_node, &root->inode_tree); |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 5103 | RB_CLEAR_NODE(&BTRFS_I(inode)->rb_node); |
Yan, Zheng | 76dda93 | 2009-09-21 16:00:26 -0400 | [diff] [blame] | 5104 | empty = RB_EMPTY_ROOT(&root->inode_tree); |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 5105 | } |
From: Nick Piggin | 03e860b | 2009-08-21 10:09:44 +0200 | [diff] [blame] | 5106 | spin_unlock(&root->inode_lock); |
Yan, Zheng | 76dda93 | 2009-09-21 16:00:26 -0400 | [diff] [blame] | 5107 | |
Stefan Behrens | 69e9c6c | 2013-09-05 16:58:43 +0200 | [diff] [blame] | 5108 | if (empty && btrfs_root_refs(&root->root_item) == 0) { |
Yan, Zheng | 76dda93 | 2009-09-21 16:00:26 -0400 | [diff] [blame] | 5109 | synchronize_srcu(&root->fs_info->subvol_srcu); |
| 5110 | spin_lock(&root->inode_lock); |
| 5111 | empty = RB_EMPTY_ROOT(&root->inode_tree); |
| 5112 | spin_unlock(&root->inode_lock); |
| 5113 | if (empty) |
| 5114 | btrfs_add_dead_root(root); |
| 5115 | } |
| 5116 | } |
| 5117 | |
Jeff Mahoney | 143bede | 2012-03-01 14:56:26 +0100 | [diff] [blame] | 5118 | void btrfs_invalidate_inodes(struct btrfs_root *root) |
Yan, Zheng | 76dda93 | 2009-09-21 16:00:26 -0400 | [diff] [blame] | 5119 | { |
| 5120 | struct rb_node *node; |
| 5121 | struct rb_node *prev; |
| 5122 | struct btrfs_inode *entry; |
| 5123 | struct inode *inode; |
| 5124 | u64 objectid = 0; |
| 5125 | |
Liu Bo | 7813b3d | 2014-02-10 17:37:25 +0800 | [diff] [blame] | 5126 | if (!test_bit(BTRFS_FS_STATE_ERROR, &root->fs_info->fs_state)) |
| 5127 | WARN_ON(btrfs_root_refs(&root->root_item) != 0); |
Yan, Zheng | 76dda93 | 2009-09-21 16:00:26 -0400 | [diff] [blame] | 5128 | |
| 5129 | spin_lock(&root->inode_lock); |
| 5130 | again: |
| 5131 | node = root->inode_tree.rb_node; |
| 5132 | prev = NULL; |
| 5133 | while (node) { |
| 5134 | prev = node; |
| 5135 | entry = rb_entry(node, struct btrfs_inode, rb_node); |
| 5136 | |
Li Zefan | 33345d01 | 2011-04-20 10:31:50 +0800 | [diff] [blame] | 5137 | if (objectid < btrfs_ino(&entry->vfs_inode)) |
Yan, Zheng | 76dda93 | 2009-09-21 16:00:26 -0400 | [diff] [blame] | 5138 | node = node->rb_left; |
Li Zefan | 33345d01 | 2011-04-20 10:31:50 +0800 | [diff] [blame] | 5139 | else if (objectid > btrfs_ino(&entry->vfs_inode)) |
Yan, Zheng | 76dda93 | 2009-09-21 16:00:26 -0400 | [diff] [blame] | 5140 | node = node->rb_right; |
| 5141 | else |
| 5142 | break; |
| 5143 | } |
| 5144 | if (!node) { |
| 5145 | while (prev) { |
| 5146 | entry = rb_entry(prev, struct btrfs_inode, rb_node); |
Li Zefan | 33345d01 | 2011-04-20 10:31:50 +0800 | [diff] [blame] | 5147 | if (objectid <= btrfs_ino(&entry->vfs_inode)) { |
Yan, Zheng | 76dda93 | 2009-09-21 16:00:26 -0400 | [diff] [blame] | 5148 | node = prev; |
| 5149 | break; |
| 5150 | } |
| 5151 | prev = rb_next(prev); |
| 5152 | } |
| 5153 | } |
| 5154 | while (node) { |
| 5155 | entry = rb_entry(node, struct btrfs_inode, rb_node); |
Li Zefan | 33345d01 | 2011-04-20 10:31:50 +0800 | [diff] [blame] | 5156 | objectid = btrfs_ino(&entry->vfs_inode) + 1; |
Yan, Zheng | 76dda93 | 2009-09-21 16:00:26 -0400 | [diff] [blame] | 5157 | inode = igrab(&entry->vfs_inode); |
| 5158 | if (inode) { |
| 5159 | spin_unlock(&root->inode_lock); |
| 5160 | if (atomic_read(&inode->i_count) > 1) |
| 5161 | d_prune_aliases(inode); |
| 5162 | /* |
Al Viro | 45321ac | 2010-06-07 13:43:19 -0400 | [diff] [blame] | 5163 | * btrfs_drop_inode will have it removed from |
Yan, Zheng | 76dda93 | 2009-09-21 16:00:26 -0400 | [diff] [blame] | 5164 | * the inode cache when its usage count |
| 5165 | * hits zero. |
| 5166 | */ |
| 5167 | iput(inode); |
| 5168 | cond_resched(); |
| 5169 | spin_lock(&root->inode_lock); |
| 5170 | goto again; |
| 5171 | } |
| 5172 | |
| 5173 | if (cond_resched_lock(&root->inode_lock)) |
| 5174 | goto again; |
| 5175 | |
| 5176 | node = rb_next(node); |
| 5177 | } |
| 5178 | spin_unlock(&root->inode_lock); |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 5179 | } |
| 5180 | |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 5181 | static int btrfs_init_locked_inode(struct inode *inode, void *p) |
| 5182 | { |
| 5183 | struct btrfs_iget_args *args = p; |
Chris Mason | 90d3e59 | 2014-01-09 17:28:00 -0800 | [diff] [blame] | 5184 | inode->i_ino = args->location->objectid; |
| 5185 | memcpy(&BTRFS_I(inode)->location, args->location, |
| 5186 | sizeof(*args->location)); |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 5187 | BTRFS_I(inode)->root = args->root; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5188 | return 0; |
| 5189 | } |
| 5190 | |
| 5191 | static int btrfs_find_actor(struct inode *inode, void *opaque) |
| 5192 | { |
| 5193 | struct btrfs_iget_args *args = opaque; |
Chris Mason | 90d3e59 | 2014-01-09 17:28:00 -0800 | [diff] [blame] | 5194 | return args->location->objectid == BTRFS_I(inode)->location.objectid && |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 5195 | args->root == BTRFS_I(inode)->root; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5196 | } |
| 5197 | |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 5198 | static struct inode *btrfs_iget_locked(struct super_block *s, |
Chris Mason | 90d3e59 | 2014-01-09 17:28:00 -0800 | [diff] [blame] | 5199 | struct btrfs_key *location, |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 5200 | struct btrfs_root *root) |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5201 | { |
| 5202 | struct inode *inode; |
| 5203 | struct btrfs_iget_args args; |
Chris Mason | 90d3e59 | 2014-01-09 17:28:00 -0800 | [diff] [blame] | 5204 | unsigned long hashval = btrfs_inode_hash(location->objectid, root); |
Filipe David Borba Manana | 778ba82 | 2013-10-06 22:22:33 +0100 | [diff] [blame] | 5205 | |
Chris Mason | 90d3e59 | 2014-01-09 17:28:00 -0800 | [diff] [blame] | 5206 | args.location = location; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5207 | args.root = root; |
| 5208 | |
Filipe David Borba Manana | 778ba82 | 2013-10-06 22:22:33 +0100 | [diff] [blame] | 5209 | inode = iget5_locked(s, hashval, btrfs_find_actor, |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5210 | btrfs_init_locked_inode, |
| 5211 | (void *)&args); |
| 5212 | return inode; |
| 5213 | } |
| 5214 | |
Balaji Rao | 1a54ef8 | 2008-07-21 02:01:04 +0530 | [diff] [blame] | 5215 | /* Get an inode object given its location and corresponding root. |
| 5216 | * Returns in *is_new if the inode was read from disk |
| 5217 | */ |
| 5218 | struct inode *btrfs_iget(struct super_block *s, struct btrfs_key *location, |
Josef Bacik | 73f7341 | 2009-12-04 17:38:27 +0000 | [diff] [blame] | 5219 | struct btrfs_root *root, int *new) |
Balaji Rao | 1a54ef8 | 2008-07-21 02:01:04 +0530 | [diff] [blame] | 5220 | { |
| 5221 | struct inode *inode; |
| 5222 | |
Chris Mason | 90d3e59 | 2014-01-09 17:28:00 -0800 | [diff] [blame] | 5223 | inode = btrfs_iget_locked(s, location, root); |
Balaji Rao | 1a54ef8 | 2008-07-21 02:01:04 +0530 | [diff] [blame] | 5224 | if (!inode) |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 5225 | return ERR_PTR(-ENOMEM); |
Balaji Rao | 1a54ef8 | 2008-07-21 02:01:04 +0530 | [diff] [blame] | 5226 | |
| 5227 | if (inode->i_state & I_NEW) { |
Balaji Rao | 1a54ef8 | 2008-07-21 02:01:04 +0530 | [diff] [blame] | 5228 | btrfs_read_locked_inode(inode); |
Mark Fasheh | 1748f84 | 2011-07-12 11:25:31 -0700 | [diff] [blame] | 5229 | if (!is_bad_inode(inode)) { |
| 5230 | inode_tree_add(inode); |
| 5231 | unlock_new_inode(inode); |
| 5232 | if (new) |
| 5233 | *new = 1; |
| 5234 | } else { |
Sergei Trofimovich | e0b6d65 | 2011-09-11 10:52:24 -0400 | [diff] [blame] | 5235 | unlock_new_inode(inode); |
| 5236 | iput(inode); |
| 5237 | inode = ERR_PTR(-ESTALE); |
Mark Fasheh | 1748f84 | 2011-07-12 11:25:31 -0700 | [diff] [blame] | 5238 | } |
| 5239 | } |
| 5240 | |
Balaji Rao | 1a54ef8 | 2008-07-21 02:01:04 +0530 | [diff] [blame] | 5241 | return inode; |
| 5242 | } |
| 5243 | |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 5244 | static struct inode *new_simple_dir(struct super_block *s, |
| 5245 | struct btrfs_key *key, |
| 5246 | struct btrfs_root *root) |
| 5247 | { |
| 5248 | struct inode *inode = new_inode(s); |
| 5249 | |
| 5250 | if (!inode) |
| 5251 | return ERR_PTR(-ENOMEM); |
| 5252 | |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 5253 | BTRFS_I(inode)->root = root; |
| 5254 | memcpy(&BTRFS_I(inode)->location, key, sizeof(*key)); |
Josef Bacik | 72ac3c0 | 2012-05-23 14:13:11 -0400 | [diff] [blame] | 5255 | set_bit(BTRFS_INODE_DUMMY, &BTRFS_I(inode)->runtime_flags); |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 5256 | |
| 5257 | inode->i_ino = BTRFS_EMPTY_SUBVOL_DIR_OBJECTID; |
Li Zefan | 848cce0 | 2012-02-21 17:04:28 +0800 | [diff] [blame] | 5258 | inode->i_op = &btrfs_dir_ro_inode_operations; |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 5259 | inode->i_fop = &simple_dir_operations; |
| 5260 | inode->i_mode = S_IFDIR | S_IRUGO | S_IWUSR | S_IXUGO; |
| 5261 | inode->i_mtime = inode->i_atime = inode->i_ctime = CURRENT_TIME; |
| 5262 | |
| 5263 | return inode; |
| 5264 | } |
| 5265 | |
Chris Mason | 3de4586 | 2008-11-17 21:02:50 -0500 | [diff] [blame] | 5266 | struct inode *btrfs_lookup_dentry(struct inode *dir, struct dentry *dentry) |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5267 | { |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 5268 | struct inode *inode; |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 5269 | struct btrfs_root *root = BTRFS_I(dir)->root; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5270 | struct btrfs_root *sub_root = root; |
| 5271 | struct btrfs_key location; |
Yan, Zheng | 76dda93 | 2009-09-21 16:00:26 -0400 | [diff] [blame] | 5272 | int index; |
Josef Bacik | b4aff1f | 2011-06-28 16:18:59 -0400 | [diff] [blame] | 5273 | int ret = 0; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5274 | |
| 5275 | if (dentry->d_name.len > BTRFS_NAME_LEN) |
| 5276 | return ERR_PTR(-ENAMETOOLONG); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 5277 | |
Jeff Layton | 39e3c95 | 2012-11-28 11:30:53 -0500 | [diff] [blame] | 5278 | ret = btrfs_inode_by_name(dir, dentry, &location); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5279 | if (ret < 0) |
| 5280 | return ERR_PTR(ret); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 5281 | |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 5282 | if (location.objectid == 0) |
Tsutomu Itoh | 5662344b3 | 2013-12-13 09:51:42 +0900 | [diff] [blame] | 5283 | return ERR_PTR(-ENOENT); |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 5284 | |
| 5285 | if (location.type == BTRFS_INODE_ITEM_KEY) { |
Josef Bacik | 73f7341 | 2009-12-04 17:38:27 +0000 | [diff] [blame] | 5286 | inode = btrfs_iget(dir->i_sb, &location, root, NULL); |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 5287 | return inode; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5288 | } |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 5289 | |
| 5290 | BUG_ON(location.type != BTRFS_ROOT_ITEM_KEY); |
| 5291 | |
Yan, Zheng | 76dda93 | 2009-09-21 16:00:26 -0400 | [diff] [blame] | 5292 | index = srcu_read_lock(&root->fs_info->subvol_srcu); |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 5293 | ret = fixup_tree_root_location(root, dir, dentry, |
| 5294 | &location, &sub_root); |
| 5295 | if (ret < 0) { |
| 5296 | if (ret != -ENOENT) |
| 5297 | inode = ERR_PTR(ret); |
| 5298 | else |
| 5299 | inode = new_simple_dir(dir->i_sb, &location, sub_root); |
| 5300 | } else { |
Josef Bacik | 73f7341 | 2009-12-04 17:38:27 +0000 | [diff] [blame] | 5301 | inode = btrfs_iget(dir->i_sb, &location, sub_root, NULL); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5302 | } |
Yan, Zheng | 76dda93 | 2009-09-21 16:00:26 -0400 | [diff] [blame] | 5303 | srcu_read_unlock(&root->fs_info->subvol_srcu, index); |
| 5304 | |
Julia Lawall | 34d19ba | 2011-01-24 19:55:19 +0000 | [diff] [blame] | 5305 | if (!IS_ERR(inode) && root != sub_root) { |
Yan, Zheng | c71bf09 | 2009-11-12 09:34:40 +0000 | [diff] [blame] | 5306 | down_read(&root->fs_info->cleanup_work_sem); |
| 5307 | if (!(inode->i_sb->s_flags & MS_RDONLY)) |
Josef Bacik | 66b4ffd | 2011-01-31 16:22:42 -0500 | [diff] [blame] | 5308 | ret = btrfs_orphan_cleanup(sub_root); |
Yan, Zheng | c71bf09 | 2009-11-12 09:34:40 +0000 | [diff] [blame] | 5309 | up_read(&root->fs_info->cleanup_work_sem); |
Josef Bacik | 01cd336 | 2013-06-03 21:39:49 -0400 | [diff] [blame] | 5310 | if (ret) { |
| 5311 | iput(inode); |
Josef Bacik | 66b4ffd | 2011-01-31 16:22:42 -0500 | [diff] [blame] | 5312 | inode = ERR_PTR(ret); |
Josef Bacik | 01cd336 | 2013-06-03 21:39:49 -0400 | [diff] [blame] | 5313 | } |
Yan, Zheng | c71bf09 | 2009-11-12 09:34:40 +0000 | [diff] [blame] | 5314 | } |
| 5315 | |
Chris Mason | 3de4586 | 2008-11-17 21:02:50 -0500 | [diff] [blame] | 5316 | return inode; |
| 5317 | } |
| 5318 | |
Nick Piggin | fe15ce4 | 2011-01-07 17:49:23 +1100 | [diff] [blame] | 5319 | static int btrfs_dentry_delete(const struct dentry *dentry) |
Yan, Zheng | 76dda93 | 2009-09-21 16:00:26 -0400 | [diff] [blame] | 5320 | { |
| 5321 | struct btrfs_root *root; |
Li Zefan | 848cce0 | 2012-02-21 17:04:28 +0800 | [diff] [blame] | 5322 | struct inode *inode = dentry->d_inode; |
Yan, Zheng | 76dda93 | 2009-09-21 16:00:26 -0400 | [diff] [blame] | 5323 | |
Li Zefan | 848cce0 | 2012-02-21 17:04:28 +0800 | [diff] [blame] | 5324 | if (!inode && !IS_ROOT(dentry)) |
| 5325 | inode = dentry->d_parent->d_inode; |
Yan, Zheng | 76dda93 | 2009-09-21 16:00:26 -0400 | [diff] [blame] | 5326 | |
Li Zefan | 848cce0 | 2012-02-21 17:04:28 +0800 | [diff] [blame] | 5327 | if (inode) { |
| 5328 | root = BTRFS_I(inode)->root; |
Yan, Zheng | efefb14 | 2009-10-09 09:25:16 -0400 | [diff] [blame] | 5329 | if (btrfs_root_refs(&root->root_item) == 0) |
| 5330 | return 1; |
Li Zefan | 848cce0 | 2012-02-21 17:04:28 +0800 | [diff] [blame] | 5331 | |
| 5332 | if (btrfs_ino(inode) == BTRFS_EMPTY_SUBVOL_DIR_OBJECTID) |
| 5333 | return 1; |
Yan, Zheng | efefb14 | 2009-10-09 09:25:16 -0400 | [diff] [blame] | 5334 | } |
Yan, Zheng | 76dda93 | 2009-09-21 16:00:26 -0400 | [diff] [blame] | 5335 | return 0; |
| 5336 | } |
| 5337 | |
Josef Bacik | b4aff1f | 2011-06-28 16:18:59 -0400 | [diff] [blame] | 5338 | static void btrfs_dentry_release(struct dentry *dentry) |
| 5339 | { |
Daeseok Youn | 944a451 | 2014-04-14 15:37:02 +0900 | [diff] [blame] | 5340 | kfree(dentry->d_fsdata); |
Josef Bacik | b4aff1f | 2011-06-28 16:18:59 -0400 | [diff] [blame] | 5341 | } |
| 5342 | |
Chris Mason | 3de4586 | 2008-11-17 21:02:50 -0500 | [diff] [blame] | 5343 | static struct dentry *btrfs_lookup(struct inode *dir, struct dentry *dentry, |
Al Viro | 00cd8dd | 2012-06-10 17:13:09 -0400 | [diff] [blame] | 5344 | unsigned int flags) |
Chris Mason | 3de4586 | 2008-11-17 21:02:50 -0500 | [diff] [blame] | 5345 | { |
Tsutomu Itoh | 5662344b3 | 2013-12-13 09:51:42 +0900 | [diff] [blame] | 5346 | struct inode *inode; |
Josef Bacik | a66e7cc | 2011-09-18 10:34:03 -0400 | [diff] [blame] | 5347 | |
Tsutomu Itoh | 5662344b3 | 2013-12-13 09:51:42 +0900 | [diff] [blame] | 5348 | inode = btrfs_lookup_dentry(dir, dentry); |
| 5349 | if (IS_ERR(inode)) { |
| 5350 | if (PTR_ERR(inode) == -ENOENT) |
| 5351 | inode = NULL; |
| 5352 | else |
| 5353 | return ERR_CAST(inode); |
| 5354 | } |
| 5355 | |
Al Viro | 41d28bc | 2014-10-12 22:24:21 -0400 | [diff] [blame] | 5356 | return d_splice_alias(inode, dentry); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5357 | } |
| 5358 | |
Miao Xie | 16cdcec | 2011-04-22 18:12:22 +0800 | [diff] [blame] | 5359 | unsigned char btrfs_filetype_table[] = { |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5360 | DT_UNKNOWN, DT_REG, DT_DIR, DT_CHR, DT_BLK, DT_FIFO, DT_SOCK, DT_LNK |
| 5361 | }; |
| 5362 | |
Al Viro | 9cdda8d | 2013-05-22 16:48:09 -0400 | [diff] [blame] | 5363 | static int btrfs_real_readdir(struct file *file, struct dir_context *ctx) |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5364 | { |
Al Viro | 9cdda8d | 2013-05-22 16:48:09 -0400 | [diff] [blame] | 5365 | struct inode *inode = file_inode(file); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5366 | struct btrfs_root *root = BTRFS_I(inode)->root; |
| 5367 | struct btrfs_item *item; |
| 5368 | struct btrfs_dir_item *di; |
| 5369 | struct btrfs_key key; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 5370 | struct btrfs_key found_key; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5371 | struct btrfs_path *path; |
Miao Xie | 16cdcec | 2011-04-22 18:12:22 +0800 | [diff] [blame] | 5372 | struct list_head ins_list; |
| 5373 | struct list_head del_list; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5374 | int ret; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 5375 | struct extent_buffer *leaf; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5376 | int slot; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5377 | unsigned char d_type; |
| 5378 | int over = 0; |
| 5379 | u32 di_cur; |
| 5380 | u32 di_total; |
| 5381 | u32 di_len; |
| 5382 | int key_type = BTRFS_DIR_INDEX_KEY; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 5383 | char tmp_name[32]; |
| 5384 | char *name_ptr; |
| 5385 | int name_len; |
Al Viro | 9cdda8d | 2013-05-22 16:48:09 -0400 | [diff] [blame] | 5386 | int is_curr = 0; /* ctx->pos points to the current index? */ |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5387 | |
| 5388 | /* FIXME, use a real flag for deciding about the key type */ |
| 5389 | if (root->fs_info->tree_root == root) |
| 5390 | key_type = BTRFS_DIR_ITEM_KEY; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 5391 | |
Al Viro | 9cdda8d | 2013-05-22 16:48:09 -0400 | [diff] [blame] | 5392 | if (!dir_emit_dots(file, ctx)) |
| 5393 | return 0; |
| 5394 | |
David Woodhouse | 49593bf | 2008-08-17 17:08:36 +0100 | [diff] [blame] | 5395 | path = btrfs_alloc_path(); |
Miao Xie | 16cdcec | 2011-04-22 18:12:22 +0800 | [diff] [blame] | 5396 | if (!path) |
| 5397 | return -ENOMEM; |
Chris Mason | ff5714c | 2011-05-28 07:00:39 -0400 | [diff] [blame] | 5398 | |
Josef Bacik | 026fd31 | 2011-05-13 10:32:11 -0400 | [diff] [blame] | 5399 | path->reada = 1; |
David Woodhouse | 49593bf | 2008-08-17 17:08:36 +0100 | [diff] [blame] | 5400 | |
Miao Xie | 16cdcec | 2011-04-22 18:12:22 +0800 | [diff] [blame] | 5401 | if (key_type == BTRFS_DIR_INDEX_KEY) { |
| 5402 | INIT_LIST_HEAD(&ins_list); |
| 5403 | INIT_LIST_HEAD(&del_list); |
| 5404 | btrfs_get_delayed_items(inode, &ins_list, &del_list); |
| 5405 | } |
| 5406 | |
David Sterba | 962a298 | 2014-06-04 18:41:45 +0200 | [diff] [blame] | 5407 | key.type = key_type; |
Al Viro | 9cdda8d | 2013-05-22 16:48:09 -0400 | [diff] [blame] | 5408 | key.offset = ctx->pos; |
Li Zefan | 33345d01 | 2011-04-20 10:31:50 +0800 | [diff] [blame] | 5409 | key.objectid = btrfs_ino(inode); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 5410 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5411 | ret = btrfs_search_slot(NULL, root, &key, path, 0, 0); |
| 5412 | if (ret < 0) |
| 5413 | goto err; |
David Woodhouse | 49593bf | 2008-08-17 17:08:36 +0100 | [diff] [blame] | 5414 | |
| 5415 | while (1) { |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 5416 | leaf = path->nodes[0]; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5417 | slot = path->slots[0]; |
Li Zefan | b9e03af | 2011-03-23 10:43:58 +0800 | [diff] [blame] | 5418 | if (slot >= btrfs_header_nritems(leaf)) { |
| 5419 | ret = btrfs_next_leaf(root, path); |
| 5420 | if (ret < 0) |
| 5421 | goto err; |
| 5422 | else if (ret > 0) |
| 5423 | break; |
| 5424 | continue; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5425 | } |
Chris Mason | 3de4586 | 2008-11-17 21:02:50 -0500 | [diff] [blame] | 5426 | |
Ross Kirk | dd3cc16 | 2013-09-16 15:58:09 +0100 | [diff] [blame] | 5427 | item = btrfs_item_nr(slot); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 5428 | btrfs_item_key_to_cpu(leaf, &found_key, slot); |
| 5429 | |
| 5430 | if (found_key.objectid != key.objectid) |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5431 | break; |
David Sterba | 962a298 | 2014-06-04 18:41:45 +0200 | [diff] [blame] | 5432 | if (found_key.type != key_type) |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5433 | break; |
Al Viro | 9cdda8d | 2013-05-22 16:48:09 -0400 | [diff] [blame] | 5434 | if (found_key.offset < ctx->pos) |
Li Zefan | b9e03af | 2011-03-23 10:43:58 +0800 | [diff] [blame] | 5435 | goto next; |
Miao Xie | 16cdcec | 2011-04-22 18:12:22 +0800 | [diff] [blame] | 5436 | if (key_type == BTRFS_DIR_INDEX_KEY && |
| 5437 | btrfs_should_delete_dir_index(&del_list, |
| 5438 | found_key.offset)) |
| 5439 | goto next; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 5440 | |
Al Viro | 9cdda8d | 2013-05-22 16:48:09 -0400 | [diff] [blame] | 5441 | ctx->pos = found_key.offset; |
Miao Xie | 16cdcec | 2011-04-22 18:12:22 +0800 | [diff] [blame] | 5442 | is_curr = 1; |
David Woodhouse | 49593bf | 2008-08-17 17:08:36 +0100 | [diff] [blame] | 5443 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5444 | di = btrfs_item_ptr(leaf, slot, struct btrfs_dir_item); |
| 5445 | di_cur = 0; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 5446 | di_total = btrfs_item_size(leaf, item); |
David Woodhouse | 49593bf | 2008-08-17 17:08:36 +0100 | [diff] [blame] | 5447 | |
| 5448 | while (di_cur < di_total) { |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 5449 | struct btrfs_key location; |
| 5450 | |
Josef Bacik | 22a94d4 | 2011-03-16 16:47:17 -0400 | [diff] [blame] | 5451 | if (verify_dir_item(root, leaf, di)) |
| 5452 | break; |
| 5453 | |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 5454 | name_len = btrfs_dir_name_len(leaf, di); |
David Woodhouse | 49593bf | 2008-08-17 17:08:36 +0100 | [diff] [blame] | 5455 | if (name_len <= sizeof(tmp_name)) { |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 5456 | name_ptr = tmp_name; |
| 5457 | } else { |
| 5458 | name_ptr = kmalloc(name_len, GFP_NOFS); |
David Woodhouse | 49593bf | 2008-08-17 17:08:36 +0100 | [diff] [blame] | 5459 | if (!name_ptr) { |
| 5460 | ret = -ENOMEM; |
| 5461 | goto err; |
| 5462 | } |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 5463 | } |
| 5464 | read_extent_buffer(leaf, name_ptr, |
| 5465 | (unsigned long)(di + 1), name_len); |
| 5466 | |
| 5467 | d_type = btrfs_filetype_table[btrfs_dir_type(leaf, di)]; |
| 5468 | btrfs_dir_item_key_to_cpu(leaf, di, &location); |
Chris Mason | 3de4586 | 2008-11-17 21:02:50 -0500 | [diff] [blame] | 5469 | |
Josef Bacik | b4aff1f | 2011-06-28 16:18:59 -0400 | [diff] [blame] | 5470 | |
Chris Mason | 3de4586 | 2008-11-17 21:02:50 -0500 | [diff] [blame] | 5471 | /* is this a reference to our own snapshot? If so |
Arne Jansen | 8c9c2bf | 2012-02-25 09:09:30 +0100 | [diff] [blame] | 5472 | * skip it. |
| 5473 | * |
| 5474 | * In contrast to old kernels, we insert the snapshot's |
| 5475 | * dir item and dir index after it has been created, so |
| 5476 | * we won't find a reference to our own snapshot. We |
| 5477 | * still keep the following code for backward |
| 5478 | * compatibility. |
Chris Mason | 3de4586 | 2008-11-17 21:02:50 -0500 | [diff] [blame] | 5479 | */ |
| 5480 | if (location.type == BTRFS_ROOT_ITEM_KEY && |
| 5481 | location.objectid == root->root_key.objectid) { |
| 5482 | over = 0; |
| 5483 | goto skip; |
| 5484 | } |
Al Viro | 9cdda8d | 2013-05-22 16:48:09 -0400 | [diff] [blame] | 5485 | over = !dir_emit(ctx, name_ptr, name_len, |
| 5486 | location.objectid, d_type); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 5487 | |
Chris Mason | 3de4586 | 2008-11-17 21:02:50 -0500 | [diff] [blame] | 5488 | skip: |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 5489 | if (name_ptr != tmp_name) |
| 5490 | kfree(name_ptr); |
| 5491 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5492 | if (over) |
| 5493 | goto nopos; |
Josef Bacik | 5103e94 | 2007-11-16 11:45:54 -0500 | [diff] [blame] | 5494 | di_len = btrfs_dir_name_len(leaf, di) + |
David Woodhouse | 49593bf | 2008-08-17 17:08:36 +0100 | [diff] [blame] | 5495 | btrfs_dir_data_len(leaf, di) + sizeof(*di); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5496 | di_cur += di_len; |
| 5497 | di = (struct btrfs_dir_item *)((char *)di + di_len); |
| 5498 | } |
Li Zefan | b9e03af | 2011-03-23 10:43:58 +0800 | [diff] [blame] | 5499 | next: |
| 5500 | path->slots[0]++; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5501 | } |
David Woodhouse | 49593bf | 2008-08-17 17:08:36 +0100 | [diff] [blame] | 5502 | |
Miao Xie | 16cdcec | 2011-04-22 18:12:22 +0800 | [diff] [blame] | 5503 | if (key_type == BTRFS_DIR_INDEX_KEY) { |
| 5504 | if (is_curr) |
Al Viro | 9cdda8d | 2013-05-22 16:48:09 -0400 | [diff] [blame] | 5505 | ctx->pos++; |
| 5506 | ret = btrfs_readdir_delayed_dir_index(ctx, &ins_list); |
Miao Xie | 16cdcec | 2011-04-22 18:12:22 +0800 | [diff] [blame] | 5507 | if (ret) |
| 5508 | goto nopos; |
| 5509 | } |
| 5510 | |
David Woodhouse | 49593bf | 2008-08-17 17:08:36 +0100 | [diff] [blame] | 5511 | /* Reached end of directory/root. Bump pos past the last item. */ |
Zach Brown | db62efb | 2013-07-11 16:19:42 -0700 | [diff] [blame] | 5512 | ctx->pos++; |
| 5513 | |
| 5514 | /* |
| 5515 | * Stop new entries from being returned after we return the last |
| 5516 | * entry. |
| 5517 | * |
| 5518 | * New directory entries are assigned a strictly increasing |
| 5519 | * offset. This means that new entries created during readdir |
| 5520 | * are *guaranteed* to be seen in the future by that readdir. |
| 5521 | * This has broken buggy programs which operate on names as |
| 5522 | * they're returned by readdir. Until we re-use freed offsets |
| 5523 | * we have this hack to stop new entries from being returned |
| 5524 | * under the assumption that they'll never reach this huge |
| 5525 | * offset. |
| 5526 | * |
| 5527 | * This is being careful not to overflow 32bit loff_t unless the |
| 5528 | * last entry requires it because doing so has broken 32bit apps |
| 5529 | * in the past. |
| 5530 | */ |
| 5531 | if (key_type == BTRFS_DIR_INDEX_KEY) { |
| 5532 | if (ctx->pos >= INT_MAX) |
| 5533 | ctx->pos = LLONG_MAX; |
| 5534 | else |
| 5535 | ctx->pos = INT_MAX; |
| 5536 | } |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5537 | nopos: |
| 5538 | ret = 0; |
| 5539 | err: |
Miao Xie | 16cdcec | 2011-04-22 18:12:22 +0800 | [diff] [blame] | 5540 | if (key_type == BTRFS_DIR_INDEX_KEY) |
| 5541 | btrfs_put_delayed_items(&ins_list, &del_list); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5542 | btrfs_free_path(path); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5543 | return ret; |
| 5544 | } |
| 5545 | |
Christoph Hellwig | a9185b4 | 2010-03-05 09:21:37 +0100 | [diff] [blame] | 5546 | int btrfs_write_inode(struct inode *inode, struct writeback_control *wbc) |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5547 | { |
| 5548 | struct btrfs_root *root = BTRFS_I(inode)->root; |
| 5549 | struct btrfs_trans_handle *trans; |
| 5550 | int ret = 0; |
Josef Bacik | 0af3d00 | 2010-06-21 14:48:16 -0400 | [diff] [blame] | 5551 | bool nolock = false; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5552 | |
Josef Bacik | 72ac3c0 | 2012-05-23 14:13:11 -0400 | [diff] [blame] | 5553 | if (test_bit(BTRFS_INODE_DUMMY, &BTRFS_I(inode)->runtime_flags)) |
Chris Mason | 4ca8b41 | 2008-08-05 13:30:48 -0400 | [diff] [blame] | 5554 | return 0; |
| 5555 | |
Liu Bo | 83eea1f | 2012-07-10 05:28:39 -0600 | [diff] [blame] | 5556 | 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] | 5557 | nolock = true; |
Josef Bacik | 0af3d00 | 2010-06-21 14:48:16 -0400 | [diff] [blame] | 5558 | |
Christoph Hellwig | a9185b4 | 2010-03-05 09:21:37 +0100 | [diff] [blame] | 5559 | if (wbc->sync_mode == WB_SYNC_ALL) { |
Josef Bacik | 0af3d00 | 2010-06-21 14:48:16 -0400 | [diff] [blame] | 5560 | if (nolock) |
Josef Bacik | 7a7eaa4 | 2011-04-13 12:54:33 -0400 | [diff] [blame] | 5561 | trans = btrfs_join_transaction_nolock(root); |
Josef Bacik | 0af3d00 | 2010-06-21 14:48:16 -0400 | [diff] [blame] | 5562 | else |
Josef Bacik | 7a7eaa4 | 2011-04-13 12:54:33 -0400 | [diff] [blame] | 5563 | trans = btrfs_join_transaction(root); |
Tsutomu Itoh | 3612b49 | 2011-01-25 02:51:38 +0000 | [diff] [blame] | 5564 | if (IS_ERR(trans)) |
| 5565 | return PTR_ERR(trans); |
Miao Xie | a698d075 | 2012-09-20 01:51:59 -0600 | [diff] [blame] | 5566 | ret = btrfs_commit_transaction(trans, root); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5567 | } |
| 5568 | return ret; |
| 5569 | } |
| 5570 | |
| 5571 | /* |
Chris Mason | 54aa1f4 | 2007-06-22 14:16:25 -0400 | [diff] [blame] | 5572 | * This is somewhat expensive, updating the tree every time the |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5573 | * inode changes. But, it is most likely to find the inode in cache. |
| 5574 | * FIXME, needs more benchmarking...there are no reasons other than performance |
| 5575 | * to keep or drop this code. |
| 5576 | */ |
Eric Sandeen | 48a3b63 | 2013-04-25 20:41:01 +0000 | [diff] [blame] | 5577 | static int btrfs_dirty_inode(struct inode *inode) |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5578 | { |
| 5579 | struct btrfs_root *root = BTRFS_I(inode)->root; |
| 5580 | struct btrfs_trans_handle *trans; |
Yan, Zheng | 8929ecfa | 2010-05-16 10:49:58 -0400 | [diff] [blame] | 5581 | int ret; |
| 5582 | |
Josef Bacik | 72ac3c0 | 2012-05-23 14:13:11 -0400 | [diff] [blame] | 5583 | if (test_bit(BTRFS_INODE_DUMMY, &BTRFS_I(inode)->runtime_flags)) |
Josef Bacik | 22c44fe | 2011-11-30 10:45:38 -0500 | [diff] [blame] | 5584 | return 0; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5585 | |
Josef Bacik | 7a7eaa4 | 2011-04-13 12:54:33 -0400 | [diff] [blame] | 5586 | trans = btrfs_join_transaction(root); |
Josef Bacik | 22c44fe | 2011-11-30 10:45:38 -0500 | [diff] [blame] | 5587 | if (IS_ERR(trans)) |
| 5588 | return PTR_ERR(trans); |
Yan, Zheng | 8929ecfa | 2010-05-16 10:49:58 -0400 | [diff] [blame] | 5589 | |
| 5590 | ret = btrfs_update_inode(trans, root, inode); |
Chris Mason | 94b6044 | 2010-05-26 11:02:00 -0400 | [diff] [blame] | 5591 | if (ret && ret == -ENOSPC) { |
| 5592 | /* whoops, lets try again with the full transaction */ |
| 5593 | btrfs_end_transaction(trans, root); |
| 5594 | trans = btrfs_start_transaction(root, 1); |
Josef Bacik | 22c44fe | 2011-11-30 10:45:38 -0500 | [diff] [blame] | 5595 | if (IS_ERR(trans)) |
| 5596 | return PTR_ERR(trans); |
Yan, Zheng | 8929ecfa | 2010-05-16 10:49:58 -0400 | [diff] [blame] | 5597 | |
Chris Mason | 94b6044 | 2010-05-26 11:02:00 -0400 | [diff] [blame] | 5598 | ret = btrfs_update_inode(trans, root, inode); |
Chris Mason | 94b6044 | 2010-05-26 11:02:00 -0400 | [diff] [blame] | 5599 | } |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5600 | btrfs_end_transaction(trans, root); |
Miao Xie | 16cdcec | 2011-04-22 18:12:22 +0800 | [diff] [blame] | 5601 | if (BTRFS_I(inode)->delayed_node) |
| 5602 | btrfs_balance_delayed_items(root); |
Josef Bacik | 22c44fe | 2011-11-30 10:45:38 -0500 | [diff] [blame] | 5603 | |
| 5604 | return ret; |
| 5605 | } |
| 5606 | |
| 5607 | /* |
| 5608 | * This is a copy of file_update_time. We need this so we can return error on |
| 5609 | * ENOSPC for updating the inode in the case of file write and mmap writes. |
| 5610 | */ |
Josef Bacik | e41f941 | 2012-03-26 09:46:47 -0400 | [diff] [blame] | 5611 | static int btrfs_update_time(struct inode *inode, struct timespec *now, |
| 5612 | int flags) |
Josef Bacik | 22c44fe | 2011-11-30 10:45:38 -0500 | [diff] [blame] | 5613 | { |
Alexander Block | 2bc556528 | 2012-06-15 09:49:33 +0200 | [diff] [blame] | 5614 | struct btrfs_root *root = BTRFS_I(inode)->root; |
| 5615 | |
| 5616 | if (btrfs_root_readonly(root)) |
| 5617 | return -EROFS; |
| 5618 | |
Josef Bacik | e41f941 | 2012-03-26 09:46:47 -0400 | [diff] [blame] | 5619 | if (flags & S_VERSION) |
Josef Bacik | 22c44fe | 2011-11-30 10:45:38 -0500 | [diff] [blame] | 5620 | inode_inc_iversion(inode); |
Josef Bacik | e41f941 | 2012-03-26 09:46:47 -0400 | [diff] [blame] | 5621 | if (flags & S_CTIME) |
| 5622 | inode->i_ctime = *now; |
| 5623 | if (flags & S_MTIME) |
| 5624 | inode->i_mtime = *now; |
| 5625 | if (flags & S_ATIME) |
| 5626 | inode->i_atime = *now; |
| 5627 | return btrfs_dirty_inode(inode); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5628 | } |
| 5629 | |
Chris Mason | d352ac6 | 2008-09-29 15:18:18 -0400 | [diff] [blame] | 5630 | /* |
| 5631 | * find the highest existing sequence number in a directory |
| 5632 | * and then set the in-memory index_cnt variable to reflect |
| 5633 | * free sequence numbers |
| 5634 | */ |
Josef Bacik | aec7477 | 2008-07-24 12:12:38 -0400 | [diff] [blame] | 5635 | static int btrfs_set_inode_index_count(struct inode *inode) |
| 5636 | { |
| 5637 | struct btrfs_root *root = BTRFS_I(inode)->root; |
| 5638 | struct btrfs_key key, found_key; |
| 5639 | struct btrfs_path *path; |
| 5640 | struct extent_buffer *leaf; |
| 5641 | int ret; |
| 5642 | |
Li Zefan | 33345d01 | 2011-04-20 10:31:50 +0800 | [diff] [blame] | 5643 | key.objectid = btrfs_ino(inode); |
David Sterba | 962a298 | 2014-06-04 18:41:45 +0200 | [diff] [blame] | 5644 | key.type = BTRFS_DIR_INDEX_KEY; |
Josef Bacik | aec7477 | 2008-07-24 12:12:38 -0400 | [diff] [blame] | 5645 | key.offset = (u64)-1; |
| 5646 | |
| 5647 | path = btrfs_alloc_path(); |
| 5648 | if (!path) |
| 5649 | return -ENOMEM; |
| 5650 | |
| 5651 | ret = btrfs_search_slot(NULL, root, &key, path, 0, 0); |
| 5652 | if (ret < 0) |
| 5653 | goto out; |
| 5654 | /* FIXME: we should be able to handle this */ |
| 5655 | if (ret == 0) |
| 5656 | goto out; |
| 5657 | ret = 0; |
| 5658 | |
| 5659 | /* |
| 5660 | * MAGIC NUMBER EXPLANATION: |
| 5661 | * since we search a directory based on f_pos we have to start at 2 |
| 5662 | * since '.' and '..' have f_pos of 0 and 1 respectively, so everybody |
| 5663 | * else has to start at 2 |
| 5664 | */ |
| 5665 | if (path->slots[0] == 0) { |
| 5666 | BTRFS_I(inode)->index_cnt = 2; |
| 5667 | goto out; |
| 5668 | } |
| 5669 | |
| 5670 | path->slots[0]--; |
| 5671 | |
| 5672 | leaf = path->nodes[0]; |
| 5673 | btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]); |
| 5674 | |
Li Zefan | 33345d01 | 2011-04-20 10:31:50 +0800 | [diff] [blame] | 5675 | if (found_key.objectid != btrfs_ino(inode) || |
David Sterba | 962a298 | 2014-06-04 18:41:45 +0200 | [diff] [blame] | 5676 | found_key.type != BTRFS_DIR_INDEX_KEY) { |
Josef Bacik | aec7477 | 2008-07-24 12:12:38 -0400 | [diff] [blame] | 5677 | BTRFS_I(inode)->index_cnt = 2; |
| 5678 | goto out; |
| 5679 | } |
| 5680 | |
| 5681 | BTRFS_I(inode)->index_cnt = found_key.offset + 1; |
| 5682 | out: |
| 5683 | btrfs_free_path(path); |
| 5684 | return ret; |
| 5685 | } |
| 5686 | |
Chris Mason | d352ac6 | 2008-09-29 15:18:18 -0400 | [diff] [blame] | 5687 | /* |
| 5688 | * helper to find a free sequence number in a given directory. This current |
| 5689 | * code is very simple, later versions will do smarter things in the btree |
| 5690 | */ |
Chris Mason | 3de4586 | 2008-11-17 21:02:50 -0500 | [diff] [blame] | 5691 | int btrfs_set_inode_index(struct inode *dir, u64 *index) |
Josef Bacik | aec7477 | 2008-07-24 12:12:38 -0400 | [diff] [blame] | 5692 | { |
| 5693 | int ret = 0; |
| 5694 | |
| 5695 | if (BTRFS_I(dir)->index_cnt == (u64)-1) { |
Miao Xie | 16cdcec | 2011-04-22 18:12:22 +0800 | [diff] [blame] | 5696 | ret = btrfs_inode_delayed_dir_index_count(dir); |
| 5697 | if (ret) { |
| 5698 | ret = btrfs_set_inode_index_count(dir); |
| 5699 | if (ret) |
| 5700 | return ret; |
| 5701 | } |
Josef Bacik | aec7477 | 2008-07-24 12:12:38 -0400 | [diff] [blame] | 5702 | } |
| 5703 | |
Chris Mason | 00e4e6b | 2008-08-05 11:18:09 -0400 | [diff] [blame] | 5704 | *index = BTRFS_I(dir)->index_cnt; |
Josef Bacik | aec7477 | 2008-07-24 12:12:38 -0400 | [diff] [blame] | 5705 | BTRFS_I(dir)->index_cnt++; |
| 5706 | |
| 5707 | return ret; |
| 5708 | } |
| 5709 | |
Chris Mason | b0d5d10 | 2014-09-08 13:08:51 -0700 | [diff] [blame] | 5710 | static int btrfs_insert_inode_locked(struct inode *inode) |
| 5711 | { |
| 5712 | struct btrfs_iget_args args; |
| 5713 | args.location = &BTRFS_I(inode)->location; |
| 5714 | args.root = BTRFS_I(inode)->root; |
| 5715 | |
| 5716 | return insert_inode_locked4(inode, |
| 5717 | btrfs_inode_hash(inode->i_ino, BTRFS_I(inode)->root), |
| 5718 | btrfs_find_actor, &args); |
| 5719 | } |
| 5720 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5721 | static struct inode *btrfs_new_inode(struct btrfs_trans_handle *trans, |
| 5722 | struct btrfs_root *root, |
Josef Bacik | aec7477 | 2008-07-24 12:12:38 -0400 | [diff] [blame] | 5723 | struct inode *dir, |
Chris Mason | 9c58309 | 2008-01-29 15:15:18 -0500 | [diff] [blame] | 5724 | const char *name, int name_len, |
Al Viro | 175a4eb | 2011-07-26 03:30:54 -0400 | [diff] [blame] | 5725 | u64 ref_objectid, u64 objectid, |
| 5726 | umode_t mode, u64 *index) |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5727 | { |
| 5728 | struct inode *inode; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 5729 | struct btrfs_inode_item *inode_item; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5730 | struct btrfs_key *location; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 5731 | struct btrfs_path *path; |
Chris Mason | 9c58309 | 2008-01-29 15:15:18 -0500 | [diff] [blame] | 5732 | struct btrfs_inode_ref *ref; |
| 5733 | struct btrfs_key key[2]; |
| 5734 | u32 sizes[2]; |
Filipe Manana | ef3b9af | 2014-04-27 20:40:45 +0100 | [diff] [blame] | 5735 | int nitems = name ? 2 : 1; |
Chris Mason | 9c58309 | 2008-01-29 15:15:18 -0500 | [diff] [blame] | 5736 | unsigned long ptr; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5737 | int ret; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5738 | |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 5739 | path = btrfs_alloc_path(); |
Mark Fasheh | d8926bb | 2011-07-13 10:38:47 -0700 | [diff] [blame] | 5740 | if (!path) |
| 5741 | return ERR_PTR(-ENOMEM); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 5742 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5743 | inode = new_inode(root->fs_info->sb); |
Yoshinori Sano | 8fb2764 | 2011-04-09 02:30:07 +0000 | [diff] [blame] | 5744 | if (!inode) { |
| 5745 | btrfs_free_path(path); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5746 | return ERR_PTR(-ENOMEM); |
Yoshinori Sano | 8fb2764 | 2011-04-09 02:30:07 +0000 | [diff] [blame] | 5747 | } |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5748 | |
Li Zefan | 581bb05 | 2011-04-20 10:06:11 +0800 | [diff] [blame] | 5749 | /* |
Filipe Manana | 5762b5c | 2014-08-01 00:10:32 +0100 | [diff] [blame] | 5750 | * O_TMPFILE, set link count to 0, so that after this point, |
| 5751 | * we fill in an inode item with the correct link count. |
| 5752 | */ |
| 5753 | if (!name) |
| 5754 | set_nlink(inode, 0); |
| 5755 | |
| 5756 | /* |
Li Zefan | 581bb05 | 2011-04-20 10:06:11 +0800 | [diff] [blame] | 5757 | * we have to initialize this early, so we can reclaim the inode |
| 5758 | * number if we fail afterwards in this function. |
| 5759 | */ |
| 5760 | inode->i_ino = objectid; |
| 5761 | |
Filipe Manana | ef3b9af | 2014-04-27 20:40:45 +0100 | [diff] [blame] | 5762 | if (dir && name) { |
liubo | 1abe9b8 | 2011-03-24 11:18:59 +0000 | [diff] [blame] | 5763 | trace_btrfs_inode_request(dir); |
| 5764 | |
Chris Mason | 3de4586 | 2008-11-17 21:02:50 -0500 | [diff] [blame] | 5765 | ret = btrfs_set_inode_index(dir, index); |
Shen Feng | 0977143 | 2009-04-02 16:46:06 -0400 | [diff] [blame] | 5766 | if (ret) { |
Yoshinori Sano | 8fb2764 | 2011-04-09 02:30:07 +0000 | [diff] [blame] | 5767 | btrfs_free_path(path); |
Shen Feng | 0977143 | 2009-04-02 16:46:06 -0400 | [diff] [blame] | 5768 | iput(inode); |
Josef Bacik | aec7477 | 2008-07-24 12:12:38 -0400 | [diff] [blame] | 5769 | return ERR_PTR(ret); |
Shen Feng | 0977143 | 2009-04-02 16:46:06 -0400 | [diff] [blame] | 5770 | } |
Filipe Manana | ef3b9af | 2014-04-27 20:40:45 +0100 | [diff] [blame] | 5771 | } else if (dir) { |
| 5772 | *index = 0; |
Josef Bacik | aec7477 | 2008-07-24 12:12:38 -0400 | [diff] [blame] | 5773 | } |
| 5774 | /* |
| 5775 | * index_cnt is ignored for everything but a dir, |
| 5776 | * btrfs_get_inode_index_count has an explanation for the magic |
| 5777 | * number |
| 5778 | */ |
| 5779 | BTRFS_I(inode)->index_cnt = 2; |
Miao Xie | 67de117 | 2013-12-26 13:07:06 +0800 | [diff] [blame] | 5780 | BTRFS_I(inode)->dir_index = *index; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5781 | BTRFS_I(inode)->root = root; |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 5782 | BTRFS_I(inode)->generation = trans->transid; |
Josef Bacik | 7619585 | 2010-11-19 02:18:02 +0000 | [diff] [blame] | 5783 | inode->i_generation = BTRFS_I(inode)->generation; |
Chris Mason | b888db2 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 5784 | |
Josef Bacik | 5dc562c | 2012-08-17 13:14:17 -0400 | [diff] [blame] | 5785 | /* |
| 5786 | * We could have gotten an inode number from somebody who was fsynced |
| 5787 | * and then removed in this same transaction, so let's just set full |
| 5788 | * sync since it will be a full sync anyway and this will blow away the |
| 5789 | * old info in the log. |
| 5790 | */ |
| 5791 | set_bit(BTRFS_INODE_NEEDS_FULL_SYNC, &BTRFS_I(inode)->runtime_flags); |
| 5792 | |
Chris Mason | 9c58309 | 2008-01-29 15:15:18 -0500 | [diff] [blame] | 5793 | key[0].objectid = objectid; |
David Sterba | 962a298 | 2014-06-04 18:41:45 +0200 | [diff] [blame] | 5794 | key[0].type = BTRFS_INODE_ITEM_KEY; |
Chris Mason | 9c58309 | 2008-01-29 15:15:18 -0500 | [diff] [blame] | 5795 | key[0].offset = 0; |
| 5796 | |
Chris Mason | 9c58309 | 2008-01-29 15:15:18 -0500 | [diff] [blame] | 5797 | sizes[0] = sizeof(struct btrfs_inode_item); |
Filipe Manana | ef3b9af | 2014-04-27 20:40:45 +0100 | [diff] [blame] | 5798 | |
| 5799 | if (name) { |
| 5800 | /* |
| 5801 | * Start new inodes with an inode_ref. This is slightly more |
| 5802 | * efficient for small numbers of hard links since they will |
| 5803 | * be packed into one item. Extended refs will kick in if we |
| 5804 | * add more hard links than can fit in the ref item. |
| 5805 | */ |
| 5806 | key[1].objectid = objectid; |
David Sterba | 962a298 | 2014-06-04 18:41:45 +0200 | [diff] [blame] | 5807 | key[1].type = BTRFS_INODE_REF_KEY; |
Filipe Manana | ef3b9af | 2014-04-27 20:40:45 +0100 | [diff] [blame] | 5808 | key[1].offset = ref_objectid; |
| 5809 | |
| 5810 | sizes[1] = name_len + sizeof(*ref); |
| 5811 | } |
Chris Mason | 9c58309 | 2008-01-29 15:15:18 -0500 | [diff] [blame] | 5812 | |
Chris Mason | b0d5d10 | 2014-09-08 13:08:51 -0700 | [diff] [blame] | 5813 | location = &BTRFS_I(inode)->location; |
| 5814 | location->objectid = objectid; |
| 5815 | location->offset = 0; |
David Sterba | 962a298 | 2014-06-04 18:41:45 +0200 | [diff] [blame] | 5816 | location->type = BTRFS_INODE_ITEM_KEY; |
Chris Mason | b0d5d10 | 2014-09-08 13:08:51 -0700 | [diff] [blame] | 5817 | |
| 5818 | ret = btrfs_insert_inode_locked(inode); |
| 5819 | if (ret < 0) |
| 5820 | goto fail; |
| 5821 | |
Chris Mason | b947343 | 2009-03-13 11:00:37 -0400 | [diff] [blame] | 5822 | path->leave_spinning = 1; |
Filipe Manana | ef3b9af | 2014-04-27 20:40:45 +0100 | [diff] [blame] | 5823 | ret = btrfs_insert_empty_items(trans, root, path, key, sizes, nitems); |
Chris Mason | 9c58309 | 2008-01-29 15:15:18 -0500 | [diff] [blame] | 5824 | if (ret != 0) |
Chris Mason | b0d5d10 | 2014-09-08 13:08:51 -0700 | [diff] [blame] | 5825 | goto fail_unlock; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 5826 | |
Dmitry Monakhov | ecc11fab | 2010-03-04 17:31:47 +0300 | [diff] [blame] | 5827 | inode_init_owner(inode, dir, mode); |
Yan Zheng | a76a3cd | 2008-10-09 11:46:29 -0400 | [diff] [blame] | 5828 | inode_set_bytes(inode, 0); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5829 | inode->i_mtime = inode->i_atime = inode->i_ctime = CURRENT_TIME; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 5830 | inode_item = btrfs_item_ptr(path->nodes[0], path->slots[0], |
| 5831 | struct btrfs_inode_item); |
Li Zefan | 293f7e0 | 2012-07-10 00:58:58 -0600 | [diff] [blame] | 5832 | memset_extent_buffer(path->nodes[0], 0, (unsigned long)inode_item, |
| 5833 | sizeof(*inode_item)); |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 5834 | fill_inode_item(trans, path->nodes[0], inode_item, inode); |
Chris Mason | 9c58309 | 2008-01-29 15:15:18 -0500 | [diff] [blame] | 5835 | |
Filipe Manana | ef3b9af | 2014-04-27 20:40:45 +0100 | [diff] [blame] | 5836 | if (name) { |
| 5837 | ref = btrfs_item_ptr(path->nodes[0], path->slots[0] + 1, |
| 5838 | struct btrfs_inode_ref); |
| 5839 | btrfs_set_inode_ref_name_len(path->nodes[0], ref, name_len); |
| 5840 | btrfs_set_inode_ref_index(path->nodes[0], ref, *index); |
| 5841 | ptr = (unsigned long)(ref + 1); |
| 5842 | write_extent_buffer(path->nodes[0], name, ptr, name_len); |
| 5843 | } |
Chris Mason | 9c58309 | 2008-01-29 15:15:18 -0500 | [diff] [blame] | 5844 | |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 5845 | btrfs_mark_buffer_dirty(path->nodes[0]); |
| 5846 | btrfs_free_path(path); |
| 5847 | |
Christoph Hellwig | 6cbff00 | 2009-04-17 10:37:41 +0200 | [diff] [blame] | 5848 | btrfs_inherit_iflags(inode, dir); |
| 5849 | |
Al Viro | 569254b | 2011-07-24 17:08:40 -0400 | [diff] [blame] | 5850 | if (S_ISREG(mode)) { |
Chris Mason | 9427216 | 2009-07-02 12:26:06 -0400 | [diff] [blame] | 5851 | if (btrfs_test_opt(root, NODATASUM)) |
| 5852 | BTRFS_I(inode)->flags |= BTRFS_INODE_NODATASUM; |
Liu Bo | 213490b | 2012-09-11 08:33:50 -0600 | [diff] [blame] | 5853 | if (btrfs_test_opt(root, NODATACOW)) |
Josef Bacik | f2bdf9a | 2013-02-21 15:28:28 -0500 | [diff] [blame] | 5854 | BTRFS_I(inode)->flags |= BTRFS_INODE_NODATACOW | |
| 5855 | BTRFS_INODE_NODATASUM; |
Chris Mason | 9427216 | 2009-07-02 12:26:06 -0400 | [diff] [blame] | 5856 | } |
| 5857 | |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 5858 | inode_tree_add(inode); |
liubo | 1abe9b8 | 2011-03-24 11:18:59 +0000 | [diff] [blame] | 5859 | |
| 5860 | trace_btrfs_inode_new(inode); |
Chris Mason | 1973f0f | 2011-06-24 13:13:29 -0400 | [diff] [blame] | 5861 | btrfs_set_inode_last_trans(trans, inode); |
liubo | 1abe9b8 | 2011-03-24 11:18:59 +0000 | [diff] [blame] | 5862 | |
Alexander Block | 8ea05e3 | 2012-07-25 17:35:53 +0200 | [diff] [blame] | 5863 | btrfs_update_root_times(trans, root); |
| 5864 | |
Filipe David Borba Manana | 6354192 | 2014-01-07 11:47:46 +0000 | [diff] [blame] | 5865 | ret = btrfs_inode_inherit_props(trans, inode, dir); |
| 5866 | if (ret) |
| 5867 | btrfs_err(root->fs_info, |
| 5868 | "error inheriting props for ino %llu (root %llu): %d", |
| 5869 | btrfs_ino(inode), root->root_key.objectid, ret); |
| 5870 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5871 | return inode; |
Chris Mason | b0d5d10 | 2014-09-08 13:08:51 -0700 | [diff] [blame] | 5872 | |
| 5873 | fail_unlock: |
| 5874 | unlock_new_inode(inode); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 5875 | fail: |
Filipe Manana | ef3b9af | 2014-04-27 20:40:45 +0100 | [diff] [blame] | 5876 | if (dir && name) |
Josef Bacik | aec7477 | 2008-07-24 12:12:38 -0400 | [diff] [blame] | 5877 | BTRFS_I(dir)->index_cnt--; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 5878 | btrfs_free_path(path); |
Shen Feng | 0977143 | 2009-04-02 16:46:06 -0400 | [diff] [blame] | 5879 | iput(inode); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 5880 | return ERR_PTR(ret); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5881 | } |
| 5882 | |
| 5883 | static inline u8 btrfs_inode_type(struct inode *inode) |
| 5884 | { |
| 5885 | return btrfs_type_by_mode[(inode->i_mode & S_IFMT) >> S_SHIFT]; |
| 5886 | } |
| 5887 | |
Chris Mason | d352ac6 | 2008-09-29 15:18:18 -0400 | [diff] [blame] | 5888 | /* |
| 5889 | * utility function to add 'inode' into 'parent_inode' with |
| 5890 | * a give name and a given sequence number. |
| 5891 | * if 'add_backref' is true, also insert a backref from the |
| 5892 | * inode to the parent directory. |
| 5893 | */ |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 5894 | int btrfs_add_link(struct btrfs_trans_handle *trans, |
| 5895 | struct inode *parent_inode, struct inode *inode, |
| 5896 | const char *name, int name_len, int add_backref, u64 index) |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5897 | { |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 5898 | int ret = 0; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5899 | struct btrfs_key key; |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 5900 | struct btrfs_root *root = BTRFS_I(parent_inode)->root; |
Li Zefan | 33345d01 | 2011-04-20 10:31:50 +0800 | [diff] [blame] | 5901 | u64 ino = btrfs_ino(inode); |
| 5902 | u64 parent_ino = btrfs_ino(parent_inode); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 5903 | |
Li Zefan | 33345d01 | 2011-04-20 10:31:50 +0800 | [diff] [blame] | 5904 | if (unlikely(ino == BTRFS_FIRST_FREE_OBJECTID)) { |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 5905 | memcpy(&key, &BTRFS_I(inode)->root->root_key, sizeof(key)); |
| 5906 | } else { |
Li Zefan | 33345d01 | 2011-04-20 10:31:50 +0800 | [diff] [blame] | 5907 | key.objectid = ino; |
David Sterba | 962a298 | 2014-06-04 18:41:45 +0200 | [diff] [blame] | 5908 | key.type = BTRFS_INODE_ITEM_KEY; |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 5909 | key.offset = 0; |
| 5910 | } |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5911 | |
Li Zefan | 33345d01 | 2011-04-20 10:31:50 +0800 | [diff] [blame] | 5912 | if (unlikely(ino == BTRFS_FIRST_FREE_OBJECTID)) { |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 5913 | ret = btrfs_add_root_ref(trans, root->fs_info->tree_root, |
| 5914 | key.objectid, root->root_key.objectid, |
Li Zefan | 33345d01 | 2011-04-20 10:31:50 +0800 | [diff] [blame] | 5915 | parent_ino, index, name, name_len); |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 5916 | } else if (add_backref) { |
Li Zefan | 33345d01 | 2011-04-20 10:31:50 +0800 | [diff] [blame] | 5917 | ret = btrfs_insert_inode_ref(trans, root, name, name_len, ino, |
| 5918 | parent_ino, index); |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 5919 | } |
| 5920 | |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 5921 | /* Nothing to clean up yet */ |
| 5922 | if (ret) |
| 5923 | return ret; |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 5924 | |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 5925 | ret = btrfs_insert_dir_item(trans, root, name, name_len, |
| 5926 | parent_inode, &key, |
| 5927 | btrfs_inode_type(inode), index); |
Chris Mason | 9c52057 | 2012-12-17 14:26:57 -0500 | [diff] [blame] | 5928 | if (ret == -EEXIST || ret == -EOVERFLOW) |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 5929 | goto fail_dir_item; |
| 5930 | else if (ret) { |
| 5931 | btrfs_abort_transaction(trans, root, ret); |
| 5932 | return ret; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5933 | } |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 5934 | |
| 5935 | btrfs_i_size_write(parent_inode, parent_inode->i_size + |
| 5936 | name_len * 2); |
Josef Bacik | 0c4d2d9 | 2012-04-05 15:03:02 -0400 | [diff] [blame] | 5937 | inode_inc_iversion(parent_inode); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 5938 | parent_inode->i_mtime = parent_inode->i_ctime = CURRENT_TIME; |
| 5939 | ret = btrfs_update_inode(trans, root, parent_inode); |
| 5940 | if (ret) |
| 5941 | btrfs_abort_transaction(trans, root, ret); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5942 | return ret; |
Chris Mason | fe66a05 | 2012-02-20 08:40:56 -0500 | [diff] [blame] | 5943 | |
| 5944 | fail_dir_item: |
| 5945 | if (unlikely(ino == BTRFS_FIRST_FREE_OBJECTID)) { |
| 5946 | u64 local_index; |
| 5947 | int err; |
| 5948 | err = btrfs_del_root_ref(trans, root->fs_info->tree_root, |
| 5949 | key.objectid, root->root_key.objectid, |
| 5950 | parent_ino, &local_index, name, name_len); |
| 5951 | |
| 5952 | } else if (add_backref) { |
| 5953 | u64 local_index; |
| 5954 | int err; |
| 5955 | |
| 5956 | err = btrfs_del_inode_ref(trans, root, name, name_len, |
| 5957 | ino, parent_ino, &local_index); |
| 5958 | } |
| 5959 | return ret; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5960 | } |
| 5961 | |
| 5962 | static int btrfs_add_nondir(struct btrfs_trans_handle *trans, |
Josef Bacik | a1b075d | 2010-11-19 20:36:11 +0000 | [diff] [blame] | 5963 | struct inode *dir, struct dentry *dentry, |
| 5964 | struct inode *inode, int backref, u64 index) |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5965 | { |
Josef Bacik | a1b075d | 2010-11-19 20:36:11 +0000 | [diff] [blame] | 5966 | int err = btrfs_add_link(trans, dir, inode, |
| 5967 | dentry->d_name.name, dentry->d_name.len, |
| 5968 | backref, index); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5969 | if (err > 0) |
| 5970 | err = -EEXIST; |
| 5971 | return err; |
| 5972 | } |
| 5973 | |
Josef Bacik | 618e21d | 2007-07-11 10:18:17 -0400 | [diff] [blame] | 5974 | static int btrfs_mknod(struct inode *dir, struct dentry *dentry, |
Al Viro | 1a67aaf | 2011-07-26 01:52:52 -0400 | [diff] [blame] | 5975 | umode_t mode, dev_t rdev) |
Josef Bacik | 618e21d | 2007-07-11 10:18:17 -0400 | [diff] [blame] | 5976 | { |
| 5977 | struct btrfs_trans_handle *trans; |
| 5978 | struct btrfs_root *root = BTRFS_I(dir)->root; |
Chris Mason | 1832a6d | 2007-12-21 16:27:21 -0500 | [diff] [blame] | 5979 | struct inode *inode = NULL; |
Josef Bacik | 618e21d | 2007-07-11 10:18:17 -0400 | [diff] [blame] | 5980 | int err; |
| 5981 | int drop_inode = 0; |
| 5982 | u64 objectid; |
Chris Mason | 00e4e6b | 2008-08-05 11:18:09 -0400 | [diff] [blame] | 5983 | u64 index = 0; |
Josef Bacik | 618e21d | 2007-07-11 10:18:17 -0400 | [diff] [blame] | 5984 | |
| 5985 | if (!new_valid_dev(rdev)) |
| 5986 | return -EINVAL; |
| 5987 | |
Josef Bacik | 9ed74f2 | 2009-09-11 16:12:44 -0400 | [diff] [blame] | 5988 | /* |
| 5989 | * 2 for inode item and ref |
| 5990 | * 2 for dir items |
| 5991 | * 1 for xattr if selinux is on |
| 5992 | */ |
Yan, Zheng | a22285a | 2010-05-16 10:48:46 -0400 | [diff] [blame] | 5993 | trans = btrfs_start_transaction(root, 5); |
| 5994 | if (IS_ERR(trans)) |
| 5995 | return PTR_ERR(trans); |
Chris Mason | 1832a6d | 2007-12-21 16:27:21 -0500 | [diff] [blame] | 5996 | |
Li Zefan | 581bb05 | 2011-04-20 10:06:11 +0800 | [diff] [blame] | 5997 | err = btrfs_find_free_ino(root, &objectid); |
| 5998 | if (err) |
| 5999 | goto out_unlock; |
| 6000 | |
Josef Bacik | aec7477 | 2008-07-24 12:12:38 -0400 | [diff] [blame] | 6001 | inode = btrfs_new_inode(trans, root, dir, dentry->d_name.name, |
Li Zefan | 33345d01 | 2011-04-20 10:31:50 +0800 | [diff] [blame] | 6002 | dentry->d_name.len, btrfs_ino(dir), objectid, |
Josef Bacik | d82a6f1 | 2011-05-11 15:26:06 -0400 | [diff] [blame] | 6003 | mode, &index); |
Tsutomu Itoh | 7cf96da | 2011-04-25 19:43:53 -0400 | [diff] [blame] | 6004 | if (IS_ERR(inode)) { |
| 6005 | err = PTR_ERR(inode); |
Josef Bacik | 618e21d | 2007-07-11 10:18:17 -0400 | [diff] [blame] | 6006 | goto out_unlock; |
Tsutomu Itoh | 7cf96da | 2011-04-25 19:43:53 -0400 | [diff] [blame] | 6007 | } |
Josef Bacik | 618e21d | 2007-07-11 10:18:17 -0400 | [diff] [blame] | 6008 | |
Casey Schaufler | ad19db7 | 2011-12-15 10:09:07 -0500 | [diff] [blame] | 6009 | /* |
| 6010 | * If the active LSM wants to access the inode during |
| 6011 | * d_instantiate it needs these. Smack checks to see |
| 6012 | * if the filesystem supports xattrs by looking at the |
| 6013 | * ops vector. |
| 6014 | */ |
Casey Schaufler | ad19db7 | 2011-12-15 10:09:07 -0500 | [diff] [blame] | 6015 | inode->i_op = &btrfs_special_inode_operations; |
Chris Mason | b0d5d10 | 2014-09-08 13:08:51 -0700 | [diff] [blame] | 6016 | init_special_inode(inode, inode->i_mode, rdev); |
| 6017 | |
| 6018 | err = btrfs_init_inode_security(trans, inode, dir, &dentry->d_name); |
Josef Bacik | 618e21d | 2007-07-11 10:18:17 -0400 | [diff] [blame] | 6019 | if (err) |
Chris Mason | b0d5d10 | 2014-09-08 13:08:51 -0700 | [diff] [blame] | 6020 | goto out_unlock_inode; |
| 6021 | |
| 6022 | err = btrfs_add_nondir(trans, dir, dentry, inode, 0, index); |
| 6023 | if (err) { |
| 6024 | goto out_unlock_inode; |
| 6025 | } else { |
Yan | 1b4ab1b | 2007-08-29 09:11:44 -0400 | [diff] [blame] | 6026 | btrfs_update_inode(trans, root, inode); |
Chris Mason | b0d5d10 | 2014-09-08 13:08:51 -0700 | [diff] [blame] | 6027 | unlock_new_inode(inode); |
Al Viro | 08c422c | 2011-12-23 07:58:13 -0500 | [diff] [blame] | 6028 | d_instantiate(dentry, inode); |
Josef Bacik | 618e21d | 2007-07-11 10:18:17 -0400 | [diff] [blame] | 6029 | } |
Chris Mason | b0d5d10 | 2014-09-08 13:08:51 -0700 | [diff] [blame] | 6030 | |
Josef Bacik | 618e21d | 2007-07-11 10:18:17 -0400 | [diff] [blame] | 6031 | out_unlock: |
Josef Bacik | 7ad85bb | 2012-01-12 19:10:12 -0500 | [diff] [blame] | 6032 | btrfs_end_transaction(trans, root); |
Josef Bacik | c581afc | 2014-02-06 16:06:06 -0500 | [diff] [blame] | 6033 | btrfs_balance_delayed_items(root); |
Liu Bo | b53d3f5 | 2012-11-14 14:34:34 +0000 | [diff] [blame] | 6034 | btrfs_btree_balance_dirty(root); |
Josef Bacik | 618e21d | 2007-07-11 10:18:17 -0400 | [diff] [blame] | 6035 | if (drop_inode) { |
| 6036 | inode_dec_link_count(inode); |
| 6037 | iput(inode); |
| 6038 | } |
Josef Bacik | 618e21d | 2007-07-11 10:18:17 -0400 | [diff] [blame] | 6039 | return err; |
Chris Mason | b0d5d10 | 2014-09-08 13:08:51 -0700 | [diff] [blame] | 6040 | |
| 6041 | out_unlock_inode: |
| 6042 | drop_inode = 1; |
| 6043 | unlock_new_inode(inode); |
| 6044 | goto out_unlock; |
| 6045 | |
Josef Bacik | 618e21d | 2007-07-11 10:18:17 -0400 | [diff] [blame] | 6046 | } |
| 6047 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 6048 | static int btrfs_create(struct inode *dir, struct dentry *dentry, |
Al Viro | ebfc3b4 | 2012-06-10 18:05:36 -0400 | [diff] [blame] | 6049 | umode_t mode, bool excl) |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 6050 | { |
| 6051 | struct btrfs_trans_handle *trans; |
| 6052 | struct btrfs_root *root = BTRFS_I(dir)->root; |
Chris Mason | 1832a6d | 2007-12-21 16:27:21 -0500 | [diff] [blame] | 6053 | struct inode *inode = NULL; |
Filipe Brandenburger | 43baa57 | 2012-11-30 03:40:09 +0000 | [diff] [blame] | 6054 | int drop_inode_on_err = 0; |
Yan, Zheng | a22285a | 2010-05-16 10:48:46 -0400 | [diff] [blame] | 6055 | int err; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 6056 | u64 objectid; |
Chris Mason | 00e4e6b | 2008-08-05 11:18:09 -0400 | [diff] [blame] | 6057 | u64 index = 0; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 6058 | |
Josef Bacik | 9ed74f2 | 2009-09-11 16:12:44 -0400 | [diff] [blame] | 6059 | /* |
| 6060 | * 2 for inode item and ref |
| 6061 | * 2 for dir items |
| 6062 | * 1 for xattr if selinux is on |
| 6063 | */ |
Yan, Zheng | a22285a | 2010-05-16 10:48:46 -0400 | [diff] [blame] | 6064 | trans = btrfs_start_transaction(root, 5); |
| 6065 | if (IS_ERR(trans)) |
| 6066 | return PTR_ERR(trans); |
Josef Bacik | 9ed74f2 | 2009-09-11 16:12:44 -0400 | [diff] [blame] | 6067 | |
Li Zefan | 581bb05 | 2011-04-20 10:06:11 +0800 | [diff] [blame] | 6068 | err = btrfs_find_free_ino(root, &objectid); |
| 6069 | if (err) |
| 6070 | goto out_unlock; |
| 6071 | |
Josef Bacik | aec7477 | 2008-07-24 12:12:38 -0400 | [diff] [blame] | 6072 | inode = btrfs_new_inode(trans, root, dir, dentry->d_name.name, |
Li Zefan | 33345d01 | 2011-04-20 10:31:50 +0800 | [diff] [blame] | 6073 | dentry->d_name.len, btrfs_ino(dir), objectid, |
Josef Bacik | d82a6f1 | 2011-05-11 15:26:06 -0400 | [diff] [blame] | 6074 | mode, &index); |
Tsutomu Itoh | 7cf96da | 2011-04-25 19:43:53 -0400 | [diff] [blame] | 6075 | if (IS_ERR(inode)) { |
| 6076 | err = PTR_ERR(inode); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 6077 | goto out_unlock; |
Tsutomu Itoh | 7cf96da | 2011-04-25 19:43:53 -0400 | [diff] [blame] | 6078 | } |
Filipe Brandenburger | 43baa57 | 2012-11-30 03:40:09 +0000 | [diff] [blame] | 6079 | drop_inode_on_err = 1; |
Casey Schaufler | ad19db7 | 2011-12-15 10:09:07 -0500 | [diff] [blame] | 6080 | /* |
| 6081 | * If the active LSM wants to access the inode during |
| 6082 | * d_instantiate it needs these. Smack checks to see |
| 6083 | * if the filesystem supports xattrs by looking at the |
| 6084 | * ops vector. |
| 6085 | */ |
| 6086 | inode->i_fop = &btrfs_file_operations; |
| 6087 | inode->i_op = &btrfs_file_inode_operations; |
Chris Mason | b0d5d10 | 2014-09-08 13:08:51 -0700 | [diff] [blame] | 6088 | inode->i_mapping->a_ops = &btrfs_aops; |
Chris Mason | b0d5d10 | 2014-09-08 13:08:51 -0700 | [diff] [blame] | 6089 | |
| 6090 | err = btrfs_init_inode_security(trans, inode, dir, &dentry->d_name); |
| 6091 | if (err) |
| 6092 | goto out_unlock_inode; |
| 6093 | |
| 6094 | err = btrfs_update_inode(trans, root, inode); |
| 6095 | if (err) |
| 6096 | goto out_unlock_inode; |
Casey Schaufler | ad19db7 | 2011-12-15 10:09:07 -0500 | [diff] [blame] | 6097 | |
Josef Bacik | a1b075d | 2010-11-19 20:36:11 +0000 | [diff] [blame] | 6098 | err = btrfs_add_nondir(trans, dir, dentry, inode, 0, index); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 6099 | if (err) |
Chris Mason | b0d5d10 | 2014-09-08 13:08:51 -0700 | [diff] [blame] | 6100 | goto out_unlock_inode; |
Filipe Brandenburger | 43baa57 | 2012-11-30 03:40:09 +0000 | [diff] [blame] | 6101 | |
Filipe Brandenburger | 43baa57 | 2012-11-30 03:40:09 +0000 | [diff] [blame] | 6102 | BTRFS_I(inode)->io_tree.ops = &btrfs_extent_io_ops; |
Chris Mason | b0d5d10 | 2014-09-08 13:08:51 -0700 | [diff] [blame] | 6103 | unlock_new_inode(inode); |
Filipe Brandenburger | 43baa57 | 2012-11-30 03:40:09 +0000 | [diff] [blame] | 6104 | d_instantiate(dentry, inode); |
| 6105 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 6106 | out_unlock: |
Josef Bacik | 7ad85bb | 2012-01-12 19:10:12 -0500 | [diff] [blame] | 6107 | btrfs_end_transaction(trans, root); |
Filipe Brandenburger | 43baa57 | 2012-11-30 03:40:09 +0000 | [diff] [blame] | 6108 | if (err && drop_inode_on_err) { |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 6109 | inode_dec_link_count(inode); |
| 6110 | iput(inode); |
| 6111 | } |
Josef Bacik | c581afc | 2014-02-06 16:06:06 -0500 | [diff] [blame] | 6112 | btrfs_balance_delayed_items(root); |
Liu Bo | b53d3f5 | 2012-11-14 14:34:34 +0000 | [diff] [blame] | 6113 | btrfs_btree_balance_dirty(root); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 6114 | return err; |
Chris Mason | b0d5d10 | 2014-09-08 13:08:51 -0700 | [diff] [blame] | 6115 | |
| 6116 | out_unlock_inode: |
| 6117 | unlock_new_inode(inode); |
| 6118 | goto out_unlock; |
| 6119 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 6120 | } |
| 6121 | |
| 6122 | static int btrfs_link(struct dentry *old_dentry, struct inode *dir, |
| 6123 | struct dentry *dentry) |
| 6124 | { |
| 6125 | struct btrfs_trans_handle *trans; |
| 6126 | struct btrfs_root *root = BTRFS_I(dir)->root; |
| 6127 | struct inode *inode = old_dentry->d_inode; |
Chris Mason | 00e4e6b | 2008-08-05 11:18:09 -0400 | [diff] [blame] | 6128 | u64 index; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 6129 | int err; |
| 6130 | int drop_inode = 0; |
| 6131 | |
TARUISI Hiroaki | 4a8be42 | 2009-11-12 07:14:26 +0000 | [diff] [blame] | 6132 | /* do not allow sys_link's with other subvols of the same device */ |
| 6133 | if (root->objectid != BTRFS_I(inode)->root->objectid) |
Mark Fasheh | 3ab3564 | 2011-03-22 17:20:26 +0000 | [diff] [blame] | 6134 | return -EXDEV; |
TARUISI Hiroaki | 4a8be42 | 2009-11-12 07:14:26 +0000 | [diff] [blame] | 6135 | |
Mark Fasheh | f186373 | 2012-08-08 11:32:27 -0700 | [diff] [blame] | 6136 | if (inode->i_nlink >= BTRFS_LINK_MAX) |
Al Viro | c055e99 | 2011-03-04 17:15:18 +0000 | [diff] [blame] | 6137 | return -EMLINK; |
Josef Bacik | 9ed74f2 | 2009-09-11 16:12:44 -0400 | [diff] [blame] | 6138 | |
Chris Mason | 3de4586 | 2008-11-17 21:02:50 -0500 | [diff] [blame] | 6139 | err = btrfs_set_inode_index(dir, &index); |
Josef Bacik | aec7477 | 2008-07-24 12:12:38 -0400 | [diff] [blame] | 6140 | if (err) |
| 6141 | goto fail; |
| 6142 | |
Yan, Zheng | a22285a | 2010-05-16 10:48:46 -0400 | [diff] [blame] | 6143 | /* |
Miao Xie | 7e6b646 | 2011-02-18 09:21:17 +0000 | [diff] [blame] | 6144 | * 2 items for inode and inode ref |
Yan, Zheng | a22285a | 2010-05-16 10:48:46 -0400 | [diff] [blame] | 6145 | * 2 items for dir items |
Miao Xie | 7e6b646 | 2011-02-18 09:21:17 +0000 | [diff] [blame] | 6146 | * 1 item for parent inode |
Yan, Zheng | a22285a | 2010-05-16 10:48:46 -0400 | [diff] [blame] | 6147 | */ |
Miao Xie | 7e6b646 | 2011-02-18 09:21:17 +0000 | [diff] [blame] | 6148 | trans = btrfs_start_transaction(root, 5); |
Yan, Zheng | a22285a | 2010-05-16 10:48:46 -0400 | [diff] [blame] | 6149 | if (IS_ERR(trans)) { |
| 6150 | err = PTR_ERR(trans); |
| 6151 | goto fail; |
| 6152 | } |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 6153 | |
Miao Xie | 67de117 | 2013-12-26 13:07:06 +0800 | [diff] [blame] | 6154 | /* There are several dir indexes for this inode, clear the cache. */ |
| 6155 | BTRFS_I(inode)->dir_index = 0ULL; |
Zach Brown | 8b558c5 | 2013-10-16 12:10:34 -0700 | [diff] [blame] | 6156 | inc_nlink(inode); |
Josef Bacik | 0c4d2d9 | 2012-04-05 15:03:02 -0400 | [diff] [blame] | 6157 | inode_inc_iversion(inode); |
Miao Xie | 3153495 | 2011-04-13 13:19:21 +0800 | [diff] [blame] | 6158 | inode->i_ctime = CURRENT_TIME; |
Al Viro | 7de9c6ee | 2010-10-23 11:11:40 -0400 | [diff] [blame] | 6159 | ihold(inode); |
Josef Bacik | e997615 | 2012-10-11 15:53:56 -0400 | [diff] [blame] | 6160 | set_bit(BTRFS_INODE_COPY_EVERYTHING, &BTRFS_I(inode)->runtime_flags); |
Josef Bacik | aec7477 | 2008-07-24 12:12:38 -0400 | [diff] [blame] | 6161 | |
Josef Bacik | a1b075d | 2010-11-19 20:36:11 +0000 | [diff] [blame] | 6162 | err = btrfs_add_nondir(trans, dir, dentry, inode, 1, index); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 6163 | |
Yan, Zheng | a571952 | 2009-09-24 09:17:31 -0400 | [diff] [blame] | 6164 | if (err) { |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 6165 | drop_inode = 1; |
Yan, Zheng | a571952 | 2009-09-24 09:17:31 -0400 | [diff] [blame] | 6166 | } else { |
Al Viro | 10d9f30 | 2011-07-16 23:09:10 -0400 | [diff] [blame] | 6167 | struct dentry *parent = dentry->d_parent; |
Yan, Zheng | a571952 | 2009-09-24 09:17:31 -0400 | [diff] [blame] | 6168 | err = btrfs_update_inode(trans, root, inode); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 6169 | if (err) |
| 6170 | goto fail; |
Filipe Manana | ef3b9af | 2014-04-27 20:40:45 +0100 | [diff] [blame] | 6171 | if (inode->i_nlink == 1) { |
| 6172 | /* |
| 6173 | * If new hard link count is 1, it's a file created |
| 6174 | * with open(2) O_TMPFILE flag. |
| 6175 | */ |
| 6176 | err = btrfs_orphan_del(trans, inode); |
| 6177 | if (err) |
| 6178 | goto fail; |
| 6179 | } |
Al Viro | 08c422c | 2011-12-23 07:58:13 -0500 | [diff] [blame] | 6180 | d_instantiate(dentry, inode); |
Josef Bacik | 6a91221 | 2010-11-20 09:48:00 +0000 | [diff] [blame] | 6181 | btrfs_log_new_name(trans, inode, NULL, parent); |
Yan, Zheng | a571952 | 2009-09-24 09:17:31 -0400 | [diff] [blame] | 6182 | } |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 6183 | |
Josef Bacik | 7ad85bb | 2012-01-12 19:10:12 -0500 | [diff] [blame] | 6184 | btrfs_end_transaction(trans, root); |
Josef Bacik | c581afc | 2014-02-06 16:06:06 -0500 | [diff] [blame] | 6185 | btrfs_balance_delayed_items(root); |
Chris Mason | 1832a6d | 2007-12-21 16:27:21 -0500 | [diff] [blame] | 6186 | fail: |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 6187 | if (drop_inode) { |
| 6188 | inode_dec_link_count(inode); |
| 6189 | iput(inode); |
| 6190 | } |
Liu Bo | b53d3f5 | 2012-11-14 14:34:34 +0000 | [diff] [blame] | 6191 | btrfs_btree_balance_dirty(root); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 6192 | return err; |
| 6193 | } |
| 6194 | |
Al Viro | 18bb1db | 2011-07-26 01:41:39 -0400 | [diff] [blame] | 6195 | 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] | 6196 | { |
Chris Mason | b9d8666 | 2008-05-02 16:13:49 -0400 | [diff] [blame] | 6197 | struct inode *inode = NULL; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 6198 | struct btrfs_trans_handle *trans; |
| 6199 | struct btrfs_root *root = BTRFS_I(dir)->root; |
| 6200 | int err = 0; |
| 6201 | int drop_on_err = 0; |
Chris Mason | b9d8666 | 2008-05-02 16:13:49 -0400 | [diff] [blame] | 6202 | u64 objectid = 0; |
Chris Mason | 00e4e6b | 2008-08-05 11:18:09 -0400 | [diff] [blame] | 6203 | u64 index = 0; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 6204 | |
Josef Bacik | 9ed74f2 | 2009-09-11 16:12:44 -0400 | [diff] [blame] | 6205 | /* |
| 6206 | * 2 items for inode and ref |
| 6207 | * 2 items for dir items |
| 6208 | * 1 for xattr if selinux is on |
| 6209 | */ |
Yan, Zheng | a22285a | 2010-05-16 10:48:46 -0400 | [diff] [blame] | 6210 | trans = btrfs_start_transaction(root, 5); |
| 6211 | if (IS_ERR(trans)) |
| 6212 | return PTR_ERR(trans); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 6213 | |
Li Zefan | 581bb05 | 2011-04-20 10:06:11 +0800 | [diff] [blame] | 6214 | err = btrfs_find_free_ino(root, &objectid); |
| 6215 | if (err) |
| 6216 | goto out_fail; |
| 6217 | |
Josef Bacik | aec7477 | 2008-07-24 12:12:38 -0400 | [diff] [blame] | 6218 | inode = btrfs_new_inode(trans, root, dir, dentry->d_name.name, |
Li Zefan | 33345d01 | 2011-04-20 10:31:50 +0800 | [diff] [blame] | 6219 | dentry->d_name.len, btrfs_ino(dir), objectid, |
Josef Bacik | d82a6f1 | 2011-05-11 15:26:06 -0400 | [diff] [blame] | 6220 | S_IFDIR | mode, &index); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 6221 | if (IS_ERR(inode)) { |
| 6222 | err = PTR_ERR(inode); |
| 6223 | goto out_fail; |
| 6224 | } |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 6225 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 6226 | drop_on_err = 1; |
Chris Mason | b0d5d10 | 2014-09-08 13:08:51 -0700 | [diff] [blame] | 6227 | /* these must be set before we unlock the inode */ |
| 6228 | inode->i_op = &btrfs_dir_inode_operations; |
| 6229 | inode->i_fop = &btrfs_dir_file_operations; |
Josef Bacik | 33268ea | 2008-07-24 12:16:36 -0400 | [diff] [blame] | 6230 | |
Eric Paris | 2a7dba3 | 2011-02-01 11:05:39 -0500 | [diff] [blame] | 6231 | err = btrfs_init_inode_security(trans, inode, dir, &dentry->d_name); |
Josef Bacik | 33268ea | 2008-07-24 12:16:36 -0400 | [diff] [blame] | 6232 | if (err) |
Chris Mason | b0d5d10 | 2014-09-08 13:08:51 -0700 | [diff] [blame] | 6233 | goto out_fail_inode; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 6234 | |
Chris Mason | dbe674a | 2008-07-17 12:54:05 -0400 | [diff] [blame] | 6235 | btrfs_i_size_write(inode, 0); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 6236 | err = btrfs_update_inode(trans, root, inode); |
| 6237 | if (err) |
Chris Mason | b0d5d10 | 2014-09-08 13:08:51 -0700 | [diff] [blame] | 6238 | goto out_fail_inode; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 6239 | |
Josef Bacik | a1b075d | 2010-11-19 20:36:11 +0000 | [diff] [blame] | 6240 | err = btrfs_add_link(trans, dir, inode, dentry->d_name.name, |
| 6241 | dentry->d_name.len, 0, index); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 6242 | if (err) |
Chris Mason | b0d5d10 | 2014-09-08 13:08:51 -0700 | [diff] [blame] | 6243 | goto out_fail_inode; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 6244 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 6245 | d_instantiate(dentry, inode); |
Chris Mason | b0d5d10 | 2014-09-08 13:08:51 -0700 | [diff] [blame] | 6246 | /* |
| 6247 | * mkdir is special. We're unlocking after we call d_instantiate |
| 6248 | * to avoid a race with nfsd calling d_instantiate. |
| 6249 | */ |
| 6250 | unlock_new_inode(inode); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 6251 | drop_on_err = 0; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 6252 | |
| 6253 | out_fail: |
Josef Bacik | 7ad85bb | 2012-01-12 19:10:12 -0500 | [diff] [blame] | 6254 | btrfs_end_transaction(trans, root); |
Wang Shilong | c7cfb8a | 2014-12-24 14:45:30 +0800 | [diff] [blame] | 6255 | if (drop_on_err) { |
| 6256 | inode_dec_link_count(inode); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 6257 | iput(inode); |
Wang Shilong | c7cfb8a | 2014-12-24 14:45:30 +0800 | [diff] [blame] | 6258 | } |
Josef Bacik | c581afc | 2014-02-06 16:06:06 -0500 | [diff] [blame] | 6259 | btrfs_balance_delayed_items(root); |
Liu Bo | b53d3f5 | 2012-11-14 14:34:34 +0000 | [diff] [blame] | 6260 | btrfs_btree_balance_dirty(root); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 6261 | return err; |
Chris Mason | b0d5d10 | 2014-09-08 13:08:51 -0700 | [diff] [blame] | 6262 | |
| 6263 | out_fail_inode: |
| 6264 | unlock_new_inode(inode); |
| 6265 | goto out_fail; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 6266 | } |
| 6267 | |
Qu Wenruo | e6c4efd | 2014-09-17 11:53:35 +0800 | [diff] [blame] | 6268 | /* Find next extent map of a given extent map, caller needs to ensure locks */ |
| 6269 | static struct extent_map *next_extent_map(struct extent_map *em) |
| 6270 | { |
| 6271 | struct rb_node *next; |
| 6272 | |
| 6273 | next = rb_next(&em->rb_node); |
| 6274 | if (!next) |
| 6275 | return NULL; |
| 6276 | return container_of(next, struct extent_map, rb_node); |
| 6277 | } |
| 6278 | |
| 6279 | static struct extent_map *prev_extent_map(struct extent_map *em) |
| 6280 | { |
| 6281 | struct rb_node *prev; |
| 6282 | |
| 6283 | prev = rb_prev(&em->rb_node); |
| 6284 | if (!prev) |
| 6285 | return NULL; |
| 6286 | return container_of(prev, struct extent_map, rb_node); |
| 6287 | } |
| 6288 | |
Chris Mason | d352ac6 | 2008-09-29 15:18:18 -0400 | [diff] [blame] | 6289 | /* helper for btfs_get_extent. Given an existing extent in the tree, |
Qu Wenruo | e6c4efd | 2014-09-17 11:53:35 +0800 | [diff] [blame] | 6290 | * the existing extent is the nearest extent to map_start, |
Chris Mason | d352ac6 | 2008-09-29 15:18:18 -0400 | [diff] [blame] | 6291 | * 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] | 6292 | * the best fitted new extent into the tree. |
Chris Mason | d352ac6 | 2008-09-29 15:18:18 -0400 | [diff] [blame] | 6293 | */ |
Chris Mason | 3b95151 | 2008-04-17 11:29:12 -0400 | [diff] [blame] | 6294 | static int merge_extent_mapping(struct extent_map_tree *em_tree, |
| 6295 | struct extent_map *existing, |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 6296 | struct extent_map *em, |
Qu Wenruo | 51f395a | 2014-08-08 13:06:20 +0800 | [diff] [blame] | 6297 | u64 map_start) |
Chris Mason | 3b95151 | 2008-04-17 11:29:12 -0400 | [diff] [blame] | 6298 | { |
Qu Wenruo | e6c4efd | 2014-09-17 11:53:35 +0800 | [diff] [blame] | 6299 | struct extent_map *prev; |
| 6300 | struct extent_map *next; |
| 6301 | u64 start; |
| 6302 | u64 end; |
Chris Mason | 3b95151 | 2008-04-17 11:29:12 -0400 | [diff] [blame] | 6303 | u64 start_diff; |
Chris Mason | 3b95151 | 2008-04-17 11:29:12 -0400 | [diff] [blame] | 6304 | |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 6305 | BUG_ON(map_start < em->start || map_start >= extent_map_end(em)); |
Qu Wenruo | e6c4efd | 2014-09-17 11:53:35 +0800 | [diff] [blame] | 6306 | |
| 6307 | if (existing->start > map_start) { |
| 6308 | next = existing; |
| 6309 | prev = prev_extent_map(next); |
| 6310 | } else { |
| 6311 | prev = existing; |
| 6312 | next = next_extent_map(prev); |
| 6313 | } |
| 6314 | |
| 6315 | start = prev ? extent_map_end(prev) : em->start; |
| 6316 | start = max_t(u64, start, em->start); |
| 6317 | end = next ? next->start : extent_map_end(em); |
| 6318 | end = min_t(u64, end, extent_map_end(em)); |
| 6319 | start_diff = start - em->start; |
| 6320 | em->start = start; |
| 6321 | em->len = end - start; |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 6322 | if (em->block_start < EXTENT_MAP_LAST_BYTE && |
| 6323 | !test_bit(EXTENT_FLAG_COMPRESSED, &em->flags)) { |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 6324 | em->block_start += start_diff; |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 6325 | em->block_len -= start_diff; |
| 6326 | } |
Josef Bacik | 09a2a8f9 | 2013-04-05 16:51:15 -0400 | [diff] [blame] | 6327 | return add_extent_mapping(em_tree, em, 0); |
Chris Mason | 3b95151 | 2008-04-17 11:29:12 -0400 | [diff] [blame] | 6328 | } |
| 6329 | |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 6330 | static noinline int uncompress_inline(struct btrfs_path *path, |
| 6331 | struct inode *inode, struct page *page, |
| 6332 | size_t pg_offset, u64 extent_offset, |
| 6333 | struct btrfs_file_extent_item *item) |
| 6334 | { |
| 6335 | int ret; |
| 6336 | struct extent_buffer *leaf = path->nodes[0]; |
| 6337 | char *tmp; |
| 6338 | size_t max_size; |
| 6339 | unsigned long inline_size; |
| 6340 | unsigned long ptr; |
Li Zefan | 261507a0 | 2010-12-17 14:21:50 +0800 | [diff] [blame] | 6341 | int compress_type; |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 6342 | |
| 6343 | WARN_ON(pg_offset != 0); |
Li Zefan | 261507a0 | 2010-12-17 14:21:50 +0800 | [diff] [blame] | 6344 | compress_type = btrfs_file_extent_compression(leaf, item); |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 6345 | max_size = btrfs_file_extent_ram_bytes(leaf, item); |
| 6346 | inline_size = btrfs_file_extent_inline_item_len(leaf, |
Ross Kirk | dd3cc16 | 2013-09-16 15:58:09 +0100 | [diff] [blame] | 6347 | btrfs_item_nr(path->slots[0])); |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 6348 | tmp = kmalloc(inline_size, GFP_NOFS); |
Tsutomu Itoh | 8d41371 | 2011-04-25 19:43:52 -0400 | [diff] [blame] | 6349 | if (!tmp) |
| 6350 | return -ENOMEM; |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 6351 | ptr = btrfs_file_extent_inline_start(item); |
| 6352 | |
| 6353 | read_extent_buffer(leaf, tmp, ptr, inline_size); |
| 6354 | |
Chris Mason | 5b050f0 | 2008-11-11 09:34:41 -0500 | [diff] [blame] | 6355 | max_size = min_t(unsigned long, PAGE_CACHE_SIZE, max_size); |
Li Zefan | 261507a0 | 2010-12-17 14:21:50 +0800 | [diff] [blame] | 6356 | ret = btrfs_decompress(compress_type, tmp, page, |
| 6357 | extent_offset, inline_size, max_size); |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 6358 | kfree(tmp); |
Zach Brown | 166ae5a | 2014-05-09 17:15:10 -0400 | [diff] [blame] | 6359 | return ret; |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 6360 | } |
| 6361 | |
Chris Mason | d352ac6 | 2008-09-29 15:18:18 -0400 | [diff] [blame] | 6362 | /* |
| 6363 | * 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] | 6364 | * the ugly parts come from merging extents from the disk with the in-ram |
| 6365 | * representation. This gets more complex because of the data=ordered code, |
Chris Mason | d352ac6 | 2008-09-29 15:18:18 -0400 | [diff] [blame] | 6366 | * where the in-ram extents might be locked pending data=ordered completion. |
| 6367 | * |
| 6368 | * This also copies inline extents directly into the page. |
| 6369 | */ |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 6370 | |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 6371 | struct extent_map *btrfs_get_extent(struct inode *inode, struct page *page, |
Chris Mason | 70dec80 | 2008-01-29 09:59:12 -0500 | [diff] [blame] | 6372 | size_t pg_offset, u64 start, u64 len, |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 6373 | int create) |
| 6374 | { |
| 6375 | int ret; |
| 6376 | int err = 0; |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 6377 | u64 extent_start = 0; |
| 6378 | u64 extent_end = 0; |
Li Zefan | 33345d01 | 2011-04-20 10:31:50 +0800 | [diff] [blame] | 6379 | u64 objectid = btrfs_ino(inode); |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 6380 | u32 found_type; |
Chris Mason | f421950 | 2008-07-22 11:18:09 -0400 | [diff] [blame] | 6381 | struct btrfs_path *path = NULL; |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 6382 | struct btrfs_root *root = BTRFS_I(inode)->root; |
| 6383 | struct btrfs_file_extent_item *item; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 6384 | struct extent_buffer *leaf; |
| 6385 | struct btrfs_key found_key; |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 6386 | struct extent_map *em = NULL; |
| 6387 | struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree; |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 6388 | struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree; |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 6389 | struct btrfs_trans_handle *trans = NULL; |
Filipe Manana | 7ffbb59 | 2014-06-09 03:48:05 +0100 | [diff] [blame] | 6390 | const bool new_inline = !page || create; |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 6391 | |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 6392 | again: |
Chris Mason | 890871b | 2009-09-02 16:24:52 -0400 | [diff] [blame] | 6393 | read_lock(&em_tree->lock); |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 6394 | em = lookup_extent_mapping(em_tree, start, len); |
Chris Mason | a061fc8 | 2008-05-07 11:43:44 -0400 | [diff] [blame] | 6395 | if (em) |
| 6396 | em->bdev = root->fs_info->fs_devices->latest_bdev; |
Chris Mason | 890871b | 2009-09-02 16:24:52 -0400 | [diff] [blame] | 6397 | read_unlock(&em_tree->lock); |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 6398 | |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 6399 | if (em) { |
Chris Mason | e1c4b74 | 2008-04-22 13:26:46 -0400 | [diff] [blame] | 6400 | if (em->start > start || em->start + em->len <= start) |
| 6401 | free_extent_map(em); |
| 6402 | else if (em->block_start == EXTENT_MAP_INLINE && page) |
Chris Mason | 70dec80 | 2008-01-29 09:59:12 -0500 | [diff] [blame] | 6403 | free_extent_map(em); |
| 6404 | else |
| 6405 | goto out; |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 6406 | } |
David Sterba | 172ddd6 | 2011-04-21 00:48:27 +0200 | [diff] [blame] | 6407 | em = alloc_extent_map(); |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 6408 | if (!em) { |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 6409 | err = -ENOMEM; |
| 6410 | goto out; |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 6411 | } |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 6412 | em->bdev = root->fs_info->fs_devices->latest_bdev; |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 6413 | em->start = EXTENT_MAP_HOLE; |
Chris Mason | 445a694 | 2008-11-10 11:53:33 -0500 | [diff] [blame] | 6414 | em->orig_start = EXTENT_MAP_HOLE; |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 6415 | em->len = (u64)-1; |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 6416 | em->block_len = (u64)-1; |
Chris Mason | f421950 | 2008-07-22 11:18:09 -0400 | [diff] [blame] | 6417 | |
| 6418 | if (!path) { |
| 6419 | path = btrfs_alloc_path(); |
Josef Bacik | 026fd31 | 2011-05-13 10:32:11 -0400 | [diff] [blame] | 6420 | if (!path) { |
| 6421 | err = -ENOMEM; |
| 6422 | goto out; |
| 6423 | } |
| 6424 | /* |
| 6425 | * Chances are we'll be called again, so go ahead and do |
| 6426 | * readahead |
| 6427 | */ |
| 6428 | path->reada = 1; |
Chris Mason | f421950 | 2008-07-22 11:18:09 -0400 | [diff] [blame] | 6429 | } |
| 6430 | |
Chris Mason | 179e29e | 2007-11-01 11:28:41 -0400 | [diff] [blame] | 6431 | ret = btrfs_lookup_file_extent(trans, root, path, |
| 6432 | objectid, start, trans != NULL); |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 6433 | if (ret < 0) { |
| 6434 | err = ret; |
| 6435 | goto out; |
| 6436 | } |
| 6437 | |
| 6438 | if (ret != 0) { |
| 6439 | if (path->slots[0] == 0) |
| 6440 | goto not_found; |
| 6441 | path->slots[0]--; |
| 6442 | } |
| 6443 | |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 6444 | leaf = path->nodes[0]; |
| 6445 | item = btrfs_item_ptr(leaf, path->slots[0], |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 6446 | struct btrfs_file_extent_item); |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 6447 | /* are we inside the extent that was found? */ |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 6448 | btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]); |
David Sterba | 962a298 | 2014-06-04 18:41:45 +0200 | [diff] [blame] | 6449 | found_type = found_key.type; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 6450 | if (found_key.objectid != objectid || |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 6451 | found_type != BTRFS_EXTENT_DATA_KEY) { |
Josef Bacik | 25a5034 | 2013-10-14 12:08:38 -0400 | [diff] [blame] | 6452 | /* |
| 6453 | * If we backup past the first extent we want to move forward |
| 6454 | * and see if there is an extent in front of us, otherwise we'll |
| 6455 | * say there is a hole for our whole search range which can |
| 6456 | * cause problems. |
| 6457 | */ |
| 6458 | extent_end = start; |
| 6459 | goto next; |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 6460 | } |
| 6461 | |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 6462 | found_type = btrfs_file_extent_type(leaf, item); |
| 6463 | extent_start = found_key.offset; |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 6464 | if (found_type == BTRFS_FILE_EXTENT_REG || |
| 6465 | found_type == BTRFS_FILE_EXTENT_PREALLOC) { |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 6466 | extent_end = extent_start + |
Chris Mason | db94535 | 2007-10-15 16:15:53 -0400 | [diff] [blame] | 6467 | btrfs_file_extent_num_bytes(leaf, item); |
Yan Zheng | 9036c10 | 2008-10-30 14:19:41 -0400 | [diff] [blame] | 6468 | } else if (found_type == BTRFS_FILE_EXTENT_INLINE) { |
| 6469 | size_t size; |
Chris Mason | 514ac8a | 2014-01-03 21:07:00 -0800 | [diff] [blame] | 6470 | size = btrfs_file_extent_inline_len(leaf, path->slots[0], item); |
Qu Wenruo | fda2832 | 2013-02-26 08:10:22 +0000 | [diff] [blame] | 6471 | extent_end = ALIGN(extent_start + size, root->sectorsize); |
Yan Zheng | 9036c10 | 2008-10-30 14:19:41 -0400 | [diff] [blame] | 6472 | } |
Josef Bacik | 25a5034 | 2013-10-14 12:08:38 -0400 | [diff] [blame] | 6473 | next: |
Yan Zheng | 9036c10 | 2008-10-30 14:19:41 -0400 | [diff] [blame] | 6474 | if (start >= extent_end) { |
| 6475 | path->slots[0]++; |
| 6476 | if (path->slots[0] >= btrfs_header_nritems(leaf)) { |
| 6477 | ret = btrfs_next_leaf(root, path); |
| 6478 | if (ret < 0) { |
| 6479 | err = ret; |
| 6480 | goto out; |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 6481 | } |
Yan Zheng | 9036c10 | 2008-10-30 14:19:41 -0400 | [diff] [blame] | 6482 | if (ret > 0) |
| 6483 | goto not_found; |
| 6484 | leaf = path->nodes[0]; |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 6485 | } |
Yan Zheng | 9036c10 | 2008-10-30 14:19:41 -0400 | [diff] [blame] | 6486 | btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]); |
| 6487 | if (found_key.objectid != objectid || |
| 6488 | found_key.type != BTRFS_EXTENT_DATA_KEY) |
| 6489 | goto not_found; |
| 6490 | if (start + len <= found_key.offset) |
| 6491 | goto not_found; |
Wang Shilong | e2eca69 | 2014-07-17 11:44:14 +0800 | [diff] [blame] | 6492 | if (start > found_key.offset) |
| 6493 | goto next; |
Yan Zheng | 9036c10 | 2008-10-30 14:19:41 -0400 | [diff] [blame] | 6494 | em->start = start; |
Josef Bacik | 70c8a91 | 2012-10-11 16:54:30 -0400 | [diff] [blame] | 6495 | em->orig_start = start; |
Yan Zheng | 9036c10 | 2008-10-30 14:19:41 -0400 | [diff] [blame] | 6496 | em->len = found_key.offset - start; |
| 6497 | goto not_found_em; |
| 6498 | } |
| 6499 | |
Filipe Manana | 7ffbb59 | 2014-06-09 03:48:05 +0100 | [diff] [blame] | 6500 | btrfs_extent_item_to_extent_map(inode, path, item, new_inline, em); |
| 6501 | |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 6502 | if (found_type == BTRFS_FILE_EXTENT_REG || |
| 6503 | found_type == BTRFS_FILE_EXTENT_PREALLOC) { |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 6504 | goto insert; |
| 6505 | } else if (found_type == BTRFS_FILE_EXTENT_INLINE) { |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 6506 | unsigned long ptr; |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 6507 | char *map; |
Chris Mason | 3326d1b | 2007-10-15 16:18:25 -0400 | [diff] [blame] | 6508 | size_t size; |
| 6509 | size_t extent_offset; |
| 6510 | size_t copy_size; |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 6511 | |
Filipe Manana | 7ffbb59 | 2014-06-09 03:48:05 +0100 | [diff] [blame] | 6512 | if (new_inline) |
Yan | 689f934 | 2007-10-29 11:41:07 -0400 | [diff] [blame] | 6513 | goto out; |
Yan | 689f934 | 2007-10-29 11:41:07 -0400 | [diff] [blame] | 6514 | |
Chris Mason | 514ac8a | 2014-01-03 21:07:00 -0800 | [diff] [blame] | 6515 | size = btrfs_file_extent_inline_len(leaf, path->slots[0], item); |
Yan Zheng | 9036c10 | 2008-10-30 14:19:41 -0400 | [diff] [blame] | 6516 | extent_offset = page_offset(page) + pg_offset - extent_start; |
Chris Mason | 70dec80 | 2008-01-29 09:59:12 -0500 | [diff] [blame] | 6517 | copy_size = min_t(u64, PAGE_CACHE_SIZE - pg_offset, |
Yan | 689f934 | 2007-10-29 11:41:07 -0400 | [diff] [blame] | 6518 | size - extent_offset); |
Chris Mason | 3326d1b | 2007-10-15 16:18:25 -0400 | [diff] [blame] | 6519 | em->start = extent_start + extent_offset; |
Qu Wenruo | fda2832 | 2013-02-26 08:10:22 +0000 | [diff] [blame] | 6520 | em->len = ALIGN(copy_size, root->sectorsize); |
Josef Bacik | b493968 | 2012-12-03 10:31:19 -0500 | [diff] [blame] | 6521 | em->orig_block_len = em->len; |
Josef Bacik | 70c8a91 | 2012-10-11 16:54:30 -0400 | [diff] [blame] | 6522 | em->orig_start = em->start; |
Yan | 689f934 | 2007-10-29 11:41:07 -0400 | [diff] [blame] | 6523 | ptr = btrfs_file_extent_inline_start(item) + extent_offset; |
Chris Mason | 179e29e | 2007-11-01 11:28:41 -0400 | [diff] [blame] | 6524 | if (create == 0 && !PageUptodate(page)) { |
Li Zefan | 261507a0 | 2010-12-17 14:21:50 +0800 | [diff] [blame] | 6525 | if (btrfs_file_extent_compression(leaf, item) != |
| 6526 | BTRFS_COMPRESS_NONE) { |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 6527 | ret = uncompress_inline(path, inode, page, |
| 6528 | pg_offset, |
| 6529 | extent_offset, item); |
Zach Brown | 166ae5a | 2014-05-09 17:15:10 -0400 | [diff] [blame] | 6530 | if (ret) { |
| 6531 | err = ret; |
| 6532 | goto out; |
| 6533 | } |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 6534 | } else { |
| 6535 | map = kmap(page); |
| 6536 | read_extent_buffer(leaf, map + pg_offset, ptr, |
| 6537 | copy_size); |
Chris Mason | 93c82d5 | 2009-09-11 12:36:29 -0400 | [diff] [blame] | 6538 | if (pg_offset + copy_size < PAGE_CACHE_SIZE) { |
| 6539 | memset(map + pg_offset + copy_size, 0, |
| 6540 | PAGE_CACHE_SIZE - pg_offset - |
| 6541 | copy_size); |
| 6542 | } |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 6543 | kunmap(page); |
| 6544 | } |
Chris Mason | 179e29e | 2007-11-01 11:28:41 -0400 | [diff] [blame] | 6545 | flush_dcache_page(page); |
| 6546 | } else if (create && PageUptodate(page)) { |
Jan Schmidt | 6bf7e08 | 2011-12-01 14:35:19 +0100 | [diff] [blame] | 6547 | BUG(); |
Chris Mason | 179e29e | 2007-11-01 11:28:41 -0400 | [diff] [blame] | 6548 | if (!trans) { |
| 6549 | kunmap(page); |
| 6550 | free_extent_map(em); |
| 6551 | em = NULL; |
Chris Mason | ff5714c | 2011-05-28 07:00:39 -0400 | [diff] [blame] | 6552 | |
David Sterba | b3b4aa7 | 2011-04-21 01:20:15 +0200 | [diff] [blame] | 6553 | btrfs_release_path(path); |
Josef Bacik | 7a7eaa4 | 2011-04-13 12:54:33 -0400 | [diff] [blame] | 6554 | trans = btrfs_join_transaction(root); |
Chris Mason | ff5714c | 2011-05-28 07:00:39 -0400 | [diff] [blame] | 6555 | |
Tsutomu Itoh | 3612b49 | 2011-01-25 02:51:38 +0000 | [diff] [blame] | 6556 | if (IS_ERR(trans)) |
| 6557 | return ERR_CAST(trans); |
Chris Mason | 179e29e | 2007-11-01 11:28:41 -0400 | [diff] [blame] | 6558 | goto again; |
| 6559 | } |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 6560 | map = kmap(page); |
Chris Mason | 70dec80 | 2008-01-29 09:59:12 -0500 | [diff] [blame] | 6561 | write_extent_buffer(leaf, map + pg_offset, ptr, |
Chris Mason | 179e29e | 2007-11-01 11:28:41 -0400 | [diff] [blame] | 6562 | copy_size); |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 6563 | kunmap(page); |
Chris Mason | 179e29e | 2007-11-01 11:28:41 -0400 | [diff] [blame] | 6564 | btrfs_mark_buffer_dirty(leaf); |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 6565 | } |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 6566 | set_extent_uptodate(io_tree, em->start, |
Arne Jansen | 507903b | 2011-04-06 10:02:20 +0000 | [diff] [blame] | 6567 | extent_map_end(em) - 1, NULL, GFP_NOFS); |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 6568 | goto insert; |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 6569 | } |
| 6570 | not_found: |
| 6571 | em->start = start; |
Josef Bacik | 70c8a91 | 2012-10-11 16:54:30 -0400 | [diff] [blame] | 6572 | em->orig_start = start; |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 6573 | em->len = len; |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 6574 | not_found_em: |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 6575 | em->block_start = EXTENT_MAP_HOLE; |
Yan Zheng | 9036c10 | 2008-10-30 14:19:41 -0400 | [diff] [blame] | 6576 | set_bit(EXTENT_FLAG_VACANCY, &em->flags); |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 6577 | insert: |
David Sterba | b3b4aa7 | 2011-04-21 01:20:15 +0200 | [diff] [blame] | 6578 | btrfs_release_path(path); |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 6579 | if (em->start > start || extent_map_end(em) <= start) { |
Simon Kirby | c2cf52e | 2013-03-19 22:41:23 +0000 | [diff] [blame] | 6580 | 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] | 6581 | em->start, em->len, start, len); |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 6582 | err = -EIO; |
| 6583 | goto out; |
| 6584 | } |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 6585 | |
| 6586 | err = 0; |
Chris Mason | 890871b | 2009-09-02 16:24:52 -0400 | [diff] [blame] | 6587 | write_lock(&em_tree->lock); |
Josef Bacik | 09a2a8f9 | 2013-04-05 16:51:15 -0400 | [diff] [blame] | 6588 | ret = add_extent_mapping(em_tree, em, 0); |
Chris Mason | 3b95151 | 2008-04-17 11:29:12 -0400 | [diff] [blame] | 6589 | /* it is possible that someone inserted the extent into the tree |
| 6590 | * while we had the lock dropped. It is also possible that |
| 6591 | * an overlapping map exists in the tree |
| 6592 | */ |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 6593 | if (ret == -EEXIST) { |
Chris Mason | 3b95151 | 2008-04-17 11:29:12 -0400 | [diff] [blame] | 6594 | struct extent_map *existing; |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 6595 | |
| 6596 | ret = 0; |
| 6597 | |
Qu Wenruo | e6c4efd | 2014-09-17 11:53:35 +0800 | [diff] [blame] | 6598 | existing = search_extent_mapping(em_tree, start, len); |
| 6599 | /* |
| 6600 | * existing will always be non-NULL, since there must be |
| 6601 | * extent causing the -EEXIST. |
| 6602 | */ |
| 6603 | if (start >= extent_map_end(existing) || |
Qu Wenruo | 32be3a1 | 2014-09-22 09:13:03 +0800 | [diff] [blame] | 6604 | start <= existing->start) { |
Qu Wenruo | e6c4efd | 2014-09-17 11:53:35 +0800 | [diff] [blame] | 6605 | /* |
| 6606 | * The existing extent map is the one nearest to |
| 6607 | * the [start, start + len) range which overlaps |
| 6608 | */ |
| 6609 | err = merge_extent_mapping(em_tree, existing, |
| 6610 | em, start); |
Chris Mason | e1c4b74 | 2008-04-22 13:26:46 -0400 | [diff] [blame] | 6611 | free_extent_map(existing); |
Qu Wenruo | e6c4efd | 2014-09-17 11:53:35 +0800 | [diff] [blame] | 6612 | if (err) { |
Chris Mason | 3b95151 | 2008-04-17 11:29:12 -0400 | [diff] [blame] | 6613 | free_extent_map(em); |
| 6614 | em = NULL; |
| 6615 | } |
| 6616 | } else { |
| 6617 | free_extent_map(em); |
| 6618 | em = existing; |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 6619 | err = 0; |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 6620 | } |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 6621 | } |
Chris Mason | 890871b | 2009-09-02 16:24:52 -0400 | [diff] [blame] | 6622 | write_unlock(&em_tree->lock); |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 6623 | out: |
liubo | 1abe9b8 | 2011-03-24 11:18:59 +0000 | [diff] [blame] | 6624 | |
Steven Rostedt | 4cd8587 | 2013-11-14 22:57:29 -0500 | [diff] [blame] | 6625 | trace_btrfs_get_extent(root, em); |
liubo | 1abe9b8 | 2011-03-24 11:18:59 +0000 | [diff] [blame] | 6626 | |
Chris Mason | f421950 | 2008-07-22 11:18:09 -0400 | [diff] [blame] | 6627 | if (path) |
| 6628 | btrfs_free_path(path); |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 6629 | if (trans) { |
| 6630 | ret = btrfs_end_transaction(trans, root); |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 6631 | if (!err) |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 6632 | err = ret; |
| 6633 | } |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 6634 | if (err) { |
| 6635 | free_extent_map(em); |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 6636 | return ERR_PTR(err); |
| 6637 | } |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 6638 | BUG_ON(!em); /* Error is always set */ |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 6639 | return em; |
| 6640 | } |
| 6641 | |
Chris Mason | ec29ed5 | 2011-02-23 16:23:20 -0500 | [diff] [blame] | 6642 | struct extent_map *btrfs_get_extent_fiemap(struct inode *inode, struct page *page, |
| 6643 | size_t pg_offset, u64 start, u64 len, |
| 6644 | int create) |
| 6645 | { |
| 6646 | struct extent_map *em; |
| 6647 | struct extent_map *hole_em = NULL; |
| 6648 | u64 range_start = start; |
| 6649 | u64 end; |
| 6650 | u64 found; |
| 6651 | u64 found_end; |
| 6652 | int err = 0; |
| 6653 | |
| 6654 | em = btrfs_get_extent(inode, page, pg_offset, start, len, create); |
| 6655 | if (IS_ERR(em)) |
| 6656 | return em; |
| 6657 | if (em) { |
| 6658 | /* |
Liu Bo | f9e4fb5 | 2013-01-07 10:10:12 +0000 | [diff] [blame] | 6659 | * if our em maps to |
| 6660 | * - a hole or |
| 6661 | * - a pre-alloc extent, |
| 6662 | * there might actually be delalloc bytes behind it. |
Chris Mason | ec29ed5 | 2011-02-23 16:23:20 -0500 | [diff] [blame] | 6663 | */ |
Liu Bo | f9e4fb5 | 2013-01-07 10:10:12 +0000 | [diff] [blame] | 6664 | if (em->block_start != EXTENT_MAP_HOLE && |
| 6665 | !test_bit(EXTENT_FLAG_PREALLOC, &em->flags)) |
Chris Mason | ec29ed5 | 2011-02-23 16:23:20 -0500 | [diff] [blame] | 6666 | return em; |
| 6667 | else |
| 6668 | hole_em = em; |
| 6669 | } |
| 6670 | |
| 6671 | /* check to see if we've wrapped (len == -1 or similar) */ |
| 6672 | end = start + len; |
| 6673 | if (end < start) |
| 6674 | end = (u64)-1; |
| 6675 | else |
| 6676 | end -= 1; |
| 6677 | |
| 6678 | em = NULL; |
| 6679 | |
| 6680 | /* ok, we didn't find anything, lets look for delalloc */ |
| 6681 | found = count_range_bits(&BTRFS_I(inode)->io_tree, &range_start, |
| 6682 | end, len, EXTENT_DELALLOC, 1); |
| 6683 | found_end = range_start + found; |
| 6684 | if (found_end < range_start) |
| 6685 | found_end = (u64)-1; |
| 6686 | |
| 6687 | /* |
| 6688 | * we didn't find anything useful, return |
| 6689 | * the original results from get_extent() |
| 6690 | */ |
| 6691 | if (range_start > end || found_end <= start) { |
| 6692 | em = hole_em; |
| 6693 | hole_em = NULL; |
| 6694 | goto out; |
| 6695 | } |
| 6696 | |
| 6697 | /* adjust the range_start to make sure it doesn't |
| 6698 | * go backwards from the start they passed in |
| 6699 | */ |
Dulshani Gunawardhana | 6787125 | 2013-10-31 10:33:04 +0530 | [diff] [blame] | 6700 | range_start = max(start, range_start); |
Chris Mason | ec29ed5 | 2011-02-23 16:23:20 -0500 | [diff] [blame] | 6701 | found = found_end - range_start; |
| 6702 | |
| 6703 | if (found > 0) { |
| 6704 | u64 hole_start = start; |
| 6705 | u64 hole_len = len; |
| 6706 | |
David Sterba | 172ddd6 | 2011-04-21 00:48:27 +0200 | [diff] [blame] | 6707 | em = alloc_extent_map(); |
Chris Mason | ec29ed5 | 2011-02-23 16:23:20 -0500 | [diff] [blame] | 6708 | if (!em) { |
| 6709 | err = -ENOMEM; |
| 6710 | goto out; |
| 6711 | } |
| 6712 | /* |
| 6713 | * when btrfs_get_extent can't find anything it |
| 6714 | * returns one huge hole |
| 6715 | * |
| 6716 | * make sure what it found really fits our range, and |
| 6717 | * adjust to make sure it is based on the start from |
| 6718 | * the caller |
| 6719 | */ |
| 6720 | if (hole_em) { |
| 6721 | u64 calc_end = extent_map_end(hole_em); |
| 6722 | |
| 6723 | if (calc_end <= start || (hole_em->start > end)) { |
| 6724 | free_extent_map(hole_em); |
| 6725 | hole_em = NULL; |
| 6726 | } else { |
| 6727 | hole_start = max(hole_em->start, start); |
| 6728 | hole_len = calc_end - hole_start; |
| 6729 | } |
| 6730 | } |
| 6731 | em->bdev = NULL; |
| 6732 | if (hole_em && range_start > hole_start) { |
| 6733 | /* our hole starts before our delalloc, so we |
| 6734 | * have to return just the parts of the hole |
| 6735 | * that go until the delalloc starts |
| 6736 | */ |
| 6737 | em->len = min(hole_len, |
| 6738 | range_start - hole_start); |
| 6739 | em->start = hole_start; |
| 6740 | em->orig_start = hole_start; |
| 6741 | /* |
| 6742 | * don't adjust block start at all, |
| 6743 | * it is fixed at EXTENT_MAP_HOLE |
| 6744 | */ |
| 6745 | em->block_start = hole_em->block_start; |
| 6746 | em->block_len = hole_len; |
Liu Bo | f9e4fb5 | 2013-01-07 10:10:12 +0000 | [diff] [blame] | 6747 | if (test_bit(EXTENT_FLAG_PREALLOC, &hole_em->flags)) |
| 6748 | set_bit(EXTENT_FLAG_PREALLOC, &em->flags); |
Chris Mason | ec29ed5 | 2011-02-23 16:23:20 -0500 | [diff] [blame] | 6749 | } else { |
| 6750 | em->start = range_start; |
| 6751 | em->len = found; |
| 6752 | em->orig_start = range_start; |
| 6753 | em->block_start = EXTENT_MAP_DELALLOC; |
| 6754 | em->block_len = found; |
| 6755 | } |
| 6756 | } else if (hole_em) { |
| 6757 | return hole_em; |
| 6758 | } |
| 6759 | out: |
| 6760 | |
| 6761 | free_extent_map(hole_em); |
| 6762 | if (err) { |
| 6763 | free_extent_map(em); |
| 6764 | return ERR_PTR(err); |
| 6765 | } |
| 6766 | return em; |
| 6767 | } |
| 6768 | |
Josef Bacik | 4b46fce | 2010-05-23 11:00:55 -0400 | [diff] [blame] | 6769 | static struct extent_map *btrfs_new_extent_direct(struct inode *inode, |
| 6770 | u64 start, u64 len) |
| 6771 | { |
| 6772 | struct btrfs_root *root = BTRFS_I(inode)->root; |
Josef Bacik | 70c8a91 | 2012-10-11 16:54:30 -0400 | [diff] [blame] | 6773 | struct extent_map *em; |
Josef Bacik | 4b46fce | 2010-05-23 11:00:55 -0400 | [diff] [blame] | 6774 | struct btrfs_key ins; |
| 6775 | u64 alloc_hint; |
| 6776 | int ret; |
Josef Bacik | 4b46fce | 2010-05-23 11:00:55 -0400 | [diff] [blame] | 6777 | |
Josef Bacik | 4b46fce | 2010-05-23 11:00:55 -0400 | [diff] [blame] | 6778 | alloc_hint = get_extent_allocation_hint(inode, start, len); |
Josef Bacik | 0036158 | 2013-08-14 14:02:47 -0400 | [diff] [blame] | 6779 | ret = btrfs_reserve_extent(root, len, root->sectorsize, 0, |
Miao Xie | e570fd2 | 2014-06-19 10:42:50 +0800 | [diff] [blame] | 6780 | alloc_hint, &ins, 1, 1); |
Josef Bacik | 0036158 | 2013-08-14 14:02:47 -0400 | [diff] [blame] | 6781 | if (ret) |
| 6782 | return ERR_PTR(ret); |
Josef Bacik | 4b46fce | 2010-05-23 11:00:55 -0400 | [diff] [blame] | 6783 | |
Josef Bacik | 70c8a91 | 2012-10-11 16:54:30 -0400 | [diff] [blame] | 6784 | em = create_pinned_em(inode, start, ins.offset, start, ins.objectid, |
Josef Bacik | cc95bef | 2013-04-04 14:31:27 -0400 | [diff] [blame] | 6785 | ins.offset, ins.offset, ins.offset, 0); |
Josef Bacik | 0036158 | 2013-08-14 14:02:47 -0400 | [diff] [blame] | 6786 | if (IS_ERR(em)) { |
Miao Xie | e570fd2 | 2014-06-19 10:42:50 +0800 | [diff] [blame] | 6787 | btrfs_free_reserved_extent(root, ins.objectid, ins.offset, 1); |
Josef Bacik | 0036158 | 2013-08-14 14:02:47 -0400 | [diff] [blame] | 6788 | return em; |
| 6789 | } |
Josef Bacik | 4b46fce | 2010-05-23 11:00:55 -0400 | [diff] [blame] | 6790 | |
| 6791 | ret = btrfs_add_ordered_extent_dio(inode, start, ins.objectid, |
| 6792 | ins.offset, ins.offset, 0); |
| 6793 | if (ret) { |
Miao Xie | e570fd2 | 2014-06-19 10:42:50 +0800 | [diff] [blame] | 6794 | btrfs_free_reserved_extent(root, ins.objectid, ins.offset, 1); |
Josef Bacik | 0036158 | 2013-08-14 14:02:47 -0400 | [diff] [blame] | 6795 | free_extent_map(em); |
| 6796 | return ERR_PTR(ret); |
Josef Bacik | 4b46fce | 2010-05-23 11:00:55 -0400 | [diff] [blame] | 6797 | } |
Josef Bacik | 0036158 | 2013-08-14 14:02:47 -0400 | [diff] [blame] | 6798 | |
Josef Bacik | 4b46fce | 2010-05-23 11:00:55 -0400 | [diff] [blame] | 6799 | return em; |
| 6800 | } |
| 6801 | |
Chris Mason | 46bfbb5 | 2010-05-26 11:04:10 -0400 | [diff] [blame] | 6802 | /* |
| 6803 | * returns 1 when the nocow is safe, < 1 on error, 0 if the |
| 6804 | * block must be cow'd |
| 6805 | */ |
Josef Bacik | 0036158 | 2013-08-14 14:02:47 -0400 | [diff] [blame] | 6806 | noinline int can_nocow_extent(struct inode *inode, u64 offset, u64 *len, |
Josef Bacik | 7ee9e44 | 2013-06-21 16:37:03 -0400 | [diff] [blame] | 6807 | u64 *orig_start, u64 *orig_block_len, |
| 6808 | u64 *ram_bytes) |
Chris Mason | 46bfbb5 | 2010-05-26 11:04:10 -0400 | [diff] [blame] | 6809 | { |
Josef Bacik | 0036158 | 2013-08-14 14:02:47 -0400 | [diff] [blame] | 6810 | struct btrfs_trans_handle *trans; |
Chris Mason | 46bfbb5 | 2010-05-26 11:04:10 -0400 | [diff] [blame] | 6811 | struct btrfs_path *path; |
| 6812 | int ret; |
| 6813 | struct extent_buffer *leaf; |
| 6814 | struct btrfs_root *root = BTRFS_I(inode)->root; |
Miao Xie | 7b2b708 | 2014-02-27 13:58:05 +0800 | [diff] [blame] | 6815 | struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree; |
Chris Mason | 46bfbb5 | 2010-05-26 11:04:10 -0400 | [diff] [blame] | 6816 | struct btrfs_file_extent_item *fi; |
| 6817 | struct btrfs_key key; |
| 6818 | u64 disk_bytenr; |
| 6819 | u64 backref_offset; |
| 6820 | u64 extent_end; |
| 6821 | u64 num_bytes; |
| 6822 | int slot; |
| 6823 | int found_type; |
Josef Bacik | 7ee9e44 | 2013-06-21 16:37:03 -0400 | [diff] [blame] | 6824 | bool nocow = (BTRFS_I(inode)->flags & BTRFS_INODE_NODATACOW); |
Miao Xie | e77751a | 2013-12-27 21:11:50 +0800 | [diff] [blame] | 6825 | |
Chris Mason | 46bfbb5 | 2010-05-26 11:04:10 -0400 | [diff] [blame] | 6826 | path = btrfs_alloc_path(); |
| 6827 | if (!path) |
| 6828 | return -ENOMEM; |
| 6829 | |
Josef Bacik | 0036158 | 2013-08-14 14:02:47 -0400 | [diff] [blame] | 6830 | ret = btrfs_lookup_file_extent(NULL, root, path, btrfs_ino(inode), |
Chris Mason | 46bfbb5 | 2010-05-26 11:04:10 -0400 | [diff] [blame] | 6831 | offset, 0); |
| 6832 | if (ret < 0) |
| 6833 | goto out; |
| 6834 | |
| 6835 | slot = path->slots[0]; |
| 6836 | if (ret == 1) { |
| 6837 | if (slot == 0) { |
| 6838 | /* can't find the item, must cow */ |
| 6839 | ret = 0; |
| 6840 | goto out; |
| 6841 | } |
| 6842 | slot--; |
| 6843 | } |
| 6844 | ret = 0; |
| 6845 | leaf = path->nodes[0]; |
| 6846 | btrfs_item_key_to_cpu(leaf, &key, slot); |
Li Zefan | 33345d01 | 2011-04-20 10:31:50 +0800 | [diff] [blame] | 6847 | if (key.objectid != btrfs_ino(inode) || |
Chris Mason | 46bfbb5 | 2010-05-26 11:04:10 -0400 | [diff] [blame] | 6848 | key.type != BTRFS_EXTENT_DATA_KEY) { |
| 6849 | /* not our file or wrong item type, must cow */ |
| 6850 | goto out; |
| 6851 | } |
| 6852 | |
| 6853 | if (key.offset > offset) { |
| 6854 | /* Wrong offset, must cow */ |
| 6855 | goto out; |
| 6856 | } |
| 6857 | |
| 6858 | fi = btrfs_item_ptr(leaf, slot, struct btrfs_file_extent_item); |
| 6859 | found_type = btrfs_file_extent_type(leaf, fi); |
| 6860 | if (found_type != BTRFS_FILE_EXTENT_REG && |
| 6861 | found_type != BTRFS_FILE_EXTENT_PREALLOC) { |
| 6862 | /* not a regular extent, must cow */ |
| 6863 | goto out; |
| 6864 | } |
Josef Bacik | 7ee9e44 | 2013-06-21 16:37:03 -0400 | [diff] [blame] | 6865 | |
| 6866 | if (!nocow && found_type == BTRFS_FILE_EXTENT_REG) |
| 6867 | goto out; |
| 6868 | |
Miao Xie | e77751a | 2013-12-27 21:11:50 +0800 | [diff] [blame] | 6869 | extent_end = key.offset + btrfs_file_extent_num_bytes(leaf, fi); |
| 6870 | if (extent_end <= offset) |
| 6871 | goto out; |
| 6872 | |
Chris Mason | 46bfbb5 | 2010-05-26 11:04:10 -0400 | [diff] [blame] | 6873 | disk_bytenr = btrfs_file_extent_disk_bytenr(leaf, fi); |
Josef Bacik | 7ee9e44 | 2013-06-21 16:37:03 -0400 | [diff] [blame] | 6874 | if (disk_bytenr == 0) |
| 6875 | goto out; |
| 6876 | |
| 6877 | if (btrfs_file_extent_compression(leaf, fi) || |
| 6878 | btrfs_file_extent_encryption(leaf, fi) || |
| 6879 | btrfs_file_extent_other_encoding(leaf, fi)) |
| 6880 | goto out; |
| 6881 | |
Chris Mason | 46bfbb5 | 2010-05-26 11:04:10 -0400 | [diff] [blame] | 6882 | backref_offset = btrfs_file_extent_offset(leaf, fi); |
| 6883 | |
Josef Bacik | 7ee9e44 | 2013-06-21 16:37:03 -0400 | [diff] [blame] | 6884 | if (orig_start) { |
| 6885 | *orig_start = key.offset - backref_offset; |
| 6886 | *orig_block_len = btrfs_file_extent_disk_num_bytes(leaf, fi); |
| 6887 | *ram_bytes = btrfs_file_extent_ram_bytes(leaf, fi); |
| 6888 | } |
Josef Bacik | eb384b5 | 2013-04-24 16:32:55 -0400 | [diff] [blame] | 6889 | |
Chris Mason | 46bfbb5 | 2010-05-26 11:04:10 -0400 | [diff] [blame] | 6890 | if (btrfs_extent_readonly(root, disk_bytenr)) |
| 6891 | goto out; |
Miao Xie | 7b2b708 | 2014-02-27 13:58:05 +0800 | [diff] [blame] | 6892 | |
| 6893 | num_bytes = min(offset + *len, extent_end) - offset; |
| 6894 | if (!nocow && found_type == BTRFS_FILE_EXTENT_PREALLOC) { |
| 6895 | u64 range_end; |
| 6896 | |
| 6897 | range_end = round_up(offset + num_bytes, root->sectorsize) - 1; |
| 6898 | ret = test_range_bit(io_tree, offset, range_end, |
| 6899 | EXTENT_DELALLOC, 0, NULL); |
| 6900 | if (ret) { |
| 6901 | ret = -EAGAIN; |
| 6902 | goto out; |
| 6903 | } |
| 6904 | } |
| 6905 | |
Josef Bacik | 1bda19e | 2013-10-18 12:10:36 -0400 | [diff] [blame] | 6906 | btrfs_release_path(path); |
Chris Mason | 46bfbb5 | 2010-05-26 11:04:10 -0400 | [diff] [blame] | 6907 | |
| 6908 | /* |
| 6909 | * look for other files referencing this extent, if we |
| 6910 | * find any we must cow |
| 6911 | */ |
Josef Bacik | 0036158 | 2013-08-14 14:02:47 -0400 | [diff] [blame] | 6912 | trans = btrfs_join_transaction(root); |
| 6913 | if (IS_ERR(trans)) { |
| 6914 | ret = 0; |
Chris Mason | 46bfbb5 | 2010-05-26 11:04:10 -0400 | [diff] [blame] | 6915 | goto out; |
Josef Bacik | 0036158 | 2013-08-14 14:02:47 -0400 | [diff] [blame] | 6916 | } |
| 6917 | |
| 6918 | ret = btrfs_cross_ref_exist(trans, root, btrfs_ino(inode), |
| 6919 | key.offset - backref_offset, disk_bytenr); |
| 6920 | btrfs_end_transaction(trans, root); |
| 6921 | if (ret) { |
| 6922 | ret = 0; |
| 6923 | goto out; |
| 6924 | } |
Chris Mason | 46bfbb5 | 2010-05-26 11:04:10 -0400 | [diff] [blame] | 6925 | |
| 6926 | /* |
| 6927 | * adjust disk_bytenr and num_bytes to cover just the bytes |
| 6928 | * in this extent we are about to write. If there |
| 6929 | * are any csums in that range we have to cow in order |
| 6930 | * to keep the csums correct |
| 6931 | */ |
| 6932 | disk_bytenr += backref_offset; |
| 6933 | disk_bytenr += offset - key.offset; |
Chris Mason | 46bfbb5 | 2010-05-26 11:04:10 -0400 | [diff] [blame] | 6934 | if (csum_exist_in_range(root, disk_bytenr, num_bytes)) |
| 6935 | goto out; |
| 6936 | /* |
| 6937 | * all of the above have passed, it is safe to overwrite this extent |
| 6938 | * without cow |
| 6939 | */ |
Josef Bacik | eb384b5 | 2013-04-24 16:32:55 -0400 | [diff] [blame] | 6940 | *len = num_bytes; |
Chris Mason | 46bfbb5 | 2010-05-26 11:04:10 -0400 | [diff] [blame] | 6941 | ret = 1; |
| 6942 | out: |
| 6943 | btrfs_free_path(path); |
| 6944 | return ret; |
| 6945 | } |
| 6946 | |
Alex Gartrell | fc4adbf | 2014-05-20 13:07:56 -0700 | [diff] [blame] | 6947 | bool btrfs_page_exists_in_range(struct inode *inode, loff_t start, loff_t end) |
| 6948 | { |
| 6949 | struct radix_tree_root *root = &inode->i_mapping->page_tree; |
| 6950 | int found = false; |
| 6951 | void **pagep = NULL; |
| 6952 | struct page *page = NULL; |
| 6953 | int start_idx; |
| 6954 | int end_idx; |
| 6955 | |
| 6956 | start_idx = start >> PAGE_CACHE_SHIFT; |
| 6957 | |
| 6958 | /* |
| 6959 | * end is the last byte in the last page. end == start is legal |
| 6960 | */ |
| 6961 | end_idx = end >> PAGE_CACHE_SHIFT; |
| 6962 | |
| 6963 | rcu_read_lock(); |
| 6964 | |
| 6965 | /* Most of the code in this while loop is lifted from |
| 6966 | * find_get_page. It's been modified to begin searching from a |
| 6967 | * page and return just the first page found in that range. If the |
| 6968 | * found idx is less than or equal to the end idx then we know that |
| 6969 | * a page exists. If no pages are found or if those pages are |
| 6970 | * outside of the range then we're fine (yay!) */ |
| 6971 | while (page == NULL && |
| 6972 | radix_tree_gang_lookup_slot(root, &pagep, NULL, start_idx, 1)) { |
| 6973 | page = radix_tree_deref_slot(pagep); |
| 6974 | if (unlikely(!page)) |
| 6975 | break; |
| 6976 | |
| 6977 | if (radix_tree_exception(page)) { |
Filipe Manana | 809f901 | 2014-06-05 13:22:25 +0100 | [diff] [blame] | 6978 | if (radix_tree_deref_retry(page)) { |
| 6979 | page = NULL; |
Alex Gartrell | fc4adbf | 2014-05-20 13:07:56 -0700 | [diff] [blame] | 6980 | continue; |
Filipe Manana | 809f901 | 2014-06-05 13:22:25 +0100 | [diff] [blame] | 6981 | } |
Alex Gartrell | fc4adbf | 2014-05-20 13:07:56 -0700 | [diff] [blame] | 6982 | /* |
| 6983 | * Otherwise, shmem/tmpfs must be storing a swap entry |
| 6984 | * here as an exceptional entry: so return it without |
| 6985 | * attempting to raise page count. |
| 6986 | */ |
Filipe Manana | 6fdef6d | 2014-06-05 13:22:26 +0100 | [diff] [blame] | 6987 | page = NULL; |
Alex Gartrell | fc4adbf | 2014-05-20 13:07:56 -0700 | [diff] [blame] | 6988 | break; /* TODO: Is this relevant for this use case? */ |
| 6989 | } |
| 6990 | |
Filipe Manana | 9140515 | 2014-06-05 13:22:24 +0100 | [diff] [blame] | 6991 | if (!page_cache_get_speculative(page)) { |
| 6992 | page = NULL; |
Alex Gartrell | fc4adbf | 2014-05-20 13:07:56 -0700 | [diff] [blame] | 6993 | continue; |
Filipe Manana | 9140515 | 2014-06-05 13:22:24 +0100 | [diff] [blame] | 6994 | } |
Alex Gartrell | fc4adbf | 2014-05-20 13:07:56 -0700 | [diff] [blame] | 6995 | |
| 6996 | /* |
| 6997 | * Has the page moved? |
| 6998 | * This is part of the lockless pagecache protocol. See |
| 6999 | * include/linux/pagemap.h for details. |
| 7000 | */ |
| 7001 | if (unlikely(page != *pagep)) { |
| 7002 | page_cache_release(page); |
| 7003 | page = NULL; |
| 7004 | } |
| 7005 | } |
| 7006 | |
| 7007 | if (page) { |
| 7008 | if (page->index <= end_idx) |
| 7009 | found = true; |
| 7010 | page_cache_release(page); |
| 7011 | } |
| 7012 | |
| 7013 | rcu_read_unlock(); |
| 7014 | return found; |
| 7015 | } |
| 7016 | |
Josef Bacik | eb838e7 | 2012-07-31 16:28:48 -0400 | [diff] [blame] | 7017 | static int lock_extent_direct(struct inode *inode, u64 lockstart, u64 lockend, |
| 7018 | struct extent_state **cached_state, int writing) |
| 7019 | { |
| 7020 | struct btrfs_ordered_extent *ordered; |
| 7021 | int ret = 0; |
| 7022 | |
| 7023 | while (1) { |
| 7024 | lock_extent_bits(&BTRFS_I(inode)->io_tree, lockstart, lockend, |
| 7025 | 0, cached_state); |
| 7026 | /* |
| 7027 | * We're concerned with the entire range that we're going to be |
| 7028 | * doing DIO to, so we need to make sure theres no ordered |
| 7029 | * extents in this range. |
| 7030 | */ |
| 7031 | ordered = btrfs_lookup_ordered_range(inode, lockstart, |
| 7032 | lockend - lockstart + 1); |
| 7033 | |
| 7034 | /* |
| 7035 | * We need to make sure there are no buffered pages in this |
| 7036 | * range either, we could have raced between the invalidate in |
| 7037 | * generic_file_direct_write and locking the extent. The |
| 7038 | * invalidate needs to happen so that reads after a write do not |
| 7039 | * get stale data. |
| 7040 | */ |
Alex Gartrell | fc4adbf | 2014-05-20 13:07:56 -0700 | [diff] [blame] | 7041 | if (!ordered && |
| 7042 | (!writing || |
| 7043 | !btrfs_page_exists_in_range(inode, lockstart, lockend))) |
Josef Bacik | eb838e7 | 2012-07-31 16:28:48 -0400 | [diff] [blame] | 7044 | break; |
| 7045 | |
| 7046 | unlock_extent_cached(&BTRFS_I(inode)->io_tree, lockstart, lockend, |
| 7047 | cached_state, GFP_NOFS); |
| 7048 | |
| 7049 | if (ordered) { |
| 7050 | btrfs_start_ordered_extent(inode, ordered, 1); |
| 7051 | btrfs_put_ordered_extent(ordered); |
| 7052 | } else { |
| 7053 | /* Screw you mmap */ |
Filipe Manana | 728404d | 2014-10-10 09:43:11 +0100 | [diff] [blame] | 7054 | ret = btrfs_fdatawrite_range(inode, lockstart, lockend); |
Filipe Manana | 075bdbd | 2014-10-09 21:18:55 +0100 | [diff] [blame] | 7055 | if (ret) |
| 7056 | break; |
| 7057 | ret = filemap_fdatawait_range(inode->i_mapping, |
| 7058 | lockstart, |
| 7059 | lockend); |
Josef Bacik | eb838e7 | 2012-07-31 16:28:48 -0400 | [diff] [blame] | 7060 | if (ret) |
| 7061 | break; |
| 7062 | |
| 7063 | /* |
| 7064 | * If we found a page that couldn't be invalidated just |
| 7065 | * fall back to buffered. |
| 7066 | */ |
| 7067 | ret = invalidate_inode_pages2_range(inode->i_mapping, |
| 7068 | lockstart >> PAGE_CACHE_SHIFT, |
| 7069 | lockend >> PAGE_CACHE_SHIFT); |
| 7070 | if (ret) |
| 7071 | break; |
| 7072 | } |
| 7073 | |
| 7074 | cond_resched(); |
| 7075 | } |
| 7076 | |
| 7077 | return ret; |
| 7078 | } |
| 7079 | |
Josef Bacik | 69ffb54 | 2012-09-11 15:40:07 -0400 | [diff] [blame] | 7080 | static struct extent_map *create_pinned_em(struct inode *inode, u64 start, |
| 7081 | u64 len, u64 orig_start, |
| 7082 | u64 block_start, u64 block_len, |
Josef Bacik | cc95bef | 2013-04-04 14:31:27 -0400 | [diff] [blame] | 7083 | u64 orig_block_len, u64 ram_bytes, |
| 7084 | int type) |
Josef Bacik | 69ffb54 | 2012-09-11 15:40:07 -0400 | [diff] [blame] | 7085 | { |
| 7086 | struct extent_map_tree *em_tree; |
| 7087 | struct extent_map *em; |
| 7088 | struct btrfs_root *root = BTRFS_I(inode)->root; |
| 7089 | int ret; |
| 7090 | |
| 7091 | em_tree = &BTRFS_I(inode)->extent_tree; |
| 7092 | em = alloc_extent_map(); |
| 7093 | if (!em) |
| 7094 | return ERR_PTR(-ENOMEM); |
| 7095 | |
| 7096 | em->start = start; |
| 7097 | em->orig_start = orig_start; |
Josef Bacik | 2ab28f3 | 2012-10-12 15:27:49 -0400 | [diff] [blame] | 7098 | em->mod_start = start; |
| 7099 | em->mod_len = len; |
Josef Bacik | 69ffb54 | 2012-09-11 15:40:07 -0400 | [diff] [blame] | 7100 | em->len = len; |
| 7101 | em->block_len = block_len; |
| 7102 | em->block_start = block_start; |
| 7103 | em->bdev = root->fs_info->fs_devices->latest_bdev; |
Josef Bacik | b493968 | 2012-12-03 10:31:19 -0500 | [diff] [blame] | 7104 | em->orig_block_len = orig_block_len; |
Josef Bacik | cc95bef | 2013-04-04 14:31:27 -0400 | [diff] [blame] | 7105 | em->ram_bytes = ram_bytes; |
Josef Bacik | 70c8a91 | 2012-10-11 16:54:30 -0400 | [diff] [blame] | 7106 | em->generation = -1; |
Josef Bacik | 69ffb54 | 2012-09-11 15:40:07 -0400 | [diff] [blame] | 7107 | set_bit(EXTENT_FLAG_PINNED, &em->flags); |
| 7108 | if (type == BTRFS_ORDERED_PREALLOC) |
Josef Bacik | b11e234 | 2012-12-03 10:58:15 -0500 | [diff] [blame] | 7109 | set_bit(EXTENT_FLAG_FILLING, &em->flags); |
Josef Bacik | 69ffb54 | 2012-09-11 15:40:07 -0400 | [diff] [blame] | 7110 | |
| 7111 | do { |
| 7112 | btrfs_drop_extent_cache(inode, em->start, |
| 7113 | em->start + em->len - 1, 0); |
| 7114 | write_lock(&em_tree->lock); |
Josef Bacik | 09a2a8f9 | 2013-04-05 16:51:15 -0400 | [diff] [blame] | 7115 | ret = add_extent_mapping(em_tree, em, 1); |
Josef Bacik | 69ffb54 | 2012-09-11 15:40:07 -0400 | [diff] [blame] | 7116 | write_unlock(&em_tree->lock); |
| 7117 | } while (ret == -EEXIST); |
| 7118 | |
| 7119 | if (ret) { |
| 7120 | free_extent_map(em); |
| 7121 | return ERR_PTR(ret); |
| 7122 | } |
| 7123 | |
| 7124 | return em; |
| 7125 | } |
| 7126 | |
| 7127 | |
Josef Bacik | 4b46fce | 2010-05-23 11:00:55 -0400 | [diff] [blame] | 7128 | static int btrfs_get_blocks_direct(struct inode *inode, sector_t iblock, |
| 7129 | struct buffer_head *bh_result, int create) |
| 7130 | { |
| 7131 | struct extent_map *em; |
| 7132 | struct btrfs_root *root = BTRFS_I(inode)->root; |
Josef Bacik | eb838e7 | 2012-07-31 16:28:48 -0400 | [diff] [blame] | 7133 | struct extent_state *cached_state = NULL; |
Josef Bacik | 4b46fce | 2010-05-23 11:00:55 -0400 | [diff] [blame] | 7134 | u64 start = iblock << inode->i_blkbits; |
Josef Bacik | eb838e7 | 2012-07-31 16:28:48 -0400 | [diff] [blame] | 7135 | u64 lockstart, lockend; |
Josef Bacik | 4b46fce | 2010-05-23 11:00:55 -0400 | [diff] [blame] | 7136 | u64 len = bh_result->b_size; |
Josef Bacik | eb838e7 | 2012-07-31 16:28:48 -0400 | [diff] [blame] | 7137 | int unlock_bits = EXTENT_LOCKED; |
Miao Xie | 0934856 | 2013-02-07 10:12:07 +0000 | [diff] [blame] | 7138 | int ret = 0; |
Josef Bacik | eb838e7 | 2012-07-31 16:28:48 -0400 | [diff] [blame] | 7139 | |
Miao Xie | 172a504 | 2013-02-21 02:48:22 -0700 | [diff] [blame] | 7140 | if (create) |
Josef Bacik | eb838e7 | 2012-07-31 16:28:48 -0400 | [diff] [blame] | 7141 | unlock_bits |= EXTENT_DELALLOC | EXTENT_DIRTY; |
Miao Xie | 172a504 | 2013-02-21 02:48:22 -0700 | [diff] [blame] | 7142 | else |
Josef Bacik | c329861 | 2012-08-03 16:49:19 -0400 | [diff] [blame] | 7143 | len = min_t(u64, len, root->sectorsize); |
Josef Bacik | eb838e7 | 2012-07-31 16:28:48 -0400 | [diff] [blame] | 7144 | |
Josef Bacik | c329861 | 2012-08-03 16:49:19 -0400 | [diff] [blame] | 7145 | lockstart = start; |
| 7146 | lockend = start + len - 1; |
| 7147 | |
Josef Bacik | eb838e7 | 2012-07-31 16:28:48 -0400 | [diff] [blame] | 7148 | /* |
| 7149 | * If this errors out it's because we couldn't invalidate pagecache for |
| 7150 | * this range and we need to fallback to buffered. |
| 7151 | */ |
| 7152 | if (lock_extent_direct(inode, lockstart, lockend, &cached_state, create)) |
| 7153 | return -ENOTBLK; |
| 7154 | |
Josef Bacik | 4b46fce | 2010-05-23 11:00:55 -0400 | [diff] [blame] | 7155 | em = btrfs_get_extent(inode, NULL, 0, start, len, 0); |
Josef Bacik | eb838e7 | 2012-07-31 16:28:48 -0400 | [diff] [blame] | 7156 | if (IS_ERR(em)) { |
| 7157 | ret = PTR_ERR(em); |
| 7158 | goto unlock_err; |
| 7159 | } |
Josef Bacik | 4b46fce | 2010-05-23 11:00:55 -0400 | [diff] [blame] | 7160 | |
| 7161 | /* |
| 7162 | * Ok for INLINE and COMPRESSED extents we need to fallback on buffered |
| 7163 | * io. INLINE is special, and we could probably kludge it in here, but |
| 7164 | * it's still buffered so for safety lets just fall back to the generic |
| 7165 | * buffered path. |
| 7166 | * |
| 7167 | * For COMPRESSED we _have_ to read the entire extent in so we can |
| 7168 | * decompress it, so there will be buffering required no matter what we |
| 7169 | * do, so go ahead and fallback to buffered. |
| 7170 | * |
| 7171 | * We return -ENOTBLK because thats what makes DIO go ahead and go back |
| 7172 | * to buffered IO. Don't blame me, this is the price we pay for using |
| 7173 | * the generic code. |
| 7174 | */ |
| 7175 | if (test_bit(EXTENT_FLAG_COMPRESSED, &em->flags) || |
| 7176 | em->block_start == EXTENT_MAP_INLINE) { |
| 7177 | free_extent_map(em); |
Josef Bacik | eb838e7 | 2012-07-31 16:28:48 -0400 | [diff] [blame] | 7178 | ret = -ENOTBLK; |
| 7179 | goto unlock_err; |
Josef Bacik | 4b46fce | 2010-05-23 11:00:55 -0400 | [diff] [blame] | 7180 | } |
| 7181 | |
| 7182 | /* Just a good old fashioned hole, return */ |
| 7183 | if (!create && (em->block_start == EXTENT_MAP_HOLE || |
| 7184 | test_bit(EXTENT_FLAG_PREALLOC, &em->flags))) { |
| 7185 | free_extent_map(em); |
Josef Bacik | eb838e7 | 2012-07-31 16:28:48 -0400 | [diff] [blame] | 7186 | goto unlock_err; |
Josef Bacik | 4b46fce | 2010-05-23 11:00:55 -0400 | [diff] [blame] | 7187 | } |
| 7188 | |
| 7189 | /* |
| 7190 | * We don't allocate a new extent in the following cases |
| 7191 | * |
| 7192 | * 1) The inode is marked as NODATACOW. In this case we'll just use the |
| 7193 | * existing extent. |
| 7194 | * 2) The extent is marked as PREALLOC. We're good to go here and can |
| 7195 | * just use the extent. |
| 7196 | * |
| 7197 | */ |
Chris Mason | 46bfbb5 | 2010-05-26 11:04:10 -0400 | [diff] [blame] | 7198 | if (!create) { |
Josef Bacik | eb838e7 | 2012-07-31 16:28:48 -0400 | [diff] [blame] | 7199 | len = min(len, em->len - (start - em->start)); |
| 7200 | lockstart = start + len; |
| 7201 | goto unlock; |
Chris Mason | 46bfbb5 | 2010-05-26 11:04:10 -0400 | [diff] [blame] | 7202 | } |
Josef Bacik | 4b46fce | 2010-05-23 11:00:55 -0400 | [diff] [blame] | 7203 | |
| 7204 | if (test_bit(EXTENT_FLAG_PREALLOC, &em->flags) || |
| 7205 | ((BTRFS_I(inode)->flags & BTRFS_INODE_NODATACOW) && |
| 7206 | em->block_start != EXTENT_MAP_HOLE)) { |
Josef Bacik | 4b46fce | 2010-05-23 11:00:55 -0400 | [diff] [blame] | 7207 | int type; |
| 7208 | int ret; |
Josef Bacik | eb384b5 | 2013-04-24 16:32:55 -0400 | [diff] [blame] | 7209 | u64 block_start, orig_start, orig_block_len, ram_bytes; |
Josef Bacik | 4b46fce | 2010-05-23 11:00:55 -0400 | [diff] [blame] | 7210 | |
| 7211 | if (test_bit(EXTENT_FLAG_PREALLOC, &em->flags)) |
| 7212 | type = BTRFS_ORDERED_PREALLOC; |
| 7213 | else |
| 7214 | type = BTRFS_ORDERED_NOCOW; |
Chris Mason | 46bfbb5 | 2010-05-26 11:04:10 -0400 | [diff] [blame] | 7215 | len = min(len, em->len - (start - em->start)); |
Josef Bacik | 4b46fce | 2010-05-23 11:00:55 -0400 | [diff] [blame] | 7216 | block_start = em->block_start + (start - em->start); |
Chris Mason | 46bfbb5 | 2010-05-26 11:04:10 -0400 | [diff] [blame] | 7217 | |
Josef Bacik | 0036158 | 2013-08-14 14:02:47 -0400 | [diff] [blame] | 7218 | if (can_nocow_extent(inode, start, &len, &orig_start, |
Josef Bacik | 7ee9e44 | 2013-06-21 16:37:03 -0400 | [diff] [blame] | 7219 | &orig_block_len, &ram_bytes) == 1) { |
Josef Bacik | 69ffb54 | 2012-09-11 15:40:07 -0400 | [diff] [blame] | 7220 | if (type == BTRFS_ORDERED_PREALLOC) { |
| 7221 | free_extent_map(em); |
| 7222 | em = create_pinned_em(inode, start, len, |
| 7223 | orig_start, |
Josef Bacik | b493968 | 2012-12-03 10:31:19 -0500 | [diff] [blame] | 7224 | block_start, len, |
Josef Bacik | cc95bef | 2013-04-04 14:31:27 -0400 | [diff] [blame] | 7225 | orig_block_len, |
| 7226 | ram_bytes, type); |
Filipe Manana | 555e128 | 2014-07-07 12:35:21 +0100 | [diff] [blame] | 7227 | if (IS_ERR(em)) { |
| 7228 | ret = PTR_ERR(em); |
Josef Bacik | 69ffb54 | 2012-09-11 15:40:07 -0400 | [diff] [blame] | 7229 | goto unlock_err; |
Filipe Manana | 555e128 | 2014-07-07 12:35:21 +0100 | [diff] [blame] | 7230 | } |
Josef Bacik | 69ffb54 | 2012-09-11 15:40:07 -0400 | [diff] [blame] | 7231 | } |
| 7232 | |
Chris Mason | 46bfbb5 | 2010-05-26 11:04:10 -0400 | [diff] [blame] | 7233 | ret = btrfs_add_ordered_extent_dio(inode, start, |
| 7234 | block_start, len, len, type); |
Chris Mason | 46bfbb5 | 2010-05-26 11:04:10 -0400 | [diff] [blame] | 7235 | if (ret) { |
| 7236 | free_extent_map(em); |
Josef Bacik | eb838e7 | 2012-07-31 16:28:48 -0400 | [diff] [blame] | 7237 | goto unlock_err; |
Chris Mason | 46bfbb5 | 2010-05-26 11:04:10 -0400 | [diff] [blame] | 7238 | } |
| 7239 | goto unlock; |
Josef Bacik | 4b46fce | 2010-05-23 11:00:55 -0400 | [diff] [blame] | 7240 | } |
Josef Bacik | 4b46fce | 2010-05-23 11:00:55 -0400 | [diff] [blame] | 7241 | } |
Josef Bacik | 0036158 | 2013-08-14 14:02:47 -0400 | [diff] [blame] | 7242 | |
Chris Mason | 46bfbb5 | 2010-05-26 11:04:10 -0400 | [diff] [blame] | 7243 | /* |
| 7244 | * this will cow the extent, reset the len in case we changed |
| 7245 | * it above |
| 7246 | */ |
| 7247 | len = bh_result->b_size; |
Josef Bacik | 70c8a91 | 2012-10-11 16:54:30 -0400 | [diff] [blame] | 7248 | free_extent_map(em); |
| 7249 | em = btrfs_new_extent_direct(inode, start, len); |
Josef Bacik | eb838e7 | 2012-07-31 16:28:48 -0400 | [diff] [blame] | 7250 | if (IS_ERR(em)) { |
| 7251 | ret = PTR_ERR(em); |
| 7252 | goto unlock_err; |
| 7253 | } |
Chris Mason | 46bfbb5 | 2010-05-26 11:04:10 -0400 | [diff] [blame] | 7254 | len = min(len, em->len - (start - em->start)); |
| 7255 | unlock: |
Josef Bacik | 4b46fce | 2010-05-23 11:00:55 -0400 | [diff] [blame] | 7256 | bh_result->b_blocknr = (em->block_start + (start - em->start)) >> |
| 7257 | inode->i_blkbits; |
Chris Mason | 46bfbb5 | 2010-05-26 11:04:10 -0400 | [diff] [blame] | 7258 | bh_result->b_size = len; |
Josef Bacik | 4b46fce | 2010-05-23 11:00:55 -0400 | [diff] [blame] | 7259 | bh_result->b_bdev = em->bdev; |
| 7260 | set_buffer_mapped(bh_result); |
Josef Bacik | c3473e8 | 2012-06-19 10:59:00 -0400 | [diff] [blame] | 7261 | if (create) { |
| 7262 | if (!test_bit(EXTENT_FLAG_PREALLOC, &em->flags)) |
| 7263 | set_buffer_new(bh_result); |
| 7264 | |
| 7265 | /* |
| 7266 | * Need to update the i_size under the extent lock so buffered |
| 7267 | * readers will get the updated i_size when we unlock. |
| 7268 | */ |
| 7269 | if (start + len > i_size_read(inode)) |
| 7270 | i_size_write(inode, start + len); |
Miao Xie | 0934856 | 2013-02-07 10:12:07 +0000 | [diff] [blame] | 7271 | |
Miao Xie | 172a504 | 2013-02-21 02:48:22 -0700 | [diff] [blame] | 7272 | spin_lock(&BTRFS_I(inode)->lock); |
| 7273 | BTRFS_I(inode)->outstanding_extents++; |
| 7274 | spin_unlock(&BTRFS_I(inode)->lock); |
| 7275 | |
Miao Xie | 0934856 | 2013-02-07 10:12:07 +0000 | [diff] [blame] | 7276 | ret = set_extent_bit(&BTRFS_I(inode)->io_tree, lockstart, |
| 7277 | lockstart + len - 1, EXTENT_DELALLOC, NULL, |
| 7278 | &cached_state, GFP_NOFS); |
| 7279 | BUG_ON(ret); |
Josef Bacik | c3473e8 | 2012-06-19 10:59:00 -0400 | [diff] [blame] | 7280 | } |
Josef Bacik | 4b46fce | 2010-05-23 11:00:55 -0400 | [diff] [blame] | 7281 | |
Josef Bacik | eb838e7 | 2012-07-31 16:28:48 -0400 | [diff] [blame] | 7282 | /* |
| 7283 | * In the case of write we need to clear and unlock the entire range, |
| 7284 | * in the case of read we need to unlock only the end area that we |
| 7285 | * aren't using if there is any left over space. |
| 7286 | */ |
Liu Bo | 24c03fa | 2012-08-22 20:10:38 -0600 | [diff] [blame] | 7287 | if (lockstart < lockend) { |
Miao Xie | 0934856 | 2013-02-07 10:12:07 +0000 | [diff] [blame] | 7288 | clear_extent_bit(&BTRFS_I(inode)->io_tree, lockstart, |
| 7289 | lockend, unlock_bits, 1, 0, |
| 7290 | &cached_state, GFP_NOFS); |
Liu Bo | 24c03fa | 2012-08-22 20:10:38 -0600 | [diff] [blame] | 7291 | } else { |
Josef Bacik | eb838e7 | 2012-07-31 16:28:48 -0400 | [diff] [blame] | 7292 | free_extent_state(cached_state); |
Liu Bo | 24c03fa | 2012-08-22 20:10:38 -0600 | [diff] [blame] | 7293 | } |
Josef Bacik | eb838e7 | 2012-07-31 16:28:48 -0400 | [diff] [blame] | 7294 | |
Josef Bacik | 4b46fce | 2010-05-23 11:00:55 -0400 | [diff] [blame] | 7295 | free_extent_map(em); |
| 7296 | |
| 7297 | return 0; |
Josef Bacik | eb838e7 | 2012-07-31 16:28:48 -0400 | [diff] [blame] | 7298 | |
| 7299 | unlock_err: |
Josef Bacik | eb838e7 | 2012-07-31 16:28:48 -0400 | [diff] [blame] | 7300 | clear_extent_bit(&BTRFS_I(inode)->io_tree, lockstart, lockend, |
| 7301 | unlock_bits, 1, 0, &cached_state, GFP_NOFS); |
| 7302 | return ret; |
Josef Bacik | 4b46fce | 2010-05-23 11:00:55 -0400 | [diff] [blame] | 7303 | } |
| 7304 | |
Miao Xie | 8b110e3 | 2014-09-12 18:44:03 +0800 | [diff] [blame] | 7305 | static inline int submit_dio_repair_bio(struct inode *inode, struct bio *bio, |
| 7306 | int rw, int mirror_num) |
| 7307 | { |
| 7308 | struct btrfs_root *root = BTRFS_I(inode)->root; |
| 7309 | int ret; |
| 7310 | |
| 7311 | BUG_ON(rw & REQ_WRITE); |
| 7312 | |
| 7313 | bio_get(bio); |
| 7314 | |
| 7315 | ret = btrfs_bio_wq_end_io(root->fs_info, bio, |
| 7316 | BTRFS_WQ_ENDIO_DIO_REPAIR); |
| 7317 | if (ret) |
| 7318 | goto err; |
| 7319 | |
| 7320 | ret = btrfs_map_bio(root, rw, bio, mirror_num, 0); |
| 7321 | err: |
| 7322 | bio_put(bio); |
| 7323 | return ret; |
| 7324 | } |
| 7325 | |
| 7326 | static int btrfs_check_dio_repairable(struct inode *inode, |
| 7327 | struct bio *failed_bio, |
| 7328 | struct io_failure_record *failrec, |
| 7329 | int failed_mirror) |
| 7330 | { |
| 7331 | int num_copies; |
| 7332 | |
| 7333 | num_copies = btrfs_num_copies(BTRFS_I(inode)->root->fs_info, |
| 7334 | failrec->logical, failrec->len); |
| 7335 | if (num_copies == 1) { |
| 7336 | /* |
| 7337 | * we only have a single copy of the data, so don't bother with |
| 7338 | * all the retry and error correction code that follows. no |
| 7339 | * matter what the error is, it is very likely to persist. |
| 7340 | */ |
| 7341 | pr_debug("Check DIO Repairable: cannot repair, num_copies=%d, next_mirror %d, failed_mirror %d\n", |
| 7342 | num_copies, failrec->this_mirror, failed_mirror); |
| 7343 | return 0; |
| 7344 | } |
| 7345 | |
| 7346 | failrec->failed_mirror = failed_mirror; |
| 7347 | failrec->this_mirror++; |
| 7348 | if (failrec->this_mirror == failed_mirror) |
| 7349 | failrec->this_mirror++; |
| 7350 | |
| 7351 | if (failrec->this_mirror > num_copies) { |
| 7352 | pr_debug("Check DIO Repairable: (fail) num_copies=%d, next_mirror %d, failed_mirror %d\n", |
| 7353 | num_copies, failrec->this_mirror, failed_mirror); |
| 7354 | return 0; |
| 7355 | } |
| 7356 | |
| 7357 | return 1; |
| 7358 | } |
| 7359 | |
| 7360 | static int dio_read_error(struct inode *inode, struct bio *failed_bio, |
| 7361 | struct page *page, u64 start, u64 end, |
| 7362 | int failed_mirror, bio_end_io_t *repair_endio, |
| 7363 | void *repair_arg) |
| 7364 | { |
| 7365 | struct io_failure_record *failrec; |
| 7366 | struct bio *bio; |
| 7367 | int isector; |
| 7368 | int read_mode; |
| 7369 | int ret; |
| 7370 | |
| 7371 | BUG_ON(failed_bio->bi_rw & REQ_WRITE); |
| 7372 | |
| 7373 | ret = btrfs_get_io_failure_record(inode, start, end, &failrec); |
| 7374 | if (ret) |
| 7375 | return ret; |
| 7376 | |
| 7377 | ret = btrfs_check_dio_repairable(inode, failed_bio, failrec, |
| 7378 | failed_mirror); |
| 7379 | if (!ret) { |
| 7380 | free_io_failure(inode, failrec); |
| 7381 | return -EIO; |
| 7382 | } |
| 7383 | |
| 7384 | if (failed_bio->bi_vcnt > 1) |
| 7385 | read_mode = READ_SYNC | REQ_FAILFAST_DEV; |
| 7386 | else |
| 7387 | read_mode = READ_SYNC; |
| 7388 | |
| 7389 | isector = start - btrfs_io_bio(failed_bio)->logical; |
| 7390 | isector >>= inode->i_sb->s_blocksize_bits; |
| 7391 | bio = btrfs_create_repair_bio(inode, failed_bio, failrec, page, |
| 7392 | 0, isector, repair_endio, repair_arg); |
| 7393 | if (!bio) { |
| 7394 | free_io_failure(inode, failrec); |
| 7395 | return -EIO; |
| 7396 | } |
| 7397 | |
| 7398 | btrfs_debug(BTRFS_I(inode)->root->fs_info, |
| 7399 | "Repair DIO Read Error: submitting new dio read[%#x] to this_mirror=%d, in_validation=%d\n", |
| 7400 | read_mode, failrec->this_mirror, failrec->in_validation); |
| 7401 | |
| 7402 | ret = submit_dio_repair_bio(inode, bio, read_mode, |
| 7403 | failrec->this_mirror); |
| 7404 | if (ret) { |
| 7405 | free_io_failure(inode, failrec); |
| 7406 | bio_put(bio); |
| 7407 | } |
| 7408 | |
| 7409 | return ret; |
| 7410 | } |
| 7411 | |
| 7412 | struct btrfs_retry_complete { |
| 7413 | struct completion done; |
| 7414 | struct inode *inode; |
| 7415 | u64 start; |
| 7416 | int uptodate; |
| 7417 | }; |
| 7418 | |
| 7419 | static void btrfs_retry_endio_nocsum(struct bio *bio, int err) |
| 7420 | { |
| 7421 | struct btrfs_retry_complete *done = bio->bi_private; |
| 7422 | struct bio_vec *bvec; |
| 7423 | int i; |
| 7424 | |
| 7425 | if (err) |
| 7426 | goto end; |
| 7427 | |
| 7428 | done->uptodate = 1; |
| 7429 | bio_for_each_segment_all(bvec, bio, i) |
| 7430 | clean_io_failure(done->inode, done->start, bvec->bv_page, 0); |
| 7431 | end: |
| 7432 | complete(&done->done); |
| 7433 | bio_put(bio); |
| 7434 | } |
| 7435 | |
| 7436 | static int __btrfs_correct_data_nocsum(struct inode *inode, |
| 7437 | struct btrfs_io_bio *io_bio) |
Josef Bacik | 4b46fce | 2010-05-23 11:00:55 -0400 | [diff] [blame] | 7438 | { |
Kent Overstreet | 2c30c71 | 2013-11-07 12:20:26 -0800 | [diff] [blame] | 7439 | struct bio_vec *bvec; |
Miao Xie | 8b110e3 | 2014-09-12 18:44:03 +0800 | [diff] [blame] | 7440 | struct btrfs_retry_complete done; |
Josef Bacik | 4b46fce | 2010-05-23 11:00:55 -0400 | [diff] [blame] | 7441 | u64 start; |
Kent Overstreet | 2c30c71 | 2013-11-07 12:20:26 -0800 | [diff] [blame] | 7442 | int i; |
Miao Xie | c1dc089 | 2014-09-12 18:43:56 +0800 | [diff] [blame] | 7443 | int ret; |
Miao Xie | dc380ae | 2014-09-12 18:43:55 +0800 | [diff] [blame] | 7444 | |
Miao Xie | c1dc089 | 2014-09-12 18:43:56 +0800 | [diff] [blame] | 7445 | start = io_bio->logical; |
Miao Xie | 8b110e3 | 2014-09-12 18:44:03 +0800 | [diff] [blame] | 7446 | done.inode = inode; |
| 7447 | |
| 7448 | bio_for_each_segment_all(bvec, &io_bio->bio, i) { |
| 7449 | try_again: |
| 7450 | done.uptodate = 0; |
| 7451 | done.start = start; |
| 7452 | init_completion(&done.done); |
| 7453 | |
| 7454 | ret = dio_read_error(inode, &io_bio->bio, bvec->bv_page, start, |
| 7455 | start + bvec->bv_len - 1, |
| 7456 | io_bio->mirror_num, |
| 7457 | btrfs_retry_endio_nocsum, &done); |
| 7458 | if (ret) |
| 7459 | return ret; |
| 7460 | |
| 7461 | wait_for_completion(&done.done); |
| 7462 | |
| 7463 | if (!done.uptodate) { |
| 7464 | /* We might have another mirror, so try again */ |
| 7465 | goto try_again; |
| 7466 | } |
| 7467 | |
| 7468 | start += bvec->bv_len; |
| 7469 | } |
| 7470 | |
| 7471 | return 0; |
| 7472 | } |
| 7473 | |
| 7474 | static void btrfs_retry_endio(struct bio *bio, int err) |
| 7475 | { |
| 7476 | struct btrfs_retry_complete *done = bio->bi_private; |
| 7477 | struct btrfs_io_bio *io_bio = btrfs_io_bio(bio); |
| 7478 | struct bio_vec *bvec; |
| 7479 | int uptodate; |
| 7480 | int ret; |
| 7481 | int i; |
| 7482 | |
| 7483 | if (err) |
| 7484 | goto end; |
| 7485 | |
| 7486 | uptodate = 1; |
| 7487 | bio_for_each_segment_all(bvec, bio, i) { |
| 7488 | ret = __readpage_endio_check(done->inode, io_bio, i, |
| 7489 | bvec->bv_page, 0, |
| 7490 | done->start, bvec->bv_len); |
| 7491 | if (!ret) |
| 7492 | clean_io_failure(done->inode, done->start, |
| 7493 | bvec->bv_page, 0); |
| 7494 | else |
| 7495 | uptodate = 0; |
| 7496 | } |
| 7497 | |
| 7498 | done->uptodate = uptodate; |
| 7499 | end: |
| 7500 | complete(&done->done); |
| 7501 | bio_put(bio); |
| 7502 | } |
| 7503 | |
| 7504 | static int __btrfs_subio_endio_read(struct inode *inode, |
| 7505 | struct btrfs_io_bio *io_bio, int err) |
| 7506 | { |
| 7507 | struct bio_vec *bvec; |
| 7508 | struct btrfs_retry_complete done; |
| 7509 | u64 start; |
| 7510 | u64 offset = 0; |
| 7511 | int i; |
| 7512 | int ret; |
| 7513 | |
| 7514 | err = 0; |
| 7515 | start = io_bio->logical; |
| 7516 | done.inode = inode; |
| 7517 | |
Miao Xie | c1dc089 | 2014-09-12 18:43:56 +0800 | [diff] [blame] | 7518 | bio_for_each_segment_all(bvec, &io_bio->bio, i) { |
Miao Xie | dc380ae | 2014-09-12 18:43:55 +0800 | [diff] [blame] | 7519 | ret = __readpage_endio_check(inode, io_bio, i, bvec->bv_page, |
| 7520 | 0, start, bvec->bv_len); |
Miao Xie | 8b110e3 | 2014-09-12 18:44:03 +0800 | [diff] [blame] | 7521 | if (likely(!ret)) |
| 7522 | goto next; |
| 7523 | try_again: |
| 7524 | done.uptodate = 0; |
| 7525 | done.start = start; |
| 7526 | init_completion(&done.done); |
| 7527 | |
| 7528 | ret = dio_read_error(inode, &io_bio->bio, bvec->bv_page, start, |
| 7529 | start + bvec->bv_len - 1, |
| 7530 | io_bio->mirror_num, |
| 7531 | btrfs_retry_endio, &done); |
| 7532 | if (ret) { |
| 7533 | err = ret; |
| 7534 | goto next; |
| 7535 | } |
| 7536 | |
| 7537 | wait_for_completion(&done.done); |
| 7538 | |
| 7539 | if (!done.uptodate) { |
| 7540 | /* We might have another mirror, so try again */ |
| 7541 | goto try_again; |
| 7542 | } |
| 7543 | next: |
| 7544 | offset += bvec->bv_len; |
Josef Bacik | 4b46fce | 2010-05-23 11:00:55 -0400 | [diff] [blame] | 7545 | start += bvec->bv_len; |
Kent Overstreet | 2c30c71 | 2013-11-07 12:20:26 -0800 | [diff] [blame] | 7546 | } |
Miao Xie | c1dc089 | 2014-09-12 18:43:56 +0800 | [diff] [blame] | 7547 | |
| 7548 | return err; |
| 7549 | } |
| 7550 | |
Miao Xie | 8b110e3 | 2014-09-12 18:44:03 +0800 | [diff] [blame] | 7551 | static int btrfs_subio_endio_read(struct inode *inode, |
| 7552 | struct btrfs_io_bio *io_bio, int err) |
| 7553 | { |
| 7554 | bool skip_csum = BTRFS_I(inode)->flags & BTRFS_INODE_NODATASUM; |
| 7555 | |
| 7556 | if (skip_csum) { |
| 7557 | if (unlikely(err)) |
| 7558 | return __btrfs_correct_data_nocsum(inode, io_bio); |
| 7559 | else |
| 7560 | return 0; |
| 7561 | } else { |
| 7562 | return __btrfs_subio_endio_read(inode, io_bio, err); |
| 7563 | } |
| 7564 | } |
| 7565 | |
Miao Xie | c1dc089 | 2014-09-12 18:43:56 +0800 | [diff] [blame] | 7566 | static void btrfs_endio_direct_read(struct bio *bio, int err) |
| 7567 | { |
| 7568 | struct btrfs_dio_private *dip = bio->bi_private; |
| 7569 | struct inode *inode = dip->inode; |
| 7570 | struct bio *dio_bio; |
| 7571 | struct btrfs_io_bio *io_bio = btrfs_io_bio(bio); |
| 7572 | |
Miao Xie | 8b110e3 | 2014-09-12 18:44:03 +0800 | [diff] [blame] | 7573 | if (dip->flags & BTRFS_DIO_ORIG_BIO_SUBMITTED) |
| 7574 | err = btrfs_subio_endio_read(inode, io_bio, err); |
Miao Xie | c1dc089 | 2014-09-12 18:43:56 +0800 | [diff] [blame] | 7575 | |
Josef Bacik | 4b46fce | 2010-05-23 11:00:55 -0400 | [diff] [blame] | 7576 | unlock_extent(&BTRFS_I(inode)->io_tree, dip->logical_offset, |
Jeff Mahoney | d008237 | 2012-03-01 14:57:19 +0100 | [diff] [blame] | 7577 | dip->logical_offset + dip->bytes - 1); |
Chris Mason | 9be3395 | 2013-05-17 18:30:14 -0400 | [diff] [blame] | 7578 | dio_bio = dip->dio_bio; |
Josef Bacik | 4b46fce | 2010-05-23 11:00:55 -0400 | [diff] [blame] | 7579 | |
Josef Bacik | 4b46fce | 2010-05-23 11:00:55 -0400 | [diff] [blame] | 7580 | kfree(dip); |
Josef Bacik | c0da7aa | 2011-03-22 11:05:07 -0400 | [diff] [blame] | 7581 | |
| 7582 | /* If we had a csum failure make sure to clear the uptodate flag */ |
| 7583 | if (err) |
Chris Mason | 9be3395 | 2013-05-17 18:30:14 -0400 | [diff] [blame] | 7584 | clear_bit(BIO_UPTODATE, &dio_bio->bi_flags); |
| 7585 | dio_end_io(dio_bio, err); |
Miao Xie | 23ea8e5 | 2014-09-12 18:43:54 +0800 | [diff] [blame] | 7586 | |
| 7587 | if (io_bio->end_io) |
| 7588 | io_bio->end_io(io_bio, err); |
Chris Mason | 9be3395 | 2013-05-17 18:30:14 -0400 | [diff] [blame] | 7589 | bio_put(bio); |
Josef Bacik | 4b46fce | 2010-05-23 11:00:55 -0400 | [diff] [blame] | 7590 | } |
| 7591 | |
| 7592 | static void btrfs_endio_direct_write(struct bio *bio, int err) |
| 7593 | { |
| 7594 | struct btrfs_dio_private *dip = bio->bi_private; |
| 7595 | struct inode *inode = dip->inode; |
| 7596 | struct btrfs_root *root = BTRFS_I(inode)->root; |
Josef Bacik | 4b46fce | 2010-05-23 11:00:55 -0400 | [diff] [blame] | 7597 | struct btrfs_ordered_extent *ordered = NULL; |
Chris Mason | 163cf09 | 2010-11-28 19:56:33 -0500 | [diff] [blame] | 7598 | u64 ordered_offset = dip->logical_offset; |
| 7599 | u64 ordered_bytes = dip->bytes; |
Chris Mason | 9be3395 | 2013-05-17 18:30:14 -0400 | [diff] [blame] | 7600 | struct bio *dio_bio; |
Josef Bacik | 4b46fce | 2010-05-23 11:00:55 -0400 | [diff] [blame] | 7601 | int ret; |
| 7602 | |
| 7603 | if (err) |
| 7604 | goto out_done; |
Chris Mason | 163cf09 | 2010-11-28 19:56:33 -0500 | [diff] [blame] | 7605 | again: |
| 7606 | ret = btrfs_dec_test_first_ordered_pending(inode, &ordered, |
| 7607 | &ordered_offset, |
Josef Bacik | 5fd0204 | 2012-05-02 14:00:54 -0400 | [diff] [blame] | 7608 | ordered_bytes, !err); |
Josef Bacik | 4b46fce | 2010-05-23 11:00:55 -0400 | [diff] [blame] | 7609 | if (!ret) |
Chris Mason | 163cf09 | 2010-11-28 19:56:33 -0500 | [diff] [blame] | 7610 | goto out_test; |
Josef Bacik | 4b46fce | 2010-05-23 11:00:55 -0400 | [diff] [blame] | 7611 | |
Liu Bo | 9e0af23 | 2014-08-15 23:36:53 +0800 | [diff] [blame] | 7612 | btrfs_init_work(&ordered->work, btrfs_endio_write_helper, |
| 7613 | finish_ordered_fn, NULL, NULL); |
Qu Wenruo | fccb5d8 | 2014-02-28 10:46:10 +0800 | [diff] [blame] | 7614 | btrfs_queue_work(root->fs_info->endio_write_workers, |
| 7615 | &ordered->work); |
Chris Mason | 163cf09 | 2010-11-28 19:56:33 -0500 | [diff] [blame] | 7616 | out_test: |
| 7617 | /* |
| 7618 | * our bio might span multiple ordered extents. If we haven't |
| 7619 | * completed the accounting for the whole dio, go back and try again |
| 7620 | */ |
| 7621 | if (ordered_offset < dip->logical_offset + dip->bytes) { |
| 7622 | ordered_bytes = dip->logical_offset + dip->bytes - |
| 7623 | ordered_offset; |
Josef Bacik | 5fd0204 | 2012-05-02 14:00:54 -0400 | [diff] [blame] | 7624 | ordered = NULL; |
Chris Mason | 163cf09 | 2010-11-28 19:56:33 -0500 | [diff] [blame] | 7625 | goto again; |
| 7626 | } |
Josef Bacik | 4b46fce | 2010-05-23 11:00:55 -0400 | [diff] [blame] | 7627 | out_done: |
Chris Mason | 9be3395 | 2013-05-17 18:30:14 -0400 | [diff] [blame] | 7628 | dio_bio = dip->dio_bio; |
Josef Bacik | 4b46fce | 2010-05-23 11:00:55 -0400 | [diff] [blame] | 7629 | |
Josef Bacik | 4b46fce | 2010-05-23 11:00:55 -0400 | [diff] [blame] | 7630 | kfree(dip); |
Josef Bacik | c0da7aa | 2011-03-22 11:05:07 -0400 | [diff] [blame] | 7631 | |
| 7632 | /* If we had an error make sure to clear the uptodate flag */ |
| 7633 | if (err) |
Chris Mason | 9be3395 | 2013-05-17 18:30:14 -0400 | [diff] [blame] | 7634 | clear_bit(BIO_UPTODATE, &dio_bio->bi_flags); |
| 7635 | dio_end_io(dio_bio, err); |
| 7636 | bio_put(bio); |
Josef Bacik | 4b46fce | 2010-05-23 11:00:55 -0400 | [diff] [blame] | 7637 | } |
| 7638 | |
Chris Mason | eaf25d9 | 2010-05-25 09:48:28 -0400 | [diff] [blame] | 7639 | static int __btrfs_submit_bio_start_direct_io(struct inode *inode, int rw, |
| 7640 | struct bio *bio, int mirror_num, |
| 7641 | unsigned long bio_flags, u64 offset) |
| 7642 | { |
| 7643 | int ret; |
| 7644 | struct btrfs_root *root = BTRFS_I(inode)->root; |
| 7645 | ret = btrfs_csum_one_bio(root, inode, bio, offset, 1); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 7646 | BUG_ON(ret); /* -ENOMEM */ |
Chris Mason | eaf25d9 | 2010-05-25 09:48:28 -0400 | [diff] [blame] | 7647 | return 0; |
| 7648 | } |
| 7649 | |
Miao Xie | e65e153 | 2010-11-22 03:04:43 +0000 | [diff] [blame] | 7650 | static void btrfs_end_dio_bio(struct bio *bio, int err) |
| 7651 | { |
| 7652 | struct btrfs_dio_private *dip = bio->bi_private; |
| 7653 | |
Miao Xie | 8b110e3 | 2014-09-12 18:44:03 +0800 | [diff] [blame] | 7654 | if (err) |
| 7655 | btrfs_warn(BTRFS_I(dip->inode)->root->fs_info, |
| 7656 | "direct IO failed ino %llu rw %lu sector %#Lx len %u err no %d", |
| 7657 | btrfs_ino(dip->inode), bio->bi_rw, |
| 7658 | (unsigned long long)bio->bi_iter.bi_sector, |
| 7659 | bio->bi_iter.bi_size, err); |
| 7660 | |
| 7661 | if (dip->subio_endio) |
| 7662 | err = dip->subio_endio(dip->inode, btrfs_io_bio(bio), err); |
Miao Xie | c1dc089 | 2014-09-12 18:43:56 +0800 | [diff] [blame] | 7663 | |
| 7664 | if (err) { |
Miao Xie | e65e153 | 2010-11-22 03:04:43 +0000 | [diff] [blame] | 7665 | dip->errors = 1; |
| 7666 | |
| 7667 | /* |
| 7668 | * before atomic variable goto zero, we must make sure |
| 7669 | * dip->errors is perceived to be set. |
| 7670 | */ |
Peter Zijlstra | 4e857c5 | 2014-03-17 18:06:10 +0100 | [diff] [blame] | 7671 | smp_mb__before_atomic(); |
Miao Xie | e65e153 | 2010-11-22 03:04:43 +0000 | [diff] [blame] | 7672 | } |
| 7673 | |
| 7674 | /* if there are more bios still pending for this dio, just exit */ |
| 7675 | if (!atomic_dec_and_test(&dip->pending_bios)) |
| 7676 | goto out; |
| 7677 | |
Chris Mason | 9be3395 | 2013-05-17 18:30:14 -0400 | [diff] [blame] | 7678 | if (dip->errors) { |
Miao Xie | e65e153 | 2010-11-22 03:04:43 +0000 | [diff] [blame] | 7679 | bio_io_error(dip->orig_bio); |
Chris Mason | 9be3395 | 2013-05-17 18:30:14 -0400 | [diff] [blame] | 7680 | } else { |
| 7681 | set_bit(BIO_UPTODATE, &dip->dio_bio->bi_flags); |
Miao Xie | e65e153 | 2010-11-22 03:04:43 +0000 | [diff] [blame] | 7682 | bio_endio(dip->orig_bio, 0); |
| 7683 | } |
| 7684 | out: |
| 7685 | bio_put(bio); |
| 7686 | } |
| 7687 | |
| 7688 | static struct bio *btrfs_dio_bio_alloc(struct block_device *bdev, |
| 7689 | u64 first_sector, gfp_t gfp_flags) |
| 7690 | { |
| 7691 | int nr_vecs = bio_get_nr_vecs(bdev); |
| 7692 | return btrfs_bio_alloc(bdev, first_sector, nr_vecs, gfp_flags); |
| 7693 | } |
| 7694 | |
Miao Xie | c1dc089 | 2014-09-12 18:43:56 +0800 | [diff] [blame] | 7695 | static inline int btrfs_lookup_and_bind_dio_csum(struct btrfs_root *root, |
| 7696 | struct inode *inode, |
| 7697 | struct btrfs_dio_private *dip, |
| 7698 | struct bio *bio, |
| 7699 | u64 file_offset) |
| 7700 | { |
| 7701 | struct btrfs_io_bio *io_bio = btrfs_io_bio(bio); |
| 7702 | struct btrfs_io_bio *orig_io_bio = btrfs_io_bio(dip->orig_bio); |
| 7703 | int ret; |
| 7704 | |
| 7705 | /* |
| 7706 | * We load all the csum data we need when we submit |
| 7707 | * the first bio to reduce the csum tree search and |
| 7708 | * contention. |
| 7709 | */ |
| 7710 | if (dip->logical_offset == file_offset) { |
| 7711 | ret = btrfs_lookup_bio_sums_dio(root, inode, dip->orig_bio, |
| 7712 | file_offset); |
| 7713 | if (ret) |
| 7714 | return ret; |
| 7715 | } |
| 7716 | |
| 7717 | if (bio == dip->orig_bio) |
| 7718 | return 0; |
| 7719 | |
| 7720 | file_offset -= dip->logical_offset; |
| 7721 | file_offset >>= inode->i_sb->s_blocksize_bits; |
| 7722 | io_bio->csum = (u8 *)(((u32 *)orig_io_bio->csum) + file_offset); |
| 7723 | |
| 7724 | return 0; |
| 7725 | } |
| 7726 | |
Miao Xie | e65e153 | 2010-11-22 03:04:43 +0000 | [diff] [blame] | 7727 | static inline int __btrfs_submit_dio_bio(struct bio *bio, struct inode *inode, |
| 7728 | int rw, u64 file_offset, int skip_sum, |
Josef Bacik | c329861 | 2012-08-03 16:49:19 -0400 | [diff] [blame] | 7729 | int async_submit) |
Miao Xie | e65e153 | 2010-11-22 03:04:43 +0000 | [diff] [blame] | 7730 | { |
Miao Xie | facc8a22 | 2013-07-25 19:22:34 +0800 | [diff] [blame] | 7731 | struct btrfs_dio_private *dip = bio->bi_private; |
Miao Xie | e65e153 | 2010-11-22 03:04:43 +0000 | [diff] [blame] | 7732 | int write = rw & REQ_WRITE; |
| 7733 | struct btrfs_root *root = BTRFS_I(inode)->root; |
| 7734 | int ret; |
| 7735 | |
Josef Bacik | b812ce2 | 2012-11-16 13:56:32 -0500 | [diff] [blame] | 7736 | if (async_submit) |
| 7737 | async_submit = !atomic_read(&BTRFS_I(inode)->sync_writers); |
| 7738 | |
Miao Xie | e65e153 | 2010-11-22 03:04:43 +0000 | [diff] [blame] | 7739 | bio_get(bio); |
Josef Bacik | 5fd0204 | 2012-05-02 14:00:54 -0400 | [diff] [blame] | 7740 | |
| 7741 | if (!write) { |
David Sterba | bfebd8b | 2014-07-30 00:25:45 +0200 | [diff] [blame] | 7742 | ret = btrfs_bio_wq_end_io(root->fs_info, bio, |
| 7743 | BTRFS_WQ_ENDIO_DATA); |
Josef Bacik | 5fd0204 | 2012-05-02 14:00:54 -0400 | [diff] [blame] | 7744 | if (ret) |
| 7745 | goto err; |
| 7746 | } |
Miao Xie | e65e153 | 2010-11-22 03:04:43 +0000 | [diff] [blame] | 7747 | |
Josef Bacik | 1ae3993 | 2011-04-06 14:41:34 -0400 | [diff] [blame] | 7748 | if (skip_sum) |
| 7749 | goto map; |
| 7750 | |
| 7751 | if (write && async_submit) { |
Miao Xie | e65e153 | 2010-11-22 03:04:43 +0000 | [diff] [blame] | 7752 | ret = btrfs_wq_submit_bio(root->fs_info, |
| 7753 | inode, rw, bio, 0, 0, |
| 7754 | file_offset, |
| 7755 | __btrfs_submit_bio_start_direct_io, |
| 7756 | __btrfs_submit_bio_done); |
| 7757 | goto err; |
Josef Bacik | 1ae3993 | 2011-04-06 14:41:34 -0400 | [diff] [blame] | 7758 | } else if (write) { |
| 7759 | /* |
| 7760 | * If we aren't doing async submit, calculate the csum of the |
| 7761 | * bio now. |
| 7762 | */ |
| 7763 | ret = btrfs_csum_one_bio(root, inode, bio, file_offset, 1); |
| 7764 | if (ret) |
| 7765 | goto err; |
Miao Xie | 23ea8e5 | 2014-09-12 18:43:54 +0800 | [diff] [blame] | 7766 | } else { |
Miao Xie | c1dc089 | 2014-09-12 18:43:56 +0800 | [diff] [blame] | 7767 | ret = btrfs_lookup_and_bind_dio_csum(root, inode, dip, bio, |
| 7768 | file_offset); |
Tsutomu Itoh | c2db107 | 2011-03-01 06:48:31 +0000 | [diff] [blame] | 7769 | if (ret) |
| 7770 | goto err; |
| 7771 | } |
Josef Bacik | 1ae3993 | 2011-04-06 14:41:34 -0400 | [diff] [blame] | 7772 | map: |
| 7773 | ret = btrfs_map_bio(root, rw, bio, 0, async_submit); |
Miao Xie | e65e153 | 2010-11-22 03:04:43 +0000 | [diff] [blame] | 7774 | err: |
| 7775 | bio_put(bio); |
| 7776 | return ret; |
| 7777 | } |
| 7778 | |
| 7779 | static int btrfs_submit_direct_hook(int rw, struct btrfs_dio_private *dip, |
| 7780 | int skip_sum) |
| 7781 | { |
| 7782 | struct inode *inode = dip->inode; |
| 7783 | struct btrfs_root *root = BTRFS_I(inode)->root; |
Miao Xie | e65e153 | 2010-11-22 03:04:43 +0000 | [diff] [blame] | 7784 | struct bio *bio; |
| 7785 | struct bio *orig_bio = dip->orig_bio; |
| 7786 | struct bio_vec *bvec = orig_bio->bi_io_vec; |
Kent Overstreet | 4f024f3 | 2013-10-11 15:44:27 -0700 | [diff] [blame] | 7787 | u64 start_sector = orig_bio->bi_iter.bi_sector; |
Miao Xie | e65e153 | 2010-11-22 03:04:43 +0000 | [diff] [blame] | 7788 | u64 file_offset = dip->logical_offset; |
| 7789 | u64 submit_len = 0; |
| 7790 | u64 map_length; |
| 7791 | int nr_pages = 0; |
Miao Xie | 23ea8e5 | 2014-09-12 18:43:54 +0800 | [diff] [blame] | 7792 | int ret; |
Josef Bacik | 1ae3993 | 2011-04-06 14:41:34 -0400 | [diff] [blame] | 7793 | int async_submit = 0; |
Miao Xie | e65e153 | 2010-11-22 03:04:43 +0000 | [diff] [blame] | 7794 | |
Kent Overstreet | 4f024f3 | 2013-10-11 15:44:27 -0700 | [diff] [blame] | 7795 | map_length = orig_bio->bi_iter.bi_size; |
David Woodhouse | 53b381b | 2013-01-29 18:40:14 -0500 | [diff] [blame] | 7796 | ret = btrfs_map_block(root->fs_info, rw, start_sector << 9, |
Miao Xie | e65e153 | 2010-11-22 03:04:43 +0000 | [diff] [blame] | 7797 | &map_length, NULL, 0); |
Miao Xie | 7a5c3c9 | 2014-06-17 18:58:59 +0800 | [diff] [blame] | 7798 | if (ret) |
Miao Xie | e65e153 | 2010-11-22 03:04:43 +0000 | [diff] [blame] | 7799 | return -EIO; |
Miao Xie | facc8a22 | 2013-07-25 19:22:34 +0800 | [diff] [blame] | 7800 | |
Kent Overstreet | 4f024f3 | 2013-10-11 15:44:27 -0700 | [diff] [blame] | 7801 | if (map_length >= orig_bio->bi_iter.bi_size) { |
Josef Bacik | 02f57c7 | 2011-04-06 14:25:44 -0400 | [diff] [blame] | 7802 | bio = orig_bio; |
Miao Xie | c1dc089 | 2014-09-12 18:43:56 +0800 | [diff] [blame] | 7803 | dip->flags |= BTRFS_DIO_ORIG_BIO_SUBMITTED; |
Josef Bacik | 02f57c7 | 2011-04-06 14:25:44 -0400 | [diff] [blame] | 7804 | goto submit; |
| 7805 | } |
| 7806 | |
David Woodhouse | 53b381b | 2013-01-29 18:40:14 -0500 | [diff] [blame] | 7807 | /* async crcs make it difficult to collect full stripe writes. */ |
| 7808 | if (btrfs_get_alloc_profile(root, 1) & |
| 7809 | (BTRFS_BLOCK_GROUP_RAID5 | BTRFS_BLOCK_GROUP_RAID6)) |
| 7810 | async_submit = 0; |
| 7811 | else |
| 7812 | async_submit = 1; |
| 7813 | |
Josef Bacik | 02f57c7 | 2011-04-06 14:25:44 -0400 | [diff] [blame] | 7814 | bio = btrfs_dio_bio_alloc(orig_bio->bi_bdev, start_sector, GFP_NOFS); |
| 7815 | if (!bio) |
| 7816 | return -ENOMEM; |
Miao Xie | 7a5c3c9 | 2014-06-17 18:58:59 +0800 | [diff] [blame] | 7817 | |
Josef Bacik | 02f57c7 | 2011-04-06 14:25:44 -0400 | [diff] [blame] | 7818 | bio->bi_private = dip; |
| 7819 | bio->bi_end_io = btrfs_end_dio_bio; |
Miao Xie | c1dc089 | 2014-09-12 18:43:56 +0800 | [diff] [blame] | 7820 | btrfs_io_bio(bio)->logical = file_offset; |
Josef Bacik | 02f57c7 | 2011-04-06 14:25:44 -0400 | [diff] [blame] | 7821 | atomic_inc(&dip->pending_bios); |
| 7822 | |
Miao Xie | e65e153 | 2010-11-22 03:04:43 +0000 | [diff] [blame] | 7823 | while (bvec <= (orig_bio->bi_io_vec + orig_bio->bi_vcnt - 1)) { |
David Sterba | ee39b43 | 2014-09-30 01:33:33 +0200 | [diff] [blame] | 7824 | if (map_length < submit_len + bvec->bv_len || |
Miao Xie | e65e153 | 2010-11-22 03:04:43 +0000 | [diff] [blame] | 7825 | bio_add_page(bio, bvec->bv_page, bvec->bv_len, |
David Sterba | ee39b43 | 2014-09-30 01:33:33 +0200 | [diff] [blame] | 7826 | bvec->bv_offset) < bvec->bv_len) { |
Miao Xie | e65e153 | 2010-11-22 03:04:43 +0000 | [diff] [blame] | 7827 | /* |
| 7828 | * inc the count before we submit the bio so |
| 7829 | * we know the end IO handler won't happen before |
| 7830 | * we inc the count. Otherwise, the dip might get freed |
| 7831 | * before we're done setting it up |
| 7832 | */ |
| 7833 | atomic_inc(&dip->pending_bios); |
| 7834 | ret = __btrfs_submit_dio_bio(bio, inode, rw, |
| 7835 | file_offset, skip_sum, |
Josef Bacik | c329861 | 2012-08-03 16:49:19 -0400 | [diff] [blame] | 7836 | async_submit); |
Miao Xie | e65e153 | 2010-11-22 03:04:43 +0000 | [diff] [blame] | 7837 | if (ret) { |
| 7838 | bio_put(bio); |
| 7839 | atomic_dec(&dip->pending_bios); |
| 7840 | goto out_err; |
| 7841 | } |
| 7842 | |
Miao Xie | e65e153 | 2010-11-22 03:04:43 +0000 | [diff] [blame] | 7843 | start_sector += submit_len >> 9; |
| 7844 | file_offset += submit_len; |
| 7845 | |
| 7846 | submit_len = 0; |
| 7847 | nr_pages = 0; |
| 7848 | |
| 7849 | bio = btrfs_dio_bio_alloc(orig_bio->bi_bdev, |
| 7850 | start_sector, GFP_NOFS); |
| 7851 | if (!bio) |
| 7852 | goto out_err; |
| 7853 | bio->bi_private = dip; |
| 7854 | bio->bi_end_io = btrfs_end_dio_bio; |
Miao Xie | c1dc089 | 2014-09-12 18:43:56 +0800 | [diff] [blame] | 7855 | btrfs_io_bio(bio)->logical = file_offset; |
Miao Xie | e65e153 | 2010-11-22 03:04:43 +0000 | [diff] [blame] | 7856 | |
Kent Overstreet | 4f024f3 | 2013-10-11 15:44:27 -0700 | [diff] [blame] | 7857 | map_length = orig_bio->bi_iter.bi_size; |
David Woodhouse | 53b381b | 2013-01-29 18:40:14 -0500 | [diff] [blame] | 7858 | ret = btrfs_map_block(root->fs_info, rw, |
Stefan Behrens | 3ec706c | 2012-11-05 15:46:42 +0100 | [diff] [blame] | 7859 | start_sector << 9, |
Miao Xie | e65e153 | 2010-11-22 03:04:43 +0000 | [diff] [blame] | 7860 | &map_length, NULL, 0); |
| 7861 | if (ret) { |
| 7862 | bio_put(bio); |
| 7863 | goto out_err; |
| 7864 | } |
| 7865 | } else { |
| 7866 | submit_len += bvec->bv_len; |
Dulshani Gunawardhana | 6787125 | 2013-10-31 10:33:04 +0530 | [diff] [blame] | 7867 | nr_pages++; |
Miao Xie | e65e153 | 2010-11-22 03:04:43 +0000 | [diff] [blame] | 7868 | bvec++; |
| 7869 | } |
| 7870 | } |
| 7871 | |
Josef Bacik | 02f57c7 | 2011-04-06 14:25:44 -0400 | [diff] [blame] | 7872 | submit: |
Miao Xie | e65e153 | 2010-11-22 03:04:43 +0000 | [diff] [blame] | 7873 | ret = __btrfs_submit_dio_bio(bio, inode, rw, file_offset, skip_sum, |
Josef Bacik | c329861 | 2012-08-03 16:49:19 -0400 | [diff] [blame] | 7874 | async_submit); |
Miao Xie | e65e153 | 2010-11-22 03:04:43 +0000 | [diff] [blame] | 7875 | if (!ret) |
| 7876 | return 0; |
| 7877 | |
| 7878 | bio_put(bio); |
| 7879 | out_err: |
| 7880 | dip->errors = 1; |
| 7881 | /* |
| 7882 | * before atomic variable goto zero, we must |
| 7883 | * make sure dip->errors is perceived to be set. |
| 7884 | */ |
Peter Zijlstra | 4e857c5 | 2014-03-17 18:06:10 +0100 | [diff] [blame] | 7885 | smp_mb__before_atomic(); |
Miao Xie | e65e153 | 2010-11-22 03:04:43 +0000 | [diff] [blame] | 7886 | if (atomic_dec_and_test(&dip->pending_bios)) |
| 7887 | bio_io_error(dip->orig_bio); |
| 7888 | |
| 7889 | /* bio_end_io() will handle error, so we needn't return it */ |
| 7890 | return 0; |
| 7891 | } |
| 7892 | |
Chris Mason | 9be3395 | 2013-05-17 18:30:14 -0400 | [diff] [blame] | 7893 | static void btrfs_submit_direct(int rw, struct bio *dio_bio, |
| 7894 | struct inode *inode, loff_t file_offset) |
Josef Bacik | 4b46fce | 2010-05-23 11:00:55 -0400 | [diff] [blame] | 7895 | { |
| 7896 | struct btrfs_root *root = BTRFS_I(inode)->root; |
| 7897 | struct btrfs_dio_private *dip; |
Chris Mason | 9be3395 | 2013-05-17 18:30:14 -0400 | [diff] [blame] | 7898 | struct bio *io_bio; |
Miao Xie | 23ea8e5 | 2014-09-12 18:43:54 +0800 | [diff] [blame] | 7899 | struct btrfs_io_bio *btrfs_bio; |
Josef Bacik | 4b46fce | 2010-05-23 11:00:55 -0400 | [diff] [blame] | 7900 | int skip_sum; |
Christoph Hellwig | 7b6d91d | 2010-08-07 18:20:39 +0200 | [diff] [blame] | 7901 | int write = rw & REQ_WRITE; |
Josef Bacik | 4b46fce | 2010-05-23 11:00:55 -0400 | [diff] [blame] | 7902 | int ret = 0; |
| 7903 | |
| 7904 | skip_sum = BTRFS_I(inode)->flags & BTRFS_INODE_NODATASUM; |
| 7905 | |
Chris Mason | 9be3395 | 2013-05-17 18:30:14 -0400 | [diff] [blame] | 7906 | io_bio = btrfs_bio_clone(dio_bio, GFP_NOFS); |
Chris Mason | 9be3395 | 2013-05-17 18:30:14 -0400 | [diff] [blame] | 7907 | if (!io_bio) { |
Josef Bacik | 4b46fce | 2010-05-23 11:00:55 -0400 | [diff] [blame] | 7908 | ret = -ENOMEM; |
| 7909 | goto free_ordered; |
| 7910 | } |
Josef Bacik | 4b46fce | 2010-05-23 11:00:55 -0400 | [diff] [blame] | 7911 | |
Miao Xie | c1dc089 | 2014-09-12 18:43:56 +0800 | [diff] [blame] | 7912 | dip = kzalloc(sizeof(*dip), GFP_NOFS); |
Chris Mason | 9be3395 | 2013-05-17 18:30:14 -0400 | [diff] [blame] | 7913 | if (!dip) { |
| 7914 | ret = -ENOMEM; |
| 7915 | goto free_io_bio; |
| 7916 | } |
| 7917 | |
| 7918 | dip->private = dio_bio->bi_private; |
Josef Bacik | 4b46fce | 2010-05-23 11:00:55 -0400 | [diff] [blame] | 7919 | dip->inode = inode; |
| 7920 | dip->logical_offset = file_offset; |
Kent Overstreet | 4f024f3 | 2013-10-11 15:44:27 -0700 | [diff] [blame] | 7921 | dip->bytes = dio_bio->bi_iter.bi_size; |
| 7922 | dip->disk_bytenr = (u64)dio_bio->bi_iter.bi_sector << 9; |
Chris Mason | 9be3395 | 2013-05-17 18:30:14 -0400 | [diff] [blame] | 7923 | io_bio->bi_private = dip; |
Chris Mason | 9be3395 | 2013-05-17 18:30:14 -0400 | [diff] [blame] | 7924 | dip->orig_bio = io_bio; |
| 7925 | dip->dio_bio = dio_bio; |
Miao Xie | e65e153 | 2010-11-22 03:04:43 +0000 | [diff] [blame] | 7926 | atomic_set(&dip->pending_bios, 0); |
Miao Xie | c1dc089 | 2014-09-12 18:43:56 +0800 | [diff] [blame] | 7927 | btrfs_bio = btrfs_io_bio(io_bio); |
| 7928 | btrfs_bio->logical = file_offset; |
Josef Bacik | 4b46fce | 2010-05-23 11:00:55 -0400 | [diff] [blame] | 7929 | |
Miao Xie | c1dc089 | 2014-09-12 18:43:56 +0800 | [diff] [blame] | 7930 | if (write) { |
Chris Mason | 9be3395 | 2013-05-17 18:30:14 -0400 | [diff] [blame] | 7931 | io_bio->bi_end_io = btrfs_endio_direct_write; |
Miao Xie | c1dc089 | 2014-09-12 18:43:56 +0800 | [diff] [blame] | 7932 | } else { |
Chris Mason | 9be3395 | 2013-05-17 18:30:14 -0400 | [diff] [blame] | 7933 | io_bio->bi_end_io = btrfs_endio_direct_read; |
Miao Xie | c1dc089 | 2014-09-12 18:43:56 +0800 | [diff] [blame] | 7934 | dip->subio_endio = btrfs_subio_endio_read; |
| 7935 | } |
Josef Bacik | 4b46fce | 2010-05-23 11:00:55 -0400 | [diff] [blame] | 7936 | |
Miao Xie | e65e153 | 2010-11-22 03:04:43 +0000 | [diff] [blame] | 7937 | ret = btrfs_submit_direct_hook(rw, dip, skip_sum); |
| 7938 | if (!ret) |
Chris Mason | eaf25d9 | 2010-05-25 09:48:28 -0400 | [diff] [blame] | 7939 | return; |
Chris Mason | 9be3395 | 2013-05-17 18:30:14 -0400 | [diff] [blame] | 7940 | |
Miao Xie | 23ea8e5 | 2014-09-12 18:43:54 +0800 | [diff] [blame] | 7941 | if (btrfs_bio->end_io) |
| 7942 | btrfs_bio->end_io(btrfs_bio, ret); |
Chris Mason | 9be3395 | 2013-05-17 18:30:14 -0400 | [diff] [blame] | 7943 | free_io_bio: |
| 7944 | bio_put(io_bio); |
| 7945 | |
Josef Bacik | 4b46fce | 2010-05-23 11:00:55 -0400 | [diff] [blame] | 7946 | free_ordered: |
| 7947 | /* |
| 7948 | * If this is a write, we need to clean up the reserved space and kill |
| 7949 | * the ordered extent. |
| 7950 | */ |
| 7951 | if (write) { |
| 7952 | struct btrfs_ordered_extent *ordered; |
Josef Bacik | 955256f | 2010-11-19 09:41:10 -0500 | [diff] [blame] | 7953 | ordered = btrfs_lookup_ordered_extent(inode, file_offset); |
Josef Bacik | 4b46fce | 2010-05-23 11:00:55 -0400 | [diff] [blame] | 7954 | if (!test_bit(BTRFS_ORDERED_PREALLOC, &ordered->flags) && |
| 7955 | !test_bit(BTRFS_ORDERED_NOCOW, &ordered->flags)) |
| 7956 | btrfs_free_reserved_extent(root, ordered->start, |
Miao Xie | e570fd2 | 2014-06-19 10:42:50 +0800 | [diff] [blame] | 7957 | ordered->disk_len, 1); |
Josef Bacik | 4b46fce | 2010-05-23 11:00:55 -0400 | [diff] [blame] | 7958 | btrfs_put_ordered_extent(ordered); |
| 7959 | btrfs_put_ordered_extent(ordered); |
| 7960 | } |
Chris Mason | 9be3395 | 2013-05-17 18:30:14 -0400 | [diff] [blame] | 7961 | bio_endio(dio_bio, ret); |
Josef Bacik | 4b46fce | 2010-05-23 11:00:55 -0400 | [diff] [blame] | 7962 | } |
| 7963 | |
Chris Mason | 5a5f79b | 2010-05-26 21:33:37 -0400 | [diff] [blame] | 7964 | 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] | 7965 | const struct iov_iter *iter, loff_t offset) |
Chris Mason | 5a5f79b | 2010-05-26 21:33:37 -0400 | [diff] [blame] | 7966 | { |
| 7967 | int seg; |
Josef Bacik | a1b75f7 | 2011-04-08 15:51:18 +0000 | [diff] [blame] | 7968 | int i; |
Chris Mason | 5a5f79b | 2010-05-26 21:33:37 -0400 | [diff] [blame] | 7969 | unsigned blocksize_mask = root->sectorsize - 1; |
| 7970 | ssize_t retval = -EINVAL; |
Chris Mason | 5a5f79b | 2010-05-26 21:33:37 -0400 | [diff] [blame] | 7971 | |
| 7972 | if (offset & blocksize_mask) |
| 7973 | goto out; |
| 7974 | |
Al Viro | 28060d5 | 2014-03-22 05:15:17 -0400 | [diff] [blame] | 7975 | if (iov_iter_alignment(iter) & blocksize_mask) |
| 7976 | goto out; |
Josef Bacik | a1b75f7 | 2011-04-08 15:51:18 +0000 | [diff] [blame] | 7977 | |
Al Viro | 28060d5 | 2014-03-22 05:15:17 -0400 | [diff] [blame] | 7978 | /* If this is a write we don't need to check anymore */ |
| 7979 | if (rw & WRITE) |
| 7980 | return 0; |
| 7981 | /* |
| 7982 | * Check to make sure we don't have duplicate iov_base's in this |
| 7983 | * iovec, if so return EINVAL, otherwise we'll get csum errors |
| 7984 | * when reading back. |
| 7985 | */ |
| 7986 | for (seg = 0; seg < iter->nr_segs; seg++) { |
| 7987 | for (i = seg + 1; i < iter->nr_segs; i++) { |
| 7988 | if (iter->iov[seg].iov_base == iter->iov[i].iov_base) |
Josef Bacik | a1b75f7 | 2011-04-08 15:51:18 +0000 | [diff] [blame] | 7989 | goto out; |
| 7990 | } |
Chris Mason | 5a5f79b | 2010-05-26 21:33:37 -0400 | [diff] [blame] | 7991 | } |
| 7992 | retval = 0; |
| 7993 | out: |
| 7994 | return retval; |
| 7995 | } |
Josef Bacik | eb838e7 | 2012-07-31 16:28:48 -0400 | [diff] [blame] | 7996 | |
Chris Mason | 1643298 | 2008-04-10 10:23:21 -0400 | [diff] [blame] | 7997 | static ssize_t btrfs_direct_IO(int rw, struct kiocb *iocb, |
Al Viro | d8d3d94 | 2014-03-04 21:27:34 -0500 | [diff] [blame] | 7998 | struct iov_iter *iter, loff_t offset) |
Chris Mason | 1643298 | 2008-04-10 10:23:21 -0400 | [diff] [blame] | 7999 | { |
Josef Bacik | 4b46fce | 2010-05-23 11:00:55 -0400 | [diff] [blame] | 8000 | struct file *file = iocb->ki_filp; |
| 8001 | struct inode *inode = file->f_mapping->host; |
Miao Xie | 0934856 | 2013-02-07 10:12:07 +0000 | [diff] [blame] | 8002 | size_t count = 0; |
Miao Xie | 2e60a51 | 2013-02-08 07:01:08 +0000 | [diff] [blame] | 8003 | int flags = 0; |
Miao Xie | 38851cc | 2013-02-08 07:04:11 +0000 | [diff] [blame] | 8004 | bool wakeup = true; |
| 8005 | bool relock = false; |
Miao Xie | 0934856 | 2013-02-07 10:12:07 +0000 | [diff] [blame] | 8006 | ssize_t ret; |
Josef Bacik | 4b46fce | 2010-05-23 11:00:55 -0400 | [diff] [blame] | 8007 | |
Al Viro | 28060d5 | 2014-03-22 05:15:17 -0400 | [diff] [blame] | 8008 | if (check_direct_IO(BTRFS_I(inode)->root, rw, iocb, iter, offset)) |
Chris Mason | 5a5f79b | 2010-05-26 21:33:37 -0400 | [diff] [blame] | 8009 | return 0; |
Chris Mason | 5a5f79b | 2010-05-26 21:33:37 -0400 | [diff] [blame] | 8010 | |
Miao Xie | 38851cc | 2013-02-08 07:04:11 +0000 | [diff] [blame] | 8011 | atomic_inc(&inode->i_dio_count); |
Peter Zijlstra | 4e857c5 | 2014-03-17 18:06:10 +0100 | [diff] [blame] | 8012 | smp_mb__after_atomic(); |
Miao Xie | 38851cc | 2013-02-08 07:04:11 +0000 | [diff] [blame] | 8013 | |
Josef Bacik | 0e267c4 | 2013-07-02 10:38:02 -0400 | [diff] [blame] | 8014 | /* |
Miao Xie | 41bd9ca | 2014-03-06 13:54:57 +0800 | [diff] [blame] | 8015 | * The generic stuff only does filemap_write_and_wait_range, which |
| 8016 | * isn't enough if we've written compressed pages to this area, so |
| 8017 | * we need to flush the dirty pages again to make absolutely sure |
| 8018 | * that any outstanding dirty pages are on disk. |
Josef Bacik | 0e267c4 | 2013-07-02 10:38:02 -0400 | [diff] [blame] | 8019 | */ |
Al Viro | a6cbcd4 | 2014-03-04 22:38:00 -0500 | [diff] [blame] | 8020 | count = iov_iter_count(iter); |
Miao Xie | 41bd9ca | 2014-03-06 13:54:57 +0800 | [diff] [blame] | 8021 | if (test_bit(BTRFS_INODE_HAS_ASYNC_EXTENT, |
| 8022 | &BTRFS_I(inode)->runtime_flags)) |
Wang Shilong | 9a025a0 | 2014-07-17 11:44:13 +0800 | [diff] [blame] | 8023 | filemap_fdatawrite_range(inode->i_mapping, offset, |
| 8024 | offset + count - 1); |
Josef Bacik | 0e267c4 | 2013-07-02 10:38:02 -0400 | [diff] [blame] | 8025 | |
Miao Xie | 0934856 | 2013-02-07 10:12:07 +0000 | [diff] [blame] | 8026 | if (rw & WRITE) { |
Miao Xie | 38851cc | 2013-02-08 07:04:11 +0000 | [diff] [blame] | 8027 | /* |
| 8028 | * If the write DIO is beyond the EOF, we need update |
| 8029 | * the isize, but it is protected by i_mutex. So we can |
| 8030 | * not unlock the i_mutex at this case. |
| 8031 | */ |
| 8032 | if (offset + count <= inode->i_size) { |
| 8033 | mutex_unlock(&inode->i_mutex); |
| 8034 | relock = true; |
| 8035 | } |
Miao Xie | 0934856 | 2013-02-07 10:12:07 +0000 | [diff] [blame] | 8036 | ret = btrfs_delalloc_reserve_space(inode, count); |
| 8037 | if (ret) |
Miao Xie | 38851cc | 2013-02-08 07:04:11 +0000 | [diff] [blame] | 8038 | goto out; |
David Sterba | ee39b43 | 2014-09-30 01:33:33 +0200 | [diff] [blame] | 8039 | } else if (test_bit(BTRFS_INODE_READDIO_NEED_LOCK, |
| 8040 | &BTRFS_I(inode)->runtime_flags)) { |
Miao Xie | 38851cc | 2013-02-08 07:04:11 +0000 | [diff] [blame] | 8041 | inode_dio_done(inode); |
| 8042 | flags = DIO_LOCKING | DIO_SKIP_HOLES; |
| 8043 | wakeup = false; |
Miao Xie | 0934856 | 2013-02-07 10:12:07 +0000 | [diff] [blame] | 8044 | } |
| 8045 | |
| 8046 | ret = __blockdev_direct_IO(rw, iocb, inode, |
| 8047 | BTRFS_I(inode)->root->fs_info->fs_devices->latest_bdev, |
Al Viro | 31b1403 | 2014-03-05 01:33:16 -0500 | [diff] [blame] | 8048 | iter, offset, btrfs_get_blocks_direct, NULL, |
Miao Xie | 2e60a51 | 2013-02-08 07:01:08 +0000 | [diff] [blame] | 8049 | btrfs_submit_direct, flags); |
Miao Xie | 0934856 | 2013-02-07 10:12:07 +0000 | [diff] [blame] | 8050 | if (rw & WRITE) { |
| 8051 | if (ret < 0 && ret != -EIOCBQUEUED) |
| 8052 | btrfs_delalloc_release_space(inode, count); |
Miao Xie | 172a504 | 2013-02-21 02:48:22 -0700 | [diff] [blame] | 8053 | else if (ret >= 0 && (size_t)ret < count) |
Miao Xie | 0934856 | 2013-02-07 10:12:07 +0000 | [diff] [blame] | 8054 | btrfs_delalloc_release_space(inode, |
| 8055 | count - (size_t)ret); |
Miao Xie | 172a504 | 2013-02-21 02:48:22 -0700 | [diff] [blame] | 8056 | else |
| 8057 | btrfs_delalloc_release_metadata(inode, 0); |
Miao Xie | 0934856 | 2013-02-07 10:12:07 +0000 | [diff] [blame] | 8058 | } |
Miao Xie | 38851cc | 2013-02-08 07:04:11 +0000 | [diff] [blame] | 8059 | out: |
Miao Xie | 2e60a51 | 2013-02-08 07:01:08 +0000 | [diff] [blame] | 8060 | if (wakeup) |
| 8061 | inode_dio_done(inode); |
Miao Xie | 38851cc | 2013-02-08 07:04:11 +0000 | [diff] [blame] | 8062 | if (relock) |
| 8063 | mutex_lock(&inode->i_mutex); |
Miao Xie | 0934856 | 2013-02-07 10:12:07 +0000 | [diff] [blame] | 8064 | |
| 8065 | return ret; |
Chris Mason | 1643298 | 2008-04-10 10:23:21 -0400 | [diff] [blame] | 8066 | } |
| 8067 | |
Tsutomu Itoh | 05dadc0 | 2012-11-29 05:08:26 +0000 | [diff] [blame] | 8068 | #define BTRFS_FIEMAP_FLAGS (FIEMAP_FLAG_SYNC) |
| 8069 | |
Yehuda Sadeh | 1506fcc | 2009-01-21 14:39:14 -0500 | [diff] [blame] | 8070 | static int btrfs_fiemap(struct inode *inode, struct fiemap_extent_info *fieinfo, |
| 8071 | __u64 start, __u64 len) |
| 8072 | { |
Tsutomu Itoh | 05dadc0 | 2012-11-29 05:08:26 +0000 | [diff] [blame] | 8073 | int ret; |
| 8074 | |
| 8075 | ret = fiemap_check_flags(fieinfo, BTRFS_FIEMAP_FLAGS); |
| 8076 | if (ret) |
| 8077 | return ret; |
| 8078 | |
Chris Mason | ec29ed5 | 2011-02-23 16:23:20 -0500 | [diff] [blame] | 8079 | return extent_fiemap(inode, fieinfo, start, len, btrfs_get_extent_fiemap); |
Yehuda Sadeh | 1506fcc | 2009-01-21 14:39:14 -0500 | [diff] [blame] | 8080 | } |
| 8081 | |
Chris Mason | 9ebefb18 | 2007-06-15 13:50:00 -0400 | [diff] [blame] | 8082 | int btrfs_readpage(struct file *file, struct page *page) |
| 8083 | { |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 8084 | struct extent_io_tree *tree; |
| 8085 | tree = &BTRFS_I(page->mapping->host)->io_tree; |
Jan Schmidt | 8ddc7d9 | 2011-06-13 20:02:58 +0200 | [diff] [blame] | 8086 | return extent_read_full_page(tree, page, btrfs_get_extent, 0); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 8087 | } |
Chris Mason | 1832a6d | 2007-12-21 16:27:21 -0500 | [diff] [blame] | 8088 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 8089 | static int btrfs_writepage(struct page *page, struct writeback_control *wbc) |
| 8090 | { |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 8091 | struct extent_io_tree *tree; |
Chris Mason | b888db2 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 8092 | |
| 8093 | |
| 8094 | if (current->flags & PF_MEMALLOC) { |
| 8095 | redirty_page_for_writepage(wbc, page); |
| 8096 | unlock_page(page); |
| 8097 | return 0; |
| 8098 | } |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 8099 | tree = &BTRFS_I(page->mapping->host)->io_tree; |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 8100 | return extent_write_full_page(tree, page, btrfs_get_extent, wbc); |
| 8101 | } |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 8102 | |
Eric Sandeen | 48a3b63 | 2013-04-25 20:41:01 +0000 | [diff] [blame] | 8103 | static int btrfs_writepages(struct address_space *mapping, |
| 8104 | struct writeback_control *wbc) |
Chris Mason | b293f02 | 2007-11-01 19:45:34 -0400 | [diff] [blame] | 8105 | { |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 8106 | struct extent_io_tree *tree; |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 8107 | |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 8108 | tree = &BTRFS_I(mapping->host)->io_tree; |
Chris Mason | b293f02 | 2007-11-01 19:45:34 -0400 | [diff] [blame] | 8109 | return extent_writepages(tree, mapping, btrfs_get_extent, wbc); |
| 8110 | } |
| 8111 | |
Chris Mason | 3ab2fb5 | 2007-11-08 10:59:22 -0500 | [diff] [blame] | 8112 | static int |
| 8113 | btrfs_readpages(struct file *file, struct address_space *mapping, |
| 8114 | struct list_head *pages, unsigned nr_pages) |
| 8115 | { |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 8116 | struct extent_io_tree *tree; |
| 8117 | tree = &BTRFS_I(mapping->host)->io_tree; |
Chris Mason | 3ab2fb5 | 2007-11-08 10:59:22 -0500 | [diff] [blame] | 8118 | return extent_readpages(tree, mapping, pages, nr_pages, |
| 8119 | btrfs_get_extent); |
| 8120 | } |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 8121 | static int __btrfs_releasepage(struct page *page, gfp_t gfp_flags) |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 8122 | { |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 8123 | struct extent_io_tree *tree; |
| 8124 | struct extent_map_tree *map; |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 8125 | int ret; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 8126 | |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 8127 | tree = &BTRFS_I(page->mapping->host)->io_tree; |
| 8128 | map = &BTRFS_I(page->mapping->host)->extent_tree; |
Chris Mason | 70dec80 | 2008-01-29 09:59:12 -0500 | [diff] [blame] | 8129 | ret = try_release_extent_mapping(map, tree, page, gfp_flags); |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 8130 | if (ret == 1) { |
| 8131 | ClearPagePrivate(page); |
| 8132 | set_page_private(page, 0); |
| 8133 | page_cache_release(page); |
| 8134 | } |
| 8135 | return ret; |
| 8136 | } |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 8137 | |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 8138 | static int btrfs_releasepage(struct page *page, gfp_t gfp_flags) |
| 8139 | { |
Chris Mason | 98509cf | 2008-09-11 15:51:43 -0400 | [diff] [blame] | 8140 | if (PageWriteback(page) || PageDirty(page)) |
| 8141 | return 0; |
Yan Zheng | b335b00 | 2009-02-12 10:06:04 -0500 | [diff] [blame] | 8142 | return __btrfs_releasepage(page, gfp_flags & GFP_NOFS); |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 8143 | } |
| 8144 | |
Lukas Czerner | d47992f | 2013-05-21 23:17:23 -0400 | [diff] [blame] | 8145 | static void btrfs_invalidatepage(struct page *page, unsigned int offset, |
| 8146 | unsigned int length) |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 8147 | { |
Josef Bacik | 5fd0204 | 2012-05-02 14:00:54 -0400 | [diff] [blame] | 8148 | struct inode *inode = page->mapping->host; |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 8149 | struct extent_io_tree *tree; |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 8150 | struct btrfs_ordered_extent *ordered; |
Josef Bacik | 2ac55d4 | 2010-02-03 19:33:23 +0000 | [diff] [blame] | 8151 | struct extent_state *cached_state = NULL; |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 8152 | u64 page_start = page_offset(page); |
| 8153 | u64 page_end = page_start + PAGE_CACHE_SIZE - 1; |
Filipe David Borba Manana | 131e404 | 2013-11-19 22:29:35 +0000 | [diff] [blame] | 8154 | int inode_evicting = inode->i_state & I_FREEING; |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 8155 | |
Chris Mason | 8b62b72 | 2009-09-02 16:53:46 -0400 | [diff] [blame] | 8156 | /* |
| 8157 | * we have the page locked, so new writeback can't start, |
| 8158 | * and the dirty bit won't be cleared while we are here. |
| 8159 | * |
| 8160 | * Wait for IO on this page so that we can safely clear |
| 8161 | * the PagePrivate2 bit and do ordered accounting |
| 8162 | */ |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 8163 | wait_on_page_writeback(page); |
Chris Mason | 8b62b72 | 2009-09-02 16:53:46 -0400 | [diff] [blame] | 8164 | |
Josef Bacik | 5fd0204 | 2012-05-02 14:00:54 -0400 | [diff] [blame] | 8165 | tree = &BTRFS_I(inode)->io_tree; |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 8166 | if (offset) { |
| 8167 | btrfs_releasepage(page, GFP_NOFS); |
| 8168 | return; |
| 8169 | } |
Filipe David Borba Manana | 131e404 | 2013-11-19 22:29:35 +0000 | [diff] [blame] | 8170 | |
| 8171 | if (!inode_evicting) |
| 8172 | lock_extent_bits(tree, page_start, page_end, 0, &cached_state); |
| 8173 | ordered = btrfs_lookup_ordered_extent(inode, page_start); |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 8174 | if (ordered) { |
Chris Mason | eb84ae0 | 2008-07-17 13:53:27 -0400 | [diff] [blame] | 8175 | /* |
| 8176 | * IO on this page will never be started, so we need |
| 8177 | * to account for any ordered extents now |
| 8178 | */ |
Filipe David Borba Manana | 131e404 | 2013-11-19 22:29:35 +0000 | [diff] [blame] | 8179 | if (!inode_evicting) |
| 8180 | clear_extent_bit(tree, page_start, page_end, |
| 8181 | EXTENT_DIRTY | EXTENT_DELALLOC | |
| 8182 | EXTENT_LOCKED | EXTENT_DO_ACCOUNTING | |
| 8183 | EXTENT_DEFRAG, 1, 0, &cached_state, |
| 8184 | GFP_NOFS); |
Chris Mason | 8b62b72 | 2009-09-02 16:53:46 -0400 | [diff] [blame] | 8185 | /* |
| 8186 | * whoever cleared the private bit is responsible |
| 8187 | * for the finish_ordered_io |
| 8188 | */ |
Josef Bacik | 77cef2e | 2013-08-29 13:57:21 -0400 | [diff] [blame] | 8189 | if (TestClearPagePrivate2(page)) { |
| 8190 | struct btrfs_ordered_inode_tree *tree; |
| 8191 | u64 new_len; |
| 8192 | |
| 8193 | tree = &BTRFS_I(inode)->ordered_tree; |
| 8194 | |
| 8195 | spin_lock_irq(&tree->lock); |
| 8196 | set_bit(BTRFS_ORDERED_TRUNCATED, &ordered->flags); |
| 8197 | new_len = page_start - ordered->file_offset; |
| 8198 | if (new_len < ordered->truncated_len) |
| 8199 | ordered->truncated_len = new_len; |
| 8200 | spin_unlock_irq(&tree->lock); |
| 8201 | |
| 8202 | if (btrfs_dec_test_ordered_pending(inode, &ordered, |
| 8203 | page_start, |
| 8204 | PAGE_CACHE_SIZE, 1)) |
| 8205 | btrfs_finish_ordered_io(ordered); |
Chris Mason | 8b62b72 | 2009-09-02 16:53:46 -0400 | [diff] [blame] | 8206 | } |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 8207 | btrfs_put_ordered_extent(ordered); |
Filipe David Borba Manana | 131e404 | 2013-11-19 22:29:35 +0000 | [diff] [blame] | 8208 | if (!inode_evicting) { |
| 8209 | cached_state = NULL; |
| 8210 | lock_extent_bits(tree, page_start, page_end, 0, |
| 8211 | &cached_state); |
| 8212 | } |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 8213 | } |
Filipe David Borba Manana | 131e404 | 2013-11-19 22:29:35 +0000 | [diff] [blame] | 8214 | |
| 8215 | if (!inode_evicting) { |
| 8216 | clear_extent_bit(tree, page_start, page_end, |
| 8217 | EXTENT_LOCKED | EXTENT_DIRTY | |
| 8218 | EXTENT_DELALLOC | EXTENT_DO_ACCOUNTING | |
| 8219 | EXTENT_DEFRAG, 1, 1, |
| 8220 | &cached_state, GFP_NOFS); |
| 8221 | |
| 8222 | __btrfs_releasepage(page, GFP_NOFS); |
| 8223 | } |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 8224 | |
Chris Mason | 4a09675 | 2008-07-21 10:29:44 -0400 | [diff] [blame] | 8225 | ClearPageChecked(page); |
Chris Mason | 9ad6b7b | 2008-04-18 16:11:30 -0400 | [diff] [blame] | 8226 | if (PagePrivate(page)) { |
Chris Mason | 9ad6b7b | 2008-04-18 16:11:30 -0400 | [diff] [blame] | 8227 | ClearPagePrivate(page); |
| 8228 | set_page_private(page, 0); |
| 8229 | page_cache_release(page); |
| 8230 | } |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 8231 | } |
| 8232 | |
Chris Mason | 9ebefb18 | 2007-06-15 13:50:00 -0400 | [diff] [blame] | 8233 | /* |
| 8234 | * btrfs_page_mkwrite() is not allowed to change the file size as it gets |
| 8235 | * called from a page fault handler when a page is first dirtied. Hence we must |
| 8236 | * be careful to check for EOF conditions here. We set the page up correctly |
| 8237 | * for a written page which means we get ENOSPC checking when writing into |
| 8238 | * holes and correct delalloc and unwritten extent mapping on filesystems that |
| 8239 | * support these features. |
| 8240 | * |
| 8241 | * We are not allowed to take the i_mutex here so we have to play games to |
| 8242 | * protect against truncate races as the page could now be beyond EOF. Because |
| 8243 | * vmtruncate() writes the inode size before removing pages, once we have the |
| 8244 | * page lock we can determine safely if the page is beyond EOF. If it is not |
| 8245 | * beyond EOF, then the page is guaranteed safe against truncation until we |
| 8246 | * unlock the page. |
| 8247 | */ |
Nick Piggin | c2ec175 | 2009-03-31 15:23:21 -0700 | [diff] [blame] | 8248 | 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] | 8249 | { |
Nick Piggin | c2ec175 | 2009-03-31 15:23:21 -0700 | [diff] [blame] | 8250 | struct page *page = vmf->page; |
Al Viro | 496ad9a | 2013-01-23 17:07:38 -0500 | [diff] [blame] | 8251 | struct inode *inode = file_inode(vma->vm_file); |
Chris Mason | 1832a6d | 2007-12-21 16:27:21 -0500 | [diff] [blame] | 8252 | struct btrfs_root *root = BTRFS_I(inode)->root; |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 8253 | struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree; |
| 8254 | struct btrfs_ordered_extent *ordered; |
Josef Bacik | 2ac55d4 | 2010-02-03 19:33:23 +0000 | [diff] [blame] | 8255 | struct extent_state *cached_state = NULL; |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 8256 | char *kaddr; |
| 8257 | unsigned long zero_start; |
Chris Mason | 9ebefb18 | 2007-06-15 13:50:00 -0400 | [diff] [blame] | 8258 | loff_t size; |
Chris Mason | 1832a6d | 2007-12-21 16:27:21 -0500 | [diff] [blame] | 8259 | int ret; |
Chris Mason | 9998eb7 | 2012-01-25 13:47:40 -0500 | [diff] [blame] | 8260 | int reserved = 0; |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 8261 | u64 page_start; |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 8262 | u64 page_end; |
Chris Mason | 9ebefb18 | 2007-06-15 13:50:00 -0400 | [diff] [blame] | 8263 | |
Jan Kara | b2b5ef5 | 2012-06-12 16:20:45 +0200 | [diff] [blame] | 8264 | sb_start_pagefault(inode->i_sb); |
Yan, Zheng | 0ca1f7c | 2010-05-16 10:48:47 -0400 | [diff] [blame] | 8265 | ret = btrfs_delalloc_reserve_space(inode, PAGE_CACHE_SIZE); |
Chris Mason | 9998eb7 | 2012-01-25 13:47:40 -0500 | [diff] [blame] | 8266 | if (!ret) { |
Josef Bacik | e41f941 | 2012-03-26 09:46:47 -0400 | [diff] [blame] | 8267 | ret = file_update_time(vma->vm_file); |
Chris Mason | 9998eb7 | 2012-01-25 13:47:40 -0500 | [diff] [blame] | 8268 | reserved = 1; |
| 8269 | } |
Nick Piggin | 56a76f8 | 2009-03-31 15:23:23 -0700 | [diff] [blame] | 8270 | if (ret) { |
| 8271 | if (ret == -ENOMEM) |
| 8272 | ret = VM_FAULT_OOM; |
| 8273 | else /* -ENOSPC, -EIO, etc */ |
| 8274 | ret = VM_FAULT_SIGBUS; |
Chris Mason | 9998eb7 | 2012-01-25 13:47:40 -0500 | [diff] [blame] | 8275 | if (reserved) |
| 8276 | goto out; |
| 8277 | goto out_noreserve; |
Nick Piggin | 56a76f8 | 2009-03-31 15:23:23 -0700 | [diff] [blame] | 8278 | } |
Chris Mason | 1832a6d | 2007-12-21 16:27:21 -0500 | [diff] [blame] | 8279 | |
Nick Piggin | 56a76f8 | 2009-03-31 15:23:23 -0700 | [diff] [blame] | 8280 | ret = VM_FAULT_NOPAGE; /* make the VM retry the fault */ |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 8281 | again: |
Chris Mason | 9ebefb18 | 2007-06-15 13:50:00 -0400 | [diff] [blame] | 8282 | lock_page(page); |
Chris Mason | 9ebefb18 | 2007-06-15 13:50:00 -0400 | [diff] [blame] | 8283 | size = i_size_read(inode); |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 8284 | page_start = page_offset(page); |
| 8285 | page_end = page_start + PAGE_CACHE_SIZE - 1; |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 8286 | |
Chris Mason | 9ebefb18 | 2007-06-15 13:50:00 -0400 | [diff] [blame] | 8287 | if ((page->mapping != inode->i_mapping) || |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 8288 | (page_start >= size)) { |
Chris Mason | 9ebefb18 | 2007-06-15 13:50:00 -0400 | [diff] [blame] | 8289 | /* page got truncated out from underneath us */ |
| 8290 | goto out_unlock; |
| 8291 | } |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 8292 | wait_on_page_writeback(page); |
| 8293 | |
Jeff Mahoney | d008237 | 2012-03-01 14:57:19 +0100 | [diff] [blame] | 8294 | lock_extent_bits(io_tree, page_start, page_end, 0, &cached_state); |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 8295 | set_page_extent_mapped(page); |
| 8296 | |
Chris Mason | eb84ae0 | 2008-07-17 13:53:27 -0400 | [diff] [blame] | 8297 | /* |
| 8298 | * we can't set the delalloc bits if there are pending ordered |
| 8299 | * extents. Drop our locks and wait for them to finish |
| 8300 | */ |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 8301 | ordered = btrfs_lookup_ordered_extent(inode, page_start); |
| 8302 | if (ordered) { |
Josef Bacik | 2ac55d4 | 2010-02-03 19:33:23 +0000 | [diff] [blame] | 8303 | unlock_extent_cached(io_tree, page_start, page_end, |
| 8304 | &cached_state, GFP_NOFS); |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 8305 | unlock_page(page); |
Chris Mason | eb84ae0 | 2008-07-17 13:53:27 -0400 | [diff] [blame] | 8306 | btrfs_start_ordered_extent(inode, ordered, 1); |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 8307 | btrfs_put_ordered_extent(ordered); |
| 8308 | goto again; |
| 8309 | } |
| 8310 | |
Josef Bacik | fbf1908 | 2009-10-01 17:10:23 -0400 | [diff] [blame] | 8311 | /* |
| 8312 | * XXX - page_mkwrite gets called every time the page is dirtied, even |
| 8313 | * if it was already dirty, so for space accounting reasons we need to |
| 8314 | * clear any delalloc bits for the range we are fixing to save. There |
| 8315 | * is probably a better way to do this, but for now keep consistent with |
| 8316 | * prepare_pages in the normal write path. |
| 8317 | */ |
Josef Bacik | 2ac55d4 | 2010-02-03 19:33:23 +0000 | [diff] [blame] | 8318 | clear_extent_bit(&BTRFS_I(inode)->io_tree, page_start, page_end, |
Liu Bo | 9e8a4a8 | 2012-09-05 19:10:51 -0600 | [diff] [blame] | 8319 | EXTENT_DIRTY | EXTENT_DELALLOC | |
| 8320 | EXTENT_DO_ACCOUNTING | EXTENT_DEFRAG, |
Josef Bacik | 2ac55d4 | 2010-02-03 19:33:23 +0000 | [diff] [blame] | 8321 | 0, 0, &cached_state, GFP_NOFS); |
Josef Bacik | fbf1908 | 2009-10-01 17:10:23 -0400 | [diff] [blame] | 8322 | |
Josef Bacik | 2ac55d4 | 2010-02-03 19:33:23 +0000 | [diff] [blame] | 8323 | ret = btrfs_set_extent_delalloc(inode, page_start, page_end, |
| 8324 | &cached_state); |
Josef Bacik | 9ed74f2 | 2009-09-11 16:12:44 -0400 | [diff] [blame] | 8325 | if (ret) { |
Josef Bacik | 2ac55d4 | 2010-02-03 19:33:23 +0000 | [diff] [blame] | 8326 | unlock_extent_cached(io_tree, page_start, page_end, |
| 8327 | &cached_state, GFP_NOFS); |
Josef Bacik | 9ed74f2 | 2009-09-11 16:12:44 -0400 | [diff] [blame] | 8328 | ret = VM_FAULT_SIGBUS; |
| 8329 | goto out_unlock; |
| 8330 | } |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 8331 | ret = 0; |
Chris Mason | 9ebefb18 | 2007-06-15 13:50:00 -0400 | [diff] [blame] | 8332 | |
| 8333 | /* page is wholly or partially inside EOF */ |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 8334 | if (page_start + PAGE_CACHE_SIZE > size) |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 8335 | zero_start = size & ~PAGE_CACHE_MASK; |
Chris Mason | 9ebefb18 | 2007-06-15 13:50:00 -0400 | [diff] [blame] | 8336 | else |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 8337 | zero_start = PAGE_CACHE_SIZE; |
Chris Mason | 9ebefb18 | 2007-06-15 13:50:00 -0400 | [diff] [blame] | 8338 | |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 8339 | if (zero_start != PAGE_CACHE_SIZE) { |
| 8340 | kaddr = kmap(page); |
| 8341 | memset(kaddr + zero_start, 0, PAGE_CACHE_SIZE - zero_start); |
| 8342 | flush_dcache_page(page); |
| 8343 | kunmap(page); |
| 8344 | } |
Chris Mason | 247e743 | 2008-07-17 12:53:51 -0400 | [diff] [blame] | 8345 | ClearPageChecked(page); |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 8346 | set_page_dirty(page); |
Chris Mason | 50a9b21 | 2009-09-11 12:33:12 -0400 | [diff] [blame] | 8347 | SetPageUptodate(page); |
Chris Mason | 5a3f23d | 2009-03-31 13:27:11 -0400 | [diff] [blame] | 8348 | |
Chris Mason | 257c62e | 2009-10-13 13:21:08 -0400 | [diff] [blame] | 8349 | BTRFS_I(inode)->last_trans = root->fs_info->generation; |
| 8350 | BTRFS_I(inode)->last_sub_trans = BTRFS_I(inode)->root->log_transid; |
Liu Bo | 46d8bc3 | 2012-08-29 01:07:55 -0600 | [diff] [blame] | 8351 | 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] | 8352 | |
Josef Bacik | 2ac55d4 | 2010-02-03 19:33:23 +0000 | [diff] [blame] | 8353 | 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] | 8354 | |
| 8355 | out_unlock: |
Jan Kara | b2b5ef5 | 2012-06-12 16:20:45 +0200 | [diff] [blame] | 8356 | if (!ret) { |
| 8357 | sb_end_pagefault(inode->i_sb); |
Chris Mason | 50a9b21 | 2009-09-11 12:33:12 -0400 | [diff] [blame] | 8358 | return VM_FAULT_LOCKED; |
Jan Kara | b2b5ef5 | 2012-06-12 16:20:45 +0200 | [diff] [blame] | 8359 | } |
Chris Mason | 9ebefb18 | 2007-06-15 13:50:00 -0400 | [diff] [blame] | 8360 | unlock_page(page); |
Chris Mason | 1832a6d | 2007-12-21 16:27:21 -0500 | [diff] [blame] | 8361 | out: |
Josef Bacik | ec39e18 | 2012-01-12 19:10:12 -0500 | [diff] [blame] | 8362 | btrfs_delalloc_release_space(inode, PAGE_CACHE_SIZE); |
Chris Mason | 9998eb7 | 2012-01-25 13:47:40 -0500 | [diff] [blame] | 8363 | out_noreserve: |
Jan Kara | b2b5ef5 | 2012-06-12 16:20:45 +0200 | [diff] [blame] | 8364 | sb_end_pagefault(inode->i_sb); |
Chris Mason | 9ebefb18 | 2007-06-15 13:50:00 -0400 | [diff] [blame] | 8365 | return ret; |
| 8366 | } |
| 8367 | |
Josef Bacik | a41ad39 | 2011-01-31 15:30:16 -0500 | [diff] [blame] | 8368 | static int btrfs_truncate(struct inode *inode) |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 8369 | { |
| 8370 | struct btrfs_root *root = BTRFS_I(inode)->root; |
Josef Bacik | fcb80c2 | 2011-05-03 10:40:22 -0400 | [diff] [blame] | 8371 | struct btrfs_block_rsv *rsv; |
Josef Bacik | a71754f | 2013-06-17 17:14:39 -0400 | [diff] [blame] | 8372 | int ret = 0; |
Josef Bacik | 3893e33 | 2011-01-31 16:03:11 -0500 | [diff] [blame] | 8373 | int err = 0; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 8374 | struct btrfs_trans_handle *trans; |
Chris Mason | dbe674a | 2008-07-17 12:54:05 -0400 | [diff] [blame] | 8375 | u64 mask = root->sectorsize - 1; |
Josef Bacik | 0712718 | 2011-08-19 10:29:59 -0400 | [diff] [blame] | 8376 | u64 min_size = btrfs_calc_trunc_metadata_size(root, 1); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 8377 | |
Josef Bacik | 0ef8b72 | 2013-10-25 16:13:35 -0400 | [diff] [blame] | 8378 | ret = btrfs_wait_ordered_range(inode, inode->i_size & (~mask), |
| 8379 | (u64)-1); |
| 8380 | if (ret) |
| 8381 | return ret; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 8382 | |
Josef Bacik | fcb80c2 | 2011-05-03 10:40:22 -0400 | [diff] [blame] | 8383 | /* |
| 8384 | * Yes ladies and gentelment, this is indeed ugly. The fact is we have |
| 8385 | * 3 things going on here |
| 8386 | * |
| 8387 | * 1) We need to reserve space for our orphan item and the space to |
| 8388 | * delete our orphan item. Lord knows we don't want to have a dangling |
| 8389 | * orphan item because we didn't reserve space to remove it. |
| 8390 | * |
| 8391 | * 2) We need to reserve space to update our inode. |
| 8392 | * |
| 8393 | * 3) We need to have something to cache all the space that is going to |
| 8394 | * be free'd up by the truncate operation, but also have some slack |
| 8395 | * space reserved in case it uses space during the truncate (thank you |
| 8396 | * very much snapshotting). |
| 8397 | * |
| 8398 | * And we need these to all be seperate. The fact is we can use alot of |
| 8399 | * space doing the truncate, and we have no earthly idea how much space |
| 8400 | * we will use, so we need the truncate reservation to be seperate so it |
| 8401 | * doesn't end up using space reserved for updating the inode or |
| 8402 | * removing the orphan item. We also need to be able to stop the |
| 8403 | * transaction and start a new one, which means we need to be able to |
| 8404 | * update the inode several times, and we have no idea of knowing how |
| 8405 | * many times that will be, so we can't just reserve 1 item for the |
| 8406 | * entirety of the opration, so that has to be done seperately as well. |
| 8407 | * Then there is the orphan item, which does indeed need to be held on |
| 8408 | * to for the whole operation, and we need nobody to touch this reserved |
| 8409 | * space except the orphan code. |
| 8410 | * |
| 8411 | * So that leaves us with |
| 8412 | * |
| 8413 | * 1) root->orphan_block_rsv - for the orphan deletion. |
| 8414 | * 2) rsv - for the truncate reservation, which we will steal from the |
| 8415 | * transaction reservation. |
| 8416 | * 3) fs_info->trans_block_rsv - this will have 1 items worth left for |
| 8417 | * updating the inode. |
| 8418 | */ |
Miao Xie | 66d8f3d | 2012-09-06 04:02:28 -0600 | [diff] [blame] | 8419 | rsv = btrfs_alloc_block_rsv(root, BTRFS_BLOCK_RSV_TEMP); |
Josef Bacik | fcb80c2 | 2011-05-03 10:40:22 -0400 | [diff] [blame] | 8420 | if (!rsv) |
| 8421 | return -ENOMEM; |
Josef Bacik | 4a33854 | 2011-08-29 11:01:31 -0400 | [diff] [blame] | 8422 | rsv->size = min_size; |
Josef Bacik | ca7e70f | 2012-08-27 17:48:15 -0400 | [diff] [blame] | 8423 | rsv->failfast = 1; |
Josef Bacik | f0cd846 | 2011-03-04 14:37:08 -0500 | [diff] [blame] | 8424 | |
Josef Bacik | 907cbce | 2011-08-08 13:46:15 -0400 | [diff] [blame] | 8425 | /* |
Josef Bacik | 0712718 | 2011-08-19 10:29:59 -0400 | [diff] [blame] | 8426 | * 1 for the truncate slack space |
Josef Bacik | 907cbce | 2011-08-08 13:46:15 -0400 | [diff] [blame] | 8427 | * 1 for updating the inode. |
| 8428 | */ |
Josef Bacik | f3fe820 | 2013-01-07 17:03:21 -0500 | [diff] [blame] | 8429 | trans = btrfs_start_transaction(root, 2); |
Josef Bacik | fcb80c2 | 2011-05-03 10:40:22 -0400 | [diff] [blame] | 8430 | if (IS_ERR(trans)) { |
| 8431 | err = PTR_ERR(trans); |
| 8432 | goto out; |
| 8433 | } |
Josef Bacik | f0cd846 | 2011-03-04 14:37:08 -0500 | [diff] [blame] | 8434 | |
Josef Bacik | 907cbce | 2011-08-08 13:46:15 -0400 | [diff] [blame] | 8435 | /* Migrate the slack space for the truncate to our reserve */ |
| 8436 | ret = btrfs_block_rsv_migrate(&root->fs_info->trans_block_rsv, rsv, |
| 8437 | min_size); |
Josef Bacik | fcb80c2 | 2011-05-03 10:40:22 -0400 | [diff] [blame] | 8438 | BUG_ON(ret); |
Josef Bacik | f0cd846 | 2011-03-04 14:37:08 -0500 | [diff] [blame] | 8439 | |
Chris Mason | 5a3f23d | 2009-03-31 13:27:11 -0400 | [diff] [blame] | 8440 | /* |
Josef Bacik | 5dc562c | 2012-08-17 13:14:17 -0400 | [diff] [blame] | 8441 | * So if we truncate and then write and fsync we normally would just |
| 8442 | * write the extents that changed, which is a problem if we need to |
| 8443 | * first truncate that entire inode. So set this flag so we write out |
| 8444 | * all of the extents in the inode to the sync log so we're completely |
| 8445 | * safe. |
| 8446 | */ |
| 8447 | set_bit(BTRFS_INODE_NEEDS_FULL_SYNC, &BTRFS_I(inode)->runtime_flags); |
Josef Bacik | ca7e70f | 2012-08-27 17:48:15 -0400 | [diff] [blame] | 8448 | trans->block_rsv = rsv; |
Josef Bacik | 5dc562c | 2012-08-17 13:14:17 -0400 | [diff] [blame] | 8449 | |
Yan, Zheng | 8082510 | 2009-11-12 09:35:36 +0000 | [diff] [blame] | 8450 | while (1) { |
| 8451 | ret = btrfs_truncate_inode_items(trans, root, inode, |
| 8452 | inode->i_size, |
| 8453 | BTRFS_EXTENT_DATA_KEY); |
Josef Bacik | ca7e70f | 2012-08-27 17:48:15 -0400 | [diff] [blame] | 8454 | if (ret != -ENOSPC) { |
Josef Bacik | 3893e33 | 2011-01-31 16:03:11 -0500 | [diff] [blame] | 8455 | err = ret; |
Yan, Zheng | 8082510 | 2009-11-12 09:35:36 +0000 | [diff] [blame] | 8456 | break; |
Josef Bacik | 3893e33 | 2011-01-31 16:03:11 -0500 | [diff] [blame] | 8457 | } |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 8458 | |
Josef Bacik | fcb80c2 | 2011-05-03 10:40:22 -0400 | [diff] [blame] | 8459 | trans->block_rsv = &root->fs_info->trans_block_rsv; |
Yan, Zheng | 8082510 | 2009-11-12 09:35:36 +0000 | [diff] [blame] | 8460 | ret = btrfs_update_inode(trans, root, inode); |
Josef Bacik | 3893e33 | 2011-01-31 16:03:11 -0500 | [diff] [blame] | 8461 | if (ret) { |
| 8462 | err = ret; |
| 8463 | break; |
| 8464 | } |
Josef Bacik | ca7e70f | 2012-08-27 17:48:15 -0400 | [diff] [blame] | 8465 | |
Yan, Zheng | 8082510 | 2009-11-12 09:35:36 +0000 | [diff] [blame] | 8466 | btrfs_end_transaction(trans, root); |
Liu Bo | b53d3f5 | 2012-11-14 14:34:34 +0000 | [diff] [blame] | 8467 | btrfs_btree_balance_dirty(root); |
Josef Bacik | ca7e70f | 2012-08-27 17:48:15 -0400 | [diff] [blame] | 8468 | |
| 8469 | trans = btrfs_start_transaction(root, 2); |
| 8470 | if (IS_ERR(trans)) { |
| 8471 | ret = err = PTR_ERR(trans); |
| 8472 | trans = NULL; |
| 8473 | break; |
| 8474 | } |
| 8475 | |
| 8476 | ret = btrfs_block_rsv_migrate(&root->fs_info->trans_block_rsv, |
| 8477 | rsv, min_size); |
| 8478 | BUG_ON(ret); /* shouldn't happen */ |
| 8479 | trans->block_rsv = rsv; |
Yan, Zheng | 8082510 | 2009-11-12 09:35:36 +0000 | [diff] [blame] | 8480 | } |
| 8481 | |
| 8482 | if (ret == 0 && inode->i_nlink > 0) { |
Josef Bacik | fcb80c2 | 2011-05-03 10:40:22 -0400 | [diff] [blame] | 8483 | trans->block_rsv = root->orphan_block_rsv; |
Yan, Zheng | 8082510 | 2009-11-12 09:35:36 +0000 | [diff] [blame] | 8484 | ret = btrfs_orphan_del(trans, inode); |
Josef Bacik | 3893e33 | 2011-01-31 16:03:11 -0500 | [diff] [blame] | 8485 | if (ret) |
| 8486 | err = ret; |
Yan, Zheng | 8082510 | 2009-11-12 09:35:36 +0000 | [diff] [blame] | 8487 | } |
| 8488 | |
Chris Mason | 917c16b | 2011-11-08 14:49:59 -0500 | [diff] [blame] | 8489 | if (trans) { |
| 8490 | trans->block_rsv = &root->fs_info->trans_block_rsv; |
| 8491 | ret = btrfs_update_inode(trans, root, inode); |
| 8492 | if (ret && !err) |
| 8493 | err = ret; |
Josef Bacik | 7b12876 | 2008-07-24 12:17:14 -0400 | [diff] [blame] | 8494 | |
Josef Bacik | 7ad85bb | 2012-01-12 19:10:12 -0500 | [diff] [blame] | 8495 | ret = btrfs_end_transaction(trans, root); |
Liu Bo | b53d3f5 | 2012-11-14 14:34:34 +0000 | [diff] [blame] | 8496 | btrfs_btree_balance_dirty(root); |
Chris Mason | 917c16b | 2011-11-08 14:49:59 -0500 | [diff] [blame] | 8497 | } |
Josef Bacik | fcb80c2 | 2011-05-03 10:40:22 -0400 | [diff] [blame] | 8498 | |
| 8499 | out: |
| 8500 | btrfs_free_block_rsv(root, rsv); |
| 8501 | |
Josef Bacik | 3893e33 | 2011-01-31 16:03:11 -0500 | [diff] [blame] | 8502 | if (ret && !err) |
| 8503 | err = ret; |
Josef Bacik | a41ad39 | 2011-01-31 15:30:16 -0500 | [diff] [blame] | 8504 | |
Josef Bacik | 3893e33 | 2011-01-31 16:03:11 -0500 | [diff] [blame] | 8505 | return err; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 8506 | } |
| 8507 | |
Sven Wegener | 3b96362 | 2008-06-09 21:57:42 -0400 | [diff] [blame] | 8508 | /* |
Chris Mason | d352ac6 | 2008-09-29 15:18:18 -0400 | [diff] [blame] | 8509 | * create a new subvolume directory/inode (helper for the ioctl). |
| 8510 | */ |
Yan Zheng | d2fb343 | 2008-12-11 16:30:39 -0500 | [diff] [blame] | 8511 | int btrfs_create_subvol_root(struct btrfs_trans_handle *trans, |
Filipe David Borba Manana | 6354192 | 2014-01-07 11:47:46 +0000 | [diff] [blame] | 8512 | struct btrfs_root *new_root, |
| 8513 | struct btrfs_root *parent_root, |
| 8514 | u64 new_dirid) |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 8515 | { |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 8516 | struct inode *inode; |
Yan, Zheng | 76dda93 | 2009-09-21 16:00:26 -0400 | [diff] [blame] | 8517 | int err; |
Chris Mason | 00e4e6b | 2008-08-05 11:18:09 -0400 | [diff] [blame] | 8518 | u64 index = 0; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 8519 | |
Florian Albrechtskirchinger | 12fc9d0 | 2012-02-10 22:15:54 +0100 | [diff] [blame] | 8520 | inode = btrfs_new_inode(trans, new_root, NULL, "..", 2, |
| 8521 | new_dirid, new_dirid, |
| 8522 | S_IFDIR | (~current_umask() & S_IRWXUGO), |
| 8523 | &index); |
Chris Mason | 54aa1f4 | 2007-06-22 14:16:25 -0400 | [diff] [blame] | 8524 | if (IS_ERR(inode)) |
Christoph Hellwig | f46b5a6 | 2008-06-11 21:53:53 -0400 | [diff] [blame] | 8525 | return PTR_ERR(inode); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 8526 | inode->i_op = &btrfs_dir_inode_operations; |
| 8527 | inode->i_fop = &btrfs_dir_file_operations; |
| 8528 | |
Miklos Szeredi | bfe8684 | 2011-10-28 14:13:29 +0200 | [diff] [blame] | 8529 | set_nlink(inode, 1); |
Chris Mason | dbe674a | 2008-07-17 12:54:05 -0400 | [diff] [blame] | 8530 | btrfs_i_size_write(inode, 0); |
Chris Mason | b0d5d10 | 2014-09-08 13:08:51 -0700 | [diff] [blame] | 8531 | unlock_new_inode(inode); |
Sven Wegener | 3b96362 | 2008-06-09 21:57:42 -0400 | [diff] [blame] | 8532 | |
Filipe David Borba Manana | 6354192 | 2014-01-07 11:47:46 +0000 | [diff] [blame] | 8533 | err = btrfs_subvol_inherit_props(trans, new_root, parent_root); |
| 8534 | if (err) |
| 8535 | btrfs_err(new_root->fs_info, |
David Sterba | 351fd35 | 2014-05-15 16:48:20 +0200 | [diff] [blame] | 8536 | "error inheriting subvolume %llu properties: %d", |
Filipe David Borba Manana | 6354192 | 2014-01-07 11:47:46 +0000 | [diff] [blame] | 8537 | new_root->root_key.objectid, err); |
| 8538 | |
Yan, Zheng | 76dda93 | 2009-09-21 16:00:26 -0400 | [diff] [blame] | 8539 | err = btrfs_update_inode(trans, new_root, inode); |
Christoph Hellwig | cb8e709 | 2008-10-09 13:39:39 -0400 | [diff] [blame] | 8540 | |
Yan, Zheng | 76dda93 | 2009-09-21 16:00:26 -0400 | [diff] [blame] | 8541 | iput(inode); |
Mark Fasheh | ce59897 | 2011-07-26 11:32:23 -0700 | [diff] [blame] | 8542 | return err; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 8543 | } |
| 8544 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 8545 | struct inode *btrfs_alloc_inode(struct super_block *sb) |
| 8546 | { |
| 8547 | struct btrfs_inode *ei; |
Yan, Zheng | 2ead6ae | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 8548 | struct inode *inode; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 8549 | |
| 8550 | ei = kmem_cache_alloc(btrfs_inode_cachep, GFP_NOFS); |
| 8551 | if (!ei) |
| 8552 | return NULL; |
Yan, Zheng | 2ead6ae | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 8553 | |
| 8554 | ei->root = NULL; |
Yan, Zheng | 2ead6ae | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 8555 | ei->generation = 0; |
Josef Bacik | 15ee9bc | 2007-08-10 16:22:09 -0400 | [diff] [blame] | 8556 | ei->last_trans = 0; |
Chris Mason | 257c62e | 2009-10-13 13:21:08 -0400 | [diff] [blame] | 8557 | ei->last_sub_trans = 0; |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 8558 | ei->logged_trans = 0; |
Yan, Zheng | 2ead6ae | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 8559 | ei->delalloc_bytes = 0; |
Wang Shilong | 47059d9 | 2014-07-03 18:22:07 +0800 | [diff] [blame] | 8560 | ei->defrag_bytes = 0; |
Yan, Zheng | 2ead6ae | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 8561 | ei->disk_i_size = 0; |
| 8562 | ei->flags = 0; |
Josef Bacik | 7709cde | 2011-08-04 10:25:02 -0400 | [diff] [blame] | 8563 | ei->csum_bytes = 0; |
Yan, Zheng | 2ead6ae | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 8564 | ei->index_cnt = (u64)-1; |
Miao Xie | 67de117 | 2013-12-26 13:07:06 +0800 | [diff] [blame] | 8565 | ei->dir_index = 0; |
Yan, Zheng | 2ead6ae | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 8566 | ei->last_unlink_trans = 0; |
Liu Bo | 46d8bc3 | 2012-08-29 01:07:55 -0600 | [diff] [blame] | 8567 | ei->last_log_commit = 0; |
Yan, Zheng | 2ead6ae | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 8568 | |
Josef Bacik | 9e0baf6 | 2011-07-15 15:16:44 +0000 | [diff] [blame] | 8569 | spin_lock_init(&ei->lock); |
| 8570 | ei->outstanding_extents = 0; |
| 8571 | ei->reserved_extents = 0; |
Yan, Zheng | 2ead6ae | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 8572 | |
Josef Bacik | 72ac3c0 | 2012-05-23 14:13:11 -0400 | [diff] [blame] | 8573 | ei->runtime_flags = 0; |
Li Zefan | 261507a0 | 2010-12-17 14:21:50 +0800 | [diff] [blame] | 8574 | ei->force_compress = BTRFS_COMPRESS_NONE; |
Yan, Zheng | 2ead6ae | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 8575 | |
Miao Xie | 16cdcec | 2011-04-22 18:12:22 +0800 | [diff] [blame] | 8576 | ei->delayed_node = NULL; |
| 8577 | |
Yan, Zheng | 2ead6ae | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 8578 | inode = &ei->vfs_inode; |
David Sterba | a8067e0 | 2011-04-21 00:34:43 +0200 | [diff] [blame] | 8579 | extent_map_tree_init(&ei->extent_tree); |
David Sterba | f993c88 | 2011-04-20 23:35:57 +0200 | [diff] [blame] | 8580 | extent_io_tree_init(&ei->io_tree, &inode->i_data); |
| 8581 | extent_io_tree_init(&ei->io_failure_tree, &inode->i_data); |
Josef Bacik | 0b32f4b | 2012-03-13 09:38:00 -0400 | [diff] [blame] | 8582 | ei->io_tree.track_uptodate = 1; |
| 8583 | ei->io_failure_tree.track_uptodate = 1; |
Josef Bacik | b812ce2 | 2012-11-16 13:56:32 -0500 | [diff] [blame] | 8584 | atomic_set(&ei->sync_writers, 0); |
Yan, Zheng | 2ead6ae | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 8585 | mutex_init(&ei->log_mutex); |
Josef Bacik | f248679 | 2012-01-13 12:09:22 -0500 | [diff] [blame] | 8586 | mutex_init(&ei->delalloc_mutex); |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 8587 | btrfs_ordered_inode_tree_init(&ei->ordered_tree); |
Yan, Zheng | 2ead6ae | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 8588 | INIT_LIST_HEAD(&ei->delalloc_inodes); |
Yan, Zheng | 2ead6ae | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 8589 | RB_CLEAR_NODE(&ei->rb_node); |
| 8590 | |
| 8591 | return inode; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 8592 | } |
| 8593 | |
Josef Bacik | aaedb55 | 2013-10-11 14:44:09 -0400 | [diff] [blame] | 8594 | #ifdef CONFIG_BTRFS_FS_RUN_SANITY_TESTS |
| 8595 | void btrfs_test_destroy_inode(struct inode *inode) |
| 8596 | { |
| 8597 | btrfs_drop_extent_cache(inode, 0, (u64)-1, 0); |
| 8598 | kmem_cache_free(btrfs_inode_cachep, BTRFS_I(inode)); |
| 8599 | } |
| 8600 | #endif |
| 8601 | |
Nick Piggin | fa0d7e3d | 2011-01-07 17:49:49 +1100 | [diff] [blame] | 8602 | static void btrfs_i_callback(struct rcu_head *head) |
| 8603 | { |
| 8604 | struct inode *inode = container_of(head, struct inode, i_rcu); |
Nick Piggin | fa0d7e3d | 2011-01-07 17:49:49 +1100 | [diff] [blame] | 8605 | kmem_cache_free(btrfs_inode_cachep, BTRFS_I(inode)); |
| 8606 | } |
| 8607 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 8608 | void btrfs_destroy_inode(struct inode *inode) |
| 8609 | { |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 8610 | struct btrfs_ordered_extent *ordered; |
Chris Mason | 5a3f23d | 2009-03-31 13:27:11 -0400 | [diff] [blame] | 8611 | struct btrfs_root *root = BTRFS_I(inode)->root; |
| 8612 | |
Al Viro | b3d9b7a | 2012-06-09 13:51:19 -0400 | [diff] [blame] | 8613 | WARN_ON(!hlist_empty(&inode->i_dentry)); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 8614 | WARN_ON(inode->i_data.nrpages); |
Josef Bacik | 9e0baf6 | 2011-07-15 15:16:44 +0000 | [diff] [blame] | 8615 | WARN_ON(BTRFS_I(inode)->outstanding_extents); |
| 8616 | WARN_ON(BTRFS_I(inode)->reserved_extents); |
Josef Bacik | 7709cde | 2011-08-04 10:25:02 -0400 | [diff] [blame] | 8617 | WARN_ON(BTRFS_I(inode)->delalloc_bytes); |
| 8618 | WARN_ON(BTRFS_I(inode)->csum_bytes); |
Wang Shilong | 47059d9 | 2014-07-03 18:22:07 +0800 | [diff] [blame] | 8619 | WARN_ON(BTRFS_I(inode)->defrag_bytes); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 8620 | |
Chris Mason | 5a3f23d | 2009-03-31 13:27:11 -0400 | [diff] [blame] | 8621 | /* |
Josef Bacik | a6dbd42 | 2009-11-11 15:53:34 -0500 | [diff] [blame] | 8622 | * This can happen where we create an inode, but somebody else also |
| 8623 | * created the same inode and we need to destroy the one we already |
| 8624 | * created. |
| 8625 | */ |
| 8626 | if (!root) |
| 8627 | goto free; |
| 8628 | |
Josef Bacik | 8a35d95 | 2012-05-23 14:26:42 -0400 | [diff] [blame] | 8629 | if (test_bit(BTRFS_INODE_HAS_ORPHAN_ITEM, |
| 8630 | &BTRFS_I(inode)->runtime_flags)) { |
Simon Kirby | c2cf52e | 2013-03-19 22:41:23 +0000 | [diff] [blame] | 8631 | btrfs_info(root->fs_info, "inode %llu still on the orphan list", |
Geert Uytterhoeven | c1c9ff7 | 2013-08-20 13:20:07 +0200 | [diff] [blame] | 8632 | btrfs_ino(inode)); |
Josef Bacik | 8a35d95 | 2012-05-23 14:26:42 -0400 | [diff] [blame] | 8633 | atomic_dec(&root->orphan_inodes); |
Josef Bacik | 7b12876 | 2008-07-24 12:17:14 -0400 | [diff] [blame] | 8634 | } |
Josef Bacik | 7b12876 | 2008-07-24 12:17:14 -0400 | [diff] [blame] | 8635 | |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 8636 | while (1) { |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 8637 | ordered = btrfs_lookup_first_ordered_extent(inode, (u64)-1); |
| 8638 | if (!ordered) |
| 8639 | break; |
| 8640 | else { |
Simon Kirby | c2cf52e | 2013-03-19 22:41:23 +0000 | [diff] [blame] | 8641 | 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] | 8642 | ordered->file_offset, ordered->len); |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 8643 | btrfs_remove_ordered_extent(inode, ordered); |
| 8644 | btrfs_put_ordered_extent(ordered); |
| 8645 | btrfs_put_ordered_extent(ordered); |
| 8646 | } |
| 8647 | } |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 8648 | inode_tree_del(inode); |
Zheng Yan | 5b21f2e | 2008-09-26 10:05:38 -0400 | [diff] [blame] | 8649 | btrfs_drop_extent_cache(inode, 0, (u64)-1, 0); |
Josef Bacik | a6dbd42 | 2009-11-11 15:53:34 -0500 | [diff] [blame] | 8650 | free: |
Nick Piggin | fa0d7e3d | 2011-01-07 17:49:49 +1100 | [diff] [blame] | 8651 | call_rcu(&inode->i_rcu, btrfs_i_callback); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 8652 | } |
| 8653 | |
Al Viro | 45321ac | 2010-06-07 13:43:19 -0400 | [diff] [blame] | 8654 | int btrfs_drop_inode(struct inode *inode) |
Yan, Zheng | 76dda93 | 2009-09-21 16:00:26 -0400 | [diff] [blame] | 8655 | { |
| 8656 | struct btrfs_root *root = BTRFS_I(inode)->root; |
Al Viro | 45321ac | 2010-06-07 13:43:19 -0400 | [diff] [blame] | 8657 | |
Naohiro Aota | 6379ef9 | 2013-06-06 09:56:34 +0000 | [diff] [blame] | 8658 | if (root == NULL) |
| 8659 | return 1; |
| 8660 | |
Liu Bo | fa6ac87 | 2013-02-20 14:10:23 +0000 | [diff] [blame] | 8661 | /* the snap/subvol tree is on deleting */ |
Stefan Behrens | 69e9c6c | 2013-09-05 16:58:43 +0200 | [diff] [blame] | 8662 | if (btrfs_root_refs(&root->root_item) == 0) |
Al Viro | 45321ac | 2010-06-07 13:43:19 -0400 | [diff] [blame] | 8663 | return 1; |
Yan, Zheng | 76dda93 | 2009-09-21 16:00:26 -0400 | [diff] [blame] | 8664 | else |
Al Viro | 45321ac | 2010-06-07 13:43:19 -0400 | [diff] [blame] | 8665 | return generic_drop_inode(inode); |
Yan, Zheng | 76dda93 | 2009-09-21 16:00:26 -0400 | [diff] [blame] | 8666 | } |
| 8667 | |
Sven Wegener | 0ee0fda | 2008-07-30 16:54:26 -0400 | [diff] [blame] | 8668 | static void init_once(void *foo) |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 8669 | { |
| 8670 | struct btrfs_inode *ei = (struct btrfs_inode *) foo; |
| 8671 | |
| 8672 | inode_init_once(&ei->vfs_inode); |
| 8673 | } |
| 8674 | |
| 8675 | void btrfs_destroy_cachep(void) |
| 8676 | { |
Kirill A. Shutemov | 8c0a853 | 2012-09-26 11:33:07 +1000 | [diff] [blame] | 8677 | /* |
| 8678 | * Make sure all delayed rcu free inodes are flushed before we |
| 8679 | * destroy cache. |
| 8680 | */ |
| 8681 | rcu_barrier(); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 8682 | if (btrfs_inode_cachep) |
| 8683 | kmem_cache_destroy(btrfs_inode_cachep); |
| 8684 | if (btrfs_trans_handle_cachep) |
| 8685 | kmem_cache_destroy(btrfs_trans_handle_cachep); |
| 8686 | if (btrfs_transaction_cachep) |
| 8687 | kmem_cache_destroy(btrfs_transaction_cachep); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 8688 | if (btrfs_path_cachep) |
| 8689 | kmem_cache_destroy(btrfs_path_cachep); |
Josef Bacik | dc89e98 | 2011-01-28 17:05:48 -0500 | [diff] [blame] | 8690 | if (btrfs_free_space_cachep) |
| 8691 | kmem_cache_destroy(btrfs_free_space_cachep); |
Miao Xie | 8ccf6f19 | 2012-10-25 09:28:04 +0000 | [diff] [blame] | 8692 | if (btrfs_delalloc_work_cachep) |
| 8693 | kmem_cache_destroy(btrfs_delalloc_work_cachep); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 8694 | } |
| 8695 | |
| 8696 | int btrfs_init_cachep(void) |
| 8697 | { |
David Sterba | 837e197 | 2012-09-07 03:00:48 -0600 | [diff] [blame] | 8698 | btrfs_inode_cachep = kmem_cache_create("btrfs_inode", |
Christoph Hellwig | 9601e3f | 2009-04-13 15:33:09 +0200 | [diff] [blame] | 8699 | sizeof(struct btrfs_inode), 0, |
| 8700 | SLAB_RECLAIM_ACCOUNT | SLAB_MEM_SPREAD, init_once); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 8701 | if (!btrfs_inode_cachep) |
| 8702 | goto fail; |
Christoph Hellwig | 9601e3f | 2009-04-13 15:33:09 +0200 | [diff] [blame] | 8703 | |
David Sterba | 837e197 | 2012-09-07 03:00:48 -0600 | [diff] [blame] | 8704 | btrfs_trans_handle_cachep = kmem_cache_create("btrfs_trans_handle", |
Christoph Hellwig | 9601e3f | 2009-04-13 15:33:09 +0200 | [diff] [blame] | 8705 | sizeof(struct btrfs_trans_handle), 0, |
| 8706 | SLAB_RECLAIM_ACCOUNT | SLAB_MEM_SPREAD, NULL); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 8707 | if (!btrfs_trans_handle_cachep) |
| 8708 | goto fail; |
Christoph Hellwig | 9601e3f | 2009-04-13 15:33:09 +0200 | [diff] [blame] | 8709 | |
David Sterba | 837e197 | 2012-09-07 03:00:48 -0600 | [diff] [blame] | 8710 | btrfs_transaction_cachep = kmem_cache_create("btrfs_transaction", |
Christoph Hellwig | 9601e3f | 2009-04-13 15:33:09 +0200 | [diff] [blame] | 8711 | sizeof(struct btrfs_transaction), 0, |
| 8712 | SLAB_RECLAIM_ACCOUNT | SLAB_MEM_SPREAD, NULL); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 8713 | if (!btrfs_transaction_cachep) |
| 8714 | goto fail; |
Christoph Hellwig | 9601e3f | 2009-04-13 15:33:09 +0200 | [diff] [blame] | 8715 | |
David Sterba | 837e197 | 2012-09-07 03:00:48 -0600 | [diff] [blame] | 8716 | btrfs_path_cachep = kmem_cache_create("btrfs_path", |
Christoph Hellwig | 9601e3f | 2009-04-13 15:33:09 +0200 | [diff] [blame] | 8717 | sizeof(struct btrfs_path), 0, |
| 8718 | SLAB_RECLAIM_ACCOUNT | SLAB_MEM_SPREAD, NULL); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 8719 | if (!btrfs_path_cachep) |
| 8720 | goto fail; |
Christoph Hellwig | 9601e3f | 2009-04-13 15:33:09 +0200 | [diff] [blame] | 8721 | |
David Sterba | 837e197 | 2012-09-07 03:00:48 -0600 | [diff] [blame] | 8722 | btrfs_free_space_cachep = kmem_cache_create("btrfs_free_space", |
Josef Bacik | dc89e98 | 2011-01-28 17:05:48 -0500 | [diff] [blame] | 8723 | sizeof(struct btrfs_free_space), 0, |
| 8724 | SLAB_RECLAIM_ACCOUNT | SLAB_MEM_SPREAD, NULL); |
| 8725 | if (!btrfs_free_space_cachep) |
| 8726 | goto fail; |
| 8727 | |
Miao Xie | 8ccf6f19 | 2012-10-25 09:28:04 +0000 | [diff] [blame] | 8728 | btrfs_delalloc_work_cachep = kmem_cache_create("btrfs_delalloc_work", |
| 8729 | sizeof(struct btrfs_delalloc_work), 0, |
| 8730 | SLAB_RECLAIM_ACCOUNT | SLAB_MEM_SPREAD, |
| 8731 | NULL); |
| 8732 | if (!btrfs_delalloc_work_cachep) |
| 8733 | goto fail; |
| 8734 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 8735 | return 0; |
| 8736 | fail: |
| 8737 | btrfs_destroy_cachep(); |
| 8738 | return -ENOMEM; |
| 8739 | } |
| 8740 | |
| 8741 | static int btrfs_getattr(struct vfsmount *mnt, |
| 8742 | struct dentry *dentry, struct kstat *stat) |
| 8743 | { |
Miao Xie | df0af1a | 2013-01-29 10:11:59 +0000 | [diff] [blame] | 8744 | u64 delalloc_bytes; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 8745 | struct inode *inode = dentry->d_inode; |
David Sterba | fadc0d8 | 2011-11-20 07:33:38 -0500 | [diff] [blame] | 8746 | u32 blocksize = inode->i_sb->s_blocksize; |
| 8747 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 8748 | generic_fillattr(inode, stat); |
Al Viro | 0ee5dc6 | 2011-07-07 15:44:25 -0400 | [diff] [blame] | 8749 | stat->dev = BTRFS_I(inode)->root->anon_dev; |
Chris Mason | d666746 | 2008-01-03 14:51:00 -0500 | [diff] [blame] | 8750 | stat->blksize = PAGE_CACHE_SIZE; |
Miao Xie | df0af1a | 2013-01-29 10:11:59 +0000 | [diff] [blame] | 8751 | |
| 8752 | spin_lock(&BTRFS_I(inode)->lock); |
| 8753 | delalloc_bytes = BTRFS_I(inode)->delalloc_bytes; |
| 8754 | spin_unlock(&BTRFS_I(inode)->lock); |
David Sterba | fadc0d8 | 2011-11-20 07:33:38 -0500 | [diff] [blame] | 8755 | stat->blocks = (ALIGN(inode_get_bytes(inode), blocksize) + |
Miao Xie | df0af1a | 2013-01-29 10:11:59 +0000 | [diff] [blame] | 8756 | ALIGN(delalloc_bytes, blocksize)) >> 9; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 8757 | return 0; |
| 8758 | } |
| 8759 | |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 8760 | static int btrfs_rename(struct inode *old_dir, struct dentry *old_dentry, |
| 8761 | struct inode *new_dir, struct dentry *new_dentry) |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 8762 | { |
| 8763 | struct btrfs_trans_handle *trans; |
| 8764 | struct btrfs_root *root = BTRFS_I(old_dir)->root; |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 8765 | struct btrfs_root *dest = BTRFS_I(new_dir)->root; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 8766 | struct inode *new_inode = new_dentry->d_inode; |
| 8767 | struct inode *old_inode = old_dentry->d_inode; |
| 8768 | struct timespec ctime = CURRENT_TIME; |
Chris Mason | 00e4e6b | 2008-08-05 11:18:09 -0400 | [diff] [blame] | 8769 | u64 index = 0; |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 8770 | u64 root_objectid; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 8771 | int ret; |
Li Zefan | 33345d01 | 2011-04-20 10:31:50 +0800 | [diff] [blame] | 8772 | u64 old_ino = btrfs_ino(old_inode); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 8773 | |
Li Zefan | 33345d01 | 2011-04-20 10:31:50 +0800 | [diff] [blame] | 8774 | if (btrfs_ino(new_dir) == BTRFS_EMPTY_SUBVOL_DIR_OBJECTID) |
Yan, Zheng | f679a84 | 2009-09-24 09:17:31 -0400 | [diff] [blame] | 8775 | return -EPERM; |
| 8776 | |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 8777 | /* we only allow rename subvolume link between subvolumes */ |
Li Zefan | 33345d01 | 2011-04-20 10:31:50 +0800 | [diff] [blame] | 8778 | if (old_ino != BTRFS_FIRST_FREE_OBJECTID && root != dest) |
Chris Mason | 3394e16 | 2008-11-17 20:42:26 -0500 | [diff] [blame] | 8779 | return -EXDEV; |
| 8780 | |
Li Zefan | 33345d01 | 2011-04-20 10:31:50 +0800 | [diff] [blame] | 8781 | if (old_ino == BTRFS_EMPTY_SUBVOL_DIR_OBJECTID || |
| 8782 | (new_inode && btrfs_ino(new_inode) == BTRFS_FIRST_FREE_OBJECTID)) |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 8783 | return -ENOTEMPTY; |
| 8784 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 8785 | if (S_ISDIR(old_inode->i_mode) && new_inode && |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 8786 | new_inode->i_size > BTRFS_EMPTY_DIR_SIZE) |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 8787 | return -ENOTEMPTY; |
Chris Mason | 9c52057 | 2012-12-17 14:26:57 -0500 | [diff] [blame] | 8788 | |
| 8789 | |
| 8790 | /* check for collisions, even if the name isn't there */ |
Josef Bacik | 4871c15 | 2013-10-09 12:24:04 -0400 | [diff] [blame] | 8791 | ret = btrfs_check_dir_item_collision(dest, new_dir->i_ino, |
Chris Mason | 9c52057 | 2012-12-17 14:26:57 -0500 | [diff] [blame] | 8792 | new_dentry->d_name.name, |
| 8793 | new_dentry->d_name.len); |
| 8794 | |
| 8795 | if (ret) { |
| 8796 | if (ret == -EEXIST) { |
| 8797 | /* we shouldn't get |
| 8798 | * eexist without a new_inode */ |
Dulshani Gunawardhana | fae7f21 | 2013-10-31 10:30:08 +0530 | [diff] [blame] | 8799 | if (WARN_ON(!new_inode)) { |
Chris Mason | 9c52057 | 2012-12-17 14:26:57 -0500 | [diff] [blame] | 8800 | return ret; |
| 8801 | } |
| 8802 | } else { |
| 8803 | /* maybe -EOVERFLOW */ |
| 8804 | return ret; |
| 8805 | } |
| 8806 | } |
| 8807 | ret = 0; |
| 8808 | |
Chris Mason | 5a3f23d | 2009-03-31 13:27:11 -0400 | [diff] [blame] | 8809 | /* |
Chris Mason | 8d875f9 | 2014-08-12 10:47:42 -0700 | [diff] [blame] | 8810 | * we're using rename to replace one file with another. Start IO on it |
| 8811 | * 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] | 8812 | */ |
Chris Mason | 8d875f9 | 2014-08-12 10:47:42 -0700 | [diff] [blame] | 8813 | 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] | 8814 | filemap_flush(old_inode->i_mapping); |
| 8815 | |
Yan, Zheng | 76dda93 | 2009-09-21 16:00:26 -0400 | [diff] [blame] | 8816 | /* close the racy window with snapshot create/destroy ioctl */ |
Li Zefan | 33345d01 | 2011-04-20 10:31:50 +0800 | [diff] [blame] | 8817 | if (old_ino == BTRFS_FIRST_FREE_OBJECTID) |
Yan, Zheng | 76dda93 | 2009-09-21 16:00:26 -0400 | [diff] [blame] | 8818 | down_read(&root->fs_info->subvol_sem); |
Yan, Zheng | a22285a | 2010-05-16 10:48:46 -0400 | [diff] [blame] | 8819 | /* |
| 8820 | * We want to reserve the absolute worst case amount of items. So if |
| 8821 | * both inodes are subvols and we need to unlink them then that would |
| 8822 | * require 4 item modifications, but if they are both normal inodes it |
| 8823 | * would require 5 item modifications, so we'll assume their normal |
| 8824 | * inodes. So 5 * 2 is 10, plus 1 for the new link, so 11 total items |
| 8825 | * should cover the worst case number of items we'll modify. |
| 8826 | */ |
Josef Bacik | 6e137ed | 2013-03-26 15:26:55 -0400 | [diff] [blame] | 8827 | trans = btrfs_start_transaction(root, 11); |
Johann Lombardi | b44c59a | 2011-03-31 13:23:47 +0000 | [diff] [blame] | 8828 | if (IS_ERR(trans)) { |
| 8829 | ret = PTR_ERR(trans); |
| 8830 | goto out_notrans; |
| 8831 | } |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 8832 | |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 8833 | if (dest != root) |
| 8834 | btrfs_record_root_in_trans(trans, dest); |
| 8835 | |
Yan, Zheng | a571952 | 2009-09-24 09:17:31 -0400 | [diff] [blame] | 8836 | ret = btrfs_set_inode_index(new_dir, &index); |
| 8837 | if (ret) |
| 8838 | goto out_fail; |
Chris Mason | 5a3f23d | 2009-03-31 13:27:11 -0400 | [diff] [blame] | 8839 | |
Miao Xie | 67de117 | 2013-12-26 13:07:06 +0800 | [diff] [blame] | 8840 | BTRFS_I(old_inode)->dir_index = 0ULL; |
Li Zefan | 33345d01 | 2011-04-20 10:31:50 +0800 | [diff] [blame] | 8841 | if (unlikely(old_ino == BTRFS_FIRST_FREE_OBJECTID)) { |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 8842 | /* force full log commit if subvolume involved. */ |
Miao Xie | 995946d | 2014-04-02 19:51:06 +0800 | [diff] [blame] | 8843 | btrfs_set_log_full_commit(root->fs_info, trans); |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 8844 | } else { |
Yan, Zheng | a571952 | 2009-09-24 09:17:31 -0400 | [diff] [blame] | 8845 | ret = btrfs_insert_inode_ref(trans, dest, |
| 8846 | new_dentry->d_name.name, |
| 8847 | new_dentry->d_name.len, |
Li Zefan | 33345d01 | 2011-04-20 10:31:50 +0800 | [diff] [blame] | 8848 | old_ino, |
| 8849 | btrfs_ino(new_dir), index); |
Yan, Zheng | a571952 | 2009-09-24 09:17:31 -0400 | [diff] [blame] | 8850 | if (ret) |
| 8851 | goto out_fail; |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 8852 | /* |
| 8853 | * this is an ugly little race, but the rename is required |
| 8854 | * to make sure that if we crash, the inode is either at the |
| 8855 | * old name or the new one. pinning the log transaction lets |
| 8856 | * us make sure we don't allow a log commit to come in after |
| 8857 | * we unlink the name but before we add the new name back in. |
| 8858 | */ |
| 8859 | btrfs_pin_log_trans(root); |
| 8860 | } |
Chris Mason | 5a3f23d | 2009-03-31 13:27:11 -0400 | [diff] [blame] | 8861 | |
Josef Bacik | 0c4d2d9 | 2012-04-05 15:03:02 -0400 | [diff] [blame] | 8862 | inode_inc_iversion(old_dir); |
| 8863 | inode_inc_iversion(new_dir); |
| 8864 | inode_inc_iversion(old_inode); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 8865 | old_dir->i_ctime = old_dir->i_mtime = ctime; |
| 8866 | new_dir->i_ctime = new_dir->i_mtime = ctime; |
| 8867 | old_inode->i_ctime = ctime; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 8868 | |
Chris Mason | 12fcfd2 | 2009-03-24 10:24:20 -0400 | [diff] [blame] | 8869 | if (old_dentry->d_parent != new_dentry->d_parent) |
| 8870 | btrfs_record_unlink_dir(trans, old_dir, old_inode, 1); |
| 8871 | |
Li Zefan | 33345d01 | 2011-04-20 10:31:50 +0800 | [diff] [blame] | 8872 | if (unlikely(old_ino == BTRFS_FIRST_FREE_OBJECTID)) { |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 8873 | root_objectid = BTRFS_I(old_inode)->root->root_key.objectid; |
| 8874 | ret = btrfs_unlink_subvol(trans, root, old_dir, root_objectid, |
| 8875 | old_dentry->d_name.name, |
| 8876 | old_dentry->d_name.len); |
| 8877 | } else { |
Al Viro | 9298679 | 2011-03-04 17:14:37 +0000 | [diff] [blame] | 8878 | ret = __btrfs_unlink_inode(trans, root, old_dir, |
| 8879 | old_dentry->d_inode, |
| 8880 | old_dentry->d_name.name, |
| 8881 | old_dentry->d_name.len); |
| 8882 | if (!ret) |
| 8883 | ret = btrfs_update_inode(trans, root, old_inode); |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 8884 | } |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 8885 | if (ret) { |
| 8886 | btrfs_abort_transaction(trans, root, ret); |
| 8887 | goto out_fail; |
| 8888 | } |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 8889 | |
| 8890 | if (new_inode) { |
Josef Bacik | 0c4d2d9 | 2012-04-05 15:03:02 -0400 | [diff] [blame] | 8891 | inode_inc_iversion(new_inode); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 8892 | new_inode->i_ctime = CURRENT_TIME; |
Li Zefan | 33345d01 | 2011-04-20 10:31:50 +0800 | [diff] [blame] | 8893 | if (unlikely(btrfs_ino(new_inode) == |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 8894 | BTRFS_EMPTY_SUBVOL_DIR_OBJECTID)) { |
| 8895 | root_objectid = BTRFS_I(new_inode)->location.objectid; |
| 8896 | ret = btrfs_unlink_subvol(trans, dest, new_dir, |
| 8897 | root_objectid, |
| 8898 | new_dentry->d_name.name, |
| 8899 | new_dentry->d_name.len); |
| 8900 | BUG_ON(new_inode->i_nlink == 0); |
| 8901 | } else { |
| 8902 | ret = btrfs_unlink_inode(trans, dest, new_dir, |
| 8903 | new_dentry->d_inode, |
| 8904 | new_dentry->d_name.name, |
| 8905 | new_dentry->d_name.len); |
| 8906 | } |
Josef Bacik | 4ef31a4 | 2013-08-13 14:10:08 -0400 | [diff] [blame] | 8907 | if (!ret && new_inode->i_nlink == 0) |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 8908 | ret = btrfs_orphan_add(trans, new_dentry->d_inode); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 8909 | if (ret) { |
| 8910 | btrfs_abort_transaction(trans, root, ret); |
| 8911 | goto out_fail; |
| 8912 | } |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 8913 | } |
Josef Bacik | aec7477 | 2008-07-24 12:12:38 -0400 | [diff] [blame] | 8914 | |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 8915 | ret = btrfs_add_link(trans, new_dir, old_inode, |
| 8916 | new_dentry->d_name.name, |
Yan, Zheng | a571952 | 2009-09-24 09:17:31 -0400 | [diff] [blame] | 8917 | new_dentry->d_name.len, 0, index); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 8918 | if (ret) { |
| 8919 | btrfs_abort_transaction(trans, root, ret); |
| 8920 | goto out_fail; |
| 8921 | } |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 8922 | |
Miao Xie | 67de117 | 2013-12-26 13:07:06 +0800 | [diff] [blame] | 8923 | if (old_inode->i_nlink == 1) |
| 8924 | BTRFS_I(old_inode)->dir_index = index; |
| 8925 | |
Li Zefan | 33345d01 | 2011-04-20 10:31:50 +0800 | [diff] [blame] | 8926 | if (old_ino != BTRFS_FIRST_FREE_OBJECTID) { |
Al Viro | 10d9f30 | 2011-07-16 23:09:10 -0400 | [diff] [blame] | 8927 | struct dentry *parent = new_dentry->d_parent; |
Josef Bacik | 6a91221 | 2010-11-20 09:48:00 +0000 | [diff] [blame] | 8928 | btrfs_log_new_name(trans, old_inode, old_dir, parent); |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 8929 | btrfs_end_log_trans(root); |
| 8930 | } |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 8931 | out_fail: |
Josef Bacik | 7ad85bb | 2012-01-12 19:10:12 -0500 | [diff] [blame] | 8932 | btrfs_end_transaction(trans, root); |
Johann Lombardi | b44c59a | 2011-03-31 13:23:47 +0000 | [diff] [blame] | 8933 | out_notrans: |
Li Zefan | 33345d01 | 2011-04-20 10:31:50 +0800 | [diff] [blame] | 8934 | if (old_ino == BTRFS_FIRST_FREE_OBJECTID) |
Yan, Zheng | 76dda93 | 2009-09-21 16:00:26 -0400 | [diff] [blame] | 8935 | up_read(&root->fs_info->subvol_sem); |
Josef Bacik | 9ed74f2 | 2009-09-11 16:12:44 -0400 | [diff] [blame] | 8936 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 8937 | return ret; |
| 8938 | } |
| 8939 | |
Miklos Szeredi | 80ace85 | 2014-07-23 15:15:32 +0200 | [diff] [blame] | 8940 | static int btrfs_rename2(struct inode *old_dir, struct dentry *old_dentry, |
| 8941 | struct inode *new_dir, struct dentry *new_dentry, |
| 8942 | unsigned int flags) |
| 8943 | { |
| 8944 | if (flags & ~RENAME_NOREPLACE) |
| 8945 | return -EINVAL; |
| 8946 | |
| 8947 | return btrfs_rename(old_dir, old_dentry, new_dir, new_dentry); |
| 8948 | } |
| 8949 | |
Miao Xie | 8ccf6f19 | 2012-10-25 09:28:04 +0000 | [diff] [blame] | 8950 | static void btrfs_run_delalloc_work(struct btrfs_work *work) |
| 8951 | { |
| 8952 | struct btrfs_delalloc_work *delalloc_work; |
Josef Bacik | 9f23e28 | 2013-10-28 15:03:41 -0400 | [diff] [blame] | 8953 | struct inode *inode; |
Miao Xie | 8ccf6f19 | 2012-10-25 09:28:04 +0000 | [diff] [blame] | 8954 | |
| 8955 | delalloc_work = container_of(work, struct btrfs_delalloc_work, |
| 8956 | work); |
Josef Bacik | 9f23e28 | 2013-10-28 15:03:41 -0400 | [diff] [blame] | 8957 | inode = delalloc_work->inode; |
| 8958 | if (delalloc_work->wait) { |
| 8959 | btrfs_wait_ordered_range(inode, 0, (u64)-1); |
| 8960 | } else { |
| 8961 | filemap_flush(inode->i_mapping); |
| 8962 | if (test_bit(BTRFS_INODE_HAS_ASYNC_EXTENT, |
| 8963 | &BTRFS_I(inode)->runtime_flags)) |
| 8964 | filemap_flush(inode->i_mapping); |
| 8965 | } |
Miao Xie | 8ccf6f19 | 2012-10-25 09:28:04 +0000 | [diff] [blame] | 8966 | |
| 8967 | if (delalloc_work->delay_iput) |
Josef Bacik | 9f23e28 | 2013-10-28 15:03:41 -0400 | [diff] [blame] | 8968 | btrfs_add_delayed_iput(inode); |
Miao Xie | 8ccf6f19 | 2012-10-25 09:28:04 +0000 | [diff] [blame] | 8969 | else |
Josef Bacik | 9f23e28 | 2013-10-28 15:03:41 -0400 | [diff] [blame] | 8970 | iput(inode); |
Miao Xie | 8ccf6f19 | 2012-10-25 09:28:04 +0000 | [diff] [blame] | 8971 | complete(&delalloc_work->completion); |
| 8972 | } |
| 8973 | |
| 8974 | struct btrfs_delalloc_work *btrfs_alloc_delalloc_work(struct inode *inode, |
| 8975 | int wait, int delay_iput) |
| 8976 | { |
| 8977 | struct btrfs_delalloc_work *work; |
| 8978 | |
| 8979 | work = kmem_cache_zalloc(btrfs_delalloc_work_cachep, GFP_NOFS); |
| 8980 | if (!work) |
| 8981 | return NULL; |
| 8982 | |
| 8983 | init_completion(&work->completion); |
| 8984 | INIT_LIST_HEAD(&work->list); |
| 8985 | work->inode = inode; |
| 8986 | work->wait = wait; |
| 8987 | work->delay_iput = delay_iput; |
Liu Bo | 9e0af23 | 2014-08-15 23:36:53 +0800 | [diff] [blame] | 8988 | WARN_ON_ONCE(!inode); |
| 8989 | btrfs_init_work(&work->work, btrfs_flush_delalloc_helper, |
| 8990 | btrfs_run_delalloc_work, NULL, NULL); |
Miao Xie | 8ccf6f19 | 2012-10-25 09:28:04 +0000 | [diff] [blame] | 8991 | |
| 8992 | return work; |
| 8993 | } |
| 8994 | |
| 8995 | void btrfs_wait_and_free_delalloc_work(struct btrfs_delalloc_work *work) |
| 8996 | { |
| 8997 | wait_for_completion(&work->completion); |
| 8998 | kmem_cache_free(btrfs_delalloc_work_cachep, work); |
| 8999 | } |
| 9000 | |
Chris Mason | d352ac6 | 2008-09-29 15:18:18 -0400 | [diff] [blame] | 9001 | /* |
| 9002 | * some fairly slow code that needs optimization. This walks the list |
| 9003 | * of all the inodes with pending delalloc and forces them to disk. |
| 9004 | */ |
Miao Xie | 6c255e6 | 2014-03-06 13:55:01 +0800 | [diff] [blame] | 9005 | static int __start_delalloc_inodes(struct btrfs_root *root, int delay_iput, |
| 9006 | int nr) |
Chris Mason | ea8c281 | 2008-08-04 23:17:27 -0400 | [diff] [blame] | 9007 | { |
Chris Mason | ea8c281 | 2008-08-04 23:17:27 -0400 | [diff] [blame] | 9008 | struct btrfs_inode *binode; |
Zheng Yan | 5b21f2e | 2008-09-26 10:05:38 -0400 | [diff] [blame] | 9009 | struct inode *inode; |
Miao Xie | 8ccf6f19 | 2012-10-25 09:28:04 +0000 | [diff] [blame] | 9010 | struct btrfs_delalloc_work *work, *next; |
| 9011 | struct list_head works; |
Miao Xie | 1eafa6c | 2013-01-22 10:49:00 +0000 | [diff] [blame] | 9012 | struct list_head splice; |
Miao Xie | 8ccf6f19 | 2012-10-25 09:28:04 +0000 | [diff] [blame] | 9013 | int ret = 0; |
Chris Mason | ea8c281 | 2008-08-04 23:17:27 -0400 | [diff] [blame] | 9014 | |
Miao Xie | 8ccf6f19 | 2012-10-25 09:28:04 +0000 | [diff] [blame] | 9015 | INIT_LIST_HEAD(&works); |
Miao Xie | 1eafa6c | 2013-01-22 10:49:00 +0000 | [diff] [blame] | 9016 | INIT_LIST_HEAD(&splice); |
Miao Xie | 63607cc | 2013-01-22 10:50:35 +0000 | [diff] [blame] | 9017 | |
Miao Xie | 573bfb7 | 2014-03-06 13:55:03 +0800 | [diff] [blame] | 9018 | mutex_lock(&root->delalloc_mutex); |
Miao Xie | eb73c1b | 2013-05-15 07:48:22 +0000 | [diff] [blame] | 9019 | spin_lock(&root->delalloc_lock); |
| 9020 | list_splice_init(&root->delalloc_inodes, &splice); |
Miao Xie | 1eafa6c | 2013-01-22 10:49:00 +0000 | [diff] [blame] | 9021 | while (!list_empty(&splice)) { |
| 9022 | binode = list_entry(splice.next, struct btrfs_inode, |
Chris Mason | ea8c281 | 2008-08-04 23:17:27 -0400 | [diff] [blame] | 9023 | delalloc_inodes); |
Miao Xie | 1eafa6c | 2013-01-22 10:49:00 +0000 | [diff] [blame] | 9024 | |
Miao Xie | eb73c1b | 2013-05-15 07:48:22 +0000 | [diff] [blame] | 9025 | list_move_tail(&binode->delalloc_inodes, |
| 9026 | &root->delalloc_inodes); |
Zheng Yan | 5b21f2e | 2008-09-26 10:05:38 -0400 | [diff] [blame] | 9027 | inode = igrab(&binode->vfs_inode); |
Miao Xie | df0af1a | 2013-01-29 10:11:59 +0000 | [diff] [blame] | 9028 | if (!inode) { |
Miao Xie | eb73c1b | 2013-05-15 07:48:22 +0000 | [diff] [blame] | 9029 | cond_resched_lock(&root->delalloc_lock); |
Miao Xie | 1eafa6c | 2013-01-22 10:49:00 +0000 | [diff] [blame] | 9030 | continue; |
Miao Xie | df0af1a | 2013-01-29 10:11:59 +0000 | [diff] [blame] | 9031 | } |
Miao Xie | eb73c1b | 2013-05-15 07:48:22 +0000 | [diff] [blame] | 9032 | spin_unlock(&root->delalloc_lock); |
Miao Xie | 1eafa6c | 2013-01-22 10:49:00 +0000 | [diff] [blame] | 9033 | |
| 9034 | work = btrfs_alloc_delalloc_work(inode, 0, delay_iput); |
David Sterba | 5d99a998 | 2014-09-29 19:20:37 +0200 | [diff] [blame] | 9035 | if (!work) { |
Josef Bacik | f4ab9ea | 2013-09-17 11:25:44 -0400 | [diff] [blame] | 9036 | if (delay_iput) |
| 9037 | btrfs_add_delayed_iput(inode); |
| 9038 | else |
| 9039 | iput(inode); |
Miao Xie | 1eafa6c | 2013-01-22 10:49:00 +0000 | [diff] [blame] | 9040 | ret = -ENOMEM; |
Wang Shilong | a1ecaab | 2014-04-02 19:53:32 +0800 | [diff] [blame] | 9041 | goto out; |
Zheng Yan | 5b21f2e | 2008-09-26 10:05:38 -0400 | [diff] [blame] | 9042 | } |
Miao Xie | 1eafa6c | 2013-01-22 10:49:00 +0000 | [diff] [blame] | 9043 | list_add_tail(&work->list, &works); |
Qu Wenruo | a44903a | 2014-02-28 10:46:09 +0800 | [diff] [blame] | 9044 | btrfs_queue_work(root->fs_info->flush_workers, |
| 9045 | &work->work); |
Miao Xie | 6c255e6 | 2014-03-06 13:55:01 +0800 | [diff] [blame] | 9046 | ret++; |
| 9047 | if (nr != -1 && ret >= nr) |
Wang Shilong | a1ecaab | 2014-04-02 19:53:32 +0800 | [diff] [blame] | 9048 | goto out; |
Zheng Yan | 5b21f2e | 2008-09-26 10:05:38 -0400 | [diff] [blame] | 9049 | cond_resched(); |
Miao Xie | eb73c1b | 2013-05-15 07:48:22 +0000 | [diff] [blame] | 9050 | spin_lock(&root->delalloc_lock); |
Chris Mason | ea8c281 | 2008-08-04 23:17:27 -0400 | [diff] [blame] | 9051 | } |
Miao Xie | eb73c1b | 2013-05-15 07:48:22 +0000 | [diff] [blame] | 9052 | spin_unlock(&root->delalloc_lock); |
Chris Mason | 8c8bee1 | 2008-09-29 11:19:10 -0400 | [diff] [blame] | 9053 | |
Wang Shilong | a1ecaab | 2014-04-02 19:53:32 +0800 | [diff] [blame] | 9054 | out: |
Miao Xie | 1eafa6c | 2013-01-22 10:49:00 +0000 | [diff] [blame] | 9055 | list_for_each_entry_safe(work, next, &works, list) { |
| 9056 | list_del_init(&work->list); |
| 9057 | btrfs_wait_and_free_delalloc_work(work); |
| 9058 | } |
| 9059 | |
Miao Xie | eb73c1b | 2013-05-15 07:48:22 +0000 | [diff] [blame] | 9060 | if (!list_empty_careful(&splice)) { |
| 9061 | spin_lock(&root->delalloc_lock); |
| 9062 | list_splice_tail(&splice, &root->delalloc_inodes); |
| 9063 | spin_unlock(&root->delalloc_lock); |
| 9064 | } |
Miao Xie | 573bfb7 | 2014-03-06 13:55:03 +0800 | [diff] [blame] | 9065 | mutex_unlock(&root->delalloc_mutex); |
Miao Xie | eb73c1b | 2013-05-15 07:48:22 +0000 | [diff] [blame] | 9066 | return ret; |
| 9067 | } |
| 9068 | |
| 9069 | int btrfs_start_delalloc_inodes(struct btrfs_root *root, int delay_iput) |
| 9070 | { |
| 9071 | int ret; |
| 9072 | |
Wang Shilong | 2c21b4d | 2014-01-14 19:42:20 +0800 | [diff] [blame] | 9073 | if (test_bit(BTRFS_FS_STATE_ERROR, &root->fs_info->fs_state)) |
Miao Xie | eb73c1b | 2013-05-15 07:48:22 +0000 | [diff] [blame] | 9074 | return -EROFS; |
| 9075 | |
Miao Xie | 6c255e6 | 2014-03-06 13:55:01 +0800 | [diff] [blame] | 9076 | ret = __start_delalloc_inodes(root, delay_iput, -1); |
| 9077 | if (ret > 0) |
| 9078 | ret = 0; |
Miao Xie | eb73c1b | 2013-05-15 07:48:22 +0000 | [diff] [blame] | 9079 | /* |
| 9080 | * the filemap_flush will queue IO into the worker threads, but |
Chris Mason | 8c8bee1 | 2008-09-29 11:19:10 -0400 | [diff] [blame] | 9081 | * we have to make sure the IO is actually started and that |
| 9082 | * ordered extents get created before we return |
| 9083 | */ |
| 9084 | atomic_inc(&root->fs_info->async_submit_draining); |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 9085 | while (atomic_read(&root->fs_info->nr_async_submits) || |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 9086 | atomic_read(&root->fs_info->async_delalloc_pages)) { |
Chris Mason | 8c8bee1 | 2008-09-29 11:19:10 -0400 | [diff] [blame] | 9087 | wait_event(root->fs_info->async_submit_wait, |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 9088 | (atomic_read(&root->fs_info->nr_async_submits) == 0 && |
| 9089 | atomic_read(&root->fs_info->async_delalloc_pages) == 0)); |
Chris Mason | 8c8bee1 | 2008-09-29 11:19:10 -0400 | [diff] [blame] | 9090 | } |
| 9091 | atomic_dec(&root->fs_info->async_submit_draining); |
Miao Xie | eb73c1b | 2013-05-15 07:48:22 +0000 | [diff] [blame] | 9092 | return ret; |
| 9093 | } |
| 9094 | |
Miao Xie | 6c255e6 | 2014-03-06 13:55:01 +0800 | [diff] [blame] | 9095 | int btrfs_start_delalloc_roots(struct btrfs_fs_info *fs_info, int delay_iput, |
| 9096 | int nr) |
Miao Xie | eb73c1b | 2013-05-15 07:48:22 +0000 | [diff] [blame] | 9097 | { |
| 9098 | struct btrfs_root *root; |
| 9099 | struct list_head splice; |
| 9100 | int ret; |
| 9101 | |
Wang Shilong | 2c21b4d | 2014-01-14 19:42:20 +0800 | [diff] [blame] | 9102 | if (test_bit(BTRFS_FS_STATE_ERROR, &fs_info->fs_state)) |
Miao Xie | eb73c1b | 2013-05-15 07:48:22 +0000 | [diff] [blame] | 9103 | return -EROFS; |
| 9104 | |
| 9105 | INIT_LIST_HEAD(&splice); |
| 9106 | |
Miao Xie | 573bfb7 | 2014-03-06 13:55:03 +0800 | [diff] [blame] | 9107 | mutex_lock(&fs_info->delalloc_root_mutex); |
Miao Xie | eb73c1b | 2013-05-15 07:48:22 +0000 | [diff] [blame] | 9108 | spin_lock(&fs_info->delalloc_root_lock); |
| 9109 | list_splice_init(&fs_info->delalloc_roots, &splice); |
Miao Xie | 6c255e6 | 2014-03-06 13:55:01 +0800 | [diff] [blame] | 9110 | while (!list_empty(&splice) && nr) { |
Miao Xie | eb73c1b | 2013-05-15 07:48:22 +0000 | [diff] [blame] | 9111 | root = list_first_entry(&splice, struct btrfs_root, |
| 9112 | delalloc_root); |
| 9113 | root = btrfs_grab_fs_root(root); |
| 9114 | BUG_ON(!root); |
| 9115 | list_move_tail(&root->delalloc_root, |
| 9116 | &fs_info->delalloc_roots); |
| 9117 | spin_unlock(&fs_info->delalloc_root_lock); |
| 9118 | |
Miao Xie | 6c255e6 | 2014-03-06 13:55:01 +0800 | [diff] [blame] | 9119 | ret = __start_delalloc_inodes(root, delay_iput, nr); |
Miao Xie | eb73c1b | 2013-05-15 07:48:22 +0000 | [diff] [blame] | 9120 | btrfs_put_fs_root(root); |
Miao Xie | 6c255e6 | 2014-03-06 13:55:01 +0800 | [diff] [blame] | 9121 | if (ret < 0) |
Miao Xie | eb73c1b | 2013-05-15 07:48:22 +0000 | [diff] [blame] | 9122 | goto out; |
| 9123 | |
Miao Xie | 6c255e6 | 2014-03-06 13:55:01 +0800 | [diff] [blame] | 9124 | if (nr != -1) { |
| 9125 | nr -= ret; |
| 9126 | WARN_ON(nr < 0); |
| 9127 | } |
Miao Xie | eb73c1b | 2013-05-15 07:48:22 +0000 | [diff] [blame] | 9128 | spin_lock(&fs_info->delalloc_root_lock); |
| 9129 | } |
| 9130 | spin_unlock(&fs_info->delalloc_root_lock); |
| 9131 | |
Miao Xie | 6c255e6 | 2014-03-06 13:55:01 +0800 | [diff] [blame] | 9132 | ret = 0; |
Miao Xie | eb73c1b | 2013-05-15 07:48:22 +0000 | [diff] [blame] | 9133 | atomic_inc(&fs_info->async_submit_draining); |
| 9134 | while (atomic_read(&fs_info->nr_async_submits) || |
| 9135 | atomic_read(&fs_info->async_delalloc_pages)) { |
| 9136 | wait_event(fs_info->async_submit_wait, |
| 9137 | (atomic_read(&fs_info->nr_async_submits) == 0 && |
| 9138 | atomic_read(&fs_info->async_delalloc_pages) == 0)); |
| 9139 | } |
| 9140 | atomic_dec(&fs_info->async_submit_draining); |
Miao Xie | 8ccf6f19 | 2012-10-25 09:28:04 +0000 | [diff] [blame] | 9141 | out: |
Miao Xie | 1eafa6c | 2013-01-22 10:49:00 +0000 | [diff] [blame] | 9142 | if (!list_empty_careful(&splice)) { |
Miao Xie | eb73c1b | 2013-05-15 07:48:22 +0000 | [diff] [blame] | 9143 | spin_lock(&fs_info->delalloc_root_lock); |
| 9144 | list_splice_tail(&splice, &fs_info->delalloc_roots); |
| 9145 | spin_unlock(&fs_info->delalloc_root_lock); |
Miao Xie | 1eafa6c | 2013-01-22 10:49:00 +0000 | [diff] [blame] | 9146 | } |
Miao Xie | 573bfb7 | 2014-03-06 13:55:03 +0800 | [diff] [blame] | 9147 | mutex_unlock(&fs_info->delalloc_root_mutex); |
Miao Xie | 8ccf6f19 | 2012-10-25 09:28:04 +0000 | [diff] [blame] | 9148 | return ret; |
Chris Mason | ea8c281 | 2008-08-04 23:17:27 -0400 | [diff] [blame] | 9149 | } |
| 9150 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 9151 | static int btrfs_symlink(struct inode *dir, struct dentry *dentry, |
| 9152 | const char *symname) |
| 9153 | { |
| 9154 | struct btrfs_trans_handle *trans; |
| 9155 | struct btrfs_root *root = BTRFS_I(dir)->root; |
| 9156 | struct btrfs_path *path; |
| 9157 | struct btrfs_key key; |
Chris Mason | 1832a6d | 2007-12-21 16:27:21 -0500 | [diff] [blame] | 9158 | struct inode *inode = NULL; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 9159 | int err; |
| 9160 | int drop_inode = 0; |
| 9161 | u64 objectid; |
Dulshani Gunawardhana | 6787125 | 2013-10-31 10:33:04 +0530 | [diff] [blame] | 9162 | u64 index = 0; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 9163 | int name_len; |
| 9164 | int datasize; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 9165 | unsigned long ptr; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 9166 | struct btrfs_file_extent_item *ei; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 9167 | struct extent_buffer *leaf; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 9168 | |
Filipe David Borba Manana | f06becc | 2013-09-16 09:53:28 +0100 | [diff] [blame] | 9169 | name_len = strlen(symname); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 9170 | if (name_len > BTRFS_MAX_INLINE_DATA_SIZE(root)) |
| 9171 | return -ENAMETOOLONG; |
Chris Mason | 1832a6d | 2007-12-21 16:27:21 -0500 | [diff] [blame] | 9172 | |
Josef Bacik | 9ed74f2 | 2009-09-11 16:12:44 -0400 | [diff] [blame] | 9173 | /* |
| 9174 | * 2 items for inode item and ref |
| 9175 | * 2 items for dir items |
| 9176 | * 1 item for xattr if selinux is on |
| 9177 | */ |
Yan, Zheng | a22285a | 2010-05-16 10:48:46 -0400 | [diff] [blame] | 9178 | trans = btrfs_start_transaction(root, 5); |
| 9179 | if (IS_ERR(trans)) |
| 9180 | return PTR_ERR(trans); |
Chris Mason | 1832a6d | 2007-12-21 16:27:21 -0500 | [diff] [blame] | 9181 | |
Li Zefan | 581bb05 | 2011-04-20 10:06:11 +0800 | [diff] [blame] | 9182 | err = btrfs_find_free_ino(root, &objectid); |
| 9183 | if (err) |
| 9184 | goto out_unlock; |
| 9185 | |
Josef Bacik | aec7477 | 2008-07-24 12:12:38 -0400 | [diff] [blame] | 9186 | inode = btrfs_new_inode(trans, root, dir, dentry->d_name.name, |
Li Zefan | 33345d01 | 2011-04-20 10:31:50 +0800 | [diff] [blame] | 9187 | dentry->d_name.len, btrfs_ino(dir), objectid, |
Josef Bacik | d82a6f1 | 2011-05-11 15:26:06 -0400 | [diff] [blame] | 9188 | S_IFLNK|S_IRWXUGO, &index); |
Tsutomu Itoh | 7cf96da | 2011-04-25 19:43:53 -0400 | [diff] [blame] | 9189 | if (IS_ERR(inode)) { |
| 9190 | err = PTR_ERR(inode); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 9191 | goto out_unlock; |
Tsutomu Itoh | 7cf96da | 2011-04-25 19:43:53 -0400 | [diff] [blame] | 9192 | } |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 9193 | |
Casey Schaufler | ad19db7 | 2011-12-15 10:09:07 -0500 | [diff] [blame] | 9194 | /* |
| 9195 | * If the active LSM wants to access the inode during |
| 9196 | * d_instantiate it needs these. Smack checks to see |
| 9197 | * if the filesystem supports xattrs by looking at the |
| 9198 | * ops vector. |
| 9199 | */ |
| 9200 | inode->i_fop = &btrfs_file_operations; |
| 9201 | inode->i_op = &btrfs_file_inode_operations; |
Chris Mason | b0d5d10 | 2014-09-08 13:08:51 -0700 | [diff] [blame] | 9202 | inode->i_mapping->a_ops = &btrfs_aops; |
Chris Mason | b0d5d10 | 2014-09-08 13:08:51 -0700 | [diff] [blame] | 9203 | BTRFS_I(inode)->io_tree.ops = &btrfs_extent_io_ops; |
| 9204 | |
| 9205 | err = btrfs_init_inode_security(trans, inode, dir, &dentry->d_name); |
| 9206 | if (err) |
| 9207 | goto out_unlock_inode; |
Casey Schaufler | ad19db7 | 2011-12-15 10:09:07 -0500 | [diff] [blame] | 9208 | |
Josef Bacik | a1b075d | 2010-11-19 20:36:11 +0000 | [diff] [blame] | 9209 | err = btrfs_add_nondir(trans, dir, dentry, inode, 0, index); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 9210 | if (err) |
Chris Mason | b0d5d10 | 2014-09-08 13:08:51 -0700 | [diff] [blame] | 9211 | goto out_unlock_inode; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 9212 | |
| 9213 | path = btrfs_alloc_path(); |
Mark Fasheh | d8926bb | 2011-07-13 10:38:47 -0700 | [diff] [blame] | 9214 | if (!path) { |
| 9215 | err = -ENOMEM; |
Chris Mason | b0d5d10 | 2014-09-08 13:08:51 -0700 | [diff] [blame] | 9216 | goto out_unlock_inode; |
Mark Fasheh | d8926bb | 2011-07-13 10:38:47 -0700 | [diff] [blame] | 9217 | } |
Li Zefan | 33345d01 | 2011-04-20 10:31:50 +0800 | [diff] [blame] | 9218 | key.objectid = btrfs_ino(inode); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 9219 | key.offset = 0; |
David Sterba | 962a298 | 2014-06-04 18:41:45 +0200 | [diff] [blame] | 9220 | key.type = BTRFS_EXTENT_DATA_KEY; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 9221 | datasize = btrfs_file_extent_calc_inline_size(name_len); |
| 9222 | err = btrfs_insert_empty_item(trans, root, path, &key, |
| 9223 | datasize); |
Chris Mason | 54aa1f4 | 2007-06-22 14:16:25 -0400 | [diff] [blame] | 9224 | if (err) { |
Julia Lawall | b083916 | 2011-05-14 07:10:51 +0000 | [diff] [blame] | 9225 | btrfs_free_path(path); |
Chris Mason | b0d5d10 | 2014-09-08 13:08:51 -0700 | [diff] [blame] | 9226 | goto out_unlock_inode; |
Chris Mason | 54aa1f4 | 2007-06-22 14:16:25 -0400 | [diff] [blame] | 9227 | } |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 9228 | leaf = path->nodes[0]; |
| 9229 | ei = btrfs_item_ptr(leaf, path->slots[0], |
| 9230 | struct btrfs_file_extent_item); |
| 9231 | btrfs_set_file_extent_generation(leaf, ei, trans->transid); |
| 9232 | btrfs_set_file_extent_type(leaf, ei, |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 9233 | BTRFS_FILE_EXTENT_INLINE); |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 9234 | btrfs_set_file_extent_encryption(leaf, ei, 0); |
| 9235 | btrfs_set_file_extent_compression(leaf, ei, 0); |
| 9236 | btrfs_set_file_extent_other_encoding(leaf, ei, 0); |
| 9237 | btrfs_set_file_extent_ram_bytes(leaf, ei, name_len); |
| 9238 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 9239 | ptr = btrfs_file_extent_inline_start(ei); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 9240 | write_extent_buffer(leaf, symname, ptr, name_len); |
| 9241 | btrfs_mark_buffer_dirty(leaf); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 9242 | btrfs_free_path(path); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 9243 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 9244 | inode->i_op = &btrfs_symlink_inode_operations; |
| 9245 | inode->i_mapping->a_ops = &btrfs_symlink_aops; |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 9246 | inode_set_bytes(inode, name_len); |
Filipe David Borba Manana | f06becc | 2013-09-16 09:53:28 +0100 | [diff] [blame] | 9247 | btrfs_i_size_write(inode, name_len); |
Chris Mason | 54aa1f4 | 2007-06-22 14:16:25 -0400 | [diff] [blame] | 9248 | err = btrfs_update_inode(trans, root, inode); |
Chris Mason | b0d5d10 | 2014-09-08 13:08:51 -0700 | [diff] [blame] | 9249 | if (err) { |
Chris Mason | 54aa1f4 | 2007-06-22 14:16:25 -0400 | [diff] [blame] | 9250 | drop_inode = 1; |
Chris Mason | b0d5d10 | 2014-09-08 13:08:51 -0700 | [diff] [blame] | 9251 | goto out_unlock_inode; |
| 9252 | } |
| 9253 | |
| 9254 | unlock_new_inode(inode); |
| 9255 | d_instantiate(dentry, inode); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 9256 | |
| 9257 | out_unlock: |
Josef Bacik | 7ad85bb | 2012-01-12 19:10:12 -0500 | [diff] [blame] | 9258 | btrfs_end_transaction(trans, root); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 9259 | if (drop_inode) { |
| 9260 | inode_dec_link_count(inode); |
| 9261 | iput(inode); |
| 9262 | } |
Liu Bo | b53d3f5 | 2012-11-14 14:34:34 +0000 | [diff] [blame] | 9263 | btrfs_btree_balance_dirty(root); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 9264 | return err; |
Chris Mason | b0d5d10 | 2014-09-08 13:08:51 -0700 | [diff] [blame] | 9265 | |
| 9266 | out_unlock_inode: |
| 9267 | drop_inode = 1; |
| 9268 | unlock_new_inode(inode); |
| 9269 | goto out_unlock; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 9270 | } |
Chris Mason | 1643298 | 2008-04-10 10:23:21 -0400 | [diff] [blame] | 9271 | |
Josef Bacik | 0af3d00 | 2010-06-21 14:48:16 -0400 | [diff] [blame] | 9272 | static int __btrfs_prealloc_file_range(struct inode *inode, int mode, |
| 9273 | u64 start, u64 num_bytes, u64 min_size, |
| 9274 | loff_t actual_len, u64 *alloc_hint, |
| 9275 | struct btrfs_trans_handle *trans) |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 9276 | { |
Josef Bacik | 5dc562c | 2012-08-17 13:14:17 -0400 | [diff] [blame] | 9277 | struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree; |
| 9278 | struct extent_map *em; |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 9279 | struct btrfs_root *root = BTRFS_I(inode)->root; |
| 9280 | struct btrfs_key ins; |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 9281 | u64 cur_offset = start; |
Josef Bacik | 55a61d1 | 2010-11-22 18:50:32 +0000 | [diff] [blame] | 9282 | u64 i_size; |
Chris Mason | 154ea28 | 2013-03-05 11:11:26 -0500 | [diff] [blame] | 9283 | u64 cur_bytes; |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 9284 | int ret = 0; |
Josef Bacik | 0af3d00 | 2010-06-21 14:48:16 -0400 | [diff] [blame] | 9285 | bool own_trans = true; |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 9286 | |
Josef Bacik | 0af3d00 | 2010-06-21 14:48:16 -0400 | [diff] [blame] | 9287 | if (trans) |
| 9288 | own_trans = false; |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 9289 | while (num_bytes > 0) { |
Josef Bacik | 0af3d00 | 2010-06-21 14:48:16 -0400 | [diff] [blame] | 9290 | if (own_trans) { |
| 9291 | trans = btrfs_start_transaction(root, 3); |
| 9292 | if (IS_ERR(trans)) { |
| 9293 | ret = PTR_ERR(trans); |
| 9294 | break; |
| 9295 | } |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 9296 | } |
Yan, Zheng | 5a303d5 | 2009-11-12 09:34:52 +0000 | [diff] [blame] | 9297 | |
Chris Mason | 154ea28 | 2013-03-05 11:11:26 -0500 | [diff] [blame] | 9298 | cur_bytes = min(num_bytes, 256ULL * 1024 * 1024); |
| 9299 | cur_bytes = max(cur_bytes, min_size); |
Josef Bacik | 0036158 | 2013-08-14 14:02:47 -0400 | [diff] [blame] | 9300 | ret = btrfs_reserve_extent(root, cur_bytes, min_size, 0, |
Miao Xie | e570fd2 | 2014-06-19 10:42:50 +0800 | [diff] [blame] | 9301 | *alloc_hint, &ins, 1, 0); |
Yan, Zheng | 5a303d5 | 2009-11-12 09:34:52 +0000 | [diff] [blame] | 9302 | if (ret) { |
Josef Bacik | 0af3d00 | 2010-06-21 14:48:16 -0400 | [diff] [blame] | 9303 | if (own_trans) |
| 9304 | btrfs_end_transaction(trans, root); |
Yan, Zheng | a22285a | 2010-05-16 10:48:46 -0400 | [diff] [blame] | 9305 | break; |
Yan, Zheng | 5a303d5 | 2009-11-12 09:34:52 +0000 | [diff] [blame] | 9306 | } |
| 9307 | |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 9308 | ret = insert_reserved_file_extent(trans, inode, |
| 9309 | cur_offset, ins.objectid, |
| 9310 | ins.offset, ins.offset, |
Yan, Zheng | 920bbbf | 2009-11-12 09:34:08 +0000 | [diff] [blame] | 9311 | ins.offset, 0, 0, 0, |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 9312 | BTRFS_FILE_EXTENT_PREALLOC); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 9313 | if (ret) { |
Josef Bacik | 857cc2f | 2013-10-07 15:21:08 -0400 | [diff] [blame] | 9314 | btrfs_free_reserved_extent(root, ins.objectid, |
Miao Xie | e570fd2 | 2014-06-19 10:42:50 +0800 | [diff] [blame] | 9315 | ins.offset, 0); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 9316 | btrfs_abort_transaction(trans, root, ret); |
| 9317 | if (own_trans) |
| 9318 | btrfs_end_transaction(trans, root); |
| 9319 | break; |
| 9320 | } |
Chris Mason | a1ed835 | 2009-09-11 12:27:37 -0400 | [diff] [blame] | 9321 | btrfs_drop_extent_cache(inode, cur_offset, |
| 9322 | cur_offset + ins.offset -1, 0); |
Yan, Zheng | 5a303d5 | 2009-11-12 09:34:52 +0000 | [diff] [blame] | 9323 | |
Josef Bacik | 5dc562c | 2012-08-17 13:14:17 -0400 | [diff] [blame] | 9324 | em = alloc_extent_map(); |
| 9325 | if (!em) { |
| 9326 | set_bit(BTRFS_INODE_NEEDS_FULL_SYNC, |
| 9327 | &BTRFS_I(inode)->runtime_flags); |
| 9328 | goto next; |
| 9329 | } |
| 9330 | |
| 9331 | em->start = cur_offset; |
| 9332 | em->orig_start = cur_offset; |
| 9333 | em->len = ins.offset; |
| 9334 | em->block_start = ins.objectid; |
| 9335 | em->block_len = ins.offset; |
Josef Bacik | b493968 | 2012-12-03 10:31:19 -0500 | [diff] [blame] | 9336 | em->orig_block_len = ins.offset; |
Josef Bacik | cc95bef | 2013-04-04 14:31:27 -0400 | [diff] [blame] | 9337 | em->ram_bytes = ins.offset; |
Josef Bacik | 5dc562c | 2012-08-17 13:14:17 -0400 | [diff] [blame] | 9338 | em->bdev = root->fs_info->fs_devices->latest_bdev; |
| 9339 | set_bit(EXTENT_FLAG_PREALLOC, &em->flags); |
| 9340 | em->generation = trans->transid; |
| 9341 | |
| 9342 | while (1) { |
| 9343 | write_lock(&em_tree->lock); |
Josef Bacik | 09a2a8f9 | 2013-04-05 16:51:15 -0400 | [diff] [blame] | 9344 | ret = add_extent_mapping(em_tree, em, 1); |
Josef Bacik | 5dc562c | 2012-08-17 13:14:17 -0400 | [diff] [blame] | 9345 | write_unlock(&em_tree->lock); |
| 9346 | if (ret != -EEXIST) |
| 9347 | break; |
| 9348 | btrfs_drop_extent_cache(inode, cur_offset, |
| 9349 | cur_offset + ins.offset - 1, |
| 9350 | 0); |
| 9351 | } |
| 9352 | free_extent_map(em); |
| 9353 | next: |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 9354 | num_bytes -= ins.offset; |
| 9355 | cur_offset += ins.offset; |
Yan, Zheng | efa5646 | 2010-05-16 10:49:59 -0400 | [diff] [blame] | 9356 | *alloc_hint = ins.objectid + ins.offset; |
Yan, Zheng | 5a303d5 | 2009-11-12 09:34:52 +0000 | [diff] [blame] | 9357 | |
Josef Bacik | 0c4d2d9 | 2012-04-05 15:03:02 -0400 | [diff] [blame] | 9358 | inode_inc_iversion(inode); |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 9359 | inode->i_ctime = CURRENT_TIME; |
Christoph Hellwig | 6cbff00 | 2009-04-17 10:37:41 +0200 | [diff] [blame] | 9360 | BTRFS_I(inode)->flags |= BTRFS_INODE_PREALLOC; |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 9361 | if (!(mode & FALLOC_FL_KEEP_SIZE) && |
Yan, Zheng | efa5646 | 2010-05-16 10:49:59 -0400 | [diff] [blame] | 9362 | (actual_len > inode->i_size) && |
| 9363 | (cur_offset > inode->i_size)) { |
Aneesh Kumar K.V | d1ea6a6 | 2010-01-20 07:28:54 +0000 | [diff] [blame] | 9364 | if (cur_offset > actual_len) |
Josef Bacik | 55a61d1 | 2010-11-22 18:50:32 +0000 | [diff] [blame] | 9365 | i_size = actual_len; |
Aneesh Kumar K.V | d1ea6a6 | 2010-01-20 07:28:54 +0000 | [diff] [blame] | 9366 | else |
Josef Bacik | 55a61d1 | 2010-11-22 18:50:32 +0000 | [diff] [blame] | 9367 | i_size = cur_offset; |
| 9368 | i_size_write(inode, i_size); |
| 9369 | btrfs_ordered_update_i_size(inode, i_size, NULL); |
Yan, Zheng | 5a303d5 | 2009-11-12 09:34:52 +0000 | [diff] [blame] | 9370 | } |
| 9371 | |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 9372 | ret = btrfs_update_inode(trans, root, inode); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 9373 | |
| 9374 | if (ret) { |
| 9375 | btrfs_abort_transaction(trans, root, ret); |
| 9376 | if (own_trans) |
| 9377 | btrfs_end_transaction(trans, root); |
| 9378 | break; |
| 9379 | } |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 9380 | |
Josef Bacik | 0af3d00 | 2010-06-21 14:48:16 -0400 | [diff] [blame] | 9381 | if (own_trans) |
| 9382 | btrfs_end_transaction(trans, root); |
Yan, Zheng | 5a303d5 | 2009-11-12 09:34:52 +0000 | [diff] [blame] | 9383 | } |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 9384 | return ret; |
| 9385 | } |
| 9386 | |
Josef Bacik | 0af3d00 | 2010-06-21 14:48:16 -0400 | [diff] [blame] | 9387 | int btrfs_prealloc_file_range(struct inode *inode, int mode, |
| 9388 | u64 start, u64 num_bytes, u64 min_size, |
| 9389 | loff_t actual_len, u64 *alloc_hint) |
| 9390 | { |
| 9391 | return __btrfs_prealloc_file_range(inode, mode, start, num_bytes, |
| 9392 | min_size, actual_len, alloc_hint, |
| 9393 | NULL); |
| 9394 | } |
| 9395 | |
| 9396 | int btrfs_prealloc_file_range_trans(struct inode *inode, |
| 9397 | struct btrfs_trans_handle *trans, int mode, |
| 9398 | u64 start, u64 num_bytes, u64 min_size, |
| 9399 | loff_t actual_len, u64 *alloc_hint) |
| 9400 | { |
| 9401 | return __btrfs_prealloc_file_range(inode, mode, start, num_bytes, |
| 9402 | min_size, actual_len, alloc_hint, trans); |
| 9403 | } |
| 9404 | |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 9405 | static int btrfs_set_page_dirty(struct page *page) |
| 9406 | { |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 9407 | return __set_page_dirty_nobuffers(page); |
| 9408 | } |
| 9409 | |
Al Viro | 10556cb | 2011-06-20 19:28:19 -0400 | [diff] [blame] | 9410 | static int btrfs_permission(struct inode *inode, int mask) |
Yan | fdebe2b | 2008-01-14 13:26:08 -0500 | [diff] [blame] | 9411 | { |
Li Zefan | b83cc96 | 2010-12-20 16:04:08 +0800 | [diff] [blame] | 9412 | struct btrfs_root *root = BTRFS_I(inode)->root; |
Jeff Mahoney | cb6db4e | 2011-08-15 17:27:21 +0000 | [diff] [blame] | 9413 | umode_t mode = inode->i_mode; |
Li Zefan | b83cc96 | 2010-12-20 16:04:08 +0800 | [diff] [blame] | 9414 | |
Jeff Mahoney | cb6db4e | 2011-08-15 17:27:21 +0000 | [diff] [blame] | 9415 | if (mask & MAY_WRITE && |
| 9416 | (S_ISREG(mode) || S_ISDIR(mode) || S_ISLNK(mode))) { |
| 9417 | if (btrfs_root_readonly(root)) |
| 9418 | return -EROFS; |
| 9419 | if (BTRFS_I(inode)->flags & BTRFS_INODE_READONLY) |
| 9420 | return -EACCES; |
| 9421 | } |
Al Viro | 2830ba7 | 2011-06-20 19:16:29 -0400 | [diff] [blame] | 9422 | return generic_permission(inode, mask); |
Yan | fdebe2b | 2008-01-14 13:26:08 -0500 | [diff] [blame] | 9423 | } |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 9424 | |
Filipe Manana | ef3b9af | 2014-04-27 20:40:45 +0100 | [diff] [blame] | 9425 | static int btrfs_tmpfile(struct inode *dir, struct dentry *dentry, umode_t mode) |
| 9426 | { |
| 9427 | struct btrfs_trans_handle *trans; |
| 9428 | struct btrfs_root *root = BTRFS_I(dir)->root; |
| 9429 | struct inode *inode = NULL; |
| 9430 | u64 objectid; |
| 9431 | u64 index; |
| 9432 | int ret = 0; |
| 9433 | |
| 9434 | /* |
| 9435 | * 5 units required for adding orphan entry |
| 9436 | */ |
| 9437 | trans = btrfs_start_transaction(root, 5); |
| 9438 | if (IS_ERR(trans)) |
| 9439 | return PTR_ERR(trans); |
| 9440 | |
| 9441 | ret = btrfs_find_free_ino(root, &objectid); |
| 9442 | if (ret) |
| 9443 | goto out; |
| 9444 | |
| 9445 | inode = btrfs_new_inode(trans, root, dir, NULL, 0, |
| 9446 | btrfs_ino(dir), objectid, mode, &index); |
| 9447 | if (IS_ERR(inode)) { |
| 9448 | ret = PTR_ERR(inode); |
| 9449 | inode = NULL; |
| 9450 | goto out; |
| 9451 | } |
| 9452 | |
Filipe Manana | ef3b9af | 2014-04-27 20:40:45 +0100 | [diff] [blame] | 9453 | inode->i_fop = &btrfs_file_operations; |
| 9454 | inode->i_op = &btrfs_file_inode_operations; |
| 9455 | |
| 9456 | inode->i_mapping->a_ops = &btrfs_aops; |
Filipe Manana | ef3b9af | 2014-04-27 20:40:45 +0100 | [diff] [blame] | 9457 | BTRFS_I(inode)->io_tree.ops = &btrfs_extent_io_ops; |
| 9458 | |
Chris Mason | b0d5d10 | 2014-09-08 13:08:51 -0700 | [diff] [blame] | 9459 | ret = btrfs_init_inode_security(trans, inode, dir, NULL); |
| 9460 | if (ret) |
| 9461 | goto out_inode; |
| 9462 | |
| 9463 | ret = btrfs_update_inode(trans, root, inode); |
| 9464 | if (ret) |
| 9465 | goto out_inode; |
Filipe Manana | ef3b9af | 2014-04-27 20:40:45 +0100 | [diff] [blame] | 9466 | ret = btrfs_orphan_add(trans, inode); |
| 9467 | if (ret) |
Chris Mason | b0d5d10 | 2014-09-08 13:08:51 -0700 | [diff] [blame] | 9468 | goto out_inode; |
Filipe Manana | ef3b9af | 2014-04-27 20:40:45 +0100 | [diff] [blame] | 9469 | |
Filipe Manana | 5762b5c | 2014-08-01 00:10:32 +0100 | [diff] [blame] | 9470 | /* |
| 9471 | * We set number of links to 0 in btrfs_new_inode(), and here we set |
| 9472 | * it to 1 because d_tmpfile() will issue a warning if the count is 0, |
| 9473 | * through: |
| 9474 | * |
| 9475 | * d_tmpfile() -> inode_dec_link_count() -> drop_nlink() |
| 9476 | */ |
| 9477 | set_nlink(inode, 1); |
Chris Mason | b0d5d10 | 2014-09-08 13:08:51 -0700 | [diff] [blame] | 9478 | unlock_new_inode(inode); |
Filipe Manana | ef3b9af | 2014-04-27 20:40:45 +0100 | [diff] [blame] | 9479 | d_tmpfile(dentry, inode); |
| 9480 | mark_inode_dirty(inode); |
| 9481 | |
| 9482 | out: |
| 9483 | btrfs_end_transaction(trans, root); |
| 9484 | if (ret) |
| 9485 | iput(inode); |
| 9486 | btrfs_balance_delayed_items(root); |
| 9487 | btrfs_btree_balance_dirty(root); |
Filipe Manana | ef3b9af | 2014-04-27 20:40:45 +0100 | [diff] [blame] | 9488 | return ret; |
Chris Mason | b0d5d10 | 2014-09-08 13:08:51 -0700 | [diff] [blame] | 9489 | |
| 9490 | out_inode: |
| 9491 | unlock_new_inode(inode); |
| 9492 | goto out; |
| 9493 | |
Filipe Manana | ef3b9af | 2014-04-27 20:40:45 +0100 | [diff] [blame] | 9494 | } |
| 9495 | |
Filipe Manana | b38ef71 | 2014-11-13 17:01:45 +0000 | [diff] [blame] | 9496 | /* Inspired by filemap_check_errors() */ |
| 9497 | int btrfs_inode_check_errors(struct inode *inode) |
| 9498 | { |
| 9499 | int ret = 0; |
| 9500 | |
| 9501 | if (test_bit(AS_ENOSPC, &inode->i_mapping->flags) && |
| 9502 | test_and_clear_bit(AS_ENOSPC, &inode->i_mapping->flags)) |
| 9503 | ret = -ENOSPC; |
| 9504 | if (test_bit(AS_EIO, &inode->i_mapping->flags) && |
| 9505 | test_and_clear_bit(AS_EIO, &inode->i_mapping->flags)) |
| 9506 | ret = -EIO; |
| 9507 | |
| 9508 | return ret; |
| 9509 | } |
| 9510 | |
Alexey Dobriyan | 6e1d5dc | 2009-09-21 17:01:11 -0700 | [diff] [blame] | 9511 | static const struct inode_operations btrfs_dir_inode_operations = { |
Chris Mason | 3394e16 | 2008-11-17 20:42:26 -0500 | [diff] [blame] | 9512 | .getattr = btrfs_getattr, |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 9513 | .lookup = btrfs_lookup, |
| 9514 | .create = btrfs_create, |
| 9515 | .unlink = btrfs_unlink, |
| 9516 | .link = btrfs_link, |
| 9517 | .mkdir = btrfs_mkdir, |
| 9518 | .rmdir = btrfs_rmdir, |
Miklos Szeredi | 80ace85 | 2014-07-23 15:15:32 +0200 | [diff] [blame] | 9519 | .rename2 = btrfs_rename2, |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 9520 | .symlink = btrfs_symlink, |
| 9521 | .setattr = btrfs_setattr, |
Josef Bacik | 618e21d | 2007-07-11 10:18:17 -0400 | [diff] [blame] | 9522 | .mknod = btrfs_mknod, |
Christoph Hellwig | 95819c0 | 2008-08-28 06:21:17 -0400 | [diff] [blame] | 9523 | .setxattr = btrfs_setxattr, |
| 9524 | .getxattr = btrfs_getxattr, |
Josef Bacik | 5103e94 | 2007-11-16 11:45:54 -0500 | [diff] [blame] | 9525 | .listxattr = btrfs_listxattr, |
Christoph Hellwig | 95819c0 | 2008-08-28 06:21:17 -0400 | [diff] [blame] | 9526 | .removexattr = btrfs_removexattr, |
Yan | fdebe2b | 2008-01-14 13:26:08 -0500 | [diff] [blame] | 9527 | .permission = btrfs_permission, |
Christoph Hellwig | 4e34e71 | 2011-07-23 17:37:31 +0200 | [diff] [blame] | 9528 | .get_acl = btrfs_get_acl, |
Christoph Hellwig | 996a710 | 2013-12-20 05:16:43 -0800 | [diff] [blame] | 9529 | .set_acl = btrfs_set_acl, |
Guangyu Sun | 93fd63c | 2013-09-16 10:42:03 -0700 | [diff] [blame] | 9530 | .update_time = btrfs_update_time, |
Filipe Manana | ef3b9af | 2014-04-27 20:40:45 +0100 | [diff] [blame] | 9531 | .tmpfile = btrfs_tmpfile, |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 9532 | }; |
Alexey Dobriyan | 6e1d5dc | 2009-09-21 17:01:11 -0700 | [diff] [blame] | 9533 | static const struct inode_operations btrfs_dir_ro_inode_operations = { |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 9534 | .lookup = btrfs_lookup, |
Yan | fdebe2b | 2008-01-14 13:26:08 -0500 | [diff] [blame] | 9535 | .permission = btrfs_permission, |
Christoph Hellwig | 4e34e71 | 2011-07-23 17:37:31 +0200 | [diff] [blame] | 9536 | .get_acl = btrfs_get_acl, |
Christoph Hellwig | 996a710 | 2013-12-20 05:16:43 -0800 | [diff] [blame] | 9537 | .set_acl = btrfs_set_acl, |
Guangyu Sun | 93fd63c | 2013-09-16 10:42:03 -0700 | [diff] [blame] | 9538 | .update_time = btrfs_update_time, |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 9539 | }; |
Yan, Zheng | 76dda93 | 2009-09-21 16:00:26 -0400 | [diff] [blame] | 9540 | |
Alexey Dobriyan | 828c095 | 2009-10-01 15:43:56 -0700 | [diff] [blame] | 9541 | static const struct file_operations btrfs_dir_file_operations = { |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 9542 | .llseek = generic_file_llseek, |
| 9543 | .read = generic_read_dir, |
Al Viro | 9cdda8d | 2013-05-22 16:48:09 -0400 | [diff] [blame] | 9544 | .iterate = btrfs_real_readdir, |
Christoph Hellwig | 34287aa | 2007-09-14 10:22:47 -0400 | [diff] [blame] | 9545 | .unlocked_ioctl = btrfs_ioctl, |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 9546 | #ifdef CONFIG_COMPAT |
Christoph Hellwig | 34287aa | 2007-09-14 10:22:47 -0400 | [diff] [blame] | 9547 | .compat_ioctl = btrfs_ioctl, |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 9548 | #endif |
Sage Weil | 6bf13c0 | 2008-06-10 10:07:39 -0400 | [diff] [blame] | 9549 | .release = btrfs_release_file, |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 9550 | .fsync = btrfs_sync_file, |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 9551 | }; |
| 9552 | |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 9553 | static struct extent_io_ops btrfs_extent_io_ops = { |
Chris Mason | 07157aa | 2007-08-30 08:50:51 -0400 | [diff] [blame] | 9554 | .fill_delalloc = run_delalloc_range, |
Chris Mason | 065631f | 2008-02-20 12:07:25 -0500 | [diff] [blame] | 9555 | .submit_bio_hook = btrfs_submit_bio_hook, |
Chris Mason | 239b14b | 2008-03-24 15:02:07 -0400 | [diff] [blame] | 9556 | .merge_bio_hook = btrfs_merge_bio_hook, |
Chris Mason | 07157aa | 2007-08-30 08:50:51 -0400 | [diff] [blame] | 9557 | .readpage_end_io_hook = btrfs_readpage_end_io_hook, |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 9558 | .writepage_end_io_hook = btrfs_writepage_end_io_hook, |
Chris Mason | 247e743 | 2008-07-17 12:53:51 -0400 | [diff] [blame] | 9559 | .writepage_start_hook = btrfs_writepage_start_hook, |
Chris Mason | b0c68f8 | 2008-01-31 11:05:37 -0500 | [diff] [blame] | 9560 | .set_bit_hook = btrfs_set_bit_hook, |
| 9561 | .clear_bit_hook = btrfs_clear_bit_hook, |
Josef Bacik | 9ed74f2 | 2009-09-11 16:12:44 -0400 | [diff] [blame] | 9562 | .merge_extent_hook = btrfs_merge_extent_hook, |
| 9563 | .split_extent_hook = btrfs_split_extent_hook, |
Chris Mason | 07157aa | 2007-08-30 08:50:51 -0400 | [diff] [blame] | 9564 | }; |
| 9565 | |
Chris Mason | 3505439 | 2009-01-21 13:11:13 -0500 | [diff] [blame] | 9566 | /* |
| 9567 | * btrfs doesn't support the bmap operation because swapfiles |
| 9568 | * use bmap to make a mapping of extents in the file. They assume |
| 9569 | * these extents won't change over the life of the file and they |
| 9570 | * use the bmap result to do IO directly to the drive. |
| 9571 | * |
| 9572 | * the btrfs bmap call would return logical addresses that aren't |
| 9573 | * suitable for IO and they also will change frequently as COW |
| 9574 | * operations happen. So, swapfile + btrfs == corruption. |
| 9575 | * |
| 9576 | * For now we're avoiding this by dropping bmap. |
| 9577 | */ |
Alexey Dobriyan | 7f09410 | 2009-09-21 17:01:10 -0700 | [diff] [blame] | 9578 | static const struct address_space_operations btrfs_aops = { |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 9579 | .readpage = btrfs_readpage, |
| 9580 | .writepage = btrfs_writepage, |
Chris Mason | b293f02 | 2007-11-01 19:45:34 -0400 | [diff] [blame] | 9581 | .writepages = btrfs_writepages, |
Chris Mason | 3ab2fb5 | 2007-11-08 10:59:22 -0500 | [diff] [blame] | 9582 | .readpages = btrfs_readpages, |
Chris Mason | 1643298 | 2008-04-10 10:23:21 -0400 | [diff] [blame] | 9583 | .direct_IO = btrfs_direct_IO, |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 9584 | .invalidatepage = btrfs_invalidatepage, |
| 9585 | .releasepage = btrfs_releasepage, |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 9586 | .set_page_dirty = btrfs_set_page_dirty, |
Andi Kleen | 465fdd9 | 2009-09-16 11:50:18 +0200 | [diff] [blame] | 9587 | .error_remove_page = generic_error_remove_page, |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 9588 | }; |
| 9589 | |
Alexey Dobriyan | 7f09410 | 2009-09-21 17:01:10 -0700 | [diff] [blame] | 9590 | static const struct address_space_operations btrfs_symlink_aops = { |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 9591 | .readpage = btrfs_readpage, |
| 9592 | .writepage = btrfs_writepage, |
Chris Mason | 2bf5a72 | 2007-08-30 11:54:02 -0400 | [diff] [blame] | 9593 | .invalidatepage = btrfs_invalidatepage, |
| 9594 | .releasepage = btrfs_releasepage, |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 9595 | }; |
| 9596 | |
Alexey Dobriyan | 6e1d5dc | 2009-09-21 17:01:11 -0700 | [diff] [blame] | 9597 | static const struct inode_operations btrfs_file_inode_operations = { |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 9598 | .getattr = btrfs_getattr, |
| 9599 | .setattr = btrfs_setattr, |
Christoph Hellwig | 95819c0 | 2008-08-28 06:21:17 -0400 | [diff] [blame] | 9600 | .setxattr = btrfs_setxattr, |
| 9601 | .getxattr = btrfs_getxattr, |
Josef Bacik | 5103e94 | 2007-11-16 11:45:54 -0500 | [diff] [blame] | 9602 | .listxattr = btrfs_listxattr, |
Christoph Hellwig | 95819c0 | 2008-08-28 06:21:17 -0400 | [diff] [blame] | 9603 | .removexattr = btrfs_removexattr, |
Yan | fdebe2b | 2008-01-14 13:26:08 -0500 | [diff] [blame] | 9604 | .permission = btrfs_permission, |
Yehuda Sadeh | 1506fcc | 2009-01-21 14:39:14 -0500 | [diff] [blame] | 9605 | .fiemap = btrfs_fiemap, |
Christoph Hellwig | 4e34e71 | 2011-07-23 17:37:31 +0200 | [diff] [blame] | 9606 | .get_acl = btrfs_get_acl, |
Christoph Hellwig | 996a710 | 2013-12-20 05:16:43 -0800 | [diff] [blame] | 9607 | .set_acl = btrfs_set_acl, |
Josef Bacik | e41f941 | 2012-03-26 09:46:47 -0400 | [diff] [blame] | 9608 | .update_time = btrfs_update_time, |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 9609 | }; |
Alexey Dobriyan | 6e1d5dc | 2009-09-21 17:01:11 -0700 | [diff] [blame] | 9610 | static const struct inode_operations btrfs_special_inode_operations = { |
Josef Bacik | 618e21d | 2007-07-11 10:18:17 -0400 | [diff] [blame] | 9611 | .getattr = btrfs_getattr, |
| 9612 | .setattr = btrfs_setattr, |
Yan | fdebe2b | 2008-01-14 13:26:08 -0500 | [diff] [blame] | 9613 | .permission = btrfs_permission, |
Christoph Hellwig | 95819c0 | 2008-08-28 06:21:17 -0400 | [diff] [blame] | 9614 | .setxattr = btrfs_setxattr, |
| 9615 | .getxattr = btrfs_getxattr, |
Josef Bacik | 33268ea | 2008-07-24 12:16:36 -0400 | [diff] [blame] | 9616 | .listxattr = btrfs_listxattr, |
Christoph Hellwig | 95819c0 | 2008-08-28 06:21:17 -0400 | [diff] [blame] | 9617 | .removexattr = btrfs_removexattr, |
Christoph Hellwig | 4e34e71 | 2011-07-23 17:37:31 +0200 | [diff] [blame] | 9618 | .get_acl = btrfs_get_acl, |
Christoph Hellwig | 996a710 | 2013-12-20 05:16:43 -0800 | [diff] [blame] | 9619 | .set_acl = btrfs_set_acl, |
Josef Bacik | e41f941 | 2012-03-26 09:46:47 -0400 | [diff] [blame] | 9620 | .update_time = btrfs_update_time, |
Josef Bacik | 618e21d | 2007-07-11 10:18:17 -0400 | [diff] [blame] | 9621 | }; |
Alexey Dobriyan | 6e1d5dc | 2009-09-21 17:01:11 -0700 | [diff] [blame] | 9622 | static const struct inode_operations btrfs_symlink_inode_operations = { |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 9623 | .readlink = generic_readlink, |
| 9624 | .follow_link = page_follow_link_light, |
| 9625 | .put_link = page_put_link, |
Li Zefan | f209561 | 2010-11-19 02:05:24 +0000 | [diff] [blame] | 9626 | .getattr = btrfs_getattr, |
Josef Bacik | 22c44fe | 2011-11-30 10:45:38 -0500 | [diff] [blame] | 9627 | .setattr = btrfs_setattr, |
Yan | fdebe2b | 2008-01-14 13:26:08 -0500 | [diff] [blame] | 9628 | .permission = btrfs_permission, |
Jim Owens | 0279b4c | 2009-02-04 09:29:13 -0500 | [diff] [blame] | 9629 | .setxattr = btrfs_setxattr, |
| 9630 | .getxattr = btrfs_getxattr, |
| 9631 | .listxattr = btrfs_listxattr, |
| 9632 | .removexattr = btrfs_removexattr, |
Josef Bacik | e41f941 | 2012-03-26 09:46:47 -0400 | [diff] [blame] | 9633 | .update_time = btrfs_update_time, |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 9634 | }; |
Yan, Zheng | 76dda93 | 2009-09-21 16:00:26 -0400 | [diff] [blame] | 9635 | |
Alexey Dobriyan | 82d339d | 2009-10-09 09:54:36 -0400 | [diff] [blame] | 9636 | const struct dentry_operations btrfs_dentry_operations = { |
Yan, Zheng | 76dda93 | 2009-09-21 16:00:26 -0400 | [diff] [blame] | 9637 | .d_delete = btrfs_dentry_delete, |
Josef Bacik | b4aff1f | 2011-06-28 16:18:59 -0400 | [diff] [blame] | 9638 | .d_release = btrfs_dentry_release, |
Yan, Zheng | 76dda93 | 2009-09-21 16:00:26 -0400 | [diff] [blame] | 9639 | }; |