Chris Mason | 6cbd557 | 2007-06-12 09:07:21 -0400 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2007 Oracle. All rights reserved. |
| 3 | * |
| 4 | * This program is free software; you can redistribute it and/or |
| 5 | * modify it under the terms of the GNU General Public |
| 6 | * License v2 as published by the Free Software Foundation. |
| 7 | * |
| 8 | * This program is distributed in the hope that it will be useful, |
| 9 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 10 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 11 | * General Public License for more details. |
| 12 | * |
| 13 | * You should have received a copy of the GNU General Public |
| 14 | * License along with this program; if not, write to the |
| 15 | * Free Software Foundation, Inc., 59 Temple Place - Suite 330, |
| 16 | * Boston, MA 021110-1307, USA. |
| 17 | */ |
| 18 | |
Chris Mason | 8f18cf1 | 2008-04-25 16:53:30 -0400 | [diff] [blame] | 19 | #include <linux/kernel.h> |
Chris Mason | 065631f | 2008-02-20 12:07:25 -0500 | [diff] [blame] | 20 | #include <linux/bio.h> |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 21 | #include <linux/buffer_head.h> |
Sage Weil | f2eb0a2 | 2008-05-02 14:43:14 -0400 | [diff] [blame] | 22 | #include <linux/file.h> |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 23 | #include <linux/fs.h> |
| 24 | #include <linux/pagemap.h> |
| 25 | #include <linux/highmem.h> |
| 26 | #include <linux/time.h> |
| 27 | #include <linux/init.h> |
| 28 | #include <linux/string.h> |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 29 | #include <linux/backing-dev.h> |
| 30 | #include <linux/mpage.h> |
| 31 | #include <linux/swap.h> |
| 32 | #include <linux/writeback.h> |
| 33 | #include <linux/statfs.h> |
| 34 | #include <linux/compat.h> |
Kent Overstreet | a27bb33 | 2013-05-07 16:19:08 -0700 | [diff] [blame] | 35 | #include <linux/aio.h> |
Chris Mason | 9ebefb18 | 2007-06-15 13:50:00 -0400 | [diff] [blame] | 36 | #include <linux/bit_spinlock.h> |
Josef Bacik | 5103e94 | 2007-11-16 11:45:54 -0500 | [diff] [blame] | 37 | #include <linux/xattr.h> |
Josef Bacik | 33268ea | 2008-07-24 12:16:36 -0400 | [diff] [blame] | 38 | #include <linux/posix_acl.h> |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 39 | #include <linux/falloc.h> |
Tejun Heo | 5a0e3ad | 2010-03-24 17:04:11 +0900 | [diff] [blame] | 40 | #include <linux/slab.h> |
David Sterba | 7a36dde | 2011-05-06 15:33:15 +0200 | [diff] [blame] | 41 | #include <linux/ratelimit.h> |
Josef Bacik | 22c44fe | 2011-11-30 10:45:38 -0500 | [diff] [blame] | 42 | #include <linux/mount.h> |
Filipe Brandenburger | 55e301f | 2013-01-29 06:04:50 +0000 | [diff] [blame] | 43 | #include <linux/btrfs.h> |
David Woodhouse | 53b381b | 2013-01-29 18:40:14 -0500 | [diff] [blame] | 44 | #include <linux/blkdev.h> |
Josef Bacik | f23b5a5 | 2013-06-19 10:16:26 -0400 | [diff] [blame] | 45 | #include <linux/posix_acl_xattr.h> |
Chris Mason | 4b4e25f | 2008-11-20 10:22:27 -0500 | [diff] [blame] | 46 | #include "compat.h" |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 47 | #include "ctree.h" |
| 48 | #include "disk-io.h" |
| 49 | #include "transaction.h" |
| 50 | #include "btrfs_inode.h" |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 51 | #include "print-tree.h" |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 52 | #include "ordered-data.h" |
Christoph Hellwig | 95819c0 | 2008-08-28 06:21:17 -0400 | [diff] [blame] | 53 | #include "xattr.h" |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 54 | #include "tree-log.h" |
Jan Schmidt | 4a54c8c | 2011-07-22 15:41:52 +0200 | [diff] [blame] | 55 | #include "volumes.h" |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 56 | #include "compression.h" |
Chris Mason | b4ce94d | 2009-02-04 09:25:08 -0500 | [diff] [blame] | 57 | #include "locking.h" |
Josef Bacik | dc89e98 | 2011-01-28 17:05:48 -0500 | [diff] [blame] | 58 | #include "free-space-cache.h" |
Li Zefan | 581bb05 | 2011-04-20 10:06:11 +0800 | [diff] [blame] | 59 | #include "inode-map.h" |
Liu Bo | 38c227d | 2013-01-29 03:18:40 +0000 | [diff] [blame] | 60 | #include "backref.h" |
Josef Bacik | f23b5a5 | 2013-06-19 10:16:26 -0400 | [diff] [blame] | 61 | #include "hash.h" |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 62 | |
| 63 | struct btrfs_iget_args { |
| 64 | u64 ino; |
| 65 | struct btrfs_root *root; |
| 66 | }; |
| 67 | |
Alexey Dobriyan | 6e1d5dc | 2009-09-21 17:01:11 -0700 | [diff] [blame] | 68 | static const struct inode_operations btrfs_dir_inode_operations; |
| 69 | static const struct inode_operations btrfs_symlink_inode_operations; |
| 70 | static const struct inode_operations btrfs_dir_ro_inode_operations; |
| 71 | static const struct inode_operations btrfs_special_inode_operations; |
| 72 | static const struct inode_operations btrfs_file_inode_operations; |
Alexey Dobriyan | 7f09410 | 2009-09-21 17:01:10 -0700 | [diff] [blame] | 73 | static const struct address_space_operations btrfs_aops; |
| 74 | static const struct address_space_operations btrfs_symlink_aops; |
Alexey Dobriyan | 828c095 | 2009-10-01 15:43:56 -0700 | [diff] [blame] | 75 | static const struct file_operations btrfs_dir_file_operations; |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 76 | static struct extent_io_ops btrfs_extent_io_ops; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 77 | |
| 78 | static struct kmem_cache *btrfs_inode_cachep; |
Miao Xie | 8ccf6f19 | 2012-10-25 09:28:04 +0000 | [diff] [blame] | 79 | static struct kmem_cache *btrfs_delalloc_work_cachep; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 80 | struct kmem_cache *btrfs_trans_handle_cachep; |
| 81 | struct kmem_cache *btrfs_transaction_cachep; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 82 | struct kmem_cache *btrfs_path_cachep; |
Josef Bacik | dc89e98 | 2011-01-28 17:05:48 -0500 | [diff] [blame] | 83 | struct kmem_cache *btrfs_free_space_cachep; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 84 | |
| 85 | #define S_SHIFT 12 |
| 86 | static unsigned char btrfs_type_by_mode[S_IFMT >> S_SHIFT] = { |
| 87 | [S_IFREG >> S_SHIFT] = BTRFS_FT_REG_FILE, |
| 88 | [S_IFDIR >> S_SHIFT] = BTRFS_FT_DIR, |
| 89 | [S_IFCHR >> S_SHIFT] = BTRFS_FT_CHRDEV, |
| 90 | [S_IFBLK >> S_SHIFT] = BTRFS_FT_BLKDEV, |
| 91 | [S_IFIFO >> S_SHIFT] = BTRFS_FT_FIFO, |
| 92 | [S_IFSOCK >> S_SHIFT] = BTRFS_FT_SOCK, |
| 93 | [S_IFLNK >> S_SHIFT] = BTRFS_FT_SYMLINK, |
| 94 | }; |
| 95 | |
Eric Sandeen | 3972f26 | 2013-01-12 02:57:22 +0000 | [diff] [blame] | 96 | static int btrfs_setsize(struct inode *inode, struct iattr *attr); |
Josef Bacik | a41ad39 | 2011-01-31 15:30:16 -0500 | [diff] [blame] | 97 | static int btrfs_truncate(struct inode *inode); |
Josef Bacik | 5fd0204 | 2012-05-02 14:00:54 -0400 | [diff] [blame] | 98 | static int btrfs_finish_ordered_io(struct btrfs_ordered_extent *ordered_extent); |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 99 | static noinline int cow_file_range(struct inode *inode, |
| 100 | struct page *locked_page, |
| 101 | u64 start, u64 end, int *page_started, |
| 102 | unsigned long *nr_written, int unlock); |
Josef Bacik | 70c8a91 | 2012-10-11 16:54:30 -0400 | [diff] [blame] | 103 | static struct extent_map *create_pinned_em(struct inode *inode, u64 start, |
| 104 | u64 len, u64 orig_start, |
| 105 | u64 block_start, u64 block_len, |
Josef Bacik | cc95bef | 2013-04-04 14:31:27 -0400 | [diff] [blame] | 106 | u64 orig_block_len, u64 ram_bytes, |
| 107 | int type); |
Josef Bacik | 7b12876 | 2008-07-24 12:17:14 -0400 | [diff] [blame] | 108 | |
Eric Sandeen | 48a3b63 | 2013-04-25 20:41:01 +0000 | [diff] [blame] | 109 | static int btrfs_dirty_inode(struct inode *inode); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 110 | |
Yan, Zheng | f34f57a | 2009-11-12 09:35:27 +0000 | [diff] [blame] | 111 | static int btrfs_init_inode_security(struct btrfs_trans_handle *trans, |
Eric Paris | 2a7dba3 | 2011-02-01 11:05:39 -0500 | [diff] [blame] | 112 | struct inode *inode, struct inode *dir, |
| 113 | const struct qstr *qstr) |
Jim Owens | 0279b4c | 2009-02-04 09:29:13 -0500 | [diff] [blame] | 114 | { |
| 115 | int err; |
| 116 | |
Yan, Zheng | f34f57a | 2009-11-12 09:35:27 +0000 | [diff] [blame] | 117 | err = btrfs_init_acl(trans, inode, dir); |
Jim Owens | 0279b4c | 2009-02-04 09:29:13 -0500 | [diff] [blame] | 118 | if (!err) |
Eric Paris | 2a7dba3 | 2011-02-01 11:05:39 -0500 | [diff] [blame] | 119 | err = btrfs_xattr_security_init(trans, inode, dir, qstr); |
Jim Owens | 0279b4c | 2009-02-04 09:29:13 -0500 | [diff] [blame] | 120 | return err; |
| 121 | } |
| 122 | |
Chris Mason | d352ac6 | 2008-09-29 15:18:18 -0400 | [diff] [blame] | 123 | /* |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 124 | * this does all the hard work for inserting an inline extent into |
| 125 | * the btree. The caller should have done a btrfs_drop_extents so that |
| 126 | * no overlapping inline items exist in the btree |
| 127 | */ |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 128 | static noinline int insert_inline_extent(struct btrfs_trans_handle *trans, |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 129 | struct btrfs_root *root, struct inode *inode, |
| 130 | u64 start, size_t size, size_t compressed_size, |
Li Zefan | fe3f566 | 2011-03-28 08:30:38 +0000 | [diff] [blame] | 131 | int compress_type, |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 132 | struct page **compressed_pages) |
| 133 | { |
| 134 | struct btrfs_key key; |
| 135 | struct btrfs_path *path; |
| 136 | struct extent_buffer *leaf; |
| 137 | struct page *page = NULL; |
| 138 | char *kaddr; |
| 139 | unsigned long ptr; |
| 140 | struct btrfs_file_extent_item *ei; |
| 141 | int err = 0; |
| 142 | int ret; |
| 143 | size_t cur_size = size; |
| 144 | size_t datasize; |
| 145 | unsigned long offset; |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 146 | |
Li Zefan | fe3f566 | 2011-03-28 08:30:38 +0000 | [diff] [blame] | 147 | if (compressed_size && compressed_pages) |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 148 | cur_size = compressed_size; |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 149 | |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 150 | path = btrfs_alloc_path(); |
| 151 | if (!path) |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 152 | return -ENOMEM; |
| 153 | |
Chris Mason | b947343 | 2009-03-13 11:00:37 -0400 | [diff] [blame] | 154 | path->leave_spinning = 1; |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 155 | |
Li Zefan | 33345d01 | 2011-04-20 10:31:50 +0800 | [diff] [blame] | 156 | key.objectid = btrfs_ino(inode); |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 157 | key.offset = start; |
| 158 | btrfs_set_key_type(&key, BTRFS_EXTENT_DATA_KEY); |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 159 | datasize = btrfs_file_extent_calc_inline_size(cur_size); |
| 160 | |
| 161 | inode_add_bytes(inode, size); |
| 162 | ret = btrfs_insert_empty_item(trans, root, path, &key, |
| 163 | datasize); |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 164 | if (ret) { |
| 165 | err = ret; |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 166 | goto fail; |
| 167 | } |
| 168 | leaf = path->nodes[0]; |
| 169 | ei = btrfs_item_ptr(leaf, path->slots[0], |
| 170 | struct btrfs_file_extent_item); |
| 171 | btrfs_set_file_extent_generation(leaf, ei, trans->transid); |
| 172 | btrfs_set_file_extent_type(leaf, ei, BTRFS_FILE_EXTENT_INLINE); |
| 173 | btrfs_set_file_extent_encryption(leaf, ei, 0); |
| 174 | btrfs_set_file_extent_other_encoding(leaf, ei, 0); |
| 175 | btrfs_set_file_extent_ram_bytes(leaf, ei, size); |
| 176 | ptr = btrfs_file_extent_inline_start(ei); |
| 177 | |
Li Zefan | 261507a0 | 2010-12-17 14:21:50 +0800 | [diff] [blame] | 178 | if (compress_type != BTRFS_COMPRESS_NONE) { |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 179 | struct page *cpage; |
| 180 | int i = 0; |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 181 | while (compressed_size > 0) { |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 182 | cpage = compressed_pages[i]; |
Chris Mason | 5b050f0 | 2008-11-11 09:34:41 -0500 | [diff] [blame] | 183 | cur_size = min_t(unsigned long, compressed_size, |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 184 | PAGE_CACHE_SIZE); |
| 185 | |
Cong Wang | 7ac687d | 2011-11-25 23:14:28 +0800 | [diff] [blame] | 186 | kaddr = kmap_atomic(cpage); |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 187 | write_extent_buffer(leaf, kaddr, ptr, cur_size); |
Cong Wang | 7ac687d | 2011-11-25 23:14:28 +0800 | [diff] [blame] | 188 | kunmap_atomic(kaddr); |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 189 | |
| 190 | i++; |
| 191 | ptr += cur_size; |
| 192 | compressed_size -= cur_size; |
| 193 | } |
| 194 | btrfs_set_file_extent_compression(leaf, ei, |
Li Zefan | 261507a0 | 2010-12-17 14:21:50 +0800 | [diff] [blame] | 195 | compress_type); |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 196 | } else { |
| 197 | page = find_get_page(inode->i_mapping, |
| 198 | start >> PAGE_CACHE_SHIFT); |
| 199 | btrfs_set_file_extent_compression(leaf, ei, 0); |
Cong Wang | 7ac687d | 2011-11-25 23:14:28 +0800 | [diff] [blame] | 200 | kaddr = kmap_atomic(page); |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 201 | offset = start & (PAGE_CACHE_SIZE - 1); |
| 202 | write_extent_buffer(leaf, kaddr + offset, ptr, size); |
Cong Wang | 7ac687d | 2011-11-25 23:14:28 +0800 | [diff] [blame] | 203 | kunmap_atomic(kaddr); |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 204 | page_cache_release(page); |
| 205 | } |
| 206 | btrfs_mark_buffer_dirty(leaf); |
| 207 | btrfs_free_path(path); |
| 208 | |
Yan, Zheng | c216775 | 2009-11-12 09:34:21 +0000 | [diff] [blame] | 209 | /* |
| 210 | * we're an inline extent, so nobody can |
| 211 | * extend the file past i_size without locking |
| 212 | * a page we already have locked. |
| 213 | * |
| 214 | * We must do any isize and inode updates |
| 215 | * before we unlock the pages. Otherwise we |
| 216 | * could end up racing with unlink. |
| 217 | */ |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 218 | BTRFS_I(inode)->disk_i_size = inode->i_size; |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 219 | ret = btrfs_update_inode(trans, root, inode); |
Yan, Zheng | c216775 | 2009-11-12 09:34:21 +0000 | [diff] [blame] | 220 | |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 221 | return ret; |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 222 | fail: |
| 223 | btrfs_free_path(path); |
| 224 | return err; |
| 225 | } |
| 226 | |
| 227 | |
| 228 | /* |
| 229 | * conditionally insert an inline extent into the file. This |
| 230 | * does the checks required to make sure the data is small enough |
| 231 | * to fit as an inline extent. |
| 232 | */ |
Josef Bacik | 0036158 | 2013-08-14 14:02:47 -0400 | [diff] [blame] | 233 | static noinline int cow_file_range_inline(struct btrfs_root *root, |
| 234 | struct inode *inode, u64 start, |
| 235 | u64 end, size_t compressed_size, |
| 236 | int compress_type, |
| 237 | struct page **compressed_pages) |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 238 | { |
Josef Bacik | 0036158 | 2013-08-14 14:02:47 -0400 | [diff] [blame] | 239 | struct btrfs_trans_handle *trans; |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 240 | u64 isize = i_size_read(inode); |
| 241 | u64 actual_end = min(end + 1, isize); |
| 242 | u64 inline_len = actual_end - start; |
Qu Wenruo | fda2832 | 2013-02-26 08:10:22 +0000 | [diff] [blame] | 243 | u64 aligned_end = ALIGN(end, root->sectorsize); |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 244 | u64 data_len = inline_len; |
| 245 | int ret; |
| 246 | |
| 247 | if (compressed_size) |
| 248 | data_len = compressed_size; |
| 249 | |
| 250 | if (start > 0 || |
Chris Mason | 70b99e6 | 2008-10-31 12:46:39 -0400 | [diff] [blame] | 251 | actual_end >= PAGE_CACHE_SIZE || |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 252 | data_len >= BTRFS_MAX_INLINE_DATA_SIZE(root) || |
| 253 | (!compressed_size && |
| 254 | (actual_end & (root->sectorsize - 1)) == 0) || |
| 255 | end + 1 < isize || |
| 256 | data_len > root->fs_info->max_inline) { |
| 257 | return 1; |
| 258 | } |
| 259 | |
Josef Bacik | 0036158 | 2013-08-14 14:02:47 -0400 | [diff] [blame] | 260 | trans = btrfs_join_transaction(root); |
| 261 | if (IS_ERR(trans)) |
| 262 | return PTR_ERR(trans); |
| 263 | trans->block_rsv = &root->fs_info->delalloc_block_rsv; |
| 264 | |
Josef Bacik | 2671485 | 2012-08-29 12:24:27 -0400 | [diff] [blame] | 265 | ret = btrfs_drop_extents(trans, root, inode, start, aligned_end, 1); |
Josef Bacik | 0036158 | 2013-08-14 14:02:47 -0400 | [diff] [blame] | 266 | if (ret) { |
| 267 | btrfs_abort_transaction(trans, root, ret); |
| 268 | goto out; |
| 269 | } |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 270 | |
| 271 | if (isize > actual_end) |
| 272 | inline_len = min_t(u64, isize, actual_end); |
| 273 | ret = insert_inline_extent(trans, root, inode, start, |
| 274 | inline_len, compressed_size, |
Li Zefan | fe3f566 | 2011-03-28 08:30:38 +0000 | [diff] [blame] | 275 | compress_type, compressed_pages); |
Josef Bacik | 2adcac1 | 2012-05-23 16:10:14 -0400 | [diff] [blame] | 276 | if (ret && ret != -ENOSPC) { |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 277 | btrfs_abort_transaction(trans, root, ret); |
Josef Bacik | 0036158 | 2013-08-14 14:02:47 -0400 | [diff] [blame] | 278 | goto out; |
Josef Bacik | 2adcac1 | 2012-05-23 16:10:14 -0400 | [diff] [blame] | 279 | } else if (ret == -ENOSPC) { |
Josef Bacik | 0036158 | 2013-08-14 14:02:47 -0400 | [diff] [blame] | 280 | ret = 1; |
| 281 | goto out; |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 282 | } |
Josef Bacik | 2adcac1 | 2012-05-23 16:10:14 -0400 | [diff] [blame] | 283 | |
Josef Bacik | bdc20e6 | 2013-02-28 13:23:38 -0500 | [diff] [blame] | 284 | set_bit(BTRFS_INODE_NEEDS_FULL_SYNC, &BTRFS_I(inode)->runtime_flags); |
Yan, Zheng | 0ca1f7c | 2010-05-16 10:48:47 -0400 | [diff] [blame] | 285 | btrfs_delalloc_release_metadata(inode, end + 1 - start); |
Chris Mason | a1ed835 | 2009-09-11 12:27:37 -0400 | [diff] [blame] | 286 | btrfs_drop_extent_cache(inode, start, aligned_end - 1, 0); |
Josef Bacik | 0036158 | 2013-08-14 14:02:47 -0400 | [diff] [blame] | 287 | out: |
| 288 | btrfs_end_transaction(trans, root); |
| 289 | return ret; |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 290 | } |
| 291 | |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 292 | struct async_extent { |
| 293 | u64 start; |
| 294 | u64 ram_size; |
| 295 | u64 compressed_size; |
| 296 | struct page **pages; |
| 297 | unsigned long nr_pages; |
Li Zefan | 261507a0 | 2010-12-17 14:21:50 +0800 | [diff] [blame] | 298 | int compress_type; |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 299 | struct list_head list; |
| 300 | }; |
| 301 | |
| 302 | struct async_cow { |
| 303 | struct inode *inode; |
| 304 | struct btrfs_root *root; |
| 305 | struct page *locked_page; |
| 306 | u64 start; |
| 307 | u64 end; |
| 308 | struct list_head extents; |
| 309 | struct btrfs_work work; |
| 310 | }; |
| 311 | |
| 312 | static noinline int add_async_extent(struct async_cow *cow, |
| 313 | u64 start, u64 ram_size, |
| 314 | u64 compressed_size, |
| 315 | struct page **pages, |
Li Zefan | 261507a0 | 2010-12-17 14:21:50 +0800 | [diff] [blame] | 316 | unsigned long nr_pages, |
| 317 | int compress_type) |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 318 | { |
| 319 | struct async_extent *async_extent; |
| 320 | |
| 321 | async_extent = kmalloc(sizeof(*async_extent), GFP_NOFS); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 322 | BUG_ON(!async_extent); /* -ENOMEM */ |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 323 | async_extent->start = start; |
| 324 | async_extent->ram_size = ram_size; |
| 325 | async_extent->compressed_size = compressed_size; |
| 326 | async_extent->pages = pages; |
| 327 | async_extent->nr_pages = nr_pages; |
Li Zefan | 261507a0 | 2010-12-17 14:21:50 +0800 | [diff] [blame] | 328 | async_extent->compress_type = compress_type; |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 329 | list_add_tail(&async_extent->list, &cow->extents); |
| 330 | return 0; |
| 331 | } |
| 332 | |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 333 | /* |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 334 | * we create compressed extents in two phases. The first |
| 335 | * phase compresses a range of pages that have already been |
| 336 | * locked (both pages and state bits are locked). |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 337 | * |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 338 | * This is done inside an ordered work queue, and the compression |
| 339 | * is spread across many cpus. The actual IO submission is step |
| 340 | * two, and the ordered work queue takes care of making sure that |
| 341 | * happens in the same order things were put onto the queue by |
| 342 | * writepages and friends. |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 343 | * |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 344 | * If this code finds it can't get good compression, it puts an |
| 345 | * entry onto the work queue to write the uncompressed bytes. This |
| 346 | * makes sure that both compressed inodes and uncompressed inodes |
Artem Bityutskiy | b257031 | 2012-07-25 18:12:06 +0300 | [diff] [blame] | 347 | * are written in the same order that the flusher thread sent them |
| 348 | * down. |
Chris Mason | d352ac6 | 2008-09-29 15:18:18 -0400 | [diff] [blame] | 349 | */ |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 350 | static noinline int compress_file_range(struct inode *inode, |
| 351 | struct page *locked_page, |
| 352 | u64 start, u64 end, |
| 353 | struct async_cow *async_cow, |
| 354 | int *num_added) |
Chris Mason | b888db2 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 355 | { |
| 356 | struct btrfs_root *root = BTRFS_I(inode)->root; |
Chris Mason | db94535 | 2007-10-15 16:15:53 -0400 | [diff] [blame] | 357 | u64 num_bytes; |
Chris Mason | db94535 | 2007-10-15 16:15:53 -0400 | [diff] [blame] | 358 | u64 blocksize = root->sectorsize; |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 359 | u64 actual_end; |
Chris Mason | 42dc7ba | 2008-12-15 11:44:56 -0500 | [diff] [blame] | 360 | u64 isize = i_size_read(inode); |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 361 | int ret = 0; |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 362 | struct page **pages = NULL; |
| 363 | unsigned long nr_pages; |
| 364 | unsigned long nr_pages_ret = 0; |
| 365 | unsigned long total_compressed = 0; |
| 366 | unsigned long total_in = 0; |
| 367 | unsigned long max_compressed = 128 * 1024; |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 368 | unsigned long max_uncompressed = 128 * 1024; |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 369 | int i; |
| 370 | int will_compress; |
Li Zefan | 261507a0 | 2010-12-17 14:21:50 +0800 | [diff] [blame] | 371 | int compress_type = root->fs_info->compress_type; |
Chris Mason | 4adaa61 | 2013-03-26 13:07:00 -0400 | [diff] [blame] | 372 | int redirty = 0; |
Chris Mason | b888db2 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 373 | |
Liu Bo | 4cb13e5 | 2012-03-29 09:57:45 -0400 | [diff] [blame] | 374 | /* if this is a small write inside eof, kick off a defrag */ |
| 375 | if ((end - start + 1) < 16 * 1024 && |
| 376 | (start > 0 || end + 1 < BTRFS_I(inode)->disk_i_size)) |
Chris Mason | 4cb5300 | 2011-05-24 15:35:30 -0400 | [diff] [blame] | 377 | btrfs_add_inode_defrag(NULL, inode); |
| 378 | |
Chris Mason | 42dc7ba | 2008-12-15 11:44:56 -0500 | [diff] [blame] | 379 | actual_end = min_t(u64, isize, end + 1); |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 380 | again: |
| 381 | will_compress = 0; |
| 382 | nr_pages = (end >> PAGE_CACHE_SHIFT) - (start >> PAGE_CACHE_SHIFT) + 1; |
| 383 | nr_pages = min(nr_pages, (128 * 1024UL) / PAGE_CACHE_SIZE); |
| 384 | |
Chris Mason | f03d9301f | 2009-02-04 09:31:06 -0500 | [diff] [blame] | 385 | /* |
| 386 | * we don't want to send crud past the end of i_size through |
| 387 | * compression, that's just a waste of CPU time. So, if the |
| 388 | * end of the file is before the start of our current |
| 389 | * requested range of bytes, we bail out to the uncompressed |
| 390 | * cleanup code that can deal with all of this. |
| 391 | * |
| 392 | * It isn't really the fastest way to fix things, but this is a |
| 393 | * very uncommon corner. |
| 394 | */ |
| 395 | if (actual_end <= start) |
| 396 | goto cleanup_and_bail_uncompressed; |
| 397 | |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 398 | total_compressed = actual_end - start; |
| 399 | |
| 400 | /* we want to make sure that amount of ram required to uncompress |
| 401 | * an extent is reasonable, so we limit the total size in ram |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 402 | * of a compressed extent to 128k. This is a crucial number |
| 403 | * because it also controls how easily we can spread reads across |
| 404 | * cpus for decompression. |
| 405 | * |
| 406 | * We also want to make sure the amount of IO required to do |
| 407 | * a random read is reasonably small, so we limit the size of |
| 408 | * a compressed extent to 128k. |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 409 | */ |
| 410 | total_compressed = min(total_compressed, max_uncompressed); |
Qu Wenruo | fda2832 | 2013-02-26 08:10:22 +0000 | [diff] [blame] | 411 | num_bytes = ALIGN(end - start + 1, blocksize); |
Chris Mason | be20aa9 | 2007-12-17 20:14:01 -0500 | [diff] [blame] | 412 | num_bytes = max(blocksize, num_bytes); |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 413 | total_in = 0; |
| 414 | ret = 0; |
Chris Mason | db94535 | 2007-10-15 16:15:53 -0400 | [diff] [blame] | 415 | |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 416 | /* |
| 417 | * we do compression for mount -o compress and when the |
| 418 | * inode has not been flagged as nocompress. This flag can |
| 419 | * change at any time if we discover bad compression ratios. |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 420 | */ |
Christoph Hellwig | 6cbff00 | 2009-04-17 10:37:41 +0200 | [diff] [blame] | 421 | if (!(BTRFS_I(inode)->flags & BTRFS_INODE_NOCOMPRESS) && |
Chris Mason | 1e701a3 | 2010-03-11 09:42:04 -0500 | [diff] [blame] | 422 | (btrfs_test_opt(root, COMPRESS) || |
Liu Bo | 75e7cb7 | 2011-03-22 10:12:20 +0000 | [diff] [blame] | 423 | (BTRFS_I(inode)->force_compress) || |
| 424 | (BTRFS_I(inode)->flags & BTRFS_INODE_COMPRESS))) { |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 425 | WARN_ON(pages); |
Chris Mason | cfbc246 | 2008-10-30 13:22:14 -0400 | [diff] [blame] | 426 | pages = kzalloc(sizeof(struct page *) * nr_pages, GFP_NOFS); |
Li Zefan | 560f7d7 | 2011-09-08 10:22:01 +0800 | [diff] [blame] | 427 | if (!pages) { |
| 428 | /* just bail out to the uncompressed code */ |
| 429 | goto cont; |
| 430 | } |
Chris Mason | 179e29e | 2007-11-01 11:28:41 -0400 | [diff] [blame] | 431 | |
Li Zefan | 261507a0 | 2010-12-17 14:21:50 +0800 | [diff] [blame] | 432 | if (BTRFS_I(inode)->force_compress) |
| 433 | compress_type = BTRFS_I(inode)->force_compress; |
| 434 | |
Chris Mason | 4adaa61 | 2013-03-26 13:07:00 -0400 | [diff] [blame] | 435 | /* |
| 436 | * we need to call clear_page_dirty_for_io on each |
| 437 | * page in the range. Otherwise applications with the file |
| 438 | * mmap'd can wander in and change the page contents while |
| 439 | * we are compressing them. |
| 440 | * |
| 441 | * If the compression fails for any reason, we set the pages |
| 442 | * dirty again later on. |
| 443 | */ |
| 444 | extent_range_clear_dirty_for_io(inode, start, end); |
| 445 | redirty = 1; |
Li Zefan | 261507a0 | 2010-12-17 14:21:50 +0800 | [diff] [blame] | 446 | ret = btrfs_compress_pages(compress_type, |
| 447 | inode->i_mapping, start, |
| 448 | total_compressed, pages, |
| 449 | nr_pages, &nr_pages_ret, |
| 450 | &total_in, |
| 451 | &total_compressed, |
| 452 | max_compressed); |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 453 | |
| 454 | if (!ret) { |
| 455 | unsigned long offset = total_compressed & |
| 456 | (PAGE_CACHE_SIZE - 1); |
| 457 | struct page *page = pages[nr_pages_ret - 1]; |
| 458 | char *kaddr; |
| 459 | |
| 460 | /* zero the tail end of the last page, we might be |
| 461 | * sending it down to disk |
| 462 | */ |
| 463 | if (offset) { |
Cong Wang | 7ac687d | 2011-11-25 23:14:28 +0800 | [diff] [blame] | 464 | kaddr = kmap_atomic(page); |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 465 | memset(kaddr + offset, 0, |
| 466 | PAGE_CACHE_SIZE - offset); |
Cong Wang | 7ac687d | 2011-11-25 23:14:28 +0800 | [diff] [blame] | 467 | kunmap_atomic(kaddr); |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 468 | } |
| 469 | will_compress = 1; |
| 470 | } |
| 471 | } |
Li Zefan | 560f7d7 | 2011-09-08 10:22:01 +0800 | [diff] [blame] | 472 | cont: |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 473 | if (start == 0) { |
| 474 | /* lets try to make an inline extent */ |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 475 | if (ret || total_in < (actual_end - start)) { |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 476 | /* we didn't compress the entire range, try |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 477 | * to make an uncompressed inline extent. |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 478 | */ |
Josef Bacik | 0036158 | 2013-08-14 14:02:47 -0400 | [diff] [blame] | 479 | ret = cow_file_range_inline(root, inode, start, end, |
| 480 | 0, 0, NULL); |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 481 | } else { |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 482 | /* try making a compressed inline extent */ |
Josef Bacik | 0036158 | 2013-08-14 14:02:47 -0400 | [diff] [blame] | 483 | ret = cow_file_range_inline(root, inode, start, end, |
Li Zefan | fe3f566 | 2011-03-28 08:30:38 +0000 | [diff] [blame] | 484 | total_compressed, |
| 485 | compress_type, pages); |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 486 | } |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 487 | if (ret <= 0) { |
Josef Bacik | 151a41b | 2013-07-29 13:22:24 -0400 | [diff] [blame] | 488 | unsigned long clear_flags = EXTENT_DELALLOC | |
| 489 | EXTENT_DEFRAG; |
| 490 | clear_flags |= (ret < 0) ? EXTENT_DO_ACCOUNTING : 0; |
| 491 | |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 492 | /* |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 493 | * inline extent creation worked or returned error, |
| 494 | * we don't need to create any more async work items. |
| 495 | * Unlock and free up our temp pages. |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 496 | */ |
Josef Bacik | c2790a2 | 2013-07-29 11:20:47 -0400 | [diff] [blame] | 497 | extent_clear_unlock_delalloc(inode, start, end, NULL, |
Josef Bacik | 151a41b | 2013-07-29 13:22:24 -0400 | [diff] [blame] | 498 | clear_flags, PAGE_UNLOCK | |
Josef Bacik | c2790a2 | 2013-07-29 11:20:47 -0400 | [diff] [blame] | 499 | PAGE_CLEAR_DIRTY | |
| 500 | PAGE_SET_WRITEBACK | |
| 501 | PAGE_END_WRITEBACK); |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 502 | goto free_pages_out; |
| 503 | } |
| 504 | } |
| 505 | |
| 506 | if (will_compress) { |
| 507 | /* |
| 508 | * we aren't doing an inline extent round the compressed size |
| 509 | * up to a block size boundary so the allocator does sane |
| 510 | * things |
| 511 | */ |
Qu Wenruo | fda2832 | 2013-02-26 08:10:22 +0000 | [diff] [blame] | 512 | total_compressed = ALIGN(total_compressed, blocksize); |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 513 | |
| 514 | /* |
| 515 | * one last check to make sure the compression is really a |
| 516 | * win, compare the page count read with the blocks on disk |
| 517 | */ |
Qu Wenruo | fda2832 | 2013-02-26 08:10:22 +0000 | [diff] [blame] | 518 | total_in = ALIGN(total_in, PAGE_CACHE_SIZE); |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 519 | if (total_compressed >= total_in) { |
| 520 | will_compress = 0; |
| 521 | } else { |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 522 | num_bytes = total_in; |
| 523 | } |
| 524 | } |
| 525 | if (!will_compress && pages) { |
| 526 | /* |
| 527 | * the compression code ran but failed to make things smaller, |
| 528 | * free any pages it allocated and our page pointer array |
| 529 | */ |
| 530 | for (i = 0; i < nr_pages_ret; i++) { |
Chris Mason | 70b99e6 | 2008-10-31 12:46:39 -0400 | [diff] [blame] | 531 | WARN_ON(pages[i]->mapping); |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 532 | page_cache_release(pages[i]); |
| 533 | } |
| 534 | kfree(pages); |
| 535 | pages = NULL; |
| 536 | total_compressed = 0; |
| 537 | nr_pages_ret = 0; |
| 538 | |
| 539 | /* flag the file so we don't compress in the future */ |
Chris Mason | 1e701a3 | 2010-03-11 09:42:04 -0500 | [diff] [blame] | 540 | if (!btrfs_test_opt(root, FORCE_COMPRESS) && |
| 541 | !(BTRFS_I(inode)->force_compress)) { |
Chris Mason | a555f81 | 2010-01-28 16:18:15 -0500 | [diff] [blame] | 542 | BTRFS_I(inode)->flags |= BTRFS_INODE_NOCOMPRESS; |
Chris Mason | 1e701a3 | 2010-03-11 09:42:04 -0500 | [diff] [blame] | 543 | } |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 544 | } |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 545 | if (will_compress) { |
| 546 | *num_added += 1; |
| 547 | |
| 548 | /* the async work queues will take care of doing actual |
| 549 | * allocation on disk for these compressed pages, |
| 550 | * and will submit them to the elevator. |
| 551 | */ |
| 552 | add_async_extent(async_cow, start, num_bytes, |
Li Zefan | 261507a0 | 2010-12-17 14:21:50 +0800 | [diff] [blame] | 553 | total_compressed, pages, nr_pages_ret, |
| 554 | compress_type); |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 555 | |
Yan, Zheng | 24ae636 | 2010-12-06 07:02:36 +0000 | [diff] [blame] | 556 | if (start + num_bytes < end) { |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 557 | start += num_bytes; |
| 558 | pages = NULL; |
| 559 | cond_resched(); |
| 560 | goto again; |
| 561 | } |
| 562 | } else { |
Chris Mason | f03d9301f | 2009-02-04 09:31:06 -0500 | [diff] [blame] | 563 | cleanup_and_bail_uncompressed: |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 564 | /* |
| 565 | * No compression, but we still need to write the pages in |
| 566 | * the file we've been given so far. redirty the locked |
| 567 | * page if it corresponds to our extent and set things up |
| 568 | * for the async work queue to run cow_file_range to do |
| 569 | * the normal delalloc dance |
| 570 | */ |
| 571 | if (page_offset(locked_page) >= start && |
| 572 | page_offset(locked_page) <= end) { |
| 573 | __set_page_dirty_nobuffers(locked_page); |
| 574 | /* unlocked later on in the async handlers */ |
| 575 | } |
Chris Mason | 4adaa61 | 2013-03-26 13:07:00 -0400 | [diff] [blame] | 576 | if (redirty) |
| 577 | extent_range_redirty_for_io(inode, start, end); |
Li Zefan | 261507a0 | 2010-12-17 14:21:50 +0800 | [diff] [blame] | 578 | add_async_extent(async_cow, start, end - start + 1, |
| 579 | 0, NULL, 0, BTRFS_COMPRESS_NONE); |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 580 | *num_added += 1; |
| 581 | } |
| 582 | |
| 583 | out: |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 584 | return ret; |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 585 | |
| 586 | free_pages_out: |
| 587 | for (i = 0; i < nr_pages_ret; i++) { |
| 588 | WARN_ON(pages[i]->mapping); |
| 589 | page_cache_release(pages[i]); |
| 590 | } |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 591 | kfree(pages); |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 592 | |
| 593 | goto out; |
| 594 | } |
| 595 | |
| 596 | /* |
| 597 | * phase two of compressed writeback. This is the ordered portion |
| 598 | * of the code, which only gets called in the order the work was |
| 599 | * queued. We walk all the async extents created by compress_file_range |
| 600 | * and send them down to the disk. |
| 601 | */ |
| 602 | static noinline int submit_compressed_extents(struct inode *inode, |
| 603 | struct async_cow *async_cow) |
| 604 | { |
| 605 | struct async_extent *async_extent; |
| 606 | u64 alloc_hint = 0; |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 607 | struct btrfs_key ins; |
| 608 | struct extent_map *em; |
| 609 | struct btrfs_root *root = BTRFS_I(inode)->root; |
| 610 | struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree; |
| 611 | struct extent_io_tree *io_tree; |
Josef Bacik | f5a84ee3 | 2009-11-10 21:23:48 -0500 | [diff] [blame] | 612 | int ret = 0; |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 613 | |
| 614 | if (list_empty(&async_cow->extents)) |
| 615 | return 0; |
| 616 | |
Josef Bacik | 3e04e7f | 2013-02-06 16:49:15 -0500 | [diff] [blame] | 617 | again: |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 618 | while (!list_empty(&async_cow->extents)) { |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 619 | async_extent = list_entry(async_cow->extents.next, |
| 620 | struct async_extent, list); |
| 621 | list_del(&async_extent->list); |
| 622 | |
| 623 | io_tree = &BTRFS_I(inode)->io_tree; |
| 624 | |
Josef Bacik | f5a84ee3 | 2009-11-10 21:23:48 -0500 | [diff] [blame] | 625 | retry: |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 626 | /* did the compression code fall back to uncompressed IO? */ |
| 627 | if (!async_extent->pages) { |
| 628 | int page_started = 0; |
| 629 | unsigned long nr_written = 0; |
| 630 | |
| 631 | lock_extent(io_tree, async_extent->start, |
Josef Bacik | 2ac55d4 | 2010-02-03 19:33:23 +0000 | [diff] [blame] | 632 | async_extent->start + |
Jeff Mahoney | d008237 | 2012-03-01 14:57:19 +0100 | [diff] [blame] | 633 | async_extent->ram_size - 1); |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 634 | |
| 635 | /* allocate blocks */ |
Josef Bacik | f5a84ee3 | 2009-11-10 21:23:48 -0500 | [diff] [blame] | 636 | ret = cow_file_range(inode, async_cow->locked_page, |
| 637 | async_extent->start, |
| 638 | async_extent->start + |
| 639 | async_extent->ram_size - 1, |
| 640 | &page_started, &nr_written, 0); |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 641 | |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 642 | /* JDM XXX */ |
| 643 | |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 644 | /* |
| 645 | * if page_started, cow_file_range inserted an |
| 646 | * inline extent and took care of all the unlocking |
| 647 | * and IO for us. Otherwise, we need to submit |
| 648 | * all those pages down to the drive. |
| 649 | */ |
Josef Bacik | f5a84ee3 | 2009-11-10 21:23:48 -0500 | [diff] [blame] | 650 | if (!page_started && !ret) |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 651 | extent_write_locked_range(io_tree, |
| 652 | inode, async_extent->start, |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 653 | async_extent->start + |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 654 | async_extent->ram_size - 1, |
| 655 | btrfs_get_extent, |
| 656 | WB_SYNC_ALL); |
Josef Bacik | 3e04e7f | 2013-02-06 16:49:15 -0500 | [diff] [blame] | 657 | else if (ret) |
| 658 | unlock_page(async_cow->locked_page); |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 659 | kfree(async_extent); |
| 660 | cond_resched(); |
| 661 | continue; |
| 662 | } |
| 663 | |
| 664 | lock_extent(io_tree, async_extent->start, |
Jeff Mahoney | d008237 | 2012-03-01 14:57:19 +0100 | [diff] [blame] | 665 | async_extent->start + async_extent->ram_size - 1); |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 666 | |
Josef Bacik | 0036158 | 2013-08-14 14:02:47 -0400 | [diff] [blame] | 667 | ret = btrfs_reserve_extent(root, |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 668 | async_extent->compressed_size, |
| 669 | async_extent->compressed_size, |
Josef Bacik | 81c9ad2 | 2012-01-18 10:56:06 -0500 | [diff] [blame] | 670 | 0, alloc_hint, &ins, 1); |
Josef Bacik | f5a84ee3 | 2009-11-10 21:23:48 -0500 | [diff] [blame] | 671 | if (ret) { |
| 672 | int i; |
Josef Bacik | 3e04e7f | 2013-02-06 16:49:15 -0500 | [diff] [blame] | 673 | |
Josef Bacik | f5a84ee3 | 2009-11-10 21:23:48 -0500 | [diff] [blame] | 674 | for (i = 0; i < async_extent->nr_pages; i++) { |
| 675 | WARN_ON(async_extent->pages[i]->mapping); |
| 676 | page_cache_release(async_extent->pages[i]); |
| 677 | } |
| 678 | kfree(async_extent->pages); |
| 679 | async_extent->nr_pages = 0; |
| 680 | async_extent->pages = NULL; |
Josef Bacik | 3e04e7f | 2013-02-06 16:49:15 -0500 | [diff] [blame] | 681 | |
Josef Bacik | fdf8e2e | 2013-06-14 16:58:23 -0400 | [diff] [blame] | 682 | if (ret == -ENOSPC) { |
| 683 | unlock_extent(io_tree, async_extent->start, |
| 684 | async_extent->start + |
| 685 | async_extent->ram_size - 1); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 686 | goto retry; |
Josef Bacik | fdf8e2e | 2013-06-14 16:58:23 -0400 | [diff] [blame] | 687 | } |
Josef Bacik | 3e04e7f | 2013-02-06 16:49:15 -0500 | [diff] [blame] | 688 | goto out_free; |
Josef Bacik | f5a84ee3 | 2009-11-10 21:23:48 -0500 | [diff] [blame] | 689 | } |
| 690 | |
Yan, Zheng | c216775 | 2009-11-12 09:34:21 +0000 | [diff] [blame] | 691 | /* |
| 692 | * here we're doing allocation and writeback of the |
| 693 | * compressed pages |
| 694 | */ |
| 695 | btrfs_drop_extent_cache(inode, async_extent->start, |
| 696 | async_extent->start + |
| 697 | async_extent->ram_size - 1, 0); |
| 698 | |
David Sterba | 172ddd6 | 2011-04-21 00:48:27 +0200 | [diff] [blame] | 699 | em = alloc_extent_map(); |
Liu Bo | b9aa55b | 2013-05-14 02:12:15 +0000 | [diff] [blame] | 700 | if (!em) { |
| 701 | ret = -ENOMEM; |
Josef Bacik | 3e04e7f | 2013-02-06 16:49:15 -0500 | [diff] [blame] | 702 | goto out_free_reserve; |
Liu Bo | b9aa55b | 2013-05-14 02:12:15 +0000 | [diff] [blame] | 703 | } |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 704 | em->start = async_extent->start; |
| 705 | em->len = async_extent->ram_size; |
Chris Mason | 445a694 | 2008-11-10 11:53:33 -0500 | [diff] [blame] | 706 | em->orig_start = em->start; |
Josef Bacik | 2ab28f3 | 2012-10-12 15:27:49 -0400 | [diff] [blame] | 707 | em->mod_start = em->start; |
| 708 | em->mod_len = em->len; |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 709 | |
| 710 | em->block_start = ins.objectid; |
| 711 | em->block_len = ins.offset; |
Josef Bacik | b493968 | 2012-12-03 10:31:19 -0500 | [diff] [blame] | 712 | em->orig_block_len = ins.offset; |
Josef Bacik | cc95bef | 2013-04-04 14:31:27 -0400 | [diff] [blame] | 713 | em->ram_bytes = async_extent->ram_size; |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 714 | em->bdev = root->fs_info->fs_devices->latest_bdev; |
Li Zefan | 261507a0 | 2010-12-17 14:21:50 +0800 | [diff] [blame] | 715 | em->compress_type = async_extent->compress_type; |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 716 | set_bit(EXTENT_FLAG_PINNED, &em->flags); |
| 717 | set_bit(EXTENT_FLAG_COMPRESSED, &em->flags); |
Josef Bacik | 70c8a91 | 2012-10-11 16:54:30 -0400 | [diff] [blame] | 718 | em->generation = -1; |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 719 | |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 720 | while (1) { |
Chris Mason | 890871b | 2009-09-02 16:24:52 -0400 | [diff] [blame] | 721 | write_lock(&em_tree->lock); |
Josef Bacik | 09a2a8f9 | 2013-04-05 16:51:15 -0400 | [diff] [blame] | 722 | ret = add_extent_mapping(em_tree, em, 1); |
Chris Mason | 890871b | 2009-09-02 16:24:52 -0400 | [diff] [blame] | 723 | write_unlock(&em_tree->lock); |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 724 | if (ret != -EEXIST) { |
| 725 | free_extent_map(em); |
| 726 | break; |
| 727 | } |
| 728 | btrfs_drop_extent_cache(inode, async_extent->start, |
| 729 | async_extent->start + |
| 730 | async_extent->ram_size - 1, 0); |
| 731 | } |
| 732 | |
Josef Bacik | 3e04e7f | 2013-02-06 16:49:15 -0500 | [diff] [blame] | 733 | if (ret) |
| 734 | goto out_free_reserve; |
| 735 | |
Li Zefan | 261507a0 | 2010-12-17 14:21:50 +0800 | [diff] [blame] | 736 | ret = btrfs_add_ordered_extent_compress(inode, |
| 737 | async_extent->start, |
| 738 | ins.objectid, |
| 739 | async_extent->ram_size, |
| 740 | ins.offset, |
| 741 | BTRFS_ORDERED_COMPRESSED, |
| 742 | async_extent->compress_type); |
Josef Bacik | 3e04e7f | 2013-02-06 16:49:15 -0500 | [diff] [blame] | 743 | if (ret) |
| 744 | goto out_free_reserve; |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 745 | |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 746 | /* |
| 747 | * clear dirty, set writeback and unlock the pages. |
| 748 | */ |
Josef Bacik | c2790a2 | 2013-07-29 11:20:47 -0400 | [diff] [blame] | 749 | extent_clear_unlock_delalloc(inode, async_extent->start, |
Chris Mason | a791e35 | 2009-10-08 11:27:10 -0400 | [diff] [blame] | 750 | async_extent->start + |
| 751 | async_extent->ram_size - 1, |
Josef Bacik | 151a41b | 2013-07-29 13:22:24 -0400 | [diff] [blame] | 752 | NULL, EXTENT_LOCKED | EXTENT_DELALLOC, |
| 753 | PAGE_UNLOCK | PAGE_CLEAR_DIRTY | |
Josef Bacik | c2790a2 | 2013-07-29 11:20:47 -0400 | [diff] [blame] | 754 | PAGE_SET_WRITEBACK); |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 755 | ret = btrfs_submit_compressed_write(inode, |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 756 | async_extent->start, |
| 757 | async_extent->ram_size, |
| 758 | ins.objectid, |
| 759 | ins.offset, async_extent->pages, |
| 760 | async_extent->nr_pages); |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 761 | alloc_hint = ins.objectid + ins.offset; |
| 762 | kfree(async_extent); |
Josef Bacik | 3e04e7f | 2013-02-06 16:49:15 -0500 | [diff] [blame] | 763 | if (ret) |
| 764 | goto out; |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 765 | cond_resched(); |
| 766 | } |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 767 | ret = 0; |
| 768 | out: |
| 769 | return ret; |
Josef Bacik | 3e04e7f | 2013-02-06 16:49:15 -0500 | [diff] [blame] | 770 | out_free_reserve: |
| 771 | btrfs_free_reserved_extent(root, ins.objectid, ins.offset); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 772 | out_free: |
Josef Bacik | c2790a2 | 2013-07-29 11:20:47 -0400 | [diff] [blame] | 773 | extent_clear_unlock_delalloc(inode, async_extent->start, |
Josef Bacik | 3e04e7f | 2013-02-06 16:49:15 -0500 | [diff] [blame] | 774 | async_extent->start + |
| 775 | async_extent->ram_size - 1, |
Josef Bacik | c2790a2 | 2013-07-29 11:20:47 -0400 | [diff] [blame] | 776 | NULL, EXTENT_LOCKED | EXTENT_DELALLOC | |
Josef Bacik | 151a41b | 2013-07-29 13:22:24 -0400 | [diff] [blame] | 777 | EXTENT_DEFRAG | EXTENT_DO_ACCOUNTING, |
| 778 | PAGE_UNLOCK | PAGE_CLEAR_DIRTY | |
| 779 | PAGE_SET_WRITEBACK | PAGE_END_WRITEBACK); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 780 | kfree(async_extent); |
Josef Bacik | 3e04e7f | 2013-02-06 16:49:15 -0500 | [diff] [blame] | 781 | goto again; |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 782 | } |
| 783 | |
Josef Bacik | 4b46fce | 2010-05-23 11:00:55 -0400 | [diff] [blame] | 784 | static u64 get_extent_allocation_hint(struct inode *inode, u64 start, |
| 785 | u64 num_bytes) |
| 786 | { |
| 787 | struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree; |
| 788 | struct extent_map *em; |
| 789 | u64 alloc_hint = 0; |
| 790 | |
| 791 | read_lock(&em_tree->lock); |
| 792 | em = search_extent_mapping(em_tree, start, num_bytes); |
| 793 | if (em) { |
| 794 | /* |
| 795 | * if block start isn't an actual block number then find the |
| 796 | * first block in this inode and use that as a hint. If that |
| 797 | * block is also bogus then just don't worry about it. |
| 798 | */ |
| 799 | if (em->block_start >= EXTENT_MAP_LAST_BYTE) { |
| 800 | free_extent_map(em); |
| 801 | em = search_extent_mapping(em_tree, 0, 0); |
| 802 | if (em && em->block_start < EXTENT_MAP_LAST_BYTE) |
| 803 | alloc_hint = em->block_start; |
| 804 | if (em) |
| 805 | free_extent_map(em); |
| 806 | } else { |
| 807 | alloc_hint = em->block_start; |
| 808 | free_extent_map(em); |
| 809 | } |
| 810 | } |
| 811 | read_unlock(&em_tree->lock); |
| 812 | |
| 813 | return alloc_hint; |
| 814 | } |
| 815 | |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 816 | /* |
| 817 | * when extent_io.c finds a delayed allocation range in the file, |
| 818 | * the call backs end up in this code. The basic idea is to |
| 819 | * allocate extents on disk for the range, and create ordered data structs |
| 820 | * in ram to track those extents. |
| 821 | * |
| 822 | * locked_page is the page that writepage had locked already. We use |
| 823 | * it to make sure we don't do extra locks or unlocks. |
| 824 | * |
| 825 | * *page_started is set to one if we unlock locked_page and do everything |
| 826 | * required to start IO on it. It may be clean and already done with |
| 827 | * IO when we return. |
| 828 | */ |
Josef Bacik | 0036158 | 2013-08-14 14:02:47 -0400 | [diff] [blame] | 829 | static noinline int cow_file_range(struct inode *inode, |
| 830 | struct page *locked_page, |
| 831 | u64 start, u64 end, int *page_started, |
| 832 | unsigned long *nr_written, |
| 833 | int unlock) |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 834 | { |
Josef Bacik | 0036158 | 2013-08-14 14:02:47 -0400 | [diff] [blame] | 835 | struct btrfs_root *root = BTRFS_I(inode)->root; |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 836 | u64 alloc_hint = 0; |
| 837 | u64 num_bytes; |
| 838 | unsigned long ram_size; |
| 839 | u64 disk_num_bytes; |
| 840 | u64 cur_alloc_size; |
| 841 | u64 blocksize = root->sectorsize; |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 842 | struct btrfs_key ins; |
| 843 | struct extent_map *em; |
| 844 | struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree; |
| 845 | int ret = 0; |
| 846 | |
Liu Bo | 83eea1f | 2012-07-10 05:28:39 -0600 | [diff] [blame] | 847 | BUG_ON(btrfs_is_free_space_inode(inode)); |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 848 | |
Qu Wenruo | fda2832 | 2013-02-26 08:10:22 +0000 | [diff] [blame] | 849 | num_bytes = ALIGN(end - start + 1, blocksize); |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 850 | num_bytes = max(blocksize, num_bytes); |
| 851 | disk_num_bytes = num_bytes; |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 852 | |
Chris Mason | 4cb5300 | 2011-05-24 15:35:30 -0400 | [diff] [blame] | 853 | /* if this is a small write inside eof, kick off defrag */ |
Liu Bo | 4cb13e5 | 2012-03-29 09:57:45 -0400 | [diff] [blame] | 854 | if (num_bytes < 64 * 1024 && |
| 855 | (start > 0 || end + 1 < BTRFS_I(inode)->disk_i_size)) |
Josef Bacik | 0036158 | 2013-08-14 14:02:47 -0400 | [diff] [blame] | 856 | btrfs_add_inode_defrag(NULL, inode); |
Chris Mason | 4cb5300 | 2011-05-24 15:35:30 -0400 | [diff] [blame] | 857 | |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 858 | if (start == 0) { |
| 859 | /* lets try to make an inline extent */ |
Josef Bacik | 0036158 | 2013-08-14 14:02:47 -0400 | [diff] [blame] | 860 | ret = cow_file_range_inline(root, inode, start, end, 0, 0, |
| 861 | NULL); |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 862 | if (ret == 0) { |
Josef Bacik | c2790a2 | 2013-07-29 11:20:47 -0400 | [diff] [blame] | 863 | extent_clear_unlock_delalloc(inode, start, end, NULL, |
| 864 | EXTENT_LOCKED | EXTENT_DELALLOC | |
Josef Bacik | 151a41b | 2013-07-29 13:22:24 -0400 | [diff] [blame] | 865 | EXTENT_DEFRAG, PAGE_UNLOCK | |
Josef Bacik | c2790a2 | 2013-07-29 11:20:47 -0400 | [diff] [blame] | 866 | PAGE_CLEAR_DIRTY | PAGE_SET_WRITEBACK | |
| 867 | PAGE_END_WRITEBACK); |
Yan, Zheng | c216775 | 2009-11-12 09:34:21 +0000 | [diff] [blame] | 868 | |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 869 | *nr_written = *nr_written + |
| 870 | (end - start + PAGE_CACHE_SIZE) / PAGE_CACHE_SIZE; |
| 871 | *page_started = 1; |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 872 | goto out; |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 873 | } else if (ret < 0) { |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 874 | goto out_unlock; |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 875 | } |
| 876 | } |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 877 | |
| 878 | BUG_ON(disk_num_bytes > |
David Sterba | 6c41761 | 2011-04-13 15:41:04 +0200 | [diff] [blame] | 879 | btrfs_super_total_bytes(root->fs_info->super_copy)); |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 880 | |
Josef Bacik | 4b46fce | 2010-05-23 11:00:55 -0400 | [diff] [blame] | 881 | alloc_hint = get_extent_allocation_hint(inode, start, num_bytes); |
Zheng Yan | 5b21f2e | 2008-09-26 10:05:38 -0400 | [diff] [blame] | 882 | btrfs_drop_extent_cache(inode, start, start + num_bytes - 1, 0); |
Chris Mason | 3b95151 | 2008-04-17 11:29:12 -0400 | [diff] [blame] | 883 | |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 884 | while (disk_num_bytes > 0) { |
Chris Mason | a791e35 | 2009-10-08 11:27:10 -0400 | [diff] [blame] | 885 | unsigned long op; |
| 886 | |
Josef Bacik | 287a0ab | 2010-03-19 18:07:23 +0000 | [diff] [blame] | 887 | cur_alloc_size = disk_num_bytes; |
Josef Bacik | 0036158 | 2013-08-14 14:02:47 -0400 | [diff] [blame] | 888 | ret = btrfs_reserve_extent(root, cur_alloc_size, |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 889 | root->sectorsize, 0, alloc_hint, |
Josef Bacik | 81c9ad2 | 2012-01-18 10:56:06 -0500 | [diff] [blame] | 890 | &ins, 1); |
Josef Bacik | 0036158 | 2013-08-14 14:02:47 -0400 | [diff] [blame] | 891 | if (ret < 0) |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 892 | goto out_unlock; |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 893 | |
David Sterba | 172ddd6 | 2011-04-21 00:48:27 +0200 | [diff] [blame] | 894 | em = alloc_extent_map(); |
Liu Bo | b9aa55b | 2013-05-14 02:12:15 +0000 | [diff] [blame] | 895 | if (!em) { |
| 896 | ret = -ENOMEM; |
Liu Bo | ace68ba | 2013-04-22 10:53:47 +0000 | [diff] [blame] | 897 | goto out_reserve; |
Liu Bo | b9aa55b | 2013-05-14 02:12:15 +0000 | [diff] [blame] | 898 | } |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 899 | em->start = start; |
Chris Mason | 445a694 | 2008-11-10 11:53:33 -0500 | [diff] [blame] | 900 | em->orig_start = em->start; |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 901 | ram_size = ins.offset; |
| 902 | em->len = ins.offset; |
Josef Bacik | 2ab28f3 | 2012-10-12 15:27:49 -0400 | [diff] [blame] | 903 | em->mod_start = em->start; |
| 904 | em->mod_len = em->len; |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 905 | |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 906 | em->block_start = ins.objectid; |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 907 | em->block_len = ins.offset; |
Josef Bacik | b493968 | 2012-12-03 10:31:19 -0500 | [diff] [blame] | 908 | em->orig_block_len = ins.offset; |
Josef Bacik | cc95bef | 2013-04-04 14:31:27 -0400 | [diff] [blame] | 909 | em->ram_bytes = ram_size; |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 910 | em->bdev = root->fs_info->fs_devices->latest_bdev; |
Chris Mason | 7f3c74f | 2008-07-18 12:01:11 -0400 | [diff] [blame] | 911 | set_bit(EXTENT_FLAG_PINNED, &em->flags); |
Josef Bacik | 70c8a91 | 2012-10-11 16:54:30 -0400 | [diff] [blame] | 912 | em->generation = -1; |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 913 | |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 914 | while (1) { |
Chris Mason | 890871b | 2009-09-02 16:24:52 -0400 | [diff] [blame] | 915 | write_lock(&em_tree->lock); |
Josef Bacik | 09a2a8f9 | 2013-04-05 16:51:15 -0400 | [diff] [blame] | 916 | ret = add_extent_mapping(em_tree, em, 1); |
Chris Mason | 890871b | 2009-09-02 16:24:52 -0400 | [diff] [blame] | 917 | write_unlock(&em_tree->lock); |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 918 | if (ret != -EEXIST) { |
| 919 | free_extent_map(em); |
| 920 | break; |
| 921 | } |
| 922 | btrfs_drop_extent_cache(inode, start, |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 923 | start + ram_size - 1, 0); |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 924 | } |
Liu Bo | ace68ba | 2013-04-22 10:53:47 +0000 | [diff] [blame] | 925 | if (ret) |
| 926 | goto out_reserve; |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 927 | |
Chris Mason | 98d20f6 | 2008-04-14 09:46:10 -0400 | [diff] [blame] | 928 | cur_alloc_size = ins.offset; |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 929 | ret = btrfs_add_ordered_extent(inode, start, ins.objectid, |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 930 | ram_size, cur_alloc_size, 0); |
Liu Bo | ace68ba | 2013-04-22 10:53:47 +0000 | [diff] [blame] | 931 | if (ret) |
| 932 | goto out_reserve; |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 933 | |
Yan Zheng | 17d217f | 2008-12-12 10:03:38 -0500 | [diff] [blame] | 934 | if (root->root_key.objectid == |
| 935 | BTRFS_DATA_RELOC_TREE_OBJECTID) { |
| 936 | ret = btrfs_reloc_clone_csums(inode, start, |
| 937 | cur_alloc_size); |
Josef Bacik | 0036158 | 2013-08-14 14:02:47 -0400 | [diff] [blame] | 938 | if (ret) |
Liu Bo | ace68ba | 2013-04-22 10:53:47 +0000 | [diff] [blame] | 939 | goto out_reserve; |
Yan Zheng | 17d217f | 2008-12-12 10:03:38 -0500 | [diff] [blame] | 940 | } |
| 941 | |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 942 | if (disk_num_bytes < cur_alloc_size) |
Chris Mason | 3b95151 | 2008-04-17 11:29:12 -0400 | [diff] [blame] | 943 | break; |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 944 | |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 945 | /* we're not doing compressed IO, don't unlock the first |
| 946 | * page (which the caller expects to stay locked), don't |
| 947 | * clear any dirty bits and don't set any writeback bits |
Chris Mason | 8b62b72 | 2009-09-02 16:53:46 -0400 | [diff] [blame] | 948 | * |
| 949 | * Do set the Private2 bit so we know this page was properly |
| 950 | * setup for writepage |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 951 | */ |
Josef Bacik | c2790a2 | 2013-07-29 11:20:47 -0400 | [diff] [blame] | 952 | op = unlock ? PAGE_UNLOCK : 0; |
| 953 | op |= PAGE_SET_PRIVATE2; |
Chris Mason | a791e35 | 2009-10-08 11:27:10 -0400 | [diff] [blame] | 954 | |
Josef Bacik | c2790a2 | 2013-07-29 11:20:47 -0400 | [diff] [blame] | 955 | extent_clear_unlock_delalloc(inode, start, |
| 956 | start + ram_size - 1, locked_page, |
| 957 | EXTENT_LOCKED | EXTENT_DELALLOC, |
| 958 | op); |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 959 | disk_num_bytes -= cur_alloc_size; |
Chris Mason | c59f895 | 2007-12-17 20:14:04 -0500 | [diff] [blame] | 960 | num_bytes -= cur_alloc_size; |
| 961 | alloc_hint = ins.objectid + ins.offset; |
| 962 | start += cur_alloc_size; |
Chris Mason | b888db2 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 963 | } |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 964 | out: |
Chris Mason | be20aa9 | 2007-12-17 20:14:01 -0500 | [diff] [blame] | 965 | return ret; |
Miao Xie | b7d5b0a | 2012-11-01 07:32:18 +0000 | [diff] [blame] | 966 | |
Liu Bo | ace68ba | 2013-04-22 10:53:47 +0000 | [diff] [blame] | 967 | out_reserve: |
| 968 | btrfs_free_reserved_extent(root, ins.objectid, ins.offset); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 969 | out_unlock: |
Josef Bacik | c2790a2 | 2013-07-29 11:20:47 -0400 | [diff] [blame] | 970 | extent_clear_unlock_delalloc(inode, start, end, locked_page, |
Josef Bacik | 151a41b | 2013-07-29 13:22:24 -0400 | [diff] [blame] | 971 | EXTENT_LOCKED | EXTENT_DO_ACCOUNTING | |
| 972 | EXTENT_DELALLOC | EXTENT_DEFRAG, |
| 973 | PAGE_UNLOCK | PAGE_CLEAR_DIRTY | |
| 974 | PAGE_SET_WRITEBACK | PAGE_END_WRITEBACK); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 975 | goto out; |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 976 | } |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 977 | |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 978 | /* |
| 979 | * work queue call back to started compression on a file and pages |
| 980 | */ |
| 981 | static noinline void async_cow_start(struct btrfs_work *work) |
| 982 | { |
| 983 | struct async_cow *async_cow; |
| 984 | int num_added = 0; |
| 985 | async_cow = container_of(work, struct async_cow, work); |
| 986 | |
| 987 | compress_file_range(async_cow->inode, async_cow->locked_page, |
| 988 | async_cow->start, async_cow->end, async_cow, |
| 989 | &num_added); |
Josef Bacik | 8180ef8 | 2012-06-08 15:16:12 -0400 | [diff] [blame] | 990 | if (num_added == 0) { |
Josef Bacik | cb77fcd | 2012-06-15 12:19:48 -0600 | [diff] [blame] | 991 | btrfs_add_delayed_iput(async_cow->inode); |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 992 | async_cow->inode = NULL; |
Josef Bacik | 8180ef8 | 2012-06-08 15:16:12 -0400 | [diff] [blame] | 993 | } |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 994 | } |
| 995 | |
| 996 | /* |
| 997 | * work queue call back to submit previously compressed pages |
| 998 | */ |
| 999 | static noinline void async_cow_submit(struct btrfs_work *work) |
| 1000 | { |
| 1001 | struct async_cow *async_cow; |
| 1002 | struct btrfs_root *root; |
| 1003 | unsigned long nr_pages; |
| 1004 | |
| 1005 | async_cow = container_of(work, struct async_cow, work); |
| 1006 | |
| 1007 | root = async_cow->root; |
| 1008 | nr_pages = (async_cow->end - async_cow->start + PAGE_CACHE_SIZE) >> |
| 1009 | PAGE_CACHE_SHIFT; |
| 1010 | |
Josef Bacik | 66657b3 | 2012-08-01 15:36:24 -0400 | [diff] [blame] | 1011 | if (atomic_sub_return(nr_pages, &root->fs_info->async_delalloc_pages) < |
Liu Bo | 287082b | 2012-06-28 04:02:24 -0600 | [diff] [blame] | 1012 | 5 * 1024 * 1024 && |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 1013 | waitqueue_active(&root->fs_info->async_submit_wait)) |
| 1014 | wake_up(&root->fs_info->async_submit_wait); |
| 1015 | |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 1016 | if (async_cow->inode) |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 1017 | submit_compressed_extents(async_cow->inode, async_cow); |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 1018 | } |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 1019 | |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 1020 | static noinline void async_cow_free(struct btrfs_work *work) |
| 1021 | { |
| 1022 | struct async_cow *async_cow; |
| 1023 | async_cow = container_of(work, struct async_cow, work); |
Josef Bacik | 8180ef8 | 2012-06-08 15:16:12 -0400 | [diff] [blame] | 1024 | if (async_cow->inode) |
Josef Bacik | cb77fcd | 2012-06-15 12:19:48 -0600 | [diff] [blame] | 1025 | btrfs_add_delayed_iput(async_cow->inode); |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 1026 | kfree(async_cow); |
| 1027 | } |
| 1028 | |
| 1029 | static int cow_file_range_async(struct inode *inode, struct page *locked_page, |
| 1030 | u64 start, u64 end, int *page_started, |
| 1031 | unsigned long *nr_written) |
| 1032 | { |
| 1033 | struct async_cow *async_cow; |
| 1034 | struct btrfs_root *root = BTRFS_I(inode)->root; |
| 1035 | unsigned long nr_pages; |
| 1036 | u64 cur_end; |
Liu Bo | 287082b | 2012-06-28 04:02:24 -0600 | [diff] [blame] | 1037 | int limit = 10 * 1024 * 1024; |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 1038 | |
Chris Mason | a3429ab | 2009-10-08 12:30:20 -0400 | [diff] [blame] | 1039 | clear_extent_bit(&BTRFS_I(inode)->io_tree, start, end, EXTENT_LOCKED, |
| 1040 | 1, 0, NULL, GFP_NOFS); |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 1041 | while (start < end) { |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 1042 | async_cow = kmalloc(sizeof(*async_cow), GFP_NOFS); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 1043 | BUG_ON(!async_cow); /* -ENOMEM */ |
Josef Bacik | 8180ef8 | 2012-06-08 15:16:12 -0400 | [diff] [blame] | 1044 | async_cow->inode = igrab(inode); |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 1045 | async_cow->root = root; |
| 1046 | async_cow->locked_page = locked_page; |
| 1047 | async_cow->start = start; |
| 1048 | |
Christoph Hellwig | 6cbff00 | 2009-04-17 10:37:41 +0200 | [diff] [blame] | 1049 | if (BTRFS_I(inode)->flags & BTRFS_INODE_NOCOMPRESS) |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 1050 | cur_end = end; |
| 1051 | else |
| 1052 | cur_end = min(end, start + 512 * 1024 - 1); |
| 1053 | |
| 1054 | async_cow->end = cur_end; |
| 1055 | INIT_LIST_HEAD(&async_cow->extents); |
| 1056 | |
| 1057 | async_cow->work.func = async_cow_start; |
| 1058 | async_cow->work.ordered_func = async_cow_submit; |
| 1059 | async_cow->work.ordered_free = async_cow_free; |
| 1060 | async_cow->work.flags = 0; |
| 1061 | |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 1062 | nr_pages = (cur_end - start + PAGE_CACHE_SIZE) >> |
| 1063 | PAGE_CACHE_SHIFT; |
| 1064 | atomic_add(nr_pages, &root->fs_info->async_delalloc_pages); |
| 1065 | |
| 1066 | btrfs_queue_worker(&root->fs_info->delalloc_workers, |
| 1067 | &async_cow->work); |
| 1068 | |
| 1069 | if (atomic_read(&root->fs_info->async_delalloc_pages) > limit) { |
| 1070 | wait_event(root->fs_info->async_submit_wait, |
| 1071 | (atomic_read(&root->fs_info->async_delalloc_pages) < |
| 1072 | limit)); |
| 1073 | } |
| 1074 | |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 1075 | while (atomic_read(&root->fs_info->async_submit_draining) && |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 1076 | atomic_read(&root->fs_info->async_delalloc_pages)) { |
| 1077 | wait_event(root->fs_info->async_submit_wait, |
| 1078 | (atomic_read(&root->fs_info->async_delalloc_pages) == |
| 1079 | 0)); |
| 1080 | } |
| 1081 | |
| 1082 | *nr_written += nr_pages; |
| 1083 | start = cur_end + 1; |
| 1084 | } |
| 1085 | *page_started = 1; |
| 1086 | return 0; |
Chris Mason | be20aa9 | 2007-12-17 20:14:01 -0500 | [diff] [blame] | 1087 | } |
| 1088 | |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 1089 | static noinline int csum_exist_in_range(struct btrfs_root *root, |
Yan Zheng | 17d217f | 2008-12-12 10:03:38 -0500 | [diff] [blame] | 1090 | u64 bytenr, u64 num_bytes) |
| 1091 | { |
| 1092 | int ret; |
| 1093 | struct btrfs_ordered_sum *sums; |
| 1094 | LIST_HEAD(list); |
| 1095 | |
Yan Zheng | 07d400a | 2009-01-06 11:42:00 -0500 | [diff] [blame] | 1096 | ret = btrfs_lookup_csums_range(root->fs_info->csum_root, bytenr, |
Arne Jansen | a2de733 | 2011-03-08 14:14:00 +0100 | [diff] [blame] | 1097 | bytenr + num_bytes - 1, &list, 0); |
Yan Zheng | 17d217f | 2008-12-12 10:03:38 -0500 | [diff] [blame] | 1098 | if (ret == 0 && list_empty(&list)) |
| 1099 | return 0; |
| 1100 | |
| 1101 | while (!list_empty(&list)) { |
| 1102 | sums = list_entry(list.next, struct btrfs_ordered_sum, list); |
| 1103 | list_del(&sums->list); |
| 1104 | kfree(sums); |
| 1105 | } |
| 1106 | return 1; |
| 1107 | } |
| 1108 | |
Chris Mason | d352ac6 | 2008-09-29 15:18:18 -0400 | [diff] [blame] | 1109 | /* |
| 1110 | * when nowcow writeback call back. This checks for snapshots or COW copies |
| 1111 | * of the extents that exist in the file, and COWs the file as required. |
| 1112 | * |
| 1113 | * If no cow copies or snapshots exist, we write directly to the existing |
| 1114 | * blocks on disk |
| 1115 | */ |
Chris Mason | 7f366cf | 2009-03-12 20:12:45 -0400 | [diff] [blame] | 1116 | static noinline int run_delalloc_nocow(struct inode *inode, |
| 1117 | struct page *locked_page, |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 1118 | u64 start, u64 end, int *page_started, int force, |
| 1119 | unsigned long *nr_written) |
Chris Mason | be20aa9 | 2007-12-17 20:14:01 -0500 | [diff] [blame] | 1120 | { |
Chris Mason | be20aa9 | 2007-12-17 20:14:01 -0500 | [diff] [blame] | 1121 | struct btrfs_root *root = BTRFS_I(inode)->root; |
Yan Zheng | 7ea394f | 2008-08-05 13:05:02 -0400 | [diff] [blame] | 1122 | struct btrfs_trans_handle *trans; |
Chris Mason | be20aa9 | 2007-12-17 20:14:01 -0500 | [diff] [blame] | 1123 | struct extent_buffer *leaf; |
Chris Mason | be20aa9 | 2007-12-17 20:14:01 -0500 | [diff] [blame] | 1124 | struct btrfs_path *path; |
Yan Zheng | 80ff385 | 2008-10-30 14:20:02 -0400 | [diff] [blame] | 1125 | struct btrfs_file_extent_item *fi; |
Chris Mason | be20aa9 | 2007-12-17 20:14:01 -0500 | [diff] [blame] | 1126 | struct btrfs_key found_key; |
Yan Zheng | 80ff385 | 2008-10-30 14:20:02 -0400 | [diff] [blame] | 1127 | u64 cow_start; |
| 1128 | u64 cur_offset; |
| 1129 | u64 extent_end; |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 1130 | u64 extent_offset; |
Yan Zheng | 80ff385 | 2008-10-30 14:20:02 -0400 | [diff] [blame] | 1131 | u64 disk_bytenr; |
| 1132 | u64 num_bytes; |
Josef Bacik | b493968 | 2012-12-03 10:31:19 -0500 | [diff] [blame] | 1133 | u64 disk_num_bytes; |
Josef Bacik | cc95bef | 2013-04-04 14:31:27 -0400 | [diff] [blame] | 1134 | u64 ram_bytes; |
Yan Zheng | 80ff385 | 2008-10-30 14:20:02 -0400 | [diff] [blame] | 1135 | int extent_type; |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 1136 | int ret, err; |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 1137 | int type; |
Yan Zheng | 80ff385 | 2008-10-30 14:20:02 -0400 | [diff] [blame] | 1138 | int nocow; |
| 1139 | int check_prev = 1; |
Li Zefan | 82d5902 | 2011-04-20 10:33:24 +0800 | [diff] [blame] | 1140 | bool nolock; |
Li Zefan | 33345d01 | 2011-04-20 10:31:50 +0800 | [diff] [blame] | 1141 | u64 ino = btrfs_ino(inode); |
Chris Mason | be20aa9 | 2007-12-17 20:14:01 -0500 | [diff] [blame] | 1142 | |
| 1143 | path = btrfs_alloc_path(); |
Josef Bacik | 17ca04a | 2012-05-31 15:58:55 -0400 | [diff] [blame] | 1144 | if (!path) { |
Josef Bacik | c2790a2 | 2013-07-29 11:20:47 -0400 | [diff] [blame] | 1145 | extent_clear_unlock_delalloc(inode, start, end, locked_page, |
| 1146 | EXTENT_LOCKED | EXTENT_DELALLOC | |
Josef Bacik | 151a41b | 2013-07-29 13:22:24 -0400 | [diff] [blame] | 1147 | EXTENT_DO_ACCOUNTING | |
| 1148 | EXTENT_DEFRAG, PAGE_UNLOCK | |
Josef Bacik | c2790a2 | 2013-07-29 11:20:47 -0400 | [diff] [blame] | 1149 | PAGE_CLEAR_DIRTY | |
| 1150 | PAGE_SET_WRITEBACK | |
| 1151 | PAGE_END_WRITEBACK); |
Mark Fasheh | d8926bb | 2011-07-13 10:38:47 -0700 | [diff] [blame] | 1152 | return -ENOMEM; |
Josef Bacik | 17ca04a | 2012-05-31 15:58:55 -0400 | [diff] [blame] | 1153 | } |
Li Zefan | 82d5902 | 2011-04-20 10:33:24 +0800 | [diff] [blame] | 1154 | |
Liu Bo | 83eea1f | 2012-07-10 05:28:39 -0600 | [diff] [blame] | 1155 | nolock = btrfs_is_free_space_inode(inode); |
Li Zefan | 82d5902 | 2011-04-20 10:33:24 +0800 | [diff] [blame] | 1156 | |
| 1157 | if (nolock) |
Josef Bacik | 7a7eaa4 | 2011-04-13 12:54:33 -0400 | [diff] [blame] | 1158 | trans = btrfs_join_transaction_nolock(root); |
Li Zefan | 82d5902 | 2011-04-20 10:33:24 +0800 | [diff] [blame] | 1159 | else |
Josef Bacik | 7a7eaa4 | 2011-04-13 12:54:33 -0400 | [diff] [blame] | 1160 | trans = btrfs_join_transaction(root); |
Chris Mason | ff5714c | 2011-05-28 07:00:39 -0400 | [diff] [blame] | 1161 | |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 1162 | if (IS_ERR(trans)) { |
Josef Bacik | c2790a2 | 2013-07-29 11:20:47 -0400 | [diff] [blame] | 1163 | extent_clear_unlock_delalloc(inode, start, end, locked_page, |
| 1164 | EXTENT_LOCKED | EXTENT_DELALLOC | |
Josef Bacik | 151a41b | 2013-07-29 13:22:24 -0400 | [diff] [blame] | 1165 | EXTENT_DO_ACCOUNTING | |
| 1166 | EXTENT_DEFRAG, PAGE_UNLOCK | |
Josef Bacik | c2790a2 | 2013-07-29 11:20:47 -0400 | [diff] [blame] | 1167 | PAGE_CLEAR_DIRTY | |
| 1168 | PAGE_SET_WRITEBACK | |
| 1169 | PAGE_END_WRITEBACK); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 1170 | btrfs_free_path(path); |
| 1171 | return PTR_ERR(trans); |
| 1172 | } |
| 1173 | |
Josef Bacik | 74b2107 | 2011-04-13 12:02:53 -0400 | [diff] [blame] | 1174 | trans->block_rsv = &root->fs_info->delalloc_block_rsv; |
Chris Mason | be20aa9 | 2007-12-17 20:14:01 -0500 | [diff] [blame] | 1175 | |
Yan Zheng | 80ff385 | 2008-10-30 14:20:02 -0400 | [diff] [blame] | 1176 | cow_start = (u64)-1; |
| 1177 | cur_offset = start; |
| 1178 | while (1) { |
Li Zefan | 33345d01 | 2011-04-20 10:31:50 +0800 | [diff] [blame] | 1179 | ret = btrfs_lookup_file_extent(trans, root, path, ino, |
Yan Zheng | 80ff385 | 2008-10-30 14:20:02 -0400 | [diff] [blame] | 1180 | cur_offset, 0); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 1181 | if (ret < 0) { |
| 1182 | btrfs_abort_transaction(trans, root, ret); |
| 1183 | goto error; |
| 1184 | } |
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); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 1198 | if (ret < 0) { |
| 1199 | btrfs_abort_transaction(trans, root, ret); |
| 1200 | goto error; |
| 1201 | } |
Yan Zheng | 80ff385 | 2008-10-30 14:20:02 -0400 | [diff] [blame] | 1202 | if (ret > 0) |
| 1203 | break; |
| 1204 | leaf = path->nodes[0]; |
| 1205 | } |
Chris Mason | be20aa9 | 2007-12-17 20:14:01 -0500 | [diff] [blame] | 1206 | |
Yan Zheng | 80ff385 | 2008-10-30 14:20:02 -0400 | [diff] [blame] | 1207 | nocow = 0; |
| 1208 | disk_bytenr = 0; |
Yan Zheng | 17d217f | 2008-12-12 10:03:38 -0500 | [diff] [blame] | 1209 | num_bytes = 0; |
Yan Zheng | 80ff385 | 2008-10-30 14:20:02 -0400 | [diff] [blame] | 1210 | btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]); |
Chris Mason | be20aa9 | 2007-12-17 20:14:01 -0500 | [diff] [blame] | 1211 | |
Li Zefan | 33345d01 | 2011-04-20 10:31:50 +0800 | [diff] [blame] | 1212 | if (found_key.objectid > ino || |
Yan Zheng | 80ff385 | 2008-10-30 14:20:02 -0400 | [diff] [blame] | 1213 | found_key.type > BTRFS_EXTENT_DATA_KEY || |
| 1214 | found_key.offset > end) |
| 1215 | break; |
Chris Mason | be20aa9 | 2007-12-17 20:14:01 -0500 | [diff] [blame] | 1216 | |
Yan Zheng | 80ff385 | 2008-10-30 14:20:02 -0400 | [diff] [blame] | 1217 | if (found_key.offset > cur_offset) { |
| 1218 | extent_end = found_key.offset; |
Chris Mason | e9061e2 | 2009-10-09 09:57:45 -0400 | [diff] [blame] | 1219 | extent_type = 0; |
Yan Zheng | 80ff385 | 2008-10-30 14:20:02 -0400 | [diff] [blame] | 1220 | goto out_check; |
| 1221 | } |
Chris Mason | c31f883 | 2008-01-08 15:46:31 -0500 | [diff] [blame] | 1222 | |
Yan Zheng | 80ff385 | 2008-10-30 14:20:02 -0400 | [diff] [blame] | 1223 | fi = btrfs_item_ptr(leaf, path->slots[0], |
| 1224 | struct btrfs_file_extent_item); |
| 1225 | extent_type = btrfs_file_extent_type(leaf, fi); |
Chris Mason | be20aa9 | 2007-12-17 20:14:01 -0500 | [diff] [blame] | 1226 | |
Josef Bacik | cc95bef | 2013-04-04 14:31:27 -0400 | [diff] [blame] | 1227 | ram_bytes = btrfs_file_extent_ram_bytes(leaf, fi); |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 1228 | if (extent_type == BTRFS_FILE_EXTENT_REG || |
| 1229 | extent_type == BTRFS_FILE_EXTENT_PREALLOC) { |
Yan Zheng | 80ff385 | 2008-10-30 14:20:02 -0400 | [diff] [blame] | 1230 | disk_bytenr = btrfs_file_extent_disk_bytenr(leaf, fi); |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 1231 | extent_offset = btrfs_file_extent_offset(leaf, fi); |
Yan Zheng | 80ff385 | 2008-10-30 14:20:02 -0400 | [diff] [blame] | 1232 | extent_end = found_key.offset + |
| 1233 | btrfs_file_extent_num_bytes(leaf, fi); |
Josef Bacik | b493968 | 2012-12-03 10:31:19 -0500 | [diff] [blame] | 1234 | disk_num_bytes = |
| 1235 | btrfs_file_extent_disk_num_bytes(leaf, fi); |
Yan Zheng | 80ff385 | 2008-10-30 14:20:02 -0400 | [diff] [blame] | 1236 | if (extent_end <= start) { |
| 1237 | path->slots[0]++; |
| 1238 | goto next_slot; |
| 1239 | } |
Yan Zheng | 17d217f | 2008-12-12 10:03:38 -0500 | [diff] [blame] | 1240 | if (disk_bytenr == 0) |
| 1241 | goto out_check; |
Yan Zheng | 80ff385 | 2008-10-30 14:20:02 -0400 | [diff] [blame] | 1242 | if (btrfs_file_extent_compression(leaf, fi) || |
| 1243 | btrfs_file_extent_encryption(leaf, fi) || |
| 1244 | btrfs_file_extent_other_encoding(leaf, fi)) |
| 1245 | goto out_check; |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 1246 | if (extent_type == BTRFS_FILE_EXTENT_REG && !force) |
| 1247 | goto out_check; |
Yan Zheng | d2fb343 | 2008-12-11 16:30:39 -0500 | [diff] [blame] | 1248 | if (btrfs_extent_readonly(root, disk_bytenr)) |
Yan Zheng | 80ff385 | 2008-10-30 14:20:02 -0400 | [diff] [blame] | 1249 | goto out_check; |
Li Zefan | 33345d01 | 2011-04-20 10:31:50 +0800 | [diff] [blame] | 1250 | if (btrfs_cross_ref_exist(trans, root, ino, |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 1251 | found_key.offset - |
| 1252 | extent_offset, disk_bytenr)) |
Yan Zheng | 17d217f | 2008-12-12 10:03:38 -0500 | [diff] [blame] | 1253 | goto out_check; |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 1254 | disk_bytenr += extent_offset; |
Yan Zheng | 17d217f | 2008-12-12 10:03:38 -0500 | [diff] [blame] | 1255 | disk_bytenr += cur_offset - found_key.offset; |
| 1256 | num_bytes = min(end + 1, extent_end) - cur_offset; |
| 1257 | /* |
| 1258 | * force cow if csum exists in the range. |
| 1259 | * this ensure that csum for a given extent are |
| 1260 | * either valid or do not exist. |
| 1261 | */ |
| 1262 | if (csum_exist_in_range(root, disk_bytenr, num_bytes)) |
| 1263 | goto out_check; |
Yan Zheng | 80ff385 | 2008-10-30 14:20:02 -0400 | [diff] [blame] | 1264 | nocow = 1; |
| 1265 | } else if (extent_type == BTRFS_FILE_EXTENT_INLINE) { |
| 1266 | extent_end = found_key.offset + |
| 1267 | btrfs_file_extent_inline_len(leaf, fi); |
| 1268 | extent_end = ALIGN(extent_end, root->sectorsize); |
| 1269 | } else { |
| 1270 | BUG_ON(1); |
| 1271 | } |
| 1272 | out_check: |
| 1273 | if (extent_end <= start) { |
| 1274 | path->slots[0]++; |
| 1275 | goto next_slot; |
| 1276 | } |
| 1277 | if (!nocow) { |
| 1278 | if (cow_start == (u64)-1) |
| 1279 | cow_start = cur_offset; |
| 1280 | cur_offset = extent_end; |
| 1281 | if (cur_offset > end) |
| 1282 | break; |
| 1283 | path->slots[0]++; |
| 1284 | goto next_slot; |
Yan Zheng | 7ea394f | 2008-08-05 13:05:02 -0400 | [diff] [blame] | 1285 | } |
Chris Mason | be20aa9 | 2007-12-17 20:14:01 -0500 | [diff] [blame] | 1286 | |
David Sterba | b3b4aa7 | 2011-04-21 01:20:15 +0200 | [diff] [blame] | 1287 | btrfs_release_path(path); |
Yan Zheng | 80ff385 | 2008-10-30 14:20:02 -0400 | [diff] [blame] | 1288 | if (cow_start != (u64)-1) { |
Josef Bacik | 0036158 | 2013-08-14 14:02:47 -0400 | [diff] [blame] | 1289 | ret = cow_file_range(inode, locked_page, |
| 1290 | cow_start, found_key.offset - 1, |
| 1291 | page_started, nr_written, 1); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 1292 | if (ret) { |
| 1293 | btrfs_abort_transaction(trans, root, ret); |
| 1294 | goto error; |
| 1295 | } |
Yan Zheng | 80ff385 | 2008-10-30 14:20:02 -0400 | [diff] [blame] | 1296 | cow_start = (u64)-1; |
Yan Zheng | 7ea394f | 2008-08-05 13:05:02 -0400 | [diff] [blame] | 1297 | } |
Yan Zheng | 80ff385 | 2008-10-30 14:20:02 -0400 | [diff] [blame] | 1298 | |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 1299 | if (extent_type == BTRFS_FILE_EXTENT_PREALLOC) { |
| 1300 | struct extent_map *em; |
| 1301 | struct extent_map_tree *em_tree; |
| 1302 | em_tree = &BTRFS_I(inode)->extent_tree; |
David Sterba | 172ddd6 | 2011-04-21 00:48:27 +0200 | [diff] [blame] | 1303 | em = alloc_extent_map(); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 1304 | BUG_ON(!em); /* -ENOMEM */ |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 1305 | em->start = cur_offset; |
Josef Bacik | 70c8a91 | 2012-10-11 16:54:30 -0400 | [diff] [blame] | 1306 | em->orig_start = found_key.offset - extent_offset; |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 1307 | em->len = num_bytes; |
| 1308 | em->block_len = num_bytes; |
| 1309 | em->block_start = disk_bytenr; |
Josef Bacik | b493968 | 2012-12-03 10:31:19 -0500 | [diff] [blame] | 1310 | em->orig_block_len = disk_num_bytes; |
Josef Bacik | cc95bef | 2013-04-04 14:31:27 -0400 | [diff] [blame] | 1311 | em->ram_bytes = ram_bytes; |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 1312 | em->bdev = root->fs_info->fs_devices->latest_bdev; |
Josef Bacik | 2ab28f3 | 2012-10-12 15:27:49 -0400 | [diff] [blame] | 1313 | em->mod_start = em->start; |
| 1314 | em->mod_len = em->len; |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 1315 | set_bit(EXTENT_FLAG_PINNED, &em->flags); |
Josef Bacik | b11e234 | 2012-12-03 10:58:15 -0500 | [diff] [blame] | 1316 | set_bit(EXTENT_FLAG_FILLING, &em->flags); |
Josef Bacik | 70c8a91 | 2012-10-11 16:54:30 -0400 | [diff] [blame] | 1317 | em->generation = -1; |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 1318 | while (1) { |
Chris Mason | 890871b | 2009-09-02 16:24:52 -0400 | [diff] [blame] | 1319 | write_lock(&em_tree->lock); |
Josef Bacik | 09a2a8f9 | 2013-04-05 16:51:15 -0400 | [diff] [blame] | 1320 | ret = add_extent_mapping(em_tree, em, 1); |
Chris Mason | 890871b | 2009-09-02 16:24:52 -0400 | [diff] [blame] | 1321 | write_unlock(&em_tree->lock); |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 1322 | if (ret != -EEXIST) { |
| 1323 | free_extent_map(em); |
| 1324 | break; |
| 1325 | } |
| 1326 | btrfs_drop_extent_cache(inode, em->start, |
| 1327 | em->start + em->len - 1, 0); |
| 1328 | } |
| 1329 | type = BTRFS_ORDERED_PREALLOC; |
| 1330 | } else { |
| 1331 | type = BTRFS_ORDERED_NOCOW; |
| 1332 | } |
Yan Zheng | 80ff385 | 2008-10-30 14:20:02 -0400 | [diff] [blame] | 1333 | |
| 1334 | ret = btrfs_add_ordered_extent(inode, cur_offset, disk_bytenr, |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 1335 | num_bytes, num_bytes, type); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 1336 | BUG_ON(ret); /* -ENOMEM */ |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 1337 | |
Yan, Zheng | efa5646 | 2010-05-16 10:49:59 -0400 | [diff] [blame] | 1338 | if (root->root_key.objectid == |
| 1339 | BTRFS_DATA_RELOC_TREE_OBJECTID) { |
| 1340 | ret = btrfs_reloc_clone_csums(inode, cur_offset, |
| 1341 | num_bytes); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 1342 | if (ret) { |
| 1343 | btrfs_abort_transaction(trans, root, ret); |
| 1344 | goto error; |
| 1345 | } |
Yan, Zheng | efa5646 | 2010-05-16 10:49:59 -0400 | [diff] [blame] | 1346 | } |
| 1347 | |
Josef Bacik | c2790a2 | 2013-07-29 11:20:47 -0400 | [diff] [blame] | 1348 | extent_clear_unlock_delalloc(inode, cur_offset, |
| 1349 | cur_offset + num_bytes - 1, |
| 1350 | locked_page, EXTENT_LOCKED | |
| 1351 | EXTENT_DELALLOC, PAGE_UNLOCK | |
| 1352 | PAGE_SET_PRIVATE2); |
Yan Zheng | 80ff385 | 2008-10-30 14:20:02 -0400 | [diff] [blame] | 1353 | cur_offset = extent_end; |
| 1354 | if (cur_offset > end) |
| 1355 | break; |
Yan Zheng | 7ea394f | 2008-08-05 13:05:02 -0400 | [diff] [blame] | 1356 | } |
David Sterba | b3b4aa7 | 2011-04-21 01:20:15 +0200 | [diff] [blame] | 1357 | btrfs_release_path(path); |
Yan Zheng | 80ff385 | 2008-10-30 14:20:02 -0400 | [diff] [blame] | 1358 | |
Josef Bacik | 17ca04a | 2012-05-31 15:58:55 -0400 | [diff] [blame] | 1359 | if (cur_offset <= end && cow_start == (u64)-1) { |
Yan Zheng | 80ff385 | 2008-10-30 14:20:02 -0400 | [diff] [blame] | 1360 | cow_start = cur_offset; |
Josef Bacik | 17ca04a | 2012-05-31 15:58:55 -0400 | [diff] [blame] | 1361 | cur_offset = end; |
| 1362 | } |
| 1363 | |
Yan Zheng | 80ff385 | 2008-10-30 14:20:02 -0400 | [diff] [blame] | 1364 | if (cow_start != (u64)-1) { |
Josef Bacik | 0036158 | 2013-08-14 14:02:47 -0400 | [diff] [blame] | 1365 | ret = cow_file_range(inode, locked_page, cow_start, end, |
| 1366 | page_started, nr_written, 1); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 1367 | if (ret) { |
| 1368 | btrfs_abort_transaction(trans, root, ret); |
| 1369 | goto error; |
| 1370 | } |
Yan Zheng | 80ff385 | 2008-10-30 14:20:02 -0400 | [diff] [blame] | 1371 | } |
| 1372 | |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 1373 | error: |
Miao Xie | a698d075 | 2012-09-20 01:51:59 -0600 | [diff] [blame] | 1374 | err = btrfs_end_transaction(trans, root); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 1375 | if (!ret) |
| 1376 | ret = err; |
| 1377 | |
Josef Bacik | 17ca04a | 2012-05-31 15:58:55 -0400 | [diff] [blame] | 1378 | if (ret && cur_offset < end) |
Josef Bacik | c2790a2 | 2013-07-29 11:20:47 -0400 | [diff] [blame] | 1379 | extent_clear_unlock_delalloc(inode, cur_offset, end, |
| 1380 | locked_page, EXTENT_LOCKED | |
Josef Bacik | 151a41b | 2013-07-29 13:22:24 -0400 | [diff] [blame] | 1381 | EXTENT_DELALLOC | EXTENT_DEFRAG | |
| 1382 | EXTENT_DO_ACCOUNTING, PAGE_UNLOCK | |
| 1383 | PAGE_CLEAR_DIRTY | |
Josef Bacik | c2790a2 | 2013-07-29 11:20:47 -0400 | [diff] [blame] | 1384 | PAGE_SET_WRITEBACK | |
| 1385 | PAGE_END_WRITEBACK); |
Yan Zheng | 7ea394f | 2008-08-05 13:05:02 -0400 | [diff] [blame] | 1386 | btrfs_free_path(path); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 1387 | return ret; |
Chris Mason | be20aa9 | 2007-12-17 20:14:01 -0500 | [diff] [blame] | 1388 | } |
| 1389 | |
Chris Mason | d352ac6 | 2008-09-29 15:18:18 -0400 | [diff] [blame] | 1390 | /* |
| 1391 | * extent_io.c call back to do delayed allocation processing |
| 1392 | */ |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 1393 | static int run_delalloc_range(struct inode *inode, struct page *locked_page, |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 1394 | u64 start, u64 end, int *page_started, |
| 1395 | unsigned long *nr_written) |
Chris Mason | be20aa9 | 2007-12-17 20:14:01 -0500 | [diff] [blame] | 1396 | { |
Chris Mason | be20aa9 | 2007-12-17 20:14:01 -0500 | [diff] [blame] | 1397 | int ret; |
Chris Mason | 7f366cf | 2009-03-12 20:12:45 -0400 | [diff] [blame] | 1398 | struct btrfs_root *root = BTRFS_I(inode)->root; |
Chris Mason | a213501 | 2008-06-25 16:01:30 -0400 | [diff] [blame] | 1399 | |
Josef Bacik | 7ddf5a4 | 2012-06-08 15:26:47 -0400 | [diff] [blame] | 1400 | if (BTRFS_I(inode)->flags & BTRFS_INODE_NODATACOW) { |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 1401 | ret = run_delalloc_nocow(inode, locked_page, start, end, |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 1402 | page_started, 1, nr_written); |
Josef Bacik | 7ddf5a4 | 2012-06-08 15:26:47 -0400 | [diff] [blame] | 1403 | } else if (BTRFS_I(inode)->flags & BTRFS_INODE_PREALLOC) { |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 1404 | ret = run_delalloc_nocow(inode, locked_page, start, end, |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 1405 | page_started, 0, nr_written); |
Josef Bacik | 7ddf5a4 | 2012-06-08 15:26:47 -0400 | [diff] [blame] | 1406 | } else if (!btrfs_test_opt(root, COMPRESS) && |
| 1407 | !(BTRFS_I(inode)->force_compress) && |
| 1408 | !(BTRFS_I(inode)->flags & BTRFS_INODE_COMPRESS)) { |
Chris Mason | 7f366cf | 2009-03-12 20:12:45 -0400 | [diff] [blame] | 1409 | ret = cow_file_range(inode, locked_page, start, end, |
| 1410 | page_started, nr_written, 1); |
Josef Bacik | 7ddf5a4 | 2012-06-08 15:26:47 -0400 | [diff] [blame] | 1411 | } else { |
| 1412 | set_bit(BTRFS_INODE_HAS_ASYNC_EXTENT, |
| 1413 | &BTRFS_I(inode)->runtime_flags); |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 1414 | ret = cow_file_range_async(inode, locked_page, start, end, |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 1415 | page_started, nr_written); |
Josef Bacik | 7ddf5a4 | 2012-06-08 15:26:47 -0400 | [diff] [blame] | 1416 | } |
Chris Mason | b888db2 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 1417 | return ret; |
| 1418 | } |
| 1419 | |
Jeff Mahoney | 1bf8504 | 2011-07-21 16:56:09 +0000 | [diff] [blame] | 1420 | static void btrfs_split_extent_hook(struct inode *inode, |
| 1421 | struct extent_state *orig, u64 split) |
Josef Bacik | 9ed74f2 | 2009-09-11 16:12:44 -0400 | [diff] [blame] | 1422 | { |
Yan, Zheng | 0ca1f7c | 2010-05-16 10:48:47 -0400 | [diff] [blame] | 1423 | /* not delalloc, ignore it */ |
Josef Bacik | 9ed74f2 | 2009-09-11 16:12:44 -0400 | [diff] [blame] | 1424 | if (!(orig->state & EXTENT_DELALLOC)) |
Jeff Mahoney | 1bf8504 | 2011-07-21 16:56:09 +0000 | [diff] [blame] | 1425 | return; |
Josef Bacik | 9ed74f2 | 2009-09-11 16:12:44 -0400 | [diff] [blame] | 1426 | |
Josef Bacik | 9e0baf6 | 2011-07-15 15:16:44 +0000 | [diff] [blame] | 1427 | spin_lock(&BTRFS_I(inode)->lock); |
| 1428 | BTRFS_I(inode)->outstanding_extents++; |
| 1429 | spin_unlock(&BTRFS_I(inode)->lock); |
Josef Bacik | 9ed74f2 | 2009-09-11 16:12:44 -0400 | [diff] [blame] | 1430 | } |
| 1431 | |
| 1432 | /* |
| 1433 | * extent_io.c merge_extent_hook, used to track merged delayed allocation |
| 1434 | * extents so we can keep track of new extents that are just merged onto old |
| 1435 | * extents, such as when we are doing sequential writes, so we can properly |
| 1436 | * account for the metadata space we'll need. |
| 1437 | */ |
Jeff Mahoney | 1bf8504 | 2011-07-21 16:56:09 +0000 | [diff] [blame] | 1438 | static void btrfs_merge_extent_hook(struct inode *inode, |
| 1439 | struct extent_state *new, |
| 1440 | struct extent_state *other) |
Josef Bacik | 9ed74f2 | 2009-09-11 16:12:44 -0400 | [diff] [blame] | 1441 | { |
Josef Bacik | 9ed74f2 | 2009-09-11 16:12:44 -0400 | [diff] [blame] | 1442 | /* not delalloc, ignore it */ |
| 1443 | if (!(other->state & EXTENT_DELALLOC)) |
Jeff Mahoney | 1bf8504 | 2011-07-21 16:56:09 +0000 | [diff] [blame] | 1444 | return; |
Josef Bacik | 9ed74f2 | 2009-09-11 16:12:44 -0400 | [diff] [blame] | 1445 | |
Josef Bacik | 9e0baf6 | 2011-07-15 15:16:44 +0000 | [diff] [blame] | 1446 | spin_lock(&BTRFS_I(inode)->lock); |
| 1447 | BTRFS_I(inode)->outstanding_extents--; |
| 1448 | spin_unlock(&BTRFS_I(inode)->lock); |
Josef Bacik | 9ed74f2 | 2009-09-11 16:12:44 -0400 | [diff] [blame] | 1449 | } |
| 1450 | |
Miao Xie | eb73c1b | 2013-05-15 07:48:22 +0000 | [diff] [blame] | 1451 | static void btrfs_add_delalloc_inodes(struct btrfs_root *root, |
| 1452 | struct inode *inode) |
| 1453 | { |
| 1454 | spin_lock(&root->delalloc_lock); |
| 1455 | if (list_empty(&BTRFS_I(inode)->delalloc_inodes)) { |
| 1456 | list_add_tail(&BTRFS_I(inode)->delalloc_inodes, |
| 1457 | &root->delalloc_inodes); |
| 1458 | set_bit(BTRFS_INODE_IN_DELALLOC_LIST, |
| 1459 | &BTRFS_I(inode)->runtime_flags); |
| 1460 | root->nr_delalloc_inodes++; |
| 1461 | if (root->nr_delalloc_inodes == 1) { |
| 1462 | spin_lock(&root->fs_info->delalloc_root_lock); |
| 1463 | BUG_ON(!list_empty(&root->delalloc_root)); |
| 1464 | list_add_tail(&root->delalloc_root, |
| 1465 | &root->fs_info->delalloc_roots); |
| 1466 | spin_unlock(&root->fs_info->delalloc_root_lock); |
| 1467 | } |
| 1468 | } |
| 1469 | spin_unlock(&root->delalloc_lock); |
| 1470 | } |
| 1471 | |
| 1472 | static void btrfs_del_delalloc_inode(struct btrfs_root *root, |
| 1473 | struct inode *inode) |
| 1474 | { |
| 1475 | spin_lock(&root->delalloc_lock); |
| 1476 | if (!list_empty(&BTRFS_I(inode)->delalloc_inodes)) { |
| 1477 | list_del_init(&BTRFS_I(inode)->delalloc_inodes); |
| 1478 | clear_bit(BTRFS_INODE_IN_DELALLOC_LIST, |
| 1479 | &BTRFS_I(inode)->runtime_flags); |
| 1480 | root->nr_delalloc_inodes--; |
| 1481 | if (!root->nr_delalloc_inodes) { |
| 1482 | spin_lock(&root->fs_info->delalloc_root_lock); |
| 1483 | BUG_ON(list_empty(&root->delalloc_root)); |
| 1484 | list_del_init(&root->delalloc_root); |
| 1485 | spin_unlock(&root->fs_info->delalloc_root_lock); |
| 1486 | } |
| 1487 | } |
| 1488 | spin_unlock(&root->delalloc_lock); |
| 1489 | } |
| 1490 | |
Chris Mason | d352ac6 | 2008-09-29 15:18:18 -0400 | [diff] [blame] | 1491 | /* |
| 1492 | * extent_io.c set_bit_hook, used to track delayed allocation |
| 1493 | * bytes in this file, and to maintain the list of inodes that |
| 1494 | * have pending delalloc work to be done. |
| 1495 | */ |
Jeff Mahoney | 1bf8504 | 2011-07-21 16:56:09 +0000 | [diff] [blame] | 1496 | static void btrfs_set_bit_hook(struct inode *inode, |
David Sterba | 4107488 | 2013-04-29 13:38:46 +0000 | [diff] [blame] | 1497 | struct extent_state *state, unsigned long *bits) |
Chris Mason | 291d673 | 2008-01-29 15:55:23 -0500 | [diff] [blame] | 1498 | { |
Josef Bacik | 9ed74f2 | 2009-09-11 16:12:44 -0400 | [diff] [blame] | 1499 | |
Chris Mason | 75eff68 | 2008-12-15 15:54:40 -0500 | [diff] [blame] | 1500 | /* |
| 1501 | * set_bit and clear bit hooks normally require _irqsave/restore |
Sergei Trofimovich | 27160b6 | 2011-05-20 20:20:32 +0000 | [diff] [blame] | 1502 | * but in this case, we are only testing for the DELALLOC |
Chris Mason | 75eff68 | 2008-12-15 15:54:40 -0500 | [diff] [blame] | 1503 | * bit, which is only set or cleared with irqs on |
| 1504 | */ |
Yan, Zheng | 0ca1f7c | 2010-05-16 10:48:47 -0400 | [diff] [blame] | 1505 | if (!(state->state & EXTENT_DELALLOC) && (*bits & EXTENT_DELALLOC)) { |
Chris Mason | 291d673 | 2008-01-29 15:55:23 -0500 | [diff] [blame] | 1506 | struct btrfs_root *root = BTRFS_I(inode)->root; |
Yan, Zheng | 0ca1f7c | 2010-05-16 10:48:47 -0400 | [diff] [blame] | 1507 | u64 len = state->end + 1 - state->start; |
Liu Bo | 83eea1f | 2012-07-10 05:28:39 -0600 | [diff] [blame] | 1508 | bool do_list = !btrfs_is_free_space_inode(inode); |
Josef Bacik | 9ed74f2 | 2009-09-11 16:12:44 -0400 | [diff] [blame] | 1509 | |
Josef Bacik | 9e0baf6 | 2011-07-15 15:16:44 +0000 | [diff] [blame] | 1510 | if (*bits & EXTENT_FIRST_DELALLOC) { |
Yan, Zheng | 0ca1f7c | 2010-05-16 10:48:47 -0400 | [diff] [blame] | 1511 | *bits &= ~EXTENT_FIRST_DELALLOC; |
Josef Bacik | 9e0baf6 | 2011-07-15 15:16:44 +0000 | [diff] [blame] | 1512 | } else { |
| 1513 | spin_lock(&BTRFS_I(inode)->lock); |
| 1514 | BTRFS_I(inode)->outstanding_extents++; |
| 1515 | spin_unlock(&BTRFS_I(inode)->lock); |
| 1516 | } |
Josef Bacik | 287a0ab | 2010-03-19 18:07:23 +0000 | [diff] [blame] | 1517 | |
Miao Xie | 963d678 | 2013-01-29 10:10:51 +0000 | [diff] [blame] | 1518 | __percpu_counter_add(&root->fs_info->delalloc_bytes, len, |
| 1519 | root->fs_info->delalloc_batch); |
Miao Xie | df0af1a | 2013-01-29 10:11:59 +0000 | [diff] [blame] | 1520 | spin_lock(&BTRFS_I(inode)->lock); |
Yan, Zheng | 0ca1f7c | 2010-05-16 10:48:47 -0400 | [diff] [blame] | 1521 | BTRFS_I(inode)->delalloc_bytes += len; |
Miao Xie | df0af1a | 2013-01-29 10:11:59 +0000 | [diff] [blame] | 1522 | if (do_list && !test_bit(BTRFS_INODE_IN_DELALLOC_LIST, |
Miao Xie | eb73c1b | 2013-05-15 07:48:22 +0000 | [diff] [blame] | 1523 | &BTRFS_I(inode)->runtime_flags)) |
| 1524 | btrfs_add_delalloc_inodes(root, inode); |
Miao Xie | df0af1a | 2013-01-29 10:11:59 +0000 | [diff] [blame] | 1525 | spin_unlock(&BTRFS_I(inode)->lock); |
Chris Mason | 291d673 | 2008-01-29 15:55:23 -0500 | [diff] [blame] | 1526 | } |
Chris Mason | 291d673 | 2008-01-29 15:55:23 -0500 | [diff] [blame] | 1527 | } |
| 1528 | |
Chris Mason | d352ac6 | 2008-09-29 15:18:18 -0400 | [diff] [blame] | 1529 | /* |
| 1530 | * extent_io.c clear_bit_hook, see set_bit_hook for why |
| 1531 | */ |
Jeff Mahoney | 1bf8504 | 2011-07-21 16:56:09 +0000 | [diff] [blame] | 1532 | static void btrfs_clear_bit_hook(struct inode *inode, |
David Sterba | 4107488 | 2013-04-29 13:38:46 +0000 | [diff] [blame] | 1533 | struct extent_state *state, |
| 1534 | unsigned long *bits) |
Chris Mason | 291d673 | 2008-01-29 15:55:23 -0500 | [diff] [blame] | 1535 | { |
Chris Mason | 75eff68 | 2008-12-15 15:54:40 -0500 | [diff] [blame] | 1536 | /* |
| 1537 | * set_bit and clear bit hooks normally require _irqsave/restore |
Sergei Trofimovich | 27160b6 | 2011-05-20 20:20:32 +0000 | [diff] [blame] | 1538 | * but in this case, we are only testing for the DELALLOC |
Chris Mason | 75eff68 | 2008-12-15 15:54:40 -0500 | [diff] [blame] | 1539 | * bit, which is only set or cleared with irqs on |
| 1540 | */ |
Yan, Zheng | 0ca1f7c | 2010-05-16 10:48:47 -0400 | [diff] [blame] | 1541 | if ((state->state & EXTENT_DELALLOC) && (*bits & EXTENT_DELALLOC)) { |
Chris Mason | 291d673 | 2008-01-29 15:55:23 -0500 | [diff] [blame] | 1542 | struct btrfs_root *root = BTRFS_I(inode)->root; |
Yan, Zheng | 0ca1f7c | 2010-05-16 10:48:47 -0400 | [diff] [blame] | 1543 | u64 len = state->end + 1 - state->start; |
Liu Bo | 83eea1f | 2012-07-10 05:28:39 -0600 | [diff] [blame] | 1544 | bool do_list = !btrfs_is_free_space_inode(inode); |
Chris Mason | bcbfce8 | 2008-04-22 13:26:47 -0400 | [diff] [blame] | 1545 | |
Josef Bacik | 9e0baf6 | 2011-07-15 15:16:44 +0000 | [diff] [blame] | 1546 | if (*bits & EXTENT_FIRST_DELALLOC) { |
Yan, Zheng | 0ca1f7c | 2010-05-16 10:48:47 -0400 | [diff] [blame] | 1547 | *bits &= ~EXTENT_FIRST_DELALLOC; |
Josef Bacik | 9e0baf6 | 2011-07-15 15:16:44 +0000 | [diff] [blame] | 1548 | } else if (!(*bits & EXTENT_DO_ACCOUNTING)) { |
| 1549 | spin_lock(&BTRFS_I(inode)->lock); |
| 1550 | BTRFS_I(inode)->outstanding_extents--; |
| 1551 | spin_unlock(&BTRFS_I(inode)->lock); |
| 1552 | } |
Yan, Zheng | 0ca1f7c | 2010-05-16 10:48:47 -0400 | [diff] [blame] | 1553 | |
Josef Bacik | b6d08f0 | 2013-09-27 14:57:43 -0400 | [diff] [blame] | 1554 | /* |
| 1555 | * We don't reserve metadata space for space cache inodes so we |
| 1556 | * don't need to call dellalloc_release_metadata if there is an |
| 1557 | * error. |
| 1558 | */ |
| 1559 | if (*bits & EXTENT_DO_ACCOUNTING && |
| 1560 | root != root->fs_info->tree_root) |
Yan, Zheng | 0ca1f7c | 2010-05-16 10:48:47 -0400 | [diff] [blame] | 1561 | btrfs_delalloc_release_metadata(inode, len); |
| 1562 | |
Josef Bacik | 0cb59c9 | 2010-07-02 12:14:14 -0400 | [diff] [blame] | 1563 | if (root->root_key.objectid != BTRFS_DATA_RELOC_TREE_OBJECTID |
Josef Bacik | 7ee9e44 | 2013-06-21 16:37:03 -0400 | [diff] [blame] | 1564 | && do_list && !(state->state & EXTENT_NORESERVE)) |
Yan, Zheng | 0ca1f7c | 2010-05-16 10:48:47 -0400 | [diff] [blame] | 1565 | btrfs_free_reserved_data_space(inode, len); |
Josef Bacik | 9ed74f2 | 2009-09-11 16:12:44 -0400 | [diff] [blame] | 1566 | |
Miao Xie | 963d678 | 2013-01-29 10:10:51 +0000 | [diff] [blame] | 1567 | __percpu_counter_add(&root->fs_info->delalloc_bytes, -len, |
| 1568 | root->fs_info->delalloc_batch); |
Miao Xie | df0af1a | 2013-01-29 10:11:59 +0000 | [diff] [blame] | 1569 | spin_lock(&BTRFS_I(inode)->lock); |
Yan, Zheng | 0ca1f7c | 2010-05-16 10:48:47 -0400 | [diff] [blame] | 1570 | BTRFS_I(inode)->delalloc_bytes -= len; |
Josef Bacik | 0cb59c9 | 2010-07-02 12:14:14 -0400 | [diff] [blame] | 1571 | if (do_list && BTRFS_I(inode)->delalloc_bytes == 0 && |
Miao Xie | df0af1a | 2013-01-29 10:11:59 +0000 | [diff] [blame] | 1572 | test_bit(BTRFS_INODE_IN_DELALLOC_LIST, |
Miao Xie | eb73c1b | 2013-05-15 07:48:22 +0000 | [diff] [blame] | 1573 | &BTRFS_I(inode)->runtime_flags)) |
| 1574 | btrfs_del_delalloc_inode(root, inode); |
Miao Xie | df0af1a | 2013-01-29 10:11:59 +0000 | [diff] [blame] | 1575 | spin_unlock(&BTRFS_I(inode)->lock); |
Chris Mason | 291d673 | 2008-01-29 15:55:23 -0500 | [diff] [blame] | 1576 | } |
Chris Mason | 291d673 | 2008-01-29 15:55:23 -0500 | [diff] [blame] | 1577 | } |
| 1578 | |
Chris Mason | d352ac6 | 2008-09-29 15:18:18 -0400 | [diff] [blame] | 1579 | /* |
| 1580 | * extent_io.c merge_bio_hook, this must check the chunk tree to make sure |
| 1581 | * we don't create bios that span stripes or chunks |
| 1582 | */ |
David Woodhouse | 64a1670 | 2009-07-15 23:29:37 +0100 | [diff] [blame] | 1583 | 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] | 1584 | size_t size, struct bio *bio, |
| 1585 | unsigned long bio_flags) |
Chris Mason | 239b14b | 2008-03-24 15:02:07 -0400 | [diff] [blame] | 1586 | { |
| 1587 | struct btrfs_root *root = BTRFS_I(page->mapping->host)->root; |
Chris Mason | a62b940 | 2008-10-03 16:31:08 -0400 | [diff] [blame] | 1588 | u64 logical = (u64)bio->bi_sector << 9; |
Chris Mason | 239b14b | 2008-03-24 15:02:07 -0400 | [diff] [blame] | 1589 | u64 length = 0; |
| 1590 | u64 map_length; |
Chris Mason | 239b14b | 2008-03-24 15:02:07 -0400 | [diff] [blame] | 1591 | int ret; |
| 1592 | |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 1593 | if (bio_flags & EXTENT_BIO_COMPRESSED) |
| 1594 | return 0; |
| 1595 | |
Chris Mason | f2d8d74 | 2008-04-21 10:03:05 -0400 | [diff] [blame] | 1596 | length = bio->bi_size; |
Chris Mason | 239b14b | 2008-03-24 15:02:07 -0400 | [diff] [blame] | 1597 | map_length = length; |
David Woodhouse | 64a1670 | 2009-07-15 23:29:37 +0100 | [diff] [blame] | 1598 | ret = btrfs_map_block(root->fs_info, rw, logical, |
Chris Mason | f188591 | 2008-04-09 16:28:12 -0400 | [diff] [blame] | 1599 | &map_length, NULL, 0); |
Stefan Behrens | 3ec706c | 2012-11-05 15:46:42 +0100 | [diff] [blame] | 1600 | /* Will always return 0 with map_multi == NULL */ |
Jeff Mahoney | 3444a97 | 2011-10-03 23:23:13 -0400 | [diff] [blame] | 1601 | BUG_ON(ret < 0); |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 1602 | if (map_length < length + size) |
Chris Mason | 239b14b | 2008-03-24 15:02:07 -0400 | [diff] [blame] | 1603 | return 1; |
Jeff Mahoney | 3444a97 | 2011-10-03 23:23:13 -0400 | [diff] [blame] | 1604 | return 0; |
Chris Mason | 239b14b | 2008-03-24 15:02:07 -0400 | [diff] [blame] | 1605 | } |
| 1606 | |
Chris Mason | d352ac6 | 2008-09-29 15:18:18 -0400 | [diff] [blame] | 1607 | /* |
| 1608 | * in order to insert checksums into the metadata in large chunks, |
| 1609 | * we wait until bio submission time. All the pages in the bio are |
| 1610 | * checksummed and sums are attached onto the ordered extent record. |
| 1611 | * |
| 1612 | * At IO completion time the cums attached on the ordered extent record |
| 1613 | * are inserted into the btree |
| 1614 | */ |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 1615 | static int __btrfs_submit_bio_start(struct inode *inode, int rw, |
| 1616 | struct bio *bio, int mirror_num, |
Chris Mason | eaf25d9 | 2010-05-25 09:48:28 -0400 | [diff] [blame] | 1617 | unsigned long bio_flags, |
| 1618 | u64 bio_offset) |
Chris Mason | 065631f | 2008-02-20 12:07:25 -0500 | [diff] [blame] | 1619 | { |
Chris Mason | 065631f | 2008-02-20 12:07:25 -0500 | [diff] [blame] | 1620 | struct btrfs_root *root = BTRFS_I(inode)->root; |
Chris Mason | 065631f | 2008-02-20 12:07:25 -0500 | [diff] [blame] | 1621 | int ret = 0; |
Chris Mason | e015640 | 2008-04-16 11:15:20 -0400 | [diff] [blame] | 1622 | |
Chris Mason | d20f704 | 2008-12-08 16:58:54 -0500 | [diff] [blame] | 1623 | ret = btrfs_csum_one_bio(root, inode, bio, 0, 0); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 1624 | BUG_ON(ret); /* -ENOMEM */ |
Chris Mason | 4a69a41 | 2008-11-06 22:03:00 -0500 | [diff] [blame] | 1625 | return 0; |
| 1626 | } |
Chris Mason | e015640 | 2008-04-16 11:15:20 -0400 | [diff] [blame] | 1627 | |
Chris Mason | 4a69a41 | 2008-11-06 22:03:00 -0500 | [diff] [blame] | 1628 | /* |
| 1629 | * in order to insert checksums into the metadata in large chunks, |
| 1630 | * we wait until bio submission time. All the pages in the bio are |
| 1631 | * checksummed and sums are attached onto the ordered extent record. |
| 1632 | * |
| 1633 | * At IO completion time the cums attached on the ordered extent record |
| 1634 | * are inserted into the btree |
| 1635 | */ |
Christoph Hellwig | b295086 | 2008-12-02 09:54:17 -0500 | [diff] [blame] | 1636 | 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] | 1637 | int mirror_num, unsigned long bio_flags, |
| 1638 | u64 bio_offset) |
Chris Mason | 4a69a41 | 2008-11-06 22:03:00 -0500 | [diff] [blame] | 1639 | { |
| 1640 | struct btrfs_root *root = BTRFS_I(inode)->root; |
Stefan Behrens | 6189192 | 2012-11-05 18:51:52 +0100 | [diff] [blame] | 1641 | int ret; |
| 1642 | |
| 1643 | ret = btrfs_map_bio(root, rw, bio, mirror_num, 1); |
| 1644 | if (ret) |
| 1645 | bio_endio(bio, ret); |
| 1646 | return ret; |
Chris Mason | 44b8bd7 | 2008-04-16 11:14:51 -0400 | [diff] [blame] | 1647 | } |
| 1648 | |
Chris Mason | d352ac6 | 2008-09-29 15:18:18 -0400 | [diff] [blame] | 1649 | /* |
Chris Mason | cad321a | 2008-12-17 14:51:42 -0500 | [diff] [blame] | 1650 | * extent_io.c submission hook. This does the right thing for csum calculation |
| 1651 | * on write, or reading the csums from the tree before a read |
Chris Mason | d352ac6 | 2008-09-29 15:18:18 -0400 | [diff] [blame] | 1652 | */ |
Christoph Hellwig | b295086 | 2008-12-02 09:54:17 -0500 | [diff] [blame] | 1653 | 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] | 1654 | int mirror_num, unsigned long bio_flags, |
| 1655 | u64 bio_offset) |
Chris Mason | 44b8bd7 | 2008-04-16 11:14:51 -0400 | [diff] [blame] | 1656 | { |
| 1657 | struct btrfs_root *root = BTRFS_I(inode)->root; |
| 1658 | int ret = 0; |
Chris Mason | 19b9bdb | 2008-10-30 14:23:13 -0400 | [diff] [blame] | 1659 | int skip_sum; |
Jeff Mahoney | 0417341 | 2011-10-03 23:23:12 -0400 | [diff] [blame] | 1660 | int metadata = 0; |
Josef Bacik | b812ce2 | 2012-11-16 13:56:32 -0500 | [diff] [blame] | 1661 | int async = !atomic_read(&BTRFS_I(inode)->sync_writers); |
Chris Mason | 44b8bd7 | 2008-04-16 11:14:51 -0400 | [diff] [blame] | 1662 | |
Christoph Hellwig | 6cbff00 | 2009-04-17 10:37:41 +0200 | [diff] [blame] | 1663 | skip_sum = BTRFS_I(inode)->flags & BTRFS_INODE_NODATASUM; |
Chris Mason | cad321a | 2008-12-17 14:51:42 -0500 | [diff] [blame] | 1664 | |
Liu Bo | 83eea1f | 2012-07-10 05:28:39 -0600 | [diff] [blame] | 1665 | if (btrfs_is_free_space_inode(inode)) |
Jeff Mahoney | 0417341 | 2011-10-03 23:23:12 -0400 | [diff] [blame] | 1666 | metadata = 2; |
| 1667 | |
Christoph Hellwig | 7b6d91d | 2010-08-07 18:20:39 +0200 | [diff] [blame] | 1668 | if (!(rw & REQ_WRITE)) { |
Josef Bacik | 5fd0204 | 2012-05-02 14:00:54 -0400 | [diff] [blame] | 1669 | ret = btrfs_bio_wq_end_io(root->fs_info, bio, metadata); |
| 1670 | if (ret) |
Stefan Behrens | 6189192 | 2012-11-05 18:51:52 +0100 | [diff] [blame] | 1671 | goto out; |
Josef Bacik | 5fd0204 | 2012-05-02 14:00:54 -0400 | [diff] [blame] | 1672 | |
Chris Mason | d20f704 | 2008-12-08 16:58:54 -0500 | [diff] [blame] | 1673 | if (bio_flags & EXTENT_BIO_COMPRESSED) { |
Stefan Behrens | 6189192 | 2012-11-05 18:51:52 +0100 | [diff] [blame] | 1674 | ret = btrfs_submit_compressed_read(inode, bio, |
| 1675 | mirror_num, |
| 1676 | bio_flags); |
| 1677 | goto out; |
Tsutomu Itoh | c2db107 | 2011-03-01 06:48:31 +0000 | [diff] [blame] | 1678 | } else if (!skip_sum) { |
| 1679 | ret = btrfs_lookup_bio_sums(root, inode, bio, NULL); |
| 1680 | if (ret) |
Stefan Behrens | 6189192 | 2012-11-05 18:51:52 +0100 | [diff] [blame] | 1681 | goto out; |
Tsutomu Itoh | c2db107 | 2011-03-01 06:48:31 +0000 | [diff] [blame] | 1682 | } |
Chris Mason | 4d1b5fb | 2008-08-20 09:44:52 -0400 | [diff] [blame] | 1683 | goto mapit; |
Josef Bacik | b812ce2 | 2012-11-16 13:56:32 -0500 | [diff] [blame] | 1684 | } else if (async && !skip_sum) { |
Yan Zheng | 17d217f | 2008-12-12 10:03:38 -0500 | [diff] [blame] | 1685 | /* csum items have already been cloned */ |
| 1686 | if (root->root_key.objectid == BTRFS_DATA_RELOC_TREE_OBJECTID) |
| 1687 | goto mapit; |
Chris Mason | 19b9bdb | 2008-10-30 14:23:13 -0400 | [diff] [blame] | 1688 | /* we're doing a write, do the async checksumming */ |
Stefan Behrens | 6189192 | 2012-11-05 18:51:52 +0100 | [diff] [blame] | 1689 | ret = btrfs_wq_submit_bio(BTRFS_I(inode)->root->fs_info, |
Chris Mason | 44b8bd7 | 2008-04-16 11:14:51 -0400 | [diff] [blame] | 1690 | inode, rw, bio, mirror_num, |
Chris Mason | eaf25d9 | 2010-05-25 09:48:28 -0400 | [diff] [blame] | 1691 | bio_flags, bio_offset, |
| 1692 | __btrfs_submit_bio_start, |
Chris Mason | 4a69a41 | 2008-11-06 22:03:00 -0500 | [diff] [blame] | 1693 | __btrfs_submit_bio_done); |
Stefan Behrens | 6189192 | 2012-11-05 18:51:52 +0100 | [diff] [blame] | 1694 | goto out; |
Josef Bacik | b812ce2 | 2012-11-16 13:56:32 -0500 | [diff] [blame] | 1695 | } else if (!skip_sum) { |
| 1696 | ret = btrfs_csum_one_bio(root, inode, bio, 0, 0); |
| 1697 | if (ret) |
| 1698 | goto out; |
Chris Mason | 19b9bdb | 2008-10-30 14:23:13 -0400 | [diff] [blame] | 1699 | } |
| 1700 | |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 1701 | mapit: |
Stefan Behrens | 6189192 | 2012-11-05 18:51:52 +0100 | [diff] [blame] | 1702 | ret = btrfs_map_bio(root, rw, bio, mirror_num, 0); |
| 1703 | |
| 1704 | out: |
| 1705 | if (ret < 0) |
| 1706 | bio_endio(bio, ret); |
| 1707 | return ret; |
Chris Mason | 065631f | 2008-02-20 12:07:25 -0500 | [diff] [blame] | 1708 | } |
Chris Mason | 6885f30 | 2008-02-20 16:11:05 -0500 | [diff] [blame] | 1709 | |
Chris Mason | d352ac6 | 2008-09-29 15:18:18 -0400 | [diff] [blame] | 1710 | /* |
| 1711 | * given a list of ordered sums record them in the inode. This happens |
| 1712 | * at IO completion time based on sums calculated at bio submission time. |
| 1713 | */ |
Chris Mason | ba1da2f | 2008-07-17 12:54:15 -0400 | [diff] [blame] | 1714 | static noinline int add_pending_csums(struct btrfs_trans_handle *trans, |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 1715 | struct inode *inode, u64 file_offset, |
| 1716 | struct list_head *list) |
| 1717 | { |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 1718 | struct btrfs_ordered_sum *sum; |
| 1719 | |
Qinghuang Feng | c6e3087 | 2009-01-21 10:59:08 -0500 | [diff] [blame] | 1720 | list_for_each_entry(sum, list, list) { |
Miao Xie | 39847c4 | 2013-03-28 08:08:20 +0000 | [diff] [blame] | 1721 | trans->adding_csums = 1; |
Chris Mason | d20f704 | 2008-12-08 16:58:54 -0500 | [diff] [blame] | 1722 | btrfs_csum_file_blocks(trans, |
| 1723 | BTRFS_I(inode)->root->fs_info->csum_root, sum); |
Miao Xie | 39847c4 | 2013-03-28 08:08:20 +0000 | [diff] [blame] | 1724 | trans->adding_csums = 0; |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 1725 | } |
| 1726 | return 0; |
| 1727 | } |
| 1728 | |
Josef Bacik | 2ac55d4 | 2010-02-03 19:33:23 +0000 | [diff] [blame] | 1729 | int btrfs_set_extent_delalloc(struct inode *inode, u64 start, u64 end, |
| 1730 | struct extent_state **cached_state) |
Chris Mason | ea8c281 | 2008-08-04 23:17:27 -0400 | [diff] [blame] | 1731 | { |
Julia Lawall | 6c1500f | 2012-11-03 20:30:18 +0000 | [diff] [blame] | 1732 | WARN_ON((end & (PAGE_CACHE_SIZE - 1)) == 0); |
Chris Mason | ea8c281 | 2008-08-04 23:17:27 -0400 | [diff] [blame] | 1733 | return set_extent_delalloc(&BTRFS_I(inode)->io_tree, start, end, |
Josef Bacik | 2ac55d4 | 2010-02-03 19:33:23 +0000 | [diff] [blame] | 1734 | cached_state, GFP_NOFS); |
Chris Mason | ea8c281 | 2008-08-04 23:17:27 -0400 | [diff] [blame] | 1735 | } |
| 1736 | |
Chris Mason | d352ac6 | 2008-09-29 15:18:18 -0400 | [diff] [blame] | 1737 | /* see btrfs_writepage_start_hook for details on why this is required */ |
Chris Mason | 247e743 | 2008-07-17 12:53:51 -0400 | [diff] [blame] | 1738 | struct btrfs_writepage_fixup { |
| 1739 | struct page *page; |
| 1740 | struct btrfs_work work; |
| 1741 | }; |
| 1742 | |
Christoph Hellwig | b295086 | 2008-12-02 09:54:17 -0500 | [diff] [blame] | 1743 | static void btrfs_writepage_fixup_worker(struct btrfs_work *work) |
Chris Mason | 247e743 | 2008-07-17 12:53:51 -0400 | [diff] [blame] | 1744 | { |
| 1745 | struct btrfs_writepage_fixup *fixup; |
| 1746 | struct btrfs_ordered_extent *ordered; |
Josef Bacik | 2ac55d4 | 2010-02-03 19:33:23 +0000 | [diff] [blame] | 1747 | struct extent_state *cached_state = NULL; |
Chris Mason | 247e743 | 2008-07-17 12:53:51 -0400 | [diff] [blame] | 1748 | struct page *page; |
| 1749 | struct inode *inode; |
| 1750 | u64 page_start; |
| 1751 | u64 page_end; |
Jeff Mahoney | 87826df | 2012-02-15 16:23:57 +0100 | [diff] [blame] | 1752 | int ret; |
Chris Mason | 247e743 | 2008-07-17 12:53:51 -0400 | [diff] [blame] | 1753 | |
| 1754 | fixup = container_of(work, struct btrfs_writepage_fixup, work); |
| 1755 | page = fixup->page; |
Chris Mason | 4a09675 | 2008-07-21 10:29:44 -0400 | [diff] [blame] | 1756 | again: |
Chris Mason | 247e743 | 2008-07-17 12:53:51 -0400 | [diff] [blame] | 1757 | lock_page(page); |
| 1758 | if (!page->mapping || !PageDirty(page) || !PageChecked(page)) { |
| 1759 | ClearPageChecked(page); |
| 1760 | goto out_page; |
| 1761 | } |
| 1762 | |
| 1763 | inode = page->mapping->host; |
| 1764 | page_start = page_offset(page); |
| 1765 | page_end = page_offset(page) + PAGE_CACHE_SIZE - 1; |
| 1766 | |
Josef Bacik | 2ac55d4 | 2010-02-03 19:33:23 +0000 | [diff] [blame] | 1767 | 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] | 1768 | &cached_state); |
Chris Mason | 4a09675 | 2008-07-21 10:29:44 -0400 | [diff] [blame] | 1769 | |
| 1770 | /* already ordered? We're done */ |
Chris Mason | 8b62b72 | 2009-09-02 16:53:46 -0400 | [diff] [blame] | 1771 | if (PagePrivate2(page)) |
Chris Mason | 247e743 | 2008-07-17 12:53:51 -0400 | [diff] [blame] | 1772 | goto out; |
Chris Mason | 4a09675 | 2008-07-21 10:29:44 -0400 | [diff] [blame] | 1773 | |
| 1774 | ordered = btrfs_lookup_ordered_extent(inode, page_start); |
| 1775 | if (ordered) { |
Josef Bacik | 2ac55d4 | 2010-02-03 19:33:23 +0000 | [diff] [blame] | 1776 | unlock_extent_cached(&BTRFS_I(inode)->io_tree, page_start, |
| 1777 | page_end, &cached_state, GFP_NOFS); |
Chris Mason | 4a09675 | 2008-07-21 10:29:44 -0400 | [diff] [blame] | 1778 | unlock_page(page); |
| 1779 | btrfs_start_ordered_extent(inode, ordered, 1); |
Jeff Mahoney | 87826df | 2012-02-15 16:23:57 +0100 | [diff] [blame] | 1780 | btrfs_put_ordered_extent(ordered); |
Chris Mason | 4a09675 | 2008-07-21 10:29:44 -0400 | [diff] [blame] | 1781 | goto again; |
| 1782 | } |
Chris Mason | 247e743 | 2008-07-17 12:53:51 -0400 | [diff] [blame] | 1783 | |
Jeff Mahoney | 87826df | 2012-02-15 16:23:57 +0100 | [diff] [blame] | 1784 | ret = btrfs_delalloc_reserve_space(inode, PAGE_CACHE_SIZE); |
| 1785 | if (ret) { |
| 1786 | mapping_set_error(page->mapping, ret); |
| 1787 | end_extent_writepage(page, ret, page_start, page_end); |
| 1788 | ClearPageChecked(page); |
| 1789 | goto out; |
| 1790 | } |
| 1791 | |
Josef Bacik | 2ac55d4 | 2010-02-03 19:33:23 +0000 | [diff] [blame] | 1792 | btrfs_set_extent_delalloc(inode, page_start, page_end, &cached_state); |
Chris Mason | 247e743 | 2008-07-17 12:53:51 -0400 | [diff] [blame] | 1793 | ClearPageChecked(page); |
Jeff Mahoney | 87826df | 2012-02-15 16:23:57 +0100 | [diff] [blame] | 1794 | set_page_dirty(page); |
Chris Mason | 247e743 | 2008-07-17 12:53:51 -0400 | [diff] [blame] | 1795 | out: |
Josef Bacik | 2ac55d4 | 2010-02-03 19:33:23 +0000 | [diff] [blame] | 1796 | unlock_extent_cached(&BTRFS_I(inode)->io_tree, page_start, page_end, |
| 1797 | &cached_state, GFP_NOFS); |
Chris Mason | 247e743 | 2008-07-17 12:53:51 -0400 | [diff] [blame] | 1798 | out_page: |
| 1799 | unlock_page(page); |
| 1800 | page_cache_release(page); |
Miao Xie | b897abe | 2011-01-26 16:19:22 +0800 | [diff] [blame] | 1801 | kfree(fixup); |
Chris Mason | 247e743 | 2008-07-17 12:53:51 -0400 | [diff] [blame] | 1802 | } |
| 1803 | |
| 1804 | /* |
| 1805 | * There are a few paths in the higher layers of the kernel that directly |
| 1806 | * set the page dirty bit without asking the filesystem if it is a |
| 1807 | * good idea. This causes problems because we want to make sure COW |
| 1808 | * properly happens and the data=ordered rules are followed. |
| 1809 | * |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 1810 | * 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] | 1811 | * hasn't been properly setup for IO. We kick off an async process |
| 1812 | * to fix it up. The async helper will wait for ordered extents, set |
| 1813 | * the delalloc bit and make it safe to write the page. |
| 1814 | */ |
Christoph Hellwig | b295086 | 2008-12-02 09:54:17 -0500 | [diff] [blame] | 1815 | 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] | 1816 | { |
| 1817 | struct inode *inode = page->mapping->host; |
| 1818 | struct btrfs_writepage_fixup *fixup; |
| 1819 | struct btrfs_root *root = BTRFS_I(inode)->root; |
Chris Mason | 247e743 | 2008-07-17 12:53:51 -0400 | [diff] [blame] | 1820 | |
Chris Mason | 8b62b72 | 2009-09-02 16:53:46 -0400 | [diff] [blame] | 1821 | /* this page is properly in the ordered list */ |
| 1822 | if (TestClearPagePrivate2(page)) |
Chris Mason | 247e743 | 2008-07-17 12:53:51 -0400 | [diff] [blame] | 1823 | return 0; |
| 1824 | |
| 1825 | if (PageChecked(page)) |
| 1826 | return -EAGAIN; |
| 1827 | |
| 1828 | fixup = kzalloc(sizeof(*fixup), GFP_NOFS); |
| 1829 | if (!fixup) |
| 1830 | return -EAGAIN; |
Chris Mason | f421950 | 2008-07-22 11:18:09 -0400 | [diff] [blame] | 1831 | |
Chris Mason | 247e743 | 2008-07-17 12:53:51 -0400 | [diff] [blame] | 1832 | SetPageChecked(page); |
| 1833 | page_cache_get(page); |
| 1834 | fixup->work.func = btrfs_writepage_fixup_worker; |
| 1835 | fixup->page = page; |
| 1836 | btrfs_queue_worker(&root->fs_info->fixup_workers, &fixup->work); |
Jeff Mahoney | 87826df | 2012-02-15 16:23:57 +0100 | [diff] [blame] | 1837 | return -EBUSY; |
Chris Mason | 247e743 | 2008-07-17 12:53:51 -0400 | [diff] [blame] | 1838 | } |
| 1839 | |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 1840 | static int insert_reserved_file_extent(struct btrfs_trans_handle *trans, |
| 1841 | struct inode *inode, u64 file_pos, |
| 1842 | u64 disk_bytenr, u64 disk_num_bytes, |
| 1843 | u64 num_bytes, u64 ram_bytes, |
| 1844 | u8 compression, u8 encryption, |
| 1845 | u16 other_encoding, int extent_type) |
| 1846 | { |
| 1847 | struct btrfs_root *root = BTRFS_I(inode)->root; |
| 1848 | struct btrfs_file_extent_item *fi; |
| 1849 | struct btrfs_path *path; |
| 1850 | struct extent_buffer *leaf; |
| 1851 | struct btrfs_key ins; |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 1852 | int ret; |
| 1853 | |
| 1854 | path = btrfs_alloc_path(); |
Mark Fasheh | d8926bb | 2011-07-13 10:38:47 -0700 | [diff] [blame] | 1855 | if (!path) |
| 1856 | return -ENOMEM; |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 1857 | |
Chris Mason | b947343 | 2009-03-13 11:00:37 -0400 | [diff] [blame] | 1858 | path->leave_spinning = 1; |
Chris Mason | a1ed835 | 2009-09-11 12:27:37 -0400 | [diff] [blame] | 1859 | |
| 1860 | /* |
| 1861 | * we may be replacing one extent in the tree with another. |
| 1862 | * The new extent is pinned in the extent map, and we don't want |
| 1863 | * to drop it from the cache until it is completely in the btree. |
| 1864 | * |
| 1865 | * So, tell btrfs_drop_extents to leave this extent in the cache. |
| 1866 | * the caller is expected to unpin it and allow it to be merged |
| 1867 | * with the others. |
| 1868 | */ |
Josef Bacik | 5dc562c | 2012-08-17 13:14:17 -0400 | [diff] [blame] | 1869 | ret = btrfs_drop_extents(trans, root, inode, file_pos, |
Josef Bacik | 2671485 | 2012-08-29 12:24:27 -0400 | [diff] [blame] | 1870 | file_pos + num_bytes, 0); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 1871 | if (ret) |
| 1872 | goto out; |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 1873 | |
Li Zefan | 33345d01 | 2011-04-20 10:31:50 +0800 | [diff] [blame] | 1874 | ins.objectid = btrfs_ino(inode); |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 1875 | ins.offset = file_pos; |
| 1876 | ins.type = BTRFS_EXTENT_DATA_KEY; |
| 1877 | ret = btrfs_insert_empty_item(trans, root, path, &ins, sizeof(*fi)); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 1878 | if (ret) |
| 1879 | goto out; |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 1880 | leaf = path->nodes[0]; |
| 1881 | fi = btrfs_item_ptr(leaf, path->slots[0], |
| 1882 | struct btrfs_file_extent_item); |
| 1883 | btrfs_set_file_extent_generation(leaf, fi, trans->transid); |
| 1884 | btrfs_set_file_extent_type(leaf, fi, extent_type); |
| 1885 | btrfs_set_file_extent_disk_bytenr(leaf, fi, disk_bytenr); |
| 1886 | btrfs_set_file_extent_disk_num_bytes(leaf, fi, disk_num_bytes); |
| 1887 | btrfs_set_file_extent_offset(leaf, fi, 0); |
| 1888 | btrfs_set_file_extent_num_bytes(leaf, fi, num_bytes); |
| 1889 | btrfs_set_file_extent_ram_bytes(leaf, fi, ram_bytes); |
| 1890 | btrfs_set_file_extent_compression(leaf, fi, compression); |
| 1891 | btrfs_set_file_extent_encryption(leaf, fi, encryption); |
| 1892 | btrfs_set_file_extent_other_encoding(leaf, fi, other_encoding); |
Chris Mason | b947343 | 2009-03-13 11:00:37 -0400 | [diff] [blame] | 1893 | |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 1894 | btrfs_mark_buffer_dirty(leaf); |
Josef Bacik | ce19533 | 2012-09-25 15:26:16 -0400 | [diff] [blame] | 1895 | btrfs_release_path(path); |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 1896 | |
| 1897 | inode_add_bytes(inode, num_bytes); |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 1898 | |
| 1899 | ins.objectid = disk_bytenr; |
| 1900 | ins.offset = disk_num_bytes; |
| 1901 | ins.type = BTRFS_EXTENT_ITEM_KEY; |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 1902 | ret = btrfs_alloc_reserved_file_extent(trans, root, |
| 1903 | root->root_key.objectid, |
Li Zefan | 33345d01 | 2011-04-20 10:31:50 +0800 | [diff] [blame] | 1904 | btrfs_ino(inode), file_pos, &ins); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 1905 | out: |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 1906 | btrfs_free_path(path); |
Chris Mason | b947343 | 2009-03-13 11:00:37 -0400 | [diff] [blame] | 1907 | |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 1908 | return ret; |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 1909 | } |
| 1910 | |
Liu Bo | 38c227d | 2013-01-29 03:18:40 +0000 | [diff] [blame] | 1911 | /* snapshot-aware defrag */ |
| 1912 | struct sa_defrag_extent_backref { |
| 1913 | struct rb_node node; |
| 1914 | struct old_sa_defrag_extent *old; |
| 1915 | u64 root_id; |
| 1916 | u64 inum; |
| 1917 | u64 file_pos; |
| 1918 | u64 extent_offset; |
| 1919 | u64 num_bytes; |
| 1920 | u64 generation; |
| 1921 | }; |
| 1922 | |
| 1923 | struct old_sa_defrag_extent { |
| 1924 | struct list_head list; |
| 1925 | struct new_sa_defrag_extent *new; |
| 1926 | |
| 1927 | u64 extent_offset; |
| 1928 | u64 bytenr; |
| 1929 | u64 offset; |
| 1930 | u64 len; |
| 1931 | int count; |
| 1932 | }; |
| 1933 | |
| 1934 | struct new_sa_defrag_extent { |
| 1935 | struct rb_root root; |
| 1936 | struct list_head head; |
| 1937 | struct btrfs_path *path; |
| 1938 | struct inode *inode; |
| 1939 | u64 file_pos; |
| 1940 | u64 len; |
| 1941 | u64 bytenr; |
| 1942 | u64 disk_len; |
| 1943 | u8 compress_type; |
| 1944 | }; |
| 1945 | |
| 1946 | static int backref_comp(struct sa_defrag_extent_backref *b1, |
| 1947 | struct sa_defrag_extent_backref *b2) |
| 1948 | { |
| 1949 | if (b1->root_id < b2->root_id) |
| 1950 | return -1; |
| 1951 | else if (b1->root_id > b2->root_id) |
| 1952 | return 1; |
| 1953 | |
| 1954 | if (b1->inum < b2->inum) |
| 1955 | return -1; |
| 1956 | else if (b1->inum > b2->inum) |
| 1957 | return 1; |
| 1958 | |
| 1959 | if (b1->file_pos < b2->file_pos) |
| 1960 | return -1; |
| 1961 | else if (b1->file_pos > b2->file_pos) |
| 1962 | return 1; |
| 1963 | |
| 1964 | /* |
| 1965 | * [------------------------------] ===> (a range of space) |
| 1966 | * |<--->| |<---->| =============> (fs/file tree A) |
| 1967 | * |<---------------------------->| ===> (fs/file tree B) |
| 1968 | * |
| 1969 | * A range of space can refer to two file extents in one tree while |
| 1970 | * refer to only one file extent in another tree. |
| 1971 | * |
| 1972 | * So we may process a disk offset more than one time(two extents in A) |
| 1973 | * and locate at the same extent(one extent in B), then insert two same |
| 1974 | * backrefs(both refer to the extent in B). |
| 1975 | */ |
| 1976 | return 0; |
| 1977 | } |
| 1978 | |
| 1979 | static void backref_insert(struct rb_root *root, |
| 1980 | struct sa_defrag_extent_backref *backref) |
| 1981 | { |
| 1982 | struct rb_node **p = &root->rb_node; |
| 1983 | struct rb_node *parent = NULL; |
| 1984 | struct sa_defrag_extent_backref *entry; |
| 1985 | int ret; |
| 1986 | |
| 1987 | while (*p) { |
| 1988 | parent = *p; |
| 1989 | entry = rb_entry(parent, struct sa_defrag_extent_backref, node); |
| 1990 | |
| 1991 | ret = backref_comp(backref, entry); |
| 1992 | if (ret < 0) |
| 1993 | p = &(*p)->rb_left; |
| 1994 | else |
| 1995 | p = &(*p)->rb_right; |
| 1996 | } |
| 1997 | |
| 1998 | rb_link_node(&backref->node, parent, p); |
| 1999 | rb_insert_color(&backref->node, root); |
| 2000 | } |
| 2001 | |
| 2002 | /* |
| 2003 | * Note the backref might has changed, and in this case we just return 0. |
| 2004 | */ |
| 2005 | static noinline int record_one_backref(u64 inum, u64 offset, u64 root_id, |
| 2006 | void *ctx) |
| 2007 | { |
| 2008 | struct btrfs_file_extent_item *extent; |
| 2009 | struct btrfs_fs_info *fs_info; |
| 2010 | struct old_sa_defrag_extent *old = ctx; |
| 2011 | struct new_sa_defrag_extent *new = old->new; |
| 2012 | struct btrfs_path *path = new->path; |
| 2013 | struct btrfs_key key; |
| 2014 | struct btrfs_root *root; |
| 2015 | struct sa_defrag_extent_backref *backref; |
| 2016 | struct extent_buffer *leaf; |
| 2017 | struct inode *inode = new->inode; |
| 2018 | int slot; |
| 2019 | int ret; |
| 2020 | u64 extent_offset; |
| 2021 | u64 num_bytes; |
| 2022 | |
| 2023 | if (BTRFS_I(inode)->root->root_key.objectid == root_id && |
| 2024 | inum == btrfs_ino(inode)) |
| 2025 | return 0; |
| 2026 | |
| 2027 | key.objectid = root_id; |
| 2028 | key.type = BTRFS_ROOT_ITEM_KEY; |
| 2029 | key.offset = (u64)-1; |
| 2030 | |
| 2031 | fs_info = BTRFS_I(inode)->root->fs_info; |
| 2032 | root = btrfs_read_fs_root_no_name(fs_info, &key); |
| 2033 | if (IS_ERR(root)) { |
| 2034 | if (PTR_ERR(root) == -ENOENT) |
| 2035 | return 0; |
| 2036 | WARN_ON(1); |
| 2037 | pr_debug("inum=%llu, offset=%llu, root_id=%llu\n", |
| 2038 | inum, offset, root_id); |
| 2039 | return PTR_ERR(root); |
| 2040 | } |
| 2041 | |
| 2042 | key.objectid = inum; |
| 2043 | key.type = BTRFS_EXTENT_DATA_KEY; |
| 2044 | if (offset > (u64)-1 << 32) |
| 2045 | key.offset = 0; |
| 2046 | else |
| 2047 | key.offset = offset; |
| 2048 | |
| 2049 | ret = btrfs_search_slot(NULL, root, &key, path, 0, 0); |
| 2050 | if (ret < 0) { |
| 2051 | WARN_ON(1); |
| 2052 | return ret; |
| 2053 | } |
Josef Bacik | 50f1319 | 2013-07-22 12:50:37 -0400 | [diff] [blame] | 2054 | ret = 0; |
Liu Bo | 38c227d | 2013-01-29 03:18:40 +0000 | [diff] [blame] | 2055 | |
| 2056 | while (1) { |
| 2057 | cond_resched(); |
| 2058 | |
| 2059 | leaf = path->nodes[0]; |
| 2060 | slot = path->slots[0]; |
| 2061 | |
| 2062 | if (slot >= btrfs_header_nritems(leaf)) { |
| 2063 | ret = btrfs_next_leaf(root, path); |
| 2064 | if (ret < 0) { |
| 2065 | goto out; |
| 2066 | } else if (ret > 0) { |
| 2067 | ret = 0; |
| 2068 | goto out; |
| 2069 | } |
| 2070 | continue; |
| 2071 | } |
| 2072 | |
| 2073 | path->slots[0]++; |
| 2074 | |
| 2075 | btrfs_item_key_to_cpu(leaf, &key, slot); |
| 2076 | |
| 2077 | if (key.objectid > inum) |
| 2078 | goto out; |
| 2079 | |
| 2080 | if (key.objectid < inum || key.type != BTRFS_EXTENT_DATA_KEY) |
| 2081 | continue; |
| 2082 | |
| 2083 | extent = btrfs_item_ptr(leaf, slot, |
| 2084 | struct btrfs_file_extent_item); |
| 2085 | |
| 2086 | if (btrfs_file_extent_disk_bytenr(leaf, extent) != old->bytenr) |
| 2087 | continue; |
| 2088 | |
Liu Bo | e68afa4 | 2013-07-01 22:13:26 +0800 | [diff] [blame] | 2089 | /* |
| 2090 | * 'offset' refers to the exact key.offset, |
| 2091 | * NOT the 'offset' field in btrfs_extent_data_ref, ie. |
| 2092 | * (key.offset - extent_offset). |
| 2093 | */ |
| 2094 | if (key.offset != offset) |
Liu Bo | 38c227d | 2013-01-29 03:18:40 +0000 | [diff] [blame] | 2095 | continue; |
| 2096 | |
Liu Bo | e68afa4 | 2013-07-01 22:13:26 +0800 | [diff] [blame] | 2097 | extent_offset = btrfs_file_extent_offset(leaf, extent); |
Liu Bo | 38c227d | 2013-01-29 03:18:40 +0000 | [diff] [blame] | 2098 | num_bytes = btrfs_file_extent_num_bytes(leaf, extent); |
Liu Bo | e68afa4 | 2013-07-01 22:13:26 +0800 | [diff] [blame] | 2099 | |
Liu Bo | 38c227d | 2013-01-29 03:18:40 +0000 | [diff] [blame] | 2100 | if (extent_offset >= old->extent_offset + old->offset + |
| 2101 | old->len || extent_offset + num_bytes <= |
| 2102 | old->extent_offset + old->offset) |
| 2103 | continue; |
Liu Bo | 38c227d | 2013-01-29 03:18:40 +0000 | [diff] [blame] | 2104 | break; |
| 2105 | } |
| 2106 | |
| 2107 | backref = kmalloc(sizeof(*backref), GFP_NOFS); |
| 2108 | if (!backref) { |
| 2109 | ret = -ENOENT; |
| 2110 | goto out; |
| 2111 | } |
| 2112 | |
| 2113 | backref->root_id = root_id; |
| 2114 | backref->inum = inum; |
Liu Bo | e68afa4 | 2013-07-01 22:13:26 +0800 | [diff] [blame] | 2115 | backref->file_pos = offset; |
Liu Bo | 38c227d | 2013-01-29 03:18:40 +0000 | [diff] [blame] | 2116 | backref->num_bytes = num_bytes; |
| 2117 | backref->extent_offset = extent_offset; |
| 2118 | backref->generation = btrfs_file_extent_generation(leaf, extent); |
| 2119 | backref->old = old; |
| 2120 | backref_insert(&new->root, backref); |
| 2121 | old->count++; |
| 2122 | out: |
| 2123 | btrfs_release_path(path); |
| 2124 | WARN_ON(ret); |
| 2125 | return ret; |
| 2126 | } |
| 2127 | |
| 2128 | static noinline bool record_extent_backrefs(struct btrfs_path *path, |
| 2129 | struct new_sa_defrag_extent *new) |
| 2130 | { |
| 2131 | struct btrfs_fs_info *fs_info = BTRFS_I(new->inode)->root->fs_info; |
| 2132 | struct old_sa_defrag_extent *old, *tmp; |
| 2133 | int ret; |
| 2134 | |
| 2135 | new->path = path; |
| 2136 | |
| 2137 | list_for_each_entry_safe(old, tmp, &new->head, list) { |
Liu Bo | e68afa4 | 2013-07-01 22:13:26 +0800 | [diff] [blame] | 2138 | ret = iterate_inodes_from_logical(old->bytenr + |
| 2139 | old->extent_offset, fs_info, |
Liu Bo | 38c227d | 2013-01-29 03:18:40 +0000 | [diff] [blame] | 2140 | path, record_one_backref, |
| 2141 | old); |
| 2142 | BUG_ON(ret < 0 && ret != -ENOENT); |
| 2143 | |
| 2144 | /* no backref to be processed for this extent */ |
| 2145 | if (!old->count) { |
| 2146 | list_del(&old->list); |
| 2147 | kfree(old); |
| 2148 | } |
| 2149 | } |
| 2150 | |
| 2151 | if (list_empty(&new->head)) |
| 2152 | return false; |
| 2153 | |
| 2154 | return true; |
| 2155 | } |
| 2156 | |
| 2157 | static int relink_is_mergable(struct extent_buffer *leaf, |
| 2158 | struct btrfs_file_extent_item *fi, |
Liu Bo | 116e002 | 2013-08-02 16:30:40 +0800 | [diff] [blame] | 2159 | struct new_sa_defrag_extent *new) |
Liu Bo | 38c227d | 2013-01-29 03:18:40 +0000 | [diff] [blame] | 2160 | { |
Liu Bo | 116e002 | 2013-08-02 16:30:40 +0800 | [diff] [blame] | 2161 | if (btrfs_file_extent_disk_bytenr(leaf, fi) != new->bytenr) |
Liu Bo | 38c227d | 2013-01-29 03:18:40 +0000 | [diff] [blame] | 2162 | return 0; |
| 2163 | |
| 2164 | if (btrfs_file_extent_type(leaf, fi) != BTRFS_FILE_EXTENT_REG) |
| 2165 | return 0; |
| 2166 | |
Liu Bo | 116e002 | 2013-08-02 16:30:40 +0800 | [diff] [blame] | 2167 | if (btrfs_file_extent_compression(leaf, fi) != new->compress_type) |
| 2168 | return 0; |
| 2169 | |
| 2170 | if (btrfs_file_extent_encryption(leaf, fi) || |
Liu Bo | 38c227d | 2013-01-29 03:18:40 +0000 | [diff] [blame] | 2171 | btrfs_file_extent_other_encoding(leaf, fi)) |
| 2172 | return 0; |
| 2173 | |
| 2174 | return 1; |
| 2175 | } |
| 2176 | |
| 2177 | /* |
| 2178 | * Note the backref might has changed, and in this case we just return 0. |
| 2179 | */ |
| 2180 | static noinline int relink_extent_backref(struct btrfs_path *path, |
| 2181 | struct sa_defrag_extent_backref *prev, |
| 2182 | struct sa_defrag_extent_backref *backref) |
| 2183 | { |
| 2184 | struct btrfs_file_extent_item *extent; |
| 2185 | struct btrfs_file_extent_item *item; |
| 2186 | struct btrfs_ordered_extent *ordered; |
| 2187 | struct btrfs_trans_handle *trans; |
| 2188 | struct btrfs_fs_info *fs_info; |
| 2189 | struct btrfs_root *root; |
| 2190 | struct btrfs_key key; |
| 2191 | struct extent_buffer *leaf; |
| 2192 | struct old_sa_defrag_extent *old = backref->old; |
| 2193 | struct new_sa_defrag_extent *new = old->new; |
| 2194 | struct inode *src_inode = new->inode; |
| 2195 | struct inode *inode; |
| 2196 | struct extent_state *cached = NULL; |
| 2197 | int ret = 0; |
| 2198 | u64 start; |
| 2199 | u64 len; |
| 2200 | u64 lock_start; |
| 2201 | u64 lock_end; |
| 2202 | bool merge = false; |
| 2203 | int index; |
| 2204 | |
| 2205 | if (prev && prev->root_id == backref->root_id && |
| 2206 | prev->inum == backref->inum && |
| 2207 | prev->file_pos + prev->num_bytes == backref->file_pos) |
| 2208 | merge = true; |
| 2209 | |
| 2210 | /* step 1: get root */ |
| 2211 | key.objectid = backref->root_id; |
| 2212 | key.type = BTRFS_ROOT_ITEM_KEY; |
| 2213 | key.offset = (u64)-1; |
| 2214 | |
| 2215 | fs_info = BTRFS_I(src_inode)->root->fs_info; |
| 2216 | index = srcu_read_lock(&fs_info->subvol_srcu); |
| 2217 | |
| 2218 | root = btrfs_read_fs_root_no_name(fs_info, &key); |
| 2219 | if (IS_ERR(root)) { |
| 2220 | srcu_read_unlock(&fs_info->subvol_srcu, index); |
| 2221 | if (PTR_ERR(root) == -ENOENT) |
| 2222 | return 0; |
| 2223 | return PTR_ERR(root); |
| 2224 | } |
Liu Bo | 38c227d | 2013-01-29 03:18:40 +0000 | [diff] [blame] | 2225 | |
| 2226 | /* step 2: get inode */ |
| 2227 | key.objectid = backref->inum; |
| 2228 | key.type = BTRFS_INODE_ITEM_KEY; |
| 2229 | key.offset = 0; |
| 2230 | |
| 2231 | inode = btrfs_iget(fs_info->sb, &key, root, NULL); |
| 2232 | if (IS_ERR(inode)) { |
| 2233 | srcu_read_unlock(&fs_info->subvol_srcu, index); |
| 2234 | return 0; |
| 2235 | } |
| 2236 | |
| 2237 | srcu_read_unlock(&fs_info->subvol_srcu, index); |
| 2238 | |
| 2239 | /* step 3: relink backref */ |
| 2240 | lock_start = backref->file_pos; |
| 2241 | lock_end = backref->file_pos + backref->num_bytes - 1; |
| 2242 | lock_extent_bits(&BTRFS_I(inode)->io_tree, lock_start, lock_end, |
| 2243 | 0, &cached); |
| 2244 | |
| 2245 | ordered = btrfs_lookup_first_ordered_extent(inode, lock_end); |
| 2246 | if (ordered) { |
| 2247 | btrfs_put_ordered_extent(ordered); |
| 2248 | goto out_unlock; |
| 2249 | } |
| 2250 | |
| 2251 | trans = btrfs_join_transaction(root); |
| 2252 | if (IS_ERR(trans)) { |
| 2253 | ret = PTR_ERR(trans); |
| 2254 | goto out_unlock; |
| 2255 | } |
| 2256 | |
| 2257 | key.objectid = backref->inum; |
| 2258 | key.type = BTRFS_EXTENT_DATA_KEY; |
| 2259 | key.offset = backref->file_pos; |
| 2260 | |
| 2261 | ret = btrfs_search_slot(NULL, root, &key, path, 0, 0); |
| 2262 | if (ret < 0) { |
| 2263 | goto out_free_path; |
| 2264 | } else if (ret > 0) { |
| 2265 | ret = 0; |
| 2266 | goto out_free_path; |
| 2267 | } |
| 2268 | |
| 2269 | extent = btrfs_item_ptr(path->nodes[0], path->slots[0], |
| 2270 | struct btrfs_file_extent_item); |
| 2271 | |
| 2272 | if (btrfs_file_extent_generation(path->nodes[0], extent) != |
| 2273 | backref->generation) |
| 2274 | goto out_free_path; |
| 2275 | |
| 2276 | btrfs_release_path(path); |
| 2277 | |
| 2278 | start = backref->file_pos; |
| 2279 | if (backref->extent_offset < old->extent_offset + old->offset) |
| 2280 | start += old->extent_offset + old->offset - |
| 2281 | backref->extent_offset; |
| 2282 | |
| 2283 | len = min(backref->extent_offset + backref->num_bytes, |
| 2284 | old->extent_offset + old->offset + old->len); |
| 2285 | len -= max(backref->extent_offset, old->extent_offset + old->offset); |
| 2286 | |
| 2287 | ret = btrfs_drop_extents(trans, root, inode, start, |
| 2288 | start + len, 1); |
| 2289 | if (ret) |
| 2290 | goto out_free_path; |
| 2291 | again: |
| 2292 | key.objectid = btrfs_ino(inode); |
| 2293 | key.type = BTRFS_EXTENT_DATA_KEY; |
| 2294 | key.offset = start; |
| 2295 | |
Liu Bo | a09a0a7 | 2013-03-11 09:20:58 +0000 | [diff] [blame] | 2296 | path->leave_spinning = 1; |
Liu Bo | 38c227d | 2013-01-29 03:18:40 +0000 | [diff] [blame] | 2297 | if (merge) { |
| 2298 | struct btrfs_file_extent_item *fi; |
| 2299 | u64 extent_len; |
| 2300 | struct btrfs_key found_key; |
| 2301 | |
| 2302 | ret = btrfs_search_slot(trans, root, &key, path, 1, 1); |
| 2303 | if (ret < 0) |
| 2304 | goto out_free_path; |
| 2305 | |
| 2306 | path->slots[0]--; |
| 2307 | leaf = path->nodes[0]; |
| 2308 | btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]); |
| 2309 | |
| 2310 | fi = btrfs_item_ptr(leaf, path->slots[0], |
| 2311 | struct btrfs_file_extent_item); |
| 2312 | extent_len = btrfs_file_extent_num_bytes(leaf, fi); |
| 2313 | |
Liu Bo | 116e002 | 2013-08-02 16:30:40 +0800 | [diff] [blame] | 2314 | if (extent_len + found_key.offset == start && |
| 2315 | relink_is_mergable(leaf, fi, new)) { |
Liu Bo | 38c227d | 2013-01-29 03:18:40 +0000 | [diff] [blame] | 2316 | btrfs_set_file_extent_num_bytes(leaf, fi, |
| 2317 | extent_len + len); |
| 2318 | btrfs_mark_buffer_dirty(leaf); |
| 2319 | inode_add_bytes(inode, len); |
| 2320 | |
| 2321 | ret = 1; |
| 2322 | goto out_free_path; |
| 2323 | } else { |
| 2324 | merge = false; |
| 2325 | btrfs_release_path(path); |
| 2326 | goto again; |
| 2327 | } |
| 2328 | } |
| 2329 | |
| 2330 | ret = btrfs_insert_empty_item(trans, root, path, &key, |
| 2331 | sizeof(*extent)); |
| 2332 | if (ret) { |
| 2333 | btrfs_abort_transaction(trans, root, ret); |
| 2334 | goto out_free_path; |
| 2335 | } |
| 2336 | |
| 2337 | leaf = path->nodes[0]; |
| 2338 | item = btrfs_item_ptr(leaf, path->slots[0], |
| 2339 | struct btrfs_file_extent_item); |
| 2340 | btrfs_set_file_extent_disk_bytenr(leaf, item, new->bytenr); |
| 2341 | btrfs_set_file_extent_disk_num_bytes(leaf, item, new->disk_len); |
| 2342 | btrfs_set_file_extent_offset(leaf, item, start - new->file_pos); |
| 2343 | btrfs_set_file_extent_num_bytes(leaf, item, len); |
| 2344 | btrfs_set_file_extent_ram_bytes(leaf, item, new->len); |
| 2345 | btrfs_set_file_extent_generation(leaf, item, trans->transid); |
| 2346 | btrfs_set_file_extent_type(leaf, item, BTRFS_FILE_EXTENT_REG); |
| 2347 | btrfs_set_file_extent_compression(leaf, item, new->compress_type); |
| 2348 | btrfs_set_file_extent_encryption(leaf, item, 0); |
| 2349 | btrfs_set_file_extent_other_encoding(leaf, item, 0); |
| 2350 | |
| 2351 | btrfs_mark_buffer_dirty(leaf); |
| 2352 | inode_add_bytes(inode, len); |
Liu Bo | a09a0a7 | 2013-03-11 09:20:58 +0000 | [diff] [blame] | 2353 | btrfs_release_path(path); |
Liu Bo | 38c227d | 2013-01-29 03:18:40 +0000 | [diff] [blame] | 2354 | |
| 2355 | ret = btrfs_inc_extent_ref(trans, root, new->bytenr, |
| 2356 | new->disk_len, 0, |
| 2357 | backref->root_id, backref->inum, |
| 2358 | new->file_pos, 0); /* start - extent_offset */ |
| 2359 | if (ret) { |
| 2360 | btrfs_abort_transaction(trans, root, ret); |
| 2361 | goto out_free_path; |
| 2362 | } |
| 2363 | |
| 2364 | ret = 1; |
| 2365 | out_free_path: |
| 2366 | btrfs_release_path(path); |
Liu Bo | a09a0a7 | 2013-03-11 09:20:58 +0000 | [diff] [blame] | 2367 | path->leave_spinning = 0; |
Liu Bo | 38c227d | 2013-01-29 03:18:40 +0000 | [diff] [blame] | 2368 | btrfs_end_transaction(trans, root); |
| 2369 | out_unlock: |
| 2370 | unlock_extent_cached(&BTRFS_I(inode)->io_tree, lock_start, lock_end, |
| 2371 | &cached, GFP_NOFS); |
| 2372 | iput(inode); |
| 2373 | return ret; |
| 2374 | } |
| 2375 | |
| 2376 | static void relink_file_extents(struct new_sa_defrag_extent *new) |
| 2377 | { |
| 2378 | struct btrfs_path *path; |
| 2379 | struct old_sa_defrag_extent *old, *tmp; |
| 2380 | struct sa_defrag_extent_backref *backref; |
| 2381 | struct sa_defrag_extent_backref *prev = NULL; |
| 2382 | struct inode *inode; |
| 2383 | struct btrfs_root *root; |
| 2384 | struct rb_node *node; |
| 2385 | int ret; |
| 2386 | |
| 2387 | inode = new->inode; |
| 2388 | root = BTRFS_I(inode)->root; |
| 2389 | |
| 2390 | path = btrfs_alloc_path(); |
| 2391 | if (!path) |
| 2392 | return; |
| 2393 | |
| 2394 | if (!record_extent_backrefs(path, new)) { |
| 2395 | btrfs_free_path(path); |
| 2396 | goto out; |
| 2397 | } |
| 2398 | btrfs_release_path(path); |
| 2399 | |
| 2400 | while (1) { |
| 2401 | node = rb_first(&new->root); |
| 2402 | if (!node) |
| 2403 | break; |
| 2404 | rb_erase(node, &new->root); |
| 2405 | |
| 2406 | backref = rb_entry(node, struct sa_defrag_extent_backref, node); |
| 2407 | |
| 2408 | ret = relink_extent_backref(path, prev, backref); |
| 2409 | WARN_ON(ret < 0); |
| 2410 | |
| 2411 | kfree(prev); |
| 2412 | |
| 2413 | if (ret == 1) |
| 2414 | prev = backref; |
| 2415 | else |
| 2416 | prev = NULL; |
| 2417 | cond_resched(); |
| 2418 | } |
| 2419 | kfree(prev); |
| 2420 | |
| 2421 | btrfs_free_path(path); |
| 2422 | |
| 2423 | list_for_each_entry_safe(old, tmp, &new->head, list) { |
| 2424 | list_del(&old->list); |
| 2425 | kfree(old); |
| 2426 | } |
| 2427 | out: |
| 2428 | atomic_dec(&root->fs_info->defrag_running); |
| 2429 | wake_up(&root->fs_info->transaction_wait); |
| 2430 | |
| 2431 | kfree(new); |
| 2432 | } |
| 2433 | |
| 2434 | static struct new_sa_defrag_extent * |
| 2435 | record_old_file_extents(struct inode *inode, |
| 2436 | struct btrfs_ordered_extent *ordered) |
| 2437 | { |
| 2438 | struct btrfs_root *root = BTRFS_I(inode)->root; |
| 2439 | struct btrfs_path *path; |
| 2440 | struct btrfs_key key; |
| 2441 | struct old_sa_defrag_extent *old, *tmp; |
| 2442 | struct new_sa_defrag_extent *new; |
| 2443 | int ret; |
| 2444 | |
| 2445 | new = kmalloc(sizeof(*new), GFP_NOFS); |
| 2446 | if (!new) |
| 2447 | return NULL; |
| 2448 | |
| 2449 | new->inode = inode; |
| 2450 | new->file_pos = ordered->file_offset; |
| 2451 | new->len = ordered->len; |
| 2452 | new->bytenr = ordered->start; |
| 2453 | new->disk_len = ordered->disk_len; |
| 2454 | new->compress_type = ordered->compress_type; |
| 2455 | new->root = RB_ROOT; |
| 2456 | INIT_LIST_HEAD(&new->head); |
| 2457 | |
| 2458 | path = btrfs_alloc_path(); |
| 2459 | if (!path) |
| 2460 | goto out_kfree; |
| 2461 | |
| 2462 | key.objectid = btrfs_ino(inode); |
| 2463 | key.type = BTRFS_EXTENT_DATA_KEY; |
| 2464 | key.offset = new->file_pos; |
| 2465 | |
| 2466 | ret = btrfs_search_slot(NULL, root, &key, path, 0, 0); |
| 2467 | if (ret < 0) |
| 2468 | goto out_free_path; |
| 2469 | if (ret > 0 && path->slots[0] > 0) |
| 2470 | path->slots[0]--; |
| 2471 | |
| 2472 | /* find out all the old extents for the file range */ |
| 2473 | while (1) { |
| 2474 | struct btrfs_file_extent_item *extent; |
| 2475 | struct extent_buffer *l; |
| 2476 | int slot; |
| 2477 | u64 num_bytes; |
| 2478 | u64 offset; |
| 2479 | u64 end; |
| 2480 | u64 disk_bytenr; |
| 2481 | u64 extent_offset; |
| 2482 | |
| 2483 | l = path->nodes[0]; |
| 2484 | slot = path->slots[0]; |
| 2485 | |
| 2486 | if (slot >= btrfs_header_nritems(l)) { |
| 2487 | ret = btrfs_next_leaf(root, path); |
| 2488 | if (ret < 0) |
| 2489 | goto out_free_list; |
| 2490 | else if (ret > 0) |
| 2491 | break; |
| 2492 | continue; |
| 2493 | } |
| 2494 | |
| 2495 | btrfs_item_key_to_cpu(l, &key, slot); |
| 2496 | |
| 2497 | if (key.objectid != btrfs_ino(inode)) |
| 2498 | break; |
| 2499 | if (key.type != BTRFS_EXTENT_DATA_KEY) |
| 2500 | break; |
| 2501 | if (key.offset >= new->file_pos + new->len) |
| 2502 | break; |
| 2503 | |
| 2504 | extent = btrfs_item_ptr(l, slot, struct btrfs_file_extent_item); |
| 2505 | |
| 2506 | num_bytes = btrfs_file_extent_num_bytes(l, extent); |
| 2507 | if (key.offset + num_bytes < new->file_pos) |
| 2508 | goto next; |
| 2509 | |
| 2510 | disk_bytenr = btrfs_file_extent_disk_bytenr(l, extent); |
| 2511 | if (!disk_bytenr) |
| 2512 | goto next; |
| 2513 | |
| 2514 | extent_offset = btrfs_file_extent_offset(l, extent); |
| 2515 | |
| 2516 | old = kmalloc(sizeof(*old), GFP_NOFS); |
| 2517 | if (!old) |
| 2518 | goto out_free_list; |
| 2519 | |
| 2520 | offset = max(new->file_pos, key.offset); |
| 2521 | end = min(new->file_pos + new->len, key.offset + num_bytes); |
| 2522 | |
| 2523 | old->bytenr = disk_bytenr; |
| 2524 | old->extent_offset = extent_offset; |
| 2525 | old->offset = offset - key.offset; |
| 2526 | old->len = end - offset; |
| 2527 | old->new = new; |
| 2528 | old->count = 0; |
| 2529 | list_add_tail(&old->list, &new->head); |
| 2530 | next: |
| 2531 | path->slots[0]++; |
| 2532 | cond_resched(); |
| 2533 | } |
| 2534 | |
| 2535 | btrfs_free_path(path); |
| 2536 | atomic_inc(&root->fs_info->defrag_running); |
| 2537 | |
| 2538 | return new; |
| 2539 | |
| 2540 | out_free_list: |
| 2541 | list_for_each_entry_safe(old, tmp, &new->head, list) { |
| 2542 | list_del(&old->list); |
| 2543 | kfree(old); |
| 2544 | } |
| 2545 | out_free_path: |
| 2546 | btrfs_free_path(path); |
| 2547 | out_kfree: |
| 2548 | kfree(new); |
| 2549 | return NULL; |
| 2550 | } |
| 2551 | |
Chris Mason | 5d13a98 | 2009-03-13 11:41:46 -0400 | [diff] [blame] | 2552 | /* |
| 2553 | * helper function for btrfs_finish_ordered_io, this |
| 2554 | * just reads in some of the csum leaves to prime them into ram |
| 2555 | * before we start the transaction. It limits the amount of btree |
| 2556 | * reads required while inside the transaction. |
| 2557 | */ |
Chris Mason | d352ac6 | 2008-09-29 15:18:18 -0400 | [diff] [blame] | 2558 | /* as ordered data IO finishes, this gets called so we can finish |
| 2559 | * an ordered extent if the range of bytes in the file it covers are |
| 2560 | * fully written. |
| 2561 | */ |
Josef Bacik | 5fd0204 | 2012-05-02 14:00:54 -0400 | [diff] [blame] | 2562 | static int btrfs_finish_ordered_io(struct btrfs_ordered_extent *ordered_extent) |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 2563 | { |
Josef Bacik | 5fd0204 | 2012-05-02 14:00:54 -0400 | [diff] [blame] | 2564 | struct inode *inode = ordered_extent->inode; |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 2565 | struct btrfs_root *root = BTRFS_I(inode)->root; |
Yan, Zheng | 0ca1f7c | 2010-05-16 10:48:47 -0400 | [diff] [blame] | 2566 | struct btrfs_trans_handle *trans = NULL; |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 2567 | struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree; |
Josef Bacik | 2ac55d4 | 2010-02-03 19:33:23 +0000 | [diff] [blame] | 2568 | struct extent_state *cached_state = NULL; |
Liu Bo | 38c227d | 2013-01-29 03:18:40 +0000 | [diff] [blame] | 2569 | struct new_sa_defrag_extent *new = NULL; |
Li Zefan | 261507a0 | 2010-12-17 14:21:50 +0800 | [diff] [blame] | 2570 | int compress_type = 0; |
Josef Bacik | 77cef2e | 2013-08-29 13:57:21 -0400 | [diff] [blame] | 2571 | int ret = 0; |
| 2572 | u64 logical_len = ordered_extent->len; |
Li Zefan | 82d5902 | 2011-04-20 10:33:24 +0800 | [diff] [blame] | 2573 | bool nolock; |
Josef Bacik | 77cef2e | 2013-08-29 13:57:21 -0400 | [diff] [blame] | 2574 | bool truncated = false; |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 2575 | |
Liu Bo | 83eea1f | 2012-07-10 05:28:39 -0600 | [diff] [blame] | 2576 | nolock = btrfs_is_free_space_inode(inode); |
Josef Bacik | 0cb59c9 | 2010-07-02 12:14:14 -0400 | [diff] [blame] | 2577 | |
Josef Bacik | 5fd0204 | 2012-05-02 14:00:54 -0400 | [diff] [blame] | 2578 | if (test_bit(BTRFS_ORDERED_IOERR, &ordered_extent->flags)) { |
| 2579 | ret = -EIO; |
| 2580 | goto out; |
| 2581 | } |
| 2582 | |
Josef Bacik | 77cef2e | 2013-08-29 13:57:21 -0400 | [diff] [blame] | 2583 | if (test_bit(BTRFS_ORDERED_TRUNCATED, &ordered_extent->flags)) { |
| 2584 | truncated = true; |
| 2585 | logical_len = ordered_extent->truncated_len; |
| 2586 | /* Truncated the entire extent, don't bother adding */ |
| 2587 | if (!logical_len) |
| 2588 | goto out; |
| 2589 | } |
| 2590 | |
Yan, Zheng | c216775 | 2009-11-12 09:34:21 +0000 | [diff] [blame] | 2591 | if (test_bit(BTRFS_ORDERED_NOCOW, &ordered_extent->flags)) { |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 2592 | BUG_ON(!list_empty(&ordered_extent->list)); /* Logic error */ |
Josef Bacik | 6c760c0 | 2012-11-09 10:53:21 -0500 | [diff] [blame] | 2593 | btrfs_ordered_update_i_size(inode, 0, ordered_extent); |
| 2594 | if (nolock) |
| 2595 | trans = btrfs_join_transaction_nolock(root); |
| 2596 | else |
| 2597 | trans = btrfs_join_transaction(root); |
| 2598 | if (IS_ERR(trans)) { |
| 2599 | ret = PTR_ERR(trans); |
| 2600 | trans = NULL; |
| 2601 | goto out; |
Yan, Zheng | c216775 | 2009-11-12 09:34:21 +0000 | [diff] [blame] | 2602 | } |
Josef Bacik | 6c760c0 | 2012-11-09 10:53:21 -0500 | [diff] [blame] | 2603 | trans->block_rsv = &root->fs_info->delalloc_block_rsv; |
| 2604 | ret = btrfs_update_inode_fallback(trans, root, inode); |
| 2605 | if (ret) /* -ENOMEM or corruption */ |
| 2606 | btrfs_abort_transaction(trans, root, ret); |
Yan, Zheng | c216775 | 2009-11-12 09:34:21 +0000 | [diff] [blame] | 2607 | goto out; |
| 2608 | } |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 2609 | |
Josef Bacik | 2ac55d4 | 2010-02-03 19:33:23 +0000 | [diff] [blame] | 2610 | lock_extent_bits(io_tree, ordered_extent->file_offset, |
| 2611 | ordered_extent->file_offset + ordered_extent->len - 1, |
Jeff Mahoney | d008237 | 2012-03-01 14:57:19 +0100 | [diff] [blame] | 2612 | 0, &cached_state); |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 2613 | |
Liu Bo | 38c227d | 2013-01-29 03:18:40 +0000 | [diff] [blame] | 2614 | ret = test_range_bit(io_tree, ordered_extent->file_offset, |
| 2615 | ordered_extent->file_offset + ordered_extent->len - 1, |
| 2616 | EXTENT_DEFRAG, 1, cached_state); |
| 2617 | if (ret) { |
| 2618 | u64 last_snapshot = btrfs_root_last_snapshot(&root->root_item); |
| 2619 | if (last_snapshot >= BTRFS_I(inode)->generation) |
| 2620 | /* the inode is shared */ |
| 2621 | new = record_old_file_extents(inode, ordered_extent); |
| 2622 | |
| 2623 | clear_extent_bit(io_tree, ordered_extent->file_offset, |
| 2624 | ordered_extent->file_offset + ordered_extent->len - 1, |
| 2625 | EXTENT_DEFRAG, 0, 0, &cached_state, GFP_NOFS); |
| 2626 | } |
| 2627 | |
Josef Bacik | 0cb59c9 | 2010-07-02 12:14:14 -0400 | [diff] [blame] | 2628 | if (nolock) |
Josef Bacik | 7a7eaa4 | 2011-04-13 12:54:33 -0400 | [diff] [blame] | 2629 | trans = btrfs_join_transaction_nolock(root); |
Josef Bacik | 0cb59c9 | 2010-07-02 12:14:14 -0400 | [diff] [blame] | 2630 | else |
Josef Bacik | 7a7eaa4 | 2011-04-13 12:54:33 -0400 | [diff] [blame] | 2631 | trans = btrfs_join_transaction(root); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 2632 | if (IS_ERR(trans)) { |
| 2633 | ret = PTR_ERR(trans); |
| 2634 | trans = NULL; |
| 2635 | goto out_unlock; |
| 2636 | } |
Yan, Zheng | 0ca1f7c | 2010-05-16 10:48:47 -0400 | [diff] [blame] | 2637 | trans->block_rsv = &root->fs_info->delalloc_block_rsv; |
Yan, Zheng | c216775 | 2009-11-12 09:34:21 +0000 | [diff] [blame] | 2638 | |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 2639 | if (test_bit(BTRFS_ORDERED_COMPRESSED, &ordered_extent->flags)) |
Li Zefan | 261507a0 | 2010-12-17 14:21:50 +0800 | [diff] [blame] | 2640 | compress_type = ordered_extent->compress_type; |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 2641 | if (test_bit(BTRFS_ORDERED_PREALLOC, &ordered_extent->flags)) { |
Li Zefan | 261507a0 | 2010-12-17 14:21:50 +0800 | [diff] [blame] | 2642 | BUG_ON(compress_type); |
Yan, Zheng | 920bbbf | 2009-11-12 09:34:08 +0000 | [diff] [blame] | 2643 | ret = btrfs_mark_extent_written(trans, inode, |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 2644 | ordered_extent->file_offset, |
| 2645 | ordered_extent->file_offset + |
Josef Bacik | 77cef2e | 2013-08-29 13:57:21 -0400 | [diff] [blame] | 2646 | logical_len); |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 2647 | } else { |
Josef Bacik | 0af3d00 | 2010-06-21 14:48:16 -0400 | [diff] [blame] | 2648 | BUG_ON(root == root->fs_info->tree_root); |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 2649 | ret = insert_reserved_file_extent(trans, inode, |
| 2650 | ordered_extent->file_offset, |
| 2651 | ordered_extent->start, |
| 2652 | ordered_extent->disk_len, |
Josef Bacik | 77cef2e | 2013-08-29 13:57:21 -0400 | [diff] [blame] | 2653 | logical_len, logical_len, |
Li Zefan | 261507a0 | 2010-12-17 14:21:50 +0800 | [diff] [blame] | 2654 | compress_type, 0, 0, |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 2655 | BTRFS_FILE_EXTENT_REG); |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 2656 | } |
Josef Bacik | 5dc562c | 2012-08-17 13:14:17 -0400 | [diff] [blame] | 2657 | unpin_extent_cache(&BTRFS_I(inode)->extent_tree, |
| 2658 | ordered_extent->file_offset, ordered_extent->len, |
| 2659 | trans->transid); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 2660 | if (ret < 0) { |
| 2661 | btrfs_abort_transaction(trans, root, ret); |
Josef Bacik | 5fd0204 | 2012-05-02 14:00:54 -0400 | [diff] [blame] | 2662 | goto out_unlock; |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 2663 | } |
Josef Bacik | 2ac55d4 | 2010-02-03 19:33:23 +0000 | [diff] [blame] | 2664 | |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 2665 | add_pending_csums(trans, inode, ordered_extent->file_offset, |
| 2666 | &ordered_extent->list); |
| 2667 | |
Josef Bacik | 6c760c0 | 2012-11-09 10:53:21 -0500 | [diff] [blame] | 2668 | btrfs_ordered_update_i_size(inode, 0, ordered_extent); |
| 2669 | ret = btrfs_update_inode_fallback(trans, root, inode); |
| 2670 | if (ret) { /* -ENOMEM or corruption */ |
| 2671 | btrfs_abort_transaction(trans, root, ret); |
| 2672 | goto out_unlock; |
Josef Bacik | 1ef30be | 2011-04-05 19:25:36 -0400 | [diff] [blame] | 2673 | } |
| 2674 | ret = 0; |
Josef Bacik | 5fd0204 | 2012-05-02 14:00:54 -0400 | [diff] [blame] | 2675 | out_unlock: |
| 2676 | unlock_extent_cached(io_tree, ordered_extent->file_offset, |
| 2677 | ordered_extent->file_offset + |
| 2678 | ordered_extent->len - 1, &cached_state, GFP_NOFS); |
Yan, Zheng | c216775 | 2009-11-12 09:34:21 +0000 | [diff] [blame] | 2679 | out: |
Josef Bacik | 5b0e95b | 2011-10-06 08:58:24 -0400 | [diff] [blame] | 2680 | if (root != root->fs_info->tree_root) |
Josef Bacik | 0cb59c9 | 2010-07-02 12:14:14 -0400 | [diff] [blame] | 2681 | btrfs_delalloc_release_metadata(inode, ordered_extent->len); |
Miao Xie | a698d075 | 2012-09-20 01:51:59 -0600 | [diff] [blame] | 2682 | if (trans) |
| 2683 | btrfs_end_transaction(trans, root); |
Josef Bacik | 0cb59c9 | 2010-07-02 12:14:14 -0400 | [diff] [blame] | 2684 | |
Josef Bacik | 77cef2e | 2013-08-29 13:57:21 -0400 | [diff] [blame] | 2685 | if (ret || truncated) { |
| 2686 | u64 start, end; |
| 2687 | |
| 2688 | if (truncated) |
| 2689 | start = ordered_extent->file_offset + logical_len; |
| 2690 | else |
| 2691 | start = ordered_extent->file_offset; |
| 2692 | end = ordered_extent->file_offset + ordered_extent->len - 1; |
| 2693 | clear_extent_uptodate(io_tree, start, end, NULL, GFP_NOFS); |
| 2694 | |
| 2695 | /* Drop the cache for the part of the extent we didn't write. */ |
| 2696 | btrfs_drop_extent_cache(inode, start, end, 0); |
Josef Bacik | 5fd0204 | 2012-05-02 14:00:54 -0400 | [diff] [blame] | 2697 | |
Josef Bacik | 0bec9ef | 2013-01-31 14:58:00 -0500 | [diff] [blame] | 2698 | /* |
| 2699 | * If the ordered extent had an IOERR or something else went |
| 2700 | * wrong we need to return the space for this ordered extent |
Josef Bacik | 77cef2e | 2013-08-29 13:57:21 -0400 | [diff] [blame] | 2701 | * back to the allocator. We only free the extent in the |
| 2702 | * truncated case if we didn't write out the extent at all. |
Josef Bacik | 0bec9ef | 2013-01-31 14:58:00 -0500 | [diff] [blame] | 2703 | */ |
Josef Bacik | 77cef2e | 2013-08-29 13:57:21 -0400 | [diff] [blame] | 2704 | if ((ret || !logical_len) && |
| 2705 | !test_bit(BTRFS_ORDERED_NOCOW, &ordered_extent->flags) && |
Josef Bacik | 0bec9ef | 2013-01-31 14:58:00 -0500 | [diff] [blame] | 2706 | !test_bit(BTRFS_ORDERED_PREALLOC, &ordered_extent->flags)) |
| 2707 | btrfs_free_reserved_extent(root, ordered_extent->start, |
| 2708 | ordered_extent->disk_len); |
| 2709 | } |
| 2710 | |
| 2711 | |
Josef Bacik | 5fd0204 | 2012-05-02 14:00:54 -0400 | [diff] [blame] | 2712 | /* |
Liu Bo | 8bad3c0 | 2012-06-18 12:14:23 +0800 | [diff] [blame] | 2713 | * This needs to be done to make sure anybody waiting knows we are done |
| 2714 | * updating everything for this ordered extent. |
Josef Bacik | 5fd0204 | 2012-05-02 14:00:54 -0400 | [diff] [blame] | 2715 | */ |
| 2716 | btrfs_remove_ordered_extent(inode, ordered_extent); |
| 2717 | |
Liu Bo | 38c227d | 2013-01-29 03:18:40 +0000 | [diff] [blame] | 2718 | /* for snapshot-aware defrag */ |
| 2719 | if (new) |
| 2720 | relink_file_extents(new); |
| 2721 | |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 2722 | /* once for us */ |
| 2723 | btrfs_put_ordered_extent(ordered_extent); |
| 2724 | /* once for the tree */ |
| 2725 | btrfs_put_ordered_extent(ordered_extent); |
| 2726 | |
Josef Bacik | 5fd0204 | 2012-05-02 14:00:54 -0400 | [diff] [blame] | 2727 | return ret; |
| 2728 | } |
| 2729 | |
| 2730 | static void finish_ordered_fn(struct btrfs_work *work) |
| 2731 | { |
| 2732 | struct btrfs_ordered_extent *ordered_extent; |
| 2733 | ordered_extent = container_of(work, struct btrfs_ordered_extent, work); |
| 2734 | btrfs_finish_ordered_io(ordered_extent); |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 2735 | } |
| 2736 | |
Christoph Hellwig | b295086 | 2008-12-02 09:54:17 -0500 | [diff] [blame] | 2737 | 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] | 2738 | struct extent_state *state, int uptodate) |
| 2739 | { |
Josef Bacik | 5fd0204 | 2012-05-02 14:00:54 -0400 | [diff] [blame] | 2740 | struct inode *inode = page->mapping->host; |
| 2741 | struct btrfs_root *root = BTRFS_I(inode)->root; |
| 2742 | struct btrfs_ordered_extent *ordered_extent = NULL; |
| 2743 | struct btrfs_workers *workers; |
| 2744 | |
liubo | 1abe9b8 | 2011-03-24 11:18:59 +0000 | [diff] [blame] | 2745 | trace_btrfs_writepage_end_io_hook(page, start, end, uptodate); |
| 2746 | |
Chris Mason | 8b62b72 | 2009-09-02 16:53:46 -0400 | [diff] [blame] | 2747 | ClearPagePrivate2(page); |
Josef Bacik | 5fd0204 | 2012-05-02 14:00:54 -0400 | [diff] [blame] | 2748 | if (!btrfs_dec_test_ordered_pending(inode, &ordered_extent, start, |
| 2749 | end - start + 1, uptodate)) |
| 2750 | return 0; |
| 2751 | |
| 2752 | ordered_extent->work.func = finish_ordered_fn; |
| 2753 | ordered_extent->work.flags = 0; |
| 2754 | |
Liu Bo | 83eea1f | 2012-07-10 05:28:39 -0600 | [diff] [blame] | 2755 | if (btrfs_is_free_space_inode(inode)) |
Josef Bacik | 5fd0204 | 2012-05-02 14:00:54 -0400 | [diff] [blame] | 2756 | workers = &root->fs_info->endio_freespace_worker; |
| 2757 | else |
| 2758 | workers = &root->fs_info->endio_write_workers; |
| 2759 | btrfs_queue_worker(workers, &ordered_extent->work); |
| 2760 | |
| 2761 | return 0; |
Chris Mason | 211f90e | 2008-07-18 11:56:15 -0400 | [diff] [blame] | 2762 | } |
| 2763 | |
Chris Mason | d352ac6 | 2008-09-29 15:18:18 -0400 | [diff] [blame] | 2764 | /* |
Chris Mason | d352ac6 | 2008-09-29 15:18:18 -0400 | [diff] [blame] | 2765 | * 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] | 2766 | * if there's a match, we allow the bio to finish. If not, the code in |
| 2767 | * extent_io.c will try to find good copies for us. |
Chris Mason | d352ac6 | 2008-09-29 15:18:18 -0400 | [diff] [blame] | 2768 | */ |
Miao Xie | facc8a22 | 2013-07-25 19:22:34 +0800 | [diff] [blame] | 2769 | static int btrfs_readpage_end_io_hook(struct btrfs_io_bio *io_bio, |
| 2770 | u64 phy_offset, struct page *page, |
| 2771 | u64 start, u64 end, int mirror) |
Chris Mason | 07157aa | 2007-08-30 08:50:51 -0400 | [diff] [blame] | 2772 | { |
Miao Xie | 4eee4fa | 2012-12-21 09:17:45 +0000 | [diff] [blame] | 2773 | size_t offset = start - page_offset(page); |
Chris Mason | 07157aa | 2007-08-30 08:50:51 -0400 | [diff] [blame] | 2774 | struct inode *inode = page->mapping->host; |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 2775 | struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree; |
Chris Mason | 07157aa | 2007-08-30 08:50:51 -0400 | [diff] [blame] | 2776 | char *kaddr; |
Chris Mason | ff79f81 | 2007-10-15 16:22:25 -0400 | [diff] [blame] | 2777 | struct btrfs_root *root = BTRFS_I(inode)->root; |
Miao Xie | facc8a22 | 2013-07-25 19:22:34 +0800 | [diff] [blame] | 2778 | u32 csum_expected; |
Chris Mason | ff79f81 | 2007-10-15 16:22:25 -0400 | [diff] [blame] | 2779 | u32 csum = ~(u32)0; |
Simon Kirby | c2cf52e | 2013-03-19 22:41:23 +0000 | [diff] [blame] | 2780 | static DEFINE_RATELIMIT_STATE(_rs, DEFAULT_RATELIMIT_INTERVAL, |
| 2781 | DEFAULT_RATELIMIT_BURST); |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 2782 | |
Chris Mason | d20f704 | 2008-12-08 16:58:54 -0500 | [diff] [blame] | 2783 | if (PageChecked(page)) { |
| 2784 | ClearPageChecked(page); |
| 2785 | goto good; |
| 2786 | } |
Christoph Hellwig | 6cbff00 | 2009-04-17 10:37:41 +0200 | [diff] [blame] | 2787 | |
| 2788 | if (BTRFS_I(inode)->flags & BTRFS_INODE_NODATASUM) |
Jan Schmidt | 08d2f34 | 2011-05-04 16:18:50 +0200 | [diff] [blame] | 2789 | goto good; |
Chris Mason | d20f704 | 2008-12-08 16:58:54 -0500 | [diff] [blame] | 2790 | |
Yan Zheng | 17d217f | 2008-12-12 10:03:38 -0500 | [diff] [blame] | 2791 | if (root->root_key.objectid == BTRFS_DATA_RELOC_TREE_OBJECTID && |
Chris Mason | 9655d29 | 2009-09-02 15:22:30 -0400 | [diff] [blame] | 2792 | test_range_bit(io_tree, start, end, EXTENT_NODATASUM, 1, NULL)) { |
Yan Zheng | 17d217f | 2008-12-12 10:03:38 -0500 | [diff] [blame] | 2793 | clear_extent_bits(io_tree, start, end, EXTENT_NODATASUM, |
| 2794 | GFP_NOFS); |
| 2795 | return 0; |
| 2796 | } |
| 2797 | |
Miao Xie | facc8a22 | 2013-07-25 19:22:34 +0800 | [diff] [blame] | 2798 | phy_offset >>= inode->i_sb->s_blocksize_bits; |
| 2799 | csum_expected = *(((u32 *)io_bio->csum) + phy_offset); |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 2800 | |
Miao Xie | facc8a22 | 2013-07-25 19:22:34 +0800 | [diff] [blame] | 2801 | kaddr = kmap_atomic(page); |
Liu Bo | b049668 | 2013-03-14 14:57:45 +0000 | [diff] [blame] | 2802 | csum = btrfs_csum_data(kaddr + offset, csum, end - start + 1); |
Chris Mason | ff79f81 | 2007-10-15 16:22:25 -0400 | [diff] [blame] | 2803 | btrfs_csum_final(csum, (char *)&csum); |
Miao Xie | facc8a22 | 2013-07-25 19:22:34 +0800 | [diff] [blame] | 2804 | if (csum != csum_expected) |
Chris Mason | 07157aa | 2007-08-30 08:50:51 -0400 | [diff] [blame] | 2805 | goto zeroit; |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 2806 | |
Cong Wang | 7ac687d | 2011-11-25 23:14:28 +0800 | [diff] [blame] | 2807 | kunmap_atomic(kaddr); |
Chris Mason | d20f704 | 2008-12-08 16:58:54 -0500 | [diff] [blame] | 2808 | good: |
Chris Mason | 07157aa | 2007-08-30 08:50:51 -0400 | [diff] [blame] | 2809 | return 0; |
| 2810 | |
| 2811 | zeroit: |
Simon Kirby | c2cf52e | 2013-03-19 22:41:23 +0000 | [diff] [blame] | 2812 | if (__ratelimit(&_rs)) |
Miao Xie | facc8a22 | 2013-07-25 19:22:34 +0800 | [diff] [blame] | 2813 | 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] | 2814 | btrfs_ino(page->mapping->host), start, csum, csum_expected); |
Chris Mason | db94535 | 2007-10-15 16:15:53 -0400 | [diff] [blame] | 2815 | memset(kaddr + offset, 1, end - start + 1); |
| 2816 | flush_dcache_page(page); |
Cong Wang | 7ac687d | 2011-11-25 23:14:28 +0800 | [diff] [blame] | 2817 | kunmap_atomic(kaddr); |
Miao Xie | facc8a22 | 2013-07-25 19:22:34 +0800 | [diff] [blame] | 2818 | if (csum_expected == 0) |
Chris Mason | 3b95151 | 2008-04-17 11:29:12 -0400 | [diff] [blame] | 2819 | return 0; |
Chris Mason | 7e38326 | 2008-04-09 16:28:12 -0400 | [diff] [blame] | 2820 | return -EIO; |
Chris Mason | 07157aa | 2007-08-30 08:50:51 -0400 | [diff] [blame] | 2821 | } |
Chris Mason | b888db2 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 2822 | |
Yan, Zheng | 24bbcf0 | 2009-11-12 09:36:34 +0000 | [diff] [blame] | 2823 | struct delayed_iput { |
| 2824 | struct list_head list; |
| 2825 | struct inode *inode; |
| 2826 | }; |
| 2827 | |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 2828 | /* JDM: If this is fs-wide, why can't we add a pointer to |
| 2829 | * btrfs_inode instead and avoid the allocation? */ |
Yan, Zheng | 24bbcf0 | 2009-11-12 09:36:34 +0000 | [diff] [blame] | 2830 | void btrfs_add_delayed_iput(struct inode *inode) |
| 2831 | { |
| 2832 | struct btrfs_fs_info *fs_info = BTRFS_I(inode)->root->fs_info; |
| 2833 | struct delayed_iput *delayed; |
| 2834 | |
| 2835 | if (atomic_add_unless(&inode->i_count, -1, 1)) |
| 2836 | return; |
| 2837 | |
| 2838 | delayed = kmalloc(sizeof(*delayed), GFP_NOFS | __GFP_NOFAIL); |
| 2839 | delayed->inode = inode; |
| 2840 | |
| 2841 | spin_lock(&fs_info->delayed_iput_lock); |
| 2842 | list_add_tail(&delayed->list, &fs_info->delayed_iputs); |
| 2843 | spin_unlock(&fs_info->delayed_iput_lock); |
| 2844 | } |
| 2845 | |
| 2846 | void btrfs_run_delayed_iputs(struct btrfs_root *root) |
| 2847 | { |
| 2848 | LIST_HEAD(list); |
| 2849 | struct btrfs_fs_info *fs_info = root->fs_info; |
| 2850 | struct delayed_iput *delayed; |
| 2851 | int empty; |
| 2852 | |
| 2853 | spin_lock(&fs_info->delayed_iput_lock); |
| 2854 | empty = list_empty(&fs_info->delayed_iputs); |
| 2855 | spin_unlock(&fs_info->delayed_iput_lock); |
| 2856 | if (empty) |
| 2857 | return; |
| 2858 | |
Yan, Zheng | 24bbcf0 | 2009-11-12 09:36:34 +0000 | [diff] [blame] | 2859 | spin_lock(&fs_info->delayed_iput_lock); |
| 2860 | list_splice_init(&fs_info->delayed_iputs, &list); |
| 2861 | spin_unlock(&fs_info->delayed_iput_lock); |
| 2862 | |
| 2863 | while (!list_empty(&list)) { |
| 2864 | delayed = list_entry(list.next, struct delayed_iput, list); |
| 2865 | list_del(&delayed->list); |
| 2866 | iput(delayed->inode); |
| 2867 | kfree(delayed); |
| 2868 | } |
Yan, Zheng | 24bbcf0 | 2009-11-12 09:36:34 +0000 | [diff] [blame] | 2869 | } |
| 2870 | |
Yan, Zheng | d68fc57 | 2010-05-16 10:49:58 -0400 | [diff] [blame] | 2871 | /* |
Justin P. Mattock | 42b2aa8 | 2011-11-28 20:31:00 -0800 | [diff] [blame] | 2872 | * This is called in transaction commit time. If there are no orphan |
Yan, Zheng | d68fc57 | 2010-05-16 10:49:58 -0400 | [diff] [blame] | 2873 | * files in the subvolume, it removes orphan item and frees block_rsv |
| 2874 | * structure. |
| 2875 | */ |
| 2876 | void btrfs_orphan_commit_root(struct btrfs_trans_handle *trans, |
| 2877 | struct btrfs_root *root) |
| 2878 | { |
Josef Bacik | 90290e1 | 2011-12-02 15:44:12 -0500 | [diff] [blame] | 2879 | struct btrfs_block_rsv *block_rsv; |
Yan, Zheng | d68fc57 | 2010-05-16 10:49:58 -0400 | [diff] [blame] | 2880 | int ret; |
| 2881 | |
Josef Bacik | 8a35d95 | 2012-05-23 14:26:42 -0400 | [diff] [blame] | 2882 | if (atomic_read(&root->orphan_inodes) || |
Yan, Zheng | d68fc57 | 2010-05-16 10:49:58 -0400 | [diff] [blame] | 2883 | root->orphan_cleanup_state != ORPHAN_CLEANUP_DONE) |
| 2884 | return; |
| 2885 | |
Josef Bacik | 90290e1 | 2011-12-02 15:44:12 -0500 | [diff] [blame] | 2886 | spin_lock(&root->orphan_lock); |
Josef Bacik | 8a35d95 | 2012-05-23 14:26:42 -0400 | [diff] [blame] | 2887 | if (atomic_read(&root->orphan_inodes)) { |
Josef Bacik | 90290e1 | 2011-12-02 15:44:12 -0500 | [diff] [blame] | 2888 | spin_unlock(&root->orphan_lock); |
| 2889 | return; |
| 2890 | } |
| 2891 | |
| 2892 | if (root->orphan_cleanup_state != ORPHAN_CLEANUP_DONE) { |
| 2893 | spin_unlock(&root->orphan_lock); |
| 2894 | return; |
| 2895 | } |
| 2896 | |
| 2897 | block_rsv = root->orphan_block_rsv; |
| 2898 | root->orphan_block_rsv = NULL; |
| 2899 | spin_unlock(&root->orphan_lock); |
| 2900 | |
Yan, Zheng | d68fc57 | 2010-05-16 10:49:58 -0400 | [diff] [blame] | 2901 | if (root->orphan_item_inserted && |
| 2902 | btrfs_root_refs(&root->root_item) > 0) { |
| 2903 | ret = btrfs_del_orphan_item(trans, root->fs_info->tree_root, |
| 2904 | root->root_key.objectid); |
Josef Bacik | 4ef31a4 | 2013-08-13 14:10:08 -0400 | [diff] [blame] | 2905 | if (ret) |
| 2906 | btrfs_abort_transaction(trans, root, ret); |
| 2907 | else |
| 2908 | root->orphan_item_inserted = 0; |
Yan, Zheng | d68fc57 | 2010-05-16 10:49:58 -0400 | [diff] [blame] | 2909 | } |
| 2910 | |
Josef Bacik | 90290e1 | 2011-12-02 15:44:12 -0500 | [diff] [blame] | 2911 | if (block_rsv) { |
| 2912 | WARN_ON(block_rsv->size > 0); |
| 2913 | btrfs_free_block_rsv(root, block_rsv); |
Yan, Zheng | d68fc57 | 2010-05-16 10:49:58 -0400 | [diff] [blame] | 2914 | } |
| 2915 | } |
| 2916 | |
| 2917 | /* |
Josef Bacik | 7b12876 | 2008-07-24 12:17:14 -0400 | [diff] [blame] | 2918 | * This creates an orphan entry for the given inode in case something goes |
| 2919 | * wrong in the middle of an unlink/truncate. |
Yan, Zheng | d68fc57 | 2010-05-16 10:49:58 -0400 | [diff] [blame] | 2920 | * |
| 2921 | * NOTE: caller of this function should reserve 5 units of metadata for |
| 2922 | * this function. |
Josef Bacik | 7b12876 | 2008-07-24 12:17:14 -0400 | [diff] [blame] | 2923 | */ |
| 2924 | int btrfs_orphan_add(struct btrfs_trans_handle *trans, struct inode *inode) |
| 2925 | { |
| 2926 | struct btrfs_root *root = BTRFS_I(inode)->root; |
Yan, Zheng | d68fc57 | 2010-05-16 10:49:58 -0400 | [diff] [blame] | 2927 | struct btrfs_block_rsv *block_rsv = NULL; |
| 2928 | int reserve = 0; |
| 2929 | int insert = 0; |
| 2930 | int ret; |
Josef Bacik | 7b12876 | 2008-07-24 12:17:14 -0400 | [diff] [blame] | 2931 | |
Yan, Zheng | d68fc57 | 2010-05-16 10:49:58 -0400 | [diff] [blame] | 2932 | if (!root->orphan_block_rsv) { |
Miao Xie | 66d8f3d | 2012-09-06 04:02:28 -0600 | [diff] [blame] | 2933 | block_rsv = btrfs_alloc_block_rsv(root, BTRFS_BLOCK_RSV_TEMP); |
Tsutomu Itoh | b532402 | 2011-07-19 07:27:20 +0000 | [diff] [blame] | 2934 | if (!block_rsv) |
| 2935 | return -ENOMEM; |
Josef Bacik | 7b12876 | 2008-07-24 12:17:14 -0400 | [diff] [blame] | 2936 | } |
| 2937 | |
Yan, Zheng | d68fc57 | 2010-05-16 10:49:58 -0400 | [diff] [blame] | 2938 | spin_lock(&root->orphan_lock); |
| 2939 | if (!root->orphan_block_rsv) { |
| 2940 | root->orphan_block_rsv = block_rsv; |
| 2941 | } else if (block_rsv) { |
| 2942 | btrfs_free_block_rsv(root, block_rsv); |
| 2943 | block_rsv = NULL; |
| 2944 | } |
Josef Bacik | 7b12876 | 2008-07-24 12:17:14 -0400 | [diff] [blame] | 2945 | |
Josef Bacik | 8a35d95 | 2012-05-23 14:26:42 -0400 | [diff] [blame] | 2946 | if (!test_and_set_bit(BTRFS_INODE_HAS_ORPHAN_ITEM, |
| 2947 | &BTRFS_I(inode)->runtime_flags)) { |
Yan, Zheng | d68fc57 | 2010-05-16 10:49:58 -0400 | [diff] [blame] | 2948 | #if 0 |
| 2949 | /* |
| 2950 | * For proper ENOSPC handling, we should do orphan |
| 2951 | * cleanup when mounting. But this introduces backward |
| 2952 | * compatibility issue. |
| 2953 | */ |
| 2954 | if (!xchg(&root->orphan_item_inserted, 1)) |
| 2955 | insert = 2; |
| 2956 | else |
| 2957 | insert = 1; |
| 2958 | #endif |
| 2959 | insert = 1; |
Miao Xie | 321f0e7 | 2012-08-28 22:13:02 -0600 | [diff] [blame] | 2960 | atomic_inc(&root->orphan_inodes); |
Yan, Zheng | d68fc57 | 2010-05-16 10:49:58 -0400 | [diff] [blame] | 2961 | } |
Josef Bacik | 7b12876 | 2008-07-24 12:17:14 -0400 | [diff] [blame] | 2962 | |
Josef Bacik | 72ac3c0 | 2012-05-23 14:13:11 -0400 | [diff] [blame] | 2963 | if (!test_and_set_bit(BTRFS_INODE_ORPHAN_META_RESERVED, |
| 2964 | &BTRFS_I(inode)->runtime_flags)) |
Yan, Zheng | d68fc57 | 2010-05-16 10:49:58 -0400 | [diff] [blame] | 2965 | reserve = 1; |
Yan, Zheng | d68fc57 | 2010-05-16 10:49:58 -0400 | [diff] [blame] | 2966 | spin_unlock(&root->orphan_lock); |
Josef Bacik | 7b12876 | 2008-07-24 12:17:14 -0400 | [diff] [blame] | 2967 | |
Yan, Zheng | d68fc57 | 2010-05-16 10:49:58 -0400 | [diff] [blame] | 2968 | /* grab metadata reservation from transaction handle */ |
| 2969 | if (reserve) { |
| 2970 | ret = btrfs_orphan_reserve_metadata(trans, inode); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 2971 | BUG_ON(ret); /* -ENOSPC in reservation; Logic error? JDM */ |
Yan, Zheng | d68fc57 | 2010-05-16 10:49:58 -0400 | [diff] [blame] | 2972 | } |
| 2973 | |
| 2974 | /* insert an orphan item to track this unlinked/truncated file */ |
| 2975 | if (insert >= 1) { |
Li Zefan | 33345d01 | 2011-04-20 10:31:50 +0800 | [diff] [blame] | 2976 | ret = btrfs_insert_orphan_item(trans, root, btrfs_ino(inode)); |
Josef Bacik | 4ef31a4 | 2013-08-13 14:10:08 -0400 | [diff] [blame] | 2977 | if (ret) { |
Filipe David Borba Manana | 703c88e | 2013-09-22 21:54:55 +0100 | [diff] [blame] | 2978 | atomic_dec(&root->orphan_inodes); |
Josef Bacik | 4ef31a4 | 2013-08-13 14:10:08 -0400 | [diff] [blame] | 2979 | if (reserve) { |
| 2980 | clear_bit(BTRFS_INODE_ORPHAN_META_RESERVED, |
| 2981 | &BTRFS_I(inode)->runtime_flags); |
| 2982 | btrfs_orphan_release_metadata(inode); |
| 2983 | } |
| 2984 | if (ret != -EEXIST) { |
Josef Bacik | e8e7cff | 2013-08-21 15:54:00 -0400 | [diff] [blame] | 2985 | clear_bit(BTRFS_INODE_HAS_ORPHAN_ITEM, |
| 2986 | &BTRFS_I(inode)->runtime_flags); |
Josef Bacik | 4ef31a4 | 2013-08-13 14:10:08 -0400 | [diff] [blame] | 2987 | btrfs_abort_transaction(trans, root, ret); |
| 2988 | return ret; |
| 2989 | } |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 2990 | } |
| 2991 | ret = 0; |
Yan, Zheng | d68fc57 | 2010-05-16 10:49:58 -0400 | [diff] [blame] | 2992 | } |
| 2993 | |
| 2994 | /* insert an orphan item to track subvolume contains orphan files */ |
| 2995 | if (insert >= 2) { |
| 2996 | ret = btrfs_insert_orphan_item(trans, root->fs_info->tree_root, |
| 2997 | root->root_key.objectid); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 2998 | if (ret && ret != -EEXIST) { |
| 2999 | btrfs_abort_transaction(trans, root, ret); |
| 3000 | return ret; |
| 3001 | } |
Yan, Zheng | d68fc57 | 2010-05-16 10:49:58 -0400 | [diff] [blame] | 3002 | } |
| 3003 | return 0; |
Josef Bacik | 7b12876 | 2008-07-24 12:17:14 -0400 | [diff] [blame] | 3004 | } |
| 3005 | |
| 3006 | /* |
| 3007 | * We have done the truncate/delete so we can go ahead and remove the orphan |
| 3008 | * item for this particular inode. |
| 3009 | */ |
Eric Sandeen | 48a3b63 | 2013-04-25 20:41:01 +0000 | [diff] [blame] | 3010 | static int btrfs_orphan_del(struct btrfs_trans_handle *trans, |
| 3011 | struct inode *inode) |
Josef Bacik | 7b12876 | 2008-07-24 12:17:14 -0400 | [diff] [blame] | 3012 | { |
| 3013 | struct btrfs_root *root = BTRFS_I(inode)->root; |
Yan, Zheng | d68fc57 | 2010-05-16 10:49:58 -0400 | [diff] [blame] | 3014 | int delete_item = 0; |
| 3015 | int release_rsv = 0; |
Josef Bacik | 7b12876 | 2008-07-24 12:17:14 -0400 | [diff] [blame] | 3016 | int ret = 0; |
| 3017 | |
Yan, Zheng | d68fc57 | 2010-05-16 10:49:58 -0400 | [diff] [blame] | 3018 | spin_lock(&root->orphan_lock); |
Josef Bacik | 8a35d95 | 2012-05-23 14:26:42 -0400 | [diff] [blame] | 3019 | if (test_and_clear_bit(BTRFS_INODE_HAS_ORPHAN_ITEM, |
| 3020 | &BTRFS_I(inode)->runtime_flags)) |
Yan, Zheng | d68fc57 | 2010-05-16 10:49:58 -0400 | [diff] [blame] | 3021 | delete_item = 1; |
Josef Bacik | 7b12876 | 2008-07-24 12:17:14 -0400 | [diff] [blame] | 3022 | |
Josef Bacik | 72ac3c0 | 2012-05-23 14:13:11 -0400 | [diff] [blame] | 3023 | if (test_and_clear_bit(BTRFS_INODE_ORPHAN_META_RESERVED, |
| 3024 | &BTRFS_I(inode)->runtime_flags)) |
Yan, Zheng | d68fc57 | 2010-05-16 10:49:58 -0400 | [diff] [blame] | 3025 | release_rsv = 1; |
Yan, Zheng | d68fc57 | 2010-05-16 10:49:58 -0400 | [diff] [blame] | 3026 | spin_unlock(&root->orphan_lock); |
| 3027 | |
Filipe David Borba Manana | 703c88e | 2013-09-22 21:54:55 +0100 | [diff] [blame] | 3028 | if (delete_item) { |
Josef Bacik | 8a35d95 | 2012-05-23 14:26:42 -0400 | [diff] [blame] | 3029 | atomic_dec(&root->orphan_inodes); |
Filipe David Borba Manana | 703c88e | 2013-09-22 21:54:55 +0100 | [diff] [blame] | 3030 | if (trans) |
| 3031 | ret = btrfs_del_orphan_item(trans, root, |
| 3032 | btrfs_ino(inode)); |
Josef Bacik | 8a35d95 | 2012-05-23 14:26:42 -0400 | [diff] [blame] | 3033 | } |
Josef Bacik | 7b12876 | 2008-07-24 12:17:14 -0400 | [diff] [blame] | 3034 | |
Filipe David Borba Manana | 703c88e | 2013-09-22 21:54:55 +0100 | [diff] [blame] | 3035 | if (release_rsv) |
| 3036 | btrfs_orphan_release_metadata(inode); |
| 3037 | |
Josef Bacik | 4ef31a4 | 2013-08-13 14:10:08 -0400 | [diff] [blame] | 3038 | return ret; |
Josef Bacik | 7b12876 | 2008-07-24 12:17:14 -0400 | [diff] [blame] | 3039 | } |
| 3040 | |
| 3041 | /* |
| 3042 | * this cleans up any orphans that may be left on the list from the last use |
| 3043 | * of this root. |
| 3044 | */ |
Josef Bacik | 66b4ffd | 2011-01-31 16:22:42 -0500 | [diff] [blame] | 3045 | int btrfs_orphan_cleanup(struct btrfs_root *root) |
Josef Bacik | 7b12876 | 2008-07-24 12:17:14 -0400 | [diff] [blame] | 3046 | { |
| 3047 | struct btrfs_path *path; |
| 3048 | struct extent_buffer *leaf; |
Josef Bacik | 7b12876 | 2008-07-24 12:17:14 -0400 | [diff] [blame] | 3049 | struct btrfs_key key, found_key; |
| 3050 | struct btrfs_trans_handle *trans; |
| 3051 | struct inode *inode; |
Josef Bacik | 8f6d7f4 | 2011-09-26 15:55:20 -0400 | [diff] [blame] | 3052 | u64 last_objectid = 0; |
Josef Bacik | 7b12876 | 2008-07-24 12:17:14 -0400 | [diff] [blame] | 3053 | int ret = 0, nr_unlink = 0, nr_truncate = 0; |
| 3054 | |
Yan, Zheng | d68fc57 | 2010-05-16 10:49:58 -0400 | [diff] [blame] | 3055 | if (cmpxchg(&root->orphan_cleanup_state, 0, ORPHAN_CLEANUP_STARTED)) |
Josef Bacik | 66b4ffd | 2011-01-31 16:22:42 -0500 | [diff] [blame] | 3056 | return 0; |
Yan, Zheng | c71bf09 | 2009-11-12 09:34:40 +0000 | [diff] [blame] | 3057 | |
| 3058 | path = btrfs_alloc_path(); |
Josef Bacik | 66b4ffd | 2011-01-31 16:22:42 -0500 | [diff] [blame] | 3059 | if (!path) { |
| 3060 | ret = -ENOMEM; |
| 3061 | goto out; |
| 3062 | } |
Josef Bacik | 7b12876 | 2008-07-24 12:17:14 -0400 | [diff] [blame] | 3063 | path->reada = -1; |
| 3064 | |
| 3065 | key.objectid = BTRFS_ORPHAN_OBJECTID; |
| 3066 | btrfs_set_key_type(&key, BTRFS_ORPHAN_ITEM_KEY); |
| 3067 | key.offset = (u64)-1; |
| 3068 | |
Josef Bacik | 7b12876 | 2008-07-24 12:17:14 -0400 | [diff] [blame] | 3069 | while (1) { |
| 3070 | ret = btrfs_search_slot(NULL, root, &key, path, 0, 0); |
Josef Bacik | 66b4ffd | 2011-01-31 16:22:42 -0500 | [diff] [blame] | 3071 | if (ret < 0) |
| 3072 | goto out; |
Josef Bacik | 7b12876 | 2008-07-24 12:17:14 -0400 | [diff] [blame] | 3073 | |
| 3074 | /* |
| 3075 | * 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] | 3076 | * 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] | 3077 | * find the key and see if we have stuff that matches |
| 3078 | */ |
| 3079 | if (ret > 0) { |
Josef Bacik | 66b4ffd | 2011-01-31 16:22:42 -0500 | [diff] [blame] | 3080 | ret = 0; |
Josef Bacik | 7b12876 | 2008-07-24 12:17:14 -0400 | [diff] [blame] | 3081 | if (path->slots[0] == 0) |
| 3082 | break; |
| 3083 | path->slots[0]--; |
| 3084 | } |
| 3085 | |
| 3086 | /* pull out the item */ |
| 3087 | leaf = path->nodes[0]; |
Josef Bacik | 7b12876 | 2008-07-24 12:17:14 -0400 | [diff] [blame] | 3088 | btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]); |
| 3089 | |
| 3090 | /* make sure the item matches what we want */ |
| 3091 | if (found_key.objectid != BTRFS_ORPHAN_OBJECTID) |
| 3092 | break; |
| 3093 | if (btrfs_key_type(&found_key) != BTRFS_ORPHAN_ITEM_KEY) |
| 3094 | break; |
| 3095 | |
| 3096 | /* release the path since we're done with it */ |
David Sterba | b3b4aa7 | 2011-04-21 01:20:15 +0200 | [diff] [blame] | 3097 | btrfs_release_path(path); |
Josef Bacik | 7b12876 | 2008-07-24 12:17:14 -0400 | [diff] [blame] | 3098 | |
| 3099 | /* |
| 3100 | * this is where we are basically btrfs_lookup, without the |
| 3101 | * crossing root thing. we store the inode number in the |
| 3102 | * offset of the orphan item. |
| 3103 | */ |
Josef Bacik | 8f6d7f4 | 2011-09-26 15:55:20 -0400 | [diff] [blame] | 3104 | |
| 3105 | if (found_key.offset == last_objectid) { |
Simon Kirby | c2cf52e | 2013-03-19 22:41:23 +0000 | [diff] [blame] | 3106 | btrfs_err(root->fs_info, |
| 3107 | "Error removing orphan entry, stopping orphan cleanup"); |
Josef Bacik | 8f6d7f4 | 2011-09-26 15:55:20 -0400 | [diff] [blame] | 3108 | ret = -EINVAL; |
| 3109 | goto out; |
| 3110 | } |
| 3111 | |
| 3112 | last_objectid = found_key.offset; |
| 3113 | |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 3114 | found_key.objectid = found_key.offset; |
| 3115 | found_key.type = BTRFS_INODE_ITEM_KEY; |
| 3116 | found_key.offset = 0; |
Josef Bacik | 73f7341 | 2009-12-04 17:38:27 +0000 | [diff] [blame] | 3117 | inode = btrfs_iget(root->fs_info->sb, &found_key, root, NULL); |
Rusty Russell | 8c6ffba | 2013-07-15 11:20:32 +0930 | [diff] [blame] | 3118 | ret = PTR_ERR_OR_ZERO(inode); |
Josef Bacik | a8c9e57 | 2011-09-21 16:55:59 -0400 | [diff] [blame] | 3119 | if (ret && ret != -ESTALE) |
Josef Bacik | 66b4ffd | 2011-01-31 16:22:42 -0500 | [diff] [blame] | 3120 | goto out; |
Josef Bacik | a8c9e57 | 2011-09-21 16:55:59 -0400 | [diff] [blame] | 3121 | |
Arne Jansen | f8e9e0b | 2011-12-14 20:12:02 -0500 | [diff] [blame] | 3122 | if (ret == -ESTALE && root == root->fs_info->tree_root) { |
| 3123 | struct btrfs_root *dead_root; |
| 3124 | struct btrfs_fs_info *fs_info = root->fs_info; |
| 3125 | int is_dead_root = 0; |
| 3126 | |
| 3127 | /* |
| 3128 | * this is an orphan in the tree root. Currently these |
| 3129 | * could come from 2 sources: |
| 3130 | * a) a snapshot deletion in progress |
| 3131 | * b) a free space cache inode |
| 3132 | * We need to distinguish those two, as the snapshot |
| 3133 | * orphan must not get deleted. |
| 3134 | * find_dead_roots already ran before us, so if this |
| 3135 | * is a snapshot deletion, we should find the root |
| 3136 | * in the dead_roots list |
| 3137 | */ |
| 3138 | spin_lock(&fs_info->trans_lock); |
| 3139 | list_for_each_entry(dead_root, &fs_info->dead_roots, |
| 3140 | root_list) { |
| 3141 | if (dead_root->root_key.objectid == |
| 3142 | found_key.objectid) { |
| 3143 | is_dead_root = 1; |
| 3144 | break; |
| 3145 | } |
| 3146 | } |
| 3147 | spin_unlock(&fs_info->trans_lock); |
| 3148 | if (is_dead_root) { |
| 3149 | /* prevent this orphan from being found again */ |
| 3150 | key.offset = found_key.objectid - 1; |
| 3151 | continue; |
| 3152 | } |
| 3153 | } |
Josef Bacik | a8c9e57 | 2011-09-21 16:55:59 -0400 | [diff] [blame] | 3154 | /* |
| 3155 | * Inode is already gone but the orphan item is still there, |
| 3156 | * kill the orphan item. |
| 3157 | */ |
| 3158 | if (ret == -ESTALE) { |
| 3159 | trans = btrfs_start_transaction(root, 1); |
| 3160 | if (IS_ERR(trans)) { |
| 3161 | ret = PTR_ERR(trans); |
| 3162 | goto out; |
| 3163 | } |
Simon Kirby | c2cf52e | 2013-03-19 22:41:23 +0000 | [diff] [blame] | 3164 | btrfs_debug(root->fs_info, "auto deleting %Lu", |
| 3165 | found_key.objectid); |
Josef Bacik | a8c9e57 | 2011-09-21 16:55:59 -0400 | [diff] [blame] | 3166 | ret = btrfs_del_orphan_item(trans, root, |
| 3167 | found_key.objectid); |
Josef Bacik | a8c9e57 | 2011-09-21 16:55:59 -0400 | [diff] [blame] | 3168 | btrfs_end_transaction(trans, root); |
Josef Bacik | 4ef31a4 | 2013-08-13 14:10:08 -0400 | [diff] [blame] | 3169 | if (ret) |
| 3170 | goto out; |
Josef Bacik | a8c9e57 | 2011-09-21 16:55:59 -0400 | [diff] [blame] | 3171 | continue; |
Josef Bacik | 66b4ffd | 2011-01-31 16:22:42 -0500 | [diff] [blame] | 3172 | } |
Josef Bacik | 7b12876 | 2008-07-24 12:17:14 -0400 | [diff] [blame] | 3173 | |
Josef Bacik | 7b12876 | 2008-07-24 12:17:14 -0400 | [diff] [blame] | 3174 | /* |
| 3175 | * add this inode to the orphan list so btrfs_orphan_del does |
| 3176 | * the proper thing when we hit it |
| 3177 | */ |
Josef Bacik | 8a35d95 | 2012-05-23 14:26:42 -0400 | [diff] [blame] | 3178 | set_bit(BTRFS_INODE_HAS_ORPHAN_ITEM, |
| 3179 | &BTRFS_I(inode)->runtime_flags); |
Josef Bacik | 925396e | 2013-02-01 15:57:47 -0500 | [diff] [blame] | 3180 | atomic_inc(&root->orphan_inodes); |
Josef Bacik | 7b12876 | 2008-07-24 12:17:14 -0400 | [diff] [blame] | 3181 | |
Josef Bacik | 7b12876 | 2008-07-24 12:17:14 -0400 | [diff] [blame] | 3182 | /* if we have links, this was a truncate, lets do that */ |
| 3183 | if (inode->i_nlink) { |
Josef Bacik | a41ad39 | 2011-01-31 15:30:16 -0500 | [diff] [blame] | 3184 | if (!S_ISREG(inode->i_mode)) { |
| 3185 | WARN_ON(1); |
| 3186 | iput(inode); |
| 3187 | continue; |
| 3188 | } |
Josef Bacik | 7b12876 | 2008-07-24 12:17:14 -0400 | [diff] [blame] | 3189 | nr_truncate++; |
Josef Bacik | f3fe820 | 2013-01-07 17:03:21 -0500 | [diff] [blame] | 3190 | |
| 3191 | /* 1 for the orphan item deletion. */ |
| 3192 | trans = btrfs_start_transaction(root, 1); |
| 3193 | if (IS_ERR(trans)) { |
Josef Bacik | c69b26b | 2013-06-03 16:51:23 -0400 | [diff] [blame] | 3194 | iput(inode); |
Josef Bacik | f3fe820 | 2013-01-07 17:03:21 -0500 | [diff] [blame] | 3195 | ret = PTR_ERR(trans); |
| 3196 | goto out; |
| 3197 | } |
| 3198 | ret = btrfs_orphan_add(trans, inode); |
| 3199 | btrfs_end_transaction(trans, root); |
Josef Bacik | c69b26b | 2013-06-03 16:51:23 -0400 | [diff] [blame] | 3200 | if (ret) { |
| 3201 | iput(inode); |
Josef Bacik | f3fe820 | 2013-01-07 17:03:21 -0500 | [diff] [blame] | 3202 | goto out; |
Josef Bacik | c69b26b | 2013-06-03 16:51:23 -0400 | [diff] [blame] | 3203 | } |
Josef Bacik | f3fe820 | 2013-01-07 17:03:21 -0500 | [diff] [blame] | 3204 | |
Josef Bacik | 66b4ffd | 2011-01-31 16:22:42 -0500 | [diff] [blame] | 3205 | ret = btrfs_truncate(inode); |
Josef Bacik | 4a7d0f6 | 2013-02-07 16:27:28 -0500 | [diff] [blame] | 3206 | if (ret) |
| 3207 | btrfs_orphan_del(NULL, inode); |
Josef Bacik | 7b12876 | 2008-07-24 12:17:14 -0400 | [diff] [blame] | 3208 | } else { |
| 3209 | nr_unlink++; |
| 3210 | } |
| 3211 | |
| 3212 | /* this will do delete_inode and everything for us */ |
| 3213 | iput(inode); |
Josef Bacik | 66b4ffd | 2011-01-31 16:22:42 -0500 | [diff] [blame] | 3214 | if (ret) |
| 3215 | goto out; |
Josef Bacik | 7b12876 | 2008-07-24 12:17:14 -0400 | [diff] [blame] | 3216 | } |
Miao Xie | 3254c87 | 2011-11-10 20:45:05 -0500 | [diff] [blame] | 3217 | /* release the path since we're done with it */ |
| 3218 | btrfs_release_path(path); |
| 3219 | |
Yan, Zheng | d68fc57 | 2010-05-16 10:49:58 -0400 | [diff] [blame] | 3220 | root->orphan_cleanup_state = ORPHAN_CLEANUP_DONE; |
| 3221 | |
| 3222 | if (root->orphan_block_rsv) |
| 3223 | btrfs_block_rsv_release(root, root->orphan_block_rsv, |
| 3224 | (u64)-1); |
| 3225 | |
| 3226 | if (root->orphan_block_rsv || root->orphan_item_inserted) { |
Josef Bacik | 7a7eaa4 | 2011-04-13 12:54:33 -0400 | [diff] [blame] | 3227 | trans = btrfs_join_transaction(root); |
Josef Bacik | 66b4ffd | 2011-01-31 16:22:42 -0500 | [diff] [blame] | 3228 | if (!IS_ERR(trans)) |
| 3229 | btrfs_end_transaction(trans, root); |
Yan, Zheng | d68fc57 | 2010-05-16 10:49:58 -0400 | [diff] [blame] | 3230 | } |
Josef Bacik | 7b12876 | 2008-07-24 12:17:14 -0400 | [diff] [blame] | 3231 | |
| 3232 | if (nr_unlink) |
David Sterba | 4884b47 | 2013-03-20 13:31:27 +0000 | [diff] [blame] | 3233 | btrfs_debug(root->fs_info, "unlinked %d orphans", nr_unlink); |
Josef Bacik | 7b12876 | 2008-07-24 12:17:14 -0400 | [diff] [blame] | 3234 | if (nr_truncate) |
David Sterba | 4884b47 | 2013-03-20 13:31:27 +0000 | [diff] [blame] | 3235 | btrfs_debug(root->fs_info, "truncated %d orphans", nr_truncate); |
Josef Bacik | 66b4ffd | 2011-01-31 16:22:42 -0500 | [diff] [blame] | 3236 | |
| 3237 | out: |
| 3238 | if (ret) |
Simon Kirby | c2cf52e | 2013-03-19 22:41:23 +0000 | [diff] [blame] | 3239 | btrfs_crit(root->fs_info, |
| 3240 | "could not do orphan cleanup %d", ret); |
Josef Bacik | 66b4ffd | 2011-01-31 16:22:42 -0500 | [diff] [blame] | 3241 | btrfs_free_path(path); |
| 3242 | return ret; |
Josef Bacik | 7b12876 | 2008-07-24 12:17:14 -0400 | [diff] [blame] | 3243 | } |
| 3244 | |
Chris Mason | d352ac6 | 2008-09-29 15:18:18 -0400 | [diff] [blame] | 3245 | /* |
Chris Mason | 46a53cc | 2009-04-27 11:47:50 -0400 | [diff] [blame] | 3246 | * very simple check to peek ahead in the leaf looking for xattrs. If we |
| 3247 | * don't find any xattrs, we know there can't be any acls. |
| 3248 | * |
| 3249 | * slot is the slot the inode is in, objectid is the objectid of the inode |
| 3250 | */ |
| 3251 | static noinline int acls_after_inode_item(struct extent_buffer *leaf, |
| 3252 | int slot, u64 objectid) |
| 3253 | { |
| 3254 | u32 nritems = btrfs_header_nritems(leaf); |
| 3255 | struct btrfs_key found_key; |
Josef Bacik | f23b5a5 | 2013-06-19 10:16:26 -0400 | [diff] [blame] | 3256 | static u64 xattr_access = 0; |
| 3257 | static u64 xattr_default = 0; |
Chris Mason | 46a53cc | 2009-04-27 11:47:50 -0400 | [diff] [blame] | 3258 | int scanned = 0; |
| 3259 | |
Josef Bacik | f23b5a5 | 2013-06-19 10:16:26 -0400 | [diff] [blame] | 3260 | if (!xattr_access) { |
| 3261 | xattr_access = btrfs_name_hash(POSIX_ACL_XATTR_ACCESS, |
| 3262 | strlen(POSIX_ACL_XATTR_ACCESS)); |
| 3263 | xattr_default = btrfs_name_hash(POSIX_ACL_XATTR_DEFAULT, |
| 3264 | strlen(POSIX_ACL_XATTR_DEFAULT)); |
| 3265 | } |
| 3266 | |
Chris Mason | 46a53cc | 2009-04-27 11:47:50 -0400 | [diff] [blame] | 3267 | slot++; |
| 3268 | while (slot < nritems) { |
| 3269 | btrfs_item_key_to_cpu(leaf, &found_key, slot); |
| 3270 | |
| 3271 | /* we found a different objectid, there must not be acls */ |
| 3272 | if (found_key.objectid != objectid) |
| 3273 | return 0; |
| 3274 | |
| 3275 | /* we found an xattr, assume we've got an acl */ |
Josef Bacik | f23b5a5 | 2013-06-19 10:16:26 -0400 | [diff] [blame] | 3276 | if (found_key.type == BTRFS_XATTR_ITEM_KEY) { |
| 3277 | if (found_key.offset == xattr_access || |
| 3278 | found_key.offset == xattr_default) |
| 3279 | return 1; |
| 3280 | } |
Chris Mason | 46a53cc | 2009-04-27 11:47:50 -0400 | [diff] [blame] | 3281 | |
| 3282 | /* |
| 3283 | * we found a key greater than an xattr key, there can't |
| 3284 | * be any acls later on |
| 3285 | */ |
| 3286 | if (found_key.type > BTRFS_XATTR_ITEM_KEY) |
| 3287 | return 0; |
| 3288 | |
| 3289 | slot++; |
| 3290 | scanned++; |
| 3291 | |
| 3292 | /* |
| 3293 | * it goes inode, inode backrefs, xattrs, extents, |
| 3294 | * so if there are a ton of hard links to an inode there can |
| 3295 | * be a lot of backrefs. Don't waste time searching too hard, |
| 3296 | * this is just an optimization |
| 3297 | */ |
| 3298 | if (scanned >= 8) |
| 3299 | break; |
| 3300 | } |
| 3301 | /* we hit the end of the leaf before we found an xattr or |
| 3302 | * something larger than an xattr. We have to assume the inode |
| 3303 | * has acls |
| 3304 | */ |
| 3305 | return 1; |
| 3306 | } |
| 3307 | |
| 3308 | /* |
Chris Mason | d352ac6 | 2008-09-29 15:18:18 -0400 | [diff] [blame] | 3309 | * read an inode from the btree into the in-memory inode |
| 3310 | */ |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 3311 | static void btrfs_read_locked_inode(struct inode *inode) |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3312 | { |
| 3313 | struct btrfs_path *path; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 3314 | struct extent_buffer *leaf; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3315 | struct btrfs_inode_item *inode_item; |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 3316 | struct btrfs_timespec *tspec; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3317 | struct btrfs_root *root = BTRFS_I(inode)->root; |
| 3318 | struct btrfs_key location; |
Chris Mason | 46a53cc | 2009-04-27 11:47:50 -0400 | [diff] [blame] | 3319 | int maybe_acls; |
Josef Bacik | 618e21d | 2007-07-11 10:18:17 -0400 | [diff] [blame] | 3320 | u32 rdev; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3321 | int ret; |
Miao Xie | 2f7e33d | 2011-06-23 07:27:13 +0000 | [diff] [blame] | 3322 | bool filled = false; |
| 3323 | |
| 3324 | ret = btrfs_fill_inode(inode, &rdev); |
| 3325 | if (!ret) |
| 3326 | filled = true; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3327 | |
| 3328 | path = btrfs_alloc_path(); |
Mark Fasheh | 1748f84 | 2011-07-12 11:25:31 -0700 | [diff] [blame] | 3329 | if (!path) |
| 3330 | goto make_bad; |
| 3331 | |
Josef Bacik | d90c732 | 2011-05-17 09:50:54 -0400 | [diff] [blame] | 3332 | path->leave_spinning = 1; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3333 | memcpy(&location, &BTRFS_I(inode)->location, sizeof(location)); |
Chris Mason | dc17ff8 | 2008-01-08 15:46:30 -0500 | [diff] [blame] | 3334 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3335 | ret = btrfs_lookup_inode(NULL, root, path, &location, 0); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 3336 | if (ret) |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3337 | goto make_bad; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3338 | |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 3339 | leaf = path->nodes[0]; |
Miao Xie | 2f7e33d | 2011-06-23 07:27:13 +0000 | [diff] [blame] | 3340 | |
| 3341 | if (filled) |
| 3342 | goto cache_acl; |
| 3343 | |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 3344 | inode_item = btrfs_item_ptr(leaf, path->slots[0], |
| 3345 | struct btrfs_inode_item); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 3346 | inode->i_mode = btrfs_inode_mode(leaf, inode_item); |
Miklos Szeredi | bfe8684 | 2011-10-28 14:13:29 +0200 | [diff] [blame] | 3347 | set_nlink(inode, btrfs_inode_nlink(leaf, inode_item)); |
Eric W. Biederman | 2f2f43d | 2012-02-10 11:05:07 -0800 | [diff] [blame] | 3348 | i_uid_write(inode, btrfs_inode_uid(leaf, inode_item)); |
| 3349 | i_gid_write(inode, btrfs_inode_gid(leaf, inode_item)); |
Chris Mason | dbe674a | 2008-07-17 12:54:05 -0400 | [diff] [blame] | 3350 | btrfs_i_size_write(inode, btrfs_inode_size(leaf, inode_item)); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 3351 | |
| 3352 | tspec = btrfs_inode_atime(inode_item); |
| 3353 | inode->i_atime.tv_sec = btrfs_timespec_sec(leaf, tspec); |
| 3354 | inode->i_atime.tv_nsec = btrfs_timespec_nsec(leaf, tspec); |
| 3355 | |
| 3356 | tspec = btrfs_inode_mtime(inode_item); |
| 3357 | inode->i_mtime.tv_sec = btrfs_timespec_sec(leaf, tspec); |
| 3358 | inode->i_mtime.tv_nsec = btrfs_timespec_nsec(leaf, tspec); |
| 3359 | |
| 3360 | tspec = btrfs_inode_ctime(inode_item); |
| 3361 | inode->i_ctime.tv_sec = btrfs_timespec_sec(leaf, tspec); |
| 3362 | inode->i_ctime.tv_nsec = btrfs_timespec_nsec(leaf, tspec); |
| 3363 | |
Yan Zheng | a76a3cd | 2008-10-09 11:46:29 -0400 | [diff] [blame] | 3364 | inode_set_bytes(inode, btrfs_inode_nbytes(leaf, inode_item)); |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 3365 | BTRFS_I(inode)->generation = btrfs_inode_generation(leaf, inode_item); |
Josef Bacik | 5dc562c | 2012-08-17 13:14:17 -0400 | [diff] [blame] | 3366 | BTRFS_I(inode)->last_trans = btrfs_inode_transid(leaf, inode_item); |
| 3367 | |
| 3368 | /* |
| 3369 | * If we were modified in the current generation and evicted from memory |
| 3370 | * and then re-read we need to do a full sync since we don't have any |
| 3371 | * idea about which extents were modified before we were evicted from |
| 3372 | * cache. |
| 3373 | */ |
| 3374 | if (BTRFS_I(inode)->last_trans == root->fs_info->generation) |
| 3375 | set_bit(BTRFS_INODE_NEEDS_FULL_SYNC, |
| 3376 | &BTRFS_I(inode)->runtime_flags); |
| 3377 | |
Josef Bacik | 0c4d2d9 | 2012-04-05 15:03:02 -0400 | [diff] [blame] | 3378 | inode->i_version = btrfs_inode_sequence(leaf, inode_item); |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 3379 | inode->i_generation = BTRFS_I(inode)->generation; |
Josef Bacik | 618e21d | 2007-07-11 10:18:17 -0400 | [diff] [blame] | 3380 | inode->i_rdev = 0; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 3381 | rdev = btrfs_inode_rdev(leaf, inode_item); |
| 3382 | |
Josef Bacik | aec7477 | 2008-07-24 12:12:38 -0400 | [diff] [blame] | 3383 | BTRFS_I(inode)->index_cnt = (u64)-1; |
Yan Zheng | d2fb343 | 2008-12-11 16:30:39 -0500 | [diff] [blame] | 3384 | BTRFS_I(inode)->flags = btrfs_inode_flags(leaf, inode_item); |
Miao Xie | 2f7e33d | 2011-06-23 07:27:13 +0000 | [diff] [blame] | 3385 | cache_acl: |
Chris Mason | 46a53cc | 2009-04-27 11:47:50 -0400 | [diff] [blame] | 3386 | /* |
| 3387 | * try to precache a NULL acl entry for files that don't have |
| 3388 | * any xattrs or acls |
| 3389 | */ |
Li Zefan | 33345d01 | 2011-04-20 10:31:50 +0800 | [diff] [blame] | 3390 | maybe_acls = acls_after_inode_item(leaf, path->slots[0], |
| 3391 | btrfs_ino(inode)); |
Al Viro | 72c0490 | 2009-06-24 16:58:48 -0400 | [diff] [blame] | 3392 | if (!maybe_acls) |
| 3393 | cache_no_acl(inode); |
Chris Mason | 46a53cc | 2009-04-27 11:47:50 -0400 | [diff] [blame] | 3394 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3395 | btrfs_free_path(path); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3396 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3397 | switch (inode->i_mode & S_IFMT) { |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3398 | case S_IFREG: |
| 3399 | inode->i_mapping->a_ops = &btrfs_aops; |
Chris Mason | 0416008 | 2008-03-26 10:28:07 -0400 | [diff] [blame] | 3400 | inode->i_mapping->backing_dev_info = &root->fs_info->bdi; |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 3401 | BTRFS_I(inode)->io_tree.ops = &btrfs_extent_io_ops; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3402 | inode->i_fop = &btrfs_file_operations; |
| 3403 | inode->i_op = &btrfs_file_inode_operations; |
| 3404 | break; |
| 3405 | case S_IFDIR: |
| 3406 | inode->i_fop = &btrfs_dir_file_operations; |
| 3407 | if (root == root->fs_info->tree_root) |
| 3408 | inode->i_op = &btrfs_dir_ro_inode_operations; |
| 3409 | else |
| 3410 | inode->i_op = &btrfs_dir_inode_operations; |
| 3411 | break; |
| 3412 | case S_IFLNK: |
| 3413 | inode->i_op = &btrfs_symlink_inode_operations; |
| 3414 | inode->i_mapping->a_ops = &btrfs_symlink_aops; |
Chris Mason | 0416008 | 2008-03-26 10:28:07 -0400 | [diff] [blame] | 3415 | inode->i_mapping->backing_dev_info = &root->fs_info->bdi; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3416 | break; |
Josef Bacik | 618e21d | 2007-07-11 10:18:17 -0400 | [diff] [blame] | 3417 | default: |
Jim Owens | 0279b4c | 2009-02-04 09:29:13 -0500 | [diff] [blame] | 3418 | inode->i_op = &btrfs_special_inode_operations; |
Josef Bacik | 618e21d | 2007-07-11 10:18:17 -0400 | [diff] [blame] | 3419 | init_special_inode(inode, inode->i_mode, rdev); |
| 3420 | break; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3421 | } |
Christoph Hellwig | 6cbff00 | 2009-04-17 10:37:41 +0200 | [diff] [blame] | 3422 | |
| 3423 | btrfs_update_iflags(inode); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3424 | return; |
| 3425 | |
| 3426 | make_bad: |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3427 | btrfs_free_path(path); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3428 | make_bad_inode(inode); |
| 3429 | } |
| 3430 | |
Chris Mason | d352ac6 | 2008-09-29 15:18:18 -0400 | [diff] [blame] | 3431 | /* |
| 3432 | * given a leaf and an inode, copy the inode fields into the leaf |
| 3433 | */ |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 3434 | static void fill_inode_item(struct btrfs_trans_handle *trans, |
| 3435 | struct extent_buffer *leaf, |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 3436 | struct btrfs_inode_item *item, |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3437 | struct inode *inode) |
| 3438 | { |
Liu Bo | 51fab69 | 2012-12-27 09:01:21 +0000 | [diff] [blame] | 3439 | struct btrfs_map_token token; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 3440 | |
Liu Bo | 51fab69 | 2012-12-27 09:01:21 +0000 | [diff] [blame] | 3441 | btrfs_init_map_token(&token); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 3442 | |
Liu Bo | 51fab69 | 2012-12-27 09:01:21 +0000 | [diff] [blame] | 3443 | btrfs_set_token_inode_uid(leaf, item, i_uid_read(inode), &token); |
| 3444 | btrfs_set_token_inode_gid(leaf, item, i_gid_read(inode), &token); |
| 3445 | btrfs_set_token_inode_size(leaf, item, BTRFS_I(inode)->disk_i_size, |
| 3446 | &token); |
| 3447 | btrfs_set_token_inode_mode(leaf, item, inode->i_mode, &token); |
| 3448 | btrfs_set_token_inode_nlink(leaf, item, inode->i_nlink, &token); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 3449 | |
Liu Bo | 51fab69 | 2012-12-27 09:01:21 +0000 | [diff] [blame] | 3450 | btrfs_set_token_timespec_sec(leaf, btrfs_inode_atime(item), |
| 3451 | inode->i_atime.tv_sec, &token); |
| 3452 | btrfs_set_token_timespec_nsec(leaf, btrfs_inode_atime(item), |
| 3453 | inode->i_atime.tv_nsec, &token); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 3454 | |
Liu Bo | 51fab69 | 2012-12-27 09:01:21 +0000 | [diff] [blame] | 3455 | btrfs_set_token_timespec_sec(leaf, btrfs_inode_mtime(item), |
| 3456 | inode->i_mtime.tv_sec, &token); |
| 3457 | btrfs_set_token_timespec_nsec(leaf, btrfs_inode_mtime(item), |
| 3458 | inode->i_mtime.tv_nsec, &token); |
| 3459 | |
| 3460 | btrfs_set_token_timespec_sec(leaf, btrfs_inode_ctime(item), |
| 3461 | inode->i_ctime.tv_sec, &token); |
| 3462 | btrfs_set_token_timespec_nsec(leaf, btrfs_inode_ctime(item), |
| 3463 | inode->i_ctime.tv_nsec, &token); |
| 3464 | |
| 3465 | btrfs_set_token_inode_nbytes(leaf, item, inode_get_bytes(inode), |
| 3466 | &token); |
| 3467 | btrfs_set_token_inode_generation(leaf, item, BTRFS_I(inode)->generation, |
| 3468 | &token); |
| 3469 | btrfs_set_token_inode_sequence(leaf, item, inode->i_version, &token); |
| 3470 | btrfs_set_token_inode_transid(leaf, item, trans->transid, &token); |
| 3471 | btrfs_set_token_inode_rdev(leaf, item, inode->i_rdev, &token); |
| 3472 | btrfs_set_token_inode_flags(leaf, item, BTRFS_I(inode)->flags, &token); |
| 3473 | btrfs_set_token_inode_block_group(leaf, item, 0, &token); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3474 | } |
| 3475 | |
Chris Mason | d352ac6 | 2008-09-29 15:18:18 -0400 | [diff] [blame] | 3476 | /* |
| 3477 | * copy everything in the in-memory inode into the btree. |
| 3478 | */ |
Chris Mason | 2115133 | 2011-11-10 20:39:08 -0500 | [diff] [blame] | 3479 | static noinline int btrfs_update_inode_item(struct btrfs_trans_handle *trans, |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 3480 | struct btrfs_root *root, struct inode *inode) |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3481 | { |
| 3482 | struct btrfs_inode_item *inode_item; |
| 3483 | struct btrfs_path *path; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 3484 | struct extent_buffer *leaf; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3485 | int ret; |
| 3486 | |
| 3487 | path = btrfs_alloc_path(); |
Miao Xie | 16cdcec | 2011-04-22 18:12:22 +0800 | [diff] [blame] | 3488 | if (!path) |
| 3489 | return -ENOMEM; |
| 3490 | |
Chris Mason | b947343 | 2009-03-13 11:00:37 -0400 | [diff] [blame] | 3491 | path->leave_spinning = 1; |
Miao Xie | 16cdcec | 2011-04-22 18:12:22 +0800 | [diff] [blame] | 3492 | ret = btrfs_lookup_inode(trans, root, path, &BTRFS_I(inode)->location, |
| 3493 | 1); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3494 | if (ret) { |
| 3495 | if (ret > 0) |
| 3496 | ret = -ENOENT; |
| 3497 | goto failed; |
| 3498 | } |
| 3499 | |
Chris Mason | b4ce94d | 2009-02-04 09:25:08 -0500 | [diff] [blame] | 3500 | btrfs_unlock_up_safe(path, 1); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 3501 | leaf = path->nodes[0]; |
| 3502 | inode_item = btrfs_item_ptr(leaf, path->slots[0], |
Miao Xie | 16cdcec | 2011-04-22 18:12:22 +0800 | [diff] [blame] | 3503 | struct btrfs_inode_item); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3504 | |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 3505 | fill_inode_item(trans, leaf, inode_item, inode); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 3506 | btrfs_mark_buffer_dirty(leaf); |
Josef Bacik | 15ee9bc | 2007-08-10 16:22:09 -0400 | [diff] [blame] | 3507 | btrfs_set_inode_last_trans(trans, inode); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3508 | ret = 0; |
| 3509 | failed: |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3510 | btrfs_free_path(path); |
| 3511 | return ret; |
| 3512 | } |
| 3513 | |
Chris Mason | d352ac6 | 2008-09-29 15:18:18 -0400 | [diff] [blame] | 3514 | /* |
Chris Mason | 2115133 | 2011-11-10 20:39:08 -0500 | [diff] [blame] | 3515 | * copy everything in the in-memory inode into the btree. |
| 3516 | */ |
| 3517 | noinline int btrfs_update_inode(struct btrfs_trans_handle *trans, |
| 3518 | struct btrfs_root *root, struct inode *inode) |
| 3519 | { |
| 3520 | int ret; |
| 3521 | |
| 3522 | /* |
| 3523 | * If the inode is a free space inode, we can deadlock during commit |
| 3524 | * if we put it into the delayed code. |
| 3525 | * |
| 3526 | * The data relocation inode should also be directly updated |
| 3527 | * without delay |
| 3528 | */ |
Liu Bo | 83eea1f | 2012-07-10 05:28:39 -0600 | [diff] [blame] | 3529 | if (!btrfs_is_free_space_inode(inode) |
Chris Mason | 2115133 | 2011-11-10 20:39:08 -0500 | [diff] [blame] | 3530 | && root->root_key.objectid != BTRFS_DATA_RELOC_TREE_OBJECTID) { |
Alexander Block | 8ea05e3 | 2012-07-25 17:35:53 +0200 | [diff] [blame] | 3531 | btrfs_update_root_times(trans, root); |
| 3532 | |
Chris Mason | 2115133 | 2011-11-10 20:39:08 -0500 | [diff] [blame] | 3533 | ret = btrfs_delayed_update_inode(trans, root, inode); |
| 3534 | if (!ret) |
| 3535 | btrfs_set_inode_last_trans(trans, inode); |
| 3536 | return ret; |
| 3537 | } |
| 3538 | |
| 3539 | return btrfs_update_inode_item(trans, root, inode); |
| 3540 | } |
| 3541 | |
Josef Bacik | be6aef6 | 2012-10-22 15:43:12 -0400 | [diff] [blame] | 3542 | noinline int btrfs_update_inode_fallback(struct btrfs_trans_handle *trans, |
| 3543 | struct btrfs_root *root, |
| 3544 | struct inode *inode) |
Chris Mason | 2115133 | 2011-11-10 20:39:08 -0500 | [diff] [blame] | 3545 | { |
| 3546 | int ret; |
| 3547 | |
| 3548 | ret = btrfs_update_inode(trans, root, inode); |
| 3549 | if (ret == -ENOSPC) |
| 3550 | return btrfs_update_inode_item(trans, root, inode); |
| 3551 | return ret; |
| 3552 | } |
| 3553 | |
| 3554 | /* |
Chris Mason | d352ac6 | 2008-09-29 15:18:18 -0400 | [diff] [blame] | 3555 | * unlink helper that gets used here in inode.c and in the tree logging |
| 3556 | * recovery code. It remove a link in a directory with a given name, and |
| 3557 | * also drops the back refs in the inode to the directory |
| 3558 | */ |
Al Viro | 9298679 | 2011-03-04 17:14:37 +0000 | [diff] [blame] | 3559 | static int __btrfs_unlink_inode(struct btrfs_trans_handle *trans, |
| 3560 | struct btrfs_root *root, |
| 3561 | struct inode *dir, struct inode *inode, |
| 3562 | const char *name, int name_len) |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3563 | { |
| 3564 | struct btrfs_path *path; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3565 | int ret = 0; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 3566 | struct extent_buffer *leaf; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3567 | struct btrfs_dir_item *di; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 3568 | struct btrfs_key key; |
Josef Bacik | aec7477 | 2008-07-24 12:12:38 -0400 | [diff] [blame] | 3569 | u64 index; |
Li Zefan | 33345d01 | 2011-04-20 10:31:50 +0800 | [diff] [blame] | 3570 | u64 ino = btrfs_ino(inode); |
| 3571 | u64 dir_ino = btrfs_ino(dir); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3572 | |
| 3573 | path = btrfs_alloc_path(); |
Chris Mason | 54aa1f4 | 2007-06-22 14:16:25 -0400 | [diff] [blame] | 3574 | if (!path) { |
| 3575 | ret = -ENOMEM; |
Tsutomu Itoh | 554233a | 2011-02-03 03:16:25 +0000 | [diff] [blame] | 3576 | goto out; |
Chris Mason | 54aa1f4 | 2007-06-22 14:16:25 -0400 | [diff] [blame] | 3577 | } |
| 3578 | |
Chris Mason | b947343 | 2009-03-13 11:00:37 -0400 | [diff] [blame] | 3579 | path->leave_spinning = 1; |
Li Zefan | 33345d01 | 2011-04-20 10:31:50 +0800 | [diff] [blame] | 3580 | di = btrfs_lookup_dir_item(trans, root, path, dir_ino, |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3581 | name, name_len, -1); |
| 3582 | if (IS_ERR(di)) { |
| 3583 | ret = PTR_ERR(di); |
| 3584 | goto err; |
| 3585 | } |
| 3586 | if (!di) { |
| 3587 | ret = -ENOENT; |
| 3588 | goto err; |
| 3589 | } |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 3590 | leaf = path->nodes[0]; |
| 3591 | btrfs_dir_item_key_to_cpu(leaf, di, &key); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3592 | ret = btrfs_delete_one_dir_name(trans, root, path, di); |
Chris Mason | 54aa1f4 | 2007-06-22 14:16:25 -0400 | [diff] [blame] | 3593 | if (ret) |
| 3594 | goto err; |
David Sterba | b3b4aa7 | 2011-04-21 01:20:15 +0200 | [diff] [blame] | 3595 | btrfs_release_path(path); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3596 | |
Li Zefan | 33345d01 | 2011-04-20 10:31:50 +0800 | [diff] [blame] | 3597 | ret = btrfs_del_inode_ref(trans, root, name, name_len, ino, |
| 3598 | dir_ino, &index); |
Josef Bacik | aec7477 | 2008-07-24 12:12:38 -0400 | [diff] [blame] | 3599 | if (ret) { |
Simon Kirby | c2cf52e | 2013-03-19 22:41:23 +0000 | [diff] [blame] | 3600 | btrfs_info(root->fs_info, |
| 3601 | "failed to delete reference to %.*s, inode %llu parent %llu", |
Geert Uytterhoeven | c1c9ff7 | 2013-08-20 13:20:07 +0200 | [diff] [blame] | 3602 | name_len, name, ino, dir_ino); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 3603 | btrfs_abort_transaction(trans, root, ret); |
Josef Bacik | aec7477 | 2008-07-24 12:12:38 -0400 | [diff] [blame] | 3604 | goto err; |
| 3605 | } |
| 3606 | |
Miao Xie | 16cdcec | 2011-04-22 18:12:22 +0800 | [diff] [blame] | 3607 | ret = btrfs_delete_delayed_dir_index(trans, root, dir, index); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 3608 | if (ret) { |
| 3609 | btrfs_abort_transaction(trans, root, ret); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3610 | goto err; |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 3611 | } |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3612 | |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 3613 | ret = btrfs_del_inode_ref_in_log(trans, root, name, name_len, |
Li Zefan | 33345d01 | 2011-04-20 10:31:50 +0800 | [diff] [blame] | 3614 | inode, dir_ino); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 3615 | if (ret != 0 && ret != -ENOENT) { |
| 3616 | btrfs_abort_transaction(trans, root, ret); |
| 3617 | goto err; |
| 3618 | } |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 3619 | |
| 3620 | ret = btrfs_del_dir_entries_in_log(trans, root, name, name_len, |
| 3621 | dir, index); |
Chris Mason | 6418c96 | 2010-10-30 07:34:24 -0400 | [diff] [blame] | 3622 | if (ret == -ENOENT) |
| 3623 | ret = 0; |
Zach Brown | d4e3991 | 2013-04-02 21:02:16 +0000 | [diff] [blame] | 3624 | else if (ret) |
| 3625 | btrfs_abort_transaction(trans, root, ret); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3626 | err: |
| 3627 | btrfs_free_path(path); |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 3628 | if (ret) |
| 3629 | goto out; |
| 3630 | |
| 3631 | btrfs_i_size_write(dir, dir->i_size - name_len * 2); |
Josef Bacik | 0c4d2d9 | 2012-04-05 15:03:02 -0400 | [diff] [blame] | 3632 | inode_inc_iversion(inode); |
| 3633 | inode_inc_iversion(dir); |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 3634 | inode->i_ctime = dir->i_mtime = dir->i_ctime = CURRENT_TIME; |
Tsutomu Itoh | b995929 | 2012-06-25 21:25:22 -0600 | [diff] [blame] | 3635 | ret = btrfs_update_inode(trans, root, dir); |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 3636 | out: |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3637 | return ret; |
| 3638 | } |
| 3639 | |
Al Viro | 9298679 | 2011-03-04 17:14:37 +0000 | [diff] [blame] | 3640 | int btrfs_unlink_inode(struct btrfs_trans_handle *trans, |
| 3641 | struct btrfs_root *root, |
| 3642 | struct inode *dir, struct inode *inode, |
| 3643 | const char *name, int name_len) |
| 3644 | { |
| 3645 | int ret; |
| 3646 | ret = __btrfs_unlink_inode(trans, root, dir, inode, name, name_len); |
| 3647 | if (!ret) { |
| 3648 | btrfs_drop_nlink(inode); |
| 3649 | ret = btrfs_update_inode(trans, root, inode); |
| 3650 | } |
| 3651 | return ret; |
| 3652 | } |
Yan, Zheng | a22285a | 2010-05-16 10:48:46 -0400 | [diff] [blame] | 3653 | |
| 3654 | /* |
| 3655 | * helper to start transaction for unlink and rmdir. |
| 3656 | * |
Josef Bacik | d52be81 | 2013-05-29 14:54:47 -0400 | [diff] [blame] | 3657 | * unlink and rmdir are special in btrfs, they do not always free space, so |
| 3658 | * if we cannot make our reservations the normal way try and see if there is |
| 3659 | * plenty of slack room in the global reserve to migrate, otherwise we cannot |
| 3660 | * allow the unlink to occur. |
Yan, Zheng | a22285a | 2010-05-16 10:48:46 -0400 | [diff] [blame] | 3661 | */ |
Josef Bacik | d52be81 | 2013-05-29 14:54:47 -0400 | [diff] [blame] | 3662 | static struct btrfs_trans_handle *__unlink_start_trans(struct inode *dir) |
Yan, Zheng | a22285a | 2010-05-16 10:48:46 -0400 | [diff] [blame] | 3663 | { |
| 3664 | struct btrfs_trans_handle *trans; |
| 3665 | struct btrfs_root *root = BTRFS_I(dir)->root; |
Yan, Zheng | a22285a | 2010-05-16 10:48:46 -0400 | [diff] [blame] | 3666 | int ret; |
| 3667 | |
Josef Bacik | e70bea5 | 2011-10-11 14:18:24 -0400 | [diff] [blame] | 3668 | /* |
| 3669 | * 1 for the possible orphan item |
| 3670 | * 1 for the dir item |
| 3671 | * 1 for the dir index |
| 3672 | * 1 for the inode ref |
Josef Bacik | e70bea5 | 2011-10-11 14:18:24 -0400 | [diff] [blame] | 3673 | * 1 for the inode |
| 3674 | */ |
Josef Bacik | 6e137ed | 2013-03-26 15:26:55 -0400 | [diff] [blame] | 3675 | trans = btrfs_start_transaction(root, 5); |
Yan, Zheng | a22285a | 2010-05-16 10:48:46 -0400 | [diff] [blame] | 3676 | if (!IS_ERR(trans) || PTR_ERR(trans) != -ENOSPC) |
| 3677 | return trans; |
| 3678 | |
Josef Bacik | d52be81 | 2013-05-29 14:54:47 -0400 | [diff] [blame] | 3679 | if (PTR_ERR(trans) == -ENOSPC) { |
| 3680 | u64 num_bytes = btrfs_calc_trans_metadata_size(root, 5); |
Yan, Zheng | a22285a | 2010-05-16 10:48:46 -0400 | [diff] [blame] | 3681 | |
Josef Bacik | d52be81 | 2013-05-29 14:54:47 -0400 | [diff] [blame] | 3682 | trans = btrfs_start_transaction(root, 0); |
| 3683 | if (IS_ERR(trans)) |
| 3684 | return trans; |
| 3685 | ret = btrfs_cond_migrate_bytes(root->fs_info, |
| 3686 | &root->fs_info->trans_block_rsv, |
| 3687 | num_bytes, 5); |
| 3688 | if (ret) { |
| 3689 | btrfs_end_transaction(trans, root); |
| 3690 | return ERR_PTR(ret); |
Yan, Zheng | a22285a | 2010-05-16 10:48:46 -0400 | [diff] [blame] | 3691 | } |
Josef Bacik | 5a77d76 | 2011-11-01 14:32:23 -0400 | [diff] [blame] | 3692 | trans->block_rsv = &root->fs_info->trans_block_rsv; |
Josef Bacik | d52be81 | 2013-05-29 14:54:47 -0400 | [diff] [blame] | 3693 | trans->bytes_reserved = num_bytes; |
Yan, Zheng | a22285a | 2010-05-16 10:48:46 -0400 | [diff] [blame] | 3694 | } |
Josef Bacik | d52be81 | 2013-05-29 14:54:47 -0400 | [diff] [blame] | 3695 | return trans; |
Yan, Zheng | a22285a | 2010-05-16 10:48:46 -0400 | [diff] [blame] | 3696 | } |
| 3697 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3698 | static int btrfs_unlink(struct inode *dir, struct dentry *dentry) |
| 3699 | { |
Yan, Zheng | a22285a | 2010-05-16 10:48:46 -0400 | [diff] [blame] | 3700 | struct btrfs_root *root = BTRFS_I(dir)->root; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3701 | struct btrfs_trans_handle *trans; |
Josef Bacik | 7b12876 | 2008-07-24 12:17:14 -0400 | [diff] [blame] | 3702 | struct inode *inode = dentry->d_inode; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3703 | int ret; |
| 3704 | |
Josef Bacik | d52be81 | 2013-05-29 14:54:47 -0400 | [diff] [blame] | 3705 | trans = __unlink_start_trans(dir); |
Yan, Zheng | a22285a | 2010-05-16 10:48:46 -0400 | [diff] [blame] | 3706 | if (IS_ERR(trans)) |
Josef Bacik | 5df6a9f | 2009-11-10 21:23:48 -0500 | [diff] [blame] | 3707 | return PTR_ERR(trans); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 3708 | |
Chris Mason | 12fcfd2 | 2009-03-24 10:24:20 -0400 | [diff] [blame] | 3709 | btrfs_record_unlink_dir(trans, dir, dentry->d_inode, 0); |
| 3710 | |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 3711 | ret = btrfs_unlink_inode(trans, root, dir, dentry->d_inode, |
| 3712 | dentry->d_name.name, dentry->d_name.len); |
Tsutomu Itoh | b532402 | 2011-07-19 07:27:20 +0000 | [diff] [blame] | 3713 | if (ret) |
| 3714 | goto out; |
Josef Bacik | 7b12876 | 2008-07-24 12:17:14 -0400 | [diff] [blame] | 3715 | |
Yan, Zheng | a22285a | 2010-05-16 10:48:46 -0400 | [diff] [blame] | 3716 | if (inode->i_nlink == 0) { |
Josef Bacik | 7b12876 | 2008-07-24 12:17:14 -0400 | [diff] [blame] | 3717 | ret = btrfs_orphan_add(trans, inode); |
Tsutomu Itoh | b532402 | 2011-07-19 07:27:20 +0000 | [diff] [blame] | 3718 | if (ret) |
| 3719 | goto out; |
Yan, Zheng | a22285a | 2010-05-16 10:48:46 -0400 | [diff] [blame] | 3720 | } |
Josef Bacik | 7b12876 | 2008-07-24 12:17:14 -0400 | [diff] [blame] | 3721 | |
Tsutomu Itoh | b532402 | 2011-07-19 07:27:20 +0000 | [diff] [blame] | 3722 | out: |
Josef Bacik | d52be81 | 2013-05-29 14:54:47 -0400 | [diff] [blame] | 3723 | btrfs_end_transaction(trans, root); |
Liu Bo | b53d3f5 | 2012-11-14 14:34:34 +0000 | [diff] [blame] | 3724 | btrfs_btree_balance_dirty(root); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3725 | return ret; |
| 3726 | } |
| 3727 | |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 3728 | int btrfs_unlink_subvol(struct btrfs_trans_handle *trans, |
| 3729 | struct btrfs_root *root, |
| 3730 | struct inode *dir, u64 objectid, |
| 3731 | const char *name, int name_len) |
| 3732 | { |
| 3733 | struct btrfs_path *path; |
| 3734 | struct extent_buffer *leaf; |
| 3735 | struct btrfs_dir_item *di; |
| 3736 | struct btrfs_key key; |
| 3737 | u64 index; |
| 3738 | int ret; |
Li Zefan | 33345d01 | 2011-04-20 10:31:50 +0800 | [diff] [blame] | 3739 | u64 dir_ino = btrfs_ino(dir); |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 3740 | |
| 3741 | path = btrfs_alloc_path(); |
| 3742 | if (!path) |
| 3743 | return -ENOMEM; |
| 3744 | |
Li Zefan | 33345d01 | 2011-04-20 10:31:50 +0800 | [diff] [blame] | 3745 | di = btrfs_lookup_dir_item(trans, root, path, dir_ino, |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 3746 | name, name_len, -1); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 3747 | if (IS_ERR_OR_NULL(di)) { |
| 3748 | if (!di) |
| 3749 | ret = -ENOENT; |
| 3750 | else |
| 3751 | ret = PTR_ERR(di); |
| 3752 | goto out; |
| 3753 | } |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 3754 | |
| 3755 | leaf = path->nodes[0]; |
| 3756 | btrfs_dir_item_key_to_cpu(leaf, di, &key); |
| 3757 | WARN_ON(key.type != BTRFS_ROOT_ITEM_KEY || key.objectid != objectid); |
| 3758 | ret = btrfs_delete_one_dir_name(trans, root, path, di); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 3759 | if (ret) { |
| 3760 | btrfs_abort_transaction(trans, root, ret); |
| 3761 | goto out; |
| 3762 | } |
David Sterba | b3b4aa7 | 2011-04-21 01:20:15 +0200 | [diff] [blame] | 3763 | btrfs_release_path(path); |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 3764 | |
| 3765 | ret = btrfs_del_root_ref(trans, root->fs_info->tree_root, |
| 3766 | objectid, root->root_key.objectid, |
Li Zefan | 33345d01 | 2011-04-20 10:31:50 +0800 | [diff] [blame] | 3767 | dir_ino, &index, name, name_len); |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 3768 | if (ret < 0) { |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 3769 | if (ret != -ENOENT) { |
| 3770 | btrfs_abort_transaction(trans, root, ret); |
| 3771 | goto out; |
| 3772 | } |
Li Zefan | 33345d01 | 2011-04-20 10:31:50 +0800 | [diff] [blame] | 3773 | di = btrfs_search_dir_index_item(root, path, dir_ino, |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 3774 | name, name_len); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 3775 | if (IS_ERR_OR_NULL(di)) { |
| 3776 | if (!di) |
| 3777 | ret = -ENOENT; |
| 3778 | else |
| 3779 | ret = PTR_ERR(di); |
| 3780 | btrfs_abort_transaction(trans, root, ret); |
| 3781 | goto out; |
| 3782 | } |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 3783 | |
| 3784 | leaf = path->nodes[0]; |
| 3785 | btrfs_item_key_to_cpu(leaf, &key, path->slots[0]); |
David Sterba | b3b4aa7 | 2011-04-21 01:20:15 +0200 | [diff] [blame] | 3786 | btrfs_release_path(path); |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 3787 | index = key.offset; |
| 3788 | } |
David Sterba | b3b4aa7 | 2011-04-21 01:20:15 +0200 | [diff] [blame] | 3789 | btrfs_release_path(path); |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 3790 | |
Miao Xie | 16cdcec | 2011-04-22 18:12:22 +0800 | [diff] [blame] | 3791 | ret = btrfs_delete_delayed_dir_index(trans, root, dir, index); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 3792 | if (ret) { |
| 3793 | btrfs_abort_transaction(trans, root, ret); |
| 3794 | goto out; |
| 3795 | } |
Miao Xie | 16cdcec | 2011-04-22 18:12:22 +0800 | [diff] [blame] | 3796 | |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 3797 | btrfs_i_size_write(dir, dir->i_size - name_len * 2); |
Josef Bacik | 0c4d2d9 | 2012-04-05 15:03:02 -0400 | [diff] [blame] | 3798 | inode_inc_iversion(dir); |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 3799 | dir->i_mtime = dir->i_ctime = CURRENT_TIME; |
Josef Bacik | 5a24e84 | 2012-08-08 10:12:59 -0600 | [diff] [blame] | 3800 | ret = btrfs_update_inode_fallback(trans, root, dir); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 3801 | if (ret) |
| 3802 | btrfs_abort_transaction(trans, root, ret); |
| 3803 | out: |
Josef Bacik | 71d7aed | 2011-06-14 14:24:32 -0400 | [diff] [blame] | 3804 | btrfs_free_path(path); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 3805 | return ret; |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 3806 | } |
| 3807 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3808 | static int btrfs_rmdir(struct inode *dir, struct dentry *dentry) |
| 3809 | { |
| 3810 | struct inode *inode = dentry->d_inode; |
Chris Mason | 1832a6d | 2007-12-21 16:27:21 -0500 | [diff] [blame] | 3811 | int err = 0; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3812 | struct btrfs_root *root = BTRFS_I(dir)->root; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3813 | struct btrfs_trans_handle *trans; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3814 | |
David Sterba | b3ae244 | 2012-09-13 16:04:34 -0600 | [diff] [blame] | 3815 | if (inode->i_size > BTRFS_EMPTY_DIR_SIZE) |
Yan | 134d451 | 2007-10-25 15:49:25 -0400 | [diff] [blame] | 3816 | return -ENOTEMPTY; |
David Sterba | b3ae244 | 2012-09-13 16:04:34 -0600 | [diff] [blame] | 3817 | if (btrfs_ino(inode) == BTRFS_FIRST_FREE_OBJECTID) |
| 3818 | return -EPERM; |
Yan | 134d451 | 2007-10-25 15:49:25 -0400 | [diff] [blame] | 3819 | |
Josef Bacik | d52be81 | 2013-05-29 14:54:47 -0400 | [diff] [blame] | 3820 | trans = __unlink_start_trans(dir); |
Yan, Zheng | a22285a | 2010-05-16 10:48:46 -0400 | [diff] [blame] | 3821 | if (IS_ERR(trans)) |
Josef Bacik | 5df6a9f | 2009-11-10 21:23:48 -0500 | [diff] [blame] | 3822 | return PTR_ERR(trans); |
Josef Bacik | 5df6a9f | 2009-11-10 21:23:48 -0500 | [diff] [blame] | 3823 | |
Li Zefan | 33345d01 | 2011-04-20 10:31:50 +0800 | [diff] [blame] | 3824 | if (unlikely(btrfs_ino(inode) == BTRFS_EMPTY_SUBVOL_DIR_OBJECTID)) { |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 3825 | err = btrfs_unlink_subvol(trans, root, dir, |
| 3826 | BTRFS_I(inode)->location.objectid, |
| 3827 | dentry->d_name.name, |
| 3828 | dentry->d_name.len); |
| 3829 | goto out; |
| 3830 | } |
| 3831 | |
Josef Bacik | 7b12876 | 2008-07-24 12:17:14 -0400 | [diff] [blame] | 3832 | err = btrfs_orphan_add(trans, inode); |
| 3833 | if (err) |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 3834 | goto out; |
Josef Bacik | 7b12876 | 2008-07-24 12:17:14 -0400 | [diff] [blame] | 3835 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3836 | /* now the directory is empty */ |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 3837 | err = btrfs_unlink_inode(trans, root, dir, dentry->d_inode, |
| 3838 | dentry->d_name.name, dentry->d_name.len); |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 3839 | if (!err) |
Chris Mason | dbe674a | 2008-07-17 12:54:05 -0400 | [diff] [blame] | 3840 | btrfs_i_size_write(inode, 0); |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 3841 | out: |
Josef Bacik | d52be81 | 2013-05-29 14:54:47 -0400 | [diff] [blame] | 3842 | btrfs_end_transaction(trans, root); |
Liu Bo | b53d3f5 | 2012-11-14 14:34:34 +0000 | [diff] [blame] | 3843 | btrfs_btree_balance_dirty(root); |
Chris Mason | 3954401 | 2007-12-12 14:38:19 -0500 | [diff] [blame] | 3844 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3845 | return err; |
| 3846 | } |
| 3847 | |
Chris Mason | 323ac95 | 2008-10-01 19:05:46 -0400 | [diff] [blame] | 3848 | /* |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3849 | * this can truncate away extent items, csum items and directory items. |
| 3850 | * 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] | 3851 | * any higher than new_size |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3852 | * |
| 3853 | * csum items that cross the new i_size are truncated to the new size |
| 3854 | * as well. |
Josef Bacik | 7b12876 | 2008-07-24 12:17:14 -0400 | [diff] [blame] | 3855 | * |
| 3856 | * min_type is the minimum key type to truncate down to. If set to 0, this |
| 3857 | * 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] | 3858 | */ |
Yan, Zheng | 8082510 | 2009-11-12 09:35:36 +0000 | [diff] [blame] | 3859 | int btrfs_truncate_inode_items(struct btrfs_trans_handle *trans, |
| 3860 | struct btrfs_root *root, |
| 3861 | struct inode *inode, |
| 3862 | u64 new_size, u32 min_type) |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3863 | { |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3864 | struct btrfs_path *path; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 3865 | struct extent_buffer *leaf; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3866 | struct btrfs_file_extent_item *fi; |
Yan, Zheng | 8082510 | 2009-11-12 09:35:36 +0000 | [diff] [blame] | 3867 | struct btrfs_key key; |
| 3868 | struct btrfs_key found_key; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3869 | u64 extent_start = 0; |
Chris Mason | db94535 | 2007-10-15 16:15:53 -0400 | [diff] [blame] | 3870 | u64 extent_num_bytes = 0; |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 3871 | u64 extent_offset = 0; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3872 | u64 item_end = 0; |
Josef Bacik | 7f4f6e0 | 2013-08-29 16:43:28 -0400 | [diff] [blame] | 3873 | u64 last_size = (u64)-1; |
Yan, Zheng | 8082510 | 2009-11-12 09:35:36 +0000 | [diff] [blame] | 3874 | u32 found_type = (u8)-1; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3875 | int found_extent; |
| 3876 | int del_item; |
Chris Mason | 85e21ba | 2008-01-29 15:11:36 -0500 | [diff] [blame] | 3877 | int pending_del_nr = 0; |
| 3878 | int pending_del_slot = 0; |
Chris Mason | 179e29e | 2007-11-01 11:28:41 -0400 | [diff] [blame] | 3879 | int extent_type = -1; |
Yan, Zheng | 8082510 | 2009-11-12 09:35:36 +0000 | [diff] [blame] | 3880 | int ret; |
| 3881 | int err = 0; |
Li Zefan | 33345d01 | 2011-04-20 10:31:50 +0800 | [diff] [blame] | 3882 | u64 ino = btrfs_ino(inode); |
Yan, Zheng | 8082510 | 2009-11-12 09:35:36 +0000 | [diff] [blame] | 3883 | |
| 3884 | BUG_ON(new_size > 0 && min_type != BTRFS_EXTENT_DATA_KEY); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3885 | |
Mark Fasheh | 0eb0e19 | 2011-07-12 16:44:10 -0700 | [diff] [blame] | 3886 | path = btrfs_alloc_path(); |
| 3887 | if (!path) |
| 3888 | return -ENOMEM; |
| 3889 | path->reada = -1; |
| 3890 | |
Josef Bacik | 5dc562c | 2012-08-17 13:14:17 -0400 | [diff] [blame] | 3891 | /* |
| 3892 | * We want to drop from the next block forward in case this new size is |
| 3893 | * not block aligned since we will be keeping the last block of the |
| 3894 | * extent just the way it is. |
| 3895 | */ |
Josef Bacik | 0af3d00 | 2010-06-21 14:48:16 -0400 | [diff] [blame] | 3896 | if (root->ref_cows || root == root->fs_info->tree_root) |
Qu Wenruo | fda2832 | 2013-02-26 08:10:22 +0000 | [diff] [blame] | 3897 | btrfs_drop_extent_cache(inode, ALIGN(new_size, |
| 3898 | root->sectorsize), (u64)-1, 0); |
Yan, Zheng | 8082510 | 2009-11-12 09:35:36 +0000 | [diff] [blame] | 3899 | |
Miao Xie | 16cdcec | 2011-04-22 18:12:22 +0800 | [diff] [blame] | 3900 | /* |
| 3901 | * This function is also used to drop the items in the log tree before |
| 3902 | * we relog the inode, so if root != BTRFS_I(inode)->root, it means |
| 3903 | * it is used to drop the loged items. So we shouldn't kill the delayed |
| 3904 | * items. |
| 3905 | */ |
| 3906 | if (min_type == 0 && root == BTRFS_I(inode)->root) |
| 3907 | btrfs_kill_delayed_inode_items(inode); |
| 3908 | |
Li Zefan | 33345d01 | 2011-04-20 10:31:50 +0800 | [diff] [blame] | 3909 | key.objectid = ino; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3910 | key.offset = (u64)-1; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 3911 | key.type = (u8)-1; |
| 3912 | |
Chris Mason | 85e21ba | 2008-01-29 15:11:36 -0500 | [diff] [blame] | 3913 | search_again: |
Chris Mason | b947343 | 2009-03-13 11:00:37 -0400 | [diff] [blame] | 3914 | path->leave_spinning = 1; |
Chris Mason | 85e21ba | 2008-01-29 15:11:36 -0500 | [diff] [blame] | 3915 | ret = btrfs_search_slot(trans, root, &key, path, -1, 1); |
Yan, Zheng | 8082510 | 2009-11-12 09:35:36 +0000 | [diff] [blame] | 3916 | if (ret < 0) { |
| 3917 | err = ret; |
| 3918 | goto out; |
| 3919 | } |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 3920 | |
Chris Mason | 85e21ba | 2008-01-29 15:11:36 -0500 | [diff] [blame] | 3921 | if (ret > 0) { |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 3922 | /* there are no items in the tree for us to truncate, we're |
| 3923 | * done |
| 3924 | */ |
Yan, Zheng | 8082510 | 2009-11-12 09:35:36 +0000 | [diff] [blame] | 3925 | if (path->slots[0] == 0) |
| 3926 | goto out; |
Chris Mason | 85e21ba | 2008-01-29 15:11:36 -0500 | [diff] [blame] | 3927 | path->slots[0]--; |
| 3928 | } |
| 3929 | |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 3930 | while (1) { |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3931 | fi = NULL; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 3932 | leaf = path->nodes[0]; |
| 3933 | btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]); |
| 3934 | found_type = btrfs_key_type(&found_key); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3935 | |
Li Zefan | 33345d01 | 2011-04-20 10:31:50 +0800 | [diff] [blame] | 3936 | if (found_key.objectid != ino) |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3937 | break; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 3938 | |
Chris Mason | 85e21ba | 2008-01-29 15:11:36 -0500 | [diff] [blame] | 3939 | if (found_type < min_type) |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3940 | break; |
| 3941 | |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 3942 | item_end = found_key.offset; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3943 | if (found_type == BTRFS_EXTENT_DATA_KEY) { |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 3944 | fi = btrfs_item_ptr(leaf, path->slots[0], |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3945 | struct btrfs_file_extent_item); |
Chris Mason | 179e29e | 2007-11-01 11:28:41 -0400 | [diff] [blame] | 3946 | extent_type = btrfs_file_extent_type(leaf, fi); |
| 3947 | if (extent_type != BTRFS_FILE_EXTENT_INLINE) { |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 3948 | item_end += |
Chris Mason | db94535 | 2007-10-15 16:15:53 -0400 | [diff] [blame] | 3949 | btrfs_file_extent_num_bytes(leaf, fi); |
Chris Mason | 179e29e | 2007-11-01 11:28:41 -0400 | [diff] [blame] | 3950 | } else if (extent_type == BTRFS_FILE_EXTENT_INLINE) { |
Chris Mason | 179e29e | 2007-11-01 11:28:41 -0400 | [diff] [blame] | 3951 | item_end += btrfs_file_extent_inline_len(leaf, |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 3952 | fi); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3953 | } |
Yan | 008630c | 2007-11-07 13:31:09 -0500 | [diff] [blame] | 3954 | item_end--; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3955 | } |
Yan, Zheng | 8082510 | 2009-11-12 09:35:36 +0000 | [diff] [blame] | 3956 | if (found_type > min_type) { |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3957 | del_item = 1; |
Yan, Zheng | 8082510 | 2009-11-12 09:35:36 +0000 | [diff] [blame] | 3958 | } else { |
| 3959 | if (item_end < new_size) |
| 3960 | break; |
| 3961 | if (found_key.offset >= new_size) |
| 3962 | del_item = 1; |
| 3963 | else |
| 3964 | del_item = 0; |
| 3965 | } |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3966 | found_extent = 0; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3967 | /* FIXME, shrink the extent if the ref count is only 1 */ |
Chris Mason | 179e29e | 2007-11-01 11:28:41 -0400 | [diff] [blame] | 3968 | if (found_type != BTRFS_EXTENT_DATA_KEY) |
| 3969 | goto delete; |
| 3970 | |
Josef Bacik | 7f4f6e0 | 2013-08-29 16:43:28 -0400 | [diff] [blame] | 3971 | if (del_item) |
| 3972 | last_size = found_key.offset; |
| 3973 | else |
| 3974 | last_size = new_size; |
| 3975 | |
Chris Mason | 179e29e | 2007-11-01 11:28:41 -0400 | [diff] [blame] | 3976 | if (extent_type != BTRFS_FILE_EXTENT_INLINE) { |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3977 | u64 num_dec; |
Chris Mason | db94535 | 2007-10-15 16:15:53 -0400 | [diff] [blame] | 3978 | extent_start = btrfs_file_extent_disk_bytenr(leaf, fi); |
Miao Xie | f70a9a6b | 2012-01-12 19:10:12 -0500 | [diff] [blame] | 3979 | if (!del_item) { |
Chris Mason | db94535 | 2007-10-15 16:15:53 -0400 | [diff] [blame] | 3980 | u64 orig_num_bytes = |
| 3981 | btrfs_file_extent_num_bytes(leaf, fi); |
Qu Wenruo | fda2832 | 2013-02-26 08:10:22 +0000 | [diff] [blame] | 3982 | extent_num_bytes = ALIGN(new_size - |
| 3983 | found_key.offset, |
| 3984 | root->sectorsize); |
Chris Mason | db94535 | 2007-10-15 16:15:53 -0400 | [diff] [blame] | 3985 | btrfs_set_file_extent_num_bytes(leaf, fi, |
| 3986 | extent_num_bytes); |
| 3987 | num_dec = (orig_num_bytes - |
Chris Mason | 9069218 | 2008-02-08 13:49:28 -0500 | [diff] [blame] | 3988 | extent_num_bytes); |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 3989 | if (root->ref_cows && extent_start != 0) |
Yan Zheng | a76a3cd | 2008-10-09 11:46:29 -0400 | [diff] [blame] | 3990 | inode_sub_bytes(inode, num_dec); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 3991 | btrfs_mark_buffer_dirty(leaf); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3992 | } else { |
Chris Mason | db94535 | 2007-10-15 16:15:53 -0400 | [diff] [blame] | 3993 | extent_num_bytes = |
| 3994 | btrfs_file_extent_disk_num_bytes(leaf, |
| 3995 | fi); |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 3996 | extent_offset = found_key.offset - |
| 3997 | btrfs_file_extent_offset(leaf, fi); |
| 3998 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3999 | /* FIXME blocksize != 4096 */ |
Chris Mason | 9069218 | 2008-02-08 13:49:28 -0500 | [diff] [blame] | 4000 | num_dec = btrfs_file_extent_num_bytes(leaf, fi); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4001 | if (extent_start != 0) { |
| 4002 | found_extent = 1; |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 4003 | if (root->ref_cows) |
Yan Zheng | a76a3cd | 2008-10-09 11:46:29 -0400 | [diff] [blame] | 4004 | inode_sub_bytes(inode, num_dec); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4005 | } |
| 4006 | } |
Chris Mason | 9069218 | 2008-02-08 13:49:28 -0500 | [diff] [blame] | 4007 | } else if (extent_type == BTRFS_FILE_EXTENT_INLINE) { |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 4008 | /* |
| 4009 | * we can't truncate inline items that have had |
| 4010 | * special encodings |
| 4011 | */ |
| 4012 | if (!del_item && |
| 4013 | btrfs_file_extent_compression(leaf, fi) == 0 && |
| 4014 | btrfs_file_extent_encryption(leaf, fi) == 0 && |
| 4015 | btrfs_file_extent_other_encoding(leaf, fi) == 0) { |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 4016 | u32 size = new_size - found_key.offset; |
| 4017 | |
| 4018 | if (root->ref_cows) { |
Yan Zheng | a76a3cd | 2008-10-09 11:46:29 -0400 | [diff] [blame] | 4019 | inode_sub_bytes(inode, item_end + 1 - |
| 4020 | new_size); |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 4021 | } |
| 4022 | size = |
| 4023 | btrfs_file_extent_calc_inline_size(size); |
Tsutomu Itoh | afe5fea | 2013-04-16 05:18:22 +0000 | [diff] [blame] | 4024 | btrfs_truncate_item(root, path, size, 1); |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 4025 | } else if (root->ref_cows) { |
Yan Zheng | a76a3cd | 2008-10-09 11:46:29 -0400 | [diff] [blame] | 4026 | inode_sub_bytes(inode, item_end + 1 - |
| 4027 | found_key.offset); |
Chris Mason | 9069218 | 2008-02-08 13:49:28 -0500 | [diff] [blame] | 4028 | } |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4029 | } |
Chris Mason | 179e29e | 2007-11-01 11:28:41 -0400 | [diff] [blame] | 4030 | delete: |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4031 | if (del_item) { |
Chris Mason | 85e21ba | 2008-01-29 15:11:36 -0500 | [diff] [blame] | 4032 | if (!pending_del_nr) { |
| 4033 | /* no pending yet, add ourselves */ |
| 4034 | pending_del_slot = path->slots[0]; |
| 4035 | pending_del_nr = 1; |
| 4036 | } else if (pending_del_nr && |
| 4037 | path->slots[0] + 1 == pending_del_slot) { |
| 4038 | /* hop on the pending chunk */ |
| 4039 | pending_del_nr++; |
| 4040 | pending_del_slot = path->slots[0]; |
| 4041 | } else { |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 4042 | BUG(); |
Chris Mason | 85e21ba | 2008-01-29 15:11:36 -0500 | [diff] [blame] | 4043 | } |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4044 | } else { |
| 4045 | break; |
| 4046 | } |
Josef Bacik | 0af3d00 | 2010-06-21 14:48:16 -0400 | [diff] [blame] | 4047 | if (found_extent && (root->ref_cows || |
| 4048 | root == root->fs_info->tree_root)) { |
Chris Mason | b947343 | 2009-03-13 11:00:37 -0400 | [diff] [blame] | 4049 | btrfs_set_path_blocking(path); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4050 | ret = btrfs_free_extent(trans, root, extent_start, |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 4051 | extent_num_bytes, 0, |
| 4052 | btrfs_header_owner(leaf), |
Arne Jansen | 66d7e7f | 2011-09-12 15:26:38 +0200 | [diff] [blame] | 4053 | ino, extent_offset, 0); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4054 | BUG_ON(ret); |
| 4055 | } |
Chris Mason | 85e21ba | 2008-01-29 15:11:36 -0500 | [diff] [blame] | 4056 | |
Yan, Zheng | 8082510 | 2009-11-12 09:35:36 +0000 | [diff] [blame] | 4057 | if (found_type == BTRFS_INODE_ITEM_KEY) |
| 4058 | break; |
| 4059 | |
| 4060 | if (path->slots[0] == 0 || |
| 4061 | path->slots[0] != pending_del_slot) { |
Yan, Zheng | 8082510 | 2009-11-12 09:35:36 +0000 | [diff] [blame] | 4062 | if (pending_del_nr) { |
| 4063 | ret = btrfs_del_items(trans, root, path, |
| 4064 | pending_del_slot, |
| 4065 | pending_del_nr); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 4066 | if (ret) { |
| 4067 | btrfs_abort_transaction(trans, |
| 4068 | root, ret); |
| 4069 | goto error; |
| 4070 | } |
Yan, Zheng | 8082510 | 2009-11-12 09:35:36 +0000 | [diff] [blame] | 4071 | pending_del_nr = 0; |
| 4072 | } |
David Sterba | b3b4aa7 | 2011-04-21 01:20:15 +0200 | [diff] [blame] | 4073 | btrfs_release_path(path); |
Chris Mason | 85e21ba | 2008-01-29 15:11:36 -0500 | [diff] [blame] | 4074 | goto search_again; |
Yan, Zheng | 8082510 | 2009-11-12 09:35:36 +0000 | [diff] [blame] | 4075 | } else { |
| 4076 | path->slots[0]--; |
Chris Mason | 85e21ba | 2008-01-29 15:11:36 -0500 | [diff] [blame] | 4077 | } |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4078 | } |
Yan, Zheng | 8082510 | 2009-11-12 09:35:36 +0000 | [diff] [blame] | 4079 | out: |
Chris Mason | 85e21ba | 2008-01-29 15:11:36 -0500 | [diff] [blame] | 4080 | if (pending_del_nr) { |
| 4081 | ret = btrfs_del_items(trans, root, path, pending_del_slot, |
| 4082 | pending_del_nr); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 4083 | if (ret) |
| 4084 | btrfs_abort_transaction(trans, root, ret); |
Chris Mason | 85e21ba | 2008-01-29 15:11:36 -0500 | [diff] [blame] | 4085 | } |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 4086 | error: |
Josef Bacik | 7f4f6e0 | 2013-08-29 16:43:28 -0400 | [diff] [blame] | 4087 | if (last_size != (u64)-1) |
| 4088 | btrfs_ordered_update_i_size(inode, last_size, NULL); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4089 | btrfs_free_path(path); |
Yan, Zheng | 8082510 | 2009-11-12 09:35:36 +0000 | [diff] [blame] | 4090 | return err; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4091 | } |
| 4092 | |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 4093 | /* |
Josef Bacik | 2aaa665 | 2012-08-29 14:27:18 -0400 | [diff] [blame] | 4094 | * btrfs_truncate_page - read, zero a chunk and write a page |
| 4095 | * @inode - inode that we're zeroing |
| 4096 | * @from - the offset to start zeroing |
| 4097 | * @len - the length to zero, 0 to zero the entire range respective to the |
| 4098 | * offset |
| 4099 | * @front - zero up to the offset instead of from the offset on |
| 4100 | * |
| 4101 | * This will find the page for the "from" offset and cow the page and zero the |
| 4102 | * 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] | 4103 | */ |
Josef Bacik | 2aaa665 | 2012-08-29 14:27:18 -0400 | [diff] [blame] | 4104 | int btrfs_truncate_page(struct inode *inode, loff_t from, loff_t len, |
| 4105 | int front) |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 4106 | { |
Josef Bacik | 2aaa665 | 2012-08-29 14:27:18 -0400 | [diff] [blame] | 4107 | struct address_space *mapping = inode->i_mapping; |
Chris Mason | db94535 | 2007-10-15 16:15:53 -0400 | [diff] [blame] | 4108 | struct btrfs_root *root = BTRFS_I(inode)->root; |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 4109 | struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree; |
| 4110 | struct btrfs_ordered_extent *ordered; |
Josef Bacik | 2ac55d4 | 2010-02-03 19:33:23 +0000 | [diff] [blame] | 4111 | struct extent_state *cached_state = NULL; |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 4112 | char *kaddr; |
Chris Mason | db94535 | 2007-10-15 16:15:53 -0400 | [diff] [blame] | 4113 | u32 blocksize = root->sectorsize; |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 4114 | pgoff_t index = from >> PAGE_CACHE_SHIFT; |
| 4115 | unsigned offset = from & (PAGE_CACHE_SIZE-1); |
| 4116 | struct page *page; |
Josef Bacik | 3b16a4e | 2011-09-21 15:05:58 -0400 | [diff] [blame] | 4117 | gfp_t mask = btrfs_alloc_write_mask(mapping); |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 4118 | int ret = 0; |
| 4119 | u64 page_start; |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 4120 | u64 page_end; |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 4121 | |
Josef Bacik | 2aaa665 | 2012-08-29 14:27:18 -0400 | [diff] [blame] | 4122 | if ((offset & (blocksize - 1)) == 0 && |
| 4123 | (!len || ((len & (blocksize - 1)) == 0))) |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 4124 | goto out; |
Yan, Zheng | 0ca1f7c | 2010-05-16 10:48:47 -0400 | [diff] [blame] | 4125 | ret = btrfs_delalloc_reserve_space(inode, PAGE_CACHE_SIZE); |
Josef Bacik | 5d5e103 | 2009-10-13 16:46:49 -0400 | [diff] [blame] | 4126 | if (ret) |
| 4127 | goto out; |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 4128 | |
Chris Mason | 211c17f | 2008-05-15 09:13:45 -0400 | [diff] [blame] | 4129 | again: |
Josef Bacik | 3b16a4e | 2011-09-21 15:05:58 -0400 | [diff] [blame] | 4130 | page = find_or_create_page(mapping, index, mask); |
Josef Bacik | 5d5e103 | 2009-10-13 16:46:49 -0400 | [diff] [blame] | 4131 | if (!page) { |
Yan, Zheng | 0ca1f7c | 2010-05-16 10:48:47 -0400 | [diff] [blame] | 4132 | btrfs_delalloc_release_space(inode, PAGE_CACHE_SIZE); |
Miao Xie | ac6a2b3 | 2012-12-05 10:56:13 +0000 | [diff] [blame] | 4133 | ret = -ENOMEM; |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 4134 | goto out; |
Josef Bacik | 5d5e103 | 2009-10-13 16:46:49 -0400 | [diff] [blame] | 4135 | } |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 4136 | |
| 4137 | page_start = page_offset(page); |
| 4138 | page_end = page_start + PAGE_CACHE_SIZE - 1; |
| 4139 | |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 4140 | if (!PageUptodate(page)) { |
| 4141 | ret = btrfs_readpage(NULL, page); |
| 4142 | lock_page(page); |
Chris Mason | 211c17f | 2008-05-15 09:13:45 -0400 | [diff] [blame] | 4143 | if (page->mapping != mapping) { |
| 4144 | unlock_page(page); |
| 4145 | page_cache_release(page); |
| 4146 | goto again; |
| 4147 | } |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 4148 | if (!PageUptodate(page)) { |
| 4149 | ret = -EIO; |
Chris Mason | 8964222 | 2008-07-24 09:41:53 -0400 | [diff] [blame] | 4150 | goto out_unlock; |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 4151 | } |
| 4152 | } |
Chris Mason | 211c17f | 2008-05-15 09:13:45 -0400 | [diff] [blame] | 4153 | wait_on_page_writeback(page); |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 4154 | |
Jeff Mahoney | d008237 | 2012-03-01 14:57:19 +0100 | [diff] [blame] | 4155 | lock_extent_bits(io_tree, page_start, page_end, 0, &cached_state); |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 4156 | set_page_extent_mapped(page); |
| 4157 | |
| 4158 | ordered = btrfs_lookup_ordered_extent(inode, page_start); |
| 4159 | if (ordered) { |
Josef Bacik | 2ac55d4 | 2010-02-03 19:33:23 +0000 | [diff] [blame] | 4160 | unlock_extent_cached(io_tree, page_start, page_end, |
| 4161 | &cached_state, GFP_NOFS); |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 4162 | unlock_page(page); |
| 4163 | page_cache_release(page); |
Chris Mason | eb84ae0 | 2008-07-17 13:53:27 -0400 | [diff] [blame] | 4164 | btrfs_start_ordered_extent(inode, ordered, 1); |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 4165 | btrfs_put_ordered_extent(ordered); |
| 4166 | goto again; |
| 4167 | } |
| 4168 | |
Josef Bacik | 2ac55d4 | 2010-02-03 19:33:23 +0000 | [diff] [blame] | 4169 | clear_extent_bit(&BTRFS_I(inode)->io_tree, page_start, page_end, |
Liu Bo | 9e8a4a8 | 2012-09-05 19:10:51 -0600 | [diff] [blame] | 4170 | EXTENT_DIRTY | EXTENT_DELALLOC | |
| 4171 | EXTENT_DO_ACCOUNTING | EXTENT_DEFRAG, |
Josef Bacik | 2ac55d4 | 2010-02-03 19:33:23 +0000 | [diff] [blame] | 4172 | 0, 0, &cached_state, GFP_NOFS); |
Josef Bacik | 5d5e103 | 2009-10-13 16:46:49 -0400 | [diff] [blame] | 4173 | |
Josef Bacik | 2ac55d4 | 2010-02-03 19:33:23 +0000 | [diff] [blame] | 4174 | ret = btrfs_set_extent_delalloc(inode, page_start, page_end, |
| 4175 | &cached_state); |
Josef Bacik | 9ed74f2 | 2009-09-11 16:12:44 -0400 | [diff] [blame] | 4176 | if (ret) { |
Josef Bacik | 2ac55d4 | 2010-02-03 19:33:23 +0000 | [diff] [blame] | 4177 | unlock_extent_cached(io_tree, page_start, page_end, |
| 4178 | &cached_state, GFP_NOFS); |
Josef Bacik | 9ed74f2 | 2009-09-11 16:12:44 -0400 | [diff] [blame] | 4179 | goto out_unlock; |
| 4180 | } |
| 4181 | |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 4182 | if (offset != PAGE_CACHE_SIZE) { |
Josef Bacik | 2aaa665 | 2012-08-29 14:27:18 -0400 | [diff] [blame] | 4183 | if (!len) |
| 4184 | len = PAGE_CACHE_SIZE - offset; |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 4185 | kaddr = kmap(page); |
Josef Bacik | 2aaa665 | 2012-08-29 14:27:18 -0400 | [diff] [blame] | 4186 | if (front) |
| 4187 | memset(kaddr, 0, offset); |
| 4188 | else |
| 4189 | memset(kaddr + offset, 0, len); |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 4190 | flush_dcache_page(page); |
| 4191 | kunmap(page); |
| 4192 | } |
Chris Mason | 247e743 | 2008-07-17 12:53:51 -0400 | [diff] [blame] | 4193 | ClearPageChecked(page); |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 4194 | set_page_dirty(page); |
Josef Bacik | 2ac55d4 | 2010-02-03 19:33:23 +0000 | [diff] [blame] | 4195 | unlock_extent_cached(io_tree, page_start, page_end, &cached_state, |
| 4196 | GFP_NOFS); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4197 | |
Chris Mason | 8964222 | 2008-07-24 09:41:53 -0400 | [diff] [blame] | 4198 | out_unlock: |
Josef Bacik | 5d5e103 | 2009-10-13 16:46:49 -0400 | [diff] [blame] | 4199 | if (ret) |
Yan, Zheng | 0ca1f7c | 2010-05-16 10:48:47 -0400 | [diff] [blame] | 4200 | btrfs_delalloc_release_space(inode, PAGE_CACHE_SIZE); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4201 | unlock_page(page); |
| 4202 | page_cache_release(page); |
| 4203 | out: |
| 4204 | return ret; |
| 4205 | } |
| 4206 | |
Josef Bacik | 695a0d0 | 2011-03-04 15:46:53 -0500 | [diff] [blame] | 4207 | /* |
| 4208 | * This function puts in dummy file extents for the area we're creating a hole |
| 4209 | * for. So if we are truncating this file to a larger size we need to insert |
| 4210 | * these file extents so that btrfs_get_extent will return a EXTENT_MAP_HOLE for |
| 4211 | * the range between oldsize and size |
| 4212 | */ |
Josef Bacik | a41ad39 | 2011-01-31 15:30:16 -0500 | [diff] [blame] | 4213 | 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] | 4214 | { |
| 4215 | struct btrfs_trans_handle *trans; |
| 4216 | struct btrfs_root *root = BTRFS_I(inode)->root; |
| 4217 | struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree; |
Yan, Zheng | a22285a | 2010-05-16 10:48:46 -0400 | [diff] [blame] | 4218 | struct extent_map *em = NULL; |
Josef Bacik | 2ac55d4 | 2010-02-03 19:33:23 +0000 | [diff] [blame] | 4219 | struct extent_state *cached_state = NULL; |
Josef Bacik | 5dc562c | 2012-08-17 13:14:17 -0400 | [diff] [blame] | 4220 | struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree; |
Qu Wenruo | fda2832 | 2013-02-26 08:10:22 +0000 | [diff] [blame] | 4221 | u64 hole_start = ALIGN(oldsize, root->sectorsize); |
| 4222 | u64 block_end = ALIGN(size, root->sectorsize); |
Yan Zheng | 9036c10 | 2008-10-30 14:19:41 -0400 | [diff] [blame] | 4223 | u64 last_byte; |
| 4224 | u64 cur_offset; |
| 4225 | u64 hole_size; |
Josef Bacik | 9ed74f2 | 2009-09-11 16:12:44 -0400 | [diff] [blame] | 4226 | int err = 0; |
Yan Zheng | 9036c10 | 2008-10-30 14:19:41 -0400 | [diff] [blame] | 4227 | |
Josef Bacik | a71754f | 2013-06-17 17:14:39 -0400 | [diff] [blame] | 4228 | /* |
| 4229 | * If our size started in the middle of a page we need to zero out the |
| 4230 | * rest of the page before we expand the i_size, otherwise we could |
| 4231 | * expose stale data. |
| 4232 | */ |
| 4233 | err = btrfs_truncate_page(inode, oldsize, 0, 0); |
| 4234 | if (err) |
| 4235 | return err; |
| 4236 | |
Yan Zheng | 9036c10 | 2008-10-30 14:19:41 -0400 | [diff] [blame] | 4237 | if (size <= hole_start) |
| 4238 | return 0; |
| 4239 | |
Yan Zheng | 9036c10 | 2008-10-30 14:19:41 -0400 | [diff] [blame] | 4240 | while (1) { |
| 4241 | struct btrfs_ordered_extent *ordered; |
Miao Xie | fa7c149 | 2013-09-26 13:15:27 +0800 | [diff] [blame] | 4242 | |
Josef Bacik | 2ac55d4 | 2010-02-03 19:33:23 +0000 | [diff] [blame] | 4243 | lock_extent_bits(io_tree, hole_start, block_end - 1, 0, |
Jeff Mahoney | d008237 | 2012-03-01 14:57:19 +0100 | [diff] [blame] | 4244 | &cached_state); |
Miao Xie | fa7c149 | 2013-09-26 13:15:27 +0800 | [diff] [blame] | 4245 | ordered = btrfs_lookup_ordered_range(inode, hole_start, |
| 4246 | block_end - hole_start); |
Yan Zheng | 9036c10 | 2008-10-30 14:19:41 -0400 | [diff] [blame] | 4247 | if (!ordered) |
| 4248 | break; |
Josef Bacik | 2ac55d4 | 2010-02-03 19:33:23 +0000 | [diff] [blame] | 4249 | unlock_extent_cached(io_tree, hole_start, block_end - 1, |
| 4250 | &cached_state, GFP_NOFS); |
Miao Xie | fa7c149 | 2013-09-26 13:15:27 +0800 | [diff] [blame] | 4251 | btrfs_start_ordered_extent(inode, ordered, 1); |
Yan Zheng | 9036c10 | 2008-10-30 14:19:41 -0400 | [diff] [blame] | 4252 | btrfs_put_ordered_extent(ordered); |
| 4253 | } |
| 4254 | |
Yan Zheng | 9036c10 | 2008-10-30 14:19:41 -0400 | [diff] [blame] | 4255 | cur_offset = hole_start; |
| 4256 | while (1) { |
| 4257 | em = btrfs_get_extent(inode, NULL, 0, cur_offset, |
| 4258 | block_end - cur_offset, 0); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 4259 | if (IS_ERR(em)) { |
| 4260 | err = PTR_ERR(em); |
Zach Brown | f276795 | 2013-01-08 19:37:58 +0000 | [diff] [blame] | 4261 | em = NULL; |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 4262 | break; |
| 4263 | } |
Yan Zheng | 9036c10 | 2008-10-30 14:19:41 -0400 | [diff] [blame] | 4264 | last_byte = min(extent_map_end(em), block_end); |
Qu Wenruo | fda2832 | 2013-02-26 08:10:22 +0000 | [diff] [blame] | 4265 | last_byte = ALIGN(last_byte , root->sectorsize); |
Yan, Zheng | 8082510 | 2009-11-12 09:35:36 +0000 | [diff] [blame] | 4266 | if (!test_bit(EXTENT_FLAG_PREALLOC, &em->flags)) { |
Josef Bacik | 5dc562c | 2012-08-17 13:14:17 -0400 | [diff] [blame] | 4267 | struct extent_map *hole_em; |
Yan Zheng | 9036c10 | 2008-10-30 14:19:41 -0400 | [diff] [blame] | 4268 | hole_size = last_byte - cur_offset; |
Yan, Zheng | 8082510 | 2009-11-12 09:35:36 +0000 | [diff] [blame] | 4269 | |
Miao Xie | 3642320 | 2011-12-14 20:12:02 -0500 | [diff] [blame] | 4270 | trans = btrfs_start_transaction(root, 3); |
Yan, Zheng | a22285a | 2010-05-16 10:48:46 -0400 | [diff] [blame] | 4271 | if (IS_ERR(trans)) { |
| 4272 | err = PTR_ERR(trans); |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 4273 | break; |
Yan, Zheng | a22285a | 2010-05-16 10:48:46 -0400 | [diff] [blame] | 4274 | } |
Yan, Zheng | 8082510 | 2009-11-12 09:35:36 +0000 | [diff] [blame] | 4275 | |
Josef Bacik | 5dc562c | 2012-08-17 13:14:17 -0400 | [diff] [blame] | 4276 | err = btrfs_drop_extents(trans, root, inode, |
| 4277 | cur_offset, |
Josef Bacik | 2671485 | 2012-08-29 12:24:27 -0400 | [diff] [blame] | 4278 | cur_offset + hole_size, 1); |
Miao Xie | 5b39737 | 2011-09-11 10:52:24 -0400 | [diff] [blame] | 4279 | if (err) { |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 4280 | btrfs_abort_transaction(trans, root, err); |
Miao Xie | 5b39737 | 2011-09-11 10:52:24 -0400 | [diff] [blame] | 4281 | btrfs_end_transaction(trans, root); |
Josef Bacik | 3893e33 | 2011-01-31 16:03:11 -0500 | [diff] [blame] | 4282 | break; |
Miao Xie | 5b39737 | 2011-09-11 10:52:24 -0400 | [diff] [blame] | 4283 | } |
Josef Bacik | 9ed74f2 | 2009-09-11 16:12:44 -0400 | [diff] [blame] | 4284 | |
Yan Zheng | 9036c10 | 2008-10-30 14:19:41 -0400 | [diff] [blame] | 4285 | err = btrfs_insert_file_extent(trans, root, |
Li Zefan | 33345d01 | 2011-04-20 10:31:50 +0800 | [diff] [blame] | 4286 | btrfs_ino(inode), cur_offset, 0, |
Yan Zheng | 9036c10 | 2008-10-30 14:19:41 -0400 | [diff] [blame] | 4287 | 0, hole_size, 0, hole_size, |
| 4288 | 0, 0, 0); |
Miao Xie | 5b39737 | 2011-09-11 10:52:24 -0400 | [diff] [blame] | 4289 | if (err) { |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 4290 | btrfs_abort_transaction(trans, root, err); |
Miao Xie | 5b39737 | 2011-09-11 10:52:24 -0400 | [diff] [blame] | 4291 | btrfs_end_transaction(trans, root); |
Josef Bacik | 3893e33 | 2011-01-31 16:03:11 -0500 | [diff] [blame] | 4292 | break; |
Miao Xie | 5b39737 | 2011-09-11 10:52:24 -0400 | [diff] [blame] | 4293 | } |
Yan, Zheng | 8082510 | 2009-11-12 09:35:36 +0000 | [diff] [blame] | 4294 | |
Josef Bacik | 5dc562c | 2012-08-17 13:14:17 -0400 | [diff] [blame] | 4295 | btrfs_drop_extent_cache(inode, cur_offset, |
| 4296 | cur_offset + hole_size - 1, 0); |
| 4297 | hole_em = alloc_extent_map(); |
| 4298 | if (!hole_em) { |
| 4299 | set_bit(BTRFS_INODE_NEEDS_FULL_SYNC, |
| 4300 | &BTRFS_I(inode)->runtime_flags); |
| 4301 | goto next; |
| 4302 | } |
| 4303 | hole_em->start = cur_offset; |
| 4304 | hole_em->len = hole_size; |
| 4305 | hole_em->orig_start = cur_offset; |
Yan, Zheng | 8082510 | 2009-11-12 09:35:36 +0000 | [diff] [blame] | 4306 | |
Josef Bacik | 5dc562c | 2012-08-17 13:14:17 -0400 | [diff] [blame] | 4307 | hole_em->block_start = EXTENT_MAP_HOLE; |
| 4308 | hole_em->block_len = 0; |
Josef Bacik | b493968 | 2012-12-03 10:31:19 -0500 | [diff] [blame] | 4309 | hole_em->orig_block_len = 0; |
Josef Bacik | cc95bef | 2013-04-04 14:31:27 -0400 | [diff] [blame] | 4310 | hole_em->ram_bytes = hole_size; |
Josef Bacik | 5dc562c | 2012-08-17 13:14:17 -0400 | [diff] [blame] | 4311 | hole_em->bdev = root->fs_info->fs_devices->latest_bdev; |
| 4312 | hole_em->compress_type = BTRFS_COMPRESS_NONE; |
| 4313 | hole_em->generation = trans->transid; |
| 4314 | |
| 4315 | while (1) { |
| 4316 | write_lock(&em_tree->lock); |
Josef Bacik | 09a2a8f9 | 2013-04-05 16:51:15 -0400 | [diff] [blame] | 4317 | err = add_extent_mapping(em_tree, hole_em, 1); |
Josef Bacik | 5dc562c | 2012-08-17 13:14:17 -0400 | [diff] [blame] | 4318 | write_unlock(&em_tree->lock); |
| 4319 | if (err != -EEXIST) |
| 4320 | break; |
| 4321 | btrfs_drop_extent_cache(inode, cur_offset, |
| 4322 | cur_offset + |
| 4323 | hole_size - 1, 0); |
| 4324 | } |
| 4325 | free_extent_map(hole_em); |
| 4326 | next: |
Miao Xie | 3642320 | 2011-12-14 20:12:02 -0500 | [diff] [blame] | 4327 | btrfs_update_inode(trans, root, inode); |
Yan, Zheng | 8082510 | 2009-11-12 09:35:36 +0000 | [diff] [blame] | 4328 | btrfs_end_transaction(trans, root); |
Yan Zheng | 9036c10 | 2008-10-30 14:19:41 -0400 | [diff] [blame] | 4329 | } |
| 4330 | free_extent_map(em); |
Yan, Zheng | a22285a | 2010-05-16 10:48:46 -0400 | [diff] [blame] | 4331 | em = NULL; |
Yan Zheng | 9036c10 | 2008-10-30 14:19:41 -0400 | [diff] [blame] | 4332 | cur_offset = last_byte; |
Yan, Zheng | 8082510 | 2009-11-12 09:35:36 +0000 | [diff] [blame] | 4333 | if (cur_offset >= block_end) |
Yan Zheng | 9036c10 | 2008-10-30 14:19:41 -0400 | [diff] [blame] | 4334 | break; |
| 4335 | } |
| 4336 | |
Yan, Zheng | a22285a | 2010-05-16 10:48:46 -0400 | [diff] [blame] | 4337 | free_extent_map(em); |
Josef Bacik | 2ac55d4 | 2010-02-03 19:33:23 +0000 | [diff] [blame] | 4338 | unlock_extent_cached(io_tree, hole_start, block_end - 1, &cached_state, |
| 4339 | GFP_NOFS); |
Yan Zheng | 9036c10 | 2008-10-30 14:19:41 -0400 | [diff] [blame] | 4340 | return err; |
| 4341 | } |
| 4342 | |
Eric Sandeen | 3972f26 | 2013-01-12 02:57:22 +0000 | [diff] [blame] | 4343 | static int btrfs_setsize(struct inode *inode, struct iattr *attr) |
Yan, Zheng | 8082510 | 2009-11-12 09:35:36 +0000 | [diff] [blame] | 4344 | { |
Miao Xie | f4a2f4c | 2011-12-14 20:12:01 -0500 | [diff] [blame] | 4345 | struct btrfs_root *root = BTRFS_I(inode)->root; |
| 4346 | struct btrfs_trans_handle *trans; |
Josef Bacik | a41ad39 | 2011-01-31 15:30:16 -0500 | [diff] [blame] | 4347 | loff_t oldsize = i_size_read(inode); |
Eric Sandeen | 3972f26 | 2013-01-12 02:57:22 +0000 | [diff] [blame] | 4348 | loff_t newsize = attr->ia_size; |
| 4349 | int mask = attr->ia_valid; |
Yan, Zheng | 8082510 | 2009-11-12 09:35:36 +0000 | [diff] [blame] | 4350 | int ret; |
| 4351 | |
Eric Sandeen | 3972f26 | 2013-01-12 02:57:22 +0000 | [diff] [blame] | 4352 | /* |
| 4353 | * The regular truncate() case without ATTR_CTIME and ATTR_MTIME is a |
| 4354 | * special case where we need to update the times despite not having |
| 4355 | * these flags set. For all other operations the VFS set these flags |
| 4356 | * explicitly if it wants a timestamp update. |
| 4357 | */ |
| 4358 | if (newsize != oldsize && (!(mask & (ATTR_CTIME | ATTR_MTIME)))) |
| 4359 | inode->i_ctime = inode->i_mtime = current_fs_time(inode->i_sb); |
| 4360 | |
Josef Bacik | a41ad39 | 2011-01-31 15:30:16 -0500 | [diff] [blame] | 4361 | if (newsize > oldsize) { |
Kirill A. Shutemov | 7caef26 | 2013-09-12 15:13:56 -0700 | [diff] [blame] | 4362 | truncate_pagecache(inode, newsize); |
Josef Bacik | a41ad39 | 2011-01-31 15:30:16 -0500 | [diff] [blame] | 4363 | ret = btrfs_cont_expand(inode, oldsize, newsize); |
Miao Xie | f4a2f4c | 2011-12-14 20:12:01 -0500 | [diff] [blame] | 4364 | if (ret) |
Yan, Zheng | 8082510 | 2009-11-12 09:35:36 +0000 | [diff] [blame] | 4365 | return ret; |
Yan, Zheng | 8082510 | 2009-11-12 09:35:36 +0000 | [diff] [blame] | 4366 | |
Miao Xie | f4a2f4c | 2011-12-14 20:12:01 -0500 | [diff] [blame] | 4367 | trans = btrfs_start_transaction(root, 1); |
| 4368 | if (IS_ERR(trans)) |
| 4369 | return PTR_ERR(trans); |
| 4370 | |
| 4371 | i_size_write(inode, newsize); |
| 4372 | btrfs_ordered_update_i_size(inode, i_size_read(inode), NULL); |
| 4373 | ret = btrfs_update_inode(trans, root, inode); |
Josef Bacik | 7ad85bb | 2012-01-12 19:10:12 -0500 | [diff] [blame] | 4374 | btrfs_end_transaction(trans, root); |
Josef Bacik | a41ad39 | 2011-01-31 15:30:16 -0500 | [diff] [blame] | 4375 | } else { |
Yan, Zheng | 8082510 | 2009-11-12 09:35:36 +0000 | [diff] [blame] | 4376 | |
Josef Bacik | a41ad39 | 2011-01-31 15:30:16 -0500 | [diff] [blame] | 4377 | /* |
| 4378 | * We're truncating a file that used to have good data down to |
| 4379 | * zero. Make sure it gets into the ordered flush list so that |
| 4380 | * any new writes get down to disk quickly. |
| 4381 | */ |
| 4382 | if (newsize == 0) |
Josef Bacik | 72ac3c0 | 2012-05-23 14:13:11 -0400 | [diff] [blame] | 4383 | set_bit(BTRFS_INODE_ORDERED_DATA_CLOSE, |
| 4384 | &BTRFS_I(inode)->runtime_flags); |
Yan, Zheng | 8082510 | 2009-11-12 09:35:36 +0000 | [diff] [blame] | 4385 | |
Josef Bacik | f3fe820 | 2013-01-07 17:03:21 -0500 | [diff] [blame] | 4386 | /* |
| 4387 | * 1 for the orphan item we're going to add |
| 4388 | * 1 for the orphan item deletion. |
| 4389 | */ |
| 4390 | trans = btrfs_start_transaction(root, 2); |
| 4391 | if (IS_ERR(trans)) |
| 4392 | return PTR_ERR(trans); |
| 4393 | |
| 4394 | /* |
| 4395 | * We need to do this in case we fail at _any_ point during the |
| 4396 | * actual truncate. Once we do the truncate_setsize we could |
| 4397 | * invalidate pages which forces any outstanding ordered io to |
| 4398 | * be instantly completed which will give us extents that need |
| 4399 | * to be truncated. If we fail to get an orphan inode down we |
| 4400 | * could have left over extents that were never meant to live, |
| 4401 | * so we need to garuntee from this point on that everything |
| 4402 | * will be consistent. |
| 4403 | */ |
| 4404 | ret = btrfs_orphan_add(trans, inode); |
| 4405 | btrfs_end_transaction(trans, root); |
| 4406 | if (ret) |
| 4407 | return ret; |
| 4408 | |
Josef Bacik | a41ad39 | 2011-01-31 15:30:16 -0500 | [diff] [blame] | 4409 | /* we don't support swapfiles, so vmtruncate shouldn't fail */ |
| 4410 | truncate_setsize(inode, newsize); |
Miao Xie | 2e60a51 | 2013-02-08 07:01:08 +0000 | [diff] [blame] | 4411 | |
| 4412 | /* Disable nonlocked read DIO to avoid the end less truncate */ |
| 4413 | btrfs_inode_block_unlocked_dio(inode); |
| 4414 | inode_dio_wait(inode); |
| 4415 | btrfs_inode_resume_unlocked_dio(inode); |
| 4416 | |
Josef Bacik | a41ad39 | 2011-01-31 15:30:16 -0500 | [diff] [blame] | 4417 | ret = btrfs_truncate(inode); |
Josef Bacik | 7f4f6e0 | 2013-08-29 16:43:28 -0400 | [diff] [blame] | 4418 | if (ret && inode->i_nlink) { |
| 4419 | int err; |
| 4420 | |
| 4421 | /* |
| 4422 | * failed to truncate, disk_i_size is only adjusted down |
| 4423 | * as we remove extents, so it should represent the true |
| 4424 | * size of the inode, so reset the in memory size and |
| 4425 | * delete our orphan entry. |
| 4426 | */ |
| 4427 | trans = btrfs_join_transaction(root); |
| 4428 | if (IS_ERR(trans)) { |
| 4429 | btrfs_orphan_del(NULL, inode); |
| 4430 | return ret; |
| 4431 | } |
| 4432 | i_size_write(inode, BTRFS_I(inode)->disk_i_size); |
| 4433 | err = btrfs_orphan_del(trans, inode); |
| 4434 | if (err) |
| 4435 | btrfs_abort_transaction(trans, root, err); |
| 4436 | btrfs_end_transaction(trans, root); |
| 4437 | } |
Yan, Zheng | 8082510 | 2009-11-12 09:35:36 +0000 | [diff] [blame] | 4438 | } |
| 4439 | |
Josef Bacik | a41ad39 | 2011-01-31 15:30:16 -0500 | [diff] [blame] | 4440 | return ret; |
Yan, Zheng | 8082510 | 2009-11-12 09:35:36 +0000 | [diff] [blame] | 4441 | } |
| 4442 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4443 | static int btrfs_setattr(struct dentry *dentry, struct iattr *attr) |
| 4444 | { |
| 4445 | struct inode *inode = dentry->d_inode; |
Li Zefan | b83cc96 | 2010-12-20 16:04:08 +0800 | [diff] [blame] | 4446 | struct btrfs_root *root = BTRFS_I(inode)->root; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4447 | int err; |
| 4448 | |
Li Zefan | b83cc96 | 2010-12-20 16:04:08 +0800 | [diff] [blame] | 4449 | if (btrfs_root_readonly(root)) |
| 4450 | return -EROFS; |
| 4451 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4452 | err = inode_change_ok(inode, attr); |
| 4453 | if (err) |
| 4454 | return err; |
| 4455 | |
Chris Mason | 5a3f23d | 2009-03-31 13:27:11 -0400 | [diff] [blame] | 4456 | if (S_ISREG(inode->i_mode) && (attr->ia_valid & ATTR_SIZE)) { |
Eric Sandeen | 3972f26 | 2013-01-12 02:57:22 +0000 | [diff] [blame] | 4457 | err = btrfs_setsize(inode, attr); |
Yan, Zheng | 8082510 | 2009-11-12 09:35:36 +0000 | [diff] [blame] | 4458 | if (err) |
| 4459 | return err; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4460 | } |
Yan Zheng | 9036c10 | 2008-10-30 14:19:41 -0400 | [diff] [blame] | 4461 | |
Christoph Hellwig | 1025774 | 2010-06-04 11:30:02 +0200 | [diff] [blame] | 4462 | if (attr->ia_valid) { |
| 4463 | setattr_copy(inode, attr); |
Josef Bacik | 0c4d2d9 | 2012-04-05 15:03:02 -0400 | [diff] [blame] | 4464 | inode_inc_iversion(inode); |
Josef Bacik | 22c44fe | 2011-11-30 10:45:38 -0500 | [diff] [blame] | 4465 | err = btrfs_dirty_inode(inode); |
Josef Bacik | 33268ea | 2008-07-24 12:16:36 -0400 | [diff] [blame] | 4466 | |
Josef Bacik | 22c44fe | 2011-11-30 10:45:38 -0500 | [diff] [blame] | 4467 | if (!err && attr->ia_valid & ATTR_MODE) |
Christoph Hellwig | 1025774 | 2010-06-04 11:30:02 +0200 | [diff] [blame] | 4468 | err = btrfs_acl_chmod(inode); |
| 4469 | } |
| 4470 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4471 | return err; |
| 4472 | } |
Chris Mason | 61295eb | 2008-01-14 16:24:38 -0500 | [diff] [blame] | 4473 | |
Al Viro | bd55597 | 2010-06-07 11:35:40 -0400 | [diff] [blame] | 4474 | void btrfs_evict_inode(struct inode *inode) |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4475 | { |
| 4476 | struct btrfs_trans_handle *trans; |
| 4477 | struct btrfs_root *root = BTRFS_I(inode)->root; |
Josef Bacik | 726c35f | 2011-09-26 15:46:06 -0400 | [diff] [blame] | 4478 | struct btrfs_block_rsv *rsv, *global_rsv; |
Josef Bacik | 0712718 | 2011-08-19 10:29:59 -0400 | [diff] [blame] | 4479 | u64 min_size = btrfs_calc_trunc_metadata_size(root, 1); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4480 | int ret; |
| 4481 | |
liubo | 1abe9b8 | 2011-03-24 11:18:59 +0000 | [diff] [blame] | 4482 | trace_btrfs_inode_evict(inode); |
| 4483 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4484 | truncate_inode_pages(&inode->i_data, 0); |
Stefan Behrens | 69e9c6c | 2013-09-05 16:58:43 +0200 | [diff] [blame] | 4485 | if (inode->i_nlink && |
| 4486 | ((btrfs_root_refs(&root->root_item) != 0 && |
| 4487 | root->root_key.objectid != BTRFS_ROOT_TREE_OBJECTID) || |
| 4488 | btrfs_is_free_space_inode(inode))) |
Al Viro | bd55597 | 2010-06-07 11:35:40 -0400 | [diff] [blame] | 4489 | goto no_delete; |
| 4490 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4491 | if (is_bad_inode(inode)) { |
Josef Bacik | 7b12876 | 2008-07-24 12:17:14 -0400 | [diff] [blame] | 4492 | btrfs_orphan_del(NULL, inode); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4493 | goto no_delete; |
| 4494 | } |
Al Viro | bd55597 | 2010-06-07 11:35:40 -0400 | [diff] [blame] | 4495 | /* 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] | 4496 | btrfs_wait_ordered_range(inode, 0, (u64)-1); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 4497 | |
Yan, Zheng | c71bf09 | 2009-11-12 09:34:40 +0000 | [diff] [blame] | 4498 | if (root->fs_info->log_root_recovering) { |
Liu Bo | 6bf0231 | 2012-06-25 21:59:09 -0600 | [diff] [blame] | 4499 | BUG_ON(test_bit(BTRFS_INODE_HAS_ORPHAN_ITEM, |
Josef Bacik | 8a35d95 | 2012-05-23 14:26:42 -0400 | [diff] [blame] | 4500 | &BTRFS_I(inode)->runtime_flags)); |
Yan, Zheng | c71bf09 | 2009-11-12 09:34:40 +0000 | [diff] [blame] | 4501 | goto no_delete; |
| 4502 | } |
| 4503 | |
Yan, Zheng | 76dda93 | 2009-09-21 16:00:26 -0400 | [diff] [blame] | 4504 | if (inode->i_nlink > 0) { |
Stefan Behrens | 69e9c6c | 2013-09-05 16:58:43 +0200 | [diff] [blame] | 4505 | BUG_ON(btrfs_root_refs(&root->root_item) != 0 && |
| 4506 | root->root_key.objectid != BTRFS_ROOT_TREE_OBJECTID); |
Yan, Zheng | 76dda93 | 2009-09-21 16:00:26 -0400 | [diff] [blame] | 4507 | goto no_delete; |
| 4508 | } |
| 4509 | |
Miao Xie | 0e8c36a | 2012-12-19 06:59:51 +0000 | [diff] [blame] | 4510 | ret = btrfs_commit_inode_delayed_inode(inode); |
| 4511 | if (ret) { |
| 4512 | btrfs_orphan_del(NULL, inode); |
| 4513 | goto no_delete; |
| 4514 | } |
| 4515 | |
Miao Xie | 66d8f3d | 2012-09-06 04:02:28 -0600 | [diff] [blame] | 4516 | rsv = btrfs_alloc_block_rsv(root, BTRFS_BLOCK_RSV_TEMP); |
Josef Bacik | 4289a66 | 2011-08-05 13:22:24 -0400 | [diff] [blame] | 4517 | if (!rsv) { |
| 4518 | btrfs_orphan_del(NULL, inode); |
| 4519 | goto no_delete; |
| 4520 | } |
Josef Bacik | 4a33854 | 2011-08-29 11:01:31 -0400 | [diff] [blame] | 4521 | rsv->size = min_size; |
Josef Bacik | ca7e70f | 2012-08-27 17:48:15 -0400 | [diff] [blame] | 4522 | rsv->failfast = 1; |
Josef Bacik | 726c35f | 2011-09-26 15:46:06 -0400 | [diff] [blame] | 4523 | global_rsv = &root->fs_info->global_block_rsv; |
Josef Bacik | 4289a66 | 2011-08-05 13:22:24 -0400 | [diff] [blame] | 4524 | |
Chris Mason | dbe674a | 2008-07-17 12:54:05 -0400 | [diff] [blame] | 4525 | btrfs_i_size_write(inode, 0); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 4526 | |
Josef Bacik | 4289a66 | 2011-08-05 13:22:24 -0400 | [diff] [blame] | 4527 | /* |
Miao Xie | 8407aa4 | 2012-09-07 01:43:32 -0600 | [diff] [blame] | 4528 | * This is a bit simpler than btrfs_truncate since we've already |
| 4529 | * reserved our space for our orphan item in the unlink, so we just |
| 4530 | * need to reserve some slack space in case we add bytes and update |
| 4531 | * inode item when doing the truncate. |
Josef Bacik | 4289a66 | 2011-08-05 13:22:24 -0400 | [diff] [blame] | 4532 | */ |
Yan, Zheng | 8082510 | 2009-11-12 09:35:36 +0000 | [diff] [blame] | 4533 | while (1) { |
Miao Xie | 08e007d | 2012-10-16 11:33:38 +0000 | [diff] [blame] | 4534 | ret = btrfs_block_rsv_refill(root, rsv, min_size, |
| 4535 | BTRFS_RESERVE_FLUSH_LIMIT); |
Yan, Zheng | 8082510 | 2009-11-12 09:35:36 +0000 | [diff] [blame] | 4536 | |
Josef Bacik | 726c35f | 2011-09-26 15:46:06 -0400 | [diff] [blame] | 4537 | /* |
| 4538 | * Try and steal from the global reserve since we will |
| 4539 | * likely not use this space anyway, we want to try as |
| 4540 | * hard as possible to get this to work. |
| 4541 | */ |
| 4542 | if (ret) |
| 4543 | ret = btrfs_block_rsv_migrate(global_rsv, rsv, min_size); |
| 4544 | |
Yan, Zheng | d68fc57 | 2010-05-16 10:49:58 -0400 | [diff] [blame] | 4545 | if (ret) { |
Simon Kirby | c2cf52e | 2013-03-19 22:41:23 +0000 | [diff] [blame] | 4546 | btrfs_warn(root->fs_info, |
| 4547 | "Could not get space for a delete, will truncate on mount %d", |
| 4548 | ret); |
Josef Bacik | 4289a66 | 2011-08-05 13:22:24 -0400 | [diff] [blame] | 4549 | btrfs_orphan_del(NULL, inode); |
| 4550 | btrfs_free_block_rsv(root, rsv); |
| 4551 | goto no_delete; |
Yan, Zheng | d68fc57 | 2010-05-16 10:49:58 -0400 | [diff] [blame] | 4552 | } |
| 4553 | |
Miao Xie | 0e8c36a | 2012-12-19 06:59:51 +0000 | [diff] [blame] | 4554 | trans = btrfs_join_transaction(root); |
Josef Bacik | 4289a66 | 2011-08-05 13:22:24 -0400 | [diff] [blame] | 4555 | if (IS_ERR(trans)) { |
| 4556 | btrfs_orphan_del(NULL, inode); |
| 4557 | btrfs_free_block_rsv(root, rsv); |
| 4558 | goto no_delete; |
| 4559 | } |
| 4560 | |
| 4561 | trans->block_rsv = rsv; |
| 4562 | |
Yan, Zheng | d68fc57 | 2010-05-16 10:49:58 -0400 | [diff] [blame] | 4563 | ret = btrfs_truncate_inode_items(trans, root, inode, 0, 0); |
Josef Bacik | ca7e70f | 2012-08-27 17:48:15 -0400 | [diff] [blame] | 4564 | if (ret != -ENOSPC) |
Yan, Zheng | 8082510 | 2009-11-12 09:35:36 +0000 | [diff] [blame] | 4565 | break; |
| 4566 | |
Miao Xie | 8407aa4 | 2012-09-07 01:43:32 -0600 | [diff] [blame] | 4567 | trans->block_rsv = &root->fs_info->trans_block_rsv; |
Yan, Zheng | 8082510 | 2009-11-12 09:35:36 +0000 | [diff] [blame] | 4568 | btrfs_end_transaction(trans, root); |
| 4569 | trans = NULL; |
Liu Bo | b53d3f5 | 2012-11-14 14:34:34 +0000 | [diff] [blame] | 4570 | btrfs_btree_balance_dirty(root); |
Josef Bacik | 7b12876 | 2008-07-24 12:17:14 -0400 | [diff] [blame] | 4571 | } |
| 4572 | |
Josef Bacik | 4289a66 | 2011-08-05 13:22:24 -0400 | [diff] [blame] | 4573 | btrfs_free_block_rsv(root, rsv); |
| 4574 | |
Josef Bacik | 4ef31a4 | 2013-08-13 14:10:08 -0400 | [diff] [blame] | 4575 | /* |
| 4576 | * Errors here aren't a big deal, it just means we leave orphan items |
| 4577 | * in the tree. They will be cleaned up on the next mount. |
| 4578 | */ |
Yan, Zheng | 8082510 | 2009-11-12 09:35:36 +0000 | [diff] [blame] | 4579 | if (ret == 0) { |
Josef Bacik | 4289a66 | 2011-08-05 13:22:24 -0400 | [diff] [blame] | 4580 | trans->block_rsv = root->orphan_block_rsv; |
Josef Bacik | 4ef31a4 | 2013-08-13 14:10:08 -0400 | [diff] [blame] | 4581 | btrfs_orphan_del(trans, inode); |
| 4582 | } else { |
| 4583 | btrfs_orphan_del(NULL, inode); |
Yan, Zheng | 8082510 | 2009-11-12 09:35:36 +0000 | [diff] [blame] | 4584 | } |
Chris Mason | 85e21ba | 2008-01-29 15:11:36 -0500 | [diff] [blame] | 4585 | |
Josef Bacik | 4289a66 | 2011-08-05 13:22:24 -0400 | [diff] [blame] | 4586 | trans->block_rsv = &root->fs_info->trans_block_rsv; |
Li Zefan | 581bb05 | 2011-04-20 10:06:11 +0800 | [diff] [blame] | 4587 | if (!(root == root->fs_info->tree_root || |
| 4588 | root->root_key.objectid == BTRFS_TREE_RELOC_OBJECTID)) |
Li Zefan | 33345d01 | 2011-04-20 10:31:50 +0800 | [diff] [blame] | 4589 | btrfs_return_ino(root, btrfs_ino(inode)); |
Li Zefan | 581bb05 | 2011-04-20 10:06:11 +0800 | [diff] [blame] | 4590 | |
Chris Mason | 54aa1f4 | 2007-06-22 14:16:25 -0400 | [diff] [blame] | 4591 | btrfs_end_transaction(trans, root); |
Liu Bo | b53d3f5 | 2012-11-14 14:34:34 +0000 | [diff] [blame] | 4592 | btrfs_btree_balance_dirty(root); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4593 | no_delete: |
Miao Xie | 89042e5 | 2013-05-15 07:48:15 +0000 | [diff] [blame] | 4594 | btrfs_remove_delayed_node(inode); |
Jan Kara | dbd5768 | 2012-05-03 14:48:02 +0200 | [diff] [blame] | 4595 | clear_inode(inode); |
Yan, Zheng | 8082510 | 2009-11-12 09:35:36 +0000 | [diff] [blame] | 4596 | return; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4597 | } |
| 4598 | |
| 4599 | /* |
| 4600 | * this returns the key found in the dir entry in the location pointer. |
| 4601 | * If no dir entries were found, location->objectid is 0. |
| 4602 | */ |
| 4603 | static int btrfs_inode_by_name(struct inode *dir, struct dentry *dentry, |
| 4604 | struct btrfs_key *location) |
| 4605 | { |
| 4606 | const char *name = dentry->d_name.name; |
| 4607 | int namelen = dentry->d_name.len; |
| 4608 | struct btrfs_dir_item *di; |
| 4609 | struct btrfs_path *path; |
| 4610 | struct btrfs_root *root = BTRFS_I(dir)->root; |
Yan | 0d9f7f3 | 2007-10-25 15:48:28 -0400 | [diff] [blame] | 4611 | int ret = 0; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4612 | |
| 4613 | path = btrfs_alloc_path(); |
Mark Fasheh | d8926bb | 2011-07-13 10:38:47 -0700 | [diff] [blame] | 4614 | if (!path) |
| 4615 | return -ENOMEM; |
Chris Mason | 3954401 | 2007-12-12 14:38:19 -0500 | [diff] [blame] | 4616 | |
Li Zefan | 33345d01 | 2011-04-20 10:31:50 +0800 | [diff] [blame] | 4617 | di = btrfs_lookup_dir_item(NULL, root, path, btrfs_ino(dir), name, |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4618 | namelen, 0); |
Yan | 0d9f7f3 | 2007-10-25 15:48:28 -0400 | [diff] [blame] | 4619 | if (IS_ERR(di)) |
| 4620 | ret = PTR_ERR(di); |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 4621 | |
David Sterba | c704005 | 2011-04-19 18:00:01 +0200 | [diff] [blame] | 4622 | if (IS_ERR_OR_NULL(di)) |
Chris Mason | 3954401 | 2007-12-12 14:38:19 -0500 | [diff] [blame] | 4623 | goto out_err; |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 4624 | |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 4625 | btrfs_dir_item_key_to_cpu(path->nodes[0], di, location); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4626 | out: |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4627 | btrfs_free_path(path); |
| 4628 | return ret; |
Chris Mason | 3954401 | 2007-12-12 14:38:19 -0500 | [diff] [blame] | 4629 | out_err: |
| 4630 | location->objectid = 0; |
| 4631 | goto out; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4632 | } |
| 4633 | |
| 4634 | /* |
| 4635 | * when we hit a tree root in a directory, the btrfs part of the inode |
| 4636 | * needs to be changed to reflect the root directory of the tree root. This |
| 4637 | * is kind of like crossing a mount point. |
| 4638 | */ |
| 4639 | static int fixup_tree_root_location(struct btrfs_root *root, |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 4640 | struct inode *dir, |
| 4641 | struct dentry *dentry, |
| 4642 | struct btrfs_key *location, |
| 4643 | struct btrfs_root **sub_root) |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4644 | { |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 4645 | struct btrfs_path *path; |
| 4646 | struct btrfs_root *new_root; |
| 4647 | struct btrfs_root_ref *ref; |
| 4648 | struct extent_buffer *leaf; |
| 4649 | int ret; |
| 4650 | int err = 0; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4651 | |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 4652 | path = btrfs_alloc_path(); |
| 4653 | if (!path) { |
| 4654 | err = -ENOMEM; |
| 4655 | goto out; |
| 4656 | } |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4657 | |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 4658 | err = -ENOENT; |
| 4659 | ret = btrfs_find_root_ref(root->fs_info->tree_root, path, |
| 4660 | BTRFS_I(dir)->root->root_key.objectid, |
| 4661 | location->objectid); |
| 4662 | if (ret) { |
| 4663 | if (ret < 0) |
| 4664 | err = ret; |
| 4665 | goto out; |
| 4666 | } |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4667 | |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 4668 | leaf = path->nodes[0]; |
| 4669 | ref = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_root_ref); |
Li Zefan | 33345d01 | 2011-04-20 10:31:50 +0800 | [diff] [blame] | 4670 | if (btrfs_root_ref_dirid(leaf, ref) != btrfs_ino(dir) || |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 4671 | btrfs_root_ref_name_len(leaf, ref) != dentry->d_name.len) |
| 4672 | goto out; |
| 4673 | |
| 4674 | ret = memcmp_extent_buffer(leaf, dentry->d_name.name, |
| 4675 | (unsigned long)(ref + 1), |
| 4676 | dentry->d_name.len); |
| 4677 | if (ret) |
| 4678 | goto out; |
| 4679 | |
David Sterba | b3b4aa7 | 2011-04-21 01:20:15 +0200 | [diff] [blame] | 4680 | btrfs_release_path(path); |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 4681 | |
| 4682 | new_root = btrfs_read_fs_root_no_name(root->fs_info, location); |
| 4683 | if (IS_ERR(new_root)) { |
| 4684 | err = PTR_ERR(new_root); |
| 4685 | goto out; |
| 4686 | } |
| 4687 | |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 4688 | *sub_root = new_root; |
| 4689 | location->objectid = btrfs_root_dirid(&new_root->root_item); |
| 4690 | location->type = BTRFS_INODE_ITEM_KEY; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4691 | location->offset = 0; |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 4692 | err = 0; |
| 4693 | out: |
| 4694 | btrfs_free_path(path); |
| 4695 | return err; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4696 | } |
| 4697 | |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 4698 | static void inode_tree_add(struct inode *inode) |
| 4699 | { |
| 4700 | struct btrfs_root *root = BTRFS_I(inode)->root; |
| 4701 | struct btrfs_inode *entry; |
From: Nick Piggin | 03e860b | 2009-08-21 10:09:44 +0200 | [diff] [blame] | 4702 | struct rb_node **p; |
| 4703 | struct rb_node *parent; |
Filipe David Borba Manana | cef2193 | 2013-09-02 12:19:13 +0100 | [diff] [blame] | 4704 | struct rb_node *new = &BTRFS_I(inode)->rb_node; |
Li Zefan | 33345d01 | 2011-04-20 10:31:50 +0800 | [diff] [blame] | 4705 | u64 ino = btrfs_ino(inode); |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 4706 | |
Al Viro | 1d3382cb | 2010-10-23 15:19:20 -0400 | [diff] [blame] | 4707 | if (inode_unhashed(inode)) |
Yan, Zheng | 76dda93 | 2009-09-21 16:00:26 -0400 | [diff] [blame] | 4708 | return; |
Miao Xie | e1409ce | 2013-05-15 07:48:16 +0000 | [diff] [blame] | 4709 | parent = NULL; |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 4710 | spin_lock(&root->inode_lock); |
Miao Xie | e1409ce | 2013-05-15 07:48:16 +0000 | [diff] [blame] | 4711 | p = &root->inode_tree.rb_node; |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 4712 | while (*p) { |
| 4713 | parent = *p; |
| 4714 | entry = rb_entry(parent, struct btrfs_inode, rb_node); |
| 4715 | |
Li Zefan | 33345d01 | 2011-04-20 10:31:50 +0800 | [diff] [blame] | 4716 | if (ino < btrfs_ino(&entry->vfs_inode)) |
From: Nick Piggin | 03e860b | 2009-08-21 10:09:44 +0200 | [diff] [blame] | 4717 | p = &parent->rb_left; |
Li Zefan | 33345d01 | 2011-04-20 10:31:50 +0800 | [diff] [blame] | 4718 | else if (ino > btrfs_ino(&entry->vfs_inode)) |
From: Nick Piggin | 03e860b | 2009-08-21 10:09:44 +0200 | [diff] [blame] | 4719 | p = &parent->rb_right; |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 4720 | else { |
| 4721 | WARN_ON(!(entry->vfs_inode.i_state & |
Al Viro | a4ffdde | 2010-06-02 17:38:30 -0400 | [diff] [blame] | 4722 | (I_WILL_FREE | I_FREEING))); |
Filipe David Borba Manana | cef2193 | 2013-09-02 12:19:13 +0100 | [diff] [blame] | 4723 | rb_replace_node(parent, new, &root->inode_tree); |
From: Nick Piggin | 03e860b | 2009-08-21 10:09:44 +0200 | [diff] [blame] | 4724 | RB_CLEAR_NODE(parent); |
| 4725 | spin_unlock(&root->inode_lock); |
Filipe David Borba Manana | cef2193 | 2013-09-02 12:19:13 +0100 | [diff] [blame] | 4726 | return; |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 4727 | } |
| 4728 | } |
Filipe David Borba Manana | cef2193 | 2013-09-02 12:19:13 +0100 | [diff] [blame] | 4729 | rb_link_node(new, parent, p); |
| 4730 | rb_insert_color(new, &root->inode_tree); |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 4731 | spin_unlock(&root->inode_lock); |
| 4732 | } |
| 4733 | |
| 4734 | static void inode_tree_del(struct inode *inode) |
| 4735 | { |
| 4736 | struct btrfs_root *root = BTRFS_I(inode)->root; |
Yan, Zheng | 76dda93 | 2009-09-21 16:00:26 -0400 | [diff] [blame] | 4737 | int empty = 0; |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 4738 | |
From: Nick Piggin | 03e860b | 2009-08-21 10:09:44 +0200 | [diff] [blame] | 4739 | spin_lock(&root->inode_lock); |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 4740 | if (!RB_EMPTY_NODE(&BTRFS_I(inode)->rb_node)) { |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 4741 | rb_erase(&BTRFS_I(inode)->rb_node, &root->inode_tree); |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 4742 | RB_CLEAR_NODE(&BTRFS_I(inode)->rb_node); |
Yan, Zheng | 76dda93 | 2009-09-21 16:00:26 -0400 | [diff] [blame] | 4743 | empty = RB_EMPTY_ROOT(&root->inode_tree); |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 4744 | } |
From: Nick Piggin | 03e860b | 2009-08-21 10:09:44 +0200 | [diff] [blame] | 4745 | spin_unlock(&root->inode_lock); |
Yan, Zheng | 76dda93 | 2009-09-21 16:00:26 -0400 | [diff] [blame] | 4746 | |
Stefan Behrens | 69e9c6c | 2013-09-05 16:58:43 +0200 | [diff] [blame] | 4747 | if (empty && btrfs_root_refs(&root->root_item) == 0) { |
Yan, Zheng | 76dda93 | 2009-09-21 16:00:26 -0400 | [diff] [blame] | 4748 | synchronize_srcu(&root->fs_info->subvol_srcu); |
| 4749 | spin_lock(&root->inode_lock); |
| 4750 | empty = RB_EMPTY_ROOT(&root->inode_tree); |
| 4751 | spin_unlock(&root->inode_lock); |
| 4752 | if (empty) |
| 4753 | btrfs_add_dead_root(root); |
| 4754 | } |
| 4755 | } |
| 4756 | |
Jeff Mahoney | 143bede | 2012-03-01 14:56:26 +0100 | [diff] [blame] | 4757 | void btrfs_invalidate_inodes(struct btrfs_root *root) |
Yan, Zheng | 76dda93 | 2009-09-21 16:00:26 -0400 | [diff] [blame] | 4758 | { |
| 4759 | struct rb_node *node; |
| 4760 | struct rb_node *prev; |
| 4761 | struct btrfs_inode *entry; |
| 4762 | struct inode *inode; |
| 4763 | u64 objectid = 0; |
| 4764 | |
| 4765 | WARN_ON(btrfs_root_refs(&root->root_item) != 0); |
| 4766 | |
| 4767 | spin_lock(&root->inode_lock); |
| 4768 | again: |
| 4769 | node = root->inode_tree.rb_node; |
| 4770 | prev = NULL; |
| 4771 | while (node) { |
| 4772 | prev = node; |
| 4773 | entry = rb_entry(node, struct btrfs_inode, rb_node); |
| 4774 | |
Li Zefan | 33345d01 | 2011-04-20 10:31:50 +0800 | [diff] [blame] | 4775 | if (objectid < btrfs_ino(&entry->vfs_inode)) |
Yan, Zheng | 76dda93 | 2009-09-21 16:00:26 -0400 | [diff] [blame] | 4776 | node = node->rb_left; |
Li Zefan | 33345d01 | 2011-04-20 10:31:50 +0800 | [diff] [blame] | 4777 | else if (objectid > btrfs_ino(&entry->vfs_inode)) |
Yan, Zheng | 76dda93 | 2009-09-21 16:00:26 -0400 | [diff] [blame] | 4778 | node = node->rb_right; |
| 4779 | else |
| 4780 | break; |
| 4781 | } |
| 4782 | if (!node) { |
| 4783 | while (prev) { |
| 4784 | entry = rb_entry(prev, struct btrfs_inode, rb_node); |
Li Zefan | 33345d01 | 2011-04-20 10:31:50 +0800 | [diff] [blame] | 4785 | if (objectid <= btrfs_ino(&entry->vfs_inode)) { |
Yan, Zheng | 76dda93 | 2009-09-21 16:00:26 -0400 | [diff] [blame] | 4786 | node = prev; |
| 4787 | break; |
| 4788 | } |
| 4789 | prev = rb_next(prev); |
| 4790 | } |
| 4791 | } |
| 4792 | while (node) { |
| 4793 | entry = rb_entry(node, struct btrfs_inode, rb_node); |
Li Zefan | 33345d01 | 2011-04-20 10:31:50 +0800 | [diff] [blame] | 4794 | objectid = btrfs_ino(&entry->vfs_inode) + 1; |
Yan, Zheng | 76dda93 | 2009-09-21 16:00:26 -0400 | [diff] [blame] | 4795 | inode = igrab(&entry->vfs_inode); |
| 4796 | if (inode) { |
| 4797 | spin_unlock(&root->inode_lock); |
| 4798 | if (atomic_read(&inode->i_count) > 1) |
| 4799 | d_prune_aliases(inode); |
| 4800 | /* |
Al Viro | 45321ac | 2010-06-07 13:43:19 -0400 | [diff] [blame] | 4801 | * btrfs_drop_inode will have it removed from |
Yan, Zheng | 76dda93 | 2009-09-21 16:00:26 -0400 | [diff] [blame] | 4802 | * the inode cache when its usage count |
| 4803 | * hits zero. |
| 4804 | */ |
| 4805 | iput(inode); |
| 4806 | cond_resched(); |
| 4807 | spin_lock(&root->inode_lock); |
| 4808 | goto again; |
| 4809 | } |
| 4810 | |
| 4811 | if (cond_resched_lock(&root->inode_lock)) |
| 4812 | goto again; |
| 4813 | |
| 4814 | node = rb_next(node); |
| 4815 | } |
| 4816 | spin_unlock(&root->inode_lock); |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 4817 | } |
| 4818 | |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 4819 | static int btrfs_init_locked_inode(struct inode *inode, void *p) |
| 4820 | { |
| 4821 | struct btrfs_iget_args *args = p; |
| 4822 | inode->i_ino = args->ino; |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 4823 | BTRFS_I(inode)->root = args->root; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4824 | return 0; |
| 4825 | } |
| 4826 | |
| 4827 | static int btrfs_find_actor(struct inode *inode, void *opaque) |
| 4828 | { |
| 4829 | struct btrfs_iget_args *args = opaque; |
Li Zefan | 33345d01 | 2011-04-20 10:31:50 +0800 | [diff] [blame] | 4830 | return args->ino == btrfs_ino(inode) && |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 4831 | args->root == BTRFS_I(inode)->root; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4832 | } |
| 4833 | |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 4834 | static struct inode *btrfs_iget_locked(struct super_block *s, |
| 4835 | u64 objectid, |
| 4836 | struct btrfs_root *root) |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4837 | { |
| 4838 | struct inode *inode; |
| 4839 | struct btrfs_iget_args args; |
Filipe David Borba Manana | 778ba82 | 2013-10-06 22:22:33 +0100 | [diff] [blame] | 4840 | unsigned long hashval = btrfs_inode_hash(objectid, root); |
| 4841 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4842 | args.ino = objectid; |
| 4843 | args.root = root; |
| 4844 | |
Filipe David Borba Manana | 778ba82 | 2013-10-06 22:22:33 +0100 | [diff] [blame] | 4845 | inode = iget5_locked(s, hashval, btrfs_find_actor, |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4846 | btrfs_init_locked_inode, |
| 4847 | (void *)&args); |
| 4848 | return inode; |
| 4849 | } |
| 4850 | |
Balaji Rao | 1a54ef8 | 2008-07-21 02:01:04 +0530 | [diff] [blame] | 4851 | /* Get an inode object given its location and corresponding root. |
| 4852 | * Returns in *is_new if the inode was read from disk |
| 4853 | */ |
| 4854 | struct inode *btrfs_iget(struct super_block *s, struct btrfs_key *location, |
Josef Bacik | 73f7341 | 2009-12-04 17:38:27 +0000 | [diff] [blame] | 4855 | struct btrfs_root *root, int *new) |
Balaji Rao | 1a54ef8 | 2008-07-21 02:01:04 +0530 | [diff] [blame] | 4856 | { |
| 4857 | struct inode *inode; |
| 4858 | |
| 4859 | inode = btrfs_iget_locked(s, location->objectid, root); |
| 4860 | if (!inode) |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 4861 | return ERR_PTR(-ENOMEM); |
Balaji Rao | 1a54ef8 | 2008-07-21 02:01:04 +0530 | [diff] [blame] | 4862 | |
| 4863 | if (inode->i_state & I_NEW) { |
| 4864 | BTRFS_I(inode)->root = root; |
| 4865 | memcpy(&BTRFS_I(inode)->location, location, sizeof(*location)); |
| 4866 | btrfs_read_locked_inode(inode); |
Mark Fasheh | 1748f84 | 2011-07-12 11:25:31 -0700 | [diff] [blame] | 4867 | if (!is_bad_inode(inode)) { |
| 4868 | inode_tree_add(inode); |
| 4869 | unlock_new_inode(inode); |
| 4870 | if (new) |
| 4871 | *new = 1; |
| 4872 | } else { |
Sergei Trofimovich | e0b6d65 | 2011-09-11 10:52:24 -0400 | [diff] [blame] | 4873 | unlock_new_inode(inode); |
| 4874 | iput(inode); |
| 4875 | inode = ERR_PTR(-ESTALE); |
Mark Fasheh | 1748f84 | 2011-07-12 11:25:31 -0700 | [diff] [blame] | 4876 | } |
| 4877 | } |
| 4878 | |
Balaji Rao | 1a54ef8 | 2008-07-21 02:01:04 +0530 | [diff] [blame] | 4879 | return inode; |
| 4880 | } |
| 4881 | |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 4882 | static struct inode *new_simple_dir(struct super_block *s, |
| 4883 | struct btrfs_key *key, |
| 4884 | struct btrfs_root *root) |
| 4885 | { |
| 4886 | struct inode *inode = new_inode(s); |
| 4887 | |
| 4888 | if (!inode) |
| 4889 | return ERR_PTR(-ENOMEM); |
| 4890 | |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 4891 | BTRFS_I(inode)->root = root; |
| 4892 | memcpy(&BTRFS_I(inode)->location, key, sizeof(*key)); |
Josef Bacik | 72ac3c0 | 2012-05-23 14:13:11 -0400 | [diff] [blame] | 4893 | set_bit(BTRFS_INODE_DUMMY, &BTRFS_I(inode)->runtime_flags); |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 4894 | |
| 4895 | inode->i_ino = BTRFS_EMPTY_SUBVOL_DIR_OBJECTID; |
Li Zefan | 848cce0 | 2012-02-21 17:04:28 +0800 | [diff] [blame] | 4896 | inode->i_op = &btrfs_dir_ro_inode_operations; |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 4897 | inode->i_fop = &simple_dir_operations; |
| 4898 | inode->i_mode = S_IFDIR | S_IRUGO | S_IWUSR | S_IXUGO; |
| 4899 | inode->i_mtime = inode->i_atime = inode->i_ctime = CURRENT_TIME; |
| 4900 | |
| 4901 | return inode; |
| 4902 | } |
| 4903 | |
Chris Mason | 3de4586 | 2008-11-17 21:02:50 -0500 | [diff] [blame] | 4904 | struct inode *btrfs_lookup_dentry(struct inode *dir, struct dentry *dentry) |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4905 | { |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 4906 | struct inode *inode; |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 4907 | struct btrfs_root *root = BTRFS_I(dir)->root; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4908 | struct btrfs_root *sub_root = root; |
| 4909 | struct btrfs_key location; |
Yan, Zheng | 76dda93 | 2009-09-21 16:00:26 -0400 | [diff] [blame] | 4910 | int index; |
Josef Bacik | b4aff1f | 2011-06-28 16:18:59 -0400 | [diff] [blame] | 4911 | int ret = 0; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4912 | |
| 4913 | if (dentry->d_name.len > BTRFS_NAME_LEN) |
| 4914 | return ERR_PTR(-ENAMETOOLONG); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 4915 | |
Jeff Layton | 39e3c95 | 2012-11-28 11:30:53 -0500 | [diff] [blame] | 4916 | ret = btrfs_inode_by_name(dir, dentry, &location); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4917 | if (ret < 0) |
| 4918 | return ERR_PTR(ret); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 4919 | |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 4920 | if (location.objectid == 0) |
| 4921 | return NULL; |
| 4922 | |
| 4923 | if (location.type == BTRFS_INODE_ITEM_KEY) { |
Josef Bacik | 73f7341 | 2009-12-04 17:38:27 +0000 | [diff] [blame] | 4924 | inode = btrfs_iget(dir->i_sb, &location, root, NULL); |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 4925 | return inode; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4926 | } |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 4927 | |
| 4928 | BUG_ON(location.type != BTRFS_ROOT_ITEM_KEY); |
| 4929 | |
Yan, Zheng | 76dda93 | 2009-09-21 16:00:26 -0400 | [diff] [blame] | 4930 | index = srcu_read_lock(&root->fs_info->subvol_srcu); |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 4931 | ret = fixup_tree_root_location(root, dir, dentry, |
| 4932 | &location, &sub_root); |
| 4933 | if (ret < 0) { |
| 4934 | if (ret != -ENOENT) |
| 4935 | inode = ERR_PTR(ret); |
| 4936 | else |
| 4937 | inode = new_simple_dir(dir->i_sb, &location, sub_root); |
| 4938 | } else { |
Josef Bacik | 73f7341 | 2009-12-04 17:38:27 +0000 | [diff] [blame] | 4939 | inode = btrfs_iget(dir->i_sb, &location, sub_root, NULL); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4940 | } |
Yan, Zheng | 76dda93 | 2009-09-21 16:00:26 -0400 | [diff] [blame] | 4941 | srcu_read_unlock(&root->fs_info->subvol_srcu, index); |
| 4942 | |
Julia Lawall | 34d19ba | 2011-01-24 19:55:19 +0000 | [diff] [blame] | 4943 | if (!IS_ERR(inode) && root != sub_root) { |
Yan, Zheng | c71bf09 | 2009-11-12 09:34:40 +0000 | [diff] [blame] | 4944 | down_read(&root->fs_info->cleanup_work_sem); |
| 4945 | if (!(inode->i_sb->s_flags & MS_RDONLY)) |
Josef Bacik | 66b4ffd | 2011-01-31 16:22:42 -0500 | [diff] [blame] | 4946 | ret = btrfs_orphan_cleanup(sub_root); |
Yan, Zheng | c71bf09 | 2009-11-12 09:34:40 +0000 | [diff] [blame] | 4947 | up_read(&root->fs_info->cleanup_work_sem); |
Josef Bacik | 01cd336 | 2013-06-03 21:39:49 -0400 | [diff] [blame] | 4948 | if (ret) { |
| 4949 | iput(inode); |
Josef Bacik | 66b4ffd | 2011-01-31 16:22:42 -0500 | [diff] [blame] | 4950 | inode = ERR_PTR(ret); |
Josef Bacik | 01cd336 | 2013-06-03 21:39:49 -0400 | [diff] [blame] | 4951 | } |
Yan, Zheng | c71bf09 | 2009-11-12 09:34:40 +0000 | [diff] [blame] | 4952 | } |
| 4953 | |
Chris Mason | 3de4586 | 2008-11-17 21:02:50 -0500 | [diff] [blame] | 4954 | return inode; |
| 4955 | } |
| 4956 | |
Nick Piggin | fe15ce4 | 2011-01-07 17:49:23 +1100 | [diff] [blame] | 4957 | static int btrfs_dentry_delete(const struct dentry *dentry) |
Yan, Zheng | 76dda93 | 2009-09-21 16:00:26 -0400 | [diff] [blame] | 4958 | { |
| 4959 | struct btrfs_root *root; |
Li Zefan | 848cce0 | 2012-02-21 17:04:28 +0800 | [diff] [blame] | 4960 | struct inode *inode = dentry->d_inode; |
Yan, Zheng | 76dda93 | 2009-09-21 16:00:26 -0400 | [diff] [blame] | 4961 | |
Li Zefan | 848cce0 | 2012-02-21 17:04:28 +0800 | [diff] [blame] | 4962 | if (!inode && !IS_ROOT(dentry)) |
| 4963 | inode = dentry->d_parent->d_inode; |
Yan, Zheng | 76dda93 | 2009-09-21 16:00:26 -0400 | [diff] [blame] | 4964 | |
Li Zefan | 848cce0 | 2012-02-21 17:04:28 +0800 | [diff] [blame] | 4965 | if (inode) { |
| 4966 | root = BTRFS_I(inode)->root; |
Yan, Zheng | efefb14 | 2009-10-09 09:25:16 -0400 | [diff] [blame] | 4967 | if (btrfs_root_refs(&root->root_item) == 0) |
| 4968 | return 1; |
Li Zefan | 848cce0 | 2012-02-21 17:04:28 +0800 | [diff] [blame] | 4969 | |
| 4970 | if (btrfs_ino(inode) == BTRFS_EMPTY_SUBVOL_DIR_OBJECTID) |
| 4971 | return 1; |
Yan, Zheng | efefb14 | 2009-10-09 09:25:16 -0400 | [diff] [blame] | 4972 | } |
Yan, Zheng | 76dda93 | 2009-09-21 16:00:26 -0400 | [diff] [blame] | 4973 | return 0; |
| 4974 | } |
| 4975 | |
Josef Bacik | b4aff1f | 2011-06-28 16:18:59 -0400 | [diff] [blame] | 4976 | static void btrfs_dentry_release(struct dentry *dentry) |
| 4977 | { |
| 4978 | if (dentry->d_fsdata) |
| 4979 | kfree(dentry->d_fsdata); |
| 4980 | } |
| 4981 | |
Chris Mason | 3de4586 | 2008-11-17 21:02:50 -0500 | [diff] [blame] | 4982 | static struct dentry *btrfs_lookup(struct inode *dir, struct dentry *dentry, |
Al Viro | 00cd8dd | 2012-06-10 17:13:09 -0400 | [diff] [blame] | 4983 | unsigned int flags) |
Chris Mason | 3de4586 | 2008-11-17 21:02:50 -0500 | [diff] [blame] | 4984 | { |
Josef Bacik | a66e7cc | 2011-09-18 10:34:03 -0400 | [diff] [blame] | 4985 | struct dentry *ret; |
| 4986 | |
| 4987 | ret = d_splice_alias(btrfs_lookup_dentry(dir, dentry), dentry); |
Josef Bacik | a66e7cc | 2011-09-18 10:34:03 -0400 | [diff] [blame] | 4988 | return ret; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4989 | } |
| 4990 | |
Miao Xie | 16cdcec | 2011-04-22 18:12:22 +0800 | [diff] [blame] | 4991 | unsigned char btrfs_filetype_table[] = { |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4992 | DT_UNKNOWN, DT_REG, DT_DIR, DT_CHR, DT_BLK, DT_FIFO, DT_SOCK, DT_LNK |
| 4993 | }; |
| 4994 | |
Al Viro | 9cdda8d | 2013-05-22 16:48:09 -0400 | [diff] [blame] | 4995 | static int btrfs_real_readdir(struct file *file, struct dir_context *ctx) |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4996 | { |
Al Viro | 9cdda8d | 2013-05-22 16:48:09 -0400 | [diff] [blame] | 4997 | struct inode *inode = file_inode(file); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4998 | struct btrfs_root *root = BTRFS_I(inode)->root; |
| 4999 | struct btrfs_item *item; |
| 5000 | struct btrfs_dir_item *di; |
| 5001 | struct btrfs_key key; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 5002 | struct btrfs_key found_key; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5003 | struct btrfs_path *path; |
Miao Xie | 16cdcec | 2011-04-22 18:12:22 +0800 | [diff] [blame] | 5004 | struct list_head ins_list; |
| 5005 | struct list_head del_list; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5006 | int ret; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 5007 | struct extent_buffer *leaf; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5008 | int slot; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5009 | unsigned char d_type; |
| 5010 | int over = 0; |
| 5011 | u32 di_cur; |
| 5012 | u32 di_total; |
| 5013 | u32 di_len; |
| 5014 | int key_type = BTRFS_DIR_INDEX_KEY; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 5015 | char tmp_name[32]; |
| 5016 | char *name_ptr; |
| 5017 | int name_len; |
Al Viro | 9cdda8d | 2013-05-22 16:48:09 -0400 | [diff] [blame] | 5018 | int is_curr = 0; /* ctx->pos points to the current index? */ |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5019 | |
| 5020 | /* FIXME, use a real flag for deciding about the key type */ |
| 5021 | if (root->fs_info->tree_root == root) |
| 5022 | key_type = BTRFS_DIR_ITEM_KEY; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 5023 | |
Al Viro | 9cdda8d | 2013-05-22 16:48:09 -0400 | [diff] [blame] | 5024 | if (!dir_emit_dots(file, ctx)) |
| 5025 | return 0; |
| 5026 | |
David Woodhouse | 49593bf | 2008-08-17 17:08:36 +0100 | [diff] [blame] | 5027 | path = btrfs_alloc_path(); |
Miao Xie | 16cdcec | 2011-04-22 18:12:22 +0800 | [diff] [blame] | 5028 | if (!path) |
| 5029 | return -ENOMEM; |
Chris Mason | ff5714c | 2011-05-28 07:00:39 -0400 | [diff] [blame] | 5030 | |
Josef Bacik | 026fd31 | 2011-05-13 10:32:11 -0400 | [diff] [blame] | 5031 | path->reada = 1; |
David Woodhouse | 49593bf | 2008-08-17 17:08:36 +0100 | [diff] [blame] | 5032 | |
Miao Xie | 16cdcec | 2011-04-22 18:12:22 +0800 | [diff] [blame] | 5033 | if (key_type == BTRFS_DIR_INDEX_KEY) { |
| 5034 | INIT_LIST_HEAD(&ins_list); |
| 5035 | INIT_LIST_HEAD(&del_list); |
| 5036 | btrfs_get_delayed_items(inode, &ins_list, &del_list); |
| 5037 | } |
| 5038 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5039 | btrfs_set_key_type(&key, key_type); |
Al Viro | 9cdda8d | 2013-05-22 16:48:09 -0400 | [diff] [blame] | 5040 | key.offset = ctx->pos; |
Li Zefan | 33345d01 | 2011-04-20 10:31:50 +0800 | [diff] [blame] | 5041 | key.objectid = btrfs_ino(inode); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 5042 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5043 | ret = btrfs_search_slot(NULL, root, &key, path, 0, 0); |
| 5044 | if (ret < 0) |
| 5045 | goto err; |
David Woodhouse | 49593bf | 2008-08-17 17:08:36 +0100 | [diff] [blame] | 5046 | |
| 5047 | while (1) { |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 5048 | leaf = path->nodes[0]; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5049 | slot = path->slots[0]; |
Li Zefan | b9e03af | 2011-03-23 10:43:58 +0800 | [diff] [blame] | 5050 | if (slot >= btrfs_header_nritems(leaf)) { |
| 5051 | ret = btrfs_next_leaf(root, path); |
| 5052 | if (ret < 0) |
| 5053 | goto err; |
| 5054 | else if (ret > 0) |
| 5055 | break; |
| 5056 | continue; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5057 | } |
Chris Mason | 3de4586 | 2008-11-17 21:02:50 -0500 | [diff] [blame] | 5058 | |
Ross Kirk | dd3cc16 | 2013-09-16 15:58:09 +0100 | [diff] [blame] | 5059 | item = btrfs_item_nr(slot); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 5060 | btrfs_item_key_to_cpu(leaf, &found_key, slot); |
| 5061 | |
| 5062 | if (found_key.objectid != key.objectid) |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5063 | break; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 5064 | if (btrfs_key_type(&found_key) != key_type) |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5065 | break; |
Al Viro | 9cdda8d | 2013-05-22 16:48:09 -0400 | [diff] [blame] | 5066 | if (found_key.offset < ctx->pos) |
Li Zefan | b9e03af | 2011-03-23 10:43:58 +0800 | [diff] [blame] | 5067 | goto next; |
Miao Xie | 16cdcec | 2011-04-22 18:12:22 +0800 | [diff] [blame] | 5068 | if (key_type == BTRFS_DIR_INDEX_KEY && |
| 5069 | btrfs_should_delete_dir_index(&del_list, |
| 5070 | found_key.offset)) |
| 5071 | goto next; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 5072 | |
Al Viro | 9cdda8d | 2013-05-22 16:48:09 -0400 | [diff] [blame] | 5073 | ctx->pos = found_key.offset; |
Miao Xie | 16cdcec | 2011-04-22 18:12:22 +0800 | [diff] [blame] | 5074 | is_curr = 1; |
David Woodhouse | 49593bf | 2008-08-17 17:08:36 +0100 | [diff] [blame] | 5075 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5076 | di = btrfs_item_ptr(leaf, slot, struct btrfs_dir_item); |
| 5077 | di_cur = 0; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 5078 | di_total = btrfs_item_size(leaf, item); |
David Woodhouse | 49593bf | 2008-08-17 17:08:36 +0100 | [diff] [blame] | 5079 | |
| 5080 | while (di_cur < di_total) { |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 5081 | struct btrfs_key location; |
| 5082 | |
Josef Bacik | 22a94d4 | 2011-03-16 16:47:17 -0400 | [diff] [blame] | 5083 | if (verify_dir_item(root, leaf, di)) |
| 5084 | break; |
| 5085 | |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 5086 | name_len = btrfs_dir_name_len(leaf, di); |
David Woodhouse | 49593bf | 2008-08-17 17:08:36 +0100 | [diff] [blame] | 5087 | if (name_len <= sizeof(tmp_name)) { |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 5088 | name_ptr = tmp_name; |
| 5089 | } else { |
| 5090 | name_ptr = kmalloc(name_len, GFP_NOFS); |
David Woodhouse | 49593bf | 2008-08-17 17:08:36 +0100 | [diff] [blame] | 5091 | if (!name_ptr) { |
| 5092 | ret = -ENOMEM; |
| 5093 | goto err; |
| 5094 | } |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 5095 | } |
| 5096 | read_extent_buffer(leaf, name_ptr, |
| 5097 | (unsigned long)(di + 1), name_len); |
| 5098 | |
| 5099 | d_type = btrfs_filetype_table[btrfs_dir_type(leaf, di)]; |
| 5100 | btrfs_dir_item_key_to_cpu(leaf, di, &location); |
Chris Mason | 3de4586 | 2008-11-17 21:02:50 -0500 | [diff] [blame] | 5101 | |
Josef Bacik | b4aff1f | 2011-06-28 16:18:59 -0400 | [diff] [blame] | 5102 | |
Chris Mason | 3de4586 | 2008-11-17 21:02:50 -0500 | [diff] [blame] | 5103 | /* is this a reference to our own snapshot? If so |
Arne Jansen | 8c9c2bf | 2012-02-25 09:09:30 +0100 | [diff] [blame] | 5104 | * skip it. |
| 5105 | * |
| 5106 | * In contrast to old kernels, we insert the snapshot's |
| 5107 | * dir item and dir index after it has been created, so |
| 5108 | * we won't find a reference to our own snapshot. We |
| 5109 | * still keep the following code for backward |
| 5110 | * compatibility. |
Chris Mason | 3de4586 | 2008-11-17 21:02:50 -0500 | [diff] [blame] | 5111 | */ |
| 5112 | if (location.type == BTRFS_ROOT_ITEM_KEY && |
| 5113 | location.objectid == root->root_key.objectid) { |
| 5114 | over = 0; |
| 5115 | goto skip; |
| 5116 | } |
Al Viro | 9cdda8d | 2013-05-22 16:48:09 -0400 | [diff] [blame] | 5117 | over = !dir_emit(ctx, name_ptr, name_len, |
| 5118 | location.objectid, d_type); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 5119 | |
Chris Mason | 3de4586 | 2008-11-17 21:02:50 -0500 | [diff] [blame] | 5120 | skip: |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 5121 | if (name_ptr != tmp_name) |
| 5122 | kfree(name_ptr); |
| 5123 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5124 | if (over) |
| 5125 | goto nopos; |
Josef Bacik | 5103e94 | 2007-11-16 11:45:54 -0500 | [diff] [blame] | 5126 | di_len = btrfs_dir_name_len(leaf, di) + |
David Woodhouse | 49593bf | 2008-08-17 17:08:36 +0100 | [diff] [blame] | 5127 | btrfs_dir_data_len(leaf, di) + sizeof(*di); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5128 | di_cur += di_len; |
| 5129 | di = (struct btrfs_dir_item *)((char *)di + di_len); |
| 5130 | } |
Li Zefan | b9e03af | 2011-03-23 10:43:58 +0800 | [diff] [blame] | 5131 | next: |
| 5132 | path->slots[0]++; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5133 | } |
David Woodhouse | 49593bf | 2008-08-17 17:08:36 +0100 | [diff] [blame] | 5134 | |
Miao Xie | 16cdcec | 2011-04-22 18:12:22 +0800 | [diff] [blame] | 5135 | if (key_type == BTRFS_DIR_INDEX_KEY) { |
| 5136 | if (is_curr) |
Al Viro | 9cdda8d | 2013-05-22 16:48:09 -0400 | [diff] [blame] | 5137 | ctx->pos++; |
| 5138 | ret = btrfs_readdir_delayed_dir_index(ctx, &ins_list); |
Miao Xie | 16cdcec | 2011-04-22 18:12:22 +0800 | [diff] [blame] | 5139 | if (ret) |
| 5140 | goto nopos; |
| 5141 | } |
| 5142 | |
David Woodhouse | 49593bf | 2008-08-17 17:08:36 +0100 | [diff] [blame] | 5143 | /* Reached end of directory/root. Bump pos past the last item. */ |
Zach Brown | db62efb | 2013-07-11 16:19:42 -0700 | [diff] [blame] | 5144 | ctx->pos++; |
| 5145 | |
| 5146 | /* |
| 5147 | * Stop new entries from being returned after we return the last |
| 5148 | * entry. |
| 5149 | * |
| 5150 | * New directory entries are assigned a strictly increasing |
| 5151 | * offset. This means that new entries created during readdir |
| 5152 | * are *guaranteed* to be seen in the future by that readdir. |
| 5153 | * This has broken buggy programs which operate on names as |
| 5154 | * they're returned by readdir. Until we re-use freed offsets |
| 5155 | * we have this hack to stop new entries from being returned |
| 5156 | * under the assumption that they'll never reach this huge |
| 5157 | * offset. |
| 5158 | * |
| 5159 | * This is being careful not to overflow 32bit loff_t unless the |
| 5160 | * last entry requires it because doing so has broken 32bit apps |
| 5161 | * in the past. |
| 5162 | */ |
| 5163 | if (key_type == BTRFS_DIR_INDEX_KEY) { |
| 5164 | if (ctx->pos >= INT_MAX) |
| 5165 | ctx->pos = LLONG_MAX; |
| 5166 | else |
| 5167 | ctx->pos = INT_MAX; |
| 5168 | } |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5169 | nopos: |
| 5170 | ret = 0; |
| 5171 | err: |
Miao Xie | 16cdcec | 2011-04-22 18:12:22 +0800 | [diff] [blame] | 5172 | if (key_type == BTRFS_DIR_INDEX_KEY) |
| 5173 | btrfs_put_delayed_items(&ins_list, &del_list); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5174 | btrfs_free_path(path); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5175 | return ret; |
| 5176 | } |
| 5177 | |
Christoph Hellwig | a9185b4 | 2010-03-05 09:21:37 +0100 | [diff] [blame] | 5178 | int btrfs_write_inode(struct inode *inode, struct writeback_control *wbc) |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5179 | { |
| 5180 | struct btrfs_root *root = BTRFS_I(inode)->root; |
| 5181 | struct btrfs_trans_handle *trans; |
| 5182 | int ret = 0; |
Josef Bacik | 0af3d00 | 2010-06-21 14:48:16 -0400 | [diff] [blame] | 5183 | bool nolock = false; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5184 | |
Josef Bacik | 72ac3c0 | 2012-05-23 14:13:11 -0400 | [diff] [blame] | 5185 | if (test_bit(BTRFS_INODE_DUMMY, &BTRFS_I(inode)->runtime_flags)) |
Chris Mason | 4ca8b41 | 2008-08-05 13:30:48 -0400 | [diff] [blame] | 5186 | return 0; |
| 5187 | |
Liu Bo | 83eea1f | 2012-07-10 05:28:39 -0600 | [diff] [blame] | 5188 | 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] | 5189 | nolock = true; |
Josef Bacik | 0af3d00 | 2010-06-21 14:48:16 -0400 | [diff] [blame] | 5190 | |
Christoph Hellwig | a9185b4 | 2010-03-05 09:21:37 +0100 | [diff] [blame] | 5191 | if (wbc->sync_mode == WB_SYNC_ALL) { |
Josef Bacik | 0af3d00 | 2010-06-21 14:48:16 -0400 | [diff] [blame] | 5192 | if (nolock) |
Josef Bacik | 7a7eaa4 | 2011-04-13 12:54:33 -0400 | [diff] [blame] | 5193 | trans = btrfs_join_transaction_nolock(root); |
Josef Bacik | 0af3d00 | 2010-06-21 14:48:16 -0400 | [diff] [blame] | 5194 | else |
Josef Bacik | 7a7eaa4 | 2011-04-13 12:54:33 -0400 | [diff] [blame] | 5195 | trans = btrfs_join_transaction(root); |
Tsutomu Itoh | 3612b49 | 2011-01-25 02:51:38 +0000 | [diff] [blame] | 5196 | if (IS_ERR(trans)) |
| 5197 | return PTR_ERR(trans); |
Miao Xie | a698d075 | 2012-09-20 01:51:59 -0600 | [diff] [blame] | 5198 | ret = btrfs_commit_transaction(trans, root); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5199 | } |
| 5200 | return ret; |
| 5201 | } |
| 5202 | |
| 5203 | /* |
Chris Mason | 54aa1f4 | 2007-06-22 14:16:25 -0400 | [diff] [blame] | 5204 | * This is somewhat expensive, updating the tree every time the |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5205 | * inode changes. But, it is most likely to find the inode in cache. |
| 5206 | * FIXME, needs more benchmarking...there are no reasons other than performance |
| 5207 | * to keep or drop this code. |
| 5208 | */ |
Eric Sandeen | 48a3b63 | 2013-04-25 20:41:01 +0000 | [diff] [blame] | 5209 | static int btrfs_dirty_inode(struct inode *inode) |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5210 | { |
| 5211 | struct btrfs_root *root = BTRFS_I(inode)->root; |
| 5212 | struct btrfs_trans_handle *trans; |
Yan, Zheng | 8929ecfa | 2010-05-16 10:49:58 -0400 | [diff] [blame] | 5213 | int ret; |
| 5214 | |
Josef Bacik | 72ac3c0 | 2012-05-23 14:13:11 -0400 | [diff] [blame] | 5215 | if (test_bit(BTRFS_INODE_DUMMY, &BTRFS_I(inode)->runtime_flags)) |
Josef Bacik | 22c44fe | 2011-11-30 10:45:38 -0500 | [diff] [blame] | 5216 | return 0; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5217 | |
Josef Bacik | 7a7eaa4 | 2011-04-13 12:54:33 -0400 | [diff] [blame] | 5218 | trans = btrfs_join_transaction(root); |
Josef Bacik | 22c44fe | 2011-11-30 10:45:38 -0500 | [diff] [blame] | 5219 | if (IS_ERR(trans)) |
| 5220 | return PTR_ERR(trans); |
Yan, Zheng | 8929ecfa | 2010-05-16 10:49:58 -0400 | [diff] [blame] | 5221 | |
| 5222 | ret = btrfs_update_inode(trans, root, inode); |
Chris Mason | 94b6044 | 2010-05-26 11:02:00 -0400 | [diff] [blame] | 5223 | if (ret && ret == -ENOSPC) { |
| 5224 | /* whoops, lets try again with the full transaction */ |
| 5225 | btrfs_end_transaction(trans, root); |
| 5226 | trans = btrfs_start_transaction(root, 1); |
Josef Bacik | 22c44fe | 2011-11-30 10:45:38 -0500 | [diff] [blame] | 5227 | if (IS_ERR(trans)) |
| 5228 | return PTR_ERR(trans); |
Yan, Zheng | 8929ecfa | 2010-05-16 10:49:58 -0400 | [diff] [blame] | 5229 | |
Chris Mason | 94b6044 | 2010-05-26 11:02:00 -0400 | [diff] [blame] | 5230 | ret = btrfs_update_inode(trans, root, inode); |
Chris Mason | 94b6044 | 2010-05-26 11:02:00 -0400 | [diff] [blame] | 5231 | } |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5232 | btrfs_end_transaction(trans, root); |
Miao Xie | 16cdcec | 2011-04-22 18:12:22 +0800 | [diff] [blame] | 5233 | if (BTRFS_I(inode)->delayed_node) |
| 5234 | btrfs_balance_delayed_items(root); |
Josef Bacik | 22c44fe | 2011-11-30 10:45:38 -0500 | [diff] [blame] | 5235 | |
| 5236 | return ret; |
| 5237 | } |
| 5238 | |
| 5239 | /* |
| 5240 | * This is a copy of file_update_time. We need this so we can return error on |
| 5241 | * ENOSPC for updating the inode in the case of file write and mmap writes. |
| 5242 | */ |
Josef Bacik | e41f941 | 2012-03-26 09:46:47 -0400 | [diff] [blame] | 5243 | static int btrfs_update_time(struct inode *inode, struct timespec *now, |
| 5244 | int flags) |
Josef Bacik | 22c44fe | 2011-11-30 10:45:38 -0500 | [diff] [blame] | 5245 | { |
Alexander Block | 2bc556528 | 2012-06-15 09:49:33 +0200 | [diff] [blame] | 5246 | struct btrfs_root *root = BTRFS_I(inode)->root; |
| 5247 | |
| 5248 | if (btrfs_root_readonly(root)) |
| 5249 | return -EROFS; |
| 5250 | |
Josef Bacik | e41f941 | 2012-03-26 09:46:47 -0400 | [diff] [blame] | 5251 | if (flags & S_VERSION) |
Josef Bacik | 22c44fe | 2011-11-30 10:45:38 -0500 | [diff] [blame] | 5252 | inode_inc_iversion(inode); |
Josef Bacik | e41f941 | 2012-03-26 09:46:47 -0400 | [diff] [blame] | 5253 | if (flags & S_CTIME) |
| 5254 | inode->i_ctime = *now; |
| 5255 | if (flags & S_MTIME) |
| 5256 | inode->i_mtime = *now; |
| 5257 | if (flags & S_ATIME) |
| 5258 | inode->i_atime = *now; |
| 5259 | return btrfs_dirty_inode(inode); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5260 | } |
| 5261 | |
Chris Mason | d352ac6 | 2008-09-29 15:18:18 -0400 | [diff] [blame] | 5262 | /* |
| 5263 | * find the highest existing sequence number in a directory |
| 5264 | * and then set the in-memory index_cnt variable to reflect |
| 5265 | * free sequence numbers |
| 5266 | */ |
Josef Bacik | aec7477 | 2008-07-24 12:12:38 -0400 | [diff] [blame] | 5267 | static int btrfs_set_inode_index_count(struct inode *inode) |
| 5268 | { |
| 5269 | struct btrfs_root *root = BTRFS_I(inode)->root; |
| 5270 | struct btrfs_key key, found_key; |
| 5271 | struct btrfs_path *path; |
| 5272 | struct extent_buffer *leaf; |
| 5273 | int ret; |
| 5274 | |
Li Zefan | 33345d01 | 2011-04-20 10:31:50 +0800 | [diff] [blame] | 5275 | key.objectid = btrfs_ino(inode); |
Josef Bacik | aec7477 | 2008-07-24 12:12:38 -0400 | [diff] [blame] | 5276 | btrfs_set_key_type(&key, BTRFS_DIR_INDEX_KEY); |
| 5277 | key.offset = (u64)-1; |
| 5278 | |
| 5279 | path = btrfs_alloc_path(); |
| 5280 | if (!path) |
| 5281 | return -ENOMEM; |
| 5282 | |
| 5283 | ret = btrfs_search_slot(NULL, root, &key, path, 0, 0); |
| 5284 | if (ret < 0) |
| 5285 | goto out; |
| 5286 | /* FIXME: we should be able to handle this */ |
| 5287 | if (ret == 0) |
| 5288 | goto out; |
| 5289 | ret = 0; |
| 5290 | |
| 5291 | /* |
| 5292 | * MAGIC NUMBER EXPLANATION: |
| 5293 | * since we search a directory based on f_pos we have to start at 2 |
| 5294 | * since '.' and '..' have f_pos of 0 and 1 respectively, so everybody |
| 5295 | * else has to start at 2 |
| 5296 | */ |
| 5297 | if (path->slots[0] == 0) { |
| 5298 | BTRFS_I(inode)->index_cnt = 2; |
| 5299 | goto out; |
| 5300 | } |
| 5301 | |
| 5302 | path->slots[0]--; |
| 5303 | |
| 5304 | leaf = path->nodes[0]; |
| 5305 | btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]); |
| 5306 | |
Li Zefan | 33345d01 | 2011-04-20 10:31:50 +0800 | [diff] [blame] | 5307 | if (found_key.objectid != btrfs_ino(inode) || |
Josef Bacik | aec7477 | 2008-07-24 12:12:38 -0400 | [diff] [blame] | 5308 | btrfs_key_type(&found_key) != BTRFS_DIR_INDEX_KEY) { |
| 5309 | BTRFS_I(inode)->index_cnt = 2; |
| 5310 | goto out; |
| 5311 | } |
| 5312 | |
| 5313 | BTRFS_I(inode)->index_cnt = found_key.offset + 1; |
| 5314 | out: |
| 5315 | btrfs_free_path(path); |
| 5316 | return ret; |
| 5317 | } |
| 5318 | |
Chris Mason | d352ac6 | 2008-09-29 15:18:18 -0400 | [diff] [blame] | 5319 | /* |
| 5320 | * helper to find a free sequence number in a given directory. This current |
| 5321 | * code is very simple, later versions will do smarter things in the btree |
| 5322 | */ |
Chris Mason | 3de4586 | 2008-11-17 21:02:50 -0500 | [diff] [blame] | 5323 | int btrfs_set_inode_index(struct inode *dir, u64 *index) |
Josef Bacik | aec7477 | 2008-07-24 12:12:38 -0400 | [diff] [blame] | 5324 | { |
| 5325 | int ret = 0; |
| 5326 | |
| 5327 | if (BTRFS_I(dir)->index_cnt == (u64)-1) { |
Miao Xie | 16cdcec | 2011-04-22 18:12:22 +0800 | [diff] [blame] | 5328 | ret = btrfs_inode_delayed_dir_index_count(dir); |
| 5329 | if (ret) { |
| 5330 | ret = btrfs_set_inode_index_count(dir); |
| 5331 | if (ret) |
| 5332 | return ret; |
| 5333 | } |
Josef Bacik | aec7477 | 2008-07-24 12:12:38 -0400 | [diff] [blame] | 5334 | } |
| 5335 | |
Chris Mason | 00e4e6b | 2008-08-05 11:18:09 -0400 | [diff] [blame] | 5336 | *index = BTRFS_I(dir)->index_cnt; |
Josef Bacik | aec7477 | 2008-07-24 12:12:38 -0400 | [diff] [blame] | 5337 | BTRFS_I(dir)->index_cnt++; |
| 5338 | |
| 5339 | return ret; |
| 5340 | } |
| 5341 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5342 | static struct inode *btrfs_new_inode(struct btrfs_trans_handle *trans, |
| 5343 | struct btrfs_root *root, |
Josef Bacik | aec7477 | 2008-07-24 12:12:38 -0400 | [diff] [blame] | 5344 | struct inode *dir, |
Chris Mason | 9c58309 | 2008-01-29 15:15:18 -0500 | [diff] [blame] | 5345 | const char *name, int name_len, |
Al Viro | 175a4eb | 2011-07-26 03:30:54 -0400 | [diff] [blame] | 5346 | u64 ref_objectid, u64 objectid, |
| 5347 | umode_t mode, u64 *index) |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5348 | { |
| 5349 | struct inode *inode; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 5350 | struct btrfs_inode_item *inode_item; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5351 | struct btrfs_key *location; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 5352 | struct btrfs_path *path; |
Chris Mason | 9c58309 | 2008-01-29 15:15:18 -0500 | [diff] [blame] | 5353 | struct btrfs_inode_ref *ref; |
| 5354 | struct btrfs_key key[2]; |
| 5355 | u32 sizes[2]; |
| 5356 | unsigned long ptr; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5357 | int ret; |
| 5358 | int owner; |
| 5359 | |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 5360 | path = btrfs_alloc_path(); |
Mark Fasheh | d8926bb | 2011-07-13 10:38:47 -0700 | [diff] [blame] | 5361 | if (!path) |
| 5362 | return ERR_PTR(-ENOMEM); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 5363 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5364 | inode = new_inode(root->fs_info->sb); |
Yoshinori Sano | 8fb2764 | 2011-04-09 02:30:07 +0000 | [diff] [blame] | 5365 | if (!inode) { |
| 5366 | btrfs_free_path(path); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5367 | return ERR_PTR(-ENOMEM); |
Yoshinori Sano | 8fb2764 | 2011-04-09 02:30:07 +0000 | [diff] [blame] | 5368 | } |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5369 | |
Li Zefan | 581bb05 | 2011-04-20 10:06:11 +0800 | [diff] [blame] | 5370 | /* |
| 5371 | * we have to initialize this early, so we can reclaim the inode |
| 5372 | * number if we fail afterwards in this function. |
| 5373 | */ |
| 5374 | inode->i_ino = objectid; |
| 5375 | |
Josef Bacik | aec7477 | 2008-07-24 12:12:38 -0400 | [diff] [blame] | 5376 | if (dir) { |
liubo | 1abe9b8 | 2011-03-24 11:18:59 +0000 | [diff] [blame] | 5377 | trace_btrfs_inode_request(dir); |
| 5378 | |
Chris Mason | 3de4586 | 2008-11-17 21:02:50 -0500 | [diff] [blame] | 5379 | ret = btrfs_set_inode_index(dir, index); |
Shen Feng | 0977143 | 2009-04-02 16:46:06 -0400 | [diff] [blame] | 5380 | if (ret) { |
Yoshinori Sano | 8fb2764 | 2011-04-09 02:30:07 +0000 | [diff] [blame] | 5381 | btrfs_free_path(path); |
Shen Feng | 0977143 | 2009-04-02 16:46:06 -0400 | [diff] [blame] | 5382 | iput(inode); |
Josef Bacik | aec7477 | 2008-07-24 12:12:38 -0400 | [diff] [blame] | 5383 | return ERR_PTR(ret); |
Shen Feng | 0977143 | 2009-04-02 16:46:06 -0400 | [diff] [blame] | 5384 | } |
Josef Bacik | aec7477 | 2008-07-24 12:12:38 -0400 | [diff] [blame] | 5385 | } |
| 5386 | /* |
| 5387 | * index_cnt is ignored for everything but a dir, |
| 5388 | * btrfs_get_inode_index_count has an explanation for the magic |
| 5389 | * number |
| 5390 | */ |
| 5391 | BTRFS_I(inode)->index_cnt = 2; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5392 | BTRFS_I(inode)->root = root; |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 5393 | BTRFS_I(inode)->generation = trans->transid; |
Josef Bacik | 7619585 | 2010-11-19 02:18:02 +0000 | [diff] [blame] | 5394 | inode->i_generation = BTRFS_I(inode)->generation; |
Chris Mason | b888db2 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 5395 | |
Josef Bacik | 5dc562c | 2012-08-17 13:14:17 -0400 | [diff] [blame] | 5396 | /* |
| 5397 | * We could have gotten an inode number from somebody who was fsynced |
| 5398 | * and then removed in this same transaction, so let's just set full |
| 5399 | * sync since it will be a full sync anyway and this will blow away the |
| 5400 | * old info in the log. |
| 5401 | */ |
| 5402 | set_bit(BTRFS_INODE_NEEDS_FULL_SYNC, &BTRFS_I(inode)->runtime_flags); |
| 5403 | |
Al Viro | 569254b | 2011-07-24 17:08:40 -0400 | [diff] [blame] | 5404 | if (S_ISDIR(mode)) |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5405 | owner = 0; |
| 5406 | else |
| 5407 | owner = 1; |
Chris Mason | 9c58309 | 2008-01-29 15:15:18 -0500 | [diff] [blame] | 5408 | |
| 5409 | key[0].objectid = objectid; |
| 5410 | btrfs_set_key_type(&key[0], BTRFS_INODE_ITEM_KEY); |
| 5411 | key[0].offset = 0; |
| 5412 | |
Mark Fasheh | f186373 | 2012-08-08 11:32:27 -0700 | [diff] [blame] | 5413 | /* |
| 5414 | * Start new inodes with an inode_ref. This is slightly more |
| 5415 | * efficient for small numbers of hard links since they will |
| 5416 | * be packed into one item. Extended refs will kick in if we |
| 5417 | * add more hard links than can fit in the ref item. |
| 5418 | */ |
Chris Mason | 9c58309 | 2008-01-29 15:15:18 -0500 | [diff] [blame] | 5419 | key[1].objectid = objectid; |
| 5420 | btrfs_set_key_type(&key[1], BTRFS_INODE_REF_KEY); |
| 5421 | key[1].offset = ref_objectid; |
| 5422 | |
| 5423 | sizes[0] = sizeof(struct btrfs_inode_item); |
| 5424 | sizes[1] = name_len + sizeof(*ref); |
| 5425 | |
Chris Mason | b947343 | 2009-03-13 11:00:37 -0400 | [diff] [blame] | 5426 | path->leave_spinning = 1; |
Chris Mason | 9c58309 | 2008-01-29 15:15:18 -0500 | [diff] [blame] | 5427 | ret = btrfs_insert_empty_items(trans, root, path, key, sizes, 2); |
| 5428 | if (ret != 0) |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 5429 | goto fail; |
| 5430 | |
Dmitry Monakhov | ecc11fab | 2010-03-04 17:31:47 +0300 | [diff] [blame] | 5431 | inode_init_owner(inode, dir, mode); |
Yan Zheng | a76a3cd | 2008-10-09 11:46:29 -0400 | [diff] [blame] | 5432 | inode_set_bytes(inode, 0); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5433 | inode->i_mtime = inode->i_atime = inode->i_ctime = CURRENT_TIME; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 5434 | inode_item = btrfs_item_ptr(path->nodes[0], path->slots[0], |
| 5435 | struct btrfs_inode_item); |
Li Zefan | 293f7e0 | 2012-07-10 00:58:58 -0600 | [diff] [blame] | 5436 | memset_extent_buffer(path->nodes[0], 0, (unsigned long)inode_item, |
| 5437 | sizeof(*inode_item)); |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 5438 | fill_inode_item(trans, path->nodes[0], inode_item, inode); |
Chris Mason | 9c58309 | 2008-01-29 15:15:18 -0500 | [diff] [blame] | 5439 | |
| 5440 | ref = btrfs_item_ptr(path->nodes[0], path->slots[0] + 1, |
| 5441 | struct btrfs_inode_ref); |
| 5442 | btrfs_set_inode_ref_name_len(path->nodes[0], ref, name_len); |
Chris Mason | 00e4e6b | 2008-08-05 11:18:09 -0400 | [diff] [blame] | 5443 | btrfs_set_inode_ref_index(path->nodes[0], ref, *index); |
Chris Mason | 9c58309 | 2008-01-29 15:15:18 -0500 | [diff] [blame] | 5444 | ptr = (unsigned long)(ref + 1); |
| 5445 | write_extent_buffer(path->nodes[0], name, ptr, name_len); |
| 5446 | |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 5447 | btrfs_mark_buffer_dirty(path->nodes[0]); |
| 5448 | btrfs_free_path(path); |
| 5449 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5450 | location = &BTRFS_I(inode)->location; |
| 5451 | location->objectid = objectid; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5452 | location->offset = 0; |
| 5453 | btrfs_set_key_type(location, BTRFS_INODE_ITEM_KEY); |
| 5454 | |
Christoph Hellwig | 6cbff00 | 2009-04-17 10:37:41 +0200 | [diff] [blame] | 5455 | btrfs_inherit_iflags(inode, dir); |
| 5456 | |
Al Viro | 569254b | 2011-07-24 17:08:40 -0400 | [diff] [blame] | 5457 | if (S_ISREG(mode)) { |
Chris Mason | 9427216 | 2009-07-02 12:26:06 -0400 | [diff] [blame] | 5458 | if (btrfs_test_opt(root, NODATASUM)) |
| 5459 | BTRFS_I(inode)->flags |= BTRFS_INODE_NODATASUM; |
Liu Bo | 213490b | 2012-09-11 08:33:50 -0600 | [diff] [blame] | 5460 | if (btrfs_test_opt(root, NODATACOW)) |
Josef Bacik | f2bdf9a | 2013-02-21 15:28:28 -0500 | [diff] [blame] | 5461 | BTRFS_I(inode)->flags |= BTRFS_INODE_NODATACOW | |
| 5462 | BTRFS_INODE_NODATASUM; |
Chris Mason | 9427216 | 2009-07-02 12:26:06 -0400 | [diff] [blame] | 5463 | } |
| 5464 | |
Filipe David Borba Manana | 778ba82 | 2013-10-06 22:22:33 +0100 | [diff] [blame] | 5465 | btrfs_insert_inode_hash(inode); |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 5466 | inode_tree_add(inode); |
liubo | 1abe9b8 | 2011-03-24 11:18:59 +0000 | [diff] [blame] | 5467 | |
| 5468 | trace_btrfs_inode_new(inode); |
Chris Mason | 1973f0f | 2011-06-24 13:13:29 -0400 | [diff] [blame] | 5469 | btrfs_set_inode_last_trans(trans, inode); |
liubo | 1abe9b8 | 2011-03-24 11:18:59 +0000 | [diff] [blame] | 5470 | |
Alexander Block | 8ea05e3 | 2012-07-25 17:35:53 +0200 | [diff] [blame] | 5471 | btrfs_update_root_times(trans, root); |
| 5472 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5473 | return inode; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 5474 | fail: |
Josef Bacik | aec7477 | 2008-07-24 12:12:38 -0400 | [diff] [blame] | 5475 | if (dir) |
| 5476 | BTRFS_I(dir)->index_cnt--; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 5477 | btrfs_free_path(path); |
Shen Feng | 0977143 | 2009-04-02 16:46:06 -0400 | [diff] [blame] | 5478 | iput(inode); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 5479 | return ERR_PTR(ret); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5480 | } |
| 5481 | |
| 5482 | static inline u8 btrfs_inode_type(struct inode *inode) |
| 5483 | { |
| 5484 | return btrfs_type_by_mode[(inode->i_mode & S_IFMT) >> S_SHIFT]; |
| 5485 | } |
| 5486 | |
Chris Mason | d352ac6 | 2008-09-29 15:18:18 -0400 | [diff] [blame] | 5487 | /* |
| 5488 | * utility function to add 'inode' into 'parent_inode' with |
| 5489 | * a give name and a given sequence number. |
| 5490 | * if 'add_backref' is true, also insert a backref from the |
| 5491 | * inode to the parent directory. |
| 5492 | */ |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 5493 | int btrfs_add_link(struct btrfs_trans_handle *trans, |
| 5494 | struct inode *parent_inode, struct inode *inode, |
| 5495 | const char *name, int name_len, int add_backref, u64 index) |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5496 | { |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 5497 | int ret = 0; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5498 | struct btrfs_key key; |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 5499 | struct btrfs_root *root = BTRFS_I(parent_inode)->root; |
Li Zefan | 33345d01 | 2011-04-20 10:31:50 +0800 | [diff] [blame] | 5500 | u64 ino = btrfs_ino(inode); |
| 5501 | u64 parent_ino = btrfs_ino(parent_inode); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 5502 | |
Li Zefan | 33345d01 | 2011-04-20 10:31:50 +0800 | [diff] [blame] | 5503 | if (unlikely(ino == BTRFS_FIRST_FREE_OBJECTID)) { |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 5504 | memcpy(&key, &BTRFS_I(inode)->root->root_key, sizeof(key)); |
| 5505 | } else { |
Li Zefan | 33345d01 | 2011-04-20 10:31:50 +0800 | [diff] [blame] | 5506 | key.objectid = ino; |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 5507 | btrfs_set_key_type(&key, BTRFS_INODE_ITEM_KEY); |
| 5508 | key.offset = 0; |
| 5509 | } |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5510 | |
Li Zefan | 33345d01 | 2011-04-20 10:31:50 +0800 | [diff] [blame] | 5511 | if (unlikely(ino == BTRFS_FIRST_FREE_OBJECTID)) { |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 5512 | ret = btrfs_add_root_ref(trans, root->fs_info->tree_root, |
| 5513 | key.objectid, root->root_key.objectid, |
Li Zefan | 33345d01 | 2011-04-20 10:31:50 +0800 | [diff] [blame] | 5514 | parent_ino, index, name, name_len); |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 5515 | } else if (add_backref) { |
Li Zefan | 33345d01 | 2011-04-20 10:31:50 +0800 | [diff] [blame] | 5516 | ret = btrfs_insert_inode_ref(trans, root, name, name_len, ino, |
| 5517 | parent_ino, index); |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 5518 | } |
| 5519 | |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 5520 | /* Nothing to clean up yet */ |
| 5521 | if (ret) |
| 5522 | return ret; |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 5523 | |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 5524 | ret = btrfs_insert_dir_item(trans, root, name, name_len, |
| 5525 | parent_inode, &key, |
| 5526 | btrfs_inode_type(inode), index); |
Chris Mason | 9c52057 | 2012-12-17 14:26:57 -0500 | [diff] [blame] | 5527 | if (ret == -EEXIST || ret == -EOVERFLOW) |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 5528 | goto fail_dir_item; |
| 5529 | else if (ret) { |
| 5530 | btrfs_abort_transaction(trans, root, ret); |
| 5531 | return ret; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5532 | } |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 5533 | |
| 5534 | btrfs_i_size_write(parent_inode, parent_inode->i_size + |
| 5535 | name_len * 2); |
Josef Bacik | 0c4d2d9 | 2012-04-05 15:03:02 -0400 | [diff] [blame] | 5536 | inode_inc_iversion(parent_inode); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 5537 | parent_inode->i_mtime = parent_inode->i_ctime = CURRENT_TIME; |
| 5538 | ret = btrfs_update_inode(trans, root, parent_inode); |
| 5539 | if (ret) |
| 5540 | btrfs_abort_transaction(trans, root, ret); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5541 | return ret; |
Chris Mason | fe66a05 | 2012-02-20 08:40:56 -0500 | [diff] [blame] | 5542 | |
| 5543 | fail_dir_item: |
| 5544 | if (unlikely(ino == BTRFS_FIRST_FREE_OBJECTID)) { |
| 5545 | u64 local_index; |
| 5546 | int err; |
| 5547 | err = btrfs_del_root_ref(trans, root->fs_info->tree_root, |
| 5548 | key.objectid, root->root_key.objectid, |
| 5549 | parent_ino, &local_index, name, name_len); |
| 5550 | |
| 5551 | } else if (add_backref) { |
| 5552 | u64 local_index; |
| 5553 | int err; |
| 5554 | |
| 5555 | err = btrfs_del_inode_ref(trans, root, name, name_len, |
| 5556 | ino, parent_ino, &local_index); |
| 5557 | } |
| 5558 | return ret; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5559 | } |
| 5560 | |
| 5561 | static int btrfs_add_nondir(struct btrfs_trans_handle *trans, |
Josef Bacik | a1b075d | 2010-11-19 20:36:11 +0000 | [diff] [blame] | 5562 | struct inode *dir, struct dentry *dentry, |
| 5563 | struct inode *inode, int backref, u64 index) |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5564 | { |
Josef Bacik | a1b075d | 2010-11-19 20:36:11 +0000 | [diff] [blame] | 5565 | int err = btrfs_add_link(trans, dir, inode, |
| 5566 | dentry->d_name.name, dentry->d_name.len, |
| 5567 | backref, index); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5568 | if (err > 0) |
| 5569 | err = -EEXIST; |
| 5570 | return err; |
| 5571 | } |
| 5572 | |
Josef Bacik | 618e21d | 2007-07-11 10:18:17 -0400 | [diff] [blame] | 5573 | static int btrfs_mknod(struct inode *dir, struct dentry *dentry, |
Al Viro | 1a67aaf | 2011-07-26 01:52:52 -0400 | [diff] [blame] | 5574 | umode_t mode, dev_t rdev) |
Josef Bacik | 618e21d | 2007-07-11 10:18:17 -0400 | [diff] [blame] | 5575 | { |
| 5576 | struct btrfs_trans_handle *trans; |
| 5577 | struct btrfs_root *root = BTRFS_I(dir)->root; |
Chris Mason | 1832a6d | 2007-12-21 16:27:21 -0500 | [diff] [blame] | 5578 | struct inode *inode = NULL; |
Josef Bacik | 618e21d | 2007-07-11 10:18:17 -0400 | [diff] [blame] | 5579 | int err; |
| 5580 | int drop_inode = 0; |
| 5581 | u64 objectid; |
Chris Mason | 00e4e6b | 2008-08-05 11:18:09 -0400 | [diff] [blame] | 5582 | u64 index = 0; |
Josef Bacik | 618e21d | 2007-07-11 10:18:17 -0400 | [diff] [blame] | 5583 | |
| 5584 | if (!new_valid_dev(rdev)) |
| 5585 | return -EINVAL; |
| 5586 | |
Josef Bacik | 9ed74f2 | 2009-09-11 16:12:44 -0400 | [diff] [blame] | 5587 | /* |
| 5588 | * 2 for inode item and ref |
| 5589 | * 2 for dir items |
| 5590 | * 1 for xattr if selinux is on |
| 5591 | */ |
Yan, Zheng | a22285a | 2010-05-16 10:48:46 -0400 | [diff] [blame] | 5592 | trans = btrfs_start_transaction(root, 5); |
| 5593 | if (IS_ERR(trans)) |
| 5594 | return PTR_ERR(trans); |
Chris Mason | 1832a6d | 2007-12-21 16:27:21 -0500 | [diff] [blame] | 5595 | |
Li Zefan | 581bb05 | 2011-04-20 10:06:11 +0800 | [diff] [blame] | 5596 | err = btrfs_find_free_ino(root, &objectid); |
| 5597 | if (err) |
| 5598 | goto out_unlock; |
| 5599 | |
Josef Bacik | aec7477 | 2008-07-24 12:12:38 -0400 | [diff] [blame] | 5600 | inode = btrfs_new_inode(trans, root, dir, dentry->d_name.name, |
Li Zefan | 33345d01 | 2011-04-20 10:31:50 +0800 | [diff] [blame] | 5601 | dentry->d_name.len, btrfs_ino(dir), objectid, |
Josef Bacik | d82a6f1d | 2011-05-11 15:26:06 -0400 | [diff] [blame] | 5602 | mode, &index); |
Tsutomu Itoh | 7cf96da | 2011-04-25 19:43:53 -0400 | [diff] [blame] | 5603 | if (IS_ERR(inode)) { |
| 5604 | err = PTR_ERR(inode); |
Josef Bacik | 618e21d | 2007-07-11 10:18:17 -0400 | [diff] [blame] | 5605 | goto out_unlock; |
Tsutomu Itoh | 7cf96da | 2011-04-25 19:43:53 -0400 | [diff] [blame] | 5606 | } |
Josef Bacik | 618e21d | 2007-07-11 10:18:17 -0400 | [diff] [blame] | 5607 | |
Eric Paris | 2a7dba3 | 2011-02-01 11:05:39 -0500 | [diff] [blame] | 5608 | err = btrfs_init_inode_security(trans, inode, dir, &dentry->d_name); |
Josef Bacik | 33268ea | 2008-07-24 12:16:36 -0400 | [diff] [blame] | 5609 | if (err) { |
| 5610 | drop_inode = 1; |
| 5611 | goto out_unlock; |
| 5612 | } |
| 5613 | |
Casey Schaufler | ad19db7 | 2011-12-15 10:09:07 -0500 | [diff] [blame] | 5614 | /* |
| 5615 | * If the active LSM wants to access the inode during |
| 5616 | * d_instantiate it needs these. Smack checks to see |
| 5617 | * if the filesystem supports xattrs by looking at the |
| 5618 | * ops vector. |
| 5619 | */ |
| 5620 | |
| 5621 | inode->i_op = &btrfs_special_inode_operations; |
Josef Bacik | a1b075d | 2010-11-19 20:36:11 +0000 | [diff] [blame] | 5622 | err = btrfs_add_nondir(trans, dir, dentry, inode, 0, index); |
Josef Bacik | 618e21d | 2007-07-11 10:18:17 -0400 | [diff] [blame] | 5623 | if (err) |
| 5624 | drop_inode = 1; |
| 5625 | else { |
Josef Bacik | 618e21d | 2007-07-11 10:18:17 -0400 | [diff] [blame] | 5626 | init_special_inode(inode, inode->i_mode, rdev); |
Yan | 1b4ab1b | 2007-08-29 09:11:44 -0400 | [diff] [blame] | 5627 | btrfs_update_inode(trans, root, inode); |
Al Viro | 08c422c | 2011-12-23 07:58:13 -0500 | [diff] [blame] | 5628 | d_instantiate(dentry, inode); |
Josef Bacik | 618e21d | 2007-07-11 10:18:17 -0400 | [diff] [blame] | 5629 | } |
Josef Bacik | 618e21d | 2007-07-11 10:18:17 -0400 | [diff] [blame] | 5630 | out_unlock: |
Josef Bacik | 7ad85bb | 2012-01-12 19:10:12 -0500 | [diff] [blame] | 5631 | btrfs_end_transaction(trans, root); |
Liu Bo | b53d3f5 | 2012-11-14 14:34:34 +0000 | [diff] [blame] | 5632 | btrfs_btree_balance_dirty(root); |
Josef Bacik | 618e21d | 2007-07-11 10:18:17 -0400 | [diff] [blame] | 5633 | if (drop_inode) { |
| 5634 | inode_dec_link_count(inode); |
| 5635 | iput(inode); |
| 5636 | } |
Josef Bacik | 618e21d | 2007-07-11 10:18:17 -0400 | [diff] [blame] | 5637 | return err; |
| 5638 | } |
| 5639 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5640 | static int btrfs_create(struct inode *dir, struct dentry *dentry, |
Al Viro | ebfc3b4 | 2012-06-10 18:05:36 -0400 | [diff] [blame] | 5641 | umode_t mode, bool excl) |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5642 | { |
| 5643 | struct btrfs_trans_handle *trans; |
| 5644 | struct btrfs_root *root = BTRFS_I(dir)->root; |
Chris Mason | 1832a6d | 2007-12-21 16:27:21 -0500 | [diff] [blame] | 5645 | struct inode *inode = NULL; |
Filipe Brandenburger | 43baa57 | 2012-11-30 03:40:09 +0000 | [diff] [blame] | 5646 | int drop_inode_on_err = 0; |
Yan, Zheng | a22285a | 2010-05-16 10:48:46 -0400 | [diff] [blame] | 5647 | int err; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5648 | u64 objectid; |
Chris Mason | 00e4e6b | 2008-08-05 11:18:09 -0400 | [diff] [blame] | 5649 | u64 index = 0; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5650 | |
Josef Bacik | 9ed74f2 | 2009-09-11 16:12:44 -0400 | [diff] [blame] | 5651 | /* |
| 5652 | * 2 for inode item and ref |
| 5653 | * 2 for dir items |
| 5654 | * 1 for xattr if selinux is on |
| 5655 | */ |
Yan, Zheng | a22285a | 2010-05-16 10:48:46 -0400 | [diff] [blame] | 5656 | trans = btrfs_start_transaction(root, 5); |
| 5657 | if (IS_ERR(trans)) |
| 5658 | return PTR_ERR(trans); |
Josef Bacik | 9ed74f2 | 2009-09-11 16:12:44 -0400 | [diff] [blame] | 5659 | |
Li Zefan | 581bb05 | 2011-04-20 10:06:11 +0800 | [diff] [blame] | 5660 | err = btrfs_find_free_ino(root, &objectid); |
| 5661 | if (err) |
| 5662 | goto out_unlock; |
| 5663 | |
Josef Bacik | aec7477 | 2008-07-24 12:12:38 -0400 | [diff] [blame] | 5664 | inode = btrfs_new_inode(trans, root, dir, dentry->d_name.name, |
Li Zefan | 33345d01 | 2011-04-20 10:31:50 +0800 | [diff] [blame] | 5665 | dentry->d_name.len, btrfs_ino(dir), objectid, |
Josef Bacik | d82a6f1d | 2011-05-11 15:26:06 -0400 | [diff] [blame] | 5666 | mode, &index); |
Tsutomu Itoh | 7cf96da | 2011-04-25 19:43:53 -0400 | [diff] [blame] | 5667 | if (IS_ERR(inode)) { |
| 5668 | err = PTR_ERR(inode); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5669 | goto out_unlock; |
Tsutomu Itoh | 7cf96da | 2011-04-25 19:43:53 -0400 | [diff] [blame] | 5670 | } |
Filipe Brandenburger | 43baa57 | 2012-11-30 03:40:09 +0000 | [diff] [blame] | 5671 | drop_inode_on_err = 1; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5672 | |
Eric Paris | 2a7dba3 | 2011-02-01 11:05:39 -0500 | [diff] [blame] | 5673 | err = btrfs_init_inode_security(trans, inode, dir, &dentry->d_name); |
Filipe Brandenburger | 43baa57 | 2012-11-30 03:40:09 +0000 | [diff] [blame] | 5674 | if (err) |
Josef Bacik | 33268ea | 2008-07-24 12:16:36 -0400 | [diff] [blame] | 5675 | goto out_unlock; |
Josef Bacik | 33268ea | 2008-07-24 12:16:36 -0400 | [diff] [blame] | 5676 | |
Filipe Brandenburger | 9185aa5 | 2012-11-30 03:40:08 +0000 | [diff] [blame] | 5677 | err = btrfs_update_inode(trans, root, inode); |
| 5678 | if (err) |
| 5679 | goto out_unlock; |
| 5680 | |
Casey Schaufler | ad19db7 | 2011-12-15 10:09:07 -0500 | [diff] [blame] | 5681 | /* |
| 5682 | * If the active LSM wants to access the inode during |
| 5683 | * d_instantiate it needs these. Smack checks to see |
| 5684 | * if the filesystem supports xattrs by looking at the |
| 5685 | * ops vector. |
| 5686 | */ |
| 5687 | inode->i_fop = &btrfs_file_operations; |
| 5688 | inode->i_op = &btrfs_file_inode_operations; |
| 5689 | |
Josef Bacik | a1b075d | 2010-11-19 20:36:11 +0000 | [diff] [blame] | 5690 | err = btrfs_add_nondir(trans, dir, dentry, inode, 0, index); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5691 | if (err) |
Filipe Brandenburger | 43baa57 | 2012-11-30 03:40:09 +0000 | [diff] [blame] | 5692 | goto out_unlock; |
| 5693 | |
| 5694 | inode->i_mapping->a_ops = &btrfs_aops; |
| 5695 | inode->i_mapping->backing_dev_info = &root->fs_info->bdi; |
| 5696 | BTRFS_I(inode)->io_tree.ops = &btrfs_extent_io_ops; |
| 5697 | d_instantiate(dentry, inode); |
| 5698 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5699 | out_unlock: |
Josef Bacik | 7ad85bb | 2012-01-12 19:10:12 -0500 | [diff] [blame] | 5700 | btrfs_end_transaction(trans, root); |
Filipe Brandenburger | 43baa57 | 2012-11-30 03:40:09 +0000 | [diff] [blame] | 5701 | if (err && drop_inode_on_err) { |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5702 | inode_dec_link_count(inode); |
| 5703 | iput(inode); |
| 5704 | } |
Liu Bo | b53d3f5 | 2012-11-14 14:34:34 +0000 | [diff] [blame] | 5705 | btrfs_btree_balance_dirty(root); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5706 | return err; |
| 5707 | } |
| 5708 | |
| 5709 | static int btrfs_link(struct dentry *old_dentry, struct inode *dir, |
| 5710 | struct dentry *dentry) |
| 5711 | { |
| 5712 | struct btrfs_trans_handle *trans; |
| 5713 | struct btrfs_root *root = BTRFS_I(dir)->root; |
| 5714 | struct inode *inode = old_dentry->d_inode; |
Chris Mason | 00e4e6b | 2008-08-05 11:18:09 -0400 | [diff] [blame] | 5715 | u64 index; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5716 | int err; |
| 5717 | int drop_inode = 0; |
| 5718 | |
TARUISI Hiroaki | 4a8be42 | 2009-11-12 07:14:26 +0000 | [diff] [blame] | 5719 | /* do not allow sys_link's with other subvols of the same device */ |
| 5720 | if (root->objectid != BTRFS_I(inode)->root->objectid) |
Mark Fasheh | 3ab3564 | 2011-03-22 17:20:26 +0000 | [diff] [blame] | 5721 | return -EXDEV; |
TARUISI Hiroaki | 4a8be42 | 2009-11-12 07:14:26 +0000 | [diff] [blame] | 5722 | |
Mark Fasheh | f186373 | 2012-08-08 11:32:27 -0700 | [diff] [blame] | 5723 | if (inode->i_nlink >= BTRFS_LINK_MAX) |
Al Viro | c055e99 | 2011-03-04 17:15:18 +0000 | [diff] [blame] | 5724 | return -EMLINK; |
Josef Bacik | 9ed74f2 | 2009-09-11 16:12:44 -0400 | [diff] [blame] | 5725 | |
Chris Mason | 3de4586 | 2008-11-17 21:02:50 -0500 | [diff] [blame] | 5726 | err = btrfs_set_inode_index(dir, &index); |
Josef Bacik | aec7477 | 2008-07-24 12:12:38 -0400 | [diff] [blame] | 5727 | if (err) |
| 5728 | goto fail; |
| 5729 | |
Yan, Zheng | a22285a | 2010-05-16 10:48:46 -0400 | [diff] [blame] | 5730 | /* |
Miao Xie | 7e6b646 | 2011-02-18 09:21:17 +0000 | [diff] [blame] | 5731 | * 2 items for inode and inode ref |
Yan, Zheng | a22285a | 2010-05-16 10:48:46 -0400 | [diff] [blame] | 5732 | * 2 items for dir items |
Miao Xie | 7e6b646 | 2011-02-18 09:21:17 +0000 | [diff] [blame] | 5733 | * 1 item for parent inode |
Yan, Zheng | a22285a | 2010-05-16 10:48:46 -0400 | [diff] [blame] | 5734 | */ |
Miao Xie | 7e6b646 | 2011-02-18 09:21:17 +0000 | [diff] [blame] | 5735 | trans = btrfs_start_transaction(root, 5); |
Yan, Zheng | a22285a | 2010-05-16 10:48:46 -0400 | [diff] [blame] | 5736 | if (IS_ERR(trans)) { |
| 5737 | err = PTR_ERR(trans); |
| 5738 | goto fail; |
| 5739 | } |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 5740 | |
Miao Xie | 3153495 | 2011-04-13 13:19:21 +0800 | [diff] [blame] | 5741 | btrfs_inc_nlink(inode); |
Josef Bacik | 0c4d2d9 | 2012-04-05 15:03:02 -0400 | [diff] [blame] | 5742 | inode_inc_iversion(inode); |
Miao Xie | 3153495 | 2011-04-13 13:19:21 +0800 | [diff] [blame] | 5743 | inode->i_ctime = CURRENT_TIME; |
Al Viro | 7de9c6ee | 2010-10-23 11:11:40 -0400 | [diff] [blame] | 5744 | ihold(inode); |
Josef Bacik | e997615 | 2012-10-11 15:53:56 -0400 | [diff] [blame] | 5745 | set_bit(BTRFS_INODE_COPY_EVERYTHING, &BTRFS_I(inode)->runtime_flags); |
Josef Bacik | aec7477 | 2008-07-24 12:12:38 -0400 | [diff] [blame] | 5746 | |
Josef Bacik | a1b075d | 2010-11-19 20:36:11 +0000 | [diff] [blame] | 5747 | err = btrfs_add_nondir(trans, dir, dentry, inode, 1, index); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 5748 | |
Yan, Zheng | a571952 | 2009-09-24 09:17:31 -0400 | [diff] [blame] | 5749 | if (err) { |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5750 | drop_inode = 1; |
Yan, Zheng | a571952 | 2009-09-24 09:17:31 -0400 | [diff] [blame] | 5751 | } else { |
Al Viro | 10d9f30 | 2011-07-16 23:09:10 -0400 | [diff] [blame] | 5752 | struct dentry *parent = dentry->d_parent; |
Yan, Zheng | a571952 | 2009-09-24 09:17:31 -0400 | [diff] [blame] | 5753 | err = btrfs_update_inode(trans, root, inode); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 5754 | if (err) |
| 5755 | goto fail; |
Al Viro | 08c422c | 2011-12-23 07:58:13 -0500 | [diff] [blame] | 5756 | d_instantiate(dentry, inode); |
Josef Bacik | 6a91221 | 2010-11-20 09:48:00 +0000 | [diff] [blame] | 5757 | btrfs_log_new_name(trans, inode, NULL, parent); |
Yan, Zheng | a571952 | 2009-09-24 09:17:31 -0400 | [diff] [blame] | 5758 | } |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5759 | |
Josef Bacik | 7ad85bb | 2012-01-12 19:10:12 -0500 | [diff] [blame] | 5760 | btrfs_end_transaction(trans, root); |
Chris Mason | 1832a6d | 2007-12-21 16:27:21 -0500 | [diff] [blame] | 5761 | fail: |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5762 | if (drop_inode) { |
| 5763 | inode_dec_link_count(inode); |
| 5764 | iput(inode); |
| 5765 | } |
Liu Bo | b53d3f5 | 2012-11-14 14:34:34 +0000 | [diff] [blame] | 5766 | btrfs_btree_balance_dirty(root); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5767 | return err; |
| 5768 | } |
| 5769 | |
Al Viro | 18bb1db | 2011-07-26 01:41:39 -0400 | [diff] [blame] | 5770 | 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] | 5771 | { |
Chris Mason | b9d8666 | 2008-05-02 16:13:49 -0400 | [diff] [blame] | 5772 | struct inode *inode = NULL; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5773 | struct btrfs_trans_handle *trans; |
| 5774 | struct btrfs_root *root = BTRFS_I(dir)->root; |
| 5775 | int err = 0; |
| 5776 | int drop_on_err = 0; |
Chris Mason | b9d8666 | 2008-05-02 16:13:49 -0400 | [diff] [blame] | 5777 | u64 objectid = 0; |
Chris Mason | 00e4e6b | 2008-08-05 11:18:09 -0400 | [diff] [blame] | 5778 | u64 index = 0; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5779 | |
Josef Bacik | 9ed74f2 | 2009-09-11 16:12:44 -0400 | [diff] [blame] | 5780 | /* |
| 5781 | * 2 items for inode and ref |
| 5782 | * 2 items for dir items |
| 5783 | * 1 for xattr if selinux is on |
| 5784 | */ |
Yan, Zheng | a22285a | 2010-05-16 10:48:46 -0400 | [diff] [blame] | 5785 | trans = btrfs_start_transaction(root, 5); |
| 5786 | if (IS_ERR(trans)) |
| 5787 | return PTR_ERR(trans); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5788 | |
Li Zefan | 581bb05 | 2011-04-20 10:06:11 +0800 | [diff] [blame] | 5789 | err = btrfs_find_free_ino(root, &objectid); |
| 5790 | if (err) |
| 5791 | goto out_fail; |
| 5792 | |
Josef Bacik | aec7477 | 2008-07-24 12:12:38 -0400 | [diff] [blame] | 5793 | inode = btrfs_new_inode(trans, root, dir, dentry->d_name.name, |
Li Zefan | 33345d01 | 2011-04-20 10:31:50 +0800 | [diff] [blame] | 5794 | dentry->d_name.len, btrfs_ino(dir), objectid, |
Josef Bacik | d82a6f1d | 2011-05-11 15:26:06 -0400 | [diff] [blame] | 5795 | S_IFDIR | mode, &index); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5796 | if (IS_ERR(inode)) { |
| 5797 | err = PTR_ERR(inode); |
| 5798 | goto out_fail; |
| 5799 | } |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 5800 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5801 | drop_on_err = 1; |
Josef Bacik | 33268ea | 2008-07-24 12:16:36 -0400 | [diff] [blame] | 5802 | |
Eric Paris | 2a7dba3 | 2011-02-01 11:05:39 -0500 | [diff] [blame] | 5803 | err = btrfs_init_inode_security(trans, inode, dir, &dentry->d_name); |
Josef Bacik | 33268ea | 2008-07-24 12:16:36 -0400 | [diff] [blame] | 5804 | if (err) |
| 5805 | goto out_fail; |
| 5806 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5807 | inode->i_op = &btrfs_dir_inode_operations; |
| 5808 | inode->i_fop = &btrfs_dir_file_operations; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5809 | |
Chris Mason | dbe674a | 2008-07-17 12:54:05 -0400 | [diff] [blame] | 5810 | btrfs_i_size_write(inode, 0); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5811 | err = btrfs_update_inode(trans, root, inode); |
| 5812 | if (err) |
| 5813 | goto out_fail; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 5814 | |
Josef Bacik | a1b075d | 2010-11-19 20:36:11 +0000 | [diff] [blame] | 5815 | err = btrfs_add_link(trans, dir, inode, dentry->d_name.name, |
| 5816 | dentry->d_name.len, 0, index); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5817 | if (err) |
| 5818 | goto out_fail; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 5819 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5820 | d_instantiate(dentry, inode); |
| 5821 | drop_on_err = 0; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5822 | |
| 5823 | out_fail: |
Josef Bacik | 7ad85bb | 2012-01-12 19:10:12 -0500 | [diff] [blame] | 5824 | btrfs_end_transaction(trans, root); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5825 | if (drop_on_err) |
| 5826 | iput(inode); |
Liu Bo | b53d3f5 | 2012-11-14 14:34:34 +0000 | [diff] [blame] | 5827 | btrfs_btree_balance_dirty(root); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5828 | return err; |
| 5829 | } |
| 5830 | |
Chris Mason | d352ac6 | 2008-09-29 15:18:18 -0400 | [diff] [blame] | 5831 | /* helper for btfs_get_extent. Given an existing extent in the tree, |
| 5832 | * and an extent that you want to insert, deal with overlap and insert |
| 5833 | * the new extent into the tree. |
| 5834 | */ |
Chris Mason | 3b95151 | 2008-04-17 11:29:12 -0400 | [diff] [blame] | 5835 | static int merge_extent_mapping(struct extent_map_tree *em_tree, |
| 5836 | struct extent_map *existing, |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 5837 | struct extent_map *em, |
| 5838 | u64 map_start, u64 map_len) |
Chris Mason | 3b95151 | 2008-04-17 11:29:12 -0400 | [diff] [blame] | 5839 | { |
| 5840 | u64 start_diff; |
Chris Mason | 3b95151 | 2008-04-17 11:29:12 -0400 | [diff] [blame] | 5841 | |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 5842 | BUG_ON(map_start < em->start || map_start >= extent_map_end(em)); |
| 5843 | start_diff = map_start - em->start; |
| 5844 | em->start = map_start; |
| 5845 | em->len = map_len; |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 5846 | if (em->block_start < EXTENT_MAP_LAST_BYTE && |
| 5847 | !test_bit(EXTENT_FLAG_COMPRESSED, &em->flags)) { |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 5848 | em->block_start += start_diff; |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 5849 | em->block_len -= start_diff; |
| 5850 | } |
Josef Bacik | 09a2a8f9 | 2013-04-05 16:51:15 -0400 | [diff] [blame] | 5851 | return add_extent_mapping(em_tree, em, 0); |
Chris Mason | 3b95151 | 2008-04-17 11:29:12 -0400 | [diff] [blame] | 5852 | } |
| 5853 | |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 5854 | static noinline int uncompress_inline(struct btrfs_path *path, |
| 5855 | struct inode *inode, struct page *page, |
| 5856 | size_t pg_offset, u64 extent_offset, |
| 5857 | struct btrfs_file_extent_item *item) |
| 5858 | { |
| 5859 | int ret; |
| 5860 | struct extent_buffer *leaf = path->nodes[0]; |
| 5861 | char *tmp; |
| 5862 | size_t max_size; |
| 5863 | unsigned long inline_size; |
| 5864 | unsigned long ptr; |
Li Zefan | 261507a0 | 2010-12-17 14:21:50 +0800 | [diff] [blame] | 5865 | int compress_type; |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 5866 | |
| 5867 | WARN_ON(pg_offset != 0); |
Li Zefan | 261507a0 | 2010-12-17 14:21:50 +0800 | [diff] [blame] | 5868 | compress_type = btrfs_file_extent_compression(leaf, item); |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 5869 | max_size = btrfs_file_extent_ram_bytes(leaf, item); |
| 5870 | inline_size = btrfs_file_extent_inline_item_len(leaf, |
Ross Kirk | dd3cc16 | 2013-09-16 15:58:09 +0100 | [diff] [blame] | 5871 | btrfs_item_nr(path->slots[0])); |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 5872 | tmp = kmalloc(inline_size, GFP_NOFS); |
Tsutomu Itoh | 8d41371 | 2011-04-25 19:43:52 -0400 | [diff] [blame] | 5873 | if (!tmp) |
| 5874 | return -ENOMEM; |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 5875 | ptr = btrfs_file_extent_inline_start(item); |
| 5876 | |
| 5877 | read_extent_buffer(leaf, tmp, ptr, inline_size); |
| 5878 | |
Chris Mason | 5b050f0 | 2008-11-11 09:34:41 -0500 | [diff] [blame] | 5879 | max_size = min_t(unsigned long, PAGE_CACHE_SIZE, max_size); |
Li Zefan | 261507a0 | 2010-12-17 14:21:50 +0800 | [diff] [blame] | 5880 | ret = btrfs_decompress(compress_type, tmp, page, |
| 5881 | extent_offset, inline_size, max_size); |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 5882 | if (ret) { |
Cong Wang | 7ac687d | 2011-11-25 23:14:28 +0800 | [diff] [blame] | 5883 | char *kaddr = kmap_atomic(page); |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 5884 | unsigned long copy_size = min_t(u64, |
| 5885 | PAGE_CACHE_SIZE - pg_offset, |
| 5886 | max_size - extent_offset); |
| 5887 | memset(kaddr + pg_offset, 0, copy_size); |
Cong Wang | 7ac687d | 2011-11-25 23:14:28 +0800 | [diff] [blame] | 5888 | kunmap_atomic(kaddr); |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 5889 | } |
| 5890 | kfree(tmp); |
| 5891 | return 0; |
| 5892 | } |
| 5893 | |
Chris Mason | d352ac6 | 2008-09-29 15:18:18 -0400 | [diff] [blame] | 5894 | /* |
| 5895 | * 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] | 5896 | * the ugly parts come from merging extents from the disk with the in-ram |
| 5897 | * representation. This gets more complex because of the data=ordered code, |
Chris Mason | d352ac6 | 2008-09-29 15:18:18 -0400 | [diff] [blame] | 5898 | * where the in-ram extents might be locked pending data=ordered completion. |
| 5899 | * |
| 5900 | * This also copies inline extents directly into the page. |
| 5901 | */ |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 5902 | |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 5903 | struct extent_map *btrfs_get_extent(struct inode *inode, struct page *page, |
Chris Mason | 70dec80 | 2008-01-29 09:59:12 -0500 | [diff] [blame] | 5904 | size_t pg_offset, u64 start, u64 len, |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 5905 | int create) |
| 5906 | { |
| 5907 | int ret; |
| 5908 | int err = 0; |
Chris Mason | db94535 | 2007-10-15 16:15:53 -0400 | [diff] [blame] | 5909 | u64 bytenr; |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 5910 | u64 extent_start = 0; |
| 5911 | u64 extent_end = 0; |
Li Zefan | 33345d01 | 2011-04-20 10:31:50 +0800 | [diff] [blame] | 5912 | u64 objectid = btrfs_ino(inode); |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 5913 | u32 found_type; |
Chris Mason | f421950 | 2008-07-22 11:18:09 -0400 | [diff] [blame] | 5914 | struct btrfs_path *path = NULL; |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 5915 | struct btrfs_root *root = BTRFS_I(inode)->root; |
| 5916 | struct btrfs_file_extent_item *item; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 5917 | struct extent_buffer *leaf; |
| 5918 | struct btrfs_key found_key; |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 5919 | struct extent_map *em = NULL; |
| 5920 | struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree; |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 5921 | struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree; |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 5922 | struct btrfs_trans_handle *trans = NULL; |
Li Zefan | 261507a0 | 2010-12-17 14:21:50 +0800 | [diff] [blame] | 5923 | int compress_type; |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 5924 | |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 5925 | again: |
Chris Mason | 890871b | 2009-09-02 16:24:52 -0400 | [diff] [blame] | 5926 | read_lock(&em_tree->lock); |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 5927 | em = lookup_extent_mapping(em_tree, start, len); |
Chris Mason | a061fc8 | 2008-05-07 11:43:44 -0400 | [diff] [blame] | 5928 | if (em) |
| 5929 | em->bdev = root->fs_info->fs_devices->latest_bdev; |
Chris Mason | 890871b | 2009-09-02 16:24:52 -0400 | [diff] [blame] | 5930 | read_unlock(&em_tree->lock); |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 5931 | |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 5932 | if (em) { |
Chris Mason | e1c4b74 | 2008-04-22 13:26:46 -0400 | [diff] [blame] | 5933 | if (em->start > start || em->start + em->len <= start) |
| 5934 | free_extent_map(em); |
| 5935 | else if (em->block_start == EXTENT_MAP_INLINE && page) |
Chris Mason | 70dec80 | 2008-01-29 09:59:12 -0500 | [diff] [blame] | 5936 | free_extent_map(em); |
| 5937 | else |
| 5938 | goto out; |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 5939 | } |
David Sterba | 172ddd6 | 2011-04-21 00:48:27 +0200 | [diff] [blame] | 5940 | em = alloc_extent_map(); |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 5941 | if (!em) { |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 5942 | err = -ENOMEM; |
| 5943 | goto out; |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 5944 | } |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 5945 | em->bdev = root->fs_info->fs_devices->latest_bdev; |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 5946 | em->start = EXTENT_MAP_HOLE; |
Chris Mason | 445a694 | 2008-11-10 11:53:33 -0500 | [diff] [blame] | 5947 | em->orig_start = EXTENT_MAP_HOLE; |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 5948 | em->len = (u64)-1; |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 5949 | em->block_len = (u64)-1; |
Chris Mason | f421950 | 2008-07-22 11:18:09 -0400 | [diff] [blame] | 5950 | |
| 5951 | if (!path) { |
| 5952 | path = btrfs_alloc_path(); |
Josef Bacik | 026fd31 | 2011-05-13 10:32:11 -0400 | [diff] [blame] | 5953 | if (!path) { |
| 5954 | err = -ENOMEM; |
| 5955 | goto out; |
| 5956 | } |
| 5957 | /* |
| 5958 | * Chances are we'll be called again, so go ahead and do |
| 5959 | * readahead |
| 5960 | */ |
| 5961 | path->reada = 1; |
Chris Mason | f421950 | 2008-07-22 11:18:09 -0400 | [diff] [blame] | 5962 | } |
| 5963 | |
Chris Mason | 179e29e | 2007-11-01 11:28:41 -0400 | [diff] [blame] | 5964 | ret = btrfs_lookup_file_extent(trans, root, path, |
| 5965 | objectid, start, trans != NULL); |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 5966 | if (ret < 0) { |
| 5967 | err = ret; |
| 5968 | goto out; |
| 5969 | } |
| 5970 | |
| 5971 | if (ret != 0) { |
| 5972 | if (path->slots[0] == 0) |
| 5973 | goto not_found; |
| 5974 | path->slots[0]--; |
| 5975 | } |
| 5976 | |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 5977 | leaf = path->nodes[0]; |
| 5978 | item = btrfs_item_ptr(leaf, path->slots[0], |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 5979 | struct btrfs_file_extent_item); |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 5980 | /* are we inside the extent that was found? */ |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 5981 | btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]); |
| 5982 | found_type = btrfs_key_type(&found_key); |
| 5983 | if (found_key.objectid != objectid || |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 5984 | found_type != BTRFS_EXTENT_DATA_KEY) { |
| 5985 | goto not_found; |
| 5986 | } |
| 5987 | |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 5988 | found_type = btrfs_file_extent_type(leaf, item); |
| 5989 | extent_start = found_key.offset; |
Li Zefan | 261507a0 | 2010-12-17 14:21:50 +0800 | [diff] [blame] | 5990 | compress_type = btrfs_file_extent_compression(leaf, item); |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 5991 | if (found_type == BTRFS_FILE_EXTENT_REG || |
| 5992 | found_type == BTRFS_FILE_EXTENT_PREALLOC) { |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 5993 | extent_end = extent_start + |
Chris Mason | db94535 | 2007-10-15 16:15:53 -0400 | [diff] [blame] | 5994 | btrfs_file_extent_num_bytes(leaf, item); |
Yan Zheng | 9036c10 | 2008-10-30 14:19:41 -0400 | [diff] [blame] | 5995 | } else if (found_type == BTRFS_FILE_EXTENT_INLINE) { |
| 5996 | size_t size; |
| 5997 | size = btrfs_file_extent_inline_len(leaf, item); |
Qu Wenruo | fda2832 | 2013-02-26 08:10:22 +0000 | [diff] [blame] | 5998 | extent_end = ALIGN(extent_start + size, root->sectorsize); |
Yan Zheng | 9036c10 | 2008-10-30 14:19:41 -0400 | [diff] [blame] | 5999 | } |
| 6000 | |
| 6001 | if (start >= extent_end) { |
| 6002 | path->slots[0]++; |
| 6003 | if (path->slots[0] >= btrfs_header_nritems(leaf)) { |
| 6004 | ret = btrfs_next_leaf(root, path); |
| 6005 | if (ret < 0) { |
| 6006 | err = ret; |
| 6007 | goto out; |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 6008 | } |
Yan Zheng | 9036c10 | 2008-10-30 14:19:41 -0400 | [diff] [blame] | 6009 | if (ret > 0) |
| 6010 | goto not_found; |
| 6011 | leaf = path->nodes[0]; |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 6012 | } |
Yan Zheng | 9036c10 | 2008-10-30 14:19:41 -0400 | [diff] [blame] | 6013 | btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]); |
| 6014 | if (found_key.objectid != objectid || |
| 6015 | found_key.type != BTRFS_EXTENT_DATA_KEY) |
| 6016 | goto not_found; |
| 6017 | if (start + len <= found_key.offset) |
| 6018 | goto not_found; |
| 6019 | em->start = start; |
Josef Bacik | 70c8a91 | 2012-10-11 16:54:30 -0400 | [diff] [blame] | 6020 | em->orig_start = start; |
Yan Zheng | 9036c10 | 2008-10-30 14:19:41 -0400 | [diff] [blame] | 6021 | em->len = found_key.offset - start; |
| 6022 | goto not_found_em; |
| 6023 | } |
| 6024 | |
Josef Bacik | cc95bef | 2013-04-04 14:31:27 -0400 | [diff] [blame] | 6025 | em->ram_bytes = btrfs_file_extent_ram_bytes(leaf, item); |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 6026 | if (found_type == BTRFS_FILE_EXTENT_REG || |
| 6027 | found_type == BTRFS_FILE_EXTENT_PREALLOC) { |
Yan Zheng | 9036c10 | 2008-10-30 14:19:41 -0400 | [diff] [blame] | 6028 | em->start = extent_start; |
| 6029 | em->len = extent_end - extent_start; |
Yan Zheng | ff5b7ee | 2008-11-10 07:34:43 -0500 | [diff] [blame] | 6030 | em->orig_start = extent_start - |
| 6031 | btrfs_file_extent_offset(leaf, item); |
Josef Bacik | b493968 | 2012-12-03 10:31:19 -0500 | [diff] [blame] | 6032 | em->orig_block_len = btrfs_file_extent_disk_num_bytes(leaf, |
| 6033 | item); |
Chris Mason | db94535 | 2007-10-15 16:15:53 -0400 | [diff] [blame] | 6034 | bytenr = btrfs_file_extent_disk_bytenr(leaf, item); |
| 6035 | if (bytenr == 0) { |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 6036 | em->block_start = EXTENT_MAP_HOLE; |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 6037 | goto insert; |
| 6038 | } |
Li Zefan | 261507a0 | 2010-12-17 14:21:50 +0800 | [diff] [blame] | 6039 | if (compress_type != BTRFS_COMPRESS_NONE) { |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 6040 | set_bit(EXTENT_FLAG_COMPRESSED, &em->flags); |
Li Zefan | 261507a0 | 2010-12-17 14:21:50 +0800 | [diff] [blame] | 6041 | em->compress_type = compress_type; |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 6042 | em->block_start = bytenr; |
Josef Bacik | b493968 | 2012-12-03 10:31:19 -0500 | [diff] [blame] | 6043 | em->block_len = em->orig_block_len; |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 6044 | } else { |
| 6045 | bytenr += btrfs_file_extent_offset(leaf, item); |
| 6046 | em->block_start = bytenr; |
| 6047 | em->block_len = em->len; |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 6048 | if (found_type == BTRFS_FILE_EXTENT_PREALLOC) |
| 6049 | set_bit(EXTENT_FLAG_PREALLOC, &em->flags); |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 6050 | } |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 6051 | goto insert; |
| 6052 | } else if (found_type == BTRFS_FILE_EXTENT_INLINE) { |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 6053 | unsigned long ptr; |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 6054 | char *map; |
Chris Mason | 3326d1b | 2007-10-15 16:18:25 -0400 | [diff] [blame] | 6055 | size_t size; |
| 6056 | size_t extent_offset; |
| 6057 | size_t copy_size; |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 6058 | |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 6059 | em->block_start = EXTENT_MAP_INLINE; |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 6060 | if (!page || create) { |
Yan | 689f934 | 2007-10-29 11:41:07 -0400 | [diff] [blame] | 6061 | em->start = extent_start; |
Yan Zheng | 9036c10 | 2008-10-30 14:19:41 -0400 | [diff] [blame] | 6062 | em->len = extent_end - extent_start; |
Yan | 689f934 | 2007-10-29 11:41:07 -0400 | [diff] [blame] | 6063 | goto out; |
| 6064 | } |
| 6065 | |
Yan Zheng | 9036c10 | 2008-10-30 14:19:41 -0400 | [diff] [blame] | 6066 | size = btrfs_file_extent_inline_len(leaf, item); |
| 6067 | extent_offset = page_offset(page) + pg_offset - extent_start; |
Chris Mason | 70dec80 | 2008-01-29 09:59:12 -0500 | [diff] [blame] | 6068 | copy_size = min_t(u64, PAGE_CACHE_SIZE - pg_offset, |
Yan | 689f934 | 2007-10-29 11:41:07 -0400 | [diff] [blame] | 6069 | size - extent_offset); |
Chris Mason | 3326d1b | 2007-10-15 16:18:25 -0400 | [diff] [blame] | 6070 | em->start = extent_start + extent_offset; |
Qu Wenruo | fda2832 | 2013-02-26 08:10:22 +0000 | [diff] [blame] | 6071 | em->len = ALIGN(copy_size, root->sectorsize); |
Josef Bacik | b493968 | 2012-12-03 10:31:19 -0500 | [diff] [blame] | 6072 | em->orig_block_len = em->len; |
Josef Bacik | 70c8a91 | 2012-10-11 16:54:30 -0400 | [diff] [blame] | 6073 | em->orig_start = em->start; |
Li Zefan | 261507a0 | 2010-12-17 14:21:50 +0800 | [diff] [blame] | 6074 | if (compress_type) { |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 6075 | set_bit(EXTENT_FLAG_COMPRESSED, &em->flags); |
Li Zefan | 261507a0 | 2010-12-17 14:21:50 +0800 | [diff] [blame] | 6076 | em->compress_type = compress_type; |
| 6077 | } |
Yan | 689f934 | 2007-10-29 11:41:07 -0400 | [diff] [blame] | 6078 | ptr = btrfs_file_extent_inline_start(item) + extent_offset; |
Chris Mason | 179e29e | 2007-11-01 11:28:41 -0400 | [diff] [blame] | 6079 | if (create == 0 && !PageUptodate(page)) { |
Li Zefan | 261507a0 | 2010-12-17 14:21:50 +0800 | [diff] [blame] | 6080 | if (btrfs_file_extent_compression(leaf, item) != |
| 6081 | BTRFS_COMPRESS_NONE) { |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 6082 | ret = uncompress_inline(path, inode, page, |
| 6083 | pg_offset, |
| 6084 | extent_offset, item); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 6085 | BUG_ON(ret); /* -ENOMEM */ |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 6086 | } else { |
| 6087 | map = kmap(page); |
| 6088 | read_extent_buffer(leaf, map + pg_offset, ptr, |
| 6089 | copy_size); |
Chris Mason | 93c82d5 | 2009-09-11 12:36:29 -0400 | [diff] [blame] | 6090 | if (pg_offset + copy_size < PAGE_CACHE_SIZE) { |
| 6091 | memset(map + pg_offset + copy_size, 0, |
| 6092 | PAGE_CACHE_SIZE - pg_offset - |
| 6093 | copy_size); |
| 6094 | } |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 6095 | kunmap(page); |
| 6096 | } |
Chris Mason | 179e29e | 2007-11-01 11:28:41 -0400 | [diff] [blame] | 6097 | flush_dcache_page(page); |
| 6098 | } else if (create && PageUptodate(page)) { |
Jan Schmidt | 6bf7e08 | 2011-12-01 14:35:19 +0100 | [diff] [blame] | 6099 | BUG(); |
Chris Mason | 179e29e | 2007-11-01 11:28:41 -0400 | [diff] [blame] | 6100 | if (!trans) { |
| 6101 | kunmap(page); |
| 6102 | free_extent_map(em); |
| 6103 | em = NULL; |
Chris Mason | ff5714c | 2011-05-28 07:00:39 -0400 | [diff] [blame] | 6104 | |
David Sterba | b3b4aa7 | 2011-04-21 01:20:15 +0200 | [diff] [blame] | 6105 | btrfs_release_path(path); |
Josef Bacik | 7a7eaa4 | 2011-04-13 12:54:33 -0400 | [diff] [blame] | 6106 | trans = btrfs_join_transaction(root); |
Chris Mason | ff5714c | 2011-05-28 07:00:39 -0400 | [diff] [blame] | 6107 | |
Tsutomu Itoh | 3612b49 | 2011-01-25 02:51:38 +0000 | [diff] [blame] | 6108 | if (IS_ERR(trans)) |
| 6109 | return ERR_CAST(trans); |
Chris Mason | 179e29e | 2007-11-01 11:28:41 -0400 | [diff] [blame] | 6110 | goto again; |
| 6111 | } |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 6112 | map = kmap(page); |
Chris Mason | 70dec80 | 2008-01-29 09:59:12 -0500 | [diff] [blame] | 6113 | write_extent_buffer(leaf, map + pg_offset, ptr, |
Chris Mason | 179e29e | 2007-11-01 11:28:41 -0400 | [diff] [blame] | 6114 | copy_size); |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 6115 | kunmap(page); |
Chris Mason | 179e29e | 2007-11-01 11:28:41 -0400 | [diff] [blame] | 6116 | btrfs_mark_buffer_dirty(leaf); |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 6117 | } |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 6118 | set_extent_uptodate(io_tree, em->start, |
Arne Jansen | 507903b | 2011-04-06 10:02:20 +0000 | [diff] [blame] | 6119 | extent_map_end(em) - 1, NULL, GFP_NOFS); |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 6120 | goto insert; |
| 6121 | } else { |
Julia Lawall | 31b1a2b | 2012-11-03 10:58:34 +0000 | [diff] [blame] | 6122 | WARN(1, KERN_ERR "btrfs unknown found_type %d\n", found_type); |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 6123 | } |
| 6124 | not_found: |
| 6125 | em->start = start; |
Josef Bacik | 70c8a91 | 2012-10-11 16:54:30 -0400 | [diff] [blame] | 6126 | em->orig_start = start; |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 6127 | em->len = len; |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 6128 | not_found_em: |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 6129 | em->block_start = EXTENT_MAP_HOLE; |
Yan Zheng | 9036c10 | 2008-10-30 14:19:41 -0400 | [diff] [blame] | 6130 | set_bit(EXTENT_FLAG_VACANCY, &em->flags); |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 6131 | insert: |
David Sterba | b3b4aa7 | 2011-04-21 01:20:15 +0200 | [diff] [blame] | 6132 | btrfs_release_path(path); |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 6133 | if (em->start > start || extent_map_end(em) <= start) { |
Simon Kirby | c2cf52e | 2013-03-19 22:41:23 +0000 | [diff] [blame] | 6134 | 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] | 6135 | em->start, em->len, start, len); |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 6136 | err = -EIO; |
| 6137 | goto out; |
| 6138 | } |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 6139 | |
| 6140 | err = 0; |
Chris Mason | 890871b | 2009-09-02 16:24:52 -0400 | [diff] [blame] | 6141 | write_lock(&em_tree->lock); |
Josef Bacik | 09a2a8f9 | 2013-04-05 16:51:15 -0400 | [diff] [blame] | 6142 | ret = add_extent_mapping(em_tree, em, 0); |
Chris Mason | 3b95151 | 2008-04-17 11:29:12 -0400 | [diff] [blame] | 6143 | /* it is possible that someone inserted the extent into the tree |
| 6144 | * while we had the lock dropped. It is also possible that |
| 6145 | * an overlapping map exists in the tree |
| 6146 | */ |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 6147 | if (ret == -EEXIST) { |
Chris Mason | 3b95151 | 2008-04-17 11:29:12 -0400 | [diff] [blame] | 6148 | struct extent_map *existing; |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 6149 | |
| 6150 | ret = 0; |
| 6151 | |
Chris Mason | 3b95151 | 2008-04-17 11:29:12 -0400 | [diff] [blame] | 6152 | existing = lookup_extent_mapping(em_tree, start, len); |
Chris Mason | e1c4b74 | 2008-04-22 13:26:46 -0400 | [diff] [blame] | 6153 | if (existing && (existing->start > start || |
| 6154 | existing->start + existing->len <= start)) { |
| 6155 | free_extent_map(existing); |
| 6156 | existing = NULL; |
| 6157 | } |
Chris Mason | 3b95151 | 2008-04-17 11:29:12 -0400 | [diff] [blame] | 6158 | if (!existing) { |
| 6159 | existing = lookup_extent_mapping(em_tree, em->start, |
| 6160 | em->len); |
| 6161 | if (existing) { |
| 6162 | err = merge_extent_mapping(em_tree, existing, |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 6163 | em, start, |
| 6164 | root->sectorsize); |
Chris Mason | 3b95151 | 2008-04-17 11:29:12 -0400 | [diff] [blame] | 6165 | free_extent_map(existing); |
| 6166 | if (err) { |
| 6167 | free_extent_map(em); |
| 6168 | em = NULL; |
| 6169 | } |
| 6170 | } else { |
| 6171 | err = -EIO; |
Chris Mason | 3b95151 | 2008-04-17 11:29:12 -0400 | [diff] [blame] | 6172 | free_extent_map(em); |
| 6173 | em = NULL; |
| 6174 | } |
| 6175 | } else { |
| 6176 | free_extent_map(em); |
| 6177 | em = existing; |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 6178 | err = 0; |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 6179 | } |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 6180 | } |
Chris Mason | 890871b | 2009-09-02 16:24:52 -0400 | [diff] [blame] | 6181 | write_unlock(&em_tree->lock); |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 6182 | out: |
liubo | 1abe9b8 | 2011-03-24 11:18:59 +0000 | [diff] [blame] | 6183 | |
Tsutomu Itoh | f0bd95e | 2012-10-01 03:08:37 -0600 | [diff] [blame] | 6184 | if (em) |
| 6185 | trace_btrfs_get_extent(root, em); |
liubo | 1abe9b8 | 2011-03-24 11:18:59 +0000 | [diff] [blame] | 6186 | |
Chris Mason | f421950 | 2008-07-22 11:18:09 -0400 | [diff] [blame] | 6187 | if (path) |
| 6188 | btrfs_free_path(path); |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 6189 | if (trans) { |
| 6190 | ret = btrfs_end_transaction(trans, root); |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 6191 | if (!err) |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 6192 | err = ret; |
| 6193 | } |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 6194 | if (err) { |
| 6195 | free_extent_map(em); |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 6196 | return ERR_PTR(err); |
| 6197 | } |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 6198 | BUG_ON(!em); /* Error is always set */ |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 6199 | return em; |
| 6200 | } |
| 6201 | |
Chris Mason | ec29ed5 | 2011-02-23 16:23:20 -0500 | [diff] [blame] | 6202 | struct extent_map *btrfs_get_extent_fiemap(struct inode *inode, struct page *page, |
| 6203 | size_t pg_offset, u64 start, u64 len, |
| 6204 | int create) |
| 6205 | { |
| 6206 | struct extent_map *em; |
| 6207 | struct extent_map *hole_em = NULL; |
| 6208 | u64 range_start = start; |
| 6209 | u64 end; |
| 6210 | u64 found; |
| 6211 | u64 found_end; |
| 6212 | int err = 0; |
| 6213 | |
| 6214 | em = btrfs_get_extent(inode, page, pg_offset, start, len, create); |
| 6215 | if (IS_ERR(em)) |
| 6216 | return em; |
| 6217 | if (em) { |
| 6218 | /* |
Liu Bo | f9e4fb5 | 2013-01-07 10:10:12 +0000 | [diff] [blame] | 6219 | * if our em maps to |
| 6220 | * - a hole or |
| 6221 | * - a pre-alloc extent, |
| 6222 | * there might actually be delalloc bytes behind it. |
Chris Mason | ec29ed5 | 2011-02-23 16:23:20 -0500 | [diff] [blame] | 6223 | */ |
Liu Bo | f9e4fb5 | 2013-01-07 10:10:12 +0000 | [diff] [blame] | 6224 | if (em->block_start != EXTENT_MAP_HOLE && |
| 6225 | !test_bit(EXTENT_FLAG_PREALLOC, &em->flags)) |
Chris Mason | ec29ed5 | 2011-02-23 16:23:20 -0500 | [diff] [blame] | 6226 | return em; |
| 6227 | else |
| 6228 | hole_em = em; |
| 6229 | } |
| 6230 | |
| 6231 | /* check to see if we've wrapped (len == -1 or similar) */ |
| 6232 | end = start + len; |
| 6233 | if (end < start) |
| 6234 | end = (u64)-1; |
| 6235 | else |
| 6236 | end -= 1; |
| 6237 | |
| 6238 | em = NULL; |
| 6239 | |
| 6240 | /* ok, we didn't find anything, lets look for delalloc */ |
| 6241 | found = count_range_bits(&BTRFS_I(inode)->io_tree, &range_start, |
| 6242 | end, len, EXTENT_DELALLOC, 1); |
| 6243 | found_end = range_start + found; |
| 6244 | if (found_end < range_start) |
| 6245 | found_end = (u64)-1; |
| 6246 | |
| 6247 | /* |
| 6248 | * we didn't find anything useful, return |
| 6249 | * the original results from get_extent() |
| 6250 | */ |
| 6251 | if (range_start > end || found_end <= start) { |
| 6252 | em = hole_em; |
| 6253 | hole_em = NULL; |
| 6254 | goto out; |
| 6255 | } |
| 6256 | |
| 6257 | /* adjust the range_start to make sure it doesn't |
| 6258 | * go backwards from the start they passed in |
| 6259 | */ |
| 6260 | range_start = max(start,range_start); |
| 6261 | found = found_end - range_start; |
| 6262 | |
| 6263 | if (found > 0) { |
| 6264 | u64 hole_start = start; |
| 6265 | u64 hole_len = len; |
| 6266 | |
David Sterba | 172ddd6 | 2011-04-21 00:48:27 +0200 | [diff] [blame] | 6267 | em = alloc_extent_map(); |
Chris Mason | ec29ed5 | 2011-02-23 16:23:20 -0500 | [diff] [blame] | 6268 | if (!em) { |
| 6269 | err = -ENOMEM; |
| 6270 | goto out; |
| 6271 | } |
| 6272 | /* |
| 6273 | * when btrfs_get_extent can't find anything it |
| 6274 | * returns one huge hole |
| 6275 | * |
| 6276 | * make sure what it found really fits our range, and |
| 6277 | * adjust to make sure it is based on the start from |
| 6278 | * the caller |
| 6279 | */ |
| 6280 | if (hole_em) { |
| 6281 | u64 calc_end = extent_map_end(hole_em); |
| 6282 | |
| 6283 | if (calc_end <= start || (hole_em->start > end)) { |
| 6284 | free_extent_map(hole_em); |
| 6285 | hole_em = NULL; |
| 6286 | } else { |
| 6287 | hole_start = max(hole_em->start, start); |
| 6288 | hole_len = calc_end - hole_start; |
| 6289 | } |
| 6290 | } |
| 6291 | em->bdev = NULL; |
| 6292 | if (hole_em && range_start > hole_start) { |
| 6293 | /* our hole starts before our delalloc, so we |
| 6294 | * have to return just the parts of the hole |
| 6295 | * that go until the delalloc starts |
| 6296 | */ |
| 6297 | em->len = min(hole_len, |
| 6298 | range_start - hole_start); |
| 6299 | em->start = hole_start; |
| 6300 | em->orig_start = hole_start; |
| 6301 | /* |
| 6302 | * don't adjust block start at all, |
| 6303 | * it is fixed at EXTENT_MAP_HOLE |
| 6304 | */ |
| 6305 | em->block_start = hole_em->block_start; |
| 6306 | em->block_len = hole_len; |
Liu Bo | f9e4fb5 | 2013-01-07 10:10:12 +0000 | [diff] [blame] | 6307 | if (test_bit(EXTENT_FLAG_PREALLOC, &hole_em->flags)) |
| 6308 | set_bit(EXTENT_FLAG_PREALLOC, &em->flags); |
Chris Mason | ec29ed5 | 2011-02-23 16:23:20 -0500 | [diff] [blame] | 6309 | } else { |
| 6310 | em->start = range_start; |
| 6311 | em->len = found; |
| 6312 | em->orig_start = range_start; |
| 6313 | em->block_start = EXTENT_MAP_DELALLOC; |
| 6314 | em->block_len = found; |
| 6315 | } |
| 6316 | } else if (hole_em) { |
| 6317 | return hole_em; |
| 6318 | } |
| 6319 | out: |
| 6320 | |
| 6321 | free_extent_map(hole_em); |
| 6322 | if (err) { |
| 6323 | free_extent_map(em); |
| 6324 | return ERR_PTR(err); |
| 6325 | } |
| 6326 | return em; |
| 6327 | } |
| 6328 | |
Josef Bacik | 4b46fce | 2010-05-23 11:00:55 -0400 | [diff] [blame] | 6329 | static struct extent_map *btrfs_new_extent_direct(struct inode *inode, |
| 6330 | u64 start, u64 len) |
| 6331 | { |
| 6332 | struct btrfs_root *root = BTRFS_I(inode)->root; |
Josef Bacik | 70c8a91 | 2012-10-11 16:54:30 -0400 | [diff] [blame] | 6333 | struct extent_map *em; |
Josef Bacik | 4b46fce | 2010-05-23 11:00:55 -0400 | [diff] [blame] | 6334 | struct btrfs_key ins; |
| 6335 | u64 alloc_hint; |
| 6336 | int ret; |
Josef Bacik | 4b46fce | 2010-05-23 11:00:55 -0400 | [diff] [blame] | 6337 | |
Josef Bacik | 4b46fce | 2010-05-23 11:00:55 -0400 | [diff] [blame] | 6338 | alloc_hint = get_extent_allocation_hint(inode, start, len); |
Josef Bacik | 0036158 | 2013-08-14 14:02:47 -0400 | [diff] [blame] | 6339 | ret = btrfs_reserve_extent(root, len, root->sectorsize, 0, |
Josef Bacik | 81c9ad2 | 2012-01-18 10:56:06 -0500 | [diff] [blame] | 6340 | alloc_hint, &ins, 1); |
Josef Bacik | 0036158 | 2013-08-14 14:02:47 -0400 | [diff] [blame] | 6341 | if (ret) |
| 6342 | return ERR_PTR(ret); |
Josef Bacik | 4b46fce | 2010-05-23 11:00:55 -0400 | [diff] [blame] | 6343 | |
Josef Bacik | 70c8a91 | 2012-10-11 16:54:30 -0400 | [diff] [blame] | 6344 | em = create_pinned_em(inode, start, ins.offset, start, ins.objectid, |
Josef Bacik | cc95bef | 2013-04-04 14:31:27 -0400 | [diff] [blame] | 6345 | ins.offset, ins.offset, ins.offset, 0); |
Josef Bacik | 0036158 | 2013-08-14 14:02:47 -0400 | [diff] [blame] | 6346 | if (IS_ERR(em)) { |
| 6347 | btrfs_free_reserved_extent(root, ins.objectid, ins.offset); |
| 6348 | return em; |
| 6349 | } |
Josef Bacik | 4b46fce | 2010-05-23 11:00:55 -0400 | [diff] [blame] | 6350 | |
| 6351 | ret = btrfs_add_ordered_extent_dio(inode, start, ins.objectid, |
| 6352 | ins.offset, ins.offset, 0); |
| 6353 | if (ret) { |
| 6354 | btrfs_free_reserved_extent(root, ins.objectid, ins.offset); |
Josef Bacik | 0036158 | 2013-08-14 14:02:47 -0400 | [diff] [blame] | 6355 | free_extent_map(em); |
| 6356 | return ERR_PTR(ret); |
Josef Bacik | 4b46fce | 2010-05-23 11:00:55 -0400 | [diff] [blame] | 6357 | } |
Josef Bacik | 0036158 | 2013-08-14 14:02:47 -0400 | [diff] [blame] | 6358 | |
Josef Bacik | 4b46fce | 2010-05-23 11:00:55 -0400 | [diff] [blame] | 6359 | return em; |
| 6360 | } |
| 6361 | |
Chris Mason | 46bfbb5 | 2010-05-26 11:04:10 -0400 | [diff] [blame] | 6362 | /* |
| 6363 | * returns 1 when the nocow is safe, < 1 on error, 0 if the |
| 6364 | * block must be cow'd |
| 6365 | */ |
Josef Bacik | 0036158 | 2013-08-14 14:02:47 -0400 | [diff] [blame] | 6366 | noinline int can_nocow_extent(struct inode *inode, u64 offset, u64 *len, |
Josef Bacik | 7ee9e44 | 2013-06-21 16:37:03 -0400 | [diff] [blame] | 6367 | u64 *orig_start, u64 *orig_block_len, |
| 6368 | u64 *ram_bytes) |
Chris Mason | 46bfbb5 | 2010-05-26 11:04:10 -0400 | [diff] [blame] | 6369 | { |
Josef Bacik | 0036158 | 2013-08-14 14:02:47 -0400 | [diff] [blame] | 6370 | struct btrfs_trans_handle *trans; |
Chris Mason | 46bfbb5 | 2010-05-26 11:04:10 -0400 | [diff] [blame] | 6371 | struct btrfs_path *path; |
| 6372 | int ret; |
| 6373 | struct extent_buffer *leaf; |
| 6374 | struct btrfs_root *root = BTRFS_I(inode)->root; |
| 6375 | struct btrfs_file_extent_item *fi; |
| 6376 | struct btrfs_key key; |
| 6377 | u64 disk_bytenr; |
| 6378 | u64 backref_offset; |
| 6379 | u64 extent_end; |
| 6380 | u64 num_bytes; |
| 6381 | int slot; |
| 6382 | int found_type; |
Josef Bacik | 7ee9e44 | 2013-06-21 16:37:03 -0400 | [diff] [blame] | 6383 | bool nocow = (BTRFS_I(inode)->flags & BTRFS_INODE_NODATACOW); |
Chris Mason | 46bfbb5 | 2010-05-26 11:04:10 -0400 | [diff] [blame] | 6384 | path = btrfs_alloc_path(); |
| 6385 | if (!path) |
| 6386 | return -ENOMEM; |
| 6387 | |
Josef Bacik | 0036158 | 2013-08-14 14:02:47 -0400 | [diff] [blame] | 6388 | ret = btrfs_lookup_file_extent(NULL, root, path, btrfs_ino(inode), |
Chris Mason | 46bfbb5 | 2010-05-26 11:04:10 -0400 | [diff] [blame] | 6389 | offset, 0); |
| 6390 | if (ret < 0) |
| 6391 | goto out; |
| 6392 | |
| 6393 | slot = path->slots[0]; |
| 6394 | if (ret == 1) { |
| 6395 | if (slot == 0) { |
| 6396 | /* can't find the item, must cow */ |
| 6397 | ret = 0; |
| 6398 | goto out; |
| 6399 | } |
| 6400 | slot--; |
| 6401 | } |
| 6402 | ret = 0; |
| 6403 | leaf = path->nodes[0]; |
| 6404 | btrfs_item_key_to_cpu(leaf, &key, slot); |
Li Zefan | 33345d01 | 2011-04-20 10:31:50 +0800 | [diff] [blame] | 6405 | if (key.objectid != btrfs_ino(inode) || |
Chris Mason | 46bfbb5 | 2010-05-26 11:04:10 -0400 | [diff] [blame] | 6406 | key.type != BTRFS_EXTENT_DATA_KEY) { |
| 6407 | /* not our file or wrong item type, must cow */ |
| 6408 | goto out; |
| 6409 | } |
| 6410 | |
| 6411 | if (key.offset > offset) { |
| 6412 | /* Wrong offset, must cow */ |
| 6413 | goto out; |
| 6414 | } |
| 6415 | |
| 6416 | fi = btrfs_item_ptr(leaf, slot, struct btrfs_file_extent_item); |
| 6417 | found_type = btrfs_file_extent_type(leaf, fi); |
| 6418 | if (found_type != BTRFS_FILE_EXTENT_REG && |
| 6419 | found_type != BTRFS_FILE_EXTENT_PREALLOC) { |
| 6420 | /* not a regular extent, must cow */ |
| 6421 | goto out; |
| 6422 | } |
Josef Bacik | 7ee9e44 | 2013-06-21 16:37:03 -0400 | [diff] [blame] | 6423 | |
| 6424 | if (!nocow && found_type == BTRFS_FILE_EXTENT_REG) |
| 6425 | goto out; |
| 6426 | |
Chris Mason | 46bfbb5 | 2010-05-26 11:04:10 -0400 | [diff] [blame] | 6427 | disk_bytenr = btrfs_file_extent_disk_bytenr(leaf, fi); |
Josef Bacik | 7ee9e44 | 2013-06-21 16:37:03 -0400 | [diff] [blame] | 6428 | if (disk_bytenr == 0) |
| 6429 | goto out; |
| 6430 | |
| 6431 | if (btrfs_file_extent_compression(leaf, fi) || |
| 6432 | btrfs_file_extent_encryption(leaf, fi) || |
| 6433 | btrfs_file_extent_other_encoding(leaf, fi)) |
| 6434 | goto out; |
| 6435 | |
Chris Mason | 46bfbb5 | 2010-05-26 11:04:10 -0400 | [diff] [blame] | 6436 | backref_offset = btrfs_file_extent_offset(leaf, fi); |
| 6437 | |
Josef Bacik | 7ee9e44 | 2013-06-21 16:37:03 -0400 | [diff] [blame] | 6438 | if (orig_start) { |
| 6439 | *orig_start = key.offset - backref_offset; |
| 6440 | *orig_block_len = btrfs_file_extent_disk_num_bytes(leaf, fi); |
| 6441 | *ram_bytes = btrfs_file_extent_ram_bytes(leaf, fi); |
| 6442 | } |
Josef Bacik | eb384b5 | 2013-04-24 16:32:55 -0400 | [diff] [blame] | 6443 | |
Chris Mason | 46bfbb5 | 2010-05-26 11:04:10 -0400 | [diff] [blame] | 6444 | extent_end = key.offset + btrfs_file_extent_num_bytes(leaf, fi); |
Chris Mason | 46bfbb5 | 2010-05-26 11:04:10 -0400 | [diff] [blame] | 6445 | |
| 6446 | if (btrfs_extent_readonly(root, disk_bytenr)) |
| 6447 | goto out; |
Josef Bacik | 1bda19e | 2013-10-18 12:10:36 -0400 | [diff] [blame] | 6448 | btrfs_release_path(path); |
Chris Mason | 46bfbb5 | 2010-05-26 11:04:10 -0400 | [diff] [blame] | 6449 | |
| 6450 | /* |
| 6451 | * look for other files referencing this extent, if we |
| 6452 | * find any we must cow |
| 6453 | */ |
Josef Bacik | 0036158 | 2013-08-14 14:02:47 -0400 | [diff] [blame] | 6454 | trans = btrfs_join_transaction(root); |
| 6455 | if (IS_ERR(trans)) { |
| 6456 | ret = 0; |
Chris Mason | 46bfbb5 | 2010-05-26 11:04:10 -0400 | [diff] [blame] | 6457 | goto out; |
Josef Bacik | 0036158 | 2013-08-14 14:02:47 -0400 | [diff] [blame] | 6458 | } |
| 6459 | |
| 6460 | ret = btrfs_cross_ref_exist(trans, root, btrfs_ino(inode), |
| 6461 | key.offset - backref_offset, disk_bytenr); |
| 6462 | btrfs_end_transaction(trans, root); |
| 6463 | if (ret) { |
| 6464 | ret = 0; |
| 6465 | goto out; |
| 6466 | } |
Chris Mason | 46bfbb5 | 2010-05-26 11:04:10 -0400 | [diff] [blame] | 6467 | |
| 6468 | /* |
| 6469 | * adjust disk_bytenr and num_bytes to cover just the bytes |
| 6470 | * in this extent we are about to write. If there |
| 6471 | * are any csums in that range we have to cow in order |
| 6472 | * to keep the csums correct |
| 6473 | */ |
| 6474 | disk_bytenr += backref_offset; |
| 6475 | disk_bytenr += offset - key.offset; |
Josef Bacik | eb384b5 | 2013-04-24 16:32:55 -0400 | [diff] [blame] | 6476 | num_bytes = min(offset + *len, extent_end) - offset; |
Chris Mason | 46bfbb5 | 2010-05-26 11:04:10 -0400 | [diff] [blame] | 6477 | if (csum_exist_in_range(root, disk_bytenr, num_bytes)) |
| 6478 | goto out; |
| 6479 | /* |
| 6480 | * all of the above have passed, it is safe to overwrite this extent |
| 6481 | * without cow |
| 6482 | */ |
Josef Bacik | eb384b5 | 2013-04-24 16:32:55 -0400 | [diff] [blame] | 6483 | *len = num_bytes; |
Chris Mason | 46bfbb5 | 2010-05-26 11:04:10 -0400 | [diff] [blame] | 6484 | ret = 1; |
| 6485 | out: |
| 6486 | btrfs_free_path(path); |
| 6487 | return ret; |
| 6488 | } |
| 6489 | |
Josef Bacik | eb838e7 | 2012-07-31 16:28:48 -0400 | [diff] [blame] | 6490 | static int lock_extent_direct(struct inode *inode, u64 lockstart, u64 lockend, |
| 6491 | struct extent_state **cached_state, int writing) |
| 6492 | { |
| 6493 | struct btrfs_ordered_extent *ordered; |
| 6494 | int ret = 0; |
| 6495 | |
| 6496 | while (1) { |
| 6497 | lock_extent_bits(&BTRFS_I(inode)->io_tree, lockstart, lockend, |
| 6498 | 0, cached_state); |
| 6499 | /* |
| 6500 | * We're concerned with the entire range that we're going to be |
| 6501 | * doing DIO to, so we need to make sure theres no ordered |
| 6502 | * extents in this range. |
| 6503 | */ |
| 6504 | ordered = btrfs_lookup_ordered_range(inode, lockstart, |
| 6505 | lockend - lockstart + 1); |
| 6506 | |
| 6507 | /* |
| 6508 | * We need to make sure there are no buffered pages in this |
| 6509 | * range either, we could have raced between the invalidate in |
| 6510 | * generic_file_direct_write and locking the extent. The |
| 6511 | * invalidate needs to happen so that reads after a write do not |
| 6512 | * get stale data. |
| 6513 | */ |
| 6514 | if (!ordered && (!writing || |
| 6515 | !test_range_bit(&BTRFS_I(inode)->io_tree, |
| 6516 | lockstart, lockend, EXTENT_UPTODATE, 0, |
| 6517 | *cached_state))) |
| 6518 | break; |
| 6519 | |
| 6520 | unlock_extent_cached(&BTRFS_I(inode)->io_tree, lockstart, lockend, |
| 6521 | cached_state, GFP_NOFS); |
| 6522 | |
| 6523 | if (ordered) { |
| 6524 | btrfs_start_ordered_extent(inode, ordered, 1); |
| 6525 | btrfs_put_ordered_extent(ordered); |
| 6526 | } else { |
| 6527 | /* Screw you mmap */ |
| 6528 | ret = filemap_write_and_wait_range(inode->i_mapping, |
| 6529 | lockstart, |
| 6530 | lockend); |
| 6531 | if (ret) |
| 6532 | break; |
| 6533 | |
| 6534 | /* |
| 6535 | * If we found a page that couldn't be invalidated just |
| 6536 | * fall back to buffered. |
| 6537 | */ |
| 6538 | ret = invalidate_inode_pages2_range(inode->i_mapping, |
| 6539 | lockstart >> PAGE_CACHE_SHIFT, |
| 6540 | lockend >> PAGE_CACHE_SHIFT); |
| 6541 | if (ret) |
| 6542 | break; |
| 6543 | } |
| 6544 | |
| 6545 | cond_resched(); |
| 6546 | } |
| 6547 | |
| 6548 | return ret; |
| 6549 | } |
| 6550 | |
Josef Bacik | 69ffb54 | 2012-09-11 15:40:07 -0400 | [diff] [blame] | 6551 | static struct extent_map *create_pinned_em(struct inode *inode, u64 start, |
| 6552 | u64 len, u64 orig_start, |
| 6553 | u64 block_start, u64 block_len, |
Josef Bacik | cc95bef | 2013-04-04 14:31:27 -0400 | [diff] [blame] | 6554 | u64 orig_block_len, u64 ram_bytes, |
| 6555 | int type) |
Josef Bacik | 69ffb54 | 2012-09-11 15:40:07 -0400 | [diff] [blame] | 6556 | { |
| 6557 | struct extent_map_tree *em_tree; |
| 6558 | struct extent_map *em; |
| 6559 | struct btrfs_root *root = BTRFS_I(inode)->root; |
| 6560 | int ret; |
| 6561 | |
| 6562 | em_tree = &BTRFS_I(inode)->extent_tree; |
| 6563 | em = alloc_extent_map(); |
| 6564 | if (!em) |
| 6565 | return ERR_PTR(-ENOMEM); |
| 6566 | |
| 6567 | em->start = start; |
| 6568 | em->orig_start = orig_start; |
Josef Bacik | 2ab28f3 | 2012-10-12 15:27:49 -0400 | [diff] [blame] | 6569 | em->mod_start = start; |
| 6570 | em->mod_len = len; |
Josef Bacik | 69ffb54 | 2012-09-11 15:40:07 -0400 | [diff] [blame] | 6571 | em->len = len; |
| 6572 | em->block_len = block_len; |
| 6573 | em->block_start = block_start; |
| 6574 | em->bdev = root->fs_info->fs_devices->latest_bdev; |
Josef Bacik | b493968 | 2012-12-03 10:31:19 -0500 | [diff] [blame] | 6575 | em->orig_block_len = orig_block_len; |
Josef Bacik | cc95bef | 2013-04-04 14:31:27 -0400 | [diff] [blame] | 6576 | em->ram_bytes = ram_bytes; |
Josef Bacik | 70c8a91 | 2012-10-11 16:54:30 -0400 | [diff] [blame] | 6577 | em->generation = -1; |
Josef Bacik | 69ffb54 | 2012-09-11 15:40:07 -0400 | [diff] [blame] | 6578 | set_bit(EXTENT_FLAG_PINNED, &em->flags); |
| 6579 | if (type == BTRFS_ORDERED_PREALLOC) |
Josef Bacik | b11e234 | 2012-12-03 10:58:15 -0500 | [diff] [blame] | 6580 | set_bit(EXTENT_FLAG_FILLING, &em->flags); |
Josef Bacik | 69ffb54 | 2012-09-11 15:40:07 -0400 | [diff] [blame] | 6581 | |
| 6582 | do { |
| 6583 | btrfs_drop_extent_cache(inode, em->start, |
| 6584 | em->start + em->len - 1, 0); |
| 6585 | write_lock(&em_tree->lock); |
Josef Bacik | 09a2a8f9 | 2013-04-05 16:51:15 -0400 | [diff] [blame] | 6586 | ret = add_extent_mapping(em_tree, em, 1); |
Josef Bacik | 69ffb54 | 2012-09-11 15:40:07 -0400 | [diff] [blame] | 6587 | write_unlock(&em_tree->lock); |
| 6588 | } while (ret == -EEXIST); |
| 6589 | |
| 6590 | if (ret) { |
| 6591 | free_extent_map(em); |
| 6592 | return ERR_PTR(ret); |
| 6593 | } |
| 6594 | |
| 6595 | return em; |
| 6596 | } |
| 6597 | |
| 6598 | |
Josef Bacik | 4b46fce | 2010-05-23 11:00:55 -0400 | [diff] [blame] | 6599 | static int btrfs_get_blocks_direct(struct inode *inode, sector_t iblock, |
| 6600 | struct buffer_head *bh_result, int create) |
| 6601 | { |
| 6602 | struct extent_map *em; |
| 6603 | struct btrfs_root *root = BTRFS_I(inode)->root; |
Josef Bacik | eb838e7 | 2012-07-31 16:28:48 -0400 | [diff] [blame] | 6604 | struct extent_state *cached_state = NULL; |
Josef Bacik | 4b46fce | 2010-05-23 11:00:55 -0400 | [diff] [blame] | 6605 | u64 start = iblock << inode->i_blkbits; |
Josef Bacik | eb838e7 | 2012-07-31 16:28:48 -0400 | [diff] [blame] | 6606 | u64 lockstart, lockend; |
Josef Bacik | 4b46fce | 2010-05-23 11:00:55 -0400 | [diff] [blame] | 6607 | u64 len = bh_result->b_size; |
Josef Bacik | eb838e7 | 2012-07-31 16:28:48 -0400 | [diff] [blame] | 6608 | int unlock_bits = EXTENT_LOCKED; |
Miao Xie | 0934856 | 2013-02-07 10:12:07 +0000 | [diff] [blame] | 6609 | int ret = 0; |
Josef Bacik | eb838e7 | 2012-07-31 16:28:48 -0400 | [diff] [blame] | 6610 | |
Miao Xie | 172a504 | 2013-02-21 02:48:22 -0700 | [diff] [blame] | 6611 | if (create) |
Josef Bacik | eb838e7 | 2012-07-31 16:28:48 -0400 | [diff] [blame] | 6612 | unlock_bits |= EXTENT_DELALLOC | EXTENT_DIRTY; |
Miao Xie | 172a504 | 2013-02-21 02:48:22 -0700 | [diff] [blame] | 6613 | else |
Josef Bacik | c329861 | 2012-08-03 16:49:19 -0400 | [diff] [blame] | 6614 | len = min_t(u64, len, root->sectorsize); |
Josef Bacik | eb838e7 | 2012-07-31 16:28:48 -0400 | [diff] [blame] | 6615 | |
Josef Bacik | c329861 | 2012-08-03 16:49:19 -0400 | [diff] [blame] | 6616 | lockstart = start; |
| 6617 | lockend = start + len - 1; |
| 6618 | |
Josef Bacik | eb838e7 | 2012-07-31 16:28:48 -0400 | [diff] [blame] | 6619 | /* |
| 6620 | * If this errors out it's because we couldn't invalidate pagecache for |
| 6621 | * this range and we need to fallback to buffered. |
| 6622 | */ |
| 6623 | if (lock_extent_direct(inode, lockstart, lockend, &cached_state, create)) |
| 6624 | return -ENOTBLK; |
| 6625 | |
Josef Bacik | 4b46fce | 2010-05-23 11:00:55 -0400 | [diff] [blame] | 6626 | em = btrfs_get_extent(inode, NULL, 0, start, len, 0); |
Josef Bacik | eb838e7 | 2012-07-31 16:28:48 -0400 | [diff] [blame] | 6627 | if (IS_ERR(em)) { |
| 6628 | ret = PTR_ERR(em); |
| 6629 | goto unlock_err; |
| 6630 | } |
Josef Bacik | 4b46fce | 2010-05-23 11:00:55 -0400 | [diff] [blame] | 6631 | |
| 6632 | /* |
| 6633 | * Ok for INLINE and COMPRESSED extents we need to fallback on buffered |
| 6634 | * io. INLINE is special, and we could probably kludge it in here, but |
| 6635 | * it's still buffered so for safety lets just fall back to the generic |
| 6636 | * buffered path. |
| 6637 | * |
| 6638 | * For COMPRESSED we _have_ to read the entire extent in so we can |
| 6639 | * decompress it, so there will be buffering required no matter what we |
| 6640 | * do, so go ahead and fallback to buffered. |
| 6641 | * |
| 6642 | * We return -ENOTBLK because thats what makes DIO go ahead and go back |
| 6643 | * to buffered IO. Don't blame me, this is the price we pay for using |
| 6644 | * the generic code. |
| 6645 | */ |
| 6646 | if (test_bit(EXTENT_FLAG_COMPRESSED, &em->flags) || |
| 6647 | em->block_start == EXTENT_MAP_INLINE) { |
| 6648 | free_extent_map(em); |
Josef Bacik | eb838e7 | 2012-07-31 16:28:48 -0400 | [diff] [blame] | 6649 | ret = -ENOTBLK; |
| 6650 | goto unlock_err; |
Josef Bacik | 4b46fce | 2010-05-23 11:00:55 -0400 | [diff] [blame] | 6651 | } |
| 6652 | |
| 6653 | /* Just a good old fashioned hole, return */ |
| 6654 | if (!create && (em->block_start == EXTENT_MAP_HOLE || |
| 6655 | test_bit(EXTENT_FLAG_PREALLOC, &em->flags))) { |
| 6656 | free_extent_map(em); |
Josef Bacik | eb838e7 | 2012-07-31 16:28:48 -0400 | [diff] [blame] | 6657 | goto unlock_err; |
Josef Bacik | 4b46fce | 2010-05-23 11:00:55 -0400 | [diff] [blame] | 6658 | } |
| 6659 | |
| 6660 | /* |
| 6661 | * We don't allocate a new extent in the following cases |
| 6662 | * |
| 6663 | * 1) The inode is marked as NODATACOW. In this case we'll just use the |
| 6664 | * existing extent. |
| 6665 | * 2) The extent is marked as PREALLOC. We're good to go here and can |
| 6666 | * just use the extent. |
| 6667 | * |
| 6668 | */ |
Chris Mason | 46bfbb5 | 2010-05-26 11:04:10 -0400 | [diff] [blame] | 6669 | if (!create) { |
Josef Bacik | eb838e7 | 2012-07-31 16:28:48 -0400 | [diff] [blame] | 6670 | len = min(len, em->len - (start - em->start)); |
| 6671 | lockstart = start + len; |
| 6672 | goto unlock; |
Chris Mason | 46bfbb5 | 2010-05-26 11:04:10 -0400 | [diff] [blame] | 6673 | } |
Josef Bacik | 4b46fce | 2010-05-23 11:00:55 -0400 | [diff] [blame] | 6674 | |
| 6675 | if (test_bit(EXTENT_FLAG_PREALLOC, &em->flags) || |
| 6676 | ((BTRFS_I(inode)->flags & BTRFS_INODE_NODATACOW) && |
| 6677 | em->block_start != EXTENT_MAP_HOLE)) { |
Josef Bacik | 4b46fce | 2010-05-23 11:00:55 -0400 | [diff] [blame] | 6678 | int type; |
| 6679 | int ret; |
Josef Bacik | eb384b5 | 2013-04-24 16:32:55 -0400 | [diff] [blame] | 6680 | u64 block_start, orig_start, orig_block_len, ram_bytes; |
Josef Bacik | 4b46fce | 2010-05-23 11:00:55 -0400 | [diff] [blame] | 6681 | |
| 6682 | if (test_bit(EXTENT_FLAG_PREALLOC, &em->flags)) |
| 6683 | type = BTRFS_ORDERED_PREALLOC; |
| 6684 | else |
| 6685 | type = BTRFS_ORDERED_NOCOW; |
Chris Mason | 46bfbb5 | 2010-05-26 11:04:10 -0400 | [diff] [blame] | 6686 | len = min(len, em->len - (start - em->start)); |
Josef Bacik | 4b46fce | 2010-05-23 11:00:55 -0400 | [diff] [blame] | 6687 | block_start = em->block_start + (start - em->start); |
Chris Mason | 46bfbb5 | 2010-05-26 11:04:10 -0400 | [diff] [blame] | 6688 | |
Josef Bacik | 0036158 | 2013-08-14 14:02:47 -0400 | [diff] [blame] | 6689 | if (can_nocow_extent(inode, start, &len, &orig_start, |
Josef Bacik | 7ee9e44 | 2013-06-21 16:37:03 -0400 | [diff] [blame] | 6690 | &orig_block_len, &ram_bytes) == 1) { |
Josef Bacik | 69ffb54 | 2012-09-11 15:40:07 -0400 | [diff] [blame] | 6691 | if (type == BTRFS_ORDERED_PREALLOC) { |
| 6692 | free_extent_map(em); |
| 6693 | em = create_pinned_em(inode, start, len, |
| 6694 | orig_start, |
Josef Bacik | b493968 | 2012-12-03 10:31:19 -0500 | [diff] [blame] | 6695 | block_start, len, |
Josef Bacik | cc95bef | 2013-04-04 14:31:27 -0400 | [diff] [blame] | 6696 | orig_block_len, |
| 6697 | ram_bytes, type); |
Josef Bacik | 0036158 | 2013-08-14 14:02:47 -0400 | [diff] [blame] | 6698 | if (IS_ERR(em)) |
Josef Bacik | 69ffb54 | 2012-09-11 15:40:07 -0400 | [diff] [blame] | 6699 | goto unlock_err; |
Josef Bacik | 69ffb54 | 2012-09-11 15:40:07 -0400 | [diff] [blame] | 6700 | } |
| 6701 | |
Chris Mason | 46bfbb5 | 2010-05-26 11:04:10 -0400 | [diff] [blame] | 6702 | ret = btrfs_add_ordered_extent_dio(inode, start, |
| 6703 | block_start, len, len, type); |
Chris Mason | 46bfbb5 | 2010-05-26 11:04:10 -0400 | [diff] [blame] | 6704 | if (ret) { |
| 6705 | free_extent_map(em); |
Josef Bacik | eb838e7 | 2012-07-31 16:28:48 -0400 | [diff] [blame] | 6706 | goto unlock_err; |
Chris Mason | 46bfbb5 | 2010-05-26 11:04:10 -0400 | [diff] [blame] | 6707 | } |
| 6708 | goto unlock; |
Josef Bacik | 4b46fce | 2010-05-23 11:00:55 -0400 | [diff] [blame] | 6709 | } |
Josef Bacik | 4b46fce | 2010-05-23 11:00:55 -0400 | [diff] [blame] | 6710 | } |
Josef Bacik | 0036158 | 2013-08-14 14:02:47 -0400 | [diff] [blame] | 6711 | |
Chris Mason | 46bfbb5 | 2010-05-26 11:04:10 -0400 | [diff] [blame] | 6712 | /* |
| 6713 | * this will cow the extent, reset the len in case we changed |
| 6714 | * it above |
| 6715 | */ |
| 6716 | len = bh_result->b_size; |
Josef Bacik | 70c8a91 | 2012-10-11 16:54:30 -0400 | [diff] [blame] | 6717 | free_extent_map(em); |
| 6718 | em = btrfs_new_extent_direct(inode, start, len); |
Josef Bacik | eb838e7 | 2012-07-31 16:28:48 -0400 | [diff] [blame] | 6719 | if (IS_ERR(em)) { |
| 6720 | ret = PTR_ERR(em); |
| 6721 | goto unlock_err; |
| 6722 | } |
Chris Mason | 46bfbb5 | 2010-05-26 11:04:10 -0400 | [diff] [blame] | 6723 | len = min(len, em->len - (start - em->start)); |
| 6724 | unlock: |
Josef Bacik | 4b46fce | 2010-05-23 11:00:55 -0400 | [diff] [blame] | 6725 | bh_result->b_blocknr = (em->block_start + (start - em->start)) >> |
| 6726 | inode->i_blkbits; |
Chris Mason | 46bfbb5 | 2010-05-26 11:04:10 -0400 | [diff] [blame] | 6727 | bh_result->b_size = len; |
Josef Bacik | 4b46fce | 2010-05-23 11:00:55 -0400 | [diff] [blame] | 6728 | bh_result->b_bdev = em->bdev; |
| 6729 | set_buffer_mapped(bh_result); |
Josef Bacik | c3473e8 | 2012-06-19 10:59:00 -0400 | [diff] [blame] | 6730 | if (create) { |
| 6731 | if (!test_bit(EXTENT_FLAG_PREALLOC, &em->flags)) |
| 6732 | set_buffer_new(bh_result); |
| 6733 | |
| 6734 | /* |
| 6735 | * Need to update the i_size under the extent lock so buffered |
| 6736 | * readers will get the updated i_size when we unlock. |
| 6737 | */ |
| 6738 | if (start + len > i_size_read(inode)) |
| 6739 | i_size_write(inode, start + len); |
Miao Xie | 0934856 | 2013-02-07 10:12:07 +0000 | [diff] [blame] | 6740 | |
Miao Xie | 172a504 | 2013-02-21 02:48:22 -0700 | [diff] [blame] | 6741 | spin_lock(&BTRFS_I(inode)->lock); |
| 6742 | BTRFS_I(inode)->outstanding_extents++; |
| 6743 | spin_unlock(&BTRFS_I(inode)->lock); |
| 6744 | |
Miao Xie | 0934856 | 2013-02-07 10:12:07 +0000 | [diff] [blame] | 6745 | ret = set_extent_bit(&BTRFS_I(inode)->io_tree, lockstart, |
| 6746 | lockstart + len - 1, EXTENT_DELALLOC, NULL, |
| 6747 | &cached_state, GFP_NOFS); |
| 6748 | BUG_ON(ret); |
Josef Bacik | c3473e8 | 2012-06-19 10:59:00 -0400 | [diff] [blame] | 6749 | } |
Josef Bacik | 4b46fce | 2010-05-23 11:00:55 -0400 | [diff] [blame] | 6750 | |
Josef Bacik | eb838e7 | 2012-07-31 16:28:48 -0400 | [diff] [blame] | 6751 | /* |
| 6752 | * In the case of write we need to clear and unlock the entire range, |
| 6753 | * in the case of read we need to unlock only the end area that we |
| 6754 | * aren't using if there is any left over space. |
| 6755 | */ |
Liu Bo | 24c03fa | 2012-08-22 20:10:38 -0600 | [diff] [blame] | 6756 | if (lockstart < lockend) { |
Miao Xie | 0934856 | 2013-02-07 10:12:07 +0000 | [diff] [blame] | 6757 | clear_extent_bit(&BTRFS_I(inode)->io_tree, lockstart, |
| 6758 | lockend, unlock_bits, 1, 0, |
| 6759 | &cached_state, GFP_NOFS); |
Liu Bo | 24c03fa | 2012-08-22 20:10:38 -0600 | [diff] [blame] | 6760 | } else { |
Josef Bacik | eb838e7 | 2012-07-31 16:28:48 -0400 | [diff] [blame] | 6761 | free_extent_state(cached_state); |
Liu Bo | 24c03fa | 2012-08-22 20:10:38 -0600 | [diff] [blame] | 6762 | } |
Josef Bacik | eb838e7 | 2012-07-31 16:28:48 -0400 | [diff] [blame] | 6763 | |
Josef Bacik | 4b46fce | 2010-05-23 11:00:55 -0400 | [diff] [blame] | 6764 | free_extent_map(em); |
| 6765 | |
| 6766 | return 0; |
Josef Bacik | eb838e7 | 2012-07-31 16:28:48 -0400 | [diff] [blame] | 6767 | |
| 6768 | unlock_err: |
Josef Bacik | eb838e7 | 2012-07-31 16:28:48 -0400 | [diff] [blame] | 6769 | clear_extent_bit(&BTRFS_I(inode)->io_tree, lockstart, lockend, |
| 6770 | unlock_bits, 1, 0, &cached_state, GFP_NOFS); |
| 6771 | return ret; |
Josef Bacik | 4b46fce | 2010-05-23 11:00:55 -0400 | [diff] [blame] | 6772 | } |
| 6773 | |
Josef Bacik | 4b46fce | 2010-05-23 11:00:55 -0400 | [diff] [blame] | 6774 | static void btrfs_endio_direct_read(struct bio *bio, int err) |
| 6775 | { |
Miao Xie | e65e153 | 2010-11-22 03:04:43 +0000 | [diff] [blame] | 6776 | struct btrfs_dio_private *dip = bio->bi_private; |
Josef Bacik | 4b46fce | 2010-05-23 11:00:55 -0400 | [diff] [blame] | 6777 | struct bio_vec *bvec_end = bio->bi_io_vec + bio->bi_vcnt - 1; |
| 6778 | struct bio_vec *bvec = bio->bi_io_vec; |
Josef Bacik | 4b46fce | 2010-05-23 11:00:55 -0400 | [diff] [blame] | 6779 | struct inode *inode = dip->inode; |
| 6780 | struct btrfs_root *root = BTRFS_I(inode)->root; |
Chris Mason | 9be3395 | 2013-05-17 18:30:14 -0400 | [diff] [blame] | 6781 | struct bio *dio_bio; |
Miao Xie | facc8a22 | 2013-07-25 19:22:34 +0800 | [diff] [blame] | 6782 | u32 *csums = (u32 *)dip->csum; |
| 6783 | int index = 0; |
Josef Bacik | 4b46fce | 2010-05-23 11:00:55 -0400 | [diff] [blame] | 6784 | u64 start; |
Josef Bacik | 4b46fce | 2010-05-23 11:00:55 -0400 | [diff] [blame] | 6785 | |
| 6786 | start = dip->logical_offset; |
| 6787 | do { |
| 6788 | if (!(BTRFS_I(inode)->flags & BTRFS_INODE_NODATASUM)) { |
| 6789 | struct page *page = bvec->bv_page; |
| 6790 | char *kaddr; |
| 6791 | u32 csum = ~(u32)0; |
| 6792 | unsigned long flags; |
| 6793 | |
| 6794 | local_irq_save(flags); |
Cong Wang | 7ac687d | 2011-11-25 23:14:28 +0800 | [diff] [blame] | 6795 | kaddr = kmap_atomic(page); |
Liu Bo | b049668 | 2013-03-14 14:57:45 +0000 | [diff] [blame] | 6796 | csum = btrfs_csum_data(kaddr + bvec->bv_offset, |
Josef Bacik | 4b46fce | 2010-05-23 11:00:55 -0400 | [diff] [blame] | 6797 | csum, bvec->bv_len); |
| 6798 | btrfs_csum_final(csum, (char *)&csum); |
Cong Wang | 7ac687d | 2011-11-25 23:14:28 +0800 | [diff] [blame] | 6799 | kunmap_atomic(kaddr); |
Josef Bacik | 4b46fce | 2010-05-23 11:00:55 -0400 | [diff] [blame] | 6800 | local_irq_restore(flags); |
| 6801 | |
| 6802 | flush_dcache_page(bvec->bv_page); |
Miao Xie | facc8a22 | 2013-07-25 19:22:34 +0800 | [diff] [blame] | 6803 | if (csum != csums[index]) { |
| 6804 | 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] | 6805 | btrfs_ino(inode), start, csum, |
| 6806 | csums[index]); |
Josef Bacik | 4b46fce | 2010-05-23 11:00:55 -0400 | [diff] [blame] | 6807 | err = -EIO; |
| 6808 | } |
| 6809 | } |
| 6810 | |
| 6811 | start += bvec->bv_len; |
Josef Bacik | 4b46fce | 2010-05-23 11:00:55 -0400 | [diff] [blame] | 6812 | bvec++; |
Miao Xie | facc8a22 | 2013-07-25 19:22:34 +0800 | [diff] [blame] | 6813 | index++; |
Josef Bacik | 4b46fce | 2010-05-23 11:00:55 -0400 | [diff] [blame] | 6814 | } while (bvec <= bvec_end); |
| 6815 | |
| 6816 | unlock_extent(&BTRFS_I(inode)->io_tree, dip->logical_offset, |
Jeff Mahoney | d008237 | 2012-03-01 14:57:19 +0100 | [diff] [blame] | 6817 | dip->logical_offset + dip->bytes - 1); |
Chris Mason | 9be3395 | 2013-05-17 18:30:14 -0400 | [diff] [blame] | 6818 | dio_bio = dip->dio_bio; |
Josef Bacik | 4b46fce | 2010-05-23 11:00:55 -0400 | [diff] [blame] | 6819 | |
Josef Bacik | 4b46fce | 2010-05-23 11:00:55 -0400 | [diff] [blame] | 6820 | kfree(dip); |
Josef Bacik | c0da7aa | 2011-03-22 11:05:07 -0400 | [diff] [blame] | 6821 | |
| 6822 | /* If we had a csum failure make sure to clear the uptodate flag */ |
| 6823 | if (err) |
Chris Mason | 9be3395 | 2013-05-17 18:30:14 -0400 | [diff] [blame] | 6824 | clear_bit(BIO_UPTODATE, &dio_bio->bi_flags); |
| 6825 | dio_end_io(dio_bio, err); |
| 6826 | bio_put(bio); |
Josef Bacik | 4b46fce | 2010-05-23 11:00:55 -0400 | [diff] [blame] | 6827 | } |
| 6828 | |
| 6829 | static void btrfs_endio_direct_write(struct bio *bio, int err) |
| 6830 | { |
| 6831 | struct btrfs_dio_private *dip = bio->bi_private; |
| 6832 | struct inode *inode = dip->inode; |
| 6833 | struct btrfs_root *root = BTRFS_I(inode)->root; |
Josef Bacik | 4b46fce | 2010-05-23 11:00:55 -0400 | [diff] [blame] | 6834 | struct btrfs_ordered_extent *ordered = NULL; |
Chris Mason | 163cf09 | 2010-11-28 19:56:33 -0500 | [diff] [blame] | 6835 | u64 ordered_offset = dip->logical_offset; |
| 6836 | u64 ordered_bytes = dip->bytes; |
Chris Mason | 9be3395 | 2013-05-17 18:30:14 -0400 | [diff] [blame] | 6837 | struct bio *dio_bio; |
Josef Bacik | 4b46fce | 2010-05-23 11:00:55 -0400 | [diff] [blame] | 6838 | int ret; |
| 6839 | |
| 6840 | if (err) |
| 6841 | goto out_done; |
Chris Mason | 163cf09 | 2010-11-28 19:56:33 -0500 | [diff] [blame] | 6842 | again: |
| 6843 | ret = btrfs_dec_test_first_ordered_pending(inode, &ordered, |
| 6844 | &ordered_offset, |
Josef Bacik | 5fd0204 | 2012-05-02 14:00:54 -0400 | [diff] [blame] | 6845 | ordered_bytes, !err); |
Josef Bacik | 4b46fce | 2010-05-23 11:00:55 -0400 | [diff] [blame] | 6846 | if (!ret) |
Chris Mason | 163cf09 | 2010-11-28 19:56:33 -0500 | [diff] [blame] | 6847 | goto out_test; |
Josef Bacik | 4b46fce | 2010-05-23 11:00:55 -0400 | [diff] [blame] | 6848 | |
Josef Bacik | 5fd0204 | 2012-05-02 14:00:54 -0400 | [diff] [blame] | 6849 | ordered->work.func = finish_ordered_fn; |
| 6850 | ordered->work.flags = 0; |
| 6851 | btrfs_queue_worker(&root->fs_info->endio_write_workers, |
| 6852 | &ordered->work); |
Chris Mason | 163cf09 | 2010-11-28 19:56:33 -0500 | [diff] [blame] | 6853 | out_test: |
| 6854 | /* |
| 6855 | * our bio might span multiple ordered extents. If we haven't |
| 6856 | * completed the accounting for the whole dio, go back and try again |
| 6857 | */ |
| 6858 | if (ordered_offset < dip->logical_offset + dip->bytes) { |
| 6859 | ordered_bytes = dip->logical_offset + dip->bytes - |
| 6860 | ordered_offset; |
Josef Bacik | 5fd0204 | 2012-05-02 14:00:54 -0400 | [diff] [blame] | 6861 | ordered = NULL; |
Chris Mason | 163cf09 | 2010-11-28 19:56:33 -0500 | [diff] [blame] | 6862 | goto again; |
| 6863 | } |
Josef Bacik | 4b46fce | 2010-05-23 11:00:55 -0400 | [diff] [blame] | 6864 | out_done: |
Chris Mason | 9be3395 | 2013-05-17 18:30:14 -0400 | [diff] [blame] | 6865 | dio_bio = dip->dio_bio; |
Josef Bacik | 4b46fce | 2010-05-23 11:00:55 -0400 | [diff] [blame] | 6866 | |
Josef Bacik | 4b46fce | 2010-05-23 11:00:55 -0400 | [diff] [blame] | 6867 | kfree(dip); |
Josef Bacik | c0da7aa | 2011-03-22 11:05:07 -0400 | [diff] [blame] | 6868 | |
| 6869 | /* If we had an error make sure to clear the uptodate flag */ |
| 6870 | if (err) |
Chris Mason | 9be3395 | 2013-05-17 18:30:14 -0400 | [diff] [blame] | 6871 | clear_bit(BIO_UPTODATE, &dio_bio->bi_flags); |
| 6872 | dio_end_io(dio_bio, err); |
| 6873 | bio_put(bio); |
Josef Bacik | 4b46fce | 2010-05-23 11:00:55 -0400 | [diff] [blame] | 6874 | } |
| 6875 | |
Chris Mason | eaf25d9 | 2010-05-25 09:48:28 -0400 | [diff] [blame] | 6876 | static int __btrfs_submit_bio_start_direct_io(struct inode *inode, int rw, |
| 6877 | struct bio *bio, int mirror_num, |
| 6878 | unsigned long bio_flags, u64 offset) |
| 6879 | { |
| 6880 | int ret; |
| 6881 | struct btrfs_root *root = BTRFS_I(inode)->root; |
| 6882 | ret = btrfs_csum_one_bio(root, inode, bio, offset, 1); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 6883 | BUG_ON(ret); /* -ENOMEM */ |
Chris Mason | eaf25d9 | 2010-05-25 09:48:28 -0400 | [diff] [blame] | 6884 | return 0; |
| 6885 | } |
| 6886 | |
Miao Xie | e65e153 | 2010-11-22 03:04:43 +0000 | [diff] [blame] | 6887 | static void btrfs_end_dio_bio(struct bio *bio, int err) |
| 6888 | { |
| 6889 | struct btrfs_dio_private *dip = bio->bi_private; |
| 6890 | |
| 6891 | if (err) { |
Li Zefan | 33345d01 | 2011-04-20 10:31:50 +0800 | [diff] [blame] | 6892 | printk(KERN_ERR "btrfs direct IO failed ino %llu rw %lu " |
Jan Beulich | 3dd1462 | 2010-12-07 14:54:09 +0000 | [diff] [blame] | 6893 | "sector %#Lx len %u err no %d\n", |
Geert Uytterhoeven | c1c9ff7 | 2013-08-20 13:20:07 +0200 | [diff] [blame] | 6894 | btrfs_ino(dip->inode), bio->bi_rw, |
Jan Beulich | 3dd1462 | 2010-12-07 14:54:09 +0000 | [diff] [blame] | 6895 | (unsigned long long)bio->bi_sector, bio->bi_size, err); |
Miao Xie | e65e153 | 2010-11-22 03:04:43 +0000 | [diff] [blame] | 6896 | dip->errors = 1; |
| 6897 | |
| 6898 | /* |
| 6899 | * before atomic variable goto zero, we must make sure |
| 6900 | * dip->errors is perceived to be set. |
| 6901 | */ |
| 6902 | smp_mb__before_atomic_dec(); |
| 6903 | } |
| 6904 | |
| 6905 | /* if there are more bios still pending for this dio, just exit */ |
| 6906 | if (!atomic_dec_and_test(&dip->pending_bios)) |
| 6907 | goto out; |
| 6908 | |
Chris Mason | 9be3395 | 2013-05-17 18:30:14 -0400 | [diff] [blame] | 6909 | if (dip->errors) { |
Miao Xie | e65e153 | 2010-11-22 03:04:43 +0000 | [diff] [blame] | 6910 | bio_io_error(dip->orig_bio); |
Chris Mason | 9be3395 | 2013-05-17 18:30:14 -0400 | [diff] [blame] | 6911 | } else { |
| 6912 | set_bit(BIO_UPTODATE, &dip->dio_bio->bi_flags); |
Miao Xie | e65e153 | 2010-11-22 03:04:43 +0000 | [diff] [blame] | 6913 | bio_endio(dip->orig_bio, 0); |
| 6914 | } |
| 6915 | out: |
| 6916 | bio_put(bio); |
| 6917 | } |
| 6918 | |
| 6919 | static struct bio *btrfs_dio_bio_alloc(struct block_device *bdev, |
| 6920 | u64 first_sector, gfp_t gfp_flags) |
| 6921 | { |
| 6922 | int nr_vecs = bio_get_nr_vecs(bdev); |
| 6923 | return btrfs_bio_alloc(bdev, first_sector, nr_vecs, gfp_flags); |
| 6924 | } |
| 6925 | |
| 6926 | static inline int __btrfs_submit_dio_bio(struct bio *bio, struct inode *inode, |
| 6927 | int rw, u64 file_offset, int skip_sum, |
Josef Bacik | c329861 | 2012-08-03 16:49:19 -0400 | [diff] [blame] | 6928 | int async_submit) |
Miao Xie | e65e153 | 2010-11-22 03:04:43 +0000 | [diff] [blame] | 6929 | { |
Miao Xie | facc8a22 | 2013-07-25 19:22:34 +0800 | [diff] [blame] | 6930 | struct btrfs_dio_private *dip = bio->bi_private; |
Miao Xie | e65e153 | 2010-11-22 03:04:43 +0000 | [diff] [blame] | 6931 | int write = rw & REQ_WRITE; |
| 6932 | struct btrfs_root *root = BTRFS_I(inode)->root; |
| 6933 | int ret; |
| 6934 | |
Josef Bacik | b812ce2 | 2012-11-16 13:56:32 -0500 | [diff] [blame] | 6935 | if (async_submit) |
| 6936 | async_submit = !atomic_read(&BTRFS_I(inode)->sync_writers); |
| 6937 | |
Miao Xie | e65e153 | 2010-11-22 03:04:43 +0000 | [diff] [blame] | 6938 | bio_get(bio); |
Josef Bacik | 5fd0204 | 2012-05-02 14:00:54 -0400 | [diff] [blame] | 6939 | |
| 6940 | if (!write) { |
| 6941 | ret = btrfs_bio_wq_end_io(root->fs_info, bio, 0); |
| 6942 | if (ret) |
| 6943 | goto err; |
| 6944 | } |
Miao Xie | e65e153 | 2010-11-22 03:04:43 +0000 | [diff] [blame] | 6945 | |
Josef Bacik | 1ae3993 | 2011-04-06 14:41:34 -0400 | [diff] [blame] | 6946 | if (skip_sum) |
| 6947 | goto map; |
| 6948 | |
| 6949 | if (write && async_submit) { |
Miao Xie | e65e153 | 2010-11-22 03:04:43 +0000 | [diff] [blame] | 6950 | ret = btrfs_wq_submit_bio(root->fs_info, |
| 6951 | inode, rw, bio, 0, 0, |
| 6952 | file_offset, |
| 6953 | __btrfs_submit_bio_start_direct_io, |
| 6954 | __btrfs_submit_bio_done); |
| 6955 | goto err; |
Josef Bacik | 1ae3993 | 2011-04-06 14:41:34 -0400 | [diff] [blame] | 6956 | } else if (write) { |
| 6957 | /* |
| 6958 | * If we aren't doing async submit, calculate the csum of the |
| 6959 | * bio now. |
| 6960 | */ |
| 6961 | ret = btrfs_csum_one_bio(root, inode, bio, file_offset, 1); |
| 6962 | if (ret) |
| 6963 | goto err; |
Tsutomu Itoh | c2db107 | 2011-03-01 06:48:31 +0000 | [diff] [blame] | 6964 | } else if (!skip_sum) { |
Miao Xie | facc8a22 | 2013-07-25 19:22:34 +0800 | [diff] [blame] | 6965 | ret = btrfs_lookup_bio_sums_dio(root, inode, dip, bio, |
| 6966 | file_offset); |
Tsutomu Itoh | c2db107 | 2011-03-01 06:48:31 +0000 | [diff] [blame] | 6967 | if (ret) |
| 6968 | goto err; |
| 6969 | } |
Miao Xie | e65e153 | 2010-11-22 03:04:43 +0000 | [diff] [blame] | 6970 | |
Josef Bacik | 1ae3993 | 2011-04-06 14:41:34 -0400 | [diff] [blame] | 6971 | map: |
| 6972 | ret = btrfs_map_bio(root, rw, bio, 0, async_submit); |
Miao Xie | e65e153 | 2010-11-22 03:04:43 +0000 | [diff] [blame] | 6973 | err: |
| 6974 | bio_put(bio); |
| 6975 | return ret; |
| 6976 | } |
| 6977 | |
| 6978 | static int btrfs_submit_direct_hook(int rw, struct btrfs_dio_private *dip, |
| 6979 | int skip_sum) |
| 6980 | { |
| 6981 | struct inode *inode = dip->inode; |
| 6982 | struct btrfs_root *root = BTRFS_I(inode)->root; |
Miao Xie | e65e153 | 2010-11-22 03:04:43 +0000 | [diff] [blame] | 6983 | struct bio *bio; |
| 6984 | struct bio *orig_bio = dip->orig_bio; |
| 6985 | struct bio_vec *bvec = orig_bio->bi_io_vec; |
| 6986 | u64 start_sector = orig_bio->bi_sector; |
| 6987 | u64 file_offset = dip->logical_offset; |
| 6988 | u64 submit_len = 0; |
| 6989 | u64 map_length; |
| 6990 | int nr_pages = 0; |
Miao Xie | e65e153 | 2010-11-22 03:04:43 +0000 | [diff] [blame] | 6991 | int ret = 0; |
Josef Bacik | 1ae3993 | 2011-04-06 14:41:34 -0400 | [diff] [blame] | 6992 | int async_submit = 0; |
Miao Xie | e65e153 | 2010-11-22 03:04:43 +0000 | [diff] [blame] | 6993 | |
Miao Xie | e65e153 | 2010-11-22 03:04:43 +0000 | [diff] [blame] | 6994 | map_length = orig_bio->bi_size; |
David Woodhouse | 53b381b | 2013-01-29 18:40:14 -0500 | [diff] [blame] | 6995 | ret = btrfs_map_block(root->fs_info, rw, start_sector << 9, |
Miao Xie | e65e153 | 2010-11-22 03:04:43 +0000 | [diff] [blame] | 6996 | &map_length, NULL, 0); |
| 6997 | if (ret) { |
Josef Bacik | 64728bb | 2011-04-25 19:43:52 -0400 | [diff] [blame] | 6998 | bio_put(orig_bio); |
Miao Xie | e65e153 | 2010-11-22 03:04:43 +0000 | [diff] [blame] | 6999 | return -EIO; |
| 7000 | } |
Miao Xie | facc8a22 | 2013-07-25 19:22:34 +0800 | [diff] [blame] | 7001 | |
Josef Bacik | 02f57c7 | 2011-04-06 14:25:44 -0400 | [diff] [blame] | 7002 | if (map_length >= orig_bio->bi_size) { |
| 7003 | bio = orig_bio; |
| 7004 | goto submit; |
| 7005 | } |
| 7006 | |
David Woodhouse | 53b381b | 2013-01-29 18:40:14 -0500 | [diff] [blame] | 7007 | /* async crcs make it difficult to collect full stripe writes. */ |
| 7008 | if (btrfs_get_alloc_profile(root, 1) & |
| 7009 | (BTRFS_BLOCK_GROUP_RAID5 | BTRFS_BLOCK_GROUP_RAID6)) |
| 7010 | async_submit = 0; |
| 7011 | else |
| 7012 | async_submit = 1; |
| 7013 | |
Josef Bacik | 02f57c7 | 2011-04-06 14:25:44 -0400 | [diff] [blame] | 7014 | bio = btrfs_dio_bio_alloc(orig_bio->bi_bdev, start_sector, GFP_NOFS); |
| 7015 | if (!bio) |
| 7016 | return -ENOMEM; |
| 7017 | bio->bi_private = dip; |
| 7018 | bio->bi_end_io = btrfs_end_dio_bio; |
| 7019 | atomic_inc(&dip->pending_bios); |
| 7020 | |
Miao Xie | e65e153 | 2010-11-22 03:04:43 +0000 | [diff] [blame] | 7021 | while (bvec <= (orig_bio->bi_io_vec + orig_bio->bi_vcnt - 1)) { |
| 7022 | if (unlikely(map_length < submit_len + bvec->bv_len || |
| 7023 | bio_add_page(bio, bvec->bv_page, bvec->bv_len, |
| 7024 | bvec->bv_offset) < bvec->bv_len)) { |
| 7025 | /* |
| 7026 | * inc the count before we submit the bio so |
| 7027 | * we know the end IO handler won't happen before |
| 7028 | * we inc the count. Otherwise, the dip might get freed |
| 7029 | * before we're done setting it up |
| 7030 | */ |
| 7031 | atomic_inc(&dip->pending_bios); |
| 7032 | ret = __btrfs_submit_dio_bio(bio, inode, rw, |
| 7033 | file_offset, skip_sum, |
Josef Bacik | c329861 | 2012-08-03 16:49:19 -0400 | [diff] [blame] | 7034 | async_submit); |
Miao Xie | e65e153 | 2010-11-22 03:04:43 +0000 | [diff] [blame] | 7035 | if (ret) { |
| 7036 | bio_put(bio); |
| 7037 | atomic_dec(&dip->pending_bios); |
| 7038 | goto out_err; |
| 7039 | } |
| 7040 | |
Miao Xie | e65e153 | 2010-11-22 03:04:43 +0000 | [diff] [blame] | 7041 | start_sector += submit_len >> 9; |
| 7042 | file_offset += submit_len; |
| 7043 | |
| 7044 | submit_len = 0; |
| 7045 | nr_pages = 0; |
| 7046 | |
| 7047 | bio = btrfs_dio_bio_alloc(orig_bio->bi_bdev, |
| 7048 | start_sector, GFP_NOFS); |
| 7049 | if (!bio) |
| 7050 | goto out_err; |
| 7051 | bio->bi_private = dip; |
| 7052 | bio->bi_end_io = btrfs_end_dio_bio; |
| 7053 | |
| 7054 | map_length = orig_bio->bi_size; |
David Woodhouse | 53b381b | 2013-01-29 18:40:14 -0500 | [diff] [blame] | 7055 | ret = btrfs_map_block(root->fs_info, rw, |
Stefan Behrens | 3ec706c | 2012-11-05 15:46:42 +0100 | [diff] [blame] | 7056 | start_sector << 9, |
Miao Xie | e65e153 | 2010-11-22 03:04:43 +0000 | [diff] [blame] | 7057 | &map_length, NULL, 0); |
| 7058 | if (ret) { |
| 7059 | bio_put(bio); |
| 7060 | goto out_err; |
| 7061 | } |
| 7062 | } else { |
| 7063 | submit_len += bvec->bv_len; |
| 7064 | nr_pages ++; |
| 7065 | bvec++; |
| 7066 | } |
| 7067 | } |
| 7068 | |
Josef Bacik | 02f57c7 | 2011-04-06 14:25:44 -0400 | [diff] [blame] | 7069 | submit: |
Miao Xie | e65e153 | 2010-11-22 03:04:43 +0000 | [diff] [blame] | 7070 | ret = __btrfs_submit_dio_bio(bio, inode, rw, file_offset, skip_sum, |
Josef Bacik | c329861 | 2012-08-03 16:49:19 -0400 | [diff] [blame] | 7071 | async_submit); |
Miao Xie | e65e153 | 2010-11-22 03:04:43 +0000 | [diff] [blame] | 7072 | if (!ret) |
| 7073 | return 0; |
| 7074 | |
| 7075 | bio_put(bio); |
| 7076 | out_err: |
| 7077 | dip->errors = 1; |
| 7078 | /* |
| 7079 | * before atomic variable goto zero, we must |
| 7080 | * make sure dip->errors is perceived to be set. |
| 7081 | */ |
| 7082 | smp_mb__before_atomic_dec(); |
| 7083 | if (atomic_dec_and_test(&dip->pending_bios)) |
| 7084 | bio_io_error(dip->orig_bio); |
| 7085 | |
| 7086 | /* bio_end_io() will handle error, so we needn't return it */ |
| 7087 | return 0; |
| 7088 | } |
| 7089 | |
Chris Mason | 9be3395 | 2013-05-17 18:30:14 -0400 | [diff] [blame] | 7090 | static void btrfs_submit_direct(int rw, struct bio *dio_bio, |
| 7091 | struct inode *inode, loff_t file_offset) |
Josef Bacik | 4b46fce | 2010-05-23 11:00:55 -0400 | [diff] [blame] | 7092 | { |
| 7093 | struct btrfs_root *root = BTRFS_I(inode)->root; |
| 7094 | struct btrfs_dio_private *dip; |
Chris Mason | 9be3395 | 2013-05-17 18:30:14 -0400 | [diff] [blame] | 7095 | struct bio *io_bio; |
Josef Bacik | 4b46fce | 2010-05-23 11:00:55 -0400 | [diff] [blame] | 7096 | int skip_sum; |
Miao Xie | facc8a22 | 2013-07-25 19:22:34 +0800 | [diff] [blame] | 7097 | int sum_len; |
Christoph Hellwig | 7b6d91d | 2010-08-07 18:20:39 +0200 | [diff] [blame] | 7098 | int write = rw & REQ_WRITE; |
Josef Bacik | 4b46fce | 2010-05-23 11:00:55 -0400 | [diff] [blame] | 7099 | int ret = 0; |
Miao Xie | facc8a22 | 2013-07-25 19:22:34 +0800 | [diff] [blame] | 7100 | u16 csum_size; |
Josef Bacik | 4b46fce | 2010-05-23 11:00:55 -0400 | [diff] [blame] | 7101 | |
| 7102 | skip_sum = BTRFS_I(inode)->flags & BTRFS_INODE_NODATASUM; |
| 7103 | |
Chris Mason | 9be3395 | 2013-05-17 18:30:14 -0400 | [diff] [blame] | 7104 | io_bio = btrfs_bio_clone(dio_bio, GFP_NOFS); |
Chris Mason | 9be3395 | 2013-05-17 18:30:14 -0400 | [diff] [blame] | 7105 | if (!io_bio) { |
Josef Bacik | 4b46fce | 2010-05-23 11:00:55 -0400 | [diff] [blame] | 7106 | ret = -ENOMEM; |
| 7107 | goto free_ordered; |
| 7108 | } |
Josef Bacik | 4b46fce | 2010-05-23 11:00:55 -0400 | [diff] [blame] | 7109 | |
Miao Xie | facc8a22 | 2013-07-25 19:22:34 +0800 | [diff] [blame] | 7110 | if (!skip_sum && !write) { |
| 7111 | csum_size = btrfs_super_csum_size(root->fs_info->super_copy); |
| 7112 | sum_len = dio_bio->bi_size >> inode->i_sb->s_blocksize_bits; |
| 7113 | sum_len *= csum_size; |
| 7114 | } else { |
| 7115 | sum_len = 0; |
| 7116 | } |
| 7117 | |
| 7118 | dip = kmalloc(sizeof(*dip) + sum_len, GFP_NOFS); |
Chris Mason | 9be3395 | 2013-05-17 18:30:14 -0400 | [diff] [blame] | 7119 | if (!dip) { |
| 7120 | ret = -ENOMEM; |
| 7121 | goto free_io_bio; |
| 7122 | } |
| 7123 | |
| 7124 | dip->private = dio_bio->bi_private; |
Josef Bacik | 4b46fce | 2010-05-23 11:00:55 -0400 | [diff] [blame] | 7125 | dip->inode = inode; |
| 7126 | dip->logical_offset = file_offset; |
Miao Xie | e6da5d2e | 2013-06-19 18:19:17 +0800 | [diff] [blame] | 7127 | dip->bytes = dio_bio->bi_size; |
Chris Mason | 9be3395 | 2013-05-17 18:30:14 -0400 | [diff] [blame] | 7128 | dip->disk_bytenr = (u64)dio_bio->bi_sector << 9; |
| 7129 | io_bio->bi_private = dip; |
Miao Xie | e65e153 | 2010-11-22 03:04:43 +0000 | [diff] [blame] | 7130 | dip->errors = 0; |
Chris Mason | 9be3395 | 2013-05-17 18:30:14 -0400 | [diff] [blame] | 7131 | dip->orig_bio = io_bio; |
| 7132 | dip->dio_bio = dio_bio; |
Miao Xie | e65e153 | 2010-11-22 03:04:43 +0000 | [diff] [blame] | 7133 | atomic_set(&dip->pending_bios, 0); |
Josef Bacik | 4b46fce | 2010-05-23 11:00:55 -0400 | [diff] [blame] | 7134 | |
| 7135 | if (write) |
Chris Mason | 9be3395 | 2013-05-17 18:30:14 -0400 | [diff] [blame] | 7136 | io_bio->bi_end_io = btrfs_endio_direct_write; |
Josef Bacik | 4b46fce | 2010-05-23 11:00:55 -0400 | [diff] [blame] | 7137 | else |
Chris Mason | 9be3395 | 2013-05-17 18:30:14 -0400 | [diff] [blame] | 7138 | io_bio->bi_end_io = btrfs_endio_direct_read; |
Josef Bacik | 4b46fce | 2010-05-23 11:00:55 -0400 | [diff] [blame] | 7139 | |
Miao Xie | e65e153 | 2010-11-22 03:04:43 +0000 | [diff] [blame] | 7140 | ret = btrfs_submit_direct_hook(rw, dip, skip_sum); |
| 7141 | if (!ret) |
Chris Mason | eaf25d9 | 2010-05-25 09:48:28 -0400 | [diff] [blame] | 7142 | return; |
Chris Mason | 9be3395 | 2013-05-17 18:30:14 -0400 | [diff] [blame] | 7143 | |
| 7144 | free_io_bio: |
| 7145 | bio_put(io_bio); |
| 7146 | |
Josef Bacik | 4b46fce | 2010-05-23 11:00:55 -0400 | [diff] [blame] | 7147 | free_ordered: |
| 7148 | /* |
| 7149 | * If this is a write, we need to clean up the reserved space and kill |
| 7150 | * the ordered extent. |
| 7151 | */ |
| 7152 | if (write) { |
| 7153 | struct btrfs_ordered_extent *ordered; |
Josef Bacik | 955256f | 2010-11-19 09:41:10 -0500 | [diff] [blame] | 7154 | ordered = btrfs_lookup_ordered_extent(inode, file_offset); |
Josef Bacik | 4b46fce | 2010-05-23 11:00:55 -0400 | [diff] [blame] | 7155 | if (!test_bit(BTRFS_ORDERED_PREALLOC, &ordered->flags) && |
| 7156 | !test_bit(BTRFS_ORDERED_NOCOW, &ordered->flags)) |
| 7157 | btrfs_free_reserved_extent(root, ordered->start, |
| 7158 | ordered->disk_len); |
| 7159 | btrfs_put_ordered_extent(ordered); |
| 7160 | btrfs_put_ordered_extent(ordered); |
| 7161 | } |
Chris Mason | 9be3395 | 2013-05-17 18:30:14 -0400 | [diff] [blame] | 7162 | bio_endio(dio_bio, ret); |
Josef Bacik | 4b46fce | 2010-05-23 11:00:55 -0400 | [diff] [blame] | 7163 | } |
| 7164 | |
Chris Mason | 5a5f79b | 2010-05-26 21:33:37 -0400 | [diff] [blame] | 7165 | static ssize_t check_direct_IO(struct btrfs_root *root, int rw, struct kiocb *iocb, |
| 7166 | const struct iovec *iov, loff_t offset, |
| 7167 | unsigned long nr_segs) |
| 7168 | { |
| 7169 | int seg; |
Josef Bacik | a1b75f7 | 2011-04-08 15:51:18 +0000 | [diff] [blame] | 7170 | int i; |
Chris Mason | 5a5f79b | 2010-05-26 21:33:37 -0400 | [diff] [blame] | 7171 | size_t size; |
| 7172 | unsigned long addr; |
| 7173 | unsigned blocksize_mask = root->sectorsize - 1; |
| 7174 | ssize_t retval = -EINVAL; |
| 7175 | loff_t end = offset; |
| 7176 | |
| 7177 | if (offset & blocksize_mask) |
| 7178 | goto out; |
| 7179 | |
| 7180 | /* Check the memory alignment. Blocks cannot straddle pages */ |
| 7181 | for (seg = 0; seg < nr_segs; seg++) { |
| 7182 | addr = (unsigned long)iov[seg].iov_base; |
| 7183 | size = iov[seg].iov_len; |
| 7184 | end += size; |
Josef Bacik | a1b75f7 | 2011-04-08 15:51:18 +0000 | [diff] [blame] | 7185 | if ((addr & blocksize_mask) || (size & blocksize_mask)) |
Chris Mason | 5a5f79b | 2010-05-26 21:33:37 -0400 | [diff] [blame] | 7186 | goto out; |
Josef Bacik | a1b75f7 | 2011-04-08 15:51:18 +0000 | [diff] [blame] | 7187 | |
| 7188 | /* If this is a write we don't need to check anymore */ |
| 7189 | if (rw & WRITE) |
| 7190 | continue; |
| 7191 | |
| 7192 | /* |
| 7193 | * Check to make sure we don't have duplicate iov_base's in this |
| 7194 | * iovec, if so return EINVAL, otherwise we'll get csum errors |
| 7195 | * when reading back. |
| 7196 | */ |
| 7197 | for (i = seg + 1; i < nr_segs; i++) { |
| 7198 | if (iov[seg].iov_base == iov[i].iov_base) |
| 7199 | goto out; |
| 7200 | } |
Chris Mason | 5a5f79b | 2010-05-26 21:33:37 -0400 | [diff] [blame] | 7201 | } |
| 7202 | retval = 0; |
| 7203 | out: |
| 7204 | return retval; |
| 7205 | } |
Josef Bacik | eb838e7 | 2012-07-31 16:28:48 -0400 | [diff] [blame] | 7206 | |
Chris Mason | 1643298 | 2008-04-10 10:23:21 -0400 | [diff] [blame] | 7207 | static ssize_t btrfs_direct_IO(int rw, struct kiocb *iocb, |
| 7208 | const struct iovec *iov, loff_t offset, |
| 7209 | unsigned long nr_segs) |
| 7210 | { |
Josef Bacik | 4b46fce | 2010-05-23 11:00:55 -0400 | [diff] [blame] | 7211 | struct file *file = iocb->ki_filp; |
| 7212 | struct inode *inode = file->f_mapping->host; |
Miao Xie | 0934856 | 2013-02-07 10:12:07 +0000 | [diff] [blame] | 7213 | size_t count = 0; |
Miao Xie | 2e60a51 | 2013-02-08 07:01:08 +0000 | [diff] [blame] | 7214 | int flags = 0; |
Miao Xie | 38851cc | 2013-02-08 07:04:11 +0000 | [diff] [blame] | 7215 | bool wakeup = true; |
| 7216 | bool relock = false; |
Miao Xie | 0934856 | 2013-02-07 10:12:07 +0000 | [diff] [blame] | 7217 | ssize_t ret; |
Josef Bacik | 4b46fce | 2010-05-23 11:00:55 -0400 | [diff] [blame] | 7218 | |
Chris Mason | 5a5f79b | 2010-05-26 21:33:37 -0400 | [diff] [blame] | 7219 | if (check_direct_IO(BTRFS_I(inode)->root, rw, iocb, iov, |
Josef Bacik | eb838e7 | 2012-07-31 16:28:48 -0400 | [diff] [blame] | 7220 | offset, nr_segs)) |
Chris Mason | 5a5f79b | 2010-05-26 21:33:37 -0400 | [diff] [blame] | 7221 | return 0; |
Chris Mason | 5a5f79b | 2010-05-26 21:33:37 -0400 | [diff] [blame] | 7222 | |
Miao Xie | 38851cc | 2013-02-08 07:04:11 +0000 | [diff] [blame] | 7223 | atomic_inc(&inode->i_dio_count); |
| 7224 | smp_mb__after_atomic_inc(); |
| 7225 | |
Josef Bacik | 0e267c4 | 2013-07-02 10:38:02 -0400 | [diff] [blame] | 7226 | /* |
| 7227 | * The generic stuff only does filemap_write_and_wait_range, which isn't |
| 7228 | * enough if we've written compressed pages to this area, so we need to |
| 7229 | * call btrfs_wait_ordered_range to make absolutely sure that any |
| 7230 | * outstanding dirty pages are on disk. |
| 7231 | */ |
| 7232 | count = iov_length(iov, nr_segs); |
| 7233 | btrfs_wait_ordered_range(inode, offset, count); |
| 7234 | |
Miao Xie | 0934856 | 2013-02-07 10:12:07 +0000 | [diff] [blame] | 7235 | if (rw & WRITE) { |
Miao Xie | 38851cc | 2013-02-08 07:04:11 +0000 | [diff] [blame] | 7236 | /* |
| 7237 | * If the write DIO is beyond the EOF, we need update |
| 7238 | * the isize, but it is protected by i_mutex. So we can |
| 7239 | * not unlock the i_mutex at this case. |
| 7240 | */ |
| 7241 | if (offset + count <= inode->i_size) { |
| 7242 | mutex_unlock(&inode->i_mutex); |
| 7243 | relock = true; |
| 7244 | } |
Miao Xie | 0934856 | 2013-02-07 10:12:07 +0000 | [diff] [blame] | 7245 | ret = btrfs_delalloc_reserve_space(inode, count); |
| 7246 | if (ret) |
Miao Xie | 38851cc | 2013-02-08 07:04:11 +0000 | [diff] [blame] | 7247 | goto out; |
| 7248 | } else if (unlikely(test_bit(BTRFS_INODE_READDIO_NEED_LOCK, |
| 7249 | &BTRFS_I(inode)->runtime_flags))) { |
| 7250 | inode_dio_done(inode); |
| 7251 | flags = DIO_LOCKING | DIO_SKIP_HOLES; |
| 7252 | wakeup = false; |
Miao Xie | 0934856 | 2013-02-07 10:12:07 +0000 | [diff] [blame] | 7253 | } |
| 7254 | |
| 7255 | ret = __blockdev_direct_IO(rw, iocb, inode, |
| 7256 | BTRFS_I(inode)->root->fs_info->fs_devices->latest_bdev, |
| 7257 | iov, offset, nr_segs, btrfs_get_blocks_direct, NULL, |
Miao Xie | 2e60a51 | 2013-02-08 07:01:08 +0000 | [diff] [blame] | 7258 | btrfs_submit_direct, flags); |
Miao Xie | 0934856 | 2013-02-07 10:12:07 +0000 | [diff] [blame] | 7259 | if (rw & WRITE) { |
| 7260 | if (ret < 0 && ret != -EIOCBQUEUED) |
| 7261 | btrfs_delalloc_release_space(inode, count); |
Miao Xie | 172a504 | 2013-02-21 02:48:22 -0700 | [diff] [blame] | 7262 | else if (ret >= 0 && (size_t)ret < count) |
Miao Xie | 0934856 | 2013-02-07 10:12:07 +0000 | [diff] [blame] | 7263 | btrfs_delalloc_release_space(inode, |
| 7264 | count - (size_t)ret); |
Miao Xie | 172a504 | 2013-02-21 02:48:22 -0700 | [diff] [blame] | 7265 | else |
| 7266 | btrfs_delalloc_release_metadata(inode, 0); |
Miao Xie | 0934856 | 2013-02-07 10:12:07 +0000 | [diff] [blame] | 7267 | } |
Miao Xie | 38851cc | 2013-02-08 07:04:11 +0000 | [diff] [blame] | 7268 | out: |
Miao Xie | 2e60a51 | 2013-02-08 07:01:08 +0000 | [diff] [blame] | 7269 | if (wakeup) |
| 7270 | inode_dio_done(inode); |
Miao Xie | 38851cc | 2013-02-08 07:04:11 +0000 | [diff] [blame] | 7271 | if (relock) |
| 7272 | mutex_lock(&inode->i_mutex); |
Miao Xie | 0934856 | 2013-02-07 10:12:07 +0000 | [diff] [blame] | 7273 | |
| 7274 | return ret; |
Chris Mason | 1643298 | 2008-04-10 10:23:21 -0400 | [diff] [blame] | 7275 | } |
| 7276 | |
Tsutomu Itoh | 05dadc0 | 2012-11-29 05:08:26 +0000 | [diff] [blame] | 7277 | #define BTRFS_FIEMAP_FLAGS (FIEMAP_FLAG_SYNC) |
| 7278 | |
Yehuda Sadeh | 1506fcc | 2009-01-21 14:39:14 -0500 | [diff] [blame] | 7279 | static int btrfs_fiemap(struct inode *inode, struct fiemap_extent_info *fieinfo, |
| 7280 | __u64 start, __u64 len) |
| 7281 | { |
Tsutomu Itoh | 05dadc0 | 2012-11-29 05:08:26 +0000 | [diff] [blame] | 7282 | int ret; |
| 7283 | |
| 7284 | ret = fiemap_check_flags(fieinfo, BTRFS_FIEMAP_FLAGS); |
| 7285 | if (ret) |
| 7286 | return ret; |
| 7287 | |
Chris Mason | ec29ed5 | 2011-02-23 16:23:20 -0500 | [diff] [blame] | 7288 | return extent_fiemap(inode, fieinfo, start, len, btrfs_get_extent_fiemap); |
Yehuda Sadeh | 1506fcc | 2009-01-21 14:39:14 -0500 | [diff] [blame] | 7289 | } |
| 7290 | |
Chris Mason | 9ebefb18 | 2007-06-15 13:50:00 -0400 | [diff] [blame] | 7291 | int btrfs_readpage(struct file *file, struct page *page) |
| 7292 | { |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 7293 | struct extent_io_tree *tree; |
| 7294 | tree = &BTRFS_I(page->mapping->host)->io_tree; |
Jan Schmidt | 8ddc7d9 | 2011-06-13 20:02:58 +0200 | [diff] [blame] | 7295 | return extent_read_full_page(tree, page, btrfs_get_extent, 0); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 7296 | } |
Chris Mason | 1832a6d | 2007-12-21 16:27:21 -0500 | [diff] [blame] | 7297 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 7298 | static int btrfs_writepage(struct page *page, struct writeback_control *wbc) |
| 7299 | { |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 7300 | struct extent_io_tree *tree; |
Chris Mason | b888db2 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 7301 | |
| 7302 | |
| 7303 | if (current->flags & PF_MEMALLOC) { |
| 7304 | redirty_page_for_writepage(wbc, page); |
| 7305 | unlock_page(page); |
| 7306 | return 0; |
| 7307 | } |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 7308 | tree = &BTRFS_I(page->mapping->host)->io_tree; |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 7309 | return extent_write_full_page(tree, page, btrfs_get_extent, wbc); |
| 7310 | } |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 7311 | |
Eric Sandeen | 48a3b63 | 2013-04-25 20:41:01 +0000 | [diff] [blame] | 7312 | static int btrfs_writepages(struct address_space *mapping, |
| 7313 | struct writeback_control *wbc) |
Chris Mason | b293f02e | 2007-11-01 19:45:34 -0400 | [diff] [blame] | 7314 | { |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 7315 | struct extent_io_tree *tree; |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 7316 | |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 7317 | tree = &BTRFS_I(mapping->host)->io_tree; |
Chris Mason | b293f02e | 2007-11-01 19:45:34 -0400 | [diff] [blame] | 7318 | return extent_writepages(tree, mapping, btrfs_get_extent, wbc); |
| 7319 | } |
| 7320 | |
Chris Mason | 3ab2fb5 | 2007-11-08 10:59:22 -0500 | [diff] [blame] | 7321 | static int |
| 7322 | btrfs_readpages(struct file *file, struct address_space *mapping, |
| 7323 | struct list_head *pages, unsigned nr_pages) |
| 7324 | { |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 7325 | struct extent_io_tree *tree; |
| 7326 | tree = &BTRFS_I(mapping->host)->io_tree; |
Chris Mason | 3ab2fb5 | 2007-11-08 10:59:22 -0500 | [diff] [blame] | 7327 | return extent_readpages(tree, mapping, pages, nr_pages, |
| 7328 | btrfs_get_extent); |
| 7329 | } |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 7330 | static int __btrfs_releasepage(struct page *page, gfp_t gfp_flags) |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 7331 | { |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 7332 | struct extent_io_tree *tree; |
| 7333 | struct extent_map_tree *map; |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 7334 | int ret; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 7335 | |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 7336 | tree = &BTRFS_I(page->mapping->host)->io_tree; |
| 7337 | map = &BTRFS_I(page->mapping->host)->extent_tree; |
Chris Mason | 70dec80 | 2008-01-29 09:59:12 -0500 | [diff] [blame] | 7338 | ret = try_release_extent_mapping(map, tree, page, gfp_flags); |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 7339 | if (ret == 1) { |
| 7340 | ClearPagePrivate(page); |
| 7341 | set_page_private(page, 0); |
| 7342 | page_cache_release(page); |
| 7343 | } |
| 7344 | return ret; |
| 7345 | } |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 7346 | |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 7347 | static int btrfs_releasepage(struct page *page, gfp_t gfp_flags) |
| 7348 | { |
Chris Mason | 98509cf | 2008-09-11 15:51:43 -0400 | [diff] [blame] | 7349 | if (PageWriteback(page) || PageDirty(page)) |
| 7350 | return 0; |
Yan Zheng | b335b00 | 2009-02-12 10:06:04 -0500 | [diff] [blame] | 7351 | return __btrfs_releasepage(page, gfp_flags & GFP_NOFS); |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 7352 | } |
| 7353 | |
Lukas Czerner | d47992f | 2013-05-21 23:17:23 -0400 | [diff] [blame] | 7354 | static void btrfs_invalidatepage(struct page *page, unsigned int offset, |
| 7355 | unsigned int length) |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 7356 | { |
Josef Bacik | 5fd0204 | 2012-05-02 14:00:54 -0400 | [diff] [blame] | 7357 | struct inode *inode = page->mapping->host; |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 7358 | struct extent_io_tree *tree; |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 7359 | struct btrfs_ordered_extent *ordered; |
Josef Bacik | 2ac55d4 | 2010-02-03 19:33:23 +0000 | [diff] [blame] | 7360 | struct extent_state *cached_state = NULL; |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 7361 | u64 page_start = page_offset(page); |
| 7362 | u64 page_end = page_start + PAGE_CACHE_SIZE - 1; |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 7363 | |
Chris Mason | 8b62b72 | 2009-09-02 16:53:46 -0400 | [diff] [blame] | 7364 | /* |
| 7365 | * we have the page locked, so new writeback can't start, |
| 7366 | * and the dirty bit won't be cleared while we are here. |
| 7367 | * |
| 7368 | * Wait for IO on this page so that we can safely clear |
| 7369 | * the PagePrivate2 bit and do ordered accounting |
| 7370 | */ |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 7371 | wait_on_page_writeback(page); |
Chris Mason | 8b62b72 | 2009-09-02 16:53:46 -0400 | [diff] [blame] | 7372 | |
Josef Bacik | 5fd0204 | 2012-05-02 14:00:54 -0400 | [diff] [blame] | 7373 | tree = &BTRFS_I(inode)->io_tree; |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 7374 | if (offset) { |
| 7375 | btrfs_releasepage(page, GFP_NOFS); |
| 7376 | return; |
| 7377 | } |
Jeff Mahoney | d008237 | 2012-03-01 14:57:19 +0100 | [diff] [blame] | 7378 | lock_extent_bits(tree, page_start, page_end, 0, &cached_state); |
Miao Xie | 4eee4fa | 2012-12-21 09:17:45 +0000 | [diff] [blame] | 7379 | ordered = btrfs_lookup_ordered_extent(inode, page_offset(page)); |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 7380 | if (ordered) { |
Chris Mason | eb84ae0 | 2008-07-17 13:53:27 -0400 | [diff] [blame] | 7381 | /* |
| 7382 | * IO on this page will never be started, so we need |
| 7383 | * to account for any ordered extents now |
| 7384 | */ |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 7385 | clear_extent_bit(tree, page_start, page_end, |
| 7386 | EXTENT_DIRTY | EXTENT_DELALLOC | |
Liu Bo | 9e8a4a8 | 2012-09-05 19:10:51 -0600 | [diff] [blame] | 7387 | EXTENT_LOCKED | EXTENT_DO_ACCOUNTING | |
| 7388 | EXTENT_DEFRAG, 1, 0, &cached_state, GFP_NOFS); |
Chris Mason | 8b62b72 | 2009-09-02 16:53:46 -0400 | [diff] [blame] | 7389 | /* |
| 7390 | * whoever cleared the private bit is responsible |
| 7391 | * for the finish_ordered_io |
| 7392 | */ |
Josef Bacik | 77cef2e | 2013-08-29 13:57:21 -0400 | [diff] [blame] | 7393 | if (TestClearPagePrivate2(page)) { |
| 7394 | struct btrfs_ordered_inode_tree *tree; |
| 7395 | u64 new_len; |
| 7396 | |
| 7397 | tree = &BTRFS_I(inode)->ordered_tree; |
| 7398 | |
| 7399 | spin_lock_irq(&tree->lock); |
| 7400 | set_bit(BTRFS_ORDERED_TRUNCATED, &ordered->flags); |
| 7401 | new_len = page_start - ordered->file_offset; |
| 7402 | if (new_len < ordered->truncated_len) |
| 7403 | ordered->truncated_len = new_len; |
| 7404 | spin_unlock_irq(&tree->lock); |
| 7405 | |
| 7406 | if (btrfs_dec_test_ordered_pending(inode, &ordered, |
| 7407 | page_start, |
| 7408 | PAGE_CACHE_SIZE, 1)) |
| 7409 | btrfs_finish_ordered_io(ordered); |
Chris Mason | 8b62b72 | 2009-09-02 16:53:46 -0400 | [diff] [blame] | 7410 | } |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 7411 | btrfs_put_ordered_extent(ordered); |
Josef Bacik | 2ac55d4 | 2010-02-03 19:33:23 +0000 | [diff] [blame] | 7412 | cached_state = NULL; |
Jeff Mahoney | d008237 | 2012-03-01 14:57:19 +0100 | [diff] [blame] | 7413 | lock_extent_bits(tree, page_start, page_end, 0, &cached_state); |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 7414 | } |
| 7415 | clear_extent_bit(tree, page_start, page_end, |
Josef Bacik | 32c00af | 2009-10-08 13:34:05 -0400 | [diff] [blame] | 7416 | EXTENT_LOCKED | EXTENT_DIRTY | EXTENT_DELALLOC | |
Liu Bo | 9e8a4a8 | 2012-09-05 19:10:51 -0600 | [diff] [blame] | 7417 | EXTENT_DO_ACCOUNTING | EXTENT_DEFRAG, 1, 1, |
| 7418 | &cached_state, GFP_NOFS); |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 7419 | __btrfs_releasepage(page, GFP_NOFS); |
| 7420 | |
Chris Mason | 4a09675 | 2008-07-21 10:29:44 -0400 | [diff] [blame] | 7421 | ClearPageChecked(page); |
Chris Mason | 9ad6b7bc | 2008-04-18 16:11:30 -0400 | [diff] [blame] | 7422 | if (PagePrivate(page)) { |
Chris Mason | 9ad6b7bc | 2008-04-18 16:11:30 -0400 | [diff] [blame] | 7423 | ClearPagePrivate(page); |
| 7424 | set_page_private(page, 0); |
| 7425 | page_cache_release(page); |
| 7426 | } |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 7427 | } |
| 7428 | |
Chris Mason | 9ebefb18 | 2007-06-15 13:50:00 -0400 | [diff] [blame] | 7429 | /* |
| 7430 | * btrfs_page_mkwrite() is not allowed to change the file size as it gets |
| 7431 | * called from a page fault handler when a page is first dirtied. Hence we must |
| 7432 | * be careful to check for EOF conditions here. We set the page up correctly |
| 7433 | * for a written page which means we get ENOSPC checking when writing into |
| 7434 | * holes and correct delalloc and unwritten extent mapping on filesystems that |
| 7435 | * support these features. |
| 7436 | * |
| 7437 | * We are not allowed to take the i_mutex here so we have to play games to |
| 7438 | * protect against truncate races as the page could now be beyond EOF. Because |
| 7439 | * vmtruncate() writes the inode size before removing pages, once we have the |
| 7440 | * page lock we can determine safely if the page is beyond EOF. If it is not |
| 7441 | * beyond EOF, then the page is guaranteed safe against truncation until we |
| 7442 | * unlock the page. |
| 7443 | */ |
Nick Piggin | c2ec175 | 2009-03-31 15:23:21 -0700 | [diff] [blame] | 7444 | 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] | 7445 | { |
Nick Piggin | c2ec175 | 2009-03-31 15:23:21 -0700 | [diff] [blame] | 7446 | struct page *page = vmf->page; |
Al Viro | 496ad9a | 2013-01-23 17:07:38 -0500 | [diff] [blame] | 7447 | struct inode *inode = file_inode(vma->vm_file); |
Chris Mason | 1832a6d | 2007-12-21 16:27:21 -0500 | [diff] [blame] | 7448 | struct btrfs_root *root = BTRFS_I(inode)->root; |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 7449 | struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree; |
| 7450 | struct btrfs_ordered_extent *ordered; |
Josef Bacik | 2ac55d4 | 2010-02-03 19:33:23 +0000 | [diff] [blame] | 7451 | struct extent_state *cached_state = NULL; |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 7452 | char *kaddr; |
| 7453 | unsigned long zero_start; |
Chris Mason | 9ebefb18 | 2007-06-15 13:50:00 -0400 | [diff] [blame] | 7454 | loff_t size; |
Chris Mason | 1832a6d | 2007-12-21 16:27:21 -0500 | [diff] [blame] | 7455 | int ret; |
Chris Mason | 9998eb7 | 2012-01-25 13:47:40 -0500 | [diff] [blame] | 7456 | int reserved = 0; |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 7457 | u64 page_start; |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 7458 | u64 page_end; |
Chris Mason | 9ebefb18 | 2007-06-15 13:50:00 -0400 | [diff] [blame] | 7459 | |
Jan Kara | b2b5ef5 | 2012-06-12 16:20:45 +0200 | [diff] [blame] | 7460 | sb_start_pagefault(inode->i_sb); |
Yan, Zheng | 0ca1f7c | 2010-05-16 10:48:47 -0400 | [diff] [blame] | 7461 | ret = btrfs_delalloc_reserve_space(inode, PAGE_CACHE_SIZE); |
Chris Mason | 9998eb7 | 2012-01-25 13:47:40 -0500 | [diff] [blame] | 7462 | if (!ret) { |
Josef Bacik | e41f941 | 2012-03-26 09:46:47 -0400 | [diff] [blame] | 7463 | ret = file_update_time(vma->vm_file); |
Chris Mason | 9998eb7 | 2012-01-25 13:47:40 -0500 | [diff] [blame] | 7464 | reserved = 1; |
| 7465 | } |
Nick Piggin | 56a76f8 | 2009-03-31 15:23:23 -0700 | [diff] [blame] | 7466 | if (ret) { |
| 7467 | if (ret == -ENOMEM) |
| 7468 | ret = VM_FAULT_OOM; |
| 7469 | else /* -ENOSPC, -EIO, etc */ |
| 7470 | ret = VM_FAULT_SIGBUS; |
Chris Mason | 9998eb7 | 2012-01-25 13:47:40 -0500 | [diff] [blame] | 7471 | if (reserved) |
| 7472 | goto out; |
| 7473 | goto out_noreserve; |
Nick Piggin | 56a76f8 | 2009-03-31 15:23:23 -0700 | [diff] [blame] | 7474 | } |
Chris Mason | 1832a6d | 2007-12-21 16:27:21 -0500 | [diff] [blame] | 7475 | |
Nick Piggin | 56a76f8 | 2009-03-31 15:23:23 -0700 | [diff] [blame] | 7476 | ret = VM_FAULT_NOPAGE; /* make the VM retry the fault */ |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 7477 | again: |
Chris Mason | 9ebefb18 | 2007-06-15 13:50:00 -0400 | [diff] [blame] | 7478 | lock_page(page); |
Chris Mason | 9ebefb18 | 2007-06-15 13:50:00 -0400 | [diff] [blame] | 7479 | size = i_size_read(inode); |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 7480 | page_start = page_offset(page); |
| 7481 | page_end = page_start + PAGE_CACHE_SIZE - 1; |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 7482 | |
Chris Mason | 9ebefb18 | 2007-06-15 13:50:00 -0400 | [diff] [blame] | 7483 | if ((page->mapping != inode->i_mapping) || |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 7484 | (page_start >= size)) { |
Chris Mason | 9ebefb18 | 2007-06-15 13:50:00 -0400 | [diff] [blame] | 7485 | /* page got truncated out from underneath us */ |
| 7486 | goto out_unlock; |
| 7487 | } |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 7488 | wait_on_page_writeback(page); |
| 7489 | |
Jeff Mahoney | d008237 | 2012-03-01 14:57:19 +0100 | [diff] [blame] | 7490 | lock_extent_bits(io_tree, page_start, page_end, 0, &cached_state); |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 7491 | set_page_extent_mapped(page); |
| 7492 | |
Chris Mason | eb84ae0 | 2008-07-17 13:53:27 -0400 | [diff] [blame] | 7493 | /* |
| 7494 | * we can't set the delalloc bits if there are pending ordered |
| 7495 | * extents. Drop our locks and wait for them to finish |
| 7496 | */ |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 7497 | ordered = btrfs_lookup_ordered_extent(inode, page_start); |
| 7498 | if (ordered) { |
Josef Bacik | 2ac55d4 | 2010-02-03 19:33:23 +0000 | [diff] [blame] | 7499 | unlock_extent_cached(io_tree, page_start, page_end, |
| 7500 | &cached_state, GFP_NOFS); |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 7501 | unlock_page(page); |
Chris Mason | eb84ae0 | 2008-07-17 13:53:27 -0400 | [diff] [blame] | 7502 | btrfs_start_ordered_extent(inode, ordered, 1); |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 7503 | btrfs_put_ordered_extent(ordered); |
| 7504 | goto again; |
| 7505 | } |
| 7506 | |
Josef Bacik | fbf1908 | 2009-10-01 17:10:23 -0400 | [diff] [blame] | 7507 | /* |
| 7508 | * XXX - page_mkwrite gets called every time the page is dirtied, even |
| 7509 | * if it was already dirty, so for space accounting reasons we need to |
| 7510 | * clear any delalloc bits for the range we are fixing to save. There |
| 7511 | * is probably a better way to do this, but for now keep consistent with |
| 7512 | * prepare_pages in the normal write path. |
| 7513 | */ |
Josef Bacik | 2ac55d4 | 2010-02-03 19:33:23 +0000 | [diff] [blame] | 7514 | clear_extent_bit(&BTRFS_I(inode)->io_tree, page_start, page_end, |
Liu Bo | 9e8a4a8 | 2012-09-05 19:10:51 -0600 | [diff] [blame] | 7515 | EXTENT_DIRTY | EXTENT_DELALLOC | |
| 7516 | EXTENT_DO_ACCOUNTING | EXTENT_DEFRAG, |
Josef Bacik | 2ac55d4 | 2010-02-03 19:33:23 +0000 | [diff] [blame] | 7517 | 0, 0, &cached_state, GFP_NOFS); |
Josef Bacik | fbf1908 | 2009-10-01 17:10:23 -0400 | [diff] [blame] | 7518 | |
Josef Bacik | 2ac55d4 | 2010-02-03 19:33:23 +0000 | [diff] [blame] | 7519 | ret = btrfs_set_extent_delalloc(inode, page_start, page_end, |
| 7520 | &cached_state); |
Josef Bacik | 9ed74f2 | 2009-09-11 16:12:44 -0400 | [diff] [blame] | 7521 | if (ret) { |
Josef Bacik | 2ac55d4 | 2010-02-03 19:33:23 +0000 | [diff] [blame] | 7522 | unlock_extent_cached(io_tree, page_start, page_end, |
| 7523 | &cached_state, GFP_NOFS); |
Josef Bacik | 9ed74f2 | 2009-09-11 16:12:44 -0400 | [diff] [blame] | 7524 | ret = VM_FAULT_SIGBUS; |
| 7525 | goto out_unlock; |
| 7526 | } |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 7527 | ret = 0; |
Chris Mason | 9ebefb18 | 2007-06-15 13:50:00 -0400 | [diff] [blame] | 7528 | |
| 7529 | /* page is wholly or partially inside EOF */ |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 7530 | if (page_start + PAGE_CACHE_SIZE > size) |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 7531 | zero_start = size & ~PAGE_CACHE_MASK; |
Chris Mason | 9ebefb18 | 2007-06-15 13:50:00 -0400 | [diff] [blame] | 7532 | else |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 7533 | zero_start = PAGE_CACHE_SIZE; |
Chris Mason | 9ebefb18 | 2007-06-15 13:50:00 -0400 | [diff] [blame] | 7534 | |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 7535 | if (zero_start != PAGE_CACHE_SIZE) { |
| 7536 | kaddr = kmap(page); |
| 7537 | memset(kaddr + zero_start, 0, PAGE_CACHE_SIZE - zero_start); |
| 7538 | flush_dcache_page(page); |
| 7539 | kunmap(page); |
| 7540 | } |
Chris Mason | 247e743 | 2008-07-17 12:53:51 -0400 | [diff] [blame] | 7541 | ClearPageChecked(page); |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 7542 | set_page_dirty(page); |
Chris Mason | 50a9b21 | 2009-09-11 12:33:12 -0400 | [diff] [blame] | 7543 | SetPageUptodate(page); |
Chris Mason | 5a3f23d | 2009-03-31 13:27:11 -0400 | [diff] [blame] | 7544 | |
Chris Mason | 257c62e | 2009-10-13 13:21:08 -0400 | [diff] [blame] | 7545 | BTRFS_I(inode)->last_trans = root->fs_info->generation; |
| 7546 | BTRFS_I(inode)->last_sub_trans = BTRFS_I(inode)->root->log_transid; |
Liu Bo | 46d8bc3 | 2012-08-29 01:07:55 -0600 | [diff] [blame] | 7547 | 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] | 7548 | |
Josef Bacik | 2ac55d4 | 2010-02-03 19:33:23 +0000 | [diff] [blame] | 7549 | 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] | 7550 | |
| 7551 | out_unlock: |
Jan Kara | b2b5ef5 | 2012-06-12 16:20:45 +0200 | [diff] [blame] | 7552 | if (!ret) { |
| 7553 | sb_end_pagefault(inode->i_sb); |
Chris Mason | 50a9b21 | 2009-09-11 12:33:12 -0400 | [diff] [blame] | 7554 | return VM_FAULT_LOCKED; |
Jan Kara | b2b5ef5 | 2012-06-12 16:20:45 +0200 | [diff] [blame] | 7555 | } |
Chris Mason | 9ebefb18 | 2007-06-15 13:50:00 -0400 | [diff] [blame] | 7556 | unlock_page(page); |
Chris Mason | 1832a6d | 2007-12-21 16:27:21 -0500 | [diff] [blame] | 7557 | out: |
Josef Bacik | ec39e18 | 2012-01-12 19:10:12 -0500 | [diff] [blame] | 7558 | btrfs_delalloc_release_space(inode, PAGE_CACHE_SIZE); |
Chris Mason | 9998eb7 | 2012-01-25 13:47:40 -0500 | [diff] [blame] | 7559 | out_noreserve: |
Jan Kara | b2b5ef5 | 2012-06-12 16:20:45 +0200 | [diff] [blame] | 7560 | sb_end_pagefault(inode->i_sb); |
Chris Mason | 9ebefb18 | 2007-06-15 13:50:00 -0400 | [diff] [blame] | 7561 | return ret; |
| 7562 | } |
| 7563 | |
Josef Bacik | a41ad39 | 2011-01-31 15:30:16 -0500 | [diff] [blame] | 7564 | static int btrfs_truncate(struct inode *inode) |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 7565 | { |
| 7566 | struct btrfs_root *root = BTRFS_I(inode)->root; |
Josef Bacik | fcb80c2 | 2011-05-03 10:40:22 -0400 | [diff] [blame] | 7567 | struct btrfs_block_rsv *rsv; |
Josef Bacik | a71754f | 2013-06-17 17:14:39 -0400 | [diff] [blame] | 7568 | int ret = 0; |
Josef Bacik | 3893e33 | 2011-01-31 16:03:11 -0500 | [diff] [blame] | 7569 | int err = 0; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 7570 | struct btrfs_trans_handle *trans; |
Chris Mason | dbe674a | 2008-07-17 12:54:05 -0400 | [diff] [blame] | 7571 | u64 mask = root->sectorsize - 1; |
Josef Bacik | 0712718 | 2011-08-19 10:29:59 -0400 | [diff] [blame] | 7572 | u64 min_size = btrfs_calc_trunc_metadata_size(root, 1); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 7573 | |
Chris Mason | 4a09675 | 2008-07-21 10:29:44 -0400 | [diff] [blame] | 7574 | btrfs_wait_ordered_range(inode, inode->i_size & (~mask), (u64)-1); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 7575 | |
Josef Bacik | fcb80c2 | 2011-05-03 10:40:22 -0400 | [diff] [blame] | 7576 | /* |
| 7577 | * Yes ladies and gentelment, this is indeed ugly. The fact is we have |
| 7578 | * 3 things going on here |
| 7579 | * |
| 7580 | * 1) We need to reserve space for our orphan item and the space to |
| 7581 | * delete our orphan item. Lord knows we don't want to have a dangling |
| 7582 | * orphan item because we didn't reserve space to remove it. |
| 7583 | * |
| 7584 | * 2) We need to reserve space to update our inode. |
| 7585 | * |
| 7586 | * 3) We need to have something to cache all the space that is going to |
| 7587 | * be free'd up by the truncate operation, but also have some slack |
| 7588 | * space reserved in case it uses space during the truncate (thank you |
| 7589 | * very much snapshotting). |
| 7590 | * |
| 7591 | * And we need these to all be seperate. The fact is we can use alot of |
| 7592 | * space doing the truncate, and we have no earthly idea how much space |
| 7593 | * we will use, so we need the truncate reservation to be seperate so it |
| 7594 | * doesn't end up using space reserved for updating the inode or |
| 7595 | * removing the orphan item. We also need to be able to stop the |
| 7596 | * transaction and start a new one, which means we need to be able to |
| 7597 | * update the inode several times, and we have no idea of knowing how |
| 7598 | * many times that will be, so we can't just reserve 1 item for the |
| 7599 | * entirety of the opration, so that has to be done seperately as well. |
| 7600 | * Then there is the orphan item, which does indeed need to be held on |
| 7601 | * to for the whole operation, and we need nobody to touch this reserved |
| 7602 | * space except the orphan code. |
| 7603 | * |
| 7604 | * So that leaves us with |
| 7605 | * |
| 7606 | * 1) root->orphan_block_rsv - for the orphan deletion. |
| 7607 | * 2) rsv - for the truncate reservation, which we will steal from the |
| 7608 | * transaction reservation. |
| 7609 | * 3) fs_info->trans_block_rsv - this will have 1 items worth left for |
| 7610 | * updating the inode. |
| 7611 | */ |
Miao Xie | 66d8f3d | 2012-09-06 04:02:28 -0600 | [diff] [blame] | 7612 | rsv = btrfs_alloc_block_rsv(root, BTRFS_BLOCK_RSV_TEMP); |
Josef Bacik | fcb80c2 | 2011-05-03 10:40:22 -0400 | [diff] [blame] | 7613 | if (!rsv) |
| 7614 | return -ENOMEM; |
Josef Bacik | 4a33854 | 2011-08-29 11:01:31 -0400 | [diff] [blame] | 7615 | rsv->size = min_size; |
Josef Bacik | ca7e70f | 2012-08-27 17:48:15 -0400 | [diff] [blame] | 7616 | rsv->failfast = 1; |
Josef Bacik | f0cd846 | 2011-03-04 14:37:08 -0500 | [diff] [blame] | 7617 | |
Josef Bacik | 907cbce | 2011-08-08 13:46:15 -0400 | [diff] [blame] | 7618 | /* |
Josef Bacik | 0712718 | 2011-08-19 10:29:59 -0400 | [diff] [blame] | 7619 | * 1 for the truncate slack space |
Josef Bacik | 907cbce | 2011-08-08 13:46:15 -0400 | [diff] [blame] | 7620 | * 1 for updating the inode. |
| 7621 | */ |
Josef Bacik | f3fe820 | 2013-01-07 17:03:21 -0500 | [diff] [blame] | 7622 | trans = btrfs_start_transaction(root, 2); |
Josef Bacik | fcb80c2 | 2011-05-03 10:40:22 -0400 | [diff] [blame] | 7623 | if (IS_ERR(trans)) { |
| 7624 | err = PTR_ERR(trans); |
| 7625 | goto out; |
| 7626 | } |
Josef Bacik | f0cd846 | 2011-03-04 14:37:08 -0500 | [diff] [blame] | 7627 | |
Josef Bacik | 907cbce | 2011-08-08 13:46:15 -0400 | [diff] [blame] | 7628 | /* Migrate the slack space for the truncate to our reserve */ |
| 7629 | ret = btrfs_block_rsv_migrate(&root->fs_info->trans_block_rsv, rsv, |
| 7630 | min_size); |
Josef Bacik | fcb80c2 | 2011-05-03 10:40:22 -0400 | [diff] [blame] | 7631 | BUG_ON(ret); |
Josef Bacik | f0cd846 | 2011-03-04 14:37:08 -0500 | [diff] [blame] | 7632 | |
Chris Mason | 5a3f23d | 2009-03-31 13:27:11 -0400 | [diff] [blame] | 7633 | /* |
| 7634 | * setattr is responsible for setting the ordered_data_close flag, |
| 7635 | * but that is only tested during the last file release. That |
| 7636 | * could happen well after the next commit, leaving a great big |
| 7637 | * window where new writes may get lost if someone chooses to write |
| 7638 | * to this file after truncating to zero |
| 7639 | * |
| 7640 | * The inode doesn't have any dirty data here, and so if we commit |
| 7641 | * this is a noop. If someone immediately starts writing to the inode |
| 7642 | * it is very likely we'll catch some of their writes in this |
| 7643 | * transaction, and the commit will find this file on the ordered |
| 7644 | * data list with good things to send down. |
| 7645 | * |
| 7646 | * This is a best effort solution, there is still a window where |
| 7647 | * using truncate to replace the contents of the file will |
| 7648 | * end up with a zero length file after a crash. |
| 7649 | */ |
Josef Bacik | 72ac3c0 | 2012-05-23 14:13:11 -0400 | [diff] [blame] | 7650 | if (inode->i_size == 0 && test_bit(BTRFS_INODE_ORDERED_DATA_CLOSE, |
| 7651 | &BTRFS_I(inode)->runtime_flags)) |
Chris Mason | 5a3f23d | 2009-03-31 13:27:11 -0400 | [diff] [blame] | 7652 | btrfs_add_ordered_operation(trans, root, inode); |
| 7653 | |
Josef Bacik | 5dc562c | 2012-08-17 13:14:17 -0400 | [diff] [blame] | 7654 | /* |
| 7655 | * So if we truncate and then write and fsync we normally would just |
| 7656 | * write the extents that changed, which is a problem if we need to |
| 7657 | * first truncate that entire inode. So set this flag so we write out |
| 7658 | * all of the extents in the inode to the sync log so we're completely |
| 7659 | * safe. |
| 7660 | */ |
| 7661 | set_bit(BTRFS_INODE_NEEDS_FULL_SYNC, &BTRFS_I(inode)->runtime_flags); |
Josef Bacik | ca7e70f | 2012-08-27 17:48:15 -0400 | [diff] [blame] | 7662 | trans->block_rsv = rsv; |
Josef Bacik | 5dc562c | 2012-08-17 13:14:17 -0400 | [diff] [blame] | 7663 | |
Yan, Zheng | 8082510 | 2009-11-12 09:35:36 +0000 | [diff] [blame] | 7664 | while (1) { |
| 7665 | ret = btrfs_truncate_inode_items(trans, root, inode, |
| 7666 | inode->i_size, |
| 7667 | BTRFS_EXTENT_DATA_KEY); |
Josef Bacik | ca7e70f | 2012-08-27 17:48:15 -0400 | [diff] [blame] | 7668 | if (ret != -ENOSPC) { |
Josef Bacik | 3893e33 | 2011-01-31 16:03:11 -0500 | [diff] [blame] | 7669 | err = ret; |
Yan, Zheng | 8082510 | 2009-11-12 09:35:36 +0000 | [diff] [blame] | 7670 | break; |
Josef Bacik | 3893e33 | 2011-01-31 16:03:11 -0500 | [diff] [blame] | 7671 | } |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 7672 | |
Josef Bacik | fcb80c2 | 2011-05-03 10:40:22 -0400 | [diff] [blame] | 7673 | trans->block_rsv = &root->fs_info->trans_block_rsv; |
Yan, Zheng | 8082510 | 2009-11-12 09:35:36 +0000 | [diff] [blame] | 7674 | ret = btrfs_update_inode(trans, root, inode); |
Josef Bacik | 3893e33 | 2011-01-31 16:03:11 -0500 | [diff] [blame] | 7675 | if (ret) { |
| 7676 | err = ret; |
| 7677 | break; |
| 7678 | } |
Josef Bacik | ca7e70f | 2012-08-27 17:48:15 -0400 | [diff] [blame] | 7679 | |
Yan, Zheng | 8082510 | 2009-11-12 09:35:36 +0000 | [diff] [blame] | 7680 | btrfs_end_transaction(trans, root); |
Liu Bo | b53d3f5 | 2012-11-14 14:34:34 +0000 | [diff] [blame] | 7681 | btrfs_btree_balance_dirty(root); |
Josef Bacik | ca7e70f | 2012-08-27 17:48:15 -0400 | [diff] [blame] | 7682 | |
| 7683 | trans = btrfs_start_transaction(root, 2); |
| 7684 | if (IS_ERR(trans)) { |
| 7685 | ret = err = PTR_ERR(trans); |
| 7686 | trans = NULL; |
| 7687 | break; |
| 7688 | } |
| 7689 | |
| 7690 | ret = btrfs_block_rsv_migrate(&root->fs_info->trans_block_rsv, |
| 7691 | rsv, min_size); |
| 7692 | BUG_ON(ret); /* shouldn't happen */ |
| 7693 | trans->block_rsv = rsv; |
Yan, Zheng | 8082510 | 2009-11-12 09:35:36 +0000 | [diff] [blame] | 7694 | } |
| 7695 | |
| 7696 | if (ret == 0 && inode->i_nlink > 0) { |
Josef Bacik | fcb80c2 | 2011-05-03 10:40:22 -0400 | [diff] [blame] | 7697 | trans->block_rsv = root->orphan_block_rsv; |
Yan, Zheng | 8082510 | 2009-11-12 09:35:36 +0000 | [diff] [blame] | 7698 | ret = btrfs_orphan_del(trans, inode); |
Josef Bacik | 3893e33 | 2011-01-31 16:03:11 -0500 | [diff] [blame] | 7699 | if (ret) |
| 7700 | err = ret; |
Yan, Zheng | 8082510 | 2009-11-12 09:35:36 +0000 | [diff] [blame] | 7701 | } |
| 7702 | |
Chris Mason | 917c16b | 2011-11-08 14:49:59 -0500 | [diff] [blame] | 7703 | if (trans) { |
| 7704 | trans->block_rsv = &root->fs_info->trans_block_rsv; |
| 7705 | ret = btrfs_update_inode(trans, root, inode); |
| 7706 | if (ret && !err) |
| 7707 | err = ret; |
Josef Bacik | 7b12876 | 2008-07-24 12:17:14 -0400 | [diff] [blame] | 7708 | |
Josef Bacik | 7ad85bb | 2012-01-12 19:10:12 -0500 | [diff] [blame] | 7709 | ret = btrfs_end_transaction(trans, root); |
Liu Bo | b53d3f5 | 2012-11-14 14:34:34 +0000 | [diff] [blame] | 7710 | btrfs_btree_balance_dirty(root); |
Chris Mason | 917c16b | 2011-11-08 14:49:59 -0500 | [diff] [blame] | 7711 | } |
Josef Bacik | fcb80c2 | 2011-05-03 10:40:22 -0400 | [diff] [blame] | 7712 | |
| 7713 | out: |
| 7714 | btrfs_free_block_rsv(root, rsv); |
| 7715 | |
Josef Bacik | 3893e33 | 2011-01-31 16:03:11 -0500 | [diff] [blame] | 7716 | if (ret && !err) |
| 7717 | err = ret; |
Josef Bacik | a41ad39 | 2011-01-31 15:30:16 -0500 | [diff] [blame] | 7718 | |
Josef Bacik | 3893e33 | 2011-01-31 16:03:11 -0500 | [diff] [blame] | 7719 | return err; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 7720 | } |
| 7721 | |
Sven Wegener | 3b96362 | 2008-06-09 21:57:42 -0400 | [diff] [blame] | 7722 | /* |
Chris Mason | d352ac6 | 2008-09-29 15:18:18 -0400 | [diff] [blame] | 7723 | * create a new subvolume directory/inode (helper for the ioctl). |
| 7724 | */ |
Yan Zheng | d2fb343 | 2008-12-11 16:30:39 -0500 | [diff] [blame] | 7725 | int btrfs_create_subvol_root(struct btrfs_trans_handle *trans, |
Josef Bacik | d82a6f1d | 2011-05-11 15:26:06 -0400 | [diff] [blame] | 7726 | struct btrfs_root *new_root, u64 new_dirid) |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 7727 | { |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 7728 | struct inode *inode; |
Yan, Zheng | 76dda93 | 2009-09-21 16:00:26 -0400 | [diff] [blame] | 7729 | int err; |
Chris Mason | 00e4e6b | 2008-08-05 11:18:09 -0400 | [diff] [blame] | 7730 | u64 index = 0; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 7731 | |
Florian Albrechtskirchinger | 12fc9d0 | 2012-02-10 22:15:54 +0100 | [diff] [blame] | 7732 | inode = btrfs_new_inode(trans, new_root, NULL, "..", 2, |
| 7733 | new_dirid, new_dirid, |
| 7734 | S_IFDIR | (~current_umask() & S_IRWXUGO), |
| 7735 | &index); |
Chris Mason | 54aa1f4 | 2007-06-22 14:16:25 -0400 | [diff] [blame] | 7736 | if (IS_ERR(inode)) |
Christoph Hellwig | f46b5a6 | 2008-06-11 21:53:53 -0400 | [diff] [blame] | 7737 | return PTR_ERR(inode); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 7738 | inode->i_op = &btrfs_dir_inode_operations; |
| 7739 | inode->i_fop = &btrfs_dir_file_operations; |
| 7740 | |
Miklos Szeredi | bfe8684 | 2011-10-28 14:13:29 +0200 | [diff] [blame] | 7741 | set_nlink(inode, 1); |
Chris Mason | dbe674a | 2008-07-17 12:54:05 -0400 | [diff] [blame] | 7742 | btrfs_i_size_write(inode, 0); |
Sven Wegener | 3b96362 | 2008-06-09 21:57:42 -0400 | [diff] [blame] | 7743 | |
Yan, Zheng | 76dda93 | 2009-09-21 16:00:26 -0400 | [diff] [blame] | 7744 | err = btrfs_update_inode(trans, new_root, inode); |
Christoph Hellwig | cb8e709 | 2008-10-09 13:39:39 -0400 | [diff] [blame] | 7745 | |
Yan, Zheng | 76dda93 | 2009-09-21 16:00:26 -0400 | [diff] [blame] | 7746 | iput(inode); |
Mark Fasheh | ce59897 | 2011-07-26 11:32:23 -0700 | [diff] [blame] | 7747 | return err; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 7748 | } |
| 7749 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 7750 | struct inode *btrfs_alloc_inode(struct super_block *sb) |
| 7751 | { |
| 7752 | struct btrfs_inode *ei; |
Yan, Zheng | 2ead6ae | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 7753 | struct inode *inode; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 7754 | |
| 7755 | ei = kmem_cache_alloc(btrfs_inode_cachep, GFP_NOFS); |
| 7756 | if (!ei) |
| 7757 | return NULL; |
Yan, Zheng | 2ead6ae | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 7758 | |
| 7759 | ei->root = NULL; |
Yan, Zheng | 2ead6ae | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 7760 | ei->generation = 0; |
Josef Bacik | 15ee9bc | 2007-08-10 16:22:09 -0400 | [diff] [blame] | 7761 | ei->last_trans = 0; |
Chris Mason | 257c62e | 2009-10-13 13:21:08 -0400 | [diff] [blame] | 7762 | ei->last_sub_trans = 0; |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 7763 | ei->logged_trans = 0; |
Yan, Zheng | 2ead6ae | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 7764 | ei->delalloc_bytes = 0; |
Yan, Zheng | 2ead6ae | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 7765 | ei->disk_i_size = 0; |
| 7766 | ei->flags = 0; |
Josef Bacik | 7709cde | 2011-08-04 10:25:02 -0400 | [diff] [blame] | 7767 | ei->csum_bytes = 0; |
Yan, Zheng | 2ead6ae | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 7768 | ei->index_cnt = (u64)-1; |
| 7769 | ei->last_unlink_trans = 0; |
Liu Bo | 46d8bc3 | 2012-08-29 01:07:55 -0600 | [diff] [blame] | 7770 | ei->last_log_commit = 0; |
Yan, Zheng | 2ead6ae | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 7771 | |
Josef Bacik | 9e0baf6 | 2011-07-15 15:16:44 +0000 | [diff] [blame] | 7772 | spin_lock_init(&ei->lock); |
| 7773 | ei->outstanding_extents = 0; |
| 7774 | ei->reserved_extents = 0; |
Yan, Zheng | 2ead6ae | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 7775 | |
Josef Bacik | 72ac3c0 | 2012-05-23 14:13:11 -0400 | [diff] [blame] | 7776 | ei->runtime_flags = 0; |
Li Zefan | 261507a0 | 2010-12-17 14:21:50 +0800 | [diff] [blame] | 7777 | ei->force_compress = BTRFS_COMPRESS_NONE; |
Yan, Zheng | 2ead6ae | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 7778 | |
Miao Xie | 16cdcec | 2011-04-22 18:12:22 +0800 | [diff] [blame] | 7779 | ei->delayed_node = NULL; |
| 7780 | |
Yan, Zheng | 2ead6ae | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 7781 | inode = &ei->vfs_inode; |
David Sterba | a8067e0 | 2011-04-21 00:34:43 +0200 | [diff] [blame] | 7782 | extent_map_tree_init(&ei->extent_tree); |
David Sterba | f993c88 | 2011-04-20 23:35:57 +0200 | [diff] [blame] | 7783 | extent_io_tree_init(&ei->io_tree, &inode->i_data); |
| 7784 | extent_io_tree_init(&ei->io_failure_tree, &inode->i_data); |
Josef Bacik | 0b32f4b | 2012-03-13 09:38:00 -0400 | [diff] [blame] | 7785 | ei->io_tree.track_uptodate = 1; |
| 7786 | ei->io_failure_tree.track_uptodate = 1; |
Josef Bacik | b812ce2 | 2012-11-16 13:56:32 -0500 | [diff] [blame] | 7787 | atomic_set(&ei->sync_writers, 0); |
Yan, Zheng | 2ead6ae | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 7788 | mutex_init(&ei->log_mutex); |
Josef Bacik | f248679 | 2012-01-13 12:09:22 -0500 | [diff] [blame] | 7789 | mutex_init(&ei->delalloc_mutex); |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 7790 | btrfs_ordered_inode_tree_init(&ei->ordered_tree); |
Yan, Zheng | 2ead6ae | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 7791 | INIT_LIST_HEAD(&ei->delalloc_inodes); |
Chris Mason | 5a3f23d | 2009-03-31 13:27:11 -0400 | [diff] [blame] | 7792 | INIT_LIST_HEAD(&ei->ordered_operations); |
Yan, Zheng | 2ead6ae | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 7793 | RB_CLEAR_NODE(&ei->rb_node); |
| 7794 | |
| 7795 | return inode; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 7796 | } |
| 7797 | |
Josef Bacik | aaedb55 | 2013-10-11 14:44:09 -0400 | [diff] [blame^] | 7798 | #ifdef CONFIG_BTRFS_FS_RUN_SANITY_TESTS |
| 7799 | void btrfs_test_destroy_inode(struct inode *inode) |
| 7800 | { |
| 7801 | btrfs_drop_extent_cache(inode, 0, (u64)-1, 0); |
| 7802 | kmem_cache_free(btrfs_inode_cachep, BTRFS_I(inode)); |
| 7803 | } |
| 7804 | #endif |
| 7805 | |
Nick Piggin | fa0d7e3d | 2011-01-07 17:49:49 +1100 | [diff] [blame] | 7806 | static void btrfs_i_callback(struct rcu_head *head) |
| 7807 | { |
| 7808 | struct inode *inode = container_of(head, struct inode, i_rcu); |
Nick Piggin | fa0d7e3d | 2011-01-07 17:49:49 +1100 | [diff] [blame] | 7809 | kmem_cache_free(btrfs_inode_cachep, BTRFS_I(inode)); |
| 7810 | } |
| 7811 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 7812 | void btrfs_destroy_inode(struct inode *inode) |
| 7813 | { |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 7814 | struct btrfs_ordered_extent *ordered; |
Chris Mason | 5a3f23d | 2009-03-31 13:27:11 -0400 | [diff] [blame] | 7815 | struct btrfs_root *root = BTRFS_I(inode)->root; |
| 7816 | |
Al Viro | b3d9b7a | 2012-06-09 13:51:19 -0400 | [diff] [blame] | 7817 | WARN_ON(!hlist_empty(&inode->i_dentry)); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 7818 | WARN_ON(inode->i_data.nrpages); |
Josef Bacik | 9e0baf6 | 2011-07-15 15:16:44 +0000 | [diff] [blame] | 7819 | WARN_ON(BTRFS_I(inode)->outstanding_extents); |
| 7820 | WARN_ON(BTRFS_I(inode)->reserved_extents); |
Josef Bacik | 7709cde | 2011-08-04 10:25:02 -0400 | [diff] [blame] | 7821 | WARN_ON(BTRFS_I(inode)->delalloc_bytes); |
| 7822 | WARN_ON(BTRFS_I(inode)->csum_bytes); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 7823 | |
Chris Mason | 5a3f23d | 2009-03-31 13:27:11 -0400 | [diff] [blame] | 7824 | /* |
Josef Bacik | a6dbd42 | 2009-11-11 15:53:34 -0500 | [diff] [blame] | 7825 | * This can happen where we create an inode, but somebody else also |
| 7826 | * created the same inode and we need to destroy the one we already |
| 7827 | * created. |
| 7828 | */ |
| 7829 | if (!root) |
| 7830 | goto free; |
| 7831 | |
| 7832 | /* |
Chris Mason | 5a3f23d | 2009-03-31 13:27:11 -0400 | [diff] [blame] | 7833 | * Make sure we're properly removed from the ordered operation |
| 7834 | * lists. |
| 7835 | */ |
| 7836 | smp_mb(); |
| 7837 | if (!list_empty(&BTRFS_I(inode)->ordered_operations)) { |
Miao Xie | 199c2a9 | 2013-05-15 07:48:23 +0000 | [diff] [blame] | 7838 | spin_lock(&root->fs_info->ordered_root_lock); |
Chris Mason | 5a3f23d | 2009-03-31 13:27:11 -0400 | [diff] [blame] | 7839 | list_del_init(&BTRFS_I(inode)->ordered_operations); |
Miao Xie | 199c2a9 | 2013-05-15 07:48:23 +0000 | [diff] [blame] | 7840 | spin_unlock(&root->fs_info->ordered_root_lock); |
Chris Mason | 5a3f23d | 2009-03-31 13:27:11 -0400 | [diff] [blame] | 7841 | } |
| 7842 | |
Josef Bacik | 8a35d95 | 2012-05-23 14:26:42 -0400 | [diff] [blame] | 7843 | if (test_bit(BTRFS_INODE_HAS_ORPHAN_ITEM, |
| 7844 | &BTRFS_I(inode)->runtime_flags)) { |
Simon Kirby | c2cf52e | 2013-03-19 22:41:23 +0000 | [diff] [blame] | 7845 | btrfs_info(root->fs_info, "inode %llu still on the orphan list", |
Geert Uytterhoeven | c1c9ff7 | 2013-08-20 13:20:07 +0200 | [diff] [blame] | 7846 | btrfs_ino(inode)); |
Josef Bacik | 8a35d95 | 2012-05-23 14:26:42 -0400 | [diff] [blame] | 7847 | atomic_dec(&root->orphan_inodes); |
Josef Bacik | 7b12876 | 2008-07-24 12:17:14 -0400 | [diff] [blame] | 7848 | } |
Josef Bacik | 7b12876 | 2008-07-24 12:17:14 -0400 | [diff] [blame] | 7849 | |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 7850 | while (1) { |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 7851 | ordered = btrfs_lookup_first_ordered_extent(inode, (u64)-1); |
| 7852 | if (!ordered) |
| 7853 | break; |
| 7854 | else { |
Simon Kirby | c2cf52e | 2013-03-19 22:41:23 +0000 | [diff] [blame] | 7855 | 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] | 7856 | ordered->file_offset, ordered->len); |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 7857 | btrfs_remove_ordered_extent(inode, ordered); |
| 7858 | btrfs_put_ordered_extent(ordered); |
| 7859 | btrfs_put_ordered_extent(ordered); |
| 7860 | } |
| 7861 | } |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 7862 | inode_tree_del(inode); |
Zheng Yan | 5b21f2e | 2008-09-26 10:05:38 -0400 | [diff] [blame] | 7863 | btrfs_drop_extent_cache(inode, 0, (u64)-1, 0); |
Josef Bacik | a6dbd42 | 2009-11-11 15:53:34 -0500 | [diff] [blame] | 7864 | free: |
Nick Piggin | fa0d7e3d | 2011-01-07 17:49:49 +1100 | [diff] [blame] | 7865 | call_rcu(&inode->i_rcu, btrfs_i_callback); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 7866 | } |
| 7867 | |
Al Viro | 45321ac | 2010-06-07 13:43:19 -0400 | [diff] [blame] | 7868 | int btrfs_drop_inode(struct inode *inode) |
Yan, Zheng | 76dda93 | 2009-09-21 16:00:26 -0400 | [diff] [blame] | 7869 | { |
| 7870 | struct btrfs_root *root = BTRFS_I(inode)->root; |
Al Viro | 45321ac | 2010-06-07 13:43:19 -0400 | [diff] [blame] | 7871 | |
Naohiro Aota | 6379ef9 | 2013-06-06 09:56:34 +0000 | [diff] [blame] | 7872 | if (root == NULL) |
| 7873 | return 1; |
| 7874 | |
Liu Bo | fa6ac87 | 2013-02-20 14:10:23 +0000 | [diff] [blame] | 7875 | /* the snap/subvol tree is on deleting */ |
Stefan Behrens | 69e9c6c | 2013-09-05 16:58:43 +0200 | [diff] [blame] | 7876 | if (btrfs_root_refs(&root->root_item) == 0) |
Al Viro | 45321ac | 2010-06-07 13:43:19 -0400 | [diff] [blame] | 7877 | return 1; |
Yan, Zheng | 76dda93 | 2009-09-21 16:00:26 -0400 | [diff] [blame] | 7878 | else |
Al Viro | 45321ac | 2010-06-07 13:43:19 -0400 | [diff] [blame] | 7879 | return generic_drop_inode(inode); |
Yan, Zheng | 76dda93 | 2009-09-21 16:00:26 -0400 | [diff] [blame] | 7880 | } |
| 7881 | |
Sven Wegener | 0ee0fda | 2008-07-30 16:54:26 -0400 | [diff] [blame] | 7882 | static void init_once(void *foo) |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 7883 | { |
| 7884 | struct btrfs_inode *ei = (struct btrfs_inode *) foo; |
| 7885 | |
| 7886 | inode_init_once(&ei->vfs_inode); |
| 7887 | } |
| 7888 | |
| 7889 | void btrfs_destroy_cachep(void) |
| 7890 | { |
Kirill A. Shutemov | 8c0a853 | 2012-09-26 11:33:07 +1000 | [diff] [blame] | 7891 | /* |
| 7892 | * Make sure all delayed rcu free inodes are flushed before we |
| 7893 | * destroy cache. |
| 7894 | */ |
| 7895 | rcu_barrier(); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 7896 | if (btrfs_inode_cachep) |
| 7897 | kmem_cache_destroy(btrfs_inode_cachep); |
| 7898 | if (btrfs_trans_handle_cachep) |
| 7899 | kmem_cache_destroy(btrfs_trans_handle_cachep); |
| 7900 | if (btrfs_transaction_cachep) |
| 7901 | kmem_cache_destroy(btrfs_transaction_cachep); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 7902 | if (btrfs_path_cachep) |
| 7903 | kmem_cache_destroy(btrfs_path_cachep); |
Josef Bacik | dc89e98 | 2011-01-28 17:05:48 -0500 | [diff] [blame] | 7904 | if (btrfs_free_space_cachep) |
| 7905 | kmem_cache_destroy(btrfs_free_space_cachep); |
Miao Xie | 8ccf6f19 | 2012-10-25 09:28:04 +0000 | [diff] [blame] | 7906 | if (btrfs_delalloc_work_cachep) |
| 7907 | kmem_cache_destroy(btrfs_delalloc_work_cachep); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 7908 | } |
| 7909 | |
| 7910 | int btrfs_init_cachep(void) |
| 7911 | { |
David Sterba | 837e197 | 2012-09-07 03:00:48 -0600 | [diff] [blame] | 7912 | btrfs_inode_cachep = kmem_cache_create("btrfs_inode", |
Christoph Hellwig | 9601e3f | 2009-04-13 15:33:09 +0200 | [diff] [blame] | 7913 | sizeof(struct btrfs_inode), 0, |
| 7914 | SLAB_RECLAIM_ACCOUNT | SLAB_MEM_SPREAD, init_once); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 7915 | if (!btrfs_inode_cachep) |
| 7916 | goto fail; |
Christoph Hellwig | 9601e3f | 2009-04-13 15:33:09 +0200 | [diff] [blame] | 7917 | |
David Sterba | 837e197 | 2012-09-07 03:00:48 -0600 | [diff] [blame] | 7918 | btrfs_trans_handle_cachep = kmem_cache_create("btrfs_trans_handle", |
Christoph Hellwig | 9601e3f | 2009-04-13 15:33:09 +0200 | [diff] [blame] | 7919 | sizeof(struct btrfs_trans_handle), 0, |
| 7920 | SLAB_RECLAIM_ACCOUNT | SLAB_MEM_SPREAD, NULL); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 7921 | if (!btrfs_trans_handle_cachep) |
| 7922 | goto fail; |
Christoph Hellwig | 9601e3f | 2009-04-13 15:33:09 +0200 | [diff] [blame] | 7923 | |
David Sterba | 837e197 | 2012-09-07 03:00:48 -0600 | [diff] [blame] | 7924 | btrfs_transaction_cachep = kmem_cache_create("btrfs_transaction", |
Christoph Hellwig | 9601e3f | 2009-04-13 15:33:09 +0200 | [diff] [blame] | 7925 | sizeof(struct btrfs_transaction), 0, |
| 7926 | SLAB_RECLAIM_ACCOUNT | SLAB_MEM_SPREAD, NULL); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 7927 | if (!btrfs_transaction_cachep) |
| 7928 | goto fail; |
Christoph Hellwig | 9601e3f | 2009-04-13 15:33:09 +0200 | [diff] [blame] | 7929 | |
David Sterba | 837e197 | 2012-09-07 03:00:48 -0600 | [diff] [blame] | 7930 | btrfs_path_cachep = kmem_cache_create("btrfs_path", |
Christoph Hellwig | 9601e3f | 2009-04-13 15:33:09 +0200 | [diff] [blame] | 7931 | sizeof(struct btrfs_path), 0, |
| 7932 | SLAB_RECLAIM_ACCOUNT | SLAB_MEM_SPREAD, NULL); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 7933 | if (!btrfs_path_cachep) |
| 7934 | goto fail; |
Christoph Hellwig | 9601e3f | 2009-04-13 15:33:09 +0200 | [diff] [blame] | 7935 | |
David Sterba | 837e197 | 2012-09-07 03:00:48 -0600 | [diff] [blame] | 7936 | btrfs_free_space_cachep = kmem_cache_create("btrfs_free_space", |
Josef Bacik | dc89e98 | 2011-01-28 17:05:48 -0500 | [diff] [blame] | 7937 | sizeof(struct btrfs_free_space), 0, |
| 7938 | SLAB_RECLAIM_ACCOUNT | SLAB_MEM_SPREAD, NULL); |
| 7939 | if (!btrfs_free_space_cachep) |
| 7940 | goto fail; |
| 7941 | |
Miao Xie | 8ccf6f19 | 2012-10-25 09:28:04 +0000 | [diff] [blame] | 7942 | btrfs_delalloc_work_cachep = kmem_cache_create("btrfs_delalloc_work", |
| 7943 | sizeof(struct btrfs_delalloc_work), 0, |
| 7944 | SLAB_RECLAIM_ACCOUNT | SLAB_MEM_SPREAD, |
| 7945 | NULL); |
| 7946 | if (!btrfs_delalloc_work_cachep) |
| 7947 | goto fail; |
| 7948 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 7949 | return 0; |
| 7950 | fail: |
| 7951 | btrfs_destroy_cachep(); |
| 7952 | return -ENOMEM; |
| 7953 | } |
| 7954 | |
| 7955 | static int btrfs_getattr(struct vfsmount *mnt, |
| 7956 | struct dentry *dentry, struct kstat *stat) |
| 7957 | { |
Miao Xie | df0af1a | 2013-01-29 10:11:59 +0000 | [diff] [blame] | 7958 | u64 delalloc_bytes; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 7959 | struct inode *inode = dentry->d_inode; |
David Sterba | fadc0d8 | 2011-11-20 07:33:38 -0500 | [diff] [blame] | 7960 | u32 blocksize = inode->i_sb->s_blocksize; |
| 7961 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 7962 | generic_fillattr(inode, stat); |
Al Viro | 0ee5dc6 | 2011-07-07 15:44:25 -0400 | [diff] [blame] | 7963 | stat->dev = BTRFS_I(inode)->root->anon_dev; |
Chris Mason | d666746 | 2008-01-03 14:51:00 -0500 | [diff] [blame] | 7964 | stat->blksize = PAGE_CACHE_SIZE; |
Miao Xie | df0af1a | 2013-01-29 10:11:59 +0000 | [diff] [blame] | 7965 | |
| 7966 | spin_lock(&BTRFS_I(inode)->lock); |
| 7967 | delalloc_bytes = BTRFS_I(inode)->delalloc_bytes; |
| 7968 | spin_unlock(&BTRFS_I(inode)->lock); |
David Sterba | fadc0d8 | 2011-11-20 07:33:38 -0500 | [diff] [blame] | 7969 | stat->blocks = (ALIGN(inode_get_bytes(inode), blocksize) + |
Miao Xie | df0af1a | 2013-01-29 10:11:59 +0000 | [diff] [blame] | 7970 | ALIGN(delalloc_bytes, blocksize)) >> 9; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 7971 | return 0; |
| 7972 | } |
| 7973 | |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 7974 | static int btrfs_rename(struct inode *old_dir, struct dentry *old_dentry, |
| 7975 | struct inode *new_dir, struct dentry *new_dentry) |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 7976 | { |
| 7977 | struct btrfs_trans_handle *trans; |
| 7978 | struct btrfs_root *root = BTRFS_I(old_dir)->root; |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 7979 | struct btrfs_root *dest = BTRFS_I(new_dir)->root; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 7980 | struct inode *new_inode = new_dentry->d_inode; |
| 7981 | struct inode *old_inode = old_dentry->d_inode; |
| 7982 | struct timespec ctime = CURRENT_TIME; |
Chris Mason | 00e4e6b | 2008-08-05 11:18:09 -0400 | [diff] [blame] | 7983 | u64 index = 0; |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 7984 | u64 root_objectid; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 7985 | int ret; |
Li Zefan | 33345d01 | 2011-04-20 10:31:50 +0800 | [diff] [blame] | 7986 | u64 old_ino = btrfs_ino(old_inode); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 7987 | |
Li Zefan | 33345d01 | 2011-04-20 10:31:50 +0800 | [diff] [blame] | 7988 | if (btrfs_ino(new_dir) == BTRFS_EMPTY_SUBVOL_DIR_OBJECTID) |
Yan, Zheng | f679a84 | 2009-09-24 09:17:31 -0400 | [diff] [blame] | 7989 | return -EPERM; |
| 7990 | |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 7991 | /* we only allow rename subvolume link between subvolumes */ |
Li Zefan | 33345d01 | 2011-04-20 10:31:50 +0800 | [diff] [blame] | 7992 | if (old_ino != BTRFS_FIRST_FREE_OBJECTID && root != dest) |
Chris Mason | 3394e16 | 2008-11-17 20:42:26 -0500 | [diff] [blame] | 7993 | return -EXDEV; |
| 7994 | |
Li Zefan | 33345d01 | 2011-04-20 10:31:50 +0800 | [diff] [blame] | 7995 | if (old_ino == BTRFS_EMPTY_SUBVOL_DIR_OBJECTID || |
| 7996 | (new_inode && btrfs_ino(new_inode) == BTRFS_FIRST_FREE_OBJECTID)) |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 7997 | return -ENOTEMPTY; |
| 7998 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 7999 | if (S_ISDIR(old_inode->i_mode) && new_inode && |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 8000 | new_inode->i_size > BTRFS_EMPTY_DIR_SIZE) |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 8001 | return -ENOTEMPTY; |
Chris Mason | 9c52057 | 2012-12-17 14:26:57 -0500 | [diff] [blame] | 8002 | |
| 8003 | |
| 8004 | /* check for collisions, even if the name isn't there */ |
Josef Bacik | 4871c15 | 2013-10-09 12:24:04 -0400 | [diff] [blame] | 8005 | ret = btrfs_check_dir_item_collision(dest, new_dir->i_ino, |
Chris Mason | 9c52057 | 2012-12-17 14:26:57 -0500 | [diff] [blame] | 8006 | new_dentry->d_name.name, |
| 8007 | new_dentry->d_name.len); |
| 8008 | |
| 8009 | if (ret) { |
| 8010 | if (ret == -EEXIST) { |
| 8011 | /* we shouldn't get |
| 8012 | * eexist without a new_inode */ |
| 8013 | if (!new_inode) { |
| 8014 | WARN_ON(1); |
| 8015 | return ret; |
| 8016 | } |
| 8017 | } else { |
| 8018 | /* maybe -EOVERFLOW */ |
| 8019 | return ret; |
| 8020 | } |
| 8021 | } |
| 8022 | ret = 0; |
| 8023 | |
Chris Mason | 5a3f23d | 2009-03-31 13:27:11 -0400 | [diff] [blame] | 8024 | /* |
| 8025 | * we're using rename to replace one file with another. |
| 8026 | * and the replacement file is large. Start IO on it now so |
| 8027 | * we don't add too much work to the end of the transaction |
| 8028 | */ |
Bartlomiej Zolnierkiewicz | 4baf8c9 | 2009-08-07 13:47:08 -0400 | [diff] [blame] | 8029 | 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] | 8030 | old_inode->i_size > BTRFS_ORDERED_OPERATIONS_FLUSH_LIMIT) |
| 8031 | filemap_flush(old_inode->i_mapping); |
| 8032 | |
Yan, Zheng | 76dda93 | 2009-09-21 16:00:26 -0400 | [diff] [blame] | 8033 | /* close the racy window with snapshot create/destroy ioctl */ |
Li Zefan | 33345d01 | 2011-04-20 10:31:50 +0800 | [diff] [blame] | 8034 | if (old_ino == BTRFS_FIRST_FREE_OBJECTID) |
Yan, Zheng | 76dda93 | 2009-09-21 16:00:26 -0400 | [diff] [blame] | 8035 | down_read(&root->fs_info->subvol_sem); |
Yan, Zheng | a22285a | 2010-05-16 10:48:46 -0400 | [diff] [blame] | 8036 | /* |
| 8037 | * We want to reserve the absolute worst case amount of items. So if |
| 8038 | * both inodes are subvols and we need to unlink them then that would |
| 8039 | * require 4 item modifications, but if they are both normal inodes it |
| 8040 | * would require 5 item modifications, so we'll assume their normal |
| 8041 | * inodes. So 5 * 2 is 10, plus 1 for the new link, so 11 total items |
| 8042 | * should cover the worst case number of items we'll modify. |
| 8043 | */ |
Josef Bacik | 6e137ed | 2013-03-26 15:26:55 -0400 | [diff] [blame] | 8044 | trans = btrfs_start_transaction(root, 11); |
Johann Lombardi | b44c59a | 2011-03-31 13:23:47 +0000 | [diff] [blame] | 8045 | if (IS_ERR(trans)) { |
| 8046 | ret = PTR_ERR(trans); |
| 8047 | goto out_notrans; |
| 8048 | } |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 8049 | |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 8050 | if (dest != root) |
| 8051 | btrfs_record_root_in_trans(trans, dest); |
| 8052 | |
Yan, Zheng | a571952 | 2009-09-24 09:17:31 -0400 | [diff] [blame] | 8053 | ret = btrfs_set_inode_index(new_dir, &index); |
| 8054 | if (ret) |
| 8055 | goto out_fail; |
Chris Mason | 5a3f23d | 2009-03-31 13:27:11 -0400 | [diff] [blame] | 8056 | |
Li Zefan | 33345d01 | 2011-04-20 10:31:50 +0800 | [diff] [blame] | 8057 | if (unlikely(old_ino == BTRFS_FIRST_FREE_OBJECTID)) { |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 8058 | /* force full log commit if subvolume involved. */ |
| 8059 | root->fs_info->last_trans_log_full_commit = trans->transid; |
| 8060 | } else { |
Yan, Zheng | a571952 | 2009-09-24 09:17:31 -0400 | [diff] [blame] | 8061 | ret = btrfs_insert_inode_ref(trans, dest, |
| 8062 | new_dentry->d_name.name, |
| 8063 | new_dentry->d_name.len, |
Li Zefan | 33345d01 | 2011-04-20 10:31:50 +0800 | [diff] [blame] | 8064 | old_ino, |
| 8065 | btrfs_ino(new_dir), index); |
Yan, Zheng | a571952 | 2009-09-24 09:17:31 -0400 | [diff] [blame] | 8066 | if (ret) |
| 8067 | goto out_fail; |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 8068 | /* |
| 8069 | * this is an ugly little race, but the rename is required |
| 8070 | * to make sure that if we crash, the inode is either at the |
| 8071 | * old name or the new one. pinning the log transaction lets |
| 8072 | * us make sure we don't allow a log commit to come in after |
| 8073 | * we unlink the name but before we add the new name back in. |
| 8074 | */ |
| 8075 | btrfs_pin_log_trans(root); |
| 8076 | } |
Chris Mason | 12fcfd2 | 2009-03-24 10:24:20 -0400 | [diff] [blame] | 8077 | /* |
Chris Mason | 5a3f23d | 2009-03-31 13:27:11 -0400 | [diff] [blame] | 8078 | * make sure the inode gets flushed if it is replacing |
| 8079 | * something. |
| 8080 | */ |
Li Zefan | 33345d01 | 2011-04-20 10:31:50 +0800 | [diff] [blame] | 8081 | 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] | 8082 | btrfs_add_ordered_operation(trans, root, old_inode); |
Chris Mason | 5a3f23d | 2009-03-31 13:27:11 -0400 | [diff] [blame] | 8083 | |
Josef Bacik | 0c4d2d9 | 2012-04-05 15:03:02 -0400 | [diff] [blame] | 8084 | inode_inc_iversion(old_dir); |
| 8085 | inode_inc_iversion(new_dir); |
| 8086 | inode_inc_iversion(old_inode); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 8087 | old_dir->i_ctime = old_dir->i_mtime = ctime; |
| 8088 | new_dir->i_ctime = new_dir->i_mtime = ctime; |
| 8089 | old_inode->i_ctime = ctime; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 8090 | |
Chris Mason | 12fcfd2 | 2009-03-24 10:24:20 -0400 | [diff] [blame] | 8091 | if (old_dentry->d_parent != new_dentry->d_parent) |
| 8092 | btrfs_record_unlink_dir(trans, old_dir, old_inode, 1); |
| 8093 | |
Li Zefan | 33345d01 | 2011-04-20 10:31:50 +0800 | [diff] [blame] | 8094 | if (unlikely(old_ino == BTRFS_FIRST_FREE_OBJECTID)) { |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 8095 | root_objectid = BTRFS_I(old_inode)->root->root_key.objectid; |
| 8096 | ret = btrfs_unlink_subvol(trans, root, old_dir, root_objectid, |
| 8097 | old_dentry->d_name.name, |
| 8098 | old_dentry->d_name.len); |
| 8099 | } else { |
Al Viro | 9298679 | 2011-03-04 17:14:37 +0000 | [diff] [blame] | 8100 | ret = __btrfs_unlink_inode(trans, root, old_dir, |
| 8101 | old_dentry->d_inode, |
| 8102 | old_dentry->d_name.name, |
| 8103 | old_dentry->d_name.len); |
| 8104 | if (!ret) |
| 8105 | ret = btrfs_update_inode(trans, root, old_inode); |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 8106 | } |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 8107 | if (ret) { |
| 8108 | btrfs_abort_transaction(trans, root, ret); |
| 8109 | goto out_fail; |
| 8110 | } |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 8111 | |
| 8112 | if (new_inode) { |
Josef Bacik | 0c4d2d9 | 2012-04-05 15:03:02 -0400 | [diff] [blame] | 8113 | inode_inc_iversion(new_inode); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 8114 | new_inode->i_ctime = CURRENT_TIME; |
Li Zefan | 33345d01 | 2011-04-20 10:31:50 +0800 | [diff] [blame] | 8115 | if (unlikely(btrfs_ino(new_inode) == |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 8116 | BTRFS_EMPTY_SUBVOL_DIR_OBJECTID)) { |
| 8117 | root_objectid = BTRFS_I(new_inode)->location.objectid; |
| 8118 | ret = btrfs_unlink_subvol(trans, dest, new_dir, |
| 8119 | root_objectid, |
| 8120 | new_dentry->d_name.name, |
| 8121 | new_dentry->d_name.len); |
| 8122 | BUG_ON(new_inode->i_nlink == 0); |
| 8123 | } else { |
| 8124 | ret = btrfs_unlink_inode(trans, dest, new_dir, |
| 8125 | new_dentry->d_inode, |
| 8126 | new_dentry->d_name.name, |
| 8127 | new_dentry->d_name.len); |
| 8128 | } |
Josef Bacik | 4ef31a4 | 2013-08-13 14:10:08 -0400 | [diff] [blame] | 8129 | if (!ret && new_inode->i_nlink == 0) |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 8130 | ret = btrfs_orphan_add(trans, new_dentry->d_inode); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 8131 | if (ret) { |
| 8132 | btrfs_abort_transaction(trans, root, ret); |
| 8133 | goto out_fail; |
| 8134 | } |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 8135 | } |
Josef Bacik | aec7477 | 2008-07-24 12:12:38 -0400 | [diff] [blame] | 8136 | |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 8137 | ret = btrfs_add_link(trans, new_dir, old_inode, |
| 8138 | new_dentry->d_name.name, |
Yan, Zheng | a571952 | 2009-09-24 09:17:31 -0400 | [diff] [blame] | 8139 | new_dentry->d_name.len, 0, index); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 8140 | if (ret) { |
| 8141 | btrfs_abort_transaction(trans, root, ret); |
| 8142 | goto out_fail; |
| 8143 | } |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 8144 | |
Li Zefan | 33345d01 | 2011-04-20 10:31:50 +0800 | [diff] [blame] | 8145 | if (old_ino != BTRFS_FIRST_FREE_OBJECTID) { |
Al Viro | 10d9f30 | 2011-07-16 23:09:10 -0400 | [diff] [blame] | 8146 | struct dentry *parent = new_dentry->d_parent; |
Josef Bacik | 6a91221 | 2010-11-20 09:48:00 +0000 | [diff] [blame] | 8147 | btrfs_log_new_name(trans, old_inode, old_dir, parent); |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 8148 | btrfs_end_log_trans(root); |
| 8149 | } |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 8150 | out_fail: |
Josef Bacik | 7ad85bb | 2012-01-12 19:10:12 -0500 | [diff] [blame] | 8151 | btrfs_end_transaction(trans, root); |
Johann Lombardi | b44c59a | 2011-03-31 13:23:47 +0000 | [diff] [blame] | 8152 | out_notrans: |
Li Zefan | 33345d01 | 2011-04-20 10:31:50 +0800 | [diff] [blame] | 8153 | if (old_ino == BTRFS_FIRST_FREE_OBJECTID) |
Yan, Zheng | 76dda93 | 2009-09-21 16:00:26 -0400 | [diff] [blame] | 8154 | up_read(&root->fs_info->subvol_sem); |
Josef Bacik | 9ed74f2 | 2009-09-11 16:12:44 -0400 | [diff] [blame] | 8155 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 8156 | return ret; |
| 8157 | } |
| 8158 | |
Miao Xie | 8ccf6f19 | 2012-10-25 09:28:04 +0000 | [diff] [blame] | 8159 | static void btrfs_run_delalloc_work(struct btrfs_work *work) |
| 8160 | { |
| 8161 | struct btrfs_delalloc_work *delalloc_work; |
| 8162 | |
| 8163 | delalloc_work = container_of(work, struct btrfs_delalloc_work, |
| 8164 | work); |
| 8165 | if (delalloc_work->wait) |
| 8166 | btrfs_wait_ordered_range(delalloc_work->inode, 0, (u64)-1); |
| 8167 | else |
| 8168 | filemap_flush(delalloc_work->inode->i_mapping); |
| 8169 | |
| 8170 | if (delalloc_work->delay_iput) |
| 8171 | btrfs_add_delayed_iput(delalloc_work->inode); |
| 8172 | else |
| 8173 | iput(delalloc_work->inode); |
| 8174 | complete(&delalloc_work->completion); |
| 8175 | } |
| 8176 | |
| 8177 | struct btrfs_delalloc_work *btrfs_alloc_delalloc_work(struct inode *inode, |
| 8178 | int wait, int delay_iput) |
| 8179 | { |
| 8180 | struct btrfs_delalloc_work *work; |
| 8181 | |
| 8182 | work = kmem_cache_zalloc(btrfs_delalloc_work_cachep, GFP_NOFS); |
| 8183 | if (!work) |
| 8184 | return NULL; |
| 8185 | |
| 8186 | init_completion(&work->completion); |
| 8187 | INIT_LIST_HEAD(&work->list); |
| 8188 | work->inode = inode; |
| 8189 | work->wait = wait; |
| 8190 | work->delay_iput = delay_iput; |
| 8191 | work->work.func = btrfs_run_delalloc_work; |
| 8192 | |
| 8193 | return work; |
| 8194 | } |
| 8195 | |
| 8196 | void btrfs_wait_and_free_delalloc_work(struct btrfs_delalloc_work *work) |
| 8197 | { |
| 8198 | wait_for_completion(&work->completion); |
| 8199 | kmem_cache_free(btrfs_delalloc_work_cachep, work); |
| 8200 | } |
| 8201 | |
Chris Mason | d352ac6 | 2008-09-29 15:18:18 -0400 | [diff] [blame] | 8202 | /* |
| 8203 | * some fairly slow code that needs optimization. This walks the list |
| 8204 | * of all the inodes with pending delalloc and forces them to disk. |
| 8205 | */ |
Miao Xie | eb73c1b | 2013-05-15 07:48:22 +0000 | [diff] [blame] | 8206 | static int __start_delalloc_inodes(struct btrfs_root *root, int delay_iput) |
Chris Mason | ea8c281 | 2008-08-04 23:17:27 -0400 | [diff] [blame] | 8207 | { |
Chris Mason | ea8c281 | 2008-08-04 23:17:27 -0400 | [diff] [blame] | 8208 | struct btrfs_inode *binode; |
Zheng Yan | 5b21f2e | 2008-09-26 10:05:38 -0400 | [diff] [blame] | 8209 | struct inode *inode; |
Miao Xie | 8ccf6f19 | 2012-10-25 09:28:04 +0000 | [diff] [blame] | 8210 | struct btrfs_delalloc_work *work, *next; |
| 8211 | struct list_head works; |
Miao Xie | 1eafa6c | 2013-01-22 10:49:00 +0000 | [diff] [blame] | 8212 | struct list_head splice; |
Miao Xie | 8ccf6f19 | 2012-10-25 09:28:04 +0000 | [diff] [blame] | 8213 | int ret = 0; |
Chris Mason | ea8c281 | 2008-08-04 23:17:27 -0400 | [diff] [blame] | 8214 | |
Miao Xie | 8ccf6f19 | 2012-10-25 09:28:04 +0000 | [diff] [blame] | 8215 | INIT_LIST_HEAD(&works); |
Miao Xie | 1eafa6c | 2013-01-22 10:49:00 +0000 | [diff] [blame] | 8216 | INIT_LIST_HEAD(&splice); |
Miao Xie | 63607cc | 2013-01-22 10:50:35 +0000 | [diff] [blame] | 8217 | |
Miao Xie | eb73c1b | 2013-05-15 07:48:22 +0000 | [diff] [blame] | 8218 | spin_lock(&root->delalloc_lock); |
| 8219 | list_splice_init(&root->delalloc_inodes, &splice); |
Miao Xie | 1eafa6c | 2013-01-22 10:49:00 +0000 | [diff] [blame] | 8220 | while (!list_empty(&splice)) { |
| 8221 | binode = list_entry(splice.next, struct btrfs_inode, |
Chris Mason | ea8c281 | 2008-08-04 23:17:27 -0400 | [diff] [blame] | 8222 | delalloc_inodes); |
Miao Xie | 1eafa6c | 2013-01-22 10:49:00 +0000 | [diff] [blame] | 8223 | |
Miao Xie | eb73c1b | 2013-05-15 07:48:22 +0000 | [diff] [blame] | 8224 | list_move_tail(&binode->delalloc_inodes, |
| 8225 | &root->delalloc_inodes); |
Zheng Yan | 5b21f2e | 2008-09-26 10:05:38 -0400 | [diff] [blame] | 8226 | inode = igrab(&binode->vfs_inode); |
Miao Xie | df0af1a | 2013-01-29 10:11:59 +0000 | [diff] [blame] | 8227 | if (!inode) { |
Miao Xie | eb73c1b | 2013-05-15 07:48:22 +0000 | [diff] [blame] | 8228 | cond_resched_lock(&root->delalloc_lock); |
Miao Xie | 1eafa6c | 2013-01-22 10:49:00 +0000 | [diff] [blame] | 8229 | continue; |
Miao Xie | df0af1a | 2013-01-29 10:11:59 +0000 | [diff] [blame] | 8230 | } |
Miao Xie | eb73c1b | 2013-05-15 07:48:22 +0000 | [diff] [blame] | 8231 | spin_unlock(&root->delalloc_lock); |
Miao Xie | 1eafa6c | 2013-01-22 10:49:00 +0000 | [diff] [blame] | 8232 | |
| 8233 | work = btrfs_alloc_delalloc_work(inode, 0, delay_iput); |
| 8234 | if (unlikely(!work)) { |
Josef Bacik | f4ab9ea | 2013-09-17 11:25:44 -0400 | [diff] [blame] | 8235 | if (delay_iput) |
| 8236 | btrfs_add_delayed_iput(inode); |
| 8237 | else |
| 8238 | iput(inode); |
Miao Xie | 1eafa6c | 2013-01-22 10:49:00 +0000 | [diff] [blame] | 8239 | ret = -ENOMEM; |
| 8240 | goto out; |
Zheng Yan | 5b21f2e | 2008-09-26 10:05:38 -0400 | [diff] [blame] | 8241 | } |
Miao Xie | 1eafa6c | 2013-01-22 10:49:00 +0000 | [diff] [blame] | 8242 | list_add_tail(&work->list, &works); |
| 8243 | btrfs_queue_worker(&root->fs_info->flush_workers, |
| 8244 | &work->work); |
| 8245 | |
Zheng Yan | 5b21f2e | 2008-09-26 10:05:38 -0400 | [diff] [blame] | 8246 | cond_resched(); |
Miao Xie | eb73c1b | 2013-05-15 07:48:22 +0000 | [diff] [blame] | 8247 | spin_lock(&root->delalloc_lock); |
Chris Mason | ea8c281 | 2008-08-04 23:17:27 -0400 | [diff] [blame] | 8248 | } |
Miao Xie | eb73c1b | 2013-05-15 07:48:22 +0000 | [diff] [blame] | 8249 | spin_unlock(&root->delalloc_lock); |
Chris Mason | 8c8bee1 | 2008-09-29 11:19:10 -0400 | [diff] [blame] | 8250 | |
Miao Xie | 1eafa6c | 2013-01-22 10:49:00 +0000 | [diff] [blame] | 8251 | list_for_each_entry_safe(work, next, &works, list) { |
| 8252 | list_del_init(&work->list); |
| 8253 | btrfs_wait_and_free_delalloc_work(work); |
| 8254 | } |
Miao Xie | eb73c1b | 2013-05-15 07:48:22 +0000 | [diff] [blame] | 8255 | return 0; |
| 8256 | out: |
| 8257 | list_for_each_entry_safe(work, next, &works, list) { |
| 8258 | list_del_init(&work->list); |
| 8259 | btrfs_wait_and_free_delalloc_work(work); |
| 8260 | } |
Miao Xie | 1eafa6c | 2013-01-22 10:49:00 +0000 | [diff] [blame] | 8261 | |
Miao Xie | eb73c1b | 2013-05-15 07:48:22 +0000 | [diff] [blame] | 8262 | if (!list_empty_careful(&splice)) { |
| 8263 | spin_lock(&root->delalloc_lock); |
| 8264 | list_splice_tail(&splice, &root->delalloc_inodes); |
| 8265 | spin_unlock(&root->delalloc_lock); |
| 8266 | } |
| 8267 | return ret; |
| 8268 | } |
| 8269 | |
| 8270 | int btrfs_start_delalloc_inodes(struct btrfs_root *root, int delay_iput) |
| 8271 | { |
| 8272 | int ret; |
| 8273 | |
| 8274 | if (root->fs_info->sb->s_flags & MS_RDONLY) |
| 8275 | return -EROFS; |
| 8276 | |
| 8277 | ret = __start_delalloc_inodes(root, delay_iput); |
| 8278 | /* |
| 8279 | * the filemap_flush will queue IO into the worker threads, but |
Chris Mason | 8c8bee1 | 2008-09-29 11:19:10 -0400 | [diff] [blame] | 8280 | * we have to make sure the IO is actually started and that |
| 8281 | * ordered extents get created before we return |
| 8282 | */ |
| 8283 | atomic_inc(&root->fs_info->async_submit_draining); |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 8284 | while (atomic_read(&root->fs_info->nr_async_submits) || |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 8285 | atomic_read(&root->fs_info->async_delalloc_pages)) { |
Chris Mason | 8c8bee1 | 2008-09-29 11:19:10 -0400 | [diff] [blame] | 8286 | wait_event(root->fs_info->async_submit_wait, |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 8287 | (atomic_read(&root->fs_info->nr_async_submits) == 0 && |
| 8288 | atomic_read(&root->fs_info->async_delalloc_pages) == 0)); |
Chris Mason | 8c8bee1 | 2008-09-29 11:19:10 -0400 | [diff] [blame] | 8289 | } |
| 8290 | atomic_dec(&root->fs_info->async_submit_draining); |
Miao Xie | eb73c1b | 2013-05-15 07:48:22 +0000 | [diff] [blame] | 8291 | return ret; |
| 8292 | } |
| 8293 | |
| 8294 | int btrfs_start_all_delalloc_inodes(struct btrfs_fs_info *fs_info, |
| 8295 | int delay_iput) |
| 8296 | { |
| 8297 | struct btrfs_root *root; |
| 8298 | struct list_head splice; |
| 8299 | int ret; |
| 8300 | |
| 8301 | if (fs_info->sb->s_flags & MS_RDONLY) |
| 8302 | return -EROFS; |
| 8303 | |
| 8304 | INIT_LIST_HEAD(&splice); |
| 8305 | |
| 8306 | spin_lock(&fs_info->delalloc_root_lock); |
| 8307 | list_splice_init(&fs_info->delalloc_roots, &splice); |
| 8308 | while (!list_empty(&splice)) { |
| 8309 | root = list_first_entry(&splice, struct btrfs_root, |
| 8310 | delalloc_root); |
| 8311 | root = btrfs_grab_fs_root(root); |
| 8312 | BUG_ON(!root); |
| 8313 | list_move_tail(&root->delalloc_root, |
| 8314 | &fs_info->delalloc_roots); |
| 8315 | spin_unlock(&fs_info->delalloc_root_lock); |
| 8316 | |
| 8317 | ret = __start_delalloc_inodes(root, delay_iput); |
| 8318 | btrfs_put_fs_root(root); |
| 8319 | if (ret) |
| 8320 | goto out; |
| 8321 | |
| 8322 | spin_lock(&fs_info->delalloc_root_lock); |
| 8323 | } |
| 8324 | spin_unlock(&fs_info->delalloc_root_lock); |
| 8325 | |
| 8326 | atomic_inc(&fs_info->async_submit_draining); |
| 8327 | while (atomic_read(&fs_info->nr_async_submits) || |
| 8328 | atomic_read(&fs_info->async_delalloc_pages)) { |
| 8329 | wait_event(fs_info->async_submit_wait, |
| 8330 | (atomic_read(&fs_info->nr_async_submits) == 0 && |
| 8331 | atomic_read(&fs_info->async_delalloc_pages) == 0)); |
| 8332 | } |
| 8333 | atomic_dec(&fs_info->async_submit_draining); |
Miao Xie | 1eafa6c | 2013-01-22 10:49:00 +0000 | [diff] [blame] | 8334 | return 0; |
Miao Xie | 8ccf6f19 | 2012-10-25 09:28:04 +0000 | [diff] [blame] | 8335 | out: |
Miao Xie | 1eafa6c | 2013-01-22 10:49:00 +0000 | [diff] [blame] | 8336 | if (!list_empty_careful(&splice)) { |
Miao Xie | eb73c1b | 2013-05-15 07:48:22 +0000 | [diff] [blame] | 8337 | spin_lock(&fs_info->delalloc_root_lock); |
| 8338 | list_splice_tail(&splice, &fs_info->delalloc_roots); |
| 8339 | spin_unlock(&fs_info->delalloc_root_lock); |
Miao Xie | 1eafa6c | 2013-01-22 10:49:00 +0000 | [diff] [blame] | 8340 | } |
Miao Xie | 8ccf6f19 | 2012-10-25 09:28:04 +0000 | [diff] [blame] | 8341 | return ret; |
Chris Mason | ea8c281 | 2008-08-04 23:17:27 -0400 | [diff] [blame] | 8342 | } |
| 8343 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 8344 | static int btrfs_symlink(struct inode *dir, struct dentry *dentry, |
| 8345 | const char *symname) |
| 8346 | { |
| 8347 | struct btrfs_trans_handle *trans; |
| 8348 | struct btrfs_root *root = BTRFS_I(dir)->root; |
| 8349 | struct btrfs_path *path; |
| 8350 | struct btrfs_key key; |
Chris Mason | 1832a6d | 2007-12-21 16:27:21 -0500 | [diff] [blame] | 8351 | struct inode *inode = NULL; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 8352 | int err; |
| 8353 | int drop_inode = 0; |
| 8354 | u64 objectid; |
Chris Mason | 00e4e6b | 2008-08-05 11:18:09 -0400 | [diff] [blame] | 8355 | u64 index = 0 ; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 8356 | int name_len; |
| 8357 | int datasize; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 8358 | unsigned long ptr; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 8359 | struct btrfs_file_extent_item *ei; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 8360 | struct extent_buffer *leaf; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 8361 | |
Filipe David Borba Manana | f06becc | 2013-09-16 09:53:28 +0100 | [diff] [blame] | 8362 | name_len = strlen(symname); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 8363 | if (name_len > BTRFS_MAX_INLINE_DATA_SIZE(root)) |
| 8364 | return -ENAMETOOLONG; |
Chris Mason | 1832a6d | 2007-12-21 16:27:21 -0500 | [diff] [blame] | 8365 | |
Josef Bacik | 9ed74f2 | 2009-09-11 16:12:44 -0400 | [diff] [blame] | 8366 | /* |
| 8367 | * 2 items for inode item and ref |
| 8368 | * 2 items for dir items |
| 8369 | * 1 item for xattr if selinux is on |
| 8370 | */ |
Yan, Zheng | a22285a | 2010-05-16 10:48:46 -0400 | [diff] [blame] | 8371 | trans = btrfs_start_transaction(root, 5); |
| 8372 | if (IS_ERR(trans)) |
| 8373 | return PTR_ERR(trans); |
Chris Mason | 1832a6d | 2007-12-21 16:27:21 -0500 | [diff] [blame] | 8374 | |
Li Zefan | 581bb05 | 2011-04-20 10:06:11 +0800 | [diff] [blame] | 8375 | err = btrfs_find_free_ino(root, &objectid); |
| 8376 | if (err) |
| 8377 | goto out_unlock; |
| 8378 | |
Josef Bacik | aec7477 | 2008-07-24 12:12:38 -0400 | [diff] [blame] | 8379 | inode = btrfs_new_inode(trans, root, dir, dentry->d_name.name, |
Li Zefan | 33345d01 | 2011-04-20 10:31:50 +0800 | [diff] [blame] | 8380 | dentry->d_name.len, btrfs_ino(dir), objectid, |
Josef Bacik | d82a6f1d | 2011-05-11 15:26:06 -0400 | [diff] [blame] | 8381 | S_IFLNK|S_IRWXUGO, &index); |
Tsutomu Itoh | 7cf96da | 2011-04-25 19:43:53 -0400 | [diff] [blame] | 8382 | if (IS_ERR(inode)) { |
| 8383 | err = PTR_ERR(inode); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 8384 | goto out_unlock; |
Tsutomu Itoh | 7cf96da | 2011-04-25 19:43:53 -0400 | [diff] [blame] | 8385 | } |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 8386 | |
Eric Paris | 2a7dba3 | 2011-02-01 11:05:39 -0500 | [diff] [blame] | 8387 | err = btrfs_init_inode_security(trans, inode, dir, &dentry->d_name); |
Josef Bacik | 33268ea | 2008-07-24 12:16:36 -0400 | [diff] [blame] | 8388 | if (err) { |
| 8389 | drop_inode = 1; |
| 8390 | goto out_unlock; |
| 8391 | } |
| 8392 | |
Casey Schaufler | ad19db7 | 2011-12-15 10:09:07 -0500 | [diff] [blame] | 8393 | /* |
| 8394 | * If the active LSM wants to access the inode during |
| 8395 | * d_instantiate it needs these. Smack checks to see |
| 8396 | * if the filesystem supports xattrs by looking at the |
| 8397 | * ops vector. |
| 8398 | */ |
| 8399 | inode->i_fop = &btrfs_file_operations; |
| 8400 | inode->i_op = &btrfs_file_inode_operations; |
| 8401 | |
Josef Bacik | a1b075d | 2010-11-19 20:36:11 +0000 | [diff] [blame] | 8402 | err = btrfs_add_nondir(trans, dir, dentry, inode, 0, index); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 8403 | if (err) |
| 8404 | drop_inode = 1; |
| 8405 | else { |
| 8406 | inode->i_mapping->a_ops = &btrfs_aops; |
Chris Mason | 0416008 | 2008-03-26 10:28:07 -0400 | [diff] [blame] | 8407 | inode->i_mapping->backing_dev_info = &root->fs_info->bdi; |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 8408 | BTRFS_I(inode)->io_tree.ops = &btrfs_extent_io_ops; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 8409 | } |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 8410 | if (drop_inode) |
| 8411 | goto out_unlock; |
| 8412 | |
| 8413 | path = btrfs_alloc_path(); |
Mark Fasheh | d8926bb | 2011-07-13 10:38:47 -0700 | [diff] [blame] | 8414 | if (!path) { |
| 8415 | err = -ENOMEM; |
| 8416 | drop_inode = 1; |
| 8417 | goto out_unlock; |
| 8418 | } |
Li Zefan | 33345d01 | 2011-04-20 10:31:50 +0800 | [diff] [blame] | 8419 | key.objectid = btrfs_ino(inode); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 8420 | key.offset = 0; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 8421 | btrfs_set_key_type(&key, BTRFS_EXTENT_DATA_KEY); |
| 8422 | datasize = btrfs_file_extent_calc_inline_size(name_len); |
| 8423 | err = btrfs_insert_empty_item(trans, root, path, &key, |
| 8424 | datasize); |
Chris Mason | 54aa1f4 | 2007-06-22 14:16:25 -0400 | [diff] [blame] | 8425 | if (err) { |
| 8426 | drop_inode = 1; |
Julia Lawall | b083916 | 2011-05-14 07:10:51 +0000 | [diff] [blame] | 8427 | btrfs_free_path(path); |
Chris Mason | 54aa1f4 | 2007-06-22 14:16:25 -0400 | [diff] [blame] | 8428 | goto out_unlock; |
| 8429 | } |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 8430 | leaf = path->nodes[0]; |
| 8431 | ei = btrfs_item_ptr(leaf, path->slots[0], |
| 8432 | struct btrfs_file_extent_item); |
| 8433 | btrfs_set_file_extent_generation(leaf, ei, trans->transid); |
| 8434 | btrfs_set_file_extent_type(leaf, ei, |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 8435 | BTRFS_FILE_EXTENT_INLINE); |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 8436 | btrfs_set_file_extent_encryption(leaf, ei, 0); |
| 8437 | btrfs_set_file_extent_compression(leaf, ei, 0); |
| 8438 | btrfs_set_file_extent_other_encoding(leaf, ei, 0); |
| 8439 | btrfs_set_file_extent_ram_bytes(leaf, ei, name_len); |
| 8440 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 8441 | ptr = btrfs_file_extent_inline_start(ei); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 8442 | write_extent_buffer(leaf, symname, ptr, name_len); |
| 8443 | btrfs_mark_buffer_dirty(leaf); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 8444 | btrfs_free_path(path); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 8445 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 8446 | inode->i_op = &btrfs_symlink_inode_operations; |
| 8447 | inode->i_mapping->a_ops = &btrfs_symlink_aops; |
Chris Mason | 0416008 | 2008-03-26 10:28:07 -0400 | [diff] [blame] | 8448 | inode->i_mapping->backing_dev_info = &root->fs_info->bdi; |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 8449 | inode_set_bytes(inode, name_len); |
Filipe David Borba Manana | f06becc | 2013-09-16 09:53:28 +0100 | [diff] [blame] | 8450 | btrfs_i_size_write(inode, name_len); |
Chris Mason | 54aa1f4 | 2007-06-22 14:16:25 -0400 | [diff] [blame] | 8451 | err = btrfs_update_inode(trans, root, inode); |
| 8452 | if (err) |
| 8453 | drop_inode = 1; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 8454 | |
| 8455 | out_unlock: |
Al Viro | 08c422c | 2011-12-23 07:58:13 -0500 | [diff] [blame] | 8456 | if (!err) |
| 8457 | d_instantiate(dentry, inode); |
Josef Bacik | 7ad85bb | 2012-01-12 19:10:12 -0500 | [diff] [blame] | 8458 | btrfs_end_transaction(trans, root); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 8459 | if (drop_inode) { |
| 8460 | inode_dec_link_count(inode); |
| 8461 | iput(inode); |
| 8462 | } |
Liu Bo | b53d3f5 | 2012-11-14 14:34:34 +0000 | [diff] [blame] | 8463 | btrfs_btree_balance_dirty(root); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 8464 | return err; |
| 8465 | } |
Chris Mason | 1643298 | 2008-04-10 10:23:21 -0400 | [diff] [blame] | 8466 | |
Josef Bacik | 0af3d00 | 2010-06-21 14:48:16 -0400 | [diff] [blame] | 8467 | static int __btrfs_prealloc_file_range(struct inode *inode, int mode, |
| 8468 | u64 start, u64 num_bytes, u64 min_size, |
| 8469 | loff_t actual_len, u64 *alloc_hint, |
| 8470 | struct btrfs_trans_handle *trans) |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 8471 | { |
Josef Bacik | 5dc562c | 2012-08-17 13:14:17 -0400 | [diff] [blame] | 8472 | struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree; |
| 8473 | struct extent_map *em; |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 8474 | struct btrfs_root *root = BTRFS_I(inode)->root; |
| 8475 | struct btrfs_key ins; |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 8476 | u64 cur_offset = start; |
Josef Bacik | 55a61d1 | 2010-11-22 18:50:32 +0000 | [diff] [blame] | 8477 | u64 i_size; |
Chris Mason | 154ea28 | 2013-03-05 11:11:26 -0500 | [diff] [blame] | 8478 | u64 cur_bytes; |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 8479 | int ret = 0; |
Josef Bacik | 0af3d00 | 2010-06-21 14:48:16 -0400 | [diff] [blame] | 8480 | bool own_trans = true; |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 8481 | |
Josef Bacik | 0af3d00 | 2010-06-21 14:48:16 -0400 | [diff] [blame] | 8482 | if (trans) |
| 8483 | own_trans = false; |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 8484 | while (num_bytes > 0) { |
Josef Bacik | 0af3d00 | 2010-06-21 14:48:16 -0400 | [diff] [blame] | 8485 | if (own_trans) { |
| 8486 | trans = btrfs_start_transaction(root, 3); |
| 8487 | if (IS_ERR(trans)) { |
| 8488 | ret = PTR_ERR(trans); |
| 8489 | break; |
| 8490 | } |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 8491 | } |
Yan, Zheng | 5a303d5 | 2009-11-12 09:34:52 +0000 | [diff] [blame] | 8492 | |
Chris Mason | 154ea28 | 2013-03-05 11:11:26 -0500 | [diff] [blame] | 8493 | cur_bytes = min(num_bytes, 256ULL * 1024 * 1024); |
| 8494 | cur_bytes = max(cur_bytes, min_size); |
Josef Bacik | 0036158 | 2013-08-14 14:02:47 -0400 | [diff] [blame] | 8495 | ret = btrfs_reserve_extent(root, cur_bytes, min_size, 0, |
| 8496 | *alloc_hint, &ins, 1); |
Yan, Zheng | 5a303d5 | 2009-11-12 09:34:52 +0000 | [diff] [blame] | 8497 | if (ret) { |
Josef Bacik | 0af3d00 | 2010-06-21 14:48:16 -0400 | [diff] [blame] | 8498 | if (own_trans) |
| 8499 | btrfs_end_transaction(trans, root); |
Yan, Zheng | a22285a | 2010-05-16 10:48:46 -0400 | [diff] [blame] | 8500 | break; |
Yan, Zheng | 5a303d5 | 2009-11-12 09:34:52 +0000 | [diff] [blame] | 8501 | } |
| 8502 | |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 8503 | ret = insert_reserved_file_extent(trans, inode, |
| 8504 | cur_offset, ins.objectid, |
| 8505 | ins.offset, ins.offset, |
Yan, Zheng | 920bbbf | 2009-11-12 09:34:08 +0000 | [diff] [blame] | 8506 | ins.offset, 0, 0, 0, |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 8507 | BTRFS_FILE_EXTENT_PREALLOC); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 8508 | if (ret) { |
Josef Bacik | 857cc2f | 2013-10-07 15:21:08 -0400 | [diff] [blame] | 8509 | btrfs_free_reserved_extent(root, ins.objectid, |
| 8510 | ins.offset); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 8511 | btrfs_abort_transaction(trans, root, ret); |
| 8512 | if (own_trans) |
| 8513 | btrfs_end_transaction(trans, root); |
| 8514 | break; |
| 8515 | } |
Chris Mason | a1ed835 | 2009-09-11 12:27:37 -0400 | [diff] [blame] | 8516 | btrfs_drop_extent_cache(inode, cur_offset, |
| 8517 | cur_offset + ins.offset -1, 0); |
Yan, Zheng | 5a303d5 | 2009-11-12 09:34:52 +0000 | [diff] [blame] | 8518 | |
Josef Bacik | 5dc562c | 2012-08-17 13:14:17 -0400 | [diff] [blame] | 8519 | em = alloc_extent_map(); |
| 8520 | if (!em) { |
| 8521 | set_bit(BTRFS_INODE_NEEDS_FULL_SYNC, |
| 8522 | &BTRFS_I(inode)->runtime_flags); |
| 8523 | goto next; |
| 8524 | } |
| 8525 | |
| 8526 | em->start = cur_offset; |
| 8527 | em->orig_start = cur_offset; |
| 8528 | em->len = ins.offset; |
| 8529 | em->block_start = ins.objectid; |
| 8530 | em->block_len = ins.offset; |
Josef Bacik | b493968 | 2012-12-03 10:31:19 -0500 | [diff] [blame] | 8531 | em->orig_block_len = ins.offset; |
Josef Bacik | cc95bef | 2013-04-04 14:31:27 -0400 | [diff] [blame] | 8532 | em->ram_bytes = ins.offset; |
Josef Bacik | 5dc562c | 2012-08-17 13:14:17 -0400 | [diff] [blame] | 8533 | em->bdev = root->fs_info->fs_devices->latest_bdev; |
| 8534 | set_bit(EXTENT_FLAG_PREALLOC, &em->flags); |
| 8535 | em->generation = trans->transid; |
| 8536 | |
| 8537 | while (1) { |
| 8538 | write_lock(&em_tree->lock); |
Josef Bacik | 09a2a8f9 | 2013-04-05 16:51:15 -0400 | [diff] [blame] | 8539 | ret = add_extent_mapping(em_tree, em, 1); |
Josef Bacik | 5dc562c | 2012-08-17 13:14:17 -0400 | [diff] [blame] | 8540 | write_unlock(&em_tree->lock); |
| 8541 | if (ret != -EEXIST) |
| 8542 | break; |
| 8543 | btrfs_drop_extent_cache(inode, cur_offset, |
| 8544 | cur_offset + ins.offset - 1, |
| 8545 | 0); |
| 8546 | } |
| 8547 | free_extent_map(em); |
| 8548 | next: |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 8549 | num_bytes -= ins.offset; |
| 8550 | cur_offset += ins.offset; |
Yan, Zheng | efa5646 | 2010-05-16 10:49:59 -0400 | [diff] [blame] | 8551 | *alloc_hint = ins.objectid + ins.offset; |
Yan, Zheng | 5a303d5 | 2009-11-12 09:34:52 +0000 | [diff] [blame] | 8552 | |
Josef Bacik | 0c4d2d9 | 2012-04-05 15:03:02 -0400 | [diff] [blame] | 8553 | inode_inc_iversion(inode); |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 8554 | inode->i_ctime = CURRENT_TIME; |
Christoph Hellwig | 6cbff00 | 2009-04-17 10:37:41 +0200 | [diff] [blame] | 8555 | BTRFS_I(inode)->flags |= BTRFS_INODE_PREALLOC; |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 8556 | if (!(mode & FALLOC_FL_KEEP_SIZE) && |
Yan, Zheng | efa5646 | 2010-05-16 10:49:59 -0400 | [diff] [blame] | 8557 | (actual_len > inode->i_size) && |
| 8558 | (cur_offset > inode->i_size)) { |
Aneesh Kumar K.V | d1ea6a6 | 2010-01-20 07:28:54 +0000 | [diff] [blame] | 8559 | if (cur_offset > actual_len) |
Josef Bacik | 55a61d1 | 2010-11-22 18:50:32 +0000 | [diff] [blame] | 8560 | i_size = actual_len; |
Aneesh Kumar K.V | d1ea6a6 | 2010-01-20 07:28:54 +0000 | [diff] [blame] | 8561 | else |
Josef Bacik | 55a61d1 | 2010-11-22 18:50:32 +0000 | [diff] [blame] | 8562 | i_size = cur_offset; |
| 8563 | i_size_write(inode, i_size); |
| 8564 | btrfs_ordered_update_i_size(inode, i_size, NULL); |
Yan, Zheng | 5a303d5 | 2009-11-12 09:34:52 +0000 | [diff] [blame] | 8565 | } |
| 8566 | |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 8567 | ret = btrfs_update_inode(trans, root, inode); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 8568 | |
| 8569 | if (ret) { |
| 8570 | btrfs_abort_transaction(trans, root, ret); |
| 8571 | if (own_trans) |
| 8572 | btrfs_end_transaction(trans, root); |
| 8573 | break; |
| 8574 | } |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 8575 | |
Josef Bacik | 0af3d00 | 2010-06-21 14:48:16 -0400 | [diff] [blame] | 8576 | if (own_trans) |
| 8577 | btrfs_end_transaction(trans, root); |
Yan, Zheng | 5a303d5 | 2009-11-12 09:34:52 +0000 | [diff] [blame] | 8578 | } |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 8579 | return ret; |
| 8580 | } |
| 8581 | |
Josef Bacik | 0af3d00 | 2010-06-21 14:48:16 -0400 | [diff] [blame] | 8582 | int btrfs_prealloc_file_range(struct inode *inode, int mode, |
| 8583 | u64 start, u64 num_bytes, u64 min_size, |
| 8584 | loff_t actual_len, u64 *alloc_hint) |
| 8585 | { |
| 8586 | return __btrfs_prealloc_file_range(inode, mode, start, num_bytes, |
| 8587 | min_size, actual_len, alloc_hint, |
| 8588 | NULL); |
| 8589 | } |
| 8590 | |
| 8591 | int btrfs_prealloc_file_range_trans(struct inode *inode, |
| 8592 | struct btrfs_trans_handle *trans, int mode, |
| 8593 | u64 start, u64 num_bytes, u64 min_size, |
| 8594 | loff_t actual_len, u64 *alloc_hint) |
| 8595 | { |
| 8596 | return __btrfs_prealloc_file_range(inode, mode, start, num_bytes, |
| 8597 | min_size, actual_len, alloc_hint, trans); |
| 8598 | } |
| 8599 | |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 8600 | static int btrfs_set_page_dirty(struct page *page) |
| 8601 | { |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 8602 | return __set_page_dirty_nobuffers(page); |
| 8603 | } |
| 8604 | |
Al Viro | 10556cb2 | 2011-06-20 19:28:19 -0400 | [diff] [blame] | 8605 | static int btrfs_permission(struct inode *inode, int mask) |
Yan | fdebe2b | 2008-01-14 13:26:08 -0500 | [diff] [blame] | 8606 | { |
Li Zefan | b83cc96 | 2010-12-20 16:04:08 +0800 | [diff] [blame] | 8607 | struct btrfs_root *root = BTRFS_I(inode)->root; |
Jeff Mahoney | cb6db4e | 2011-08-15 17:27:21 +0000 | [diff] [blame] | 8608 | umode_t mode = inode->i_mode; |
Li Zefan | b83cc96 | 2010-12-20 16:04:08 +0800 | [diff] [blame] | 8609 | |
Jeff Mahoney | cb6db4e | 2011-08-15 17:27:21 +0000 | [diff] [blame] | 8610 | if (mask & MAY_WRITE && |
| 8611 | (S_ISREG(mode) || S_ISDIR(mode) || S_ISLNK(mode))) { |
| 8612 | if (btrfs_root_readonly(root)) |
| 8613 | return -EROFS; |
| 8614 | if (BTRFS_I(inode)->flags & BTRFS_INODE_READONLY) |
| 8615 | return -EACCES; |
| 8616 | } |
Al Viro | 2830ba7 | 2011-06-20 19:16:29 -0400 | [diff] [blame] | 8617 | return generic_permission(inode, mask); |
Yan | fdebe2b | 2008-01-14 13:26:08 -0500 | [diff] [blame] | 8618 | } |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 8619 | |
Alexey Dobriyan | 6e1d5dc | 2009-09-21 17:01:11 -0700 | [diff] [blame] | 8620 | static const struct inode_operations btrfs_dir_inode_operations = { |
Chris Mason | 3394e16 | 2008-11-17 20:42:26 -0500 | [diff] [blame] | 8621 | .getattr = btrfs_getattr, |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 8622 | .lookup = btrfs_lookup, |
| 8623 | .create = btrfs_create, |
| 8624 | .unlink = btrfs_unlink, |
| 8625 | .link = btrfs_link, |
| 8626 | .mkdir = btrfs_mkdir, |
| 8627 | .rmdir = btrfs_rmdir, |
| 8628 | .rename = btrfs_rename, |
| 8629 | .symlink = btrfs_symlink, |
| 8630 | .setattr = btrfs_setattr, |
Josef Bacik | 618e21d | 2007-07-11 10:18:17 -0400 | [diff] [blame] | 8631 | .mknod = btrfs_mknod, |
Christoph Hellwig | 95819c0 | 2008-08-28 06:21:17 -0400 | [diff] [blame] | 8632 | .setxattr = btrfs_setxattr, |
| 8633 | .getxattr = btrfs_getxattr, |
Josef Bacik | 5103e94 | 2007-11-16 11:45:54 -0500 | [diff] [blame] | 8634 | .listxattr = btrfs_listxattr, |
Christoph Hellwig | 95819c0 | 2008-08-28 06:21:17 -0400 | [diff] [blame] | 8635 | .removexattr = btrfs_removexattr, |
Yan | fdebe2b | 2008-01-14 13:26:08 -0500 | [diff] [blame] | 8636 | .permission = btrfs_permission, |
Christoph Hellwig | 4e34e71 | 2011-07-23 17:37:31 +0200 | [diff] [blame] | 8637 | .get_acl = btrfs_get_acl, |
Guangyu Sun | 93fd63c | 2013-09-16 10:42:03 -0700 | [diff] [blame] | 8638 | .update_time = btrfs_update_time, |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 8639 | }; |
Alexey Dobriyan | 6e1d5dc | 2009-09-21 17:01:11 -0700 | [diff] [blame] | 8640 | static const struct inode_operations btrfs_dir_ro_inode_operations = { |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 8641 | .lookup = btrfs_lookup, |
Yan | fdebe2b | 2008-01-14 13:26:08 -0500 | [diff] [blame] | 8642 | .permission = btrfs_permission, |
Christoph Hellwig | 4e34e71 | 2011-07-23 17:37:31 +0200 | [diff] [blame] | 8643 | .get_acl = btrfs_get_acl, |
Guangyu Sun | 93fd63c | 2013-09-16 10:42:03 -0700 | [diff] [blame] | 8644 | .update_time = btrfs_update_time, |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 8645 | }; |
Yan, Zheng | 76dda93 | 2009-09-21 16:00:26 -0400 | [diff] [blame] | 8646 | |
Alexey Dobriyan | 828c095 | 2009-10-01 15:43:56 -0700 | [diff] [blame] | 8647 | static const struct file_operations btrfs_dir_file_operations = { |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 8648 | .llseek = generic_file_llseek, |
| 8649 | .read = generic_read_dir, |
Al Viro | 9cdda8d | 2013-05-22 16:48:09 -0400 | [diff] [blame] | 8650 | .iterate = btrfs_real_readdir, |
Christoph Hellwig | 34287aa | 2007-09-14 10:22:47 -0400 | [diff] [blame] | 8651 | .unlocked_ioctl = btrfs_ioctl, |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 8652 | #ifdef CONFIG_COMPAT |
Christoph Hellwig | 34287aa | 2007-09-14 10:22:47 -0400 | [diff] [blame] | 8653 | .compat_ioctl = btrfs_ioctl, |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 8654 | #endif |
Sage Weil | 6bf13c0 | 2008-06-10 10:07:39 -0400 | [diff] [blame] | 8655 | .release = btrfs_release_file, |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 8656 | .fsync = btrfs_sync_file, |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 8657 | }; |
| 8658 | |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 8659 | static struct extent_io_ops btrfs_extent_io_ops = { |
Chris Mason | 07157aa | 2007-08-30 08:50:51 -0400 | [diff] [blame] | 8660 | .fill_delalloc = run_delalloc_range, |
Chris Mason | 065631f | 2008-02-20 12:07:25 -0500 | [diff] [blame] | 8661 | .submit_bio_hook = btrfs_submit_bio_hook, |
Chris Mason | 239b14b | 2008-03-24 15:02:07 -0400 | [diff] [blame] | 8662 | .merge_bio_hook = btrfs_merge_bio_hook, |
Chris Mason | 07157aa | 2007-08-30 08:50:51 -0400 | [diff] [blame] | 8663 | .readpage_end_io_hook = btrfs_readpage_end_io_hook, |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 8664 | .writepage_end_io_hook = btrfs_writepage_end_io_hook, |
Chris Mason | 247e743 | 2008-07-17 12:53:51 -0400 | [diff] [blame] | 8665 | .writepage_start_hook = btrfs_writepage_start_hook, |
Chris Mason | b0c68f8 | 2008-01-31 11:05:37 -0500 | [diff] [blame] | 8666 | .set_bit_hook = btrfs_set_bit_hook, |
| 8667 | .clear_bit_hook = btrfs_clear_bit_hook, |
Josef Bacik | 9ed74f2 | 2009-09-11 16:12:44 -0400 | [diff] [blame] | 8668 | .merge_extent_hook = btrfs_merge_extent_hook, |
| 8669 | .split_extent_hook = btrfs_split_extent_hook, |
Chris Mason | 07157aa | 2007-08-30 08:50:51 -0400 | [diff] [blame] | 8670 | }; |
| 8671 | |
Chris Mason | 3505439 | 2009-01-21 13:11:13 -0500 | [diff] [blame] | 8672 | /* |
| 8673 | * btrfs doesn't support the bmap operation because swapfiles |
| 8674 | * use bmap to make a mapping of extents in the file. They assume |
| 8675 | * these extents won't change over the life of the file and they |
| 8676 | * use the bmap result to do IO directly to the drive. |
| 8677 | * |
| 8678 | * the btrfs bmap call would return logical addresses that aren't |
| 8679 | * suitable for IO and they also will change frequently as COW |
| 8680 | * operations happen. So, swapfile + btrfs == corruption. |
| 8681 | * |
| 8682 | * For now we're avoiding this by dropping bmap. |
| 8683 | */ |
Alexey Dobriyan | 7f09410 | 2009-09-21 17:01:10 -0700 | [diff] [blame] | 8684 | static const struct address_space_operations btrfs_aops = { |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 8685 | .readpage = btrfs_readpage, |
| 8686 | .writepage = btrfs_writepage, |
Chris Mason | b293f02e | 2007-11-01 19:45:34 -0400 | [diff] [blame] | 8687 | .writepages = btrfs_writepages, |
Chris Mason | 3ab2fb5 | 2007-11-08 10:59:22 -0500 | [diff] [blame] | 8688 | .readpages = btrfs_readpages, |
Chris Mason | 1643298 | 2008-04-10 10:23:21 -0400 | [diff] [blame] | 8689 | .direct_IO = btrfs_direct_IO, |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 8690 | .invalidatepage = btrfs_invalidatepage, |
| 8691 | .releasepage = btrfs_releasepage, |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 8692 | .set_page_dirty = btrfs_set_page_dirty, |
Andi Kleen | 465fdd9 | 2009-09-16 11:50:18 +0200 | [diff] [blame] | 8693 | .error_remove_page = generic_error_remove_page, |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 8694 | }; |
| 8695 | |
Alexey Dobriyan | 7f09410 | 2009-09-21 17:01:10 -0700 | [diff] [blame] | 8696 | static const struct address_space_operations btrfs_symlink_aops = { |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 8697 | .readpage = btrfs_readpage, |
| 8698 | .writepage = btrfs_writepage, |
Chris Mason | 2bf5a72 | 2007-08-30 11:54:02 -0400 | [diff] [blame] | 8699 | .invalidatepage = btrfs_invalidatepage, |
| 8700 | .releasepage = btrfs_releasepage, |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 8701 | }; |
| 8702 | |
Alexey Dobriyan | 6e1d5dc | 2009-09-21 17:01:11 -0700 | [diff] [blame] | 8703 | static const struct inode_operations btrfs_file_inode_operations = { |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 8704 | .getattr = btrfs_getattr, |
| 8705 | .setattr = btrfs_setattr, |
Christoph Hellwig | 95819c0 | 2008-08-28 06:21:17 -0400 | [diff] [blame] | 8706 | .setxattr = btrfs_setxattr, |
| 8707 | .getxattr = btrfs_getxattr, |
Josef Bacik | 5103e94 | 2007-11-16 11:45:54 -0500 | [diff] [blame] | 8708 | .listxattr = btrfs_listxattr, |
Christoph Hellwig | 95819c0 | 2008-08-28 06:21:17 -0400 | [diff] [blame] | 8709 | .removexattr = btrfs_removexattr, |
Yan | fdebe2b | 2008-01-14 13:26:08 -0500 | [diff] [blame] | 8710 | .permission = btrfs_permission, |
Yehuda Sadeh | 1506fcc | 2009-01-21 14:39:14 -0500 | [diff] [blame] | 8711 | .fiemap = btrfs_fiemap, |
Christoph Hellwig | 4e34e71 | 2011-07-23 17:37:31 +0200 | [diff] [blame] | 8712 | .get_acl = btrfs_get_acl, |
Josef Bacik | e41f941 | 2012-03-26 09:46:47 -0400 | [diff] [blame] | 8713 | .update_time = btrfs_update_time, |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 8714 | }; |
Alexey Dobriyan | 6e1d5dc | 2009-09-21 17:01:11 -0700 | [diff] [blame] | 8715 | static const struct inode_operations btrfs_special_inode_operations = { |
Josef Bacik | 618e21d | 2007-07-11 10:18:17 -0400 | [diff] [blame] | 8716 | .getattr = btrfs_getattr, |
| 8717 | .setattr = btrfs_setattr, |
Yan | fdebe2b | 2008-01-14 13:26:08 -0500 | [diff] [blame] | 8718 | .permission = btrfs_permission, |
Christoph Hellwig | 95819c0 | 2008-08-28 06:21:17 -0400 | [diff] [blame] | 8719 | .setxattr = btrfs_setxattr, |
| 8720 | .getxattr = btrfs_getxattr, |
Josef Bacik | 33268ea | 2008-07-24 12:16:36 -0400 | [diff] [blame] | 8721 | .listxattr = btrfs_listxattr, |
Christoph Hellwig | 95819c0 | 2008-08-28 06:21:17 -0400 | [diff] [blame] | 8722 | .removexattr = btrfs_removexattr, |
Christoph Hellwig | 4e34e71 | 2011-07-23 17:37:31 +0200 | [diff] [blame] | 8723 | .get_acl = btrfs_get_acl, |
Josef Bacik | e41f941 | 2012-03-26 09:46:47 -0400 | [diff] [blame] | 8724 | .update_time = btrfs_update_time, |
Josef Bacik | 618e21d | 2007-07-11 10:18:17 -0400 | [diff] [blame] | 8725 | }; |
Alexey Dobriyan | 6e1d5dc | 2009-09-21 17:01:11 -0700 | [diff] [blame] | 8726 | static const struct inode_operations btrfs_symlink_inode_operations = { |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 8727 | .readlink = generic_readlink, |
| 8728 | .follow_link = page_follow_link_light, |
| 8729 | .put_link = page_put_link, |
Li Zefan | f209561 | 2010-11-19 02:05:24 +0000 | [diff] [blame] | 8730 | .getattr = btrfs_getattr, |
Josef Bacik | 22c44fe | 2011-11-30 10:45:38 -0500 | [diff] [blame] | 8731 | .setattr = btrfs_setattr, |
Yan | fdebe2b | 2008-01-14 13:26:08 -0500 | [diff] [blame] | 8732 | .permission = btrfs_permission, |
Jim Owens | 0279b4c | 2009-02-04 09:29:13 -0500 | [diff] [blame] | 8733 | .setxattr = btrfs_setxattr, |
| 8734 | .getxattr = btrfs_getxattr, |
| 8735 | .listxattr = btrfs_listxattr, |
| 8736 | .removexattr = btrfs_removexattr, |
Christoph Hellwig | 4e34e71 | 2011-07-23 17:37:31 +0200 | [diff] [blame] | 8737 | .get_acl = btrfs_get_acl, |
Josef Bacik | e41f941 | 2012-03-26 09:46:47 -0400 | [diff] [blame] | 8738 | .update_time = btrfs_update_time, |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 8739 | }; |
Yan, Zheng | 76dda93 | 2009-09-21 16:00:26 -0400 | [diff] [blame] | 8740 | |
Alexey Dobriyan | 82d339d | 2009-10-09 09:54:36 -0400 | [diff] [blame] | 8741 | const struct dentry_operations btrfs_dentry_operations = { |
Yan, Zheng | 76dda93 | 2009-09-21 16:00:26 -0400 | [diff] [blame] | 8742 | .d_delete = btrfs_dentry_delete, |
Josef Bacik | b4aff1f | 2011-06-28 16:18:59 -0400 | [diff] [blame] | 8743 | .d_release = btrfs_dentry_release, |
Yan, Zheng | 76dda93 | 2009-09-21 16:00:26 -0400 | [diff] [blame] | 8744 | }; |