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 | 4b4e25f | 2008-11-20 10:22:27 -0500 | [diff] [blame] | 46 | #include "compat.h" |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 47 | #include "ctree.h" |
| 48 | #include "disk-io.h" |
| 49 | #include "transaction.h" |
| 50 | #include "btrfs_inode.h" |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 51 | #include "print-tree.h" |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 52 | #include "ordered-data.h" |
Christoph Hellwig | 95819c0 | 2008-08-28 06:21:17 -0400 | [diff] [blame] | 53 | #include "xattr.h" |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 54 | #include "tree-log.h" |
Jan Schmidt | 4a54c8c | 2011-07-22 15:41:52 +0200 | [diff] [blame] | 55 | #include "volumes.h" |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 56 | #include "compression.h" |
Chris Mason | b4ce94d | 2009-02-04 09:25:08 -0500 | [diff] [blame] | 57 | #include "locking.h" |
Josef Bacik | dc89e98 | 2011-01-28 17:05:48 -0500 | [diff] [blame] | 58 | #include "free-space-cache.h" |
Li Zefan | 581bb05 | 2011-04-20 10:06:11 +0800 | [diff] [blame] | 59 | #include "inode-map.h" |
Liu Bo | 38c227d | 2013-01-29 03:18:40 +0000 | [diff] [blame] | 60 | #include "backref.h" |
Josef Bacik | f23b5a5 | 2013-06-19 10:16:26 -0400 | [diff] [blame] | 61 | #include "hash.h" |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 62 | |
| 63 | struct btrfs_iget_args { |
| 64 | u64 ino; |
| 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 | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 128 | static noinline int insert_inline_extent(struct btrfs_trans_handle *trans, |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 129 | struct btrfs_root *root, struct inode *inode, |
| 130 | u64 start, size_t size, size_t compressed_size, |
Li Zefan | fe3f566 | 2011-03-28 08:30:38 +0000 | [diff] [blame] | 131 | int compress_type, |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 132 | struct page **compressed_pages) |
| 133 | { |
| 134 | struct btrfs_key key; |
| 135 | struct btrfs_path *path; |
| 136 | struct extent_buffer *leaf; |
| 137 | struct page *page = NULL; |
| 138 | char *kaddr; |
| 139 | unsigned long ptr; |
| 140 | struct btrfs_file_extent_item *ei; |
| 141 | int err = 0; |
| 142 | int ret; |
| 143 | size_t cur_size = size; |
| 144 | size_t datasize; |
| 145 | unsigned long offset; |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 146 | |
Li Zefan | fe3f566 | 2011-03-28 08:30:38 +0000 | [diff] [blame] | 147 | if (compressed_size && compressed_pages) |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 148 | cur_size = compressed_size; |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 149 | |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 150 | path = btrfs_alloc_path(); |
| 151 | if (!path) |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 152 | return -ENOMEM; |
| 153 | |
Chris Mason | b947343 | 2009-03-13 11:00:37 -0400 | [diff] [blame] | 154 | path->leave_spinning = 1; |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 155 | |
Li Zefan | 33345d01 | 2011-04-20 10:31:50 +0800 | [diff] [blame] | 156 | key.objectid = btrfs_ino(inode); |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 157 | key.offset = start; |
| 158 | btrfs_set_key_type(&key, BTRFS_EXTENT_DATA_KEY); |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 159 | datasize = btrfs_file_extent_calc_inline_size(cur_size); |
| 160 | |
| 161 | inode_add_bytes(inode, size); |
| 162 | ret = btrfs_insert_empty_item(trans, root, path, &key, |
| 163 | datasize); |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 164 | if (ret) { |
| 165 | err = ret; |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 166 | goto fail; |
| 167 | } |
| 168 | leaf = path->nodes[0]; |
| 169 | ei = btrfs_item_ptr(leaf, path->slots[0], |
| 170 | struct btrfs_file_extent_item); |
| 171 | btrfs_set_file_extent_generation(leaf, ei, trans->transid); |
| 172 | btrfs_set_file_extent_type(leaf, ei, BTRFS_FILE_EXTENT_INLINE); |
| 173 | btrfs_set_file_extent_encryption(leaf, ei, 0); |
| 174 | btrfs_set_file_extent_other_encoding(leaf, ei, 0); |
| 175 | btrfs_set_file_extent_ram_bytes(leaf, ei, size); |
| 176 | ptr = btrfs_file_extent_inline_start(ei); |
| 177 | |
Li Zefan | 261507a0 | 2010-12-17 14:21:50 +0800 | [diff] [blame] | 178 | if (compress_type != BTRFS_COMPRESS_NONE) { |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 179 | struct page *cpage; |
| 180 | int i = 0; |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 181 | while (compressed_size > 0) { |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 182 | cpage = compressed_pages[i]; |
Chris Mason | 5b050f0 | 2008-11-11 09:34:41 -0500 | [diff] [blame] | 183 | cur_size = min_t(unsigned long, compressed_size, |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 184 | PAGE_CACHE_SIZE); |
| 185 | |
Cong Wang | 7ac687d | 2011-11-25 23:14:28 +0800 | [diff] [blame] | 186 | kaddr = kmap_atomic(cpage); |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 187 | write_extent_buffer(leaf, kaddr, ptr, cur_size); |
Cong Wang | 7ac687d | 2011-11-25 23:14:28 +0800 | [diff] [blame] | 188 | kunmap_atomic(kaddr); |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 189 | |
| 190 | i++; |
| 191 | ptr += cur_size; |
| 192 | compressed_size -= cur_size; |
| 193 | } |
| 194 | btrfs_set_file_extent_compression(leaf, ei, |
Li Zefan | 261507a0 | 2010-12-17 14:21:50 +0800 | [diff] [blame] | 195 | compress_type); |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 196 | } else { |
| 197 | page = find_get_page(inode->i_mapping, |
| 198 | start >> PAGE_CACHE_SHIFT); |
| 199 | btrfs_set_file_extent_compression(leaf, ei, 0); |
Cong Wang | 7ac687d | 2011-11-25 23:14:28 +0800 | [diff] [blame] | 200 | kaddr = kmap_atomic(page); |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 201 | offset = start & (PAGE_CACHE_SIZE - 1); |
| 202 | write_extent_buffer(leaf, kaddr + offset, ptr, size); |
Cong Wang | 7ac687d | 2011-11-25 23:14:28 +0800 | [diff] [blame] | 203 | kunmap_atomic(kaddr); |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 204 | page_cache_release(page); |
| 205 | } |
| 206 | btrfs_mark_buffer_dirty(leaf); |
| 207 | btrfs_free_path(path); |
| 208 | |
Yan, Zheng | c216775 | 2009-11-12 09:34:21 +0000 | [diff] [blame] | 209 | /* |
| 210 | * we're an inline extent, so nobody can |
| 211 | * extend the file past i_size without locking |
| 212 | * a page we already have locked. |
| 213 | * |
| 214 | * We must do any isize and inode updates |
| 215 | * before we unlock the pages. Otherwise we |
| 216 | * could end up racing with unlink. |
| 217 | */ |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 218 | BTRFS_I(inode)->disk_i_size = inode->i_size; |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 219 | ret = btrfs_update_inode(trans, root, inode); |
Yan, Zheng | c216775 | 2009-11-12 09:34:21 +0000 | [diff] [blame] | 220 | |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 221 | return ret; |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 222 | fail: |
| 223 | btrfs_free_path(path); |
| 224 | return err; |
| 225 | } |
| 226 | |
| 227 | |
| 228 | /* |
| 229 | * conditionally insert an inline extent into the file. This |
| 230 | * does the checks required to make sure the data is small enough |
| 231 | * to fit as an inline extent. |
| 232 | */ |
Chris Mason | 7f366cf | 2009-03-12 20:12:45 -0400 | [diff] [blame] | 233 | static noinline int cow_file_range_inline(struct btrfs_trans_handle *trans, |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 234 | struct btrfs_root *root, |
| 235 | struct inode *inode, u64 start, u64 end, |
Li Zefan | fe3f566 | 2011-03-28 08:30:38 +0000 | [diff] [blame] | 236 | size_t compressed_size, int compress_type, |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 237 | struct page **compressed_pages) |
| 238 | { |
| 239 | u64 isize = i_size_read(inode); |
| 240 | u64 actual_end = min(end + 1, isize); |
| 241 | u64 inline_len = actual_end - start; |
Qu Wenruo | fda2832 | 2013-02-26 08:10:22 +0000 | [diff] [blame] | 242 | u64 aligned_end = ALIGN(end, root->sectorsize); |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 243 | u64 data_len = inline_len; |
| 244 | int ret; |
| 245 | |
| 246 | if (compressed_size) |
| 247 | data_len = compressed_size; |
| 248 | |
| 249 | if (start > 0 || |
Chris Mason | 70b99e6 | 2008-10-31 12:46:39 -0400 | [diff] [blame] | 250 | actual_end >= PAGE_CACHE_SIZE || |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 251 | data_len >= BTRFS_MAX_INLINE_DATA_SIZE(root) || |
| 252 | (!compressed_size && |
| 253 | (actual_end & (root->sectorsize - 1)) == 0) || |
| 254 | end + 1 < isize || |
| 255 | data_len > root->fs_info->max_inline) { |
| 256 | return 1; |
| 257 | } |
| 258 | |
Josef Bacik | 2671485 | 2012-08-29 12:24:27 -0400 | [diff] [blame] | 259 | ret = btrfs_drop_extents(trans, root, inode, start, aligned_end, 1); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 260 | if (ret) |
| 261 | return ret; |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 262 | |
| 263 | if (isize > actual_end) |
| 264 | inline_len = min_t(u64, isize, actual_end); |
| 265 | ret = insert_inline_extent(trans, root, inode, start, |
| 266 | inline_len, compressed_size, |
Li Zefan | fe3f566 | 2011-03-28 08:30:38 +0000 | [diff] [blame] | 267 | compress_type, compressed_pages); |
Josef Bacik | 2adcac1 | 2012-05-23 16:10:14 -0400 | [diff] [blame] | 268 | if (ret && ret != -ENOSPC) { |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 269 | btrfs_abort_transaction(trans, root, ret); |
| 270 | return ret; |
Josef Bacik | 2adcac1 | 2012-05-23 16:10:14 -0400 | [diff] [blame] | 271 | } else if (ret == -ENOSPC) { |
| 272 | return 1; |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 273 | } |
Josef Bacik | 2adcac1 | 2012-05-23 16:10:14 -0400 | [diff] [blame] | 274 | |
Josef Bacik | bdc20e6 | 2013-02-28 13:23:38 -0500 | [diff] [blame] | 275 | set_bit(BTRFS_INODE_NEEDS_FULL_SYNC, &BTRFS_I(inode)->runtime_flags); |
Yan, Zheng | 0ca1f7c | 2010-05-16 10:48:47 -0400 | [diff] [blame] | 276 | btrfs_delalloc_release_metadata(inode, end + 1 - start); |
Chris Mason | a1ed835 | 2009-09-11 12:27:37 -0400 | [diff] [blame] | 277 | btrfs_drop_extent_cache(inode, start, aligned_end - 1, 0); |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 278 | return 0; |
| 279 | } |
| 280 | |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 281 | struct async_extent { |
| 282 | u64 start; |
| 283 | u64 ram_size; |
| 284 | u64 compressed_size; |
| 285 | struct page **pages; |
| 286 | unsigned long nr_pages; |
Li Zefan | 261507a0 | 2010-12-17 14:21:50 +0800 | [diff] [blame] | 287 | int compress_type; |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 288 | struct list_head list; |
| 289 | }; |
| 290 | |
| 291 | struct async_cow { |
| 292 | struct inode *inode; |
| 293 | struct btrfs_root *root; |
| 294 | struct page *locked_page; |
| 295 | u64 start; |
| 296 | u64 end; |
| 297 | struct list_head extents; |
| 298 | struct btrfs_work work; |
| 299 | }; |
| 300 | |
| 301 | static noinline int add_async_extent(struct async_cow *cow, |
| 302 | u64 start, u64 ram_size, |
| 303 | u64 compressed_size, |
| 304 | struct page **pages, |
Li Zefan | 261507a0 | 2010-12-17 14:21:50 +0800 | [diff] [blame] | 305 | unsigned long nr_pages, |
| 306 | int compress_type) |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 307 | { |
| 308 | struct async_extent *async_extent; |
| 309 | |
| 310 | async_extent = kmalloc(sizeof(*async_extent), GFP_NOFS); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 311 | BUG_ON(!async_extent); /* -ENOMEM */ |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 312 | async_extent->start = start; |
| 313 | async_extent->ram_size = ram_size; |
| 314 | async_extent->compressed_size = compressed_size; |
| 315 | async_extent->pages = pages; |
| 316 | async_extent->nr_pages = nr_pages; |
Li Zefan | 261507a0 | 2010-12-17 14:21:50 +0800 | [diff] [blame] | 317 | async_extent->compress_type = compress_type; |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 318 | list_add_tail(&async_extent->list, &cow->extents); |
| 319 | return 0; |
| 320 | } |
| 321 | |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 322 | /* |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 323 | * we create compressed extents in two phases. The first |
| 324 | * phase compresses a range of pages that have already been |
| 325 | * locked (both pages and state bits are locked). |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 326 | * |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 327 | * This is done inside an ordered work queue, and the compression |
| 328 | * is spread across many cpus. The actual IO submission is step |
| 329 | * two, and the ordered work queue takes care of making sure that |
| 330 | * happens in the same order things were put onto the queue by |
| 331 | * writepages and friends. |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 332 | * |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 333 | * If this code finds it can't get good compression, it puts an |
| 334 | * entry onto the work queue to write the uncompressed bytes. This |
| 335 | * makes sure that both compressed inodes and uncompressed inodes |
Artem Bityutskiy | b257031 | 2012-07-25 18:12:06 +0300 | [diff] [blame] | 336 | * are written in the same order that the flusher thread sent them |
| 337 | * down. |
Chris Mason | d352ac6 | 2008-09-29 15:18:18 -0400 | [diff] [blame] | 338 | */ |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 339 | static noinline int compress_file_range(struct inode *inode, |
| 340 | struct page *locked_page, |
| 341 | u64 start, u64 end, |
| 342 | struct async_cow *async_cow, |
| 343 | int *num_added) |
Chris Mason | b888db2 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 344 | { |
| 345 | struct btrfs_root *root = BTRFS_I(inode)->root; |
| 346 | struct btrfs_trans_handle *trans; |
Chris Mason | db94535 | 2007-10-15 16:15:53 -0400 | [diff] [blame] | 347 | u64 num_bytes; |
Chris Mason | db94535 | 2007-10-15 16:15:53 -0400 | [diff] [blame] | 348 | u64 blocksize = root->sectorsize; |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 349 | u64 actual_end; |
Chris Mason | 42dc7ba | 2008-12-15 11:44:56 -0500 | [diff] [blame] | 350 | u64 isize = i_size_read(inode); |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 351 | int ret = 0; |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 352 | struct page **pages = NULL; |
| 353 | unsigned long nr_pages; |
| 354 | unsigned long nr_pages_ret = 0; |
| 355 | unsigned long total_compressed = 0; |
| 356 | unsigned long total_in = 0; |
| 357 | unsigned long max_compressed = 128 * 1024; |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 358 | unsigned long max_uncompressed = 128 * 1024; |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 359 | int i; |
| 360 | int will_compress; |
Li Zefan | 261507a0 | 2010-12-17 14:21:50 +0800 | [diff] [blame] | 361 | int compress_type = root->fs_info->compress_type; |
Chris Mason | 4adaa61 | 2013-03-26 13:07:00 -0400 | [diff] [blame] | 362 | int redirty = 0; |
Chris Mason | b888db2 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 363 | |
Liu Bo | 4cb13e5 | 2012-03-29 09:57:45 -0400 | [diff] [blame] | 364 | /* if this is a small write inside eof, kick off a defrag */ |
| 365 | if ((end - start + 1) < 16 * 1024 && |
| 366 | (start > 0 || end + 1 < BTRFS_I(inode)->disk_i_size)) |
Chris Mason | 4cb5300 | 2011-05-24 15:35:30 -0400 | [diff] [blame] | 367 | btrfs_add_inode_defrag(NULL, inode); |
| 368 | |
Chris Mason | 42dc7ba | 2008-12-15 11:44:56 -0500 | [diff] [blame] | 369 | actual_end = min_t(u64, isize, end + 1); |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 370 | again: |
| 371 | will_compress = 0; |
| 372 | nr_pages = (end >> PAGE_CACHE_SHIFT) - (start >> PAGE_CACHE_SHIFT) + 1; |
| 373 | nr_pages = min(nr_pages, (128 * 1024UL) / PAGE_CACHE_SIZE); |
| 374 | |
Chris Mason | f03d9301f | 2009-02-04 09:31:06 -0500 | [diff] [blame] | 375 | /* |
| 376 | * we don't want to send crud past the end of i_size through |
| 377 | * compression, that's just a waste of CPU time. So, if the |
| 378 | * end of the file is before the start of our current |
| 379 | * requested range of bytes, we bail out to the uncompressed |
| 380 | * cleanup code that can deal with all of this. |
| 381 | * |
| 382 | * It isn't really the fastest way to fix things, but this is a |
| 383 | * very uncommon corner. |
| 384 | */ |
| 385 | if (actual_end <= start) |
| 386 | goto cleanup_and_bail_uncompressed; |
| 387 | |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 388 | total_compressed = actual_end - start; |
| 389 | |
| 390 | /* we want to make sure that amount of ram required to uncompress |
| 391 | * an extent is reasonable, so we limit the total size in ram |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 392 | * of a compressed extent to 128k. This is a crucial number |
| 393 | * because it also controls how easily we can spread reads across |
| 394 | * cpus for decompression. |
| 395 | * |
| 396 | * We also want to make sure the amount of IO required to do |
| 397 | * a random read is reasonably small, so we limit the size of |
| 398 | * a compressed extent to 128k. |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 399 | */ |
| 400 | total_compressed = min(total_compressed, max_uncompressed); |
Qu Wenruo | fda2832 | 2013-02-26 08:10:22 +0000 | [diff] [blame] | 401 | num_bytes = ALIGN(end - start + 1, blocksize); |
Chris Mason | be20aa9 | 2007-12-17 20:14:01 -0500 | [diff] [blame] | 402 | num_bytes = max(blocksize, num_bytes); |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 403 | total_in = 0; |
| 404 | ret = 0; |
Chris Mason | db94535 | 2007-10-15 16:15:53 -0400 | [diff] [blame] | 405 | |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 406 | /* |
| 407 | * we do compression for mount -o compress and when the |
| 408 | * inode has not been flagged as nocompress. This flag can |
| 409 | * change at any time if we discover bad compression ratios. |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 410 | */ |
Christoph Hellwig | 6cbff00 | 2009-04-17 10:37:41 +0200 | [diff] [blame] | 411 | if (!(BTRFS_I(inode)->flags & BTRFS_INODE_NOCOMPRESS) && |
Chris Mason | 1e701a3 | 2010-03-11 09:42:04 -0500 | [diff] [blame] | 412 | (btrfs_test_opt(root, COMPRESS) || |
Liu Bo | 75e7cb7 | 2011-03-22 10:12:20 +0000 | [diff] [blame] | 413 | (BTRFS_I(inode)->force_compress) || |
| 414 | (BTRFS_I(inode)->flags & BTRFS_INODE_COMPRESS))) { |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 415 | WARN_ON(pages); |
Chris Mason | cfbc246 | 2008-10-30 13:22:14 -0400 | [diff] [blame] | 416 | pages = kzalloc(sizeof(struct page *) * nr_pages, GFP_NOFS); |
Li Zefan | 560f7d7 | 2011-09-08 10:22:01 +0800 | [diff] [blame] | 417 | if (!pages) { |
| 418 | /* just bail out to the uncompressed code */ |
| 419 | goto cont; |
| 420 | } |
Chris Mason | 179e29e | 2007-11-01 11:28:41 -0400 | [diff] [blame] | 421 | |
Li Zefan | 261507a0 | 2010-12-17 14:21:50 +0800 | [diff] [blame] | 422 | if (BTRFS_I(inode)->force_compress) |
| 423 | compress_type = BTRFS_I(inode)->force_compress; |
| 424 | |
Chris Mason | 4adaa61 | 2013-03-26 13:07:00 -0400 | [diff] [blame] | 425 | /* |
| 426 | * we need to call clear_page_dirty_for_io on each |
| 427 | * page in the range. Otherwise applications with the file |
| 428 | * mmap'd can wander in and change the page contents while |
| 429 | * we are compressing them. |
| 430 | * |
| 431 | * If the compression fails for any reason, we set the pages |
| 432 | * dirty again later on. |
| 433 | */ |
| 434 | extent_range_clear_dirty_for_io(inode, start, end); |
| 435 | redirty = 1; |
Li Zefan | 261507a0 | 2010-12-17 14:21:50 +0800 | [diff] [blame] | 436 | ret = btrfs_compress_pages(compress_type, |
| 437 | inode->i_mapping, start, |
| 438 | total_compressed, pages, |
| 439 | nr_pages, &nr_pages_ret, |
| 440 | &total_in, |
| 441 | &total_compressed, |
| 442 | max_compressed); |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 443 | |
| 444 | if (!ret) { |
| 445 | unsigned long offset = total_compressed & |
| 446 | (PAGE_CACHE_SIZE - 1); |
| 447 | struct page *page = pages[nr_pages_ret - 1]; |
| 448 | char *kaddr; |
| 449 | |
| 450 | /* zero the tail end of the last page, we might be |
| 451 | * sending it down to disk |
| 452 | */ |
| 453 | if (offset) { |
Cong Wang | 7ac687d | 2011-11-25 23:14:28 +0800 | [diff] [blame] | 454 | kaddr = kmap_atomic(page); |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 455 | memset(kaddr + offset, 0, |
| 456 | PAGE_CACHE_SIZE - offset); |
Cong Wang | 7ac687d | 2011-11-25 23:14:28 +0800 | [diff] [blame] | 457 | kunmap_atomic(kaddr); |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 458 | } |
| 459 | will_compress = 1; |
| 460 | } |
| 461 | } |
Li Zefan | 560f7d7 | 2011-09-08 10:22:01 +0800 | [diff] [blame] | 462 | cont: |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 463 | if (start == 0) { |
Josef Bacik | 7a7eaa4 | 2011-04-13 12:54:33 -0400 | [diff] [blame] | 464 | trans = btrfs_join_transaction(root); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 465 | if (IS_ERR(trans)) { |
| 466 | ret = PTR_ERR(trans); |
| 467 | trans = NULL; |
| 468 | goto cleanup_and_out; |
| 469 | } |
Yan, Zheng | 0ca1f7c | 2010-05-16 10:48:47 -0400 | [diff] [blame] | 470 | trans->block_rsv = &root->fs_info->delalloc_block_rsv; |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 471 | |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 472 | /* lets try to make an inline extent */ |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 473 | if (ret || total_in < (actual_end - start)) { |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 474 | /* we didn't compress the entire range, try |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 475 | * to make an uncompressed inline extent. |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 476 | */ |
| 477 | ret = cow_file_range_inline(trans, root, inode, |
Li Zefan | fe3f566 | 2011-03-28 08:30:38 +0000 | [diff] [blame] | 478 | start, end, 0, 0, NULL); |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 479 | } else { |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 480 | /* try making a compressed inline extent */ |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 481 | ret = cow_file_range_inline(trans, root, inode, |
| 482 | start, end, |
Li Zefan | fe3f566 | 2011-03-28 08:30:38 +0000 | [diff] [blame] | 483 | total_compressed, |
| 484 | compress_type, pages); |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 485 | } |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 486 | if (ret <= 0) { |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 487 | /* |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 488 | * inline extent creation worked or returned error, |
| 489 | * we don't need to create any more async work items. |
| 490 | * Unlock and free up our temp pages. |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 491 | */ |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 492 | extent_clear_unlock_delalloc(inode, |
Chris Mason | a791e35 | 2009-10-08 11:27:10 -0400 | [diff] [blame] | 493 | &BTRFS_I(inode)->io_tree, |
| 494 | start, end, NULL, |
| 495 | EXTENT_CLEAR_UNLOCK_PAGE | EXTENT_CLEAR_DIRTY | |
Chris Mason | a3429ab | 2009-10-08 12:30:20 -0400 | [diff] [blame] | 496 | EXTENT_CLEAR_DELALLOC | |
Chris Mason | a791e35 | 2009-10-08 11:27:10 -0400 | [diff] [blame] | 497 | EXTENT_SET_WRITEBACK | EXTENT_END_WRITEBACK); |
Yan, Zheng | c216775 | 2009-11-12 09:34:21 +0000 | [diff] [blame] | 498 | |
| 499 | btrfs_end_transaction(trans, root); |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 500 | goto free_pages_out; |
| 501 | } |
Yan, Zheng | c216775 | 2009-11-12 09:34:21 +0000 | [diff] [blame] | 502 | btrfs_end_transaction(trans, root); |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 503 | } |
| 504 | |
| 505 | if (will_compress) { |
| 506 | /* |
| 507 | * we aren't doing an inline extent round the compressed size |
| 508 | * up to a block size boundary so the allocator does sane |
| 509 | * things |
| 510 | */ |
Qu Wenruo | fda2832 | 2013-02-26 08:10:22 +0000 | [diff] [blame] | 511 | total_compressed = ALIGN(total_compressed, blocksize); |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 512 | |
| 513 | /* |
| 514 | * one last check to make sure the compression is really a |
| 515 | * win, compare the page count read with the blocks on disk |
| 516 | */ |
Qu Wenruo | fda2832 | 2013-02-26 08:10:22 +0000 | [diff] [blame] | 517 | total_in = ALIGN(total_in, PAGE_CACHE_SIZE); |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 518 | if (total_compressed >= total_in) { |
| 519 | will_compress = 0; |
| 520 | } else { |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 521 | num_bytes = total_in; |
| 522 | } |
| 523 | } |
| 524 | if (!will_compress && pages) { |
| 525 | /* |
| 526 | * the compression code ran but failed to make things smaller, |
| 527 | * free any pages it allocated and our page pointer array |
| 528 | */ |
| 529 | for (i = 0; i < nr_pages_ret; i++) { |
Chris Mason | 70b99e6 | 2008-10-31 12:46:39 -0400 | [diff] [blame] | 530 | WARN_ON(pages[i]->mapping); |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 531 | page_cache_release(pages[i]); |
| 532 | } |
| 533 | kfree(pages); |
| 534 | pages = NULL; |
| 535 | total_compressed = 0; |
| 536 | nr_pages_ret = 0; |
| 537 | |
| 538 | /* flag the file so we don't compress in the future */ |
Chris Mason | 1e701a3 | 2010-03-11 09:42:04 -0500 | [diff] [blame] | 539 | if (!btrfs_test_opt(root, FORCE_COMPRESS) && |
| 540 | !(BTRFS_I(inode)->force_compress)) { |
Chris Mason | a555f81 | 2010-01-28 16:18:15 -0500 | [diff] [blame] | 541 | BTRFS_I(inode)->flags |= BTRFS_INODE_NOCOMPRESS; |
Chris Mason | 1e701a3 | 2010-03-11 09:42:04 -0500 | [diff] [blame] | 542 | } |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 543 | } |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 544 | if (will_compress) { |
| 545 | *num_added += 1; |
| 546 | |
| 547 | /* the async work queues will take care of doing actual |
| 548 | * allocation on disk for these compressed pages, |
| 549 | * and will submit them to the elevator. |
| 550 | */ |
| 551 | add_async_extent(async_cow, start, num_bytes, |
Li Zefan | 261507a0 | 2010-12-17 14:21:50 +0800 | [diff] [blame] | 552 | total_compressed, pages, nr_pages_ret, |
| 553 | compress_type); |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 554 | |
Yan, Zheng | 24ae636 | 2010-12-06 07:02:36 +0000 | [diff] [blame] | 555 | if (start + num_bytes < end) { |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 556 | start += num_bytes; |
| 557 | pages = NULL; |
| 558 | cond_resched(); |
| 559 | goto again; |
| 560 | } |
| 561 | } else { |
Chris Mason | f03d9301f | 2009-02-04 09:31:06 -0500 | [diff] [blame] | 562 | cleanup_and_bail_uncompressed: |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 563 | /* |
| 564 | * No compression, but we still need to write the pages in |
| 565 | * the file we've been given so far. redirty the locked |
| 566 | * page if it corresponds to our extent and set things up |
| 567 | * for the async work queue to run cow_file_range to do |
| 568 | * the normal delalloc dance |
| 569 | */ |
| 570 | if (page_offset(locked_page) >= start && |
| 571 | page_offset(locked_page) <= end) { |
| 572 | __set_page_dirty_nobuffers(locked_page); |
| 573 | /* unlocked later on in the async handlers */ |
| 574 | } |
Chris Mason | 4adaa61 | 2013-03-26 13:07:00 -0400 | [diff] [blame] | 575 | if (redirty) |
| 576 | extent_range_redirty_for_io(inode, start, end); |
Li Zefan | 261507a0 | 2010-12-17 14:21:50 +0800 | [diff] [blame] | 577 | add_async_extent(async_cow, start, end - start + 1, |
| 578 | 0, NULL, 0, BTRFS_COMPRESS_NONE); |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 579 | *num_added += 1; |
| 580 | } |
| 581 | |
| 582 | out: |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 583 | return ret; |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 584 | |
| 585 | free_pages_out: |
| 586 | for (i = 0; i < nr_pages_ret; i++) { |
| 587 | WARN_ON(pages[i]->mapping); |
| 588 | page_cache_release(pages[i]); |
| 589 | } |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 590 | kfree(pages); |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 591 | |
| 592 | goto out; |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 593 | |
| 594 | cleanup_and_out: |
| 595 | extent_clear_unlock_delalloc(inode, &BTRFS_I(inode)->io_tree, |
| 596 | start, end, NULL, |
| 597 | EXTENT_CLEAR_UNLOCK_PAGE | |
| 598 | EXTENT_CLEAR_DIRTY | |
| 599 | EXTENT_CLEAR_DELALLOC | |
| 600 | EXTENT_SET_WRITEBACK | |
| 601 | EXTENT_END_WRITEBACK); |
| 602 | if (!trans || IS_ERR(trans)) |
| 603 | btrfs_error(root->fs_info, ret, "Failed to join transaction"); |
| 604 | else |
| 605 | btrfs_abort_transaction(trans, root, ret); |
| 606 | goto free_pages_out; |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 607 | } |
| 608 | |
| 609 | /* |
| 610 | * phase two of compressed writeback. This is the ordered portion |
| 611 | * of the code, which only gets called in the order the work was |
| 612 | * queued. We walk all the async extents created by compress_file_range |
| 613 | * and send them down to the disk. |
| 614 | */ |
| 615 | static noinline int submit_compressed_extents(struct inode *inode, |
| 616 | struct async_cow *async_cow) |
| 617 | { |
| 618 | struct async_extent *async_extent; |
| 619 | u64 alloc_hint = 0; |
| 620 | struct btrfs_trans_handle *trans; |
| 621 | struct btrfs_key ins; |
| 622 | struct extent_map *em; |
| 623 | struct btrfs_root *root = BTRFS_I(inode)->root; |
| 624 | struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree; |
| 625 | struct extent_io_tree *io_tree; |
Josef Bacik | f5a84ee3 | 2009-11-10 21:23:48 -0500 | [diff] [blame] | 626 | int ret = 0; |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 627 | |
| 628 | if (list_empty(&async_cow->extents)) |
| 629 | return 0; |
| 630 | |
Josef Bacik | 3e04e7f | 2013-02-06 16:49:15 -0500 | [diff] [blame] | 631 | again: |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 632 | while (!list_empty(&async_cow->extents)) { |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 633 | async_extent = list_entry(async_cow->extents.next, |
| 634 | struct async_extent, list); |
| 635 | list_del(&async_extent->list); |
| 636 | |
| 637 | io_tree = &BTRFS_I(inode)->io_tree; |
| 638 | |
Josef Bacik | f5a84ee3 | 2009-11-10 21:23:48 -0500 | [diff] [blame] | 639 | retry: |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 640 | /* did the compression code fall back to uncompressed IO? */ |
| 641 | if (!async_extent->pages) { |
| 642 | int page_started = 0; |
| 643 | unsigned long nr_written = 0; |
| 644 | |
| 645 | lock_extent(io_tree, async_extent->start, |
Josef Bacik | 2ac55d4 | 2010-02-03 19:33:23 +0000 | [diff] [blame] | 646 | async_extent->start + |
Jeff Mahoney | d008237 | 2012-03-01 14:57:19 +0100 | [diff] [blame] | 647 | async_extent->ram_size - 1); |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 648 | |
| 649 | /* allocate blocks */ |
Josef Bacik | f5a84ee3 | 2009-11-10 21:23:48 -0500 | [diff] [blame] | 650 | ret = cow_file_range(inode, async_cow->locked_page, |
| 651 | async_extent->start, |
| 652 | async_extent->start + |
| 653 | async_extent->ram_size - 1, |
| 654 | &page_started, &nr_written, 0); |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 655 | |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 656 | /* JDM XXX */ |
| 657 | |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 658 | /* |
| 659 | * if page_started, cow_file_range inserted an |
| 660 | * inline extent and took care of all the unlocking |
| 661 | * and IO for us. Otherwise, we need to submit |
| 662 | * all those pages down to the drive. |
| 663 | */ |
Josef Bacik | f5a84ee3 | 2009-11-10 21:23:48 -0500 | [diff] [blame] | 664 | if (!page_started && !ret) |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 665 | extent_write_locked_range(io_tree, |
| 666 | inode, async_extent->start, |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 667 | async_extent->start + |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 668 | async_extent->ram_size - 1, |
| 669 | btrfs_get_extent, |
| 670 | WB_SYNC_ALL); |
Josef Bacik | 3e04e7f | 2013-02-06 16:49:15 -0500 | [diff] [blame] | 671 | else if (ret) |
| 672 | unlock_page(async_cow->locked_page); |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 673 | kfree(async_extent); |
| 674 | cond_resched(); |
| 675 | continue; |
| 676 | } |
| 677 | |
| 678 | lock_extent(io_tree, async_extent->start, |
Jeff Mahoney | d008237 | 2012-03-01 14:57:19 +0100 | [diff] [blame] | 679 | async_extent->start + async_extent->ram_size - 1); |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 680 | |
Josef Bacik | 7a7eaa4 | 2011-04-13 12:54:33 -0400 | [diff] [blame] | 681 | trans = btrfs_join_transaction(root); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 682 | if (IS_ERR(trans)) { |
| 683 | ret = PTR_ERR(trans); |
| 684 | } else { |
| 685 | trans->block_rsv = &root->fs_info->delalloc_block_rsv; |
| 686 | ret = btrfs_reserve_extent(trans, root, |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 687 | async_extent->compressed_size, |
| 688 | async_extent->compressed_size, |
Josef Bacik | 81c9ad2 | 2012-01-18 10:56:06 -0500 | [diff] [blame] | 689 | 0, alloc_hint, &ins, 1); |
Miao Xie | 962197b | 2012-09-11 23:27:35 -0600 | [diff] [blame] | 690 | if (ret && ret != -ENOSPC) |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 691 | btrfs_abort_transaction(trans, root, ret); |
| 692 | btrfs_end_transaction(trans, root); |
| 693 | } |
Yan, Zheng | c216775 | 2009-11-12 09:34:21 +0000 | [diff] [blame] | 694 | |
Josef Bacik | f5a84ee3 | 2009-11-10 21:23:48 -0500 | [diff] [blame] | 695 | if (ret) { |
| 696 | int i; |
Josef Bacik | 3e04e7f | 2013-02-06 16:49:15 -0500 | [diff] [blame] | 697 | |
Josef Bacik | f5a84ee3 | 2009-11-10 21:23:48 -0500 | [diff] [blame] | 698 | for (i = 0; i < async_extent->nr_pages; i++) { |
| 699 | WARN_ON(async_extent->pages[i]->mapping); |
| 700 | page_cache_release(async_extent->pages[i]); |
| 701 | } |
| 702 | kfree(async_extent->pages); |
| 703 | async_extent->nr_pages = 0; |
| 704 | async_extent->pages = NULL; |
Josef Bacik | 3e04e7f | 2013-02-06 16:49:15 -0500 | [diff] [blame] | 705 | |
Josef Bacik | fdf8e2e | 2013-06-14 16:58:23 -0400 | [diff] [blame] | 706 | if (ret == -ENOSPC) { |
| 707 | unlock_extent(io_tree, async_extent->start, |
| 708 | async_extent->start + |
| 709 | async_extent->ram_size - 1); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 710 | goto retry; |
Josef Bacik | fdf8e2e | 2013-06-14 16:58:23 -0400 | [diff] [blame] | 711 | } |
Josef Bacik | 3e04e7f | 2013-02-06 16:49:15 -0500 | [diff] [blame] | 712 | goto out_free; |
Josef Bacik | f5a84ee3 | 2009-11-10 21:23:48 -0500 | [diff] [blame] | 713 | } |
| 714 | |
Yan, Zheng | c216775 | 2009-11-12 09:34:21 +0000 | [diff] [blame] | 715 | /* |
| 716 | * here we're doing allocation and writeback of the |
| 717 | * compressed pages |
| 718 | */ |
| 719 | btrfs_drop_extent_cache(inode, async_extent->start, |
| 720 | async_extent->start + |
| 721 | async_extent->ram_size - 1, 0); |
| 722 | |
David Sterba | 172ddd6 | 2011-04-21 00:48:27 +0200 | [diff] [blame] | 723 | em = alloc_extent_map(); |
Liu Bo | b9aa55b | 2013-05-14 02:12:15 +0000 | [diff] [blame] | 724 | if (!em) { |
| 725 | ret = -ENOMEM; |
Josef Bacik | 3e04e7f | 2013-02-06 16:49:15 -0500 | [diff] [blame] | 726 | goto out_free_reserve; |
Liu Bo | b9aa55b | 2013-05-14 02:12:15 +0000 | [diff] [blame] | 727 | } |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 728 | em->start = async_extent->start; |
| 729 | em->len = async_extent->ram_size; |
Chris Mason | 445a694 | 2008-11-10 11:53:33 -0500 | [diff] [blame] | 730 | em->orig_start = em->start; |
Josef Bacik | 2ab28f3 | 2012-10-12 15:27:49 -0400 | [diff] [blame] | 731 | em->mod_start = em->start; |
| 732 | em->mod_len = em->len; |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 733 | |
| 734 | em->block_start = ins.objectid; |
| 735 | em->block_len = ins.offset; |
Josef Bacik | b493968 | 2012-12-03 10:31:19 -0500 | [diff] [blame] | 736 | em->orig_block_len = ins.offset; |
Josef Bacik | cc95bef | 2013-04-04 14:31:27 -0400 | [diff] [blame] | 737 | em->ram_bytes = async_extent->ram_size; |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 738 | em->bdev = root->fs_info->fs_devices->latest_bdev; |
Li Zefan | 261507a0 | 2010-12-17 14:21:50 +0800 | [diff] [blame] | 739 | em->compress_type = async_extent->compress_type; |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 740 | set_bit(EXTENT_FLAG_PINNED, &em->flags); |
| 741 | set_bit(EXTENT_FLAG_COMPRESSED, &em->flags); |
Josef Bacik | 70c8a91 | 2012-10-11 16:54:30 -0400 | [diff] [blame] | 742 | em->generation = -1; |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 743 | |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 744 | while (1) { |
Chris Mason | 890871b | 2009-09-02 16:24:52 -0400 | [diff] [blame] | 745 | write_lock(&em_tree->lock); |
Josef Bacik | 09a2a8f9 | 2013-04-05 16:51:15 -0400 | [diff] [blame] | 746 | ret = add_extent_mapping(em_tree, em, 1); |
Chris Mason | 890871b | 2009-09-02 16:24:52 -0400 | [diff] [blame] | 747 | write_unlock(&em_tree->lock); |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 748 | if (ret != -EEXIST) { |
| 749 | free_extent_map(em); |
| 750 | break; |
| 751 | } |
| 752 | btrfs_drop_extent_cache(inode, async_extent->start, |
| 753 | async_extent->start + |
| 754 | async_extent->ram_size - 1, 0); |
| 755 | } |
| 756 | |
Josef Bacik | 3e04e7f | 2013-02-06 16:49:15 -0500 | [diff] [blame] | 757 | if (ret) |
| 758 | goto out_free_reserve; |
| 759 | |
Li Zefan | 261507a0 | 2010-12-17 14:21:50 +0800 | [diff] [blame] | 760 | ret = btrfs_add_ordered_extent_compress(inode, |
| 761 | async_extent->start, |
| 762 | ins.objectid, |
| 763 | async_extent->ram_size, |
| 764 | ins.offset, |
| 765 | BTRFS_ORDERED_COMPRESSED, |
| 766 | async_extent->compress_type); |
Josef Bacik | 3e04e7f | 2013-02-06 16:49:15 -0500 | [diff] [blame] | 767 | if (ret) |
| 768 | goto out_free_reserve; |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 769 | |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 770 | /* |
| 771 | * clear dirty, set writeback and unlock the pages. |
| 772 | */ |
| 773 | extent_clear_unlock_delalloc(inode, |
Chris Mason | a791e35 | 2009-10-08 11:27:10 -0400 | [diff] [blame] | 774 | &BTRFS_I(inode)->io_tree, |
| 775 | async_extent->start, |
| 776 | async_extent->start + |
| 777 | async_extent->ram_size - 1, |
| 778 | NULL, EXTENT_CLEAR_UNLOCK_PAGE | |
| 779 | EXTENT_CLEAR_UNLOCK | |
Chris Mason | a3429ab | 2009-10-08 12:30:20 -0400 | [diff] [blame] | 780 | EXTENT_CLEAR_DELALLOC | |
Chris Mason | a791e35 | 2009-10-08 11:27:10 -0400 | [diff] [blame] | 781 | EXTENT_CLEAR_DIRTY | EXTENT_SET_WRITEBACK); |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 782 | |
| 783 | ret = btrfs_submit_compressed_write(inode, |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 784 | async_extent->start, |
| 785 | async_extent->ram_size, |
| 786 | ins.objectid, |
| 787 | ins.offset, async_extent->pages, |
| 788 | async_extent->nr_pages); |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 789 | alloc_hint = ins.objectid + ins.offset; |
| 790 | kfree(async_extent); |
Josef Bacik | 3e04e7f | 2013-02-06 16:49:15 -0500 | [diff] [blame] | 791 | if (ret) |
| 792 | goto out; |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 793 | cond_resched(); |
| 794 | } |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 795 | ret = 0; |
| 796 | out: |
| 797 | return ret; |
Josef Bacik | 3e04e7f | 2013-02-06 16:49:15 -0500 | [diff] [blame] | 798 | out_free_reserve: |
| 799 | btrfs_free_reserved_extent(root, ins.objectid, ins.offset); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 800 | out_free: |
Josef Bacik | 3e04e7f | 2013-02-06 16:49:15 -0500 | [diff] [blame] | 801 | extent_clear_unlock_delalloc(inode, &BTRFS_I(inode)->io_tree, |
| 802 | async_extent->start, |
| 803 | async_extent->start + |
| 804 | async_extent->ram_size - 1, |
| 805 | NULL, EXTENT_CLEAR_UNLOCK_PAGE | |
| 806 | EXTENT_CLEAR_UNLOCK | |
| 807 | EXTENT_CLEAR_DELALLOC | |
| 808 | EXTENT_CLEAR_DIRTY | |
| 809 | EXTENT_SET_WRITEBACK | |
| 810 | EXTENT_END_WRITEBACK); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 811 | kfree(async_extent); |
Josef Bacik | 3e04e7f | 2013-02-06 16:49:15 -0500 | [diff] [blame] | 812 | goto again; |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 813 | } |
| 814 | |
Josef Bacik | 4b46fce | 2010-05-23 11:00:55 -0400 | [diff] [blame] | 815 | static u64 get_extent_allocation_hint(struct inode *inode, u64 start, |
| 816 | u64 num_bytes) |
| 817 | { |
| 818 | struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree; |
| 819 | struct extent_map *em; |
| 820 | u64 alloc_hint = 0; |
| 821 | |
| 822 | read_lock(&em_tree->lock); |
| 823 | em = search_extent_mapping(em_tree, start, num_bytes); |
| 824 | if (em) { |
| 825 | /* |
| 826 | * if block start isn't an actual block number then find the |
| 827 | * first block in this inode and use that as a hint. If that |
| 828 | * block is also bogus then just don't worry about it. |
| 829 | */ |
| 830 | if (em->block_start >= EXTENT_MAP_LAST_BYTE) { |
| 831 | free_extent_map(em); |
| 832 | em = search_extent_mapping(em_tree, 0, 0); |
| 833 | if (em && em->block_start < EXTENT_MAP_LAST_BYTE) |
| 834 | alloc_hint = em->block_start; |
| 835 | if (em) |
| 836 | free_extent_map(em); |
| 837 | } else { |
| 838 | alloc_hint = em->block_start; |
| 839 | free_extent_map(em); |
| 840 | } |
| 841 | } |
| 842 | read_unlock(&em_tree->lock); |
| 843 | |
| 844 | return alloc_hint; |
| 845 | } |
| 846 | |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 847 | /* |
| 848 | * when extent_io.c finds a delayed allocation range in the file, |
| 849 | * the call backs end up in this code. The basic idea is to |
| 850 | * allocate extents on disk for the range, and create ordered data structs |
| 851 | * in ram to track those extents. |
| 852 | * |
| 853 | * locked_page is the page that writepage had locked already. We use |
| 854 | * it to make sure we don't do extra locks or unlocks. |
| 855 | * |
| 856 | * *page_started is set to one if we unlock locked_page and do everything |
| 857 | * required to start IO on it. It may be clean and already done with |
| 858 | * IO when we return. |
| 859 | */ |
Miao Xie | b7d5b0a | 2012-11-01 07:32:18 +0000 | [diff] [blame] | 860 | static noinline int __cow_file_range(struct btrfs_trans_handle *trans, |
| 861 | struct inode *inode, |
| 862 | struct btrfs_root *root, |
| 863 | struct page *locked_page, |
| 864 | u64 start, u64 end, int *page_started, |
| 865 | unsigned long *nr_written, |
| 866 | int unlock) |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 867 | { |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 868 | u64 alloc_hint = 0; |
| 869 | u64 num_bytes; |
| 870 | unsigned long ram_size; |
| 871 | u64 disk_num_bytes; |
| 872 | u64 cur_alloc_size; |
| 873 | u64 blocksize = root->sectorsize; |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 874 | struct btrfs_key ins; |
| 875 | struct extent_map *em; |
| 876 | struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree; |
| 877 | int ret = 0; |
| 878 | |
Liu Bo | 83eea1f | 2012-07-10 05:28:39 -0600 | [diff] [blame] | 879 | BUG_ON(btrfs_is_free_space_inode(inode)); |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 880 | |
Qu Wenruo | fda2832 | 2013-02-26 08:10:22 +0000 | [diff] [blame] | 881 | num_bytes = ALIGN(end - start + 1, blocksize); |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 882 | num_bytes = max(blocksize, num_bytes); |
| 883 | disk_num_bytes = num_bytes; |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 884 | |
Chris Mason | 4cb5300 | 2011-05-24 15:35:30 -0400 | [diff] [blame] | 885 | /* if this is a small write inside eof, kick off defrag */ |
Liu Bo | 4cb13e5 | 2012-03-29 09:57:45 -0400 | [diff] [blame] | 886 | if (num_bytes < 64 * 1024 && |
| 887 | (start > 0 || end + 1 < BTRFS_I(inode)->disk_i_size)) |
Chris Mason | 4cb5300 | 2011-05-24 15:35:30 -0400 | [diff] [blame] | 888 | btrfs_add_inode_defrag(trans, inode); |
| 889 | |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 890 | if (start == 0) { |
| 891 | /* lets try to make an inline extent */ |
| 892 | ret = cow_file_range_inline(trans, root, inode, |
Li Zefan | fe3f566 | 2011-03-28 08:30:38 +0000 | [diff] [blame] | 893 | start, end, 0, 0, NULL); |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 894 | if (ret == 0) { |
| 895 | extent_clear_unlock_delalloc(inode, |
Chris Mason | a791e35 | 2009-10-08 11:27:10 -0400 | [diff] [blame] | 896 | &BTRFS_I(inode)->io_tree, |
| 897 | start, end, NULL, |
| 898 | EXTENT_CLEAR_UNLOCK_PAGE | |
| 899 | EXTENT_CLEAR_UNLOCK | |
| 900 | EXTENT_CLEAR_DELALLOC | |
| 901 | EXTENT_CLEAR_DIRTY | |
| 902 | EXTENT_SET_WRITEBACK | |
| 903 | EXTENT_END_WRITEBACK); |
Yan, Zheng | c216775 | 2009-11-12 09:34:21 +0000 | [diff] [blame] | 904 | |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 905 | *nr_written = *nr_written + |
| 906 | (end - start + PAGE_CACHE_SIZE) / PAGE_CACHE_SIZE; |
| 907 | *page_started = 1; |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 908 | goto out; |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 909 | } else if (ret < 0) { |
| 910 | btrfs_abort_transaction(trans, root, ret); |
| 911 | goto out_unlock; |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 912 | } |
| 913 | } |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 914 | |
| 915 | BUG_ON(disk_num_bytes > |
David Sterba | 6c41761 | 2011-04-13 15:41:04 +0200 | [diff] [blame] | 916 | btrfs_super_total_bytes(root->fs_info->super_copy)); |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 917 | |
Josef Bacik | 4b46fce | 2010-05-23 11:00:55 -0400 | [diff] [blame] | 918 | alloc_hint = get_extent_allocation_hint(inode, start, num_bytes); |
Zheng Yan | 5b21f2e | 2008-09-26 10:05:38 -0400 | [diff] [blame] | 919 | btrfs_drop_extent_cache(inode, start, start + num_bytes - 1, 0); |
Chris Mason | 3b95151 | 2008-04-17 11:29:12 -0400 | [diff] [blame] | 920 | |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 921 | while (disk_num_bytes > 0) { |
Chris Mason | a791e35 | 2009-10-08 11:27:10 -0400 | [diff] [blame] | 922 | unsigned long op; |
| 923 | |
Josef Bacik | 287a0ab | 2010-03-19 18:07:23 +0000 | [diff] [blame] | 924 | cur_alloc_size = disk_num_bytes; |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 925 | ret = btrfs_reserve_extent(trans, root, cur_alloc_size, |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 926 | root->sectorsize, 0, alloc_hint, |
Josef Bacik | 81c9ad2 | 2012-01-18 10:56:06 -0500 | [diff] [blame] | 927 | &ins, 1); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 928 | if (ret < 0) { |
| 929 | btrfs_abort_transaction(trans, root, ret); |
| 930 | goto out_unlock; |
| 931 | } |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 932 | |
David Sterba | 172ddd6 | 2011-04-21 00:48:27 +0200 | [diff] [blame] | 933 | em = alloc_extent_map(); |
Liu Bo | b9aa55b | 2013-05-14 02:12:15 +0000 | [diff] [blame] | 934 | if (!em) { |
| 935 | ret = -ENOMEM; |
Liu Bo | ace68ba | 2013-04-22 10:53:47 +0000 | [diff] [blame] | 936 | goto out_reserve; |
Liu Bo | b9aa55b | 2013-05-14 02:12:15 +0000 | [diff] [blame] | 937 | } |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 938 | em->start = start; |
Chris Mason | 445a694 | 2008-11-10 11:53:33 -0500 | [diff] [blame] | 939 | em->orig_start = em->start; |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 940 | ram_size = ins.offset; |
| 941 | em->len = ins.offset; |
Josef Bacik | 2ab28f3 | 2012-10-12 15:27:49 -0400 | [diff] [blame] | 942 | em->mod_start = em->start; |
| 943 | em->mod_len = em->len; |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 944 | |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 945 | em->block_start = ins.objectid; |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 946 | em->block_len = ins.offset; |
Josef Bacik | b493968 | 2012-12-03 10:31:19 -0500 | [diff] [blame] | 947 | em->orig_block_len = ins.offset; |
Josef Bacik | cc95bef | 2013-04-04 14:31:27 -0400 | [diff] [blame] | 948 | em->ram_bytes = ram_size; |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 949 | em->bdev = root->fs_info->fs_devices->latest_bdev; |
Chris Mason | 7f3c74f | 2008-07-18 12:01:11 -0400 | [diff] [blame] | 950 | set_bit(EXTENT_FLAG_PINNED, &em->flags); |
Josef Bacik | 70c8a91 | 2012-10-11 16:54:30 -0400 | [diff] [blame] | 951 | em->generation = -1; |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 952 | |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 953 | while (1) { |
Chris Mason | 890871b | 2009-09-02 16:24:52 -0400 | [diff] [blame] | 954 | write_lock(&em_tree->lock); |
Josef Bacik | 09a2a8f9 | 2013-04-05 16:51:15 -0400 | [diff] [blame] | 955 | ret = add_extent_mapping(em_tree, em, 1); |
Chris Mason | 890871b | 2009-09-02 16:24:52 -0400 | [diff] [blame] | 956 | write_unlock(&em_tree->lock); |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 957 | if (ret != -EEXIST) { |
| 958 | free_extent_map(em); |
| 959 | break; |
| 960 | } |
| 961 | btrfs_drop_extent_cache(inode, start, |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 962 | start + ram_size - 1, 0); |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 963 | } |
Liu Bo | ace68ba | 2013-04-22 10:53:47 +0000 | [diff] [blame] | 964 | if (ret) |
| 965 | goto out_reserve; |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 966 | |
Chris Mason | 98d20f6 | 2008-04-14 09:46:10 -0400 | [diff] [blame] | 967 | cur_alloc_size = ins.offset; |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 968 | ret = btrfs_add_ordered_extent(inode, start, ins.objectid, |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 969 | ram_size, cur_alloc_size, 0); |
Liu Bo | ace68ba | 2013-04-22 10:53:47 +0000 | [diff] [blame] | 970 | if (ret) |
| 971 | goto out_reserve; |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 972 | |
Yan Zheng | 17d217f | 2008-12-12 10:03:38 -0500 | [diff] [blame] | 973 | if (root->root_key.objectid == |
| 974 | BTRFS_DATA_RELOC_TREE_OBJECTID) { |
| 975 | ret = btrfs_reloc_clone_csums(inode, start, |
| 976 | cur_alloc_size); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 977 | if (ret) { |
| 978 | btrfs_abort_transaction(trans, root, ret); |
Liu Bo | ace68ba | 2013-04-22 10:53:47 +0000 | [diff] [blame] | 979 | goto out_reserve; |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 980 | } |
Yan Zheng | 17d217f | 2008-12-12 10:03:38 -0500 | [diff] [blame] | 981 | } |
| 982 | |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 983 | if (disk_num_bytes < cur_alloc_size) |
Chris Mason | 3b95151 | 2008-04-17 11:29:12 -0400 | [diff] [blame] | 984 | break; |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 985 | |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 986 | /* we're not doing compressed IO, don't unlock the first |
| 987 | * page (which the caller expects to stay locked), don't |
| 988 | * clear any dirty bits and don't set any writeback bits |
Chris Mason | 8b62b72 | 2009-09-02 16:53:46 -0400 | [diff] [blame] | 989 | * |
| 990 | * Do set the Private2 bit so we know this page was properly |
| 991 | * setup for writepage |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 992 | */ |
Chris Mason | a791e35 | 2009-10-08 11:27:10 -0400 | [diff] [blame] | 993 | op = unlock ? EXTENT_CLEAR_UNLOCK_PAGE : 0; |
| 994 | op |= EXTENT_CLEAR_UNLOCK | EXTENT_CLEAR_DELALLOC | |
| 995 | EXTENT_SET_PRIVATE2; |
| 996 | |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 997 | extent_clear_unlock_delalloc(inode, &BTRFS_I(inode)->io_tree, |
| 998 | start, start + ram_size - 1, |
Chris Mason | a791e35 | 2009-10-08 11:27:10 -0400 | [diff] [blame] | 999 | locked_page, op); |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 1000 | disk_num_bytes -= cur_alloc_size; |
Chris Mason | c59f895 | 2007-12-17 20:14:04 -0500 | [diff] [blame] | 1001 | num_bytes -= cur_alloc_size; |
| 1002 | alloc_hint = ins.objectid + ins.offset; |
| 1003 | start += cur_alloc_size; |
Chris Mason | b888db2 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 1004 | } |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 1005 | out: |
Chris Mason | be20aa9 | 2007-12-17 20:14:01 -0500 | [diff] [blame] | 1006 | return ret; |
Miao Xie | b7d5b0a | 2012-11-01 07:32:18 +0000 | [diff] [blame] | 1007 | |
Liu Bo | ace68ba | 2013-04-22 10:53:47 +0000 | [diff] [blame] | 1008 | out_reserve: |
| 1009 | btrfs_free_reserved_extent(root, ins.objectid, ins.offset); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 1010 | out_unlock: |
| 1011 | extent_clear_unlock_delalloc(inode, |
| 1012 | &BTRFS_I(inode)->io_tree, |
Josef Bacik | beb42dd | 2012-05-30 15:35:17 -0400 | [diff] [blame] | 1013 | start, end, locked_page, |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 1014 | EXTENT_CLEAR_UNLOCK_PAGE | |
| 1015 | EXTENT_CLEAR_UNLOCK | |
| 1016 | EXTENT_CLEAR_DELALLOC | |
| 1017 | EXTENT_CLEAR_DIRTY | |
| 1018 | EXTENT_SET_WRITEBACK | |
| 1019 | EXTENT_END_WRITEBACK); |
| 1020 | |
| 1021 | goto out; |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 1022 | } |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 1023 | |
Miao Xie | b7d5b0a | 2012-11-01 07:32:18 +0000 | [diff] [blame] | 1024 | static noinline int cow_file_range(struct inode *inode, |
| 1025 | struct page *locked_page, |
| 1026 | u64 start, u64 end, int *page_started, |
| 1027 | unsigned long *nr_written, |
| 1028 | int unlock) |
| 1029 | { |
| 1030 | struct btrfs_trans_handle *trans; |
| 1031 | struct btrfs_root *root = BTRFS_I(inode)->root; |
| 1032 | int ret; |
| 1033 | |
| 1034 | trans = btrfs_join_transaction(root); |
| 1035 | if (IS_ERR(trans)) { |
| 1036 | extent_clear_unlock_delalloc(inode, |
| 1037 | &BTRFS_I(inode)->io_tree, |
| 1038 | start, end, locked_page, |
| 1039 | EXTENT_CLEAR_UNLOCK_PAGE | |
| 1040 | EXTENT_CLEAR_UNLOCK | |
| 1041 | EXTENT_CLEAR_DELALLOC | |
| 1042 | EXTENT_CLEAR_DIRTY | |
| 1043 | EXTENT_SET_WRITEBACK | |
| 1044 | EXTENT_END_WRITEBACK); |
| 1045 | return PTR_ERR(trans); |
| 1046 | } |
| 1047 | trans->block_rsv = &root->fs_info->delalloc_block_rsv; |
| 1048 | |
| 1049 | ret = __cow_file_range(trans, inode, root, locked_page, start, end, |
| 1050 | page_started, nr_written, unlock); |
| 1051 | |
| 1052 | btrfs_end_transaction(trans, root); |
| 1053 | |
| 1054 | return ret; |
| 1055 | } |
| 1056 | |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 1057 | /* |
| 1058 | * work queue call back to started compression on a file and pages |
| 1059 | */ |
| 1060 | static noinline void async_cow_start(struct btrfs_work *work) |
| 1061 | { |
| 1062 | struct async_cow *async_cow; |
| 1063 | int num_added = 0; |
| 1064 | async_cow = container_of(work, struct async_cow, work); |
| 1065 | |
| 1066 | compress_file_range(async_cow->inode, async_cow->locked_page, |
| 1067 | async_cow->start, async_cow->end, async_cow, |
| 1068 | &num_added); |
Josef Bacik | 8180ef8 | 2012-06-08 15:16:12 -0400 | [diff] [blame] | 1069 | if (num_added == 0) { |
Josef Bacik | cb77fcd | 2012-06-15 12:19:48 -0600 | [diff] [blame] | 1070 | btrfs_add_delayed_iput(async_cow->inode); |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 1071 | async_cow->inode = NULL; |
Josef Bacik | 8180ef8 | 2012-06-08 15:16:12 -0400 | [diff] [blame] | 1072 | } |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 1073 | } |
| 1074 | |
| 1075 | /* |
| 1076 | * work queue call back to submit previously compressed pages |
| 1077 | */ |
| 1078 | static noinline void async_cow_submit(struct btrfs_work *work) |
| 1079 | { |
| 1080 | struct async_cow *async_cow; |
| 1081 | struct btrfs_root *root; |
| 1082 | unsigned long nr_pages; |
| 1083 | |
| 1084 | async_cow = container_of(work, struct async_cow, work); |
| 1085 | |
| 1086 | root = async_cow->root; |
| 1087 | nr_pages = (async_cow->end - async_cow->start + PAGE_CACHE_SIZE) >> |
| 1088 | PAGE_CACHE_SHIFT; |
| 1089 | |
Josef Bacik | 66657b3 | 2012-08-01 15:36:24 -0400 | [diff] [blame] | 1090 | if (atomic_sub_return(nr_pages, &root->fs_info->async_delalloc_pages) < |
Liu Bo | 287082b | 2012-06-28 04:02:24 -0600 | [diff] [blame] | 1091 | 5 * 1024 * 1024 && |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 1092 | waitqueue_active(&root->fs_info->async_submit_wait)) |
| 1093 | wake_up(&root->fs_info->async_submit_wait); |
| 1094 | |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 1095 | if (async_cow->inode) |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 1096 | submit_compressed_extents(async_cow->inode, async_cow); |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 1097 | } |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 1098 | |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 1099 | static noinline void async_cow_free(struct btrfs_work *work) |
| 1100 | { |
| 1101 | struct async_cow *async_cow; |
| 1102 | async_cow = container_of(work, struct async_cow, work); |
Josef Bacik | 8180ef8 | 2012-06-08 15:16:12 -0400 | [diff] [blame] | 1103 | if (async_cow->inode) |
Josef Bacik | cb77fcd | 2012-06-15 12:19:48 -0600 | [diff] [blame] | 1104 | btrfs_add_delayed_iput(async_cow->inode); |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 1105 | kfree(async_cow); |
| 1106 | } |
| 1107 | |
| 1108 | static int cow_file_range_async(struct inode *inode, struct page *locked_page, |
| 1109 | u64 start, u64 end, int *page_started, |
| 1110 | unsigned long *nr_written) |
| 1111 | { |
| 1112 | struct async_cow *async_cow; |
| 1113 | struct btrfs_root *root = BTRFS_I(inode)->root; |
| 1114 | unsigned long nr_pages; |
| 1115 | u64 cur_end; |
Liu Bo | 287082b | 2012-06-28 04:02:24 -0600 | [diff] [blame] | 1116 | int limit = 10 * 1024 * 1024; |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 1117 | |
Chris Mason | a3429ab | 2009-10-08 12:30:20 -0400 | [diff] [blame] | 1118 | clear_extent_bit(&BTRFS_I(inode)->io_tree, start, end, EXTENT_LOCKED, |
| 1119 | 1, 0, NULL, GFP_NOFS); |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 1120 | while (start < end) { |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 1121 | async_cow = kmalloc(sizeof(*async_cow), GFP_NOFS); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 1122 | BUG_ON(!async_cow); /* -ENOMEM */ |
Josef Bacik | 8180ef8 | 2012-06-08 15:16:12 -0400 | [diff] [blame] | 1123 | async_cow->inode = igrab(inode); |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 1124 | async_cow->root = root; |
| 1125 | async_cow->locked_page = locked_page; |
| 1126 | async_cow->start = start; |
| 1127 | |
Christoph Hellwig | 6cbff00 | 2009-04-17 10:37:41 +0200 | [diff] [blame] | 1128 | if (BTRFS_I(inode)->flags & BTRFS_INODE_NOCOMPRESS) |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 1129 | cur_end = end; |
| 1130 | else |
| 1131 | cur_end = min(end, start + 512 * 1024 - 1); |
| 1132 | |
| 1133 | async_cow->end = cur_end; |
| 1134 | INIT_LIST_HEAD(&async_cow->extents); |
| 1135 | |
| 1136 | async_cow->work.func = async_cow_start; |
| 1137 | async_cow->work.ordered_func = async_cow_submit; |
| 1138 | async_cow->work.ordered_free = async_cow_free; |
| 1139 | async_cow->work.flags = 0; |
| 1140 | |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 1141 | nr_pages = (cur_end - start + PAGE_CACHE_SIZE) >> |
| 1142 | PAGE_CACHE_SHIFT; |
| 1143 | atomic_add(nr_pages, &root->fs_info->async_delalloc_pages); |
| 1144 | |
| 1145 | btrfs_queue_worker(&root->fs_info->delalloc_workers, |
| 1146 | &async_cow->work); |
| 1147 | |
| 1148 | if (atomic_read(&root->fs_info->async_delalloc_pages) > limit) { |
| 1149 | wait_event(root->fs_info->async_submit_wait, |
| 1150 | (atomic_read(&root->fs_info->async_delalloc_pages) < |
| 1151 | limit)); |
| 1152 | } |
| 1153 | |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 1154 | while (atomic_read(&root->fs_info->async_submit_draining) && |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 1155 | atomic_read(&root->fs_info->async_delalloc_pages)) { |
| 1156 | wait_event(root->fs_info->async_submit_wait, |
| 1157 | (atomic_read(&root->fs_info->async_delalloc_pages) == |
| 1158 | 0)); |
| 1159 | } |
| 1160 | |
| 1161 | *nr_written += nr_pages; |
| 1162 | start = cur_end + 1; |
| 1163 | } |
| 1164 | *page_started = 1; |
| 1165 | return 0; |
Chris Mason | be20aa9 | 2007-12-17 20:14:01 -0500 | [diff] [blame] | 1166 | } |
| 1167 | |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 1168 | static noinline int csum_exist_in_range(struct btrfs_root *root, |
Yan Zheng | 17d217f | 2008-12-12 10:03:38 -0500 | [diff] [blame] | 1169 | u64 bytenr, u64 num_bytes) |
| 1170 | { |
| 1171 | int ret; |
| 1172 | struct btrfs_ordered_sum *sums; |
| 1173 | LIST_HEAD(list); |
| 1174 | |
Yan Zheng | 07d400a | 2009-01-06 11:42:00 -0500 | [diff] [blame] | 1175 | ret = btrfs_lookup_csums_range(root->fs_info->csum_root, bytenr, |
Arne Jansen | a2de733 | 2011-03-08 14:14:00 +0100 | [diff] [blame] | 1176 | bytenr + num_bytes - 1, &list, 0); |
Yan Zheng | 17d217f | 2008-12-12 10:03:38 -0500 | [diff] [blame] | 1177 | if (ret == 0 && list_empty(&list)) |
| 1178 | return 0; |
| 1179 | |
| 1180 | while (!list_empty(&list)) { |
| 1181 | sums = list_entry(list.next, struct btrfs_ordered_sum, list); |
| 1182 | list_del(&sums->list); |
| 1183 | kfree(sums); |
| 1184 | } |
| 1185 | return 1; |
| 1186 | } |
| 1187 | |
Chris Mason | d352ac6 | 2008-09-29 15:18:18 -0400 | [diff] [blame] | 1188 | /* |
| 1189 | * when nowcow writeback call back. This checks for snapshots or COW copies |
| 1190 | * of the extents that exist in the file, and COWs the file as required. |
| 1191 | * |
| 1192 | * If no cow copies or snapshots exist, we write directly to the existing |
| 1193 | * blocks on disk |
| 1194 | */ |
Chris Mason | 7f366cf | 2009-03-12 20:12:45 -0400 | [diff] [blame] | 1195 | static noinline int run_delalloc_nocow(struct inode *inode, |
| 1196 | struct page *locked_page, |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 1197 | u64 start, u64 end, int *page_started, int force, |
| 1198 | unsigned long *nr_written) |
Chris Mason | be20aa9 | 2007-12-17 20:14:01 -0500 | [diff] [blame] | 1199 | { |
Chris Mason | be20aa9 | 2007-12-17 20:14:01 -0500 | [diff] [blame] | 1200 | struct btrfs_root *root = BTRFS_I(inode)->root; |
Yan Zheng | 7ea394f | 2008-08-05 13:05:02 -0400 | [diff] [blame] | 1201 | struct btrfs_trans_handle *trans; |
Chris Mason | be20aa9 | 2007-12-17 20:14:01 -0500 | [diff] [blame] | 1202 | struct extent_buffer *leaf; |
Chris Mason | be20aa9 | 2007-12-17 20:14:01 -0500 | [diff] [blame] | 1203 | struct btrfs_path *path; |
Yan Zheng | 80ff385 | 2008-10-30 14:20:02 -0400 | [diff] [blame] | 1204 | struct btrfs_file_extent_item *fi; |
Chris Mason | be20aa9 | 2007-12-17 20:14:01 -0500 | [diff] [blame] | 1205 | struct btrfs_key found_key; |
Yan Zheng | 80ff385 | 2008-10-30 14:20:02 -0400 | [diff] [blame] | 1206 | u64 cow_start; |
| 1207 | u64 cur_offset; |
| 1208 | u64 extent_end; |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 1209 | u64 extent_offset; |
Yan Zheng | 80ff385 | 2008-10-30 14:20:02 -0400 | [diff] [blame] | 1210 | u64 disk_bytenr; |
| 1211 | u64 num_bytes; |
Josef Bacik | b493968 | 2012-12-03 10:31:19 -0500 | [diff] [blame] | 1212 | u64 disk_num_bytes; |
Josef Bacik | cc95bef | 2013-04-04 14:31:27 -0400 | [diff] [blame] | 1213 | u64 ram_bytes; |
Yan Zheng | 80ff385 | 2008-10-30 14:20:02 -0400 | [diff] [blame] | 1214 | int extent_type; |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 1215 | int ret, err; |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 1216 | int type; |
Yan Zheng | 80ff385 | 2008-10-30 14:20:02 -0400 | [diff] [blame] | 1217 | int nocow; |
| 1218 | int check_prev = 1; |
Li Zefan | 82d5902 | 2011-04-20 10:33:24 +0800 | [diff] [blame] | 1219 | bool nolock; |
Li Zefan | 33345d01 | 2011-04-20 10:31:50 +0800 | [diff] [blame] | 1220 | u64 ino = btrfs_ino(inode); |
Chris Mason | be20aa9 | 2007-12-17 20:14:01 -0500 | [diff] [blame] | 1221 | |
| 1222 | path = btrfs_alloc_path(); |
Josef Bacik | 17ca04a | 2012-05-31 15:58:55 -0400 | [diff] [blame] | 1223 | if (!path) { |
| 1224 | extent_clear_unlock_delalloc(inode, |
| 1225 | &BTRFS_I(inode)->io_tree, |
| 1226 | start, end, locked_page, |
| 1227 | EXTENT_CLEAR_UNLOCK_PAGE | |
| 1228 | EXTENT_CLEAR_UNLOCK | |
| 1229 | EXTENT_CLEAR_DELALLOC | |
| 1230 | EXTENT_CLEAR_DIRTY | |
| 1231 | EXTENT_SET_WRITEBACK | |
| 1232 | EXTENT_END_WRITEBACK); |
Mark Fasheh | d8926bb | 2011-07-13 10:38:47 -0700 | [diff] [blame] | 1233 | return -ENOMEM; |
Josef Bacik | 17ca04a | 2012-05-31 15:58:55 -0400 | [diff] [blame] | 1234 | } |
Li Zefan | 82d5902 | 2011-04-20 10:33:24 +0800 | [diff] [blame] | 1235 | |
Liu Bo | 83eea1f | 2012-07-10 05:28:39 -0600 | [diff] [blame] | 1236 | nolock = btrfs_is_free_space_inode(inode); |
Li Zefan | 82d5902 | 2011-04-20 10:33:24 +0800 | [diff] [blame] | 1237 | |
| 1238 | if (nolock) |
Josef Bacik | 7a7eaa4 | 2011-04-13 12:54:33 -0400 | [diff] [blame] | 1239 | trans = btrfs_join_transaction_nolock(root); |
Li Zefan | 82d5902 | 2011-04-20 10:33:24 +0800 | [diff] [blame] | 1240 | else |
Josef Bacik | 7a7eaa4 | 2011-04-13 12:54:33 -0400 | [diff] [blame] | 1241 | trans = btrfs_join_transaction(root); |
Chris Mason | ff5714c | 2011-05-28 07:00:39 -0400 | [diff] [blame] | 1242 | |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 1243 | if (IS_ERR(trans)) { |
Josef Bacik | 17ca04a | 2012-05-31 15:58:55 -0400 | [diff] [blame] | 1244 | extent_clear_unlock_delalloc(inode, |
| 1245 | &BTRFS_I(inode)->io_tree, |
| 1246 | start, end, locked_page, |
| 1247 | EXTENT_CLEAR_UNLOCK_PAGE | |
| 1248 | EXTENT_CLEAR_UNLOCK | |
| 1249 | EXTENT_CLEAR_DELALLOC | |
| 1250 | EXTENT_CLEAR_DIRTY | |
| 1251 | EXTENT_SET_WRITEBACK | |
| 1252 | EXTENT_END_WRITEBACK); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 1253 | btrfs_free_path(path); |
| 1254 | return PTR_ERR(trans); |
| 1255 | } |
| 1256 | |
Josef Bacik | 74b2107 | 2011-04-13 12:02:53 -0400 | [diff] [blame] | 1257 | trans->block_rsv = &root->fs_info->delalloc_block_rsv; |
Chris Mason | be20aa9 | 2007-12-17 20:14:01 -0500 | [diff] [blame] | 1258 | |
Yan Zheng | 80ff385 | 2008-10-30 14:20:02 -0400 | [diff] [blame] | 1259 | cow_start = (u64)-1; |
| 1260 | cur_offset = start; |
| 1261 | while (1) { |
Li Zefan | 33345d01 | 2011-04-20 10:31:50 +0800 | [diff] [blame] | 1262 | ret = btrfs_lookup_file_extent(trans, root, path, ino, |
Yan Zheng | 80ff385 | 2008-10-30 14:20:02 -0400 | [diff] [blame] | 1263 | cur_offset, 0); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 1264 | if (ret < 0) { |
| 1265 | btrfs_abort_transaction(trans, root, ret); |
| 1266 | goto error; |
| 1267 | } |
Yan Zheng | 80ff385 | 2008-10-30 14:20:02 -0400 | [diff] [blame] | 1268 | if (ret > 0 && path->slots[0] > 0 && check_prev) { |
| 1269 | leaf = path->nodes[0]; |
| 1270 | btrfs_item_key_to_cpu(leaf, &found_key, |
| 1271 | path->slots[0] - 1); |
Li Zefan | 33345d01 | 2011-04-20 10:31:50 +0800 | [diff] [blame] | 1272 | if (found_key.objectid == ino && |
Yan Zheng | 80ff385 | 2008-10-30 14:20:02 -0400 | [diff] [blame] | 1273 | found_key.type == BTRFS_EXTENT_DATA_KEY) |
| 1274 | path->slots[0]--; |
| 1275 | } |
| 1276 | check_prev = 0; |
| 1277 | next_slot: |
| 1278 | leaf = path->nodes[0]; |
| 1279 | if (path->slots[0] >= btrfs_header_nritems(leaf)) { |
| 1280 | ret = btrfs_next_leaf(root, path); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 1281 | if (ret < 0) { |
| 1282 | btrfs_abort_transaction(trans, root, ret); |
| 1283 | goto error; |
| 1284 | } |
Yan Zheng | 80ff385 | 2008-10-30 14:20:02 -0400 | [diff] [blame] | 1285 | if (ret > 0) |
| 1286 | break; |
| 1287 | leaf = path->nodes[0]; |
| 1288 | } |
Chris Mason | be20aa9 | 2007-12-17 20:14:01 -0500 | [diff] [blame] | 1289 | |
Yan Zheng | 80ff385 | 2008-10-30 14:20:02 -0400 | [diff] [blame] | 1290 | nocow = 0; |
| 1291 | disk_bytenr = 0; |
Yan Zheng | 17d217f | 2008-12-12 10:03:38 -0500 | [diff] [blame] | 1292 | num_bytes = 0; |
Yan Zheng | 80ff385 | 2008-10-30 14:20:02 -0400 | [diff] [blame] | 1293 | btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]); |
Chris Mason | be20aa9 | 2007-12-17 20:14:01 -0500 | [diff] [blame] | 1294 | |
Li Zefan | 33345d01 | 2011-04-20 10:31:50 +0800 | [diff] [blame] | 1295 | if (found_key.objectid > ino || |
Yan Zheng | 80ff385 | 2008-10-30 14:20:02 -0400 | [diff] [blame] | 1296 | found_key.type > BTRFS_EXTENT_DATA_KEY || |
| 1297 | found_key.offset > end) |
| 1298 | break; |
Chris Mason | be20aa9 | 2007-12-17 20:14:01 -0500 | [diff] [blame] | 1299 | |
Yan Zheng | 80ff385 | 2008-10-30 14:20:02 -0400 | [diff] [blame] | 1300 | if (found_key.offset > cur_offset) { |
| 1301 | extent_end = found_key.offset; |
Chris Mason | e9061e2 | 2009-10-09 09:57:45 -0400 | [diff] [blame] | 1302 | extent_type = 0; |
Yan Zheng | 80ff385 | 2008-10-30 14:20:02 -0400 | [diff] [blame] | 1303 | goto out_check; |
| 1304 | } |
Chris Mason | c31f883 | 2008-01-08 15:46:31 -0500 | [diff] [blame] | 1305 | |
Yan Zheng | 80ff385 | 2008-10-30 14:20:02 -0400 | [diff] [blame] | 1306 | fi = btrfs_item_ptr(leaf, path->slots[0], |
| 1307 | struct btrfs_file_extent_item); |
| 1308 | extent_type = btrfs_file_extent_type(leaf, fi); |
Chris Mason | be20aa9 | 2007-12-17 20:14:01 -0500 | [diff] [blame] | 1309 | |
Josef Bacik | cc95bef | 2013-04-04 14:31:27 -0400 | [diff] [blame] | 1310 | ram_bytes = btrfs_file_extent_ram_bytes(leaf, fi); |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 1311 | if (extent_type == BTRFS_FILE_EXTENT_REG || |
| 1312 | extent_type == BTRFS_FILE_EXTENT_PREALLOC) { |
Yan Zheng | 80ff385 | 2008-10-30 14:20:02 -0400 | [diff] [blame] | 1313 | disk_bytenr = btrfs_file_extent_disk_bytenr(leaf, fi); |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 1314 | extent_offset = btrfs_file_extent_offset(leaf, fi); |
Yan Zheng | 80ff385 | 2008-10-30 14:20:02 -0400 | [diff] [blame] | 1315 | extent_end = found_key.offset + |
| 1316 | btrfs_file_extent_num_bytes(leaf, fi); |
Josef Bacik | b493968 | 2012-12-03 10:31:19 -0500 | [diff] [blame] | 1317 | disk_num_bytes = |
| 1318 | btrfs_file_extent_disk_num_bytes(leaf, fi); |
Yan Zheng | 80ff385 | 2008-10-30 14:20:02 -0400 | [diff] [blame] | 1319 | if (extent_end <= start) { |
| 1320 | path->slots[0]++; |
| 1321 | goto next_slot; |
| 1322 | } |
Yan Zheng | 17d217f | 2008-12-12 10:03:38 -0500 | [diff] [blame] | 1323 | if (disk_bytenr == 0) |
| 1324 | goto out_check; |
Yan Zheng | 80ff385 | 2008-10-30 14:20:02 -0400 | [diff] [blame] | 1325 | if (btrfs_file_extent_compression(leaf, fi) || |
| 1326 | btrfs_file_extent_encryption(leaf, fi) || |
| 1327 | btrfs_file_extent_other_encoding(leaf, fi)) |
| 1328 | goto out_check; |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 1329 | if (extent_type == BTRFS_FILE_EXTENT_REG && !force) |
| 1330 | goto out_check; |
Yan Zheng | d2fb343 | 2008-12-11 16:30:39 -0500 | [diff] [blame] | 1331 | if (btrfs_extent_readonly(root, disk_bytenr)) |
Yan Zheng | 80ff385 | 2008-10-30 14:20:02 -0400 | [diff] [blame] | 1332 | goto out_check; |
Li Zefan | 33345d01 | 2011-04-20 10:31:50 +0800 | [diff] [blame] | 1333 | if (btrfs_cross_ref_exist(trans, root, ino, |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 1334 | found_key.offset - |
| 1335 | extent_offset, disk_bytenr)) |
Yan Zheng | 17d217f | 2008-12-12 10:03:38 -0500 | [diff] [blame] | 1336 | goto out_check; |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 1337 | disk_bytenr += extent_offset; |
Yan Zheng | 17d217f | 2008-12-12 10:03:38 -0500 | [diff] [blame] | 1338 | disk_bytenr += cur_offset - found_key.offset; |
| 1339 | num_bytes = min(end + 1, extent_end) - cur_offset; |
| 1340 | /* |
| 1341 | * force cow if csum exists in the range. |
| 1342 | * this ensure that csum for a given extent are |
| 1343 | * either valid or do not exist. |
| 1344 | */ |
| 1345 | if (csum_exist_in_range(root, disk_bytenr, num_bytes)) |
| 1346 | goto out_check; |
Yan Zheng | 80ff385 | 2008-10-30 14:20:02 -0400 | [diff] [blame] | 1347 | nocow = 1; |
| 1348 | } else if (extent_type == BTRFS_FILE_EXTENT_INLINE) { |
| 1349 | extent_end = found_key.offset + |
| 1350 | btrfs_file_extent_inline_len(leaf, fi); |
| 1351 | extent_end = ALIGN(extent_end, root->sectorsize); |
| 1352 | } else { |
| 1353 | BUG_ON(1); |
| 1354 | } |
| 1355 | out_check: |
| 1356 | if (extent_end <= start) { |
| 1357 | path->slots[0]++; |
| 1358 | goto next_slot; |
| 1359 | } |
| 1360 | if (!nocow) { |
| 1361 | if (cow_start == (u64)-1) |
| 1362 | cow_start = cur_offset; |
| 1363 | cur_offset = extent_end; |
| 1364 | if (cur_offset > end) |
| 1365 | break; |
| 1366 | path->slots[0]++; |
| 1367 | goto next_slot; |
Yan Zheng | 7ea394f | 2008-08-05 13:05:02 -0400 | [diff] [blame] | 1368 | } |
Chris Mason | be20aa9 | 2007-12-17 20:14:01 -0500 | [diff] [blame] | 1369 | |
David Sterba | b3b4aa7 | 2011-04-21 01:20:15 +0200 | [diff] [blame] | 1370 | btrfs_release_path(path); |
Yan Zheng | 80ff385 | 2008-10-30 14:20:02 -0400 | [diff] [blame] | 1371 | if (cow_start != (u64)-1) { |
Miao Xie | b7d5b0a | 2012-11-01 07:32:18 +0000 | [diff] [blame] | 1372 | ret = __cow_file_range(trans, inode, root, locked_page, |
| 1373 | cow_start, found_key.offset - 1, |
| 1374 | page_started, nr_written, 1); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 1375 | if (ret) { |
| 1376 | btrfs_abort_transaction(trans, root, ret); |
| 1377 | goto error; |
| 1378 | } |
Yan Zheng | 80ff385 | 2008-10-30 14:20:02 -0400 | [diff] [blame] | 1379 | cow_start = (u64)-1; |
Yan Zheng | 7ea394f | 2008-08-05 13:05:02 -0400 | [diff] [blame] | 1380 | } |
Yan Zheng | 80ff385 | 2008-10-30 14:20:02 -0400 | [diff] [blame] | 1381 | |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 1382 | if (extent_type == BTRFS_FILE_EXTENT_PREALLOC) { |
| 1383 | struct extent_map *em; |
| 1384 | struct extent_map_tree *em_tree; |
| 1385 | em_tree = &BTRFS_I(inode)->extent_tree; |
David Sterba | 172ddd6 | 2011-04-21 00:48:27 +0200 | [diff] [blame] | 1386 | em = alloc_extent_map(); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 1387 | BUG_ON(!em); /* -ENOMEM */ |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 1388 | em->start = cur_offset; |
Josef Bacik | 70c8a91 | 2012-10-11 16:54:30 -0400 | [diff] [blame] | 1389 | em->orig_start = found_key.offset - extent_offset; |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 1390 | em->len = num_bytes; |
| 1391 | em->block_len = num_bytes; |
| 1392 | em->block_start = disk_bytenr; |
Josef Bacik | b493968 | 2012-12-03 10:31:19 -0500 | [diff] [blame] | 1393 | em->orig_block_len = disk_num_bytes; |
Josef Bacik | cc95bef | 2013-04-04 14:31:27 -0400 | [diff] [blame] | 1394 | em->ram_bytes = ram_bytes; |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 1395 | em->bdev = root->fs_info->fs_devices->latest_bdev; |
Josef Bacik | 2ab28f3 | 2012-10-12 15:27:49 -0400 | [diff] [blame] | 1396 | em->mod_start = em->start; |
| 1397 | em->mod_len = em->len; |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 1398 | set_bit(EXTENT_FLAG_PINNED, &em->flags); |
Josef Bacik | b11e234 | 2012-12-03 10:58:15 -0500 | [diff] [blame] | 1399 | set_bit(EXTENT_FLAG_FILLING, &em->flags); |
Josef Bacik | 70c8a91 | 2012-10-11 16:54:30 -0400 | [diff] [blame] | 1400 | em->generation = -1; |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 1401 | while (1) { |
Chris Mason | 890871b | 2009-09-02 16:24:52 -0400 | [diff] [blame] | 1402 | write_lock(&em_tree->lock); |
Josef Bacik | 09a2a8f9 | 2013-04-05 16:51:15 -0400 | [diff] [blame] | 1403 | ret = add_extent_mapping(em_tree, em, 1); |
Chris Mason | 890871b | 2009-09-02 16:24:52 -0400 | [diff] [blame] | 1404 | write_unlock(&em_tree->lock); |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 1405 | if (ret != -EEXIST) { |
| 1406 | free_extent_map(em); |
| 1407 | break; |
| 1408 | } |
| 1409 | btrfs_drop_extent_cache(inode, em->start, |
| 1410 | em->start + em->len - 1, 0); |
| 1411 | } |
| 1412 | type = BTRFS_ORDERED_PREALLOC; |
| 1413 | } else { |
| 1414 | type = BTRFS_ORDERED_NOCOW; |
| 1415 | } |
Yan Zheng | 80ff385 | 2008-10-30 14:20:02 -0400 | [diff] [blame] | 1416 | |
| 1417 | ret = btrfs_add_ordered_extent(inode, cur_offset, disk_bytenr, |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 1418 | num_bytes, num_bytes, type); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 1419 | BUG_ON(ret); /* -ENOMEM */ |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 1420 | |
Yan, Zheng | efa5646 | 2010-05-16 10:49:59 -0400 | [diff] [blame] | 1421 | if (root->root_key.objectid == |
| 1422 | BTRFS_DATA_RELOC_TREE_OBJECTID) { |
| 1423 | ret = btrfs_reloc_clone_csums(inode, cur_offset, |
| 1424 | num_bytes); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 1425 | if (ret) { |
| 1426 | btrfs_abort_transaction(trans, root, ret); |
| 1427 | goto error; |
| 1428 | } |
Yan, Zheng | efa5646 | 2010-05-16 10:49:59 -0400 | [diff] [blame] | 1429 | } |
| 1430 | |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 1431 | extent_clear_unlock_delalloc(inode, &BTRFS_I(inode)->io_tree, |
Chris Mason | a791e35 | 2009-10-08 11:27:10 -0400 | [diff] [blame] | 1432 | cur_offset, cur_offset + num_bytes - 1, |
| 1433 | locked_page, EXTENT_CLEAR_UNLOCK_PAGE | |
| 1434 | EXTENT_CLEAR_UNLOCK | EXTENT_CLEAR_DELALLOC | |
| 1435 | EXTENT_SET_PRIVATE2); |
Yan Zheng | 80ff385 | 2008-10-30 14:20:02 -0400 | [diff] [blame] | 1436 | cur_offset = extent_end; |
| 1437 | if (cur_offset > end) |
| 1438 | break; |
Yan Zheng | 7ea394f | 2008-08-05 13:05:02 -0400 | [diff] [blame] | 1439 | } |
David Sterba | b3b4aa7 | 2011-04-21 01:20:15 +0200 | [diff] [blame] | 1440 | btrfs_release_path(path); |
Yan Zheng | 80ff385 | 2008-10-30 14:20:02 -0400 | [diff] [blame] | 1441 | |
Josef Bacik | 17ca04a | 2012-05-31 15:58:55 -0400 | [diff] [blame] | 1442 | if (cur_offset <= end && cow_start == (u64)-1) { |
Yan Zheng | 80ff385 | 2008-10-30 14:20:02 -0400 | [diff] [blame] | 1443 | cow_start = cur_offset; |
Josef Bacik | 17ca04a | 2012-05-31 15:58:55 -0400 | [diff] [blame] | 1444 | cur_offset = end; |
| 1445 | } |
| 1446 | |
Yan Zheng | 80ff385 | 2008-10-30 14:20:02 -0400 | [diff] [blame] | 1447 | if (cow_start != (u64)-1) { |
Miao Xie | b7d5b0a | 2012-11-01 07:32:18 +0000 | [diff] [blame] | 1448 | ret = __cow_file_range(trans, inode, root, locked_page, |
| 1449 | cow_start, end, |
| 1450 | page_started, nr_written, 1); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 1451 | if (ret) { |
| 1452 | btrfs_abort_transaction(trans, root, ret); |
| 1453 | goto error; |
| 1454 | } |
Yan Zheng | 80ff385 | 2008-10-30 14:20:02 -0400 | [diff] [blame] | 1455 | } |
| 1456 | |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 1457 | error: |
Miao Xie | a698d075 | 2012-09-20 01:51:59 -0600 | [diff] [blame] | 1458 | err = btrfs_end_transaction(trans, root); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 1459 | if (!ret) |
| 1460 | ret = err; |
| 1461 | |
Josef Bacik | 17ca04a | 2012-05-31 15:58:55 -0400 | [diff] [blame] | 1462 | if (ret && cur_offset < end) |
| 1463 | extent_clear_unlock_delalloc(inode, |
| 1464 | &BTRFS_I(inode)->io_tree, |
| 1465 | cur_offset, end, locked_page, |
| 1466 | EXTENT_CLEAR_UNLOCK_PAGE | |
| 1467 | EXTENT_CLEAR_UNLOCK | |
| 1468 | EXTENT_CLEAR_DELALLOC | |
| 1469 | EXTENT_CLEAR_DIRTY | |
| 1470 | EXTENT_SET_WRITEBACK | |
| 1471 | EXTENT_END_WRITEBACK); |
| 1472 | |
Yan Zheng | 7ea394f | 2008-08-05 13:05:02 -0400 | [diff] [blame] | 1473 | btrfs_free_path(path); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 1474 | return ret; |
Chris Mason | be20aa9 | 2007-12-17 20:14:01 -0500 | [diff] [blame] | 1475 | } |
| 1476 | |
Chris Mason | d352ac6 | 2008-09-29 15:18:18 -0400 | [diff] [blame] | 1477 | /* |
| 1478 | * extent_io.c call back to do delayed allocation processing |
| 1479 | */ |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 1480 | static int run_delalloc_range(struct inode *inode, struct page *locked_page, |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 1481 | u64 start, u64 end, int *page_started, |
| 1482 | unsigned long *nr_written) |
Chris Mason | be20aa9 | 2007-12-17 20:14:01 -0500 | [diff] [blame] | 1483 | { |
Chris Mason | be20aa9 | 2007-12-17 20:14:01 -0500 | [diff] [blame] | 1484 | int ret; |
Chris Mason | 7f366cf | 2009-03-12 20:12:45 -0400 | [diff] [blame] | 1485 | struct btrfs_root *root = BTRFS_I(inode)->root; |
Chris Mason | a213501 | 2008-06-25 16:01:30 -0400 | [diff] [blame] | 1486 | |
Josef Bacik | 7ddf5a4 | 2012-06-08 15:26:47 -0400 | [diff] [blame] | 1487 | if (BTRFS_I(inode)->flags & BTRFS_INODE_NODATACOW) { |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 1488 | ret = run_delalloc_nocow(inode, locked_page, start, end, |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 1489 | page_started, 1, nr_written); |
Josef Bacik | 7ddf5a4 | 2012-06-08 15:26:47 -0400 | [diff] [blame] | 1490 | } else if (BTRFS_I(inode)->flags & BTRFS_INODE_PREALLOC) { |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 1491 | ret = run_delalloc_nocow(inode, locked_page, start, end, |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 1492 | page_started, 0, nr_written); |
Josef Bacik | 7ddf5a4 | 2012-06-08 15:26:47 -0400 | [diff] [blame] | 1493 | } else if (!btrfs_test_opt(root, COMPRESS) && |
| 1494 | !(BTRFS_I(inode)->force_compress) && |
| 1495 | !(BTRFS_I(inode)->flags & BTRFS_INODE_COMPRESS)) { |
Chris Mason | 7f366cf | 2009-03-12 20:12:45 -0400 | [diff] [blame] | 1496 | ret = cow_file_range(inode, locked_page, start, end, |
| 1497 | page_started, nr_written, 1); |
Josef Bacik | 7ddf5a4 | 2012-06-08 15:26:47 -0400 | [diff] [blame] | 1498 | } else { |
| 1499 | set_bit(BTRFS_INODE_HAS_ASYNC_EXTENT, |
| 1500 | &BTRFS_I(inode)->runtime_flags); |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 1501 | ret = cow_file_range_async(inode, locked_page, start, end, |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 1502 | page_started, nr_written); |
Josef Bacik | 7ddf5a4 | 2012-06-08 15:26:47 -0400 | [diff] [blame] | 1503 | } |
Chris Mason | b888db2 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 1504 | return ret; |
| 1505 | } |
| 1506 | |
Jeff Mahoney | 1bf8504 | 2011-07-21 16:56:09 +0000 | [diff] [blame] | 1507 | static void btrfs_split_extent_hook(struct inode *inode, |
| 1508 | struct extent_state *orig, u64 split) |
Josef Bacik | 9ed74f2 | 2009-09-11 16:12:44 -0400 | [diff] [blame] | 1509 | { |
Yan, Zheng | 0ca1f7c | 2010-05-16 10:48:47 -0400 | [diff] [blame] | 1510 | /* not delalloc, ignore it */ |
Josef Bacik | 9ed74f2 | 2009-09-11 16:12:44 -0400 | [diff] [blame] | 1511 | if (!(orig->state & EXTENT_DELALLOC)) |
Jeff Mahoney | 1bf8504 | 2011-07-21 16:56:09 +0000 | [diff] [blame] | 1512 | return; |
Josef Bacik | 9ed74f2 | 2009-09-11 16:12:44 -0400 | [diff] [blame] | 1513 | |
Josef Bacik | 9e0baf6 | 2011-07-15 15:16:44 +0000 | [diff] [blame] | 1514 | spin_lock(&BTRFS_I(inode)->lock); |
| 1515 | BTRFS_I(inode)->outstanding_extents++; |
| 1516 | spin_unlock(&BTRFS_I(inode)->lock); |
Josef Bacik | 9ed74f2 | 2009-09-11 16:12:44 -0400 | [diff] [blame] | 1517 | } |
| 1518 | |
| 1519 | /* |
| 1520 | * extent_io.c merge_extent_hook, used to track merged delayed allocation |
| 1521 | * extents so we can keep track of new extents that are just merged onto old |
| 1522 | * extents, such as when we are doing sequential writes, so we can properly |
| 1523 | * account for the metadata space we'll need. |
| 1524 | */ |
Jeff Mahoney | 1bf8504 | 2011-07-21 16:56:09 +0000 | [diff] [blame] | 1525 | static void btrfs_merge_extent_hook(struct inode *inode, |
| 1526 | struct extent_state *new, |
| 1527 | struct extent_state *other) |
Josef Bacik | 9ed74f2 | 2009-09-11 16:12:44 -0400 | [diff] [blame] | 1528 | { |
Josef Bacik | 9ed74f2 | 2009-09-11 16:12:44 -0400 | [diff] [blame] | 1529 | /* not delalloc, ignore it */ |
| 1530 | if (!(other->state & EXTENT_DELALLOC)) |
Jeff Mahoney | 1bf8504 | 2011-07-21 16:56:09 +0000 | [diff] [blame] | 1531 | return; |
Josef Bacik | 9ed74f2 | 2009-09-11 16:12:44 -0400 | [diff] [blame] | 1532 | |
Josef Bacik | 9e0baf6 | 2011-07-15 15:16:44 +0000 | [diff] [blame] | 1533 | spin_lock(&BTRFS_I(inode)->lock); |
| 1534 | BTRFS_I(inode)->outstanding_extents--; |
| 1535 | spin_unlock(&BTRFS_I(inode)->lock); |
Josef Bacik | 9ed74f2 | 2009-09-11 16:12:44 -0400 | [diff] [blame] | 1536 | } |
| 1537 | |
Miao Xie | eb73c1b | 2013-05-15 07:48:22 +0000 | [diff] [blame] | 1538 | static void btrfs_add_delalloc_inodes(struct btrfs_root *root, |
| 1539 | struct inode *inode) |
| 1540 | { |
| 1541 | spin_lock(&root->delalloc_lock); |
| 1542 | if (list_empty(&BTRFS_I(inode)->delalloc_inodes)) { |
| 1543 | list_add_tail(&BTRFS_I(inode)->delalloc_inodes, |
| 1544 | &root->delalloc_inodes); |
| 1545 | set_bit(BTRFS_INODE_IN_DELALLOC_LIST, |
| 1546 | &BTRFS_I(inode)->runtime_flags); |
| 1547 | root->nr_delalloc_inodes++; |
| 1548 | if (root->nr_delalloc_inodes == 1) { |
| 1549 | spin_lock(&root->fs_info->delalloc_root_lock); |
| 1550 | BUG_ON(!list_empty(&root->delalloc_root)); |
| 1551 | list_add_tail(&root->delalloc_root, |
| 1552 | &root->fs_info->delalloc_roots); |
| 1553 | spin_unlock(&root->fs_info->delalloc_root_lock); |
| 1554 | } |
| 1555 | } |
| 1556 | spin_unlock(&root->delalloc_lock); |
| 1557 | } |
| 1558 | |
| 1559 | static void btrfs_del_delalloc_inode(struct btrfs_root *root, |
| 1560 | struct inode *inode) |
| 1561 | { |
| 1562 | spin_lock(&root->delalloc_lock); |
| 1563 | if (!list_empty(&BTRFS_I(inode)->delalloc_inodes)) { |
| 1564 | list_del_init(&BTRFS_I(inode)->delalloc_inodes); |
| 1565 | clear_bit(BTRFS_INODE_IN_DELALLOC_LIST, |
| 1566 | &BTRFS_I(inode)->runtime_flags); |
| 1567 | root->nr_delalloc_inodes--; |
| 1568 | if (!root->nr_delalloc_inodes) { |
| 1569 | spin_lock(&root->fs_info->delalloc_root_lock); |
| 1570 | BUG_ON(list_empty(&root->delalloc_root)); |
| 1571 | list_del_init(&root->delalloc_root); |
| 1572 | spin_unlock(&root->fs_info->delalloc_root_lock); |
| 1573 | } |
| 1574 | } |
| 1575 | spin_unlock(&root->delalloc_lock); |
| 1576 | } |
| 1577 | |
Chris Mason | d352ac6 | 2008-09-29 15:18:18 -0400 | [diff] [blame] | 1578 | /* |
| 1579 | * extent_io.c set_bit_hook, used to track delayed allocation |
| 1580 | * bytes in this file, and to maintain the list of inodes that |
| 1581 | * have pending delalloc work to be done. |
| 1582 | */ |
Jeff Mahoney | 1bf8504 | 2011-07-21 16:56:09 +0000 | [diff] [blame] | 1583 | static void btrfs_set_bit_hook(struct inode *inode, |
David Sterba | 4107488 | 2013-04-29 13:38:46 +0000 | [diff] [blame] | 1584 | struct extent_state *state, unsigned long *bits) |
Chris Mason | 291d673 | 2008-01-29 15:55:23 -0500 | [diff] [blame] | 1585 | { |
Josef Bacik | 9ed74f2 | 2009-09-11 16:12:44 -0400 | [diff] [blame] | 1586 | |
Chris Mason | 75eff68 | 2008-12-15 15:54:40 -0500 | [diff] [blame] | 1587 | /* |
| 1588 | * set_bit and clear bit hooks normally require _irqsave/restore |
Sergei Trofimovich | 27160b6 | 2011-05-20 20:20:32 +0000 | [diff] [blame] | 1589 | * but in this case, we are only testing for the DELALLOC |
Chris Mason | 75eff68 | 2008-12-15 15:54:40 -0500 | [diff] [blame] | 1590 | * bit, which is only set or cleared with irqs on |
| 1591 | */ |
Yan, Zheng | 0ca1f7c | 2010-05-16 10:48:47 -0400 | [diff] [blame] | 1592 | if (!(state->state & EXTENT_DELALLOC) && (*bits & EXTENT_DELALLOC)) { |
Chris Mason | 291d673 | 2008-01-29 15:55:23 -0500 | [diff] [blame] | 1593 | struct btrfs_root *root = BTRFS_I(inode)->root; |
Yan, Zheng | 0ca1f7c | 2010-05-16 10:48:47 -0400 | [diff] [blame] | 1594 | u64 len = state->end + 1 - state->start; |
Liu Bo | 83eea1f | 2012-07-10 05:28:39 -0600 | [diff] [blame] | 1595 | bool do_list = !btrfs_is_free_space_inode(inode); |
Josef Bacik | 9ed74f2 | 2009-09-11 16:12:44 -0400 | [diff] [blame] | 1596 | |
Josef Bacik | 9e0baf6 | 2011-07-15 15:16:44 +0000 | [diff] [blame] | 1597 | if (*bits & EXTENT_FIRST_DELALLOC) { |
Yan, Zheng | 0ca1f7c | 2010-05-16 10:48:47 -0400 | [diff] [blame] | 1598 | *bits &= ~EXTENT_FIRST_DELALLOC; |
Josef Bacik | 9e0baf6 | 2011-07-15 15:16:44 +0000 | [diff] [blame] | 1599 | } else { |
| 1600 | spin_lock(&BTRFS_I(inode)->lock); |
| 1601 | BTRFS_I(inode)->outstanding_extents++; |
| 1602 | spin_unlock(&BTRFS_I(inode)->lock); |
| 1603 | } |
Josef Bacik | 287a0ab | 2010-03-19 18:07:23 +0000 | [diff] [blame] | 1604 | |
Miao Xie | 963d678 | 2013-01-29 10:10:51 +0000 | [diff] [blame] | 1605 | __percpu_counter_add(&root->fs_info->delalloc_bytes, len, |
| 1606 | root->fs_info->delalloc_batch); |
Miao Xie | df0af1a | 2013-01-29 10:11:59 +0000 | [diff] [blame] | 1607 | spin_lock(&BTRFS_I(inode)->lock); |
Yan, Zheng | 0ca1f7c | 2010-05-16 10:48:47 -0400 | [diff] [blame] | 1608 | BTRFS_I(inode)->delalloc_bytes += len; |
Miao Xie | df0af1a | 2013-01-29 10:11:59 +0000 | [diff] [blame] | 1609 | if (do_list && !test_bit(BTRFS_INODE_IN_DELALLOC_LIST, |
Miao Xie | eb73c1b | 2013-05-15 07:48:22 +0000 | [diff] [blame] | 1610 | &BTRFS_I(inode)->runtime_flags)) |
| 1611 | btrfs_add_delalloc_inodes(root, inode); |
Miao Xie | df0af1a | 2013-01-29 10:11:59 +0000 | [diff] [blame] | 1612 | spin_unlock(&BTRFS_I(inode)->lock); |
Chris Mason | 291d673 | 2008-01-29 15:55:23 -0500 | [diff] [blame] | 1613 | } |
Chris Mason | 291d673 | 2008-01-29 15:55:23 -0500 | [diff] [blame] | 1614 | } |
| 1615 | |
Chris Mason | d352ac6 | 2008-09-29 15:18:18 -0400 | [diff] [blame] | 1616 | /* |
| 1617 | * extent_io.c clear_bit_hook, see set_bit_hook for why |
| 1618 | */ |
Jeff Mahoney | 1bf8504 | 2011-07-21 16:56:09 +0000 | [diff] [blame] | 1619 | static void btrfs_clear_bit_hook(struct inode *inode, |
David Sterba | 4107488 | 2013-04-29 13:38:46 +0000 | [diff] [blame] | 1620 | struct extent_state *state, |
| 1621 | unsigned long *bits) |
Chris Mason | 291d673 | 2008-01-29 15:55:23 -0500 | [diff] [blame] | 1622 | { |
Chris Mason | 75eff68 | 2008-12-15 15:54:40 -0500 | [diff] [blame] | 1623 | /* |
| 1624 | * set_bit and clear bit hooks normally require _irqsave/restore |
Sergei Trofimovich | 27160b6 | 2011-05-20 20:20:32 +0000 | [diff] [blame] | 1625 | * but in this case, we are only testing for the DELALLOC |
Chris Mason | 75eff68 | 2008-12-15 15:54:40 -0500 | [diff] [blame] | 1626 | * bit, which is only set or cleared with irqs on |
| 1627 | */ |
Yan, Zheng | 0ca1f7c | 2010-05-16 10:48:47 -0400 | [diff] [blame] | 1628 | if ((state->state & EXTENT_DELALLOC) && (*bits & EXTENT_DELALLOC)) { |
Chris Mason | 291d673 | 2008-01-29 15:55:23 -0500 | [diff] [blame] | 1629 | struct btrfs_root *root = BTRFS_I(inode)->root; |
Yan, Zheng | 0ca1f7c | 2010-05-16 10:48:47 -0400 | [diff] [blame] | 1630 | u64 len = state->end + 1 - state->start; |
Liu Bo | 83eea1f | 2012-07-10 05:28:39 -0600 | [diff] [blame] | 1631 | bool do_list = !btrfs_is_free_space_inode(inode); |
Chris Mason | bcbfce8 | 2008-04-22 13:26:47 -0400 | [diff] [blame] | 1632 | |
Josef Bacik | 9e0baf6 | 2011-07-15 15:16:44 +0000 | [diff] [blame] | 1633 | if (*bits & EXTENT_FIRST_DELALLOC) { |
Yan, Zheng | 0ca1f7c | 2010-05-16 10:48:47 -0400 | [diff] [blame] | 1634 | *bits &= ~EXTENT_FIRST_DELALLOC; |
Josef Bacik | 9e0baf6 | 2011-07-15 15:16:44 +0000 | [diff] [blame] | 1635 | } else if (!(*bits & EXTENT_DO_ACCOUNTING)) { |
| 1636 | spin_lock(&BTRFS_I(inode)->lock); |
| 1637 | BTRFS_I(inode)->outstanding_extents--; |
| 1638 | spin_unlock(&BTRFS_I(inode)->lock); |
| 1639 | } |
Yan, Zheng | 0ca1f7c | 2010-05-16 10:48:47 -0400 | [diff] [blame] | 1640 | |
| 1641 | if (*bits & EXTENT_DO_ACCOUNTING) |
| 1642 | btrfs_delalloc_release_metadata(inode, len); |
| 1643 | |
Josef Bacik | 0cb59c9 | 2010-07-02 12:14:14 -0400 | [diff] [blame] | 1644 | if (root->root_key.objectid != BTRFS_DATA_RELOC_TREE_OBJECTID |
Josef Bacik | 7ee9e44 | 2013-06-21 16:37:03 -0400 | [diff] [blame] | 1645 | && do_list && !(state->state & EXTENT_NORESERVE)) |
Yan, Zheng | 0ca1f7c | 2010-05-16 10:48:47 -0400 | [diff] [blame] | 1646 | btrfs_free_reserved_data_space(inode, len); |
Josef Bacik | 9ed74f2 | 2009-09-11 16:12:44 -0400 | [diff] [blame] | 1647 | |
Miao Xie | 963d678 | 2013-01-29 10:10:51 +0000 | [diff] [blame] | 1648 | __percpu_counter_add(&root->fs_info->delalloc_bytes, -len, |
| 1649 | root->fs_info->delalloc_batch); |
Miao Xie | df0af1a | 2013-01-29 10:11:59 +0000 | [diff] [blame] | 1650 | spin_lock(&BTRFS_I(inode)->lock); |
Yan, Zheng | 0ca1f7c | 2010-05-16 10:48:47 -0400 | [diff] [blame] | 1651 | BTRFS_I(inode)->delalloc_bytes -= len; |
Josef Bacik | 0cb59c9 | 2010-07-02 12:14:14 -0400 | [diff] [blame] | 1652 | if (do_list && BTRFS_I(inode)->delalloc_bytes == 0 && |
Miao Xie | df0af1a | 2013-01-29 10:11:59 +0000 | [diff] [blame] | 1653 | test_bit(BTRFS_INODE_IN_DELALLOC_LIST, |
Miao Xie | eb73c1b | 2013-05-15 07:48:22 +0000 | [diff] [blame] | 1654 | &BTRFS_I(inode)->runtime_flags)) |
| 1655 | btrfs_del_delalloc_inode(root, inode); |
Miao Xie | df0af1a | 2013-01-29 10:11:59 +0000 | [diff] [blame] | 1656 | spin_unlock(&BTRFS_I(inode)->lock); |
Chris Mason | 291d673 | 2008-01-29 15:55:23 -0500 | [diff] [blame] | 1657 | } |
Chris Mason | 291d673 | 2008-01-29 15:55:23 -0500 | [diff] [blame] | 1658 | } |
| 1659 | |
Chris Mason | d352ac6 | 2008-09-29 15:18:18 -0400 | [diff] [blame] | 1660 | /* |
| 1661 | * extent_io.c merge_bio_hook, this must check the chunk tree to make sure |
| 1662 | * we don't create bios that span stripes or chunks |
| 1663 | */ |
David Woodhouse | 64a1670 | 2009-07-15 23:29:37 +0100 | [diff] [blame] | 1664 | 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] | 1665 | size_t size, struct bio *bio, |
| 1666 | unsigned long bio_flags) |
Chris Mason | 239b14b | 2008-03-24 15:02:07 -0400 | [diff] [blame] | 1667 | { |
| 1668 | struct btrfs_root *root = BTRFS_I(page->mapping->host)->root; |
Chris Mason | a62b940 | 2008-10-03 16:31:08 -0400 | [diff] [blame] | 1669 | u64 logical = (u64)bio->bi_sector << 9; |
Chris Mason | 239b14b | 2008-03-24 15:02:07 -0400 | [diff] [blame] | 1670 | u64 length = 0; |
| 1671 | u64 map_length; |
Chris Mason | 239b14b | 2008-03-24 15:02:07 -0400 | [diff] [blame] | 1672 | int ret; |
| 1673 | |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 1674 | if (bio_flags & EXTENT_BIO_COMPRESSED) |
| 1675 | return 0; |
| 1676 | |
Chris Mason | f2d8d74 | 2008-04-21 10:03:05 -0400 | [diff] [blame] | 1677 | length = bio->bi_size; |
Chris Mason | 239b14b | 2008-03-24 15:02:07 -0400 | [diff] [blame] | 1678 | map_length = length; |
David Woodhouse | 64a1670 | 2009-07-15 23:29:37 +0100 | [diff] [blame] | 1679 | ret = btrfs_map_block(root->fs_info, rw, logical, |
Chris Mason | f188591 | 2008-04-09 16:28:12 -0400 | [diff] [blame] | 1680 | &map_length, NULL, 0); |
Stefan Behrens | 3ec706c | 2012-11-05 15:46:42 +0100 | [diff] [blame] | 1681 | /* Will always return 0 with map_multi == NULL */ |
Jeff Mahoney | 3444a97 | 2011-10-03 23:23:13 -0400 | [diff] [blame] | 1682 | BUG_ON(ret < 0); |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 1683 | if (map_length < length + size) |
Chris Mason | 239b14b | 2008-03-24 15:02:07 -0400 | [diff] [blame] | 1684 | return 1; |
Jeff Mahoney | 3444a97 | 2011-10-03 23:23:13 -0400 | [diff] [blame] | 1685 | return 0; |
Chris Mason | 239b14b | 2008-03-24 15:02:07 -0400 | [diff] [blame] | 1686 | } |
| 1687 | |
Chris Mason | d352ac6 | 2008-09-29 15:18:18 -0400 | [diff] [blame] | 1688 | /* |
| 1689 | * in order to insert checksums into the metadata in large chunks, |
| 1690 | * we wait until bio submission time. All the pages in the bio are |
| 1691 | * checksummed and sums are attached onto the ordered extent record. |
| 1692 | * |
| 1693 | * At IO completion time the cums attached on the ordered extent record |
| 1694 | * are inserted into the btree |
| 1695 | */ |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 1696 | static int __btrfs_submit_bio_start(struct inode *inode, int rw, |
| 1697 | struct bio *bio, int mirror_num, |
Chris Mason | eaf25d9 | 2010-05-25 09:48:28 -0400 | [diff] [blame] | 1698 | unsigned long bio_flags, |
| 1699 | u64 bio_offset) |
Chris Mason | 065631f | 2008-02-20 12:07:25 -0500 | [diff] [blame] | 1700 | { |
Chris Mason | 065631f | 2008-02-20 12:07:25 -0500 | [diff] [blame] | 1701 | struct btrfs_root *root = BTRFS_I(inode)->root; |
Chris Mason | 065631f | 2008-02-20 12:07:25 -0500 | [diff] [blame] | 1702 | int ret = 0; |
Chris Mason | e015640 | 2008-04-16 11:15:20 -0400 | [diff] [blame] | 1703 | |
Chris Mason | d20f704 | 2008-12-08 16:58:54 -0500 | [diff] [blame] | 1704 | ret = btrfs_csum_one_bio(root, inode, bio, 0, 0); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 1705 | BUG_ON(ret); /* -ENOMEM */ |
Chris Mason | 4a69a41 | 2008-11-06 22:03:00 -0500 | [diff] [blame] | 1706 | return 0; |
| 1707 | } |
Chris Mason | e015640 | 2008-04-16 11:15:20 -0400 | [diff] [blame] | 1708 | |
Chris Mason | 4a69a41 | 2008-11-06 22:03:00 -0500 | [diff] [blame] | 1709 | /* |
| 1710 | * in order to insert checksums into the metadata in large chunks, |
| 1711 | * we wait until bio submission time. All the pages in the bio are |
| 1712 | * checksummed and sums are attached onto the ordered extent record. |
| 1713 | * |
| 1714 | * At IO completion time the cums attached on the ordered extent record |
| 1715 | * are inserted into the btree |
| 1716 | */ |
Christoph Hellwig | b295086 | 2008-12-02 09:54:17 -0500 | [diff] [blame] | 1717 | 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] | 1718 | int mirror_num, unsigned long bio_flags, |
| 1719 | u64 bio_offset) |
Chris Mason | 4a69a41 | 2008-11-06 22:03:00 -0500 | [diff] [blame] | 1720 | { |
| 1721 | struct btrfs_root *root = BTRFS_I(inode)->root; |
Stefan Behrens | 6189192 | 2012-11-05 18:51:52 +0100 | [diff] [blame] | 1722 | int ret; |
| 1723 | |
| 1724 | ret = btrfs_map_bio(root, rw, bio, mirror_num, 1); |
| 1725 | if (ret) |
| 1726 | bio_endio(bio, ret); |
| 1727 | return ret; |
Chris Mason | 44b8bd7 | 2008-04-16 11:14:51 -0400 | [diff] [blame] | 1728 | } |
| 1729 | |
Chris Mason | d352ac6 | 2008-09-29 15:18:18 -0400 | [diff] [blame] | 1730 | /* |
Chris Mason | cad321a | 2008-12-17 14:51:42 -0500 | [diff] [blame] | 1731 | * extent_io.c submission hook. This does the right thing for csum calculation |
| 1732 | * on write, or reading the csums from the tree before a read |
Chris Mason | d352ac6 | 2008-09-29 15:18:18 -0400 | [diff] [blame] | 1733 | */ |
Christoph Hellwig | b295086 | 2008-12-02 09:54:17 -0500 | [diff] [blame] | 1734 | 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] | 1735 | int mirror_num, unsigned long bio_flags, |
| 1736 | u64 bio_offset) |
Chris Mason | 44b8bd7 | 2008-04-16 11:14:51 -0400 | [diff] [blame] | 1737 | { |
| 1738 | struct btrfs_root *root = BTRFS_I(inode)->root; |
| 1739 | int ret = 0; |
Chris Mason | 19b9bdb | 2008-10-30 14:23:13 -0400 | [diff] [blame] | 1740 | int skip_sum; |
Jeff Mahoney | 0417341 | 2011-10-03 23:23:12 -0400 | [diff] [blame] | 1741 | int metadata = 0; |
Josef Bacik | b812ce2 | 2012-11-16 13:56:32 -0500 | [diff] [blame] | 1742 | int async = !atomic_read(&BTRFS_I(inode)->sync_writers); |
Chris Mason | 44b8bd7 | 2008-04-16 11:14:51 -0400 | [diff] [blame] | 1743 | |
Christoph Hellwig | 6cbff00 | 2009-04-17 10:37:41 +0200 | [diff] [blame] | 1744 | skip_sum = BTRFS_I(inode)->flags & BTRFS_INODE_NODATASUM; |
Chris Mason | cad321a | 2008-12-17 14:51:42 -0500 | [diff] [blame] | 1745 | |
Liu Bo | 83eea1f | 2012-07-10 05:28:39 -0600 | [diff] [blame] | 1746 | if (btrfs_is_free_space_inode(inode)) |
Jeff Mahoney | 0417341 | 2011-10-03 23:23:12 -0400 | [diff] [blame] | 1747 | metadata = 2; |
| 1748 | |
Christoph Hellwig | 7b6d91d | 2010-08-07 18:20:39 +0200 | [diff] [blame] | 1749 | if (!(rw & REQ_WRITE)) { |
Josef Bacik | 5fd0204 | 2012-05-02 14:00:54 -0400 | [diff] [blame] | 1750 | ret = btrfs_bio_wq_end_io(root->fs_info, bio, metadata); |
| 1751 | if (ret) |
Stefan Behrens | 6189192 | 2012-11-05 18:51:52 +0100 | [diff] [blame] | 1752 | goto out; |
Josef Bacik | 5fd0204 | 2012-05-02 14:00:54 -0400 | [diff] [blame] | 1753 | |
Chris Mason | d20f704 | 2008-12-08 16:58:54 -0500 | [diff] [blame] | 1754 | if (bio_flags & EXTENT_BIO_COMPRESSED) { |
Stefan Behrens | 6189192 | 2012-11-05 18:51:52 +0100 | [diff] [blame] | 1755 | ret = btrfs_submit_compressed_read(inode, bio, |
| 1756 | mirror_num, |
| 1757 | bio_flags); |
| 1758 | goto out; |
Tsutomu Itoh | c2db107 | 2011-03-01 06:48:31 +0000 | [diff] [blame] | 1759 | } else if (!skip_sum) { |
| 1760 | ret = btrfs_lookup_bio_sums(root, inode, bio, NULL); |
| 1761 | if (ret) |
Stefan Behrens | 6189192 | 2012-11-05 18:51:52 +0100 | [diff] [blame] | 1762 | goto out; |
Tsutomu Itoh | c2db107 | 2011-03-01 06:48:31 +0000 | [diff] [blame] | 1763 | } |
Chris Mason | 4d1b5fb | 2008-08-20 09:44:52 -0400 | [diff] [blame] | 1764 | goto mapit; |
Josef Bacik | b812ce2 | 2012-11-16 13:56:32 -0500 | [diff] [blame] | 1765 | } else if (async && !skip_sum) { |
Yan Zheng | 17d217f | 2008-12-12 10:03:38 -0500 | [diff] [blame] | 1766 | /* csum items have already been cloned */ |
| 1767 | if (root->root_key.objectid == BTRFS_DATA_RELOC_TREE_OBJECTID) |
| 1768 | goto mapit; |
Chris Mason | 19b9bdb | 2008-10-30 14:23:13 -0400 | [diff] [blame] | 1769 | /* we're doing a write, do the async checksumming */ |
Stefan Behrens | 6189192 | 2012-11-05 18:51:52 +0100 | [diff] [blame] | 1770 | ret = btrfs_wq_submit_bio(BTRFS_I(inode)->root->fs_info, |
Chris Mason | 44b8bd7 | 2008-04-16 11:14:51 -0400 | [diff] [blame] | 1771 | inode, rw, bio, mirror_num, |
Chris Mason | eaf25d9 | 2010-05-25 09:48:28 -0400 | [diff] [blame] | 1772 | bio_flags, bio_offset, |
| 1773 | __btrfs_submit_bio_start, |
Chris Mason | 4a69a41 | 2008-11-06 22:03:00 -0500 | [diff] [blame] | 1774 | __btrfs_submit_bio_done); |
Stefan Behrens | 6189192 | 2012-11-05 18:51:52 +0100 | [diff] [blame] | 1775 | goto out; |
Josef Bacik | b812ce2 | 2012-11-16 13:56:32 -0500 | [diff] [blame] | 1776 | } else if (!skip_sum) { |
| 1777 | ret = btrfs_csum_one_bio(root, inode, bio, 0, 0); |
| 1778 | if (ret) |
| 1779 | goto out; |
Chris Mason | 19b9bdb | 2008-10-30 14:23:13 -0400 | [diff] [blame] | 1780 | } |
| 1781 | |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 1782 | mapit: |
Stefan Behrens | 6189192 | 2012-11-05 18:51:52 +0100 | [diff] [blame] | 1783 | ret = btrfs_map_bio(root, rw, bio, mirror_num, 0); |
| 1784 | |
| 1785 | out: |
| 1786 | if (ret < 0) |
| 1787 | bio_endio(bio, ret); |
| 1788 | return ret; |
Chris Mason | 065631f | 2008-02-20 12:07:25 -0500 | [diff] [blame] | 1789 | } |
Chris Mason | 6885f30 | 2008-02-20 16:11:05 -0500 | [diff] [blame] | 1790 | |
Chris Mason | d352ac6 | 2008-09-29 15:18:18 -0400 | [diff] [blame] | 1791 | /* |
| 1792 | * given a list of ordered sums record them in the inode. This happens |
| 1793 | * at IO completion time based on sums calculated at bio submission time. |
| 1794 | */ |
Chris Mason | ba1da2f | 2008-07-17 12:54:15 -0400 | [diff] [blame] | 1795 | static noinline int add_pending_csums(struct btrfs_trans_handle *trans, |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 1796 | struct inode *inode, u64 file_offset, |
| 1797 | struct list_head *list) |
| 1798 | { |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 1799 | struct btrfs_ordered_sum *sum; |
| 1800 | |
Qinghuang Feng | c6e3087 | 2009-01-21 10:59:08 -0500 | [diff] [blame] | 1801 | list_for_each_entry(sum, list, list) { |
Miao Xie | 39847c4 | 2013-03-28 08:08:20 +0000 | [diff] [blame] | 1802 | trans->adding_csums = 1; |
Chris Mason | d20f704 | 2008-12-08 16:58:54 -0500 | [diff] [blame] | 1803 | btrfs_csum_file_blocks(trans, |
| 1804 | BTRFS_I(inode)->root->fs_info->csum_root, sum); |
Miao Xie | 39847c4 | 2013-03-28 08:08:20 +0000 | [diff] [blame] | 1805 | trans->adding_csums = 0; |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 1806 | } |
| 1807 | return 0; |
| 1808 | } |
| 1809 | |
Josef Bacik | 2ac55d4 | 2010-02-03 19:33:23 +0000 | [diff] [blame] | 1810 | int btrfs_set_extent_delalloc(struct inode *inode, u64 start, u64 end, |
| 1811 | struct extent_state **cached_state) |
Chris Mason | ea8c281 | 2008-08-04 23:17:27 -0400 | [diff] [blame] | 1812 | { |
Julia Lawall | 6c1500f | 2012-11-03 20:30:18 +0000 | [diff] [blame] | 1813 | WARN_ON((end & (PAGE_CACHE_SIZE - 1)) == 0); |
Chris Mason | ea8c281 | 2008-08-04 23:17:27 -0400 | [diff] [blame] | 1814 | return set_extent_delalloc(&BTRFS_I(inode)->io_tree, start, end, |
Josef Bacik | 2ac55d4 | 2010-02-03 19:33:23 +0000 | [diff] [blame] | 1815 | cached_state, GFP_NOFS); |
Chris Mason | ea8c281 | 2008-08-04 23:17:27 -0400 | [diff] [blame] | 1816 | } |
| 1817 | |
Chris Mason | d352ac6 | 2008-09-29 15:18:18 -0400 | [diff] [blame] | 1818 | /* see btrfs_writepage_start_hook for details on why this is required */ |
Chris Mason | 247e743 | 2008-07-17 12:53:51 -0400 | [diff] [blame] | 1819 | struct btrfs_writepage_fixup { |
| 1820 | struct page *page; |
| 1821 | struct btrfs_work work; |
| 1822 | }; |
| 1823 | |
Christoph Hellwig | b295086 | 2008-12-02 09:54:17 -0500 | [diff] [blame] | 1824 | static void btrfs_writepage_fixup_worker(struct btrfs_work *work) |
Chris Mason | 247e743 | 2008-07-17 12:53:51 -0400 | [diff] [blame] | 1825 | { |
| 1826 | struct btrfs_writepage_fixup *fixup; |
| 1827 | struct btrfs_ordered_extent *ordered; |
Josef Bacik | 2ac55d4 | 2010-02-03 19:33:23 +0000 | [diff] [blame] | 1828 | struct extent_state *cached_state = NULL; |
Chris Mason | 247e743 | 2008-07-17 12:53:51 -0400 | [diff] [blame] | 1829 | struct page *page; |
| 1830 | struct inode *inode; |
| 1831 | u64 page_start; |
| 1832 | u64 page_end; |
Jeff Mahoney | 87826df | 2012-02-15 16:23:57 +0100 | [diff] [blame] | 1833 | int ret; |
Chris Mason | 247e743 | 2008-07-17 12:53:51 -0400 | [diff] [blame] | 1834 | |
| 1835 | fixup = container_of(work, struct btrfs_writepage_fixup, work); |
| 1836 | page = fixup->page; |
Chris Mason | 4a09675 | 2008-07-21 10:29:44 -0400 | [diff] [blame] | 1837 | again: |
Chris Mason | 247e743 | 2008-07-17 12:53:51 -0400 | [diff] [blame] | 1838 | lock_page(page); |
| 1839 | if (!page->mapping || !PageDirty(page) || !PageChecked(page)) { |
| 1840 | ClearPageChecked(page); |
| 1841 | goto out_page; |
| 1842 | } |
| 1843 | |
| 1844 | inode = page->mapping->host; |
| 1845 | page_start = page_offset(page); |
| 1846 | page_end = page_offset(page) + PAGE_CACHE_SIZE - 1; |
| 1847 | |
Josef Bacik | 2ac55d4 | 2010-02-03 19:33:23 +0000 | [diff] [blame] | 1848 | 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] | 1849 | &cached_state); |
Chris Mason | 4a09675 | 2008-07-21 10:29:44 -0400 | [diff] [blame] | 1850 | |
| 1851 | /* already ordered? We're done */ |
Chris Mason | 8b62b72 | 2009-09-02 16:53:46 -0400 | [diff] [blame] | 1852 | if (PagePrivate2(page)) |
Chris Mason | 247e743 | 2008-07-17 12:53:51 -0400 | [diff] [blame] | 1853 | goto out; |
Chris Mason | 4a09675 | 2008-07-21 10:29:44 -0400 | [diff] [blame] | 1854 | |
| 1855 | ordered = btrfs_lookup_ordered_extent(inode, page_start); |
| 1856 | if (ordered) { |
Josef Bacik | 2ac55d4 | 2010-02-03 19:33:23 +0000 | [diff] [blame] | 1857 | unlock_extent_cached(&BTRFS_I(inode)->io_tree, page_start, |
| 1858 | page_end, &cached_state, GFP_NOFS); |
Chris Mason | 4a09675 | 2008-07-21 10:29:44 -0400 | [diff] [blame] | 1859 | unlock_page(page); |
| 1860 | btrfs_start_ordered_extent(inode, ordered, 1); |
Jeff Mahoney | 87826df | 2012-02-15 16:23:57 +0100 | [diff] [blame] | 1861 | btrfs_put_ordered_extent(ordered); |
Chris Mason | 4a09675 | 2008-07-21 10:29:44 -0400 | [diff] [blame] | 1862 | goto again; |
| 1863 | } |
Chris Mason | 247e743 | 2008-07-17 12:53:51 -0400 | [diff] [blame] | 1864 | |
Jeff Mahoney | 87826df | 2012-02-15 16:23:57 +0100 | [diff] [blame] | 1865 | ret = btrfs_delalloc_reserve_space(inode, PAGE_CACHE_SIZE); |
| 1866 | if (ret) { |
| 1867 | mapping_set_error(page->mapping, ret); |
| 1868 | end_extent_writepage(page, ret, page_start, page_end); |
| 1869 | ClearPageChecked(page); |
| 1870 | goto out; |
| 1871 | } |
| 1872 | |
Josef Bacik | 2ac55d4 | 2010-02-03 19:33:23 +0000 | [diff] [blame] | 1873 | btrfs_set_extent_delalloc(inode, page_start, page_end, &cached_state); |
Chris Mason | 247e743 | 2008-07-17 12:53:51 -0400 | [diff] [blame] | 1874 | ClearPageChecked(page); |
Jeff Mahoney | 87826df | 2012-02-15 16:23:57 +0100 | [diff] [blame] | 1875 | set_page_dirty(page); |
Chris Mason | 247e743 | 2008-07-17 12:53:51 -0400 | [diff] [blame] | 1876 | out: |
Josef Bacik | 2ac55d4 | 2010-02-03 19:33:23 +0000 | [diff] [blame] | 1877 | unlock_extent_cached(&BTRFS_I(inode)->io_tree, page_start, page_end, |
| 1878 | &cached_state, GFP_NOFS); |
Chris Mason | 247e743 | 2008-07-17 12:53:51 -0400 | [diff] [blame] | 1879 | out_page: |
| 1880 | unlock_page(page); |
| 1881 | page_cache_release(page); |
Miao Xie | b897abe | 2011-01-26 16:19:22 +0800 | [diff] [blame] | 1882 | kfree(fixup); |
Chris Mason | 247e743 | 2008-07-17 12:53:51 -0400 | [diff] [blame] | 1883 | } |
| 1884 | |
| 1885 | /* |
| 1886 | * There are a few paths in the higher layers of the kernel that directly |
| 1887 | * set the page dirty bit without asking the filesystem if it is a |
| 1888 | * good idea. This causes problems because we want to make sure COW |
| 1889 | * properly happens and the data=ordered rules are followed. |
| 1890 | * |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 1891 | * 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] | 1892 | * hasn't been properly setup for IO. We kick off an async process |
| 1893 | * to fix it up. The async helper will wait for ordered extents, set |
| 1894 | * the delalloc bit and make it safe to write the page. |
| 1895 | */ |
Christoph Hellwig | b295086 | 2008-12-02 09:54:17 -0500 | [diff] [blame] | 1896 | 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] | 1897 | { |
| 1898 | struct inode *inode = page->mapping->host; |
| 1899 | struct btrfs_writepage_fixup *fixup; |
| 1900 | struct btrfs_root *root = BTRFS_I(inode)->root; |
Chris Mason | 247e743 | 2008-07-17 12:53:51 -0400 | [diff] [blame] | 1901 | |
Chris Mason | 8b62b72 | 2009-09-02 16:53:46 -0400 | [diff] [blame] | 1902 | /* this page is properly in the ordered list */ |
| 1903 | if (TestClearPagePrivate2(page)) |
Chris Mason | 247e743 | 2008-07-17 12:53:51 -0400 | [diff] [blame] | 1904 | return 0; |
| 1905 | |
| 1906 | if (PageChecked(page)) |
| 1907 | return -EAGAIN; |
| 1908 | |
| 1909 | fixup = kzalloc(sizeof(*fixup), GFP_NOFS); |
| 1910 | if (!fixup) |
| 1911 | return -EAGAIN; |
Chris Mason | f421950 | 2008-07-22 11:18:09 -0400 | [diff] [blame] | 1912 | |
Chris Mason | 247e743 | 2008-07-17 12:53:51 -0400 | [diff] [blame] | 1913 | SetPageChecked(page); |
| 1914 | page_cache_get(page); |
| 1915 | fixup->work.func = btrfs_writepage_fixup_worker; |
| 1916 | fixup->page = page; |
| 1917 | btrfs_queue_worker(&root->fs_info->fixup_workers, &fixup->work); |
Jeff Mahoney | 87826df | 2012-02-15 16:23:57 +0100 | [diff] [blame] | 1918 | return -EBUSY; |
Chris Mason | 247e743 | 2008-07-17 12:53:51 -0400 | [diff] [blame] | 1919 | } |
| 1920 | |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 1921 | static int insert_reserved_file_extent(struct btrfs_trans_handle *trans, |
| 1922 | struct inode *inode, u64 file_pos, |
| 1923 | u64 disk_bytenr, u64 disk_num_bytes, |
| 1924 | u64 num_bytes, u64 ram_bytes, |
| 1925 | u8 compression, u8 encryption, |
| 1926 | u16 other_encoding, int extent_type) |
| 1927 | { |
| 1928 | struct btrfs_root *root = BTRFS_I(inode)->root; |
| 1929 | struct btrfs_file_extent_item *fi; |
| 1930 | struct btrfs_path *path; |
| 1931 | struct extent_buffer *leaf; |
| 1932 | struct btrfs_key ins; |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 1933 | int ret; |
| 1934 | |
| 1935 | path = btrfs_alloc_path(); |
Mark Fasheh | d8926bb | 2011-07-13 10:38:47 -0700 | [diff] [blame] | 1936 | if (!path) |
| 1937 | return -ENOMEM; |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 1938 | |
Chris Mason | b947343 | 2009-03-13 11:00:37 -0400 | [diff] [blame] | 1939 | path->leave_spinning = 1; |
Chris Mason | a1ed835 | 2009-09-11 12:27:37 -0400 | [diff] [blame] | 1940 | |
| 1941 | /* |
| 1942 | * we may be replacing one extent in the tree with another. |
| 1943 | * The new extent is pinned in the extent map, and we don't want |
| 1944 | * to drop it from the cache until it is completely in the btree. |
| 1945 | * |
| 1946 | * So, tell btrfs_drop_extents to leave this extent in the cache. |
| 1947 | * the caller is expected to unpin it and allow it to be merged |
| 1948 | * with the others. |
| 1949 | */ |
Josef Bacik | 5dc562c | 2012-08-17 13:14:17 -0400 | [diff] [blame] | 1950 | ret = btrfs_drop_extents(trans, root, inode, file_pos, |
Josef Bacik | 2671485 | 2012-08-29 12:24:27 -0400 | [diff] [blame] | 1951 | file_pos + num_bytes, 0); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 1952 | if (ret) |
| 1953 | goto out; |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 1954 | |
Li Zefan | 33345d01 | 2011-04-20 10:31:50 +0800 | [diff] [blame] | 1955 | ins.objectid = btrfs_ino(inode); |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 1956 | ins.offset = file_pos; |
| 1957 | ins.type = BTRFS_EXTENT_DATA_KEY; |
| 1958 | ret = btrfs_insert_empty_item(trans, root, path, &ins, sizeof(*fi)); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 1959 | if (ret) |
| 1960 | goto out; |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 1961 | leaf = path->nodes[0]; |
| 1962 | fi = btrfs_item_ptr(leaf, path->slots[0], |
| 1963 | struct btrfs_file_extent_item); |
| 1964 | btrfs_set_file_extent_generation(leaf, fi, trans->transid); |
| 1965 | btrfs_set_file_extent_type(leaf, fi, extent_type); |
| 1966 | btrfs_set_file_extent_disk_bytenr(leaf, fi, disk_bytenr); |
| 1967 | btrfs_set_file_extent_disk_num_bytes(leaf, fi, disk_num_bytes); |
| 1968 | btrfs_set_file_extent_offset(leaf, fi, 0); |
| 1969 | btrfs_set_file_extent_num_bytes(leaf, fi, num_bytes); |
| 1970 | btrfs_set_file_extent_ram_bytes(leaf, fi, ram_bytes); |
| 1971 | btrfs_set_file_extent_compression(leaf, fi, compression); |
| 1972 | btrfs_set_file_extent_encryption(leaf, fi, encryption); |
| 1973 | btrfs_set_file_extent_other_encoding(leaf, fi, other_encoding); |
Chris Mason | b947343 | 2009-03-13 11:00:37 -0400 | [diff] [blame] | 1974 | |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 1975 | btrfs_mark_buffer_dirty(leaf); |
Josef Bacik | ce19533 | 2012-09-25 15:26:16 -0400 | [diff] [blame] | 1976 | btrfs_release_path(path); |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 1977 | |
| 1978 | inode_add_bytes(inode, num_bytes); |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 1979 | |
| 1980 | ins.objectid = disk_bytenr; |
| 1981 | ins.offset = disk_num_bytes; |
| 1982 | ins.type = BTRFS_EXTENT_ITEM_KEY; |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 1983 | ret = btrfs_alloc_reserved_file_extent(trans, root, |
| 1984 | root->root_key.objectid, |
Li Zefan | 33345d01 | 2011-04-20 10:31:50 +0800 | [diff] [blame] | 1985 | btrfs_ino(inode), file_pos, &ins); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 1986 | out: |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 1987 | btrfs_free_path(path); |
Chris Mason | b947343 | 2009-03-13 11:00:37 -0400 | [diff] [blame] | 1988 | |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 1989 | return ret; |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 1990 | } |
| 1991 | |
Liu Bo | 38c227d | 2013-01-29 03:18:40 +0000 | [diff] [blame] | 1992 | /* snapshot-aware defrag */ |
| 1993 | struct sa_defrag_extent_backref { |
| 1994 | struct rb_node node; |
| 1995 | struct old_sa_defrag_extent *old; |
| 1996 | u64 root_id; |
| 1997 | u64 inum; |
| 1998 | u64 file_pos; |
| 1999 | u64 extent_offset; |
| 2000 | u64 num_bytes; |
| 2001 | u64 generation; |
| 2002 | }; |
| 2003 | |
| 2004 | struct old_sa_defrag_extent { |
| 2005 | struct list_head list; |
| 2006 | struct new_sa_defrag_extent *new; |
| 2007 | |
| 2008 | u64 extent_offset; |
| 2009 | u64 bytenr; |
| 2010 | u64 offset; |
| 2011 | u64 len; |
| 2012 | int count; |
| 2013 | }; |
| 2014 | |
| 2015 | struct new_sa_defrag_extent { |
| 2016 | struct rb_root root; |
| 2017 | struct list_head head; |
| 2018 | struct btrfs_path *path; |
| 2019 | struct inode *inode; |
| 2020 | u64 file_pos; |
| 2021 | u64 len; |
| 2022 | u64 bytenr; |
| 2023 | u64 disk_len; |
| 2024 | u8 compress_type; |
| 2025 | }; |
| 2026 | |
| 2027 | static int backref_comp(struct sa_defrag_extent_backref *b1, |
| 2028 | struct sa_defrag_extent_backref *b2) |
| 2029 | { |
| 2030 | if (b1->root_id < b2->root_id) |
| 2031 | return -1; |
| 2032 | else if (b1->root_id > b2->root_id) |
| 2033 | return 1; |
| 2034 | |
| 2035 | if (b1->inum < b2->inum) |
| 2036 | return -1; |
| 2037 | else if (b1->inum > b2->inum) |
| 2038 | return 1; |
| 2039 | |
| 2040 | if (b1->file_pos < b2->file_pos) |
| 2041 | return -1; |
| 2042 | else if (b1->file_pos > b2->file_pos) |
| 2043 | return 1; |
| 2044 | |
| 2045 | /* |
| 2046 | * [------------------------------] ===> (a range of space) |
| 2047 | * |<--->| |<---->| =============> (fs/file tree A) |
| 2048 | * |<---------------------------->| ===> (fs/file tree B) |
| 2049 | * |
| 2050 | * A range of space can refer to two file extents in one tree while |
| 2051 | * refer to only one file extent in another tree. |
| 2052 | * |
| 2053 | * So we may process a disk offset more than one time(two extents in A) |
| 2054 | * and locate at the same extent(one extent in B), then insert two same |
| 2055 | * backrefs(both refer to the extent in B). |
| 2056 | */ |
| 2057 | return 0; |
| 2058 | } |
| 2059 | |
| 2060 | static void backref_insert(struct rb_root *root, |
| 2061 | struct sa_defrag_extent_backref *backref) |
| 2062 | { |
| 2063 | struct rb_node **p = &root->rb_node; |
| 2064 | struct rb_node *parent = NULL; |
| 2065 | struct sa_defrag_extent_backref *entry; |
| 2066 | int ret; |
| 2067 | |
| 2068 | while (*p) { |
| 2069 | parent = *p; |
| 2070 | entry = rb_entry(parent, struct sa_defrag_extent_backref, node); |
| 2071 | |
| 2072 | ret = backref_comp(backref, entry); |
| 2073 | if (ret < 0) |
| 2074 | p = &(*p)->rb_left; |
| 2075 | else |
| 2076 | p = &(*p)->rb_right; |
| 2077 | } |
| 2078 | |
| 2079 | rb_link_node(&backref->node, parent, p); |
| 2080 | rb_insert_color(&backref->node, root); |
| 2081 | } |
| 2082 | |
| 2083 | /* |
| 2084 | * Note the backref might has changed, and in this case we just return 0. |
| 2085 | */ |
| 2086 | static noinline int record_one_backref(u64 inum, u64 offset, u64 root_id, |
| 2087 | void *ctx) |
| 2088 | { |
| 2089 | struct btrfs_file_extent_item *extent; |
| 2090 | struct btrfs_fs_info *fs_info; |
| 2091 | struct old_sa_defrag_extent *old = ctx; |
| 2092 | struct new_sa_defrag_extent *new = old->new; |
| 2093 | struct btrfs_path *path = new->path; |
| 2094 | struct btrfs_key key; |
| 2095 | struct btrfs_root *root; |
| 2096 | struct sa_defrag_extent_backref *backref; |
| 2097 | struct extent_buffer *leaf; |
| 2098 | struct inode *inode = new->inode; |
| 2099 | int slot; |
| 2100 | int ret; |
| 2101 | u64 extent_offset; |
| 2102 | u64 num_bytes; |
| 2103 | |
| 2104 | if (BTRFS_I(inode)->root->root_key.objectid == root_id && |
| 2105 | inum == btrfs_ino(inode)) |
| 2106 | return 0; |
| 2107 | |
| 2108 | key.objectid = root_id; |
| 2109 | key.type = BTRFS_ROOT_ITEM_KEY; |
| 2110 | key.offset = (u64)-1; |
| 2111 | |
| 2112 | fs_info = BTRFS_I(inode)->root->fs_info; |
| 2113 | root = btrfs_read_fs_root_no_name(fs_info, &key); |
| 2114 | if (IS_ERR(root)) { |
| 2115 | if (PTR_ERR(root) == -ENOENT) |
| 2116 | return 0; |
| 2117 | WARN_ON(1); |
| 2118 | pr_debug("inum=%llu, offset=%llu, root_id=%llu\n", |
| 2119 | inum, offset, root_id); |
| 2120 | return PTR_ERR(root); |
| 2121 | } |
| 2122 | |
| 2123 | key.objectid = inum; |
| 2124 | key.type = BTRFS_EXTENT_DATA_KEY; |
| 2125 | if (offset > (u64)-1 << 32) |
| 2126 | key.offset = 0; |
| 2127 | else |
| 2128 | key.offset = offset; |
| 2129 | |
| 2130 | ret = btrfs_search_slot(NULL, root, &key, path, 0, 0); |
| 2131 | if (ret < 0) { |
| 2132 | WARN_ON(1); |
| 2133 | return ret; |
| 2134 | } |
Josef Bacik | 50f1319 | 2013-07-22 12:50:37 -0400 | [diff] [blame] | 2135 | ret = 0; |
Liu Bo | 38c227d | 2013-01-29 03:18:40 +0000 | [diff] [blame] | 2136 | |
| 2137 | while (1) { |
| 2138 | cond_resched(); |
| 2139 | |
| 2140 | leaf = path->nodes[0]; |
| 2141 | slot = path->slots[0]; |
| 2142 | |
| 2143 | if (slot >= btrfs_header_nritems(leaf)) { |
| 2144 | ret = btrfs_next_leaf(root, path); |
| 2145 | if (ret < 0) { |
| 2146 | goto out; |
| 2147 | } else if (ret > 0) { |
| 2148 | ret = 0; |
| 2149 | goto out; |
| 2150 | } |
| 2151 | continue; |
| 2152 | } |
| 2153 | |
| 2154 | path->slots[0]++; |
| 2155 | |
| 2156 | btrfs_item_key_to_cpu(leaf, &key, slot); |
| 2157 | |
| 2158 | if (key.objectid > inum) |
| 2159 | goto out; |
| 2160 | |
| 2161 | if (key.objectid < inum || key.type != BTRFS_EXTENT_DATA_KEY) |
| 2162 | continue; |
| 2163 | |
| 2164 | extent = btrfs_item_ptr(leaf, slot, |
| 2165 | struct btrfs_file_extent_item); |
| 2166 | |
| 2167 | if (btrfs_file_extent_disk_bytenr(leaf, extent) != old->bytenr) |
| 2168 | continue; |
| 2169 | |
Liu Bo | e68afa4 | 2013-07-01 22:13:26 +0800 | [diff] [blame] | 2170 | /* |
| 2171 | * 'offset' refers to the exact key.offset, |
| 2172 | * NOT the 'offset' field in btrfs_extent_data_ref, ie. |
| 2173 | * (key.offset - extent_offset). |
| 2174 | */ |
| 2175 | if (key.offset != offset) |
Liu Bo | 38c227d | 2013-01-29 03:18:40 +0000 | [diff] [blame] | 2176 | continue; |
| 2177 | |
Liu Bo | e68afa4 | 2013-07-01 22:13:26 +0800 | [diff] [blame] | 2178 | extent_offset = btrfs_file_extent_offset(leaf, extent); |
Liu Bo | 38c227d | 2013-01-29 03:18:40 +0000 | [diff] [blame] | 2179 | num_bytes = btrfs_file_extent_num_bytes(leaf, extent); |
Liu Bo | e68afa4 | 2013-07-01 22:13:26 +0800 | [diff] [blame] | 2180 | |
Liu Bo | 38c227d | 2013-01-29 03:18:40 +0000 | [diff] [blame] | 2181 | if (extent_offset >= old->extent_offset + old->offset + |
| 2182 | old->len || extent_offset + num_bytes <= |
| 2183 | old->extent_offset + old->offset) |
| 2184 | continue; |
Liu Bo | 38c227d | 2013-01-29 03:18:40 +0000 | [diff] [blame] | 2185 | break; |
| 2186 | } |
| 2187 | |
| 2188 | backref = kmalloc(sizeof(*backref), GFP_NOFS); |
| 2189 | if (!backref) { |
| 2190 | ret = -ENOENT; |
| 2191 | goto out; |
| 2192 | } |
| 2193 | |
| 2194 | backref->root_id = root_id; |
| 2195 | backref->inum = inum; |
Liu Bo | e68afa4 | 2013-07-01 22:13:26 +0800 | [diff] [blame] | 2196 | backref->file_pos = offset; |
Liu Bo | 38c227d | 2013-01-29 03:18:40 +0000 | [diff] [blame] | 2197 | backref->num_bytes = num_bytes; |
| 2198 | backref->extent_offset = extent_offset; |
| 2199 | backref->generation = btrfs_file_extent_generation(leaf, extent); |
| 2200 | backref->old = old; |
| 2201 | backref_insert(&new->root, backref); |
| 2202 | old->count++; |
| 2203 | out: |
| 2204 | btrfs_release_path(path); |
| 2205 | WARN_ON(ret); |
| 2206 | return ret; |
| 2207 | } |
| 2208 | |
| 2209 | static noinline bool record_extent_backrefs(struct btrfs_path *path, |
| 2210 | struct new_sa_defrag_extent *new) |
| 2211 | { |
| 2212 | struct btrfs_fs_info *fs_info = BTRFS_I(new->inode)->root->fs_info; |
| 2213 | struct old_sa_defrag_extent *old, *tmp; |
| 2214 | int ret; |
| 2215 | |
| 2216 | new->path = path; |
| 2217 | |
| 2218 | list_for_each_entry_safe(old, tmp, &new->head, list) { |
Liu Bo | e68afa4 | 2013-07-01 22:13:26 +0800 | [diff] [blame] | 2219 | ret = iterate_inodes_from_logical(old->bytenr + |
| 2220 | old->extent_offset, fs_info, |
Liu Bo | 38c227d | 2013-01-29 03:18:40 +0000 | [diff] [blame] | 2221 | path, record_one_backref, |
| 2222 | old); |
| 2223 | BUG_ON(ret < 0 && ret != -ENOENT); |
| 2224 | |
| 2225 | /* no backref to be processed for this extent */ |
| 2226 | if (!old->count) { |
| 2227 | list_del(&old->list); |
| 2228 | kfree(old); |
| 2229 | } |
| 2230 | } |
| 2231 | |
| 2232 | if (list_empty(&new->head)) |
| 2233 | return false; |
| 2234 | |
| 2235 | return true; |
| 2236 | } |
| 2237 | |
| 2238 | static int relink_is_mergable(struct extent_buffer *leaf, |
| 2239 | struct btrfs_file_extent_item *fi, |
| 2240 | u64 disk_bytenr) |
| 2241 | { |
| 2242 | if (btrfs_file_extent_disk_bytenr(leaf, fi) != disk_bytenr) |
| 2243 | return 0; |
| 2244 | |
| 2245 | if (btrfs_file_extent_type(leaf, fi) != BTRFS_FILE_EXTENT_REG) |
| 2246 | return 0; |
| 2247 | |
| 2248 | if (btrfs_file_extent_compression(leaf, fi) || |
| 2249 | btrfs_file_extent_encryption(leaf, fi) || |
| 2250 | btrfs_file_extent_other_encoding(leaf, fi)) |
| 2251 | return 0; |
| 2252 | |
| 2253 | return 1; |
| 2254 | } |
| 2255 | |
| 2256 | /* |
| 2257 | * Note the backref might has changed, and in this case we just return 0. |
| 2258 | */ |
| 2259 | static noinline int relink_extent_backref(struct btrfs_path *path, |
| 2260 | struct sa_defrag_extent_backref *prev, |
| 2261 | struct sa_defrag_extent_backref *backref) |
| 2262 | { |
| 2263 | struct btrfs_file_extent_item *extent; |
| 2264 | struct btrfs_file_extent_item *item; |
| 2265 | struct btrfs_ordered_extent *ordered; |
| 2266 | struct btrfs_trans_handle *trans; |
| 2267 | struct btrfs_fs_info *fs_info; |
| 2268 | struct btrfs_root *root; |
| 2269 | struct btrfs_key key; |
| 2270 | struct extent_buffer *leaf; |
| 2271 | struct old_sa_defrag_extent *old = backref->old; |
| 2272 | struct new_sa_defrag_extent *new = old->new; |
| 2273 | struct inode *src_inode = new->inode; |
| 2274 | struct inode *inode; |
| 2275 | struct extent_state *cached = NULL; |
| 2276 | int ret = 0; |
| 2277 | u64 start; |
| 2278 | u64 len; |
| 2279 | u64 lock_start; |
| 2280 | u64 lock_end; |
| 2281 | bool merge = false; |
| 2282 | int index; |
| 2283 | |
| 2284 | if (prev && prev->root_id == backref->root_id && |
| 2285 | prev->inum == backref->inum && |
| 2286 | prev->file_pos + prev->num_bytes == backref->file_pos) |
| 2287 | merge = true; |
| 2288 | |
| 2289 | /* step 1: get root */ |
| 2290 | key.objectid = backref->root_id; |
| 2291 | key.type = BTRFS_ROOT_ITEM_KEY; |
| 2292 | key.offset = (u64)-1; |
| 2293 | |
| 2294 | fs_info = BTRFS_I(src_inode)->root->fs_info; |
| 2295 | index = srcu_read_lock(&fs_info->subvol_srcu); |
| 2296 | |
| 2297 | root = btrfs_read_fs_root_no_name(fs_info, &key); |
| 2298 | if (IS_ERR(root)) { |
| 2299 | srcu_read_unlock(&fs_info->subvol_srcu, index); |
| 2300 | if (PTR_ERR(root) == -ENOENT) |
| 2301 | return 0; |
| 2302 | return PTR_ERR(root); |
| 2303 | } |
Liu Bo | 38c227d | 2013-01-29 03:18:40 +0000 | [diff] [blame] | 2304 | |
| 2305 | /* step 2: get inode */ |
| 2306 | key.objectid = backref->inum; |
| 2307 | key.type = BTRFS_INODE_ITEM_KEY; |
| 2308 | key.offset = 0; |
| 2309 | |
| 2310 | inode = btrfs_iget(fs_info->sb, &key, root, NULL); |
| 2311 | if (IS_ERR(inode)) { |
| 2312 | srcu_read_unlock(&fs_info->subvol_srcu, index); |
| 2313 | return 0; |
| 2314 | } |
| 2315 | |
| 2316 | srcu_read_unlock(&fs_info->subvol_srcu, index); |
| 2317 | |
| 2318 | /* step 3: relink backref */ |
| 2319 | lock_start = backref->file_pos; |
| 2320 | lock_end = backref->file_pos + backref->num_bytes - 1; |
| 2321 | lock_extent_bits(&BTRFS_I(inode)->io_tree, lock_start, lock_end, |
| 2322 | 0, &cached); |
| 2323 | |
| 2324 | ordered = btrfs_lookup_first_ordered_extent(inode, lock_end); |
| 2325 | if (ordered) { |
| 2326 | btrfs_put_ordered_extent(ordered); |
| 2327 | goto out_unlock; |
| 2328 | } |
| 2329 | |
| 2330 | trans = btrfs_join_transaction(root); |
| 2331 | if (IS_ERR(trans)) { |
| 2332 | ret = PTR_ERR(trans); |
| 2333 | goto out_unlock; |
| 2334 | } |
| 2335 | |
| 2336 | key.objectid = backref->inum; |
| 2337 | key.type = BTRFS_EXTENT_DATA_KEY; |
| 2338 | key.offset = backref->file_pos; |
| 2339 | |
| 2340 | ret = btrfs_search_slot(NULL, root, &key, path, 0, 0); |
| 2341 | if (ret < 0) { |
| 2342 | goto out_free_path; |
| 2343 | } else if (ret > 0) { |
| 2344 | ret = 0; |
| 2345 | goto out_free_path; |
| 2346 | } |
| 2347 | |
| 2348 | extent = btrfs_item_ptr(path->nodes[0], path->slots[0], |
| 2349 | struct btrfs_file_extent_item); |
| 2350 | |
| 2351 | if (btrfs_file_extent_generation(path->nodes[0], extent) != |
| 2352 | backref->generation) |
| 2353 | goto out_free_path; |
| 2354 | |
| 2355 | btrfs_release_path(path); |
| 2356 | |
| 2357 | start = backref->file_pos; |
| 2358 | if (backref->extent_offset < old->extent_offset + old->offset) |
| 2359 | start += old->extent_offset + old->offset - |
| 2360 | backref->extent_offset; |
| 2361 | |
| 2362 | len = min(backref->extent_offset + backref->num_bytes, |
| 2363 | old->extent_offset + old->offset + old->len); |
| 2364 | len -= max(backref->extent_offset, old->extent_offset + old->offset); |
| 2365 | |
| 2366 | ret = btrfs_drop_extents(trans, root, inode, start, |
| 2367 | start + len, 1); |
| 2368 | if (ret) |
| 2369 | goto out_free_path; |
| 2370 | again: |
| 2371 | key.objectid = btrfs_ino(inode); |
| 2372 | key.type = BTRFS_EXTENT_DATA_KEY; |
| 2373 | key.offset = start; |
| 2374 | |
Liu Bo | a09a0a7 | 2013-03-11 09:20:58 +0000 | [diff] [blame] | 2375 | path->leave_spinning = 1; |
Liu Bo | 38c227d | 2013-01-29 03:18:40 +0000 | [diff] [blame] | 2376 | if (merge) { |
| 2377 | struct btrfs_file_extent_item *fi; |
| 2378 | u64 extent_len; |
| 2379 | struct btrfs_key found_key; |
| 2380 | |
| 2381 | ret = btrfs_search_slot(trans, root, &key, path, 1, 1); |
| 2382 | if (ret < 0) |
| 2383 | goto out_free_path; |
| 2384 | |
| 2385 | path->slots[0]--; |
| 2386 | leaf = path->nodes[0]; |
| 2387 | btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]); |
| 2388 | |
| 2389 | fi = btrfs_item_ptr(leaf, path->slots[0], |
| 2390 | struct btrfs_file_extent_item); |
| 2391 | extent_len = btrfs_file_extent_num_bytes(leaf, fi); |
| 2392 | |
| 2393 | if (relink_is_mergable(leaf, fi, new->bytenr) && |
| 2394 | extent_len + found_key.offset == start) { |
| 2395 | btrfs_set_file_extent_num_bytes(leaf, fi, |
| 2396 | extent_len + len); |
| 2397 | btrfs_mark_buffer_dirty(leaf); |
| 2398 | inode_add_bytes(inode, len); |
| 2399 | |
| 2400 | ret = 1; |
| 2401 | goto out_free_path; |
| 2402 | } else { |
| 2403 | merge = false; |
| 2404 | btrfs_release_path(path); |
| 2405 | goto again; |
| 2406 | } |
| 2407 | } |
| 2408 | |
| 2409 | ret = btrfs_insert_empty_item(trans, root, path, &key, |
| 2410 | sizeof(*extent)); |
| 2411 | if (ret) { |
| 2412 | btrfs_abort_transaction(trans, root, ret); |
| 2413 | goto out_free_path; |
| 2414 | } |
| 2415 | |
| 2416 | leaf = path->nodes[0]; |
| 2417 | item = btrfs_item_ptr(leaf, path->slots[0], |
| 2418 | struct btrfs_file_extent_item); |
| 2419 | btrfs_set_file_extent_disk_bytenr(leaf, item, new->bytenr); |
| 2420 | btrfs_set_file_extent_disk_num_bytes(leaf, item, new->disk_len); |
| 2421 | btrfs_set_file_extent_offset(leaf, item, start - new->file_pos); |
| 2422 | btrfs_set_file_extent_num_bytes(leaf, item, len); |
| 2423 | btrfs_set_file_extent_ram_bytes(leaf, item, new->len); |
| 2424 | btrfs_set_file_extent_generation(leaf, item, trans->transid); |
| 2425 | btrfs_set_file_extent_type(leaf, item, BTRFS_FILE_EXTENT_REG); |
| 2426 | btrfs_set_file_extent_compression(leaf, item, new->compress_type); |
| 2427 | btrfs_set_file_extent_encryption(leaf, item, 0); |
| 2428 | btrfs_set_file_extent_other_encoding(leaf, item, 0); |
| 2429 | |
| 2430 | btrfs_mark_buffer_dirty(leaf); |
| 2431 | inode_add_bytes(inode, len); |
Liu Bo | a09a0a7 | 2013-03-11 09:20:58 +0000 | [diff] [blame] | 2432 | btrfs_release_path(path); |
Liu Bo | 38c227d | 2013-01-29 03:18:40 +0000 | [diff] [blame] | 2433 | |
| 2434 | ret = btrfs_inc_extent_ref(trans, root, new->bytenr, |
| 2435 | new->disk_len, 0, |
| 2436 | backref->root_id, backref->inum, |
| 2437 | new->file_pos, 0); /* start - extent_offset */ |
| 2438 | if (ret) { |
| 2439 | btrfs_abort_transaction(trans, root, ret); |
| 2440 | goto out_free_path; |
| 2441 | } |
| 2442 | |
| 2443 | ret = 1; |
| 2444 | out_free_path: |
| 2445 | btrfs_release_path(path); |
Liu Bo | a09a0a7 | 2013-03-11 09:20:58 +0000 | [diff] [blame] | 2446 | path->leave_spinning = 0; |
Liu Bo | 38c227d | 2013-01-29 03:18:40 +0000 | [diff] [blame] | 2447 | btrfs_end_transaction(trans, root); |
| 2448 | out_unlock: |
| 2449 | unlock_extent_cached(&BTRFS_I(inode)->io_tree, lock_start, lock_end, |
| 2450 | &cached, GFP_NOFS); |
| 2451 | iput(inode); |
| 2452 | return ret; |
| 2453 | } |
| 2454 | |
| 2455 | static void relink_file_extents(struct new_sa_defrag_extent *new) |
| 2456 | { |
| 2457 | struct btrfs_path *path; |
| 2458 | struct old_sa_defrag_extent *old, *tmp; |
| 2459 | struct sa_defrag_extent_backref *backref; |
| 2460 | struct sa_defrag_extent_backref *prev = NULL; |
| 2461 | struct inode *inode; |
| 2462 | struct btrfs_root *root; |
| 2463 | struct rb_node *node; |
| 2464 | int ret; |
| 2465 | |
| 2466 | inode = new->inode; |
| 2467 | root = BTRFS_I(inode)->root; |
| 2468 | |
| 2469 | path = btrfs_alloc_path(); |
| 2470 | if (!path) |
| 2471 | return; |
| 2472 | |
| 2473 | if (!record_extent_backrefs(path, new)) { |
| 2474 | btrfs_free_path(path); |
| 2475 | goto out; |
| 2476 | } |
| 2477 | btrfs_release_path(path); |
| 2478 | |
| 2479 | while (1) { |
| 2480 | node = rb_first(&new->root); |
| 2481 | if (!node) |
| 2482 | break; |
| 2483 | rb_erase(node, &new->root); |
| 2484 | |
| 2485 | backref = rb_entry(node, struct sa_defrag_extent_backref, node); |
| 2486 | |
| 2487 | ret = relink_extent_backref(path, prev, backref); |
| 2488 | WARN_ON(ret < 0); |
| 2489 | |
| 2490 | kfree(prev); |
| 2491 | |
| 2492 | if (ret == 1) |
| 2493 | prev = backref; |
| 2494 | else |
| 2495 | prev = NULL; |
| 2496 | cond_resched(); |
| 2497 | } |
| 2498 | kfree(prev); |
| 2499 | |
| 2500 | btrfs_free_path(path); |
| 2501 | |
| 2502 | list_for_each_entry_safe(old, tmp, &new->head, list) { |
| 2503 | list_del(&old->list); |
| 2504 | kfree(old); |
| 2505 | } |
| 2506 | out: |
| 2507 | atomic_dec(&root->fs_info->defrag_running); |
| 2508 | wake_up(&root->fs_info->transaction_wait); |
| 2509 | |
| 2510 | kfree(new); |
| 2511 | } |
| 2512 | |
| 2513 | static struct new_sa_defrag_extent * |
| 2514 | record_old_file_extents(struct inode *inode, |
| 2515 | struct btrfs_ordered_extent *ordered) |
| 2516 | { |
| 2517 | struct btrfs_root *root = BTRFS_I(inode)->root; |
| 2518 | struct btrfs_path *path; |
| 2519 | struct btrfs_key key; |
| 2520 | struct old_sa_defrag_extent *old, *tmp; |
| 2521 | struct new_sa_defrag_extent *new; |
| 2522 | int ret; |
| 2523 | |
| 2524 | new = kmalloc(sizeof(*new), GFP_NOFS); |
| 2525 | if (!new) |
| 2526 | return NULL; |
| 2527 | |
| 2528 | new->inode = inode; |
| 2529 | new->file_pos = ordered->file_offset; |
| 2530 | new->len = ordered->len; |
| 2531 | new->bytenr = ordered->start; |
| 2532 | new->disk_len = ordered->disk_len; |
| 2533 | new->compress_type = ordered->compress_type; |
| 2534 | new->root = RB_ROOT; |
| 2535 | INIT_LIST_HEAD(&new->head); |
| 2536 | |
| 2537 | path = btrfs_alloc_path(); |
| 2538 | if (!path) |
| 2539 | goto out_kfree; |
| 2540 | |
| 2541 | key.objectid = btrfs_ino(inode); |
| 2542 | key.type = BTRFS_EXTENT_DATA_KEY; |
| 2543 | key.offset = new->file_pos; |
| 2544 | |
| 2545 | ret = btrfs_search_slot(NULL, root, &key, path, 0, 0); |
| 2546 | if (ret < 0) |
| 2547 | goto out_free_path; |
| 2548 | if (ret > 0 && path->slots[0] > 0) |
| 2549 | path->slots[0]--; |
| 2550 | |
| 2551 | /* find out all the old extents for the file range */ |
| 2552 | while (1) { |
| 2553 | struct btrfs_file_extent_item *extent; |
| 2554 | struct extent_buffer *l; |
| 2555 | int slot; |
| 2556 | u64 num_bytes; |
| 2557 | u64 offset; |
| 2558 | u64 end; |
| 2559 | u64 disk_bytenr; |
| 2560 | u64 extent_offset; |
| 2561 | |
| 2562 | l = path->nodes[0]; |
| 2563 | slot = path->slots[0]; |
| 2564 | |
| 2565 | if (slot >= btrfs_header_nritems(l)) { |
| 2566 | ret = btrfs_next_leaf(root, path); |
| 2567 | if (ret < 0) |
| 2568 | goto out_free_list; |
| 2569 | else if (ret > 0) |
| 2570 | break; |
| 2571 | continue; |
| 2572 | } |
| 2573 | |
| 2574 | btrfs_item_key_to_cpu(l, &key, slot); |
| 2575 | |
| 2576 | if (key.objectid != btrfs_ino(inode)) |
| 2577 | break; |
| 2578 | if (key.type != BTRFS_EXTENT_DATA_KEY) |
| 2579 | break; |
| 2580 | if (key.offset >= new->file_pos + new->len) |
| 2581 | break; |
| 2582 | |
| 2583 | extent = btrfs_item_ptr(l, slot, struct btrfs_file_extent_item); |
| 2584 | |
| 2585 | num_bytes = btrfs_file_extent_num_bytes(l, extent); |
| 2586 | if (key.offset + num_bytes < new->file_pos) |
| 2587 | goto next; |
| 2588 | |
| 2589 | disk_bytenr = btrfs_file_extent_disk_bytenr(l, extent); |
| 2590 | if (!disk_bytenr) |
| 2591 | goto next; |
| 2592 | |
| 2593 | extent_offset = btrfs_file_extent_offset(l, extent); |
| 2594 | |
| 2595 | old = kmalloc(sizeof(*old), GFP_NOFS); |
| 2596 | if (!old) |
| 2597 | goto out_free_list; |
| 2598 | |
| 2599 | offset = max(new->file_pos, key.offset); |
| 2600 | end = min(new->file_pos + new->len, key.offset + num_bytes); |
| 2601 | |
| 2602 | old->bytenr = disk_bytenr; |
| 2603 | old->extent_offset = extent_offset; |
| 2604 | old->offset = offset - key.offset; |
| 2605 | old->len = end - offset; |
| 2606 | old->new = new; |
| 2607 | old->count = 0; |
| 2608 | list_add_tail(&old->list, &new->head); |
| 2609 | next: |
| 2610 | path->slots[0]++; |
| 2611 | cond_resched(); |
| 2612 | } |
| 2613 | |
| 2614 | btrfs_free_path(path); |
| 2615 | atomic_inc(&root->fs_info->defrag_running); |
| 2616 | |
| 2617 | return new; |
| 2618 | |
| 2619 | out_free_list: |
| 2620 | list_for_each_entry_safe(old, tmp, &new->head, list) { |
| 2621 | list_del(&old->list); |
| 2622 | kfree(old); |
| 2623 | } |
| 2624 | out_free_path: |
| 2625 | btrfs_free_path(path); |
| 2626 | out_kfree: |
| 2627 | kfree(new); |
| 2628 | return NULL; |
| 2629 | } |
| 2630 | |
Chris Mason | 5d13a98 | 2009-03-13 11:41:46 -0400 | [diff] [blame] | 2631 | /* |
| 2632 | * helper function for btrfs_finish_ordered_io, this |
| 2633 | * just reads in some of the csum leaves to prime them into ram |
| 2634 | * before we start the transaction. It limits the amount of btree |
| 2635 | * reads required while inside the transaction. |
| 2636 | */ |
Chris Mason | d352ac6 | 2008-09-29 15:18:18 -0400 | [diff] [blame] | 2637 | /* as ordered data IO finishes, this gets called so we can finish |
| 2638 | * an ordered extent if the range of bytes in the file it covers are |
| 2639 | * fully written. |
| 2640 | */ |
Josef Bacik | 5fd0204 | 2012-05-02 14:00:54 -0400 | [diff] [blame] | 2641 | static int btrfs_finish_ordered_io(struct btrfs_ordered_extent *ordered_extent) |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 2642 | { |
Josef Bacik | 5fd0204 | 2012-05-02 14:00:54 -0400 | [diff] [blame] | 2643 | struct inode *inode = ordered_extent->inode; |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 2644 | struct btrfs_root *root = BTRFS_I(inode)->root; |
Yan, Zheng | 0ca1f7c | 2010-05-16 10:48:47 -0400 | [diff] [blame] | 2645 | struct btrfs_trans_handle *trans = NULL; |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 2646 | struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree; |
Josef Bacik | 2ac55d4 | 2010-02-03 19:33:23 +0000 | [diff] [blame] | 2647 | struct extent_state *cached_state = NULL; |
Liu Bo | 38c227d | 2013-01-29 03:18:40 +0000 | [diff] [blame] | 2648 | struct new_sa_defrag_extent *new = NULL; |
Li Zefan | 261507a0 | 2010-12-17 14:21:50 +0800 | [diff] [blame] | 2649 | int compress_type = 0; |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 2650 | int ret; |
Li Zefan | 82d5902 | 2011-04-20 10:33:24 +0800 | [diff] [blame] | 2651 | bool nolock; |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 2652 | |
Liu Bo | 83eea1f | 2012-07-10 05:28:39 -0600 | [diff] [blame] | 2653 | nolock = btrfs_is_free_space_inode(inode); |
Josef Bacik | 0cb59c9 | 2010-07-02 12:14:14 -0400 | [diff] [blame] | 2654 | |
Josef Bacik | 5fd0204 | 2012-05-02 14:00:54 -0400 | [diff] [blame] | 2655 | if (test_bit(BTRFS_ORDERED_IOERR, &ordered_extent->flags)) { |
| 2656 | ret = -EIO; |
| 2657 | goto out; |
| 2658 | } |
| 2659 | |
Yan, Zheng | c216775 | 2009-11-12 09:34:21 +0000 | [diff] [blame] | 2660 | if (test_bit(BTRFS_ORDERED_NOCOW, &ordered_extent->flags)) { |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 2661 | BUG_ON(!list_empty(&ordered_extent->list)); /* Logic error */ |
Josef Bacik | 6c760c0 | 2012-11-09 10:53:21 -0500 | [diff] [blame] | 2662 | btrfs_ordered_update_i_size(inode, 0, ordered_extent); |
| 2663 | if (nolock) |
| 2664 | trans = btrfs_join_transaction_nolock(root); |
| 2665 | else |
| 2666 | trans = btrfs_join_transaction(root); |
| 2667 | if (IS_ERR(trans)) { |
| 2668 | ret = PTR_ERR(trans); |
| 2669 | trans = NULL; |
| 2670 | goto out; |
Yan, Zheng | c216775 | 2009-11-12 09:34:21 +0000 | [diff] [blame] | 2671 | } |
Josef Bacik | 6c760c0 | 2012-11-09 10:53:21 -0500 | [diff] [blame] | 2672 | trans->block_rsv = &root->fs_info->delalloc_block_rsv; |
| 2673 | ret = btrfs_update_inode_fallback(trans, root, inode); |
| 2674 | if (ret) /* -ENOMEM or corruption */ |
| 2675 | btrfs_abort_transaction(trans, root, ret); |
Yan, Zheng | c216775 | 2009-11-12 09:34:21 +0000 | [diff] [blame] | 2676 | goto out; |
| 2677 | } |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 2678 | |
Josef Bacik | 2ac55d4 | 2010-02-03 19:33:23 +0000 | [diff] [blame] | 2679 | lock_extent_bits(io_tree, ordered_extent->file_offset, |
| 2680 | ordered_extent->file_offset + ordered_extent->len - 1, |
Jeff Mahoney | d008237 | 2012-03-01 14:57:19 +0100 | [diff] [blame] | 2681 | 0, &cached_state); |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 2682 | |
Liu Bo | 38c227d | 2013-01-29 03:18:40 +0000 | [diff] [blame] | 2683 | ret = test_range_bit(io_tree, ordered_extent->file_offset, |
| 2684 | ordered_extent->file_offset + ordered_extent->len - 1, |
| 2685 | EXTENT_DEFRAG, 1, cached_state); |
| 2686 | if (ret) { |
| 2687 | u64 last_snapshot = btrfs_root_last_snapshot(&root->root_item); |
| 2688 | if (last_snapshot >= BTRFS_I(inode)->generation) |
| 2689 | /* the inode is shared */ |
| 2690 | new = record_old_file_extents(inode, ordered_extent); |
| 2691 | |
| 2692 | clear_extent_bit(io_tree, ordered_extent->file_offset, |
| 2693 | ordered_extent->file_offset + ordered_extent->len - 1, |
| 2694 | EXTENT_DEFRAG, 0, 0, &cached_state, GFP_NOFS); |
| 2695 | } |
| 2696 | |
Josef Bacik | 0cb59c9 | 2010-07-02 12:14:14 -0400 | [diff] [blame] | 2697 | if (nolock) |
Josef Bacik | 7a7eaa4 | 2011-04-13 12:54:33 -0400 | [diff] [blame] | 2698 | trans = btrfs_join_transaction_nolock(root); |
Josef Bacik | 0cb59c9 | 2010-07-02 12:14:14 -0400 | [diff] [blame] | 2699 | else |
Josef Bacik | 7a7eaa4 | 2011-04-13 12:54:33 -0400 | [diff] [blame] | 2700 | trans = btrfs_join_transaction(root); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 2701 | if (IS_ERR(trans)) { |
| 2702 | ret = PTR_ERR(trans); |
| 2703 | trans = NULL; |
| 2704 | goto out_unlock; |
| 2705 | } |
Yan, Zheng | 0ca1f7c | 2010-05-16 10:48:47 -0400 | [diff] [blame] | 2706 | trans->block_rsv = &root->fs_info->delalloc_block_rsv; |
Yan, Zheng | c216775 | 2009-11-12 09:34:21 +0000 | [diff] [blame] | 2707 | |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 2708 | if (test_bit(BTRFS_ORDERED_COMPRESSED, &ordered_extent->flags)) |
Li Zefan | 261507a0 | 2010-12-17 14:21:50 +0800 | [diff] [blame] | 2709 | compress_type = ordered_extent->compress_type; |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 2710 | if (test_bit(BTRFS_ORDERED_PREALLOC, &ordered_extent->flags)) { |
Li Zefan | 261507a0 | 2010-12-17 14:21:50 +0800 | [diff] [blame] | 2711 | BUG_ON(compress_type); |
Yan, Zheng | 920bbbf | 2009-11-12 09:34:08 +0000 | [diff] [blame] | 2712 | ret = btrfs_mark_extent_written(trans, inode, |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 2713 | ordered_extent->file_offset, |
| 2714 | ordered_extent->file_offset + |
| 2715 | ordered_extent->len); |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 2716 | } else { |
Josef Bacik | 0af3d00 | 2010-06-21 14:48:16 -0400 | [diff] [blame] | 2717 | BUG_ON(root == root->fs_info->tree_root); |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 2718 | ret = insert_reserved_file_extent(trans, inode, |
| 2719 | ordered_extent->file_offset, |
| 2720 | ordered_extent->start, |
| 2721 | ordered_extent->disk_len, |
| 2722 | ordered_extent->len, |
| 2723 | ordered_extent->len, |
Li Zefan | 261507a0 | 2010-12-17 14:21:50 +0800 | [diff] [blame] | 2724 | compress_type, 0, 0, |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 2725 | BTRFS_FILE_EXTENT_REG); |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 2726 | } |
Josef Bacik | 5dc562c | 2012-08-17 13:14:17 -0400 | [diff] [blame] | 2727 | unpin_extent_cache(&BTRFS_I(inode)->extent_tree, |
| 2728 | ordered_extent->file_offset, ordered_extent->len, |
| 2729 | trans->transid); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 2730 | if (ret < 0) { |
| 2731 | btrfs_abort_transaction(trans, root, ret); |
Josef Bacik | 5fd0204 | 2012-05-02 14:00:54 -0400 | [diff] [blame] | 2732 | goto out_unlock; |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 2733 | } |
Josef Bacik | 2ac55d4 | 2010-02-03 19:33:23 +0000 | [diff] [blame] | 2734 | |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 2735 | add_pending_csums(trans, inode, ordered_extent->file_offset, |
| 2736 | &ordered_extent->list); |
| 2737 | |
Josef Bacik | 6c760c0 | 2012-11-09 10:53:21 -0500 | [diff] [blame] | 2738 | btrfs_ordered_update_i_size(inode, 0, ordered_extent); |
| 2739 | ret = btrfs_update_inode_fallback(trans, root, inode); |
| 2740 | if (ret) { /* -ENOMEM or corruption */ |
| 2741 | btrfs_abort_transaction(trans, root, ret); |
| 2742 | goto out_unlock; |
Josef Bacik | 1ef30be | 2011-04-05 19:25:36 -0400 | [diff] [blame] | 2743 | } |
| 2744 | ret = 0; |
Josef Bacik | 5fd0204 | 2012-05-02 14:00:54 -0400 | [diff] [blame] | 2745 | out_unlock: |
| 2746 | unlock_extent_cached(io_tree, ordered_extent->file_offset, |
| 2747 | ordered_extent->file_offset + |
| 2748 | ordered_extent->len - 1, &cached_state, GFP_NOFS); |
Yan, Zheng | c216775 | 2009-11-12 09:34:21 +0000 | [diff] [blame] | 2749 | out: |
Josef Bacik | 5b0e95b | 2011-10-06 08:58:24 -0400 | [diff] [blame] | 2750 | if (root != root->fs_info->tree_root) |
Josef Bacik | 0cb59c9 | 2010-07-02 12:14:14 -0400 | [diff] [blame] | 2751 | btrfs_delalloc_release_metadata(inode, ordered_extent->len); |
Miao Xie | a698d075 | 2012-09-20 01:51:59 -0600 | [diff] [blame] | 2752 | if (trans) |
| 2753 | btrfs_end_transaction(trans, root); |
Josef Bacik | 0cb59c9 | 2010-07-02 12:14:14 -0400 | [diff] [blame] | 2754 | |
Josef Bacik | 0bec9ef | 2013-01-31 14:58:00 -0500 | [diff] [blame] | 2755 | if (ret) { |
Josef Bacik | 5fd0204 | 2012-05-02 14:00:54 -0400 | [diff] [blame] | 2756 | clear_extent_uptodate(io_tree, ordered_extent->file_offset, |
| 2757 | ordered_extent->file_offset + |
| 2758 | ordered_extent->len - 1, NULL, GFP_NOFS); |
| 2759 | |
Josef Bacik | 0bec9ef | 2013-01-31 14:58:00 -0500 | [diff] [blame] | 2760 | /* |
| 2761 | * If the ordered extent had an IOERR or something else went |
| 2762 | * wrong we need to return the space for this ordered extent |
| 2763 | * back to the allocator. |
| 2764 | */ |
| 2765 | if (!test_bit(BTRFS_ORDERED_NOCOW, &ordered_extent->flags) && |
| 2766 | !test_bit(BTRFS_ORDERED_PREALLOC, &ordered_extent->flags)) |
| 2767 | btrfs_free_reserved_extent(root, ordered_extent->start, |
| 2768 | ordered_extent->disk_len); |
| 2769 | } |
| 2770 | |
| 2771 | |
Josef Bacik | 5fd0204 | 2012-05-02 14:00:54 -0400 | [diff] [blame] | 2772 | /* |
Liu Bo | 8bad3c0 | 2012-06-18 12:14:23 +0800 | [diff] [blame] | 2773 | * This needs to be done to make sure anybody waiting knows we are done |
| 2774 | * updating everything for this ordered extent. |
Josef Bacik | 5fd0204 | 2012-05-02 14:00:54 -0400 | [diff] [blame] | 2775 | */ |
| 2776 | btrfs_remove_ordered_extent(inode, ordered_extent); |
| 2777 | |
Liu Bo | 38c227d | 2013-01-29 03:18:40 +0000 | [diff] [blame] | 2778 | /* for snapshot-aware defrag */ |
| 2779 | if (new) |
| 2780 | relink_file_extents(new); |
| 2781 | |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 2782 | /* once for us */ |
| 2783 | btrfs_put_ordered_extent(ordered_extent); |
| 2784 | /* once for the tree */ |
| 2785 | btrfs_put_ordered_extent(ordered_extent); |
| 2786 | |
Josef Bacik | 5fd0204 | 2012-05-02 14:00:54 -0400 | [diff] [blame] | 2787 | return ret; |
| 2788 | } |
| 2789 | |
| 2790 | static void finish_ordered_fn(struct btrfs_work *work) |
| 2791 | { |
| 2792 | struct btrfs_ordered_extent *ordered_extent; |
| 2793 | ordered_extent = container_of(work, struct btrfs_ordered_extent, work); |
| 2794 | btrfs_finish_ordered_io(ordered_extent); |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 2795 | } |
| 2796 | |
Christoph Hellwig | b295086 | 2008-12-02 09:54:17 -0500 | [diff] [blame] | 2797 | 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] | 2798 | struct extent_state *state, int uptodate) |
| 2799 | { |
Josef Bacik | 5fd0204 | 2012-05-02 14:00:54 -0400 | [diff] [blame] | 2800 | struct inode *inode = page->mapping->host; |
| 2801 | struct btrfs_root *root = BTRFS_I(inode)->root; |
| 2802 | struct btrfs_ordered_extent *ordered_extent = NULL; |
| 2803 | struct btrfs_workers *workers; |
| 2804 | |
liubo | 1abe9b8 | 2011-03-24 11:18:59 +0000 | [diff] [blame] | 2805 | trace_btrfs_writepage_end_io_hook(page, start, end, uptodate); |
| 2806 | |
Chris Mason | 8b62b72 | 2009-09-02 16:53:46 -0400 | [diff] [blame] | 2807 | ClearPagePrivate2(page); |
Josef Bacik | 5fd0204 | 2012-05-02 14:00:54 -0400 | [diff] [blame] | 2808 | if (!btrfs_dec_test_ordered_pending(inode, &ordered_extent, start, |
| 2809 | end - start + 1, uptodate)) |
| 2810 | return 0; |
| 2811 | |
| 2812 | ordered_extent->work.func = finish_ordered_fn; |
| 2813 | ordered_extent->work.flags = 0; |
| 2814 | |
Liu Bo | 83eea1f | 2012-07-10 05:28:39 -0600 | [diff] [blame] | 2815 | if (btrfs_is_free_space_inode(inode)) |
Josef Bacik | 5fd0204 | 2012-05-02 14:00:54 -0400 | [diff] [blame] | 2816 | workers = &root->fs_info->endio_freespace_worker; |
| 2817 | else |
| 2818 | workers = &root->fs_info->endio_write_workers; |
| 2819 | btrfs_queue_worker(workers, &ordered_extent->work); |
| 2820 | |
| 2821 | return 0; |
Chris Mason | 211f90e | 2008-07-18 11:56:15 -0400 | [diff] [blame] | 2822 | } |
| 2823 | |
Chris Mason | d352ac6 | 2008-09-29 15:18:18 -0400 | [diff] [blame] | 2824 | /* |
Chris Mason | d352ac6 | 2008-09-29 15:18:18 -0400 | [diff] [blame] | 2825 | * 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] | 2826 | * if there's a match, we allow the bio to finish. If not, the code in |
| 2827 | * extent_io.c will try to find good copies for us. |
Chris Mason | d352ac6 | 2008-09-29 15:18:18 -0400 | [diff] [blame] | 2828 | */ |
Miao Xie | facc8a22 | 2013-07-25 19:22:34 +0800 | [diff] [blame] | 2829 | static int btrfs_readpage_end_io_hook(struct btrfs_io_bio *io_bio, |
| 2830 | u64 phy_offset, struct page *page, |
| 2831 | u64 start, u64 end, int mirror) |
Chris Mason | 07157aa | 2007-08-30 08:50:51 -0400 | [diff] [blame] | 2832 | { |
Miao Xie | 4eee4fa | 2012-12-21 09:17:45 +0000 | [diff] [blame] | 2833 | size_t offset = start - page_offset(page); |
Chris Mason | 07157aa | 2007-08-30 08:50:51 -0400 | [diff] [blame] | 2834 | struct inode *inode = page->mapping->host; |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 2835 | struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree; |
Chris Mason | 07157aa | 2007-08-30 08:50:51 -0400 | [diff] [blame] | 2836 | char *kaddr; |
Chris Mason | ff79f81 | 2007-10-15 16:22:25 -0400 | [diff] [blame] | 2837 | struct btrfs_root *root = BTRFS_I(inode)->root; |
Miao Xie | facc8a22 | 2013-07-25 19:22:34 +0800 | [diff] [blame] | 2838 | u32 csum_expected; |
Chris Mason | ff79f81 | 2007-10-15 16:22:25 -0400 | [diff] [blame] | 2839 | u32 csum = ~(u32)0; |
Simon Kirby | c2cf52e | 2013-03-19 22:41:23 +0000 | [diff] [blame] | 2840 | static DEFINE_RATELIMIT_STATE(_rs, DEFAULT_RATELIMIT_INTERVAL, |
| 2841 | DEFAULT_RATELIMIT_BURST); |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 2842 | |
Chris Mason | d20f704 | 2008-12-08 16:58:54 -0500 | [diff] [blame] | 2843 | if (PageChecked(page)) { |
| 2844 | ClearPageChecked(page); |
| 2845 | goto good; |
| 2846 | } |
Christoph Hellwig | 6cbff00 | 2009-04-17 10:37:41 +0200 | [diff] [blame] | 2847 | |
| 2848 | if (BTRFS_I(inode)->flags & BTRFS_INODE_NODATASUM) |
Jan Schmidt | 08d2f34 | 2011-05-04 16:18:50 +0200 | [diff] [blame] | 2849 | goto good; |
Chris Mason | d20f704 | 2008-12-08 16:58:54 -0500 | [diff] [blame] | 2850 | |
Yan Zheng | 17d217f | 2008-12-12 10:03:38 -0500 | [diff] [blame] | 2851 | if (root->root_key.objectid == BTRFS_DATA_RELOC_TREE_OBJECTID && |
Chris Mason | 9655d29 | 2009-09-02 15:22:30 -0400 | [diff] [blame] | 2852 | test_range_bit(io_tree, start, end, EXTENT_NODATASUM, 1, NULL)) { |
Yan Zheng | 17d217f | 2008-12-12 10:03:38 -0500 | [diff] [blame] | 2853 | clear_extent_bits(io_tree, start, end, EXTENT_NODATASUM, |
| 2854 | GFP_NOFS); |
| 2855 | return 0; |
| 2856 | } |
| 2857 | |
Miao Xie | facc8a22 | 2013-07-25 19:22:34 +0800 | [diff] [blame] | 2858 | phy_offset >>= inode->i_sb->s_blocksize_bits; |
| 2859 | csum_expected = *(((u32 *)io_bio->csum) + phy_offset); |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 2860 | |
Miao Xie | facc8a22 | 2013-07-25 19:22:34 +0800 | [diff] [blame] | 2861 | kaddr = kmap_atomic(page); |
Liu Bo | b049668 | 2013-03-14 14:57:45 +0000 | [diff] [blame] | 2862 | csum = btrfs_csum_data(kaddr + offset, csum, end - start + 1); |
Chris Mason | ff79f81 | 2007-10-15 16:22:25 -0400 | [diff] [blame] | 2863 | btrfs_csum_final(csum, (char *)&csum); |
Miao Xie | facc8a22 | 2013-07-25 19:22:34 +0800 | [diff] [blame] | 2864 | if (csum != csum_expected) |
Chris Mason | 07157aa | 2007-08-30 08:50:51 -0400 | [diff] [blame] | 2865 | goto zeroit; |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 2866 | |
Cong Wang | 7ac687d | 2011-11-25 23:14:28 +0800 | [diff] [blame] | 2867 | kunmap_atomic(kaddr); |
Chris Mason | d20f704 | 2008-12-08 16:58:54 -0500 | [diff] [blame] | 2868 | good: |
Chris Mason | 07157aa | 2007-08-30 08:50:51 -0400 | [diff] [blame] | 2869 | return 0; |
| 2870 | |
| 2871 | zeroit: |
Simon Kirby | c2cf52e | 2013-03-19 22:41:23 +0000 | [diff] [blame] | 2872 | if (__ratelimit(&_rs)) |
Miao Xie | facc8a22 | 2013-07-25 19:22:34 +0800 | [diff] [blame] | 2873 | btrfs_info(root->fs_info, "csum failed ino %llu off %llu csum %u expected csum %u", |
Simon Kirby | c2cf52e | 2013-03-19 22:41:23 +0000 | [diff] [blame] | 2874 | (unsigned long long)btrfs_ino(page->mapping->host), |
Miao Xie | facc8a22 | 2013-07-25 19:22:34 +0800 | [diff] [blame] | 2875 | (unsigned long long)start, csum, csum_expected); |
Chris Mason | db94535 | 2007-10-15 16:15:53 -0400 | [diff] [blame] | 2876 | memset(kaddr + offset, 1, end - start + 1); |
| 2877 | flush_dcache_page(page); |
Cong Wang | 7ac687d | 2011-11-25 23:14:28 +0800 | [diff] [blame] | 2878 | kunmap_atomic(kaddr); |
Miao Xie | facc8a22 | 2013-07-25 19:22:34 +0800 | [diff] [blame] | 2879 | if (csum_expected == 0) |
Chris Mason | 3b95151 | 2008-04-17 11:29:12 -0400 | [diff] [blame] | 2880 | return 0; |
Chris Mason | 7e38326 | 2008-04-09 16:28:12 -0400 | [diff] [blame] | 2881 | return -EIO; |
Chris Mason | 07157aa | 2007-08-30 08:50:51 -0400 | [diff] [blame] | 2882 | } |
Chris Mason | b888db2 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 2883 | |
Yan, Zheng | 24bbcf0 | 2009-11-12 09:36:34 +0000 | [diff] [blame] | 2884 | struct delayed_iput { |
| 2885 | struct list_head list; |
| 2886 | struct inode *inode; |
| 2887 | }; |
| 2888 | |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 2889 | /* JDM: If this is fs-wide, why can't we add a pointer to |
| 2890 | * btrfs_inode instead and avoid the allocation? */ |
Yan, Zheng | 24bbcf0 | 2009-11-12 09:36:34 +0000 | [diff] [blame] | 2891 | void btrfs_add_delayed_iput(struct inode *inode) |
| 2892 | { |
| 2893 | struct btrfs_fs_info *fs_info = BTRFS_I(inode)->root->fs_info; |
| 2894 | struct delayed_iput *delayed; |
| 2895 | |
| 2896 | if (atomic_add_unless(&inode->i_count, -1, 1)) |
| 2897 | return; |
| 2898 | |
| 2899 | delayed = kmalloc(sizeof(*delayed), GFP_NOFS | __GFP_NOFAIL); |
| 2900 | delayed->inode = inode; |
| 2901 | |
| 2902 | spin_lock(&fs_info->delayed_iput_lock); |
| 2903 | list_add_tail(&delayed->list, &fs_info->delayed_iputs); |
| 2904 | spin_unlock(&fs_info->delayed_iput_lock); |
| 2905 | } |
| 2906 | |
| 2907 | void btrfs_run_delayed_iputs(struct btrfs_root *root) |
| 2908 | { |
| 2909 | LIST_HEAD(list); |
| 2910 | struct btrfs_fs_info *fs_info = root->fs_info; |
| 2911 | struct delayed_iput *delayed; |
| 2912 | int empty; |
| 2913 | |
| 2914 | spin_lock(&fs_info->delayed_iput_lock); |
| 2915 | empty = list_empty(&fs_info->delayed_iputs); |
| 2916 | spin_unlock(&fs_info->delayed_iput_lock); |
| 2917 | if (empty) |
| 2918 | return; |
| 2919 | |
Yan, Zheng | 24bbcf0 | 2009-11-12 09:36:34 +0000 | [diff] [blame] | 2920 | spin_lock(&fs_info->delayed_iput_lock); |
| 2921 | list_splice_init(&fs_info->delayed_iputs, &list); |
| 2922 | spin_unlock(&fs_info->delayed_iput_lock); |
| 2923 | |
| 2924 | while (!list_empty(&list)) { |
| 2925 | delayed = list_entry(list.next, struct delayed_iput, list); |
| 2926 | list_del(&delayed->list); |
| 2927 | iput(delayed->inode); |
| 2928 | kfree(delayed); |
| 2929 | } |
Yan, Zheng | 24bbcf0 | 2009-11-12 09:36:34 +0000 | [diff] [blame] | 2930 | } |
| 2931 | |
Yan, Zheng | d68fc57 | 2010-05-16 10:49:58 -0400 | [diff] [blame] | 2932 | /* |
Justin P. Mattock | 42b2aa8 | 2011-11-28 20:31:00 -0800 | [diff] [blame] | 2933 | * This is called in transaction commit time. If there are no orphan |
Yan, Zheng | d68fc57 | 2010-05-16 10:49:58 -0400 | [diff] [blame] | 2934 | * files in the subvolume, it removes orphan item and frees block_rsv |
| 2935 | * structure. |
| 2936 | */ |
| 2937 | void btrfs_orphan_commit_root(struct btrfs_trans_handle *trans, |
| 2938 | struct btrfs_root *root) |
| 2939 | { |
Josef Bacik | 90290e1 | 2011-12-02 15:44:12 -0500 | [diff] [blame] | 2940 | struct btrfs_block_rsv *block_rsv; |
Yan, Zheng | d68fc57 | 2010-05-16 10:49:58 -0400 | [diff] [blame] | 2941 | int ret; |
| 2942 | |
Josef Bacik | 8a35d95 | 2012-05-23 14:26:42 -0400 | [diff] [blame] | 2943 | if (atomic_read(&root->orphan_inodes) || |
Yan, Zheng | d68fc57 | 2010-05-16 10:49:58 -0400 | [diff] [blame] | 2944 | root->orphan_cleanup_state != ORPHAN_CLEANUP_DONE) |
| 2945 | return; |
| 2946 | |
Josef Bacik | 90290e1 | 2011-12-02 15:44:12 -0500 | [diff] [blame] | 2947 | spin_lock(&root->orphan_lock); |
Josef Bacik | 8a35d95 | 2012-05-23 14:26:42 -0400 | [diff] [blame] | 2948 | if (atomic_read(&root->orphan_inodes)) { |
Josef Bacik | 90290e1 | 2011-12-02 15:44:12 -0500 | [diff] [blame] | 2949 | spin_unlock(&root->orphan_lock); |
| 2950 | return; |
| 2951 | } |
| 2952 | |
| 2953 | if (root->orphan_cleanup_state != ORPHAN_CLEANUP_DONE) { |
| 2954 | spin_unlock(&root->orphan_lock); |
| 2955 | return; |
| 2956 | } |
| 2957 | |
| 2958 | block_rsv = root->orphan_block_rsv; |
| 2959 | root->orphan_block_rsv = NULL; |
| 2960 | spin_unlock(&root->orphan_lock); |
| 2961 | |
Yan, Zheng | d68fc57 | 2010-05-16 10:49:58 -0400 | [diff] [blame] | 2962 | if (root->orphan_item_inserted && |
| 2963 | btrfs_root_refs(&root->root_item) > 0) { |
| 2964 | ret = btrfs_del_orphan_item(trans, root->fs_info->tree_root, |
| 2965 | root->root_key.objectid); |
| 2966 | BUG_ON(ret); |
| 2967 | root->orphan_item_inserted = 0; |
| 2968 | } |
| 2969 | |
Josef Bacik | 90290e1 | 2011-12-02 15:44:12 -0500 | [diff] [blame] | 2970 | if (block_rsv) { |
| 2971 | WARN_ON(block_rsv->size > 0); |
| 2972 | btrfs_free_block_rsv(root, block_rsv); |
Yan, Zheng | d68fc57 | 2010-05-16 10:49:58 -0400 | [diff] [blame] | 2973 | } |
| 2974 | } |
| 2975 | |
| 2976 | /* |
Josef Bacik | 7b12876 | 2008-07-24 12:17:14 -0400 | [diff] [blame] | 2977 | * This creates an orphan entry for the given inode in case something goes |
| 2978 | * wrong in the middle of an unlink/truncate. |
Yan, Zheng | d68fc57 | 2010-05-16 10:49:58 -0400 | [diff] [blame] | 2979 | * |
| 2980 | * NOTE: caller of this function should reserve 5 units of metadata for |
| 2981 | * this function. |
Josef Bacik | 7b12876 | 2008-07-24 12:17:14 -0400 | [diff] [blame] | 2982 | */ |
| 2983 | int btrfs_orphan_add(struct btrfs_trans_handle *trans, struct inode *inode) |
| 2984 | { |
| 2985 | struct btrfs_root *root = BTRFS_I(inode)->root; |
Yan, Zheng | d68fc57 | 2010-05-16 10:49:58 -0400 | [diff] [blame] | 2986 | struct btrfs_block_rsv *block_rsv = NULL; |
| 2987 | int reserve = 0; |
| 2988 | int insert = 0; |
| 2989 | int ret; |
Josef Bacik | 7b12876 | 2008-07-24 12:17:14 -0400 | [diff] [blame] | 2990 | |
Yan, Zheng | d68fc57 | 2010-05-16 10:49:58 -0400 | [diff] [blame] | 2991 | if (!root->orphan_block_rsv) { |
Miao Xie | 66d8f3d | 2012-09-06 04:02:28 -0600 | [diff] [blame] | 2992 | block_rsv = btrfs_alloc_block_rsv(root, BTRFS_BLOCK_RSV_TEMP); |
Tsutomu Itoh | b532402 | 2011-07-19 07:27:20 +0000 | [diff] [blame] | 2993 | if (!block_rsv) |
| 2994 | return -ENOMEM; |
Josef Bacik | 7b12876 | 2008-07-24 12:17:14 -0400 | [diff] [blame] | 2995 | } |
| 2996 | |
Yan, Zheng | d68fc57 | 2010-05-16 10:49:58 -0400 | [diff] [blame] | 2997 | spin_lock(&root->orphan_lock); |
| 2998 | if (!root->orphan_block_rsv) { |
| 2999 | root->orphan_block_rsv = block_rsv; |
| 3000 | } else if (block_rsv) { |
| 3001 | btrfs_free_block_rsv(root, block_rsv); |
| 3002 | block_rsv = NULL; |
| 3003 | } |
Josef Bacik | 7b12876 | 2008-07-24 12:17:14 -0400 | [diff] [blame] | 3004 | |
Josef Bacik | 8a35d95 | 2012-05-23 14:26:42 -0400 | [diff] [blame] | 3005 | if (!test_and_set_bit(BTRFS_INODE_HAS_ORPHAN_ITEM, |
| 3006 | &BTRFS_I(inode)->runtime_flags)) { |
Yan, Zheng | d68fc57 | 2010-05-16 10:49:58 -0400 | [diff] [blame] | 3007 | #if 0 |
| 3008 | /* |
| 3009 | * For proper ENOSPC handling, we should do orphan |
| 3010 | * cleanup when mounting. But this introduces backward |
| 3011 | * compatibility issue. |
| 3012 | */ |
| 3013 | if (!xchg(&root->orphan_item_inserted, 1)) |
| 3014 | insert = 2; |
| 3015 | else |
| 3016 | insert = 1; |
| 3017 | #endif |
| 3018 | insert = 1; |
Miao Xie | 321f0e7 | 2012-08-28 22:13:02 -0600 | [diff] [blame] | 3019 | atomic_inc(&root->orphan_inodes); |
Yan, Zheng | d68fc57 | 2010-05-16 10:49:58 -0400 | [diff] [blame] | 3020 | } |
Josef Bacik | 7b12876 | 2008-07-24 12:17:14 -0400 | [diff] [blame] | 3021 | |
Josef Bacik | 72ac3c0 | 2012-05-23 14:13:11 -0400 | [diff] [blame] | 3022 | if (!test_and_set_bit(BTRFS_INODE_ORPHAN_META_RESERVED, |
| 3023 | &BTRFS_I(inode)->runtime_flags)) |
Yan, Zheng | d68fc57 | 2010-05-16 10:49:58 -0400 | [diff] [blame] | 3024 | reserve = 1; |
Yan, Zheng | d68fc57 | 2010-05-16 10:49:58 -0400 | [diff] [blame] | 3025 | spin_unlock(&root->orphan_lock); |
Josef Bacik | 7b12876 | 2008-07-24 12:17:14 -0400 | [diff] [blame] | 3026 | |
Yan, Zheng | d68fc57 | 2010-05-16 10:49:58 -0400 | [diff] [blame] | 3027 | /* grab metadata reservation from transaction handle */ |
| 3028 | if (reserve) { |
| 3029 | ret = btrfs_orphan_reserve_metadata(trans, inode); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 3030 | BUG_ON(ret); /* -ENOSPC in reservation; Logic error? JDM */ |
Yan, Zheng | d68fc57 | 2010-05-16 10:49:58 -0400 | [diff] [blame] | 3031 | } |
| 3032 | |
| 3033 | /* insert an orphan item to track this unlinked/truncated file */ |
| 3034 | if (insert >= 1) { |
Li Zefan | 33345d01 | 2011-04-20 10:31:50 +0800 | [diff] [blame] | 3035 | ret = btrfs_insert_orphan_item(trans, root, btrfs_ino(inode)); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 3036 | if (ret && ret != -EEXIST) { |
Josef Bacik | 8a35d95 | 2012-05-23 14:26:42 -0400 | [diff] [blame] | 3037 | clear_bit(BTRFS_INODE_HAS_ORPHAN_ITEM, |
| 3038 | &BTRFS_I(inode)->runtime_flags); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 3039 | btrfs_abort_transaction(trans, root, ret); |
| 3040 | return ret; |
| 3041 | } |
| 3042 | ret = 0; |
Yan, Zheng | d68fc57 | 2010-05-16 10:49:58 -0400 | [diff] [blame] | 3043 | } |
| 3044 | |
| 3045 | /* insert an orphan item to track subvolume contains orphan files */ |
| 3046 | if (insert >= 2) { |
| 3047 | ret = btrfs_insert_orphan_item(trans, root->fs_info->tree_root, |
| 3048 | root->root_key.objectid); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 3049 | if (ret && ret != -EEXIST) { |
| 3050 | btrfs_abort_transaction(trans, root, ret); |
| 3051 | return ret; |
| 3052 | } |
Yan, Zheng | d68fc57 | 2010-05-16 10:49:58 -0400 | [diff] [blame] | 3053 | } |
| 3054 | return 0; |
Josef Bacik | 7b12876 | 2008-07-24 12:17:14 -0400 | [diff] [blame] | 3055 | } |
| 3056 | |
| 3057 | /* |
| 3058 | * We have done the truncate/delete so we can go ahead and remove the orphan |
| 3059 | * item for this particular inode. |
| 3060 | */ |
Eric Sandeen | 48a3b63 | 2013-04-25 20:41:01 +0000 | [diff] [blame] | 3061 | static int btrfs_orphan_del(struct btrfs_trans_handle *trans, |
| 3062 | struct inode *inode) |
Josef Bacik | 7b12876 | 2008-07-24 12:17:14 -0400 | [diff] [blame] | 3063 | { |
| 3064 | struct btrfs_root *root = BTRFS_I(inode)->root; |
Yan, Zheng | d68fc57 | 2010-05-16 10:49:58 -0400 | [diff] [blame] | 3065 | int delete_item = 0; |
| 3066 | int release_rsv = 0; |
Josef Bacik | 7b12876 | 2008-07-24 12:17:14 -0400 | [diff] [blame] | 3067 | int ret = 0; |
| 3068 | |
Yan, Zheng | d68fc57 | 2010-05-16 10:49:58 -0400 | [diff] [blame] | 3069 | spin_lock(&root->orphan_lock); |
Josef Bacik | 8a35d95 | 2012-05-23 14:26:42 -0400 | [diff] [blame] | 3070 | if (test_and_clear_bit(BTRFS_INODE_HAS_ORPHAN_ITEM, |
| 3071 | &BTRFS_I(inode)->runtime_flags)) |
Yan, Zheng | d68fc57 | 2010-05-16 10:49:58 -0400 | [diff] [blame] | 3072 | delete_item = 1; |
Josef Bacik | 7b12876 | 2008-07-24 12:17:14 -0400 | [diff] [blame] | 3073 | |
Josef Bacik | 72ac3c0 | 2012-05-23 14:13:11 -0400 | [diff] [blame] | 3074 | if (test_and_clear_bit(BTRFS_INODE_ORPHAN_META_RESERVED, |
| 3075 | &BTRFS_I(inode)->runtime_flags)) |
Yan, Zheng | d68fc57 | 2010-05-16 10:49:58 -0400 | [diff] [blame] | 3076 | release_rsv = 1; |
Yan, Zheng | d68fc57 | 2010-05-16 10:49:58 -0400 | [diff] [blame] | 3077 | spin_unlock(&root->orphan_lock); |
| 3078 | |
| 3079 | if (trans && delete_item) { |
Li Zefan | 33345d01 | 2011-04-20 10:31:50 +0800 | [diff] [blame] | 3080 | ret = btrfs_del_orphan_item(trans, root, btrfs_ino(inode)); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 3081 | BUG_ON(ret); /* -ENOMEM or corruption (JDM: Recheck) */ |
Josef Bacik | 7b12876 | 2008-07-24 12:17:14 -0400 | [diff] [blame] | 3082 | } |
| 3083 | |
Josef Bacik | 8a35d95 | 2012-05-23 14:26:42 -0400 | [diff] [blame] | 3084 | if (release_rsv) { |
Yan, Zheng | d68fc57 | 2010-05-16 10:49:58 -0400 | [diff] [blame] | 3085 | btrfs_orphan_release_metadata(inode); |
Josef Bacik | 8a35d95 | 2012-05-23 14:26:42 -0400 | [diff] [blame] | 3086 | atomic_dec(&root->orphan_inodes); |
| 3087 | } |
Josef Bacik | 7b12876 | 2008-07-24 12:17:14 -0400 | [diff] [blame] | 3088 | |
Yan, Zheng | d68fc57 | 2010-05-16 10:49:58 -0400 | [diff] [blame] | 3089 | return 0; |
Josef Bacik | 7b12876 | 2008-07-24 12:17:14 -0400 | [diff] [blame] | 3090 | } |
| 3091 | |
| 3092 | /* |
| 3093 | * this cleans up any orphans that may be left on the list from the last use |
| 3094 | * of this root. |
| 3095 | */ |
Josef Bacik | 66b4ffd | 2011-01-31 16:22:42 -0500 | [diff] [blame] | 3096 | int btrfs_orphan_cleanup(struct btrfs_root *root) |
Josef Bacik | 7b12876 | 2008-07-24 12:17:14 -0400 | [diff] [blame] | 3097 | { |
| 3098 | struct btrfs_path *path; |
| 3099 | struct extent_buffer *leaf; |
Josef Bacik | 7b12876 | 2008-07-24 12:17:14 -0400 | [diff] [blame] | 3100 | struct btrfs_key key, found_key; |
| 3101 | struct btrfs_trans_handle *trans; |
| 3102 | struct inode *inode; |
Josef Bacik | 8f6d7f4 | 2011-09-26 15:55:20 -0400 | [diff] [blame] | 3103 | u64 last_objectid = 0; |
Josef Bacik | 7b12876 | 2008-07-24 12:17:14 -0400 | [diff] [blame] | 3104 | int ret = 0, nr_unlink = 0, nr_truncate = 0; |
| 3105 | |
Yan, Zheng | d68fc57 | 2010-05-16 10:49:58 -0400 | [diff] [blame] | 3106 | if (cmpxchg(&root->orphan_cleanup_state, 0, ORPHAN_CLEANUP_STARTED)) |
Josef Bacik | 66b4ffd | 2011-01-31 16:22:42 -0500 | [diff] [blame] | 3107 | return 0; |
Yan, Zheng | c71bf09 | 2009-11-12 09:34:40 +0000 | [diff] [blame] | 3108 | |
| 3109 | path = btrfs_alloc_path(); |
Josef Bacik | 66b4ffd | 2011-01-31 16:22:42 -0500 | [diff] [blame] | 3110 | if (!path) { |
| 3111 | ret = -ENOMEM; |
| 3112 | goto out; |
| 3113 | } |
Josef Bacik | 7b12876 | 2008-07-24 12:17:14 -0400 | [diff] [blame] | 3114 | path->reada = -1; |
| 3115 | |
| 3116 | key.objectid = BTRFS_ORPHAN_OBJECTID; |
| 3117 | btrfs_set_key_type(&key, BTRFS_ORPHAN_ITEM_KEY); |
| 3118 | key.offset = (u64)-1; |
| 3119 | |
Josef Bacik | 7b12876 | 2008-07-24 12:17:14 -0400 | [diff] [blame] | 3120 | while (1) { |
| 3121 | ret = btrfs_search_slot(NULL, root, &key, path, 0, 0); |
Josef Bacik | 66b4ffd | 2011-01-31 16:22:42 -0500 | [diff] [blame] | 3122 | if (ret < 0) |
| 3123 | goto out; |
Josef Bacik | 7b12876 | 2008-07-24 12:17:14 -0400 | [diff] [blame] | 3124 | |
| 3125 | /* |
| 3126 | * 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] | 3127 | * 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] | 3128 | * find the key and see if we have stuff that matches |
| 3129 | */ |
| 3130 | if (ret > 0) { |
Josef Bacik | 66b4ffd | 2011-01-31 16:22:42 -0500 | [diff] [blame] | 3131 | ret = 0; |
Josef Bacik | 7b12876 | 2008-07-24 12:17:14 -0400 | [diff] [blame] | 3132 | if (path->slots[0] == 0) |
| 3133 | break; |
| 3134 | path->slots[0]--; |
| 3135 | } |
| 3136 | |
| 3137 | /* pull out the item */ |
| 3138 | leaf = path->nodes[0]; |
Josef Bacik | 7b12876 | 2008-07-24 12:17:14 -0400 | [diff] [blame] | 3139 | btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]); |
| 3140 | |
| 3141 | /* make sure the item matches what we want */ |
| 3142 | if (found_key.objectid != BTRFS_ORPHAN_OBJECTID) |
| 3143 | break; |
| 3144 | if (btrfs_key_type(&found_key) != BTRFS_ORPHAN_ITEM_KEY) |
| 3145 | break; |
| 3146 | |
| 3147 | /* release the path since we're done with it */ |
David Sterba | b3b4aa7 | 2011-04-21 01:20:15 +0200 | [diff] [blame] | 3148 | btrfs_release_path(path); |
Josef Bacik | 7b12876 | 2008-07-24 12:17:14 -0400 | [diff] [blame] | 3149 | |
| 3150 | /* |
| 3151 | * this is where we are basically btrfs_lookup, without the |
| 3152 | * crossing root thing. we store the inode number in the |
| 3153 | * offset of the orphan item. |
| 3154 | */ |
Josef Bacik | 8f6d7f4 | 2011-09-26 15:55:20 -0400 | [diff] [blame] | 3155 | |
| 3156 | if (found_key.offset == last_objectid) { |
Simon Kirby | c2cf52e | 2013-03-19 22:41:23 +0000 | [diff] [blame] | 3157 | btrfs_err(root->fs_info, |
| 3158 | "Error removing orphan entry, stopping orphan cleanup"); |
Josef Bacik | 8f6d7f4 | 2011-09-26 15:55:20 -0400 | [diff] [blame] | 3159 | ret = -EINVAL; |
| 3160 | goto out; |
| 3161 | } |
| 3162 | |
| 3163 | last_objectid = found_key.offset; |
| 3164 | |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 3165 | found_key.objectid = found_key.offset; |
| 3166 | found_key.type = BTRFS_INODE_ITEM_KEY; |
| 3167 | found_key.offset = 0; |
Josef Bacik | 73f7341 | 2009-12-04 17:38:27 +0000 | [diff] [blame] | 3168 | inode = btrfs_iget(root->fs_info->sb, &found_key, root, NULL); |
Josef Bacik | a8c9e57 | 2011-09-21 16:55:59 -0400 | [diff] [blame] | 3169 | ret = PTR_RET(inode); |
| 3170 | if (ret && ret != -ESTALE) |
Josef Bacik | 66b4ffd | 2011-01-31 16:22:42 -0500 | [diff] [blame] | 3171 | goto out; |
Josef Bacik | a8c9e57 | 2011-09-21 16:55:59 -0400 | [diff] [blame] | 3172 | |
Arne Jansen | f8e9e0b | 2011-12-14 20:12:02 -0500 | [diff] [blame] | 3173 | if (ret == -ESTALE && root == root->fs_info->tree_root) { |
| 3174 | struct btrfs_root *dead_root; |
| 3175 | struct btrfs_fs_info *fs_info = root->fs_info; |
| 3176 | int is_dead_root = 0; |
| 3177 | |
| 3178 | /* |
| 3179 | * this is an orphan in the tree root. Currently these |
| 3180 | * could come from 2 sources: |
| 3181 | * a) a snapshot deletion in progress |
| 3182 | * b) a free space cache inode |
| 3183 | * We need to distinguish those two, as the snapshot |
| 3184 | * orphan must not get deleted. |
| 3185 | * find_dead_roots already ran before us, so if this |
| 3186 | * is a snapshot deletion, we should find the root |
| 3187 | * in the dead_roots list |
| 3188 | */ |
| 3189 | spin_lock(&fs_info->trans_lock); |
| 3190 | list_for_each_entry(dead_root, &fs_info->dead_roots, |
| 3191 | root_list) { |
| 3192 | if (dead_root->root_key.objectid == |
| 3193 | found_key.objectid) { |
| 3194 | is_dead_root = 1; |
| 3195 | break; |
| 3196 | } |
| 3197 | } |
| 3198 | spin_unlock(&fs_info->trans_lock); |
| 3199 | if (is_dead_root) { |
| 3200 | /* prevent this orphan from being found again */ |
| 3201 | key.offset = found_key.objectid - 1; |
| 3202 | continue; |
| 3203 | } |
| 3204 | } |
Josef Bacik | a8c9e57 | 2011-09-21 16:55:59 -0400 | [diff] [blame] | 3205 | /* |
| 3206 | * Inode is already gone but the orphan item is still there, |
| 3207 | * kill the orphan item. |
| 3208 | */ |
| 3209 | if (ret == -ESTALE) { |
| 3210 | trans = btrfs_start_transaction(root, 1); |
| 3211 | if (IS_ERR(trans)) { |
| 3212 | ret = PTR_ERR(trans); |
| 3213 | goto out; |
| 3214 | } |
Simon Kirby | c2cf52e | 2013-03-19 22:41:23 +0000 | [diff] [blame] | 3215 | btrfs_debug(root->fs_info, "auto deleting %Lu", |
| 3216 | found_key.objectid); |
Josef Bacik | a8c9e57 | 2011-09-21 16:55:59 -0400 | [diff] [blame] | 3217 | ret = btrfs_del_orphan_item(trans, root, |
| 3218 | found_key.objectid); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 3219 | BUG_ON(ret); /* -ENOMEM or corruption (JDM: Recheck) */ |
Josef Bacik | a8c9e57 | 2011-09-21 16:55:59 -0400 | [diff] [blame] | 3220 | btrfs_end_transaction(trans, root); |
| 3221 | continue; |
Josef Bacik | 66b4ffd | 2011-01-31 16:22:42 -0500 | [diff] [blame] | 3222 | } |
Josef Bacik | 7b12876 | 2008-07-24 12:17:14 -0400 | [diff] [blame] | 3223 | |
Josef Bacik | 7b12876 | 2008-07-24 12:17:14 -0400 | [diff] [blame] | 3224 | /* |
| 3225 | * add this inode to the orphan list so btrfs_orphan_del does |
| 3226 | * the proper thing when we hit it |
| 3227 | */ |
Josef Bacik | 8a35d95 | 2012-05-23 14:26:42 -0400 | [diff] [blame] | 3228 | set_bit(BTRFS_INODE_HAS_ORPHAN_ITEM, |
| 3229 | &BTRFS_I(inode)->runtime_flags); |
Josef Bacik | 925396e | 2013-02-01 15:57:47 -0500 | [diff] [blame] | 3230 | atomic_inc(&root->orphan_inodes); |
Josef Bacik | 7b12876 | 2008-07-24 12:17:14 -0400 | [diff] [blame] | 3231 | |
Josef Bacik | 7b12876 | 2008-07-24 12:17:14 -0400 | [diff] [blame] | 3232 | /* if we have links, this was a truncate, lets do that */ |
| 3233 | if (inode->i_nlink) { |
Josef Bacik | a41ad39 | 2011-01-31 15:30:16 -0500 | [diff] [blame] | 3234 | if (!S_ISREG(inode->i_mode)) { |
| 3235 | WARN_ON(1); |
| 3236 | iput(inode); |
| 3237 | continue; |
| 3238 | } |
Josef Bacik | 7b12876 | 2008-07-24 12:17:14 -0400 | [diff] [blame] | 3239 | nr_truncate++; |
Josef Bacik | f3fe820 | 2013-01-07 17:03:21 -0500 | [diff] [blame] | 3240 | |
| 3241 | /* 1 for the orphan item deletion. */ |
| 3242 | trans = btrfs_start_transaction(root, 1); |
| 3243 | if (IS_ERR(trans)) { |
Josef Bacik | c69b26b | 2013-06-03 16:51:23 -0400 | [diff] [blame] | 3244 | iput(inode); |
Josef Bacik | f3fe820 | 2013-01-07 17:03:21 -0500 | [diff] [blame] | 3245 | ret = PTR_ERR(trans); |
| 3246 | goto out; |
| 3247 | } |
| 3248 | ret = btrfs_orphan_add(trans, inode); |
| 3249 | btrfs_end_transaction(trans, root); |
Josef Bacik | c69b26b | 2013-06-03 16:51:23 -0400 | [diff] [blame] | 3250 | if (ret) { |
| 3251 | iput(inode); |
Josef Bacik | f3fe820 | 2013-01-07 17:03:21 -0500 | [diff] [blame] | 3252 | goto out; |
Josef Bacik | c69b26b | 2013-06-03 16:51:23 -0400 | [diff] [blame] | 3253 | } |
Josef Bacik | f3fe820 | 2013-01-07 17:03:21 -0500 | [diff] [blame] | 3254 | |
Josef Bacik | 66b4ffd | 2011-01-31 16:22:42 -0500 | [diff] [blame] | 3255 | ret = btrfs_truncate(inode); |
Josef Bacik | 4a7d0f6 | 2013-02-07 16:27:28 -0500 | [diff] [blame] | 3256 | if (ret) |
| 3257 | btrfs_orphan_del(NULL, inode); |
Josef Bacik | 7b12876 | 2008-07-24 12:17:14 -0400 | [diff] [blame] | 3258 | } else { |
| 3259 | nr_unlink++; |
| 3260 | } |
| 3261 | |
| 3262 | /* this will do delete_inode and everything for us */ |
| 3263 | iput(inode); |
Josef Bacik | 66b4ffd | 2011-01-31 16:22:42 -0500 | [diff] [blame] | 3264 | if (ret) |
| 3265 | goto out; |
Josef Bacik | 7b12876 | 2008-07-24 12:17:14 -0400 | [diff] [blame] | 3266 | } |
Miao Xie | 3254c87 | 2011-11-10 20:45:05 -0500 | [diff] [blame] | 3267 | /* release the path since we're done with it */ |
| 3268 | btrfs_release_path(path); |
| 3269 | |
Yan, Zheng | d68fc57 | 2010-05-16 10:49:58 -0400 | [diff] [blame] | 3270 | root->orphan_cleanup_state = ORPHAN_CLEANUP_DONE; |
| 3271 | |
| 3272 | if (root->orphan_block_rsv) |
| 3273 | btrfs_block_rsv_release(root, root->orphan_block_rsv, |
| 3274 | (u64)-1); |
| 3275 | |
| 3276 | if (root->orphan_block_rsv || root->orphan_item_inserted) { |
Josef Bacik | 7a7eaa4 | 2011-04-13 12:54:33 -0400 | [diff] [blame] | 3277 | trans = btrfs_join_transaction(root); |
Josef Bacik | 66b4ffd | 2011-01-31 16:22:42 -0500 | [diff] [blame] | 3278 | if (!IS_ERR(trans)) |
| 3279 | btrfs_end_transaction(trans, root); |
Yan, Zheng | d68fc57 | 2010-05-16 10:49:58 -0400 | [diff] [blame] | 3280 | } |
Josef Bacik | 7b12876 | 2008-07-24 12:17:14 -0400 | [diff] [blame] | 3281 | |
| 3282 | if (nr_unlink) |
David Sterba | 4884b47 | 2013-03-20 13:31:27 +0000 | [diff] [blame] | 3283 | btrfs_debug(root->fs_info, "unlinked %d orphans", nr_unlink); |
Josef Bacik | 7b12876 | 2008-07-24 12:17:14 -0400 | [diff] [blame] | 3284 | if (nr_truncate) |
David Sterba | 4884b47 | 2013-03-20 13:31:27 +0000 | [diff] [blame] | 3285 | btrfs_debug(root->fs_info, "truncated %d orphans", nr_truncate); |
Josef Bacik | 66b4ffd | 2011-01-31 16:22:42 -0500 | [diff] [blame] | 3286 | |
| 3287 | out: |
| 3288 | if (ret) |
Simon Kirby | c2cf52e | 2013-03-19 22:41:23 +0000 | [diff] [blame] | 3289 | btrfs_crit(root->fs_info, |
| 3290 | "could not do orphan cleanup %d", ret); |
Josef Bacik | 66b4ffd | 2011-01-31 16:22:42 -0500 | [diff] [blame] | 3291 | btrfs_free_path(path); |
| 3292 | return ret; |
Josef Bacik | 7b12876 | 2008-07-24 12:17:14 -0400 | [diff] [blame] | 3293 | } |
| 3294 | |
Chris Mason | d352ac6 | 2008-09-29 15:18:18 -0400 | [diff] [blame] | 3295 | /* |
Chris Mason | 46a53cc | 2009-04-27 11:47:50 -0400 | [diff] [blame] | 3296 | * very simple check to peek ahead in the leaf looking for xattrs. If we |
| 3297 | * don't find any xattrs, we know there can't be any acls. |
| 3298 | * |
| 3299 | * slot is the slot the inode is in, objectid is the objectid of the inode |
| 3300 | */ |
| 3301 | static noinline int acls_after_inode_item(struct extent_buffer *leaf, |
| 3302 | int slot, u64 objectid) |
| 3303 | { |
| 3304 | u32 nritems = btrfs_header_nritems(leaf); |
| 3305 | struct btrfs_key found_key; |
Josef Bacik | f23b5a5 | 2013-06-19 10:16:26 -0400 | [diff] [blame] | 3306 | static u64 xattr_access = 0; |
| 3307 | static u64 xattr_default = 0; |
Chris Mason | 46a53cc | 2009-04-27 11:47:50 -0400 | [diff] [blame] | 3308 | int scanned = 0; |
| 3309 | |
Josef Bacik | f23b5a5 | 2013-06-19 10:16:26 -0400 | [diff] [blame] | 3310 | if (!xattr_access) { |
| 3311 | xattr_access = btrfs_name_hash(POSIX_ACL_XATTR_ACCESS, |
| 3312 | strlen(POSIX_ACL_XATTR_ACCESS)); |
| 3313 | xattr_default = btrfs_name_hash(POSIX_ACL_XATTR_DEFAULT, |
| 3314 | strlen(POSIX_ACL_XATTR_DEFAULT)); |
| 3315 | } |
| 3316 | |
Chris Mason | 46a53cc | 2009-04-27 11:47:50 -0400 | [diff] [blame] | 3317 | slot++; |
| 3318 | while (slot < nritems) { |
| 3319 | btrfs_item_key_to_cpu(leaf, &found_key, slot); |
| 3320 | |
| 3321 | /* we found a different objectid, there must not be acls */ |
| 3322 | if (found_key.objectid != objectid) |
| 3323 | return 0; |
| 3324 | |
| 3325 | /* we found an xattr, assume we've got an acl */ |
Josef Bacik | f23b5a5 | 2013-06-19 10:16:26 -0400 | [diff] [blame] | 3326 | if (found_key.type == BTRFS_XATTR_ITEM_KEY) { |
| 3327 | if (found_key.offset == xattr_access || |
| 3328 | found_key.offset == xattr_default) |
| 3329 | return 1; |
| 3330 | } |
Chris Mason | 46a53cc | 2009-04-27 11:47:50 -0400 | [diff] [blame] | 3331 | |
| 3332 | /* |
| 3333 | * we found a key greater than an xattr key, there can't |
| 3334 | * be any acls later on |
| 3335 | */ |
| 3336 | if (found_key.type > BTRFS_XATTR_ITEM_KEY) |
| 3337 | return 0; |
| 3338 | |
| 3339 | slot++; |
| 3340 | scanned++; |
| 3341 | |
| 3342 | /* |
| 3343 | * it goes inode, inode backrefs, xattrs, extents, |
| 3344 | * so if there are a ton of hard links to an inode there can |
| 3345 | * be a lot of backrefs. Don't waste time searching too hard, |
| 3346 | * this is just an optimization |
| 3347 | */ |
| 3348 | if (scanned >= 8) |
| 3349 | break; |
| 3350 | } |
| 3351 | /* we hit the end of the leaf before we found an xattr or |
| 3352 | * something larger than an xattr. We have to assume the inode |
| 3353 | * has acls |
| 3354 | */ |
| 3355 | return 1; |
| 3356 | } |
| 3357 | |
| 3358 | /* |
Chris Mason | d352ac6 | 2008-09-29 15:18:18 -0400 | [diff] [blame] | 3359 | * read an inode from the btree into the in-memory inode |
| 3360 | */ |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 3361 | static void btrfs_read_locked_inode(struct inode *inode) |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3362 | { |
| 3363 | struct btrfs_path *path; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 3364 | struct extent_buffer *leaf; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3365 | struct btrfs_inode_item *inode_item; |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 3366 | struct btrfs_timespec *tspec; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3367 | struct btrfs_root *root = BTRFS_I(inode)->root; |
| 3368 | struct btrfs_key location; |
Chris Mason | 46a53cc | 2009-04-27 11:47:50 -0400 | [diff] [blame] | 3369 | int maybe_acls; |
Josef Bacik | 618e21d | 2007-07-11 10:18:17 -0400 | [diff] [blame] | 3370 | u32 rdev; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3371 | int ret; |
Miao Xie | 2f7e33d | 2011-06-23 07:27:13 +0000 | [diff] [blame] | 3372 | bool filled = false; |
| 3373 | |
| 3374 | ret = btrfs_fill_inode(inode, &rdev); |
| 3375 | if (!ret) |
| 3376 | filled = true; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3377 | |
| 3378 | path = btrfs_alloc_path(); |
Mark Fasheh | 1748f84 | 2011-07-12 11:25:31 -0700 | [diff] [blame] | 3379 | if (!path) |
| 3380 | goto make_bad; |
| 3381 | |
Josef Bacik | d90c732 | 2011-05-17 09:50:54 -0400 | [diff] [blame] | 3382 | path->leave_spinning = 1; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3383 | memcpy(&location, &BTRFS_I(inode)->location, sizeof(location)); |
Chris Mason | dc17ff8 | 2008-01-08 15:46:30 -0500 | [diff] [blame] | 3384 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3385 | ret = btrfs_lookup_inode(NULL, root, path, &location, 0); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 3386 | if (ret) |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3387 | goto make_bad; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3388 | |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 3389 | leaf = path->nodes[0]; |
Miao Xie | 2f7e33d | 2011-06-23 07:27:13 +0000 | [diff] [blame] | 3390 | |
| 3391 | if (filled) |
| 3392 | goto cache_acl; |
| 3393 | |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 3394 | inode_item = btrfs_item_ptr(leaf, path->slots[0], |
| 3395 | struct btrfs_inode_item); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 3396 | inode->i_mode = btrfs_inode_mode(leaf, inode_item); |
Miklos Szeredi | bfe8684 | 2011-10-28 14:13:29 +0200 | [diff] [blame] | 3397 | set_nlink(inode, btrfs_inode_nlink(leaf, inode_item)); |
Eric W. Biederman | 2f2f43d | 2012-02-10 11:05:07 -0800 | [diff] [blame] | 3398 | i_uid_write(inode, btrfs_inode_uid(leaf, inode_item)); |
| 3399 | i_gid_write(inode, btrfs_inode_gid(leaf, inode_item)); |
Chris Mason | dbe674a | 2008-07-17 12:54:05 -0400 | [diff] [blame] | 3400 | btrfs_i_size_write(inode, btrfs_inode_size(leaf, inode_item)); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 3401 | |
| 3402 | tspec = btrfs_inode_atime(inode_item); |
| 3403 | inode->i_atime.tv_sec = btrfs_timespec_sec(leaf, tspec); |
| 3404 | inode->i_atime.tv_nsec = btrfs_timespec_nsec(leaf, tspec); |
| 3405 | |
| 3406 | tspec = btrfs_inode_mtime(inode_item); |
| 3407 | inode->i_mtime.tv_sec = btrfs_timespec_sec(leaf, tspec); |
| 3408 | inode->i_mtime.tv_nsec = btrfs_timespec_nsec(leaf, tspec); |
| 3409 | |
| 3410 | tspec = btrfs_inode_ctime(inode_item); |
| 3411 | inode->i_ctime.tv_sec = btrfs_timespec_sec(leaf, tspec); |
| 3412 | inode->i_ctime.tv_nsec = btrfs_timespec_nsec(leaf, tspec); |
| 3413 | |
Yan Zheng | a76a3cd | 2008-10-09 11:46:29 -0400 | [diff] [blame] | 3414 | inode_set_bytes(inode, btrfs_inode_nbytes(leaf, inode_item)); |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 3415 | BTRFS_I(inode)->generation = btrfs_inode_generation(leaf, inode_item); |
Josef Bacik | 5dc562c | 2012-08-17 13:14:17 -0400 | [diff] [blame] | 3416 | BTRFS_I(inode)->last_trans = btrfs_inode_transid(leaf, inode_item); |
| 3417 | |
| 3418 | /* |
| 3419 | * If we were modified in the current generation and evicted from memory |
| 3420 | * and then re-read we need to do a full sync since we don't have any |
| 3421 | * idea about which extents were modified before we were evicted from |
| 3422 | * cache. |
| 3423 | */ |
| 3424 | if (BTRFS_I(inode)->last_trans == root->fs_info->generation) |
| 3425 | set_bit(BTRFS_INODE_NEEDS_FULL_SYNC, |
| 3426 | &BTRFS_I(inode)->runtime_flags); |
| 3427 | |
Josef Bacik | 0c4d2d9 | 2012-04-05 15:03:02 -0400 | [diff] [blame] | 3428 | inode->i_version = btrfs_inode_sequence(leaf, inode_item); |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 3429 | inode->i_generation = BTRFS_I(inode)->generation; |
Josef Bacik | 618e21d | 2007-07-11 10:18:17 -0400 | [diff] [blame] | 3430 | inode->i_rdev = 0; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 3431 | rdev = btrfs_inode_rdev(leaf, inode_item); |
| 3432 | |
Josef Bacik | aec7477 | 2008-07-24 12:12:38 -0400 | [diff] [blame] | 3433 | BTRFS_I(inode)->index_cnt = (u64)-1; |
Yan Zheng | d2fb343 | 2008-12-11 16:30:39 -0500 | [diff] [blame] | 3434 | BTRFS_I(inode)->flags = btrfs_inode_flags(leaf, inode_item); |
Miao Xie | 2f7e33d | 2011-06-23 07:27:13 +0000 | [diff] [blame] | 3435 | cache_acl: |
Chris Mason | 46a53cc | 2009-04-27 11:47:50 -0400 | [diff] [blame] | 3436 | /* |
| 3437 | * try to precache a NULL acl entry for files that don't have |
| 3438 | * any xattrs or acls |
| 3439 | */ |
Li Zefan | 33345d01 | 2011-04-20 10:31:50 +0800 | [diff] [blame] | 3440 | maybe_acls = acls_after_inode_item(leaf, path->slots[0], |
| 3441 | btrfs_ino(inode)); |
Al Viro | 72c0490 | 2009-06-24 16:58:48 -0400 | [diff] [blame] | 3442 | if (!maybe_acls) |
| 3443 | cache_no_acl(inode); |
Chris Mason | 46a53cc | 2009-04-27 11:47:50 -0400 | [diff] [blame] | 3444 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3445 | btrfs_free_path(path); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3446 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3447 | switch (inode->i_mode & S_IFMT) { |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3448 | case S_IFREG: |
| 3449 | inode->i_mapping->a_ops = &btrfs_aops; |
Chris Mason | 0416008 | 2008-03-26 10:28:07 -0400 | [diff] [blame] | 3450 | inode->i_mapping->backing_dev_info = &root->fs_info->bdi; |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 3451 | BTRFS_I(inode)->io_tree.ops = &btrfs_extent_io_ops; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3452 | inode->i_fop = &btrfs_file_operations; |
| 3453 | inode->i_op = &btrfs_file_inode_operations; |
| 3454 | break; |
| 3455 | case S_IFDIR: |
| 3456 | inode->i_fop = &btrfs_dir_file_operations; |
| 3457 | if (root == root->fs_info->tree_root) |
| 3458 | inode->i_op = &btrfs_dir_ro_inode_operations; |
| 3459 | else |
| 3460 | inode->i_op = &btrfs_dir_inode_operations; |
| 3461 | break; |
| 3462 | case S_IFLNK: |
| 3463 | inode->i_op = &btrfs_symlink_inode_operations; |
| 3464 | inode->i_mapping->a_ops = &btrfs_symlink_aops; |
Chris Mason | 0416008 | 2008-03-26 10:28:07 -0400 | [diff] [blame] | 3465 | inode->i_mapping->backing_dev_info = &root->fs_info->bdi; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3466 | break; |
Josef Bacik | 618e21d | 2007-07-11 10:18:17 -0400 | [diff] [blame] | 3467 | default: |
Jim Owens | 0279b4c | 2009-02-04 09:29:13 -0500 | [diff] [blame] | 3468 | inode->i_op = &btrfs_special_inode_operations; |
Josef Bacik | 618e21d | 2007-07-11 10:18:17 -0400 | [diff] [blame] | 3469 | init_special_inode(inode, inode->i_mode, rdev); |
| 3470 | break; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3471 | } |
Christoph Hellwig | 6cbff00 | 2009-04-17 10:37:41 +0200 | [diff] [blame] | 3472 | |
| 3473 | btrfs_update_iflags(inode); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3474 | return; |
| 3475 | |
| 3476 | make_bad: |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3477 | btrfs_free_path(path); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3478 | make_bad_inode(inode); |
| 3479 | } |
| 3480 | |
Chris Mason | d352ac6 | 2008-09-29 15:18:18 -0400 | [diff] [blame] | 3481 | /* |
| 3482 | * given a leaf and an inode, copy the inode fields into the leaf |
| 3483 | */ |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 3484 | static void fill_inode_item(struct btrfs_trans_handle *trans, |
| 3485 | struct extent_buffer *leaf, |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 3486 | struct btrfs_inode_item *item, |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3487 | struct inode *inode) |
| 3488 | { |
Liu Bo | 51fab69 | 2012-12-27 09:01:21 +0000 | [diff] [blame] | 3489 | struct btrfs_map_token token; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 3490 | |
Liu Bo | 51fab69 | 2012-12-27 09:01:21 +0000 | [diff] [blame] | 3491 | btrfs_init_map_token(&token); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 3492 | |
Liu Bo | 51fab69 | 2012-12-27 09:01:21 +0000 | [diff] [blame] | 3493 | btrfs_set_token_inode_uid(leaf, item, i_uid_read(inode), &token); |
| 3494 | btrfs_set_token_inode_gid(leaf, item, i_gid_read(inode), &token); |
| 3495 | btrfs_set_token_inode_size(leaf, item, BTRFS_I(inode)->disk_i_size, |
| 3496 | &token); |
| 3497 | btrfs_set_token_inode_mode(leaf, item, inode->i_mode, &token); |
| 3498 | btrfs_set_token_inode_nlink(leaf, item, inode->i_nlink, &token); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 3499 | |
Liu Bo | 51fab69 | 2012-12-27 09:01:21 +0000 | [diff] [blame] | 3500 | btrfs_set_token_timespec_sec(leaf, btrfs_inode_atime(item), |
| 3501 | inode->i_atime.tv_sec, &token); |
| 3502 | btrfs_set_token_timespec_nsec(leaf, btrfs_inode_atime(item), |
| 3503 | inode->i_atime.tv_nsec, &token); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 3504 | |
Liu Bo | 51fab69 | 2012-12-27 09:01:21 +0000 | [diff] [blame] | 3505 | btrfs_set_token_timespec_sec(leaf, btrfs_inode_mtime(item), |
| 3506 | inode->i_mtime.tv_sec, &token); |
| 3507 | btrfs_set_token_timespec_nsec(leaf, btrfs_inode_mtime(item), |
| 3508 | inode->i_mtime.tv_nsec, &token); |
| 3509 | |
| 3510 | btrfs_set_token_timespec_sec(leaf, btrfs_inode_ctime(item), |
| 3511 | inode->i_ctime.tv_sec, &token); |
| 3512 | btrfs_set_token_timespec_nsec(leaf, btrfs_inode_ctime(item), |
| 3513 | inode->i_ctime.tv_nsec, &token); |
| 3514 | |
| 3515 | btrfs_set_token_inode_nbytes(leaf, item, inode_get_bytes(inode), |
| 3516 | &token); |
| 3517 | btrfs_set_token_inode_generation(leaf, item, BTRFS_I(inode)->generation, |
| 3518 | &token); |
| 3519 | btrfs_set_token_inode_sequence(leaf, item, inode->i_version, &token); |
| 3520 | btrfs_set_token_inode_transid(leaf, item, trans->transid, &token); |
| 3521 | btrfs_set_token_inode_rdev(leaf, item, inode->i_rdev, &token); |
| 3522 | btrfs_set_token_inode_flags(leaf, item, BTRFS_I(inode)->flags, &token); |
| 3523 | btrfs_set_token_inode_block_group(leaf, item, 0, &token); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3524 | } |
| 3525 | |
Chris Mason | d352ac6 | 2008-09-29 15:18:18 -0400 | [diff] [blame] | 3526 | /* |
| 3527 | * copy everything in the in-memory inode into the btree. |
| 3528 | */ |
Chris Mason | 2115133 | 2011-11-10 20:39:08 -0500 | [diff] [blame] | 3529 | static noinline int btrfs_update_inode_item(struct btrfs_trans_handle *trans, |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 3530 | struct btrfs_root *root, struct inode *inode) |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3531 | { |
| 3532 | struct btrfs_inode_item *inode_item; |
| 3533 | struct btrfs_path *path; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 3534 | struct extent_buffer *leaf; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3535 | int ret; |
| 3536 | |
| 3537 | path = btrfs_alloc_path(); |
Miao Xie | 16cdcec | 2011-04-22 18:12:22 +0800 | [diff] [blame] | 3538 | if (!path) |
| 3539 | return -ENOMEM; |
| 3540 | |
Chris Mason | b947343 | 2009-03-13 11:00:37 -0400 | [diff] [blame] | 3541 | path->leave_spinning = 1; |
Miao Xie | 16cdcec | 2011-04-22 18:12:22 +0800 | [diff] [blame] | 3542 | ret = btrfs_lookup_inode(trans, root, path, &BTRFS_I(inode)->location, |
| 3543 | 1); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3544 | if (ret) { |
| 3545 | if (ret > 0) |
| 3546 | ret = -ENOENT; |
| 3547 | goto failed; |
| 3548 | } |
| 3549 | |
Chris Mason | b4ce94d | 2009-02-04 09:25:08 -0500 | [diff] [blame] | 3550 | btrfs_unlock_up_safe(path, 1); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 3551 | leaf = path->nodes[0]; |
| 3552 | inode_item = btrfs_item_ptr(leaf, path->slots[0], |
Miao Xie | 16cdcec | 2011-04-22 18:12:22 +0800 | [diff] [blame] | 3553 | struct btrfs_inode_item); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3554 | |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 3555 | fill_inode_item(trans, leaf, inode_item, inode); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 3556 | btrfs_mark_buffer_dirty(leaf); |
Josef Bacik | 15ee9bc | 2007-08-10 16:22:09 -0400 | [diff] [blame] | 3557 | btrfs_set_inode_last_trans(trans, inode); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3558 | ret = 0; |
| 3559 | failed: |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3560 | btrfs_free_path(path); |
| 3561 | return ret; |
| 3562 | } |
| 3563 | |
Chris Mason | d352ac6 | 2008-09-29 15:18:18 -0400 | [diff] [blame] | 3564 | /* |
Chris Mason | 2115133 | 2011-11-10 20:39:08 -0500 | [diff] [blame] | 3565 | * copy everything in the in-memory inode into the btree. |
| 3566 | */ |
| 3567 | noinline int btrfs_update_inode(struct btrfs_trans_handle *trans, |
| 3568 | struct btrfs_root *root, struct inode *inode) |
| 3569 | { |
| 3570 | int ret; |
| 3571 | |
| 3572 | /* |
| 3573 | * If the inode is a free space inode, we can deadlock during commit |
| 3574 | * if we put it into the delayed code. |
| 3575 | * |
| 3576 | * The data relocation inode should also be directly updated |
| 3577 | * without delay |
| 3578 | */ |
Liu Bo | 83eea1f | 2012-07-10 05:28:39 -0600 | [diff] [blame] | 3579 | if (!btrfs_is_free_space_inode(inode) |
Chris Mason | 2115133 | 2011-11-10 20:39:08 -0500 | [diff] [blame] | 3580 | && root->root_key.objectid != BTRFS_DATA_RELOC_TREE_OBJECTID) { |
Alexander Block | 8ea05e3 | 2012-07-25 17:35:53 +0200 | [diff] [blame] | 3581 | btrfs_update_root_times(trans, root); |
| 3582 | |
Chris Mason | 2115133 | 2011-11-10 20:39:08 -0500 | [diff] [blame] | 3583 | ret = btrfs_delayed_update_inode(trans, root, inode); |
| 3584 | if (!ret) |
| 3585 | btrfs_set_inode_last_trans(trans, inode); |
| 3586 | return ret; |
| 3587 | } |
| 3588 | |
| 3589 | return btrfs_update_inode_item(trans, root, inode); |
| 3590 | } |
| 3591 | |
Josef Bacik | be6aef6 | 2012-10-22 15:43:12 -0400 | [diff] [blame] | 3592 | noinline int btrfs_update_inode_fallback(struct btrfs_trans_handle *trans, |
| 3593 | struct btrfs_root *root, |
| 3594 | struct inode *inode) |
Chris Mason | 2115133 | 2011-11-10 20:39:08 -0500 | [diff] [blame] | 3595 | { |
| 3596 | int ret; |
| 3597 | |
| 3598 | ret = btrfs_update_inode(trans, root, inode); |
| 3599 | if (ret == -ENOSPC) |
| 3600 | return btrfs_update_inode_item(trans, root, inode); |
| 3601 | return ret; |
| 3602 | } |
| 3603 | |
| 3604 | /* |
Chris Mason | d352ac6 | 2008-09-29 15:18:18 -0400 | [diff] [blame] | 3605 | * unlink helper that gets used here in inode.c and in the tree logging |
| 3606 | * recovery code. It remove a link in a directory with a given name, and |
| 3607 | * also drops the back refs in the inode to the directory |
| 3608 | */ |
Al Viro | 9298679 | 2011-03-04 17:14:37 +0000 | [diff] [blame] | 3609 | static int __btrfs_unlink_inode(struct btrfs_trans_handle *trans, |
| 3610 | struct btrfs_root *root, |
| 3611 | struct inode *dir, struct inode *inode, |
| 3612 | const char *name, int name_len) |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3613 | { |
| 3614 | struct btrfs_path *path; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3615 | int ret = 0; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 3616 | struct extent_buffer *leaf; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3617 | struct btrfs_dir_item *di; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 3618 | struct btrfs_key key; |
Josef Bacik | aec7477 | 2008-07-24 12:12:38 -0400 | [diff] [blame] | 3619 | u64 index; |
Li Zefan | 33345d01 | 2011-04-20 10:31:50 +0800 | [diff] [blame] | 3620 | u64 ino = btrfs_ino(inode); |
| 3621 | u64 dir_ino = btrfs_ino(dir); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3622 | |
| 3623 | path = btrfs_alloc_path(); |
Chris Mason | 54aa1f4 | 2007-06-22 14:16:25 -0400 | [diff] [blame] | 3624 | if (!path) { |
| 3625 | ret = -ENOMEM; |
Tsutomu Itoh | 554233a | 2011-02-03 03:16:25 +0000 | [diff] [blame] | 3626 | goto out; |
Chris Mason | 54aa1f4 | 2007-06-22 14:16:25 -0400 | [diff] [blame] | 3627 | } |
| 3628 | |
Chris Mason | b947343 | 2009-03-13 11:00:37 -0400 | [diff] [blame] | 3629 | path->leave_spinning = 1; |
Li Zefan | 33345d01 | 2011-04-20 10:31:50 +0800 | [diff] [blame] | 3630 | di = btrfs_lookup_dir_item(trans, root, path, dir_ino, |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3631 | name, name_len, -1); |
| 3632 | if (IS_ERR(di)) { |
| 3633 | ret = PTR_ERR(di); |
| 3634 | goto err; |
| 3635 | } |
| 3636 | if (!di) { |
| 3637 | ret = -ENOENT; |
| 3638 | goto err; |
| 3639 | } |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 3640 | leaf = path->nodes[0]; |
| 3641 | btrfs_dir_item_key_to_cpu(leaf, di, &key); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3642 | ret = btrfs_delete_one_dir_name(trans, root, path, di); |
Chris Mason | 54aa1f4 | 2007-06-22 14:16:25 -0400 | [diff] [blame] | 3643 | if (ret) |
| 3644 | goto err; |
David Sterba | b3b4aa7 | 2011-04-21 01:20:15 +0200 | [diff] [blame] | 3645 | btrfs_release_path(path); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3646 | |
Li Zefan | 33345d01 | 2011-04-20 10:31:50 +0800 | [diff] [blame] | 3647 | ret = btrfs_del_inode_ref(trans, root, name, name_len, ino, |
| 3648 | dir_ino, &index); |
Josef Bacik | aec7477 | 2008-07-24 12:12:38 -0400 | [diff] [blame] | 3649 | if (ret) { |
Simon Kirby | c2cf52e | 2013-03-19 22:41:23 +0000 | [diff] [blame] | 3650 | btrfs_info(root->fs_info, |
| 3651 | "failed to delete reference to %.*s, inode %llu parent %llu", |
| 3652 | name_len, name, |
| 3653 | (unsigned long long)ino, (unsigned long long)dir_ino); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 3654 | btrfs_abort_transaction(trans, root, ret); |
Josef Bacik | aec7477 | 2008-07-24 12:12:38 -0400 | [diff] [blame] | 3655 | goto err; |
| 3656 | } |
| 3657 | |
Miao Xie | 16cdcec | 2011-04-22 18:12:22 +0800 | [diff] [blame] | 3658 | ret = btrfs_delete_delayed_dir_index(trans, root, dir, index); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 3659 | if (ret) { |
| 3660 | btrfs_abort_transaction(trans, root, ret); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3661 | goto err; |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 3662 | } |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3663 | |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 3664 | ret = btrfs_del_inode_ref_in_log(trans, root, name, name_len, |
Li Zefan | 33345d01 | 2011-04-20 10:31:50 +0800 | [diff] [blame] | 3665 | inode, dir_ino); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 3666 | if (ret != 0 && ret != -ENOENT) { |
| 3667 | btrfs_abort_transaction(trans, root, ret); |
| 3668 | goto err; |
| 3669 | } |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 3670 | |
| 3671 | ret = btrfs_del_dir_entries_in_log(trans, root, name, name_len, |
| 3672 | dir, index); |
Chris Mason | 6418c96 | 2010-10-30 07:34:24 -0400 | [diff] [blame] | 3673 | if (ret == -ENOENT) |
| 3674 | ret = 0; |
Zach Brown | d4e3991 | 2013-04-02 21:02:16 +0000 | [diff] [blame] | 3675 | else if (ret) |
| 3676 | btrfs_abort_transaction(trans, root, ret); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3677 | err: |
| 3678 | btrfs_free_path(path); |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 3679 | if (ret) |
| 3680 | goto out; |
| 3681 | |
| 3682 | btrfs_i_size_write(dir, dir->i_size - name_len * 2); |
Josef Bacik | 0c4d2d9 | 2012-04-05 15:03:02 -0400 | [diff] [blame] | 3683 | inode_inc_iversion(inode); |
| 3684 | inode_inc_iversion(dir); |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 3685 | inode->i_ctime = dir->i_mtime = dir->i_ctime = CURRENT_TIME; |
Tsutomu Itoh | b995929 | 2012-06-25 21:25:22 -0600 | [diff] [blame] | 3686 | ret = btrfs_update_inode(trans, root, dir); |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 3687 | out: |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3688 | return ret; |
| 3689 | } |
| 3690 | |
Al Viro | 9298679 | 2011-03-04 17:14:37 +0000 | [diff] [blame] | 3691 | int btrfs_unlink_inode(struct btrfs_trans_handle *trans, |
| 3692 | struct btrfs_root *root, |
| 3693 | struct inode *dir, struct inode *inode, |
| 3694 | const char *name, int name_len) |
| 3695 | { |
| 3696 | int ret; |
| 3697 | ret = __btrfs_unlink_inode(trans, root, dir, inode, name, name_len); |
| 3698 | if (!ret) { |
| 3699 | btrfs_drop_nlink(inode); |
| 3700 | ret = btrfs_update_inode(trans, root, inode); |
| 3701 | } |
| 3702 | return ret; |
| 3703 | } |
Yan, Zheng | a22285a | 2010-05-16 10:48:46 -0400 | [diff] [blame] | 3704 | |
| 3705 | /* |
| 3706 | * helper to start transaction for unlink and rmdir. |
| 3707 | * |
Josef Bacik | d52be81 | 2013-05-29 14:54:47 -0400 | [diff] [blame] | 3708 | * unlink and rmdir are special in btrfs, they do not always free space, so |
| 3709 | * if we cannot make our reservations the normal way try and see if there is |
| 3710 | * plenty of slack room in the global reserve to migrate, otherwise we cannot |
| 3711 | * allow the unlink to occur. |
Yan, Zheng | a22285a | 2010-05-16 10:48:46 -0400 | [diff] [blame] | 3712 | */ |
Josef Bacik | d52be81 | 2013-05-29 14:54:47 -0400 | [diff] [blame] | 3713 | static struct btrfs_trans_handle *__unlink_start_trans(struct inode *dir) |
Yan, Zheng | a22285a | 2010-05-16 10:48:46 -0400 | [diff] [blame] | 3714 | { |
| 3715 | struct btrfs_trans_handle *trans; |
| 3716 | struct btrfs_root *root = BTRFS_I(dir)->root; |
Yan, Zheng | a22285a | 2010-05-16 10:48:46 -0400 | [diff] [blame] | 3717 | int ret; |
| 3718 | |
Josef Bacik | e70bea5 | 2011-10-11 14:18:24 -0400 | [diff] [blame] | 3719 | /* |
| 3720 | * 1 for the possible orphan item |
| 3721 | * 1 for the dir item |
| 3722 | * 1 for the dir index |
| 3723 | * 1 for the inode ref |
Josef Bacik | e70bea5 | 2011-10-11 14:18:24 -0400 | [diff] [blame] | 3724 | * 1 for the inode |
| 3725 | */ |
Josef Bacik | 6e137ed | 2013-03-26 15:26:55 -0400 | [diff] [blame] | 3726 | trans = btrfs_start_transaction(root, 5); |
Yan, Zheng | a22285a | 2010-05-16 10:48:46 -0400 | [diff] [blame] | 3727 | if (!IS_ERR(trans) || PTR_ERR(trans) != -ENOSPC) |
| 3728 | return trans; |
| 3729 | |
Josef Bacik | d52be81 | 2013-05-29 14:54:47 -0400 | [diff] [blame] | 3730 | if (PTR_ERR(trans) == -ENOSPC) { |
| 3731 | u64 num_bytes = btrfs_calc_trans_metadata_size(root, 5); |
Yan, Zheng | a22285a | 2010-05-16 10:48:46 -0400 | [diff] [blame] | 3732 | |
Josef Bacik | d52be81 | 2013-05-29 14:54:47 -0400 | [diff] [blame] | 3733 | trans = btrfs_start_transaction(root, 0); |
| 3734 | if (IS_ERR(trans)) |
| 3735 | return trans; |
| 3736 | ret = btrfs_cond_migrate_bytes(root->fs_info, |
| 3737 | &root->fs_info->trans_block_rsv, |
| 3738 | num_bytes, 5); |
| 3739 | if (ret) { |
| 3740 | btrfs_end_transaction(trans, root); |
| 3741 | return ERR_PTR(ret); |
Yan, Zheng | a22285a | 2010-05-16 10:48:46 -0400 | [diff] [blame] | 3742 | } |
Josef Bacik | 5a77d76 | 2011-11-01 14:32:23 -0400 | [diff] [blame] | 3743 | trans->block_rsv = &root->fs_info->trans_block_rsv; |
Josef Bacik | d52be81 | 2013-05-29 14:54:47 -0400 | [diff] [blame] | 3744 | trans->bytes_reserved = num_bytes; |
Yan, Zheng | a22285a | 2010-05-16 10:48:46 -0400 | [diff] [blame] | 3745 | } |
Josef Bacik | d52be81 | 2013-05-29 14:54:47 -0400 | [diff] [blame] | 3746 | return trans; |
Yan, Zheng | a22285a | 2010-05-16 10:48:46 -0400 | [diff] [blame] | 3747 | } |
| 3748 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3749 | static int btrfs_unlink(struct inode *dir, struct dentry *dentry) |
| 3750 | { |
Yan, Zheng | a22285a | 2010-05-16 10:48:46 -0400 | [diff] [blame] | 3751 | struct btrfs_root *root = BTRFS_I(dir)->root; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3752 | struct btrfs_trans_handle *trans; |
Josef Bacik | 7b12876 | 2008-07-24 12:17:14 -0400 | [diff] [blame] | 3753 | struct inode *inode = dentry->d_inode; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3754 | int ret; |
| 3755 | |
Josef Bacik | d52be81 | 2013-05-29 14:54:47 -0400 | [diff] [blame] | 3756 | trans = __unlink_start_trans(dir); |
Yan, Zheng | a22285a | 2010-05-16 10:48:46 -0400 | [diff] [blame] | 3757 | if (IS_ERR(trans)) |
Josef Bacik | 5df6a9f | 2009-11-10 21:23:48 -0500 | [diff] [blame] | 3758 | return PTR_ERR(trans); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 3759 | |
Chris Mason | 12fcfd2 | 2009-03-24 10:24:20 -0400 | [diff] [blame] | 3760 | btrfs_record_unlink_dir(trans, dir, dentry->d_inode, 0); |
| 3761 | |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 3762 | ret = btrfs_unlink_inode(trans, root, dir, dentry->d_inode, |
| 3763 | dentry->d_name.name, dentry->d_name.len); |
Tsutomu Itoh | b532402 | 2011-07-19 07:27:20 +0000 | [diff] [blame] | 3764 | if (ret) |
| 3765 | goto out; |
Josef Bacik | 7b12876 | 2008-07-24 12:17:14 -0400 | [diff] [blame] | 3766 | |
Yan, Zheng | a22285a | 2010-05-16 10:48:46 -0400 | [diff] [blame] | 3767 | if (inode->i_nlink == 0) { |
Josef Bacik | 7b12876 | 2008-07-24 12:17:14 -0400 | [diff] [blame] | 3768 | ret = btrfs_orphan_add(trans, inode); |
Tsutomu Itoh | b532402 | 2011-07-19 07:27:20 +0000 | [diff] [blame] | 3769 | if (ret) |
| 3770 | goto out; |
Yan, Zheng | a22285a | 2010-05-16 10:48:46 -0400 | [diff] [blame] | 3771 | } |
Josef Bacik | 7b12876 | 2008-07-24 12:17:14 -0400 | [diff] [blame] | 3772 | |
Tsutomu Itoh | b532402 | 2011-07-19 07:27:20 +0000 | [diff] [blame] | 3773 | out: |
Josef Bacik | d52be81 | 2013-05-29 14:54:47 -0400 | [diff] [blame] | 3774 | btrfs_end_transaction(trans, root); |
Liu Bo | b53d3f5 | 2012-11-14 14:34:34 +0000 | [diff] [blame] | 3775 | btrfs_btree_balance_dirty(root); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3776 | return ret; |
| 3777 | } |
| 3778 | |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 3779 | int btrfs_unlink_subvol(struct btrfs_trans_handle *trans, |
| 3780 | struct btrfs_root *root, |
| 3781 | struct inode *dir, u64 objectid, |
| 3782 | const char *name, int name_len) |
| 3783 | { |
| 3784 | struct btrfs_path *path; |
| 3785 | struct extent_buffer *leaf; |
| 3786 | struct btrfs_dir_item *di; |
| 3787 | struct btrfs_key key; |
| 3788 | u64 index; |
| 3789 | int ret; |
Li Zefan | 33345d01 | 2011-04-20 10:31:50 +0800 | [diff] [blame] | 3790 | u64 dir_ino = btrfs_ino(dir); |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 3791 | |
| 3792 | path = btrfs_alloc_path(); |
| 3793 | if (!path) |
| 3794 | return -ENOMEM; |
| 3795 | |
Li Zefan | 33345d01 | 2011-04-20 10:31:50 +0800 | [diff] [blame] | 3796 | di = btrfs_lookup_dir_item(trans, root, path, dir_ino, |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 3797 | name, name_len, -1); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 3798 | if (IS_ERR_OR_NULL(di)) { |
| 3799 | if (!di) |
| 3800 | ret = -ENOENT; |
| 3801 | else |
| 3802 | ret = PTR_ERR(di); |
| 3803 | goto out; |
| 3804 | } |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 3805 | |
| 3806 | leaf = path->nodes[0]; |
| 3807 | btrfs_dir_item_key_to_cpu(leaf, di, &key); |
| 3808 | WARN_ON(key.type != BTRFS_ROOT_ITEM_KEY || key.objectid != objectid); |
| 3809 | ret = btrfs_delete_one_dir_name(trans, root, path, di); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 3810 | if (ret) { |
| 3811 | btrfs_abort_transaction(trans, root, ret); |
| 3812 | goto out; |
| 3813 | } |
David Sterba | b3b4aa7 | 2011-04-21 01:20:15 +0200 | [diff] [blame] | 3814 | btrfs_release_path(path); |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 3815 | |
| 3816 | ret = btrfs_del_root_ref(trans, root->fs_info->tree_root, |
| 3817 | objectid, root->root_key.objectid, |
Li Zefan | 33345d01 | 2011-04-20 10:31:50 +0800 | [diff] [blame] | 3818 | dir_ino, &index, name, name_len); |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 3819 | if (ret < 0) { |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 3820 | if (ret != -ENOENT) { |
| 3821 | btrfs_abort_transaction(trans, root, ret); |
| 3822 | goto out; |
| 3823 | } |
Li Zefan | 33345d01 | 2011-04-20 10:31:50 +0800 | [diff] [blame] | 3824 | di = btrfs_search_dir_index_item(root, path, dir_ino, |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 3825 | name, name_len); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 3826 | if (IS_ERR_OR_NULL(di)) { |
| 3827 | if (!di) |
| 3828 | ret = -ENOENT; |
| 3829 | else |
| 3830 | ret = PTR_ERR(di); |
| 3831 | btrfs_abort_transaction(trans, root, ret); |
| 3832 | goto out; |
| 3833 | } |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 3834 | |
| 3835 | leaf = path->nodes[0]; |
| 3836 | btrfs_item_key_to_cpu(leaf, &key, path->slots[0]); |
David Sterba | b3b4aa7 | 2011-04-21 01:20:15 +0200 | [diff] [blame] | 3837 | btrfs_release_path(path); |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 3838 | index = key.offset; |
| 3839 | } |
David Sterba | b3b4aa7 | 2011-04-21 01:20:15 +0200 | [diff] [blame] | 3840 | btrfs_release_path(path); |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 3841 | |
Miao Xie | 16cdcec | 2011-04-22 18:12:22 +0800 | [diff] [blame] | 3842 | ret = btrfs_delete_delayed_dir_index(trans, root, dir, index); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 3843 | if (ret) { |
| 3844 | btrfs_abort_transaction(trans, root, ret); |
| 3845 | goto out; |
| 3846 | } |
Miao Xie | 16cdcec | 2011-04-22 18:12:22 +0800 | [diff] [blame] | 3847 | |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 3848 | btrfs_i_size_write(dir, dir->i_size - name_len * 2); |
Josef Bacik | 0c4d2d9 | 2012-04-05 15:03:02 -0400 | [diff] [blame] | 3849 | inode_inc_iversion(dir); |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 3850 | dir->i_mtime = dir->i_ctime = CURRENT_TIME; |
Josef Bacik | 5a24e84 | 2012-08-08 10:12:59 -0600 | [diff] [blame] | 3851 | ret = btrfs_update_inode_fallback(trans, root, dir); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 3852 | if (ret) |
| 3853 | btrfs_abort_transaction(trans, root, ret); |
| 3854 | out: |
Josef Bacik | 71d7aed | 2011-06-14 14:24:32 -0400 | [diff] [blame] | 3855 | btrfs_free_path(path); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 3856 | return ret; |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 3857 | } |
| 3858 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3859 | static int btrfs_rmdir(struct inode *dir, struct dentry *dentry) |
| 3860 | { |
| 3861 | struct inode *inode = dentry->d_inode; |
Chris Mason | 1832a6d | 2007-12-21 16:27:21 -0500 | [diff] [blame] | 3862 | int err = 0; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3863 | struct btrfs_root *root = BTRFS_I(dir)->root; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3864 | struct btrfs_trans_handle *trans; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3865 | |
David Sterba | b3ae244 | 2012-09-13 16:04:34 -0600 | [diff] [blame] | 3866 | if (inode->i_size > BTRFS_EMPTY_DIR_SIZE) |
Yan | 134d451 | 2007-10-25 15:49:25 -0400 | [diff] [blame] | 3867 | return -ENOTEMPTY; |
David Sterba | b3ae244 | 2012-09-13 16:04:34 -0600 | [diff] [blame] | 3868 | if (btrfs_ino(inode) == BTRFS_FIRST_FREE_OBJECTID) |
| 3869 | return -EPERM; |
Yan | 134d451 | 2007-10-25 15:49:25 -0400 | [diff] [blame] | 3870 | |
Josef Bacik | d52be81 | 2013-05-29 14:54:47 -0400 | [diff] [blame] | 3871 | trans = __unlink_start_trans(dir); |
Yan, Zheng | a22285a | 2010-05-16 10:48:46 -0400 | [diff] [blame] | 3872 | if (IS_ERR(trans)) |
Josef Bacik | 5df6a9f | 2009-11-10 21:23:48 -0500 | [diff] [blame] | 3873 | return PTR_ERR(trans); |
Josef Bacik | 5df6a9f | 2009-11-10 21:23:48 -0500 | [diff] [blame] | 3874 | |
Li Zefan | 33345d01 | 2011-04-20 10:31:50 +0800 | [diff] [blame] | 3875 | if (unlikely(btrfs_ino(inode) == BTRFS_EMPTY_SUBVOL_DIR_OBJECTID)) { |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 3876 | err = btrfs_unlink_subvol(trans, root, dir, |
| 3877 | BTRFS_I(inode)->location.objectid, |
| 3878 | dentry->d_name.name, |
| 3879 | dentry->d_name.len); |
| 3880 | goto out; |
| 3881 | } |
| 3882 | |
Josef Bacik | 7b12876 | 2008-07-24 12:17:14 -0400 | [diff] [blame] | 3883 | err = btrfs_orphan_add(trans, inode); |
| 3884 | if (err) |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 3885 | goto out; |
Josef Bacik | 7b12876 | 2008-07-24 12:17:14 -0400 | [diff] [blame] | 3886 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3887 | /* now the directory is empty */ |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 3888 | err = btrfs_unlink_inode(trans, root, dir, dentry->d_inode, |
| 3889 | dentry->d_name.name, dentry->d_name.len); |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 3890 | if (!err) |
Chris Mason | dbe674a | 2008-07-17 12:54:05 -0400 | [diff] [blame] | 3891 | btrfs_i_size_write(inode, 0); |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 3892 | out: |
Josef Bacik | d52be81 | 2013-05-29 14:54:47 -0400 | [diff] [blame] | 3893 | btrfs_end_transaction(trans, root); |
Liu Bo | b53d3f5 | 2012-11-14 14:34:34 +0000 | [diff] [blame] | 3894 | btrfs_btree_balance_dirty(root); |
Chris Mason | 3954401 | 2007-12-12 14:38:19 -0500 | [diff] [blame] | 3895 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3896 | return err; |
| 3897 | } |
| 3898 | |
Chris Mason | 323ac95 | 2008-10-01 19:05:46 -0400 | [diff] [blame] | 3899 | /* |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3900 | * this can truncate away extent items, csum items and directory items. |
| 3901 | * 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] | 3902 | * any higher than new_size |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3903 | * |
| 3904 | * csum items that cross the new i_size are truncated to the new size |
| 3905 | * as well. |
Josef Bacik | 7b12876 | 2008-07-24 12:17:14 -0400 | [diff] [blame] | 3906 | * |
| 3907 | * min_type is the minimum key type to truncate down to. If set to 0, this |
| 3908 | * 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] | 3909 | */ |
Yan, Zheng | 8082510 | 2009-11-12 09:35:36 +0000 | [diff] [blame] | 3910 | int btrfs_truncate_inode_items(struct btrfs_trans_handle *trans, |
| 3911 | struct btrfs_root *root, |
| 3912 | struct inode *inode, |
| 3913 | u64 new_size, u32 min_type) |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3914 | { |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3915 | struct btrfs_path *path; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 3916 | struct extent_buffer *leaf; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3917 | struct btrfs_file_extent_item *fi; |
Yan, Zheng | 8082510 | 2009-11-12 09:35:36 +0000 | [diff] [blame] | 3918 | struct btrfs_key key; |
| 3919 | struct btrfs_key found_key; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3920 | u64 extent_start = 0; |
Chris Mason | db94535 | 2007-10-15 16:15:53 -0400 | [diff] [blame] | 3921 | u64 extent_num_bytes = 0; |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 3922 | u64 extent_offset = 0; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3923 | u64 item_end = 0; |
Yan, Zheng | 8082510 | 2009-11-12 09:35:36 +0000 | [diff] [blame] | 3924 | u32 found_type = (u8)-1; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3925 | int found_extent; |
| 3926 | int del_item; |
Chris Mason | 85e21ba | 2008-01-29 15:11:36 -0500 | [diff] [blame] | 3927 | int pending_del_nr = 0; |
| 3928 | int pending_del_slot = 0; |
Chris Mason | 179e29e | 2007-11-01 11:28:41 -0400 | [diff] [blame] | 3929 | int extent_type = -1; |
Yan, Zheng | 8082510 | 2009-11-12 09:35:36 +0000 | [diff] [blame] | 3930 | int ret; |
| 3931 | int err = 0; |
Li Zefan | 33345d01 | 2011-04-20 10:31:50 +0800 | [diff] [blame] | 3932 | u64 ino = btrfs_ino(inode); |
Yan, Zheng | 8082510 | 2009-11-12 09:35:36 +0000 | [diff] [blame] | 3933 | |
| 3934 | BUG_ON(new_size > 0 && min_type != BTRFS_EXTENT_DATA_KEY); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3935 | |
Mark Fasheh | 0eb0e19 | 2011-07-12 16:44:10 -0700 | [diff] [blame] | 3936 | path = btrfs_alloc_path(); |
| 3937 | if (!path) |
| 3938 | return -ENOMEM; |
| 3939 | path->reada = -1; |
| 3940 | |
Josef Bacik | 5dc562c | 2012-08-17 13:14:17 -0400 | [diff] [blame] | 3941 | /* |
| 3942 | * We want to drop from the next block forward in case this new size is |
| 3943 | * not block aligned since we will be keeping the last block of the |
| 3944 | * extent just the way it is. |
| 3945 | */ |
Josef Bacik | 0af3d00 | 2010-06-21 14:48:16 -0400 | [diff] [blame] | 3946 | if (root->ref_cows || root == root->fs_info->tree_root) |
Qu Wenruo | fda2832 | 2013-02-26 08:10:22 +0000 | [diff] [blame] | 3947 | btrfs_drop_extent_cache(inode, ALIGN(new_size, |
| 3948 | root->sectorsize), (u64)-1, 0); |
Yan, Zheng | 8082510 | 2009-11-12 09:35:36 +0000 | [diff] [blame] | 3949 | |
Miao Xie | 16cdcec | 2011-04-22 18:12:22 +0800 | [diff] [blame] | 3950 | /* |
| 3951 | * This function is also used to drop the items in the log tree before |
| 3952 | * we relog the inode, so if root != BTRFS_I(inode)->root, it means |
| 3953 | * it is used to drop the loged items. So we shouldn't kill the delayed |
| 3954 | * items. |
| 3955 | */ |
| 3956 | if (min_type == 0 && root == BTRFS_I(inode)->root) |
| 3957 | btrfs_kill_delayed_inode_items(inode); |
| 3958 | |
Li Zefan | 33345d01 | 2011-04-20 10:31:50 +0800 | [diff] [blame] | 3959 | key.objectid = ino; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3960 | key.offset = (u64)-1; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 3961 | key.type = (u8)-1; |
| 3962 | |
Chris Mason | 85e21ba | 2008-01-29 15:11:36 -0500 | [diff] [blame] | 3963 | search_again: |
Chris Mason | b947343 | 2009-03-13 11:00:37 -0400 | [diff] [blame] | 3964 | path->leave_spinning = 1; |
Chris Mason | 85e21ba | 2008-01-29 15:11:36 -0500 | [diff] [blame] | 3965 | ret = btrfs_search_slot(trans, root, &key, path, -1, 1); |
Yan, Zheng | 8082510 | 2009-11-12 09:35:36 +0000 | [diff] [blame] | 3966 | if (ret < 0) { |
| 3967 | err = ret; |
| 3968 | goto out; |
| 3969 | } |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 3970 | |
Chris Mason | 85e21ba | 2008-01-29 15:11:36 -0500 | [diff] [blame] | 3971 | if (ret > 0) { |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 3972 | /* there are no items in the tree for us to truncate, we're |
| 3973 | * done |
| 3974 | */ |
Yan, Zheng | 8082510 | 2009-11-12 09:35:36 +0000 | [diff] [blame] | 3975 | if (path->slots[0] == 0) |
| 3976 | goto out; |
Chris Mason | 85e21ba | 2008-01-29 15:11:36 -0500 | [diff] [blame] | 3977 | path->slots[0]--; |
| 3978 | } |
| 3979 | |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 3980 | while (1) { |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3981 | fi = NULL; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 3982 | leaf = path->nodes[0]; |
| 3983 | btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]); |
| 3984 | found_type = btrfs_key_type(&found_key); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3985 | |
Li Zefan | 33345d01 | 2011-04-20 10:31:50 +0800 | [diff] [blame] | 3986 | if (found_key.objectid != ino) |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3987 | break; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 3988 | |
Chris Mason | 85e21ba | 2008-01-29 15:11:36 -0500 | [diff] [blame] | 3989 | if (found_type < min_type) |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3990 | break; |
| 3991 | |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 3992 | item_end = found_key.offset; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3993 | if (found_type == BTRFS_EXTENT_DATA_KEY) { |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 3994 | fi = btrfs_item_ptr(leaf, path->slots[0], |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3995 | struct btrfs_file_extent_item); |
Chris Mason | 179e29e | 2007-11-01 11:28:41 -0400 | [diff] [blame] | 3996 | extent_type = btrfs_file_extent_type(leaf, fi); |
| 3997 | if (extent_type != BTRFS_FILE_EXTENT_INLINE) { |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 3998 | item_end += |
Chris Mason | db94535 | 2007-10-15 16:15:53 -0400 | [diff] [blame] | 3999 | btrfs_file_extent_num_bytes(leaf, fi); |
Chris Mason | 179e29e | 2007-11-01 11:28:41 -0400 | [diff] [blame] | 4000 | } else if (extent_type == BTRFS_FILE_EXTENT_INLINE) { |
Chris Mason | 179e29e | 2007-11-01 11:28:41 -0400 | [diff] [blame] | 4001 | item_end += btrfs_file_extent_inline_len(leaf, |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 4002 | fi); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4003 | } |
Yan | 008630c | 2007-11-07 13:31:09 -0500 | [diff] [blame] | 4004 | item_end--; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4005 | } |
Yan, Zheng | 8082510 | 2009-11-12 09:35:36 +0000 | [diff] [blame] | 4006 | if (found_type > min_type) { |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4007 | del_item = 1; |
Yan, Zheng | 8082510 | 2009-11-12 09:35:36 +0000 | [diff] [blame] | 4008 | } else { |
| 4009 | if (item_end < new_size) |
| 4010 | break; |
| 4011 | if (found_key.offset >= new_size) |
| 4012 | del_item = 1; |
| 4013 | else |
| 4014 | del_item = 0; |
| 4015 | } |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4016 | found_extent = 0; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4017 | /* FIXME, shrink the extent if the ref count is only 1 */ |
Chris Mason | 179e29e | 2007-11-01 11:28:41 -0400 | [diff] [blame] | 4018 | if (found_type != BTRFS_EXTENT_DATA_KEY) |
| 4019 | goto delete; |
| 4020 | |
| 4021 | if (extent_type != BTRFS_FILE_EXTENT_INLINE) { |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4022 | u64 num_dec; |
Chris Mason | db94535 | 2007-10-15 16:15:53 -0400 | [diff] [blame] | 4023 | extent_start = btrfs_file_extent_disk_bytenr(leaf, fi); |
Miao Xie | f70a9a6b | 2012-01-12 19:10:12 -0500 | [diff] [blame] | 4024 | if (!del_item) { |
Chris Mason | db94535 | 2007-10-15 16:15:53 -0400 | [diff] [blame] | 4025 | u64 orig_num_bytes = |
| 4026 | btrfs_file_extent_num_bytes(leaf, fi); |
Qu Wenruo | fda2832 | 2013-02-26 08:10:22 +0000 | [diff] [blame] | 4027 | extent_num_bytes = ALIGN(new_size - |
| 4028 | found_key.offset, |
| 4029 | root->sectorsize); |
Chris Mason | db94535 | 2007-10-15 16:15:53 -0400 | [diff] [blame] | 4030 | btrfs_set_file_extent_num_bytes(leaf, fi, |
| 4031 | extent_num_bytes); |
| 4032 | num_dec = (orig_num_bytes - |
Chris Mason | 9069218 | 2008-02-08 13:49:28 -0500 | [diff] [blame] | 4033 | extent_num_bytes); |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 4034 | if (root->ref_cows && extent_start != 0) |
Yan Zheng | a76a3cd | 2008-10-09 11:46:29 -0400 | [diff] [blame] | 4035 | inode_sub_bytes(inode, num_dec); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 4036 | btrfs_mark_buffer_dirty(leaf); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4037 | } else { |
Chris Mason | db94535 | 2007-10-15 16:15:53 -0400 | [diff] [blame] | 4038 | extent_num_bytes = |
| 4039 | btrfs_file_extent_disk_num_bytes(leaf, |
| 4040 | fi); |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 4041 | extent_offset = found_key.offset - |
| 4042 | btrfs_file_extent_offset(leaf, fi); |
| 4043 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4044 | /* FIXME blocksize != 4096 */ |
Chris Mason | 9069218 | 2008-02-08 13:49:28 -0500 | [diff] [blame] | 4045 | num_dec = btrfs_file_extent_num_bytes(leaf, fi); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4046 | if (extent_start != 0) { |
| 4047 | found_extent = 1; |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 4048 | if (root->ref_cows) |
Yan Zheng | a76a3cd | 2008-10-09 11:46:29 -0400 | [diff] [blame] | 4049 | inode_sub_bytes(inode, num_dec); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4050 | } |
| 4051 | } |
Chris Mason | 9069218 | 2008-02-08 13:49:28 -0500 | [diff] [blame] | 4052 | } else if (extent_type == BTRFS_FILE_EXTENT_INLINE) { |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 4053 | /* |
| 4054 | * we can't truncate inline items that have had |
| 4055 | * special encodings |
| 4056 | */ |
| 4057 | if (!del_item && |
| 4058 | btrfs_file_extent_compression(leaf, fi) == 0 && |
| 4059 | btrfs_file_extent_encryption(leaf, fi) == 0 && |
| 4060 | btrfs_file_extent_other_encoding(leaf, fi) == 0) { |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 4061 | u32 size = new_size - found_key.offset; |
| 4062 | |
| 4063 | if (root->ref_cows) { |
Yan Zheng | a76a3cd | 2008-10-09 11:46:29 -0400 | [diff] [blame] | 4064 | inode_sub_bytes(inode, item_end + 1 - |
| 4065 | new_size); |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 4066 | } |
| 4067 | size = |
| 4068 | btrfs_file_extent_calc_inline_size(size); |
Tsutomu Itoh | afe5fea | 2013-04-16 05:18:22 +0000 | [diff] [blame] | 4069 | btrfs_truncate_item(root, path, size, 1); |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 4070 | } else if (root->ref_cows) { |
Yan Zheng | a76a3cd | 2008-10-09 11:46:29 -0400 | [diff] [blame] | 4071 | inode_sub_bytes(inode, item_end + 1 - |
| 4072 | found_key.offset); |
Chris Mason | 9069218 | 2008-02-08 13:49:28 -0500 | [diff] [blame] | 4073 | } |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4074 | } |
Chris Mason | 179e29e | 2007-11-01 11:28:41 -0400 | [diff] [blame] | 4075 | delete: |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4076 | if (del_item) { |
Chris Mason | 85e21ba | 2008-01-29 15:11:36 -0500 | [diff] [blame] | 4077 | if (!pending_del_nr) { |
| 4078 | /* no pending yet, add ourselves */ |
| 4079 | pending_del_slot = path->slots[0]; |
| 4080 | pending_del_nr = 1; |
| 4081 | } else if (pending_del_nr && |
| 4082 | path->slots[0] + 1 == pending_del_slot) { |
| 4083 | /* hop on the pending chunk */ |
| 4084 | pending_del_nr++; |
| 4085 | pending_del_slot = path->slots[0]; |
| 4086 | } else { |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 4087 | BUG(); |
Chris Mason | 85e21ba | 2008-01-29 15:11:36 -0500 | [diff] [blame] | 4088 | } |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4089 | } else { |
| 4090 | break; |
| 4091 | } |
Josef Bacik | 0af3d00 | 2010-06-21 14:48:16 -0400 | [diff] [blame] | 4092 | if (found_extent && (root->ref_cows || |
| 4093 | root == root->fs_info->tree_root)) { |
Chris Mason | b947343 | 2009-03-13 11:00:37 -0400 | [diff] [blame] | 4094 | btrfs_set_path_blocking(path); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4095 | ret = btrfs_free_extent(trans, root, extent_start, |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 4096 | extent_num_bytes, 0, |
| 4097 | btrfs_header_owner(leaf), |
Arne Jansen | 66d7e7f | 2011-09-12 15:26:38 +0200 | [diff] [blame] | 4098 | ino, extent_offset, 0); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4099 | BUG_ON(ret); |
| 4100 | } |
Chris Mason | 85e21ba | 2008-01-29 15:11:36 -0500 | [diff] [blame] | 4101 | |
Yan, Zheng | 8082510 | 2009-11-12 09:35:36 +0000 | [diff] [blame] | 4102 | if (found_type == BTRFS_INODE_ITEM_KEY) |
| 4103 | break; |
| 4104 | |
| 4105 | if (path->slots[0] == 0 || |
| 4106 | path->slots[0] != pending_del_slot) { |
Yan, Zheng | 8082510 | 2009-11-12 09:35:36 +0000 | [diff] [blame] | 4107 | if (pending_del_nr) { |
| 4108 | ret = btrfs_del_items(trans, root, path, |
| 4109 | pending_del_slot, |
| 4110 | pending_del_nr); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 4111 | if (ret) { |
| 4112 | btrfs_abort_transaction(trans, |
| 4113 | root, ret); |
| 4114 | goto error; |
| 4115 | } |
Yan, Zheng | 8082510 | 2009-11-12 09:35:36 +0000 | [diff] [blame] | 4116 | pending_del_nr = 0; |
| 4117 | } |
David Sterba | b3b4aa7 | 2011-04-21 01:20:15 +0200 | [diff] [blame] | 4118 | btrfs_release_path(path); |
Chris Mason | 85e21ba | 2008-01-29 15:11:36 -0500 | [diff] [blame] | 4119 | goto search_again; |
Yan, Zheng | 8082510 | 2009-11-12 09:35:36 +0000 | [diff] [blame] | 4120 | } else { |
| 4121 | path->slots[0]--; |
Chris Mason | 85e21ba | 2008-01-29 15:11:36 -0500 | [diff] [blame] | 4122 | } |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4123 | } |
Yan, Zheng | 8082510 | 2009-11-12 09:35:36 +0000 | [diff] [blame] | 4124 | out: |
Chris Mason | 85e21ba | 2008-01-29 15:11:36 -0500 | [diff] [blame] | 4125 | if (pending_del_nr) { |
| 4126 | ret = btrfs_del_items(trans, root, path, pending_del_slot, |
| 4127 | pending_del_nr); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 4128 | if (ret) |
| 4129 | btrfs_abort_transaction(trans, root, ret); |
Chris Mason | 85e21ba | 2008-01-29 15:11:36 -0500 | [diff] [blame] | 4130 | } |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 4131 | error: |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4132 | btrfs_free_path(path); |
Yan, Zheng | 8082510 | 2009-11-12 09:35:36 +0000 | [diff] [blame] | 4133 | return err; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4134 | } |
| 4135 | |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 4136 | /* |
Josef Bacik | 2aaa665 | 2012-08-29 14:27:18 -0400 | [diff] [blame] | 4137 | * btrfs_truncate_page - read, zero a chunk and write a page |
| 4138 | * @inode - inode that we're zeroing |
| 4139 | * @from - the offset to start zeroing |
| 4140 | * @len - the length to zero, 0 to zero the entire range respective to the |
| 4141 | * offset |
| 4142 | * @front - zero up to the offset instead of from the offset on |
| 4143 | * |
| 4144 | * This will find the page for the "from" offset and cow the page and zero the |
| 4145 | * 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] | 4146 | */ |
Josef Bacik | 2aaa665 | 2012-08-29 14:27:18 -0400 | [diff] [blame] | 4147 | int btrfs_truncate_page(struct inode *inode, loff_t from, loff_t len, |
| 4148 | int front) |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 4149 | { |
Josef Bacik | 2aaa665 | 2012-08-29 14:27:18 -0400 | [diff] [blame] | 4150 | struct address_space *mapping = inode->i_mapping; |
Chris Mason | db94535 | 2007-10-15 16:15:53 -0400 | [diff] [blame] | 4151 | struct btrfs_root *root = BTRFS_I(inode)->root; |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 4152 | struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree; |
| 4153 | struct btrfs_ordered_extent *ordered; |
Josef Bacik | 2ac55d4 | 2010-02-03 19:33:23 +0000 | [diff] [blame] | 4154 | struct extent_state *cached_state = NULL; |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 4155 | char *kaddr; |
Chris Mason | db94535 | 2007-10-15 16:15:53 -0400 | [diff] [blame] | 4156 | u32 blocksize = root->sectorsize; |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 4157 | pgoff_t index = from >> PAGE_CACHE_SHIFT; |
| 4158 | unsigned offset = from & (PAGE_CACHE_SIZE-1); |
| 4159 | struct page *page; |
Josef Bacik | 3b16a4e | 2011-09-21 15:05:58 -0400 | [diff] [blame] | 4160 | gfp_t mask = btrfs_alloc_write_mask(mapping); |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 4161 | int ret = 0; |
| 4162 | u64 page_start; |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 4163 | u64 page_end; |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 4164 | |
Josef Bacik | 2aaa665 | 2012-08-29 14:27:18 -0400 | [diff] [blame] | 4165 | if ((offset & (blocksize - 1)) == 0 && |
| 4166 | (!len || ((len & (blocksize - 1)) == 0))) |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 4167 | goto out; |
Yan, Zheng | 0ca1f7c | 2010-05-16 10:48:47 -0400 | [diff] [blame] | 4168 | ret = btrfs_delalloc_reserve_space(inode, PAGE_CACHE_SIZE); |
Josef Bacik | 5d5e103 | 2009-10-13 16:46:49 -0400 | [diff] [blame] | 4169 | if (ret) |
| 4170 | goto out; |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 4171 | |
Chris Mason | 211c17f | 2008-05-15 09:13:45 -0400 | [diff] [blame] | 4172 | again: |
Josef Bacik | 3b16a4e | 2011-09-21 15:05:58 -0400 | [diff] [blame] | 4173 | page = find_or_create_page(mapping, index, mask); |
Josef Bacik | 5d5e103 | 2009-10-13 16:46:49 -0400 | [diff] [blame] | 4174 | if (!page) { |
Yan, Zheng | 0ca1f7c | 2010-05-16 10:48:47 -0400 | [diff] [blame] | 4175 | btrfs_delalloc_release_space(inode, PAGE_CACHE_SIZE); |
Miao Xie | ac6a2b3 | 2012-12-05 10:56:13 +0000 | [diff] [blame] | 4176 | ret = -ENOMEM; |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 4177 | goto out; |
Josef Bacik | 5d5e103 | 2009-10-13 16:46:49 -0400 | [diff] [blame] | 4178 | } |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 4179 | |
| 4180 | page_start = page_offset(page); |
| 4181 | page_end = page_start + PAGE_CACHE_SIZE - 1; |
| 4182 | |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 4183 | if (!PageUptodate(page)) { |
| 4184 | ret = btrfs_readpage(NULL, page); |
| 4185 | lock_page(page); |
Chris Mason | 211c17f | 2008-05-15 09:13:45 -0400 | [diff] [blame] | 4186 | if (page->mapping != mapping) { |
| 4187 | unlock_page(page); |
| 4188 | page_cache_release(page); |
| 4189 | goto again; |
| 4190 | } |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 4191 | if (!PageUptodate(page)) { |
| 4192 | ret = -EIO; |
Chris Mason | 8964222 | 2008-07-24 09:41:53 -0400 | [diff] [blame] | 4193 | goto out_unlock; |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 4194 | } |
| 4195 | } |
Chris Mason | 211c17f | 2008-05-15 09:13:45 -0400 | [diff] [blame] | 4196 | wait_on_page_writeback(page); |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 4197 | |
Jeff Mahoney | d008237 | 2012-03-01 14:57:19 +0100 | [diff] [blame] | 4198 | lock_extent_bits(io_tree, page_start, page_end, 0, &cached_state); |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 4199 | set_page_extent_mapped(page); |
| 4200 | |
| 4201 | ordered = btrfs_lookup_ordered_extent(inode, page_start); |
| 4202 | if (ordered) { |
Josef Bacik | 2ac55d4 | 2010-02-03 19:33:23 +0000 | [diff] [blame] | 4203 | unlock_extent_cached(io_tree, page_start, page_end, |
| 4204 | &cached_state, GFP_NOFS); |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 4205 | unlock_page(page); |
| 4206 | page_cache_release(page); |
Chris Mason | eb84ae0 | 2008-07-17 13:53:27 -0400 | [diff] [blame] | 4207 | btrfs_start_ordered_extent(inode, ordered, 1); |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 4208 | btrfs_put_ordered_extent(ordered); |
| 4209 | goto again; |
| 4210 | } |
| 4211 | |
Josef Bacik | 2ac55d4 | 2010-02-03 19:33:23 +0000 | [diff] [blame] | 4212 | clear_extent_bit(&BTRFS_I(inode)->io_tree, page_start, page_end, |
Liu Bo | 9e8a4a8 | 2012-09-05 19:10:51 -0600 | [diff] [blame] | 4213 | EXTENT_DIRTY | EXTENT_DELALLOC | |
| 4214 | EXTENT_DO_ACCOUNTING | EXTENT_DEFRAG, |
Josef Bacik | 2ac55d4 | 2010-02-03 19:33:23 +0000 | [diff] [blame] | 4215 | 0, 0, &cached_state, GFP_NOFS); |
Josef Bacik | 5d5e103 | 2009-10-13 16:46:49 -0400 | [diff] [blame] | 4216 | |
Josef Bacik | 2ac55d4 | 2010-02-03 19:33:23 +0000 | [diff] [blame] | 4217 | ret = btrfs_set_extent_delalloc(inode, page_start, page_end, |
| 4218 | &cached_state); |
Josef Bacik | 9ed74f2 | 2009-09-11 16:12:44 -0400 | [diff] [blame] | 4219 | if (ret) { |
Josef Bacik | 2ac55d4 | 2010-02-03 19:33:23 +0000 | [diff] [blame] | 4220 | unlock_extent_cached(io_tree, page_start, page_end, |
| 4221 | &cached_state, GFP_NOFS); |
Josef Bacik | 9ed74f2 | 2009-09-11 16:12:44 -0400 | [diff] [blame] | 4222 | goto out_unlock; |
| 4223 | } |
| 4224 | |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 4225 | if (offset != PAGE_CACHE_SIZE) { |
Josef Bacik | 2aaa665 | 2012-08-29 14:27:18 -0400 | [diff] [blame] | 4226 | if (!len) |
| 4227 | len = PAGE_CACHE_SIZE - offset; |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 4228 | kaddr = kmap(page); |
Josef Bacik | 2aaa665 | 2012-08-29 14:27:18 -0400 | [diff] [blame] | 4229 | if (front) |
| 4230 | memset(kaddr, 0, offset); |
| 4231 | else |
| 4232 | memset(kaddr + offset, 0, len); |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 4233 | flush_dcache_page(page); |
| 4234 | kunmap(page); |
| 4235 | } |
Chris Mason | 247e743 | 2008-07-17 12:53:51 -0400 | [diff] [blame] | 4236 | ClearPageChecked(page); |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 4237 | set_page_dirty(page); |
Josef Bacik | 2ac55d4 | 2010-02-03 19:33:23 +0000 | [diff] [blame] | 4238 | unlock_extent_cached(io_tree, page_start, page_end, &cached_state, |
| 4239 | GFP_NOFS); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4240 | |
Chris Mason | 8964222 | 2008-07-24 09:41:53 -0400 | [diff] [blame] | 4241 | out_unlock: |
Josef Bacik | 5d5e103 | 2009-10-13 16:46:49 -0400 | [diff] [blame] | 4242 | if (ret) |
Yan, Zheng | 0ca1f7c | 2010-05-16 10:48:47 -0400 | [diff] [blame] | 4243 | btrfs_delalloc_release_space(inode, PAGE_CACHE_SIZE); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4244 | unlock_page(page); |
| 4245 | page_cache_release(page); |
| 4246 | out: |
| 4247 | return ret; |
| 4248 | } |
| 4249 | |
Josef Bacik | 695a0d0 | 2011-03-04 15:46:53 -0500 | [diff] [blame] | 4250 | /* |
| 4251 | * This function puts in dummy file extents for the area we're creating a hole |
| 4252 | * for. So if we are truncating this file to a larger size we need to insert |
| 4253 | * these file extents so that btrfs_get_extent will return a EXTENT_MAP_HOLE for |
| 4254 | * the range between oldsize and size |
| 4255 | */ |
Josef Bacik | a41ad39 | 2011-01-31 15:30:16 -0500 | [diff] [blame] | 4256 | 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] | 4257 | { |
| 4258 | struct btrfs_trans_handle *trans; |
| 4259 | struct btrfs_root *root = BTRFS_I(inode)->root; |
| 4260 | struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree; |
Yan, Zheng | a22285a | 2010-05-16 10:48:46 -0400 | [diff] [blame] | 4261 | struct extent_map *em = NULL; |
Josef Bacik | 2ac55d4 | 2010-02-03 19:33:23 +0000 | [diff] [blame] | 4262 | struct extent_state *cached_state = NULL; |
Josef Bacik | 5dc562c | 2012-08-17 13:14:17 -0400 | [diff] [blame] | 4263 | struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree; |
Qu Wenruo | fda2832 | 2013-02-26 08:10:22 +0000 | [diff] [blame] | 4264 | u64 hole_start = ALIGN(oldsize, root->sectorsize); |
| 4265 | u64 block_end = ALIGN(size, root->sectorsize); |
Yan Zheng | 9036c10 | 2008-10-30 14:19:41 -0400 | [diff] [blame] | 4266 | u64 last_byte; |
| 4267 | u64 cur_offset; |
| 4268 | u64 hole_size; |
Josef Bacik | 9ed74f2 | 2009-09-11 16:12:44 -0400 | [diff] [blame] | 4269 | int err = 0; |
Yan Zheng | 9036c10 | 2008-10-30 14:19:41 -0400 | [diff] [blame] | 4270 | |
Josef Bacik | a71754f | 2013-06-17 17:14:39 -0400 | [diff] [blame] | 4271 | /* |
| 4272 | * If our size started in the middle of a page we need to zero out the |
| 4273 | * rest of the page before we expand the i_size, otherwise we could |
| 4274 | * expose stale data. |
| 4275 | */ |
| 4276 | err = btrfs_truncate_page(inode, oldsize, 0, 0); |
| 4277 | if (err) |
| 4278 | return err; |
| 4279 | |
Yan Zheng | 9036c10 | 2008-10-30 14:19:41 -0400 | [diff] [blame] | 4280 | if (size <= hole_start) |
| 4281 | return 0; |
| 4282 | |
Yan Zheng | 9036c10 | 2008-10-30 14:19:41 -0400 | [diff] [blame] | 4283 | while (1) { |
| 4284 | struct btrfs_ordered_extent *ordered; |
| 4285 | btrfs_wait_ordered_range(inode, hole_start, |
| 4286 | block_end - hole_start); |
Josef Bacik | 2ac55d4 | 2010-02-03 19:33:23 +0000 | [diff] [blame] | 4287 | lock_extent_bits(io_tree, hole_start, block_end - 1, 0, |
Jeff Mahoney | d008237 | 2012-03-01 14:57:19 +0100 | [diff] [blame] | 4288 | &cached_state); |
Yan Zheng | 9036c10 | 2008-10-30 14:19:41 -0400 | [diff] [blame] | 4289 | ordered = btrfs_lookup_ordered_extent(inode, hole_start); |
| 4290 | if (!ordered) |
| 4291 | break; |
Josef Bacik | 2ac55d4 | 2010-02-03 19:33:23 +0000 | [diff] [blame] | 4292 | unlock_extent_cached(io_tree, hole_start, block_end - 1, |
| 4293 | &cached_state, GFP_NOFS); |
Yan Zheng | 9036c10 | 2008-10-30 14:19:41 -0400 | [diff] [blame] | 4294 | btrfs_put_ordered_extent(ordered); |
| 4295 | } |
| 4296 | |
Yan Zheng | 9036c10 | 2008-10-30 14:19:41 -0400 | [diff] [blame] | 4297 | cur_offset = hole_start; |
| 4298 | while (1) { |
| 4299 | em = btrfs_get_extent(inode, NULL, 0, cur_offset, |
| 4300 | block_end - cur_offset, 0); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 4301 | if (IS_ERR(em)) { |
| 4302 | err = PTR_ERR(em); |
Zach Brown | f276795 | 2013-01-08 19:37:58 +0000 | [diff] [blame] | 4303 | em = NULL; |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 4304 | break; |
| 4305 | } |
Yan Zheng | 9036c10 | 2008-10-30 14:19:41 -0400 | [diff] [blame] | 4306 | last_byte = min(extent_map_end(em), block_end); |
Qu Wenruo | fda2832 | 2013-02-26 08:10:22 +0000 | [diff] [blame] | 4307 | last_byte = ALIGN(last_byte , root->sectorsize); |
Yan, Zheng | 8082510 | 2009-11-12 09:35:36 +0000 | [diff] [blame] | 4308 | if (!test_bit(EXTENT_FLAG_PREALLOC, &em->flags)) { |
Josef Bacik | 5dc562c | 2012-08-17 13:14:17 -0400 | [diff] [blame] | 4309 | struct extent_map *hole_em; |
Yan Zheng | 9036c10 | 2008-10-30 14:19:41 -0400 | [diff] [blame] | 4310 | hole_size = last_byte - cur_offset; |
Yan, Zheng | 8082510 | 2009-11-12 09:35:36 +0000 | [diff] [blame] | 4311 | |
Miao Xie | 3642320 | 2011-12-14 20:12:02 -0500 | [diff] [blame] | 4312 | trans = btrfs_start_transaction(root, 3); |
Yan, Zheng | a22285a | 2010-05-16 10:48:46 -0400 | [diff] [blame] | 4313 | if (IS_ERR(trans)) { |
| 4314 | err = PTR_ERR(trans); |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 4315 | break; |
Yan, Zheng | a22285a | 2010-05-16 10:48:46 -0400 | [diff] [blame] | 4316 | } |
Yan, Zheng | 8082510 | 2009-11-12 09:35:36 +0000 | [diff] [blame] | 4317 | |
Josef Bacik | 5dc562c | 2012-08-17 13:14:17 -0400 | [diff] [blame] | 4318 | err = btrfs_drop_extents(trans, root, inode, |
| 4319 | cur_offset, |
Josef Bacik | 2671485 | 2012-08-29 12:24:27 -0400 | [diff] [blame] | 4320 | cur_offset + hole_size, 1); |
Miao Xie | 5b39737 | 2011-09-11 10:52:24 -0400 | [diff] [blame] | 4321 | if (err) { |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 4322 | btrfs_abort_transaction(trans, root, err); |
Miao Xie | 5b39737 | 2011-09-11 10:52:24 -0400 | [diff] [blame] | 4323 | btrfs_end_transaction(trans, root); |
Josef Bacik | 3893e33 | 2011-01-31 16:03:11 -0500 | [diff] [blame] | 4324 | break; |
Miao Xie | 5b39737 | 2011-09-11 10:52:24 -0400 | [diff] [blame] | 4325 | } |
Josef Bacik | 9ed74f2 | 2009-09-11 16:12:44 -0400 | [diff] [blame] | 4326 | |
Yan Zheng | 9036c10 | 2008-10-30 14:19:41 -0400 | [diff] [blame] | 4327 | err = btrfs_insert_file_extent(trans, root, |
Li Zefan | 33345d01 | 2011-04-20 10:31:50 +0800 | [diff] [blame] | 4328 | btrfs_ino(inode), cur_offset, 0, |
Yan Zheng | 9036c10 | 2008-10-30 14:19:41 -0400 | [diff] [blame] | 4329 | 0, hole_size, 0, hole_size, |
| 4330 | 0, 0, 0); |
Miao Xie | 5b39737 | 2011-09-11 10:52:24 -0400 | [diff] [blame] | 4331 | if (err) { |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 4332 | btrfs_abort_transaction(trans, root, err); |
Miao Xie | 5b39737 | 2011-09-11 10:52:24 -0400 | [diff] [blame] | 4333 | btrfs_end_transaction(trans, root); |
Josef Bacik | 3893e33 | 2011-01-31 16:03:11 -0500 | [diff] [blame] | 4334 | break; |
Miao Xie | 5b39737 | 2011-09-11 10:52:24 -0400 | [diff] [blame] | 4335 | } |
Yan, Zheng | 8082510 | 2009-11-12 09:35:36 +0000 | [diff] [blame] | 4336 | |
Josef Bacik | 5dc562c | 2012-08-17 13:14:17 -0400 | [diff] [blame] | 4337 | btrfs_drop_extent_cache(inode, cur_offset, |
| 4338 | cur_offset + hole_size - 1, 0); |
| 4339 | hole_em = alloc_extent_map(); |
| 4340 | if (!hole_em) { |
| 4341 | set_bit(BTRFS_INODE_NEEDS_FULL_SYNC, |
| 4342 | &BTRFS_I(inode)->runtime_flags); |
| 4343 | goto next; |
| 4344 | } |
| 4345 | hole_em->start = cur_offset; |
| 4346 | hole_em->len = hole_size; |
| 4347 | hole_em->orig_start = cur_offset; |
Yan, Zheng | 8082510 | 2009-11-12 09:35:36 +0000 | [diff] [blame] | 4348 | |
Josef Bacik | 5dc562c | 2012-08-17 13:14:17 -0400 | [diff] [blame] | 4349 | hole_em->block_start = EXTENT_MAP_HOLE; |
| 4350 | hole_em->block_len = 0; |
Josef Bacik | b493968 | 2012-12-03 10:31:19 -0500 | [diff] [blame] | 4351 | hole_em->orig_block_len = 0; |
Josef Bacik | cc95bef | 2013-04-04 14:31:27 -0400 | [diff] [blame] | 4352 | hole_em->ram_bytes = hole_size; |
Josef Bacik | 5dc562c | 2012-08-17 13:14:17 -0400 | [diff] [blame] | 4353 | hole_em->bdev = root->fs_info->fs_devices->latest_bdev; |
| 4354 | hole_em->compress_type = BTRFS_COMPRESS_NONE; |
| 4355 | hole_em->generation = trans->transid; |
| 4356 | |
| 4357 | while (1) { |
| 4358 | write_lock(&em_tree->lock); |
Josef Bacik | 09a2a8f9 | 2013-04-05 16:51:15 -0400 | [diff] [blame] | 4359 | err = add_extent_mapping(em_tree, hole_em, 1); |
Josef Bacik | 5dc562c | 2012-08-17 13:14:17 -0400 | [diff] [blame] | 4360 | write_unlock(&em_tree->lock); |
| 4361 | if (err != -EEXIST) |
| 4362 | break; |
| 4363 | btrfs_drop_extent_cache(inode, cur_offset, |
| 4364 | cur_offset + |
| 4365 | hole_size - 1, 0); |
| 4366 | } |
| 4367 | free_extent_map(hole_em); |
| 4368 | next: |
Miao Xie | 3642320 | 2011-12-14 20:12:02 -0500 | [diff] [blame] | 4369 | btrfs_update_inode(trans, root, inode); |
Yan, Zheng | 8082510 | 2009-11-12 09:35:36 +0000 | [diff] [blame] | 4370 | btrfs_end_transaction(trans, root); |
Yan Zheng | 9036c10 | 2008-10-30 14:19:41 -0400 | [diff] [blame] | 4371 | } |
| 4372 | free_extent_map(em); |
Yan, Zheng | a22285a | 2010-05-16 10:48:46 -0400 | [diff] [blame] | 4373 | em = NULL; |
Yan Zheng | 9036c10 | 2008-10-30 14:19:41 -0400 | [diff] [blame] | 4374 | cur_offset = last_byte; |
Yan, Zheng | 8082510 | 2009-11-12 09:35:36 +0000 | [diff] [blame] | 4375 | if (cur_offset >= block_end) |
Yan Zheng | 9036c10 | 2008-10-30 14:19:41 -0400 | [diff] [blame] | 4376 | break; |
| 4377 | } |
| 4378 | |
Yan, Zheng | a22285a | 2010-05-16 10:48:46 -0400 | [diff] [blame] | 4379 | free_extent_map(em); |
Josef Bacik | 2ac55d4 | 2010-02-03 19:33:23 +0000 | [diff] [blame] | 4380 | unlock_extent_cached(io_tree, hole_start, block_end - 1, &cached_state, |
| 4381 | GFP_NOFS); |
Yan Zheng | 9036c10 | 2008-10-30 14:19:41 -0400 | [diff] [blame] | 4382 | return err; |
| 4383 | } |
| 4384 | |
Eric Sandeen | 3972f26 | 2013-01-12 02:57:22 +0000 | [diff] [blame] | 4385 | static int btrfs_setsize(struct inode *inode, struct iattr *attr) |
Yan, Zheng | 8082510 | 2009-11-12 09:35:36 +0000 | [diff] [blame] | 4386 | { |
Miao Xie | f4a2f4c | 2011-12-14 20:12:01 -0500 | [diff] [blame] | 4387 | struct btrfs_root *root = BTRFS_I(inode)->root; |
| 4388 | struct btrfs_trans_handle *trans; |
Josef Bacik | a41ad39 | 2011-01-31 15:30:16 -0500 | [diff] [blame] | 4389 | loff_t oldsize = i_size_read(inode); |
Eric Sandeen | 3972f26 | 2013-01-12 02:57:22 +0000 | [diff] [blame] | 4390 | loff_t newsize = attr->ia_size; |
| 4391 | int mask = attr->ia_valid; |
Yan, Zheng | 8082510 | 2009-11-12 09:35:36 +0000 | [diff] [blame] | 4392 | int ret; |
| 4393 | |
Eric Sandeen | 3972f26 | 2013-01-12 02:57:22 +0000 | [diff] [blame] | 4394 | /* |
| 4395 | * The regular truncate() case without ATTR_CTIME and ATTR_MTIME is a |
| 4396 | * special case where we need to update the times despite not having |
| 4397 | * these flags set. For all other operations the VFS set these flags |
| 4398 | * explicitly if it wants a timestamp update. |
| 4399 | */ |
| 4400 | if (newsize != oldsize && (!(mask & (ATTR_CTIME | ATTR_MTIME)))) |
| 4401 | inode->i_ctime = inode->i_mtime = current_fs_time(inode->i_sb); |
| 4402 | |
Josef Bacik | a41ad39 | 2011-01-31 15:30:16 -0500 | [diff] [blame] | 4403 | if (newsize > oldsize) { |
Josef Bacik | a41ad39 | 2011-01-31 15:30:16 -0500 | [diff] [blame] | 4404 | truncate_pagecache(inode, oldsize, newsize); |
| 4405 | ret = btrfs_cont_expand(inode, oldsize, newsize); |
Miao Xie | f4a2f4c | 2011-12-14 20:12:01 -0500 | [diff] [blame] | 4406 | if (ret) |
Yan, Zheng | 8082510 | 2009-11-12 09:35:36 +0000 | [diff] [blame] | 4407 | return ret; |
Yan, Zheng | 8082510 | 2009-11-12 09:35:36 +0000 | [diff] [blame] | 4408 | |
Miao Xie | f4a2f4c | 2011-12-14 20:12:01 -0500 | [diff] [blame] | 4409 | trans = btrfs_start_transaction(root, 1); |
| 4410 | if (IS_ERR(trans)) |
| 4411 | return PTR_ERR(trans); |
| 4412 | |
| 4413 | i_size_write(inode, newsize); |
| 4414 | btrfs_ordered_update_i_size(inode, i_size_read(inode), NULL); |
| 4415 | ret = btrfs_update_inode(trans, root, inode); |
Josef Bacik | 7ad85bb | 2012-01-12 19:10:12 -0500 | [diff] [blame] | 4416 | btrfs_end_transaction(trans, root); |
Josef Bacik | a41ad39 | 2011-01-31 15:30:16 -0500 | [diff] [blame] | 4417 | } else { |
Yan, Zheng | 8082510 | 2009-11-12 09:35:36 +0000 | [diff] [blame] | 4418 | |
Josef Bacik | a41ad39 | 2011-01-31 15:30:16 -0500 | [diff] [blame] | 4419 | /* |
| 4420 | * We're truncating a file that used to have good data down to |
| 4421 | * zero. Make sure it gets into the ordered flush list so that |
| 4422 | * any new writes get down to disk quickly. |
| 4423 | */ |
| 4424 | if (newsize == 0) |
Josef Bacik | 72ac3c0 | 2012-05-23 14:13:11 -0400 | [diff] [blame] | 4425 | set_bit(BTRFS_INODE_ORDERED_DATA_CLOSE, |
| 4426 | &BTRFS_I(inode)->runtime_flags); |
Yan, Zheng | 8082510 | 2009-11-12 09:35:36 +0000 | [diff] [blame] | 4427 | |
Josef Bacik | f3fe820 | 2013-01-07 17:03:21 -0500 | [diff] [blame] | 4428 | /* |
| 4429 | * 1 for the orphan item we're going to add |
| 4430 | * 1 for the orphan item deletion. |
| 4431 | */ |
| 4432 | trans = btrfs_start_transaction(root, 2); |
| 4433 | if (IS_ERR(trans)) |
| 4434 | return PTR_ERR(trans); |
| 4435 | |
| 4436 | /* |
| 4437 | * We need to do this in case we fail at _any_ point during the |
| 4438 | * actual truncate. Once we do the truncate_setsize we could |
| 4439 | * invalidate pages which forces any outstanding ordered io to |
| 4440 | * be instantly completed which will give us extents that need |
| 4441 | * to be truncated. If we fail to get an orphan inode down we |
| 4442 | * could have left over extents that were never meant to live, |
| 4443 | * so we need to garuntee from this point on that everything |
| 4444 | * will be consistent. |
| 4445 | */ |
| 4446 | ret = btrfs_orphan_add(trans, inode); |
| 4447 | btrfs_end_transaction(trans, root); |
| 4448 | if (ret) |
| 4449 | return ret; |
| 4450 | |
Josef Bacik | a41ad39 | 2011-01-31 15:30:16 -0500 | [diff] [blame] | 4451 | /* we don't support swapfiles, so vmtruncate shouldn't fail */ |
| 4452 | truncate_setsize(inode, newsize); |
Miao Xie | 2e60a51 | 2013-02-08 07:01:08 +0000 | [diff] [blame] | 4453 | |
| 4454 | /* Disable nonlocked read DIO to avoid the end less truncate */ |
| 4455 | btrfs_inode_block_unlocked_dio(inode); |
| 4456 | inode_dio_wait(inode); |
| 4457 | btrfs_inode_resume_unlocked_dio(inode); |
| 4458 | |
Josef Bacik | a41ad39 | 2011-01-31 15:30:16 -0500 | [diff] [blame] | 4459 | ret = btrfs_truncate(inode); |
Josef Bacik | f3fe820 | 2013-01-07 17:03:21 -0500 | [diff] [blame] | 4460 | if (ret && inode->i_nlink) |
| 4461 | btrfs_orphan_del(NULL, inode); |
Yan, Zheng | 8082510 | 2009-11-12 09:35:36 +0000 | [diff] [blame] | 4462 | } |
| 4463 | |
Josef Bacik | a41ad39 | 2011-01-31 15:30:16 -0500 | [diff] [blame] | 4464 | return ret; |
Yan, Zheng | 8082510 | 2009-11-12 09:35:36 +0000 | [diff] [blame] | 4465 | } |
| 4466 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4467 | static int btrfs_setattr(struct dentry *dentry, struct iattr *attr) |
| 4468 | { |
| 4469 | struct inode *inode = dentry->d_inode; |
Li Zefan | b83cc96 | 2010-12-20 16:04:08 +0800 | [diff] [blame] | 4470 | struct btrfs_root *root = BTRFS_I(inode)->root; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4471 | int err; |
| 4472 | |
Li Zefan | b83cc96 | 2010-12-20 16:04:08 +0800 | [diff] [blame] | 4473 | if (btrfs_root_readonly(root)) |
| 4474 | return -EROFS; |
| 4475 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4476 | err = inode_change_ok(inode, attr); |
| 4477 | if (err) |
| 4478 | return err; |
| 4479 | |
Chris Mason | 5a3f23d | 2009-03-31 13:27:11 -0400 | [diff] [blame] | 4480 | if (S_ISREG(inode->i_mode) && (attr->ia_valid & ATTR_SIZE)) { |
Eric Sandeen | 3972f26 | 2013-01-12 02:57:22 +0000 | [diff] [blame] | 4481 | err = btrfs_setsize(inode, attr); |
Yan, Zheng | 8082510 | 2009-11-12 09:35:36 +0000 | [diff] [blame] | 4482 | if (err) |
| 4483 | return err; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4484 | } |
Yan Zheng | 9036c10 | 2008-10-30 14:19:41 -0400 | [diff] [blame] | 4485 | |
Christoph Hellwig | 1025774 | 2010-06-04 11:30:02 +0200 | [diff] [blame] | 4486 | if (attr->ia_valid) { |
| 4487 | setattr_copy(inode, attr); |
Josef Bacik | 0c4d2d9 | 2012-04-05 15:03:02 -0400 | [diff] [blame] | 4488 | inode_inc_iversion(inode); |
Josef Bacik | 22c44fe | 2011-11-30 10:45:38 -0500 | [diff] [blame] | 4489 | err = btrfs_dirty_inode(inode); |
Josef Bacik | 33268ea | 2008-07-24 12:16:36 -0400 | [diff] [blame] | 4490 | |
Josef Bacik | 22c44fe | 2011-11-30 10:45:38 -0500 | [diff] [blame] | 4491 | if (!err && attr->ia_valid & ATTR_MODE) |
Christoph Hellwig | 1025774 | 2010-06-04 11:30:02 +0200 | [diff] [blame] | 4492 | err = btrfs_acl_chmod(inode); |
| 4493 | } |
| 4494 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4495 | return err; |
| 4496 | } |
Chris Mason | 61295eb | 2008-01-14 16:24:38 -0500 | [diff] [blame] | 4497 | |
Al Viro | bd55597 | 2010-06-07 11:35:40 -0400 | [diff] [blame] | 4498 | void btrfs_evict_inode(struct inode *inode) |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4499 | { |
| 4500 | struct btrfs_trans_handle *trans; |
| 4501 | struct btrfs_root *root = BTRFS_I(inode)->root; |
Josef Bacik | 726c35f | 2011-09-26 15:46:06 -0400 | [diff] [blame] | 4502 | struct btrfs_block_rsv *rsv, *global_rsv; |
Josef Bacik | 0712718 | 2011-08-19 10:29:59 -0400 | [diff] [blame] | 4503 | u64 min_size = btrfs_calc_trunc_metadata_size(root, 1); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4504 | int ret; |
| 4505 | |
liubo | 1abe9b8 | 2011-03-24 11:18:59 +0000 | [diff] [blame] | 4506 | trace_btrfs_inode_evict(inode); |
| 4507 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4508 | truncate_inode_pages(&inode->i_data, 0); |
Josef Bacik | 0af3d00 | 2010-06-21 14:48:16 -0400 | [diff] [blame] | 4509 | if (inode->i_nlink && (btrfs_root_refs(&root->root_item) != 0 || |
Liu Bo | 83eea1f | 2012-07-10 05:28:39 -0600 | [diff] [blame] | 4510 | btrfs_is_free_space_inode(inode))) |
Al Viro | bd55597 | 2010-06-07 11:35:40 -0400 | [diff] [blame] | 4511 | goto no_delete; |
| 4512 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4513 | if (is_bad_inode(inode)) { |
Josef Bacik | 7b12876 | 2008-07-24 12:17:14 -0400 | [diff] [blame] | 4514 | btrfs_orphan_del(NULL, inode); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4515 | goto no_delete; |
| 4516 | } |
Al Viro | bd55597 | 2010-06-07 11:35:40 -0400 | [diff] [blame] | 4517 | /* 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] | 4518 | btrfs_wait_ordered_range(inode, 0, (u64)-1); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 4519 | |
Yan, Zheng | c71bf09 | 2009-11-12 09:34:40 +0000 | [diff] [blame] | 4520 | if (root->fs_info->log_root_recovering) { |
Liu Bo | 6bf0231 | 2012-06-25 21:59:09 -0600 | [diff] [blame] | 4521 | BUG_ON(test_bit(BTRFS_INODE_HAS_ORPHAN_ITEM, |
Josef Bacik | 8a35d95 | 2012-05-23 14:26:42 -0400 | [diff] [blame] | 4522 | &BTRFS_I(inode)->runtime_flags)); |
Yan, Zheng | c71bf09 | 2009-11-12 09:34:40 +0000 | [diff] [blame] | 4523 | goto no_delete; |
| 4524 | } |
| 4525 | |
Yan, Zheng | 76dda93 | 2009-09-21 16:00:26 -0400 | [diff] [blame] | 4526 | if (inode->i_nlink > 0) { |
| 4527 | BUG_ON(btrfs_root_refs(&root->root_item) != 0); |
| 4528 | goto no_delete; |
| 4529 | } |
| 4530 | |
Miao Xie | 0e8c36a | 2012-12-19 06:59:51 +0000 | [diff] [blame] | 4531 | ret = btrfs_commit_inode_delayed_inode(inode); |
| 4532 | if (ret) { |
| 4533 | btrfs_orphan_del(NULL, inode); |
| 4534 | goto no_delete; |
| 4535 | } |
| 4536 | |
Miao Xie | 66d8f3d | 2012-09-06 04:02:28 -0600 | [diff] [blame] | 4537 | rsv = btrfs_alloc_block_rsv(root, BTRFS_BLOCK_RSV_TEMP); |
Josef Bacik | 4289a66 | 2011-08-05 13:22:24 -0400 | [diff] [blame] | 4538 | if (!rsv) { |
| 4539 | btrfs_orphan_del(NULL, inode); |
| 4540 | goto no_delete; |
| 4541 | } |
Josef Bacik | 4a33854 | 2011-08-29 11:01:31 -0400 | [diff] [blame] | 4542 | rsv->size = min_size; |
Josef Bacik | ca7e70f | 2012-08-27 17:48:15 -0400 | [diff] [blame] | 4543 | rsv->failfast = 1; |
Josef Bacik | 726c35f | 2011-09-26 15:46:06 -0400 | [diff] [blame] | 4544 | global_rsv = &root->fs_info->global_block_rsv; |
Josef Bacik | 4289a66 | 2011-08-05 13:22:24 -0400 | [diff] [blame] | 4545 | |
Chris Mason | dbe674a | 2008-07-17 12:54:05 -0400 | [diff] [blame] | 4546 | btrfs_i_size_write(inode, 0); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 4547 | |
Josef Bacik | 4289a66 | 2011-08-05 13:22:24 -0400 | [diff] [blame] | 4548 | /* |
Miao Xie | 8407aa4 | 2012-09-07 01:43:32 -0600 | [diff] [blame] | 4549 | * This is a bit simpler than btrfs_truncate since we've already |
| 4550 | * reserved our space for our orphan item in the unlink, so we just |
| 4551 | * need to reserve some slack space in case we add bytes and update |
| 4552 | * inode item when doing the truncate. |
Josef Bacik | 4289a66 | 2011-08-05 13:22:24 -0400 | [diff] [blame] | 4553 | */ |
Yan, Zheng | 8082510 | 2009-11-12 09:35:36 +0000 | [diff] [blame] | 4554 | while (1) { |
Miao Xie | 08e007d | 2012-10-16 11:33:38 +0000 | [diff] [blame] | 4555 | ret = btrfs_block_rsv_refill(root, rsv, min_size, |
| 4556 | BTRFS_RESERVE_FLUSH_LIMIT); |
Yan, Zheng | 8082510 | 2009-11-12 09:35:36 +0000 | [diff] [blame] | 4557 | |
Josef Bacik | 726c35f | 2011-09-26 15:46:06 -0400 | [diff] [blame] | 4558 | /* |
| 4559 | * Try and steal from the global reserve since we will |
| 4560 | * likely not use this space anyway, we want to try as |
| 4561 | * hard as possible to get this to work. |
| 4562 | */ |
| 4563 | if (ret) |
| 4564 | ret = btrfs_block_rsv_migrate(global_rsv, rsv, min_size); |
| 4565 | |
Yan, Zheng | d68fc57 | 2010-05-16 10:49:58 -0400 | [diff] [blame] | 4566 | if (ret) { |
Simon Kirby | c2cf52e | 2013-03-19 22:41:23 +0000 | [diff] [blame] | 4567 | btrfs_warn(root->fs_info, |
| 4568 | "Could not get space for a delete, will truncate on mount %d", |
| 4569 | ret); |
Josef Bacik | 4289a66 | 2011-08-05 13:22:24 -0400 | [diff] [blame] | 4570 | btrfs_orphan_del(NULL, inode); |
| 4571 | btrfs_free_block_rsv(root, rsv); |
| 4572 | goto no_delete; |
Yan, Zheng | d68fc57 | 2010-05-16 10:49:58 -0400 | [diff] [blame] | 4573 | } |
| 4574 | |
Miao Xie | 0e8c36a | 2012-12-19 06:59:51 +0000 | [diff] [blame] | 4575 | trans = btrfs_join_transaction(root); |
Josef Bacik | 4289a66 | 2011-08-05 13:22:24 -0400 | [diff] [blame] | 4576 | if (IS_ERR(trans)) { |
| 4577 | btrfs_orphan_del(NULL, inode); |
| 4578 | btrfs_free_block_rsv(root, rsv); |
| 4579 | goto no_delete; |
| 4580 | } |
| 4581 | |
| 4582 | trans->block_rsv = rsv; |
| 4583 | |
Yan, Zheng | d68fc57 | 2010-05-16 10:49:58 -0400 | [diff] [blame] | 4584 | ret = btrfs_truncate_inode_items(trans, root, inode, 0, 0); |
Josef Bacik | ca7e70f | 2012-08-27 17:48:15 -0400 | [diff] [blame] | 4585 | if (ret != -ENOSPC) |
Yan, Zheng | 8082510 | 2009-11-12 09:35:36 +0000 | [diff] [blame] | 4586 | break; |
| 4587 | |
Miao Xie | 8407aa4 | 2012-09-07 01:43:32 -0600 | [diff] [blame] | 4588 | trans->block_rsv = &root->fs_info->trans_block_rsv; |
Yan, Zheng | 8082510 | 2009-11-12 09:35:36 +0000 | [diff] [blame] | 4589 | btrfs_end_transaction(trans, root); |
| 4590 | trans = NULL; |
Liu Bo | b53d3f5 | 2012-11-14 14:34:34 +0000 | [diff] [blame] | 4591 | btrfs_btree_balance_dirty(root); |
Josef Bacik | 7b12876 | 2008-07-24 12:17:14 -0400 | [diff] [blame] | 4592 | } |
| 4593 | |
Josef Bacik | 4289a66 | 2011-08-05 13:22:24 -0400 | [diff] [blame] | 4594 | btrfs_free_block_rsv(root, rsv); |
| 4595 | |
Yan, Zheng | 8082510 | 2009-11-12 09:35:36 +0000 | [diff] [blame] | 4596 | if (ret == 0) { |
Josef Bacik | 4289a66 | 2011-08-05 13:22:24 -0400 | [diff] [blame] | 4597 | trans->block_rsv = root->orphan_block_rsv; |
Yan, Zheng | 8082510 | 2009-11-12 09:35:36 +0000 | [diff] [blame] | 4598 | ret = btrfs_orphan_del(trans, inode); |
| 4599 | BUG_ON(ret); |
| 4600 | } |
Chris Mason | 85e21ba | 2008-01-29 15:11:36 -0500 | [diff] [blame] | 4601 | |
Josef Bacik | 4289a66 | 2011-08-05 13:22:24 -0400 | [diff] [blame] | 4602 | trans->block_rsv = &root->fs_info->trans_block_rsv; |
Li Zefan | 581bb05 | 2011-04-20 10:06:11 +0800 | [diff] [blame] | 4603 | if (!(root == root->fs_info->tree_root || |
| 4604 | root->root_key.objectid == BTRFS_TREE_RELOC_OBJECTID)) |
Li Zefan | 33345d01 | 2011-04-20 10:31:50 +0800 | [diff] [blame] | 4605 | btrfs_return_ino(root, btrfs_ino(inode)); |
Li Zefan | 581bb05 | 2011-04-20 10:06:11 +0800 | [diff] [blame] | 4606 | |
Chris Mason | 54aa1f4 | 2007-06-22 14:16:25 -0400 | [diff] [blame] | 4607 | btrfs_end_transaction(trans, root); |
Liu Bo | b53d3f5 | 2012-11-14 14:34:34 +0000 | [diff] [blame] | 4608 | btrfs_btree_balance_dirty(root); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4609 | no_delete: |
Miao Xie | 89042e5 | 2013-05-15 07:48:15 +0000 | [diff] [blame] | 4610 | btrfs_remove_delayed_node(inode); |
Jan Kara | dbd5768 | 2012-05-03 14:48:02 +0200 | [diff] [blame] | 4611 | clear_inode(inode); |
Yan, Zheng | 8082510 | 2009-11-12 09:35:36 +0000 | [diff] [blame] | 4612 | return; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4613 | } |
| 4614 | |
| 4615 | /* |
| 4616 | * this returns the key found in the dir entry in the location pointer. |
| 4617 | * If no dir entries were found, location->objectid is 0. |
| 4618 | */ |
| 4619 | static int btrfs_inode_by_name(struct inode *dir, struct dentry *dentry, |
| 4620 | struct btrfs_key *location) |
| 4621 | { |
| 4622 | const char *name = dentry->d_name.name; |
| 4623 | int namelen = dentry->d_name.len; |
| 4624 | struct btrfs_dir_item *di; |
| 4625 | struct btrfs_path *path; |
| 4626 | struct btrfs_root *root = BTRFS_I(dir)->root; |
Yan | 0d9f7f3 | 2007-10-25 15:48:28 -0400 | [diff] [blame] | 4627 | int ret = 0; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4628 | |
| 4629 | path = btrfs_alloc_path(); |
Mark Fasheh | d8926bb | 2011-07-13 10:38:47 -0700 | [diff] [blame] | 4630 | if (!path) |
| 4631 | return -ENOMEM; |
Chris Mason | 3954401 | 2007-12-12 14:38:19 -0500 | [diff] [blame] | 4632 | |
Li Zefan | 33345d01 | 2011-04-20 10:31:50 +0800 | [diff] [blame] | 4633 | di = btrfs_lookup_dir_item(NULL, root, path, btrfs_ino(dir), name, |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4634 | namelen, 0); |
Yan | 0d9f7f3 | 2007-10-25 15:48:28 -0400 | [diff] [blame] | 4635 | if (IS_ERR(di)) |
| 4636 | ret = PTR_ERR(di); |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 4637 | |
David Sterba | c704005 | 2011-04-19 18:00:01 +0200 | [diff] [blame] | 4638 | if (IS_ERR_OR_NULL(di)) |
Chris Mason | 3954401 | 2007-12-12 14:38:19 -0500 | [diff] [blame] | 4639 | goto out_err; |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 4640 | |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 4641 | btrfs_dir_item_key_to_cpu(path->nodes[0], di, location); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4642 | out: |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4643 | btrfs_free_path(path); |
| 4644 | return ret; |
Chris Mason | 3954401 | 2007-12-12 14:38:19 -0500 | [diff] [blame] | 4645 | out_err: |
| 4646 | location->objectid = 0; |
| 4647 | goto out; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4648 | } |
| 4649 | |
| 4650 | /* |
| 4651 | * when we hit a tree root in a directory, the btrfs part of the inode |
| 4652 | * needs to be changed to reflect the root directory of the tree root. This |
| 4653 | * is kind of like crossing a mount point. |
| 4654 | */ |
| 4655 | static int fixup_tree_root_location(struct btrfs_root *root, |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 4656 | struct inode *dir, |
| 4657 | struct dentry *dentry, |
| 4658 | struct btrfs_key *location, |
| 4659 | struct btrfs_root **sub_root) |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4660 | { |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 4661 | struct btrfs_path *path; |
| 4662 | struct btrfs_root *new_root; |
| 4663 | struct btrfs_root_ref *ref; |
| 4664 | struct extent_buffer *leaf; |
| 4665 | int ret; |
| 4666 | int err = 0; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4667 | |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 4668 | path = btrfs_alloc_path(); |
| 4669 | if (!path) { |
| 4670 | err = -ENOMEM; |
| 4671 | goto out; |
| 4672 | } |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4673 | |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 4674 | err = -ENOENT; |
| 4675 | ret = btrfs_find_root_ref(root->fs_info->tree_root, path, |
| 4676 | BTRFS_I(dir)->root->root_key.objectid, |
| 4677 | location->objectid); |
| 4678 | if (ret) { |
| 4679 | if (ret < 0) |
| 4680 | err = ret; |
| 4681 | goto out; |
| 4682 | } |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4683 | |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 4684 | leaf = path->nodes[0]; |
| 4685 | ref = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_root_ref); |
Li Zefan | 33345d01 | 2011-04-20 10:31:50 +0800 | [diff] [blame] | 4686 | if (btrfs_root_ref_dirid(leaf, ref) != btrfs_ino(dir) || |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 4687 | btrfs_root_ref_name_len(leaf, ref) != dentry->d_name.len) |
| 4688 | goto out; |
| 4689 | |
| 4690 | ret = memcmp_extent_buffer(leaf, dentry->d_name.name, |
| 4691 | (unsigned long)(ref + 1), |
| 4692 | dentry->d_name.len); |
| 4693 | if (ret) |
| 4694 | goto out; |
| 4695 | |
David Sterba | b3b4aa7 | 2011-04-21 01:20:15 +0200 | [diff] [blame] | 4696 | btrfs_release_path(path); |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 4697 | |
| 4698 | new_root = btrfs_read_fs_root_no_name(root->fs_info, location); |
| 4699 | if (IS_ERR(new_root)) { |
| 4700 | err = PTR_ERR(new_root); |
| 4701 | goto out; |
| 4702 | } |
| 4703 | |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 4704 | *sub_root = new_root; |
| 4705 | location->objectid = btrfs_root_dirid(&new_root->root_item); |
| 4706 | location->type = BTRFS_INODE_ITEM_KEY; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4707 | location->offset = 0; |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 4708 | err = 0; |
| 4709 | out: |
| 4710 | btrfs_free_path(path); |
| 4711 | return err; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4712 | } |
| 4713 | |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 4714 | static void inode_tree_add(struct inode *inode) |
| 4715 | { |
| 4716 | struct btrfs_root *root = BTRFS_I(inode)->root; |
| 4717 | struct btrfs_inode *entry; |
From: Nick Piggin | 03e860b | 2009-08-21 10:09:44 +0200 | [diff] [blame] | 4718 | struct rb_node **p; |
| 4719 | struct rb_node *parent; |
Li Zefan | 33345d01 | 2011-04-20 10:31:50 +0800 | [diff] [blame] | 4720 | u64 ino = btrfs_ino(inode); |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 4721 | |
Al Viro | 1d3382cb | 2010-10-23 15:19:20 -0400 | [diff] [blame] | 4722 | if (inode_unhashed(inode)) |
Yan, Zheng | 76dda93 | 2009-09-21 16:00:26 -0400 | [diff] [blame] | 4723 | return; |
Miao Xie | e1409ce | 2013-05-15 07:48:16 +0000 | [diff] [blame] | 4724 | again: |
| 4725 | parent = NULL; |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 4726 | spin_lock(&root->inode_lock); |
Miao Xie | e1409ce | 2013-05-15 07:48:16 +0000 | [diff] [blame] | 4727 | p = &root->inode_tree.rb_node; |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 4728 | while (*p) { |
| 4729 | parent = *p; |
| 4730 | entry = rb_entry(parent, struct btrfs_inode, rb_node); |
| 4731 | |
Li Zefan | 33345d01 | 2011-04-20 10:31:50 +0800 | [diff] [blame] | 4732 | if (ino < btrfs_ino(&entry->vfs_inode)) |
From: Nick Piggin | 03e860b | 2009-08-21 10:09:44 +0200 | [diff] [blame] | 4733 | p = &parent->rb_left; |
Li Zefan | 33345d01 | 2011-04-20 10:31:50 +0800 | [diff] [blame] | 4734 | else if (ino > btrfs_ino(&entry->vfs_inode)) |
From: Nick Piggin | 03e860b | 2009-08-21 10:09:44 +0200 | [diff] [blame] | 4735 | p = &parent->rb_right; |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 4736 | else { |
| 4737 | WARN_ON(!(entry->vfs_inode.i_state & |
Al Viro | a4ffdde | 2010-06-02 17:38:30 -0400 | [diff] [blame] | 4738 | (I_WILL_FREE | I_FREEING))); |
From: Nick Piggin | 03e860b | 2009-08-21 10:09:44 +0200 | [diff] [blame] | 4739 | rb_erase(parent, &root->inode_tree); |
| 4740 | RB_CLEAR_NODE(parent); |
| 4741 | spin_unlock(&root->inode_lock); |
| 4742 | goto again; |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 4743 | } |
| 4744 | } |
| 4745 | rb_link_node(&BTRFS_I(inode)->rb_node, parent, p); |
| 4746 | rb_insert_color(&BTRFS_I(inode)->rb_node, &root->inode_tree); |
| 4747 | spin_unlock(&root->inode_lock); |
| 4748 | } |
| 4749 | |
| 4750 | static void inode_tree_del(struct inode *inode) |
| 4751 | { |
| 4752 | struct btrfs_root *root = BTRFS_I(inode)->root; |
Yan, Zheng | 76dda93 | 2009-09-21 16:00:26 -0400 | [diff] [blame] | 4753 | int empty = 0; |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 4754 | |
From: Nick Piggin | 03e860b | 2009-08-21 10:09:44 +0200 | [diff] [blame] | 4755 | spin_lock(&root->inode_lock); |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 4756 | if (!RB_EMPTY_NODE(&BTRFS_I(inode)->rb_node)) { |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 4757 | rb_erase(&BTRFS_I(inode)->rb_node, &root->inode_tree); |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 4758 | RB_CLEAR_NODE(&BTRFS_I(inode)->rb_node); |
Yan, Zheng | 76dda93 | 2009-09-21 16:00:26 -0400 | [diff] [blame] | 4759 | empty = RB_EMPTY_ROOT(&root->inode_tree); |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 4760 | } |
From: Nick Piggin | 03e860b | 2009-08-21 10:09:44 +0200 | [diff] [blame] | 4761 | spin_unlock(&root->inode_lock); |
Yan, Zheng | 76dda93 | 2009-09-21 16:00:26 -0400 | [diff] [blame] | 4762 | |
Josef Bacik | 0af3d00 | 2010-06-21 14:48:16 -0400 | [diff] [blame] | 4763 | /* |
| 4764 | * Free space cache has inodes in the tree root, but the tree root has a |
| 4765 | * root_refs of 0, so this could end up dropping the tree root as a |
| 4766 | * snapshot, so we need the extra !root->fs_info->tree_root check to |
| 4767 | * make sure we don't drop it. |
| 4768 | */ |
| 4769 | if (empty && btrfs_root_refs(&root->root_item) == 0 && |
| 4770 | root != root->fs_info->tree_root) { |
Yan, Zheng | 76dda93 | 2009-09-21 16:00:26 -0400 | [diff] [blame] | 4771 | synchronize_srcu(&root->fs_info->subvol_srcu); |
| 4772 | spin_lock(&root->inode_lock); |
| 4773 | empty = RB_EMPTY_ROOT(&root->inode_tree); |
| 4774 | spin_unlock(&root->inode_lock); |
| 4775 | if (empty) |
| 4776 | btrfs_add_dead_root(root); |
| 4777 | } |
| 4778 | } |
| 4779 | |
Jeff Mahoney | 143bede | 2012-03-01 14:56:26 +0100 | [diff] [blame] | 4780 | void btrfs_invalidate_inodes(struct btrfs_root *root) |
Yan, Zheng | 76dda93 | 2009-09-21 16:00:26 -0400 | [diff] [blame] | 4781 | { |
| 4782 | struct rb_node *node; |
| 4783 | struct rb_node *prev; |
| 4784 | struct btrfs_inode *entry; |
| 4785 | struct inode *inode; |
| 4786 | u64 objectid = 0; |
| 4787 | |
| 4788 | WARN_ON(btrfs_root_refs(&root->root_item) != 0); |
| 4789 | |
| 4790 | spin_lock(&root->inode_lock); |
| 4791 | again: |
| 4792 | node = root->inode_tree.rb_node; |
| 4793 | prev = NULL; |
| 4794 | while (node) { |
| 4795 | prev = node; |
| 4796 | entry = rb_entry(node, struct btrfs_inode, rb_node); |
| 4797 | |
Li Zefan | 33345d01 | 2011-04-20 10:31:50 +0800 | [diff] [blame] | 4798 | if (objectid < btrfs_ino(&entry->vfs_inode)) |
Yan, Zheng | 76dda93 | 2009-09-21 16:00:26 -0400 | [diff] [blame] | 4799 | node = node->rb_left; |
Li Zefan | 33345d01 | 2011-04-20 10:31:50 +0800 | [diff] [blame] | 4800 | else if (objectid > btrfs_ino(&entry->vfs_inode)) |
Yan, Zheng | 76dda93 | 2009-09-21 16:00:26 -0400 | [diff] [blame] | 4801 | node = node->rb_right; |
| 4802 | else |
| 4803 | break; |
| 4804 | } |
| 4805 | if (!node) { |
| 4806 | while (prev) { |
| 4807 | entry = rb_entry(prev, struct btrfs_inode, rb_node); |
Li Zefan | 33345d01 | 2011-04-20 10:31:50 +0800 | [diff] [blame] | 4808 | if (objectid <= btrfs_ino(&entry->vfs_inode)) { |
Yan, Zheng | 76dda93 | 2009-09-21 16:00:26 -0400 | [diff] [blame] | 4809 | node = prev; |
| 4810 | break; |
| 4811 | } |
| 4812 | prev = rb_next(prev); |
| 4813 | } |
| 4814 | } |
| 4815 | while (node) { |
| 4816 | entry = rb_entry(node, struct btrfs_inode, rb_node); |
Li Zefan | 33345d01 | 2011-04-20 10:31:50 +0800 | [diff] [blame] | 4817 | objectid = btrfs_ino(&entry->vfs_inode) + 1; |
Yan, Zheng | 76dda93 | 2009-09-21 16:00:26 -0400 | [diff] [blame] | 4818 | inode = igrab(&entry->vfs_inode); |
| 4819 | if (inode) { |
| 4820 | spin_unlock(&root->inode_lock); |
| 4821 | if (atomic_read(&inode->i_count) > 1) |
| 4822 | d_prune_aliases(inode); |
| 4823 | /* |
Al Viro | 45321ac | 2010-06-07 13:43:19 -0400 | [diff] [blame] | 4824 | * btrfs_drop_inode will have it removed from |
Yan, Zheng | 76dda93 | 2009-09-21 16:00:26 -0400 | [diff] [blame] | 4825 | * the inode cache when its usage count |
| 4826 | * hits zero. |
| 4827 | */ |
| 4828 | iput(inode); |
| 4829 | cond_resched(); |
| 4830 | spin_lock(&root->inode_lock); |
| 4831 | goto again; |
| 4832 | } |
| 4833 | |
| 4834 | if (cond_resched_lock(&root->inode_lock)) |
| 4835 | goto again; |
| 4836 | |
| 4837 | node = rb_next(node); |
| 4838 | } |
| 4839 | spin_unlock(&root->inode_lock); |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 4840 | } |
| 4841 | |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 4842 | static int btrfs_init_locked_inode(struct inode *inode, void *p) |
| 4843 | { |
| 4844 | struct btrfs_iget_args *args = p; |
| 4845 | inode->i_ino = args->ino; |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 4846 | BTRFS_I(inode)->root = args->root; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4847 | return 0; |
| 4848 | } |
| 4849 | |
| 4850 | static int btrfs_find_actor(struct inode *inode, void *opaque) |
| 4851 | { |
| 4852 | struct btrfs_iget_args *args = opaque; |
Li Zefan | 33345d01 | 2011-04-20 10:31:50 +0800 | [diff] [blame] | 4853 | return args->ino == btrfs_ino(inode) && |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 4854 | args->root == BTRFS_I(inode)->root; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4855 | } |
| 4856 | |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 4857 | static struct inode *btrfs_iget_locked(struct super_block *s, |
| 4858 | u64 objectid, |
| 4859 | struct btrfs_root *root) |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4860 | { |
| 4861 | struct inode *inode; |
| 4862 | struct btrfs_iget_args args; |
| 4863 | args.ino = objectid; |
| 4864 | args.root = root; |
| 4865 | |
| 4866 | inode = iget5_locked(s, objectid, btrfs_find_actor, |
| 4867 | btrfs_init_locked_inode, |
| 4868 | (void *)&args); |
| 4869 | return inode; |
| 4870 | } |
| 4871 | |
Balaji Rao | 1a54ef8 | 2008-07-21 02:01:04 +0530 | [diff] [blame] | 4872 | /* Get an inode object given its location and corresponding root. |
| 4873 | * Returns in *is_new if the inode was read from disk |
| 4874 | */ |
| 4875 | struct inode *btrfs_iget(struct super_block *s, struct btrfs_key *location, |
Josef Bacik | 73f7341 | 2009-12-04 17:38:27 +0000 | [diff] [blame] | 4876 | struct btrfs_root *root, int *new) |
Balaji Rao | 1a54ef8 | 2008-07-21 02:01:04 +0530 | [diff] [blame] | 4877 | { |
| 4878 | struct inode *inode; |
| 4879 | |
| 4880 | inode = btrfs_iget_locked(s, location->objectid, root); |
| 4881 | if (!inode) |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 4882 | return ERR_PTR(-ENOMEM); |
Balaji Rao | 1a54ef8 | 2008-07-21 02:01:04 +0530 | [diff] [blame] | 4883 | |
| 4884 | if (inode->i_state & I_NEW) { |
| 4885 | BTRFS_I(inode)->root = root; |
| 4886 | memcpy(&BTRFS_I(inode)->location, location, sizeof(*location)); |
| 4887 | btrfs_read_locked_inode(inode); |
Mark Fasheh | 1748f84 | 2011-07-12 11:25:31 -0700 | [diff] [blame] | 4888 | if (!is_bad_inode(inode)) { |
| 4889 | inode_tree_add(inode); |
| 4890 | unlock_new_inode(inode); |
| 4891 | if (new) |
| 4892 | *new = 1; |
| 4893 | } else { |
Sergei Trofimovich | e0b6d65 | 2011-09-11 10:52:24 -0400 | [diff] [blame] | 4894 | unlock_new_inode(inode); |
| 4895 | iput(inode); |
| 4896 | inode = ERR_PTR(-ESTALE); |
Mark Fasheh | 1748f84 | 2011-07-12 11:25:31 -0700 | [diff] [blame] | 4897 | } |
| 4898 | } |
| 4899 | |
Balaji Rao | 1a54ef8 | 2008-07-21 02:01:04 +0530 | [diff] [blame] | 4900 | return inode; |
| 4901 | } |
| 4902 | |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 4903 | static struct inode *new_simple_dir(struct super_block *s, |
| 4904 | struct btrfs_key *key, |
| 4905 | struct btrfs_root *root) |
| 4906 | { |
| 4907 | struct inode *inode = new_inode(s); |
| 4908 | |
| 4909 | if (!inode) |
| 4910 | return ERR_PTR(-ENOMEM); |
| 4911 | |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 4912 | BTRFS_I(inode)->root = root; |
| 4913 | memcpy(&BTRFS_I(inode)->location, key, sizeof(*key)); |
Josef Bacik | 72ac3c0 | 2012-05-23 14:13:11 -0400 | [diff] [blame] | 4914 | set_bit(BTRFS_INODE_DUMMY, &BTRFS_I(inode)->runtime_flags); |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 4915 | |
| 4916 | inode->i_ino = BTRFS_EMPTY_SUBVOL_DIR_OBJECTID; |
Li Zefan | 848cce0 | 2012-02-21 17:04:28 +0800 | [diff] [blame] | 4917 | inode->i_op = &btrfs_dir_ro_inode_operations; |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 4918 | inode->i_fop = &simple_dir_operations; |
| 4919 | inode->i_mode = S_IFDIR | S_IRUGO | S_IWUSR | S_IXUGO; |
| 4920 | inode->i_mtime = inode->i_atime = inode->i_ctime = CURRENT_TIME; |
| 4921 | |
| 4922 | return inode; |
| 4923 | } |
| 4924 | |
Chris Mason | 3de4586 | 2008-11-17 21:02:50 -0500 | [diff] [blame] | 4925 | struct inode *btrfs_lookup_dentry(struct inode *dir, struct dentry *dentry) |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4926 | { |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 4927 | struct inode *inode; |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 4928 | struct btrfs_root *root = BTRFS_I(dir)->root; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4929 | struct btrfs_root *sub_root = root; |
| 4930 | struct btrfs_key location; |
Yan, Zheng | 76dda93 | 2009-09-21 16:00:26 -0400 | [diff] [blame] | 4931 | int index; |
Josef Bacik | b4aff1f | 2011-06-28 16:18:59 -0400 | [diff] [blame] | 4932 | int ret = 0; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4933 | |
| 4934 | if (dentry->d_name.len > BTRFS_NAME_LEN) |
| 4935 | return ERR_PTR(-ENAMETOOLONG); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 4936 | |
Jeff Layton | 39e3c95 | 2012-11-28 11:30:53 -0500 | [diff] [blame] | 4937 | ret = btrfs_inode_by_name(dir, dentry, &location); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4938 | if (ret < 0) |
| 4939 | return ERR_PTR(ret); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 4940 | |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 4941 | if (location.objectid == 0) |
| 4942 | return NULL; |
| 4943 | |
| 4944 | if (location.type == BTRFS_INODE_ITEM_KEY) { |
Josef Bacik | 73f7341 | 2009-12-04 17:38:27 +0000 | [diff] [blame] | 4945 | inode = btrfs_iget(dir->i_sb, &location, root, NULL); |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 4946 | return inode; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4947 | } |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 4948 | |
| 4949 | BUG_ON(location.type != BTRFS_ROOT_ITEM_KEY); |
| 4950 | |
Yan, Zheng | 76dda93 | 2009-09-21 16:00:26 -0400 | [diff] [blame] | 4951 | index = srcu_read_lock(&root->fs_info->subvol_srcu); |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 4952 | ret = fixup_tree_root_location(root, dir, dentry, |
| 4953 | &location, &sub_root); |
| 4954 | if (ret < 0) { |
| 4955 | if (ret != -ENOENT) |
| 4956 | inode = ERR_PTR(ret); |
| 4957 | else |
| 4958 | inode = new_simple_dir(dir->i_sb, &location, sub_root); |
| 4959 | } else { |
Josef Bacik | 73f7341 | 2009-12-04 17:38:27 +0000 | [diff] [blame] | 4960 | inode = btrfs_iget(dir->i_sb, &location, sub_root, NULL); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4961 | } |
Yan, Zheng | 76dda93 | 2009-09-21 16:00:26 -0400 | [diff] [blame] | 4962 | srcu_read_unlock(&root->fs_info->subvol_srcu, index); |
| 4963 | |
Julia Lawall | 34d19ba | 2011-01-24 19:55:19 +0000 | [diff] [blame] | 4964 | if (!IS_ERR(inode) && root != sub_root) { |
Yan, Zheng | c71bf09 | 2009-11-12 09:34:40 +0000 | [diff] [blame] | 4965 | down_read(&root->fs_info->cleanup_work_sem); |
| 4966 | if (!(inode->i_sb->s_flags & MS_RDONLY)) |
Josef Bacik | 66b4ffd | 2011-01-31 16:22:42 -0500 | [diff] [blame] | 4967 | ret = btrfs_orphan_cleanup(sub_root); |
Yan, Zheng | c71bf09 | 2009-11-12 09:34:40 +0000 | [diff] [blame] | 4968 | up_read(&root->fs_info->cleanup_work_sem); |
Josef Bacik | 01cd336 | 2013-06-03 21:39:49 -0400 | [diff] [blame] | 4969 | if (ret) { |
| 4970 | iput(inode); |
Josef Bacik | 66b4ffd | 2011-01-31 16:22:42 -0500 | [diff] [blame] | 4971 | inode = ERR_PTR(ret); |
Josef Bacik | 01cd336 | 2013-06-03 21:39:49 -0400 | [diff] [blame] | 4972 | } |
Yan, Zheng | c71bf09 | 2009-11-12 09:34:40 +0000 | [diff] [blame] | 4973 | } |
| 4974 | |
Chris Mason | 3de4586 | 2008-11-17 21:02:50 -0500 | [diff] [blame] | 4975 | return inode; |
| 4976 | } |
| 4977 | |
Nick Piggin | fe15ce4 | 2011-01-07 17:49:23 +1100 | [diff] [blame] | 4978 | static int btrfs_dentry_delete(const struct dentry *dentry) |
Yan, Zheng | 76dda93 | 2009-09-21 16:00:26 -0400 | [diff] [blame] | 4979 | { |
| 4980 | struct btrfs_root *root; |
Li Zefan | 848cce0 | 2012-02-21 17:04:28 +0800 | [diff] [blame] | 4981 | struct inode *inode = dentry->d_inode; |
Yan, Zheng | 76dda93 | 2009-09-21 16:00:26 -0400 | [diff] [blame] | 4982 | |
Li Zefan | 848cce0 | 2012-02-21 17:04:28 +0800 | [diff] [blame] | 4983 | if (!inode && !IS_ROOT(dentry)) |
| 4984 | inode = dentry->d_parent->d_inode; |
Yan, Zheng | 76dda93 | 2009-09-21 16:00:26 -0400 | [diff] [blame] | 4985 | |
Li Zefan | 848cce0 | 2012-02-21 17:04:28 +0800 | [diff] [blame] | 4986 | if (inode) { |
| 4987 | root = BTRFS_I(inode)->root; |
Yan, Zheng | efefb14 | 2009-10-09 09:25:16 -0400 | [diff] [blame] | 4988 | if (btrfs_root_refs(&root->root_item) == 0) |
| 4989 | return 1; |
Li Zefan | 848cce0 | 2012-02-21 17:04:28 +0800 | [diff] [blame] | 4990 | |
| 4991 | if (btrfs_ino(inode) == BTRFS_EMPTY_SUBVOL_DIR_OBJECTID) |
| 4992 | return 1; |
Yan, Zheng | efefb14 | 2009-10-09 09:25:16 -0400 | [diff] [blame] | 4993 | } |
Yan, Zheng | 76dda93 | 2009-09-21 16:00:26 -0400 | [diff] [blame] | 4994 | return 0; |
| 4995 | } |
| 4996 | |
Josef Bacik | b4aff1f | 2011-06-28 16:18:59 -0400 | [diff] [blame] | 4997 | static void btrfs_dentry_release(struct dentry *dentry) |
| 4998 | { |
| 4999 | if (dentry->d_fsdata) |
| 5000 | kfree(dentry->d_fsdata); |
| 5001 | } |
| 5002 | |
Chris Mason | 3de4586 | 2008-11-17 21:02:50 -0500 | [diff] [blame] | 5003 | static struct dentry *btrfs_lookup(struct inode *dir, struct dentry *dentry, |
Al Viro | 00cd8dd | 2012-06-10 17:13:09 -0400 | [diff] [blame] | 5004 | unsigned int flags) |
Chris Mason | 3de4586 | 2008-11-17 21:02:50 -0500 | [diff] [blame] | 5005 | { |
Josef Bacik | a66e7cc | 2011-09-18 10:34:03 -0400 | [diff] [blame] | 5006 | struct dentry *ret; |
| 5007 | |
| 5008 | ret = d_splice_alias(btrfs_lookup_dentry(dir, dentry), dentry); |
Josef Bacik | a66e7cc | 2011-09-18 10:34:03 -0400 | [diff] [blame] | 5009 | return ret; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5010 | } |
| 5011 | |
Miao Xie | 16cdcec | 2011-04-22 18:12:22 +0800 | [diff] [blame] | 5012 | unsigned char btrfs_filetype_table[] = { |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5013 | DT_UNKNOWN, DT_REG, DT_DIR, DT_CHR, DT_BLK, DT_FIFO, DT_SOCK, DT_LNK |
| 5014 | }; |
| 5015 | |
Al Viro | 9cdda8d | 2013-05-22 16:48:09 -0400 | [diff] [blame] | 5016 | static int btrfs_real_readdir(struct file *file, struct dir_context *ctx) |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5017 | { |
Al Viro | 9cdda8d | 2013-05-22 16:48:09 -0400 | [diff] [blame] | 5018 | struct inode *inode = file_inode(file); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5019 | struct btrfs_root *root = BTRFS_I(inode)->root; |
| 5020 | struct btrfs_item *item; |
| 5021 | struct btrfs_dir_item *di; |
| 5022 | struct btrfs_key key; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 5023 | struct btrfs_key found_key; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5024 | struct btrfs_path *path; |
Miao Xie | 16cdcec | 2011-04-22 18:12:22 +0800 | [diff] [blame] | 5025 | struct list_head ins_list; |
| 5026 | struct list_head del_list; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5027 | int ret; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 5028 | struct extent_buffer *leaf; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5029 | int slot; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5030 | unsigned char d_type; |
| 5031 | int over = 0; |
| 5032 | u32 di_cur; |
| 5033 | u32 di_total; |
| 5034 | u32 di_len; |
| 5035 | int key_type = BTRFS_DIR_INDEX_KEY; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 5036 | char tmp_name[32]; |
| 5037 | char *name_ptr; |
| 5038 | int name_len; |
Al Viro | 9cdda8d | 2013-05-22 16:48:09 -0400 | [diff] [blame] | 5039 | int is_curr = 0; /* ctx->pos points to the current index? */ |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5040 | |
| 5041 | /* FIXME, use a real flag for deciding about the key type */ |
| 5042 | if (root->fs_info->tree_root == root) |
| 5043 | key_type = BTRFS_DIR_ITEM_KEY; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 5044 | |
Al Viro | 9cdda8d | 2013-05-22 16:48:09 -0400 | [diff] [blame] | 5045 | if (!dir_emit_dots(file, ctx)) |
| 5046 | return 0; |
| 5047 | |
David Woodhouse | 49593bf | 2008-08-17 17:08:36 +0100 | [diff] [blame] | 5048 | path = btrfs_alloc_path(); |
Miao Xie | 16cdcec | 2011-04-22 18:12:22 +0800 | [diff] [blame] | 5049 | if (!path) |
| 5050 | return -ENOMEM; |
Chris Mason | ff5714c | 2011-05-28 07:00:39 -0400 | [diff] [blame] | 5051 | |
Josef Bacik | 026fd31 | 2011-05-13 10:32:11 -0400 | [diff] [blame] | 5052 | path->reada = 1; |
David Woodhouse | 49593bf | 2008-08-17 17:08:36 +0100 | [diff] [blame] | 5053 | |
Miao Xie | 16cdcec | 2011-04-22 18:12:22 +0800 | [diff] [blame] | 5054 | if (key_type == BTRFS_DIR_INDEX_KEY) { |
| 5055 | INIT_LIST_HEAD(&ins_list); |
| 5056 | INIT_LIST_HEAD(&del_list); |
| 5057 | btrfs_get_delayed_items(inode, &ins_list, &del_list); |
| 5058 | } |
| 5059 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5060 | btrfs_set_key_type(&key, key_type); |
Al Viro | 9cdda8d | 2013-05-22 16:48:09 -0400 | [diff] [blame] | 5061 | key.offset = ctx->pos; |
Li Zefan | 33345d01 | 2011-04-20 10:31:50 +0800 | [diff] [blame] | 5062 | key.objectid = btrfs_ino(inode); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 5063 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5064 | ret = btrfs_search_slot(NULL, root, &key, path, 0, 0); |
| 5065 | if (ret < 0) |
| 5066 | goto err; |
David Woodhouse | 49593bf | 2008-08-17 17:08:36 +0100 | [diff] [blame] | 5067 | |
| 5068 | while (1) { |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 5069 | leaf = path->nodes[0]; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5070 | slot = path->slots[0]; |
Li Zefan | b9e03af | 2011-03-23 10:43:58 +0800 | [diff] [blame] | 5071 | if (slot >= btrfs_header_nritems(leaf)) { |
| 5072 | ret = btrfs_next_leaf(root, path); |
| 5073 | if (ret < 0) |
| 5074 | goto err; |
| 5075 | else if (ret > 0) |
| 5076 | break; |
| 5077 | continue; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5078 | } |
Chris Mason | 3de4586 | 2008-11-17 21:02:50 -0500 | [diff] [blame] | 5079 | |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 5080 | item = btrfs_item_nr(leaf, slot); |
| 5081 | btrfs_item_key_to_cpu(leaf, &found_key, slot); |
| 5082 | |
| 5083 | if (found_key.objectid != key.objectid) |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5084 | break; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 5085 | if (btrfs_key_type(&found_key) != key_type) |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5086 | break; |
Al Viro | 9cdda8d | 2013-05-22 16:48:09 -0400 | [diff] [blame] | 5087 | if (found_key.offset < ctx->pos) |
Li Zefan | b9e03af | 2011-03-23 10:43:58 +0800 | [diff] [blame] | 5088 | goto next; |
Miao Xie | 16cdcec | 2011-04-22 18:12:22 +0800 | [diff] [blame] | 5089 | if (key_type == BTRFS_DIR_INDEX_KEY && |
| 5090 | btrfs_should_delete_dir_index(&del_list, |
| 5091 | found_key.offset)) |
| 5092 | goto next; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 5093 | |
Al Viro | 9cdda8d | 2013-05-22 16:48:09 -0400 | [diff] [blame] | 5094 | ctx->pos = found_key.offset; |
Miao Xie | 16cdcec | 2011-04-22 18:12:22 +0800 | [diff] [blame] | 5095 | is_curr = 1; |
David Woodhouse | 49593bf | 2008-08-17 17:08:36 +0100 | [diff] [blame] | 5096 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5097 | di = btrfs_item_ptr(leaf, slot, struct btrfs_dir_item); |
| 5098 | di_cur = 0; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 5099 | di_total = btrfs_item_size(leaf, item); |
David Woodhouse | 49593bf | 2008-08-17 17:08:36 +0100 | [diff] [blame] | 5100 | |
| 5101 | while (di_cur < di_total) { |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 5102 | struct btrfs_key location; |
| 5103 | |
Josef Bacik | 22a94d4 | 2011-03-16 16:47:17 -0400 | [diff] [blame] | 5104 | if (verify_dir_item(root, leaf, di)) |
| 5105 | break; |
| 5106 | |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 5107 | name_len = btrfs_dir_name_len(leaf, di); |
David Woodhouse | 49593bf | 2008-08-17 17:08:36 +0100 | [diff] [blame] | 5108 | if (name_len <= sizeof(tmp_name)) { |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 5109 | name_ptr = tmp_name; |
| 5110 | } else { |
| 5111 | name_ptr = kmalloc(name_len, GFP_NOFS); |
David Woodhouse | 49593bf | 2008-08-17 17:08:36 +0100 | [diff] [blame] | 5112 | if (!name_ptr) { |
| 5113 | ret = -ENOMEM; |
| 5114 | goto err; |
| 5115 | } |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 5116 | } |
| 5117 | read_extent_buffer(leaf, name_ptr, |
| 5118 | (unsigned long)(di + 1), name_len); |
| 5119 | |
| 5120 | d_type = btrfs_filetype_table[btrfs_dir_type(leaf, di)]; |
| 5121 | btrfs_dir_item_key_to_cpu(leaf, di, &location); |
Chris Mason | 3de4586 | 2008-11-17 21:02:50 -0500 | [diff] [blame] | 5122 | |
Josef Bacik | b4aff1f | 2011-06-28 16:18:59 -0400 | [diff] [blame] | 5123 | |
Chris Mason | 3de4586 | 2008-11-17 21:02:50 -0500 | [diff] [blame] | 5124 | /* is this a reference to our own snapshot? If so |
Arne Jansen | 8c9c2bf | 2012-02-25 09:09:30 +0100 | [diff] [blame] | 5125 | * skip it. |
| 5126 | * |
| 5127 | * In contrast to old kernels, we insert the snapshot's |
| 5128 | * dir item and dir index after it has been created, so |
| 5129 | * we won't find a reference to our own snapshot. We |
| 5130 | * still keep the following code for backward |
| 5131 | * compatibility. |
Chris Mason | 3de4586 | 2008-11-17 21:02:50 -0500 | [diff] [blame] | 5132 | */ |
| 5133 | if (location.type == BTRFS_ROOT_ITEM_KEY && |
| 5134 | location.objectid == root->root_key.objectid) { |
| 5135 | over = 0; |
| 5136 | goto skip; |
| 5137 | } |
Al Viro | 9cdda8d | 2013-05-22 16:48:09 -0400 | [diff] [blame] | 5138 | over = !dir_emit(ctx, name_ptr, name_len, |
| 5139 | location.objectid, d_type); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 5140 | |
Chris Mason | 3de4586 | 2008-11-17 21:02:50 -0500 | [diff] [blame] | 5141 | skip: |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 5142 | if (name_ptr != tmp_name) |
| 5143 | kfree(name_ptr); |
| 5144 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5145 | if (over) |
| 5146 | goto nopos; |
Josef Bacik | 5103e94 | 2007-11-16 11:45:54 -0500 | [diff] [blame] | 5147 | di_len = btrfs_dir_name_len(leaf, di) + |
David Woodhouse | 49593bf | 2008-08-17 17:08:36 +0100 | [diff] [blame] | 5148 | btrfs_dir_data_len(leaf, di) + sizeof(*di); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5149 | di_cur += di_len; |
| 5150 | di = (struct btrfs_dir_item *)((char *)di + di_len); |
| 5151 | } |
Li Zefan | b9e03af | 2011-03-23 10:43:58 +0800 | [diff] [blame] | 5152 | next: |
| 5153 | path->slots[0]++; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5154 | } |
David Woodhouse | 49593bf | 2008-08-17 17:08:36 +0100 | [diff] [blame] | 5155 | |
Miao Xie | 16cdcec | 2011-04-22 18:12:22 +0800 | [diff] [blame] | 5156 | if (key_type == BTRFS_DIR_INDEX_KEY) { |
| 5157 | if (is_curr) |
Al Viro | 9cdda8d | 2013-05-22 16:48:09 -0400 | [diff] [blame] | 5158 | ctx->pos++; |
| 5159 | ret = btrfs_readdir_delayed_dir_index(ctx, &ins_list); |
Miao Xie | 16cdcec | 2011-04-22 18:12:22 +0800 | [diff] [blame] | 5160 | if (ret) |
| 5161 | goto nopos; |
| 5162 | } |
| 5163 | |
David Woodhouse | 49593bf | 2008-08-17 17:08:36 +0100 | [diff] [blame] | 5164 | /* Reached end of directory/root. Bump pos past the last item. */ |
Zach Brown | db62efb | 2013-07-11 16:19:42 -0700 | [diff] [blame] | 5165 | ctx->pos++; |
| 5166 | |
| 5167 | /* |
| 5168 | * Stop new entries from being returned after we return the last |
| 5169 | * entry. |
| 5170 | * |
| 5171 | * New directory entries are assigned a strictly increasing |
| 5172 | * offset. This means that new entries created during readdir |
| 5173 | * are *guaranteed* to be seen in the future by that readdir. |
| 5174 | * This has broken buggy programs which operate on names as |
| 5175 | * they're returned by readdir. Until we re-use freed offsets |
| 5176 | * we have this hack to stop new entries from being returned |
| 5177 | * under the assumption that they'll never reach this huge |
| 5178 | * offset. |
| 5179 | * |
| 5180 | * This is being careful not to overflow 32bit loff_t unless the |
| 5181 | * last entry requires it because doing so has broken 32bit apps |
| 5182 | * in the past. |
| 5183 | */ |
| 5184 | if (key_type == BTRFS_DIR_INDEX_KEY) { |
| 5185 | if (ctx->pos >= INT_MAX) |
| 5186 | ctx->pos = LLONG_MAX; |
| 5187 | else |
| 5188 | ctx->pos = INT_MAX; |
| 5189 | } |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5190 | nopos: |
| 5191 | ret = 0; |
| 5192 | err: |
Miao Xie | 16cdcec | 2011-04-22 18:12:22 +0800 | [diff] [blame] | 5193 | if (key_type == BTRFS_DIR_INDEX_KEY) |
| 5194 | btrfs_put_delayed_items(&ins_list, &del_list); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5195 | btrfs_free_path(path); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5196 | return ret; |
| 5197 | } |
| 5198 | |
Christoph Hellwig | a9185b4 | 2010-03-05 09:21:37 +0100 | [diff] [blame] | 5199 | int btrfs_write_inode(struct inode *inode, struct writeback_control *wbc) |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5200 | { |
| 5201 | struct btrfs_root *root = BTRFS_I(inode)->root; |
| 5202 | struct btrfs_trans_handle *trans; |
| 5203 | int ret = 0; |
Josef Bacik | 0af3d00 | 2010-06-21 14:48:16 -0400 | [diff] [blame] | 5204 | bool nolock = false; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5205 | |
Josef Bacik | 72ac3c0 | 2012-05-23 14:13:11 -0400 | [diff] [blame] | 5206 | if (test_bit(BTRFS_INODE_DUMMY, &BTRFS_I(inode)->runtime_flags)) |
Chris Mason | 4ca8b41 | 2008-08-05 13:30:48 -0400 | [diff] [blame] | 5207 | return 0; |
| 5208 | |
Liu Bo | 83eea1f | 2012-07-10 05:28:39 -0600 | [diff] [blame] | 5209 | 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] | 5210 | nolock = true; |
Josef Bacik | 0af3d00 | 2010-06-21 14:48:16 -0400 | [diff] [blame] | 5211 | |
Christoph Hellwig | a9185b4 | 2010-03-05 09:21:37 +0100 | [diff] [blame] | 5212 | if (wbc->sync_mode == WB_SYNC_ALL) { |
Josef Bacik | 0af3d00 | 2010-06-21 14:48:16 -0400 | [diff] [blame] | 5213 | if (nolock) |
Josef Bacik | 7a7eaa4 | 2011-04-13 12:54:33 -0400 | [diff] [blame] | 5214 | trans = btrfs_join_transaction_nolock(root); |
Josef Bacik | 0af3d00 | 2010-06-21 14:48:16 -0400 | [diff] [blame] | 5215 | else |
Josef Bacik | 7a7eaa4 | 2011-04-13 12:54:33 -0400 | [diff] [blame] | 5216 | trans = btrfs_join_transaction(root); |
Tsutomu Itoh | 3612b49 | 2011-01-25 02:51:38 +0000 | [diff] [blame] | 5217 | if (IS_ERR(trans)) |
| 5218 | return PTR_ERR(trans); |
Miao Xie | a698d075 | 2012-09-20 01:51:59 -0600 | [diff] [blame] | 5219 | ret = btrfs_commit_transaction(trans, root); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5220 | } |
| 5221 | return ret; |
| 5222 | } |
| 5223 | |
| 5224 | /* |
Chris Mason | 54aa1f4 | 2007-06-22 14:16:25 -0400 | [diff] [blame] | 5225 | * This is somewhat expensive, updating the tree every time the |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5226 | * inode changes. But, it is most likely to find the inode in cache. |
| 5227 | * FIXME, needs more benchmarking...there are no reasons other than performance |
| 5228 | * to keep or drop this code. |
| 5229 | */ |
Eric Sandeen | 48a3b63 | 2013-04-25 20:41:01 +0000 | [diff] [blame] | 5230 | static int btrfs_dirty_inode(struct inode *inode) |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5231 | { |
| 5232 | struct btrfs_root *root = BTRFS_I(inode)->root; |
| 5233 | struct btrfs_trans_handle *trans; |
Yan, Zheng | 8929ecfa | 2010-05-16 10:49:58 -0400 | [diff] [blame] | 5234 | int ret; |
| 5235 | |
Josef Bacik | 72ac3c0 | 2012-05-23 14:13:11 -0400 | [diff] [blame] | 5236 | if (test_bit(BTRFS_INODE_DUMMY, &BTRFS_I(inode)->runtime_flags)) |
Josef Bacik | 22c44fe | 2011-11-30 10:45:38 -0500 | [diff] [blame] | 5237 | return 0; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5238 | |
Josef Bacik | 7a7eaa4 | 2011-04-13 12:54:33 -0400 | [diff] [blame] | 5239 | trans = btrfs_join_transaction(root); |
Josef Bacik | 22c44fe | 2011-11-30 10:45:38 -0500 | [diff] [blame] | 5240 | if (IS_ERR(trans)) |
| 5241 | return PTR_ERR(trans); |
Yan, Zheng | 8929ecfa | 2010-05-16 10:49:58 -0400 | [diff] [blame] | 5242 | |
| 5243 | ret = btrfs_update_inode(trans, root, inode); |
Chris Mason | 94b6044 | 2010-05-26 11:02:00 -0400 | [diff] [blame] | 5244 | if (ret && ret == -ENOSPC) { |
| 5245 | /* whoops, lets try again with the full transaction */ |
| 5246 | btrfs_end_transaction(trans, root); |
| 5247 | trans = btrfs_start_transaction(root, 1); |
Josef Bacik | 22c44fe | 2011-11-30 10:45:38 -0500 | [diff] [blame] | 5248 | if (IS_ERR(trans)) |
| 5249 | return PTR_ERR(trans); |
Yan, Zheng | 8929ecfa | 2010-05-16 10:49:58 -0400 | [diff] [blame] | 5250 | |
Chris Mason | 94b6044 | 2010-05-26 11:02:00 -0400 | [diff] [blame] | 5251 | ret = btrfs_update_inode(trans, root, inode); |
Chris Mason | 94b6044 | 2010-05-26 11:02:00 -0400 | [diff] [blame] | 5252 | } |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5253 | btrfs_end_transaction(trans, root); |
Miao Xie | 16cdcec | 2011-04-22 18:12:22 +0800 | [diff] [blame] | 5254 | if (BTRFS_I(inode)->delayed_node) |
| 5255 | btrfs_balance_delayed_items(root); |
Josef Bacik | 22c44fe | 2011-11-30 10:45:38 -0500 | [diff] [blame] | 5256 | |
| 5257 | return ret; |
| 5258 | } |
| 5259 | |
| 5260 | /* |
| 5261 | * This is a copy of file_update_time. We need this so we can return error on |
| 5262 | * ENOSPC for updating the inode in the case of file write and mmap writes. |
| 5263 | */ |
Josef Bacik | e41f941 | 2012-03-26 09:46:47 -0400 | [diff] [blame] | 5264 | static int btrfs_update_time(struct inode *inode, struct timespec *now, |
| 5265 | int flags) |
Josef Bacik | 22c44fe | 2011-11-30 10:45:38 -0500 | [diff] [blame] | 5266 | { |
Alexander Block | 2bc556528 | 2012-06-15 09:49:33 +0200 | [diff] [blame] | 5267 | struct btrfs_root *root = BTRFS_I(inode)->root; |
| 5268 | |
| 5269 | if (btrfs_root_readonly(root)) |
| 5270 | return -EROFS; |
| 5271 | |
Josef Bacik | e41f941 | 2012-03-26 09:46:47 -0400 | [diff] [blame] | 5272 | if (flags & S_VERSION) |
Josef Bacik | 22c44fe | 2011-11-30 10:45:38 -0500 | [diff] [blame] | 5273 | inode_inc_iversion(inode); |
Josef Bacik | e41f941 | 2012-03-26 09:46:47 -0400 | [diff] [blame] | 5274 | if (flags & S_CTIME) |
| 5275 | inode->i_ctime = *now; |
| 5276 | if (flags & S_MTIME) |
| 5277 | inode->i_mtime = *now; |
| 5278 | if (flags & S_ATIME) |
| 5279 | inode->i_atime = *now; |
| 5280 | return btrfs_dirty_inode(inode); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5281 | } |
| 5282 | |
Chris Mason | d352ac6 | 2008-09-29 15:18:18 -0400 | [diff] [blame] | 5283 | /* |
| 5284 | * find the highest existing sequence number in a directory |
| 5285 | * and then set the in-memory index_cnt variable to reflect |
| 5286 | * free sequence numbers |
| 5287 | */ |
Josef Bacik | aec7477 | 2008-07-24 12:12:38 -0400 | [diff] [blame] | 5288 | static int btrfs_set_inode_index_count(struct inode *inode) |
| 5289 | { |
| 5290 | struct btrfs_root *root = BTRFS_I(inode)->root; |
| 5291 | struct btrfs_key key, found_key; |
| 5292 | struct btrfs_path *path; |
| 5293 | struct extent_buffer *leaf; |
| 5294 | int ret; |
| 5295 | |
Li Zefan | 33345d01 | 2011-04-20 10:31:50 +0800 | [diff] [blame] | 5296 | key.objectid = btrfs_ino(inode); |
Josef Bacik | aec7477 | 2008-07-24 12:12:38 -0400 | [diff] [blame] | 5297 | btrfs_set_key_type(&key, BTRFS_DIR_INDEX_KEY); |
| 5298 | key.offset = (u64)-1; |
| 5299 | |
| 5300 | path = btrfs_alloc_path(); |
| 5301 | if (!path) |
| 5302 | return -ENOMEM; |
| 5303 | |
| 5304 | ret = btrfs_search_slot(NULL, root, &key, path, 0, 0); |
| 5305 | if (ret < 0) |
| 5306 | goto out; |
| 5307 | /* FIXME: we should be able to handle this */ |
| 5308 | if (ret == 0) |
| 5309 | goto out; |
| 5310 | ret = 0; |
| 5311 | |
| 5312 | /* |
| 5313 | * MAGIC NUMBER EXPLANATION: |
| 5314 | * since we search a directory based on f_pos we have to start at 2 |
| 5315 | * since '.' and '..' have f_pos of 0 and 1 respectively, so everybody |
| 5316 | * else has to start at 2 |
| 5317 | */ |
| 5318 | if (path->slots[0] == 0) { |
| 5319 | BTRFS_I(inode)->index_cnt = 2; |
| 5320 | goto out; |
| 5321 | } |
| 5322 | |
| 5323 | path->slots[0]--; |
| 5324 | |
| 5325 | leaf = path->nodes[0]; |
| 5326 | btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]); |
| 5327 | |
Li Zefan | 33345d01 | 2011-04-20 10:31:50 +0800 | [diff] [blame] | 5328 | if (found_key.objectid != btrfs_ino(inode) || |
Josef Bacik | aec7477 | 2008-07-24 12:12:38 -0400 | [diff] [blame] | 5329 | btrfs_key_type(&found_key) != BTRFS_DIR_INDEX_KEY) { |
| 5330 | BTRFS_I(inode)->index_cnt = 2; |
| 5331 | goto out; |
| 5332 | } |
| 5333 | |
| 5334 | BTRFS_I(inode)->index_cnt = found_key.offset + 1; |
| 5335 | out: |
| 5336 | btrfs_free_path(path); |
| 5337 | return ret; |
| 5338 | } |
| 5339 | |
Chris Mason | d352ac6 | 2008-09-29 15:18:18 -0400 | [diff] [blame] | 5340 | /* |
| 5341 | * helper to find a free sequence number in a given directory. This current |
| 5342 | * code is very simple, later versions will do smarter things in the btree |
| 5343 | */ |
Chris Mason | 3de4586 | 2008-11-17 21:02:50 -0500 | [diff] [blame] | 5344 | int btrfs_set_inode_index(struct inode *dir, u64 *index) |
Josef Bacik | aec7477 | 2008-07-24 12:12:38 -0400 | [diff] [blame] | 5345 | { |
| 5346 | int ret = 0; |
| 5347 | |
| 5348 | if (BTRFS_I(dir)->index_cnt == (u64)-1) { |
Miao Xie | 16cdcec | 2011-04-22 18:12:22 +0800 | [diff] [blame] | 5349 | ret = btrfs_inode_delayed_dir_index_count(dir); |
| 5350 | if (ret) { |
| 5351 | ret = btrfs_set_inode_index_count(dir); |
| 5352 | if (ret) |
| 5353 | return ret; |
| 5354 | } |
Josef Bacik | aec7477 | 2008-07-24 12:12:38 -0400 | [diff] [blame] | 5355 | } |
| 5356 | |
Chris Mason | 00e4e6b | 2008-08-05 11:18:09 -0400 | [diff] [blame] | 5357 | *index = BTRFS_I(dir)->index_cnt; |
Josef Bacik | aec7477 | 2008-07-24 12:12:38 -0400 | [diff] [blame] | 5358 | BTRFS_I(dir)->index_cnt++; |
| 5359 | |
| 5360 | return ret; |
| 5361 | } |
| 5362 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5363 | static struct inode *btrfs_new_inode(struct btrfs_trans_handle *trans, |
| 5364 | struct btrfs_root *root, |
Josef Bacik | aec7477 | 2008-07-24 12:12:38 -0400 | [diff] [blame] | 5365 | struct inode *dir, |
Chris Mason | 9c58309 | 2008-01-29 15:15:18 -0500 | [diff] [blame] | 5366 | const char *name, int name_len, |
Al Viro | 175a4eb | 2011-07-26 03:30:54 -0400 | [diff] [blame] | 5367 | u64 ref_objectid, u64 objectid, |
| 5368 | umode_t mode, u64 *index) |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5369 | { |
| 5370 | struct inode *inode; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 5371 | struct btrfs_inode_item *inode_item; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5372 | struct btrfs_key *location; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 5373 | struct btrfs_path *path; |
Chris Mason | 9c58309 | 2008-01-29 15:15:18 -0500 | [diff] [blame] | 5374 | struct btrfs_inode_ref *ref; |
| 5375 | struct btrfs_key key[2]; |
| 5376 | u32 sizes[2]; |
| 5377 | unsigned long ptr; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5378 | int ret; |
| 5379 | int owner; |
| 5380 | |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 5381 | path = btrfs_alloc_path(); |
Mark Fasheh | d8926bb | 2011-07-13 10:38:47 -0700 | [diff] [blame] | 5382 | if (!path) |
| 5383 | return ERR_PTR(-ENOMEM); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 5384 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5385 | inode = new_inode(root->fs_info->sb); |
Yoshinori Sano | 8fb2764 | 2011-04-09 02:30:07 +0000 | [diff] [blame] | 5386 | if (!inode) { |
| 5387 | btrfs_free_path(path); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5388 | return ERR_PTR(-ENOMEM); |
Yoshinori Sano | 8fb2764 | 2011-04-09 02:30:07 +0000 | [diff] [blame] | 5389 | } |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5390 | |
Li Zefan | 581bb05 | 2011-04-20 10:06:11 +0800 | [diff] [blame] | 5391 | /* |
| 5392 | * we have to initialize this early, so we can reclaim the inode |
| 5393 | * number if we fail afterwards in this function. |
| 5394 | */ |
| 5395 | inode->i_ino = objectid; |
| 5396 | |
Josef Bacik | aec7477 | 2008-07-24 12:12:38 -0400 | [diff] [blame] | 5397 | if (dir) { |
liubo | 1abe9b8 | 2011-03-24 11:18:59 +0000 | [diff] [blame] | 5398 | trace_btrfs_inode_request(dir); |
| 5399 | |
Chris Mason | 3de4586 | 2008-11-17 21:02:50 -0500 | [diff] [blame] | 5400 | ret = btrfs_set_inode_index(dir, index); |
Shen Feng | 0977143 | 2009-04-02 16:46:06 -0400 | [diff] [blame] | 5401 | if (ret) { |
Yoshinori Sano | 8fb2764 | 2011-04-09 02:30:07 +0000 | [diff] [blame] | 5402 | btrfs_free_path(path); |
Shen Feng | 0977143 | 2009-04-02 16:46:06 -0400 | [diff] [blame] | 5403 | iput(inode); |
Josef Bacik | aec7477 | 2008-07-24 12:12:38 -0400 | [diff] [blame] | 5404 | return ERR_PTR(ret); |
Shen Feng | 0977143 | 2009-04-02 16:46:06 -0400 | [diff] [blame] | 5405 | } |
Josef Bacik | aec7477 | 2008-07-24 12:12:38 -0400 | [diff] [blame] | 5406 | } |
| 5407 | /* |
| 5408 | * index_cnt is ignored for everything but a dir, |
| 5409 | * btrfs_get_inode_index_count has an explanation for the magic |
| 5410 | * number |
| 5411 | */ |
| 5412 | BTRFS_I(inode)->index_cnt = 2; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5413 | BTRFS_I(inode)->root = root; |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 5414 | BTRFS_I(inode)->generation = trans->transid; |
Josef Bacik | 7619585 | 2010-11-19 02:18:02 +0000 | [diff] [blame] | 5415 | inode->i_generation = BTRFS_I(inode)->generation; |
Chris Mason | b888db2 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 5416 | |
Josef Bacik | 5dc562c | 2012-08-17 13:14:17 -0400 | [diff] [blame] | 5417 | /* |
| 5418 | * We could have gotten an inode number from somebody who was fsynced |
| 5419 | * and then removed in this same transaction, so let's just set full |
| 5420 | * sync since it will be a full sync anyway and this will blow away the |
| 5421 | * old info in the log. |
| 5422 | */ |
| 5423 | set_bit(BTRFS_INODE_NEEDS_FULL_SYNC, &BTRFS_I(inode)->runtime_flags); |
| 5424 | |
Al Viro | 569254b | 2011-07-24 17:08:40 -0400 | [diff] [blame] | 5425 | if (S_ISDIR(mode)) |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5426 | owner = 0; |
| 5427 | else |
| 5428 | owner = 1; |
Chris Mason | 9c58309 | 2008-01-29 15:15:18 -0500 | [diff] [blame] | 5429 | |
| 5430 | key[0].objectid = objectid; |
| 5431 | btrfs_set_key_type(&key[0], BTRFS_INODE_ITEM_KEY); |
| 5432 | key[0].offset = 0; |
| 5433 | |
Mark Fasheh | f186373 | 2012-08-08 11:32:27 -0700 | [diff] [blame] | 5434 | /* |
| 5435 | * Start new inodes with an inode_ref. This is slightly more |
| 5436 | * efficient for small numbers of hard links since they will |
| 5437 | * be packed into one item. Extended refs will kick in if we |
| 5438 | * add more hard links than can fit in the ref item. |
| 5439 | */ |
Chris Mason | 9c58309 | 2008-01-29 15:15:18 -0500 | [diff] [blame] | 5440 | key[1].objectid = objectid; |
| 5441 | btrfs_set_key_type(&key[1], BTRFS_INODE_REF_KEY); |
| 5442 | key[1].offset = ref_objectid; |
| 5443 | |
| 5444 | sizes[0] = sizeof(struct btrfs_inode_item); |
| 5445 | sizes[1] = name_len + sizeof(*ref); |
| 5446 | |
Chris Mason | b947343 | 2009-03-13 11:00:37 -0400 | [diff] [blame] | 5447 | path->leave_spinning = 1; |
Chris Mason | 9c58309 | 2008-01-29 15:15:18 -0500 | [diff] [blame] | 5448 | ret = btrfs_insert_empty_items(trans, root, path, key, sizes, 2); |
| 5449 | if (ret != 0) |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 5450 | goto fail; |
| 5451 | |
Dmitry Monakhov | ecc11fab | 2010-03-04 17:31:47 +0300 | [diff] [blame] | 5452 | inode_init_owner(inode, dir, mode); |
Yan Zheng | a76a3cd | 2008-10-09 11:46:29 -0400 | [diff] [blame] | 5453 | inode_set_bytes(inode, 0); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5454 | inode->i_mtime = inode->i_atime = inode->i_ctime = CURRENT_TIME; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 5455 | inode_item = btrfs_item_ptr(path->nodes[0], path->slots[0], |
| 5456 | struct btrfs_inode_item); |
Li Zefan | 293f7e0 | 2012-07-10 00:58:58 -0600 | [diff] [blame] | 5457 | memset_extent_buffer(path->nodes[0], 0, (unsigned long)inode_item, |
| 5458 | sizeof(*inode_item)); |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 5459 | fill_inode_item(trans, path->nodes[0], inode_item, inode); |
Chris Mason | 9c58309 | 2008-01-29 15:15:18 -0500 | [diff] [blame] | 5460 | |
| 5461 | ref = btrfs_item_ptr(path->nodes[0], path->slots[0] + 1, |
| 5462 | struct btrfs_inode_ref); |
| 5463 | btrfs_set_inode_ref_name_len(path->nodes[0], ref, name_len); |
Chris Mason | 00e4e6b | 2008-08-05 11:18:09 -0400 | [diff] [blame] | 5464 | btrfs_set_inode_ref_index(path->nodes[0], ref, *index); |
Chris Mason | 9c58309 | 2008-01-29 15:15:18 -0500 | [diff] [blame] | 5465 | ptr = (unsigned long)(ref + 1); |
| 5466 | write_extent_buffer(path->nodes[0], name, ptr, name_len); |
| 5467 | |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 5468 | btrfs_mark_buffer_dirty(path->nodes[0]); |
| 5469 | btrfs_free_path(path); |
| 5470 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5471 | location = &BTRFS_I(inode)->location; |
| 5472 | location->objectid = objectid; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5473 | location->offset = 0; |
| 5474 | btrfs_set_key_type(location, BTRFS_INODE_ITEM_KEY); |
| 5475 | |
Christoph Hellwig | 6cbff00 | 2009-04-17 10:37:41 +0200 | [diff] [blame] | 5476 | btrfs_inherit_iflags(inode, dir); |
| 5477 | |
Al Viro | 569254b | 2011-07-24 17:08:40 -0400 | [diff] [blame] | 5478 | if (S_ISREG(mode)) { |
Chris Mason | 9427216 | 2009-07-02 12:26:06 -0400 | [diff] [blame] | 5479 | if (btrfs_test_opt(root, NODATASUM)) |
| 5480 | BTRFS_I(inode)->flags |= BTRFS_INODE_NODATASUM; |
Liu Bo | 213490b | 2012-09-11 08:33:50 -0600 | [diff] [blame] | 5481 | if (btrfs_test_opt(root, NODATACOW)) |
Josef Bacik | f2bdf9a | 2013-02-21 15:28:28 -0500 | [diff] [blame] | 5482 | BTRFS_I(inode)->flags |= BTRFS_INODE_NODATACOW | |
| 5483 | BTRFS_INODE_NODATASUM; |
Chris Mason | 9427216 | 2009-07-02 12:26:06 -0400 | [diff] [blame] | 5484 | } |
| 5485 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5486 | insert_inode_hash(inode); |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 5487 | inode_tree_add(inode); |
liubo | 1abe9b8 | 2011-03-24 11:18:59 +0000 | [diff] [blame] | 5488 | |
| 5489 | trace_btrfs_inode_new(inode); |
Chris Mason | 1973f0f | 2011-06-24 13:13:29 -0400 | [diff] [blame] | 5490 | btrfs_set_inode_last_trans(trans, inode); |
liubo | 1abe9b8 | 2011-03-24 11:18:59 +0000 | [diff] [blame] | 5491 | |
Alexander Block | 8ea05e3 | 2012-07-25 17:35:53 +0200 | [diff] [blame] | 5492 | btrfs_update_root_times(trans, root); |
| 5493 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5494 | return inode; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 5495 | fail: |
Josef Bacik | aec7477 | 2008-07-24 12:12:38 -0400 | [diff] [blame] | 5496 | if (dir) |
| 5497 | BTRFS_I(dir)->index_cnt--; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 5498 | btrfs_free_path(path); |
Shen Feng | 0977143 | 2009-04-02 16:46:06 -0400 | [diff] [blame] | 5499 | iput(inode); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 5500 | return ERR_PTR(ret); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5501 | } |
| 5502 | |
| 5503 | static inline u8 btrfs_inode_type(struct inode *inode) |
| 5504 | { |
| 5505 | return btrfs_type_by_mode[(inode->i_mode & S_IFMT) >> S_SHIFT]; |
| 5506 | } |
| 5507 | |
Chris Mason | d352ac6 | 2008-09-29 15:18:18 -0400 | [diff] [blame] | 5508 | /* |
| 5509 | * utility function to add 'inode' into 'parent_inode' with |
| 5510 | * a give name and a given sequence number. |
| 5511 | * if 'add_backref' is true, also insert a backref from the |
| 5512 | * inode to the parent directory. |
| 5513 | */ |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 5514 | int btrfs_add_link(struct btrfs_trans_handle *trans, |
| 5515 | struct inode *parent_inode, struct inode *inode, |
| 5516 | const char *name, int name_len, int add_backref, u64 index) |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5517 | { |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 5518 | int ret = 0; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5519 | struct btrfs_key key; |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 5520 | struct btrfs_root *root = BTRFS_I(parent_inode)->root; |
Li Zefan | 33345d01 | 2011-04-20 10:31:50 +0800 | [diff] [blame] | 5521 | u64 ino = btrfs_ino(inode); |
| 5522 | u64 parent_ino = btrfs_ino(parent_inode); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 5523 | |
Li Zefan | 33345d01 | 2011-04-20 10:31:50 +0800 | [diff] [blame] | 5524 | if (unlikely(ino == BTRFS_FIRST_FREE_OBJECTID)) { |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 5525 | memcpy(&key, &BTRFS_I(inode)->root->root_key, sizeof(key)); |
| 5526 | } else { |
Li Zefan | 33345d01 | 2011-04-20 10:31:50 +0800 | [diff] [blame] | 5527 | key.objectid = ino; |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 5528 | btrfs_set_key_type(&key, BTRFS_INODE_ITEM_KEY); |
| 5529 | key.offset = 0; |
| 5530 | } |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5531 | |
Li Zefan | 33345d01 | 2011-04-20 10:31:50 +0800 | [diff] [blame] | 5532 | if (unlikely(ino == BTRFS_FIRST_FREE_OBJECTID)) { |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 5533 | ret = btrfs_add_root_ref(trans, root->fs_info->tree_root, |
| 5534 | key.objectid, root->root_key.objectid, |
Li Zefan | 33345d01 | 2011-04-20 10:31:50 +0800 | [diff] [blame] | 5535 | parent_ino, index, name, name_len); |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 5536 | } else if (add_backref) { |
Li Zefan | 33345d01 | 2011-04-20 10:31:50 +0800 | [diff] [blame] | 5537 | ret = btrfs_insert_inode_ref(trans, root, name, name_len, ino, |
| 5538 | parent_ino, index); |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 5539 | } |
| 5540 | |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 5541 | /* Nothing to clean up yet */ |
| 5542 | if (ret) |
| 5543 | return ret; |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 5544 | |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 5545 | ret = btrfs_insert_dir_item(trans, root, name, name_len, |
| 5546 | parent_inode, &key, |
| 5547 | btrfs_inode_type(inode), index); |
Chris Mason | 9c52057 | 2012-12-17 14:26:57 -0500 | [diff] [blame] | 5548 | if (ret == -EEXIST || ret == -EOVERFLOW) |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 5549 | goto fail_dir_item; |
| 5550 | else if (ret) { |
| 5551 | btrfs_abort_transaction(trans, root, ret); |
| 5552 | return ret; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5553 | } |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 5554 | |
| 5555 | btrfs_i_size_write(parent_inode, parent_inode->i_size + |
| 5556 | name_len * 2); |
Josef Bacik | 0c4d2d9 | 2012-04-05 15:03:02 -0400 | [diff] [blame] | 5557 | inode_inc_iversion(parent_inode); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 5558 | parent_inode->i_mtime = parent_inode->i_ctime = CURRENT_TIME; |
| 5559 | ret = btrfs_update_inode(trans, root, parent_inode); |
| 5560 | if (ret) |
| 5561 | btrfs_abort_transaction(trans, root, ret); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5562 | return ret; |
Chris Mason | fe66a05 | 2012-02-20 08:40:56 -0500 | [diff] [blame] | 5563 | |
| 5564 | fail_dir_item: |
| 5565 | if (unlikely(ino == BTRFS_FIRST_FREE_OBJECTID)) { |
| 5566 | u64 local_index; |
| 5567 | int err; |
| 5568 | err = btrfs_del_root_ref(trans, root->fs_info->tree_root, |
| 5569 | key.objectid, root->root_key.objectid, |
| 5570 | parent_ino, &local_index, name, name_len); |
| 5571 | |
| 5572 | } else if (add_backref) { |
| 5573 | u64 local_index; |
| 5574 | int err; |
| 5575 | |
| 5576 | err = btrfs_del_inode_ref(trans, root, name, name_len, |
| 5577 | ino, parent_ino, &local_index); |
| 5578 | } |
| 5579 | return ret; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5580 | } |
| 5581 | |
| 5582 | static int btrfs_add_nondir(struct btrfs_trans_handle *trans, |
Josef Bacik | a1b075d | 2010-11-19 20:36:11 +0000 | [diff] [blame] | 5583 | struct inode *dir, struct dentry *dentry, |
| 5584 | struct inode *inode, int backref, u64 index) |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5585 | { |
Josef Bacik | a1b075d | 2010-11-19 20:36:11 +0000 | [diff] [blame] | 5586 | int err = btrfs_add_link(trans, dir, inode, |
| 5587 | dentry->d_name.name, dentry->d_name.len, |
| 5588 | backref, index); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5589 | if (err > 0) |
| 5590 | err = -EEXIST; |
| 5591 | return err; |
| 5592 | } |
| 5593 | |
Josef Bacik | 618e21d | 2007-07-11 10:18:17 -0400 | [diff] [blame] | 5594 | static int btrfs_mknod(struct inode *dir, struct dentry *dentry, |
Al Viro | 1a67aaf | 2011-07-26 01:52:52 -0400 | [diff] [blame] | 5595 | umode_t mode, dev_t rdev) |
Josef Bacik | 618e21d | 2007-07-11 10:18:17 -0400 | [diff] [blame] | 5596 | { |
| 5597 | struct btrfs_trans_handle *trans; |
| 5598 | struct btrfs_root *root = BTRFS_I(dir)->root; |
Chris Mason | 1832a6d | 2007-12-21 16:27:21 -0500 | [diff] [blame] | 5599 | struct inode *inode = NULL; |
Josef Bacik | 618e21d | 2007-07-11 10:18:17 -0400 | [diff] [blame] | 5600 | int err; |
| 5601 | int drop_inode = 0; |
| 5602 | u64 objectid; |
Chris Mason | 00e4e6b | 2008-08-05 11:18:09 -0400 | [diff] [blame] | 5603 | u64 index = 0; |
Josef Bacik | 618e21d | 2007-07-11 10:18:17 -0400 | [diff] [blame] | 5604 | |
| 5605 | if (!new_valid_dev(rdev)) |
| 5606 | return -EINVAL; |
| 5607 | |
Josef Bacik | 9ed74f2 | 2009-09-11 16:12:44 -0400 | [diff] [blame] | 5608 | /* |
| 5609 | * 2 for inode item and ref |
| 5610 | * 2 for dir items |
| 5611 | * 1 for xattr if selinux is on |
| 5612 | */ |
Yan, Zheng | a22285a | 2010-05-16 10:48:46 -0400 | [diff] [blame] | 5613 | trans = btrfs_start_transaction(root, 5); |
| 5614 | if (IS_ERR(trans)) |
| 5615 | return PTR_ERR(trans); |
Chris Mason | 1832a6d | 2007-12-21 16:27:21 -0500 | [diff] [blame] | 5616 | |
Li Zefan | 581bb05 | 2011-04-20 10:06:11 +0800 | [diff] [blame] | 5617 | err = btrfs_find_free_ino(root, &objectid); |
| 5618 | if (err) |
| 5619 | goto out_unlock; |
| 5620 | |
Josef Bacik | aec7477 | 2008-07-24 12:12:38 -0400 | [diff] [blame] | 5621 | inode = btrfs_new_inode(trans, root, dir, dentry->d_name.name, |
Li Zefan | 33345d01 | 2011-04-20 10:31:50 +0800 | [diff] [blame] | 5622 | dentry->d_name.len, btrfs_ino(dir), objectid, |
Josef Bacik | d82a6f1d | 2011-05-11 15:26:06 -0400 | [diff] [blame] | 5623 | mode, &index); |
Tsutomu Itoh | 7cf96da | 2011-04-25 19:43:53 -0400 | [diff] [blame] | 5624 | if (IS_ERR(inode)) { |
| 5625 | err = PTR_ERR(inode); |
Josef Bacik | 618e21d | 2007-07-11 10:18:17 -0400 | [diff] [blame] | 5626 | goto out_unlock; |
Tsutomu Itoh | 7cf96da | 2011-04-25 19:43:53 -0400 | [diff] [blame] | 5627 | } |
Josef Bacik | 618e21d | 2007-07-11 10:18:17 -0400 | [diff] [blame] | 5628 | |
Eric Paris | 2a7dba3 | 2011-02-01 11:05:39 -0500 | [diff] [blame] | 5629 | err = btrfs_init_inode_security(trans, inode, dir, &dentry->d_name); |
Josef Bacik | 33268ea | 2008-07-24 12:16:36 -0400 | [diff] [blame] | 5630 | if (err) { |
| 5631 | drop_inode = 1; |
| 5632 | goto out_unlock; |
| 5633 | } |
| 5634 | |
Casey Schaufler | ad19db7 | 2011-12-15 10:09:07 -0500 | [diff] [blame] | 5635 | /* |
| 5636 | * If the active LSM wants to access the inode during |
| 5637 | * d_instantiate it needs these. Smack checks to see |
| 5638 | * if the filesystem supports xattrs by looking at the |
| 5639 | * ops vector. |
| 5640 | */ |
| 5641 | |
| 5642 | inode->i_op = &btrfs_special_inode_operations; |
Josef Bacik | a1b075d | 2010-11-19 20:36:11 +0000 | [diff] [blame] | 5643 | err = btrfs_add_nondir(trans, dir, dentry, inode, 0, index); |
Josef Bacik | 618e21d | 2007-07-11 10:18:17 -0400 | [diff] [blame] | 5644 | if (err) |
| 5645 | drop_inode = 1; |
| 5646 | else { |
Josef Bacik | 618e21d | 2007-07-11 10:18:17 -0400 | [diff] [blame] | 5647 | init_special_inode(inode, inode->i_mode, rdev); |
Yan | 1b4ab1b | 2007-08-29 09:11:44 -0400 | [diff] [blame] | 5648 | btrfs_update_inode(trans, root, inode); |
Al Viro | 08c422c | 2011-12-23 07:58:13 -0500 | [diff] [blame] | 5649 | d_instantiate(dentry, inode); |
Josef Bacik | 618e21d | 2007-07-11 10:18:17 -0400 | [diff] [blame] | 5650 | } |
Josef Bacik | 618e21d | 2007-07-11 10:18:17 -0400 | [diff] [blame] | 5651 | out_unlock: |
Josef Bacik | 7ad85bb | 2012-01-12 19:10:12 -0500 | [diff] [blame] | 5652 | btrfs_end_transaction(trans, root); |
Liu Bo | b53d3f5 | 2012-11-14 14:34:34 +0000 | [diff] [blame] | 5653 | btrfs_btree_balance_dirty(root); |
Josef Bacik | 618e21d | 2007-07-11 10:18:17 -0400 | [diff] [blame] | 5654 | if (drop_inode) { |
| 5655 | inode_dec_link_count(inode); |
| 5656 | iput(inode); |
| 5657 | } |
Josef Bacik | 618e21d | 2007-07-11 10:18:17 -0400 | [diff] [blame] | 5658 | return err; |
| 5659 | } |
| 5660 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5661 | static int btrfs_create(struct inode *dir, struct dentry *dentry, |
Al Viro | ebfc3b4 | 2012-06-10 18:05:36 -0400 | [diff] [blame] | 5662 | umode_t mode, bool excl) |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5663 | { |
| 5664 | struct btrfs_trans_handle *trans; |
| 5665 | struct btrfs_root *root = BTRFS_I(dir)->root; |
Chris Mason | 1832a6d | 2007-12-21 16:27:21 -0500 | [diff] [blame] | 5666 | struct inode *inode = NULL; |
Filipe Brandenburger | 43baa57 | 2012-11-30 03:40:09 +0000 | [diff] [blame] | 5667 | int drop_inode_on_err = 0; |
Yan, Zheng | a22285a | 2010-05-16 10:48:46 -0400 | [diff] [blame] | 5668 | int err; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5669 | u64 objectid; |
Chris Mason | 00e4e6b | 2008-08-05 11:18:09 -0400 | [diff] [blame] | 5670 | u64 index = 0; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5671 | |
Josef Bacik | 9ed74f2 | 2009-09-11 16:12:44 -0400 | [diff] [blame] | 5672 | /* |
| 5673 | * 2 for inode item and ref |
| 5674 | * 2 for dir items |
| 5675 | * 1 for xattr if selinux is on |
| 5676 | */ |
Yan, Zheng | a22285a | 2010-05-16 10:48:46 -0400 | [diff] [blame] | 5677 | trans = btrfs_start_transaction(root, 5); |
| 5678 | if (IS_ERR(trans)) |
| 5679 | return PTR_ERR(trans); |
Josef Bacik | 9ed74f2 | 2009-09-11 16:12:44 -0400 | [diff] [blame] | 5680 | |
Li Zefan | 581bb05 | 2011-04-20 10:06:11 +0800 | [diff] [blame] | 5681 | err = btrfs_find_free_ino(root, &objectid); |
| 5682 | if (err) |
| 5683 | goto out_unlock; |
| 5684 | |
Josef Bacik | aec7477 | 2008-07-24 12:12:38 -0400 | [diff] [blame] | 5685 | inode = btrfs_new_inode(trans, root, dir, dentry->d_name.name, |
Li Zefan | 33345d01 | 2011-04-20 10:31:50 +0800 | [diff] [blame] | 5686 | dentry->d_name.len, btrfs_ino(dir), objectid, |
Josef Bacik | d82a6f1d | 2011-05-11 15:26:06 -0400 | [diff] [blame] | 5687 | mode, &index); |
Tsutomu Itoh | 7cf96da | 2011-04-25 19:43:53 -0400 | [diff] [blame] | 5688 | if (IS_ERR(inode)) { |
| 5689 | err = PTR_ERR(inode); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5690 | goto out_unlock; |
Tsutomu Itoh | 7cf96da | 2011-04-25 19:43:53 -0400 | [diff] [blame] | 5691 | } |
Filipe Brandenburger | 43baa57 | 2012-11-30 03:40:09 +0000 | [diff] [blame] | 5692 | drop_inode_on_err = 1; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5693 | |
Eric Paris | 2a7dba3 | 2011-02-01 11:05:39 -0500 | [diff] [blame] | 5694 | err = btrfs_init_inode_security(trans, inode, dir, &dentry->d_name); |
Filipe Brandenburger | 43baa57 | 2012-11-30 03:40:09 +0000 | [diff] [blame] | 5695 | if (err) |
Josef Bacik | 33268ea | 2008-07-24 12:16:36 -0400 | [diff] [blame] | 5696 | goto out_unlock; |
Josef Bacik | 33268ea | 2008-07-24 12:16:36 -0400 | [diff] [blame] | 5697 | |
Filipe Brandenburger | 9185aa5 | 2012-11-30 03:40:08 +0000 | [diff] [blame] | 5698 | err = btrfs_update_inode(trans, root, inode); |
| 5699 | if (err) |
| 5700 | goto out_unlock; |
| 5701 | |
Casey Schaufler | ad19db7 | 2011-12-15 10:09:07 -0500 | [diff] [blame] | 5702 | /* |
| 5703 | * If the active LSM wants to access the inode during |
| 5704 | * d_instantiate it needs these. Smack checks to see |
| 5705 | * if the filesystem supports xattrs by looking at the |
| 5706 | * ops vector. |
| 5707 | */ |
| 5708 | inode->i_fop = &btrfs_file_operations; |
| 5709 | inode->i_op = &btrfs_file_inode_operations; |
| 5710 | |
Josef Bacik | a1b075d | 2010-11-19 20:36:11 +0000 | [diff] [blame] | 5711 | err = btrfs_add_nondir(trans, dir, dentry, inode, 0, index); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5712 | if (err) |
Filipe Brandenburger | 43baa57 | 2012-11-30 03:40:09 +0000 | [diff] [blame] | 5713 | goto out_unlock; |
| 5714 | |
| 5715 | inode->i_mapping->a_ops = &btrfs_aops; |
| 5716 | inode->i_mapping->backing_dev_info = &root->fs_info->bdi; |
| 5717 | BTRFS_I(inode)->io_tree.ops = &btrfs_extent_io_ops; |
| 5718 | d_instantiate(dentry, inode); |
| 5719 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5720 | out_unlock: |
Josef Bacik | 7ad85bb | 2012-01-12 19:10:12 -0500 | [diff] [blame] | 5721 | btrfs_end_transaction(trans, root); |
Filipe Brandenburger | 43baa57 | 2012-11-30 03:40:09 +0000 | [diff] [blame] | 5722 | if (err && drop_inode_on_err) { |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5723 | inode_dec_link_count(inode); |
| 5724 | iput(inode); |
| 5725 | } |
Liu Bo | b53d3f5 | 2012-11-14 14:34:34 +0000 | [diff] [blame] | 5726 | btrfs_btree_balance_dirty(root); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5727 | return err; |
| 5728 | } |
| 5729 | |
| 5730 | static int btrfs_link(struct dentry *old_dentry, struct inode *dir, |
| 5731 | struct dentry *dentry) |
| 5732 | { |
| 5733 | struct btrfs_trans_handle *trans; |
| 5734 | struct btrfs_root *root = BTRFS_I(dir)->root; |
| 5735 | struct inode *inode = old_dentry->d_inode; |
Chris Mason | 00e4e6b | 2008-08-05 11:18:09 -0400 | [diff] [blame] | 5736 | u64 index; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5737 | int err; |
| 5738 | int drop_inode = 0; |
| 5739 | |
TARUISI Hiroaki | 4a8be42 | 2009-11-12 07:14:26 +0000 | [diff] [blame] | 5740 | /* do not allow sys_link's with other subvols of the same device */ |
| 5741 | if (root->objectid != BTRFS_I(inode)->root->objectid) |
Mark Fasheh | 3ab3564 | 2011-03-22 17:20:26 +0000 | [diff] [blame] | 5742 | return -EXDEV; |
TARUISI Hiroaki | 4a8be42 | 2009-11-12 07:14:26 +0000 | [diff] [blame] | 5743 | |
Mark Fasheh | f186373 | 2012-08-08 11:32:27 -0700 | [diff] [blame] | 5744 | if (inode->i_nlink >= BTRFS_LINK_MAX) |
Al Viro | c055e99 | 2011-03-04 17:15:18 +0000 | [diff] [blame] | 5745 | return -EMLINK; |
Josef Bacik | 9ed74f2 | 2009-09-11 16:12:44 -0400 | [diff] [blame] | 5746 | |
Chris Mason | 3de4586 | 2008-11-17 21:02:50 -0500 | [diff] [blame] | 5747 | err = btrfs_set_inode_index(dir, &index); |
Josef Bacik | aec7477 | 2008-07-24 12:12:38 -0400 | [diff] [blame] | 5748 | if (err) |
| 5749 | goto fail; |
| 5750 | |
Yan, Zheng | a22285a | 2010-05-16 10:48:46 -0400 | [diff] [blame] | 5751 | /* |
Miao Xie | 7e6b646 | 2011-02-18 09:21:17 +0000 | [diff] [blame] | 5752 | * 2 items for inode and inode ref |
Yan, Zheng | a22285a | 2010-05-16 10:48:46 -0400 | [diff] [blame] | 5753 | * 2 items for dir items |
Miao Xie | 7e6b646 | 2011-02-18 09:21:17 +0000 | [diff] [blame] | 5754 | * 1 item for parent inode |
Yan, Zheng | a22285a | 2010-05-16 10:48:46 -0400 | [diff] [blame] | 5755 | */ |
Miao Xie | 7e6b646 | 2011-02-18 09:21:17 +0000 | [diff] [blame] | 5756 | trans = btrfs_start_transaction(root, 5); |
Yan, Zheng | a22285a | 2010-05-16 10:48:46 -0400 | [diff] [blame] | 5757 | if (IS_ERR(trans)) { |
| 5758 | err = PTR_ERR(trans); |
| 5759 | goto fail; |
| 5760 | } |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 5761 | |
Miao Xie | 3153495 | 2011-04-13 13:19:21 +0800 | [diff] [blame] | 5762 | btrfs_inc_nlink(inode); |
Josef Bacik | 0c4d2d9 | 2012-04-05 15:03:02 -0400 | [diff] [blame] | 5763 | inode_inc_iversion(inode); |
Miao Xie | 3153495 | 2011-04-13 13:19:21 +0800 | [diff] [blame] | 5764 | inode->i_ctime = CURRENT_TIME; |
Al Viro | 7de9c6ee | 2010-10-23 11:11:40 -0400 | [diff] [blame] | 5765 | ihold(inode); |
Josef Bacik | e997615 | 2012-10-11 15:53:56 -0400 | [diff] [blame] | 5766 | set_bit(BTRFS_INODE_COPY_EVERYTHING, &BTRFS_I(inode)->runtime_flags); |
Josef Bacik | aec7477 | 2008-07-24 12:12:38 -0400 | [diff] [blame] | 5767 | |
Josef Bacik | a1b075d | 2010-11-19 20:36:11 +0000 | [diff] [blame] | 5768 | err = btrfs_add_nondir(trans, dir, dentry, inode, 1, index); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 5769 | |
Yan, Zheng | a571952 | 2009-09-24 09:17:31 -0400 | [diff] [blame] | 5770 | if (err) { |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5771 | drop_inode = 1; |
Yan, Zheng | a571952 | 2009-09-24 09:17:31 -0400 | [diff] [blame] | 5772 | } else { |
Al Viro | 10d9f30 | 2011-07-16 23:09:10 -0400 | [diff] [blame] | 5773 | struct dentry *parent = dentry->d_parent; |
Yan, Zheng | a571952 | 2009-09-24 09:17:31 -0400 | [diff] [blame] | 5774 | err = btrfs_update_inode(trans, root, inode); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 5775 | if (err) |
| 5776 | goto fail; |
Al Viro | 08c422c | 2011-12-23 07:58:13 -0500 | [diff] [blame] | 5777 | d_instantiate(dentry, inode); |
Josef Bacik | 6a91221 | 2010-11-20 09:48:00 +0000 | [diff] [blame] | 5778 | btrfs_log_new_name(trans, inode, NULL, parent); |
Yan, Zheng | a571952 | 2009-09-24 09:17:31 -0400 | [diff] [blame] | 5779 | } |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5780 | |
Josef Bacik | 7ad85bb | 2012-01-12 19:10:12 -0500 | [diff] [blame] | 5781 | btrfs_end_transaction(trans, root); |
Chris Mason | 1832a6d | 2007-12-21 16:27:21 -0500 | [diff] [blame] | 5782 | fail: |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5783 | if (drop_inode) { |
| 5784 | inode_dec_link_count(inode); |
| 5785 | iput(inode); |
| 5786 | } |
Liu Bo | b53d3f5 | 2012-11-14 14:34:34 +0000 | [diff] [blame] | 5787 | btrfs_btree_balance_dirty(root); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5788 | return err; |
| 5789 | } |
| 5790 | |
Al Viro | 18bb1db | 2011-07-26 01:41:39 -0400 | [diff] [blame] | 5791 | 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] | 5792 | { |
Chris Mason | b9d8666 | 2008-05-02 16:13:49 -0400 | [diff] [blame] | 5793 | struct inode *inode = NULL; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5794 | struct btrfs_trans_handle *trans; |
| 5795 | struct btrfs_root *root = BTRFS_I(dir)->root; |
| 5796 | int err = 0; |
| 5797 | int drop_on_err = 0; |
Chris Mason | b9d8666 | 2008-05-02 16:13:49 -0400 | [diff] [blame] | 5798 | u64 objectid = 0; |
Chris Mason | 00e4e6b | 2008-08-05 11:18:09 -0400 | [diff] [blame] | 5799 | u64 index = 0; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5800 | |
Josef Bacik | 9ed74f2 | 2009-09-11 16:12:44 -0400 | [diff] [blame] | 5801 | /* |
| 5802 | * 2 items for inode and ref |
| 5803 | * 2 items for dir items |
| 5804 | * 1 for xattr if selinux is on |
| 5805 | */ |
Yan, Zheng | a22285a | 2010-05-16 10:48:46 -0400 | [diff] [blame] | 5806 | trans = btrfs_start_transaction(root, 5); |
| 5807 | if (IS_ERR(trans)) |
| 5808 | return PTR_ERR(trans); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5809 | |
Li Zefan | 581bb05 | 2011-04-20 10:06:11 +0800 | [diff] [blame] | 5810 | err = btrfs_find_free_ino(root, &objectid); |
| 5811 | if (err) |
| 5812 | goto out_fail; |
| 5813 | |
Josef Bacik | aec7477 | 2008-07-24 12:12:38 -0400 | [diff] [blame] | 5814 | inode = btrfs_new_inode(trans, root, dir, dentry->d_name.name, |
Li Zefan | 33345d01 | 2011-04-20 10:31:50 +0800 | [diff] [blame] | 5815 | dentry->d_name.len, btrfs_ino(dir), objectid, |
Josef Bacik | d82a6f1d | 2011-05-11 15:26:06 -0400 | [diff] [blame] | 5816 | S_IFDIR | mode, &index); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5817 | if (IS_ERR(inode)) { |
| 5818 | err = PTR_ERR(inode); |
| 5819 | goto out_fail; |
| 5820 | } |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 5821 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5822 | drop_on_err = 1; |
Josef Bacik | 33268ea | 2008-07-24 12:16:36 -0400 | [diff] [blame] | 5823 | |
Eric Paris | 2a7dba3 | 2011-02-01 11:05:39 -0500 | [diff] [blame] | 5824 | err = btrfs_init_inode_security(trans, inode, dir, &dentry->d_name); |
Josef Bacik | 33268ea | 2008-07-24 12:16:36 -0400 | [diff] [blame] | 5825 | if (err) |
| 5826 | goto out_fail; |
| 5827 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5828 | inode->i_op = &btrfs_dir_inode_operations; |
| 5829 | inode->i_fop = &btrfs_dir_file_operations; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5830 | |
Chris Mason | dbe674a | 2008-07-17 12:54:05 -0400 | [diff] [blame] | 5831 | btrfs_i_size_write(inode, 0); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5832 | err = btrfs_update_inode(trans, root, inode); |
| 5833 | if (err) |
| 5834 | goto out_fail; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 5835 | |
Josef Bacik | a1b075d | 2010-11-19 20:36:11 +0000 | [diff] [blame] | 5836 | err = btrfs_add_link(trans, dir, inode, dentry->d_name.name, |
| 5837 | dentry->d_name.len, 0, index); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5838 | if (err) |
| 5839 | goto out_fail; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 5840 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5841 | d_instantiate(dentry, inode); |
| 5842 | drop_on_err = 0; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5843 | |
| 5844 | out_fail: |
Josef Bacik | 7ad85bb | 2012-01-12 19:10:12 -0500 | [diff] [blame] | 5845 | btrfs_end_transaction(trans, root); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5846 | if (drop_on_err) |
| 5847 | iput(inode); |
Liu Bo | b53d3f5 | 2012-11-14 14:34:34 +0000 | [diff] [blame] | 5848 | btrfs_btree_balance_dirty(root); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5849 | return err; |
| 5850 | } |
| 5851 | |
Chris Mason | d352ac6 | 2008-09-29 15:18:18 -0400 | [diff] [blame] | 5852 | /* helper for btfs_get_extent. Given an existing extent in the tree, |
| 5853 | * and an extent that you want to insert, deal with overlap and insert |
| 5854 | * the new extent into the tree. |
| 5855 | */ |
Chris Mason | 3b95151 | 2008-04-17 11:29:12 -0400 | [diff] [blame] | 5856 | static int merge_extent_mapping(struct extent_map_tree *em_tree, |
| 5857 | struct extent_map *existing, |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 5858 | struct extent_map *em, |
| 5859 | u64 map_start, u64 map_len) |
Chris Mason | 3b95151 | 2008-04-17 11:29:12 -0400 | [diff] [blame] | 5860 | { |
| 5861 | u64 start_diff; |
Chris Mason | 3b95151 | 2008-04-17 11:29:12 -0400 | [diff] [blame] | 5862 | |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 5863 | BUG_ON(map_start < em->start || map_start >= extent_map_end(em)); |
| 5864 | start_diff = map_start - em->start; |
| 5865 | em->start = map_start; |
| 5866 | em->len = map_len; |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 5867 | if (em->block_start < EXTENT_MAP_LAST_BYTE && |
| 5868 | !test_bit(EXTENT_FLAG_COMPRESSED, &em->flags)) { |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 5869 | em->block_start += start_diff; |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 5870 | em->block_len -= start_diff; |
| 5871 | } |
Josef Bacik | 09a2a8f9 | 2013-04-05 16:51:15 -0400 | [diff] [blame] | 5872 | return add_extent_mapping(em_tree, em, 0); |
Chris Mason | 3b95151 | 2008-04-17 11:29:12 -0400 | [diff] [blame] | 5873 | } |
| 5874 | |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 5875 | static noinline int uncompress_inline(struct btrfs_path *path, |
| 5876 | struct inode *inode, struct page *page, |
| 5877 | size_t pg_offset, u64 extent_offset, |
| 5878 | struct btrfs_file_extent_item *item) |
| 5879 | { |
| 5880 | int ret; |
| 5881 | struct extent_buffer *leaf = path->nodes[0]; |
| 5882 | char *tmp; |
| 5883 | size_t max_size; |
| 5884 | unsigned long inline_size; |
| 5885 | unsigned long ptr; |
Li Zefan | 261507a0 | 2010-12-17 14:21:50 +0800 | [diff] [blame] | 5886 | int compress_type; |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 5887 | |
| 5888 | WARN_ON(pg_offset != 0); |
Li Zefan | 261507a0 | 2010-12-17 14:21:50 +0800 | [diff] [blame] | 5889 | compress_type = btrfs_file_extent_compression(leaf, item); |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 5890 | max_size = btrfs_file_extent_ram_bytes(leaf, item); |
| 5891 | inline_size = btrfs_file_extent_inline_item_len(leaf, |
| 5892 | btrfs_item_nr(leaf, path->slots[0])); |
| 5893 | tmp = kmalloc(inline_size, GFP_NOFS); |
Tsutomu Itoh | 8d41371 | 2011-04-25 19:43:52 -0400 | [diff] [blame] | 5894 | if (!tmp) |
| 5895 | return -ENOMEM; |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 5896 | ptr = btrfs_file_extent_inline_start(item); |
| 5897 | |
| 5898 | read_extent_buffer(leaf, tmp, ptr, inline_size); |
| 5899 | |
Chris Mason | 5b050f0 | 2008-11-11 09:34:41 -0500 | [diff] [blame] | 5900 | max_size = min_t(unsigned long, PAGE_CACHE_SIZE, max_size); |
Li Zefan | 261507a0 | 2010-12-17 14:21:50 +0800 | [diff] [blame] | 5901 | ret = btrfs_decompress(compress_type, tmp, page, |
| 5902 | extent_offset, inline_size, max_size); |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 5903 | if (ret) { |
Cong Wang | 7ac687d | 2011-11-25 23:14:28 +0800 | [diff] [blame] | 5904 | char *kaddr = kmap_atomic(page); |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 5905 | unsigned long copy_size = min_t(u64, |
| 5906 | PAGE_CACHE_SIZE - pg_offset, |
| 5907 | max_size - extent_offset); |
| 5908 | memset(kaddr + pg_offset, 0, copy_size); |
Cong Wang | 7ac687d | 2011-11-25 23:14:28 +0800 | [diff] [blame] | 5909 | kunmap_atomic(kaddr); |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 5910 | } |
| 5911 | kfree(tmp); |
| 5912 | return 0; |
| 5913 | } |
| 5914 | |
Chris Mason | d352ac6 | 2008-09-29 15:18:18 -0400 | [diff] [blame] | 5915 | /* |
| 5916 | * 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] | 5917 | * the ugly parts come from merging extents from the disk with the in-ram |
| 5918 | * representation. This gets more complex because of the data=ordered code, |
Chris Mason | d352ac6 | 2008-09-29 15:18:18 -0400 | [diff] [blame] | 5919 | * where the in-ram extents might be locked pending data=ordered completion. |
| 5920 | * |
| 5921 | * This also copies inline extents directly into the page. |
| 5922 | */ |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 5923 | |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 5924 | struct extent_map *btrfs_get_extent(struct inode *inode, struct page *page, |
Chris Mason | 70dec80 | 2008-01-29 09:59:12 -0500 | [diff] [blame] | 5925 | size_t pg_offset, u64 start, u64 len, |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 5926 | int create) |
| 5927 | { |
| 5928 | int ret; |
| 5929 | int err = 0; |
Chris Mason | db94535 | 2007-10-15 16:15:53 -0400 | [diff] [blame] | 5930 | u64 bytenr; |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 5931 | u64 extent_start = 0; |
| 5932 | u64 extent_end = 0; |
Li Zefan | 33345d01 | 2011-04-20 10:31:50 +0800 | [diff] [blame] | 5933 | u64 objectid = btrfs_ino(inode); |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 5934 | u32 found_type; |
Chris Mason | f421950 | 2008-07-22 11:18:09 -0400 | [diff] [blame] | 5935 | struct btrfs_path *path = NULL; |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 5936 | struct btrfs_root *root = BTRFS_I(inode)->root; |
| 5937 | struct btrfs_file_extent_item *item; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 5938 | struct extent_buffer *leaf; |
| 5939 | struct btrfs_key found_key; |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 5940 | struct extent_map *em = NULL; |
| 5941 | struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree; |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 5942 | struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree; |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 5943 | struct btrfs_trans_handle *trans = NULL; |
Li Zefan | 261507a0 | 2010-12-17 14:21:50 +0800 | [diff] [blame] | 5944 | int compress_type; |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 5945 | |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 5946 | again: |
Chris Mason | 890871b | 2009-09-02 16:24:52 -0400 | [diff] [blame] | 5947 | read_lock(&em_tree->lock); |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 5948 | em = lookup_extent_mapping(em_tree, start, len); |
Chris Mason | a061fc8 | 2008-05-07 11:43:44 -0400 | [diff] [blame] | 5949 | if (em) |
| 5950 | em->bdev = root->fs_info->fs_devices->latest_bdev; |
Chris Mason | 890871b | 2009-09-02 16:24:52 -0400 | [diff] [blame] | 5951 | read_unlock(&em_tree->lock); |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 5952 | |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 5953 | if (em) { |
Chris Mason | e1c4b74 | 2008-04-22 13:26:46 -0400 | [diff] [blame] | 5954 | if (em->start > start || em->start + em->len <= start) |
| 5955 | free_extent_map(em); |
| 5956 | else if (em->block_start == EXTENT_MAP_INLINE && page) |
Chris Mason | 70dec80 | 2008-01-29 09:59:12 -0500 | [diff] [blame] | 5957 | free_extent_map(em); |
| 5958 | else |
| 5959 | goto out; |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 5960 | } |
David Sterba | 172ddd6 | 2011-04-21 00:48:27 +0200 | [diff] [blame] | 5961 | em = alloc_extent_map(); |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 5962 | if (!em) { |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 5963 | err = -ENOMEM; |
| 5964 | goto out; |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 5965 | } |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 5966 | em->bdev = root->fs_info->fs_devices->latest_bdev; |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 5967 | em->start = EXTENT_MAP_HOLE; |
Chris Mason | 445a694 | 2008-11-10 11:53:33 -0500 | [diff] [blame] | 5968 | em->orig_start = EXTENT_MAP_HOLE; |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 5969 | em->len = (u64)-1; |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 5970 | em->block_len = (u64)-1; |
Chris Mason | f421950 | 2008-07-22 11:18:09 -0400 | [diff] [blame] | 5971 | |
| 5972 | if (!path) { |
| 5973 | path = btrfs_alloc_path(); |
Josef Bacik | 026fd31 | 2011-05-13 10:32:11 -0400 | [diff] [blame] | 5974 | if (!path) { |
| 5975 | err = -ENOMEM; |
| 5976 | goto out; |
| 5977 | } |
| 5978 | /* |
| 5979 | * Chances are we'll be called again, so go ahead and do |
| 5980 | * readahead |
| 5981 | */ |
| 5982 | path->reada = 1; |
Chris Mason | f421950 | 2008-07-22 11:18:09 -0400 | [diff] [blame] | 5983 | } |
| 5984 | |
Chris Mason | 179e29e | 2007-11-01 11:28:41 -0400 | [diff] [blame] | 5985 | ret = btrfs_lookup_file_extent(trans, root, path, |
| 5986 | objectid, start, trans != NULL); |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 5987 | if (ret < 0) { |
| 5988 | err = ret; |
| 5989 | goto out; |
| 5990 | } |
| 5991 | |
| 5992 | if (ret != 0) { |
| 5993 | if (path->slots[0] == 0) |
| 5994 | goto not_found; |
| 5995 | path->slots[0]--; |
| 5996 | } |
| 5997 | |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 5998 | leaf = path->nodes[0]; |
| 5999 | item = btrfs_item_ptr(leaf, path->slots[0], |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 6000 | struct btrfs_file_extent_item); |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 6001 | /* are we inside the extent that was found? */ |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 6002 | btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]); |
| 6003 | found_type = btrfs_key_type(&found_key); |
| 6004 | if (found_key.objectid != objectid || |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 6005 | found_type != BTRFS_EXTENT_DATA_KEY) { |
| 6006 | goto not_found; |
| 6007 | } |
| 6008 | |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 6009 | found_type = btrfs_file_extent_type(leaf, item); |
| 6010 | extent_start = found_key.offset; |
Li Zefan | 261507a0 | 2010-12-17 14:21:50 +0800 | [diff] [blame] | 6011 | compress_type = btrfs_file_extent_compression(leaf, item); |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 6012 | if (found_type == BTRFS_FILE_EXTENT_REG || |
| 6013 | found_type == BTRFS_FILE_EXTENT_PREALLOC) { |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 6014 | extent_end = extent_start + |
Chris Mason | db94535 | 2007-10-15 16:15:53 -0400 | [diff] [blame] | 6015 | btrfs_file_extent_num_bytes(leaf, item); |
Yan Zheng | 9036c10 | 2008-10-30 14:19:41 -0400 | [diff] [blame] | 6016 | } else if (found_type == BTRFS_FILE_EXTENT_INLINE) { |
| 6017 | size_t size; |
| 6018 | size = btrfs_file_extent_inline_len(leaf, item); |
Qu Wenruo | fda2832 | 2013-02-26 08:10:22 +0000 | [diff] [blame] | 6019 | extent_end = ALIGN(extent_start + size, root->sectorsize); |
Yan Zheng | 9036c10 | 2008-10-30 14:19:41 -0400 | [diff] [blame] | 6020 | } |
| 6021 | |
| 6022 | if (start >= extent_end) { |
| 6023 | path->slots[0]++; |
| 6024 | if (path->slots[0] >= btrfs_header_nritems(leaf)) { |
| 6025 | ret = btrfs_next_leaf(root, path); |
| 6026 | if (ret < 0) { |
| 6027 | err = ret; |
| 6028 | goto out; |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 6029 | } |
Yan Zheng | 9036c10 | 2008-10-30 14:19:41 -0400 | [diff] [blame] | 6030 | if (ret > 0) |
| 6031 | goto not_found; |
| 6032 | leaf = path->nodes[0]; |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 6033 | } |
Yan Zheng | 9036c10 | 2008-10-30 14:19:41 -0400 | [diff] [blame] | 6034 | btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]); |
| 6035 | if (found_key.objectid != objectid || |
| 6036 | found_key.type != BTRFS_EXTENT_DATA_KEY) |
| 6037 | goto not_found; |
| 6038 | if (start + len <= found_key.offset) |
| 6039 | goto not_found; |
| 6040 | em->start = start; |
Josef Bacik | 70c8a91 | 2012-10-11 16:54:30 -0400 | [diff] [blame] | 6041 | em->orig_start = start; |
Yan Zheng | 9036c10 | 2008-10-30 14:19:41 -0400 | [diff] [blame] | 6042 | em->len = found_key.offset - start; |
| 6043 | goto not_found_em; |
| 6044 | } |
| 6045 | |
Josef Bacik | cc95bef | 2013-04-04 14:31:27 -0400 | [diff] [blame] | 6046 | em->ram_bytes = btrfs_file_extent_ram_bytes(leaf, item); |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 6047 | if (found_type == BTRFS_FILE_EXTENT_REG || |
| 6048 | found_type == BTRFS_FILE_EXTENT_PREALLOC) { |
Yan Zheng | 9036c10 | 2008-10-30 14:19:41 -0400 | [diff] [blame] | 6049 | em->start = extent_start; |
| 6050 | em->len = extent_end - extent_start; |
Yan Zheng | ff5b7ee | 2008-11-10 07:34:43 -0500 | [diff] [blame] | 6051 | em->orig_start = extent_start - |
| 6052 | btrfs_file_extent_offset(leaf, item); |
Josef Bacik | b493968 | 2012-12-03 10:31:19 -0500 | [diff] [blame] | 6053 | em->orig_block_len = btrfs_file_extent_disk_num_bytes(leaf, |
| 6054 | item); |
Chris Mason | db94535 | 2007-10-15 16:15:53 -0400 | [diff] [blame] | 6055 | bytenr = btrfs_file_extent_disk_bytenr(leaf, item); |
| 6056 | if (bytenr == 0) { |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 6057 | em->block_start = EXTENT_MAP_HOLE; |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 6058 | goto insert; |
| 6059 | } |
Li Zefan | 261507a0 | 2010-12-17 14:21:50 +0800 | [diff] [blame] | 6060 | if (compress_type != BTRFS_COMPRESS_NONE) { |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 6061 | set_bit(EXTENT_FLAG_COMPRESSED, &em->flags); |
Li Zefan | 261507a0 | 2010-12-17 14:21:50 +0800 | [diff] [blame] | 6062 | em->compress_type = compress_type; |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 6063 | em->block_start = bytenr; |
Josef Bacik | b493968 | 2012-12-03 10:31:19 -0500 | [diff] [blame] | 6064 | em->block_len = em->orig_block_len; |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 6065 | } else { |
| 6066 | bytenr += btrfs_file_extent_offset(leaf, item); |
| 6067 | em->block_start = bytenr; |
| 6068 | em->block_len = em->len; |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 6069 | if (found_type == BTRFS_FILE_EXTENT_PREALLOC) |
| 6070 | set_bit(EXTENT_FLAG_PREALLOC, &em->flags); |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 6071 | } |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 6072 | goto insert; |
| 6073 | } else if (found_type == BTRFS_FILE_EXTENT_INLINE) { |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 6074 | unsigned long ptr; |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 6075 | char *map; |
Chris Mason | 3326d1b | 2007-10-15 16:18:25 -0400 | [diff] [blame] | 6076 | size_t size; |
| 6077 | size_t extent_offset; |
| 6078 | size_t copy_size; |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 6079 | |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 6080 | em->block_start = EXTENT_MAP_INLINE; |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 6081 | if (!page || create) { |
Yan | 689f934 | 2007-10-29 11:41:07 -0400 | [diff] [blame] | 6082 | em->start = extent_start; |
Yan Zheng | 9036c10 | 2008-10-30 14:19:41 -0400 | [diff] [blame] | 6083 | em->len = extent_end - extent_start; |
Yan | 689f934 | 2007-10-29 11:41:07 -0400 | [diff] [blame] | 6084 | goto out; |
| 6085 | } |
| 6086 | |
Yan Zheng | 9036c10 | 2008-10-30 14:19:41 -0400 | [diff] [blame] | 6087 | size = btrfs_file_extent_inline_len(leaf, item); |
| 6088 | extent_offset = page_offset(page) + pg_offset - extent_start; |
Chris Mason | 70dec80 | 2008-01-29 09:59:12 -0500 | [diff] [blame] | 6089 | copy_size = min_t(u64, PAGE_CACHE_SIZE - pg_offset, |
Yan | 689f934 | 2007-10-29 11:41:07 -0400 | [diff] [blame] | 6090 | size - extent_offset); |
Chris Mason | 3326d1b | 2007-10-15 16:18:25 -0400 | [diff] [blame] | 6091 | em->start = extent_start + extent_offset; |
Qu Wenruo | fda2832 | 2013-02-26 08:10:22 +0000 | [diff] [blame] | 6092 | em->len = ALIGN(copy_size, root->sectorsize); |
Josef Bacik | b493968 | 2012-12-03 10:31:19 -0500 | [diff] [blame] | 6093 | em->orig_block_len = em->len; |
Josef Bacik | 70c8a91 | 2012-10-11 16:54:30 -0400 | [diff] [blame] | 6094 | em->orig_start = em->start; |
Li Zefan | 261507a0 | 2010-12-17 14:21:50 +0800 | [diff] [blame] | 6095 | if (compress_type) { |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 6096 | set_bit(EXTENT_FLAG_COMPRESSED, &em->flags); |
Li Zefan | 261507a0 | 2010-12-17 14:21:50 +0800 | [diff] [blame] | 6097 | em->compress_type = compress_type; |
| 6098 | } |
Yan | 689f934 | 2007-10-29 11:41:07 -0400 | [diff] [blame] | 6099 | ptr = btrfs_file_extent_inline_start(item) + extent_offset; |
Chris Mason | 179e29e | 2007-11-01 11:28:41 -0400 | [diff] [blame] | 6100 | if (create == 0 && !PageUptodate(page)) { |
Li Zefan | 261507a0 | 2010-12-17 14:21:50 +0800 | [diff] [blame] | 6101 | if (btrfs_file_extent_compression(leaf, item) != |
| 6102 | BTRFS_COMPRESS_NONE) { |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 6103 | ret = uncompress_inline(path, inode, page, |
| 6104 | pg_offset, |
| 6105 | extent_offset, item); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 6106 | BUG_ON(ret); /* -ENOMEM */ |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 6107 | } else { |
| 6108 | map = kmap(page); |
| 6109 | read_extent_buffer(leaf, map + pg_offset, ptr, |
| 6110 | copy_size); |
Chris Mason | 93c82d5 | 2009-09-11 12:36:29 -0400 | [diff] [blame] | 6111 | if (pg_offset + copy_size < PAGE_CACHE_SIZE) { |
| 6112 | memset(map + pg_offset + copy_size, 0, |
| 6113 | PAGE_CACHE_SIZE - pg_offset - |
| 6114 | copy_size); |
| 6115 | } |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 6116 | kunmap(page); |
| 6117 | } |
Chris Mason | 179e29e | 2007-11-01 11:28:41 -0400 | [diff] [blame] | 6118 | flush_dcache_page(page); |
| 6119 | } else if (create && PageUptodate(page)) { |
Jan Schmidt | 6bf7e08 | 2011-12-01 14:35:19 +0100 | [diff] [blame] | 6120 | BUG(); |
Chris Mason | 179e29e | 2007-11-01 11:28:41 -0400 | [diff] [blame] | 6121 | if (!trans) { |
| 6122 | kunmap(page); |
| 6123 | free_extent_map(em); |
| 6124 | em = NULL; |
Chris Mason | ff5714c | 2011-05-28 07:00:39 -0400 | [diff] [blame] | 6125 | |
David Sterba | b3b4aa7 | 2011-04-21 01:20:15 +0200 | [diff] [blame] | 6126 | btrfs_release_path(path); |
Josef Bacik | 7a7eaa4 | 2011-04-13 12:54:33 -0400 | [diff] [blame] | 6127 | trans = btrfs_join_transaction(root); |
Chris Mason | ff5714c | 2011-05-28 07:00:39 -0400 | [diff] [blame] | 6128 | |
Tsutomu Itoh | 3612b49 | 2011-01-25 02:51:38 +0000 | [diff] [blame] | 6129 | if (IS_ERR(trans)) |
| 6130 | return ERR_CAST(trans); |
Chris Mason | 179e29e | 2007-11-01 11:28:41 -0400 | [diff] [blame] | 6131 | goto again; |
| 6132 | } |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 6133 | map = kmap(page); |
Chris Mason | 70dec80 | 2008-01-29 09:59:12 -0500 | [diff] [blame] | 6134 | write_extent_buffer(leaf, map + pg_offset, ptr, |
Chris Mason | 179e29e | 2007-11-01 11:28:41 -0400 | [diff] [blame] | 6135 | copy_size); |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 6136 | kunmap(page); |
Chris Mason | 179e29e | 2007-11-01 11:28:41 -0400 | [diff] [blame] | 6137 | btrfs_mark_buffer_dirty(leaf); |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 6138 | } |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 6139 | set_extent_uptodate(io_tree, em->start, |
Arne Jansen | 507903b | 2011-04-06 10:02:20 +0000 | [diff] [blame] | 6140 | extent_map_end(em) - 1, NULL, GFP_NOFS); |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 6141 | goto insert; |
| 6142 | } else { |
Julia Lawall | 31b1a2b | 2012-11-03 10:58:34 +0000 | [diff] [blame] | 6143 | WARN(1, KERN_ERR "btrfs unknown found_type %d\n", found_type); |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 6144 | } |
| 6145 | not_found: |
| 6146 | em->start = start; |
Josef Bacik | 70c8a91 | 2012-10-11 16:54:30 -0400 | [diff] [blame] | 6147 | em->orig_start = start; |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 6148 | em->len = len; |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 6149 | not_found_em: |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 6150 | em->block_start = EXTENT_MAP_HOLE; |
Yan Zheng | 9036c10 | 2008-10-30 14:19:41 -0400 | [diff] [blame] | 6151 | set_bit(EXTENT_FLAG_VACANCY, &em->flags); |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 6152 | insert: |
David Sterba | b3b4aa7 | 2011-04-21 01:20:15 +0200 | [diff] [blame] | 6153 | btrfs_release_path(path); |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 6154 | if (em->start > start || extent_map_end(em) <= start) { |
Simon Kirby | c2cf52e | 2013-03-19 22:41:23 +0000 | [diff] [blame] | 6155 | btrfs_err(root->fs_info, "bad extent! em: [%llu %llu] passed [%llu %llu]", |
| 6156 | (unsigned long long)em->start, |
| 6157 | (unsigned long long)em->len, |
| 6158 | (unsigned long long)start, |
| 6159 | (unsigned long long)len); |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 6160 | err = -EIO; |
| 6161 | goto out; |
| 6162 | } |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 6163 | |
| 6164 | err = 0; |
Chris Mason | 890871b | 2009-09-02 16:24:52 -0400 | [diff] [blame] | 6165 | write_lock(&em_tree->lock); |
Josef Bacik | 09a2a8f9 | 2013-04-05 16:51:15 -0400 | [diff] [blame] | 6166 | ret = add_extent_mapping(em_tree, em, 0); |
Chris Mason | 3b95151 | 2008-04-17 11:29:12 -0400 | [diff] [blame] | 6167 | /* it is possible that someone inserted the extent into the tree |
| 6168 | * while we had the lock dropped. It is also possible that |
| 6169 | * an overlapping map exists in the tree |
| 6170 | */ |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 6171 | if (ret == -EEXIST) { |
Chris Mason | 3b95151 | 2008-04-17 11:29:12 -0400 | [diff] [blame] | 6172 | struct extent_map *existing; |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 6173 | |
| 6174 | ret = 0; |
| 6175 | |
Chris Mason | 3b95151 | 2008-04-17 11:29:12 -0400 | [diff] [blame] | 6176 | existing = lookup_extent_mapping(em_tree, start, len); |
Chris Mason | e1c4b74 | 2008-04-22 13:26:46 -0400 | [diff] [blame] | 6177 | if (existing && (existing->start > start || |
| 6178 | existing->start + existing->len <= start)) { |
| 6179 | free_extent_map(existing); |
| 6180 | existing = NULL; |
| 6181 | } |
Chris Mason | 3b95151 | 2008-04-17 11:29:12 -0400 | [diff] [blame] | 6182 | if (!existing) { |
| 6183 | existing = lookup_extent_mapping(em_tree, em->start, |
| 6184 | em->len); |
| 6185 | if (existing) { |
| 6186 | err = merge_extent_mapping(em_tree, existing, |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 6187 | em, start, |
| 6188 | root->sectorsize); |
Chris Mason | 3b95151 | 2008-04-17 11:29:12 -0400 | [diff] [blame] | 6189 | free_extent_map(existing); |
| 6190 | if (err) { |
| 6191 | free_extent_map(em); |
| 6192 | em = NULL; |
| 6193 | } |
| 6194 | } else { |
| 6195 | err = -EIO; |
Chris Mason | 3b95151 | 2008-04-17 11:29:12 -0400 | [diff] [blame] | 6196 | free_extent_map(em); |
| 6197 | em = NULL; |
| 6198 | } |
| 6199 | } else { |
| 6200 | free_extent_map(em); |
| 6201 | em = existing; |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 6202 | err = 0; |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 6203 | } |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 6204 | } |
Chris Mason | 890871b | 2009-09-02 16:24:52 -0400 | [diff] [blame] | 6205 | write_unlock(&em_tree->lock); |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 6206 | out: |
liubo | 1abe9b8 | 2011-03-24 11:18:59 +0000 | [diff] [blame] | 6207 | |
Tsutomu Itoh | f0bd95e | 2012-10-01 03:08:37 -0600 | [diff] [blame] | 6208 | if (em) |
| 6209 | trace_btrfs_get_extent(root, em); |
liubo | 1abe9b8 | 2011-03-24 11:18:59 +0000 | [diff] [blame] | 6210 | |
Chris Mason | f421950 | 2008-07-22 11:18:09 -0400 | [diff] [blame] | 6211 | if (path) |
| 6212 | btrfs_free_path(path); |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 6213 | if (trans) { |
| 6214 | ret = btrfs_end_transaction(trans, root); |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 6215 | if (!err) |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 6216 | err = ret; |
| 6217 | } |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 6218 | if (err) { |
| 6219 | free_extent_map(em); |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 6220 | return ERR_PTR(err); |
| 6221 | } |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 6222 | BUG_ON(!em); /* Error is always set */ |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 6223 | return em; |
| 6224 | } |
| 6225 | |
Chris Mason | ec29ed5 | 2011-02-23 16:23:20 -0500 | [diff] [blame] | 6226 | struct extent_map *btrfs_get_extent_fiemap(struct inode *inode, struct page *page, |
| 6227 | size_t pg_offset, u64 start, u64 len, |
| 6228 | int create) |
| 6229 | { |
| 6230 | struct extent_map *em; |
| 6231 | struct extent_map *hole_em = NULL; |
| 6232 | u64 range_start = start; |
| 6233 | u64 end; |
| 6234 | u64 found; |
| 6235 | u64 found_end; |
| 6236 | int err = 0; |
| 6237 | |
| 6238 | em = btrfs_get_extent(inode, page, pg_offset, start, len, create); |
| 6239 | if (IS_ERR(em)) |
| 6240 | return em; |
| 6241 | if (em) { |
| 6242 | /* |
Liu Bo | f9e4fb5 | 2013-01-07 10:10:12 +0000 | [diff] [blame] | 6243 | * if our em maps to |
| 6244 | * - a hole or |
| 6245 | * - a pre-alloc extent, |
| 6246 | * there might actually be delalloc bytes behind it. |
Chris Mason | ec29ed5 | 2011-02-23 16:23:20 -0500 | [diff] [blame] | 6247 | */ |
Liu Bo | f9e4fb5 | 2013-01-07 10:10:12 +0000 | [diff] [blame] | 6248 | if (em->block_start != EXTENT_MAP_HOLE && |
| 6249 | !test_bit(EXTENT_FLAG_PREALLOC, &em->flags)) |
Chris Mason | ec29ed5 | 2011-02-23 16:23:20 -0500 | [diff] [blame] | 6250 | return em; |
| 6251 | else |
| 6252 | hole_em = em; |
| 6253 | } |
| 6254 | |
| 6255 | /* check to see if we've wrapped (len == -1 or similar) */ |
| 6256 | end = start + len; |
| 6257 | if (end < start) |
| 6258 | end = (u64)-1; |
| 6259 | else |
| 6260 | end -= 1; |
| 6261 | |
| 6262 | em = NULL; |
| 6263 | |
| 6264 | /* ok, we didn't find anything, lets look for delalloc */ |
| 6265 | found = count_range_bits(&BTRFS_I(inode)->io_tree, &range_start, |
| 6266 | end, len, EXTENT_DELALLOC, 1); |
| 6267 | found_end = range_start + found; |
| 6268 | if (found_end < range_start) |
| 6269 | found_end = (u64)-1; |
| 6270 | |
| 6271 | /* |
| 6272 | * we didn't find anything useful, return |
| 6273 | * the original results from get_extent() |
| 6274 | */ |
| 6275 | if (range_start > end || found_end <= start) { |
| 6276 | em = hole_em; |
| 6277 | hole_em = NULL; |
| 6278 | goto out; |
| 6279 | } |
| 6280 | |
| 6281 | /* adjust the range_start to make sure it doesn't |
| 6282 | * go backwards from the start they passed in |
| 6283 | */ |
| 6284 | range_start = max(start,range_start); |
| 6285 | found = found_end - range_start; |
| 6286 | |
| 6287 | if (found > 0) { |
| 6288 | u64 hole_start = start; |
| 6289 | u64 hole_len = len; |
| 6290 | |
David Sterba | 172ddd6 | 2011-04-21 00:48:27 +0200 | [diff] [blame] | 6291 | em = alloc_extent_map(); |
Chris Mason | ec29ed5 | 2011-02-23 16:23:20 -0500 | [diff] [blame] | 6292 | if (!em) { |
| 6293 | err = -ENOMEM; |
| 6294 | goto out; |
| 6295 | } |
| 6296 | /* |
| 6297 | * when btrfs_get_extent can't find anything it |
| 6298 | * returns one huge hole |
| 6299 | * |
| 6300 | * make sure what it found really fits our range, and |
| 6301 | * adjust to make sure it is based on the start from |
| 6302 | * the caller |
| 6303 | */ |
| 6304 | if (hole_em) { |
| 6305 | u64 calc_end = extent_map_end(hole_em); |
| 6306 | |
| 6307 | if (calc_end <= start || (hole_em->start > end)) { |
| 6308 | free_extent_map(hole_em); |
| 6309 | hole_em = NULL; |
| 6310 | } else { |
| 6311 | hole_start = max(hole_em->start, start); |
| 6312 | hole_len = calc_end - hole_start; |
| 6313 | } |
| 6314 | } |
| 6315 | em->bdev = NULL; |
| 6316 | if (hole_em && range_start > hole_start) { |
| 6317 | /* our hole starts before our delalloc, so we |
| 6318 | * have to return just the parts of the hole |
| 6319 | * that go until the delalloc starts |
| 6320 | */ |
| 6321 | em->len = min(hole_len, |
| 6322 | range_start - hole_start); |
| 6323 | em->start = hole_start; |
| 6324 | em->orig_start = hole_start; |
| 6325 | /* |
| 6326 | * don't adjust block start at all, |
| 6327 | * it is fixed at EXTENT_MAP_HOLE |
| 6328 | */ |
| 6329 | em->block_start = hole_em->block_start; |
| 6330 | em->block_len = hole_len; |
Liu Bo | f9e4fb5 | 2013-01-07 10:10:12 +0000 | [diff] [blame] | 6331 | if (test_bit(EXTENT_FLAG_PREALLOC, &hole_em->flags)) |
| 6332 | set_bit(EXTENT_FLAG_PREALLOC, &em->flags); |
Chris Mason | ec29ed5 | 2011-02-23 16:23:20 -0500 | [diff] [blame] | 6333 | } else { |
| 6334 | em->start = range_start; |
| 6335 | em->len = found; |
| 6336 | em->orig_start = range_start; |
| 6337 | em->block_start = EXTENT_MAP_DELALLOC; |
| 6338 | em->block_len = found; |
| 6339 | } |
| 6340 | } else if (hole_em) { |
| 6341 | return hole_em; |
| 6342 | } |
| 6343 | out: |
| 6344 | |
| 6345 | free_extent_map(hole_em); |
| 6346 | if (err) { |
| 6347 | free_extent_map(em); |
| 6348 | return ERR_PTR(err); |
| 6349 | } |
| 6350 | return em; |
| 6351 | } |
| 6352 | |
Josef Bacik | 4b46fce | 2010-05-23 11:00:55 -0400 | [diff] [blame] | 6353 | static struct extent_map *btrfs_new_extent_direct(struct inode *inode, |
| 6354 | u64 start, u64 len) |
| 6355 | { |
| 6356 | struct btrfs_root *root = BTRFS_I(inode)->root; |
| 6357 | struct btrfs_trans_handle *trans; |
Josef Bacik | 70c8a91 | 2012-10-11 16:54:30 -0400 | [diff] [blame] | 6358 | struct extent_map *em; |
Josef Bacik | 4b46fce | 2010-05-23 11:00:55 -0400 | [diff] [blame] | 6359 | struct btrfs_key ins; |
| 6360 | u64 alloc_hint; |
| 6361 | int ret; |
Josef Bacik | 4b46fce | 2010-05-23 11:00:55 -0400 | [diff] [blame] | 6362 | |
Josef Bacik | 7a7eaa4 | 2011-04-13 12:54:33 -0400 | [diff] [blame] | 6363 | trans = btrfs_join_transaction(root); |
Tsutomu Itoh | 3612b49 | 2011-01-25 02:51:38 +0000 | [diff] [blame] | 6364 | if (IS_ERR(trans)) |
| 6365 | return ERR_CAST(trans); |
Josef Bacik | 4b46fce | 2010-05-23 11:00:55 -0400 | [diff] [blame] | 6366 | |
| 6367 | trans->block_rsv = &root->fs_info->delalloc_block_rsv; |
| 6368 | |
| 6369 | alloc_hint = get_extent_allocation_hint(inode, start, len); |
| 6370 | ret = btrfs_reserve_extent(trans, root, len, root->sectorsize, 0, |
Josef Bacik | 81c9ad2 | 2012-01-18 10:56:06 -0500 | [diff] [blame] | 6371 | alloc_hint, &ins, 1); |
Josef Bacik | 4b46fce | 2010-05-23 11:00:55 -0400 | [diff] [blame] | 6372 | if (ret) { |
| 6373 | em = ERR_PTR(ret); |
| 6374 | goto out; |
| 6375 | } |
| 6376 | |
Josef Bacik | 70c8a91 | 2012-10-11 16:54:30 -0400 | [diff] [blame] | 6377 | em = create_pinned_em(inode, start, ins.offset, start, ins.objectid, |
Josef Bacik | cc95bef | 2013-04-04 14:31:27 -0400 | [diff] [blame] | 6378 | ins.offset, ins.offset, ins.offset, 0); |
Josef Bacik | 70c8a91 | 2012-10-11 16:54:30 -0400 | [diff] [blame] | 6379 | if (IS_ERR(em)) |
| 6380 | goto out; |
Josef Bacik | 4b46fce | 2010-05-23 11:00:55 -0400 | [diff] [blame] | 6381 | |
| 6382 | ret = btrfs_add_ordered_extent_dio(inode, start, ins.objectid, |
| 6383 | ins.offset, ins.offset, 0); |
| 6384 | if (ret) { |
| 6385 | btrfs_free_reserved_extent(root, ins.objectid, ins.offset); |
| 6386 | em = ERR_PTR(ret); |
| 6387 | } |
| 6388 | out: |
| 6389 | btrfs_end_transaction(trans, root); |
| 6390 | return em; |
| 6391 | } |
| 6392 | |
Chris Mason | 46bfbb5 | 2010-05-26 11:04:10 -0400 | [diff] [blame] | 6393 | /* |
| 6394 | * returns 1 when the nocow is safe, < 1 on error, 0 if the |
| 6395 | * block must be cow'd |
| 6396 | */ |
Josef Bacik | 7ee9e44 | 2013-06-21 16:37:03 -0400 | [diff] [blame] | 6397 | noinline int can_nocow_extent(struct btrfs_trans_handle *trans, |
| 6398 | struct inode *inode, u64 offset, u64 *len, |
| 6399 | u64 *orig_start, u64 *orig_block_len, |
| 6400 | u64 *ram_bytes) |
Chris Mason | 46bfbb5 | 2010-05-26 11:04:10 -0400 | [diff] [blame] | 6401 | { |
| 6402 | struct btrfs_path *path; |
| 6403 | int ret; |
| 6404 | struct extent_buffer *leaf; |
| 6405 | struct btrfs_root *root = BTRFS_I(inode)->root; |
| 6406 | struct btrfs_file_extent_item *fi; |
| 6407 | struct btrfs_key key; |
| 6408 | u64 disk_bytenr; |
| 6409 | u64 backref_offset; |
| 6410 | u64 extent_end; |
| 6411 | u64 num_bytes; |
| 6412 | int slot; |
| 6413 | int found_type; |
Josef Bacik | 7ee9e44 | 2013-06-21 16:37:03 -0400 | [diff] [blame] | 6414 | bool nocow = (BTRFS_I(inode)->flags & BTRFS_INODE_NODATACOW); |
Chris Mason | 46bfbb5 | 2010-05-26 11:04:10 -0400 | [diff] [blame] | 6415 | path = btrfs_alloc_path(); |
| 6416 | if (!path) |
| 6417 | return -ENOMEM; |
| 6418 | |
Li Zefan | 33345d01 | 2011-04-20 10:31:50 +0800 | [diff] [blame] | 6419 | ret = btrfs_lookup_file_extent(trans, root, path, btrfs_ino(inode), |
Chris Mason | 46bfbb5 | 2010-05-26 11:04:10 -0400 | [diff] [blame] | 6420 | offset, 0); |
| 6421 | if (ret < 0) |
| 6422 | goto out; |
| 6423 | |
| 6424 | slot = path->slots[0]; |
| 6425 | if (ret == 1) { |
| 6426 | if (slot == 0) { |
| 6427 | /* can't find the item, must cow */ |
| 6428 | ret = 0; |
| 6429 | goto out; |
| 6430 | } |
| 6431 | slot--; |
| 6432 | } |
| 6433 | ret = 0; |
| 6434 | leaf = path->nodes[0]; |
| 6435 | btrfs_item_key_to_cpu(leaf, &key, slot); |
Li Zefan | 33345d01 | 2011-04-20 10:31:50 +0800 | [diff] [blame] | 6436 | if (key.objectid != btrfs_ino(inode) || |
Chris Mason | 46bfbb5 | 2010-05-26 11:04:10 -0400 | [diff] [blame] | 6437 | key.type != BTRFS_EXTENT_DATA_KEY) { |
| 6438 | /* not our file or wrong item type, must cow */ |
| 6439 | goto out; |
| 6440 | } |
| 6441 | |
| 6442 | if (key.offset > offset) { |
| 6443 | /* Wrong offset, must cow */ |
| 6444 | goto out; |
| 6445 | } |
| 6446 | |
| 6447 | fi = btrfs_item_ptr(leaf, slot, struct btrfs_file_extent_item); |
| 6448 | found_type = btrfs_file_extent_type(leaf, fi); |
| 6449 | if (found_type != BTRFS_FILE_EXTENT_REG && |
| 6450 | found_type != BTRFS_FILE_EXTENT_PREALLOC) { |
| 6451 | /* not a regular extent, must cow */ |
| 6452 | goto out; |
| 6453 | } |
Josef Bacik | 7ee9e44 | 2013-06-21 16:37:03 -0400 | [diff] [blame] | 6454 | |
| 6455 | if (!nocow && found_type == BTRFS_FILE_EXTENT_REG) |
| 6456 | goto out; |
| 6457 | |
Chris Mason | 46bfbb5 | 2010-05-26 11:04:10 -0400 | [diff] [blame] | 6458 | disk_bytenr = btrfs_file_extent_disk_bytenr(leaf, fi); |
Josef Bacik | 7ee9e44 | 2013-06-21 16:37:03 -0400 | [diff] [blame] | 6459 | if (disk_bytenr == 0) |
| 6460 | goto out; |
| 6461 | |
| 6462 | if (btrfs_file_extent_compression(leaf, fi) || |
| 6463 | btrfs_file_extent_encryption(leaf, fi) || |
| 6464 | btrfs_file_extent_other_encoding(leaf, fi)) |
| 6465 | goto out; |
| 6466 | |
Chris Mason | 46bfbb5 | 2010-05-26 11:04:10 -0400 | [diff] [blame] | 6467 | backref_offset = btrfs_file_extent_offset(leaf, fi); |
| 6468 | |
Josef Bacik | 7ee9e44 | 2013-06-21 16:37:03 -0400 | [diff] [blame] | 6469 | if (orig_start) { |
| 6470 | *orig_start = key.offset - backref_offset; |
| 6471 | *orig_block_len = btrfs_file_extent_disk_num_bytes(leaf, fi); |
| 6472 | *ram_bytes = btrfs_file_extent_ram_bytes(leaf, fi); |
| 6473 | } |
Josef Bacik | eb384b5 | 2013-04-24 16:32:55 -0400 | [diff] [blame] | 6474 | |
Chris Mason | 46bfbb5 | 2010-05-26 11:04:10 -0400 | [diff] [blame] | 6475 | extent_end = key.offset + btrfs_file_extent_num_bytes(leaf, fi); |
Chris Mason | 46bfbb5 | 2010-05-26 11:04:10 -0400 | [diff] [blame] | 6476 | |
| 6477 | if (btrfs_extent_readonly(root, disk_bytenr)) |
| 6478 | goto out; |
| 6479 | |
| 6480 | /* |
| 6481 | * look for other files referencing this extent, if we |
| 6482 | * find any we must cow |
| 6483 | */ |
Li Zefan | 33345d01 | 2011-04-20 10:31:50 +0800 | [diff] [blame] | 6484 | if (btrfs_cross_ref_exist(trans, root, btrfs_ino(inode), |
Chris Mason | 46bfbb5 | 2010-05-26 11:04:10 -0400 | [diff] [blame] | 6485 | key.offset - backref_offset, disk_bytenr)) |
| 6486 | goto out; |
| 6487 | |
| 6488 | /* |
| 6489 | * adjust disk_bytenr and num_bytes to cover just the bytes |
| 6490 | * in this extent we are about to write. If there |
| 6491 | * are any csums in that range we have to cow in order |
| 6492 | * to keep the csums correct |
| 6493 | */ |
| 6494 | disk_bytenr += backref_offset; |
| 6495 | disk_bytenr += offset - key.offset; |
Josef Bacik | eb384b5 | 2013-04-24 16:32:55 -0400 | [diff] [blame] | 6496 | num_bytes = min(offset + *len, extent_end) - offset; |
Chris Mason | 46bfbb5 | 2010-05-26 11:04:10 -0400 | [diff] [blame] | 6497 | if (csum_exist_in_range(root, disk_bytenr, num_bytes)) |
| 6498 | goto out; |
| 6499 | /* |
| 6500 | * all of the above have passed, it is safe to overwrite this extent |
| 6501 | * without cow |
| 6502 | */ |
Josef Bacik | eb384b5 | 2013-04-24 16:32:55 -0400 | [diff] [blame] | 6503 | *len = num_bytes; |
Chris Mason | 46bfbb5 | 2010-05-26 11:04:10 -0400 | [diff] [blame] | 6504 | ret = 1; |
| 6505 | out: |
| 6506 | btrfs_free_path(path); |
| 6507 | return ret; |
| 6508 | } |
| 6509 | |
Josef Bacik | eb838e7 | 2012-07-31 16:28:48 -0400 | [diff] [blame] | 6510 | static int lock_extent_direct(struct inode *inode, u64 lockstart, u64 lockend, |
| 6511 | struct extent_state **cached_state, int writing) |
| 6512 | { |
| 6513 | struct btrfs_ordered_extent *ordered; |
| 6514 | int ret = 0; |
| 6515 | |
| 6516 | while (1) { |
| 6517 | lock_extent_bits(&BTRFS_I(inode)->io_tree, lockstart, lockend, |
| 6518 | 0, cached_state); |
| 6519 | /* |
| 6520 | * We're concerned with the entire range that we're going to be |
| 6521 | * doing DIO to, so we need to make sure theres no ordered |
| 6522 | * extents in this range. |
| 6523 | */ |
| 6524 | ordered = btrfs_lookup_ordered_range(inode, lockstart, |
| 6525 | lockend - lockstart + 1); |
| 6526 | |
| 6527 | /* |
| 6528 | * We need to make sure there are no buffered pages in this |
| 6529 | * range either, we could have raced between the invalidate in |
| 6530 | * generic_file_direct_write and locking the extent. The |
| 6531 | * invalidate needs to happen so that reads after a write do not |
| 6532 | * get stale data. |
| 6533 | */ |
| 6534 | if (!ordered && (!writing || |
| 6535 | !test_range_bit(&BTRFS_I(inode)->io_tree, |
| 6536 | lockstart, lockend, EXTENT_UPTODATE, 0, |
| 6537 | *cached_state))) |
| 6538 | break; |
| 6539 | |
| 6540 | unlock_extent_cached(&BTRFS_I(inode)->io_tree, lockstart, lockend, |
| 6541 | cached_state, GFP_NOFS); |
| 6542 | |
| 6543 | if (ordered) { |
| 6544 | btrfs_start_ordered_extent(inode, ordered, 1); |
| 6545 | btrfs_put_ordered_extent(ordered); |
| 6546 | } else { |
| 6547 | /* Screw you mmap */ |
| 6548 | ret = filemap_write_and_wait_range(inode->i_mapping, |
| 6549 | lockstart, |
| 6550 | lockend); |
| 6551 | if (ret) |
| 6552 | break; |
| 6553 | |
| 6554 | /* |
| 6555 | * If we found a page that couldn't be invalidated just |
| 6556 | * fall back to buffered. |
| 6557 | */ |
| 6558 | ret = invalidate_inode_pages2_range(inode->i_mapping, |
| 6559 | lockstart >> PAGE_CACHE_SHIFT, |
| 6560 | lockend >> PAGE_CACHE_SHIFT); |
| 6561 | if (ret) |
| 6562 | break; |
| 6563 | } |
| 6564 | |
| 6565 | cond_resched(); |
| 6566 | } |
| 6567 | |
| 6568 | return ret; |
| 6569 | } |
| 6570 | |
Josef Bacik | 69ffb54 | 2012-09-11 15:40:07 -0400 | [diff] [blame] | 6571 | static struct extent_map *create_pinned_em(struct inode *inode, u64 start, |
| 6572 | u64 len, u64 orig_start, |
| 6573 | u64 block_start, u64 block_len, |
Josef Bacik | cc95bef | 2013-04-04 14:31:27 -0400 | [diff] [blame] | 6574 | u64 orig_block_len, u64 ram_bytes, |
| 6575 | int type) |
Josef Bacik | 69ffb54 | 2012-09-11 15:40:07 -0400 | [diff] [blame] | 6576 | { |
| 6577 | struct extent_map_tree *em_tree; |
| 6578 | struct extent_map *em; |
| 6579 | struct btrfs_root *root = BTRFS_I(inode)->root; |
| 6580 | int ret; |
| 6581 | |
| 6582 | em_tree = &BTRFS_I(inode)->extent_tree; |
| 6583 | em = alloc_extent_map(); |
| 6584 | if (!em) |
| 6585 | return ERR_PTR(-ENOMEM); |
| 6586 | |
| 6587 | em->start = start; |
| 6588 | em->orig_start = orig_start; |
Josef Bacik | 2ab28f3 | 2012-10-12 15:27:49 -0400 | [diff] [blame] | 6589 | em->mod_start = start; |
| 6590 | em->mod_len = len; |
Josef Bacik | 69ffb54 | 2012-09-11 15:40:07 -0400 | [diff] [blame] | 6591 | em->len = len; |
| 6592 | em->block_len = block_len; |
| 6593 | em->block_start = block_start; |
| 6594 | em->bdev = root->fs_info->fs_devices->latest_bdev; |
Josef Bacik | b493968 | 2012-12-03 10:31:19 -0500 | [diff] [blame] | 6595 | em->orig_block_len = orig_block_len; |
Josef Bacik | cc95bef | 2013-04-04 14:31:27 -0400 | [diff] [blame] | 6596 | em->ram_bytes = ram_bytes; |
Josef Bacik | 70c8a91 | 2012-10-11 16:54:30 -0400 | [diff] [blame] | 6597 | em->generation = -1; |
Josef Bacik | 69ffb54 | 2012-09-11 15:40:07 -0400 | [diff] [blame] | 6598 | set_bit(EXTENT_FLAG_PINNED, &em->flags); |
| 6599 | if (type == BTRFS_ORDERED_PREALLOC) |
Josef Bacik | b11e234 | 2012-12-03 10:58:15 -0500 | [diff] [blame] | 6600 | set_bit(EXTENT_FLAG_FILLING, &em->flags); |
Josef Bacik | 69ffb54 | 2012-09-11 15:40:07 -0400 | [diff] [blame] | 6601 | |
| 6602 | do { |
| 6603 | btrfs_drop_extent_cache(inode, em->start, |
| 6604 | em->start + em->len - 1, 0); |
| 6605 | write_lock(&em_tree->lock); |
Josef Bacik | 09a2a8f9 | 2013-04-05 16:51:15 -0400 | [diff] [blame] | 6606 | ret = add_extent_mapping(em_tree, em, 1); |
Josef Bacik | 69ffb54 | 2012-09-11 15:40:07 -0400 | [diff] [blame] | 6607 | write_unlock(&em_tree->lock); |
| 6608 | } while (ret == -EEXIST); |
| 6609 | |
| 6610 | if (ret) { |
| 6611 | free_extent_map(em); |
| 6612 | return ERR_PTR(ret); |
| 6613 | } |
| 6614 | |
| 6615 | return em; |
| 6616 | } |
| 6617 | |
| 6618 | |
Josef Bacik | 4b46fce | 2010-05-23 11:00:55 -0400 | [diff] [blame] | 6619 | static int btrfs_get_blocks_direct(struct inode *inode, sector_t iblock, |
| 6620 | struct buffer_head *bh_result, int create) |
| 6621 | { |
| 6622 | struct extent_map *em; |
| 6623 | struct btrfs_root *root = BTRFS_I(inode)->root; |
Josef Bacik | eb838e7 | 2012-07-31 16:28:48 -0400 | [diff] [blame] | 6624 | struct extent_state *cached_state = NULL; |
Josef Bacik | 4b46fce | 2010-05-23 11:00:55 -0400 | [diff] [blame] | 6625 | u64 start = iblock << inode->i_blkbits; |
Josef Bacik | eb838e7 | 2012-07-31 16:28:48 -0400 | [diff] [blame] | 6626 | u64 lockstart, lockend; |
Josef Bacik | 4b46fce | 2010-05-23 11:00:55 -0400 | [diff] [blame] | 6627 | u64 len = bh_result->b_size; |
Chris Mason | 46bfbb5 | 2010-05-26 11:04:10 -0400 | [diff] [blame] | 6628 | struct btrfs_trans_handle *trans; |
Josef Bacik | eb838e7 | 2012-07-31 16:28:48 -0400 | [diff] [blame] | 6629 | int unlock_bits = EXTENT_LOCKED; |
Miao Xie | 0934856 | 2013-02-07 10:12:07 +0000 | [diff] [blame] | 6630 | int ret = 0; |
Josef Bacik | eb838e7 | 2012-07-31 16:28:48 -0400 | [diff] [blame] | 6631 | |
Miao Xie | 172a504 | 2013-02-21 02:48:22 -0700 | [diff] [blame] | 6632 | if (create) |
Josef Bacik | eb838e7 | 2012-07-31 16:28:48 -0400 | [diff] [blame] | 6633 | unlock_bits |= EXTENT_DELALLOC | EXTENT_DIRTY; |
Miao Xie | 172a504 | 2013-02-21 02:48:22 -0700 | [diff] [blame] | 6634 | else |
Josef Bacik | c329861 | 2012-08-03 16:49:19 -0400 | [diff] [blame] | 6635 | len = min_t(u64, len, root->sectorsize); |
Josef Bacik | eb838e7 | 2012-07-31 16:28:48 -0400 | [diff] [blame] | 6636 | |
Josef Bacik | c329861 | 2012-08-03 16:49:19 -0400 | [diff] [blame] | 6637 | lockstart = start; |
| 6638 | lockend = start + len - 1; |
| 6639 | |
Josef Bacik | eb838e7 | 2012-07-31 16:28:48 -0400 | [diff] [blame] | 6640 | /* |
| 6641 | * If this errors out it's because we couldn't invalidate pagecache for |
| 6642 | * this range and we need to fallback to buffered. |
| 6643 | */ |
| 6644 | if (lock_extent_direct(inode, lockstart, lockend, &cached_state, create)) |
| 6645 | return -ENOTBLK; |
| 6646 | |
Josef Bacik | 4b46fce | 2010-05-23 11:00:55 -0400 | [diff] [blame] | 6647 | em = btrfs_get_extent(inode, NULL, 0, start, len, 0); |
Josef Bacik | eb838e7 | 2012-07-31 16:28:48 -0400 | [diff] [blame] | 6648 | if (IS_ERR(em)) { |
| 6649 | ret = PTR_ERR(em); |
| 6650 | goto unlock_err; |
| 6651 | } |
Josef Bacik | 4b46fce | 2010-05-23 11:00:55 -0400 | [diff] [blame] | 6652 | |
| 6653 | /* |
| 6654 | * Ok for INLINE and COMPRESSED extents we need to fallback on buffered |
| 6655 | * io. INLINE is special, and we could probably kludge it in here, but |
| 6656 | * it's still buffered so for safety lets just fall back to the generic |
| 6657 | * buffered path. |
| 6658 | * |
| 6659 | * For COMPRESSED we _have_ to read the entire extent in so we can |
| 6660 | * decompress it, so there will be buffering required no matter what we |
| 6661 | * do, so go ahead and fallback to buffered. |
| 6662 | * |
| 6663 | * We return -ENOTBLK because thats what makes DIO go ahead and go back |
| 6664 | * to buffered IO. Don't blame me, this is the price we pay for using |
| 6665 | * the generic code. |
| 6666 | */ |
| 6667 | if (test_bit(EXTENT_FLAG_COMPRESSED, &em->flags) || |
| 6668 | em->block_start == EXTENT_MAP_INLINE) { |
| 6669 | free_extent_map(em); |
Josef Bacik | eb838e7 | 2012-07-31 16:28:48 -0400 | [diff] [blame] | 6670 | ret = -ENOTBLK; |
| 6671 | goto unlock_err; |
Josef Bacik | 4b46fce | 2010-05-23 11:00:55 -0400 | [diff] [blame] | 6672 | } |
| 6673 | |
| 6674 | /* Just a good old fashioned hole, return */ |
| 6675 | if (!create && (em->block_start == EXTENT_MAP_HOLE || |
| 6676 | test_bit(EXTENT_FLAG_PREALLOC, &em->flags))) { |
| 6677 | free_extent_map(em); |
Josef Bacik | eb838e7 | 2012-07-31 16:28:48 -0400 | [diff] [blame] | 6678 | goto unlock_err; |
Josef Bacik | 4b46fce | 2010-05-23 11:00:55 -0400 | [diff] [blame] | 6679 | } |
| 6680 | |
| 6681 | /* |
| 6682 | * We don't allocate a new extent in the following cases |
| 6683 | * |
| 6684 | * 1) The inode is marked as NODATACOW. In this case we'll just use the |
| 6685 | * existing extent. |
| 6686 | * 2) The extent is marked as PREALLOC. We're good to go here and can |
| 6687 | * just use the extent. |
| 6688 | * |
| 6689 | */ |
Chris Mason | 46bfbb5 | 2010-05-26 11:04:10 -0400 | [diff] [blame] | 6690 | if (!create) { |
Josef Bacik | eb838e7 | 2012-07-31 16:28:48 -0400 | [diff] [blame] | 6691 | len = min(len, em->len - (start - em->start)); |
| 6692 | lockstart = start + len; |
| 6693 | goto unlock; |
Chris Mason | 46bfbb5 | 2010-05-26 11:04:10 -0400 | [diff] [blame] | 6694 | } |
Josef Bacik | 4b46fce | 2010-05-23 11:00:55 -0400 | [diff] [blame] | 6695 | |
| 6696 | if (test_bit(EXTENT_FLAG_PREALLOC, &em->flags) || |
| 6697 | ((BTRFS_I(inode)->flags & BTRFS_INODE_NODATACOW) && |
| 6698 | em->block_start != EXTENT_MAP_HOLE)) { |
Josef Bacik | 4b46fce | 2010-05-23 11:00:55 -0400 | [diff] [blame] | 6699 | int type; |
| 6700 | int ret; |
Josef Bacik | eb384b5 | 2013-04-24 16:32:55 -0400 | [diff] [blame] | 6701 | u64 block_start, orig_start, orig_block_len, ram_bytes; |
Josef Bacik | 4b46fce | 2010-05-23 11:00:55 -0400 | [diff] [blame] | 6702 | |
| 6703 | if (test_bit(EXTENT_FLAG_PREALLOC, &em->flags)) |
| 6704 | type = BTRFS_ORDERED_PREALLOC; |
| 6705 | else |
| 6706 | type = BTRFS_ORDERED_NOCOW; |
Chris Mason | 46bfbb5 | 2010-05-26 11:04:10 -0400 | [diff] [blame] | 6707 | len = min(len, em->len - (start - em->start)); |
Josef Bacik | 4b46fce | 2010-05-23 11:00:55 -0400 | [diff] [blame] | 6708 | block_start = em->block_start + (start - em->start); |
Chris Mason | 46bfbb5 | 2010-05-26 11:04:10 -0400 | [diff] [blame] | 6709 | |
| 6710 | /* |
| 6711 | * we're not going to log anything, but we do need |
| 6712 | * to make sure the current transaction stays open |
| 6713 | * while we look for nocow cross refs |
| 6714 | */ |
Josef Bacik | 7a7eaa4 | 2011-04-13 12:54:33 -0400 | [diff] [blame] | 6715 | trans = btrfs_join_transaction(root); |
Tsutomu Itoh | 3612b49 | 2011-01-25 02:51:38 +0000 | [diff] [blame] | 6716 | if (IS_ERR(trans)) |
Chris Mason | 46bfbb5 | 2010-05-26 11:04:10 -0400 | [diff] [blame] | 6717 | goto must_cow; |
| 6718 | |
Josef Bacik | 7ee9e44 | 2013-06-21 16:37:03 -0400 | [diff] [blame] | 6719 | if (can_nocow_extent(trans, inode, start, &len, &orig_start, |
| 6720 | &orig_block_len, &ram_bytes) == 1) { |
Josef Bacik | 69ffb54 | 2012-09-11 15:40:07 -0400 | [diff] [blame] | 6721 | if (type == BTRFS_ORDERED_PREALLOC) { |
| 6722 | free_extent_map(em); |
| 6723 | em = create_pinned_em(inode, start, len, |
| 6724 | orig_start, |
Josef Bacik | b493968 | 2012-12-03 10:31:19 -0500 | [diff] [blame] | 6725 | block_start, len, |
Josef Bacik | cc95bef | 2013-04-04 14:31:27 -0400 | [diff] [blame] | 6726 | orig_block_len, |
| 6727 | ram_bytes, type); |
Josef Bacik | 69ffb54 | 2012-09-11 15:40:07 -0400 | [diff] [blame] | 6728 | if (IS_ERR(em)) { |
| 6729 | btrfs_end_transaction(trans, root); |
| 6730 | goto unlock_err; |
| 6731 | } |
| 6732 | } |
| 6733 | |
Chris Mason | 46bfbb5 | 2010-05-26 11:04:10 -0400 | [diff] [blame] | 6734 | ret = btrfs_add_ordered_extent_dio(inode, start, |
| 6735 | block_start, len, len, type); |
| 6736 | btrfs_end_transaction(trans, root); |
| 6737 | if (ret) { |
| 6738 | free_extent_map(em); |
Josef Bacik | eb838e7 | 2012-07-31 16:28:48 -0400 | [diff] [blame] | 6739 | goto unlock_err; |
Chris Mason | 46bfbb5 | 2010-05-26 11:04:10 -0400 | [diff] [blame] | 6740 | } |
| 6741 | goto unlock; |
Josef Bacik | 4b46fce | 2010-05-23 11:00:55 -0400 | [diff] [blame] | 6742 | } |
Chris Mason | 46bfbb5 | 2010-05-26 11:04:10 -0400 | [diff] [blame] | 6743 | btrfs_end_transaction(trans, root); |
Josef Bacik | 4b46fce | 2010-05-23 11:00:55 -0400 | [diff] [blame] | 6744 | } |
Chris Mason | 46bfbb5 | 2010-05-26 11:04:10 -0400 | [diff] [blame] | 6745 | must_cow: |
| 6746 | /* |
| 6747 | * this will cow the extent, reset the len in case we changed |
| 6748 | * it above |
| 6749 | */ |
| 6750 | len = bh_result->b_size; |
Josef Bacik | 70c8a91 | 2012-10-11 16:54:30 -0400 | [diff] [blame] | 6751 | free_extent_map(em); |
| 6752 | em = btrfs_new_extent_direct(inode, start, len); |
Josef Bacik | eb838e7 | 2012-07-31 16:28:48 -0400 | [diff] [blame] | 6753 | if (IS_ERR(em)) { |
| 6754 | ret = PTR_ERR(em); |
| 6755 | goto unlock_err; |
| 6756 | } |
Chris Mason | 46bfbb5 | 2010-05-26 11:04:10 -0400 | [diff] [blame] | 6757 | len = min(len, em->len - (start - em->start)); |
| 6758 | unlock: |
Josef Bacik | 4b46fce | 2010-05-23 11:00:55 -0400 | [diff] [blame] | 6759 | bh_result->b_blocknr = (em->block_start + (start - em->start)) >> |
| 6760 | inode->i_blkbits; |
Chris Mason | 46bfbb5 | 2010-05-26 11:04:10 -0400 | [diff] [blame] | 6761 | bh_result->b_size = len; |
Josef Bacik | 4b46fce | 2010-05-23 11:00:55 -0400 | [diff] [blame] | 6762 | bh_result->b_bdev = em->bdev; |
| 6763 | set_buffer_mapped(bh_result); |
Josef Bacik | c3473e8 | 2012-06-19 10:59:00 -0400 | [diff] [blame] | 6764 | if (create) { |
| 6765 | if (!test_bit(EXTENT_FLAG_PREALLOC, &em->flags)) |
| 6766 | set_buffer_new(bh_result); |
| 6767 | |
| 6768 | /* |
| 6769 | * Need to update the i_size under the extent lock so buffered |
| 6770 | * readers will get the updated i_size when we unlock. |
| 6771 | */ |
| 6772 | if (start + len > i_size_read(inode)) |
| 6773 | i_size_write(inode, start + len); |
Miao Xie | 0934856 | 2013-02-07 10:12:07 +0000 | [diff] [blame] | 6774 | |
Miao Xie | 172a504 | 2013-02-21 02:48:22 -0700 | [diff] [blame] | 6775 | spin_lock(&BTRFS_I(inode)->lock); |
| 6776 | BTRFS_I(inode)->outstanding_extents++; |
| 6777 | spin_unlock(&BTRFS_I(inode)->lock); |
| 6778 | |
Miao Xie | 0934856 | 2013-02-07 10:12:07 +0000 | [diff] [blame] | 6779 | ret = set_extent_bit(&BTRFS_I(inode)->io_tree, lockstart, |
| 6780 | lockstart + len - 1, EXTENT_DELALLOC, NULL, |
| 6781 | &cached_state, GFP_NOFS); |
| 6782 | BUG_ON(ret); |
Josef Bacik | c3473e8 | 2012-06-19 10:59:00 -0400 | [diff] [blame] | 6783 | } |
Josef Bacik | 4b46fce | 2010-05-23 11:00:55 -0400 | [diff] [blame] | 6784 | |
Josef Bacik | eb838e7 | 2012-07-31 16:28:48 -0400 | [diff] [blame] | 6785 | /* |
| 6786 | * In the case of write we need to clear and unlock the entire range, |
| 6787 | * in the case of read we need to unlock only the end area that we |
| 6788 | * aren't using if there is any left over space. |
| 6789 | */ |
Liu Bo | 24c03fa | 2012-08-22 20:10:38 -0600 | [diff] [blame] | 6790 | if (lockstart < lockend) { |
Miao Xie | 0934856 | 2013-02-07 10:12:07 +0000 | [diff] [blame] | 6791 | clear_extent_bit(&BTRFS_I(inode)->io_tree, lockstart, |
| 6792 | lockend, unlock_bits, 1, 0, |
| 6793 | &cached_state, GFP_NOFS); |
Liu Bo | 24c03fa | 2012-08-22 20:10:38 -0600 | [diff] [blame] | 6794 | } else { |
Josef Bacik | eb838e7 | 2012-07-31 16:28:48 -0400 | [diff] [blame] | 6795 | free_extent_state(cached_state); |
Liu Bo | 24c03fa | 2012-08-22 20:10:38 -0600 | [diff] [blame] | 6796 | } |
Josef Bacik | eb838e7 | 2012-07-31 16:28:48 -0400 | [diff] [blame] | 6797 | |
Josef Bacik | 4b46fce | 2010-05-23 11:00:55 -0400 | [diff] [blame] | 6798 | free_extent_map(em); |
| 6799 | |
| 6800 | return 0; |
Josef Bacik | eb838e7 | 2012-07-31 16:28:48 -0400 | [diff] [blame] | 6801 | |
| 6802 | unlock_err: |
Josef Bacik | eb838e7 | 2012-07-31 16:28:48 -0400 | [diff] [blame] | 6803 | clear_extent_bit(&BTRFS_I(inode)->io_tree, lockstart, lockend, |
| 6804 | unlock_bits, 1, 0, &cached_state, GFP_NOFS); |
| 6805 | return ret; |
Josef Bacik | 4b46fce | 2010-05-23 11:00:55 -0400 | [diff] [blame] | 6806 | } |
| 6807 | |
Josef Bacik | 4b46fce | 2010-05-23 11:00:55 -0400 | [diff] [blame] | 6808 | static void btrfs_endio_direct_read(struct bio *bio, int err) |
| 6809 | { |
Miao Xie | e65e153 | 2010-11-22 03:04:43 +0000 | [diff] [blame] | 6810 | struct btrfs_dio_private *dip = bio->bi_private; |
Josef Bacik | 4b46fce | 2010-05-23 11:00:55 -0400 | [diff] [blame] | 6811 | struct bio_vec *bvec_end = bio->bi_io_vec + bio->bi_vcnt - 1; |
| 6812 | struct bio_vec *bvec = bio->bi_io_vec; |
Josef Bacik | 4b46fce | 2010-05-23 11:00:55 -0400 | [diff] [blame] | 6813 | struct inode *inode = dip->inode; |
| 6814 | struct btrfs_root *root = BTRFS_I(inode)->root; |
Chris Mason | 9be3395 | 2013-05-17 18:30:14 -0400 | [diff] [blame] | 6815 | struct bio *dio_bio; |
Miao Xie | facc8a22 | 2013-07-25 19:22:34 +0800 | [diff] [blame] | 6816 | u32 *csums = (u32 *)dip->csum; |
| 6817 | int index = 0; |
Josef Bacik | 4b46fce | 2010-05-23 11:00:55 -0400 | [diff] [blame] | 6818 | u64 start; |
Josef Bacik | 4b46fce | 2010-05-23 11:00:55 -0400 | [diff] [blame] | 6819 | |
| 6820 | start = dip->logical_offset; |
| 6821 | do { |
| 6822 | if (!(BTRFS_I(inode)->flags & BTRFS_INODE_NODATASUM)) { |
| 6823 | struct page *page = bvec->bv_page; |
| 6824 | char *kaddr; |
| 6825 | u32 csum = ~(u32)0; |
| 6826 | unsigned long flags; |
| 6827 | |
| 6828 | local_irq_save(flags); |
Cong Wang | 7ac687d | 2011-11-25 23:14:28 +0800 | [diff] [blame] | 6829 | kaddr = kmap_atomic(page); |
Liu Bo | b049668 | 2013-03-14 14:57:45 +0000 | [diff] [blame] | 6830 | csum = btrfs_csum_data(kaddr + bvec->bv_offset, |
Josef Bacik | 4b46fce | 2010-05-23 11:00:55 -0400 | [diff] [blame] | 6831 | csum, bvec->bv_len); |
| 6832 | btrfs_csum_final(csum, (char *)&csum); |
Cong Wang | 7ac687d | 2011-11-25 23:14:28 +0800 | [diff] [blame] | 6833 | kunmap_atomic(kaddr); |
Josef Bacik | 4b46fce | 2010-05-23 11:00:55 -0400 | [diff] [blame] | 6834 | local_irq_restore(flags); |
| 6835 | |
| 6836 | flush_dcache_page(bvec->bv_page); |
Miao Xie | facc8a22 | 2013-07-25 19:22:34 +0800 | [diff] [blame] | 6837 | if (csum != csums[index]) { |
| 6838 | btrfs_err(root->fs_info, "csum failed ino %llu off %llu csum %u expected csum %u", |
| 6839 | (unsigned long long)btrfs_ino(inode), |
| 6840 | (unsigned long long)start, |
| 6841 | csum, csums[index]); |
Josef Bacik | 4b46fce | 2010-05-23 11:00:55 -0400 | [diff] [blame] | 6842 | err = -EIO; |
| 6843 | } |
| 6844 | } |
| 6845 | |
| 6846 | start += bvec->bv_len; |
Josef Bacik | 4b46fce | 2010-05-23 11:00:55 -0400 | [diff] [blame] | 6847 | bvec++; |
Miao Xie | facc8a22 | 2013-07-25 19:22:34 +0800 | [diff] [blame] | 6848 | index++; |
Josef Bacik | 4b46fce | 2010-05-23 11:00:55 -0400 | [diff] [blame] | 6849 | } while (bvec <= bvec_end); |
| 6850 | |
| 6851 | unlock_extent(&BTRFS_I(inode)->io_tree, dip->logical_offset, |
Jeff Mahoney | d008237 | 2012-03-01 14:57:19 +0100 | [diff] [blame] | 6852 | dip->logical_offset + dip->bytes - 1); |
Chris Mason | 9be3395 | 2013-05-17 18:30:14 -0400 | [diff] [blame] | 6853 | dio_bio = dip->dio_bio; |
Josef Bacik | 4b46fce | 2010-05-23 11:00:55 -0400 | [diff] [blame] | 6854 | |
Josef Bacik | 4b46fce | 2010-05-23 11:00:55 -0400 | [diff] [blame] | 6855 | kfree(dip); |
Josef Bacik | c0da7aa | 2011-03-22 11:05:07 -0400 | [diff] [blame] | 6856 | |
| 6857 | /* If we had a csum failure make sure to clear the uptodate flag */ |
| 6858 | if (err) |
Chris Mason | 9be3395 | 2013-05-17 18:30:14 -0400 | [diff] [blame] | 6859 | clear_bit(BIO_UPTODATE, &dio_bio->bi_flags); |
| 6860 | dio_end_io(dio_bio, err); |
| 6861 | bio_put(bio); |
Josef Bacik | 4b46fce | 2010-05-23 11:00:55 -0400 | [diff] [blame] | 6862 | } |
| 6863 | |
| 6864 | static void btrfs_endio_direct_write(struct bio *bio, int err) |
| 6865 | { |
| 6866 | struct btrfs_dio_private *dip = bio->bi_private; |
| 6867 | struct inode *inode = dip->inode; |
| 6868 | struct btrfs_root *root = BTRFS_I(inode)->root; |
Josef Bacik | 4b46fce | 2010-05-23 11:00:55 -0400 | [diff] [blame] | 6869 | struct btrfs_ordered_extent *ordered = NULL; |
Chris Mason | 163cf09 | 2010-11-28 19:56:33 -0500 | [diff] [blame] | 6870 | u64 ordered_offset = dip->logical_offset; |
| 6871 | u64 ordered_bytes = dip->bytes; |
Chris Mason | 9be3395 | 2013-05-17 18:30:14 -0400 | [diff] [blame] | 6872 | struct bio *dio_bio; |
Josef Bacik | 4b46fce | 2010-05-23 11:00:55 -0400 | [diff] [blame] | 6873 | int ret; |
| 6874 | |
| 6875 | if (err) |
| 6876 | goto out_done; |
Chris Mason | 163cf09 | 2010-11-28 19:56:33 -0500 | [diff] [blame] | 6877 | again: |
| 6878 | ret = btrfs_dec_test_first_ordered_pending(inode, &ordered, |
| 6879 | &ordered_offset, |
Josef Bacik | 5fd0204 | 2012-05-02 14:00:54 -0400 | [diff] [blame] | 6880 | ordered_bytes, !err); |
Josef Bacik | 4b46fce | 2010-05-23 11:00:55 -0400 | [diff] [blame] | 6881 | if (!ret) |
Chris Mason | 163cf09 | 2010-11-28 19:56:33 -0500 | [diff] [blame] | 6882 | goto out_test; |
Josef Bacik | 4b46fce | 2010-05-23 11:00:55 -0400 | [diff] [blame] | 6883 | |
Josef Bacik | 5fd0204 | 2012-05-02 14:00:54 -0400 | [diff] [blame] | 6884 | ordered->work.func = finish_ordered_fn; |
| 6885 | ordered->work.flags = 0; |
| 6886 | btrfs_queue_worker(&root->fs_info->endio_write_workers, |
| 6887 | &ordered->work); |
Chris Mason | 163cf09 | 2010-11-28 19:56:33 -0500 | [diff] [blame] | 6888 | out_test: |
| 6889 | /* |
| 6890 | * our bio might span multiple ordered extents. If we haven't |
| 6891 | * completed the accounting for the whole dio, go back and try again |
| 6892 | */ |
| 6893 | if (ordered_offset < dip->logical_offset + dip->bytes) { |
| 6894 | ordered_bytes = dip->logical_offset + dip->bytes - |
| 6895 | ordered_offset; |
Josef Bacik | 5fd0204 | 2012-05-02 14:00:54 -0400 | [diff] [blame] | 6896 | ordered = NULL; |
Chris Mason | 163cf09 | 2010-11-28 19:56:33 -0500 | [diff] [blame] | 6897 | goto again; |
| 6898 | } |
Josef Bacik | 4b46fce | 2010-05-23 11:00:55 -0400 | [diff] [blame] | 6899 | out_done: |
Chris Mason | 9be3395 | 2013-05-17 18:30:14 -0400 | [diff] [blame] | 6900 | dio_bio = dip->dio_bio; |
Josef Bacik | 4b46fce | 2010-05-23 11:00:55 -0400 | [diff] [blame] | 6901 | |
Josef Bacik | 4b46fce | 2010-05-23 11:00:55 -0400 | [diff] [blame] | 6902 | kfree(dip); |
Josef Bacik | c0da7aa | 2011-03-22 11:05:07 -0400 | [diff] [blame] | 6903 | |
| 6904 | /* If we had an error make sure to clear the uptodate flag */ |
| 6905 | if (err) |
Chris Mason | 9be3395 | 2013-05-17 18:30:14 -0400 | [diff] [blame] | 6906 | clear_bit(BIO_UPTODATE, &dio_bio->bi_flags); |
| 6907 | dio_end_io(dio_bio, err); |
| 6908 | bio_put(bio); |
Josef Bacik | 4b46fce | 2010-05-23 11:00:55 -0400 | [diff] [blame] | 6909 | } |
| 6910 | |
Chris Mason | eaf25d9 | 2010-05-25 09:48:28 -0400 | [diff] [blame] | 6911 | static int __btrfs_submit_bio_start_direct_io(struct inode *inode, int rw, |
| 6912 | struct bio *bio, int mirror_num, |
| 6913 | unsigned long bio_flags, u64 offset) |
| 6914 | { |
| 6915 | int ret; |
| 6916 | struct btrfs_root *root = BTRFS_I(inode)->root; |
| 6917 | ret = btrfs_csum_one_bio(root, inode, bio, offset, 1); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 6918 | BUG_ON(ret); /* -ENOMEM */ |
Chris Mason | eaf25d9 | 2010-05-25 09:48:28 -0400 | [diff] [blame] | 6919 | return 0; |
| 6920 | } |
| 6921 | |
Miao Xie | e65e153 | 2010-11-22 03:04:43 +0000 | [diff] [blame] | 6922 | static void btrfs_end_dio_bio(struct bio *bio, int err) |
| 6923 | { |
| 6924 | struct btrfs_dio_private *dip = bio->bi_private; |
| 6925 | |
| 6926 | if (err) { |
Li Zefan | 33345d01 | 2011-04-20 10:31:50 +0800 | [diff] [blame] | 6927 | printk(KERN_ERR "btrfs direct IO failed ino %llu rw %lu " |
Jan Beulich | 3dd1462 | 2010-12-07 14:54:09 +0000 | [diff] [blame] | 6928 | "sector %#Lx len %u err no %d\n", |
Li Zefan | 33345d01 | 2011-04-20 10:31:50 +0800 | [diff] [blame] | 6929 | (unsigned long long)btrfs_ino(dip->inode), bio->bi_rw, |
Jan Beulich | 3dd1462 | 2010-12-07 14:54:09 +0000 | [diff] [blame] | 6930 | (unsigned long long)bio->bi_sector, bio->bi_size, err); |
Miao Xie | e65e153 | 2010-11-22 03:04:43 +0000 | [diff] [blame] | 6931 | dip->errors = 1; |
| 6932 | |
| 6933 | /* |
| 6934 | * before atomic variable goto zero, we must make sure |
| 6935 | * dip->errors is perceived to be set. |
| 6936 | */ |
| 6937 | smp_mb__before_atomic_dec(); |
| 6938 | } |
| 6939 | |
| 6940 | /* if there are more bios still pending for this dio, just exit */ |
| 6941 | if (!atomic_dec_and_test(&dip->pending_bios)) |
| 6942 | goto out; |
| 6943 | |
Chris Mason | 9be3395 | 2013-05-17 18:30:14 -0400 | [diff] [blame] | 6944 | if (dip->errors) { |
Miao Xie | e65e153 | 2010-11-22 03:04:43 +0000 | [diff] [blame] | 6945 | bio_io_error(dip->orig_bio); |
Chris Mason | 9be3395 | 2013-05-17 18:30:14 -0400 | [diff] [blame] | 6946 | } else { |
| 6947 | set_bit(BIO_UPTODATE, &dip->dio_bio->bi_flags); |
Miao Xie | e65e153 | 2010-11-22 03:04:43 +0000 | [diff] [blame] | 6948 | bio_endio(dip->orig_bio, 0); |
| 6949 | } |
| 6950 | out: |
| 6951 | bio_put(bio); |
| 6952 | } |
| 6953 | |
| 6954 | static struct bio *btrfs_dio_bio_alloc(struct block_device *bdev, |
| 6955 | u64 first_sector, gfp_t gfp_flags) |
| 6956 | { |
| 6957 | int nr_vecs = bio_get_nr_vecs(bdev); |
| 6958 | return btrfs_bio_alloc(bdev, first_sector, nr_vecs, gfp_flags); |
| 6959 | } |
| 6960 | |
| 6961 | static inline int __btrfs_submit_dio_bio(struct bio *bio, struct inode *inode, |
| 6962 | int rw, u64 file_offset, int skip_sum, |
Josef Bacik | c329861 | 2012-08-03 16:49:19 -0400 | [diff] [blame] | 6963 | int async_submit) |
Miao Xie | e65e153 | 2010-11-22 03:04:43 +0000 | [diff] [blame] | 6964 | { |
Miao Xie | facc8a22 | 2013-07-25 19:22:34 +0800 | [diff] [blame] | 6965 | struct btrfs_dio_private *dip = bio->bi_private; |
Miao Xie | e65e153 | 2010-11-22 03:04:43 +0000 | [diff] [blame] | 6966 | int write = rw & REQ_WRITE; |
| 6967 | struct btrfs_root *root = BTRFS_I(inode)->root; |
| 6968 | int ret; |
| 6969 | |
Josef Bacik | b812ce2 | 2012-11-16 13:56:32 -0500 | [diff] [blame] | 6970 | if (async_submit) |
| 6971 | async_submit = !atomic_read(&BTRFS_I(inode)->sync_writers); |
| 6972 | |
Miao Xie | e65e153 | 2010-11-22 03:04:43 +0000 | [diff] [blame] | 6973 | bio_get(bio); |
Josef Bacik | 5fd0204 | 2012-05-02 14:00:54 -0400 | [diff] [blame] | 6974 | |
| 6975 | if (!write) { |
| 6976 | ret = btrfs_bio_wq_end_io(root->fs_info, bio, 0); |
| 6977 | if (ret) |
| 6978 | goto err; |
| 6979 | } |
Miao Xie | e65e153 | 2010-11-22 03:04:43 +0000 | [diff] [blame] | 6980 | |
Josef Bacik | 1ae3993 | 2011-04-06 14:41:34 -0400 | [diff] [blame] | 6981 | if (skip_sum) |
| 6982 | goto map; |
| 6983 | |
| 6984 | if (write && async_submit) { |
Miao Xie | e65e153 | 2010-11-22 03:04:43 +0000 | [diff] [blame] | 6985 | ret = btrfs_wq_submit_bio(root->fs_info, |
| 6986 | inode, rw, bio, 0, 0, |
| 6987 | file_offset, |
| 6988 | __btrfs_submit_bio_start_direct_io, |
| 6989 | __btrfs_submit_bio_done); |
| 6990 | goto err; |
Josef Bacik | 1ae3993 | 2011-04-06 14:41:34 -0400 | [diff] [blame] | 6991 | } else if (write) { |
| 6992 | /* |
| 6993 | * If we aren't doing async submit, calculate the csum of the |
| 6994 | * bio now. |
| 6995 | */ |
| 6996 | ret = btrfs_csum_one_bio(root, inode, bio, file_offset, 1); |
| 6997 | if (ret) |
| 6998 | goto err; |
Tsutomu Itoh | c2db107 | 2011-03-01 06:48:31 +0000 | [diff] [blame] | 6999 | } else if (!skip_sum) { |
Miao Xie | facc8a22 | 2013-07-25 19:22:34 +0800 | [diff] [blame] | 7000 | ret = btrfs_lookup_bio_sums_dio(root, inode, dip, bio, |
| 7001 | file_offset); |
Tsutomu Itoh | c2db107 | 2011-03-01 06:48:31 +0000 | [diff] [blame] | 7002 | if (ret) |
| 7003 | goto err; |
| 7004 | } |
Miao Xie | e65e153 | 2010-11-22 03:04:43 +0000 | [diff] [blame] | 7005 | |
Josef Bacik | 1ae3993 | 2011-04-06 14:41:34 -0400 | [diff] [blame] | 7006 | map: |
| 7007 | ret = btrfs_map_bio(root, rw, bio, 0, async_submit); |
Miao Xie | e65e153 | 2010-11-22 03:04:43 +0000 | [diff] [blame] | 7008 | err: |
| 7009 | bio_put(bio); |
| 7010 | return ret; |
| 7011 | } |
| 7012 | |
| 7013 | static int btrfs_submit_direct_hook(int rw, struct btrfs_dio_private *dip, |
| 7014 | int skip_sum) |
| 7015 | { |
| 7016 | struct inode *inode = dip->inode; |
| 7017 | struct btrfs_root *root = BTRFS_I(inode)->root; |
Miao Xie | e65e153 | 2010-11-22 03:04:43 +0000 | [diff] [blame] | 7018 | struct bio *bio; |
| 7019 | struct bio *orig_bio = dip->orig_bio; |
| 7020 | struct bio_vec *bvec = orig_bio->bi_io_vec; |
| 7021 | u64 start_sector = orig_bio->bi_sector; |
| 7022 | u64 file_offset = dip->logical_offset; |
| 7023 | u64 submit_len = 0; |
| 7024 | u64 map_length; |
| 7025 | int nr_pages = 0; |
Miao Xie | e65e153 | 2010-11-22 03:04:43 +0000 | [diff] [blame] | 7026 | int ret = 0; |
Josef Bacik | 1ae3993 | 2011-04-06 14:41:34 -0400 | [diff] [blame] | 7027 | int async_submit = 0; |
Miao Xie | e65e153 | 2010-11-22 03:04:43 +0000 | [diff] [blame] | 7028 | |
Miao Xie | e65e153 | 2010-11-22 03:04:43 +0000 | [diff] [blame] | 7029 | map_length = orig_bio->bi_size; |
David Woodhouse | 53b381b | 2013-01-29 18:40:14 -0500 | [diff] [blame] | 7030 | ret = btrfs_map_block(root->fs_info, rw, start_sector << 9, |
Miao Xie | e65e153 | 2010-11-22 03:04:43 +0000 | [diff] [blame] | 7031 | &map_length, NULL, 0); |
| 7032 | if (ret) { |
Josef Bacik | 64728bb | 2011-04-25 19:43:52 -0400 | [diff] [blame] | 7033 | bio_put(orig_bio); |
Miao Xie | e65e153 | 2010-11-22 03:04:43 +0000 | [diff] [blame] | 7034 | return -EIO; |
| 7035 | } |
Miao Xie | facc8a22 | 2013-07-25 19:22:34 +0800 | [diff] [blame] | 7036 | |
Josef Bacik | 02f57c7 | 2011-04-06 14:25:44 -0400 | [diff] [blame] | 7037 | if (map_length >= orig_bio->bi_size) { |
| 7038 | bio = orig_bio; |
| 7039 | goto submit; |
| 7040 | } |
| 7041 | |
David Woodhouse | 53b381b | 2013-01-29 18:40:14 -0500 | [diff] [blame] | 7042 | /* async crcs make it difficult to collect full stripe writes. */ |
| 7043 | if (btrfs_get_alloc_profile(root, 1) & |
| 7044 | (BTRFS_BLOCK_GROUP_RAID5 | BTRFS_BLOCK_GROUP_RAID6)) |
| 7045 | async_submit = 0; |
| 7046 | else |
| 7047 | async_submit = 1; |
| 7048 | |
Josef Bacik | 02f57c7 | 2011-04-06 14:25:44 -0400 | [diff] [blame] | 7049 | bio = btrfs_dio_bio_alloc(orig_bio->bi_bdev, start_sector, GFP_NOFS); |
| 7050 | if (!bio) |
| 7051 | return -ENOMEM; |
| 7052 | bio->bi_private = dip; |
| 7053 | bio->bi_end_io = btrfs_end_dio_bio; |
| 7054 | atomic_inc(&dip->pending_bios); |
| 7055 | |
Miao Xie | e65e153 | 2010-11-22 03:04:43 +0000 | [diff] [blame] | 7056 | while (bvec <= (orig_bio->bi_io_vec + orig_bio->bi_vcnt - 1)) { |
| 7057 | if (unlikely(map_length < submit_len + bvec->bv_len || |
| 7058 | bio_add_page(bio, bvec->bv_page, bvec->bv_len, |
| 7059 | bvec->bv_offset) < bvec->bv_len)) { |
| 7060 | /* |
| 7061 | * inc the count before we submit the bio so |
| 7062 | * we know the end IO handler won't happen before |
| 7063 | * we inc the count. Otherwise, the dip might get freed |
| 7064 | * before we're done setting it up |
| 7065 | */ |
| 7066 | atomic_inc(&dip->pending_bios); |
| 7067 | ret = __btrfs_submit_dio_bio(bio, inode, rw, |
| 7068 | file_offset, skip_sum, |
Josef Bacik | c329861 | 2012-08-03 16:49:19 -0400 | [diff] [blame] | 7069 | async_submit); |
Miao Xie | e65e153 | 2010-11-22 03:04:43 +0000 | [diff] [blame] | 7070 | if (ret) { |
| 7071 | bio_put(bio); |
| 7072 | atomic_dec(&dip->pending_bios); |
| 7073 | goto out_err; |
| 7074 | } |
| 7075 | |
Miao Xie | e65e153 | 2010-11-22 03:04:43 +0000 | [diff] [blame] | 7076 | start_sector += submit_len >> 9; |
| 7077 | file_offset += submit_len; |
| 7078 | |
| 7079 | submit_len = 0; |
| 7080 | nr_pages = 0; |
| 7081 | |
| 7082 | bio = btrfs_dio_bio_alloc(orig_bio->bi_bdev, |
| 7083 | start_sector, GFP_NOFS); |
| 7084 | if (!bio) |
| 7085 | goto out_err; |
| 7086 | bio->bi_private = dip; |
| 7087 | bio->bi_end_io = btrfs_end_dio_bio; |
| 7088 | |
| 7089 | map_length = orig_bio->bi_size; |
David Woodhouse | 53b381b | 2013-01-29 18:40:14 -0500 | [diff] [blame] | 7090 | ret = btrfs_map_block(root->fs_info, rw, |
Stefan Behrens | 3ec706c | 2012-11-05 15:46:42 +0100 | [diff] [blame] | 7091 | start_sector << 9, |
Miao Xie | e65e153 | 2010-11-22 03:04:43 +0000 | [diff] [blame] | 7092 | &map_length, NULL, 0); |
| 7093 | if (ret) { |
| 7094 | bio_put(bio); |
| 7095 | goto out_err; |
| 7096 | } |
| 7097 | } else { |
| 7098 | submit_len += bvec->bv_len; |
| 7099 | nr_pages ++; |
| 7100 | bvec++; |
| 7101 | } |
| 7102 | } |
| 7103 | |
Josef Bacik | 02f57c7 | 2011-04-06 14:25:44 -0400 | [diff] [blame] | 7104 | submit: |
Miao Xie | e65e153 | 2010-11-22 03:04:43 +0000 | [diff] [blame] | 7105 | ret = __btrfs_submit_dio_bio(bio, inode, rw, file_offset, skip_sum, |
Josef Bacik | c329861 | 2012-08-03 16:49:19 -0400 | [diff] [blame] | 7106 | async_submit); |
Miao Xie | e65e153 | 2010-11-22 03:04:43 +0000 | [diff] [blame] | 7107 | if (!ret) |
| 7108 | return 0; |
| 7109 | |
| 7110 | bio_put(bio); |
| 7111 | out_err: |
| 7112 | dip->errors = 1; |
| 7113 | /* |
| 7114 | * before atomic variable goto zero, we must |
| 7115 | * make sure dip->errors is perceived to be set. |
| 7116 | */ |
| 7117 | smp_mb__before_atomic_dec(); |
| 7118 | if (atomic_dec_and_test(&dip->pending_bios)) |
| 7119 | bio_io_error(dip->orig_bio); |
| 7120 | |
| 7121 | /* bio_end_io() will handle error, so we needn't return it */ |
| 7122 | return 0; |
| 7123 | } |
| 7124 | |
Chris Mason | 9be3395 | 2013-05-17 18:30:14 -0400 | [diff] [blame] | 7125 | static void btrfs_submit_direct(int rw, struct bio *dio_bio, |
| 7126 | struct inode *inode, loff_t file_offset) |
Josef Bacik | 4b46fce | 2010-05-23 11:00:55 -0400 | [diff] [blame] | 7127 | { |
| 7128 | struct btrfs_root *root = BTRFS_I(inode)->root; |
| 7129 | struct btrfs_dio_private *dip; |
Chris Mason | 9be3395 | 2013-05-17 18:30:14 -0400 | [diff] [blame] | 7130 | struct bio *io_bio; |
Josef Bacik | 4b46fce | 2010-05-23 11:00:55 -0400 | [diff] [blame] | 7131 | int skip_sum; |
Miao Xie | facc8a22 | 2013-07-25 19:22:34 +0800 | [diff] [blame] | 7132 | int sum_len; |
Christoph Hellwig | 7b6d91d | 2010-08-07 18:20:39 +0200 | [diff] [blame] | 7133 | int write = rw & REQ_WRITE; |
Josef Bacik | 4b46fce | 2010-05-23 11:00:55 -0400 | [diff] [blame] | 7134 | int ret = 0; |
Miao Xie | facc8a22 | 2013-07-25 19:22:34 +0800 | [diff] [blame] | 7135 | u16 csum_size; |
Josef Bacik | 4b46fce | 2010-05-23 11:00:55 -0400 | [diff] [blame] | 7136 | |
| 7137 | skip_sum = BTRFS_I(inode)->flags & BTRFS_INODE_NODATASUM; |
| 7138 | |
Chris Mason | 9be3395 | 2013-05-17 18:30:14 -0400 | [diff] [blame] | 7139 | io_bio = btrfs_bio_clone(dio_bio, GFP_NOFS); |
Chris Mason | 9be3395 | 2013-05-17 18:30:14 -0400 | [diff] [blame] | 7140 | if (!io_bio) { |
Josef Bacik | 4b46fce | 2010-05-23 11:00:55 -0400 | [diff] [blame] | 7141 | ret = -ENOMEM; |
| 7142 | goto free_ordered; |
| 7143 | } |
Josef Bacik | 4b46fce | 2010-05-23 11:00:55 -0400 | [diff] [blame] | 7144 | |
Miao Xie | facc8a22 | 2013-07-25 19:22:34 +0800 | [diff] [blame] | 7145 | if (!skip_sum && !write) { |
| 7146 | csum_size = btrfs_super_csum_size(root->fs_info->super_copy); |
| 7147 | sum_len = dio_bio->bi_size >> inode->i_sb->s_blocksize_bits; |
| 7148 | sum_len *= csum_size; |
| 7149 | } else { |
| 7150 | sum_len = 0; |
| 7151 | } |
| 7152 | |
| 7153 | dip = kmalloc(sizeof(*dip) + sum_len, GFP_NOFS); |
Chris Mason | 9be3395 | 2013-05-17 18:30:14 -0400 | [diff] [blame] | 7154 | if (!dip) { |
| 7155 | ret = -ENOMEM; |
| 7156 | goto free_io_bio; |
| 7157 | } |
| 7158 | |
| 7159 | dip->private = dio_bio->bi_private; |
Josef Bacik | 4b46fce | 2010-05-23 11:00:55 -0400 | [diff] [blame] | 7160 | dip->inode = inode; |
| 7161 | dip->logical_offset = file_offset; |
Miao Xie | e6da5d2e | 2013-06-19 18:19:17 +0800 | [diff] [blame] | 7162 | dip->bytes = dio_bio->bi_size; |
Chris Mason | 9be3395 | 2013-05-17 18:30:14 -0400 | [diff] [blame] | 7163 | dip->disk_bytenr = (u64)dio_bio->bi_sector << 9; |
| 7164 | io_bio->bi_private = dip; |
Miao Xie | e65e153 | 2010-11-22 03:04:43 +0000 | [diff] [blame] | 7165 | dip->errors = 0; |
Chris Mason | 9be3395 | 2013-05-17 18:30:14 -0400 | [diff] [blame] | 7166 | dip->orig_bio = io_bio; |
| 7167 | dip->dio_bio = dio_bio; |
Miao Xie | e65e153 | 2010-11-22 03:04:43 +0000 | [diff] [blame] | 7168 | atomic_set(&dip->pending_bios, 0); |
Josef Bacik | 4b46fce | 2010-05-23 11:00:55 -0400 | [diff] [blame] | 7169 | |
| 7170 | if (write) |
Chris Mason | 9be3395 | 2013-05-17 18:30:14 -0400 | [diff] [blame] | 7171 | io_bio->bi_end_io = btrfs_endio_direct_write; |
Josef Bacik | 4b46fce | 2010-05-23 11:00:55 -0400 | [diff] [blame] | 7172 | else |
Chris Mason | 9be3395 | 2013-05-17 18:30:14 -0400 | [diff] [blame] | 7173 | io_bio->bi_end_io = btrfs_endio_direct_read; |
Josef Bacik | 4b46fce | 2010-05-23 11:00:55 -0400 | [diff] [blame] | 7174 | |
Miao Xie | e65e153 | 2010-11-22 03:04:43 +0000 | [diff] [blame] | 7175 | ret = btrfs_submit_direct_hook(rw, dip, skip_sum); |
| 7176 | if (!ret) |
Chris Mason | eaf25d9 | 2010-05-25 09:48:28 -0400 | [diff] [blame] | 7177 | return; |
Chris Mason | 9be3395 | 2013-05-17 18:30:14 -0400 | [diff] [blame] | 7178 | |
| 7179 | free_io_bio: |
| 7180 | bio_put(io_bio); |
| 7181 | |
Josef Bacik | 4b46fce | 2010-05-23 11:00:55 -0400 | [diff] [blame] | 7182 | free_ordered: |
| 7183 | /* |
| 7184 | * If this is a write, we need to clean up the reserved space and kill |
| 7185 | * the ordered extent. |
| 7186 | */ |
| 7187 | if (write) { |
| 7188 | struct btrfs_ordered_extent *ordered; |
Josef Bacik | 955256f | 2010-11-19 09:41:10 -0500 | [diff] [blame] | 7189 | ordered = btrfs_lookup_ordered_extent(inode, file_offset); |
Josef Bacik | 4b46fce | 2010-05-23 11:00:55 -0400 | [diff] [blame] | 7190 | if (!test_bit(BTRFS_ORDERED_PREALLOC, &ordered->flags) && |
| 7191 | !test_bit(BTRFS_ORDERED_NOCOW, &ordered->flags)) |
| 7192 | btrfs_free_reserved_extent(root, ordered->start, |
| 7193 | ordered->disk_len); |
| 7194 | btrfs_put_ordered_extent(ordered); |
| 7195 | btrfs_put_ordered_extent(ordered); |
| 7196 | } |
Chris Mason | 9be3395 | 2013-05-17 18:30:14 -0400 | [diff] [blame] | 7197 | bio_endio(dio_bio, ret); |
Josef Bacik | 4b46fce | 2010-05-23 11:00:55 -0400 | [diff] [blame] | 7198 | } |
| 7199 | |
Chris Mason | 5a5f79b | 2010-05-26 21:33:37 -0400 | [diff] [blame] | 7200 | static ssize_t check_direct_IO(struct btrfs_root *root, int rw, struct kiocb *iocb, |
| 7201 | const struct iovec *iov, loff_t offset, |
| 7202 | unsigned long nr_segs) |
| 7203 | { |
| 7204 | int seg; |
Josef Bacik | a1b75f7 | 2011-04-08 15:51:18 +0000 | [diff] [blame] | 7205 | int i; |
Chris Mason | 5a5f79b | 2010-05-26 21:33:37 -0400 | [diff] [blame] | 7206 | size_t size; |
| 7207 | unsigned long addr; |
| 7208 | unsigned blocksize_mask = root->sectorsize - 1; |
| 7209 | ssize_t retval = -EINVAL; |
| 7210 | loff_t end = offset; |
| 7211 | |
| 7212 | if (offset & blocksize_mask) |
| 7213 | goto out; |
| 7214 | |
| 7215 | /* Check the memory alignment. Blocks cannot straddle pages */ |
| 7216 | for (seg = 0; seg < nr_segs; seg++) { |
| 7217 | addr = (unsigned long)iov[seg].iov_base; |
| 7218 | size = iov[seg].iov_len; |
| 7219 | end += size; |
Josef Bacik | a1b75f7 | 2011-04-08 15:51:18 +0000 | [diff] [blame] | 7220 | if ((addr & blocksize_mask) || (size & blocksize_mask)) |
Chris Mason | 5a5f79b | 2010-05-26 21:33:37 -0400 | [diff] [blame] | 7221 | goto out; |
Josef Bacik | a1b75f7 | 2011-04-08 15:51:18 +0000 | [diff] [blame] | 7222 | |
| 7223 | /* If this is a write we don't need to check anymore */ |
| 7224 | if (rw & WRITE) |
| 7225 | continue; |
| 7226 | |
| 7227 | /* |
| 7228 | * Check to make sure we don't have duplicate iov_base's in this |
| 7229 | * iovec, if so return EINVAL, otherwise we'll get csum errors |
| 7230 | * when reading back. |
| 7231 | */ |
| 7232 | for (i = seg + 1; i < nr_segs; i++) { |
| 7233 | if (iov[seg].iov_base == iov[i].iov_base) |
| 7234 | goto out; |
| 7235 | } |
Chris Mason | 5a5f79b | 2010-05-26 21:33:37 -0400 | [diff] [blame] | 7236 | } |
| 7237 | retval = 0; |
| 7238 | out: |
| 7239 | return retval; |
| 7240 | } |
Josef Bacik | eb838e7 | 2012-07-31 16:28:48 -0400 | [diff] [blame] | 7241 | |
Chris Mason | 1643298 | 2008-04-10 10:23:21 -0400 | [diff] [blame] | 7242 | static ssize_t btrfs_direct_IO(int rw, struct kiocb *iocb, |
| 7243 | const struct iovec *iov, loff_t offset, |
| 7244 | unsigned long nr_segs) |
| 7245 | { |
Josef Bacik | 4b46fce | 2010-05-23 11:00:55 -0400 | [diff] [blame] | 7246 | struct file *file = iocb->ki_filp; |
| 7247 | struct inode *inode = file->f_mapping->host; |
Miao Xie | 0934856 | 2013-02-07 10:12:07 +0000 | [diff] [blame] | 7248 | size_t count = 0; |
Miao Xie | 2e60a51 | 2013-02-08 07:01:08 +0000 | [diff] [blame] | 7249 | int flags = 0; |
Miao Xie | 38851cc | 2013-02-08 07:04:11 +0000 | [diff] [blame] | 7250 | bool wakeup = true; |
| 7251 | bool relock = false; |
Miao Xie | 0934856 | 2013-02-07 10:12:07 +0000 | [diff] [blame] | 7252 | ssize_t ret; |
Josef Bacik | 4b46fce | 2010-05-23 11:00:55 -0400 | [diff] [blame] | 7253 | |
Chris Mason | 5a5f79b | 2010-05-26 21:33:37 -0400 | [diff] [blame] | 7254 | if (check_direct_IO(BTRFS_I(inode)->root, rw, iocb, iov, |
Josef Bacik | eb838e7 | 2012-07-31 16:28:48 -0400 | [diff] [blame] | 7255 | offset, nr_segs)) |
Chris Mason | 5a5f79b | 2010-05-26 21:33:37 -0400 | [diff] [blame] | 7256 | return 0; |
Chris Mason | 5a5f79b | 2010-05-26 21:33:37 -0400 | [diff] [blame] | 7257 | |
Miao Xie | 38851cc | 2013-02-08 07:04:11 +0000 | [diff] [blame] | 7258 | atomic_inc(&inode->i_dio_count); |
| 7259 | smp_mb__after_atomic_inc(); |
| 7260 | |
Josef Bacik | 0e267c4 | 2013-07-02 10:38:02 -0400 | [diff] [blame] | 7261 | /* |
| 7262 | * The generic stuff only does filemap_write_and_wait_range, which isn't |
| 7263 | * enough if we've written compressed pages to this area, so we need to |
| 7264 | * call btrfs_wait_ordered_range to make absolutely sure that any |
| 7265 | * outstanding dirty pages are on disk. |
| 7266 | */ |
| 7267 | count = iov_length(iov, nr_segs); |
| 7268 | btrfs_wait_ordered_range(inode, offset, count); |
| 7269 | |
Miao Xie | 0934856 | 2013-02-07 10:12:07 +0000 | [diff] [blame] | 7270 | if (rw & WRITE) { |
Miao Xie | 38851cc | 2013-02-08 07:04:11 +0000 | [diff] [blame] | 7271 | /* |
| 7272 | * If the write DIO is beyond the EOF, we need update |
| 7273 | * the isize, but it is protected by i_mutex. So we can |
| 7274 | * not unlock the i_mutex at this case. |
| 7275 | */ |
| 7276 | if (offset + count <= inode->i_size) { |
| 7277 | mutex_unlock(&inode->i_mutex); |
| 7278 | relock = true; |
| 7279 | } |
Miao Xie | 0934856 | 2013-02-07 10:12:07 +0000 | [diff] [blame] | 7280 | ret = btrfs_delalloc_reserve_space(inode, count); |
| 7281 | if (ret) |
Miao Xie | 38851cc | 2013-02-08 07:04:11 +0000 | [diff] [blame] | 7282 | goto out; |
| 7283 | } else if (unlikely(test_bit(BTRFS_INODE_READDIO_NEED_LOCK, |
| 7284 | &BTRFS_I(inode)->runtime_flags))) { |
| 7285 | inode_dio_done(inode); |
| 7286 | flags = DIO_LOCKING | DIO_SKIP_HOLES; |
| 7287 | wakeup = false; |
Miao Xie | 0934856 | 2013-02-07 10:12:07 +0000 | [diff] [blame] | 7288 | } |
| 7289 | |
| 7290 | ret = __blockdev_direct_IO(rw, iocb, inode, |
| 7291 | BTRFS_I(inode)->root->fs_info->fs_devices->latest_bdev, |
| 7292 | iov, offset, nr_segs, btrfs_get_blocks_direct, NULL, |
Miao Xie | 2e60a51 | 2013-02-08 07:01:08 +0000 | [diff] [blame] | 7293 | btrfs_submit_direct, flags); |
Miao Xie | 0934856 | 2013-02-07 10:12:07 +0000 | [diff] [blame] | 7294 | if (rw & WRITE) { |
| 7295 | if (ret < 0 && ret != -EIOCBQUEUED) |
| 7296 | btrfs_delalloc_release_space(inode, count); |
Miao Xie | 172a504 | 2013-02-21 02:48:22 -0700 | [diff] [blame] | 7297 | else if (ret >= 0 && (size_t)ret < count) |
Miao Xie | 0934856 | 2013-02-07 10:12:07 +0000 | [diff] [blame] | 7298 | btrfs_delalloc_release_space(inode, |
| 7299 | count - (size_t)ret); |
Miao Xie | 172a504 | 2013-02-21 02:48:22 -0700 | [diff] [blame] | 7300 | else |
| 7301 | btrfs_delalloc_release_metadata(inode, 0); |
Miao Xie | 0934856 | 2013-02-07 10:12:07 +0000 | [diff] [blame] | 7302 | } |
Miao Xie | 38851cc | 2013-02-08 07:04:11 +0000 | [diff] [blame] | 7303 | out: |
Miao Xie | 2e60a51 | 2013-02-08 07:01:08 +0000 | [diff] [blame] | 7304 | if (wakeup) |
| 7305 | inode_dio_done(inode); |
Miao Xie | 38851cc | 2013-02-08 07:04:11 +0000 | [diff] [blame] | 7306 | if (relock) |
| 7307 | mutex_lock(&inode->i_mutex); |
Miao Xie | 0934856 | 2013-02-07 10:12:07 +0000 | [diff] [blame] | 7308 | |
| 7309 | return ret; |
Chris Mason | 1643298 | 2008-04-10 10:23:21 -0400 | [diff] [blame] | 7310 | } |
| 7311 | |
Tsutomu Itoh | 05dadc0 | 2012-11-29 05:08:26 +0000 | [diff] [blame] | 7312 | #define BTRFS_FIEMAP_FLAGS (FIEMAP_FLAG_SYNC) |
| 7313 | |
Yehuda Sadeh | 1506fcc | 2009-01-21 14:39:14 -0500 | [diff] [blame] | 7314 | static int btrfs_fiemap(struct inode *inode, struct fiemap_extent_info *fieinfo, |
| 7315 | __u64 start, __u64 len) |
| 7316 | { |
Tsutomu Itoh | 05dadc0 | 2012-11-29 05:08:26 +0000 | [diff] [blame] | 7317 | int ret; |
| 7318 | |
| 7319 | ret = fiemap_check_flags(fieinfo, BTRFS_FIEMAP_FLAGS); |
| 7320 | if (ret) |
| 7321 | return ret; |
| 7322 | |
Chris Mason | ec29ed5 | 2011-02-23 16:23:20 -0500 | [diff] [blame] | 7323 | return extent_fiemap(inode, fieinfo, start, len, btrfs_get_extent_fiemap); |
Yehuda Sadeh | 1506fcc | 2009-01-21 14:39:14 -0500 | [diff] [blame] | 7324 | } |
| 7325 | |
Chris Mason | 9ebefb18 | 2007-06-15 13:50:00 -0400 | [diff] [blame] | 7326 | int btrfs_readpage(struct file *file, struct page *page) |
| 7327 | { |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 7328 | struct extent_io_tree *tree; |
| 7329 | tree = &BTRFS_I(page->mapping->host)->io_tree; |
Jan Schmidt | 8ddc7d9 | 2011-06-13 20:02:58 +0200 | [diff] [blame] | 7330 | return extent_read_full_page(tree, page, btrfs_get_extent, 0); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 7331 | } |
Chris Mason | 1832a6d | 2007-12-21 16:27:21 -0500 | [diff] [blame] | 7332 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 7333 | static int btrfs_writepage(struct page *page, struct writeback_control *wbc) |
| 7334 | { |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 7335 | struct extent_io_tree *tree; |
Chris Mason | b888db2 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 7336 | |
| 7337 | |
| 7338 | if (current->flags & PF_MEMALLOC) { |
| 7339 | redirty_page_for_writepage(wbc, page); |
| 7340 | unlock_page(page); |
| 7341 | return 0; |
| 7342 | } |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 7343 | tree = &BTRFS_I(page->mapping->host)->io_tree; |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 7344 | return extent_write_full_page(tree, page, btrfs_get_extent, wbc); |
| 7345 | } |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 7346 | |
Eric Sandeen | 48a3b63 | 2013-04-25 20:41:01 +0000 | [diff] [blame] | 7347 | static int btrfs_writepages(struct address_space *mapping, |
| 7348 | struct writeback_control *wbc) |
Chris Mason | b293f02e | 2007-11-01 19:45:34 -0400 | [diff] [blame] | 7349 | { |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 7350 | struct extent_io_tree *tree; |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 7351 | |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 7352 | tree = &BTRFS_I(mapping->host)->io_tree; |
Chris Mason | b293f02e | 2007-11-01 19:45:34 -0400 | [diff] [blame] | 7353 | return extent_writepages(tree, mapping, btrfs_get_extent, wbc); |
| 7354 | } |
| 7355 | |
Chris Mason | 3ab2fb5 | 2007-11-08 10:59:22 -0500 | [diff] [blame] | 7356 | static int |
| 7357 | btrfs_readpages(struct file *file, struct address_space *mapping, |
| 7358 | struct list_head *pages, unsigned nr_pages) |
| 7359 | { |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 7360 | struct extent_io_tree *tree; |
| 7361 | tree = &BTRFS_I(mapping->host)->io_tree; |
Chris Mason | 3ab2fb5 | 2007-11-08 10:59:22 -0500 | [diff] [blame] | 7362 | return extent_readpages(tree, mapping, pages, nr_pages, |
| 7363 | btrfs_get_extent); |
| 7364 | } |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 7365 | static int __btrfs_releasepage(struct page *page, gfp_t gfp_flags) |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 7366 | { |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 7367 | struct extent_io_tree *tree; |
| 7368 | struct extent_map_tree *map; |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 7369 | int ret; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 7370 | |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 7371 | tree = &BTRFS_I(page->mapping->host)->io_tree; |
| 7372 | map = &BTRFS_I(page->mapping->host)->extent_tree; |
Chris Mason | 70dec80 | 2008-01-29 09:59:12 -0500 | [diff] [blame] | 7373 | ret = try_release_extent_mapping(map, tree, page, gfp_flags); |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 7374 | if (ret == 1) { |
| 7375 | ClearPagePrivate(page); |
| 7376 | set_page_private(page, 0); |
| 7377 | page_cache_release(page); |
| 7378 | } |
| 7379 | return ret; |
| 7380 | } |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 7381 | |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 7382 | static int btrfs_releasepage(struct page *page, gfp_t gfp_flags) |
| 7383 | { |
Chris Mason | 98509cf | 2008-09-11 15:51:43 -0400 | [diff] [blame] | 7384 | if (PageWriteback(page) || PageDirty(page)) |
| 7385 | return 0; |
Yan Zheng | b335b00 | 2009-02-12 10:06:04 -0500 | [diff] [blame] | 7386 | return __btrfs_releasepage(page, gfp_flags & GFP_NOFS); |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 7387 | } |
| 7388 | |
Lukas Czerner | d47992f | 2013-05-21 23:17:23 -0400 | [diff] [blame] | 7389 | static void btrfs_invalidatepage(struct page *page, unsigned int offset, |
| 7390 | unsigned int length) |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 7391 | { |
Josef Bacik | 5fd0204 | 2012-05-02 14:00:54 -0400 | [diff] [blame] | 7392 | struct inode *inode = page->mapping->host; |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 7393 | struct extent_io_tree *tree; |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 7394 | struct btrfs_ordered_extent *ordered; |
Josef Bacik | 2ac55d4 | 2010-02-03 19:33:23 +0000 | [diff] [blame] | 7395 | struct extent_state *cached_state = NULL; |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 7396 | u64 page_start = page_offset(page); |
| 7397 | u64 page_end = page_start + PAGE_CACHE_SIZE - 1; |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 7398 | |
Chris Mason | 8b62b72 | 2009-09-02 16:53:46 -0400 | [diff] [blame] | 7399 | /* |
| 7400 | * we have the page locked, so new writeback can't start, |
| 7401 | * and the dirty bit won't be cleared while we are here. |
| 7402 | * |
| 7403 | * Wait for IO on this page so that we can safely clear |
| 7404 | * the PagePrivate2 bit and do ordered accounting |
| 7405 | */ |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 7406 | wait_on_page_writeback(page); |
Chris Mason | 8b62b72 | 2009-09-02 16:53:46 -0400 | [diff] [blame] | 7407 | |
Josef Bacik | 5fd0204 | 2012-05-02 14:00:54 -0400 | [diff] [blame] | 7408 | tree = &BTRFS_I(inode)->io_tree; |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 7409 | if (offset) { |
| 7410 | btrfs_releasepage(page, GFP_NOFS); |
| 7411 | return; |
| 7412 | } |
Jeff Mahoney | d008237 | 2012-03-01 14:57:19 +0100 | [diff] [blame] | 7413 | lock_extent_bits(tree, page_start, page_end, 0, &cached_state); |
Miao Xie | 4eee4fa | 2012-12-21 09:17:45 +0000 | [diff] [blame] | 7414 | ordered = btrfs_lookup_ordered_extent(inode, page_offset(page)); |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 7415 | if (ordered) { |
Chris Mason | eb84ae0 | 2008-07-17 13:53:27 -0400 | [diff] [blame] | 7416 | /* |
| 7417 | * IO on this page will never be started, so we need |
| 7418 | * to account for any ordered extents now |
| 7419 | */ |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 7420 | clear_extent_bit(tree, page_start, page_end, |
| 7421 | EXTENT_DIRTY | EXTENT_DELALLOC | |
Liu Bo | 9e8a4a8 | 2012-09-05 19:10:51 -0600 | [diff] [blame] | 7422 | EXTENT_LOCKED | EXTENT_DO_ACCOUNTING | |
| 7423 | EXTENT_DEFRAG, 1, 0, &cached_state, GFP_NOFS); |
Chris Mason | 8b62b72 | 2009-09-02 16:53:46 -0400 | [diff] [blame] | 7424 | /* |
| 7425 | * whoever cleared the private bit is responsible |
| 7426 | * for the finish_ordered_io |
| 7427 | */ |
Josef Bacik | 5fd0204 | 2012-05-02 14:00:54 -0400 | [diff] [blame] | 7428 | if (TestClearPagePrivate2(page) && |
| 7429 | btrfs_dec_test_ordered_pending(inode, &ordered, page_start, |
| 7430 | PAGE_CACHE_SIZE, 1)) { |
| 7431 | btrfs_finish_ordered_io(ordered); |
Chris Mason | 8b62b72 | 2009-09-02 16:53:46 -0400 | [diff] [blame] | 7432 | } |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 7433 | btrfs_put_ordered_extent(ordered); |
Josef Bacik | 2ac55d4 | 2010-02-03 19:33:23 +0000 | [diff] [blame] | 7434 | cached_state = NULL; |
Jeff Mahoney | d008237 | 2012-03-01 14:57:19 +0100 | [diff] [blame] | 7435 | lock_extent_bits(tree, page_start, page_end, 0, &cached_state); |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 7436 | } |
| 7437 | clear_extent_bit(tree, page_start, page_end, |
Josef Bacik | 32c00af | 2009-10-08 13:34:05 -0400 | [diff] [blame] | 7438 | EXTENT_LOCKED | EXTENT_DIRTY | EXTENT_DELALLOC | |
Liu Bo | 9e8a4a8 | 2012-09-05 19:10:51 -0600 | [diff] [blame] | 7439 | EXTENT_DO_ACCOUNTING | EXTENT_DEFRAG, 1, 1, |
| 7440 | &cached_state, GFP_NOFS); |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 7441 | __btrfs_releasepage(page, GFP_NOFS); |
| 7442 | |
Chris Mason | 4a09675 | 2008-07-21 10:29:44 -0400 | [diff] [blame] | 7443 | ClearPageChecked(page); |
Chris Mason | 9ad6b7bc | 2008-04-18 16:11:30 -0400 | [diff] [blame] | 7444 | if (PagePrivate(page)) { |
Chris Mason | 9ad6b7bc | 2008-04-18 16:11:30 -0400 | [diff] [blame] | 7445 | ClearPagePrivate(page); |
| 7446 | set_page_private(page, 0); |
| 7447 | page_cache_release(page); |
| 7448 | } |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 7449 | } |
| 7450 | |
Chris Mason | 9ebefb18 | 2007-06-15 13:50:00 -0400 | [diff] [blame] | 7451 | /* |
| 7452 | * btrfs_page_mkwrite() is not allowed to change the file size as it gets |
| 7453 | * called from a page fault handler when a page is first dirtied. Hence we must |
| 7454 | * be careful to check for EOF conditions here. We set the page up correctly |
| 7455 | * for a written page which means we get ENOSPC checking when writing into |
| 7456 | * holes and correct delalloc and unwritten extent mapping on filesystems that |
| 7457 | * support these features. |
| 7458 | * |
| 7459 | * We are not allowed to take the i_mutex here so we have to play games to |
| 7460 | * protect against truncate races as the page could now be beyond EOF. Because |
| 7461 | * vmtruncate() writes the inode size before removing pages, once we have the |
| 7462 | * page lock we can determine safely if the page is beyond EOF. If it is not |
| 7463 | * beyond EOF, then the page is guaranteed safe against truncation until we |
| 7464 | * unlock the page. |
| 7465 | */ |
Nick Piggin | c2ec175 | 2009-03-31 15:23:21 -0700 | [diff] [blame] | 7466 | 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] | 7467 | { |
Nick Piggin | c2ec175 | 2009-03-31 15:23:21 -0700 | [diff] [blame] | 7468 | struct page *page = vmf->page; |
Al Viro | 496ad9a | 2013-01-23 17:07:38 -0500 | [diff] [blame] | 7469 | struct inode *inode = file_inode(vma->vm_file); |
Chris Mason | 1832a6d | 2007-12-21 16:27:21 -0500 | [diff] [blame] | 7470 | struct btrfs_root *root = BTRFS_I(inode)->root; |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 7471 | struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree; |
| 7472 | struct btrfs_ordered_extent *ordered; |
Josef Bacik | 2ac55d4 | 2010-02-03 19:33:23 +0000 | [diff] [blame] | 7473 | struct extent_state *cached_state = NULL; |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 7474 | char *kaddr; |
| 7475 | unsigned long zero_start; |
Chris Mason | 9ebefb18 | 2007-06-15 13:50:00 -0400 | [diff] [blame] | 7476 | loff_t size; |
Chris Mason | 1832a6d | 2007-12-21 16:27:21 -0500 | [diff] [blame] | 7477 | int ret; |
Chris Mason | 9998eb7 | 2012-01-25 13:47:40 -0500 | [diff] [blame] | 7478 | int reserved = 0; |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 7479 | u64 page_start; |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 7480 | u64 page_end; |
Chris Mason | 9ebefb18 | 2007-06-15 13:50:00 -0400 | [diff] [blame] | 7481 | |
Jan Kara | b2b5ef5 | 2012-06-12 16:20:45 +0200 | [diff] [blame] | 7482 | sb_start_pagefault(inode->i_sb); |
Yan, Zheng | 0ca1f7c | 2010-05-16 10:48:47 -0400 | [diff] [blame] | 7483 | ret = btrfs_delalloc_reserve_space(inode, PAGE_CACHE_SIZE); |
Chris Mason | 9998eb7 | 2012-01-25 13:47:40 -0500 | [diff] [blame] | 7484 | if (!ret) { |
Josef Bacik | e41f941 | 2012-03-26 09:46:47 -0400 | [diff] [blame] | 7485 | ret = file_update_time(vma->vm_file); |
Chris Mason | 9998eb7 | 2012-01-25 13:47:40 -0500 | [diff] [blame] | 7486 | reserved = 1; |
| 7487 | } |
Nick Piggin | 56a76f8 | 2009-03-31 15:23:23 -0700 | [diff] [blame] | 7488 | if (ret) { |
| 7489 | if (ret == -ENOMEM) |
| 7490 | ret = VM_FAULT_OOM; |
| 7491 | else /* -ENOSPC, -EIO, etc */ |
| 7492 | ret = VM_FAULT_SIGBUS; |
Chris Mason | 9998eb7 | 2012-01-25 13:47:40 -0500 | [diff] [blame] | 7493 | if (reserved) |
| 7494 | goto out; |
| 7495 | goto out_noreserve; |
Nick Piggin | 56a76f8 | 2009-03-31 15:23:23 -0700 | [diff] [blame] | 7496 | } |
Chris Mason | 1832a6d | 2007-12-21 16:27:21 -0500 | [diff] [blame] | 7497 | |
Nick Piggin | 56a76f8 | 2009-03-31 15:23:23 -0700 | [diff] [blame] | 7498 | ret = VM_FAULT_NOPAGE; /* make the VM retry the fault */ |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 7499 | again: |
Chris Mason | 9ebefb18 | 2007-06-15 13:50:00 -0400 | [diff] [blame] | 7500 | lock_page(page); |
Chris Mason | 9ebefb18 | 2007-06-15 13:50:00 -0400 | [diff] [blame] | 7501 | size = i_size_read(inode); |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 7502 | page_start = page_offset(page); |
| 7503 | page_end = page_start + PAGE_CACHE_SIZE - 1; |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 7504 | |
Chris Mason | 9ebefb18 | 2007-06-15 13:50:00 -0400 | [diff] [blame] | 7505 | if ((page->mapping != inode->i_mapping) || |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 7506 | (page_start >= size)) { |
Chris Mason | 9ebefb18 | 2007-06-15 13:50:00 -0400 | [diff] [blame] | 7507 | /* page got truncated out from underneath us */ |
| 7508 | goto out_unlock; |
| 7509 | } |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 7510 | wait_on_page_writeback(page); |
| 7511 | |
Jeff Mahoney | d008237 | 2012-03-01 14:57:19 +0100 | [diff] [blame] | 7512 | lock_extent_bits(io_tree, page_start, page_end, 0, &cached_state); |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 7513 | set_page_extent_mapped(page); |
| 7514 | |
Chris Mason | eb84ae0 | 2008-07-17 13:53:27 -0400 | [diff] [blame] | 7515 | /* |
| 7516 | * we can't set the delalloc bits if there are pending ordered |
| 7517 | * extents. Drop our locks and wait for them to finish |
| 7518 | */ |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 7519 | ordered = btrfs_lookup_ordered_extent(inode, page_start); |
| 7520 | if (ordered) { |
Josef Bacik | 2ac55d4 | 2010-02-03 19:33:23 +0000 | [diff] [blame] | 7521 | unlock_extent_cached(io_tree, page_start, page_end, |
| 7522 | &cached_state, GFP_NOFS); |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 7523 | unlock_page(page); |
Chris Mason | eb84ae0 | 2008-07-17 13:53:27 -0400 | [diff] [blame] | 7524 | btrfs_start_ordered_extent(inode, ordered, 1); |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 7525 | btrfs_put_ordered_extent(ordered); |
| 7526 | goto again; |
| 7527 | } |
| 7528 | |
Josef Bacik | fbf1908 | 2009-10-01 17:10:23 -0400 | [diff] [blame] | 7529 | /* |
| 7530 | * XXX - page_mkwrite gets called every time the page is dirtied, even |
| 7531 | * if it was already dirty, so for space accounting reasons we need to |
| 7532 | * clear any delalloc bits for the range we are fixing to save. There |
| 7533 | * is probably a better way to do this, but for now keep consistent with |
| 7534 | * prepare_pages in the normal write path. |
| 7535 | */ |
Josef Bacik | 2ac55d4 | 2010-02-03 19:33:23 +0000 | [diff] [blame] | 7536 | clear_extent_bit(&BTRFS_I(inode)->io_tree, page_start, page_end, |
Liu Bo | 9e8a4a8 | 2012-09-05 19:10:51 -0600 | [diff] [blame] | 7537 | EXTENT_DIRTY | EXTENT_DELALLOC | |
| 7538 | EXTENT_DO_ACCOUNTING | EXTENT_DEFRAG, |
Josef Bacik | 2ac55d4 | 2010-02-03 19:33:23 +0000 | [diff] [blame] | 7539 | 0, 0, &cached_state, GFP_NOFS); |
Josef Bacik | fbf1908 | 2009-10-01 17:10:23 -0400 | [diff] [blame] | 7540 | |
Josef Bacik | 2ac55d4 | 2010-02-03 19:33:23 +0000 | [diff] [blame] | 7541 | ret = btrfs_set_extent_delalloc(inode, page_start, page_end, |
| 7542 | &cached_state); |
Josef Bacik | 9ed74f2 | 2009-09-11 16:12:44 -0400 | [diff] [blame] | 7543 | if (ret) { |
Josef Bacik | 2ac55d4 | 2010-02-03 19:33:23 +0000 | [diff] [blame] | 7544 | unlock_extent_cached(io_tree, page_start, page_end, |
| 7545 | &cached_state, GFP_NOFS); |
Josef Bacik | 9ed74f2 | 2009-09-11 16:12:44 -0400 | [diff] [blame] | 7546 | ret = VM_FAULT_SIGBUS; |
| 7547 | goto out_unlock; |
| 7548 | } |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 7549 | ret = 0; |
Chris Mason | 9ebefb18 | 2007-06-15 13:50:00 -0400 | [diff] [blame] | 7550 | |
| 7551 | /* page is wholly or partially inside EOF */ |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 7552 | if (page_start + PAGE_CACHE_SIZE > size) |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 7553 | zero_start = size & ~PAGE_CACHE_MASK; |
Chris Mason | 9ebefb18 | 2007-06-15 13:50:00 -0400 | [diff] [blame] | 7554 | else |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 7555 | zero_start = PAGE_CACHE_SIZE; |
Chris Mason | 9ebefb18 | 2007-06-15 13:50:00 -0400 | [diff] [blame] | 7556 | |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 7557 | if (zero_start != PAGE_CACHE_SIZE) { |
| 7558 | kaddr = kmap(page); |
| 7559 | memset(kaddr + zero_start, 0, PAGE_CACHE_SIZE - zero_start); |
| 7560 | flush_dcache_page(page); |
| 7561 | kunmap(page); |
| 7562 | } |
Chris Mason | 247e743 | 2008-07-17 12:53:51 -0400 | [diff] [blame] | 7563 | ClearPageChecked(page); |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 7564 | set_page_dirty(page); |
Chris Mason | 50a9b21 | 2009-09-11 12:33:12 -0400 | [diff] [blame] | 7565 | SetPageUptodate(page); |
Chris Mason | 5a3f23d | 2009-03-31 13:27:11 -0400 | [diff] [blame] | 7566 | |
Chris Mason | 257c62e | 2009-10-13 13:21:08 -0400 | [diff] [blame] | 7567 | BTRFS_I(inode)->last_trans = root->fs_info->generation; |
| 7568 | BTRFS_I(inode)->last_sub_trans = BTRFS_I(inode)->root->log_transid; |
Liu Bo | 46d8bc3 | 2012-08-29 01:07:55 -0600 | [diff] [blame] | 7569 | 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] | 7570 | |
Josef Bacik | 2ac55d4 | 2010-02-03 19:33:23 +0000 | [diff] [blame] | 7571 | 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] | 7572 | |
| 7573 | out_unlock: |
Jan Kara | b2b5ef5 | 2012-06-12 16:20:45 +0200 | [diff] [blame] | 7574 | if (!ret) { |
| 7575 | sb_end_pagefault(inode->i_sb); |
Chris Mason | 50a9b21 | 2009-09-11 12:33:12 -0400 | [diff] [blame] | 7576 | return VM_FAULT_LOCKED; |
Jan Kara | b2b5ef5 | 2012-06-12 16:20:45 +0200 | [diff] [blame] | 7577 | } |
Chris Mason | 9ebefb18 | 2007-06-15 13:50:00 -0400 | [diff] [blame] | 7578 | unlock_page(page); |
Chris Mason | 1832a6d | 2007-12-21 16:27:21 -0500 | [diff] [blame] | 7579 | out: |
Josef Bacik | ec39e18 | 2012-01-12 19:10:12 -0500 | [diff] [blame] | 7580 | btrfs_delalloc_release_space(inode, PAGE_CACHE_SIZE); |
Chris Mason | 9998eb7 | 2012-01-25 13:47:40 -0500 | [diff] [blame] | 7581 | out_noreserve: |
Jan Kara | b2b5ef5 | 2012-06-12 16:20:45 +0200 | [diff] [blame] | 7582 | sb_end_pagefault(inode->i_sb); |
Chris Mason | 9ebefb18 | 2007-06-15 13:50:00 -0400 | [diff] [blame] | 7583 | return ret; |
| 7584 | } |
| 7585 | |
Josef Bacik | a41ad39 | 2011-01-31 15:30:16 -0500 | [diff] [blame] | 7586 | static int btrfs_truncate(struct inode *inode) |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 7587 | { |
| 7588 | struct btrfs_root *root = BTRFS_I(inode)->root; |
Josef Bacik | fcb80c2 | 2011-05-03 10:40:22 -0400 | [diff] [blame] | 7589 | struct btrfs_block_rsv *rsv; |
Josef Bacik | a71754f | 2013-06-17 17:14:39 -0400 | [diff] [blame] | 7590 | int ret = 0; |
Josef Bacik | 3893e33 | 2011-01-31 16:03:11 -0500 | [diff] [blame] | 7591 | int err = 0; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 7592 | struct btrfs_trans_handle *trans; |
Chris Mason | dbe674a | 2008-07-17 12:54:05 -0400 | [diff] [blame] | 7593 | u64 mask = root->sectorsize - 1; |
Josef Bacik | 0712718 | 2011-08-19 10:29:59 -0400 | [diff] [blame] | 7594 | u64 min_size = btrfs_calc_trunc_metadata_size(root, 1); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 7595 | |
Chris Mason | 4a09675 | 2008-07-21 10:29:44 -0400 | [diff] [blame] | 7596 | btrfs_wait_ordered_range(inode, inode->i_size & (~mask), (u64)-1); |
Yan, Zheng | 8082510 | 2009-11-12 09:35:36 +0000 | [diff] [blame] | 7597 | btrfs_ordered_update_i_size(inode, inode->i_size, NULL); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 7598 | |
Josef Bacik | fcb80c2 | 2011-05-03 10:40:22 -0400 | [diff] [blame] | 7599 | /* |
| 7600 | * Yes ladies and gentelment, this is indeed ugly. The fact is we have |
| 7601 | * 3 things going on here |
| 7602 | * |
| 7603 | * 1) We need to reserve space for our orphan item and the space to |
| 7604 | * delete our orphan item. Lord knows we don't want to have a dangling |
| 7605 | * orphan item because we didn't reserve space to remove it. |
| 7606 | * |
| 7607 | * 2) We need to reserve space to update our inode. |
| 7608 | * |
| 7609 | * 3) We need to have something to cache all the space that is going to |
| 7610 | * be free'd up by the truncate operation, but also have some slack |
| 7611 | * space reserved in case it uses space during the truncate (thank you |
| 7612 | * very much snapshotting). |
| 7613 | * |
| 7614 | * And we need these to all be seperate. The fact is we can use alot of |
| 7615 | * space doing the truncate, and we have no earthly idea how much space |
| 7616 | * we will use, so we need the truncate reservation to be seperate so it |
| 7617 | * doesn't end up using space reserved for updating the inode or |
| 7618 | * removing the orphan item. We also need to be able to stop the |
| 7619 | * transaction and start a new one, which means we need to be able to |
| 7620 | * update the inode several times, and we have no idea of knowing how |
| 7621 | * many times that will be, so we can't just reserve 1 item for the |
| 7622 | * entirety of the opration, so that has to be done seperately as well. |
| 7623 | * Then there is the orphan item, which does indeed need to be held on |
| 7624 | * to for the whole operation, and we need nobody to touch this reserved |
| 7625 | * space except the orphan code. |
| 7626 | * |
| 7627 | * So that leaves us with |
| 7628 | * |
| 7629 | * 1) root->orphan_block_rsv - for the orphan deletion. |
| 7630 | * 2) rsv - for the truncate reservation, which we will steal from the |
| 7631 | * transaction reservation. |
| 7632 | * 3) fs_info->trans_block_rsv - this will have 1 items worth left for |
| 7633 | * updating the inode. |
| 7634 | */ |
Miao Xie | 66d8f3d | 2012-09-06 04:02:28 -0600 | [diff] [blame] | 7635 | rsv = btrfs_alloc_block_rsv(root, BTRFS_BLOCK_RSV_TEMP); |
Josef Bacik | fcb80c2 | 2011-05-03 10:40:22 -0400 | [diff] [blame] | 7636 | if (!rsv) |
| 7637 | return -ENOMEM; |
Josef Bacik | 4a33854 | 2011-08-29 11:01:31 -0400 | [diff] [blame] | 7638 | rsv->size = min_size; |
Josef Bacik | ca7e70f | 2012-08-27 17:48:15 -0400 | [diff] [blame] | 7639 | rsv->failfast = 1; |
Josef Bacik | f0cd846 | 2011-03-04 14:37:08 -0500 | [diff] [blame] | 7640 | |
Josef Bacik | 907cbce | 2011-08-08 13:46:15 -0400 | [diff] [blame] | 7641 | /* |
Josef Bacik | 0712718 | 2011-08-19 10:29:59 -0400 | [diff] [blame] | 7642 | * 1 for the truncate slack space |
Josef Bacik | 907cbce | 2011-08-08 13:46:15 -0400 | [diff] [blame] | 7643 | * 1 for updating the inode. |
| 7644 | */ |
Josef Bacik | f3fe820 | 2013-01-07 17:03:21 -0500 | [diff] [blame] | 7645 | trans = btrfs_start_transaction(root, 2); |
Josef Bacik | fcb80c2 | 2011-05-03 10:40:22 -0400 | [diff] [blame] | 7646 | if (IS_ERR(trans)) { |
| 7647 | err = PTR_ERR(trans); |
| 7648 | goto out; |
| 7649 | } |
Josef Bacik | f0cd846 | 2011-03-04 14:37:08 -0500 | [diff] [blame] | 7650 | |
Josef Bacik | 907cbce | 2011-08-08 13:46:15 -0400 | [diff] [blame] | 7651 | /* Migrate the slack space for the truncate to our reserve */ |
| 7652 | ret = btrfs_block_rsv_migrate(&root->fs_info->trans_block_rsv, rsv, |
| 7653 | min_size); |
Josef Bacik | fcb80c2 | 2011-05-03 10:40:22 -0400 | [diff] [blame] | 7654 | BUG_ON(ret); |
Josef Bacik | f0cd846 | 2011-03-04 14:37:08 -0500 | [diff] [blame] | 7655 | |
Chris Mason | 5a3f23d | 2009-03-31 13:27:11 -0400 | [diff] [blame] | 7656 | /* |
| 7657 | * setattr is responsible for setting the ordered_data_close flag, |
| 7658 | * but that is only tested during the last file release. That |
| 7659 | * could happen well after the next commit, leaving a great big |
| 7660 | * window where new writes may get lost if someone chooses to write |
| 7661 | * to this file after truncating to zero |
| 7662 | * |
| 7663 | * The inode doesn't have any dirty data here, and so if we commit |
| 7664 | * this is a noop. If someone immediately starts writing to the inode |
| 7665 | * it is very likely we'll catch some of their writes in this |
| 7666 | * transaction, and the commit will find this file on the ordered |
| 7667 | * data list with good things to send down. |
| 7668 | * |
| 7669 | * This is a best effort solution, there is still a window where |
| 7670 | * using truncate to replace the contents of the file will |
| 7671 | * end up with a zero length file after a crash. |
| 7672 | */ |
Josef Bacik | 72ac3c0 | 2012-05-23 14:13:11 -0400 | [diff] [blame] | 7673 | if (inode->i_size == 0 && test_bit(BTRFS_INODE_ORDERED_DATA_CLOSE, |
| 7674 | &BTRFS_I(inode)->runtime_flags)) |
Chris Mason | 5a3f23d | 2009-03-31 13:27:11 -0400 | [diff] [blame] | 7675 | btrfs_add_ordered_operation(trans, root, inode); |
| 7676 | |
Josef Bacik | 5dc562c | 2012-08-17 13:14:17 -0400 | [diff] [blame] | 7677 | /* |
| 7678 | * So if we truncate and then write and fsync we normally would just |
| 7679 | * write the extents that changed, which is a problem if we need to |
| 7680 | * first truncate that entire inode. So set this flag so we write out |
| 7681 | * all of the extents in the inode to the sync log so we're completely |
| 7682 | * safe. |
| 7683 | */ |
| 7684 | set_bit(BTRFS_INODE_NEEDS_FULL_SYNC, &BTRFS_I(inode)->runtime_flags); |
Josef Bacik | ca7e70f | 2012-08-27 17:48:15 -0400 | [diff] [blame] | 7685 | trans->block_rsv = rsv; |
Josef Bacik | 5dc562c | 2012-08-17 13:14:17 -0400 | [diff] [blame] | 7686 | |
Yan, Zheng | 8082510 | 2009-11-12 09:35:36 +0000 | [diff] [blame] | 7687 | while (1) { |
| 7688 | ret = btrfs_truncate_inode_items(trans, root, inode, |
| 7689 | inode->i_size, |
| 7690 | BTRFS_EXTENT_DATA_KEY); |
Josef Bacik | ca7e70f | 2012-08-27 17:48:15 -0400 | [diff] [blame] | 7691 | if (ret != -ENOSPC) { |
Josef Bacik | 3893e33 | 2011-01-31 16:03:11 -0500 | [diff] [blame] | 7692 | err = ret; |
Yan, Zheng | 8082510 | 2009-11-12 09:35:36 +0000 | [diff] [blame] | 7693 | break; |
Josef Bacik | 3893e33 | 2011-01-31 16:03:11 -0500 | [diff] [blame] | 7694 | } |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 7695 | |
Josef Bacik | fcb80c2 | 2011-05-03 10:40:22 -0400 | [diff] [blame] | 7696 | trans->block_rsv = &root->fs_info->trans_block_rsv; |
Yan, Zheng | 8082510 | 2009-11-12 09:35:36 +0000 | [diff] [blame] | 7697 | ret = btrfs_update_inode(trans, root, inode); |
Josef Bacik | 3893e33 | 2011-01-31 16:03:11 -0500 | [diff] [blame] | 7698 | if (ret) { |
| 7699 | err = ret; |
| 7700 | break; |
| 7701 | } |
Josef Bacik | ca7e70f | 2012-08-27 17:48:15 -0400 | [diff] [blame] | 7702 | |
Yan, Zheng | 8082510 | 2009-11-12 09:35:36 +0000 | [diff] [blame] | 7703 | btrfs_end_transaction(trans, root); |
Liu Bo | b53d3f5 | 2012-11-14 14:34:34 +0000 | [diff] [blame] | 7704 | btrfs_btree_balance_dirty(root); |
Josef Bacik | ca7e70f | 2012-08-27 17:48:15 -0400 | [diff] [blame] | 7705 | |
| 7706 | trans = btrfs_start_transaction(root, 2); |
| 7707 | if (IS_ERR(trans)) { |
| 7708 | ret = err = PTR_ERR(trans); |
| 7709 | trans = NULL; |
| 7710 | break; |
| 7711 | } |
| 7712 | |
| 7713 | ret = btrfs_block_rsv_migrate(&root->fs_info->trans_block_rsv, |
| 7714 | rsv, min_size); |
| 7715 | BUG_ON(ret); /* shouldn't happen */ |
| 7716 | trans->block_rsv = rsv; |
Yan, Zheng | 8082510 | 2009-11-12 09:35:36 +0000 | [diff] [blame] | 7717 | } |
| 7718 | |
| 7719 | if (ret == 0 && inode->i_nlink > 0) { |
Josef Bacik | fcb80c2 | 2011-05-03 10:40:22 -0400 | [diff] [blame] | 7720 | trans->block_rsv = root->orphan_block_rsv; |
Yan, Zheng | 8082510 | 2009-11-12 09:35:36 +0000 | [diff] [blame] | 7721 | ret = btrfs_orphan_del(trans, inode); |
Josef Bacik | 3893e33 | 2011-01-31 16:03:11 -0500 | [diff] [blame] | 7722 | if (ret) |
| 7723 | err = ret; |
Yan, Zheng | 8082510 | 2009-11-12 09:35:36 +0000 | [diff] [blame] | 7724 | } |
| 7725 | |
Chris Mason | 917c16b | 2011-11-08 14:49:59 -0500 | [diff] [blame] | 7726 | if (trans) { |
| 7727 | trans->block_rsv = &root->fs_info->trans_block_rsv; |
| 7728 | ret = btrfs_update_inode(trans, root, inode); |
| 7729 | if (ret && !err) |
| 7730 | err = ret; |
Josef Bacik | 7b12876 | 2008-07-24 12:17:14 -0400 | [diff] [blame] | 7731 | |
Josef Bacik | 7ad85bb | 2012-01-12 19:10:12 -0500 | [diff] [blame] | 7732 | ret = btrfs_end_transaction(trans, root); |
Liu Bo | b53d3f5 | 2012-11-14 14:34:34 +0000 | [diff] [blame] | 7733 | btrfs_btree_balance_dirty(root); |
Chris Mason | 917c16b | 2011-11-08 14:49:59 -0500 | [diff] [blame] | 7734 | } |
Josef Bacik | fcb80c2 | 2011-05-03 10:40:22 -0400 | [diff] [blame] | 7735 | |
| 7736 | out: |
| 7737 | btrfs_free_block_rsv(root, rsv); |
| 7738 | |
Josef Bacik | 3893e33 | 2011-01-31 16:03:11 -0500 | [diff] [blame] | 7739 | if (ret && !err) |
| 7740 | err = ret; |
Josef Bacik | a41ad39 | 2011-01-31 15:30:16 -0500 | [diff] [blame] | 7741 | |
Josef Bacik | 3893e33 | 2011-01-31 16:03:11 -0500 | [diff] [blame] | 7742 | return err; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 7743 | } |
| 7744 | |
Sven Wegener | 3b96362 | 2008-06-09 21:57:42 -0400 | [diff] [blame] | 7745 | /* |
Chris Mason | d352ac6 | 2008-09-29 15:18:18 -0400 | [diff] [blame] | 7746 | * create a new subvolume directory/inode (helper for the ioctl). |
| 7747 | */ |
Yan Zheng | d2fb343 | 2008-12-11 16:30:39 -0500 | [diff] [blame] | 7748 | int btrfs_create_subvol_root(struct btrfs_trans_handle *trans, |
Josef Bacik | d82a6f1d | 2011-05-11 15:26:06 -0400 | [diff] [blame] | 7749 | struct btrfs_root *new_root, u64 new_dirid) |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 7750 | { |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 7751 | struct inode *inode; |
Yan, Zheng | 76dda93 | 2009-09-21 16:00:26 -0400 | [diff] [blame] | 7752 | int err; |
Chris Mason | 00e4e6b | 2008-08-05 11:18:09 -0400 | [diff] [blame] | 7753 | u64 index = 0; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 7754 | |
Florian Albrechtskirchinger | 12fc9d0 | 2012-02-10 22:15:54 +0100 | [diff] [blame] | 7755 | inode = btrfs_new_inode(trans, new_root, NULL, "..", 2, |
| 7756 | new_dirid, new_dirid, |
| 7757 | S_IFDIR | (~current_umask() & S_IRWXUGO), |
| 7758 | &index); |
Chris Mason | 54aa1f4 | 2007-06-22 14:16:25 -0400 | [diff] [blame] | 7759 | if (IS_ERR(inode)) |
Christoph Hellwig | f46b5a6 | 2008-06-11 21:53:53 -0400 | [diff] [blame] | 7760 | return PTR_ERR(inode); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 7761 | inode->i_op = &btrfs_dir_inode_operations; |
| 7762 | inode->i_fop = &btrfs_dir_file_operations; |
| 7763 | |
Miklos Szeredi | bfe8684 | 2011-10-28 14:13:29 +0200 | [diff] [blame] | 7764 | set_nlink(inode, 1); |
Chris Mason | dbe674a | 2008-07-17 12:54:05 -0400 | [diff] [blame] | 7765 | btrfs_i_size_write(inode, 0); |
Sven Wegener | 3b96362 | 2008-06-09 21:57:42 -0400 | [diff] [blame] | 7766 | |
Yan, Zheng | 76dda93 | 2009-09-21 16:00:26 -0400 | [diff] [blame] | 7767 | err = btrfs_update_inode(trans, new_root, inode); |
Christoph Hellwig | cb8e709 | 2008-10-09 13:39:39 -0400 | [diff] [blame] | 7768 | |
Yan, Zheng | 76dda93 | 2009-09-21 16:00:26 -0400 | [diff] [blame] | 7769 | iput(inode); |
Mark Fasheh | ce59897 | 2011-07-26 11:32:23 -0700 | [diff] [blame] | 7770 | return err; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 7771 | } |
| 7772 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 7773 | struct inode *btrfs_alloc_inode(struct super_block *sb) |
| 7774 | { |
| 7775 | struct btrfs_inode *ei; |
Yan, Zheng | 2ead6ae | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 7776 | struct inode *inode; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 7777 | |
| 7778 | ei = kmem_cache_alloc(btrfs_inode_cachep, GFP_NOFS); |
| 7779 | if (!ei) |
| 7780 | return NULL; |
Yan, Zheng | 2ead6ae | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 7781 | |
| 7782 | ei->root = NULL; |
Yan, Zheng | 2ead6ae | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 7783 | ei->generation = 0; |
Josef Bacik | 15ee9bc | 2007-08-10 16:22:09 -0400 | [diff] [blame] | 7784 | ei->last_trans = 0; |
Chris Mason | 257c62e | 2009-10-13 13:21:08 -0400 | [diff] [blame] | 7785 | ei->last_sub_trans = 0; |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 7786 | ei->logged_trans = 0; |
Yan, Zheng | 2ead6ae | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 7787 | ei->delalloc_bytes = 0; |
Yan, Zheng | 2ead6ae | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 7788 | ei->disk_i_size = 0; |
| 7789 | ei->flags = 0; |
Josef Bacik | 7709cde | 2011-08-04 10:25:02 -0400 | [diff] [blame] | 7790 | ei->csum_bytes = 0; |
Yan, Zheng | 2ead6ae | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 7791 | ei->index_cnt = (u64)-1; |
| 7792 | ei->last_unlink_trans = 0; |
Liu Bo | 46d8bc3 | 2012-08-29 01:07:55 -0600 | [diff] [blame] | 7793 | ei->last_log_commit = 0; |
Yan, Zheng | 2ead6ae | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 7794 | |
Josef Bacik | 9e0baf6 | 2011-07-15 15:16:44 +0000 | [diff] [blame] | 7795 | spin_lock_init(&ei->lock); |
| 7796 | ei->outstanding_extents = 0; |
| 7797 | ei->reserved_extents = 0; |
Yan, Zheng | 2ead6ae | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 7798 | |
Josef Bacik | 72ac3c0 | 2012-05-23 14:13:11 -0400 | [diff] [blame] | 7799 | ei->runtime_flags = 0; |
Li Zefan | 261507a0 | 2010-12-17 14:21:50 +0800 | [diff] [blame] | 7800 | ei->force_compress = BTRFS_COMPRESS_NONE; |
Yan, Zheng | 2ead6ae | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 7801 | |
Miao Xie | 16cdcec | 2011-04-22 18:12:22 +0800 | [diff] [blame] | 7802 | ei->delayed_node = NULL; |
| 7803 | |
Yan, Zheng | 2ead6ae | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 7804 | inode = &ei->vfs_inode; |
David Sterba | a8067e0 | 2011-04-21 00:34:43 +0200 | [diff] [blame] | 7805 | extent_map_tree_init(&ei->extent_tree); |
David Sterba | f993c88 | 2011-04-20 23:35:57 +0200 | [diff] [blame] | 7806 | extent_io_tree_init(&ei->io_tree, &inode->i_data); |
| 7807 | extent_io_tree_init(&ei->io_failure_tree, &inode->i_data); |
Josef Bacik | 0b32f4b | 2012-03-13 09:38:00 -0400 | [diff] [blame] | 7808 | ei->io_tree.track_uptodate = 1; |
| 7809 | ei->io_failure_tree.track_uptodate = 1; |
Josef Bacik | b812ce2 | 2012-11-16 13:56:32 -0500 | [diff] [blame] | 7810 | atomic_set(&ei->sync_writers, 0); |
Yan, Zheng | 2ead6ae | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 7811 | mutex_init(&ei->log_mutex); |
Josef Bacik | f248679 | 2012-01-13 12:09:22 -0500 | [diff] [blame] | 7812 | mutex_init(&ei->delalloc_mutex); |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 7813 | btrfs_ordered_inode_tree_init(&ei->ordered_tree); |
Yan, Zheng | 2ead6ae | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 7814 | INIT_LIST_HEAD(&ei->delalloc_inodes); |
Chris Mason | 5a3f23d | 2009-03-31 13:27:11 -0400 | [diff] [blame] | 7815 | INIT_LIST_HEAD(&ei->ordered_operations); |
Yan, Zheng | 2ead6ae | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 7816 | RB_CLEAR_NODE(&ei->rb_node); |
| 7817 | |
| 7818 | return inode; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 7819 | } |
| 7820 | |
Nick Piggin | fa0d7e3d | 2011-01-07 17:49:49 +1100 | [diff] [blame] | 7821 | static void btrfs_i_callback(struct rcu_head *head) |
| 7822 | { |
| 7823 | struct inode *inode = container_of(head, struct inode, i_rcu); |
Nick Piggin | fa0d7e3d | 2011-01-07 17:49:49 +1100 | [diff] [blame] | 7824 | kmem_cache_free(btrfs_inode_cachep, BTRFS_I(inode)); |
| 7825 | } |
| 7826 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 7827 | void btrfs_destroy_inode(struct inode *inode) |
| 7828 | { |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 7829 | struct btrfs_ordered_extent *ordered; |
Chris Mason | 5a3f23d | 2009-03-31 13:27:11 -0400 | [diff] [blame] | 7830 | struct btrfs_root *root = BTRFS_I(inode)->root; |
| 7831 | |
Al Viro | b3d9b7a | 2012-06-09 13:51:19 -0400 | [diff] [blame] | 7832 | WARN_ON(!hlist_empty(&inode->i_dentry)); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 7833 | WARN_ON(inode->i_data.nrpages); |
Josef Bacik | 9e0baf6 | 2011-07-15 15:16:44 +0000 | [diff] [blame] | 7834 | WARN_ON(BTRFS_I(inode)->outstanding_extents); |
| 7835 | WARN_ON(BTRFS_I(inode)->reserved_extents); |
Josef Bacik | 7709cde | 2011-08-04 10:25:02 -0400 | [diff] [blame] | 7836 | WARN_ON(BTRFS_I(inode)->delalloc_bytes); |
| 7837 | WARN_ON(BTRFS_I(inode)->csum_bytes); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 7838 | |
Chris Mason | 5a3f23d | 2009-03-31 13:27:11 -0400 | [diff] [blame] | 7839 | /* |
Josef Bacik | a6dbd42 | 2009-11-11 15:53:34 -0500 | [diff] [blame] | 7840 | * This can happen where we create an inode, but somebody else also |
| 7841 | * created the same inode and we need to destroy the one we already |
| 7842 | * created. |
| 7843 | */ |
| 7844 | if (!root) |
| 7845 | goto free; |
| 7846 | |
| 7847 | /* |
Chris Mason | 5a3f23d | 2009-03-31 13:27:11 -0400 | [diff] [blame] | 7848 | * Make sure we're properly removed from the ordered operation |
| 7849 | * lists. |
| 7850 | */ |
| 7851 | smp_mb(); |
| 7852 | if (!list_empty(&BTRFS_I(inode)->ordered_operations)) { |
Miao Xie | 199c2a9 | 2013-05-15 07:48:23 +0000 | [diff] [blame] | 7853 | spin_lock(&root->fs_info->ordered_root_lock); |
Chris Mason | 5a3f23d | 2009-03-31 13:27:11 -0400 | [diff] [blame] | 7854 | list_del_init(&BTRFS_I(inode)->ordered_operations); |
Miao Xie | 199c2a9 | 2013-05-15 07:48:23 +0000 | [diff] [blame] | 7855 | spin_unlock(&root->fs_info->ordered_root_lock); |
Chris Mason | 5a3f23d | 2009-03-31 13:27:11 -0400 | [diff] [blame] | 7856 | } |
| 7857 | |
Josef Bacik | 8a35d95 | 2012-05-23 14:26:42 -0400 | [diff] [blame] | 7858 | if (test_bit(BTRFS_INODE_HAS_ORPHAN_ITEM, |
| 7859 | &BTRFS_I(inode)->runtime_flags)) { |
Simon Kirby | c2cf52e | 2013-03-19 22:41:23 +0000 | [diff] [blame] | 7860 | btrfs_info(root->fs_info, "inode %llu still on the orphan list", |
| 7861 | (unsigned long long)btrfs_ino(inode)); |
Josef Bacik | 8a35d95 | 2012-05-23 14:26:42 -0400 | [diff] [blame] | 7862 | atomic_dec(&root->orphan_inodes); |
Josef Bacik | 7b12876 | 2008-07-24 12:17:14 -0400 | [diff] [blame] | 7863 | } |
Josef Bacik | 7b12876 | 2008-07-24 12:17:14 -0400 | [diff] [blame] | 7864 | |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 7865 | while (1) { |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 7866 | ordered = btrfs_lookup_first_ordered_extent(inode, (u64)-1); |
| 7867 | if (!ordered) |
| 7868 | break; |
| 7869 | else { |
Simon Kirby | c2cf52e | 2013-03-19 22:41:23 +0000 | [diff] [blame] | 7870 | btrfs_err(root->fs_info, "found ordered extent %llu %llu on inode cleanup", |
| 7871 | (unsigned long long)ordered->file_offset, |
| 7872 | (unsigned long long)ordered->len); |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 7873 | btrfs_remove_ordered_extent(inode, ordered); |
| 7874 | btrfs_put_ordered_extent(ordered); |
| 7875 | btrfs_put_ordered_extent(ordered); |
| 7876 | } |
| 7877 | } |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 7878 | inode_tree_del(inode); |
Zheng Yan | 5b21f2e | 2008-09-26 10:05:38 -0400 | [diff] [blame] | 7879 | btrfs_drop_extent_cache(inode, 0, (u64)-1, 0); |
Josef Bacik | a6dbd42 | 2009-11-11 15:53:34 -0500 | [diff] [blame] | 7880 | free: |
Nick Piggin | fa0d7e3d | 2011-01-07 17:49:49 +1100 | [diff] [blame] | 7881 | call_rcu(&inode->i_rcu, btrfs_i_callback); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 7882 | } |
| 7883 | |
Al Viro | 45321ac | 2010-06-07 13:43:19 -0400 | [diff] [blame] | 7884 | int btrfs_drop_inode(struct inode *inode) |
Yan, Zheng | 76dda93 | 2009-09-21 16:00:26 -0400 | [diff] [blame] | 7885 | { |
| 7886 | struct btrfs_root *root = BTRFS_I(inode)->root; |
Al Viro | 45321ac | 2010-06-07 13:43:19 -0400 | [diff] [blame] | 7887 | |
Naohiro Aota | 6379ef9 | 2013-06-06 09:56:34 +0000 | [diff] [blame] | 7888 | if (root == NULL) |
| 7889 | return 1; |
| 7890 | |
Liu Bo | fa6ac87 | 2013-02-20 14:10:23 +0000 | [diff] [blame] | 7891 | /* the snap/subvol tree is on deleting */ |
Josef Bacik | 0af3d00 | 2010-06-21 14:48:16 -0400 | [diff] [blame] | 7892 | if (btrfs_root_refs(&root->root_item) == 0 && |
Liu Bo | fa6ac87 | 2013-02-20 14:10:23 +0000 | [diff] [blame] | 7893 | root != root->fs_info->tree_root) |
Al Viro | 45321ac | 2010-06-07 13:43:19 -0400 | [diff] [blame] | 7894 | return 1; |
Yan, Zheng | 76dda93 | 2009-09-21 16:00:26 -0400 | [diff] [blame] | 7895 | else |
Al Viro | 45321ac | 2010-06-07 13:43:19 -0400 | [diff] [blame] | 7896 | return generic_drop_inode(inode); |
Yan, Zheng | 76dda93 | 2009-09-21 16:00:26 -0400 | [diff] [blame] | 7897 | } |
| 7898 | |
Sven Wegener | 0ee0fda | 2008-07-30 16:54:26 -0400 | [diff] [blame] | 7899 | static void init_once(void *foo) |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 7900 | { |
| 7901 | struct btrfs_inode *ei = (struct btrfs_inode *) foo; |
| 7902 | |
| 7903 | inode_init_once(&ei->vfs_inode); |
| 7904 | } |
| 7905 | |
| 7906 | void btrfs_destroy_cachep(void) |
| 7907 | { |
Kirill A. Shutemov | 8c0a853 | 2012-09-26 11:33:07 +1000 | [diff] [blame] | 7908 | /* |
| 7909 | * Make sure all delayed rcu free inodes are flushed before we |
| 7910 | * destroy cache. |
| 7911 | */ |
| 7912 | rcu_barrier(); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 7913 | if (btrfs_inode_cachep) |
| 7914 | kmem_cache_destroy(btrfs_inode_cachep); |
| 7915 | if (btrfs_trans_handle_cachep) |
| 7916 | kmem_cache_destroy(btrfs_trans_handle_cachep); |
| 7917 | if (btrfs_transaction_cachep) |
| 7918 | kmem_cache_destroy(btrfs_transaction_cachep); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 7919 | if (btrfs_path_cachep) |
| 7920 | kmem_cache_destroy(btrfs_path_cachep); |
Josef Bacik | dc89e98 | 2011-01-28 17:05:48 -0500 | [diff] [blame] | 7921 | if (btrfs_free_space_cachep) |
| 7922 | kmem_cache_destroy(btrfs_free_space_cachep); |
Miao Xie | 8ccf6f19 | 2012-10-25 09:28:04 +0000 | [diff] [blame] | 7923 | if (btrfs_delalloc_work_cachep) |
| 7924 | kmem_cache_destroy(btrfs_delalloc_work_cachep); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 7925 | } |
| 7926 | |
| 7927 | int btrfs_init_cachep(void) |
| 7928 | { |
David Sterba | 837e197 | 2012-09-07 03:00:48 -0600 | [diff] [blame] | 7929 | btrfs_inode_cachep = kmem_cache_create("btrfs_inode", |
Christoph Hellwig | 9601e3f | 2009-04-13 15:33:09 +0200 | [diff] [blame] | 7930 | sizeof(struct btrfs_inode), 0, |
| 7931 | SLAB_RECLAIM_ACCOUNT | SLAB_MEM_SPREAD, init_once); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 7932 | if (!btrfs_inode_cachep) |
| 7933 | goto fail; |
Christoph Hellwig | 9601e3f | 2009-04-13 15:33:09 +0200 | [diff] [blame] | 7934 | |
David Sterba | 837e197 | 2012-09-07 03:00:48 -0600 | [diff] [blame] | 7935 | btrfs_trans_handle_cachep = kmem_cache_create("btrfs_trans_handle", |
Christoph Hellwig | 9601e3f | 2009-04-13 15:33:09 +0200 | [diff] [blame] | 7936 | sizeof(struct btrfs_trans_handle), 0, |
| 7937 | SLAB_RECLAIM_ACCOUNT | SLAB_MEM_SPREAD, NULL); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 7938 | if (!btrfs_trans_handle_cachep) |
| 7939 | goto fail; |
Christoph Hellwig | 9601e3f | 2009-04-13 15:33:09 +0200 | [diff] [blame] | 7940 | |
David Sterba | 837e197 | 2012-09-07 03:00:48 -0600 | [diff] [blame] | 7941 | btrfs_transaction_cachep = kmem_cache_create("btrfs_transaction", |
Christoph Hellwig | 9601e3f | 2009-04-13 15:33:09 +0200 | [diff] [blame] | 7942 | sizeof(struct btrfs_transaction), 0, |
| 7943 | SLAB_RECLAIM_ACCOUNT | SLAB_MEM_SPREAD, NULL); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 7944 | if (!btrfs_transaction_cachep) |
| 7945 | goto fail; |
Christoph Hellwig | 9601e3f | 2009-04-13 15:33:09 +0200 | [diff] [blame] | 7946 | |
David Sterba | 837e197 | 2012-09-07 03:00:48 -0600 | [diff] [blame] | 7947 | btrfs_path_cachep = kmem_cache_create("btrfs_path", |
Christoph Hellwig | 9601e3f | 2009-04-13 15:33:09 +0200 | [diff] [blame] | 7948 | sizeof(struct btrfs_path), 0, |
| 7949 | SLAB_RECLAIM_ACCOUNT | SLAB_MEM_SPREAD, NULL); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 7950 | if (!btrfs_path_cachep) |
| 7951 | goto fail; |
Christoph Hellwig | 9601e3f | 2009-04-13 15:33:09 +0200 | [diff] [blame] | 7952 | |
David Sterba | 837e197 | 2012-09-07 03:00:48 -0600 | [diff] [blame] | 7953 | btrfs_free_space_cachep = kmem_cache_create("btrfs_free_space", |
Josef Bacik | dc89e98 | 2011-01-28 17:05:48 -0500 | [diff] [blame] | 7954 | sizeof(struct btrfs_free_space), 0, |
| 7955 | SLAB_RECLAIM_ACCOUNT | SLAB_MEM_SPREAD, NULL); |
| 7956 | if (!btrfs_free_space_cachep) |
| 7957 | goto fail; |
| 7958 | |
Miao Xie | 8ccf6f19 | 2012-10-25 09:28:04 +0000 | [diff] [blame] | 7959 | btrfs_delalloc_work_cachep = kmem_cache_create("btrfs_delalloc_work", |
| 7960 | sizeof(struct btrfs_delalloc_work), 0, |
| 7961 | SLAB_RECLAIM_ACCOUNT | SLAB_MEM_SPREAD, |
| 7962 | NULL); |
| 7963 | if (!btrfs_delalloc_work_cachep) |
| 7964 | goto fail; |
| 7965 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 7966 | return 0; |
| 7967 | fail: |
| 7968 | btrfs_destroy_cachep(); |
| 7969 | return -ENOMEM; |
| 7970 | } |
| 7971 | |
| 7972 | static int btrfs_getattr(struct vfsmount *mnt, |
| 7973 | struct dentry *dentry, struct kstat *stat) |
| 7974 | { |
Miao Xie | df0af1a | 2013-01-29 10:11:59 +0000 | [diff] [blame] | 7975 | u64 delalloc_bytes; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 7976 | struct inode *inode = dentry->d_inode; |
David Sterba | fadc0d8 | 2011-11-20 07:33:38 -0500 | [diff] [blame] | 7977 | u32 blocksize = inode->i_sb->s_blocksize; |
| 7978 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 7979 | generic_fillattr(inode, stat); |
Al Viro | 0ee5dc6 | 2011-07-07 15:44:25 -0400 | [diff] [blame] | 7980 | stat->dev = BTRFS_I(inode)->root->anon_dev; |
Chris Mason | d666746 | 2008-01-03 14:51:00 -0500 | [diff] [blame] | 7981 | stat->blksize = PAGE_CACHE_SIZE; |
Miao Xie | df0af1a | 2013-01-29 10:11:59 +0000 | [diff] [blame] | 7982 | |
| 7983 | spin_lock(&BTRFS_I(inode)->lock); |
| 7984 | delalloc_bytes = BTRFS_I(inode)->delalloc_bytes; |
| 7985 | spin_unlock(&BTRFS_I(inode)->lock); |
David Sterba | fadc0d8 | 2011-11-20 07:33:38 -0500 | [diff] [blame] | 7986 | stat->blocks = (ALIGN(inode_get_bytes(inode), blocksize) + |
Miao Xie | df0af1a | 2013-01-29 10:11:59 +0000 | [diff] [blame] | 7987 | ALIGN(delalloc_bytes, blocksize)) >> 9; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 7988 | return 0; |
| 7989 | } |
| 7990 | |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 7991 | static int btrfs_rename(struct inode *old_dir, struct dentry *old_dentry, |
| 7992 | struct inode *new_dir, struct dentry *new_dentry) |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 7993 | { |
| 7994 | struct btrfs_trans_handle *trans; |
| 7995 | struct btrfs_root *root = BTRFS_I(old_dir)->root; |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 7996 | struct btrfs_root *dest = BTRFS_I(new_dir)->root; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 7997 | struct inode *new_inode = new_dentry->d_inode; |
| 7998 | struct inode *old_inode = old_dentry->d_inode; |
| 7999 | struct timespec ctime = CURRENT_TIME; |
Chris Mason | 00e4e6b | 2008-08-05 11:18:09 -0400 | [diff] [blame] | 8000 | u64 index = 0; |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 8001 | u64 root_objectid; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 8002 | int ret; |
Li Zefan | 33345d01 | 2011-04-20 10:31:50 +0800 | [diff] [blame] | 8003 | u64 old_ino = btrfs_ino(old_inode); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 8004 | |
Li Zefan | 33345d01 | 2011-04-20 10:31:50 +0800 | [diff] [blame] | 8005 | if (btrfs_ino(new_dir) == BTRFS_EMPTY_SUBVOL_DIR_OBJECTID) |
Yan, Zheng | f679a84 | 2009-09-24 09:17:31 -0400 | [diff] [blame] | 8006 | return -EPERM; |
| 8007 | |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 8008 | /* we only allow rename subvolume link between subvolumes */ |
Li Zefan | 33345d01 | 2011-04-20 10:31:50 +0800 | [diff] [blame] | 8009 | if (old_ino != BTRFS_FIRST_FREE_OBJECTID && root != dest) |
Chris Mason | 3394e16 | 2008-11-17 20:42:26 -0500 | [diff] [blame] | 8010 | return -EXDEV; |
| 8011 | |
Li Zefan | 33345d01 | 2011-04-20 10:31:50 +0800 | [diff] [blame] | 8012 | if (old_ino == BTRFS_EMPTY_SUBVOL_DIR_OBJECTID || |
| 8013 | (new_inode && btrfs_ino(new_inode) == BTRFS_FIRST_FREE_OBJECTID)) |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 8014 | return -ENOTEMPTY; |
| 8015 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 8016 | if (S_ISDIR(old_inode->i_mode) && new_inode && |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 8017 | new_inode->i_size > BTRFS_EMPTY_DIR_SIZE) |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 8018 | return -ENOTEMPTY; |
Chris Mason | 9c52057 | 2012-12-17 14:26:57 -0500 | [diff] [blame] | 8019 | |
| 8020 | |
| 8021 | /* check for collisions, even if the name isn't there */ |
| 8022 | ret = btrfs_check_dir_item_collision(root, new_dir->i_ino, |
| 8023 | new_dentry->d_name.name, |
| 8024 | new_dentry->d_name.len); |
| 8025 | |
| 8026 | if (ret) { |
| 8027 | if (ret == -EEXIST) { |
| 8028 | /* we shouldn't get |
| 8029 | * eexist without a new_inode */ |
| 8030 | if (!new_inode) { |
| 8031 | WARN_ON(1); |
| 8032 | return ret; |
| 8033 | } |
| 8034 | } else { |
| 8035 | /* maybe -EOVERFLOW */ |
| 8036 | return ret; |
| 8037 | } |
| 8038 | } |
| 8039 | ret = 0; |
| 8040 | |
Chris Mason | 5a3f23d | 2009-03-31 13:27:11 -0400 | [diff] [blame] | 8041 | /* |
| 8042 | * we're using rename to replace one file with another. |
| 8043 | * and the replacement file is large. Start IO on it now so |
| 8044 | * we don't add too much work to the end of the transaction |
| 8045 | */ |
Bartlomiej Zolnierkiewicz | 4baf8c9 | 2009-08-07 13:47:08 -0400 | [diff] [blame] | 8046 | 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] | 8047 | old_inode->i_size > BTRFS_ORDERED_OPERATIONS_FLUSH_LIMIT) |
| 8048 | filemap_flush(old_inode->i_mapping); |
| 8049 | |
Yan, Zheng | 76dda93 | 2009-09-21 16:00:26 -0400 | [diff] [blame] | 8050 | /* close the racy window with snapshot create/destroy ioctl */ |
Li Zefan | 33345d01 | 2011-04-20 10:31:50 +0800 | [diff] [blame] | 8051 | if (old_ino == BTRFS_FIRST_FREE_OBJECTID) |
Yan, Zheng | 76dda93 | 2009-09-21 16:00:26 -0400 | [diff] [blame] | 8052 | down_read(&root->fs_info->subvol_sem); |
Yan, Zheng | a22285a | 2010-05-16 10:48:46 -0400 | [diff] [blame] | 8053 | /* |
| 8054 | * We want to reserve the absolute worst case amount of items. So if |
| 8055 | * both inodes are subvols and we need to unlink them then that would |
| 8056 | * require 4 item modifications, but if they are both normal inodes it |
| 8057 | * would require 5 item modifications, so we'll assume their normal |
| 8058 | * inodes. So 5 * 2 is 10, plus 1 for the new link, so 11 total items |
| 8059 | * should cover the worst case number of items we'll modify. |
| 8060 | */ |
Josef Bacik | 6e137ed | 2013-03-26 15:26:55 -0400 | [diff] [blame] | 8061 | trans = btrfs_start_transaction(root, 11); |
Johann Lombardi | b44c59a | 2011-03-31 13:23:47 +0000 | [diff] [blame] | 8062 | if (IS_ERR(trans)) { |
| 8063 | ret = PTR_ERR(trans); |
| 8064 | goto out_notrans; |
| 8065 | } |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 8066 | |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 8067 | if (dest != root) |
| 8068 | btrfs_record_root_in_trans(trans, dest); |
| 8069 | |
Yan, Zheng | a571952 | 2009-09-24 09:17:31 -0400 | [diff] [blame] | 8070 | ret = btrfs_set_inode_index(new_dir, &index); |
| 8071 | if (ret) |
| 8072 | goto out_fail; |
Chris Mason | 5a3f23d | 2009-03-31 13:27:11 -0400 | [diff] [blame] | 8073 | |
Li Zefan | 33345d01 | 2011-04-20 10:31:50 +0800 | [diff] [blame] | 8074 | if (unlikely(old_ino == BTRFS_FIRST_FREE_OBJECTID)) { |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 8075 | /* force full log commit if subvolume involved. */ |
| 8076 | root->fs_info->last_trans_log_full_commit = trans->transid; |
| 8077 | } else { |
Yan, Zheng | a571952 | 2009-09-24 09:17:31 -0400 | [diff] [blame] | 8078 | ret = btrfs_insert_inode_ref(trans, dest, |
| 8079 | new_dentry->d_name.name, |
| 8080 | new_dentry->d_name.len, |
Li Zefan | 33345d01 | 2011-04-20 10:31:50 +0800 | [diff] [blame] | 8081 | old_ino, |
| 8082 | btrfs_ino(new_dir), index); |
Yan, Zheng | a571952 | 2009-09-24 09:17:31 -0400 | [diff] [blame] | 8083 | if (ret) |
| 8084 | goto out_fail; |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 8085 | /* |
| 8086 | * this is an ugly little race, but the rename is required |
| 8087 | * to make sure that if we crash, the inode is either at the |
| 8088 | * old name or the new one. pinning the log transaction lets |
| 8089 | * us make sure we don't allow a log commit to come in after |
| 8090 | * we unlink the name but before we add the new name back in. |
| 8091 | */ |
| 8092 | btrfs_pin_log_trans(root); |
| 8093 | } |
Chris Mason | 12fcfd2 | 2009-03-24 10:24:20 -0400 | [diff] [blame] | 8094 | /* |
Chris Mason | 5a3f23d | 2009-03-31 13:27:11 -0400 | [diff] [blame] | 8095 | * make sure the inode gets flushed if it is replacing |
| 8096 | * something. |
| 8097 | */ |
Li Zefan | 33345d01 | 2011-04-20 10:31:50 +0800 | [diff] [blame] | 8098 | if (new_inode && new_inode->i_size && S_ISREG(old_inode->i_mode)) |
Chris Mason | 5a3f23d | 2009-03-31 13:27:11 -0400 | [diff] [blame] | 8099 | btrfs_add_ordered_operation(trans, root, old_inode); |
Chris Mason | 5a3f23d | 2009-03-31 13:27:11 -0400 | [diff] [blame] | 8100 | |
Josef Bacik | 0c4d2d9 | 2012-04-05 15:03:02 -0400 | [diff] [blame] | 8101 | inode_inc_iversion(old_dir); |
| 8102 | inode_inc_iversion(new_dir); |
| 8103 | inode_inc_iversion(old_inode); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 8104 | old_dir->i_ctime = old_dir->i_mtime = ctime; |
| 8105 | new_dir->i_ctime = new_dir->i_mtime = ctime; |
| 8106 | old_inode->i_ctime = ctime; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 8107 | |
Chris Mason | 12fcfd2 | 2009-03-24 10:24:20 -0400 | [diff] [blame] | 8108 | if (old_dentry->d_parent != new_dentry->d_parent) |
| 8109 | btrfs_record_unlink_dir(trans, old_dir, old_inode, 1); |
| 8110 | |
Li Zefan | 33345d01 | 2011-04-20 10:31:50 +0800 | [diff] [blame] | 8111 | if (unlikely(old_ino == BTRFS_FIRST_FREE_OBJECTID)) { |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 8112 | root_objectid = BTRFS_I(old_inode)->root->root_key.objectid; |
| 8113 | ret = btrfs_unlink_subvol(trans, root, old_dir, root_objectid, |
| 8114 | old_dentry->d_name.name, |
| 8115 | old_dentry->d_name.len); |
| 8116 | } else { |
Al Viro | 9298679 | 2011-03-04 17:14:37 +0000 | [diff] [blame] | 8117 | ret = __btrfs_unlink_inode(trans, root, old_dir, |
| 8118 | old_dentry->d_inode, |
| 8119 | old_dentry->d_name.name, |
| 8120 | old_dentry->d_name.len); |
| 8121 | if (!ret) |
| 8122 | ret = btrfs_update_inode(trans, root, old_inode); |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 8123 | } |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 8124 | if (ret) { |
| 8125 | btrfs_abort_transaction(trans, root, ret); |
| 8126 | goto out_fail; |
| 8127 | } |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 8128 | |
| 8129 | if (new_inode) { |
Josef Bacik | 0c4d2d9 | 2012-04-05 15:03:02 -0400 | [diff] [blame] | 8130 | inode_inc_iversion(new_inode); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 8131 | new_inode->i_ctime = CURRENT_TIME; |
Li Zefan | 33345d01 | 2011-04-20 10:31:50 +0800 | [diff] [blame] | 8132 | if (unlikely(btrfs_ino(new_inode) == |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 8133 | BTRFS_EMPTY_SUBVOL_DIR_OBJECTID)) { |
| 8134 | root_objectid = BTRFS_I(new_inode)->location.objectid; |
| 8135 | ret = btrfs_unlink_subvol(trans, dest, new_dir, |
| 8136 | root_objectid, |
| 8137 | new_dentry->d_name.name, |
| 8138 | new_dentry->d_name.len); |
| 8139 | BUG_ON(new_inode->i_nlink == 0); |
| 8140 | } else { |
| 8141 | ret = btrfs_unlink_inode(trans, dest, new_dir, |
| 8142 | new_dentry->d_inode, |
| 8143 | new_dentry->d_name.name, |
| 8144 | new_dentry->d_name.len); |
| 8145 | } |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 8146 | if (!ret && new_inode->i_nlink == 0) { |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 8147 | ret = btrfs_orphan_add(trans, new_dentry->d_inode); |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 8148 | BUG_ON(ret); |
Josef Bacik | 7b12876 | 2008-07-24 12:17:14 -0400 | [diff] [blame] | 8149 | } |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 8150 | if (ret) { |
| 8151 | btrfs_abort_transaction(trans, root, ret); |
| 8152 | goto out_fail; |
| 8153 | } |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 8154 | } |
Josef Bacik | aec7477 | 2008-07-24 12:12:38 -0400 | [diff] [blame] | 8155 | |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 8156 | ret = btrfs_add_link(trans, new_dir, old_inode, |
| 8157 | new_dentry->d_name.name, |
Yan, Zheng | a571952 | 2009-09-24 09:17:31 -0400 | [diff] [blame] | 8158 | new_dentry->d_name.len, 0, index); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 8159 | if (ret) { |
| 8160 | btrfs_abort_transaction(trans, root, ret); |
| 8161 | goto out_fail; |
| 8162 | } |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 8163 | |
Li Zefan | 33345d01 | 2011-04-20 10:31:50 +0800 | [diff] [blame] | 8164 | if (old_ino != BTRFS_FIRST_FREE_OBJECTID) { |
Al Viro | 10d9f30 | 2011-07-16 23:09:10 -0400 | [diff] [blame] | 8165 | struct dentry *parent = new_dentry->d_parent; |
Josef Bacik | 6a91221 | 2010-11-20 09:48:00 +0000 | [diff] [blame] | 8166 | btrfs_log_new_name(trans, old_inode, old_dir, parent); |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 8167 | btrfs_end_log_trans(root); |
| 8168 | } |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 8169 | out_fail: |
Josef Bacik | 7ad85bb | 2012-01-12 19:10:12 -0500 | [diff] [blame] | 8170 | btrfs_end_transaction(trans, root); |
Johann Lombardi | b44c59a | 2011-03-31 13:23:47 +0000 | [diff] [blame] | 8171 | out_notrans: |
Li Zefan | 33345d01 | 2011-04-20 10:31:50 +0800 | [diff] [blame] | 8172 | if (old_ino == BTRFS_FIRST_FREE_OBJECTID) |
Yan, Zheng | 76dda93 | 2009-09-21 16:00:26 -0400 | [diff] [blame] | 8173 | up_read(&root->fs_info->subvol_sem); |
Josef Bacik | 9ed74f2 | 2009-09-11 16:12:44 -0400 | [diff] [blame] | 8174 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 8175 | return ret; |
| 8176 | } |
| 8177 | |
Miao Xie | 8ccf6f19 | 2012-10-25 09:28:04 +0000 | [diff] [blame] | 8178 | static void btrfs_run_delalloc_work(struct btrfs_work *work) |
| 8179 | { |
| 8180 | struct btrfs_delalloc_work *delalloc_work; |
| 8181 | |
| 8182 | delalloc_work = container_of(work, struct btrfs_delalloc_work, |
| 8183 | work); |
| 8184 | if (delalloc_work->wait) |
| 8185 | btrfs_wait_ordered_range(delalloc_work->inode, 0, (u64)-1); |
| 8186 | else |
| 8187 | filemap_flush(delalloc_work->inode->i_mapping); |
| 8188 | |
| 8189 | if (delalloc_work->delay_iput) |
| 8190 | btrfs_add_delayed_iput(delalloc_work->inode); |
| 8191 | else |
| 8192 | iput(delalloc_work->inode); |
| 8193 | complete(&delalloc_work->completion); |
| 8194 | } |
| 8195 | |
| 8196 | struct btrfs_delalloc_work *btrfs_alloc_delalloc_work(struct inode *inode, |
| 8197 | int wait, int delay_iput) |
| 8198 | { |
| 8199 | struct btrfs_delalloc_work *work; |
| 8200 | |
| 8201 | work = kmem_cache_zalloc(btrfs_delalloc_work_cachep, GFP_NOFS); |
| 8202 | if (!work) |
| 8203 | return NULL; |
| 8204 | |
| 8205 | init_completion(&work->completion); |
| 8206 | INIT_LIST_HEAD(&work->list); |
| 8207 | work->inode = inode; |
| 8208 | work->wait = wait; |
| 8209 | work->delay_iput = delay_iput; |
| 8210 | work->work.func = btrfs_run_delalloc_work; |
| 8211 | |
| 8212 | return work; |
| 8213 | } |
| 8214 | |
| 8215 | void btrfs_wait_and_free_delalloc_work(struct btrfs_delalloc_work *work) |
| 8216 | { |
| 8217 | wait_for_completion(&work->completion); |
| 8218 | kmem_cache_free(btrfs_delalloc_work_cachep, work); |
| 8219 | } |
| 8220 | |
Chris Mason | d352ac6 | 2008-09-29 15:18:18 -0400 | [diff] [blame] | 8221 | /* |
| 8222 | * some fairly slow code that needs optimization. This walks the list |
| 8223 | * of all the inodes with pending delalloc and forces them to disk. |
| 8224 | */ |
Miao Xie | eb73c1b | 2013-05-15 07:48:22 +0000 | [diff] [blame] | 8225 | static int __start_delalloc_inodes(struct btrfs_root *root, int delay_iput) |
Chris Mason | ea8c281 | 2008-08-04 23:17:27 -0400 | [diff] [blame] | 8226 | { |
Chris Mason | ea8c281 | 2008-08-04 23:17:27 -0400 | [diff] [blame] | 8227 | struct btrfs_inode *binode; |
Zheng Yan | 5b21f2e | 2008-09-26 10:05:38 -0400 | [diff] [blame] | 8228 | struct inode *inode; |
Miao Xie | 8ccf6f19 | 2012-10-25 09:28:04 +0000 | [diff] [blame] | 8229 | struct btrfs_delalloc_work *work, *next; |
| 8230 | struct list_head works; |
Miao Xie | 1eafa6c | 2013-01-22 10:49:00 +0000 | [diff] [blame] | 8231 | struct list_head splice; |
Miao Xie | 8ccf6f19 | 2012-10-25 09:28:04 +0000 | [diff] [blame] | 8232 | int ret = 0; |
Chris Mason | ea8c281 | 2008-08-04 23:17:27 -0400 | [diff] [blame] | 8233 | |
Miao Xie | 8ccf6f19 | 2012-10-25 09:28:04 +0000 | [diff] [blame] | 8234 | INIT_LIST_HEAD(&works); |
Miao Xie | 1eafa6c | 2013-01-22 10:49:00 +0000 | [diff] [blame] | 8235 | INIT_LIST_HEAD(&splice); |
Miao Xie | 63607cc | 2013-01-22 10:50:35 +0000 | [diff] [blame] | 8236 | |
Miao Xie | eb73c1b | 2013-05-15 07:48:22 +0000 | [diff] [blame] | 8237 | spin_lock(&root->delalloc_lock); |
| 8238 | list_splice_init(&root->delalloc_inodes, &splice); |
Miao Xie | 1eafa6c | 2013-01-22 10:49:00 +0000 | [diff] [blame] | 8239 | while (!list_empty(&splice)) { |
| 8240 | binode = list_entry(splice.next, struct btrfs_inode, |
Chris Mason | ea8c281 | 2008-08-04 23:17:27 -0400 | [diff] [blame] | 8241 | delalloc_inodes); |
Miao Xie | 1eafa6c | 2013-01-22 10:49:00 +0000 | [diff] [blame] | 8242 | |
Miao Xie | eb73c1b | 2013-05-15 07:48:22 +0000 | [diff] [blame] | 8243 | list_move_tail(&binode->delalloc_inodes, |
| 8244 | &root->delalloc_inodes); |
Zheng Yan | 5b21f2e | 2008-09-26 10:05:38 -0400 | [diff] [blame] | 8245 | inode = igrab(&binode->vfs_inode); |
Miao Xie | df0af1a | 2013-01-29 10:11:59 +0000 | [diff] [blame] | 8246 | if (!inode) { |
Miao Xie | eb73c1b | 2013-05-15 07:48:22 +0000 | [diff] [blame] | 8247 | cond_resched_lock(&root->delalloc_lock); |
Miao Xie | 1eafa6c | 2013-01-22 10:49:00 +0000 | [diff] [blame] | 8248 | continue; |
Miao Xie | df0af1a | 2013-01-29 10:11:59 +0000 | [diff] [blame] | 8249 | } |
Miao Xie | eb73c1b | 2013-05-15 07:48:22 +0000 | [diff] [blame] | 8250 | spin_unlock(&root->delalloc_lock); |
Miao Xie | 1eafa6c | 2013-01-22 10:49:00 +0000 | [diff] [blame] | 8251 | |
| 8252 | work = btrfs_alloc_delalloc_work(inode, 0, delay_iput); |
| 8253 | if (unlikely(!work)) { |
| 8254 | ret = -ENOMEM; |
| 8255 | goto out; |
Zheng Yan | 5b21f2e | 2008-09-26 10:05:38 -0400 | [diff] [blame] | 8256 | } |
Miao Xie | 1eafa6c | 2013-01-22 10:49:00 +0000 | [diff] [blame] | 8257 | list_add_tail(&work->list, &works); |
| 8258 | btrfs_queue_worker(&root->fs_info->flush_workers, |
| 8259 | &work->work); |
| 8260 | |
Zheng Yan | 5b21f2e | 2008-09-26 10:05:38 -0400 | [diff] [blame] | 8261 | cond_resched(); |
Miao Xie | eb73c1b | 2013-05-15 07:48:22 +0000 | [diff] [blame] | 8262 | spin_lock(&root->delalloc_lock); |
Chris Mason | ea8c281 | 2008-08-04 23:17:27 -0400 | [diff] [blame] | 8263 | } |
Miao Xie | eb73c1b | 2013-05-15 07:48:22 +0000 | [diff] [blame] | 8264 | spin_unlock(&root->delalloc_lock); |
Chris Mason | 8c8bee1 | 2008-09-29 11:19:10 -0400 | [diff] [blame] | 8265 | |
Miao Xie | 1eafa6c | 2013-01-22 10:49:00 +0000 | [diff] [blame] | 8266 | list_for_each_entry_safe(work, next, &works, list) { |
| 8267 | list_del_init(&work->list); |
| 8268 | btrfs_wait_and_free_delalloc_work(work); |
| 8269 | } |
Miao Xie | eb73c1b | 2013-05-15 07:48:22 +0000 | [diff] [blame] | 8270 | return 0; |
| 8271 | out: |
| 8272 | list_for_each_entry_safe(work, next, &works, list) { |
| 8273 | list_del_init(&work->list); |
| 8274 | btrfs_wait_and_free_delalloc_work(work); |
| 8275 | } |
Miao Xie | 1eafa6c | 2013-01-22 10:49:00 +0000 | [diff] [blame] | 8276 | |
Miao Xie | eb73c1b | 2013-05-15 07:48:22 +0000 | [diff] [blame] | 8277 | if (!list_empty_careful(&splice)) { |
| 8278 | spin_lock(&root->delalloc_lock); |
| 8279 | list_splice_tail(&splice, &root->delalloc_inodes); |
| 8280 | spin_unlock(&root->delalloc_lock); |
| 8281 | } |
| 8282 | return ret; |
| 8283 | } |
| 8284 | |
| 8285 | int btrfs_start_delalloc_inodes(struct btrfs_root *root, int delay_iput) |
| 8286 | { |
| 8287 | int ret; |
| 8288 | |
| 8289 | if (root->fs_info->sb->s_flags & MS_RDONLY) |
| 8290 | return -EROFS; |
| 8291 | |
| 8292 | ret = __start_delalloc_inodes(root, delay_iput); |
| 8293 | /* |
| 8294 | * the filemap_flush will queue IO into the worker threads, but |
Chris Mason | 8c8bee1 | 2008-09-29 11:19:10 -0400 | [diff] [blame] | 8295 | * we have to make sure the IO is actually started and that |
| 8296 | * ordered extents get created before we return |
| 8297 | */ |
| 8298 | atomic_inc(&root->fs_info->async_submit_draining); |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 8299 | while (atomic_read(&root->fs_info->nr_async_submits) || |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 8300 | atomic_read(&root->fs_info->async_delalloc_pages)) { |
Chris Mason | 8c8bee1 | 2008-09-29 11:19:10 -0400 | [diff] [blame] | 8301 | wait_event(root->fs_info->async_submit_wait, |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 8302 | (atomic_read(&root->fs_info->nr_async_submits) == 0 && |
| 8303 | atomic_read(&root->fs_info->async_delalloc_pages) == 0)); |
Chris Mason | 8c8bee1 | 2008-09-29 11:19:10 -0400 | [diff] [blame] | 8304 | } |
| 8305 | atomic_dec(&root->fs_info->async_submit_draining); |
Miao Xie | eb73c1b | 2013-05-15 07:48:22 +0000 | [diff] [blame] | 8306 | return ret; |
| 8307 | } |
| 8308 | |
| 8309 | int btrfs_start_all_delalloc_inodes(struct btrfs_fs_info *fs_info, |
| 8310 | int delay_iput) |
| 8311 | { |
| 8312 | struct btrfs_root *root; |
| 8313 | struct list_head splice; |
| 8314 | int ret; |
| 8315 | |
| 8316 | if (fs_info->sb->s_flags & MS_RDONLY) |
| 8317 | return -EROFS; |
| 8318 | |
| 8319 | INIT_LIST_HEAD(&splice); |
| 8320 | |
| 8321 | spin_lock(&fs_info->delalloc_root_lock); |
| 8322 | list_splice_init(&fs_info->delalloc_roots, &splice); |
| 8323 | while (!list_empty(&splice)) { |
| 8324 | root = list_first_entry(&splice, struct btrfs_root, |
| 8325 | delalloc_root); |
| 8326 | root = btrfs_grab_fs_root(root); |
| 8327 | BUG_ON(!root); |
| 8328 | list_move_tail(&root->delalloc_root, |
| 8329 | &fs_info->delalloc_roots); |
| 8330 | spin_unlock(&fs_info->delalloc_root_lock); |
| 8331 | |
| 8332 | ret = __start_delalloc_inodes(root, delay_iput); |
| 8333 | btrfs_put_fs_root(root); |
| 8334 | if (ret) |
| 8335 | goto out; |
| 8336 | |
| 8337 | spin_lock(&fs_info->delalloc_root_lock); |
| 8338 | } |
| 8339 | spin_unlock(&fs_info->delalloc_root_lock); |
| 8340 | |
| 8341 | atomic_inc(&fs_info->async_submit_draining); |
| 8342 | while (atomic_read(&fs_info->nr_async_submits) || |
| 8343 | atomic_read(&fs_info->async_delalloc_pages)) { |
| 8344 | wait_event(fs_info->async_submit_wait, |
| 8345 | (atomic_read(&fs_info->nr_async_submits) == 0 && |
| 8346 | atomic_read(&fs_info->async_delalloc_pages) == 0)); |
| 8347 | } |
| 8348 | atomic_dec(&fs_info->async_submit_draining); |
Miao Xie | 1eafa6c | 2013-01-22 10:49:00 +0000 | [diff] [blame] | 8349 | return 0; |
Miao Xie | 8ccf6f19 | 2012-10-25 09:28:04 +0000 | [diff] [blame] | 8350 | out: |
Miao Xie | 1eafa6c | 2013-01-22 10:49:00 +0000 | [diff] [blame] | 8351 | if (!list_empty_careful(&splice)) { |
Miao Xie | eb73c1b | 2013-05-15 07:48:22 +0000 | [diff] [blame] | 8352 | spin_lock(&fs_info->delalloc_root_lock); |
| 8353 | list_splice_tail(&splice, &fs_info->delalloc_roots); |
| 8354 | spin_unlock(&fs_info->delalloc_root_lock); |
Miao Xie | 1eafa6c | 2013-01-22 10:49:00 +0000 | [diff] [blame] | 8355 | } |
Miao Xie | 8ccf6f19 | 2012-10-25 09:28:04 +0000 | [diff] [blame] | 8356 | return ret; |
Chris Mason | ea8c281 | 2008-08-04 23:17:27 -0400 | [diff] [blame] | 8357 | } |
| 8358 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 8359 | static int btrfs_symlink(struct inode *dir, struct dentry *dentry, |
| 8360 | const char *symname) |
| 8361 | { |
| 8362 | struct btrfs_trans_handle *trans; |
| 8363 | struct btrfs_root *root = BTRFS_I(dir)->root; |
| 8364 | struct btrfs_path *path; |
| 8365 | struct btrfs_key key; |
Chris Mason | 1832a6d | 2007-12-21 16:27:21 -0500 | [diff] [blame] | 8366 | struct inode *inode = NULL; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 8367 | int err; |
| 8368 | int drop_inode = 0; |
| 8369 | u64 objectid; |
Chris Mason | 00e4e6b | 2008-08-05 11:18:09 -0400 | [diff] [blame] | 8370 | u64 index = 0 ; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 8371 | int name_len; |
| 8372 | int datasize; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 8373 | unsigned long ptr; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 8374 | struct btrfs_file_extent_item *ei; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 8375 | struct extent_buffer *leaf; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 8376 | |
| 8377 | name_len = strlen(symname) + 1; |
| 8378 | if (name_len > BTRFS_MAX_INLINE_DATA_SIZE(root)) |
| 8379 | return -ENAMETOOLONG; |
Chris Mason | 1832a6d | 2007-12-21 16:27:21 -0500 | [diff] [blame] | 8380 | |
Josef Bacik | 9ed74f2 | 2009-09-11 16:12:44 -0400 | [diff] [blame] | 8381 | /* |
| 8382 | * 2 items for inode item and ref |
| 8383 | * 2 items for dir items |
| 8384 | * 1 item for xattr if selinux is on |
| 8385 | */ |
Yan, Zheng | a22285a | 2010-05-16 10:48:46 -0400 | [diff] [blame] | 8386 | trans = btrfs_start_transaction(root, 5); |
| 8387 | if (IS_ERR(trans)) |
| 8388 | return PTR_ERR(trans); |
Chris Mason | 1832a6d | 2007-12-21 16:27:21 -0500 | [diff] [blame] | 8389 | |
Li Zefan | 581bb05 | 2011-04-20 10:06:11 +0800 | [diff] [blame] | 8390 | err = btrfs_find_free_ino(root, &objectid); |
| 8391 | if (err) |
| 8392 | goto out_unlock; |
| 8393 | |
Josef Bacik | aec7477 | 2008-07-24 12:12:38 -0400 | [diff] [blame] | 8394 | inode = btrfs_new_inode(trans, root, dir, dentry->d_name.name, |
Li Zefan | 33345d01 | 2011-04-20 10:31:50 +0800 | [diff] [blame] | 8395 | dentry->d_name.len, btrfs_ino(dir), objectid, |
Josef Bacik | d82a6f1d | 2011-05-11 15:26:06 -0400 | [diff] [blame] | 8396 | S_IFLNK|S_IRWXUGO, &index); |
Tsutomu Itoh | 7cf96da | 2011-04-25 19:43:53 -0400 | [diff] [blame] | 8397 | if (IS_ERR(inode)) { |
| 8398 | err = PTR_ERR(inode); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 8399 | goto out_unlock; |
Tsutomu Itoh | 7cf96da | 2011-04-25 19:43:53 -0400 | [diff] [blame] | 8400 | } |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 8401 | |
Eric Paris | 2a7dba3 | 2011-02-01 11:05:39 -0500 | [diff] [blame] | 8402 | err = btrfs_init_inode_security(trans, inode, dir, &dentry->d_name); |
Josef Bacik | 33268ea | 2008-07-24 12:16:36 -0400 | [diff] [blame] | 8403 | if (err) { |
| 8404 | drop_inode = 1; |
| 8405 | goto out_unlock; |
| 8406 | } |
| 8407 | |
Casey Schaufler | ad19db7 | 2011-12-15 10:09:07 -0500 | [diff] [blame] | 8408 | /* |
| 8409 | * If the active LSM wants to access the inode during |
| 8410 | * d_instantiate it needs these. Smack checks to see |
| 8411 | * if the filesystem supports xattrs by looking at the |
| 8412 | * ops vector. |
| 8413 | */ |
| 8414 | inode->i_fop = &btrfs_file_operations; |
| 8415 | inode->i_op = &btrfs_file_inode_operations; |
| 8416 | |
Josef Bacik | a1b075d | 2010-11-19 20:36:11 +0000 | [diff] [blame] | 8417 | err = btrfs_add_nondir(trans, dir, dentry, inode, 0, index); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 8418 | if (err) |
| 8419 | drop_inode = 1; |
| 8420 | else { |
| 8421 | inode->i_mapping->a_ops = &btrfs_aops; |
Chris Mason | 0416008 | 2008-03-26 10:28:07 -0400 | [diff] [blame] | 8422 | inode->i_mapping->backing_dev_info = &root->fs_info->bdi; |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 8423 | BTRFS_I(inode)->io_tree.ops = &btrfs_extent_io_ops; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 8424 | } |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 8425 | if (drop_inode) |
| 8426 | goto out_unlock; |
| 8427 | |
| 8428 | path = btrfs_alloc_path(); |
Mark Fasheh | d8926bb | 2011-07-13 10:38:47 -0700 | [diff] [blame] | 8429 | if (!path) { |
| 8430 | err = -ENOMEM; |
| 8431 | drop_inode = 1; |
| 8432 | goto out_unlock; |
| 8433 | } |
Li Zefan | 33345d01 | 2011-04-20 10:31:50 +0800 | [diff] [blame] | 8434 | key.objectid = btrfs_ino(inode); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 8435 | key.offset = 0; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 8436 | btrfs_set_key_type(&key, BTRFS_EXTENT_DATA_KEY); |
| 8437 | datasize = btrfs_file_extent_calc_inline_size(name_len); |
| 8438 | err = btrfs_insert_empty_item(trans, root, path, &key, |
| 8439 | datasize); |
Chris Mason | 54aa1f4 | 2007-06-22 14:16:25 -0400 | [diff] [blame] | 8440 | if (err) { |
| 8441 | drop_inode = 1; |
Julia Lawall | b083916 | 2011-05-14 07:10:51 +0000 | [diff] [blame] | 8442 | btrfs_free_path(path); |
Chris Mason | 54aa1f4 | 2007-06-22 14:16:25 -0400 | [diff] [blame] | 8443 | goto out_unlock; |
| 8444 | } |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 8445 | leaf = path->nodes[0]; |
| 8446 | ei = btrfs_item_ptr(leaf, path->slots[0], |
| 8447 | struct btrfs_file_extent_item); |
| 8448 | btrfs_set_file_extent_generation(leaf, ei, trans->transid); |
| 8449 | btrfs_set_file_extent_type(leaf, ei, |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 8450 | BTRFS_FILE_EXTENT_INLINE); |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 8451 | btrfs_set_file_extent_encryption(leaf, ei, 0); |
| 8452 | btrfs_set_file_extent_compression(leaf, ei, 0); |
| 8453 | btrfs_set_file_extent_other_encoding(leaf, ei, 0); |
| 8454 | btrfs_set_file_extent_ram_bytes(leaf, ei, name_len); |
| 8455 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 8456 | ptr = btrfs_file_extent_inline_start(ei); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 8457 | write_extent_buffer(leaf, symname, ptr, name_len); |
| 8458 | btrfs_mark_buffer_dirty(leaf); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 8459 | btrfs_free_path(path); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 8460 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 8461 | inode->i_op = &btrfs_symlink_inode_operations; |
| 8462 | inode->i_mapping->a_ops = &btrfs_symlink_aops; |
Chris Mason | 0416008 | 2008-03-26 10:28:07 -0400 | [diff] [blame] | 8463 | inode->i_mapping->backing_dev_info = &root->fs_info->bdi; |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 8464 | inode_set_bytes(inode, name_len); |
Chris Mason | dbe674a | 2008-07-17 12:54:05 -0400 | [diff] [blame] | 8465 | btrfs_i_size_write(inode, name_len - 1); |
Chris Mason | 54aa1f4 | 2007-06-22 14:16:25 -0400 | [diff] [blame] | 8466 | err = btrfs_update_inode(trans, root, inode); |
| 8467 | if (err) |
| 8468 | drop_inode = 1; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 8469 | |
| 8470 | out_unlock: |
Al Viro | 08c422c | 2011-12-23 07:58:13 -0500 | [diff] [blame] | 8471 | if (!err) |
| 8472 | d_instantiate(dentry, inode); |
Josef Bacik | 7ad85bb | 2012-01-12 19:10:12 -0500 | [diff] [blame] | 8473 | btrfs_end_transaction(trans, root); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 8474 | if (drop_inode) { |
| 8475 | inode_dec_link_count(inode); |
| 8476 | iput(inode); |
| 8477 | } |
Liu Bo | b53d3f5 | 2012-11-14 14:34:34 +0000 | [diff] [blame] | 8478 | btrfs_btree_balance_dirty(root); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 8479 | return err; |
| 8480 | } |
Chris Mason | 1643298 | 2008-04-10 10:23:21 -0400 | [diff] [blame] | 8481 | |
Josef Bacik | 0af3d00 | 2010-06-21 14:48:16 -0400 | [diff] [blame] | 8482 | static int __btrfs_prealloc_file_range(struct inode *inode, int mode, |
| 8483 | u64 start, u64 num_bytes, u64 min_size, |
| 8484 | loff_t actual_len, u64 *alloc_hint, |
| 8485 | struct btrfs_trans_handle *trans) |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 8486 | { |
Josef Bacik | 5dc562c | 2012-08-17 13:14:17 -0400 | [diff] [blame] | 8487 | struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree; |
| 8488 | struct extent_map *em; |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 8489 | struct btrfs_root *root = BTRFS_I(inode)->root; |
| 8490 | struct btrfs_key ins; |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 8491 | u64 cur_offset = start; |
Josef Bacik | 55a61d1 | 2010-11-22 18:50:32 +0000 | [diff] [blame] | 8492 | u64 i_size; |
Chris Mason | 154ea28 | 2013-03-05 11:11:26 -0500 | [diff] [blame] | 8493 | u64 cur_bytes; |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 8494 | int ret = 0; |
Josef Bacik | 0af3d00 | 2010-06-21 14:48:16 -0400 | [diff] [blame] | 8495 | bool own_trans = true; |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 8496 | |
Josef Bacik | 0af3d00 | 2010-06-21 14:48:16 -0400 | [diff] [blame] | 8497 | if (trans) |
| 8498 | own_trans = false; |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 8499 | while (num_bytes > 0) { |
Josef Bacik | 0af3d00 | 2010-06-21 14:48:16 -0400 | [diff] [blame] | 8500 | if (own_trans) { |
| 8501 | trans = btrfs_start_transaction(root, 3); |
| 8502 | if (IS_ERR(trans)) { |
| 8503 | ret = PTR_ERR(trans); |
| 8504 | break; |
| 8505 | } |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 8506 | } |
Yan, Zheng | 5a303d5 | 2009-11-12 09:34:52 +0000 | [diff] [blame] | 8507 | |
Chris Mason | 154ea28 | 2013-03-05 11:11:26 -0500 | [diff] [blame] | 8508 | cur_bytes = min(num_bytes, 256ULL * 1024 * 1024); |
| 8509 | cur_bytes = max(cur_bytes, min_size); |
| 8510 | ret = btrfs_reserve_extent(trans, root, cur_bytes, |
Zach Brown | 24542bf | 2012-11-16 00:04:43 +0000 | [diff] [blame] | 8511 | min_size, 0, *alloc_hint, &ins, 1); |
Yan, Zheng | 5a303d5 | 2009-11-12 09:34:52 +0000 | [diff] [blame] | 8512 | if (ret) { |
Josef Bacik | 0af3d00 | 2010-06-21 14:48:16 -0400 | [diff] [blame] | 8513 | if (own_trans) |
| 8514 | btrfs_end_transaction(trans, root); |
Yan, Zheng | a22285a | 2010-05-16 10:48:46 -0400 | [diff] [blame] | 8515 | break; |
Yan, Zheng | 5a303d5 | 2009-11-12 09:34:52 +0000 | [diff] [blame] | 8516 | } |
| 8517 | |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 8518 | ret = insert_reserved_file_extent(trans, inode, |
| 8519 | cur_offset, ins.objectid, |
| 8520 | ins.offset, ins.offset, |
Yan, Zheng | 920bbbf | 2009-11-12 09:34:08 +0000 | [diff] [blame] | 8521 | ins.offset, 0, 0, 0, |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 8522 | BTRFS_FILE_EXTENT_PREALLOC); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 8523 | if (ret) { |
| 8524 | btrfs_abort_transaction(trans, root, ret); |
| 8525 | if (own_trans) |
| 8526 | btrfs_end_transaction(trans, root); |
| 8527 | break; |
| 8528 | } |
Chris Mason | a1ed835 | 2009-09-11 12:27:37 -0400 | [diff] [blame] | 8529 | btrfs_drop_extent_cache(inode, cur_offset, |
| 8530 | cur_offset + ins.offset -1, 0); |
Yan, Zheng | 5a303d5 | 2009-11-12 09:34:52 +0000 | [diff] [blame] | 8531 | |
Josef Bacik | 5dc562c | 2012-08-17 13:14:17 -0400 | [diff] [blame] | 8532 | em = alloc_extent_map(); |
| 8533 | if (!em) { |
| 8534 | set_bit(BTRFS_INODE_NEEDS_FULL_SYNC, |
| 8535 | &BTRFS_I(inode)->runtime_flags); |
| 8536 | goto next; |
| 8537 | } |
| 8538 | |
| 8539 | em->start = cur_offset; |
| 8540 | em->orig_start = cur_offset; |
| 8541 | em->len = ins.offset; |
| 8542 | em->block_start = ins.objectid; |
| 8543 | em->block_len = ins.offset; |
Josef Bacik | b493968 | 2012-12-03 10:31:19 -0500 | [diff] [blame] | 8544 | em->orig_block_len = ins.offset; |
Josef Bacik | cc95bef | 2013-04-04 14:31:27 -0400 | [diff] [blame] | 8545 | em->ram_bytes = ins.offset; |
Josef Bacik | 5dc562c | 2012-08-17 13:14:17 -0400 | [diff] [blame] | 8546 | em->bdev = root->fs_info->fs_devices->latest_bdev; |
| 8547 | set_bit(EXTENT_FLAG_PREALLOC, &em->flags); |
| 8548 | em->generation = trans->transid; |
| 8549 | |
| 8550 | while (1) { |
| 8551 | write_lock(&em_tree->lock); |
Josef Bacik | 09a2a8f9 | 2013-04-05 16:51:15 -0400 | [diff] [blame] | 8552 | ret = add_extent_mapping(em_tree, em, 1); |
Josef Bacik | 5dc562c | 2012-08-17 13:14:17 -0400 | [diff] [blame] | 8553 | write_unlock(&em_tree->lock); |
| 8554 | if (ret != -EEXIST) |
| 8555 | break; |
| 8556 | btrfs_drop_extent_cache(inode, cur_offset, |
| 8557 | cur_offset + ins.offset - 1, |
| 8558 | 0); |
| 8559 | } |
| 8560 | free_extent_map(em); |
| 8561 | next: |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 8562 | num_bytes -= ins.offset; |
| 8563 | cur_offset += ins.offset; |
Yan, Zheng | efa5646 | 2010-05-16 10:49:59 -0400 | [diff] [blame] | 8564 | *alloc_hint = ins.objectid + ins.offset; |
Yan, Zheng | 5a303d5 | 2009-11-12 09:34:52 +0000 | [diff] [blame] | 8565 | |
Josef Bacik | 0c4d2d9 | 2012-04-05 15:03:02 -0400 | [diff] [blame] | 8566 | inode_inc_iversion(inode); |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 8567 | inode->i_ctime = CURRENT_TIME; |
Christoph Hellwig | 6cbff00 | 2009-04-17 10:37:41 +0200 | [diff] [blame] | 8568 | BTRFS_I(inode)->flags |= BTRFS_INODE_PREALLOC; |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 8569 | if (!(mode & FALLOC_FL_KEEP_SIZE) && |
Yan, Zheng | efa5646 | 2010-05-16 10:49:59 -0400 | [diff] [blame] | 8570 | (actual_len > inode->i_size) && |
| 8571 | (cur_offset > inode->i_size)) { |
Aneesh Kumar K.V | d1ea6a6 | 2010-01-20 07:28:54 +0000 | [diff] [blame] | 8572 | if (cur_offset > actual_len) |
Josef Bacik | 55a61d1 | 2010-11-22 18:50:32 +0000 | [diff] [blame] | 8573 | i_size = actual_len; |
Aneesh Kumar K.V | d1ea6a6 | 2010-01-20 07:28:54 +0000 | [diff] [blame] | 8574 | else |
Josef Bacik | 55a61d1 | 2010-11-22 18:50:32 +0000 | [diff] [blame] | 8575 | i_size = cur_offset; |
| 8576 | i_size_write(inode, i_size); |
| 8577 | btrfs_ordered_update_i_size(inode, i_size, NULL); |
Yan, Zheng | 5a303d5 | 2009-11-12 09:34:52 +0000 | [diff] [blame] | 8578 | } |
| 8579 | |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 8580 | ret = btrfs_update_inode(trans, root, inode); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 8581 | |
| 8582 | if (ret) { |
| 8583 | btrfs_abort_transaction(trans, root, ret); |
| 8584 | if (own_trans) |
| 8585 | btrfs_end_transaction(trans, root); |
| 8586 | break; |
| 8587 | } |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 8588 | |
Josef Bacik | 0af3d00 | 2010-06-21 14:48:16 -0400 | [diff] [blame] | 8589 | if (own_trans) |
| 8590 | btrfs_end_transaction(trans, root); |
Yan, Zheng | 5a303d5 | 2009-11-12 09:34:52 +0000 | [diff] [blame] | 8591 | } |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 8592 | return ret; |
| 8593 | } |
| 8594 | |
Josef Bacik | 0af3d00 | 2010-06-21 14:48:16 -0400 | [diff] [blame] | 8595 | int btrfs_prealloc_file_range(struct inode *inode, int mode, |
| 8596 | u64 start, u64 num_bytes, u64 min_size, |
| 8597 | loff_t actual_len, u64 *alloc_hint) |
| 8598 | { |
| 8599 | return __btrfs_prealloc_file_range(inode, mode, start, num_bytes, |
| 8600 | min_size, actual_len, alloc_hint, |
| 8601 | NULL); |
| 8602 | } |
| 8603 | |
| 8604 | int btrfs_prealloc_file_range_trans(struct inode *inode, |
| 8605 | struct btrfs_trans_handle *trans, int mode, |
| 8606 | u64 start, u64 num_bytes, u64 min_size, |
| 8607 | loff_t actual_len, u64 *alloc_hint) |
| 8608 | { |
| 8609 | return __btrfs_prealloc_file_range(inode, mode, start, num_bytes, |
| 8610 | min_size, actual_len, alloc_hint, trans); |
| 8611 | } |
| 8612 | |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 8613 | static int btrfs_set_page_dirty(struct page *page) |
| 8614 | { |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 8615 | return __set_page_dirty_nobuffers(page); |
| 8616 | } |
| 8617 | |
Al Viro | 10556cb2 | 2011-06-20 19:28:19 -0400 | [diff] [blame] | 8618 | static int btrfs_permission(struct inode *inode, int mask) |
Yan | fdebe2b | 2008-01-14 13:26:08 -0500 | [diff] [blame] | 8619 | { |
Li Zefan | b83cc96 | 2010-12-20 16:04:08 +0800 | [diff] [blame] | 8620 | struct btrfs_root *root = BTRFS_I(inode)->root; |
Jeff Mahoney | cb6db4e | 2011-08-15 17:27:21 +0000 | [diff] [blame] | 8621 | umode_t mode = inode->i_mode; |
Li Zefan | b83cc96 | 2010-12-20 16:04:08 +0800 | [diff] [blame] | 8622 | |
Jeff Mahoney | cb6db4e | 2011-08-15 17:27:21 +0000 | [diff] [blame] | 8623 | if (mask & MAY_WRITE && |
| 8624 | (S_ISREG(mode) || S_ISDIR(mode) || S_ISLNK(mode))) { |
| 8625 | if (btrfs_root_readonly(root)) |
| 8626 | return -EROFS; |
| 8627 | if (BTRFS_I(inode)->flags & BTRFS_INODE_READONLY) |
| 8628 | return -EACCES; |
| 8629 | } |
Al Viro | 2830ba7 | 2011-06-20 19:16:29 -0400 | [diff] [blame] | 8630 | return generic_permission(inode, mask); |
Yan | fdebe2b | 2008-01-14 13:26:08 -0500 | [diff] [blame] | 8631 | } |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 8632 | |
Alexey Dobriyan | 6e1d5dc | 2009-09-21 17:01:11 -0700 | [diff] [blame] | 8633 | static const struct inode_operations btrfs_dir_inode_operations = { |
Chris Mason | 3394e16 | 2008-11-17 20:42:26 -0500 | [diff] [blame] | 8634 | .getattr = btrfs_getattr, |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 8635 | .lookup = btrfs_lookup, |
| 8636 | .create = btrfs_create, |
| 8637 | .unlink = btrfs_unlink, |
| 8638 | .link = btrfs_link, |
| 8639 | .mkdir = btrfs_mkdir, |
| 8640 | .rmdir = btrfs_rmdir, |
| 8641 | .rename = btrfs_rename, |
| 8642 | .symlink = btrfs_symlink, |
| 8643 | .setattr = btrfs_setattr, |
Josef Bacik | 618e21d | 2007-07-11 10:18:17 -0400 | [diff] [blame] | 8644 | .mknod = btrfs_mknod, |
Christoph Hellwig | 95819c0 | 2008-08-28 06:21:17 -0400 | [diff] [blame] | 8645 | .setxattr = btrfs_setxattr, |
| 8646 | .getxattr = btrfs_getxattr, |
Josef Bacik | 5103e94 | 2007-11-16 11:45:54 -0500 | [diff] [blame] | 8647 | .listxattr = btrfs_listxattr, |
Christoph Hellwig | 95819c0 | 2008-08-28 06:21:17 -0400 | [diff] [blame] | 8648 | .removexattr = btrfs_removexattr, |
Yan | fdebe2b | 2008-01-14 13:26:08 -0500 | [diff] [blame] | 8649 | .permission = btrfs_permission, |
Christoph Hellwig | 4e34e71 | 2011-07-23 17:37:31 +0200 | [diff] [blame] | 8650 | .get_acl = btrfs_get_acl, |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 8651 | }; |
Alexey Dobriyan | 6e1d5dc | 2009-09-21 17:01:11 -0700 | [diff] [blame] | 8652 | static const struct inode_operations btrfs_dir_ro_inode_operations = { |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 8653 | .lookup = btrfs_lookup, |
Yan | fdebe2b | 2008-01-14 13:26:08 -0500 | [diff] [blame] | 8654 | .permission = btrfs_permission, |
Christoph Hellwig | 4e34e71 | 2011-07-23 17:37:31 +0200 | [diff] [blame] | 8655 | .get_acl = btrfs_get_acl, |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 8656 | }; |
Yan, Zheng | 76dda93 | 2009-09-21 16:00:26 -0400 | [diff] [blame] | 8657 | |
Alexey Dobriyan | 828c095 | 2009-10-01 15:43:56 -0700 | [diff] [blame] | 8658 | static const struct file_operations btrfs_dir_file_operations = { |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 8659 | .llseek = generic_file_llseek, |
| 8660 | .read = generic_read_dir, |
Al Viro | 9cdda8d | 2013-05-22 16:48:09 -0400 | [diff] [blame] | 8661 | .iterate = btrfs_real_readdir, |
Christoph Hellwig | 34287aa | 2007-09-14 10:22:47 -0400 | [diff] [blame] | 8662 | .unlocked_ioctl = btrfs_ioctl, |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 8663 | #ifdef CONFIG_COMPAT |
Christoph Hellwig | 34287aa | 2007-09-14 10:22:47 -0400 | [diff] [blame] | 8664 | .compat_ioctl = btrfs_ioctl, |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 8665 | #endif |
Sage Weil | 6bf13c0 | 2008-06-10 10:07:39 -0400 | [diff] [blame] | 8666 | .release = btrfs_release_file, |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 8667 | .fsync = btrfs_sync_file, |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 8668 | }; |
| 8669 | |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 8670 | static struct extent_io_ops btrfs_extent_io_ops = { |
Chris Mason | 07157aa | 2007-08-30 08:50:51 -0400 | [diff] [blame] | 8671 | .fill_delalloc = run_delalloc_range, |
Chris Mason | 065631f | 2008-02-20 12:07:25 -0500 | [diff] [blame] | 8672 | .submit_bio_hook = btrfs_submit_bio_hook, |
Chris Mason | 239b14b | 2008-03-24 15:02:07 -0400 | [diff] [blame] | 8673 | .merge_bio_hook = btrfs_merge_bio_hook, |
Chris Mason | 07157aa | 2007-08-30 08:50:51 -0400 | [diff] [blame] | 8674 | .readpage_end_io_hook = btrfs_readpage_end_io_hook, |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 8675 | .writepage_end_io_hook = btrfs_writepage_end_io_hook, |
Chris Mason | 247e743 | 2008-07-17 12:53:51 -0400 | [diff] [blame] | 8676 | .writepage_start_hook = btrfs_writepage_start_hook, |
Chris Mason | b0c68f8 | 2008-01-31 11:05:37 -0500 | [diff] [blame] | 8677 | .set_bit_hook = btrfs_set_bit_hook, |
| 8678 | .clear_bit_hook = btrfs_clear_bit_hook, |
Josef Bacik | 9ed74f2 | 2009-09-11 16:12:44 -0400 | [diff] [blame] | 8679 | .merge_extent_hook = btrfs_merge_extent_hook, |
| 8680 | .split_extent_hook = btrfs_split_extent_hook, |
Chris Mason | 07157aa | 2007-08-30 08:50:51 -0400 | [diff] [blame] | 8681 | }; |
| 8682 | |
Chris Mason | 3505439 | 2009-01-21 13:11:13 -0500 | [diff] [blame] | 8683 | /* |
| 8684 | * btrfs doesn't support the bmap operation because swapfiles |
| 8685 | * use bmap to make a mapping of extents in the file. They assume |
| 8686 | * these extents won't change over the life of the file and they |
| 8687 | * use the bmap result to do IO directly to the drive. |
| 8688 | * |
| 8689 | * the btrfs bmap call would return logical addresses that aren't |
| 8690 | * suitable for IO and they also will change frequently as COW |
| 8691 | * operations happen. So, swapfile + btrfs == corruption. |
| 8692 | * |
| 8693 | * For now we're avoiding this by dropping bmap. |
| 8694 | */ |
Alexey Dobriyan | 7f09410 | 2009-09-21 17:01:10 -0700 | [diff] [blame] | 8695 | static const struct address_space_operations btrfs_aops = { |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 8696 | .readpage = btrfs_readpage, |
| 8697 | .writepage = btrfs_writepage, |
Chris Mason | b293f02e | 2007-11-01 19:45:34 -0400 | [diff] [blame] | 8698 | .writepages = btrfs_writepages, |
Chris Mason | 3ab2fb5 | 2007-11-08 10:59:22 -0500 | [diff] [blame] | 8699 | .readpages = btrfs_readpages, |
Chris Mason | 1643298 | 2008-04-10 10:23:21 -0400 | [diff] [blame] | 8700 | .direct_IO = btrfs_direct_IO, |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 8701 | .invalidatepage = btrfs_invalidatepage, |
| 8702 | .releasepage = btrfs_releasepage, |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 8703 | .set_page_dirty = btrfs_set_page_dirty, |
Andi Kleen | 465fdd9 | 2009-09-16 11:50:18 +0200 | [diff] [blame] | 8704 | .error_remove_page = generic_error_remove_page, |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 8705 | }; |
| 8706 | |
Alexey Dobriyan | 7f09410 | 2009-09-21 17:01:10 -0700 | [diff] [blame] | 8707 | static const struct address_space_operations btrfs_symlink_aops = { |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 8708 | .readpage = btrfs_readpage, |
| 8709 | .writepage = btrfs_writepage, |
Chris Mason | 2bf5a72 | 2007-08-30 11:54:02 -0400 | [diff] [blame] | 8710 | .invalidatepage = btrfs_invalidatepage, |
| 8711 | .releasepage = btrfs_releasepage, |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 8712 | }; |
| 8713 | |
Alexey Dobriyan | 6e1d5dc | 2009-09-21 17:01:11 -0700 | [diff] [blame] | 8714 | static const struct inode_operations btrfs_file_inode_operations = { |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 8715 | .getattr = btrfs_getattr, |
| 8716 | .setattr = btrfs_setattr, |
Christoph Hellwig | 95819c0 | 2008-08-28 06:21:17 -0400 | [diff] [blame] | 8717 | .setxattr = btrfs_setxattr, |
| 8718 | .getxattr = btrfs_getxattr, |
Josef Bacik | 5103e94 | 2007-11-16 11:45:54 -0500 | [diff] [blame] | 8719 | .listxattr = btrfs_listxattr, |
Christoph Hellwig | 95819c0 | 2008-08-28 06:21:17 -0400 | [diff] [blame] | 8720 | .removexattr = btrfs_removexattr, |
Yan | fdebe2b | 2008-01-14 13:26:08 -0500 | [diff] [blame] | 8721 | .permission = btrfs_permission, |
Yehuda Sadeh | 1506fcc | 2009-01-21 14:39:14 -0500 | [diff] [blame] | 8722 | .fiemap = btrfs_fiemap, |
Christoph Hellwig | 4e34e71 | 2011-07-23 17:37:31 +0200 | [diff] [blame] | 8723 | .get_acl = btrfs_get_acl, |
Josef Bacik | e41f941 | 2012-03-26 09:46:47 -0400 | [diff] [blame] | 8724 | .update_time = btrfs_update_time, |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 8725 | }; |
Alexey Dobriyan | 6e1d5dc | 2009-09-21 17:01:11 -0700 | [diff] [blame] | 8726 | static const struct inode_operations btrfs_special_inode_operations = { |
Josef Bacik | 618e21d | 2007-07-11 10:18:17 -0400 | [diff] [blame] | 8727 | .getattr = btrfs_getattr, |
| 8728 | .setattr = btrfs_setattr, |
Yan | fdebe2b | 2008-01-14 13:26:08 -0500 | [diff] [blame] | 8729 | .permission = btrfs_permission, |
Christoph Hellwig | 95819c0 | 2008-08-28 06:21:17 -0400 | [diff] [blame] | 8730 | .setxattr = btrfs_setxattr, |
| 8731 | .getxattr = btrfs_getxattr, |
Josef Bacik | 33268ea | 2008-07-24 12:16:36 -0400 | [diff] [blame] | 8732 | .listxattr = btrfs_listxattr, |
Christoph Hellwig | 95819c0 | 2008-08-28 06:21:17 -0400 | [diff] [blame] | 8733 | .removexattr = btrfs_removexattr, |
Christoph Hellwig | 4e34e71 | 2011-07-23 17:37:31 +0200 | [diff] [blame] | 8734 | .get_acl = btrfs_get_acl, |
Josef Bacik | e41f941 | 2012-03-26 09:46:47 -0400 | [diff] [blame] | 8735 | .update_time = btrfs_update_time, |
Josef Bacik | 618e21d | 2007-07-11 10:18:17 -0400 | [diff] [blame] | 8736 | }; |
Alexey Dobriyan | 6e1d5dc | 2009-09-21 17:01:11 -0700 | [diff] [blame] | 8737 | static const struct inode_operations btrfs_symlink_inode_operations = { |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 8738 | .readlink = generic_readlink, |
| 8739 | .follow_link = page_follow_link_light, |
| 8740 | .put_link = page_put_link, |
Li Zefan | f209561 | 2010-11-19 02:05:24 +0000 | [diff] [blame] | 8741 | .getattr = btrfs_getattr, |
Josef Bacik | 22c44fe | 2011-11-30 10:45:38 -0500 | [diff] [blame] | 8742 | .setattr = btrfs_setattr, |
Yan | fdebe2b | 2008-01-14 13:26:08 -0500 | [diff] [blame] | 8743 | .permission = btrfs_permission, |
Jim Owens | 0279b4c | 2009-02-04 09:29:13 -0500 | [diff] [blame] | 8744 | .setxattr = btrfs_setxattr, |
| 8745 | .getxattr = btrfs_getxattr, |
| 8746 | .listxattr = btrfs_listxattr, |
| 8747 | .removexattr = btrfs_removexattr, |
Christoph Hellwig | 4e34e71 | 2011-07-23 17:37:31 +0200 | [diff] [blame] | 8748 | .get_acl = btrfs_get_acl, |
Josef Bacik | e41f941 | 2012-03-26 09:46:47 -0400 | [diff] [blame] | 8749 | .update_time = btrfs_update_time, |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 8750 | }; |
Yan, Zheng | 76dda93 | 2009-09-21 16:00:26 -0400 | [diff] [blame] | 8751 | |
Alexey Dobriyan | 82d339d | 2009-10-09 09:54:36 -0400 | [diff] [blame] | 8752 | const struct dentry_operations btrfs_dentry_operations = { |
Yan, Zheng | 76dda93 | 2009-09-21 16:00:26 -0400 | [diff] [blame] | 8753 | .d_delete = btrfs_dentry_delete, |
Josef Bacik | b4aff1f | 2011-06-28 16:18:59 -0400 | [diff] [blame] | 8754 | .d_release = btrfs_dentry_release, |
Yan, Zheng | 76dda93 | 2009-09-21 16:00:26 -0400 | [diff] [blame] | 8755 | }; |