Chris Mason | 6cbd557 | 2007-06-12 09:07:21 -0400 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2007 Oracle. All rights reserved. |
| 3 | * |
| 4 | * This program is free software; you can redistribute it and/or |
| 5 | * modify it under the terms of the GNU General Public |
| 6 | * License v2 as published by the Free Software Foundation. |
| 7 | * |
| 8 | * This program is distributed in the hope that it will be useful, |
| 9 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 10 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 11 | * General Public License for more details. |
| 12 | * |
| 13 | * You should have received a copy of the GNU General Public |
| 14 | * License along with this program; if not, write to the |
| 15 | * Free Software Foundation, Inc., 59 Temple Place - Suite 330, |
| 16 | * Boston, MA 021110-1307, USA. |
| 17 | */ |
| 18 | |
Chris Mason | 8f18cf1 | 2008-04-25 16:53:30 -0400 | [diff] [blame] | 19 | #include <linux/kernel.h> |
Chris Mason | 065631f | 2008-02-20 12:07:25 -0500 | [diff] [blame] | 20 | #include <linux/bio.h> |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 21 | #include <linux/buffer_head.h> |
Sage Weil | f2eb0a2 | 2008-05-02 14:43:14 -0400 | [diff] [blame] | 22 | #include <linux/file.h> |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 23 | #include <linux/fs.h> |
| 24 | #include <linux/pagemap.h> |
| 25 | #include <linux/highmem.h> |
| 26 | #include <linux/time.h> |
| 27 | #include <linux/init.h> |
| 28 | #include <linux/string.h> |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 29 | #include <linux/backing-dev.h> |
| 30 | #include <linux/mpage.h> |
| 31 | #include <linux/swap.h> |
| 32 | #include <linux/writeback.h> |
| 33 | #include <linux/statfs.h> |
| 34 | #include <linux/compat.h> |
Kent Overstreet | a27bb33 | 2013-05-07 16:19:08 -0700 | [diff] [blame] | 35 | #include <linux/aio.h> |
Chris Mason | 9ebefb18 | 2007-06-15 13:50:00 -0400 | [diff] [blame] | 36 | #include <linux/bit_spinlock.h> |
Josef Bacik | 5103e94 | 2007-11-16 11:45:54 -0500 | [diff] [blame] | 37 | #include <linux/xattr.h> |
Josef Bacik | 33268ea | 2008-07-24 12:16:36 -0400 | [diff] [blame] | 38 | #include <linux/posix_acl.h> |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 39 | #include <linux/falloc.h> |
Tejun Heo | 5a0e3ad | 2010-03-24 17:04:11 +0900 | [diff] [blame] | 40 | #include <linux/slab.h> |
David Sterba | 7a36dde | 2011-05-06 15:33:15 +0200 | [diff] [blame] | 41 | #include <linux/ratelimit.h> |
Josef Bacik | 22c44fe | 2011-11-30 10:45:38 -0500 | [diff] [blame] | 42 | #include <linux/mount.h> |
Filipe Brandenburger | 55e301f | 2013-01-29 06:04:50 +0000 | [diff] [blame] | 43 | #include <linux/btrfs.h> |
David Woodhouse | 53b381b | 2013-01-29 18:40:14 -0500 | [diff] [blame] | 44 | #include <linux/blkdev.h> |
Josef Bacik | f23b5a5 | 2013-06-19 10:16:26 -0400 | [diff] [blame] | 45 | #include <linux/posix_acl_xattr.h> |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 46 | #include "ctree.h" |
| 47 | #include "disk-io.h" |
| 48 | #include "transaction.h" |
| 49 | #include "btrfs_inode.h" |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 50 | #include "print-tree.h" |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 51 | #include "ordered-data.h" |
Christoph Hellwig | 95819c0 | 2008-08-28 06:21:17 -0400 | [diff] [blame] | 52 | #include "xattr.h" |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 53 | #include "tree-log.h" |
Jan Schmidt | 4a54c8c | 2011-07-22 15:41:52 +0200 | [diff] [blame] | 54 | #include "volumes.h" |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 55 | #include "compression.h" |
Chris Mason | b4ce94d | 2009-02-04 09:25:08 -0500 | [diff] [blame] | 56 | #include "locking.h" |
Josef Bacik | dc89e98 | 2011-01-28 17:05:48 -0500 | [diff] [blame] | 57 | #include "free-space-cache.h" |
Li Zefan | 581bb05 | 2011-04-20 10:06:11 +0800 | [diff] [blame] | 58 | #include "inode-map.h" |
Liu Bo | 38c227d | 2013-01-29 03:18:40 +0000 | [diff] [blame] | 59 | #include "backref.h" |
Josef Bacik | f23b5a5 | 2013-06-19 10:16:26 -0400 | [diff] [blame] | 60 | #include "hash.h" |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 61 | |
| 62 | struct btrfs_iget_args { |
| 63 | u64 ino; |
| 64 | struct btrfs_root *root; |
| 65 | }; |
| 66 | |
Alexey Dobriyan | 6e1d5dc | 2009-09-21 17:01:11 -0700 | [diff] [blame] | 67 | static const struct inode_operations btrfs_dir_inode_operations; |
| 68 | static const struct inode_operations btrfs_symlink_inode_operations; |
| 69 | static const struct inode_operations btrfs_dir_ro_inode_operations; |
| 70 | static const struct inode_operations btrfs_special_inode_operations; |
| 71 | static const struct inode_operations btrfs_file_inode_operations; |
Alexey Dobriyan | 7f09410 | 2009-09-21 17:01:10 -0700 | [diff] [blame] | 72 | static const struct address_space_operations btrfs_aops; |
| 73 | static const struct address_space_operations btrfs_symlink_aops; |
Alexey Dobriyan | 828c095 | 2009-10-01 15:43:56 -0700 | [diff] [blame] | 74 | static const struct file_operations btrfs_dir_file_operations; |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 75 | static struct extent_io_ops btrfs_extent_io_ops; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 76 | |
| 77 | static struct kmem_cache *btrfs_inode_cachep; |
Miao Xie | 8ccf6f19 | 2012-10-25 09:28:04 +0000 | [diff] [blame] | 78 | static struct kmem_cache *btrfs_delalloc_work_cachep; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 79 | struct kmem_cache *btrfs_trans_handle_cachep; |
| 80 | struct kmem_cache *btrfs_transaction_cachep; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 81 | struct kmem_cache *btrfs_path_cachep; |
Josef Bacik | dc89e98 | 2011-01-28 17:05:48 -0500 | [diff] [blame] | 82 | struct kmem_cache *btrfs_free_space_cachep; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 83 | |
| 84 | #define S_SHIFT 12 |
| 85 | static unsigned char btrfs_type_by_mode[S_IFMT >> S_SHIFT] = { |
| 86 | [S_IFREG >> S_SHIFT] = BTRFS_FT_REG_FILE, |
| 87 | [S_IFDIR >> S_SHIFT] = BTRFS_FT_DIR, |
| 88 | [S_IFCHR >> S_SHIFT] = BTRFS_FT_CHRDEV, |
| 89 | [S_IFBLK >> S_SHIFT] = BTRFS_FT_BLKDEV, |
| 90 | [S_IFIFO >> S_SHIFT] = BTRFS_FT_FIFO, |
| 91 | [S_IFSOCK >> S_SHIFT] = BTRFS_FT_SOCK, |
| 92 | [S_IFLNK >> S_SHIFT] = BTRFS_FT_SYMLINK, |
| 93 | }; |
| 94 | |
Eric Sandeen | 3972f26 | 2013-01-12 02:57:22 +0000 | [diff] [blame] | 95 | static int btrfs_setsize(struct inode *inode, struct iattr *attr); |
Josef Bacik | a41ad39 | 2011-01-31 15:30:16 -0500 | [diff] [blame] | 96 | static int btrfs_truncate(struct inode *inode); |
Josef Bacik | 5fd0204 | 2012-05-02 14:00:54 -0400 | [diff] [blame] | 97 | static int btrfs_finish_ordered_io(struct btrfs_ordered_extent *ordered_extent); |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 98 | static noinline int cow_file_range(struct inode *inode, |
| 99 | struct page *locked_page, |
| 100 | u64 start, u64 end, int *page_started, |
| 101 | unsigned long *nr_written, int unlock); |
Josef Bacik | 70c8a91 | 2012-10-11 16:54:30 -0400 | [diff] [blame] | 102 | static struct extent_map *create_pinned_em(struct inode *inode, u64 start, |
| 103 | u64 len, u64 orig_start, |
| 104 | u64 block_start, u64 block_len, |
Josef Bacik | cc95bef | 2013-04-04 14:31:27 -0400 | [diff] [blame] | 105 | u64 orig_block_len, u64 ram_bytes, |
| 106 | int type); |
Josef Bacik | 7b12876 | 2008-07-24 12:17:14 -0400 | [diff] [blame] | 107 | |
Eric Sandeen | 48a3b63 | 2013-04-25 20:41:01 +0000 | [diff] [blame] | 108 | static int btrfs_dirty_inode(struct inode *inode); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 109 | |
Yan, Zheng | f34f57a | 2009-11-12 09:35:27 +0000 | [diff] [blame] | 110 | static int btrfs_init_inode_security(struct btrfs_trans_handle *trans, |
Eric Paris | 2a7dba3 | 2011-02-01 11:05:39 -0500 | [diff] [blame] | 111 | struct inode *inode, struct inode *dir, |
| 112 | const struct qstr *qstr) |
Jim Owens | 0279b4c | 2009-02-04 09:29:13 -0500 | [diff] [blame] | 113 | { |
| 114 | int err; |
| 115 | |
Yan, Zheng | f34f57a | 2009-11-12 09:35:27 +0000 | [diff] [blame] | 116 | err = btrfs_init_acl(trans, inode, dir); |
Jim Owens | 0279b4c | 2009-02-04 09:29:13 -0500 | [diff] [blame] | 117 | if (!err) |
Eric Paris | 2a7dba3 | 2011-02-01 11:05:39 -0500 | [diff] [blame] | 118 | err = btrfs_xattr_security_init(trans, inode, dir, qstr); |
Jim Owens | 0279b4c | 2009-02-04 09:29:13 -0500 | [diff] [blame] | 119 | return err; |
| 120 | } |
| 121 | |
Chris Mason | d352ac6 | 2008-09-29 15:18:18 -0400 | [diff] [blame] | 122 | /* |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 123 | * this does all the hard work for inserting an inline extent into |
| 124 | * the btree. The caller should have done a btrfs_drop_extents so that |
| 125 | * no overlapping inline items exist in the btree |
| 126 | */ |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 127 | static noinline int insert_inline_extent(struct btrfs_trans_handle *trans, |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 128 | struct btrfs_root *root, struct inode *inode, |
| 129 | u64 start, size_t size, size_t compressed_size, |
Li Zefan | fe3f566 | 2011-03-28 08:30:38 +0000 | [diff] [blame] | 130 | int compress_type, |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 131 | struct page **compressed_pages) |
| 132 | { |
| 133 | struct btrfs_key key; |
| 134 | struct btrfs_path *path; |
| 135 | struct extent_buffer *leaf; |
| 136 | struct page *page = NULL; |
| 137 | char *kaddr; |
| 138 | unsigned long ptr; |
| 139 | struct btrfs_file_extent_item *ei; |
| 140 | int err = 0; |
| 141 | int ret; |
| 142 | size_t cur_size = size; |
| 143 | size_t datasize; |
| 144 | unsigned long offset; |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 145 | |
Li Zefan | fe3f566 | 2011-03-28 08:30:38 +0000 | [diff] [blame] | 146 | if (compressed_size && compressed_pages) |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 147 | cur_size = compressed_size; |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 148 | |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 149 | path = btrfs_alloc_path(); |
| 150 | if (!path) |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 151 | return -ENOMEM; |
| 152 | |
Chris Mason | b947343 | 2009-03-13 11:00:37 -0400 | [diff] [blame] | 153 | path->leave_spinning = 1; |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 154 | |
Li Zefan | 33345d01 | 2011-04-20 10:31:50 +0800 | [diff] [blame] | 155 | key.objectid = btrfs_ino(inode); |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 156 | key.offset = start; |
| 157 | btrfs_set_key_type(&key, BTRFS_EXTENT_DATA_KEY); |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 158 | datasize = btrfs_file_extent_calc_inline_size(cur_size); |
| 159 | |
| 160 | inode_add_bytes(inode, size); |
| 161 | ret = btrfs_insert_empty_item(trans, root, path, &key, |
| 162 | datasize); |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 163 | if (ret) { |
| 164 | err = ret; |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 165 | goto fail; |
| 166 | } |
| 167 | leaf = path->nodes[0]; |
| 168 | ei = btrfs_item_ptr(leaf, path->slots[0], |
| 169 | struct btrfs_file_extent_item); |
| 170 | btrfs_set_file_extent_generation(leaf, ei, trans->transid); |
| 171 | btrfs_set_file_extent_type(leaf, ei, BTRFS_FILE_EXTENT_INLINE); |
| 172 | btrfs_set_file_extent_encryption(leaf, ei, 0); |
| 173 | btrfs_set_file_extent_other_encoding(leaf, ei, 0); |
| 174 | btrfs_set_file_extent_ram_bytes(leaf, ei, size); |
| 175 | ptr = btrfs_file_extent_inline_start(ei); |
| 176 | |
Li Zefan | 261507a0 | 2010-12-17 14:21:50 +0800 | [diff] [blame] | 177 | if (compress_type != BTRFS_COMPRESS_NONE) { |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 178 | struct page *cpage; |
| 179 | int i = 0; |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 180 | while (compressed_size > 0) { |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 181 | cpage = compressed_pages[i]; |
Chris Mason | 5b050f0 | 2008-11-11 09:34:41 -0500 | [diff] [blame] | 182 | cur_size = min_t(unsigned long, compressed_size, |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 183 | PAGE_CACHE_SIZE); |
| 184 | |
Cong Wang | 7ac687d | 2011-11-25 23:14:28 +0800 | [diff] [blame] | 185 | kaddr = kmap_atomic(cpage); |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 186 | write_extent_buffer(leaf, kaddr, ptr, cur_size); |
Cong Wang | 7ac687d | 2011-11-25 23:14:28 +0800 | [diff] [blame] | 187 | kunmap_atomic(kaddr); |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 188 | |
| 189 | i++; |
| 190 | ptr += cur_size; |
| 191 | compressed_size -= cur_size; |
| 192 | } |
| 193 | btrfs_set_file_extent_compression(leaf, ei, |
Li Zefan | 261507a0 | 2010-12-17 14:21:50 +0800 | [diff] [blame] | 194 | compress_type); |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 195 | } else { |
| 196 | page = find_get_page(inode->i_mapping, |
| 197 | start >> PAGE_CACHE_SHIFT); |
| 198 | btrfs_set_file_extent_compression(leaf, ei, 0); |
Cong Wang | 7ac687d | 2011-11-25 23:14:28 +0800 | [diff] [blame] | 199 | kaddr = kmap_atomic(page); |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 200 | offset = start & (PAGE_CACHE_SIZE - 1); |
| 201 | write_extent_buffer(leaf, kaddr + offset, ptr, size); |
Cong Wang | 7ac687d | 2011-11-25 23:14:28 +0800 | [diff] [blame] | 202 | kunmap_atomic(kaddr); |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 203 | page_cache_release(page); |
| 204 | } |
| 205 | btrfs_mark_buffer_dirty(leaf); |
| 206 | btrfs_free_path(path); |
| 207 | |
Yan, Zheng | c216775 | 2009-11-12 09:34:21 +0000 | [diff] [blame] | 208 | /* |
| 209 | * we're an inline extent, so nobody can |
| 210 | * extend the file past i_size without locking |
| 211 | * a page we already have locked. |
| 212 | * |
| 213 | * We must do any isize and inode updates |
| 214 | * before we unlock the pages. Otherwise we |
| 215 | * could end up racing with unlink. |
| 216 | */ |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 217 | BTRFS_I(inode)->disk_i_size = inode->i_size; |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 218 | ret = btrfs_update_inode(trans, root, inode); |
Yan, Zheng | c216775 | 2009-11-12 09:34:21 +0000 | [diff] [blame] | 219 | |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 220 | return ret; |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 221 | fail: |
| 222 | btrfs_free_path(path); |
| 223 | return err; |
| 224 | } |
| 225 | |
| 226 | |
| 227 | /* |
| 228 | * conditionally insert an inline extent into the file. This |
| 229 | * does the checks required to make sure the data is small enough |
| 230 | * to fit as an inline extent. |
| 231 | */ |
Josef Bacik | 0036158 | 2013-08-14 14:02:47 -0400 | [diff] [blame] | 232 | static noinline int cow_file_range_inline(struct btrfs_root *root, |
| 233 | struct inode *inode, u64 start, |
| 234 | u64 end, size_t compressed_size, |
| 235 | int compress_type, |
| 236 | struct page **compressed_pages) |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 237 | { |
Josef Bacik | 0036158 | 2013-08-14 14:02:47 -0400 | [diff] [blame] | 238 | struct btrfs_trans_handle *trans; |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 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 | 0036158 | 2013-08-14 14:02:47 -0400 | [diff] [blame] | 259 | trans = btrfs_join_transaction(root); |
| 260 | if (IS_ERR(trans)) |
| 261 | return PTR_ERR(trans); |
| 262 | trans->block_rsv = &root->fs_info->delalloc_block_rsv; |
| 263 | |
Josef Bacik | 2671485 | 2012-08-29 12:24:27 -0400 | [diff] [blame] | 264 | ret = btrfs_drop_extents(trans, root, inode, start, aligned_end, 1); |
Josef Bacik | 0036158 | 2013-08-14 14:02:47 -0400 | [diff] [blame] | 265 | if (ret) { |
| 266 | btrfs_abort_transaction(trans, root, ret); |
| 267 | goto out; |
| 268 | } |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 269 | |
| 270 | if (isize > actual_end) |
| 271 | inline_len = min_t(u64, isize, actual_end); |
| 272 | ret = insert_inline_extent(trans, root, inode, start, |
| 273 | inline_len, compressed_size, |
Li Zefan | fe3f566 | 2011-03-28 08:30:38 +0000 | [diff] [blame] | 274 | compress_type, compressed_pages); |
Josef Bacik | 2adcac1 | 2012-05-23 16:10:14 -0400 | [diff] [blame] | 275 | if (ret && ret != -ENOSPC) { |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 276 | btrfs_abort_transaction(trans, root, ret); |
Josef Bacik | 0036158 | 2013-08-14 14:02:47 -0400 | [diff] [blame] | 277 | goto out; |
Josef Bacik | 2adcac1 | 2012-05-23 16:10:14 -0400 | [diff] [blame] | 278 | } else if (ret == -ENOSPC) { |
Josef Bacik | 0036158 | 2013-08-14 14:02:47 -0400 | [diff] [blame] | 279 | ret = 1; |
| 280 | goto out; |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 281 | } |
Josef Bacik | 2adcac1 | 2012-05-23 16:10:14 -0400 | [diff] [blame] | 282 | |
Josef Bacik | bdc20e6 | 2013-02-28 13:23:38 -0500 | [diff] [blame] | 283 | set_bit(BTRFS_INODE_NEEDS_FULL_SYNC, &BTRFS_I(inode)->runtime_flags); |
Yan, Zheng | 0ca1f7c | 2010-05-16 10:48:47 -0400 | [diff] [blame] | 284 | btrfs_delalloc_release_metadata(inode, end + 1 - start); |
Chris Mason | a1ed835 | 2009-09-11 12:27:37 -0400 | [diff] [blame] | 285 | btrfs_drop_extent_cache(inode, start, aligned_end - 1, 0); |
Josef Bacik | 0036158 | 2013-08-14 14:02:47 -0400 | [diff] [blame] | 286 | out: |
| 287 | btrfs_end_transaction(trans, root); |
| 288 | return ret; |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 289 | } |
| 290 | |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 291 | struct async_extent { |
| 292 | u64 start; |
| 293 | u64 ram_size; |
| 294 | u64 compressed_size; |
| 295 | struct page **pages; |
| 296 | unsigned long nr_pages; |
Li Zefan | 261507a0 | 2010-12-17 14:21:50 +0800 | [diff] [blame] | 297 | int compress_type; |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 298 | struct list_head list; |
| 299 | }; |
| 300 | |
| 301 | struct async_cow { |
| 302 | struct inode *inode; |
| 303 | struct btrfs_root *root; |
| 304 | struct page *locked_page; |
| 305 | u64 start; |
| 306 | u64 end; |
| 307 | struct list_head extents; |
| 308 | struct btrfs_work work; |
| 309 | }; |
| 310 | |
| 311 | static noinline int add_async_extent(struct async_cow *cow, |
| 312 | u64 start, u64 ram_size, |
| 313 | u64 compressed_size, |
| 314 | struct page **pages, |
Li Zefan | 261507a0 | 2010-12-17 14:21:50 +0800 | [diff] [blame] | 315 | unsigned long nr_pages, |
| 316 | int compress_type) |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 317 | { |
| 318 | struct async_extent *async_extent; |
| 319 | |
| 320 | async_extent = kmalloc(sizeof(*async_extent), GFP_NOFS); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 321 | BUG_ON(!async_extent); /* -ENOMEM */ |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 322 | async_extent->start = start; |
| 323 | async_extent->ram_size = ram_size; |
| 324 | async_extent->compressed_size = compressed_size; |
| 325 | async_extent->pages = pages; |
| 326 | async_extent->nr_pages = nr_pages; |
Li Zefan | 261507a0 | 2010-12-17 14:21:50 +0800 | [diff] [blame] | 327 | async_extent->compress_type = compress_type; |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 328 | list_add_tail(&async_extent->list, &cow->extents); |
| 329 | return 0; |
| 330 | } |
| 331 | |
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 | * we create compressed extents in two phases. The first |
| 334 | * phase compresses a range of pages that have already been |
| 335 | * locked (both pages and state bits are locked). |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 336 | * |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 337 | * This is done inside an ordered work queue, and the compression |
| 338 | * is spread across many cpus. The actual IO submission is step |
| 339 | * two, and the ordered work queue takes care of making sure that |
| 340 | * happens in the same order things were put onto the queue by |
| 341 | * writepages and friends. |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 342 | * |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 343 | * If this code finds it can't get good compression, it puts an |
| 344 | * entry onto the work queue to write the uncompressed bytes. This |
| 345 | * makes sure that both compressed inodes and uncompressed inodes |
Artem Bityutskiy | b257031 | 2012-07-25 18:12:06 +0300 | [diff] [blame] | 346 | * are written in the same order that the flusher thread sent them |
| 347 | * down. |
Chris Mason | d352ac6 | 2008-09-29 15:18:18 -0400 | [diff] [blame] | 348 | */ |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 349 | static noinline int compress_file_range(struct inode *inode, |
| 350 | struct page *locked_page, |
| 351 | u64 start, u64 end, |
| 352 | struct async_cow *async_cow, |
| 353 | int *num_added) |
Chris Mason | b888db2 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 354 | { |
| 355 | struct btrfs_root *root = BTRFS_I(inode)->root; |
Chris Mason | db94535 | 2007-10-15 16:15:53 -0400 | [diff] [blame] | 356 | u64 num_bytes; |
Chris Mason | db94535 | 2007-10-15 16:15:53 -0400 | [diff] [blame] | 357 | u64 blocksize = root->sectorsize; |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 358 | u64 actual_end; |
Chris Mason | 42dc7ba | 2008-12-15 11:44:56 -0500 | [diff] [blame] | 359 | u64 isize = i_size_read(inode); |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 360 | int ret = 0; |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 361 | struct page **pages = NULL; |
| 362 | unsigned long nr_pages; |
| 363 | unsigned long nr_pages_ret = 0; |
| 364 | unsigned long total_compressed = 0; |
| 365 | unsigned long total_in = 0; |
| 366 | unsigned long max_compressed = 128 * 1024; |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 367 | unsigned long max_uncompressed = 128 * 1024; |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 368 | int i; |
| 369 | int will_compress; |
Li Zefan | 261507a0 | 2010-12-17 14:21:50 +0800 | [diff] [blame] | 370 | int compress_type = root->fs_info->compress_type; |
Chris Mason | 4adaa61 | 2013-03-26 13:07:00 -0400 | [diff] [blame] | 371 | int redirty = 0; |
Chris Mason | b888db2 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 372 | |
Liu Bo | 4cb13e5 | 2012-03-29 09:57:45 -0400 | [diff] [blame] | 373 | /* if this is a small write inside eof, kick off a defrag */ |
| 374 | if ((end - start + 1) < 16 * 1024 && |
| 375 | (start > 0 || end + 1 < BTRFS_I(inode)->disk_i_size)) |
Chris Mason | 4cb5300 | 2011-05-24 15:35:30 -0400 | [diff] [blame] | 376 | btrfs_add_inode_defrag(NULL, inode); |
| 377 | |
Chris Mason | 42dc7ba | 2008-12-15 11:44:56 -0500 | [diff] [blame] | 378 | actual_end = min_t(u64, isize, end + 1); |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 379 | again: |
| 380 | will_compress = 0; |
| 381 | nr_pages = (end >> PAGE_CACHE_SHIFT) - (start >> PAGE_CACHE_SHIFT) + 1; |
| 382 | nr_pages = min(nr_pages, (128 * 1024UL) / PAGE_CACHE_SIZE); |
| 383 | |
Chris Mason | f03d9301f | 2009-02-04 09:31:06 -0500 | [diff] [blame] | 384 | /* |
| 385 | * we don't want to send crud past the end of i_size through |
| 386 | * compression, that's just a waste of CPU time. So, if the |
| 387 | * end of the file is before the start of our current |
| 388 | * requested range of bytes, we bail out to the uncompressed |
| 389 | * cleanup code that can deal with all of this. |
| 390 | * |
| 391 | * It isn't really the fastest way to fix things, but this is a |
| 392 | * very uncommon corner. |
| 393 | */ |
| 394 | if (actual_end <= start) |
| 395 | goto cleanup_and_bail_uncompressed; |
| 396 | |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 397 | total_compressed = actual_end - start; |
| 398 | |
| 399 | /* we want to make sure that amount of ram required to uncompress |
| 400 | * an extent is reasonable, so we limit the total size in ram |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 401 | * of a compressed extent to 128k. This is a crucial number |
| 402 | * because it also controls how easily we can spread reads across |
| 403 | * cpus for decompression. |
| 404 | * |
| 405 | * We also want to make sure the amount of IO required to do |
| 406 | * a random read is reasonably small, so we limit the size of |
| 407 | * a compressed extent to 128k. |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 408 | */ |
| 409 | total_compressed = min(total_compressed, max_uncompressed); |
Qu Wenruo | fda2832 | 2013-02-26 08:10:22 +0000 | [diff] [blame] | 410 | num_bytes = ALIGN(end - start + 1, blocksize); |
Chris Mason | be20aa9 | 2007-12-17 20:14:01 -0500 | [diff] [blame] | 411 | num_bytes = max(blocksize, num_bytes); |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 412 | total_in = 0; |
| 413 | ret = 0; |
Chris Mason | db94535 | 2007-10-15 16:15:53 -0400 | [diff] [blame] | 414 | |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 415 | /* |
| 416 | * we do compression for mount -o compress and when the |
| 417 | * inode has not been flagged as nocompress. This flag can |
| 418 | * change at any time if we discover bad compression ratios. |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 419 | */ |
Christoph Hellwig | 6cbff00 | 2009-04-17 10:37:41 +0200 | [diff] [blame] | 420 | if (!(BTRFS_I(inode)->flags & BTRFS_INODE_NOCOMPRESS) && |
Chris Mason | 1e701a3 | 2010-03-11 09:42:04 -0500 | [diff] [blame] | 421 | (btrfs_test_opt(root, COMPRESS) || |
Liu Bo | 75e7cb7 | 2011-03-22 10:12:20 +0000 | [diff] [blame] | 422 | (BTRFS_I(inode)->force_compress) || |
| 423 | (BTRFS_I(inode)->flags & BTRFS_INODE_COMPRESS))) { |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 424 | WARN_ON(pages); |
Chris Mason | cfbc246 | 2008-10-30 13:22:14 -0400 | [diff] [blame] | 425 | pages = kzalloc(sizeof(struct page *) * nr_pages, GFP_NOFS); |
Li Zefan | 560f7d7 | 2011-09-08 10:22:01 +0800 | [diff] [blame] | 426 | if (!pages) { |
| 427 | /* just bail out to the uncompressed code */ |
| 428 | goto cont; |
| 429 | } |
Chris Mason | 179e29e | 2007-11-01 11:28:41 -0400 | [diff] [blame] | 430 | |
Li Zefan | 261507a0 | 2010-12-17 14:21:50 +0800 | [diff] [blame] | 431 | if (BTRFS_I(inode)->force_compress) |
| 432 | compress_type = BTRFS_I(inode)->force_compress; |
| 433 | |
Chris Mason | 4adaa61 | 2013-03-26 13:07:00 -0400 | [diff] [blame] | 434 | /* |
| 435 | * we need to call clear_page_dirty_for_io on each |
| 436 | * page in the range. Otherwise applications with the file |
| 437 | * mmap'd can wander in and change the page contents while |
| 438 | * we are compressing them. |
| 439 | * |
| 440 | * If the compression fails for any reason, we set the pages |
| 441 | * dirty again later on. |
| 442 | */ |
| 443 | extent_range_clear_dirty_for_io(inode, start, end); |
| 444 | redirty = 1; |
Li Zefan | 261507a0 | 2010-12-17 14:21:50 +0800 | [diff] [blame] | 445 | ret = btrfs_compress_pages(compress_type, |
| 446 | inode->i_mapping, start, |
| 447 | total_compressed, pages, |
| 448 | nr_pages, &nr_pages_ret, |
| 449 | &total_in, |
| 450 | &total_compressed, |
| 451 | max_compressed); |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 452 | |
| 453 | if (!ret) { |
| 454 | unsigned long offset = total_compressed & |
| 455 | (PAGE_CACHE_SIZE - 1); |
| 456 | struct page *page = pages[nr_pages_ret - 1]; |
| 457 | char *kaddr; |
| 458 | |
| 459 | /* zero the tail end of the last page, we might be |
| 460 | * sending it down to disk |
| 461 | */ |
| 462 | if (offset) { |
Cong Wang | 7ac687d | 2011-11-25 23:14:28 +0800 | [diff] [blame] | 463 | kaddr = kmap_atomic(page); |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 464 | memset(kaddr + offset, 0, |
| 465 | PAGE_CACHE_SIZE - offset); |
Cong Wang | 7ac687d | 2011-11-25 23:14:28 +0800 | [diff] [blame] | 466 | kunmap_atomic(kaddr); |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 467 | } |
| 468 | will_compress = 1; |
| 469 | } |
| 470 | } |
Li Zefan | 560f7d7 | 2011-09-08 10:22:01 +0800 | [diff] [blame] | 471 | cont: |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 472 | if (start == 0) { |
| 473 | /* lets try to make an inline extent */ |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 474 | if (ret || total_in < (actual_end - start)) { |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 475 | /* we didn't compress the entire range, try |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 476 | * to make an uncompressed inline extent. |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 477 | */ |
Josef Bacik | 0036158 | 2013-08-14 14:02:47 -0400 | [diff] [blame] | 478 | ret = cow_file_range_inline(root, inode, start, end, |
| 479 | 0, 0, NULL); |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 480 | } else { |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 481 | /* try making a compressed inline extent */ |
Josef Bacik | 0036158 | 2013-08-14 14:02:47 -0400 | [diff] [blame] | 482 | ret = cow_file_range_inline(root, inode, 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) { |
Josef Bacik | 151a41b | 2013-07-29 13:22:24 -0400 | [diff] [blame] | 487 | unsigned long clear_flags = EXTENT_DELALLOC | |
| 488 | EXTENT_DEFRAG; |
| 489 | clear_flags |= (ret < 0) ? EXTENT_DO_ACCOUNTING : 0; |
| 490 | |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 491 | /* |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 492 | * inline extent creation worked or returned error, |
| 493 | * we don't need to create any more async work items. |
| 494 | * Unlock and free up our temp pages. |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 495 | */ |
Josef Bacik | c2790a2 | 2013-07-29 11:20:47 -0400 | [diff] [blame] | 496 | extent_clear_unlock_delalloc(inode, start, end, NULL, |
Josef Bacik | 151a41b | 2013-07-29 13:22:24 -0400 | [diff] [blame] | 497 | clear_flags, PAGE_UNLOCK | |
Josef Bacik | c2790a2 | 2013-07-29 11:20:47 -0400 | [diff] [blame] | 498 | PAGE_CLEAR_DIRTY | |
| 499 | PAGE_SET_WRITEBACK | |
| 500 | PAGE_END_WRITEBACK); |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 501 | goto free_pages_out; |
| 502 | } |
| 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; |
| 593 | } |
| 594 | |
| 595 | /* |
| 596 | * phase two of compressed writeback. This is the ordered portion |
| 597 | * of the code, which only gets called in the order the work was |
| 598 | * queued. We walk all the async extents created by compress_file_range |
| 599 | * and send them down to the disk. |
| 600 | */ |
| 601 | static noinline int submit_compressed_extents(struct inode *inode, |
| 602 | struct async_cow *async_cow) |
| 603 | { |
| 604 | struct async_extent *async_extent; |
| 605 | u64 alloc_hint = 0; |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 606 | struct btrfs_key ins; |
| 607 | struct extent_map *em; |
| 608 | struct btrfs_root *root = BTRFS_I(inode)->root; |
| 609 | struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree; |
| 610 | struct extent_io_tree *io_tree; |
Josef Bacik | f5a84ee3 | 2009-11-10 21:23:48 -0500 | [diff] [blame] | 611 | int ret = 0; |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 612 | |
| 613 | if (list_empty(&async_cow->extents)) |
| 614 | return 0; |
| 615 | |
Josef Bacik | 3e04e7f | 2013-02-06 16:49:15 -0500 | [diff] [blame] | 616 | again: |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 617 | while (!list_empty(&async_cow->extents)) { |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 618 | async_extent = list_entry(async_cow->extents.next, |
| 619 | struct async_extent, list); |
| 620 | list_del(&async_extent->list); |
| 621 | |
| 622 | io_tree = &BTRFS_I(inode)->io_tree; |
| 623 | |
Josef Bacik | f5a84ee3 | 2009-11-10 21:23:48 -0500 | [diff] [blame] | 624 | retry: |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 625 | /* did the compression code fall back to uncompressed IO? */ |
| 626 | if (!async_extent->pages) { |
| 627 | int page_started = 0; |
| 628 | unsigned long nr_written = 0; |
| 629 | |
| 630 | lock_extent(io_tree, async_extent->start, |
Josef Bacik | 2ac55d4 | 2010-02-03 19:33:23 +0000 | [diff] [blame] | 631 | async_extent->start + |
Jeff Mahoney | d008237 | 2012-03-01 14:57:19 +0100 | [diff] [blame] | 632 | async_extent->ram_size - 1); |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 633 | |
| 634 | /* allocate blocks */ |
Josef Bacik | f5a84ee3 | 2009-11-10 21:23:48 -0500 | [diff] [blame] | 635 | ret = cow_file_range(inode, async_cow->locked_page, |
| 636 | async_extent->start, |
| 637 | async_extent->start + |
| 638 | async_extent->ram_size - 1, |
| 639 | &page_started, &nr_written, 0); |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 640 | |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 641 | /* JDM XXX */ |
| 642 | |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 643 | /* |
| 644 | * if page_started, cow_file_range inserted an |
| 645 | * inline extent and took care of all the unlocking |
| 646 | * and IO for us. Otherwise, we need to submit |
| 647 | * all those pages down to the drive. |
| 648 | */ |
Josef Bacik | f5a84ee3 | 2009-11-10 21:23:48 -0500 | [diff] [blame] | 649 | if (!page_started && !ret) |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 650 | extent_write_locked_range(io_tree, |
| 651 | inode, async_extent->start, |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 652 | async_extent->start + |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 653 | async_extent->ram_size - 1, |
| 654 | btrfs_get_extent, |
| 655 | WB_SYNC_ALL); |
Josef Bacik | 3e04e7f | 2013-02-06 16:49:15 -0500 | [diff] [blame] | 656 | else if (ret) |
| 657 | unlock_page(async_cow->locked_page); |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 658 | kfree(async_extent); |
| 659 | cond_resched(); |
| 660 | continue; |
| 661 | } |
| 662 | |
| 663 | lock_extent(io_tree, async_extent->start, |
Jeff Mahoney | d008237 | 2012-03-01 14:57:19 +0100 | [diff] [blame] | 664 | async_extent->start + async_extent->ram_size - 1); |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 665 | |
Josef Bacik | 0036158 | 2013-08-14 14:02:47 -0400 | [diff] [blame] | 666 | ret = btrfs_reserve_extent(root, |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 667 | async_extent->compressed_size, |
| 668 | async_extent->compressed_size, |
Josef Bacik | 81c9ad2 | 2012-01-18 10:56:06 -0500 | [diff] [blame] | 669 | 0, alloc_hint, &ins, 1); |
Josef Bacik | f5a84ee3 | 2009-11-10 21:23:48 -0500 | [diff] [blame] | 670 | if (ret) { |
| 671 | int i; |
Josef Bacik | 3e04e7f | 2013-02-06 16:49:15 -0500 | [diff] [blame] | 672 | |
Josef Bacik | f5a84ee3 | 2009-11-10 21:23:48 -0500 | [diff] [blame] | 673 | for (i = 0; i < async_extent->nr_pages; i++) { |
| 674 | WARN_ON(async_extent->pages[i]->mapping); |
| 675 | page_cache_release(async_extent->pages[i]); |
| 676 | } |
| 677 | kfree(async_extent->pages); |
| 678 | async_extent->nr_pages = 0; |
| 679 | async_extent->pages = NULL; |
Josef Bacik | 3e04e7f | 2013-02-06 16:49:15 -0500 | [diff] [blame] | 680 | |
Josef Bacik | fdf8e2e | 2013-06-14 16:58:23 -0400 | [diff] [blame] | 681 | if (ret == -ENOSPC) { |
| 682 | unlock_extent(io_tree, async_extent->start, |
| 683 | async_extent->start + |
| 684 | async_extent->ram_size - 1); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 685 | goto retry; |
Josef Bacik | fdf8e2e | 2013-06-14 16:58:23 -0400 | [diff] [blame] | 686 | } |
Josef Bacik | 3e04e7f | 2013-02-06 16:49:15 -0500 | [diff] [blame] | 687 | goto out_free; |
Josef Bacik | f5a84ee3 | 2009-11-10 21:23:48 -0500 | [diff] [blame] | 688 | } |
| 689 | |
Yan, Zheng | c216775 | 2009-11-12 09:34:21 +0000 | [diff] [blame] | 690 | /* |
| 691 | * here we're doing allocation and writeback of the |
| 692 | * compressed pages |
| 693 | */ |
| 694 | btrfs_drop_extent_cache(inode, async_extent->start, |
| 695 | async_extent->start + |
| 696 | async_extent->ram_size - 1, 0); |
| 697 | |
David Sterba | 172ddd6 | 2011-04-21 00:48:27 +0200 | [diff] [blame] | 698 | em = alloc_extent_map(); |
Liu Bo | b9aa55b | 2013-05-14 02:12:15 +0000 | [diff] [blame] | 699 | if (!em) { |
| 700 | ret = -ENOMEM; |
Josef Bacik | 3e04e7f | 2013-02-06 16:49:15 -0500 | [diff] [blame] | 701 | goto out_free_reserve; |
Liu Bo | b9aa55b | 2013-05-14 02:12:15 +0000 | [diff] [blame] | 702 | } |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 703 | em->start = async_extent->start; |
| 704 | em->len = async_extent->ram_size; |
Chris Mason | 445a694 | 2008-11-10 11:53:33 -0500 | [diff] [blame] | 705 | em->orig_start = em->start; |
Josef Bacik | 2ab28f3 | 2012-10-12 15:27:49 -0400 | [diff] [blame] | 706 | em->mod_start = em->start; |
| 707 | em->mod_len = em->len; |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 708 | |
| 709 | em->block_start = ins.objectid; |
| 710 | em->block_len = ins.offset; |
Josef Bacik | b493968 | 2012-12-03 10:31:19 -0500 | [diff] [blame] | 711 | em->orig_block_len = ins.offset; |
Josef Bacik | cc95bef | 2013-04-04 14:31:27 -0400 | [diff] [blame] | 712 | em->ram_bytes = async_extent->ram_size; |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 713 | em->bdev = root->fs_info->fs_devices->latest_bdev; |
Li Zefan | 261507a0 | 2010-12-17 14:21:50 +0800 | [diff] [blame] | 714 | em->compress_type = async_extent->compress_type; |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 715 | set_bit(EXTENT_FLAG_PINNED, &em->flags); |
| 716 | set_bit(EXTENT_FLAG_COMPRESSED, &em->flags); |
Josef Bacik | 70c8a91 | 2012-10-11 16:54:30 -0400 | [diff] [blame] | 717 | em->generation = -1; |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 718 | |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 719 | while (1) { |
Chris Mason | 890871b | 2009-09-02 16:24:52 -0400 | [diff] [blame] | 720 | write_lock(&em_tree->lock); |
Josef Bacik | 09a2a8f9 | 2013-04-05 16:51:15 -0400 | [diff] [blame] | 721 | ret = add_extent_mapping(em_tree, em, 1); |
Chris Mason | 890871b | 2009-09-02 16:24:52 -0400 | [diff] [blame] | 722 | write_unlock(&em_tree->lock); |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 723 | if (ret != -EEXIST) { |
| 724 | free_extent_map(em); |
| 725 | break; |
| 726 | } |
| 727 | btrfs_drop_extent_cache(inode, async_extent->start, |
| 728 | async_extent->start + |
| 729 | async_extent->ram_size - 1, 0); |
| 730 | } |
| 731 | |
Josef Bacik | 3e04e7f | 2013-02-06 16:49:15 -0500 | [diff] [blame] | 732 | if (ret) |
| 733 | goto out_free_reserve; |
| 734 | |
Li Zefan | 261507a0 | 2010-12-17 14:21:50 +0800 | [diff] [blame] | 735 | ret = btrfs_add_ordered_extent_compress(inode, |
| 736 | async_extent->start, |
| 737 | ins.objectid, |
| 738 | async_extent->ram_size, |
| 739 | ins.offset, |
| 740 | BTRFS_ORDERED_COMPRESSED, |
| 741 | async_extent->compress_type); |
Josef Bacik | 3e04e7f | 2013-02-06 16:49:15 -0500 | [diff] [blame] | 742 | if (ret) |
| 743 | goto out_free_reserve; |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 744 | |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 745 | /* |
| 746 | * clear dirty, set writeback and unlock the pages. |
| 747 | */ |
Josef Bacik | c2790a2 | 2013-07-29 11:20:47 -0400 | [diff] [blame] | 748 | extent_clear_unlock_delalloc(inode, async_extent->start, |
Chris Mason | a791e35 | 2009-10-08 11:27:10 -0400 | [diff] [blame] | 749 | async_extent->start + |
| 750 | async_extent->ram_size - 1, |
Josef Bacik | 151a41b | 2013-07-29 13:22:24 -0400 | [diff] [blame] | 751 | NULL, EXTENT_LOCKED | EXTENT_DELALLOC, |
| 752 | PAGE_UNLOCK | PAGE_CLEAR_DIRTY | |
Josef Bacik | c2790a2 | 2013-07-29 11:20:47 -0400 | [diff] [blame] | 753 | PAGE_SET_WRITEBACK); |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 754 | ret = btrfs_submit_compressed_write(inode, |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 755 | async_extent->start, |
| 756 | async_extent->ram_size, |
| 757 | ins.objectid, |
| 758 | ins.offset, async_extent->pages, |
| 759 | async_extent->nr_pages); |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 760 | alloc_hint = ins.objectid + ins.offset; |
| 761 | kfree(async_extent); |
Josef Bacik | 3e04e7f | 2013-02-06 16:49:15 -0500 | [diff] [blame] | 762 | if (ret) |
| 763 | goto out; |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 764 | cond_resched(); |
| 765 | } |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 766 | ret = 0; |
| 767 | out: |
| 768 | return ret; |
Josef Bacik | 3e04e7f | 2013-02-06 16:49:15 -0500 | [diff] [blame] | 769 | out_free_reserve: |
| 770 | btrfs_free_reserved_extent(root, ins.objectid, ins.offset); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 771 | out_free: |
Josef Bacik | c2790a2 | 2013-07-29 11:20:47 -0400 | [diff] [blame] | 772 | extent_clear_unlock_delalloc(inode, async_extent->start, |
Josef Bacik | 3e04e7f | 2013-02-06 16:49:15 -0500 | [diff] [blame] | 773 | async_extent->start + |
| 774 | async_extent->ram_size - 1, |
Josef Bacik | c2790a2 | 2013-07-29 11:20:47 -0400 | [diff] [blame] | 775 | NULL, EXTENT_LOCKED | EXTENT_DELALLOC | |
Josef Bacik | 151a41b | 2013-07-29 13:22:24 -0400 | [diff] [blame] | 776 | EXTENT_DEFRAG | EXTENT_DO_ACCOUNTING, |
| 777 | PAGE_UNLOCK | PAGE_CLEAR_DIRTY | |
| 778 | PAGE_SET_WRITEBACK | PAGE_END_WRITEBACK); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 779 | kfree(async_extent); |
Josef Bacik | 3e04e7f | 2013-02-06 16:49:15 -0500 | [diff] [blame] | 780 | goto again; |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 781 | } |
| 782 | |
Josef Bacik | 4b46fce | 2010-05-23 11:00:55 -0400 | [diff] [blame] | 783 | static u64 get_extent_allocation_hint(struct inode *inode, u64 start, |
| 784 | u64 num_bytes) |
| 785 | { |
| 786 | struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree; |
| 787 | struct extent_map *em; |
| 788 | u64 alloc_hint = 0; |
| 789 | |
| 790 | read_lock(&em_tree->lock); |
| 791 | em = search_extent_mapping(em_tree, start, num_bytes); |
| 792 | if (em) { |
| 793 | /* |
| 794 | * if block start isn't an actual block number then find the |
| 795 | * first block in this inode and use that as a hint. If that |
| 796 | * block is also bogus then just don't worry about it. |
| 797 | */ |
| 798 | if (em->block_start >= EXTENT_MAP_LAST_BYTE) { |
| 799 | free_extent_map(em); |
| 800 | em = search_extent_mapping(em_tree, 0, 0); |
| 801 | if (em && em->block_start < EXTENT_MAP_LAST_BYTE) |
| 802 | alloc_hint = em->block_start; |
| 803 | if (em) |
| 804 | free_extent_map(em); |
| 805 | } else { |
| 806 | alloc_hint = em->block_start; |
| 807 | free_extent_map(em); |
| 808 | } |
| 809 | } |
| 810 | read_unlock(&em_tree->lock); |
| 811 | |
| 812 | return alloc_hint; |
| 813 | } |
| 814 | |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 815 | /* |
| 816 | * when extent_io.c finds a delayed allocation range in the file, |
| 817 | * the call backs end up in this code. The basic idea is to |
| 818 | * allocate extents on disk for the range, and create ordered data structs |
| 819 | * in ram to track those extents. |
| 820 | * |
| 821 | * locked_page is the page that writepage had locked already. We use |
| 822 | * it to make sure we don't do extra locks or unlocks. |
| 823 | * |
| 824 | * *page_started is set to one if we unlock locked_page and do everything |
| 825 | * required to start IO on it. It may be clean and already done with |
| 826 | * IO when we return. |
| 827 | */ |
Josef Bacik | 0036158 | 2013-08-14 14:02:47 -0400 | [diff] [blame] | 828 | static noinline int cow_file_range(struct inode *inode, |
| 829 | struct page *locked_page, |
| 830 | u64 start, u64 end, int *page_started, |
| 831 | unsigned long *nr_written, |
| 832 | int unlock) |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 833 | { |
Josef Bacik | 0036158 | 2013-08-14 14:02:47 -0400 | [diff] [blame] | 834 | struct btrfs_root *root = BTRFS_I(inode)->root; |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 835 | u64 alloc_hint = 0; |
| 836 | u64 num_bytes; |
| 837 | unsigned long ram_size; |
| 838 | u64 disk_num_bytes; |
| 839 | u64 cur_alloc_size; |
| 840 | u64 blocksize = root->sectorsize; |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 841 | struct btrfs_key ins; |
| 842 | struct extent_map *em; |
| 843 | struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree; |
| 844 | int ret = 0; |
| 845 | |
Josef Bacik | 02ecd2c | 2013-10-25 16:19:08 -0400 | [diff] [blame] | 846 | if (btrfs_is_free_space_inode(inode)) { |
| 847 | WARN_ON_ONCE(1); |
| 848 | return -EINVAL; |
| 849 | } |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 850 | |
Qu Wenruo | fda2832 | 2013-02-26 08:10:22 +0000 | [diff] [blame] | 851 | num_bytes = ALIGN(end - start + 1, blocksize); |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 852 | num_bytes = max(blocksize, num_bytes); |
| 853 | disk_num_bytes = num_bytes; |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 854 | |
Chris Mason | 4cb5300 | 2011-05-24 15:35:30 -0400 | [diff] [blame] | 855 | /* if this is a small write inside eof, kick off defrag */ |
Liu Bo | 4cb13e5 | 2012-03-29 09:57:45 -0400 | [diff] [blame] | 856 | if (num_bytes < 64 * 1024 && |
| 857 | (start > 0 || end + 1 < BTRFS_I(inode)->disk_i_size)) |
Josef Bacik | 0036158 | 2013-08-14 14:02:47 -0400 | [diff] [blame] | 858 | btrfs_add_inode_defrag(NULL, inode); |
Chris Mason | 4cb5300 | 2011-05-24 15:35:30 -0400 | [diff] [blame] | 859 | |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 860 | if (start == 0) { |
| 861 | /* lets try to make an inline extent */ |
Josef Bacik | 0036158 | 2013-08-14 14:02:47 -0400 | [diff] [blame] | 862 | ret = cow_file_range_inline(root, inode, start, end, 0, 0, |
| 863 | NULL); |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 864 | if (ret == 0) { |
Josef Bacik | c2790a2 | 2013-07-29 11:20:47 -0400 | [diff] [blame] | 865 | extent_clear_unlock_delalloc(inode, start, end, NULL, |
| 866 | EXTENT_LOCKED | EXTENT_DELALLOC | |
Josef Bacik | 151a41b | 2013-07-29 13:22:24 -0400 | [diff] [blame] | 867 | EXTENT_DEFRAG, PAGE_UNLOCK | |
Josef Bacik | c2790a2 | 2013-07-29 11:20:47 -0400 | [diff] [blame] | 868 | PAGE_CLEAR_DIRTY | PAGE_SET_WRITEBACK | |
| 869 | PAGE_END_WRITEBACK); |
Yan, Zheng | c216775 | 2009-11-12 09:34:21 +0000 | [diff] [blame] | 870 | |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 871 | *nr_written = *nr_written + |
| 872 | (end - start + PAGE_CACHE_SIZE) / PAGE_CACHE_SIZE; |
| 873 | *page_started = 1; |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 874 | goto out; |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 875 | } else if (ret < 0) { |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 876 | goto out_unlock; |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 877 | } |
| 878 | } |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 879 | |
| 880 | BUG_ON(disk_num_bytes > |
David Sterba | 6c41761 | 2011-04-13 15:41:04 +0200 | [diff] [blame] | 881 | btrfs_super_total_bytes(root->fs_info->super_copy)); |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 882 | |
Josef Bacik | 4b46fce | 2010-05-23 11:00:55 -0400 | [diff] [blame] | 883 | alloc_hint = get_extent_allocation_hint(inode, start, num_bytes); |
Zheng Yan | 5b21f2e | 2008-09-26 10:05:38 -0400 | [diff] [blame] | 884 | btrfs_drop_extent_cache(inode, start, start + num_bytes - 1, 0); |
Chris Mason | 3b95151 | 2008-04-17 11:29:12 -0400 | [diff] [blame] | 885 | |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 886 | while (disk_num_bytes > 0) { |
Chris Mason | a791e35 | 2009-10-08 11:27:10 -0400 | [diff] [blame] | 887 | unsigned long op; |
| 888 | |
Josef Bacik | 287a0ab | 2010-03-19 18:07:23 +0000 | [diff] [blame] | 889 | cur_alloc_size = disk_num_bytes; |
Josef Bacik | 0036158 | 2013-08-14 14:02:47 -0400 | [diff] [blame] | 890 | ret = btrfs_reserve_extent(root, cur_alloc_size, |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 891 | root->sectorsize, 0, alloc_hint, |
Josef Bacik | 81c9ad2 | 2012-01-18 10:56:06 -0500 | [diff] [blame] | 892 | &ins, 1); |
Josef Bacik | 0036158 | 2013-08-14 14:02:47 -0400 | [diff] [blame] | 893 | if (ret < 0) |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 894 | goto out_unlock; |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 895 | |
David Sterba | 172ddd6 | 2011-04-21 00:48:27 +0200 | [diff] [blame] | 896 | em = alloc_extent_map(); |
Liu Bo | b9aa55b | 2013-05-14 02:12:15 +0000 | [diff] [blame] | 897 | if (!em) { |
| 898 | ret = -ENOMEM; |
Liu Bo | ace68ba | 2013-04-22 10:53:47 +0000 | [diff] [blame] | 899 | goto out_reserve; |
Liu Bo | b9aa55b | 2013-05-14 02:12:15 +0000 | [diff] [blame] | 900 | } |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 901 | em->start = start; |
Chris Mason | 445a694 | 2008-11-10 11:53:33 -0500 | [diff] [blame] | 902 | em->orig_start = em->start; |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 903 | ram_size = ins.offset; |
| 904 | em->len = ins.offset; |
Josef Bacik | 2ab28f3 | 2012-10-12 15:27:49 -0400 | [diff] [blame] | 905 | em->mod_start = em->start; |
| 906 | em->mod_len = em->len; |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 907 | |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 908 | em->block_start = ins.objectid; |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 909 | em->block_len = ins.offset; |
Josef Bacik | b493968 | 2012-12-03 10:31:19 -0500 | [diff] [blame] | 910 | em->orig_block_len = ins.offset; |
Josef Bacik | cc95bef | 2013-04-04 14:31:27 -0400 | [diff] [blame] | 911 | em->ram_bytes = ram_size; |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 912 | em->bdev = root->fs_info->fs_devices->latest_bdev; |
Chris Mason | 7f3c74f | 2008-07-18 12:01:11 -0400 | [diff] [blame] | 913 | set_bit(EXTENT_FLAG_PINNED, &em->flags); |
Josef Bacik | 70c8a91 | 2012-10-11 16:54:30 -0400 | [diff] [blame] | 914 | em->generation = -1; |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 915 | |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 916 | while (1) { |
Chris Mason | 890871b | 2009-09-02 16:24:52 -0400 | [diff] [blame] | 917 | write_lock(&em_tree->lock); |
Josef Bacik | 09a2a8f9 | 2013-04-05 16:51:15 -0400 | [diff] [blame] | 918 | ret = add_extent_mapping(em_tree, em, 1); |
Chris Mason | 890871b | 2009-09-02 16:24:52 -0400 | [diff] [blame] | 919 | write_unlock(&em_tree->lock); |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 920 | if (ret != -EEXIST) { |
| 921 | free_extent_map(em); |
| 922 | break; |
| 923 | } |
| 924 | btrfs_drop_extent_cache(inode, start, |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 925 | start + ram_size - 1, 0); |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 926 | } |
Liu Bo | ace68ba | 2013-04-22 10:53:47 +0000 | [diff] [blame] | 927 | if (ret) |
| 928 | goto out_reserve; |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 929 | |
Chris Mason | 98d20f6 | 2008-04-14 09:46:10 -0400 | [diff] [blame] | 930 | cur_alloc_size = ins.offset; |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 931 | ret = btrfs_add_ordered_extent(inode, start, ins.objectid, |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 932 | ram_size, cur_alloc_size, 0); |
Liu Bo | ace68ba | 2013-04-22 10:53:47 +0000 | [diff] [blame] | 933 | if (ret) |
| 934 | goto out_reserve; |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 935 | |
Yan Zheng | 17d217f | 2008-12-12 10:03:38 -0500 | [diff] [blame] | 936 | if (root->root_key.objectid == |
| 937 | BTRFS_DATA_RELOC_TREE_OBJECTID) { |
| 938 | ret = btrfs_reloc_clone_csums(inode, start, |
| 939 | cur_alloc_size); |
Josef Bacik | 0036158 | 2013-08-14 14:02:47 -0400 | [diff] [blame] | 940 | if (ret) |
Liu Bo | ace68ba | 2013-04-22 10:53:47 +0000 | [diff] [blame] | 941 | goto out_reserve; |
Yan Zheng | 17d217f | 2008-12-12 10:03:38 -0500 | [diff] [blame] | 942 | } |
| 943 | |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 944 | if (disk_num_bytes < cur_alloc_size) |
Chris Mason | 3b95151 | 2008-04-17 11:29:12 -0400 | [diff] [blame] | 945 | break; |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 946 | |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 947 | /* we're not doing compressed IO, don't unlock the first |
| 948 | * page (which the caller expects to stay locked), don't |
| 949 | * clear any dirty bits and don't set any writeback bits |
Chris Mason | 8b62b72 | 2009-09-02 16:53:46 -0400 | [diff] [blame] | 950 | * |
| 951 | * Do set the Private2 bit so we know this page was properly |
| 952 | * setup for writepage |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 953 | */ |
Josef Bacik | c2790a2 | 2013-07-29 11:20:47 -0400 | [diff] [blame] | 954 | op = unlock ? PAGE_UNLOCK : 0; |
| 955 | op |= PAGE_SET_PRIVATE2; |
Chris Mason | a791e35 | 2009-10-08 11:27:10 -0400 | [diff] [blame] | 956 | |
Josef Bacik | c2790a2 | 2013-07-29 11:20:47 -0400 | [diff] [blame] | 957 | extent_clear_unlock_delalloc(inode, start, |
| 958 | start + ram_size - 1, locked_page, |
| 959 | EXTENT_LOCKED | EXTENT_DELALLOC, |
| 960 | op); |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 961 | disk_num_bytes -= cur_alloc_size; |
Chris Mason | c59f895 | 2007-12-17 20:14:04 -0500 | [diff] [blame] | 962 | num_bytes -= cur_alloc_size; |
| 963 | alloc_hint = ins.objectid + ins.offset; |
| 964 | start += cur_alloc_size; |
Chris Mason | b888db2 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 965 | } |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 966 | out: |
Chris Mason | be20aa9 | 2007-12-17 20:14:01 -0500 | [diff] [blame] | 967 | return ret; |
Miao Xie | b7d5b0a | 2012-11-01 07:32:18 +0000 | [diff] [blame] | 968 | |
Liu Bo | ace68ba | 2013-04-22 10:53:47 +0000 | [diff] [blame] | 969 | out_reserve: |
| 970 | btrfs_free_reserved_extent(root, ins.objectid, ins.offset); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 971 | out_unlock: |
Josef Bacik | c2790a2 | 2013-07-29 11:20:47 -0400 | [diff] [blame] | 972 | extent_clear_unlock_delalloc(inode, start, end, locked_page, |
Josef Bacik | 151a41b | 2013-07-29 13:22:24 -0400 | [diff] [blame] | 973 | EXTENT_LOCKED | EXTENT_DO_ACCOUNTING | |
| 974 | EXTENT_DELALLOC | EXTENT_DEFRAG, |
| 975 | PAGE_UNLOCK | PAGE_CLEAR_DIRTY | |
| 976 | PAGE_SET_WRITEBACK | PAGE_END_WRITEBACK); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 977 | goto out; |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 978 | } |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 979 | |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 980 | /* |
| 981 | * work queue call back to started compression on a file and pages |
| 982 | */ |
| 983 | static noinline void async_cow_start(struct btrfs_work *work) |
| 984 | { |
| 985 | struct async_cow *async_cow; |
| 986 | int num_added = 0; |
| 987 | async_cow = container_of(work, struct async_cow, work); |
| 988 | |
| 989 | compress_file_range(async_cow->inode, async_cow->locked_page, |
| 990 | async_cow->start, async_cow->end, async_cow, |
| 991 | &num_added); |
Josef Bacik | 8180ef8 | 2012-06-08 15:16:12 -0400 | [diff] [blame] | 992 | if (num_added == 0) { |
Josef Bacik | cb77fcd | 2012-06-15 12:19:48 -0600 | [diff] [blame] | 993 | btrfs_add_delayed_iput(async_cow->inode); |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 994 | async_cow->inode = NULL; |
Josef Bacik | 8180ef8 | 2012-06-08 15:16:12 -0400 | [diff] [blame] | 995 | } |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 996 | } |
| 997 | |
| 998 | /* |
| 999 | * work queue call back to submit previously compressed pages |
| 1000 | */ |
| 1001 | static noinline void async_cow_submit(struct btrfs_work *work) |
| 1002 | { |
| 1003 | struct async_cow *async_cow; |
| 1004 | struct btrfs_root *root; |
| 1005 | unsigned long nr_pages; |
| 1006 | |
| 1007 | async_cow = container_of(work, struct async_cow, work); |
| 1008 | |
| 1009 | root = async_cow->root; |
| 1010 | nr_pages = (async_cow->end - async_cow->start + PAGE_CACHE_SIZE) >> |
| 1011 | PAGE_CACHE_SHIFT; |
| 1012 | |
Josef Bacik | 66657b3 | 2012-08-01 15:36:24 -0400 | [diff] [blame] | 1013 | if (atomic_sub_return(nr_pages, &root->fs_info->async_delalloc_pages) < |
Liu Bo | 287082b | 2012-06-28 04:02:24 -0600 | [diff] [blame] | 1014 | 5 * 1024 * 1024 && |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 1015 | waitqueue_active(&root->fs_info->async_submit_wait)) |
| 1016 | wake_up(&root->fs_info->async_submit_wait); |
| 1017 | |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 1018 | if (async_cow->inode) |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 1019 | submit_compressed_extents(async_cow->inode, async_cow); |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 1020 | } |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 1021 | |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 1022 | static noinline void async_cow_free(struct btrfs_work *work) |
| 1023 | { |
| 1024 | struct async_cow *async_cow; |
| 1025 | async_cow = container_of(work, struct async_cow, work); |
Josef Bacik | 8180ef8 | 2012-06-08 15:16:12 -0400 | [diff] [blame] | 1026 | if (async_cow->inode) |
Josef Bacik | cb77fcd | 2012-06-15 12:19:48 -0600 | [diff] [blame] | 1027 | btrfs_add_delayed_iput(async_cow->inode); |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 1028 | kfree(async_cow); |
| 1029 | } |
| 1030 | |
| 1031 | static int cow_file_range_async(struct inode *inode, struct page *locked_page, |
| 1032 | u64 start, u64 end, int *page_started, |
| 1033 | unsigned long *nr_written) |
| 1034 | { |
| 1035 | struct async_cow *async_cow; |
| 1036 | struct btrfs_root *root = BTRFS_I(inode)->root; |
| 1037 | unsigned long nr_pages; |
| 1038 | u64 cur_end; |
Liu Bo | 287082b | 2012-06-28 04:02:24 -0600 | [diff] [blame] | 1039 | int limit = 10 * 1024 * 1024; |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 1040 | |
Chris Mason | a3429ab | 2009-10-08 12:30:20 -0400 | [diff] [blame] | 1041 | clear_extent_bit(&BTRFS_I(inode)->io_tree, start, end, EXTENT_LOCKED, |
| 1042 | 1, 0, NULL, GFP_NOFS); |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 1043 | while (start < end) { |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 1044 | async_cow = kmalloc(sizeof(*async_cow), GFP_NOFS); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 1045 | BUG_ON(!async_cow); /* -ENOMEM */ |
Josef Bacik | 8180ef8 | 2012-06-08 15:16:12 -0400 | [diff] [blame] | 1046 | async_cow->inode = igrab(inode); |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 1047 | async_cow->root = root; |
| 1048 | async_cow->locked_page = locked_page; |
| 1049 | async_cow->start = start; |
| 1050 | |
Christoph Hellwig | 6cbff00 | 2009-04-17 10:37:41 +0200 | [diff] [blame] | 1051 | if (BTRFS_I(inode)->flags & BTRFS_INODE_NOCOMPRESS) |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 1052 | cur_end = end; |
| 1053 | else |
| 1054 | cur_end = min(end, start + 512 * 1024 - 1); |
| 1055 | |
| 1056 | async_cow->end = cur_end; |
| 1057 | INIT_LIST_HEAD(&async_cow->extents); |
| 1058 | |
| 1059 | async_cow->work.func = async_cow_start; |
| 1060 | async_cow->work.ordered_func = async_cow_submit; |
| 1061 | async_cow->work.ordered_free = async_cow_free; |
| 1062 | async_cow->work.flags = 0; |
| 1063 | |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 1064 | nr_pages = (cur_end - start + PAGE_CACHE_SIZE) >> |
| 1065 | PAGE_CACHE_SHIFT; |
| 1066 | atomic_add(nr_pages, &root->fs_info->async_delalloc_pages); |
| 1067 | |
| 1068 | btrfs_queue_worker(&root->fs_info->delalloc_workers, |
| 1069 | &async_cow->work); |
| 1070 | |
| 1071 | if (atomic_read(&root->fs_info->async_delalloc_pages) > limit) { |
| 1072 | wait_event(root->fs_info->async_submit_wait, |
| 1073 | (atomic_read(&root->fs_info->async_delalloc_pages) < |
| 1074 | limit)); |
| 1075 | } |
| 1076 | |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 1077 | while (atomic_read(&root->fs_info->async_submit_draining) && |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 1078 | atomic_read(&root->fs_info->async_delalloc_pages)) { |
| 1079 | wait_event(root->fs_info->async_submit_wait, |
| 1080 | (atomic_read(&root->fs_info->async_delalloc_pages) == |
| 1081 | 0)); |
| 1082 | } |
| 1083 | |
| 1084 | *nr_written += nr_pages; |
| 1085 | start = cur_end + 1; |
| 1086 | } |
| 1087 | *page_started = 1; |
| 1088 | return 0; |
Chris Mason | be20aa9 | 2007-12-17 20:14:01 -0500 | [diff] [blame] | 1089 | } |
| 1090 | |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 1091 | static noinline int csum_exist_in_range(struct btrfs_root *root, |
Yan Zheng | 17d217f | 2008-12-12 10:03:38 -0500 | [diff] [blame] | 1092 | u64 bytenr, u64 num_bytes) |
| 1093 | { |
| 1094 | int ret; |
| 1095 | struct btrfs_ordered_sum *sums; |
| 1096 | LIST_HEAD(list); |
| 1097 | |
Yan Zheng | 07d400a | 2009-01-06 11:42:00 -0500 | [diff] [blame] | 1098 | ret = btrfs_lookup_csums_range(root->fs_info->csum_root, bytenr, |
Arne Jansen | a2de733 | 2011-03-08 14:14:00 +0100 | [diff] [blame] | 1099 | bytenr + num_bytes - 1, &list, 0); |
Yan Zheng | 17d217f | 2008-12-12 10:03:38 -0500 | [diff] [blame] | 1100 | if (ret == 0 && list_empty(&list)) |
| 1101 | return 0; |
| 1102 | |
| 1103 | while (!list_empty(&list)) { |
| 1104 | sums = list_entry(list.next, struct btrfs_ordered_sum, list); |
| 1105 | list_del(&sums->list); |
| 1106 | kfree(sums); |
| 1107 | } |
| 1108 | return 1; |
| 1109 | } |
| 1110 | |
Chris Mason | d352ac6 | 2008-09-29 15:18:18 -0400 | [diff] [blame] | 1111 | /* |
| 1112 | * when nowcow writeback call back. This checks for snapshots or COW copies |
| 1113 | * of the extents that exist in the file, and COWs the file as required. |
| 1114 | * |
| 1115 | * If no cow copies or snapshots exist, we write directly to the existing |
| 1116 | * blocks on disk |
| 1117 | */ |
Chris Mason | 7f366cf | 2009-03-12 20:12:45 -0400 | [diff] [blame] | 1118 | static noinline int run_delalloc_nocow(struct inode *inode, |
| 1119 | struct page *locked_page, |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 1120 | u64 start, u64 end, int *page_started, int force, |
| 1121 | unsigned long *nr_written) |
Chris Mason | be20aa9 | 2007-12-17 20:14:01 -0500 | [diff] [blame] | 1122 | { |
Chris Mason | be20aa9 | 2007-12-17 20:14:01 -0500 | [diff] [blame] | 1123 | struct btrfs_root *root = BTRFS_I(inode)->root; |
Yan Zheng | 7ea394f | 2008-08-05 13:05:02 -0400 | [diff] [blame] | 1124 | struct btrfs_trans_handle *trans; |
Chris Mason | be20aa9 | 2007-12-17 20:14:01 -0500 | [diff] [blame] | 1125 | struct extent_buffer *leaf; |
Chris Mason | be20aa9 | 2007-12-17 20:14:01 -0500 | [diff] [blame] | 1126 | struct btrfs_path *path; |
Yan Zheng | 80ff385 | 2008-10-30 14:20:02 -0400 | [diff] [blame] | 1127 | struct btrfs_file_extent_item *fi; |
Chris Mason | be20aa9 | 2007-12-17 20:14:01 -0500 | [diff] [blame] | 1128 | struct btrfs_key found_key; |
Yan Zheng | 80ff385 | 2008-10-30 14:20:02 -0400 | [diff] [blame] | 1129 | u64 cow_start; |
| 1130 | u64 cur_offset; |
| 1131 | u64 extent_end; |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 1132 | u64 extent_offset; |
Yan Zheng | 80ff385 | 2008-10-30 14:20:02 -0400 | [diff] [blame] | 1133 | u64 disk_bytenr; |
| 1134 | u64 num_bytes; |
Josef Bacik | b493968 | 2012-12-03 10:31:19 -0500 | [diff] [blame] | 1135 | u64 disk_num_bytes; |
Josef Bacik | cc95bef | 2013-04-04 14:31:27 -0400 | [diff] [blame] | 1136 | u64 ram_bytes; |
Yan Zheng | 80ff385 | 2008-10-30 14:20:02 -0400 | [diff] [blame] | 1137 | int extent_type; |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 1138 | int ret, err; |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 1139 | int type; |
Yan Zheng | 80ff385 | 2008-10-30 14:20:02 -0400 | [diff] [blame] | 1140 | int nocow; |
| 1141 | int check_prev = 1; |
Li Zefan | 82d5902 | 2011-04-20 10:33:24 +0800 | [diff] [blame] | 1142 | bool nolock; |
Li Zefan | 33345d01 | 2011-04-20 10:31:50 +0800 | [diff] [blame] | 1143 | u64 ino = btrfs_ino(inode); |
Chris Mason | be20aa9 | 2007-12-17 20:14:01 -0500 | [diff] [blame] | 1144 | |
| 1145 | path = btrfs_alloc_path(); |
Josef Bacik | 17ca04a | 2012-05-31 15:58:55 -0400 | [diff] [blame] | 1146 | if (!path) { |
Josef Bacik | c2790a2 | 2013-07-29 11:20:47 -0400 | [diff] [blame] | 1147 | extent_clear_unlock_delalloc(inode, start, end, locked_page, |
| 1148 | EXTENT_LOCKED | EXTENT_DELALLOC | |
Josef Bacik | 151a41b | 2013-07-29 13:22:24 -0400 | [diff] [blame] | 1149 | EXTENT_DO_ACCOUNTING | |
| 1150 | EXTENT_DEFRAG, PAGE_UNLOCK | |
Josef Bacik | c2790a2 | 2013-07-29 11:20:47 -0400 | [diff] [blame] | 1151 | PAGE_CLEAR_DIRTY | |
| 1152 | PAGE_SET_WRITEBACK | |
| 1153 | PAGE_END_WRITEBACK); |
Mark Fasheh | d8926bb | 2011-07-13 10:38:47 -0700 | [diff] [blame] | 1154 | return -ENOMEM; |
Josef Bacik | 17ca04a | 2012-05-31 15:58:55 -0400 | [diff] [blame] | 1155 | } |
Li Zefan | 82d5902 | 2011-04-20 10:33:24 +0800 | [diff] [blame] | 1156 | |
Liu Bo | 83eea1f | 2012-07-10 05:28:39 -0600 | [diff] [blame] | 1157 | nolock = btrfs_is_free_space_inode(inode); |
Li Zefan | 82d5902 | 2011-04-20 10:33:24 +0800 | [diff] [blame] | 1158 | |
| 1159 | if (nolock) |
Josef Bacik | 7a7eaa4 | 2011-04-13 12:54:33 -0400 | [diff] [blame] | 1160 | trans = btrfs_join_transaction_nolock(root); |
Li Zefan | 82d5902 | 2011-04-20 10:33:24 +0800 | [diff] [blame] | 1161 | else |
Josef Bacik | 7a7eaa4 | 2011-04-13 12:54:33 -0400 | [diff] [blame] | 1162 | trans = btrfs_join_transaction(root); |
Chris Mason | ff5714c | 2011-05-28 07:00:39 -0400 | [diff] [blame] | 1163 | |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 1164 | if (IS_ERR(trans)) { |
Josef Bacik | c2790a2 | 2013-07-29 11:20:47 -0400 | [diff] [blame] | 1165 | extent_clear_unlock_delalloc(inode, start, end, locked_page, |
| 1166 | EXTENT_LOCKED | EXTENT_DELALLOC | |
Josef Bacik | 151a41b | 2013-07-29 13:22:24 -0400 | [diff] [blame] | 1167 | EXTENT_DO_ACCOUNTING | |
| 1168 | EXTENT_DEFRAG, PAGE_UNLOCK | |
Josef Bacik | c2790a2 | 2013-07-29 11:20:47 -0400 | [diff] [blame] | 1169 | PAGE_CLEAR_DIRTY | |
| 1170 | PAGE_SET_WRITEBACK | |
| 1171 | PAGE_END_WRITEBACK); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 1172 | btrfs_free_path(path); |
| 1173 | return PTR_ERR(trans); |
| 1174 | } |
| 1175 | |
Josef Bacik | 74b2107 | 2011-04-13 12:02:53 -0400 | [diff] [blame] | 1176 | trans->block_rsv = &root->fs_info->delalloc_block_rsv; |
Chris Mason | be20aa9 | 2007-12-17 20:14:01 -0500 | [diff] [blame] | 1177 | |
Yan Zheng | 80ff385 | 2008-10-30 14:20:02 -0400 | [diff] [blame] | 1178 | cow_start = (u64)-1; |
| 1179 | cur_offset = start; |
| 1180 | while (1) { |
Li Zefan | 33345d01 | 2011-04-20 10:31:50 +0800 | [diff] [blame] | 1181 | ret = btrfs_lookup_file_extent(trans, root, path, ino, |
Yan Zheng | 80ff385 | 2008-10-30 14:20:02 -0400 | [diff] [blame] | 1182 | cur_offset, 0); |
Josef Bacik | d788a34 | 2013-10-25 16:55:08 -0400 | [diff] [blame] | 1183 | if (ret < 0) |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 1184 | goto error; |
Yan Zheng | 80ff385 | 2008-10-30 14:20:02 -0400 | [diff] [blame] | 1185 | if (ret > 0 && path->slots[0] > 0 && check_prev) { |
| 1186 | leaf = path->nodes[0]; |
| 1187 | btrfs_item_key_to_cpu(leaf, &found_key, |
| 1188 | path->slots[0] - 1); |
Li Zefan | 33345d01 | 2011-04-20 10:31:50 +0800 | [diff] [blame] | 1189 | if (found_key.objectid == ino && |
Yan Zheng | 80ff385 | 2008-10-30 14:20:02 -0400 | [diff] [blame] | 1190 | found_key.type == BTRFS_EXTENT_DATA_KEY) |
| 1191 | path->slots[0]--; |
| 1192 | } |
| 1193 | check_prev = 0; |
| 1194 | next_slot: |
| 1195 | leaf = path->nodes[0]; |
| 1196 | if (path->slots[0] >= btrfs_header_nritems(leaf)) { |
| 1197 | ret = btrfs_next_leaf(root, path); |
Josef Bacik | d788a34 | 2013-10-25 16:55:08 -0400 | [diff] [blame] | 1198 | if (ret < 0) |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 1199 | goto error; |
Yan Zheng | 80ff385 | 2008-10-30 14:20:02 -0400 | [diff] [blame] | 1200 | if (ret > 0) |
| 1201 | break; |
| 1202 | leaf = path->nodes[0]; |
| 1203 | } |
Chris Mason | be20aa9 | 2007-12-17 20:14:01 -0500 | [diff] [blame] | 1204 | |
Yan Zheng | 80ff385 | 2008-10-30 14:20:02 -0400 | [diff] [blame] | 1205 | nocow = 0; |
| 1206 | disk_bytenr = 0; |
Yan Zheng | 17d217f | 2008-12-12 10:03:38 -0500 | [diff] [blame] | 1207 | num_bytes = 0; |
Yan Zheng | 80ff385 | 2008-10-30 14:20:02 -0400 | [diff] [blame] | 1208 | btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]); |
Chris Mason | be20aa9 | 2007-12-17 20:14:01 -0500 | [diff] [blame] | 1209 | |
Li Zefan | 33345d01 | 2011-04-20 10:31:50 +0800 | [diff] [blame] | 1210 | if (found_key.objectid > ino || |
Yan Zheng | 80ff385 | 2008-10-30 14:20:02 -0400 | [diff] [blame] | 1211 | found_key.type > BTRFS_EXTENT_DATA_KEY || |
| 1212 | found_key.offset > end) |
| 1213 | break; |
Chris Mason | be20aa9 | 2007-12-17 20:14:01 -0500 | [diff] [blame] | 1214 | |
Yan Zheng | 80ff385 | 2008-10-30 14:20:02 -0400 | [diff] [blame] | 1215 | if (found_key.offset > cur_offset) { |
| 1216 | extent_end = found_key.offset; |
Chris Mason | e9061e2 | 2009-10-09 09:57:45 -0400 | [diff] [blame] | 1217 | extent_type = 0; |
Yan Zheng | 80ff385 | 2008-10-30 14:20:02 -0400 | [diff] [blame] | 1218 | goto out_check; |
| 1219 | } |
Chris Mason | c31f883 | 2008-01-08 15:46:31 -0500 | [diff] [blame] | 1220 | |
Yan Zheng | 80ff385 | 2008-10-30 14:20:02 -0400 | [diff] [blame] | 1221 | fi = btrfs_item_ptr(leaf, path->slots[0], |
| 1222 | struct btrfs_file_extent_item); |
| 1223 | extent_type = btrfs_file_extent_type(leaf, fi); |
Chris Mason | be20aa9 | 2007-12-17 20:14:01 -0500 | [diff] [blame] | 1224 | |
Josef Bacik | cc95bef | 2013-04-04 14:31:27 -0400 | [diff] [blame] | 1225 | ram_bytes = btrfs_file_extent_ram_bytes(leaf, fi); |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 1226 | if (extent_type == BTRFS_FILE_EXTENT_REG || |
| 1227 | extent_type == BTRFS_FILE_EXTENT_PREALLOC) { |
Yan Zheng | 80ff385 | 2008-10-30 14:20:02 -0400 | [diff] [blame] | 1228 | disk_bytenr = btrfs_file_extent_disk_bytenr(leaf, fi); |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 1229 | extent_offset = btrfs_file_extent_offset(leaf, fi); |
Yan Zheng | 80ff385 | 2008-10-30 14:20:02 -0400 | [diff] [blame] | 1230 | extent_end = found_key.offset + |
| 1231 | btrfs_file_extent_num_bytes(leaf, fi); |
Josef Bacik | b493968 | 2012-12-03 10:31:19 -0500 | [diff] [blame] | 1232 | disk_num_bytes = |
| 1233 | btrfs_file_extent_disk_num_bytes(leaf, fi); |
Yan Zheng | 80ff385 | 2008-10-30 14:20:02 -0400 | [diff] [blame] | 1234 | if (extent_end <= start) { |
| 1235 | path->slots[0]++; |
| 1236 | goto next_slot; |
| 1237 | } |
Yan Zheng | 17d217f | 2008-12-12 10:03:38 -0500 | [diff] [blame] | 1238 | if (disk_bytenr == 0) |
| 1239 | goto out_check; |
Yan Zheng | 80ff385 | 2008-10-30 14:20:02 -0400 | [diff] [blame] | 1240 | if (btrfs_file_extent_compression(leaf, fi) || |
| 1241 | btrfs_file_extent_encryption(leaf, fi) || |
| 1242 | btrfs_file_extent_other_encoding(leaf, fi)) |
| 1243 | goto out_check; |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 1244 | if (extent_type == BTRFS_FILE_EXTENT_REG && !force) |
| 1245 | goto out_check; |
Yan Zheng | d2fb343 | 2008-12-11 16:30:39 -0500 | [diff] [blame] | 1246 | if (btrfs_extent_readonly(root, disk_bytenr)) |
Yan Zheng | 80ff385 | 2008-10-30 14:20:02 -0400 | [diff] [blame] | 1247 | goto out_check; |
Li Zefan | 33345d01 | 2011-04-20 10:31:50 +0800 | [diff] [blame] | 1248 | if (btrfs_cross_ref_exist(trans, root, ino, |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 1249 | found_key.offset - |
| 1250 | extent_offset, disk_bytenr)) |
Yan Zheng | 17d217f | 2008-12-12 10:03:38 -0500 | [diff] [blame] | 1251 | goto out_check; |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 1252 | disk_bytenr += extent_offset; |
Yan Zheng | 17d217f | 2008-12-12 10:03:38 -0500 | [diff] [blame] | 1253 | disk_bytenr += cur_offset - found_key.offset; |
| 1254 | num_bytes = min(end + 1, extent_end) - cur_offset; |
| 1255 | /* |
| 1256 | * force cow if csum exists in the range. |
| 1257 | * this ensure that csum for a given extent are |
| 1258 | * either valid or do not exist. |
| 1259 | */ |
| 1260 | if (csum_exist_in_range(root, disk_bytenr, num_bytes)) |
| 1261 | goto out_check; |
Yan Zheng | 80ff385 | 2008-10-30 14:20:02 -0400 | [diff] [blame] | 1262 | nocow = 1; |
| 1263 | } else if (extent_type == BTRFS_FILE_EXTENT_INLINE) { |
| 1264 | extent_end = found_key.offset + |
| 1265 | btrfs_file_extent_inline_len(leaf, fi); |
| 1266 | extent_end = ALIGN(extent_end, root->sectorsize); |
| 1267 | } else { |
| 1268 | BUG_ON(1); |
| 1269 | } |
| 1270 | out_check: |
| 1271 | if (extent_end <= start) { |
| 1272 | path->slots[0]++; |
| 1273 | goto next_slot; |
| 1274 | } |
| 1275 | if (!nocow) { |
| 1276 | if (cow_start == (u64)-1) |
| 1277 | cow_start = cur_offset; |
| 1278 | cur_offset = extent_end; |
| 1279 | if (cur_offset > end) |
| 1280 | break; |
| 1281 | path->slots[0]++; |
| 1282 | goto next_slot; |
Yan Zheng | 7ea394f | 2008-08-05 13:05:02 -0400 | [diff] [blame] | 1283 | } |
Chris Mason | be20aa9 | 2007-12-17 20:14:01 -0500 | [diff] [blame] | 1284 | |
David Sterba | b3b4aa7 | 2011-04-21 01:20:15 +0200 | [diff] [blame] | 1285 | btrfs_release_path(path); |
Yan Zheng | 80ff385 | 2008-10-30 14:20:02 -0400 | [diff] [blame] | 1286 | if (cow_start != (u64)-1) { |
Josef Bacik | 0036158 | 2013-08-14 14:02:47 -0400 | [diff] [blame] | 1287 | ret = cow_file_range(inode, locked_page, |
| 1288 | cow_start, found_key.offset - 1, |
| 1289 | page_started, nr_written, 1); |
Josef Bacik | d788a34 | 2013-10-25 16:55:08 -0400 | [diff] [blame] | 1290 | if (ret) |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 1291 | goto error; |
Yan Zheng | 80ff385 | 2008-10-30 14:20:02 -0400 | [diff] [blame] | 1292 | cow_start = (u64)-1; |
Yan Zheng | 7ea394f | 2008-08-05 13:05:02 -0400 | [diff] [blame] | 1293 | } |
Yan Zheng | 80ff385 | 2008-10-30 14:20:02 -0400 | [diff] [blame] | 1294 | |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 1295 | if (extent_type == BTRFS_FILE_EXTENT_PREALLOC) { |
| 1296 | struct extent_map *em; |
| 1297 | struct extent_map_tree *em_tree; |
| 1298 | em_tree = &BTRFS_I(inode)->extent_tree; |
David Sterba | 172ddd6 | 2011-04-21 00:48:27 +0200 | [diff] [blame] | 1299 | em = alloc_extent_map(); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 1300 | BUG_ON(!em); /* -ENOMEM */ |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 1301 | em->start = cur_offset; |
Josef Bacik | 70c8a91 | 2012-10-11 16:54:30 -0400 | [diff] [blame] | 1302 | em->orig_start = found_key.offset - extent_offset; |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 1303 | em->len = num_bytes; |
| 1304 | em->block_len = num_bytes; |
| 1305 | em->block_start = disk_bytenr; |
Josef Bacik | b493968 | 2012-12-03 10:31:19 -0500 | [diff] [blame] | 1306 | em->orig_block_len = disk_num_bytes; |
Josef Bacik | cc95bef | 2013-04-04 14:31:27 -0400 | [diff] [blame] | 1307 | em->ram_bytes = ram_bytes; |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 1308 | em->bdev = root->fs_info->fs_devices->latest_bdev; |
Josef Bacik | 2ab28f3 | 2012-10-12 15:27:49 -0400 | [diff] [blame] | 1309 | em->mod_start = em->start; |
| 1310 | em->mod_len = em->len; |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 1311 | set_bit(EXTENT_FLAG_PINNED, &em->flags); |
Josef Bacik | b11e234 | 2012-12-03 10:58:15 -0500 | [diff] [blame] | 1312 | set_bit(EXTENT_FLAG_FILLING, &em->flags); |
Josef Bacik | 70c8a91 | 2012-10-11 16:54:30 -0400 | [diff] [blame] | 1313 | em->generation = -1; |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 1314 | while (1) { |
Chris Mason | 890871b | 2009-09-02 16:24:52 -0400 | [diff] [blame] | 1315 | write_lock(&em_tree->lock); |
Josef Bacik | 09a2a8f9 | 2013-04-05 16:51:15 -0400 | [diff] [blame] | 1316 | ret = add_extent_mapping(em_tree, em, 1); |
Chris Mason | 890871b | 2009-09-02 16:24:52 -0400 | [diff] [blame] | 1317 | write_unlock(&em_tree->lock); |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 1318 | if (ret != -EEXIST) { |
| 1319 | free_extent_map(em); |
| 1320 | break; |
| 1321 | } |
| 1322 | btrfs_drop_extent_cache(inode, em->start, |
| 1323 | em->start + em->len - 1, 0); |
| 1324 | } |
| 1325 | type = BTRFS_ORDERED_PREALLOC; |
| 1326 | } else { |
| 1327 | type = BTRFS_ORDERED_NOCOW; |
| 1328 | } |
Yan Zheng | 80ff385 | 2008-10-30 14:20:02 -0400 | [diff] [blame] | 1329 | |
| 1330 | ret = btrfs_add_ordered_extent(inode, cur_offset, disk_bytenr, |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 1331 | num_bytes, num_bytes, type); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 1332 | BUG_ON(ret); /* -ENOMEM */ |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 1333 | |
Yan, Zheng | efa5646 | 2010-05-16 10:49:59 -0400 | [diff] [blame] | 1334 | if (root->root_key.objectid == |
| 1335 | BTRFS_DATA_RELOC_TREE_OBJECTID) { |
| 1336 | ret = btrfs_reloc_clone_csums(inode, cur_offset, |
| 1337 | num_bytes); |
Josef Bacik | d788a34 | 2013-10-25 16:55:08 -0400 | [diff] [blame] | 1338 | if (ret) |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 1339 | goto error; |
Yan, Zheng | efa5646 | 2010-05-16 10:49:59 -0400 | [diff] [blame] | 1340 | } |
| 1341 | |
Josef Bacik | c2790a2 | 2013-07-29 11:20:47 -0400 | [diff] [blame] | 1342 | extent_clear_unlock_delalloc(inode, cur_offset, |
| 1343 | cur_offset + num_bytes - 1, |
| 1344 | locked_page, EXTENT_LOCKED | |
| 1345 | EXTENT_DELALLOC, PAGE_UNLOCK | |
| 1346 | PAGE_SET_PRIVATE2); |
Yan Zheng | 80ff385 | 2008-10-30 14:20:02 -0400 | [diff] [blame] | 1347 | cur_offset = extent_end; |
| 1348 | if (cur_offset > end) |
| 1349 | break; |
Yan Zheng | 7ea394f | 2008-08-05 13:05:02 -0400 | [diff] [blame] | 1350 | } |
David Sterba | b3b4aa7 | 2011-04-21 01:20:15 +0200 | [diff] [blame] | 1351 | btrfs_release_path(path); |
Yan Zheng | 80ff385 | 2008-10-30 14:20:02 -0400 | [diff] [blame] | 1352 | |
Josef Bacik | 17ca04a | 2012-05-31 15:58:55 -0400 | [diff] [blame] | 1353 | if (cur_offset <= end && cow_start == (u64)-1) { |
Yan Zheng | 80ff385 | 2008-10-30 14:20:02 -0400 | [diff] [blame] | 1354 | cow_start = cur_offset; |
Josef Bacik | 17ca04a | 2012-05-31 15:58:55 -0400 | [diff] [blame] | 1355 | cur_offset = end; |
| 1356 | } |
| 1357 | |
Yan Zheng | 80ff385 | 2008-10-30 14:20:02 -0400 | [diff] [blame] | 1358 | if (cow_start != (u64)-1) { |
Josef Bacik | 0036158 | 2013-08-14 14:02:47 -0400 | [diff] [blame] | 1359 | ret = cow_file_range(inode, locked_page, cow_start, end, |
| 1360 | page_started, nr_written, 1); |
Josef Bacik | d788a34 | 2013-10-25 16:55:08 -0400 | [diff] [blame] | 1361 | if (ret) |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 1362 | goto error; |
Yan Zheng | 80ff385 | 2008-10-30 14:20:02 -0400 | [diff] [blame] | 1363 | } |
| 1364 | |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 1365 | error: |
Miao Xie | a698d075 | 2012-09-20 01:51:59 -0600 | [diff] [blame] | 1366 | err = btrfs_end_transaction(trans, root); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 1367 | if (!ret) |
| 1368 | ret = err; |
| 1369 | |
Josef Bacik | 17ca04a | 2012-05-31 15:58:55 -0400 | [diff] [blame] | 1370 | if (ret && cur_offset < end) |
Josef Bacik | c2790a2 | 2013-07-29 11:20:47 -0400 | [diff] [blame] | 1371 | extent_clear_unlock_delalloc(inode, cur_offset, end, |
| 1372 | locked_page, EXTENT_LOCKED | |
Josef Bacik | 151a41b | 2013-07-29 13:22:24 -0400 | [diff] [blame] | 1373 | EXTENT_DELALLOC | EXTENT_DEFRAG | |
| 1374 | EXTENT_DO_ACCOUNTING, PAGE_UNLOCK | |
| 1375 | PAGE_CLEAR_DIRTY | |
Josef Bacik | c2790a2 | 2013-07-29 11:20:47 -0400 | [diff] [blame] | 1376 | PAGE_SET_WRITEBACK | |
| 1377 | PAGE_END_WRITEBACK); |
Yan Zheng | 7ea394f | 2008-08-05 13:05:02 -0400 | [diff] [blame] | 1378 | btrfs_free_path(path); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 1379 | return ret; |
Chris Mason | be20aa9 | 2007-12-17 20:14:01 -0500 | [diff] [blame] | 1380 | } |
| 1381 | |
Chris Mason | d352ac6 | 2008-09-29 15:18:18 -0400 | [diff] [blame] | 1382 | /* |
| 1383 | * extent_io.c call back to do delayed allocation processing |
| 1384 | */ |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 1385 | static int run_delalloc_range(struct inode *inode, struct page *locked_page, |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 1386 | u64 start, u64 end, int *page_started, |
| 1387 | unsigned long *nr_written) |
Chris Mason | be20aa9 | 2007-12-17 20:14:01 -0500 | [diff] [blame] | 1388 | { |
Chris Mason | be20aa9 | 2007-12-17 20:14:01 -0500 | [diff] [blame] | 1389 | int ret; |
Chris Mason | 7f366cf | 2009-03-12 20:12:45 -0400 | [diff] [blame] | 1390 | struct btrfs_root *root = BTRFS_I(inode)->root; |
Chris Mason | a213501 | 2008-06-25 16:01:30 -0400 | [diff] [blame] | 1391 | |
Josef Bacik | 7ddf5a4 | 2012-06-08 15:26:47 -0400 | [diff] [blame] | 1392 | if (BTRFS_I(inode)->flags & BTRFS_INODE_NODATACOW) { |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 1393 | ret = run_delalloc_nocow(inode, locked_page, start, end, |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 1394 | page_started, 1, nr_written); |
Josef Bacik | 7ddf5a4 | 2012-06-08 15:26:47 -0400 | [diff] [blame] | 1395 | } else if (BTRFS_I(inode)->flags & BTRFS_INODE_PREALLOC) { |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 1396 | ret = run_delalloc_nocow(inode, locked_page, start, end, |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 1397 | page_started, 0, nr_written); |
Josef Bacik | 7ddf5a4 | 2012-06-08 15:26:47 -0400 | [diff] [blame] | 1398 | } else if (!btrfs_test_opt(root, COMPRESS) && |
| 1399 | !(BTRFS_I(inode)->force_compress) && |
| 1400 | !(BTRFS_I(inode)->flags & BTRFS_INODE_COMPRESS)) { |
Chris Mason | 7f366cf | 2009-03-12 20:12:45 -0400 | [diff] [blame] | 1401 | ret = cow_file_range(inode, locked_page, start, end, |
| 1402 | page_started, nr_written, 1); |
Josef Bacik | 7ddf5a4 | 2012-06-08 15:26:47 -0400 | [diff] [blame] | 1403 | } else { |
| 1404 | set_bit(BTRFS_INODE_HAS_ASYNC_EXTENT, |
| 1405 | &BTRFS_I(inode)->runtime_flags); |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 1406 | ret = cow_file_range_async(inode, locked_page, start, end, |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 1407 | page_started, nr_written); |
Josef Bacik | 7ddf5a4 | 2012-06-08 15:26:47 -0400 | [diff] [blame] | 1408 | } |
Chris Mason | b888db2 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 1409 | return ret; |
| 1410 | } |
| 1411 | |
Jeff Mahoney | 1bf8504 | 2011-07-21 16:56:09 +0000 | [diff] [blame] | 1412 | static void btrfs_split_extent_hook(struct inode *inode, |
| 1413 | struct extent_state *orig, u64 split) |
Josef Bacik | 9ed74f2 | 2009-09-11 16:12:44 -0400 | [diff] [blame] | 1414 | { |
Yan, Zheng | 0ca1f7c | 2010-05-16 10:48:47 -0400 | [diff] [blame] | 1415 | /* not delalloc, ignore it */ |
Josef Bacik | 9ed74f2 | 2009-09-11 16:12:44 -0400 | [diff] [blame] | 1416 | if (!(orig->state & EXTENT_DELALLOC)) |
Jeff Mahoney | 1bf8504 | 2011-07-21 16:56:09 +0000 | [diff] [blame] | 1417 | return; |
Josef Bacik | 9ed74f2 | 2009-09-11 16:12:44 -0400 | [diff] [blame] | 1418 | |
Josef Bacik | 9e0baf6 | 2011-07-15 15:16:44 +0000 | [diff] [blame] | 1419 | spin_lock(&BTRFS_I(inode)->lock); |
| 1420 | BTRFS_I(inode)->outstanding_extents++; |
| 1421 | spin_unlock(&BTRFS_I(inode)->lock); |
Josef Bacik | 9ed74f2 | 2009-09-11 16:12:44 -0400 | [diff] [blame] | 1422 | } |
| 1423 | |
| 1424 | /* |
| 1425 | * extent_io.c merge_extent_hook, used to track merged delayed allocation |
| 1426 | * extents so we can keep track of new extents that are just merged onto old |
| 1427 | * extents, such as when we are doing sequential writes, so we can properly |
| 1428 | * account for the metadata space we'll need. |
| 1429 | */ |
Jeff Mahoney | 1bf8504 | 2011-07-21 16:56:09 +0000 | [diff] [blame] | 1430 | static void btrfs_merge_extent_hook(struct inode *inode, |
| 1431 | struct extent_state *new, |
| 1432 | struct extent_state *other) |
Josef Bacik | 9ed74f2 | 2009-09-11 16:12:44 -0400 | [diff] [blame] | 1433 | { |
Josef Bacik | 9ed74f2 | 2009-09-11 16:12:44 -0400 | [diff] [blame] | 1434 | /* not delalloc, ignore it */ |
| 1435 | if (!(other->state & EXTENT_DELALLOC)) |
Jeff Mahoney | 1bf8504 | 2011-07-21 16:56:09 +0000 | [diff] [blame] | 1436 | return; |
Josef Bacik | 9ed74f2 | 2009-09-11 16:12:44 -0400 | [diff] [blame] | 1437 | |
Josef Bacik | 9e0baf6 | 2011-07-15 15:16:44 +0000 | [diff] [blame] | 1438 | spin_lock(&BTRFS_I(inode)->lock); |
| 1439 | BTRFS_I(inode)->outstanding_extents--; |
| 1440 | spin_unlock(&BTRFS_I(inode)->lock); |
Josef Bacik | 9ed74f2 | 2009-09-11 16:12:44 -0400 | [diff] [blame] | 1441 | } |
| 1442 | |
Miao Xie | eb73c1b | 2013-05-15 07:48:22 +0000 | [diff] [blame] | 1443 | static void btrfs_add_delalloc_inodes(struct btrfs_root *root, |
| 1444 | struct inode *inode) |
| 1445 | { |
| 1446 | spin_lock(&root->delalloc_lock); |
| 1447 | if (list_empty(&BTRFS_I(inode)->delalloc_inodes)) { |
| 1448 | list_add_tail(&BTRFS_I(inode)->delalloc_inodes, |
| 1449 | &root->delalloc_inodes); |
| 1450 | set_bit(BTRFS_INODE_IN_DELALLOC_LIST, |
| 1451 | &BTRFS_I(inode)->runtime_flags); |
| 1452 | root->nr_delalloc_inodes++; |
| 1453 | if (root->nr_delalloc_inodes == 1) { |
| 1454 | spin_lock(&root->fs_info->delalloc_root_lock); |
| 1455 | BUG_ON(!list_empty(&root->delalloc_root)); |
| 1456 | list_add_tail(&root->delalloc_root, |
| 1457 | &root->fs_info->delalloc_roots); |
| 1458 | spin_unlock(&root->fs_info->delalloc_root_lock); |
| 1459 | } |
| 1460 | } |
| 1461 | spin_unlock(&root->delalloc_lock); |
| 1462 | } |
| 1463 | |
| 1464 | static void btrfs_del_delalloc_inode(struct btrfs_root *root, |
| 1465 | struct inode *inode) |
| 1466 | { |
| 1467 | spin_lock(&root->delalloc_lock); |
| 1468 | if (!list_empty(&BTRFS_I(inode)->delalloc_inodes)) { |
| 1469 | list_del_init(&BTRFS_I(inode)->delalloc_inodes); |
| 1470 | clear_bit(BTRFS_INODE_IN_DELALLOC_LIST, |
| 1471 | &BTRFS_I(inode)->runtime_flags); |
| 1472 | root->nr_delalloc_inodes--; |
| 1473 | if (!root->nr_delalloc_inodes) { |
| 1474 | spin_lock(&root->fs_info->delalloc_root_lock); |
| 1475 | BUG_ON(list_empty(&root->delalloc_root)); |
| 1476 | list_del_init(&root->delalloc_root); |
| 1477 | spin_unlock(&root->fs_info->delalloc_root_lock); |
| 1478 | } |
| 1479 | } |
| 1480 | spin_unlock(&root->delalloc_lock); |
| 1481 | } |
| 1482 | |
Chris Mason | d352ac6 | 2008-09-29 15:18:18 -0400 | [diff] [blame] | 1483 | /* |
| 1484 | * extent_io.c set_bit_hook, used to track delayed allocation |
| 1485 | * bytes in this file, and to maintain the list of inodes that |
| 1486 | * have pending delalloc work to be done. |
| 1487 | */ |
Jeff Mahoney | 1bf8504 | 2011-07-21 16:56:09 +0000 | [diff] [blame] | 1488 | static void btrfs_set_bit_hook(struct inode *inode, |
David Sterba | 4107488 | 2013-04-29 13:38:46 +0000 | [diff] [blame] | 1489 | struct extent_state *state, unsigned long *bits) |
Chris Mason | 291d673 | 2008-01-29 15:55:23 -0500 | [diff] [blame] | 1490 | { |
Josef Bacik | 9ed74f2 | 2009-09-11 16:12:44 -0400 | [diff] [blame] | 1491 | |
Chris Mason | 75eff68 | 2008-12-15 15:54:40 -0500 | [diff] [blame] | 1492 | /* |
| 1493 | * set_bit and clear bit hooks normally require _irqsave/restore |
Sergei Trofimovich | 27160b6 | 2011-05-20 20:20:32 +0000 | [diff] [blame] | 1494 | * but in this case, we are only testing for the DELALLOC |
Chris Mason | 75eff68 | 2008-12-15 15:54:40 -0500 | [diff] [blame] | 1495 | * bit, which is only set or cleared with irqs on |
| 1496 | */ |
Yan, Zheng | 0ca1f7c | 2010-05-16 10:48:47 -0400 | [diff] [blame] | 1497 | if (!(state->state & EXTENT_DELALLOC) && (*bits & EXTENT_DELALLOC)) { |
Chris Mason | 291d673 | 2008-01-29 15:55:23 -0500 | [diff] [blame] | 1498 | struct btrfs_root *root = BTRFS_I(inode)->root; |
Yan, Zheng | 0ca1f7c | 2010-05-16 10:48:47 -0400 | [diff] [blame] | 1499 | u64 len = state->end + 1 - state->start; |
Liu Bo | 83eea1f | 2012-07-10 05:28:39 -0600 | [diff] [blame] | 1500 | bool do_list = !btrfs_is_free_space_inode(inode); |
Josef Bacik | 9ed74f2 | 2009-09-11 16:12:44 -0400 | [diff] [blame] | 1501 | |
Josef Bacik | 9e0baf6 | 2011-07-15 15:16:44 +0000 | [diff] [blame] | 1502 | if (*bits & EXTENT_FIRST_DELALLOC) { |
Yan, Zheng | 0ca1f7c | 2010-05-16 10:48:47 -0400 | [diff] [blame] | 1503 | *bits &= ~EXTENT_FIRST_DELALLOC; |
Josef Bacik | 9e0baf6 | 2011-07-15 15:16:44 +0000 | [diff] [blame] | 1504 | } else { |
| 1505 | spin_lock(&BTRFS_I(inode)->lock); |
| 1506 | BTRFS_I(inode)->outstanding_extents++; |
| 1507 | spin_unlock(&BTRFS_I(inode)->lock); |
| 1508 | } |
Josef Bacik | 287a0ab | 2010-03-19 18:07:23 +0000 | [diff] [blame] | 1509 | |
Miao Xie | 963d678 | 2013-01-29 10:10:51 +0000 | [diff] [blame] | 1510 | __percpu_counter_add(&root->fs_info->delalloc_bytes, len, |
| 1511 | root->fs_info->delalloc_batch); |
Miao Xie | df0af1a | 2013-01-29 10:11:59 +0000 | [diff] [blame] | 1512 | spin_lock(&BTRFS_I(inode)->lock); |
Yan, Zheng | 0ca1f7c | 2010-05-16 10:48:47 -0400 | [diff] [blame] | 1513 | BTRFS_I(inode)->delalloc_bytes += len; |
Miao Xie | df0af1a | 2013-01-29 10:11:59 +0000 | [diff] [blame] | 1514 | if (do_list && !test_bit(BTRFS_INODE_IN_DELALLOC_LIST, |
Miao Xie | eb73c1b | 2013-05-15 07:48:22 +0000 | [diff] [blame] | 1515 | &BTRFS_I(inode)->runtime_flags)) |
| 1516 | btrfs_add_delalloc_inodes(root, inode); |
Miao Xie | df0af1a | 2013-01-29 10:11:59 +0000 | [diff] [blame] | 1517 | spin_unlock(&BTRFS_I(inode)->lock); |
Chris Mason | 291d673 | 2008-01-29 15:55:23 -0500 | [diff] [blame] | 1518 | } |
Chris Mason | 291d673 | 2008-01-29 15:55:23 -0500 | [diff] [blame] | 1519 | } |
| 1520 | |
Chris Mason | d352ac6 | 2008-09-29 15:18:18 -0400 | [diff] [blame] | 1521 | /* |
| 1522 | * extent_io.c clear_bit_hook, see set_bit_hook for why |
| 1523 | */ |
Jeff Mahoney | 1bf8504 | 2011-07-21 16:56:09 +0000 | [diff] [blame] | 1524 | static void btrfs_clear_bit_hook(struct inode *inode, |
David Sterba | 4107488 | 2013-04-29 13:38:46 +0000 | [diff] [blame] | 1525 | struct extent_state *state, |
| 1526 | unsigned long *bits) |
Chris Mason | 291d673 | 2008-01-29 15:55:23 -0500 | [diff] [blame] | 1527 | { |
Chris Mason | 75eff68 | 2008-12-15 15:54:40 -0500 | [diff] [blame] | 1528 | /* |
| 1529 | * set_bit and clear bit hooks normally require _irqsave/restore |
Sergei Trofimovich | 27160b6 | 2011-05-20 20:20:32 +0000 | [diff] [blame] | 1530 | * but in this case, we are only testing for the DELALLOC |
Chris Mason | 75eff68 | 2008-12-15 15:54:40 -0500 | [diff] [blame] | 1531 | * bit, which is only set or cleared with irqs on |
| 1532 | */ |
Yan, Zheng | 0ca1f7c | 2010-05-16 10:48:47 -0400 | [diff] [blame] | 1533 | if ((state->state & EXTENT_DELALLOC) && (*bits & EXTENT_DELALLOC)) { |
Chris Mason | 291d673 | 2008-01-29 15:55:23 -0500 | [diff] [blame] | 1534 | struct btrfs_root *root = BTRFS_I(inode)->root; |
Yan, Zheng | 0ca1f7c | 2010-05-16 10:48:47 -0400 | [diff] [blame] | 1535 | u64 len = state->end + 1 - state->start; |
Liu Bo | 83eea1f | 2012-07-10 05:28:39 -0600 | [diff] [blame] | 1536 | bool do_list = !btrfs_is_free_space_inode(inode); |
Chris Mason | bcbfce8 | 2008-04-22 13:26:47 -0400 | [diff] [blame] | 1537 | |
Josef Bacik | 9e0baf6 | 2011-07-15 15:16:44 +0000 | [diff] [blame] | 1538 | if (*bits & EXTENT_FIRST_DELALLOC) { |
Yan, Zheng | 0ca1f7c | 2010-05-16 10:48:47 -0400 | [diff] [blame] | 1539 | *bits &= ~EXTENT_FIRST_DELALLOC; |
Josef Bacik | 9e0baf6 | 2011-07-15 15:16:44 +0000 | [diff] [blame] | 1540 | } else if (!(*bits & EXTENT_DO_ACCOUNTING)) { |
| 1541 | spin_lock(&BTRFS_I(inode)->lock); |
| 1542 | BTRFS_I(inode)->outstanding_extents--; |
| 1543 | spin_unlock(&BTRFS_I(inode)->lock); |
| 1544 | } |
Yan, Zheng | 0ca1f7c | 2010-05-16 10:48:47 -0400 | [diff] [blame] | 1545 | |
Josef Bacik | b6d08f0 | 2013-09-27 14:57:43 -0400 | [diff] [blame] | 1546 | /* |
| 1547 | * We don't reserve metadata space for space cache inodes so we |
| 1548 | * don't need to call dellalloc_release_metadata if there is an |
| 1549 | * error. |
| 1550 | */ |
| 1551 | if (*bits & EXTENT_DO_ACCOUNTING && |
| 1552 | root != root->fs_info->tree_root) |
Yan, Zheng | 0ca1f7c | 2010-05-16 10:48:47 -0400 | [diff] [blame] | 1553 | btrfs_delalloc_release_metadata(inode, len); |
| 1554 | |
Josef Bacik | 0cb59c9 | 2010-07-02 12:14:14 -0400 | [diff] [blame] | 1555 | if (root->root_key.objectid != BTRFS_DATA_RELOC_TREE_OBJECTID |
Josef Bacik | 7ee9e44 | 2013-06-21 16:37:03 -0400 | [diff] [blame] | 1556 | && do_list && !(state->state & EXTENT_NORESERVE)) |
Yan, Zheng | 0ca1f7c | 2010-05-16 10:48:47 -0400 | [diff] [blame] | 1557 | btrfs_free_reserved_data_space(inode, len); |
Josef Bacik | 9ed74f2 | 2009-09-11 16:12:44 -0400 | [diff] [blame] | 1558 | |
Miao Xie | 963d678 | 2013-01-29 10:10:51 +0000 | [diff] [blame] | 1559 | __percpu_counter_add(&root->fs_info->delalloc_bytes, -len, |
| 1560 | root->fs_info->delalloc_batch); |
Miao Xie | df0af1a | 2013-01-29 10:11:59 +0000 | [diff] [blame] | 1561 | spin_lock(&BTRFS_I(inode)->lock); |
Yan, Zheng | 0ca1f7c | 2010-05-16 10:48:47 -0400 | [diff] [blame] | 1562 | BTRFS_I(inode)->delalloc_bytes -= len; |
Josef Bacik | 0cb59c9 | 2010-07-02 12:14:14 -0400 | [diff] [blame] | 1563 | if (do_list && BTRFS_I(inode)->delalloc_bytes == 0 && |
Miao Xie | df0af1a | 2013-01-29 10:11:59 +0000 | [diff] [blame] | 1564 | test_bit(BTRFS_INODE_IN_DELALLOC_LIST, |
Miao Xie | eb73c1b | 2013-05-15 07:48:22 +0000 | [diff] [blame] | 1565 | &BTRFS_I(inode)->runtime_flags)) |
| 1566 | btrfs_del_delalloc_inode(root, inode); |
Miao Xie | df0af1a | 2013-01-29 10:11:59 +0000 | [diff] [blame] | 1567 | spin_unlock(&BTRFS_I(inode)->lock); |
Chris Mason | 291d673 | 2008-01-29 15:55:23 -0500 | [diff] [blame] | 1568 | } |
Chris Mason | 291d673 | 2008-01-29 15:55:23 -0500 | [diff] [blame] | 1569 | } |
| 1570 | |
Chris Mason | d352ac6 | 2008-09-29 15:18:18 -0400 | [diff] [blame] | 1571 | /* |
| 1572 | * extent_io.c merge_bio_hook, this must check the chunk tree to make sure |
| 1573 | * we don't create bios that span stripes or chunks |
| 1574 | */ |
David Woodhouse | 64a1670 | 2009-07-15 23:29:37 +0100 | [diff] [blame] | 1575 | 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] | 1576 | size_t size, struct bio *bio, |
| 1577 | unsigned long bio_flags) |
Chris Mason | 239b14b | 2008-03-24 15:02:07 -0400 | [diff] [blame] | 1578 | { |
| 1579 | struct btrfs_root *root = BTRFS_I(page->mapping->host)->root; |
Chris Mason | a62b940 | 2008-10-03 16:31:08 -0400 | [diff] [blame] | 1580 | u64 logical = (u64)bio->bi_sector << 9; |
Chris Mason | 239b14b | 2008-03-24 15:02:07 -0400 | [diff] [blame] | 1581 | u64 length = 0; |
| 1582 | u64 map_length; |
Chris Mason | 239b14b | 2008-03-24 15:02:07 -0400 | [diff] [blame] | 1583 | int ret; |
| 1584 | |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 1585 | if (bio_flags & EXTENT_BIO_COMPRESSED) |
| 1586 | return 0; |
| 1587 | |
Chris Mason | f2d8d74 | 2008-04-21 10:03:05 -0400 | [diff] [blame] | 1588 | length = bio->bi_size; |
Chris Mason | 239b14b | 2008-03-24 15:02:07 -0400 | [diff] [blame] | 1589 | map_length = length; |
David Woodhouse | 64a1670 | 2009-07-15 23:29:37 +0100 | [diff] [blame] | 1590 | ret = btrfs_map_block(root->fs_info, rw, logical, |
Chris Mason | f188591 | 2008-04-09 16:28:12 -0400 | [diff] [blame] | 1591 | &map_length, NULL, 0); |
Stefan Behrens | 3ec706c | 2012-11-05 15:46:42 +0100 | [diff] [blame] | 1592 | /* Will always return 0 with map_multi == NULL */ |
Jeff Mahoney | 3444a97 | 2011-10-03 23:23:13 -0400 | [diff] [blame] | 1593 | BUG_ON(ret < 0); |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 1594 | if (map_length < length + size) |
Chris Mason | 239b14b | 2008-03-24 15:02:07 -0400 | [diff] [blame] | 1595 | return 1; |
Jeff Mahoney | 3444a97 | 2011-10-03 23:23:13 -0400 | [diff] [blame] | 1596 | return 0; |
Chris Mason | 239b14b | 2008-03-24 15:02:07 -0400 | [diff] [blame] | 1597 | } |
| 1598 | |
Chris Mason | d352ac6 | 2008-09-29 15:18:18 -0400 | [diff] [blame] | 1599 | /* |
| 1600 | * in order to insert checksums into the metadata in large chunks, |
| 1601 | * we wait until bio submission time. All the pages in the bio are |
| 1602 | * checksummed and sums are attached onto the ordered extent record. |
| 1603 | * |
| 1604 | * At IO completion time the cums attached on the ordered extent record |
| 1605 | * are inserted into the btree |
| 1606 | */ |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 1607 | static int __btrfs_submit_bio_start(struct inode *inode, int rw, |
| 1608 | struct bio *bio, int mirror_num, |
Chris Mason | eaf25d9 | 2010-05-25 09:48:28 -0400 | [diff] [blame] | 1609 | unsigned long bio_flags, |
| 1610 | u64 bio_offset) |
Chris Mason | 065631f | 2008-02-20 12:07:25 -0500 | [diff] [blame] | 1611 | { |
Chris Mason | 065631f | 2008-02-20 12:07:25 -0500 | [diff] [blame] | 1612 | struct btrfs_root *root = BTRFS_I(inode)->root; |
Chris Mason | 065631f | 2008-02-20 12:07:25 -0500 | [diff] [blame] | 1613 | int ret = 0; |
Chris Mason | e015640 | 2008-04-16 11:15:20 -0400 | [diff] [blame] | 1614 | |
Chris Mason | d20f704 | 2008-12-08 16:58:54 -0500 | [diff] [blame] | 1615 | ret = btrfs_csum_one_bio(root, inode, bio, 0, 0); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 1616 | BUG_ON(ret); /* -ENOMEM */ |
Chris Mason | 4a69a41 | 2008-11-06 22:03:00 -0500 | [diff] [blame] | 1617 | return 0; |
| 1618 | } |
Chris Mason | e015640 | 2008-04-16 11:15:20 -0400 | [diff] [blame] | 1619 | |
Chris Mason | 4a69a41 | 2008-11-06 22:03:00 -0500 | [diff] [blame] | 1620 | /* |
| 1621 | * in order to insert checksums into the metadata in large chunks, |
| 1622 | * we wait until bio submission time. All the pages in the bio are |
| 1623 | * checksummed and sums are attached onto the ordered extent record. |
| 1624 | * |
| 1625 | * At IO completion time the cums attached on the ordered extent record |
| 1626 | * are inserted into the btree |
| 1627 | */ |
Christoph Hellwig | b295086 | 2008-12-02 09:54:17 -0500 | [diff] [blame] | 1628 | 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] | 1629 | int mirror_num, unsigned long bio_flags, |
| 1630 | u64 bio_offset) |
Chris Mason | 4a69a41 | 2008-11-06 22:03:00 -0500 | [diff] [blame] | 1631 | { |
| 1632 | struct btrfs_root *root = BTRFS_I(inode)->root; |
Stefan Behrens | 6189192 | 2012-11-05 18:51:52 +0100 | [diff] [blame] | 1633 | int ret; |
| 1634 | |
| 1635 | ret = btrfs_map_bio(root, rw, bio, mirror_num, 1); |
| 1636 | if (ret) |
| 1637 | bio_endio(bio, ret); |
| 1638 | return ret; |
Chris Mason | 44b8bd7 | 2008-04-16 11:14:51 -0400 | [diff] [blame] | 1639 | } |
| 1640 | |
Chris Mason | d352ac6 | 2008-09-29 15:18:18 -0400 | [diff] [blame] | 1641 | /* |
Chris Mason | cad321a | 2008-12-17 14:51:42 -0500 | [diff] [blame] | 1642 | * extent_io.c submission hook. This does the right thing for csum calculation |
| 1643 | * on write, or reading the csums from the tree before a read |
Chris Mason | d352ac6 | 2008-09-29 15:18:18 -0400 | [diff] [blame] | 1644 | */ |
Christoph Hellwig | b295086 | 2008-12-02 09:54:17 -0500 | [diff] [blame] | 1645 | 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] | 1646 | int mirror_num, unsigned long bio_flags, |
| 1647 | u64 bio_offset) |
Chris Mason | 44b8bd7 | 2008-04-16 11:14:51 -0400 | [diff] [blame] | 1648 | { |
| 1649 | struct btrfs_root *root = BTRFS_I(inode)->root; |
| 1650 | int ret = 0; |
Chris Mason | 19b9bdb | 2008-10-30 14:23:13 -0400 | [diff] [blame] | 1651 | int skip_sum; |
Jeff Mahoney | 0417341 | 2011-10-03 23:23:12 -0400 | [diff] [blame] | 1652 | int metadata = 0; |
Josef Bacik | b812ce2 | 2012-11-16 13:56:32 -0500 | [diff] [blame] | 1653 | int async = !atomic_read(&BTRFS_I(inode)->sync_writers); |
Chris Mason | 44b8bd7 | 2008-04-16 11:14:51 -0400 | [diff] [blame] | 1654 | |
Christoph Hellwig | 6cbff00 | 2009-04-17 10:37:41 +0200 | [diff] [blame] | 1655 | skip_sum = BTRFS_I(inode)->flags & BTRFS_INODE_NODATASUM; |
Chris Mason | cad321a | 2008-12-17 14:51:42 -0500 | [diff] [blame] | 1656 | |
Liu Bo | 83eea1f | 2012-07-10 05:28:39 -0600 | [diff] [blame] | 1657 | if (btrfs_is_free_space_inode(inode)) |
Jeff Mahoney | 0417341 | 2011-10-03 23:23:12 -0400 | [diff] [blame] | 1658 | metadata = 2; |
| 1659 | |
Christoph Hellwig | 7b6d91d | 2010-08-07 18:20:39 +0200 | [diff] [blame] | 1660 | if (!(rw & REQ_WRITE)) { |
Josef Bacik | 5fd0204 | 2012-05-02 14:00:54 -0400 | [diff] [blame] | 1661 | ret = btrfs_bio_wq_end_io(root->fs_info, bio, metadata); |
| 1662 | if (ret) |
Stefan Behrens | 6189192 | 2012-11-05 18:51:52 +0100 | [diff] [blame] | 1663 | goto out; |
Josef Bacik | 5fd0204 | 2012-05-02 14:00:54 -0400 | [diff] [blame] | 1664 | |
Chris Mason | d20f704 | 2008-12-08 16:58:54 -0500 | [diff] [blame] | 1665 | if (bio_flags & EXTENT_BIO_COMPRESSED) { |
Stefan Behrens | 6189192 | 2012-11-05 18:51:52 +0100 | [diff] [blame] | 1666 | ret = btrfs_submit_compressed_read(inode, bio, |
| 1667 | mirror_num, |
| 1668 | bio_flags); |
| 1669 | goto out; |
Tsutomu Itoh | c2db107 | 2011-03-01 06:48:31 +0000 | [diff] [blame] | 1670 | } else if (!skip_sum) { |
| 1671 | ret = btrfs_lookup_bio_sums(root, inode, bio, NULL); |
| 1672 | if (ret) |
Stefan Behrens | 6189192 | 2012-11-05 18:51:52 +0100 | [diff] [blame] | 1673 | goto out; |
Tsutomu Itoh | c2db107 | 2011-03-01 06:48:31 +0000 | [diff] [blame] | 1674 | } |
Chris Mason | 4d1b5fb | 2008-08-20 09:44:52 -0400 | [diff] [blame] | 1675 | goto mapit; |
Josef Bacik | b812ce2 | 2012-11-16 13:56:32 -0500 | [diff] [blame] | 1676 | } else if (async && !skip_sum) { |
Yan Zheng | 17d217f | 2008-12-12 10:03:38 -0500 | [diff] [blame] | 1677 | /* csum items have already been cloned */ |
| 1678 | if (root->root_key.objectid == BTRFS_DATA_RELOC_TREE_OBJECTID) |
| 1679 | goto mapit; |
Chris Mason | 19b9bdb | 2008-10-30 14:23:13 -0400 | [diff] [blame] | 1680 | /* we're doing a write, do the async checksumming */ |
Stefan Behrens | 6189192 | 2012-11-05 18:51:52 +0100 | [diff] [blame] | 1681 | ret = btrfs_wq_submit_bio(BTRFS_I(inode)->root->fs_info, |
Chris Mason | 44b8bd7 | 2008-04-16 11:14:51 -0400 | [diff] [blame] | 1682 | inode, rw, bio, mirror_num, |
Chris Mason | eaf25d9 | 2010-05-25 09:48:28 -0400 | [diff] [blame] | 1683 | bio_flags, bio_offset, |
| 1684 | __btrfs_submit_bio_start, |
Chris Mason | 4a69a41 | 2008-11-06 22:03:00 -0500 | [diff] [blame] | 1685 | __btrfs_submit_bio_done); |
Stefan Behrens | 6189192 | 2012-11-05 18:51:52 +0100 | [diff] [blame] | 1686 | goto out; |
Josef Bacik | b812ce2 | 2012-11-16 13:56:32 -0500 | [diff] [blame] | 1687 | } else if (!skip_sum) { |
| 1688 | ret = btrfs_csum_one_bio(root, inode, bio, 0, 0); |
| 1689 | if (ret) |
| 1690 | goto out; |
Chris Mason | 19b9bdb | 2008-10-30 14:23:13 -0400 | [diff] [blame] | 1691 | } |
| 1692 | |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 1693 | mapit: |
Stefan Behrens | 6189192 | 2012-11-05 18:51:52 +0100 | [diff] [blame] | 1694 | ret = btrfs_map_bio(root, rw, bio, mirror_num, 0); |
| 1695 | |
| 1696 | out: |
| 1697 | if (ret < 0) |
| 1698 | bio_endio(bio, ret); |
| 1699 | return ret; |
Chris Mason | 065631f | 2008-02-20 12:07:25 -0500 | [diff] [blame] | 1700 | } |
Chris Mason | 6885f30 | 2008-02-20 16:11:05 -0500 | [diff] [blame] | 1701 | |
Chris Mason | d352ac6 | 2008-09-29 15:18:18 -0400 | [diff] [blame] | 1702 | /* |
| 1703 | * given a list of ordered sums record them in the inode. This happens |
| 1704 | * at IO completion time based on sums calculated at bio submission time. |
| 1705 | */ |
Chris Mason | ba1da2f | 2008-07-17 12:54:15 -0400 | [diff] [blame] | 1706 | static noinline int add_pending_csums(struct btrfs_trans_handle *trans, |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 1707 | struct inode *inode, u64 file_offset, |
| 1708 | struct list_head *list) |
| 1709 | { |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 1710 | struct btrfs_ordered_sum *sum; |
| 1711 | |
Qinghuang Feng | c6e3087 | 2009-01-21 10:59:08 -0500 | [diff] [blame] | 1712 | list_for_each_entry(sum, list, list) { |
Miao Xie | 39847c4 | 2013-03-28 08:08:20 +0000 | [diff] [blame] | 1713 | trans->adding_csums = 1; |
Chris Mason | d20f704 | 2008-12-08 16:58:54 -0500 | [diff] [blame] | 1714 | btrfs_csum_file_blocks(trans, |
| 1715 | BTRFS_I(inode)->root->fs_info->csum_root, sum); |
Miao Xie | 39847c4 | 2013-03-28 08:08:20 +0000 | [diff] [blame] | 1716 | trans->adding_csums = 0; |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 1717 | } |
| 1718 | return 0; |
| 1719 | } |
| 1720 | |
Josef Bacik | 2ac55d4 | 2010-02-03 19:33:23 +0000 | [diff] [blame] | 1721 | int btrfs_set_extent_delalloc(struct inode *inode, u64 start, u64 end, |
| 1722 | struct extent_state **cached_state) |
Chris Mason | ea8c281 | 2008-08-04 23:17:27 -0400 | [diff] [blame] | 1723 | { |
Julia Lawall | 6c1500f | 2012-11-03 20:30:18 +0000 | [diff] [blame] | 1724 | WARN_ON((end & (PAGE_CACHE_SIZE - 1)) == 0); |
Chris Mason | ea8c281 | 2008-08-04 23:17:27 -0400 | [diff] [blame] | 1725 | return set_extent_delalloc(&BTRFS_I(inode)->io_tree, start, end, |
Josef Bacik | 2ac55d4 | 2010-02-03 19:33:23 +0000 | [diff] [blame] | 1726 | cached_state, GFP_NOFS); |
Chris Mason | ea8c281 | 2008-08-04 23:17:27 -0400 | [diff] [blame] | 1727 | } |
| 1728 | |
Chris Mason | d352ac6 | 2008-09-29 15:18:18 -0400 | [diff] [blame] | 1729 | /* see btrfs_writepage_start_hook for details on why this is required */ |
Chris Mason | 247e743 | 2008-07-17 12:53:51 -0400 | [diff] [blame] | 1730 | struct btrfs_writepage_fixup { |
| 1731 | struct page *page; |
| 1732 | struct btrfs_work work; |
| 1733 | }; |
| 1734 | |
Christoph Hellwig | b295086 | 2008-12-02 09:54:17 -0500 | [diff] [blame] | 1735 | static void btrfs_writepage_fixup_worker(struct btrfs_work *work) |
Chris Mason | 247e743 | 2008-07-17 12:53:51 -0400 | [diff] [blame] | 1736 | { |
| 1737 | struct btrfs_writepage_fixup *fixup; |
| 1738 | struct btrfs_ordered_extent *ordered; |
Josef Bacik | 2ac55d4 | 2010-02-03 19:33:23 +0000 | [diff] [blame] | 1739 | struct extent_state *cached_state = NULL; |
Chris Mason | 247e743 | 2008-07-17 12:53:51 -0400 | [diff] [blame] | 1740 | struct page *page; |
| 1741 | struct inode *inode; |
| 1742 | u64 page_start; |
| 1743 | u64 page_end; |
Jeff Mahoney | 87826df | 2012-02-15 16:23:57 +0100 | [diff] [blame] | 1744 | int ret; |
Chris Mason | 247e743 | 2008-07-17 12:53:51 -0400 | [diff] [blame] | 1745 | |
| 1746 | fixup = container_of(work, struct btrfs_writepage_fixup, work); |
| 1747 | page = fixup->page; |
Chris Mason | 4a09675 | 2008-07-21 10:29:44 -0400 | [diff] [blame] | 1748 | again: |
Chris Mason | 247e743 | 2008-07-17 12:53:51 -0400 | [diff] [blame] | 1749 | lock_page(page); |
| 1750 | if (!page->mapping || !PageDirty(page) || !PageChecked(page)) { |
| 1751 | ClearPageChecked(page); |
| 1752 | goto out_page; |
| 1753 | } |
| 1754 | |
| 1755 | inode = page->mapping->host; |
| 1756 | page_start = page_offset(page); |
| 1757 | page_end = page_offset(page) + PAGE_CACHE_SIZE - 1; |
| 1758 | |
Josef Bacik | 2ac55d4 | 2010-02-03 19:33:23 +0000 | [diff] [blame] | 1759 | 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] | 1760 | &cached_state); |
Chris Mason | 4a09675 | 2008-07-21 10:29:44 -0400 | [diff] [blame] | 1761 | |
| 1762 | /* already ordered? We're done */ |
Chris Mason | 8b62b72 | 2009-09-02 16:53:46 -0400 | [diff] [blame] | 1763 | if (PagePrivate2(page)) |
Chris Mason | 247e743 | 2008-07-17 12:53:51 -0400 | [diff] [blame] | 1764 | goto out; |
Chris Mason | 4a09675 | 2008-07-21 10:29:44 -0400 | [diff] [blame] | 1765 | |
| 1766 | ordered = btrfs_lookup_ordered_extent(inode, page_start); |
| 1767 | if (ordered) { |
Josef Bacik | 2ac55d4 | 2010-02-03 19:33:23 +0000 | [diff] [blame] | 1768 | unlock_extent_cached(&BTRFS_I(inode)->io_tree, page_start, |
| 1769 | page_end, &cached_state, GFP_NOFS); |
Chris Mason | 4a09675 | 2008-07-21 10:29:44 -0400 | [diff] [blame] | 1770 | unlock_page(page); |
| 1771 | btrfs_start_ordered_extent(inode, ordered, 1); |
Jeff Mahoney | 87826df | 2012-02-15 16:23:57 +0100 | [diff] [blame] | 1772 | btrfs_put_ordered_extent(ordered); |
Chris Mason | 4a09675 | 2008-07-21 10:29:44 -0400 | [diff] [blame] | 1773 | goto again; |
| 1774 | } |
Chris Mason | 247e743 | 2008-07-17 12:53:51 -0400 | [diff] [blame] | 1775 | |
Jeff Mahoney | 87826df | 2012-02-15 16:23:57 +0100 | [diff] [blame] | 1776 | ret = btrfs_delalloc_reserve_space(inode, PAGE_CACHE_SIZE); |
| 1777 | if (ret) { |
| 1778 | mapping_set_error(page->mapping, ret); |
| 1779 | end_extent_writepage(page, ret, page_start, page_end); |
| 1780 | ClearPageChecked(page); |
| 1781 | goto out; |
| 1782 | } |
| 1783 | |
Josef Bacik | 2ac55d4 | 2010-02-03 19:33:23 +0000 | [diff] [blame] | 1784 | btrfs_set_extent_delalloc(inode, page_start, page_end, &cached_state); |
Chris Mason | 247e743 | 2008-07-17 12:53:51 -0400 | [diff] [blame] | 1785 | ClearPageChecked(page); |
Jeff Mahoney | 87826df | 2012-02-15 16:23:57 +0100 | [diff] [blame] | 1786 | set_page_dirty(page); |
Chris Mason | 247e743 | 2008-07-17 12:53:51 -0400 | [diff] [blame] | 1787 | out: |
Josef Bacik | 2ac55d4 | 2010-02-03 19:33:23 +0000 | [diff] [blame] | 1788 | unlock_extent_cached(&BTRFS_I(inode)->io_tree, page_start, page_end, |
| 1789 | &cached_state, GFP_NOFS); |
Chris Mason | 247e743 | 2008-07-17 12:53:51 -0400 | [diff] [blame] | 1790 | out_page: |
| 1791 | unlock_page(page); |
| 1792 | page_cache_release(page); |
Miao Xie | b897abe | 2011-01-26 16:19:22 +0800 | [diff] [blame] | 1793 | kfree(fixup); |
Chris Mason | 247e743 | 2008-07-17 12:53:51 -0400 | [diff] [blame] | 1794 | } |
| 1795 | |
| 1796 | /* |
| 1797 | * There are a few paths in the higher layers of the kernel that directly |
| 1798 | * set the page dirty bit without asking the filesystem if it is a |
| 1799 | * good idea. This causes problems because we want to make sure COW |
| 1800 | * properly happens and the data=ordered rules are followed. |
| 1801 | * |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 1802 | * 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] | 1803 | * hasn't been properly setup for IO. We kick off an async process |
| 1804 | * to fix it up. The async helper will wait for ordered extents, set |
| 1805 | * the delalloc bit and make it safe to write the page. |
| 1806 | */ |
Christoph Hellwig | b295086 | 2008-12-02 09:54:17 -0500 | [diff] [blame] | 1807 | 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] | 1808 | { |
| 1809 | struct inode *inode = page->mapping->host; |
| 1810 | struct btrfs_writepage_fixup *fixup; |
| 1811 | struct btrfs_root *root = BTRFS_I(inode)->root; |
Chris Mason | 247e743 | 2008-07-17 12:53:51 -0400 | [diff] [blame] | 1812 | |
Chris Mason | 8b62b72 | 2009-09-02 16:53:46 -0400 | [diff] [blame] | 1813 | /* this page is properly in the ordered list */ |
| 1814 | if (TestClearPagePrivate2(page)) |
Chris Mason | 247e743 | 2008-07-17 12:53:51 -0400 | [diff] [blame] | 1815 | return 0; |
| 1816 | |
| 1817 | if (PageChecked(page)) |
| 1818 | return -EAGAIN; |
| 1819 | |
| 1820 | fixup = kzalloc(sizeof(*fixup), GFP_NOFS); |
| 1821 | if (!fixup) |
| 1822 | return -EAGAIN; |
Chris Mason | f421950 | 2008-07-22 11:18:09 -0400 | [diff] [blame] | 1823 | |
Chris Mason | 247e743 | 2008-07-17 12:53:51 -0400 | [diff] [blame] | 1824 | SetPageChecked(page); |
| 1825 | page_cache_get(page); |
| 1826 | fixup->work.func = btrfs_writepage_fixup_worker; |
| 1827 | fixup->page = page; |
| 1828 | btrfs_queue_worker(&root->fs_info->fixup_workers, &fixup->work); |
Jeff Mahoney | 87826df | 2012-02-15 16:23:57 +0100 | [diff] [blame] | 1829 | return -EBUSY; |
Chris Mason | 247e743 | 2008-07-17 12:53:51 -0400 | [diff] [blame] | 1830 | } |
| 1831 | |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 1832 | static int insert_reserved_file_extent(struct btrfs_trans_handle *trans, |
| 1833 | struct inode *inode, u64 file_pos, |
| 1834 | u64 disk_bytenr, u64 disk_num_bytes, |
| 1835 | u64 num_bytes, u64 ram_bytes, |
| 1836 | u8 compression, u8 encryption, |
| 1837 | u16 other_encoding, int extent_type) |
| 1838 | { |
| 1839 | struct btrfs_root *root = BTRFS_I(inode)->root; |
| 1840 | struct btrfs_file_extent_item *fi; |
| 1841 | struct btrfs_path *path; |
| 1842 | struct extent_buffer *leaf; |
| 1843 | struct btrfs_key ins; |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 1844 | int ret; |
| 1845 | |
| 1846 | path = btrfs_alloc_path(); |
Mark Fasheh | d8926bb | 2011-07-13 10:38:47 -0700 | [diff] [blame] | 1847 | if (!path) |
| 1848 | return -ENOMEM; |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 1849 | |
Chris Mason | b947343 | 2009-03-13 11:00:37 -0400 | [diff] [blame] | 1850 | path->leave_spinning = 1; |
Chris Mason | a1ed835 | 2009-09-11 12:27:37 -0400 | [diff] [blame] | 1851 | |
| 1852 | /* |
| 1853 | * we may be replacing one extent in the tree with another. |
| 1854 | * The new extent is pinned in the extent map, and we don't want |
| 1855 | * to drop it from the cache until it is completely in the btree. |
| 1856 | * |
| 1857 | * So, tell btrfs_drop_extents to leave this extent in the cache. |
| 1858 | * the caller is expected to unpin it and allow it to be merged |
| 1859 | * with the others. |
| 1860 | */ |
Josef Bacik | 5dc562c | 2012-08-17 13:14:17 -0400 | [diff] [blame] | 1861 | ret = btrfs_drop_extents(trans, root, inode, file_pos, |
Josef Bacik | 2671485 | 2012-08-29 12:24:27 -0400 | [diff] [blame] | 1862 | file_pos + num_bytes, 0); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 1863 | if (ret) |
| 1864 | goto out; |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 1865 | |
Li Zefan | 33345d01 | 2011-04-20 10:31:50 +0800 | [diff] [blame] | 1866 | ins.objectid = btrfs_ino(inode); |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 1867 | ins.offset = file_pos; |
| 1868 | ins.type = BTRFS_EXTENT_DATA_KEY; |
| 1869 | ret = btrfs_insert_empty_item(trans, root, path, &ins, sizeof(*fi)); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 1870 | if (ret) |
| 1871 | goto out; |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 1872 | leaf = path->nodes[0]; |
| 1873 | fi = btrfs_item_ptr(leaf, path->slots[0], |
| 1874 | struct btrfs_file_extent_item); |
| 1875 | btrfs_set_file_extent_generation(leaf, fi, trans->transid); |
| 1876 | btrfs_set_file_extent_type(leaf, fi, extent_type); |
| 1877 | btrfs_set_file_extent_disk_bytenr(leaf, fi, disk_bytenr); |
| 1878 | btrfs_set_file_extent_disk_num_bytes(leaf, fi, disk_num_bytes); |
| 1879 | btrfs_set_file_extent_offset(leaf, fi, 0); |
| 1880 | btrfs_set_file_extent_num_bytes(leaf, fi, num_bytes); |
| 1881 | btrfs_set_file_extent_ram_bytes(leaf, fi, ram_bytes); |
| 1882 | btrfs_set_file_extent_compression(leaf, fi, compression); |
| 1883 | btrfs_set_file_extent_encryption(leaf, fi, encryption); |
| 1884 | btrfs_set_file_extent_other_encoding(leaf, fi, other_encoding); |
Chris Mason | b947343 | 2009-03-13 11:00:37 -0400 | [diff] [blame] | 1885 | |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 1886 | btrfs_mark_buffer_dirty(leaf); |
Josef Bacik | ce19533 | 2012-09-25 15:26:16 -0400 | [diff] [blame] | 1887 | btrfs_release_path(path); |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 1888 | |
| 1889 | inode_add_bytes(inode, num_bytes); |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 1890 | |
| 1891 | ins.objectid = disk_bytenr; |
| 1892 | ins.offset = disk_num_bytes; |
| 1893 | ins.type = BTRFS_EXTENT_ITEM_KEY; |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 1894 | ret = btrfs_alloc_reserved_file_extent(trans, root, |
| 1895 | root->root_key.objectid, |
Li Zefan | 33345d01 | 2011-04-20 10:31:50 +0800 | [diff] [blame] | 1896 | btrfs_ino(inode), file_pos, &ins); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 1897 | out: |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 1898 | btrfs_free_path(path); |
Chris Mason | b947343 | 2009-03-13 11:00:37 -0400 | [diff] [blame] | 1899 | |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 1900 | return ret; |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 1901 | } |
| 1902 | |
Liu Bo | 38c227d | 2013-01-29 03:18:40 +0000 | [diff] [blame] | 1903 | /* snapshot-aware defrag */ |
| 1904 | struct sa_defrag_extent_backref { |
| 1905 | struct rb_node node; |
| 1906 | struct old_sa_defrag_extent *old; |
| 1907 | u64 root_id; |
| 1908 | u64 inum; |
| 1909 | u64 file_pos; |
| 1910 | u64 extent_offset; |
| 1911 | u64 num_bytes; |
| 1912 | u64 generation; |
| 1913 | }; |
| 1914 | |
| 1915 | struct old_sa_defrag_extent { |
| 1916 | struct list_head list; |
| 1917 | struct new_sa_defrag_extent *new; |
| 1918 | |
| 1919 | u64 extent_offset; |
| 1920 | u64 bytenr; |
| 1921 | u64 offset; |
| 1922 | u64 len; |
| 1923 | int count; |
| 1924 | }; |
| 1925 | |
| 1926 | struct new_sa_defrag_extent { |
| 1927 | struct rb_root root; |
| 1928 | struct list_head head; |
| 1929 | struct btrfs_path *path; |
| 1930 | struct inode *inode; |
| 1931 | u64 file_pos; |
| 1932 | u64 len; |
| 1933 | u64 bytenr; |
| 1934 | u64 disk_len; |
| 1935 | u8 compress_type; |
| 1936 | }; |
| 1937 | |
| 1938 | static int backref_comp(struct sa_defrag_extent_backref *b1, |
| 1939 | struct sa_defrag_extent_backref *b2) |
| 1940 | { |
| 1941 | if (b1->root_id < b2->root_id) |
| 1942 | return -1; |
| 1943 | else if (b1->root_id > b2->root_id) |
| 1944 | return 1; |
| 1945 | |
| 1946 | if (b1->inum < b2->inum) |
| 1947 | return -1; |
| 1948 | else if (b1->inum > b2->inum) |
| 1949 | return 1; |
| 1950 | |
| 1951 | if (b1->file_pos < b2->file_pos) |
| 1952 | return -1; |
| 1953 | else if (b1->file_pos > b2->file_pos) |
| 1954 | return 1; |
| 1955 | |
| 1956 | /* |
| 1957 | * [------------------------------] ===> (a range of space) |
| 1958 | * |<--->| |<---->| =============> (fs/file tree A) |
| 1959 | * |<---------------------------->| ===> (fs/file tree B) |
| 1960 | * |
| 1961 | * A range of space can refer to two file extents in one tree while |
| 1962 | * refer to only one file extent in another tree. |
| 1963 | * |
| 1964 | * So we may process a disk offset more than one time(two extents in A) |
| 1965 | * and locate at the same extent(one extent in B), then insert two same |
| 1966 | * backrefs(both refer to the extent in B). |
| 1967 | */ |
| 1968 | return 0; |
| 1969 | } |
| 1970 | |
| 1971 | static void backref_insert(struct rb_root *root, |
| 1972 | struct sa_defrag_extent_backref *backref) |
| 1973 | { |
| 1974 | struct rb_node **p = &root->rb_node; |
| 1975 | struct rb_node *parent = NULL; |
| 1976 | struct sa_defrag_extent_backref *entry; |
| 1977 | int ret; |
| 1978 | |
| 1979 | while (*p) { |
| 1980 | parent = *p; |
| 1981 | entry = rb_entry(parent, struct sa_defrag_extent_backref, node); |
| 1982 | |
| 1983 | ret = backref_comp(backref, entry); |
| 1984 | if (ret < 0) |
| 1985 | p = &(*p)->rb_left; |
| 1986 | else |
| 1987 | p = &(*p)->rb_right; |
| 1988 | } |
| 1989 | |
| 1990 | rb_link_node(&backref->node, parent, p); |
| 1991 | rb_insert_color(&backref->node, root); |
| 1992 | } |
| 1993 | |
| 1994 | /* |
| 1995 | * Note the backref might has changed, and in this case we just return 0. |
| 1996 | */ |
| 1997 | static noinline int record_one_backref(u64 inum, u64 offset, u64 root_id, |
| 1998 | void *ctx) |
| 1999 | { |
| 2000 | struct btrfs_file_extent_item *extent; |
| 2001 | struct btrfs_fs_info *fs_info; |
| 2002 | struct old_sa_defrag_extent *old = ctx; |
| 2003 | struct new_sa_defrag_extent *new = old->new; |
| 2004 | struct btrfs_path *path = new->path; |
| 2005 | struct btrfs_key key; |
| 2006 | struct btrfs_root *root; |
| 2007 | struct sa_defrag_extent_backref *backref; |
| 2008 | struct extent_buffer *leaf; |
| 2009 | struct inode *inode = new->inode; |
| 2010 | int slot; |
| 2011 | int ret; |
| 2012 | u64 extent_offset; |
| 2013 | u64 num_bytes; |
| 2014 | |
| 2015 | if (BTRFS_I(inode)->root->root_key.objectid == root_id && |
| 2016 | inum == btrfs_ino(inode)) |
| 2017 | return 0; |
| 2018 | |
| 2019 | key.objectid = root_id; |
| 2020 | key.type = BTRFS_ROOT_ITEM_KEY; |
| 2021 | key.offset = (u64)-1; |
| 2022 | |
| 2023 | fs_info = BTRFS_I(inode)->root->fs_info; |
| 2024 | root = btrfs_read_fs_root_no_name(fs_info, &key); |
| 2025 | if (IS_ERR(root)) { |
| 2026 | if (PTR_ERR(root) == -ENOENT) |
| 2027 | return 0; |
| 2028 | WARN_ON(1); |
| 2029 | pr_debug("inum=%llu, offset=%llu, root_id=%llu\n", |
| 2030 | inum, offset, root_id); |
| 2031 | return PTR_ERR(root); |
| 2032 | } |
| 2033 | |
| 2034 | key.objectid = inum; |
| 2035 | key.type = BTRFS_EXTENT_DATA_KEY; |
| 2036 | if (offset > (u64)-1 << 32) |
| 2037 | key.offset = 0; |
| 2038 | else |
| 2039 | key.offset = offset; |
| 2040 | |
| 2041 | ret = btrfs_search_slot(NULL, root, &key, path, 0, 0); |
Dulshani Gunawardhana | fae7f21 | 2013-10-31 10:30:08 +0530 | [diff] [blame] | 2042 | if (WARN_ON(ret < 0)) |
Liu Bo | 38c227d | 2013-01-29 03:18:40 +0000 | [diff] [blame] | 2043 | return ret; |
Josef Bacik | 50f1319 | 2013-07-22 12:50:37 -0400 | [diff] [blame] | 2044 | ret = 0; |
Liu Bo | 38c227d | 2013-01-29 03:18:40 +0000 | [diff] [blame] | 2045 | |
| 2046 | while (1) { |
| 2047 | cond_resched(); |
| 2048 | |
| 2049 | leaf = path->nodes[0]; |
| 2050 | slot = path->slots[0]; |
| 2051 | |
| 2052 | if (slot >= btrfs_header_nritems(leaf)) { |
| 2053 | ret = btrfs_next_leaf(root, path); |
| 2054 | if (ret < 0) { |
| 2055 | goto out; |
| 2056 | } else if (ret > 0) { |
| 2057 | ret = 0; |
| 2058 | goto out; |
| 2059 | } |
| 2060 | continue; |
| 2061 | } |
| 2062 | |
| 2063 | path->slots[0]++; |
| 2064 | |
| 2065 | btrfs_item_key_to_cpu(leaf, &key, slot); |
| 2066 | |
| 2067 | if (key.objectid > inum) |
| 2068 | goto out; |
| 2069 | |
| 2070 | if (key.objectid < inum || key.type != BTRFS_EXTENT_DATA_KEY) |
| 2071 | continue; |
| 2072 | |
| 2073 | extent = btrfs_item_ptr(leaf, slot, |
| 2074 | struct btrfs_file_extent_item); |
| 2075 | |
| 2076 | if (btrfs_file_extent_disk_bytenr(leaf, extent) != old->bytenr) |
| 2077 | continue; |
| 2078 | |
Liu Bo | e68afa4 | 2013-07-01 22:13:26 +0800 | [diff] [blame] | 2079 | /* |
| 2080 | * 'offset' refers to the exact key.offset, |
| 2081 | * NOT the 'offset' field in btrfs_extent_data_ref, ie. |
| 2082 | * (key.offset - extent_offset). |
| 2083 | */ |
| 2084 | if (key.offset != offset) |
Liu Bo | 38c227d | 2013-01-29 03:18:40 +0000 | [diff] [blame] | 2085 | continue; |
| 2086 | |
Liu Bo | e68afa4 | 2013-07-01 22:13:26 +0800 | [diff] [blame] | 2087 | extent_offset = btrfs_file_extent_offset(leaf, extent); |
Liu Bo | 38c227d | 2013-01-29 03:18:40 +0000 | [diff] [blame] | 2088 | num_bytes = btrfs_file_extent_num_bytes(leaf, extent); |
Liu Bo | e68afa4 | 2013-07-01 22:13:26 +0800 | [diff] [blame] | 2089 | |
Liu Bo | 38c227d | 2013-01-29 03:18:40 +0000 | [diff] [blame] | 2090 | if (extent_offset >= old->extent_offset + old->offset + |
| 2091 | old->len || extent_offset + num_bytes <= |
| 2092 | old->extent_offset + old->offset) |
| 2093 | continue; |
Liu Bo | 38c227d | 2013-01-29 03:18:40 +0000 | [diff] [blame] | 2094 | break; |
| 2095 | } |
| 2096 | |
| 2097 | backref = kmalloc(sizeof(*backref), GFP_NOFS); |
| 2098 | if (!backref) { |
| 2099 | ret = -ENOENT; |
| 2100 | goto out; |
| 2101 | } |
| 2102 | |
| 2103 | backref->root_id = root_id; |
| 2104 | backref->inum = inum; |
Liu Bo | e68afa4 | 2013-07-01 22:13:26 +0800 | [diff] [blame] | 2105 | backref->file_pos = offset; |
Liu Bo | 38c227d | 2013-01-29 03:18:40 +0000 | [diff] [blame] | 2106 | backref->num_bytes = num_bytes; |
| 2107 | backref->extent_offset = extent_offset; |
| 2108 | backref->generation = btrfs_file_extent_generation(leaf, extent); |
| 2109 | backref->old = old; |
| 2110 | backref_insert(&new->root, backref); |
| 2111 | old->count++; |
| 2112 | out: |
| 2113 | btrfs_release_path(path); |
| 2114 | WARN_ON(ret); |
| 2115 | return ret; |
| 2116 | } |
| 2117 | |
| 2118 | static noinline bool record_extent_backrefs(struct btrfs_path *path, |
| 2119 | struct new_sa_defrag_extent *new) |
| 2120 | { |
| 2121 | struct btrfs_fs_info *fs_info = BTRFS_I(new->inode)->root->fs_info; |
| 2122 | struct old_sa_defrag_extent *old, *tmp; |
| 2123 | int ret; |
| 2124 | |
| 2125 | new->path = path; |
| 2126 | |
| 2127 | list_for_each_entry_safe(old, tmp, &new->head, list) { |
Liu Bo | e68afa4 | 2013-07-01 22:13:26 +0800 | [diff] [blame] | 2128 | ret = iterate_inodes_from_logical(old->bytenr + |
| 2129 | old->extent_offset, fs_info, |
Liu Bo | 38c227d | 2013-01-29 03:18:40 +0000 | [diff] [blame] | 2130 | path, record_one_backref, |
| 2131 | old); |
Josef Bacik | 4724b10 | 2013-11-05 11:11:40 -0500 | [diff] [blame] | 2132 | if (ret < 0 && ret != -ENOENT) |
| 2133 | return false; |
Liu Bo | 38c227d | 2013-01-29 03:18:40 +0000 | [diff] [blame] | 2134 | |
| 2135 | /* no backref to be processed for this extent */ |
| 2136 | if (!old->count) { |
| 2137 | list_del(&old->list); |
| 2138 | kfree(old); |
| 2139 | } |
| 2140 | } |
| 2141 | |
| 2142 | if (list_empty(&new->head)) |
| 2143 | return false; |
| 2144 | |
| 2145 | return true; |
| 2146 | } |
| 2147 | |
| 2148 | static int relink_is_mergable(struct extent_buffer *leaf, |
| 2149 | struct btrfs_file_extent_item *fi, |
Liu Bo | 116e002 | 2013-08-02 16:30:40 +0800 | [diff] [blame] | 2150 | struct new_sa_defrag_extent *new) |
Liu Bo | 38c227d | 2013-01-29 03:18:40 +0000 | [diff] [blame] | 2151 | { |
Liu Bo | 116e002 | 2013-08-02 16:30:40 +0800 | [diff] [blame] | 2152 | if (btrfs_file_extent_disk_bytenr(leaf, fi) != new->bytenr) |
Liu Bo | 38c227d | 2013-01-29 03:18:40 +0000 | [diff] [blame] | 2153 | return 0; |
| 2154 | |
| 2155 | if (btrfs_file_extent_type(leaf, fi) != BTRFS_FILE_EXTENT_REG) |
| 2156 | return 0; |
| 2157 | |
Liu Bo | 116e002 | 2013-08-02 16:30:40 +0800 | [diff] [blame] | 2158 | if (btrfs_file_extent_compression(leaf, fi) != new->compress_type) |
| 2159 | return 0; |
| 2160 | |
| 2161 | if (btrfs_file_extent_encryption(leaf, fi) || |
Liu Bo | 38c227d | 2013-01-29 03:18:40 +0000 | [diff] [blame] | 2162 | btrfs_file_extent_other_encoding(leaf, fi)) |
| 2163 | return 0; |
| 2164 | |
| 2165 | return 1; |
| 2166 | } |
| 2167 | |
| 2168 | /* |
| 2169 | * Note the backref might has changed, and in this case we just return 0. |
| 2170 | */ |
| 2171 | static noinline int relink_extent_backref(struct btrfs_path *path, |
| 2172 | struct sa_defrag_extent_backref *prev, |
| 2173 | struct sa_defrag_extent_backref *backref) |
| 2174 | { |
| 2175 | struct btrfs_file_extent_item *extent; |
| 2176 | struct btrfs_file_extent_item *item; |
| 2177 | struct btrfs_ordered_extent *ordered; |
| 2178 | struct btrfs_trans_handle *trans; |
| 2179 | struct btrfs_fs_info *fs_info; |
| 2180 | struct btrfs_root *root; |
| 2181 | struct btrfs_key key; |
| 2182 | struct extent_buffer *leaf; |
| 2183 | struct old_sa_defrag_extent *old = backref->old; |
| 2184 | struct new_sa_defrag_extent *new = old->new; |
| 2185 | struct inode *src_inode = new->inode; |
| 2186 | struct inode *inode; |
| 2187 | struct extent_state *cached = NULL; |
| 2188 | int ret = 0; |
| 2189 | u64 start; |
| 2190 | u64 len; |
| 2191 | u64 lock_start; |
| 2192 | u64 lock_end; |
| 2193 | bool merge = false; |
| 2194 | int index; |
| 2195 | |
| 2196 | if (prev && prev->root_id == backref->root_id && |
| 2197 | prev->inum == backref->inum && |
| 2198 | prev->file_pos + prev->num_bytes == backref->file_pos) |
| 2199 | merge = true; |
| 2200 | |
| 2201 | /* step 1: get root */ |
| 2202 | key.objectid = backref->root_id; |
| 2203 | key.type = BTRFS_ROOT_ITEM_KEY; |
| 2204 | key.offset = (u64)-1; |
| 2205 | |
| 2206 | fs_info = BTRFS_I(src_inode)->root->fs_info; |
| 2207 | index = srcu_read_lock(&fs_info->subvol_srcu); |
| 2208 | |
| 2209 | root = btrfs_read_fs_root_no_name(fs_info, &key); |
| 2210 | if (IS_ERR(root)) { |
| 2211 | srcu_read_unlock(&fs_info->subvol_srcu, index); |
| 2212 | if (PTR_ERR(root) == -ENOENT) |
| 2213 | return 0; |
| 2214 | return PTR_ERR(root); |
| 2215 | } |
Liu Bo | 38c227d | 2013-01-29 03:18:40 +0000 | [diff] [blame] | 2216 | |
| 2217 | /* step 2: get inode */ |
| 2218 | key.objectid = backref->inum; |
| 2219 | key.type = BTRFS_INODE_ITEM_KEY; |
| 2220 | key.offset = 0; |
| 2221 | |
| 2222 | inode = btrfs_iget(fs_info->sb, &key, root, NULL); |
| 2223 | if (IS_ERR(inode)) { |
| 2224 | srcu_read_unlock(&fs_info->subvol_srcu, index); |
| 2225 | return 0; |
| 2226 | } |
| 2227 | |
| 2228 | srcu_read_unlock(&fs_info->subvol_srcu, index); |
| 2229 | |
| 2230 | /* step 3: relink backref */ |
| 2231 | lock_start = backref->file_pos; |
| 2232 | lock_end = backref->file_pos + backref->num_bytes - 1; |
| 2233 | lock_extent_bits(&BTRFS_I(inode)->io_tree, lock_start, lock_end, |
| 2234 | 0, &cached); |
| 2235 | |
| 2236 | ordered = btrfs_lookup_first_ordered_extent(inode, lock_end); |
| 2237 | if (ordered) { |
| 2238 | btrfs_put_ordered_extent(ordered); |
| 2239 | goto out_unlock; |
| 2240 | } |
| 2241 | |
| 2242 | trans = btrfs_join_transaction(root); |
| 2243 | if (IS_ERR(trans)) { |
| 2244 | ret = PTR_ERR(trans); |
| 2245 | goto out_unlock; |
| 2246 | } |
| 2247 | |
| 2248 | key.objectid = backref->inum; |
| 2249 | key.type = BTRFS_EXTENT_DATA_KEY; |
| 2250 | key.offset = backref->file_pos; |
| 2251 | |
| 2252 | ret = btrfs_search_slot(NULL, root, &key, path, 0, 0); |
| 2253 | if (ret < 0) { |
| 2254 | goto out_free_path; |
| 2255 | } else if (ret > 0) { |
| 2256 | ret = 0; |
| 2257 | goto out_free_path; |
| 2258 | } |
| 2259 | |
| 2260 | extent = btrfs_item_ptr(path->nodes[0], path->slots[0], |
| 2261 | struct btrfs_file_extent_item); |
| 2262 | |
| 2263 | if (btrfs_file_extent_generation(path->nodes[0], extent) != |
| 2264 | backref->generation) |
| 2265 | goto out_free_path; |
| 2266 | |
| 2267 | btrfs_release_path(path); |
| 2268 | |
| 2269 | start = backref->file_pos; |
| 2270 | if (backref->extent_offset < old->extent_offset + old->offset) |
| 2271 | start += old->extent_offset + old->offset - |
| 2272 | backref->extent_offset; |
| 2273 | |
| 2274 | len = min(backref->extent_offset + backref->num_bytes, |
| 2275 | old->extent_offset + old->offset + old->len); |
| 2276 | len -= max(backref->extent_offset, old->extent_offset + old->offset); |
| 2277 | |
| 2278 | ret = btrfs_drop_extents(trans, root, inode, start, |
| 2279 | start + len, 1); |
| 2280 | if (ret) |
| 2281 | goto out_free_path; |
| 2282 | again: |
| 2283 | key.objectid = btrfs_ino(inode); |
| 2284 | key.type = BTRFS_EXTENT_DATA_KEY; |
| 2285 | key.offset = start; |
| 2286 | |
Liu Bo | a09a0a7 | 2013-03-11 09:20:58 +0000 | [diff] [blame] | 2287 | path->leave_spinning = 1; |
Liu Bo | 38c227d | 2013-01-29 03:18:40 +0000 | [diff] [blame] | 2288 | if (merge) { |
| 2289 | struct btrfs_file_extent_item *fi; |
| 2290 | u64 extent_len; |
| 2291 | struct btrfs_key found_key; |
| 2292 | |
| 2293 | ret = btrfs_search_slot(trans, root, &key, path, 1, 1); |
| 2294 | if (ret < 0) |
| 2295 | goto out_free_path; |
| 2296 | |
| 2297 | path->slots[0]--; |
| 2298 | leaf = path->nodes[0]; |
| 2299 | btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]); |
| 2300 | |
| 2301 | fi = btrfs_item_ptr(leaf, path->slots[0], |
| 2302 | struct btrfs_file_extent_item); |
| 2303 | extent_len = btrfs_file_extent_num_bytes(leaf, fi); |
| 2304 | |
Liu Bo | 116e002 | 2013-08-02 16:30:40 +0800 | [diff] [blame] | 2305 | if (extent_len + found_key.offset == start && |
| 2306 | relink_is_mergable(leaf, fi, new)) { |
Liu Bo | 38c227d | 2013-01-29 03:18:40 +0000 | [diff] [blame] | 2307 | btrfs_set_file_extent_num_bytes(leaf, fi, |
| 2308 | extent_len + len); |
| 2309 | btrfs_mark_buffer_dirty(leaf); |
| 2310 | inode_add_bytes(inode, len); |
| 2311 | |
| 2312 | ret = 1; |
| 2313 | goto out_free_path; |
| 2314 | } else { |
| 2315 | merge = false; |
| 2316 | btrfs_release_path(path); |
| 2317 | goto again; |
| 2318 | } |
| 2319 | } |
| 2320 | |
| 2321 | ret = btrfs_insert_empty_item(trans, root, path, &key, |
| 2322 | sizeof(*extent)); |
| 2323 | if (ret) { |
| 2324 | btrfs_abort_transaction(trans, root, ret); |
| 2325 | goto out_free_path; |
| 2326 | } |
| 2327 | |
| 2328 | leaf = path->nodes[0]; |
| 2329 | item = btrfs_item_ptr(leaf, path->slots[0], |
| 2330 | struct btrfs_file_extent_item); |
| 2331 | btrfs_set_file_extent_disk_bytenr(leaf, item, new->bytenr); |
| 2332 | btrfs_set_file_extent_disk_num_bytes(leaf, item, new->disk_len); |
| 2333 | btrfs_set_file_extent_offset(leaf, item, start - new->file_pos); |
| 2334 | btrfs_set_file_extent_num_bytes(leaf, item, len); |
| 2335 | btrfs_set_file_extent_ram_bytes(leaf, item, new->len); |
| 2336 | btrfs_set_file_extent_generation(leaf, item, trans->transid); |
| 2337 | btrfs_set_file_extent_type(leaf, item, BTRFS_FILE_EXTENT_REG); |
| 2338 | btrfs_set_file_extent_compression(leaf, item, new->compress_type); |
| 2339 | btrfs_set_file_extent_encryption(leaf, item, 0); |
| 2340 | btrfs_set_file_extent_other_encoding(leaf, item, 0); |
| 2341 | |
| 2342 | btrfs_mark_buffer_dirty(leaf); |
| 2343 | inode_add_bytes(inode, len); |
Liu Bo | a09a0a7 | 2013-03-11 09:20:58 +0000 | [diff] [blame] | 2344 | btrfs_release_path(path); |
Liu Bo | 38c227d | 2013-01-29 03:18:40 +0000 | [diff] [blame] | 2345 | |
| 2346 | ret = btrfs_inc_extent_ref(trans, root, new->bytenr, |
| 2347 | new->disk_len, 0, |
| 2348 | backref->root_id, backref->inum, |
| 2349 | new->file_pos, 0); /* start - extent_offset */ |
| 2350 | if (ret) { |
| 2351 | btrfs_abort_transaction(trans, root, ret); |
| 2352 | goto out_free_path; |
| 2353 | } |
| 2354 | |
| 2355 | ret = 1; |
| 2356 | out_free_path: |
| 2357 | btrfs_release_path(path); |
Liu Bo | a09a0a7 | 2013-03-11 09:20:58 +0000 | [diff] [blame] | 2358 | path->leave_spinning = 0; |
Liu Bo | 38c227d | 2013-01-29 03:18:40 +0000 | [diff] [blame] | 2359 | btrfs_end_transaction(trans, root); |
| 2360 | out_unlock: |
| 2361 | unlock_extent_cached(&BTRFS_I(inode)->io_tree, lock_start, lock_end, |
| 2362 | &cached, GFP_NOFS); |
| 2363 | iput(inode); |
| 2364 | return ret; |
| 2365 | } |
| 2366 | |
Liu Bo | 6f51956 | 2013-10-29 10:45:05 +0800 | [diff] [blame] | 2367 | static void free_sa_defrag_extent(struct new_sa_defrag_extent *new) |
| 2368 | { |
| 2369 | struct old_sa_defrag_extent *old, *tmp; |
| 2370 | |
| 2371 | if (!new) |
| 2372 | return; |
| 2373 | |
| 2374 | list_for_each_entry_safe(old, tmp, &new->head, list) { |
| 2375 | list_del(&old->list); |
| 2376 | kfree(old); |
| 2377 | } |
| 2378 | kfree(new); |
| 2379 | } |
| 2380 | |
Liu Bo | 38c227d | 2013-01-29 03:18:40 +0000 | [diff] [blame] | 2381 | static void relink_file_extents(struct new_sa_defrag_extent *new) |
| 2382 | { |
| 2383 | struct btrfs_path *path; |
Liu Bo | 38c227d | 2013-01-29 03:18:40 +0000 | [diff] [blame] | 2384 | struct sa_defrag_extent_backref *backref; |
| 2385 | struct sa_defrag_extent_backref *prev = NULL; |
| 2386 | struct inode *inode; |
| 2387 | struct btrfs_root *root; |
| 2388 | struct rb_node *node; |
| 2389 | int ret; |
| 2390 | |
| 2391 | inode = new->inode; |
| 2392 | root = BTRFS_I(inode)->root; |
| 2393 | |
| 2394 | path = btrfs_alloc_path(); |
| 2395 | if (!path) |
| 2396 | return; |
| 2397 | |
| 2398 | if (!record_extent_backrefs(path, new)) { |
| 2399 | btrfs_free_path(path); |
| 2400 | goto out; |
| 2401 | } |
| 2402 | btrfs_release_path(path); |
| 2403 | |
| 2404 | while (1) { |
| 2405 | node = rb_first(&new->root); |
| 2406 | if (!node) |
| 2407 | break; |
| 2408 | rb_erase(node, &new->root); |
| 2409 | |
| 2410 | backref = rb_entry(node, struct sa_defrag_extent_backref, node); |
| 2411 | |
| 2412 | ret = relink_extent_backref(path, prev, backref); |
| 2413 | WARN_ON(ret < 0); |
| 2414 | |
| 2415 | kfree(prev); |
| 2416 | |
| 2417 | if (ret == 1) |
| 2418 | prev = backref; |
| 2419 | else |
| 2420 | prev = NULL; |
| 2421 | cond_resched(); |
| 2422 | } |
| 2423 | kfree(prev); |
| 2424 | |
| 2425 | btrfs_free_path(path); |
Liu Bo | 38c227d | 2013-01-29 03:18:40 +0000 | [diff] [blame] | 2426 | out: |
Liu Bo | 6f51956 | 2013-10-29 10:45:05 +0800 | [diff] [blame] | 2427 | free_sa_defrag_extent(new); |
| 2428 | |
Liu Bo | 38c227d | 2013-01-29 03:18:40 +0000 | [diff] [blame] | 2429 | atomic_dec(&root->fs_info->defrag_running); |
| 2430 | wake_up(&root->fs_info->transaction_wait); |
Liu Bo | 38c227d | 2013-01-29 03:18:40 +0000 | [diff] [blame] | 2431 | } |
| 2432 | |
| 2433 | static struct new_sa_defrag_extent * |
| 2434 | record_old_file_extents(struct inode *inode, |
| 2435 | struct btrfs_ordered_extent *ordered) |
| 2436 | { |
| 2437 | struct btrfs_root *root = BTRFS_I(inode)->root; |
| 2438 | struct btrfs_path *path; |
| 2439 | struct btrfs_key key; |
Liu Bo | 6f51956 | 2013-10-29 10:45:05 +0800 | [diff] [blame] | 2440 | struct old_sa_defrag_extent *old; |
Liu Bo | 38c227d | 2013-01-29 03:18:40 +0000 | [diff] [blame] | 2441 | struct new_sa_defrag_extent *new; |
| 2442 | int ret; |
| 2443 | |
| 2444 | new = kmalloc(sizeof(*new), GFP_NOFS); |
| 2445 | if (!new) |
| 2446 | return NULL; |
| 2447 | |
| 2448 | new->inode = inode; |
| 2449 | new->file_pos = ordered->file_offset; |
| 2450 | new->len = ordered->len; |
| 2451 | new->bytenr = ordered->start; |
| 2452 | new->disk_len = ordered->disk_len; |
| 2453 | new->compress_type = ordered->compress_type; |
| 2454 | new->root = RB_ROOT; |
| 2455 | INIT_LIST_HEAD(&new->head); |
| 2456 | |
| 2457 | path = btrfs_alloc_path(); |
| 2458 | if (!path) |
| 2459 | goto out_kfree; |
| 2460 | |
| 2461 | key.objectid = btrfs_ino(inode); |
| 2462 | key.type = BTRFS_EXTENT_DATA_KEY; |
| 2463 | key.offset = new->file_pos; |
| 2464 | |
| 2465 | ret = btrfs_search_slot(NULL, root, &key, path, 0, 0); |
| 2466 | if (ret < 0) |
| 2467 | goto out_free_path; |
| 2468 | if (ret > 0 && path->slots[0] > 0) |
| 2469 | path->slots[0]--; |
| 2470 | |
| 2471 | /* find out all the old extents for the file range */ |
| 2472 | while (1) { |
| 2473 | struct btrfs_file_extent_item *extent; |
| 2474 | struct extent_buffer *l; |
| 2475 | int slot; |
| 2476 | u64 num_bytes; |
| 2477 | u64 offset; |
| 2478 | u64 end; |
| 2479 | u64 disk_bytenr; |
| 2480 | u64 extent_offset; |
| 2481 | |
| 2482 | l = path->nodes[0]; |
| 2483 | slot = path->slots[0]; |
| 2484 | |
| 2485 | if (slot >= btrfs_header_nritems(l)) { |
| 2486 | ret = btrfs_next_leaf(root, path); |
| 2487 | if (ret < 0) |
Liu Bo | 6f51956 | 2013-10-29 10:45:05 +0800 | [diff] [blame] | 2488 | goto out_free_path; |
Liu Bo | 38c227d | 2013-01-29 03:18:40 +0000 | [diff] [blame] | 2489 | else if (ret > 0) |
| 2490 | break; |
| 2491 | continue; |
| 2492 | } |
| 2493 | |
| 2494 | btrfs_item_key_to_cpu(l, &key, slot); |
| 2495 | |
| 2496 | if (key.objectid != btrfs_ino(inode)) |
| 2497 | break; |
| 2498 | if (key.type != BTRFS_EXTENT_DATA_KEY) |
| 2499 | break; |
| 2500 | if (key.offset >= new->file_pos + new->len) |
| 2501 | break; |
| 2502 | |
| 2503 | extent = btrfs_item_ptr(l, slot, struct btrfs_file_extent_item); |
| 2504 | |
| 2505 | num_bytes = btrfs_file_extent_num_bytes(l, extent); |
| 2506 | if (key.offset + num_bytes < new->file_pos) |
| 2507 | goto next; |
| 2508 | |
| 2509 | disk_bytenr = btrfs_file_extent_disk_bytenr(l, extent); |
| 2510 | if (!disk_bytenr) |
| 2511 | goto next; |
| 2512 | |
| 2513 | extent_offset = btrfs_file_extent_offset(l, extent); |
| 2514 | |
| 2515 | old = kmalloc(sizeof(*old), GFP_NOFS); |
| 2516 | if (!old) |
Liu Bo | 6f51956 | 2013-10-29 10:45:05 +0800 | [diff] [blame] | 2517 | goto out_free_path; |
Liu Bo | 38c227d | 2013-01-29 03:18:40 +0000 | [diff] [blame] | 2518 | |
| 2519 | offset = max(new->file_pos, key.offset); |
| 2520 | end = min(new->file_pos + new->len, key.offset + num_bytes); |
| 2521 | |
| 2522 | old->bytenr = disk_bytenr; |
| 2523 | old->extent_offset = extent_offset; |
| 2524 | old->offset = offset - key.offset; |
| 2525 | old->len = end - offset; |
| 2526 | old->new = new; |
| 2527 | old->count = 0; |
| 2528 | list_add_tail(&old->list, &new->head); |
| 2529 | next: |
| 2530 | path->slots[0]++; |
| 2531 | cond_resched(); |
| 2532 | } |
| 2533 | |
| 2534 | btrfs_free_path(path); |
| 2535 | atomic_inc(&root->fs_info->defrag_running); |
| 2536 | |
| 2537 | return new; |
| 2538 | |
Liu Bo | 38c227d | 2013-01-29 03:18:40 +0000 | [diff] [blame] | 2539 | out_free_path: |
| 2540 | btrfs_free_path(path); |
| 2541 | out_kfree: |
Liu Bo | 6f51956 | 2013-10-29 10:45:05 +0800 | [diff] [blame] | 2542 | free_sa_defrag_extent(new); |
Liu Bo | 38c227d | 2013-01-29 03:18:40 +0000 | [diff] [blame] | 2543 | return NULL; |
| 2544 | } |
| 2545 | |
Chris Mason | d352ac6 | 2008-09-29 15:18:18 -0400 | [diff] [blame] | 2546 | /* as ordered data IO finishes, this gets called so we can finish |
| 2547 | * an ordered extent if the range of bytes in the file it covers are |
| 2548 | * fully written. |
| 2549 | */ |
Josef Bacik | 5fd0204 | 2012-05-02 14:00:54 -0400 | [diff] [blame] | 2550 | static int btrfs_finish_ordered_io(struct btrfs_ordered_extent *ordered_extent) |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 2551 | { |
Josef Bacik | 5fd0204 | 2012-05-02 14:00:54 -0400 | [diff] [blame] | 2552 | struct inode *inode = ordered_extent->inode; |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 2553 | struct btrfs_root *root = BTRFS_I(inode)->root; |
Yan, Zheng | 0ca1f7c | 2010-05-16 10:48:47 -0400 | [diff] [blame] | 2554 | struct btrfs_trans_handle *trans = NULL; |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 2555 | struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree; |
Josef Bacik | 2ac55d4 | 2010-02-03 19:33:23 +0000 | [diff] [blame] | 2556 | struct extent_state *cached_state = NULL; |
Liu Bo | 38c227d | 2013-01-29 03:18:40 +0000 | [diff] [blame] | 2557 | struct new_sa_defrag_extent *new = NULL; |
Li Zefan | 261507a0 | 2010-12-17 14:21:50 +0800 | [diff] [blame] | 2558 | int compress_type = 0; |
Josef Bacik | 77cef2e | 2013-08-29 13:57:21 -0400 | [diff] [blame] | 2559 | int ret = 0; |
| 2560 | u64 logical_len = ordered_extent->len; |
Li Zefan | 82d5902 | 2011-04-20 10:33:24 +0800 | [diff] [blame] | 2561 | bool nolock; |
Josef Bacik | 77cef2e | 2013-08-29 13:57:21 -0400 | [diff] [blame] | 2562 | bool truncated = false; |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 2563 | |
Liu Bo | 83eea1f | 2012-07-10 05:28:39 -0600 | [diff] [blame] | 2564 | nolock = btrfs_is_free_space_inode(inode); |
Josef Bacik | 0cb59c9 | 2010-07-02 12:14:14 -0400 | [diff] [blame] | 2565 | |
Josef Bacik | 5fd0204 | 2012-05-02 14:00:54 -0400 | [diff] [blame] | 2566 | if (test_bit(BTRFS_ORDERED_IOERR, &ordered_extent->flags)) { |
| 2567 | ret = -EIO; |
| 2568 | goto out; |
| 2569 | } |
| 2570 | |
Josef Bacik | 77cef2e | 2013-08-29 13:57:21 -0400 | [diff] [blame] | 2571 | if (test_bit(BTRFS_ORDERED_TRUNCATED, &ordered_extent->flags)) { |
| 2572 | truncated = true; |
| 2573 | logical_len = ordered_extent->truncated_len; |
| 2574 | /* Truncated the entire extent, don't bother adding */ |
| 2575 | if (!logical_len) |
| 2576 | goto out; |
| 2577 | } |
| 2578 | |
Yan, Zheng | c216775 | 2009-11-12 09:34:21 +0000 | [diff] [blame] | 2579 | if (test_bit(BTRFS_ORDERED_NOCOW, &ordered_extent->flags)) { |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 2580 | BUG_ON(!list_empty(&ordered_extent->list)); /* Logic error */ |
Josef Bacik | 6c760c0 | 2012-11-09 10:53:21 -0500 | [diff] [blame] | 2581 | btrfs_ordered_update_i_size(inode, 0, ordered_extent); |
| 2582 | if (nolock) |
| 2583 | trans = btrfs_join_transaction_nolock(root); |
| 2584 | else |
| 2585 | trans = btrfs_join_transaction(root); |
| 2586 | if (IS_ERR(trans)) { |
| 2587 | ret = PTR_ERR(trans); |
| 2588 | trans = NULL; |
| 2589 | goto out; |
Yan, Zheng | c216775 | 2009-11-12 09:34:21 +0000 | [diff] [blame] | 2590 | } |
Josef Bacik | 6c760c0 | 2012-11-09 10:53:21 -0500 | [diff] [blame] | 2591 | trans->block_rsv = &root->fs_info->delalloc_block_rsv; |
| 2592 | ret = btrfs_update_inode_fallback(trans, root, inode); |
| 2593 | if (ret) /* -ENOMEM or corruption */ |
| 2594 | btrfs_abort_transaction(trans, root, ret); |
Yan, Zheng | c216775 | 2009-11-12 09:34:21 +0000 | [diff] [blame] | 2595 | goto out; |
| 2596 | } |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 2597 | |
Josef Bacik | 2ac55d4 | 2010-02-03 19:33:23 +0000 | [diff] [blame] | 2598 | lock_extent_bits(io_tree, ordered_extent->file_offset, |
| 2599 | ordered_extent->file_offset + ordered_extent->len - 1, |
Jeff Mahoney | d008237 | 2012-03-01 14:57:19 +0100 | [diff] [blame] | 2600 | 0, &cached_state); |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 2601 | |
Liu Bo | 38c227d | 2013-01-29 03:18:40 +0000 | [diff] [blame] | 2602 | ret = test_range_bit(io_tree, ordered_extent->file_offset, |
| 2603 | ordered_extent->file_offset + ordered_extent->len - 1, |
| 2604 | EXTENT_DEFRAG, 1, cached_state); |
| 2605 | if (ret) { |
| 2606 | u64 last_snapshot = btrfs_root_last_snapshot(&root->root_item); |
| 2607 | if (last_snapshot >= BTRFS_I(inode)->generation) |
| 2608 | /* the inode is shared */ |
| 2609 | new = record_old_file_extents(inode, ordered_extent); |
| 2610 | |
| 2611 | clear_extent_bit(io_tree, ordered_extent->file_offset, |
| 2612 | ordered_extent->file_offset + ordered_extent->len - 1, |
| 2613 | EXTENT_DEFRAG, 0, 0, &cached_state, GFP_NOFS); |
| 2614 | } |
| 2615 | |
Josef Bacik | 0cb59c9 | 2010-07-02 12:14:14 -0400 | [diff] [blame] | 2616 | if (nolock) |
Josef Bacik | 7a7eaa4 | 2011-04-13 12:54:33 -0400 | [diff] [blame] | 2617 | trans = btrfs_join_transaction_nolock(root); |
Josef Bacik | 0cb59c9 | 2010-07-02 12:14:14 -0400 | [diff] [blame] | 2618 | else |
Josef Bacik | 7a7eaa4 | 2011-04-13 12:54:33 -0400 | [diff] [blame] | 2619 | trans = btrfs_join_transaction(root); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 2620 | if (IS_ERR(trans)) { |
| 2621 | ret = PTR_ERR(trans); |
| 2622 | trans = NULL; |
| 2623 | goto out_unlock; |
| 2624 | } |
Yan, Zheng | 0ca1f7c | 2010-05-16 10:48:47 -0400 | [diff] [blame] | 2625 | trans->block_rsv = &root->fs_info->delalloc_block_rsv; |
Yan, Zheng | c216775 | 2009-11-12 09:34:21 +0000 | [diff] [blame] | 2626 | |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 2627 | if (test_bit(BTRFS_ORDERED_COMPRESSED, &ordered_extent->flags)) |
Li Zefan | 261507a0 | 2010-12-17 14:21:50 +0800 | [diff] [blame] | 2628 | compress_type = ordered_extent->compress_type; |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 2629 | if (test_bit(BTRFS_ORDERED_PREALLOC, &ordered_extent->flags)) { |
Li Zefan | 261507a0 | 2010-12-17 14:21:50 +0800 | [diff] [blame] | 2630 | BUG_ON(compress_type); |
Yan, Zheng | 920bbbf | 2009-11-12 09:34:08 +0000 | [diff] [blame] | 2631 | ret = btrfs_mark_extent_written(trans, inode, |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 2632 | ordered_extent->file_offset, |
| 2633 | ordered_extent->file_offset + |
Josef Bacik | 77cef2e | 2013-08-29 13:57:21 -0400 | [diff] [blame] | 2634 | logical_len); |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 2635 | } else { |
Josef Bacik | 0af3d00 | 2010-06-21 14:48:16 -0400 | [diff] [blame] | 2636 | BUG_ON(root == root->fs_info->tree_root); |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 2637 | ret = insert_reserved_file_extent(trans, inode, |
| 2638 | ordered_extent->file_offset, |
| 2639 | ordered_extent->start, |
| 2640 | ordered_extent->disk_len, |
Josef Bacik | 77cef2e | 2013-08-29 13:57:21 -0400 | [diff] [blame] | 2641 | logical_len, logical_len, |
Li Zefan | 261507a0 | 2010-12-17 14:21:50 +0800 | [diff] [blame] | 2642 | compress_type, 0, 0, |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 2643 | BTRFS_FILE_EXTENT_REG); |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 2644 | } |
Josef Bacik | 5dc562c | 2012-08-17 13:14:17 -0400 | [diff] [blame] | 2645 | unpin_extent_cache(&BTRFS_I(inode)->extent_tree, |
| 2646 | ordered_extent->file_offset, ordered_extent->len, |
| 2647 | trans->transid); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 2648 | if (ret < 0) { |
| 2649 | btrfs_abort_transaction(trans, root, ret); |
Josef Bacik | 5fd0204 | 2012-05-02 14:00:54 -0400 | [diff] [blame] | 2650 | goto out_unlock; |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 2651 | } |
Josef Bacik | 2ac55d4 | 2010-02-03 19:33:23 +0000 | [diff] [blame] | 2652 | |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 2653 | add_pending_csums(trans, inode, ordered_extent->file_offset, |
| 2654 | &ordered_extent->list); |
| 2655 | |
Josef Bacik | 6c760c0 | 2012-11-09 10:53:21 -0500 | [diff] [blame] | 2656 | btrfs_ordered_update_i_size(inode, 0, ordered_extent); |
| 2657 | ret = btrfs_update_inode_fallback(trans, root, inode); |
| 2658 | if (ret) { /* -ENOMEM or corruption */ |
| 2659 | btrfs_abort_transaction(trans, root, ret); |
| 2660 | goto out_unlock; |
Josef Bacik | 1ef30be | 2011-04-05 19:25:36 -0400 | [diff] [blame] | 2661 | } |
| 2662 | ret = 0; |
Josef Bacik | 5fd0204 | 2012-05-02 14:00:54 -0400 | [diff] [blame] | 2663 | out_unlock: |
| 2664 | unlock_extent_cached(io_tree, ordered_extent->file_offset, |
| 2665 | ordered_extent->file_offset + |
| 2666 | ordered_extent->len - 1, &cached_state, GFP_NOFS); |
Yan, Zheng | c216775 | 2009-11-12 09:34:21 +0000 | [diff] [blame] | 2667 | out: |
Josef Bacik | 5b0e95b | 2011-10-06 08:58:24 -0400 | [diff] [blame] | 2668 | if (root != root->fs_info->tree_root) |
Josef Bacik | 0cb59c9 | 2010-07-02 12:14:14 -0400 | [diff] [blame] | 2669 | btrfs_delalloc_release_metadata(inode, ordered_extent->len); |
Miao Xie | a698d075 | 2012-09-20 01:51:59 -0600 | [diff] [blame] | 2670 | if (trans) |
| 2671 | btrfs_end_transaction(trans, root); |
Josef Bacik | 0cb59c9 | 2010-07-02 12:14:14 -0400 | [diff] [blame] | 2672 | |
Josef Bacik | 77cef2e | 2013-08-29 13:57:21 -0400 | [diff] [blame] | 2673 | if (ret || truncated) { |
| 2674 | u64 start, end; |
| 2675 | |
| 2676 | if (truncated) |
| 2677 | start = ordered_extent->file_offset + logical_len; |
| 2678 | else |
| 2679 | start = ordered_extent->file_offset; |
| 2680 | end = ordered_extent->file_offset + ordered_extent->len - 1; |
| 2681 | clear_extent_uptodate(io_tree, start, end, NULL, GFP_NOFS); |
| 2682 | |
| 2683 | /* Drop the cache for the part of the extent we didn't write. */ |
| 2684 | btrfs_drop_extent_cache(inode, start, end, 0); |
Josef Bacik | 5fd0204 | 2012-05-02 14:00:54 -0400 | [diff] [blame] | 2685 | |
Josef Bacik | 0bec9ef | 2013-01-31 14:58:00 -0500 | [diff] [blame] | 2686 | /* |
| 2687 | * If the ordered extent had an IOERR or something else went |
| 2688 | * wrong we need to return the space for this ordered extent |
Josef Bacik | 77cef2e | 2013-08-29 13:57:21 -0400 | [diff] [blame] | 2689 | * back to the allocator. We only free the extent in the |
| 2690 | * truncated case if we didn't write out the extent at all. |
Josef Bacik | 0bec9ef | 2013-01-31 14:58:00 -0500 | [diff] [blame] | 2691 | */ |
Josef Bacik | 77cef2e | 2013-08-29 13:57:21 -0400 | [diff] [blame] | 2692 | if ((ret || !logical_len) && |
| 2693 | !test_bit(BTRFS_ORDERED_NOCOW, &ordered_extent->flags) && |
Josef Bacik | 0bec9ef | 2013-01-31 14:58:00 -0500 | [diff] [blame] | 2694 | !test_bit(BTRFS_ORDERED_PREALLOC, &ordered_extent->flags)) |
| 2695 | btrfs_free_reserved_extent(root, ordered_extent->start, |
| 2696 | ordered_extent->disk_len); |
| 2697 | } |
| 2698 | |
| 2699 | |
Josef Bacik | 5fd0204 | 2012-05-02 14:00:54 -0400 | [diff] [blame] | 2700 | /* |
Liu Bo | 8bad3c0 | 2012-06-18 12:14:23 +0800 | [diff] [blame] | 2701 | * This needs to be done to make sure anybody waiting knows we are done |
| 2702 | * updating everything for this ordered extent. |
Josef Bacik | 5fd0204 | 2012-05-02 14:00:54 -0400 | [diff] [blame] | 2703 | */ |
| 2704 | btrfs_remove_ordered_extent(inode, ordered_extent); |
| 2705 | |
Liu Bo | 38c227d | 2013-01-29 03:18:40 +0000 | [diff] [blame] | 2706 | /* for snapshot-aware defrag */ |
Liu Bo | 6f51956 | 2013-10-29 10:45:05 +0800 | [diff] [blame] | 2707 | if (new) { |
| 2708 | if (ret) { |
| 2709 | free_sa_defrag_extent(new); |
| 2710 | atomic_dec(&root->fs_info->defrag_running); |
| 2711 | } else { |
| 2712 | relink_file_extents(new); |
| 2713 | } |
| 2714 | } |
Liu Bo | 38c227d | 2013-01-29 03:18:40 +0000 | [diff] [blame] | 2715 | |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 2716 | /* once for us */ |
| 2717 | btrfs_put_ordered_extent(ordered_extent); |
| 2718 | /* once for the tree */ |
| 2719 | btrfs_put_ordered_extent(ordered_extent); |
| 2720 | |
Josef Bacik | 5fd0204 | 2012-05-02 14:00:54 -0400 | [diff] [blame] | 2721 | return ret; |
| 2722 | } |
| 2723 | |
| 2724 | static void finish_ordered_fn(struct btrfs_work *work) |
| 2725 | { |
| 2726 | struct btrfs_ordered_extent *ordered_extent; |
| 2727 | ordered_extent = container_of(work, struct btrfs_ordered_extent, work); |
| 2728 | btrfs_finish_ordered_io(ordered_extent); |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 2729 | } |
| 2730 | |
Christoph Hellwig | b295086 | 2008-12-02 09:54:17 -0500 | [diff] [blame] | 2731 | 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] | 2732 | struct extent_state *state, int uptodate) |
| 2733 | { |
Josef Bacik | 5fd0204 | 2012-05-02 14:00:54 -0400 | [diff] [blame] | 2734 | struct inode *inode = page->mapping->host; |
| 2735 | struct btrfs_root *root = BTRFS_I(inode)->root; |
| 2736 | struct btrfs_ordered_extent *ordered_extent = NULL; |
| 2737 | struct btrfs_workers *workers; |
| 2738 | |
liubo | 1abe9b8 | 2011-03-24 11:18:59 +0000 | [diff] [blame] | 2739 | trace_btrfs_writepage_end_io_hook(page, start, end, uptodate); |
| 2740 | |
Chris Mason | 8b62b72 | 2009-09-02 16:53:46 -0400 | [diff] [blame] | 2741 | ClearPagePrivate2(page); |
Josef Bacik | 5fd0204 | 2012-05-02 14:00:54 -0400 | [diff] [blame] | 2742 | if (!btrfs_dec_test_ordered_pending(inode, &ordered_extent, start, |
| 2743 | end - start + 1, uptodate)) |
| 2744 | return 0; |
| 2745 | |
| 2746 | ordered_extent->work.func = finish_ordered_fn; |
| 2747 | ordered_extent->work.flags = 0; |
| 2748 | |
Liu Bo | 83eea1f | 2012-07-10 05:28:39 -0600 | [diff] [blame] | 2749 | if (btrfs_is_free_space_inode(inode)) |
Josef Bacik | 5fd0204 | 2012-05-02 14:00:54 -0400 | [diff] [blame] | 2750 | workers = &root->fs_info->endio_freespace_worker; |
| 2751 | else |
| 2752 | workers = &root->fs_info->endio_write_workers; |
| 2753 | btrfs_queue_worker(workers, &ordered_extent->work); |
| 2754 | |
| 2755 | return 0; |
Chris Mason | 211f90e | 2008-07-18 11:56:15 -0400 | [diff] [blame] | 2756 | } |
| 2757 | |
Chris Mason | d352ac6 | 2008-09-29 15:18:18 -0400 | [diff] [blame] | 2758 | /* |
Chris Mason | d352ac6 | 2008-09-29 15:18:18 -0400 | [diff] [blame] | 2759 | * 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] | 2760 | * if there's a match, we allow the bio to finish. If not, the code in |
| 2761 | * extent_io.c will try to find good copies for us. |
Chris Mason | d352ac6 | 2008-09-29 15:18:18 -0400 | [diff] [blame] | 2762 | */ |
Miao Xie | facc8a22 | 2013-07-25 19:22:34 +0800 | [diff] [blame] | 2763 | static int btrfs_readpage_end_io_hook(struct btrfs_io_bio *io_bio, |
| 2764 | u64 phy_offset, struct page *page, |
| 2765 | u64 start, u64 end, int mirror) |
Chris Mason | 07157aa | 2007-08-30 08:50:51 -0400 | [diff] [blame] | 2766 | { |
Miao Xie | 4eee4fa | 2012-12-21 09:17:45 +0000 | [diff] [blame] | 2767 | size_t offset = start - page_offset(page); |
Chris Mason | 07157aa | 2007-08-30 08:50:51 -0400 | [diff] [blame] | 2768 | struct inode *inode = page->mapping->host; |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 2769 | struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree; |
Chris Mason | 07157aa | 2007-08-30 08:50:51 -0400 | [diff] [blame] | 2770 | char *kaddr; |
Chris Mason | ff79f81 | 2007-10-15 16:22:25 -0400 | [diff] [blame] | 2771 | struct btrfs_root *root = BTRFS_I(inode)->root; |
Miao Xie | facc8a22 | 2013-07-25 19:22:34 +0800 | [diff] [blame] | 2772 | u32 csum_expected; |
Chris Mason | ff79f81 | 2007-10-15 16:22:25 -0400 | [diff] [blame] | 2773 | u32 csum = ~(u32)0; |
Simon Kirby | c2cf52e | 2013-03-19 22:41:23 +0000 | [diff] [blame] | 2774 | static DEFINE_RATELIMIT_STATE(_rs, DEFAULT_RATELIMIT_INTERVAL, |
| 2775 | DEFAULT_RATELIMIT_BURST); |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 2776 | |
Chris Mason | d20f704 | 2008-12-08 16:58:54 -0500 | [diff] [blame] | 2777 | if (PageChecked(page)) { |
| 2778 | ClearPageChecked(page); |
| 2779 | goto good; |
| 2780 | } |
Christoph Hellwig | 6cbff00 | 2009-04-17 10:37:41 +0200 | [diff] [blame] | 2781 | |
| 2782 | if (BTRFS_I(inode)->flags & BTRFS_INODE_NODATASUM) |
Jan Schmidt | 08d2f34 | 2011-05-04 16:18:50 +0200 | [diff] [blame] | 2783 | goto good; |
Chris Mason | d20f704 | 2008-12-08 16:58:54 -0500 | [diff] [blame] | 2784 | |
Yan Zheng | 17d217f | 2008-12-12 10:03:38 -0500 | [diff] [blame] | 2785 | if (root->root_key.objectid == BTRFS_DATA_RELOC_TREE_OBJECTID && |
Chris Mason | 9655d29 | 2009-09-02 15:22:30 -0400 | [diff] [blame] | 2786 | test_range_bit(io_tree, start, end, EXTENT_NODATASUM, 1, NULL)) { |
Yan Zheng | 17d217f | 2008-12-12 10:03:38 -0500 | [diff] [blame] | 2787 | clear_extent_bits(io_tree, start, end, EXTENT_NODATASUM, |
| 2788 | GFP_NOFS); |
| 2789 | return 0; |
| 2790 | } |
| 2791 | |
Miao Xie | facc8a22 | 2013-07-25 19:22:34 +0800 | [diff] [blame] | 2792 | phy_offset >>= inode->i_sb->s_blocksize_bits; |
| 2793 | csum_expected = *(((u32 *)io_bio->csum) + phy_offset); |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 2794 | |
Miao Xie | facc8a22 | 2013-07-25 19:22:34 +0800 | [diff] [blame] | 2795 | kaddr = kmap_atomic(page); |
Liu Bo | b049668 | 2013-03-14 14:57:45 +0000 | [diff] [blame] | 2796 | csum = btrfs_csum_data(kaddr + offset, csum, end - start + 1); |
Chris Mason | ff79f81 | 2007-10-15 16:22:25 -0400 | [diff] [blame] | 2797 | btrfs_csum_final(csum, (char *)&csum); |
Miao Xie | facc8a22 | 2013-07-25 19:22:34 +0800 | [diff] [blame] | 2798 | if (csum != csum_expected) |
Chris Mason | 07157aa | 2007-08-30 08:50:51 -0400 | [diff] [blame] | 2799 | goto zeroit; |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 2800 | |
Cong Wang | 7ac687d | 2011-11-25 23:14:28 +0800 | [diff] [blame] | 2801 | kunmap_atomic(kaddr); |
Chris Mason | d20f704 | 2008-12-08 16:58:54 -0500 | [diff] [blame] | 2802 | good: |
Chris Mason | 07157aa | 2007-08-30 08:50:51 -0400 | [diff] [blame] | 2803 | return 0; |
| 2804 | |
| 2805 | zeroit: |
Simon Kirby | c2cf52e | 2013-03-19 22:41:23 +0000 | [diff] [blame] | 2806 | if (__ratelimit(&_rs)) |
Miao Xie | facc8a22 | 2013-07-25 19:22:34 +0800 | [diff] [blame] | 2807 | btrfs_info(root->fs_info, "csum failed ino %llu off %llu csum %u expected csum %u", |
Geert Uytterhoeven | c1c9ff7 | 2013-08-20 13:20:07 +0200 | [diff] [blame] | 2808 | btrfs_ino(page->mapping->host), start, csum, csum_expected); |
Chris Mason | db94535 | 2007-10-15 16:15:53 -0400 | [diff] [blame] | 2809 | memset(kaddr + offset, 1, end - start + 1); |
| 2810 | flush_dcache_page(page); |
Cong Wang | 7ac687d | 2011-11-25 23:14:28 +0800 | [diff] [blame] | 2811 | kunmap_atomic(kaddr); |
Miao Xie | facc8a22 | 2013-07-25 19:22:34 +0800 | [diff] [blame] | 2812 | if (csum_expected == 0) |
Chris Mason | 3b95151 | 2008-04-17 11:29:12 -0400 | [diff] [blame] | 2813 | return 0; |
Chris Mason | 7e38326 | 2008-04-09 16:28:12 -0400 | [diff] [blame] | 2814 | return -EIO; |
Chris Mason | 07157aa | 2007-08-30 08:50:51 -0400 | [diff] [blame] | 2815 | } |
Chris Mason | b888db2 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 2816 | |
Yan, Zheng | 24bbcf0 | 2009-11-12 09:36:34 +0000 | [diff] [blame] | 2817 | struct delayed_iput { |
| 2818 | struct list_head list; |
| 2819 | struct inode *inode; |
| 2820 | }; |
| 2821 | |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 2822 | /* JDM: If this is fs-wide, why can't we add a pointer to |
| 2823 | * btrfs_inode instead and avoid the allocation? */ |
Yan, Zheng | 24bbcf0 | 2009-11-12 09:36:34 +0000 | [diff] [blame] | 2824 | void btrfs_add_delayed_iput(struct inode *inode) |
| 2825 | { |
| 2826 | struct btrfs_fs_info *fs_info = BTRFS_I(inode)->root->fs_info; |
| 2827 | struct delayed_iput *delayed; |
| 2828 | |
| 2829 | if (atomic_add_unless(&inode->i_count, -1, 1)) |
| 2830 | return; |
| 2831 | |
| 2832 | delayed = kmalloc(sizeof(*delayed), GFP_NOFS | __GFP_NOFAIL); |
| 2833 | delayed->inode = inode; |
| 2834 | |
| 2835 | spin_lock(&fs_info->delayed_iput_lock); |
| 2836 | list_add_tail(&delayed->list, &fs_info->delayed_iputs); |
| 2837 | spin_unlock(&fs_info->delayed_iput_lock); |
| 2838 | } |
| 2839 | |
| 2840 | void btrfs_run_delayed_iputs(struct btrfs_root *root) |
| 2841 | { |
| 2842 | LIST_HEAD(list); |
| 2843 | struct btrfs_fs_info *fs_info = root->fs_info; |
| 2844 | struct delayed_iput *delayed; |
| 2845 | int empty; |
| 2846 | |
| 2847 | spin_lock(&fs_info->delayed_iput_lock); |
| 2848 | empty = list_empty(&fs_info->delayed_iputs); |
| 2849 | spin_unlock(&fs_info->delayed_iput_lock); |
| 2850 | if (empty) |
| 2851 | return; |
| 2852 | |
Yan, Zheng | 24bbcf0 | 2009-11-12 09:36:34 +0000 | [diff] [blame] | 2853 | spin_lock(&fs_info->delayed_iput_lock); |
| 2854 | list_splice_init(&fs_info->delayed_iputs, &list); |
| 2855 | spin_unlock(&fs_info->delayed_iput_lock); |
| 2856 | |
| 2857 | while (!list_empty(&list)) { |
| 2858 | delayed = list_entry(list.next, struct delayed_iput, list); |
| 2859 | list_del(&delayed->list); |
| 2860 | iput(delayed->inode); |
| 2861 | kfree(delayed); |
| 2862 | } |
Yan, Zheng | 24bbcf0 | 2009-11-12 09:36:34 +0000 | [diff] [blame] | 2863 | } |
| 2864 | |
Yan, Zheng | d68fc57 | 2010-05-16 10:49:58 -0400 | [diff] [blame] | 2865 | /* |
Justin P. Mattock | 42b2aa8 | 2011-11-28 20:31:00 -0800 | [diff] [blame] | 2866 | * This is called in transaction commit time. If there are no orphan |
Yan, Zheng | d68fc57 | 2010-05-16 10:49:58 -0400 | [diff] [blame] | 2867 | * files in the subvolume, it removes orphan item and frees block_rsv |
| 2868 | * structure. |
| 2869 | */ |
| 2870 | void btrfs_orphan_commit_root(struct btrfs_trans_handle *trans, |
| 2871 | struct btrfs_root *root) |
| 2872 | { |
Josef Bacik | 90290e1 | 2011-12-02 15:44:12 -0500 | [diff] [blame] | 2873 | struct btrfs_block_rsv *block_rsv; |
Yan, Zheng | d68fc57 | 2010-05-16 10:49:58 -0400 | [diff] [blame] | 2874 | int ret; |
| 2875 | |
Josef Bacik | 8a35d95 | 2012-05-23 14:26:42 -0400 | [diff] [blame] | 2876 | if (atomic_read(&root->orphan_inodes) || |
Yan, Zheng | d68fc57 | 2010-05-16 10:49:58 -0400 | [diff] [blame] | 2877 | root->orphan_cleanup_state != ORPHAN_CLEANUP_DONE) |
| 2878 | return; |
| 2879 | |
Josef Bacik | 90290e1 | 2011-12-02 15:44:12 -0500 | [diff] [blame] | 2880 | spin_lock(&root->orphan_lock); |
Josef Bacik | 8a35d95 | 2012-05-23 14:26:42 -0400 | [diff] [blame] | 2881 | if (atomic_read(&root->orphan_inodes)) { |
Josef Bacik | 90290e1 | 2011-12-02 15:44:12 -0500 | [diff] [blame] | 2882 | spin_unlock(&root->orphan_lock); |
| 2883 | return; |
| 2884 | } |
| 2885 | |
| 2886 | if (root->orphan_cleanup_state != ORPHAN_CLEANUP_DONE) { |
| 2887 | spin_unlock(&root->orphan_lock); |
| 2888 | return; |
| 2889 | } |
| 2890 | |
| 2891 | block_rsv = root->orphan_block_rsv; |
| 2892 | root->orphan_block_rsv = NULL; |
| 2893 | spin_unlock(&root->orphan_lock); |
| 2894 | |
Yan, Zheng | d68fc57 | 2010-05-16 10:49:58 -0400 | [diff] [blame] | 2895 | if (root->orphan_item_inserted && |
| 2896 | btrfs_root_refs(&root->root_item) > 0) { |
| 2897 | ret = btrfs_del_orphan_item(trans, root->fs_info->tree_root, |
| 2898 | root->root_key.objectid); |
Josef Bacik | 4ef31a4 | 2013-08-13 14:10:08 -0400 | [diff] [blame] | 2899 | if (ret) |
| 2900 | btrfs_abort_transaction(trans, root, ret); |
| 2901 | else |
| 2902 | root->orphan_item_inserted = 0; |
Yan, Zheng | d68fc57 | 2010-05-16 10:49:58 -0400 | [diff] [blame] | 2903 | } |
| 2904 | |
Josef Bacik | 90290e1 | 2011-12-02 15:44:12 -0500 | [diff] [blame] | 2905 | if (block_rsv) { |
| 2906 | WARN_ON(block_rsv->size > 0); |
| 2907 | btrfs_free_block_rsv(root, block_rsv); |
Yan, Zheng | d68fc57 | 2010-05-16 10:49:58 -0400 | [diff] [blame] | 2908 | } |
| 2909 | } |
| 2910 | |
| 2911 | /* |
Josef Bacik | 7b12876 | 2008-07-24 12:17:14 -0400 | [diff] [blame] | 2912 | * This creates an orphan entry for the given inode in case something goes |
| 2913 | * wrong in the middle of an unlink/truncate. |
Yan, Zheng | d68fc57 | 2010-05-16 10:49:58 -0400 | [diff] [blame] | 2914 | * |
| 2915 | * NOTE: caller of this function should reserve 5 units of metadata for |
| 2916 | * this function. |
Josef Bacik | 7b12876 | 2008-07-24 12:17:14 -0400 | [diff] [blame] | 2917 | */ |
| 2918 | int btrfs_orphan_add(struct btrfs_trans_handle *trans, struct inode *inode) |
| 2919 | { |
| 2920 | struct btrfs_root *root = BTRFS_I(inode)->root; |
Yan, Zheng | d68fc57 | 2010-05-16 10:49:58 -0400 | [diff] [blame] | 2921 | struct btrfs_block_rsv *block_rsv = NULL; |
| 2922 | int reserve = 0; |
| 2923 | int insert = 0; |
| 2924 | int ret; |
Josef Bacik | 7b12876 | 2008-07-24 12:17:14 -0400 | [diff] [blame] | 2925 | |
Yan, Zheng | d68fc57 | 2010-05-16 10:49:58 -0400 | [diff] [blame] | 2926 | if (!root->orphan_block_rsv) { |
Miao Xie | 66d8f3d | 2012-09-06 04:02:28 -0600 | [diff] [blame] | 2927 | block_rsv = btrfs_alloc_block_rsv(root, BTRFS_BLOCK_RSV_TEMP); |
Tsutomu Itoh | b532402 | 2011-07-19 07:27:20 +0000 | [diff] [blame] | 2928 | if (!block_rsv) |
| 2929 | return -ENOMEM; |
Josef Bacik | 7b12876 | 2008-07-24 12:17:14 -0400 | [diff] [blame] | 2930 | } |
| 2931 | |
Yan, Zheng | d68fc57 | 2010-05-16 10:49:58 -0400 | [diff] [blame] | 2932 | spin_lock(&root->orphan_lock); |
| 2933 | if (!root->orphan_block_rsv) { |
| 2934 | root->orphan_block_rsv = block_rsv; |
| 2935 | } else if (block_rsv) { |
| 2936 | btrfs_free_block_rsv(root, block_rsv); |
| 2937 | block_rsv = NULL; |
| 2938 | } |
Josef Bacik | 7b12876 | 2008-07-24 12:17:14 -0400 | [diff] [blame] | 2939 | |
Josef Bacik | 8a35d95 | 2012-05-23 14:26:42 -0400 | [diff] [blame] | 2940 | if (!test_and_set_bit(BTRFS_INODE_HAS_ORPHAN_ITEM, |
| 2941 | &BTRFS_I(inode)->runtime_flags)) { |
Yan, Zheng | d68fc57 | 2010-05-16 10:49:58 -0400 | [diff] [blame] | 2942 | #if 0 |
| 2943 | /* |
| 2944 | * For proper ENOSPC handling, we should do orphan |
| 2945 | * cleanup when mounting. But this introduces backward |
| 2946 | * compatibility issue. |
| 2947 | */ |
| 2948 | if (!xchg(&root->orphan_item_inserted, 1)) |
| 2949 | insert = 2; |
| 2950 | else |
| 2951 | insert = 1; |
| 2952 | #endif |
| 2953 | insert = 1; |
Miao Xie | 321f0e7 | 2012-08-28 22:13:02 -0600 | [diff] [blame] | 2954 | atomic_inc(&root->orphan_inodes); |
Yan, Zheng | d68fc57 | 2010-05-16 10:49:58 -0400 | [diff] [blame] | 2955 | } |
Josef Bacik | 7b12876 | 2008-07-24 12:17:14 -0400 | [diff] [blame] | 2956 | |
Josef Bacik | 72ac3c0 | 2012-05-23 14:13:11 -0400 | [diff] [blame] | 2957 | if (!test_and_set_bit(BTRFS_INODE_ORPHAN_META_RESERVED, |
| 2958 | &BTRFS_I(inode)->runtime_flags)) |
Yan, Zheng | d68fc57 | 2010-05-16 10:49:58 -0400 | [diff] [blame] | 2959 | reserve = 1; |
Yan, Zheng | d68fc57 | 2010-05-16 10:49:58 -0400 | [diff] [blame] | 2960 | spin_unlock(&root->orphan_lock); |
Josef Bacik | 7b12876 | 2008-07-24 12:17:14 -0400 | [diff] [blame] | 2961 | |
Yan, Zheng | d68fc57 | 2010-05-16 10:49:58 -0400 | [diff] [blame] | 2962 | /* grab metadata reservation from transaction handle */ |
| 2963 | if (reserve) { |
| 2964 | ret = btrfs_orphan_reserve_metadata(trans, inode); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 2965 | BUG_ON(ret); /* -ENOSPC in reservation; Logic error? JDM */ |
Yan, Zheng | d68fc57 | 2010-05-16 10:49:58 -0400 | [diff] [blame] | 2966 | } |
| 2967 | |
| 2968 | /* insert an orphan item to track this unlinked/truncated file */ |
| 2969 | if (insert >= 1) { |
Li Zefan | 33345d01 | 2011-04-20 10:31:50 +0800 | [diff] [blame] | 2970 | ret = btrfs_insert_orphan_item(trans, root, btrfs_ino(inode)); |
Josef Bacik | 4ef31a4 | 2013-08-13 14:10:08 -0400 | [diff] [blame] | 2971 | if (ret) { |
Filipe David Borba Manana | 703c88e | 2013-09-22 21:54:55 +0100 | [diff] [blame] | 2972 | atomic_dec(&root->orphan_inodes); |
Josef Bacik | 4ef31a4 | 2013-08-13 14:10:08 -0400 | [diff] [blame] | 2973 | if (reserve) { |
| 2974 | clear_bit(BTRFS_INODE_ORPHAN_META_RESERVED, |
| 2975 | &BTRFS_I(inode)->runtime_flags); |
| 2976 | btrfs_orphan_release_metadata(inode); |
| 2977 | } |
| 2978 | if (ret != -EEXIST) { |
Josef Bacik | e8e7cff | 2013-08-21 15:54:00 -0400 | [diff] [blame] | 2979 | clear_bit(BTRFS_INODE_HAS_ORPHAN_ITEM, |
| 2980 | &BTRFS_I(inode)->runtime_flags); |
Josef Bacik | 4ef31a4 | 2013-08-13 14:10:08 -0400 | [diff] [blame] | 2981 | btrfs_abort_transaction(trans, root, ret); |
| 2982 | return ret; |
| 2983 | } |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 2984 | } |
| 2985 | ret = 0; |
Yan, Zheng | d68fc57 | 2010-05-16 10:49:58 -0400 | [diff] [blame] | 2986 | } |
| 2987 | |
| 2988 | /* insert an orphan item to track subvolume contains orphan files */ |
| 2989 | if (insert >= 2) { |
| 2990 | ret = btrfs_insert_orphan_item(trans, root->fs_info->tree_root, |
| 2991 | root->root_key.objectid); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 2992 | if (ret && ret != -EEXIST) { |
| 2993 | btrfs_abort_transaction(trans, root, ret); |
| 2994 | return ret; |
| 2995 | } |
Yan, Zheng | d68fc57 | 2010-05-16 10:49:58 -0400 | [diff] [blame] | 2996 | } |
| 2997 | return 0; |
Josef Bacik | 7b12876 | 2008-07-24 12:17:14 -0400 | [diff] [blame] | 2998 | } |
| 2999 | |
| 3000 | /* |
| 3001 | * We have done the truncate/delete so we can go ahead and remove the orphan |
| 3002 | * item for this particular inode. |
| 3003 | */ |
Eric Sandeen | 48a3b63 | 2013-04-25 20:41:01 +0000 | [diff] [blame] | 3004 | static int btrfs_orphan_del(struct btrfs_trans_handle *trans, |
| 3005 | struct inode *inode) |
Josef Bacik | 7b12876 | 2008-07-24 12:17:14 -0400 | [diff] [blame] | 3006 | { |
| 3007 | struct btrfs_root *root = BTRFS_I(inode)->root; |
Yan, Zheng | d68fc57 | 2010-05-16 10:49:58 -0400 | [diff] [blame] | 3008 | int delete_item = 0; |
| 3009 | int release_rsv = 0; |
Josef Bacik | 7b12876 | 2008-07-24 12:17:14 -0400 | [diff] [blame] | 3010 | int ret = 0; |
| 3011 | |
Yan, Zheng | d68fc57 | 2010-05-16 10:49:58 -0400 | [diff] [blame] | 3012 | spin_lock(&root->orphan_lock); |
Josef Bacik | 8a35d95 | 2012-05-23 14:26:42 -0400 | [diff] [blame] | 3013 | if (test_and_clear_bit(BTRFS_INODE_HAS_ORPHAN_ITEM, |
| 3014 | &BTRFS_I(inode)->runtime_flags)) |
Yan, Zheng | d68fc57 | 2010-05-16 10:49:58 -0400 | [diff] [blame] | 3015 | delete_item = 1; |
Josef Bacik | 7b12876 | 2008-07-24 12:17:14 -0400 | [diff] [blame] | 3016 | |
Josef Bacik | 72ac3c0 | 2012-05-23 14:13:11 -0400 | [diff] [blame] | 3017 | if (test_and_clear_bit(BTRFS_INODE_ORPHAN_META_RESERVED, |
| 3018 | &BTRFS_I(inode)->runtime_flags)) |
Yan, Zheng | d68fc57 | 2010-05-16 10:49:58 -0400 | [diff] [blame] | 3019 | release_rsv = 1; |
Yan, Zheng | d68fc57 | 2010-05-16 10:49:58 -0400 | [diff] [blame] | 3020 | spin_unlock(&root->orphan_lock); |
| 3021 | |
Filipe David Borba Manana | 703c88e | 2013-09-22 21:54:55 +0100 | [diff] [blame] | 3022 | if (delete_item) { |
Josef Bacik | 8a35d95 | 2012-05-23 14:26:42 -0400 | [diff] [blame] | 3023 | atomic_dec(&root->orphan_inodes); |
Filipe David Borba Manana | 703c88e | 2013-09-22 21:54:55 +0100 | [diff] [blame] | 3024 | if (trans) |
| 3025 | ret = btrfs_del_orphan_item(trans, root, |
| 3026 | btrfs_ino(inode)); |
Josef Bacik | 8a35d95 | 2012-05-23 14:26:42 -0400 | [diff] [blame] | 3027 | } |
Josef Bacik | 7b12876 | 2008-07-24 12:17:14 -0400 | [diff] [blame] | 3028 | |
Filipe David Borba Manana | 703c88e | 2013-09-22 21:54:55 +0100 | [diff] [blame] | 3029 | if (release_rsv) |
| 3030 | btrfs_orphan_release_metadata(inode); |
| 3031 | |
Josef Bacik | 4ef31a4 | 2013-08-13 14:10:08 -0400 | [diff] [blame] | 3032 | return ret; |
Josef Bacik | 7b12876 | 2008-07-24 12:17:14 -0400 | [diff] [blame] | 3033 | } |
| 3034 | |
| 3035 | /* |
| 3036 | * this cleans up any orphans that may be left on the list from the last use |
| 3037 | * of this root. |
| 3038 | */ |
Josef Bacik | 66b4ffd | 2011-01-31 16:22:42 -0500 | [diff] [blame] | 3039 | int btrfs_orphan_cleanup(struct btrfs_root *root) |
Josef Bacik | 7b12876 | 2008-07-24 12:17:14 -0400 | [diff] [blame] | 3040 | { |
| 3041 | struct btrfs_path *path; |
| 3042 | struct extent_buffer *leaf; |
Josef Bacik | 7b12876 | 2008-07-24 12:17:14 -0400 | [diff] [blame] | 3043 | struct btrfs_key key, found_key; |
| 3044 | struct btrfs_trans_handle *trans; |
| 3045 | struct inode *inode; |
Josef Bacik | 8f6d7f4 | 2011-09-26 15:55:20 -0400 | [diff] [blame] | 3046 | u64 last_objectid = 0; |
Josef Bacik | 7b12876 | 2008-07-24 12:17:14 -0400 | [diff] [blame] | 3047 | int ret = 0, nr_unlink = 0, nr_truncate = 0; |
| 3048 | |
Yan, Zheng | d68fc57 | 2010-05-16 10:49:58 -0400 | [diff] [blame] | 3049 | if (cmpxchg(&root->orphan_cleanup_state, 0, ORPHAN_CLEANUP_STARTED)) |
Josef Bacik | 66b4ffd | 2011-01-31 16:22:42 -0500 | [diff] [blame] | 3050 | return 0; |
Yan, Zheng | c71bf09 | 2009-11-12 09:34:40 +0000 | [diff] [blame] | 3051 | |
| 3052 | path = btrfs_alloc_path(); |
Josef Bacik | 66b4ffd | 2011-01-31 16:22:42 -0500 | [diff] [blame] | 3053 | if (!path) { |
| 3054 | ret = -ENOMEM; |
| 3055 | goto out; |
| 3056 | } |
Josef Bacik | 7b12876 | 2008-07-24 12:17:14 -0400 | [diff] [blame] | 3057 | path->reada = -1; |
| 3058 | |
| 3059 | key.objectid = BTRFS_ORPHAN_OBJECTID; |
| 3060 | btrfs_set_key_type(&key, BTRFS_ORPHAN_ITEM_KEY); |
| 3061 | key.offset = (u64)-1; |
| 3062 | |
Josef Bacik | 7b12876 | 2008-07-24 12:17:14 -0400 | [diff] [blame] | 3063 | while (1) { |
| 3064 | ret = btrfs_search_slot(NULL, root, &key, path, 0, 0); |
Josef Bacik | 66b4ffd | 2011-01-31 16:22:42 -0500 | [diff] [blame] | 3065 | if (ret < 0) |
| 3066 | goto out; |
Josef Bacik | 7b12876 | 2008-07-24 12:17:14 -0400 | [diff] [blame] | 3067 | |
| 3068 | /* |
| 3069 | * 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] | 3070 | * 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] | 3071 | * find the key and see if we have stuff that matches |
| 3072 | */ |
| 3073 | if (ret > 0) { |
Josef Bacik | 66b4ffd | 2011-01-31 16:22:42 -0500 | [diff] [blame] | 3074 | ret = 0; |
Josef Bacik | 7b12876 | 2008-07-24 12:17:14 -0400 | [diff] [blame] | 3075 | if (path->slots[0] == 0) |
| 3076 | break; |
| 3077 | path->slots[0]--; |
| 3078 | } |
| 3079 | |
| 3080 | /* pull out the item */ |
| 3081 | leaf = path->nodes[0]; |
Josef Bacik | 7b12876 | 2008-07-24 12:17:14 -0400 | [diff] [blame] | 3082 | btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]); |
| 3083 | |
| 3084 | /* make sure the item matches what we want */ |
| 3085 | if (found_key.objectid != BTRFS_ORPHAN_OBJECTID) |
| 3086 | break; |
| 3087 | if (btrfs_key_type(&found_key) != BTRFS_ORPHAN_ITEM_KEY) |
| 3088 | break; |
| 3089 | |
| 3090 | /* release the path since we're done with it */ |
David Sterba | b3b4aa7 | 2011-04-21 01:20:15 +0200 | [diff] [blame] | 3091 | btrfs_release_path(path); |
Josef Bacik | 7b12876 | 2008-07-24 12:17:14 -0400 | [diff] [blame] | 3092 | |
| 3093 | /* |
| 3094 | * this is where we are basically btrfs_lookup, without the |
| 3095 | * crossing root thing. we store the inode number in the |
| 3096 | * offset of the orphan item. |
| 3097 | */ |
Josef Bacik | 8f6d7f4 | 2011-09-26 15:55:20 -0400 | [diff] [blame] | 3098 | |
| 3099 | if (found_key.offset == last_objectid) { |
Simon Kirby | c2cf52e | 2013-03-19 22:41:23 +0000 | [diff] [blame] | 3100 | btrfs_err(root->fs_info, |
| 3101 | "Error removing orphan entry, stopping orphan cleanup"); |
Josef Bacik | 8f6d7f4 | 2011-09-26 15:55:20 -0400 | [diff] [blame] | 3102 | ret = -EINVAL; |
| 3103 | goto out; |
| 3104 | } |
| 3105 | |
| 3106 | last_objectid = found_key.offset; |
| 3107 | |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 3108 | found_key.objectid = found_key.offset; |
| 3109 | found_key.type = BTRFS_INODE_ITEM_KEY; |
| 3110 | found_key.offset = 0; |
Josef Bacik | 73f7341 | 2009-12-04 17:38:27 +0000 | [diff] [blame] | 3111 | inode = btrfs_iget(root->fs_info->sb, &found_key, root, NULL); |
Rusty Russell | 8c6ffba | 2013-07-15 11:20:32 +0930 | [diff] [blame] | 3112 | ret = PTR_ERR_OR_ZERO(inode); |
Josef Bacik | a8c9e57 | 2011-09-21 16:55:59 -0400 | [diff] [blame] | 3113 | if (ret && ret != -ESTALE) |
Josef Bacik | 66b4ffd | 2011-01-31 16:22:42 -0500 | [diff] [blame] | 3114 | goto out; |
Josef Bacik | a8c9e57 | 2011-09-21 16:55:59 -0400 | [diff] [blame] | 3115 | |
Arne Jansen | f8e9e0b | 2011-12-14 20:12:02 -0500 | [diff] [blame] | 3116 | if (ret == -ESTALE && root == root->fs_info->tree_root) { |
| 3117 | struct btrfs_root *dead_root; |
| 3118 | struct btrfs_fs_info *fs_info = root->fs_info; |
| 3119 | int is_dead_root = 0; |
| 3120 | |
| 3121 | /* |
| 3122 | * this is an orphan in the tree root. Currently these |
| 3123 | * could come from 2 sources: |
| 3124 | * a) a snapshot deletion in progress |
| 3125 | * b) a free space cache inode |
| 3126 | * We need to distinguish those two, as the snapshot |
| 3127 | * orphan must not get deleted. |
| 3128 | * find_dead_roots already ran before us, so if this |
| 3129 | * is a snapshot deletion, we should find the root |
| 3130 | * in the dead_roots list |
| 3131 | */ |
| 3132 | spin_lock(&fs_info->trans_lock); |
| 3133 | list_for_each_entry(dead_root, &fs_info->dead_roots, |
| 3134 | root_list) { |
| 3135 | if (dead_root->root_key.objectid == |
| 3136 | found_key.objectid) { |
| 3137 | is_dead_root = 1; |
| 3138 | break; |
| 3139 | } |
| 3140 | } |
| 3141 | spin_unlock(&fs_info->trans_lock); |
| 3142 | if (is_dead_root) { |
| 3143 | /* prevent this orphan from being found again */ |
| 3144 | key.offset = found_key.objectid - 1; |
| 3145 | continue; |
| 3146 | } |
| 3147 | } |
Josef Bacik | a8c9e57 | 2011-09-21 16:55:59 -0400 | [diff] [blame] | 3148 | /* |
| 3149 | * Inode is already gone but the orphan item is still there, |
| 3150 | * kill the orphan item. |
| 3151 | */ |
| 3152 | if (ret == -ESTALE) { |
| 3153 | trans = btrfs_start_transaction(root, 1); |
| 3154 | if (IS_ERR(trans)) { |
| 3155 | ret = PTR_ERR(trans); |
| 3156 | goto out; |
| 3157 | } |
Simon Kirby | c2cf52e | 2013-03-19 22:41:23 +0000 | [diff] [blame] | 3158 | btrfs_debug(root->fs_info, "auto deleting %Lu", |
| 3159 | found_key.objectid); |
Josef Bacik | a8c9e57 | 2011-09-21 16:55:59 -0400 | [diff] [blame] | 3160 | ret = btrfs_del_orphan_item(trans, root, |
| 3161 | found_key.objectid); |
Josef Bacik | a8c9e57 | 2011-09-21 16:55:59 -0400 | [diff] [blame] | 3162 | btrfs_end_transaction(trans, root); |
Josef Bacik | 4ef31a4 | 2013-08-13 14:10:08 -0400 | [diff] [blame] | 3163 | if (ret) |
| 3164 | goto out; |
Josef Bacik | a8c9e57 | 2011-09-21 16:55:59 -0400 | [diff] [blame] | 3165 | continue; |
Josef Bacik | 66b4ffd | 2011-01-31 16:22:42 -0500 | [diff] [blame] | 3166 | } |
Josef Bacik | 7b12876 | 2008-07-24 12:17:14 -0400 | [diff] [blame] | 3167 | |
Josef Bacik | 7b12876 | 2008-07-24 12:17:14 -0400 | [diff] [blame] | 3168 | /* |
| 3169 | * add this inode to the orphan list so btrfs_orphan_del does |
| 3170 | * the proper thing when we hit it |
| 3171 | */ |
Josef Bacik | 8a35d95 | 2012-05-23 14:26:42 -0400 | [diff] [blame] | 3172 | set_bit(BTRFS_INODE_HAS_ORPHAN_ITEM, |
| 3173 | &BTRFS_I(inode)->runtime_flags); |
Josef Bacik | 925396e | 2013-02-01 15:57:47 -0500 | [diff] [blame] | 3174 | atomic_inc(&root->orphan_inodes); |
Josef Bacik | 7b12876 | 2008-07-24 12:17:14 -0400 | [diff] [blame] | 3175 | |
Josef Bacik | 7b12876 | 2008-07-24 12:17:14 -0400 | [diff] [blame] | 3176 | /* if we have links, this was a truncate, lets do that */ |
| 3177 | if (inode->i_nlink) { |
Dulshani Gunawardhana | fae7f21 | 2013-10-31 10:30:08 +0530 | [diff] [blame] | 3178 | if (WARN_ON(!S_ISREG(inode->i_mode))) { |
Josef Bacik | a41ad39 | 2011-01-31 15:30:16 -0500 | [diff] [blame] | 3179 | iput(inode); |
| 3180 | continue; |
| 3181 | } |
Josef Bacik | 7b12876 | 2008-07-24 12:17:14 -0400 | [diff] [blame] | 3182 | nr_truncate++; |
Josef Bacik | f3fe820 | 2013-01-07 17:03:21 -0500 | [diff] [blame] | 3183 | |
| 3184 | /* 1 for the orphan item deletion. */ |
| 3185 | trans = btrfs_start_transaction(root, 1); |
| 3186 | if (IS_ERR(trans)) { |
Josef Bacik | c69b26b | 2013-06-03 16:51:23 -0400 | [diff] [blame] | 3187 | iput(inode); |
Josef Bacik | f3fe820 | 2013-01-07 17:03:21 -0500 | [diff] [blame] | 3188 | ret = PTR_ERR(trans); |
| 3189 | goto out; |
| 3190 | } |
| 3191 | ret = btrfs_orphan_add(trans, inode); |
| 3192 | btrfs_end_transaction(trans, root); |
Josef Bacik | c69b26b | 2013-06-03 16:51:23 -0400 | [diff] [blame] | 3193 | if (ret) { |
| 3194 | iput(inode); |
Josef Bacik | f3fe820 | 2013-01-07 17:03:21 -0500 | [diff] [blame] | 3195 | goto out; |
Josef Bacik | c69b26b | 2013-06-03 16:51:23 -0400 | [diff] [blame] | 3196 | } |
Josef Bacik | f3fe820 | 2013-01-07 17:03:21 -0500 | [diff] [blame] | 3197 | |
Josef Bacik | 66b4ffd | 2011-01-31 16:22:42 -0500 | [diff] [blame] | 3198 | ret = btrfs_truncate(inode); |
Josef Bacik | 4a7d0f6 | 2013-02-07 16:27:28 -0500 | [diff] [blame] | 3199 | if (ret) |
| 3200 | btrfs_orphan_del(NULL, inode); |
Josef Bacik | 7b12876 | 2008-07-24 12:17:14 -0400 | [diff] [blame] | 3201 | } else { |
| 3202 | nr_unlink++; |
| 3203 | } |
| 3204 | |
| 3205 | /* this will do delete_inode and everything for us */ |
| 3206 | iput(inode); |
Josef Bacik | 66b4ffd | 2011-01-31 16:22:42 -0500 | [diff] [blame] | 3207 | if (ret) |
| 3208 | goto out; |
Josef Bacik | 7b12876 | 2008-07-24 12:17:14 -0400 | [diff] [blame] | 3209 | } |
Miao Xie | 3254c87 | 2011-11-10 20:45:05 -0500 | [diff] [blame] | 3210 | /* release the path since we're done with it */ |
| 3211 | btrfs_release_path(path); |
| 3212 | |
Yan, Zheng | d68fc57 | 2010-05-16 10:49:58 -0400 | [diff] [blame] | 3213 | root->orphan_cleanup_state = ORPHAN_CLEANUP_DONE; |
| 3214 | |
| 3215 | if (root->orphan_block_rsv) |
| 3216 | btrfs_block_rsv_release(root, root->orphan_block_rsv, |
| 3217 | (u64)-1); |
| 3218 | |
| 3219 | if (root->orphan_block_rsv || root->orphan_item_inserted) { |
Josef Bacik | 7a7eaa4 | 2011-04-13 12:54:33 -0400 | [diff] [blame] | 3220 | trans = btrfs_join_transaction(root); |
Josef Bacik | 66b4ffd | 2011-01-31 16:22:42 -0500 | [diff] [blame] | 3221 | if (!IS_ERR(trans)) |
| 3222 | btrfs_end_transaction(trans, root); |
Yan, Zheng | d68fc57 | 2010-05-16 10:49:58 -0400 | [diff] [blame] | 3223 | } |
Josef Bacik | 7b12876 | 2008-07-24 12:17:14 -0400 | [diff] [blame] | 3224 | |
| 3225 | if (nr_unlink) |
David Sterba | 4884b47 | 2013-03-20 13:31:27 +0000 | [diff] [blame] | 3226 | btrfs_debug(root->fs_info, "unlinked %d orphans", nr_unlink); |
Josef Bacik | 7b12876 | 2008-07-24 12:17:14 -0400 | [diff] [blame] | 3227 | if (nr_truncate) |
David Sterba | 4884b47 | 2013-03-20 13:31:27 +0000 | [diff] [blame] | 3228 | btrfs_debug(root->fs_info, "truncated %d orphans", nr_truncate); |
Josef Bacik | 66b4ffd | 2011-01-31 16:22:42 -0500 | [diff] [blame] | 3229 | |
| 3230 | out: |
| 3231 | if (ret) |
Simon Kirby | c2cf52e | 2013-03-19 22:41:23 +0000 | [diff] [blame] | 3232 | btrfs_crit(root->fs_info, |
| 3233 | "could not do orphan cleanup %d", ret); |
Josef Bacik | 66b4ffd | 2011-01-31 16:22:42 -0500 | [diff] [blame] | 3234 | btrfs_free_path(path); |
| 3235 | return ret; |
Josef Bacik | 7b12876 | 2008-07-24 12:17:14 -0400 | [diff] [blame] | 3236 | } |
| 3237 | |
Chris Mason | d352ac6 | 2008-09-29 15:18:18 -0400 | [diff] [blame] | 3238 | /* |
Chris Mason | 46a53cc | 2009-04-27 11:47:50 -0400 | [diff] [blame] | 3239 | * very simple check to peek ahead in the leaf looking for xattrs. If we |
| 3240 | * don't find any xattrs, we know there can't be any acls. |
| 3241 | * |
| 3242 | * slot is the slot the inode is in, objectid is the objectid of the inode |
| 3243 | */ |
| 3244 | static noinline int acls_after_inode_item(struct extent_buffer *leaf, |
| 3245 | int slot, u64 objectid) |
| 3246 | { |
| 3247 | u32 nritems = btrfs_header_nritems(leaf); |
| 3248 | struct btrfs_key found_key; |
Josef Bacik | f23b5a5 | 2013-06-19 10:16:26 -0400 | [diff] [blame] | 3249 | static u64 xattr_access = 0; |
| 3250 | static u64 xattr_default = 0; |
Chris Mason | 46a53cc | 2009-04-27 11:47:50 -0400 | [diff] [blame] | 3251 | int scanned = 0; |
| 3252 | |
Josef Bacik | f23b5a5 | 2013-06-19 10:16:26 -0400 | [diff] [blame] | 3253 | if (!xattr_access) { |
| 3254 | xattr_access = btrfs_name_hash(POSIX_ACL_XATTR_ACCESS, |
| 3255 | strlen(POSIX_ACL_XATTR_ACCESS)); |
| 3256 | xattr_default = btrfs_name_hash(POSIX_ACL_XATTR_DEFAULT, |
| 3257 | strlen(POSIX_ACL_XATTR_DEFAULT)); |
| 3258 | } |
| 3259 | |
Chris Mason | 46a53cc | 2009-04-27 11:47:50 -0400 | [diff] [blame] | 3260 | slot++; |
| 3261 | while (slot < nritems) { |
| 3262 | btrfs_item_key_to_cpu(leaf, &found_key, slot); |
| 3263 | |
| 3264 | /* we found a different objectid, there must not be acls */ |
| 3265 | if (found_key.objectid != objectid) |
| 3266 | return 0; |
| 3267 | |
| 3268 | /* we found an xattr, assume we've got an acl */ |
Josef Bacik | f23b5a5 | 2013-06-19 10:16:26 -0400 | [diff] [blame] | 3269 | if (found_key.type == BTRFS_XATTR_ITEM_KEY) { |
| 3270 | if (found_key.offset == xattr_access || |
| 3271 | found_key.offset == xattr_default) |
| 3272 | return 1; |
| 3273 | } |
Chris Mason | 46a53cc | 2009-04-27 11:47:50 -0400 | [diff] [blame] | 3274 | |
| 3275 | /* |
| 3276 | * we found a key greater than an xattr key, there can't |
| 3277 | * be any acls later on |
| 3278 | */ |
| 3279 | if (found_key.type > BTRFS_XATTR_ITEM_KEY) |
| 3280 | return 0; |
| 3281 | |
| 3282 | slot++; |
| 3283 | scanned++; |
| 3284 | |
| 3285 | /* |
| 3286 | * it goes inode, inode backrefs, xattrs, extents, |
| 3287 | * so if there are a ton of hard links to an inode there can |
| 3288 | * be a lot of backrefs. Don't waste time searching too hard, |
| 3289 | * this is just an optimization |
| 3290 | */ |
| 3291 | if (scanned >= 8) |
| 3292 | break; |
| 3293 | } |
| 3294 | /* we hit the end of the leaf before we found an xattr or |
| 3295 | * something larger than an xattr. We have to assume the inode |
| 3296 | * has acls |
| 3297 | */ |
| 3298 | return 1; |
| 3299 | } |
| 3300 | |
| 3301 | /* |
Chris Mason | d352ac6 | 2008-09-29 15:18:18 -0400 | [diff] [blame] | 3302 | * read an inode from the btree into the in-memory inode |
| 3303 | */ |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 3304 | static void btrfs_read_locked_inode(struct inode *inode) |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3305 | { |
| 3306 | struct btrfs_path *path; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 3307 | struct extent_buffer *leaf; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3308 | struct btrfs_inode_item *inode_item; |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 3309 | struct btrfs_timespec *tspec; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3310 | struct btrfs_root *root = BTRFS_I(inode)->root; |
| 3311 | struct btrfs_key location; |
Miao Xie | 67de117 | 2013-12-26 13:07:06 +0800 | [diff] [blame^] | 3312 | unsigned long ptr; |
Chris Mason | 46a53cc | 2009-04-27 11:47:50 -0400 | [diff] [blame] | 3313 | int maybe_acls; |
Josef Bacik | 618e21d | 2007-07-11 10:18:17 -0400 | [diff] [blame] | 3314 | u32 rdev; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3315 | int ret; |
Miao Xie | 2f7e33d | 2011-06-23 07:27:13 +0000 | [diff] [blame] | 3316 | bool filled = false; |
| 3317 | |
| 3318 | ret = btrfs_fill_inode(inode, &rdev); |
| 3319 | if (!ret) |
| 3320 | filled = true; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3321 | |
| 3322 | path = btrfs_alloc_path(); |
Mark Fasheh | 1748f84 | 2011-07-12 11:25:31 -0700 | [diff] [blame] | 3323 | if (!path) |
| 3324 | goto make_bad; |
| 3325 | |
Josef Bacik | d90c732 | 2011-05-17 09:50:54 -0400 | [diff] [blame] | 3326 | path->leave_spinning = 1; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3327 | memcpy(&location, &BTRFS_I(inode)->location, sizeof(location)); |
Chris Mason | dc17ff8 | 2008-01-08 15:46:30 -0500 | [diff] [blame] | 3328 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3329 | ret = btrfs_lookup_inode(NULL, root, path, &location, 0); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 3330 | if (ret) |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3331 | goto make_bad; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3332 | |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 3333 | leaf = path->nodes[0]; |
Miao Xie | 2f7e33d | 2011-06-23 07:27:13 +0000 | [diff] [blame] | 3334 | |
| 3335 | if (filled) |
Miao Xie | 67de117 | 2013-12-26 13:07:06 +0800 | [diff] [blame^] | 3336 | goto cache_index; |
Miao Xie | 2f7e33d | 2011-06-23 07:27:13 +0000 | [diff] [blame] | 3337 | |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 3338 | inode_item = btrfs_item_ptr(leaf, path->slots[0], |
| 3339 | struct btrfs_inode_item); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 3340 | inode->i_mode = btrfs_inode_mode(leaf, inode_item); |
Miklos Szeredi | bfe8684 | 2011-10-28 14:13:29 +0200 | [diff] [blame] | 3341 | set_nlink(inode, btrfs_inode_nlink(leaf, inode_item)); |
Eric W. Biederman | 2f2f43d | 2012-02-10 11:05:07 -0800 | [diff] [blame] | 3342 | i_uid_write(inode, btrfs_inode_uid(leaf, inode_item)); |
| 3343 | i_gid_write(inode, btrfs_inode_gid(leaf, inode_item)); |
Chris Mason | dbe674a | 2008-07-17 12:54:05 -0400 | [diff] [blame] | 3344 | btrfs_i_size_write(inode, btrfs_inode_size(leaf, inode_item)); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 3345 | |
| 3346 | tspec = btrfs_inode_atime(inode_item); |
| 3347 | inode->i_atime.tv_sec = btrfs_timespec_sec(leaf, tspec); |
| 3348 | inode->i_atime.tv_nsec = btrfs_timespec_nsec(leaf, tspec); |
| 3349 | |
| 3350 | tspec = btrfs_inode_mtime(inode_item); |
| 3351 | inode->i_mtime.tv_sec = btrfs_timespec_sec(leaf, tspec); |
| 3352 | inode->i_mtime.tv_nsec = btrfs_timespec_nsec(leaf, tspec); |
| 3353 | |
| 3354 | tspec = btrfs_inode_ctime(inode_item); |
| 3355 | inode->i_ctime.tv_sec = btrfs_timespec_sec(leaf, tspec); |
| 3356 | inode->i_ctime.tv_nsec = btrfs_timespec_nsec(leaf, tspec); |
| 3357 | |
Yan Zheng | a76a3cd | 2008-10-09 11:46:29 -0400 | [diff] [blame] | 3358 | inode_set_bytes(inode, btrfs_inode_nbytes(leaf, inode_item)); |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 3359 | BTRFS_I(inode)->generation = btrfs_inode_generation(leaf, inode_item); |
Josef Bacik | 5dc562c | 2012-08-17 13:14:17 -0400 | [diff] [blame] | 3360 | BTRFS_I(inode)->last_trans = btrfs_inode_transid(leaf, inode_item); |
| 3361 | |
| 3362 | /* |
| 3363 | * If we were modified in the current generation and evicted from memory |
| 3364 | * and then re-read we need to do a full sync since we don't have any |
| 3365 | * idea about which extents were modified before we were evicted from |
| 3366 | * cache. |
| 3367 | */ |
| 3368 | if (BTRFS_I(inode)->last_trans == root->fs_info->generation) |
| 3369 | set_bit(BTRFS_INODE_NEEDS_FULL_SYNC, |
| 3370 | &BTRFS_I(inode)->runtime_flags); |
| 3371 | |
Josef Bacik | 0c4d2d9 | 2012-04-05 15:03:02 -0400 | [diff] [blame] | 3372 | inode->i_version = btrfs_inode_sequence(leaf, inode_item); |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 3373 | inode->i_generation = BTRFS_I(inode)->generation; |
Josef Bacik | 618e21d | 2007-07-11 10:18:17 -0400 | [diff] [blame] | 3374 | inode->i_rdev = 0; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 3375 | rdev = btrfs_inode_rdev(leaf, inode_item); |
| 3376 | |
Josef Bacik | aec7477 | 2008-07-24 12:12:38 -0400 | [diff] [blame] | 3377 | BTRFS_I(inode)->index_cnt = (u64)-1; |
Yan Zheng | d2fb343 | 2008-12-11 16:30:39 -0500 | [diff] [blame] | 3378 | BTRFS_I(inode)->flags = btrfs_inode_flags(leaf, inode_item); |
Miao Xie | 67de117 | 2013-12-26 13:07:06 +0800 | [diff] [blame^] | 3379 | |
| 3380 | cache_index: |
| 3381 | path->slots[0]++; |
| 3382 | if (inode->i_nlink != 1 || |
| 3383 | path->slots[0] >= btrfs_header_nritems(leaf)) |
| 3384 | goto cache_acl; |
| 3385 | |
| 3386 | btrfs_item_key_to_cpu(leaf, &location, path->slots[0]); |
| 3387 | if (location.objectid != btrfs_ino(inode)) |
| 3388 | goto cache_acl; |
| 3389 | |
| 3390 | ptr = btrfs_item_ptr_offset(leaf, path->slots[0]); |
| 3391 | if (location.type == BTRFS_INODE_REF_KEY) { |
| 3392 | struct btrfs_inode_ref *ref; |
| 3393 | |
| 3394 | ref = (struct btrfs_inode_ref *)ptr; |
| 3395 | BTRFS_I(inode)->dir_index = btrfs_inode_ref_index(leaf, ref); |
| 3396 | } else if (location.type == BTRFS_INODE_EXTREF_KEY) { |
| 3397 | struct btrfs_inode_extref *extref; |
| 3398 | |
| 3399 | extref = (struct btrfs_inode_extref *)ptr; |
| 3400 | BTRFS_I(inode)->dir_index = btrfs_inode_extref_index(leaf, |
| 3401 | extref); |
| 3402 | } |
Miao Xie | 2f7e33d | 2011-06-23 07:27:13 +0000 | [diff] [blame] | 3403 | cache_acl: |
Chris Mason | 46a53cc | 2009-04-27 11:47:50 -0400 | [diff] [blame] | 3404 | /* |
| 3405 | * try to precache a NULL acl entry for files that don't have |
| 3406 | * any xattrs or acls |
| 3407 | */ |
Li Zefan | 33345d01 | 2011-04-20 10:31:50 +0800 | [diff] [blame] | 3408 | maybe_acls = acls_after_inode_item(leaf, path->slots[0], |
| 3409 | btrfs_ino(inode)); |
Al Viro | 72c0490 | 2009-06-24 16:58:48 -0400 | [diff] [blame] | 3410 | if (!maybe_acls) |
| 3411 | cache_no_acl(inode); |
Chris Mason | 46a53cc | 2009-04-27 11:47:50 -0400 | [diff] [blame] | 3412 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3413 | btrfs_free_path(path); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3414 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3415 | switch (inode->i_mode & S_IFMT) { |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3416 | case S_IFREG: |
| 3417 | inode->i_mapping->a_ops = &btrfs_aops; |
Chris Mason | 0416008 | 2008-03-26 10:28:07 -0400 | [diff] [blame] | 3418 | inode->i_mapping->backing_dev_info = &root->fs_info->bdi; |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 3419 | BTRFS_I(inode)->io_tree.ops = &btrfs_extent_io_ops; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3420 | inode->i_fop = &btrfs_file_operations; |
| 3421 | inode->i_op = &btrfs_file_inode_operations; |
| 3422 | break; |
| 3423 | case S_IFDIR: |
| 3424 | inode->i_fop = &btrfs_dir_file_operations; |
| 3425 | if (root == root->fs_info->tree_root) |
| 3426 | inode->i_op = &btrfs_dir_ro_inode_operations; |
| 3427 | else |
| 3428 | inode->i_op = &btrfs_dir_inode_operations; |
| 3429 | break; |
| 3430 | case S_IFLNK: |
| 3431 | inode->i_op = &btrfs_symlink_inode_operations; |
| 3432 | inode->i_mapping->a_ops = &btrfs_symlink_aops; |
Chris Mason | 0416008 | 2008-03-26 10:28:07 -0400 | [diff] [blame] | 3433 | inode->i_mapping->backing_dev_info = &root->fs_info->bdi; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3434 | break; |
Josef Bacik | 618e21d | 2007-07-11 10:18:17 -0400 | [diff] [blame] | 3435 | default: |
Jim Owens | 0279b4c | 2009-02-04 09:29:13 -0500 | [diff] [blame] | 3436 | inode->i_op = &btrfs_special_inode_operations; |
Josef Bacik | 618e21d | 2007-07-11 10:18:17 -0400 | [diff] [blame] | 3437 | init_special_inode(inode, inode->i_mode, rdev); |
| 3438 | break; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3439 | } |
Christoph Hellwig | 6cbff00 | 2009-04-17 10:37:41 +0200 | [diff] [blame] | 3440 | |
| 3441 | btrfs_update_iflags(inode); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3442 | return; |
| 3443 | |
| 3444 | make_bad: |
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 | make_bad_inode(inode); |
| 3447 | } |
| 3448 | |
Chris Mason | d352ac6 | 2008-09-29 15:18:18 -0400 | [diff] [blame] | 3449 | /* |
| 3450 | * given a leaf and an inode, copy the inode fields into the leaf |
| 3451 | */ |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 3452 | static void fill_inode_item(struct btrfs_trans_handle *trans, |
| 3453 | struct extent_buffer *leaf, |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 3454 | struct btrfs_inode_item *item, |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3455 | struct inode *inode) |
| 3456 | { |
Liu Bo | 51fab69 | 2012-12-27 09:01:21 +0000 | [diff] [blame] | 3457 | struct btrfs_map_token token; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 3458 | |
Liu Bo | 51fab69 | 2012-12-27 09:01:21 +0000 | [diff] [blame] | 3459 | btrfs_init_map_token(&token); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 3460 | |
Liu Bo | 51fab69 | 2012-12-27 09:01:21 +0000 | [diff] [blame] | 3461 | btrfs_set_token_inode_uid(leaf, item, i_uid_read(inode), &token); |
| 3462 | btrfs_set_token_inode_gid(leaf, item, i_gid_read(inode), &token); |
| 3463 | btrfs_set_token_inode_size(leaf, item, BTRFS_I(inode)->disk_i_size, |
| 3464 | &token); |
| 3465 | btrfs_set_token_inode_mode(leaf, item, inode->i_mode, &token); |
| 3466 | btrfs_set_token_inode_nlink(leaf, item, inode->i_nlink, &token); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 3467 | |
Liu Bo | 51fab69 | 2012-12-27 09:01:21 +0000 | [diff] [blame] | 3468 | btrfs_set_token_timespec_sec(leaf, btrfs_inode_atime(item), |
| 3469 | inode->i_atime.tv_sec, &token); |
| 3470 | btrfs_set_token_timespec_nsec(leaf, btrfs_inode_atime(item), |
| 3471 | inode->i_atime.tv_nsec, &token); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 3472 | |
Liu Bo | 51fab69 | 2012-12-27 09:01:21 +0000 | [diff] [blame] | 3473 | btrfs_set_token_timespec_sec(leaf, btrfs_inode_mtime(item), |
| 3474 | inode->i_mtime.tv_sec, &token); |
| 3475 | btrfs_set_token_timespec_nsec(leaf, btrfs_inode_mtime(item), |
| 3476 | inode->i_mtime.tv_nsec, &token); |
| 3477 | |
| 3478 | btrfs_set_token_timespec_sec(leaf, btrfs_inode_ctime(item), |
| 3479 | inode->i_ctime.tv_sec, &token); |
| 3480 | btrfs_set_token_timespec_nsec(leaf, btrfs_inode_ctime(item), |
| 3481 | inode->i_ctime.tv_nsec, &token); |
| 3482 | |
| 3483 | btrfs_set_token_inode_nbytes(leaf, item, inode_get_bytes(inode), |
| 3484 | &token); |
| 3485 | btrfs_set_token_inode_generation(leaf, item, BTRFS_I(inode)->generation, |
| 3486 | &token); |
| 3487 | btrfs_set_token_inode_sequence(leaf, item, inode->i_version, &token); |
| 3488 | btrfs_set_token_inode_transid(leaf, item, trans->transid, &token); |
| 3489 | btrfs_set_token_inode_rdev(leaf, item, inode->i_rdev, &token); |
| 3490 | btrfs_set_token_inode_flags(leaf, item, BTRFS_I(inode)->flags, &token); |
| 3491 | btrfs_set_token_inode_block_group(leaf, item, 0, &token); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3492 | } |
| 3493 | |
Chris Mason | d352ac6 | 2008-09-29 15:18:18 -0400 | [diff] [blame] | 3494 | /* |
| 3495 | * copy everything in the in-memory inode into the btree. |
| 3496 | */ |
Chris Mason | 2115133 | 2011-11-10 20:39:08 -0500 | [diff] [blame] | 3497 | static noinline int btrfs_update_inode_item(struct btrfs_trans_handle *trans, |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 3498 | struct btrfs_root *root, struct inode *inode) |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3499 | { |
| 3500 | struct btrfs_inode_item *inode_item; |
| 3501 | struct btrfs_path *path; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 3502 | struct extent_buffer *leaf; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3503 | int ret; |
| 3504 | |
| 3505 | path = btrfs_alloc_path(); |
Miao Xie | 16cdcec | 2011-04-22 18:12:22 +0800 | [diff] [blame] | 3506 | if (!path) |
| 3507 | return -ENOMEM; |
| 3508 | |
Chris Mason | b947343 | 2009-03-13 11:00:37 -0400 | [diff] [blame] | 3509 | path->leave_spinning = 1; |
Miao Xie | 16cdcec | 2011-04-22 18:12:22 +0800 | [diff] [blame] | 3510 | ret = btrfs_lookup_inode(trans, root, path, &BTRFS_I(inode)->location, |
| 3511 | 1); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3512 | if (ret) { |
| 3513 | if (ret > 0) |
| 3514 | ret = -ENOENT; |
| 3515 | goto failed; |
| 3516 | } |
| 3517 | |
Chris Mason | b4ce94d | 2009-02-04 09:25:08 -0500 | [diff] [blame] | 3518 | btrfs_unlock_up_safe(path, 1); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 3519 | leaf = path->nodes[0]; |
| 3520 | inode_item = btrfs_item_ptr(leaf, path->slots[0], |
Miao Xie | 16cdcec | 2011-04-22 18:12:22 +0800 | [diff] [blame] | 3521 | struct btrfs_inode_item); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3522 | |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 3523 | fill_inode_item(trans, leaf, inode_item, inode); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 3524 | btrfs_mark_buffer_dirty(leaf); |
Josef Bacik | 15ee9bc | 2007-08-10 16:22:09 -0400 | [diff] [blame] | 3525 | btrfs_set_inode_last_trans(trans, inode); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3526 | ret = 0; |
| 3527 | failed: |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3528 | btrfs_free_path(path); |
| 3529 | return ret; |
| 3530 | } |
| 3531 | |
Chris Mason | d352ac6 | 2008-09-29 15:18:18 -0400 | [diff] [blame] | 3532 | /* |
Chris Mason | 2115133 | 2011-11-10 20:39:08 -0500 | [diff] [blame] | 3533 | * copy everything in the in-memory inode into the btree. |
| 3534 | */ |
| 3535 | noinline int btrfs_update_inode(struct btrfs_trans_handle *trans, |
| 3536 | struct btrfs_root *root, struct inode *inode) |
| 3537 | { |
| 3538 | int ret; |
| 3539 | |
| 3540 | /* |
| 3541 | * If the inode is a free space inode, we can deadlock during commit |
| 3542 | * if we put it into the delayed code. |
| 3543 | * |
| 3544 | * The data relocation inode should also be directly updated |
| 3545 | * without delay |
| 3546 | */ |
Liu Bo | 83eea1f | 2012-07-10 05:28:39 -0600 | [diff] [blame] | 3547 | if (!btrfs_is_free_space_inode(inode) |
Chris Mason | 2115133 | 2011-11-10 20:39:08 -0500 | [diff] [blame] | 3548 | && root->root_key.objectid != BTRFS_DATA_RELOC_TREE_OBJECTID) { |
Alexander Block | 8ea05e3 | 2012-07-25 17:35:53 +0200 | [diff] [blame] | 3549 | btrfs_update_root_times(trans, root); |
| 3550 | |
Chris Mason | 2115133 | 2011-11-10 20:39:08 -0500 | [diff] [blame] | 3551 | ret = btrfs_delayed_update_inode(trans, root, inode); |
| 3552 | if (!ret) |
| 3553 | btrfs_set_inode_last_trans(trans, inode); |
| 3554 | return ret; |
| 3555 | } |
| 3556 | |
| 3557 | return btrfs_update_inode_item(trans, root, inode); |
| 3558 | } |
| 3559 | |
Josef Bacik | be6aef6 | 2012-10-22 15:43:12 -0400 | [diff] [blame] | 3560 | noinline int btrfs_update_inode_fallback(struct btrfs_trans_handle *trans, |
| 3561 | struct btrfs_root *root, |
| 3562 | struct inode *inode) |
Chris Mason | 2115133 | 2011-11-10 20:39:08 -0500 | [diff] [blame] | 3563 | { |
| 3564 | int ret; |
| 3565 | |
| 3566 | ret = btrfs_update_inode(trans, root, inode); |
| 3567 | if (ret == -ENOSPC) |
| 3568 | return btrfs_update_inode_item(trans, root, inode); |
| 3569 | return ret; |
| 3570 | } |
| 3571 | |
| 3572 | /* |
Chris Mason | d352ac6 | 2008-09-29 15:18:18 -0400 | [diff] [blame] | 3573 | * unlink helper that gets used here in inode.c and in the tree logging |
| 3574 | * recovery code. It remove a link in a directory with a given name, and |
| 3575 | * also drops the back refs in the inode to the directory |
| 3576 | */ |
Al Viro | 9298679 | 2011-03-04 17:14:37 +0000 | [diff] [blame] | 3577 | static int __btrfs_unlink_inode(struct btrfs_trans_handle *trans, |
| 3578 | struct btrfs_root *root, |
| 3579 | struct inode *dir, struct inode *inode, |
| 3580 | const char *name, int name_len) |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3581 | { |
| 3582 | struct btrfs_path *path; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3583 | int ret = 0; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 3584 | struct extent_buffer *leaf; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3585 | struct btrfs_dir_item *di; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 3586 | struct btrfs_key key; |
Josef Bacik | aec7477 | 2008-07-24 12:12:38 -0400 | [diff] [blame] | 3587 | u64 index; |
Li Zefan | 33345d01 | 2011-04-20 10:31:50 +0800 | [diff] [blame] | 3588 | u64 ino = btrfs_ino(inode); |
| 3589 | u64 dir_ino = btrfs_ino(dir); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3590 | |
| 3591 | path = btrfs_alloc_path(); |
Chris Mason | 54aa1f4 | 2007-06-22 14:16:25 -0400 | [diff] [blame] | 3592 | if (!path) { |
| 3593 | ret = -ENOMEM; |
Tsutomu Itoh | 554233a | 2011-02-03 03:16:25 +0000 | [diff] [blame] | 3594 | goto out; |
Chris Mason | 54aa1f4 | 2007-06-22 14:16:25 -0400 | [diff] [blame] | 3595 | } |
| 3596 | |
Chris Mason | b947343 | 2009-03-13 11:00:37 -0400 | [diff] [blame] | 3597 | path->leave_spinning = 1; |
Li Zefan | 33345d01 | 2011-04-20 10:31:50 +0800 | [diff] [blame] | 3598 | di = btrfs_lookup_dir_item(trans, root, path, dir_ino, |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3599 | name, name_len, -1); |
| 3600 | if (IS_ERR(di)) { |
| 3601 | ret = PTR_ERR(di); |
| 3602 | goto err; |
| 3603 | } |
| 3604 | if (!di) { |
| 3605 | ret = -ENOENT; |
| 3606 | goto err; |
| 3607 | } |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 3608 | leaf = path->nodes[0]; |
| 3609 | btrfs_dir_item_key_to_cpu(leaf, di, &key); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3610 | ret = btrfs_delete_one_dir_name(trans, root, path, di); |
Chris Mason | 54aa1f4 | 2007-06-22 14:16:25 -0400 | [diff] [blame] | 3611 | if (ret) |
| 3612 | goto err; |
David Sterba | b3b4aa7 | 2011-04-21 01:20:15 +0200 | [diff] [blame] | 3613 | btrfs_release_path(path); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3614 | |
Miao Xie | 67de117 | 2013-12-26 13:07:06 +0800 | [diff] [blame^] | 3615 | /* |
| 3616 | * If we don't have dir index, we have to get it by looking up |
| 3617 | * the inode ref, since we get the inode ref, remove it directly, |
| 3618 | * it is unnecessary to do delayed deletion. |
| 3619 | * |
| 3620 | * But if we have dir index, needn't search inode ref to get it. |
| 3621 | * Since the inode ref is close to the inode item, it is better |
| 3622 | * that we delay to delete it, and just do this deletion when |
| 3623 | * we update the inode item. |
| 3624 | */ |
| 3625 | if (BTRFS_I(inode)->dir_index) { |
| 3626 | ret = btrfs_delayed_delete_inode_ref(inode); |
| 3627 | if (!ret) { |
| 3628 | index = BTRFS_I(inode)->dir_index; |
| 3629 | goto skip_backref; |
| 3630 | } |
| 3631 | } |
| 3632 | |
Li Zefan | 33345d01 | 2011-04-20 10:31:50 +0800 | [diff] [blame] | 3633 | ret = btrfs_del_inode_ref(trans, root, name, name_len, ino, |
| 3634 | dir_ino, &index); |
Josef Bacik | aec7477 | 2008-07-24 12:12:38 -0400 | [diff] [blame] | 3635 | if (ret) { |
Simon Kirby | c2cf52e | 2013-03-19 22:41:23 +0000 | [diff] [blame] | 3636 | btrfs_info(root->fs_info, |
| 3637 | "failed to delete reference to %.*s, inode %llu parent %llu", |
Geert Uytterhoeven | c1c9ff7 | 2013-08-20 13:20:07 +0200 | [diff] [blame] | 3638 | name_len, name, ino, dir_ino); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 3639 | btrfs_abort_transaction(trans, root, ret); |
Josef Bacik | aec7477 | 2008-07-24 12:12:38 -0400 | [diff] [blame] | 3640 | goto err; |
| 3641 | } |
Miao Xie | 67de117 | 2013-12-26 13:07:06 +0800 | [diff] [blame^] | 3642 | skip_backref: |
Miao Xie | 16cdcec | 2011-04-22 18:12:22 +0800 | [diff] [blame] | 3643 | ret = btrfs_delete_delayed_dir_index(trans, root, dir, index); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 3644 | if (ret) { |
| 3645 | btrfs_abort_transaction(trans, root, ret); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3646 | goto err; |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 3647 | } |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3648 | |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 3649 | ret = btrfs_del_inode_ref_in_log(trans, root, name, name_len, |
Li Zefan | 33345d01 | 2011-04-20 10:31:50 +0800 | [diff] [blame] | 3650 | inode, dir_ino); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 3651 | if (ret != 0 && ret != -ENOENT) { |
| 3652 | btrfs_abort_transaction(trans, root, ret); |
| 3653 | goto err; |
| 3654 | } |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 3655 | |
| 3656 | ret = btrfs_del_dir_entries_in_log(trans, root, name, name_len, |
| 3657 | dir, index); |
Chris Mason | 6418c96 | 2010-10-30 07:34:24 -0400 | [diff] [blame] | 3658 | if (ret == -ENOENT) |
| 3659 | ret = 0; |
Zach Brown | d4e3991 | 2013-04-02 21:02:16 +0000 | [diff] [blame] | 3660 | else if (ret) |
| 3661 | btrfs_abort_transaction(trans, root, ret); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3662 | err: |
| 3663 | btrfs_free_path(path); |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 3664 | if (ret) |
| 3665 | goto out; |
| 3666 | |
| 3667 | btrfs_i_size_write(dir, dir->i_size - name_len * 2); |
Josef Bacik | 0c4d2d9 | 2012-04-05 15:03:02 -0400 | [diff] [blame] | 3668 | inode_inc_iversion(inode); |
| 3669 | inode_inc_iversion(dir); |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 3670 | inode->i_ctime = dir->i_mtime = dir->i_ctime = CURRENT_TIME; |
Tsutomu Itoh | b995929 | 2012-06-25 21:25:22 -0600 | [diff] [blame] | 3671 | ret = btrfs_update_inode(trans, root, dir); |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 3672 | out: |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3673 | return ret; |
| 3674 | } |
| 3675 | |
Al Viro | 9298679 | 2011-03-04 17:14:37 +0000 | [diff] [blame] | 3676 | int btrfs_unlink_inode(struct btrfs_trans_handle *trans, |
| 3677 | struct btrfs_root *root, |
| 3678 | struct inode *dir, struct inode *inode, |
| 3679 | const char *name, int name_len) |
| 3680 | { |
| 3681 | int ret; |
| 3682 | ret = __btrfs_unlink_inode(trans, root, dir, inode, name, name_len); |
| 3683 | if (!ret) { |
Zach Brown | 8b558c5 | 2013-10-16 12:10:34 -0700 | [diff] [blame] | 3684 | drop_nlink(inode); |
Al Viro | 9298679 | 2011-03-04 17:14:37 +0000 | [diff] [blame] | 3685 | ret = btrfs_update_inode(trans, root, inode); |
| 3686 | } |
| 3687 | return ret; |
| 3688 | } |
Yan, Zheng | a22285a | 2010-05-16 10:48:46 -0400 | [diff] [blame] | 3689 | |
| 3690 | /* |
| 3691 | * helper to start transaction for unlink and rmdir. |
| 3692 | * |
Josef Bacik | d52be81 | 2013-05-29 14:54:47 -0400 | [diff] [blame] | 3693 | * unlink and rmdir are special in btrfs, they do not always free space, so |
| 3694 | * if we cannot make our reservations the normal way try and see if there is |
| 3695 | * plenty of slack room in the global reserve to migrate, otherwise we cannot |
| 3696 | * allow the unlink to occur. |
Yan, Zheng | a22285a | 2010-05-16 10:48:46 -0400 | [diff] [blame] | 3697 | */ |
Josef Bacik | d52be81 | 2013-05-29 14:54:47 -0400 | [diff] [blame] | 3698 | static struct btrfs_trans_handle *__unlink_start_trans(struct inode *dir) |
Yan, Zheng | a22285a | 2010-05-16 10:48:46 -0400 | [diff] [blame] | 3699 | { |
| 3700 | struct btrfs_trans_handle *trans; |
| 3701 | struct btrfs_root *root = BTRFS_I(dir)->root; |
Yan, Zheng | a22285a | 2010-05-16 10:48:46 -0400 | [diff] [blame] | 3702 | int ret; |
| 3703 | |
Josef Bacik | e70bea5 | 2011-10-11 14:18:24 -0400 | [diff] [blame] | 3704 | /* |
| 3705 | * 1 for the possible orphan item |
| 3706 | * 1 for the dir item |
| 3707 | * 1 for the dir index |
| 3708 | * 1 for the inode ref |
Josef Bacik | e70bea5 | 2011-10-11 14:18:24 -0400 | [diff] [blame] | 3709 | * 1 for the inode |
| 3710 | */ |
Josef Bacik | 6e137ed | 2013-03-26 15:26:55 -0400 | [diff] [blame] | 3711 | trans = btrfs_start_transaction(root, 5); |
Yan, Zheng | a22285a | 2010-05-16 10:48:46 -0400 | [diff] [blame] | 3712 | if (!IS_ERR(trans) || PTR_ERR(trans) != -ENOSPC) |
| 3713 | return trans; |
| 3714 | |
Josef Bacik | d52be81 | 2013-05-29 14:54:47 -0400 | [diff] [blame] | 3715 | if (PTR_ERR(trans) == -ENOSPC) { |
| 3716 | u64 num_bytes = btrfs_calc_trans_metadata_size(root, 5); |
Yan, Zheng | a22285a | 2010-05-16 10:48:46 -0400 | [diff] [blame] | 3717 | |
Josef Bacik | d52be81 | 2013-05-29 14:54:47 -0400 | [diff] [blame] | 3718 | trans = btrfs_start_transaction(root, 0); |
| 3719 | if (IS_ERR(trans)) |
| 3720 | return trans; |
| 3721 | ret = btrfs_cond_migrate_bytes(root->fs_info, |
| 3722 | &root->fs_info->trans_block_rsv, |
| 3723 | num_bytes, 5); |
| 3724 | if (ret) { |
| 3725 | btrfs_end_transaction(trans, root); |
| 3726 | return ERR_PTR(ret); |
Yan, Zheng | a22285a | 2010-05-16 10:48:46 -0400 | [diff] [blame] | 3727 | } |
Josef Bacik | 5a77d76 | 2011-11-01 14:32:23 -0400 | [diff] [blame] | 3728 | trans->block_rsv = &root->fs_info->trans_block_rsv; |
Josef Bacik | d52be81 | 2013-05-29 14:54:47 -0400 | [diff] [blame] | 3729 | trans->bytes_reserved = num_bytes; |
Yan, Zheng | a22285a | 2010-05-16 10:48:46 -0400 | [diff] [blame] | 3730 | } |
Josef Bacik | d52be81 | 2013-05-29 14:54:47 -0400 | [diff] [blame] | 3731 | return trans; |
Yan, Zheng | a22285a | 2010-05-16 10:48:46 -0400 | [diff] [blame] | 3732 | } |
| 3733 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3734 | static int btrfs_unlink(struct inode *dir, struct dentry *dentry) |
| 3735 | { |
Yan, Zheng | a22285a | 2010-05-16 10:48:46 -0400 | [diff] [blame] | 3736 | struct btrfs_root *root = BTRFS_I(dir)->root; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3737 | struct btrfs_trans_handle *trans; |
Josef Bacik | 7b12876 | 2008-07-24 12:17:14 -0400 | [diff] [blame] | 3738 | struct inode *inode = dentry->d_inode; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3739 | int ret; |
| 3740 | |
Josef Bacik | d52be81 | 2013-05-29 14:54:47 -0400 | [diff] [blame] | 3741 | trans = __unlink_start_trans(dir); |
Yan, Zheng | a22285a | 2010-05-16 10:48:46 -0400 | [diff] [blame] | 3742 | if (IS_ERR(trans)) |
Josef Bacik | 5df6a9f | 2009-11-10 21:23:48 -0500 | [diff] [blame] | 3743 | return PTR_ERR(trans); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 3744 | |
Chris Mason | 12fcfd2 | 2009-03-24 10:24:20 -0400 | [diff] [blame] | 3745 | btrfs_record_unlink_dir(trans, dir, dentry->d_inode, 0); |
| 3746 | |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 3747 | ret = btrfs_unlink_inode(trans, root, dir, dentry->d_inode, |
| 3748 | dentry->d_name.name, dentry->d_name.len); |
Tsutomu Itoh | b532402 | 2011-07-19 07:27:20 +0000 | [diff] [blame] | 3749 | if (ret) |
| 3750 | goto out; |
Josef Bacik | 7b12876 | 2008-07-24 12:17:14 -0400 | [diff] [blame] | 3751 | |
Yan, Zheng | a22285a | 2010-05-16 10:48:46 -0400 | [diff] [blame] | 3752 | if (inode->i_nlink == 0) { |
Josef Bacik | 7b12876 | 2008-07-24 12:17:14 -0400 | [diff] [blame] | 3753 | ret = btrfs_orphan_add(trans, inode); |
Tsutomu Itoh | b532402 | 2011-07-19 07:27:20 +0000 | [diff] [blame] | 3754 | if (ret) |
| 3755 | goto out; |
Yan, Zheng | a22285a | 2010-05-16 10:48:46 -0400 | [diff] [blame] | 3756 | } |
Josef Bacik | 7b12876 | 2008-07-24 12:17:14 -0400 | [diff] [blame] | 3757 | |
Tsutomu Itoh | b532402 | 2011-07-19 07:27:20 +0000 | [diff] [blame] | 3758 | out: |
Josef Bacik | d52be81 | 2013-05-29 14:54:47 -0400 | [diff] [blame] | 3759 | btrfs_end_transaction(trans, root); |
Liu Bo | b53d3f5 | 2012-11-14 14:34:34 +0000 | [diff] [blame] | 3760 | btrfs_btree_balance_dirty(root); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3761 | return ret; |
| 3762 | } |
| 3763 | |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 3764 | int btrfs_unlink_subvol(struct btrfs_trans_handle *trans, |
| 3765 | struct btrfs_root *root, |
| 3766 | struct inode *dir, u64 objectid, |
| 3767 | const char *name, int name_len) |
| 3768 | { |
| 3769 | struct btrfs_path *path; |
| 3770 | struct extent_buffer *leaf; |
| 3771 | struct btrfs_dir_item *di; |
| 3772 | struct btrfs_key key; |
| 3773 | u64 index; |
| 3774 | int ret; |
Li Zefan | 33345d01 | 2011-04-20 10:31:50 +0800 | [diff] [blame] | 3775 | u64 dir_ino = btrfs_ino(dir); |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 3776 | |
| 3777 | path = btrfs_alloc_path(); |
| 3778 | if (!path) |
| 3779 | return -ENOMEM; |
| 3780 | |
Li Zefan | 33345d01 | 2011-04-20 10:31:50 +0800 | [diff] [blame] | 3781 | di = btrfs_lookup_dir_item(trans, root, path, dir_ino, |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 3782 | name, name_len, -1); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 3783 | if (IS_ERR_OR_NULL(di)) { |
| 3784 | if (!di) |
| 3785 | ret = -ENOENT; |
| 3786 | else |
| 3787 | ret = PTR_ERR(di); |
| 3788 | goto out; |
| 3789 | } |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 3790 | |
| 3791 | leaf = path->nodes[0]; |
| 3792 | btrfs_dir_item_key_to_cpu(leaf, di, &key); |
| 3793 | WARN_ON(key.type != BTRFS_ROOT_ITEM_KEY || key.objectid != objectid); |
| 3794 | ret = btrfs_delete_one_dir_name(trans, root, path, di); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 3795 | if (ret) { |
| 3796 | btrfs_abort_transaction(trans, root, ret); |
| 3797 | goto out; |
| 3798 | } |
David Sterba | b3b4aa7 | 2011-04-21 01:20:15 +0200 | [diff] [blame] | 3799 | btrfs_release_path(path); |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 3800 | |
| 3801 | ret = btrfs_del_root_ref(trans, root->fs_info->tree_root, |
| 3802 | objectid, root->root_key.objectid, |
Li Zefan | 33345d01 | 2011-04-20 10:31:50 +0800 | [diff] [blame] | 3803 | dir_ino, &index, name, name_len); |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 3804 | if (ret < 0) { |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 3805 | if (ret != -ENOENT) { |
| 3806 | btrfs_abort_transaction(trans, root, ret); |
| 3807 | goto out; |
| 3808 | } |
Li Zefan | 33345d01 | 2011-04-20 10:31:50 +0800 | [diff] [blame] | 3809 | di = btrfs_search_dir_index_item(root, path, dir_ino, |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 3810 | name, name_len); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 3811 | if (IS_ERR_OR_NULL(di)) { |
| 3812 | if (!di) |
| 3813 | ret = -ENOENT; |
| 3814 | else |
| 3815 | ret = PTR_ERR(di); |
| 3816 | btrfs_abort_transaction(trans, root, ret); |
| 3817 | goto out; |
| 3818 | } |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 3819 | |
| 3820 | leaf = path->nodes[0]; |
| 3821 | btrfs_item_key_to_cpu(leaf, &key, path->slots[0]); |
David Sterba | b3b4aa7 | 2011-04-21 01:20:15 +0200 | [diff] [blame] | 3822 | btrfs_release_path(path); |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 3823 | index = key.offset; |
| 3824 | } |
David Sterba | b3b4aa7 | 2011-04-21 01:20:15 +0200 | [diff] [blame] | 3825 | btrfs_release_path(path); |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 3826 | |
Miao Xie | 16cdcec | 2011-04-22 18:12:22 +0800 | [diff] [blame] | 3827 | ret = btrfs_delete_delayed_dir_index(trans, root, dir, index); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 3828 | if (ret) { |
| 3829 | btrfs_abort_transaction(trans, root, ret); |
| 3830 | goto out; |
| 3831 | } |
Miao Xie | 16cdcec | 2011-04-22 18:12:22 +0800 | [diff] [blame] | 3832 | |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 3833 | btrfs_i_size_write(dir, dir->i_size - name_len * 2); |
Josef Bacik | 0c4d2d9 | 2012-04-05 15:03:02 -0400 | [diff] [blame] | 3834 | inode_inc_iversion(dir); |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 3835 | dir->i_mtime = dir->i_ctime = CURRENT_TIME; |
Josef Bacik | 5a24e84 | 2012-08-08 10:12:59 -0600 | [diff] [blame] | 3836 | ret = btrfs_update_inode_fallback(trans, root, dir); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 3837 | if (ret) |
| 3838 | btrfs_abort_transaction(trans, root, ret); |
| 3839 | out: |
Josef Bacik | 71d7aed | 2011-06-14 14:24:32 -0400 | [diff] [blame] | 3840 | btrfs_free_path(path); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 3841 | return ret; |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 3842 | } |
| 3843 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3844 | static int btrfs_rmdir(struct inode *dir, struct dentry *dentry) |
| 3845 | { |
| 3846 | struct inode *inode = dentry->d_inode; |
Chris Mason | 1832a6d | 2007-12-21 16:27:21 -0500 | [diff] [blame] | 3847 | int err = 0; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3848 | struct btrfs_root *root = BTRFS_I(dir)->root; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3849 | struct btrfs_trans_handle *trans; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3850 | |
David Sterba | b3ae244 | 2012-09-13 16:04:34 -0600 | [diff] [blame] | 3851 | if (inode->i_size > BTRFS_EMPTY_DIR_SIZE) |
Yan | 134d451 | 2007-10-25 15:49:25 -0400 | [diff] [blame] | 3852 | return -ENOTEMPTY; |
David Sterba | b3ae244 | 2012-09-13 16:04:34 -0600 | [diff] [blame] | 3853 | if (btrfs_ino(inode) == BTRFS_FIRST_FREE_OBJECTID) |
| 3854 | return -EPERM; |
Yan | 134d451 | 2007-10-25 15:49:25 -0400 | [diff] [blame] | 3855 | |
Josef Bacik | d52be81 | 2013-05-29 14:54:47 -0400 | [diff] [blame] | 3856 | trans = __unlink_start_trans(dir); |
Yan, Zheng | a22285a | 2010-05-16 10:48:46 -0400 | [diff] [blame] | 3857 | if (IS_ERR(trans)) |
Josef Bacik | 5df6a9f | 2009-11-10 21:23:48 -0500 | [diff] [blame] | 3858 | return PTR_ERR(trans); |
Josef Bacik | 5df6a9f | 2009-11-10 21:23:48 -0500 | [diff] [blame] | 3859 | |
Li Zefan | 33345d01 | 2011-04-20 10:31:50 +0800 | [diff] [blame] | 3860 | if (unlikely(btrfs_ino(inode) == BTRFS_EMPTY_SUBVOL_DIR_OBJECTID)) { |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 3861 | err = btrfs_unlink_subvol(trans, root, dir, |
| 3862 | BTRFS_I(inode)->location.objectid, |
| 3863 | dentry->d_name.name, |
| 3864 | dentry->d_name.len); |
| 3865 | goto out; |
| 3866 | } |
| 3867 | |
Josef Bacik | 7b12876 | 2008-07-24 12:17:14 -0400 | [diff] [blame] | 3868 | err = btrfs_orphan_add(trans, inode); |
| 3869 | if (err) |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 3870 | goto out; |
Josef Bacik | 7b12876 | 2008-07-24 12:17:14 -0400 | [diff] [blame] | 3871 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3872 | /* now the directory is empty */ |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 3873 | err = btrfs_unlink_inode(trans, root, dir, dentry->d_inode, |
| 3874 | dentry->d_name.name, dentry->d_name.len); |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 3875 | if (!err) |
Chris Mason | dbe674a | 2008-07-17 12:54:05 -0400 | [diff] [blame] | 3876 | btrfs_i_size_write(inode, 0); |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 3877 | out: |
Josef Bacik | d52be81 | 2013-05-29 14:54:47 -0400 | [diff] [blame] | 3878 | btrfs_end_transaction(trans, root); |
Liu Bo | b53d3f5 | 2012-11-14 14:34:34 +0000 | [diff] [blame] | 3879 | btrfs_btree_balance_dirty(root); |
Chris Mason | 3954401 | 2007-12-12 14:38:19 -0500 | [diff] [blame] | 3880 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3881 | return err; |
| 3882 | } |
| 3883 | |
Chris Mason | 323ac95 | 2008-10-01 19:05:46 -0400 | [diff] [blame] | 3884 | /* |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3885 | * this can truncate away extent items, csum items and directory items. |
| 3886 | * 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] | 3887 | * any higher than new_size |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3888 | * |
| 3889 | * csum items that cross the new i_size are truncated to the new size |
| 3890 | * as well. |
Josef Bacik | 7b12876 | 2008-07-24 12:17:14 -0400 | [diff] [blame] | 3891 | * |
| 3892 | * min_type is the minimum key type to truncate down to. If set to 0, this |
| 3893 | * 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] | 3894 | */ |
Yan, Zheng | 8082510 | 2009-11-12 09:35:36 +0000 | [diff] [blame] | 3895 | int btrfs_truncate_inode_items(struct btrfs_trans_handle *trans, |
| 3896 | struct btrfs_root *root, |
| 3897 | struct inode *inode, |
| 3898 | u64 new_size, u32 min_type) |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3899 | { |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3900 | struct btrfs_path *path; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 3901 | struct extent_buffer *leaf; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3902 | struct btrfs_file_extent_item *fi; |
Yan, Zheng | 8082510 | 2009-11-12 09:35:36 +0000 | [diff] [blame] | 3903 | struct btrfs_key key; |
| 3904 | struct btrfs_key found_key; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3905 | u64 extent_start = 0; |
Chris Mason | db94535 | 2007-10-15 16:15:53 -0400 | [diff] [blame] | 3906 | u64 extent_num_bytes = 0; |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 3907 | u64 extent_offset = 0; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3908 | u64 item_end = 0; |
Josef Bacik | 7f4f6e0 | 2013-08-29 16:43:28 -0400 | [diff] [blame] | 3909 | u64 last_size = (u64)-1; |
Yan, Zheng | 8082510 | 2009-11-12 09:35:36 +0000 | [diff] [blame] | 3910 | u32 found_type = (u8)-1; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3911 | int found_extent; |
| 3912 | int del_item; |
Chris Mason | 85e21ba | 2008-01-29 15:11:36 -0500 | [diff] [blame] | 3913 | int pending_del_nr = 0; |
| 3914 | int pending_del_slot = 0; |
Chris Mason | 179e29e | 2007-11-01 11:28:41 -0400 | [diff] [blame] | 3915 | int extent_type = -1; |
Yan, Zheng | 8082510 | 2009-11-12 09:35:36 +0000 | [diff] [blame] | 3916 | int ret; |
| 3917 | int err = 0; |
Li Zefan | 33345d01 | 2011-04-20 10:31:50 +0800 | [diff] [blame] | 3918 | u64 ino = btrfs_ino(inode); |
Yan, Zheng | 8082510 | 2009-11-12 09:35:36 +0000 | [diff] [blame] | 3919 | |
| 3920 | BUG_ON(new_size > 0 && min_type != BTRFS_EXTENT_DATA_KEY); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3921 | |
Mark Fasheh | 0eb0e19 | 2011-07-12 16:44:10 -0700 | [diff] [blame] | 3922 | path = btrfs_alloc_path(); |
| 3923 | if (!path) |
| 3924 | return -ENOMEM; |
| 3925 | path->reada = -1; |
| 3926 | |
Josef Bacik | 5dc562c | 2012-08-17 13:14:17 -0400 | [diff] [blame] | 3927 | /* |
| 3928 | * We want to drop from the next block forward in case this new size is |
| 3929 | * not block aligned since we will be keeping the last block of the |
| 3930 | * extent just the way it is. |
| 3931 | */ |
Josef Bacik | 0af3d00 | 2010-06-21 14:48:16 -0400 | [diff] [blame] | 3932 | if (root->ref_cows || root == root->fs_info->tree_root) |
Qu Wenruo | fda2832 | 2013-02-26 08:10:22 +0000 | [diff] [blame] | 3933 | btrfs_drop_extent_cache(inode, ALIGN(new_size, |
| 3934 | root->sectorsize), (u64)-1, 0); |
Yan, Zheng | 8082510 | 2009-11-12 09:35:36 +0000 | [diff] [blame] | 3935 | |
Miao Xie | 16cdcec | 2011-04-22 18:12:22 +0800 | [diff] [blame] | 3936 | /* |
| 3937 | * This function is also used to drop the items in the log tree before |
| 3938 | * we relog the inode, so if root != BTRFS_I(inode)->root, it means |
| 3939 | * it is used to drop the loged items. So we shouldn't kill the delayed |
| 3940 | * items. |
| 3941 | */ |
| 3942 | if (min_type == 0 && root == BTRFS_I(inode)->root) |
| 3943 | btrfs_kill_delayed_inode_items(inode); |
| 3944 | |
Li Zefan | 33345d01 | 2011-04-20 10:31:50 +0800 | [diff] [blame] | 3945 | key.objectid = ino; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3946 | key.offset = (u64)-1; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 3947 | key.type = (u8)-1; |
| 3948 | |
Chris Mason | 85e21ba | 2008-01-29 15:11:36 -0500 | [diff] [blame] | 3949 | search_again: |
Chris Mason | b947343 | 2009-03-13 11:00:37 -0400 | [diff] [blame] | 3950 | path->leave_spinning = 1; |
Chris Mason | 85e21ba | 2008-01-29 15:11:36 -0500 | [diff] [blame] | 3951 | ret = btrfs_search_slot(trans, root, &key, path, -1, 1); |
Yan, Zheng | 8082510 | 2009-11-12 09:35:36 +0000 | [diff] [blame] | 3952 | if (ret < 0) { |
| 3953 | err = ret; |
| 3954 | goto out; |
| 3955 | } |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 3956 | |
Chris Mason | 85e21ba | 2008-01-29 15:11:36 -0500 | [diff] [blame] | 3957 | if (ret > 0) { |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 3958 | /* there are no items in the tree for us to truncate, we're |
| 3959 | * done |
| 3960 | */ |
Yan, Zheng | 8082510 | 2009-11-12 09:35:36 +0000 | [diff] [blame] | 3961 | if (path->slots[0] == 0) |
| 3962 | goto out; |
Chris Mason | 85e21ba | 2008-01-29 15:11:36 -0500 | [diff] [blame] | 3963 | path->slots[0]--; |
| 3964 | } |
| 3965 | |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 3966 | while (1) { |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3967 | fi = NULL; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 3968 | leaf = path->nodes[0]; |
| 3969 | btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]); |
| 3970 | found_type = btrfs_key_type(&found_key); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3971 | |
Li Zefan | 33345d01 | 2011-04-20 10:31:50 +0800 | [diff] [blame] | 3972 | if (found_key.objectid != ino) |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3973 | break; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 3974 | |
Chris Mason | 85e21ba | 2008-01-29 15:11:36 -0500 | [diff] [blame] | 3975 | if (found_type < min_type) |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3976 | break; |
| 3977 | |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 3978 | item_end = found_key.offset; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3979 | if (found_type == BTRFS_EXTENT_DATA_KEY) { |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 3980 | fi = btrfs_item_ptr(leaf, path->slots[0], |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3981 | struct btrfs_file_extent_item); |
Chris Mason | 179e29e | 2007-11-01 11:28:41 -0400 | [diff] [blame] | 3982 | extent_type = btrfs_file_extent_type(leaf, fi); |
| 3983 | if (extent_type != BTRFS_FILE_EXTENT_INLINE) { |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 3984 | item_end += |
Chris Mason | db94535 | 2007-10-15 16:15:53 -0400 | [diff] [blame] | 3985 | btrfs_file_extent_num_bytes(leaf, fi); |
Chris Mason | 179e29e | 2007-11-01 11:28:41 -0400 | [diff] [blame] | 3986 | } else if (extent_type == BTRFS_FILE_EXTENT_INLINE) { |
Chris Mason | 179e29e | 2007-11-01 11:28:41 -0400 | [diff] [blame] | 3987 | item_end += btrfs_file_extent_inline_len(leaf, |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 3988 | fi); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3989 | } |
Yan | 008630c | 2007-11-07 13:31:09 -0500 | [diff] [blame] | 3990 | item_end--; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3991 | } |
Yan, Zheng | 8082510 | 2009-11-12 09:35:36 +0000 | [diff] [blame] | 3992 | if (found_type > min_type) { |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3993 | del_item = 1; |
Yan, Zheng | 8082510 | 2009-11-12 09:35:36 +0000 | [diff] [blame] | 3994 | } else { |
| 3995 | if (item_end < new_size) |
| 3996 | break; |
| 3997 | if (found_key.offset >= new_size) |
| 3998 | del_item = 1; |
| 3999 | else |
| 4000 | del_item = 0; |
| 4001 | } |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4002 | found_extent = 0; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4003 | /* FIXME, shrink the extent if the ref count is only 1 */ |
Chris Mason | 179e29e | 2007-11-01 11:28:41 -0400 | [diff] [blame] | 4004 | if (found_type != BTRFS_EXTENT_DATA_KEY) |
| 4005 | goto delete; |
| 4006 | |
Josef Bacik | 7f4f6e0 | 2013-08-29 16:43:28 -0400 | [diff] [blame] | 4007 | if (del_item) |
| 4008 | last_size = found_key.offset; |
| 4009 | else |
| 4010 | last_size = new_size; |
| 4011 | |
Chris Mason | 179e29e | 2007-11-01 11:28:41 -0400 | [diff] [blame] | 4012 | if (extent_type != BTRFS_FILE_EXTENT_INLINE) { |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4013 | u64 num_dec; |
Chris Mason | db94535 | 2007-10-15 16:15:53 -0400 | [diff] [blame] | 4014 | extent_start = btrfs_file_extent_disk_bytenr(leaf, fi); |
Miao Xie | f70a9a6b | 2012-01-12 19:10:12 -0500 | [diff] [blame] | 4015 | if (!del_item) { |
Chris Mason | db94535 | 2007-10-15 16:15:53 -0400 | [diff] [blame] | 4016 | u64 orig_num_bytes = |
| 4017 | btrfs_file_extent_num_bytes(leaf, fi); |
Qu Wenruo | fda2832 | 2013-02-26 08:10:22 +0000 | [diff] [blame] | 4018 | extent_num_bytes = ALIGN(new_size - |
| 4019 | found_key.offset, |
| 4020 | root->sectorsize); |
Chris Mason | db94535 | 2007-10-15 16:15:53 -0400 | [diff] [blame] | 4021 | btrfs_set_file_extent_num_bytes(leaf, fi, |
| 4022 | extent_num_bytes); |
| 4023 | num_dec = (orig_num_bytes - |
Chris Mason | 9069218 | 2008-02-08 13:49:28 -0500 | [diff] [blame] | 4024 | extent_num_bytes); |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 4025 | if (root->ref_cows && extent_start != 0) |
Yan Zheng | a76a3cd | 2008-10-09 11:46:29 -0400 | [diff] [blame] | 4026 | inode_sub_bytes(inode, num_dec); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 4027 | btrfs_mark_buffer_dirty(leaf); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4028 | } else { |
Chris Mason | db94535 | 2007-10-15 16:15:53 -0400 | [diff] [blame] | 4029 | extent_num_bytes = |
| 4030 | btrfs_file_extent_disk_num_bytes(leaf, |
| 4031 | fi); |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 4032 | extent_offset = found_key.offset - |
| 4033 | btrfs_file_extent_offset(leaf, fi); |
| 4034 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4035 | /* FIXME blocksize != 4096 */ |
Chris Mason | 9069218 | 2008-02-08 13:49:28 -0500 | [diff] [blame] | 4036 | num_dec = btrfs_file_extent_num_bytes(leaf, fi); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4037 | if (extent_start != 0) { |
| 4038 | found_extent = 1; |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 4039 | if (root->ref_cows) |
Yan Zheng | a76a3cd | 2008-10-09 11:46:29 -0400 | [diff] [blame] | 4040 | inode_sub_bytes(inode, num_dec); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4041 | } |
| 4042 | } |
Chris Mason | 9069218 | 2008-02-08 13:49:28 -0500 | [diff] [blame] | 4043 | } else if (extent_type == BTRFS_FILE_EXTENT_INLINE) { |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 4044 | /* |
| 4045 | * we can't truncate inline items that have had |
| 4046 | * special encodings |
| 4047 | */ |
| 4048 | if (!del_item && |
| 4049 | btrfs_file_extent_compression(leaf, fi) == 0 && |
| 4050 | btrfs_file_extent_encryption(leaf, fi) == 0 && |
| 4051 | btrfs_file_extent_other_encoding(leaf, fi) == 0) { |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 4052 | u32 size = new_size - found_key.offset; |
| 4053 | |
| 4054 | if (root->ref_cows) { |
Yan Zheng | a76a3cd | 2008-10-09 11:46:29 -0400 | [diff] [blame] | 4055 | inode_sub_bytes(inode, item_end + 1 - |
| 4056 | new_size); |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 4057 | } |
| 4058 | size = |
| 4059 | btrfs_file_extent_calc_inline_size(size); |
Tsutomu Itoh | afe5fea | 2013-04-16 05:18:22 +0000 | [diff] [blame] | 4060 | btrfs_truncate_item(root, path, size, 1); |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 4061 | } else if (root->ref_cows) { |
Yan Zheng | a76a3cd | 2008-10-09 11:46:29 -0400 | [diff] [blame] | 4062 | inode_sub_bytes(inode, item_end + 1 - |
| 4063 | found_key.offset); |
Chris Mason | 9069218 | 2008-02-08 13:49:28 -0500 | [diff] [blame] | 4064 | } |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4065 | } |
Chris Mason | 179e29e | 2007-11-01 11:28:41 -0400 | [diff] [blame] | 4066 | delete: |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4067 | if (del_item) { |
Chris Mason | 85e21ba | 2008-01-29 15:11:36 -0500 | [diff] [blame] | 4068 | if (!pending_del_nr) { |
| 4069 | /* no pending yet, add ourselves */ |
| 4070 | pending_del_slot = path->slots[0]; |
| 4071 | pending_del_nr = 1; |
| 4072 | } else if (pending_del_nr && |
| 4073 | path->slots[0] + 1 == pending_del_slot) { |
| 4074 | /* hop on the pending chunk */ |
| 4075 | pending_del_nr++; |
| 4076 | pending_del_slot = path->slots[0]; |
| 4077 | } else { |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 4078 | BUG(); |
Chris Mason | 85e21ba | 2008-01-29 15:11:36 -0500 | [diff] [blame] | 4079 | } |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4080 | } else { |
| 4081 | break; |
| 4082 | } |
Josef Bacik | 0af3d00 | 2010-06-21 14:48:16 -0400 | [diff] [blame] | 4083 | if (found_extent && (root->ref_cows || |
| 4084 | root == root->fs_info->tree_root)) { |
Chris Mason | b947343 | 2009-03-13 11:00:37 -0400 | [diff] [blame] | 4085 | btrfs_set_path_blocking(path); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4086 | ret = btrfs_free_extent(trans, root, extent_start, |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 4087 | extent_num_bytes, 0, |
| 4088 | btrfs_header_owner(leaf), |
Arne Jansen | 66d7e7f | 2011-09-12 15:26:38 +0200 | [diff] [blame] | 4089 | ino, extent_offset, 0); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4090 | BUG_ON(ret); |
| 4091 | } |
Chris Mason | 85e21ba | 2008-01-29 15:11:36 -0500 | [diff] [blame] | 4092 | |
Yan, Zheng | 8082510 | 2009-11-12 09:35:36 +0000 | [diff] [blame] | 4093 | if (found_type == BTRFS_INODE_ITEM_KEY) |
| 4094 | break; |
| 4095 | |
| 4096 | if (path->slots[0] == 0 || |
| 4097 | path->slots[0] != pending_del_slot) { |
Yan, Zheng | 8082510 | 2009-11-12 09:35:36 +0000 | [diff] [blame] | 4098 | if (pending_del_nr) { |
| 4099 | ret = btrfs_del_items(trans, root, path, |
| 4100 | pending_del_slot, |
| 4101 | pending_del_nr); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 4102 | if (ret) { |
| 4103 | btrfs_abort_transaction(trans, |
| 4104 | root, ret); |
| 4105 | goto error; |
| 4106 | } |
Yan, Zheng | 8082510 | 2009-11-12 09:35:36 +0000 | [diff] [blame] | 4107 | pending_del_nr = 0; |
| 4108 | } |
David Sterba | b3b4aa7 | 2011-04-21 01:20:15 +0200 | [diff] [blame] | 4109 | btrfs_release_path(path); |
Chris Mason | 85e21ba | 2008-01-29 15:11:36 -0500 | [diff] [blame] | 4110 | goto search_again; |
Yan, Zheng | 8082510 | 2009-11-12 09:35:36 +0000 | [diff] [blame] | 4111 | } else { |
| 4112 | path->slots[0]--; |
Chris Mason | 85e21ba | 2008-01-29 15:11:36 -0500 | [diff] [blame] | 4113 | } |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4114 | } |
Yan, Zheng | 8082510 | 2009-11-12 09:35:36 +0000 | [diff] [blame] | 4115 | out: |
Chris Mason | 85e21ba | 2008-01-29 15:11:36 -0500 | [diff] [blame] | 4116 | if (pending_del_nr) { |
| 4117 | ret = btrfs_del_items(trans, root, path, pending_del_slot, |
| 4118 | pending_del_nr); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 4119 | if (ret) |
| 4120 | btrfs_abort_transaction(trans, root, ret); |
Chris Mason | 85e21ba | 2008-01-29 15:11:36 -0500 | [diff] [blame] | 4121 | } |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 4122 | error: |
Josef Bacik | 7f4f6e0 | 2013-08-29 16:43:28 -0400 | [diff] [blame] | 4123 | if (last_size != (u64)-1) |
| 4124 | btrfs_ordered_update_i_size(inode, last_size, NULL); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4125 | btrfs_free_path(path); |
Yan, Zheng | 8082510 | 2009-11-12 09:35:36 +0000 | [diff] [blame] | 4126 | return err; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4127 | } |
| 4128 | |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 4129 | /* |
Josef Bacik | 2aaa665 | 2012-08-29 14:27:18 -0400 | [diff] [blame] | 4130 | * btrfs_truncate_page - read, zero a chunk and write a page |
| 4131 | * @inode - inode that we're zeroing |
| 4132 | * @from - the offset to start zeroing |
| 4133 | * @len - the length to zero, 0 to zero the entire range respective to the |
| 4134 | * offset |
| 4135 | * @front - zero up to the offset instead of from the offset on |
| 4136 | * |
| 4137 | * This will find the page for the "from" offset and cow the page and zero the |
| 4138 | * 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] | 4139 | */ |
Josef Bacik | 2aaa665 | 2012-08-29 14:27:18 -0400 | [diff] [blame] | 4140 | int btrfs_truncate_page(struct inode *inode, loff_t from, loff_t len, |
| 4141 | int front) |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 4142 | { |
Josef Bacik | 2aaa665 | 2012-08-29 14:27:18 -0400 | [diff] [blame] | 4143 | struct address_space *mapping = inode->i_mapping; |
Chris Mason | db94535 | 2007-10-15 16:15:53 -0400 | [diff] [blame] | 4144 | struct btrfs_root *root = BTRFS_I(inode)->root; |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 4145 | struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree; |
| 4146 | struct btrfs_ordered_extent *ordered; |
Josef Bacik | 2ac55d4 | 2010-02-03 19:33:23 +0000 | [diff] [blame] | 4147 | struct extent_state *cached_state = NULL; |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 4148 | char *kaddr; |
Chris Mason | db94535 | 2007-10-15 16:15:53 -0400 | [diff] [blame] | 4149 | u32 blocksize = root->sectorsize; |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 4150 | pgoff_t index = from >> PAGE_CACHE_SHIFT; |
| 4151 | unsigned offset = from & (PAGE_CACHE_SIZE-1); |
| 4152 | struct page *page; |
Josef Bacik | 3b16a4e | 2011-09-21 15:05:58 -0400 | [diff] [blame] | 4153 | gfp_t mask = btrfs_alloc_write_mask(mapping); |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 4154 | int ret = 0; |
| 4155 | u64 page_start; |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 4156 | u64 page_end; |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 4157 | |
Josef Bacik | 2aaa665 | 2012-08-29 14:27:18 -0400 | [diff] [blame] | 4158 | if ((offset & (blocksize - 1)) == 0 && |
| 4159 | (!len || ((len & (blocksize - 1)) == 0))) |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 4160 | goto out; |
Yan, Zheng | 0ca1f7c | 2010-05-16 10:48:47 -0400 | [diff] [blame] | 4161 | ret = btrfs_delalloc_reserve_space(inode, PAGE_CACHE_SIZE); |
Josef Bacik | 5d5e103 | 2009-10-13 16:46:49 -0400 | [diff] [blame] | 4162 | if (ret) |
| 4163 | goto out; |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 4164 | |
Chris Mason | 211c17f | 2008-05-15 09:13:45 -0400 | [diff] [blame] | 4165 | again: |
Josef Bacik | 3b16a4e | 2011-09-21 15:05:58 -0400 | [diff] [blame] | 4166 | page = find_or_create_page(mapping, index, mask); |
Josef Bacik | 5d5e103 | 2009-10-13 16:46:49 -0400 | [diff] [blame] | 4167 | if (!page) { |
Yan, Zheng | 0ca1f7c | 2010-05-16 10:48:47 -0400 | [diff] [blame] | 4168 | btrfs_delalloc_release_space(inode, PAGE_CACHE_SIZE); |
Miao Xie | ac6a2b3 | 2012-12-05 10:56:13 +0000 | [diff] [blame] | 4169 | ret = -ENOMEM; |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 4170 | goto out; |
Josef Bacik | 5d5e103 | 2009-10-13 16:46:49 -0400 | [diff] [blame] | 4171 | } |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 4172 | |
| 4173 | page_start = page_offset(page); |
| 4174 | page_end = page_start + PAGE_CACHE_SIZE - 1; |
| 4175 | |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 4176 | if (!PageUptodate(page)) { |
| 4177 | ret = btrfs_readpage(NULL, page); |
| 4178 | lock_page(page); |
Chris Mason | 211c17f | 2008-05-15 09:13:45 -0400 | [diff] [blame] | 4179 | if (page->mapping != mapping) { |
| 4180 | unlock_page(page); |
| 4181 | page_cache_release(page); |
| 4182 | goto again; |
| 4183 | } |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 4184 | if (!PageUptodate(page)) { |
| 4185 | ret = -EIO; |
Chris Mason | 8964222 | 2008-07-24 09:41:53 -0400 | [diff] [blame] | 4186 | goto out_unlock; |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 4187 | } |
| 4188 | } |
Chris Mason | 211c17f | 2008-05-15 09:13:45 -0400 | [diff] [blame] | 4189 | wait_on_page_writeback(page); |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 4190 | |
Jeff Mahoney | d008237 | 2012-03-01 14:57:19 +0100 | [diff] [blame] | 4191 | lock_extent_bits(io_tree, page_start, page_end, 0, &cached_state); |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 4192 | set_page_extent_mapped(page); |
| 4193 | |
| 4194 | ordered = btrfs_lookup_ordered_extent(inode, page_start); |
| 4195 | if (ordered) { |
Josef Bacik | 2ac55d4 | 2010-02-03 19:33:23 +0000 | [diff] [blame] | 4196 | unlock_extent_cached(io_tree, page_start, page_end, |
| 4197 | &cached_state, GFP_NOFS); |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 4198 | unlock_page(page); |
| 4199 | page_cache_release(page); |
Chris Mason | eb84ae0 | 2008-07-17 13:53:27 -0400 | [diff] [blame] | 4200 | btrfs_start_ordered_extent(inode, ordered, 1); |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 4201 | btrfs_put_ordered_extent(ordered); |
| 4202 | goto again; |
| 4203 | } |
| 4204 | |
Josef Bacik | 2ac55d4 | 2010-02-03 19:33:23 +0000 | [diff] [blame] | 4205 | clear_extent_bit(&BTRFS_I(inode)->io_tree, page_start, page_end, |
Liu Bo | 9e8a4a8 | 2012-09-05 19:10:51 -0600 | [diff] [blame] | 4206 | EXTENT_DIRTY | EXTENT_DELALLOC | |
| 4207 | EXTENT_DO_ACCOUNTING | EXTENT_DEFRAG, |
Josef Bacik | 2ac55d4 | 2010-02-03 19:33:23 +0000 | [diff] [blame] | 4208 | 0, 0, &cached_state, GFP_NOFS); |
Josef Bacik | 5d5e103 | 2009-10-13 16:46:49 -0400 | [diff] [blame] | 4209 | |
Josef Bacik | 2ac55d4 | 2010-02-03 19:33:23 +0000 | [diff] [blame] | 4210 | ret = btrfs_set_extent_delalloc(inode, page_start, page_end, |
| 4211 | &cached_state); |
Josef Bacik | 9ed74f2 | 2009-09-11 16:12:44 -0400 | [diff] [blame] | 4212 | if (ret) { |
Josef Bacik | 2ac55d4 | 2010-02-03 19:33:23 +0000 | [diff] [blame] | 4213 | unlock_extent_cached(io_tree, page_start, page_end, |
| 4214 | &cached_state, GFP_NOFS); |
Josef Bacik | 9ed74f2 | 2009-09-11 16:12:44 -0400 | [diff] [blame] | 4215 | goto out_unlock; |
| 4216 | } |
| 4217 | |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 4218 | if (offset != PAGE_CACHE_SIZE) { |
Josef Bacik | 2aaa665 | 2012-08-29 14:27:18 -0400 | [diff] [blame] | 4219 | if (!len) |
| 4220 | len = PAGE_CACHE_SIZE - offset; |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 4221 | kaddr = kmap(page); |
Josef Bacik | 2aaa665 | 2012-08-29 14:27:18 -0400 | [diff] [blame] | 4222 | if (front) |
| 4223 | memset(kaddr, 0, offset); |
| 4224 | else |
| 4225 | memset(kaddr + offset, 0, len); |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 4226 | flush_dcache_page(page); |
| 4227 | kunmap(page); |
| 4228 | } |
Chris Mason | 247e743 | 2008-07-17 12:53:51 -0400 | [diff] [blame] | 4229 | ClearPageChecked(page); |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 4230 | set_page_dirty(page); |
Josef Bacik | 2ac55d4 | 2010-02-03 19:33:23 +0000 | [diff] [blame] | 4231 | unlock_extent_cached(io_tree, page_start, page_end, &cached_state, |
| 4232 | GFP_NOFS); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4233 | |
Chris Mason | 8964222 | 2008-07-24 09:41:53 -0400 | [diff] [blame] | 4234 | out_unlock: |
Josef Bacik | 5d5e103 | 2009-10-13 16:46:49 -0400 | [diff] [blame] | 4235 | if (ret) |
Yan, Zheng | 0ca1f7c | 2010-05-16 10:48:47 -0400 | [diff] [blame] | 4236 | btrfs_delalloc_release_space(inode, PAGE_CACHE_SIZE); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4237 | unlock_page(page); |
| 4238 | page_cache_release(page); |
| 4239 | out: |
| 4240 | return ret; |
| 4241 | } |
| 4242 | |
Josef Bacik | 16e7549 | 2013-10-22 12:18:51 -0400 | [diff] [blame] | 4243 | static int maybe_insert_hole(struct btrfs_root *root, struct inode *inode, |
| 4244 | u64 offset, u64 len) |
| 4245 | { |
| 4246 | struct btrfs_trans_handle *trans; |
| 4247 | int ret; |
| 4248 | |
| 4249 | /* |
| 4250 | * Still need to make sure the inode looks like it's been updated so |
| 4251 | * that any holes get logged if we fsync. |
| 4252 | */ |
| 4253 | if (btrfs_fs_incompat(root->fs_info, NO_HOLES)) { |
| 4254 | BTRFS_I(inode)->last_trans = root->fs_info->generation; |
| 4255 | BTRFS_I(inode)->last_sub_trans = root->log_transid; |
| 4256 | BTRFS_I(inode)->last_log_commit = root->last_log_commit; |
| 4257 | return 0; |
| 4258 | } |
| 4259 | |
| 4260 | /* |
| 4261 | * 1 - for the one we're dropping |
| 4262 | * 1 - for the one we're adding |
| 4263 | * 1 - for updating the inode. |
| 4264 | */ |
| 4265 | trans = btrfs_start_transaction(root, 3); |
| 4266 | if (IS_ERR(trans)) |
| 4267 | return PTR_ERR(trans); |
| 4268 | |
| 4269 | ret = btrfs_drop_extents(trans, root, inode, offset, offset + len, 1); |
| 4270 | if (ret) { |
| 4271 | btrfs_abort_transaction(trans, root, ret); |
| 4272 | btrfs_end_transaction(trans, root); |
| 4273 | return ret; |
| 4274 | } |
| 4275 | |
| 4276 | ret = btrfs_insert_file_extent(trans, root, btrfs_ino(inode), offset, |
| 4277 | 0, 0, len, 0, len, 0, 0, 0); |
| 4278 | if (ret) |
| 4279 | btrfs_abort_transaction(trans, root, ret); |
| 4280 | else |
| 4281 | btrfs_update_inode(trans, root, inode); |
| 4282 | btrfs_end_transaction(trans, root); |
| 4283 | return ret; |
| 4284 | } |
| 4285 | |
Josef Bacik | 695a0d0 | 2011-03-04 15:46:53 -0500 | [diff] [blame] | 4286 | /* |
| 4287 | * This function puts in dummy file extents for the area we're creating a hole |
| 4288 | * for. So if we are truncating this file to a larger size we need to insert |
| 4289 | * these file extents so that btrfs_get_extent will return a EXTENT_MAP_HOLE for |
| 4290 | * the range between oldsize and size |
| 4291 | */ |
Josef Bacik | a41ad39 | 2011-01-31 15:30:16 -0500 | [diff] [blame] | 4292 | 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] | 4293 | { |
Yan Zheng | 9036c10 | 2008-10-30 14:19:41 -0400 | [diff] [blame] | 4294 | struct btrfs_root *root = BTRFS_I(inode)->root; |
| 4295 | struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree; |
Yan, Zheng | a22285a | 2010-05-16 10:48:46 -0400 | [diff] [blame] | 4296 | struct extent_map *em = NULL; |
Josef Bacik | 2ac55d4 | 2010-02-03 19:33:23 +0000 | [diff] [blame] | 4297 | struct extent_state *cached_state = NULL; |
Josef Bacik | 5dc562c | 2012-08-17 13:14:17 -0400 | [diff] [blame] | 4298 | struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree; |
Qu Wenruo | fda2832 | 2013-02-26 08:10:22 +0000 | [diff] [blame] | 4299 | u64 hole_start = ALIGN(oldsize, root->sectorsize); |
| 4300 | u64 block_end = ALIGN(size, root->sectorsize); |
Yan Zheng | 9036c10 | 2008-10-30 14:19:41 -0400 | [diff] [blame] | 4301 | u64 last_byte; |
| 4302 | u64 cur_offset; |
| 4303 | u64 hole_size; |
Josef Bacik | 9ed74f2 | 2009-09-11 16:12:44 -0400 | [diff] [blame] | 4304 | int err = 0; |
Yan Zheng | 9036c10 | 2008-10-30 14:19:41 -0400 | [diff] [blame] | 4305 | |
Josef Bacik | a71754f | 2013-06-17 17:14:39 -0400 | [diff] [blame] | 4306 | /* |
| 4307 | * If our size started in the middle of a page we need to zero out the |
| 4308 | * rest of the page before we expand the i_size, otherwise we could |
| 4309 | * expose stale data. |
| 4310 | */ |
| 4311 | err = btrfs_truncate_page(inode, oldsize, 0, 0); |
| 4312 | if (err) |
| 4313 | return err; |
| 4314 | |
Yan Zheng | 9036c10 | 2008-10-30 14:19:41 -0400 | [diff] [blame] | 4315 | if (size <= hole_start) |
| 4316 | return 0; |
| 4317 | |
Yan Zheng | 9036c10 | 2008-10-30 14:19:41 -0400 | [diff] [blame] | 4318 | while (1) { |
| 4319 | struct btrfs_ordered_extent *ordered; |
Miao Xie | fa7c149 | 2013-09-26 13:15:27 +0800 | [diff] [blame] | 4320 | |
Josef Bacik | 2ac55d4 | 2010-02-03 19:33:23 +0000 | [diff] [blame] | 4321 | lock_extent_bits(io_tree, hole_start, block_end - 1, 0, |
Jeff Mahoney | d008237 | 2012-03-01 14:57:19 +0100 | [diff] [blame] | 4322 | &cached_state); |
Miao Xie | fa7c149 | 2013-09-26 13:15:27 +0800 | [diff] [blame] | 4323 | ordered = btrfs_lookup_ordered_range(inode, hole_start, |
| 4324 | block_end - hole_start); |
Yan Zheng | 9036c10 | 2008-10-30 14:19:41 -0400 | [diff] [blame] | 4325 | if (!ordered) |
| 4326 | break; |
Josef Bacik | 2ac55d4 | 2010-02-03 19:33:23 +0000 | [diff] [blame] | 4327 | unlock_extent_cached(io_tree, hole_start, block_end - 1, |
| 4328 | &cached_state, GFP_NOFS); |
Miao Xie | fa7c149 | 2013-09-26 13:15:27 +0800 | [diff] [blame] | 4329 | btrfs_start_ordered_extent(inode, ordered, 1); |
Yan Zheng | 9036c10 | 2008-10-30 14:19:41 -0400 | [diff] [blame] | 4330 | btrfs_put_ordered_extent(ordered); |
| 4331 | } |
| 4332 | |
Yan Zheng | 9036c10 | 2008-10-30 14:19:41 -0400 | [diff] [blame] | 4333 | cur_offset = hole_start; |
| 4334 | while (1) { |
| 4335 | em = btrfs_get_extent(inode, NULL, 0, cur_offset, |
| 4336 | block_end - cur_offset, 0); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 4337 | if (IS_ERR(em)) { |
| 4338 | err = PTR_ERR(em); |
Zach Brown | f276795 | 2013-01-08 19:37:58 +0000 | [diff] [blame] | 4339 | em = NULL; |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 4340 | break; |
| 4341 | } |
Yan Zheng | 9036c10 | 2008-10-30 14:19:41 -0400 | [diff] [blame] | 4342 | last_byte = min(extent_map_end(em), block_end); |
Qu Wenruo | fda2832 | 2013-02-26 08:10:22 +0000 | [diff] [blame] | 4343 | last_byte = ALIGN(last_byte , root->sectorsize); |
Yan, Zheng | 8082510 | 2009-11-12 09:35:36 +0000 | [diff] [blame] | 4344 | if (!test_bit(EXTENT_FLAG_PREALLOC, &em->flags)) { |
Josef Bacik | 5dc562c | 2012-08-17 13:14:17 -0400 | [diff] [blame] | 4345 | struct extent_map *hole_em; |
Yan Zheng | 9036c10 | 2008-10-30 14:19:41 -0400 | [diff] [blame] | 4346 | hole_size = last_byte - cur_offset; |
Yan, Zheng | 8082510 | 2009-11-12 09:35:36 +0000 | [diff] [blame] | 4347 | |
Josef Bacik | 16e7549 | 2013-10-22 12:18:51 -0400 | [diff] [blame] | 4348 | err = maybe_insert_hole(root, inode, cur_offset, |
| 4349 | hole_size); |
| 4350 | if (err) |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 4351 | break; |
Josef Bacik | 5dc562c | 2012-08-17 13:14:17 -0400 | [diff] [blame] | 4352 | btrfs_drop_extent_cache(inode, cur_offset, |
| 4353 | cur_offset + hole_size - 1, 0); |
| 4354 | hole_em = alloc_extent_map(); |
| 4355 | if (!hole_em) { |
| 4356 | set_bit(BTRFS_INODE_NEEDS_FULL_SYNC, |
| 4357 | &BTRFS_I(inode)->runtime_flags); |
| 4358 | goto next; |
| 4359 | } |
| 4360 | hole_em->start = cur_offset; |
| 4361 | hole_em->len = hole_size; |
| 4362 | hole_em->orig_start = cur_offset; |
Yan, Zheng | 8082510 | 2009-11-12 09:35:36 +0000 | [diff] [blame] | 4363 | |
Josef Bacik | 5dc562c | 2012-08-17 13:14:17 -0400 | [diff] [blame] | 4364 | hole_em->block_start = EXTENT_MAP_HOLE; |
| 4365 | hole_em->block_len = 0; |
Josef Bacik | b493968 | 2012-12-03 10:31:19 -0500 | [diff] [blame] | 4366 | hole_em->orig_block_len = 0; |
Josef Bacik | cc95bef | 2013-04-04 14:31:27 -0400 | [diff] [blame] | 4367 | hole_em->ram_bytes = hole_size; |
Josef Bacik | 5dc562c | 2012-08-17 13:14:17 -0400 | [diff] [blame] | 4368 | hole_em->bdev = root->fs_info->fs_devices->latest_bdev; |
| 4369 | hole_em->compress_type = BTRFS_COMPRESS_NONE; |
Josef Bacik | 16e7549 | 2013-10-22 12:18:51 -0400 | [diff] [blame] | 4370 | hole_em->generation = root->fs_info->generation; |
Josef Bacik | 5dc562c | 2012-08-17 13:14:17 -0400 | [diff] [blame] | 4371 | |
| 4372 | while (1) { |
| 4373 | write_lock(&em_tree->lock); |
Josef Bacik | 09a2a8f9 | 2013-04-05 16:51:15 -0400 | [diff] [blame] | 4374 | err = add_extent_mapping(em_tree, hole_em, 1); |
Josef Bacik | 5dc562c | 2012-08-17 13:14:17 -0400 | [diff] [blame] | 4375 | write_unlock(&em_tree->lock); |
| 4376 | if (err != -EEXIST) |
| 4377 | break; |
| 4378 | btrfs_drop_extent_cache(inode, cur_offset, |
| 4379 | cur_offset + |
| 4380 | hole_size - 1, 0); |
| 4381 | } |
| 4382 | free_extent_map(hole_em); |
Yan Zheng | 9036c10 | 2008-10-30 14:19:41 -0400 | [diff] [blame] | 4383 | } |
Josef Bacik | 16e7549 | 2013-10-22 12:18:51 -0400 | [diff] [blame] | 4384 | next: |
Yan Zheng | 9036c10 | 2008-10-30 14:19:41 -0400 | [diff] [blame] | 4385 | free_extent_map(em); |
Yan, Zheng | a22285a | 2010-05-16 10:48:46 -0400 | [diff] [blame] | 4386 | em = NULL; |
Yan Zheng | 9036c10 | 2008-10-30 14:19:41 -0400 | [diff] [blame] | 4387 | cur_offset = last_byte; |
Yan, Zheng | 8082510 | 2009-11-12 09:35:36 +0000 | [diff] [blame] | 4388 | if (cur_offset >= block_end) |
Yan Zheng | 9036c10 | 2008-10-30 14:19:41 -0400 | [diff] [blame] | 4389 | break; |
| 4390 | } |
Yan, Zheng | a22285a | 2010-05-16 10:48:46 -0400 | [diff] [blame] | 4391 | free_extent_map(em); |
Josef Bacik | 2ac55d4 | 2010-02-03 19:33:23 +0000 | [diff] [blame] | 4392 | unlock_extent_cached(io_tree, hole_start, block_end - 1, &cached_state, |
| 4393 | GFP_NOFS); |
Yan Zheng | 9036c10 | 2008-10-30 14:19:41 -0400 | [diff] [blame] | 4394 | return err; |
| 4395 | } |
| 4396 | |
Eric Sandeen | 3972f26 | 2013-01-12 02:57:22 +0000 | [diff] [blame] | 4397 | static int btrfs_setsize(struct inode *inode, struct iattr *attr) |
Yan, Zheng | 8082510 | 2009-11-12 09:35:36 +0000 | [diff] [blame] | 4398 | { |
Miao Xie | f4a2f4c | 2011-12-14 20:12:01 -0500 | [diff] [blame] | 4399 | struct btrfs_root *root = BTRFS_I(inode)->root; |
| 4400 | struct btrfs_trans_handle *trans; |
Josef Bacik | a41ad39 | 2011-01-31 15:30:16 -0500 | [diff] [blame] | 4401 | loff_t oldsize = i_size_read(inode); |
Eric Sandeen | 3972f26 | 2013-01-12 02:57:22 +0000 | [diff] [blame] | 4402 | loff_t newsize = attr->ia_size; |
| 4403 | int mask = attr->ia_valid; |
Yan, Zheng | 8082510 | 2009-11-12 09:35:36 +0000 | [diff] [blame] | 4404 | int ret; |
| 4405 | |
Eric Sandeen | 3972f26 | 2013-01-12 02:57:22 +0000 | [diff] [blame] | 4406 | /* |
| 4407 | * The regular truncate() case without ATTR_CTIME and ATTR_MTIME is a |
| 4408 | * special case where we need to update the times despite not having |
| 4409 | * these flags set. For all other operations the VFS set these flags |
| 4410 | * explicitly if it wants a timestamp update. |
| 4411 | */ |
| 4412 | if (newsize != oldsize && (!(mask & (ATTR_CTIME | ATTR_MTIME)))) |
| 4413 | inode->i_ctime = inode->i_mtime = current_fs_time(inode->i_sb); |
| 4414 | |
Josef Bacik | a41ad39 | 2011-01-31 15:30:16 -0500 | [diff] [blame] | 4415 | if (newsize > oldsize) { |
Kirill A. Shutemov | 7caef26 | 2013-09-12 15:13:56 -0700 | [diff] [blame] | 4416 | truncate_pagecache(inode, newsize); |
Josef Bacik | a41ad39 | 2011-01-31 15:30:16 -0500 | [diff] [blame] | 4417 | ret = btrfs_cont_expand(inode, oldsize, newsize); |
Miao Xie | f4a2f4c | 2011-12-14 20:12:01 -0500 | [diff] [blame] | 4418 | if (ret) |
Yan, Zheng | 8082510 | 2009-11-12 09:35:36 +0000 | [diff] [blame] | 4419 | return ret; |
Yan, Zheng | 8082510 | 2009-11-12 09:35:36 +0000 | [diff] [blame] | 4420 | |
Miao Xie | f4a2f4c | 2011-12-14 20:12:01 -0500 | [diff] [blame] | 4421 | trans = btrfs_start_transaction(root, 1); |
| 4422 | if (IS_ERR(trans)) |
| 4423 | return PTR_ERR(trans); |
| 4424 | |
| 4425 | i_size_write(inode, newsize); |
| 4426 | btrfs_ordered_update_i_size(inode, i_size_read(inode), NULL); |
| 4427 | ret = btrfs_update_inode(trans, root, inode); |
Josef Bacik | 7ad85bb | 2012-01-12 19:10:12 -0500 | [diff] [blame] | 4428 | btrfs_end_transaction(trans, root); |
Josef Bacik | a41ad39 | 2011-01-31 15:30:16 -0500 | [diff] [blame] | 4429 | } else { |
Yan, Zheng | 8082510 | 2009-11-12 09:35:36 +0000 | [diff] [blame] | 4430 | |
Josef Bacik | a41ad39 | 2011-01-31 15:30:16 -0500 | [diff] [blame] | 4431 | /* |
| 4432 | * We're truncating a file that used to have good data down to |
| 4433 | * zero. Make sure it gets into the ordered flush list so that |
| 4434 | * any new writes get down to disk quickly. |
| 4435 | */ |
| 4436 | if (newsize == 0) |
Josef Bacik | 72ac3c0 | 2012-05-23 14:13:11 -0400 | [diff] [blame] | 4437 | set_bit(BTRFS_INODE_ORDERED_DATA_CLOSE, |
| 4438 | &BTRFS_I(inode)->runtime_flags); |
Yan, Zheng | 8082510 | 2009-11-12 09:35:36 +0000 | [diff] [blame] | 4439 | |
Josef Bacik | f3fe820 | 2013-01-07 17:03:21 -0500 | [diff] [blame] | 4440 | /* |
| 4441 | * 1 for the orphan item we're going to add |
| 4442 | * 1 for the orphan item deletion. |
| 4443 | */ |
| 4444 | trans = btrfs_start_transaction(root, 2); |
| 4445 | if (IS_ERR(trans)) |
| 4446 | return PTR_ERR(trans); |
| 4447 | |
| 4448 | /* |
| 4449 | * We need to do this in case we fail at _any_ point during the |
| 4450 | * actual truncate. Once we do the truncate_setsize we could |
| 4451 | * invalidate pages which forces any outstanding ordered io to |
| 4452 | * be instantly completed which will give us extents that need |
| 4453 | * to be truncated. If we fail to get an orphan inode down we |
| 4454 | * could have left over extents that were never meant to live, |
| 4455 | * so we need to garuntee from this point on that everything |
| 4456 | * will be consistent. |
| 4457 | */ |
| 4458 | ret = btrfs_orphan_add(trans, inode); |
| 4459 | btrfs_end_transaction(trans, root); |
| 4460 | if (ret) |
| 4461 | return ret; |
| 4462 | |
Josef Bacik | a41ad39 | 2011-01-31 15:30:16 -0500 | [diff] [blame] | 4463 | /* we don't support swapfiles, so vmtruncate shouldn't fail */ |
| 4464 | truncate_setsize(inode, newsize); |
Miao Xie | 2e60a51 | 2013-02-08 07:01:08 +0000 | [diff] [blame] | 4465 | |
| 4466 | /* Disable nonlocked read DIO to avoid the end less truncate */ |
| 4467 | btrfs_inode_block_unlocked_dio(inode); |
| 4468 | inode_dio_wait(inode); |
| 4469 | btrfs_inode_resume_unlocked_dio(inode); |
| 4470 | |
Josef Bacik | a41ad39 | 2011-01-31 15:30:16 -0500 | [diff] [blame] | 4471 | ret = btrfs_truncate(inode); |
Josef Bacik | 7f4f6e0 | 2013-08-29 16:43:28 -0400 | [diff] [blame] | 4472 | if (ret && inode->i_nlink) { |
| 4473 | int err; |
| 4474 | |
| 4475 | /* |
| 4476 | * failed to truncate, disk_i_size is only adjusted down |
| 4477 | * as we remove extents, so it should represent the true |
| 4478 | * size of the inode, so reset the in memory size and |
| 4479 | * delete our orphan entry. |
| 4480 | */ |
| 4481 | trans = btrfs_join_transaction(root); |
| 4482 | if (IS_ERR(trans)) { |
| 4483 | btrfs_orphan_del(NULL, inode); |
| 4484 | return ret; |
| 4485 | } |
| 4486 | i_size_write(inode, BTRFS_I(inode)->disk_i_size); |
| 4487 | err = btrfs_orphan_del(trans, inode); |
| 4488 | if (err) |
| 4489 | btrfs_abort_transaction(trans, root, err); |
| 4490 | btrfs_end_transaction(trans, root); |
| 4491 | } |
Yan, Zheng | 8082510 | 2009-11-12 09:35:36 +0000 | [diff] [blame] | 4492 | } |
| 4493 | |
Josef Bacik | a41ad39 | 2011-01-31 15:30:16 -0500 | [diff] [blame] | 4494 | return ret; |
Yan, Zheng | 8082510 | 2009-11-12 09:35:36 +0000 | [diff] [blame] | 4495 | } |
| 4496 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4497 | static int btrfs_setattr(struct dentry *dentry, struct iattr *attr) |
| 4498 | { |
| 4499 | struct inode *inode = dentry->d_inode; |
Li Zefan | b83cc96 | 2010-12-20 16:04:08 +0800 | [diff] [blame] | 4500 | struct btrfs_root *root = BTRFS_I(inode)->root; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4501 | int err; |
| 4502 | |
Li Zefan | b83cc96 | 2010-12-20 16:04:08 +0800 | [diff] [blame] | 4503 | if (btrfs_root_readonly(root)) |
| 4504 | return -EROFS; |
| 4505 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4506 | err = inode_change_ok(inode, attr); |
| 4507 | if (err) |
| 4508 | return err; |
| 4509 | |
Chris Mason | 5a3f23d | 2009-03-31 13:27:11 -0400 | [diff] [blame] | 4510 | if (S_ISREG(inode->i_mode) && (attr->ia_valid & ATTR_SIZE)) { |
Eric Sandeen | 3972f26 | 2013-01-12 02:57:22 +0000 | [diff] [blame] | 4511 | err = btrfs_setsize(inode, attr); |
Yan, Zheng | 8082510 | 2009-11-12 09:35:36 +0000 | [diff] [blame] | 4512 | if (err) |
| 4513 | return err; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4514 | } |
Yan Zheng | 9036c10 | 2008-10-30 14:19:41 -0400 | [diff] [blame] | 4515 | |
Christoph Hellwig | 1025774 | 2010-06-04 11:30:02 +0200 | [diff] [blame] | 4516 | if (attr->ia_valid) { |
| 4517 | setattr_copy(inode, attr); |
Josef Bacik | 0c4d2d9 | 2012-04-05 15:03:02 -0400 | [diff] [blame] | 4518 | inode_inc_iversion(inode); |
Josef Bacik | 22c44fe | 2011-11-30 10:45:38 -0500 | [diff] [blame] | 4519 | err = btrfs_dirty_inode(inode); |
Josef Bacik | 33268ea | 2008-07-24 12:16:36 -0400 | [diff] [blame] | 4520 | |
Josef Bacik | 22c44fe | 2011-11-30 10:45:38 -0500 | [diff] [blame] | 4521 | if (!err && attr->ia_valid & ATTR_MODE) |
Christoph Hellwig | 1025774 | 2010-06-04 11:30:02 +0200 | [diff] [blame] | 4522 | err = btrfs_acl_chmod(inode); |
| 4523 | } |
| 4524 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4525 | return err; |
| 4526 | } |
Chris Mason | 61295eb | 2008-01-14 16:24:38 -0500 | [diff] [blame] | 4527 | |
Filipe David Borba Manana | 131e404 | 2013-11-19 22:29:35 +0000 | [diff] [blame] | 4528 | /* |
| 4529 | * While truncating the inode pages during eviction, we get the VFS calling |
| 4530 | * btrfs_invalidatepage() against each page of the inode. This is slow because |
| 4531 | * the calls to btrfs_invalidatepage() result in a huge amount of calls to |
| 4532 | * lock_extent_bits() and clear_extent_bit(), which keep merging and splitting |
| 4533 | * extent_state structures over and over, wasting lots of time. |
| 4534 | * |
| 4535 | * Therefore if the inode is being evicted, let btrfs_invalidatepage() skip all |
| 4536 | * those expensive operations on a per page basis and do only the ordered io |
| 4537 | * finishing, while we release here the extent_map and extent_state structures, |
| 4538 | * without the excessive merging and splitting. |
| 4539 | */ |
| 4540 | static void evict_inode_truncate_pages(struct inode *inode) |
| 4541 | { |
| 4542 | struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree; |
| 4543 | struct extent_map_tree *map_tree = &BTRFS_I(inode)->extent_tree; |
| 4544 | struct rb_node *node; |
| 4545 | |
| 4546 | ASSERT(inode->i_state & I_FREEING); |
| 4547 | truncate_inode_pages(&inode->i_data, 0); |
| 4548 | |
| 4549 | write_lock(&map_tree->lock); |
| 4550 | while (!RB_EMPTY_ROOT(&map_tree->map)) { |
| 4551 | struct extent_map *em; |
| 4552 | |
| 4553 | node = rb_first(&map_tree->map); |
| 4554 | em = rb_entry(node, struct extent_map, rb_node); |
Wang Shilong | 180589e | 2013-12-14 15:27:31 +0800 | [diff] [blame] | 4555 | clear_bit(EXTENT_FLAG_PINNED, &em->flags); |
| 4556 | clear_bit(EXTENT_FLAG_LOGGING, &em->flags); |
Filipe David Borba Manana | 131e404 | 2013-11-19 22:29:35 +0000 | [diff] [blame] | 4557 | remove_extent_mapping(map_tree, em); |
| 4558 | free_extent_map(em); |
| 4559 | } |
| 4560 | write_unlock(&map_tree->lock); |
| 4561 | |
| 4562 | spin_lock(&io_tree->lock); |
| 4563 | while (!RB_EMPTY_ROOT(&io_tree->state)) { |
| 4564 | struct extent_state *state; |
| 4565 | struct extent_state *cached_state = NULL; |
| 4566 | |
| 4567 | node = rb_first(&io_tree->state); |
| 4568 | state = rb_entry(node, struct extent_state, rb_node); |
| 4569 | atomic_inc(&state->refs); |
| 4570 | spin_unlock(&io_tree->lock); |
| 4571 | |
| 4572 | lock_extent_bits(io_tree, state->start, state->end, |
| 4573 | 0, &cached_state); |
| 4574 | clear_extent_bit(io_tree, state->start, state->end, |
| 4575 | EXTENT_LOCKED | EXTENT_DIRTY | |
| 4576 | EXTENT_DELALLOC | EXTENT_DO_ACCOUNTING | |
| 4577 | EXTENT_DEFRAG, 1, 1, |
| 4578 | &cached_state, GFP_NOFS); |
| 4579 | free_extent_state(state); |
| 4580 | |
| 4581 | spin_lock(&io_tree->lock); |
| 4582 | } |
| 4583 | spin_unlock(&io_tree->lock); |
| 4584 | } |
| 4585 | |
Al Viro | bd55597 | 2010-06-07 11:35:40 -0400 | [diff] [blame] | 4586 | void btrfs_evict_inode(struct inode *inode) |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4587 | { |
| 4588 | struct btrfs_trans_handle *trans; |
| 4589 | struct btrfs_root *root = BTRFS_I(inode)->root; |
Josef Bacik | 726c35f | 2011-09-26 15:46:06 -0400 | [diff] [blame] | 4590 | struct btrfs_block_rsv *rsv, *global_rsv; |
Josef Bacik | 0712718 | 2011-08-19 10:29:59 -0400 | [diff] [blame] | 4591 | u64 min_size = btrfs_calc_trunc_metadata_size(root, 1); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4592 | int ret; |
| 4593 | |
liubo | 1abe9b8 | 2011-03-24 11:18:59 +0000 | [diff] [blame] | 4594 | trace_btrfs_inode_evict(inode); |
| 4595 | |
Filipe David Borba Manana | 131e404 | 2013-11-19 22:29:35 +0000 | [diff] [blame] | 4596 | evict_inode_truncate_pages(inode); |
| 4597 | |
Stefan Behrens | 69e9c6c | 2013-09-05 16:58:43 +0200 | [diff] [blame] | 4598 | if (inode->i_nlink && |
| 4599 | ((btrfs_root_refs(&root->root_item) != 0 && |
| 4600 | root->root_key.objectid != BTRFS_ROOT_TREE_OBJECTID) || |
| 4601 | btrfs_is_free_space_inode(inode))) |
Al Viro | bd55597 | 2010-06-07 11:35:40 -0400 | [diff] [blame] | 4602 | goto no_delete; |
| 4603 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4604 | if (is_bad_inode(inode)) { |
Josef Bacik | 7b12876 | 2008-07-24 12:17:14 -0400 | [diff] [blame] | 4605 | btrfs_orphan_del(NULL, inode); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4606 | goto no_delete; |
| 4607 | } |
Al Viro | bd55597 | 2010-06-07 11:35:40 -0400 | [diff] [blame] | 4608 | /* 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] | 4609 | btrfs_wait_ordered_range(inode, 0, (u64)-1); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 4610 | |
Yan, Zheng | c71bf09 | 2009-11-12 09:34:40 +0000 | [diff] [blame] | 4611 | if (root->fs_info->log_root_recovering) { |
Liu Bo | 6bf0231 | 2012-06-25 21:59:09 -0600 | [diff] [blame] | 4612 | BUG_ON(test_bit(BTRFS_INODE_HAS_ORPHAN_ITEM, |
Josef Bacik | 8a35d95 | 2012-05-23 14:26:42 -0400 | [diff] [blame] | 4613 | &BTRFS_I(inode)->runtime_flags)); |
Yan, Zheng | c71bf09 | 2009-11-12 09:34:40 +0000 | [diff] [blame] | 4614 | goto no_delete; |
| 4615 | } |
| 4616 | |
Yan, Zheng | 76dda93 | 2009-09-21 16:00:26 -0400 | [diff] [blame] | 4617 | if (inode->i_nlink > 0) { |
Stefan Behrens | 69e9c6c | 2013-09-05 16:58:43 +0200 | [diff] [blame] | 4618 | BUG_ON(btrfs_root_refs(&root->root_item) != 0 && |
| 4619 | root->root_key.objectid != BTRFS_ROOT_TREE_OBJECTID); |
Yan, Zheng | 76dda93 | 2009-09-21 16:00:26 -0400 | [diff] [blame] | 4620 | goto no_delete; |
| 4621 | } |
| 4622 | |
Miao Xie | 0e8c36a | 2012-12-19 06:59:51 +0000 | [diff] [blame] | 4623 | ret = btrfs_commit_inode_delayed_inode(inode); |
| 4624 | if (ret) { |
| 4625 | btrfs_orphan_del(NULL, inode); |
| 4626 | goto no_delete; |
| 4627 | } |
| 4628 | |
Miao Xie | 66d8f3d | 2012-09-06 04:02:28 -0600 | [diff] [blame] | 4629 | rsv = btrfs_alloc_block_rsv(root, BTRFS_BLOCK_RSV_TEMP); |
Josef Bacik | 4289a66 | 2011-08-05 13:22:24 -0400 | [diff] [blame] | 4630 | if (!rsv) { |
| 4631 | btrfs_orphan_del(NULL, inode); |
| 4632 | goto no_delete; |
| 4633 | } |
Josef Bacik | 4a33854 | 2011-08-29 11:01:31 -0400 | [diff] [blame] | 4634 | rsv->size = min_size; |
Josef Bacik | ca7e70f | 2012-08-27 17:48:15 -0400 | [diff] [blame] | 4635 | rsv->failfast = 1; |
Josef Bacik | 726c35f | 2011-09-26 15:46:06 -0400 | [diff] [blame] | 4636 | global_rsv = &root->fs_info->global_block_rsv; |
Josef Bacik | 4289a66 | 2011-08-05 13:22:24 -0400 | [diff] [blame] | 4637 | |
Chris Mason | dbe674a | 2008-07-17 12:54:05 -0400 | [diff] [blame] | 4638 | btrfs_i_size_write(inode, 0); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 4639 | |
Josef Bacik | 4289a66 | 2011-08-05 13:22:24 -0400 | [diff] [blame] | 4640 | /* |
Miao Xie | 8407aa4 | 2012-09-07 01:43:32 -0600 | [diff] [blame] | 4641 | * This is a bit simpler than btrfs_truncate since we've already |
| 4642 | * reserved our space for our orphan item in the unlink, so we just |
| 4643 | * need to reserve some slack space in case we add bytes and update |
| 4644 | * inode item when doing the truncate. |
Josef Bacik | 4289a66 | 2011-08-05 13:22:24 -0400 | [diff] [blame] | 4645 | */ |
Yan, Zheng | 8082510 | 2009-11-12 09:35:36 +0000 | [diff] [blame] | 4646 | while (1) { |
Miao Xie | 08e007d | 2012-10-16 11:33:38 +0000 | [diff] [blame] | 4647 | ret = btrfs_block_rsv_refill(root, rsv, min_size, |
| 4648 | BTRFS_RESERVE_FLUSH_LIMIT); |
Yan, Zheng | 8082510 | 2009-11-12 09:35:36 +0000 | [diff] [blame] | 4649 | |
Josef Bacik | 726c35f | 2011-09-26 15:46:06 -0400 | [diff] [blame] | 4650 | /* |
| 4651 | * Try and steal from the global reserve since we will |
| 4652 | * likely not use this space anyway, we want to try as |
| 4653 | * hard as possible to get this to work. |
| 4654 | */ |
| 4655 | if (ret) |
| 4656 | ret = btrfs_block_rsv_migrate(global_rsv, rsv, min_size); |
| 4657 | |
Yan, Zheng | d68fc57 | 2010-05-16 10:49:58 -0400 | [diff] [blame] | 4658 | if (ret) { |
Simon Kirby | c2cf52e | 2013-03-19 22:41:23 +0000 | [diff] [blame] | 4659 | btrfs_warn(root->fs_info, |
| 4660 | "Could not get space for a delete, will truncate on mount %d", |
| 4661 | ret); |
Josef Bacik | 4289a66 | 2011-08-05 13:22:24 -0400 | [diff] [blame] | 4662 | btrfs_orphan_del(NULL, inode); |
| 4663 | btrfs_free_block_rsv(root, rsv); |
| 4664 | goto no_delete; |
Yan, Zheng | d68fc57 | 2010-05-16 10:49:58 -0400 | [diff] [blame] | 4665 | } |
| 4666 | |
Miao Xie | 0e8c36a | 2012-12-19 06:59:51 +0000 | [diff] [blame] | 4667 | trans = btrfs_join_transaction(root); |
Josef Bacik | 4289a66 | 2011-08-05 13:22:24 -0400 | [diff] [blame] | 4668 | if (IS_ERR(trans)) { |
| 4669 | btrfs_orphan_del(NULL, inode); |
| 4670 | btrfs_free_block_rsv(root, rsv); |
| 4671 | goto no_delete; |
| 4672 | } |
| 4673 | |
| 4674 | trans->block_rsv = rsv; |
| 4675 | |
Yan, Zheng | d68fc57 | 2010-05-16 10:49:58 -0400 | [diff] [blame] | 4676 | ret = btrfs_truncate_inode_items(trans, root, inode, 0, 0); |
Josef Bacik | ca7e70f | 2012-08-27 17:48:15 -0400 | [diff] [blame] | 4677 | if (ret != -ENOSPC) |
Yan, Zheng | 8082510 | 2009-11-12 09:35:36 +0000 | [diff] [blame] | 4678 | break; |
| 4679 | |
Miao Xie | 8407aa4 | 2012-09-07 01:43:32 -0600 | [diff] [blame] | 4680 | trans->block_rsv = &root->fs_info->trans_block_rsv; |
Yan, Zheng | 8082510 | 2009-11-12 09:35:36 +0000 | [diff] [blame] | 4681 | btrfs_end_transaction(trans, root); |
| 4682 | trans = NULL; |
Liu Bo | b53d3f5 | 2012-11-14 14:34:34 +0000 | [diff] [blame] | 4683 | btrfs_btree_balance_dirty(root); |
Josef Bacik | 7b12876 | 2008-07-24 12:17:14 -0400 | [diff] [blame] | 4684 | } |
| 4685 | |
Josef Bacik | 4289a66 | 2011-08-05 13:22:24 -0400 | [diff] [blame] | 4686 | btrfs_free_block_rsv(root, rsv); |
| 4687 | |
Josef Bacik | 4ef31a4 | 2013-08-13 14:10:08 -0400 | [diff] [blame] | 4688 | /* |
| 4689 | * Errors here aren't a big deal, it just means we leave orphan items |
| 4690 | * in the tree. They will be cleaned up on the next mount. |
| 4691 | */ |
Yan, Zheng | 8082510 | 2009-11-12 09:35:36 +0000 | [diff] [blame] | 4692 | if (ret == 0) { |
Josef Bacik | 4289a66 | 2011-08-05 13:22:24 -0400 | [diff] [blame] | 4693 | trans->block_rsv = root->orphan_block_rsv; |
Josef Bacik | 4ef31a4 | 2013-08-13 14:10:08 -0400 | [diff] [blame] | 4694 | btrfs_orphan_del(trans, inode); |
| 4695 | } else { |
| 4696 | btrfs_orphan_del(NULL, inode); |
Yan, Zheng | 8082510 | 2009-11-12 09:35:36 +0000 | [diff] [blame] | 4697 | } |
Chris Mason | 85e21ba | 2008-01-29 15:11:36 -0500 | [diff] [blame] | 4698 | |
Josef Bacik | 4289a66 | 2011-08-05 13:22:24 -0400 | [diff] [blame] | 4699 | trans->block_rsv = &root->fs_info->trans_block_rsv; |
Li Zefan | 581bb05 | 2011-04-20 10:06:11 +0800 | [diff] [blame] | 4700 | if (!(root == root->fs_info->tree_root || |
| 4701 | root->root_key.objectid == BTRFS_TREE_RELOC_OBJECTID)) |
Li Zefan | 33345d01 | 2011-04-20 10:31:50 +0800 | [diff] [blame] | 4702 | btrfs_return_ino(root, btrfs_ino(inode)); |
Li Zefan | 581bb05 | 2011-04-20 10:06:11 +0800 | [diff] [blame] | 4703 | |
Chris Mason | 54aa1f4 | 2007-06-22 14:16:25 -0400 | [diff] [blame] | 4704 | btrfs_end_transaction(trans, root); |
Liu Bo | b53d3f5 | 2012-11-14 14:34:34 +0000 | [diff] [blame] | 4705 | btrfs_btree_balance_dirty(root); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4706 | no_delete: |
Miao Xie | 89042e5 | 2013-05-15 07:48:15 +0000 | [diff] [blame] | 4707 | btrfs_remove_delayed_node(inode); |
Jan Kara | dbd5768 | 2012-05-03 14:48:02 +0200 | [diff] [blame] | 4708 | clear_inode(inode); |
Yan, Zheng | 8082510 | 2009-11-12 09:35:36 +0000 | [diff] [blame] | 4709 | return; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4710 | } |
| 4711 | |
| 4712 | /* |
| 4713 | * this returns the key found in the dir entry in the location pointer. |
| 4714 | * If no dir entries were found, location->objectid is 0. |
| 4715 | */ |
| 4716 | static int btrfs_inode_by_name(struct inode *dir, struct dentry *dentry, |
| 4717 | struct btrfs_key *location) |
| 4718 | { |
| 4719 | const char *name = dentry->d_name.name; |
| 4720 | int namelen = dentry->d_name.len; |
| 4721 | struct btrfs_dir_item *di; |
| 4722 | struct btrfs_path *path; |
| 4723 | struct btrfs_root *root = BTRFS_I(dir)->root; |
Yan | 0d9f7f3 | 2007-10-25 15:48:28 -0400 | [diff] [blame] | 4724 | int ret = 0; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4725 | |
| 4726 | path = btrfs_alloc_path(); |
Mark Fasheh | d8926bb | 2011-07-13 10:38:47 -0700 | [diff] [blame] | 4727 | if (!path) |
| 4728 | return -ENOMEM; |
Chris Mason | 3954401 | 2007-12-12 14:38:19 -0500 | [diff] [blame] | 4729 | |
Li Zefan | 33345d01 | 2011-04-20 10:31:50 +0800 | [diff] [blame] | 4730 | di = btrfs_lookup_dir_item(NULL, root, path, btrfs_ino(dir), name, |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4731 | namelen, 0); |
Yan | 0d9f7f3 | 2007-10-25 15:48:28 -0400 | [diff] [blame] | 4732 | if (IS_ERR(di)) |
| 4733 | ret = PTR_ERR(di); |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 4734 | |
David Sterba | c704005 | 2011-04-19 18:00:01 +0200 | [diff] [blame] | 4735 | if (IS_ERR_OR_NULL(di)) |
Chris Mason | 3954401 | 2007-12-12 14:38:19 -0500 | [diff] [blame] | 4736 | goto out_err; |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 4737 | |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 4738 | btrfs_dir_item_key_to_cpu(path->nodes[0], di, location); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4739 | out: |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4740 | btrfs_free_path(path); |
| 4741 | return ret; |
Chris Mason | 3954401 | 2007-12-12 14:38:19 -0500 | [diff] [blame] | 4742 | out_err: |
| 4743 | location->objectid = 0; |
| 4744 | goto out; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4745 | } |
| 4746 | |
| 4747 | /* |
| 4748 | * when we hit a tree root in a directory, the btrfs part of the inode |
| 4749 | * needs to be changed to reflect the root directory of the tree root. This |
| 4750 | * is kind of like crossing a mount point. |
| 4751 | */ |
| 4752 | static int fixup_tree_root_location(struct btrfs_root *root, |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 4753 | struct inode *dir, |
| 4754 | struct dentry *dentry, |
| 4755 | struct btrfs_key *location, |
| 4756 | struct btrfs_root **sub_root) |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4757 | { |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 4758 | struct btrfs_path *path; |
| 4759 | struct btrfs_root *new_root; |
| 4760 | struct btrfs_root_ref *ref; |
| 4761 | struct extent_buffer *leaf; |
| 4762 | int ret; |
| 4763 | int err = 0; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4764 | |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 4765 | path = btrfs_alloc_path(); |
| 4766 | if (!path) { |
| 4767 | err = -ENOMEM; |
| 4768 | goto out; |
| 4769 | } |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4770 | |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 4771 | err = -ENOENT; |
Kelley Nielsen | 75ac2dd | 2013-11-04 19:35:58 -0800 | [diff] [blame] | 4772 | ret = btrfs_find_item(root->fs_info->tree_root, path, |
| 4773 | BTRFS_I(dir)->root->root_key.objectid, |
| 4774 | location->objectid, BTRFS_ROOT_REF_KEY, NULL); |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 4775 | if (ret) { |
| 4776 | if (ret < 0) |
| 4777 | err = ret; |
| 4778 | goto out; |
| 4779 | } |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4780 | |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 4781 | leaf = path->nodes[0]; |
| 4782 | ref = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_root_ref); |
Li Zefan | 33345d01 | 2011-04-20 10:31:50 +0800 | [diff] [blame] | 4783 | if (btrfs_root_ref_dirid(leaf, ref) != btrfs_ino(dir) || |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 4784 | btrfs_root_ref_name_len(leaf, ref) != dentry->d_name.len) |
| 4785 | goto out; |
| 4786 | |
| 4787 | ret = memcmp_extent_buffer(leaf, dentry->d_name.name, |
| 4788 | (unsigned long)(ref + 1), |
| 4789 | dentry->d_name.len); |
| 4790 | if (ret) |
| 4791 | goto out; |
| 4792 | |
David Sterba | b3b4aa7 | 2011-04-21 01:20:15 +0200 | [diff] [blame] | 4793 | btrfs_release_path(path); |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 4794 | |
| 4795 | new_root = btrfs_read_fs_root_no_name(root->fs_info, location); |
| 4796 | if (IS_ERR(new_root)) { |
| 4797 | err = PTR_ERR(new_root); |
| 4798 | goto out; |
| 4799 | } |
| 4800 | |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 4801 | *sub_root = new_root; |
| 4802 | location->objectid = btrfs_root_dirid(&new_root->root_item); |
| 4803 | location->type = BTRFS_INODE_ITEM_KEY; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4804 | location->offset = 0; |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 4805 | err = 0; |
| 4806 | out: |
| 4807 | btrfs_free_path(path); |
| 4808 | return err; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4809 | } |
| 4810 | |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 4811 | static void inode_tree_add(struct inode *inode) |
| 4812 | { |
| 4813 | struct btrfs_root *root = BTRFS_I(inode)->root; |
| 4814 | struct btrfs_inode *entry; |
From: Nick Piggin | 03e860b | 2009-08-21 10:09:44 +0200 | [diff] [blame] | 4815 | struct rb_node **p; |
| 4816 | struct rb_node *parent; |
Filipe David Borba Manana | cef2193 | 2013-09-02 12:19:13 +0100 | [diff] [blame] | 4817 | struct rb_node *new = &BTRFS_I(inode)->rb_node; |
Li Zefan | 33345d01 | 2011-04-20 10:31:50 +0800 | [diff] [blame] | 4818 | u64 ino = btrfs_ino(inode); |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 4819 | |
Al Viro | 1d3382cb | 2010-10-23 15:19:20 -0400 | [diff] [blame] | 4820 | if (inode_unhashed(inode)) |
Yan, Zheng | 76dda93 | 2009-09-21 16:00:26 -0400 | [diff] [blame] | 4821 | return; |
Miao Xie | e1409ce | 2013-05-15 07:48:16 +0000 | [diff] [blame] | 4822 | parent = NULL; |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 4823 | spin_lock(&root->inode_lock); |
Miao Xie | e1409ce | 2013-05-15 07:48:16 +0000 | [diff] [blame] | 4824 | p = &root->inode_tree.rb_node; |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 4825 | while (*p) { |
| 4826 | parent = *p; |
| 4827 | entry = rb_entry(parent, struct btrfs_inode, rb_node); |
| 4828 | |
Li Zefan | 33345d01 | 2011-04-20 10:31:50 +0800 | [diff] [blame] | 4829 | if (ino < btrfs_ino(&entry->vfs_inode)) |
From: Nick Piggin | 03e860b | 2009-08-21 10:09:44 +0200 | [diff] [blame] | 4830 | p = &parent->rb_left; |
Li Zefan | 33345d01 | 2011-04-20 10:31:50 +0800 | [diff] [blame] | 4831 | else if (ino > btrfs_ino(&entry->vfs_inode)) |
From: Nick Piggin | 03e860b | 2009-08-21 10:09:44 +0200 | [diff] [blame] | 4832 | p = &parent->rb_right; |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 4833 | else { |
| 4834 | WARN_ON(!(entry->vfs_inode.i_state & |
Al Viro | a4ffdde | 2010-06-02 17:38:30 -0400 | [diff] [blame] | 4835 | (I_WILL_FREE | I_FREEING))); |
Filipe David Borba Manana | cef2193 | 2013-09-02 12:19:13 +0100 | [diff] [blame] | 4836 | rb_replace_node(parent, new, &root->inode_tree); |
From: Nick Piggin | 03e860b | 2009-08-21 10:09:44 +0200 | [diff] [blame] | 4837 | RB_CLEAR_NODE(parent); |
| 4838 | spin_unlock(&root->inode_lock); |
Filipe David Borba Manana | cef2193 | 2013-09-02 12:19:13 +0100 | [diff] [blame] | 4839 | return; |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 4840 | } |
| 4841 | } |
Filipe David Borba Manana | cef2193 | 2013-09-02 12:19:13 +0100 | [diff] [blame] | 4842 | rb_link_node(new, parent, p); |
| 4843 | rb_insert_color(new, &root->inode_tree); |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 4844 | spin_unlock(&root->inode_lock); |
| 4845 | } |
| 4846 | |
| 4847 | static void inode_tree_del(struct inode *inode) |
| 4848 | { |
| 4849 | struct btrfs_root *root = BTRFS_I(inode)->root; |
Yan, Zheng | 76dda93 | 2009-09-21 16:00:26 -0400 | [diff] [blame] | 4850 | int empty = 0; |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 4851 | |
From: Nick Piggin | 03e860b | 2009-08-21 10:09:44 +0200 | [diff] [blame] | 4852 | spin_lock(&root->inode_lock); |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 4853 | if (!RB_EMPTY_NODE(&BTRFS_I(inode)->rb_node)) { |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 4854 | rb_erase(&BTRFS_I(inode)->rb_node, &root->inode_tree); |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 4855 | RB_CLEAR_NODE(&BTRFS_I(inode)->rb_node); |
Yan, Zheng | 76dda93 | 2009-09-21 16:00:26 -0400 | [diff] [blame] | 4856 | empty = RB_EMPTY_ROOT(&root->inode_tree); |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 4857 | } |
From: Nick Piggin | 03e860b | 2009-08-21 10:09:44 +0200 | [diff] [blame] | 4858 | spin_unlock(&root->inode_lock); |
Yan, Zheng | 76dda93 | 2009-09-21 16:00:26 -0400 | [diff] [blame] | 4859 | |
Stefan Behrens | 69e9c6c | 2013-09-05 16:58:43 +0200 | [diff] [blame] | 4860 | if (empty && btrfs_root_refs(&root->root_item) == 0) { |
Yan, Zheng | 76dda93 | 2009-09-21 16:00:26 -0400 | [diff] [blame] | 4861 | synchronize_srcu(&root->fs_info->subvol_srcu); |
| 4862 | spin_lock(&root->inode_lock); |
| 4863 | empty = RB_EMPTY_ROOT(&root->inode_tree); |
| 4864 | spin_unlock(&root->inode_lock); |
| 4865 | if (empty) |
| 4866 | btrfs_add_dead_root(root); |
| 4867 | } |
| 4868 | } |
| 4869 | |
Jeff Mahoney | 143bede | 2012-03-01 14:56:26 +0100 | [diff] [blame] | 4870 | void btrfs_invalidate_inodes(struct btrfs_root *root) |
Yan, Zheng | 76dda93 | 2009-09-21 16:00:26 -0400 | [diff] [blame] | 4871 | { |
| 4872 | struct rb_node *node; |
| 4873 | struct rb_node *prev; |
| 4874 | struct btrfs_inode *entry; |
| 4875 | struct inode *inode; |
| 4876 | u64 objectid = 0; |
| 4877 | |
| 4878 | WARN_ON(btrfs_root_refs(&root->root_item) != 0); |
| 4879 | |
| 4880 | spin_lock(&root->inode_lock); |
| 4881 | again: |
| 4882 | node = root->inode_tree.rb_node; |
| 4883 | prev = NULL; |
| 4884 | while (node) { |
| 4885 | prev = node; |
| 4886 | entry = rb_entry(node, struct btrfs_inode, rb_node); |
| 4887 | |
Li Zefan | 33345d01 | 2011-04-20 10:31:50 +0800 | [diff] [blame] | 4888 | if (objectid < btrfs_ino(&entry->vfs_inode)) |
Yan, Zheng | 76dda93 | 2009-09-21 16:00:26 -0400 | [diff] [blame] | 4889 | node = node->rb_left; |
Li Zefan | 33345d01 | 2011-04-20 10:31:50 +0800 | [diff] [blame] | 4890 | else if (objectid > btrfs_ino(&entry->vfs_inode)) |
Yan, Zheng | 76dda93 | 2009-09-21 16:00:26 -0400 | [diff] [blame] | 4891 | node = node->rb_right; |
| 4892 | else |
| 4893 | break; |
| 4894 | } |
| 4895 | if (!node) { |
| 4896 | while (prev) { |
| 4897 | entry = rb_entry(prev, struct btrfs_inode, rb_node); |
Li Zefan | 33345d01 | 2011-04-20 10:31:50 +0800 | [diff] [blame] | 4898 | if (objectid <= btrfs_ino(&entry->vfs_inode)) { |
Yan, Zheng | 76dda93 | 2009-09-21 16:00:26 -0400 | [diff] [blame] | 4899 | node = prev; |
| 4900 | break; |
| 4901 | } |
| 4902 | prev = rb_next(prev); |
| 4903 | } |
| 4904 | } |
| 4905 | while (node) { |
| 4906 | entry = rb_entry(node, struct btrfs_inode, rb_node); |
Li Zefan | 33345d01 | 2011-04-20 10:31:50 +0800 | [diff] [blame] | 4907 | objectid = btrfs_ino(&entry->vfs_inode) + 1; |
Yan, Zheng | 76dda93 | 2009-09-21 16:00:26 -0400 | [diff] [blame] | 4908 | inode = igrab(&entry->vfs_inode); |
| 4909 | if (inode) { |
| 4910 | spin_unlock(&root->inode_lock); |
| 4911 | if (atomic_read(&inode->i_count) > 1) |
| 4912 | d_prune_aliases(inode); |
| 4913 | /* |
Al Viro | 45321ac | 2010-06-07 13:43:19 -0400 | [diff] [blame] | 4914 | * btrfs_drop_inode will have it removed from |
Yan, Zheng | 76dda93 | 2009-09-21 16:00:26 -0400 | [diff] [blame] | 4915 | * the inode cache when its usage count |
| 4916 | * hits zero. |
| 4917 | */ |
| 4918 | iput(inode); |
| 4919 | cond_resched(); |
| 4920 | spin_lock(&root->inode_lock); |
| 4921 | goto again; |
| 4922 | } |
| 4923 | |
| 4924 | if (cond_resched_lock(&root->inode_lock)) |
| 4925 | goto again; |
| 4926 | |
| 4927 | node = rb_next(node); |
| 4928 | } |
| 4929 | spin_unlock(&root->inode_lock); |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 4930 | } |
| 4931 | |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 4932 | static int btrfs_init_locked_inode(struct inode *inode, void *p) |
| 4933 | { |
| 4934 | struct btrfs_iget_args *args = p; |
| 4935 | inode->i_ino = args->ino; |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 4936 | BTRFS_I(inode)->root = args->root; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4937 | return 0; |
| 4938 | } |
| 4939 | |
| 4940 | static int btrfs_find_actor(struct inode *inode, void *opaque) |
| 4941 | { |
| 4942 | struct btrfs_iget_args *args = opaque; |
Li Zefan | 33345d01 | 2011-04-20 10:31:50 +0800 | [diff] [blame] | 4943 | return args->ino == btrfs_ino(inode) && |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 4944 | args->root == BTRFS_I(inode)->root; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4945 | } |
| 4946 | |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 4947 | static struct inode *btrfs_iget_locked(struct super_block *s, |
| 4948 | u64 objectid, |
| 4949 | struct btrfs_root *root) |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4950 | { |
| 4951 | struct inode *inode; |
| 4952 | struct btrfs_iget_args args; |
Filipe David Borba Manana | 778ba82 | 2013-10-06 22:22:33 +0100 | [diff] [blame] | 4953 | unsigned long hashval = btrfs_inode_hash(objectid, root); |
| 4954 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4955 | args.ino = objectid; |
| 4956 | args.root = root; |
| 4957 | |
Filipe David Borba Manana | 778ba82 | 2013-10-06 22:22:33 +0100 | [diff] [blame] | 4958 | inode = iget5_locked(s, hashval, btrfs_find_actor, |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4959 | btrfs_init_locked_inode, |
| 4960 | (void *)&args); |
| 4961 | return inode; |
| 4962 | } |
| 4963 | |
Balaji Rao | 1a54ef8 | 2008-07-21 02:01:04 +0530 | [diff] [blame] | 4964 | /* Get an inode object given its location and corresponding root. |
| 4965 | * Returns in *is_new if the inode was read from disk |
| 4966 | */ |
| 4967 | struct inode *btrfs_iget(struct super_block *s, struct btrfs_key *location, |
Josef Bacik | 73f7341 | 2009-12-04 17:38:27 +0000 | [diff] [blame] | 4968 | struct btrfs_root *root, int *new) |
Balaji Rao | 1a54ef8 | 2008-07-21 02:01:04 +0530 | [diff] [blame] | 4969 | { |
| 4970 | struct inode *inode; |
| 4971 | |
| 4972 | inode = btrfs_iget_locked(s, location->objectid, root); |
| 4973 | if (!inode) |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 4974 | return ERR_PTR(-ENOMEM); |
Balaji Rao | 1a54ef8 | 2008-07-21 02:01:04 +0530 | [diff] [blame] | 4975 | |
| 4976 | if (inode->i_state & I_NEW) { |
| 4977 | BTRFS_I(inode)->root = root; |
| 4978 | memcpy(&BTRFS_I(inode)->location, location, sizeof(*location)); |
| 4979 | btrfs_read_locked_inode(inode); |
Mark Fasheh | 1748f84 | 2011-07-12 11:25:31 -0700 | [diff] [blame] | 4980 | if (!is_bad_inode(inode)) { |
| 4981 | inode_tree_add(inode); |
| 4982 | unlock_new_inode(inode); |
| 4983 | if (new) |
| 4984 | *new = 1; |
| 4985 | } else { |
Sergei Trofimovich | e0b6d65 | 2011-09-11 10:52:24 -0400 | [diff] [blame] | 4986 | unlock_new_inode(inode); |
| 4987 | iput(inode); |
| 4988 | inode = ERR_PTR(-ESTALE); |
Mark Fasheh | 1748f84 | 2011-07-12 11:25:31 -0700 | [diff] [blame] | 4989 | } |
| 4990 | } |
| 4991 | |
Balaji Rao | 1a54ef8 | 2008-07-21 02:01:04 +0530 | [diff] [blame] | 4992 | return inode; |
| 4993 | } |
| 4994 | |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 4995 | static struct inode *new_simple_dir(struct super_block *s, |
| 4996 | struct btrfs_key *key, |
| 4997 | struct btrfs_root *root) |
| 4998 | { |
| 4999 | struct inode *inode = new_inode(s); |
| 5000 | |
| 5001 | if (!inode) |
| 5002 | return ERR_PTR(-ENOMEM); |
| 5003 | |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 5004 | BTRFS_I(inode)->root = root; |
| 5005 | memcpy(&BTRFS_I(inode)->location, key, sizeof(*key)); |
Josef Bacik | 72ac3c0 | 2012-05-23 14:13:11 -0400 | [diff] [blame] | 5006 | set_bit(BTRFS_INODE_DUMMY, &BTRFS_I(inode)->runtime_flags); |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 5007 | |
| 5008 | inode->i_ino = BTRFS_EMPTY_SUBVOL_DIR_OBJECTID; |
Li Zefan | 848cce0 | 2012-02-21 17:04:28 +0800 | [diff] [blame] | 5009 | inode->i_op = &btrfs_dir_ro_inode_operations; |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 5010 | inode->i_fop = &simple_dir_operations; |
| 5011 | inode->i_mode = S_IFDIR | S_IRUGO | S_IWUSR | S_IXUGO; |
| 5012 | inode->i_mtime = inode->i_atime = inode->i_ctime = CURRENT_TIME; |
| 5013 | |
| 5014 | return inode; |
| 5015 | } |
| 5016 | |
Chris Mason | 3de4586 | 2008-11-17 21:02:50 -0500 | [diff] [blame] | 5017 | struct inode *btrfs_lookup_dentry(struct inode *dir, struct dentry *dentry) |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5018 | { |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 5019 | struct inode *inode; |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 5020 | struct btrfs_root *root = BTRFS_I(dir)->root; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5021 | struct btrfs_root *sub_root = root; |
| 5022 | struct btrfs_key location; |
Yan, Zheng | 76dda93 | 2009-09-21 16:00:26 -0400 | [diff] [blame] | 5023 | int index; |
Josef Bacik | b4aff1f | 2011-06-28 16:18:59 -0400 | [diff] [blame] | 5024 | int ret = 0; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5025 | |
| 5026 | if (dentry->d_name.len > BTRFS_NAME_LEN) |
| 5027 | return ERR_PTR(-ENAMETOOLONG); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 5028 | |
Jeff Layton | 39e3c95 | 2012-11-28 11:30:53 -0500 | [diff] [blame] | 5029 | ret = btrfs_inode_by_name(dir, dentry, &location); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5030 | if (ret < 0) |
| 5031 | return ERR_PTR(ret); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 5032 | |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 5033 | if (location.objectid == 0) |
Tsutomu Itoh | 5662344b3 | 2013-12-13 09:51:42 +0900 | [diff] [blame] | 5034 | return ERR_PTR(-ENOENT); |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 5035 | |
| 5036 | if (location.type == BTRFS_INODE_ITEM_KEY) { |
Josef Bacik | 73f7341 | 2009-12-04 17:38:27 +0000 | [diff] [blame] | 5037 | inode = btrfs_iget(dir->i_sb, &location, root, NULL); |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 5038 | return inode; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5039 | } |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 5040 | |
| 5041 | BUG_ON(location.type != BTRFS_ROOT_ITEM_KEY); |
| 5042 | |
Yan, Zheng | 76dda93 | 2009-09-21 16:00:26 -0400 | [diff] [blame] | 5043 | index = srcu_read_lock(&root->fs_info->subvol_srcu); |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 5044 | ret = fixup_tree_root_location(root, dir, dentry, |
| 5045 | &location, &sub_root); |
| 5046 | if (ret < 0) { |
| 5047 | if (ret != -ENOENT) |
| 5048 | inode = ERR_PTR(ret); |
| 5049 | else |
| 5050 | inode = new_simple_dir(dir->i_sb, &location, sub_root); |
| 5051 | } else { |
Josef Bacik | 73f7341 | 2009-12-04 17:38:27 +0000 | [diff] [blame] | 5052 | inode = btrfs_iget(dir->i_sb, &location, sub_root, NULL); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5053 | } |
Yan, Zheng | 76dda93 | 2009-09-21 16:00:26 -0400 | [diff] [blame] | 5054 | srcu_read_unlock(&root->fs_info->subvol_srcu, index); |
| 5055 | |
Julia Lawall | 34d19ba | 2011-01-24 19:55:19 +0000 | [diff] [blame] | 5056 | if (!IS_ERR(inode) && root != sub_root) { |
Yan, Zheng | c71bf09 | 2009-11-12 09:34:40 +0000 | [diff] [blame] | 5057 | down_read(&root->fs_info->cleanup_work_sem); |
| 5058 | if (!(inode->i_sb->s_flags & MS_RDONLY)) |
Josef Bacik | 66b4ffd | 2011-01-31 16:22:42 -0500 | [diff] [blame] | 5059 | ret = btrfs_orphan_cleanup(sub_root); |
Yan, Zheng | c71bf09 | 2009-11-12 09:34:40 +0000 | [diff] [blame] | 5060 | up_read(&root->fs_info->cleanup_work_sem); |
Josef Bacik | 01cd336 | 2013-06-03 21:39:49 -0400 | [diff] [blame] | 5061 | if (ret) { |
| 5062 | iput(inode); |
Josef Bacik | 66b4ffd | 2011-01-31 16:22:42 -0500 | [diff] [blame] | 5063 | inode = ERR_PTR(ret); |
Josef Bacik | 01cd336 | 2013-06-03 21:39:49 -0400 | [diff] [blame] | 5064 | } |
Yan, Zheng | c71bf09 | 2009-11-12 09:34:40 +0000 | [diff] [blame] | 5065 | } |
| 5066 | |
Chris Mason | 3de4586 | 2008-11-17 21:02:50 -0500 | [diff] [blame] | 5067 | return inode; |
| 5068 | } |
| 5069 | |
Nick Piggin | fe15ce4 | 2011-01-07 17:49:23 +1100 | [diff] [blame] | 5070 | static int btrfs_dentry_delete(const struct dentry *dentry) |
Yan, Zheng | 76dda93 | 2009-09-21 16:00:26 -0400 | [diff] [blame] | 5071 | { |
| 5072 | struct btrfs_root *root; |
Li Zefan | 848cce0 | 2012-02-21 17:04:28 +0800 | [diff] [blame] | 5073 | struct inode *inode = dentry->d_inode; |
Yan, Zheng | 76dda93 | 2009-09-21 16:00:26 -0400 | [diff] [blame] | 5074 | |
Li Zefan | 848cce0 | 2012-02-21 17:04:28 +0800 | [diff] [blame] | 5075 | if (!inode && !IS_ROOT(dentry)) |
| 5076 | inode = dentry->d_parent->d_inode; |
Yan, Zheng | 76dda93 | 2009-09-21 16:00:26 -0400 | [diff] [blame] | 5077 | |
Li Zefan | 848cce0 | 2012-02-21 17:04:28 +0800 | [diff] [blame] | 5078 | if (inode) { |
| 5079 | root = BTRFS_I(inode)->root; |
Yan, Zheng | efefb14 | 2009-10-09 09:25:16 -0400 | [diff] [blame] | 5080 | if (btrfs_root_refs(&root->root_item) == 0) |
| 5081 | return 1; |
Li Zefan | 848cce0 | 2012-02-21 17:04:28 +0800 | [diff] [blame] | 5082 | |
| 5083 | if (btrfs_ino(inode) == BTRFS_EMPTY_SUBVOL_DIR_OBJECTID) |
| 5084 | return 1; |
Yan, Zheng | efefb14 | 2009-10-09 09:25:16 -0400 | [diff] [blame] | 5085 | } |
Yan, Zheng | 76dda93 | 2009-09-21 16:00:26 -0400 | [diff] [blame] | 5086 | return 0; |
| 5087 | } |
| 5088 | |
Josef Bacik | b4aff1f | 2011-06-28 16:18:59 -0400 | [diff] [blame] | 5089 | static void btrfs_dentry_release(struct dentry *dentry) |
| 5090 | { |
| 5091 | if (dentry->d_fsdata) |
| 5092 | kfree(dentry->d_fsdata); |
| 5093 | } |
| 5094 | |
Chris Mason | 3de4586 | 2008-11-17 21:02:50 -0500 | [diff] [blame] | 5095 | static struct dentry *btrfs_lookup(struct inode *dir, struct dentry *dentry, |
Al Viro | 00cd8dd | 2012-06-10 17:13:09 -0400 | [diff] [blame] | 5096 | unsigned int flags) |
Chris Mason | 3de4586 | 2008-11-17 21:02:50 -0500 | [diff] [blame] | 5097 | { |
Tsutomu Itoh | 5662344b3 | 2013-12-13 09:51:42 +0900 | [diff] [blame] | 5098 | struct inode *inode; |
Josef Bacik | a66e7cc | 2011-09-18 10:34:03 -0400 | [diff] [blame] | 5099 | |
Tsutomu Itoh | 5662344b3 | 2013-12-13 09:51:42 +0900 | [diff] [blame] | 5100 | inode = btrfs_lookup_dentry(dir, dentry); |
| 5101 | if (IS_ERR(inode)) { |
| 5102 | if (PTR_ERR(inode) == -ENOENT) |
| 5103 | inode = NULL; |
| 5104 | else |
| 5105 | return ERR_CAST(inode); |
| 5106 | } |
| 5107 | |
| 5108 | return d_splice_alias(inode, dentry); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5109 | } |
| 5110 | |
Miao Xie | 16cdcec | 2011-04-22 18:12:22 +0800 | [diff] [blame] | 5111 | unsigned char btrfs_filetype_table[] = { |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5112 | DT_UNKNOWN, DT_REG, DT_DIR, DT_CHR, DT_BLK, DT_FIFO, DT_SOCK, DT_LNK |
| 5113 | }; |
| 5114 | |
Al Viro | 9cdda8d | 2013-05-22 16:48:09 -0400 | [diff] [blame] | 5115 | static int btrfs_real_readdir(struct file *file, struct dir_context *ctx) |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5116 | { |
Al Viro | 9cdda8d | 2013-05-22 16:48:09 -0400 | [diff] [blame] | 5117 | struct inode *inode = file_inode(file); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5118 | struct btrfs_root *root = BTRFS_I(inode)->root; |
| 5119 | struct btrfs_item *item; |
| 5120 | struct btrfs_dir_item *di; |
| 5121 | struct btrfs_key key; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 5122 | struct btrfs_key found_key; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5123 | struct btrfs_path *path; |
Miao Xie | 16cdcec | 2011-04-22 18:12:22 +0800 | [diff] [blame] | 5124 | struct list_head ins_list; |
| 5125 | struct list_head del_list; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5126 | int ret; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 5127 | struct extent_buffer *leaf; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5128 | int slot; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5129 | unsigned char d_type; |
| 5130 | int over = 0; |
| 5131 | u32 di_cur; |
| 5132 | u32 di_total; |
| 5133 | u32 di_len; |
| 5134 | int key_type = BTRFS_DIR_INDEX_KEY; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 5135 | char tmp_name[32]; |
| 5136 | char *name_ptr; |
| 5137 | int name_len; |
Al Viro | 9cdda8d | 2013-05-22 16:48:09 -0400 | [diff] [blame] | 5138 | int is_curr = 0; /* ctx->pos points to the current index? */ |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5139 | |
| 5140 | /* FIXME, use a real flag for deciding about the key type */ |
| 5141 | if (root->fs_info->tree_root == root) |
| 5142 | key_type = BTRFS_DIR_ITEM_KEY; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 5143 | |
Al Viro | 9cdda8d | 2013-05-22 16:48:09 -0400 | [diff] [blame] | 5144 | if (!dir_emit_dots(file, ctx)) |
| 5145 | return 0; |
| 5146 | |
David Woodhouse | 49593bf | 2008-08-17 17:08:36 +0100 | [diff] [blame] | 5147 | path = btrfs_alloc_path(); |
Miao Xie | 16cdcec | 2011-04-22 18:12:22 +0800 | [diff] [blame] | 5148 | if (!path) |
| 5149 | return -ENOMEM; |
Chris Mason | ff5714c | 2011-05-28 07:00:39 -0400 | [diff] [blame] | 5150 | |
Josef Bacik | 026fd31 | 2011-05-13 10:32:11 -0400 | [diff] [blame] | 5151 | path->reada = 1; |
David Woodhouse | 49593bf | 2008-08-17 17:08:36 +0100 | [diff] [blame] | 5152 | |
Miao Xie | 16cdcec | 2011-04-22 18:12:22 +0800 | [diff] [blame] | 5153 | if (key_type == BTRFS_DIR_INDEX_KEY) { |
| 5154 | INIT_LIST_HEAD(&ins_list); |
| 5155 | INIT_LIST_HEAD(&del_list); |
| 5156 | btrfs_get_delayed_items(inode, &ins_list, &del_list); |
| 5157 | } |
| 5158 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5159 | btrfs_set_key_type(&key, key_type); |
Al Viro | 9cdda8d | 2013-05-22 16:48:09 -0400 | [diff] [blame] | 5160 | key.offset = ctx->pos; |
Li Zefan | 33345d01 | 2011-04-20 10:31:50 +0800 | [diff] [blame] | 5161 | key.objectid = btrfs_ino(inode); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 5162 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5163 | ret = btrfs_search_slot(NULL, root, &key, path, 0, 0); |
| 5164 | if (ret < 0) |
| 5165 | goto err; |
David Woodhouse | 49593bf | 2008-08-17 17:08:36 +0100 | [diff] [blame] | 5166 | |
| 5167 | while (1) { |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 5168 | leaf = path->nodes[0]; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5169 | slot = path->slots[0]; |
Li Zefan | b9e03af | 2011-03-23 10:43:58 +0800 | [diff] [blame] | 5170 | if (slot >= btrfs_header_nritems(leaf)) { |
| 5171 | ret = btrfs_next_leaf(root, path); |
| 5172 | if (ret < 0) |
| 5173 | goto err; |
| 5174 | else if (ret > 0) |
| 5175 | break; |
| 5176 | continue; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5177 | } |
Chris Mason | 3de4586 | 2008-11-17 21:02:50 -0500 | [diff] [blame] | 5178 | |
Ross Kirk | dd3cc16 | 2013-09-16 15:58:09 +0100 | [diff] [blame] | 5179 | item = btrfs_item_nr(slot); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 5180 | btrfs_item_key_to_cpu(leaf, &found_key, slot); |
| 5181 | |
| 5182 | if (found_key.objectid != key.objectid) |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5183 | break; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 5184 | if (btrfs_key_type(&found_key) != key_type) |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5185 | break; |
Al Viro | 9cdda8d | 2013-05-22 16:48:09 -0400 | [diff] [blame] | 5186 | if (found_key.offset < ctx->pos) |
Li Zefan | b9e03af | 2011-03-23 10:43:58 +0800 | [diff] [blame] | 5187 | goto next; |
Miao Xie | 16cdcec | 2011-04-22 18:12:22 +0800 | [diff] [blame] | 5188 | if (key_type == BTRFS_DIR_INDEX_KEY && |
| 5189 | btrfs_should_delete_dir_index(&del_list, |
| 5190 | found_key.offset)) |
| 5191 | goto next; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 5192 | |
Al Viro | 9cdda8d | 2013-05-22 16:48:09 -0400 | [diff] [blame] | 5193 | ctx->pos = found_key.offset; |
Miao Xie | 16cdcec | 2011-04-22 18:12:22 +0800 | [diff] [blame] | 5194 | is_curr = 1; |
David Woodhouse | 49593bf | 2008-08-17 17:08:36 +0100 | [diff] [blame] | 5195 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5196 | di = btrfs_item_ptr(leaf, slot, struct btrfs_dir_item); |
| 5197 | di_cur = 0; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 5198 | di_total = btrfs_item_size(leaf, item); |
David Woodhouse | 49593bf | 2008-08-17 17:08:36 +0100 | [diff] [blame] | 5199 | |
| 5200 | while (di_cur < di_total) { |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 5201 | struct btrfs_key location; |
| 5202 | |
Josef Bacik | 22a94d4 | 2011-03-16 16:47:17 -0400 | [diff] [blame] | 5203 | if (verify_dir_item(root, leaf, di)) |
| 5204 | break; |
| 5205 | |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 5206 | name_len = btrfs_dir_name_len(leaf, di); |
David Woodhouse | 49593bf | 2008-08-17 17:08:36 +0100 | [diff] [blame] | 5207 | if (name_len <= sizeof(tmp_name)) { |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 5208 | name_ptr = tmp_name; |
| 5209 | } else { |
| 5210 | name_ptr = kmalloc(name_len, GFP_NOFS); |
David Woodhouse | 49593bf | 2008-08-17 17:08:36 +0100 | [diff] [blame] | 5211 | if (!name_ptr) { |
| 5212 | ret = -ENOMEM; |
| 5213 | goto err; |
| 5214 | } |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 5215 | } |
| 5216 | read_extent_buffer(leaf, name_ptr, |
| 5217 | (unsigned long)(di + 1), name_len); |
| 5218 | |
| 5219 | d_type = btrfs_filetype_table[btrfs_dir_type(leaf, di)]; |
| 5220 | btrfs_dir_item_key_to_cpu(leaf, di, &location); |
Chris Mason | 3de4586 | 2008-11-17 21:02:50 -0500 | [diff] [blame] | 5221 | |
Josef Bacik | b4aff1f | 2011-06-28 16:18:59 -0400 | [diff] [blame] | 5222 | |
Chris Mason | 3de4586 | 2008-11-17 21:02:50 -0500 | [diff] [blame] | 5223 | /* is this a reference to our own snapshot? If so |
Arne Jansen | 8c9c2bf | 2012-02-25 09:09:30 +0100 | [diff] [blame] | 5224 | * skip it. |
| 5225 | * |
| 5226 | * In contrast to old kernels, we insert the snapshot's |
| 5227 | * dir item and dir index after it has been created, so |
| 5228 | * we won't find a reference to our own snapshot. We |
| 5229 | * still keep the following code for backward |
| 5230 | * compatibility. |
Chris Mason | 3de4586 | 2008-11-17 21:02:50 -0500 | [diff] [blame] | 5231 | */ |
| 5232 | if (location.type == BTRFS_ROOT_ITEM_KEY && |
| 5233 | location.objectid == root->root_key.objectid) { |
| 5234 | over = 0; |
| 5235 | goto skip; |
| 5236 | } |
Al Viro | 9cdda8d | 2013-05-22 16:48:09 -0400 | [diff] [blame] | 5237 | over = !dir_emit(ctx, name_ptr, name_len, |
| 5238 | location.objectid, d_type); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 5239 | |
Chris Mason | 3de4586 | 2008-11-17 21:02:50 -0500 | [diff] [blame] | 5240 | skip: |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 5241 | if (name_ptr != tmp_name) |
| 5242 | kfree(name_ptr); |
| 5243 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5244 | if (over) |
| 5245 | goto nopos; |
Josef Bacik | 5103e94 | 2007-11-16 11:45:54 -0500 | [diff] [blame] | 5246 | di_len = btrfs_dir_name_len(leaf, di) + |
David Woodhouse | 49593bf | 2008-08-17 17:08:36 +0100 | [diff] [blame] | 5247 | btrfs_dir_data_len(leaf, di) + sizeof(*di); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5248 | di_cur += di_len; |
| 5249 | di = (struct btrfs_dir_item *)((char *)di + di_len); |
| 5250 | } |
Li Zefan | b9e03af | 2011-03-23 10:43:58 +0800 | [diff] [blame] | 5251 | next: |
| 5252 | path->slots[0]++; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5253 | } |
David Woodhouse | 49593bf | 2008-08-17 17:08:36 +0100 | [diff] [blame] | 5254 | |
Miao Xie | 16cdcec | 2011-04-22 18:12:22 +0800 | [diff] [blame] | 5255 | if (key_type == BTRFS_DIR_INDEX_KEY) { |
| 5256 | if (is_curr) |
Al Viro | 9cdda8d | 2013-05-22 16:48:09 -0400 | [diff] [blame] | 5257 | ctx->pos++; |
| 5258 | ret = btrfs_readdir_delayed_dir_index(ctx, &ins_list); |
Miao Xie | 16cdcec | 2011-04-22 18:12:22 +0800 | [diff] [blame] | 5259 | if (ret) |
| 5260 | goto nopos; |
| 5261 | } |
| 5262 | |
David Woodhouse | 49593bf | 2008-08-17 17:08:36 +0100 | [diff] [blame] | 5263 | /* Reached end of directory/root. Bump pos past the last item. */ |
Zach Brown | db62efb | 2013-07-11 16:19:42 -0700 | [diff] [blame] | 5264 | ctx->pos++; |
| 5265 | |
| 5266 | /* |
| 5267 | * Stop new entries from being returned after we return the last |
| 5268 | * entry. |
| 5269 | * |
| 5270 | * New directory entries are assigned a strictly increasing |
| 5271 | * offset. This means that new entries created during readdir |
| 5272 | * are *guaranteed* to be seen in the future by that readdir. |
| 5273 | * This has broken buggy programs which operate on names as |
| 5274 | * they're returned by readdir. Until we re-use freed offsets |
| 5275 | * we have this hack to stop new entries from being returned |
| 5276 | * under the assumption that they'll never reach this huge |
| 5277 | * offset. |
| 5278 | * |
| 5279 | * This is being careful not to overflow 32bit loff_t unless the |
| 5280 | * last entry requires it because doing so has broken 32bit apps |
| 5281 | * in the past. |
| 5282 | */ |
| 5283 | if (key_type == BTRFS_DIR_INDEX_KEY) { |
| 5284 | if (ctx->pos >= INT_MAX) |
| 5285 | ctx->pos = LLONG_MAX; |
| 5286 | else |
| 5287 | ctx->pos = INT_MAX; |
| 5288 | } |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5289 | nopos: |
| 5290 | ret = 0; |
| 5291 | err: |
Miao Xie | 16cdcec | 2011-04-22 18:12:22 +0800 | [diff] [blame] | 5292 | if (key_type == BTRFS_DIR_INDEX_KEY) |
| 5293 | btrfs_put_delayed_items(&ins_list, &del_list); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5294 | btrfs_free_path(path); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5295 | return ret; |
| 5296 | } |
| 5297 | |
Christoph Hellwig | a9185b4 | 2010-03-05 09:21:37 +0100 | [diff] [blame] | 5298 | int btrfs_write_inode(struct inode *inode, struct writeback_control *wbc) |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5299 | { |
| 5300 | struct btrfs_root *root = BTRFS_I(inode)->root; |
| 5301 | struct btrfs_trans_handle *trans; |
| 5302 | int ret = 0; |
Josef Bacik | 0af3d00 | 2010-06-21 14:48:16 -0400 | [diff] [blame] | 5303 | bool nolock = false; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5304 | |
Josef Bacik | 72ac3c0 | 2012-05-23 14:13:11 -0400 | [diff] [blame] | 5305 | if (test_bit(BTRFS_INODE_DUMMY, &BTRFS_I(inode)->runtime_flags)) |
Chris Mason | 4ca8b41 | 2008-08-05 13:30:48 -0400 | [diff] [blame] | 5306 | return 0; |
| 5307 | |
Liu Bo | 83eea1f | 2012-07-10 05:28:39 -0600 | [diff] [blame] | 5308 | 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] | 5309 | nolock = true; |
Josef Bacik | 0af3d00 | 2010-06-21 14:48:16 -0400 | [diff] [blame] | 5310 | |
Christoph Hellwig | a9185b4 | 2010-03-05 09:21:37 +0100 | [diff] [blame] | 5311 | if (wbc->sync_mode == WB_SYNC_ALL) { |
Josef Bacik | 0af3d00 | 2010-06-21 14:48:16 -0400 | [diff] [blame] | 5312 | if (nolock) |
Josef Bacik | 7a7eaa4 | 2011-04-13 12:54:33 -0400 | [diff] [blame] | 5313 | trans = btrfs_join_transaction_nolock(root); |
Josef Bacik | 0af3d00 | 2010-06-21 14:48:16 -0400 | [diff] [blame] | 5314 | else |
Josef Bacik | 7a7eaa4 | 2011-04-13 12:54:33 -0400 | [diff] [blame] | 5315 | trans = btrfs_join_transaction(root); |
Tsutomu Itoh | 3612b49 | 2011-01-25 02:51:38 +0000 | [diff] [blame] | 5316 | if (IS_ERR(trans)) |
| 5317 | return PTR_ERR(trans); |
Miao Xie | a698d075 | 2012-09-20 01:51:59 -0600 | [diff] [blame] | 5318 | ret = btrfs_commit_transaction(trans, root); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5319 | } |
| 5320 | return ret; |
| 5321 | } |
| 5322 | |
| 5323 | /* |
Chris Mason | 54aa1f4 | 2007-06-22 14:16:25 -0400 | [diff] [blame] | 5324 | * This is somewhat expensive, updating the tree every time the |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5325 | * inode changes. But, it is most likely to find the inode in cache. |
| 5326 | * FIXME, needs more benchmarking...there are no reasons other than performance |
| 5327 | * to keep or drop this code. |
| 5328 | */ |
Eric Sandeen | 48a3b63 | 2013-04-25 20:41:01 +0000 | [diff] [blame] | 5329 | static int btrfs_dirty_inode(struct inode *inode) |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5330 | { |
| 5331 | struct btrfs_root *root = BTRFS_I(inode)->root; |
| 5332 | struct btrfs_trans_handle *trans; |
Yan, Zheng | 8929ecfa | 2010-05-16 10:49:58 -0400 | [diff] [blame] | 5333 | int ret; |
| 5334 | |
Josef Bacik | 72ac3c0 | 2012-05-23 14:13:11 -0400 | [diff] [blame] | 5335 | if (test_bit(BTRFS_INODE_DUMMY, &BTRFS_I(inode)->runtime_flags)) |
Josef Bacik | 22c44fe | 2011-11-30 10:45:38 -0500 | [diff] [blame] | 5336 | return 0; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5337 | |
Josef Bacik | 7a7eaa4 | 2011-04-13 12:54:33 -0400 | [diff] [blame] | 5338 | trans = btrfs_join_transaction(root); |
Josef Bacik | 22c44fe | 2011-11-30 10:45:38 -0500 | [diff] [blame] | 5339 | if (IS_ERR(trans)) |
| 5340 | return PTR_ERR(trans); |
Yan, Zheng | 8929ecfa | 2010-05-16 10:49:58 -0400 | [diff] [blame] | 5341 | |
| 5342 | ret = btrfs_update_inode(trans, root, inode); |
Chris Mason | 94b6044 | 2010-05-26 11:02:00 -0400 | [diff] [blame] | 5343 | if (ret && ret == -ENOSPC) { |
| 5344 | /* whoops, lets try again with the full transaction */ |
| 5345 | btrfs_end_transaction(trans, root); |
| 5346 | trans = btrfs_start_transaction(root, 1); |
Josef Bacik | 22c44fe | 2011-11-30 10:45:38 -0500 | [diff] [blame] | 5347 | if (IS_ERR(trans)) |
| 5348 | return PTR_ERR(trans); |
Yan, Zheng | 8929ecfa | 2010-05-16 10:49:58 -0400 | [diff] [blame] | 5349 | |
Chris Mason | 94b6044 | 2010-05-26 11:02:00 -0400 | [diff] [blame] | 5350 | ret = btrfs_update_inode(trans, root, inode); |
Chris Mason | 94b6044 | 2010-05-26 11:02:00 -0400 | [diff] [blame] | 5351 | } |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5352 | btrfs_end_transaction(trans, root); |
Miao Xie | 16cdcec | 2011-04-22 18:12:22 +0800 | [diff] [blame] | 5353 | if (BTRFS_I(inode)->delayed_node) |
| 5354 | btrfs_balance_delayed_items(root); |
Josef Bacik | 22c44fe | 2011-11-30 10:45:38 -0500 | [diff] [blame] | 5355 | |
| 5356 | return ret; |
| 5357 | } |
| 5358 | |
| 5359 | /* |
| 5360 | * This is a copy of file_update_time. We need this so we can return error on |
| 5361 | * ENOSPC for updating the inode in the case of file write and mmap writes. |
| 5362 | */ |
Josef Bacik | e41f941 | 2012-03-26 09:46:47 -0400 | [diff] [blame] | 5363 | static int btrfs_update_time(struct inode *inode, struct timespec *now, |
| 5364 | int flags) |
Josef Bacik | 22c44fe | 2011-11-30 10:45:38 -0500 | [diff] [blame] | 5365 | { |
Alexander Block | 2bc556528 | 2012-06-15 09:49:33 +0200 | [diff] [blame] | 5366 | struct btrfs_root *root = BTRFS_I(inode)->root; |
| 5367 | |
| 5368 | if (btrfs_root_readonly(root)) |
| 5369 | return -EROFS; |
| 5370 | |
Josef Bacik | e41f941 | 2012-03-26 09:46:47 -0400 | [diff] [blame] | 5371 | if (flags & S_VERSION) |
Josef Bacik | 22c44fe | 2011-11-30 10:45:38 -0500 | [diff] [blame] | 5372 | inode_inc_iversion(inode); |
Josef Bacik | e41f941 | 2012-03-26 09:46:47 -0400 | [diff] [blame] | 5373 | if (flags & S_CTIME) |
| 5374 | inode->i_ctime = *now; |
| 5375 | if (flags & S_MTIME) |
| 5376 | inode->i_mtime = *now; |
| 5377 | if (flags & S_ATIME) |
| 5378 | inode->i_atime = *now; |
| 5379 | return btrfs_dirty_inode(inode); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5380 | } |
| 5381 | |
Chris Mason | d352ac6 | 2008-09-29 15:18:18 -0400 | [diff] [blame] | 5382 | /* |
| 5383 | * find the highest existing sequence number in a directory |
| 5384 | * and then set the in-memory index_cnt variable to reflect |
| 5385 | * free sequence numbers |
| 5386 | */ |
Josef Bacik | aec7477 | 2008-07-24 12:12:38 -0400 | [diff] [blame] | 5387 | static int btrfs_set_inode_index_count(struct inode *inode) |
| 5388 | { |
| 5389 | struct btrfs_root *root = BTRFS_I(inode)->root; |
| 5390 | struct btrfs_key key, found_key; |
| 5391 | struct btrfs_path *path; |
| 5392 | struct extent_buffer *leaf; |
| 5393 | int ret; |
| 5394 | |
Li Zefan | 33345d01 | 2011-04-20 10:31:50 +0800 | [diff] [blame] | 5395 | key.objectid = btrfs_ino(inode); |
Josef Bacik | aec7477 | 2008-07-24 12:12:38 -0400 | [diff] [blame] | 5396 | btrfs_set_key_type(&key, BTRFS_DIR_INDEX_KEY); |
| 5397 | key.offset = (u64)-1; |
| 5398 | |
| 5399 | path = btrfs_alloc_path(); |
| 5400 | if (!path) |
| 5401 | return -ENOMEM; |
| 5402 | |
| 5403 | ret = btrfs_search_slot(NULL, root, &key, path, 0, 0); |
| 5404 | if (ret < 0) |
| 5405 | goto out; |
| 5406 | /* FIXME: we should be able to handle this */ |
| 5407 | if (ret == 0) |
| 5408 | goto out; |
| 5409 | ret = 0; |
| 5410 | |
| 5411 | /* |
| 5412 | * MAGIC NUMBER EXPLANATION: |
| 5413 | * since we search a directory based on f_pos we have to start at 2 |
| 5414 | * since '.' and '..' have f_pos of 0 and 1 respectively, so everybody |
| 5415 | * else has to start at 2 |
| 5416 | */ |
| 5417 | if (path->slots[0] == 0) { |
| 5418 | BTRFS_I(inode)->index_cnt = 2; |
| 5419 | goto out; |
| 5420 | } |
| 5421 | |
| 5422 | path->slots[0]--; |
| 5423 | |
| 5424 | leaf = path->nodes[0]; |
| 5425 | btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]); |
| 5426 | |
Li Zefan | 33345d01 | 2011-04-20 10:31:50 +0800 | [diff] [blame] | 5427 | if (found_key.objectid != btrfs_ino(inode) || |
Josef Bacik | aec7477 | 2008-07-24 12:12:38 -0400 | [diff] [blame] | 5428 | btrfs_key_type(&found_key) != BTRFS_DIR_INDEX_KEY) { |
| 5429 | BTRFS_I(inode)->index_cnt = 2; |
| 5430 | goto out; |
| 5431 | } |
| 5432 | |
| 5433 | BTRFS_I(inode)->index_cnt = found_key.offset + 1; |
| 5434 | out: |
| 5435 | btrfs_free_path(path); |
| 5436 | return ret; |
| 5437 | } |
| 5438 | |
Chris Mason | d352ac6 | 2008-09-29 15:18:18 -0400 | [diff] [blame] | 5439 | /* |
| 5440 | * helper to find a free sequence number in a given directory. This current |
| 5441 | * code is very simple, later versions will do smarter things in the btree |
| 5442 | */ |
Chris Mason | 3de4586 | 2008-11-17 21:02:50 -0500 | [diff] [blame] | 5443 | int btrfs_set_inode_index(struct inode *dir, u64 *index) |
Josef Bacik | aec7477 | 2008-07-24 12:12:38 -0400 | [diff] [blame] | 5444 | { |
| 5445 | int ret = 0; |
| 5446 | |
| 5447 | if (BTRFS_I(dir)->index_cnt == (u64)-1) { |
Miao Xie | 16cdcec | 2011-04-22 18:12:22 +0800 | [diff] [blame] | 5448 | ret = btrfs_inode_delayed_dir_index_count(dir); |
| 5449 | if (ret) { |
| 5450 | ret = btrfs_set_inode_index_count(dir); |
| 5451 | if (ret) |
| 5452 | return ret; |
| 5453 | } |
Josef Bacik | aec7477 | 2008-07-24 12:12:38 -0400 | [diff] [blame] | 5454 | } |
| 5455 | |
Chris Mason | 00e4e6b | 2008-08-05 11:18:09 -0400 | [diff] [blame] | 5456 | *index = BTRFS_I(dir)->index_cnt; |
Josef Bacik | aec7477 | 2008-07-24 12:12:38 -0400 | [diff] [blame] | 5457 | BTRFS_I(dir)->index_cnt++; |
| 5458 | |
| 5459 | return ret; |
| 5460 | } |
| 5461 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5462 | static struct inode *btrfs_new_inode(struct btrfs_trans_handle *trans, |
| 5463 | struct btrfs_root *root, |
Josef Bacik | aec7477 | 2008-07-24 12:12:38 -0400 | [diff] [blame] | 5464 | struct inode *dir, |
Chris Mason | 9c58309 | 2008-01-29 15:15:18 -0500 | [diff] [blame] | 5465 | const char *name, int name_len, |
Al Viro | 175a4eb | 2011-07-26 03:30:54 -0400 | [diff] [blame] | 5466 | u64 ref_objectid, u64 objectid, |
| 5467 | umode_t mode, u64 *index) |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5468 | { |
| 5469 | struct inode *inode; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 5470 | struct btrfs_inode_item *inode_item; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5471 | struct btrfs_key *location; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 5472 | struct btrfs_path *path; |
Chris Mason | 9c58309 | 2008-01-29 15:15:18 -0500 | [diff] [blame] | 5473 | struct btrfs_inode_ref *ref; |
| 5474 | struct btrfs_key key[2]; |
| 5475 | u32 sizes[2]; |
| 5476 | unsigned long ptr; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5477 | int ret; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5478 | |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 5479 | path = btrfs_alloc_path(); |
Mark Fasheh | d8926bb | 2011-07-13 10:38:47 -0700 | [diff] [blame] | 5480 | if (!path) |
| 5481 | return ERR_PTR(-ENOMEM); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 5482 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5483 | inode = new_inode(root->fs_info->sb); |
Yoshinori Sano | 8fb2764 | 2011-04-09 02:30:07 +0000 | [diff] [blame] | 5484 | if (!inode) { |
| 5485 | btrfs_free_path(path); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5486 | return ERR_PTR(-ENOMEM); |
Yoshinori Sano | 8fb2764 | 2011-04-09 02:30:07 +0000 | [diff] [blame] | 5487 | } |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5488 | |
Li Zefan | 581bb05 | 2011-04-20 10:06:11 +0800 | [diff] [blame] | 5489 | /* |
| 5490 | * we have to initialize this early, so we can reclaim the inode |
| 5491 | * number if we fail afterwards in this function. |
| 5492 | */ |
| 5493 | inode->i_ino = objectid; |
| 5494 | |
Josef Bacik | aec7477 | 2008-07-24 12:12:38 -0400 | [diff] [blame] | 5495 | if (dir) { |
liubo | 1abe9b8 | 2011-03-24 11:18:59 +0000 | [diff] [blame] | 5496 | trace_btrfs_inode_request(dir); |
| 5497 | |
Chris Mason | 3de4586 | 2008-11-17 21:02:50 -0500 | [diff] [blame] | 5498 | ret = btrfs_set_inode_index(dir, index); |
Shen Feng | 0977143 | 2009-04-02 16:46:06 -0400 | [diff] [blame] | 5499 | if (ret) { |
Yoshinori Sano | 8fb2764 | 2011-04-09 02:30:07 +0000 | [diff] [blame] | 5500 | btrfs_free_path(path); |
Shen Feng | 0977143 | 2009-04-02 16:46:06 -0400 | [diff] [blame] | 5501 | iput(inode); |
Josef Bacik | aec7477 | 2008-07-24 12:12:38 -0400 | [diff] [blame] | 5502 | return ERR_PTR(ret); |
Shen Feng | 0977143 | 2009-04-02 16:46:06 -0400 | [diff] [blame] | 5503 | } |
Josef Bacik | aec7477 | 2008-07-24 12:12:38 -0400 | [diff] [blame] | 5504 | } |
| 5505 | /* |
| 5506 | * index_cnt is ignored for everything but a dir, |
| 5507 | * btrfs_get_inode_index_count has an explanation for the magic |
| 5508 | * number |
| 5509 | */ |
| 5510 | BTRFS_I(inode)->index_cnt = 2; |
Miao Xie | 67de117 | 2013-12-26 13:07:06 +0800 | [diff] [blame^] | 5511 | BTRFS_I(inode)->dir_index = *index; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5512 | BTRFS_I(inode)->root = root; |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 5513 | BTRFS_I(inode)->generation = trans->transid; |
Josef Bacik | 7619585 | 2010-11-19 02:18:02 +0000 | [diff] [blame] | 5514 | inode->i_generation = BTRFS_I(inode)->generation; |
Chris Mason | b888db2 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 5515 | |
Josef Bacik | 5dc562c | 2012-08-17 13:14:17 -0400 | [diff] [blame] | 5516 | /* |
| 5517 | * We could have gotten an inode number from somebody who was fsynced |
| 5518 | * and then removed in this same transaction, so let's just set full |
| 5519 | * sync since it will be a full sync anyway and this will blow away the |
| 5520 | * old info in the log. |
| 5521 | */ |
| 5522 | set_bit(BTRFS_INODE_NEEDS_FULL_SYNC, &BTRFS_I(inode)->runtime_flags); |
| 5523 | |
Chris Mason | 9c58309 | 2008-01-29 15:15:18 -0500 | [diff] [blame] | 5524 | key[0].objectid = objectid; |
| 5525 | btrfs_set_key_type(&key[0], BTRFS_INODE_ITEM_KEY); |
| 5526 | key[0].offset = 0; |
| 5527 | |
Mark Fasheh | f186373 | 2012-08-08 11:32:27 -0700 | [diff] [blame] | 5528 | /* |
| 5529 | * Start new inodes with an inode_ref. This is slightly more |
| 5530 | * efficient for small numbers of hard links since they will |
| 5531 | * be packed into one item. Extended refs will kick in if we |
| 5532 | * add more hard links than can fit in the ref item. |
| 5533 | */ |
Chris Mason | 9c58309 | 2008-01-29 15:15:18 -0500 | [diff] [blame] | 5534 | key[1].objectid = objectid; |
| 5535 | btrfs_set_key_type(&key[1], BTRFS_INODE_REF_KEY); |
| 5536 | key[1].offset = ref_objectid; |
| 5537 | |
| 5538 | sizes[0] = sizeof(struct btrfs_inode_item); |
| 5539 | sizes[1] = name_len + sizeof(*ref); |
| 5540 | |
Chris Mason | b947343 | 2009-03-13 11:00:37 -0400 | [diff] [blame] | 5541 | path->leave_spinning = 1; |
Chris Mason | 9c58309 | 2008-01-29 15:15:18 -0500 | [diff] [blame] | 5542 | ret = btrfs_insert_empty_items(trans, root, path, key, sizes, 2); |
| 5543 | if (ret != 0) |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 5544 | goto fail; |
| 5545 | |
Dmitry Monakhov | ecc11fab | 2010-03-04 17:31:47 +0300 | [diff] [blame] | 5546 | inode_init_owner(inode, dir, mode); |
Yan Zheng | a76a3cd | 2008-10-09 11:46:29 -0400 | [diff] [blame] | 5547 | inode_set_bytes(inode, 0); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5548 | inode->i_mtime = inode->i_atime = inode->i_ctime = CURRENT_TIME; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 5549 | inode_item = btrfs_item_ptr(path->nodes[0], path->slots[0], |
| 5550 | struct btrfs_inode_item); |
Li Zefan | 293f7e0 | 2012-07-10 00:58:58 -0600 | [diff] [blame] | 5551 | memset_extent_buffer(path->nodes[0], 0, (unsigned long)inode_item, |
| 5552 | sizeof(*inode_item)); |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 5553 | fill_inode_item(trans, path->nodes[0], inode_item, inode); |
Chris Mason | 9c58309 | 2008-01-29 15:15:18 -0500 | [diff] [blame] | 5554 | |
| 5555 | ref = btrfs_item_ptr(path->nodes[0], path->slots[0] + 1, |
| 5556 | struct btrfs_inode_ref); |
| 5557 | btrfs_set_inode_ref_name_len(path->nodes[0], ref, name_len); |
Chris Mason | 00e4e6b | 2008-08-05 11:18:09 -0400 | [diff] [blame] | 5558 | btrfs_set_inode_ref_index(path->nodes[0], ref, *index); |
Chris Mason | 9c58309 | 2008-01-29 15:15:18 -0500 | [diff] [blame] | 5559 | ptr = (unsigned long)(ref + 1); |
| 5560 | write_extent_buffer(path->nodes[0], name, ptr, name_len); |
| 5561 | |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 5562 | btrfs_mark_buffer_dirty(path->nodes[0]); |
| 5563 | btrfs_free_path(path); |
| 5564 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5565 | location = &BTRFS_I(inode)->location; |
| 5566 | location->objectid = objectid; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5567 | location->offset = 0; |
| 5568 | btrfs_set_key_type(location, BTRFS_INODE_ITEM_KEY); |
| 5569 | |
Christoph Hellwig | 6cbff00 | 2009-04-17 10:37:41 +0200 | [diff] [blame] | 5570 | btrfs_inherit_iflags(inode, dir); |
| 5571 | |
Al Viro | 569254b | 2011-07-24 17:08:40 -0400 | [diff] [blame] | 5572 | if (S_ISREG(mode)) { |
Chris Mason | 9427216 | 2009-07-02 12:26:06 -0400 | [diff] [blame] | 5573 | if (btrfs_test_opt(root, NODATASUM)) |
| 5574 | BTRFS_I(inode)->flags |= BTRFS_INODE_NODATASUM; |
Liu Bo | 213490b | 2012-09-11 08:33:50 -0600 | [diff] [blame] | 5575 | if (btrfs_test_opt(root, NODATACOW)) |
Josef Bacik | f2bdf9a | 2013-02-21 15:28:28 -0500 | [diff] [blame] | 5576 | BTRFS_I(inode)->flags |= BTRFS_INODE_NODATACOW | |
| 5577 | BTRFS_INODE_NODATASUM; |
Chris Mason | 9427216 | 2009-07-02 12:26:06 -0400 | [diff] [blame] | 5578 | } |
| 5579 | |
Filipe David Borba Manana | 778ba82 | 2013-10-06 22:22:33 +0100 | [diff] [blame] | 5580 | btrfs_insert_inode_hash(inode); |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 5581 | inode_tree_add(inode); |
liubo | 1abe9b8 | 2011-03-24 11:18:59 +0000 | [diff] [blame] | 5582 | |
| 5583 | trace_btrfs_inode_new(inode); |
Chris Mason | 1973f0f | 2011-06-24 13:13:29 -0400 | [diff] [blame] | 5584 | btrfs_set_inode_last_trans(trans, inode); |
liubo | 1abe9b8 | 2011-03-24 11:18:59 +0000 | [diff] [blame] | 5585 | |
Alexander Block | 8ea05e3 | 2012-07-25 17:35:53 +0200 | [diff] [blame] | 5586 | btrfs_update_root_times(trans, root); |
| 5587 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5588 | return inode; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 5589 | fail: |
Josef Bacik | aec7477 | 2008-07-24 12:12:38 -0400 | [diff] [blame] | 5590 | if (dir) |
| 5591 | BTRFS_I(dir)->index_cnt--; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 5592 | btrfs_free_path(path); |
Shen Feng | 0977143 | 2009-04-02 16:46:06 -0400 | [diff] [blame] | 5593 | iput(inode); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 5594 | return ERR_PTR(ret); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5595 | } |
| 5596 | |
| 5597 | static inline u8 btrfs_inode_type(struct inode *inode) |
| 5598 | { |
| 5599 | return btrfs_type_by_mode[(inode->i_mode & S_IFMT) >> S_SHIFT]; |
| 5600 | } |
| 5601 | |
Chris Mason | d352ac6 | 2008-09-29 15:18:18 -0400 | [diff] [blame] | 5602 | /* |
| 5603 | * utility function to add 'inode' into 'parent_inode' with |
| 5604 | * a give name and a given sequence number. |
| 5605 | * if 'add_backref' is true, also insert a backref from the |
| 5606 | * inode to the parent directory. |
| 5607 | */ |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 5608 | int btrfs_add_link(struct btrfs_trans_handle *trans, |
| 5609 | struct inode *parent_inode, struct inode *inode, |
| 5610 | const char *name, int name_len, int add_backref, u64 index) |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5611 | { |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 5612 | int ret = 0; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5613 | struct btrfs_key key; |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 5614 | struct btrfs_root *root = BTRFS_I(parent_inode)->root; |
Li Zefan | 33345d01 | 2011-04-20 10:31:50 +0800 | [diff] [blame] | 5615 | u64 ino = btrfs_ino(inode); |
| 5616 | u64 parent_ino = btrfs_ino(parent_inode); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 5617 | |
Li Zefan | 33345d01 | 2011-04-20 10:31:50 +0800 | [diff] [blame] | 5618 | if (unlikely(ino == BTRFS_FIRST_FREE_OBJECTID)) { |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 5619 | memcpy(&key, &BTRFS_I(inode)->root->root_key, sizeof(key)); |
| 5620 | } else { |
Li Zefan | 33345d01 | 2011-04-20 10:31:50 +0800 | [diff] [blame] | 5621 | key.objectid = ino; |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 5622 | btrfs_set_key_type(&key, BTRFS_INODE_ITEM_KEY); |
| 5623 | key.offset = 0; |
| 5624 | } |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5625 | |
Li Zefan | 33345d01 | 2011-04-20 10:31:50 +0800 | [diff] [blame] | 5626 | if (unlikely(ino == BTRFS_FIRST_FREE_OBJECTID)) { |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 5627 | ret = btrfs_add_root_ref(trans, root->fs_info->tree_root, |
| 5628 | key.objectid, root->root_key.objectid, |
Li Zefan | 33345d01 | 2011-04-20 10:31:50 +0800 | [diff] [blame] | 5629 | parent_ino, index, name, name_len); |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 5630 | } else if (add_backref) { |
Li Zefan | 33345d01 | 2011-04-20 10:31:50 +0800 | [diff] [blame] | 5631 | ret = btrfs_insert_inode_ref(trans, root, name, name_len, ino, |
| 5632 | parent_ino, index); |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 5633 | } |
| 5634 | |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 5635 | /* Nothing to clean up yet */ |
| 5636 | if (ret) |
| 5637 | return ret; |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 5638 | |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 5639 | ret = btrfs_insert_dir_item(trans, root, name, name_len, |
| 5640 | parent_inode, &key, |
| 5641 | btrfs_inode_type(inode), index); |
Chris Mason | 9c52057 | 2012-12-17 14:26:57 -0500 | [diff] [blame] | 5642 | if (ret == -EEXIST || ret == -EOVERFLOW) |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 5643 | goto fail_dir_item; |
| 5644 | else if (ret) { |
| 5645 | btrfs_abort_transaction(trans, root, ret); |
| 5646 | return ret; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5647 | } |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 5648 | |
| 5649 | btrfs_i_size_write(parent_inode, parent_inode->i_size + |
| 5650 | name_len * 2); |
Josef Bacik | 0c4d2d9 | 2012-04-05 15:03:02 -0400 | [diff] [blame] | 5651 | inode_inc_iversion(parent_inode); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 5652 | parent_inode->i_mtime = parent_inode->i_ctime = CURRENT_TIME; |
| 5653 | ret = btrfs_update_inode(trans, root, parent_inode); |
| 5654 | if (ret) |
| 5655 | btrfs_abort_transaction(trans, root, ret); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5656 | return ret; |
Chris Mason | fe66a05 | 2012-02-20 08:40:56 -0500 | [diff] [blame] | 5657 | |
| 5658 | fail_dir_item: |
| 5659 | if (unlikely(ino == BTRFS_FIRST_FREE_OBJECTID)) { |
| 5660 | u64 local_index; |
| 5661 | int err; |
| 5662 | err = btrfs_del_root_ref(trans, root->fs_info->tree_root, |
| 5663 | key.objectid, root->root_key.objectid, |
| 5664 | parent_ino, &local_index, name, name_len); |
| 5665 | |
| 5666 | } else if (add_backref) { |
| 5667 | u64 local_index; |
| 5668 | int err; |
| 5669 | |
| 5670 | err = btrfs_del_inode_ref(trans, root, name, name_len, |
| 5671 | ino, parent_ino, &local_index); |
| 5672 | } |
| 5673 | return ret; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5674 | } |
| 5675 | |
| 5676 | static int btrfs_add_nondir(struct btrfs_trans_handle *trans, |
Josef Bacik | a1b075d | 2010-11-19 20:36:11 +0000 | [diff] [blame] | 5677 | struct inode *dir, struct dentry *dentry, |
| 5678 | struct inode *inode, int backref, u64 index) |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5679 | { |
Josef Bacik | a1b075d | 2010-11-19 20:36:11 +0000 | [diff] [blame] | 5680 | int err = btrfs_add_link(trans, dir, inode, |
| 5681 | dentry->d_name.name, dentry->d_name.len, |
| 5682 | backref, index); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5683 | if (err > 0) |
| 5684 | err = -EEXIST; |
| 5685 | return err; |
| 5686 | } |
| 5687 | |
Josef Bacik | 618e21d | 2007-07-11 10:18:17 -0400 | [diff] [blame] | 5688 | static int btrfs_mknod(struct inode *dir, struct dentry *dentry, |
Al Viro | 1a67aaf | 2011-07-26 01:52:52 -0400 | [diff] [blame] | 5689 | umode_t mode, dev_t rdev) |
Josef Bacik | 618e21d | 2007-07-11 10:18:17 -0400 | [diff] [blame] | 5690 | { |
| 5691 | struct btrfs_trans_handle *trans; |
| 5692 | struct btrfs_root *root = BTRFS_I(dir)->root; |
Chris Mason | 1832a6d | 2007-12-21 16:27:21 -0500 | [diff] [blame] | 5693 | struct inode *inode = NULL; |
Josef Bacik | 618e21d | 2007-07-11 10:18:17 -0400 | [diff] [blame] | 5694 | int err; |
| 5695 | int drop_inode = 0; |
| 5696 | u64 objectid; |
Chris Mason | 00e4e6b | 2008-08-05 11:18:09 -0400 | [diff] [blame] | 5697 | u64 index = 0; |
Josef Bacik | 618e21d | 2007-07-11 10:18:17 -0400 | [diff] [blame] | 5698 | |
| 5699 | if (!new_valid_dev(rdev)) |
| 5700 | return -EINVAL; |
| 5701 | |
Josef Bacik | 9ed74f2 | 2009-09-11 16:12:44 -0400 | [diff] [blame] | 5702 | /* |
| 5703 | * 2 for inode item and ref |
| 5704 | * 2 for dir items |
| 5705 | * 1 for xattr if selinux is on |
| 5706 | */ |
Yan, Zheng | a22285a | 2010-05-16 10:48:46 -0400 | [diff] [blame] | 5707 | trans = btrfs_start_transaction(root, 5); |
| 5708 | if (IS_ERR(trans)) |
| 5709 | return PTR_ERR(trans); |
Chris Mason | 1832a6d | 2007-12-21 16:27:21 -0500 | [diff] [blame] | 5710 | |
Li Zefan | 581bb05 | 2011-04-20 10:06:11 +0800 | [diff] [blame] | 5711 | err = btrfs_find_free_ino(root, &objectid); |
| 5712 | if (err) |
| 5713 | goto out_unlock; |
| 5714 | |
Josef Bacik | aec7477 | 2008-07-24 12:12:38 -0400 | [diff] [blame] | 5715 | inode = btrfs_new_inode(trans, root, dir, dentry->d_name.name, |
Li Zefan | 33345d01 | 2011-04-20 10:31:50 +0800 | [diff] [blame] | 5716 | dentry->d_name.len, btrfs_ino(dir), objectid, |
Josef Bacik | d82a6f1d | 2011-05-11 15:26:06 -0400 | [diff] [blame] | 5717 | mode, &index); |
Tsutomu Itoh | 7cf96da | 2011-04-25 19:43:53 -0400 | [diff] [blame] | 5718 | if (IS_ERR(inode)) { |
| 5719 | err = PTR_ERR(inode); |
Josef Bacik | 618e21d | 2007-07-11 10:18:17 -0400 | [diff] [blame] | 5720 | goto out_unlock; |
Tsutomu Itoh | 7cf96da | 2011-04-25 19:43:53 -0400 | [diff] [blame] | 5721 | } |
Josef Bacik | 618e21d | 2007-07-11 10:18:17 -0400 | [diff] [blame] | 5722 | |
Eric Paris | 2a7dba3 | 2011-02-01 11:05:39 -0500 | [diff] [blame] | 5723 | err = btrfs_init_inode_security(trans, inode, dir, &dentry->d_name); |
Josef Bacik | 33268ea | 2008-07-24 12:16:36 -0400 | [diff] [blame] | 5724 | if (err) { |
| 5725 | drop_inode = 1; |
| 5726 | goto out_unlock; |
| 5727 | } |
| 5728 | |
Casey Schaufler | ad19db7 | 2011-12-15 10:09:07 -0500 | [diff] [blame] | 5729 | /* |
| 5730 | * If the active LSM wants to access the inode during |
| 5731 | * d_instantiate it needs these. Smack checks to see |
| 5732 | * if the filesystem supports xattrs by looking at the |
| 5733 | * ops vector. |
| 5734 | */ |
| 5735 | |
| 5736 | inode->i_op = &btrfs_special_inode_operations; |
Josef Bacik | a1b075d | 2010-11-19 20:36:11 +0000 | [diff] [blame] | 5737 | err = btrfs_add_nondir(trans, dir, dentry, inode, 0, index); |
Josef Bacik | 618e21d | 2007-07-11 10:18:17 -0400 | [diff] [blame] | 5738 | if (err) |
| 5739 | drop_inode = 1; |
| 5740 | else { |
Josef Bacik | 618e21d | 2007-07-11 10:18:17 -0400 | [diff] [blame] | 5741 | init_special_inode(inode, inode->i_mode, rdev); |
Yan | 1b4ab1b | 2007-08-29 09:11:44 -0400 | [diff] [blame] | 5742 | btrfs_update_inode(trans, root, inode); |
Al Viro | 08c422c | 2011-12-23 07:58:13 -0500 | [diff] [blame] | 5743 | d_instantiate(dentry, inode); |
Josef Bacik | 618e21d | 2007-07-11 10:18:17 -0400 | [diff] [blame] | 5744 | } |
Josef Bacik | 618e21d | 2007-07-11 10:18:17 -0400 | [diff] [blame] | 5745 | out_unlock: |
Josef Bacik | 7ad85bb | 2012-01-12 19:10:12 -0500 | [diff] [blame] | 5746 | btrfs_end_transaction(trans, root); |
Liu Bo | b53d3f5 | 2012-11-14 14:34:34 +0000 | [diff] [blame] | 5747 | btrfs_btree_balance_dirty(root); |
Josef Bacik | 618e21d | 2007-07-11 10:18:17 -0400 | [diff] [blame] | 5748 | if (drop_inode) { |
| 5749 | inode_dec_link_count(inode); |
| 5750 | iput(inode); |
| 5751 | } |
Josef Bacik | 618e21d | 2007-07-11 10:18:17 -0400 | [diff] [blame] | 5752 | return err; |
| 5753 | } |
| 5754 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5755 | static int btrfs_create(struct inode *dir, struct dentry *dentry, |
Al Viro | ebfc3b4 | 2012-06-10 18:05:36 -0400 | [diff] [blame] | 5756 | umode_t mode, bool excl) |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5757 | { |
| 5758 | struct btrfs_trans_handle *trans; |
| 5759 | struct btrfs_root *root = BTRFS_I(dir)->root; |
Chris Mason | 1832a6d | 2007-12-21 16:27:21 -0500 | [diff] [blame] | 5760 | struct inode *inode = NULL; |
Filipe Brandenburger | 43baa57 | 2012-11-30 03:40:09 +0000 | [diff] [blame] | 5761 | int drop_inode_on_err = 0; |
Yan, Zheng | a22285a | 2010-05-16 10:48:46 -0400 | [diff] [blame] | 5762 | int err; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5763 | u64 objectid; |
Chris Mason | 00e4e6b | 2008-08-05 11:18:09 -0400 | [diff] [blame] | 5764 | u64 index = 0; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5765 | |
Josef Bacik | 9ed74f2 | 2009-09-11 16:12:44 -0400 | [diff] [blame] | 5766 | /* |
| 5767 | * 2 for inode item and ref |
| 5768 | * 2 for dir items |
| 5769 | * 1 for xattr if selinux is on |
| 5770 | */ |
Yan, Zheng | a22285a | 2010-05-16 10:48:46 -0400 | [diff] [blame] | 5771 | trans = btrfs_start_transaction(root, 5); |
| 5772 | if (IS_ERR(trans)) |
| 5773 | return PTR_ERR(trans); |
Josef Bacik | 9ed74f2 | 2009-09-11 16:12:44 -0400 | [diff] [blame] | 5774 | |
Li Zefan | 581bb05 | 2011-04-20 10:06:11 +0800 | [diff] [blame] | 5775 | err = btrfs_find_free_ino(root, &objectid); |
| 5776 | if (err) |
| 5777 | goto out_unlock; |
| 5778 | |
Josef Bacik | aec7477 | 2008-07-24 12:12:38 -0400 | [diff] [blame] | 5779 | inode = btrfs_new_inode(trans, root, dir, dentry->d_name.name, |
Li Zefan | 33345d01 | 2011-04-20 10:31:50 +0800 | [diff] [blame] | 5780 | dentry->d_name.len, btrfs_ino(dir), objectid, |
Josef Bacik | d82a6f1d | 2011-05-11 15:26:06 -0400 | [diff] [blame] | 5781 | mode, &index); |
Tsutomu Itoh | 7cf96da | 2011-04-25 19:43:53 -0400 | [diff] [blame] | 5782 | if (IS_ERR(inode)) { |
| 5783 | err = PTR_ERR(inode); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5784 | goto out_unlock; |
Tsutomu Itoh | 7cf96da | 2011-04-25 19:43:53 -0400 | [diff] [blame] | 5785 | } |
Filipe Brandenburger | 43baa57 | 2012-11-30 03:40:09 +0000 | [diff] [blame] | 5786 | drop_inode_on_err = 1; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5787 | |
Eric Paris | 2a7dba3 | 2011-02-01 11:05:39 -0500 | [diff] [blame] | 5788 | err = btrfs_init_inode_security(trans, inode, dir, &dentry->d_name); |
Filipe Brandenburger | 43baa57 | 2012-11-30 03:40:09 +0000 | [diff] [blame] | 5789 | if (err) |
Josef Bacik | 33268ea | 2008-07-24 12:16:36 -0400 | [diff] [blame] | 5790 | goto out_unlock; |
Josef Bacik | 33268ea | 2008-07-24 12:16:36 -0400 | [diff] [blame] | 5791 | |
Filipe Brandenburger | 9185aa5 | 2012-11-30 03:40:08 +0000 | [diff] [blame] | 5792 | err = btrfs_update_inode(trans, root, inode); |
| 5793 | if (err) |
| 5794 | goto out_unlock; |
| 5795 | |
Casey Schaufler | ad19db7 | 2011-12-15 10:09:07 -0500 | [diff] [blame] | 5796 | /* |
| 5797 | * If the active LSM wants to access the inode during |
| 5798 | * d_instantiate it needs these. Smack checks to see |
| 5799 | * if the filesystem supports xattrs by looking at the |
| 5800 | * ops vector. |
| 5801 | */ |
| 5802 | inode->i_fop = &btrfs_file_operations; |
| 5803 | inode->i_op = &btrfs_file_inode_operations; |
| 5804 | |
Josef Bacik | a1b075d | 2010-11-19 20:36:11 +0000 | [diff] [blame] | 5805 | err = btrfs_add_nondir(trans, dir, dentry, inode, 0, index); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5806 | if (err) |
Filipe Brandenburger | 43baa57 | 2012-11-30 03:40:09 +0000 | [diff] [blame] | 5807 | goto out_unlock; |
| 5808 | |
| 5809 | inode->i_mapping->a_ops = &btrfs_aops; |
| 5810 | inode->i_mapping->backing_dev_info = &root->fs_info->bdi; |
| 5811 | BTRFS_I(inode)->io_tree.ops = &btrfs_extent_io_ops; |
| 5812 | d_instantiate(dentry, inode); |
| 5813 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5814 | out_unlock: |
Josef Bacik | 7ad85bb | 2012-01-12 19:10:12 -0500 | [diff] [blame] | 5815 | btrfs_end_transaction(trans, root); |
Filipe Brandenburger | 43baa57 | 2012-11-30 03:40:09 +0000 | [diff] [blame] | 5816 | if (err && drop_inode_on_err) { |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5817 | inode_dec_link_count(inode); |
| 5818 | iput(inode); |
| 5819 | } |
Liu Bo | b53d3f5 | 2012-11-14 14:34:34 +0000 | [diff] [blame] | 5820 | btrfs_btree_balance_dirty(root); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5821 | return err; |
| 5822 | } |
| 5823 | |
| 5824 | static int btrfs_link(struct dentry *old_dentry, struct inode *dir, |
| 5825 | struct dentry *dentry) |
| 5826 | { |
| 5827 | struct btrfs_trans_handle *trans; |
| 5828 | struct btrfs_root *root = BTRFS_I(dir)->root; |
| 5829 | struct inode *inode = old_dentry->d_inode; |
Chris Mason | 00e4e6b | 2008-08-05 11:18:09 -0400 | [diff] [blame] | 5830 | u64 index; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5831 | int err; |
| 5832 | int drop_inode = 0; |
| 5833 | |
TARUISI Hiroaki | 4a8be42 | 2009-11-12 07:14:26 +0000 | [diff] [blame] | 5834 | /* do not allow sys_link's with other subvols of the same device */ |
| 5835 | if (root->objectid != BTRFS_I(inode)->root->objectid) |
Mark Fasheh | 3ab3564 | 2011-03-22 17:20:26 +0000 | [diff] [blame] | 5836 | return -EXDEV; |
TARUISI Hiroaki | 4a8be42 | 2009-11-12 07:14:26 +0000 | [diff] [blame] | 5837 | |
Mark Fasheh | f186373 | 2012-08-08 11:32:27 -0700 | [diff] [blame] | 5838 | if (inode->i_nlink >= BTRFS_LINK_MAX) |
Al Viro | c055e99 | 2011-03-04 17:15:18 +0000 | [diff] [blame] | 5839 | return -EMLINK; |
Josef Bacik | 9ed74f2 | 2009-09-11 16:12:44 -0400 | [diff] [blame] | 5840 | |
Chris Mason | 3de4586 | 2008-11-17 21:02:50 -0500 | [diff] [blame] | 5841 | err = btrfs_set_inode_index(dir, &index); |
Josef Bacik | aec7477 | 2008-07-24 12:12:38 -0400 | [diff] [blame] | 5842 | if (err) |
| 5843 | goto fail; |
| 5844 | |
Yan, Zheng | a22285a | 2010-05-16 10:48:46 -0400 | [diff] [blame] | 5845 | /* |
Miao Xie | 7e6b646 | 2011-02-18 09:21:17 +0000 | [diff] [blame] | 5846 | * 2 items for inode and inode ref |
Yan, Zheng | a22285a | 2010-05-16 10:48:46 -0400 | [diff] [blame] | 5847 | * 2 items for dir items |
Miao Xie | 7e6b646 | 2011-02-18 09:21:17 +0000 | [diff] [blame] | 5848 | * 1 item for parent inode |
Yan, Zheng | a22285a | 2010-05-16 10:48:46 -0400 | [diff] [blame] | 5849 | */ |
Miao Xie | 7e6b646 | 2011-02-18 09:21:17 +0000 | [diff] [blame] | 5850 | trans = btrfs_start_transaction(root, 5); |
Yan, Zheng | a22285a | 2010-05-16 10:48:46 -0400 | [diff] [blame] | 5851 | if (IS_ERR(trans)) { |
| 5852 | err = PTR_ERR(trans); |
| 5853 | goto fail; |
| 5854 | } |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 5855 | |
Miao Xie | 67de117 | 2013-12-26 13:07:06 +0800 | [diff] [blame^] | 5856 | /* There are several dir indexes for this inode, clear the cache. */ |
| 5857 | BTRFS_I(inode)->dir_index = 0ULL; |
Zach Brown | 8b558c5 | 2013-10-16 12:10:34 -0700 | [diff] [blame] | 5858 | inc_nlink(inode); |
Josef Bacik | 0c4d2d9 | 2012-04-05 15:03:02 -0400 | [diff] [blame] | 5859 | inode_inc_iversion(inode); |
Miao Xie | 3153495 | 2011-04-13 13:19:21 +0800 | [diff] [blame] | 5860 | inode->i_ctime = CURRENT_TIME; |
Al Viro | 7de9c6ee | 2010-10-23 11:11:40 -0400 | [diff] [blame] | 5861 | ihold(inode); |
Josef Bacik | e997615 | 2012-10-11 15:53:56 -0400 | [diff] [blame] | 5862 | set_bit(BTRFS_INODE_COPY_EVERYTHING, &BTRFS_I(inode)->runtime_flags); |
Josef Bacik | aec7477 | 2008-07-24 12:12:38 -0400 | [diff] [blame] | 5863 | |
Josef Bacik | a1b075d | 2010-11-19 20:36:11 +0000 | [diff] [blame] | 5864 | err = btrfs_add_nondir(trans, dir, dentry, inode, 1, index); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 5865 | |
Yan, Zheng | a571952 | 2009-09-24 09:17:31 -0400 | [diff] [blame] | 5866 | if (err) { |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5867 | drop_inode = 1; |
Yan, Zheng | a571952 | 2009-09-24 09:17:31 -0400 | [diff] [blame] | 5868 | } else { |
Al Viro | 10d9f30 | 2011-07-16 23:09:10 -0400 | [diff] [blame] | 5869 | struct dentry *parent = dentry->d_parent; |
Yan, Zheng | a571952 | 2009-09-24 09:17:31 -0400 | [diff] [blame] | 5870 | err = btrfs_update_inode(trans, root, inode); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 5871 | if (err) |
| 5872 | goto fail; |
Al Viro | 08c422c | 2011-12-23 07:58:13 -0500 | [diff] [blame] | 5873 | d_instantiate(dentry, inode); |
Josef Bacik | 6a91221 | 2010-11-20 09:48:00 +0000 | [diff] [blame] | 5874 | btrfs_log_new_name(trans, inode, NULL, parent); |
Yan, Zheng | a571952 | 2009-09-24 09:17:31 -0400 | [diff] [blame] | 5875 | } |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5876 | |
Josef Bacik | 7ad85bb | 2012-01-12 19:10:12 -0500 | [diff] [blame] | 5877 | btrfs_end_transaction(trans, root); |
Chris Mason | 1832a6d | 2007-12-21 16:27:21 -0500 | [diff] [blame] | 5878 | fail: |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5879 | if (drop_inode) { |
| 5880 | inode_dec_link_count(inode); |
| 5881 | iput(inode); |
| 5882 | } |
Liu Bo | b53d3f5 | 2012-11-14 14:34:34 +0000 | [diff] [blame] | 5883 | btrfs_btree_balance_dirty(root); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5884 | return err; |
| 5885 | } |
| 5886 | |
Al Viro | 18bb1db | 2011-07-26 01:41:39 -0400 | [diff] [blame] | 5887 | 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] | 5888 | { |
Chris Mason | b9d8666 | 2008-05-02 16:13:49 -0400 | [diff] [blame] | 5889 | struct inode *inode = NULL; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5890 | struct btrfs_trans_handle *trans; |
| 5891 | struct btrfs_root *root = BTRFS_I(dir)->root; |
| 5892 | int err = 0; |
| 5893 | int drop_on_err = 0; |
Chris Mason | b9d8666 | 2008-05-02 16:13:49 -0400 | [diff] [blame] | 5894 | u64 objectid = 0; |
Chris Mason | 00e4e6b | 2008-08-05 11:18:09 -0400 | [diff] [blame] | 5895 | u64 index = 0; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5896 | |
Josef Bacik | 9ed74f2 | 2009-09-11 16:12:44 -0400 | [diff] [blame] | 5897 | /* |
| 5898 | * 2 items for inode and ref |
| 5899 | * 2 items for dir items |
| 5900 | * 1 for xattr if selinux is on |
| 5901 | */ |
Yan, Zheng | a22285a | 2010-05-16 10:48:46 -0400 | [diff] [blame] | 5902 | trans = btrfs_start_transaction(root, 5); |
| 5903 | if (IS_ERR(trans)) |
| 5904 | return PTR_ERR(trans); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5905 | |
Li Zefan | 581bb05 | 2011-04-20 10:06:11 +0800 | [diff] [blame] | 5906 | err = btrfs_find_free_ino(root, &objectid); |
| 5907 | if (err) |
| 5908 | goto out_fail; |
| 5909 | |
Josef Bacik | aec7477 | 2008-07-24 12:12:38 -0400 | [diff] [blame] | 5910 | inode = btrfs_new_inode(trans, root, dir, dentry->d_name.name, |
Li Zefan | 33345d01 | 2011-04-20 10:31:50 +0800 | [diff] [blame] | 5911 | dentry->d_name.len, btrfs_ino(dir), objectid, |
Josef Bacik | d82a6f1d | 2011-05-11 15:26:06 -0400 | [diff] [blame] | 5912 | S_IFDIR | mode, &index); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5913 | if (IS_ERR(inode)) { |
| 5914 | err = PTR_ERR(inode); |
| 5915 | goto out_fail; |
| 5916 | } |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 5917 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5918 | drop_on_err = 1; |
Josef Bacik | 33268ea | 2008-07-24 12:16:36 -0400 | [diff] [blame] | 5919 | |
Eric Paris | 2a7dba3 | 2011-02-01 11:05:39 -0500 | [diff] [blame] | 5920 | err = btrfs_init_inode_security(trans, inode, dir, &dentry->d_name); |
Josef Bacik | 33268ea | 2008-07-24 12:16:36 -0400 | [diff] [blame] | 5921 | if (err) |
| 5922 | goto out_fail; |
| 5923 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5924 | inode->i_op = &btrfs_dir_inode_operations; |
| 5925 | inode->i_fop = &btrfs_dir_file_operations; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5926 | |
Chris Mason | dbe674a | 2008-07-17 12:54:05 -0400 | [diff] [blame] | 5927 | btrfs_i_size_write(inode, 0); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5928 | err = btrfs_update_inode(trans, root, inode); |
| 5929 | if (err) |
| 5930 | goto out_fail; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 5931 | |
Josef Bacik | a1b075d | 2010-11-19 20:36:11 +0000 | [diff] [blame] | 5932 | err = btrfs_add_link(trans, dir, inode, dentry->d_name.name, |
| 5933 | dentry->d_name.len, 0, index); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5934 | if (err) |
| 5935 | goto out_fail; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 5936 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5937 | d_instantiate(dentry, inode); |
| 5938 | drop_on_err = 0; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5939 | |
| 5940 | out_fail: |
Josef Bacik | 7ad85bb | 2012-01-12 19:10:12 -0500 | [diff] [blame] | 5941 | btrfs_end_transaction(trans, root); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5942 | if (drop_on_err) |
| 5943 | iput(inode); |
Liu Bo | b53d3f5 | 2012-11-14 14:34:34 +0000 | [diff] [blame] | 5944 | btrfs_btree_balance_dirty(root); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5945 | return err; |
| 5946 | } |
| 5947 | |
Chris Mason | d352ac6 | 2008-09-29 15:18:18 -0400 | [diff] [blame] | 5948 | /* helper for btfs_get_extent. Given an existing extent in the tree, |
| 5949 | * and an extent that you want to insert, deal with overlap and insert |
| 5950 | * the new extent into the tree. |
| 5951 | */ |
Chris Mason | 3b95151 | 2008-04-17 11:29:12 -0400 | [diff] [blame] | 5952 | static int merge_extent_mapping(struct extent_map_tree *em_tree, |
| 5953 | struct extent_map *existing, |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 5954 | struct extent_map *em, |
| 5955 | u64 map_start, u64 map_len) |
Chris Mason | 3b95151 | 2008-04-17 11:29:12 -0400 | [diff] [blame] | 5956 | { |
| 5957 | u64 start_diff; |
Chris Mason | 3b95151 | 2008-04-17 11:29:12 -0400 | [diff] [blame] | 5958 | |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 5959 | BUG_ON(map_start < em->start || map_start >= extent_map_end(em)); |
| 5960 | start_diff = map_start - em->start; |
| 5961 | em->start = map_start; |
| 5962 | em->len = map_len; |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 5963 | if (em->block_start < EXTENT_MAP_LAST_BYTE && |
| 5964 | !test_bit(EXTENT_FLAG_COMPRESSED, &em->flags)) { |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 5965 | em->block_start += start_diff; |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 5966 | em->block_len -= start_diff; |
| 5967 | } |
Josef Bacik | 09a2a8f9 | 2013-04-05 16:51:15 -0400 | [diff] [blame] | 5968 | return add_extent_mapping(em_tree, em, 0); |
Chris Mason | 3b95151 | 2008-04-17 11:29:12 -0400 | [diff] [blame] | 5969 | } |
| 5970 | |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 5971 | static noinline int uncompress_inline(struct btrfs_path *path, |
| 5972 | struct inode *inode, struct page *page, |
| 5973 | size_t pg_offset, u64 extent_offset, |
| 5974 | struct btrfs_file_extent_item *item) |
| 5975 | { |
| 5976 | int ret; |
| 5977 | struct extent_buffer *leaf = path->nodes[0]; |
| 5978 | char *tmp; |
| 5979 | size_t max_size; |
| 5980 | unsigned long inline_size; |
| 5981 | unsigned long ptr; |
Li Zefan | 261507a0 | 2010-12-17 14:21:50 +0800 | [diff] [blame] | 5982 | int compress_type; |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 5983 | |
| 5984 | WARN_ON(pg_offset != 0); |
Li Zefan | 261507a0 | 2010-12-17 14:21:50 +0800 | [diff] [blame] | 5985 | compress_type = btrfs_file_extent_compression(leaf, item); |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 5986 | max_size = btrfs_file_extent_ram_bytes(leaf, item); |
| 5987 | inline_size = btrfs_file_extent_inline_item_len(leaf, |
Ross Kirk | dd3cc16 | 2013-09-16 15:58:09 +0100 | [diff] [blame] | 5988 | btrfs_item_nr(path->slots[0])); |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 5989 | tmp = kmalloc(inline_size, GFP_NOFS); |
Tsutomu Itoh | 8d41371 | 2011-04-25 19:43:52 -0400 | [diff] [blame] | 5990 | if (!tmp) |
| 5991 | return -ENOMEM; |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 5992 | ptr = btrfs_file_extent_inline_start(item); |
| 5993 | |
| 5994 | read_extent_buffer(leaf, tmp, ptr, inline_size); |
| 5995 | |
Chris Mason | 5b050f0 | 2008-11-11 09:34:41 -0500 | [diff] [blame] | 5996 | max_size = min_t(unsigned long, PAGE_CACHE_SIZE, max_size); |
Li Zefan | 261507a0 | 2010-12-17 14:21:50 +0800 | [diff] [blame] | 5997 | ret = btrfs_decompress(compress_type, tmp, page, |
| 5998 | extent_offset, inline_size, max_size); |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 5999 | if (ret) { |
Cong Wang | 7ac687d | 2011-11-25 23:14:28 +0800 | [diff] [blame] | 6000 | char *kaddr = kmap_atomic(page); |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 6001 | unsigned long copy_size = min_t(u64, |
| 6002 | PAGE_CACHE_SIZE - pg_offset, |
| 6003 | max_size - extent_offset); |
| 6004 | memset(kaddr + pg_offset, 0, copy_size); |
Cong Wang | 7ac687d | 2011-11-25 23:14:28 +0800 | [diff] [blame] | 6005 | kunmap_atomic(kaddr); |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 6006 | } |
| 6007 | kfree(tmp); |
| 6008 | return 0; |
| 6009 | } |
| 6010 | |
Chris Mason | d352ac6 | 2008-09-29 15:18:18 -0400 | [diff] [blame] | 6011 | /* |
| 6012 | * 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] | 6013 | * the ugly parts come from merging extents from the disk with the in-ram |
| 6014 | * representation. This gets more complex because of the data=ordered code, |
Chris Mason | d352ac6 | 2008-09-29 15:18:18 -0400 | [diff] [blame] | 6015 | * where the in-ram extents might be locked pending data=ordered completion. |
| 6016 | * |
| 6017 | * This also copies inline extents directly into the page. |
| 6018 | */ |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 6019 | |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 6020 | struct extent_map *btrfs_get_extent(struct inode *inode, struct page *page, |
Chris Mason | 70dec80 | 2008-01-29 09:59:12 -0500 | [diff] [blame] | 6021 | size_t pg_offset, u64 start, u64 len, |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 6022 | int create) |
| 6023 | { |
| 6024 | int ret; |
| 6025 | int err = 0; |
Chris Mason | db94535 | 2007-10-15 16:15:53 -0400 | [diff] [blame] | 6026 | u64 bytenr; |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 6027 | u64 extent_start = 0; |
| 6028 | u64 extent_end = 0; |
Li Zefan | 33345d01 | 2011-04-20 10:31:50 +0800 | [diff] [blame] | 6029 | u64 objectid = btrfs_ino(inode); |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 6030 | u32 found_type; |
Chris Mason | f421950 | 2008-07-22 11:18:09 -0400 | [diff] [blame] | 6031 | struct btrfs_path *path = NULL; |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 6032 | struct btrfs_root *root = BTRFS_I(inode)->root; |
| 6033 | struct btrfs_file_extent_item *item; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 6034 | struct extent_buffer *leaf; |
| 6035 | struct btrfs_key found_key; |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 6036 | struct extent_map *em = NULL; |
| 6037 | struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree; |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 6038 | struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree; |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 6039 | struct btrfs_trans_handle *trans = NULL; |
Li Zefan | 261507a0 | 2010-12-17 14:21:50 +0800 | [diff] [blame] | 6040 | int compress_type; |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 6041 | |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 6042 | again: |
Chris Mason | 890871b | 2009-09-02 16:24:52 -0400 | [diff] [blame] | 6043 | read_lock(&em_tree->lock); |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 6044 | em = lookup_extent_mapping(em_tree, start, len); |
Chris Mason | a061fc8 | 2008-05-07 11:43:44 -0400 | [diff] [blame] | 6045 | if (em) |
| 6046 | em->bdev = root->fs_info->fs_devices->latest_bdev; |
Chris Mason | 890871b | 2009-09-02 16:24:52 -0400 | [diff] [blame] | 6047 | read_unlock(&em_tree->lock); |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 6048 | |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 6049 | if (em) { |
Chris Mason | e1c4b74 | 2008-04-22 13:26:46 -0400 | [diff] [blame] | 6050 | if (em->start > start || em->start + em->len <= start) |
| 6051 | free_extent_map(em); |
| 6052 | else if (em->block_start == EXTENT_MAP_INLINE && page) |
Chris Mason | 70dec80 | 2008-01-29 09:59:12 -0500 | [diff] [blame] | 6053 | free_extent_map(em); |
| 6054 | else |
| 6055 | goto out; |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 6056 | } |
David Sterba | 172ddd6 | 2011-04-21 00:48:27 +0200 | [diff] [blame] | 6057 | em = alloc_extent_map(); |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 6058 | if (!em) { |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 6059 | err = -ENOMEM; |
| 6060 | goto out; |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 6061 | } |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 6062 | em->bdev = root->fs_info->fs_devices->latest_bdev; |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 6063 | em->start = EXTENT_MAP_HOLE; |
Chris Mason | 445a694 | 2008-11-10 11:53:33 -0500 | [diff] [blame] | 6064 | em->orig_start = EXTENT_MAP_HOLE; |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 6065 | em->len = (u64)-1; |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 6066 | em->block_len = (u64)-1; |
Chris Mason | f421950 | 2008-07-22 11:18:09 -0400 | [diff] [blame] | 6067 | |
| 6068 | if (!path) { |
| 6069 | path = btrfs_alloc_path(); |
Josef Bacik | 026fd31 | 2011-05-13 10:32:11 -0400 | [diff] [blame] | 6070 | if (!path) { |
| 6071 | err = -ENOMEM; |
| 6072 | goto out; |
| 6073 | } |
| 6074 | /* |
| 6075 | * Chances are we'll be called again, so go ahead and do |
| 6076 | * readahead |
| 6077 | */ |
| 6078 | path->reada = 1; |
Chris Mason | f421950 | 2008-07-22 11:18:09 -0400 | [diff] [blame] | 6079 | } |
| 6080 | |
Chris Mason | 179e29e | 2007-11-01 11:28:41 -0400 | [diff] [blame] | 6081 | ret = btrfs_lookup_file_extent(trans, root, path, |
| 6082 | objectid, start, trans != NULL); |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 6083 | if (ret < 0) { |
| 6084 | err = ret; |
| 6085 | goto out; |
| 6086 | } |
| 6087 | |
| 6088 | if (ret != 0) { |
| 6089 | if (path->slots[0] == 0) |
| 6090 | goto not_found; |
| 6091 | path->slots[0]--; |
| 6092 | } |
| 6093 | |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 6094 | leaf = path->nodes[0]; |
| 6095 | item = btrfs_item_ptr(leaf, path->slots[0], |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 6096 | struct btrfs_file_extent_item); |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 6097 | /* are we inside the extent that was found? */ |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 6098 | btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]); |
| 6099 | found_type = btrfs_key_type(&found_key); |
| 6100 | if (found_key.objectid != objectid || |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 6101 | found_type != BTRFS_EXTENT_DATA_KEY) { |
Josef Bacik | 25a5034 | 2013-10-14 12:08:38 -0400 | [diff] [blame] | 6102 | /* |
| 6103 | * If we backup past the first extent we want to move forward |
| 6104 | * and see if there is an extent in front of us, otherwise we'll |
| 6105 | * say there is a hole for our whole search range which can |
| 6106 | * cause problems. |
| 6107 | */ |
| 6108 | extent_end = start; |
| 6109 | goto next; |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 6110 | } |
| 6111 | |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 6112 | found_type = btrfs_file_extent_type(leaf, item); |
| 6113 | extent_start = found_key.offset; |
Li Zefan | 261507a0 | 2010-12-17 14:21:50 +0800 | [diff] [blame] | 6114 | compress_type = btrfs_file_extent_compression(leaf, item); |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 6115 | if (found_type == BTRFS_FILE_EXTENT_REG || |
| 6116 | found_type == BTRFS_FILE_EXTENT_PREALLOC) { |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 6117 | extent_end = extent_start + |
Chris Mason | db94535 | 2007-10-15 16:15:53 -0400 | [diff] [blame] | 6118 | btrfs_file_extent_num_bytes(leaf, item); |
Yan Zheng | 9036c10 | 2008-10-30 14:19:41 -0400 | [diff] [blame] | 6119 | } else if (found_type == BTRFS_FILE_EXTENT_INLINE) { |
| 6120 | size_t size; |
| 6121 | size = btrfs_file_extent_inline_len(leaf, item); |
Qu Wenruo | fda2832 | 2013-02-26 08:10:22 +0000 | [diff] [blame] | 6122 | extent_end = ALIGN(extent_start + size, root->sectorsize); |
Yan Zheng | 9036c10 | 2008-10-30 14:19:41 -0400 | [diff] [blame] | 6123 | } |
Josef Bacik | 25a5034 | 2013-10-14 12:08:38 -0400 | [diff] [blame] | 6124 | next: |
Yan Zheng | 9036c10 | 2008-10-30 14:19:41 -0400 | [diff] [blame] | 6125 | if (start >= extent_end) { |
| 6126 | path->slots[0]++; |
| 6127 | if (path->slots[0] >= btrfs_header_nritems(leaf)) { |
| 6128 | ret = btrfs_next_leaf(root, path); |
| 6129 | if (ret < 0) { |
| 6130 | err = ret; |
| 6131 | goto out; |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 6132 | } |
Yan Zheng | 9036c10 | 2008-10-30 14:19:41 -0400 | [diff] [blame] | 6133 | if (ret > 0) |
| 6134 | goto not_found; |
| 6135 | leaf = path->nodes[0]; |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 6136 | } |
Yan Zheng | 9036c10 | 2008-10-30 14:19:41 -0400 | [diff] [blame] | 6137 | btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]); |
| 6138 | if (found_key.objectid != objectid || |
| 6139 | found_key.type != BTRFS_EXTENT_DATA_KEY) |
| 6140 | goto not_found; |
| 6141 | if (start + len <= found_key.offset) |
| 6142 | goto not_found; |
| 6143 | em->start = start; |
Josef Bacik | 70c8a91 | 2012-10-11 16:54:30 -0400 | [diff] [blame] | 6144 | em->orig_start = start; |
Yan Zheng | 9036c10 | 2008-10-30 14:19:41 -0400 | [diff] [blame] | 6145 | em->len = found_key.offset - start; |
| 6146 | goto not_found_em; |
| 6147 | } |
| 6148 | |
Josef Bacik | cc95bef | 2013-04-04 14:31:27 -0400 | [diff] [blame] | 6149 | em->ram_bytes = btrfs_file_extent_ram_bytes(leaf, item); |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 6150 | if (found_type == BTRFS_FILE_EXTENT_REG || |
| 6151 | found_type == BTRFS_FILE_EXTENT_PREALLOC) { |
Yan Zheng | 9036c10 | 2008-10-30 14:19:41 -0400 | [diff] [blame] | 6152 | em->start = extent_start; |
| 6153 | em->len = extent_end - extent_start; |
Yan Zheng | ff5b7ee | 2008-11-10 07:34:43 -0500 | [diff] [blame] | 6154 | em->orig_start = extent_start - |
| 6155 | btrfs_file_extent_offset(leaf, item); |
Josef Bacik | b493968 | 2012-12-03 10:31:19 -0500 | [diff] [blame] | 6156 | em->orig_block_len = btrfs_file_extent_disk_num_bytes(leaf, |
| 6157 | item); |
Chris Mason | db94535 | 2007-10-15 16:15:53 -0400 | [diff] [blame] | 6158 | bytenr = btrfs_file_extent_disk_bytenr(leaf, item); |
| 6159 | if (bytenr == 0) { |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 6160 | em->block_start = EXTENT_MAP_HOLE; |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 6161 | goto insert; |
| 6162 | } |
Li Zefan | 261507a0 | 2010-12-17 14:21:50 +0800 | [diff] [blame] | 6163 | if (compress_type != BTRFS_COMPRESS_NONE) { |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 6164 | set_bit(EXTENT_FLAG_COMPRESSED, &em->flags); |
Li Zefan | 261507a0 | 2010-12-17 14:21:50 +0800 | [diff] [blame] | 6165 | em->compress_type = compress_type; |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 6166 | em->block_start = bytenr; |
Josef Bacik | b493968 | 2012-12-03 10:31:19 -0500 | [diff] [blame] | 6167 | em->block_len = em->orig_block_len; |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 6168 | } else { |
| 6169 | bytenr += btrfs_file_extent_offset(leaf, item); |
| 6170 | em->block_start = bytenr; |
| 6171 | em->block_len = em->len; |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 6172 | if (found_type == BTRFS_FILE_EXTENT_PREALLOC) |
| 6173 | set_bit(EXTENT_FLAG_PREALLOC, &em->flags); |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 6174 | } |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 6175 | goto insert; |
| 6176 | } else if (found_type == BTRFS_FILE_EXTENT_INLINE) { |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 6177 | unsigned long ptr; |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 6178 | char *map; |
Chris Mason | 3326d1b | 2007-10-15 16:18:25 -0400 | [diff] [blame] | 6179 | size_t size; |
| 6180 | size_t extent_offset; |
| 6181 | size_t copy_size; |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 6182 | |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 6183 | em->block_start = EXTENT_MAP_INLINE; |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 6184 | if (!page || create) { |
Yan | 689f934 | 2007-10-29 11:41:07 -0400 | [diff] [blame] | 6185 | em->start = extent_start; |
Yan Zheng | 9036c10 | 2008-10-30 14:19:41 -0400 | [diff] [blame] | 6186 | em->len = extent_end - extent_start; |
Yan | 689f934 | 2007-10-29 11:41:07 -0400 | [diff] [blame] | 6187 | goto out; |
| 6188 | } |
| 6189 | |
Yan Zheng | 9036c10 | 2008-10-30 14:19:41 -0400 | [diff] [blame] | 6190 | size = btrfs_file_extent_inline_len(leaf, item); |
| 6191 | extent_offset = page_offset(page) + pg_offset - extent_start; |
Chris Mason | 70dec80 | 2008-01-29 09:59:12 -0500 | [diff] [blame] | 6192 | copy_size = min_t(u64, PAGE_CACHE_SIZE - pg_offset, |
Yan | 689f934 | 2007-10-29 11:41:07 -0400 | [diff] [blame] | 6193 | size - extent_offset); |
Chris Mason | 3326d1b | 2007-10-15 16:18:25 -0400 | [diff] [blame] | 6194 | em->start = extent_start + extent_offset; |
Qu Wenruo | fda2832 | 2013-02-26 08:10:22 +0000 | [diff] [blame] | 6195 | em->len = ALIGN(copy_size, root->sectorsize); |
Josef Bacik | b493968 | 2012-12-03 10:31:19 -0500 | [diff] [blame] | 6196 | em->orig_block_len = em->len; |
Josef Bacik | 70c8a91 | 2012-10-11 16:54:30 -0400 | [diff] [blame] | 6197 | em->orig_start = em->start; |
Li Zefan | 261507a0 | 2010-12-17 14:21:50 +0800 | [diff] [blame] | 6198 | if (compress_type) { |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 6199 | set_bit(EXTENT_FLAG_COMPRESSED, &em->flags); |
Li Zefan | 261507a0 | 2010-12-17 14:21:50 +0800 | [diff] [blame] | 6200 | em->compress_type = compress_type; |
| 6201 | } |
Yan | 689f934 | 2007-10-29 11:41:07 -0400 | [diff] [blame] | 6202 | ptr = btrfs_file_extent_inline_start(item) + extent_offset; |
Chris Mason | 179e29e | 2007-11-01 11:28:41 -0400 | [diff] [blame] | 6203 | if (create == 0 && !PageUptodate(page)) { |
Li Zefan | 261507a0 | 2010-12-17 14:21:50 +0800 | [diff] [blame] | 6204 | if (btrfs_file_extent_compression(leaf, item) != |
| 6205 | BTRFS_COMPRESS_NONE) { |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 6206 | ret = uncompress_inline(path, inode, page, |
| 6207 | pg_offset, |
| 6208 | extent_offset, item); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 6209 | BUG_ON(ret); /* -ENOMEM */ |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 6210 | } else { |
| 6211 | map = kmap(page); |
| 6212 | read_extent_buffer(leaf, map + pg_offset, ptr, |
| 6213 | copy_size); |
Chris Mason | 93c82d5 | 2009-09-11 12:36:29 -0400 | [diff] [blame] | 6214 | if (pg_offset + copy_size < PAGE_CACHE_SIZE) { |
| 6215 | memset(map + pg_offset + copy_size, 0, |
| 6216 | PAGE_CACHE_SIZE - pg_offset - |
| 6217 | copy_size); |
| 6218 | } |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 6219 | kunmap(page); |
| 6220 | } |
Chris Mason | 179e29e | 2007-11-01 11:28:41 -0400 | [diff] [blame] | 6221 | flush_dcache_page(page); |
| 6222 | } else if (create && PageUptodate(page)) { |
Jan Schmidt | 6bf7e08 | 2011-12-01 14:35:19 +0100 | [diff] [blame] | 6223 | BUG(); |
Chris Mason | 179e29e | 2007-11-01 11:28:41 -0400 | [diff] [blame] | 6224 | if (!trans) { |
| 6225 | kunmap(page); |
| 6226 | free_extent_map(em); |
| 6227 | em = NULL; |
Chris Mason | ff5714c | 2011-05-28 07:00:39 -0400 | [diff] [blame] | 6228 | |
David Sterba | b3b4aa7 | 2011-04-21 01:20:15 +0200 | [diff] [blame] | 6229 | btrfs_release_path(path); |
Josef Bacik | 7a7eaa4 | 2011-04-13 12:54:33 -0400 | [diff] [blame] | 6230 | trans = btrfs_join_transaction(root); |
Chris Mason | ff5714c | 2011-05-28 07:00:39 -0400 | [diff] [blame] | 6231 | |
Tsutomu Itoh | 3612b49 | 2011-01-25 02:51:38 +0000 | [diff] [blame] | 6232 | if (IS_ERR(trans)) |
| 6233 | return ERR_CAST(trans); |
Chris Mason | 179e29e | 2007-11-01 11:28:41 -0400 | [diff] [blame] | 6234 | goto again; |
| 6235 | } |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 6236 | map = kmap(page); |
Chris Mason | 70dec80 | 2008-01-29 09:59:12 -0500 | [diff] [blame] | 6237 | write_extent_buffer(leaf, map + pg_offset, ptr, |
Chris Mason | 179e29e | 2007-11-01 11:28:41 -0400 | [diff] [blame] | 6238 | copy_size); |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 6239 | kunmap(page); |
Chris Mason | 179e29e | 2007-11-01 11:28:41 -0400 | [diff] [blame] | 6240 | btrfs_mark_buffer_dirty(leaf); |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 6241 | } |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 6242 | set_extent_uptodate(io_tree, em->start, |
Arne Jansen | 507903b | 2011-04-06 10:02:20 +0000 | [diff] [blame] | 6243 | extent_map_end(em) - 1, NULL, GFP_NOFS); |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 6244 | goto insert; |
| 6245 | } else { |
Julia Lawall | 31b1a2b | 2012-11-03 10:58:34 +0000 | [diff] [blame] | 6246 | WARN(1, KERN_ERR "btrfs unknown found_type %d\n", found_type); |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 6247 | } |
| 6248 | not_found: |
| 6249 | em->start = start; |
Josef Bacik | 70c8a91 | 2012-10-11 16:54:30 -0400 | [diff] [blame] | 6250 | em->orig_start = start; |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 6251 | em->len = len; |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 6252 | not_found_em: |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 6253 | em->block_start = EXTENT_MAP_HOLE; |
Yan Zheng | 9036c10 | 2008-10-30 14:19:41 -0400 | [diff] [blame] | 6254 | set_bit(EXTENT_FLAG_VACANCY, &em->flags); |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 6255 | insert: |
David Sterba | b3b4aa7 | 2011-04-21 01:20:15 +0200 | [diff] [blame] | 6256 | btrfs_release_path(path); |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 6257 | if (em->start > start || extent_map_end(em) <= start) { |
Simon Kirby | c2cf52e | 2013-03-19 22:41:23 +0000 | [diff] [blame] | 6258 | btrfs_err(root->fs_info, "bad extent! em: [%llu %llu] passed [%llu %llu]", |
Geert Uytterhoeven | c1c9ff7 | 2013-08-20 13:20:07 +0200 | [diff] [blame] | 6259 | em->start, em->len, start, len); |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 6260 | err = -EIO; |
| 6261 | goto out; |
| 6262 | } |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 6263 | |
| 6264 | err = 0; |
Chris Mason | 890871b | 2009-09-02 16:24:52 -0400 | [diff] [blame] | 6265 | write_lock(&em_tree->lock); |
Josef Bacik | 09a2a8f9 | 2013-04-05 16:51:15 -0400 | [diff] [blame] | 6266 | ret = add_extent_mapping(em_tree, em, 0); |
Chris Mason | 3b95151 | 2008-04-17 11:29:12 -0400 | [diff] [blame] | 6267 | /* it is possible that someone inserted the extent into the tree |
| 6268 | * while we had the lock dropped. It is also possible that |
| 6269 | * an overlapping map exists in the tree |
| 6270 | */ |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 6271 | if (ret == -EEXIST) { |
Chris Mason | 3b95151 | 2008-04-17 11:29:12 -0400 | [diff] [blame] | 6272 | struct extent_map *existing; |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 6273 | |
| 6274 | ret = 0; |
| 6275 | |
Chris Mason | 3b95151 | 2008-04-17 11:29:12 -0400 | [diff] [blame] | 6276 | existing = lookup_extent_mapping(em_tree, start, len); |
Chris Mason | e1c4b74 | 2008-04-22 13:26:46 -0400 | [diff] [blame] | 6277 | if (existing && (existing->start > start || |
| 6278 | existing->start + existing->len <= start)) { |
| 6279 | free_extent_map(existing); |
| 6280 | existing = NULL; |
| 6281 | } |
Chris Mason | 3b95151 | 2008-04-17 11:29:12 -0400 | [diff] [blame] | 6282 | if (!existing) { |
| 6283 | existing = lookup_extent_mapping(em_tree, em->start, |
| 6284 | em->len); |
| 6285 | if (existing) { |
| 6286 | err = merge_extent_mapping(em_tree, existing, |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 6287 | em, start, |
| 6288 | root->sectorsize); |
Chris Mason | 3b95151 | 2008-04-17 11:29:12 -0400 | [diff] [blame] | 6289 | free_extent_map(existing); |
| 6290 | if (err) { |
| 6291 | free_extent_map(em); |
| 6292 | em = NULL; |
| 6293 | } |
| 6294 | } else { |
| 6295 | err = -EIO; |
Chris Mason | 3b95151 | 2008-04-17 11:29:12 -0400 | [diff] [blame] | 6296 | free_extent_map(em); |
| 6297 | em = NULL; |
| 6298 | } |
| 6299 | } else { |
| 6300 | free_extent_map(em); |
| 6301 | em = existing; |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 6302 | err = 0; |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 6303 | } |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 6304 | } |
Chris Mason | 890871b | 2009-09-02 16:24:52 -0400 | [diff] [blame] | 6305 | write_unlock(&em_tree->lock); |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 6306 | out: |
liubo | 1abe9b8 | 2011-03-24 11:18:59 +0000 | [diff] [blame] | 6307 | |
Steven Rostedt | 4cd8587 | 2013-11-14 22:57:29 -0500 | [diff] [blame] | 6308 | trace_btrfs_get_extent(root, em); |
liubo | 1abe9b8 | 2011-03-24 11:18:59 +0000 | [diff] [blame] | 6309 | |
Chris Mason | f421950 | 2008-07-22 11:18:09 -0400 | [diff] [blame] | 6310 | if (path) |
| 6311 | btrfs_free_path(path); |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 6312 | if (trans) { |
| 6313 | ret = btrfs_end_transaction(trans, root); |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 6314 | if (!err) |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 6315 | err = ret; |
| 6316 | } |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 6317 | if (err) { |
| 6318 | free_extent_map(em); |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 6319 | return ERR_PTR(err); |
| 6320 | } |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 6321 | BUG_ON(!em); /* Error is always set */ |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 6322 | return em; |
| 6323 | } |
| 6324 | |
Chris Mason | ec29ed5 | 2011-02-23 16:23:20 -0500 | [diff] [blame] | 6325 | struct extent_map *btrfs_get_extent_fiemap(struct inode *inode, struct page *page, |
| 6326 | size_t pg_offset, u64 start, u64 len, |
| 6327 | int create) |
| 6328 | { |
| 6329 | struct extent_map *em; |
| 6330 | struct extent_map *hole_em = NULL; |
| 6331 | u64 range_start = start; |
| 6332 | u64 end; |
| 6333 | u64 found; |
| 6334 | u64 found_end; |
| 6335 | int err = 0; |
| 6336 | |
| 6337 | em = btrfs_get_extent(inode, page, pg_offset, start, len, create); |
| 6338 | if (IS_ERR(em)) |
| 6339 | return em; |
| 6340 | if (em) { |
| 6341 | /* |
Liu Bo | f9e4fb5 | 2013-01-07 10:10:12 +0000 | [diff] [blame] | 6342 | * if our em maps to |
| 6343 | * - a hole or |
| 6344 | * - a pre-alloc extent, |
| 6345 | * there might actually be delalloc bytes behind it. |
Chris Mason | ec29ed5 | 2011-02-23 16:23:20 -0500 | [diff] [blame] | 6346 | */ |
Liu Bo | f9e4fb5 | 2013-01-07 10:10:12 +0000 | [diff] [blame] | 6347 | if (em->block_start != EXTENT_MAP_HOLE && |
| 6348 | !test_bit(EXTENT_FLAG_PREALLOC, &em->flags)) |
Chris Mason | ec29ed5 | 2011-02-23 16:23:20 -0500 | [diff] [blame] | 6349 | return em; |
| 6350 | else |
| 6351 | hole_em = em; |
| 6352 | } |
| 6353 | |
| 6354 | /* check to see if we've wrapped (len == -1 or similar) */ |
| 6355 | end = start + len; |
| 6356 | if (end < start) |
| 6357 | end = (u64)-1; |
| 6358 | else |
| 6359 | end -= 1; |
| 6360 | |
| 6361 | em = NULL; |
| 6362 | |
| 6363 | /* ok, we didn't find anything, lets look for delalloc */ |
| 6364 | found = count_range_bits(&BTRFS_I(inode)->io_tree, &range_start, |
| 6365 | end, len, EXTENT_DELALLOC, 1); |
| 6366 | found_end = range_start + found; |
| 6367 | if (found_end < range_start) |
| 6368 | found_end = (u64)-1; |
| 6369 | |
| 6370 | /* |
| 6371 | * we didn't find anything useful, return |
| 6372 | * the original results from get_extent() |
| 6373 | */ |
| 6374 | if (range_start > end || found_end <= start) { |
| 6375 | em = hole_em; |
| 6376 | hole_em = NULL; |
| 6377 | goto out; |
| 6378 | } |
| 6379 | |
| 6380 | /* adjust the range_start to make sure it doesn't |
| 6381 | * go backwards from the start they passed in |
| 6382 | */ |
Dulshani Gunawardhana | 6787125 | 2013-10-31 10:33:04 +0530 | [diff] [blame] | 6383 | range_start = max(start, range_start); |
Chris Mason | ec29ed5 | 2011-02-23 16:23:20 -0500 | [diff] [blame] | 6384 | found = found_end - range_start; |
| 6385 | |
| 6386 | if (found > 0) { |
| 6387 | u64 hole_start = start; |
| 6388 | u64 hole_len = len; |
| 6389 | |
David Sterba | 172ddd6 | 2011-04-21 00:48:27 +0200 | [diff] [blame] | 6390 | em = alloc_extent_map(); |
Chris Mason | ec29ed5 | 2011-02-23 16:23:20 -0500 | [diff] [blame] | 6391 | if (!em) { |
| 6392 | err = -ENOMEM; |
| 6393 | goto out; |
| 6394 | } |
| 6395 | /* |
| 6396 | * when btrfs_get_extent can't find anything it |
| 6397 | * returns one huge hole |
| 6398 | * |
| 6399 | * make sure what it found really fits our range, and |
| 6400 | * adjust to make sure it is based on the start from |
| 6401 | * the caller |
| 6402 | */ |
| 6403 | if (hole_em) { |
| 6404 | u64 calc_end = extent_map_end(hole_em); |
| 6405 | |
| 6406 | if (calc_end <= start || (hole_em->start > end)) { |
| 6407 | free_extent_map(hole_em); |
| 6408 | hole_em = NULL; |
| 6409 | } else { |
| 6410 | hole_start = max(hole_em->start, start); |
| 6411 | hole_len = calc_end - hole_start; |
| 6412 | } |
| 6413 | } |
| 6414 | em->bdev = NULL; |
| 6415 | if (hole_em && range_start > hole_start) { |
| 6416 | /* our hole starts before our delalloc, so we |
| 6417 | * have to return just the parts of the hole |
| 6418 | * that go until the delalloc starts |
| 6419 | */ |
| 6420 | em->len = min(hole_len, |
| 6421 | range_start - hole_start); |
| 6422 | em->start = hole_start; |
| 6423 | em->orig_start = hole_start; |
| 6424 | /* |
| 6425 | * don't adjust block start at all, |
| 6426 | * it is fixed at EXTENT_MAP_HOLE |
| 6427 | */ |
| 6428 | em->block_start = hole_em->block_start; |
| 6429 | em->block_len = hole_len; |
Liu Bo | f9e4fb5 | 2013-01-07 10:10:12 +0000 | [diff] [blame] | 6430 | if (test_bit(EXTENT_FLAG_PREALLOC, &hole_em->flags)) |
| 6431 | set_bit(EXTENT_FLAG_PREALLOC, &em->flags); |
Chris Mason | ec29ed5 | 2011-02-23 16:23:20 -0500 | [diff] [blame] | 6432 | } else { |
| 6433 | em->start = range_start; |
| 6434 | em->len = found; |
| 6435 | em->orig_start = range_start; |
| 6436 | em->block_start = EXTENT_MAP_DELALLOC; |
| 6437 | em->block_len = found; |
| 6438 | } |
| 6439 | } else if (hole_em) { |
| 6440 | return hole_em; |
| 6441 | } |
| 6442 | out: |
| 6443 | |
| 6444 | free_extent_map(hole_em); |
| 6445 | if (err) { |
| 6446 | free_extent_map(em); |
| 6447 | return ERR_PTR(err); |
| 6448 | } |
| 6449 | return em; |
| 6450 | } |
| 6451 | |
Josef Bacik | 4b46fce | 2010-05-23 11:00:55 -0400 | [diff] [blame] | 6452 | static struct extent_map *btrfs_new_extent_direct(struct inode *inode, |
| 6453 | u64 start, u64 len) |
| 6454 | { |
| 6455 | struct btrfs_root *root = BTRFS_I(inode)->root; |
Josef Bacik | 70c8a91 | 2012-10-11 16:54:30 -0400 | [diff] [blame] | 6456 | struct extent_map *em; |
Josef Bacik | 4b46fce | 2010-05-23 11:00:55 -0400 | [diff] [blame] | 6457 | struct btrfs_key ins; |
| 6458 | u64 alloc_hint; |
| 6459 | int ret; |
Josef Bacik | 4b46fce | 2010-05-23 11:00:55 -0400 | [diff] [blame] | 6460 | |
Josef Bacik | 4b46fce | 2010-05-23 11:00:55 -0400 | [diff] [blame] | 6461 | alloc_hint = get_extent_allocation_hint(inode, start, len); |
Josef Bacik | 0036158 | 2013-08-14 14:02:47 -0400 | [diff] [blame] | 6462 | ret = btrfs_reserve_extent(root, len, root->sectorsize, 0, |
Josef Bacik | 81c9ad2 | 2012-01-18 10:56:06 -0500 | [diff] [blame] | 6463 | alloc_hint, &ins, 1); |
Josef Bacik | 0036158 | 2013-08-14 14:02:47 -0400 | [diff] [blame] | 6464 | if (ret) |
| 6465 | return ERR_PTR(ret); |
Josef Bacik | 4b46fce | 2010-05-23 11:00:55 -0400 | [diff] [blame] | 6466 | |
Josef Bacik | 70c8a91 | 2012-10-11 16:54:30 -0400 | [diff] [blame] | 6467 | em = create_pinned_em(inode, start, ins.offset, start, ins.objectid, |
Josef Bacik | cc95bef | 2013-04-04 14:31:27 -0400 | [diff] [blame] | 6468 | ins.offset, ins.offset, ins.offset, 0); |
Josef Bacik | 0036158 | 2013-08-14 14:02:47 -0400 | [diff] [blame] | 6469 | if (IS_ERR(em)) { |
| 6470 | btrfs_free_reserved_extent(root, ins.objectid, ins.offset); |
| 6471 | return em; |
| 6472 | } |
Josef Bacik | 4b46fce | 2010-05-23 11:00:55 -0400 | [diff] [blame] | 6473 | |
| 6474 | ret = btrfs_add_ordered_extent_dio(inode, start, ins.objectid, |
| 6475 | ins.offset, ins.offset, 0); |
| 6476 | if (ret) { |
| 6477 | btrfs_free_reserved_extent(root, ins.objectid, ins.offset); |
Josef Bacik | 0036158 | 2013-08-14 14:02:47 -0400 | [diff] [blame] | 6478 | free_extent_map(em); |
| 6479 | return ERR_PTR(ret); |
Josef Bacik | 4b46fce | 2010-05-23 11:00:55 -0400 | [diff] [blame] | 6480 | } |
Josef Bacik | 0036158 | 2013-08-14 14:02:47 -0400 | [diff] [blame] | 6481 | |
Josef Bacik | 4b46fce | 2010-05-23 11:00:55 -0400 | [diff] [blame] | 6482 | return em; |
| 6483 | } |
| 6484 | |
Chris Mason | 46bfbb5 | 2010-05-26 11:04:10 -0400 | [diff] [blame] | 6485 | /* |
| 6486 | * returns 1 when the nocow is safe, < 1 on error, 0 if the |
| 6487 | * block must be cow'd |
| 6488 | */ |
Josef Bacik | 0036158 | 2013-08-14 14:02:47 -0400 | [diff] [blame] | 6489 | noinline int can_nocow_extent(struct inode *inode, u64 offset, u64 *len, |
Josef Bacik | 7ee9e44 | 2013-06-21 16:37:03 -0400 | [diff] [blame] | 6490 | u64 *orig_start, u64 *orig_block_len, |
| 6491 | u64 *ram_bytes) |
Chris Mason | 46bfbb5 | 2010-05-26 11:04:10 -0400 | [diff] [blame] | 6492 | { |
Josef Bacik | 0036158 | 2013-08-14 14:02:47 -0400 | [diff] [blame] | 6493 | struct btrfs_trans_handle *trans; |
Chris Mason | 46bfbb5 | 2010-05-26 11:04:10 -0400 | [diff] [blame] | 6494 | struct btrfs_path *path; |
| 6495 | int ret; |
| 6496 | struct extent_buffer *leaf; |
| 6497 | struct btrfs_root *root = BTRFS_I(inode)->root; |
| 6498 | struct btrfs_file_extent_item *fi; |
| 6499 | struct btrfs_key key; |
| 6500 | u64 disk_bytenr; |
| 6501 | u64 backref_offset; |
| 6502 | u64 extent_end; |
| 6503 | u64 num_bytes; |
| 6504 | int slot; |
| 6505 | int found_type; |
Josef Bacik | 7ee9e44 | 2013-06-21 16:37:03 -0400 | [diff] [blame] | 6506 | bool nocow = (BTRFS_I(inode)->flags & BTRFS_INODE_NODATACOW); |
Chris Mason | 46bfbb5 | 2010-05-26 11:04:10 -0400 | [diff] [blame] | 6507 | path = btrfs_alloc_path(); |
| 6508 | if (!path) |
| 6509 | return -ENOMEM; |
| 6510 | |
Josef Bacik | 0036158 | 2013-08-14 14:02:47 -0400 | [diff] [blame] | 6511 | ret = btrfs_lookup_file_extent(NULL, root, path, btrfs_ino(inode), |
Chris Mason | 46bfbb5 | 2010-05-26 11:04:10 -0400 | [diff] [blame] | 6512 | offset, 0); |
| 6513 | if (ret < 0) |
| 6514 | goto out; |
| 6515 | |
| 6516 | slot = path->slots[0]; |
| 6517 | if (ret == 1) { |
| 6518 | if (slot == 0) { |
| 6519 | /* can't find the item, must cow */ |
| 6520 | ret = 0; |
| 6521 | goto out; |
| 6522 | } |
| 6523 | slot--; |
| 6524 | } |
| 6525 | ret = 0; |
| 6526 | leaf = path->nodes[0]; |
| 6527 | btrfs_item_key_to_cpu(leaf, &key, slot); |
Li Zefan | 33345d01 | 2011-04-20 10:31:50 +0800 | [diff] [blame] | 6528 | if (key.objectid != btrfs_ino(inode) || |
Chris Mason | 46bfbb5 | 2010-05-26 11:04:10 -0400 | [diff] [blame] | 6529 | key.type != BTRFS_EXTENT_DATA_KEY) { |
| 6530 | /* not our file or wrong item type, must cow */ |
| 6531 | goto out; |
| 6532 | } |
| 6533 | |
| 6534 | if (key.offset > offset) { |
| 6535 | /* Wrong offset, must cow */ |
| 6536 | goto out; |
| 6537 | } |
| 6538 | |
| 6539 | fi = btrfs_item_ptr(leaf, slot, struct btrfs_file_extent_item); |
| 6540 | found_type = btrfs_file_extent_type(leaf, fi); |
| 6541 | if (found_type != BTRFS_FILE_EXTENT_REG && |
| 6542 | found_type != BTRFS_FILE_EXTENT_PREALLOC) { |
| 6543 | /* not a regular extent, must cow */ |
| 6544 | goto out; |
| 6545 | } |
Josef Bacik | 7ee9e44 | 2013-06-21 16:37:03 -0400 | [diff] [blame] | 6546 | |
| 6547 | if (!nocow && found_type == BTRFS_FILE_EXTENT_REG) |
| 6548 | goto out; |
| 6549 | |
Chris Mason | 46bfbb5 | 2010-05-26 11:04:10 -0400 | [diff] [blame] | 6550 | disk_bytenr = btrfs_file_extent_disk_bytenr(leaf, fi); |
Josef Bacik | 7ee9e44 | 2013-06-21 16:37:03 -0400 | [diff] [blame] | 6551 | if (disk_bytenr == 0) |
| 6552 | goto out; |
| 6553 | |
| 6554 | if (btrfs_file_extent_compression(leaf, fi) || |
| 6555 | btrfs_file_extent_encryption(leaf, fi) || |
| 6556 | btrfs_file_extent_other_encoding(leaf, fi)) |
| 6557 | goto out; |
| 6558 | |
Chris Mason | 46bfbb5 | 2010-05-26 11:04:10 -0400 | [diff] [blame] | 6559 | backref_offset = btrfs_file_extent_offset(leaf, fi); |
| 6560 | |
Josef Bacik | 7ee9e44 | 2013-06-21 16:37:03 -0400 | [diff] [blame] | 6561 | if (orig_start) { |
| 6562 | *orig_start = key.offset - backref_offset; |
| 6563 | *orig_block_len = btrfs_file_extent_disk_num_bytes(leaf, fi); |
| 6564 | *ram_bytes = btrfs_file_extent_ram_bytes(leaf, fi); |
| 6565 | } |
Josef Bacik | eb384b5 | 2013-04-24 16:32:55 -0400 | [diff] [blame] | 6566 | |
Chris Mason | 46bfbb5 | 2010-05-26 11:04:10 -0400 | [diff] [blame] | 6567 | extent_end = key.offset + btrfs_file_extent_num_bytes(leaf, fi); |
Chris Mason | 46bfbb5 | 2010-05-26 11:04:10 -0400 | [diff] [blame] | 6568 | |
| 6569 | if (btrfs_extent_readonly(root, disk_bytenr)) |
| 6570 | goto out; |
Josef Bacik | 1bda19e | 2013-10-18 12:10:36 -0400 | [diff] [blame] | 6571 | btrfs_release_path(path); |
Chris Mason | 46bfbb5 | 2010-05-26 11:04:10 -0400 | [diff] [blame] | 6572 | |
| 6573 | /* |
| 6574 | * look for other files referencing this extent, if we |
| 6575 | * find any we must cow |
| 6576 | */ |
Josef Bacik | 0036158 | 2013-08-14 14:02:47 -0400 | [diff] [blame] | 6577 | trans = btrfs_join_transaction(root); |
| 6578 | if (IS_ERR(trans)) { |
| 6579 | ret = 0; |
Chris Mason | 46bfbb5 | 2010-05-26 11:04:10 -0400 | [diff] [blame] | 6580 | goto out; |
Josef Bacik | 0036158 | 2013-08-14 14:02:47 -0400 | [diff] [blame] | 6581 | } |
| 6582 | |
| 6583 | ret = btrfs_cross_ref_exist(trans, root, btrfs_ino(inode), |
| 6584 | key.offset - backref_offset, disk_bytenr); |
| 6585 | btrfs_end_transaction(trans, root); |
| 6586 | if (ret) { |
| 6587 | ret = 0; |
| 6588 | goto out; |
| 6589 | } |
Chris Mason | 46bfbb5 | 2010-05-26 11:04:10 -0400 | [diff] [blame] | 6590 | |
| 6591 | /* |
| 6592 | * adjust disk_bytenr and num_bytes to cover just the bytes |
| 6593 | * in this extent we are about to write. If there |
| 6594 | * are any csums in that range we have to cow in order |
| 6595 | * to keep the csums correct |
| 6596 | */ |
| 6597 | disk_bytenr += backref_offset; |
| 6598 | disk_bytenr += offset - key.offset; |
Josef Bacik | eb384b5 | 2013-04-24 16:32:55 -0400 | [diff] [blame] | 6599 | num_bytes = min(offset + *len, extent_end) - offset; |
Chris Mason | 46bfbb5 | 2010-05-26 11:04:10 -0400 | [diff] [blame] | 6600 | if (csum_exist_in_range(root, disk_bytenr, num_bytes)) |
| 6601 | goto out; |
| 6602 | /* |
| 6603 | * all of the above have passed, it is safe to overwrite this extent |
| 6604 | * without cow |
| 6605 | */ |
Josef Bacik | eb384b5 | 2013-04-24 16:32:55 -0400 | [diff] [blame] | 6606 | *len = num_bytes; |
Chris Mason | 46bfbb5 | 2010-05-26 11:04:10 -0400 | [diff] [blame] | 6607 | ret = 1; |
| 6608 | out: |
| 6609 | btrfs_free_path(path); |
| 6610 | return ret; |
| 6611 | } |
| 6612 | |
Josef Bacik | eb838e7 | 2012-07-31 16:28:48 -0400 | [diff] [blame] | 6613 | static int lock_extent_direct(struct inode *inode, u64 lockstart, u64 lockend, |
| 6614 | struct extent_state **cached_state, int writing) |
| 6615 | { |
| 6616 | struct btrfs_ordered_extent *ordered; |
| 6617 | int ret = 0; |
| 6618 | |
| 6619 | while (1) { |
| 6620 | lock_extent_bits(&BTRFS_I(inode)->io_tree, lockstart, lockend, |
| 6621 | 0, cached_state); |
| 6622 | /* |
| 6623 | * We're concerned with the entire range that we're going to be |
| 6624 | * doing DIO to, so we need to make sure theres no ordered |
| 6625 | * extents in this range. |
| 6626 | */ |
| 6627 | ordered = btrfs_lookup_ordered_range(inode, lockstart, |
| 6628 | lockend - lockstart + 1); |
| 6629 | |
| 6630 | /* |
| 6631 | * We need to make sure there are no buffered pages in this |
| 6632 | * range either, we could have raced between the invalidate in |
| 6633 | * generic_file_direct_write and locking the extent. The |
| 6634 | * invalidate needs to happen so that reads after a write do not |
| 6635 | * get stale data. |
| 6636 | */ |
| 6637 | if (!ordered && (!writing || |
| 6638 | !test_range_bit(&BTRFS_I(inode)->io_tree, |
| 6639 | lockstart, lockend, EXTENT_UPTODATE, 0, |
| 6640 | *cached_state))) |
| 6641 | break; |
| 6642 | |
| 6643 | unlock_extent_cached(&BTRFS_I(inode)->io_tree, lockstart, lockend, |
| 6644 | cached_state, GFP_NOFS); |
| 6645 | |
| 6646 | if (ordered) { |
| 6647 | btrfs_start_ordered_extent(inode, ordered, 1); |
| 6648 | btrfs_put_ordered_extent(ordered); |
| 6649 | } else { |
| 6650 | /* Screw you mmap */ |
| 6651 | ret = filemap_write_and_wait_range(inode->i_mapping, |
| 6652 | lockstart, |
| 6653 | lockend); |
| 6654 | if (ret) |
| 6655 | break; |
| 6656 | |
| 6657 | /* |
| 6658 | * If we found a page that couldn't be invalidated just |
| 6659 | * fall back to buffered. |
| 6660 | */ |
| 6661 | ret = invalidate_inode_pages2_range(inode->i_mapping, |
| 6662 | lockstart >> PAGE_CACHE_SHIFT, |
| 6663 | lockend >> PAGE_CACHE_SHIFT); |
| 6664 | if (ret) |
| 6665 | break; |
| 6666 | } |
| 6667 | |
| 6668 | cond_resched(); |
| 6669 | } |
| 6670 | |
| 6671 | return ret; |
| 6672 | } |
| 6673 | |
Josef Bacik | 69ffb54 | 2012-09-11 15:40:07 -0400 | [diff] [blame] | 6674 | static struct extent_map *create_pinned_em(struct inode *inode, u64 start, |
| 6675 | u64 len, u64 orig_start, |
| 6676 | u64 block_start, u64 block_len, |
Josef Bacik | cc95bef | 2013-04-04 14:31:27 -0400 | [diff] [blame] | 6677 | u64 orig_block_len, u64 ram_bytes, |
| 6678 | int type) |
Josef Bacik | 69ffb54 | 2012-09-11 15:40:07 -0400 | [diff] [blame] | 6679 | { |
| 6680 | struct extent_map_tree *em_tree; |
| 6681 | struct extent_map *em; |
| 6682 | struct btrfs_root *root = BTRFS_I(inode)->root; |
| 6683 | int ret; |
| 6684 | |
| 6685 | em_tree = &BTRFS_I(inode)->extent_tree; |
| 6686 | em = alloc_extent_map(); |
| 6687 | if (!em) |
| 6688 | return ERR_PTR(-ENOMEM); |
| 6689 | |
| 6690 | em->start = start; |
| 6691 | em->orig_start = orig_start; |
Josef Bacik | 2ab28f3 | 2012-10-12 15:27:49 -0400 | [diff] [blame] | 6692 | em->mod_start = start; |
| 6693 | em->mod_len = len; |
Josef Bacik | 69ffb54 | 2012-09-11 15:40:07 -0400 | [diff] [blame] | 6694 | em->len = len; |
| 6695 | em->block_len = block_len; |
| 6696 | em->block_start = block_start; |
| 6697 | em->bdev = root->fs_info->fs_devices->latest_bdev; |
Josef Bacik | b493968 | 2012-12-03 10:31:19 -0500 | [diff] [blame] | 6698 | em->orig_block_len = orig_block_len; |
Josef Bacik | cc95bef | 2013-04-04 14:31:27 -0400 | [diff] [blame] | 6699 | em->ram_bytes = ram_bytes; |
Josef Bacik | 70c8a91 | 2012-10-11 16:54:30 -0400 | [diff] [blame] | 6700 | em->generation = -1; |
Josef Bacik | 69ffb54 | 2012-09-11 15:40:07 -0400 | [diff] [blame] | 6701 | set_bit(EXTENT_FLAG_PINNED, &em->flags); |
| 6702 | if (type == BTRFS_ORDERED_PREALLOC) |
Josef Bacik | b11e234 | 2012-12-03 10:58:15 -0500 | [diff] [blame] | 6703 | set_bit(EXTENT_FLAG_FILLING, &em->flags); |
Josef Bacik | 69ffb54 | 2012-09-11 15:40:07 -0400 | [diff] [blame] | 6704 | |
| 6705 | do { |
| 6706 | btrfs_drop_extent_cache(inode, em->start, |
| 6707 | em->start + em->len - 1, 0); |
| 6708 | write_lock(&em_tree->lock); |
Josef Bacik | 09a2a8f9 | 2013-04-05 16:51:15 -0400 | [diff] [blame] | 6709 | ret = add_extent_mapping(em_tree, em, 1); |
Josef Bacik | 69ffb54 | 2012-09-11 15:40:07 -0400 | [diff] [blame] | 6710 | write_unlock(&em_tree->lock); |
| 6711 | } while (ret == -EEXIST); |
| 6712 | |
| 6713 | if (ret) { |
| 6714 | free_extent_map(em); |
| 6715 | return ERR_PTR(ret); |
| 6716 | } |
| 6717 | |
| 6718 | return em; |
| 6719 | } |
| 6720 | |
| 6721 | |
Josef Bacik | 4b46fce | 2010-05-23 11:00:55 -0400 | [diff] [blame] | 6722 | static int btrfs_get_blocks_direct(struct inode *inode, sector_t iblock, |
| 6723 | struct buffer_head *bh_result, int create) |
| 6724 | { |
| 6725 | struct extent_map *em; |
| 6726 | struct btrfs_root *root = BTRFS_I(inode)->root; |
Josef Bacik | eb838e7 | 2012-07-31 16:28:48 -0400 | [diff] [blame] | 6727 | struct extent_state *cached_state = NULL; |
Josef Bacik | 4b46fce | 2010-05-23 11:00:55 -0400 | [diff] [blame] | 6728 | u64 start = iblock << inode->i_blkbits; |
Josef Bacik | eb838e7 | 2012-07-31 16:28:48 -0400 | [diff] [blame] | 6729 | u64 lockstart, lockend; |
Josef Bacik | 4b46fce | 2010-05-23 11:00:55 -0400 | [diff] [blame] | 6730 | u64 len = bh_result->b_size; |
Josef Bacik | eb838e7 | 2012-07-31 16:28:48 -0400 | [diff] [blame] | 6731 | int unlock_bits = EXTENT_LOCKED; |
Miao Xie | 0934856 | 2013-02-07 10:12:07 +0000 | [diff] [blame] | 6732 | int ret = 0; |
Josef Bacik | eb838e7 | 2012-07-31 16:28:48 -0400 | [diff] [blame] | 6733 | |
Miao Xie | 172a504 | 2013-02-21 02:48:22 -0700 | [diff] [blame] | 6734 | if (create) |
Josef Bacik | eb838e7 | 2012-07-31 16:28:48 -0400 | [diff] [blame] | 6735 | unlock_bits |= EXTENT_DELALLOC | EXTENT_DIRTY; |
Miao Xie | 172a504 | 2013-02-21 02:48:22 -0700 | [diff] [blame] | 6736 | else |
Josef Bacik | c329861 | 2012-08-03 16:49:19 -0400 | [diff] [blame] | 6737 | len = min_t(u64, len, root->sectorsize); |
Josef Bacik | eb838e7 | 2012-07-31 16:28:48 -0400 | [diff] [blame] | 6738 | |
Josef Bacik | c329861 | 2012-08-03 16:49:19 -0400 | [diff] [blame] | 6739 | lockstart = start; |
| 6740 | lockend = start + len - 1; |
| 6741 | |
Josef Bacik | eb838e7 | 2012-07-31 16:28:48 -0400 | [diff] [blame] | 6742 | /* |
| 6743 | * If this errors out it's because we couldn't invalidate pagecache for |
| 6744 | * this range and we need to fallback to buffered. |
| 6745 | */ |
| 6746 | if (lock_extent_direct(inode, lockstart, lockend, &cached_state, create)) |
| 6747 | return -ENOTBLK; |
| 6748 | |
Josef Bacik | 4b46fce | 2010-05-23 11:00:55 -0400 | [diff] [blame] | 6749 | em = btrfs_get_extent(inode, NULL, 0, start, len, 0); |
Josef Bacik | eb838e7 | 2012-07-31 16:28:48 -0400 | [diff] [blame] | 6750 | if (IS_ERR(em)) { |
| 6751 | ret = PTR_ERR(em); |
| 6752 | goto unlock_err; |
| 6753 | } |
Josef Bacik | 4b46fce | 2010-05-23 11:00:55 -0400 | [diff] [blame] | 6754 | |
| 6755 | /* |
| 6756 | * Ok for INLINE and COMPRESSED extents we need to fallback on buffered |
| 6757 | * io. INLINE is special, and we could probably kludge it in here, but |
| 6758 | * it's still buffered so for safety lets just fall back to the generic |
| 6759 | * buffered path. |
| 6760 | * |
| 6761 | * For COMPRESSED we _have_ to read the entire extent in so we can |
| 6762 | * decompress it, so there will be buffering required no matter what we |
| 6763 | * do, so go ahead and fallback to buffered. |
| 6764 | * |
| 6765 | * We return -ENOTBLK because thats what makes DIO go ahead and go back |
| 6766 | * to buffered IO. Don't blame me, this is the price we pay for using |
| 6767 | * the generic code. |
| 6768 | */ |
| 6769 | if (test_bit(EXTENT_FLAG_COMPRESSED, &em->flags) || |
| 6770 | em->block_start == EXTENT_MAP_INLINE) { |
| 6771 | free_extent_map(em); |
Josef Bacik | eb838e7 | 2012-07-31 16:28:48 -0400 | [diff] [blame] | 6772 | ret = -ENOTBLK; |
| 6773 | goto unlock_err; |
Josef Bacik | 4b46fce | 2010-05-23 11:00:55 -0400 | [diff] [blame] | 6774 | } |
| 6775 | |
| 6776 | /* Just a good old fashioned hole, return */ |
| 6777 | if (!create && (em->block_start == EXTENT_MAP_HOLE || |
| 6778 | test_bit(EXTENT_FLAG_PREALLOC, &em->flags))) { |
| 6779 | free_extent_map(em); |
Josef Bacik | eb838e7 | 2012-07-31 16:28:48 -0400 | [diff] [blame] | 6780 | goto unlock_err; |
Josef Bacik | 4b46fce | 2010-05-23 11:00:55 -0400 | [diff] [blame] | 6781 | } |
| 6782 | |
| 6783 | /* |
| 6784 | * We don't allocate a new extent in the following cases |
| 6785 | * |
| 6786 | * 1) The inode is marked as NODATACOW. In this case we'll just use the |
| 6787 | * existing extent. |
| 6788 | * 2) The extent is marked as PREALLOC. We're good to go here and can |
| 6789 | * just use the extent. |
| 6790 | * |
| 6791 | */ |
Chris Mason | 46bfbb5 | 2010-05-26 11:04:10 -0400 | [diff] [blame] | 6792 | if (!create) { |
Josef Bacik | eb838e7 | 2012-07-31 16:28:48 -0400 | [diff] [blame] | 6793 | len = min(len, em->len - (start - em->start)); |
| 6794 | lockstart = start + len; |
| 6795 | goto unlock; |
Chris Mason | 46bfbb5 | 2010-05-26 11:04:10 -0400 | [diff] [blame] | 6796 | } |
Josef Bacik | 4b46fce | 2010-05-23 11:00:55 -0400 | [diff] [blame] | 6797 | |
| 6798 | if (test_bit(EXTENT_FLAG_PREALLOC, &em->flags) || |
| 6799 | ((BTRFS_I(inode)->flags & BTRFS_INODE_NODATACOW) && |
| 6800 | em->block_start != EXTENT_MAP_HOLE)) { |
Josef Bacik | 4b46fce | 2010-05-23 11:00:55 -0400 | [diff] [blame] | 6801 | int type; |
| 6802 | int ret; |
Josef Bacik | eb384b5 | 2013-04-24 16:32:55 -0400 | [diff] [blame] | 6803 | u64 block_start, orig_start, orig_block_len, ram_bytes; |
Josef Bacik | 4b46fce | 2010-05-23 11:00:55 -0400 | [diff] [blame] | 6804 | |
| 6805 | if (test_bit(EXTENT_FLAG_PREALLOC, &em->flags)) |
| 6806 | type = BTRFS_ORDERED_PREALLOC; |
| 6807 | else |
| 6808 | type = BTRFS_ORDERED_NOCOW; |
Chris Mason | 46bfbb5 | 2010-05-26 11:04:10 -0400 | [diff] [blame] | 6809 | len = min(len, em->len - (start - em->start)); |
Josef Bacik | 4b46fce | 2010-05-23 11:00:55 -0400 | [diff] [blame] | 6810 | block_start = em->block_start + (start - em->start); |
Chris Mason | 46bfbb5 | 2010-05-26 11:04:10 -0400 | [diff] [blame] | 6811 | |
Josef Bacik | 0036158 | 2013-08-14 14:02:47 -0400 | [diff] [blame] | 6812 | if (can_nocow_extent(inode, start, &len, &orig_start, |
Josef Bacik | 7ee9e44 | 2013-06-21 16:37:03 -0400 | [diff] [blame] | 6813 | &orig_block_len, &ram_bytes) == 1) { |
Josef Bacik | 69ffb54 | 2012-09-11 15:40:07 -0400 | [diff] [blame] | 6814 | if (type == BTRFS_ORDERED_PREALLOC) { |
| 6815 | free_extent_map(em); |
| 6816 | em = create_pinned_em(inode, start, len, |
| 6817 | orig_start, |
Josef Bacik | b493968 | 2012-12-03 10:31:19 -0500 | [diff] [blame] | 6818 | block_start, len, |
Josef Bacik | cc95bef | 2013-04-04 14:31:27 -0400 | [diff] [blame] | 6819 | orig_block_len, |
| 6820 | ram_bytes, type); |
Josef Bacik | 0036158 | 2013-08-14 14:02:47 -0400 | [diff] [blame] | 6821 | if (IS_ERR(em)) |
Josef Bacik | 69ffb54 | 2012-09-11 15:40:07 -0400 | [diff] [blame] | 6822 | goto unlock_err; |
Josef Bacik | 69ffb54 | 2012-09-11 15:40:07 -0400 | [diff] [blame] | 6823 | } |
| 6824 | |
Chris Mason | 46bfbb5 | 2010-05-26 11:04:10 -0400 | [diff] [blame] | 6825 | ret = btrfs_add_ordered_extent_dio(inode, start, |
| 6826 | block_start, len, len, type); |
Chris Mason | 46bfbb5 | 2010-05-26 11:04:10 -0400 | [diff] [blame] | 6827 | if (ret) { |
| 6828 | free_extent_map(em); |
Josef Bacik | eb838e7 | 2012-07-31 16:28:48 -0400 | [diff] [blame] | 6829 | goto unlock_err; |
Chris Mason | 46bfbb5 | 2010-05-26 11:04:10 -0400 | [diff] [blame] | 6830 | } |
| 6831 | goto unlock; |
Josef Bacik | 4b46fce | 2010-05-23 11:00:55 -0400 | [diff] [blame] | 6832 | } |
Josef Bacik | 4b46fce | 2010-05-23 11:00:55 -0400 | [diff] [blame] | 6833 | } |
Josef Bacik | 0036158 | 2013-08-14 14:02:47 -0400 | [diff] [blame] | 6834 | |
Chris Mason | 46bfbb5 | 2010-05-26 11:04:10 -0400 | [diff] [blame] | 6835 | /* |
| 6836 | * this will cow the extent, reset the len in case we changed |
| 6837 | * it above |
| 6838 | */ |
| 6839 | len = bh_result->b_size; |
Josef Bacik | 70c8a91 | 2012-10-11 16:54:30 -0400 | [diff] [blame] | 6840 | free_extent_map(em); |
| 6841 | em = btrfs_new_extent_direct(inode, start, len); |
Josef Bacik | eb838e7 | 2012-07-31 16:28:48 -0400 | [diff] [blame] | 6842 | if (IS_ERR(em)) { |
| 6843 | ret = PTR_ERR(em); |
| 6844 | goto unlock_err; |
| 6845 | } |
Chris Mason | 46bfbb5 | 2010-05-26 11:04:10 -0400 | [diff] [blame] | 6846 | len = min(len, em->len - (start - em->start)); |
| 6847 | unlock: |
Josef Bacik | 4b46fce | 2010-05-23 11:00:55 -0400 | [diff] [blame] | 6848 | bh_result->b_blocknr = (em->block_start + (start - em->start)) >> |
| 6849 | inode->i_blkbits; |
Chris Mason | 46bfbb5 | 2010-05-26 11:04:10 -0400 | [diff] [blame] | 6850 | bh_result->b_size = len; |
Josef Bacik | 4b46fce | 2010-05-23 11:00:55 -0400 | [diff] [blame] | 6851 | bh_result->b_bdev = em->bdev; |
| 6852 | set_buffer_mapped(bh_result); |
Josef Bacik | c3473e8 | 2012-06-19 10:59:00 -0400 | [diff] [blame] | 6853 | if (create) { |
| 6854 | if (!test_bit(EXTENT_FLAG_PREALLOC, &em->flags)) |
| 6855 | set_buffer_new(bh_result); |
| 6856 | |
| 6857 | /* |
| 6858 | * Need to update the i_size under the extent lock so buffered |
| 6859 | * readers will get the updated i_size when we unlock. |
| 6860 | */ |
| 6861 | if (start + len > i_size_read(inode)) |
| 6862 | i_size_write(inode, start + len); |
Miao Xie | 0934856 | 2013-02-07 10:12:07 +0000 | [diff] [blame] | 6863 | |
Miao Xie | 172a504 | 2013-02-21 02:48:22 -0700 | [diff] [blame] | 6864 | spin_lock(&BTRFS_I(inode)->lock); |
| 6865 | BTRFS_I(inode)->outstanding_extents++; |
| 6866 | spin_unlock(&BTRFS_I(inode)->lock); |
| 6867 | |
Miao Xie | 0934856 | 2013-02-07 10:12:07 +0000 | [diff] [blame] | 6868 | ret = set_extent_bit(&BTRFS_I(inode)->io_tree, lockstart, |
| 6869 | lockstart + len - 1, EXTENT_DELALLOC, NULL, |
| 6870 | &cached_state, GFP_NOFS); |
| 6871 | BUG_ON(ret); |
Josef Bacik | c3473e8 | 2012-06-19 10:59:00 -0400 | [diff] [blame] | 6872 | } |
Josef Bacik | 4b46fce | 2010-05-23 11:00:55 -0400 | [diff] [blame] | 6873 | |
Josef Bacik | eb838e7 | 2012-07-31 16:28:48 -0400 | [diff] [blame] | 6874 | /* |
| 6875 | * In the case of write we need to clear and unlock the entire range, |
| 6876 | * in the case of read we need to unlock only the end area that we |
| 6877 | * aren't using if there is any left over space. |
| 6878 | */ |
Liu Bo | 24c03fa | 2012-08-22 20:10:38 -0600 | [diff] [blame] | 6879 | if (lockstart < lockend) { |
Miao Xie | 0934856 | 2013-02-07 10:12:07 +0000 | [diff] [blame] | 6880 | clear_extent_bit(&BTRFS_I(inode)->io_tree, lockstart, |
| 6881 | lockend, unlock_bits, 1, 0, |
| 6882 | &cached_state, GFP_NOFS); |
Liu Bo | 24c03fa | 2012-08-22 20:10:38 -0600 | [diff] [blame] | 6883 | } else { |
Josef Bacik | eb838e7 | 2012-07-31 16:28:48 -0400 | [diff] [blame] | 6884 | free_extent_state(cached_state); |
Liu Bo | 24c03fa | 2012-08-22 20:10:38 -0600 | [diff] [blame] | 6885 | } |
Josef Bacik | eb838e7 | 2012-07-31 16:28:48 -0400 | [diff] [blame] | 6886 | |
Josef Bacik | 4b46fce | 2010-05-23 11:00:55 -0400 | [diff] [blame] | 6887 | free_extent_map(em); |
| 6888 | |
| 6889 | return 0; |
Josef Bacik | eb838e7 | 2012-07-31 16:28:48 -0400 | [diff] [blame] | 6890 | |
| 6891 | unlock_err: |
Josef Bacik | eb838e7 | 2012-07-31 16:28:48 -0400 | [diff] [blame] | 6892 | clear_extent_bit(&BTRFS_I(inode)->io_tree, lockstart, lockend, |
| 6893 | unlock_bits, 1, 0, &cached_state, GFP_NOFS); |
| 6894 | return ret; |
Josef Bacik | 4b46fce | 2010-05-23 11:00:55 -0400 | [diff] [blame] | 6895 | } |
| 6896 | |
Josef Bacik | 4b46fce | 2010-05-23 11:00:55 -0400 | [diff] [blame] | 6897 | static void btrfs_endio_direct_read(struct bio *bio, int err) |
| 6898 | { |
Miao Xie | e65e153 | 2010-11-22 03:04:43 +0000 | [diff] [blame] | 6899 | struct btrfs_dio_private *dip = bio->bi_private; |
Josef Bacik | 4b46fce | 2010-05-23 11:00:55 -0400 | [diff] [blame] | 6900 | struct bio_vec *bvec_end = bio->bi_io_vec + bio->bi_vcnt - 1; |
| 6901 | struct bio_vec *bvec = bio->bi_io_vec; |
Josef Bacik | 4b46fce | 2010-05-23 11:00:55 -0400 | [diff] [blame] | 6902 | struct inode *inode = dip->inode; |
| 6903 | struct btrfs_root *root = BTRFS_I(inode)->root; |
Chris Mason | 9be3395 | 2013-05-17 18:30:14 -0400 | [diff] [blame] | 6904 | struct bio *dio_bio; |
Miao Xie | facc8a22 | 2013-07-25 19:22:34 +0800 | [diff] [blame] | 6905 | u32 *csums = (u32 *)dip->csum; |
| 6906 | int index = 0; |
Josef Bacik | 4b46fce | 2010-05-23 11:00:55 -0400 | [diff] [blame] | 6907 | u64 start; |
Josef Bacik | 4b46fce | 2010-05-23 11:00:55 -0400 | [diff] [blame] | 6908 | |
| 6909 | start = dip->logical_offset; |
| 6910 | do { |
| 6911 | if (!(BTRFS_I(inode)->flags & BTRFS_INODE_NODATASUM)) { |
| 6912 | struct page *page = bvec->bv_page; |
| 6913 | char *kaddr; |
| 6914 | u32 csum = ~(u32)0; |
| 6915 | unsigned long flags; |
| 6916 | |
| 6917 | local_irq_save(flags); |
Cong Wang | 7ac687d | 2011-11-25 23:14:28 +0800 | [diff] [blame] | 6918 | kaddr = kmap_atomic(page); |
Liu Bo | b049668 | 2013-03-14 14:57:45 +0000 | [diff] [blame] | 6919 | csum = btrfs_csum_data(kaddr + bvec->bv_offset, |
Josef Bacik | 4b46fce | 2010-05-23 11:00:55 -0400 | [diff] [blame] | 6920 | csum, bvec->bv_len); |
| 6921 | btrfs_csum_final(csum, (char *)&csum); |
Cong Wang | 7ac687d | 2011-11-25 23:14:28 +0800 | [diff] [blame] | 6922 | kunmap_atomic(kaddr); |
Josef Bacik | 4b46fce | 2010-05-23 11:00:55 -0400 | [diff] [blame] | 6923 | local_irq_restore(flags); |
| 6924 | |
| 6925 | flush_dcache_page(bvec->bv_page); |
Miao Xie | facc8a22 | 2013-07-25 19:22:34 +0800 | [diff] [blame] | 6926 | if (csum != csums[index]) { |
| 6927 | btrfs_err(root->fs_info, "csum failed ino %llu off %llu csum %u expected csum %u", |
Geert Uytterhoeven | c1c9ff7 | 2013-08-20 13:20:07 +0200 | [diff] [blame] | 6928 | btrfs_ino(inode), start, csum, |
| 6929 | csums[index]); |
Josef Bacik | 4b46fce | 2010-05-23 11:00:55 -0400 | [diff] [blame] | 6930 | err = -EIO; |
| 6931 | } |
| 6932 | } |
| 6933 | |
| 6934 | start += bvec->bv_len; |
Josef Bacik | 4b46fce | 2010-05-23 11:00:55 -0400 | [diff] [blame] | 6935 | bvec++; |
Miao Xie | facc8a22 | 2013-07-25 19:22:34 +0800 | [diff] [blame] | 6936 | index++; |
Josef Bacik | 4b46fce | 2010-05-23 11:00:55 -0400 | [diff] [blame] | 6937 | } while (bvec <= bvec_end); |
| 6938 | |
| 6939 | unlock_extent(&BTRFS_I(inode)->io_tree, dip->logical_offset, |
Jeff Mahoney | d008237 | 2012-03-01 14:57:19 +0100 | [diff] [blame] | 6940 | dip->logical_offset + dip->bytes - 1); |
Chris Mason | 9be3395 | 2013-05-17 18:30:14 -0400 | [diff] [blame] | 6941 | dio_bio = dip->dio_bio; |
Josef Bacik | 4b46fce | 2010-05-23 11:00:55 -0400 | [diff] [blame] | 6942 | |
Josef Bacik | 4b46fce | 2010-05-23 11:00:55 -0400 | [diff] [blame] | 6943 | kfree(dip); |
Josef Bacik | c0da7aa | 2011-03-22 11:05:07 -0400 | [diff] [blame] | 6944 | |
| 6945 | /* If we had a csum failure make sure to clear the uptodate flag */ |
| 6946 | if (err) |
Chris Mason | 9be3395 | 2013-05-17 18:30:14 -0400 | [diff] [blame] | 6947 | clear_bit(BIO_UPTODATE, &dio_bio->bi_flags); |
| 6948 | dio_end_io(dio_bio, err); |
| 6949 | bio_put(bio); |
Josef Bacik | 4b46fce | 2010-05-23 11:00:55 -0400 | [diff] [blame] | 6950 | } |
| 6951 | |
| 6952 | static void btrfs_endio_direct_write(struct bio *bio, int err) |
| 6953 | { |
| 6954 | struct btrfs_dio_private *dip = bio->bi_private; |
| 6955 | struct inode *inode = dip->inode; |
| 6956 | struct btrfs_root *root = BTRFS_I(inode)->root; |
Josef Bacik | 4b46fce | 2010-05-23 11:00:55 -0400 | [diff] [blame] | 6957 | struct btrfs_ordered_extent *ordered = NULL; |
Chris Mason | 163cf09 | 2010-11-28 19:56:33 -0500 | [diff] [blame] | 6958 | u64 ordered_offset = dip->logical_offset; |
| 6959 | u64 ordered_bytes = dip->bytes; |
Chris Mason | 9be3395 | 2013-05-17 18:30:14 -0400 | [diff] [blame] | 6960 | struct bio *dio_bio; |
Josef Bacik | 4b46fce | 2010-05-23 11:00:55 -0400 | [diff] [blame] | 6961 | int ret; |
| 6962 | |
| 6963 | if (err) |
| 6964 | goto out_done; |
Chris Mason | 163cf09 | 2010-11-28 19:56:33 -0500 | [diff] [blame] | 6965 | again: |
| 6966 | ret = btrfs_dec_test_first_ordered_pending(inode, &ordered, |
| 6967 | &ordered_offset, |
Josef Bacik | 5fd0204 | 2012-05-02 14:00:54 -0400 | [diff] [blame] | 6968 | ordered_bytes, !err); |
Josef Bacik | 4b46fce | 2010-05-23 11:00:55 -0400 | [diff] [blame] | 6969 | if (!ret) |
Chris Mason | 163cf09 | 2010-11-28 19:56:33 -0500 | [diff] [blame] | 6970 | goto out_test; |
Josef Bacik | 4b46fce | 2010-05-23 11:00:55 -0400 | [diff] [blame] | 6971 | |
Josef Bacik | 5fd0204 | 2012-05-02 14:00:54 -0400 | [diff] [blame] | 6972 | ordered->work.func = finish_ordered_fn; |
| 6973 | ordered->work.flags = 0; |
| 6974 | btrfs_queue_worker(&root->fs_info->endio_write_workers, |
| 6975 | &ordered->work); |
Chris Mason | 163cf09 | 2010-11-28 19:56:33 -0500 | [diff] [blame] | 6976 | out_test: |
| 6977 | /* |
| 6978 | * our bio might span multiple ordered extents. If we haven't |
| 6979 | * completed the accounting for the whole dio, go back and try again |
| 6980 | */ |
| 6981 | if (ordered_offset < dip->logical_offset + dip->bytes) { |
| 6982 | ordered_bytes = dip->logical_offset + dip->bytes - |
| 6983 | ordered_offset; |
Josef Bacik | 5fd0204 | 2012-05-02 14:00:54 -0400 | [diff] [blame] | 6984 | ordered = NULL; |
Chris Mason | 163cf09 | 2010-11-28 19:56:33 -0500 | [diff] [blame] | 6985 | goto again; |
| 6986 | } |
Josef Bacik | 4b46fce | 2010-05-23 11:00:55 -0400 | [diff] [blame] | 6987 | out_done: |
Chris Mason | 9be3395 | 2013-05-17 18:30:14 -0400 | [diff] [blame] | 6988 | dio_bio = dip->dio_bio; |
Josef Bacik | 4b46fce | 2010-05-23 11:00:55 -0400 | [diff] [blame] | 6989 | |
Josef Bacik | 4b46fce | 2010-05-23 11:00:55 -0400 | [diff] [blame] | 6990 | kfree(dip); |
Josef Bacik | c0da7aa | 2011-03-22 11:05:07 -0400 | [diff] [blame] | 6991 | |
| 6992 | /* If we had an error make sure to clear the uptodate flag */ |
| 6993 | if (err) |
Chris Mason | 9be3395 | 2013-05-17 18:30:14 -0400 | [diff] [blame] | 6994 | clear_bit(BIO_UPTODATE, &dio_bio->bi_flags); |
| 6995 | dio_end_io(dio_bio, err); |
| 6996 | bio_put(bio); |
Josef Bacik | 4b46fce | 2010-05-23 11:00:55 -0400 | [diff] [blame] | 6997 | } |
| 6998 | |
Chris Mason | eaf25d9 | 2010-05-25 09:48:28 -0400 | [diff] [blame] | 6999 | static int __btrfs_submit_bio_start_direct_io(struct inode *inode, int rw, |
| 7000 | struct bio *bio, int mirror_num, |
| 7001 | unsigned long bio_flags, u64 offset) |
| 7002 | { |
| 7003 | int ret; |
| 7004 | struct btrfs_root *root = BTRFS_I(inode)->root; |
| 7005 | ret = btrfs_csum_one_bio(root, inode, bio, offset, 1); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 7006 | BUG_ON(ret); /* -ENOMEM */ |
Chris Mason | eaf25d9 | 2010-05-25 09:48:28 -0400 | [diff] [blame] | 7007 | return 0; |
| 7008 | } |
| 7009 | |
Miao Xie | e65e153 | 2010-11-22 03:04:43 +0000 | [diff] [blame] | 7010 | static void btrfs_end_dio_bio(struct bio *bio, int err) |
| 7011 | { |
| 7012 | struct btrfs_dio_private *dip = bio->bi_private; |
| 7013 | |
| 7014 | if (err) { |
Frank Holton | efe120a | 2013-12-20 11:37:06 -0500 | [diff] [blame] | 7015 | btrfs_err(BTRFS_I(dip->inode)->root->fs_info, |
| 7016 | "direct IO failed ino %llu rw %lu sector %#Lx len %u err no %d", |
Geert Uytterhoeven | c1c9ff7 | 2013-08-20 13:20:07 +0200 | [diff] [blame] | 7017 | btrfs_ino(dip->inode), bio->bi_rw, |
Jan Beulich | 3dd1462 | 2010-12-07 14:54:09 +0000 | [diff] [blame] | 7018 | (unsigned long long)bio->bi_sector, bio->bi_size, err); |
Miao Xie | e65e153 | 2010-11-22 03:04:43 +0000 | [diff] [blame] | 7019 | dip->errors = 1; |
| 7020 | |
| 7021 | /* |
| 7022 | * before atomic variable goto zero, we must make sure |
| 7023 | * dip->errors is perceived to be set. |
| 7024 | */ |
| 7025 | smp_mb__before_atomic_dec(); |
| 7026 | } |
| 7027 | |
| 7028 | /* if there are more bios still pending for this dio, just exit */ |
| 7029 | if (!atomic_dec_and_test(&dip->pending_bios)) |
| 7030 | goto out; |
| 7031 | |
Chris Mason | 9be3395 | 2013-05-17 18:30:14 -0400 | [diff] [blame] | 7032 | if (dip->errors) { |
Miao Xie | e65e153 | 2010-11-22 03:04:43 +0000 | [diff] [blame] | 7033 | bio_io_error(dip->orig_bio); |
Chris Mason | 9be3395 | 2013-05-17 18:30:14 -0400 | [diff] [blame] | 7034 | } else { |
| 7035 | set_bit(BIO_UPTODATE, &dip->dio_bio->bi_flags); |
Miao Xie | e65e153 | 2010-11-22 03:04:43 +0000 | [diff] [blame] | 7036 | bio_endio(dip->orig_bio, 0); |
| 7037 | } |
| 7038 | out: |
| 7039 | bio_put(bio); |
| 7040 | } |
| 7041 | |
| 7042 | static struct bio *btrfs_dio_bio_alloc(struct block_device *bdev, |
| 7043 | u64 first_sector, gfp_t gfp_flags) |
| 7044 | { |
| 7045 | int nr_vecs = bio_get_nr_vecs(bdev); |
| 7046 | return btrfs_bio_alloc(bdev, first_sector, nr_vecs, gfp_flags); |
| 7047 | } |
| 7048 | |
| 7049 | static inline int __btrfs_submit_dio_bio(struct bio *bio, struct inode *inode, |
| 7050 | int rw, u64 file_offset, int skip_sum, |
Josef Bacik | c329861 | 2012-08-03 16:49:19 -0400 | [diff] [blame] | 7051 | int async_submit) |
Miao Xie | e65e153 | 2010-11-22 03:04:43 +0000 | [diff] [blame] | 7052 | { |
Miao Xie | facc8a22 | 2013-07-25 19:22:34 +0800 | [diff] [blame] | 7053 | struct btrfs_dio_private *dip = bio->bi_private; |
Miao Xie | e65e153 | 2010-11-22 03:04:43 +0000 | [diff] [blame] | 7054 | int write = rw & REQ_WRITE; |
| 7055 | struct btrfs_root *root = BTRFS_I(inode)->root; |
| 7056 | int ret; |
| 7057 | |
Josef Bacik | b812ce2 | 2012-11-16 13:56:32 -0500 | [diff] [blame] | 7058 | if (async_submit) |
| 7059 | async_submit = !atomic_read(&BTRFS_I(inode)->sync_writers); |
| 7060 | |
Miao Xie | e65e153 | 2010-11-22 03:04:43 +0000 | [diff] [blame] | 7061 | bio_get(bio); |
Josef Bacik | 5fd0204 | 2012-05-02 14:00:54 -0400 | [diff] [blame] | 7062 | |
| 7063 | if (!write) { |
| 7064 | ret = btrfs_bio_wq_end_io(root->fs_info, bio, 0); |
| 7065 | if (ret) |
| 7066 | goto err; |
| 7067 | } |
Miao Xie | e65e153 | 2010-11-22 03:04:43 +0000 | [diff] [blame] | 7068 | |
Josef Bacik | 1ae3993 | 2011-04-06 14:41:34 -0400 | [diff] [blame] | 7069 | if (skip_sum) |
| 7070 | goto map; |
| 7071 | |
| 7072 | if (write && async_submit) { |
Miao Xie | e65e153 | 2010-11-22 03:04:43 +0000 | [diff] [blame] | 7073 | ret = btrfs_wq_submit_bio(root->fs_info, |
| 7074 | inode, rw, bio, 0, 0, |
| 7075 | file_offset, |
| 7076 | __btrfs_submit_bio_start_direct_io, |
| 7077 | __btrfs_submit_bio_done); |
| 7078 | goto err; |
Josef Bacik | 1ae3993 | 2011-04-06 14:41:34 -0400 | [diff] [blame] | 7079 | } else if (write) { |
| 7080 | /* |
| 7081 | * If we aren't doing async submit, calculate the csum of the |
| 7082 | * bio now. |
| 7083 | */ |
| 7084 | ret = btrfs_csum_one_bio(root, inode, bio, file_offset, 1); |
| 7085 | if (ret) |
| 7086 | goto err; |
Tsutomu Itoh | c2db107 | 2011-03-01 06:48:31 +0000 | [diff] [blame] | 7087 | } else if (!skip_sum) { |
Miao Xie | facc8a22 | 2013-07-25 19:22:34 +0800 | [diff] [blame] | 7088 | ret = btrfs_lookup_bio_sums_dio(root, inode, dip, bio, |
| 7089 | file_offset); |
Tsutomu Itoh | c2db107 | 2011-03-01 06:48:31 +0000 | [diff] [blame] | 7090 | if (ret) |
| 7091 | goto err; |
| 7092 | } |
Miao Xie | e65e153 | 2010-11-22 03:04:43 +0000 | [diff] [blame] | 7093 | |
Josef Bacik | 1ae3993 | 2011-04-06 14:41:34 -0400 | [diff] [blame] | 7094 | map: |
| 7095 | ret = btrfs_map_bio(root, rw, bio, 0, async_submit); |
Miao Xie | e65e153 | 2010-11-22 03:04:43 +0000 | [diff] [blame] | 7096 | err: |
| 7097 | bio_put(bio); |
| 7098 | return ret; |
| 7099 | } |
| 7100 | |
| 7101 | static int btrfs_submit_direct_hook(int rw, struct btrfs_dio_private *dip, |
| 7102 | int skip_sum) |
| 7103 | { |
| 7104 | struct inode *inode = dip->inode; |
| 7105 | struct btrfs_root *root = BTRFS_I(inode)->root; |
Miao Xie | e65e153 | 2010-11-22 03:04:43 +0000 | [diff] [blame] | 7106 | struct bio *bio; |
| 7107 | struct bio *orig_bio = dip->orig_bio; |
| 7108 | struct bio_vec *bvec = orig_bio->bi_io_vec; |
| 7109 | u64 start_sector = orig_bio->bi_sector; |
| 7110 | u64 file_offset = dip->logical_offset; |
| 7111 | u64 submit_len = 0; |
| 7112 | u64 map_length; |
| 7113 | int nr_pages = 0; |
Miao Xie | e65e153 | 2010-11-22 03:04:43 +0000 | [diff] [blame] | 7114 | int ret = 0; |
Josef Bacik | 1ae3993 | 2011-04-06 14:41:34 -0400 | [diff] [blame] | 7115 | int async_submit = 0; |
Miao Xie | e65e153 | 2010-11-22 03:04:43 +0000 | [diff] [blame] | 7116 | |
Miao Xie | e65e153 | 2010-11-22 03:04:43 +0000 | [diff] [blame] | 7117 | map_length = orig_bio->bi_size; |
David Woodhouse | 53b381b | 2013-01-29 18:40:14 -0500 | [diff] [blame] | 7118 | ret = btrfs_map_block(root->fs_info, rw, start_sector << 9, |
Miao Xie | e65e153 | 2010-11-22 03:04:43 +0000 | [diff] [blame] | 7119 | &map_length, NULL, 0); |
| 7120 | if (ret) { |
Josef Bacik | 64728bb | 2011-04-25 19:43:52 -0400 | [diff] [blame] | 7121 | bio_put(orig_bio); |
Miao Xie | e65e153 | 2010-11-22 03:04:43 +0000 | [diff] [blame] | 7122 | return -EIO; |
| 7123 | } |
Miao Xie | facc8a22 | 2013-07-25 19:22:34 +0800 | [diff] [blame] | 7124 | |
Josef Bacik | 02f57c7 | 2011-04-06 14:25:44 -0400 | [diff] [blame] | 7125 | if (map_length >= orig_bio->bi_size) { |
| 7126 | bio = orig_bio; |
| 7127 | goto submit; |
| 7128 | } |
| 7129 | |
David Woodhouse | 53b381b | 2013-01-29 18:40:14 -0500 | [diff] [blame] | 7130 | /* async crcs make it difficult to collect full stripe writes. */ |
| 7131 | if (btrfs_get_alloc_profile(root, 1) & |
| 7132 | (BTRFS_BLOCK_GROUP_RAID5 | BTRFS_BLOCK_GROUP_RAID6)) |
| 7133 | async_submit = 0; |
| 7134 | else |
| 7135 | async_submit = 1; |
| 7136 | |
Josef Bacik | 02f57c7 | 2011-04-06 14:25:44 -0400 | [diff] [blame] | 7137 | bio = btrfs_dio_bio_alloc(orig_bio->bi_bdev, start_sector, GFP_NOFS); |
| 7138 | if (!bio) |
| 7139 | return -ENOMEM; |
| 7140 | bio->bi_private = dip; |
| 7141 | bio->bi_end_io = btrfs_end_dio_bio; |
| 7142 | atomic_inc(&dip->pending_bios); |
| 7143 | |
Miao Xie | e65e153 | 2010-11-22 03:04:43 +0000 | [diff] [blame] | 7144 | while (bvec <= (orig_bio->bi_io_vec + orig_bio->bi_vcnt - 1)) { |
| 7145 | if (unlikely(map_length < submit_len + bvec->bv_len || |
| 7146 | bio_add_page(bio, bvec->bv_page, bvec->bv_len, |
| 7147 | bvec->bv_offset) < bvec->bv_len)) { |
| 7148 | /* |
| 7149 | * inc the count before we submit the bio so |
| 7150 | * we know the end IO handler won't happen before |
| 7151 | * we inc the count. Otherwise, the dip might get freed |
| 7152 | * before we're done setting it up |
| 7153 | */ |
| 7154 | atomic_inc(&dip->pending_bios); |
| 7155 | ret = __btrfs_submit_dio_bio(bio, inode, rw, |
| 7156 | file_offset, skip_sum, |
Josef Bacik | c329861 | 2012-08-03 16:49:19 -0400 | [diff] [blame] | 7157 | async_submit); |
Miao Xie | e65e153 | 2010-11-22 03:04:43 +0000 | [diff] [blame] | 7158 | if (ret) { |
| 7159 | bio_put(bio); |
| 7160 | atomic_dec(&dip->pending_bios); |
| 7161 | goto out_err; |
| 7162 | } |
| 7163 | |
Miao Xie | e65e153 | 2010-11-22 03:04:43 +0000 | [diff] [blame] | 7164 | start_sector += submit_len >> 9; |
| 7165 | file_offset += submit_len; |
| 7166 | |
| 7167 | submit_len = 0; |
| 7168 | nr_pages = 0; |
| 7169 | |
| 7170 | bio = btrfs_dio_bio_alloc(orig_bio->bi_bdev, |
| 7171 | start_sector, GFP_NOFS); |
| 7172 | if (!bio) |
| 7173 | goto out_err; |
| 7174 | bio->bi_private = dip; |
| 7175 | bio->bi_end_io = btrfs_end_dio_bio; |
| 7176 | |
| 7177 | map_length = orig_bio->bi_size; |
David Woodhouse | 53b381b | 2013-01-29 18:40:14 -0500 | [diff] [blame] | 7178 | ret = btrfs_map_block(root->fs_info, rw, |
Stefan Behrens | 3ec706c | 2012-11-05 15:46:42 +0100 | [diff] [blame] | 7179 | start_sector << 9, |
Miao Xie | e65e153 | 2010-11-22 03:04:43 +0000 | [diff] [blame] | 7180 | &map_length, NULL, 0); |
| 7181 | if (ret) { |
| 7182 | bio_put(bio); |
| 7183 | goto out_err; |
| 7184 | } |
| 7185 | } else { |
| 7186 | submit_len += bvec->bv_len; |
Dulshani Gunawardhana | 6787125 | 2013-10-31 10:33:04 +0530 | [diff] [blame] | 7187 | nr_pages++; |
Miao Xie | e65e153 | 2010-11-22 03:04:43 +0000 | [diff] [blame] | 7188 | bvec++; |
| 7189 | } |
| 7190 | } |
| 7191 | |
Josef Bacik | 02f57c7 | 2011-04-06 14:25:44 -0400 | [diff] [blame] | 7192 | submit: |
Miao Xie | e65e153 | 2010-11-22 03:04:43 +0000 | [diff] [blame] | 7193 | ret = __btrfs_submit_dio_bio(bio, inode, rw, file_offset, skip_sum, |
Josef Bacik | c329861 | 2012-08-03 16:49:19 -0400 | [diff] [blame] | 7194 | async_submit); |
Miao Xie | e65e153 | 2010-11-22 03:04:43 +0000 | [diff] [blame] | 7195 | if (!ret) |
| 7196 | return 0; |
| 7197 | |
| 7198 | bio_put(bio); |
| 7199 | out_err: |
| 7200 | dip->errors = 1; |
| 7201 | /* |
| 7202 | * before atomic variable goto zero, we must |
| 7203 | * make sure dip->errors is perceived to be set. |
| 7204 | */ |
| 7205 | smp_mb__before_atomic_dec(); |
| 7206 | if (atomic_dec_and_test(&dip->pending_bios)) |
| 7207 | bio_io_error(dip->orig_bio); |
| 7208 | |
| 7209 | /* bio_end_io() will handle error, so we needn't return it */ |
| 7210 | return 0; |
| 7211 | } |
| 7212 | |
Chris Mason | 9be3395 | 2013-05-17 18:30:14 -0400 | [diff] [blame] | 7213 | static void btrfs_submit_direct(int rw, struct bio *dio_bio, |
| 7214 | struct inode *inode, loff_t file_offset) |
Josef Bacik | 4b46fce | 2010-05-23 11:00:55 -0400 | [diff] [blame] | 7215 | { |
| 7216 | struct btrfs_root *root = BTRFS_I(inode)->root; |
| 7217 | struct btrfs_dio_private *dip; |
Chris Mason | 9be3395 | 2013-05-17 18:30:14 -0400 | [diff] [blame] | 7218 | struct bio *io_bio; |
Josef Bacik | 4b46fce | 2010-05-23 11:00:55 -0400 | [diff] [blame] | 7219 | int skip_sum; |
Miao Xie | facc8a22 | 2013-07-25 19:22:34 +0800 | [diff] [blame] | 7220 | int sum_len; |
Christoph Hellwig | 7b6d91d | 2010-08-07 18:20:39 +0200 | [diff] [blame] | 7221 | int write = rw & REQ_WRITE; |
Josef Bacik | 4b46fce | 2010-05-23 11:00:55 -0400 | [diff] [blame] | 7222 | int ret = 0; |
Miao Xie | facc8a22 | 2013-07-25 19:22:34 +0800 | [diff] [blame] | 7223 | u16 csum_size; |
Josef Bacik | 4b46fce | 2010-05-23 11:00:55 -0400 | [diff] [blame] | 7224 | |
| 7225 | skip_sum = BTRFS_I(inode)->flags & BTRFS_INODE_NODATASUM; |
| 7226 | |
Chris Mason | 9be3395 | 2013-05-17 18:30:14 -0400 | [diff] [blame] | 7227 | io_bio = btrfs_bio_clone(dio_bio, GFP_NOFS); |
Chris Mason | 9be3395 | 2013-05-17 18:30:14 -0400 | [diff] [blame] | 7228 | if (!io_bio) { |
Josef Bacik | 4b46fce | 2010-05-23 11:00:55 -0400 | [diff] [blame] | 7229 | ret = -ENOMEM; |
| 7230 | goto free_ordered; |
| 7231 | } |
Josef Bacik | 4b46fce | 2010-05-23 11:00:55 -0400 | [diff] [blame] | 7232 | |
Miao Xie | facc8a22 | 2013-07-25 19:22:34 +0800 | [diff] [blame] | 7233 | if (!skip_sum && !write) { |
| 7234 | csum_size = btrfs_super_csum_size(root->fs_info->super_copy); |
| 7235 | sum_len = dio_bio->bi_size >> inode->i_sb->s_blocksize_bits; |
| 7236 | sum_len *= csum_size; |
| 7237 | } else { |
| 7238 | sum_len = 0; |
| 7239 | } |
| 7240 | |
| 7241 | dip = kmalloc(sizeof(*dip) + sum_len, GFP_NOFS); |
Chris Mason | 9be3395 | 2013-05-17 18:30:14 -0400 | [diff] [blame] | 7242 | if (!dip) { |
| 7243 | ret = -ENOMEM; |
| 7244 | goto free_io_bio; |
| 7245 | } |
| 7246 | |
| 7247 | dip->private = dio_bio->bi_private; |
Josef Bacik | 4b46fce | 2010-05-23 11:00:55 -0400 | [diff] [blame] | 7248 | dip->inode = inode; |
| 7249 | dip->logical_offset = file_offset; |
Miao Xie | e6da5d2e | 2013-06-19 18:19:17 +0800 | [diff] [blame] | 7250 | dip->bytes = dio_bio->bi_size; |
Chris Mason | 9be3395 | 2013-05-17 18:30:14 -0400 | [diff] [blame] | 7251 | dip->disk_bytenr = (u64)dio_bio->bi_sector << 9; |
| 7252 | io_bio->bi_private = dip; |
Miao Xie | e65e153 | 2010-11-22 03:04:43 +0000 | [diff] [blame] | 7253 | dip->errors = 0; |
Chris Mason | 9be3395 | 2013-05-17 18:30:14 -0400 | [diff] [blame] | 7254 | dip->orig_bio = io_bio; |
| 7255 | dip->dio_bio = dio_bio; |
Miao Xie | e65e153 | 2010-11-22 03:04:43 +0000 | [diff] [blame] | 7256 | atomic_set(&dip->pending_bios, 0); |
Josef Bacik | 4b46fce | 2010-05-23 11:00:55 -0400 | [diff] [blame] | 7257 | |
| 7258 | if (write) |
Chris Mason | 9be3395 | 2013-05-17 18:30:14 -0400 | [diff] [blame] | 7259 | io_bio->bi_end_io = btrfs_endio_direct_write; |
Josef Bacik | 4b46fce | 2010-05-23 11:00:55 -0400 | [diff] [blame] | 7260 | else |
Chris Mason | 9be3395 | 2013-05-17 18:30:14 -0400 | [diff] [blame] | 7261 | io_bio->bi_end_io = btrfs_endio_direct_read; |
Josef Bacik | 4b46fce | 2010-05-23 11:00:55 -0400 | [diff] [blame] | 7262 | |
Miao Xie | e65e153 | 2010-11-22 03:04:43 +0000 | [diff] [blame] | 7263 | ret = btrfs_submit_direct_hook(rw, dip, skip_sum); |
| 7264 | if (!ret) |
Chris Mason | eaf25d9 | 2010-05-25 09:48:28 -0400 | [diff] [blame] | 7265 | return; |
Chris Mason | 9be3395 | 2013-05-17 18:30:14 -0400 | [diff] [blame] | 7266 | |
| 7267 | free_io_bio: |
| 7268 | bio_put(io_bio); |
| 7269 | |
Josef Bacik | 4b46fce | 2010-05-23 11:00:55 -0400 | [diff] [blame] | 7270 | free_ordered: |
| 7271 | /* |
| 7272 | * If this is a write, we need to clean up the reserved space and kill |
| 7273 | * the ordered extent. |
| 7274 | */ |
| 7275 | if (write) { |
| 7276 | struct btrfs_ordered_extent *ordered; |
Josef Bacik | 955256f | 2010-11-19 09:41:10 -0500 | [diff] [blame] | 7277 | ordered = btrfs_lookup_ordered_extent(inode, file_offset); |
Josef Bacik | 4b46fce | 2010-05-23 11:00:55 -0400 | [diff] [blame] | 7278 | if (!test_bit(BTRFS_ORDERED_PREALLOC, &ordered->flags) && |
| 7279 | !test_bit(BTRFS_ORDERED_NOCOW, &ordered->flags)) |
| 7280 | btrfs_free_reserved_extent(root, ordered->start, |
| 7281 | ordered->disk_len); |
| 7282 | btrfs_put_ordered_extent(ordered); |
| 7283 | btrfs_put_ordered_extent(ordered); |
| 7284 | } |
Chris Mason | 9be3395 | 2013-05-17 18:30:14 -0400 | [diff] [blame] | 7285 | bio_endio(dio_bio, ret); |
Josef Bacik | 4b46fce | 2010-05-23 11:00:55 -0400 | [diff] [blame] | 7286 | } |
| 7287 | |
Chris Mason | 5a5f79b | 2010-05-26 21:33:37 -0400 | [diff] [blame] | 7288 | static ssize_t check_direct_IO(struct btrfs_root *root, int rw, struct kiocb *iocb, |
| 7289 | const struct iovec *iov, loff_t offset, |
| 7290 | unsigned long nr_segs) |
| 7291 | { |
| 7292 | int seg; |
Josef Bacik | a1b75f7 | 2011-04-08 15:51:18 +0000 | [diff] [blame] | 7293 | int i; |
Chris Mason | 5a5f79b | 2010-05-26 21:33:37 -0400 | [diff] [blame] | 7294 | size_t size; |
| 7295 | unsigned long addr; |
| 7296 | unsigned blocksize_mask = root->sectorsize - 1; |
| 7297 | ssize_t retval = -EINVAL; |
| 7298 | loff_t end = offset; |
| 7299 | |
| 7300 | if (offset & blocksize_mask) |
| 7301 | goto out; |
| 7302 | |
| 7303 | /* Check the memory alignment. Blocks cannot straddle pages */ |
| 7304 | for (seg = 0; seg < nr_segs; seg++) { |
| 7305 | addr = (unsigned long)iov[seg].iov_base; |
| 7306 | size = iov[seg].iov_len; |
| 7307 | end += size; |
Josef Bacik | a1b75f7 | 2011-04-08 15:51:18 +0000 | [diff] [blame] | 7308 | if ((addr & blocksize_mask) || (size & blocksize_mask)) |
Chris Mason | 5a5f79b | 2010-05-26 21:33:37 -0400 | [diff] [blame] | 7309 | goto out; |
Josef Bacik | a1b75f7 | 2011-04-08 15:51:18 +0000 | [diff] [blame] | 7310 | |
| 7311 | /* If this is a write we don't need to check anymore */ |
| 7312 | if (rw & WRITE) |
| 7313 | continue; |
| 7314 | |
| 7315 | /* |
| 7316 | * Check to make sure we don't have duplicate iov_base's in this |
| 7317 | * iovec, if so return EINVAL, otherwise we'll get csum errors |
| 7318 | * when reading back. |
| 7319 | */ |
| 7320 | for (i = seg + 1; i < nr_segs; i++) { |
| 7321 | if (iov[seg].iov_base == iov[i].iov_base) |
| 7322 | goto out; |
| 7323 | } |
Chris Mason | 5a5f79b | 2010-05-26 21:33:37 -0400 | [diff] [blame] | 7324 | } |
| 7325 | retval = 0; |
| 7326 | out: |
| 7327 | return retval; |
| 7328 | } |
Josef Bacik | eb838e7 | 2012-07-31 16:28:48 -0400 | [diff] [blame] | 7329 | |
Chris Mason | 1643298 | 2008-04-10 10:23:21 -0400 | [diff] [blame] | 7330 | static ssize_t btrfs_direct_IO(int rw, struct kiocb *iocb, |
| 7331 | const struct iovec *iov, loff_t offset, |
| 7332 | unsigned long nr_segs) |
| 7333 | { |
Josef Bacik | 4b46fce | 2010-05-23 11:00:55 -0400 | [diff] [blame] | 7334 | struct file *file = iocb->ki_filp; |
| 7335 | struct inode *inode = file->f_mapping->host; |
Miao Xie | 0934856 | 2013-02-07 10:12:07 +0000 | [diff] [blame] | 7336 | size_t count = 0; |
Miao Xie | 2e60a51 | 2013-02-08 07:01:08 +0000 | [diff] [blame] | 7337 | int flags = 0; |
Miao Xie | 38851cc | 2013-02-08 07:04:11 +0000 | [diff] [blame] | 7338 | bool wakeup = true; |
| 7339 | bool relock = false; |
Miao Xie | 0934856 | 2013-02-07 10:12:07 +0000 | [diff] [blame] | 7340 | ssize_t ret; |
Josef Bacik | 4b46fce | 2010-05-23 11:00:55 -0400 | [diff] [blame] | 7341 | |
Chris Mason | 5a5f79b | 2010-05-26 21:33:37 -0400 | [diff] [blame] | 7342 | if (check_direct_IO(BTRFS_I(inode)->root, rw, iocb, iov, |
Josef Bacik | eb838e7 | 2012-07-31 16:28:48 -0400 | [diff] [blame] | 7343 | offset, nr_segs)) |
Chris Mason | 5a5f79b | 2010-05-26 21:33:37 -0400 | [diff] [blame] | 7344 | return 0; |
Chris Mason | 5a5f79b | 2010-05-26 21:33:37 -0400 | [diff] [blame] | 7345 | |
Miao Xie | 38851cc | 2013-02-08 07:04:11 +0000 | [diff] [blame] | 7346 | atomic_inc(&inode->i_dio_count); |
| 7347 | smp_mb__after_atomic_inc(); |
| 7348 | |
Josef Bacik | 0e267c4 | 2013-07-02 10:38:02 -0400 | [diff] [blame] | 7349 | /* |
| 7350 | * The generic stuff only does filemap_write_and_wait_range, which isn't |
| 7351 | * enough if we've written compressed pages to this area, so we need to |
| 7352 | * call btrfs_wait_ordered_range to make absolutely sure that any |
| 7353 | * outstanding dirty pages are on disk. |
| 7354 | */ |
| 7355 | count = iov_length(iov, nr_segs); |
Josef Bacik | 0ef8b72 | 2013-10-25 16:13:35 -0400 | [diff] [blame] | 7356 | ret = btrfs_wait_ordered_range(inode, offset, count); |
| 7357 | if (ret) |
| 7358 | return ret; |
Josef Bacik | 0e267c4 | 2013-07-02 10:38:02 -0400 | [diff] [blame] | 7359 | |
Miao Xie | 0934856 | 2013-02-07 10:12:07 +0000 | [diff] [blame] | 7360 | if (rw & WRITE) { |
Miao Xie | 38851cc | 2013-02-08 07:04:11 +0000 | [diff] [blame] | 7361 | /* |
| 7362 | * If the write DIO is beyond the EOF, we need update |
| 7363 | * the isize, but it is protected by i_mutex. So we can |
| 7364 | * not unlock the i_mutex at this case. |
| 7365 | */ |
| 7366 | if (offset + count <= inode->i_size) { |
| 7367 | mutex_unlock(&inode->i_mutex); |
| 7368 | relock = true; |
| 7369 | } |
Miao Xie | 0934856 | 2013-02-07 10:12:07 +0000 | [diff] [blame] | 7370 | ret = btrfs_delalloc_reserve_space(inode, count); |
| 7371 | if (ret) |
Miao Xie | 38851cc | 2013-02-08 07:04:11 +0000 | [diff] [blame] | 7372 | goto out; |
| 7373 | } else if (unlikely(test_bit(BTRFS_INODE_READDIO_NEED_LOCK, |
| 7374 | &BTRFS_I(inode)->runtime_flags))) { |
| 7375 | inode_dio_done(inode); |
| 7376 | flags = DIO_LOCKING | DIO_SKIP_HOLES; |
| 7377 | wakeup = false; |
Miao Xie | 0934856 | 2013-02-07 10:12:07 +0000 | [diff] [blame] | 7378 | } |
| 7379 | |
| 7380 | ret = __blockdev_direct_IO(rw, iocb, inode, |
| 7381 | BTRFS_I(inode)->root->fs_info->fs_devices->latest_bdev, |
| 7382 | iov, offset, nr_segs, btrfs_get_blocks_direct, NULL, |
Miao Xie | 2e60a51 | 2013-02-08 07:01:08 +0000 | [diff] [blame] | 7383 | btrfs_submit_direct, flags); |
Miao Xie | 0934856 | 2013-02-07 10:12:07 +0000 | [diff] [blame] | 7384 | if (rw & WRITE) { |
| 7385 | if (ret < 0 && ret != -EIOCBQUEUED) |
| 7386 | btrfs_delalloc_release_space(inode, count); |
Miao Xie | 172a504 | 2013-02-21 02:48:22 -0700 | [diff] [blame] | 7387 | else if (ret >= 0 && (size_t)ret < count) |
Miao Xie | 0934856 | 2013-02-07 10:12:07 +0000 | [diff] [blame] | 7388 | btrfs_delalloc_release_space(inode, |
| 7389 | count - (size_t)ret); |
Miao Xie | 172a504 | 2013-02-21 02:48:22 -0700 | [diff] [blame] | 7390 | else |
| 7391 | btrfs_delalloc_release_metadata(inode, 0); |
Miao Xie | 0934856 | 2013-02-07 10:12:07 +0000 | [diff] [blame] | 7392 | } |
Miao Xie | 38851cc | 2013-02-08 07:04:11 +0000 | [diff] [blame] | 7393 | out: |
Miao Xie | 2e60a51 | 2013-02-08 07:01:08 +0000 | [diff] [blame] | 7394 | if (wakeup) |
| 7395 | inode_dio_done(inode); |
Miao Xie | 38851cc | 2013-02-08 07:04:11 +0000 | [diff] [blame] | 7396 | if (relock) |
| 7397 | mutex_lock(&inode->i_mutex); |
Miao Xie | 0934856 | 2013-02-07 10:12:07 +0000 | [diff] [blame] | 7398 | |
| 7399 | return ret; |
Chris Mason | 1643298 | 2008-04-10 10:23:21 -0400 | [diff] [blame] | 7400 | } |
| 7401 | |
Tsutomu Itoh | 05dadc0 | 2012-11-29 05:08:26 +0000 | [diff] [blame] | 7402 | #define BTRFS_FIEMAP_FLAGS (FIEMAP_FLAG_SYNC) |
| 7403 | |
Yehuda Sadeh | 1506fcc | 2009-01-21 14:39:14 -0500 | [diff] [blame] | 7404 | static int btrfs_fiemap(struct inode *inode, struct fiemap_extent_info *fieinfo, |
| 7405 | __u64 start, __u64 len) |
| 7406 | { |
Tsutomu Itoh | 05dadc0 | 2012-11-29 05:08:26 +0000 | [diff] [blame] | 7407 | int ret; |
| 7408 | |
| 7409 | ret = fiemap_check_flags(fieinfo, BTRFS_FIEMAP_FLAGS); |
| 7410 | if (ret) |
| 7411 | return ret; |
| 7412 | |
Chris Mason | ec29ed5 | 2011-02-23 16:23:20 -0500 | [diff] [blame] | 7413 | return extent_fiemap(inode, fieinfo, start, len, btrfs_get_extent_fiemap); |
Yehuda Sadeh | 1506fcc | 2009-01-21 14:39:14 -0500 | [diff] [blame] | 7414 | } |
| 7415 | |
Chris Mason | 9ebefb18 | 2007-06-15 13:50:00 -0400 | [diff] [blame] | 7416 | int btrfs_readpage(struct file *file, struct page *page) |
| 7417 | { |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 7418 | struct extent_io_tree *tree; |
| 7419 | tree = &BTRFS_I(page->mapping->host)->io_tree; |
Jan Schmidt | 8ddc7d9 | 2011-06-13 20:02:58 +0200 | [diff] [blame] | 7420 | return extent_read_full_page(tree, page, btrfs_get_extent, 0); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 7421 | } |
Chris Mason | 1832a6d | 2007-12-21 16:27:21 -0500 | [diff] [blame] | 7422 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 7423 | static int btrfs_writepage(struct page *page, struct writeback_control *wbc) |
| 7424 | { |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 7425 | struct extent_io_tree *tree; |
Chris Mason | b888db2 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 7426 | |
| 7427 | |
| 7428 | if (current->flags & PF_MEMALLOC) { |
| 7429 | redirty_page_for_writepage(wbc, page); |
| 7430 | unlock_page(page); |
| 7431 | return 0; |
| 7432 | } |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 7433 | tree = &BTRFS_I(page->mapping->host)->io_tree; |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 7434 | return extent_write_full_page(tree, page, btrfs_get_extent, wbc); |
| 7435 | } |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 7436 | |
Eric Sandeen | 48a3b63 | 2013-04-25 20:41:01 +0000 | [diff] [blame] | 7437 | static int btrfs_writepages(struct address_space *mapping, |
| 7438 | struct writeback_control *wbc) |
Chris Mason | b293f02e | 2007-11-01 19:45:34 -0400 | [diff] [blame] | 7439 | { |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 7440 | struct extent_io_tree *tree; |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 7441 | |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 7442 | tree = &BTRFS_I(mapping->host)->io_tree; |
Chris Mason | b293f02e | 2007-11-01 19:45:34 -0400 | [diff] [blame] | 7443 | return extent_writepages(tree, mapping, btrfs_get_extent, wbc); |
| 7444 | } |
| 7445 | |
Chris Mason | 3ab2fb5 | 2007-11-08 10:59:22 -0500 | [diff] [blame] | 7446 | static int |
| 7447 | btrfs_readpages(struct file *file, struct address_space *mapping, |
| 7448 | struct list_head *pages, unsigned nr_pages) |
| 7449 | { |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 7450 | struct extent_io_tree *tree; |
| 7451 | tree = &BTRFS_I(mapping->host)->io_tree; |
Chris Mason | 3ab2fb5 | 2007-11-08 10:59:22 -0500 | [diff] [blame] | 7452 | return extent_readpages(tree, mapping, pages, nr_pages, |
| 7453 | btrfs_get_extent); |
| 7454 | } |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 7455 | static int __btrfs_releasepage(struct page *page, gfp_t gfp_flags) |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 7456 | { |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 7457 | struct extent_io_tree *tree; |
| 7458 | struct extent_map_tree *map; |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 7459 | int ret; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 7460 | |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 7461 | tree = &BTRFS_I(page->mapping->host)->io_tree; |
| 7462 | map = &BTRFS_I(page->mapping->host)->extent_tree; |
Chris Mason | 70dec80 | 2008-01-29 09:59:12 -0500 | [diff] [blame] | 7463 | ret = try_release_extent_mapping(map, tree, page, gfp_flags); |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 7464 | if (ret == 1) { |
| 7465 | ClearPagePrivate(page); |
| 7466 | set_page_private(page, 0); |
| 7467 | page_cache_release(page); |
| 7468 | } |
| 7469 | return ret; |
| 7470 | } |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 7471 | |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 7472 | static int btrfs_releasepage(struct page *page, gfp_t gfp_flags) |
| 7473 | { |
Chris Mason | 98509cf | 2008-09-11 15:51:43 -0400 | [diff] [blame] | 7474 | if (PageWriteback(page) || PageDirty(page)) |
| 7475 | return 0; |
Yan Zheng | b335b00 | 2009-02-12 10:06:04 -0500 | [diff] [blame] | 7476 | return __btrfs_releasepage(page, gfp_flags & GFP_NOFS); |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 7477 | } |
| 7478 | |
Lukas Czerner | d47992f | 2013-05-21 23:17:23 -0400 | [diff] [blame] | 7479 | static void btrfs_invalidatepage(struct page *page, unsigned int offset, |
| 7480 | unsigned int length) |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 7481 | { |
Josef Bacik | 5fd0204 | 2012-05-02 14:00:54 -0400 | [diff] [blame] | 7482 | struct inode *inode = page->mapping->host; |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 7483 | struct extent_io_tree *tree; |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 7484 | struct btrfs_ordered_extent *ordered; |
Josef Bacik | 2ac55d4 | 2010-02-03 19:33:23 +0000 | [diff] [blame] | 7485 | struct extent_state *cached_state = NULL; |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 7486 | u64 page_start = page_offset(page); |
| 7487 | u64 page_end = page_start + PAGE_CACHE_SIZE - 1; |
Filipe David Borba Manana | 131e404 | 2013-11-19 22:29:35 +0000 | [diff] [blame] | 7488 | int inode_evicting = inode->i_state & I_FREEING; |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 7489 | |
Chris Mason | 8b62b72 | 2009-09-02 16:53:46 -0400 | [diff] [blame] | 7490 | /* |
| 7491 | * we have the page locked, so new writeback can't start, |
| 7492 | * and the dirty bit won't be cleared while we are here. |
| 7493 | * |
| 7494 | * Wait for IO on this page so that we can safely clear |
| 7495 | * the PagePrivate2 bit and do ordered accounting |
| 7496 | */ |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 7497 | wait_on_page_writeback(page); |
Chris Mason | 8b62b72 | 2009-09-02 16:53:46 -0400 | [diff] [blame] | 7498 | |
Josef Bacik | 5fd0204 | 2012-05-02 14:00:54 -0400 | [diff] [blame] | 7499 | tree = &BTRFS_I(inode)->io_tree; |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 7500 | if (offset) { |
| 7501 | btrfs_releasepage(page, GFP_NOFS); |
| 7502 | return; |
| 7503 | } |
Filipe David Borba Manana | 131e404 | 2013-11-19 22:29:35 +0000 | [diff] [blame] | 7504 | |
| 7505 | if (!inode_evicting) |
| 7506 | lock_extent_bits(tree, page_start, page_end, 0, &cached_state); |
| 7507 | ordered = btrfs_lookup_ordered_extent(inode, page_start); |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 7508 | if (ordered) { |
Chris Mason | eb84ae0 | 2008-07-17 13:53:27 -0400 | [diff] [blame] | 7509 | /* |
| 7510 | * IO on this page will never be started, so we need |
| 7511 | * to account for any ordered extents now |
| 7512 | */ |
Filipe David Borba Manana | 131e404 | 2013-11-19 22:29:35 +0000 | [diff] [blame] | 7513 | if (!inode_evicting) |
| 7514 | clear_extent_bit(tree, page_start, page_end, |
| 7515 | EXTENT_DIRTY | EXTENT_DELALLOC | |
| 7516 | EXTENT_LOCKED | EXTENT_DO_ACCOUNTING | |
| 7517 | EXTENT_DEFRAG, 1, 0, &cached_state, |
| 7518 | GFP_NOFS); |
Chris Mason | 8b62b72 | 2009-09-02 16:53:46 -0400 | [diff] [blame] | 7519 | /* |
| 7520 | * whoever cleared the private bit is responsible |
| 7521 | * for the finish_ordered_io |
| 7522 | */ |
Josef Bacik | 77cef2e | 2013-08-29 13:57:21 -0400 | [diff] [blame] | 7523 | if (TestClearPagePrivate2(page)) { |
| 7524 | struct btrfs_ordered_inode_tree *tree; |
| 7525 | u64 new_len; |
| 7526 | |
| 7527 | tree = &BTRFS_I(inode)->ordered_tree; |
| 7528 | |
| 7529 | spin_lock_irq(&tree->lock); |
| 7530 | set_bit(BTRFS_ORDERED_TRUNCATED, &ordered->flags); |
| 7531 | new_len = page_start - ordered->file_offset; |
| 7532 | if (new_len < ordered->truncated_len) |
| 7533 | ordered->truncated_len = new_len; |
| 7534 | spin_unlock_irq(&tree->lock); |
| 7535 | |
| 7536 | if (btrfs_dec_test_ordered_pending(inode, &ordered, |
| 7537 | page_start, |
| 7538 | PAGE_CACHE_SIZE, 1)) |
| 7539 | btrfs_finish_ordered_io(ordered); |
Chris Mason | 8b62b72 | 2009-09-02 16:53:46 -0400 | [diff] [blame] | 7540 | } |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 7541 | btrfs_put_ordered_extent(ordered); |
Filipe David Borba Manana | 131e404 | 2013-11-19 22:29:35 +0000 | [diff] [blame] | 7542 | if (!inode_evicting) { |
| 7543 | cached_state = NULL; |
| 7544 | lock_extent_bits(tree, page_start, page_end, 0, |
| 7545 | &cached_state); |
| 7546 | } |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 7547 | } |
Filipe David Borba Manana | 131e404 | 2013-11-19 22:29:35 +0000 | [diff] [blame] | 7548 | |
| 7549 | if (!inode_evicting) { |
| 7550 | clear_extent_bit(tree, page_start, page_end, |
| 7551 | EXTENT_LOCKED | EXTENT_DIRTY | |
| 7552 | EXTENT_DELALLOC | EXTENT_DO_ACCOUNTING | |
| 7553 | EXTENT_DEFRAG, 1, 1, |
| 7554 | &cached_state, GFP_NOFS); |
| 7555 | |
| 7556 | __btrfs_releasepage(page, GFP_NOFS); |
| 7557 | } |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 7558 | |
Chris Mason | 4a09675 | 2008-07-21 10:29:44 -0400 | [diff] [blame] | 7559 | ClearPageChecked(page); |
Chris Mason | 9ad6b7bc | 2008-04-18 16:11:30 -0400 | [diff] [blame] | 7560 | if (PagePrivate(page)) { |
Chris Mason | 9ad6b7bc | 2008-04-18 16:11:30 -0400 | [diff] [blame] | 7561 | ClearPagePrivate(page); |
| 7562 | set_page_private(page, 0); |
| 7563 | page_cache_release(page); |
| 7564 | } |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 7565 | } |
| 7566 | |
Chris Mason | 9ebefb18 | 2007-06-15 13:50:00 -0400 | [diff] [blame] | 7567 | /* |
| 7568 | * btrfs_page_mkwrite() is not allowed to change the file size as it gets |
| 7569 | * called from a page fault handler when a page is first dirtied. Hence we must |
| 7570 | * be careful to check for EOF conditions here. We set the page up correctly |
| 7571 | * for a written page which means we get ENOSPC checking when writing into |
| 7572 | * holes and correct delalloc and unwritten extent mapping on filesystems that |
| 7573 | * support these features. |
| 7574 | * |
| 7575 | * We are not allowed to take the i_mutex here so we have to play games to |
| 7576 | * protect against truncate races as the page could now be beyond EOF. Because |
| 7577 | * vmtruncate() writes the inode size before removing pages, once we have the |
| 7578 | * page lock we can determine safely if the page is beyond EOF. If it is not |
| 7579 | * beyond EOF, then the page is guaranteed safe against truncation until we |
| 7580 | * unlock the page. |
| 7581 | */ |
Nick Piggin | c2ec175 | 2009-03-31 15:23:21 -0700 | [diff] [blame] | 7582 | 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] | 7583 | { |
Nick Piggin | c2ec175 | 2009-03-31 15:23:21 -0700 | [diff] [blame] | 7584 | struct page *page = vmf->page; |
Al Viro | 496ad9a | 2013-01-23 17:07:38 -0500 | [diff] [blame] | 7585 | struct inode *inode = file_inode(vma->vm_file); |
Chris Mason | 1832a6d | 2007-12-21 16:27:21 -0500 | [diff] [blame] | 7586 | struct btrfs_root *root = BTRFS_I(inode)->root; |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 7587 | struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree; |
| 7588 | struct btrfs_ordered_extent *ordered; |
Josef Bacik | 2ac55d4 | 2010-02-03 19:33:23 +0000 | [diff] [blame] | 7589 | struct extent_state *cached_state = NULL; |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 7590 | char *kaddr; |
| 7591 | unsigned long zero_start; |
Chris Mason | 9ebefb18 | 2007-06-15 13:50:00 -0400 | [diff] [blame] | 7592 | loff_t size; |
Chris Mason | 1832a6d | 2007-12-21 16:27:21 -0500 | [diff] [blame] | 7593 | int ret; |
Chris Mason | 9998eb7 | 2012-01-25 13:47:40 -0500 | [diff] [blame] | 7594 | int reserved = 0; |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 7595 | u64 page_start; |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 7596 | u64 page_end; |
Chris Mason | 9ebefb18 | 2007-06-15 13:50:00 -0400 | [diff] [blame] | 7597 | |
Jan Kara | b2b5ef5 | 2012-06-12 16:20:45 +0200 | [diff] [blame] | 7598 | sb_start_pagefault(inode->i_sb); |
Yan, Zheng | 0ca1f7c | 2010-05-16 10:48:47 -0400 | [diff] [blame] | 7599 | ret = btrfs_delalloc_reserve_space(inode, PAGE_CACHE_SIZE); |
Chris Mason | 9998eb7 | 2012-01-25 13:47:40 -0500 | [diff] [blame] | 7600 | if (!ret) { |
Josef Bacik | e41f941 | 2012-03-26 09:46:47 -0400 | [diff] [blame] | 7601 | ret = file_update_time(vma->vm_file); |
Chris Mason | 9998eb7 | 2012-01-25 13:47:40 -0500 | [diff] [blame] | 7602 | reserved = 1; |
| 7603 | } |
Nick Piggin | 56a76f8 | 2009-03-31 15:23:23 -0700 | [diff] [blame] | 7604 | if (ret) { |
| 7605 | if (ret == -ENOMEM) |
| 7606 | ret = VM_FAULT_OOM; |
| 7607 | else /* -ENOSPC, -EIO, etc */ |
| 7608 | ret = VM_FAULT_SIGBUS; |
Chris Mason | 9998eb7 | 2012-01-25 13:47:40 -0500 | [diff] [blame] | 7609 | if (reserved) |
| 7610 | goto out; |
| 7611 | goto out_noreserve; |
Nick Piggin | 56a76f8 | 2009-03-31 15:23:23 -0700 | [diff] [blame] | 7612 | } |
Chris Mason | 1832a6d | 2007-12-21 16:27:21 -0500 | [diff] [blame] | 7613 | |
Nick Piggin | 56a76f8 | 2009-03-31 15:23:23 -0700 | [diff] [blame] | 7614 | ret = VM_FAULT_NOPAGE; /* make the VM retry the fault */ |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 7615 | again: |
Chris Mason | 9ebefb18 | 2007-06-15 13:50:00 -0400 | [diff] [blame] | 7616 | lock_page(page); |
Chris Mason | 9ebefb18 | 2007-06-15 13:50:00 -0400 | [diff] [blame] | 7617 | size = i_size_read(inode); |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 7618 | page_start = page_offset(page); |
| 7619 | page_end = page_start + PAGE_CACHE_SIZE - 1; |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 7620 | |
Chris Mason | 9ebefb18 | 2007-06-15 13:50:00 -0400 | [diff] [blame] | 7621 | if ((page->mapping != inode->i_mapping) || |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 7622 | (page_start >= size)) { |
Chris Mason | 9ebefb18 | 2007-06-15 13:50:00 -0400 | [diff] [blame] | 7623 | /* page got truncated out from underneath us */ |
| 7624 | goto out_unlock; |
| 7625 | } |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 7626 | wait_on_page_writeback(page); |
| 7627 | |
Jeff Mahoney | d008237 | 2012-03-01 14:57:19 +0100 | [diff] [blame] | 7628 | lock_extent_bits(io_tree, page_start, page_end, 0, &cached_state); |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 7629 | set_page_extent_mapped(page); |
| 7630 | |
Chris Mason | eb84ae0 | 2008-07-17 13:53:27 -0400 | [diff] [blame] | 7631 | /* |
| 7632 | * we can't set the delalloc bits if there are pending ordered |
| 7633 | * extents. Drop our locks and wait for them to finish |
| 7634 | */ |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 7635 | ordered = btrfs_lookup_ordered_extent(inode, page_start); |
| 7636 | if (ordered) { |
Josef Bacik | 2ac55d4 | 2010-02-03 19:33:23 +0000 | [diff] [blame] | 7637 | unlock_extent_cached(io_tree, page_start, page_end, |
| 7638 | &cached_state, GFP_NOFS); |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 7639 | unlock_page(page); |
Chris Mason | eb84ae0 | 2008-07-17 13:53:27 -0400 | [diff] [blame] | 7640 | btrfs_start_ordered_extent(inode, ordered, 1); |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 7641 | btrfs_put_ordered_extent(ordered); |
| 7642 | goto again; |
| 7643 | } |
| 7644 | |
Josef Bacik | fbf1908 | 2009-10-01 17:10:23 -0400 | [diff] [blame] | 7645 | /* |
| 7646 | * XXX - page_mkwrite gets called every time the page is dirtied, even |
| 7647 | * if it was already dirty, so for space accounting reasons we need to |
| 7648 | * clear any delalloc bits for the range we are fixing to save. There |
| 7649 | * is probably a better way to do this, but for now keep consistent with |
| 7650 | * prepare_pages in the normal write path. |
| 7651 | */ |
Josef Bacik | 2ac55d4 | 2010-02-03 19:33:23 +0000 | [diff] [blame] | 7652 | clear_extent_bit(&BTRFS_I(inode)->io_tree, page_start, page_end, |
Liu Bo | 9e8a4a8 | 2012-09-05 19:10:51 -0600 | [diff] [blame] | 7653 | EXTENT_DIRTY | EXTENT_DELALLOC | |
| 7654 | EXTENT_DO_ACCOUNTING | EXTENT_DEFRAG, |
Josef Bacik | 2ac55d4 | 2010-02-03 19:33:23 +0000 | [diff] [blame] | 7655 | 0, 0, &cached_state, GFP_NOFS); |
Josef Bacik | fbf1908 | 2009-10-01 17:10:23 -0400 | [diff] [blame] | 7656 | |
Josef Bacik | 2ac55d4 | 2010-02-03 19:33:23 +0000 | [diff] [blame] | 7657 | ret = btrfs_set_extent_delalloc(inode, page_start, page_end, |
| 7658 | &cached_state); |
Josef Bacik | 9ed74f2 | 2009-09-11 16:12:44 -0400 | [diff] [blame] | 7659 | if (ret) { |
Josef Bacik | 2ac55d4 | 2010-02-03 19:33:23 +0000 | [diff] [blame] | 7660 | unlock_extent_cached(io_tree, page_start, page_end, |
| 7661 | &cached_state, GFP_NOFS); |
Josef Bacik | 9ed74f2 | 2009-09-11 16:12:44 -0400 | [diff] [blame] | 7662 | ret = VM_FAULT_SIGBUS; |
| 7663 | goto out_unlock; |
| 7664 | } |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 7665 | ret = 0; |
Chris Mason | 9ebefb18 | 2007-06-15 13:50:00 -0400 | [diff] [blame] | 7666 | |
| 7667 | /* page is wholly or partially inside EOF */ |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 7668 | if (page_start + PAGE_CACHE_SIZE > size) |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 7669 | zero_start = size & ~PAGE_CACHE_MASK; |
Chris Mason | 9ebefb18 | 2007-06-15 13:50:00 -0400 | [diff] [blame] | 7670 | else |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 7671 | zero_start = PAGE_CACHE_SIZE; |
Chris Mason | 9ebefb18 | 2007-06-15 13:50:00 -0400 | [diff] [blame] | 7672 | |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 7673 | if (zero_start != PAGE_CACHE_SIZE) { |
| 7674 | kaddr = kmap(page); |
| 7675 | memset(kaddr + zero_start, 0, PAGE_CACHE_SIZE - zero_start); |
| 7676 | flush_dcache_page(page); |
| 7677 | kunmap(page); |
| 7678 | } |
Chris Mason | 247e743 | 2008-07-17 12:53:51 -0400 | [diff] [blame] | 7679 | ClearPageChecked(page); |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 7680 | set_page_dirty(page); |
Chris Mason | 50a9b21 | 2009-09-11 12:33:12 -0400 | [diff] [blame] | 7681 | SetPageUptodate(page); |
Chris Mason | 5a3f23d | 2009-03-31 13:27:11 -0400 | [diff] [blame] | 7682 | |
Chris Mason | 257c62e | 2009-10-13 13:21:08 -0400 | [diff] [blame] | 7683 | BTRFS_I(inode)->last_trans = root->fs_info->generation; |
| 7684 | BTRFS_I(inode)->last_sub_trans = BTRFS_I(inode)->root->log_transid; |
Liu Bo | 46d8bc3 | 2012-08-29 01:07:55 -0600 | [diff] [blame] | 7685 | 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] | 7686 | |
Josef Bacik | 2ac55d4 | 2010-02-03 19:33:23 +0000 | [diff] [blame] | 7687 | 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] | 7688 | |
| 7689 | out_unlock: |
Jan Kara | b2b5ef5 | 2012-06-12 16:20:45 +0200 | [diff] [blame] | 7690 | if (!ret) { |
| 7691 | sb_end_pagefault(inode->i_sb); |
Chris Mason | 50a9b21 | 2009-09-11 12:33:12 -0400 | [diff] [blame] | 7692 | return VM_FAULT_LOCKED; |
Jan Kara | b2b5ef5 | 2012-06-12 16:20:45 +0200 | [diff] [blame] | 7693 | } |
Chris Mason | 9ebefb18 | 2007-06-15 13:50:00 -0400 | [diff] [blame] | 7694 | unlock_page(page); |
Chris Mason | 1832a6d | 2007-12-21 16:27:21 -0500 | [diff] [blame] | 7695 | out: |
Josef Bacik | ec39e18 | 2012-01-12 19:10:12 -0500 | [diff] [blame] | 7696 | btrfs_delalloc_release_space(inode, PAGE_CACHE_SIZE); |
Chris Mason | 9998eb7 | 2012-01-25 13:47:40 -0500 | [diff] [blame] | 7697 | out_noreserve: |
Jan Kara | b2b5ef5 | 2012-06-12 16:20:45 +0200 | [diff] [blame] | 7698 | sb_end_pagefault(inode->i_sb); |
Chris Mason | 9ebefb18 | 2007-06-15 13:50:00 -0400 | [diff] [blame] | 7699 | return ret; |
| 7700 | } |
| 7701 | |
Josef Bacik | a41ad39 | 2011-01-31 15:30:16 -0500 | [diff] [blame] | 7702 | static int btrfs_truncate(struct inode *inode) |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 7703 | { |
| 7704 | struct btrfs_root *root = BTRFS_I(inode)->root; |
Josef Bacik | fcb80c2 | 2011-05-03 10:40:22 -0400 | [diff] [blame] | 7705 | struct btrfs_block_rsv *rsv; |
Josef Bacik | a71754f | 2013-06-17 17:14:39 -0400 | [diff] [blame] | 7706 | int ret = 0; |
Josef Bacik | 3893e33 | 2011-01-31 16:03:11 -0500 | [diff] [blame] | 7707 | int err = 0; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 7708 | struct btrfs_trans_handle *trans; |
Chris Mason | dbe674a | 2008-07-17 12:54:05 -0400 | [diff] [blame] | 7709 | u64 mask = root->sectorsize - 1; |
Josef Bacik | 0712718 | 2011-08-19 10:29:59 -0400 | [diff] [blame] | 7710 | u64 min_size = btrfs_calc_trunc_metadata_size(root, 1); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 7711 | |
Josef Bacik | 0ef8b72 | 2013-10-25 16:13:35 -0400 | [diff] [blame] | 7712 | ret = btrfs_wait_ordered_range(inode, inode->i_size & (~mask), |
| 7713 | (u64)-1); |
| 7714 | if (ret) |
| 7715 | return ret; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 7716 | |
Josef Bacik | fcb80c2 | 2011-05-03 10:40:22 -0400 | [diff] [blame] | 7717 | /* |
| 7718 | * Yes ladies and gentelment, this is indeed ugly. The fact is we have |
| 7719 | * 3 things going on here |
| 7720 | * |
| 7721 | * 1) We need to reserve space for our orphan item and the space to |
| 7722 | * delete our orphan item. Lord knows we don't want to have a dangling |
| 7723 | * orphan item because we didn't reserve space to remove it. |
| 7724 | * |
| 7725 | * 2) We need to reserve space to update our inode. |
| 7726 | * |
| 7727 | * 3) We need to have something to cache all the space that is going to |
| 7728 | * be free'd up by the truncate operation, but also have some slack |
| 7729 | * space reserved in case it uses space during the truncate (thank you |
| 7730 | * very much snapshotting). |
| 7731 | * |
| 7732 | * And we need these to all be seperate. The fact is we can use alot of |
| 7733 | * space doing the truncate, and we have no earthly idea how much space |
| 7734 | * we will use, so we need the truncate reservation to be seperate so it |
| 7735 | * doesn't end up using space reserved for updating the inode or |
| 7736 | * removing the orphan item. We also need to be able to stop the |
| 7737 | * transaction and start a new one, which means we need to be able to |
| 7738 | * update the inode several times, and we have no idea of knowing how |
| 7739 | * many times that will be, so we can't just reserve 1 item for the |
| 7740 | * entirety of the opration, so that has to be done seperately as well. |
| 7741 | * Then there is the orphan item, which does indeed need to be held on |
| 7742 | * to for the whole operation, and we need nobody to touch this reserved |
| 7743 | * space except the orphan code. |
| 7744 | * |
| 7745 | * So that leaves us with |
| 7746 | * |
| 7747 | * 1) root->orphan_block_rsv - for the orphan deletion. |
| 7748 | * 2) rsv - for the truncate reservation, which we will steal from the |
| 7749 | * transaction reservation. |
| 7750 | * 3) fs_info->trans_block_rsv - this will have 1 items worth left for |
| 7751 | * updating the inode. |
| 7752 | */ |
Miao Xie | 66d8f3d | 2012-09-06 04:02:28 -0600 | [diff] [blame] | 7753 | rsv = btrfs_alloc_block_rsv(root, BTRFS_BLOCK_RSV_TEMP); |
Josef Bacik | fcb80c2 | 2011-05-03 10:40:22 -0400 | [diff] [blame] | 7754 | if (!rsv) |
| 7755 | return -ENOMEM; |
Josef Bacik | 4a33854 | 2011-08-29 11:01:31 -0400 | [diff] [blame] | 7756 | rsv->size = min_size; |
Josef Bacik | ca7e70f | 2012-08-27 17:48:15 -0400 | [diff] [blame] | 7757 | rsv->failfast = 1; |
Josef Bacik | f0cd846 | 2011-03-04 14:37:08 -0500 | [diff] [blame] | 7758 | |
Josef Bacik | 907cbce | 2011-08-08 13:46:15 -0400 | [diff] [blame] | 7759 | /* |
Josef Bacik | 0712718 | 2011-08-19 10:29:59 -0400 | [diff] [blame] | 7760 | * 1 for the truncate slack space |
Josef Bacik | 907cbce | 2011-08-08 13:46:15 -0400 | [diff] [blame] | 7761 | * 1 for updating the inode. |
| 7762 | */ |
Josef Bacik | f3fe820 | 2013-01-07 17:03:21 -0500 | [diff] [blame] | 7763 | trans = btrfs_start_transaction(root, 2); |
Josef Bacik | fcb80c2 | 2011-05-03 10:40:22 -0400 | [diff] [blame] | 7764 | if (IS_ERR(trans)) { |
| 7765 | err = PTR_ERR(trans); |
| 7766 | goto out; |
| 7767 | } |
Josef Bacik | f0cd846 | 2011-03-04 14:37:08 -0500 | [diff] [blame] | 7768 | |
Josef Bacik | 907cbce | 2011-08-08 13:46:15 -0400 | [diff] [blame] | 7769 | /* Migrate the slack space for the truncate to our reserve */ |
| 7770 | ret = btrfs_block_rsv_migrate(&root->fs_info->trans_block_rsv, rsv, |
| 7771 | min_size); |
Josef Bacik | fcb80c2 | 2011-05-03 10:40:22 -0400 | [diff] [blame] | 7772 | BUG_ON(ret); |
Josef Bacik | f0cd846 | 2011-03-04 14:37:08 -0500 | [diff] [blame] | 7773 | |
Chris Mason | 5a3f23d | 2009-03-31 13:27:11 -0400 | [diff] [blame] | 7774 | /* |
| 7775 | * setattr is responsible for setting the ordered_data_close flag, |
| 7776 | * but that is only tested during the last file release. That |
| 7777 | * could happen well after the next commit, leaving a great big |
| 7778 | * window where new writes may get lost if someone chooses to write |
| 7779 | * to this file after truncating to zero |
| 7780 | * |
| 7781 | * The inode doesn't have any dirty data here, and so if we commit |
| 7782 | * this is a noop. If someone immediately starts writing to the inode |
| 7783 | * it is very likely we'll catch some of their writes in this |
| 7784 | * transaction, and the commit will find this file on the ordered |
| 7785 | * data list with good things to send down. |
| 7786 | * |
| 7787 | * This is a best effort solution, there is still a window where |
| 7788 | * using truncate to replace the contents of the file will |
| 7789 | * end up with a zero length file after a crash. |
| 7790 | */ |
Josef Bacik | 72ac3c0 | 2012-05-23 14:13:11 -0400 | [diff] [blame] | 7791 | if (inode->i_size == 0 && test_bit(BTRFS_INODE_ORDERED_DATA_CLOSE, |
| 7792 | &BTRFS_I(inode)->runtime_flags)) |
Chris Mason | 5a3f23d | 2009-03-31 13:27:11 -0400 | [diff] [blame] | 7793 | btrfs_add_ordered_operation(trans, root, inode); |
| 7794 | |
Josef Bacik | 5dc562c | 2012-08-17 13:14:17 -0400 | [diff] [blame] | 7795 | /* |
| 7796 | * So if we truncate and then write and fsync we normally would just |
| 7797 | * write the extents that changed, which is a problem if we need to |
| 7798 | * first truncate that entire inode. So set this flag so we write out |
| 7799 | * all of the extents in the inode to the sync log so we're completely |
| 7800 | * safe. |
| 7801 | */ |
| 7802 | set_bit(BTRFS_INODE_NEEDS_FULL_SYNC, &BTRFS_I(inode)->runtime_flags); |
Josef Bacik | ca7e70f | 2012-08-27 17:48:15 -0400 | [diff] [blame] | 7803 | trans->block_rsv = rsv; |
Josef Bacik | 5dc562c | 2012-08-17 13:14:17 -0400 | [diff] [blame] | 7804 | |
Yan, Zheng | 8082510 | 2009-11-12 09:35:36 +0000 | [diff] [blame] | 7805 | while (1) { |
| 7806 | ret = btrfs_truncate_inode_items(trans, root, inode, |
| 7807 | inode->i_size, |
| 7808 | BTRFS_EXTENT_DATA_KEY); |
Josef Bacik | ca7e70f | 2012-08-27 17:48:15 -0400 | [diff] [blame] | 7809 | if (ret != -ENOSPC) { |
Josef Bacik | 3893e33 | 2011-01-31 16:03:11 -0500 | [diff] [blame] | 7810 | err = ret; |
Yan, Zheng | 8082510 | 2009-11-12 09:35:36 +0000 | [diff] [blame] | 7811 | break; |
Josef Bacik | 3893e33 | 2011-01-31 16:03:11 -0500 | [diff] [blame] | 7812 | } |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 7813 | |
Josef Bacik | fcb80c2 | 2011-05-03 10:40:22 -0400 | [diff] [blame] | 7814 | trans->block_rsv = &root->fs_info->trans_block_rsv; |
Yan, Zheng | 8082510 | 2009-11-12 09:35:36 +0000 | [diff] [blame] | 7815 | ret = btrfs_update_inode(trans, root, inode); |
Josef Bacik | 3893e33 | 2011-01-31 16:03:11 -0500 | [diff] [blame] | 7816 | if (ret) { |
| 7817 | err = ret; |
| 7818 | break; |
| 7819 | } |
Josef Bacik | ca7e70f | 2012-08-27 17:48:15 -0400 | [diff] [blame] | 7820 | |
Yan, Zheng | 8082510 | 2009-11-12 09:35:36 +0000 | [diff] [blame] | 7821 | btrfs_end_transaction(trans, root); |
Liu Bo | b53d3f5 | 2012-11-14 14:34:34 +0000 | [diff] [blame] | 7822 | btrfs_btree_balance_dirty(root); |
Josef Bacik | ca7e70f | 2012-08-27 17:48:15 -0400 | [diff] [blame] | 7823 | |
| 7824 | trans = btrfs_start_transaction(root, 2); |
| 7825 | if (IS_ERR(trans)) { |
| 7826 | ret = err = PTR_ERR(trans); |
| 7827 | trans = NULL; |
| 7828 | break; |
| 7829 | } |
| 7830 | |
| 7831 | ret = btrfs_block_rsv_migrate(&root->fs_info->trans_block_rsv, |
| 7832 | rsv, min_size); |
| 7833 | BUG_ON(ret); /* shouldn't happen */ |
| 7834 | trans->block_rsv = rsv; |
Yan, Zheng | 8082510 | 2009-11-12 09:35:36 +0000 | [diff] [blame] | 7835 | } |
| 7836 | |
| 7837 | if (ret == 0 && inode->i_nlink > 0) { |
Josef Bacik | fcb80c2 | 2011-05-03 10:40:22 -0400 | [diff] [blame] | 7838 | trans->block_rsv = root->orphan_block_rsv; |
Yan, Zheng | 8082510 | 2009-11-12 09:35:36 +0000 | [diff] [blame] | 7839 | ret = btrfs_orphan_del(trans, inode); |
Josef Bacik | 3893e33 | 2011-01-31 16:03:11 -0500 | [diff] [blame] | 7840 | if (ret) |
| 7841 | err = ret; |
Yan, Zheng | 8082510 | 2009-11-12 09:35:36 +0000 | [diff] [blame] | 7842 | } |
| 7843 | |
Chris Mason | 917c16b | 2011-11-08 14:49:59 -0500 | [diff] [blame] | 7844 | if (trans) { |
| 7845 | trans->block_rsv = &root->fs_info->trans_block_rsv; |
| 7846 | ret = btrfs_update_inode(trans, root, inode); |
| 7847 | if (ret && !err) |
| 7848 | err = ret; |
Josef Bacik | 7b12876 | 2008-07-24 12:17:14 -0400 | [diff] [blame] | 7849 | |
Josef Bacik | 7ad85bb | 2012-01-12 19:10:12 -0500 | [diff] [blame] | 7850 | ret = btrfs_end_transaction(trans, root); |
Liu Bo | b53d3f5 | 2012-11-14 14:34:34 +0000 | [diff] [blame] | 7851 | btrfs_btree_balance_dirty(root); |
Chris Mason | 917c16b | 2011-11-08 14:49:59 -0500 | [diff] [blame] | 7852 | } |
Josef Bacik | fcb80c2 | 2011-05-03 10:40:22 -0400 | [diff] [blame] | 7853 | |
| 7854 | out: |
| 7855 | btrfs_free_block_rsv(root, rsv); |
| 7856 | |
Josef Bacik | 3893e33 | 2011-01-31 16:03:11 -0500 | [diff] [blame] | 7857 | if (ret && !err) |
| 7858 | err = ret; |
Josef Bacik | a41ad39 | 2011-01-31 15:30:16 -0500 | [diff] [blame] | 7859 | |
Josef Bacik | 3893e33 | 2011-01-31 16:03:11 -0500 | [diff] [blame] | 7860 | return err; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 7861 | } |
| 7862 | |
Sven Wegener | 3b96362 | 2008-06-09 21:57:42 -0400 | [diff] [blame] | 7863 | /* |
Chris Mason | d352ac6 | 2008-09-29 15:18:18 -0400 | [diff] [blame] | 7864 | * create a new subvolume directory/inode (helper for the ioctl). |
| 7865 | */ |
Yan Zheng | d2fb343 | 2008-12-11 16:30:39 -0500 | [diff] [blame] | 7866 | int btrfs_create_subvol_root(struct btrfs_trans_handle *trans, |
Josef Bacik | d82a6f1d | 2011-05-11 15:26:06 -0400 | [diff] [blame] | 7867 | struct btrfs_root *new_root, u64 new_dirid) |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 7868 | { |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 7869 | struct inode *inode; |
Yan, Zheng | 76dda93 | 2009-09-21 16:00:26 -0400 | [diff] [blame] | 7870 | int err; |
Chris Mason | 00e4e6b | 2008-08-05 11:18:09 -0400 | [diff] [blame] | 7871 | u64 index = 0; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 7872 | |
Florian Albrechtskirchinger | 12fc9d0 | 2012-02-10 22:15:54 +0100 | [diff] [blame] | 7873 | inode = btrfs_new_inode(trans, new_root, NULL, "..", 2, |
| 7874 | new_dirid, new_dirid, |
| 7875 | S_IFDIR | (~current_umask() & S_IRWXUGO), |
| 7876 | &index); |
Chris Mason | 54aa1f4 | 2007-06-22 14:16:25 -0400 | [diff] [blame] | 7877 | if (IS_ERR(inode)) |
Christoph Hellwig | f46b5a6 | 2008-06-11 21:53:53 -0400 | [diff] [blame] | 7878 | return PTR_ERR(inode); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 7879 | inode->i_op = &btrfs_dir_inode_operations; |
| 7880 | inode->i_fop = &btrfs_dir_file_operations; |
| 7881 | |
Miklos Szeredi | bfe8684 | 2011-10-28 14:13:29 +0200 | [diff] [blame] | 7882 | set_nlink(inode, 1); |
Chris Mason | dbe674a | 2008-07-17 12:54:05 -0400 | [diff] [blame] | 7883 | btrfs_i_size_write(inode, 0); |
Sven Wegener | 3b96362 | 2008-06-09 21:57:42 -0400 | [diff] [blame] | 7884 | |
Yan, Zheng | 76dda93 | 2009-09-21 16:00:26 -0400 | [diff] [blame] | 7885 | err = btrfs_update_inode(trans, new_root, inode); |
Christoph Hellwig | cb8e709 | 2008-10-09 13:39:39 -0400 | [diff] [blame] | 7886 | |
Yan, Zheng | 76dda93 | 2009-09-21 16:00:26 -0400 | [diff] [blame] | 7887 | iput(inode); |
Mark Fasheh | ce59897 | 2011-07-26 11:32:23 -0700 | [diff] [blame] | 7888 | return err; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 7889 | } |
| 7890 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 7891 | struct inode *btrfs_alloc_inode(struct super_block *sb) |
| 7892 | { |
| 7893 | struct btrfs_inode *ei; |
Yan, Zheng | 2ead6ae | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 7894 | struct inode *inode; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 7895 | |
| 7896 | ei = kmem_cache_alloc(btrfs_inode_cachep, GFP_NOFS); |
| 7897 | if (!ei) |
| 7898 | return NULL; |
Yan, Zheng | 2ead6ae | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 7899 | |
| 7900 | ei->root = NULL; |
Yan, Zheng | 2ead6ae | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 7901 | ei->generation = 0; |
Josef Bacik | 15ee9bc | 2007-08-10 16:22:09 -0400 | [diff] [blame] | 7902 | ei->last_trans = 0; |
Chris Mason | 257c62e | 2009-10-13 13:21:08 -0400 | [diff] [blame] | 7903 | ei->last_sub_trans = 0; |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 7904 | ei->logged_trans = 0; |
Yan, Zheng | 2ead6ae | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 7905 | ei->delalloc_bytes = 0; |
Yan, Zheng | 2ead6ae | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 7906 | ei->disk_i_size = 0; |
| 7907 | ei->flags = 0; |
Josef Bacik | 7709cde | 2011-08-04 10:25:02 -0400 | [diff] [blame] | 7908 | ei->csum_bytes = 0; |
Yan, Zheng | 2ead6ae | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 7909 | ei->index_cnt = (u64)-1; |
Miao Xie | 67de117 | 2013-12-26 13:07:06 +0800 | [diff] [blame^] | 7910 | ei->dir_index = 0; |
Yan, Zheng | 2ead6ae | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 7911 | ei->last_unlink_trans = 0; |
Liu Bo | 46d8bc3 | 2012-08-29 01:07:55 -0600 | [diff] [blame] | 7912 | ei->last_log_commit = 0; |
Yan, Zheng | 2ead6ae | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 7913 | |
Josef Bacik | 9e0baf6 | 2011-07-15 15:16:44 +0000 | [diff] [blame] | 7914 | spin_lock_init(&ei->lock); |
| 7915 | ei->outstanding_extents = 0; |
| 7916 | ei->reserved_extents = 0; |
Yan, Zheng | 2ead6ae | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 7917 | |
Josef Bacik | 72ac3c0 | 2012-05-23 14:13:11 -0400 | [diff] [blame] | 7918 | ei->runtime_flags = 0; |
Li Zefan | 261507a0 | 2010-12-17 14:21:50 +0800 | [diff] [blame] | 7919 | ei->force_compress = BTRFS_COMPRESS_NONE; |
Yan, Zheng | 2ead6ae | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 7920 | |
Miao Xie | 16cdcec | 2011-04-22 18:12:22 +0800 | [diff] [blame] | 7921 | ei->delayed_node = NULL; |
| 7922 | |
Yan, Zheng | 2ead6ae | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 7923 | inode = &ei->vfs_inode; |
David Sterba | a8067e0 | 2011-04-21 00:34:43 +0200 | [diff] [blame] | 7924 | extent_map_tree_init(&ei->extent_tree); |
David Sterba | f993c88 | 2011-04-20 23:35:57 +0200 | [diff] [blame] | 7925 | extent_io_tree_init(&ei->io_tree, &inode->i_data); |
| 7926 | extent_io_tree_init(&ei->io_failure_tree, &inode->i_data); |
Josef Bacik | 0b32f4b | 2012-03-13 09:38:00 -0400 | [diff] [blame] | 7927 | ei->io_tree.track_uptodate = 1; |
| 7928 | ei->io_failure_tree.track_uptodate = 1; |
Josef Bacik | b812ce2 | 2012-11-16 13:56:32 -0500 | [diff] [blame] | 7929 | atomic_set(&ei->sync_writers, 0); |
Yan, Zheng | 2ead6ae | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 7930 | mutex_init(&ei->log_mutex); |
Josef Bacik | f248679 | 2012-01-13 12:09:22 -0500 | [diff] [blame] | 7931 | mutex_init(&ei->delalloc_mutex); |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 7932 | btrfs_ordered_inode_tree_init(&ei->ordered_tree); |
Yan, Zheng | 2ead6ae | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 7933 | INIT_LIST_HEAD(&ei->delalloc_inodes); |
Chris Mason | 5a3f23d | 2009-03-31 13:27:11 -0400 | [diff] [blame] | 7934 | INIT_LIST_HEAD(&ei->ordered_operations); |
Yan, Zheng | 2ead6ae | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 7935 | RB_CLEAR_NODE(&ei->rb_node); |
| 7936 | |
| 7937 | return inode; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 7938 | } |
| 7939 | |
Josef Bacik | aaedb55 | 2013-10-11 14:44:09 -0400 | [diff] [blame] | 7940 | #ifdef CONFIG_BTRFS_FS_RUN_SANITY_TESTS |
| 7941 | void btrfs_test_destroy_inode(struct inode *inode) |
| 7942 | { |
| 7943 | btrfs_drop_extent_cache(inode, 0, (u64)-1, 0); |
| 7944 | kmem_cache_free(btrfs_inode_cachep, BTRFS_I(inode)); |
| 7945 | } |
| 7946 | #endif |
| 7947 | |
Nick Piggin | fa0d7e3d | 2011-01-07 17:49:49 +1100 | [diff] [blame] | 7948 | static void btrfs_i_callback(struct rcu_head *head) |
| 7949 | { |
| 7950 | struct inode *inode = container_of(head, struct inode, i_rcu); |
Nick Piggin | fa0d7e3d | 2011-01-07 17:49:49 +1100 | [diff] [blame] | 7951 | kmem_cache_free(btrfs_inode_cachep, BTRFS_I(inode)); |
| 7952 | } |
| 7953 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 7954 | void btrfs_destroy_inode(struct inode *inode) |
| 7955 | { |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 7956 | struct btrfs_ordered_extent *ordered; |
Chris Mason | 5a3f23d | 2009-03-31 13:27:11 -0400 | [diff] [blame] | 7957 | struct btrfs_root *root = BTRFS_I(inode)->root; |
| 7958 | |
Al Viro | b3d9b7a | 2012-06-09 13:51:19 -0400 | [diff] [blame] | 7959 | WARN_ON(!hlist_empty(&inode->i_dentry)); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 7960 | WARN_ON(inode->i_data.nrpages); |
Josef Bacik | 9e0baf6 | 2011-07-15 15:16:44 +0000 | [diff] [blame] | 7961 | WARN_ON(BTRFS_I(inode)->outstanding_extents); |
| 7962 | WARN_ON(BTRFS_I(inode)->reserved_extents); |
Josef Bacik | 7709cde | 2011-08-04 10:25:02 -0400 | [diff] [blame] | 7963 | WARN_ON(BTRFS_I(inode)->delalloc_bytes); |
| 7964 | WARN_ON(BTRFS_I(inode)->csum_bytes); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 7965 | |
Chris Mason | 5a3f23d | 2009-03-31 13:27:11 -0400 | [diff] [blame] | 7966 | /* |
Josef Bacik | a6dbd42 | 2009-11-11 15:53:34 -0500 | [diff] [blame] | 7967 | * This can happen where we create an inode, but somebody else also |
| 7968 | * created the same inode and we need to destroy the one we already |
| 7969 | * created. |
| 7970 | */ |
| 7971 | if (!root) |
| 7972 | goto free; |
| 7973 | |
| 7974 | /* |
Chris Mason | 5a3f23d | 2009-03-31 13:27:11 -0400 | [diff] [blame] | 7975 | * Make sure we're properly removed from the ordered operation |
| 7976 | * lists. |
| 7977 | */ |
| 7978 | smp_mb(); |
| 7979 | if (!list_empty(&BTRFS_I(inode)->ordered_operations)) { |
Miao Xie | 199c2a9 | 2013-05-15 07:48:23 +0000 | [diff] [blame] | 7980 | spin_lock(&root->fs_info->ordered_root_lock); |
Chris Mason | 5a3f23d | 2009-03-31 13:27:11 -0400 | [diff] [blame] | 7981 | list_del_init(&BTRFS_I(inode)->ordered_operations); |
Miao Xie | 199c2a9 | 2013-05-15 07:48:23 +0000 | [diff] [blame] | 7982 | spin_unlock(&root->fs_info->ordered_root_lock); |
Chris Mason | 5a3f23d | 2009-03-31 13:27:11 -0400 | [diff] [blame] | 7983 | } |
| 7984 | |
Josef Bacik | 8a35d95 | 2012-05-23 14:26:42 -0400 | [diff] [blame] | 7985 | if (test_bit(BTRFS_INODE_HAS_ORPHAN_ITEM, |
| 7986 | &BTRFS_I(inode)->runtime_flags)) { |
Simon Kirby | c2cf52e | 2013-03-19 22:41:23 +0000 | [diff] [blame] | 7987 | btrfs_info(root->fs_info, "inode %llu still on the orphan list", |
Geert Uytterhoeven | c1c9ff7 | 2013-08-20 13:20:07 +0200 | [diff] [blame] | 7988 | btrfs_ino(inode)); |
Josef Bacik | 8a35d95 | 2012-05-23 14:26:42 -0400 | [diff] [blame] | 7989 | atomic_dec(&root->orphan_inodes); |
Josef Bacik | 7b12876 | 2008-07-24 12:17:14 -0400 | [diff] [blame] | 7990 | } |
Josef Bacik | 7b12876 | 2008-07-24 12:17:14 -0400 | [diff] [blame] | 7991 | |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 7992 | while (1) { |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 7993 | ordered = btrfs_lookup_first_ordered_extent(inode, (u64)-1); |
| 7994 | if (!ordered) |
| 7995 | break; |
| 7996 | else { |
Simon Kirby | c2cf52e | 2013-03-19 22:41:23 +0000 | [diff] [blame] | 7997 | btrfs_err(root->fs_info, "found ordered extent %llu %llu on inode cleanup", |
Geert Uytterhoeven | c1c9ff7 | 2013-08-20 13:20:07 +0200 | [diff] [blame] | 7998 | ordered->file_offset, ordered->len); |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 7999 | btrfs_remove_ordered_extent(inode, ordered); |
| 8000 | btrfs_put_ordered_extent(ordered); |
| 8001 | btrfs_put_ordered_extent(ordered); |
| 8002 | } |
| 8003 | } |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 8004 | inode_tree_del(inode); |
Zheng Yan | 5b21f2e | 2008-09-26 10:05:38 -0400 | [diff] [blame] | 8005 | btrfs_drop_extent_cache(inode, 0, (u64)-1, 0); |
Josef Bacik | a6dbd42 | 2009-11-11 15:53:34 -0500 | [diff] [blame] | 8006 | free: |
Nick Piggin | fa0d7e3d | 2011-01-07 17:49:49 +1100 | [diff] [blame] | 8007 | call_rcu(&inode->i_rcu, btrfs_i_callback); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 8008 | } |
| 8009 | |
Al Viro | 45321ac | 2010-06-07 13:43:19 -0400 | [diff] [blame] | 8010 | int btrfs_drop_inode(struct inode *inode) |
Yan, Zheng | 76dda93 | 2009-09-21 16:00:26 -0400 | [diff] [blame] | 8011 | { |
| 8012 | struct btrfs_root *root = BTRFS_I(inode)->root; |
Al Viro | 45321ac | 2010-06-07 13:43:19 -0400 | [diff] [blame] | 8013 | |
Naohiro Aota | 6379ef9 | 2013-06-06 09:56:34 +0000 | [diff] [blame] | 8014 | if (root == NULL) |
| 8015 | return 1; |
| 8016 | |
Liu Bo | fa6ac87 | 2013-02-20 14:10:23 +0000 | [diff] [blame] | 8017 | /* the snap/subvol tree is on deleting */ |
Stefan Behrens | 69e9c6c | 2013-09-05 16:58:43 +0200 | [diff] [blame] | 8018 | if (btrfs_root_refs(&root->root_item) == 0) |
Al Viro | 45321ac | 2010-06-07 13:43:19 -0400 | [diff] [blame] | 8019 | return 1; |
Yan, Zheng | 76dda93 | 2009-09-21 16:00:26 -0400 | [diff] [blame] | 8020 | else |
Al Viro | 45321ac | 2010-06-07 13:43:19 -0400 | [diff] [blame] | 8021 | return generic_drop_inode(inode); |
Yan, Zheng | 76dda93 | 2009-09-21 16:00:26 -0400 | [diff] [blame] | 8022 | } |
| 8023 | |
Sven Wegener | 0ee0fda | 2008-07-30 16:54:26 -0400 | [diff] [blame] | 8024 | static void init_once(void *foo) |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 8025 | { |
| 8026 | struct btrfs_inode *ei = (struct btrfs_inode *) foo; |
| 8027 | |
| 8028 | inode_init_once(&ei->vfs_inode); |
| 8029 | } |
| 8030 | |
| 8031 | void btrfs_destroy_cachep(void) |
| 8032 | { |
Kirill A. Shutemov | 8c0a853 | 2012-09-26 11:33:07 +1000 | [diff] [blame] | 8033 | /* |
| 8034 | * Make sure all delayed rcu free inodes are flushed before we |
| 8035 | * destroy cache. |
| 8036 | */ |
| 8037 | rcu_barrier(); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 8038 | if (btrfs_inode_cachep) |
| 8039 | kmem_cache_destroy(btrfs_inode_cachep); |
| 8040 | if (btrfs_trans_handle_cachep) |
| 8041 | kmem_cache_destroy(btrfs_trans_handle_cachep); |
| 8042 | if (btrfs_transaction_cachep) |
| 8043 | kmem_cache_destroy(btrfs_transaction_cachep); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 8044 | if (btrfs_path_cachep) |
| 8045 | kmem_cache_destroy(btrfs_path_cachep); |
Josef Bacik | dc89e98 | 2011-01-28 17:05:48 -0500 | [diff] [blame] | 8046 | if (btrfs_free_space_cachep) |
| 8047 | kmem_cache_destroy(btrfs_free_space_cachep); |
Miao Xie | 8ccf6f19 | 2012-10-25 09:28:04 +0000 | [diff] [blame] | 8048 | if (btrfs_delalloc_work_cachep) |
| 8049 | kmem_cache_destroy(btrfs_delalloc_work_cachep); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 8050 | } |
| 8051 | |
| 8052 | int btrfs_init_cachep(void) |
| 8053 | { |
David Sterba | 837e197 | 2012-09-07 03:00:48 -0600 | [diff] [blame] | 8054 | btrfs_inode_cachep = kmem_cache_create("btrfs_inode", |
Christoph Hellwig | 9601e3f | 2009-04-13 15:33:09 +0200 | [diff] [blame] | 8055 | sizeof(struct btrfs_inode), 0, |
| 8056 | SLAB_RECLAIM_ACCOUNT | SLAB_MEM_SPREAD, init_once); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 8057 | if (!btrfs_inode_cachep) |
| 8058 | goto fail; |
Christoph Hellwig | 9601e3f | 2009-04-13 15:33:09 +0200 | [diff] [blame] | 8059 | |
David Sterba | 837e197 | 2012-09-07 03:00:48 -0600 | [diff] [blame] | 8060 | btrfs_trans_handle_cachep = kmem_cache_create("btrfs_trans_handle", |
Christoph Hellwig | 9601e3f | 2009-04-13 15:33:09 +0200 | [diff] [blame] | 8061 | sizeof(struct btrfs_trans_handle), 0, |
| 8062 | SLAB_RECLAIM_ACCOUNT | SLAB_MEM_SPREAD, NULL); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 8063 | if (!btrfs_trans_handle_cachep) |
| 8064 | goto fail; |
Christoph Hellwig | 9601e3f | 2009-04-13 15:33:09 +0200 | [diff] [blame] | 8065 | |
David Sterba | 837e197 | 2012-09-07 03:00:48 -0600 | [diff] [blame] | 8066 | btrfs_transaction_cachep = kmem_cache_create("btrfs_transaction", |
Christoph Hellwig | 9601e3f | 2009-04-13 15:33:09 +0200 | [diff] [blame] | 8067 | sizeof(struct btrfs_transaction), 0, |
| 8068 | SLAB_RECLAIM_ACCOUNT | SLAB_MEM_SPREAD, NULL); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 8069 | if (!btrfs_transaction_cachep) |
| 8070 | goto fail; |
Christoph Hellwig | 9601e3f | 2009-04-13 15:33:09 +0200 | [diff] [blame] | 8071 | |
David Sterba | 837e197 | 2012-09-07 03:00:48 -0600 | [diff] [blame] | 8072 | btrfs_path_cachep = kmem_cache_create("btrfs_path", |
Christoph Hellwig | 9601e3f | 2009-04-13 15:33:09 +0200 | [diff] [blame] | 8073 | sizeof(struct btrfs_path), 0, |
| 8074 | SLAB_RECLAIM_ACCOUNT | SLAB_MEM_SPREAD, NULL); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 8075 | if (!btrfs_path_cachep) |
| 8076 | goto fail; |
Christoph Hellwig | 9601e3f | 2009-04-13 15:33:09 +0200 | [diff] [blame] | 8077 | |
David Sterba | 837e197 | 2012-09-07 03:00:48 -0600 | [diff] [blame] | 8078 | btrfs_free_space_cachep = kmem_cache_create("btrfs_free_space", |
Josef Bacik | dc89e98 | 2011-01-28 17:05:48 -0500 | [diff] [blame] | 8079 | sizeof(struct btrfs_free_space), 0, |
| 8080 | SLAB_RECLAIM_ACCOUNT | SLAB_MEM_SPREAD, NULL); |
| 8081 | if (!btrfs_free_space_cachep) |
| 8082 | goto fail; |
| 8083 | |
Miao Xie | 8ccf6f19 | 2012-10-25 09:28:04 +0000 | [diff] [blame] | 8084 | btrfs_delalloc_work_cachep = kmem_cache_create("btrfs_delalloc_work", |
| 8085 | sizeof(struct btrfs_delalloc_work), 0, |
| 8086 | SLAB_RECLAIM_ACCOUNT | SLAB_MEM_SPREAD, |
| 8087 | NULL); |
| 8088 | if (!btrfs_delalloc_work_cachep) |
| 8089 | goto fail; |
| 8090 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 8091 | return 0; |
| 8092 | fail: |
| 8093 | btrfs_destroy_cachep(); |
| 8094 | return -ENOMEM; |
| 8095 | } |
| 8096 | |
| 8097 | static int btrfs_getattr(struct vfsmount *mnt, |
| 8098 | struct dentry *dentry, struct kstat *stat) |
| 8099 | { |
Miao Xie | df0af1a | 2013-01-29 10:11:59 +0000 | [diff] [blame] | 8100 | u64 delalloc_bytes; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 8101 | struct inode *inode = dentry->d_inode; |
David Sterba | fadc0d8 | 2011-11-20 07:33:38 -0500 | [diff] [blame] | 8102 | u32 blocksize = inode->i_sb->s_blocksize; |
| 8103 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 8104 | generic_fillattr(inode, stat); |
Al Viro | 0ee5dc6 | 2011-07-07 15:44:25 -0400 | [diff] [blame] | 8105 | stat->dev = BTRFS_I(inode)->root->anon_dev; |
Chris Mason | d666746 | 2008-01-03 14:51:00 -0500 | [diff] [blame] | 8106 | stat->blksize = PAGE_CACHE_SIZE; |
Miao Xie | df0af1a | 2013-01-29 10:11:59 +0000 | [diff] [blame] | 8107 | |
| 8108 | spin_lock(&BTRFS_I(inode)->lock); |
| 8109 | delalloc_bytes = BTRFS_I(inode)->delalloc_bytes; |
| 8110 | spin_unlock(&BTRFS_I(inode)->lock); |
David Sterba | fadc0d8 | 2011-11-20 07:33:38 -0500 | [diff] [blame] | 8111 | stat->blocks = (ALIGN(inode_get_bytes(inode), blocksize) + |
Miao Xie | df0af1a | 2013-01-29 10:11:59 +0000 | [diff] [blame] | 8112 | ALIGN(delalloc_bytes, blocksize)) >> 9; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 8113 | return 0; |
| 8114 | } |
| 8115 | |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 8116 | static int btrfs_rename(struct inode *old_dir, struct dentry *old_dentry, |
| 8117 | struct inode *new_dir, struct dentry *new_dentry) |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 8118 | { |
| 8119 | struct btrfs_trans_handle *trans; |
| 8120 | struct btrfs_root *root = BTRFS_I(old_dir)->root; |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 8121 | struct btrfs_root *dest = BTRFS_I(new_dir)->root; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 8122 | struct inode *new_inode = new_dentry->d_inode; |
| 8123 | struct inode *old_inode = old_dentry->d_inode; |
| 8124 | struct timespec ctime = CURRENT_TIME; |
Chris Mason | 00e4e6b | 2008-08-05 11:18:09 -0400 | [diff] [blame] | 8125 | u64 index = 0; |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 8126 | u64 root_objectid; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 8127 | int ret; |
Li Zefan | 33345d01 | 2011-04-20 10:31:50 +0800 | [diff] [blame] | 8128 | u64 old_ino = btrfs_ino(old_inode); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 8129 | |
Li Zefan | 33345d01 | 2011-04-20 10:31:50 +0800 | [diff] [blame] | 8130 | if (btrfs_ino(new_dir) == BTRFS_EMPTY_SUBVOL_DIR_OBJECTID) |
Yan, Zheng | f679a84 | 2009-09-24 09:17:31 -0400 | [diff] [blame] | 8131 | return -EPERM; |
| 8132 | |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 8133 | /* we only allow rename subvolume link between subvolumes */ |
Li Zefan | 33345d01 | 2011-04-20 10:31:50 +0800 | [diff] [blame] | 8134 | if (old_ino != BTRFS_FIRST_FREE_OBJECTID && root != dest) |
Chris Mason | 3394e16 | 2008-11-17 20:42:26 -0500 | [diff] [blame] | 8135 | return -EXDEV; |
| 8136 | |
Li Zefan | 33345d01 | 2011-04-20 10:31:50 +0800 | [diff] [blame] | 8137 | if (old_ino == BTRFS_EMPTY_SUBVOL_DIR_OBJECTID || |
| 8138 | (new_inode && btrfs_ino(new_inode) == BTRFS_FIRST_FREE_OBJECTID)) |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 8139 | return -ENOTEMPTY; |
| 8140 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 8141 | if (S_ISDIR(old_inode->i_mode) && new_inode && |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 8142 | new_inode->i_size > BTRFS_EMPTY_DIR_SIZE) |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 8143 | return -ENOTEMPTY; |
Chris Mason | 9c52057 | 2012-12-17 14:26:57 -0500 | [diff] [blame] | 8144 | |
| 8145 | |
| 8146 | /* check for collisions, even if the name isn't there */ |
Josef Bacik | 4871c15 | 2013-10-09 12:24:04 -0400 | [diff] [blame] | 8147 | ret = btrfs_check_dir_item_collision(dest, new_dir->i_ino, |
Chris Mason | 9c52057 | 2012-12-17 14:26:57 -0500 | [diff] [blame] | 8148 | new_dentry->d_name.name, |
| 8149 | new_dentry->d_name.len); |
| 8150 | |
| 8151 | if (ret) { |
| 8152 | if (ret == -EEXIST) { |
| 8153 | /* we shouldn't get |
| 8154 | * eexist without a new_inode */ |
Dulshani Gunawardhana | fae7f21 | 2013-10-31 10:30:08 +0530 | [diff] [blame] | 8155 | if (WARN_ON(!new_inode)) { |
Chris Mason | 9c52057 | 2012-12-17 14:26:57 -0500 | [diff] [blame] | 8156 | return ret; |
| 8157 | } |
| 8158 | } else { |
| 8159 | /* maybe -EOVERFLOW */ |
| 8160 | return ret; |
| 8161 | } |
| 8162 | } |
| 8163 | ret = 0; |
| 8164 | |
Chris Mason | 5a3f23d | 2009-03-31 13:27:11 -0400 | [diff] [blame] | 8165 | /* |
| 8166 | * we're using rename to replace one file with another. |
| 8167 | * and the replacement file is large. Start IO on it now so |
| 8168 | * we don't add too much work to the end of the transaction |
| 8169 | */ |
Bartlomiej Zolnierkiewicz | 4baf8c9 | 2009-08-07 13:47:08 -0400 | [diff] [blame] | 8170 | 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] | 8171 | old_inode->i_size > BTRFS_ORDERED_OPERATIONS_FLUSH_LIMIT) |
| 8172 | filemap_flush(old_inode->i_mapping); |
| 8173 | |
Yan, Zheng | 76dda93 | 2009-09-21 16:00:26 -0400 | [diff] [blame] | 8174 | /* close the racy window with snapshot create/destroy ioctl */ |
Li Zefan | 33345d01 | 2011-04-20 10:31:50 +0800 | [diff] [blame] | 8175 | if (old_ino == BTRFS_FIRST_FREE_OBJECTID) |
Yan, Zheng | 76dda93 | 2009-09-21 16:00:26 -0400 | [diff] [blame] | 8176 | down_read(&root->fs_info->subvol_sem); |
Yan, Zheng | a22285a | 2010-05-16 10:48:46 -0400 | [diff] [blame] | 8177 | /* |
| 8178 | * We want to reserve the absolute worst case amount of items. So if |
| 8179 | * both inodes are subvols and we need to unlink them then that would |
| 8180 | * require 4 item modifications, but if they are both normal inodes it |
| 8181 | * would require 5 item modifications, so we'll assume their normal |
| 8182 | * inodes. So 5 * 2 is 10, plus 1 for the new link, so 11 total items |
| 8183 | * should cover the worst case number of items we'll modify. |
| 8184 | */ |
Josef Bacik | 6e137ed | 2013-03-26 15:26:55 -0400 | [diff] [blame] | 8185 | trans = btrfs_start_transaction(root, 11); |
Johann Lombardi | b44c59a | 2011-03-31 13:23:47 +0000 | [diff] [blame] | 8186 | if (IS_ERR(trans)) { |
| 8187 | ret = PTR_ERR(trans); |
| 8188 | goto out_notrans; |
| 8189 | } |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 8190 | |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 8191 | if (dest != root) |
| 8192 | btrfs_record_root_in_trans(trans, dest); |
| 8193 | |
Yan, Zheng | a571952 | 2009-09-24 09:17:31 -0400 | [diff] [blame] | 8194 | ret = btrfs_set_inode_index(new_dir, &index); |
| 8195 | if (ret) |
| 8196 | goto out_fail; |
Chris Mason | 5a3f23d | 2009-03-31 13:27:11 -0400 | [diff] [blame] | 8197 | |
Miao Xie | 67de117 | 2013-12-26 13:07:06 +0800 | [diff] [blame^] | 8198 | BTRFS_I(old_inode)->dir_index = 0ULL; |
Li Zefan | 33345d01 | 2011-04-20 10:31:50 +0800 | [diff] [blame] | 8199 | if (unlikely(old_ino == BTRFS_FIRST_FREE_OBJECTID)) { |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 8200 | /* force full log commit if subvolume involved. */ |
| 8201 | root->fs_info->last_trans_log_full_commit = trans->transid; |
| 8202 | } else { |
Yan, Zheng | a571952 | 2009-09-24 09:17:31 -0400 | [diff] [blame] | 8203 | ret = btrfs_insert_inode_ref(trans, dest, |
| 8204 | new_dentry->d_name.name, |
| 8205 | new_dentry->d_name.len, |
Li Zefan | 33345d01 | 2011-04-20 10:31:50 +0800 | [diff] [blame] | 8206 | old_ino, |
| 8207 | btrfs_ino(new_dir), index); |
Yan, Zheng | a571952 | 2009-09-24 09:17:31 -0400 | [diff] [blame] | 8208 | if (ret) |
| 8209 | goto out_fail; |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 8210 | /* |
| 8211 | * this is an ugly little race, but the rename is required |
| 8212 | * to make sure that if we crash, the inode is either at the |
| 8213 | * old name or the new one. pinning the log transaction lets |
| 8214 | * us make sure we don't allow a log commit to come in after |
| 8215 | * we unlink the name but before we add the new name back in. |
| 8216 | */ |
| 8217 | btrfs_pin_log_trans(root); |
| 8218 | } |
Chris Mason | 12fcfd2 | 2009-03-24 10:24:20 -0400 | [diff] [blame] | 8219 | /* |
Chris Mason | 5a3f23d | 2009-03-31 13:27:11 -0400 | [diff] [blame] | 8220 | * make sure the inode gets flushed if it is replacing |
| 8221 | * something. |
| 8222 | */ |
Li Zefan | 33345d01 | 2011-04-20 10:31:50 +0800 | [diff] [blame] | 8223 | 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] | 8224 | btrfs_add_ordered_operation(trans, root, old_inode); |
Chris Mason | 5a3f23d | 2009-03-31 13:27:11 -0400 | [diff] [blame] | 8225 | |
Josef Bacik | 0c4d2d9 | 2012-04-05 15:03:02 -0400 | [diff] [blame] | 8226 | inode_inc_iversion(old_dir); |
| 8227 | inode_inc_iversion(new_dir); |
| 8228 | inode_inc_iversion(old_inode); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 8229 | old_dir->i_ctime = old_dir->i_mtime = ctime; |
| 8230 | new_dir->i_ctime = new_dir->i_mtime = ctime; |
| 8231 | old_inode->i_ctime = ctime; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 8232 | |
Chris Mason | 12fcfd2 | 2009-03-24 10:24:20 -0400 | [diff] [blame] | 8233 | if (old_dentry->d_parent != new_dentry->d_parent) |
| 8234 | btrfs_record_unlink_dir(trans, old_dir, old_inode, 1); |
| 8235 | |
Li Zefan | 33345d01 | 2011-04-20 10:31:50 +0800 | [diff] [blame] | 8236 | if (unlikely(old_ino == BTRFS_FIRST_FREE_OBJECTID)) { |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 8237 | root_objectid = BTRFS_I(old_inode)->root->root_key.objectid; |
| 8238 | ret = btrfs_unlink_subvol(trans, root, old_dir, root_objectid, |
| 8239 | old_dentry->d_name.name, |
| 8240 | old_dentry->d_name.len); |
| 8241 | } else { |
Al Viro | 9298679 | 2011-03-04 17:14:37 +0000 | [diff] [blame] | 8242 | ret = __btrfs_unlink_inode(trans, root, old_dir, |
| 8243 | old_dentry->d_inode, |
| 8244 | old_dentry->d_name.name, |
| 8245 | old_dentry->d_name.len); |
| 8246 | if (!ret) |
| 8247 | ret = btrfs_update_inode(trans, root, old_inode); |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 8248 | } |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 8249 | if (ret) { |
| 8250 | btrfs_abort_transaction(trans, root, ret); |
| 8251 | goto out_fail; |
| 8252 | } |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 8253 | |
| 8254 | if (new_inode) { |
Josef Bacik | 0c4d2d9 | 2012-04-05 15:03:02 -0400 | [diff] [blame] | 8255 | inode_inc_iversion(new_inode); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 8256 | new_inode->i_ctime = CURRENT_TIME; |
Li Zefan | 33345d01 | 2011-04-20 10:31:50 +0800 | [diff] [blame] | 8257 | if (unlikely(btrfs_ino(new_inode) == |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 8258 | BTRFS_EMPTY_SUBVOL_DIR_OBJECTID)) { |
| 8259 | root_objectid = BTRFS_I(new_inode)->location.objectid; |
| 8260 | ret = btrfs_unlink_subvol(trans, dest, new_dir, |
| 8261 | root_objectid, |
| 8262 | new_dentry->d_name.name, |
| 8263 | new_dentry->d_name.len); |
| 8264 | BUG_ON(new_inode->i_nlink == 0); |
| 8265 | } else { |
| 8266 | ret = btrfs_unlink_inode(trans, dest, new_dir, |
| 8267 | new_dentry->d_inode, |
| 8268 | new_dentry->d_name.name, |
| 8269 | new_dentry->d_name.len); |
| 8270 | } |
Josef Bacik | 4ef31a4 | 2013-08-13 14:10:08 -0400 | [diff] [blame] | 8271 | if (!ret && new_inode->i_nlink == 0) |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 8272 | ret = btrfs_orphan_add(trans, new_dentry->d_inode); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 8273 | if (ret) { |
| 8274 | btrfs_abort_transaction(trans, root, ret); |
| 8275 | goto out_fail; |
| 8276 | } |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 8277 | } |
Josef Bacik | aec7477 | 2008-07-24 12:12:38 -0400 | [diff] [blame] | 8278 | |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 8279 | ret = btrfs_add_link(trans, new_dir, old_inode, |
| 8280 | new_dentry->d_name.name, |
Yan, Zheng | a571952 | 2009-09-24 09:17:31 -0400 | [diff] [blame] | 8281 | new_dentry->d_name.len, 0, index); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 8282 | if (ret) { |
| 8283 | btrfs_abort_transaction(trans, root, ret); |
| 8284 | goto out_fail; |
| 8285 | } |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 8286 | |
Miao Xie | 67de117 | 2013-12-26 13:07:06 +0800 | [diff] [blame^] | 8287 | if (old_inode->i_nlink == 1) |
| 8288 | BTRFS_I(old_inode)->dir_index = index; |
| 8289 | |
Li Zefan | 33345d01 | 2011-04-20 10:31:50 +0800 | [diff] [blame] | 8290 | if (old_ino != BTRFS_FIRST_FREE_OBJECTID) { |
Al Viro | 10d9f30 | 2011-07-16 23:09:10 -0400 | [diff] [blame] | 8291 | struct dentry *parent = new_dentry->d_parent; |
Josef Bacik | 6a91221 | 2010-11-20 09:48:00 +0000 | [diff] [blame] | 8292 | btrfs_log_new_name(trans, old_inode, old_dir, parent); |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 8293 | btrfs_end_log_trans(root); |
| 8294 | } |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 8295 | out_fail: |
Josef Bacik | 7ad85bb | 2012-01-12 19:10:12 -0500 | [diff] [blame] | 8296 | btrfs_end_transaction(trans, root); |
Johann Lombardi | b44c59a | 2011-03-31 13:23:47 +0000 | [diff] [blame] | 8297 | out_notrans: |
Li Zefan | 33345d01 | 2011-04-20 10:31:50 +0800 | [diff] [blame] | 8298 | if (old_ino == BTRFS_FIRST_FREE_OBJECTID) |
Yan, Zheng | 76dda93 | 2009-09-21 16:00:26 -0400 | [diff] [blame] | 8299 | up_read(&root->fs_info->subvol_sem); |
Josef Bacik | 9ed74f2 | 2009-09-11 16:12:44 -0400 | [diff] [blame] | 8300 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 8301 | return ret; |
| 8302 | } |
| 8303 | |
Miao Xie | 8ccf6f19 | 2012-10-25 09:28:04 +0000 | [diff] [blame] | 8304 | static void btrfs_run_delalloc_work(struct btrfs_work *work) |
| 8305 | { |
| 8306 | struct btrfs_delalloc_work *delalloc_work; |
Josef Bacik | 9f23e28 | 2013-10-28 15:03:41 -0400 | [diff] [blame] | 8307 | struct inode *inode; |
Miao Xie | 8ccf6f19 | 2012-10-25 09:28:04 +0000 | [diff] [blame] | 8308 | |
| 8309 | delalloc_work = container_of(work, struct btrfs_delalloc_work, |
| 8310 | work); |
Josef Bacik | 9f23e28 | 2013-10-28 15:03:41 -0400 | [diff] [blame] | 8311 | inode = delalloc_work->inode; |
| 8312 | if (delalloc_work->wait) { |
| 8313 | btrfs_wait_ordered_range(inode, 0, (u64)-1); |
| 8314 | } else { |
| 8315 | filemap_flush(inode->i_mapping); |
| 8316 | if (test_bit(BTRFS_INODE_HAS_ASYNC_EXTENT, |
| 8317 | &BTRFS_I(inode)->runtime_flags)) |
| 8318 | filemap_flush(inode->i_mapping); |
| 8319 | } |
Miao Xie | 8ccf6f19 | 2012-10-25 09:28:04 +0000 | [diff] [blame] | 8320 | |
| 8321 | if (delalloc_work->delay_iput) |
Josef Bacik | 9f23e28 | 2013-10-28 15:03:41 -0400 | [diff] [blame] | 8322 | btrfs_add_delayed_iput(inode); |
Miao Xie | 8ccf6f19 | 2012-10-25 09:28:04 +0000 | [diff] [blame] | 8323 | else |
Josef Bacik | 9f23e28 | 2013-10-28 15:03:41 -0400 | [diff] [blame] | 8324 | iput(inode); |
Miao Xie | 8ccf6f19 | 2012-10-25 09:28:04 +0000 | [diff] [blame] | 8325 | complete(&delalloc_work->completion); |
| 8326 | } |
| 8327 | |
| 8328 | struct btrfs_delalloc_work *btrfs_alloc_delalloc_work(struct inode *inode, |
| 8329 | int wait, int delay_iput) |
| 8330 | { |
| 8331 | struct btrfs_delalloc_work *work; |
| 8332 | |
| 8333 | work = kmem_cache_zalloc(btrfs_delalloc_work_cachep, GFP_NOFS); |
| 8334 | if (!work) |
| 8335 | return NULL; |
| 8336 | |
| 8337 | init_completion(&work->completion); |
| 8338 | INIT_LIST_HEAD(&work->list); |
| 8339 | work->inode = inode; |
| 8340 | work->wait = wait; |
| 8341 | work->delay_iput = delay_iput; |
| 8342 | work->work.func = btrfs_run_delalloc_work; |
| 8343 | |
| 8344 | return work; |
| 8345 | } |
| 8346 | |
| 8347 | void btrfs_wait_and_free_delalloc_work(struct btrfs_delalloc_work *work) |
| 8348 | { |
| 8349 | wait_for_completion(&work->completion); |
| 8350 | kmem_cache_free(btrfs_delalloc_work_cachep, work); |
| 8351 | } |
| 8352 | |
Chris Mason | d352ac6 | 2008-09-29 15:18:18 -0400 | [diff] [blame] | 8353 | /* |
| 8354 | * some fairly slow code that needs optimization. This walks the list |
| 8355 | * of all the inodes with pending delalloc and forces them to disk. |
| 8356 | */ |
Miao Xie | eb73c1b | 2013-05-15 07:48:22 +0000 | [diff] [blame] | 8357 | static int __start_delalloc_inodes(struct btrfs_root *root, int delay_iput) |
Chris Mason | ea8c281 | 2008-08-04 23:17:27 -0400 | [diff] [blame] | 8358 | { |
Chris Mason | ea8c281 | 2008-08-04 23:17:27 -0400 | [diff] [blame] | 8359 | struct btrfs_inode *binode; |
Zheng Yan | 5b21f2e | 2008-09-26 10:05:38 -0400 | [diff] [blame] | 8360 | struct inode *inode; |
Miao Xie | 8ccf6f19 | 2012-10-25 09:28:04 +0000 | [diff] [blame] | 8361 | struct btrfs_delalloc_work *work, *next; |
| 8362 | struct list_head works; |
Miao Xie | 1eafa6c | 2013-01-22 10:49:00 +0000 | [diff] [blame] | 8363 | struct list_head splice; |
Miao Xie | 8ccf6f19 | 2012-10-25 09:28:04 +0000 | [diff] [blame] | 8364 | int ret = 0; |
Chris Mason | ea8c281 | 2008-08-04 23:17:27 -0400 | [diff] [blame] | 8365 | |
Miao Xie | 8ccf6f19 | 2012-10-25 09:28:04 +0000 | [diff] [blame] | 8366 | INIT_LIST_HEAD(&works); |
Miao Xie | 1eafa6c | 2013-01-22 10:49:00 +0000 | [diff] [blame] | 8367 | INIT_LIST_HEAD(&splice); |
Miao Xie | 63607cc | 2013-01-22 10:50:35 +0000 | [diff] [blame] | 8368 | |
Miao Xie | eb73c1b | 2013-05-15 07:48:22 +0000 | [diff] [blame] | 8369 | spin_lock(&root->delalloc_lock); |
| 8370 | list_splice_init(&root->delalloc_inodes, &splice); |
Miao Xie | 1eafa6c | 2013-01-22 10:49:00 +0000 | [diff] [blame] | 8371 | while (!list_empty(&splice)) { |
| 8372 | binode = list_entry(splice.next, struct btrfs_inode, |
Chris Mason | ea8c281 | 2008-08-04 23:17:27 -0400 | [diff] [blame] | 8373 | delalloc_inodes); |
Miao Xie | 1eafa6c | 2013-01-22 10:49:00 +0000 | [diff] [blame] | 8374 | |
Miao Xie | eb73c1b | 2013-05-15 07:48:22 +0000 | [diff] [blame] | 8375 | list_move_tail(&binode->delalloc_inodes, |
| 8376 | &root->delalloc_inodes); |
Zheng Yan | 5b21f2e | 2008-09-26 10:05:38 -0400 | [diff] [blame] | 8377 | inode = igrab(&binode->vfs_inode); |
Miao Xie | df0af1a | 2013-01-29 10:11:59 +0000 | [diff] [blame] | 8378 | if (!inode) { |
Miao Xie | eb73c1b | 2013-05-15 07:48:22 +0000 | [diff] [blame] | 8379 | cond_resched_lock(&root->delalloc_lock); |
Miao Xie | 1eafa6c | 2013-01-22 10:49:00 +0000 | [diff] [blame] | 8380 | continue; |
Miao Xie | df0af1a | 2013-01-29 10:11:59 +0000 | [diff] [blame] | 8381 | } |
Miao Xie | eb73c1b | 2013-05-15 07:48:22 +0000 | [diff] [blame] | 8382 | spin_unlock(&root->delalloc_lock); |
Miao Xie | 1eafa6c | 2013-01-22 10:49:00 +0000 | [diff] [blame] | 8383 | |
| 8384 | work = btrfs_alloc_delalloc_work(inode, 0, delay_iput); |
| 8385 | if (unlikely(!work)) { |
Josef Bacik | f4ab9ea | 2013-09-17 11:25:44 -0400 | [diff] [blame] | 8386 | if (delay_iput) |
| 8387 | btrfs_add_delayed_iput(inode); |
| 8388 | else |
| 8389 | iput(inode); |
Miao Xie | 1eafa6c | 2013-01-22 10:49:00 +0000 | [diff] [blame] | 8390 | ret = -ENOMEM; |
| 8391 | goto out; |
Zheng Yan | 5b21f2e | 2008-09-26 10:05:38 -0400 | [diff] [blame] | 8392 | } |
Miao Xie | 1eafa6c | 2013-01-22 10:49:00 +0000 | [diff] [blame] | 8393 | list_add_tail(&work->list, &works); |
| 8394 | btrfs_queue_worker(&root->fs_info->flush_workers, |
| 8395 | &work->work); |
| 8396 | |
Zheng Yan | 5b21f2e | 2008-09-26 10:05:38 -0400 | [diff] [blame] | 8397 | cond_resched(); |
Miao Xie | eb73c1b | 2013-05-15 07:48:22 +0000 | [diff] [blame] | 8398 | spin_lock(&root->delalloc_lock); |
Chris Mason | ea8c281 | 2008-08-04 23:17:27 -0400 | [diff] [blame] | 8399 | } |
Miao Xie | eb73c1b | 2013-05-15 07:48:22 +0000 | [diff] [blame] | 8400 | spin_unlock(&root->delalloc_lock); |
Chris Mason | 8c8bee1 | 2008-09-29 11:19:10 -0400 | [diff] [blame] | 8401 | |
Miao Xie | 1eafa6c | 2013-01-22 10:49:00 +0000 | [diff] [blame] | 8402 | list_for_each_entry_safe(work, next, &works, list) { |
| 8403 | list_del_init(&work->list); |
| 8404 | btrfs_wait_and_free_delalloc_work(work); |
| 8405 | } |
Miao Xie | eb73c1b | 2013-05-15 07:48:22 +0000 | [diff] [blame] | 8406 | return 0; |
| 8407 | out: |
| 8408 | list_for_each_entry_safe(work, next, &works, list) { |
| 8409 | list_del_init(&work->list); |
| 8410 | btrfs_wait_and_free_delalloc_work(work); |
| 8411 | } |
Miao Xie | 1eafa6c | 2013-01-22 10:49:00 +0000 | [diff] [blame] | 8412 | |
Miao Xie | eb73c1b | 2013-05-15 07:48:22 +0000 | [diff] [blame] | 8413 | if (!list_empty_careful(&splice)) { |
| 8414 | spin_lock(&root->delalloc_lock); |
| 8415 | list_splice_tail(&splice, &root->delalloc_inodes); |
| 8416 | spin_unlock(&root->delalloc_lock); |
| 8417 | } |
| 8418 | return ret; |
| 8419 | } |
| 8420 | |
| 8421 | int btrfs_start_delalloc_inodes(struct btrfs_root *root, int delay_iput) |
| 8422 | { |
| 8423 | int ret; |
| 8424 | |
| 8425 | if (root->fs_info->sb->s_flags & MS_RDONLY) |
| 8426 | return -EROFS; |
| 8427 | |
| 8428 | ret = __start_delalloc_inodes(root, delay_iput); |
| 8429 | /* |
| 8430 | * the filemap_flush will queue IO into the worker threads, but |
Chris Mason | 8c8bee1 | 2008-09-29 11:19:10 -0400 | [diff] [blame] | 8431 | * we have to make sure the IO is actually started and that |
| 8432 | * ordered extents get created before we return |
| 8433 | */ |
| 8434 | atomic_inc(&root->fs_info->async_submit_draining); |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 8435 | while (atomic_read(&root->fs_info->nr_async_submits) || |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 8436 | atomic_read(&root->fs_info->async_delalloc_pages)) { |
Chris Mason | 8c8bee1 | 2008-09-29 11:19:10 -0400 | [diff] [blame] | 8437 | wait_event(root->fs_info->async_submit_wait, |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 8438 | (atomic_read(&root->fs_info->nr_async_submits) == 0 && |
| 8439 | atomic_read(&root->fs_info->async_delalloc_pages) == 0)); |
Chris Mason | 8c8bee1 | 2008-09-29 11:19:10 -0400 | [diff] [blame] | 8440 | } |
| 8441 | atomic_dec(&root->fs_info->async_submit_draining); |
Miao Xie | eb73c1b | 2013-05-15 07:48:22 +0000 | [diff] [blame] | 8442 | return ret; |
| 8443 | } |
| 8444 | |
Miao Xie | 91aef86 | 2013-11-04 23:13:26 +0800 | [diff] [blame] | 8445 | int btrfs_start_delalloc_roots(struct btrfs_fs_info *fs_info, int delay_iput) |
Miao Xie | eb73c1b | 2013-05-15 07:48:22 +0000 | [diff] [blame] | 8446 | { |
| 8447 | struct btrfs_root *root; |
| 8448 | struct list_head splice; |
| 8449 | int ret; |
| 8450 | |
| 8451 | if (fs_info->sb->s_flags & MS_RDONLY) |
| 8452 | return -EROFS; |
| 8453 | |
| 8454 | INIT_LIST_HEAD(&splice); |
| 8455 | |
| 8456 | spin_lock(&fs_info->delalloc_root_lock); |
| 8457 | list_splice_init(&fs_info->delalloc_roots, &splice); |
| 8458 | while (!list_empty(&splice)) { |
| 8459 | root = list_first_entry(&splice, struct btrfs_root, |
| 8460 | delalloc_root); |
| 8461 | root = btrfs_grab_fs_root(root); |
| 8462 | BUG_ON(!root); |
| 8463 | list_move_tail(&root->delalloc_root, |
| 8464 | &fs_info->delalloc_roots); |
| 8465 | spin_unlock(&fs_info->delalloc_root_lock); |
| 8466 | |
| 8467 | ret = __start_delalloc_inodes(root, delay_iput); |
| 8468 | btrfs_put_fs_root(root); |
| 8469 | if (ret) |
| 8470 | goto out; |
| 8471 | |
| 8472 | spin_lock(&fs_info->delalloc_root_lock); |
| 8473 | } |
| 8474 | spin_unlock(&fs_info->delalloc_root_lock); |
| 8475 | |
| 8476 | atomic_inc(&fs_info->async_submit_draining); |
| 8477 | while (atomic_read(&fs_info->nr_async_submits) || |
| 8478 | atomic_read(&fs_info->async_delalloc_pages)) { |
| 8479 | wait_event(fs_info->async_submit_wait, |
| 8480 | (atomic_read(&fs_info->nr_async_submits) == 0 && |
| 8481 | atomic_read(&fs_info->async_delalloc_pages) == 0)); |
| 8482 | } |
| 8483 | atomic_dec(&fs_info->async_submit_draining); |
Miao Xie | 1eafa6c | 2013-01-22 10:49:00 +0000 | [diff] [blame] | 8484 | return 0; |
Miao Xie | 8ccf6f19 | 2012-10-25 09:28:04 +0000 | [diff] [blame] | 8485 | out: |
Miao Xie | 1eafa6c | 2013-01-22 10:49:00 +0000 | [diff] [blame] | 8486 | if (!list_empty_careful(&splice)) { |
Miao Xie | eb73c1b | 2013-05-15 07:48:22 +0000 | [diff] [blame] | 8487 | spin_lock(&fs_info->delalloc_root_lock); |
| 8488 | list_splice_tail(&splice, &fs_info->delalloc_roots); |
| 8489 | spin_unlock(&fs_info->delalloc_root_lock); |
Miao Xie | 1eafa6c | 2013-01-22 10:49:00 +0000 | [diff] [blame] | 8490 | } |
Miao Xie | 8ccf6f19 | 2012-10-25 09:28:04 +0000 | [diff] [blame] | 8491 | return ret; |
Chris Mason | ea8c281 | 2008-08-04 23:17:27 -0400 | [diff] [blame] | 8492 | } |
| 8493 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 8494 | static int btrfs_symlink(struct inode *dir, struct dentry *dentry, |
| 8495 | const char *symname) |
| 8496 | { |
| 8497 | struct btrfs_trans_handle *trans; |
| 8498 | struct btrfs_root *root = BTRFS_I(dir)->root; |
| 8499 | struct btrfs_path *path; |
| 8500 | struct btrfs_key key; |
Chris Mason | 1832a6d | 2007-12-21 16:27:21 -0500 | [diff] [blame] | 8501 | struct inode *inode = NULL; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 8502 | int err; |
| 8503 | int drop_inode = 0; |
| 8504 | u64 objectid; |
Dulshani Gunawardhana | 6787125 | 2013-10-31 10:33:04 +0530 | [diff] [blame] | 8505 | u64 index = 0; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 8506 | int name_len; |
| 8507 | int datasize; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 8508 | unsigned long ptr; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 8509 | struct btrfs_file_extent_item *ei; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 8510 | struct extent_buffer *leaf; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 8511 | |
Filipe David Borba Manana | f06becc | 2013-09-16 09:53:28 +0100 | [diff] [blame] | 8512 | name_len = strlen(symname); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 8513 | if (name_len > BTRFS_MAX_INLINE_DATA_SIZE(root)) |
| 8514 | return -ENAMETOOLONG; |
Chris Mason | 1832a6d | 2007-12-21 16:27:21 -0500 | [diff] [blame] | 8515 | |
Josef Bacik | 9ed74f2 | 2009-09-11 16:12:44 -0400 | [diff] [blame] | 8516 | /* |
| 8517 | * 2 items for inode item and ref |
| 8518 | * 2 items for dir items |
| 8519 | * 1 item for xattr if selinux is on |
| 8520 | */ |
Yan, Zheng | a22285a | 2010-05-16 10:48:46 -0400 | [diff] [blame] | 8521 | trans = btrfs_start_transaction(root, 5); |
| 8522 | if (IS_ERR(trans)) |
| 8523 | return PTR_ERR(trans); |
Chris Mason | 1832a6d | 2007-12-21 16:27:21 -0500 | [diff] [blame] | 8524 | |
Li Zefan | 581bb05 | 2011-04-20 10:06:11 +0800 | [diff] [blame] | 8525 | err = btrfs_find_free_ino(root, &objectid); |
| 8526 | if (err) |
| 8527 | goto out_unlock; |
| 8528 | |
Josef Bacik | aec7477 | 2008-07-24 12:12:38 -0400 | [diff] [blame] | 8529 | inode = btrfs_new_inode(trans, root, dir, dentry->d_name.name, |
Li Zefan | 33345d01 | 2011-04-20 10:31:50 +0800 | [diff] [blame] | 8530 | dentry->d_name.len, btrfs_ino(dir), objectid, |
Josef Bacik | d82a6f1d | 2011-05-11 15:26:06 -0400 | [diff] [blame] | 8531 | S_IFLNK|S_IRWXUGO, &index); |
Tsutomu Itoh | 7cf96da | 2011-04-25 19:43:53 -0400 | [diff] [blame] | 8532 | if (IS_ERR(inode)) { |
| 8533 | err = PTR_ERR(inode); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 8534 | goto out_unlock; |
Tsutomu Itoh | 7cf96da | 2011-04-25 19:43:53 -0400 | [diff] [blame] | 8535 | } |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 8536 | |
Eric Paris | 2a7dba3 | 2011-02-01 11:05:39 -0500 | [diff] [blame] | 8537 | err = btrfs_init_inode_security(trans, inode, dir, &dentry->d_name); |
Josef Bacik | 33268ea | 2008-07-24 12:16:36 -0400 | [diff] [blame] | 8538 | if (err) { |
| 8539 | drop_inode = 1; |
| 8540 | goto out_unlock; |
| 8541 | } |
| 8542 | |
Casey Schaufler | ad19db7 | 2011-12-15 10:09:07 -0500 | [diff] [blame] | 8543 | /* |
| 8544 | * If the active LSM wants to access the inode during |
| 8545 | * d_instantiate it needs these. Smack checks to see |
| 8546 | * if the filesystem supports xattrs by looking at the |
| 8547 | * ops vector. |
| 8548 | */ |
| 8549 | inode->i_fop = &btrfs_file_operations; |
| 8550 | inode->i_op = &btrfs_file_inode_operations; |
| 8551 | |
Josef Bacik | a1b075d | 2010-11-19 20:36:11 +0000 | [diff] [blame] | 8552 | err = btrfs_add_nondir(trans, dir, dentry, inode, 0, index); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 8553 | if (err) |
| 8554 | drop_inode = 1; |
| 8555 | else { |
| 8556 | inode->i_mapping->a_ops = &btrfs_aops; |
Chris Mason | 0416008 | 2008-03-26 10:28:07 -0400 | [diff] [blame] | 8557 | inode->i_mapping->backing_dev_info = &root->fs_info->bdi; |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 8558 | BTRFS_I(inode)->io_tree.ops = &btrfs_extent_io_ops; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 8559 | } |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 8560 | if (drop_inode) |
| 8561 | goto out_unlock; |
| 8562 | |
| 8563 | path = btrfs_alloc_path(); |
Mark Fasheh | d8926bb | 2011-07-13 10:38:47 -0700 | [diff] [blame] | 8564 | if (!path) { |
| 8565 | err = -ENOMEM; |
| 8566 | drop_inode = 1; |
| 8567 | goto out_unlock; |
| 8568 | } |
Li Zefan | 33345d01 | 2011-04-20 10:31:50 +0800 | [diff] [blame] | 8569 | key.objectid = btrfs_ino(inode); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 8570 | key.offset = 0; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 8571 | btrfs_set_key_type(&key, BTRFS_EXTENT_DATA_KEY); |
| 8572 | datasize = btrfs_file_extent_calc_inline_size(name_len); |
| 8573 | err = btrfs_insert_empty_item(trans, root, path, &key, |
| 8574 | datasize); |
Chris Mason | 54aa1f4 | 2007-06-22 14:16:25 -0400 | [diff] [blame] | 8575 | if (err) { |
| 8576 | drop_inode = 1; |
Julia Lawall | b083916 | 2011-05-14 07:10:51 +0000 | [diff] [blame] | 8577 | btrfs_free_path(path); |
Chris Mason | 54aa1f4 | 2007-06-22 14:16:25 -0400 | [diff] [blame] | 8578 | goto out_unlock; |
| 8579 | } |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 8580 | leaf = path->nodes[0]; |
| 8581 | ei = btrfs_item_ptr(leaf, path->slots[0], |
| 8582 | struct btrfs_file_extent_item); |
| 8583 | btrfs_set_file_extent_generation(leaf, ei, trans->transid); |
| 8584 | btrfs_set_file_extent_type(leaf, ei, |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 8585 | BTRFS_FILE_EXTENT_INLINE); |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 8586 | btrfs_set_file_extent_encryption(leaf, ei, 0); |
| 8587 | btrfs_set_file_extent_compression(leaf, ei, 0); |
| 8588 | btrfs_set_file_extent_other_encoding(leaf, ei, 0); |
| 8589 | btrfs_set_file_extent_ram_bytes(leaf, ei, name_len); |
| 8590 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 8591 | ptr = btrfs_file_extent_inline_start(ei); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 8592 | write_extent_buffer(leaf, symname, ptr, name_len); |
| 8593 | btrfs_mark_buffer_dirty(leaf); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 8594 | btrfs_free_path(path); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 8595 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 8596 | inode->i_op = &btrfs_symlink_inode_operations; |
| 8597 | inode->i_mapping->a_ops = &btrfs_symlink_aops; |
Chris Mason | 0416008 | 2008-03-26 10:28:07 -0400 | [diff] [blame] | 8598 | inode->i_mapping->backing_dev_info = &root->fs_info->bdi; |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 8599 | inode_set_bytes(inode, name_len); |
Filipe David Borba Manana | f06becc | 2013-09-16 09:53:28 +0100 | [diff] [blame] | 8600 | btrfs_i_size_write(inode, name_len); |
Chris Mason | 54aa1f4 | 2007-06-22 14:16:25 -0400 | [diff] [blame] | 8601 | err = btrfs_update_inode(trans, root, inode); |
| 8602 | if (err) |
| 8603 | drop_inode = 1; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 8604 | |
| 8605 | out_unlock: |
Al Viro | 08c422c | 2011-12-23 07:58:13 -0500 | [diff] [blame] | 8606 | if (!err) |
| 8607 | d_instantiate(dentry, inode); |
Josef Bacik | 7ad85bb | 2012-01-12 19:10:12 -0500 | [diff] [blame] | 8608 | btrfs_end_transaction(trans, root); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 8609 | if (drop_inode) { |
| 8610 | inode_dec_link_count(inode); |
| 8611 | iput(inode); |
| 8612 | } |
Liu Bo | b53d3f5 | 2012-11-14 14:34:34 +0000 | [diff] [blame] | 8613 | btrfs_btree_balance_dirty(root); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 8614 | return err; |
| 8615 | } |
Chris Mason | 1643298 | 2008-04-10 10:23:21 -0400 | [diff] [blame] | 8616 | |
Josef Bacik | 0af3d00 | 2010-06-21 14:48:16 -0400 | [diff] [blame] | 8617 | static int __btrfs_prealloc_file_range(struct inode *inode, int mode, |
| 8618 | u64 start, u64 num_bytes, u64 min_size, |
| 8619 | loff_t actual_len, u64 *alloc_hint, |
| 8620 | struct btrfs_trans_handle *trans) |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 8621 | { |
Josef Bacik | 5dc562c | 2012-08-17 13:14:17 -0400 | [diff] [blame] | 8622 | struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree; |
| 8623 | struct extent_map *em; |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 8624 | struct btrfs_root *root = BTRFS_I(inode)->root; |
| 8625 | struct btrfs_key ins; |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 8626 | u64 cur_offset = start; |
Josef Bacik | 55a61d1 | 2010-11-22 18:50:32 +0000 | [diff] [blame] | 8627 | u64 i_size; |
Chris Mason | 154ea28 | 2013-03-05 11:11:26 -0500 | [diff] [blame] | 8628 | u64 cur_bytes; |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 8629 | int ret = 0; |
Josef Bacik | 0af3d00 | 2010-06-21 14:48:16 -0400 | [diff] [blame] | 8630 | bool own_trans = true; |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 8631 | |
Josef Bacik | 0af3d00 | 2010-06-21 14:48:16 -0400 | [diff] [blame] | 8632 | if (trans) |
| 8633 | own_trans = false; |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 8634 | while (num_bytes > 0) { |
Josef Bacik | 0af3d00 | 2010-06-21 14:48:16 -0400 | [diff] [blame] | 8635 | if (own_trans) { |
| 8636 | trans = btrfs_start_transaction(root, 3); |
| 8637 | if (IS_ERR(trans)) { |
| 8638 | ret = PTR_ERR(trans); |
| 8639 | break; |
| 8640 | } |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 8641 | } |
Yan, Zheng | 5a303d5 | 2009-11-12 09:34:52 +0000 | [diff] [blame] | 8642 | |
Chris Mason | 154ea28 | 2013-03-05 11:11:26 -0500 | [diff] [blame] | 8643 | cur_bytes = min(num_bytes, 256ULL * 1024 * 1024); |
| 8644 | cur_bytes = max(cur_bytes, min_size); |
Josef Bacik | 0036158 | 2013-08-14 14:02:47 -0400 | [diff] [blame] | 8645 | ret = btrfs_reserve_extent(root, cur_bytes, min_size, 0, |
| 8646 | *alloc_hint, &ins, 1); |
Yan, Zheng | 5a303d5 | 2009-11-12 09:34:52 +0000 | [diff] [blame] | 8647 | if (ret) { |
Josef Bacik | 0af3d00 | 2010-06-21 14:48:16 -0400 | [diff] [blame] | 8648 | if (own_trans) |
| 8649 | btrfs_end_transaction(trans, root); |
Yan, Zheng | a22285a | 2010-05-16 10:48:46 -0400 | [diff] [blame] | 8650 | break; |
Yan, Zheng | 5a303d5 | 2009-11-12 09:34:52 +0000 | [diff] [blame] | 8651 | } |
| 8652 | |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 8653 | ret = insert_reserved_file_extent(trans, inode, |
| 8654 | cur_offset, ins.objectid, |
| 8655 | ins.offset, ins.offset, |
Yan, Zheng | 920bbbf | 2009-11-12 09:34:08 +0000 | [diff] [blame] | 8656 | ins.offset, 0, 0, 0, |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 8657 | BTRFS_FILE_EXTENT_PREALLOC); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 8658 | if (ret) { |
Josef Bacik | 857cc2f | 2013-10-07 15:21:08 -0400 | [diff] [blame] | 8659 | btrfs_free_reserved_extent(root, ins.objectid, |
| 8660 | ins.offset); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 8661 | btrfs_abort_transaction(trans, root, ret); |
| 8662 | if (own_trans) |
| 8663 | btrfs_end_transaction(trans, root); |
| 8664 | break; |
| 8665 | } |
Chris Mason | a1ed835 | 2009-09-11 12:27:37 -0400 | [diff] [blame] | 8666 | btrfs_drop_extent_cache(inode, cur_offset, |
| 8667 | cur_offset + ins.offset -1, 0); |
Yan, Zheng | 5a303d5 | 2009-11-12 09:34:52 +0000 | [diff] [blame] | 8668 | |
Josef Bacik | 5dc562c | 2012-08-17 13:14:17 -0400 | [diff] [blame] | 8669 | em = alloc_extent_map(); |
| 8670 | if (!em) { |
| 8671 | set_bit(BTRFS_INODE_NEEDS_FULL_SYNC, |
| 8672 | &BTRFS_I(inode)->runtime_flags); |
| 8673 | goto next; |
| 8674 | } |
| 8675 | |
| 8676 | em->start = cur_offset; |
| 8677 | em->orig_start = cur_offset; |
| 8678 | em->len = ins.offset; |
| 8679 | em->block_start = ins.objectid; |
| 8680 | em->block_len = ins.offset; |
Josef Bacik | b493968 | 2012-12-03 10:31:19 -0500 | [diff] [blame] | 8681 | em->orig_block_len = ins.offset; |
Josef Bacik | cc95bef | 2013-04-04 14:31:27 -0400 | [diff] [blame] | 8682 | em->ram_bytes = ins.offset; |
Josef Bacik | 5dc562c | 2012-08-17 13:14:17 -0400 | [diff] [blame] | 8683 | em->bdev = root->fs_info->fs_devices->latest_bdev; |
| 8684 | set_bit(EXTENT_FLAG_PREALLOC, &em->flags); |
| 8685 | em->generation = trans->transid; |
| 8686 | |
| 8687 | while (1) { |
| 8688 | write_lock(&em_tree->lock); |
Josef Bacik | 09a2a8f9 | 2013-04-05 16:51:15 -0400 | [diff] [blame] | 8689 | ret = add_extent_mapping(em_tree, em, 1); |
Josef Bacik | 5dc562c | 2012-08-17 13:14:17 -0400 | [diff] [blame] | 8690 | write_unlock(&em_tree->lock); |
| 8691 | if (ret != -EEXIST) |
| 8692 | break; |
| 8693 | btrfs_drop_extent_cache(inode, cur_offset, |
| 8694 | cur_offset + ins.offset - 1, |
| 8695 | 0); |
| 8696 | } |
| 8697 | free_extent_map(em); |
| 8698 | next: |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 8699 | num_bytes -= ins.offset; |
| 8700 | cur_offset += ins.offset; |
Yan, Zheng | efa5646 | 2010-05-16 10:49:59 -0400 | [diff] [blame] | 8701 | *alloc_hint = ins.objectid + ins.offset; |
Yan, Zheng | 5a303d5 | 2009-11-12 09:34:52 +0000 | [diff] [blame] | 8702 | |
Josef Bacik | 0c4d2d9 | 2012-04-05 15:03:02 -0400 | [diff] [blame] | 8703 | inode_inc_iversion(inode); |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 8704 | inode->i_ctime = CURRENT_TIME; |
Christoph Hellwig | 6cbff00 | 2009-04-17 10:37:41 +0200 | [diff] [blame] | 8705 | BTRFS_I(inode)->flags |= BTRFS_INODE_PREALLOC; |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 8706 | if (!(mode & FALLOC_FL_KEEP_SIZE) && |
Yan, Zheng | efa5646 | 2010-05-16 10:49:59 -0400 | [diff] [blame] | 8707 | (actual_len > inode->i_size) && |
| 8708 | (cur_offset > inode->i_size)) { |
Aneesh Kumar K.V | d1ea6a6 | 2010-01-20 07:28:54 +0000 | [diff] [blame] | 8709 | if (cur_offset > actual_len) |
Josef Bacik | 55a61d1 | 2010-11-22 18:50:32 +0000 | [diff] [blame] | 8710 | i_size = actual_len; |
Aneesh Kumar K.V | d1ea6a6 | 2010-01-20 07:28:54 +0000 | [diff] [blame] | 8711 | else |
Josef Bacik | 55a61d1 | 2010-11-22 18:50:32 +0000 | [diff] [blame] | 8712 | i_size = cur_offset; |
| 8713 | i_size_write(inode, i_size); |
| 8714 | btrfs_ordered_update_i_size(inode, i_size, NULL); |
Yan, Zheng | 5a303d5 | 2009-11-12 09:34:52 +0000 | [diff] [blame] | 8715 | } |
| 8716 | |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 8717 | ret = btrfs_update_inode(trans, root, inode); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 8718 | |
| 8719 | if (ret) { |
| 8720 | btrfs_abort_transaction(trans, root, ret); |
| 8721 | if (own_trans) |
| 8722 | btrfs_end_transaction(trans, root); |
| 8723 | break; |
| 8724 | } |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 8725 | |
Josef Bacik | 0af3d00 | 2010-06-21 14:48:16 -0400 | [diff] [blame] | 8726 | if (own_trans) |
| 8727 | btrfs_end_transaction(trans, root); |
Yan, Zheng | 5a303d5 | 2009-11-12 09:34:52 +0000 | [diff] [blame] | 8728 | } |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 8729 | return ret; |
| 8730 | } |
| 8731 | |
Josef Bacik | 0af3d00 | 2010-06-21 14:48:16 -0400 | [diff] [blame] | 8732 | int btrfs_prealloc_file_range(struct inode *inode, int mode, |
| 8733 | u64 start, u64 num_bytes, u64 min_size, |
| 8734 | loff_t actual_len, u64 *alloc_hint) |
| 8735 | { |
| 8736 | return __btrfs_prealloc_file_range(inode, mode, start, num_bytes, |
| 8737 | min_size, actual_len, alloc_hint, |
| 8738 | NULL); |
| 8739 | } |
| 8740 | |
| 8741 | int btrfs_prealloc_file_range_trans(struct inode *inode, |
| 8742 | struct btrfs_trans_handle *trans, int mode, |
| 8743 | u64 start, u64 num_bytes, u64 min_size, |
| 8744 | loff_t actual_len, u64 *alloc_hint) |
| 8745 | { |
| 8746 | return __btrfs_prealloc_file_range(inode, mode, start, num_bytes, |
| 8747 | min_size, actual_len, alloc_hint, trans); |
| 8748 | } |
| 8749 | |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 8750 | static int btrfs_set_page_dirty(struct page *page) |
| 8751 | { |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 8752 | return __set_page_dirty_nobuffers(page); |
| 8753 | } |
| 8754 | |
Al Viro | 10556cb2 | 2011-06-20 19:28:19 -0400 | [diff] [blame] | 8755 | static int btrfs_permission(struct inode *inode, int mask) |
Yan | fdebe2b | 2008-01-14 13:26:08 -0500 | [diff] [blame] | 8756 | { |
Li Zefan | b83cc96 | 2010-12-20 16:04:08 +0800 | [diff] [blame] | 8757 | struct btrfs_root *root = BTRFS_I(inode)->root; |
Jeff Mahoney | cb6db4e | 2011-08-15 17:27:21 +0000 | [diff] [blame] | 8758 | umode_t mode = inode->i_mode; |
Li Zefan | b83cc96 | 2010-12-20 16:04:08 +0800 | [diff] [blame] | 8759 | |
Jeff Mahoney | cb6db4e | 2011-08-15 17:27:21 +0000 | [diff] [blame] | 8760 | if (mask & MAY_WRITE && |
| 8761 | (S_ISREG(mode) || S_ISDIR(mode) || S_ISLNK(mode))) { |
| 8762 | if (btrfs_root_readonly(root)) |
| 8763 | return -EROFS; |
| 8764 | if (BTRFS_I(inode)->flags & BTRFS_INODE_READONLY) |
| 8765 | return -EACCES; |
| 8766 | } |
Al Viro | 2830ba7 | 2011-06-20 19:16:29 -0400 | [diff] [blame] | 8767 | return generic_permission(inode, mask); |
Yan | fdebe2b | 2008-01-14 13:26:08 -0500 | [diff] [blame] | 8768 | } |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 8769 | |
Alexey Dobriyan | 6e1d5dc | 2009-09-21 17:01:11 -0700 | [diff] [blame] | 8770 | static const struct inode_operations btrfs_dir_inode_operations = { |
Chris Mason | 3394e16 | 2008-11-17 20:42:26 -0500 | [diff] [blame] | 8771 | .getattr = btrfs_getattr, |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 8772 | .lookup = btrfs_lookup, |
| 8773 | .create = btrfs_create, |
| 8774 | .unlink = btrfs_unlink, |
| 8775 | .link = btrfs_link, |
| 8776 | .mkdir = btrfs_mkdir, |
| 8777 | .rmdir = btrfs_rmdir, |
| 8778 | .rename = btrfs_rename, |
| 8779 | .symlink = btrfs_symlink, |
| 8780 | .setattr = btrfs_setattr, |
Josef Bacik | 618e21d | 2007-07-11 10:18:17 -0400 | [diff] [blame] | 8781 | .mknod = btrfs_mknod, |
Christoph Hellwig | 95819c0 | 2008-08-28 06:21:17 -0400 | [diff] [blame] | 8782 | .setxattr = btrfs_setxattr, |
| 8783 | .getxattr = btrfs_getxattr, |
Josef Bacik | 5103e94 | 2007-11-16 11:45:54 -0500 | [diff] [blame] | 8784 | .listxattr = btrfs_listxattr, |
Christoph Hellwig | 95819c0 | 2008-08-28 06:21:17 -0400 | [diff] [blame] | 8785 | .removexattr = btrfs_removexattr, |
Yan | fdebe2b | 2008-01-14 13:26:08 -0500 | [diff] [blame] | 8786 | .permission = btrfs_permission, |
Christoph Hellwig | 4e34e71 | 2011-07-23 17:37:31 +0200 | [diff] [blame] | 8787 | .get_acl = btrfs_get_acl, |
Guangyu Sun | 93fd63c | 2013-09-16 10:42:03 -0700 | [diff] [blame] | 8788 | .update_time = btrfs_update_time, |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 8789 | }; |
Alexey Dobriyan | 6e1d5dc | 2009-09-21 17:01:11 -0700 | [diff] [blame] | 8790 | static const struct inode_operations btrfs_dir_ro_inode_operations = { |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 8791 | .lookup = btrfs_lookup, |
Yan | fdebe2b | 2008-01-14 13:26:08 -0500 | [diff] [blame] | 8792 | .permission = btrfs_permission, |
Christoph Hellwig | 4e34e71 | 2011-07-23 17:37:31 +0200 | [diff] [blame] | 8793 | .get_acl = btrfs_get_acl, |
Guangyu Sun | 93fd63c | 2013-09-16 10:42:03 -0700 | [diff] [blame] | 8794 | .update_time = btrfs_update_time, |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 8795 | }; |
Yan, Zheng | 76dda93 | 2009-09-21 16:00:26 -0400 | [diff] [blame] | 8796 | |
Alexey Dobriyan | 828c095 | 2009-10-01 15:43:56 -0700 | [diff] [blame] | 8797 | static const struct file_operations btrfs_dir_file_operations = { |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 8798 | .llseek = generic_file_llseek, |
| 8799 | .read = generic_read_dir, |
Al Viro | 9cdda8d | 2013-05-22 16:48:09 -0400 | [diff] [blame] | 8800 | .iterate = btrfs_real_readdir, |
Christoph Hellwig | 34287aa | 2007-09-14 10:22:47 -0400 | [diff] [blame] | 8801 | .unlocked_ioctl = btrfs_ioctl, |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 8802 | #ifdef CONFIG_COMPAT |
Christoph Hellwig | 34287aa | 2007-09-14 10:22:47 -0400 | [diff] [blame] | 8803 | .compat_ioctl = btrfs_ioctl, |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 8804 | #endif |
Sage Weil | 6bf13c0 | 2008-06-10 10:07:39 -0400 | [diff] [blame] | 8805 | .release = btrfs_release_file, |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 8806 | .fsync = btrfs_sync_file, |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 8807 | }; |
| 8808 | |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 8809 | static struct extent_io_ops btrfs_extent_io_ops = { |
Chris Mason | 07157aa | 2007-08-30 08:50:51 -0400 | [diff] [blame] | 8810 | .fill_delalloc = run_delalloc_range, |
Chris Mason | 065631f | 2008-02-20 12:07:25 -0500 | [diff] [blame] | 8811 | .submit_bio_hook = btrfs_submit_bio_hook, |
Chris Mason | 239b14b | 2008-03-24 15:02:07 -0400 | [diff] [blame] | 8812 | .merge_bio_hook = btrfs_merge_bio_hook, |
Chris Mason | 07157aa | 2007-08-30 08:50:51 -0400 | [diff] [blame] | 8813 | .readpage_end_io_hook = btrfs_readpage_end_io_hook, |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 8814 | .writepage_end_io_hook = btrfs_writepage_end_io_hook, |
Chris Mason | 247e743 | 2008-07-17 12:53:51 -0400 | [diff] [blame] | 8815 | .writepage_start_hook = btrfs_writepage_start_hook, |
Chris Mason | b0c68f8 | 2008-01-31 11:05:37 -0500 | [diff] [blame] | 8816 | .set_bit_hook = btrfs_set_bit_hook, |
| 8817 | .clear_bit_hook = btrfs_clear_bit_hook, |
Josef Bacik | 9ed74f2 | 2009-09-11 16:12:44 -0400 | [diff] [blame] | 8818 | .merge_extent_hook = btrfs_merge_extent_hook, |
| 8819 | .split_extent_hook = btrfs_split_extent_hook, |
Chris Mason | 07157aa | 2007-08-30 08:50:51 -0400 | [diff] [blame] | 8820 | }; |
| 8821 | |
Chris Mason | 3505439 | 2009-01-21 13:11:13 -0500 | [diff] [blame] | 8822 | /* |
| 8823 | * btrfs doesn't support the bmap operation because swapfiles |
| 8824 | * use bmap to make a mapping of extents in the file. They assume |
| 8825 | * these extents won't change over the life of the file and they |
| 8826 | * use the bmap result to do IO directly to the drive. |
| 8827 | * |
| 8828 | * the btrfs bmap call would return logical addresses that aren't |
| 8829 | * suitable for IO and they also will change frequently as COW |
| 8830 | * operations happen. So, swapfile + btrfs == corruption. |
| 8831 | * |
| 8832 | * For now we're avoiding this by dropping bmap. |
| 8833 | */ |
Alexey Dobriyan | 7f09410 | 2009-09-21 17:01:10 -0700 | [diff] [blame] | 8834 | static const struct address_space_operations btrfs_aops = { |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 8835 | .readpage = btrfs_readpage, |
| 8836 | .writepage = btrfs_writepage, |
Chris Mason | b293f02e | 2007-11-01 19:45:34 -0400 | [diff] [blame] | 8837 | .writepages = btrfs_writepages, |
Chris Mason | 3ab2fb5 | 2007-11-08 10:59:22 -0500 | [diff] [blame] | 8838 | .readpages = btrfs_readpages, |
Chris Mason | 1643298 | 2008-04-10 10:23:21 -0400 | [diff] [blame] | 8839 | .direct_IO = btrfs_direct_IO, |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 8840 | .invalidatepage = btrfs_invalidatepage, |
| 8841 | .releasepage = btrfs_releasepage, |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 8842 | .set_page_dirty = btrfs_set_page_dirty, |
Andi Kleen | 465fdd9 | 2009-09-16 11:50:18 +0200 | [diff] [blame] | 8843 | .error_remove_page = generic_error_remove_page, |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 8844 | }; |
| 8845 | |
Alexey Dobriyan | 7f09410 | 2009-09-21 17:01:10 -0700 | [diff] [blame] | 8846 | static const struct address_space_operations btrfs_symlink_aops = { |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 8847 | .readpage = btrfs_readpage, |
| 8848 | .writepage = btrfs_writepage, |
Chris Mason | 2bf5a72 | 2007-08-30 11:54:02 -0400 | [diff] [blame] | 8849 | .invalidatepage = btrfs_invalidatepage, |
| 8850 | .releasepage = btrfs_releasepage, |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 8851 | }; |
| 8852 | |
Alexey Dobriyan | 6e1d5dc | 2009-09-21 17:01:11 -0700 | [diff] [blame] | 8853 | static const struct inode_operations btrfs_file_inode_operations = { |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 8854 | .getattr = btrfs_getattr, |
| 8855 | .setattr = btrfs_setattr, |
Christoph Hellwig | 95819c0 | 2008-08-28 06:21:17 -0400 | [diff] [blame] | 8856 | .setxattr = btrfs_setxattr, |
| 8857 | .getxattr = btrfs_getxattr, |
Josef Bacik | 5103e94 | 2007-11-16 11:45:54 -0500 | [diff] [blame] | 8858 | .listxattr = btrfs_listxattr, |
Christoph Hellwig | 95819c0 | 2008-08-28 06:21:17 -0400 | [diff] [blame] | 8859 | .removexattr = btrfs_removexattr, |
Yan | fdebe2b | 2008-01-14 13:26:08 -0500 | [diff] [blame] | 8860 | .permission = btrfs_permission, |
Yehuda Sadeh | 1506fcc | 2009-01-21 14:39:14 -0500 | [diff] [blame] | 8861 | .fiemap = btrfs_fiemap, |
Christoph Hellwig | 4e34e71 | 2011-07-23 17:37:31 +0200 | [diff] [blame] | 8862 | .get_acl = btrfs_get_acl, |
Josef Bacik | e41f941 | 2012-03-26 09:46:47 -0400 | [diff] [blame] | 8863 | .update_time = btrfs_update_time, |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 8864 | }; |
Alexey Dobriyan | 6e1d5dc | 2009-09-21 17:01:11 -0700 | [diff] [blame] | 8865 | static const struct inode_operations btrfs_special_inode_operations = { |
Josef Bacik | 618e21d | 2007-07-11 10:18:17 -0400 | [diff] [blame] | 8866 | .getattr = btrfs_getattr, |
| 8867 | .setattr = btrfs_setattr, |
Yan | fdebe2b | 2008-01-14 13:26:08 -0500 | [diff] [blame] | 8868 | .permission = btrfs_permission, |
Christoph Hellwig | 95819c0 | 2008-08-28 06:21:17 -0400 | [diff] [blame] | 8869 | .setxattr = btrfs_setxattr, |
| 8870 | .getxattr = btrfs_getxattr, |
Josef Bacik | 33268ea | 2008-07-24 12:16:36 -0400 | [diff] [blame] | 8871 | .listxattr = btrfs_listxattr, |
Christoph Hellwig | 95819c0 | 2008-08-28 06:21:17 -0400 | [diff] [blame] | 8872 | .removexattr = btrfs_removexattr, |
Christoph Hellwig | 4e34e71 | 2011-07-23 17:37:31 +0200 | [diff] [blame] | 8873 | .get_acl = btrfs_get_acl, |
Josef Bacik | e41f941 | 2012-03-26 09:46:47 -0400 | [diff] [blame] | 8874 | .update_time = btrfs_update_time, |
Josef Bacik | 618e21d | 2007-07-11 10:18:17 -0400 | [diff] [blame] | 8875 | }; |
Alexey Dobriyan | 6e1d5dc | 2009-09-21 17:01:11 -0700 | [diff] [blame] | 8876 | static const struct inode_operations btrfs_symlink_inode_operations = { |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 8877 | .readlink = generic_readlink, |
| 8878 | .follow_link = page_follow_link_light, |
| 8879 | .put_link = page_put_link, |
Li Zefan | f209561 | 2010-11-19 02:05:24 +0000 | [diff] [blame] | 8880 | .getattr = btrfs_getattr, |
Josef Bacik | 22c44fe | 2011-11-30 10:45:38 -0500 | [diff] [blame] | 8881 | .setattr = btrfs_setattr, |
Yan | fdebe2b | 2008-01-14 13:26:08 -0500 | [diff] [blame] | 8882 | .permission = btrfs_permission, |
Jim Owens | 0279b4c | 2009-02-04 09:29:13 -0500 | [diff] [blame] | 8883 | .setxattr = btrfs_setxattr, |
| 8884 | .getxattr = btrfs_getxattr, |
| 8885 | .listxattr = btrfs_listxattr, |
| 8886 | .removexattr = btrfs_removexattr, |
Christoph Hellwig | 4e34e71 | 2011-07-23 17:37:31 +0200 | [diff] [blame] | 8887 | .get_acl = btrfs_get_acl, |
Josef Bacik | e41f941 | 2012-03-26 09:46:47 -0400 | [diff] [blame] | 8888 | .update_time = btrfs_update_time, |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 8889 | }; |
Yan, Zheng | 76dda93 | 2009-09-21 16:00:26 -0400 | [diff] [blame] | 8890 | |
Alexey Dobriyan | 82d339d | 2009-10-09 09:54:36 -0400 | [diff] [blame] | 8891 | const struct dentry_operations btrfs_dentry_operations = { |
Yan, Zheng | 76dda93 | 2009-09-21 16:00:26 -0400 | [diff] [blame] | 8892 | .d_delete = btrfs_dentry_delete, |
Josef Bacik | b4aff1f | 2011-06-28 16:18:59 -0400 | [diff] [blame] | 8893 | .d_release = btrfs_dentry_release, |
Yan, Zheng | 76dda93 | 2009-09-21 16:00:26 -0400 | [diff] [blame] | 8894 | }; |